2 DxeServicesLib memory allocation routines
4 Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/HobLib.h>
12 #include <Library/UefiBootServicesTableLib.h>
13 #include <Library/DxeServicesLib.h>
16 Allocates one or more 4KB pages of a given type from a memory region that is
19 Allocates the number of 4KB pages of type 'MemoryType' and returns a
20 pointer to the allocated buffer. The buffer returned is aligned on a 4KB
21 boundary. If Pages is 0, then NULL is returned. If there is not enough
22 memory remaining to satisfy the request, then NULL is returned.
24 @param[in] MemoryType The memory type to allocate
25 @param[in] Pages The number of 4 KB pages to allocate.
27 @return A pointer to the allocated buffer or NULL if allocation fails.
32 AllocatePeiAccessiblePages (
33 IN EFI_MEMORY_TYPE MemoryType
,
38 EFI_ALLOCATE_TYPE AllocType
;
39 EFI_PHYSICAL_ADDRESS Memory
;
40 EFI_HOB_HANDOFF_INFO_TABLE
*PhitHob
;
46 AllocType
= AllocateAnyPages
;
48 // A X64 build of DXE may be combined with a 32-bit build of PEI, and so we
49 // need to check the memory limit set by PEI, and allocate below 4 GB if the
50 // limit is set to 4 GB or lower.
52 PhitHob
= (EFI_HOB_HANDOFF_INFO_TABLE
*)GetHobList ();
53 if (PhitHob
->EfiFreeMemoryTop
<= MAX_UINT32
) {
54 AllocType
= AllocateMaxAddress
;
58 Status
= gBS
->AllocatePages (AllocType
, MemoryType
, Pages
, &Memory
);
59 if (EFI_ERROR (Status
)) {
62 return (VOID
*)(UINTN
)Memory
;