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