]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Mem/Pool.c
MdeModulePkg: Add function comment for 'GetPoolIndexFromSize'.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Mem / Pool.c
1 /** @file
2 UEFI Memory pool management functions.
3
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
9
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.
12
13 **/
14
15 #include "DxeMain.h"
16 #include "Imem.h"
17
18 #define POOL_FREE_SIGNATURE SIGNATURE_32('p','f','r','0')
19 typedef struct {
20 UINT32 Signature;
21 UINT32 Index;
22 LIST_ENTRY Link;
23 } POOL_FREE;
24
25
26 #define POOL_HEAD_SIGNATURE SIGNATURE_32('p','h','d','0')
27 typedef struct {
28 UINT32 Signature;
29 UINT32 Reserved;
30 EFI_MEMORY_TYPE Type;
31 UINTN Size;
32 CHAR8 Data[1];
33 } POOL_HEAD;
34
35 #define SIZE_OF_POOL_HEAD OFFSET_OF(POOL_HEAD,Data)
36
37 #define POOL_TAIL_SIGNATURE SIGNATURE_32('p','t','a','l')
38 typedef struct {
39 UINT32 Signature;
40 UINT32 Reserved;
41 UINTN Size;
42 } POOL_TAIL;
43
44 #define POOL_OVERHEAD (SIZE_OF_POOL_HEAD + sizeof(POOL_TAIL))
45
46 #define HEAD_TO_TAIL(a) \
47 ((POOL_TAIL *) (((CHAR8 *) (a)) + (a)->Size - sizeof(POOL_TAIL)));
48
49 //
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
53 //
54 STATIC CONST UINT16 mPoolSizeTable[] = {
55 64, 128, 192, 320, 512, 832, 1344, 2176, 3520, 5696, 9216, 14912, 24128
56 };
57
58 #define SIZE_TO_LIST(a) (GetPoolIndexFromSize (a))
59 #define LIST_TO_SIZE(a) (mPoolSizeTable [a])
60
61 #define MAX_POOL_LIST (sizeof (mPoolSizeTable) / sizeof (mPoolSizeTable[0]))
62
63 #define MAX_POOL_SIZE (MAX_ADDRESS - POOL_OVERHEAD)
64
65 //
66 // Globals
67 //
68
69 #define POOL_SIGNATURE SIGNATURE_32('p','l','s','t')
70 typedef struct {
71 INTN Signature;
72 UINTN Used;
73 EFI_MEMORY_TYPE MemoryType;
74 LIST_ENTRY FreeList[MAX_POOL_LIST];
75 LIST_ENTRY Link;
76 } POOL;
77
78 //
79 // Pool header for each memory type.
80 //
81 POOL mPoolHead[EfiMaxMemoryType];
82
83 //
84 // List of pool header to search for the appropriate memory type.
85 //
86 LIST_ENTRY mPoolHeadList = INITIALIZE_LIST_HEAD_VARIABLE (mPoolHeadList);
87
88 /**
89 Get pool size table index from the specified size.
90
91 @param Size The specified size to get index from pool table.
92
93 @return The index of pool size table.
94
95 **/
96 STATIC
97 UINTN
98 GetPoolIndexFromSize (
99 UINTN Size
100 )
101 {
102 UINTN Index;
103
104 for (Index = 0; Index < MAX_POOL_LIST; Index++) {
105 if (mPoolSizeTable [Index] >= Size) {
106 return Index;
107 }
108 }
109 return MAX_POOL_LIST;
110 }
111
112 /**
113 Called to initialize the pool.
114
115 **/
116 VOID
117 CoreInitializePool (
118 VOID
119 )
120 {
121 UINTN Type;
122 UINTN Index;
123
124 for (Type=0; Type < EfiMaxMemoryType; Type++) {
125 mPoolHead[Type].Signature = 0;
126 mPoolHead[Type].Used = 0;
127 mPoolHead[Type].MemoryType = (EFI_MEMORY_TYPE) Type;
128 for (Index=0; Index < MAX_POOL_LIST; Index++) {
129 InitializeListHead (&mPoolHead[Type].FreeList[Index]);
130 }
131 }
132 }
133
134
135 /**
136 Look up pool head for specified memory type.
137
138 @param MemoryType Memory type of which pool head is looked for
139
140 @return Pointer of Corresponding pool head.
141
142 **/
143 POOL *
144 LookupPoolHead (
145 IN EFI_MEMORY_TYPE MemoryType
146 )
147 {
148 LIST_ENTRY *Link;
149 POOL *Pool;
150 UINTN Index;
151
152 if ((UINT32)MemoryType < EfiMaxMemoryType) {
153 return &mPoolHead[MemoryType];
154 }
155
156 //
157 // MemoryType values in the range 0x80000000..0xFFFFFFFF are reserved for use by UEFI
158 // OS loaders that are provided by operating system vendors
159 //
160 if ((INT32)MemoryType < 0) {
161
162 for (Link = mPoolHeadList.ForwardLink; Link != &mPoolHeadList; Link = Link->ForwardLink) {
163 Pool = CR(Link, POOL, Link, POOL_SIGNATURE);
164 if (Pool->MemoryType == MemoryType) {
165 return Pool;
166 }
167 }
168
169 Pool = CoreAllocatePoolI (EfiBootServicesData, sizeof (POOL));
170 if (Pool == NULL) {
171 return NULL;
172 }
173
174 Pool->Signature = POOL_SIGNATURE;
175 Pool->Used = 0;
176 Pool->MemoryType = MemoryType;
177 for (Index=0; Index < MAX_POOL_LIST; Index++) {
178 InitializeListHead (&Pool->FreeList[Index]);
179 }
180
181 InsertHeadList (&mPoolHeadList, &Pool->Link);
182
183 return Pool;
184 }
185
186 return NULL;
187 }
188
189
190
191 /**
192 Allocate pool of a particular type.
193
194 @param PoolType Type of pool to allocate
195 @param Size The amount of pool to allocate
196 @param Buffer The address to return a pointer to the allocated
197 pool
198
199 @retval EFI_INVALID_PARAMETER PoolType not valid or Buffer is NULL.
200 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
201 @retval EFI_SUCCESS Pool successfully allocated.
202
203 **/
204 EFI_STATUS
205 EFIAPI
206 CoreInternalAllocatePool (
207 IN EFI_MEMORY_TYPE PoolType,
208 IN UINTN Size,
209 OUT VOID **Buffer
210 )
211 {
212 EFI_STATUS Status;
213
214 //
215 // If it's not a valid type, fail it
216 //
217 if ((PoolType >= EfiMaxMemoryType && PoolType <= 0x7fffffff) ||
218 PoolType == EfiConventionalMemory) {
219 return EFI_INVALID_PARAMETER;
220 }
221
222 if (Buffer == NULL) {
223 return EFI_INVALID_PARAMETER;
224 }
225
226 *Buffer = NULL;
227
228 //
229 // If size is too large, fail it
230 // Base on the EFI spec, return status of EFI_OUT_OF_RESOURCES
231 //
232 if (Size > MAX_POOL_SIZE) {
233 return EFI_OUT_OF_RESOURCES;
234 }
235
236 //
237 // Acquire the memory lock and make the allocation
238 //
239 Status = CoreAcquireLockOrFail (&gMemoryLock);
240 if (EFI_ERROR (Status)) {
241 return EFI_OUT_OF_RESOURCES;
242 }
243
244 *Buffer = CoreAllocatePoolI (PoolType, Size);
245 CoreReleaseMemoryLock ();
246 return (*Buffer != NULL) ? EFI_SUCCESS : EFI_OUT_OF_RESOURCES;
247 }
248
249 /**
250 Allocate pool of a particular type.
251
252 @param PoolType Type of pool to allocate
253 @param Size The amount of pool to allocate
254 @param Buffer The address to return a pointer to the allocated
255 pool
256
257 @retval EFI_INVALID_PARAMETER PoolType not valid or Buffer is NULL.
258 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
259 @retval EFI_SUCCESS Pool successfully allocated.
260
261 **/
262 EFI_STATUS
263 EFIAPI
264 CoreAllocatePool (
265 IN EFI_MEMORY_TYPE PoolType,
266 IN UINTN Size,
267 OUT VOID **Buffer
268 )
269 {
270 EFI_STATUS Status;
271
272 Status = CoreInternalAllocatePool (PoolType, Size, Buffer);
273 if (!EFI_ERROR (Status)) {
274 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionAllocatePool, PoolType, Size, *Buffer);
275 }
276 return Status;
277 }
278
279 /**
280 Internal function to allocate pool of a particular type.
281 Caller must have the memory lock held
282
283 @param PoolType Type of pool to allocate
284 @param Size The amount of pool to allocate
285
286 @return The allocate pool, or NULL
287
288 **/
289 VOID *
290 CoreAllocatePoolI (
291 IN EFI_MEMORY_TYPE PoolType,
292 IN UINTN Size
293 )
294 {
295 POOL *Pool;
296 POOL_FREE *Free;
297 POOL_HEAD *Head;
298 POOL_TAIL *Tail;
299 CHAR8 *NewPage;
300 VOID *Buffer;
301 UINTN Index;
302 UINTN FSize;
303 UINTN Offset, MaxOffset;
304 UINTN NoPages;
305 UINTN Granularity;
306
307 ASSERT_LOCKED (&gMemoryLock);
308
309 if (PoolType == EfiACPIReclaimMemory ||
310 PoolType == EfiACPIMemoryNVS ||
311 PoolType == EfiRuntimeServicesCode ||
312 PoolType == EfiRuntimeServicesData) {
313
314 Granularity = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;
315 } else {
316 Granularity = DEFAULT_PAGE_ALLOCATION;
317 }
318
319 //
320 // Adjust the size by the pool header & tail overhead
321 //
322
323 //
324 // Adjusting the Size to be of proper alignment so that
325 // we don't get an unaligned access fault later when
326 // pool_Tail is being initialized
327 //
328 Size = ALIGN_VARIABLE (Size);
329
330 Size += POOL_OVERHEAD;
331 Index = SIZE_TO_LIST(Size);
332 Pool = LookupPoolHead (PoolType);
333 if (Pool== NULL) {
334 return NULL;
335 }
336 Head = NULL;
337
338 //
339 // If allocation is over max size, just allocate pages for the request
340 // (slow)
341 //
342 if (Index >= SIZE_TO_LIST (Granularity)) {
343 NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (Granularity) - 1;
344 NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (Granularity) - 1);
345 Head = CoreAllocatePoolPages (PoolType, NoPages, Granularity);
346 goto Done;
347 }
348
349 //
350 // If there's no free pool in the proper list size, go get some more pages
351 //
352 if (IsListEmpty (&Pool->FreeList[Index])) {
353
354 Offset = LIST_TO_SIZE (Index);
355 MaxOffset = Granularity;
356
357 //
358 // Check the bins holding larger blocks, and carve one up if needed
359 //
360 while (++Index < SIZE_TO_LIST (Granularity)) {
361 if (!IsListEmpty (&Pool->FreeList[Index])) {
362 Free = CR (Pool->FreeList[Index].ForwardLink, POOL_FREE, Link, POOL_FREE_SIGNATURE);
363 RemoveEntryList (&Free->Link);
364 NewPage = (VOID *) Free;
365 MaxOffset = LIST_TO_SIZE (Index);
366 goto Carve;
367 }
368 }
369
370 //
371 // Get another page
372 //
373 NewPage = CoreAllocatePoolPages(PoolType, EFI_SIZE_TO_PAGES (Granularity), Granularity);
374 if (NewPage == NULL) {
375 goto Done;
376 }
377
378 //
379 // Serve the allocation request from the head of the allocated block
380 //
381 Carve:
382 Head = (POOL_HEAD *) NewPage;
383
384 //
385 // Carve up remaining space into free pool blocks
386 //
387 Index--;
388 while (Offset < MaxOffset) {
389 ASSERT (Index < MAX_POOL_LIST);
390 FSize = LIST_TO_SIZE(Index);
391
392 while (Offset + FSize <= MaxOffset) {
393 Free = (POOL_FREE *) &NewPage[Offset];
394 Free->Signature = POOL_FREE_SIGNATURE;
395 Free->Index = (UINT32)Index;
396 InsertHeadList (&Pool->FreeList[Index], &Free->Link);
397 Offset += FSize;
398 }
399 Index -= 1;
400 }
401
402 ASSERT (Offset == MaxOffset);
403 goto Done;
404 }
405
406 //
407 // Remove entry from free pool list
408 //
409 Free = CR (Pool->FreeList[Index].ForwardLink, POOL_FREE, Link, POOL_FREE_SIGNATURE);
410 RemoveEntryList (&Free->Link);
411
412 Head = (POOL_HEAD *) Free;
413
414 Done:
415 Buffer = NULL;
416
417 if (Head != NULL) {
418
419 //
420 // If we have a pool buffer, fill in the header & tail info
421 //
422 Head->Signature = POOL_HEAD_SIGNATURE;
423 Head->Size = Size;
424 Head->Type = (EFI_MEMORY_TYPE) PoolType;
425 Tail = HEAD_TO_TAIL (Head);
426 Tail->Signature = POOL_TAIL_SIGNATURE;
427 Tail->Size = Size;
428 Buffer = Head->Data;
429 DEBUG_CLEAR_MEMORY (Buffer, Size - POOL_OVERHEAD);
430
431 DEBUG ((
432 DEBUG_POOL,
433 "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType,
434 Buffer,
435 (UINT64)(Size - POOL_OVERHEAD),
436 (UINT64) Pool->Used
437 ));
438
439 //
440 // Account the allocation
441 //
442 Pool->Used += Size;
443
444 } else {
445 DEBUG ((DEBUG_ERROR | DEBUG_POOL, "AllocatePool: failed to allocate %ld bytes\n", (UINT64) Size));
446 }
447
448 return Buffer;
449 }
450
451
452
453 /**
454 Frees pool.
455
456 @param Buffer The allocated pool entry to free
457
458 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
459 @retval EFI_SUCCESS Pool successfully freed.
460
461 **/
462 EFI_STATUS
463 EFIAPI
464 CoreInternalFreePool (
465 IN VOID *Buffer
466 )
467 {
468 EFI_STATUS Status;
469
470 if (Buffer == NULL) {
471 return EFI_INVALID_PARAMETER;
472 }
473
474 CoreAcquireMemoryLock ();
475 Status = CoreFreePoolI (Buffer);
476 CoreReleaseMemoryLock ();
477 return Status;
478 }
479
480 /**
481 Frees pool.
482
483 @param Buffer The allocated pool entry to free
484
485 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
486 @retval EFI_SUCCESS Pool successfully freed.
487
488 **/
489 EFI_STATUS
490 EFIAPI
491 CoreFreePool (
492 IN VOID *Buffer
493 )
494 {
495 EFI_STATUS Status;
496
497 Status = CoreInternalFreePool (Buffer);
498 if (!EFI_ERROR (Status)) {
499 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionFreePool, (EFI_MEMORY_TYPE) 0, 0, Buffer);
500 }
501 return Status;
502 }
503
504 /**
505 Internal function to free a pool entry.
506 Caller must have the memory lock held
507
508 @param Buffer The allocated pool entry to free
509
510 @retval EFI_INVALID_PARAMETER Buffer not valid
511 @retval EFI_SUCCESS Buffer successfully freed.
512
513 **/
514 EFI_STATUS
515 CoreFreePoolI (
516 IN VOID *Buffer
517 )
518 {
519 POOL *Pool;
520 POOL_HEAD *Head;
521 POOL_TAIL *Tail;
522 POOL_FREE *Free;
523 UINTN Index;
524 UINTN NoPages;
525 UINTN Size;
526 CHAR8 *NewPage;
527 UINTN Offset;
528 BOOLEAN AllFree;
529 UINTN Granularity;
530
531 ASSERT(Buffer != NULL);
532 //
533 // Get the head & tail of the pool entry
534 //
535 Head = CR (Buffer, POOL_HEAD, Data, POOL_HEAD_SIGNATURE);
536 ASSERT(Head != NULL);
537
538 if (Head->Signature != POOL_HEAD_SIGNATURE) {
539 return EFI_INVALID_PARAMETER;
540 }
541
542 Tail = HEAD_TO_TAIL (Head);
543 ASSERT(Tail != NULL);
544
545 //
546 // Debug
547 //
548 ASSERT (Tail->Signature == POOL_TAIL_SIGNATURE);
549 ASSERT (Head->Size == Tail->Size);
550 ASSERT_LOCKED (&gMemoryLock);
551
552 if (Tail->Signature != POOL_TAIL_SIGNATURE) {
553 return EFI_INVALID_PARAMETER;
554 }
555
556 if (Head->Size != Tail->Size) {
557 return EFI_INVALID_PARAMETER;
558 }
559
560 //
561 // Determine the pool type and account for it
562 //
563 Size = Head->Size;
564 Pool = LookupPoolHead (Head->Type);
565 if (Pool == NULL) {
566 return EFI_INVALID_PARAMETER;
567 }
568 Pool->Used -= Size;
569 DEBUG ((DEBUG_POOL, "FreePool: %p (len %lx) %,ld\n", Head->Data, (UINT64)(Head->Size - POOL_OVERHEAD), (UINT64) Pool->Used));
570
571 if (Head->Type == EfiACPIReclaimMemory ||
572 Head->Type == EfiACPIMemoryNVS ||
573 Head->Type == EfiRuntimeServicesCode ||
574 Head->Type == EfiRuntimeServicesData) {
575
576 Granularity = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;
577 } else {
578 Granularity = DEFAULT_PAGE_ALLOCATION;
579 }
580
581 //
582 // Determine the pool list
583 //
584 Index = SIZE_TO_LIST(Size);
585 DEBUG_CLEAR_MEMORY (Head, Size);
586
587 //
588 // If it's not on the list, it must be pool pages
589 //
590 if (Index >= SIZE_TO_LIST (Granularity)) {
591
592 //
593 // Return the memory pages back to free memory
594 //
595 NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (Granularity) - 1;
596 NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (Granularity) - 1);
597 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN) Head, NoPages);
598
599 } else {
600
601 //
602 // Put the pool entry onto the free pool list
603 //
604 Free = (POOL_FREE *) Head;
605 ASSERT(Free != NULL);
606 Free->Signature = POOL_FREE_SIGNATURE;
607 Free->Index = (UINT32)Index;
608 InsertHeadList (&Pool->FreeList[Index], &Free->Link);
609
610 //
611 // See if all the pool entries in the same page as Free are freed pool
612 // entries
613 //
614 NewPage = (CHAR8 *)((UINTN)Free & ~(Granularity - 1));
615 Free = (POOL_FREE *) &NewPage[0];
616 ASSERT(Free != NULL);
617
618 if (Free->Signature == POOL_FREE_SIGNATURE) {
619
620 AllFree = TRUE;
621 Offset = 0;
622
623 while ((Offset < Granularity) && (AllFree)) {
624 Free = (POOL_FREE *) &NewPage[Offset];
625 ASSERT(Free != NULL);
626 if (Free->Signature != POOL_FREE_SIGNATURE) {
627 AllFree = FALSE;
628 }
629 Offset += LIST_TO_SIZE(Free->Index);
630 }
631
632 if (AllFree) {
633
634 //
635 // All of the pool entries in the same page as Free are free pool
636 // entries
637 // Remove all of these pool entries from the free loop lists.
638 //
639 Free = (POOL_FREE *) &NewPage[0];
640 ASSERT(Free != NULL);
641 Offset = 0;
642
643 while (Offset < Granularity) {
644 Free = (POOL_FREE *) &NewPage[Offset];
645 ASSERT(Free != NULL);
646 RemoveEntryList (&Free->Link);
647 Offset += LIST_TO_SIZE(Free->Index);
648 }
649
650 //
651 // Free the page
652 //
653 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN)NewPage, EFI_SIZE_TO_PAGES (Granularity));
654 }
655 }
656 }
657
658 //
659 // If this is an OS specific memory type, then check to see if the last
660 // portion of that memory type has been freed. If it has, then free the
661 // list entry for that memory type
662 //
663 if ((INT32)Pool->MemoryType < 0 && Pool->Used == 0) {
664 RemoveEntryList (&Pool->Link);
665 CoreFreePoolI (Pool);
666 }
667
668 return EFI_SUCCESS;
669 }
670