EmbeddedPkg/PrePi: Check for enough space before aligning heap pointer

Update check for enough space to occur prior to alignment offset.
This prevents cases where EfiFreeMemoryTop < EfiFreeMemoryBottom.

Signed-off-by: Jeff Brasen <jbrasen@nvidia.com>
Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
This commit is contained in:
Jeff Brasen 2022-09-23 09:56:16 -06:00 committed by mergify[bot]
parent 2500ce1011
commit ef9974b298

View File

@ -23,30 +23,24 @@ InternalAllocatePages (
) )
{ {
EFI_PEI_HOB_POINTERS Hob; EFI_PEI_HOB_POINTERS Hob;
EFI_PHYSICAL_ADDRESS Offset; EFI_PHYSICAL_ADDRESS NewTop;
Hob.Raw = GetHobList (); Hob.Raw = GetHobList ();
// Check to see if on 4k boundary NewTop = Hob.HandoffInformationTable->EfiFreeMemoryTop & ~(EFI_PHYSICAL_ADDRESS)EFI_PAGE_MASK;
Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF; NewTop -= Pages * EFI_PAGE_SIZE;
if (Offset != 0) {
// If not aligned, make the allocation aligned.
Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;
}
// //
// Verify that there is sufficient memory to satisfy the allocation // Verify that there is sufficient memory to satisfy the allocation
// //
if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) { if (NewTop < (Hob.HandoffInformationTable->EfiFreeMemoryBottom + sizeof (EFI_HOB_MEMORY_ALLOCATION))) {
return 0; return NULL;
} else { }
// //
// Update the PHIT to reflect the memory usage // Update the PHIT to reflect the memory usage
// //
Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE; Hob.HandoffInformationTable->EfiFreeMemoryTop = NewTop;
// This routine used to create a memory allocation HOB a la PEI, but that's not
// necessary for us.
// //
// Create a memory allocation HOB. // Create a memory allocation HOB.
@ -56,8 +50,8 @@ InternalAllocatePages (
Pages * EFI_PAGE_SIZE, Pages * EFI_PAGE_SIZE,
MemoryType MemoryType
); );
return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop; return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
}
} }
/** /**