2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/ke/bug.c
5 * PURPOSE: Bugcheck Support
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES ******************************************************************/
13 #include <internal/debug.h>
15 #if defined (ALLOC_PRAGMA)
16 #pragma alloc_text(INIT, KiInitializeBugCheck)
19 /* GLOBALS *******************************************************************/
21 LIST_ENTRY KeBugcheckCallbackListHead
;
22 LIST_ENTRY KeBugcheckReasonCallbackListHead
;
23 KSPIN_LOCK BugCheckCallbackLock
;
24 ULONG KeBugCheckActive
, KeBugCheckOwner
;
25 LONG KeBugCheckOwnerRecursionCount
;
26 PRTL_MESSAGE_RESOURCE_DATA KiBugCodeMessages
;
27 ULONG KeBugCheckCount
= 1;
28 ULONG KiHardwareTrigger
;
29 PUNICODE_STRING KiBugCheckDriver
;
30 ULONG_PTR KiBugCheckData
[5];
32 /* PRIVATE FUNCTIONS *********************************************************/
36 KiRosPrintAddress(PVOID address
)
38 PLIST_ENTRY current_entry
;
39 PLDR_DATA_TABLE_ENTRY current
;
40 extern LIST_ENTRY PsLoadedModuleList
;
41 ULONG_PTR RelativeAddress
;
46 current_entry
= PsLoadedModuleList
.Flink
;
48 while (current_entry
!= &PsLoadedModuleList
)
50 current
= CONTAINING_RECORD(current_entry
,
54 if (address
>= (PVOID
)current
->DllBase
&&
55 address
< (PVOID
)((ULONG_PTR
)current
->DllBase
+
56 current
->SizeOfImage
))
58 RelativeAddress
= (ULONG_PTR
)address
-
59 (ULONG_PTR
)current
->DllBase
;
60 DbgPrint("<%wZ: %x>", ¤t
->FullDllName
, RelativeAddress
);
63 current_entry
= current_entry
->Flink
;
72 KeRosDumpStackFrames(IN PULONG Frame OPTIONAL
,
73 IN ULONG FrameCount OPTIONAL
)
78 /* If the caller didn't ask, assume 32 frames */
79 if (!FrameCount
) FrameCount
= 32;
81 /* Get the current frames */
82 FrameCount
= RtlCaptureStackBackTrace(2, FrameCount
, (PVOID
*)Frames
, NULL
);
84 /* Now loop them (skip the two. One for the dumper, one for the caller) */
85 for (i
= 0; i
< FrameCount
; i
++)
90 /* If we had a custom frame, make sure we've reached it first */
91 if ((Frame
) && (Frame
[1] == Addr
))
102 if (!KeRosPrintAddress((PVOID
)Addr
)) DbgPrint("<%X>", Addr
);
104 /* Go to the next frame */
108 /* Finish the output */
115 KiInitializeBugCheck(VOID
)
117 PRTL_MESSAGE_RESOURCE_DATA BugCheckData
;
118 LDR_RESOURCE_INFO ResourceInfo
;
119 PIMAGE_RESOURCE_DATA_ENTRY ResourceDataEntry
;
121 PLDR_DATA_TABLE_ENTRY LdrEntry
;
123 /* Get the kernel entry */
124 LdrEntry
= CONTAINING_RECORD(KeLoaderBlock
->LoadOrderListHead
.Flink
,
125 LDR_DATA_TABLE_ENTRY
,
128 /* Cache the Bugcheck Message Strings. Prepare the Lookup Data */
129 ResourceInfo
.Type
= 11;
130 ResourceInfo
.Name
= 1;
131 ResourceInfo
.Language
= 9;
134 Status
= LdrFindResource_U(LdrEntry
->DllBase
,
139 /* Make sure it worked */
140 if (NT_SUCCESS(Status
))
142 /* Now actually get a pointer to it */
143 Status
= LdrAccessResource(LdrEntry
->DllBase
,
145 (PVOID
*)&BugCheckData
,
147 if (NT_SUCCESS(Status
)) KiBugCodeMessages
= BugCheckData
;
153 KeGetBugMessageText(IN ULONG BugCheckCode
,
154 OUT PANSI_STRING OutputString OPTIONAL
)
158 ULONG_PTR MessageEntry
;
161 /* Find the message. This code is based on RtlFindMesssage */
162 for (i
= 0; i
< KiBugCodeMessages
->NumberOfBlocks
; i
++)
164 /* Check if the ID Matches */
165 if ((BugCheckCode
>= KiBugCodeMessages
->Blocks
[i
].LowId
) &&
166 (BugCheckCode
<= KiBugCodeMessages
->Blocks
[i
].HighId
))
168 /* Get Offset to Entry */
169 MessageEntry
= KiBugCodeMessages
->Blocks
[i
].OffsetToEntries
+
170 (ULONG_PTR
)KiBugCodeMessages
;
171 IdOffset
= BugCheckCode
- KiBugCodeMessages
->Blocks
[i
].LowId
;
173 /* Get offset to ID */
174 for (i
= 0; i
< IdOffset
; i
++)
176 /* Advance in the Entries */
177 MessageEntry
+= ((PRTL_MESSAGE_RESOURCE_ENTRY
)MessageEntry
)->
181 /* Get the final Code */
182 BugCode
= ((PRTL_MESSAGE_RESOURCE_ENTRY
)MessageEntry
)->Text
;
185 /* Return it in the OutputString */
188 OutputString
->Buffer
= BugCode
;
189 OutputString
->Length
= i
+ 1;
190 OutputString
->MaximumLength
= i
+ 1;
194 /* Direct Output to Screen */
195 InbvDisplayString(BugCode
);
196 InbvDisplayString("\r");
205 KiDoBugCheckCallbacks(VOID
)
207 PKBUGCHECK_CALLBACK_RECORD CurrentRecord
;
208 PLIST_ENTRY ListHead
, NextEntry
, LastEntry
;
211 /* First make sure that the list is Initialized... it might not be */
212 ListHead
= &KeBugcheckCallbackListHead
;
213 if ((ListHead
->Flink
) && (ListHead
->Blink
))
216 LastEntry
= ListHead
;
217 NextEntry
= ListHead
->Flink
;
218 while (NextEntry
!= ListHead
)
221 CurrentRecord
= CONTAINING_RECORD(NextEntry
,
222 KBUGCHECK_CALLBACK_RECORD
,
226 if (CurrentRecord
->Entry
.Blink
!= LastEntry
) return;
227 Checksum
= (ULONG_PTR
)CurrentRecord
->CallbackRoutine
;
228 Checksum
+= (ULONG_PTR
)CurrentRecord
->Buffer
;
229 Checksum
+= (ULONG_PTR
)CurrentRecord
->Length
;
230 Checksum
+= (ULONG_PTR
)CurrentRecord
->Component
;
232 /* Make sure it's inserted and valitdated */
233 if ((CurrentRecord
->State
== BufferInserted
) &&
234 (CurrentRecord
->Checksum
== Checksum
))
236 /* Call the routine */
237 CurrentRecord
->State
= BufferStarted
;
238 (CurrentRecord
->CallbackRoutine
)(CurrentRecord
->Buffer
,
239 CurrentRecord
->Length
);
240 CurrentRecord
->State
= BufferFinished
;
243 /* Go to the next entry */
244 LastEntry
= NextEntry
;
245 NextEntry
= NextEntry
->Flink
;
252 KiBugCheckDebugBreak(IN ULONG StatusCode
)
254 /* If KDBG isn't connected, freeze the CPU, otherwise, break */
255 if (KdDebuggerNotPresent
) for (;;) Ke386HaltProcessor();
256 DbgBreakPointWithStatus(StatusCode
);
261 KiPcToFileHeader(IN PVOID Eip
,
262 OUT PLDR_DATA_TABLE_ENTRY
*LdrEntry
,
263 IN BOOLEAN DriversOnly
,
264 OUT PBOOLEAN InKernel
)
267 PVOID ImageBase
, EipBase
= NULL
;
268 PLDR_DATA_TABLE_ENTRY Entry
;
269 PLIST_ENTRY ListHead
, NextEntry
;
270 extern LIST_ENTRY PsLoadedModuleList
;
275 /* Set list pointers and make sure it's valid */
276 ListHead
= &PsLoadedModuleList
;
277 NextEntry
= ListHead
->Flink
;
281 while (NextEntry
!= ListHead
)
286 /* Check if this is a kernel entry and we only want drivers */
287 if ((i
<= 2) && (DriversOnly
== TRUE
))
290 NextEntry
= NextEntry
->Flink
;
294 /* Get the loader entry */
295 Entry
= CONTAINING_RECORD(NextEntry
,
296 LDR_DATA_TABLE_ENTRY
,
299 /* Move to the next entry */
300 NextEntry
= NextEntry
->Flink
;
301 ImageBase
= Entry
->DllBase
;
303 /* Check if this is the right one */
304 if (((ULONG_PTR
)Eip
>= (ULONG_PTR
)Entry
->DllBase
) &&
305 ((ULONG_PTR
)Eip
< ((ULONG_PTR
)Entry
->DllBase
+ Entry
->SizeOfImage
)))
307 /* Return this entry */
311 /* Check if this was a kernel or HAL entry */
312 if (i
<= 2) *InKernel
= TRUE
;
318 /* Return the base address */
324 KeBugCheckUnicodeToAnsi(IN PUNICODE_STRING Unicode
,
332 /* Set length and normalize it */
333 i
= Unicode
->Length
/ sizeof(WCHAR
);
334 i
= min(i
, Length
- 1);
336 /* Set source and destination, and copy */
337 pw
= Unicode
->Buffer
;
339 while (i
--) *p
++ = (CHAR
)*pw
++;
341 /* Null terminate and return */
348 KiDumpParameterImages(IN PCHAR Message
,
349 IN PULONG_PTR Parameters
,
350 IN ULONG ParameterCount
,
351 IN PKE_BUGCHECK_UNICODE_TO_ANSI ConversionRoutine
)
355 PLDR_DATA_TABLE_ENTRY LdrEntry
;
357 PUNICODE_STRING DriverName
;
359 PIMAGE_NT_HEADERS NtHeader
;
361 BOOLEAN FirstRun
= TRUE
;
363 /* Loop parameters */
364 for (i
= 0; i
< ParameterCount
; i
++)
366 /* Get the base for this parameter */
367 ImageBase
= KiPcToFileHeader((PVOID
)Parameters
[i
],
373 /* Driver wasn't found, check for unloaded driver */
374 DriverName
= NULL
; // FIXME: ROS can't
375 if (!DriverName
) continue;
377 /* Convert the driver name */
378 ImageBase
= (PVOID
)Parameters
[i
];
379 ConversionRoutine(DriverName
, AnsiName
, sizeof(AnsiName
));
383 /* Get the NT Headers and Timestamp */
384 NtHeader
= RtlImageNtHeader(LdrEntry
->DllBase
);
385 TimeStamp
= NtHeader
->FileHeader
.TimeDateStamp
;
387 /* Convert the driver name */
388 DriverName
= &LdrEntry
->BaseDllName
;
389 ConversionRoutine(&LdrEntry
->BaseDllName
,
394 /* Format driver name */
396 "%s** %12s - Address %p base at %p, DateStamp %08lx\n",
397 FirstRun
? "\r\n*":"*",
399 (PVOID
)Parameters
[i
],
403 /* Check if we only had one parameter */
404 if (ParameterCount
<= 1)
406 /* Then just save the name */
407 KiBugCheckDriver
= DriverName
;
411 /* Otherwise, display the message */
412 InbvDisplayString(Message
);
422 KiDisplayBlueScreen(IN ULONG MessageId
,
423 IN BOOLEAN IsHardError
,
424 IN PCHAR HardErrCaption OPTIONAL
,
425 IN PCHAR HardErrMessage OPTIONAL
,
430 /* Check if bootvid is installed */
431 if (InbvIsBootDriverInstalled())
433 /* Acquire ownership and reset the display */
434 InbvAcquireDisplayOwnership();
437 /* Display blue screen */
438 InbvSolidColorFill(0, 0, 639, 479, 4);
439 InbvSetTextColor(15);
440 InbvInstallDisplayStringFilter(NULL
);
441 InbvEnableDisplayString(TRUE
);
442 InbvSetScrollRegion(0, 0, 639, 479);
445 /* Check if this is a hard error */
448 /* Display caption and message */
449 if (HardErrCaption
) InbvDisplayString(HardErrCaption
);
450 if (HardErrMessage
) InbvDisplayString(HardErrMessage
);
453 /* Begin the display */
454 InbvDisplayString("\r\n");
456 /* Print out initial message */
457 KeGetBugMessageText(BUGCHECK_MESSAGE_INTRO
, NULL
);
458 InbvDisplayString("\r\n\r\n");
460 /* Check if we have a driver */
461 if (KiBugCheckDriver
)
463 /* Print out into to driver name */
464 KeGetBugMessageText(BUGCODE_ID_DRIVER
, NULL
);
466 /* Convert and print out driver name */
467 KeBugCheckUnicodeToAnsi(KiBugCheckDriver
, AnsiName
, sizeof(AnsiName
));
468 InbvDisplayString(" ");
469 InbvDisplayString(AnsiName
);
470 InbvDisplayString("\r\n\r\n");
473 /* Check if this is the generic message */
474 if (MessageId
== BUGCODE_PSS_MESSAGE
)
476 /* It is, so get the bug code string as well */
477 KeGetBugMessageText(KiBugCheckData
[0], NULL
);
478 InbvDisplayString("\r\n\r\n");
481 /* Print second introduction message */
482 KeGetBugMessageText(PSS_MESSAGE_INTRO
, NULL
);
483 InbvDisplayString("\r\n\r\n");
485 /* Get the bug code string */
486 KeGetBugMessageText(MessageId
, NULL
);
487 InbvDisplayString("\r\n\r\n");
489 /* Print message for technical information */
490 KeGetBugMessageText(BUGCHECK_TECH_INFO
, NULL
);
492 /* Show the techincal Data */
494 "\r\n\r\n*** STOP: 0x%08lX (0x%p,0x%p,0x%p,0x%p)\r\n\r\n",
496 (PVOID
)KiBugCheckData
[1],
497 (PVOID
)KiBugCheckData
[2],
498 (PVOID
)KiBugCheckData
[3],
499 (PVOID
)KiBugCheckData
[4]);
500 InbvDisplayString(AnsiName
);
502 /* Check if we have a driver*/
503 if (KiBugCheckDriver
)
505 /* Display technical driver data */
506 InbvDisplayString(Message
);
510 /* Dump parameter information */
511 KiDumpParameterImages(Message
,
512 (PVOID
)&KiBugCheckData
[1],
514 KeBugCheckUnicodeToAnsi
);
520 KeBugCheckWithTf(IN ULONG BugCheckCode
,
521 IN ULONG_PTR BugCheckParameter1
,
522 IN ULONG_PTR BugCheckParameter2
,
523 IN ULONG_PTR BugCheckParameter3
,
524 IN ULONG_PTR BugCheckParameter4
,
525 IN PKTRAP_FRAME TrapFrame
)
527 PKPRCB Prcb
= KeGetCurrentPrcb();
531 BOOLEAN IsSystem
, IsHardError
= FALSE
, Reboot
= FALSE
;
532 PCHAR HardErrCaption
= NULL
, HardErrMessage
= NULL
;
533 PVOID Eip
= NULL
, Memory
;
535 PLDR_DATA_TABLE_ENTRY LdrEntry
;
536 PULONG_PTR HardErrorParameters
;
542 /* Set active bugcheck */
543 KeBugCheckActive
= TRUE
;
544 KiBugCheckDriver
= NULL
;
546 /* Check if this is power failure simulation */
547 if (BugCheckCode
== POWER_FAILURE_SIMULATE
)
549 /* Call the Callbacks and reboot */;
550 KiDoBugCheckCallbacks();
551 HalReturnToFirmware(HalRebootRoutine
);
554 /* Save the IRQL and set hardware trigger */
555 Prcb
->DebuggerSavedIRQL
= KeGetCurrentIrql();
556 InterlockedIncrement((PLONG
)&KiHardwareTrigger
);
558 /* Capture the CPU Context */
559 RtlCaptureContext(&Prcb
->ProcessorState
.ContextFrame
);
560 KiSaveProcessorControlState(&Prcb
->ProcessorState
);
561 Context
= Prcb
->ProcessorState
.ContextFrame
;
563 /* FIXME: Call the Watchdog if it's registered */
565 /* Check which bugcode this is */
566 switch (BugCheckCode
)
568 /* These bug checks already have detailed messages, keep them */
569 case UNEXPECTED_KERNEL_MODE_TRAP
:
570 case DRIVER_CORRUPTED_EXPOOL
:
571 case ACPI_BIOS_ERROR
:
572 case ACPI_BIOS_FATAL_ERROR
:
573 case THREAD_STUCK_IN_DEVICE_DRIVER
:
575 case FAT_FILE_SYSTEM
:
576 case NO_MORE_SYSTEM_PTES
:
577 case INACCESSIBLE_BOOT_DEVICE
:
579 /* Keep the same code */
580 MessageId
= BugCheckCode
;
583 /* Check if this is a kernel-mode exception */
584 case KERNEL_MODE_EXCEPTION_NOT_HANDLED
:
585 //case SYSTEM_THREAD_EXCEPTION_NOT_HANDLED:
586 case KMODE_EXCEPTION_NOT_HANDLED
:
588 /* Use the generic text message */
589 MessageId
= KMODE_EXCEPTION_NOT_HANDLED
;
592 /* File-system errors */
593 case NTFS_FILE_SYSTEM
:
595 /* Use the generic message for FAT */
596 MessageId
= FAT_FILE_SYSTEM
;
599 /* Check if this is a coruption of the Mm's Pool */
600 case DRIVER_CORRUPTED_MMPOOL
:
602 /* Use generic corruption message */
603 MessageId
= DRIVER_CORRUPTED_EXPOOL
;
606 /* Check if this is a signature check failure */
607 case STATUS_SYSTEM_IMAGE_BAD_SIGNATURE
:
609 /* Use the generic corruption message */
610 MessageId
= BUGCODE_PSS_MESSAGE_SIGNATURE
;
613 /* All other codes */
616 /* Use the default bugcheck message */
617 MessageId
= BUGCODE_PSS_MESSAGE
;
621 /* Save bugcheck data */
622 KiBugCheckData
[0] = BugCheckCode
;
623 KiBugCheckData
[1] = BugCheckParameter1
;
624 KiBugCheckData
[2] = BugCheckParameter2
;
625 KiBugCheckData
[3] = BugCheckParameter3
;
626 KiBugCheckData
[4] = BugCheckParameter4
;
628 /* Now check what bugcheck this is */
629 switch (BugCheckCode
)
631 /* Invalid access to R/O memory or Unhandled KM Exception */
632 case KERNEL_MODE_EXCEPTION_NOT_HANDLED
:
633 case ATTEMPTED_WRITE_TO_READONLY_MEMORY
:
634 case ATTEMPTED_EXECUTE_OF_NOEXECUTE_MEMORY
:
636 /* Check if we have a trap frame */
639 /* Use parameter 3 as a trap frame, if it exists */
640 if (BugCheckParameter3
) TrapFrame
= (PVOID
)BugCheckParameter3
;
643 /* Check if we got one now and if we need to get EIP */
645 (BugCheckCode
!= KERNEL_MODE_EXCEPTION_NOT_HANDLED
))
648 Eip
= (PVOID
)TrapFrame
->Eip
;
653 case IRQL_NOT_LESS_OR_EQUAL
:
656 * The NT kernel has 3 special sections:
657 * MISYSPTE, POOLMI and POOLCODE. The bug check code can
658 * determine in which of these sections this bugcode happened
659 * and provide a more detailed analysis. For now, we don't.
662 /* Eip is in parameter 4 */
663 Eip
= (PVOID
)BugCheckParameter4
;
665 /* Get the driver base */
666 DriverBase
= KiPcToFileHeader(Eip
, &LdrEntry
, FALSE
, &IsSystem
);
670 * The error happened inside the kernel or HAL.
671 * Get the memory address that was being referenced.
673 Memory
= (PVOID
)BugCheckParameter1
;
675 /* Find to which driver it belongs */
676 DriverBase
= KiPcToFileHeader(Memory
,
682 /* Get the driver name and update the bug code */
683 KiBugCheckDriver
= &LdrEntry
->BaseDllName
;
684 KiBugCheckData
[0] = DRIVER_PORTION_MUST_BE_NONPAGED
;
688 /* Find the driver that unloaded at this address */
689 KiBugCheckDriver
= NULL
; // FIXME: ROS can't locate
691 /* Check if the cause was an unloaded driver */
692 if (KiBugCheckDriver
)
694 /* Update bug check code */
696 SYSTEM_SCAN_AT_RAISED_IRQL_CAUGHT_IMPROPER_DRIVER_UNLOAD
;
702 /* Update the bug check code */
703 KiBugCheckData
[0] = DRIVER_IRQL_NOT_LESS_OR_EQUAL
;
706 /* Clear EIP so we don't look it up later */
711 case FATAL_UNHANDLED_HARD_ERROR
:
713 /* Copy bug check data from hard error */
714 HardErrorParameters
= (PULONG_PTR
)BugCheckParameter2
;
715 KiBugCheckData
[0] = BugCheckParameter1
;
716 KiBugCheckData
[1] = HardErrorParameters
[0];
717 KiBugCheckData
[2] = HardErrorParameters
[1];
718 KiBugCheckData
[3] = HardErrorParameters
[2];
719 KiBugCheckData
[4] = HardErrorParameters
[3];
721 /* Remember that this is hard error and set the caption/message */
723 HardErrCaption
= (PCHAR
)BugCheckParameter3
;
724 HardErrMessage
= (PCHAR
)BugCheckParameter4
;
728 case PAGE_FAULT_IN_NONPAGED_AREA
:
730 /* Assume no driver */
733 /* Check if we have a trap frame */
736 /* We don't, use parameter 3 if possible */
737 if (BugCheckParameter3
) TrapFrame
= (PVOID
)BugCheckParameter3
;
740 /* Check if we have a frame now */
744 Eip
= (PVOID
)TrapFrame
->Eip
;
745 KiBugCheckData
[3] = (ULONG
)Eip
;
747 /* Find out if was in the kernel or drivers */
748 DriverBase
= KiPcToFileHeader(Eip
,
755 * Now we should check if this happened in:
756 * 1) Special Pool 2) Free Special Pool 3) Session Pool
757 * and update the bugcheck code appropriately.
760 /* Check if we didn't have a driver base */
763 /* Find the driver that unloaded at this address */
764 KiBugCheckDriver
= NULL
; // FIXME: ROS can't locate
766 /* Check if the cause was an unloaded driver */
767 if (KiBugCheckDriver
)
770 DRIVER_UNLOADED_WITHOUT_CANCELLING_PENDING_OPERATIONS
;
775 /* Check if the driver forgot to unlock pages */
776 case DRIVER_LEFT_LOCKED_PAGES_IN_PROCESS
:
778 /* EIP is in parameter 1 */
779 Eip
= (PVOID
)BugCheckParameter1
;
782 /* Check if the driver consumed too many PTEs */
783 case DRIVER_USED_EXCESSIVE_PTES
:
785 /* Loader entry is in parameter 1 */
786 LdrEntry
= (PVOID
)BugCheckParameter1
;
787 KiBugCheckDriver
= &LdrEntry
->BaseDllName
;
790 /* Check if the driver has a stuck thread */
791 case THREAD_STUCK_IN_DEVICE_DRIVER
:
793 /* The name is in Parameter 3 */
794 KiBugCheckDriver
= (PVOID
)BugCheckParameter3
;
802 /* Do we have a driver name? */
803 if (KiBugCheckDriver
)
805 /* Convert it to ANSI */
806 KeBugCheckUnicodeToAnsi(KiBugCheckDriver
, AnsiName
, sizeof(AnsiName
));
810 /* Do we have an EIP? */
813 /* Dump image name */
814 KiDumpParameterImages(AnsiName
,
817 KeBugCheckUnicodeToAnsi
);
821 /* Check if we need to save the context for KD */
823 /* Check if a debugger is connected */
824 if ((BugCheckCode
!= MANUALLY_INITIATED_CRASH
) && (KdDebuggerEnabled
))
826 /* Crash on the debugger console */
827 DbgPrint("\n*** Fatal System Error: 0x%08lx\n"
828 " (0x%p,0x%p,0x%p,0x%p)\n\n",
835 /* Check if the debugger isn't currently connected */
836 if (!KdDebuggerNotPresent
)
838 /* Check if we have a driver to blame */
839 if (KiBugCheckDriver
)
842 DbgPrint("Driver at fault: %s.\n", AnsiName
);
845 /* Check if this was a hard error */
848 /* Print caption and message */
849 if (HardErrCaption
) DbgPrint(HardErrCaption
);
850 if (HardErrMessage
) DbgPrint(HardErrMessage
);
853 /* Break in the debugger */
854 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_FIRST
);
860 * Ok, so debugging is enabled, but KDBG isn't there.
861 * We'll manually dump the stack for the user.
863 KeRosDumpStackFrames(NULL
, 0);
867 /* Raise IRQL to HIGH_LEVEL */
869 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
871 /* ROS HACK: Unlock the Kernel Address Space if we own it */
872 if (KernelAddressSpaceLock
.Owner
== KeGetCurrentThread())
874 MmUnlockAddressSpace(MmGetKernelAddressSpace());
877 /* Avoid recursion */
878 if (!InterlockedDecrement((PLONG
)&KeBugCheckCount
))
881 /* Set CPU that is bug checking now */
882 KeBugCheckOwner
= Prcb
->Number
;
884 /* Freeze the other CPUs */
885 for (i
= 0; i
< KeNumberProcessors
; i
++)
887 if (i
!= (LONG
)KeGetCurrentProcessorNumber())
889 /* Send the IPI and give them one second to catch up */
890 KiIpiSendRequest(1 << i
, IPI_FREEZE
);
891 KeStallExecutionProcessor(1000000);
896 /* Display the BSOD */
897 KiDisplayBlueScreen(MessageId
,
903 /* Check if the debugger is disabled but we can enable it */
904 //if (!(KdDebuggerEnabled) && !(KdPitchDebugger))
907 //KdEnableDebuggerWithLock(FALSE);
911 /* Otherwise, print the last line */
912 InbvDisplayString("\r\n");
915 /* Save the context */
916 Prcb
->ProcessorState
.ContextFrame
= Context
;
918 /* FIXME: Support Triage Dump */
920 /* Write the crash dump */
921 MmDumpToPagingFile(KiBugCheckData
[4],
930 /* Increase recursion count */
931 KeBugCheckOwnerRecursionCount
++;
932 if (KeBugCheckOwnerRecursionCount
== 2)
934 /* Break in the debugger */
935 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_SECOND
);
937 else if (KeBugCheckOwnerRecursionCount
> 2)
940 for (;;) Ke386HaltProcessor();
944 /* Call the Callbacks */
945 KiDoBugCheckCallbacks();
947 /* FIXME: Call Watchdog if enabled */
949 /* Check if we have to reboot */
953 DbgUnLoadImageSymbols(NULL
, NtCurrentProcess(), 0);
954 HalReturnToFirmware(HalRebootRoutine
);
957 /* Attempt to break in the debugger (otherwise halt CPU) */
958 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_SECOND
);
961 /* PUBLIC FUNCTIONS **********************************************************/
968 KeDeregisterBugCheckCallback(IN PKBUGCHECK_CALLBACK_RECORD CallbackRecord
)
971 BOOLEAN Status
= FALSE
;
973 /* Raise IRQL to High */
974 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
976 /* Check the Current State */
977 if (CallbackRecord
->State
== BufferInserted
)
979 /* Reset state and remove from list */
980 CallbackRecord
->State
= BufferEmpty
;
981 RemoveEntryList(&CallbackRecord
->Entry
);
985 /* Lower IRQL and return */
986 KeLowerIrql(OldIrql
);
995 KeDeregisterBugCheckReasonCallback(
996 IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord
)
999 BOOLEAN Status
= FALSE
;
1001 /* Raise IRQL to High */
1002 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1004 /* Check the Current State */
1005 if (CallbackRecord
->State
== BufferInserted
)
1007 /* Reset state and remove from list */
1008 CallbackRecord
->State
= BufferEmpty
;
1009 RemoveEntryList(&CallbackRecord
->Entry
);
1013 /* Lower IRQL and return */
1014 KeLowerIrql(OldIrql
);
1023 KeRegisterBugCheckCallback(IN PKBUGCHECK_CALLBACK_RECORD CallbackRecord
,
1024 IN PKBUGCHECK_CALLBACK_ROUTINE CallbackRoutine
,
1027 IN PUCHAR Component
)
1030 BOOLEAN Status
= FALSE
;
1032 /* Raise IRQL to High */
1033 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1035 /* Check the Current State first so we don't double-register */
1036 if (CallbackRecord
->State
== BufferEmpty
)
1038 /* Set the Callback Settings and insert into the list */
1039 CallbackRecord
->Length
= Length
;
1040 CallbackRecord
->Buffer
= Buffer
;
1041 CallbackRecord
->Component
= Component
;
1042 CallbackRecord
->CallbackRoutine
= CallbackRoutine
;
1043 CallbackRecord
->State
= BufferInserted
;
1044 InsertTailList(&KeBugcheckCallbackListHead
, &CallbackRecord
->Entry
);
1048 /* Lower IRQL and return */
1049 KeLowerIrql(OldIrql
);
1058 KeRegisterBugCheckReasonCallback(
1059 IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord
,
1060 IN PKBUGCHECK_REASON_CALLBACK_ROUTINE CallbackRoutine
,
1061 IN KBUGCHECK_CALLBACK_REASON Reason
,
1062 IN PUCHAR Component
)
1065 BOOLEAN Status
= FALSE
;
1067 /* Raise IRQL to High */
1068 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1070 /* Check the Current State first so we don't double-register */
1071 if (CallbackRecord
->State
== BufferEmpty
)
1073 /* Set the Callback Settings and insert into the list */
1074 CallbackRecord
->Component
= Component
;
1075 CallbackRecord
->CallbackRoutine
= CallbackRoutine
;
1076 CallbackRecord
->State
= BufferInserted
;
1077 CallbackRecord
->Reason
= Reason
;
1078 InsertTailList(&KeBugcheckReasonCallbackListHead
,
1079 &CallbackRecord
->Entry
);
1083 /* Lower IRQL and return */
1084 KeLowerIrql(OldIrql
);
1093 KeBugCheckEx(IN ULONG BugCheckCode
,
1094 IN ULONG_PTR BugCheckParameter1
,
1095 IN ULONG_PTR BugCheckParameter2
,
1096 IN ULONG_PTR BugCheckParameter3
,
1097 IN ULONG_PTR BugCheckParameter4
)
1099 /* Call the internal API */
1100 KeBugCheckWithTf(BugCheckCode
,
1113 KeBugCheck(ULONG BugCheckCode
)
1115 /* Call the internal API */
1116 KeBugCheckWithTf(BugCheckCode
, 0, 0, 0, 0, NULL
);