--- /dev/null
+/** @file\r
+ Support routines for memory profile for PiSmmCore.\r
+\r
+ Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
+ This program and the accompanying materials \r
+ are licensed and made available under the terms and conditions of the BSD License \r
+ which accompanies this distribution. The full text of the license may be found at \r
+ http://opensource.org/licenses/bsd-license.php. \r
+\r
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+\r
+**/\r
+\r
+#include <PiSmm.h>\r
+\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/DebugLib.h>\r
+\r
+#include <Guid/MemoryProfile.h>\r
+\r
+#include "PiSmmCoreMemoryProfileServices.h"\r
+\r
+EDKII_MEMORY_PROFILE_PROTOCOL *mLibProfileProtocol;\r
+\r
+/**\r
+ Check whether the start address of buffer is within any of the SMRAM ranges.\r
+\r
+ @param[in] Buffer The pointer to the buffer to be checked.\r
+\r
+ @retval TURE The buffer is in SMRAM ranges.\r
+ @retval FALSE The buffer is out of SMRAM ranges.\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+BufferInSmram (\r
+ IN VOID *Buffer\r
+ );\r
+\r
+/**\r
+ The constructor function initializes memory profile for SMM phase.\r
+\r
+ @param ImageHandle The firmware allocated handle for the EFI image.\r
+ @param SystemTable A pointer to the EFI System Table.\r
+\r
+ @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PiSmmCoreMemoryProfileLibConstructor (\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE *SystemTable\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Locate Profile Protocol\r
+ //\r
+ Status = gBS->LocateProtocol (\r
+ &gEdkiiMemoryProfileGuid,\r
+ NULL,\r
+ (VOID **)&mLibProfileProtocol\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ mLibProfileProtocol = NULL;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Record memory profile of multilevel caller.\r
+\r
+ @param[in] CallerAddress Address of caller.\r
+ @param[in] Action Memory profile action.\r
+ @param[in] MemoryType Memory type.\r
+ EfiMaxMemoryType means the MemoryType is unknown.\r
+ @param[in] Buffer Buffer address.\r
+ @param[in] Size Buffer size.\r
+ @param[in] ActionString String for memory profile action.\r
+ Only needed for user defined allocate action.\r
+\r
+ @return EFI_SUCCESS Memory profile is updated.\r
+ @return EFI_UNSUPPORTED Memory profile is unsupported,\r
+ or memory profile for the image is not required,\r
+ or memory profile for the memory type is not required.\r
+ @return EFI_ACCESS_DENIED It is during memory profile data getting.\r
+ @return EFI_ABORTED Memory profile recording is not enabled.\r
+ @return EFI_OUT_OF_RESOURCES No enough resource to update memory profile for allocate action.\r
+ @return EFI_NOT_FOUND No matched allocate info found for free action.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+MemoryProfileLibRecord (\r
+ IN PHYSICAL_ADDRESS CallerAddress,\r
+ IN MEMORY_PROFILE_ACTION Action,\r
+ IN EFI_MEMORY_TYPE MemoryType,\r
+ IN VOID *Buffer,\r
+ IN UINTN Size,\r
+ IN CHAR8 *ActionString OPTIONAL\r
+ )\r
+{\r
+ if (BufferInSmram (Buffer)) {\r
+ return SmmCoreUpdateProfile (CallerAddress, Action, MemoryType, Size, Buffer, ActionString);\r
+ } else {\r
+ if (mLibProfileProtocol == NULL) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ return mLibProfileProtocol->Record (\r
+ mLibProfileProtocol,\r
+ CallerAddress,\r
+ Action,\r
+ MemoryType,\r
+ Buffer,\r
+ Size,\r
+ ActionString\r
+ );\r
+ }\r
+}\r
+\r