]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Gcd/Gcd.c
MdeModulePkg/Gcd: Suppress incorrect compiler/analyzer warnings
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Gcd / Gcd.c
index f9ad44609e5397f34b9964e9db26a8ee2557ec5d..e17e98230b79beb063c5a00cbe38c251e86e9fc3 100644 (file)
@@ -3,7 +3,7 @@
   The GCD services are used to manage the memory and I/O regions that\r
   are accessible to the CPU that is executing the DXE core.\r
 \r
-Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -16,6 +16,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include "DxeMain.h"\r
 #include "Gcd.h"\r
+#include "Mem/HeapGuard.h"\r
 \r
 #define MINIMUM_INITIAL_MEMORY_SIZE 0x10000\r
 \r
@@ -25,9 +26,11 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
                                        EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED      | \\r
                                        EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED     | \\r
                                        EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED | \\r
+                                       EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED | \\r
                                        EFI_RESOURCE_ATTRIBUTE_16_BIT_IO           | \\r
                                        EFI_RESOURCE_ATTRIBUTE_32_BIT_IO           | \\r
-                                       EFI_RESOURCE_ATTRIBUTE_64_BIT_IO           )\r
+                                       EFI_RESOURCE_ATTRIBUTE_64_BIT_IO           | \\r
+                                       EFI_RESOURCE_ATTRIBUTE_PERSISTENT          )\r
 \r
 #define TESTED_MEMORY_ATTRIBUTES      (EFI_RESOURCE_ATTRIBUTE_PRESENT     | \\r
                                        EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \\r
@@ -38,7 +41,12 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #define PRESENT_MEMORY_ATTRIBUTES     (EFI_RESOURCE_ATTRIBUTE_PRESENT)\r
 \r
-#define INVALID_CPU_ARCH_ATTRIBUTES   0xffffffff\r
+#define EXCLUSIVE_MEMORY_ATTRIBUTES   (EFI_MEMORY_UC | EFI_MEMORY_WC | \\r
+                                       EFI_MEMORY_WT | EFI_MEMORY_WB | \\r
+                                       EFI_MEMORY_WP | EFI_MEMORY_UCE)\r
+\r
+#define NONEXCLUSIVE_MEMORY_ATTRIBUTES (EFI_MEMORY_XP | EFI_MEMORY_RP | \\r
+                                        EFI_MEMORY_RO)\r
 \r
 //\r
 // Module Variables\r
@@ -81,20 +89,166 @@ EFI_GCD_MAP_ENTRY mGcdIoSpaceMapEntryTemplate = {
 };\r
 \r
 GCD_ATTRIBUTE_CONVERSION_ENTRY mAttributeConversionTable[] = {\r
-  { EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE,             EFI_MEMORY_UC,          TRUE  },\r
-  { EFI_RESOURCE_ATTRIBUTE_UNCACHED_EXPORTED,       EFI_MEMORY_UCE,         TRUE  },\r
-  { EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE,       EFI_MEMORY_WC,          TRUE  },\r
-  { EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE, EFI_MEMORY_WT,          TRUE  },\r
-  { EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE,    EFI_MEMORY_WB,          TRUE  },\r
-  { EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED,          EFI_MEMORY_RP,          TRUE  },\r
-  { EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED,         EFI_MEMORY_WP,          TRUE  },\r
-  { EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED,     EFI_MEMORY_XP,          TRUE  },\r
-  { EFI_RESOURCE_ATTRIBUTE_PRESENT,                 EFI_MEMORY_PRESENT,     FALSE },\r
-  { EFI_RESOURCE_ATTRIBUTE_INITIALIZED,             EFI_MEMORY_INITIALIZED, FALSE },\r
-  { EFI_RESOURCE_ATTRIBUTE_TESTED,                  EFI_MEMORY_TESTED,      FALSE },\r
-  { 0,                                              0,                      FALSE }\r
+  { EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE,             EFI_MEMORY_UC,              TRUE  },\r
+  { EFI_RESOURCE_ATTRIBUTE_UNCACHED_EXPORTED,       EFI_MEMORY_UCE,             TRUE  },\r
+  { EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE,       EFI_MEMORY_WC,              TRUE  },\r
+  { EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE, EFI_MEMORY_WT,              TRUE  },\r
+  { EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE,    EFI_MEMORY_WB,              TRUE  },\r
+  { EFI_RESOURCE_ATTRIBUTE_READ_PROTECTABLE,        EFI_MEMORY_RP,              TRUE  },\r
+  { EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTABLE,       EFI_MEMORY_WP,              TRUE  },\r
+  { EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTABLE,   EFI_MEMORY_XP,              TRUE  },\r
+  { EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTABLE,   EFI_MEMORY_RO,              TRUE  },\r
+  { EFI_RESOURCE_ATTRIBUTE_PRESENT,                 EFI_MEMORY_PRESENT,         FALSE },\r
+  { EFI_RESOURCE_ATTRIBUTE_INITIALIZED,             EFI_MEMORY_INITIALIZED,     FALSE },\r
+  { EFI_RESOURCE_ATTRIBUTE_TESTED,                  EFI_MEMORY_TESTED,          FALSE },\r
+  { EFI_RESOURCE_ATTRIBUTE_PERSISTABLE,             EFI_MEMORY_NV,              TRUE  },\r
+  { EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE,           EFI_MEMORY_MORE_RELIABLE,   TRUE  },\r
+  { 0,                                              0,                          FALSE }\r
+};\r
+\r
+///\r
+/// Lookup table used to print GCD Memory Space Map\r
+///\r
+GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mGcdMemoryTypeNames[] = {\r
+  "NonExist ",  // EfiGcdMemoryTypeNonExistent\r
+  "Reserved ",  // EfiGcdMemoryTypeReserved\r
+  "SystemMem",  // EfiGcdMemoryTypeSystemMemory\r
+  "MMIO     ",  // EfiGcdMemoryTypeMemoryMappedIo\r
+  "PersisMem",  // EfiGcdMemoryTypePersistent\r
+  "MoreRelia",  // EfiGcdMemoryTypeMoreReliable\r
+  "Unknown  "   // EfiGcdMemoryTypeMaximum\r
+};\r
+\r
+///\r
+/// Lookup table used to print GCD I/O Space Map\r
+///\r
+GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mGcdIoTypeNames[] = {\r
+  "NonExist",  // EfiGcdIoTypeNonExistent\r
+  "Reserved",  // EfiGcdIoTypeReserved\r
+  "I/O     ",  // EfiGcdIoTypeIo\r
+  "Unknown "   // EfiGcdIoTypeMaximum \r
+};\r
+\r
+///\r
+/// Lookup table used to print GCD Allocation Types\r
+///\r
+GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mGcdAllocationTypeNames[] = {\r
+  "AnySearchBottomUp        ",  // EfiGcdAllocateAnySearchBottomUp\r
+  "MaxAddressSearchBottomUp ",  // EfiGcdAllocateMaxAddressSearchBottomUp\r
+  "AtAddress                ",  // EfiGcdAllocateAddress\r
+  "AnySearchTopDown         ",  // EfiGcdAllocateAnySearchTopDown\r
+  "MaxAddressSearchTopDown  ",  // EfiGcdAllocateMaxAddressSearchTopDown\r
+  "Unknown                  "   // EfiGcdMaxAllocateType\r
 };\r
 \r
