]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Gcd/Gcd.c
Enhance BDS to support Boot/Driver option whose option number >= 0xFF.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Gcd / Gcd.c
index f96cae8684d8ca3b4e06ef8bfec42cc02b471b8d..467e4a00793e8dbae029ff4c365fa3841d9b4709 100644 (file)
@@ -3,8 +3,8 @@
   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 - 2008, Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2011, 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
@@ -15,6 +15,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 **/\r
 \r
 #include "DxeMain.h"\r
+#include "Gcd.h"\r
 \r
 #define MINIMUM_INITIAL_MEMORY_SIZE 0x10000\r
 \r
@@ -94,6 +95,108 @@ GCD_ATTRIBUTE_CONVERSION_ENTRY mAttributeConversionTable[] = {
   { 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
+  "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
+**/\r
+VOID\r
+EFIAPI\r
+CoreDumpGcdMemorySpaceMap (\r
+  VOID\r
+  )\r
+{\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_EFI_ERROR (Status);\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
+  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
+**/\r
+VOID\r
+EFIAPI\r
+CoreDumpGcdIoSpaceMap (\r
+  VOID\r
+  )\r
+{\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_EFI_ERROR (Status);\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
 /**\r
   Acquire memory lock on mGcdMemorySpaceLock.\r
@@ -570,11 +673,12 @@ 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_CODE_BEGIN ();\r
+      DEBUG ((DEBUG_GCD, "  Status = %r\n", EFI_INVALID_PARAMETER));\r
+    DEBUG_CODE_END ();\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -582,10 +686,11 @@ CoreConvertSpace (
   if ((Operation & GCD_MEMORY_SPACE_OPERATION) != 0) {\r
     CoreAcquireGcdMemoryLock ();\r
     Map = &mGcdMemorySpaceMap;\r
-  }\r
-  if ((Operation & GCD_IO_SPACE_OPERATION) != 0) {\r
+  } else if ((Operation & GCD_IO_SPACE_OPERATION) != 0) {\r
     CoreAcquireGcdIoLock ();\r
     Map = &mGcdIoSpaceMap;\r
+  } else {\r
+    ASSERT (FALSE);\r
   }\r
 \r
   //\r
@@ -597,6 +702,7 @@ CoreConvertSpace (
 \r
     goto Done;\r
   }\r
+  ASSERT (StartLink != NULL && EndLink != NULL);\r
 \r
   //\r
   // Verify that the list of descriptors are unallocated non-existent memory.\r
@@ -682,30 +788,26 @@ CoreConvertSpace (
     Status = EFI_OUT_OF_RESOURCES;\r
     goto Done;\r
   }\r
+  ASSERT (TopEntry != NULL && BottomEntry != NULL);\r
 \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)) {\r
-        Status = EFI_ACCESS_DENIED;\r
-        goto Done;\r
-      }\r
-\r
-      Status = CpuArch->SetMemoryAttributes (\r
-                          CpuArch,\r
-                          BaseAddress,\r
-                          Length,\r
-                          CpuArchAttributes\r
-                          );\r
-      if (EFI_ERROR (Status)) {\r
-        goto Done;\r
+    if (CpuArchAttributes != INVALID_CPU_ARCH_ATTRIBUTES) {\r
+      if (gCpu != NULL) {\r
+        Status = gCpu->SetMemoryAttributes (\r
+                         gCpu,\r
+                         BaseAddress,\r
+                         Length,\r
+                         CpuArchAttributes\r
+                         );\r
+        if (EFI_ERROR (Status)) {\r
+          goto Done;\r
+        }\r
       }\r
     }\r
-\r
   }\r
 \r
   //\r
@@ -764,11 +866,26 @@ CoreConvertSpace (
   Status = CoreCleanupGcdMapEntry (TopEntry, BottomEntry, StartLink, EndLink, Map);\r
 \r
 Done:\r
+  DEBUG_CODE_BEGIN ();\r
+    DEBUG ((DEBUG_GCD, "  Status = %r\n", Status));\r
+  DEBUG_CODE_END ();\r
+\r
   if ((Operation & GCD_MEMORY_SPACE_OPERATION) != 0) {\r
     CoreReleaseGcdMemoryLock ();\r
+    DEBUG_CODE_BEGIN ();\r
+      //\r
+      // Do not dump GCD Memory Space Map for GCD changes below 16 MB  \r
+      //\r
+      if (BaseAddress >= BASE_16MB) {\r
+        CoreDumpGcdMemorySpaceMap ();\r
+      }\r
+    DEBUG_CODE_END ();\r
   }\r
   if ((Operation & GCD_IO_SPACE_OPERATION) != 0) {\r
     CoreReleaseGcdIoLock ();\r
+    DEBUG_CODE_BEGIN ();\r
+      CoreDumpGcdIoSpaceMap ();\r
+    DEBUG_CODE_END ();\r
   }\r
 \r
   return Status;\r
@@ -867,24 +984,45 @@ CoreAllocateSpace (
   // Make sure parameters are valid\r
   //\r
   if (GcdAllocateType < 0 || GcdAllocateType >= EfiGcdMaxAllocateType) {\r
+    DEBUG_CODE_BEGIN ();\r
+      DEBUG ((DEBUG_GCD, "  Status = %r\n", EFI_INVALID_PARAMETER));\r
+    DEBUG_CODE_END ();\r
     return EFI_INVALID_PARAMETER;\r
   }\r
   if (GcdMemoryType < 0 || GcdMemoryType >= EfiGcdMemoryTypeMaximum) {\r
+    DEBUG_CODE_BEGIN ();\r
+      DEBUG ((DEBUG_GCD, "  Status = %r\n", EFI_INVALID_PARAMETER));\r
+    DEBUG_CODE_END ();\r
     return EFI_INVALID_PARAMETER;\r
   }\r
   if (GcdIoType < 0 || GcdIoType >= EfiGcdIoTypeMaximum) {\r
+    DEBUG_CODE_BEGIN ();\r
+      DEBUG ((DEBUG_GCD, "  Status = %r\n", EFI_INVALID_PARAMETER));\r
+    DEBUG_CODE_END ();\r
     return EFI_INVALID_PARAMETER;\r
   }\r
   if (BaseAddress == NULL) {\r
+    DEBUG_CODE_BEGIN ();\r
+      DEBUG ((DEBUG_GCD, "  Status = %r\n", EFI_INVALID_PARAMETER));\r
+    DEBUG_CODE_END ();\r
     return EFI_INVALID_PARAMETER;\r
   }\r
   if (ImageHandle == NULL) {\r
+    DEBUG_CODE_BEGIN ();\r
+      DEBUG ((DEBUG_GCD, "  Status = %r\n", EFI_INVALID_PARAMETER));\r
+    DEBUG_CODE_END ();\r
     return EFI_INVALID_PARAMETER;\r
   }\r
   if (Alignment >= 64) {\r
+    DEBUG_CODE_BEGIN ();\r
+      DEBUG ((DEBUG_GCD, "  Status = %r\n", EFI_NOT_FOUND));\r
+    DEBUG_CODE_END ();\r
     return EFI_NOT_FOUND;\r
   }\r
   if (Length == 0) {\r
+    DEBUG_CODE_BEGIN ();\r
+      DEBUG ((DEBUG_GCD, "  Status = %r\n", EFI_INVALID_PARAMETER));\r
+    DEBUG_CODE_END ();\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -892,10 +1030,11 @@ CoreAllocateSpace (
   if ((Operation & GCD_MEMORY_SPACE_OPERATION) != 0) {\r
     CoreAcquireGcdMemoryLock ();\r
     Map = &mGcdMemorySpaceMap;\r
-  }\r
-  if ((Operation & GCD_IO_SPACE_OPERATION) != 0) {\r
+  } else if ((Operation & GCD_IO_SPACE_OPERATION) != 0) {\r
     CoreAcquireGcdIoLock ();\r
     Map = &mGcdIoSpaceMap;\r
+  } else {\r
+    ASSERT (FALSE);\r
   }\r
 \r
   Found     = FALSE;\r
@@ -923,6 +1062,7 @@ CoreAllocateSpace (
       Status = EFI_NOT_FOUND;\r
       goto Done;\r
     }\r
+    ASSERT (StartLink != NULL && EndLink != NULL);\r
 \r
     //\r
     // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.\r
@@ -1006,6 +1146,7 @@ CoreAllocateSpace (
         Status = EFI_NOT_FOUND;\r
         goto Done;\r
       }\r
+      ASSERT (StartLink != NULL && EndLink != NULL);\r
 \r
       Link = StartLink;\r
       //\r
@@ -1041,6 +1182,7 @@ CoreAllocateSpace (
     Status = EFI_OUT_OF_RESOURCES;\r
     goto Done;\r
   }\r
+  ASSERT (TopEntry != NULL && BottomEntry != NULL);\r
 \r
   //\r
   // Convert/Insert the list of descriptors from StartLink to EndLink\r
@@ -1060,13 +1202,32 @@ CoreAllocateSpace (
   Status = CoreCleanupGcdMapEntry (TopEntry, BottomEntry, StartLink, EndLink, Map);\r
 \r
 Done:\r
+  DEBUG_CODE_BEGIN ();\r
+    DEBUG ((DEBUG_GCD, "  Status = %r", Status));\r
+    if (!EFI_ERROR (Status)) {\r
+      DEBUG ((DEBUG_GCD, "  (BaseAddress = %016lx)\n", *BaseAddress));\r
+    }\r
+    DEBUG ((DEBUG_GCD, "\n"));\r
+  DEBUG_CODE_END ();\r
+  \r
   if ((Operation & GCD_MEMORY_SPACE_OPERATION) != 0) {\r
     CoreReleaseGcdMemoryLock ();\r
+    DEBUG_CODE_BEGIN ();\r
+      //\r
+      // Do not dump GCD Memory Space Map for GCD changes below 16 MB  \r
+      //\r
+      if (*BaseAddress >= BASE_16MB) {\r
+        CoreDumpGcdMemorySpaceMap ();\r
+      }\r
+    DEBUG_CODE_END ();\r
   }\r
   if ((Operation & GCD_IO_SPACE_OPERATION) !=0) {\r
     CoreReleaseGcdIoLock ();\r
+    DEBUG_CODE_BEGIN ();\r
+      CoreDumpGcdIoSpaceMap ();\r
+    DEBUG_CODE_END ();\r
   }\r
-\r
+  \r
   return Status;\r
 }\r
 \r
@@ -1091,6 +1252,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
@@ -1134,6 +1299,13 @@ CoreAllocateMemorySpace (
   IN     EFI_HANDLE             DeviceHandle OPTIONAL\r
   )\r
 {\r
+  DEBUG ((DEBUG_GCD, "GCD:AllocateMemorySpace(Base=%016lx,Length=%016lx)\n", *BaseAddress, Length));\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
@@ -1241,6 +1413,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
@@ -1262,6 +1436,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
@@ -1327,6 +1503,7 @@ CoreGetMemorySpaceDescriptor (
   if (EFI_ERROR (Status)) {\r
     Status = EFI_NOT_FOUND;\r
   } else {\r
+    ASSERT (StartLink != NULL && EndLink != NULL);\r
     //\r
     // Copy the contents of the found descriptor into Descriptor\r
     //\r
@@ -1360,6 +1537,9 @@ 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
@@ -1452,6 +1632,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
@@ -1491,6 +1674,13 @@ CoreAllocateIoSpace (
   IN     EFI_HANDLE             DeviceHandle OPTIONAL\r
   )\r
 {\r
+  DEBUG ((DEBUG_GCD, "GCD:AllocateIoSpace(Base=%016lx,Length=%016lx)\n", *BaseAddress, Length));\r
+  DEBUG ((DEBUG_GCD, "  GcdAllocateType = %a\n", mGcdAllocationTypeNames[MIN (GcdAllocateType, EfiGcdMaxAllocateType)]));\r
+  DEBUG ((DEBUG_GCD, "  GcdMemoryType   = %a\n", mGcdIoTypeNames[MIN (GcdIoType, 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_IO_OPERATION,\r
            GcdAllocateType,\r
@@ -1522,6 +1712,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
@@ -1543,6 +1735,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
@@ -1606,6 +1800,7 @@ CoreGetIoSpaceDescriptor (
   if (EFI_ERROR (Status)) {\r
     Status = EFI_NOT_FOUND;\r
   } else {\r
+    ASSERT (StartLink != NULL && EndLink != NULL);\r
     //\r
     // Copy the contents of the found descriptor into Descriptor\r
     //\r
@@ -1686,7 +1881,6 @@ Done:
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Converts a Resource Descriptor HOB attributes mask to an EFI Memory Descriptor\r
   capabilities mask\r
@@ -1723,12 +1917,11 @@ CoreConvertResourceDescriptorHobAttributesToCapabilities (
 \r
 \r
 /**\r
-  External function. Initializes the GCD and memory services based on the memory\r
-  descriptor HOBs.  This function is responsible for priming the GCD map and the\r
-  memory map, so memory allocations and resource allocations can be made.  The first\r
-  part of this function can not depend on any memory services until at least one\r
-  memory descriptor is provided to the memory services.  Then the memory services\r
-  can be used to intialize the GCD map.\r
+  External function. Initializes memory services based on the memory\r
+  descriptor HOBs.  This function is responsible for priming the memory\r
+  map, so memory allocations and resource allocations can be made.\r
+  The first part of this function can not depend on any memory services\r
+  until at least one memory descriptor is provided to the memory services.\r
 \r
   @param  HobStart               The start address of the HOB.\r
   @param  MemoryBaseAddress      Start address of memory region found to init DXE\r
@@ -1763,6 +1956,7 @@ CoreInitializeMemoryServices (
   EFI_PHYSICAL_ADDRESS               HighAddress;\r
   EFI_HOB_RESOURCE_DESCRIPTOR        *MaxResourceHob;\r
   EFI_HOB_GUID_TYPE                  *GuidHob;\r
+  UINT32                              ReservedCodePageNumber;\r
 \r
   //\r
   // Point at the first HOB.  This must be the PHIT HOB.\r
@@ -1793,7 +1987,17 @@ CoreInitializeMemoryServices (
   // Cache the PHIT HOB for later use\r
   //\r
   PhitHob = Hob.HandoffInformationTable;\r
-\r
+  \r
+  if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
+       ReservedCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r
+       ReservedCodePageNumber += PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r
+   \r
+       //\r
+       // cache the Top address for loading modules at Fixed Address \r
+       //\r
+    gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress = PhitHob->EfiMemoryTop \r
+                                                                   + EFI_PAGES_TO_SIZE(ReservedCodePageNumber);\r
+  }\r
   //\r
   // See if a Memory Type Information HOB is available\r
   //\r
@@ -1857,7 +2061,7 @@ CoreInitializeMemoryServices (
   // The max address must be within the physically addressible range for the processor.\r
   //\r
   MaxMemoryLength = 0;\r
-  MaxAddress      = EFI_MAX_ADDRESS;\r
+  MaxAddress      = MAX_ADDRESS;\r
   do {\r
     HighAddress = 0;\r
     Found       = FALSE;\r
@@ -1938,11 +2142,8 @@ CoreInitializeMemoryServices (
 /**\r
   External function. Initializes the GCD and memory services based on the memory\r
   descriptor HOBs.  This function is responsible for priming the GCD map and the\r
-  memory map, so memory allocations and resource allocations can be made.  The first\r
-  part of this function can not depend on any memory services until at least one\r
-  memory descriptor is provided to the memory services.  Then the memory services\r
-  can be used to intialize the GCD map. The HobStart will be relocated to a pool\r
-  buffer.\r
+  memory map, so memory allocations and resource allocations can be made. The\r
+  HobStart will be relocated to a pool buffer.\r
 \r
   @param  HobStart               The start address of the HOB\r
   @param  MemoryBaseAddress      Start address of memory region found to init DXE\r
@@ -2003,6 +2204,11 @@ CoreInitializeGcdServices (
 \r
   InsertHeadList (&mGcdMemorySpaceMap, &Entry->Link);\r
 \r
+  DEBUG_CODE_BEGIN ();\r
+    DEBUG ((DEBUG_GCD, "GCD:Initial GCD Memory Space Map\n"));\r
+    CoreDumpGcdMemorySpaceMap ();\r
+  DEBUG_CODE_END ();\r
+  \r
   //\r
   // Initialize the GCD I/O Space Map\r
   //\r
@@ -2013,6 +2219,11 @@ CoreInitializeGcdServices (
 \r
   InsertHeadList (&mGcdIoSpaceMap, &Entry->Link);\r
 \r
+  DEBUG_CODE_BEGIN ();\r
+    DEBUG ((DEBUG_GCD, "GCD:Initial GCD I/O Space Map\n"));\r
+    CoreDumpGcdIoSpaceMap ();\r
+  DEBUG_CODE_END ();\r
+  \r
   //\r
   // Walk the HOB list and add all resource descriptors to the GCD\r
   //\r
@@ -2154,6 +2365,8 @@ CoreInitializeGcdServices (
   // 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
   for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
     if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeSystemMemory) {\r
       if (MemorySpaceMap[Index].ImageHandle == NULL) {\r