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')
47 #define POOL_OVERHEAD (SIZE_OF_POOL_HEAD + sizeof(POOL_TAIL))
49 #define HEAD_TO_TAIL(a) \
50 ((POOL_TAIL *) (((CHAR8 *) (a)) + (a)->Size - sizeof(POOL_TAIL)));
53 #define SIZE_TO_LIST(a) ((a) >> POOL_SHIFT)
54 #define LIST_TO_SIZE(a) ((a+1) << POOL_SHIFT)
56 #define MAX_POOL_LIST SIZE_TO_LIST(DEFAULT_PAGE_ALLOCATION)
58 #define MAX_POOL_SIZE (MAX_ADDRESS - POOL_OVERHEAD)
64 #define POOL_SIGNATURE SIGNATURE_32('p','l','s','t')
68 EFI_MEMORY_TYPE MemoryType
;
69 LIST_ENTRY FreeList
[MAX_POOL_LIST
];
74 // Pool header for each memory type.
76 POOL mPoolHead
[EfiMaxMemoryType
];
79 // List of pool header to search for the appropriate memory type.
81 LIST_ENTRY mPoolHeadList
= INITIALIZE_LIST_HEAD_VARIABLE (mPoolHeadList
);
85 Called to initialize the pool.
96 for (Type
=0; Type
< EfiMaxMemoryType
; Type
++) {
97 mPoolHead
[Type
].Signature
= 0;
98 mPoolHead
[Type
].Used
= 0;
99 mPoolHead
[Type
].MemoryType
= (EFI_MEMORY_TYPE
) Type
;
100 for (Index
=0; Index
< MAX_POOL_LIST
; Index
++) {
101 InitializeListHead (&mPoolHead
[Type
].FreeList
[Index
]);
108 Look up pool head for specified memory type.
110 @param MemoryType Memory type of which pool head is looked for
112 @return Pointer of Corresponding pool head.
117 IN EFI_MEMORY_TYPE MemoryType
124 if ((UINT32
)MemoryType
< EfiMaxMemoryType
) {
125 return &mPoolHead
[MemoryType
];
129 // MemoryType values in the range 0x80000000..0xFFFFFFFF are reserved for use by UEFI
130 // OS loaders that are provided by operating system vendors
132 if ((INT32
)MemoryType
< 0) {
134 for (Link
= mPoolHeadList
.ForwardLink
; Link
!= &mPoolHeadList
; Link
= Link
->ForwardLink
) {
135 Pool
= CR(Link
, POOL
, Link
, POOL_SIGNATURE
);
136 if (Pool
->MemoryType
== MemoryType
) {
141 Pool
= CoreAllocatePoolI (EfiBootServicesData
, sizeof (POOL
));
146 Pool
->Signature
= POOL_SIGNATURE
;
148 Pool
->MemoryType
= MemoryType
;
149 for (Index
=0; Index
< MAX_POOL_LIST
; Index
++) {
150 InitializeListHead (&Pool
->FreeList
[Index
]);
153 InsertHeadList (&mPoolHeadList
, &Pool
->Link
);
164 Allocate pool of a particular type.
166 @param PoolType Type of pool to allocate
167 @param Size The amount of pool to allocate
168 @param Buffer The address to return a pointer to the allocated
171 @retval EFI_INVALID_PARAMETER PoolType not valid or Buffer is NULL.
172 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
173 @retval EFI_SUCCESS Pool successfully allocated.
179 IN EFI_MEMORY_TYPE PoolType
,
187 // If it's not a valid type, fail it
189 if ((PoolType
>= EfiMaxMemoryType
&& PoolType
<= 0x7fffffff) ||
190 PoolType
== EfiConventionalMemory
) {
191 return EFI_INVALID_PARAMETER
;
194 if (Buffer
== NULL
) {
195 return EFI_INVALID_PARAMETER
;
201 // If size is too large, fail it
202 // Base on the EFI spec, return status of EFI_OUT_OF_RESOURCES
204 if (Size
> MAX_POOL_SIZE
) {
205 return EFI_OUT_OF_RESOURCES
;
209 // Acquire the memory lock and make the allocation
211 Status
= CoreAcquireLockOrFail (&gMemoryLock
);
212 if (EFI_ERROR (Status
)) {
213 return EFI_OUT_OF_RESOURCES
;
216 *Buffer
= CoreAllocatePoolI (PoolType
, Size
);
217 CoreReleaseMemoryLock ();
218 return (*Buffer
!= NULL
) ? EFI_SUCCESS
: EFI_OUT_OF_RESOURCES
;
224 Internal function to allocate pool of a particular type.
225 Caller must have the memory lock held
227 @param PoolType Type of pool to allocate
228 @param Size The amount of pool to allocate
230 @return The allocate pool, or NULL
235 IN EFI_MEMORY_TYPE PoolType
,
250 ASSERT_LOCKED (&gMemoryLock
);
253 // Adjust the size by the pool header & tail overhead
257 // Adjusting the Size to be of proper alignment so that
258 // we don't get an unaligned access fault later when
259 // pool_Tail is being initialized
261 Size
= ALIGN_VARIABLE (Size
);
263 Size
+= POOL_OVERHEAD
;
264 Index
= SIZE_TO_LIST(Size
);
265 Pool
= LookupPoolHead (PoolType
);
272 // If allocation is over max size, just allocate pages for the request
275 if (Index
>= MAX_POOL_LIST
) {
276 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
) - 1;
277 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
) - 1);
278 Head
= CoreAllocatePoolPages (PoolType
, NoPages
, DEFAULT_PAGE_ALLOCATION
);
283 // If there's no free pool in the proper list size, go get some more pages
285 if (IsListEmpty (&Pool
->FreeList
[Index
])) {
290 NewPage
= CoreAllocatePoolPages(PoolType
, EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
), DEFAULT_PAGE_ALLOCATION
);
291 if (NewPage
== NULL
) {
296 // Carve up new page into free pool blocks
299 while (Offset
< DEFAULT_PAGE_ALLOCATION
) {
300 ASSERT (Index
< MAX_POOL_LIST
);
301 FSize
= LIST_TO_SIZE(Index
);
303 while (Offset
+ FSize
<= DEFAULT_PAGE_ALLOCATION
) {
304 Free
= (POOL_FREE
*) &NewPage
[Offset
];
305 Free
->Signature
= POOL_FREE_SIGNATURE
;
306 Free
->Index
= (UINT32
)Index
;
307 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
314 ASSERT (Offset
== DEFAULT_PAGE_ALLOCATION
);
315 Index
= SIZE_TO_LIST(Size
);
319 // Remove entry from free pool list
321 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
322 RemoveEntryList (&Free
->Link
);
324 Head
= (POOL_HEAD
*) Free
;
332 // If we have a pool buffer, fill in the header & tail info
334 Head
->Signature
= POOL_HEAD_SIGNATURE
;
336 Head
->Type
= (EFI_MEMORY_TYPE
) PoolType
;
337 Tail
= HEAD_TO_TAIL (Head
);
338 Tail
->Signature
= POOL_TAIL_SIGNATURE
;
341 DEBUG_CLEAR_MEMORY (Buffer
, Size
- POOL_OVERHEAD
);
345 "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType
,
347 (UINT64
)(Size
- POOL_OVERHEAD
),
352 // Account the allocation
357 DEBUG ((DEBUG_ERROR
| DEBUG_POOL
, "AllocatePool: failed to allocate %ld bytes\n", (UINT64
) Size
));
368 @param Buffer The allocated pool entry to free
370 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
371 @retval EFI_SUCCESS Pool successfully freed.
382 if (Buffer
== NULL
) {
383 return EFI_INVALID_PARAMETER
;
386 CoreAcquireMemoryLock ();
387 Status
= CoreFreePoolI (Buffer
);
388 CoreReleaseMemoryLock ();
395 Internal function to free a pool entry.
396 Caller must have the memory lock held
398 @param Buffer The allocated pool entry to free
400 @retval EFI_INVALID_PARAMETER Buffer not valid
401 @retval EFI_SUCCESS Buffer successfully freed.
421 ASSERT(Buffer
!= NULL
);
423 // Get the head & tail of the pool entry
425 Head
= CR (Buffer
, POOL_HEAD
, Data
, POOL_HEAD_SIGNATURE
);
426 ASSERT(Head
!= NULL
);
428 if (Head
->Signature
!= POOL_HEAD_SIGNATURE
) {
429 return EFI_INVALID_PARAMETER
;
432 Tail
= HEAD_TO_TAIL (Head
);
433 ASSERT(Tail
!= NULL
);
438 ASSERT (Tail
->Signature
== POOL_TAIL_SIGNATURE
);
439 ASSERT (Head
->Size
== Tail
->Size
);
440 ASSERT_LOCKED (&gMemoryLock
);
442 if (Tail
->Signature
!= POOL_TAIL_SIGNATURE
) {
443 return EFI_INVALID_PARAMETER
;
446 if (Head
->Size
!= Tail
->Size
) {
447 return EFI_INVALID_PARAMETER
;
451 // Determine the pool type and account for it
454 Pool
= LookupPoolHead (Head
->Type
);
456 return EFI_INVALID_PARAMETER
;
459 DEBUG ((DEBUG_POOL
, "FreePool: %p (len %lx) %,ld\n", Head
->Data
, (UINT64
)(Head
->Size
- POOL_OVERHEAD
), (UINT64
) Pool
->Used
));
462 // Determine the pool list
464 Index
= SIZE_TO_LIST(Size
);
465 DEBUG_CLEAR_MEMORY (Head
, Size
);
468 // If it's not on the list, it must be pool pages
470 if (Index
>= MAX_POOL_LIST
) {
473 // Return the memory pages back to free memory
475 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
) - 1;
476 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
) - 1);
477 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Head
, NoPages
);
482 // Put the pool entry onto the free pool list
484 Free
= (POOL_FREE
*) Head
;
485 ASSERT(Free
!= NULL
);
486 Free
->Signature
= POOL_FREE_SIGNATURE
;
487 Free
->Index
= (UINT32
)Index
;
488 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
491 // See if all the pool entries in the same page as Free are freed pool
494 NewPage
= (CHAR8
*)((UINTN
)Free
& ~((DEFAULT_PAGE_ALLOCATION
) -1));
495 Free
= (POOL_FREE
*) &NewPage
[0];
496 ASSERT(Free
!= NULL
);
498 if (Free
->Signature
== POOL_FREE_SIGNATURE
) {
505 while ((Offset
< DEFAULT_PAGE_ALLOCATION
) && (AllFree
)) {
506 FSize
= LIST_TO_SIZE(Index
);
507 while (Offset
+ FSize
<= DEFAULT_PAGE_ALLOCATION
) {
508 Free
= (POOL_FREE
*) &NewPage
[Offset
];
509 ASSERT(Free
!= NULL
);
510 if (Free
->Signature
!= POOL_FREE_SIGNATURE
) {
521 // All of the pool entries in the same page as Free are free pool
523 // Remove all of these pool entries from the free loop lists.
525 Free
= (POOL_FREE
*) &NewPage
[0];
526 ASSERT(Free
!= NULL
);
530 while (Offset
< DEFAULT_PAGE_ALLOCATION
) {
531 FSize
= LIST_TO_SIZE(Index
);
532 while (Offset
+ FSize
<= DEFAULT_PAGE_ALLOCATION
) {
533 Free
= (POOL_FREE
*) &NewPage
[Offset
];
534 ASSERT(Free
!= NULL
);
535 RemoveEntryList (&Free
->Link
);
544 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
)NewPage
, EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
));
550 // If this is an OS specific memory type, then check to see if the last
551 // portion of that memory type has been freed. If it has, then free the
552 // list entry for that memory type
554 if ((INT32
)Pool
->MemoryType
< 0 && Pool
->Used
== 0) {
555 RemoveEntryList (&Pool
->Link
);
556 CoreFreePoolI (Pool
);