]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Mem/Page.c
MdeModulePkg/DxeCore: switch to MdePkg allocation granularity macros
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Mem / Page.c
index 04844e3305fa9a21bdcca4ebede0799beba15c9b..260a30a214c7117a148e61863c4657f6366a6c45 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   UEFI Memory page management functions.\r
 \r
-Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2007 - 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
@@ -15,8 +15,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include "DxeMain.h"\r
 #include "Imem.h"\r
 \r
-#define EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT  (EFI_PAGE_SIZE)\r
-\r
 //\r
 // Entry for tracking the memory regions for each memory type to coalesce similar memory types\r
 //\r
@@ -67,6 +65,7 @@ EFI_MEMORY_TYPE_STATISTICS mMemoryTypeStatistics[EfiMaxMemoryType + 1] = {
   { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiMemoryMappedIO\r
   { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiMemoryMappedIOPortSpace\r
   { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  TRUE  },  // EfiPalCode\r
+  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiPersistentMemory\r
   { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }   // EfiMaxMemoryType\r
 };\r
 \r
@@ -88,6 +87,7 @@ EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = {
   { EfiMemoryMappedIO,          0 },\r
   { EfiMemoryMappedIOPortSpace, 0 },\r
   { EfiPalCode,                 0 },\r
+  { EfiPersistentMemory,        0 },\r
   { EfiMaxMemoryType,           0 }\r
 };\r
 //\r
@@ -177,7 +177,20 @@ CoreAddRange (
   ASSERT_LOCKED (&gMemoryLock);\r
 \r
   DEBUG ((DEBUG_PAGE, "AddRange: %lx-%lx to %d\n", Start, End, Type));\r
-\r
+  \r
+  //\r
+  // If memory of type EfiConventionalMemory is being added that includes the page \r
+  // starting at address 0, then zero the page starting at address 0.  This has \r
+  // two benifits.  It helps find NULL pointer bugs and it also maximizes \r
+  // compatibility with operating systems that may evaluate memory in this page \r
+  // for legacy data structures.  If memory of any other type is added starting \r
+  // at address 0, then do not zero the page at address 0 because the page is being \r
+  // used for other purposes.\r
+  //  \r
+  if (Type == EfiConventionalMemory && Start == 0 && (End >= EFI_PAGE_SIZE - 1)) {\r
+    SetMem ((VOID *)(UINTN)Start, EFI_PAGE_SIZE, 0);\r
+  }\r
+  \r
   //\r
   // Memory map being altered so updated key\r
   //\r
@@ -189,7 +202,7 @@ CoreAddRange (
   // If we are in EFI 1.10 compatability mode no event groups will be\r
   // found and nothing will happen we we call this function. These events\r
   // will get signaled but since a lock is held around the call to this\r
-  // function the notificaiton events will only be called after this funciton\r
+  // function the notificaiton events will only be called after this function\r
   // returns and the lock is released.\r
   //\r
   CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid);\r
@@ -272,12 +285,14 @@ AllocateMemoryMapEntry (
     //\r
     // The list is empty, to allocate one page to refuel the list\r
     //\r
-    FreeDescriptorEntries = CoreAllocatePoolPages (EfiBootServicesData, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION), DEFAULT_PAGE_ALLOCATION);\r
-    if(FreeDescriptorEntries != NULL) {\r
+    FreeDescriptorEntries = CoreAllocatePoolPages (EfiBootServicesData,\r
+                              EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION_GRANULARITY),\r
+                              DEFAULT_PAGE_ALLOCATION_GRANULARITY);\r
+    if (FreeDescriptorEntries != NULL) {\r
       //\r
       // Enque the free memmory map entries into the list\r
       //\r
-      for (Index = 0; Index< DEFAULT_PAGE_ALLOCATION / sizeof(MEMORY_MAP); Index++) {\r
+      for (Index = 0; Index < DEFAULT_PAGE_ALLOCATION_GRANULARITY / sizeof(MEMORY_MAP); Index++) {\r
         FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE;\r
         InsertTailList (&mFreeMemoryMapEntryList, &FreeDescriptorEntries[Index].Link);\r
       }\r
@@ -401,7 +416,11 @@ PromoteMemoryResource (
       //\r
       // Update the GCD map\r
       //\r
-      Entry->GcdMemoryType = EfiGcdMemoryTypeSystemMemory;\r
+      if ((Entry->Capabilities & EFI_MEMORY_MORE_RELIABLE) == EFI_MEMORY_MORE_RELIABLE) {\r
+        Entry->GcdMemoryType = EfiGcdMemoryTypeMoreReliable;\r
+      } else {\r
+        Entry->GcdMemoryType = EfiGcdMemoryTypeSystemMemory;\r
+      }\r
       Entry->Capabilities |= EFI_MEMORY_TESTED;\r
       Entry->ImageHandle  = gDxeCoreImageHandle;\r
       Entry->DeviceHandle = NULL;\r
@@ -525,7 +544,7 @@ CoreAddMemoryDescriptor (
     return;\r
   }\r
 \r
-  if (Type >= EfiMaxMemoryType && Type <= 0x7fffffff) {\r
+  if (Type >= EfiMaxMemoryType && Type < MEMORY_TYPE_OEM_RESERVED_MIN) {\r
     return;\r
   }\r
   CoreAcquireMemoryLock ();\r
@@ -534,6 +553,9 @@ CoreAddMemoryDescriptor (
   CoreFreeMemoryMapStack ();\r
   CoreReleaseMemoryLock ();\r
 \r
+  ApplyMemoryProtectionPolicy (EfiMaxMemoryType, Type, Start,\r
+    LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT));\r
+\r
   //\r
   // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type\r
   //\r
@@ -557,7 +579,7 @@ CoreAddMemoryDescriptor (
     // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
     //\r
     Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r
-    if (Type < 0 || Type > EfiMaxMemoryType) {\r
+    if ((UINT32)Type > EfiMaxMemoryType) {\r
       continue;\r
     }\r
     if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
@@ -581,7 +603,7 @@ CoreAddMemoryDescriptor (
           // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
           //\r
           Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[FreeIndex].Type);\r
-          if (Type < 0 || Type > EfiMaxMemoryType) {\r
+          if ((UINT32)Type > EfiMaxMemoryType) {\r
             continue;\r
           }\r
 \r
@@ -624,7 +646,7 @@ CoreAddMemoryDescriptor (
     // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
     //\r
     Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r
-    if (Type < 0 || Type > EfiMaxMemoryType) {\r
+    if ((UINT32)Type > EfiMaxMemoryType) {\r
       continue;\r
     }\r
     if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
@@ -658,13 +680,17 @@ CoreAddMemoryDescriptor (
 \r
 \r
 /**\r
-  Internal function.  Converts a memory range to the specified type.\r
-  The range must exist in the memory map.\r
+  Internal function.  Converts a memory range to the specified type or attributes.\r
+  The range must exist in the memory map.  Either ChangingType or\r
+  ChangingAttributes must be set, but not both.\r
 \r
   @param  Start                  The first address of the range Must be page\r
                                  aligned\r
   @param  NumberOfPages          The number of pages to convert\r
+  @param  ChangingType           Boolean indicating that type value should be changed\r
   @param  NewType                The new type for the memory range\r
+  @param  ChangingAttributes     Boolean indicating that attributes value should be changed\r
+  @param  NewAttributes          The new attributes for the memory range\r
 \r
   @retval EFI_INVALID_PARAMETER  Invalid parameter\r
   @retval EFI_NOT_FOUND          Could not find a descriptor cover the specified\r
@@ -674,10 +700,13 @@ CoreAddMemoryDescriptor (
 \r
 **/\r
 EFI_STATUS\r
-CoreConvertPages (\r
+CoreConvertPagesEx (\r
   IN UINT64           Start,\r
   IN UINT64           NumberOfPages,\r
-  IN EFI_MEMORY_TYPE  NewType\r
+  IN BOOLEAN          ChangingType,\r
+  IN EFI_MEMORY_TYPE  NewType,\r
+  IN BOOLEAN          ChangingAttributes,\r
+  IN UINT64           NewAttributes\r
   )\r
 {\r
 \r
@@ -685,6 +714,7 @@ CoreConvertPages (
   UINT64          End;\r
   UINT64          RangeEnd;\r
   UINT64          Attribute;\r
+  EFI_MEMORY_TYPE MemType;\r
   LIST_ENTRY      *Link;\r
   MEMORY_MAP      *Entry;\r
 \r
@@ -696,8 +726,9 @@ CoreConvertPages (
   ASSERT ((Start & EFI_PAGE_MASK) == 0);\r
   ASSERT (End > Start) ;\r
   ASSERT_LOCKED (&gMemoryLock);\r
+  ASSERT ( (ChangingType == FALSE) || (ChangingAttributes == FALSE) );\r
 \r
-  if (NumberOfPages == 0 || ((Start & EFI_PAGE_MASK) != 0) || (Start > (Start + NumberOfBytes))) {\r
+  if (NumberOfPages == 0 || ((Start & EFI_PAGE_MASK) != 0) || (Start >= End)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -734,36 +765,43 @@ CoreConvertPages (
       RangeEnd = Entry->End;\r
     }\r
 \r
-    DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to %d\n", Start, RangeEnd, NewType));\r
-\r
-    //\r
-    // Debug code - verify conversion is allowed\r
-    //\r
-    if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) {\r
-      DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: Incompatible memory types\n"));\r
-      return EFI_NOT_FOUND;\r
+    if (ChangingType) {\r
+      DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to type %d\n", Start, RangeEnd, NewType));\r
+    }\r
+    if (ChangingAttributes) {\r
+      DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to attr %lx\n", Start, RangeEnd, NewAttributes));\r
     }\r
 \r
-    //\r
-    // Update counters for the number of pages allocated to each memory type\r
-    //\r
-    if (Entry->Type >= 0 && Entry->Type < EfiMaxMemoryType) {\r
-      if ((Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress && Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) ||\r
-          (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress)                                                          ) {\r
-        if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) {\r
-          mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0;\r
-        } else {\r
-          mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages -= NumberOfPages;\r
+    if (ChangingType) {\r
+      //\r
+      // Debug code - verify conversion is allowed\r
+      //\r
+      if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) {\r
+        DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: Incompatible memory types\n"));\r
+        return EFI_NOT_FOUND;\r
+      }\r
+\r
+      //\r
+      // Update counters for the number of pages allocated to each memory type\r
+      //\r
+      if ((UINT32)Entry->Type < EfiMaxMemoryType) {\r
+        if ((Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress && Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) ||\r
+            (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress)                                                          ) {\r
+          if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) {\r
+            mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0;\r
+          } else {\r
+            mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages -= NumberOfPages;\r
+          }\r
         }\r
       }\r
-    }\r
 \r
-    if (NewType >= 0 && NewType < EfiMaxMemoryType) {\r
-      if ((Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) ||\r
-          (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress)                                                  ) {\r
-        mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages;\r
-        if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) {\r
-          gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages;\r
+      if ((UINT32)NewType < EfiMaxMemoryType) {\r
+        if ((Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) ||\r
+            (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress)                                                  ) {\r
+          mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages;\r
+          if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) {\r
+            gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages;\r
+          }\r
         }\r
       }\r
     }\r
@@ -817,9 +855,15 @@ CoreConvertPages (
 \r
     //\r
     // The new range inherits the same Attribute as the Entry\r
-    //it is being cut out of\r
+    // it is being cut out of unless attributes are being changed\r
     //\r
-    Attribute = Entry->Attribute;\r
+    if (ChangingType) {\r
+      Attribute = Entry->Attribute;\r
+      MemType = NewType;\r
+    } else {\r
+      Attribute = NewAttributes;\r
+      MemType = Entry->Type;\r
+    }\r
 \r
     //\r
     // If the descriptor is empty, then remove it from the map\r
@@ -832,9 +876,20 @@ CoreConvertPages (
     //\r
     // Add our new range in\r
     //\r
-    CoreAddRange (NewType, Start, RangeEnd, Attribute);\r
-    if (NewType == EfiConventionalMemory) {\r
-      DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) Start, (UINTN) (RangeEnd - Start + 1));\r
+    CoreAddRange (MemType, Start, RangeEnd, Attribute);\r
+    if (ChangingType && (MemType == EfiConventionalMemory)) {\r
+      //\r
+      // Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this\r
+      // macro will ASSERT() if address is 0.  Instead, CoreAddRange() guarantees\r
+      // that the page starting at address 0 is always filled with zeros.\r
+      //\r
+      if (Start == 0) {\r
+        if (RangeEnd > EFI_PAGE_SIZE) {\r
+          DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) EFI_PAGE_SIZE, (UINTN) (RangeEnd - EFI_PAGE_SIZE + 1));\r
+        }\r
+      } else {\r
+        DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) Start, (UINTN) (RangeEnd - Start + 1));\r
+      }\r
     }\r
 \r
     //\r
@@ -856,6 +911,59 @@ CoreConvertPages (
 }\r
 \r
 \r
+/**\r
+  Internal function.  Converts a memory range to the specified type.\r
+  The range must exist in the memory map.\r
+\r
+  @param  Start                  The first address of the range Must be page\r
+                                 aligned\r
+  @param  NumberOfPages          The number of pages to convert\r
+  @param  NewType                The new type for the memory range\r
+\r
+  @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+  @retval EFI_NOT_FOUND          Could not find a descriptor cover the specified\r
+                                 range  or convertion not allowed.\r
+  @retval EFI_SUCCESS            Successfully converts the memory range to the\r
+                                 specified type.\r
+\r
+**/\r
+EFI_STATUS\r
+CoreConvertPages (\r
+  IN UINT64           Start,\r
+  IN UINT64           NumberOfPages,\r
+  IN EFI_MEMORY_TYPE  NewType\r
+  )\r
+{\r
+  return CoreConvertPagesEx(Start, NumberOfPages, TRUE, NewType, FALSE, 0);\r
+}\r
+\r
+\r
+/**\r
+  Internal function.  Converts a memory range to use new attributes.\r
+\r
+  @param  Start                  The first address of the range Must be page\r
+                                 aligned\r
+  @param  NumberOfPages          The number of pages to convert\r
+  @param  NewAttributes          The new attributes value for the range.\r
+\r
+**/\r
+VOID\r
+CoreUpdateMemoryAttributes (\r
+  IN EFI_PHYSICAL_ADDRESS  Start,\r
+  IN UINT64                NumberOfPages,\r
+  IN UINT64                NewAttributes\r
+  )\r
+{\r
+  CoreAcquireMemoryLock ();\r
+\r
+  //\r
+  // Update the attributes to the new value\r
+  //\r
+  CoreConvertPagesEx(Start, NumberOfPages, FALSE, (EFI_MEMORY_TYPE)0, TRUE, NewAttributes);\r
+\r
+  CoreReleaseMemoryLock ();\r
+}\r
+\r
 \r
 /**\r
   Internal function. Finds a consecutive free page range below\r
@@ -906,7 +1014,7 @@ CoreFindFreePagesI (
     //\r
     // Set MaxAddress to a page boundary\r
     //\r
-    MaxAddress &= ~EFI_PAGE_MASK;\r
+    MaxAddress &= ~(UINT64)EFI_PAGE_MASK;\r
 \r
     //\r
     // Set MaxAddress to end of the page\r
@@ -946,6 +1054,11 @@ CoreFindFreePagesI (
 \r
     DescEnd = ((DescEnd + 1) & (~(Alignment - 1))) - 1;\r
 \r
+    // Skip if DescEnd is less than DescStart after alignment clipping\r
+    if (DescEnd < DescStart) {\r
+      continue;\r
+    }\r
+\r
     //\r
     // Compute the number of bytes we can used from this\r
     // descriptor, and see it's enough to satisfy the request\r
@@ -1011,7 +1124,7 @@ FindFreePages (
   //\r
   // Attempt to find free pages in the preferred bin based on the requested memory type\r
   //\r
-  if (NewType >= 0 && NewType < EfiMaxMemoryType && MaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) {\r
+  if ((UINT32)NewType < EfiMaxMemoryType && MaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) {\r
     Start = CoreFindFreePagesI (\r
               mMemoryTypeStatistics[NewType].MaximumAddress, \r
               mMemoryTypeStatistics[NewType].BaseAddress, \r
@@ -1082,7 +1195,7 @@ FindFreePages (
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-CoreAllocatePages (\r
+CoreInternalAllocatePages (\r
   IN EFI_ALLOCATE_TYPE      Type,\r
   IN EFI_MEMORY_TYPE        MemoryType,\r
   IN UINTN                  NumberOfPages,\r
@@ -1091,26 +1204,32 @@ CoreAllocatePages (
 {\r
   EFI_STATUS      Status;\r
   UINT64          Start;\r
+  UINT64          NumberOfBytes;\r
+  UINT64          End;\r
   UINT64          MaxAddress;\r
   UINTN           Alignment;\r
 \r
-  if (Type < AllocateAnyPages || Type >= (UINTN) MaxAllocateType) {\r
+  if ((UINT32)Type >= MaxAllocateType) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((MemoryType >= EfiMaxMemoryType && MemoryType < MEMORY_TYPE_OEM_RESERVED_MIN) ||\r
+       (MemoryType == EfiConventionalMemory) || (MemoryType == EfiPersistentMemory)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if ((MemoryType >= EfiMaxMemoryType && MemoryType <= 0x7fffffff) ||\r
-       MemoryType == EfiConventionalMemory) {\r
+  if (Memory == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;\r
+  Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r
 \r
   if  (MemoryType == EfiACPIReclaimMemory   ||\r
        MemoryType == EfiACPIMemoryNVS       ||\r
        MemoryType == EfiRuntimeServicesCode ||\r
        MemoryType == EfiRuntimeServicesData) {\r
 \r
-    Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
+    Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r
   }\r
 \r
   if (Type == AllocateAddress) {\r
@@ -1132,6 +1251,30 @@ CoreAllocatePages (
   //\r
   MaxAddress = MAX_ADDRESS;\r
 \r
+  //\r
+  // Check for Type AllocateAddress,\r
+  // if NumberOfPages is 0 or\r
+  // if (NumberOfPages << EFI_PAGE_SHIFT) is above MAX_ADDRESS or\r
+  // if (Start + NumberOfBytes) rolls over 0 or\r
+  // if Start is above MAX_ADDRESS or\r
+  // if End is above MAX_ADDRESS,\r
+  // return EFI_NOT_FOUND.\r
+  //\r
+  if (Type == AllocateAddress) {\r
+    if ((NumberOfPages == 0) ||\r
+        (NumberOfPages > RShiftU64 (MaxAddress, EFI_PAGE_SHIFT))) {\r
+      return EFI_NOT_FOUND;\r
+    }\r
+    NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
+    End = Start + NumberOfBytes - 1;\r
+\r
+    if ((Start >= End) ||\r
+        (Start > MaxAddress) || \r
+        (End > MaxAddress)) {\r
+      return EFI_NOT_FOUND;\r
+    }\r
+  }\r
+\r
   if (Type == AllocateMaxAddress) {\r
     MaxAddress = Start;\r
   }\r
@@ -1164,12 +1307,58 @@ Done:
   return Status;\r
 }\r
 \r
+/**\r
+  Allocates pages from the memory map.\r
+\r
+  @param  Type                   The type of allocation to perform\r
+  @param  MemoryType             The type of memory to turn the allocated pages\r
+                                 into\r
+  @param  NumberOfPages          The number of pages to allocate\r
+  @param  Memory                 A pointer to receive the base allocated memory\r
+                                 address\r
+\r
+  @return Status. On success, Memory is filled in with the base address allocated\r
+  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in\r
+                                 spec.\r
+  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.\r
+  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.\r
+  @retval EFI_SUCCESS            Pages successfully allocated.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CoreAllocatePages (\r
+  IN  EFI_ALLOCATE_TYPE     Type,\r
+  IN  EFI_MEMORY_TYPE       MemoryType,\r
+  IN  UINTN                 NumberOfPages,\r
+  OUT EFI_PHYSICAL_ADDRESS  *Memory\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  Status = CoreInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory);\r
+  if (!EFI_ERROR (Status)) {\r
+    CoreUpdateProfile (\r
+      (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r
+      MemoryProfileActionAllocatePages,\r
+      MemoryType,\r
+      EFI_PAGES_TO_SIZE (NumberOfPages),\r
+      (VOID *) (UINTN) *Memory,\r
+      NULL\r
+      );\r
+    InstallMemoryAttributesTableOnMemoryAllocation (MemoryType);\r
+    ApplyMemoryProtectionPolicy (EfiConventionalMemory, MemoryType, *Memory,\r
+      EFI_PAGES_TO_SIZE (NumberOfPages));\r
+  }\r
+  return Status;\r
+}\r
 \r
 /**\r
   Frees previous allocated pages.\r
 \r
   @param  Memory                 Base address of memory being freed\r
   @param  NumberOfPages          The number of pages to free\r
+  @param  MemoryType             Pointer to memory type\r
 \r
   @retval EFI_NOT_FOUND          Could not find the entry that covers the range\r
   @retval EFI_INVALID_PARAMETER  Address not aligned\r
@@ -1178,9 +1367,10 @@ Done:
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-CoreFreePages (\r
+CoreInternalFreePages (\r
   IN EFI_PHYSICAL_ADDRESS   Memory,\r
-  IN UINTN                  NumberOfPages\r
+  IN UINTN                  NumberOfPages,\r
+  OUT EFI_MEMORY_TYPE       *MemoryType OPTIONAL\r
   )\r
 {\r
   EFI_STATUS      Status;\r
@@ -1208,7 +1398,7 @@ CoreFreePages (
     goto Done;\r
   }\r
 \r
-  Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;\r
+  Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r
 \r
   ASSERT (Entry != NULL);\r
   if  (Entry->Type == EfiACPIReclaimMemory   ||\r
@@ -1216,7 +1406,7 @@ CoreFreePages (
        Entry->Type == EfiRuntimeServicesCode ||\r
        Entry->Type == EfiRuntimeServicesData) {\r
 \r
-    Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
+    Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r
 \r
   }\r
 \r
@@ -1228,6 +1418,10 @@ CoreFreePages (
   NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r
   NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r
 \r
+  if (MemoryType != NULL) {\r
+    *MemoryType = Entry->Type;\r
+  }\r
+\r
   Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
 \r
   if (EFI_ERROR (Status)) {\r
@@ -1239,6 +1433,44 @@ Done:
   return Status;\r
 }\r
 \r
+/**\r
+  Frees previous allocated pages.\r
+\r
+  @param  Memory                 Base address of memory being freed\r
+  @param  NumberOfPages          The number of pages to free\r
+\r
+  @retval EFI_NOT_FOUND          Could not find the entry that covers the range\r
+  @retval EFI_INVALID_PARAMETER  Address not aligned\r
+  @return EFI_SUCCESS         -Pages successfully freed.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CoreFreePages (\r
+  IN EFI_PHYSICAL_ADDRESS  Memory,\r
+  IN UINTN                 NumberOfPages\r
+  )\r
+{\r
+  EFI_STATUS        Status;\r
+  EFI_MEMORY_TYPE   MemoryType;\r
+\r
+  Status = CoreInternalFreePages (Memory, NumberOfPages, &MemoryType);\r
+  if (!EFI_ERROR (Status)) {\r
+    CoreUpdateProfile (\r
+      (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r
+      MemoryProfileActionFreePages,\r
+      MemoryType,\r
+      EFI_PAGES_TO_SIZE (NumberOfPages),\r
+      (VOID *) (UINTN) Memory,\r
+      NULL\r
+      );\r
+    InstallMemoryAttributesTableOnMemoryAllocation (MemoryType);\r
+    ApplyMemoryProtectionPolicy (MemoryType, EfiConventionalMemory, Memory,\r
+      EFI_PAGES_TO_SIZE (NumberOfPages));\r
+  }\r
+  return Status;\r
+}\r
+\r
 /**\r
   This function checks to see if the last memory map descriptor in a memory map\r
   can be merged with any of the other memory map descriptors in a memorymap.\r
@@ -1363,10 +1595,11 @@ CoreGetMemoryMap (
   EFI_STATUS                        Status;\r
   UINTN                             Size;\r
   UINTN                             BufferSize;\r
-  UINTN                             NumberOfRuntimeEntries;\r
+  UINTN                             NumberOfEntries;\r
   LIST_ENTRY                        *Link;\r
   MEMORY_MAP                        *Entry;\r
   EFI_GCD_MAP_ENTRY                 *GcdMapEntry;\r
+  EFI_GCD_MAP_ENTRY                 MergeGcdMapEntry;\r
   EFI_MEMORY_TYPE                   Type;\r
   EFI_MEMORY_DESCRIPTOR             *MemoryMapStart;\r
 \r
@@ -1380,16 +1613,17 @@ CoreGetMemoryMap (
   CoreAcquireGcdMemoryLock ();\r
 \r
   //\r
-  // Count the number of Reserved and MMIO entries that are marked for runtime use\r
+  // Count the number of Reserved and runtime MMIO entries\r
+  // And, count the number of Persistent entries.\r
   //\r
-  NumberOfRuntimeEntries = 0;\r
+  NumberOfEntries = 0;\r
   for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r
     GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
-    if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
-        (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {\r
-      if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
-        NumberOfRuntimeEntries++;\r
-      }\r
+    if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypePersistentMemory) || \r
+        (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
+        ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&\r
+        ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME))) {\r
+      NumberOfEntries ++;\r
     }\r
   }\r
 \r
@@ -1415,7 +1649,7 @@ CoreGetMemoryMap (
   //\r
   // Compute the buffer size needed to fit the entire map\r
   //\r
-  BufferSize = Size * NumberOfRuntimeEntries;\r
+  BufferSize = Size * NumberOfEntries;\r
   for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
     BufferSize += Size;\r
   }\r
@@ -1477,36 +1711,98 @@ CoreGetMemoryMap (
     MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r
   }\r
 \r
-  for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r
-    GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
-    if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
-        (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {\r
-      if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
-        // \r
-        // Create EFI_MEMORY_DESCRIPTOR for every Reserved and MMIO GCD entries\r
-        // that are marked for runtime use\r
-        //\r
-        MemoryMap->PhysicalStart = GcdMapEntry->BaseAddress;\r
-        MemoryMap->VirtualStart  = 0;\r
-        MemoryMap->NumberOfPages = RShiftU64 ((GcdMapEntry->EndAddress - GcdMapEntry->BaseAddress + 1), EFI_PAGE_SHIFT);\r
-        MemoryMap->Attribute     = GcdMapEntry->Attributes & ~EFI_MEMORY_PORT_IO;\r
-\r
-        if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) {\r
-          MemoryMap->Type = EfiReservedMemoryType;\r
-        } else if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {\r
-          if ((GcdMapEntry->Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) {\r
-            MemoryMap->Type = EfiMemoryMappedIOPortSpace;\r
-          } else {\r
-            MemoryMap->Type = EfiMemoryMappedIO;\r
-          }\r
-        }\r
\r
+  ZeroMem (&MergeGcdMapEntry, sizeof (MergeGcdMapEntry));\r
+  GcdMapEntry = NULL;\r
+  for (Link = mGcdMemorySpaceMap.ForwardLink; ; Link = Link->ForwardLink) {\r
+    if (Link != &mGcdMemorySpaceMap) {\r
+      //\r
+      // Merge adjacent same type and attribute GCD memory range\r
+      //\r
+      GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
+  \r
+      if ((MergeGcdMapEntry.Capabilities == GcdMapEntry->Capabilities) && \r
+          (MergeGcdMapEntry.Attributes == GcdMapEntry->Attributes) &&\r
+          (MergeGcdMapEntry.GcdMemoryType == GcdMapEntry->GcdMemoryType) &&\r
+          (MergeGcdMapEntry.GcdIoType == GcdMapEntry->GcdIoType)) {\r
+        MergeGcdMapEntry.EndAddress  = GcdMapEntry->EndAddress;\r
+        continue;\r
+      }\r
+    }\r
 \r
-        //\r
-        // Check to see if the new Memory Map Descriptor can be merged with an \r
-        // existing descriptor if they are adjacent and have the same attributes\r
-        //\r
-        MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r
+    if ((MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
+        ((MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&\r
+        ((MergeGcdMapEntry.Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME))) {\r
+      //\r
+      // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR, \r
+      // it will be recorded as page PhysicalStart and NumberOfPages. \r
+      //\r
+      ASSERT ((MergeGcdMapEntry.BaseAddress & EFI_PAGE_MASK) == 0);\r
+      ASSERT (((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1) & EFI_PAGE_MASK) == 0);\r
+      \r
+      // \r
+      // Create EFI_MEMORY_DESCRIPTOR for every Reserved and runtime MMIO GCD entries\r
+      //\r
+      MemoryMap->PhysicalStart = MergeGcdMapEntry.BaseAddress;\r
+      MemoryMap->VirtualStart  = 0;\r
+      MemoryMap->NumberOfPages = RShiftU64 ((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1), EFI_PAGE_SHIFT);\r
+      MemoryMap->Attribute     = (MergeGcdMapEntry.Attributes & ~EFI_MEMORY_PORT_IO) | \r
+                                (MergeGcdMapEntry.Capabilities & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP | EFI_MEMORY_RO |\r
+                                EFI_MEMORY_UC | EFI_MEMORY_UCE | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB));\r
+\r
+      if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeReserved) {\r
+        MemoryMap->Type = EfiReservedMemoryType;\r
+      } else if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {\r
+        if ((MergeGcdMapEntry.Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) {\r
+          MemoryMap->Type = EfiMemoryMappedIOPortSpace;\r
+        } else {\r
+          MemoryMap->Type = EfiMemoryMappedIO;\r
+        }\r
       }\r
+\r
+      //\r
+      // Check to see if the new Memory Map Descriptor can be merged with an \r
+      // existing descriptor if they are adjacent and have the same attributes\r
+      //\r
+      MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r
+    }\r
+    \r
+    if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypePersistentMemory) {\r
+      //\r
+      // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR, \r
+      // it will be recorded as page PhysicalStart and NumberOfPages. \r
+      //\r
+      ASSERT ((MergeGcdMapEntry.BaseAddress & EFI_PAGE_MASK) == 0);\r
+      ASSERT (((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1) & EFI_PAGE_MASK) == 0);\r
+\r
+      // \r
+      // Create EFI_MEMORY_DESCRIPTOR for every Persistent GCD entries\r
+      //\r
+      MemoryMap->PhysicalStart = MergeGcdMapEntry.BaseAddress;\r
+      MemoryMap->VirtualStart  = 0;\r
+      MemoryMap->NumberOfPages = RShiftU64 ((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1), EFI_PAGE_SHIFT);\r
+      MemoryMap->Attribute     = MergeGcdMapEntry.Attributes | EFI_MEMORY_NV | \r
+                                (MergeGcdMapEntry.Capabilities & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP | EFI_MEMORY_RO |\r
+                                EFI_MEMORY_UC | EFI_MEMORY_UCE | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB));\r
+      MemoryMap->Type          = EfiPersistentMemory;\r
+      \r
+      //\r
+      // Check to see if the new Memory Map Descriptor can be merged with an \r
+      // existing descriptor if they are adjacent and have the same attributes\r
+      //\r
+      MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r
+    }\r
+    if (Link == &mGcdMemorySpaceMap) {\r
+      //\r
+      // break loop when arrive at head.\r
+      //\r
+      break;\r
+    }\r
+    if (GcdMapEntry != NULL) {\r
+      //\r
+      // Copy new GCD map entry for the following GCD range merge\r
+      //\r
+      CopyMem (&MergeGcdMapEntry, GcdMapEntry, sizeof (MergeGcdMapEntry));\r
     }\r
   }\r
 \r
@@ -1625,21 +1921,20 @@ CoreTerminateMemoryMap (
 \r
     for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
       Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
-      if ((Entry->Attribute & EFI_MEMORY_RUNTIME) != 0) {\r
-        if (Entry->Type == EfiACPIReclaimMemory || Entry->Type == EfiACPIMemoryNVS) {\r
-          DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));\r
-          Status =  EFI_INVALID_PARAMETER;\r
-          goto Done;\r
-        }\r
-        if ((Entry->Start & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {\r
-          DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
-          Status =  EFI_INVALID_PARAMETER;\r
-          goto Done;\r
-        }\r
-        if (((Entry->End + 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {\r
-          DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
-          Status =  EFI_INVALID_PARAMETER;\r
-          goto Done;\r
+      if (Entry->Type < EfiMaxMemoryType) {\r
+        if (mMemoryTypeStatistics[Entry->Type].Runtime) {\r
+          ASSERT (Entry->Type != EfiACPIReclaimMemory);\r
+          ASSERT (Entry->Type != EfiACPIMemoryNVS);\r
+          if ((Entry->Start & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) != 0) {\r
+            DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
+            Status =  EFI_INVALID_PARAMETER;\r
+            goto Done;\r
+          }\r
+          if (((Entry->End + 1) & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) != 0) {\r
+            DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
+            Status =  EFI_INVALID_PARAMETER;\r
+            goto Done;\r
+          }\r
         }\r
       }\r
     }\r