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