2 EFI PEI Core memory services
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
13 Initialize the memory services.
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
19 @param OldCoreData Pointer to the PEI Core data.
20 NULL if being run in non-permanent memory mode.
24 InitializeMemoryServices (
25 IN PEI_CORE_INSTANCE
*PrivateData
,
26 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreData
,
27 IN PEI_CORE_INSTANCE
*OldCoreData
30 PrivateData
->SwitchStackSignal
= FALSE
;
33 // First entering PeiCore, following code will initialized some field
34 // in PeiCore's private data according to hand off data from SEC core.
36 if (OldCoreData
== NULL
) {
37 PrivateData
->PeiMemoryInstalled
= FALSE
;
38 PrivateData
->HobList
.Raw
= SecCoreData
->PeiTemporaryRamBase
;
40 PeiCoreBuildHobHandoffInfoTable (
41 BOOT_WITH_FULL_CONFIGURATION
,
42 (EFI_PHYSICAL_ADDRESS
)(UINTN
)SecCoreData
->PeiTemporaryRamBase
,
43 (UINTN
)SecCoreData
->PeiTemporaryRamSize
47 // Set Ps to point to ServiceTableShadow in Cache
49 PrivateData
->Ps
= &(PrivateData
->ServiceTableShadow
);
57 This function registers the found memory configuration with the PEI Foundation.
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.
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.
68 @return EFI_SUCCESS Always success.
74 IN CONST EFI_PEI_SERVICES
**PeiServices
,
75 IN EFI_PHYSICAL_ADDRESS MemoryBegin
,
76 IN UINT64 MemoryLength
79 PEI_CORE_INSTANCE
*PrivateData
;
81 DEBUG ((DEBUG_INFO
, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin
, MemoryLength
));
82 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
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.
89 if (PrivateData
->PeiMemoryInstalled
) {
90 DEBUG ((DEBUG_ERROR
, "ERROR: PeiInstallPeiMemory is called more than once!\n"));
95 PrivateData
->PhysicalMemoryBegin
= MemoryBegin
;
96 PrivateData
->PhysicalMemoryLength
= MemoryLength
;
97 PrivateData
->FreePhysicalMemoryTop
= MemoryBegin
+ MemoryLength
;
99 PrivateData
->SwitchStackSignal
= TRUE
;
105 Migrate memory pages allocated in pre-memory phase.
106 Copy memory pages at temporary heap top to permanent heap top.
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.
114 IN PEI_CORE_INSTANCE
*Private
,
115 IN BOOLEAN TemporaryRamMigrated
118 EFI_PHYSICAL_ADDRESS NewMemPagesBase
;
119 EFI_PHYSICAL_ADDRESS MemPagesBase
;
121 Private
->MemoryPages
.Size
= (UINTN
)(Private
->HobList
.HandoffInformationTable
->EfiMemoryTop
-
122 Private
->HobList
.HandoffInformationTable
->EfiFreeMemoryTop
);
123 if (Private
->MemoryPages
.Size
== 0) {
125 // No any memory page allocated in pre-memory phase.
130 Private
->MemoryPages
.Base
= Private
->HobList
.HandoffInformationTable
->EfiFreeMemoryTop
;
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
);
137 // Copy memory pages at temporary heap top to permanent heap top.
139 if (TemporaryRamMigrated
) {
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.
144 MemPagesBase
= Private
->MemoryPages
.Base
;
145 if (Private
->HeapOffsetPositive
) {
146 MemPagesBase
+= Private
->HeapOffset
;
148 MemPagesBase
-= Private
->HeapOffset
;
151 CopyMem ((VOID
*)(UINTN
)NewMemPagesBase
, (VOID
*)(UINTN
)MemPagesBase
, Private
->MemoryPages
.Size
);
153 CopyMem ((VOID
*)(UINTN
)NewMemPagesBase
, (VOID
*)(UINTN
)Private
->MemoryPages
.Base
, Private
->MemoryPages
.Size
);
156 if (NewMemPagesBase
>= Private
->MemoryPages
.Base
) {
157 Private
->MemoryPages
.OffsetPositive
= TRUE
;
158 Private
->MemoryPages
.Offset
= (UINTN
)(NewMemPagesBase
- Private
->MemoryPages
.Base
);
160 Private
->MemoryPages
.OffsetPositive
= FALSE
;
161 Private
->MemoryPages
.Offset
= (UINTN
)(Private
->MemoryPages
.Base
- NewMemPagesBase
);
164 DEBUG ((DEBUG_INFO
, "Pages Offset = 0x%lX\n", (UINT64
)Private
->MemoryPages
.Offset
));
166 Private
->FreePhysicalMemoryTop
= NewMemPagesBase
;
170 Removes any FV HOBs whose base address is not in PEI installed memory.
172 @param[in] Private Pointer to PeiCore's private data structure.
176 RemoveFvHobsInTemporaryMemory (
177 IN PEI_CORE_INSTANCE
*Private
180 EFI_PEI_HOB_POINTERS Hob
;
181 EFI_HOB_FIRMWARE_VOLUME
*FirmwareVolumeHob
;
183 DEBUG ((DEBUG_INFO
, "Removing FVs in FV HOB not already migrated to permanent memory.\n"));
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"));
191 " BA=%016lx L=%016lx\n",
192 FirmwareVolumeHob
->BaseAddress
,
193 FirmwareVolumeHob
->Length
197 ((EFI_PHYSICAL_ADDRESS
)(UINTN
)FirmwareVolumeHob
->BaseAddress
>= Private
->PhysicalMemoryBegin
) &&
198 (((EFI_PHYSICAL_ADDRESS
)(UINTN
)FirmwareVolumeHob
->BaseAddress
+ (FirmwareVolumeHob
->Length
- 1)) < Private
->FreePhysicalMemoryTop
)
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
;
210 Migrate the base address in firmware volume allocation HOBs
211 from temporary memory to PEI installed memory.
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.
220 IN PEI_CORE_INSTANCE
*PrivateData
,
221 IN UINTN OrgFvHandle
,
225 EFI_PEI_HOB_POINTERS Hob
;
226 EFI_HOB_FIRMWARE_VOLUME
*FirmwareVolumeHob
;
227 EFI_HOB_FIRMWARE_VOLUME2
*FirmwareVolume2Hob
;
228 EFI_HOB_FIRMWARE_VOLUME3
*FirmwareVolume3Hob
;
230 DEBUG ((DEBUG_INFO
, "Converting FVs in FV HOB.\n"));
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
;
238 } else if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_FV2
) {
239 FirmwareVolume2Hob
= Hob
.FirmwareVolume2
;
240 if (FirmwareVolume2Hob
->BaseAddress
== OrgFvHandle
) {
241 FirmwareVolume2Hob
->BaseAddress
= FvHandle
;
243 } else if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_FV3
) {
244 FirmwareVolume3Hob
= Hob
.FirmwareVolume3
;
245 if (FirmwareVolume3Hob
->BaseAddress
== OrgFvHandle
) {
246 FirmwareVolume3Hob
->BaseAddress
= FvHandle
;
253 Migrate MemoryBaseAddress in memory allocation HOBs
254 from the temporary memory to PEI installed memory.
256 @param[in] PrivateData Pointer to PeiCore's private data structure.
260 ConvertMemoryAllocationHobs (
261 IN PEI_CORE_INSTANCE
*PrivateData
264 EFI_PEI_HOB_POINTERS Hob
;
265 EFI_HOB_MEMORY_ALLOCATION
*MemoryAllocationHob
;
266 EFI_PHYSICAL_ADDRESS OldMemPagesBase
;
267 UINTN OldMemPagesSize
;
269 if (PrivateData
->MemoryPages
.Size
== 0) {
271 // No any memory page allocated in pre-memory phase.
276 OldMemPagesBase
= PrivateData
->MemoryPages
.Base
;
277 OldMemPagesSize
= PrivateData
->MemoryPages
.Size
;
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
))
287 if (PrivateData
->MemoryPages
.OffsetPositive
) {
288 MemoryAllocationHob
->AllocDescriptor
.MemoryBaseAddress
+= PrivateData
->MemoryPages
.Offset
;
290 MemoryAllocationHob
->AllocDescriptor
.MemoryBaseAddress
-= PrivateData
->MemoryPages
.Offset
;
294 Hob
.Raw
= GET_NEXT_HOB (Hob
);
295 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob
.Raw
);
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.
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.
310 InternalBuildMemoryAllocationHob (
311 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
313 IN EFI_MEMORY_TYPE MemoryType
316 EFI_PEI_HOB_POINTERS Hob
;
317 EFI_HOB_MEMORY_ALLOCATION
*MemoryAllocationHob
;
320 // Search unused(freed) memory allocation HOB.
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
;
330 Hob
.Raw
= GET_NEXT_HOB (Hob
);
331 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_UNUSED
, Hob
.Raw
);
334 if (MemoryAllocationHob
!= NULL
) {
336 // Reuse the unused(freed) memory allocation HOB.
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
;
344 // Zero the reserved space to match HOB spec
346 ZeroMem (MemoryAllocationHob
->AllocDescriptor
.Reserved
, sizeof (MemoryAllocationHob
->AllocDescriptor
.Reserved
));
349 // No unused(freed) memory allocation HOB found.
350 // Build memory allocation HOB normally.
352 BuildMemoryAllocationHob (
361 Update or split memory allocation HOB for memory pages allocate and free.
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.
374 UpdateOrSplitMemoryAllocationHob (
375 IN OUT EFI_HOB_MEMORY_ALLOCATION
*MemoryAllocationHob
,
376 IN EFI_PHYSICAL_ADDRESS Memory
,
378 IN EFI_MEMORY_TYPE MemoryType
381 if ((Memory
+ Bytes
) <
382 (MemoryAllocationHob
->AllocDescriptor
.MemoryBaseAddress
+ MemoryAllocationHob
->AllocDescriptor
.MemoryLength
))
385 // Last pages need to be split out.
387 InternalBuildMemoryAllocationHob (
389 (MemoryAllocationHob
->AllocDescriptor
.MemoryBaseAddress
+ MemoryAllocationHob
->AllocDescriptor
.MemoryLength
) - (Memory
+ Bytes
),
390 MemoryAllocationHob
->AllocDescriptor
.MemoryType
394 if (Memory
> MemoryAllocationHob
->AllocDescriptor
.MemoryBaseAddress
) {
396 // First pages need to be split out.
398 InternalBuildMemoryAllocationHob (
399 MemoryAllocationHob
->AllocDescriptor
.MemoryBaseAddress
,
400 Memory
- MemoryAllocationHob
->AllocDescriptor
.MemoryBaseAddress
,
401 MemoryAllocationHob
->AllocDescriptor
.MemoryType
406 // Update the memory allocation HOB.
408 MemoryAllocationHob
->AllocDescriptor
.MemoryBaseAddress
= Memory
;
409 MemoryAllocationHob
->AllocDescriptor
.MemoryLength
= Bytes
;
410 MemoryAllocationHob
->AllocDescriptor
.MemoryType
= MemoryType
;
414 Merge adjacent free memory ranges in memory allocation HOBs.
416 @retval TRUE There are free memory ranges merged.
417 @retval FALSE No free memory ranges merged.
421 MergeFreeMemoryInMemoryAllocationHob (
425 EFI_PEI_HOB_POINTERS Hob
;
426 EFI_PEI_HOB_POINTERS Hob2
;
427 EFI_HOB_MEMORY_ALLOCATION
*MemoryHob
;
428 EFI_HOB_MEMORY_ALLOCATION
*MemoryHob2
;
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
;
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
)) {
449 // Merge adjacent two free memory ranges.
451 MemoryHob2
->AllocDescriptor
.MemoryLength
+= MemoryHob
->AllocDescriptor
.MemoryLength
;
454 // Mark MemoryHob to be unused(freed).
456 MemoryHob
->Header
.HobType
= EFI_HOB_TYPE_UNUSED
;
458 } else if (End
== MemoryHob2
->AllocDescriptor
.MemoryBaseAddress
) {
460 // Merge adjacent two free memory ranges.
462 MemoryHob2
->AllocDescriptor
.MemoryBaseAddress
= MemoryHob
->AllocDescriptor
.MemoryBaseAddress
;
463 MemoryHob2
->AllocDescriptor
.MemoryLength
+= MemoryHob
->AllocDescriptor
.MemoryLength
;
466 // Mark MemoryHob to be unused(freed).
468 MemoryHob
->Header
.HobType
= EFI_HOB_TYPE_UNUSED
;
473 Hob2
.Raw
= GET_NEXT_HOB (Hob2
);
474 Hob2
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob2
.Raw
);
478 Hob
.Raw
= GET_NEXT_HOB (Hob
);
479 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob
.Raw
);
486 Find free memory by searching memory allocation HOBs.
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.
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.
499 FindFreeMemoryFromMemoryAllocationHob (
500 IN EFI_MEMORY_TYPE MemoryType
,
502 IN UINTN Granularity
,
503 OUT EFI_PHYSICAL_ADDRESS
*Memory
506 EFI_PEI_HOB_POINTERS Hob
;
507 EFI_HOB_MEMORY_ALLOCATION
*MemoryAllocationHob
;
509 EFI_PHYSICAL_ADDRESS BaseAddress
;
511 Bytes
= LShiftU64 (Pages
, EFI_PAGE_SHIFT
);
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
))
521 // Found one memory allocation HOB with big enough free memory.
523 MemoryAllocationHob
= (EFI_HOB_MEMORY_ALLOCATION
*)Hob
.Raw
;
524 BaseAddress
= MemoryAllocationHob
->AllocDescriptor
.MemoryBaseAddress
+
525 MemoryAllocationHob
->AllocDescriptor
.MemoryLength
- Bytes
;
527 // Make sure the granularity could be satisfied.
529 BaseAddress
&= ~((EFI_PHYSICAL_ADDRESS
)Granularity
- 1);
530 if (BaseAddress
>= MemoryAllocationHob
->AllocDescriptor
.MemoryBaseAddress
) {
535 MemoryAllocationHob
= NULL
;
541 Hob
.Raw
= GET_NEXT_HOB (Hob
);
542 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob
.Raw
);
545 if (MemoryAllocationHob
!= NULL
) {
546 UpdateOrSplitMemoryAllocationHob (MemoryAllocationHob
, BaseAddress
, Bytes
, MemoryType
);
547 *Memory
= BaseAddress
;
550 if (MergeFreeMemoryInMemoryAllocationHob ()) {
552 // Retry if there are free memory ranges merged.
554 return FindFreeMemoryFromMemoryAllocationHob (MemoryType
, Pages
, Granularity
, Memory
);
557 return EFI_NOT_FOUND
;
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.
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.
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.
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.
586 IN CONST EFI_PEI_SERVICES
**PeiServices
,
587 IN EFI_MEMORY_TYPE MemoryType
,
589 OUT EFI_PHYSICAL_ADDRESS
*Memory
593 PEI_CORE_INSTANCE
*PrivateData
;
594 EFI_PEI_HOB_POINTERS Hob
;
595 EFI_PHYSICAL_ADDRESS
*FreeMemoryTop
;
596 EFI_PHYSICAL_ADDRESS
*FreeMemoryBottom
;
597 UINTN RemainingPages
;
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
))
611 return EFI_INVALID_PARAMETER
;
614 Granularity
= DEFAULT_PAGE_ALLOCATION_GRANULARITY
;
616 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
617 Hob
.Raw
= PrivateData
->HobList
.Raw
;
619 if (Hob
.Raw
== NULL
) {
621 // HOB is not initialized yet.
623 return EFI_NOT_AVAILABLE_YET
;
626 if ((RUNTIME_PAGE_ALLOCATION_GRANULARITY
> DEFAULT_PAGE_ALLOCATION_GRANULARITY
) &&
627 ((MemoryType
== EfiACPIReclaimMemory
) ||
628 (MemoryType
== EfiACPIMemoryNVS
) ||
629 (MemoryType
== EfiRuntimeServicesCode
) ||
630 (MemoryType
== EfiRuntimeServicesData
)))
632 Granularity
= RUNTIME_PAGE_ALLOCATION_GRANULARITY
;
636 "AllocatePages: aligning allocation to %d KB\n",
637 Granularity
/ SIZE_1KB
641 if (!PrivateData
->PeiMemoryInstalled
&& PrivateData
->SwitchStackSignal
) {
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.
646 FreeMemoryTop
= &(PrivateData
->FreePhysicalMemoryTop
);
647 FreeMemoryBottom
= &(PrivateData
->PhysicalMemoryBegin
);
649 FreeMemoryTop
= &(Hob
.HandoffInformationTable
->EfiFreeMemoryTop
);
650 FreeMemoryBottom
= &(Hob
.HandoffInformationTable
->EfiFreeMemoryBottom
);
654 // Check to see if on correct boundary for the memory type.
655 // If not aligned, make the allocation aligned.
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
;
663 *(FreeMemoryTop
) -= Padding
;
664 if (Padding
>= EFI_PAGE_SIZE
) {
666 // Create a memory allocation HOB to cover
667 // the pages that we will lose to rounding
669 InternalBuildMemoryAllocationHob (
671 Padding
& ~(UINTN
)EFI_PAGE_MASK
,
672 EfiConventionalMemory
677 // Verify that there is sufficient memory to satisfy the allocation.
679 RemainingPages
= (UINTN
)(*FreeMemoryTop
- *FreeMemoryBottom
) >> EFI_PAGE_SHIFT
;
681 // The number of remaining pages needs to be greater than or equal to that of the request pages.
683 Pages
= ALIGN_VALUE (Pages
, EFI_SIZE_TO_PAGES (Granularity
));
684 if (RemainingPages
< Pages
) {
686 // Try to find free memory by searching memory allocation HOBs.
688 Status
= FindFreeMemoryFromMemoryAllocationHob (MemoryType
, Pages
, Granularity
, Memory
);
689 if (!EFI_ERROR (Status
)) {
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
;
698 // Update the PHIT to reflect the memory usage
700 *(FreeMemoryTop
) -= Pages
* EFI_PAGE_SIZE
;
703 // Update the value for the caller
705 *Memory
= *(FreeMemoryTop
);
708 // Create a memory allocation HOB.
710 InternalBuildMemoryAllocationHob (
712 Pages
* EFI_PAGE_SIZE
,
721 Mark the memory allocation HOB to be unused(freed) and update *FreeMemoryTop
722 if MemoryBaseAddress == *FreeMemoryTop.
724 @param[in] PrivateData Pointer to PeiCore's private data structure.
725 @param[in, out] MemoryAllocationHobToFree Pointer to memory allocation HOB to be freed.
729 FreeMemoryAllocationHob (
730 IN PEI_CORE_INSTANCE
*PrivateData
,
731 IN OUT EFI_HOB_MEMORY_ALLOCATION
*MemoryAllocationHobToFree
734 EFI_PEI_HOB_POINTERS Hob
;
735 EFI_PHYSICAL_ADDRESS
*FreeMemoryTop
;
736 EFI_HOB_MEMORY_ALLOCATION
*MemoryAllocationHob
;
738 Hob
.Raw
= PrivateData
->HobList
.Raw
;
740 if (!PrivateData
->PeiMemoryInstalled
&& PrivateData
->SwitchStackSignal
) {
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.
745 FreeMemoryTop
= &(PrivateData
->FreePhysicalMemoryTop
);
747 FreeMemoryTop
= &(Hob
.HandoffInformationTable
->EfiFreeMemoryTop
);
750 if (MemoryAllocationHobToFree
->AllocDescriptor
.MemoryBaseAddress
== *FreeMemoryTop
) {
752 // Update *FreeMemoryTop.
754 *FreeMemoryTop
+= MemoryAllocationHobToFree
->AllocDescriptor
.MemoryLength
;
756 // Mark the memory allocation HOB to be unused(freed).
758 MemoryAllocationHobToFree
->Header
.HobType
= EFI_HOB_TYPE_UNUSED
;
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
))
767 // Found memory allocation HOB that has EfiConventionalMemory MemoryType and
768 // MemoryBaseAddress == new *FreeMemoryTop.
770 MemoryAllocationHob
= (EFI_HOB_MEMORY_ALLOCATION
*)Hob
.Raw
;
774 Hob
.Raw
= GET_NEXT_HOB (Hob
);
775 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob
.Raw
);
779 // Free memory allocation HOB iteratively.
781 if (MemoryAllocationHob
!= NULL
) {
782 FreeMemoryAllocationHob (PrivateData
, MemoryAllocationHob
);
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.
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
803 IN CONST EFI_PEI_SERVICES
**PeiServices
,
804 IN EFI_PHYSICAL_ADDRESS Memory
,
808 PEI_CORE_INSTANCE
*PrivateData
;
812 EFI_PEI_HOB_POINTERS Hob
;
813 EFI_HOB_MEMORY_ALLOCATION
*MemoryAllocationHob
;
815 Bytes
= LShiftU64 (Pages
, EFI_PAGE_SHIFT
);
817 End
= Start
+ Bytes
- 1;
819 if ((Pages
== 0) || ((Start
& EFI_PAGE_MASK
) != 0) || (Start
>= End
)) {
820 return EFI_INVALID_PARAMETER
;
823 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
824 Hob
.Raw
= PrivateData
->HobList
.Raw
;
826 if (Hob
.Raw
== NULL
) {
828 // HOB is not initialized yet.
830 return EFI_NOT_AVAILABLE_YET
;
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
)))
841 // Found the memory allocation HOB that includes the memory pages to be freed.
843 MemoryAllocationHob
= (EFI_HOB_MEMORY_ALLOCATION
*)Hob
.Raw
;
847 Hob
.Raw
= GET_NEXT_HOB (Hob
);
848 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob
.Raw
);
851 if (MemoryAllocationHob
!= NULL
) {
852 UpdateOrSplitMemoryAllocationHob (MemoryAllocationHob
, Memory
, Bytes
, EfiConventionalMemory
);
853 FreeMemoryAllocationHob (PrivateData
, MemoryAllocationHob
);
856 return EFI_NOT_FOUND
;
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
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
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.
879 IN CONST EFI_PEI_SERVICES
**PeiServices
,
885 EFI_HOB_MEMORY_POOL
*Hob
;
888 // If some "post-memory" PEIM wishes to allocate larger pool,
889 // it should use AllocatePages service instead.
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)
896 if (Size
> (0xFFF8 - sizeof (EFI_HOB_MEMORY_POOL
))) {
897 return EFI_OUT_OF_RESOURCES
;
900 Status
= PeiServicesCreateHob (
901 EFI_HOB_TYPE_MEMORY_POOL
,
902 (UINT16
)(sizeof (EFI_HOB_MEMORY_POOL
) + Size
),
905 ASSERT_EFI_ERROR (Status
);
907 if (EFI_ERROR (Status
)) {