1 /** @file 2 Support routines for memory profile for Smm phase drivers. 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/SmmServicesTableLib.h> 19 #include <Library/DebugLib.h> 20 21 #include <Guid/MemoryProfile.h> 22 23 EDKII_MEMORY_PROFILE_PROTOCOL *mLibProfileProtocol; 24 EDKII_SMM_MEMORY_PROFILE_PROTOCOL *mLibSmmProfileProtocol; 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 TRUE 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 SmmMemoryProfileLibConstructor ( 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 Status = gSmst->SmmLocateProtocol ( 71 &gEdkiiSmmMemoryProfileGuid, 72 NULL, 73 (VOID **)&mLibSmmProfileProtocol 74 ); 75 if (EFI_ERROR (Status)) { 76 mLibSmmProfileProtocol = NULL; 77 } 78 79 return EFI_SUCCESS; 80 } 81 82 /** 83 Record memory profile of multilevel caller. 84 85 @param[in] CallerAddress Address of caller. 86 @param[in] Action Memory profile action. 87 @param[in] MemoryType Memory type. 88 EfiMaxMemoryType means the MemoryType is unknown. 89 @param[in] Buffer Buffer address. 90 @param[in] Size Buffer size. 91 @param[in] ActionString String for memory profile action. 92 Only needed for user defined allocate action. 93 94 @return EFI_SUCCESS Memory profile is updated. 95 @return EFI_UNSUPPORTED Memory profile is unsupported, 96 or memory profile for the image is not required, 97 or memory profile for the memory type is not required. 98 @return EFI_ACCESS_DENIED It is during memory profile data getting. 99 @return EFI_ABORTED Memory profile recording is not enabled. 100 @return EFI_OUT_OF_RESOURCES No enough resource to update memory profile for allocate action. 101 @return EFI_NOT_FOUND No matched allocate info found for free action. 102 103 **/ 104 EFI_STATUS 105 EFIAPI 106 MemoryProfileLibRecord ( 107 IN PHYSICAL_ADDRESS CallerAddress, 108 IN MEMORY_PROFILE_ACTION Action, 109 IN EFI_MEMORY_TYPE MemoryType, 110 IN VOID *Buffer, 111 IN UINTN Size, 112 IN CHAR8 *ActionString OPTIONAL 113 ) 114 { 115 if (BufferInSmram (Buffer)) { 116 if (mLibSmmProfileProtocol == NULL) { 117 return EFI_UNSUPPORTED; 118 } 119 return mLibSmmProfileProtocol->Record ( 120 mLibSmmProfileProtocol, 121 CallerAddress, 122 Action, 123 MemoryType, 124 Buffer, 125 Size, 126 ActionString 127 ); 128 } else { 129 if (mLibProfileProtocol == NULL) { 130 return EFI_UNSUPPORTED; 131 } 132 return mLibProfileProtocol->Record ( 133 mLibProfileProtocol, 134 CallerAddress, 135 Action, 136 MemoryType, 137 Buffer, 138 Size, 139 ActionString 140 ); 141 } 142 } 143 144