2 UEFI Memory pool management functions.
4 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
5 All rights reserved. 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.
17 #define POOL_FREE_SIGNATURE EFI_SIGNATURE_32('p','f','r','0')
25 #define POOL_HEAD_SIGNATURE EFI_SIGNATURE_32('p','h','d','0')
34 #define SIZE_OF_POOL_HEAD EFI_FIELD_OFFSET(POOL_HEAD,Data)
36 #define POOL_TAIL_SIGNATURE EFI_SIGNATURE_32('p','t','a','l')
45 #define POOL_OVERHEAD (SIZE_OF_POOL_HEAD + sizeof(POOL_TAIL))
47 #define HEAD_TO_TAIL(a) \
48 ((POOL_TAIL *) (((CHAR8 *) (a)) + (a)->Size - sizeof(POOL_TAIL)));
51 #define SIZE_TO_LIST(a) ((a) >> POOL_SHIFT)
52 #define LIST_TO_SIZE(a) ((a+1) << POOL_SHIFT)
54 #define MAX_POOL_LIST SIZE_TO_LIST(DEFAULT_PAGE_ALLOCATION)
56 #define MAX_POOL_SIZE (MAX_ADDRESS - POOL_OVERHEAD)
62 #define POOL_SIGNATURE EFI_SIGNATURE_32('p','l','s','t')
66 EFI_MEMORY_TYPE MemoryType
;
67 LIST_ENTRY FreeList
[MAX_POOL_LIST
];
72 // Pool header for each memory type.
74 POOL mPoolHead
[EfiMaxMemoryType
];
77 // List of pool header to search for the appropriate memory type.
79 LIST_ENTRY mPoolHeadList
= INITIALIZE_LIST_HEAD_VARIABLE (mPoolHeadList
);
83 Called to initialize the pool.
94 for (Type
=0; Type
< EfiMaxMemoryType
; Type
++) {
95 mPoolHead
[Type
].Signature
= 0;
96 mPoolHead
[Type
].Used
= 0;
97 mPoolHead
[Type
].MemoryType
= (EFI_MEMORY_TYPE
) Type
;
98 for (Index
=0; Index
< MAX_POOL_LIST
; Index
++) {
99 InitializeListHead (&mPoolHead
[Type
].FreeList
[Index
]);
106 Look up pool head for specified memory type.
108 @param MemoryType Memory type of which pool head is looked for
110 @return Pointer of Corresponding pool head.
115 IN EFI_MEMORY_TYPE MemoryType
122 if (MemoryType
>= 0 && MemoryType
< EfiMaxMemoryType
) {
123 return &mPoolHead
[MemoryType
];
126 if (MemoryType
< 0) {
128 for (Link
= mPoolHeadList
.ForwardLink
; Link
!= &mPoolHeadList
; Link
= Link
->ForwardLink
) {
129 Pool
= CR(Link
, POOL
, Link
, POOL_SIGNATURE
);
130 if (Pool
->MemoryType
== MemoryType
) {
135 Pool
= CoreAllocatePoolI (EfiBootServicesData
, sizeof (POOL
));
140 Pool
->Signature
= POOL_SIGNATURE
;
142 Pool
->MemoryType
= MemoryType
;
143 for (Index
=0; Index
< MAX_POOL_LIST
; Index
++) {
144 InitializeListHead (&Pool
->FreeList
[Index
]);
147 InsertHeadList (&mPoolHeadList
, &Pool
->Link
);
158 Allocate pool of a particular type.
160 @param PoolType Type of pool to allocate
161 @param Size The amount of pool to allocate
162 @param Buffer The address to return a pointer to the allocated
165 @retval EFI_INVALID_PARAMETER PoolType not valid
166 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
167 @retval EFI_SUCCESS Pool successfully allocated.
173 IN EFI_MEMORY_TYPE PoolType
,
181 // If it's not a valid type, fail it
183 if ((PoolType
>= EfiMaxMemoryType
&& PoolType
<= 0x7fffffff) ||
184 PoolType
== EfiConventionalMemory
) {
185 return EFI_INVALID_PARAMETER
;
191 // If size is too large, fail it
192 // Base on the EFI spec, return status of EFI_OUT_OF_RESOURCES
194 if (Size
> MAX_POOL_SIZE
) {
195 return EFI_OUT_OF_RESOURCES
;
199 // Acquire the memory lock and make the allocation
201 Status
= CoreAcquireLockOrFail (&gMemoryLock
);
202 if (EFI_ERROR (Status
)) {
203 return EFI_OUT_OF_RESOURCES
;
206 *Buffer
= CoreAllocatePoolI (PoolType
, Size
);
207 CoreReleaseMemoryLock ();
208 return (*Buffer
!= NULL
) ? EFI_SUCCESS
: EFI_OUT_OF_RESOURCES
;
214 Internal function to allocate pool of a particular type.
215 Caller must have the memory lock held
217 @param PoolType Type of pool to allocate
218 @param Size The amount of pool to allocate
220 @return The allocate pool, or NULL
225 IN EFI_MEMORY_TYPE PoolType
,
240 ASSERT_LOCKED (&gMemoryLock
);
243 // Adjust the size by the pool header & tail overhead
247 // Adjusting the Size to be of proper alignment so that
248 // we don't get an unaligned access fault later when
249 // pool_Tail is being initialized
251 Size
= ALIGN_VARIABLE (Size
);
253 Size
+= POOL_OVERHEAD
;
254 Index
= SIZE_TO_LIST(Size
);
255 Pool
= LookupPoolHead (PoolType
);
262 // If allocation is over max size, just allocate pages for the request
265 if (Index
>= MAX_POOL_LIST
) {
266 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
) - 1;
267 NoPages
&= ~(EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
) - 1);
268 Head
= CoreAllocatePoolPages (PoolType
, NoPages
, DEFAULT_PAGE_ALLOCATION
);
273 // If there's no free pool in the proper list size, go get some more pages
275 if (IsListEmpty (&Pool
->FreeList
[Index
])) {
280 NewPage
= CoreAllocatePoolPages(PoolType
, EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
), DEFAULT_PAGE_ALLOCATION
);
281 if (NewPage
== NULL
) {
286 // Carve up new page into free pool blocks
289 while (Offset
< DEFAULT_PAGE_ALLOCATION
) {
290 ASSERT (Index
< MAX_POOL_LIST
);
291 FSize
= LIST_TO_SIZE(Index
);
293 while (Offset
+ FSize
<= DEFAULT_PAGE_ALLOCATION
) {
294 Free
= (POOL_FREE
*) &NewPage
[Offset
];
295 Free
->Signature
= POOL_FREE_SIGNATURE
;
296 Free
->Index
= (UINT32
)Index
;
297 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
304 ASSERT (Offset
== DEFAULT_PAGE_ALLOCATION
);
305 Index
= SIZE_TO_LIST(Size
);
309 // Remove entry from free pool list
311 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
312 RemoveEntryList (&Free
->Link
);
314 Head
= (POOL_HEAD
*) Free
;
322 // If we have a pool buffer, fill in the header & tail info
324 Head
->Signature
= POOL_HEAD_SIGNATURE
;
325 Head
->Size
= (UINT32
) Size
;
326 Head
->Type
= (EFI_MEMORY_TYPE
) PoolType
;
327 Tail
= HEAD_TO_TAIL (Head
);
328 Tail
->Signature
= POOL_TAIL_SIGNATURE
;
329 Tail
->Size
= (UINT32
) Size
;
331 DEBUG_CLEAR_MEMORY (Buffer
, Size
- POOL_OVERHEAD
);
335 "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType
,
337 (UINT64
)(Size
- POOL_OVERHEAD
),
342 // Account the allocation
347 DEBUG ((DEBUG_ERROR
| DEBUG_POOL
, "AllocatePool: failed to allocate %ld bytes\n", (UINT64
) Size
));
358 @param Buffer The allocated pool entry to free
360 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
361 @retval EFI_SUCCESS Pool successfully freed.
372 if (Buffer
== NULL
) {
373 return EFI_INVALID_PARAMETER
;
376 CoreAcquireMemoryLock ();
377 Status
= CoreFreePoolI (Buffer
);
378 CoreReleaseMemoryLock ();
385 Internal function to free a pool entry.
386 Caller must have the memory lock held
388 @param Buffer The allocated pool entry to free
390 @retval EFI_INVALID_PARAMETER Buffer not valid
391 @retval EFI_SUCCESS Buffer successfully freed.
411 ASSERT(Buffer
!= NULL
);
413 // Get the head & tail of the pool entry
415 Head
= CR (Buffer
, POOL_HEAD
, Data
, POOL_HEAD_SIGNATURE
);
416 ASSERT(Head
!= NULL
);
418 if (Head
->Signature
!= POOL_HEAD_SIGNATURE
) {
419 return EFI_INVALID_PARAMETER
;
422 Tail
= HEAD_TO_TAIL (Head
);
423 ASSERT(Tail
!= NULL
);
428 ASSERT (Tail
->Signature
== POOL_TAIL_SIGNATURE
);
429 ASSERT (Head
->Size
== Tail
->Size
);
430 ASSERT_LOCKED (&gMemoryLock
);
432 if (Tail
->Signature
!= POOL_TAIL_SIGNATURE
) {
433 return EFI_INVALID_PARAMETER
;
436 if (Head
->Size
!= Tail
->Size
) {
437 return EFI_INVALID_PARAMETER
;
441 // Determine the pool type and account for it
444 Pool
= LookupPoolHead (Head
->Type
);
446 return EFI_INVALID_PARAMETER
;
449 DEBUG ((DEBUG_POOL
, "FreePool: %p (len %lx) %,ld\n", Head
->Data
, (UINT64
)(Head
->Size
- POOL_OVERHEAD
), (UINT64
) Pool
->Used
));
452 // Determine the pool list
454 Index
= SIZE_TO_LIST(Size
);
455 DEBUG_CLEAR_MEMORY (Head
, Size
);
458 // If it's not on the list, it must be pool pages
460 if (Index
>= MAX_POOL_LIST
) {
463 // Return the memory pages back to free memory
465 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
) - 1;
466 NoPages
&= ~(EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
) - 1);
467 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Head
, NoPages
);
472 // Put the pool entry onto the free pool list
474 Free
= (POOL_FREE
*) Head
;
475 ASSERT(Free
!= NULL
);
476 Free
->Signature
= POOL_FREE_SIGNATURE
;
477 Free
->Index
= (UINT32
)Index
;
478 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
481 // See if all the pool entries in the same page as Free are freed pool
484 NewPage
= (CHAR8
*)((UINTN
)Free
& ~((DEFAULT_PAGE_ALLOCATION
) -1));
485 Free
= (POOL_FREE
*) &NewPage
[0];
486 ASSERT(Free
!= NULL
);
488 if (Free
->Signature
== POOL_FREE_SIGNATURE
) {
495 while ((Offset
< DEFAULT_PAGE_ALLOCATION
) && (AllFree
)) {
496 FSize
= LIST_TO_SIZE(Index
);
497 while (Offset
+ FSize
<= DEFAULT_PAGE_ALLOCATION
) {
498 Free
= (POOL_FREE
*) &NewPage
[Offset
];
499 ASSERT(Free
!= NULL
);
500 if (Free
->Signature
!= POOL_FREE_SIGNATURE
) {
511 // All of the pool entries in the same page as Free are free pool
513 // Remove all of these pool entries from the free loop lists.
515 Free
= (POOL_FREE
*) &NewPage
[0];
516 ASSERT(Free
!= NULL
);
520 while (Offset
< DEFAULT_PAGE_ALLOCATION
) {
521 FSize
= LIST_TO_SIZE(Index
);
522 while (Offset
+ FSize
<= DEFAULT_PAGE_ALLOCATION
) {
523 Free
= (POOL_FREE
*) &NewPage
[Offset
];
524 ASSERT(Free
!= NULL
);
525 RemoveEntryList (&Free
->Link
);
534 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
)NewPage
, EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
));
540 // If this is an OS specific memory type, then check to see if the last
541 // portion of that memory type has been freed. If it has, then free the
542 // list entry for that memory type
544 if (Pool
->MemoryType
< 0 && Pool
->Used
== 0) {
545 RemoveEntryList (&Pool
->Link
);
546 CoreFreePoolI (Pool
);