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 #define POOL_FREE_SIGNATURE SIGNATURE_32('p','f','r','0')
26 #define POOL_HEAD_SIGNATURE SIGNATURE_32('p','h','d','0')
35 #define SIZE_OF_POOL_HEAD OFFSET_OF(POOL_HEAD,Data)
37 #define POOL_TAIL_SIGNATURE SIGNATURE_32('p','t','a','l')
44 #define POOL_OVERHEAD (SIZE_OF_POOL_HEAD + sizeof(POOL_TAIL))
46 #define HEAD_TO_TAIL(a) \
47 ((POOL_TAIL *) (((CHAR8 *) (a)) + (a)->Size - sizeof(POOL_TAIL)));
50 // Each element is the sum of the 2 previous ones: this allows us to migrate
51 // blocks between bins by splitting them up, while not wasting too much memory
52 // as we would in a strict power-of-2 sequence
54 STATIC CONST UINT16 mPoolSizeTable
[] = {
55 128, 256, 384, 640, 1024, 1664, 2688, 4352, 7040, 11392, 18432, 29824
58 #define SIZE_TO_LIST(a) (GetPoolIndexFromSize (a))
59 #define LIST_TO_SIZE(a) (mPoolSizeTable [a])
61 #define MAX_POOL_LIST (sizeof (mPoolSizeTable) / sizeof (mPoolSizeTable[0]))
63 #define MAX_POOL_SIZE (MAX_ADDRESS - POOL_OVERHEAD)
69 #define POOL_SIGNATURE SIGNATURE_32('p','l','s','t')
73 EFI_MEMORY_TYPE MemoryType
;
74 LIST_ENTRY FreeList
[MAX_POOL_LIST
];
79 // Pool header for each memory type.
81 POOL mPoolHead
[EfiMaxMemoryType
];
84 // List of pool header to search for the appropriate memory type.
86 LIST_ENTRY mPoolHeadList
= INITIALIZE_LIST_HEAD_VARIABLE (mPoolHeadList
);
89 Get pool size table index from the specified size.
91 @param Size The specified size to get index from pool table.
93 @return The index of pool size table.
98 GetPoolIndexFromSize (
104 for (Index
= 0; Index
< MAX_POOL_LIST
; Index
++) {
105 if (mPoolSizeTable
[Index
] >= Size
) {
109 return MAX_POOL_LIST
;
113 Called to initialize the pool.
124 for (Type
=0; Type
< EfiMaxMemoryType
; Type
++) {
125 mPoolHead
[Type
].Signature
= 0;
126 mPoolHead
[Type
].Used
= 0;
127 mPoolHead
[Type
].MemoryType
= (EFI_MEMORY_TYPE
) Type
;
128 for (Index
=0; Index
< MAX_POOL_LIST
; Index
++) {
129 InitializeListHead (&mPoolHead
[Type
].FreeList
[Index
]);
136 Look up pool head for specified memory type.
138 @param MemoryType Memory type of which pool head is looked for
140 @return Pointer of Corresponding pool head.
145 IN EFI_MEMORY_TYPE MemoryType
152 if ((UINT32
)MemoryType
< EfiMaxMemoryType
) {
153 return &mPoolHead
[MemoryType
];
157 // MemoryType values in the range 0x80000000..0xFFFFFFFF are reserved for use by UEFI
158 // OS loaders that are provided by operating system vendors.
159 // MemoryType values in the range 0x70000000..0x7FFFFFFF are reserved for OEM use.
161 if ((UINT32
) MemoryType
>= MEMORY_TYPE_OEM_RESERVED_MIN
) {
163 for (Link
= mPoolHeadList
.ForwardLink
; Link
!= &mPoolHeadList
; Link
= Link
->ForwardLink
) {
164 Pool
= CR(Link
, POOL
, Link
, POOL_SIGNATURE
);
165 if (Pool
->MemoryType
== MemoryType
) {
170 Pool
= CoreAllocatePoolI (EfiBootServicesData
, sizeof (POOL
));
175 Pool
->Signature
= POOL_SIGNATURE
;
177 Pool
->MemoryType
= MemoryType
;
178 for (Index
=0; Index
< MAX_POOL_LIST
; Index
++) {
179 InitializeListHead (&Pool
->FreeList
[Index
]);
182 InsertHeadList (&mPoolHeadList
, &Pool
->Link
);
193 Allocate pool of a particular type.
195 @param PoolType Type of pool to allocate
196 @param Size The amount of pool to allocate
197 @param Buffer The address to return a pointer to the allocated
200 @retval EFI_INVALID_PARAMETER Buffer is NULL.
201 PoolType is in the range EfiMaxMemoryType..0x6FFFFFFF.
202 PoolType is EfiPersistentMemory.
203 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
204 @retval EFI_SUCCESS Pool successfully allocated.
209 CoreInternalAllocatePool (
210 IN EFI_MEMORY_TYPE PoolType
,
218 // If it's not a valid type, fail it
220 if ((PoolType
>= EfiMaxMemoryType
&& PoolType
< MEMORY_TYPE_OEM_RESERVED_MIN
) ||
221 (PoolType
== EfiConventionalMemory
) || (PoolType
== EfiPersistentMemory
)) {
222 return EFI_INVALID_PARAMETER
;
225 if (Buffer
== NULL
) {
226 return EFI_INVALID_PARAMETER
;
232 // If size is too large, fail it
233 // Base on the EFI spec, return status of EFI_OUT_OF_RESOURCES
235 if (Size
> MAX_POOL_SIZE
) {
236 return EFI_OUT_OF_RESOURCES
;
240 // Acquire the memory lock and make the allocation
242 Status
= CoreAcquireLockOrFail (&gMemoryLock
);
243 if (EFI_ERROR (Status
)) {
244 return EFI_OUT_OF_RESOURCES
;
247 *Buffer
= CoreAllocatePoolI (PoolType
, Size
);
248 CoreReleaseMemoryLock ();
249 return (*Buffer
!= NULL
) ? EFI_SUCCESS
: EFI_OUT_OF_RESOURCES
;
253 Allocate pool of a particular type.
255 @param PoolType Type of pool to allocate
256 @param Size The amount of pool to allocate
257 @param Buffer The address to return a pointer to the allocated
260 @retval EFI_INVALID_PARAMETER Buffer is NULL.
261 PoolType is in the range EfiMaxMemoryType..0x6FFFFFFF.
262 PoolType is EfiPersistentMemory.
263 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
264 @retval EFI_SUCCESS Pool successfully allocated.
270 IN EFI_MEMORY_TYPE PoolType
,
277 Status
= CoreInternalAllocatePool (PoolType
, Size
, Buffer
);
278 if (!EFI_ERROR (Status
)) {
280 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
281 MemoryProfileActionAllocatePool
,
287 InstallMemoryAttributesTableOnMemoryAllocation (PoolType
);
293 Internal function to allocate pool of a particular type.
294 Caller must have the memory lock held
296 @param PoolType Type of pool to allocate
297 @param Size The amount of pool to allocate
299 @return The allocate pool, or NULL
304 IN EFI_MEMORY_TYPE PoolType
,
316 UINTN Offset
, MaxOffset
;
320 ASSERT_LOCKED (&gMemoryLock
);
322 if (PoolType
== EfiACPIReclaimMemory
||
323 PoolType
== EfiACPIMemoryNVS
||
324 PoolType
== EfiRuntimeServicesCode
||
325 PoolType
== EfiRuntimeServicesData
) {
327 Granularity
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
329 Granularity
= DEFAULT_PAGE_ALLOCATION
;
333 // Adjust the size by the pool header & tail overhead
337 // Adjusting the Size to be of proper alignment so that
338 // we don't get an unaligned access fault later when
339 // pool_Tail is being initialized
341 Size
= ALIGN_VARIABLE (Size
);
343 Size
+= POOL_OVERHEAD
;
344 Index
= SIZE_TO_LIST(Size
);
345 Pool
= LookupPoolHead (PoolType
);
352 // If allocation is over max size, just allocate pages for the request
355 if (Index
>= SIZE_TO_LIST (Granularity
)) {
356 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (Granularity
) - 1;
357 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (Granularity
) - 1);
358 Head
= CoreAllocatePoolPages (PoolType
, NoPages
, Granularity
);
363 // If there's no free pool in the proper list size, go get some more pages
365 if (IsListEmpty (&Pool
->FreeList
[Index
])) {
367 Offset
= LIST_TO_SIZE (Index
);
368 MaxOffset
= Granularity
;
371 // Check the bins holding larger blocks, and carve one up if needed
373 while (++Index
< SIZE_TO_LIST (Granularity
)) {
374 if (!IsListEmpty (&Pool
->FreeList
[Index
])) {
375 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
376 RemoveEntryList (&Free
->Link
);
377 NewPage
= (VOID
*) Free
;
378 MaxOffset
= LIST_TO_SIZE (Index
);
386 NewPage
= CoreAllocatePoolPages(PoolType
, EFI_SIZE_TO_PAGES (Granularity
), Granularity
);
387 if (NewPage
== NULL
) {
392 // Serve the allocation request from the head of the allocated block
395 Head
= (POOL_HEAD
*) NewPage
;
398 // Carve up remaining space into free pool blocks
401 while (Offset
< MaxOffset
) {
402 ASSERT (Index
< MAX_POOL_LIST
);
403 FSize
= LIST_TO_SIZE(Index
);
405 while (Offset
+ FSize
<= MaxOffset
) {
406 Free
= (POOL_FREE
*) &NewPage
[Offset
];
407 Free
->Signature
= POOL_FREE_SIGNATURE
;
408 Free
->Index
= (UINT32
)Index
;
409 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
415 ASSERT (Offset
== MaxOffset
);
420 // Remove entry from free pool list
422 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
423 RemoveEntryList (&Free
->Link
);
425 Head
= (POOL_HEAD
*) Free
;
433 // If we have a pool buffer, fill in the header & tail info
435 Head
->Signature
= POOL_HEAD_SIGNATURE
;
437 Head
->Type
= (EFI_MEMORY_TYPE
) PoolType
;
438 Tail
= HEAD_TO_TAIL (Head
);
439 Tail
->Signature
= POOL_TAIL_SIGNATURE
;
442 DEBUG_CLEAR_MEMORY (Buffer
, Size
- POOL_OVERHEAD
);
446 "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType
,
448 (UINT64
)(Size
- POOL_OVERHEAD
),
453 // Account the allocation
458 DEBUG ((DEBUG_ERROR
| DEBUG_POOL
, "AllocatePool: failed to allocate %ld bytes\n", (UINT64
) Size
));
469 @param Buffer The allocated pool entry to free
470 @param PoolType Pointer to pool type
472 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
473 @retval EFI_SUCCESS Pool successfully freed.
478 CoreInternalFreePool (
480 OUT EFI_MEMORY_TYPE
*PoolType OPTIONAL
485 if (Buffer
== NULL
) {
486 return EFI_INVALID_PARAMETER
;
489 CoreAcquireMemoryLock ();
490 Status
= CoreFreePoolI (Buffer
, PoolType
);
491 CoreReleaseMemoryLock ();
498 @param Buffer The allocated pool entry to free
500 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
501 @retval EFI_SUCCESS Pool successfully freed.
511 EFI_MEMORY_TYPE PoolType
;
513 Status
= CoreInternalFreePool (Buffer
, &PoolType
);
514 if (!EFI_ERROR (Status
)) {
516 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
517 MemoryProfileActionFreePool
,
523 InstallMemoryAttributesTableOnMemoryAllocation (PoolType
);
529 Internal function to free a pool entry.
530 Caller must have the memory lock held
532 @param Buffer The allocated pool entry to free
533 @param PoolType Pointer to pool type
535 @retval EFI_INVALID_PARAMETER Buffer not valid
536 @retval EFI_SUCCESS Buffer successfully freed.
542 OUT EFI_MEMORY_TYPE
*PoolType OPTIONAL
557 ASSERT(Buffer
!= NULL
);
559 // Get the head & tail of the pool entry
561 Head
= CR (Buffer
, POOL_HEAD
, Data
, POOL_HEAD_SIGNATURE
);
562 ASSERT(Head
!= NULL
);
564 if (Head
->Signature
!= POOL_HEAD_SIGNATURE
) {
565 return EFI_INVALID_PARAMETER
;
568 Tail
= HEAD_TO_TAIL (Head
);
569 ASSERT(Tail
!= NULL
);
574 ASSERT (Tail
->Signature
== POOL_TAIL_SIGNATURE
);
575 ASSERT (Head
->Size
== Tail
->Size
);
576 ASSERT_LOCKED (&gMemoryLock
);
578 if (Tail
->Signature
!= POOL_TAIL_SIGNATURE
) {
579 return EFI_INVALID_PARAMETER
;
582 if (Head
->Size
!= Tail
->Size
) {
583 return EFI_INVALID_PARAMETER
;
587 // Determine the pool type and account for it
590 Pool
= LookupPoolHead (Head
->Type
);
592 return EFI_INVALID_PARAMETER
;
595 DEBUG ((DEBUG_POOL
, "FreePool: %p (len %lx) %,ld\n", Head
->Data
, (UINT64
)(Head
->Size
- POOL_OVERHEAD
), (UINT64
) Pool
->Used
));
597 if (Head
->Type
== EfiACPIReclaimMemory
||
598 Head
->Type
== EfiACPIMemoryNVS
||
599 Head
->Type
== EfiRuntimeServicesCode
||
600 Head
->Type
== EfiRuntimeServicesData
) {
602 Granularity
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
604 Granularity
= DEFAULT_PAGE_ALLOCATION
;
607 if (PoolType
!= NULL
) {
608 *PoolType
= Head
->Type
;
612 // Determine the pool list
614 Index
= SIZE_TO_LIST(Size
);
615 DEBUG_CLEAR_MEMORY (Head
, Size
);
618 // If it's not on the list, it must be pool pages
620 if (Index
>= SIZE_TO_LIST (Granularity
)) {
623 // Return the memory pages back to free memory
625 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (Granularity
) - 1;
626 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (Granularity
) - 1);
627 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Head
, NoPages
);
632 // Put the pool entry onto the free pool list
634 Free
= (POOL_FREE
*) Head
;
635 ASSERT(Free
!= NULL
);
636 Free
->Signature
= POOL_FREE_SIGNATURE
;
637 Free
->Index
= (UINT32
)Index
;
638 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
641 // See if all the pool entries in the same page as Free are freed pool
644 NewPage
= (CHAR8
*)((UINTN
)Free
& ~(Granularity
- 1));
645 Free
= (POOL_FREE
*) &NewPage
[0];
646 ASSERT(Free
!= NULL
);
648 if (Free
->Signature
== POOL_FREE_SIGNATURE
) {
653 while ((Offset
< Granularity
) && (AllFree
)) {
654 Free
= (POOL_FREE
*) &NewPage
[Offset
];
655 ASSERT(Free
!= NULL
);
656 if (Free
->Signature
!= POOL_FREE_SIGNATURE
) {
659 Offset
+= LIST_TO_SIZE(Free
->Index
);
665 // All of the pool entries in the same page as Free are free pool
667 // Remove all of these pool entries from the free loop lists.
669 Free
= (POOL_FREE
*) &NewPage
[0];
670 ASSERT(Free
!= NULL
);
673 while (Offset
< Granularity
) {
674 Free
= (POOL_FREE
*) &NewPage
[Offset
];
675 ASSERT(Free
!= NULL
);
676 RemoveEntryList (&Free
->Link
);
677 Offset
+= LIST_TO_SIZE(Free
->Index
);
683 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
)NewPage
, EFI_SIZE_TO_PAGES (Granularity
));
689 // If this is an OS/OEM specific memory type, then check to see if the last
690 // portion of that memory type has been freed. If it has, then free the
691 // list entry for that memory type
693 if (((UINT32
) Pool
->MemoryType
>= MEMORY_TYPE_OEM_RESERVED_MIN
) && Pool
->Used
== 0) {
694 RemoveEntryList (&Pool
->Link
);
695 CoreFreePoolI (Pool
, NULL
);