Merge trunk head (r43756)
[reactos.git] / reactos / ntoskrnl / ke / bug.c
index 500dff6..f9cc43e 100644 (file)
@@ -37,13 +37,13 @@ UNICODE_STRING KeRosVideoBiosDate, KeRosVideoBiosVersion;
 
 PVOID
 NTAPI
-KiPcToFileHeader(IN PVOID Eip,
+KiPcToFileHeader(IN PVOID Pc,
                  OUT PLDR_DATA_TABLE_ENTRY *LdrEntry,
                  IN BOOLEAN DriversOnly,
                  OUT PBOOLEAN InKernel)
 {
     ULONG i = 0;
-    PVOID ImageBase, EipBase = NULL;
+    PVOID ImageBase, PcBase = NULL;
     PLDR_DATA_TABLE_ENTRY Entry;
     PLIST_ENTRY ListHead, NextEntry;
 
@@ -82,12 +82,12 @@ KiPcToFileHeader(IN PVOID Eip,
             ImageBase = Entry->DllBase;
 
             /* Check if this is the right one */
-            if (((ULONG_PTR)Eip >= (ULONG_PTR)Entry->DllBase) &&
-                ((ULONG_PTR)Eip < ((ULONG_PTR)Entry->DllBase + Entry->SizeOfImage)))
+            if (((ULONG_PTR)Pc >= (ULONG_PTR)Entry->DllBase) &&
+                ((ULONG_PTR)Pc < ((ULONG_PTR)Entry->DllBase + Entry->SizeOfImage)))
             {
                 /* Return this entry */
                 *LdrEntry = Entry;
-                EipBase = ImageBase;
+                PcBase = ImageBase;
 
                 /* Check if this was a kernel or HAL entry */
                 if (i <= 2) *InKernel = TRUE;
@@ -97,7 +97,7 @@ KiPcToFileHeader(IN PVOID Eip,
     }
 
     /* Return the base address */
-    return EipBase;
+    return PcBase;
 }
 
 BOOLEAN
@@ -138,10 +138,10 @@ KiRosPrintAddress(PVOID address)
 
 PVOID
 NTAPI
-KiRosPcToUserFileHeader(IN PVOID Eip,
+KiRosPcToUserFileHeader(IN PVOID Pc,
                         OUT PLDR_DATA_TABLE_ENTRY *LdrEntry)
 {
-    PVOID ImageBase, EipBase = NULL;
+    PVOID ImageBase, PcBase = NULL;
     PLDR_DATA_TABLE_ENTRY Entry;
     PLIST_ENTRY ListHead, NextEntry;
 
@@ -170,19 +170,19 @@ KiRosPcToUserFileHeader(IN PVOID Eip,
             ImageBase = Entry->DllBase;
 
             /* Check if this is the right one */
-            if (((ULONG_PTR)Eip >= (ULONG_PTR)Entry->DllBase) &&
-                ((ULONG_PTR)Eip < ((ULONG_PTR)Entry->DllBase + Entry->SizeOfImage)))
+            if (((ULONG_PTR)Pc >= (ULONG_PTR)Entry->DllBase) &&
+                ((ULONG_PTR)Pc < ((ULONG_PTR)Entry->DllBase + Entry->SizeOfImage)))
             {
                 /* Return this entry */
                 *LdrEntry = Entry;
-                EipBase = ImageBase;
+                PcBase = ImageBase;
                 break;
             }
         }
     }
 
     /* Return the base address */
-    return EipBase;
+    return PcBase;
 }
 
 USHORT
@@ -547,15 +547,29 @@ KiDoBugCheckCallbacks(VOID)
     }
 }
 
-DECLSPEC_NORETURN
 VOID
 NTAPI
 KiBugCheckDebugBreak(IN ULONG StatusCode)
 {
-    /* If KDBG isn't connected, freeze the CPU, otherwise, break */
-    if (KdDebuggerNotPresent) for (;;) KeArchHaltProcessor();
-    DbgBreakPointWithStatus(StatusCode);
-    while (TRUE);
+    /*
+     * Wrap this in SEH so we don't crash if
+     * there is no debugger or if it disconnected
+     */
+DoBreak:
+    _SEH2_TRY
+    {
+        /* Breakpoint */
+        DbgBreakPointWithStatus(StatusCode);
+    }
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+    {
+        /* No debugger, halt the CPU */
+        HalHaltSystem();
+    }
+    _SEH2_END;
+
+    /* Break again if this wasn't first try */
+    if (StatusCode != DBG_STATUS_BUGCHECK_FIRST) goto DoBreak;
 }
 
 PCHAR
@@ -765,7 +779,7 @@ KeBugCheckWithTf(IN ULONG BugCheckCode,
     CHAR AnsiName[128];
     BOOLEAN IsSystem, IsHardError = FALSE, Reboot = FALSE;
     PCHAR HardErrCaption = NULL, HardErrMessage = NULL;
-    PVOID Eip = NULL, Memory;
+    PVOID Pc = NULL, Memory;
     PVOID DriverBase;
     PLDR_DATA_TABLE_ENTRY LdrEntry;
     PULONG_PTR HardErrorParameters;
@@ -875,16 +889,12 @@ KeBugCheckWithTf(IN ULONG BugCheckCode,
                 if (BugCheckParameter3) TrapFrame = (PVOID)BugCheckParameter3;
             }
 
-            /* Check if we got one now and if we need to get EIP */
+            /* Check if we got one now and if we need to get the Program Counter */
             if ((TrapFrame) &&
                 (BugCheckCode != KERNEL_MODE_EXCEPTION_NOT_HANDLED))
             {
-#ifdef _M_IX86
-                /* Get EIP */
-                Eip = (PVOID)TrapFrame->Eip;
-#elif defined(_M_PPC)
-                Eip = (PVOID)TrapFrame->Dr0; /* srr0 */
-#endif
+                /* Get the Program Counter */
+                Pc = (PVOID)KeGetTrapFramePc(TrapFrame);
             }
             break;
 
@@ -898,11 +908,14 @@ KeBugCheckWithTf(IN ULONG BugCheckCode,
              * and provide a more detailed analysis. For now, we don't.
              */
 
-            /* Eip is in parameter 4 */
-            Eip = (PVOID)BugCheckParameter4;
+            /* Program Counter is in parameter 4 */
+            Pc = (PVOID)BugCheckParameter4;
 
             /* Get the driver base */
-            DriverBase = KiPcToFileHeader(Eip, &LdrEntry, FALSE, &IsSystem);
+            DriverBase = KiPcToFileHeader(Pc,
+                                          &LdrEntry,
+                                          FALSE,
+                                          &IsSystem);
             if (IsSystem)
             {
                 /*
@@ -942,8 +955,8 @@ KeBugCheckWithTf(IN ULONG BugCheckCode,
                 KiBugCheckData[0] = DRIVER_IRQL_NOT_LESS_OR_EQUAL;
             }
 
-            /* Clear EIP so we don't look it up later */
-            Eip = NULL;
+            /* Clear Pc so we don't look it up later */
+            Pc = NULL;
             break;
 
         /* Hard error */
@@ -979,17 +992,12 @@ KeBugCheckWithTf(IN ULONG BugCheckCode,
             /* Check if we have a frame now */
             if (TrapFrame)
             {
-#ifdef _M_IX86
-                /* Get EIP */
-                Eip = (PVOID)TrapFrame->Eip;
-                KiBugCheckData[3] = (ULONG)Eip;
-#elif defined(_M_PPC)
-                Eip = (PVOID)TrapFrame->Dr0; /* srr0 */
-                KiBugCheckData[3] = (ULONG)Eip;
-#endif
+                /* Get the Program Counter */
+                Pc = (PVOID)KeGetTrapFramePc(TrapFrame);
+                KiBugCheckData[3] = (ULONG_PTR)Pc;
 
                 /* Find out if was in the kernel or drivers */
-                DriverBase = KiPcToFileHeader(Eip,
+                DriverBase = KiPcToFileHeader(Pc,
                                               &LdrEntry,
                                               FALSE,
                                               &IsSystem);
@@ -1019,8 +1027,8 @@ KeBugCheckWithTf(IN ULONG BugCheckCode,
         /* Check if the driver forgot to unlock pages */
         case DRIVER_LEFT_LOCKED_PAGES_IN_PROCESS:
 
-            /* EIP is in parameter 1 */
-            Eip = (PVOID)BugCheckParameter1;
+            /* Program Counter is in parameter 1 */
+            Pc = (PVOID)BugCheckParameter1;
             break;
 
         /* Check if the driver consumed too many PTEs */
@@ -1051,12 +1059,12 @@ KeBugCheckWithTf(IN ULONG BugCheckCode,
     }
     else
     {
-        /* Do we have an EIP? */
-        if (Eip)
+        /* Do we have a Program Counter? */
+        if (Pc)
         {
             /* Dump image name */
             KiDumpParameterImages(AnsiName,
-                                  (PULONG_PTR)&Eip,
+                                  (PULONG_PTR)&Pc,
                                   1,
                                   KeBugCheckUnicodeToAnsi);
         }
@@ -1176,8 +1184,8 @@ KeBugCheckWithTf(IN ULONG BugCheckCode,
         }
         else if (KeBugCheckOwnerRecursionCount > 2)
         {
-            /* Halt the CPU */
-            for (;;) KeArchHaltProcessor();
+            /* Halt execution */
+            while (TRUE);
         }
     }
 
@@ -1190,12 +1198,15 @@ KeBugCheckWithTf(IN ULONG BugCheckCode,
     if (Reboot)
     {
         /* Unload symbols */
-        DbgUnLoadImageSymbols(NULL, NtCurrentProcess(), 0);
+        DbgUnLoadImageSymbols(NULL, (PVOID)MAXULONG_PTR, 0);
         HalReturnToFirmware(HalRebootRoutine);
     }
 
     /* Attempt to break in the debugger (otherwise halt CPU) */
     KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_SECOND);
+
+    /* Shouldn't get here */
+    while (TRUE);
 }
 
 /* PUBLIC FUNCTIONS **********************************************************/
@@ -1416,7 +1427,7 @@ KeEnterKernelDebugger(VOID)
         }
     }
 
-    /* Bugcheck */
+    /* Break in the debugger */
     KiBugCheckDebugBreak(DBG_STATUS_FATAL);
 }