2 UEFI Memory pool management functions.
4 Copyright (c) 2006 - 2010, 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')
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 #define SIZE_TO_LIST(a) ((a) >> POOL_SHIFT)
53 #define LIST_TO_SIZE(a) ((a+1) << POOL_SHIFT)
55 #define MAX_POOL_LIST SIZE_TO_LIST(DEFAULT_PAGE_ALLOCATION)
57 #define MAX_POOL_SIZE (MAX_ADDRESS - POOL_OVERHEAD)
63 #define POOL_SIGNATURE SIGNATURE_32('p','l','s','t')
67 EFI_MEMORY_TYPE MemoryType
;
68 LIST_ENTRY FreeList
[MAX_POOL_LIST
];
73 // Pool header for each memory type.
75 POOL mPoolHead
[EfiMaxMemoryType
];
78 // List of pool header to search for the appropriate memory type.
80 LIST_ENTRY mPoolHeadList
= INITIALIZE_LIST_HEAD_VARIABLE (mPoolHeadList
);
84 Called to initialize the pool.
95 for (Type
=0; Type
< EfiMaxMemoryType
; Type
++) {
96 mPoolHead
[Type
].Signature
= 0;
97 mPoolHead
[Type
].Used
= 0;
98 mPoolHead
[Type
].MemoryType
= (EFI_MEMORY_TYPE
) Type
;
99 for (Index
=0; Index
< MAX_POOL_LIST
; Index
++) {
100 InitializeListHead (&mPoolHead
[Type
].FreeList
[Index
]);
107 Look up pool head for specified memory type.
109 @param MemoryType Memory type of which pool head is looked for
111 @return Pointer of Corresponding pool head.
116 IN EFI_MEMORY_TYPE MemoryType
123 if (MemoryType
>= 0 && MemoryType
< EfiMaxMemoryType
) {
124 return &mPoolHead
[MemoryType
];
128 // MemoryType values in the range 0x80000000..0xFFFFFFFF are reserved for use by UEFI
129 // OS loaders that are provided by operating system vendors
131 if (MemoryType
>= (INT32
)0x80000000 && MemoryType
<= (INT32
)0xffffffff) {
133 for (Link
= mPoolHeadList
.ForwardLink
; Link
!= &mPoolHeadList
; Link
= Link
->ForwardLink
) {
134 Pool
= CR(Link
, POOL
, Link
, POOL_SIGNATURE
);
135 if (Pool
->MemoryType
== MemoryType
) {
140 Pool
= CoreAllocatePoolI (EfiBootServicesData
, sizeof (POOL
));
145 Pool
->Signature
= POOL_SIGNATURE
;
147 Pool
->MemoryType
= MemoryType
;
148 for (Index
=0; Index
< MAX_POOL_LIST
; Index
++) {
149 InitializeListHead (&Pool
->FreeList
[Index
]);
152 InsertHeadList (&mPoolHeadList
, &Pool
->Link
);
163 Allocate pool of a particular type.
165 @param PoolType Type of pool to allocate
166 @param Size The amount of pool to allocate
167 @param Buffer The address to return a pointer to the allocated
170 @retval EFI_INVALID_PARAMETER PoolType not valid
171 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
172 @retval EFI_SUCCESS Pool successfully allocated.
178 IN EFI_MEMORY_TYPE PoolType
,
186 // If it's not a valid type, fail it
188 if ((PoolType
>= EfiMaxMemoryType
&& PoolType
<= 0x7fffffff) ||
189 PoolType
== EfiConventionalMemory
) {
190 return EFI_INVALID_PARAMETER
;
196 // If size is too large, fail it
197 // Base on the EFI spec, return status of EFI_OUT_OF_RESOURCES
199 if (Size
> MAX_POOL_SIZE
) {
200 return EFI_OUT_OF_RESOURCES
;
204 // Acquire the memory lock and make the allocation
206 Status
= CoreAcquireLockOrFail (&gMemoryLock
);
207 if (EFI_ERROR (Status
)) {
208 return EFI_OUT_OF_RESOURCES
;
211 *Buffer
= CoreAllocatePoolI (PoolType
, Size
);
212 CoreReleaseMemoryLock ();
213 return (*Buffer
!= NULL
) ? EFI_SUCCESS
: EFI_OUT_OF_RESOURCES
;
219 Internal function to allocate pool of a particular type.
220 Caller must have the memory lock held
222 @param PoolType Type of pool to allocate
223 @param Size The amount of pool to allocate
225 @return The allocate pool, or NULL
230 IN EFI_MEMORY_TYPE PoolType
,
245 ASSERT_LOCKED (&gMemoryLock
);
248 // Adjust the size by the pool header & tail overhead
252 // Adjusting the Size to be of proper alignment so that
253 // we don't get an unaligned access fault later when
254 // pool_Tail is being initialized
256 Size
= ALIGN_VARIABLE (Size
);
258 Size
+= POOL_OVERHEAD
;
259 Index
= SIZE_TO_LIST(Size
);
260 Pool
= LookupPoolHead (PoolType
);
267 // If allocation is over max size, just allocate pages for the request
270 if (Index
>= MAX_POOL_LIST
) {
271 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
) - 1;
272 NoPages
&= ~(EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
) - 1);
273 Head
= CoreAllocatePoolPages (PoolType
, NoPages
, DEFAULT_PAGE_ALLOCATION
);
278 // If there's no free pool in the proper list size, go get some more pages
280 if (IsListEmpty (&Pool
->FreeList
[Index
])) {
285 NewPage
= CoreAllocatePoolPages(PoolType
, EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
), DEFAULT_PAGE_ALLOCATION
);
286 if (NewPage
== NULL
) {
291 // Carve up new page into free pool blocks
294 while (Offset
< DEFAULT_PAGE_ALLOCATION
) {
295 ASSERT (Index
< MAX_POOL_LIST
);
296 FSize
= LIST_TO_SIZE(Index
);
298 while (Offset
+ FSize
<= DEFAULT_PAGE_ALLOCATION
) {
299 Free
= (POOL_FREE
*) &NewPage
[Offset
];
300 Free
->Signature
= POOL_FREE_SIGNATURE
;
301 Free
->Index
= (UINT32
)Index
;
302 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
309 ASSERT (Offset
== DEFAULT_PAGE_ALLOCATION
);
310 Index
= SIZE_TO_LIST(Size
);
314 // Remove entry from free pool list
316 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
317 RemoveEntryList (&Free
->Link
);
319 Head
= (POOL_HEAD
*) Free
;
327 // If we have a pool buffer, fill in the header & tail info
329 Head
->Signature
= POOL_HEAD_SIGNATURE
;
330 Head
->Size
= (UINT32
) Size
;
331 Head
->Type
= (EFI_MEMORY_TYPE
) PoolType
;
332 Tail
= HEAD_TO_TAIL (Head
);
333 Tail
->Signature
= POOL_TAIL_SIGNATURE
;
334 Tail
->Size
= (UINT32
) Size
;
336 DEBUG_CLEAR_MEMORY (Buffer
, Size
- POOL_OVERHEAD
);
340 "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType
,
342 (UINT64
)(Size
- POOL_OVERHEAD
),
347 // Account the allocation
352 DEBUG ((DEBUG_ERROR
| DEBUG_POOL
, "AllocatePool: failed to allocate %ld bytes\n", (UINT64
) Size
));
363 @param Buffer The allocated pool entry to free
365 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
366 @retval EFI_SUCCESS Pool successfully freed.
377 if (Buffer
== NULL
) {
378 return EFI_INVALID_PARAMETER
;
381 CoreAcquireMemoryLock ();
382 Status
= CoreFreePoolI (Buffer
);
383 CoreReleaseMemoryLock ();
390 Internal function to free a pool entry.
391 Caller must have the memory lock held
393 @param Buffer The allocated pool entry to free
395 @retval EFI_INVALID_PARAMETER Buffer not valid
396 @retval EFI_SUCCESS Buffer successfully freed.
416 ASSERT(Buffer
!= NULL
);
418 // Get the head & tail of the pool entry
420 Head
= CR (Buffer
, POOL_HEAD
, Data
, POOL_HEAD_SIGNATURE
);
421 ASSERT(Head
!= NULL
);
423 if (Head
->Signature
!= POOL_HEAD_SIGNATURE
) {
424 return EFI_INVALID_PARAMETER
;
427 Tail
= HEAD_TO_TAIL (Head
);
428 ASSERT(Tail
!= NULL
);
433 ASSERT (Tail
->Signature
== POOL_TAIL_SIGNATURE
);
434 ASSERT (Head
->Size
== Tail
->Size
);
435 ASSERT_LOCKED (&gMemoryLock
);
437 if (Tail
->Signature
!= POOL_TAIL_SIGNATURE
) {
438 return EFI_INVALID_PARAMETER
;
441 if (Head
->Size
!= Tail
->Size
) {
442 return EFI_INVALID_PARAMETER
;
446 // Determine the pool type and account for it
449 Pool
= LookupPoolHead (Head
->Type
);
451 return EFI_INVALID_PARAMETER
;
454 DEBUG ((DEBUG_POOL
, "FreePool: %p (len %lx) %,ld\n", Head
->Data
, (UINT64
)(Head
->Size
- POOL_OVERHEAD
), (UINT64
) Pool
->Used
));
457 // Determine the pool list
459 Index
= SIZE_TO_LIST(Size
);
460 DEBUG_CLEAR_MEMORY (Head
, Size
);
463 // If it's not on the list, it must be pool pages
465 if (Index
>= MAX_POOL_LIST
) {
468 // Return the memory pages back to free memory
470 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
) - 1;
471 NoPages
&= ~(EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
) - 1);
472 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Head
, NoPages
);
477 // Put the pool entry onto the free pool list
479 Free
= (POOL_FREE
*) Head
;
480 ASSERT(Free
!= NULL
);
481 Free
->Signature
= POOL_FREE_SIGNATURE
;
482 Free
->Index
= (UINT32
)Index
;
483 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
486 // See if all the pool entries in the same page as Free are freed pool
489 NewPage
= (CHAR8
*)((UINTN
)Free
& ~((DEFAULT_PAGE_ALLOCATION
) -1));
490 Free
= (POOL_FREE
*) &NewPage
[0];
491 ASSERT(Free
!= NULL
);
493 if (Free
->Signature
== POOL_FREE_SIGNATURE
) {
500 while ((Offset
< DEFAULT_PAGE_ALLOCATION
) && (AllFree
)) {
501 FSize
= LIST_TO_SIZE(Index
);
502 while (Offset
+ FSize
<= DEFAULT_PAGE_ALLOCATION
) {
503 Free
= (POOL_FREE
*) &NewPage
[Offset
];
504 ASSERT(Free
!= NULL
);
505 if (Free
->Signature
!= POOL_FREE_SIGNATURE
) {
516 // All of the pool entries in the same page as Free are free pool
518 // Remove all of these pool entries from the free loop lists.
520 Free
= (POOL_FREE
*) &NewPage
[0];
521 ASSERT(Free
!= NULL
);
525 while (Offset
< DEFAULT_PAGE_ALLOCATION
) {
526 FSize
= LIST_TO_SIZE(Index
);
527 while (Offset
+ FSize
<= DEFAULT_PAGE_ALLOCATION
) {
528 Free
= (POOL_FREE
*) &NewPage
[Offset
];
529 ASSERT(Free
!= NULL
);
530 RemoveEntryList (&Free
->Link
);
539 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
)NewPage
, EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
));
545 // If this is an OS specific memory type, then check to see if the last
546 // portion of that memory type has been freed. If it has, then free the
547 // list entry for that memory type
549 if (Pool
->MemoryType
< 0 && Pool
->Used
== 0) {
550 RemoveEntryList (&Pool
->Link
);
551 CoreFreePoolI (Pool
);