]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/MemoryAllocationLib.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Library / PiSmmCoreMemoryAllocationLib / MemoryAllocationLib.c
index e4f7756f72e108bbb220feff9e5a30ac5ff35040..895fa2062a8a37303149bac940fdb8ab8a21d955 100644 (file)
@@ -1,79 +1,46 @@
 /** @file\r
-  Support routines for memory allocation routines based on SMM Core internal functions.\r
-\r
-  Copyright (c) 2006 - 2015, 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
+  Support routines for memory allocation routines based on SMM Core internal functions,\r
+  with memory profile support.\r
+\r
+  The PI System Management Mode Core Interface Specification only allows the use\r
+  of EfiRuntimeServicesCode and EfiRuntimeServicesData memory types for memory\r
+  allocations as the SMRAM space should be reserved after BDS phase.  The functions\r
+  in the Memory Allocation Library use EfiBootServicesData as the default memory\r
+  allocation type.  For this SMM specific instance of the Memory Allocation Library,\r
+  EfiRuntimeServicesData is used as the default memory type for all allocations.\r
+  In addition, allocation for the Reserved memory types are not supported and will\r
+  always return NULL.\r
+\r
+  Copyright (c) 2006 - 2018, 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 <Protocol/SmmAccess2.h>\r
 #include <Library/MemoryAllocationLib.h>\r
 #include <Library/UefiBootServicesTableLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/DebugLib.h>\r
 #include "PiSmmCoreMemoryAllocationServices.h"\r
 \r
-EFI_SMRAM_DESCRIPTOR  *mSmramRanges    = NULL;\r
-UINTN                 mSmramRangeCount = 0;\r
-\r
-/**\r
-  This function gets and caches SMRAM ranges that are present in the system.\r
-    \r
-  It will ASSERT() if SMM Access2 Protocol doesn't exist.\r
-  It will ASSERT() if SMRAM ranges can't be got.\r
-  It will ASSERT() if Resource can't be allocated for cache SMRAM range. \r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-GetSmramRanges (\r
-  VOID\r
-  )\r
-{\r
-  EFI_STATUS                Status;\r
-  EFI_SMM_ACCESS2_PROTOCOL  *SmmAccess;\r
-  UINTN                     Size;\r
-\r
-  //\r
-  // Locate SMM Access2 Protocol\r
-  //\r
-  Status = gBS->LocateProtocol (\r
-                  &gEfiSmmAccess2ProtocolGuid, \r
-                  NULL, \r
-                  (VOID **)&SmmAccess\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // Get SMRAM range information\r
-  //\r
-  Size = 0;\r
-  Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r
-  ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
+#include <Library/MemoryProfileLib.h>\r
 \r
-  mSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);\r
-  ASSERT (mSmramRanges != NULL);\r
-\r
-  Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
-}\r
+EFI_SMRAM_DESCRIPTOR  *mSmmCoreMemoryAllocLibSmramRanges    = NULL;\r
+UINTN                 mSmmCoreMemoryAllocLibSmramRangeCount = 0;\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     TRUE     The buffer is in SMRAM ranges.\r
   @retval     FALSE    The buffer is out of SMRAM ranges.\r
 **/\r
 BOOLEAN\r
