]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/DxeServicesLib/X64/Allocate.c
MdePkg/DxeServicesLib: introduce AllocatePeiAccessiblePages routine
[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 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.
10
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.
13
14 **/
15
16 #include <PiDxe.h>
17 #include <Library/HobLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/DxeServicesLib.h>
20
21 /**
22 Allocates one or more 4KB pages of a given type from a memory region that is
23 accessible to PEI.
24
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.
29
30 @param[in] MemoryType The memory type to allocate
31 @param[in] Pages The number of 4 KB pages to allocate.
32
33 @return A pointer to the allocated buffer or NULL if allocation fails.
34
35 **/
36 VOID *
37 EFIAPI
38 AllocatePeiAccessiblePages (
39 IN EFI_MEMORY_TYPE MemoryType,
40 IN UINTN Pages
41 )
42 {
43 EFI_STATUS Status;
44 EFI_ALLOCATE_TYPE AllocType;
45 EFI_PHYSICAL_ADDRESS Memory;
46 EFI_HOB_HANDOFF_INFO_TABLE *PhitHob;
47
48 if (Pages == 0) {
49 return NULL;
50 }
51
52 AllocType = AllocateAnyPages;
53 //
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.
57 //
58 PhitHob = (EFI_HOB_HANDOFF_INFO_TABLE *)GetHobList ();
59 if (PhitHob->EfiFreeMemoryTop <= MAX_UINT32) {
60 AllocType = AllocateMaxAddress;
61 Memory = MAX_UINT32;
62 }
63
64 Status = gBS->AllocatePages (AllocType, MemoryType, Pages, &Memory);
65 if (EFI_ERROR (Status)) {
66 return NULL;
67 }
68 return (VOID *)(UINTN)Memory;
69 }