+/**\r
+  Dump the entire contents if the GCD Memory Space Map using DEBUG() macros when\r
+  PcdDebugPrintErrorLevel has the DEBUG_GCD bit set.\r
+\r
+  @param  InitialMap  TRUE if the initial GCD Memory Map is being dumped.  Otherwise, FALSE.\r
+  \r
+**/\r
+VOID\r
+EFIAPI\r
+CoreDumpGcdMemorySpaceMap (\r
+  BOOLEAN  InitialMap\r
+  )\r
+{\r
+  DEBUG_CODE (\r
+    EFI_STATUS                       Status;\r
+    UINTN                            NumberOfDescriptors;\r
+    EFI_GCD_MEMORY_SPACE_DESCRIPTOR  *MemorySpaceMap;\r
+    UINTN                            Index;\r
+   \r
+    Status = CoreGetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);\r
+    ASSERT (Status == EFI_SUCCESS && MemorySpaceMap != NULL);\r
+\r
+    if (InitialMap) {\r
+      DEBUG ((DEBUG_GCD, "GCD:Initial GCD Memory Space Map\n"));\r
+    }\r
+    DEBUG ((DEBUG_GCD, "GCDMemType Range                             Capabilities     Attributes      \n"));\r
+    DEBUG ((DEBUG_GCD, "========== ================================= ================ ================\n"));\r
+    for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
+      DEBUG ((DEBUG_GCD, "%a  %016lx-%016lx %016lx %016lx%c\n", \r
+        mGcdMemoryTypeNames[MIN (MemorySpaceMap[Index].GcdMemoryType, EfiGcdMemoryTypeMaximum)],\r
+        MemorySpaceMap[Index].BaseAddress, \r
+        MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length - 1,\r
+        MemorySpaceMap[Index].Capabilities, \r
+        MemorySpaceMap[Index].Attributes,\r
+        MemorySpaceMap[Index].ImageHandle == NULL ? ' ' : '*'\r
+        ));\r
+    }\r
+    DEBUG ((DEBUG_GCD, "\n"));\r
+    FreePool (MemorySpaceMap);\r
+  );\r
+}\r
+\r
+/**\r
+  Dump the entire contents if the GCD I/O Space Map using DEBUG() macros when \r
+  PcdDebugPrintErrorLevel has the DEBUG_GCD bit set.\r
+\r
+  @param  InitialMap  TRUE if the initial GCD I/O Map is being dumped.  Otherwise, FALSE.\r
+  \r
+**/\r
+VOID\r
+EFIAPI\r
+CoreDumpGcdIoSpaceMap (\r
+  BOOLEAN  InitialMap\r
+  )\r
+{\r
+  DEBUG_CODE (\r
+    EFI_STATUS                   Status;\r
+    UINTN                        NumberOfDescriptors;\r
+    EFI_GCD_IO_SPACE_DESCRIPTOR  *IoSpaceMap;\r
+    UINTN                        Index;\r
+    \r
+    Status = CoreGetIoSpaceMap (&NumberOfDescriptors, &IoSpaceMap);\r
+    ASSERT (Status == EFI_SUCCESS && IoSpaceMap != NULL);\r
+    \r
+    if (InitialMap) {\r
+      DEBUG ((DEBUG_GCD, "GCD:Initial GCD I/O Space Map\n"));\r
+    }  \r
+    \r
+    DEBUG ((DEBUG_GCD, "GCDIoType  Range                            \n"));\r
+    DEBUG ((DEBUG_GCD, "========== =================================\n"));\r
+    for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
+      DEBUG ((DEBUG_GCD, "%a   %016lx-%016lx%c\n", \r
+        mGcdIoTypeNames[MIN (IoSpaceMap[Index].GcdIoType, EfiGcdIoTypeMaximum)],\r
+        IoSpaceMap[Index].BaseAddress, \r
+        IoSpaceMap[Index].BaseAddress + IoSpaceMap[Index].Length - 1,\r
+        IoSpaceMap[Index].ImageHandle == NULL ? ' ' : '*'\r
+        ));\r
+    }\r
+    DEBUG ((DEBUG_GCD, "\n"));\r
+    FreePool (IoSpaceMap);\r
+  );\r
+}\r
+  \r
+/**\r
+  Validate resource descriptor HOB's attributes.\r
+\r
+  If Attributes includes some memory resource's settings, it should include \r
+  the corresponding capabilites also.\r
+\r
+  @param  Attributes  Resource descriptor HOB attributes.\r
+\r
+**/\r
+VOID\r
+CoreValidateResourceDescriptorHobAttributes (\r
+  IN UINT64  Attributes\r
+  )\r
+{\r
+  ASSERT (((Attributes & EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED) == 0) ||\r
+          ((Attributes & EFI_RESOURCE_ATTRIBUTE_READ_PROTECTABLE) != 0));\r
+  ASSERT (((Attributes & EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED) == 0) ||\r
+          ((Attributes & EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTABLE) != 0));\r
+  ASSERT (((Attributes & EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED) == 0) ||\r
+          ((Attributes & EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTABLE) != 0));\r
+  ASSERT (((Attributes & EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED) == 0) ||\r
+          ((Attributes & EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTABLE) != 0));\r
+  ASSERT (((Attributes & EFI_RESOURCE_ATTRIBUTE_PERSISTENT) == 0) ||\r
+          ((Attributes & EFI_RESOURCE_ATTRIBUTE_PERSISTABLE) != 0));\r
+}\r
 \r
 /**\r
   Acquire memory lock on mGcdMemorySpaceLock.\r
@@ -236,12 +390,21 @@ CoreAllocateGcdMapEntry (
   IN OUT EFI_GCD_MAP_ENTRY  **BottomEntry\r
   )\r
 {\r
+  //\r
+  // Set to mOnGuarding to TRUE before memory allocation. This will make sure\r
+  // that the entry memory is not "guarded" by HeapGuard. Otherwise it might\r
+  // cause problem when it's freed (if HeapGuard is enabled).\r
+  //\r
+  mOnGuarding = TRUE;\r
   *TopEntry = AllocateZeroPool (sizeof (EFI_GCD_MAP_ENTRY));\r
+  mOnGuarding = FALSE;\r
   if (*TopEntry == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
+  mOnGuarding = TRUE;\r
   *BottomEntry = AllocateZeroPool (sizeof (EFI_GCD_MAP_ENTRY));\r
+  mOnGuarding = FALSE;\r
   if (*BottomEntry == NULL) {\r
     CoreFreePool (*TopEntry);\r
     return EFI_OUT_OF_RESOURCES;\r
@@ -276,10 +439,10 @@ CoreInsertGcdMapEntry (
   )\r
 {\r
   ASSERT (Length != 0);\r
-  ASSERT (TopEntry->Signature == 0);\r
-  ASSERT (BottomEntry->Signature == 0);\r
 \r
   if (BaseAddress > Entry->BaseAddress) {\r
+    ASSERT (BottomEntry->Signature == 0);\r
+\r
     CopyMem (BottomEntry, Entry, sizeof (EFI_GCD_MAP_ENTRY));\r
     Entry->BaseAddress      = BaseAddress;\r
     BottomEntry->EndAddress = BaseAddress - 1;\r
@@ -287,6 +450,8 @@ CoreInsertGcdMapEntry (
   }\r
 \r
   if ((BaseAddress + Length - 1) < Entry->EndAddress) {\r
+    ASSERT (TopEntry->Signature == 0);\r
+\r
     CopyMem (TopEntry, Entry, sizeof (EFI_GCD_MAP_ENTRY));\r
     TopEntry->BaseAddress = BaseAddress + Length;\r
     Entry->EndAddress     = BaseAddress + Length - 1;\r
@@ -504,28 +669,25 @@ ConverToCpuArchAttributes (
   UINT64 Attributes\r
   )\r
 {\r
-  if ( (Attributes & EFI_MEMORY_UC) == EFI_MEMORY_UC) {\r
-    return EFI_MEMORY_UC;\r
-  }\r
+  UINT64      CpuArchAttributes;\r
 \r
-  if ( (Attributes & EFI_MEMORY_WC ) == EFI_MEMORY_WC) {\r
-    return EFI_MEMORY_WC;\r
-  }\r
-\r
-  if ( (Attributes & EFI_MEMORY_WT ) == EFI_MEMORY_WT) {\r
-    return EFI_MEMORY_WT;\r
-  }\r
-\r
-  if ( (Attributes & EFI_MEMORY_WB) == EFI_MEMORY_WB) {\r
-    return EFI_MEMORY_WB;\r
-  }\r
-\r
-  if ( (Attributes & EFI_MEMORY_WP) == EFI_MEMORY_WP) {\r
-    return EFI_MEMORY_WP;\r
-  }\r
-\r
-  return INVALID_CPU_ARCH_ATTRIBUTES;\r
+  CpuArchAttributes = Attributes & NONEXCLUSIVE_MEMORY_ATTRIBUTES;\r
 \r
+  if ( (Attributes & EFI_MEMORY_UC) == EFI_MEMORY_UC) {\r
+    CpuArchAttributes |= EFI_MEMORY_UC;\r
+  } else if ( (Attributes & EFI_MEMORY_WC ) == EFI_MEMORY_WC) {\r
+    CpuArchAttributes |= EFI_MEMORY_WC;\r
+  } else if ( (Attributes & EFI_MEMORY_WT ) == EFI_MEMORY_WT) {\r
+    CpuArchAttributes |= EFI_MEMORY_WT;\r
+  } else if ( (Attributes & EFI_MEMORY_WB) == EFI_MEMORY_WB) {\r
+    CpuArchAttributes |= EFI_MEMORY_WB;\r
+  } else if ( (Attributes & EFI_MEMORY_UCE) == EFI_MEMORY_UCE) {\r
+    CpuArchAttributes |= EFI_MEMORY_UCE;\r
+  } else if ( (Attributes & EFI_MEMORY_WP) == EFI_MEMORY_WP) {\r
+    CpuArchAttributes |= EFI_MEMORY_WP;\r
+  }\r
+\r
+  return CpuArchAttributes;\r
 }\r
 \r
 \r
@@ -550,7 +712,8 @@ ConverToCpuArchAttributes (
   @retval EFI_NOT_FOUND          Free a non-using space or remove a non-exist\r
                                  space, and so on.\r
   @retval EFI_OUT_OF_RESOURCES   No buffer could be allocated.\r
-\r
+  @retval EFI_NOT_AVAILABLE_YET  The attributes cannot be set because CPU architectural protocol\r
+                                 is not available yet.\r
 **/\r
 EFI_STATUS\r
 CoreConvertSpace (\r
@@ -571,11 +734,10 @@ CoreConvertSpace (
   EFI_GCD_MAP_ENTRY  *BottomEntry;\r
   LIST_ENTRY         *StartLink;\r
   LIST_ENTRY         *EndLink;\r
-\r
-  EFI_CPU_ARCH_PROTOCOL           *CpuArch;\r
-  UINT64                          CpuArchAttributes;\r
+  UINT64             CpuArchAttributes;\r
 \r
   if (Length == 0) {\r
+    DEBUG ((DEBUG_GCD, "  Status = %r\n", EFI_INVALID_PARAMETER));\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -659,7 +821,7 @@ CoreConvertSpace (
       }\r
       break;\r
     //\r
-    // Set attribute operations\r
+    // Set attributes operation\r
     //\r
     case GCD_SET_ATTRIBUTES_MEMORY_OPERATION:\r
       if ((Attributes & EFI_MEMORY_RUNTIME) != 0) {\r
@@ -673,6 +835,23 @@ CoreConvertSpace (
         goto Done;\r
       }\r
       break;\r
+    //\r
+    // Set capabilities operation\r
+    //\r
+    case GCD_SET_CAPABILITIES_MEMORY_OPERATION:\r
+      if ((BaseAddress & EFI_PAGE_MASK) != 0 || (Length & EFI_PAGE_MASK) != 0) {\r
+        Status = EFI_INVALID_PARAMETER;\r
+\r
+        goto Done;\r
+      }\r
+      //\r
+      // Current attributes must still be supported with new capabilities\r
+      //\r
+      if ((Capabilities & Entry->Attributes) != Entry->Attributes) {\r
+        Status = EFI_UNSUPPORTED;\r
+        goto Done;\r
+      }\r
+      break;\r
     }\r
     Link = Link->ForwardLink;\r
   }\r
@@ -687,29 +866,46 @@ CoreConvertSpace (
   }\r
   ASSERT (TopEntry != NULL && BottomEntry != NULL);\r
 \r
+  //\r
+  // Initialize CpuArchAttributes to suppress incorrect compiler/analyzer warnings.\r
+  //\r
+  CpuArchAttributes = 0;\r
   if (Operation == GCD_SET_ATTRIBUTES_MEMORY_OPERATION) {\r
     //\r
     // Call CPU Arch Protocol to attempt to set attributes on the range\r
     //\r
     CpuArchAttributes = ConverToCpuArchAttributes (Attributes);\r
-    if ( CpuArchAttributes != INVALID_CPU_ARCH_ATTRIBUTES ) {\r
-      Status = CoreLocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&CpuArch);\r
-      if (EFI_ERROR (Status) || CpuArch == NULL) {\r
-        Status = EFI_ACCESS_DENIED;\r
-        goto Done;\r
+    //\r
+    // CPU arch attributes include page attributes and cache attributes. \r
+    // Only page attributes supports to be cleared, but not cache attributes.\r
+    // Caller is expected to use GetMemorySpaceDescriptor() to get the current\r
+    // attributes, AND/OR attributes, and then calls SetMemorySpaceAttributes()\r
+    // to set the new attributes.\r
+    // So 0 CPU arch attributes should not happen as memory should always have\r
+    // a cache attribute (no matter UC or WB, etc). \r
+    //\r
+    // Here, 0 CPU arch attributes will be filtered to be compatible with the\r
+    // case that caller just calls SetMemorySpaceAttributes() with none CPU\r
+    // arch attributes (for example, RUNTIME) as the purpose of the case is not\r
+    // to clear CPU arch attributes.\r
+    //\r
+    if (CpuArchAttributes != 0) {\r
+      if (gCpu == NULL) {\r
+        Status = EFI_NOT_AVAILABLE_YET;\r
+      } else {\r
+        Status = gCpu->SetMemoryAttributes (\r
+                         gCpu,\r
+                         BaseAddress,\r
+                         Length,\r
+                         CpuArchAttributes\r
+                         );\r
       }\r
-\r
-      Status = CpuArch->SetMemoryAttributes (\r
-                          CpuArch,\r
-                          BaseAddress,\r
-                          Length,\r
-                          CpuArchAttributes\r
-                          );\r
       if (EFI_ERROR (Status)) {\r
+        CoreFreePool (TopEntry);\r
+        CoreFreePool (BottomEntry);\r
         goto Done;\r
       }\r
     }\r
-\r
   }\r
 \r
   //\r
@@ -753,11 +949,24 @@ CoreConvertSpace (
       Entry->GcdIoType = EfiGcdIoTypeNonExistent;\r
       break;\r
     //\r
-    // Set attribute operations\r
+    // Set attributes operation\r
     //\r
     case GCD_SET_ATTRIBUTES_MEMORY_OPERATION:\r
+      if (CpuArchAttributes == 0) {\r
+        //\r
+        // Keep original CPU arch attributes when caller just calls\r
+        // SetMemorySpaceAttributes() with none CPU arch attributes (for example, RUNTIME).\r
+        //\r
+        Attributes |= (Entry->Attributes & (EXCLUSIVE_MEMORY_ATTRIBUTES | NONEXCLUSIVE_MEMORY_ATTRIBUTES));\r
+      }\r
       Entry->Attributes = Attributes;\r
       break;\r
+    //\r
+    // Set capabilities operation\r
+    //\r
+    case GCD_SET_CAPABILITIES_MEMORY_OPERATION:\r
+      Entry->Capabilities = Capabilities;\r
+      break;\r
     }\r
     Link = Link->ForwardLink;\r
   }\r
@@ -768,11 +977,15 @@ CoreConvertSpace (
   Status = CoreCleanupGcdMapEntry (TopEntry, BottomEntry, StartLink, EndLink, Map);\r
 \r
 Done:\r
+  DEBUG ((DEBUG_GCD, "  Status = %r\n", Status));\r
+\r
   if ((Operation & GCD_MEMORY_SPACE_OPERATION) != 0) {\r
     CoreReleaseGcdMemoryLock ();\r
+    CoreDumpGcdMemorySpaceMap (FALSE);\r
   }\r
   if ((Operation & GCD_IO_SPACE_OPERATION) != 0) {\r
     CoreReleaseGcdIoLock ();\r
+    CoreDumpGcdIoSpaceMap (FALSE);\r
   }\r
 \r
   return Status;\r
@@ -870,25 +1083,32 @@ CoreAllocateSpace (
   //\r
   // Make sure parameters are valid\r
   //\r
-  if (GcdAllocateType < 0 || GcdAllocateType >= EfiGcdMaxAllocateType) {\r
+  if ((UINT32)GcdAllocateType >= EfiGcdMaxAllocateType) {\r
+    DEBUG ((DEBUG_GCD, "  Status = %r\n", EFI_INVALID_PARAMETER));\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  if (GcdMemoryType < 0 || GcdMemoryType >= EfiGcdMemoryTypeMaximum) {\r
+  if ((UINT32)GcdMemoryType >= EfiGcdMemoryTypeMaximum) {\r
+    DEBUG ((DEBUG_GCD, "  Status = %r\n", EFI_INVALID_PARAMETER));\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  if (GcdIoType < 0 || GcdIoType >= EfiGcdIoTypeMaximum) {\r
+  if ((UINT32)GcdIoType >= EfiGcdIoTypeMaximum) {\r
+    DEBUG ((DEBUG_GCD, "  Status = %r\n", EFI_INVALID_PARAMETER));\r
     return EFI_INVALID_PARAMETER;\r
   }\r
   if (BaseAddress == NULL) {\r
+    DEBUG ((DEBUG_GCD, "  Status = %r\n", EFI_INVALID_PARAMETER));\r
     return EFI_INVALID_PARAMETER;\r
   }\r
   if (ImageHandle == NULL) {\r
+    DEBUG ((DEBUG_GCD, "  Status = %r\n", EFI_INVALID_PARAMETER));\r
     return EFI_INVALID_PARAMETER;\r
   }\r
   if (Alignment >= 64) {\r
+    DEBUG ((DEBUG_GCD, "  Status = %r\n", EFI_NOT_FOUND));\r
     return EFI_NOT_FOUND;\r
   }\r
   if (Length == 0) {\r
+    DEBUG ((DEBUG_GCD, "  Status = %r\n", EFI_INVALID_PARAMETER));\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -1068,11 +1288,19 @@ CoreAllocateSpace (
   Status = CoreCleanupGcdMapEntry (TopEntry, BottomEntry, StartLink, EndLink, Map);\r
 \r
 Done:\r
+  DEBUG ((DEBUG_GCD, "  Status = %r", Status));\r
+  if (!EFI_ERROR (Status)) {\r
+    DEBUG ((DEBUG_GCD, "  (BaseAddress = %016lx)", *BaseAddress));\r
+  }\r
+  DEBUG ((DEBUG_GCD, "\n"));\r
+  \r
   if ((Operation & GCD_MEMORY_SPACE_OPERATION) != 0) {\r
     CoreReleaseGcdMemoryLock ();\r
+    CoreDumpGcdMemorySpaceMap (FALSE);\r
   }\r
   if ((Operation & GCD_IO_SPACE_OPERATION) !=0) {\r
     CoreReleaseGcdIoLock ();\r
+    CoreDumpGcdIoSpaceMap (FALSE);\r
   }\r
 \r
   return Status;\r
@@ -1099,6 +1327,10 @@ CoreInternalAddMemorySpace (
   IN UINT64                Capabilities\r
   )\r
 {\r
+  DEBUG ((DEBUG_GCD, "GCD:AddMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));\r
+  DEBUG ((DEBUG_GCD, "  GcdMemoryType   = %a\n", mGcdMemoryTypeNames[MIN (GcdMemoryType, EfiGcdMemoryTypeMaximum)]));\r
+  DEBUG ((DEBUG_GCD, "  Capabilities    = %016lx\n", Capabilities));\r
+\r
   //\r
   // Make sure parameters are valid\r
   //\r
@@ -1142,6 +1374,17 @@ CoreAllocateMemorySpace (
   IN     EFI_HANDLE             DeviceHandle OPTIONAL\r
   )\r
 {\r
+  if (BaseAddress != NULL) {\r
+    DEBUG ((DEBUG_GCD, "GCD:AllocateMemorySpace(Base=%016lx,Length=%016lx)\n", *BaseAddress, Length));\r
+  } else {\r
+    DEBUG ((DEBUG_GCD, "GCD:AllocateMemorySpace(Base=<NULL>,Length=%016lx)\n", Length));\r
+  }\r
+  DEBUG ((DEBUG_GCD, "  GcdAllocateType = %a\n", mGcdAllocationTypeNames[MIN (GcdAllocateType, EfiGcdMaxAllocateType)]));\r
+  DEBUG ((DEBUG_GCD, "  GcdMemoryType   = %a\n", mGcdMemoryTypeNames[MIN (GcdMemoryType, EfiGcdMemoryTypeMaximum)]));\r
+  DEBUG ((DEBUG_GCD, "  Alignment       = %016lx\n", LShiftU64 (1, Alignment)));\r
+  DEBUG ((DEBUG_GCD, "  ImageHandle     = %p\n", ImageHandle));\r
+  DEBUG ((DEBUG_GCD, "  DeviceHandle    = %p\n", DeviceHandle));\r
+  \r
   return CoreAllocateSpace (\r
            GCD_ALLOCATE_MEMORY_OPERATION,\r
            GcdAllocateType,\r
@@ -1183,9 +1426,9 @@ CoreAddMemorySpace (
 \r
   Status = CoreInternalAddMemorySpace (GcdMemoryType, BaseAddress, Length, Capabilities);\r
 \r
-  if (!EFI_ERROR (Status) && GcdMemoryType == EfiGcdMemoryTypeSystemMemory) {\r
+  if (!EFI_ERROR (Status) && ((GcdMemoryType == EfiGcdMemoryTypeSystemMemory) || (GcdMemoryType == EfiGcdMemoryTypeMoreReliable))) {\r
 \r
-    PageBaseAddress = PageAlignLength (BaseAddress);\r
+    PageBaseAddress = PageAlignAddress (BaseAddress);\r
     PageLength      = PageAlignLength (BaseAddress + Length - PageBaseAddress);\r
 \r
     Status = CoreAllocateMemorySpace (\r
@@ -1249,6 +1492,8 @@ CoreFreeMemorySpace (
   IN UINT64                Length\r
   )\r
 {\r
+  DEBUG ((DEBUG_GCD, "GCD:FreeMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));\r
+\r
   return CoreConvertSpace (GCD_FREE_MEMORY_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);\r
 }\r
 \r
@@ -1270,6 +1515,8 @@ CoreRemoveMemorySpace (
   IN UINT64                Length\r
   )\r
 {\r
+  DEBUG ((DEBUG_GCD, "GCD:RemoveMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));\r
+  \r
   return CoreConvertSpace (GCD_REMOVE_MEMORY_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);\r
 }\r
 \r
@@ -1357,8 +1604,18 @@ CoreGetMemorySpaceDescriptor (
   @param  Length                 Specified length\r
   @param  Attributes             Specified attributes\r
 \r
-  @retval EFI_SUCCESS            Successfully set attribute of a segment of\r
-                                 memory space.\r
+  @retval EFI_SUCCESS           The attributes were set for the memory region.\r
+  @retval EFI_INVALID_PARAMETER Length is zero. \r
+  @retval EFI_UNSUPPORTED       The processor does not support one or more bytes of the memory\r
+                                resource range specified by BaseAddress and Length.\r
+  @retval EFI_UNSUPPORTED       The bit mask of attributes is not support for the memory resource\r
+                                range specified by BaseAddress and Length.\r
+  @retval EFI_ACCESS_DEFINED    The attributes for the memory resource range specified by\r
+                                BaseAddress and Length cannot be modified.\r
+  @retval EFI_OUT_OF_RESOURCES  There are not enough system resources to modify the attributes of\r
+                                the memory resource range.\r
+  @retval EFI_NOT_AVAILABLE_YET The attributes cannot be set because CPU architectural protocol is\r
+                                not available yet.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1369,10 +1626,52 @@ CoreSetMemorySpaceAttributes (
   IN UINT64                Attributes\r
   )\r
 {\r
+  DEBUG ((DEBUG_GCD, "GCD:SetMemorySpaceAttributes(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));\r
+  DEBUG ((DEBUG_GCD, "  Attributes  = %016lx\n", Attributes));\r
+\r
   return CoreConvertSpace (GCD_SET_ATTRIBUTES_MEMORY_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, Attributes);\r
 }\r
 \r
 \r
+/**\r
+  Modifies the capabilities for a memory region in the global coherency domain of the\r
+  processor.\r
+\r
+  @param  BaseAddress      The physical address that is the start address of a memory region.\r
+  @param  Length           The size in bytes of the memory region.\r
+  @param  Capabilities     The bit mask of capabilities that the memory region supports.\r
+\r
+  @retval EFI_SUCCESS           The capabilities were set for the memory region.\r
+  @retval EFI_INVALID_PARAMETER Length is zero.\r
+  @retval EFI_UNSUPPORTED       The capabilities specified by Capabilities do not include the\r
+                                memory region attributes currently in use.\r
+  @retval EFI_ACCESS_DENIED     The capabilities for the memory resource range specified by\r
+                                BaseAddress and Length cannot be modified.\r
+  @retval EFI_OUT_OF_RESOURCES  There are not enough system resources to modify the capabilities\r
+                                of the memory resource range.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CoreSetMemorySpaceCapabilities (\r
+  IN EFI_PHYSICAL_ADDRESS  BaseAddress,\r
+  IN UINT64                Length,\r
+  IN UINT64                Capabilities\r
+  )\r
+{\r
+  EFI_STATUS    Status;\r
+\r
+  DEBUG ((DEBUG_GCD, "GCD:CoreSetMemorySpaceCapabilities(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));\r
+  DEBUG ((DEBUG_GCD, "  Capabilities  = %016lx\n", Capabilities));\r
+\r
+  Status = CoreConvertSpace (GCD_SET_CAPABILITIES_MEMORY_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, Capabilities, 0);\r
+  if (!EFI_ERROR(Status)) {\r
+    CoreUpdateMemoryAttributes(BaseAddress, RShiftU64(Length, EFI_PAGE_SHIFT), Capabilities & (~EFI_MEMORY_RUNTIME));\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+\r
 /**\r
   Returns a map of the memory resources in the global coherency domain of the\r
   processor.\r
@@ -1461,6 +1760,9 @@ CoreAddIoSpace (
   IN UINT64                Length\r
   )\r
 {\r
+  DEBUG ((DEBUG_GCD, "GCD:AddIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));\r
+  DEBUG ((DEBUG_GCD, "  GcdIoType    = %a\n", mGcdIoTypeNames[MIN (GcdIoType, EfiGcdIoTypeMaximum)]));\r
+  \r
   //\r
   // Make sure parameters are valid\r
   //\r
@@ -1500,6 +1802,17 @@ CoreAllocateIoSpace (
   IN     EFI_HANDLE             DeviceHandle OPTIONAL\r
   )\r
 {\r
+  if (BaseAddress != NULL) {\r
+    DEBUG ((DEBUG_GCD, "GCD:AllocateIoSpace(Base=%016lx,Length=%016lx)\n", *BaseAddress, Length));\r
+  } else {\r
+    DEBUG ((DEBUG_GCD, "GCD:AllocateIoSpace(Base=<NULL>,Length=%016lx)\n", Length));\r
+  }\r
+  DEBUG ((DEBUG_GCD, "  GcdAllocateType = %a\n", mGcdAllocationTypeNames[MIN (GcdAllocateType, EfiGcdMaxAllocateType)]));\r
+  DEBUG ((DEBUG_GCD, "  GcdIoType       = %a\n", mGcdIoTypeNames[MIN (GcdIoType, EfiGcdIoTypeMaximum)]));\r
+  DEBUG ((DEBUG_GCD, "  Alignment       = %016lx\n", LShiftU64 (1, Alignment)));\r
+  DEBUG ((DEBUG_GCD, "  ImageHandle     = %p\n", ImageHandle));\r
+  DEBUG ((DEBUG_GCD, "  DeviceHandle    = %p\n", DeviceHandle));\r
+  \r
   return CoreAllocateSpace (\r
            GCD_ALLOCATE_IO_OPERATION,\r
            GcdAllocateType,\r
@@ -1531,6 +1844,8 @@ CoreFreeIoSpace (
   IN UINT64                Length\r
   )\r
 {\r
+  DEBUG ((DEBUG_GCD, "GCD:FreeIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));\r
+\r
   return CoreConvertSpace (GCD_FREE_IO_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);\r
 }\r
 \r
@@ -1552,6 +1867,8 @@ CoreRemoveIoSpace (
   IN UINT64                Length\r
   )\r
 {\r
+  DEBUG ((DEBUG_GCD, "GCD:RemoveIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));\r
+  \r
   return CoreConvertSpace (GCD_REMOVE_IO_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);\r
 }\r
 \r
@@ -1721,7 +2038,7 @@ CoreConvertResourceDescriptorHobAttributesToCapabilities (
   // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask\r
   //\r
   for (Capabilities = 0, Conversion = mAttributeConversionTable; Conversion->Attribute != 0; Conversion++) {\r
-    if (Conversion->Memory || (GcdMemoryType != EfiGcdMemoryTypeSystemMemory)) {\r
+    if (Conversion->Memory || ((GcdMemoryType != EfiGcdMemoryTypeSystemMemory) && (GcdMemoryType != EfiGcdMemoryTypeMoreReliable))) {\r
       if (Attributes & Conversion->Attribute) {\r
         Capabilities |= Conversion->Capability;\r
       }\r
@@ -1731,6 +2048,30 @@ CoreConvertResourceDescriptorHobAttributesToCapabilities (
   return Capabilities;\r
 }\r
 \r
+/**\r
+  Calculate total memory bin size neeeded.\r
+\r
+  @return The total memory bin size neeeded.\r
+\r
+**/\r
+UINT64\r
+CalculateTotalMemoryBinSizeNeeded (\r
+  VOID\r
+  )\r
+{\r
+  UINTN     Index;\r
+  UINT64    TotalSize;\r
+\r
+  //\r
+  // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array\r
+  //\r
+  TotalSize = 0;\r
+  for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
+    TotalSize += LShiftU64 (gMemoryTypeInformation[Index].NumberOfPages, EFI_PAGE_SHIFT);\r
+  }\r
+\r
+  return TotalSize;\r
+}\r
 \r
 /**\r
   External function. Initializes memory services based on the memory\r
@@ -1765,14 +2106,12 @@ CoreInitializeMemoryServices (
   UINT64                             Length;\r
   UINT64                             Attributes;\r
   UINT64                             Capabilities;\r
-  EFI_PHYSICAL_ADDRESS               MaxMemoryBaseAddress;\r
-  UINT64                             MaxMemoryLength;\r
-  UINT64                             MaxMemoryAttributes;\r
-  EFI_PHYSICAL_ADDRESS               MaxAddress;\r
+  EFI_PHYSICAL_ADDRESS               TestedMemoryBaseAddress;\r
+  UINT64                             TestedMemoryLength;\r
   EFI_PHYSICAL_ADDRESS               HighAddress;\r
-  EFI_HOB_RESOURCE_DESCRIPTOR        *MaxResourceHob;\r
   EFI_HOB_GUID_TYPE                  *GuidHob;\r
-  UINT32                              ReservedCodePageNumber;\r
+  UINT32                             ReservedCodePageNumber;\r
+  UINT64                             MinimalMemorySizeNeeded;\r
 \r
   //\r
   // Point at the first HOB.  This must be the PHIT HOB.\r
@@ -1790,14 +2129,10 @@ CoreInitializeMemoryServices (
   // Initialize Local Variables\r
   //\r
   PhitResourceHob       = NULL;\r
-  MaxResourceHob        = NULL;\r
   ResourceHob           = NULL;\r
   BaseAddress           = 0;\r
   Length                = 0;\r
   Attributes            = 0;\r
-  MaxMemoryBaseAddress  = 0;\r
-  MaxMemoryLength       = 0;\r
-  MaxMemoryAttributes   = 0;\r
 \r
   //\r
   // Cache the PHIT HOB for later use\r
@@ -1827,43 +2162,72 @@ CoreInitializeMemoryServices (
   }\r
 \r
   //\r
-  // Find the Resource Descriptor HOB that contains range FreeMemoryBaseAddress..FreeMemoryLength\r
+  // Include the total memory bin size needed to make sure memory bin could be allocated successfully.\r
+  //\r
+  MinimalMemorySizeNeeded = MINIMUM_INITIAL_MEMORY_SIZE + CalculateTotalMemoryBinSizeNeeded ();\r
+\r
+  //\r
+  // Find the Resource Descriptor HOB that contains PHIT range EfiFreeMemoryBottom..EfiFreeMemoryTop\r
   //\r
-  Length = 0;\r
   Found  = FALSE;\r
   for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
+    //\r
+    // Skip all HOBs except Resource Descriptor HOBs\r
+    //\r
+    if (GET_HOB_TYPE (Hob) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
+      continue;\r
+    }\r
 \r
-    if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
-\r
-      ResourceHob = Hob.ResourceDescriptor;\r
+    //\r
+    // Skip Resource Descriptor HOBs that do not describe tested system memory\r
+    //\r
+    ResourceHob = Hob.ResourceDescriptor;\r
+    if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY) {\r
+      continue;\r
+    }\r
+    if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) != TESTED_MEMORY_ATTRIBUTES) {\r
+      continue;\r
+    }\r
 \r
-      if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY                                       &&\r
-          (ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == TESTED_MEMORY_ATTRIBUTES    ) {\r
+    //\r
+    // Skip Resource Descriptor HOBs that do not contain the PHIT range EfiFreeMemoryBottom..EfiFreeMemoryTop\r
+    //\r
+    if (PhitHob->EfiFreeMemoryBottom < ResourceHob->PhysicalStart) {\r
+      continue;\r
+    }\r
+    if (PhitHob->EfiFreeMemoryTop > (ResourceHob->PhysicalStart + ResourceHob->ResourceLength)) {\r
+      continue;\r
+    }\r
 \r
-        if (PhitHob->EfiFreeMemoryBottom >= ResourceHob->PhysicalStart                         &&\r
-            PhitHob->EfiFreeMemoryTop    <= (ResourceHob->PhysicalStart + ResourceHob->ResourceLength)    ) {\r
+    //\r
+    // Cache the resource descriptor HOB for the memory region described by the PHIT HOB\r
+    //\r
+    PhitResourceHob = ResourceHob;\r
+    Found = TRUE;\r
 \r
-          //\r
-          // Cache the resource descriptor HOB for the memory region described by the PHIT HOB\r
-          //\r
-          PhitResourceHob = ResourceHob;\r
-          Found = TRUE;\r
-\r
-          Attributes  = PhitResourceHob->ResourceAttribute;\r
-          BaseAddress = PageAlignAddress (PhitHob->EfiMemoryTop);\r
-          Length      = PageAlignLength  (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - BaseAddress);\r
-          if (Length < MINIMUM_INITIAL_MEMORY_SIZE) {\r
-            BaseAddress = PageAlignAddress (PhitHob->EfiFreeMemoryBottom);\r
-            Length      = PageAlignLength  (PhitHob->EfiFreeMemoryTop - BaseAddress);\r
-            if (Length < MINIMUM_INITIAL_MEMORY_SIZE) {\r
-              BaseAddress = PageAlignAddress (ResourceHob->PhysicalStart);\r
-              Length      = PageAlignLength  ((UINT64)((UINTN)*HobStart - BaseAddress));\r
-            }\r
-          }\r
-          break;\r
-        }\r
+    //\r
+    // Compute range between PHIT EfiMemoryTop and the end of the Resource Descriptor HOB\r
+    //\r
+    Attributes  = PhitResourceHob->ResourceAttribute;\r
+    BaseAddress = PageAlignAddress (PhitHob->EfiMemoryTop);\r
+    Length      = PageAlignLength  (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - BaseAddress);\r
+    if (Length < MinimalMemorySizeNeeded) {\r
+      //\r
+      // If that range is not large enough to intialize the DXE Core, then \r
+      // Compute range between PHIT EfiFreeMemoryBottom and PHIT EfiFreeMemoryTop\r
+      //\r
+      BaseAddress = PageAlignAddress (PhitHob->EfiFreeMemoryBottom);\r
+      Length      = PageAlignLength  (PhitHob->EfiFreeMemoryTop - BaseAddress);\r
+      if (Length < MinimalMemorySizeNeeded) {\r
+        //\r
+        // If that range is not large enough to intialize the DXE Core, then \r
+        // Compute range between the start of the Resource Descriptor HOB and the start of the HOB List\r
+        //\r
+        BaseAddress = PageAlignAddress (ResourceHob->PhysicalStart);\r
+        Length      = PageAlignLength  ((UINT64)((UINTN)*HobStart - BaseAddress));\r
       }\r
     }\r
+    break;\r
   }\r
 \r
   //\r
@@ -1872,71 +2236,88 @@ CoreInitializeMemoryServices (
   ASSERT (Found);\r
 \r
   //\r
-  // Search all the resource descriptor HOBs from the highest possible addresses down for a memory\r
-  // region that is big enough to initialize the DXE core.  Always skip the PHIT Resource HOB.\r
-  // The max address must be within the physically addressible range for the processor.\r
+  // Take the range in the resource descriptor HOB for the memory region described\r
+  // by the PHIT as higher priority if it is big enough. It can make the memory bin\r
+  // allocated to be at the same memory region with PHIT that has more better compatibility\r
+  // to avoid memory fragmentation for some code practices assume and allocate <4G ACPI memory.\r
   //\r
-  MaxMemoryLength = 0;\r
-  MaxAddress      = MAX_ADDRESS;\r
-  do {\r
-    HighAddress = 0;\r
-    Found       = FALSE;\r
+  if (Length < MinimalMemorySizeNeeded) {\r
     //\r
-    // Search for a tested memory region that is below MaxAddress\r
+    // Search all the resource descriptor HOBs from the highest possible addresses down for a memory\r
+    // region that is big enough to initialize the DXE core.  Always skip the PHIT Resource HOB.\r
+    // The max address must be within the physically addressible range for the processor.\r
     //\r
+    HighAddress = MAX_ADDRESS;\r
     for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
+      //\r
+      // Skip the Resource Descriptor HOB that contains the PHIT\r
+      //\r
+      if (Hob.ResourceDescriptor == PhitResourceHob) {\r
+        continue;\r
+      }\r
+      //\r
+      // Skip all HOBs except Resource Descriptor HOBs\r
+      //\r
+      if (GET_HOB_TYPE (Hob) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
+        continue;\r
+      }\r
 \r
       //\r
-      // See if this is a resource descriptor HOB that does not contain the PHIT.\r
+      // Skip Resource Descriptor HOBs that do not describe tested system memory below MAX_ADDRESS\r
       //\r
-      if (Hob.ResourceDescriptor != PhitResourceHob && GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
+      ResourceHob = Hob.ResourceDescriptor;\r
+      if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY) {\r
+        continue;\r
+      }\r
+      if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) != TESTED_MEMORY_ATTRIBUTES) {\r
+        continue;\r
+      }\r
+      if ((ResourceHob->PhysicalStart + ResourceHob->ResourceLength) > (EFI_PHYSICAL_ADDRESS)MAX_ADDRESS) {\r
+        continue;\r
+      }\r
 \r
-        ResourceHob = Hob.ResourceDescriptor;\r
-        //\r
-        // See if this resource descrior HOB describes tested system memory below MaxAddress\r
-        //\r
-        if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
-           (ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == TESTED_MEMORY_ATTRIBUTES &&\r
-            ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MaxAddress) {\r
-          //\r
-          // See if this is the highest tested system memory region below MaxAddress\r
-          //\r
-          if (ResourceHob->PhysicalStart > HighAddress) {\r
+      //\r
+      // Skip Resource Descriptor HOBs that are below a previously found Resource Descriptor HOB\r
+      //\r
+      if (HighAddress != (EFI_PHYSICAL_ADDRESS)MAX_ADDRESS && ResourceHob->PhysicalStart <= HighAddress) {\r
+        continue;\r
+      }\r
 \r
-            MaxResourceHob = ResourceHob;\r
-            HighAddress = MaxResourceHob->PhysicalStart;\r
-            Found = TRUE;\r
-          }\r
-        }\r
+      //\r
+      // Skip Resource Descriptor HOBs that are not large enough to initilize the DXE Core\r
+      //\r
+      TestedMemoryBaseAddress = PageAlignAddress (ResourceHob->PhysicalStart);\r
+      TestedMemoryLength      = PageAlignLength  (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - TestedMemoryBaseAddress);\r
+      if (TestedMemoryLength < MinimalMemorySizeNeeded) {\r
+        continue;\r
       }\r
-    }\r
-    if (Found) {\r
+\r
       //\r
-      // Compute the size of the tested memory region below MaxAddrees\r
+      // Save the range described by the Resource Descriptor that is large enough to initilize the DXE Core\r
       //\r
-      MaxMemoryBaseAddress = PageAlignAddress (MaxResourceHob->PhysicalStart);\r
-      MaxMemoryLength      = PageAlignLength  (MaxResourceHob->PhysicalStart + MaxResourceHob->ResourceLength - MaxMemoryBaseAddress);\r
-      MaxMemoryAttributes  = MaxResourceHob->ResourceAttribute;\r
+      BaseAddress = TestedMemoryBaseAddress;\r
+      Length      = TestedMemoryLength;\r
+      Attributes  = ResourceHob->ResourceAttribute; \r
+      HighAddress = ResourceHob->PhysicalStart;\r
     }\r
-    MaxAddress = ResourceHob->PhysicalStart;\r
-  } while (Found && MaxMemoryLength < MINIMUM_INITIAL_MEMORY_SIZE);\r
-\r
-  if ((Length < MINIMUM_INITIAL_MEMORY_SIZE) ||\r
-      (MaxMemoryBaseAddress > BaseAddress && MaxMemoryLength >= MINIMUM_INITIAL_MEMORY_SIZE)) {\r
-    BaseAddress = MaxMemoryBaseAddress;\r
-    Length      = MaxMemoryLength;\r
-    Attributes  = MaxMemoryAttributes;\r
   }\r
 \r
+  DEBUG ((EFI_D_INFO, "CoreInitializeMemoryServices:\n"));\r
+  DEBUG ((EFI_D_INFO, "  BaseAddress - 0x%lx Length - 0x%lx MinimalMemorySizeNeeded - 0x%lx\n", BaseAddress, Length, MinimalMemorySizeNeeded));\r
+\r
   //\r
   // If no memory regions are found that are big enough to initialize the DXE core, then ASSERT().\r
   //\r
-  ASSERT (Length >= MINIMUM_INITIAL_MEMORY_SIZE);\r
+  ASSERT (Length >= MinimalMemorySizeNeeded);\r
 \r
   //\r
   // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask\r
   //\r
-  Capabilities = CoreConvertResourceDescriptorHobAttributesToCapabilities (EfiGcdMemoryTypeSystemMemory, Attributes);\r
+  if ((Attributes & EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE) == EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE) {\r
+    Capabilities = CoreConvertResourceDescriptorHobAttributesToCapabilities (EfiGcdMemoryTypeMoreReliable, Attributes);\r
+  } else {\r
+    Capabilities = CoreConvertResourceDescriptorHobAttributesToCapabilities (EfiGcdMemoryTypeSystemMemory, Attributes);\r
+  }\r
 \r
   //\r
   // Declare the very first memory region, so the EFI Memory Services are available.\r
@@ -1996,6 +2377,7 @@ CoreInitializeGcdServices (
   UINTN                              Index;\r
   UINT64                             Capabilities;\r
   EFI_HOB_CPU *                      CpuHob;\r
+  EFI_GCD_MEMORY_SPACE_DESCRIPTOR    *MemorySpaceMapHobList;\r
 \r
   //\r
   // Cache the PHIT HOB for later use\r
@@ -2020,6 +2402,8 @@ CoreInitializeGcdServices (
 \r
   InsertHeadList (&mGcdMemorySpaceMap, &Entry->Link);\r
 \r
+  CoreDumpGcdMemorySpaceMap (TRUE);\r
+  \r
   //\r
   // Initialize the GCD I/O Space Map\r
   //\r
@@ -2030,6 +2414,8 @@ CoreInitializeGcdServices (
 \r
   InsertHeadList (&mGcdIoSpaceMap, &Entry->Link);\r
 \r
+  CoreDumpGcdIoSpaceMap (TRUE);\r
+  \r
   //\r
   // Walk the HOB list and add all resource descriptors to the GCD\r
   //\r
@@ -2045,7 +2431,11 @@ CoreInitializeGcdServices (
       switch (ResourceHob->ResourceType) {\r
       case EFI_RESOURCE_SYSTEM_MEMORY:\r
         if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == TESTED_MEMORY_ATTRIBUTES) {\r
-          GcdMemoryType = EfiGcdMemoryTypeSystemMemory;\r
+          if ((ResourceHob->ResourceAttribute & EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE) == EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE) {\r
+            GcdMemoryType = EfiGcdMemoryTypeMoreReliable;\r
+          } else {\r
+            GcdMemoryType = EfiGcdMemoryTypeSystemMemory;\r
+          }\r
         }\r
         if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == INITIALIZED_MEMORY_ATTRIBUTES) {\r
           GcdMemoryType = EfiGcdMemoryTypeReserved;\r
@@ -2053,6 +2443,9 @@ CoreInitializeGcdServices (
         if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == PRESENT_MEMORY_ATTRIBUTES) {\r
           GcdMemoryType = EfiGcdMemoryTypeReserved;\r
         }\r
+        if ((ResourceHob->ResourceAttribute & EFI_RESOURCE_ATTRIBUTE_PERSISTENT) == EFI_RESOURCE_ATTRIBUTE_PERSISTENT) {\r
+          GcdMemoryType = EfiGcdMemoryTypePersistent;\r
+        }\r
         break;\r
       case EFI_RESOURCE_MEMORY_MAPPED_IO:\r
       case EFI_RESOURCE_FIRMWARE_DEVICE:\r
@@ -2071,6 +2464,11 @@ CoreInitializeGcdServices (
       }\r
 \r
       if (GcdMemoryType != EfiGcdMemoryTypeNonExistent) {\r
+        //\r
+        // Validate the Resource HOB Attributes\r
+        //\r
+        CoreValidateResourceDescriptorHobAttributes (ResourceHob->ResourceAttribute);\r
+\r
         //\r
         // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask\r
         //\r
@@ -2100,15 +2498,20 @@ CoreInitializeGcdServices (
   //\r
   // Allocate first memory region from the GCD by the DXE core\r
   //\r
-  Status = CoreAllocateMemorySpace (\r
-             EfiGcdAllocateAddress,\r
-             EfiGcdMemoryTypeSystemMemory,\r
-             0,\r
-             MemoryLength,\r
-             &MemoryBaseAddress,\r
-             gDxeCoreImageHandle,\r
-             NULL\r
-             );\r
+  Status = CoreGetMemorySpaceDescriptor (MemoryBaseAddress, &Descriptor);\r
+  if (!EFI_ERROR (Status)) {\r
+    ASSERT ((Descriptor.GcdMemoryType == EfiGcdMemoryTypeSystemMemory) ||\r
+            (Descriptor.GcdMemoryType == EfiGcdMemoryTypeMoreReliable));\r
+    Status = CoreAllocateMemorySpace (\r
+               EfiGcdAllocateAddress,\r
+               Descriptor.GcdMemoryType,\r
+               0,\r
+               MemoryLength,\r
+               &MemoryBaseAddress,\r
+               gDxeCoreImageHandle,\r
+               NULL\r
+               );\r
+  }\r
 \r
   //\r
   // Walk the HOB list and allocate all memory space that is consumed by memory allocation HOBs,\r
@@ -2129,7 +2532,9 @@ CoreInitializeGcdServices (
                    gDxeCoreImageHandle,\r
                    NULL\r
                    );\r
-        if (!EFI_ERROR (Status) && Descriptor.GcdMemoryType == EfiGcdMemoryTypeSystemMemory) {\r
+        if (!EFI_ERROR (Status) &&\r
+            ((Descriptor.GcdMemoryType == EfiGcdMemoryTypeSystemMemory) ||\r
+             (Descriptor.GcdMemoryType == EfiGcdMemoryTypeMoreReliable))) {\r
           CoreAddMemoryDescriptor (\r
             MemoryHob->AllocDescriptor.MemoryType,\r
             MemoryHob->AllocDescriptor.MemoryBaseAddress,\r
@@ -2155,32 +2560,32 @@ CoreInitializeGcdServices (
     }\r
   }\r
 \r
-  //\r
-  // Relocate HOB List to an allocated pool buffer.\r
-  //\r
-  NewHobList = AllocateCopyPool (\r
-                 (UINTN)PhitHob->EfiFreeMemoryBottom - (UINTN)(*HobStart),\r
-                 *HobStart\r
-                 );\r
-  ASSERT (NewHobList != NULL);\r
-\r
-  *HobStart = NewHobList;\r
-  gHobList  = NewHobList;\r
-\r
   //\r
   // Add and allocate the remaining unallocated system memory to the memory services.\r
   //\r
   Status = CoreGetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);\r
   ASSERT (Status == EFI_SUCCESS);\r
 \r
+  MemorySpaceMapHobList = NULL;\r
   for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
-    if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeSystemMemory) {\r
+    if ((MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeSystemMemory) ||\r
+        (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeMoreReliable)) {\r
       if (MemorySpaceMap[Index].ImageHandle == NULL) {\r
         BaseAddress  = PageAlignAddress (MemorySpaceMap[Index].BaseAddress);\r
         Length       = PageAlignLength  (MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length - BaseAddress);\r
         if (Length == 0 || MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length < BaseAddress) {\r
           continue;\r
         }\r
+        if (((UINTN) MemorySpaceMap[Index].BaseAddress <= (UINTN) (*HobStart)) &&\r
+            ((UINTN) (MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length) >= (UINTN) PhitHob->EfiFreeMemoryBottom)) {\r
+          //\r
+          // Skip the memory space that covers HOB List, it should be processed\r
+          // after HOB List relocation to avoid the resources allocated by others\r
+          // to corrupt HOB List before its relocation.\r
+          //\r
+          MemorySpaceMapHobList = &MemorySpaceMap[Index];\r
+          continue;\r
+        }\r
         CoreAddMemoryDescriptor (\r
           EfiConventionalMemory,\r
           BaseAddress,\r
@@ -2189,7 +2594,7 @@ CoreInitializeGcdServices (
           );\r
         Status = CoreAllocateMemorySpace (\r
                    EfiGcdAllocateAddress,\r
-                   EfiGcdMemoryTypeSystemMemory,\r
+                   MemorySpaceMap[Index].GcdMemoryType,\r
                    0,\r
                    Length,\r
                    &BaseAddress,\r
@@ -2199,6 +2604,47 @@ CoreInitializeGcdServices (
       }\r
     }\r
   }\r
+\r
+  //\r
+  // Relocate HOB List to an allocated pool buffer.\r
+  // The relocation should be at after all the tested memory resources added\r
+  // (except the memory space that covers HOB List) to the memory services,\r
+  // because the memory resource found in CoreInitializeMemoryServices()\r
+  // may have not enough remaining resource for HOB List.\r
+  //\r
+  NewHobList = AllocateCopyPool (\r
+                 (UINTN) PhitHob->EfiFreeMemoryBottom - (UINTN) (*HobStart),\r
+                 *HobStart\r
+                 );\r
+  ASSERT (NewHobList != NULL);\r
+\r
+  *HobStart = NewHobList;\r
+  gHobList  = NewHobList;\r
+\r
+  if (MemorySpaceMapHobList != NULL) {\r
+    //\r
+    // Add and allocate the memory space that covers HOB List to the memory services\r
+    // after HOB List relocation.\r
+    //\r
+    BaseAddress = PageAlignAddress (MemorySpaceMapHobList->BaseAddress);\r
+    Length      = PageAlignLength  (MemorySpaceMapHobList->BaseAddress + MemorySpaceMapHobList->Length - BaseAddress);\r
+    CoreAddMemoryDescriptor (\r
+      EfiConventionalMemory,\r
+      BaseAddress,\r
+      RShiftU64 (Length, EFI_PAGE_SHIFT),\r
+      MemorySpaceMapHobList->Capabilities & (~EFI_MEMORY_RUNTIME)\r
+      );\r
+    Status = CoreAllocateMemorySpace (\r
+               EfiGcdAllocateAddress,\r
+               MemorySpaceMapHobList->GcdMemoryType,\r
+               0,\r
+               Length,\r
+               &BaseAddress,\r
+               gDxeCoreImageHandle,\r
+               NULL\r
+               );\r
+  }\r
+\r
   CoreFreePool (MemorySpaceMap);\r
 \r
   return EFI_SUCCESS;\r