]>
Commit | Line | Data |
---|---|---|
022a9bf7 | 1 | /** @file\r |
d1102dba | 2 | Support routines for memory allocation routines based\r |
022a9bf7 | 3 | on SMM Services Table services for SMM phase drivers, with memory profile support.\r |
d1102dba | 4 | \r |
022a9bf7 | 5 | The PI System Management Mode Core Interface Specification only allows the use\r |
d1102dba LG |
6 | of EfiRuntimeServicesCode and EfiRuntimeServicesData memory types for memory\r |
7 | allocations through the SMM Services Table as the SMRAM space should be\r | |
022a9bf7 | 8 | reserved after BDS phase. The functions in the Memory Allocation Library use\r |
d1102dba LG |
9 | EfiBootServicesData as the default memory allocation type. For this SMM\r |
10 | specific instance of the Memory Allocation Library, EfiRuntimeServicesData\r | |
11 | is used as the default memory type for all allocations. In addition,\r | |
12 | allocation for the Reserved memory types are not supported and will always\r | |
022a9bf7 SZ |
13 | return NULL.\r |
14 | \r | |
d1102dba | 15 | Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r |
9d510e61 | 16 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
022a9bf7 SZ |
17 | \r |
18 | **/\r | |
19 | \r | |
20 | #include <PiSmm.h>\r | |
21 | \r | |
22 | #include <Protocol/SmmAccess2.h>\r | |
23 | #include <Library/MemoryAllocationLib.h>\r | |
24 | #include <Library/UefiBootServicesTableLib.h>\r | |
25 | #include <Library/SmmServicesTableLib.h>\r | |
26 | #include <Library/BaseMemoryLib.h>\r | |
27 | #include <Library/DebugLib.h>\r | |
28 | \r | |
29 | #include <Library/MemoryProfileLib.h>\r | |
30 | \r | |
31 | EFI_SMRAM_DESCRIPTOR *mSmramRanges;\r | |
32 | UINTN mSmramRangeCount;\r | |
33 | \r | |
34 | /**\r | |
35 | The constructor function caches SMRAM ranges that are present in the system.\r | |
d1102dba | 36 | \r |
022a9bf7 SZ |
37 | It will ASSERT() if SMM Access2 Protocol doesn't exist.\r |
38 | It will ASSERT() if SMRAM ranges can't be got.\r | |
d1102dba | 39 | It will ASSERT() if Resource can't be allocated for cache SMRAM range.\r |
022a9bf7 SZ |
40 | It will always return EFI_SUCCESS.\r |
41 | \r | |
42 | @param ImageHandle The firmware allocated handle for the EFI image.\r | |
43 | @param SystemTable A pointer to the EFI System Table.\r | |
44 | \r | |
45 | @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r | |
46 | \r | |
47 | **/\r | |
48 | EFI_STATUS\r | |
49 | EFIAPI\r | |
50 | SmmMemoryAllocationLibConstructor (\r | |
51 | IN EFI_HANDLE ImageHandle,\r | |
52 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
53 | )\r | |
54 | {\r | |
55 | EFI_STATUS Status;\r | |
56 | EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;\r | |
57 | UINTN Size;\r | |
58 | \r | |
59 | //\r | |
60 | // Locate SMM Access2 Protocol\r | |
61 | //\r | |
62 | Status = gBS->LocateProtocol (\r | |
d1102dba LG |
63 | &gEfiSmmAccess2ProtocolGuid,\r |
64 | NULL,\r | |
022a9bf7 SZ |
65 | (VOID **)&SmmAccess\r |
66 | );\r | |
67 | ASSERT_EFI_ERROR (Status);\r | |
68 | \r | |
69 | //\r | |
70 | // Get SMRAM range information\r | |
71 | //\r | |
72 | Size = 0;\r | |
73 | Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r | |
74 | ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r | |
75 | \r | |
76 | mSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);\r | |
77 | ASSERT (mSmramRanges != NULL);\r | |
78 | \r | |
79 | Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);\r | |
80 | ASSERT_EFI_ERROR (Status);\r | |
81 | \r | |
82 | mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r | |
83 | \r | |
84 | return EFI_SUCCESS;\r | |
85 | }\r | |
86 | \r | |
87 | /**\r | |
d1102dba | 88 | If SMM driver exits with an error, it must call this routine\r |
022a9bf7 SZ |
89 | to free the allocated resource before the exiting.\r |
90 | \r | |
91 | @param[in] ImageHandle The firmware allocated handle for the EFI image.\r | |
92 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
93 | \r | |
94 | @retval EFI_SUCCESS The deconstructor always returns EFI_SUCCESS.\r | |
95 | **/\r | |
96 | EFI_STATUS\r | |
97 | EFIAPI\r | |
98 | SmmMemoryAllocationLibDestructor (\r | |
99 | IN EFI_HANDLE ImageHandle,\r | |
100 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
101 | )\r | |
102 | {\r | |
103 | FreePool (mSmramRanges);\r | |
104 | \r | |
105 | return EFI_SUCCESS;\r | |
106 | }\r | |
107 | \r | |
108 | /**\r | |
109 | Check whether the start address of buffer is within any of the SMRAM ranges.\r | |
110 | \r | |
111 | @param[in] Buffer The pointer to the buffer to be checked.\r | |
112 | \r | |
3b28e744 | 113 | @retval TRUE The buffer is in SMRAM ranges.\r |
022a9bf7 SZ |
114 | @retval FALSE The buffer is out of SMRAM ranges.\r |
115 | **/\r | |
116 | BOOLEAN\r | |
117 | EFIAPI\r | |
118 | BufferInSmram (\r | |
119 | IN VOID *Buffer\r | |
120 | )\r | |
121 | {\r | |
122 | UINTN Index;\r | |
123 | \r | |
124 | for (Index = 0; Index < mSmramRangeCount; Index ++) {\r | |
d1102dba | 125 | if (((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer >= mSmramRanges[Index].CpuStart) &&\r |
022a9bf7 SZ |
126 | ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer < (mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize))) {\r |
127 | return TRUE;\r | |
128 | }\r | |
129 | }\r | |
130 | \r | |
131 | return FALSE;\r | |
132 | }\r | |
133 | \r | |
134 | /**\r | |
135 | Allocates one or more 4KB pages of a certain memory type.\r | |
136 | \r | |
d1102dba LG |
137 | Allocates the number of 4KB pages of a certain memory type and returns a pointer\r |
138 | to the allocated buffer. The buffer returned is aligned on a 4KB boundary. If\r | |
139 | Pages is 0, then NULL is returned. If there is not enough memory remaining to\r | |
022a9bf7 SZ |
140 | satisfy the request, then NULL is returned.\r |
141 | \r | |
142 | @param MemoryType The type of memory to allocate.\r | |
143 | @param Pages The number of 4 KB pages to allocate.\r | |
144 | \r | |
145 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
146 | \r | |
147 | **/\r | |
148 | VOID *\r | |
149 | InternalAllocatePages (\r | |
d1102dba | 150 | IN EFI_MEMORY_TYPE MemoryType,\r |
022a9bf7 SZ |
151 | IN UINTN Pages\r |
152 | )\r | |
153 | {\r | |
154 | EFI_STATUS Status;\r | |
d1102dba | 155 | EFI_PHYSICAL_ADDRESS Memory;\r |
022a9bf7 SZ |
156 | \r |
157 | if (Pages == 0) {\r | |
158 | return NULL;\r | |
159 | }\r | |
160 | \r | |
161 | Status = gSmst->SmmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);\r | |
162 | if (EFI_ERROR (Status)) {\r | |
163 | return NULL;\r | |
164 | }\r | |
165 | return (VOID *) (UINTN) Memory;\r | |
166 | }\r | |
167 | \r | |
168 | /**\r | |
169 | Allocates one or more 4KB pages of type EfiRuntimeServicesData.\r | |
170 | \r | |
d1102dba LG |
171 | Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer\r |
172 | to the allocated buffer. The buffer returned is aligned on a 4KB boundary. If\r | |
173 | Pages is 0, then NULL is returned. If there is not enough memory remaining to\r | |
022a9bf7 SZ |
174 | satisfy the request, then NULL is returned.\r |
175 | \r | |
176 | @param Pages The number of 4 KB pages to allocate.\r | |
177 | \r | |
178 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
179 | \r | |
180 | **/\r | |
181 | VOID *\r | |
182 | EFIAPI\r | |
183 | AllocatePages (\r | |
184 | IN UINTN Pages\r | |
185 | )\r | |
186 | {\r | |
187 | VOID *Buffer;\r | |
188 | \r | |
189 | Buffer = InternalAllocatePages (EfiRuntimeServicesData, Pages);\r | |
190 | if (Buffer != NULL) {\r | |
191 | MemoryProfileLibRecord (\r | |
192 | (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r | |
193 | MEMORY_PROFILE_ACTION_LIB_ALLOCATE_PAGES,\r | |
194 | EfiRuntimeServicesData,\r | |
195 | Buffer,\r | |
196 | EFI_PAGES_TO_SIZE(Pages),\r | |
197 | NULL\r | |
198 | );\r | |
199 | }\r | |
200 | return Buffer;\r | |
201 | }\r | |
202 | \r | |
203 | /**\r | |
204 | Allocates one or more 4KB pages of type EfiRuntimeServicesData.\r | |
205 | \r | |
d1102dba LG |
206 | Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a\r |
207 | pointer to the allocated buffer. The buffer returned is aligned on a 4KB boundary.\r | |
208 | If Pages is 0, then NULL is returned. If there is not enough memory remaining\r | |
022a9bf7 SZ |
209 | to satisfy the request, then NULL is returned.\r |
210 | \r | |
211 | @param Pages The number of 4 KB pages to allocate.\r | |
212 | \r | |
213 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
214 | \r | |
215 | **/\r | |
216 | VOID *\r | |
217 | EFIAPI\r | |
218 | AllocateRuntimePages (\r | |
219 | IN UINTN Pages\r | |
220 | )\r | |
221 | {\r | |
222 | VOID *Buffer;\r | |
223 | \r | |
224 | Buffer = InternalAllocatePages (EfiRuntimeServicesData, Pages);\r | |
225 | if (Buffer != NULL) {\r | |
226 | MemoryProfileLibRecord (\r | |
227 | (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r | |
228 | MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_PAGES,\r | |
229 | EfiRuntimeServicesData,\r | |
230 | Buffer,\r | |
231 | EFI_PAGES_TO_SIZE(Pages),\r | |
232 | NULL\r | |
233 | );\r | |
234 | }\r | |
235 | return Buffer;\r | |
236 | }\r | |
237 | \r | |
238 | /**\r | |
239 | Allocates one or more 4KB pages of type EfiReservedMemoryType.\r | |
240 | \r | |
d1102dba LG |
241 | Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a\r |
242 | pointer to the allocated buffer. The buffer returned is aligned on a 4KB boundary.\r | |
243 | If Pages is 0, then NULL is returned. If there is not enough memory remaining\r | |
022a9bf7 SZ |
244 | to satisfy the request, then NULL is returned.\r |
245 | \r | |
246 | @param Pages The number of 4 KB pages to allocate.\r | |
247 | \r | |
248 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
249 | \r | |
250 | **/\r | |
251 | VOID *\r | |
252 | EFIAPI\r | |
253 | AllocateReservedPages (\r | |
254 | IN UINTN Pages\r | |
255 | )\r | |
256 | {\r | |
257 | return NULL;\r | |
258 | }\r | |
259 | \r | |
260 | /**\r | |
261 | Frees one or more 4KB pages that were previously allocated with one of the page allocation\r | |
262 | functions in the Memory Allocation Library.\r | |
263 | \r | |
d1102dba LG |
264 | Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer.\r |
265 | Buffer must have been allocated on a previous call to the page allocation services\r | |
266 | of the Memory Allocation Library. If it is not possible to free allocated pages,\r | |
022a9bf7 | 267 | then this function will perform no actions.\r |
d1102dba LG |
268 | \r |
269 | If Buffer was not allocated with a page allocation function in the Memory Allocation\r | |
022a9bf7 SZ |
270 | Library, then ASSERT().\r |
271 | If Pages is zero, then ASSERT().\r | |
d1102dba | 272 | \r |
022a9bf7 SZ |
273 | @param Buffer The pointer to the buffer of pages to free.\r |
274 | @param Pages The number of 4 KB pages to free.\r | |
275 | \r | |
276 | **/\r | |
277 | VOID\r | |
278 | EFIAPI\r | |
279 | FreePages (\r | |
280 | IN VOID *Buffer,\r | |
281 | IN UINTN Pages\r | |
282 | )\r | |
283 | {\r | |
284 | EFI_STATUS Status;\r | |
285 | \r | |
286 | ASSERT (Pages != 0);\r | |
287 | if (BufferInSmram (Buffer)) {\r | |
288 | //\r | |
289 | // When Buffer is in SMRAM range, it should be allocated by gSmst->SmmAllocatePages() service.\r | |
290 | // So, gSmst->SmmFreePages() service is used to free it.\r | |
291 | //\r | |
292 | Status = gSmst->SmmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r | |
293 | } else {\r | |
294 | //\r | |
295 | // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.\r | |
296 | // So, gBS->FreePages() service is used to free it.\r | |
297 | //\r | |
298 | Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r | |
299 | }\r | |
300 | ASSERT_EFI_ERROR (Status);\r | |
301 | }\r | |
302 | \r | |
303 | /**\r | |
304 | Allocates one or more 4KB pages of a certain memory type at a specified alignment.\r | |
305 | \r | |
d1102dba LG |
306 | Allocates the number of 4KB pages specified by Pages of a certain memory type\r |
307 | with an alignment specified by Alignment. The allocated buffer is returned.\r | |
308 | If Pages is 0, then NULL is returned. If there is not enough memory at the\r | |
022a9bf7 SZ |
309 | specified alignment remaining to satisfy the request, then NULL is returned.\r |
310 | If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r | |
311 | If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r | |
312 | \r | |
313 | @param MemoryType The type of memory to allocate.\r | |
314 | @param Pages The number of 4 KB pages to allocate.\r | |
d1102dba | 315 | @param Alignment The requested alignment of the allocation.\r |
022a9bf7 SZ |
316 | Must be a power of two.\r |
317 | If Alignment is zero, then byte alignment is used.\r | |
318 | \r | |
319 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
320 | \r | |
321 | **/\r | |
322 | VOID *\r | |
323 | InternalAllocateAlignedPages (\r | |
d1102dba | 324 | IN EFI_MEMORY_TYPE MemoryType,\r |
022a9bf7 SZ |
325 | IN UINTN Pages,\r |
326 | IN UINTN Alignment\r | |
327 | )\r | |
328 | {\r | |
329 | EFI_STATUS Status;\r | |
330 | EFI_PHYSICAL_ADDRESS Memory;\r | |
331 | UINTN AlignedMemory;\r | |
332 | UINTN AlignmentMask;\r | |
333 | UINTN UnalignedPages;\r | |
334 | UINTN RealPages;\r | |
335 | \r | |
336 | //\r | |
337 | // Alignment must be a power of two or zero.\r | |
338 | //\r | |
339 | ASSERT ((Alignment & (Alignment - 1)) == 0);\r | |
d1102dba | 340 | \r |
022a9bf7 SZ |
341 | if (Pages == 0) {\r |
342 | return NULL;\r | |
343 | }\r | |
344 | if (Alignment > EFI_PAGE_SIZE) {\r | |
345 | //\r | |
346 | // Calculate the total number of pages since alignment is larger than page size.\r | |
347 | //\r | |
348 | AlignmentMask = Alignment - 1;\r | |
349 | RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment);\r | |
350 | //\r | |
351 | // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r | |
352 | //\r | |
353 | ASSERT (RealPages > Pages);\r | |
d1102dba | 354 | \r |
022a9bf7 SZ |
355 | Status = gSmst->SmmAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);\r |
356 | if (EFI_ERROR (Status)) {\r | |
357 | return NULL;\r | |
358 | }\r | |
359 | AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;\r | |
360 | UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);\r | |
361 | if (UnalignedPages > 0) {\r | |
362 | //\r | |
363 | // Free first unaligned page(s).\r | |
364 | //\r | |
365 | Status = gSmst->SmmFreePages (Memory, UnalignedPages);\r | |
366 | ASSERT_EFI_ERROR (Status);\r | |
367 | }\r | |
16f69227 | 368 | Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);\r |
022a9bf7 SZ |
369 | UnalignedPages = RealPages - Pages - UnalignedPages;\r |
370 | if (UnalignedPages > 0) {\r | |
371 | //\r | |
372 | // Free last unaligned page(s).\r | |
373 | //\r | |
374 | Status = gSmst->SmmFreePages (Memory, UnalignedPages);\r | |
375 | ASSERT_EFI_ERROR (Status);\r | |
376 | }\r | |
377 | } else {\r | |
378 | //\r | |
379 | // Do not over-allocate pages in this case.\r | |
380 | //\r | |
381 | Status = gSmst->SmmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);\r | |
382 | if (EFI_ERROR (Status)) {\r | |
383 | return NULL;\r | |
384 | }\r | |
385 | AlignedMemory = (UINTN) Memory;\r | |
386 | }\r | |
387 | return (VOID *) AlignedMemory;\r | |
388 | }\r | |
389 | \r | |
390 | /**\r | |
391 | Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.\r | |
392 | \r | |
d1102dba LG |
393 | Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData\r |
394 | with an alignment specified by Alignment. The allocated buffer is returned.\r | |
395 | If Pages is 0, then NULL is returned. If there is not enough memory at the\r | |
022a9bf7 | 396 | specified alignment remaining to satisfy the request, then NULL is returned.\r |
d1102dba | 397 | \r |
022a9bf7 SZ |
398 | If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r |
399 | If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r | |
400 | \r | |
401 | @param Pages The number of 4 KB pages to allocate.\r | |
d1102dba | 402 | @param Alignment The requested alignment of the allocation.\r |
022a9bf7 SZ |
403 | Must be a power of two.\r |
404 | If Alignment is zero, then byte alignment is used.\r | |
405 | \r | |
406 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
407 | \r | |
408 | **/\r | |
409 | VOID *\r | |
410 | EFIAPI\r | |
411 | AllocateAlignedPages (\r | |
412 | IN UINTN Pages,\r | |
413 | IN UINTN Alignment\r | |
414 | )\r | |
415 | {\r | |
416 | VOID *Buffer;\r | |
417 | \r | |
418 | Buffer = InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r | |
419 | if (Buffer != NULL) {\r | |
420 | MemoryProfileLibRecord (\r | |
421 | (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r | |
422 | MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_PAGES,\r | |
423 | EfiRuntimeServicesData,\r | |
424 | Buffer,\r | |
425 | EFI_PAGES_TO_SIZE(Pages),\r | |
426 | NULL\r | |
427 | );\r | |
428 | }\r | |
429 | return Buffer;\r | |
430 | }\r | |
431 | \r | |
432 | /**\r | |
433 | Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.\r | |
434 | \r | |
d1102dba LG |
435 | Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData\r |
436 | with an alignment specified by Alignment. The allocated buffer is returned.\r | |
437 | If Pages is 0, then NULL is returned. If there is not enough memory at the\r | |
022a9bf7 | 438 | specified alignment remaining to satisfy the request, then NULL is returned.\r |
d1102dba | 439 | \r |
022a9bf7 SZ |
440 | If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r |
441 | If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r | |
442 | \r | |
443 | @param Pages The number of 4 KB pages to allocate.\r | |
d1102dba | 444 | @param Alignment The requested alignment of the allocation.\r |
022a9bf7 SZ |
445 | Must be a power of two.\r |
446 | If Alignment is zero, then byte alignment is used.\r | |
447 | \r | |
448 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
449 | \r | |
450 | **/\r | |
451 | VOID *\r | |
452 | EFIAPI\r | |
453 | AllocateAlignedRuntimePages (\r | |
454 | IN UINTN Pages,\r | |
455 | IN UINTN Alignment\r | |
456 | )\r | |
457 | {\r | |
458 | VOID *Buffer;\r | |
459 | \r | |
460 | Buffer = InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r | |
461 | if (Buffer != NULL) {\r | |
462 | MemoryProfileLibRecord (\r | |
463 | (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r | |
464 | MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RUNTIME_PAGES,\r | |
465 | EfiRuntimeServicesData,\r | |
466 | Buffer,\r | |
467 | EFI_PAGES_TO_SIZE(Pages),\r | |
468 | NULL\r | |
469 | );\r | |
470 | }\r | |
471 | return Buffer;\r | |
472 | }\r | |
473 | \r | |
474 | /**\r | |
475 | Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.\r | |
476 | \r | |
d1102dba LG |
477 | Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType\r |
478 | with an alignment specified by Alignment. The allocated buffer is returned.\r | |
479 | If Pages is 0, then NULL is returned. If there is not enough memory at the\r | |
022a9bf7 | 480 | specified alignment remaining to satisfy the request, then NULL is returned.\r |
d1102dba | 481 | \r |
022a9bf7 SZ |
482 | If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r |
483 | If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r | |
484 | \r | |
485 | @param Pages The number of 4 KB pages to allocate.\r | |
d1102dba | 486 | @param Alignment The requested alignment of the allocation.\r |
022a9bf7 SZ |
487 | Must be a power of two.\r |
488 | If Alignment is zero, then byte alignment is used.\r | |
489 | \r | |
490 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
491 | \r | |
492 | **/\r | |
493 | VOID *\r | |
494 | EFIAPI\r | |
495 | AllocateAlignedReservedPages (\r | |
496 | IN UINTN Pages,\r | |
497 | IN UINTN Alignment\r | |
498 | )\r | |
499 | {\r | |
500 | return NULL;\r | |
501 | }\r | |
502 | \r | |
503 | /**\r | |
504 | Frees one or more 4KB pages that were previously allocated with one of the aligned page\r | |
505 | allocation functions in the Memory Allocation Library.\r | |
506 | \r | |
d1102dba LG |
507 | Frees the number of 4KB pages specified by Pages from the buffer specified by\r |
508 | Buffer. Buffer must have been allocated on a previous call to the aligned page\r | |
509 | allocation services of the Memory Allocation Library. If it is not possible to\r | |
022a9bf7 | 510 | free allocated pages, then this function will perform no actions.\r |
d1102dba LG |
511 | \r |
512 | If Buffer was not allocated with an aligned page allocation function in the\r | |
022a9bf7 SZ |
513 | Memory Allocation Library, then ASSERT().\r |
514 | If Pages is zero, then ASSERT().\r | |
d1102dba | 515 | \r |
022a9bf7 SZ |
516 | @param Buffer The pointer to the buffer of pages to free.\r |
517 | @param Pages The number of 4 KB pages to free.\r | |
518 | \r | |
519 | **/\r | |
520 | VOID\r | |
521 | EFIAPI\r | |
522 | FreeAlignedPages (\r | |
523 | IN VOID *Buffer,\r | |
524 | IN UINTN Pages\r | |
525 | )\r | |
526 | {\r | |
527 | EFI_STATUS Status;\r | |
528 | \r | |
529 | ASSERT (Pages != 0);\r | |
530 | if (BufferInSmram (Buffer)) {\r | |
531 | //\r | |
532 | // When Buffer is in SMRAM range, it should be allocated by gSmst->SmmAllocatePages() service.\r | |
533 | // So, gSmst->SmmFreePages() service is used to free it.\r | |
534 | //\r | |
535 | Status = gSmst->SmmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r | |
536 | } else {\r | |
537 | //\r | |
538 | // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.\r | |
539 | // So, gBS->FreePages() service is used to free it.\r | |
540 | //\r | |
541 | Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r | |
542 | }\r | |
543 | ASSERT_EFI_ERROR (Status);\r | |
544 | }\r | |
545 | \r | |
546 | /**\r | |
547 | Allocates a buffer of a certain pool type.\r | |
548 | \r | |
d1102dba LG |
549 | Allocates the number bytes specified by AllocationSize of a certain pool type\r |
550 | and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r | |
551 | valid buffer of 0 size is returned. If there is not enough memory remaining to\r | |
022a9bf7 SZ |
552 | satisfy the request, then NULL is returned.\r |
553 | \r | |
554 | @param MemoryType The type of memory to allocate.\r | |
555 | @param AllocationSize The number of bytes to allocate.\r | |
556 | \r | |
557 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
558 | \r | |
559 | **/\r | |
560 | VOID *\r | |
561 | InternalAllocatePool (\r | |
d1102dba | 562 | IN EFI_MEMORY_TYPE MemoryType,\r |
022a9bf7 SZ |
563 | IN UINTN AllocationSize\r |
564 | )\r | |
565 | {\r | |
566 | EFI_STATUS Status;\r | |
567 | VOID *Memory;\r | |
568 | \r | |
569 | Status = gSmst->SmmAllocatePool (MemoryType, AllocationSize, &Memory);\r | |
570 | if (EFI_ERROR (Status)) {\r | |
571 | Memory = NULL;\r | |
572 | }\r | |
573 | return Memory;\r | |
574 | }\r | |
575 | \r | |
576 | /**\r | |
577 | Allocates a buffer of type EfiRuntimeServicesData.\r | |
578 | \r | |
d1102dba LG |
579 | Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData\r |
580 | and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r | |
581 | valid buffer of 0 size is returned. If there is not enough memory remaining to\r | |
022a9bf7 SZ |
582 | satisfy the request, then NULL is returned.\r |
583 | \r | |
584 | @param AllocationSize The number of bytes to allocate.\r | |
585 | \r | |
586 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
587 | \r | |
588 | **/\r | |
589 | VOID *\r | |
590 | EFIAPI\r | |
591 | AllocatePool (\r | |
592 | IN UINTN AllocationSize\r | |
593 | )\r | |
594 | {\r | |
595 | VOID *Buffer;\r | |
596 | \r | |
597 | Buffer = InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r | |
598 | if (Buffer != NULL) {\r | |
599 | MemoryProfileLibRecord (\r | |
600 | (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r | |
601 | MEMORY_PROFILE_ACTION_LIB_ALLOCATE_POOL,\r | |
602 | EfiRuntimeServicesData,\r | |
603 | Buffer,\r | |
604 | AllocationSize,\r | |
605 | NULL\r | |
606 | );\r | |
607 | }\r | |
608 | return Buffer;\r | |
609 | }\r | |
610 | \r | |
611 | /**\r | |
612 | Allocates a buffer of type EfiRuntimeServicesData.\r | |
613 | \r | |
d1102dba LG |
614 | Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData\r |
615 | and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r | |
616 | valid buffer of 0 size is returned. If there is not enough memory remaining to\r | |
022a9bf7 SZ |
617 | satisfy the request, then NULL is returned.\r |
618 | \r | |
619 | @param AllocationSize The number of bytes to allocate.\r | |
620 | \r | |
621 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
622 | \r | |
623 | **/\r | |
624 | VOID *\r | |
625 | EFIAPI\r | |
626 | AllocateRuntimePool (\r | |
627 | IN UINTN AllocationSize\r | |
628 | )\r | |
629 | {\r | |
630 | VOID *Buffer;\r | |
631 | \r | |
632 | Buffer = InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r | |
633 | if (Buffer != NULL) {\r | |
634 | MemoryProfileLibRecord (\r | |
635 | (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r | |
636 | MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_POOL,\r | |
637 | EfiRuntimeServicesData,\r | |
638 | Buffer,\r | |
639 | AllocationSize,\r | |
640 | NULL\r | |
641 | );\r | |
642 | }\r | |
643 | return Buffer;\r | |
644 | }\r | |
645 | \r | |
646 | /**\r | |
647 | Allocates a buffer of type EfiReservedMemoryType.\r | |
648 | \r | |
d1102dba LG |
649 | Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType\r |
650 | and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r | |
651 | valid buffer of 0 size is returned. If there is not enough memory remaining to\r | |
022a9bf7 SZ |
652 | satisfy the request, then NULL is returned.\r |
653 | \r | |
654 | @param AllocationSize The number of bytes to allocate.\r | |
655 | \r | |
656 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
657 | \r | |
658 | **/\r | |
659 | VOID *\r | |
660 | EFIAPI\r | |
661 | AllocateReservedPool (\r | |
662 | IN UINTN AllocationSize\r | |
663 | )\r | |
664 | {\r | |
665 | return NULL;\r | |
666 | }\r | |
667 | \r | |
668 | /**\r | |
669 | Allocates and zeros a buffer of a certain pool type.\r | |
670 | \r | |
d1102dba LG |
671 | Allocates the number bytes specified by AllocationSize of a certain pool type,\r |
672 | clears the buffer with zeros, and returns a pointer to the allocated buffer.\r | |
673 | If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is\r | |
022a9bf7 SZ |
674 | not enough memory remaining to satisfy the request, then NULL is returned.\r |
675 | \r | |
676 | @param PoolType The type of memory to allocate.\r | |
677 | @param AllocationSize The number of bytes to allocate and zero.\r | |
678 | \r | |
679 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
680 | \r | |
681 | **/\r | |
682 | VOID *\r | |
683 | InternalAllocateZeroPool (\r | |
d1102dba | 684 | IN EFI_MEMORY_TYPE PoolType,\r |
022a9bf7 | 685 | IN UINTN AllocationSize\r |
d1102dba | 686 | )\r |
022a9bf7 SZ |
687 | {\r |
688 | VOID *Memory;\r | |
689 | \r | |
690 | Memory = InternalAllocatePool (PoolType, AllocationSize);\r | |
691 | if (Memory != NULL) {\r | |
692 | Memory = ZeroMem (Memory, AllocationSize);\r | |
693 | }\r | |
694 | return Memory;\r | |
695 | }\r | |
696 | \r | |
697 | /**\r | |
698 | Allocates and zeros a buffer of type EfiRuntimeServicesData.\r | |
699 | \r | |
d1102dba LG |
700 | Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData,\r |
701 | clears the buffer with zeros, and returns a pointer to the allocated buffer.\r | |
702 | If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is\r | |
022a9bf7 SZ |
703 | not enough memory remaining to satisfy the request, then NULL is returned.\r |
704 | \r | |
705 | @param AllocationSize The number of bytes to allocate and zero.\r | |
706 | \r | |
707 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
708 | \r | |
709 | **/\r | |
710 | VOID *\r | |
711 | EFIAPI\r | |
712 | AllocateZeroPool (\r | |
713 | IN UINTN AllocationSize\r | |
714 | )\r | |
715 | {\r | |
716 | VOID *Buffer;\r | |
717 | \r | |
718 | Buffer = InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r | |
719 | if (Buffer != NULL) {\r | |
720 | MemoryProfileLibRecord (\r | |
721 | (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r | |
722 | MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ZERO_POOL,\r | |
723 | EfiRuntimeServicesData,\r | |
724 | Buffer,\r | |
725 | AllocationSize,\r | |
726 | NULL\r | |
727 | );\r | |
728 | }\r | |
729 | return Buffer;\r | |
730 | }\r | |
731 | \r | |
732 | /**\r | |
733 | Allocates and zeros a buffer of type EfiRuntimeServicesData.\r | |
734 | \r | |
d1102dba LG |
735 | Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData,\r |
736 | clears the buffer with zeros, and returns a pointer to the allocated buffer.\r | |
737 | If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is\r | |
022a9bf7 SZ |
738 | not enough memory remaining to satisfy the request, then NULL is returned.\r |
739 | \r | |
740 | @param AllocationSize The number of bytes to allocate and zero.\r | |
741 | \r | |
742 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
743 | \r | |
744 | **/\r | |
745 | VOID *\r | |
746 | EFIAPI\r | |
747 | AllocateRuntimeZeroPool (\r | |
748 | IN UINTN AllocationSize\r | |
749 | )\r | |
750 | {\r | |
751 | VOID *Buffer;\r | |
752 | \r | |
753 | Buffer = InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r | |
754 | if (Buffer != NULL) {\r | |
755 | MemoryProfileLibRecord (\r | |
756 | (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r | |
757 | MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_ZERO_POOL,\r | |
758 | EfiRuntimeServicesData,\r | |
759 | Buffer,\r | |
760 | AllocationSize,\r | |
761 | NULL\r | |
762 | );\r | |
763 | }\r | |
764 | return Buffer;\r | |
765 | }\r | |
766 | \r | |
767 | /**\r | |
768 | Allocates and zeros a buffer of type EfiReservedMemoryType.\r | |
769 | \r | |
d1102dba LG |
770 | Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType,\r |
771 | clears the buffer with zeros, and returns a pointer to the allocated buffer.\r | |
772 | If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is\r | |
022a9bf7 SZ |
773 | not enough memory remaining to satisfy the request, then NULL is returned.\r |
774 | \r | |
775 | @param AllocationSize The number of bytes to allocate and zero.\r | |
776 | \r | |
777 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
778 | \r | |
779 | **/\r | |
780 | VOID *\r | |
781 | EFIAPI\r | |
782 | AllocateReservedZeroPool (\r | |
783 | IN UINTN AllocationSize\r | |
784 | )\r | |
785 | {\r | |
786 | return NULL;\r | |
787 | }\r | |
788 | \r | |
789 | /**\r | |
790 | Copies a buffer to an allocated buffer of a certain pool type.\r | |
791 | \r | |
d1102dba LG |
792 | Allocates the number bytes specified by AllocationSize of a certain pool type,\r |
793 | copies AllocationSize bytes from Buffer to the newly allocated buffer, and returns\r | |
794 | a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer\r | |
795 | of 0 size is returned. If there is not enough memory remaining to satisfy the\r | |
022a9bf7 | 796 | request, then NULL is returned. If Buffer is NULL, then ASSERT().\r |
d1102dba | 797 | If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r |
022a9bf7 SZ |
798 | \r |
799 | @param PoolType The type of pool to allocate.\r | |
800 | @param AllocationSize The number of bytes to allocate and zero.\r | |
801 | @param Buffer The buffer to copy to the allocated buffer.\r | |
802 | \r | |
803 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
804 | \r | |
805 | **/\r | |
806 | VOID *\r | |
807 | InternalAllocateCopyPool (\r | |
d1102dba | 808 | IN EFI_MEMORY_TYPE PoolType,\r |
022a9bf7 SZ |
809 | IN UINTN AllocationSize,\r |
810 | IN CONST VOID *Buffer\r | |
d1102dba | 811 | )\r |
022a9bf7 SZ |
812 | {\r |
813 | VOID *Memory;\r | |
814 | \r | |
815 | ASSERT (Buffer != NULL);\r | |
816 | ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r | |
817 | \r | |
818 | Memory = InternalAllocatePool (PoolType, AllocationSize);\r | |
819 | if (Memory != NULL) {\r | |
820 | Memory = CopyMem (Memory, Buffer, AllocationSize);\r | |
821 | }\r | |
822 | return Memory;\r | |
d1102dba | 823 | }\r |
022a9bf7 SZ |
824 | \r |
825 | /**\r | |
826 | Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.\r | |
827 | \r | |
d1102dba LG |
828 | Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData,\r |
829 | copies AllocationSize bytes from Buffer to the newly allocated buffer, and returns\r | |
830 | a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer\r | |
831 | of 0 size is returned. If there is not enough memory remaining to satisfy the\r | |
022a9bf7 | 832 | request, then NULL is returned.\r |
d1102dba | 833 | \r |
022a9bf7 | 834 | If Buffer is NULL, then ASSERT().\r |
d1102dba | 835 | If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r |
022a9bf7 SZ |
836 | \r |
837 | @param AllocationSize The number of bytes to allocate and zero.\r | |
838 | @param Buffer The buffer to copy to the allocated buffer.\r | |
839 | \r | |
840 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
841 | \r | |
842 | **/\r | |
843 | VOID *\r | |
844 | EFIAPI\r | |
845 | AllocateCopyPool (\r | |
846 | IN UINTN AllocationSize,\r | |
847 | IN CONST VOID *Buffer\r | |
848 | )\r | |
849 | {\r | |
850 | VOID *NewBuffer;\r | |
851 | \r | |
852 | NewBuffer = InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r | |
853 | if (NewBuffer != NULL) {\r | |
854 | MemoryProfileLibRecord (\r | |
855 | (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r | |
856 | MEMORY_PROFILE_ACTION_LIB_ALLOCATE_COPY_POOL,\r | |
857 | EfiRuntimeServicesData,\r | |
858 | NewBuffer,\r | |
859 | AllocationSize,\r | |
860 | NULL\r | |
861 | );\r | |
862 | }\r | |
863 | return NewBuffer;\r | |
864 | }\r | |
865 | \r | |
866 | /**\r | |
867 | Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.\r | |
868 | \r | |
d1102dba LG |
869 | Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData,\r |
870 | copies AllocationSize bytes from Buffer to the newly allocated buffer, and returns\r | |
871 | a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer\r | |
872 | of 0 size is returned. If there is not enough memory remaining to satisfy the\r | |
022a9bf7 | 873 | request, then NULL is returned.\r |
d1102dba | 874 | \r |
022a9bf7 | 875 | If Buffer is NULL, then ASSERT().\r |
d1102dba | 876 | If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r |
022a9bf7 SZ |
877 | \r |
878 | @param AllocationSize The number of bytes to allocate and zero.\r | |
879 | @param Buffer The buffer to copy to the allocated buffer.\r | |
880 | \r | |
881 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
882 | \r | |
883 | **/\r | |
884 | VOID *\r | |
885 | EFIAPI\r | |
886 | AllocateRuntimeCopyPool (\r | |
887 | IN UINTN AllocationSize,\r | |
888 | IN CONST VOID *Buffer\r | |
889 | )\r | |
890 | {\r | |
891 | VOID *NewBuffer;\r | |
892 | \r | |
893 | NewBuffer = InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r | |
894 | if (NewBuffer != NULL) {\r | |
895 | MemoryProfileLibRecord (\r | |
896 | (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r | |
897 | MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_COPY_POOL,\r | |
898 | EfiRuntimeServicesData,\r | |
899 | NewBuffer,\r | |
900 | AllocationSize,\r | |
901 | NULL\r | |
902 | );\r | |
903 | }\r | |
904 | return NewBuffer;\r | |
905 | }\r | |
906 | \r | |
907 | /**\r | |
908 | Copies a buffer to an allocated buffer of type EfiReservedMemoryType.\r | |
909 | \r | |
d1102dba LG |
910 | Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType,\r |
911 | copies AllocationSize bytes from Buffer to the newly allocated buffer, and returns\r | |
912 | a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer\r | |
913 | of 0 size is returned. If there is not enough memory remaining to satisfy the\r | |
022a9bf7 | 914 | request, then NULL is returned.\r |
d1102dba | 915 | \r |
022a9bf7 | 916 | If Buffer is NULL, then ASSERT().\r |
d1102dba | 917 | If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r |
022a9bf7 SZ |
918 | \r |
919 | @param AllocationSize The number of bytes to allocate and zero.\r | |
920 | @param Buffer The buffer to copy to the allocated buffer.\r | |
921 | \r | |
922 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
923 | \r | |
924 | **/\r | |
925 | VOID *\r | |
926 | EFIAPI\r | |
927 | AllocateReservedCopyPool (\r | |
928 | IN UINTN AllocationSize,\r | |
929 | IN CONST VOID *Buffer\r | |
930 | )\r | |
931 | {\r | |
932 | return NULL;\r | |
933 | }\r | |
934 | \r | |
935 | /**\r | |
936 | Reallocates a buffer of a specified memory type.\r | |
937 | \r | |
938 | Allocates and zeros the number bytes specified by NewSize from memory of the type\r | |
d1102dba LG |
939 | specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and\r |
940 | NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r | |
941 | OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r | |
942 | If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r | |
022a9bf7 | 943 | enough memory remaining to satisfy the request, then NULL is returned.\r |
d1102dba LG |
944 | \r |
945 | If the allocation of the new buffer is successful and the smaller of NewSize\r | |
022a9bf7 SZ |
946 | and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r |
947 | \r | |
948 | @param PoolType The type of pool to allocate.\r | |
949 | @param OldSize The size, in bytes, of OldBuffer.\r | |
950 | @param NewSize The size, in bytes, of the buffer to reallocate.\r | |
d1102dba | 951 | @param OldBuffer The buffer to copy to the allocated buffer. This is an\r |
022a9bf7 SZ |
952 | optional parameter that may be NULL.\r |
953 | \r | |
954 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
955 | \r | |
956 | **/\r | |
957 | VOID *\r | |
958 | InternalReallocatePool (\r | |
d1102dba | 959 | IN EFI_MEMORY_TYPE PoolType,\r |
022a9bf7 SZ |
960 | IN UINTN OldSize,\r |
961 | IN UINTN NewSize,\r | |
962 | IN VOID *OldBuffer OPTIONAL\r | |
963 | )\r | |
964 | {\r | |
965 | VOID *NewBuffer;\r | |
966 | \r | |
967 | NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);\r | |
968 | if (NewBuffer != NULL && OldBuffer != NULL) {\r | |
969 | CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));\r | |
970 | FreePool (OldBuffer);\r | |
971 | }\r | |
972 | return NewBuffer;\r | |
973 | }\r | |
974 | \r | |
975 | /**\r | |
976 | Reallocates a buffer of type EfiRuntimeServicesData.\r | |
977 | \r | |
978 | Allocates and zeros the number bytes specified by NewSize from memory of type\r | |
d1102dba LG |
979 | EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and\r |
980 | NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r | |
981 | OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r | |
982 | If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r | |
022a9bf7 | 983 | enough memory remaining to satisfy the request, then NULL is returned.\r |
d1102dba LG |
984 | \r |
985 | If the allocation of the new buffer is successful and the smaller of NewSize\r | |
022a9bf7 SZ |
986 | and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r |
987 | \r | |
988 | @param OldSize The size, in bytes, of OldBuffer.\r | |
989 | @param NewSize The size, in bytes, of the buffer to reallocate.\r | |
d1102dba | 990 | @param OldBuffer The buffer to copy to the allocated buffer. This is an\r |
022a9bf7 SZ |
991 | optional parameter that may be NULL.\r |
992 | \r | |
993 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
994 | \r | |
995 | **/\r | |
996 | VOID *\r | |
997 | EFIAPI\r | |
998 | ReallocatePool (\r | |
999 | IN UINTN OldSize,\r | |
1000 | IN UINTN NewSize,\r | |
1001 | IN VOID *OldBuffer OPTIONAL\r | |
1002 | )\r | |
1003 | {\r | |
1004 | VOID *Buffer;\r | |
1005 | \r | |
1006 | Buffer = InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r | |
1007 | if (Buffer != NULL) {\r | |
1008 | MemoryProfileLibRecord (\r | |
1009 | (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r | |
1010 | MEMORY_PROFILE_ACTION_LIB_REALLOCATE_POOL,\r | |
1011 | EfiRuntimeServicesData,\r | |
1012 | Buffer,\r | |
1013 | NewSize,\r | |
1014 | NULL\r | |
1015 | );\r | |
1016 | }\r | |
1017 | return Buffer;\r | |
1018 | }\r | |
1019 | \r | |
1020 | /**\r | |
1021 | Reallocates a buffer of type EfiRuntimeServicesData.\r | |
1022 | \r | |
1023 | Allocates and zeros the number bytes specified by NewSize from memory of type\r | |
d1102dba LG |
1024 | EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize\r |
1025 | and NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r | |
1026 | OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r | |
1027 | If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r | |
022a9bf7 SZ |
1028 | enough memory remaining to satisfy the request, then NULL is returned.\r |
1029 | \r | |
d1102dba | 1030 | If the allocation of the new buffer is successful and the smaller of NewSize\r |
022a9bf7 SZ |
1031 | and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r |
1032 | \r | |
1033 | @param OldSize The size, in bytes, of OldBuffer.\r | |
1034 | @param NewSize The size, in bytes, of the buffer to reallocate.\r | |
d1102dba | 1035 | @param OldBuffer The buffer to copy to the allocated buffer. This is an\r |
022a9bf7 SZ |
1036 | optional parameter that may be NULL.\r |
1037 | \r | |
1038 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
1039 | \r | |
1040 | **/\r | |
1041 | VOID *\r | |
1042 | EFIAPI\r | |
1043 | ReallocateRuntimePool (\r | |
1044 | IN UINTN OldSize,\r | |
1045 | IN UINTN NewSize,\r | |
1046 | IN VOID *OldBuffer OPTIONAL\r | |
1047 | )\r | |
1048 | {\r | |
1049 | VOID *Buffer;\r | |
1050 | \r | |
1051 | Buffer = InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r | |
1052 | if (Buffer != NULL) {\r | |
1053 | MemoryProfileLibRecord (\r | |
1054 | (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r | |
1055 | MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RUNTIME_POOL,\r | |
1056 | EfiRuntimeServicesData,\r | |
1057 | Buffer,\r | |
1058 | NewSize,\r | |
1059 | NULL\r | |
1060 | );\r | |
1061 | }\r | |
1062 | return Buffer;\r | |
1063 | }\r | |
1064 | \r | |
1065 | /**\r | |
1066 | Reallocates a buffer of type EfiReservedMemoryType.\r | |
1067 | \r | |
1068 | Allocates and zeros the number bytes specified by NewSize from memory of type\r | |
d1102dba LG |
1069 | EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize\r |
1070 | and NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r | |
1071 | OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r | |
1072 | If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r | |
022a9bf7 SZ |
1073 | enough memory remaining to satisfy the request, then NULL is returned.\r |
1074 | \r | |
d1102dba | 1075 | If the allocation of the new buffer is successful and the smaller of NewSize\r |
022a9bf7 SZ |
1076 | and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r |
1077 | \r | |
1078 | @param OldSize The size, in bytes, of OldBuffer.\r | |
1079 | @param NewSize The size, in bytes, of the buffer to reallocate.\r | |
d1102dba | 1080 | @param OldBuffer The buffer to copy to the allocated buffer. This is an\r |
022a9bf7 SZ |
1081 | optional parameter that may be NULL.\r |
1082 | \r | |
1083 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
1084 | \r | |
1085 | **/\r | |
1086 | VOID *\r | |
1087 | EFIAPI\r | |
1088 | ReallocateReservedPool (\r | |
1089 | IN UINTN OldSize,\r | |
1090 | IN UINTN NewSize,\r | |
1091 | IN VOID *OldBuffer OPTIONAL\r | |
1092 | )\r | |
1093 | {\r | |
1094 | return NULL;\r | |
1095 | }\r | |
1096 | \r | |
1097 | /**\r | |
d1102dba | 1098 | Frees a buffer that was previously allocated with one of the pool allocation\r |
022a9bf7 SZ |
1099 | functions in the Memory Allocation Library.\r |
1100 | \r | |
d1102dba LG |
1101 | Frees the buffer specified by Buffer. Buffer must have been allocated on a\r |
1102 | previous call to the pool allocation services of the Memory Allocation Library.\r | |
1103 | If it is not possible to free pool resources, then this function will perform\r | |
022a9bf7 | 1104 | no actions.\r |
d1102dba LG |
1105 | \r |
1106 | If Buffer was not allocated with a pool allocation function in the Memory\r | |
022a9bf7 SZ |
1107 | Allocation Library, then ASSERT().\r |
1108 | \r | |
1109 | @param Buffer The pointer to the buffer to free.\r | |
1110 | \r | |
1111 | **/\r | |
1112 | VOID\r | |
1113 | EFIAPI\r | |
1114 | FreePool (\r | |
1115 | IN VOID *Buffer\r | |
1116 | )\r | |
1117 | {\r | |
1118 | EFI_STATUS Status;\r | |
1119 | \r | |
1120 | if (BufferInSmram (Buffer)) {\r | |
1121 | //\r | |
1122 | // When Buffer is in SMRAM range, it should be allocated by gSmst->SmmAllocatePool() service.\r | |
1123 | // So, gSmst->SmmFreePool() service is used to free it.\r | |
1124 | //\r | |
1125 | Status = gSmst->SmmFreePool (Buffer);\r | |
1126 | } else {\r | |
1127 | //\r | |
1128 | // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePool() service.\r | |
1129 | // So, gBS->FreePool() service is used to free it.\r | |
1130 | //\r | |
1131 | Status = gBS->FreePool (Buffer);\r | |
1132 | }\r | |
1133 | ASSERT_EFI_ERROR (Status);\r | |
1134 | }\r |