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