2 UEFI Memory pool management functions.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "HeapGuard.h"
13 STATIC EFI_LOCK mPoolMemoryLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY
);
15 #define POOL_FREE_SIGNATURE SIGNATURE_32('p','f','r','0')
23 #define POOL_HEAD_SIGNATURE SIGNATURE_32('p','h','d','0')
24 #define POOLPAGE_HEAD_SIGNATURE SIGNATURE_32('p','h','d','1')
33 #define SIZE_OF_POOL_HEAD OFFSET_OF(POOL_HEAD,Data)
35 #define POOL_TAIL_SIGNATURE SIGNATURE_32('p','t','a','l')
42 #define POOL_OVERHEAD (SIZE_OF_POOL_HEAD + sizeof(POOL_TAIL))
44 #define HEAD_TO_TAIL(a) \
45 ((POOL_TAIL *) (((CHAR8 *) (a)) + (a)->Size - sizeof(POOL_TAIL)));
48 // Each element is the sum of the 2 previous ones: this allows us to migrate
49 // blocks between bins by splitting them up, while not wasting too much memory
50 // as we would in a strict power-of-2 sequence
52 STATIC CONST UINT16 mPoolSizeTable
[] = {
53 128, 256, 384, 640, 1024, 1664, 2688, 4352, 7040, 11392, 18432, 29824
56 #define SIZE_TO_LIST(a) (GetPoolIndexFromSize (a))
57 #define LIST_TO_SIZE(a) (mPoolSizeTable [a])
59 #define MAX_POOL_LIST (ARRAY_SIZE (mPoolSizeTable))
61 #define MAX_POOL_SIZE (MAX_ADDRESS - POOL_OVERHEAD)
67 #define POOL_SIGNATURE SIGNATURE_32('p','l','s','t')
71 EFI_MEMORY_TYPE MemoryType
;
72 LIST_ENTRY FreeList
[MAX_POOL_LIST
];
77 // Pool header for each memory type.
79 POOL mPoolHead
[EfiMaxMemoryType
];
82 // List of pool header to search for the appropriate memory type.
84 LIST_ENTRY mPoolHeadList
= INITIALIZE_LIST_HEAD_VARIABLE (mPoolHeadList
);
87 Get pool size table index from the specified size.
89 @param Size The specified size to get index from pool table.
91 @return The index of pool size table.
96 GetPoolIndexFromSize (
102 for (Index
= 0; Index
< MAX_POOL_LIST
; Index
++) {
103 if (mPoolSizeTable
[Index
] >= Size
) {
107 return MAX_POOL_LIST
;
111 Called to initialize the pool.
122 for (Type
=0; Type
< EfiMaxMemoryType
; Type
++) {
123 mPoolHead
[Type
].Signature
= 0;
124 mPoolHead
[Type
].Used
= 0;
125 mPoolHead
[Type
].MemoryType
= (EFI_MEMORY_TYPE
) Type
;
126 for (Index
=0; Index
< MAX_POOL_LIST
; Index
++) {
127 InitializeListHead (&mPoolHead
[Type
].FreeList
[Index
]);
134 Look up pool head for specified memory type.
136 @param MemoryType Memory type of which pool head is looked for
138 @return Pointer of Corresponding pool head.
143 IN EFI_MEMORY_TYPE MemoryType
150 if ((UINT32
)MemoryType
< EfiMaxMemoryType
) {
151 return &mPoolHead
[MemoryType
];
155 // MemoryType values in the range 0x80000000..0xFFFFFFFF are reserved for use by UEFI
156 // OS loaders that are provided by operating system vendors.
157 // MemoryType values in the range 0x70000000..0x7FFFFFFF are reserved for OEM use.
159 if ((UINT32
) MemoryType
>= MEMORY_TYPE_OEM_RESERVED_MIN
) {
161 for (Link
= mPoolHeadList
.ForwardLink
; Link
!= &mPoolHeadList
; Link
= Link
->ForwardLink
) {
162 Pool
= CR(Link
, POOL
, Link
, POOL_SIGNATURE
);
163 if (Pool
->MemoryType
== MemoryType
) {
168 Pool
= CoreAllocatePoolI (EfiBootServicesData
, sizeof (POOL
), FALSE
);
173 Pool
->Signature
= POOL_SIGNATURE
;
175 Pool
->MemoryType
= MemoryType
;
176 for (Index
=0; Index
< MAX_POOL_LIST
; Index
++) {
177 InitializeListHead (&Pool
->FreeList
[Index
]);
180 InsertHeadList (&mPoolHeadList
, &Pool
->Link
);
191 Allocate pool of a particular type.
193 @param PoolType Type of pool to allocate
194 @param Size The amount of pool to allocate
195 @param Buffer The address to return a pointer to the allocated
198 @retval EFI_INVALID_PARAMETER Buffer is NULL.
199 PoolType is in the range EfiMaxMemoryType..0x6FFFFFFF.
200 PoolType is EfiPersistentMemory.
201 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
202 @retval EFI_SUCCESS Pool successfully allocated.
207 CoreInternalAllocatePool (
208 IN EFI_MEMORY_TYPE PoolType
,
217 // If it's not a valid type, fail it
219 if ((PoolType
>= EfiMaxMemoryType
&& PoolType
< MEMORY_TYPE_OEM_RESERVED_MIN
) ||
220 (PoolType
== EfiConventionalMemory
) || (PoolType
== EfiPersistentMemory
)) {
221 return EFI_INVALID_PARAMETER
;
224 if (Buffer
== NULL
) {
225 return EFI_INVALID_PARAMETER
;
231 // If size is too large, fail it
232 // Base on the EFI spec, return status of EFI_OUT_OF_RESOURCES
234 if (Size
> MAX_POOL_SIZE
) {
235 return EFI_OUT_OF_RESOURCES
;
238 NeedGuard
= IsPoolTypeToGuard (PoolType
) && !mOnGuarding
;
241 // Acquire the memory lock and make the allocation
243 Status
= CoreAcquireLockOrFail (&mPoolMemoryLock
);
244 if (EFI_ERROR (Status
)) {
245 return EFI_OUT_OF_RESOURCES
;
248 *Buffer
= CoreAllocatePoolI (PoolType
, Size
, NeedGuard
);
249 CoreReleaseLock (&mPoolMemoryLock
);
250 return (*Buffer
!= NULL
) ? EFI_SUCCESS
: EFI_OUT_OF_RESOURCES
;
254 Allocate pool of a particular type.
256 @param PoolType Type of pool to allocate
257 @param Size The amount of pool to allocate
258 @param Buffer The address to return a pointer to the allocated
261 @retval EFI_INVALID_PARAMETER Buffer is NULL.
262 PoolType is in the range EfiMaxMemoryType..0x6FFFFFFF.
263 PoolType is EfiPersistentMemory.
264 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
265 @retval EFI_SUCCESS Pool successfully allocated.
271 IN EFI_MEMORY_TYPE PoolType
,
278 Status
= CoreInternalAllocatePool (PoolType
, Size
, Buffer
);
279 if (!EFI_ERROR (Status
)) {
281 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
282 MemoryProfileActionAllocatePool
,
288 InstallMemoryAttributesTableOnMemoryAllocation (PoolType
);
294 Internal function. Used by the pool functions to allocate pages
295 to back pool allocation requests.
297 @param PoolType The type of memory for the new pool pages
298 @param NoPages No of pages to allocate
299 @param Granularity Bits to align.
300 @param NeedGuard Flag to indicate Guard page is needed or not
302 @return The allocated memory, or NULL
307 CoreAllocatePoolPagesI (
308 IN EFI_MEMORY_TYPE PoolType
,
310 IN UINTN Granularity
,
317 Status
= CoreAcquireLockOrFail (&gMemoryLock
);
318 if (EFI_ERROR (Status
)) {
322 Buffer
= CoreAllocatePoolPages (PoolType
, NoPages
, Granularity
, NeedGuard
);
323 CoreReleaseMemoryLock ();
325 if (Buffer
!= NULL
) {
327 SetGuardForMemory ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Buffer
, NoPages
);
329 ApplyMemoryProtectionPolicy(EfiConventionalMemory
, PoolType
,
330 (EFI_PHYSICAL_ADDRESS
)(UINTN
)Buffer
, EFI_PAGES_TO_SIZE (NoPages
));
336 Internal function to allocate pool of a particular type.
337 Caller must have the memory lock held
339 @param PoolType Type of pool to allocate
340 @param Size The amount of pool to allocate
341 @param NeedGuard Flag to indicate Guard page is needed or not
343 @return The allocate pool, or NULL
348 IN EFI_MEMORY_TYPE PoolType
,
361 UINTN Offset
, MaxOffset
;
367 ASSERT_LOCKED (&mPoolMemoryLock
);
369 if (PoolType
== EfiACPIReclaimMemory
||
370 PoolType
== EfiACPIMemoryNVS
||
371 PoolType
== EfiRuntimeServicesCode
||
372 PoolType
== EfiRuntimeServicesData
) {
374 Granularity
= RUNTIME_PAGE_ALLOCATION_GRANULARITY
;
376 Granularity
= DEFAULT_PAGE_ALLOCATION_GRANULARITY
;
380 // Adjust the size by the pool header & tail overhead
383 HasPoolTail
= !(NeedGuard
&&
384 ((PcdGet8 (PcdHeapGuardPropertyMask
) & BIT7
) == 0));
385 PageAsPool
= (IsHeapGuardEnabled (GUARD_HEAP_TYPE_FREED
) && !mOnGuarding
);
388 // Adjusting the Size to be of proper alignment so that
389 // we don't get an unaligned access fault later when
390 // pool_Tail is being initialized
392 Size
= ALIGN_VARIABLE (Size
);
394 Size
+= POOL_OVERHEAD
;
395 Index
= SIZE_TO_LIST(Size
);
396 Pool
= LookupPoolHead (PoolType
);
403 // If allocation is over max size, just allocate pages for the request
406 if (Index
>= SIZE_TO_LIST (Granularity
) || NeedGuard
|| PageAsPool
) {
408 Size
-= sizeof (POOL_TAIL
);
410 NoPages
= EFI_SIZE_TO_PAGES (Size
) + EFI_SIZE_TO_PAGES (Granularity
) - 1;
411 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (Granularity
) - 1);
412 Head
= CoreAllocatePoolPagesI (PoolType
, NoPages
, Granularity
, NeedGuard
);
414 Head
= AdjustPoolHeadA ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Head
, NoPages
, Size
);
420 // If there's no free pool in the proper list size, go get some more pages
422 if (IsListEmpty (&Pool
->FreeList
[Index
])) {
424 Offset
= LIST_TO_SIZE (Index
);
425 MaxOffset
= Granularity
;
428 // Check the bins holding larger blocks, and carve one up if needed
430 while (++Index
< SIZE_TO_LIST (Granularity
)) {
431 if (!IsListEmpty (&Pool
->FreeList
[Index
])) {
432 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
433 RemoveEntryList (&Free
->Link
);
434 NewPage
= (VOID
*) Free
;
435 MaxOffset
= LIST_TO_SIZE (Index
);
443 NewPage
= CoreAllocatePoolPagesI (PoolType
, EFI_SIZE_TO_PAGES (Granularity
),
444 Granularity
, NeedGuard
);
445 if (NewPage
== NULL
) {
450 // Serve the allocation request from the head of the allocated block
453 Head
= (POOL_HEAD
*) NewPage
;
456 // Carve up remaining space into free pool blocks
459 while (Offset
< MaxOffset
) {
460 ASSERT (Index
< MAX_POOL_LIST
);
461 FSize
= LIST_TO_SIZE(Index
);
463 while (Offset
+ FSize
<= MaxOffset
) {
464 Free
= (POOL_FREE
*) &NewPage
[Offset
];
465 Free
->Signature
= POOL_FREE_SIGNATURE
;
466 Free
->Index
= (UINT32
)Index
;
467 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
473 ASSERT (Offset
== MaxOffset
);
478 // Remove entry from free pool list
480 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
481 RemoveEntryList (&Free
->Link
);
483 Head
= (POOL_HEAD
*) Free
;
491 // Account the allocation
496 // If we have a pool buffer, fill in the header & tail info
498 Head
->Signature
= (PageAsPool
) ? POOLPAGE_HEAD_SIGNATURE
: POOL_HEAD_SIGNATURE
;
500 Head
->Type
= (EFI_MEMORY_TYPE
) PoolType
;
504 Tail
= HEAD_TO_TAIL (Head
);
505 Tail
->Signature
= POOL_TAIL_SIGNATURE
;
508 Size
-= POOL_OVERHEAD
;
510 Size
-= SIZE_OF_POOL_HEAD
;
513 DEBUG_CLEAR_MEMORY (Buffer
, Size
);
517 "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType
,
525 DEBUG ((DEBUG_ERROR
| DEBUG_POOL
, "AllocatePool: failed to allocate %ld bytes\n", (UINT64
) Size
));
536 @param Buffer The allocated pool entry to free
537 @param PoolType Pointer to pool type
539 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
540 @retval EFI_SUCCESS Pool successfully freed.
545 CoreInternalFreePool (
547 OUT EFI_MEMORY_TYPE
*PoolType OPTIONAL
552 if (Buffer
== NULL
) {
553 return EFI_INVALID_PARAMETER
;
556 CoreAcquireLock (&mPoolMemoryLock
);
557 Status
= CoreFreePoolI (Buffer
, PoolType
);
558 CoreReleaseLock (&mPoolMemoryLock
);
565 @param Buffer The allocated pool entry to free
567 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
568 @retval EFI_SUCCESS Pool successfully freed.
578 EFI_MEMORY_TYPE PoolType
;
580 Status
= CoreInternalFreePool (Buffer
, &PoolType
);
581 if (!EFI_ERROR (Status
)) {
583 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
584 MemoryProfileActionFreePool
,
590 InstallMemoryAttributesTableOnMemoryAllocation (PoolType
);
596 Internal function. Frees pool pages allocated via CoreAllocatePoolPagesI().
598 @param PoolType The type of memory for the pool pages
599 @param Memory The base address to free
600 @param NoPages The number of pages to free
606 IN EFI_MEMORY_TYPE PoolType
,
607 IN EFI_PHYSICAL_ADDRESS Memory
,
611 CoreAcquireMemoryLock ();
612 CoreFreePoolPages (Memory
, NoPages
);
613 CoreReleaseMemoryLock ();
615 GuardFreedPagesChecked (Memory
, NoPages
);
616 ApplyMemoryProtectionPolicy (PoolType
, EfiConventionalMemory
,
617 (EFI_PHYSICAL_ADDRESS
)(UINTN
)Memory
, EFI_PAGES_TO_SIZE (NoPages
));
621 Internal function. Frees guarded pool pages.
623 @param PoolType The type of memory for the pool pages
624 @param Memory The base address to free
625 @param NoPages The number of pages to free
630 CoreFreePoolPagesWithGuard (
631 IN EFI_MEMORY_TYPE PoolType
,
632 IN EFI_PHYSICAL_ADDRESS Memory
,
636 EFI_PHYSICAL_ADDRESS MemoryGuarded
;
637 UINTN NoPagesGuarded
;
639 MemoryGuarded
= Memory
;
640 NoPagesGuarded
= NoPages
;
642 AdjustMemoryF (&Memory
, &NoPages
);
644 // It's safe to unset Guard page inside memory lock because there should
645 // be no memory allocation occurred in updating memory page attribute at
646 // this point. And unsetting Guard page before free will prevent Guard
647 // page just freed back to pool from being allocated right away before
648 // marking it usable (from non-present to present).
650 UnsetGuardForMemory (MemoryGuarded
, NoPagesGuarded
);
652 CoreFreePoolPagesI (PoolType
, Memory
, NoPages
);
657 Internal function to free a pool entry.
658 Caller must have the memory lock held
660 @param Buffer The allocated pool entry to free
661 @param PoolType Pointer to pool type
663 @retval EFI_INVALID_PARAMETER Buffer not valid
664 @retval EFI_SUCCESS Buffer successfully freed.
670 OUT EFI_MEMORY_TYPE
*PoolType OPTIONAL
688 ASSERT(Buffer
!= NULL
);
690 // Get the head & tail of the pool entry
692 Head
= BASE_CR (Buffer
, POOL_HEAD
, Data
);
693 ASSERT(Head
!= NULL
);
695 if (Head
->Signature
!= POOL_HEAD_SIGNATURE
&&
696 Head
->Signature
!= POOLPAGE_HEAD_SIGNATURE
) {
697 ASSERT (Head
->Signature
== POOL_HEAD_SIGNATURE
||
698 Head
->Signature
== POOLPAGE_HEAD_SIGNATURE
);
699 return EFI_INVALID_PARAMETER
;
702 IsGuarded
= IsPoolTypeToGuard (Head
->Type
) &&
703 IsMemoryGuarded ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Head
);
704 HasPoolTail
= !(IsGuarded
&&
705 ((PcdGet8 (PcdHeapGuardPropertyMask
) & BIT7
) == 0));
706 PageAsPool
= (Head
->Signature
== POOLPAGE_HEAD_SIGNATURE
);
709 Tail
= HEAD_TO_TAIL (Head
);
710 ASSERT (Tail
!= NULL
);
715 ASSERT (Tail
->Signature
== POOL_TAIL_SIGNATURE
);
716 ASSERT (Head
->Size
== Tail
->Size
);
718 if (Tail
->Signature
!= POOL_TAIL_SIGNATURE
) {
719 return EFI_INVALID_PARAMETER
;
722 if (Head
->Size
!= Tail
->Size
) {
723 return EFI_INVALID_PARAMETER
;
727 ASSERT_LOCKED (&mPoolMemoryLock
);
730 // Determine the pool type and account for it
733 Pool
= LookupPoolHead (Head
->Type
);
735 return EFI_INVALID_PARAMETER
;
738 DEBUG ((DEBUG_POOL
, "FreePool: %p (len %lx) %,ld\n", Head
->Data
, (UINT64
)(Head
->Size
- POOL_OVERHEAD
), (UINT64
) Pool
->Used
));
740 if (Head
->Type
== EfiACPIReclaimMemory
||
741 Head
->Type
== EfiACPIMemoryNVS
||
742 Head
->Type
== EfiRuntimeServicesCode
||
743 Head
->Type
== EfiRuntimeServicesData
) {
745 Granularity
= RUNTIME_PAGE_ALLOCATION_GRANULARITY
;
747 Granularity
= DEFAULT_PAGE_ALLOCATION_GRANULARITY
;
750 if (PoolType
!= NULL
) {
751 *PoolType
= Head
->Type
;
755 // Determine the pool list
757 Index
= SIZE_TO_LIST(Size
);
758 DEBUG_CLEAR_MEMORY (Head
, Size
);
761 // If it's not on the list, it must be pool pages
763 if (Index
>= SIZE_TO_LIST (Granularity
) || IsGuarded
|| PageAsPool
) {
766 // Return the memory pages back to free memory
768 NoPages
= EFI_SIZE_TO_PAGES (Size
) + EFI_SIZE_TO_PAGES (Granularity
) - 1;
769 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (Granularity
) - 1);
771 Head
= AdjustPoolHeadF ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Head
);
772 CoreFreePoolPagesWithGuard (
774 (EFI_PHYSICAL_ADDRESS
)(UINTN
)Head
,
780 (EFI_PHYSICAL_ADDRESS
)(UINTN
)Head
,
788 // Put the pool entry onto the free pool list
790 Free
= (POOL_FREE
*) Head
;
791 ASSERT(Free
!= NULL
);
792 Free
->Signature
= POOL_FREE_SIGNATURE
;
793 Free
->Index
= (UINT32
)Index
;
794 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
797 // See if all the pool entries in the same page as Free are freed pool
800 NewPage
= (CHAR8
*)((UINTN
)Free
& ~(Granularity
- 1));
801 Free
= (POOL_FREE
*) &NewPage
[0];
802 ASSERT(Free
!= NULL
);
804 if (Free
->Signature
== POOL_FREE_SIGNATURE
) {
809 while ((Offset
< Granularity
) && (AllFree
)) {
810 Free
= (POOL_FREE
*) &NewPage
[Offset
];
811 ASSERT(Free
!= NULL
);
812 if (Free
->Signature
!= POOL_FREE_SIGNATURE
) {
815 Offset
+= LIST_TO_SIZE(Free
->Index
);
821 // All of the pool entries in the same page as Free are free pool
823 // Remove all of these pool entries from the free loop lists.
825 Free
= (POOL_FREE
*) &NewPage
[0];
826 ASSERT(Free
!= NULL
);
829 while (Offset
< Granularity
) {
830 Free
= (POOL_FREE
*) &NewPage
[Offset
];
831 ASSERT(Free
!= NULL
);
832 RemoveEntryList (&Free
->Link
);
833 Offset
+= LIST_TO_SIZE(Free
->Index
);
839 CoreFreePoolPagesI (Pool
->MemoryType
, (EFI_PHYSICAL_ADDRESS
) (UINTN
)NewPage
,
840 EFI_SIZE_TO_PAGES (Granularity
));
846 // If this is an OS/OEM specific memory type, then check to see if the last
847 // portion of that memory type has been freed. If it has, then free the
848 // list entry for that memory type
850 if (((UINT32
) Pool
->MemoryType
>= MEMORY_TYPE_OEM_RESERVED_MIN
) && Pool
->Used
== 0) {
851 RemoveEntryList (&Pool
->Link
);
852 CoreFreePoolI (Pool
, NULL
);