]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemoryProfileLib.c
32ef22256ff216a62d8bccf8e1b467af79b40651
[mirror_edk2.git] / MdeModulePkg / Library / PiSmmCoreMemoryAllocationLib / PiSmmCoreMemoryProfileLib.c
1 /** @file
2 Support routines for memory profile for PiSmmCore.
3
4 Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <PiSmm.h>
10
11 #include <Library/UefiBootServicesTableLib.h>
12 #include <Library/DebugLib.h>
13
14 #include <Guid/MemoryProfile.h>
15
16 #include "PiSmmCoreMemoryProfileServices.h"
17
18 EDKII_MEMORY_PROFILE_PROTOCOL *mLibProfileProtocol;
19
20 /**
21 Check whether the start address of buffer is within any of the SMRAM ranges.
22
23 @param[in] Buffer The pointer to the buffer to be checked.
24
25 @retval TRUE The buffer is in SMRAM ranges.
26 @retval FALSE The buffer is out of SMRAM ranges.
27 **/
28 BOOLEAN
29 EFIAPI
30 BufferInSmram (
31 IN VOID *Buffer
32 );
33
34 /**
35 The constructor function initializes memory profile for SMM phase.
36
37 @param ImageHandle The firmware allocated handle for the EFI image.
38 @param SystemTable A pointer to the EFI System Table.
39
40 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
41
42 **/
43 EFI_STATUS
44 EFIAPI
45 PiSmmCoreMemoryProfileLibConstructor (
46 IN EFI_HANDLE ImageHandle,
47 IN EFI_SYSTEM_TABLE *SystemTable
48 )
49 {
50 EFI_STATUS Status;
51
52 //
53 // Locate Profile Protocol
54 //
55 Status = gBS->LocateProtocol (
56 &gEdkiiMemoryProfileGuid,
57 NULL,
58 (VOID **)&mLibProfileProtocol
59 );
60 if (EFI_ERROR (Status)) {
61 mLibProfileProtocol = NULL;
62 }
63
64 return EFI_SUCCESS;
65 }
66
67 /**
68 Record memory profile of multilevel caller.
69
70 @param[in] CallerAddress Address of caller.
71 @param[in] Action Memory profile action.
72 @param[in] MemoryType Memory type.
73 EfiMaxMemoryType means the MemoryType is unknown.
74 @param[in] Buffer Buffer address.
75 @param[in] Size Buffer size.
76 @param[in] ActionString String for memory profile action.
77 Only needed for user defined allocate action.
78
79 @return EFI_SUCCESS Memory profile is updated.
80 @return EFI_UNSUPPORTED Memory profile is unsupported,
81 or memory profile for the image is not required,
82 or memory profile for the memory type is not required.
83 @return EFI_ACCESS_DENIED It is during memory profile data getting.
84 @return EFI_ABORTED Memory profile recording is not enabled.
85 @return EFI_OUT_OF_RESOURCES No enough resource to update memory profile for allocate action.
86 @return EFI_NOT_FOUND No matched allocate info found for free action.
87
88 **/
89 EFI_STATUS
90 EFIAPI
91 MemoryProfileLibRecord (
92 IN PHYSICAL_ADDRESS CallerAddress,
93 IN MEMORY_PROFILE_ACTION Action,
94 IN EFI_MEMORY_TYPE MemoryType,
95 IN VOID *Buffer,
96 IN UINTN Size,
97 IN CHAR8 *ActionString OPTIONAL
98 )
99 {
100 if (BufferInSmram (Buffer)) {
101 return SmmCoreUpdateProfile (CallerAddress, Action, MemoryType, Size, Buffer, ActionString);
102 } else {
103 if (mLibProfileProtocol == NULL) {
104 return EFI_UNSUPPORTED;
105 }
106 return mLibProfileProtocol->Record (
107 mLibProfileProtocol,
108 CallerAddress,
109 Action,
110 MemoryType,
111 Buffer,
112 Size,
113 ActionString
114 );
115 }
116 }
117