2 Support routines for memory allocation routines based on SMM Core internal functions.
4 Copyright (c) 2006 - 2015, 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
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.
17 #include <Protocol/SmmAccess2.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
22 #include "PiSmmCoreMemoryAllocationServices.h"
24 EFI_SMRAM_DESCRIPTOR
*mSmramRanges
= NULL
;
25 UINTN mSmramRangeCount
= 0;
28 This function gets and caches SMRAM ranges that are present in the system.
30 It will ASSERT() if SMM Access2 Protocol doesn't exist.
31 It will ASSERT() if SMRAM ranges can't be got.
32 It will ASSERT() if Resource can't be allocated for cache SMRAM range.
42 EFI_SMM_ACCESS2_PROTOCOL
*SmmAccess
;
46 // Locate SMM Access2 Protocol
48 Status
= gBS
->LocateProtocol (
49 &gEfiSmmAccess2ProtocolGuid
,
53 ASSERT_EFI_ERROR (Status
);
56 // Get SMRAM range information
59 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, NULL
);
60 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
62 mSmramRanges
= (EFI_SMRAM_DESCRIPTOR
*) AllocatePool (Size
);
63 ASSERT (mSmramRanges
!= NULL
);
65 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, mSmramRanges
);
66 ASSERT_EFI_ERROR (Status
);
68 mSmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
72 Check whether the start address of buffer is within any of the SMRAM ranges.
74 @param[in] Buffer The pointer to the buffer to be checked.
76 @retval TURE The buffer is in SMRAM ranges.
77 @retval FALSE The buffer is out of SMRAM ranges.
87 if (mSmramRanges
== NULL
) {
89 // SMRAM ranges is not got. Try to get them all.
94 for (Index
= 0; Index
< mSmramRangeCount
; Index
++) {
95 if (((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
>= mSmramRanges
[Index
].CpuStart
) &&
96 ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
< (mSmramRanges
[Index
].CpuStart
+ mSmramRanges
[Index
].PhysicalSize
))) {
105 Allocates one or more 4KB pages of a certain memory type.
107 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
108 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.
109 If there is not enough memory remaining to satisfy the request, then NULL is returned.
111 @param MemoryType The type of memory to allocate.
112 @param Pages The number of 4 KB pages to allocate.
114 @return A pointer to the allocated buffer or NULL if allocation fails.
118 InternalAllocatePages (
119 IN EFI_MEMORY_TYPE MemoryType
,
124 EFI_PHYSICAL_ADDRESS Memory
;
130 Status
= SmmAllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
131 if (EFI_ERROR (Status
)) {
134 return (VOID
*) (UINTN
) Memory
;
138 Allocates one or more 4KB pages of type EfiBootServicesData.
140 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
141 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
142 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
145 @param Pages The number of 4 KB pages to allocate.
147 @return A pointer to the allocated buffer or NULL if allocation fails.
156 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
160 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
162 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
163 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
164 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
167 @param Pages The number of 4 KB pages to allocate.
169 @return A pointer to the allocated buffer or NULL if allocation fails.
174 AllocateRuntimePages (
178 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
182 Allocates one or more 4KB pages of type EfiReservedMemoryType.
184 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
185 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
186 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
189 @param Pages The number of 4 KB pages to allocate.
191 @return A pointer to the allocated buffer or NULL if allocation fails.
196 AllocateReservedPages (
204 Frees one or more 4KB pages that were previously allocated with one of the page allocation
205 functions in the Memory Allocation Library.
207 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
208 must have been allocated on a previous call to the page allocation services of the Memory
209 Allocation Library. If it is not possible to free allocated pages, then this function will
212 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
214 If Pages is zero, then ASSERT().
216 @param Buffer Pointer to the buffer of pages to free.
217 @param Pages The number of 4 KB pages to free.
230 if (BufferInSmram (Buffer
)) {
232 // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePages() service.
233 // So, SmmFreePages() service is used to free it.
235 Status
= SmmFreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
238 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.
239 // So, gBS->FreePages() service is used to free it.
241 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
243 ASSERT_EFI_ERROR (Status
);
247 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
249 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
250 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
251 If there is not enough memory at the specified alignment remaining to satisfy the request, then
253 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
254 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
256 @param MemoryType The type of memory to allocate.
257 @param Pages The number of 4 KB pages to allocate.
258 @param Alignment The requested alignment of the allocation. Must be a power of two.
259 If Alignment is zero, then byte alignment is used.
261 @return A pointer to the allocated buffer or NULL if allocation fails.
265 InternalAllocateAlignedPages (
266 IN EFI_MEMORY_TYPE MemoryType
,
272 EFI_PHYSICAL_ADDRESS Memory
;
275 UINTN UnalignedPages
;
279 // Alignment must be a power of two or zero.
281 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
286 if (Alignment
> EFI_PAGE_SIZE
) {
288 // Calculate the total number of pages since alignment is larger than page size.
290 AlignmentMask
= Alignment
- 1;
291 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
293 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
295 ASSERT (RealPages
> Pages
);
297 Status
= SmmAllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
298 if (EFI_ERROR (Status
)) {
301 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
302 UnalignedPages
= EFI_SIZE_TO_PAGES (AlignedMemory
- (UINTN
) Memory
);
303 if (UnalignedPages
> 0) {
305 // Free first unaligned page(s).
307 Status
= SmmFreePages (Memory
, UnalignedPages
);
308 ASSERT_EFI_ERROR (Status
);
310 Memory
= (EFI_PHYSICAL_ADDRESS
) (AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
));
311 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
312 if (UnalignedPages
> 0) {
314 // Free last unaligned page(s).
316 Status
= SmmFreePages (Memory
, UnalignedPages
);
317 ASSERT_EFI_ERROR (Status
);
321 // Do not over-allocate pages in this case.
323 Status
= SmmAllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
324 if (EFI_ERROR (Status
)) {
327 AlignedMemory
= (UINTN
) Memory
;
329 return (VOID
*) AlignedMemory
;
333 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
335 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
336 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
337 returned. If there is not enough memory at the specified alignment remaining to satisfy the
338 request, then NULL is returned.
340 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
341 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
343 @param Pages The number of 4 KB pages to allocate.
344 @param Alignment The requested alignment of the allocation. Must be a power of two.
345 If Alignment is zero, then byte alignment is used.
347 @return A pointer to the allocated buffer or NULL if allocation fails.
352 AllocateAlignedPages (
357 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
361 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
363 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
364 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
365 returned. If there is not enough memory at the specified alignment remaining to satisfy the
366 request, then NULL is returned.
368 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
369 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
371 @param Pages The number of 4 KB pages to allocate.
372 @param Alignment The requested alignment of the allocation. Must be a power of two.
373 If Alignment is zero, then byte alignment is used.
375 @return A pointer to the allocated buffer or NULL if allocation fails.
380 AllocateAlignedRuntimePages (
385 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
389 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
391 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
392 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
393 returned. If there is not enough memory at the specified alignment remaining to satisfy the
394 request, then NULL is returned.
396 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
397 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
399 @param Pages The number of 4 KB pages to allocate.
400 @param Alignment The requested alignment of the allocation. Must be a power of two.
401 If Alignment is zero, then byte alignment is used.
403 @return A pointer to the allocated buffer or NULL if allocation fails.
408 AllocateAlignedReservedPages (
417 Frees one or more 4KB pages that were previously allocated with one of the aligned page
418 allocation functions in the Memory Allocation Library.
420 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
421 must have been allocated on a previous call to the aligned page allocation services of the Memory
422 Allocation Library. If it is not possible to free allocated pages, then this function will
425 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
426 Library, then ASSERT().
427 If Pages is zero, then ASSERT().
429 @param Buffer Pointer to the buffer of pages to free.
430 @param Pages The number of 4 KB pages to free.
443 if (BufferInSmram (Buffer
)) {
445 // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePages() service.
446 // So, SmmFreePages() service is used to free it.
448 Status
= SmmFreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
451 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.
452 // So, gBS->FreePages() service is used to free it.
454 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
456 ASSERT_EFI_ERROR (Status
);
460 Allocates a buffer of a certain pool type.
462 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
463 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
464 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
466 @param MemoryType The type of memory to allocate.
467 @param AllocationSize The number of bytes to allocate.
469 @return A pointer to the allocated buffer or NULL if allocation fails.
473 InternalAllocatePool (
474 IN EFI_MEMORY_TYPE MemoryType
,
475 IN UINTN AllocationSize
483 Status
= SmmAllocatePool (MemoryType
, AllocationSize
, &Memory
);
484 if (EFI_ERROR (Status
)) {
491 Allocates a buffer of type EfiBootServicesData.
493 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
494 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
495 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
497 @param AllocationSize The number of bytes to allocate.
499 @return A pointer to the allocated buffer or NULL if allocation fails.
505 IN UINTN AllocationSize
508 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
512 Allocates a buffer of type EfiRuntimeServicesData.
514 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
515 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
516 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
518 @param AllocationSize The number of bytes to allocate.
520 @return A pointer to the allocated buffer or NULL if allocation fails.
525 AllocateRuntimePool (
526 IN UINTN AllocationSize
529 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
533 Allocates a buffer of type EfiReservedMemoryType.
535 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns
536 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
537 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
539 @param AllocationSize The number of bytes to allocate.
541 @return A pointer to the allocated buffer or NULL if allocation fails.
546 AllocateReservedPool (
547 IN UINTN AllocationSize
554 Allocates and zeros a buffer of a certain pool type.
556 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer
557 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
558 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
559 then NULL is returned.
561 @param PoolType The type of memory to allocate.
562 @param AllocationSize The number of bytes to allocate and zero.
564 @return A pointer to the allocated buffer or NULL if allocation fails.
568 InternalAllocateZeroPool (
569 IN EFI_MEMORY_TYPE PoolType
,
570 IN UINTN AllocationSize
575 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
576 if (Memory
!= NULL
) {
577 Memory
= ZeroMem (Memory
, AllocationSize
);
583 Allocates and zeros a buffer of type EfiBootServicesData.
585 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
586 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
587 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
588 request, then NULL is returned.
590 @param AllocationSize The number of bytes to allocate and zero.
592 @return A pointer to the allocated buffer or NULL if allocation fails.
598 IN UINTN AllocationSize
601 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
605 Allocates and zeros a buffer of type EfiRuntimeServicesData.
607 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
608 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
609 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
610 request, then NULL is returned.
612 @param AllocationSize The number of bytes to allocate and zero.
614 @return A pointer to the allocated buffer or NULL if allocation fails.
619 AllocateRuntimeZeroPool (
620 IN UINTN AllocationSize
623 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
627 Allocates and zeros a buffer of type EfiReservedMemoryType.
629 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
630 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
631 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
632 request, then NULL is returned.
634 @param AllocationSize The number of bytes to allocate and zero.
636 @return A pointer to the allocated buffer or NULL if allocation fails.
641 AllocateReservedZeroPool (
642 IN UINTN AllocationSize
649 Copies a buffer to an allocated buffer of a certain pool type.
651 Allocates the number bytes specified by AllocationSize of a certain pool type, copies
652 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
653 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
654 is not enough memory remaining to satisfy the request, then NULL is returned.
655 If Buffer is NULL, then ASSERT().
656 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
658 @param PoolType The type of pool to allocate.
659 @param AllocationSize The number of bytes to allocate and zero.
660 @param Buffer The buffer to copy to the allocated buffer.
662 @return A pointer to the allocated buffer or NULL if allocation fails.
666 InternalAllocateCopyPool (
667 IN EFI_MEMORY_TYPE PoolType
,
668 IN UINTN AllocationSize
,
669 IN CONST VOID
*Buffer
674 ASSERT (Buffer
!= NULL
);
675 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
677 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
678 if (Memory
!= NULL
) {
679 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
685 Copies a buffer to an allocated buffer of type EfiBootServicesData.
687 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
688 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
689 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
690 is not enough memory remaining to satisfy the request, then NULL is returned.
692 If Buffer is NULL, then ASSERT().
693 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
695 @param AllocationSize The number of bytes to allocate and zero.
696 @param Buffer The buffer to copy to the allocated buffer.
698 @return A pointer to the allocated buffer or NULL if allocation fails.
704 IN UINTN AllocationSize
,
705 IN CONST VOID
*Buffer
708 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
712 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
714 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
715 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
716 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
717 is not enough memory remaining to satisfy the request, then NULL is returned.
719 If Buffer is NULL, then ASSERT().
720 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
722 @param AllocationSize The number of bytes to allocate and zero.
723 @param Buffer The buffer to copy to the allocated buffer.
725 @return A pointer to the allocated buffer or NULL if allocation fails.
730 AllocateRuntimeCopyPool (
731 IN UINTN AllocationSize
,
732 IN CONST VOID
*Buffer
735 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
739 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
741 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
742 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
743 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
744 is not enough memory remaining to satisfy the request, then NULL is returned.
746 If Buffer is NULL, then ASSERT().
747 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
749 @param AllocationSize The number of bytes to allocate and zero.
750 @param Buffer The buffer to copy to the allocated buffer.
752 @return A pointer to the allocated buffer or NULL if allocation fails.
757 AllocateReservedCopyPool (
758 IN UINTN AllocationSize
,
759 IN CONST VOID
*Buffer
766 Reallocates a buffer of a specified memory type.
768 Allocates and zeros the number bytes specified by NewSize from memory of the type
769 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and
770 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
771 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
772 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
773 enough memory remaining to satisfy the request, then NULL is returned.
775 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
776 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
778 @param PoolType The type of pool to allocate.
779 @param OldSize The size, in bytes, of OldBuffer.
780 @param NewSize The size, in bytes, of the buffer to reallocate.
781 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
782 parameter that may be NULL.
784 @return A pointer to the allocated buffer or NULL if allocation fails.
788 InternalReallocatePool (
789 IN EFI_MEMORY_TYPE PoolType
,
792 IN VOID
*OldBuffer OPTIONAL
797 NewBuffer
= InternalAllocateZeroPool (PoolType
, NewSize
);
798 if (NewBuffer
!= NULL
&& OldBuffer
!= NULL
) {
799 CopyMem (NewBuffer
, OldBuffer
, MIN (OldSize
, NewSize
));
800 FreePool (OldBuffer
);
806 Reallocates a buffer of type EfiBootServicesData.
808 Allocates and zeros the number bytes specified by NewSize from memory of type
809 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
810 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
811 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
812 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
813 enough memory remaining to satisfy the request, then NULL is returned.
815 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
816 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
818 @param OldSize The size, in bytes, of OldBuffer.
819 @param NewSize The size, in bytes, of the buffer to reallocate.
820 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
821 parameter that may be NULL.
823 @return A pointer to the allocated buffer or NULL if allocation fails.
831 IN VOID
*OldBuffer OPTIONAL
834 return InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
838 Reallocates a buffer of type EfiRuntimeServicesData.
840 Allocates and zeros the number bytes specified by NewSize from memory of type
841 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
842 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
843 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
844 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
845 enough memory remaining to satisfy the request, then NULL is returned.
847 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
848 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
850 @param OldSize The size, in bytes, of OldBuffer.
851 @param NewSize The size, in bytes, of the buffer to reallocate.
852 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
853 parameter that may be NULL.
855 @return A pointer to the allocated buffer or NULL if allocation fails.
860 ReallocateRuntimePool (
863 IN VOID
*OldBuffer OPTIONAL
866 return InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
870 Reallocates a buffer of type EfiReservedMemoryType.
872 Allocates and zeros the number bytes specified by NewSize from memory of type
873 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and
874 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
875 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
876 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
877 enough memory remaining to satisfy the request, then NULL is returned.
879 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
880 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
882 @param OldSize The size, in bytes, of OldBuffer.
883 @param NewSize The size, in bytes, of the buffer to reallocate.
884 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
885 parameter that may be NULL.
887 @return A pointer to the allocated buffer or NULL if allocation fails.
892 ReallocateReservedPool (
895 IN VOID
*OldBuffer OPTIONAL
902 Frees a buffer that was previously allocated with one of the pool allocation functions in the
903 Memory Allocation Library.
905 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
906 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
907 resources, then this function will perform no actions.
909 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
912 @param Buffer Pointer to the buffer to free.
923 if (BufferInSmram (Buffer
)) {
925 // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePool() service.
926 // So, SmmFreePool() service is used to free it.
928 Status
= SmmFreePool (Buffer
);
931 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePool() service.
932 // So, gBS->FreePool() service is used to free it.
934 Status
= gBS
->FreePool (Buffer
);
936 ASSERT_EFI_ERROR (Status
);
940 The constructor function calls SmmInitializeMemoryServices to initialize memory in SMRAM.
942 @param ImageHandle The firmware allocated handle for the EFI image.
943 @param SystemTable A pointer to the EFI System Table.
945 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
950 PiSmmCoreMemoryAllocationLibConstructor (
951 IN EFI_HANDLE ImageHandle
,
952 IN EFI_SYSTEM_TABLE
*SystemTable
955 SMM_CORE_PRIVATE_DATA
*SmmCorePrivate
;
957 SmmCorePrivate
= (SMM_CORE_PRIVATE_DATA
*)ImageHandle
;
959 // Initialize memory service using free SMRAM
961 SmmInitializeMemoryServices (SmmCorePrivate
->SmramRangeCount
, SmmCorePrivate
->SmramRanges
);