2 x64 CPU Exception Handler.
4 Copyright (c) 2012 - 2016, 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 ExceptionType Exception type.
94 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
97 ArchRestoreExceptionContext (
98 IN UINTN ExceptionType
,
99 IN EFI_SYSTEM_CONTEXT SystemContext
102 SystemContext
.SystemContextX64
->Ss
= mReservedVectors
[ExceptionType
].OldSs
;
103 SystemContext
.SystemContextX64
->Rsp
= mReservedVectors
[ExceptionType
].OldSp
;
104 SystemContext
.SystemContextX64
->Rflags
= mReservedVectors
[ExceptionType
].OldFlags
;
105 SystemContext
.SystemContextX64
->Cs
= mReservedVectors
[ExceptionType
].OldCs
;
106 SystemContext
.SystemContextX64
->Rip
= mReservedVectors
[ExceptionType
].OldIp
;
107 SystemContext
.SystemContextX64
->ExceptionData
= mReservedVectors
[ExceptionType
].ExceptionData
;
111 Display CPU information.
113 @param ExceptionType Exception type.
114 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
118 IN EFI_EXCEPTION_TYPE ExceptionType
,
119 IN EFI_SYSTEM_CONTEXT SystemContext
125 InternalPrintMessage (
126 "!!!! X64 Exception Type - %02x(%a) CPU Apic ID - %08x !!!!\n",
128 GetExceptionNameStr (ExceptionType
),
132 InternalPrintMessage (
133 "RIP - %016lx, CS - %016lx, RFLAGS - %016lx\n",
134 SystemContext
.SystemContextX64
->Rip
,
135 SystemContext
.SystemContextX64
->Cs
,
136 SystemContext
.SystemContextX64
->Rflags
138 if (mErrorCodeFlag
& (1 << ExceptionType
)) {
139 InternalPrintMessage (
140 "ExceptionData - %016lx\n",
141 SystemContext
.SystemContextX64
->ExceptionData
144 InternalPrintMessage (
145 "RAX - %016lx, RCX - %016lx, RDX - %016lx\n",
146 SystemContext
.SystemContextX64
->Rax
,
147 SystemContext
.SystemContextX64
->Rcx
,
148 SystemContext
.SystemContextX64
->Rdx
150 InternalPrintMessage (
151 "RBX - %016lx, RSP - %016lx, RBP - %016lx\n",
152 SystemContext
.SystemContextX64
->Rbx
,
153 SystemContext
.SystemContextX64
->Rsp
,
154 SystemContext
.SystemContextX64
->Rbp
156 InternalPrintMessage (
157 "RSI - %016lx, RDI - %016lx\n",
158 SystemContext
.SystemContextX64
->Rsi
,
159 SystemContext
.SystemContextX64
->Rdi
161 InternalPrintMessage (
162 "R8 - %016lx, R9 - %016lx, R10 - %016lx\n",
163 SystemContext
.SystemContextX64
->R8
,
164 SystemContext
.SystemContextX64
->R9
,
165 SystemContext
.SystemContextX64
->R10
167 InternalPrintMessage (
168 "R11 - %016lx, R12 - %016lx, R13 - %016lx\n",
169 SystemContext
.SystemContextX64
->R11
,
170 SystemContext
.SystemContextX64
->R12
,
171 SystemContext
.SystemContextX64
->R13
173 InternalPrintMessage (
174 "R14 - %016lx, R15 - %016lx\n",
175 SystemContext
.SystemContextX64
->R14
,
176 SystemContext
.SystemContextX64
->R15
178 InternalPrintMessage (
179 "DS - %016lx, ES - %016lx, FS - %016lx\n",
180 SystemContext
.SystemContextX64
->Ds
,
181 SystemContext
.SystemContextX64
->Es
,
182 SystemContext
.SystemContextX64
->Fs
184 InternalPrintMessage (
185 "GS - %016lx, SS - %016lx\n",
186 SystemContext
.SystemContextX64
->Gs
,
187 SystemContext
.SystemContextX64
->Ss
189 InternalPrintMessage (
190 "CR0 - %016lx, CR2 - %016lx, CR3 - %016lx\n",
191 SystemContext
.SystemContextX64
->Cr0
,
192 SystemContext
.SystemContextX64
->Cr2
,
193 SystemContext
.SystemContextX64
->Cr3
195 InternalPrintMessage (
196 "CR4 - %016lx, CR8 - %016lx\n",
197 SystemContext
.SystemContextX64
->Cr4
,
198 SystemContext
.SystemContextX64
->Cr8
200 InternalPrintMessage (
201 "DR0 - %016lx, DR1 - %016lx, DR2 - %016lx\n",
202 SystemContext
.SystemContextX64
->Dr0
,
203 SystemContext
.SystemContextX64
->Dr1
,
204 SystemContext
.SystemContextX64
->Dr2
206 InternalPrintMessage (
207 "DR3 - %016lx, DR6 - %016lx, DR7 - %016lx\n",
208 SystemContext
.SystemContextX64
->Dr3
,
209 SystemContext
.SystemContextX64
->Dr6
,
210 SystemContext
.SystemContextX64
->Dr7
212 InternalPrintMessage (
213 "GDTR - %016lx %016lx, LDTR - %016lx\n",
214 SystemContext
.SystemContextX64
->Gdtr
[0],
215 SystemContext
.SystemContextX64
->Gdtr
[1],
216 SystemContext
.SystemContextX64
->Ldtr
218 InternalPrintMessage (
219 "IDTR - %016lx %016lx, TR - %016lx\n",
220 SystemContext
.SystemContextX64
->Idtr
[0],
221 SystemContext
.SystemContextX64
->Idtr
[1],
222 SystemContext
.SystemContextX64
->Tr
224 InternalPrintMessage (
225 "FXSAVE_STATE - %016lx\n",
226 &SystemContext
.SystemContextX64
->FxSaveState
230 // Find module image base and module entry point by RIP
232 ImageBase
= FindModuleImageBase (SystemContext
.SystemContextX64
->Rip
, &EntryPoint
);
233 if (ImageBase
!= 0) {
234 InternalPrintMessage (
235 " (ImageBase=%016lx, EntryPoint=%016lx) !!!!\n",