X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdePkg%2FLibrary%2FPeiMemoryAllocationLib%2FMemoryAllocationLib.c;h=443c3c6b1231d9e4e4bdbc063ec6bdd03686644f;hb=6809c8723efbc37566db9cd35b725aa81b304094;hp=d711aa79cf91d7c6d19a032b362db0c0515e8e68;hpb=19388d2960b2fe0347da23799e93ccc52f540214;p=mirror_edk2.git diff --git a/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c b/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c index d711aa79cf..443c3c6b12 100644 --- a/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c +++ b/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c @@ -2,11 +2,11 @@ Support routines for memory allocation routines based on PeiService for PEI phase drivers. - Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.
+ Copyright (c) 2006 - 2014, 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 + 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. @@ -21,6 +21,7 @@ #include #include #include +#include /** @@ -44,15 +45,43 @@ InternalAllocatePages ( { EFI_STATUS Status; EFI_PHYSICAL_ADDRESS Memory; + EFI_MEMORY_TYPE RequestType; + EFI_PEI_HOB_POINTERS Hob; if (Pages == 0) { return NULL; } - Status = PeiServicesAllocatePages (MemoryType, Pages, &Memory); + RequestType = MemoryType; + if (MemoryType == EfiReservedMemoryType) { + // + // PEI AllocatePages() doesn't support EfiReservedMemoryType. + // Change RequestType to EfiBootServicesData for memory allocation. + // + RequestType = EfiBootServicesData; + } + + Status = PeiServicesAllocatePages (RequestType, Pages, &Memory); if (EFI_ERROR (Status)) { return NULL; } + + if (MemoryType == EfiReservedMemoryType) { + // + // Memory type needs to be updated to EfiReservedMemoryType. Per PI spec Volume 1, + // PEI AllocatePages() will automate the creation of the Memory Allocation HOB types. + // Search Memory Allocation HOB and find the matched memory region, + // then change its memory type to EfiReservedMemoryType. + // + Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION); + while (Hob.Raw != NULL && Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress != Memory) { + Hob.Raw = GET_NEXT_HOB (Hob); + Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw); + } + ASSERT (Hob.Raw != NULL); + Hob.MemoryAllocation->AllocDescriptor.MemoryType = EfiReservedMemoryType; + } + return (VOID *) (UINTN) Memory; } @@ -135,7 +164,7 @@ AllocateReservedPages ( 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. **/ @@ -160,10 +189,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. @@ -176,8 +207,15 @@ InternalAllocateAlignedPages ( IN UINTN Alignment ) { - VOID *Memory; - UINTN AlignmentMask; + EFI_PHYSICAL_ADDRESS Memory; + EFI_PHYSICAL_ADDRESS AlignedMemory; + EFI_PEI_HOB_POINTERS Hob; + BOOLEAN SkipBeforeMemHob = FALSE; + BOOLEAN SkipAfterMemHob = FALSE; + EFI_PHYSICAL_ADDRESS HobBaseAddress; + UINT64 HobLength; + EFI_MEMORY_TYPE HobMemoryType; + UINTN TotalPages; // // Alignment must be a power of two or zero. @@ -190,17 +228,134 @@ InternalAllocateAlignedPages ( // // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow. // - ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment))); + ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment))); + // // We would rather waste some memory to save PEI code size. + // meaning in addition to the requested size for the aligned mem, + // we simply reserve an overhead memory equal to Alignmemt(page-aligned), no matter what. + // The overhead mem size could be reduced later with more involved malloc mechanisms + // (e.g., somthing that can detect the alignment boundary before allocating memory or + // can request that memory be allocated at a certain address that is aleady aligned). + // + TotalPages = Pages + (Alignment <= EFI_PAGE_SIZE ? 0 : EFI_SIZE_TO_PAGES(Alignment)); + Memory = (EFI_PHYSICAL_ADDRESS) (UINTN) InternalAllocatePages (MemoryType, TotalPages); + if (Memory == 0) { + DEBUG((DEBUG_INFO, "Out of memory resource! \n")); + return NULL; + } + DEBUG ((DEBUG_INFO, "Allocated Memory unaligned: Address = 0x%LX, Pages = 0x%X, Type = %d \n", Memory, TotalPages, (UINTN) MemoryType)); + + // + // Alignment calculation + // + AlignedMemory = Memory; + if (Alignment > EFI_PAGE_SIZE) { + AlignedMemory = ALIGN_VALUE (Memory, Alignment); + } + DEBUG ((DEBUG_INFO, "After aligning to 0x%X bytes: Address = 0x%LX, Pages = 0x%X \n", Alignment, AlignedMemory, Pages)); + + // + // In general three HOBs cover the total allocated space. + // The aligned portion is covered by the aligned mem HOB and + // the unaligned(to be freed) portions before and after the aligned portion are covered by newly created HOBs. + // + // Before mem HOB covers the region between "Memory" and "AlignedMemory" + // Aligned mem HOB covers the region between "AlignedMemory" and "AlignedMemory + EFI_PAGES_TO_SIZE(Pages)" + // After mem HOB covers the region between "AlignedMemory + EFI_PAGES_TO_SIZE(Pages)" and "Memory + EFI_PAGES_TO_SIZE(TotalPages)" + // + // The before or after mem HOBs need to be skipped under special cases where the aligned portion + // touches either the top or bottom of the original allocated space. + // + if (Memory == AlignedMemory) { + SkipBeforeMemHob = TRUE; + } + if ((Memory + EFI_PAGES_TO_SIZE(TotalPages)) == (AlignedMemory + EFI_PAGES_TO_SIZE(Pages))) { + // + // This condition is never met in the current implementation. + // There is always some after-mem since the overhead mem(used in TotalPages) + // is no less than Alignment. + // + SkipAfterMemHob = TRUE; + } + + // + // Search for the mem HOB referring to the original(unaligned) allocation + // and update the size and type if needed. + // + Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION); + while (Hob.Raw != NULL) { + if (Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress == Memory) { + break; + } + Hob.Raw = GET_NEXT_HOB (Hob); + Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw); + } + ASSERT (Hob.Raw != NULL); + if (SkipBeforeMemHob) { + // + // Use this HOB as aligned mem HOB as there is no portion before it. + // + HobLength = EFI_PAGES_TO_SIZE(Pages); + Hob.MemoryAllocation->AllocDescriptor.MemoryLength = HobLength; + } else { + // + // Use this HOB as before mem HOB and create a new HOB for the aligned portion + // + HobLength = (AlignedMemory - Memory); + Hob.MemoryAllocation->AllocDescriptor.MemoryLength = HobLength; + Hob.MemoryAllocation->AllocDescriptor.MemoryType = EfiConventionalMemory; + } + + HobBaseAddress = Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress; + HobMemoryType = Hob.MemoryAllocation->AllocDescriptor.MemoryType; + // - Memory = InternalAllocatePages (MemoryType, Pages + EFI_SIZE_TO_PAGES (Alignment)); - if (Alignment == 0) { - AlignmentMask = Alignment; + // Build the aligned mem HOB if needed + // + if (!SkipBeforeMemHob) { + DEBUG((DEBUG_INFO, "Updated before-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n", + HobBaseAddress, HobLength, (UINTN) HobMemoryType)); + + HobBaseAddress = AlignedMemory; + HobLength = EFI_PAGES_TO_SIZE(Pages); + HobMemoryType = MemoryType; + + BuildMemoryAllocationHob ( + HobBaseAddress, + HobLength, + HobMemoryType + ); + + DEBUG((DEBUG_INFO, "Created aligned-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n", + HobBaseAddress, HobLength, (UINTN) HobMemoryType)); } else { - AlignmentMask = Alignment - 1; + if (HobBaseAddress != 0) { + DEBUG((DEBUG_INFO, "Updated aligned-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n", + HobBaseAddress, HobLength, (UINTN) HobMemoryType)); + } } - return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask); + + + // + // Build the after mem HOB if needed + // + if (!SkipAfterMemHob) { + HobBaseAddress = AlignedMemory + EFI_PAGES_TO_SIZE(Pages); + HobLength = (Memory + EFI_PAGES_TO_SIZE(TotalPages)) - (AlignedMemory + EFI_PAGES_TO_SIZE(Pages)); + HobMemoryType = EfiConventionalMemory; + + BuildMemoryAllocationHob ( + HobBaseAddress, + HobLength, + HobMemoryType + ); + + DEBUG((DEBUG_INFO, "Created after-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n", + HobBaseAddress, HobLength, (UINTN) HobMemoryType)); + } + + return (VOID *) (UINTN) AlignedMemory; } /** @@ -212,9 +367,11 @@ 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. 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. @@ -239,9 +396,11 @@ 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. 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. @@ -266,9 +425,11 @@ 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. 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. @@ -297,7 +458,7 @@ AllocateAlignedReservedPages ( 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. **/ @@ -654,8 +815,8 @@ AllocateReservedCopyPool ( @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. @@ -757,8 +918,8 @@ ReallocateRuntimePool ( @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,7 +946,7 @@ ReallocateReservedPool ( 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