30 PrivateData->SwitchStackSignal =
FALSE;
36 if (OldCoreData ==
NULL) {
37 PrivateData->PeiMemoryInstalled =
FALSE;
38 PrivateData->HobList.Raw = SecCoreData->PeiTemporaryRamBase;
41 BOOT_WITH_FULL_CONFIGURATION,
43 (
UINTN)SecCoreData->PeiTemporaryRamSize
49 PrivateData->Ps = &(PrivateData->ServiceTableShadow);
76 IN UINT64 MemoryLength
81 DEBUG ((DEBUG_INFO,
"PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin, MemoryLength));
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;
115 IN BOOLEAN TemporaryRamMigrated
121 Private->MemoryPages.Size = (
UINTN)(Private->HobList.HandoffInformationTable->EfiMemoryTop -
122 Private->HobList.HandoffInformationTable->EfiFreeMemoryTop);
123 if (Private->MemoryPages.Size == 0) {
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);
139 if (TemporaryRamMigrated) {
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;
190 DEBUG ((DEBUG_INFO,
"Converting FVs in FV HOB.\n"));
192 for (Hob.Raw =
GetHobList (); !END_OF_HOB_LIST (Hob); Hob.Raw = GET_NEXT_HOB (Hob)) {
193 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV) {
194 FirmwareVolumeHob = Hob.FirmwareVolume;
195 if (FirmwareVolumeHob->
BaseAddress == OrgFvHandle) {
198 }
else if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV2) {
199 FirmwareVolume2Hob = Hob.FirmwareVolume2;
200 if (FirmwareVolume2Hob->
BaseAddress == OrgFvHandle) {
203 }
else if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV3) {
204 FirmwareVolume3Hob = Hob.FirmwareVolume3;
205 if (FirmwareVolume3Hob->
BaseAddress == OrgFvHandle) {
227 UINTN OldMemPagesSize;
229 if (PrivateData->MemoryPages.Size == 0) {
236 OldMemPagesBase = PrivateData->MemoryPages.Base;
237 OldMemPagesSize = PrivateData->MemoryPages.Size;
239 MemoryAllocationHob =
NULL;
240 Hob.Raw =
GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);
241 while (Hob.Raw !=
NULL) {
247 if (PrivateData->MemoryPages.OffsetPositive) {
254 Hob.Raw = GET_NEXT_HOB (Hob);
255 Hob.Raw =
GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);
282 MemoryAllocationHob =
NULL;
284 while (Hob.Raw !=
NULL) {
290 Hob.Raw = GET_NEXT_HOB (Hob);
291 Hob.Raw =
GetNextHob (EFI_HOB_TYPE_UNUSED, Hob.Raw);
294 if (MemoryAllocationHob !=
NULL) {
298 MemoryAllocationHob->
Header.
HobType = EFI_HOB_TYPE_MEMORY_ALLOCATION;
341 if ((Memory + Bytes) <
342 (MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress + MemoryAllocationHob->AllocDescriptor.MemoryLength))
349 (MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress + MemoryAllocationHob->AllocDescriptor.MemoryLength) - (Memory + Bytes),
350 MemoryAllocationHob->AllocDescriptor.MemoryType
354 if (Memory > MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress) {
359 MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress,
360 Memory - MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress,
361 MemoryAllocationHob->AllocDescriptor.MemoryType
368 MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress = Memory;
369 MemoryAllocationHob->AllocDescriptor.MemoryLength = Bytes;
370 MemoryAllocationHob->AllocDescriptor.MemoryType = MemoryType;
395 Hob.Raw =
GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);
396 while (Hob.Raw !=
NULL) {
402 Hob2.Raw = GET_NEXT_HOB (Hob);
403 Hob2.Raw =
GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);
404 while (Hob2.Raw !=
NULL) {
433 Hob2.Raw = GET_NEXT_HOB (Hob2);
434 Hob2.Raw =
GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob2.Raw);
438 Hob.Raw = GET_NEXT_HOB (Hob);
439 Hob.Raw =
GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);
471 Bytes =
LShiftU64 (Pages, EFI_PAGE_SHIFT);
474 MemoryAllocationHob =
NULL;
475 Hob.Raw =
GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);
476 while (Hob.Raw !=
NULL) {
495 MemoryAllocationHob =
NULL;
501 Hob.Raw = GET_NEXT_HOB (Hob);
502 Hob.Raw =
GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);
505 if (MemoryAllocationHob !=
NULL) {
507 *Memory = BaseAddress;
517 return EFI_NOT_FOUND;
557 UINTN RemainingPages;
558 UINTN RemainingMemory;
572 return EFI_INVALID_PARAMETER;
578 Hob.Raw = PrivateData->HobList.Raw;
580 if (Hob.Raw ==
NULL) {
593 Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
597 "AllocatePages: aligning allocation to %d KB\n",
598 Granularity / SIZE_1KB
602 if (!PrivateData->PeiMemoryInstalled && PrivateData->SwitchStackSignal) {
607 FreeMemoryTop = &(PrivateData->FreePhysicalMemoryTop);
608 FreeMemoryBottom = &(PrivateData->PhysicalMemoryBegin);
618 Padding = *(FreeMemoryTop) & (Granularity - 1);
619 if ((
UINTN)(*FreeMemoryTop - *FreeMemoryBottom) < Padding) {
620 DEBUG ((DEBUG_ERROR,
"AllocatePages failed: Out of space after padding.\n"));
621 return EFI_OUT_OF_RESOURCES;
624 *(FreeMemoryTop) -= Padding;
625 if (Padding >= EFI_PAGE_SIZE) {
632 Padding & ~(
UINTN)EFI_PAGE_MASK,
640 RemainingMemory = (
UINTN)(*FreeMemoryTop - *FreeMemoryBottom);
641 RemainingPages = (
UINTN)(
RShiftU64 (RemainingMemory, EFI_PAGE_SHIFT));
648 if ((RemainingPages > Pages) ||
649 ((RemainingPages == Pages) &&
655 *(FreeMemoryTop) -= Pages * EFI_PAGE_SIZE;
660 *Memory = *(FreeMemoryTop);
667 Pages * EFI_PAGE_SIZE,
677 if (!EFI_ERROR (Status)) {
681 DEBUG ((DEBUG_ERROR,
"AllocatePages failed: No 0x%lx Pages is available.\n", (UINT64)Pages));
682 DEBUG ((DEBUG_ERROR,
"There is only left 0x%lx pages memory resource to be allocated.\n", (UINT64)RemainingPages));
683 return EFI_OUT_OF_RESOURCES;
705 Hob.Raw = PrivateData->HobList.Raw;
707 if (!PrivateData->PeiMemoryInstalled && PrivateData->SwitchStackSignal) {
712 FreeMemoryTop = &(PrivateData->FreePhysicalMemoryTop);
717 if (MemoryAllocationHobToFree->AllocDescriptor.MemoryBaseAddress == *FreeMemoryTop) {
721 *FreeMemoryTop += MemoryAllocationHobToFree->AllocDescriptor.MemoryLength;
725 MemoryAllocationHobToFree->Header.HobType = EFI_HOB_TYPE_UNUSED;
727 MemoryAllocationHob =
NULL;
728 Hob.Raw =
GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);
729 while (Hob.Raw !=
NULL) {
741 Hob.Raw = GET_NEXT_HOB (Hob);
742 Hob.Raw =
GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);
748 if (MemoryAllocationHob !=
NULL) {
782 Bytes =
LShiftU64 (Pages, EFI_PAGE_SHIFT);
784 End = Start + Bytes - 1;
786 if ((Pages == 0) || ((Start & EFI_PAGE_MASK) != 0) || (Start >= End)) {
787 return EFI_INVALID_PARAMETER;
791 Hob.Raw = PrivateData->HobList.Raw;
793 if (Hob.Raw ==
NULL) {
800 MemoryAllocationHob =
NULL;
801 Hob.Raw =
GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);
802 while (Hob.Raw !=
NULL) {
814 Hob.Raw = GET_NEXT_HOB (Hob);
815 Hob.Raw =
GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);
818 if (MemoryAllocationHob !=
NULL) {
823 return EFI_NOT_FOUND;
864 return EFI_OUT_OF_RESOURCES;
868 EFI_HOB_TYPE_MEMORY_POOL,
872 if (EFI_ERROR (Status)) {
#define DEFAULT_PAGE_ALLOCATION_GRANULARITY
VOID *EFIAPI GetFirstHob(IN UINT16 Type)
VOID EFIAPI BuildMemoryAllocationHob(IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN EFI_MEMORY_TYPE MemoryType)
VOID *EFIAPI GetNextHob(IN UINT16 Type, IN CONST VOID *HobStart)
VOID *EFIAPI GetHobList(VOID)
UINT64 EFIAPI RShiftU64(IN UINT64 Operand, IN UINTN Count)
UINT64 EFIAPI LShiftU64(IN UINT64 Operand, IN UINTN Count)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
EFI_STATUS EFIAPI PeiServicesCreateHob(IN UINT16 Type, IN UINT16 Length, OUT VOID **Hob)
EFI_STATUS PeiCoreBuildHobHandoffInfoTable(IN EFI_BOOT_MODE BootMode, IN EFI_PHYSICAL_ADDRESS MemoryBegin, IN UINT64 MemoryLength)
#define ALIGN_VALUE(Value, Alignment)
#define DEBUG(Expression)
EFI_STATUS EFIAPI PeiInstallPeiMemory(IN CONST EFI_PEI_SERVICES **PeiServices, IN EFI_PHYSICAL_ADDRESS MemoryBegin, IN UINT64 MemoryLength)
VOID ConvertMemoryAllocationHobs(IN PEI_CORE_INSTANCE *PrivateData)
BOOLEAN MergeFreeMemoryInMemoryAllocationHob(VOID)
VOID ConvertFvHob(IN PEI_CORE_INSTANCE *PrivateData, IN UINTN OrgFvHandle, IN UINTN FvHandle)
VOID MigrateMemoryPages(IN PEI_CORE_INSTANCE *Private, IN BOOLEAN TemporaryRamMigrated)
EFI_STATUS EFIAPI PeiFreePages(IN CONST EFI_PEI_SERVICES **PeiServices, IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN Pages)
VOID UpdateOrSplitMemoryAllocationHob(IN OUT EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHob, IN EFI_PHYSICAL_ADDRESS Memory, IN UINT64 Bytes, IN EFI_MEMORY_TYPE MemoryType)
EFI_STATUS EFIAPI PeiAllocatePages(IN CONST EFI_PEI_SERVICES **PeiServices, IN EFI_MEMORY_TYPE MemoryType, IN UINTN Pages, OUT EFI_PHYSICAL_ADDRESS *Memory)
EFI_STATUS FindFreeMemoryFromMemoryAllocationHob(IN EFI_MEMORY_TYPE MemoryType, IN UINTN Pages, IN UINTN Granularity, OUT EFI_PHYSICAL_ADDRESS *Memory)
VOID InternalBuildMemoryAllocationHob(IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN EFI_MEMORY_TYPE MemoryType)
VOID InitializeMemoryServices(IN PEI_CORE_INSTANCE *PrivateData, IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData, IN PEI_CORE_INSTANCE *OldCoreData)
EFI_STATUS EFIAPI PeiAllocatePool(IN CONST EFI_PEI_SERVICES **PeiServices, IN UINTN Size, OUT VOID **Buffer)
VOID FreeMemoryAllocationHob(IN PEI_CORE_INSTANCE *PrivateData, IN OUT EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHobToFree)
#define PEI_CORE_INSTANCE_FROM_PS_THIS(a)
#define EFI_NOT_AVAILABLE_YET
UINT64 EFI_PHYSICAL_ADDRESS
#define EFI_SIZE_TO_PAGES(Size)
EFI_PHYSICAL_ADDRESS BaseAddress
EFI_PHYSICAL_ADDRESS BaseAddress
EFI_PHYSICAL_ADDRESS BaseAddress
EFI_PHYSICAL_ADDRESS EfiFreeMemoryBottom
EFI_PHYSICAL_ADDRESS EfiFreeMemoryTop
EFI_PHYSICAL_ADDRESS MemoryBaseAddress
EFI_MEMORY_TYPE MemoryType
EFI_HOB_GENERIC_HEADER Header
EFI_HOB_MEMORY_ALLOCATION_HEADER AllocDescriptor