From a46bbdd379b54cd465ba75a84596babc8cbc5dbc Mon Sep 17 00:00:00 2001 From: Star Zeng Date: Thu, 17 Sep 2015 08:32:14 +0000 Subject: [PATCH] MdeModulePkg DxeCore: Relocate HOB List after other tested memory resources added The HOB List relocation should be at after all the tested memory resources added (except the memory space that covers HOB List) to the memory services, because the memory resource found in CoreInitializeMemoryServices() may have not enough remaining resource for HOB List. And the memory space that covers HOB List should be processed after HOB List relocation to avoid the resources allocated by others to corrupt HOB List before its relocation. Cc: Jiewen Yao Cc: Liming Gao Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Star Zeng Reviewed-by: Jiewen Yao git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@18499 6f19259b-4bc3-4df7-8a09-765794883524 --- MdeModulePkg/Core/Dxe/Gcd/Gcd.c | 65 +++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Gcd/Gcd.c b/MdeModulePkg/Core/Dxe/Gcd/Gcd.c index a50fda2466..77586a9ac8 100644 --- a/MdeModulePkg/Core/Dxe/Gcd/Gcd.c +++ b/MdeModulePkg/Core/Dxe/Gcd/Gcd.c @@ -2311,6 +2311,7 @@ CoreInitializeGcdServices ( UINTN Index; UINT64 Capabilities; EFI_HOB_CPU * CpuHob; + EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMapHobList; // // Cache the PHIT HOB for later use @@ -2493,24 +2494,13 @@ CoreInitializeGcdServices ( } } - // - // Relocate HOB List to an allocated pool buffer. - // - NewHobList = AllocateCopyPool ( - (UINTN)PhitHob->EfiFreeMemoryBottom - (UINTN)(*HobStart), - *HobStart - ); - ASSERT (NewHobList != NULL); - - *HobStart = NewHobList; - gHobList = NewHobList; - // // Add and allocate the remaining unallocated system memory to the memory services. // Status = CoreGetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap); ASSERT (Status == EFI_SUCCESS); + MemorySpaceMapHobList = NULL; for (Index = 0; Index < NumberOfDescriptors; Index++) { if ((MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeSystemMemory) || (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeMoreReliable)) { @@ -2520,6 +2510,16 @@ CoreInitializeGcdServices ( if (Length == 0 || MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length < BaseAddress) { continue; } + if (((UINTN) MemorySpaceMap[Index].BaseAddress <= (UINTN) (*HobStart)) && + ((UINTN) (MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length) >= (UINTN) PhitHob->EfiFreeMemoryBottom)) { + // + // Skip the memory space that covers HOB List, it should be processed + // after HOB List relocation to avoid the resources allocated by others + // to corrupt HOB List before its relocation. + // + MemorySpaceMapHobList = &MemorySpaceMap[Index]; + continue; + } CoreAddMemoryDescriptor ( EfiConventionalMemory, BaseAddress, @@ -2538,6 +2538,47 @@ CoreInitializeGcdServices ( } } } + + // + // Relocate HOB List to an allocated pool buffer. + // The relocation should be at after all the tested memory resources added + // (except the memory space that covers HOB List) to the memory services, + // because the memory resource found in CoreInitializeMemoryServices() + // may have not enough remaining resource for HOB List. + // + NewHobList = AllocateCopyPool ( + (UINTN) PhitHob->EfiFreeMemoryBottom - (UINTN) (*HobStart), + *HobStart + ); + ASSERT (NewHobList != NULL); + + *HobStart = NewHobList; + gHobList = NewHobList; + + if (MemorySpaceMapHobList != NULL) { + // + // Add and allocate the memory space that covers HOB List to the memory services + // after HOB List relocation. + // + BaseAddress = PageAlignAddress (MemorySpaceMapHobList->BaseAddress); + Length = PageAlignLength (MemorySpaceMapHobList->BaseAddress + MemorySpaceMapHobList->Length - BaseAddress); + CoreAddMemoryDescriptor ( + EfiConventionalMemory, + BaseAddress, + RShiftU64 (Length, EFI_PAGE_SHIFT), + MemorySpaceMapHobList->Capabilities & (~EFI_MEMORY_RUNTIME) + ); + Status = CoreAllocateMemorySpace ( + EfiGcdAllocateAddress, + MemorySpaceMapHobList->GcdMemoryType, + 0, + Length, + &BaseAddress, + gDxeCoreImageHandle, + NULL + ); + } + CoreFreePool (MemorySpaceMap); return EFI_SUCCESS; -- 2.39.2