]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/DefaultExceptionHandlerLib/Arm/DefaultExceptionHandler.c
ARM Packages: Replace tabs by spaces for indentation
[mirror_edk2.git] / ArmPkg / Library / DefaultExceptionHandlerLib / Arm / DefaultExceptionHandler.c
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 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include <Uefi.h>
18 #include <Library/BaseLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/PeCoffGetEntryPointLib.h>
21 #include <Library/PrintLib.h>
22 #include <Library/ArmDisassemblerLib.h>
23 #include <Library/SerialPortLib.h>
24
25 #include <Guid/DebugImageInfoTable.h>
26
27 #include <Protocol/DebugSupport.h>
28 #include <Library/DefaultExceptionHandlerLib.h>
29
30 EFI_DEBUG_IMAGE_INFO_TABLE_HEADER *gDebugImageTableHeader = NULL;
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 32 byte string that contains string version of CPSR
52
53 **/
54 VOID
55 CpsrString (
56 IN UINT32 Cpsr,
57 OUT CHAR8 *ReturnStr
58 )
59 {
60 UINTN Index;
61 CHAR8* Str;
62 CHAR8* ModeStr;
63 CPSR_CHAR CpsrChar[] = {
64 { 31, 'n' },
65 { 30, 'z' },
66 { 29, 'c' },
67 { 28, 'v' },
68
69 { 9, 'e' },
70 { 8, 'a' },
71 { 7, 'i' },
72 { 6, 'f' },
73 { 5, 't' },
74 { 0, '?' }
75 };
76
77 Str = ReturnStr;
78
79 for (Index = 0; CpsrChar[Index].BIT != 0; Index++, Str++) {
80 *Str = CpsrChar[Index].Char;
81 if ((Cpsr & (1 << CpsrChar[Index].BIT)) != 0) {
82 // Concert to upper case if bit is set
83 *Str &= ~0x20;
84 }
85 }
86
87 *Str++ = '_';
88 *Str = '\0';
89
90 switch (Cpsr & 0x1f) {
91 case 0x10:
92 ModeStr = "usr";
93 break;
94 case 0x011:
95 ModeStr = "fiq";
96 break;
97 case 0x12:
98 ModeStr = "irq";
99 break;
100 case 0x13:
101 ModeStr = "svc";
102 break;
103 case 0x16:
104 ModeStr = "mon";
105 break;
106 case 0x17:
107 ModeStr = "abt";
108 break;
109 case 0x1b:
110 ModeStr = "und";
111 break;
112 case 0x1f:
113 ModeStr = "sys";
114 break;
115
116 default:
117 ModeStr = "???";
118 break;
119 }
120
121 AsciiStrCat (Str, ModeStr);
122 return;
123 }
124
125 CHAR8 *
126 FaultStatusToString (
127 IN UINT32 Status
128 )
129 {
130 CHAR8 *FaultSource;
131
132 switch (Status) {
133 case 0x01: FaultSource = "Alignment fault"; break;
134 case 0x02: FaultSource = "Debug event fault"; break;
135 case 0x03: FaultSource = "Access Flag fault on Section"; break;
136 case 0x04: FaultSource = "Cache maintenance operation fault[2]"; break;
137 case 0x05: FaultSource = "Translation fault on Section"; break;
138 case 0x06: FaultSource = "Access Flag fault on Page"; break;
139 case 0x07: FaultSource = "Translation fault on Page"; break;
140 case 0x08: FaultSource = "Precise External Abort"; break;
141 case 0x09: FaultSource = "Domain fault on Section"; break;
142 case 0x0b: FaultSource = "Domain fault on Page"; break;
143 case 0x0c: FaultSource = "External abort on translation, first level"; break;
144 case 0x0d: FaultSource = "Permission fault on Section"; break;
145 case 0x0e: FaultSource = "External abort on translation, second level"; break;
146 case 0x0f: FaultSource = "Permission fault on Page"; break;
147 case 0x16: FaultSource = "Imprecise External Abort"; break;
148 default: FaultSource = "No function"; break;
149 }
150
151 return FaultSource;
152 }
153
154 STATIC CHAR8 *gExceptionTypeString[] = {
155 "Reset",
156 "Undefined OpCode",
157 "SVC",
158 "Prefetch Abort",
159 "Data Abort",
160 "Undefined",
161 "IRQ",
162 "FIQ"
163 };
164
165 /**
166 This is the default action to take on an unexpected exception
167
168 Since this is exception context don't do anything crazy like try to allcoate memory.
169
170 @param ExceptionType Type of the exception
171 @param SystemContext Register state at the time of the Exception
172
173
174 **/
175 VOID
176 DefaultExceptionHandler (
177 IN EFI_EXCEPTION_TYPE ExceptionType,
178 IN OUT EFI_SYSTEM_CONTEXT SystemContext
179 )
180 {
181 CHAR8 Buffer[100];
182 UINTN CharCount;
183 UINT32 DfsrStatus;
184 UINT32 IfsrStatus;
185 BOOLEAN DfsrWrite;
186 UINT32 PcAdjust = 0;
187
188 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"\n%a Exception PC at 0x%08x CPSR 0x%08x ",
189 gExceptionTypeString[ExceptionType], SystemContext.SystemContextArm->PC, SystemContext.SystemContextArm->CPSR);
190 SerialPortWrite ((UINT8 *) Buffer, CharCount);
191
192 DEBUG_CODE_BEGIN ();
193 CHAR8 *Pdb;
194 UINT32 ImageBase;
195 UINT32 PeCoffSizeOfHeader;
196 UINT32 Offset;
197 CHAR8 CpsrStr[32]; // char per bit. Lower 5-bits are mode that is a 3 char string
198 CHAR8 Buffer[80];
199 UINT8 *DisAsm;
200 UINT32 ItBlock;
201
202 CpsrString (SystemContext.SystemContextArm->CPSR, CpsrStr);
203 DEBUG ((EFI_D_ERROR, "%a\n", CpsrStr));
204
205 Pdb = GetImageName (SystemContext.SystemContextArm->PC, &ImageBase, &PeCoffSizeOfHeader);
206 Offset = SystemContext.SystemContextArm->PC - ImageBase;
207 if (Pdb != NULL) {
208 DEBUG ((EFI_D_ERROR, "%a\n", Pdb));
209
210 //
211 // A PE/COFF image loads its headers into memory so the headers are
212 // included in the linked addresses. ELF and Mach-O images do not
213 // include the headers so the first byte of the image is usually
214 // text (code). If you look at link maps from ELF or Mach-O images
215 // you need to subtract out the size of the PE/COFF header to get
216 // get the offset that matches the link map.
217 //
218 DEBUG ((EFI_D_ERROR, "loaded at 0x%08x (PE/COFF offset) 0x%x (ELF or Mach-O offset) 0x%x", ImageBase, Offset, Offset - PeCoffSizeOfHeader));
219
220 // If we come from an image it is safe to show the instruction. We know it should not fault
221 DisAsm = (UINT8 *)(UINTN)SystemContext.SystemContextArm->PC;
222 ItBlock = 0;
223 DisassembleInstruction (&DisAsm, (SystemContext.SystemContextArm->CPSR & BIT5) == BIT5, TRUE, &ItBlock, Buffer, sizeof (Buffer));
224 DEBUG ((EFI_D_ERROR, "\n%a", Buffer));
225
226 switch (ExceptionType) {
227 case EXCEPT_ARM_UNDEFINED_INSTRUCTION:
228 case EXCEPT_ARM_SOFTWARE_INTERRUPT:
229 case EXCEPT_ARM_PREFETCH_ABORT:
230 case EXCEPT_ARM_DATA_ABORT:
231 // advance PC past the faulting instruction
232 PcAdjust = (UINTN)DisAsm - SystemContext.SystemContextArm->PC;
233 break;
234
235 default:
236 break;
237 }
238
239 }
240 DEBUG_CODE_END ();
241 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));
242 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));
243 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));
244 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));
245 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));
246
247 // Bit10 is Status[4] Bit3:0 is Status[3:0]
248 DfsrStatus = (SystemContext.SystemContextArm->DFSR & 0xf) | ((SystemContext.SystemContextArm->DFSR >> 6) & 0x10);
249 DfsrWrite = (SystemContext.SystemContextArm->DFSR & BIT11) != 0;
250 if (DfsrStatus != 0x00) {
251 DEBUG ((EFI_D_ERROR, " %a: %a 0x%08x\n", FaultStatusToString (DfsrStatus), DfsrWrite ? "write to" : "read from", SystemContext.SystemContextArm->DFAR));
252 }
253
254 IfsrStatus = (SystemContext.SystemContextArm->IFSR & 0xf) | ((SystemContext.SystemContextArm->IFSR >> 6) & 0x10);
255 if (IfsrStatus != 0) {
256 DEBUG ((EFI_D_ERROR, " Instruction %a at 0x%08x\n", FaultStatusToString (SystemContext.SystemContextArm->IFSR & 0xf), SystemContext.SystemContextArm->IFAR));
257 }
258
259 DEBUG ((EFI_D_ERROR, "\n"));
260 ASSERT (FALSE);
261
262 // Clear the error registers that we have already displayed incase some one wants to keep going
263 SystemContext.SystemContextArm->DFSR = 0;
264 SystemContext.SystemContextArm->IFSR = 0;
265
266 // If some one is stepping past the exception handler adjust the PC to point to the next instruction
267 SystemContext.SystemContextArm->PC += PcAdjust;
268 }