X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FCore%2FDxe%2FMem%2FPool.c;h=5248ee2e6c0221cc2fbd713078b2a4a621c03da0;hp=f99293da5c51a2eb7dd04b565102f02e548658af;hb=d4731a98a3a5ddc2fed73d2998884f2cbee44ba9;hpb=7970100ccbd48e6f931b078cb8e615eabee26141 diff --git a/MdeModulePkg/Core/Dxe/Mem/Pool.c b/MdeModulePkg/Core/Dxe/Mem/Pool.c index f99293da5c..5248ee2e6c 100644 --- a/MdeModulePkg/Core/Dxe/Mem/Pool.c +++ b/MdeModulePkg/Core/Dxe/Mem/Pool.c @@ -1,7 +1,7 @@ /** @file UEFI Memory pool management functions. -Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2016, 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 @@ -15,6 +15,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "DxeMain.h" #include "Imem.h" +STATIC EFI_LOCK mPoolMemoryLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY); + #define POOL_FREE_SIGNATURE SIGNATURE_32('p','f','r','0') typedef struct { UINT32 Signature; @@ -41,19 +43,24 @@ typedef struct { UINTN Size; } POOL_TAIL; - -#define POOL_SHIFT 7 - #define POOL_OVERHEAD (SIZE_OF_POOL_HEAD + sizeof(POOL_TAIL)) #define HEAD_TO_TAIL(a) \ ((POOL_TAIL *) (((CHAR8 *) (a)) + (a)->Size - sizeof(POOL_TAIL))); +// +// Each element is the sum of the 2 previous ones: this allows us to migrate +// blocks between bins by splitting them up, while not wasting too much memory +// as we would in a strict power-of-2 sequence +// +STATIC CONST UINT16 mPoolSizeTable[] = { + 128, 256, 384, 640, 1024, 1664, 2688, 4352, 7040, 11392, 18432, 29824 +}; -#define SIZE_TO_LIST(a) ((a) >> POOL_SHIFT) -#define LIST_TO_SIZE(a) ((a+1) << POOL_SHIFT) +#define SIZE_TO_LIST(a) (GetPoolIndexFromSize (a)) +#define LIST_TO_SIZE(a) (mPoolSizeTable [a]) -#define MAX_POOL_LIST SIZE_TO_LIST(DEFAULT_PAGE_ALLOCATION) +#define MAX_POOL_LIST (ARRAY_SIZE (mPoolSizeTable)) #define MAX_POOL_SIZE (MAX_ADDRESS - POOL_OVERHEAD) @@ -80,6 +87,29 @@ POOL mPoolHead[EfiMaxMemoryType]; // LIST_ENTRY mPoolHeadList = INITIALIZE_LIST_HEAD_VARIABLE (mPoolHeadList); +/** + Get pool size table index from the specified size. + + @param Size The specified size to get index from pool table. + + @return The index of pool size table. + +**/ +STATIC +UINTN +GetPoolIndexFromSize ( + UINTN Size + ) +{ + UINTN Index; + + for (Index = 0; Index < MAX_POOL_LIST; Index++) { + if (mPoolSizeTable [Index] >= Size) { + return Index; + } + } + return MAX_POOL_LIST; +} /** Called to initialize the pool. @@ -126,10 +156,11 @@ LookupPoolHead ( } // - // MemoryType values in the range 0x80000000..0xFFFFFFFF are reserved for use by UEFI - // OS loaders that are provided by operating system vendors + // MemoryType values in the range 0x80000000..0xFFFFFFFF are reserved for use by UEFI + // OS loaders that are provided by operating system vendors. + // MemoryType values in the range 0x70000000..0x7FFFFFFF are reserved for OEM use. // - if ((INT32)MemoryType < 0) { + if ((UINT32) MemoryType >= MEMORY_TYPE_OEM_RESERVED_MIN) { for (Link = mPoolHeadList.ForwardLink; Link != &mPoolHeadList; Link = Link->ForwardLink) { Pool = CR(Link, POOL, Link, POOL_SIGNATURE); @@ -168,7 +199,9 @@ LookupPoolHead ( @param Buffer The address to return a pointer to the allocated pool - @retval EFI_INVALID_PARAMETER PoolType not valid or Buffer is NULL. + @retval EFI_INVALID_PARAMETER Buffer is NULL. + PoolType is in the range EfiMaxMemoryType..0x6FFFFFFF. + PoolType is EfiPersistentMemory. @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed. @retval EFI_SUCCESS Pool successfully allocated. @@ -186,8 +219,8 @@ CoreInternalAllocatePool ( // // If it's not a valid type, fail it // - if ((PoolType >= EfiMaxMemoryType && PoolType <= 0x7fffffff) || - PoolType == EfiConventionalMemory) { + if ((PoolType >= EfiMaxMemoryType && PoolType < MEMORY_TYPE_OEM_RESERVED_MIN) || + (PoolType == EfiConventionalMemory) || (PoolType == EfiPersistentMemory)) { return EFI_INVALID_PARAMETER; } @@ -208,13 +241,13 @@ CoreInternalAllocatePool ( // // Acquire the memory lock and make the allocation // - Status = CoreAcquireLockOrFail (&gMemoryLock); + Status = CoreAcquireLockOrFail (&mPoolMemoryLock); if (EFI_ERROR (Status)) { return EFI_OUT_OF_RESOURCES; } *Buffer = CoreAllocatePoolI (PoolType, Size); - CoreReleaseMemoryLock (); + CoreReleaseLock (&mPoolMemoryLock); return (*Buffer != NULL) ? EFI_SUCCESS : EFI_OUT_OF_RESOURCES; } @@ -226,7 +259,9 @@ CoreInternalAllocatePool ( @param Buffer The address to return a pointer to the allocated pool - @retval EFI_INVALID_PARAMETER PoolType not valid or Buffer is NULL. + @retval EFI_INVALID_PARAMETER Buffer is NULL. + PoolType is in the range EfiMaxMemoryType..0x6FFFFFFF. + PoolType is EfiPersistentMemory. @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed. @retval EFI_SUCCESS Pool successfully allocated. @@ -243,11 +278,45 @@ CoreAllocatePool ( Status = CoreInternalAllocatePool (PoolType, Size, Buffer); if (!EFI_ERROR (Status)) { - CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionAllocatePool, PoolType, Size, *Buffer); + CoreUpdateProfile ( + (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), + MemoryProfileActionAllocatePool, + PoolType, + Size, + *Buffer, + NULL + ); + InstallMemoryAttributesTableOnMemoryAllocation (PoolType); } return Status; } +STATIC +VOID * +CoreAllocatePoolPagesI ( + IN EFI_MEMORY_TYPE PoolType, + IN UINTN NoPages, + IN UINTN Granularity + ) +{ + VOID *Buffer; + EFI_STATUS Status; + + Status = CoreAcquireLockOrFail (&gMemoryLock); + if (EFI_ERROR (Status)) { + return NULL; + } + + Buffer = CoreAllocatePoolPages (PoolType, NoPages, Granularity); + CoreReleaseMemoryLock (); + + if (Buffer != NULL) { + ApplyMemoryProtectionPolicy (EfiConventionalMemory, PoolType, + (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, EFI_PAGES_TO_SIZE (NoPages)); + } + return Buffer; +} + /** Internal function to allocate pool of a particular type. Caller must have the memory lock held @@ -272,20 +341,20 @@ CoreAllocatePoolI ( VOID *Buffer; UINTN Index; UINTN FSize; - UINTN Offset; + UINTN Offset, MaxOffset; UINTN NoPages; UINTN Granularity; - ASSERT_LOCKED (&gMemoryLock); + ASSERT_LOCKED (&mPoolMemoryLock); if (PoolType == EfiACPIReclaimMemory || PoolType == EfiACPIMemoryNVS || PoolType == EfiRuntimeServicesCode || PoolType == EfiRuntimeServicesData) { - Granularity = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT; + Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY; } else { - Granularity = DEFAULT_PAGE_ALLOCATION; + Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY; } // @@ -311,10 +380,10 @@ CoreAllocatePoolI ( // If allocation is over max size, just allocate pages for the request // (slow) // - if (Index >= MAX_POOL_LIST) { + if (Index >= SIZE_TO_LIST (Granularity)) { NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (Granularity) - 1; NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (Granularity) - 1); - Head = CoreAllocatePoolPages (PoolType, NoPages, Granularity); + Head = CoreAllocatePoolPagesI (PoolType, NoPages, Granularity); goto Done; } @@ -323,35 +392,56 @@ CoreAllocatePoolI ( // if (IsListEmpty (&Pool->FreeList[Index])) { + Offset = LIST_TO_SIZE (Index); + MaxOffset = Granularity; + + // + // Check the bins holding larger blocks, and carve one up if needed + // + while (++Index < SIZE_TO_LIST (Granularity)) { + if (!IsListEmpty (&Pool->FreeList[Index])) { + Free = CR (Pool->FreeList[Index].ForwardLink, POOL_FREE, Link, POOL_FREE_SIGNATURE); + RemoveEntryList (&Free->Link); + NewPage = (VOID *) Free; + MaxOffset = LIST_TO_SIZE (Index); + goto Carve; + } + } + // // Get another page // - NewPage = CoreAllocatePoolPages(PoolType, EFI_SIZE_TO_PAGES (Granularity), Granularity); + NewPage = CoreAllocatePoolPagesI (PoolType, EFI_SIZE_TO_PAGES (Granularity), Granularity); if (NewPage == NULL) { goto Done; } // - // Carve up new page into free pool blocks + // Serve the allocation request from the head of the allocated block + // +Carve: + Head = (POOL_HEAD *) NewPage; + // - Offset = 0; - while (Offset < Granularity) { + // Carve up remaining space into free pool blocks + // + Index--; + while (Offset < MaxOffset) { ASSERT (Index < MAX_POOL_LIST); FSize = LIST_TO_SIZE(Index); - while (Offset + FSize <= Granularity) { + while (Offset + FSize <= MaxOffset) { Free = (POOL_FREE *) &NewPage[Offset]; Free->Signature = POOL_FREE_SIGNATURE; Free->Index = (UINT32)Index; InsertHeadList (&Pool->FreeList[Index], &Free->Link); Offset += FSize; } - Index -= 1; } - ASSERT (Offset == Granularity); - Index = SIZE_TO_LIST(Size); + ASSERT (Offset == MaxOffset); + goto Done; } // @@ -405,6 +495,7 @@ Done: Frees pool. @param Buffer The allocated pool entry to free + @param PoolType Pointer to pool type @retval EFI_INVALID_PARAMETER Buffer is not a valid value. @retval EFI_SUCCESS Pool successfully freed. @@ -413,7 +504,8 @@ Done: EFI_STATUS EFIAPI CoreInternalFreePool ( - IN VOID *Buffer + IN VOID *Buffer, + OUT EFI_MEMORY_TYPE *PoolType OPTIONAL ) { EFI_STATUS Status; @@ -422,9 +514,9 @@ CoreInternalFreePool ( return EFI_INVALID_PARAMETER; } - CoreAcquireMemoryLock (); - Status = CoreFreePoolI (Buffer); - CoreReleaseMemoryLock (); + CoreAcquireLock (&mPoolMemoryLock); + Status = CoreFreePoolI (Buffer, PoolType); + CoreReleaseLock (&mPoolMemoryLock); return Status; } @@ -443,20 +535,46 @@ CoreFreePool ( IN VOID *Buffer ) { - EFI_STATUS Status; + EFI_STATUS Status; + EFI_MEMORY_TYPE PoolType; - Status = CoreInternalFreePool (Buffer); + Status = CoreInternalFreePool (Buffer, &PoolType); if (!EFI_ERROR (Status)) { - CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionFreePool, (EFI_MEMORY_TYPE) 0, 0, Buffer); + CoreUpdateProfile ( + (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), + MemoryProfileActionFreePool, + PoolType, + 0, + Buffer, + NULL + ); + InstallMemoryAttributesTableOnMemoryAllocation (PoolType); } return Status; } +STATIC +VOID +CoreFreePoolPagesI ( + IN EFI_MEMORY_TYPE PoolType, + IN EFI_PHYSICAL_ADDRESS Memory, + IN UINTN NoPages + ) +{ + CoreAcquireMemoryLock (); + CoreFreePoolPages (Memory, NoPages); + CoreReleaseMemoryLock (); + + ApplyMemoryProtectionPolicy (PoolType, EfiConventionalMemory, + (EFI_PHYSICAL_ADDRESS)(UINTN)Memory, EFI_PAGES_TO_SIZE (NoPages)); +} + /** Internal function to free a pool entry. Caller must have the memory lock held @param Buffer The allocated pool entry to free + @param PoolType Pointer to pool type @retval EFI_INVALID_PARAMETER Buffer not valid @retval EFI_SUCCESS Buffer successfully freed. @@ -464,7 +582,8 @@ CoreFreePool ( **/ EFI_STATUS CoreFreePoolI ( - IN VOID *Buffer + IN VOID *Buffer, + OUT EFI_MEMORY_TYPE *PoolType OPTIONAL ) { POOL *Pool; @@ -475,7 +594,6 @@ CoreFreePoolI ( UINTN NoPages; UINTN Size; CHAR8 *NewPage; - UINTN FSize; UINTN Offset; BOOLEAN AllFree; UINTN Granularity; @@ -499,7 +617,7 @@ CoreFreePoolI ( // ASSERT (Tail->Signature == POOL_TAIL_SIGNATURE); ASSERT (Head->Size == Tail->Size); - ASSERT_LOCKED (&gMemoryLock); + ASSERT_LOCKED (&mPoolMemoryLock); if (Tail->Signature != POOL_TAIL_SIGNATURE) { return EFI_INVALID_PARAMETER; @@ -525,9 +643,13 @@ CoreFreePoolI ( Head->Type == EfiRuntimeServicesCode || Head->Type == EfiRuntimeServicesData) { - Granularity = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT; + Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY; } else { - Granularity = DEFAULT_PAGE_ALLOCATION; + Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY; + } + + if (PoolType != NULL) { + *PoolType = Head->Type; } // @@ -539,14 +661,14 @@ CoreFreePoolI ( // // If it's not on the list, it must be pool pages // - if (Index >= MAX_POOL_LIST) { + if (Index >= SIZE_TO_LIST (Granularity)) { // // Return the memory pages back to free memory // NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (Granularity) - 1; NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (Granularity) - 1); - CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN) Head, NoPages); + CoreFreePoolPagesI (Pool->MemoryType, (EFI_PHYSICAL_ADDRESS) (UINTN) Head, NoPages); } else { @@ -569,22 +691,16 @@ CoreFreePoolI ( if (Free->Signature == POOL_FREE_SIGNATURE) { - Index = Free->Index; - AllFree = TRUE; Offset = 0; while ((Offset < Granularity) && (AllFree)) { - FSize = LIST_TO_SIZE(Index); - while (Offset + FSize <= Granularity) { - Free = (POOL_FREE *) &NewPage[Offset]; - ASSERT(Free != NULL); - if (Free->Signature != POOL_FREE_SIGNATURE) { - AllFree = FALSE; - } - Offset += FSize; + Free = (POOL_FREE *) &NewPage[Offset]; + ASSERT(Free != NULL); + if (Free->Signature != POOL_FREE_SIGNATURE) { + AllFree = FALSE; } - Index -= 1; + Offset += LIST_TO_SIZE(Free->Index); } if (AllFree) { @@ -596,36 +712,32 @@ CoreFreePoolI ( // Free = (POOL_FREE *) &NewPage[0]; ASSERT(Free != NULL); - Index = Free->Index; Offset = 0; while (Offset < Granularity) { - FSize = LIST_TO_SIZE(Index); - while (Offset + FSize <= Granularity) { - Free = (POOL_FREE *) &NewPage[Offset]; - ASSERT(Free != NULL); - RemoveEntryList (&Free->Link); - Offset += FSize; - } - Index -= 1; + Free = (POOL_FREE *) &NewPage[Offset]; + ASSERT(Free != NULL); + RemoveEntryList (&Free->Link); + Offset += LIST_TO_SIZE(Free->Index); } // // Free the page // - CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN)NewPage, EFI_SIZE_TO_PAGES (Granularity)); + CoreFreePoolPagesI (Pool->MemoryType, (EFI_PHYSICAL_ADDRESS) (UINTN)NewPage, + EFI_SIZE_TO_PAGES (Granularity)); } } } // - // If this is an OS specific memory type, then check to see if the last + // If this is an OS/OEM specific memory type, then check to see if the last // portion of that memory type has been freed. If it has, then free the // list entry for that memory type // - if ((INT32)Pool->MemoryType < 0 && Pool->Used == 0) { + if (((UINT32) Pool->MemoryType >= MEMORY_TYPE_OEM_RESERVED_MIN) && Pool->Used == 0) { RemoveEntryList (&Pool->Link); - CoreFreePoolI (Pool); + CoreFreePoolI (Pool, NULL); } return EFI_SUCCESS;