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