20 OUT IA32_IDT_GATE_DESCRIPTOR *IdtEntry,
24 IdtEntry->Bits.OffsetLow = (UINT16)(
UINTN)InterruptHandler;
25 IdtEntry->Bits.OffsetHigh = (UINT16)((
UINTN)InterruptHandler >> 16);
26 IdtEntry->Bits.OffsetUpper = (UINT32)((
UINTN)InterruptHandler >> 32);
27 IdtEntry->Bits.GateType = IA32_IDT_GATE_TYPE_INTERRUPT_32;
38 IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry
41 return IdtEntry->Bits.OffsetLow + (((
UINTN)IdtEntry->Bits.OffsetHigh) << 16) +
42 (((
UINTN)IdtEntry->Bits.OffsetUpper) << 32);
62 ReservedVectors = ExceptionHandlerData->ReservedVectors;
68 ReservedVectors[ExceptionType].OldSs = SystemContext.SystemContextX64->Ss;
69 ReservedVectors[ExceptionType].OldSp = SystemContext.SystemContextX64->Rsp;
70 ReservedVectors[ExceptionType].OldFlags = SystemContext.SystemContextX64->Rflags;
71 ReservedVectors[ExceptionType].OldCs = SystemContext.SystemContextX64->Cs;
72 ReservedVectors[ExceptionType].OldIp = SystemContext.SystemContextX64->Rip;
73 ReservedVectors[ExceptionType].ExceptionData = SystemContext.SystemContextX64->ExceptionData;
77 Eflags.UintN = SystemContext.SystemContextX64->Rflags;
79 SystemContext.SystemContextX64->Rflags = Eflags.UintN;
83 SystemContext.SystemContextX64->Rip = (
UINTN)ReservedVectors[ExceptionType].HookAfterStubHeaderCode;
102 ReservedVectors = ExceptionHandlerData->ReservedVectors;
103 SystemContext.SystemContextX64->Ss = ReservedVectors[ExceptionType].OldSs;
104 SystemContext.SystemContextX64->Rsp = ReservedVectors[ExceptionType].OldSp;
105 SystemContext.SystemContextX64->Rflags = ReservedVectors[ExceptionType].OldFlags;
106 SystemContext.SystemContextX64->Cs = ReservedVectors[ExceptionType].OldCs;
107 SystemContext.SystemContextX64->Rip = ReservedVectors[ExceptionType].OldIp;
108 SystemContext.SystemContextX64->ExceptionData = ReservedVectors[ExceptionType].ExceptionData;
130 IA32_DESCRIPTOR Gdtr;
131 IA32_DESCRIPTOR Idtr;
132 IA32_IDT_GATE_DESCRIPTOR *IdtTable;
133 IA32_TSS_DESCRIPTOR *TssDesc;
134 IA32_TASK_STATE_SEGMENT *Tss;
140 UINT8 *StackSwitchExceptions;
141 UINTN NeedBufferSize;
143 if (BufferSize ==
NULL) {
144 return EFI_INVALID_PARAMETER;
150 if (CPU_STACK_SWITCH_EXCEPTION_NUMBER >
ARRAY_SIZE (Tss->IST)) {
151 return EFI_UNSUPPORTED;
180 NeedBufferSize = CPU_STACK_SWITCH_EXCEPTION_NUMBER * CPU_KNOWN_GOOD_STACK_SIZE +
181 sizeof (IA32_TSS_DESCRIPTOR) +
182 Gdtr.Limit + 1 + CPU_TSS_DESC_SIZE +
185 if (*BufferSize < NeedBufferSize) {
186 *BufferSize = NeedBufferSize;
187 return EFI_BUFFER_TOO_SMALL;
190 if (Buffer ==
NULL) {
191 return EFI_INVALID_PARAMETER;
195 StackSwitchExceptions = CPU_STACK_SWITCH_EXCEPTION_LIST;
196 StackTop = (
UINTN)Buffer + CPU_STACK_SWITCH_EXCEPTION_NUMBER * CPU_KNOWN_GOOD_STACK_SIZE;
197 NewGdtTable =
ALIGN_POINTER (StackTop,
sizeof (IA32_TSS_DESCRIPTOR));
198 TssDesc = (IA32_TSS_DESCRIPTOR *)((
UINTN)NewGdtTable + Gdtr.Limit + 1);
199 Tss = (IA32_TASK_STATE_SEGMENT *)((
UINTN)TssDesc + CPU_TSS_DESC_SIZE);
201 CopyMem (NewGdtTable, (VOID *)Gdtr.Base, Gdtr.Limit + 1);
202 Gdtr.Base = (
UINTN)NewGdtTable;
203 Gdtr.Limit = (UINT16)(Gdtr.Limit + CPU_TSS_DESC_SIZE);
209 TssBase = (
UINTN)Tss;
211 TssDesc->Uint128.Uint64 = 0;
212 TssDesc->Uint128.Uint64_1 = 0;
213 TssDesc->Bits.LimitLow =
sizeof (IA32_TASK_STATE_SEGMENT) - 1;
214 TssDesc->Bits.BaseLow = (UINT16)TssBase;
215 TssDesc->Bits.BaseMidl = (UINT8)(TssBase >> 16);
216 TssDesc->Bits.Type = IA32_GDT_TYPE_TSS;
218 TssDesc->Bits.LimitHigh = 0;
219 TssDesc->Bits.BaseMidh = (UINT8)(TssBase >> 24);
220 TssDesc->Bits.BaseHigh = (UINT32)(TssBase >> 32);
231 IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)Idtr.Base;
232 for (Index = 0; Index < CPU_STACK_SWITCH_EXCEPTION_NUMBER; ++Index) {
236 Tss->IST[Index] = StackTop;
237 StackTop -= CPU_KNOWN_GOOD_STACK_SIZE;
242 Vector = StackSwitchExceptions[Index];
243 if ((Vector >= CPU_EXCEPTION_NUM) ||
244 (Vector >= (Idtr.Limit + 1) /
sizeof (IA32_IDT_GATE_DESCRIPTOR)))
249 IdtTable[Vector].Bits.Reserved_0 = (UINT8)(Index + 1);
279 "!!!! X64 Exception Type - %02x(%a) CPU Apic ID - %08x !!!!\n",
284 if ((mErrorCodeFlag & (1 << ExceptionType)) != 0) {
286 "ExceptionData - %016lx",
287 SystemContext.SystemContextX64->ExceptionData
289 if (ExceptionType == EXCEPT_IA32_PAGE_FAULT) {
291 " I:%x R:%x U:%x W:%x P:%x PK:%x SS:%x SGX:%x",
292 (SystemContext.SystemContextX64->ExceptionData & IA32_PF_EC_ID) != 0,
293 (SystemContext.SystemContextX64->ExceptionData & IA32_PF_EC_RSVD) != 0,
294 (SystemContext.SystemContextX64->ExceptionData & IA32_PF_EC_US) != 0,
295 (SystemContext.SystemContextX64->ExceptionData & IA32_PF_EC_WR) != 0,
296 (SystemContext.SystemContextX64->ExceptionData & IA32_PF_EC_P) != 0,
297 (SystemContext.SystemContextX64->ExceptionData & IA32_PF_EC_PK) != 0,
298 (SystemContext.SystemContextX64->ExceptionData & IA32_PF_EC_SS) != 0,
299 (SystemContext.SystemContextX64->ExceptionData & IA32_PF_EC_SGX) != 0
307 "RIP - %016lx, CS - %016lx, RFLAGS - %016lx\n",
308 SystemContext.SystemContextX64->Rip,
309 SystemContext.SystemContextX64->Cs,
310 SystemContext.SystemContextX64->Rflags
313 "RAX - %016lx, RCX - %016lx, RDX - %016lx\n",
314 SystemContext.SystemContextX64->Rax,
315 SystemContext.SystemContextX64->Rcx,
316 SystemContext.SystemContextX64->Rdx
319 "RBX - %016lx, RSP - %016lx, RBP - %016lx\n",
320 SystemContext.SystemContextX64->Rbx,
321 SystemContext.SystemContextX64->Rsp,
322 SystemContext.SystemContextX64->Rbp
325 "RSI - %016lx, RDI - %016lx\n",
326 SystemContext.SystemContextX64->Rsi,
327 SystemContext.SystemContextX64->Rdi
330 "R8 - %016lx, R9 - %016lx, R10 - %016lx\n",
331 SystemContext.SystemContextX64->R8,
332 SystemContext.SystemContextX64->R9,
333 SystemContext.SystemContextX64->R10
336 "R11 - %016lx, R12 - %016lx, R13 - %016lx\n",
337 SystemContext.SystemContextX64->R11,
338 SystemContext.SystemContextX64->R12,
339 SystemContext.SystemContextX64->R13
342 "R14 - %016lx, R15 - %016lx\n",
343 SystemContext.SystemContextX64->R14,
344 SystemContext.SystemContextX64->R15
347 "DS - %016lx, ES - %016lx, FS - %016lx\n",
348 SystemContext.SystemContextX64->Ds,
349 SystemContext.SystemContextX64->Es,
350 SystemContext.SystemContextX64->Fs
353 "GS - %016lx, SS - %016lx\n",
354 SystemContext.SystemContextX64->Gs,
355 SystemContext.SystemContextX64->Ss
358 "CR0 - %016lx, CR2 - %016lx, CR3 - %016lx\n",
359 SystemContext.SystemContextX64->Cr0,
360 SystemContext.SystemContextX64->Cr2,
361 SystemContext.SystemContextX64->Cr3
364 "CR4 - %016lx, CR8 - %016lx\n",
365 SystemContext.SystemContextX64->Cr4,
366 SystemContext.SystemContextX64->Cr8
369 "DR0 - %016lx, DR1 - %016lx, DR2 - %016lx\n",
370 SystemContext.SystemContextX64->Dr0,
371 SystemContext.SystemContextX64->Dr1,
372 SystemContext.SystemContextX64->Dr2
375 "DR3 - %016lx, DR6 - %016lx, DR7 - %016lx\n",
376 SystemContext.SystemContextX64->Dr3,
377 SystemContext.SystemContextX64->Dr6,
378 SystemContext.SystemContextX64->Dr7
381 "GDTR - %016lx %016lx, LDTR - %016lx\n",
382 SystemContext.SystemContextX64->Gdtr[0],
383 SystemContext.SystemContextX64->Gdtr[1],
384 SystemContext.SystemContextX64->Ldtr
387 "IDTR - %016lx %016lx, TR - %016lx\n",
388 SystemContext.SystemContextX64->Idtr[0],
389 SystemContext.SystemContextX64->Idtr[1],
390 SystemContext.SystemContextX64->Tr
393 "FXSAVE_STATE - %016lx\n",
394 &SystemContext.SystemContextX64->FxSaveState
414 if ((ExceptionType == EXCEPT_IA32_PAGE_FAULT) &&
415 ((SystemContext.SystemContextX64->ExceptionData & IA32_PF_EC_ID) != 0))
#define CPU_STACK_ALIGNMENT
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID DumpModuleImageInfo(IN UINTN CurrentEip)
STATIC CONST CHAR8 * GetExceptionNameStr(IN EFI_EXCEPTION_TYPE ExceptionType)
STATIC VOID EFIAPI InternalPrintMessage(IN CONST CHAR8 *Format,...)
VOID ArchSaveExceptionContext(IN UINTN ExceptionType, IN EFI_SYSTEM_CONTEXT SystemContext, IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData)
VOID EFIAPI DumpCpuContext(IN EFI_EXCEPTION_TYPE ExceptionType, IN EFI_SYSTEM_CONTEXT SystemContext)
VOID ArchRestoreExceptionContext(IN UINTN ExceptionType, IN EFI_SYSTEM_CONTEXT SystemContext, IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData)
VOID DumpImageAndCpuContent(IN EFI_EXCEPTION_TYPE ExceptionType, IN EFI_SYSTEM_CONTEXT SystemContext)
UINTN ArchGetIdtHandler(IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry)
EFI_STATUS ArchSetupExceptionStack(IN VOID *Buffer, IN OUT UINTN *BufferSize)
VOID ArchUpdateIdtEntry(OUT IA32_IDT_GATE_DESCRIPTOR *IdtEntry, IN UINTN InterruptHandler)
UINT32 EFIAPI GetApicId(VOID)
#define ALIGN_POINTER(Pointer, Alignment)
#define ARRAY_SIZE(Array)
VOID EFIAPI AsmReadGdtr(OUT IA32_DESCRIPTOR *Gdtr)
VOID EFIAPI AsmReadIdtr(OUT IA32_DESCRIPTOR *Idtr)
VOID EFIAPI AsmWriteTr(IN UINT16 Selector)
VOID EFIAPI AsmWriteGdtr(IN CONST IA32_DESCRIPTOR *Gdtr)