]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Pei/Memory/MemoryServices.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Memory / MemoryServices.c
... / ...
CommitLineData
1/** @file\r
2 EFI PEI Core memory services\r
3\r
4Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
5SPDX-License-Identifier: BSD-2-Clause-Patent\r
6\r
7**/\r
8\r
9#include "PeiMain.h"\r
10\r
11/**\r
12\r
13 Initialize the memory services.\r
14\r
15 @param PrivateData Points to PeiCore's private instance data.\r
16 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
17 environment, such as the size and location of temporary RAM, the stack location and\r
18 the BFV location.\r
19 @param OldCoreData Pointer to the PEI Core data.\r
20 NULL if being run in non-permanent memory mode.\r
21\r
22**/\r
23VOID\r
24InitializeMemoryServices (\r
25 IN PEI_CORE_INSTANCE *PrivateData,\r
26 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r
27 IN PEI_CORE_INSTANCE *OldCoreData\r
28 )\r
29{\r
30 PrivateData->SwitchStackSignal = FALSE;\r
31\r
32 //\r
33 // First entering PeiCore, following code will initialized some field\r
34 // in PeiCore's private data according to hand off data from SEC core.\r
35 //\r
36 if (OldCoreData == NULL) {\r
37 PrivateData->PeiMemoryInstalled = FALSE;\r
38 PrivateData->HobList.Raw = SecCoreData->PeiTemporaryRamBase;\r
39\r
40 PeiCoreBuildHobHandoffInfoTable (\r
41 BOOT_WITH_FULL_CONFIGURATION,\r
42 (EFI_PHYSICAL_ADDRESS)(UINTN)SecCoreData->PeiTemporaryRamBase,\r
43 (UINTN)SecCoreData->PeiTemporaryRamSize\r
44 );\r
45\r
46 //\r
47 // Set Ps to point to ServiceTableShadow in Cache\r
48 //\r
49 PrivateData->Ps = &(PrivateData->ServiceTableShadow);\r
50 }\r
51\r
52 return;\r
53}\r
54\r
55/**\r
56\r
57 This function registers the found memory configuration with the PEI Foundation.\r
58\r
59 The usage model is that the PEIM that discovers the permanent memory shall invoke this service.\r
60 This routine will hold discoveried memory information into PeiCore's private data,\r
61 and set SwitchStackSignal flag. After PEIM who discovery memory is dispatched,\r
62 PeiDispatcher will migrate temporary memory to permanent memory.\r
63\r
64 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
65 @param MemoryBegin Start of memory address.\r
66 @param MemoryLength Length of memory.\r
67\r
68 @return EFI_SUCCESS Always success.\r
69\r
70**/\r
71EFI_STATUS\r
72EFIAPI\r
73PeiInstallPeiMemory (\r
74 IN CONST EFI_PEI_SERVICES **PeiServices,\r
75 IN EFI_PHYSICAL_ADDRESS MemoryBegin,\r
76 IN UINT64 MemoryLength\r
77 )\r
78{\r
79 PEI_CORE_INSTANCE *PrivateData;\r
80\r
81 DEBUG ((DEBUG_INFO, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin, MemoryLength));\r
82 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
83\r
84 //\r
85 // PEI_SERVICE.InstallPeiMemory should only be called one time during whole PEI phase.\r
86 // If it is invoked more than one time, ASSERT information is given for developer debugging in debug tip and\r
87 // simply return EFI_SUCCESS in release tip to ignore it.\r
88 //\r
89 if (PrivateData->PeiMemoryInstalled) {\r
90 DEBUG ((DEBUG_ERROR, "ERROR: PeiInstallPeiMemory is called more than once!\n"));\r
91 ASSERT (FALSE);\r
92 return EFI_SUCCESS;\r
93 }\r
94\r
95 PrivateData->PhysicalMemoryBegin = MemoryBegin;\r
96 PrivateData->PhysicalMemoryLength = MemoryLength;\r
97 PrivateData->FreePhysicalMemoryTop = MemoryBegin + MemoryLength;\r
98\r
99 PrivateData->SwitchStackSignal = TRUE;\r
100\r
101 return EFI_SUCCESS;\r
102}\r
103\r
104/**\r
105 Migrate memory pages allocated in pre-memory phase.\r
106 Copy memory pages at temporary heap top to permanent heap top.\r
107\r
108 @param[in] Private Pointer to the private data passed in from caller.\r
109 @param[in] TemporaryRamMigrated Temporary memory has been migrated to permanent memory.\r
110\r
111**/\r
112VOID\r
113MigrateMemoryPages (\r
114 IN PEI_CORE_INSTANCE *Private,\r
115 IN BOOLEAN TemporaryRamMigrated\r
116 )\r
117{\r
118 EFI_PHYSICAL_ADDRESS NewMemPagesBase;\r
119 EFI_PHYSICAL_ADDRESS MemPagesBase;\r
120\r
121 Private->MemoryPages.Size = (UINTN)(Private->HobList.HandoffInformationTable->EfiMemoryTop -\r
122 Private->HobList.HandoffInformationTable->EfiFreeMemoryTop);\r
123 if (Private->MemoryPages.Size == 0) {\r
124 //\r
125 // No any memory page allocated in pre-memory phase.\r
126 //\r
127 return;\r
128 }\r
129\r
130 Private->MemoryPages.Base = Private->HobList.HandoffInformationTable->EfiFreeMemoryTop;\r
131\r
132 ASSERT (Private->MemoryPages.Size <= Private->FreePhysicalMemoryTop);\r
133 NewMemPagesBase = Private->FreePhysicalMemoryTop - Private->MemoryPages.Size;\r
134 NewMemPagesBase &= ~(UINT64)EFI_PAGE_MASK;\r
135 ASSERT (NewMemPagesBase >= Private->PhysicalMemoryBegin);\r
136 //\r
137 // Copy memory pages at temporary heap top to permanent heap top.\r
138 //\r
139 if (TemporaryRamMigrated) {\r
140 //\r
141 // Memory pages at temporary heap top has been migrated to permanent heap,\r
142 // Here still needs to copy them from permanent heap to permanent heap top.\r
143 //\r
144 MemPagesBase = Private->MemoryPages.Base;\r
145 if (Private->HeapOffsetPositive) {\r
146 MemPagesBase += Private->HeapOffset;\r
147 } else {\r
148 MemPagesBase -= Private->HeapOffset;\r
149 }\r
150\r
151 CopyMem ((VOID *)(UINTN)NewMemPagesBase, (VOID *)(UINTN)MemPagesBase, Private->MemoryPages.Size);\r
152 } else {\r
153 CopyMem ((VOID *)(UINTN)NewMemPagesBase, (VOID *)(UINTN)Private->MemoryPages.Base, Private->MemoryPages.Size);\r
154 }\r
155\r
156 if (NewMemPagesBase >= Private->MemoryPages.Base) {\r
157 Private->MemoryPages.OffsetPositive = TRUE;\r
158 Private->MemoryPages.Offset = (UINTN)(NewMemPagesBase - Private->MemoryPages.Base);\r
159 } else {\r
160 Private->MemoryPages.OffsetPositive = FALSE;\r
161 Private->MemoryPages.Offset = (UINTN)(Private->MemoryPages.Base - NewMemPagesBase);\r
162 }\r
163\r
164 DEBUG ((DEBUG_INFO, "Pages Offset = 0x%lX\n", (UINT64)Private->MemoryPages.Offset));\r
165\r
166 Private->FreePhysicalMemoryTop = NewMemPagesBase;\r
167}\r
168\r
169/**\r
170 Removes any FV HOBs whose base address is not in PEI installed memory.\r
171\r
172 @param[in] Private Pointer to PeiCore's private data structure.\r
173\r
174**/\r
175VOID\r
176RemoveFvHobsInTemporaryMemory (\r
177 IN PEI_CORE_INSTANCE *Private\r
178 )\r
179{\r
180 EFI_PEI_HOB_POINTERS Hob;\r
181 EFI_HOB_FIRMWARE_VOLUME *FirmwareVolumeHob;\r
182\r
183 DEBUG ((DEBUG_INFO, "Removing FVs in FV HOB not already migrated to permanent memory.\n"));\r
184\r
185 for (Hob.Raw = GetHobList (); !END_OF_HOB_LIST (Hob); Hob.Raw = GET_NEXT_HOB (Hob)) {\r
186 if ((GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV) || (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV2) || (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV3)) {\r
187 FirmwareVolumeHob = Hob.FirmwareVolume;\r
188 DEBUG ((DEBUG_INFO, " Found FV HOB.\n"));\r
189 DEBUG ((\r
190 DEBUG_INFO,\r
191 " BA=%016lx L=%016lx\n",\r
192 FirmwareVolumeHob->BaseAddress,\r
193 FirmwareVolumeHob->Length\r
194 ));\r
195 if (\r
196 !(\r
197 ((EFI_PHYSICAL_ADDRESS)(UINTN)FirmwareVolumeHob->BaseAddress >= Private->PhysicalMemoryBegin) &&\r
198 (((EFI_PHYSICAL_ADDRESS)(UINTN)FirmwareVolumeHob->BaseAddress + (FirmwareVolumeHob->Length - 1)) < Private->FreePhysicalMemoryTop)\r
199 )\r
200 )\r
201 {\r
202 DEBUG ((DEBUG_INFO, " Removing FV HOB to an FV in T-RAM (was not migrated).\n"));\r
203 Hob.Header->HobType = EFI_HOB_TYPE_UNUSED;\r
204 }\r
205 }\r
206 }\r
207}\r
208\r
209/**\r
210 Migrate the base address in firmware volume allocation HOBs\r
211 from temporary memory to PEI installed memory.\r
212\r
213 @param[in] PrivateData Pointer to PeiCore's private data structure.\r
214 @param[in] OrgFvHandle Address of FV Handle in temporary memory.\r
215 @param[in] FvHandle Address of FV Handle in permanent memory.\r
216\r
217**/\r
218VOID\r
219ConvertFvHob (\r
220 IN PEI_CORE_INSTANCE *PrivateData,\r
221 IN UINTN OrgFvHandle,\r
222 IN UINTN FvHandle\r
223 )\r
224{\r
225 EFI_PEI_HOB_POINTERS Hob;\r
226 EFI_HOB_FIRMWARE_VOLUME *FirmwareVolumeHob;\r
227 EFI_HOB_FIRMWARE_VOLUME2 *FirmwareVolume2Hob;\r
228 EFI_HOB_FIRMWARE_VOLUME3 *FirmwareVolume3Hob;\r
229\r
230 DEBUG ((DEBUG_INFO, "Converting FVs in FV HOB.\n"));\r
231\r
232 for (Hob.Raw = GetHobList (); !END_OF_HOB_LIST (Hob); Hob.Raw = GET_NEXT_HOB (Hob)) {\r
233 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV) {\r
234 FirmwareVolumeHob = Hob.FirmwareVolume;\r
235 if (FirmwareVolumeHob->BaseAddress == OrgFvHandle) {\r
236 FirmwareVolumeHob->BaseAddress = FvHandle;\r
237 }\r
238 } else if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV2) {\r
239 FirmwareVolume2Hob = Hob.FirmwareVolume2;\r
240 if (FirmwareVolume2Hob->BaseAddress == OrgFvHandle) {\r
241 FirmwareVolume2Hob->BaseAddress = FvHandle;\r
242 }\r
243 } else if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV3) {\r
244 FirmwareVolume3Hob = Hob.FirmwareVolume3;\r
245 if (FirmwareVolume3Hob->BaseAddress == OrgFvHandle) {\r
246 FirmwareVolume3Hob->BaseAddress = FvHandle;\r
247 }\r
248 }\r
249 }\r
250}\r
251\r
252/**\r
253 Migrate MemoryBaseAddress in memory allocation HOBs\r
254 from the temporary memory to PEI installed memory.\r
255\r
256 @param[in] PrivateData Pointer to PeiCore's private data structure.\r
257\r
258**/\r
259VOID\r
260ConvertMemoryAllocationHobs (\r
261 IN PEI_CORE_INSTANCE *PrivateData\r
262 )\r
263{\r
264 EFI_PEI_HOB_POINTERS Hob;\r
265 EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHob;\r
266 EFI_PHYSICAL_ADDRESS OldMemPagesBase;\r
267 UINTN OldMemPagesSize;\r
268\r
269 if (PrivateData->MemoryPages.Size == 0) {\r
270 //\r
271 // No any memory page allocated in pre-memory phase.\r
272 //\r
273 return;\r
274 }\r
275\r
276 OldMemPagesBase = PrivateData->MemoryPages.Base;\r
277 OldMemPagesSize = PrivateData->MemoryPages.Size;\r
278\r
279 MemoryAllocationHob = NULL;\r
280 Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r
281 while (Hob.Raw != NULL) {\r
282 MemoryAllocationHob = (EFI_HOB_MEMORY_ALLOCATION *)Hob.Raw;\r
283 if ((MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress >= OldMemPagesBase) &&\r
284 (MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress < (OldMemPagesBase + OldMemPagesSize))\r
285 )\r
286 {\r
287 if (PrivateData->MemoryPages.OffsetPositive) {\r
288 MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress += PrivateData->MemoryPages.Offset;\r
289 } else {\r
290 MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress -= PrivateData->MemoryPages.Offset;\r
291 }\r
292 }\r
293\r
294 Hob.Raw = GET_NEXT_HOB (Hob);\r
295 Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r
296 }\r
297}\r
298\r
299/**\r
300 Internal function to build a HOB for the memory allocation.\r
301 It will search and reuse the unused(freed) memory allocation HOB,\r
302 or build memory allocation HOB normally if no unused(freed) memory allocation HOB found.\r
303\r
304 @param[in] BaseAddress The 64 bit physical address of the memory.\r
305 @param[in] Length The length of the memory allocation in bytes.\r
306 @param[in] MemoryType The type of memory allocated by this HOB.\r
307\r
308**/\r
309VOID\r
310InternalBuildMemoryAllocationHob (\r
311 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
312 IN UINT64 Length,\r
313 IN EFI_MEMORY_TYPE MemoryType\r
314 )\r
315{\r
316 EFI_PEI_HOB_POINTERS Hob;\r
317 EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHob;\r
318\r
319 //\r
320 // Search unused(freed) memory allocation HOB.\r
321 //\r
322 MemoryAllocationHob = NULL;\r
323 Hob.Raw = GetFirstHob (EFI_HOB_TYPE_UNUSED);\r
324 while (Hob.Raw != NULL) {\r
325 if (Hob.Header->HobLength == sizeof (EFI_HOB_MEMORY_ALLOCATION)) {\r
326 MemoryAllocationHob = (EFI_HOB_MEMORY_ALLOCATION *)Hob.Raw;\r
327 break;\r
328 }\r
329\r
330 Hob.Raw = GET_NEXT_HOB (Hob);\r
331 Hob.Raw = GetNextHob (EFI_HOB_TYPE_UNUSED, Hob.Raw);\r
332 }\r
333\r
334 if (MemoryAllocationHob != NULL) {\r
335 //\r
336 // Reuse the unused(freed) memory allocation HOB.\r
337 //\r
338 MemoryAllocationHob->Header.HobType = EFI_HOB_TYPE_MEMORY_ALLOCATION;\r
339 ZeroMem (&(MemoryAllocationHob->AllocDescriptor.Name), sizeof (EFI_GUID));\r
340 MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress = BaseAddress;\r
341 MemoryAllocationHob->AllocDescriptor.MemoryLength = Length;\r
342 MemoryAllocationHob->AllocDescriptor.MemoryType = MemoryType;\r
343 //\r
344 // Zero the reserved space to match HOB spec\r
345 //\r
346 ZeroMem (MemoryAllocationHob->AllocDescriptor.Reserved, sizeof (MemoryAllocationHob->AllocDescriptor.Reserved));\r
347 } else {\r
348 //\r
349 // No unused(freed) memory allocation HOB found.\r
350 // Build memory allocation HOB normally.\r
351 //\r
352 BuildMemoryAllocationHob (\r
353 BaseAddress,\r
354 Length,\r
355 MemoryType\r
356 );\r
357 }\r
358}\r
359\r
360/**\r
361 Update or split memory allocation HOB for memory pages allocate and free.\r
362\r
363 @param[in, out] MemoryAllocationHob Pointer to the memory allocation HOB\r
364 that needs to be updated or split.\r
365 On output, it will be filled with\r
366 the input Memory, Bytes and MemoryType.\r
367 @param[in] Memory Memory to allocate or free.\r
368 @param[in] Bytes Bytes to allocate or free.\r
369 @param[in] MemoryType EfiConventionalMemory for pages free,\r
370 others for pages allocate.\r
371\r
372**/\r
373VOID\r
374UpdateOrSplitMemoryAllocationHob (\r
375 IN OUT EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHob,\r
376 IN EFI_PHYSICAL_ADDRESS Memory,\r
377 IN UINT64 Bytes,\r
378 IN EFI_MEMORY_TYPE MemoryType\r
379 )\r
380{\r
381 if ((Memory + Bytes) <\r
382 (MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress + MemoryAllocationHob->AllocDescriptor.MemoryLength))\r
383 {\r
384 //\r
385 // Last pages need to be split out.\r
386 //\r
387 InternalBuildMemoryAllocationHob (\r
388 Memory + Bytes,\r
389 (MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress + MemoryAllocationHob->AllocDescriptor.MemoryLength) - (Memory + Bytes),\r
390 MemoryAllocationHob->AllocDescriptor.MemoryType\r
391 );\r
392 }\r
393\r
394 if (Memory > MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress) {\r
395 //\r
396 // First pages need to be split out.\r
397 //\r
398 InternalBuildMemoryAllocationHob (\r
399 MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress,\r
400 Memory - MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress,\r
401 MemoryAllocationHob->AllocDescriptor.MemoryType\r
402 );\r
403 }\r
404\r
405 //\r
406 // Update the memory allocation HOB.\r
407 //\r
408 MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress = Memory;\r
409 MemoryAllocationHob->AllocDescriptor.MemoryLength = Bytes;\r
410 MemoryAllocationHob->AllocDescriptor.MemoryType = MemoryType;\r
411}\r
412\r
413/**\r
414 Merge adjacent free memory ranges in memory allocation HOBs.\r
415\r
416 @retval TRUE There are free memory ranges merged.\r
417 @retval FALSE No free memory ranges merged.\r
418\r
419**/\r
420BOOLEAN\r
421MergeFreeMemoryInMemoryAllocationHob (\r
422 VOID\r
423 )\r
424{\r
425 EFI_PEI_HOB_POINTERS Hob;\r
426 EFI_PEI_HOB_POINTERS Hob2;\r
427 EFI_HOB_MEMORY_ALLOCATION *MemoryHob;\r
428 EFI_HOB_MEMORY_ALLOCATION *MemoryHob2;\r
429 UINT64 Start;\r
430 UINT64 End;\r
431 BOOLEAN Merged;\r
432\r
433 Merged = FALSE;\r
434\r
435 Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r
436 while (Hob.Raw != NULL) {\r
437 if (Hob.MemoryAllocation->AllocDescriptor.MemoryType == EfiConventionalMemory) {\r
438 MemoryHob = (EFI_HOB_MEMORY_ALLOCATION *)Hob.Raw;\r
439 Start = MemoryHob->AllocDescriptor.MemoryBaseAddress;\r
440 End = MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength;\r
441\r
442 Hob2.Raw = GET_NEXT_HOB (Hob);\r
443 Hob2.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r
444 while (Hob2.Raw != NULL) {\r
445 if (Hob2.MemoryAllocation->AllocDescriptor.MemoryType == EfiConventionalMemory) {\r
446 MemoryHob2 = (EFI_HOB_MEMORY_ALLOCATION *)Hob2.Raw;\r
447 if (Start == (MemoryHob2->AllocDescriptor.MemoryBaseAddress + MemoryHob2->AllocDescriptor.MemoryLength)) {\r
448 //\r
449 // Merge adjacent two free memory ranges.\r
450 //\r
451 MemoryHob2->AllocDescriptor.MemoryLength += MemoryHob->AllocDescriptor.MemoryLength;\r
452 Merged = TRUE;\r
453 //\r
454 // Mark MemoryHob to be unused(freed).\r
455 //\r
456 MemoryHob->Header.HobType = EFI_HOB_TYPE_UNUSED;\r
457 break;\r
458 } else if (End == MemoryHob2->AllocDescriptor.MemoryBaseAddress) {\r
459 //\r
460 // Merge adjacent two free memory ranges.\r
461 //\r
462 MemoryHob2->AllocDescriptor.MemoryBaseAddress = MemoryHob->AllocDescriptor.MemoryBaseAddress;\r
463 MemoryHob2->AllocDescriptor.MemoryLength += MemoryHob->AllocDescriptor.MemoryLength;\r
464 Merged = TRUE;\r
465 //\r
466 // Mark MemoryHob to be unused(freed).\r
467 //\r
468 MemoryHob->Header.HobType = EFI_HOB_TYPE_UNUSED;\r
469 break;\r
470 }\r
471 }\r
472\r
473 Hob2.Raw = GET_NEXT_HOB (Hob2);\r
474 Hob2.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob2.Raw);\r
475 }\r
476 }\r
477\r
478 Hob.Raw = GET_NEXT_HOB (Hob);\r
479 Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r
480 }\r
481\r
482 return Merged;\r
483}\r
484\r
485/**\r
486 Find free memory by searching memory allocation HOBs.\r
487\r
488 @param[in] MemoryType The type of memory to allocate.\r
489 @param[in] Pages The number of contiguous 4 KB pages to allocate.\r
490 @param[in] Granularity Page allocation granularity.\r
491 @param[out] Memory Pointer to a physical address. On output, the address is set to the base\r
492 of the page range that was allocated.\r
493\r
494 @retval EFI_SUCCESS The memory range was successfully allocated.\r
495 @retval EFI_NOT_FOUND No memory allocation HOB with big enough free memory found.\r
496\r
497**/\r
498EFI_STATUS\r
499FindFreeMemoryFromMemoryAllocationHob (\r
500 IN EFI_MEMORY_TYPE MemoryType,\r
501 IN UINTN Pages,\r
502 IN UINTN Granularity,\r
503 OUT EFI_PHYSICAL_ADDRESS *Memory\r
504 )\r
505{\r
506 EFI_PEI_HOB_POINTERS Hob;\r
507 EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHob;\r
508 UINT64 Bytes;\r
509 EFI_PHYSICAL_ADDRESS BaseAddress;\r
510\r
511 Bytes = LShiftU64 (Pages, EFI_PAGE_SHIFT);\r
512\r
513 BaseAddress = 0;\r
514 MemoryAllocationHob = NULL;\r
515 Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r
516 while (Hob.Raw != NULL) {\r
517 if ((Hob.MemoryAllocation->AllocDescriptor.MemoryType == EfiConventionalMemory) &&\r
518 (Hob.MemoryAllocation->AllocDescriptor.MemoryLength >= Bytes))\r
519 {\r
520 //\r
521 // Found one memory allocation HOB with big enough free memory.\r
522 //\r
523 MemoryAllocationHob = (EFI_HOB_MEMORY_ALLOCATION *)Hob.Raw;\r
524 BaseAddress = MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress +\r
525 MemoryAllocationHob->AllocDescriptor.MemoryLength - Bytes;\r
526 //\r
527 // Make sure the granularity could be satisfied.\r
528 //\r
529 BaseAddress &= ~((EFI_PHYSICAL_ADDRESS)Granularity - 1);\r
530 if (BaseAddress >= MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress) {\r
531 break;\r
532 }\r
533\r
534 BaseAddress = 0;\r
535 MemoryAllocationHob = NULL;\r
536 }\r
537\r
538 //\r
539 // Continue to find.\r
540 //\r
541 Hob.Raw = GET_NEXT_HOB (Hob);\r
542 Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r
543 }\r
544\r
545 if (MemoryAllocationHob != NULL) {\r
546 UpdateOrSplitMemoryAllocationHob (MemoryAllocationHob, BaseAddress, Bytes, MemoryType);\r
547 *Memory = BaseAddress;\r
548 return EFI_SUCCESS;\r
549 } else {\r
550 if (MergeFreeMemoryInMemoryAllocationHob ()) {\r
551 //\r
552 // Retry if there are free memory ranges merged.\r
553 //\r
554 return FindFreeMemoryFromMemoryAllocationHob (MemoryType, Pages, Granularity, Memory);\r
555 }\r
556\r
557 return EFI_NOT_FOUND;\r
558 }\r
559}\r
560\r
561/**\r
562 The purpose of the service is to publish an interface that allows\r
563 PEIMs to allocate memory ranges that are managed by the PEI Foundation.\r
564\r
565 Prior to InstallPeiMemory() being called, PEI will allocate pages from the heap.\r
566 After InstallPeiMemory() is called, PEI will allocate pages within the region\r
567 of memory provided by InstallPeiMemory() service in a best-effort fashion.\r
568 Location-specific allocations are not managed by the PEI foundation code.\r
569\r
570 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
571 @param MemoryType The type of memory to allocate.\r
572 @param Pages The number of contiguous 4 KB pages to allocate.\r
573 @param Memory Pointer to a physical address. On output, the address is set to the base\r
574 of the page range that was allocated.\r
575\r
576 @retval EFI_SUCCESS The memory range was successfully allocated.\r
577 @retval EFI_OUT_OF_RESOURCES The pages could not be allocated.\r
578 @retval EFI_INVALID_PARAMETER Type is not equal to EfiLoaderCode, EfiLoaderData, EfiRuntimeServicesCode,\r
579 EfiRuntimeServicesData, EfiBootServicesCode, EfiBootServicesData,\r
580 EfiACPIReclaimMemory, EfiReservedMemoryType, or EfiACPIMemoryNVS.\r
581\r
582**/\r
583EFI_STATUS\r
584EFIAPI\r
585PeiAllocatePages (\r
586 IN CONST EFI_PEI_SERVICES **PeiServices,\r
587 IN EFI_MEMORY_TYPE MemoryType,\r
588 IN UINTN Pages,\r
589 OUT EFI_PHYSICAL_ADDRESS *Memory\r
590 )\r
591{\r
592 EFI_STATUS Status;\r
593 PEI_CORE_INSTANCE *PrivateData;\r
594 EFI_PEI_HOB_POINTERS Hob;\r
595 EFI_PHYSICAL_ADDRESS *FreeMemoryTop;\r
596 EFI_PHYSICAL_ADDRESS *FreeMemoryBottom;\r
597 UINTN RemainingPages;\r
598 UINTN Granularity;\r
599 UINTN Padding;\r
600\r
601 if ((MemoryType != EfiLoaderCode) &&\r
602 (MemoryType != EfiLoaderData) &&\r
603 (MemoryType != EfiRuntimeServicesCode) &&\r
604 (MemoryType != EfiRuntimeServicesData) &&\r
605 (MemoryType != EfiBootServicesCode) &&\r
606 (MemoryType != EfiBootServicesData) &&\r
607 (MemoryType != EfiACPIReclaimMemory) &&\r
608 (MemoryType != EfiReservedMemoryType) &&\r
609 (MemoryType != EfiACPIMemoryNVS))\r
610 {\r
611 return EFI_INVALID_PARAMETER;\r
612 }\r
613\r
614 Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r
615\r
616 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
617 Hob.Raw = PrivateData->HobList.Raw;\r
618\r
619 if (Hob.Raw == NULL) {\r
620 //\r
621 // HOB is not initialized yet.\r
622 //\r
623 return EFI_NOT_AVAILABLE_YET;\r
624 }\r
625\r
626 if ((RUNTIME_PAGE_ALLOCATION_GRANULARITY > DEFAULT_PAGE_ALLOCATION_GRANULARITY) &&\r
627 ((MemoryType == EfiACPIReclaimMemory) ||\r
628 (MemoryType == EfiACPIMemoryNVS) ||\r
629 (MemoryType == EfiRuntimeServicesCode) ||\r
630 (MemoryType == EfiRuntimeServicesData)))\r
631 {\r
632 Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r
633\r
634 DEBUG ((\r
635 DEBUG_INFO,\r
636 "AllocatePages: aligning allocation to %d KB\n",\r
637 Granularity / SIZE_1KB\r
638 ));\r
639 }\r
640\r
641 if (!PrivateData->PeiMemoryInstalled && PrivateData->SwitchStackSignal) {\r
642 //\r
643 // When PeiInstallMemory is called but temporary memory has *not* been moved to permanent memory,\r
644 // the AllocatePage will depend on the field of PEI_CORE_INSTANCE structure.\r
645 //\r
646 FreeMemoryTop = &(PrivateData->FreePhysicalMemoryTop);\r
647 FreeMemoryBottom = &(PrivateData->PhysicalMemoryBegin);\r
648 } else {\r
649 FreeMemoryTop = &(Hob.HandoffInformationTable->EfiFreeMemoryTop);\r
650 FreeMemoryBottom = &(Hob.HandoffInformationTable->EfiFreeMemoryBottom);\r
651 }\r
652\r
653 //\r
654 // Check to see if on correct boundary for the memory type.\r
655 // If not aligned, make the allocation aligned.\r
656 //\r
657 Padding = *(FreeMemoryTop) & (Granularity - 1);\r
658 if ((UINTN)(*FreeMemoryTop - *FreeMemoryBottom) < Padding) {\r
659 DEBUG ((DEBUG_ERROR, "AllocatePages failed: Out of space after padding.\n"));\r
660 return EFI_OUT_OF_RESOURCES;\r
661 }\r
662\r
663 *(FreeMemoryTop) -= Padding;\r
664 if (Padding >= EFI_PAGE_SIZE) {\r
665 //\r
666 // Create a memory allocation HOB to cover\r
667 // the pages that we will lose to rounding\r
668 //\r
669 InternalBuildMemoryAllocationHob (\r
670 *(FreeMemoryTop),\r
671 Padding & ~(UINTN)EFI_PAGE_MASK,\r
672 EfiConventionalMemory\r
673 );\r
674 }\r
675\r
676 //\r
677 // Verify that there is sufficient memory to satisfy the allocation.\r
678 //\r
679 RemainingPages = (UINTN)(*FreeMemoryTop - *FreeMemoryBottom) >> EFI_PAGE_SHIFT;\r
680 //\r
681 // The number of remaining pages needs to be greater than or equal to that of the request pages.\r
682 //\r
683 Pages = ALIGN_VALUE (Pages, EFI_SIZE_TO_PAGES (Granularity));\r
684 if (RemainingPages < Pages) {\r
685 //\r
686 // Try to find free memory by searching memory allocation HOBs.\r
687 //\r
688 Status = FindFreeMemoryFromMemoryAllocationHob (MemoryType, Pages, Granularity, Memory);\r
689 if (!EFI_ERROR (Status)) {\r
690 return Status;\r
691 }\r
692\r
693 DEBUG ((DEBUG_ERROR, "AllocatePages failed: No 0x%lx Pages is available.\n", (UINT64)Pages));\r
694 DEBUG ((DEBUG_ERROR, "There is only left 0x%lx pages memory resource to be allocated.\n", (UINT64)RemainingPages));\r
695 return EFI_OUT_OF_RESOURCES;\r
696 } else {\r
697 //\r
698 // Update the PHIT to reflect the memory usage\r
699 //\r
700 *(FreeMemoryTop) -= Pages * EFI_PAGE_SIZE;\r
701\r
702 //\r
703 // Update the value for the caller\r
704 //\r
705 *Memory = *(FreeMemoryTop);\r
706\r
707 //\r
708 // Create a memory allocation HOB.\r
709 //\r
710 InternalBuildMemoryAllocationHob (\r
711 *(FreeMemoryTop),\r
712 Pages * EFI_PAGE_SIZE,\r
713 MemoryType\r
714 );\r
715\r
716 return EFI_SUCCESS;\r
717 }\r
718}\r
719\r
720/**\r
721 Mark the memory allocation HOB to be unused(freed) and update *FreeMemoryTop\r
722 if MemoryBaseAddress == *FreeMemoryTop.\r
723\r
724 @param[in] PrivateData Pointer to PeiCore's private data structure.\r
725 @param[in, out] MemoryAllocationHobToFree Pointer to memory allocation HOB to be freed.\r
726\r
727**/\r
728VOID\r
729FreeMemoryAllocationHob (\r
730 IN PEI_CORE_INSTANCE *PrivateData,\r
731 IN OUT EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHobToFree\r
732 )\r
733{\r
734 EFI_PEI_HOB_POINTERS Hob;\r
735 EFI_PHYSICAL_ADDRESS *FreeMemoryTop;\r
736 EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHob;\r
737\r
738 Hob.Raw = PrivateData->HobList.Raw;\r
739\r
740 if (!PrivateData->PeiMemoryInstalled && PrivateData->SwitchStackSignal) {\r
741 //\r
742 // When PeiInstallMemory is called but temporary memory has *not* been moved to permanent memory,\r
743 // use the FreePhysicalMemoryTop field of PEI_CORE_INSTANCE structure.\r
744 //\r
745 FreeMemoryTop = &(PrivateData->FreePhysicalMemoryTop);\r
746 } else {\r
747 FreeMemoryTop = &(Hob.HandoffInformationTable->EfiFreeMemoryTop);\r
748 }\r
749\r
750 if (MemoryAllocationHobToFree->AllocDescriptor.MemoryBaseAddress == *FreeMemoryTop) {\r
751 //\r
752 // Update *FreeMemoryTop.\r
753 //\r
754 *FreeMemoryTop += MemoryAllocationHobToFree->AllocDescriptor.MemoryLength;\r
755 //\r
756 // Mark the memory allocation HOB to be unused(freed).\r
757 //\r
758 MemoryAllocationHobToFree->Header.HobType = EFI_HOB_TYPE_UNUSED;\r
759\r
760 MemoryAllocationHob = NULL;\r
761 Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r
762 while (Hob.Raw != NULL) {\r
763 if ((Hob.MemoryAllocation->AllocDescriptor.MemoryType == EfiConventionalMemory) &&\r
764 (Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress == *FreeMemoryTop))\r
765 {\r
766 //\r
767 // Found memory allocation HOB that has EfiConventionalMemory MemoryType and\r
768 // MemoryBaseAddress == new *FreeMemoryTop.\r
769 //\r
770 MemoryAllocationHob = (EFI_HOB_MEMORY_ALLOCATION *)Hob.Raw;\r
771 break;\r
772 }\r
773\r
774 Hob.Raw = GET_NEXT_HOB (Hob);\r
775 Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r
776 }\r
777\r
778 //\r
779 // Free memory allocation HOB iteratively.\r
780 //\r
781 if (MemoryAllocationHob != NULL) {\r
782 FreeMemoryAllocationHob (PrivateData, MemoryAllocationHob);\r
783 }\r
784 }\r
785}\r
786\r
787/**\r
788 Frees memory pages.\r
789\r
790 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
791 @param[in] Memory The base physical address of the pages to be freed.\r
792 @param[in] Pages The number of contiguous 4 KB pages to free.\r
793\r
794 @retval EFI_SUCCESS The requested pages were freed.\r
795 @retval EFI_INVALID_PARAMETER Memory is not a page-aligned address or Pages is invalid.\r
796 @retval EFI_NOT_FOUND The requested memory pages were not allocated with\r
797 AllocatePages().\r
798\r
799**/\r
800EFI_STATUS\r
801EFIAPI\r
802PeiFreePages (\r
803 IN CONST EFI_PEI_SERVICES **PeiServices,\r
804 IN EFI_PHYSICAL_ADDRESS Memory,\r
805 IN UINTN Pages\r
806 )\r
807{\r
808 PEI_CORE_INSTANCE *PrivateData;\r
809 UINT64 Bytes;\r
810 UINT64 Start;\r
811 UINT64 End;\r
812 EFI_PEI_HOB_POINTERS Hob;\r
813 EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHob;\r
814\r
815 Bytes = LShiftU64 (Pages, EFI_PAGE_SHIFT);\r
816 Start = Memory;\r
817 End = Start + Bytes - 1;\r
818\r
819 if ((Pages == 0) || ((Start & EFI_PAGE_MASK) != 0) || (Start >= End)) {\r
820 return EFI_INVALID_PARAMETER;\r
821 }\r
822\r
823 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
824 Hob.Raw = PrivateData->HobList.Raw;\r
825\r
826 if (Hob.Raw == NULL) {\r
827 //\r
828 // HOB is not initialized yet.\r
829 //\r
830 return EFI_NOT_AVAILABLE_YET;\r
831 }\r
832\r
833 MemoryAllocationHob = NULL;\r
834 Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r
835 while (Hob.Raw != NULL) {\r
836 if ((Hob.MemoryAllocation->AllocDescriptor.MemoryType != EfiConventionalMemory) &&\r
837 (Memory >= Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress) &&\r
838 ((Memory + Bytes) <= (Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress + Hob.MemoryAllocation->AllocDescriptor.MemoryLength)))\r
839 {\r
840 //\r
841 // Found the memory allocation HOB that includes the memory pages to be freed.\r
842 //\r
843 MemoryAllocationHob = (EFI_HOB_MEMORY_ALLOCATION *)Hob.Raw;\r
844 break;\r
845 }\r
846\r
847 Hob.Raw = GET_NEXT_HOB (Hob);\r
848 Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r
849 }\r
850\r
851 if (MemoryAllocationHob != NULL) {\r
852 UpdateOrSplitMemoryAllocationHob (MemoryAllocationHob, Memory, Bytes, EfiConventionalMemory);\r
853 FreeMemoryAllocationHob (PrivateData, MemoryAllocationHob);\r
854 return EFI_SUCCESS;\r
855 } else {\r
856 return EFI_NOT_FOUND;\r
857 }\r
858}\r
859\r
860/**\r
861\r
862 Pool allocation service. Before permanent memory is discovered, the pool will\r
863 be allocated in the heap in temporary memory. Generally, the size of the heap in temporary\r
864 memory does not exceed 64K, so the biggest pool size could be allocated is\r
865 64K.\r
866\r
867 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
868 @param Size Amount of memory required\r
869 @param Buffer Address of pointer to the buffer\r
870\r
871 @retval EFI_SUCCESS The allocation was successful\r
872 @retval EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement\r
873 to allocate the requested size.\r
874\r
875**/\r
876EFI_STATUS\r
877EFIAPI\r
878PeiAllocatePool (\r
879 IN CONST EFI_PEI_SERVICES **PeiServices,\r
880 IN UINTN Size,\r
881 OUT VOID **Buffer\r
882 )\r
883{\r
884 EFI_STATUS Status;\r
885 EFI_HOB_MEMORY_POOL *Hob;\r
886\r
887 //\r
888 // If some "post-memory" PEIM wishes to allocate larger pool,\r
889 // it should use AllocatePages service instead.\r
890 //\r
891\r
892 //\r
893 // Generally, the size of heap in temporary memory does not exceed 64K,\r
894 // HobLength is multiples of 8 bytes, so the maximum size of pool is 0xFFF8 - sizeof (EFI_HOB_MEMORY_POOL)\r
895 //\r
896 if (Size > (0xFFF8 - sizeof (EFI_HOB_MEMORY_POOL))) {\r
897 return EFI_OUT_OF_RESOURCES;\r
898 }\r
899\r
900 Status = PeiServicesCreateHob (\r
901 EFI_HOB_TYPE_MEMORY_POOL,\r
902 (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + Size),\r
903 (VOID **)&Hob\r
904 );\r
905 ASSERT_EFI_ERROR (Status);\r
906\r
907 if (EFI_ERROR (Status)) {\r
908 *Buffer = NULL;\r
909 } else {\r
910 *Buffer = Hob + 1;\r
911 }\r
912\r
913 return Status;\r
914}\r