]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / EmbeddedPkg / Library / PrePiMemoryAllocationLib / MemoryAllocationLib.c
CommitLineData
a6caee65 1/** @file\r
2 Implementation of the 6 PEI Ffs (FV) APIs in library form.\r
3\r
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
3402aac7 5\r
878b807a 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
a6caee65 7\r
8**/\r
9\r
10#include <PiPei.h>\r
11\r
12#include <Library/BaseLib.h>\r
864cba95 13#include <Library/BaseMemoryLib.h>\r
a6caee65 14#include <Library/PrePiLib.h>\r
15#include <Library/DebugLib.h>\r
16\r
fb008dbe 17STATIC\r
a6caee65 18VOID *\r
19EFIAPI\r
fb008dbe
MX
20InternalAllocatePages (\r
21 IN UINTN Pages,\r
22 IN EFI_MEMORY_TYPE MemoryType\r
a6caee65 23 )\r
24{\r
e7108d0e 25 EFI_PEI_HOB_POINTERS Hob;\r
ef9974b2 26 EFI_PHYSICAL_ADDRESS NewTop;\r
a6caee65 27\r
28 Hob.Raw = GetHobList ();\r
29\r
ef9974b2
JB
30 NewTop = Hob.HandoffInformationTable->EfiFreeMemoryTop & ~(EFI_PHYSICAL_ADDRESS)EFI_PAGE_MASK;\r
31 NewTop -= Pages * EFI_PAGE_SIZE;\r
a6caee65 32\r
33 //\r
34 // Verify that there is sufficient memory to satisfy the allocation\r
35 //\r
ef9974b2
JB
36 if (NewTop < (Hob.HandoffInformationTable->EfiFreeMemoryBottom + sizeof (EFI_HOB_MEMORY_ALLOCATION))) {\r
37 return NULL;\r
38 }\r
39\r
40 //\r
41 // Update the PHIT to reflect the memory usage\r
42 //\r
43 Hob.HandoffInformationTable->EfiFreeMemoryTop = NewTop;\r
a6caee65 44\r
ef9974b2
JB
45 //\r
46 // Create a memory allocation HOB.\r
47 //\r
48 BuildMemoryAllocationHob (\r
49 Hob.HandoffInformationTable->EfiFreeMemoryTop,\r
50 Pages * EFI_PAGE_SIZE,\r
51 MemoryType\r
52 );\r
a6caee65 53\r
ef9974b2 54 return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;\r
a6caee65 55}\r
56\r
fb008dbe
MX
57/**\r
58 Allocates one or more 4KB pages of type EfiBootServicesData.\r
59\r
60 Allocates the number of 4KB pages of MemoryType and returns a pointer to the\r
61 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
62 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
63 returned.\r
64\r
65 @param Pages The number of 4 KB pages to allocate.\r
66\r
67 @return A pointer to the allocated buffer or NULL if allocation fails.\r
68\r
69**/\r
70VOID *\r
71EFIAPI\r
72AllocatePages (\r
73 IN UINTN Pages\r
74 )\r
75{\r
76 return InternalAllocatePages (Pages, EfiBootServicesData);\r
77}\r
78\r
79/**\r
80 Allocates one or more 4KB pages of type EfiRuntimeServicesData.\r
81\r
82 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r
83 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
84 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
85 returned.\r
86\r
87 @param Pages The number of 4 KB pages to allocate.\r
88\r
89 @return A pointer to the allocated buffer or NULL if allocation fails.\r
90\r
91**/\r
92VOID *\r
93EFIAPI\r
94AllocateRuntimePages (\r
95 IN UINTN Pages\r
96 )\r
97{\r
98 return InternalAllocatePages (Pages, EfiRuntimeServicesData);\r
99}\r
100\r
a6caee65 101/**\r
102 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r
103\r
104 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r
105 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
106 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
107 request, then NULL is returned.\r
108 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
109\r
110 @param Pages The number of 4 KB pages to allocate.\r
111 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
112 If Alignment is zero, then byte alignment is used.\r
113\r
114 @return A pointer to the allocated buffer or NULL if allocation fails.\r
115\r
116**/\r
117VOID *\r
118EFIAPI\r
119AllocateAlignedPages (\r
120 IN UINTN Pages,\r
121 IN UINTN Alignment\r
122 )\r
123{\r
e7108d0e
MK
124 VOID *Memory;\r
125 UINTN AlignmentMask;\r
a6caee65 126\r
127 //\r
128 // Alignment must be a power of two or zero.\r
129 //\r
130 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
131\r
132 if (Pages == 0) {\r
133 return NULL;\r
134 }\r
e7108d0e 135\r
a6caee65 136 //\r
137 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
138 //\r
139 ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));\r
140 //\r
141 // We would rather waste some memory to save PEI code size.\r
142 //\r
143 Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));\r
144 if (Alignment == 0) {\r
145 AlignmentMask = Alignment;\r
146 } else {\r
147 AlignmentMask = Alignment - 1;\r
148 }\r
a6caee65 149\r
e7108d0e
MK
150 return (VOID *)(UINTN)(((UINTN)Memory + AlignmentMask) & ~AlignmentMask);\r
151}\r
a6caee65 152\r
0b342ffb
OM
153/**\r
154 Frees one or more 4KB pages that were previously allocated with one of the page allocation\r
155 functions in the Memory Allocation Library.\r
156\r
157 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
158 must have been allocated on a previous call to the page allocation services of the Memory\r
159 Allocation Library. If it is not possible to free allocated pages, then this function will\r
160 perform no actions.\r
161\r
162 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,\r
163 then ASSERT().\r
164 If Pages is zero, then ASSERT().\r
a6caee65 165\r
0b342ffb
OM
166 @param Buffer Pointer to the buffer of pages to free.\r
167 @param Pages The number of 4 KB pages to free.\r
168\r
169**/\r
170VOID\r
171EFIAPI\r
172FreePages (\r
173 IN VOID *Buffer,\r
174 IN UINTN Pages\r
175 )\r
176{\r
177 // For now, we do not support the ability to free pages in the PrePei Memory Allocator.\r
178 // The allocated memory is lost.\r
179}\r
a6caee65 180\r
181/**\r
182 Allocates a buffer of type EfiBootServicesData.\r
183\r
184 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
185 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
186 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
187\r
188 @param AllocationSize The number of bytes to allocate.\r
189\r
190 @return A pointer to the allocated buffer or NULL if allocation fails.\r
191\r
192**/\r
193VOID *\r
194EFIAPI\r
195AllocatePool (\r
196 IN UINTN AllocationSize\r
197 )\r
198{\r
e7108d0e 199 EFI_HOB_MEMORY_POOL *Hob;\r
a6caee65 200\r
201 Hob = GetHobList ();\r
202\r
a6caee65 203 //\r
204 // Verify that there is sufficient memory to satisfy the allocation\r
205 //\r
206 if (AllocationSize > 0x10000) {\r
c6a72cd7 207 // Please call AllocatePages for big allocations\r
a6caee65 208 return 0;\r
209 } else {\r
e7108d0e
MK
210 Hob = (EFI_HOB_MEMORY_POOL *)CreateHob (\r
211 EFI_HOB_TYPE_MEMORY_POOL,\r
18198171 212 (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) +\r
e7108d0e
MK
213 AllocationSize)\r
214 );\r
a6caee65 215 return (VOID *)(Hob + 1);\r
216 }\r
217}\r
218\r
864cba95
JB
219/**\r
220 Allocates and zeros a buffer of type EfiBootServicesData.\r
221\r
222 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
223 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
224 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
225 request, then NULL is returned.\r
226\r
227 @param AllocationSize The number of bytes to allocate and zero.\r
228\r
229 @return A pointer to the allocated buffer or NULL if allocation fails.\r
230\r
231**/\r
232VOID *\r
233EFIAPI\r
234AllocateZeroPool (\r
235 IN UINTN AllocationSize\r
236 )\r
237{\r
e7108d0e 238 VOID *Buffer;\r
864cba95
JB
239\r
240 Buffer = AllocatePool (AllocationSize);\r
241 if (Buffer == NULL) {\r
242 return NULL;\r
243 }\r
244\r
245 ZeroMem (Buffer, AllocationSize);\r
246\r
247 return Buffer;\r
248}\r
249\r
a6caee65 250/**\r
251 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
252 Memory Allocation Library.\r
253\r
254 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
255 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
256 resources, then this function will perform no actions.\r
257\r
258 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
259 then ASSERT().\r
260\r
261 @param Buffer Pointer to the buffer to free.\r
262\r
263**/\r
264VOID\r
265EFIAPI\r
266FreePool (\r
e7108d0e 267 IN VOID *Buffer\r
a6caee65 268 )\r
269{\r
270 // Not implemented yet\r
271}\r