2 UEFI Memory pool management functions.
4 Copyright (c) 2006 - 2014, 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 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
201 @retval EFI_SUCCESS Pool successfully allocated.
206 CoreInternalAllocatePool (
207 IN EFI_MEMORY_TYPE PoolType
,
215 // If it's not a valid type, fail it
217 if ((PoolType
>= EfiMaxMemoryType
&& PoolType
<= 0x7fffffff) ||
218 PoolType
== EfiConventionalMemory
) {
219 return EFI_INVALID_PARAMETER
;
222 if (Buffer
== NULL
) {
223 return EFI_INVALID_PARAMETER
;
229 // If size is too large, fail it
230 // Base on the EFI spec, return status of EFI_OUT_OF_RESOURCES
232 if (Size
> MAX_POOL_SIZE
) {
233 return EFI_OUT_OF_RESOURCES
;
237 // Acquire the memory lock and make the allocation
239 Status
= CoreAcquireLockOrFail (&gMemoryLock
);
240 if (EFI_ERROR (Status
)) {
241 return EFI_OUT_OF_RESOURCES
;
244 *Buffer
= CoreAllocatePoolI (PoolType
, Size
);
245 CoreReleaseMemoryLock ();
246 return (*Buffer
!= NULL
) ? EFI_SUCCESS
: EFI_OUT_OF_RESOURCES
;
250 Allocate pool of a particular type.
252 @param PoolType Type of pool to allocate
253 @param Size The amount of pool to allocate
254 @param Buffer The address to return a pointer to the allocated
257 @retval EFI_INVALID_PARAMETER PoolType not valid or Buffer is NULL.
258 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
259 @retval EFI_SUCCESS Pool successfully allocated.
265 IN EFI_MEMORY_TYPE PoolType
,
272 Status
= CoreInternalAllocatePool (PoolType
, Size
, Buffer
);
273 if (!EFI_ERROR (Status
)) {
274 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0), MemoryProfileActionAllocatePool
, PoolType
, Size
, *Buffer
);
280 Internal function to allocate pool of a particular type.
281 Caller must have the memory lock held
283 @param PoolType Type of pool to allocate
284 @param Size The amount of pool to allocate
286 @return The allocate pool, or NULL
291 IN EFI_MEMORY_TYPE PoolType
,
303 UINTN Offset
, MaxOffset
;
307 ASSERT_LOCKED (&gMemoryLock
);
309 if (PoolType
== EfiACPIReclaimMemory
||
310 PoolType
== EfiACPIMemoryNVS
||
311 PoolType
== EfiRuntimeServicesCode
||
312 PoolType
== EfiRuntimeServicesData
) {
314 Granularity
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
316 Granularity
= DEFAULT_PAGE_ALLOCATION
;
320 // Adjust the size by the pool header & tail overhead
324 // Adjusting the Size to be of proper alignment so that
325 // we don't get an unaligned access fault later when
326 // pool_Tail is being initialized
328 Size
= ALIGN_VARIABLE (Size
);
330 Size
+= POOL_OVERHEAD
;
331 Index
= SIZE_TO_LIST(Size
);
332 Pool
= LookupPoolHead (PoolType
);
339 // If allocation is over max size, just allocate pages for the request
342 if (Index
>= SIZE_TO_LIST (Granularity
)) {
343 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (Granularity
) - 1;
344 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (Granularity
) - 1);
345 Head
= CoreAllocatePoolPages (PoolType
, NoPages
, Granularity
);
350 // If there's no free pool in the proper list size, go get some more pages
352 if (IsListEmpty (&Pool
->FreeList
[Index
])) {
354 Offset
= LIST_TO_SIZE (Index
);
355 MaxOffset
= Granularity
;
358 // Check the bins holding larger blocks, and carve one up if needed
360 while (++Index
< SIZE_TO_LIST (Granularity
)) {
361 if (!IsListEmpty (&Pool
->FreeList
[Index
])) {
362 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
363 RemoveEntryList (&Free
->Link
);
364 NewPage
= (VOID
*) Free
;
365 MaxOffset
= LIST_TO_SIZE (Index
);
373 NewPage
= CoreAllocatePoolPages(PoolType
, EFI_SIZE_TO_PAGES (Granularity
), Granularity
);
374 if (NewPage
== NULL
) {
379 // Serve the allocation request from the head of the allocated block
382 Head
= (POOL_HEAD
*) NewPage
;
385 // Carve up remaining space into free pool blocks
388 while (Offset
< MaxOffset
) {
389 ASSERT (Index
< MAX_POOL_LIST
);
390 FSize
= LIST_TO_SIZE(Index
);
392 while (Offset
+ FSize
<= MaxOffset
) {
393 Free
= (POOL_FREE
*) &NewPage
[Offset
];
394 Free
->Signature
= POOL_FREE_SIGNATURE
;
395 Free
->Index
= (UINT32
)Index
;
396 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
402 ASSERT (Offset
== MaxOffset
);
407 // Remove entry from free pool list
409 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
410 RemoveEntryList (&Free
->Link
);
412 Head
= (POOL_HEAD
*) Free
;
420 // If we have a pool buffer, fill in the header & tail info
422 Head
->Signature
= POOL_HEAD_SIGNATURE
;
424 Head
->Type
= (EFI_MEMORY_TYPE
) PoolType
;
425 Tail
= HEAD_TO_TAIL (Head
);
426 Tail
->Signature
= POOL_TAIL_SIGNATURE
;
429 DEBUG_CLEAR_MEMORY (Buffer
, Size
- POOL_OVERHEAD
);
433 "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType
,
435 (UINT64
)(Size
- POOL_OVERHEAD
),
440 // Account the allocation
445 DEBUG ((DEBUG_ERROR
| DEBUG_POOL
, "AllocatePool: failed to allocate %ld bytes\n", (UINT64
) Size
));
456 @param Buffer The allocated pool entry to free
458 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
459 @retval EFI_SUCCESS Pool successfully freed.
464 CoreInternalFreePool (
470 if (Buffer
== NULL
) {
471 return EFI_INVALID_PARAMETER
;
474 CoreAcquireMemoryLock ();
475 Status
= CoreFreePoolI (Buffer
);
476 CoreReleaseMemoryLock ();
483 @param Buffer The allocated pool entry to free
485 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
486 @retval EFI_SUCCESS Pool successfully freed.
497 Status
= CoreInternalFreePool (Buffer
);
498 if (!EFI_ERROR (Status
)) {
499 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0), MemoryProfileActionFreePool
, (EFI_MEMORY_TYPE
) 0, 0, Buffer
);
505 Internal function to free a pool entry.
506 Caller must have the memory lock held
508 @param Buffer The allocated pool entry to free
510 @retval EFI_INVALID_PARAMETER Buffer not valid
511 @retval EFI_SUCCESS Buffer successfully freed.
531 ASSERT(Buffer
!= NULL
);
533 // Get the head & tail of the pool entry
535 Head
= CR (Buffer
, POOL_HEAD
, Data
, POOL_HEAD_SIGNATURE
);
536 ASSERT(Head
!= NULL
);
538 if (Head
->Signature
!= POOL_HEAD_SIGNATURE
) {
539 return EFI_INVALID_PARAMETER
;
542 Tail
= HEAD_TO_TAIL (Head
);
543 ASSERT(Tail
!= NULL
);
548 ASSERT (Tail
->Signature
== POOL_TAIL_SIGNATURE
);
549 ASSERT (Head
->Size
== Tail
->Size
);
550 ASSERT_LOCKED (&gMemoryLock
);
552 if (Tail
->Signature
!= POOL_TAIL_SIGNATURE
) {
553 return EFI_INVALID_PARAMETER
;
556 if (Head
->Size
!= Tail
->Size
) {
557 return EFI_INVALID_PARAMETER
;
561 // Determine the pool type and account for it
564 Pool
= LookupPoolHead (Head
->Type
);
566 return EFI_INVALID_PARAMETER
;
569 DEBUG ((DEBUG_POOL
, "FreePool: %p (len %lx) %,ld\n", Head
->Data
, (UINT64
)(Head
->Size
- POOL_OVERHEAD
), (UINT64
) Pool
->Used
));
571 if (Head
->Type
== EfiACPIReclaimMemory
||
572 Head
->Type
== EfiACPIMemoryNVS
||
573 Head
->Type
== EfiRuntimeServicesCode
||
574 Head
->Type
== EfiRuntimeServicesData
) {
576 Granularity
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
578 Granularity
= DEFAULT_PAGE_ALLOCATION
;
582 // Determine the pool list
584 Index
= SIZE_TO_LIST(Size
);
585 DEBUG_CLEAR_MEMORY (Head
, Size
);
588 // If it's not on the list, it must be pool pages
590 if (Index
>= SIZE_TO_LIST (Granularity
)) {
593 // Return the memory pages back to free memory
595 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (Granularity
) - 1;
596 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (Granularity
) - 1);
597 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Head
, NoPages
);
602 // Put the pool entry onto the free pool list
604 Free
= (POOL_FREE
*) Head
;
605 ASSERT(Free
!= NULL
);
606 Free
->Signature
= POOL_FREE_SIGNATURE
;
607 Free
->Index
= (UINT32
)Index
;
608 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
611 // See if all the pool entries in the same page as Free are freed pool
614 NewPage
= (CHAR8
*)((UINTN
)Free
& ~(Granularity
- 1));
615 Free
= (POOL_FREE
*) &NewPage
[0];
616 ASSERT(Free
!= NULL
);
618 if (Free
->Signature
== POOL_FREE_SIGNATURE
) {
623 while ((Offset
< Granularity
) && (AllFree
)) {
624 Free
= (POOL_FREE
*) &NewPage
[Offset
];
625 ASSERT(Free
!= NULL
);
626 if (Free
->Signature
!= POOL_FREE_SIGNATURE
) {
629 Offset
+= LIST_TO_SIZE(Free
->Index
);
635 // All of the pool entries in the same page as Free are free pool
637 // Remove all of these pool entries from the free loop lists.
639 Free
= (POOL_FREE
*) &NewPage
[0];
640 ASSERT(Free
!= NULL
);
643 while (Offset
< Granularity
) {
644 Free
= (POOL_FREE
*) &NewPage
[Offset
];
645 ASSERT(Free
!= NULL
);
646 RemoveEntryList (&Free
->Link
);
647 Offset
+= LIST_TO_SIZE(Free
->Index
);
653 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
)NewPage
, EFI_SIZE_TO_PAGES (Granularity
));
659 // If this is an OS specific memory type, then check to see if the last
660 // portion of that memory type has been freed. If it has, then free the
661 // list entry for that memory type
663 if ((INT32
)Pool
->MemoryType
< 0 && Pool
->Used
== 0) {
664 RemoveEntryList (&Pool
->Link
);
665 CoreFreePoolI (Pool
);