From c3f671eff14b1830f6ccb2092f10dce80eb2c9ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20Ha=CC=88user?= <8659494+mhaeuser@users.noreply.github.com> Date: Mon, 10 Apr 2023 21:09:49 +0200 Subject: [PATCH] BaseTools/Common: Fix aligned page alloc functions --- BaseTools/Source/C/Common/CommonLib.c | 22 +++++ .../CommonMemoryAllocationLib/AlignedPages.c | 84 +++++++++++++++++++ .../CommonMemoryAllocationLib/AlignedPages.h | 60 +++++++++++++ .../CommonMemoryAllocationLib.c | 51 ++--------- .../CommonMemoryAllocationLib.inf | 2 + OpenCorePkg | 2 +- 6 files changed, 175 insertions(+), 46 deletions(-) create mode 100644 MdeModulePkg/Library/CommonMemoryAllocationLib/AlignedPages.c create mode 100644 MdeModulePkg/Library/CommonMemoryAllocationLib/AlignedPages.h diff --git a/BaseTools/Source/C/Common/CommonLib.c b/BaseTools/Source/C/Common/CommonLib.c index d4264cfdfa..0c8beee6aa 100644 --- a/BaseTools/Source/C/Common/CommonLib.c +++ b/BaseTools/Source/C/Common/CommonLib.c @@ -644,6 +644,28 @@ PhaseFreePool ( InternalAlignedFree (Buffer); } +VOID * +InternalAllocateAlignedPages ( + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN Pages, + IN UINTN Alignment + ) +{ + UINTN BufferSize; + + BufferSize = EFI_PAGES_TO_SIZE (Pages); + return InternalAlignedAlloc (MAX (Alignment, EFI_PAGE_SIZE), BufferSize); +} + +VOID +InternalFreeAlignedPages ( + IN VOID *Buffer, + IN UINTN Pages + ) +{ + InternalAlignedFree (Buffer); +} + VOID EFIAPI CpuBreakpoint ( diff --git a/MdeModulePkg/Library/CommonMemoryAllocationLib/AlignedPages.c b/MdeModulePkg/Library/CommonMemoryAllocationLib/AlignedPages.c new file mode 100644 index 0000000000..47203089e3 --- /dev/null +++ b/MdeModulePkg/Library/CommonMemoryAllocationLib/AlignedPages.c @@ -0,0 +1,84 @@ +/** @file + Common code for the Memory Allocation Library aligned page allocation code. + + Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include + +#include +#include + +#include "AlignedPages.h" + +/** + Allocates one or more 4KB pages of a certain memory type at a specified alignment. + + Allocates the number of 4KB pages specified by Pages of a certain memory type with an 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 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. + If Alignment is zero, then byte alignment is used. + + @return A pointer to the allocated buffer or NULL if allocation fails. + +**/ +VOID * +InternalAllocateAlignedPages ( + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN Pages, + IN UINTN Alignment + ) +{ + EFI_STATUS Status; + EFI_PHYSICAL_ADDRESS Memory; + + Status = AllocateAlignedPagesEx ( + AllocateAnyPages, + MemoryType, + Pages, + Alignment, + &Memory + ); + if (EFI_ERROR (Status)) { + return NULL; + } + + return (VOID *)(UINTN)Memory; +} + +/** + Frees one or more 4KB pages that were previously allocated with one of the aligned page + allocation functions in the Memory Allocation Library. + + 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 + 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 Pages The number of 4 KB pages to free. + +**/ +VOID +InternalFreeAlignedPages ( + IN VOID *Buffer, + IN UINTN Pages + ) +{ + FreePages (Buffer, Pages); +} diff --git a/MdeModulePkg/Library/CommonMemoryAllocationLib/AlignedPages.h b/MdeModulePkg/Library/CommonMemoryAllocationLib/AlignedPages.h new file mode 100644 index 0000000000..d5aa547b30 --- /dev/null +++ b/MdeModulePkg/Library/CommonMemoryAllocationLib/AlignedPages.h @@ -0,0 +1,60 @@ +/** @file + Common code for the Memory Allocation Library aligned page allocation code. + + Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef ALIGNED_PAGES_H_ +#define ALIGNED_PAGES_H_ + +/** + Allocates one or more 4KB pages of a certain memory type at a specified alignment. + + Allocates the number of 4KB pages specified by Pages of a certain memory type with an 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 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. + If Alignment is zero, then byte alignment is used. + + @return A pointer to the allocated buffer or NULL if allocation fails. + +**/ +VOID * +InternalAllocateAlignedPages ( + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN Pages, + IN UINTN Alignment + ); + +/** + Frees one or more 4KB pages that were previously allocated with one of the aligned page + allocation functions in the Memory Allocation Library. + + 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 + 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 Pages The number of 4 KB pages to free. + +**/ +VOID +InternalFreeAlignedPages ( + IN VOID *Buffer, + IN UINTN Pages + ); + +#endif // ALIGNED_PAGES_H_ diff --git a/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.c b/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.c index 18365a70b9..7e02718a2b 100644 --- a/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.c +++ b/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.c @@ -7,7 +7,9 @@ **/ -#include +#include +#include +#include #include #include @@ -17,6 +19,8 @@ #include +#include "AlignedPages.h" + /** Allocates one or more 4KB pages of type EfiBootServicesData. @@ -203,49 +207,6 @@ FreePages ( ASSERT_EFI_ERROR (Status); } -/** - Allocates one or more 4KB pages of a certain memory type at a specified alignment. - - Allocates the number of 4KB pages specified by Pages of a certain memory type with an 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 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. - If Alignment is zero, then byte alignment is used. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -STATIC -VOID * -InternalAllocateAlignedPages ( - IN EFI_MEMORY_TYPE MemoryType, - IN UINTN Pages, - IN UINTN Alignment - ) -{ - EFI_STATUS Status; - EFI_PHYSICAL_ADDRESS Memory; - - Status = AllocateAlignedPagesEx ( - AllocateAnyPages, - MemoryType, - Pages, - Alignment, - &Memory - ); - if (EFI_ERROR (Status)) { - return NULL; - } - - return (VOID *)(UINTN)Memory; -} - /** Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment. @@ -406,7 +367,7 @@ FreeAlignedPages ( IN UINTN Pages ) { - FreePages (Buffer, Pages); + InternalFreeAlignedPages (Buffer, Pages); } /** diff --git a/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.inf b/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.inf index 8c806dc642..6bbecffbb0 100644 --- a/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.inf +++ b/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.inf @@ -23,6 +23,8 @@ # [Sources] + AlignedPages.h + AlignedPages.c CommonMemoryAllocationLib.c CommonMemoryAllocationLibEx.c diff --git a/OpenCorePkg b/OpenCorePkg index cda8a58595..997ef4ae8f 160000 --- a/OpenCorePkg +++ b/OpenCorePkg @@ -1 +1 @@ -Subproject commit cda8a58595f933a7ab5c5f8e883ad52f187a5525 +Subproject commit 997ef4ae8f53a4b7f67e12b8d19a05368e6c2728