2 UEFI Memory pool management functions.
4 Copyright (c) 2006 - 2015, 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
160 if ((INT32
)MemoryType
< 0) {
162 for (Link
= mPoolHeadList
.ForwardLink
; Link
!= &mPoolHeadList
; Link
= Link
->ForwardLink
) {
163 Pool
= CR(Link
, POOL
, Link
, POOL_SIGNATURE
);
164 if (Pool
->MemoryType
== MemoryType
) {
169 Pool
= CoreAllocatePoolI (EfiBootServicesData
, sizeof (POOL
));
174 Pool
->Signature
= POOL_SIGNATURE
;
176 Pool
->MemoryType
= MemoryType
;
177 for (Index
=0; Index
< MAX_POOL_LIST
; Index
++) {
178 InitializeListHead (&Pool
->FreeList
[Index
]);
181 InsertHeadList (&mPoolHeadList
, &Pool
->Link
);
192 Allocate pool of a particular type.
194 @param PoolType Type of pool to allocate
195 @param Size The amount of pool to allocate
196 @param Buffer The address to return a pointer to the allocated
199 @retval EFI_INVALID_PARAMETER PoolType not valid or Buffer is NULL.
200 PoolType was 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
,
216 // If it's not a valid type, fail it
218 if ((PoolType
>= EfiMaxMemoryType
&& PoolType
<= 0x7fffffff) ||
219 (PoolType
== EfiConventionalMemory
) || (PoolType
== EfiPersistentMemory
)) {
220 return EFI_INVALID_PARAMETER
;
223 if (Buffer
== NULL
) {
224 return EFI_INVALID_PARAMETER
;
230 // If size is too large, fail it
231 // Base on the EFI spec, return status of EFI_OUT_OF_RESOURCES
233 if (Size
> MAX_POOL_SIZE
) {
234 return EFI_OUT_OF_RESOURCES
;
238 // Acquire the memory lock and make the allocation
240 Status
= CoreAcquireLockOrFail (&gMemoryLock
);
241 if (EFI_ERROR (Status
)) {
242 return EFI_OUT_OF_RESOURCES
;
245 *Buffer
= CoreAllocatePoolI (PoolType
, Size
);
246 CoreReleaseMemoryLock ();
247 return (*Buffer
!= NULL
) ? EFI_SUCCESS
: EFI_OUT_OF_RESOURCES
;
251 Allocate pool of a particular type.
253 @param PoolType Type of pool to allocate
254 @param Size The amount of pool to allocate
255 @param Buffer The address to return a pointer to the allocated
258 @retval EFI_INVALID_PARAMETER PoolType not valid or Buffer is NULL.
259 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
260 @retval EFI_SUCCESS Pool successfully allocated.
266 IN EFI_MEMORY_TYPE PoolType
,
273 Status
= CoreInternalAllocatePool (PoolType
, Size
, Buffer
);
274 if (!EFI_ERROR (Status
)) {
275 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0), MemoryProfileActionAllocatePool
, PoolType
, Size
, *Buffer
);
281 Internal function to allocate pool of a particular type.
282 Caller must have the memory lock held
284 @param PoolType Type of pool to allocate
285 @param Size The amount of pool to allocate
287 @return The allocate pool, or NULL
292 IN EFI_MEMORY_TYPE PoolType
,
304 UINTN Offset
, MaxOffset
;
308 ASSERT_LOCKED (&gMemoryLock
);
310 if (PoolType
== EfiACPIReclaimMemory
||
311 PoolType
== EfiACPIMemoryNVS
||
312 PoolType
== EfiRuntimeServicesCode
||
313 PoolType
== EfiRuntimeServicesData
) {
315 Granularity
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
317 Granularity
= DEFAULT_PAGE_ALLOCATION
;
321 // Adjust the size by the pool header & tail overhead
325 // Adjusting the Size to be of proper alignment so that
326 // we don't get an unaligned access fault later when
327 // pool_Tail is being initialized
329 Size
= ALIGN_VARIABLE (Size
);
331 Size
+= POOL_OVERHEAD
;
332 Index
= SIZE_TO_LIST(Size
);
333 Pool
= LookupPoolHead (PoolType
);
340 // If allocation is over max size, just allocate pages for the request
343 if (Index
>= SIZE_TO_LIST (Granularity
)) {
344 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (Granularity
) - 1;
345 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (Granularity
) - 1);
346 Head
= CoreAllocatePoolPages (PoolType
, NoPages
, Granularity
);
351 // If there's no free pool in the proper list size, go get some more pages
353 if (IsListEmpty (&Pool
->FreeList
[Index
])) {
355 Offset
= LIST_TO_SIZE (Index
);
356 MaxOffset
= Granularity
;
359 // Check the bins holding larger blocks, and carve one up if needed
361 while (++Index
< SIZE_TO_LIST (Granularity
)) {
362 if (!IsListEmpty (&Pool
->FreeList
[Index
])) {
363 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
364 RemoveEntryList (&Free
->Link
);
365 NewPage
= (VOID
*) Free
;
366 MaxOffset
= LIST_TO_SIZE (Index
);
374 NewPage
= CoreAllocatePoolPages(PoolType
, EFI_SIZE_TO_PAGES (Granularity
), Granularity
);
375 if (NewPage
== NULL
) {
380 // Serve the allocation request from the head of the allocated block
383 Head
= (POOL_HEAD
*) NewPage
;
386 // Carve up remaining space into free pool blocks
389 while (Offset
< MaxOffset
) {
390 ASSERT (Index
< MAX_POOL_LIST
);
391 FSize
= LIST_TO_SIZE(Index
);
393 while (Offset
+ FSize
<= MaxOffset
) {
394 Free
= (POOL_FREE
*) &NewPage
[Offset
];
395 Free
->Signature
= POOL_FREE_SIGNATURE
;
396 Free
->Index
= (UINT32
)Index
;
397 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
403 ASSERT (Offset
== MaxOffset
);
408 // Remove entry from free pool list
410 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
411 RemoveEntryList (&Free
->Link
);
413 Head
= (POOL_HEAD
*) Free
;
421 // If we have a pool buffer, fill in the header & tail info
423 Head
->Signature
= POOL_HEAD_SIGNATURE
;
425 Head
->Type
= (EFI_MEMORY_TYPE
) PoolType
;
426 Tail
= HEAD_TO_TAIL (Head
);
427 Tail
->Signature
= POOL_TAIL_SIGNATURE
;
430 DEBUG_CLEAR_MEMORY (Buffer
, Size
- POOL_OVERHEAD
);
434 "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType
,
436 (UINT64
)(Size
- POOL_OVERHEAD
),
441 // Account the allocation
446 DEBUG ((DEBUG_ERROR
| DEBUG_POOL
, "AllocatePool: failed to allocate %ld bytes\n", (UINT64
) Size
));
457 @param Buffer The allocated pool entry to free
459 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
460 @retval EFI_SUCCESS Pool successfully freed.
465 CoreInternalFreePool (
471 if (Buffer
== NULL
) {
472 return EFI_INVALID_PARAMETER
;
475 CoreAcquireMemoryLock ();
476 Status
= CoreFreePoolI (Buffer
);
477 CoreReleaseMemoryLock ();
484 @param Buffer The allocated pool entry to free
486 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
487 @retval EFI_SUCCESS Pool successfully freed.
498 Status
= CoreInternalFreePool (Buffer
);
499 if (!EFI_ERROR (Status
)) {
500 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0), MemoryProfileActionFreePool
, (EFI_MEMORY_TYPE
) 0, 0, Buffer
);
506 Internal function to free a pool entry.
507 Caller must have the memory lock held
509 @param Buffer The allocated pool entry to free
511 @retval EFI_INVALID_PARAMETER Buffer not valid
512 @retval EFI_SUCCESS Buffer successfully freed.
532 ASSERT(Buffer
!= NULL
);
534 // Get the head & tail of the pool entry
536 Head
= CR (Buffer
, POOL_HEAD
, Data
, POOL_HEAD_SIGNATURE
);
537 ASSERT(Head
!= NULL
);
539 if (Head
->Signature
!= POOL_HEAD_SIGNATURE
) {
540 return EFI_INVALID_PARAMETER
;
543 Tail
= HEAD_TO_TAIL (Head
);
544 ASSERT(Tail
!= NULL
);
549 ASSERT (Tail
->Signature
== POOL_TAIL_SIGNATURE
);
550 ASSERT (Head
->Size
== Tail
->Size
);
551 ASSERT_LOCKED (&gMemoryLock
);
553 if (Tail
->Signature
!= POOL_TAIL_SIGNATURE
) {
554 return EFI_INVALID_PARAMETER
;
557 if (Head
->Size
!= Tail
->Size
) {
558 return EFI_INVALID_PARAMETER
;
562 // Determine the pool type and account for it
565 Pool
= LookupPoolHead (Head
->Type
);
567 return EFI_INVALID_PARAMETER
;
570 DEBUG ((DEBUG_POOL
, "FreePool: %p (len %lx) %,ld\n", Head
->Data
, (UINT64
)(Head
->Size
- POOL_OVERHEAD
), (UINT64
) Pool
->Used
));
572 if (Head
->Type
== EfiACPIReclaimMemory
||
573 Head
->Type
== EfiACPIMemoryNVS
||
574 Head
->Type
== EfiRuntimeServicesCode
||
575 Head
->Type
== EfiRuntimeServicesData
) {
577 Granularity
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
579 Granularity
= DEFAULT_PAGE_ALLOCATION
;
583 // Determine the pool list
585 Index
= SIZE_TO_LIST(Size
);
586 DEBUG_CLEAR_MEMORY (Head
, Size
);
589 // If it's not on the list, it must be pool pages
591 if (Index
>= SIZE_TO_LIST (Granularity
)) {
594 // Return the memory pages back to free memory
596 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (Granularity
) - 1;
597 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (Granularity
) - 1);
598 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Head
, NoPages
);
603 // Put the pool entry onto the free pool list
605 Free
= (POOL_FREE
*) Head
;
606 ASSERT(Free
!= NULL
);
607 Free
->Signature
= POOL_FREE_SIGNATURE
;
608 Free
->Index
= (UINT32
)Index
;
609 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
612 // See if all the pool entries in the same page as Free are freed pool
615 NewPage
= (CHAR8
*)((UINTN
)Free
& ~(Granularity
- 1));
616 Free
= (POOL_FREE
*) &NewPage
[0];
617 ASSERT(Free
!= NULL
);
619 if (Free
->Signature
== POOL_FREE_SIGNATURE
) {
624 while ((Offset
< Granularity
) && (AllFree
)) {
625 Free
= (POOL_FREE
*) &NewPage
[Offset
];
626 ASSERT(Free
!= NULL
);
627 if (Free
->Signature
!= POOL_FREE_SIGNATURE
) {
630 Offset
+= LIST_TO_SIZE(Free
->Index
);
636 // All of the pool entries in the same page as Free are free pool
638 // Remove all of these pool entries from the free loop lists.
640 Free
= (POOL_FREE
*) &NewPage
[0];
641 ASSERT(Free
!= NULL
);
644 while (Offset
< Granularity
) {
645 Free
= (POOL_FREE
*) &NewPage
[Offset
];
646 ASSERT(Free
!= NULL
);
647 RemoveEntryList (&Free
->Link
);
648 Offset
+= LIST_TO_SIZE(Free
->Index
);
654 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
)NewPage
, EFI_SIZE_TO_PAGES (Granularity
));
660 // If this is an OS specific memory type, then check to see if the last
661 // portion of that memory type has been freed. If it has, then free the
662 // list entry for that memory type
664 if ((INT32
)Pool
->MemoryType
< 0 && Pool
->Used
== 0) {
665 RemoveEntryList (&Pool
->Link
);
666 CoreFreePoolI (Pool
);