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