2 Support routines for memory allocation routines based on Standalone MM Core internal functions.
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
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
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.
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"
26 EFI_MM_SYSTEM_TABLE
*gMmst
= NULL
;
29 Allocates one or more 4KB pages of a certain memory type.
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.
35 @param MemoryType The type of memory to allocate.
36 @param Pages The number of 4 KB pages to allocate.
38 @return A pointer to the allocated buffer or NULL if allocation fails.
42 InternalAllocatePages (
43 IN EFI_MEMORY_TYPE MemoryType
,
48 EFI_PHYSICAL_ADDRESS Memory
;
54 Status
= gMmst
->MmAllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
55 if (EFI_ERROR (Status
)) {
58 return (VOID
*) (UINTN
) Memory
;
62 Allocates one or more 4KB pages of type EfiBootServicesData.
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
69 @param Pages The number of 4 KB pages to allocate.
71 @return A pointer to the allocated buffer or NULL if allocation fails.
80 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
84 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
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
91 @param Pages The number of 4 KB pages to allocate.
93 @return A pointer to the allocated buffer or NULL if allocation fails.
98 AllocateRuntimePages (
102 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
106 Allocates one or more 4KB pages of type EfiReservedMemoryType.
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
113 @param Pages The number of 4 KB pages to allocate.
115 @return A pointer to the allocated buffer or NULL if allocation fails.
120 AllocateReservedPages (
128 Frees one or more 4KB pages that were previously allocated with one of the page allocation
129 functions in the Memory Allocation Library.
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
136 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
138 If Pages is zero, then ASSERT().
140 @param Buffer Pointer to the buffer of pages to free.
141 @param Pages The number of 4 KB pages to free.
154 Status
= gMmst
->MmFreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
155 ASSERT_EFI_ERROR (Status
);
159 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
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
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().
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.
173 @return A pointer to the allocated buffer or NULL if allocation fails.
177 InternalAllocateAlignedPages (
178 IN EFI_MEMORY_TYPE MemoryType
,
184 EFI_PHYSICAL_ADDRESS Memory
;
187 UINTN UnalignedPages
;
191 // Alignment must be a power of two or zero.
193 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
198 if (Alignment
> EFI_PAGE_SIZE
) {
200 // Calculate the total number of pages since alignment is larger than page size.
202 AlignmentMask
= Alignment
- 1;
203 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
205 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
207 ASSERT (RealPages
> Pages
);
209 Status
= gMmst
->MmAllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
210 if (EFI_ERROR (Status
)) {
213 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
214 UnalignedPages
= EFI_SIZE_TO_PAGES (AlignedMemory
- (UINTN
) Memory
);
215 if (UnalignedPages
> 0) {
217 // Free first unaligned page(s).
219 Status
= gMmst
->MmFreePages (Memory
, UnalignedPages
);
220 ASSERT_EFI_ERROR (Status
);
222 Memory
= (EFI_PHYSICAL_ADDRESS
) (AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
));
223 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
224 if (UnalignedPages
> 0) {
226 // Free last unaligned page(s).
228 Status
= gMmst
->MmFreePages (Memory
, UnalignedPages
);
229 ASSERT_EFI_ERROR (Status
);
233 // Do not over-allocate pages in this case.
235 Status
= gMmst
->MmAllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
236 if (EFI_ERROR (Status
)) {
239 AlignedMemory
= (UINTN
) Memory
;
241 return (VOID
*) AlignedMemory
;
245 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
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.
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().
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.
259 @return A pointer to the allocated buffer or NULL if allocation fails.
264 AllocateAlignedPages (
269 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
273 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
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.
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().
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.
287 @return A pointer to the allocated buffer or NULL if allocation fails.
292 AllocateAlignedRuntimePages (
297 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
301 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
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.
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().
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.
315 @return A pointer to the allocated buffer or NULL if allocation fails.
320 AllocateAlignedReservedPages (
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.
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
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().
341 @param Buffer Pointer to the buffer of pages to free.
342 @param Pages The number of 4 KB pages to free.
355 Status
= gMmst
->MmFreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
356 ASSERT_EFI_ERROR (Status
);
360 Allocates a buffer of a certain pool type.
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.
366 @param MemoryType The type of memory to allocate.
367 @param AllocationSize The number of bytes to allocate.
369 @return A pointer to the allocated buffer or NULL if allocation fails.
373 InternalAllocatePool (
374 IN EFI_MEMORY_TYPE MemoryType
,
375 IN UINTN AllocationSize
383 Status
= gMmst
->MmAllocatePool (MemoryType
, AllocationSize
, &Memory
);
384 if (EFI_ERROR (Status
)) {
391 Allocates a buffer of type EfiBootServicesData.
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.
397 @param AllocationSize The number of bytes to allocate.
399 @return A pointer to the allocated buffer or NULL if allocation fails.
405 IN UINTN AllocationSize
408 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
412 Allocates a buffer of type EfiRuntimeServicesData.
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.
418 @param AllocationSize The number of bytes to allocate.
420 @return A pointer to the allocated buffer or NULL if allocation fails.
425 AllocateRuntimePool (
426 IN UINTN AllocationSize
429 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
433 Allocates a buffer of type EfiReservedMemoryType.
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.
439 @param AllocationSize The number of bytes to allocate.
441 @return A pointer to the allocated buffer or NULL if allocation fails.
446 AllocateReservedPool (
447 IN UINTN AllocationSize
454 Allocates and zeros a buffer of a certain pool type.
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.
461 @param PoolType The type of memory to allocate.
462 @param AllocationSize The number of bytes to allocate and zero.
464 @return A pointer to the allocated buffer or NULL if allocation fails.
468 InternalAllocateZeroPool (
469 IN EFI_MEMORY_TYPE PoolType
,
470 IN UINTN AllocationSize
475 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
476 if (Memory
!= NULL
) {
477 Memory
= ZeroMem (Memory
, AllocationSize
);
483 Allocates and zeros a buffer of type EfiBootServicesData.
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.
490 @param AllocationSize The number of bytes to allocate and zero.
492 @return A pointer to the allocated buffer or NULL if allocation fails.
498 IN UINTN AllocationSize
501 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
505 Allocates and zeros a buffer of type EfiRuntimeServicesData.
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.
512 @param AllocationSize The number of bytes to allocate and zero.
514 @return A pointer to the allocated buffer or NULL if allocation fails.
519 AllocateRuntimeZeroPool (
520 IN UINTN AllocationSize
523 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
527 Allocates and zeros a buffer of type EfiReservedMemoryType.
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.
534 @param AllocationSize The number of bytes to allocate and zero.
536 @return A pointer to the allocated buffer or NULL if allocation fails.
541 AllocateReservedZeroPool (
542 IN UINTN AllocationSize
549 Copies a buffer to an allocated buffer of a certain pool type.
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().
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.
562 @return A pointer to the allocated buffer or NULL if allocation fails.
566 InternalAllocateCopyPool (
567 IN EFI_MEMORY_TYPE PoolType
,
568 IN UINTN AllocationSize
,
569 IN CONST VOID
*Buffer
574 ASSERT (Buffer
!= NULL
);
575 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
577 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
578 if (Memory
!= NULL
) {
579 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
585 Copies a buffer to an allocated buffer of type EfiBootServicesData.
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.
592 If Buffer is NULL, then ASSERT().
593 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
595 @param AllocationSize The number of bytes to allocate and zero.
596 @param Buffer The buffer to copy to the allocated buffer.
598 @return A pointer to the allocated buffer or NULL if allocation fails.
604 IN UINTN AllocationSize
,
605 IN CONST VOID
*Buffer
608 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
612 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
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.
619 If Buffer is NULL, then ASSERT().
620 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
622 @param AllocationSize The number of bytes to allocate and zero.
623 @param Buffer The buffer to copy to the allocated buffer.
625 @return A pointer to the allocated buffer or NULL if allocation fails.
630 AllocateRuntimeCopyPool (
631 IN UINTN AllocationSize
,
632 IN CONST VOID
*Buffer
635 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
639 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
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.
646 If Buffer is NULL, then ASSERT().
647 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
649 @param AllocationSize The number of bytes to allocate and zero.
650 @param Buffer The buffer to copy to the allocated buffer.
652 @return A pointer to the allocated buffer or NULL if allocation fails.
657 AllocateReservedCopyPool (
658 IN UINTN AllocationSize
,
659 IN CONST VOID
*Buffer
666 Reallocates a buffer of a specified memory type.
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.
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().
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.
684 @return A pointer to the allocated buffer or NULL if allocation fails.
688 InternalReallocatePool (
689 IN EFI_MEMORY_TYPE PoolType
,
692 IN VOID
*OldBuffer OPTIONAL
697 NewBuffer
= InternalAllocateZeroPool (PoolType
, NewSize
);
698 if (NewBuffer
!= NULL
&& OldBuffer
!= NULL
) {
699 CopyMem (NewBuffer
, OldBuffer
, MIN (OldSize
, NewSize
));
700 FreePool (OldBuffer
);
706 Reallocates a buffer of type EfiBootServicesData.
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.
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().
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.
723 @return A pointer to the allocated buffer or NULL if allocation fails.
731 IN VOID
*OldBuffer OPTIONAL
734 return InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
738 Reallocates a buffer of type EfiRuntimeServicesData.
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.
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().
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.
755 @return A pointer to the allocated buffer or NULL if allocation fails.
760 ReallocateRuntimePool (
763 IN VOID
*OldBuffer OPTIONAL
766 return InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
770 Reallocates a buffer of type EfiReservedMemoryType.
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.
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().
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.
787 @return A pointer to the allocated buffer or NULL if allocation fails.
792 ReallocateReservedPool (
795 IN VOID
*OldBuffer OPTIONAL
802 Frees a buffer that was previously allocated with one of the pool allocation functions in the
803 Memory Allocation Library.
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.
809 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
812 @param Buffer Pointer to the buffer to free.
823 Status
= gMmst
->MmFreePool (Buffer
);
824 ASSERT_EFI_ERROR (Status
);
828 The constructor function calls MmInitializeMemoryServices to initialize
829 memory in MMRAM and caches EFI_MM_SYSTEM_TABLE pointer.
831 @param ImageHandle The firmware allocated handle for the EFI image.
832 @param SystemTable A pointer to the Management mode System Table.
834 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
839 MemoryAllocationLibConstructor (
840 IN EFI_HANDLE ImageHandle
,
841 IN EFI_MM_SYSTEM_TABLE
*MmSystemTable
844 MM_CORE_PRIVATE_DATA
*MmCorePrivate
;
845 EFI_HOB_GUID_TYPE
*GuidHob
;
846 MM_CORE_DATA_HOB_DATA
*DataInHob
;
848 EFI_MMRAM_HOB_DESCRIPTOR_BLOCK
*MmramRangesHobData
;
849 EFI_MMRAM_DESCRIPTOR
*MmramRanges
;
850 UINT32 MmramRangeCount
;
851 EFI_HOB_GUID_TYPE
*MmramRangesHob
;
853 HobStart
= GetHobList ();
854 DEBUG ((DEBUG_INFO
, "StandaloneMmCoreMemoryAllocationLibConstructor - 0x%x\n", HobStart
));
857 // Extract MM Core Private context from the Hob. If absent search for
858 // a Hob containing the MMRAM ranges
860 GuidHob
= GetNextGuidHob (&gMmCoreDataHobGuid
, HobStart
);
861 if (GuidHob
== NULL
) {
862 MmramRangesHob
= GetNextGuidHob (&gEfiMmPeiMmramMemoryReserveGuid
, HobStart
);
863 if (MmramRangesHob
== NULL
) {
864 return EFI_UNSUPPORTED
;
867 MmramRangesHobData
= GET_GUID_HOB_DATA (MmramRangesHob
);
868 if (MmramRangesHobData
== NULL
) {
869 return EFI_UNSUPPORTED
;
872 MmramRanges
= MmramRangesHobData
->Descriptor
;
873 if (MmramRanges
== NULL
) {
874 return EFI_UNSUPPORTED
;
877 MmramRangeCount
= MmramRangesHobData
->NumberOfMmReservedRegions
;
878 if (MmramRanges
== NULL
) {
879 return EFI_UNSUPPORTED
;
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
;
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
));
900 // Initialize memory service using free MMRAM
902 DEBUG ((DEBUG_INFO
, "MmInitializeMemoryServices\n"));
903 MmInitializeMemoryServices ((UINTN
)MmramRangeCount
, (VOID
*)(UINTN
)MmramRanges
);
905 // Initialize MM Services Table
906 gMmst
= MmSystemTable
;