TianoCore EDK2 master
Loading...
Searching...
No Matches
SetIdtEntry.c
Go to the documentation of this file.
1
13#include "ScriptExecute.h"
14
15//
16// 8 extra pages for PF handler.
17//
18#define EXTRA_PAGE_TABLE_PAGES 8
19
20#define IA32_PG_P BIT0
21#define IA32_PG_RW BIT1
22#define IA32_PG_PS BIT7
23
24UINT64 mPhyMask;
25VOID *mOriginalHandler;
26UINTN mPageFaultBuffer;
27UINTN mPageFaultIndex = 0;
28//
29// Store the uplink information for each page being used.
30//
31UINT64 *mPageFaultUplink[EXTRA_PAGE_TABLE_PAGES];
32
37VOID
38EFIAPI
40 VOID
41 );
42
49VOID
51 IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry
52 )
53{
54 UINT32 RegEax;
55 UINT8 PhysicalAddressBits;
56 UINTN PageFaultHandlerHookAddress;
57
58 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
59 if (RegEax >= 0x80000008) {
60 AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
61 PhysicalAddressBits = (UINT8)RegEax;
62 } else {
63 PhysicalAddressBits = 36;
64 }
65
66 mPhyMask = LShiftU64 (1, PhysicalAddressBits) - 1;
67 mPhyMask &= (1ull << 48) - SIZE_4KB;
68
69 //
70 // Set Page Fault entry to catch >4G access
71 //
72 PageFaultHandlerHookAddress = (UINTN)PageFaultHandlerHook;
73 mOriginalHandler = (VOID *)(UINTN)(LShiftU64 (IdtEntry->Bits.OffsetUpper, 32) + IdtEntry->Bits.OffsetLow + (IdtEntry->Bits.OffsetHigh << 16));
74 IdtEntry->Bits.OffsetLow = (UINT16)PageFaultHandlerHookAddress;
75 IdtEntry->Bits.Selector = (UINT16)AsmReadCs ();
76 IdtEntry->Bits.Reserved_0 = 0;
77 IdtEntry->Bits.GateType = IA32_IDT_GATE_TYPE_INTERRUPT_32;
78 IdtEntry->Bits.OffsetHigh = (UINT16)(PageFaultHandlerHookAddress >> 16);
79 IdtEntry->Bits.OffsetUpper = (UINT32)(PageFaultHandlerHookAddress >> 32);
80 IdtEntry->Bits.Reserved_1 = 0;
81
82 if (mPage1GSupport) {
83 mPageFaultBuffer = (UINTN)(AsmReadCr3 () & mPhyMask) + EFI_PAGES_TO_SIZE (2);
84 } else {
85 mPageFaultBuffer = (UINTN)(AsmReadCr3 () & mPhyMask) + EFI_PAGES_TO_SIZE (6);
86 }
87
88 ZeroMem (mPageFaultUplink, sizeof (mPageFaultUplink));
89}
90
99BOOLEAN
101 IN ACPI_S3_CONTEXT *AcpiS3Context
102 )
103{
105
106 Facs = (EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *)((UINTN)(AcpiS3Context->AcpiFacsTable));
107 if ((Facs == NULL) ||
109 ((Facs->FirmwareWakingVector == 0) && (Facs->XFirmwareWakingVector == 0)))
110 {
111 // Something wrong with FACS
112 return FALSE;
113 }
114
115 if (Facs->XFirmwareWakingVector != 0) {
117 ((Facs->Flags & EFI_ACPI_4_0_64BIT_WAKE_SUPPORTED_F) != 0) &&
118 ((Facs->OspmFlags & EFI_ACPI_4_0_OSPM_64BIT_WAKE__F) != 0))
119 {
120 // Both BIOS and OS wants 64bit vector
121 if (sizeof (UINTN) == sizeof (UINT64)) {
122 return TRUE;
123 }
124 }
125 }
126
127 return FALSE;
128}
129
136VOID
138 IN ACPI_S3_CONTEXT *AcpiS3Context
139 )
140{
141 IA32_IDT_GATE_DESCRIPTOR *IdtEntry;
142 IA32_DESCRIPTOR *IdtDescriptor;
143 UINTN S3DebugBuffer;
144 EFI_STATUS Status;
145
146 //
147 // Restore IDT for debug
148 //
149 IdtDescriptor = (IA32_DESCRIPTOR *)(UINTN)(AcpiS3Context->IdtrProfile);
150 AsmWriteIdtr (IdtDescriptor);
151
152 //
153 // Setup the default CPU exception handlers
154 //
155 Status = InitializeCpuExceptionHandlers (NULL);
156 ASSERT_EFI_ERROR (Status);
157
159 //
160 // Update IDT entry INT3 if the instruction is valid in it
161 //
162 S3DebugBuffer = (UINTN)(AcpiS3Context->S3DebugBufferAddress);
163 if (*(UINTN *)S3DebugBuffer != (UINTN)-1) {
164 IdtEntry = (IA32_IDT_GATE_DESCRIPTOR *)(IdtDescriptor->Base + (3 * sizeof (IA32_IDT_GATE_DESCRIPTOR)));
165 IdtEntry->Bits.OffsetLow = (UINT16)S3DebugBuffer;
166 IdtEntry->Bits.Selector = (UINT16)AsmReadCs ();
167 IdtEntry->Bits.Reserved_0 = 0;
168 IdtEntry->Bits.GateType = IA32_IDT_GATE_TYPE_INTERRUPT_32;
169 IdtEntry->Bits.OffsetHigh = (UINT16)(S3DebugBuffer >> 16);
170 IdtEntry->Bits.OffsetUpper = (UINT32)(S3DebugBuffer >> 32);
171 IdtEntry->Bits.Reserved_1 = 0;
172 }
173
175
176 //
177 // If both BIOS and OS wants long mode waking vector,
178 // S3ResumePei should have established 1:1 Virtual to Physical identity mapping page table,
179 // no need to hook page fault handler.
180 //
181 if (!IsLongModeWakingVector (AcpiS3Context)) {
182 IdtEntry = (IA32_IDT_GATE_DESCRIPTOR *)(IdtDescriptor->Base + (14 * sizeof (IA32_IDT_GATE_DESCRIPTOR)));
183 HookPageFaultHandler (IdtEntry);
184 }
185}
186
193VOID
195 IN OUT UINT64 *Uplink
196 )
197{
198 UINTN Address;
199
200 Address = mPageFaultBuffer + EFI_PAGES_TO_SIZE (mPageFaultIndex);
201 ZeroMem ((VOID *)Address, EFI_PAGES_TO_SIZE (1));
202
203 //
204 // Cut the previous uplink if it exists and wasn't overwritten.
205 //
206 if ((mPageFaultUplink[mPageFaultIndex] != NULL) &&
207 ((*mPageFaultUplink[mPageFaultIndex] & ~mAddressEncMask & mPhyMask) == Address))
208 {
209 *mPageFaultUplink[mPageFaultIndex] = 0;
210 }
211
212 //
213 // Link & Record the current uplink.
214 //
215 *Uplink = Address | mAddressEncMask | IA32_PG_P | IA32_PG_RW;
216 mPageFaultUplink[mPageFaultIndex] = Uplink;
217
218 mPageFaultIndex = (mPageFaultIndex + 1) % EXTRA_PAGE_TABLE_PAGES;
219}
220
228BOOLEAN
229EFIAPI
231 VOID
232 )
233{
234 UINT64 *PageTable;
235 UINT64 PFAddress;
236 UINTN PTIndex;
237
238 PFAddress = AsmReadCr2 ();
239 DEBUG ((DEBUG_INFO, "BootScript - PageFaultHandler: Cr2 - %lx\n", PFAddress));
240
241 if (PFAddress >= mPhyMask + SIZE_4KB) {
242 return FALSE;
243 }
244
245 PFAddress &= mPhyMask;
246
247 PageTable = (UINT64 *)(UINTN)(AsmReadCr3 () & mPhyMask);
248
249 PTIndex = BitFieldRead64 (PFAddress, 39, 47);
250 // PML4E
251 if ((PageTable[PTIndex] & IA32_PG_P) == 0) {
252 AcquirePage (&PageTable[PTIndex]);
253 }
254
255 PageTable = (UINT64 *)(UINTN)(PageTable[PTIndex] & ~mAddressEncMask & mPhyMask);
256 PTIndex = BitFieldRead64 (PFAddress, 30, 38);
257 // PDPTE
258 if (mPage1GSupport) {
259 PageTable[PTIndex] = ((PFAddress | mAddressEncMask) & ~((1ull << 30) - 1)) | IA32_PG_P | IA32_PG_RW | IA32_PG_PS;
260 } else {
261 if ((PageTable[PTIndex] & IA32_PG_P) == 0) {
262 AcquirePage (&PageTable[PTIndex]);
263 }
264
265 PageTable = (UINT64 *)(UINTN)(PageTable[PTIndex] & ~mAddressEncMask & mPhyMask);
266 PTIndex = BitFieldRead64 (PFAddress, 21, 29);
267 // PD
268 PageTable[PTIndex] = ((PFAddress | mAddressEncMask) & ~((1ull << 21) - 1)) | IA32_PG_P | IA32_PG_RW | IA32_PG_PS;
269 }
270
271 return TRUE;
272}
UINT64 UINTN
#define EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE
Definition: Acpi40.h:1180
#define EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_VERSION
Definition: Acpi40.h:238
#define EFI_ACPI_4_0_OSPM_64BIT_WAKE__F
Definition: Acpi40.h:251
UINT64 EFIAPI BitFieldRead64(IN UINT64 Operand, IN UINTN StartBit, IN UINTN EndBit)
Definition: BitField.c:719
UINT64 EFIAPI LShiftU64(IN UINT64 Operand, IN UINTN Count)
Definition: LShiftU64.c:28
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID SetIdtEntry(IN ACPI_S3_CONTEXT *AcpiS3Context)
Definition: SetIdtEntry.c:20
UINTN EFIAPI AsmReadCr3(VOID)
UINTN EFIAPI AsmReadCr2(VOID)
UINT16 EFIAPI AsmReadCs(VOID)
#define NULL
Definition: Base.h:319
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
#define DEBUG_CODE_BEGIN()
Definition: DebugLib.h:564
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define DEBUG_CODE_END()
Definition: DebugLib.h:578
UINT32 EFIAPI AsmCpuid(IN UINT32 Index, OUT UINT32 *RegisterEax OPTIONAL, OUT UINT32 *RegisterEbx OPTIONAL, OUT UINT32 *RegisterEcx OPTIONAL, OUT UINT32 *RegisterEdx OPTIONAL)
Definition: CpuId.c:36
#define EFI_PAGES_TO_SIZE(Pages)
Definition: UefiBaseType.h:213
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID EFIAPI PageFaultHandlerHook(VOID)
VOID AcquirePage(IN OUT UINT64 *Uplink)
Definition: SetIdtEntry.c:194
BOOLEAN EFIAPI PageFaultHandler(VOID)
Definition: SetIdtEntry.c:230
VOID HookPageFaultHandler(IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry)
Definition: SetIdtEntry.c:50
BOOLEAN IsLongModeWakingVector(IN ACPI_S3_CONTEXT *AcpiS3Context)
Definition: SetIdtEntry.c:100
VOID EFIAPI AsmWriteIdtr(IN CONST IA32_DESCRIPTOR *Idtr)