]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Mem/Page.c
MdeModulePkg: Add New Memory Attributes
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Mem / Page.c
index c9219cc068b392c14052d77f0548715a86399146..2c2c9cd6c334ecaf3b143611f3793fa111699b5e 100644 (file)
@@ -1,19 +1,14 @@
 /** @file\r
   UEFI Memory page management functions.\r
 \r
-Copyright (c) 2007 - 2017, 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) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "DxeMain.h"\r
 #include "Imem.h"\r
+#include "HeapGuard.h"\r
 \r
 //\r
 // Entry for tracking the memory regions for each memory type to coalesce similar memory types\r
@@ -51,26 +46,26 @@ LIST_ENTRY   mFreeMemoryMapEntryList = INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemor
 BOOLEAN      mMemoryTypeInformationInitialized = FALSE;\r
 \r
 EFI_MEMORY_TYPE_STATISTICS mMemoryTypeStatistics[EfiMaxMemoryType + 1] = {\r
-  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  FALSE },  // EfiReservedMemoryType\r
-  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiLoaderCode\r
-  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiLoaderData\r
-  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiBootServicesCode\r
-  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiBootServicesData\r
-  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  TRUE  },  // EfiRuntimeServicesCode\r
-  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  TRUE  },  // EfiRuntimeServicesData\r
-  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiConventionalMemory\r
-  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiUnusableMemory\r
-  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  FALSE },  // EfiACPIReclaimMemory\r
-  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  FALSE },  // EfiACPIMemoryNVS\r
-  { 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
+  { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  FALSE },  // EfiReservedMemoryType\r
+  { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiLoaderCode\r
+  { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiLoaderData\r
+  { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiBootServicesCode\r
+  { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiBootServicesData\r
+  { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  TRUE  },  // EfiRuntimeServicesCode\r
+  { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  TRUE  },  // EfiRuntimeServicesData\r
+  { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiConventionalMemory\r
+  { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiUnusableMemory\r
+  { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  FALSE },  // EfiACPIReclaimMemory\r
+  { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  FALSE },  // EfiACPIMemoryNVS\r
+  { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiMemoryMappedIO\r
+  { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiMemoryMappedIOPortSpace\r
+  { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  TRUE  },  // EfiPalCode\r
+  { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiPersistentMemory\r
+  { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }   // EfiMaxMemoryType\r
 };\r
 \r
-EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress = MAX_ADDRESS;\r
-EFI_PHYSICAL_ADDRESS mDefaultBaseAddress = MAX_ADDRESS;\r
+EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress = MAX_ALLOC_ADDRESS;\r
+EFI_PHYSICAL_ADDRESS mDefaultBaseAddress = MAX_ALLOC_ADDRESS;\r
 \r
 EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = {\r
   { EfiReservedMemoryType,      0 },\r
@@ -92,7 +87,7 @@ EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = {
 };\r
 //\r
 // Only used when load module at fixed address feature is enabled. True means the memory is alreay successfully allocated\r
-// and ready to load the module in to specified address.or else, the memory is not ready and module will be loaded at a \r
+// and ready to load the module in to specified address.or else, the memory is not ready and module will be loaded at a\r
 //  address assigned by DXE core.\r
 //\r
 GLOBAL_REMOVE_IF_UNREFERENCED   BOOLEAN       gLoadFixedAddressCodeMemoryReady = FALSE;\r
@@ -177,22 +172,22 @@ CoreAddRange (
   ASSERT_LOCKED (&gMemoryLock);\r
 \r
   DEBUG ((DEBUG_PAGE, "AddRange: %lx-%lx to %d\n", Start, End, Type));\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
+\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
+  //\r
   if (Type == EfiConventionalMemory && Start == 0 && (End >= EFI_PAGE_SIZE - 1)) {\r
     if ((PcdGet8 (PcdNullPointerDetectionPropertyMask) & BIT0) == 0) {\r
       SetMem ((VOID *)(UINTN)Start, EFI_PAGE_SIZE, 0);\r
     }\r
   }\r
-  \r
+\r
   //\r
   // Memory map being altered so updated key\r
   //\r
@@ -287,9 +282,12 @@ AllocateMemoryMapEntry (
     //\r
     // The list is empty, to allocate one page to refuel the list\r
     //\r
-    FreeDescriptorEntries = CoreAllocatePoolPages (EfiBootServicesData,\r
+    FreeDescriptorEntries = CoreAllocatePoolPages (\r
+                              EfiBootServicesData,\r
                               EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION_GRANULARITY),\r
-                              DEFAULT_PAGE_ALLOCATION_GRANULARITY);\r
+                              DEFAULT_PAGE_ALLOCATION_GRANULARITY,\r
+                              FALSE\r
+                              );\r
     if (FreeDescriptorEntries != NULL) {\r
       //\r
       // Enque the free memmory map entries into the list\r
@@ -397,9 +395,12 @@ PromoteMemoryResource (
   VOID\r
   )\r
 {\r
-  LIST_ENTRY         *Link;\r
-  EFI_GCD_MAP_ENTRY  *Entry;\r
-  BOOLEAN            Promoted;\r
+  LIST_ENTRY                        *Link;\r
+  EFI_GCD_MAP_ENTRY                 *Entry;\r
+  BOOLEAN                           Promoted;\r
+  EFI_PHYSICAL_ADDRESS              StartAddress;\r
+  EFI_PHYSICAL_ADDRESS              EndAddress;\r
+  EFI_GCD_MEMORY_SPACE_DESCRIPTOR   Descriptor;\r
 \r
   DEBUG ((DEBUG_PAGE, "Promote the memory resource\n"));\r
 \r
@@ -412,7 +413,7 @@ PromoteMemoryResource (
     Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
 \r
     if (Entry->GcdMemoryType == EfiGcdMemoryTypeReserved &&\r
-        Entry->EndAddress < MAX_ADDRESS &&\r
+        Entry->EndAddress < MAX_ALLOC_ADDRESS &&\r
         (Entry->Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r
           (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)) {\r
       //\r
@@ -447,11 +448,29 @@ PromoteMemoryResource (
 \r
   CoreReleaseGcdMemoryLock ();\r
 \r
+  if (!Promoted) {\r
+    //\r
+    // If freed-memory guard is enabled, we could promote pages from\r
+    // guarded free pages.\r
+    //\r
+    Promoted = PromoteGuardedFreePages (&StartAddress, &EndAddress);\r
+    if (Promoted) {\r
+      CoreGetMemorySpaceDescriptor (StartAddress, &Descriptor);\r
+      CoreAddRange (\r
+        EfiConventionalMemory,\r
+        StartAddress,\r
+        EndAddress,\r
+        Descriptor.Capabilities & ~(EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED |\r
+                                    EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)\r
+        );\r
+    }\r
+  }\r
+\r
   return Promoted;\r
 }\r
 /**\r
-  This function try to allocate Runtime code & Boot time code memory range. If LMFA enabled, 2 patchable PCD \r
-  PcdLoadFixAddressRuntimeCodePageNumber & PcdLoadFixAddressBootTimeCodePageNumber which are set by tools will record the \r
+  This function try to allocate Runtime code & Boot time code memory range. If LMFA enabled, 2 patchable PCD\r
+  PcdLoadFixAddressRuntimeCodePageNumber & PcdLoadFixAddressBootTimeCodePageNumber which are set by tools will record the\r
   size of boot time and runtime code.\r
 \r
 **/\r
