]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Mem/Pool.c
1) Add type cast for better coding style.
[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 CoreAllocatePool (
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
223 /**
224 Internal function to allocate pool of a particular type.
225 Caller must have the memory lock held
226
227 @param PoolType Type of pool to allocate
228 @param Size The amount of pool to allocate
229
230 @return The allocate pool, or NULL
231
232 **/
233 VOID *
234 CoreAllocatePoolI (
235 IN EFI_MEMORY_TYPE PoolType,
236 IN UINTN Size
237 )
238 {
239 POOL *Pool;
240 POOL_FREE *Free;
241 POOL_HEAD *Head;
242 POOL_TAIL *Tail;
243 CHAR8 *NewPage;
244 VOID *Buffer;
245 UINTN Index;
246 UINTN FSize;
247 UINTN Offset;
248 UINTN NoPages;
249
250 ASSERT_LOCKED (&gMemoryLock);
251
252 //
253 // Adjust the size by the pool header & tail overhead
254 //
255
256 //
257 // Adjusting the Size to be of proper alignment so that
258 // we don't get an unaligned access fault later when
259 // pool_Tail is being initialized
260 //
261 Size = ALIGN_VARIABLE (Size);
262
263 Size += POOL_OVERHEAD;
264 Index = SIZE_TO_LIST(Size);
265 Pool = LookupPoolHead (PoolType);
266 if (Pool== NULL) {
267 return NULL;
268 }
269 Head = NULL;
270
271 //
272 // If allocation is over max size, just allocate pages for the request
273 // (slow)
274 //
275 if (Index >= MAX_POOL_LIST) {
276 NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION) - 1;
277 NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION) - 1);
278 Head = CoreAllocatePoolPages (PoolType, NoPages, DEFAULT_PAGE_ALLOCATION);
279 goto Done;
280 }
281
282 //
283 // If there's no free pool in the proper list size, go get some more pages
284 //
285 if (IsListEmpty (&Pool->FreeList[Index])) {
286
287 //
288 // Get another page
289 //
290 NewPage = CoreAllocatePoolPages(PoolType, EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION), DEFAULT_PAGE_ALLOCATION);
291 if (NewPage == NULL) {
292 goto Done;
293 }
294
295 //
296 // Carve up new page into free pool blocks
297 //
298 Offset = 0;
299 while (Offset < DEFAULT_PAGE_ALLOCATION) {
300 ASSERT (Index < MAX_POOL_LIST);
301 FSize = LIST_TO_SIZE(Index);
302
303 while (Offset + FSize <= DEFAULT_PAGE_ALLOCATION) {
304 Free = (POOL_FREE *) &NewPage[Offset];
305 Free->Signature = POOL_FREE_SIGNATURE;
306 Free->Index = (UINT32)Index;
307 InsertHeadList (&Pool->FreeList[Index], &Free->Link);
308 Offset += FSize;
309 }
310
311 Index -= 1;
312 }
313
314 ASSERT (Offset == DEFAULT_PAGE_ALLOCATION);
315 Index = SIZE_TO_LIST(Size);
316 }
317
318 //
319 // Remove entry from free pool list
320 //
321 Free = CR (Pool->FreeList[Index].ForwardLink, POOL_FREE, Link, POOL_FREE_SIGNATURE);
322 RemoveEntryList (&Free->Link);
323
324 Head = (POOL_HEAD *) Free;
325
326 Done:
327 Buffer = NULL;
328
329 if (Head != NULL) {
330
331 //
332 // If we have a pool buffer, fill in the header & tail info
333 //
334 Head->Signature = POOL_HEAD_SIGNATURE;
335 Head->Size = Size;
336 Head->Type = (EFI_MEMORY_TYPE) PoolType;
337 Tail = HEAD_TO_TAIL (Head);
338 Tail->Signature = POOL_TAIL_SIGNATURE;
339 Tail->Size = Size;
340 Buffer = Head->Data;
341 DEBUG_CLEAR_MEMORY (Buffer, Size - POOL_OVERHEAD);
342
343 DEBUG ((
344 DEBUG_POOL,
345 "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType,
346 Buffer,
347 (UINT64)(Size - POOL_OVERHEAD),
348 (UINT64) Pool->Used
349 ));
350
351 //
352 // Account the allocation
353 //
354 Pool->Used += Size;
355
356 } else {
357 DEBUG ((DEBUG_ERROR | DEBUG_POOL, "AllocatePool: failed to allocate %ld bytes\n", (UINT64) Size));
358 }
359
360 return Buffer;
361 }
362
363
364
365 /**
366 Frees pool.
367
368 @param Buffer The allocated pool entry to free
369
370 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
371 @retval EFI_SUCCESS Pool successfully freed.
372
373 **/
374 EFI_STATUS
375 EFIAPI
376 CoreFreePool (
377 IN VOID *Buffer
378 )
379 {
380 EFI_STATUS Status;
381
382 if (Buffer == NULL) {
383 return EFI_INVALID_PARAMETER;
384 }
385
386 CoreAcquireMemoryLock ();
387 Status = CoreFreePoolI (Buffer);
388 CoreReleaseMemoryLock ();
389 return Status;
390 }
391
392
393
394 /**
395 Internal function to free a pool entry.
396 Caller must have the memory lock held
397
398 @param Buffer The allocated pool entry to free
399
400 @retval EFI_INVALID_PARAMETER Buffer not valid
401 @retval EFI_SUCCESS Buffer successfully freed.
402
403 **/
404 EFI_STATUS
405 CoreFreePoolI (
406 IN VOID *Buffer
407 )
408 {
409 POOL *Pool;
410 POOL_HEAD *Head;
411 POOL_TAIL *Tail;
412 POOL_FREE *Free;
413 UINTN Index;
414 UINTN NoPages;
415 UINTN Size;
416 CHAR8 *NewPage;
417 UINTN FSize;
418 UINTN Offset;
419 BOOLEAN AllFree;
420
421 ASSERT(Buffer != NULL);
422 //
423 // Get the head & tail of the pool entry
424 //
425 Head = CR (Buffer, POOL_HEAD, Data, POOL_HEAD_SIGNATURE);
426 ASSERT(Head != NULL);
427
428 if (Head->Signature != POOL_HEAD_SIGNATURE) {
429 return EFI_INVALID_PARAMETER;
430 }
431
432 Tail = HEAD_TO_TAIL (Head);
433 ASSERT(Tail != NULL);
434
435 //
436 // Debug
437 //
438 ASSERT (Tail->Signature == POOL_TAIL_SIGNATURE);
439 ASSERT (Head->Size == Tail->Size);
440 ASSERT_LOCKED (&gMemoryLock);
441
442 if (Tail->Signature != POOL_TAIL_SIGNATURE) {
443 return EFI_INVALID_PARAMETER;
444 }
445
446 if (Head->Size != Tail->Size) {
447 return EFI_INVALID_PARAMETER;
448 }
449
450 //
451 // Determine the pool type and account for it
452 //
453 Size = Head->Size;
454 Pool = LookupPoolHead (Head->Type);
455 if (Pool == NULL) {
456 return EFI_INVALID_PARAMETER;
457 }
458 Pool->Used -= Size;
459 DEBUG ((DEBUG_POOL, "FreePool: %p (len %lx) %,ld\n", Head->Data, (UINT64)(Head->Size - POOL_OVERHEAD), (UINT64) Pool->Used));
460
461 //
462 // Determine the pool list
463 //
464 Index = SIZE_TO_LIST(Size);
465 DEBUG_CLEAR_MEMORY (Head, Size);
466
467 //
468 // If it's not on the list, it must be pool pages
469 //
470 if (Index >= MAX_POOL_LIST) {
471
472 //
473 // Return the memory pages back to free memory
474 //
475 NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION) - 1;
476 NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION) - 1);
477 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN) Head, NoPages);
478
479 } else {
480
481 //
482 // Put the pool entry onto the free pool list
483 //
484 Free = (POOL_FREE *) Head;
485 ASSERT(Free != NULL);
486 Free->Signature = POOL_FREE_SIGNATURE;
487 Free->Index = (UINT32)Index;
488 InsertHeadList (&Pool->FreeList[Index], &Free->Link);
489
490 //
491 // See if all the pool entries in the same page as Free are freed pool
492 // entries
493 //
494 NewPage = (CHAR8 *)((UINTN)Free & ~((DEFAULT_PAGE_ALLOCATION) -1));
495 Free = (POOL_FREE *) &NewPage[0];
496 ASSERT(Free != NULL);
497
498 if (Free->Signature == POOL_FREE_SIGNATURE) {
499
500 Index = Free->Index;
501
502 AllFree = TRUE;
503 Offset = 0;
504
505 while ((Offset < DEFAULT_PAGE_ALLOCATION) && (AllFree)) {
506 FSize = LIST_TO_SIZE(Index);
507 while (Offset + FSize <= DEFAULT_PAGE_ALLOCATION) {
508 Free = (POOL_FREE *) &NewPage[Offset];
509 ASSERT(Free != NULL);
510 if (Free->Signature != POOL_FREE_SIGNATURE) {
511 AllFree = FALSE;
512 }
513 Offset += FSize;
514 }
515 Index -= 1;
516 }
517
518 if (AllFree) {
519
520 //
521 // All of the pool entries in the same page as Free are free pool
522 // entries
523 // Remove all of these pool entries from the free loop lists.
524 //
525 Free = (POOL_FREE *) &NewPage[0];
526 ASSERT(Free != NULL);
527 Index = Free->Index;
528 Offset = 0;
529
530 while (Offset < DEFAULT_PAGE_ALLOCATION) {
531 FSize = LIST_TO_SIZE(Index);
532 while (Offset + FSize <= DEFAULT_PAGE_ALLOCATION) {
533 Free = (POOL_FREE *) &NewPage[Offset];
534 ASSERT(Free != NULL);
535 RemoveEntryList (&Free->Link);
536 Offset += FSize;
537 }
538 Index -= 1;
539 }
540
541 //
542 // Free the page
543 //
544 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN)NewPage, EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION));
545 }
546 }
547 }
548
549 //
550 // If this is an OS specific memory type, then check to see if the last
551 // portion of that memory type has been freed. If it has, then free the
552 // list entry for that memory type
553 //
554 if ((INT32)Pool->MemoryType < 0 && Pool->Used == 0) {
555 RemoveEntryList (&Pool->Link);
556 CoreFreePoolI (Pool);
557 }
558
559 return EFI_SUCCESS;
560 }