]> git.proxmox.com Git - mirror_edk2.git/blame - StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / StandaloneMmPkg / Library / StandaloneMmCoreMemoryAllocationLib / StandaloneMmCoreMemoryAllocationLib.c
CommitLineData
2c868eef
SV
1/** @file\r
2 Support routines for memory allocation routines based on Standalone MM Core internal functions.\r
3\r
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
0720bb7e 5 Copyright (c) 2016 - 2021, ARM Limited. All rights reserved.<BR>\r
2c868eef 6\r
86094561 7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
2c868eef
SV
8\r
9**/\r
10\r
11#include <PiMm.h>\r
12\r
13#include <Guid/MmramMemoryReserve.h>\r
14#include <Library/MemoryAllocationLib.h>\r
15#include <Library/BaseMemoryLib.h>\r
16#include <Library/DebugLib.h>\r
17#include <Library/HobLib.h>\r
18#include "StandaloneMmCoreMemoryAllocationServices.h"\r
19\r
91415a36 20EFI_MM_SYSTEM_TABLE *gMmst = NULL;\r
2c868eef
SV
21\r
22/**\r
23 Allocates one or more 4KB pages of a certain memory type.\r
24\r
25 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated\r
26 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.\r
27 If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
28\r
29 @param MemoryType The type of memory to allocate.\r
30 @param Pages The number of 4 KB pages to allocate.\r
31\r
32 @return A pointer to the allocated buffer or NULL if allocation fails.\r
33\r
34**/\r
35VOID *\r
36InternalAllocatePages (\r
37 IN EFI_MEMORY_TYPE MemoryType,\r
38 IN UINTN Pages\r
39 )\r
40{\r
41 EFI_STATUS Status;\r
42 EFI_PHYSICAL_ADDRESS Memory;\r
43\r
44 if (Pages == 0) {\r
45 return NULL;\r
46 }\r
47\r
48 Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);\r
49 if (EFI_ERROR (Status)) {\r
50 return NULL;\r
51 }\r
91415a36
MK
52\r
53 return (VOID *)(UINTN)Memory;\r
2c868eef
SV
54}\r
55\r
56/**\r
57 Allocates one or more 4KB pages of type EfiBootServicesData.\r
58\r
59 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the\r
60 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
61 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
62 returned.\r
63\r
64 @param Pages The number of 4 KB pages to allocate.\r
65\r
66 @return A pointer to the allocated buffer or NULL if allocation fails.\r
67\r
68**/\r
69VOID *\r
70EFIAPI\r
71AllocatePages (\r
72 IN UINTN Pages\r
73 )\r
74{\r
75 return InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
76}\r
77\r
78/**\r
79 Allocates one or more 4KB pages of type EfiRuntimeServicesData.\r
80\r
81 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r
82 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
83 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
84 returned.\r
85\r
86 @param Pages The number of 4 KB pages to allocate.\r
87\r
88 @return A pointer to the allocated buffer or NULL if allocation fails.\r
89\r
90**/\r
91VOID *\r
92EFIAPI\r
93AllocateRuntimePages (\r
94 IN UINTN Pages\r
95 )\r
96{\r
97 return InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
98}\r
99\r
100/**\r
101 Allocates one or more 4KB pages of type EfiReservedMemoryType.\r
102\r
103 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the\r
104 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
105 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
106 returned.\r
107\r
108 @param Pages The number of 4 KB pages to allocate.\r
109\r
110 @return A pointer to the allocated buffer or NULL if allocation fails.\r
111\r
112**/\r
113VOID *\r
114EFIAPI\r
115AllocateReservedPages (\r
116 IN UINTN Pages\r
117 )\r
118{\r
119 return NULL;\r
120}\r
121\r
122/**\r
123 Frees one or more 4KB pages that were previously allocated with one of the page allocation\r
124 functions in the Memory Allocation Library.\r
125\r
126 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
127 must have been allocated on a previous call to the page allocation services of the Memory\r
128 Allocation Library. If it is not possible to free allocated pages, then this function will\r
129 perform no actions.\r
130\r
131 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,\r
132 then ASSERT().\r
133 If Pages is zero, then ASSERT().\r
134\r
135 @param Buffer Pointer to the buffer of pages to free.\r
136 @param Pages The number of 4 KB pages to free.\r
137\r
138**/\r
139VOID\r
140EFIAPI\r
141FreePages (\r
142 IN VOID *Buffer,\r
143 IN UINTN Pages\r
144 )\r
145{\r
146 EFI_STATUS Status;\r
147\r
148 ASSERT (Pages != 0);\r
91415a36 149 Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);\r
2c868eef
SV
150 ASSERT_EFI_ERROR (Status);\r
151}\r
152\r
153/**\r
154 Allocates one or more 4KB pages of a certain memory type at a specified alignment.\r
155\r
156 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment\r
157 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.\r
158 If there is not enough memory at the specified alignment remaining to satisfy the request, then\r
159 NULL is returned.\r
160 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
161 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
162\r
163 @param MemoryType The type of memory to allocate.\r
164 @param Pages The number of 4 KB pages to allocate.\r
165 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
166 If Alignment is zero, then byte alignment is used.\r
167\r
168 @return A pointer to the allocated buffer or NULL if allocation fails.\r
169\r
170**/\r
171VOID *\r
172InternalAllocateAlignedPages (\r
173 IN EFI_MEMORY_TYPE MemoryType,\r
174 IN UINTN Pages,\r
175 IN UINTN Alignment\r
176 )\r
177{\r
178 EFI_STATUS Status;\r
179 EFI_PHYSICAL_ADDRESS Memory;\r
180 UINTN AlignedMemory;\r
181 UINTN AlignmentMask;\r
182 UINTN UnalignedPages;\r
183 UINTN RealPages;\r
184\r
185 //\r
186 // Alignment must be a power of two or zero.\r
187 //\r
188 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
189\r
190 if (Pages == 0) {\r
191 return NULL;\r
192 }\r
91415a36 193\r
2c868eef
SV
194 if (Alignment > EFI_PAGE_SIZE) {\r
195 //\r
196 // Calculate the total number of pages since alignment is larger than page size.\r
197 //\r
91415a36
MK
198 AlignmentMask = Alignment - 1;\r
199 RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment);\r
2c868eef
SV
200 //\r
201 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
202 //\r
203 ASSERT (RealPages > Pages);\r
204\r
205 Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);\r
206 if (EFI_ERROR (Status)) {\r
207 return NULL;\r
208 }\r
91415a36
MK
209\r
210 AlignedMemory = ((UINTN)Memory + AlignmentMask) & ~AlignmentMask;\r
211 UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)Memory);\r
2c868eef
SV
212 if (UnalignedPages > 0) {\r
213 //\r
214 // Free first unaligned page(s).\r
215 //\r
216 Status = gMmst->MmFreePages (Memory, UnalignedPages);\r
217 ASSERT_EFI_ERROR (Status);\r
218 }\r
91415a36
MK
219\r
220 Memory = (EFI_PHYSICAL_ADDRESS)(AlignedMemory + EFI_PAGES_TO_SIZE (Pages));\r
2c868eef
SV
221 UnalignedPages = RealPages - Pages - UnalignedPages;\r
222 if (UnalignedPages > 0) {\r
223 //\r
224 // Free last unaligned page(s).\r
225 //\r
226 Status = gMmst->MmFreePages (Memory, UnalignedPages);\r
227 ASSERT_EFI_ERROR (Status);\r
228 }\r
229 } else {\r
230 //\r
231 // Do not over-allocate pages in this case.\r
232 //\r
233 Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);\r
234 if (EFI_ERROR (Status)) {\r
235 return NULL;\r
236 }\r
91415a36
MK
237\r
238 AlignedMemory = (UINTN)Memory;\r
2c868eef 239 }\r
91415a36
MK
240\r
241 return (VOID *)AlignedMemory;\r
2c868eef
SV
242}\r
243\r
244/**\r
245 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r
246\r
247 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r
248 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
249 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
250 request, then NULL is returned.\r
251\r
252 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
253 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
254\r
255 @param Pages The number of 4 KB pages to allocate.\r
256 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
257 If Alignment is zero, then byte alignment is used.\r
258\r
259 @return A pointer to the allocated buffer or NULL if allocation fails.\r
260\r
261**/\r
262VOID *\r
263EFIAPI\r
264AllocateAlignedPages (\r
265 IN UINTN Pages,\r
266 IN UINTN Alignment\r
267 )\r
268{\r
269 return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
270}\r
271\r
272/**\r
273 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.\r
274\r
275 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an\r
276 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
277 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
278 request, then NULL is returned.\r
279\r
280 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
281 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
282\r
283 @param Pages The number of 4 KB pages to allocate.\r
284 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
285 If Alignment is zero, then byte alignment is used.\r
286\r
287 @return A pointer to the allocated buffer or NULL if allocation fails.\r
288\r
289**/\r
290VOID *\r
291EFIAPI\r
292AllocateAlignedRuntimePages (\r
293 IN UINTN Pages,\r
294 IN UINTN Alignment\r
295 )\r
296{\r
297 return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
298}\r
299\r
300/**\r
301 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.\r
302\r
303 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an\r
304 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
305 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
306 request, then NULL is returned.\r
307\r
308 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
309 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
310\r
311 @param Pages The number of 4 KB pages to allocate.\r
312 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
313 If Alignment is zero, then byte alignment is used.\r
314\r
315 @return A pointer to the allocated buffer or NULL if allocation fails.\r
316\r
317**/\r
318VOID *\r
319EFIAPI\r
320AllocateAlignedReservedPages (\r
321 IN UINTN Pages,\r
322 IN UINTN Alignment\r
323 )\r
324{\r
325 return NULL;\r
326}\r
327\r
328/**\r
329 Frees one or more 4KB pages that were previously allocated with one of the aligned page\r
330 allocation functions in the Memory Allocation Library.\r
331\r
332 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
333 must have been allocated on a previous call to the aligned page allocation services of the Memory\r
334 Allocation Library. If it is not possible to free allocated pages, then this function will\r
335 perform no actions.\r
336\r
337 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation\r
338 Library, then ASSERT().\r
339 If Pages is zero, then ASSERT().\r
340\r
341 @param Buffer Pointer to the buffer of pages to free.\r
342 @param Pages The number of 4 KB pages to free.\r
343\r
344**/\r
345VOID\r
346EFIAPI\r
347FreeAlignedPages (\r
348 IN VOID *Buffer,\r
349 IN UINTN Pages\r
350 )\r
351{\r
352 EFI_STATUS Status;\r
353\r
354 ASSERT (Pages != 0);\r
91415a36 355 Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);\r
2c868eef
SV
356 ASSERT_EFI_ERROR (Status);\r
357}\r
358\r
359/**\r
360 Allocates a buffer of a certain pool type.\r
361\r
362 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a\r
363 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
364 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
365\r
366 @param MemoryType The type of memory to allocate.\r
367 @param AllocationSize The number of bytes to allocate.\r
368\r
369 @return A pointer to the allocated buffer or NULL if allocation fails.\r
370\r
371**/\r
372VOID *\r
373InternalAllocatePool (\r
374 IN EFI_MEMORY_TYPE MemoryType,\r
375 IN UINTN AllocationSize\r
376 )\r
377{\r
378 EFI_STATUS Status;\r
379 VOID *Memory;\r
380\r
381 Memory = NULL;\r
382\r
383 Status = gMmst->MmAllocatePool (MemoryType, AllocationSize, &Memory);\r
384 if (EFI_ERROR (Status)) {\r
385 Memory = NULL;\r
386 }\r
91415a36 387\r
2c868eef
SV
388 return Memory;\r
389}\r
390\r
391/**\r
392 Allocates a buffer of type EfiBootServicesData.\r
393\r
394 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
395 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
396 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
397\r
398 @param AllocationSize The number of bytes to allocate.\r
399\r
400 @return A pointer to the allocated buffer or NULL if allocation fails.\r
401\r
402**/\r
403VOID *\r
404EFIAPI\r
405AllocatePool (\r
406 IN UINTN AllocationSize\r
407 )\r
408{\r
409 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
410}\r
411\r
412/**\r
413 Allocates a buffer of type EfiRuntimeServicesData.\r
414\r
415 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns\r
416 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
417 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
418\r
419 @param AllocationSize The number of bytes to allocate.\r
420\r
421 @return A pointer to the allocated buffer or NULL if allocation fails.\r
422\r
423**/\r
424VOID *\r
425EFIAPI\r
426AllocateRuntimePool (\r
427 IN UINTN AllocationSize\r
428 )\r
429{\r
430 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
431}\r
432\r
433/**\r
434 Allocates a buffer of type EfiReservedMemoryType.\r
435\r
436 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns\r
437 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
438 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
439\r
440 @param AllocationSize The number of bytes to allocate.\r
441\r
442 @return A pointer to the allocated buffer or NULL if allocation fails.\r
443\r
444**/\r
445VOID *\r
446EFIAPI\r
447AllocateReservedPool (\r
448 IN UINTN AllocationSize\r
449 )\r
450{\r
451 return NULL;\r
452}\r
453\r
454/**\r
455 Allocates and zeros a buffer of a certain pool type.\r
456\r
457 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer\r
458 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid\r
459 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,\r
460 then NULL is returned.\r
461\r
462 @param PoolType The type of memory to allocate.\r
463 @param AllocationSize The number of bytes to allocate and zero.\r
464\r
465 @return A pointer to the allocated buffer or NULL if allocation fails.\r
466\r
467**/\r
468VOID *\r
469InternalAllocateZeroPool (\r
470 IN EFI_MEMORY_TYPE PoolType,\r
471 IN UINTN AllocationSize\r
472 )\r
473{\r
474 VOID *Memory;\r
475\r
476 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
477 if (Memory != NULL) {\r
478 Memory = ZeroMem (Memory, AllocationSize);\r
479 }\r
91415a36 480\r
2c868eef
SV
481 return Memory;\r
482}\r
483\r
484/**\r
485 Allocates and zeros a buffer of type EfiBootServicesData.\r
486\r
487 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
488 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
489 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
490 request, then NULL is returned.\r
491\r
492 @param AllocationSize The number of bytes to allocate and zero.\r
493\r
494 @return A pointer to the allocated buffer or NULL if allocation fails.\r
495\r
496**/\r
497VOID *\r
498EFIAPI\r
499AllocateZeroPool (\r
500 IN UINTN AllocationSize\r
501 )\r
502{\r
503 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
504}\r
505\r
506/**\r
507 Allocates and zeros a buffer of type EfiRuntimeServicesData.\r
508\r
509 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the\r
510 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
511 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
512 request, then NULL is returned.\r
513\r
514 @param AllocationSize The number of bytes to allocate and zero.\r
515\r
516 @return A pointer to the allocated buffer or NULL if allocation fails.\r
517\r
518**/\r
519VOID *\r
520EFIAPI\r
521AllocateRuntimeZeroPool (\r
522 IN UINTN AllocationSize\r
523 )\r
524{\r
525 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
526}\r
527\r
528/**\r
529 Allocates and zeros a buffer of type EfiReservedMemoryType.\r
530\r
531 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the\r
532 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
533 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
534 request, then NULL is returned.\r
535\r
536 @param AllocationSize The number of bytes to allocate and zero.\r
537\r
538 @return A pointer to the allocated buffer or NULL if allocation fails.\r
539\r
540**/\r
541VOID *\r
542EFIAPI\r
543AllocateReservedZeroPool (\r
544 IN UINTN AllocationSize\r
545 )\r
546{\r
547 return NULL;\r
548}\r
549\r
550/**\r
551 Copies a buffer to an allocated buffer of a certain pool type.\r
552\r
553 Allocates the number bytes specified by AllocationSize of a certain pool type, copies\r
554 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
555 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
556 is not enough memory remaining to satisfy the request, then NULL is returned.\r
557 If Buffer is NULL, then ASSERT().\r
558 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
559\r
560 @param PoolType The type of pool to allocate.\r
561 @param AllocationSize The number of bytes to allocate and zero.\r
562 @param Buffer The buffer to copy to the allocated buffer.\r
563\r
564 @return A pointer to the allocated buffer or NULL if allocation fails.\r
565\r
566**/\r
567VOID *\r
568InternalAllocateCopyPool (\r
569 IN EFI_MEMORY_TYPE PoolType,\r
570 IN UINTN AllocationSize,\r
571 IN CONST VOID *Buffer\r
572 )\r
573{\r
574 VOID *Memory;\r
575\r
576 ASSERT (Buffer != NULL);\r
91415a36 577 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN)Buffer + 1));\r
2c868eef
SV
578\r
579 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
580 if (Memory != NULL) {\r
581 Memory = CopyMem (Memory, Buffer, AllocationSize);\r
582 }\r
91415a36 583\r
2c868eef
SV
584 return Memory;\r
585}\r
586\r
587/**\r
588 Copies a buffer to an allocated buffer of type EfiBootServicesData.\r
589\r
590 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies\r
591 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
592 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
593 is not enough memory remaining to satisfy the request, then NULL is returned.\r
594\r
595 If Buffer is NULL, then ASSERT().\r
596 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
597\r
598 @param AllocationSize The number of bytes to allocate and zero.\r
599 @param Buffer The buffer to copy to the allocated buffer.\r
600\r
601 @return A pointer to the allocated buffer or NULL if allocation fails.\r
602\r
603**/\r
604VOID *\r
605EFIAPI\r
606AllocateCopyPool (\r
607 IN UINTN AllocationSize,\r
608 IN CONST VOID *Buffer\r
609 )\r
610{\r
611 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
612}\r
613\r
614/**\r
615 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.\r
616\r
617 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies\r
618 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
619 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
620 is not enough memory remaining to satisfy the request, then NULL is returned.\r
621\r
622 If Buffer is NULL, then ASSERT().\r
623 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
624\r
625 @param AllocationSize The number of bytes to allocate and zero.\r
626 @param Buffer The buffer to copy to the allocated buffer.\r
627\r
628 @return A pointer to the allocated buffer or NULL if allocation fails.\r
629\r
630**/\r
631VOID *\r
632EFIAPI\r
633AllocateRuntimeCopyPool (\r
634 IN UINTN AllocationSize,\r
635 IN CONST VOID *Buffer\r
636 )\r
637{\r
638 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
639}\r
640\r
641/**\r
642 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.\r
643\r
644 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies\r
645 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
646 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
647 is not enough memory remaining to satisfy the request, then NULL is returned.\r
648\r
649 If Buffer is NULL, then ASSERT().\r
650 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
651\r
652 @param AllocationSize The number of bytes to allocate and zero.\r
653 @param Buffer The buffer to copy to the allocated buffer.\r
654\r
655 @return A pointer to the allocated buffer or NULL if allocation fails.\r
656\r
657**/\r
658VOID *\r
659EFIAPI\r
660AllocateReservedCopyPool (\r
661 IN UINTN AllocationSize,\r
662 IN CONST VOID *Buffer\r
663 )\r
664{\r
665 return NULL;\r
666}\r
667\r
668/**\r
669 Reallocates a buffer of a specified memory type.\r
670\r
671 Allocates and zeros the number bytes specified by NewSize from memory of the type\r
672 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and\r
673 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
674 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
675 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
676 enough memory remaining to satisfy the request, then NULL is returned.\r
677\r
678 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
679 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
680\r
681 @param PoolType The type of pool to allocate.\r
682 @param OldSize The size, in bytes, of OldBuffer.\r
683 @param NewSize The size, in bytes, of the buffer to reallocate.\r
684 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
685 parameter that may be NULL.\r
686\r
687 @return A pointer to the allocated buffer or NULL if allocation fails.\r
688\r
689**/\r
690VOID *\r
691InternalReallocatePool (\r
692 IN EFI_MEMORY_TYPE PoolType,\r
693 IN UINTN OldSize,\r
694 IN UINTN NewSize,\r
695 IN VOID *OldBuffer OPTIONAL\r
696 )\r
697{\r
698 VOID *NewBuffer;\r
699\r
700 NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);\r
91415a36 701 if ((NewBuffer != NULL) && (OldBuffer != NULL)) {\r
2c868eef
SV
702 CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));\r
703 FreePool (OldBuffer);\r
704 }\r
91415a36 705\r
2c868eef
SV
706 return NewBuffer;\r
707}\r
708\r
709/**\r
710 Reallocates a buffer of type EfiBootServicesData.\r
711\r
712 Allocates and zeros the number bytes specified by NewSize from memory of type\r
713 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and\r
714 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
715 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
716 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
717 enough memory remaining to satisfy the request, then NULL is returned.\r
718\r
719 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
720 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
721\r
722 @param OldSize The size, in bytes, of OldBuffer.\r
723 @param NewSize The size, in bytes, of the buffer to reallocate.\r
724 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
725 parameter that may be NULL.\r
726\r
727 @return A pointer to the allocated buffer or NULL if allocation fails.\r
728\r
729**/\r
730VOID *\r
731EFIAPI\r
732ReallocatePool (\r
733 IN UINTN OldSize,\r
734 IN UINTN NewSize,\r
735 IN VOID *OldBuffer OPTIONAL\r
736 )\r
737{\r
738 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
739}\r
740\r
741/**\r
742 Reallocates a buffer of type EfiRuntimeServicesData.\r
743\r
744 Allocates and zeros the number bytes specified by NewSize from memory of type\r
745 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and\r
746 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
747 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
748 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
749 enough memory remaining to satisfy the request, then NULL is returned.\r
750\r
751 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
752 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
753\r
754 @param OldSize The size, in bytes, of OldBuffer.\r
755 @param NewSize The size, in bytes, of the buffer to reallocate.\r
756 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
757 parameter that may be NULL.\r
758\r
759 @return A pointer to the allocated buffer or NULL if allocation fails.\r
760\r
761**/\r
762VOID *\r
763EFIAPI\r
764ReallocateRuntimePool (\r
765 IN UINTN OldSize,\r
766 IN UINTN NewSize,\r
767 IN VOID *OldBuffer OPTIONAL\r
768 )\r
769{\r
770 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
771}\r
772\r
773/**\r
774 Reallocates a buffer of type EfiReservedMemoryType.\r
775\r
776 Allocates and zeros the number bytes specified by NewSize from memory of type\r
777 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and\r
778 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
779 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
780 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
781 enough memory remaining to satisfy the request, then NULL is returned.\r
782\r
783 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
784 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
785\r
786 @param OldSize The size, in bytes, of OldBuffer.\r
787 @param NewSize The size, in bytes, of the buffer to reallocate.\r
788 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
789 parameter that may be NULL.\r
790\r
791 @return A pointer to the allocated buffer or NULL if allocation fails.\r
792\r
793**/\r
794VOID *\r
795EFIAPI\r
796ReallocateReservedPool (\r
797 IN UINTN OldSize,\r
798 IN UINTN NewSize,\r
799 IN VOID *OldBuffer OPTIONAL\r
800 )\r
801{\r
802 return NULL;\r
803}\r
804\r
805/**\r
806 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
807 Memory Allocation Library.\r
808\r
809 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
810 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
811 resources, then this function will perform no actions.\r
812\r
813 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
814 then ASSERT().\r
815\r
816 @param Buffer Pointer to the buffer to free.\r
817\r
818**/\r
819VOID\r
820EFIAPI\r
821FreePool (\r
91415a36 822 IN VOID *Buffer\r
2c868eef
SV
823 )\r
824{\r
91415a36 825 EFI_STATUS Status;\r
2c868eef
SV
826\r
827 Status = gMmst->MmFreePool (Buffer);\r
828 ASSERT_EFI_ERROR (Status);\r
829}\r
830\r
831/**\r
832 The constructor function calls MmInitializeMemoryServices to initialize\r
833 memory in MMRAM and caches EFI_MM_SYSTEM_TABLE pointer.\r
834\r
0720bb7e
SM
835 @param [in] ImageHandle The firmware allocated handle for the EFI image.\r
836 @param [in] MmSystemTable A pointer to the Management mode System Table.\r
2c868eef
SV
837\r
838 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
839\r
840**/\r
841EFI_STATUS\r
842EFIAPI\r
843MemoryAllocationLibConstructor (\r
91415a36
MK
844 IN EFI_HANDLE ImageHandle,\r
845 IN EFI_MM_SYSTEM_TABLE *MmSystemTable\r
2c868eef
SV
846 )\r
847{\r
91415a36 848 MM_CORE_PRIVATE_DATA *MmCorePrivate;\r
2c868eef 849 EFI_HOB_GUID_TYPE *GuidHob;\r
91415a36 850 MM_CORE_DATA_HOB_DATA *DataInHob;\r
2c868eef
SV
851 VOID *HobStart;\r
852 EFI_MMRAM_HOB_DESCRIPTOR_BLOCK *MmramRangesHobData;\r
853 EFI_MMRAM_DESCRIPTOR *MmramRanges;\r
91415a36 854 UINTN MmramRangeCount;\r
2c868eef
SV
855 EFI_HOB_GUID_TYPE *MmramRangesHob;\r
856\r
857 HobStart = GetHobList ();\r
858 DEBUG ((DEBUG_INFO, "StandaloneMmCoreMemoryAllocationLibConstructor - 0x%x\n", HobStart));\r
859\r
860 //\r
861 // Extract MM Core Private context from the Hob. If absent search for\r
862 // a Hob containing the MMRAM ranges\r
863 //\r
864 GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);\r
865 if (GuidHob == NULL) {\r
866 MmramRangesHob = GetNextGuidHob (&gEfiMmPeiMmramMemoryReserveGuid, HobStart);\r
867 if (MmramRangesHob == NULL) {\r
868 return EFI_UNSUPPORTED;\r
869 }\r
870\r
871 MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);\r
872 if (MmramRangesHobData == NULL) {\r
873 return EFI_UNSUPPORTED;\r
874 }\r
875\r
876 MmramRanges = MmramRangesHobData->Descriptor;\r
877 if (MmramRanges == NULL) {\r
878 return EFI_UNSUPPORTED;\r
879 }\r
880\r
91415a36 881 MmramRangeCount = (UINTN)MmramRangesHobData->NumberOfMmReservedRegions;\r
2c868eef
SV
882 if (MmramRanges == NULL) {\r
883 return EFI_UNSUPPORTED;\r
884 }\r
2c868eef 885 } else {\r
91415a36
MK
886 DataInHob = GET_GUID_HOB_DATA (GuidHob);\r
887 MmCorePrivate = (MM_CORE_PRIVATE_DATA *)(UINTN)DataInHob->Address;\r
2c868eef 888 MmramRanges = (EFI_MMRAM_DESCRIPTOR *)(UINTN)MmCorePrivate->MmramRanges;\r
91415a36 889 MmramRangeCount = (UINTN)MmCorePrivate->MmramRangeCount;\r
2c868eef
SV
890 }\r
891\r
892 {\r
91415a36 893 UINTN Index;\r
2c868eef
SV
894\r
895 DEBUG ((DEBUG_INFO, "MmramRangeCount - 0x%x\n", MmramRangeCount));\r
896 for (Index = 0; Index < MmramRangeCount; Index++) {\r
91415a36
MK
897 DEBUG ((\r
898 DEBUG_INFO,\r
899 "MmramRanges[%d]: 0x%016lx - 0x%016lx\n",\r
900 Index,\r
901 MmramRanges[Index].CpuStart,\r
902 MmramRanges[Index].PhysicalSize\r
903 ));\r
2c868eef
SV
904 }\r
905 }\r
906\r
907 //\r
908 // Initialize memory service using free MMRAM\r
909 //\r
910 DEBUG ((DEBUG_INFO, "MmInitializeMemoryServices\n"));\r
911 MmInitializeMemoryServices ((UINTN)MmramRangeCount, (VOID *)(UINTN)MmramRanges);\r
912\r
913 // Initialize MM Services Table\r
914 gMmst = MmSystemTable;\r
915 return EFI_SUCCESS;\r
916}\r