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
}\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
(MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeMoreReliable)) {\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
}\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