@@ -469,7 +488,7 @@ CoreLoadingFixedAddressHook (
    //\r
    // Make sure these 2 areas are not initialzied.\r
    //\r
-   if (!gLoadFixedAddressCodeMemoryReady) {   \r
+   if (!gLoadFixedAddressCodeMemoryReady) {\r
      RuntimeCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r
      BootTimeCodePageNumber= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r
      RuntimeCodeBase       = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress - EFI_PAGES_TO_SIZE (RuntimeCodePageNumber));\r
@@ -485,7 +504,7 @@ CoreLoadingFixedAddressHook (
                        );\r
      if (EFI_ERROR(Status)) {\r
        //\r
-       // Runtime memory allocation failed \r
+       // Runtime memory allocation failed\r
        //\r
        return;\r
      }\r
@@ -500,19 +519,19 @@ CoreLoadingFixedAddressHook (
                        );\r
      if (EFI_ERROR(Status)) {\r
        //\r
-        // boot memory allocation failed. Free Runtime code range and will try the allocation again when \r
-        // new memory range is installed.\r
-        //\r
-        CoreFreePages (\r
+        // boot memory allocation failed. Free Runtime code range and will try the allocation again when\r
+        // new memory range is installed.\r
+        //\r
+        CoreFreePages (\r
               RuntimeCodeBase,\r
               RuntimeCodePageNumber\r
               );\r
        return;\r
      }\r
      gLoadFixedAddressCodeMemoryReady = TRUE;\r
-   } \r
+   }\r
    return;\r
-}  \r
+}\r
 \r
 /**\r
   Called to initialize the memory map and add descriptors to\r
@@ -541,7 +560,7 @@ CoreAddMemoryDescriptor (
   EFI_STATUS                  Status;\r
   UINTN                       Index;\r
   UINTN                       FreeIndex;\r
-  \r
+\r
   if ((Start & EFI_PAGE_MASK) != 0) {\r
     return;\r
   }\r
@@ -564,7 +583,7 @@ CoreAddMemoryDescriptor (
   if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
     CoreLoadingFixedAddressHook();\r
   }\r
-  \r
+\r
   //\r
   // Check to see if the statistics for the different memory types have already been established\r
   //\r
@@ -572,7 +591,7 @@ CoreAddMemoryDescriptor (
     return;\r
   }\r
 \r
-  \r
+\r
   //\r
   // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array\r
   //\r
@@ -615,7 +634,7 @@ CoreAddMemoryDescriptor (
               gMemoryTypeInformation[FreeIndex].NumberOfPages\r
               );\r
             mMemoryTypeStatistics[Type].BaseAddress    = 0;\r
-            mMemoryTypeStatistics[Type].MaximumAddress = MAX_ADDRESS;\r
+            mMemoryTypeStatistics[Type].MaximumAddress = MAX_ALLOC_ADDRESS;\r
           }\r
         }\r
         return;\r
@@ -672,7 +691,7 @@ CoreAddMemoryDescriptor (
       }\r
     }\r
     mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0;\r
-    if (mMemoryTypeStatistics[Type].MaximumAddress == MAX_ADDRESS) {\r
+    if (mMemoryTypeStatistics[Type].MaximumAddress == MAX_ALLOC_ADDRESS) {\r
       mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress;\r
     }\r
   }\r
@@ -892,9 +911,15 @@ CoreConvertPagesEx (
     }\r
 \r
     //\r
-    // Add our new range in\r
+    // Add our new range in. Don't do this for freed pages if freed-memory\r
+    // guard is enabled.\r
     //\r
-    CoreAddRange (MemType, Start, RangeEnd, Attribute);\r
+    if (!IsHeapGuardEnabled (GUARD_HEAP_TYPE_FREED) ||\r
+        !ChangingType ||\r
+        MemType != EfiConventionalMemory) {\r
+      CoreAddRange (MemType, Start, RangeEnd, Attribute);\r
+    }\r
+\r
     if (ChangingType && (MemType == EfiConventionalMemory)) {\r
       //\r
       // Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this\r
@@ -993,6 +1018,7 @@ CoreUpdateMemoryAttributes (
   @param  NewType                The type of memory the range is going to be\r
                                  turned into\r
   @param  Alignment              Bits to align with\r
+  @param  NeedGuard              Flag to indicate Guard page is needed or not\r
 \r
   @return The base address of the range, or 0 if the range was not found\r
 \r
@@ -1003,7 +1029,8 @@ CoreFindFreePagesI (
   IN UINT64           MinAddress,\r
   IN UINT64           NumberOfPages,\r
   IN EFI_MEMORY_TYPE  NewType,\r
-  IN UINTN            Alignment\r
+  IN UINTN            Alignment,\r
+  IN BOOLEAN          NeedGuard\r
   )\r
 {\r
   UINT64          NumberOfBytes;\r
@@ -1095,6 +1122,17 @@ CoreFindFreePagesI (
       // If this is the best match so far remember it\r
       //\r
       if (DescEnd > Target) {\r
+        if (NeedGuard) {\r
+          DescEnd = AdjustMemoryS (\r
+                      DescEnd + 1 - DescNumberOfBytes,\r
+                      DescNumberOfBytes,\r
+                      NumberOfBytes\r
+                      );\r
+          if (DescEnd == 0) {\r
+            continue;\r
+          }\r
+        }\r
+\r
         Target = DescEnd;\r
       }\r
     }\r
@@ -1125,6 +1163,7 @@ CoreFindFreePagesI (
   @param  NewType                The type of memory the range is going to be\r
                                  turned into\r
   @param  Alignment              Bits to align with\r
+  @param  NeedGuard              Flag to indicate Guard page is needed or not\r
 \r
   @return The base address of the range, or 0 if the range was not found.\r
 \r
@@ -1134,7 +1173,8 @@ FindFreePages (
     IN UINT64           MaxAddress,\r
     IN UINT64           NoPages,\r
     IN EFI_MEMORY_TYPE  NewType,\r
-    IN UINTN            Alignment\r
+    IN UINTN            Alignment,\r
+    IN BOOLEAN          NeedGuard\r
     )\r
 {\r
   UINT64   Start;\r
@@ -1144,11 +1184,12 @@ FindFreePages (
   //\r
   if ((UINT32)NewType < EfiMaxMemoryType && MaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) {\r
     Start = CoreFindFreePagesI (\r
-              mMemoryTypeStatistics[NewType].MaximumAddress, \r
-              mMemoryTypeStatistics[NewType].BaseAddress, \r
-              NoPages, \r
-              NewType, \r
-              Alignment\r
+              mMemoryTypeStatistics[NewType].MaximumAddress,\r
+              mMemoryTypeStatistics[NewType].BaseAddress,\r
+              NoPages,\r
+              NewType,\r
+              Alignment,\r
+              NeedGuard\r
               );\r
     if (Start != 0) {\r
       return Start;\r
@@ -1159,7 +1200,8 @@ FindFreePages (
   // Attempt to find free pages in the default allocation bin\r
   //\r
   if (MaxAddress >= mDefaultMaximumAddress) {\r
-    Start = CoreFindFreePagesI (mDefaultMaximumAddress, 0, NoPages, NewType, Alignment);\r
+    Start = CoreFindFreePagesI (mDefaultMaximumAddress, 0, NoPages, NewType,\r
+                                Alignment, NeedGuard);\r
     if (Start != 0) {\r
       if (Start < mDefaultBaseAddress) {\r
         mDefaultBaseAddress = Start;\r
@@ -1169,12 +1211,13 @@ FindFreePages (
   }\r
 \r
   //\r
-  // The allocation did not succeed in any of the prefered bins even after \r
-  // promoting resources. Attempt to find free pages anywhere is the requested \r
-  // address range.  If this allocation fails, then there are not enough \r
+  // The allocation did not succeed in any of the prefered bins even after\r
+  // promoting resources. Attempt to find free pages anywhere is the requested\r
+  // address range.  If this allocation fails, then there are not enough\r
   // resources anywhere to satisfy the request.\r
   //\r
-  Start = CoreFindFreePagesI (MaxAddress, 0, NoPages, NewType, Alignment);\r
+  Start = CoreFindFreePagesI (MaxAddress, 0, NoPages, NewType, Alignment,\r
+                              NeedGuard);\r
   if (Start != 0) {\r
     return Start;\r
   }\r
@@ -1189,7 +1232,7 @@ FindFreePages (
   //\r
   // If any memory resources were promoted, then re-attempt the allocation\r
   //\r
-  return FindFreePages (MaxAddress, NoPages, NewType, Alignment);\r
+  return FindFreePages (MaxAddress, NoPages, NewType, Alignment, NeedGuard);\r
 }\r
 \r
 \r
@@ -1202,6 +1245,7 @@ FindFreePages (
   @param  NumberOfPages          The number of pages to allocate\r
   @param  Memory                 A pointer to receive the base allocated memory\r
                                  address\r
+  @param  NeedGuard              Flag to indicate Guard page is needed or not\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
@@ -1217,15 +1261,17 @@ CoreInternalAllocatePages (
   IN EFI_ALLOCATE_TYPE      Type,\r
   IN EFI_MEMORY_TYPE        MemoryType,\r
   IN UINTN                  NumberOfPages,\r
-  IN OUT EFI_PHYSICAL_ADDRESS  *Memory\r
+  IN OUT EFI_PHYSICAL_ADDRESS  *Memory,\r
+  IN BOOLEAN                NeedGuard\r
   )\r
 {\r
-  EFI_STATUS      Status;\r
-  UINT64          Start;\r
-  UINT64          NumberOfBytes;\r
-  UINT64          End;\r
-  UINT64          MaxAddress;\r
-  UINTN           Alignment;\r
+  EFI_STATUS       Status;\r
+  UINT64           Start;\r
+  UINT64           NumberOfBytes;\r
+  UINT64           End;\r
+  UINT64           MaxAddress;\r
+  UINTN            Alignment;\r
+  EFI_MEMORY_TYPE  CheckType;\r
 \r
   if ((UINT32)Type >= MaxAllocateType) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -1267,15 +1313,16 @@ CoreInternalAllocatePages (
   //\r
   // The max address is the max natively addressable address for the processor\r
   //\r
-  MaxAddress = MAX_ADDRESS;\r
+  MaxAddress = MAX_ALLOC_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 (NumberOfPages << EFI_PAGE_SHIFT) is above MAX_ALLOC_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
+  // if Start is above MAX_ALLOC_ADDRESS or\r
+  // if End is above MAX_ALLOC_ADDRESS,\r
+  // if Start..End overlaps any tracked MemoryTypeStatistics range\r
   // return EFI_NOT_FOUND.\r
   //\r
   if (Type == AllocateAddress) {\r
@@ -1287,10 +1334,37 @@ CoreInternalAllocatePages (
     End = Start + NumberOfBytes - 1;\r
 \r
     if ((Start >= End) ||\r
-        (Start > MaxAddress) || \r
+        (Start > MaxAddress) ||\r
         (End > MaxAddress)) {\r
       return EFI_NOT_FOUND;\r
     }\r
+\r
+    //\r
+    // A driver is allowed to call AllocatePages using an AllocateAddress type.  This type of\r
+    // AllocatePage request the exact physical address if it is not used.  The existing code\r
+    // will allow this request even in 'special' pages.  The problem with this is that the\r
+    // reason to have 'special' pages for OS hibernate/resume is defeated as memory is\r
+    // fragmented.\r
+    //\r
+\r
+    for (CheckType = (EFI_MEMORY_TYPE) 0; CheckType < EfiMaxMemoryType; CheckType++) {\r
+      if (MemoryType != CheckType &&\r
+          mMemoryTypeStatistics[CheckType].Special &&\r
+          mMemoryTypeStatistics[CheckType].NumberOfPages > 0) {\r
+        if (Start >= mMemoryTypeStatistics[CheckType].BaseAddress &&\r
+            Start <= mMemoryTypeStatistics[CheckType].MaximumAddress) {\r
+          return EFI_NOT_FOUND;\r
+        }\r
+        if (End >= mMemoryTypeStatistics[CheckType].BaseAddress &&\r
+            End <= mMemoryTypeStatistics[CheckType].MaximumAddress) {\r
+          return EFI_NOT_FOUND;\r
+        }\r
+        if (Start < mMemoryTypeStatistics[CheckType].BaseAddress &&\r
+            End   > mMemoryTypeStatistics[CheckType].MaximumAddress) {\r
+          return EFI_NOT_FOUND;\r
+        }\r
+      }\r
+    }\r
   }\r
 \r
   if (Type == AllocateMaxAddress) {\r
@@ -1303,7 +1377,8 @@ CoreInternalAllocatePages (
   // If not a specific address, then find an address to allocate\r
   //\r
   if (Type != AllocateAddress) {\r
-    Start = FindFreePages (MaxAddress, NumberOfPages, MemoryType, Alignment);\r
+    Start = FindFreePages (MaxAddress, NumberOfPages, MemoryType, Alignment,\r
+                           NeedGuard);\r
     if (Start == 0) {\r
       Status = EFI_OUT_OF_RESOURCES;\r
       goto Done;\r
@@ -1313,12 +1388,19 @@ CoreInternalAllocatePages (
   //\r
   // Convert pages from FreeMemory to the requested type\r
   //\r
-  Status = CoreConvertPages (Start, NumberOfPages, MemoryType);\r
+  if (NeedGuard) {\r
+    Status = CoreConvertPagesWithGuard(Start, NumberOfPages, MemoryType);\r
+  } else {\r
+    Status = CoreConvertPages(Start, NumberOfPages, MemoryType);\r
+  }\r
 \r
 Done:\r
   CoreReleaseMemoryLock ();\r
 \r
   if (!EFI_ERROR (Status)) {\r
+    if (NeedGuard) {\r
+      SetGuardForMemory (Start, NumberOfPages);\r
+    }\r
     *Memory = Start;\r
   }\r
 \r
@@ -1353,8 +1435,11 @@ CoreAllocatePages (
   )\r
 {\r
   EFI_STATUS  Status;\r
+  BOOLEAN     NeedGuard;\r
 \r
-  Status = CoreInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory);\r
+  NeedGuard = IsPageTypeToGuard (MemoryType, Type) && !mOnGuarding;\r
+  Status = CoreInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory,\r
+                                      NeedGuard);\r
   if (!EFI_ERROR (Status)) {\r
     CoreUpdateProfile (\r
       (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r
@@ -1395,6 +1480,7 @@ CoreInternalFreePages (
   LIST_ENTRY      *Link;\r
   MEMORY_MAP      *Entry;\r
   UINTN           Alignment;\r
+  BOOLEAN         IsGuarded;\r
 \r
   //\r
   // Free the range\r
@@ -1404,6 +1490,7 @@ CoreInternalFreePages (
   //\r
   // Find the entry that the covers the range\r
   //\r
+  IsGuarded = FALSE;\r
   Entry = NULL;\r
   for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
     Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
@@ -1440,10 +1527,13 @@ CoreInternalFreePages (
     *MemoryType = Entry->Type;\r
   }\r
 \r
-  Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    goto Done;\r
+  IsGuarded = IsPageTypeToGuard (Entry->Type, AllocateAnyPages) &&\r
+              IsMemoryGuarded (Memory);\r
+  if (IsGuarded) {\r
+    Status = CoreConvertPagesWithGuard (Memory, NumberOfPages,\r
+                                        EfiConventionalMemory);\r
+  } else {\r
+    Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
   }\r
 \r
 Done:\r
@@ -1474,6 +1564,7 @@ CoreFreePages (
 \r
   Status = CoreInternalFreePages (Memory, NumberOfPages, &MemoryType);\r
   if (!EFI_ERROR (Status)) {\r
+    GuardFreedPagesChecked (Memory, NumberOfPages);\r
     CoreUpdateProfile (\r
       (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r
       MemoryProfileActionFreePages,\r
@@ -1531,7 +1622,7 @@ MergeMemoryMapDescriptor (
     //\r
     // Check to see if MemoryMapDescriptor is immediately above MemoryMap\r
     //\r
-    if (MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) { \r
+    if (MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) {\r
       //\r
       // Merge MemoryMapDescriptor into MemoryMap\r
       //\r
@@ -1564,7 +1655,7 @@ MergeMemoryMapDescriptor (
   //\r
   // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap.\r
   //\r
-  // Return the slot immediately after MemoryMapDescriptor as the next available \r
+  // Return the slot immediately after MemoryMapDescriptor as the next available\r
   // slot in the MemoryMap array\r
   //\r
   return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor, DescriptorSize);\r
@@ -1620,6 +1711,7 @@ CoreGetMemoryMap (
   EFI_GCD_MAP_ENTRY                 MergeGcdMapEntry;\r
   EFI_MEMORY_TYPE                   Type;\r
   EFI_MEMORY_DESCRIPTOR             *MemoryMapStart;\r
+  EFI_MEMORY_DESCRIPTOR             *MemoryMapEnd;\r
 \r
   //\r
   // Make sure the parameters are valid\r
@@ -1637,7 +1729,7 @@ CoreGetMemoryMap (
   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 == EfiGcdMemoryTypePersistent) || \r
+    if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypePersistent) ||\r
         (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
         ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&\r
         ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME))) {\r
@@ -1723,13 +1815,13 @@ CoreGetMemoryMap (
     }\r
 \r
     //\r
-    // Check to see if the new Memory Map Descriptor can be merged with an \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
\r
+\r
   ZeroMem (&MergeGcdMapEntry, sizeof (MergeGcdMapEntry));\r
   GcdMapEntry = NULL;\r
   for (Link = mGcdMemorySpaceMap.ForwardLink; ; Link = Link->ForwardLink) {\r
@@ -1738,8 +1830,8 @@ CoreGetMemoryMap (
       // 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
+\r
+      if ((MergeGcdMapEntry.Capabilities == GcdMapEntry->Capabilities) &&\r
           (MergeGcdMapEntry.Attributes == GcdMapEntry->Attributes) &&\r
           (MergeGcdMapEntry.GcdMemoryType == GcdMapEntry->GcdMemoryType) &&\r
           (MergeGcdMapEntry.GcdIoType == GcdMapEntry->GcdIoType)) {\r
@@ -1752,21 +1844,20 @@ CoreGetMemoryMap (
         ((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
+      // 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
+\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
+      MemoryMap->Attribute     = (MergeGcdMapEntry.Attributes & ~EFI_MEMORY_PORT_IO) |\r
+                                (MergeGcdMapEntry.Capabilities & (EFI_CACHE_ATTRIBUTE_MASK | EFI_MEMORY_ATTRIBUTE_MASK));\r
 \r
       if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeReserved) {\r
         MemoryMap->Type = EfiReservedMemoryType;\r
@@ -1779,33 +1870,32 @@ CoreGetMemoryMap (
       }\r
 \r
       //\r
-      // Check to see if the new Memory Map Descriptor can be merged with an \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
+\r
     if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypePersistent) {\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
+      // 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
+      //\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->Attribute     = MergeGcdMapEntry.Attributes | EFI_MEMORY_NV |\r
+                                (MergeGcdMapEntry.Capabilities & (EFI_CACHE_ATTRIBUTE_MASK | EFI_MEMORY_ATTRIBUTE_MASK));\r
       MemoryMap->Type          = EfiPersistentMemory;\r
-      \r
+\r
       //\r
-      // Check to see if the new Memory Map Descriptor can be merged with an \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
@@ -1829,6 +1919,26 @@ CoreGetMemoryMap (
   //\r
   BufferSize = ((UINT8 *)MemoryMap - (UINT8 *)MemoryMapStart);\r
 \r
+  //\r
+  // Note: Some OSs will treat EFI_MEMORY_DESCRIPTOR.Attribute as really\r
+  //       set attributes and change memory paging attribute accordingly.\r
+  //       But current EFI_MEMORY_DESCRIPTOR.Attribute is assigned by\r
+  //       value from Capabilities in GCD memory map. This might cause\r
+  //       boot problems. Clearing all paging related capabilities can\r
+  //       workaround it. Following code is supposed to be removed once\r
+  //       the usage of EFI_MEMORY_DESCRIPTOR.Attribute is clarified in\r
+  //       UEFI spec and adopted by both EDK-II Core and all supported\r
+  //       OSs.\r
+  //\r
+  MemoryMapEnd = MemoryMap;\r
+  MemoryMap = MemoryMapStart;\r
+  while (MemoryMap < MemoryMapEnd) {\r
+    MemoryMap->Attribute &= ~(UINT64)EFI_MEMORY_ATTRIBUTE_MASK;\r
+    MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size);\r
+  }\r
+  MergeMemoryMap (MemoryMapStart, &BufferSize, Size);\r
+  MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMapStart + BufferSize);\r
+\r
   Status = EFI_SUCCESS;\r
 \r
 Done:\r
@@ -1845,6 +1955,10 @@ Done:
 \r
   *MemoryMapSize = BufferSize;\r
 \r
+  DEBUG_CODE (\r
+    DumpGuardedMemoryBitmap ();\r
+  );\r
+\r
   return Status;\r
 }\r
 \r
@@ -1856,6 +1970,7 @@ Done:
   @param  PoolType               The type of memory for the new pool pages\r
   @param  NumberOfPages          No of pages to allocate\r
   @param  Alignment              Bits to align.\r
+  @param  NeedGuard              Flag to indicate Guard page is needed or not\r
 \r
   @return The allocated memory, or NULL\r
 \r
@@ -1864,7 +1979,8 @@ VOID *
 CoreAllocatePoolPages (\r
   IN EFI_MEMORY_TYPE    PoolType,\r
   IN UINTN              NumberOfPages,\r
-  IN UINTN              Alignment\r
+  IN UINTN              Alignment,\r
+  IN BOOLEAN            NeedGuard\r
   )\r
 {\r
   UINT64            Start;\r
@@ -1872,7 +1988,8 @@ CoreAllocatePoolPages (
   //\r
   // Find the pages to convert\r
   //\r
-  Start = FindFreePages (MAX_ADDRESS, NumberOfPages, PoolType, Alignment);\r
+  Start = FindFreePages (MAX_ALLOC_ADDRESS, NumberOfPages, PoolType, Alignment,\r
+                         NeedGuard);\r
 \r
   //\r
   // Convert it to boot services data\r
@@ -1880,7 +1997,11 @@ CoreAllocatePoolPages (
   if (Start == 0) {\r
     DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32)NumberOfPages));\r
   } else {\r
-    CoreConvertPages (Start, NumberOfPages, PoolType);\r
+    if (NeedGuard) {\r
+      CoreConvertPagesWithGuard (Start, NumberOfPages, PoolType);\r
+    } else {\r
+      CoreConvertPages (Start, NumberOfPages, PoolType);\r
+    }\r
   }\r
 \r
   return (VOID *)(UINTN) Start;\r