TianoCore EDK2 master
Loading...
Searching...
No Matches
DefaultExceptionHandler.c
Go to the documentation of this file.
1
11#include <Uefi.h>
12#include <Library/UefiLib.h>
13#include <Library/BaseLib.h>
14#include <Library/DebugLib.h>
16#include <Library/PrintLib.h>
20
24
25//
26// Maximum number of characters to print to serial (UINT8s) and to console if
27// available (as UINT16s)
28//
29#define MAX_PRINT_CHARS 100
30
31STATIC CHAR8 *gExceptionTypeString[] = {
32 "Synchronous",
33 "IRQ",
34 "FIQ",
35 "SError"
36};
37
38STATIC BOOLEAN mRecursiveException;
39
40CHAR8 *
42 IN UINTN FaultAddress,
43 OUT UINTN *ImageBase,
44 OUT UINTN *PeCoffSizeOfHeaders
45 );
46
48VOID
49DescribeInstructionOrDataAbort (
50 IN CHAR8 *AbortType,
51 IN UINTN Iss
52 )
53{
54 CHAR8 *AbortCause;
55
56 switch (Iss & 0x3f) {
57 case 0x0: AbortCause = "Address size fault, zeroth level of translation or translation table base register";
58 break;
59 case 0x1: AbortCause = "Address size fault, first level";
60 break;
61 case 0x2: AbortCause = "Address size fault, second level";
62 break;
63 case 0x3: AbortCause = "Address size fault, third level";
64 break;
65 case 0x4: AbortCause = "Translation fault, zeroth level";
66 break;
67 case 0x5: AbortCause = "Translation fault, first level";
68 break;
69 case 0x6: AbortCause = "Translation fault, second level";
70 break;
71 case 0x7: AbortCause = "Translation fault, third level";
72 break;
73 case 0x9: AbortCause = "Access flag fault, first level";
74 break;
75 case 0xa: AbortCause = "Access flag fault, second level";
76 break;
77 case 0xb: AbortCause = "Access flag fault, third level";
78 break;
79 case 0xd: AbortCause = "Permission fault, first level";
80 break;
81 case 0xe: AbortCause = "Permission fault, second level";
82 break;
83 case 0xf: AbortCause = "Permission fault, third level";
84 break;
85 case 0x10: AbortCause = "Synchronous external abort";
86 break;
87 case 0x18: AbortCause = "Synchronous parity error on memory access";
88 break;
89 case 0x11: AbortCause = "Asynchronous external abort";
90 break;
91 case 0x19: AbortCause = "Asynchronous parity error on memory access";
92 break;
93 case 0x14: AbortCause = "Synchronous external abort on translation table walk, zeroth level";
94 break;
95 case 0x15: AbortCause = "Synchronous external abort on translation table walk, first level";
96 break;
97 case 0x16: AbortCause = "Synchronous external abort on translation table walk, second level";
98 break;
99 case 0x17: AbortCause = "Synchronous external abort on translation table walk, third level";
100 break;
101 case 0x1c: AbortCause = "Synchronous parity error on memory access on translation table walk, zeroth level";
102 break;
103 case 0x1d: AbortCause = "Synchronous parity error on memory access on translation table walk, first level";
104 break;
105 case 0x1e: AbortCause = "Synchronous parity error on memory access on translation table walk, second level";
106 break;
107 case 0x1f: AbortCause = "Synchronous parity error on memory access on translation table walk, third level";
108 break;
109 case 0x21: AbortCause = "Alignment fault";
110 break;
111 case 0x22: AbortCause = "Debug event";
112 break;
113 case 0x30: AbortCause = "TLB conflict abort";
114 break;
115 case 0x33:
116 case 0x34: AbortCause = "IMPLEMENTATION DEFINED";
117 break;
118 case 0x35:
119 case 0x36: AbortCause = "Domain fault";
120 break;
121 default: AbortCause = "";
122 break;
123 }
124
125 DEBUG ((DEBUG_ERROR, "\n%a: %a\n", AbortType, AbortCause));
126}
127
128STATIC
129VOID
130DescribeExceptionSyndrome (
131 IN UINT32 Esr
132 )
133{
134 CHAR8 *Message;
135 UINTN Ec;
136 UINTN Iss;
137
138 Ec = Esr >> 26;
139 Iss = Esr & 0x00ffffff;
140
141 switch (Ec) {
142 case 0x15: Message = "SVC executed in AArch64";
143 break;
144 case 0x20:
145 case 0x21: DescribeInstructionOrDataAbort ("Instruction abort", Iss);
146 return;
147 case 0x22: Message = "PC alignment fault";
148 break;
149 case 0x23: Message = "SP alignment fault";
150 break;
151 case 0x24:
152 case 0x25: DescribeInstructionOrDataAbort ("Data abort", Iss);
153 return;
154 default: return;
155 }
156
157 DEBUG ((DEBUG_ERROR, "\n %a \n", Message));
158}
159
160STATIC
161CONST CHAR8 *
162BaseName (
163 IN CONST CHAR8 *FullName
164 )
165{
166 CONST CHAR8 *Str;
167
168 Str = FullName + AsciiStrLen (FullName);
169
170 while (--Str > FullName) {
171 if ((*Str == '/') || (*Str == '\\')) {
172 return Str + 1;
173 }
174 }
175
176 return Str;
177}
178
188VOID
190 IN EFI_EXCEPTION_TYPE ExceptionType,
191 IN OUT EFI_SYSTEM_CONTEXT SystemContext
192 )
193{
194 CHAR8 Buffer[MAX_PRINT_CHARS];
195 CHAR16 UnicodeBuffer[MAX_PRINT_CHARS];
196 UINTN CharCount;
197 INT32 Offset;
198
199 if (mRecursiveException) {
200 STATIC CHAR8 CONST Message[] = "\nRecursive exception occurred while dumping the CPU state\n";
201 SerialPortWrite ((UINT8 *)Message, sizeof Message - 1);
202 CpuDeadLoop ();
203 }
204
205 mRecursiveException = TRUE;
206
207 CharCount = AsciiSPrint (Buffer, sizeof (Buffer), "\n\n%a Exception at 0x%016lx\n", gExceptionTypeString[ExceptionType], SystemContext.SystemContextAArch64->ELR);
208 SerialPortWrite ((UINT8 *)Buffer, CharCount);
209
210 // Prepare a unicode buffer for ConOut, if applicable, in case the buffer
211 // gets reused.
212 UnicodeSPrintAsciiFormat (UnicodeBuffer, MAX_PRINT_CHARS, Buffer);
213
215 CHAR8 *Pdb, *PrevPdb;
216 UINTN ImageBase;
217 UINTN PeCoffSizeOfHeader;
218 UINT64 *Fp;
219 UINT64 RootFp[2];
220 UINTN Idx;
221
222 PrevPdb = Pdb = GetImageName (SystemContext.SystemContextAArch64->ELR, &ImageBase, &PeCoffSizeOfHeader);
223 if (Pdb != NULL) {
224 DEBUG ((
225 DEBUG_ERROR,
226 "PC 0x%012lx (0x%012lx+0x%08x) [ 0] %a\n",
227 SystemContext.SystemContextAArch64->ELR,
228 ImageBase,
229 SystemContext.SystemContextAArch64->ELR - ImageBase,
230 BaseName (Pdb)
231 ));
232 } else {
233 DEBUG ((DEBUG_ERROR, "PC 0x%012lx\n", SystemContext.SystemContextAArch64->ELR));
234 }
235
236 if ((UINT64 *)SystemContext.SystemContextAArch64->FP != 0) {
237 Idx = 0;
238
239 RootFp[0] = ((UINT64 *)SystemContext.SystemContextAArch64->FP)[0];
240 RootFp[1] = ((UINT64 *)SystemContext.SystemContextAArch64->FP)[1];
241 if (RootFp[1] != SystemContext.SystemContextAArch64->LR) {
242 RootFp[0] = SystemContext.SystemContextAArch64->FP;
243 RootFp[1] = SystemContext.SystemContextAArch64->LR;
244 }
245
246 for (Fp = RootFp; Fp[0] != 0; Fp = (UINT64 *)Fp[0]) {
247 Pdb = GetImageName (Fp[1], &ImageBase, &PeCoffSizeOfHeader);
248 if (Pdb != NULL) {
249 if (Pdb != PrevPdb) {
250 Idx++;
251 PrevPdb = Pdb;
252 }
253
254 DEBUG ((
255 DEBUG_ERROR,
256 "PC 0x%012lx (0x%012lx+0x%08x) [% 2d] %a\n",
257 Fp[1],
258 ImageBase,
259 Fp[1] - ImageBase,
260 Idx,
261 BaseName (Pdb)
262 ));
263 } else {
264 DEBUG ((DEBUG_ERROR, "PC 0x%012lx\n", Fp[1]));
265 }
266 }
267
268 PrevPdb = Pdb = GetImageName (SystemContext.SystemContextAArch64->ELR, &ImageBase, &PeCoffSizeOfHeader);
269 if (Pdb != NULL) {
270 DEBUG ((DEBUG_ERROR, "\n[ 0] %a\n", Pdb));
271 }
272
273 Idx = 0;
274 for (Fp = RootFp; Fp[0] != 0; Fp = (UINT64 *)Fp[0]) {
275 Pdb = GetImageName (Fp[1], &ImageBase, &PeCoffSizeOfHeader);
276 if ((Pdb != NULL) && (Pdb != PrevPdb)) {
277 DEBUG ((DEBUG_ERROR, "[% 2d] %a\n", ++Idx, Pdb));
278 PrevPdb = Pdb;
279 }
280 }
281 }
282
284
285 DEBUG ((DEBUG_ERROR, "\n X0 0x%016lx X1 0x%016lx X2 0x%016lx X3 0x%016lx\n", SystemContext.SystemContextAArch64->X0, SystemContext.SystemContextAArch64->X1, SystemContext.SystemContextAArch64->X2, SystemContext.SystemContextAArch64->X3));
286 DEBUG ((DEBUG_ERROR, " X4 0x%016lx X5 0x%016lx X6 0x%016lx X7 0x%016lx\n", SystemContext.SystemContextAArch64->X4, SystemContext.SystemContextAArch64->X5, SystemContext.SystemContextAArch64->X6, SystemContext.SystemContextAArch64->X7));
287 DEBUG ((DEBUG_ERROR, " X8 0x%016lx X9 0x%016lx X10 0x%016lx X11 0x%016lx\n", SystemContext.SystemContextAArch64->X8, SystemContext.SystemContextAArch64->X9, SystemContext.SystemContextAArch64->X10, SystemContext.SystemContextAArch64->X11));
288 DEBUG ((DEBUG_ERROR, " X12 0x%016lx X13 0x%016lx X14 0x%016lx X15 0x%016lx\n", SystemContext.SystemContextAArch64->X12, SystemContext.SystemContextAArch64->X13, SystemContext.SystemContextAArch64->X14, SystemContext.SystemContextAArch64->X15));
289 DEBUG ((DEBUG_ERROR, " X16 0x%016lx X17 0x%016lx X18 0x%016lx X19 0x%016lx\n", SystemContext.SystemContextAArch64->X16, SystemContext.SystemContextAArch64->X17, SystemContext.SystemContextAArch64->X18, SystemContext.SystemContextAArch64->X19));
290 DEBUG ((DEBUG_ERROR, " X20 0x%016lx X21 0x%016lx X22 0x%016lx X23 0x%016lx\n", SystemContext.SystemContextAArch64->X20, SystemContext.SystemContextAArch64->X21, SystemContext.SystemContextAArch64->X22, SystemContext.SystemContextAArch64->X23));
291 DEBUG ((DEBUG_ERROR, " X24 0x%016lx X25 0x%016lx X26 0x%016lx X27 0x%016lx\n", SystemContext.SystemContextAArch64->X24, SystemContext.SystemContextAArch64->X25, SystemContext.SystemContextAArch64->X26, SystemContext.SystemContextAArch64->X27));
292 DEBUG ((DEBUG_ERROR, " X28 0x%016lx FP 0x%016lx LR 0x%016lx \n", SystemContext.SystemContextAArch64->X28, SystemContext.SystemContextAArch64->FP, SystemContext.SystemContextAArch64->LR));
293
294 /* We save these as 128bit numbers, but have to print them as two 64bit numbers,
295 so swap the 64bit words to correctly represent a 128bit number. */
296 DEBUG ((DEBUG_ERROR, "\n V0 0x%016lx %016lx V1 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V0[1], SystemContext.SystemContextAArch64->V0[0], SystemContext.SystemContextAArch64->V1[1], SystemContext.SystemContextAArch64->V1[0]));
297 DEBUG ((DEBUG_ERROR, " V2 0x%016lx %016lx V3 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V2[1], SystemContext.SystemContextAArch64->V2[0], SystemContext.SystemContextAArch64->V3[1], SystemContext.SystemContextAArch64->V3[0]));
298 DEBUG ((DEBUG_ERROR, " V4 0x%016lx %016lx V5 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V4[1], SystemContext.SystemContextAArch64->V4[0], SystemContext.SystemContextAArch64->V5[1], SystemContext.SystemContextAArch64->V5[0]));
299 DEBUG ((DEBUG_ERROR, " V6 0x%016lx %016lx V7 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V6[1], SystemContext.SystemContextAArch64->V6[0], SystemContext.SystemContextAArch64->V7[1], SystemContext.SystemContextAArch64->V7[0]));
300 DEBUG ((DEBUG_ERROR, " V8 0x%016lx %016lx V9 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V8[1], SystemContext.SystemContextAArch64->V8[0], SystemContext.SystemContextAArch64->V9[1], SystemContext.SystemContextAArch64->V9[0]));
301 DEBUG ((DEBUG_ERROR, " V10 0x%016lx %016lx V11 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V10[1], SystemContext.SystemContextAArch64->V10[0], SystemContext.SystemContextAArch64->V11[1], SystemContext.SystemContextAArch64->V11[0]));
302 DEBUG ((DEBUG_ERROR, " V12 0x%016lx %016lx V13 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V12[1], SystemContext.SystemContextAArch64->V12[0], SystemContext.SystemContextAArch64->V13[1], SystemContext.SystemContextAArch64->V13[0]));
303 DEBUG ((DEBUG_ERROR, " V14 0x%016lx %016lx V15 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V14[1], SystemContext.SystemContextAArch64->V14[0], SystemContext.SystemContextAArch64->V15[1], SystemContext.SystemContextAArch64->V15[0]));
304 DEBUG ((DEBUG_ERROR, " V16 0x%016lx %016lx V17 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V16[1], SystemContext.SystemContextAArch64->V16[0], SystemContext.SystemContextAArch64->V17[1], SystemContext.SystemContextAArch64->V17[0]));
305 DEBUG ((DEBUG_ERROR, " V18 0x%016lx %016lx V19 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V18[1], SystemContext.SystemContextAArch64->V18[0], SystemContext.SystemContextAArch64->V19[1], SystemContext.SystemContextAArch64->V19[0]));
306 DEBUG ((DEBUG_ERROR, " V20 0x%016lx %016lx V21 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V20[1], SystemContext.SystemContextAArch64->V20[0], SystemContext.SystemContextAArch64->V21[1], SystemContext.SystemContextAArch64->V21[0]));
307 DEBUG ((DEBUG_ERROR, " V22 0x%016lx %016lx V23 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V22[1], SystemContext.SystemContextAArch64->V22[0], SystemContext.SystemContextAArch64->V23[1], SystemContext.SystemContextAArch64->V23[0]));
308 DEBUG ((DEBUG_ERROR, " V24 0x%016lx %016lx V25 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V24[1], SystemContext.SystemContextAArch64->V24[0], SystemContext.SystemContextAArch64->V25[1], SystemContext.SystemContextAArch64->V25[0]));
309 DEBUG ((DEBUG_ERROR, " V26 0x%016lx %016lx V27 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V26[1], SystemContext.SystemContextAArch64->V26[0], SystemContext.SystemContextAArch64->V27[1], SystemContext.SystemContextAArch64->V27[0]));
310 DEBUG ((DEBUG_ERROR, " V28 0x%016lx %016lx V29 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V28[1], SystemContext.SystemContextAArch64->V28[0], SystemContext.SystemContextAArch64->V29[1], SystemContext.SystemContextAArch64->V29[0]));
311 DEBUG ((DEBUG_ERROR, " V30 0x%016lx %016lx V31 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V30[1], SystemContext.SystemContextAArch64->V30[0], SystemContext.SystemContextAArch64->V31[1], SystemContext.SystemContextAArch64->V31[0]));
312
313 DEBUG ((DEBUG_ERROR, "\n SP 0x%016lx ELR 0x%016lx SPSR 0x%08lx FPSR 0x%08lx\n ESR 0x%08lx FAR 0x%016lx\n", SystemContext.SystemContextAArch64->SP, SystemContext.SystemContextAArch64->ELR, SystemContext.SystemContextAArch64->SPSR, SystemContext.SystemContextAArch64->FPSR, SystemContext.SystemContextAArch64->ESR, SystemContext.SystemContextAArch64->FAR));
314
315 DEBUG ((DEBUG_ERROR, "\n ESR : EC 0x%02x IL 0x%x ISS 0x%08x\n", (SystemContext.SystemContextAArch64->ESR & 0xFC000000) >> 26, (SystemContext.SystemContextAArch64->ESR >> 25) & 0x1, SystemContext.SystemContextAArch64->ESR & 0x1FFFFFF));
316
317 DescribeExceptionSyndrome (SystemContext.SystemContextAArch64->ESR);
318
319 DEBUG ((DEBUG_ERROR, "\nStack dump:\n"));
320 for (Offset = -256; Offset < 256; Offset += 32) {
321 DEBUG ((
322 DEBUG_ERROR,
323 "%c %013lx: %016lx %016lx %016lx %016lx\n",
324 Offset == 0 ? '>' : ' ',
325 SystemContext.SystemContextAArch64->SP + Offset,
326 *(UINT64 *)(SystemContext.SystemContextAArch64->SP + Offset),
327 *(UINT64 *)(SystemContext.SystemContextAArch64->SP + Offset + 8),
328 *(UINT64 *)(SystemContext.SystemContextAArch64->SP + Offset + 16),
329 *(UINT64 *)(SystemContext.SystemContextAArch64->SP + Offset + 24)
330 ));
331 }
332
333 // Attempt to print that we had a synchronous exception to ConOut. We do
334 // this after the serial logging as ConOut's logging is more complex and we
335 // aren't guaranteed to succeed.
336 if (gST->ConOut != NULL) {
337 gST->ConOut->OutputString (gST->ConOut, UnicodeBuffer);
338 }
339
340 ASSERT (FALSE);
341 CpuDeadLoop ();
342}
CHAR8 * GetImageName(IN UINTN FaultAddress, OUT UINTN *ImageBase, OUT UINTN *PeCoffSizeOfHeaders)
VOID DefaultExceptionHandler(IN EFI_EXCEPTION_TYPE ExceptionType, IN OUT EFI_SYSTEM_CONTEXT SystemContext)
UINT64 UINTN
UINTN EFIAPI SerialPortWrite(IN UINT8 *Buffer, IN UINTN NumberOfBytes)
Definition: SerialPortLib.c:52
UINTN EFIAPI AsciiStrLen(IN CONST CHAR8 *String)
Definition: String.c:641
VOID EFIAPI CpuDeadLoop(VOID)
Definition: CpuDeadLoop.c:25
UINTN EFIAPI UnicodeSPrintAsciiFormat(OUT CHAR16 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR8 *FormatString,...)
Definition: PrintLib.c:583
UINTN EFIAPI AsciiSPrint(OUT CHAR8 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR8 *FormatString,...)
Definition: PrintLib.c:813
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define DEBUG_CODE_BEGIN()
Definition: DebugLib.h:564
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define DEBUG_CODE_END()
Definition: DebugLib.h:578
INTN EFI_EXCEPTION_TYPE
Definition: DebugSupport.h:35
EFI_SYSTEM_TABLE * gST
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * ConOut
Definition: UefiSpec.h:2064