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