]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c
UefiCpuPkg: Update code to use new structure field names
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / X64 / ArchExceptionHandler.c
1 /** @file
2 x64 CPU Exception Handler.
3
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
9
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.
12
13 **/
14
15 #include "CpuExceptionCommon.h"
16
17 /**
18 Return address map of exception handler template so that C code can generate
19 exception tables.
20
21 @param IdtEntry Pointer to IDT entry to be updated.
22 @param InterruptHandler IDT handler value.
23 **/
24 VOID
25 ArchUpdateIdtEntry (
26 IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry,
27 IN UINTN InterruptHandler
28 )
29 {
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;
34 }
35
36 /**
37 Read IDT handler value from IDT entry.
38
39 @param IdtEntry Pointer to IDT entry to be read.
40
41 **/
42 UINTN
43 ArchGetIdtHandler (
44 IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry
45 )
46 {
47 return IdtEntry->Bits.OffsetLow + (((UINTN) IdtEntry->Bits.OffsetHigh) << 16) +
48 (((UINTN) IdtEntry->Bits.OffsetUpper) << 32);
49 }
50
51 /**
52 Save CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
53
54 @param[in] ExceptionType Exception type.
55 @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.
56 @param[in] ExceptionHandlerData Pointer to exception handler data.
57 **/
58 VOID
59 ArchSaveExceptionContext (
60 IN UINTN ExceptionType,
61 IN EFI_SYSTEM_CONTEXT SystemContext,
62 IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData
63 )
64 {
65 IA32_EFLAGS32 Eflags;
66 RESERVED_VECTORS_DATA *ReservedVectors;
67
68 ReservedVectors = ExceptionHandlerData->ReservedVectors;
69 //
70 // Save Exception context in global variable
71 //
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;
78 //
79 // Clear IF flag to avoid old IDT handler enable interrupt by IRET
80 //
81 Eflags.UintN = SystemContext.SystemContextX64->Rflags;
82 Eflags.Bits.IF = 0;
83 SystemContext.SystemContextX64->Rflags = Eflags.UintN;
84 //
85 // Modify the EIP in stack, then old IDT handler will return to the stub code
86 //
87 SystemContext.SystemContextX64->Rip = (UINTN) ReservedVectors[ExceptionType].HookAfterStubHeaderCode;
88 }
89
90 /**
91 Restore CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
92
93 @param[in] ExceptionType Exception type.
94 @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.
95 @param[in] ExceptionHandlerData Pointer to exception handler data.
96 **/
97 VOID
98 ArchRestoreExceptionContext (
99 IN UINTN ExceptionType,
100 IN EFI_SYSTEM_CONTEXT SystemContext,
101 IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData
102 )
103 {
104 RESERVED_VECTORS_DATA *ReservedVectors;
105
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;
113 }
114
115 /**
116 Setup separate stack for given exceptions.
117
118 @param[in] StackSwitchData Pointer to data required for setuping up
119 stack switch.
120
121 @retval EFI_SUCCESS The exceptions have been successfully
122 initialized with new stack.
123 @retval EFI_INVALID_PARAMETER StackSwitchData contains invalid content.
124
125 **/
126 EFI_STATUS
127 ArchSetupExcpetionStack (
128 IN CPU_EXCEPTION_INIT_DATA *StackSwitchData
129 )
130 {
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;
136 UINTN StackTop;
137 UINTN Index;
138 UINTN Vector;
139 UINTN TssBase;
140 UINTN GdtSize;
141
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;
154 }
155
156 //
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.
160 //
161 if (((UINTN)StackSwitchData->X64.GdtTable & (IA32_GDT_ALIGNMENT - 1)) != 0) {
162 return EFI_INVALID_PARAMETER;
163 }
164
165 if ((UINTN)StackSwitchData->X64.ExceptionTssDesc < (UINTN)(StackSwitchData->X64.GdtTable)) {
166 return EFI_INVALID_PARAMETER;
167 }
168
169 if (((UINTN)StackSwitchData->X64.ExceptionTssDesc + StackSwitchData->X64.ExceptionTssDescSize) >
170 ((UINTN)(StackSwitchData->X64.GdtTable) + StackSwitchData->X64.GdtTableSize)) {
171 return EFI_INVALID_PARAMETER;
172 }
173
174 //
175 // One task gate descriptor and one task-state segment are needed.
176 //
177 if (StackSwitchData->X64.ExceptionTssDescSize < sizeof (IA32_TSS_DESCRIPTOR)) {
178 return EFI_INVALID_PARAMETER;
179 }
180 if (StackSwitchData->X64.ExceptionTssSize < sizeof (IA32_TASK_STATE_SEGMENT)) {
181 return EFI_INVALID_PARAMETER;
182 }
183
184 //
185 // Interrupt stack table supports only 7 vectors.
186 //
187 TssDesc = StackSwitchData->X64.ExceptionTssDesc;
188 Tss = StackSwitchData->X64.ExceptionTss;
189 if (StackSwitchData->X64.StackSwitchExceptionNumber > ARRAY_SIZE (Tss->Ist)) {
190 return EFI_INVALID_PARAMETER;
191 }
192
193 //
194 // Initialize new GDT table and/or IDT table, if any
195 //
196 AsmReadIdtr (&Idtr);
197 AsmReadGdtr (&Gdtr);
198
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;
205 }
206
207 if ((UINTN)StackSwitchData->X64.IdtTable != Idtr.Base) {
208 Idtr.Base = (UINTN)StackSwitchData->X64.IdtTable;
209 }
210 if (StackSwitchData->X64.IdtTableSize > 0) {
211 Idtr.Limit = (UINT16)(StackSwitchData->X64.IdtTableSize - 1);
212 }
213
214 //
215 // Fixup current task descriptor. Task-state segment for current task will
216 // be filled by processor during task switching.
217 //
218 TssBase = (UINTN)Tss;
219
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);
228
229 //
230 // Fixup exception task descriptor and task-state segment
231 //
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) {
236 //
237 // Fixup IST
238 //
239 Tss->Ist[Index] = StackTop;
240 StackTop -= StackSwitchData->X64.KnownGoodStackSize;
241
242 //
243 // Set the IST field to enable corresponding IST
244 //
245 Vector = StackSwitchData->X64.StackSwitchExceptions[Index];
246 if (Vector >= CPU_EXCEPTION_NUM ||
247 Vector >= (Idtr.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR)) {
248 continue;
249 }
250 IdtTable[Vector].Bits.Reserved_0 = (UINT8)(Index + 1);
251 }
252
253 //
254 // Publish GDT
255 //
256 AsmWriteGdtr (&Gdtr);
257
258 //
259 // Load current task
260 //
261 AsmWriteTr ((UINT16)((UINTN)StackSwitchData->X64.ExceptionTssDesc - Gdtr.Base));
262
263 //
264 // Publish IDT
265 //
266 AsmWriteIdtr (&Idtr);
267
268 return EFI_SUCCESS;
269 }
270
271 /**
272 Display CPU information.
273
274 @param ExceptionType Exception type.
275 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
276 **/
277 VOID
278 EFIAPI
279 DumpCpuContext (
280 IN EFI_EXCEPTION_TYPE ExceptionType,
281 IN EFI_SYSTEM_CONTEXT SystemContext
282 )
283 {
284 InternalPrintMessage (
285 "!!!! X64 Exception Type - %02x(%a) CPU Apic ID - %08x !!!!\n",
286 ExceptionType,
287 GetExceptionNameStr (ExceptionType),
288 GetApicId ()
289 );
290 if ((mErrorCodeFlag & (1 << ExceptionType)) != 0) {
291 InternalPrintMessage (
292 "ExceptionData - %016lx",
293 SystemContext.SystemContextX64->ExceptionData
294 );
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
305 );
306 }
307 InternalPrintMessage ("\n");
308 }
309 InternalPrintMessage (
310 "RIP - %016lx, CS - %016lx, RFLAGS - %016lx\n",
311 SystemContext.SystemContextX64->Rip,
312 SystemContext.SystemContextX64->Cs,
313 SystemContext.SystemContextX64->Rflags
314 );
315 InternalPrintMessage (
316 "RAX - %016lx, RCX - %016lx, RDX - %016lx\n",
317 SystemContext.SystemContextX64->Rax,
318 SystemContext.SystemContextX64->Rcx,
319 SystemContext.SystemContextX64->Rdx
320 );
321 InternalPrintMessage (
322 "RBX - %016lx, RSP - %016lx, RBP - %016lx\n",
323 SystemContext.SystemContextX64->Rbx,
324 SystemContext.SystemContextX64->Rsp,
325 SystemContext.SystemContextX64->Rbp
326 );
327 InternalPrintMessage (
328 "RSI - %016lx, RDI - %016lx\n",
329 SystemContext.SystemContextX64->Rsi,
330 SystemContext.SystemContextX64->Rdi
331 );
332 InternalPrintMessage (
333 "R8 - %016lx, R9 - %016lx, R10 - %016lx\n",
334 SystemContext.SystemContextX64->R8,
335 SystemContext.SystemContextX64->R9,
336 SystemContext.SystemContextX64->R10
337 );
338 InternalPrintMessage (
339 "R11 - %016lx, R12 - %016lx, R13 - %016lx\n",
340 SystemContext.SystemContextX64->R11,
341 SystemContext.SystemContextX64->R12,
342 SystemContext.SystemContextX64->R13
343 );
344 InternalPrintMessage (
345 "R14 - %016lx, R15 - %016lx\n",
346 SystemContext.SystemContextX64->R14,
347 SystemContext.SystemContextX64->R15
348 );
349 InternalPrintMessage (
350 "DS - %016lx, ES - %016lx, FS - %016lx\n",
351 SystemContext.SystemContextX64->Ds,
352 SystemContext.SystemContextX64->Es,
353 SystemContext.SystemContextX64->Fs
354 );
355 InternalPrintMessage (
356 "GS - %016lx, SS - %016lx\n",
357 SystemContext.SystemContextX64->Gs,
358 SystemContext.SystemContextX64->Ss
359 );
360 InternalPrintMessage (
361 "CR0 - %016lx, CR2 - %016lx, CR3 - %016lx\n",
362 SystemContext.SystemContextX64->Cr0,
363 SystemContext.SystemContextX64->Cr2,
364 SystemContext.SystemContextX64->Cr3
365 );
366 InternalPrintMessage (
367 "CR4 - %016lx, CR8 - %016lx\n",
368 SystemContext.SystemContextX64->Cr4,
369 SystemContext.SystemContextX64->Cr8
370 );
371 InternalPrintMessage (
372 "DR0 - %016lx, DR1 - %016lx, DR2 - %016lx\n",
373 SystemContext.SystemContextX64->Dr0,
374 SystemContext.SystemContextX64->Dr1,
375 SystemContext.SystemContextX64->Dr2
376 );
377 InternalPrintMessage (
378 "DR3 - %016lx, DR6 - %016lx, DR7 - %016lx\n",
379 SystemContext.SystemContextX64->Dr3,
380 SystemContext.SystemContextX64->Dr6,
381 SystemContext.SystemContextX64->Dr7
382 );
383 InternalPrintMessage (
384 "GDTR - %016lx %016lx, LDTR - %016lx\n",
385 SystemContext.SystemContextX64->Gdtr[0],
386 SystemContext.SystemContextX64->Gdtr[1],
387 SystemContext.SystemContextX64->Ldtr
388 );
389 InternalPrintMessage (
390 "IDTR - %016lx %016lx, TR - %016lx\n",
391 SystemContext.SystemContextX64->Idtr[0],
392 SystemContext.SystemContextX64->Idtr[1],
393 SystemContext.SystemContextX64->Tr
394 );
395 InternalPrintMessage (
396 "FXSAVE_STATE - %016lx\n",
397 &SystemContext.SystemContextX64->FxSaveState
398 );
399 }
400
401 /**
402 Display CPU information.
403
404 @param ExceptionType Exception type.
405 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
406 **/
407 VOID
408 DumpImageAndCpuContent (
409 IN EFI_EXCEPTION_TYPE ExceptionType,
410 IN EFI_SYSTEM_CONTEXT SystemContext
411 )
412 {
413 DumpCpuContext (ExceptionType, SystemContext);
414 //
415 // Dump module image base and module entry point by RIP
416 //
417 DumpModuleImageInfo (SystemContext.SystemContextX64->Rip);
418 }