2 x64 CPU Exception Handler.
4 Copyright (c) 2012 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "CpuExceptionCommon.h"
18 Return address map of exception handler template so that C code can generate
21 @param IdtEntry Pointer to IDT entry to be updated.
22 @param InterruptHandler IDT handler value.
26 IN IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
,
27 IN UINTN InterruptHandler
30 IdtEntry
->Bits
.OffsetLow
= (UINT16
)(UINTN
)InterruptHandler
;
31 IdtEntry
->Bits
.OffsetHigh
= (UINT16
)((UINTN
)InterruptHandler
>> 16);
32 IdtEntry
->Bits
.OffsetUpper
= (UINT32
)((UINTN
)InterruptHandler
>> 32);
33 IdtEntry
->Bits
.GateType
= IA32_IDT_GATE_TYPE_INTERRUPT_32
;
37 Read IDT handler value from IDT entry.
39 @param IdtEntry Pointer to IDT entry to be read.
44 IN IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
47 return IdtEntry
->Bits
.OffsetLow
+ (((UINTN
) IdtEntry
->Bits
.OffsetHigh
) << 16) +
48 (((UINTN
) IdtEntry
->Bits
.OffsetUpper
) << 32);
52 Save CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
54 @param[in] ExceptionType Exception type.
55 @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.
56 @param[in] ExceptionHandlerData Pointer to exception handler data.
59 ArchSaveExceptionContext (
60 IN UINTN ExceptionType
,
61 IN EFI_SYSTEM_CONTEXT SystemContext
,
62 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
66 RESERVED_VECTORS_DATA
*ReservedVectors
;
68 ReservedVectors
= ExceptionHandlerData
->ReservedVectors
;
70 // Save Exception context in global variable
72 ReservedVectors
[ExceptionType
].OldSs
= SystemContext
.SystemContextX64
->Ss
;
73 ReservedVectors
[ExceptionType
].OldSp
= SystemContext
.SystemContextX64
->Rsp
;
74 ReservedVectors
[ExceptionType
].OldFlags
= SystemContext
.SystemContextX64
->Rflags
;
75 ReservedVectors
[ExceptionType
].OldCs
= SystemContext
.SystemContextX64
->Cs
;
76 ReservedVectors
[ExceptionType
].OldIp
= SystemContext
.SystemContextX64
->Rip
;
77 ReservedVectors
[ExceptionType
].ExceptionData
= SystemContext
.SystemContextX64
->ExceptionData
;
79 // Clear IF flag to avoid old IDT handler enable interrupt by IRET
81 Eflags
.UintN
= SystemContext
.SystemContextX64
->Rflags
;
83 SystemContext
.SystemContextX64
->Rflags
= Eflags
.UintN
;
85 // Modify the EIP in stack, then old IDT handler will return to the stub code
87 SystemContext
.SystemContextX64
->Rip
= (UINTN
) ReservedVectors
[ExceptionType
].HookAfterStubHeaderCode
;
91 Restore CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
93 @param[in] ExceptionType Exception type.
94 @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.
95 @param[in] ExceptionHandlerData Pointer to exception handler data.
98 ArchRestoreExceptionContext (
99 IN UINTN ExceptionType
,
100 IN EFI_SYSTEM_CONTEXT SystemContext
,
101 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
104 RESERVED_VECTORS_DATA
*ReservedVectors
;
106 ReservedVectors
= ExceptionHandlerData
->ReservedVectors
;
107 SystemContext
.SystemContextX64
->Ss
= ReservedVectors
[ExceptionType
].OldSs
;
108 SystemContext
.SystemContextX64
->Rsp
= ReservedVectors
[ExceptionType
].OldSp
;
109 SystemContext
.SystemContextX64
->Rflags
= ReservedVectors
[ExceptionType
].OldFlags
;
110 SystemContext
.SystemContextX64
->Cs
= ReservedVectors
[ExceptionType
].OldCs
;
111 SystemContext
.SystemContextX64
->Rip
= ReservedVectors
[ExceptionType
].OldIp
;
112 SystemContext
.SystemContextX64
->ExceptionData
= ReservedVectors
[ExceptionType
].ExceptionData
;
116 Setup separate stack for given exceptions.
118 @param[in] StackSwitchData Pointer to data required for setuping up
121 @retval EFI_SUCCESS The exceptions have been successfully
122 initialized with new stack.
123 @retval EFI_INVALID_PARAMETER StackSwitchData contains invalid content.
127 ArchSetupExcpetionStack (
128 IN CPU_EXCEPTION_INIT_DATA
*StackSwitchData
131 IA32_DESCRIPTOR Gdtr
;
132 IA32_DESCRIPTOR Idtr
;
133 IA32_IDT_GATE_DESCRIPTOR
*IdtTable
;
134 IA32_TSS_DESCRIPTOR
*TssDesc
;
135 IA32_TASK_STATE_SEGMENT
*Tss
;
142 if (StackSwitchData
== NULL
||
143 StackSwitchData
->Ia32
.Revision
!= CPU_EXCEPTION_INIT_DATA_REV
||
144 StackSwitchData
->X64
.KnownGoodStackTop
== 0 ||
145 StackSwitchData
->X64
.KnownGoodStackSize
== 0 ||
146 StackSwitchData
->X64
.StackSwitchExceptions
== NULL
||
147 StackSwitchData
->X64
.StackSwitchExceptionNumber
== 0 ||
148 StackSwitchData
->X64
.StackSwitchExceptionNumber
> CPU_EXCEPTION_NUM
||
149 StackSwitchData
->X64
.GdtTable
== NULL
||
150 StackSwitchData
->X64
.IdtTable
== NULL
||
151 StackSwitchData
->X64
.ExceptionTssDesc
== NULL
||
152 StackSwitchData
->X64
.ExceptionTss
== NULL
) {
153 return EFI_INVALID_PARAMETER
;
157 // The caller is responsible for that the GDT table, no matter the existing
158 // one or newly allocated, has enough space to hold descriptors for exception
159 // task-state segments.
161 if (((UINTN
)StackSwitchData
->X64
.GdtTable
& (IA32_GDT_ALIGNMENT
- 1)) != 0) {
162 return EFI_INVALID_PARAMETER
;
165 if ((UINTN
)StackSwitchData
->X64
.ExceptionTssDesc
< (UINTN
)(StackSwitchData
->X64
.GdtTable
)) {
166 return EFI_INVALID_PARAMETER
;
169 if (((UINTN
)StackSwitchData
->X64
.ExceptionTssDesc
+ StackSwitchData
->X64
.ExceptionTssDescSize
) >
170 ((UINTN
)(StackSwitchData
->X64
.GdtTable
) + StackSwitchData
->X64
.GdtTableSize
)) {
171 return EFI_INVALID_PARAMETER
;
175 // One task gate descriptor and one task-state segment are needed.
177 if (StackSwitchData
->X64
.ExceptionTssDescSize
< sizeof (IA32_TSS_DESCRIPTOR
)) {
178 return EFI_INVALID_PARAMETER
;
180 if (StackSwitchData
->X64
.ExceptionTssSize
< sizeof (IA32_TASK_STATE_SEGMENT
)) {
181 return EFI_INVALID_PARAMETER
;
185 // Interrupt stack table supports only 7 vectors.
187 TssDesc
= StackSwitchData
->X64
.ExceptionTssDesc
;
188 Tss
= StackSwitchData
->X64
.ExceptionTss
;
189 if (StackSwitchData
->X64
.StackSwitchExceptionNumber
> ARRAY_SIZE (Tss
->Ist
)) {
190 return EFI_INVALID_PARAMETER
;
194 // Initialize new GDT table and/or IDT table, if any
199 GdtSize
= (UINTN
)TssDesc
+ sizeof (IA32_TSS_DESCRIPTOR
) -
200 (UINTN
)(StackSwitchData
->X64
.GdtTable
);
201 if ((UINTN
)StackSwitchData
->X64
.GdtTable
!= Gdtr
.Base
) {
202 CopyMem (StackSwitchData
->X64
.GdtTable
, (VOID
*)Gdtr
.Base
, Gdtr
.Limit
+ 1);
203 Gdtr
.Base
= (UINTN
)StackSwitchData
->X64
.GdtTable
;
204 Gdtr
.Limit
= (UINT16
)GdtSize
- 1;
207 if ((UINTN
)StackSwitchData
->X64
.IdtTable
!= Idtr
.Base
) {
208 Idtr
.Base
= (UINTN
)StackSwitchData
->X64
.IdtTable
;
210 if (StackSwitchData
->X64
.IdtTableSize
> 0) {
211 Idtr
.Limit
= (UINT16
)(StackSwitchData
->X64
.IdtTableSize
- 1);
215 // Fixup current task descriptor. Task-state segment for current task will
216 // be filled by processor during task switching.
218 TssBase
= (UINTN
)Tss
;
220 TssDesc
->Bits
.LimitLow
= sizeof(IA32_TASK_STATE_SEGMENT
) - 1;
221 TssDesc
->Bits
.BaseLow
= (UINT16
)TssBase
;
222 TssDesc
->Bits
.BaseMidl
= (UINT8
)(TssBase
>> 16);
223 TssDesc
->Bits
.Type
= IA32_GDT_TYPE_TSS
;
224 TssDesc
->Bits
.Present
= 1;
225 TssDesc
->Bits
.LimitHigh
= 0;
226 TssDesc
->Bits
.BaseMidh
= (UINT8
)(TssBase
>> 24);
227 TssDesc
->Bits
.BaseHigh
= (UINT32
)(TssBase
>> 32);
230 // Fixup exception task descriptor and task-state segment
232 StackTop
= StackSwitchData
->X64
.KnownGoodStackTop
- CPU_STACK_ALIGNMENT
;
233 StackTop
= (UINTN
)ALIGN_POINTER (StackTop
, CPU_STACK_ALIGNMENT
);
234 IdtTable
= StackSwitchData
->X64
.IdtTable
;
235 for (Index
= 0; Index
< StackSwitchData
->X64
.StackSwitchExceptionNumber
; ++Index
) {
239 Tss
->Ist
[Index
] = StackTop
;
240 StackTop
-= StackSwitchData
->X64
.KnownGoodStackSize
;
243 // Set the IST field to enable corresponding IST
245 Vector
= StackSwitchData
->X64
.StackSwitchExceptions
[Index
];
246 if (Vector
>= CPU_EXCEPTION_NUM
||
247 Vector
>= (Idtr
.Limit
+ 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR
)) {
250 IdtTable
[Vector
].Bits
.Reserved_0
= (UINT8
)(Index
+ 1);
256 AsmWriteGdtr (&Gdtr
);
261 AsmWriteTr ((UINT16
)((UINTN
)StackSwitchData
->X64
.ExceptionTssDesc
- Gdtr
.Base
));
266 AsmWriteIdtr (&Idtr
);
272 Display CPU information.
274 @param ExceptionType Exception type.
275 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
280 IN EFI_EXCEPTION_TYPE ExceptionType
,
281 IN EFI_SYSTEM_CONTEXT SystemContext
284 InternalPrintMessage (
285 "!!!! X64 Exception Type - %02x(%a) CPU Apic ID - %08x !!!!\n",
287 GetExceptionNameStr (ExceptionType
),
290 if ((mErrorCodeFlag
& (1 << ExceptionType
)) != 0) {
291 InternalPrintMessage (
292 "ExceptionData - %016lx",
293 SystemContext
.SystemContextX64
->ExceptionData
295 if (ExceptionType
== EXCEPT_IA32_PAGE_FAULT
) {
296 InternalPrintMessage (
297 " I:%x R:%x U:%x W:%x P:%x PK:%x S:%x",
298 (SystemContext
.SystemContextX64
->ExceptionData
& IA32_PF_EC_ID
) != 0,
299 (SystemContext
.SystemContextX64
->ExceptionData
& IA32_PF_EC_RSVD
) != 0,
300 (SystemContext
.SystemContextX64
->ExceptionData
& IA32_PF_EC_US
) != 0,
301 (SystemContext
.SystemContextX64
->ExceptionData
& IA32_PF_EC_WR
) != 0,
302 (SystemContext
.SystemContextX64
->ExceptionData
& IA32_PF_EC_P
) != 0,
303 (SystemContext
.SystemContextX64
->ExceptionData
& IA32_PF_EC_PK
) != 0,
304 (SystemContext
.SystemContextX64
->ExceptionData
& IA32_PF_EC_SGX
) != 0
307 InternalPrintMessage ("\n");
309 InternalPrintMessage (
310 "RIP - %016lx, CS - %016lx, RFLAGS - %016lx\n",
311 SystemContext
.SystemContextX64
->Rip
,
312 SystemContext
.SystemContextX64
->Cs
,
313 SystemContext
.SystemContextX64
->Rflags
315 InternalPrintMessage (
316 "RAX - %016lx, RCX - %016lx, RDX - %016lx\n",
317 SystemContext
.SystemContextX64
->Rax
,
318 SystemContext
.SystemContextX64
->Rcx
,
319 SystemContext
.SystemContextX64
->Rdx
321 InternalPrintMessage (
322 "RBX - %016lx, RSP - %016lx, RBP - %016lx\n",
323 SystemContext
.SystemContextX64
->Rbx
,
324 SystemContext
.SystemContextX64
->Rsp
,
325 SystemContext
.SystemContextX64
->Rbp
327 InternalPrintMessage (
328 "RSI - %016lx, RDI - %016lx\n",
329 SystemContext
.SystemContextX64
->Rsi
,
330 SystemContext
.SystemContextX64
->Rdi
332 InternalPrintMessage (
333 "R8 - %016lx, R9 - %016lx, R10 - %016lx\n",
334 SystemContext
.SystemContextX64
->R8
,
335 SystemContext
.SystemContextX64
->R9
,
336 SystemContext
.SystemContextX64
->R10
338 InternalPrintMessage (
339 "R11 - %016lx, R12 - %016lx, R13 - %016lx\n",
340 SystemContext
.SystemContextX64
->R11
,
341 SystemContext
.SystemContextX64
->R12
,
342 SystemContext
.SystemContextX64
->R13
344 InternalPrintMessage (
345 "R14 - %016lx, R15 - %016lx\n",
346 SystemContext
.SystemContextX64
->R14
,
347 SystemContext
.SystemContextX64
->R15
349 InternalPrintMessage (
350 "DS - %016lx, ES - %016lx, FS - %016lx\n",
351 SystemContext
.SystemContextX64
->Ds
,
352 SystemContext
.SystemContextX64
->Es
,
353 SystemContext
.SystemContextX64
->Fs
355 InternalPrintMessage (
356 "GS - %016lx, SS - %016lx\n",
357 SystemContext
.SystemContextX64
->Gs
,
358 SystemContext
.SystemContextX64
->Ss
360 InternalPrintMessage (
361 "CR0 - %016lx, CR2 - %016lx, CR3 - %016lx\n",
362 SystemContext
.SystemContextX64
->Cr0
,
363 SystemContext
.SystemContextX64
->Cr2
,
364 SystemContext
.SystemContextX64
->Cr3
366 InternalPrintMessage (
367 "CR4 - %016lx, CR8 - %016lx\n",
368 SystemContext
.SystemContextX64
->Cr4
,
369 SystemContext
.SystemContextX64
->Cr8
371 InternalPrintMessage (
372 "DR0 - %016lx, DR1 - %016lx, DR2 - %016lx\n",
373 SystemContext
.SystemContextX64
->Dr0
,
374 SystemContext
.SystemContextX64
->Dr1
,
375 SystemContext
.SystemContextX64
->Dr2
377 InternalPrintMessage (
378 "DR3 - %016lx, DR6 - %016lx, DR7 - %016lx\n",
379 SystemContext
.SystemContextX64
->Dr3
,
380 SystemContext
.SystemContextX64
->Dr6
,
381 SystemContext
.SystemContextX64
->Dr7
383 InternalPrintMessage (
384 "GDTR - %016lx %016lx, LDTR - %016lx\n",
385 SystemContext
.SystemContextX64
->Gdtr
[0],
386 SystemContext
.SystemContextX64
->Gdtr
[1],
387 SystemContext
.SystemContextX64
->Ldtr
389 InternalPrintMessage (
390 "IDTR - %016lx %016lx, TR - %016lx\n",
391 SystemContext
.SystemContextX64
->Idtr
[0],
392 SystemContext
.SystemContextX64
->Idtr
[1],
393 SystemContext
.SystemContextX64
->Tr
395 InternalPrintMessage (
396 "FXSAVE_STATE - %016lx\n",
397 &SystemContext
.SystemContextX64
->FxSaveState
402 Display CPU information.
404 @param ExceptionType Exception type.
405 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
408 DumpImageAndCpuContent (
409 IN EFI_EXCEPTION_TYPE ExceptionType
,
410 IN EFI_SYSTEM_CONTEXT SystemContext
413 DumpCpuContext (ExceptionType
, SystemContext
);
415 // Dump module image base and module entry point by RIP
417 DumpModuleImageInfo (SystemContext
.SystemContextX64
->Rip
);