21 OUT IA32_IDT_GATE_DESCRIPTOR *IdtEntry,
25 IdtEntry->Bits.OffsetLow = (UINT16)(
UINTN)InterruptHandler;
26 IdtEntry->Bits.OffsetHigh = (UINT16)((
UINTN)InterruptHandler >> 16);
27 IdtEntry->Bits.GateType = IA32_IDT_GATE_TYPE_INTERRUPT_32;
38 IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry
41 return (
UINTN)IdtEntry->Bits.OffsetLow + (((
UINTN)IdtEntry->Bits.OffsetHigh) << 16);
61 ReservedVectors = ExceptionHandlerData->ReservedVectors;
67 ReservedVectors[ExceptionType].OldFlags = SystemContext.SystemContextIa32->Eflags;
68 ReservedVectors[ExceptionType].OldCs = SystemContext.SystemContextIa32->Cs;
69 ReservedVectors[ExceptionType].OldIp = SystemContext.SystemContextIa32->Eip;
70 ReservedVectors[ExceptionType].ExceptionData = SystemContext.SystemContextIa32->ExceptionData;
74 Eflags.UintN = SystemContext.SystemContextIa32->Eflags;
76 SystemContext.SystemContextIa32->Eflags = Eflags.UintN;
80 SystemContext.SystemContextIa32->Eip = (
UINTN)ReservedVectors[ExceptionType].HookAfterStubHeaderCode;
99 ReservedVectors = ExceptionHandlerData->ReservedVectors;
100 SystemContext.SystemContextIa32->Eflags = ReservedVectors[ExceptionType].OldFlags;
101 SystemContext.SystemContextIa32->Cs = ReservedVectors[ExceptionType].OldCs;
102 SystemContext.SystemContextIa32->Eip = ReservedVectors[ExceptionType].OldIp;
103 SystemContext.SystemContextIa32->ExceptionData = ReservedVectors[ExceptionType].ExceptionData;
124 IA32_DESCRIPTOR Gdtr;
125 IA32_DESCRIPTOR Idtr;
126 IA32_IDT_GATE_DESCRIPTOR *IdtTable;
127 IA32_TSS_DESCRIPTOR *TssDesc;
128 IA32_TSS_DESCRIPTOR *TssDescBase;
129 IA32_TASK_STATE_SEGMENT *Tss;
135 UINT8 *StackSwitchExceptions;
136 UINTN NeedBufferSize;
139 if (BufferSize ==
NULL) {
140 return EFI_INVALID_PARAMETER;
173 NeedBufferSize = CPU_STACK_SWITCH_EXCEPTION_NUMBER * CPU_KNOWN_GOOD_STACK_SIZE +
174 sizeof (IA32_TSS_DESCRIPTOR) +
175 Gdtr.Limit + 1 + CPU_TSS_DESC_SIZE +
178 if (*BufferSize < NeedBufferSize) {
179 *BufferSize = NeedBufferSize;
180 return EFI_BUFFER_TOO_SMALL;
183 if (Buffer ==
NULL) {
184 return EFI_INVALID_PARAMETER;
188 StackSwitchExceptions = CPU_STACK_SWITCH_EXCEPTION_LIST;
189 StackTop = (
UINTN)Buffer + CPU_STACK_SWITCH_EXCEPTION_NUMBER * CPU_KNOWN_GOOD_STACK_SIZE;
190 NewGdtTable =
ALIGN_POINTER (StackTop,
sizeof (IA32_TSS_DESCRIPTOR));
191 TssDesc = (IA32_TSS_DESCRIPTOR *)((
UINTN)NewGdtTable + Gdtr.Limit + 1);
192 Tss = (IA32_TASK_STATE_SEGMENT *)((
UINTN)TssDesc + CPU_TSS_DESC_SIZE);
193 TssDescBase = TssDesc;
195 CopyMem (NewGdtTable, (VOID *)Gdtr.Base, Gdtr.Limit + 1);
196 Gdtr.Base = (
UINTN)NewGdtTable;
197 Gdtr.Limit = (UINT16)(Gdtr.Limit + CPU_TSS_DESC_SIZE);
203 TssBase = (
UINTN)Tss;
206 TssDesc->Bits.LimitLow =
sizeof (IA32_TASK_STATE_SEGMENT) - 1;
207 TssDesc->Bits.BaseLow = (UINT16)TssBase;
208 TssDesc->Bits.BaseMid = (UINT8)(TssBase >> 16);
209 TssDesc->Bits.Type = IA32_GDT_TYPE_TSS;
211 TssDesc->Bits.LimitHigh = 0;
212 TssDesc->Bits.BaseHigh = (UINT8)(TssBase >> 24);
223 IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)Idtr.Base;
224 for (Index = 0; Index < CPU_STACK_SWITCH_EXCEPTION_NUMBER; ++Index) {
231 TssBase = (
UINTN)Tss;
234 TssDesc->Bits.LimitLow =
sizeof (IA32_TASK_STATE_SEGMENT) - 1;
235 TssDesc->Bits.BaseLow = (UINT16)TssBase;
236 TssDesc->Bits.BaseMid = (UINT8)(TssBase >> 16);
237 TssDesc->Bits.Type = IA32_GDT_TYPE_TSS;
239 TssDesc->Bits.LimitHigh = 0;
240 TssDesc->Bits.BaseHigh = (UINT8)(TssBase >> 24);
245 Vector = StackSwitchExceptions[Index];
246 if ((Vector >= CPU_EXCEPTION_NUM) ||
247 (Vector >= (Idtr.Limit + 1) /
sizeof (IA32_IDT_GATE_DESCRIPTOR)))
253 Tss->EIP = (UINT32)(TemplateMap.ExceptionStart
254 + Vector * TemplateMap.ExceptionStubHeaderSize);
265 StackTop -= CPU_KNOWN_GOOD_STACK_SIZE;
270 IdtTable[Vector].Bits.OffsetLow = 0;
271 IdtTable[Vector].Bits.Selector = (UINT16)((
UINTN)TssDesc - Gdtr.Base);
272 IdtTable[Vector].Bits.Reserved_0 = 0;
273 IdtTable[Vector].Bits.GateType = IA32_IDT_GATE_TYPE_TASK;
274 IdtTable[Vector].Bits.OffsetHigh = 0;
304 "!!!! IA32 Exception Type - %02x(%a) CPU Apic ID - %08x !!!!\n",
309 if ((mErrorCodeFlag & (1 << ExceptionType)) != 0) {
311 "ExceptionData - %08x",
312 SystemContext.SystemContextIa32->ExceptionData
314 if (ExceptionType == EXCEPT_IA32_PAGE_FAULT) {
316 " I:%x R:%x U:%x W:%x P:%x PK:%x SS:%x SGX:%x",
317 (SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_ID) != 0,
318 (SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_RSVD) != 0,
319 (SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_US) != 0,
320 (SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_WR) != 0,
321 (SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_P) != 0,
322 (SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_PK) != 0,
323 (SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_SS) != 0,
324 (SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_SGX) != 0
332 "EIP - %08x, CS - %08x, EFLAGS - %08x\n",
333 SystemContext.SystemContextIa32->Eip,
334 SystemContext.SystemContextIa32->Cs,
335 SystemContext.SystemContextIa32->Eflags
338 "EAX - %08x, ECX - %08x, EDX - %08x, EBX - %08x\n",
339 SystemContext.SystemContextIa32->Eax,
340 SystemContext.SystemContextIa32->Ecx,
341 SystemContext.SystemContextIa32->Edx,
342 SystemContext.SystemContextIa32->Ebx
345 "ESP - %08x, EBP - %08x, ESI - %08x, EDI - %08x\n",
346 SystemContext.SystemContextIa32->Esp,
347 SystemContext.SystemContextIa32->Ebp,
348 SystemContext.SystemContextIa32->Esi,
349 SystemContext.SystemContextIa32->Edi
352 "DS - %08x, ES - %08x, FS - %08x, GS - %08x, SS - %08x\n",
353 SystemContext.SystemContextIa32->Ds,
354 SystemContext.SystemContextIa32->Es,
355 SystemContext.SystemContextIa32->Fs,
356 SystemContext.SystemContextIa32->Gs,
357 SystemContext.SystemContextIa32->Ss
360 "CR0 - %08x, CR2 - %08x, CR3 - %08x, CR4 - %08x\n",
361 SystemContext.SystemContextIa32->Cr0,
362 SystemContext.SystemContextIa32->Cr2,
363 SystemContext.SystemContextIa32->Cr3,
364 SystemContext.SystemContextIa32->Cr4
367 "DR0 - %08x, DR1 - %08x, DR2 - %08x, DR3 - %08x\n",
368 SystemContext.SystemContextIa32->Dr0,
369 SystemContext.SystemContextIa32->Dr1,
370 SystemContext.SystemContextIa32->Dr2,
371 SystemContext.SystemContextIa32->Dr3
374 "DR6 - %08x, DR7 - %08x\n",
375 SystemContext.SystemContextIa32->Dr6,
376 SystemContext.SystemContextIa32->Dr7
379 "GDTR - %08x %08x, IDTR - %08x %08x\n",
380 SystemContext.SystemContextIa32->Gdtr[0],
381 SystemContext.SystemContextIa32->Gdtr[1],
382 SystemContext.SystemContextIa32->Idtr[0],
383 SystemContext.SystemContextIa32->Idtr[1]
386 "LDTR - %08x, TR - %08x\n",
387 SystemContext.SystemContextIa32->Ldtr,
388 SystemContext.SystemContextIa32->Tr
391 "FXSAVE_STATE - %08x\n",
392 &SystemContext.SystemContextIa32->FxSaveState
412 if ((ExceptionType == EXCEPT_IA32_PAGE_FAULT) &&
413 ((SystemContext.SystemContextIa32->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)
VOID EFIAPI AsmGetTssTemplateMap(OUT EXCEPTION_HANDLER_TEMPLATE_MAP *AddressMap)
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)
UINT16 EFIAPI AsmReadFs(VOID)
UINT16 EFIAPI AsmReadEs(VOID)
UINTN EFIAPI AsmReadCr3(VOID)
UINT16 EFIAPI AsmReadGs(VOID)
UINT16 EFIAPI AsmReadSs(VOID)
UINT16 EFIAPI AsmReadCs(VOID)
UINT16 EFIAPI AsmReadDs(VOID)
UINT32 EFIAPI GetApicId(VOID)
#define ALIGN_POINTER(Pointer, Alignment)
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)