]> git.proxmox.com Git - mirror_edk2.git/blob - StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c
StandaloneMmPkg/MemoryAllocationLib: Add MM memory allocation library.
[mirror_edk2.git] / StandaloneMmPkg / Library / StandaloneMmCoreMemoryAllocationLib / StandaloneMmCoreMemoryAllocationLib.c
1 /** @file
2 Support routines for memory allocation routines based on Standalone MM Core internal functions.
3
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
6
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include <PiMm.h>
18
19 #include <Guid/MmramMemoryReserve.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/HobLib.h>
24 #include "StandaloneMmCoreMemoryAllocationServices.h"
25
26 EFI_MM_SYSTEM_TABLE *gMmst = NULL;
27
28 /**
29 Allocates one or more 4KB pages of a certain memory type.
30
31 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
32 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.
33 If there is not enough memory remaining to satisfy the request, then NULL is returned.
34
35 @param MemoryType The type of memory to allocate.
36 @param Pages The number of 4 KB pages to allocate.
37
38 @return A pointer to the allocated buffer or NULL if allocation fails.
39
40 **/
41 VOID *
42 InternalAllocatePages (
43 IN EFI_MEMORY_TYPE MemoryType,
44 IN UINTN Pages
45 )
46 {
47 EFI_STATUS Status;
48 EFI_PHYSICAL_ADDRESS Memory;
49
50 if (Pages == 0) {
51 return NULL;
52 }
53
54 Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);
55 if (EFI_ERROR (Status)) {
56 return NULL;
57 }
58 return (VOID *) (UINTN) Memory;
59 }
60
61 /**
62 Allocates one or more 4KB pages of type EfiBootServicesData.
63
64 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
65 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
66 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
67 returned.
68
69 @param Pages The number of 4 KB pages to allocate.
70
71 @return A pointer to the allocated buffer or NULL if allocation fails.
72
73 **/
74 VOID *
75 EFIAPI
76 AllocatePages (
77 IN UINTN Pages
78 )
79 {
80 return InternalAllocatePages (EfiRuntimeServicesData, Pages);
81 }
82
83 /**
84 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
85
86 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
87 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
88 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
89 returned.
90
91 @param Pages The number of 4 KB pages to allocate.
92
93 @return A pointer to the allocated buffer or NULL if allocation fails.
94
95 **/
96 VOID *
97 EFIAPI
98 AllocateRuntimePages (
99 IN UINTN Pages
100 )
101 {
102 return InternalAllocatePages (EfiRuntimeServicesData, Pages);
103 }
104
105 /**
106 Allocates one or more 4KB pages of type EfiReservedMemoryType.
107
108 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
109 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
110 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
111 returned.
112
113 @param Pages The number of 4 KB pages to allocate.
114
115 @return A pointer to the allocated buffer or NULL if allocation fails.
116
117 **/
118 VOID *
119 EFIAPI
120 AllocateReservedPages (
121 IN UINTN Pages
122 )
123 {
124 return NULL;
125 }
126
127 /**
128 Frees one or more 4KB pages that were previously allocated with one of the page allocation
129 functions in the Memory Allocation Library.
130
131 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
132 must have been allocated on a previous call to the page allocation services of the Memory
133 Allocation Library. If it is not possible to free allocated pages, then this function will
134 perform no actions.
135
136 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
137 then ASSERT().
138 If Pages is zero, then ASSERT().
139
140 @param Buffer Pointer to the buffer of pages to free.
141 @param Pages The number of 4 KB pages to free.
142
143 **/
144 VOID
145 EFIAPI
146 FreePages (
147 IN VOID *Buffer,
148 IN UINTN Pages
149 )
150 {
151 EFI_STATUS Status;
152
153 ASSERT (Pages != 0);
154 Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);
155 ASSERT_EFI_ERROR (Status);
156 }
157
158 /**
159 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
160
161 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
162 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
163 If there is not enough memory at the specified alignment remaining to satisfy the request, then
164 NULL is returned.
165 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
166 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
167
168 @param MemoryType The type of memory to allocate.
169 @param Pages The number of 4 KB pages to allocate.
170 @param Alignment The requested alignment of the allocation. Must be a power of two.
171 If Alignment is zero, then byte alignment is used.
172
173 @return A pointer to the allocated buffer or NULL if allocation fails.
174
175 **/
176 VOID *
177 InternalAllocateAlignedPages (
178 IN EFI_MEMORY_TYPE MemoryType,
179 IN UINTN Pages,
180 IN UINTN Alignment
181 )
182 {
183 EFI_STATUS Status;
184 EFI_PHYSICAL_ADDRESS Memory;
185 UINTN AlignedMemory;
186 UINTN AlignmentMask;
187 UINTN UnalignedPages;
188 UINTN RealPages;
189
190 //
191 // Alignment must be a power of two or zero.
192 //
193 ASSERT ((Alignment & (Alignment - 1)) == 0);
194
195 if (Pages == 0) {
196 return NULL;
197 }
198 if (Alignment > EFI_PAGE_SIZE) {
199 //
200 // Calculate the total number of pages since alignment is larger than page size.
201 //
202 AlignmentMask = Alignment - 1;
203 RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment);
204 //
205 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
206 //
207 ASSERT (RealPages > Pages);
208
209 Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);
210 if (EFI_ERROR (Status)) {
211 return NULL;
212 }
213 AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;
214 UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);
215 if (UnalignedPages > 0) {
216 //
217 // Free first unaligned page(s).
218 //
219 Status = gMmst->MmFreePages (Memory, UnalignedPages);
220 ASSERT_EFI_ERROR (Status);
221 }
222 Memory = (EFI_PHYSICAL_ADDRESS) (AlignedMemory + EFI_PAGES_TO_SIZE (Pages));
223 UnalignedPages = RealPages - Pages - UnalignedPages;
224 if (UnalignedPages > 0) {
225 //
226 // Free last unaligned page(s).
227 //
228 Status = gMmst->MmFreePages (Memory, UnalignedPages);
229 ASSERT_EFI_ERROR (Status);
230 }
231 } else {
232 //
233 // Do not over-allocate pages in this case.
234 //
235 Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);
236 if (EFI_ERROR (Status)) {
237 return NULL;
238 }
239 AlignedMemory = (UINTN) Memory;
240 }
241 return (VOID *) AlignedMemory;
242 }
243
244 /**
245 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
246
247 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
248 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
249 returned. If there is not enough memory at the specified alignment remaining to satisfy the
250 request, then NULL is returned.
251
252 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
253 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
254
255 @param Pages The number of 4 KB pages to allocate.
256 @param Alignment The requested alignment of the allocation. Must be a power of two.
257 If Alignment is zero, then byte alignment is used.
258
259 @return A pointer to the allocated buffer or NULL if allocation fails.
260
261 **/
262 VOID *
263 EFIAPI
264 AllocateAlignedPages (
265 IN UINTN Pages,
266 IN UINTN Alignment
267 )
268 {
269 return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);
270 }
271
272 /**
273 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
274
275 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
276 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
277 returned. If there is not enough memory at the specified alignment remaining to satisfy the
278 request, then NULL is returned.
279
280 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
281 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
282
283 @param Pages The number of 4 KB pages to allocate.
284 @param Alignment The requested alignment of the allocation. Must be a power of two.
285 If Alignment is zero, then byte alignment is used.
286
287 @return A pointer to the allocated buffer or NULL if allocation fails.
288
289 **/
290 VOID *
291 EFIAPI
292 AllocateAlignedRuntimePages (
293 IN UINTN Pages,
294 IN UINTN Alignment
295 )
296 {
297 return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);
298 }
299
300 /**
301 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
302
303 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
304 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
305 returned. If there is not enough memory at the specified alignment remaining to satisfy the
306 request, then NULL is returned.
307
308 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
309 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
310
311 @param Pages The number of 4 KB pages to allocate.
312 @param Alignment The requested alignment of the allocation. Must be a power of two.
313 If Alignment is zero, then byte alignment is used.
314
315 @return A pointer to the allocated buffer or NULL if allocation fails.
316
317 **/
318 VOID *
319 EFIAPI
320 AllocateAlignedReservedPages (
321 IN UINTN Pages,
322 IN UINTN Alignment
323 )
324 {
325 return NULL;
326 }
327
328 /**
329 Frees one or more 4KB pages that were previously allocated with one of the aligned page
330 allocation functions in the Memory Allocation Library.
331
332 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
333 must have been allocated on a previous call to the aligned page allocation services of the Memory
334 Allocation Library. If it is not possible to free allocated pages, then this function will
335 perform no actions.
336
337 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
338 Library, then ASSERT().
339 If Pages is zero, then ASSERT().
340
341 @param Buffer Pointer to the buffer of pages to free.
342 @param Pages The number of 4 KB pages to free.
343
344 **/
345 VOID
346 EFIAPI
347 FreeAlignedPages (
348 IN VOID *Buffer,
349 IN UINTN Pages
350 )
351 {
352 EFI_STATUS Status;
353
354 ASSERT (Pages != 0);
355 Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);
356 ASSERT_EFI_ERROR (Status);
357 }
358
359 /**
360 Allocates a buffer of a certain pool type.
361
362 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
363 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
364 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
365
366 @param MemoryType The type of memory to allocate.
367 @param AllocationSize The number of bytes to allocate.
368
369 @return A pointer to the allocated buffer or NULL if allocation fails.
370
371 **/
372 VOID *
373 InternalAllocatePool (
374 IN EFI_MEMORY_TYPE MemoryType,
375 IN UINTN AllocationSize
376 )
377 {
378 EFI_STATUS Status;
379 VOID *Memory;
380
381 Memory = NULL;
382
383 Status = gMmst->MmAllocatePool (MemoryType, AllocationSize, &Memory);
384 if (EFI_ERROR (Status)) {
385 Memory = NULL;
386 }
387 return Memory;
388 }
389
390 /**
391 Allocates a buffer of type EfiBootServicesData.
392
393 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
394 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
395 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
396
397 @param AllocationSize The number of bytes to allocate.
398
399 @return A pointer to the allocated buffer or NULL if allocation fails.
400
401 **/
402 VOID *
403 EFIAPI
404 AllocatePool (
405 IN UINTN AllocationSize
406 )
407 {
408 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);
409 }
410
411 /**
412 Allocates a buffer of type EfiRuntimeServicesData.
413
414 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
415 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
416 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
417
418 @param AllocationSize The number of bytes to allocate.
419
420 @return A pointer to the allocated buffer or NULL if allocation fails.
421
422 **/
423 VOID *
424 EFIAPI
425 AllocateRuntimePool (
426 IN UINTN AllocationSize
427 )
428 {
429 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);
430 }
431
432 /**
433 Allocates a buffer of type EfiReservedMemoryType.
434
435 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns
436 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
437 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
438
439 @param AllocationSize The number of bytes to allocate.
440
441 @return A pointer to the allocated buffer or NULL if allocation fails.
442
443 **/
444 VOID *
445 EFIAPI
446 AllocateReservedPool (
447 IN UINTN AllocationSize
448 )
449 {
450 return NULL;
451 }
452
453 /**
454 Allocates and zeros a buffer of a certain pool type.
455
456 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer
457 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
458 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
459 then NULL is returned.
460
461 @param PoolType The type of memory to allocate.
462 @param AllocationSize The number of bytes to allocate and zero.
463
464 @return A pointer to the allocated buffer or NULL if allocation fails.
465
466 **/
467 VOID *
468 InternalAllocateZeroPool (
469 IN EFI_MEMORY_TYPE PoolType,
470 IN UINTN AllocationSize
471 )
472 {
473 VOID *Memory;
474
475 Memory = InternalAllocatePool (PoolType, AllocationSize);
476 if (Memory != NULL) {
477 Memory = ZeroMem (Memory, AllocationSize);
478 }
479 return Memory;
480 }
481
482 /**
483 Allocates and zeros a buffer of type EfiBootServicesData.
484
485 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
486 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
487 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
488 request, then NULL is returned.
489
490 @param AllocationSize The number of bytes to allocate and zero.
491
492 @return A pointer to the allocated buffer or NULL if allocation fails.
493
494 **/
495 VOID *
496 EFIAPI
497 AllocateZeroPool (
498 IN UINTN AllocationSize
499 )
500 {
501 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);
502 }
503
504 /**
505 Allocates and zeros a buffer of type EfiRuntimeServicesData.
506
507 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
508 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
509 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
510 request, then NULL is returned.
511
512 @param AllocationSize The number of bytes to allocate and zero.
513
514 @return A pointer to the allocated buffer or NULL if allocation fails.
515
516 **/
517 VOID *
518 EFIAPI
519 AllocateRuntimeZeroPool (
520 IN UINTN AllocationSize
521 )
522 {
523 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);
524 }
525
526 /**
527 Allocates and zeros a buffer of type EfiReservedMemoryType.
528
529 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
530 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
531 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
532 request, then NULL is returned.
533
534 @param AllocationSize The number of bytes to allocate and zero.
535
536 @return A pointer to the allocated buffer or NULL if allocation fails.
537
538 **/
539 VOID *
540 EFIAPI
541 AllocateReservedZeroPool (
542 IN UINTN AllocationSize
543 )
544 {
545 return NULL;
546 }
547
548 /**
549 Copies a buffer to an allocated buffer of a certain pool type.
550
551 Allocates the number bytes specified by AllocationSize of a certain pool type, copies
552 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
553 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
554 is not enough memory remaining to satisfy the request, then NULL is returned.
555 If Buffer is NULL, then ASSERT().
556 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
557
558 @param PoolType The type of pool to allocate.
559 @param AllocationSize The number of bytes to allocate and zero.
560 @param Buffer The buffer to copy to the allocated buffer.
561
562 @return A pointer to the allocated buffer or NULL if allocation fails.
563
564 **/
565 VOID *
566 InternalAllocateCopyPool (
567 IN EFI_MEMORY_TYPE PoolType,
568 IN UINTN AllocationSize,
569 IN CONST VOID *Buffer
570 )
571 {
572 VOID *Memory;
573
574 ASSERT (Buffer != NULL);
575 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));
576
577 Memory = InternalAllocatePool (PoolType, AllocationSize);
578 if (Memory != NULL) {
579 Memory = CopyMem (Memory, Buffer, AllocationSize);
580 }
581 return Memory;
582 }
583
584 /**
585 Copies a buffer to an allocated buffer of type EfiBootServicesData.
586
587 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
588 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
589 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
590 is not enough memory remaining to satisfy the request, then NULL is returned.
591
592 If Buffer is NULL, then ASSERT().
593 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
594
595 @param AllocationSize The number of bytes to allocate and zero.
596 @param Buffer The buffer to copy to the allocated buffer.
597
598 @return A pointer to the allocated buffer or NULL if allocation fails.
599
600 **/
601 VOID *
602 EFIAPI
603 AllocateCopyPool (
604 IN UINTN AllocationSize,
605 IN CONST VOID *Buffer
606 )
607 {
608 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);
609 }
610
611 /**
612 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
613
614 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
615 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
616 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
617 is not enough memory remaining to satisfy the request, then NULL is returned.
618
619 If Buffer is NULL, then ASSERT().
620 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
621
622 @param AllocationSize The number of bytes to allocate and zero.
623 @param Buffer The buffer to copy to the allocated buffer.
624
625 @return A pointer to the allocated buffer or NULL if allocation fails.
626
627 **/
628 VOID *
629 EFIAPI
630 AllocateRuntimeCopyPool (
631 IN UINTN AllocationSize,
632 IN CONST VOID *Buffer
633 )
634 {
635 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);
636 }
637
638 /**
639 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
640
641 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
642 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
643 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
644 is not enough memory remaining to satisfy the request, then NULL is returned.
645
646 If Buffer is NULL, then ASSERT().
647 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
648
649 @param AllocationSize The number of bytes to allocate and zero.
650 @param Buffer The buffer to copy to the allocated buffer.
651
652 @return A pointer to the allocated buffer or NULL if allocation fails.
653
654 **/
655 VOID *
656 EFIAPI
657 AllocateReservedCopyPool (
658 IN UINTN AllocationSize,
659 IN CONST VOID *Buffer
660 )
661 {
662 return NULL;
663 }
664
665 /**
666 Reallocates a buffer of a specified memory type.
667
668 Allocates and zeros the number bytes specified by NewSize from memory of the type
669 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and
670 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
671 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
672 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
673 enough memory remaining to satisfy the request, then NULL is returned.
674
675 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
676 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
677
678 @param PoolType The type of pool to allocate.
679 @param OldSize The size, in bytes, of OldBuffer.
680 @param NewSize The size, in bytes, of the buffer to reallocate.
681 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
682 parameter that may be NULL.
683
684 @return A pointer to the allocated buffer or NULL if allocation fails.
685
686 **/
687 VOID *
688 InternalReallocatePool (
689 IN EFI_MEMORY_TYPE PoolType,
690 IN UINTN OldSize,
691 IN UINTN NewSize,
692 IN VOID *OldBuffer OPTIONAL
693 )
694 {
695 VOID *NewBuffer;
696
697 NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);
698 if (NewBuffer != NULL && OldBuffer != NULL) {
699 CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));
700 FreePool (OldBuffer);
701 }
702 return NewBuffer;
703 }
704
705 /**
706 Reallocates a buffer of type EfiBootServicesData.
707
708 Allocates and zeros the number bytes specified by NewSize from memory of type
709 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
710 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
711 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
712 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
713 enough memory remaining to satisfy the request, then NULL is returned.
714
715 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
716 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
717
718 @param OldSize The size, in bytes, of OldBuffer.
719 @param NewSize The size, in bytes, of the buffer to reallocate.
720 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
721 parameter that may be NULL.
722
723 @return A pointer to the allocated buffer or NULL if allocation fails.
724
725 **/
726 VOID *
727 EFIAPI
728 ReallocatePool (
729 IN UINTN OldSize,
730 IN UINTN NewSize,
731 IN VOID *OldBuffer OPTIONAL
732 )
733 {
734 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);
735 }
736
737 /**
738 Reallocates a buffer of type EfiRuntimeServicesData.
739
740 Allocates and zeros the number bytes specified by NewSize from memory of type
741 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
742 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
743 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
744 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
745 enough memory remaining to satisfy the request, then NULL is returned.
746
747 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
748 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
749
750 @param OldSize The size, in bytes, of OldBuffer.
751 @param NewSize The size, in bytes, of the buffer to reallocate.
752 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
753 parameter that may be NULL.
754
755 @return A pointer to the allocated buffer or NULL if allocation fails.
756
757 **/
758 VOID *
759 EFIAPI
760 ReallocateRuntimePool (
761 IN UINTN OldSize,
762 IN UINTN NewSize,
763 IN VOID *OldBuffer OPTIONAL
764 )
765 {
766 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);
767 }
768
769 /**
770 Reallocates a buffer of type EfiReservedMemoryType.
771
772 Allocates and zeros the number bytes specified by NewSize from memory of type
773 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and
774 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
775 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
776 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
777 enough memory remaining to satisfy the request, then NULL is returned.
778
779 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
780 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
781
782 @param OldSize The size, in bytes, of OldBuffer.
783 @param NewSize The size, in bytes, of the buffer to reallocate.
784 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
785 parameter that may be NULL.
786
787 @return A pointer to the allocated buffer or NULL if allocation fails.
788
789 **/
790 VOID *
791 EFIAPI
792 ReallocateReservedPool (
793 IN UINTN OldSize,
794 IN UINTN NewSize,
795 IN VOID *OldBuffer OPTIONAL
796 )
797 {
798 return NULL;
799 }
800
801 /**
802 Frees a buffer that was previously allocated with one of the pool allocation functions in the
803 Memory Allocation Library.
804
805 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
806 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
807 resources, then this function will perform no actions.
808
809 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
810 then ASSERT().
811
812 @param Buffer Pointer to the buffer to free.
813
814 **/
815 VOID
816 EFIAPI
817 FreePool (
818 IN VOID *Buffer
819 )
820 {
821 EFI_STATUS Status;
822
823 Status = gMmst->MmFreePool (Buffer);
824 ASSERT_EFI_ERROR (Status);
825 }
826
827 /**
828 The constructor function calls MmInitializeMemoryServices to initialize
829 memory in MMRAM and caches EFI_MM_SYSTEM_TABLE pointer.
830
831 @param ImageHandle The firmware allocated handle for the EFI image.
832 @param SystemTable A pointer to the Management mode System Table.
833
834 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
835
836 **/
837 EFI_STATUS
838 EFIAPI
839 MemoryAllocationLibConstructor (
840 IN EFI_HANDLE ImageHandle,
841 IN EFI_MM_SYSTEM_TABLE *MmSystemTable
842 )
843 {
844 MM_CORE_PRIVATE_DATA *MmCorePrivate;
845 EFI_HOB_GUID_TYPE *GuidHob;
846 MM_CORE_DATA_HOB_DATA *DataInHob;
847 VOID *HobStart;
848 EFI_MMRAM_HOB_DESCRIPTOR_BLOCK *MmramRangesHobData;
849 EFI_MMRAM_DESCRIPTOR *MmramRanges;
850 UINT32 MmramRangeCount;
851 EFI_HOB_GUID_TYPE *MmramRangesHob;
852
853 HobStart = GetHobList ();
854 DEBUG ((DEBUG_INFO, "StandaloneMmCoreMemoryAllocationLibConstructor - 0x%x\n", HobStart));
855
856 //
857 // Extract MM Core Private context from the Hob. If absent search for
858 // a Hob containing the MMRAM ranges
859 //
860 GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);
861 if (GuidHob == NULL) {
862 MmramRangesHob = GetNextGuidHob (&gEfiMmPeiMmramMemoryReserveGuid, HobStart);
863 if (MmramRangesHob == NULL) {
864 return EFI_UNSUPPORTED;
865 }
866
867 MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);
868 if (MmramRangesHobData == NULL) {
869 return EFI_UNSUPPORTED;
870 }
871
872 MmramRanges = MmramRangesHobData->Descriptor;
873 if (MmramRanges == NULL) {
874 return EFI_UNSUPPORTED;
875 }
876
877 MmramRangeCount = MmramRangesHobData->NumberOfMmReservedRegions;
878 if (MmramRanges == NULL) {
879 return EFI_UNSUPPORTED;
880 }
881
882 } else {
883 DataInHob = GET_GUID_HOB_DATA (GuidHob);
884 MmCorePrivate = (MM_CORE_PRIVATE_DATA *)(UINTN)DataInHob->Address;
885 MmramRanges = (EFI_MMRAM_DESCRIPTOR *)(UINTN)MmCorePrivate->MmramRanges;
886 MmramRangeCount = MmCorePrivate->MmramRangeCount;
887 }
888
889 {
890 UINTN Index;
891
892 DEBUG ((DEBUG_INFO, "MmramRangeCount - 0x%x\n", MmramRangeCount));
893 for (Index = 0; Index < MmramRangeCount; Index++) {
894 DEBUG ((DEBUG_INFO, "MmramRanges[%d]: 0x%016lx - 0x%016lx\n",
895 Index, MmramRanges[Index].CpuStart, MmramRanges[Index].PhysicalSize));
896 }
897 }
898
899 //
900 // Initialize memory service using free MMRAM
901 //
902 DEBUG ((DEBUG_INFO, "MmInitializeMemoryServices\n"));
903 MmInitializeMemoryServices ((UINTN)MmramRangeCount, (VOID *)(UINTN)MmramRanges);
904
905 // Initialize MM Services Table
906 gMmst = MmSystemTable;
907 return EFI_SUCCESS;
908 }