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