]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/DxeServicesLib/X64/Allocate.c
MdePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Library / DxeServicesLib / X64 / Allocate.c
1 /** @file
2 DxeServicesLib memory allocation routines
3
4 Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <PiDxe.h>
11 #include <Library/HobLib.h>
12 #include <Library/UefiBootServicesTableLib.h>
13 #include <Library/DxeServicesLib.h>
14
15 /**
16 Allocates one or more 4KB pages of a given type from a memory region that is
17 accessible to PEI.
18
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.
23
24 @param[in] MemoryType The memory type to allocate
25 @param[in] Pages The number of 4 KB pages to allocate.
26
27 @return A pointer to the allocated buffer or NULL if allocation fails.
28
29 **/
30 VOID *
31 EFIAPI
32 AllocatePeiAccessiblePages (
33 IN EFI_MEMORY_TYPE MemoryType,
34 IN UINTN Pages
35 )
36 {
37 EFI_STATUS Status;
38 EFI_ALLOCATE_TYPE AllocType;
39 EFI_PHYSICAL_ADDRESS Memory;
40 EFI_HOB_HANDOFF_INFO_TABLE *PhitHob;
41
42 if (Pages == 0) {
43 return NULL;
44 }
45
46 AllocType = AllocateAnyPages;
47 //
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.
51 //
52 PhitHob = (EFI_HOB_HANDOFF_INFO_TABLE *)GetHobList ();
53 if (PhitHob->EfiFreeMemoryTop <= MAX_UINT32) {
54 AllocType = AllocateMaxAddress;
55 Memory = MAX_UINT32;
56 }
57
58 Status = gBS->AllocatePages (AllocType, MemoryType, Pages, &Memory);
59 if (EFI_ERROR (Status)) {
60 return NULL;
61 }
62 return (VOID *)(UINTN)Memory;
63 }