17#include "VirtualMemory.h"
20#define STACK_SIZE 0x20000
21#define IDT_ENTRY_COUNT 32
41 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
44 { 0xffff, 0, 0, 0x2, 1, 0, 1, 0xf, 0, 0, 1, 1, 0 }
47 { 0xffff, 0, 0, 0xf, 1, 0, 1, 0xf, 0, 0, 1, 1, 0 }
50 { 0xffff, 0, 0, 0x3, 1, 0, 1, 0xf, 0, 0, 1, 1, 0 }
53 { 0xffff, 0, 0, 0xa, 1, 0, 1, 0xf, 0, 0, 1, 1, 0 }
56 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
59 { 0xffff, 0, 0, 0x2, 1, 0, 1, 0xf, 0, 0, 1, 1, 0 }
62 { 0xffff, 0, 0, 0xa, 1, 0, 1, 0xf, 0, 1, 0, 1, 0 }
65 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
73 sizeof (gGdtEntries) - 1,
98 UINT8 PhysicalAddressBits;
100 UINTN IndexOfPdpEntries;
101 UINTN IndexOfPageDirectoryEntries;
102 UINT32 NumberOfPdpEntriesNeeded;
108 UINT64 AddressEncMask;
113 AddressEncMask =
PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;
115 PhysicalAddressBits = 32;
120 NumberOfPdpEntriesNeeded = (UINT32)
LShiftU64 (1, (PhysicalAddressBits - 30));
122 TotalPagesNum = NumberOfPdpEntriesNeeded + 1;
124 ASSERT (PageAddress != 0);
126 PageMap = (VOID *)PageAddress;
127 PageAddress += SIZE_4KB;
129 PageDirectoryPointerEntry = PageMap;
132 for (IndexOfPdpEntries = 0; IndexOfPdpEntries < NumberOfPdpEntriesNeeded; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {
137 PageDirectoryEntry = (VOID *)PageAddress;
138 PageAddress += SIZE_4KB;
143 PageDirectoryPointerEntry->Uint64 = (UINT64)(
UINTN)PageDirectoryEntry | AddressEncMask;
144 PageDirectoryPointerEntry->Bits.Present = 1;
146 for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PhysicalAddress += SIZE_2MB) {
148 || ( (PhysicalAddress < StackBase + StackSize)
149 && ((PhysicalAddress + SIZE_2MB) > StackBase)))
154 Split2MPageTo4K (PhysicalAddress, (UINT64 *)PageDirectoryEntry, StackBase, StackSize, 0, 0);
159 PageDirectoryEntry->Uint64 = (UINT64)PhysicalAddress | AddressEncMask;
160 PageDirectoryEntry->Bits.ReadWrite = 1;
161 PageDirectoryEntry->Bits.Present = 1;
162 PageDirectoryEntry->Bits.MustBe1 = 1;
167 for ( ; IndexOfPdpEntries < 512; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {
169 PageDirectoryPointerEntry,
180 return (
UINTN)PageMap;
197 BOOLEAN Ia32PaeSupport;
199 Ia32PaeSupport =
FALSE;
203 if ((RegEdx & BIT6) != 0) {
204 Ia32PaeSupport =
TRUE;
208 return Ia32PaeSupport;
231 if (
PcdGet8 (PcdHeapGuardPropertyMask) != 0) {
266 UINTN SizeOfTemplate;
278 IoWrite8 (LEGACY_8259_MASK_REGISTER_MASTER, 0xFF);
279 IoWrite8 (LEGACY_8259_MASK_REGISTER_SLAVE, 0xFF);
290 ASSERT (BaseOfStack != 0);
334 ASSERT (VectorAddress != 0);
341 IdtTableForX64->PeiService =
NULL;
344 IdtTable = IdtTableForX64->IdtTable;
345 for (Index = 0; Index < IDT_ENTRY_COUNT; Index++) {
346 IdtTable[Index].Ia32IdtEntry.Bits.GateType = 0x8e;
347 IdtTable[Index].Ia32IdtEntry.Bits.Reserved_0 = 0;
348 IdtTable[Index].Ia32IdtEntry.Bits.Selector = SYS_CODE64_SEL;
350 IdtTable[Index].Ia32IdtEntry.Bits.OffsetLow = (UINT16)VectorAddress;
351 IdtTable[Index].Ia32IdtEntry.Bits.OffsetHigh = (UINT16)(
RShiftU64 (VectorAddress, 16));
352 IdtTable[Index].Offset32To63 = (UINT32)(
RShiftU64 (VectorAddress, 32));
353 IdtTable[Index].Reserved = 0;
355 CopyMem ((VOID *)(
UINTN)VectorAddress, TemplateBase, SizeOfTemplate);
358 VectorAddress += SizeOfTemplate;
361 gLidtDescriptor.Base = (
UINTN)IdtTable;
367 "%a() Stack Base: 0x%lx, Stack Size: 0x%x\n",
387 DEBUG ((DEBUG_ERROR,
"NOT support 32bit UEFI payload\n"));
VOID EFIAPI BuildMemoryAllocationHob(IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN EFI_MEMORY_TYPE MemoryType)
VOID EFIAPI CpuDeadLoop(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 FitUplEntryPoint(IN UINTN BootloaderParameter)
BOOLEAN IsIa32PaeSupport(VOID)
VOID HandOffToDxeCore(IN EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint, IN EFI_PEI_HOB_POINTERS HobList)
UINTN Create4GPageTablesIa32Pae(IN EFI_PHYSICAL_ADDRESS StackBase, IN UINTN StackSize)
BOOLEAN ToBuildPageTable(VOID)
EFI_STATUS EFIAPI _ModuleEntryPoint(IN UINTN BootloaderParameter)
UINTN EFIAPI AsmWriteCr3(UINTN Cr3)
UINTN EFIAPI AsmReadCr0(VOID)
UINTN EFIAPI AsmWriteCr0(UINTN Cr0)
UINT8 EFIAPI IoWrite8(IN UINTN Port, IN UINT8 Value)
VOID Split2MPageTo4K(IN EFI_PHYSICAL_ADDRESS PhysicalAddress, IN OUT UINT64 *PageEntry2M, IN EFI_PHYSICAL_ADDRESS StackBase, IN UINTN StackSize, IN EFI_PHYSICAL_ADDRESS GhcbBase, IN UINTN GhcbSize)
BOOLEAN IsEnableNonExecNeeded(VOID)
VOID ClearFirst4KPage(IN VOID *HobStart)
VOID EnablePageTableProtection(IN UINTN PageTableBase, IN BOOLEAN Level4Paging)
VOID * AllocatePageTableMemory(IN UINTN Pages)
UINTN CreateIdentityMappingPageTables(IN EFI_PHYSICAL_ADDRESS StackBase, IN UINTN StackSize, IN EFI_PHYSICAL_ADDRESS GhcbBase, IN UINTN GhcbSize)
BOOLEAN IsNullDetectionEnabled(VOID)
VOID EFIAPI AsmVectorFixup(VOID *VectorBase, UINT8 VectorNum)
UINTN EFIAPI AsmGetVectorTemplatInfo(OUT VOID **TemplateBase)
#define ALIGN_POINTER(Pointer, Alignment)
#define GLOBAL_REMOVE_IF_UNREFERENCED
#define DEBUG(Expression)
UINT32 EFIAPI AsmCpuid(IN UINT32 Index, OUT UINT32 *RegisterEax OPTIONAL, OUT UINT32 *RegisterEbx OPTIONAL, OUT UINT32 *RegisterEcx OPTIONAL, OUT UINT32 *RegisterEdx OPTIONAL)
#define PcdGet64(TokenName)
#define PcdGet8(TokenName)
#define PcdGetBool(TokenName)
#define FeaturePcdGet(TokenName)
VOID *EFIAPI AllocatePages(IN UINTN Pages)
VOID UpdateStackHob(IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length)
UINT64 EFI_PHYSICAL_ADDRESS
#define EFI_PAGES_TO_SIZE(Pages)
#define EFI_SIZE_TO_PAGES(Size)
VOID EFIAPI InitializeFloatingPointUnits(VOID)
VOID EFIAPI AsmEnablePaging64(IN UINT16 Cs, IN UINT64 EntryPoint, IN UINT64 Context1 OPTIONAL, IN UINT64 Context2 OPTIONAL, IN UINT64 NewStack)
VOID EFIAPI AsmWriteGdtr(IN CONST IA32_DESCRIPTOR *Gdtr)
VOID EFIAPI AsmWriteIdtr(IN CONST IA32_DESCRIPTOR *Idtr)