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