]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmPkg/Library/DefaultExceptionHandlerLib/Arm/DefaultExceptionHandler.c
ArmPkg/DefaultExceptionHandlerLib ARM: avoid endless loop in RELEASE builds
[mirror_edk2.git] / ArmPkg / Library / DefaultExceptionHandlerLib / Arm / DefaultExceptionHandler.c
index 74e2a7602cd4838896413fbc5760c260c126dab0..cc79cb2fa3017bdd33fe7de616099f18a84aeb16 100644 (file)
@@ -3,7 +3,7 @@
 \r
   Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
   Copyright (c) 2012, ARM Ltd. All rights reserved.<BR>\r
-  \r
+\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
 #include <Protocol/DebugSupport.h>\r
 #include <Library/DefaultExceptionHandlerLib.h>\r
 \r
-EFI_DEBUG_IMAGE_INFO_TABLE_HEADER *gDebugImageTableHeader = NULL;\r
+//\r
+// The number of elements in a CHAR8 array, including the terminating NUL, that\r
+// is meant to hold the string rendering of the CPSR.\r
+//\r
+#define CPSR_STRING_SIZE 32\r
 \r
 typedef struct {\r
   UINT32  BIT;\r
@@ -42,13 +46,14 @@ GetImageName (
   );\r
 \r
 /**\r
-  Convert the Current Program Status Register (CPSR) to a string. The string is \r
-  a defacto standard in the ARM world. \r
-  \r
+  Convert the Current Program Status Register (CPSR) to a string. The string is\r
+  a defacto standard in the ARM world.\r
+\r
   It is possible to add extra bits by adding them to CpsrChar array.\r
 \r
   @param  Cpsr         ARM CPSR register value\r
-  @param  ReturnStr    32 byte string that contains string version of CPSR\r
+  @param  ReturnStr    CPSR_STRING_SIZE byte string that contains string\r
+                       version of CPSR\r
 \r
 **/\r
 VOID\r
@@ -73,7 +78,7 @@ CpsrString (
     { 5,  't' },\r
     { 0,  '?' }\r
   };\r
-  \r
+\r
   Str = ReturnStr;\r
 \r
   for (Index = 0; CpsrChar[Index].BIT != 0; Index++, Str++) {\r
@@ -83,10 +88,10 @@ CpsrString (
       *Str &= ~0x20;\r
     }\r
   }\r
-  \r
+\r
   *Str++ = '_';\r
   *Str = '\0';\r
-  \r
+\r
   switch (Cpsr & 0x1f) {\r
   case 0x10:\r
     ModeStr = "usr";\r
@@ -112,15 +117,17 @@ CpsrString (
   case 0x1f:\r
     ModeStr = "sys";\r
     break;\r
-  \r
+\r
   default:\r
     ModeStr = "???";\r
     break;\r
   }\r
-  \r
-  AsciiStrCat (Str, ModeStr);\r
-  return;\r
-}  \r
+\r
+  //\r
+  // See the interface contract in the leading comment block.\r
+  //\r
+  AsciiStrCatS (Str, CPSR_STRING_SIZE - (Str - ReturnStr), ModeStr);\r
+}\r
 \r
 CHAR8 *\r
 FaultStatusToString (\r
@@ -164,7 +171,7 @@ STATIC CHAR8 *gExceptionTypeString[] = {
 \r
 /**\r
   This is the default action to take on an unexpected exception\r
-  \r
+\r
   Since this is exception context don't do anything crazy like try to allcoate memory.\r
 \r
   @param  ExceptionType    Type of the exception\r
@@ -186,7 +193,7 @@ DefaultExceptionHandler (
   UINT32    PcAdjust = 0;\r
 \r
   CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"\n%a Exception PC at 0x%08x  CPSR 0x%08x ",\r
-         gExceptionTypeString[ExceptionType], SystemContext.SystemContextArm->PC, SystemContext.SystemContextArm->CPSR);\r
+         gExceptionTypeString[ExceptionType], SystemContext.SystemContextArm->PC, SystemContext.SystemContextArm->CPSR);\r
   SerialPortWrite ((UINT8 *) Buffer, CharCount);\r
 \r
   DEBUG_CODE_BEGIN ();\r
@@ -194,35 +201,36 @@ DefaultExceptionHandler (
     UINT32  ImageBase;\r
     UINT32  PeCoffSizeOfHeader;\r
     UINT32  Offset;\r
-    CHAR8   CpsrStr[32];  // char per bit. Lower 5-bits are mode that is a 3 char string\r
+    CHAR8   CpsrStr[CPSR_STRING_SIZE];  // char per bit. Lower 5-bits are mode\r
+                                        // that is a 3 char string\r
     CHAR8   Buffer[80];\r
     UINT8   *DisAsm;\r
     UINT32  ItBlock;\r
-    \r
+\r
     CpsrString (SystemContext.SystemContextArm->CPSR, CpsrStr);\r
     DEBUG ((EFI_D_ERROR, "%a\n", CpsrStr));\r
-  \r
+\r
     Pdb = GetImageName (SystemContext.SystemContextArm->PC, &ImageBase, &PeCoffSizeOfHeader);\r
     Offset = SystemContext.SystemContextArm->PC - ImageBase;\r
     if (Pdb != NULL) {\r
       DEBUG ((EFI_D_ERROR, "%a\n", Pdb));\r
 \r
       //\r
-      // A PE/COFF image loads its headers into memory so the headers are \r
+      // A PE/COFF image loads its headers into memory so the headers are\r
       // included in the linked addresses. ELF and Mach-O images do not\r
       // include the headers so the first byte of the image is usually\r
       // text (code). If you look at link maps from ELF or Mach-O images\r
       // you need to subtract out the size of the PE/COFF header to get\r
-      // get the offset that matches the link map. \r
+      // get the offset that matches the link map.\r
       //\r
       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
+\r
       // If we come from an image it is safe to show the instruction. We know it should not fault\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
+\r
       switch (ExceptionType) {\r
       case EXCEPT_ARM_UNDEFINED_INSTRUCTION:\r
       case EXCEPT_ARM_SOFTWARE_INTERRUPT:\r
@@ -231,7 +239,7 @@ DefaultExceptionHandler (
         // advance PC past the faulting instruction\r
         PcAdjust = (UINTN)DisAsm - SystemContext.SystemContextArm->PC;\r
         break;\r
-      \r
+\r
       default:\r
         break;\r
       }\r
@@ -258,11 +266,13 @@ DefaultExceptionHandler (
 \r
   DEBUG ((EFI_D_ERROR, "\n"));\r
   ASSERT (FALSE);\r
-  \r
+\r
+  CpuDeadLoop ();   // may return if executing under a debugger\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
+  // 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