X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdePkg%2FLibrary%2FPeiMemoryAllocationLib%2FMemoryAllocationLib.c;h=5cd24a38a3c66c6526618ee326453897a267fdab;hb=c2ab95806bf0e5ae272f47ea6f192c12ce7ca069;hp=531ad7472fc72cb5355057abdd03488115b79c1a;hpb=58380e9c6174f23df78f777b4209c0fd75245cda;p=mirror_edk2.git diff --git a/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c b/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c index 531ad7472f..5cd24a38a3 100644 --- a/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c +++ b/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c @@ -2,7 +2,7 @@ Support routines for memory allocation routines based on PeiService for PEI phase drivers. - Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.
+ Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -21,6 +21,7 @@ #include #include #include +#include /** @@ -53,6 +54,7 @@ InternalAllocatePages ( if (EFI_ERROR (Status)) { return NULL; } + return (VOID *) (UINTN) Memory; } @@ -146,10 +148,11 @@ FreePages ( IN UINTN Pages ) { + EFI_STATUS Status; + ASSERT (Pages != 0); - // - // PEI phase does not support to free pages, so leave it as NOP. - // + Status = PeiServicesFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages); + ASSERT_EFI_ERROR (Status); } /** @@ -160,6 +163,7 @@ FreePages ( If there is not enough memory at the specified alignment remaining to satisfy the request, then NULL is returned. If Alignment is not a power of two and Alignment is not zero, then ASSERT(). + If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT(). @param MemoryType The type of memory to allocate. @param Pages The number of 4 KB pages to allocate. @@ -177,8 +181,12 @@ InternalAllocateAlignedPages ( IN UINTN Alignment ) { - VOID *Memory; - UINTN AlignmentMask; + EFI_STATUS Status; + EFI_PHYSICAL_ADDRESS Memory; + UINTN AlignedMemory; + UINTN AlignmentMask; + UINTN UnalignedPages; + UINTN RealPages; // // Alignment must be a power of two or zero. @@ -188,20 +196,50 @@ InternalAllocateAlignedPages ( if (Pages == 0) { return NULL; } - // - // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow. - // - ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment))); - // - // We would rather waste some memory to save PEI code size. - // - Memory = InternalAllocatePages (MemoryType, Pages + EFI_SIZE_TO_PAGES (Alignment)); - if (Alignment == 0) { - AlignmentMask = Alignment; + if (Alignment > EFI_PAGE_SIZE) { + // + // Calculate the total number of pages since alignment is larger than page size. + // + AlignmentMask = Alignment - 1; + RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment); + // + // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow. + // + ASSERT (RealPages > Pages); + + Status = PeiServicesAllocatePages (MemoryType, RealPages, &Memory); + if (EFI_ERROR (Status)) { + return NULL; + } + AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask; + UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory); + if (UnalignedPages > 0) { + // + // Free first unaligned page(s). + // + Status = PeiServicesFreePages (Memory, UnalignedPages); + ASSERT_EFI_ERROR (Status); + } + Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages); + UnalignedPages = RealPages - Pages - UnalignedPages; + if (UnalignedPages > 0) { + // + // Free last unaligned page(s). + // + Status = PeiServicesFreePages (Memory, UnalignedPages); + ASSERT_EFI_ERROR (Status); + } } else { - AlignmentMask = Alignment - 1; + // + // Do not over-allocate pages in this case. + // + Status = PeiServicesAllocatePages (MemoryType, Pages, &Memory); + if (EFI_ERROR (Status)) { + return NULL; + } + AlignedMemory = (UINTN) Memory; } - return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask); + return (VOID *) AlignedMemory; } /** @@ -213,6 +251,7 @@ InternalAllocateAlignedPages ( request, then NULL is returned. If Alignment is not a power of two and Alignment is not zero, then ASSERT(). + If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT(). @param Pages The number of 4 KB pages to allocate. @param Alignment The requested alignment of the allocation. @@ -241,6 +280,7 @@ AllocateAlignedPages ( request, then NULL is returned. If Alignment is not a power of two and Alignment is not zero, then ASSERT(). + If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT(). @param Pages The number of 4 KB pages to allocate. @param Alignment The requested alignment of the allocation. @@ -269,6 +309,7 @@ AllocateAlignedRuntimePages ( request, then NULL is returned. If Alignment is not a power of two and Alignment is not zero, then ASSERT(). + If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT(). @param Pages The number of 4 KB pages to allocate. @param Alignment The requested alignment of the allocation. @@ -312,10 +353,11 @@ FreeAlignedPages ( IN UINTN Pages ) { + EFI_STATUS Status; + ASSERT (Pages != 0); - // - // PEI phase does not support to free pages, so leave it as NOP. - // + Status = PeiServicesFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages); + ASSERT_EFI_ERROR (Status); } /**