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.
178 CoreInternalAllocatePool (
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
;
222 Allocate pool of a particular type.
224 @param PoolType Type of pool to allocate
225 @param Size The amount of pool to allocate
226 @param Buffer The address to return a pointer to the allocated
229 @retval EFI_INVALID_PARAMETER PoolType not valid or Buffer is NULL.
230 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
231 @retval EFI_SUCCESS Pool successfully allocated.
237 IN EFI_MEMORY_TYPE PoolType
,
244 Status
= CoreInternalAllocatePool (PoolType
, Size
, Buffer
);
245 if (!EFI_ERROR (Status
)) {
246 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0), MemoryProfileActionAllocatePool
, PoolType
, Size
, *Buffer
);
252 Internal function to allocate pool of a particular type.
253 Caller must have the memory lock held
255 @param PoolType Type of pool to allocate
256 @param Size The amount of pool to allocate
258 @return The allocate pool, or NULL
263 IN EFI_MEMORY_TYPE PoolType
,
278 ASSERT_LOCKED (&gMemoryLock
);
281 // Adjust the size by the pool header & tail overhead
285 // Adjusting the Size to be of proper alignment so that
286 // we don't get an unaligned access fault later when
287 // pool_Tail is being initialized
289 Size
= ALIGN_VARIABLE (Size
);
291 Size
+= POOL_OVERHEAD
;
292 Index
= SIZE_TO_LIST(Size
);
293 Pool
= LookupPoolHead (PoolType
);
300 // If allocation is over max size, just allocate pages for the request
303 if (Index
>= MAX_POOL_LIST
) {
304 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
) - 1;
305 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
) - 1);
306 Head
= CoreAllocatePoolPages (PoolType
, NoPages
, DEFAULT_PAGE_ALLOCATION
);
311 // If there's no free pool in the proper list size, go get some more pages
313 if (IsListEmpty (&Pool
->FreeList
[Index
])) {
318 NewPage
= CoreAllocatePoolPages(PoolType
, EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
), DEFAULT_PAGE_ALLOCATION
);
319 if (NewPage
== NULL
) {
324 // Carve up new page into free pool blocks
327 while (Offset
< DEFAULT_PAGE_ALLOCATION
) {
328 ASSERT (Index
< MAX_POOL_LIST
);
329 FSize
= LIST_TO_SIZE(Index
);
331 while (Offset
+ FSize
<= DEFAULT_PAGE_ALLOCATION
) {
332 Free
= (POOL_FREE
*) &NewPage
[Offset
];
333 Free
->Signature
= POOL_FREE_SIGNATURE
;
334 Free
->Index
= (UINT32
)Index
;
335 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
342 ASSERT (Offset
== DEFAULT_PAGE_ALLOCATION
);
343 Index
= SIZE_TO_LIST(Size
);
347 // Remove entry from free pool list
349 Free
= CR (Pool
->FreeList
[Index
].ForwardLink
, POOL_FREE
, Link
, POOL_FREE_SIGNATURE
);
350 RemoveEntryList (&Free
->Link
);
352 Head
= (POOL_HEAD
*) Free
;
360 // If we have a pool buffer, fill in the header & tail info
362 Head
->Signature
= POOL_HEAD_SIGNATURE
;
364 Head
->Type
= (EFI_MEMORY_TYPE
) PoolType
;
365 Tail
= HEAD_TO_TAIL (Head
);
366 Tail
->Signature
= POOL_TAIL_SIGNATURE
;
369 DEBUG_CLEAR_MEMORY (Buffer
, Size
- POOL_OVERHEAD
);
373 "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType
,
375 (UINT64
)(Size
- POOL_OVERHEAD
),
380 // Account the allocation
385 DEBUG ((DEBUG_ERROR
| DEBUG_POOL
, "AllocatePool: failed to allocate %ld bytes\n", (UINT64
) Size
));
396 @param Buffer The allocated pool entry to free
398 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
399 @retval EFI_SUCCESS Pool successfully freed.
404 CoreInternalFreePool (
410 if (Buffer
== NULL
) {
411 return EFI_INVALID_PARAMETER
;
414 CoreAcquireMemoryLock ();
415 Status
= CoreFreePoolI (Buffer
);
416 CoreReleaseMemoryLock ();
423 @param Buffer The allocated pool entry to free
425 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
426 @retval EFI_SUCCESS Pool successfully freed.
437 Status
= CoreInternalFreePool (Buffer
);
438 if (!EFI_ERROR (Status
)) {
439 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0), MemoryProfileActionFreePool
, (EFI_MEMORY_TYPE
) 0, 0, Buffer
);
445 Internal function to free a pool entry.
446 Caller must have the memory lock held
448 @param Buffer The allocated pool entry to free
450 @retval EFI_INVALID_PARAMETER Buffer not valid
451 @retval EFI_SUCCESS Buffer successfully freed.
471 ASSERT(Buffer
!= NULL
);
473 // Get the head & tail of the pool entry
475 Head
= CR (Buffer
, POOL_HEAD
, Data
, POOL_HEAD_SIGNATURE
);
476 ASSERT(Head
!= NULL
);
478 if (Head
->Signature
!= POOL_HEAD_SIGNATURE
) {
479 return EFI_INVALID_PARAMETER
;
482 Tail
= HEAD_TO_TAIL (Head
);
483 ASSERT(Tail
!= NULL
);
488 ASSERT (Tail
->Signature
== POOL_TAIL_SIGNATURE
);
489 ASSERT (Head
->Size
== Tail
->Size
);
490 ASSERT_LOCKED (&gMemoryLock
);
492 if (Tail
->Signature
!= POOL_TAIL_SIGNATURE
) {
493 return EFI_INVALID_PARAMETER
;
496 if (Head
->Size
!= Tail
->Size
) {
497 return EFI_INVALID_PARAMETER
;
501 // Determine the pool type and account for it
504 Pool
= LookupPoolHead (Head
->Type
);
506 return EFI_INVALID_PARAMETER
;
509 DEBUG ((DEBUG_POOL
, "FreePool: %p (len %lx) %,ld\n", Head
->Data
, (UINT64
)(Head
->Size
- POOL_OVERHEAD
), (UINT64
) Pool
->Used
));
512 // Determine the pool list
514 Index
= SIZE_TO_LIST(Size
);
515 DEBUG_CLEAR_MEMORY (Head
, Size
);
518 // If it's not on the list, it must be pool pages
520 if (Index
>= MAX_POOL_LIST
) {
523 // Return the memory pages back to free memory
525 NoPages
= EFI_SIZE_TO_PAGES(Size
) + EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
) - 1;
526 NoPages
&= ~(UINTN
)(EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
) - 1);
527 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Head
, NoPages
);
532 // Put the pool entry onto the free pool list
534 Free
= (POOL_FREE
*) Head
;
535 ASSERT(Free
!= NULL
);
536 Free
->Signature
= POOL_FREE_SIGNATURE
;
537 Free
->Index
= (UINT32
)Index
;
538 InsertHeadList (&Pool
->FreeList
[Index
], &Free
->Link
);
541 // See if all the pool entries in the same page as Free are freed pool
544 NewPage
= (CHAR8
*)((UINTN
)Free
& ~((DEFAULT_PAGE_ALLOCATION
) -1));
545 Free
= (POOL_FREE
*) &NewPage
[0];
546 ASSERT(Free
!= NULL
);
548 if (Free
->Signature
== POOL_FREE_SIGNATURE
) {
555 while ((Offset
< DEFAULT_PAGE_ALLOCATION
) && (AllFree
)) {
556 FSize
= LIST_TO_SIZE(Index
);
557 while (Offset
+ FSize
<= DEFAULT_PAGE_ALLOCATION
) {
558 Free
= (POOL_FREE
*) &NewPage
[Offset
];
559 ASSERT(Free
!= NULL
);
560 if (Free
->Signature
!= POOL_FREE_SIGNATURE
) {
571 // All of the pool entries in the same page as Free are free pool
573 // Remove all of these pool entries from the free loop lists.
575 Free
= (POOL_FREE
*) &NewPage
[0];
576 ASSERT(Free
!= NULL
);
580 while (Offset
< DEFAULT_PAGE_ALLOCATION
) {
581 FSize
= LIST_TO_SIZE(Index
);
582 while (Offset
+ FSize
<= DEFAULT_PAGE_ALLOCATION
) {
583 Free
= (POOL_FREE
*) &NewPage
[Offset
];
584 ASSERT(Free
!= NULL
);
585 RemoveEntryList (&Free
->Link
);
594 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS
) (UINTN
)NewPage
, EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION
));
600 // If this is an OS specific memory type, then check to see if the last
601 // portion of that memory type has been freed. If it has, then free the
602 // list entry for that memory type
604 if ((INT32
)Pool
->MemoryType
< 0 && Pool
->Used
== 0) {
605 RemoveEntryList (&Pool
->Link
);
606 CoreFreePoolI (Pool
);