X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdePkg%2FLibrary%2FPeiMemoryAllocationLib%2FMemoryAllocationLib.c;h=4f1a3041a4734b8b60b994da81677b99370b5a39;hb=9095d37b8fe5bfc3d02adad6ba7fd7359ebc0107;hp=6e0517fe4d9087a5f776b6a5b009386a258e8a21;hpb=446b94b0a1b98635d7db21d67fff158c6f153de4;p=mirror_edk2.git diff --git a/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c b/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c index 6e0517fe4d..4f1a3041a4 100644 --- a/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c +++ b/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c @@ -1,15 +1,15 @@ /** @file - Support routines for memory allocation routines + Support routines for memory allocation routines based on PeiService for PEI phase drivers. - Copyright (c) 2006 - 2008, 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 - http://opensource.org/licenses/bsd-license.php + Copyright (c) 2006 - 2018, 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 + http://opensource.org/licenses/bsd-license.php. - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, - WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. **/ @@ -21,6 +21,7 @@ #include #include #include +#include /** @@ -38,12 +39,12 @@ **/ VOID * InternalAllocatePages ( - IN EFI_MEMORY_TYPE MemoryType, + IN EFI_MEMORY_TYPE MemoryType, IN UINTN Pages ) { EFI_STATUS Status; - EFI_PHYSICAL_ADDRESS Memory; + EFI_PHYSICAL_ADDRESS Memory; if (Pages == 0) { return NULL; @@ -53,6 +54,7 @@ InternalAllocatePages ( if (EFI_ERROR (Status)) { return NULL; } + return (VOID *) (UINTN) Memory; } @@ -130,12 +132,12 @@ AllocateReservedPages ( must have been allocated on a previous call to the page allocation services of the Memory Allocation Library. If it is not possible to free allocated pages, then this function will perform no actions. - + If Buffer was not allocated with a page allocation function in the Memory Allocation Library, then ASSERT(). If Pages is zero, then ASSERT(). - - @param Buffer Pointer to the buffer of pages to free. + + @param Buffer The pointer to the buffer of pages to free. @param Pages The number of 4 KB pages to free. **/ @@ -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,10 +163,12 @@ 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. - @param Alignment The requested alignment of the allocation. Must be a power of two. + @param Alignment The requested alignment of the allocation. + Must be a power of two. If Alignment is zero, then byte alignment is used. @return A pointer to the allocated buffer or NULL if allocation fails. @@ -171,13 +176,17 @@ FreePages ( **/ VOID * InternalAllocateAlignedPages ( - IN EFI_MEMORY_TYPE MemoryType, + IN EFI_MEMORY_TYPE MemoryType, IN UINTN Pages, 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. @@ -187,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; } /** @@ -210,11 +249,13 @@ InternalAllocateAlignedPages ( alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned. 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 Pages The number of 4 KB pages to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. + @param Alignment The requested alignment of the allocation. + Must be a power of two. If Alignment is zero, then byte alignment is used. @return A pointer to the allocated buffer or NULL if allocation fails. @@ -237,11 +278,13 @@ AllocateAlignedPages ( alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned. 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 Pages The number of 4 KB pages to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. + @param Alignment The requested alignment of the allocation. + Must be a power of two. If Alignment is zero, then byte alignment is used. @return A pointer to the allocated buffer or NULL if allocation fails. @@ -264,11 +307,13 @@ AllocateAlignedRuntimePages ( alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned. 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 Pages The number of 4 KB pages to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. + @param Alignment The requested alignment of the allocation. + Must be a power of two. If Alignment is zero, then byte alignment is used. @return A pointer to the allocated buffer or NULL if allocation fails. @@ -290,14 +335,14 @@ AllocateAlignedReservedPages ( Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer must have been allocated on a previous call to the aligned page allocation services of the Memory - Allocation Library. If it is not possible to free allocated pages, then this function will + Allocation Library. If it is not possible to free allocated pages, then this function will perform no actions. - + If Buffer was not allocated with an aligned page allocation function in the Memory Allocation Library, then ASSERT(). If Pages is zero, then ASSERT(). - - @param Buffer Pointer to the buffer of pages to free. + + @param Buffer The pointer to the buffer of pages to free. @param Pages The number of 4 KB pages to free. **/ @@ -308,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); } /** @@ -329,14 +375,14 @@ FreeAlignedPages ( **/ VOID * InternalAllocatePool ( - IN EFI_MEMORY_TYPE MemoryType, + IN EFI_MEMORY_TYPE MemoryType, IN UINTN AllocationSize ) { // - // If we need lots of small runtime/reserved memory type from PEI in the future, - // we can consider providing a more complex algorithm that allocates runtime pages and - // provide pool allocations from those pages. + // If we need lots of small runtime/reserved memory type from PEI in the future, + // we can consider providing a more complex algorithm that allocates runtime pages and + // provide pool allocations from those pages. // return InternalAllocatePages (MemoryType, EFI_SIZE_TO_PAGES (AllocationSize)); } @@ -361,7 +407,7 @@ AllocatePool ( { EFI_STATUS Status; VOID *Buffer; - + Status = PeiServicesAllocatePool (AllocationSize, &Buffer); if (EFI_ERROR (Status)) { Buffer = NULL; @@ -427,9 +473,9 @@ AllocateReservedPool ( **/ VOID * InternalAllocateZeroPool ( - IN EFI_MEMORY_TYPE PoolType, + IN EFI_MEMORY_TYPE PoolType, IN UINTN AllocationSize - ) + ) { VOID *Memory; @@ -520,7 +566,7 @@ AllocateReservedZeroPool ( allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. If Buffer is NULL, then ASSERT(). - If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). + If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). @param PoolType The type of pool to allocate. @param AllocationSize The number of bytes to allocate and zero. @@ -531,10 +577,10 @@ AllocateReservedZeroPool ( **/ VOID * InternalAllocateCopyPool ( - IN EFI_MEMORY_TYPE PoolType, + IN EFI_MEMORY_TYPE PoolType, IN UINTN AllocationSize, IN CONST VOID *Buffer - ) + ) { VOID *Memory; @@ -546,7 +592,7 @@ InternalAllocateCopyPool ( Memory = CopyMem (Memory, Buffer, AllocationSize); } return Memory; -} +} /** Copies a buffer to an allocated buffer of type EfiBootServicesData. @@ -555,9 +601,9 @@ InternalAllocateCopyPool ( AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. - + If Buffer is NULL, then ASSERT(). - If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). + If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). @param AllocationSize The number of bytes to allocate and zero. @param Buffer The buffer to copy to the allocated buffer. @@ -591,9 +637,9 @@ AllocateCopyPool ( AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. - + If Buffer is NULL, then ASSERT(). - If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). + If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). @param AllocationSize The number of bytes to allocate and zero. @param Buffer The buffer to copy to the allocated buffer. @@ -618,9 +664,9 @@ AllocateRuntimeCopyPool ( AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. - + If Buffer is NULL, then ASSERT(). - If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). + If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). @param AllocationSize The number of bytes to allocate and zero. @param Buffer The buffer to copy to the allocated buffer. @@ -642,27 +688,27 @@ AllocateReservedCopyPool ( Reallocates a buffer of a specified memory type. Allocates and zeros the number bytes specified by NewSize from memory of the type - specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and - NewSize bytes are copied from OldBuffer to the newly allocated buffer, and - OldBuffer is freed. A pointer to the newly allocated buffer is returned. - If NewSize is 0, then a valid buffer of 0 size is returned. If there is not + specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and + NewSize bytes are copied from OldBuffer to the newly allocated buffer, and + OldBuffer is freed. A pointer to the newly allocated buffer is returned. + If NewSize is 0, then a valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. - - If the smaller of NewSize and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1), - then ASSERT(). + + If the allocation of the new buffer is successful and the smaller of NewSize and OldSize + is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT(). @param PoolType The type of pool to allocate. @param OldSize The size, in bytes, of OldBuffer. @param NewSize The size, in bytes, of the buffer to reallocate. - @param OldBuffer The buffer to copy to the allocated buffer. This is an optional - parameter that may be NULL. + @param OldBuffer The buffer to copy to the allocated buffer. This is an + optional parameter that may be NULL. @return A pointer to the allocated buffer or NULL if allocation fails. **/ VOID * InternalReallocatePool ( - IN EFI_MEMORY_TYPE PoolType, + IN EFI_MEMORY_TYPE PoolType, IN UINTN OldSize, IN UINTN NewSize, IN VOID *OldBuffer OPTIONAL @@ -670,10 +716,6 @@ InternalReallocatePool ( { VOID *NewBuffer; - // - // Check the boundary for OldBuffer even if the allocation failure. - // - ASSERT (OldBuffer == NULL || MIN (OldSize, NewSize) <= MAX_ADDRESS - (UINTN) OldBuffer + 1); NewBuffer = InternalAllocateZeroPool (PoolType, NewSize); if (NewBuffer != NULL && OldBuffer != NULL) { CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize)); @@ -686,18 +728,18 @@ InternalReallocatePool ( Reallocates a buffer of type EfiBootServicesData. Allocates and zeros the number bytes specified by NewSize from memory of type - EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and - NewSize bytes are copied from OldBuffer to the newly allocated buffer, and - OldBuffer is freed. A pointer to the newly allocated buffer is returned. - If NewSize is 0, then a valid buffer of 0 size is returned. If there is not + EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and + NewSize bytes are copied from OldBuffer to the newly allocated buffer, and + OldBuffer is freed. A pointer to the newly allocated buffer is returned. + If NewSize is 0, then a valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. - - If the smaller of NewSize and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1), - then ASSERT(). + + If the allocation of the new buffer is successful and the smaller of NewSize and OldSize + is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT(). @param OldSize The size, in bytes, of OldBuffer. @param NewSize The size, in bytes, of the buffer to reallocate. - @param OldBuffer The buffer to copy to the allocated buffer. This is an optional + @param OldBuffer The buffer to copy to the allocated buffer. This is an optional parameter that may be NULL. @return A pointer to the allocated buffer or NULL if allocation fails. @@ -718,18 +760,18 @@ ReallocatePool ( Reallocates a buffer of type EfiRuntimeServicesData. Allocates and zeros the number bytes specified by NewSize from memory of type - EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and - NewSize bytes are copied from OldBuffer to the newly allocated buffer, and - OldBuffer is freed. A pointer to the newly allocated buffer is returned. - If NewSize is 0, then a valid buffer of 0 size is returned. If there is not + EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and + NewSize bytes are copied from OldBuffer to the newly allocated buffer, and + OldBuffer is freed. A pointer to the newly allocated buffer is returned. + If NewSize is 0, then a valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. - If the smaller of NewSize and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1), - then ASSERT(). + If the allocation of the new buffer is successful and the smaller of NewSize and OldSize + is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT(). @param OldSize The size, in bytes, of OldBuffer. @param NewSize The size, in bytes, of the buffer to reallocate. - @param OldBuffer The buffer to copy to the allocated buffer. This is an optional + @param OldBuffer The buffer to copy to the allocated buffer. This is an optional parameter that may be NULL. @return A pointer to the allocated buffer or NULL if allocation fails. @@ -750,19 +792,19 @@ ReallocateRuntimePool ( Reallocates a buffer of type EfiReservedMemoryType. Allocates and zeros the number bytes specified by NewSize from memory of type - EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and - NewSize bytes are copied from OldBuffer to the newly allocated buffer, and - OldBuffer is freed. A pointer to the newly allocated buffer is returned. - If NewSize is 0, then a valid buffer of 0 size is returned. If there is not + EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and + NewSize bytes are copied from OldBuffer to the newly allocated buffer, and + OldBuffer is freed. A pointer to the newly allocated buffer is returned. + If NewSize is 0, then a valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. - If the smaller of NewSize and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1), - then ASSERT(). + If the allocation of the new buffer is successful and the smaller of NewSize and OldSize + is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT(). @param OldSize The size, in bytes, of OldBuffer. @param NewSize The size, in bytes, of the buffer to reallocate. - @param OldBuffer The buffer to copy to the allocated buffer. This is an optional - parameter that may be NULL. + @param OldBuffer The buffer to copy to the allocated buffer. This is an + optional parameter that may be NULL. @return A pointer to the allocated buffer or NULL if allocation fails. @@ -785,11 +827,11 @@ ReallocateReservedPool ( Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the pool allocation services of the Memory Allocation Library. If it is not possible to free pool resources, then this function will perform no actions. - + If Buffer was not allocated with a pool allocation function in the Memory Allocation Library, then ASSERT(). - @param Buffer Pointer to the buffer to free. + @param Buffer The pointer to the buffer to free. **/ VOID