]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/DxeServicesLib/X64/Allocate.c
MdePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Library / DxeServicesLib / X64 / Allocate.c
CommitLineData
a40e0b7a
AB
1/** @file\r
2 DxeServicesLib memory allocation routines\r
3\r
4 Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>\r
5\r
9344f092 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
a40e0b7a
AB
7\r
8**/\r
9\r
10#include <PiDxe.h>\r
11#include <Library/HobLib.h>\r
12#include <Library/UefiBootServicesTableLib.h>\r
13#include <Library/DxeServicesLib.h>\r
14\r
15/**\r
16 Allocates one or more 4KB pages of a given type from a memory region that is\r
17 accessible to PEI.\r
18\r
19 Allocates the number of 4KB pages of type 'MemoryType' and returns a\r
20 pointer to the allocated buffer. The buffer returned is aligned on a 4KB\r
21 boundary. If Pages is 0, then NULL is returned. If there is not enough\r
22 memory remaining to satisfy the request, then NULL is returned.\r
23\r
24 @param[in] MemoryType The memory type to allocate\r
25 @param[in] Pages The number of 4 KB pages to allocate.\r
26\r
27 @return A pointer to the allocated buffer or NULL if allocation fails.\r
28\r
29**/\r
30VOID *\r
31EFIAPI\r
32AllocatePeiAccessiblePages (\r
33 IN EFI_MEMORY_TYPE MemoryType,\r
34 IN UINTN Pages\r
35 )\r
36{\r
37 EFI_STATUS Status;\r
38 EFI_ALLOCATE_TYPE AllocType;\r
39 EFI_PHYSICAL_ADDRESS Memory;\r
40 EFI_HOB_HANDOFF_INFO_TABLE *PhitHob;\r
41\r
42 if (Pages == 0) {\r
43 return NULL;\r
44 }\r
45\r
46 AllocType = AllocateAnyPages;\r
47 //\r
48 // A X64 build of DXE may be combined with a 32-bit build of PEI, and so we\r
49 // need to check the memory limit set by PEI, and allocate below 4 GB if the\r
50 // limit is set to 4 GB or lower.\r
51 //\r
52 PhitHob = (EFI_HOB_HANDOFF_INFO_TABLE *)GetHobList ();\r
53 if (PhitHob->EfiFreeMemoryTop <= MAX_UINT32) {\r
54 AllocType = AllocateMaxAddress;\r
55 Memory = MAX_UINT32;\r
56 }\r
57\r
58 Status = gBS->AllocatePages (AllocType, MemoryType, Pages, &Memory);\r
59 if (EFI_ERROR (Status)) {\r
60 return NULL;\r
61 }\r
62 return (VOID *)(UINTN)Memory;\r
63}\r