]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Gcd/Gcd.c
ShellPkg: Fix GCC build fail and code refine.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Gcd / Gcd.c
index 5292d368c5a0a89a1aa6dd795fedf754dd86821b..77586a9ac8f3adcb85d6e868fef3d7f6bdb0025f 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 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2015, 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
@@ -25,9 +25,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
@@ -81,18 +83,21 @@ 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_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_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
@@ -103,6 +108,8 @@ GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mGcdMemoryTypeNames[] = {
   "Reserved ",  // EfiGcdMemoryTypeReserved\r
   "SystemMem",  // EfiGcdMemoryTypeSystemMemory\r
   "MMIO     ",  // EfiGcdMemoryTypeMemoryMappedIo\r
+  "PersisMem",  // EfiGcdMemoryTypePersistentMemory\r
+  "MoreRelia",  // EfiGcdMemoryTypeMoreReliable\r
   "Unknown  "   // EfiGcdMemoryTypeMaximum\r
 };\r
 \r
@@ -148,7 +155,7 @@ CoreDumpGcdMemorySpaceMap (
     UINTN                            Index;\r
    \r
     Status = CoreGetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);\r
-    ASSERT_EFI_ERROR (Status);\r
+    ASSERT (Status == EFI_SUCCESS && MemorySpaceMap != NULL);\r
 \r
     if (InitialMap) {\r
       DEBUG ((DEBUG_GCD, "GCD:Initial GCD Memory Space Map\n"));\r
@@ -190,7 +197,7 @@ CoreDumpGcdIoSpaceMap (
     UINTN                        Index;\r
     \r
     Status = CoreGetIoSpaceMap (&NumberOfDescriptors, &IoSpaceMap);\r
-    ASSERT_EFI_ERROR (Status);\r
+    ASSERT (Status == EFI_SUCCESS && IoSpaceMap != NULL);\r
     \r
     if (InitialMap) {\r
       DEBUG ((DEBUG_GCD, "GCD:Initial GCD I/O Space Map\n"));\r
@@ -231,6 +238,10 @@ CoreValidateResourceDescriptorHobAttributes (
           ((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
@@ -689,7 +700,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
@@ -797,7 +809,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
@@ -811,6 +823,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
@@ -831,16 +860,20 @@ CoreConvertSpace (
     //\r
     CpuArchAttributes = ConverToCpuArchAttributes (Attributes);\r
     if (CpuArchAttributes != INVALID_CPU_ARCH_ATTRIBUTES) {\r
-      if (gCpu != NULL) {\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
-        if (EFI_ERROR (Status)) {\r
-          goto Done;\r
-        }\r
+      }\r
+      if (EFI_ERROR (Status)) {\r
+        CoreFreePool (TopEntry);\r
+        CoreFreePool (BottomEntry);\r
+        goto Done;\r
       }\r
     }\r
   }\r
@@ -886,11 +919,17 @@ CoreConvertSpace (
       Entry->GcdIoType = EfiGcdIoTypeNonExistent;\r
       break;\r
     //\r
-    // Set attribute operations\r
+    // Set attributes operation\r
     //\r
     case GCD_SET_ATTRIBUTES_MEMORY_OPERATION:\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
@@ -1007,15 +1046,15 @@ 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
@@ -1346,9 +1385,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
@@ -1524,8 +1563,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
@@ -1543,6 +1592,45 @@ CoreSetMemorySpaceAttributes (
 }\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);\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
@@ -1905,7 +1993,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
@@ -2159,7 +2247,11 @@ CoreInitializeMemoryServices (
   //\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
@@ -2219,6 +2311,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
@@ -2272,7 +2365,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
@@ -2280,6 +2377,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 = EfiGcdMemoryTypePersistentMemory;\r
+        }\r
         break;\r
       case EFI_RESOURCE_MEMORY_MAPPED_IO:\r
       case EFI_RESOURCE_FIRMWARE_DEVICE:\r
@@ -2332,15 +2432,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
@@ -2361,7 +2466,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
@@ -2387,32 +2494,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
@@ -2421,7 +2528,7 @@ CoreInitializeGcdServices (
           );\r
         Status = CoreAllocateMemorySpace (\r
                    EfiGcdAllocateAddress,\r
-                   EfiGcdMemoryTypeSystemMemory,\r
+                   MemorySpaceMap[Index].GcdMemoryType,\r
                    0,\r
                    Length,\r
                    &BaseAddress,\r
@@ -2431,6 +2538,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