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 64, 128, 192, 320, 512, 832, 1344, 2176, 3520, 5696, 9216, 14912, 24128
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
)) {
279 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0), MemoryProfileActionAllocatePool
, PoolType
, Size
, *Buffer
);
280 InstallMemoryAttributesTableOnMemoryAllocation (PoolType
);
286 Internal function to allocate pool of a particular type.
287 Caller must have the memory lock held
289 @param PoolType Type of pool to allocate
290 @param Size The amount of pool to allocate
292 @return The allocate pool, or NULL
297 IN EFI_MEMORY_TYPE PoolType
,
309 UINTN Offset
, MaxOffset
;
313 ASSERT_LOCKED (&gMemoryLock
);
315 if (PoolType
== EfiACPIReclaimMemory
||
316 PoolType
== EfiACPIMemoryNVS
||
317 PoolType
== EfiRuntimeServicesCode
||
318 PoolType
== EfiRuntimeServicesData
) {
320 Granularity
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
322 Granularity
= DEFAULT_PAGE_ALLOCATION
;
326 // Adjust the size by the pool header & tail overhead
330 // Adjusting the Size to be of proper alignment so that
331 // we don't get an unaligned access fault later when
332 // pool_Tail is being initialized
334 Size
= ALIGN_VARIABLE (Size
);
336 Size
+= POOL_OVERHEAD
;
337 Index
= SIZE_TO_LIST(Size
);
338 Pool
= LookupPoolHead (PoolType
);
345 // If allocation is over max size, just allocate pages for the request
348 if (Index
>= SIZE_TO_LIST (Granularity
)) {
349 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (Granularity
) - 1;
350 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (Granularity
) - 1);
351 Head
= CoreAllocatePoolPages (PoolType
, NoPages
, Granularity
);
356 // If there's no free pool in the proper list size, go get some more pages
358 if (IsListEmpty (&Pool
->FreeList
[Index
])) {
360 Offset
= LIST_TO_SIZE (Index
);
361 MaxOffset
= Granularity
;
364 // Check the bins holding larger blocks, and carve one up if needed
366 while (++Index
< SIZE_TO_LIST (Granularity
)) {
367 if (!IsListEmpty (&Pool
->FreeList
[Index
])) {
368 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
369 RemoveEntryList (&Free
->Link
);
370 NewPage
= (VOID
*) Free
;
371 MaxOffset
= LIST_TO_SIZE (Index
);
379 NewPage
= CoreAllocatePoolPages(PoolType
, EFI_SIZE_TO_PAGES (Granularity
), Granularity
);
380 if (NewPage
== NULL
) {
385 // Serve the allocation request from the head of the allocated block
388 Head
= (POOL_HEAD
*) NewPage
;
391 // Carve up remaining space into free pool blocks
394 while (Offset
< MaxOffset
) {
395 ASSERT (Index
< MAX_POOL_LIST
);
396 FSize
= LIST_TO_SIZE(Index
);
398 while (Offset
+ FSize
<= MaxOffset
) {
399 Free
= (POOL_FREE
*) &NewPage
[Offset
];
400 Free
->Signature
= POOL_FREE_SIGNATURE
;
401 Free
->Index
= (UINT32
)Index
;
402 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
408 ASSERT (Offset
== MaxOffset
);
413 // Remove entry from free pool list
415 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
416 RemoveEntryList (&Free
->Link
);
418 Head
= (POOL_HEAD
*) Free
;
426 // If we have a pool buffer, fill in the header & tail info
428 Head
->Signature
= POOL_HEAD_SIGNATURE
;
430 Head
->Type
= (EFI_MEMORY_TYPE
) PoolType
;
431 Tail
= HEAD_TO_TAIL (Head
);
432 Tail
->Signature
= POOL_TAIL_SIGNATURE
;
435 DEBUG_CLEAR_MEMORY (Buffer
, Size
- POOL_OVERHEAD
);
439 "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType
,
441 (UINT64
)(Size
- POOL_OVERHEAD
),
446 // Account the allocation
451 DEBUG ((DEBUG_ERROR
| DEBUG_POOL
, "AllocatePool: failed to allocate %ld bytes\n", (UINT64
) Size
));
462 @param Buffer The allocated pool entry to free
463 @param PoolType Pointer to pool type
465 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
466 @retval EFI_SUCCESS Pool successfully freed.
471 CoreInternalFreePool (
473 OUT EFI_MEMORY_TYPE
*PoolType OPTIONAL
478 if (Buffer
== NULL
) {
479 return EFI_INVALID_PARAMETER
;
482 CoreAcquireMemoryLock ();
483 Status
= CoreFreePoolI (Buffer
, PoolType
);
484 CoreReleaseMemoryLock ();
491 @param Buffer The allocated pool entry to free
493 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
494 @retval EFI_SUCCESS Pool successfully freed.
504 EFI_MEMORY_TYPE PoolType
;
506 Status
= CoreInternalFreePool (Buffer
, &PoolType
);
507 if (!EFI_ERROR (Status
)) {
508 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0), MemoryProfileActionFreePool
, PoolType
, 0, Buffer
);
509 InstallMemoryAttributesTableOnMemoryAllocation (PoolType
);
515 Internal function to free a pool entry.
516 Caller must have the memory lock held
518 @param Buffer The allocated pool entry to free
519 @param PoolType Pointer to pool type
521 @retval EFI_INVALID_PARAMETER Buffer not valid
522 @retval EFI_SUCCESS Buffer successfully freed.
528 OUT EFI_MEMORY_TYPE
*PoolType OPTIONAL
543 ASSERT(Buffer
!= NULL
);
545 // Get the head & tail of the pool entry
547 Head
= CR (Buffer
, POOL_HEAD
, Data
, POOL_HEAD_SIGNATURE
);
548 ASSERT(Head
!= NULL
);
550 if (Head
->Signature
!= POOL_HEAD_SIGNATURE
) {
551 return EFI_INVALID_PARAMETER
;
554 Tail
= HEAD_TO_TAIL (Head
);
555 ASSERT(Tail
!= NULL
);
560 ASSERT (Tail
->Signature
== POOL_TAIL_SIGNATURE
);
561 ASSERT (Head
->Size
== Tail
->Size
);
562 ASSERT_LOCKED (&gMemoryLock
);
564 if (Tail
->Signature
!= POOL_TAIL_SIGNATURE
) {
565 return EFI_INVALID_PARAMETER
;
568 if (Head
->Size
!= Tail
->Size
) {
569 return EFI_INVALID_PARAMETER
;
573 // Determine the pool type and account for it
576 Pool
= LookupPoolHead (Head
->Type
);
578 return EFI_INVALID_PARAMETER
;
581 DEBUG ((DEBUG_POOL
, "FreePool: %p (len %lx) %,ld\n", Head
->Data
, (UINT64
)(Head
->Size
- POOL_OVERHEAD
), (UINT64
) Pool
->Used
));
583 if (Head
->Type
== EfiACPIReclaimMemory
||
584 Head
->Type
== EfiACPIMemoryNVS
||
585 Head
->Type
== EfiRuntimeServicesCode
||
586 Head
->Type
== EfiRuntimeServicesData
) {
588 Granularity
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
590 Granularity
= DEFAULT_PAGE_ALLOCATION
;
593 if (PoolType
!= NULL
) {
594 *PoolType
= Head
->Type
;
598 // Determine the pool list
600 Index
= SIZE_TO_LIST(Size
);
601 DEBUG_CLEAR_MEMORY (Head
, Size
);
604 // If it's not on the list, it must be pool pages
606 if (Index
>= SIZE_TO_LIST (Granularity
)) {
609 // Return the memory pages back to free memory
611 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (Granularity
) - 1;
612 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (Granularity
) - 1);
613 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Head
, NoPages
);
618 // Put the pool entry onto the free pool list
620 Free
= (POOL_FREE
*) Head
;
621 ASSERT(Free
!= NULL
);
622 Free
->Signature
= POOL_FREE_SIGNATURE
;
623 Free
->Index
= (UINT32
)Index
;
624 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
627 // See if all the pool entries in the same page as Free are freed pool
630 NewPage
= (CHAR8
*)((UINTN
)Free
& ~(Granularity
- 1));
631 Free
= (POOL_FREE
*) &NewPage
[0];
632 ASSERT(Free
!= NULL
);
634 if (Free
->Signature
== POOL_FREE_SIGNATURE
) {
639 while ((Offset
< Granularity
) && (AllFree
)) {
640 Free
= (POOL_FREE
*) &NewPage
[Offset
];
641 ASSERT(Free
!= NULL
);
642 if (Free
->Signature
!= POOL_FREE_SIGNATURE
) {
645 Offset
+= LIST_TO_SIZE(Free
->Index
);
651 // All of the pool entries in the same page as Free are free pool
653 // Remove all of these pool entries from the free loop lists.
655 Free
= (POOL_FREE
*) &NewPage
[0];
656 ASSERT(Free
!= NULL
);
659 while (Offset
< Granularity
) {
660 Free
= (POOL_FREE
*) &NewPage
[Offset
];
661 ASSERT(Free
!= NULL
);
662 RemoveEntryList (&Free
->Link
);
663 Offset
+= LIST_TO_SIZE(Free
->Index
);
669 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
)NewPage
, EFI_SIZE_TO_PAGES (Granularity
));
675 // If this is an OS/OEM specific memory type, then check to see if the last
676 // portion of that memory type has been freed. If it has, then free the
677 // list entry for that memory type
679 if (((UINT32
) Pool
->MemoryType
>= MEMORY_TYPE_OEM_RESERVED_MIN
) && Pool
->Used
== 0) {
680 RemoveEntryList (&Pool
->Link
);
681 CoreFreePoolI (Pool
, NULL
);