TianoCore EDK2 master
Loading...
Searching...
No Matches
DefaultExceptionHandler.c
Go to the documentation of this file.
1
11#include <Uefi.h>
12#include <Library/BaseLib.h>
13#include <Library/DebugLib.h>
15#include <Library/PrintLib.h>
19#include <Library/UefiLib.h>
20
22
25
26//
27// Maximum number of characters to print to serial (UINT8s) and to console if
28// available (as UINT16s)
29//
30#define MAX_PRINT_CHARS 100
31
32//
33// The number of elements in a CHAR8 array, including the terminating NUL, that
34// is meant to hold the string rendering of the CPSR.
35//
36#define CPSR_STRING_SIZE 32
37
38typedef struct {
39 UINT32 BIT;
40 CHAR8 Char;
41} CPSR_CHAR;
42
43STATIC CONST CPSR_CHAR mCpsrChar[] = {
44 { 31, 'n' },
45 { 30, 'z' },
46 { 29, 'c' },
47 { 28, 'v' },
48
49 { 9, 'e' },
50 { 8, 'a' },
51 { 7, 'i' },
52 { 6, 'f' },
53 { 5, 't' },
54 { 0, '?' }
55};
56
57CHAR8 *
59 IN UINTN FaultAddress,
60 OUT UINTN *ImageBase,
61 OUT UINTN *PeCoffSizeOfHeaders
62 );
63
75VOID
77 IN UINT32 Cpsr,
78 OUT CHAR8 *ReturnStr
79 )
80{
81 UINTN Index;
82 CHAR8 *Str;
83 CHAR8 *ModeStr;
84
85 Str = ReturnStr;
86
87 for (Index = 0; mCpsrChar[Index].BIT != 0; Index++, Str++) {
88 *Str = mCpsrChar[Index].Char;
89 if ((Cpsr & (1 << mCpsrChar[Index].BIT)) != 0) {
90 // Concert to upper case if bit is set
91 *Str &= ~0x20;
92 }
93 }
94
95 *Str++ = '_';
96 *Str = '\0';
97
98 switch (Cpsr & 0x1f) {
99 case 0x10:
100 ModeStr = "usr";
101 break;
102 case 0x011:
103 ModeStr = "fiq";
104 break;
105 case 0x12:
106 ModeStr = "irq";
107 break;
108 case 0x13:
109 ModeStr = "svc";
110 break;
111 case 0x16:
112 ModeStr = "mon";
113 break;
114 case 0x17:
115 ModeStr = "abt";
116 break;
117 case 0x1b:
118 ModeStr = "und";
119 break;
120 case 0x1f:
121 ModeStr = "sys";
122 break;
123
124 default:
125 ModeStr = "???";
126 break;
127 }
128
129 //
130 // See the interface contract in the leading comment block.
131 //
132 AsciiStrCatS (Str, CPSR_STRING_SIZE - (Str - ReturnStr), ModeStr);
133}
134
135CHAR8 *
136FaultStatusToString (
137 IN UINT32 Status
138 )
139{
140 CHAR8 *FaultSource;
141
142 switch (Status) {
143 case 0x01: FaultSource = "Alignment fault";
144 break;
145 case 0x02: FaultSource = "Debug event fault";
146 break;
147 case 0x03: FaultSource = "Access Flag fault on Section";
148 break;
149 case 0x04: FaultSource = "Cache maintenance operation fault[2]";
150 break;
151 case 0x05: FaultSource = "Translation fault on Section";
152 break;
153 case 0x06: FaultSource = "Access Flag fault on Page";
154 break;
155 case 0x07: FaultSource = "Translation fault on Page";
156 break;
157 case 0x08: FaultSource = "Precise External Abort";
158 break;
159 case 0x09: FaultSource = "Domain fault on Section";
160 break;
161 case 0x0b: FaultSource = "Domain fault on Page";
162 break;
163 case 0x0c: FaultSource = "External abort on translation, first level";
164 break;
165 case 0x0d: FaultSource = "Permission fault on Section";
166 break;
167 case 0x0e: FaultSource = "External abort on translation, second level";
168 break;
169 case 0x0f: FaultSource = "Permission fault on Page";
170 break;
171 case 0x16: FaultSource = "Imprecise External Abort";
172 break;
173 default: FaultSource = "No function";
174 break;
175 }
176
177 return FaultSource;
178}
179
180STATIC CHAR8 *gExceptionTypeString[] = {
181 "Reset",
182 "Undefined OpCode",
183 "SVC",
184 "Prefetch Abort",
185 "Data Abort",
186 "Undefined",
187 "IRQ",
188 "FIQ"
189};
190
201VOID
203 IN EFI_EXCEPTION_TYPE ExceptionType,
204 IN OUT EFI_SYSTEM_CONTEXT SystemContext
205 )
206{
207 CHAR8 Buffer[MAX_PRINT_CHARS];
208 CHAR16 UnicodeBuffer[MAX_PRINT_CHARS];
209 UINTN CharCount;
210 UINT32 DfsrStatus;
211 UINT32 IfsrStatus;
212 BOOLEAN DfsrWrite;
213 UINT32 PcAdjust;
214
215 PcAdjust = 0;
216
217 CharCount = AsciiSPrint (
218 Buffer,
219 sizeof (Buffer),
220 "\n%a Exception PC at 0x%08x CPSR 0x%08x ",
221 gExceptionTypeString[ExceptionType],
222 SystemContext.SystemContextArm->PC,
223 SystemContext.SystemContextArm->CPSR
224 );
225 SerialPortWrite ((UINT8 *)Buffer, CharCount);
226
227 // Prepare a unicode buffer for ConOut, if applicable, as Buffer is used
228 // below.
229 UnicodeSPrintAsciiFormat (UnicodeBuffer, MAX_PRINT_CHARS, Buffer);
230
232 CHAR8 *Pdb;
233 UINT32 ImageBase;
234 UINT32 PeCoffSizeOfHeader;
235 UINT32 Offset;
236 CHAR8 CpsrStr[CPSR_STRING_SIZE]; // char per bit. Lower 5-bits are mode
237 // that is a 3 char string
238 CHAR8 Buffer[80];
239 UINT8 *DisAsm;
240 UINT32 ItBlock;
241
242 CpsrString (SystemContext.SystemContextArm->CPSR, CpsrStr);
243 DEBUG ((DEBUG_ERROR, "%a\n", CpsrStr));
244
245 Pdb = GetImageName (SystemContext.SystemContextArm->PC, &ImageBase, &PeCoffSizeOfHeader);
246 Offset = SystemContext.SystemContextArm->PC - ImageBase;
247 if (Pdb != NULL) {
248 DEBUG ((DEBUG_ERROR, "%a\n", Pdb));
249
250 //
251 // A PE/COFF image loads its headers into memory so the headers are
252 // included in the linked addresses. ELF and Mach-O images do not
253 // include the headers so the first byte of the image is usually
254 // text (code). If you look at link maps from ELF or Mach-O images
255 // you need to subtract out the size of the PE/COFF header to get
256 // get the offset that matches the link map.
257 //
258 DEBUG ((DEBUG_ERROR, "loaded at 0x%08x (PE/COFF offset) 0x%x (ELF or Mach-O offset) 0x%x", ImageBase, Offset, Offset - PeCoffSizeOfHeader));
259
260 // If we come from an image it is safe to show the instruction. We know it should not fault
261 DisAsm = (UINT8 *)(UINTN)SystemContext.SystemContextArm->PC;
262 ItBlock = 0;
263 DisassembleInstruction (&DisAsm, (SystemContext.SystemContextArm->CPSR & BIT5) == BIT5, TRUE, &ItBlock, Buffer, sizeof (Buffer));
264 DEBUG ((DEBUG_ERROR, "\n%a", Buffer));
265
266 switch (ExceptionType) {
267 case EXCEPT_ARM_UNDEFINED_INSTRUCTION:
268 case EXCEPT_ARM_SOFTWARE_INTERRUPT:
269 case EXCEPT_ARM_PREFETCH_ABORT:
270 case EXCEPT_ARM_DATA_ABORT:
271 // advance PC past the faulting instruction
272 PcAdjust = (UINTN)DisAsm - SystemContext.SystemContextArm->PC;
273 break;
274
275 default:
276 break;
277 }
278 }
279
281 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));
282 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));
283 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));
284 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));
285 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));
286
287 // Bit10 is Status[4] Bit3:0 is Status[3:0]
288 DfsrStatus = (SystemContext.SystemContextArm->DFSR & 0xf) | ((SystemContext.SystemContextArm->DFSR >> 6) & 0x10);
289 DfsrWrite = (SystemContext.SystemContextArm->DFSR & BIT11) != 0;
290 if (DfsrStatus != 0x00) {
291 DEBUG ((DEBUG_ERROR, " %a: %a 0x%08x\n", FaultStatusToString (DfsrStatus), DfsrWrite ? "write to" : "read from", SystemContext.SystemContextArm->DFAR));
292 }
293
294 IfsrStatus = (SystemContext.SystemContextArm->IFSR & 0xf) | ((SystemContext.SystemContextArm->IFSR >> 6) & 0x10);
295 if (IfsrStatus != 0) {
296 DEBUG ((DEBUG_ERROR, " Instruction %a at 0x%08x\n", FaultStatusToString (SystemContext.SystemContextArm->IFSR & 0xf), SystemContext.SystemContextArm->IFAR));
297 }
298
299 DEBUG ((DEBUG_ERROR, "\n"));
300
301 // Attempt to print that we had a synchronous exception to ConOut. We do
302 // this after the serial logging as ConOut's logging is more complex and we
303 // aren't guaranteed to succeed.
304 if (gST->ConOut != NULL) {
305 gST->ConOut->OutputString (gST->ConOut, UnicodeBuffer);
306 }
307
308 ASSERT (FALSE);
309
310 CpuDeadLoop (); // may return if executing under a debugger
311
312 // Clear the error registers that we have already displayed incase some one wants to keep going
313 SystemContext.SystemContextArm->DFSR = 0;
314 SystemContext.SystemContextArm->IFSR = 0;
315
316 // If some one is stepping past the exception handler adjust the PC to point to the next instruction
317 SystemContext.SystemContextArm->PC += PcAdjust;
318}
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
VOID CpsrString(IN UINT32 Cpsr, OUT CHAR8 *ReturnStr)
VOID DisassembleInstruction(IN UINT8 **OpCodePtr, IN BOOLEAN Thumb, IN BOOLEAN Extended, IN OUT UINT32 *ItBlock, OUT CHAR8 *Buf, OUT UINTN Size)
UINTN EFIAPI SerialPortWrite(IN UINT8 *Buffer, IN UINTN NumberOfBytes)
Definition: SerialPortLib.c:52
RETURN_STATUS EFIAPI AsciiStrCatS(IN OUT CHAR8 *Destination, IN UINTN DestMax, IN CONST CHAR8 *Source)
Definition: SafeString.c:1964
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