]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Mem/Pool.c
MdeModulePkg/DxeCore: Fixed compiler error 'enumerated type mixed with another type'
[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
45 #define POOL_SHIFT 7
46
47 #define POOL_OVERHEAD (SIZE_OF_POOL_HEAD + sizeof(POOL_TAIL))
48
49 #define HEAD_TO_TAIL(a) \
50 ((POOL_TAIL *) (((CHAR8 *) (a)) + (a)->Size - sizeof(POOL_TAIL)));
51
52
53 #define SIZE_TO_LIST(a) ((a) >> POOL_SHIFT)
54 #define LIST_TO_SIZE(a) ((a+1) << POOL_SHIFT)
55
56 #define MAX_POOL_LIST SIZE_TO_LIST(DEFAULT_PAGE_ALLOCATION)
57
58 #define MAX_POOL_SIZE (MAX_ADDRESS - POOL_OVERHEAD)
59
60 //
61 // Globals
62 //
63
64 #define POOL_SIGNATURE SIGNATURE_32('p','l','s','t')
65 typedef struct {
66 INTN Signature;
67 UINTN Used;
68 EFI_MEMORY_TYPE MemoryType;
69 LIST_ENTRY FreeList[MAX_POOL_LIST];
70 LIST_ENTRY Link;
71 } POOL;
72
73 //
74 // Pool header for each memory type.
75 //
76 POOL mPoolHead[EfiMaxMemoryType];
77
78 //
79 // List of pool header to search for the appropriate memory type.
80 //
81 LIST_ENTRY mPoolHeadList = INITIALIZE_LIST_HEAD_VARIABLE (mPoolHeadList);
82
83
84 /**
85 Called to initialize the pool.
86
87 **/
88 VOID
89 CoreInitializePool (
90 VOID
91 )
92 {
93 UINTN Type;
94 UINTN Index;
95
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]);
102 }
103 }
104 }
105
106
107 /**
108 Look up pool head for specified memory type.
109
110 @param MemoryType Memory type of which pool head is looked for
111
112 @return Pointer of Corresponding pool head.
113
114 **/
115 POOL *
116 LookupPoolHead (
117 IN EFI_MEMORY_TYPE MemoryType
118 )
119 {
120 LIST_ENTRY *Link;
121 POOL *Pool;
122 UINTN Index;
123
124 if ((UINT32)MemoryType < EfiMaxMemoryType) {
125 return &mPoolHead[MemoryType];
126 }
127
128 //
129 // MemoryType values in the range 0x80000000..0xFFFFFFFF are reserved for use by UEFI
130 // OS loaders that are provided by operating system vendors
131 //
132 if ((INT32)MemoryType < 0) {
133
134 for (Link = mPoolHeadList.ForwardLink; Link != &mPoolHeadList; Link = Link->ForwardLink) {
135 Pool = CR(Link, POOL, Link, POOL_SIGNATURE);
136 if (Pool->MemoryType == MemoryType) {
137 return Pool;
138 }
139 }
140
141 Pool = CoreAllocatePoolI (EfiBootServicesData, sizeof (POOL));
142 if (Pool == NULL) {
143 return NULL;
144 }
145
146 Pool->Signature = POOL_SIGNATURE;
147 Pool->Used = 0;
148 Pool->MemoryType = MemoryType;
149 for (Index=0; Index < MAX_POOL_LIST; Index++) {
150 InitializeListHead (&Pool->FreeList[Index]);
151 }
152
153 InsertHeadList (&mPoolHeadList, &Pool->Link);
154
155 return Pool;
156 }
157
158 return NULL;
159 }
160
161
162
163 /**
164 Allocate pool of a particular type.
165
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
169 pool
170
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.
174
175 **/
176 EFI_STATUS
177 EFIAPI
178 CoreInternalAllocatePool (
179 IN EFI_MEMORY_TYPE PoolType,
180 IN UINTN Size,
181 OUT VOID **Buffer
182 )
183 {
184 EFI_STATUS Status;
185
186 //
187 // If it's not a valid type, fail it
188 //
189 if ((PoolType >= EfiMaxMemoryType && PoolType <= 0x7fffffff) ||
190 PoolType == EfiConventionalMemory) {
191 return EFI_INVALID_PARAMETER;
192 }
193
194 if (Buffer == NULL) {
195 return EFI_INVALID_PARAMETER;
196 }
197
198 *Buffer = NULL;
199
200 //
201 // If size is too large, fail it
202 // Base on the EFI spec, return status of EFI_OUT_OF_RESOURCES
203 //
204 if (Size > MAX_POOL_SIZE) {
205 return EFI_OUT_OF_RESOURCES;
206 }
207
208 //
209 // Acquire the memory lock and make the allocation
210 //
211 Status = CoreAcquireLockOrFail (&gMemoryLock);
212 if (EFI_ERROR (Status)) {
213 return EFI_OUT_OF_RESOURCES;
214 }
215
216 *Buffer = CoreAllocatePoolI (PoolType, Size);
217 CoreReleaseMemoryLock ();
218 return (*Buffer != NULL) ? EFI_SUCCESS : EFI_OUT_OF_RESOURCES;
219 }
220
221 /**
222 Allocate pool of a particular type.
223
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
227 pool
228
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.
232
233 **/
234 EFI_STATUS
235 EFIAPI
236 CoreAllocatePool (
237 IN EFI_MEMORY_TYPE PoolType,
238 IN UINTN Size,
239 OUT VOID **Buffer
240 )
241 {
242 EFI_STATUS Status;
243
244 Status = CoreInternalAllocatePool (PoolType, Size, Buffer);
245 if (!EFI_ERROR (Status)) {
246 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionAllocatePool, PoolType, Size, *Buffer);
247 }
248 return Status;
249 }
250
251 /**
252 Internal function to allocate pool of a particular type.
253 Caller must have the memory lock held
254
255 @param PoolType Type of pool to allocate
256 @param Size The amount of pool to allocate
257
258 @return The allocate pool, or NULL
259
260 **/
261 VOID *
262 CoreAllocatePoolI (
263 IN EFI_MEMORY_TYPE PoolType,
264 IN UINTN Size
265 )
266 {
267 POOL *Pool;
268 POOL_FREE *Free;
269 POOL_HEAD *Head;
270 POOL_TAIL *Tail;
271 CHAR8 *NewPage;
272 VOID *Buffer;
273 UINTN Index;
274 UINTN FSize;
275 UINTN Offset;
276 UINTN NoPages;
277
278 ASSERT_LOCKED (&gMemoryLock);
279
280 //
281 // Adjust the size by the pool header & tail overhead
282 //
283
284 //
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
288 //
289 Size = ALIGN_VARIABLE (Size);
290
291 Size += POOL_OVERHEAD;
292 Index = SIZE_TO_LIST(Size);
293 Pool = LookupPoolHead (PoolType);
294 if (Pool== NULL) {
295 return NULL;
296 }
297 Head = NULL;
298
299 //
300 // If allocation is over max size, just allocate pages for the request
301 // (slow)
302 //
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);
307 goto Done;
308 }
309
310 //
311 // If there's no free pool in the proper list size, go get some more pages
312 //
313 if (IsListEmpty (&Pool->FreeList[Index])) {
314
315 //
316 // Get another page
317 //
318 NewPage = CoreAllocatePoolPages(PoolType, EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION), DEFAULT_PAGE_ALLOCATION);
319 if (NewPage == NULL) {
320 goto Done;
321 }
322
323 //
324 // Carve up new page into free pool blocks
325 //
326 Offset = 0;
327 while (Offset < DEFAULT_PAGE_ALLOCATION) {
328 ASSERT (Index < MAX_POOL_LIST);
329 FSize = LIST_TO_SIZE(Index);
330
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);
336 Offset += FSize;
337 }
338
339 Index -= 1;
340 }
341
342 ASSERT (Offset == DEFAULT_PAGE_ALLOCATION);
343 Index = SIZE_TO_LIST(Size);
344 }
345
346 //
347 // Remove entry from free pool list
348 //
349 Free = CR (Pool->FreeList[Index].ForwardLink, POOL_FREE, Link, POOL_FREE_SIGNATURE);
350 RemoveEntryList (&Free->Link);
351
352 Head = (POOL_HEAD *) Free;
353
354 Done:
355 Buffer = NULL;
356
357 if (Head != NULL) {
358
359 //
360 // If we have a pool buffer, fill in the header & tail info
361 //
362 Head->Signature = POOL_HEAD_SIGNATURE;
363 Head->Size = Size;
364 Head->Type = (EFI_MEMORY_TYPE) PoolType;
365 Tail = HEAD_TO_TAIL (Head);
366 Tail->Signature = POOL_TAIL_SIGNATURE;
367 Tail->Size = Size;
368 Buffer = Head->Data;
369 DEBUG_CLEAR_MEMORY (Buffer, Size - POOL_OVERHEAD);
370
371 DEBUG ((
372 DEBUG_POOL,
373 "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType,
374 Buffer,
375 (UINT64)(Size - POOL_OVERHEAD),
376 (UINT64) Pool->Used
377 ));
378
379 //
380 // Account the allocation
381 //
382 Pool->Used += Size;
383
384 } else {
385 DEBUG ((DEBUG_ERROR | DEBUG_POOL, "AllocatePool: failed to allocate %ld bytes\n", (UINT64) Size));
386 }
387
388 return Buffer;
389 }
390
391
392
393 /**
394 Frees pool.
395
396 @param Buffer The allocated pool entry to free
397
398 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
399 @retval EFI_SUCCESS Pool successfully freed.
400
401 **/
402 EFI_STATUS
403 EFIAPI
404 CoreInternalFreePool (
405 IN VOID *Buffer
406 )
407 {
408 EFI_STATUS Status;
409
410 if (Buffer == NULL) {
411 return EFI_INVALID_PARAMETER;
412 }
413
414 CoreAcquireMemoryLock ();
415 Status = CoreFreePoolI (Buffer);
416 CoreReleaseMemoryLock ();
417 return Status;
418 }
419
420 /**
421 Frees pool.
422
423 @param Buffer The allocated pool entry to free
424
425 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
426 @retval EFI_SUCCESS Pool successfully freed.
427
428 **/
429 EFI_STATUS
430 EFIAPI
431 CoreFreePool (
432 IN VOID *Buffer
433 )
434 {
435 EFI_STATUS Status;
436
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);
440 }
441 return Status;
442 }
443
444 /**
445 Internal function to free a pool entry.
446 Caller must have the memory lock held
447
448 @param Buffer The allocated pool entry to free
449
450 @retval EFI_INVALID_PARAMETER Buffer not valid
451 @retval EFI_SUCCESS Buffer successfully freed.
452
453 **/
454 EFI_STATUS
455 CoreFreePoolI (
456 IN VOID *Buffer
457 )
458 {
459 POOL *Pool;
460 POOL_HEAD *Head;
461 POOL_TAIL *Tail;
462 POOL_FREE *Free;
463 UINTN Index;
464 UINTN NoPages;
465 UINTN Size;
466 CHAR8 *NewPage;
467 UINTN FSize;
468 UINTN Offset;
469 BOOLEAN AllFree;
470
471 ASSERT(Buffer != NULL);
472 //
473 // Get the head & tail of the pool entry
474 //
475 Head = CR (Buffer, POOL_HEAD, Data, POOL_HEAD_SIGNATURE);
476 ASSERT(Head != NULL);
477
478 if (Head->Signature != POOL_HEAD_SIGNATURE) {
479 return EFI_INVALID_PARAMETER;
480 }
481
482 Tail = HEAD_TO_TAIL (Head);
483 ASSERT(Tail != NULL);
484
485 //
486 // Debug
487 //
488 ASSERT (Tail->Signature == POOL_TAIL_SIGNATURE);
489 ASSERT (Head->Size == Tail->Size);
490 ASSERT_LOCKED (&gMemoryLock);
491
492 if (Tail->Signature != POOL_TAIL_SIGNATURE) {
493 return EFI_INVALID_PARAMETER;
494 }
495
496 if (Head->Size != Tail->Size) {
497 return EFI_INVALID_PARAMETER;
498 }
499
500 //
501 // Determine the pool type and account for it
502 //
503 Size = Head->Size;
504 Pool = LookupPoolHead (Head->Type);
505 if (Pool == NULL) {
506 return EFI_INVALID_PARAMETER;
507 }
508 Pool->Used -= Size;
509 DEBUG ((DEBUG_POOL, "FreePool: %p (len %lx) %,ld\n", Head->Data, (UINT64)(Head->Size - POOL_OVERHEAD), (UINT64) Pool->Used));
510
511 //
512 // Determine the pool list
513 //
514 Index = SIZE_TO_LIST(Size);
515 DEBUG_CLEAR_MEMORY (Head, Size);
516
517 //
518 // If it's not on the list, it must be pool pages
519 //
520 if (Index >= MAX_POOL_LIST) {
521
522 //
523 // Return the memory pages back to free memory
524 //
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);
528
529 } else {
530
531 //
532 // Put the pool entry onto the free pool list
533 //
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);
539
540 //
541 // See if all the pool entries in the same page as Free are freed pool
542 // entries
543 //
544 NewPage = (CHAR8 *)((UINTN)Free & ~((DEFAULT_PAGE_ALLOCATION) -1));
545 Free = (POOL_FREE *) &NewPage[0];
546 ASSERT(Free != NULL);
547
548 if (Free->Signature == POOL_FREE_SIGNATURE) {
549
550 Index = Free->Index;
551
552 AllFree = TRUE;
553 Offset = 0;
554
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) {
561 AllFree = FALSE;
562 }
563 Offset += FSize;
564 }
565 Index -= 1;
566 }
567
568 if (AllFree) {
569
570 //
571 // All of the pool entries in the same page as Free are free pool
572 // entries
573 // Remove all of these pool entries from the free loop lists.
574 //
575 Free = (POOL_FREE *) &NewPage[0];
576 ASSERT(Free != NULL);
577 Index = Free->Index;
578 Offset = 0;
579
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);
586 Offset += FSize;
587 }
588 Index -= 1;
589 }
590
591 //
592 // Free the page
593 //
594 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN)NewPage, EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION));
595 }
596 }
597 }
598
599 //
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
603 //
604 if ((INT32)Pool->MemoryType < 0 && Pool->Used == 0) {
605 RemoveEntryList (&Pool->Link);
606 CoreFreePoolI (Pool);
607 }
608
609 return EFI_SUCCESS;
610 }
611