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