2 DxeServicesLib memory allocation routines
4 Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>
6 This program and the accompanying materials are licensed and made available
7 under the terms and conditions of the BSD License which accompanies this
8 distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/HobLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/DxeServicesLib.h>
22 Allocates one or more 4KB pages of a given type from a memory region that is
25 Allocates the number of 4KB pages of type 'MemoryType' and returns a
26 pointer to the allocated buffer. The buffer returned is aligned on a 4KB
27 boundary. If Pages is 0, then NULL is returned. If there is not enough
28 memory remaining to satisfy the request, then NULL is returned.
30 @param[in] MemoryType The memory type to allocate
31 @param[in] Pages The number of 4 KB pages to allocate.
33 @return A pointer to the allocated buffer or NULL if allocation fails.
38 AllocatePeiAccessiblePages (
39 IN EFI_MEMORY_TYPE MemoryType
,
44 EFI_ALLOCATE_TYPE AllocType
;
45 EFI_PHYSICAL_ADDRESS Memory
;
46 EFI_HOB_HANDOFF_INFO_TABLE
*PhitHob
;
52 AllocType
= AllocateAnyPages
;
54 // A X64 build of DXE may be combined with a 32-bit build of PEI, and so we
55 // need to check the memory limit set by PEI, and allocate below 4 GB if the
56 // limit is set to 4 GB or lower.
58 PhitHob
= (EFI_HOB_HANDOFF_INFO_TABLE
*)GetHobList ();
59 if (PhitHob
->EfiFreeMemoryTop
<= MAX_UINT32
) {
60 AllocType
= AllocateMaxAddress
;
64 Status
= gBS
->AllocatePages (AllocType
, MemoryType
, Pages
, &Memory
);
65 if (EFI_ERROR (Status
)) {
68 return (VOID
*)(UINTN
)Memory
;