@@ -84,16 +51,9 @@ BufferInSmram (
 {\r
   UINTN  Index;\r
 \r
-  if (mSmramRanges == NULL) {\r
-    //\r
-    // SMRAM ranges is not got. Try to get them all.\r
-    //\r
-    GetSmramRanges();\r
-  }\r
-\r
-  for (Index = 0; Index < mSmramRangeCount; Index ++) {\r
-    if (((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer >= mSmramRanges[Index].CpuStart) && \r
-        ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer < (mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize))) {\r
+  for (Index = 0; Index < mSmmCoreMemoryAllocLibSmramRangeCount; Index ++) {\r
+    if (((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer >= mSmmCoreMemoryAllocLibSmramRanges[Index].CpuStart) &&\r
+        ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer < (mSmmCoreMemoryAllocLibSmramRanges[Index].CpuStart + mSmmCoreMemoryAllocLibSmramRanges[Index].PhysicalSize))) {\r
       return TRUE;\r
     }\r
   }\r
@@ -116,12 +76,12 @@ BufferInSmram (
 **/\r
 VOID *\r
 InternalAllocatePages (\r
-  IN EFI_MEMORY_TYPE  MemoryType,  \r
+  IN EFI_MEMORY_TYPE  MemoryType,\r
   IN UINTN            Pages\r
   )\r
 {\r
   EFI_STATUS            Status;\r
-  EFI_PHYSICAL_ADDRESS  Memory; \r
+  EFI_PHYSICAL_ADDRESS  Memory;\r
 \r
   if (Pages == 0) {\r
     return NULL;\r
@@ -135,9 +95,9 @@ InternalAllocatePages (
 }\r
 \r
 /**\r
-  Allocates one or more 4KB pages of type EfiBootServicesData.\r
+  Allocates one or more 4KB pages of type EfiRuntimeServicesData.\r
 \r
-  Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the\r
+  Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r
   allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If Pages is 0, then NULL\r
   is returned.  If there is not enough memory remaining to satisfy the request, then NULL is\r
   returned.\r
@@ -153,7 +113,20 @@ AllocatePages (
   IN UINTN  Pages\r
   )\r
 {\r
-  return InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
+  VOID  *Buffer;\r
+\r
+  Buffer = InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
+  if (Buffer != NULL) {\r
+    MemoryProfileLibRecord (\r
+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_PAGES,\r
+      EfiRuntimeServicesData,\r
+      Buffer,\r
+      EFI_PAGES_TO_SIZE(Pages),\r
+      NULL\r
+      );\r
+  }\r
+  return Buffer;\r
 }\r
 \r
 /**\r
@@ -175,7 +148,20 @@ AllocateRuntimePages (
   IN UINTN  Pages\r
   )\r
 {\r
-  return InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
+  VOID  *Buffer;\r
+\r
+  Buffer = InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
+  if (Buffer != NULL) {\r
+    MemoryProfileLibRecord (\r
+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_PAGES,\r
+      EfiRuntimeServicesData,\r
+      Buffer,\r
+      EFI_PAGES_TO_SIZE(Pages),\r
+      NULL\r
+      );\r
+  }\r
+  return Buffer;\r
 }\r
 \r
 /**\r
@@ -208,11 +194,11 @@ AllocateReservedPages (
   must have been allocated on a previous call to the page allocation services of the Memory\r
   Allocation Library.  If it is not possible to free allocated pages, then this function will\r
   perform no actions.\r
-  \r
+\r
   If Buffer was not allocated with a page allocation function in the Memory Allocation Library,\r
   then ASSERT().\r
   If Pages is zero, then ASSERT().\r
\r
+\r
   @param  Buffer                Pointer to the buffer of pages to free.\r
   @param  Pages                 The number of 4 KB pages to free.\r
 \r
@@ -263,7 +249,7 @@ FreePages (
 **/\r
 VOID *\r
 InternalAllocateAlignedPages (\r
-  IN EFI_MEMORY_TYPE  MemoryType,  \r
+  IN EFI_MEMORY_TYPE  MemoryType,\r
   IN UINTN            Pages,\r
   IN UINTN            Alignment\r
   )\r
@@ -279,13 +265,13 @@ InternalAllocateAlignedPages (
   // Alignment must be a power of two or zero.\r
   //\r
   ASSERT ((Alignment & (Alignment - 1)) == 0);\r
\r
+\r
   if (Pages == 0) {\r
     return NULL;\r
   }\r
   if (Alignment > EFI_PAGE_SIZE) {\r
     //\r
-    // Caculate the total number of pages since alignment is larger than page size.\r
+    // Calculate the total number of pages since alignment is larger than page size.\r
     //\r
     AlignmentMask  = Alignment - 1;\r
     RealPages      = Pages + EFI_SIZE_TO_PAGES (Alignment);\r
@@ -293,7 +279,7 @@ InternalAllocateAlignedPages (
     // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
     //\r
     ASSERT (RealPages > Pages);\r
\r
+\r
     Status         = SmmAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);\r
     if (EFI_ERROR (Status)) {\r
       return NULL;\r
@@ -307,7 +293,7 @@ InternalAllocateAlignedPages (
       Status = SmmFreePages (Memory, UnalignedPages);\r
       ASSERT_EFI_ERROR (Status);\r
     }\r
-    Memory         = (EFI_PHYSICAL_ADDRESS) (AlignedMemory + EFI_PAGES_TO_SIZE (Pages));\r
+    Memory         = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);\r
     UnalignedPages = RealPages - Pages - UnalignedPages;\r
     if (UnalignedPages > 0) {\r
       //\r
@@ -330,13 +316,13 @@ InternalAllocateAlignedPages (
 }\r
 \r
 /**\r
-  Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r
+  Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.\r
 \r
-  Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r
+  Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an\r
   alignment specified by Alignment.  The allocated buffer is returned.  If Pages is 0, then NULL is\r
   returned.  If there is not enough memory at the specified alignment remaining to satisfy the\r
   request, then NULL is returned.\r
-  \r
+\r
   If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
   If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
 \r
@@ -354,7 +340,20 @@ AllocateAlignedPages (
   IN UINTN  Alignment\r
   )\r
 {\r
-  return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
+  VOID  *Buffer;\r
+\r
+  Buffer = InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
+  if (Buffer != NULL) {\r
+    MemoryProfileLibRecord (\r
+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_PAGES,\r
+      EfiRuntimeServicesData,\r
+      Buffer,\r
+      EFI_PAGES_TO_SIZE(Pages),\r
+      NULL\r
+      );\r
+  }\r
+  return Buffer;\r
 }\r
 \r
 /**\r
@@ -364,7 +363,7 @@ AllocateAlignedPages (
   alignment specified by Alignment.  The allocated buffer is returned.  If Pages is 0, then NULL is\r
   returned.  If there is not enough memory at the specified alignment remaining to satisfy the\r
   request, then NULL is returned.\r
-  \r
+\r
   If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
   If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
 \r
@@ -382,7 +381,20 @@ AllocateAlignedRuntimePages (
   IN UINTN  Alignment\r
   )\r
 {\r
-  return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
+  VOID  *Buffer;\r
+\r
+  Buffer = InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
+  if (Buffer != NULL) {\r
+    MemoryProfileLibRecord (\r
+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RUNTIME_PAGES,\r
+      EfiRuntimeServicesData,\r
+      Buffer,\r
+      EFI_PAGES_TO_SIZE(Pages),\r
+      NULL\r
+      );\r
+  }\r
+  return Buffer;\r
 }\r
 \r
 /**\r
@@ -392,7 +404,7 @@ AllocateAlignedRuntimePages (
   alignment specified by Alignment.  The allocated buffer is returned.  If Pages is 0, then NULL is\r
   returned.  If there is not enough memory at the specified alignment remaining to satisfy the\r
   request, then NULL is returned.\r
-  \r
+\r
   If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
   If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
 \r
@@ -419,13 +431,13 @@ AllocateAlignedReservedPages (
 \r
   Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer.  Buffer\r
   must have been allocated on a previous call to the aligned page allocation services of the Memory\r
-  Allocation Library.  If it is not possible to free allocated pages, then this function will \r
+  Allocation Library.  If it is not possible to free allocated pages, then this function will\r
   perform no actions.\r
-  \r
+\r
   If Buffer was not allocated with an aligned page allocation function in the Memory Allocation\r
   Library, then ASSERT().\r
   If Pages is zero, then ASSERT().\r
-  \r
+\r
   @param  Buffer                Pointer to the buffer of pages to free.\r
   @param  Pages                 The number of 4 KB pages to free.\r
 \r
@@ -471,7 +483,7 @@ FreeAlignedPages (
 **/\r
 VOID *\r
 InternalAllocatePool (\r
-  IN EFI_MEMORY_TYPE  MemoryType,  \r
+  IN EFI_MEMORY_TYPE  MemoryType,\r
   IN UINTN            AllocationSize\r
   )\r
 {\r
@@ -488,9 +500,9 @@ InternalAllocatePool (
 }\r
 \r
 /**\r
-  Allocates a buffer of type EfiBootServicesData.\r
+  Allocates a buffer of type EfiRuntimeServicesData.\r
 \r
-  Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
+  Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns a\r
   pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is\r
   returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
 \r
@@ -505,7 +517,20 @@ AllocatePool (
   IN UINTN  AllocationSize\r
   )\r
 {\r
-  return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
+  VOID  *Buffer;\r
+\r
+  Buffer = InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
+  if (Buffer != NULL) {\r
+    MemoryProfileLibRecord (\r
+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_POOL,\r
+      EfiRuntimeServicesData,\r
+      Buffer,\r
+      AllocationSize,\r
+      NULL\r
+      );\r
+  }\r
+  return Buffer;\r
 }\r
 \r
 /**\r
@@ -526,7 +551,20 @@ AllocateRuntimePool (
   IN UINTN  AllocationSize\r
   )\r
 {\r
-  return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
+  VOID  *Buffer;\r
+\r
+  Buffer = InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
+  if (Buffer != NULL) {\r
+    MemoryProfileLibRecord (\r
+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_POOL,\r
+      EfiRuntimeServicesData,\r
+      Buffer,\r
+      AllocationSize,\r
+      NULL\r
+      );\r
+  }\r
+  return Buffer;\r
 }\r
 \r
 /**\r
@@ -566,9 +604,9 @@ AllocateReservedPool (
 **/\r
 VOID *\r
 InternalAllocateZeroPool (\r
-  IN EFI_MEMORY_TYPE  PoolType,  \r
+  IN EFI_MEMORY_TYPE  PoolType,\r
   IN UINTN            AllocationSize\r
-  ) \r
+  )\r
 {\r
   VOID  *Memory;\r
 \r
@@ -580,9 +618,9 @@ InternalAllocateZeroPool (
 }\r
 \r
 /**\r
-  Allocates and zeros a buffer of type EfiBootServicesData.\r
+  Allocates and zeros a buffer of type EfiRuntimeServicesData.\r
 \r
-  Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
+  Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the\r
   buffer with zeros, and returns a pointer to the allocated buffer.  If AllocationSize is 0, then a\r
   valid buffer of 0 size is returned.  If there is not enough memory remaining to satisfy the\r
   request, then NULL is returned.\r
@@ -598,7 +636,20 @@ AllocateZeroPool (
   IN UINTN  AllocationSize\r
   )\r
 {\r
-  return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
+  VOID  *Buffer;\r
+\r
+  Buffer = InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
+  if (Buffer != NULL) {\r
+    MemoryProfileLibRecord (\r
+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ZERO_POOL,\r
+      EfiRuntimeServicesData,\r
+      Buffer,\r
+      AllocationSize,\r
+      NULL\r
+      );\r
+  }\r
+  return Buffer;\r
 }\r
 \r
 /**\r
@@ -620,7 +671,20 @@ AllocateRuntimeZeroPool (
   IN UINTN  AllocationSize\r
   )\r
 {\r
-  return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
+  VOID  *Buffer;\r
+\r
+  Buffer = InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
+  if (Buffer != NULL) {\r
+    MemoryProfileLibRecord (\r
+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_ZERO_POOL,\r
+      EfiRuntimeServicesData,\r
+      Buffer,\r
+      AllocationSize,\r
+      NULL\r
+      );\r
+  }\r
+  return Buffer;\r
 }\r
 \r
 /**\r
@@ -653,7 +717,7 @@ AllocateReservedZeroPool (
   allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there\r
   is not enough memory remaining to satisfy the request, then NULL is returned.\r
   If Buffer is NULL, then ASSERT().\r
-  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
+  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
 \r
   @param  PoolType              The type of pool to allocate.\r
   @param  AllocationSize        The number of bytes to allocate and zero.\r
@@ -664,10 +728,10 @@ AllocateReservedZeroPool (
 **/\r
 VOID *\r
 InternalAllocateCopyPool (\r
-  IN EFI_MEMORY_TYPE  PoolType,  \r
+  IN EFI_MEMORY_TYPE  PoolType,\r
   IN UINTN            AllocationSize,\r
   IN CONST VOID       *Buffer\r
-  ) \r
+  )\r
 {\r
   VOID  *Memory;\r
 \r
@@ -679,18 +743,18 @@ InternalAllocateCopyPool (
      Memory = CopyMem (Memory, Buffer, AllocationSize);\r
   }\r
   return Memory;\r
-} \r
+}\r
 \r
 /**\r
-  Copies a buffer to an allocated buffer of type EfiBootServicesData.\r
+  Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.\r
 \r
-  Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies\r
+  Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies\r
   AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
   allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there\r
   is not enough memory remaining to satisfy the request, then NULL is returned.\r
-  \r
+\r
   If Buffer is NULL, then ASSERT().\r
-  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
+  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
 \r
   @param  AllocationSize        The number of bytes to allocate and zero.\r
   @param  Buffer                The buffer to copy to the allocated buffer.\r
@@ -705,7 +769,20 @@ AllocateCopyPool (
   IN CONST VOID  *Buffer\r
   )\r
 {\r
-  return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
+  VOID  *NewBuffer;\r
+\r
+  NewBuffer = InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
+  if (NewBuffer != NULL) {\r
+    MemoryProfileLibRecord (\r
+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_COPY_POOL,\r
+      EfiRuntimeServicesData,\r
+      NewBuffer,\r
+      AllocationSize,\r
+      NULL\r
+      );\r
+  }\r
+  return NewBuffer;\r
 }\r
 \r
 /**\r
@@ -715,9 +792,9 @@ AllocateCopyPool (
   AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
   allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there\r
   is not enough memory remaining to satisfy the request, then NULL is returned.\r
-  \r
+\r
   If Buffer is NULL, then ASSERT().\r
-  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
+  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
 \r
   @param  AllocationSize        The number of bytes to allocate and zero.\r
   @param  Buffer                The buffer to copy to the allocated buffer.\r
@@ -732,7 +809,20 @@ AllocateRuntimeCopyPool (
   IN CONST VOID  *Buffer\r
   )\r
 {\r
-  return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
+  VOID  *NewBuffer;\r
+\r
+  NewBuffer = InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
+  if (NewBuffer != NULL) {\r
+    MemoryProfileLibRecord (\r
+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_COPY_POOL,\r
+      EfiRuntimeServicesData,\r
+      NewBuffer,\r
+      AllocationSize,\r
+      NULL\r
+      );\r
+  }\r
+  return NewBuffer;\r
 }\r
 \r
 /**\r
@@ -742,9 +832,9 @@ AllocateRuntimeCopyPool (
   AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
   allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there\r
   is not enough memory remaining to satisfy the request, then NULL is returned.\r
-  \r
+\r
   If Buffer is NULL, then ASSERT().\r
-  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
+  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
 \r
   @param  AllocationSize        The number of bytes to allocate and zero.\r
   @param  Buffer                The buffer to copy to the allocated buffer.\r
@@ -766,19 +856,19 @@ AllocateReservedCopyPool (
   Reallocates a buffer of a specified memory type.\r
 \r
   Allocates and zeros the number bytes specified by NewSize from memory of the type\r
-  specified by PoolType.  If OldBuffer is not NULL, then the smaller of OldSize and \r
-  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
-  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.  \r
-  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not \r
+  specified by PoolType.  If OldBuffer is not NULL, then the smaller of OldSize and\r
+  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
+  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.\r
+  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not\r
   enough memory remaining to satisfy the request, then NULL is returned.\r
-  \r
+\r
   If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
   is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
 \r
   @param  PoolType       The type of pool to allocate.\r
   @param  OldSize        The size, in bytes, of OldBuffer.\r
   @param  NewSize        The size, in bytes, of the buffer to reallocate.\r
-  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional \r
+  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional\r
                          parameter that may be NULL.\r
 \r
   @return A pointer to the allocated buffer or NULL if allocation fails.\r
@@ -786,7 +876,7 @@ AllocateReservedCopyPool (
 **/\r
 VOID *\r
 InternalReallocatePool (\r
-  IN EFI_MEMORY_TYPE  PoolType,  \r
+  IN EFI_MEMORY_TYPE  PoolType,\r
   IN UINTN            OldSize,\r
   IN UINTN            NewSize,\r
   IN VOID             *OldBuffer  OPTIONAL\r
@@ -803,21 +893,21 @@ InternalReallocatePool (
 }\r
 \r
 /**\r
-  Reallocates a buffer of type EfiBootServicesData.\r
+  Reallocates a buffer of type EfiRuntimeServicesData.\r
 \r
   Allocates and zeros the number bytes specified by NewSize from memory of type\r
-  EfiBootServicesData.  If OldBuffer is not NULL, then the smaller of OldSize and \r
-  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
-  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.  \r
-  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not \r
+  EfiRuntimeServicesData.  If OldBuffer is not NULL, then the smaller of OldSize and\r
+  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
+  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.\r
+  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not\r
   enough memory remaining to satisfy the request, then NULL is returned.\r
-  \r
+\r
   If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
   is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
 \r
   @param  OldSize        The size, in bytes, of OldBuffer.\r
   @param  NewSize        The size, in bytes, of the buffer to reallocate.\r
-  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional \r
+  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional\r
                          parameter that may be NULL.\r
 \r
   @return A pointer to the allocated buffer or NULL if allocation fails.\r
@@ -831,17 +921,30 @@ ReallocatePool (
   IN VOID   *OldBuffer  OPTIONAL\r
   )\r
 {\r
-  return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
+  VOID  *Buffer;\r
+\r
+  Buffer = InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
+  if (Buffer != NULL) {\r
+    MemoryProfileLibRecord (\r
+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      MEMORY_PROFILE_ACTION_LIB_REALLOCATE_POOL,\r
+      EfiRuntimeServicesData,\r
+      Buffer,\r
+      NewSize,\r
+      NULL\r
+      );\r
+  }\r
+  return Buffer;\r
 }\r
 \r
 /**\r
   Reallocates a buffer of type EfiRuntimeServicesData.\r
 \r
   Allocates and zeros the number bytes specified by NewSize from memory of type\r
-  EfiRuntimeServicesData.  If OldBuffer is not NULL, then the smaller of OldSize and \r
-  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
-  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.  \r
-  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not \r
+  EfiRuntimeServicesData.  If OldBuffer is not NULL, then the smaller of OldSize and\r
+  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
+  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.\r
+  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not\r
   enough memory remaining to satisfy the request, then NULL is returned.\r
 \r
   If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
@@ -849,7 +952,7 @@ ReallocatePool (
 \r
   @param  OldSize        The size, in bytes, of OldBuffer.\r
   @param  NewSize        The size, in bytes, of the buffer to reallocate.\r
-  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional \r
+  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional\r
                          parameter that may be NULL.\r
 \r
   @return A pointer to the allocated buffer or NULL if allocation fails.\r
@@ -863,17 +966,30 @@ ReallocateRuntimePool (
   IN VOID   *OldBuffer  OPTIONAL\r
   )\r
 {\r
-  return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
+  VOID  *Buffer;\r
+\r
+  Buffer = InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
+  if (Buffer != NULL) {\r
+    MemoryProfileLibRecord (\r
+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RUNTIME_POOL,\r
+      EfiRuntimeServicesData,\r
+      Buffer,\r
+      NewSize,\r
+      NULL\r
+      );\r
+  }\r
+  return Buffer;\r
 }\r
 \r
 /**\r
   Reallocates a buffer of type EfiReservedMemoryType.\r
 \r
   Allocates and zeros the number bytes specified by NewSize from memory of type\r
-  EfiReservedMemoryType.  If OldBuffer is not NULL, then the smaller of OldSize and \r
-  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
-  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.  \r
-  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not \r
+  EfiReservedMemoryType.  If OldBuffer is not NULL, then the smaller of OldSize and\r
+  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
+  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.\r
+  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not\r
   enough memory remaining to satisfy the request, then NULL is returned.\r
 \r
   If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
@@ -881,7 +997,7 @@ ReallocateRuntimePool (
 \r
   @param  OldSize        The size, in bytes, of OldBuffer.\r
   @param  NewSize        The size, in bytes, of the buffer to reallocate.\r
-  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional \r
+  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional\r
                          parameter that may be NULL.\r
 \r
   @return A pointer to the allocated buffer or NULL if allocation fails.\r
@@ -905,7 +1021,7 @@ ReallocateReservedPool (
   Frees the buffer specified by Buffer.  Buffer must have been allocated on a previous call to the\r
   pool allocation services of the Memory Allocation Library.  If it is not possible to free pool\r
   resources, then this function will perform no actions.\r
-  \r
+\r
   If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
   then ASSERT().\r
 \r
@@ -952,12 +1068,44 @@ PiSmmCoreMemoryAllocationLibConstructor (
   IN EFI_SYSTEM_TABLE  *SystemTable\r
   )\r
 {\r
+  EFI_STATUS             Status;\r
   SMM_CORE_PRIVATE_DATA  *SmmCorePrivate;\r
+  UINTN                  Size;\r
+  VOID                   *BootServicesData;\r
 \r
   SmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;\r
+\r
+  //\r
+  // The FreePool()/FreePages() will need use SmramRanges data to know whether\r
+  // the buffer to free is in SMRAM range or not. And there may be FreePool()/\r
+  // FreePages() indrectly during calling SmmInitializeMemoryServices(), but\r
+  // no SMRAM could be allocated before calling SmmInitializeMemoryServices(),\r
+  // so temporarily use BootServicesData to hold the SmramRanges data.\r
+  //\r
+  mSmmCoreMemoryAllocLibSmramRangeCount = SmmCorePrivate->SmramRangeCount;\r
+  Size = mSmmCoreMemoryAllocLibSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR);\r
+  Status = gBS->AllocatePool (EfiBootServicesData, Size, (VOID **) &mSmmCoreMemoryAllocLibSmramRanges);\r
+  ASSERT_EFI_ERROR (Status);\r
+  ASSERT (mSmmCoreMemoryAllocLibSmramRanges != NULL);\r
+  CopyMem (mSmmCoreMemoryAllocLibSmramRanges, SmmCorePrivate->SmramRanges, Size);\r
+\r
   //\r
   // Initialize memory service using free SMRAM\r
   //\r
   SmmInitializeMemoryServices (SmmCorePrivate->SmramRangeCount, SmmCorePrivate->SmramRanges);\r
+\r
+  //\r
+  // Move the SmramRanges data from BootServicesData to SMRAM.\r
+  //\r
+  BootServicesData = mSmmCoreMemoryAllocLibSmramRanges;\r
+  mSmmCoreMemoryAllocLibSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocateCopyPool (Size, (VOID *) BootServicesData);\r
+  ASSERT (mSmmCoreMemoryAllocLibSmramRanges != NULL);\r
+\r
+  //\r
+  // Free the temporarily used BootServicesData.\r
+  //\r
+  Status = gBS->FreePool (BootServicesData);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
   return EFI_SUCCESS;\r
 }\r