]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/MemoryAllocationLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Library / PiSmmCoreMemoryAllocationLib / MemoryAllocationLib.c
index 08dd17ba693fde27eef7d230ed42ce5918f2754b..9a4d5b8cac251b7f9572a7f937c818dd8e57a477 100644 (file)
@@ -1,24 +1,18 @@
 /** @file\r
   Support routines for memory allocation routines based on SMM Core internal functions,\r
   with memory profile support.\r
-  \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
+  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 - 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
+  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -40,20 +34,21 @@ UINTN                 mSmmCoreMemoryAllocLibSmramRangeCount = 0;
 \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
 EFIAPI\r
 BufferInSmram (\r
-  IN VOID *Buffer\r
+  IN VOID  *Buffer\r
   )\r
 {\r
   UINTN  Index;\r
 \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
+  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
+    {\r
       return TRUE;\r
     }\r
   }\r
@@ -76,12 +71,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
@@ -91,7 +86,8 @@ InternalAllocatePages (
   if (EFI_ERROR (Status)) {\r
     return NULL;\r
   }\r
-  return (VOID *) (UINTN) Memory;\r
+\r
+  return (VOID *)(UINTN)Memory;\r
 }\r
 \r
 /**\r
@@ -118,14 +114,15 @@ AllocatePages (
   Buffer = InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\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
+      EFI_PAGES_TO_SIZE (Pages),\r
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -153,14 +150,15 @@ AllocateRuntimePages (
   Buffer = InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\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
+      EFI_PAGES_TO_SIZE (Pages),\r
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -194,11 +192,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
@@ -218,14 +216,15 @@ FreePages (
     // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePages() service.\r
     // So, SmmFreePages() service is used to free it.\r
     //\r
-    Status = SmmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+    Status = SmmFreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);\r
   } else {\r
     //\r
     // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.\r
     // So, gBS->FreePages() service is used to free it.\r
     //\r
-    Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+    Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);\r
   }\r
+\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
@@ -249,7 +248,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
@@ -265,27 +264,29 @@ 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
+\r
   if (Alignment > EFI_PAGE_SIZE) {\r
     //\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
+    AlignmentMask = Alignment - 1;\r
+    RealPages     = Pages + EFI_SIZE_TO_PAGES (Alignment);\r
     //\r
     // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
     //\r
     ASSERT (RealPages > Pages);\r
\r
-    Status         = SmmAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);\r
+\r
+    Status = SmmAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);\r
     if (EFI_ERROR (Status)) {\r
       return NULL;\r
     }\r
-    AlignedMemory  = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;\r
-    UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);\r
+\r
+    AlignedMemory  = ((UINTN)Memory + AlignmentMask) & ~AlignmentMask;\r
+    UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)Memory);\r
     if (UnalignedPages > 0) {\r
       //\r
       // Free first unaligned page(s).\r
@@ -293,7 +294,8 @@ InternalAllocateAlignedPages (
       Status = SmmFreePages (Memory, UnalignedPages);\r
       ASSERT_EFI_ERROR (Status);\r
     }\r
-    Memory         = (EFI_PHYSICAL_ADDRESS) (AlignedMemory + EFI_PAGES_TO_SIZE (Pages));\r
+\r
+    Memory         = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);\r
     UnalignedPages = RealPages - Pages - UnalignedPages;\r
     if (UnalignedPages > 0) {\r
       //\r
@@ -310,9 +312,11 @@ InternalAllocateAlignedPages (
     if (EFI_ERROR (Status)) {\r
       return NULL;\r
     }\r
-    AlignedMemory  = (UINTN) Memory;\r
+\r
+    AlignedMemory = (UINTN)Memory;\r
   }\r
-  return (VOID *) AlignedMemory;\r
+\r
+  return (VOID *)AlignedMemory;\r
 }\r
 \r
 /**\r
@@ -322,7 +326,7 @@ InternalAllocateAlignedPages (
   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
@@ -345,14 +349,15 @@ AllocateAlignedPages (
   Buffer = InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\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
+      EFI_PAGES_TO_SIZE (Pages),\r
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -363,7 +368,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
@@ -386,14 +391,15 @@ AllocateAlignedRuntimePages (
   Buffer = InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\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
+      EFI_PAGES_TO_SIZE (Pages),\r
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -404,7 +410,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
@@ -431,13 +437,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
@@ -457,14 +463,15 @@ FreeAlignedPages (
     // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePages() service.\r
     // So, SmmFreePages() service is used to free it.\r
     //\r
-    Status = SmmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+    Status = SmmFreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);\r
   } else {\r
     //\r
     // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.\r
     // So, gBS->FreePages() service is used to free it.\r
     //\r
-    Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+    Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);\r
   }\r
+\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
@@ -483,7 +490,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
@@ -496,6 +503,7 @@ InternalAllocatePool (
   if (EFI_ERROR (Status)) {\r
     Memory = NULL;\r
   }\r
+\r
   return Memory;\r
 }\r
 \r
@@ -522,7 +530,7 @@ AllocatePool (
   Buffer = InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_POOL,\r
       EfiRuntimeServicesData,\r
       Buffer,\r
@@ -530,6 +538,7 @@ AllocatePool (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -556,7 +565,7 @@ AllocateRuntimePool (
   Buffer = InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_POOL,\r
       EfiRuntimeServicesData,\r
       Buffer,\r
@@ -564,6 +573,7 @@ AllocateRuntimePool (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -604,9 +614,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
@@ -614,6 +624,7 @@ InternalAllocateZeroPool (
   if (Memory != NULL) {\r
     Memory = ZeroMem (Memory, AllocationSize);\r
   }\r
+\r
   return Memory;\r
 }\r
 \r
@@ -641,7 +652,7 @@ AllocateZeroPool (
   Buffer = InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ZERO_POOL,\r
       EfiRuntimeServicesData,\r
       Buffer,\r
@@ -649,6 +660,7 @@ AllocateZeroPool (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -676,7 +688,7 @@ AllocateRuntimeZeroPool (
   Buffer = InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_ZERO_POOL,\r
       EfiRuntimeServicesData,\r
       Buffer,\r
@@ -684,6 +696,7 @@ AllocateRuntimeZeroPool (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -717,7 +730,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
@@ -728,22 +741,23 @@ 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
   ASSERT (Buffer != NULL);\r
-  ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r
+  ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN)Buffer + 1));\r
 \r
   Memory = InternalAllocatePool (PoolType, AllocationSize);\r
   if (Memory != NULL) {\r
-     Memory = CopyMem (Memory, Buffer, AllocationSize);\r
+    Memory = CopyMem (Memory, Buffer, AllocationSize);\r
   }\r
+\r
   return Memory;\r
-} \r
+}\r
 \r
 /**\r
   Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.\r
@@ -752,9 +766,9 @@ InternalAllocateCopyPool (
   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
@@ -774,7 +788,7 @@ AllocateCopyPool (
   NewBuffer = InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
   if (NewBuffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_COPY_POOL,\r
       EfiRuntimeServicesData,\r
       NewBuffer,\r
@@ -782,6 +796,7 @@ AllocateCopyPool (
       NULL\r
       );\r
   }\r
+\r
   return NewBuffer;\r
 }\r
 \r
@@ -792,9 +807,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
@@ -814,7 +829,7 @@ AllocateRuntimeCopyPool (
   NewBuffer = InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
   if (NewBuffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_COPY_POOL,\r
       EfiRuntimeServicesData,\r
       NewBuffer,\r
@@ -822,6 +837,7 @@ AllocateRuntimeCopyPool (
       NULL\r
       );\r
   }\r
+\r
   return NewBuffer;\r
 }\r
 \r
@@ -832,9 +848,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
@@ -856,19 +872,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
@@ -876,7 +892,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
@@ -885,10 +901,11 @@ InternalReallocatePool (
   VOID  *NewBuffer;\r
 \r
   NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);\r
-  if (NewBuffer != NULL && OldBuffer != NULL) {\r
+  if ((NewBuffer != NULL) && (OldBuffer != NULL)) {\r
     CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));\r
     FreePool (OldBuffer);\r
   }\r
+\r
   return NewBuffer;\r
 }\r
 \r
@@ -896,18 +913,18 @@ InternalReallocatePool (
   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
+\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
@@ -926,7 +943,7 @@ ReallocatePool (
   Buffer = InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_REALLOCATE_POOL,\r
       EfiRuntimeServicesData,\r
       Buffer,\r
@@ -934,6 +951,7 @@ ReallocatePool (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -941,10 +959,10 @@ ReallocatePool (
   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
@@ -952,7 +970,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
@@ -971,7 +989,7 @@ ReallocateRuntimePool (
   Buffer = InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RUNTIME_POOL,\r
       EfiRuntimeServicesData,\r
       Buffer,\r
@@ -979,6 +997,7 @@ ReallocateRuntimePool (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -986,10 +1005,10 @@ ReallocateRuntimePool (
   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
@@ -997,7 +1016,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
@@ -1021,7 +1040,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
@@ -1031,10 +1050,10 @@ ReallocateReservedPool (
 VOID\r
 EFIAPI\r
 FreePool (\r
-  IN VOID   *Buffer\r
+  IN VOID  *Buffer\r
   )\r
 {\r
-  EFI_STATUS    Status;\r
+  EFI_STATUS  Status;\r
 \r
   if (BufferInSmram (Buffer)) {\r
     //\r
@@ -1049,6 +1068,7 @@ FreePool (
     //\r
     Status = gBS->FreePool (Buffer);\r
   }\r
+\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
@@ -1068,20 +1088,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
-  mSmmCoreMemoryAllocLibSmramRangeCount = SmmCorePrivate->SmramRangeCount;\r
-  Size = mSmmCoreMemoryAllocLibSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR);\r
-  mSmmCoreMemoryAllocLibSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);\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
-  CopyMem (mSmmCoreMemoryAllocLibSmramRanges, SmmCorePrivate->SmramRanges, Size);\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