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
);
90 GetPoolIndexFromSize (
96 for (Index
= 0; Index
< MAX_POOL_LIST
; Index
++) {
97 if (mPoolSizeTable
[Index
] >= Size
) {
101 return MAX_POOL_LIST
;
105 Called to initialize the pool.
116 for (Type
=0; Type
< EfiMaxMemoryType
; Type
++) {
117 mPoolHead
[Type
].Signature
= 0;
118 mPoolHead
[Type
].Used
= 0;
119 mPoolHead
[Type
].MemoryType
= (EFI_MEMORY_TYPE
) Type
;
120 for (Index
=0; Index
< MAX_POOL_LIST
; Index
++) {
121 InitializeListHead (&mPoolHead
[Type
].FreeList
[Index
]);
128 Look up pool head for specified memory type.
130 @param MemoryType Memory type of which pool head is looked for
132 @return Pointer of Corresponding pool head.
137 IN EFI_MEMORY_TYPE MemoryType
144 if ((UINT32
)MemoryType
< EfiMaxMemoryType
) {
145 return &mPoolHead
[MemoryType
];
149 // MemoryType values in the range 0x80000000..0xFFFFFFFF are reserved for use by UEFI
150 // OS loaders that are provided by operating system vendors
152 if ((INT32
)MemoryType
< 0) {
154 for (Link
= mPoolHeadList
.ForwardLink
; Link
!= &mPoolHeadList
; Link
= Link
->ForwardLink
) {
155 Pool
= CR(Link
, POOL
, Link
, POOL_SIGNATURE
);
156 if (Pool
->MemoryType
== MemoryType
) {
161 Pool
= CoreAllocatePoolI (EfiBootServicesData
, sizeof (POOL
));
166 Pool
->Signature
= POOL_SIGNATURE
;
168 Pool
->MemoryType
= MemoryType
;
169 for (Index
=0; Index
< MAX_POOL_LIST
; Index
++) {
170 InitializeListHead (&Pool
->FreeList
[Index
]);
173 InsertHeadList (&mPoolHeadList
, &Pool
->Link
);
184 Allocate pool of a particular type.
186 @param PoolType Type of pool to allocate
187 @param Size The amount of pool to allocate
188 @param Buffer The address to return a pointer to the allocated
191 @retval EFI_INVALID_PARAMETER PoolType not valid or Buffer is NULL.
192 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
193 @retval EFI_SUCCESS Pool successfully allocated.
198 CoreInternalAllocatePool (
199 IN EFI_MEMORY_TYPE PoolType
,
207 // If it's not a valid type, fail it
209 if ((PoolType
>= EfiMaxMemoryType
&& PoolType
<= 0x7fffffff) ||
210 PoolType
== EfiConventionalMemory
) {
211 return EFI_INVALID_PARAMETER
;
214 if (Buffer
== NULL
) {
215 return EFI_INVALID_PARAMETER
;
221 // If size is too large, fail it
222 // Base on the EFI spec, return status of EFI_OUT_OF_RESOURCES
224 if (Size
> MAX_POOL_SIZE
) {
225 return EFI_OUT_OF_RESOURCES
;
229 // Acquire the memory lock and make the allocation
231 Status
= CoreAcquireLockOrFail (&gMemoryLock
);
232 if (EFI_ERROR (Status
)) {
233 return EFI_OUT_OF_RESOURCES
;
236 *Buffer
= CoreAllocatePoolI (PoolType
, Size
);
237 CoreReleaseMemoryLock ();
238 return (*Buffer
!= NULL
) ? EFI_SUCCESS
: EFI_OUT_OF_RESOURCES
;
242 Allocate pool of a particular type.
244 @param PoolType Type of pool to allocate
245 @param Size The amount of pool to allocate
246 @param Buffer The address to return a pointer to the allocated
249 @retval EFI_INVALID_PARAMETER PoolType not valid or Buffer is NULL.
250 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
251 @retval EFI_SUCCESS Pool successfully allocated.
257 IN EFI_MEMORY_TYPE PoolType
,
264 Status
= CoreInternalAllocatePool (PoolType
, Size
, Buffer
);
265 if (!EFI_ERROR (Status
)) {
266 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0), MemoryProfileActionAllocatePool
, PoolType
, Size
, *Buffer
);
272 Internal function to allocate pool of a particular type.
273 Caller must have the memory lock held
275 @param PoolType Type of pool to allocate
276 @param Size The amount of pool to allocate
278 @return The allocate pool, or NULL
283 IN EFI_MEMORY_TYPE PoolType
,
299 ASSERT_LOCKED (&gMemoryLock
);
301 if (PoolType
== EfiACPIReclaimMemory
||
302 PoolType
== EfiACPIMemoryNVS
||
303 PoolType
== EfiRuntimeServicesCode
||
304 PoolType
== EfiRuntimeServicesData
) {
306 Granularity
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
308 Granularity
= DEFAULT_PAGE_ALLOCATION
;
312 // Adjust the size by the pool header & tail overhead
316 // Adjusting the Size to be of proper alignment so that
317 // we don't get an unaligned access fault later when
318 // pool_Tail is being initialized
320 Size
= ALIGN_VARIABLE (Size
);
322 Size
+= POOL_OVERHEAD
;
323 Index
= SIZE_TO_LIST(Size
);
324 Pool
= LookupPoolHead (PoolType
);
331 // If allocation is over max size, just allocate pages for the request
334 if (Index
>= SIZE_TO_LIST (Granularity
)) {
335 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (Granularity
) - 1;
336 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (Granularity
) - 1);
337 Head
= CoreAllocatePoolPages (PoolType
, NoPages
, Granularity
);
342 // If there's no free pool in the proper list size, go get some more pages
344 if (IsListEmpty (&Pool
->FreeList
[Index
])) {
349 NewPage
= CoreAllocatePoolPages(PoolType
, EFI_SIZE_TO_PAGES (Granularity
), Granularity
);
350 if (NewPage
== NULL
) {
355 // Carve up new page into free pool blocks
358 while (Offset
< Granularity
) {
359 ASSERT (Index
< MAX_POOL_LIST
);
360 FSize
= LIST_TO_SIZE(Index
);
362 while (Offset
+ FSize
<= Granularity
) {
363 Free
= (POOL_FREE
*) &NewPage
[Offset
];
364 Free
->Signature
= POOL_FREE_SIGNATURE
;
365 Free
->Index
= (UINT32
)Index
;
366 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
373 ASSERT (Offset
== Granularity
);
374 Index
= SIZE_TO_LIST(Size
);
378 // Remove entry from free pool list
380 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
381 RemoveEntryList (&Free
->Link
);
383 Head
= (POOL_HEAD
*) Free
;
391 // If we have a pool buffer, fill in the header & tail info
393 Head
->Signature
= POOL_HEAD_SIGNATURE
;
395 Head
->Type
= (EFI_MEMORY_TYPE
) PoolType
;
396 Tail
= HEAD_TO_TAIL (Head
);
397 Tail
->Signature
= POOL_TAIL_SIGNATURE
;
400 DEBUG_CLEAR_MEMORY (Buffer
, Size
- POOL_OVERHEAD
);
404 "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType
,
406 (UINT64
)(Size
- POOL_OVERHEAD
),
411 // Account the allocation
416 DEBUG ((DEBUG_ERROR
| DEBUG_POOL
, "AllocatePool: failed to allocate %ld bytes\n", (UINT64
) Size
));
427 @param Buffer The allocated pool entry to free
429 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
430 @retval EFI_SUCCESS Pool successfully freed.
435 CoreInternalFreePool (
441 if (Buffer
== NULL
) {
442 return EFI_INVALID_PARAMETER
;
445 CoreAcquireMemoryLock ();
446 Status
= CoreFreePoolI (Buffer
);
447 CoreReleaseMemoryLock ();
454 @param Buffer The allocated pool entry to free
456 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
457 @retval EFI_SUCCESS Pool successfully freed.
468 Status
= CoreInternalFreePool (Buffer
);
469 if (!EFI_ERROR (Status
)) {
470 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0), MemoryProfileActionFreePool
, (EFI_MEMORY_TYPE
) 0, 0, Buffer
);
476 Internal function to free a pool entry.
477 Caller must have the memory lock held
479 @param Buffer The allocated pool entry to free
481 @retval EFI_INVALID_PARAMETER Buffer not valid
482 @retval EFI_SUCCESS Buffer successfully freed.
503 ASSERT(Buffer
!= NULL
);
505 // Get the head & tail of the pool entry
507 Head
= CR (Buffer
, POOL_HEAD
, Data
, POOL_HEAD_SIGNATURE
);
508 ASSERT(Head
!= NULL
);
510 if (Head
->Signature
!= POOL_HEAD_SIGNATURE
) {
511 return EFI_INVALID_PARAMETER
;
514 Tail
= HEAD_TO_TAIL (Head
);
515 ASSERT(Tail
!= NULL
);
520 ASSERT (Tail
->Signature
== POOL_TAIL_SIGNATURE
);
521 ASSERT (Head
->Size
== Tail
->Size
);
522 ASSERT_LOCKED (&gMemoryLock
);
524 if (Tail
->Signature
!= POOL_TAIL_SIGNATURE
) {
525 return EFI_INVALID_PARAMETER
;
528 if (Head
->Size
!= Tail
->Size
) {
529 return EFI_INVALID_PARAMETER
;
533 // Determine the pool type and account for it
536 Pool
= LookupPoolHead (Head
->Type
);
538 return EFI_INVALID_PARAMETER
;
541 DEBUG ((DEBUG_POOL
, "FreePool: %p (len %lx) %,ld\n", Head
->Data
, (UINT64
)(Head
->Size
- POOL_OVERHEAD
), (UINT64
) Pool
->Used
));
543 if (Head
->Type
== EfiACPIReclaimMemory
||
544 Head
->Type
== EfiACPIMemoryNVS
||
545 Head
->Type
== EfiRuntimeServicesCode
||
546 Head
->Type
== EfiRuntimeServicesData
) {
548 Granularity
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
550 Granularity
= DEFAULT_PAGE_ALLOCATION
;
554 // Determine the pool list
556 Index
= SIZE_TO_LIST(Size
);
557 DEBUG_CLEAR_MEMORY (Head
, Size
);
560 // If it's not on the list, it must be pool pages
562 if (Index
>= SIZE_TO_LIST (Granularity
)) {
565 // Return the memory pages back to free memory
567 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (Granularity
) - 1;
568 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (Granularity
) - 1);
569 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Head
, NoPages
);
574 // Put the pool entry onto the free pool list
576 Free
= (POOL_FREE
*) Head
;
577 ASSERT(Free
!= NULL
);
578 Free
->Signature
= POOL_FREE_SIGNATURE
;
579 Free
->Index
= (UINT32
)Index
;
580 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
583 // See if all the pool entries in the same page as Free are freed pool
586 NewPage
= (CHAR8
*)((UINTN
)Free
& ~(Granularity
- 1));
587 Free
= (POOL_FREE
*) &NewPage
[0];
588 ASSERT(Free
!= NULL
);
590 if (Free
->Signature
== POOL_FREE_SIGNATURE
) {
597 while ((Offset
< Granularity
) && (AllFree
)) {
598 FSize
= LIST_TO_SIZE(Index
);
599 while (Offset
+ FSize
<= Granularity
) {
600 Free
= (POOL_FREE
*) &NewPage
[Offset
];
601 ASSERT(Free
!= NULL
);
602 if (Free
->Signature
!= POOL_FREE_SIGNATURE
) {
613 // All of the pool entries in the same page as Free are free pool
615 // Remove all of these pool entries from the free loop lists.
617 Free
= (POOL_FREE
*) &NewPage
[0];
618 ASSERT(Free
!= NULL
);
622 while (Offset
< Granularity
) {
623 FSize
= LIST_TO_SIZE(Index
);
624 while (Offset
+ FSize
<= Granularity
) {
625 Free
= (POOL_FREE
*) &NewPage
[Offset
];
626 ASSERT(Free
!= NULL
);
627 RemoveEntryList (&Free
->Link
);
636 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
)NewPage
, EFI_SIZE_TO_PAGES (Granularity
));
642 // If this is an OS specific memory type, then check to see if the last
643 // portion of that memory type has been freed. If it has, then free the
644 // list entry for that memory type
646 if ((INT32
)Pool
->MemoryType
< 0 && Pool
->Used
== 0) {
647 RemoveEntryList (&Pool
->Link
);
648 CoreFreePoolI (Pool
);