]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmPkg/Library/DefaultExceptionHandlerLib/DefaultExceptionHandler.c
Sync up ArmPkg with patch from mailing list. Changed name of BdsLib.h to BdsUnixLib...
[mirror_edk2.git] / ArmPkg / Library / DefaultExceptionHandlerLib / DefaultExceptionHandler.c
index 56504948a34e42002c3b01b2f4c25aee0cbacc55..e969dfa7634922e302bf78ccd6cd3490ee20c412 100644 (file)
@@ -1,9 +1,9 @@
 /** @file\r
   Default exception handler\r
 \r
-  Copyright (c) 2008-2010, Apple Inc. All rights reserved.\r
+  Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
   \r
-  All rights reserved. This program and the accompanying materials\r
+  This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
   http://opensource.org/licenses/bsd-license.php\r
 #include <Library/BaseLib.h>\r
 #include <Library/DebugLib.h>\r
 #include <Library/PeCoffGetEntryPointLib.h>\r
+#include <Library/ArmDisassemblerLib.h>\r
 \r
 #include <Guid/DebugImageInfoTable.h>\r
 #include <Protocol/DebugSupport.h>\r
 #include <Protocol/LoadedImage.h>\r
 \r
 \r
-VOID\r
-DisassembleArmInstruction (\r
-  IN  UINT32    *OpCodePtr,\r
-  OUT CHAR8     *Buf,\r
-  OUT UINTN     Size\r
-  );\r
-\r
-VOID\r
-DisassembleThumbInstruction (\r
-  IN  UINT16    *OpCodePtr,\r
-  OUT CHAR8     *Buf,\r
-  OUT UINTN     Size\r
-  );\r
-\r
-\r
 EFI_DEBUG_IMAGE_INFO_TABLE_HEADER *gDebugImageTableHeader = NULL;\r
 \r
 \r
@@ -178,7 +164,37 @@ CpsrString (
   AsciiStrCat (Str, ModeStr);\r
   return;\r
 }  \r
-  \r
+\r
+CHAR8 *\r
+FaultStatusToString (\r
+  IN  UINT32  Status\r
+  )\r
+{\r
+  CHAR8 *FaultSource;\r
+\r
+  switch (Status) {\r
+    case 0x01: FaultSource = "Alignment fault"; break;\r
+    case 0x02: FaultSource = "Debug event fault"; break;\r
+    case 0x03: FaultSource = "Access Flag fault on Section"; break;\r
+    case 0x04: FaultSource = "Cache maintenance operation fault[2]"; break;\r
+    case 0x05: FaultSource = "Translation fault on Section"; break;\r
+    case 0x06: FaultSource = "Access Flag fault on Page"; break;\r
+    case 0x07: FaultSource = "Translation fault on Page"; break;\r
+    case 0x08: FaultSource = "Precise External Abort"; break;\r
+    case 0x09: FaultSource = "Domain fault on Section"; break;\r
+    case 0x0b: FaultSource = "Domain fault on Page"; break;\r
+    case 0x0c: FaultSource = "External abort on translation, first level"; break;\r
+    case 0x0d: FaultSource = "Permission fault on Section"; break;\r
+    case 0x0e: FaultSource = "External abort on translation, second level"; break;\r
+    case 0x0f: FaultSource = "Permission fault on Page"; break;\r
+    case 0x16: FaultSource = "Imprecise External Abort"; break;\r
+    default:   FaultSource = "No function"; break;\r
+    }\r
+\r
+  return FaultSource;\r
+}\r
+\r
+\r
 CHAR8 *gExceptionTypeString[] = {\r
   "Reset",\r
   "Undefined OpCode",\r
@@ -207,6 +223,10 @@ DefaultExceptionHandler (
   IN OUT EFI_SYSTEM_CONTEXT           SystemContext\r
   )\r
 {\r
+  UINT32    DfsrStatus;\r
+  UINT32    IfsrStatus;\r
+  BOOLEAN   DfsrWrite;\r
+  UINT32    PcAdjust = 0;\r
 \r
   DEBUG ((EFI_D_ERROR, "\n%a Exception PC at 0x%08x  CPSR 0x%08x ", gExceptionTypeString[ExceptionType], SystemContext.SystemContextArm->PC, SystemContext.SystemContextArm->CPSR));\r
   DEBUG_CODE_BEGIN ();\r
@@ -216,6 +236,8 @@ DefaultExceptionHandler (
     UINT32  Offset;\r
     CHAR8   CpsrStr[32];  // char per bit. Lower 5-bits are mode that is a 3 char string\r
     CHAR8   Buffer[80];\r
+    UINT8   *DisAsm;\r
+    UINT32  ItBlock;\r
     \r
     CpsrString (SystemContext.SystemContextArm->CPSR, CpsrStr);\r
     DEBUG ((EFI_D_ERROR, "%a\n", CpsrStr));\r
@@ -236,23 +258,53 @@ DefaultExceptionHandler (
       DEBUG ((EFI_D_ERROR, "loaded at 0x%08x (PE/COFF offset) 0x%x (ELF or Mach-O offset) 0x%x", ImageBase, Offset, Offset - PeCoffSizeOfHeader));\r
       \r
       // If we come from an image it is safe to show the instruction. We know it should not fault\r
-      if ((SystemContext.SystemContextArm->CPSR & 0x20) == 0) {\r
-        // ARM\r
-        DisassembleArmInstruction ((UINT32 *)(UINTN)SystemContext.SystemContextArm->PC, Buffer, sizeof (Buffer));\r
-        DEBUG ((EFI_D_ERROR, "\n%a", Buffer));\r
-      } else {\r
-        // Thumb\r
-        DEBUG ((EFI_D_ERROR, "\nFaulting Instruction 0x%04x", (UINT16 *)(UINTN)SystemContext.SystemContextArm->PC));\r
+      DisAsm = (UINT8 *)(UINTN)SystemContext.SystemContextArm->PC;\r
+      ItBlock = 0;\r
+      DisassembleInstruction (&DisAsm, (SystemContext.SystemContextArm->CPSR & BIT5) == BIT5, TRUE, &ItBlock, Buffer, sizeof (Buffer));\r
+      DEBUG ((EFI_D_ERROR, "\n%a", Buffer));\r
+      \r
+      switch (ExceptionType) {\r
+      case EXCEPT_ARM_UNDEFINED_INSTRUCTION:\r
+      case EXCEPT_ARM_SOFTWARE_INTERRUPT:\r
+      case EXCEPT_ARM_PREFETCH_ABORT:\r
+      case EXCEPT_ARM_DATA_ABORT:\r
+        // advance PC past the faulting instruction\r
+        PcAdjust = (UINTN)DisAsm - SystemContext.SystemContextArm->PC;\r
+        break;\r
+      \r
+      default:\r
+        break;\r
       }\r
+\r
     }\r
   DEBUG_CODE_END ();\r
   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));\r
   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));\r
   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));\r
   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));\r
-  DEBUG ((EFI_D_ERROR, "DFSR 0x%08x DFAR 0x%08x IFSR 0x%08x IFAR 0x%08x\n\n", SystemContext.SystemContextArm->DFSR, SystemContext.SystemContextArm->DFAR, SystemContext.SystemContextArm->IFSR, SystemContext.SystemContextArm->IFAR));\r
+  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));\r
 \r
+  // Bit10 is Status[4] Bit3:0 is Status[3:0]\r
+  DfsrStatus = (SystemContext.SystemContextArm->DFSR & 0xf) | ((SystemContext.SystemContextArm->DFSR >> 6) & 0x10);\r
+  DfsrWrite = (SystemContext.SystemContextArm->DFSR & BIT11) != 0;\r
+  if (DfsrStatus != 0x00) {\r
+    DEBUG ((EFI_D_ERROR, " %a: %a 0x%08x\n", FaultStatusToString (DfsrStatus), DfsrWrite ? "write to" : "read from", SystemContext.SystemContextArm->DFAR));\r
+  }\r
+\r
+  IfsrStatus = (SystemContext.SystemContextArm->IFSR & 0xf) | ((SystemContext.SystemContextArm->IFSR >> 6) & 0x10);\r
+  if (IfsrStatus != 0) {\r
+    DEBUG ((EFI_D_ERROR, " Instruction %a at 0x%08x\n", FaultStatusToString (SystemContext.SystemContextArm->IFSR & 0xf), SystemContext.SystemContextArm->IFAR));\r
+  }\r
+\r
+  DEBUG ((EFI_D_ERROR, "\n"));\r
   ASSERT (FALSE);\r
+  \r
+  // Clear the error registers that we have already displayed incase some one wants to keep going\r
+  SystemContext.SystemContextArm->DFSR = 0;\r
+  SystemContext.SystemContextArm->IFSR = 0;\r
+\r
+  // If some one is stepping past the exception handler adjust the PC to point to the next instruction \r
+  SystemContext.SystemContextArm->PC += PcAdjust;\r
 }\r
 \r
 \r