b6fbb19f46837a06dbfda7f5b2873a1715a61753
[mirror_edk2.git] / ArmPkg / Library / DefaultExceptionHandlerLib / DefaultExceptionHandler.c
1 /** @file
2 Default exception handler
3
4 Copyright (c) 2008-2010, Apple Inc. All rights reserved.
5
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include <Uefi.h>
17 #include <Library/UefiLib.h>
18 #include <Library/BaseLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/PeCoffGetEntryPointLib.h>
21 #include <Library/ArmDisassemblerLib.h>
22
23 #include <Guid/DebugImageInfoTable.h>
24 #include <Protocol/DebugSupport.h>
25 #include <Protocol/LoadedImage.h>
26
27
28 EFI_DEBUG_IMAGE_INFO_TABLE_HEADER *gDebugImageTableHeader = NULL;
29
30
31 typedef struct {
32 UINT32 BIT;
33 CHAR8 Char;
34 } CPSR_CHAR;
35
36
37
38
39 /**
40 Use the EFI Debug Image Table to lookup the FaultAddress and find which PE/COFF image
41 it came from. As long as the PE/COFF image contains a debug directory entry a
42 string can be returned. For ELF and Mach-O images the string points to the Mach-O or ELF
43 image. Microsoft tools contain a pointer to the PDB file that contains the debug information.
44
45 @param FaultAddress Address to find PE/COFF image for.
46 @param ImageBase Return load address of found image
47 @param PeCoffSizeOfHeaders Return the size of the PE/COFF header for the image that was found
48
49 @retval NULL FaultAddress not in a loaded PE/COFF image.
50 @retval Path and file name of PE/COFF image.
51
52 **/
53 CHAR8 *
54 GetImageName (
55 IN UINT32 FaultAddress,
56 OUT UINT32 *ImageBase,
57 OUT UINT32 *PeCoffSizeOfHeaders
58 )
59 {
60 EFI_DEBUG_IMAGE_INFO *DebugTable;
61 UINTN Entry;
62 CHAR8 *Address;
63
64
65 DebugTable = gDebugImageTableHeader->EfiDebugImageInfoTable;
66 if (DebugTable == NULL) {
67 return NULL;
68 }
69
70 Address = (CHAR8 *)(UINTN)FaultAddress;
71 for (Entry = 0; Entry < gDebugImageTableHeader->TableSize; Entry++, DebugTable++) {
72 if (DebugTable->NormalImage != NULL) {
73 if ((DebugTable->NormalImage->ImageInfoType == EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL) &&
74 (DebugTable->NormalImage->LoadedImageProtocolInstance != NULL)) {
75 if ((Address >= (CHAR8 *)DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase) &&
76 (Address <= ((CHAR8 *)DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase + DebugTable->NormalImage->LoadedImageProtocolInstance->ImageSize))) {
77 *ImageBase = (UINT32)DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase;
78 *PeCoffSizeOfHeaders = PeCoffGetSizeOfHeaders ((VOID *)(UINTN)*ImageBase);
79 return PeCoffLoaderGetPdbPointer (DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase);
80 }
81 }
82 }
83 }
84
85 return NULL;
86 }
87
88
89 /**
90 Convert the Current Program Status Register (CPSR) to a string. The string is
91 a defacto standard in the ARM world.
92
93 It is possible to add extra bits by adding them to CpsrChar array.
94
95 @param Cpsr ARM CPSR register value
96 @param ReturnStr 32 byte string that contains string version of CPSR
97
98 **/
99 VOID
100 CpsrString (
101 IN UINT32 Cpsr,
102 OUT CHAR8 *ReturnStr
103 )
104 {
105 UINTN Index;
106 CHAR8 *Str = ReturnStr;
107 CHAR8 *ModeStr;
108 CPSR_CHAR CpsrChar[] = {
109 { 31, 'n' },
110 { 30, 'z' },
111 { 29, 'c' },
112 { 28, 'v' },
113
114 { 9, 'e' },
115 { 8, 'a' },
116 { 7, 'i' },
117 { 6, 'f' },
118 { 5, 't' },
119 { 0, '?' }
120 };
121
122 for (Index = 0; CpsrChar[Index].BIT != 0; Index++, Str++) {
123 *Str = CpsrChar[Index].Char;
124 if ((Cpsr & (1 << CpsrChar[Index].BIT)) != 0) {
125 // Concert to upper case if bit is set
126 *Str &= ~0x20;
127 }
128 }
129
130 *Str++ = '_';
131 *Str = '\0';
132
133 switch (Cpsr & 0x1f) {
134 case 0x10:
135 ModeStr = "usr";
136 break;
137 case 0x011:
138 ModeStr = "fiq";
139 break;
140 case 0x12:
141 ModeStr = "irq";
142 break;
143 case 0x13:
144 ModeStr = "svc";
145 break;
146 case 0x16:
147 ModeStr = "mon";
148 break;
149 case 0x17:
150 ModeStr = "abt";
151 break;
152 case 0x1b:
153 ModeStr = "und";
154 break;
155 case 0x1f:
156 ModeStr = "sys";
157 break;
158
159 default:
160 ModeStr = "???";
161 break;
162 }
163
164 AsciiStrCat (Str, ModeStr);
165 return;
166 }
167
168 CHAR8 *
169 FaultStatusToString (
170 IN UINT32 Status
171 )
172 {
173 CHAR8 *FaultSource;
174
175 switch (Status) {
176 case 0x01: FaultSource = "Alignment fault"; break;
177 case 0x02: FaultSource = "Debug event fault"; break;
178 case 0x03: FaultSource = "Access Flag fault on Section"; break;
179 case 0x04: FaultSource = "Cache maintenance operation fault[2]"; break;
180 case 0x05: FaultSource = "Translation fault on Section"; break;
181 case 0x06: FaultSource = "Access Flag fault on Page"; break;
182 case 0x07: FaultSource = "Translation fault on Page"; break;
183 case 0x08: FaultSource = "Precise External Abort"; break;
184 case 0x09: FaultSource = "Domain fault on Section"; break;
185 case 0x0b: FaultSource = "Domain fault on Page"; break;
186 case 0x0c: FaultSource = "External abort on translation, first level"; break;
187 case 0x0d: FaultSource = "Permission fault on Section"; break;
188 case 0x0e: FaultSource = "External abort on translation, second level"; break;
189 case 0x0f: FaultSource = "Permission fault on Page"; break;
190 case 0x16: FaultSource = "Imprecise External Abort"; break;
191 default: FaultSource = "No function"; break;
192 }
193
194 return FaultSource;
195 }
196
197
198 CHAR8 *gExceptionTypeString[] = {
199 "Reset",
200 "Undefined OpCode",
201 "SWI",
202 "Prefetch Abort",
203 "Data Abort",
204 "Undefined",
205 "IRQ",
206 "FIQ"
207 };
208
209
210 /**
211 This is the default action to take on an unexpected exception
212
213 Since this is exception context don't do anything crazy like try to allcoate memory.
214
215 @param ExceptionType Type of the exception
216 @param SystemContext Register state at the time of the Exception
217
218
219 **/
220 VOID
221 DefaultExceptionHandler (
222 IN EFI_EXCEPTION_TYPE ExceptionType,
223 IN OUT EFI_SYSTEM_CONTEXT SystemContext
224 )
225 {
226 UINT32 DfsrStatus;
227 BOOLEAN DfsrWrite;
228
229 DEBUG ((EFI_D_ERROR, "\n%a Exception PC at 0x%08x CPSR 0x%08x ", gExceptionTypeString[ExceptionType], SystemContext.SystemContextArm->PC, SystemContext.SystemContextArm->CPSR));
230 DEBUG_CODE_BEGIN ();
231 CHAR8 *Pdb;
232 UINT32 ImageBase;
233 UINT32 PeCoffSizeOfHeader;
234 UINT32 Offset;
235 CHAR8 CpsrStr[32]; // char per bit. Lower 5-bits are mode that is a 3 char string
236 CHAR8 Buffer[80];
237 UINT8 *DisAsm;
238
239 CpsrString (SystemContext.SystemContextArm->CPSR, CpsrStr);
240 DEBUG ((EFI_D_ERROR, "%a\n", CpsrStr));
241
242 Pdb = GetImageName (SystemContext.SystemContextArm->PC, &ImageBase, &PeCoffSizeOfHeader);
243 Offset = SystemContext.SystemContextArm->PC - ImageBase;
244 if (Pdb != NULL) {
245 DEBUG ((EFI_D_ERROR, "%a\n", Pdb));
246
247 //
248 // A PE/COFF image loads its headers into memory so the headers are
249 // included in the linked addressess. ELF and Mach-O images do not
250 // include the headers so the first byte of the image is usually
251 // text (code). If you look at link maps from ELF or Mach-O images
252 // you need to subtact out the size of the PE/COFF header to get
253 // get the offset that matches the link map.
254 //
255 DEBUG ((EFI_D_ERROR, "loaded at 0x%08x (PE/COFF offset) 0x%x (ELF or Mach-O offset) 0x%x", ImageBase, Offset, Offset - PeCoffSizeOfHeader));
256
257 // If we come from an image it is safe to show the instruction. We know it should not fault
258 DisAsm = (UINT8 *)(UINTN)SystemContext.SystemContextArm->PC;
259 DisassembleInstruction (&DisAsm, (SystemContext.SystemContextArm->CPSR & BIT5) == BIT5, Buffer, sizeof (Buffer));
260 DEBUG ((EFI_D_ERROR, "\n%a", Buffer));
261
262 }
263 DEBUG_CODE_END ();
264 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));
265 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));
266 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));
267 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));
268 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));
269
270 // Bit10 is Status[4] Bit3:0 is Status[3:0]
271 DfsrStatus = (SystemContext.SystemContextArm->DFSR & 0xf) | ((SystemContext.SystemContextArm->DFSR >> 6) & 0x10);
272 DfsrWrite = (SystemContext.SystemContextArm->DFSR & BIT11) != 0;
273 if (DfsrStatus != 0x00) {
274 DEBUG ((EFI_D_ERROR, " %a: %a 0x%08x\n", FaultStatusToString (DfsrStatus), DfsrWrite ? "write to" : "read from", SystemContext.SystemContextArm->DFAR));
275 }
276 if ((SystemContext.SystemContextArm->IFSR & 0xf) != 0x00) {
277 DEBUG ((EFI_D_ERROR, "Instruction %a at 0x%08x, \n", FaultStatusToString (SystemContext.SystemContextArm->IFSR & 0xf), SystemContext.SystemContextArm->IFAR));
278 }
279
280 DEBUG ((EFI_D_ERROR, "\n"));
281 ASSERT (FALSE);
282 }
283
284
285
286
287 /**
288 The constructor function caches EFI Debug table information for use in the exception handler.
289
290
291 @param ImageHandle The firmware allocated handle for the EFI image.
292 @param SystemTable A pointer to the EFI System Table.
293
294 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
295
296 **/
297 EFI_STATUS
298 EFIAPI
299 DefaultExceptionHandlerConstructor (
300 IN EFI_HANDLE ImageHandle,
301 IN EFI_SYSTEM_TABLE *SystemTable
302 )
303 {
304 EFI_STATUS Status;
305
306
307 Status = EfiGetSystemConfigurationTable (&gEfiDebugImageInfoTableGuid, (VOID **)&gDebugImageTableHeader);
308 if (EFI_ERROR (Status)) {
309 gDebugImageTableHeader = NULL;
310 }
311 return Status;
312 }