2 UEFI Memory pool management functions.
4 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 STATIC EFI_LOCK mPoolMemoryLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY
);
20 #define POOL_FREE_SIGNATURE SIGNATURE_32('p','f','r','0')
28 #define POOL_HEAD_SIGNATURE SIGNATURE_32('p','h','d','0')
37 #define SIZE_OF_POOL_HEAD OFFSET_OF(POOL_HEAD,Data)
39 #define POOL_TAIL_SIGNATURE SIGNATURE_32('p','t','a','l')
46 #define POOL_OVERHEAD (SIZE_OF_POOL_HEAD + sizeof(POOL_TAIL))
48 #define HEAD_TO_TAIL(a) \
49 ((POOL_TAIL *) (((CHAR8 *) (a)) + (a)->Size - sizeof(POOL_TAIL)));
52 // Each element is the sum of the 2 previous ones: this allows us to migrate
53 // blocks between bins by splitting them up, while not wasting too much memory
54 // as we would in a strict power-of-2 sequence
56 STATIC CONST UINT16 mPoolSizeTable
[] = {
57 128, 256, 384, 640, 1024, 1664, 2688, 4352, 7040, 11392, 18432, 29824
60 #define SIZE_TO_LIST(a) (GetPoolIndexFromSize (a))
61 #define LIST_TO_SIZE(a) (mPoolSizeTable [a])
63 #define MAX_POOL_LIST (ARRAY_SIZE (mPoolSizeTable))
65 #define MAX_POOL_SIZE (MAX_ADDRESS - POOL_OVERHEAD)
71 #define POOL_SIGNATURE SIGNATURE_32('p','l','s','t')
75 EFI_MEMORY_TYPE MemoryType
;
76 LIST_ENTRY FreeList
[MAX_POOL_LIST
];
81 // Pool header for each memory type.
83 POOL mPoolHead
[EfiMaxMemoryType
];
86 // List of pool header to search for the appropriate memory type.
88 LIST_ENTRY mPoolHeadList
= INITIALIZE_LIST_HEAD_VARIABLE (mPoolHeadList
);
91 Get pool size table index from the specified size.
93 @param Size The specified size to get index from pool table.
95 @return The index of pool size table.
100 GetPoolIndexFromSize (
106 for (Index
= 0; Index
< MAX_POOL_LIST
; Index
++) {
107 if (mPoolSizeTable
[Index
] >= Size
) {
111 return MAX_POOL_LIST
;
115 Called to initialize the pool.
126 for (Type
=0; Type
< EfiMaxMemoryType
; Type
++) {
127 mPoolHead
[Type
].Signature
= 0;
128 mPoolHead
[Type
].Used
= 0;
129 mPoolHead
[Type
].MemoryType
= (EFI_MEMORY_TYPE
) Type
;
130 for (Index
=0; Index
< MAX_POOL_LIST
; Index
++) {
131 InitializeListHead (&mPoolHead
[Type
].FreeList
[Index
]);
138 Look up pool head for specified memory type.
140 @param MemoryType Memory type of which pool head is looked for
142 @return Pointer of Corresponding pool head.
147 IN EFI_MEMORY_TYPE MemoryType
154 if ((UINT32
)MemoryType
< EfiMaxMemoryType
) {
155 return &mPoolHead
[MemoryType
];
159 // MemoryType values in the range 0x80000000..0xFFFFFFFF are reserved for use by UEFI
160 // OS loaders that are provided by operating system vendors.
161 // MemoryType values in the range 0x70000000..0x7FFFFFFF are reserved for OEM use.
163 if ((UINT32
) MemoryType
>= MEMORY_TYPE_OEM_RESERVED_MIN
) {
165 for (Link
= mPoolHeadList
.ForwardLink
; Link
!= &mPoolHeadList
; Link
= Link
->ForwardLink
) {
166 Pool
= CR(Link
, POOL
, Link
, POOL_SIGNATURE
);
167 if (Pool
->MemoryType
== MemoryType
) {
172 Pool
= CoreAllocatePoolI (EfiBootServicesData
, sizeof (POOL
));
177 Pool
->Signature
= POOL_SIGNATURE
;
179 Pool
->MemoryType
= MemoryType
;
180 for (Index
=0; Index
< MAX_POOL_LIST
; Index
++) {
181 InitializeListHead (&Pool
->FreeList
[Index
]);
184 InsertHeadList (&mPoolHeadList
, &Pool
->Link
);
195 Allocate pool of a particular type.
197 @param PoolType Type of pool to allocate
198 @param Size The amount of pool to allocate
199 @param Buffer The address to return a pointer to the allocated
202 @retval EFI_INVALID_PARAMETER Buffer is NULL.
203 PoolType is in the range EfiMaxMemoryType..0x6FFFFFFF.
204 PoolType is EfiPersistentMemory.
205 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
206 @retval EFI_SUCCESS Pool successfully allocated.
211 CoreInternalAllocatePool (
212 IN EFI_MEMORY_TYPE PoolType
,
220 // If it's not a valid type, fail it
222 if ((PoolType
>= EfiMaxMemoryType
&& PoolType
< MEMORY_TYPE_OEM_RESERVED_MIN
) ||
223 (PoolType
== EfiConventionalMemory
) || (PoolType
== EfiPersistentMemory
)) {
224 return EFI_INVALID_PARAMETER
;
227 if (Buffer
== NULL
) {
228 return EFI_INVALID_PARAMETER
;
234 // If size is too large, fail it
235 // Base on the EFI spec, return status of EFI_OUT_OF_RESOURCES
237 if (Size
> MAX_POOL_SIZE
) {
238 return EFI_OUT_OF_RESOURCES
;
242 // Acquire the memory lock and make the allocation
244 Status
= CoreAcquireLockOrFail (&mPoolMemoryLock
);
245 if (EFI_ERROR (Status
)) {
246 return EFI_OUT_OF_RESOURCES
;
249 *Buffer
= CoreAllocatePoolI (PoolType
, Size
);
250 CoreReleaseLock (&mPoolMemoryLock
);
251 return (*Buffer
!= NULL
) ? EFI_SUCCESS
: EFI_OUT_OF_RESOURCES
;
255 Allocate pool of a particular type.
257 @param PoolType Type of pool to allocate
258 @param Size The amount of pool to allocate
259 @param Buffer The address to return a pointer to the allocated
262 @retval EFI_INVALID_PARAMETER Buffer is NULL.
263 PoolType is in the range EfiMaxMemoryType..0x6FFFFFFF.
264 PoolType is EfiPersistentMemory.
265 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
266 @retval EFI_SUCCESS Pool successfully allocated.
272 IN EFI_MEMORY_TYPE PoolType
,
279 Status
= CoreInternalAllocatePool (PoolType
, Size
, Buffer
);
280 if (!EFI_ERROR (Status
)) {
282 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
283 MemoryProfileActionAllocatePool
,
289 InstallMemoryAttributesTableOnMemoryAllocation (PoolType
);
296 CoreAllocatePoolPagesI (
297 IN EFI_MEMORY_TYPE PoolType
,
305 Status
= CoreAcquireLockOrFail (&gMemoryLock
);
306 if (EFI_ERROR (Status
)) {
310 Buffer
= CoreAllocatePoolPages (PoolType
, NoPages
, Granularity
);
311 CoreReleaseMemoryLock ();
317 Internal function to allocate pool of a particular type.
318 Caller must have the memory lock held
320 @param PoolType Type of pool to allocate
321 @param Size The amount of pool to allocate
323 @return The allocate pool, or NULL
328 IN EFI_MEMORY_TYPE PoolType
,
340 UINTN Offset
, MaxOffset
;
344 ASSERT_LOCKED (&mPoolMemoryLock
);
346 if (PoolType
== EfiACPIReclaimMemory
||
347 PoolType
== EfiACPIMemoryNVS
||
348 PoolType
== EfiRuntimeServicesCode
||
349 PoolType
== EfiRuntimeServicesData
) {
351 Granularity
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
353 Granularity
= DEFAULT_PAGE_ALLOCATION
;
357 // Adjust the size by the pool header & tail overhead
361 // Adjusting the Size to be of proper alignment so that
362 // we don't get an unaligned access fault later when
363 // pool_Tail is being initialized
365 Size
= ALIGN_VARIABLE (Size
);
367 Size
+= POOL_OVERHEAD
;
368 Index
= SIZE_TO_LIST(Size
);
369 Pool
= LookupPoolHead (PoolType
);
376 // If allocation is over max size, just allocate pages for the request
379 if (Index
>= SIZE_TO_LIST (Granularity
)) {
380 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (Granularity
) - 1;
381 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (Granularity
) - 1);
382 Head
= CoreAllocatePoolPagesI (PoolType
, NoPages
, Granularity
);
387 // If there's no free pool in the proper list size, go get some more pages
389 if (IsListEmpty (&Pool
->FreeList
[Index
])) {
391 Offset
= LIST_TO_SIZE (Index
);
392 MaxOffset
= Granularity
;
395 // Check the bins holding larger blocks, and carve one up if needed
397 while (++Index
< SIZE_TO_LIST (Granularity
)) {
398 if (!IsListEmpty (&Pool
->FreeList
[Index
])) {
399 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
400 RemoveEntryList (&Free
->Link
);
401 NewPage
= (VOID
*) Free
;
402 MaxOffset
= LIST_TO_SIZE (Index
);
410 NewPage
= CoreAllocatePoolPagesI (PoolType
, EFI_SIZE_TO_PAGES (Granularity
), Granularity
);
411 if (NewPage
== NULL
) {
416 // Serve the allocation request from the head of the allocated block
419 Head
= (POOL_HEAD
*) NewPage
;
422 // Carve up remaining space into free pool blocks
425 while (Offset
< MaxOffset
) {
426 ASSERT (Index
< MAX_POOL_LIST
);
427 FSize
= LIST_TO_SIZE(Index
);
429 while (Offset
+ FSize
<= MaxOffset
) {
430 Free
= (POOL_FREE
*) &NewPage
[Offset
];
431 Free
->Signature
= POOL_FREE_SIGNATURE
;
432 Free
->Index
= (UINT32
)Index
;
433 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
439 ASSERT (Offset
== MaxOffset
);
444 // Remove entry from free pool list
446 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
447 RemoveEntryList (&Free
->Link
);
449 Head
= (POOL_HEAD
*) Free
;
457 // If we have a pool buffer, fill in the header & tail info
459 Head
->Signature
= POOL_HEAD_SIGNATURE
;
461 Head
->Type
= (EFI_MEMORY_TYPE
) PoolType
;
462 Tail
= HEAD_TO_TAIL (Head
);
463 Tail
->Signature
= POOL_TAIL_SIGNATURE
;
466 DEBUG_CLEAR_MEMORY (Buffer
, Size
- POOL_OVERHEAD
);
470 "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType
,
472 (UINT64
)(Size
- POOL_OVERHEAD
),
477 // Account the allocation
482 DEBUG ((DEBUG_ERROR
| DEBUG_POOL
, "AllocatePool: failed to allocate %ld bytes\n", (UINT64
) Size
));
493 @param Buffer The allocated pool entry to free
494 @param PoolType Pointer to pool type
496 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
497 @retval EFI_SUCCESS Pool successfully freed.
502 CoreInternalFreePool (
504 OUT EFI_MEMORY_TYPE
*PoolType OPTIONAL
509 if (Buffer
== NULL
) {
510 return EFI_INVALID_PARAMETER
;
513 CoreAcquireLock (&mPoolMemoryLock
);
514 Status
= CoreFreePoolI (Buffer
, PoolType
);
515 CoreReleaseLock (&mPoolMemoryLock
);
522 @param Buffer The allocated pool entry to free
524 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
525 @retval EFI_SUCCESS Pool successfully freed.
535 EFI_MEMORY_TYPE PoolType
;
537 Status
= CoreInternalFreePool (Buffer
, &PoolType
);
538 if (!EFI_ERROR (Status
)) {
540 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
541 MemoryProfileActionFreePool
,
547 InstallMemoryAttributesTableOnMemoryAllocation (PoolType
);
555 IN EFI_MEMORY_TYPE PoolType
,
556 IN EFI_PHYSICAL_ADDRESS Memory
,
560 CoreAcquireMemoryLock ();
561 CoreFreePoolPages (Memory
, NoPages
);
562 CoreReleaseMemoryLock ();
566 Internal function to free a pool entry.
567 Caller must have the memory lock held
569 @param Buffer The allocated pool entry to free
570 @param PoolType Pointer to pool type
572 @retval EFI_INVALID_PARAMETER Buffer not valid
573 @retval EFI_SUCCESS Buffer successfully freed.
579 OUT EFI_MEMORY_TYPE
*PoolType OPTIONAL
594 ASSERT(Buffer
!= NULL
);
596 // Get the head & tail of the pool entry
598 Head
= CR (Buffer
, POOL_HEAD
, Data
, POOL_HEAD_SIGNATURE
);
599 ASSERT(Head
!= NULL
);
601 if (Head
->Signature
!= POOL_HEAD_SIGNATURE
) {
602 return EFI_INVALID_PARAMETER
;
605 Tail
= HEAD_TO_TAIL (Head
);
606 ASSERT(Tail
!= NULL
);
611 ASSERT (Tail
->Signature
== POOL_TAIL_SIGNATURE
);
612 ASSERT (Head
->Size
== Tail
->Size
);
613 ASSERT_LOCKED (&mPoolMemoryLock
);
615 if (Tail
->Signature
!= POOL_TAIL_SIGNATURE
) {
616 return EFI_INVALID_PARAMETER
;
619 if (Head
->Size
!= Tail
->Size
) {
620 return EFI_INVALID_PARAMETER
;
624 // Determine the pool type and account for it
627 Pool
= LookupPoolHead (Head
->Type
);
629 return EFI_INVALID_PARAMETER
;
632 DEBUG ((DEBUG_POOL
, "FreePool: %p (len %lx) %,ld\n", Head
->Data
, (UINT64
)(Head
->Size
- POOL_OVERHEAD
), (UINT64
) Pool
->Used
));
634 if (Head
->Type
== EfiACPIReclaimMemory
||
635 Head
->Type
== EfiACPIMemoryNVS
||
636 Head
->Type
== EfiRuntimeServicesCode
||
637 Head
->Type
== EfiRuntimeServicesData
) {
639 Granularity
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
641 Granularity
= DEFAULT_PAGE_ALLOCATION
;
644 if (PoolType
!= NULL
) {
645 *PoolType
= Head
->Type
;
649 // Determine the pool list
651 Index
= SIZE_TO_LIST(Size
);
652 DEBUG_CLEAR_MEMORY (Head
, Size
);
655 // If it's not on the list, it must be pool pages
657 if (Index
>= SIZE_TO_LIST (Granularity
)) {
660 // Return the memory pages back to free memory
662 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (Granularity
) - 1;
663 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (Granularity
) - 1);
664 CoreFreePoolPagesI (Pool
->MemoryType
, (EFI_PHYSICAL_ADDRESS
) (UINTN
) Head
, NoPages
);
669 // Put the pool entry onto the free pool list
671 Free
= (POOL_FREE
*) Head
;
672 ASSERT(Free
!= NULL
);
673 Free
->Signature
= POOL_FREE_SIGNATURE
;
674 Free
->Index
= (UINT32
)Index
;
675 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
678 // See if all the pool entries in the same page as Free are freed pool
681 NewPage
= (CHAR8
*)((UINTN
)Free
& ~(Granularity
- 1));
682 Free
= (POOL_FREE
*) &NewPage
[0];
683 ASSERT(Free
!= NULL
);
685 if (Free
->Signature
== POOL_FREE_SIGNATURE
) {
690 while ((Offset
< Granularity
) && (AllFree
)) {
691 Free
= (POOL_FREE
*) &NewPage
[Offset
];
692 ASSERT(Free
!= NULL
);
693 if (Free
->Signature
!= POOL_FREE_SIGNATURE
) {
696 Offset
+= LIST_TO_SIZE(Free
->Index
);
702 // All of the pool entries in the same page as Free are free pool
704 // Remove all of these pool entries from the free loop lists.
706 Free
= (POOL_FREE
*) &NewPage
[0];
707 ASSERT(Free
!= NULL
);
710 while (Offset
< Granularity
) {
711 Free
= (POOL_FREE
*) &NewPage
[Offset
];
712 ASSERT(Free
!= NULL
);
713 RemoveEntryList (&Free
->Link
);
714 Offset
+= LIST_TO_SIZE(Free
->Index
);
720 CoreFreePoolPagesI (Pool
->MemoryType
, (EFI_PHYSICAL_ADDRESS
) (UINTN
)NewPage
,
721 EFI_SIZE_TO_PAGES (Granularity
));
727 // If this is an OS/OEM specific memory type, then check to see if the last
728 // portion of that memory type has been freed. If it has, then free the
729 // list entry for that memory type
731 if (((UINT32
) Pool
->MemoryType
>= MEMORY_TYPE_OEM_RESERVED_MIN
) && Pool
->Used
== 0) {
732 RemoveEntryList (&Pool
->Link
);
733 CoreFreePoolI (Pool
, NULL
);