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 ();
313 if (Buffer
!= NULL
) {
314 ApplyMemoryProtectionPolicy (EfiConventionalMemory
, PoolType
,
315 (EFI_PHYSICAL_ADDRESS
)(UINTN
)Buffer
, EFI_PAGES_TO_SIZE (NoPages
));
321 Internal function to allocate pool of a particular type.
322 Caller must have the memory lock held
324 @param PoolType Type of pool to allocate
325 @param Size The amount of pool to allocate
327 @return The allocate pool, or NULL
332 IN EFI_MEMORY_TYPE PoolType
,
344 UINTN Offset
, MaxOffset
;
348 ASSERT_LOCKED (&mPoolMemoryLock
);
350 if (PoolType
== EfiACPIReclaimMemory
||
351 PoolType
== EfiACPIMemoryNVS
||
352 PoolType
== EfiRuntimeServicesCode
||
353 PoolType
== EfiRuntimeServicesData
) {
355 Granularity
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
357 Granularity
= DEFAULT_PAGE_ALLOCATION
;
361 // Adjust the size by the pool header & tail overhead
365 // Adjusting the Size to be of proper alignment so that
366 // we don't get an unaligned access fault later when
367 // pool_Tail is being initialized
369 Size
= ALIGN_VARIABLE (Size
);
371 Size
+= POOL_OVERHEAD
;
372 Index
= SIZE_TO_LIST(Size
);
373 Pool
= LookupPoolHead (PoolType
);
380 // If allocation is over max size, just allocate pages for the request
383 if (Index
>= SIZE_TO_LIST (Granularity
)) {
384 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (Granularity
) - 1;
385 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (Granularity
) - 1);
386 Head
= CoreAllocatePoolPagesI (PoolType
, NoPages
, Granularity
);
391 // If there's no free pool in the proper list size, go get some more pages
393 if (IsListEmpty (&Pool
->FreeList
[Index
])) {
395 Offset
= LIST_TO_SIZE (Index
);
396 MaxOffset
= Granularity
;
399 // Check the bins holding larger blocks, and carve one up if needed
401 while (++Index
< SIZE_TO_LIST (Granularity
)) {
402 if (!IsListEmpty (&Pool
->FreeList
[Index
])) {
403 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
404 RemoveEntryList (&Free
->Link
);
405 NewPage
= (VOID
*) Free
;
406 MaxOffset
= LIST_TO_SIZE (Index
);
414 NewPage
= CoreAllocatePoolPagesI (PoolType
, EFI_SIZE_TO_PAGES (Granularity
), Granularity
);
415 if (NewPage
== NULL
) {
420 // Serve the allocation request from the head of the allocated block
423 Head
= (POOL_HEAD
*) NewPage
;
426 // Carve up remaining space into free pool blocks
429 while (Offset
< MaxOffset
) {
430 ASSERT (Index
< MAX_POOL_LIST
);
431 FSize
= LIST_TO_SIZE(Index
);
433 while (Offset
+ FSize
<= MaxOffset
) {
434 Free
= (POOL_FREE
*) &NewPage
[Offset
];
435 Free
->Signature
= POOL_FREE_SIGNATURE
;
436 Free
->Index
= (UINT32
)Index
;
437 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
443 ASSERT (Offset
== MaxOffset
);
448 // Remove entry from free pool list
450 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
451 RemoveEntryList (&Free
->Link
);
453 Head
= (POOL_HEAD
*) Free
;
461 // If we have a pool buffer, fill in the header & tail info
463 Head
->Signature
= POOL_HEAD_SIGNATURE
;
465 Head
->Type
= (EFI_MEMORY_TYPE
) PoolType
;
466 Tail
= HEAD_TO_TAIL (Head
);
467 Tail
->Signature
= POOL_TAIL_SIGNATURE
;
470 DEBUG_CLEAR_MEMORY (Buffer
, Size
- POOL_OVERHEAD
);
474 "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType
,
476 (UINT64
)(Size
- POOL_OVERHEAD
),
481 // Account the allocation
486 DEBUG ((DEBUG_ERROR
| DEBUG_POOL
, "AllocatePool: failed to allocate %ld bytes\n", (UINT64
) Size
));
497 @param Buffer The allocated pool entry to free
498 @param PoolType Pointer to pool type
500 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
501 @retval EFI_SUCCESS Pool successfully freed.
506 CoreInternalFreePool (
508 OUT EFI_MEMORY_TYPE
*PoolType OPTIONAL
513 if (Buffer
== NULL
) {
514 return EFI_INVALID_PARAMETER
;
517 CoreAcquireLock (&mPoolMemoryLock
);
518 Status
= CoreFreePoolI (Buffer
, PoolType
);
519 CoreReleaseLock (&mPoolMemoryLock
);
526 @param Buffer The allocated pool entry to free
528 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
529 @retval EFI_SUCCESS Pool successfully freed.
539 EFI_MEMORY_TYPE PoolType
;
541 Status
= CoreInternalFreePool (Buffer
, &PoolType
);
542 if (!EFI_ERROR (Status
)) {
544 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
545 MemoryProfileActionFreePool
,
551 InstallMemoryAttributesTableOnMemoryAllocation (PoolType
);
559 IN EFI_MEMORY_TYPE PoolType
,
560 IN EFI_PHYSICAL_ADDRESS Memory
,
564 CoreAcquireMemoryLock ();
565 CoreFreePoolPages (Memory
, NoPages
);
566 CoreReleaseMemoryLock ();
568 ApplyMemoryProtectionPolicy (PoolType
, EfiConventionalMemory
,
569 (EFI_PHYSICAL_ADDRESS
)(UINTN
)Memory
, EFI_PAGES_TO_SIZE (NoPages
));
573 Internal function to free a pool entry.
574 Caller must have the memory lock held
576 @param Buffer The allocated pool entry to free
577 @param PoolType Pointer to pool type
579 @retval EFI_INVALID_PARAMETER Buffer not valid
580 @retval EFI_SUCCESS Buffer successfully freed.
586 OUT EFI_MEMORY_TYPE
*PoolType OPTIONAL
601 ASSERT(Buffer
!= NULL
);
603 // Get the head & tail of the pool entry
605 Head
= CR (Buffer
, POOL_HEAD
, Data
, POOL_HEAD_SIGNATURE
);
606 ASSERT(Head
!= NULL
);
608 if (Head
->Signature
!= POOL_HEAD_SIGNATURE
) {
609 return EFI_INVALID_PARAMETER
;
612 Tail
= HEAD_TO_TAIL (Head
);
613 ASSERT(Tail
!= NULL
);
618 ASSERT (Tail
->Signature
== POOL_TAIL_SIGNATURE
);
619 ASSERT (Head
->Size
== Tail
->Size
);
620 ASSERT_LOCKED (&mPoolMemoryLock
);
622 if (Tail
->Signature
!= POOL_TAIL_SIGNATURE
) {
623 return EFI_INVALID_PARAMETER
;
626 if (Head
->Size
!= Tail
->Size
) {
627 return EFI_INVALID_PARAMETER
;
631 // Determine the pool type and account for it
634 Pool
= LookupPoolHead (Head
->Type
);
636 return EFI_INVALID_PARAMETER
;
639 DEBUG ((DEBUG_POOL
, "FreePool: %p (len %lx) %,ld\n", Head
->Data
, (UINT64
)(Head
->Size
- POOL_OVERHEAD
), (UINT64
) Pool
->Used
));
641 if (Head
->Type
== EfiACPIReclaimMemory
||
642 Head
->Type
== EfiACPIMemoryNVS
||
643 Head
->Type
== EfiRuntimeServicesCode
||
644 Head
->Type
== EfiRuntimeServicesData
) {
646 Granularity
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
648 Granularity
= DEFAULT_PAGE_ALLOCATION
;
651 if (PoolType
!= NULL
) {
652 *PoolType
= Head
->Type
;
656 // Determine the pool list
658 Index
= SIZE_TO_LIST(Size
);
659 DEBUG_CLEAR_MEMORY (Head
, Size
);
662 // If it's not on the list, it must be pool pages
664 if (Index
>= SIZE_TO_LIST (Granularity
)) {
667 // Return the memory pages back to free memory
669 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (Granularity
) - 1;
670 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (Granularity
) - 1);
671 CoreFreePoolPagesI (Pool
->MemoryType
, (EFI_PHYSICAL_ADDRESS
) (UINTN
) Head
, NoPages
);
676 // Put the pool entry onto the free pool list
678 Free
= (POOL_FREE
*) Head
;
679 ASSERT(Free
!= NULL
);
680 Free
->Signature
= POOL_FREE_SIGNATURE
;
681 Free
->Index
= (UINT32
)Index
;
682 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
685 // See if all the pool entries in the same page as Free are freed pool
688 NewPage
= (CHAR8
*)((UINTN
)Free
& ~(Granularity
- 1));
689 Free
= (POOL_FREE
*) &NewPage
[0];
690 ASSERT(Free
!= NULL
);
692 if (Free
->Signature
== POOL_FREE_SIGNATURE
) {
697 while ((Offset
< Granularity
) && (AllFree
)) {
698 Free
= (POOL_FREE
*) &NewPage
[Offset
];
699 ASSERT(Free
!= NULL
);
700 if (Free
->Signature
!= POOL_FREE_SIGNATURE
) {
703 Offset
+= LIST_TO_SIZE(Free
->Index
);
709 // All of the pool entries in the same page as Free are free pool
711 // Remove all of these pool entries from the free loop lists.
713 Free
= (POOL_FREE
*) &NewPage
[0];
714 ASSERT(Free
!= NULL
);
717 while (Offset
< Granularity
) {
718 Free
= (POOL_FREE
*) &NewPage
[Offset
];
719 ASSERT(Free
!= NULL
);
720 RemoveEntryList (&Free
->Link
);
721 Offset
+= LIST_TO_SIZE(Free
->Index
);
727 CoreFreePoolPagesI (Pool
->MemoryType
, (EFI_PHYSICAL_ADDRESS
) (UINTN
)NewPage
,
728 EFI_SIZE_TO_PAGES (Granularity
));
734 // If this is an OS/OEM specific memory type, then check to see if the last
735 // portion of that memory type has been freed. If it has, then free the
736 // list entry for that memory type
738 if (((UINT32
) Pool
->MemoryType
>= MEMORY_TYPE_OEM_RESERVED_MIN
) && Pool
->Used
== 0) {
739 RemoveEntryList (&Pool
->Link
);
740 CoreFreePoolI (Pool
, NULL
);