2 Default exception handler
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
5 Copyright (c) 2012, ARM Ltd. All rights reserved.<BR>
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
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.
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>
25 #include <Guid/DebugImageInfoTable.h>
27 #include <Protocol/DebugSupport.h>
28 #include <Library/DefaultExceptionHandlerLib.h>
30 EFI_DEBUG_IMAGE_INFO_TABLE_HEADER
*gDebugImageTableHeader
= NULL
;
39 IN UINT32 FaultAddress
,
40 OUT UINT32
*ImageBase
,
41 OUT UINT32
*PeCoffSizeOfHeaders
45 Convert the Current Program Status Register (CPSR) to a string. The string is
46 a defacto standard in the ARM world.
48 It is possible to add extra bits by adding them to CpsrChar array.
50 @param Cpsr ARM CPSR register value
51 @param ReturnStr 32 byte string that contains string version of CPSR
63 CPSR_CHAR CpsrChar
[] = {
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
90 switch (Cpsr
& 0x1f) {
121 AsciiStrCat (Str
, ModeStr
);
126 FaultStatusToString (
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;
154 STATIC CHAR8
*gExceptionTypeString
[] = {
166 This is the default action to take on an unexpected exception
168 Since this is exception context don't do anything crazy like try to allcoate memory.
170 @param ExceptionType Type of the exception
171 @param SystemContext Register state at the time of the Exception
176 DefaultExceptionHandler (
177 IN EFI_EXCEPTION_TYPE ExceptionType
,
178 IN OUT EFI_SYSTEM_CONTEXT SystemContext
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
);
195 UINT32 PeCoffSizeOfHeader
;
197 CHAR8 CpsrStr
[32]; // char per bit. Lower 5-bits are mode that is a 3 char string
202 CpsrString (SystemContext
.SystemContextArm
->CPSR
, CpsrStr
);
203 DEBUG ((EFI_D_ERROR
, "%a\n", CpsrStr
));
205 Pdb
= GetImageName (SystemContext
.SystemContextArm
->PC
, &ImageBase
, &PeCoffSizeOfHeader
);
206 Offset
= SystemContext
.SystemContextArm
->PC
- ImageBase
;
208 DEBUG ((EFI_D_ERROR
, "%a\n", Pdb
));
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.
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
));
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
;
223 DisassembleInstruction (&DisAsm
, (SystemContext
.SystemContextArm
->CPSR
& BIT5
) == BIT5
, TRUE
, &ItBlock
, Buffer
, sizeof (Buffer
));
224 DEBUG ((EFI_D_ERROR
, "\n%a", Buffer
));
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
;
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
));
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
));
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
));
259 DEBUG ((EFI_D_ERROR
, "\n"));
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;
266 // If some one is stepping past the exception handler adjust the PC to point to the next instruction
267 SystemContext
.SystemContextArm
->PC
+= PcAdjust
;