]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/DefaultExceptionHandlerLib/AArch64/DefaultExceptionHandler.c
2bb0888b43a03c13e47da50442c548cd54919351
[mirror_edk2.git] / ArmPkg / Library / DefaultExceptionHandlerLib / AArch64 / DefaultExceptionHandler.c
1 /** @file
2 Default exception handler
3
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
5 Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include <Uefi.h>
12 #include <Library/UefiLib.h>
13 #include <Library/BaseLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/PeCoffGetEntryPointLib.h>
16 #include <Library/PrintLib.h>
17 #include <Library/ArmDisassemblerLib.h>
18 #include <Library/SerialPortLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20
21 #include <Guid/DebugImageInfoTable.h>
22 #include <Protocol/DebugSupport.h>
23 #include <Protocol/LoadedImage.h>
24
25 STATIC CHAR8 *gExceptionTypeString[] = {
26 "Synchronous",
27 "IRQ",
28 "FIQ",
29 "SError"
30 };
31
32 STATIC BOOLEAN mRecursiveException;
33
34 CHAR8 *
35 GetImageName (
36 IN UINTN FaultAddress,
37 OUT UINTN *ImageBase,
38 OUT UINTN *PeCoffSizeOfHeaders
39 );
40
41 STATIC
42 VOID
43 DescribeInstructionOrDataAbort (
44 IN CHAR8 *AbortType,
45 IN UINTN Iss
46 )
47 {
48 CHAR8 *AbortCause;
49
50 switch (Iss & 0x3f) {
51 case 0x0: AbortCause = "Address size fault, zeroth level of translation or translation table base register"; break;
52 case 0x1: AbortCause = "Address size fault, first level"; break;
53 case 0x2: AbortCause = "Address size fault, second level"; break;
54 case 0x3: AbortCause = "Address size fault, third level"; break;
55 case 0x4: AbortCause = "Translation fault, zeroth level"; break;
56 case 0x5: AbortCause = "Translation fault, first level"; break;
57 case 0x6: AbortCause = "Translation fault, second level"; break;
58 case 0x7: AbortCause = "Translation fault, third level"; break;
59 case 0x9: AbortCause = "Access flag fault, first level"; break;
60 case 0xa: AbortCause = "Access flag fault, second level"; break;
61 case 0xb: AbortCause = "Access flag fault, third level"; break;
62 case 0xd: AbortCause = "Permission fault, first level"; break;
63 case 0xe: AbortCause = "Permission fault, second level"; break;
64 case 0xf: AbortCause = "Permission fault, third level"; break;
65 case 0x10: AbortCause = "Synchronous external abort"; break;
66 case 0x18: AbortCause = "Synchronous parity error on memory access"; break;
67 case 0x11: AbortCause = "Asynchronous external abort"; break;
68 case 0x19: AbortCause = "Asynchronous parity error on memory access"; break;
69 case 0x14: AbortCause = "Synchronous external abort on translation table walk, zeroth level"; break;
70 case 0x15: AbortCause = "Synchronous external abort on translation table walk, first level"; break;
71 case 0x16: AbortCause = "Synchronous external abort on translation table walk, second level"; break;
72 case 0x17: AbortCause = "Synchronous external abort on translation table walk, third level"; break;
73 case 0x1c: AbortCause = "Synchronous parity error on memory access on translation table walk, zeroth level"; break;
74 case 0x1d: AbortCause = "Synchronous parity error on memory access on translation table walk, first level"; break;
75 case 0x1e: AbortCause = "Synchronous parity error on memory access on translation table walk, second level"; break;
76 case 0x1f: AbortCause = "Synchronous parity error on memory access on translation table walk, third level"; break;
77 case 0x21: AbortCause = "Alignment fault"; break;
78 case 0x22: AbortCause = "Debug event"; break;
79 case 0x30: AbortCause = "TLB conflict abort"; break;
80 case 0x33:
81 case 0x34: AbortCause = "IMPLEMENTATION DEFINED"; break;
82 case 0x35:
83 case 0x36: AbortCause = "Domain fault"; break;
84 default: AbortCause = ""; break;
85 }
86
87 DEBUG ((DEBUG_ERROR, "\n%a: %a\n", AbortType, AbortCause));
88 }
89
90 STATIC
91 VOID
92 DescribeExceptionSyndrome (
93 IN UINT32 Esr
94 )
95 {
96 CHAR8 *Message;
97 UINTN Ec;
98 UINTN Iss;
99
100 Ec = Esr >> 26;
101 Iss = Esr & 0x00ffffff;
102
103 switch (Ec) {
104 case 0x15: Message = "SVC executed in AArch64"; break;
105 case 0x20:
106 case 0x21: DescribeInstructionOrDataAbort ("Instruction abort", Iss); return;
107 case 0x22: Message = "PC alignment fault"; break;
108 case 0x23: Message = "SP alignment fault"; break;
109 case 0x24:
110 case 0x25: DescribeInstructionOrDataAbort ("Data abort", Iss); return;
111 default: return;
112 }
113
114 DEBUG ((DEBUG_ERROR, "\n %a \n", Message));
115 }
116
117 #ifndef MDEPKG_NDEBUG
118 STATIC
119 CONST CHAR8 *
120 BaseName (
121 IN CONST CHAR8 *FullName
122 )
123 {
124 CONST CHAR8 *Str;
125
126 Str = FullName + AsciiStrLen (FullName);
127
128 while (--Str > FullName) {
129 if (*Str == '/' || *Str == '\\') {
130 return Str + 1;
131 }
132 }
133 return Str;
134 }
135 #endif
136
137 /**
138 This is the default action to take on an unexpected exception
139
140 Since this is exception context don't do anything crazy like try to allocate memory.
141
142 @param ExceptionType Type of the exception
143 @param SystemContext Register state at the time of the Exception
144
145 **/
146 VOID
147 DefaultExceptionHandler (
148 IN EFI_EXCEPTION_TYPE ExceptionType,
149 IN OUT EFI_SYSTEM_CONTEXT SystemContext
150 )
151 {
152 CHAR8 Buffer[100];
153 UINTN CharCount;
154 INT32 Offset;
155
156 if (mRecursiveException) {
157 STATIC CHAR8 CONST Message[] = "\nRecursive exception occurred while dumping the CPU state\n";
158
159 SerialPortWrite ((UINT8 *)Message, sizeof Message - 1);
160 if (gST->ConOut != NULL) {
161 AsciiPrint (Message);
162 }
163 CpuDeadLoop ();
164 }
165 mRecursiveException = TRUE;
166
167 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"\n\n%a Exception at 0x%016lx\n", gExceptionTypeString[ExceptionType], SystemContext.SystemContextAArch64->ELR);
168 SerialPortWrite ((UINT8 *) Buffer, CharCount);
169 if (gST->ConOut != NULL) {
170 AsciiPrint (Buffer);
171 }
172
173 DEBUG_CODE_BEGIN ();
174 CHAR8 *Pdb, *PrevPdb;
175 UINTN ImageBase;
176 UINTN PeCoffSizeOfHeader;
177 UINT64 *Fp;
178 UINT64 RootFp[2];
179 UINTN Idx;
180
181 PrevPdb = Pdb = GetImageName (SystemContext.SystemContextAArch64->ELR, &ImageBase, &PeCoffSizeOfHeader);
182 if (Pdb != NULL) {
183 DEBUG ((DEBUG_ERROR, "PC 0x%012lx (0x%012lx+0x%08x) [ 0] %a\n",
184 SystemContext.SystemContextAArch64->ELR, ImageBase,
185 SystemContext.SystemContextAArch64->ELR - ImageBase, BaseName (Pdb)));
186 } else {
187 DEBUG ((DEBUG_ERROR, "PC 0x%012lx\n", SystemContext.SystemContextAArch64->ELR));
188 }
189
190 if ((UINT64 *)SystemContext.SystemContextAArch64->FP != 0) {
191 Idx = 0;
192
193 RootFp[0] = ((UINT64 *)SystemContext.SystemContextAArch64->FP)[0];
194 RootFp[1] = ((UINT64 *)SystemContext.SystemContextAArch64->FP)[1];
195 if (RootFp[1] != SystemContext.SystemContextAArch64->LR) {
196 RootFp[0] = SystemContext.SystemContextAArch64->FP;
197 RootFp[1] = SystemContext.SystemContextAArch64->LR;
198 }
199 for (Fp = RootFp; Fp[0] != 0; Fp = (UINT64 *)Fp[0]) {
200 Pdb = GetImageName (Fp[1], &ImageBase, &PeCoffSizeOfHeader);
201 if (Pdb != NULL) {
202 if (Pdb != PrevPdb) {
203 Idx++;
204 PrevPdb = Pdb;
205 }
206 DEBUG ((DEBUG_ERROR, "PC 0x%012lx (0x%012lx+0x%08x) [% 2d] %a\n",
207 Fp[1], ImageBase, Fp[1] - ImageBase, Idx, BaseName (Pdb)));
208 } else {
209 DEBUG ((DEBUG_ERROR, "PC 0x%012lx\n", Fp[1]));
210 }
211 }
212 PrevPdb = Pdb = GetImageName (SystemContext.SystemContextAArch64->ELR, &ImageBase, &PeCoffSizeOfHeader);
213 if (Pdb != NULL) {
214 DEBUG ((DEBUG_ERROR, "\n[ 0] %a\n", Pdb));
215 }
216
217 Idx = 0;
218 for (Fp = RootFp; Fp[0] != 0; Fp = (UINT64 *)Fp[0]) {
219 Pdb = GetImageName (Fp[1], &ImageBase, &PeCoffSizeOfHeader);
220 if (Pdb != NULL && Pdb != PrevPdb) {
221 DEBUG ((DEBUG_ERROR, "[% 2d] %a\n", ++Idx, Pdb));
222 PrevPdb = Pdb;
223 }
224 }
225 }
226 DEBUG_CODE_END ();
227
228 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));
229 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));
230 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));
231 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));
232 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));
233 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));
234 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));
235 DEBUG ((DEBUG_ERROR, " X28 0x%016lx FP 0x%016lx LR 0x%016lx \n", SystemContext.SystemContextAArch64->X28, SystemContext.SystemContextAArch64->FP, SystemContext.SystemContextAArch64->LR));
236
237 /* We save these as 128bit numbers, but have to print them as two 64bit numbers,
238 so swap the 64bit words to correctly represent a 128bit number. */
239 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]));
240 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]));
241 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]));
242 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]));
243 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]));
244 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]));
245 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]));
246 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]));
247 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]));
248 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]));
249 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]));
250 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]));
251 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]));
252 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]));
253 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]));
254 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]));
255
256 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));
257
258 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 ));
259
260 DescribeExceptionSyndrome (SystemContext.SystemContextAArch64->ESR);
261
262 DEBUG ((DEBUG_ERROR, "\nStack dump:\n"));
263 for (Offset = -256; Offset < 256; Offset += 32) {
264 DEBUG ((DEBUG_ERROR, "%c %013lx: %016lx %016lx %016lx %016lx\n",
265 Offset == 0 ? '>' : ' ',
266 SystemContext.SystemContextAArch64->SP + Offset,
267 *(UINT64 *)(SystemContext.SystemContextAArch64->SP + Offset),
268 *(UINT64 *)(SystemContext.SystemContextAArch64->SP + Offset + 8),
269 *(UINT64 *)(SystemContext.SystemContextAArch64->SP + Offset + 16),
270 *(UINT64 *)(SystemContext.SystemContextAArch64->SP + Offset + 24)));
271 }
272
273 ASSERT (FALSE);
274 CpuDeadLoop ();
275 }