]>
Commit | Line | Data |
---|---|---|
8f07f895 | 1 | /** @file\r |
2 | x64 CPU Exception Hanlder.\r | |
3 | \r | |
e41aad15 | 4 | Copyright (c) 2012 - 2013, Intel Corporation. All rights reserved.<BR>\r |
8f07f895 | 5 | This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include "CpuExceptionCommon.h"\r | |
16 | \r | |
17 | /**\r | |
e41aad15 JF |
18 | Return address map of exception handler template so that C code can generate\r |
19 | exception tables.\r | |
8f07f895 | 20 | \r |
e41aad15 JF |
21 | @param IdtEntry Pointer to IDT entry to be updated.\r |
22 | @param InterruptHandler IDT handler value.\r | |
8f07f895 | 23 | **/\r |
24 | VOID\r | |
e41aad15 JF |
25 | ArchUpdateIdtEntry (\r |
26 | IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry,\r | |
27 | IN UINTN InterruptHandler\r | |
8f07f895 | 28 | )\r |
29 | {\r | |
e41aad15 JF |
30 | IdtEntry->Bits.OffsetLow = (UINT16)(UINTN)InterruptHandler;\r |
31 | IdtEntry->Bits.OffsetHigh = (UINT16)((UINTN)InterruptHandler >> 16);\r | |
32 | IdtEntry->Bits.OffsetUpper = (UINT32)((UINTN)InterruptHandler >> 32); \r | |
33 | IdtEntry->Bits.GateType = IA32_IDT_GATE_TYPE_INTERRUPT_32;\r | |
34 | }\r | |
35 | \r | |
36 | /**\r | |
37 | Read IDT handler value from IDT entry.\r | |
38 | \r | |
39 | @param IdtEntry Pointer to IDT entry to be read.\r | |
40 | \r | |
41 | **/\r | |
42 | UINTN\r | |
43 | ArchGetIdtHandler (\r | |
44 | IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry\r | |
45 | )\r | |
46 | {\r | |
47 | return IdtEntry->Bits.OffsetLow + (((UINTN) IdtEntry->Bits.OffsetHigh) << 16) +\r | |
48 | (((UINTN) IdtEntry->Bits.OffsetUpper) << 32);\r | |
49 | }\r | |
8f07f895 | 50 | \r |
e41aad15 JF |
51 | /**\r |
52 | Save CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.\r | |
53 | \r | |
54 | @param ExceptionType Exception type.\r | |
55 | @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r | |
56 | **/\r | |
57 | VOID\r | |
58 | ArchSaveExceptionContext (\r | |
59 | IN UINTN ExceptionType,\r | |
60 | IN EFI_SYSTEM_CONTEXT SystemContext \r | |
61 | )\r | |
62 | {\r | |
63 | IA32_EFLAGS32 Eflags;\r | |
8f07f895 | 64 | //\r |
e41aad15 | 65 | // Save Exception context in global variable\r |
8f07f895 | 66 | //\r |
e41aad15 JF |
67 | mReservedVectors[ExceptionType].OldSs = SystemContext.SystemContextX64->Ss;\r |
68 | mReservedVectors[ExceptionType].OldSp = SystemContext.SystemContextX64->Rsp;\r | |
69 | mReservedVectors[ExceptionType].OldFlags = SystemContext.SystemContextX64->Rflags;\r | |
70 | mReservedVectors[ExceptionType].OldCs = SystemContext.SystemContextX64->Cs;\r | |
71 | mReservedVectors[ExceptionType].OldIp = SystemContext.SystemContextX64->Rip;\r | |
72 | mReservedVectors[ExceptionType].ExceptionData = SystemContext.SystemContextX64->ExceptionData;\r | |
8f07f895 | 73 | //\r |
e41aad15 | 74 | // Clear IF flag to avoid old IDT handler enable interrupt by IRET\r |
8f07f895 | 75 | //\r |
e41aad15 JF |
76 | Eflags.UintN = SystemContext.SystemContextX64->Rflags;\r |
77 | Eflags.Bits.IF = 0; \r | |
78 | SystemContext.SystemContextX64->Rflags = Eflags.UintN;\r | |
79 | //\r | |
80 | // Modify the EIP in stack, then old IDT handler will return to the stub code\r | |
81 | //\r | |
82 | SystemContext.SystemContextX64->Rip = (UINTN) mReservedVectors[ExceptionType].HookAfterStubHeaderCode;\r | |
8f07f895 | 83 | }\r |
84 | \r | |
85 | /**\r | |
e41aad15 | 86 | Restore CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.\r |
8f07f895 | 87 | \r |
88 | @param ExceptionType Exception type.\r | |
89 | @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r | |
90 | **/\r | |
91 | VOID\r | |
e41aad15 | 92 | ArchRestoreExceptionContext (\r |
8f07f895 | 93 | IN UINTN ExceptionType,\r |
e41aad15 JF |
94 | IN EFI_SYSTEM_CONTEXT SystemContext \r |
95 | )\r | |
96 | {\r | |
97 | SystemContext.SystemContextX64->Ss = mReservedVectors[ExceptionType].OldSs;\r | |
98 | SystemContext.SystemContextX64->Rsp = mReservedVectors[ExceptionType].OldSp;\r | |
99 | SystemContext.SystemContextX64->Rflags = mReservedVectors[ExceptionType].OldFlags;\r | |
100 | SystemContext.SystemContextX64->Cs = mReservedVectors[ExceptionType].OldCs;\r | |
101 | SystemContext.SystemContextX64->Rip = mReservedVectors[ExceptionType].OldIp;\r | |
102 | SystemContext.SystemContextX64->ExceptionData = mReservedVectors[ExceptionType].ExceptionData;\r | |
103 | }\r | |
104 | \r | |
105 | /**\r | |
106 | Dump CPU content information.\r | |
107 | \r | |
108 | @param ExceptionType Exception type.\r | |
109 | @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r | |
110 | **/\r | |
111 | VOID\r | |
112 | DumpCpuContent (\r | |
113 | IN EFI_EXCEPTION_TYPE ExceptionType,\r | |
8f07f895 | 114 | IN EFI_SYSTEM_CONTEXT SystemContext\r |
115 | )\r | |
116 | {\r | |
117 | UINTN ImageBase;\r | |
118 | UINTN EntryPoint;\r | |
119 | \r | |
120 | InternalPrintMessage (\r | |
121 | "!!!! X64 Exception Type - %016lx CPU Apic ID - %08x !!!!\n",\r | |
122 | ExceptionType,\r | |
123 | GetApicId ()\r | |
124 | );\r | |
125 | InternalPrintMessage (\r | |
126 | "RIP - %016lx, CS - %016lx, RFLAGS - %016lx\n",\r | |
127 | SystemContext.SystemContextX64->Rip,\r | |
128 | SystemContext.SystemContextX64->Cs,\r | |
129 | SystemContext.SystemContextX64->Rflags\r | |
130 | );\r | |
131 | if (mErrorCodeFlag & (1 << ExceptionType)) {\r | |
132 | InternalPrintMessage (\r | |
133 | "ExceptionData - %016lx\n",\r | |
134 | SystemContext.SystemContextX64->ExceptionData\r | |
135 | );\r | |
136 | }\r | |
137 | InternalPrintMessage (\r | |
138 | "RAX - %016lx, RCX - %016lx, RDX - %016lx\n",\r | |
139 | SystemContext.SystemContextX64->Rax,\r | |
140 | SystemContext.SystemContextX64->Rcx,\r | |
141 | SystemContext.SystemContextX64->Rdx\r | |
142 | );\r | |
143 | InternalPrintMessage (\r | |
144 | "RBX - %016lx, RSP - %016lx, RBP - %016lx\n",\r | |
145 | SystemContext.SystemContextX64->Rbx,\r | |
146 | SystemContext.SystemContextX64->Rsp,\r | |
147 | SystemContext.SystemContextX64->Rbp\r | |
148 | );\r | |
149 | InternalPrintMessage (\r | |
150 | "RSI - %016lx, RDI - %016lx\n",\r | |
151 | SystemContext.SystemContextX64->Rsi,\r | |
152 | SystemContext.SystemContextX64->Rdi\r | |
153 | );\r | |
154 | InternalPrintMessage (\r | |
155 | "R8 - %016lx, R9 - %016lx, R10 - %016lx\n",\r | |
156 | SystemContext.SystemContextX64->R8,\r | |
157 | SystemContext.SystemContextX64->R9,\r | |
158 | SystemContext.SystemContextX64->R10\r | |
159 | );\r | |
160 | InternalPrintMessage (\r | |
161 | "R11 - %016lx, R12 - %016lx, R13 - %016lx\n",\r | |
162 | SystemContext.SystemContextX64->R11,\r | |
163 | SystemContext.SystemContextX64->R12,\r | |
164 | SystemContext.SystemContextX64->R13\r | |
165 | );\r | |
166 | InternalPrintMessage (\r | |
167 | "R14 - %016lx, R15 - %016lx\n",\r | |
168 | SystemContext.SystemContextX64->R14,\r | |
169 | SystemContext.SystemContextX64->R15\r | |
170 | );\r | |
171 | InternalPrintMessage (\r | |
172 | "DS - %016lx, ES - %016lx, FS - %016lx\n",\r | |
173 | SystemContext.SystemContextX64->Ds,\r | |
174 | SystemContext.SystemContextX64->Es,\r | |
175 | SystemContext.SystemContextX64->Fs\r | |
176 | );\r | |
177 | InternalPrintMessage (\r | |
178 | "GS - %016lx, SS - %016lx\n",\r | |
179 | SystemContext.SystemContextX64->Gs,\r | |
180 | SystemContext.SystemContextX64->Ss\r | |
181 | );\r | |
182 | InternalPrintMessage (\r | |
183 | "CR0 - %016lx, CR2 - %016lx, CR3 - %016lx\n",\r | |
184 | SystemContext.SystemContextX64->Cr0,\r | |
185 | SystemContext.SystemContextX64->Cr2,\r | |
186 | SystemContext.SystemContextX64->Cr3\r | |
187 | );\r | |
188 | InternalPrintMessage (\r | |
189 | "CR4 - %016lx, CR8 - %016lx\n",\r | |
190 | SystemContext.SystemContextX64->Cr4,\r | |
191 | SystemContext.SystemContextX64->Cr8\r | |
192 | );\r | |
193 | InternalPrintMessage (\r | |
194 | "DR0 - %016lx, DR1 - %016lx, DR2 - %016lx\n",\r | |
195 | SystemContext.SystemContextX64->Dr0,\r | |
196 | SystemContext.SystemContextX64->Dr1,\r | |
197 | SystemContext.SystemContextX64->Dr2\r | |
198 | );\r | |
199 | InternalPrintMessage (\r | |
200 | "DR3 - %016lx, DR6 - %016lx, DR7 - %016lx\n",\r | |
201 | SystemContext.SystemContextX64->Dr3,\r | |
202 | SystemContext.SystemContextX64->Dr6,\r | |
203 | SystemContext.SystemContextX64->Dr7\r | |
204 | );\r | |
205 | InternalPrintMessage (\r | |
206 | "GDTR - %016lx %016lx, LDTR - %016lx\n",\r | |
207 | SystemContext.SystemContextX64->Gdtr[0],\r | |
208 | SystemContext.SystemContextX64->Gdtr[1],\r | |
209 | SystemContext.SystemContextX64->Ldtr\r | |
210 | );\r | |
211 | InternalPrintMessage (\r | |
212 | "IDTR - %016lx %016lx, TR - %016lx\n",\r | |
213 | SystemContext.SystemContextX64->Idtr[0],\r | |
214 | SystemContext.SystemContextX64->Idtr[1],\r | |
215 | SystemContext.SystemContextX64->Tr\r | |
216 | );\r | |
217 | InternalPrintMessage (\r | |
218 | "FXSAVE_STATE - %016lx\n",\r | |
219 | &SystemContext.SystemContextX64->FxSaveState\r | |
220 | );\r | |
221 | \r | |
222 | //\r | |
223 | // Find module image base and module entry point by RIP\r | |
224 | //\r | |
225 | ImageBase = FindModuleImageBase (SystemContext.SystemContextX64->Rip, &EntryPoint);\r | |
226 | if (ImageBase != 0) {\r | |
227 | InternalPrintMessage (\r | |
228 | " (ImageBase=%016lx, EntryPoint=%016lx) !!!!\n",\r | |
229 | ImageBase,\r | |
230 | EntryPoint\r | |
231 | );\r | |
232 | }\r | |
233 | }\r |