]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/DefaultExceptionHandlerLib/Arm/DefaultExceptionHandler.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / ArmPkg / Library / DefaultExceptionHandlerLib / Arm / DefaultExceptionHandler.c
1 /** @file
2 Default exception handler
3
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
5 Copyright (c) 2012 - 2021, 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/BaseLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/PeCoffGetEntryPointLib.h>
15 #include <Library/PrintLib.h>
16 #include <Library/ArmDisassemblerLib.h>
17 #include <Library/SerialPortLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/UefiLib.h>
20
21 #include <Guid/DebugImageInfoTable.h>
22
23 #include <Protocol/DebugSupport.h>
24 #include <Library/DefaultExceptionHandlerLib.h>
25
26 //
27 // The number of elements in a CHAR8 array, including the terminating NUL, that
28 // is meant to hold the string rendering of the CPSR.
29 //
30 #define CPSR_STRING_SIZE 32
31
32 typedef struct {
33 UINT32 BIT;
34 CHAR8 Char;
35 } CPSR_CHAR;
36
37 STATIC CONST CPSR_CHAR mCpsrChar[] = {
38 { 31, 'n' },
39 { 30, 'z' },
40 { 29, 'c' },
41 { 28, 'v' },
42
43 { 9, 'e' },
44 { 8, 'a' },
45 { 7, 'i' },
46 { 6, 'f' },
47 { 5, 't' },
48 { 0, '?' }
49 };
50
51 CHAR8 *
52 GetImageName (
53 IN UINTN FaultAddress,
54 OUT UINTN *ImageBase,
55 OUT UINTN *PeCoffSizeOfHeaders
56 );
57
58 /**
59 Convert the Current Program Status Register (CPSR) to a string. The string is
60 a defacto standard in the ARM world.
61
62 It is possible to add extra bits by adding them to mCpsrChar array.
63
64 @param Cpsr ARM CPSR register value
65 @param ReturnStr CPSR_STRING_SIZE byte string that contains string
66 version of CPSR
67
68 **/
69 VOID
70 CpsrString (
71 IN UINT32 Cpsr,
72 OUT CHAR8 *ReturnStr
73 )
74 {
75 UINTN Index;
76 CHAR8 *Str;
77 CHAR8 *ModeStr;
78
79 Str = ReturnStr;
80
81 for (Index = 0; mCpsrChar[Index].BIT != 0; Index++, Str++) {
82 *Str = mCpsrChar[Index].Char;
83 if ((Cpsr & (1 << mCpsrChar[Index].BIT)) != 0) {
84 // Concert to upper case if bit is set
85 *Str &= ~0x20;
86 }
87 }
88
89 *Str++ = '_';
90 *Str = '\0';
91
92 switch (Cpsr & 0x1f) {
93 case 0x10:
94 ModeStr = "usr";
95 break;
96 case 0x011:
97 ModeStr = "fiq";
98 break;
99 case 0x12:
100 ModeStr = "irq";
101 break;
102 case 0x13:
103 ModeStr = "svc";
104 break;
105 case 0x16:
106 ModeStr = "mon";
107 break;
108 case 0x17:
109 ModeStr = "abt";
110 break;
111 case 0x1b:
112 ModeStr = "und";
113 break;
114 case 0x1f:
115 ModeStr = "sys";
116 break;
117
118 default:
119 ModeStr = "???";
120 break;
121 }
122
123 //
124 // See the interface contract in the leading comment block.
125 //
126 AsciiStrCatS (Str, CPSR_STRING_SIZE - (Str - ReturnStr), ModeStr);
127 }
128
129 CHAR8 *
130 FaultStatusToString (
131 IN UINT32 Status
132 )
133 {
134 CHAR8 *FaultSource;
135
136 switch (Status) {
137 case 0x01: FaultSource = "Alignment fault";
138 break;
139 case 0x02: FaultSource = "Debug event fault";
140 break;
141 case 0x03: FaultSource = "Access Flag fault on Section";
142 break;
143 case 0x04: FaultSource = "Cache maintenance operation fault[2]";
144 break;
145 case 0x05: FaultSource = "Translation fault on Section";
146 break;
147 case 0x06: FaultSource = "Access Flag fault on Page";
148 break;
149 case 0x07: FaultSource = "Translation fault on Page";
150 break;
151 case 0x08: FaultSource = "Precise External Abort";
152 break;
153 case 0x09: FaultSource = "Domain fault on Section";
154 break;
155 case 0x0b: FaultSource = "Domain fault on Page";
156 break;
157 case 0x0c: FaultSource = "External abort on translation, first level";
158 break;
159 case 0x0d: FaultSource = "Permission fault on Section";
160 break;
161 case 0x0e: FaultSource = "External abort on translation, second level";
162 break;
163 case 0x0f: FaultSource = "Permission fault on Page";
164 break;
165 case 0x16: FaultSource = "Imprecise External Abort";
166 break;
167 default: FaultSource = "No function";
168 break;
169 }
170
171 return FaultSource;
172 }
173
174 STATIC CHAR8 *gExceptionTypeString[] = {
175 "Reset",
176 "Undefined OpCode",
177 "SVC",
178 "Prefetch Abort",
179 "Data Abort",
180 "Undefined",
181 "IRQ",
182 "FIQ"
183 };
184
185 /**
186 This is the default action to take on an unexpected exception
187
188 Since this is exception context don't do anything crazy like try to allocate memory.
189
190 @param ExceptionType Type of the exception
191 @param SystemContext Register state at the time of the Exception
192
193
194 **/
195 VOID
196 DefaultExceptionHandler (
197 IN EFI_EXCEPTION_TYPE ExceptionType,
198 IN OUT EFI_SYSTEM_CONTEXT SystemContext
199 )
200 {
201 CHAR8 Buffer[100];
202 UINTN CharCount;
203 UINT32 DfsrStatus;
204 UINT32 IfsrStatus;
205 BOOLEAN DfsrWrite;
206 UINT32 PcAdjust;
207
208 PcAdjust = 0;
209
210 CharCount = AsciiSPrint (
211 Buffer,
212 sizeof (Buffer),
213 "\n%a Exception PC at 0x%08x CPSR 0x%08x ",
214 gExceptionTypeString[ExceptionType],
215 SystemContext.SystemContextArm->PC,
216 SystemContext.SystemContextArm->CPSR
217 );
218 SerialPortWrite ((UINT8 *)Buffer, CharCount);
219 if (gST->ConOut != NULL) {
220 AsciiPrint (Buffer);
221 }
222
223 DEBUG_CODE_BEGIN ();
224 CHAR8 *Pdb;
225 UINT32 ImageBase;
226 UINT32 PeCoffSizeOfHeader;
227 UINT32 Offset;
228 CHAR8 CpsrStr[CPSR_STRING_SIZE]; // char per bit. Lower 5-bits are mode
229 // that is a 3 char string
230 CHAR8 Buffer[80];
231 UINT8 *DisAsm;
232 UINT32 ItBlock;
233
234 CpsrString (SystemContext.SystemContextArm->CPSR, CpsrStr);
235 DEBUG ((DEBUG_ERROR, "%a\n", CpsrStr));
236
237 Pdb = GetImageName (SystemContext.SystemContextArm->PC, &ImageBase, &PeCoffSizeOfHeader);
238 Offset = SystemContext.SystemContextArm->PC - ImageBase;
239 if (Pdb != NULL) {
240 DEBUG ((DEBUG_ERROR, "%a\n", Pdb));
241
242 //
243 // A PE/COFF image loads its headers into memory so the headers are
244 // included in the linked addresses. ELF and Mach-O images do not
245 // include the headers so the first byte of the image is usually
246 // text (code). If you look at link maps from ELF or Mach-O images
247 // you need to subtract out the size of the PE/COFF header to get
248 // get the offset that matches the link map.
249 //
250 DEBUG ((DEBUG_ERROR, "loaded at 0x%08x (PE/COFF offset) 0x%x (ELF or Mach-O offset) 0x%x", ImageBase, Offset, Offset - PeCoffSizeOfHeader));
251
252 // If we come from an image it is safe to show the instruction. We know it should not fault
253 DisAsm = (UINT8 *)(UINTN)SystemContext.SystemContextArm->PC;
254 ItBlock = 0;
255 DisassembleInstruction (&DisAsm, (SystemContext.SystemContextArm->CPSR & BIT5) == BIT5, TRUE, &ItBlock, Buffer, sizeof (Buffer));
256 DEBUG ((DEBUG_ERROR, "\n%a", Buffer));
257
258 switch (ExceptionType) {
259 case EXCEPT_ARM_UNDEFINED_INSTRUCTION:
260 case EXCEPT_ARM_SOFTWARE_INTERRUPT:
261 case EXCEPT_ARM_PREFETCH_ABORT:
262 case EXCEPT_ARM_DATA_ABORT:
263 // advance PC past the faulting instruction
264 PcAdjust = (UINTN)DisAsm - SystemContext.SystemContextArm->PC;
265 break;
266
267 default:
268 break;
269 }
270 }
271
272 DEBUG_CODE_END ();
273 DEBUG ((DEBUG_ERROR, "\n R0 0x%08x R1 0x%08x R2 0x%08x R3 0x%08x\n", SystemContext.SystemContextArm->R0, SystemContext.SystemContextArm->R1, SystemContext.SystemContextArm->R2, SystemContext.SystemContextArm->R3));
274 DEBUG ((DEBUG_ERROR, " R4 0x%08x R5 0x%08x R6 0x%08x R7 0x%08x\n", SystemContext.SystemContextArm->R4, SystemContext.SystemContextArm->R5, SystemContext.SystemContextArm->R6, SystemContext.SystemContextArm->R7));
275 DEBUG ((DEBUG_ERROR, " R8 0x%08x R9 0x%08x R10 0x%08x R11 0x%08x\n", SystemContext.SystemContextArm->R8, SystemContext.SystemContextArm->R9, SystemContext.SystemContextArm->R10, SystemContext.SystemContextArm->R11));
276 DEBUG ((DEBUG_ERROR, " R12 0x%08x SP 0x%08x LR 0x%08x PC 0x%08x\n", SystemContext.SystemContextArm->R12, SystemContext.SystemContextArm->SP, SystemContext.SystemContextArm->LR, SystemContext.SystemContextArm->PC));
277 DEBUG ((DEBUG_ERROR, "DFSR 0x%08x DFAR 0x%08x IFSR 0x%08x IFAR 0x%08x\n", SystemContext.SystemContextArm->DFSR, SystemContext.SystemContextArm->DFAR, SystemContext.SystemContextArm->IFSR, SystemContext.SystemContextArm->IFAR));
278
279 // Bit10 is Status[4] Bit3:0 is Status[3:0]
280 DfsrStatus = (SystemContext.SystemContextArm->DFSR & 0xf) | ((SystemContext.SystemContextArm->DFSR >> 6) & 0x10);
281 DfsrWrite = (SystemContext.SystemContextArm->DFSR & BIT11) != 0;
282 if (DfsrStatus != 0x00) {
283 DEBUG ((DEBUG_ERROR, " %a: %a 0x%08x\n", FaultStatusToString (DfsrStatus), DfsrWrite ? "write to" : "read from", SystemContext.SystemContextArm->DFAR));
284 }
285
286 IfsrStatus = (SystemContext.SystemContextArm->IFSR & 0xf) | ((SystemContext.SystemContextArm->IFSR >> 6) & 0x10);
287 if (IfsrStatus != 0) {
288 DEBUG ((DEBUG_ERROR, " Instruction %a at 0x%08x\n", FaultStatusToString (SystemContext.SystemContextArm->IFSR & 0xf), SystemContext.SystemContextArm->IFAR));
289 }
290
291 DEBUG ((DEBUG_ERROR, "\n"));
292 ASSERT (FALSE);
293
294 CpuDeadLoop (); // may return if executing under a debugger
295
296 // Clear the error registers that we have already displayed incase some one wants to keep going
297 SystemContext.SystemContextArm->DFSR = 0;
298 SystemContext.SystemContextArm->IFSR = 0;
299
300 // If some one is stepping past the exception handler adjust the PC to point to the next instruction
301 SystemContext.SystemContextArm->PC += PcAdjust;
302 }