]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Mem/Page.c
EmulatorPkg: Record Argc, Argv and Envp in EmuThunk Ppi
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Mem / Page.c
index b02bafb4bed9de2e66573f91f2290e7cc796caa0..160289c1f9ecc80dd054654746e9a611668c8793 100644 (file)
 /** @file\r
   UEFI Memory page management functions.\r
 \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
-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
-\r
-#define EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT  (EFI_PAGE_SIZE)\r
+#include "HeapGuard.h"\r
+#include <Pi/PrePiDxeCis.h>\r
 \r
 //\r
 // Entry for tracking the memory regions for each memory type to coalesce similar memory types\r
 //\r
 typedef struct {\r
-  EFI_PHYSICAL_ADDRESS  BaseAddress;\r
-  EFI_PHYSICAL_ADDRESS  MaximumAddress;\r
-  UINT64                CurrentNumberOfPages;\r
-  UINT64                NumberOfPages;\r
-  UINTN                 InformationIndex;\r
-  BOOLEAN               Special;\r
-  BOOLEAN               Runtime;\r
+  EFI_PHYSICAL_ADDRESS    BaseAddress;\r
+  EFI_PHYSICAL_ADDRESS    MaximumAddress;\r
+  UINT64                  CurrentNumberOfPages;\r
+  UINT64                  NumberOfPages;\r
+  UINTN                   InformationIndex;\r
+  BOOLEAN                 Special;\r
+  BOOLEAN                 Runtime;\r
 } EFI_MEMORY_TYPE_STATISTICS;\r
 \r
 //\r
 // MemoryMap - The current memory map\r
 //\r
-UINTN     mMemoryMapKey = 0;\r
+UINTN  mMemoryMapKey = 0;\r
 \r
-#define MAX_MAP_DEPTH 6\r
+#define MAX_MAP_DEPTH  6\r
 \r
 ///\r
 /// mMapDepth - depth of new descriptor stack\r
 ///\r
-UINTN         mMapDepth = 0;\r
+UINTN  mMapDepth = 0;\r
 ///\r
 /// mMapStack - space to use as temp storage to build new map descriptors\r
 ///\r
-MEMORY_MAP    mMapStack[MAX_MAP_DEPTH];\r
-UINTN         mFreeMapStack = 0;\r
+MEMORY_MAP  mMapStack[MAX_MAP_DEPTH];\r
+UINTN       mFreeMapStack = 0;\r
 ///\r
 /// This list maintain the free memory map list\r
 ///\r
-LIST_ENTRY   mFreeMemoryMapEntryList = INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList);\r
-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
+LIST_ENTRY  mFreeMemoryMapEntryList           = INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList);\r
+BOOLEAN     mMemoryTypeInformationInitialized = FALSE;\r
+\r
+EFI_MEMORY_TYPE_STATISTICS  mMemoryTypeStatistics[EfiMaxMemoryType + 1] = {\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, TRUE,  FALSE },  // EfiUnacceptedMemoryType\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
-\r
-EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = {\r
-  { EfiReservedMemoryType,      0 },\r
-  { EfiLoaderCode,              0 },\r
-  { EfiLoaderData,              0 },\r
-  { EfiBootServicesCode,        0 },\r
-  { EfiBootServicesData,        0 },\r
-  { EfiRuntimeServicesCode,     0 },\r
-  { EfiRuntimeServicesData,     0 },\r
-  { EfiConventionalMemory,      0 },\r
-  { EfiUnusableMemory,          0 },\r
-  { EfiACPIReclaimMemory,       0 },\r
-  { EfiACPIMemoryNVS,           0 },\r
-  { EfiMemoryMappedIO,          0 },\r
-  { EfiMemoryMappedIOPortSpace, 0 },\r
-  { EfiPalCode,                 0 },\r
-  { EfiPersistentMemory,        0 },\r
-  { EfiMaxMemoryType,           0 }\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
+  { EfiLoaderCode,                  0 },\r
+  { EfiLoaderData,                  0 },\r
+  { EfiBootServicesCode,            0 },\r
+  { EfiBootServicesData,            0 },\r
+  { EfiRuntimeServicesCode,         0 },\r
+  { EfiRuntimeServicesData,         0 },\r
+  { EfiConventionalMemory,          0 },\r
+  { EfiUnusableMemory,              0 },\r
+  { EfiACPIReclaimMemory,           0 },\r
+  { EfiACPIMemoryNVS,               0 },\r
+  { EfiMemoryMappedIO,              0 },\r
+  { EfiMemoryMappedIOPortSpace,     0 },\r
+  { EfiPalCode,                     0 },\r
+  { EfiPersistentMemory,            0 },\r
+  { EFI_GCD_MEMORY_TYPE_UNACCEPTED, 0 },\r
+  { EfiMaxMemoryType,               0 }\r
 };\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
+GLOBAL_REMOVE_IF_UNREFERENCED   BOOLEAN  gLoadFixedAddressCodeMemoryReady = FALSE;\r
 \r
 /**\r
   Enter critical section by gaining lock on gMemoryLock.\r
@@ -111,8 +107,6 @@ CoreAcquireMemoryLock (
   CoreAcquireLock (&gMemoryLock);\r
 }\r
 \r
-\r
-\r
 /**\r
   Exit critical section by releasing lock on gMemoryLock.\r
 \r
@@ -125,9 +119,6 @@ CoreReleaseMemoryLock (
   CoreReleaseLock (&gMemoryLock);\r
 }\r
 \r
-\r
-\r
-\r
 /**\r
   Internal function.  Removes a descriptor entry.\r
 \r
@@ -136,7 +127,7 @@ CoreReleaseMemoryLock (
 **/\r
 VOID\r
 RemoveMemoryMapEntry (\r
-  IN OUT MEMORY_MAP      *Entry\r
+  IN OUT MEMORY_MAP  *Entry\r
   )\r
 {\r
   RemoveEntryList (&Entry->Link);\r
@@ -164,35 +155,37 @@ RemoveMemoryMapEntry (
 **/\r
 VOID\r
 CoreAddRange (\r
-  IN EFI_MEMORY_TYPE          Type,\r
-  IN EFI_PHYSICAL_ADDRESS     Start,\r
-  IN EFI_PHYSICAL_ADDRESS     End,\r
-  IN UINT64                   Attribute\r
+  IN EFI_MEMORY_TYPE       Type,\r
+  IN EFI_PHYSICAL_ADDRESS  Start,\r
+  IN EFI_PHYSICAL_ADDRESS  End,\r
+  IN UINT64                Attribute\r
   )\r
 {\r
-  LIST_ENTRY        *Link;\r
-  MEMORY_MAP        *Entry;\r
+  LIST_ENTRY  *Link;\r
+  MEMORY_MAP  *Entry;\r
 \r
   ASSERT ((Start & EFI_PAGE_MASK) == 0);\r
-  ASSERT (End > Start) ;\r
+  ASSERT (End > Start);\r
 \r
   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
-  if (Type == EfiConventionalMemory && Start == 0 && (End >= EFI_PAGE_SIZE - 1)) {\r
-    SetMem ((VOID *)(UINTN)Start, EFI_PAGE_SIZE, 0);\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
@@ -204,7 +197,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
@@ -231,12 +224,9 @@ CoreAddRange (
     }\r
 \r
     if (Entry->End + 1 == Start) {\r
-\r
       Start = Entry->Start;\r
       RemoveMemoryMapEntry (Entry);\r
-\r
     } else if (Entry->Start == End + 1) {\r
-\r
       End = Entry->End;\r
       RemoveMemoryMapEntry (Entry);\r
     }\r
@@ -246,19 +236,19 @@ CoreAddRange (
   // Add descriptor\r
   //\r
 \r
-  mMapStack[mMapDepth].Signature     = MEMORY_MAP_SIGNATURE;\r
-  mMapStack[mMapDepth].FromPages      = FALSE;\r
-  mMapStack[mMapDepth].Type          = Type;\r
-  mMapStack[mMapDepth].Start         = Start;\r
-  mMapStack[mMapDepth].End           = End;\r
-  mMapStack[mMapDepth].VirtualStart  = 0;\r
-  mMapStack[mMapDepth].Attribute     = Attribute;\r
+  mMapStack[mMapDepth].Signature    = MEMORY_MAP_SIGNATURE;\r
+  mMapStack[mMapDepth].FromPages    = FALSE;\r
+  mMapStack[mMapDepth].Type         = Type;\r
+  mMapStack[mMapDepth].Start        = Start;\r
+  mMapStack[mMapDepth].End          = End;\r
+  mMapStack[mMapDepth].VirtualStart = 0;\r
+  mMapStack[mMapDepth].Attribute    = Attribute;\r
   InsertTailList (&gMemoryMap, &mMapStack[mMapDepth].Link);\r
 \r
   mMapDepth += 1;\r
   ASSERT (mMapDepth < MAX_MAP_DEPTH);\r
 \r
-  return ;\r
+  return;\r
 }\r
 \r
 /**\r
@@ -279,20 +269,25 @@ AllocateMemoryMapEntry (
   VOID\r
   )\r
 {\r
-  MEMORY_MAP*            FreeDescriptorEntries;\r
-  MEMORY_MAP*            Entry;\r
-  UINTN                  Index;\r
+  MEMORY_MAP  *FreeDescriptorEntries;\r
+  MEMORY_MAP  *Entry;\r
+  UINTN       Index;\r
 \r
   if (IsListEmpty (&mFreeMemoryMapEntryList)) {\r
     //\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 (\r
+                              EfiBootServicesData,\r
+                              EFI_SIZE_TO_PAGES (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
       //\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
@@ -300,6 +295,7 @@ AllocateMemoryMapEntry (
       return NULL;\r
     }\r
   }\r
+\r
   //\r
   // dequeue the first descriptor from the list\r
   //\r
@@ -309,7 +305,6 @@ AllocateMemoryMapEntry (
   return Entry;\r
 }\r
 \r
-\r
 /**\r
   Internal function.  Moves any memory descriptors that are on the\r
   temporary descriptor stack to heap.\r
@@ -320,9 +315,9 @@ CoreFreeMemoryMapStack (
   VOID\r
   )\r
 {\r
-  MEMORY_MAP      *Entry;\r
-  MEMORY_MAP      *Entry2;\r
-  LIST_ENTRY      *Link2;\r
+  MEMORY_MAP  *Entry;\r
+  MEMORY_MAP  *Entry2;\r
+  LIST_ENTRY  *Link2;\r
 \r
   ASSERT_LOCKED (&gMemoryLock);\r
 \r
@@ -330,7 +325,7 @@ CoreFreeMemoryMapStack (
   // If already freeing the map stack, then return\r
   //\r
   if (mFreeMapStack != 0) {\r
-    return ;\r
+    return;\r
   }\r
 \r
   //\r
@@ -352,14 +347,13 @@ CoreFreeMemoryMapStack (
     mMapDepth -= 1;\r
 \r
     if (mMapStack[mMapDepth].Link.ForwardLink != NULL) {\r
-\r
       //\r
       // Move this entry to general memory\r
       //\r
       RemoveEntryList (&mMapStack[mMapDepth].Link);\r
       mMapStack[mMapDepth].Link.ForwardLink = NULL;\r
 \r
-      CopyMem (Entry , &mMapStack[mMapDepth], sizeof (MEMORY_MAP));\r
+      CopyMem (Entry, &mMapStack[mMapDepth], sizeof (MEMORY_MAP));\r
       Entry->FromPages = TRUE;\r
 \r
       //\r
@@ -367,13 +361,12 @@ CoreFreeMemoryMapStack (
       //\r
       for (Link2 = gMemoryMap.ForwardLink; Link2 != &gMemoryMap; Link2 = Link2->ForwardLink) {\r
         Entry2 = CR (Link2, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
-        if (Entry2->FromPages && Entry2->Start > Entry->Start) {\r
+        if (Entry2->FromPages && (Entry2->Start > Entry->Start)) {\r
           break;\r
         }\r
       }\r
 \r
       InsertTailList (Link2, &Entry->Link);\r
-\r
     } else {\r
       //\r
       // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,\r
@@ -395,24 +388,27 @@ 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
   CoreAcquireGcdMemoryLock ();\r
 \r
   Promoted = FALSE;\r
-  Link = mGcdMemorySpaceMap.ForwardLink;\r
+  Link     = mGcdMemorySpaceMap.ForwardLink;\r
   while (Link != &mGcdMemorySpaceMap) {\r
-\r
     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->Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r
-          (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)) {\r
+    if ((Entry->GcdMemoryType == EfiGcdMemoryTypeReserved) &&\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
       //\r
       // Update the GCD map\r
       //\r
@@ -421,9 +417,10 @@ PromoteMemoryResource (
       } else {\r
         Entry->GcdMemoryType = EfiGcdMemoryTypeSystemMemory;\r
       }\r
+\r
       Entry->Capabilities |= EFI_MEMORY_TESTED;\r
-      Entry->ImageHandle  = gDxeCoreImageHandle;\r
-      Entry->DeviceHandle = NULL;\r
+      Entry->ImageHandle   = gDxeCoreImageHandle;\r
+      Entry->DeviceHandle  = NULL;\r
 \r
       //\r
       // Add to allocable system memory resource\r
@@ -445,11 +442,30 @@ 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
 /**\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
@@ -458,59 +474,62 @@ CoreLoadingFixedAddressHook (
   VOID\r
   )\r
 {\r
-   UINT32                     RuntimeCodePageNumber;\r
-   UINT32                     BootTimeCodePageNumber;\r
-   EFI_PHYSICAL_ADDRESS       RuntimeCodeBase;\r
-   EFI_PHYSICAL_ADDRESS       BootTimeCodeBase;\r
-   EFI_STATUS                 Status;\r
-\r
-   //\r
-   // Make sure these 2 areas are not initialzied.\r
-   //\r
-   if (!gLoadFixedAddressCodeMemoryReady) {   \r
-     RuntimeCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r
-     BootTimeCodePageNumber= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r
-     RuntimeCodeBase       = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress - EFI_PAGES_TO_SIZE (RuntimeCodePageNumber));\r
-     BootTimeCodeBase      = (EFI_PHYSICAL_ADDRESS)(RuntimeCodeBase - EFI_PAGES_TO_SIZE (BootTimeCodePageNumber));\r
-     //\r
-     // Try to allocate runtime memory.\r
-     //\r
-     Status = CoreAllocatePages (\r
-                       AllocateAddress,\r
-                       EfiRuntimeServicesCode,\r
-                       RuntimeCodePageNumber,\r
-                       &RuntimeCodeBase\r
-                       );\r
-     if (EFI_ERROR(Status)) {\r
-       //\r
-       // Runtime memory allocation failed \r
-       //\r
-       return;\r
-     }\r
-     //\r
-     // Try to allocate boot memory.\r
-     //\r
-     Status = CoreAllocatePages (\r
-                       AllocateAddress,\r
-                       EfiBootServicesCode,\r
-                       BootTimeCodePageNumber,\r
-                       &BootTimeCodeBase\r
-                       );\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
-              RuntimeCodeBase,\r
-              RuntimeCodePageNumber\r
-              );\r
-       return;\r
-     }\r
-     gLoadFixedAddressCodeMemoryReady = TRUE;\r
-   } \r
-   return;\r
-}  \r
+  UINT32                RuntimeCodePageNumber;\r
+  UINT32                BootTimeCodePageNumber;\r
+  EFI_PHYSICAL_ADDRESS  RuntimeCodeBase;\r
+  EFI_PHYSICAL_ADDRESS  BootTimeCodeBase;\r
+  EFI_STATUS            Status;\r
+\r
+  //\r
+  // Make sure these 2 areas are not initialzied.\r
+  //\r
+  if (!gLoadFixedAddressCodeMemoryReady) {\r
+    RuntimeCodePageNumber  = PcdGet32 (PcdLoadFixAddressRuntimeCodePageNumber);\r
+    BootTimeCodePageNumber = PcdGet32 (PcdLoadFixAddressBootTimeCodePageNumber);\r
+    RuntimeCodeBase        = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress - EFI_PAGES_TO_SIZE (RuntimeCodePageNumber));\r
+    BootTimeCodeBase       = (EFI_PHYSICAL_ADDRESS)(RuntimeCodeBase - EFI_PAGES_TO_SIZE (BootTimeCodePageNumber));\r
+    //\r
+    // Try to allocate runtime memory.\r
+    //\r
+    Status = CoreAllocatePages (\r
+               AllocateAddress,\r
+               EfiRuntimeServicesCode,\r
+               RuntimeCodePageNumber,\r
+               &RuntimeCodeBase\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      //\r
+      // Runtime memory allocation failed\r
+      //\r
+      return;\r
+    }\r
+\r
+    //\r
+    // Try to allocate boot memory.\r
+    //\r
+    Status = CoreAllocatePages (\r
+               AllocateAddress,\r
+               EfiBootServicesCode,\r
+               BootTimeCodePageNumber,\r
+               &BootTimeCodeBase\r
+               );\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
+        RuntimeCodeBase,\r
+        RuntimeCodePageNumber\r
+        );\r
+      return;\r
+    }\r
+\r
+    gLoadFixedAddressCodeMemoryReady = TRUE;\r
+  }\r
+\r
+  return;\r
+}\r
 \r
 /**\r
   Called to initialize the memory map and add descriptors to\r
@@ -535,31 +554,39 @@ CoreAddMemoryDescriptor (
   IN UINT64                Attribute\r
   )\r
 {\r
-  EFI_PHYSICAL_ADDRESS        End;\r
-  EFI_STATUS                  Status;\r
-  UINTN                       Index;\r
-  UINTN                       FreeIndex;\r
-  \r
+  EFI_PHYSICAL_ADDRESS  End;\r
+  EFI_STATUS            Status;\r
+  UINTN                 Index;\r
+  UINTN                 FreeIndex;\r
+\r
   if ((Start & EFI_PAGE_MASK) != 0) {\r
     return;\r
   }\r
 \r
-  if (Type >= EfiMaxMemoryType && Type < MEMORY_TYPE_OEM_RESERVED_MIN) {\r
+  if ((Type >= EfiMaxMemoryType) && (Type < MEMORY_TYPE_OEM_RESERVED_MIN)) {\r
     return;\r
   }\r
+\r
   CoreAcquireMemoryLock ();\r
   End = Start + LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT) - 1;\r
   CoreAddRange (Type, Start, End, Attribute);\r
   CoreFreeMemoryMapStack ();\r
   CoreReleaseMemoryLock ();\r
 \r
+  ApplyMemoryProtectionPolicy (\r
+    EfiMaxMemoryType,\r
+    Type,\r
+    Start,\r
+    LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT)\r
+    );\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
-  if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
-    CoreLoadingFixedAddressHook();\r
+  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
@@ -567,7 +594,6 @@ CoreAddMemoryDescriptor (
     return;\r
   }\r
 \r
-  \r
   //\r
   // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array\r
   //\r
@@ -575,10 +601,11 @@ CoreAddMemoryDescriptor (
     //\r
     // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
     //\r
-    Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r
+    Type = (EFI_MEMORY_TYPE)(gMemoryTypeInformation[Index].Type);\r
     if ((UINT32)Type > EfiMaxMemoryType) {\r
       continue;\r
     }\r
+\r
     if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
       //\r
       // Allocate pages for the current memory type from the top of available memory\r
@@ -599,7 +626,7 @@ CoreAddMemoryDescriptor (
           //\r
           // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
           //\r
-          Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[FreeIndex].Type);\r
+          Type = (EFI_MEMORY_TYPE)(gMemoryTypeInformation[FreeIndex].Type);\r
           if ((UINT32)Type > EfiMaxMemoryType) {\r
             continue;\r
           }\r
@@ -610,9 +637,10 @@ 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
+\r
         return;\r
       }\r
 \r
@@ -642,10 +670,11 @@ CoreAddMemoryDescriptor (
     //\r
     // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
     //\r
-    Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r
+    Type = (EFI_MEMORY_TYPE)(gMemoryTypeInformation[Index].Type);\r
     if ((UINT32)Type > EfiMaxMemoryType) {\r
       continue;\r
     }\r
+\r
     if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
       CoreFreePages (\r
         mMemoryTypeStatistics[Type].BaseAddress,\r
@@ -660,14 +689,15 @@ CoreAddMemoryDescriptor (
   // If the number of pages reserved for a memory type is 0, then all allocations for that type\r
   // should be in the default range.\r
   //\r
-  for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r
+  for (Type = (EFI_MEMORY_TYPE)0; Type < EfiMaxMemoryType; Type++) {\r
     for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
       if (Type == (EFI_MEMORY_TYPE)gMemoryTypeInformation[Index].Type) {\r
         mMemoryTypeStatistics[Type].InformationIndex = Index;\r
       }\r
     }\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
@@ -675,7 +705,6 @@ CoreAddMemoryDescriptor (
   mMemoryTypeInformationInitialized = TRUE;\r
 }\r
 \r
-\r
 /**\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
@@ -706,26 +735,25 @@ CoreConvertPagesEx (
   IN UINT64           NewAttributes\r
   )\r
 {\r
-\r
-  UINT64          NumberOfBytes;\r
-  UINT64          End;\r
-  UINT64          RangeEnd;\r
-  UINT64          Attribute;\r
-  EFI_MEMORY_TYPE MemType;\r
-  LIST_ENTRY      *Link;\r
-  MEMORY_MAP      *Entry;\r
-\r
-  Entry = NULL;\r
+  UINT64           NumberOfBytes;\r
+  UINT64           End;\r
+  UINT64           RangeEnd;\r
+  UINT64           Attribute;\r
+  EFI_MEMORY_TYPE  MemType;\r
+  LIST_ENTRY       *Link;\r
+  MEMORY_MAP       *Entry;\r
+\r
+  Entry         = NULL;\r
   NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
-  End = Start + NumberOfBytes - 1;\r
+  End           = Start + NumberOfBytes - 1;\r
 \r
   ASSERT (NumberOfPages);\r
   ASSERT ((Start & EFI_PAGE_MASK) == 0);\r
-  ASSERT (End > Start) ;\r
+  ASSERT (End > Start);\r
   ASSERT_LOCKED (&gMemoryLock);\r
-  ASSERT ( (ChangingType == FALSE) || (ChangingAttributes == FALSE) );\r
+  ASSERT ((ChangingType == FALSE) || (ChangingAttributes == FALSE));\r
 \r
-  if (NumberOfPages == 0 || ((Start & EFI_PAGE_MASK) != 0) || (Start >= End)) {\r
+  if ((NumberOfPages == 0) || ((Start & EFI_PAGE_MASK) != 0) || (Start >= End)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -734,14 +762,13 @@ CoreConvertPagesEx (
   //\r
 \r
   while (Start < End) {\r
-\r
     //\r
     // Find the entry that the covers the range\r
     //\r
     for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
       Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
 \r
-      if (Entry->Start <= Start && Entry->End > Start) {\r
+      if ((Entry->Start <= Start) && (Entry->End > Start)) {\r
         break;\r
       }\r
     }\r
@@ -751,6 +778,18 @@ CoreConvertPagesEx (
       return EFI_NOT_FOUND;\r
     }\r
 \r
+    //\r
+    // If we are converting the type of the range from EfiConventionalMemory to\r
+    // another type, we have to ensure that the entire range is covered by a\r
+    // single entry.\r
+    //\r
+    if (ChangingType && (NewType != EfiConventionalMemory)) {\r
+      if (Entry->End < End) {\r
+        DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: range %lx - %lx covers multiple entries\n", Start, End));\r
+        return EFI_NOT_FOUND;\r
+      }\r
+    }\r
+\r
     //\r
     // Convert range to the end, or to the end of the descriptor\r
     // if that's all we've got\r
@@ -765,6 +804,7 @@ CoreConvertPagesEx (
     if (ChangingType) {\r
       DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to type %d\n", Start, RangeEnd, NewType));\r
     }\r
+\r
     if (ChangingAttributes) {\r
       DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to attr %lx\n", Start, RangeEnd, NewAttributes));\r
     }\r
@@ -773,8 +813,14 @@ CoreConvertPagesEx (
       //\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
+      if (!((NewType == EfiConventionalMemory) ? 1 : 0) ^ ((Entry->Type == EfiConventionalMemory) ? 1 : 0)) {\r
+        DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: Incompatible memory types, "));\r
+        if (Entry->Type == EfiConventionalMemory) {\r
+          DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "the pages to free have been freed\n"));\r
+        } else {\r
+          DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "the pages to allocate have been allocated\n"));\r
+        }\r
+\r
         return EFI_NOT_FOUND;\r
       }\r
 \r
@@ -782,8 +828,9 @@ CoreConvertPagesEx (
       // 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 (((Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress) && (Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress)) ||\r
+            ((Start >= mDefaultBaseAddress) && (Start <= mDefaultMaximumAddress)))\r
+        {\r
           if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) {\r
             mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0;\r
           } else {\r
@@ -793,8 +840,9 @@ CoreConvertPagesEx (
       }\r
 \r
       if ((UINT32)NewType < EfiMaxMemoryType) {\r
-        if ((Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) ||\r
-            (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress)                                                  ) {\r
+        if (((Start >= mMemoryTypeStatistics[NewType].BaseAddress) && (Start <= mMemoryTypeStatistics[NewType].MaximumAddress)) ||\r
+            ((Start >= mDefaultBaseAddress) && (Start <= mDefaultMaximumAddress)))\r
+        {\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
@@ -807,21 +855,16 @@ CoreConvertPagesEx (
     // Pull range out of descriptor\r
     //\r
     if (Entry->Start == Start) {\r
-\r
       //\r
       // Clip start\r
       //\r
       Entry->Start = RangeEnd + 1;\r
-\r
     } else if (Entry->End == RangeEnd) {\r
-\r
       //\r
       // Clip end\r
       //\r
       Entry->End = Start - 1;\r
-\r
     } else {\r
-\r
       //\r
       // Pull it out of the center, clip current\r
       //\r
@@ -830,7 +873,7 @@ CoreConvertPagesEx (
       // Add a new one\r
       //\r
       mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r
-      mMapStack[mMapDepth].FromPages  = FALSE;\r
+      mMapStack[mMapDepth].FromPages = FALSE;\r
       mMapStack[mMapDepth].Type      = Entry->Type;\r
       mMapStack[mMapDepth].Start     = RangeEnd+1;\r
       mMapStack[mMapDepth].End       = Entry->End;\r
@@ -856,10 +899,10 @@ CoreConvertPagesEx (
     //\r
     if (ChangingType) {\r
       Attribute = Entry->Attribute;\r
-      MemType = NewType;\r
+      MemType   = NewType;\r
     } else {\r
       Attribute = NewAttributes;\r
-      MemType = Entry->Type;\r
+      MemType   = Entry->Type;\r
     }\r
 \r
     //\r
@@ -871,9 +914,16 @@ 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
+    {\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
@@ -882,10 +932,10 @@ CoreConvertPagesEx (
       //\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
+          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
+        DEBUG_CLEAR_MEMORY ((VOID *)(UINTN)Start, (UINTN)(RangeEnd - Start + 1));\r
       }\r
     }\r
 \r
@@ -907,7 +957,6 @@ CoreConvertPagesEx (
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Internal function.  Converts a memory range to the specified type.\r
   The range must exist in the memory map.\r
@@ -931,10 +980,9 @@ CoreConvertPages (
   IN EFI_MEMORY_TYPE  NewType\r
   )\r
 {\r
-  return CoreConvertPagesEx(Start, NumberOfPages, TRUE, NewType, FALSE, 0);\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
@@ -956,12 +1004,11 @@ CoreUpdateMemoryAttributes (
   //\r
   // Update the attributes to the new value\r
   //\r
-  CoreConvertPagesEx(Start, NumberOfPages, FALSE, (EFI_MEMORY_TYPE)0, TRUE, NewAttributes);\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
   the requested address.\r
@@ -972,6 +1019,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
@@ -982,23 +1030,23 @@ 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
-  UINT64          Target;\r
-  UINT64          DescStart;\r
-  UINT64          DescEnd;\r
-  UINT64          DescNumberOfBytes;\r
-  LIST_ENTRY      *Link;\r
-  MEMORY_MAP      *Entry;\r
-\r
-  if ((MaxAddress < EFI_PAGE_MASK) ||(NumberOfPages == 0)) {\r
+  UINT64      NumberOfBytes;\r
+  UINT64      Target;\r
+  UINT64      DescStart;\r
+  UINT64      DescEnd;\r
+  UINT64      DescNumberOfBytes;\r
+  LIST_ENTRY  *Link;\r
+  MEMORY_MAP  *Entry;\r
+\r
+  if ((MaxAddress < EFI_PAGE_MASK) || (NumberOfPages == 0)) {\r
     return 0;\r
   }\r
 \r
   if ((MaxAddress & EFI_PAGE_MASK) != EFI_PAGE_MASK) {\r
-\r
     //\r
     // If MaxAddress is not aligned to the end of a page\r
     //\r
@@ -1020,7 +1068,7 @@ CoreFindFreePagesI (
   }\r
 \r
   NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
-  Target = 0;\r
+  Target        = 0;\r
 \r
   for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
     Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
@@ -1033,7 +1081,7 @@ CoreFindFreePagesI (
     }\r
 \r
     DescStart = Entry->Start;\r
-    DescEnd = Entry->End;\r
+    DescEnd   = Entry->End;\r
 \r
     //\r
     // If desc is past max allowed address or below min allowed address, skip it\r
@@ -1074,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
@@ -1094,7 +1153,6 @@ CoreFindFreePagesI (
   return Target;\r
 }\r
 \r
-\r
 /**\r
   Internal function.  Finds a consecutive free page range below\r
   the requested address\r
@@ -1104,30 +1162,33 @@ 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
 **/\r
 UINT64\r
 FindFreePages (\r
-    IN UINT64           MaxAddress,\r
-    IN UINT64           NoPages,\r
-    IN EFI_MEMORY_TYPE  NewType,\r
-    IN UINTN            Alignment\r
-    )\r
+  IN UINT64           MaxAddress,\r
+  IN UINT64           NoPages,\r
+  IN EFI_MEMORY_TYPE  NewType,\r
+  IN UINTN            Alignment,\r
+  IN BOOLEAN          NeedGuard\r
+  )\r
 {\r
-  UINT64   Start;\r
+  UINT64  Start;\r
 \r
   //\r
   // Attempt to find free pages in the preferred bin based on the requested memory type\r
   //\r
-  if ((UINT32)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
-              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
@@ -1138,22 +1199,37 @@ 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 (\r
+              mDefaultMaximumAddress,\r
+              0,\r
+              NoPages,\r
+              NewType,\r
+              Alignment,\r
+              NeedGuard\r
+              );\r
     if (Start != 0) {\r
       if (Start < mDefaultBaseAddress) {\r
         mDefaultBaseAddress = Start;\r
       }\r
+\r
       return Start;\r
     }\r
   }\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 (\r
+            MaxAddress,\r
+            0,\r
+            NoPages,\r
+            NewType,\r
+            Alignment,\r
+            NeedGuard\r
+            );\r
   if (Start != 0) {\r
     return Start;\r
   }\r
@@ -1168,10 +1244,9 @@ 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
 /**\r
   Allocates pages from the memory map.\r
 \r
@@ -1181,6 +1256,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
@@ -1193,25 +1269,28 @@ FindFreePages (
 EFI_STATUS\r
 EFIAPI\r
 CoreInternalAllocatePages (\r
-  IN EFI_ALLOCATE_TYPE      Type,\r
-  IN EFI_MEMORY_TYPE        MemoryType,\r
-  IN UINTN                  NumberOfPages,\r
-  IN OUT EFI_PHYSICAL_ADDRESS  *Memory\r
+  IN EFI_ALLOCATE_TYPE         Type,\r
+  IN EFI_MEMORY_TYPE           MemoryType,\r
+  IN UINTN                     NumberOfPages,\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
   }\r
 \r
-  if ((MemoryType >= EfiMaxMemoryType && MemoryType < MEMORY_TYPE_OEM_RESERVED_MIN) ||\r
-       (MemoryType == EfiConventionalMemory) || (MemoryType == EfiPersistentMemory)) {\r
+  if (((MemoryType >= EfiMaxMemoryType) && (MemoryType < MEMORY_TYPE_OEM_RESERVED_MIN)) ||\r
+      (MemoryType == EfiConventionalMemory) || (MemoryType == EfiPersistentMemory) || (MemoryType == EfiUnacceptedMemoryType))\r
+  {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -1219,14 +1298,14 @@ CoreInternalAllocatePages (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;\r
-\r
-  if  (MemoryType == EfiACPIReclaimMemory   ||\r
-       MemoryType == EfiACPIMemoryNVS       ||\r
-       MemoryType == EfiRuntimeServicesCode ||\r
-       MemoryType == EfiRuntimeServicesData) {\r
+  Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r
 \r
-    Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
+  if ((MemoryType == EfiACPIReclaimMemory) ||\r
+      (MemoryType == EfiACPIMemoryNVS) ||\r
+      (MemoryType == EfiRuntimeServicesCode) ||\r
+      (MemoryType == EfiRuntimeServicesData))\r
+  {\r
+    Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r
   }\r
 \r
   if (Type == AllocateAddress) {\r
@@ -1246,30 +1325,67 @@ 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
     if ((NumberOfPages == 0) ||\r
-        (NumberOfPages > RShiftU64 (MaxAddress, EFI_PAGE_SHIFT))) {\r
+        (NumberOfPages > RShiftU64 (MaxAddress, EFI_PAGE_SHIFT)))\r
+    {\r
       return EFI_NOT_FOUND;\r
     }\r
+\r
     NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
-    End = Start + NumberOfBytes - 1;\r
+    End           = Start + NumberOfBytes - 1;\r
 \r
     if ((Start >= End) ||\r
-        (Start > MaxAddress) || \r
-        (End > MaxAddress)) {\r
+        (Start > MaxAddress) ||\r
+        (End > MaxAddress))\r
+    {\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
+      {\r
+        if ((Start >= mMemoryTypeStatistics[CheckType].BaseAddress) &&\r
+            (Start <= mMemoryTypeStatistics[CheckType].MaximumAddress))\r
+        {\r
+          return EFI_NOT_FOUND;\r
+        }\r
+\r
+        if ((End >= mMemoryTypeStatistics[CheckType].BaseAddress) &&\r
+            (End <= mMemoryTypeStatistics[CheckType].MaximumAddress))\r
+        {\r
+          return EFI_NOT_FOUND;\r
+        }\r
+\r
+        if ((Start < mMemoryTypeStatistics[CheckType].BaseAddress) &&\r
+            (End   > mMemoryTypeStatistics[CheckType].MaximumAddress))\r
+        {\r
+          return EFI_NOT_FOUND;\r
+        }\r
+      }\r
+    }\r
   }\r
 \r
   if (Type == AllocateMaxAddress) {\r
@@ -1282,7 +1398,13 @@ 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 (\r
+              MaxAddress,\r
+              NumberOfPages,\r
+              MemoryType,\r
+              Alignment,\r
+              NeedGuard\r
+              );\r
     if (Start == 0) {\r
       Status = EFI_OUT_OF_RESOURCES;\r
       goto Done;\r
@@ -1292,12 +1414,34 @@ 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
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // If requested memory region is unavailable it may be untested memory\r
+    // Attempt to promote memory resources, then re-attempt the allocation\r
+    //\r
+    if (PromoteMemoryResource ()) {\r
+      if (NeedGuard) {\r
+        Status = CoreConvertPagesWithGuard (Start, NumberOfPages, MemoryType);\r
+      } else {\r
+        Status = CoreConvertPages (Start, NumberOfPages, MemoryType);\r
+      }\r
+    }\r
+  }\r
 \r
 Done:\r
   CoreReleaseMemoryLock ();\r
 \r
   if (!EFI_ERROR (Status)) {\r
+    if (NeedGuard) {\r
+      SetGuardForMemory (Start, NumberOfPages);\r
+    }\r
+\r
     *Memory = Start;\r
   }\r
 \r
@@ -1332,19 +1476,34 @@ CoreAllocatePages (
   )\r
 {\r
   EFI_STATUS  Status;\r
-\r
-  Status = CoreInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory);\r
+  BOOLEAN     NeedGuard;\r
+\r
+  NeedGuard = IsPageTypeToGuard (MemoryType, Type) && !mOnGuarding;\r
+  Status    = CoreInternalAllocatePages (\r
+                Type,\r
+                MemoryType,\r
+                NumberOfPages,\r
+                Memory,\r
+                NeedGuard\r
+                );\r
   if (!EFI_ERROR (Status)) {\r
     CoreUpdateProfile (\r
-      (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r
+      (EFI_PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MemoryProfileActionAllocatePages,\r
       MemoryType,\r
       EFI_PAGES_TO_SIZE (NumberOfPages),\r
-      (VOID *) (UINTN) *Memory,\r
+      (VOID *)(UINTN)*Memory,\r
       NULL\r
       );\r
     InstallMemoryAttributesTableOnMemoryAllocation (MemoryType);\r
+    ApplyMemoryProtectionPolicy (\r
+      EfiConventionalMemory,\r
+      MemoryType,\r
+      *Memory,\r
+      EFI_PAGES_TO_SIZE (NumberOfPages)\r
+      );\r
   }\r
+\r
   return Status;\r
 }\r
 \r
@@ -1363,15 +1522,16 @@ CoreAllocatePages (
 EFI_STATUS\r
 EFIAPI\r
 CoreInternalFreePages (\r
-  IN EFI_PHYSICAL_ADDRESS   Memory,\r
-  IN UINTN                  NumberOfPages,\r
-  OUT EFI_MEMORY_TYPE       *MemoryType OPTIONAL\r
+  IN EFI_PHYSICAL_ADDRESS  Memory,\r
+  IN UINTN                 NumberOfPages,\r
+  OUT EFI_MEMORY_TYPE      *MemoryType OPTIONAL\r
   )\r
 {\r
-  EFI_STATUS      Status;\r
-  LIST_ENTRY      *Link;\r
-  MEMORY_MAP      *Entry;\r
-  UINTN           Alignment;\r
+  EFI_STATUS  Status;\r
+  LIST_ENTRY  *Link;\r
+  MEMORY_MAP  *Entry;\r
+  UINTN       Alignment;\r
+  BOOLEAN     IsGuarded;\r
 \r
   //\r
   // Free the range\r
@@ -1381,28 +1541,29 @@ CoreInternalFreePages (
   //\r
   // Find the entry that the covers the range\r
   //\r
-  Entry = NULL;\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
-    if (Entry->Start <= Memory && Entry->End > Memory) {\r
-        break;\r
+    Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
+    if ((Entry->Start <= Memory) && (Entry->End > Memory)) {\r
+      break;\r
     }\r
   }\r
+\r
   if (Link == &gMemoryMap) {\r
     Status = EFI_NOT_FOUND;\r
     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
-       Entry->Type == EfiACPIMemoryNVS       ||\r
-       Entry->Type == EfiRuntimeServicesCode ||\r
-       Entry->Type == EfiRuntimeServicesData) {\r
-\r
-    Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
-\r
+  if ((Entry->Type == EfiACPIReclaimMemory) ||\r
+      (Entry->Type == EfiACPIMemoryNVS) ||\r
+      (Entry->Type == EfiRuntimeServicesCode) ||\r
+      (Entry->Type == EfiRuntimeServicesData))\r
+  {\r
+    Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r
   }\r
 \r
   if ((Memory & (Alignment - 1)) != 0) {\r
@@ -1417,10 +1578,16 @@ 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 (\r
+               Memory,\r
+               NumberOfPages,\r
+               EfiConventionalMemory\r
+               );\r
+  } else {\r
+    Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
   }\r
 \r
 Done:\r
@@ -1446,21 +1613,29 @@ CoreFreePages (
   IN UINTN                 NumberOfPages\r
   )\r
 {\r
-  EFI_STATUS        Status;\r
-  EFI_MEMORY_TYPE   MemoryType;\r
+  EFI_STATUS       Status;\r
+  EFI_MEMORY_TYPE  MemoryType;\r
 \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
+      (EFI_PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MemoryProfileActionFreePages,\r
       MemoryType,\r
       EFI_PAGES_TO_SIZE (NumberOfPages),\r
-      (VOID *) (UINTN) Memory,\r
+      (VOID *)(UINTN)Memory,\r
       NULL\r
       );\r
     InstallMemoryAttributesTableOnMemoryAllocation (MemoryType);\r
+    ApplyMemoryProtectionPolicy (\r
+      MemoryType,\r
+      EfiConventionalMemory,\r
+      Memory,\r
+      EFI_PAGES_TO_SIZE (NumberOfPages)\r
+      );\r
   }\r
+\r
   return Status;\r
 }\r
 \r
@@ -1488,7 +1663,7 @@ MergeMemoryMapDescriptor (
   //\r
   // Traverse the array of descriptors in MemoryMap\r
   //\r
-  for (; MemoryMap != MemoryMapDescriptor; MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize)) {\r
+  for ( ; MemoryMap != MemoryMapDescriptor; MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize)) {\r
     //\r
     // Check to see if the Type fields are identical.\r
     //\r
@@ -1506,7 +1681,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
@@ -1539,7 +1714,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
@@ -1585,16 +1760,17 @@ CoreGetMemoryMap (
   OUT UINT32                    *DescriptorVersion\r
   )\r
 {\r
-  EFI_STATUS                        Status;\r
-  UINTN                             Size;\r
-  UINTN                             BufferSize;\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
+  EFI_STATUS             Status;\r
+  UINTN                  Size;\r
+  UINTN                  BufferSize;\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
+  EFI_MEMORY_DESCRIPTOR  *MemoryMapEnd;\r
 \r
   //\r
   // Make sure the parameters are valid\r
@@ -1612,11 +1788,12 @@ 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 == EfiGcdMemoryTypePersistentMemory) || \r
+    if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypePersistent) ||\r
         (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
         ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&\r
-        ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME))) {\r
-      NumberOfEntries ++;\r
+         ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME)))\r
+    {\r
+      NumberOfEntries++;\r
     }\r
   }\r
 \r
@@ -1627,7 +1804,7 @@ CoreGetMemoryMap (
   // prevent people from having pointer math bugs in their code.\r
   // now you have to use *DescriptorSize to make things work.\r
   //\r
-  Size += sizeof(UINT64) - (Size % sizeof (UINT64));\r
+  Size += sizeof (UINT64) - (Size % sizeof (UINT64));\r
 \r
   if (DescriptorSize != NULL) {\r
     *DescriptorSize = Size;\r
@@ -1669,10 +1846,10 @@ CoreGetMemoryMap (
     //\r
     // Convert internal map into an EFI_MEMORY_DESCRIPTOR\r
     //\r
-    MemoryMap->Type           = Entry->Type;\r
-    MemoryMap->PhysicalStart  = Entry->Start;\r
-    MemoryMap->VirtualStart   = Entry->VirtualStart;\r
-    MemoryMap->NumberOfPages  = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);\r
+    MemoryMap->Type          = Entry->Type;\r
+    MemoryMap->PhysicalStart = Entry->Start;\r
+    MemoryMap->VirtualStart  = Entry->VirtualStart;\r
+    MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);\r
     //\r
     // If the memory type is EfiConventionalMemory, then determine if the range is part of a\r
     // memory type bin and needs to be converted to the same memory type as the rest of the\r
@@ -1681,15 +1858,17 @@ CoreGetMemoryMap (
     // differences across reboots.\r
     //\r
     if (MemoryMap->Type == EfiConventionalMemory) {\r
-      for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r
+      for (Type = (EFI_MEMORY_TYPE)0; Type < EfiMaxMemoryType; Type++) {\r
         if (mMemoryTypeStatistics[Type].Special                        &&\r
-            mMemoryTypeStatistics[Type].NumberOfPages > 0              &&\r
-            Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress    &&\r
-            Entry->End   <= mMemoryTypeStatistics[Type].MaximumAddress) {\r
+            (mMemoryTypeStatistics[Type].NumberOfPages > 0) &&\r
+            (Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress) &&\r
+            (Entry->End   <= mMemoryTypeStatistics[Type].MaximumAddress))\r
+        {\r
           MemoryMap->Type = Type;\r
         }\r
       }\r
     }\r
+\r
     MemoryMap->Attribute = Entry->Attribute;\r
     if (MemoryMap->Type < EfiMaxMemoryType) {\r
       if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) {\r
@@ -1698,13 +1877,12 @@ 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
   ZeroMem (&MergeGcdMapEntry, sizeof (MergeGcdMapEntry));\r
   GcdMapEntry = NULL;\r
   for (Link = mGcdMemorySpaceMap.ForwardLink; ; Link = Link->ForwardLink) {\r
@@ -1713,35 +1891,36 @@ 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
-        MergeGcdMapEntry.EndAddress  = GcdMapEntry->EndAddress;\r
+          (MergeGcdMapEntry.GcdIoType == GcdMapEntry->GcdIoType))\r
+      {\r
+        MergeGcdMapEntry.EndAddress = GcdMapEntry->EndAddress;\r
         continue;\r
       }\r
     }\r
 \r
     if ((MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
         ((MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&\r
-        ((MergeGcdMapEntry.Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME))) {\r
+         ((MergeGcdMapEntry.Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME)))\r
+    {\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
@@ -1754,43 +1933,70 @@ 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
-    if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypePersistentMemory) {\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->Type          = EfiPersistentMemory;\r
-      \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
-      // 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
+    if (MergeGcdMapEntry.GcdMemoryType == EFI_GCD_MEMORY_TYPE_UNACCEPTED) {\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 Unaccepted 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 |\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 = EfiUnacceptedMemoryType;\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 (Link == &mGcdMemorySpaceMap) {\r
       //\r
       // break loop when arrive at head.\r
       //\r
       break;\r
     }\r
+\r
     if (GcdMapEntry != NULL) {\r
       //\r
       // Copy new GCD map entry for the following GCD range merge\r
@@ -1804,6 +2010,27 @@ 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 page-access permission related\r
+  //       capabilities can workaround it. Following code is supposed to\r
+  //       be removed once the usage of EFI_MEMORY_DESCRIPTOR.Attribute\r
+  //       is clarified in UEFI spec and adopted by both EDK-II Core and\r
+  //       all supported OSs.\r
+  //\r
+  MemoryMapEnd = MemoryMap;\r
+  MemoryMap    = MemoryMapStart;\r
+  while (MemoryMap < MemoryMapEnd) {\r
+    MemoryMap->Attribute &= ~(UINT64)EFI_MEMORY_ACCESS_MASK;\r
+    MemoryMap             = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size);\r
+  }\r
+\r
+  MergeMemoryMap (MemoryMapStart, &BufferSize, Size);\r
+  MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMapStart + BufferSize);\r
+\r
   Status = EFI_SUCCESS;\r
 \r
 Done:\r
@@ -1820,10 +2047,13 @@ Done:
 \r
   *MemoryMapSize = BufferSize;\r
 \r
+  DEBUG_CODE (\r
+    DumpGuardedMemoryBitmap ();\r
+    );\r
+\r
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Internal function.  Used by the pool functions to allocate pages\r
   to back pool allocation requests.\r
@@ -1831,23 +2061,31 @@ 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
 **/\r
 VOID *\r
 CoreAllocatePoolPages (\r
-  IN EFI_MEMORY_TYPE    PoolType,\r
-  IN UINTN              NumberOfPages,\r
-  IN UINTN              Alignment\r
+  IN EFI_MEMORY_TYPE  PoolType,\r
+  IN UINTN            NumberOfPages,\r
+  IN UINTN            Alignment,\r
+  IN BOOLEAN          NeedGuard\r
   )\r
 {\r
-  UINT64            Start;\r
+  UINT64  Start;\r
 \r
   //\r
   // Find the pages to convert\r
   //\r
-  Start = FindFreePages (MAX_ADDRESS, NumberOfPages, PoolType, Alignment);\r
+  Start = FindFreePages (\r
+            MAX_ALLOC_ADDRESS,\r
+            NumberOfPages,\r
+            PoolType,\r
+            Alignment,\r
+            NeedGuard\r
+            );\r
 \r
   //\r
   // Convert it to boot services data\r
@@ -1855,13 +2093,16 @@ 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
+  return (VOID *)(UINTN)Start;\r
 }\r
 \r
-\r
 /**\r
   Internal function.  Frees pool pages allocated via AllocatePoolPages ()\r
 \r
@@ -1871,15 +2112,13 @@ CoreAllocatePoolPages (
 **/\r
 VOID\r
 CoreFreePoolPages (\r
-  IN EFI_PHYSICAL_ADDRESS   Memory,\r
-  IN UINTN                  NumberOfPages\r
+  IN EFI_PHYSICAL_ADDRESS  Memory,\r
+  IN UINTN                 NumberOfPages\r
   )\r
 {\r
   CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
 }\r
 \r
-\r
-\r
 /**\r
   Make sure the memory map is following all the construction rules,\r
   it is the last time to check memory map error before exit boot services.\r
@@ -1893,19 +2132,18 @@ CoreFreePoolPages (
 **/\r
 EFI_STATUS\r
 CoreTerminateMemoryMap (\r
-  IN UINTN          MapKey\r
+  IN UINTN  MapKey\r
   )\r
 {\r
-  EFI_STATUS        Status;\r
-  LIST_ENTRY        *Link;\r
-  MEMORY_MAP        *Entry;\r
+  EFI_STATUS  Status;\r
+  LIST_ENTRY  *Link;\r
+  MEMORY_MAP  *Entry;\r
 \r
   Status = EFI_SUCCESS;\r
 \r
   CoreAcquireMemoryLock ();\r
 \r
   if (MapKey == mMemoryMapKey) {\r
-\r
     //\r
     // Make sure the memory map is following all the construction rules\r
     // This is the last chance we will be able to display any messages on\r
@@ -1913,18 +2151,19 @@ CoreTerminateMemoryMap (
     //\r
 \r
     for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
-      Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
+      Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\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 & (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
+          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) & (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
+\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
@@ -1948,12 +2187,3 @@ Done:
 \r
   return Status;\r
 }\r
-\r
-\r
-\r
-\r
-\r
-\r
-\r
-\r
-\r