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