]>
Commit | Line | Data |
---|---|---|
8f07f895 | 1 | /** @file\r |
e3644786 | 2 | IA32 CPU Exception Handler functons.\r |
8f07f895 | 3 | \r |
81b21fc2 | 4 | Copyright (c) 2012 - 2016, 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 | |
20 | \r | |
21 | @param IdtEntry Pointer to IDT entry to be updated.\r | |
22 | @param InterruptHandler IDT handler value.\r | |
8f07f895 | 23 | \r |
24 | **/\r | |
25 | VOID\r | |
e41aad15 JF |
26 | ArchUpdateIdtEntry (\r |
27 | IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry,\r | |
28 | IN UINTN InterruptHandler\r | |
29 | )\r | |
30 | {\r | |
31 | IdtEntry->Bits.OffsetLow = (UINT16)(UINTN)InterruptHandler;\r | |
32 | IdtEntry->Bits.OffsetHigh = (UINT16)((UINTN)InterruptHandler >> 16);\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 | |
8f07f895 | 45 | )\r |
46 | {\r | |
e41aad15 JF |
47 | return (UINTN)IdtEntry->Bits.OffsetLow + (((UINTN)IdtEntry->Bits.OffsetHigh) << 16);\r |
48 | }\r | |
49 | \r | |
50 | /**\r | |
51 | Save CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.\r | |
8f07f895 | 52 | \r |
81b21fc2 JF |
53 | @param[in] ExceptionType Exception type.\r |
54 | @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r | |
55 | @param[in] ExceptionHandlerData Pointer to exception handler data.\r | |
e41aad15 JF |
56 | **/\r |
57 | VOID\r | |
58 | ArchSaveExceptionContext (\r | |
81b21fc2 JF |
59 | IN UINTN ExceptionType,\r |
60 | IN EFI_SYSTEM_CONTEXT SystemContext,\r | |
61 | IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData\r | |
e41aad15 JF |
62 | )\r |
63 | {\r | |
64 | IA32_EFLAGS32 Eflags;\r | |
81b21fc2 JF |
65 | RESERVED_VECTORS_DATA *ReservedVectors;\r |
66 | \r | |
67 | ReservedVectors = ExceptionHandlerData->ReservedVectors;\r | |
8f07f895 | 68 | //\r |
e41aad15 | 69 | // Save Exception context in global variable\r |
8f07f895 | 70 | //\r |
81b21fc2 JF |
71 | ReservedVectors[ExceptionType].OldFlags = SystemContext.SystemContextIa32->Eflags;\r |
72 | ReservedVectors[ExceptionType].OldCs = SystemContext.SystemContextIa32->Cs;\r | |
73 | ReservedVectors[ExceptionType].OldIp = SystemContext.SystemContextIa32->Eip;\r | |
74 | ReservedVectors[ExceptionType].ExceptionData = SystemContext.SystemContextIa32->ExceptionData;\r | |
e41aad15 JF |
75 | //\r |
76 | // Clear IF flag to avoid old IDT handler enable interrupt by IRET\r | |
8f07f895 | 77 | //\r |
e41aad15 JF |
78 | Eflags.UintN = SystemContext.SystemContextIa32->Eflags;\r |
79 | Eflags.Bits.IF = 0; \r | |
80 | SystemContext.SystemContextIa32->Eflags = Eflags.UintN;\r | |
8f07f895 | 81 | //\r |
e41aad15 JF |
82 | // Modify the EIP in stack, then old IDT handler will return to the stub code\r |
83 | //\r | |
81b21fc2 | 84 | SystemContext.SystemContextIa32->Eip = (UINTN) ReservedVectors[ExceptionType].HookAfterStubHeaderCode;\r |
e41aad15 | 85 | }\r |
8f07f895 | 86 | \r |
e41aad15 JF |
87 | /**\r |
88 | Restore CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.\r | |
8f07f895 | 89 | \r |
368c54e7 JF |
90 | @param[in] ExceptionType Exception type.\r |
91 | @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r | |
92 | @param[in] ExceptionHandlerData Pointer to exception handler data.\r | |
e41aad15 JF |
93 | **/\r |
94 | VOID\r | |
95 | ArchRestoreExceptionContext (\r | |
368c54e7 JF |
96 | IN UINTN ExceptionType,\r |
97 | IN EFI_SYSTEM_CONTEXT SystemContext,\r | |
98 | IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData\r | |
e41aad15 JF |
99 | )\r |
100 | {\r | |
368c54e7 JF |
101 | RESERVED_VECTORS_DATA *ReservedVectors;\r |
102 | \r | |
103 | ReservedVectors = ExceptionHandlerData->ReservedVectors;\r | |
104 | SystemContext.SystemContextIa32->Eflags = ReservedVectors[ExceptionType].OldFlags;\r | |
105 | SystemContext.SystemContextIa32->Cs = ReservedVectors[ExceptionType].OldCs;\r | |
106 | SystemContext.SystemContextIa32->Eip = ReservedVectors[ExceptionType].OldIp;\r | |
107 | SystemContext.SystemContextIa32->ExceptionData = ReservedVectors[ExceptionType].ExceptionData;\r | |
8f07f895 | 108 | }\r |
109 | \r | |
110 | /**\r | |
3f25e6ea | 111 | Display CPU information.\r |
8f07f895 | 112 | \r |
113 | @param ExceptionType Exception type.\r | |
114 | @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r | |
115 | **/\r | |
116 | VOID\r | |
117 | DumpCpuContent (\r | |
e41aad15 | 118 | IN EFI_EXCEPTION_TYPE ExceptionType,\r |
8f07f895 | 119 | IN EFI_SYSTEM_CONTEXT SystemContext\r |
120 | )\r | |
121 | {\r | |
122 | UINTN ImageBase;\r | |
123 | UINTN EntryPoint;\r | |
124 | \r | |
125 | InternalPrintMessage (\r | |
a51ee144 | 126 | "!!!! IA32 Exception Type - %02x(%a) CPU Apic ID - %08x !!!!\n",\r |
8f07f895 | 127 | ExceptionType,\r |
a51ee144 | 128 | GetExceptionNameStr (ExceptionType),\r |
8f07f895 | 129 | GetApicId ()\r |
130 | );\r | |
a51ee144 | 131 | \r |
8f07f895 | 132 | InternalPrintMessage (\r |
133 | "EIP - %08x, CS - %08x, EFLAGS - %08x\n",\r | |
134 | SystemContext.SystemContextIa32->Eip,\r | |
135 | SystemContext.SystemContextIa32->Cs,\r | |
136 | SystemContext.SystemContextIa32->Eflags\r | |
137 | );\r | |
138 | if ((mErrorCodeFlag & (1 << ExceptionType)) != 0) {\r | |
139 | InternalPrintMessage (\r | |
140 | "ExceptionData - %08x\n",\r | |
141 | SystemContext.SystemContextIa32->ExceptionData\r | |
142 | );\r | |
143 | }\r | |
144 | InternalPrintMessage (\r | |
145 | "EAX - %08x, ECX - %08x, EDX - %08x, EBX - %08x\n",\r | |
146 | SystemContext.SystemContextIa32->Eax,\r | |
147 | SystemContext.SystemContextIa32->Ecx,\r | |
148 | SystemContext.SystemContextIa32->Edx,\r | |
149 | SystemContext.SystemContextIa32->Ebx\r | |
150 | );\r | |
151 | InternalPrintMessage (\r | |
152 | "ESP - %08x, EBP - %08x, ESI - %08x, EDI - %08x\n",\r | |
153 | SystemContext.SystemContextIa32->Esp,\r | |
154 | SystemContext.SystemContextIa32->Ebp,\r | |
155 | SystemContext.SystemContextIa32->Esi,\r | |
156 | SystemContext.SystemContextIa32->Edi\r | |
157 | );\r | |
158 | InternalPrintMessage (\r | |
159 | "DS - %08x, ES - %08x, FS - %08x, GS - %08x, SS - %08x\n",\r | |
160 | SystemContext.SystemContextIa32->Ds,\r | |
161 | SystemContext.SystemContextIa32->Es,\r | |
162 | SystemContext.SystemContextIa32->Fs,\r | |
163 | SystemContext.SystemContextIa32->Gs,\r | |
164 | SystemContext.SystemContextIa32->Ss\r | |
165 | );\r | |
166 | InternalPrintMessage (\r | |
167 | "CR0 - %08x, CR2 - %08x, CR3 - %08x, CR4 - %08x\n",\r | |
168 | SystemContext.SystemContextIa32->Cr0,\r | |
169 | SystemContext.SystemContextIa32->Cr2,\r | |
170 | SystemContext.SystemContextIa32->Cr3,\r | |
171 | SystemContext.SystemContextIa32->Cr4\r | |
172 | );\r | |
173 | InternalPrintMessage (\r | |
174 | "DR0 - %08x, DR1 - %08x, DR2 - %08x, DR3 - %08x\n",\r | |
175 | SystemContext.SystemContextIa32->Dr0,\r | |
176 | SystemContext.SystemContextIa32->Dr1,\r | |
177 | SystemContext.SystemContextIa32->Dr2,\r | |
178 | SystemContext.SystemContextIa32->Dr3\r | |
179 | );\r | |
180 | InternalPrintMessage (\r | |
181 | "DR6 - %08x, DR7 - %08x\n",\r | |
182 | SystemContext.SystemContextIa32->Dr6,\r | |
183 | SystemContext.SystemContextIa32->Dr7\r | |
184 | );\r | |
185 | InternalPrintMessage (\r | |
186 | "GDTR - %08x %08x, IDTR - %08x %08x\n",\r | |
187 | SystemContext.SystemContextIa32->Gdtr[0],\r | |
188 | SystemContext.SystemContextIa32->Gdtr[1],\r | |
189 | SystemContext.SystemContextIa32->Idtr[0],\r | |
190 | SystemContext.SystemContextIa32->Idtr[1]\r | |
191 | );\r | |
192 | InternalPrintMessage (\r | |
193 | "LDTR - %08x, TR - %08x\n",\r | |
194 | SystemContext.SystemContextIa32->Ldtr,\r | |
195 | SystemContext.SystemContextIa32->Tr\r | |
196 | );\r | |
197 | InternalPrintMessage (\r | |
198 | "FXSAVE_STATE - %08x\n",\r | |
199 | &SystemContext.SystemContextIa32->FxSaveState\r | |
200 | );\r | |
201 | \r | |
202 | //\r | |
203 | // Find module image base and module entry point by RIP\r | |
204 | //\r | |
205 | ImageBase = FindModuleImageBase (SystemContext.SystemContextIa32->Eip, &EntryPoint);\r | |
206 | if (ImageBase != 0) {\r | |
207 | InternalPrintMessage (\r | |
208 | " (ImageBase=%08x, EntryPoint=%08x) !!!!\n",\r | |
209 | ImageBase,\r | |
210 | EntryPoint\r | |
211 | );\r | |
212 | }\r | |
213 | }\r |