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