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 /* Bugzilla Reporting */
33 UNICODE_STRING KeRosProcessorName
, KeRosBiosDate
, KeRosBiosVersion
;
34 UNICODE_STRING KeRosVideoBiosDate
, KeRosVideoBiosVersion
;
36 /* PRIVATE FUNCTIONS *********************************************************/
40 KiPcToFileHeader(IN PVOID Eip
,
41 OUT PLDR_DATA_TABLE_ENTRY
*LdrEntry
,
42 IN BOOLEAN DriversOnly
,
43 OUT PBOOLEAN InKernel
)
46 PVOID ImageBase
, EipBase
= NULL
;
47 PLDR_DATA_TABLE_ENTRY Entry
;
48 PLIST_ENTRY ListHead
, NextEntry
;
50 /* Check which list we should use */
51 ListHead
= (KeLoaderBlock
) ? &KeLoaderBlock
->LoadOrderListHead
:
57 /* Set list pointers and make sure it's valid */
58 NextEntry
= ListHead
->Flink
;
62 while (NextEntry
!= ListHead
)
67 /* Check if this is a kernel entry and we only want drivers */
68 if ((i
<= 2) && (DriversOnly
== TRUE
))
71 NextEntry
= NextEntry
->Flink
;
75 /* Get the loader entry */
76 Entry
= CONTAINING_RECORD(NextEntry
,
80 /* Move to the next entry */
81 NextEntry
= NextEntry
->Flink
;
82 ImageBase
= Entry
->DllBase
;
84 /* Check if this is the right one */
85 if (((ULONG_PTR
)Eip
>= (ULONG_PTR
)Entry
->DllBase
) &&
86 ((ULONG_PTR
)Eip
< ((ULONG_PTR
)Entry
->DllBase
+ Entry
->SizeOfImage
)))
88 /* Return this entry */
92 /* Check if this was a kernel or HAL entry */
93 if (i
<= 2) *InKernel
= TRUE
;
99 /* Return the base address */
105 KiRosPrintAddress(PVOID address
)
107 PLIST_ENTRY current_entry
;
108 PLDR_DATA_TABLE_ENTRY current
;
109 extern LIST_ENTRY PsLoadedModuleList
;
110 ULONG_PTR RelativeAddress
;
115 current_entry
= PsLoadedModuleList
.Flink
;
117 while (current_entry
!= &PsLoadedModuleList
)
119 current
= CONTAINING_RECORD(current_entry
,
120 LDR_DATA_TABLE_ENTRY
,
123 if (address
>= (PVOID
)current
->DllBase
&&
124 address
< (PVOID
)((ULONG_PTR
)current
->DllBase
+
125 current
->SizeOfImage
))
127 RelativeAddress
= (ULONG_PTR
)address
-
128 (ULONG_PTR
)current
->DllBase
;
129 DbgPrint("<%wZ: %x>", ¤t
->FullDllName
, RelativeAddress
);
132 current_entry
= current_entry
->Flink
;
141 KiRosPcToUserFileHeader(IN PVOID Eip
,
142 OUT PLDR_DATA_TABLE_ENTRY
*LdrEntry
)
144 PVOID ImageBase
, EipBase
= NULL
;
145 PLDR_DATA_TABLE_ENTRY Entry
;
146 PLIST_ENTRY ListHead
, NextEntry
;
149 * We know this is valid because we should only be called after a
150 * succesfull address from RtlWalkFrameChain for UserMode, which
151 * validates everything for us.
153 ListHead
= &KeGetCurrentThread()->
154 Teb
->ProcessEnvironmentBlock
->Ldr
->InLoadOrderModuleList
;
156 /* Set list pointers and make sure it's valid */
157 NextEntry
= ListHead
->Flink
;
161 while (NextEntry
!= ListHead
)
163 /* Get the loader entry */
164 Entry
= CONTAINING_RECORD(NextEntry
,
165 LDR_DATA_TABLE_ENTRY
,
168 /* Move to the next entry */
169 NextEntry
= NextEntry
->Flink
;
170 ImageBase
= Entry
->DllBase
;
172 /* Check if this is the right one */
173 if (((ULONG_PTR
)Eip
>= (ULONG_PTR
)Entry
->DllBase
) &&
174 ((ULONG_PTR
)Eip
< ((ULONG_PTR
)Entry
->DllBase
+ Entry
->SizeOfImage
)))
176 /* Return this entry */
184 /* Return the base address */
190 KeRosCaptureUserStackBackTrace(IN ULONG FramesToSkip
,
191 IN ULONG FramesToCapture
,
192 OUT PVOID
*BackTrace
,
193 OUT PULONG BackTraceHash OPTIONAL
)
195 PVOID Frames
[2 * 64];
199 /* Skip a frame for the caller */
202 /* Don't go past the limit */
203 if ((FramesToCapture
+ FramesToSkip
) >= 128) return 0;
205 /* Do the back trace */
206 FrameCount
= RtlWalkFrameChain(Frames
, FramesToCapture
+ FramesToSkip
, 1);
208 /* Make sure we're not skipping all of them */
209 if (FrameCount
<= FramesToSkip
) return 0;
211 /* Loop all the frames */
212 for (i
= 0; i
< FramesToCapture
; i
++)
214 /* Don't go past the limit */
215 if ((FramesToSkip
+ i
) >= FrameCount
) break;
217 /* Save this entry and hash it */
218 BackTrace
[i
] = Frames
[FramesToSkip
+ i
];
219 Hash
+= PtrToUlong(BackTrace
[i
]);
223 if (BackTraceHash
) *BackTraceHash
= Hash
;
225 /* Clear the other entries and return count */
226 RtlFillMemoryUlong(Frames
, 128, 0);
232 KeRosDumpStackFrames(IN PULONG Frame OPTIONAL
,
233 IN ULONG FrameCount OPTIONAL
)
238 PLDR_DATA_TABLE_ENTRY LdrEntry
;
240 /* If the caller didn't ask, assume 32 frames */
241 if (!FrameCount
|| FrameCount
> 32) FrameCount
= 32;
243 /* Get the current frames */
244 FrameCount
= RtlCaptureStackBackTrace(2, FrameCount
, (PVOID
*)Frames
, NULL
);
246 /* Now loop them (skip the two. One for the dumper, one for the caller) */
247 for (i
= 0; i
< FrameCount
; i
++)
252 /* If we had a custom frame, make sure we've reached it first */
253 if ((Frame
) && (Frame
[1] == Addr
))
259 /* Skip this entry */
263 /* Get the base for this file */
264 if (KiPcToFileHeader((PVOID
)Addr
, &LdrEntry
, FALSE
, &InSystem
))
266 /* Print out the module name */
267 Addr
-= (ULONG_PTR
)LdrEntry
->DllBase
;
268 DbgPrint("<%wZ: %x>", &LdrEntry
->FullDllName
, Addr
);
272 /* Print only the address */
273 DbgPrint("<%x>", Addr
);
276 /* Go to the next frame */
280 /* Get the current frames */
281 FrameCount
= KeRosCaptureUserStackBackTrace(-1, 32, (PVOID
*)Frames
, NULL
);
284 for (i
= 0; i
< FrameCount
; i
++)
289 /* Get the base for this file */
290 if (KiRosPcToUserFileHeader((PVOID
)Addr
, &LdrEntry
))
292 /* Print out the module name */
294 if (!KdbSymPrintAddress((PVOID
)Addr
))
297 /* Fall back to usual printing */
298 Addr
-= (ULONG_PTR
)LdrEntry
->DllBase
;
299 DbgPrint("<%wZ: %x>", &LdrEntry
->FullDllName
, Addr
);
304 /* Print only the address */
305 DbgPrint("<%x>", Addr
);
308 /* Go to the next frame */
312 /* Finish the output */
318 KeRosDumpTriageForBugZillaReport(VOID
)
321 extern BOOLEAN KiFastSystemCallDisable
, KiSMTProcessorsPresent
;
322 extern ULONG KeI386MachineType
, MxcsrFeatureMask
;
323 extern BOOLEAN Ke386Pae
, Ke386NoExecute
;
325 DbgPrint("ReactOS has crashed! Please go to http://www.reactos.org/bugzilla/enter_bug.cgi to file a bug!\n");
326 DbgPrint("\nHardware Information\n");
327 DbgPrint("Processor Architecture: %d\n"
329 "System Call Disabled: %d\n"
332 "MXCsr Feature Mask: %d\n"
339 "Active Processors: %d\n"
340 "Pentium LOCK Bug: %d\n"
341 "Hyperthreading: %d\n"
342 "CPU Manufacturer: %s\n"
350 "BIOS Version: %wZ\n"
351 "Video BIOS Date: %wZ\n"
352 "Video BIOS Version: %wZ\n"
354 KeProcessorArchitecture
,
356 KiFastSystemCallDisable
,
367 KiI386PentiumLockErrataPresent
,
368 KiSMTProcessorsPresent
,
369 KeGetCurrentPrcb()->VendorString
,
371 KeGetCurrentPrcb()->CpuID
,
372 KeGetCurrentPrcb()->CpuType
,
373 KeGetCurrentPrcb()->CpuStep
,
374 KeGetCurrentPrcb()->MHz
,
375 ((PKIPCR
)KeGetPcr())->SecondLevelCacheSize
,
379 &KeRosVideoBiosVersion
,
380 MmNumberOfPhysicalPages
* PAGE_SIZE
);
387 KiInitializeBugCheck(VOID
)
389 PRTL_MESSAGE_RESOURCE_DATA BugCheckData
;
390 LDR_RESOURCE_INFO ResourceInfo
;
391 PIMAGE_RESOURCE_DATA_ENTRY ResourceDataEntry
;
393 PLDR_DATA_TABLE_ENTRY LdrEntry
;
395 /* Get the kernel entry */
396 LdrEntry
= CONTAINING_RECORD(KeLoaderBlock
->LoadOrderListHead
.Flink
,
397 LDR_DATA_TABLE_ENTRY
,
400 /* Cache the Bugcheck Message Strings. Prepare the Lookup Data */
401 ResourceInfo
.Type
= 11;
402 ResourceInfo
.Name
= 1;
403 ResourceInfo
.Language
= 9;
406 Status
= LdrFindResource_U(LdrEntry
->DllBase
,
411 /* Make sure it worked */
412 if (NT_SUCCESS(Status
))
414 /* Now actually get a pointer to it */
415 Status
= LdrAccessResource(LdrEntry
->DllBase
,
417 (PVOID
*)&BugCheckData
,
419 if (NT_SUCCESS(Status
)) KiBugCodeMessages
= BugCheckData
;
425 KeGetBugMessageText(IN ULONG BugCheckCode
,
426 OUT PANSI_STRING OutputString OPTIONAL
)
430 ULONG_PTR MessageEntry
;
432 BOOLEAN Result
= FALSE
;
434 /* Make sure we're not bugchecking too early */
435 if (!KiBugCodeMessages
) return Result
;
437 /* Find the message. This code is based on RtlFindMesssage */
438 for (i
= 0; i
< KiBugCodeMessages
->NumberOfBlocks
; i
++)
440 /* Check if the ID Matches */
441 if ((BugCheckCode
>= KiBugCodeMessages
->Blocks
[i
].LowId
) &&
442 (BugCheckCode
<= KiBugCodeMessages
->Blocks
[i
].HighId
))
444 /* Get Offset to Entry */
445 MessageEntry
= KiBugCodeMessages
->Blocks
[i
].OffsetToEntries
+
446 (ULONG_PTR
)KiBugCodeMessages
;
447 IdOffset
= BugCheckCode
- KiBugCodeMessages
->Blocks
[i
].LowId
;
449 /* Get offset to ID */
450 for (i
= 0; i
< IdOffset
; i
++)
452 /* Advance in the Entries */
453 MessageEntry
+= ((PRTL_MESSAGE_RESOURCE_ENTRY
)MessageEntry
)->
457 /* Get the final Code */
458 BugCode
= ((PRTL_MESSAGE_RESOURCE_ENTRY
)MessageEntry
)->Text
;
461 /* Handle newlines */
462 while ((i
> 0) && ((BugCode
[i
] == '\n') ||
463 (BugCode
[i
] == '\r') ||
464 (BugCode
[i
] == ANSI_NULL
)))
466 /* Check if we have a string to return */
467 if (!OutputString
) BugCode
[i
] = ANSI_NULL
;
471 /* Check if caller wants an output string */
474 /* Return it in the OutputString */
475 OutputString
->Buffer
= BugCode
;
476 OutputString
->Length
= (USHORT
)i
+ 1;
477 OutputString
->MaximumLength
= (USHORT
)i
+ 1;
481 /* Direct Output to Screen */
482 InbvDisplayString(BugCode
);
483 InbvDisplayString("\r");
492 /* Return the result */
498 KiDoBugCheckCallbacks(VOID
)
500 PKBUGCHECK_CALLBACK_RECORD CurrentRecord
;
501 PLIST_ENTRY ListHead
, NextEntry
, LastEntry
;
504 /* First make sure that the list is Initialized... it might not be */
505 ListHead
= &KeBugcheckCallbackListHead
;
506 if ((ListHead
->Flink
) && (ListHead
->Blink
))
509 LastEntry
= ListHead
;
510 NextEntry
= ListHead
->Flink
;
511 while (NextEntry
!= ListHead
)
514 CurrentRecord
= CONTAINING_RECORD(NextEntry
,
515 KBUGCHECK_CALLBACK_RECORD
,
519 if (CurrentRecord
->Entry
.Blink
!= LastEntry
) return;
520 Checksum
= (ULONG_PTR
)CurrentRecord
->CallbackRoutine
;
521 Checksum
+= (ULONG_PTR
)CurrentRecord
->Buffer
;
522 Checksum
+= (ULONG_PTR
)CurrentRecord
->Length
;
523 Checksum
+= (ULONG_PTR
)CurrentRecord
->Component
;
525 /* Make sure it's inserted and valitdated */
526 if ((CurrentRecord
->State
== BufferInserted
) &&
527 (CurrentRecord
->Checksum
== Checksum
))
529 /* Call the routine */
530 CurrentRecord
->State
= BufferStarted
;
531 (CurrentRecord
->CallbackRoutine
)(CurrentRecord
->Buffer
,
532 CurrentRecord
->Length
);
533 CurrentRecord
->State
= BufferFinished
;
536 /* Go to the next entry */
537 LastEntry
= NextEntry
;
538 NextEntry
= NextEntry
->Flink
;
545 KiBugCheckDebugBreak(IN ULONG StatusCode
)
547 /* If KDBG isn't connected, freeze the CPU, otherwise, break */
548 if (KdDebuggerNotPresent
) for (;;) KeArchHaltProcessor();
549 DbgBreakPointWithStatus(StatusCode
);
554 KeBugCheckUnicodeToAnsi(IN PUNICODE_STRING Unicode
,
562 /* Set length and normalize it */
563 i
= Unicode
->Length
/ sizeof(WCHAR
);
564 i
= min(i
, Length
- 1);
566 /* Set source and destination, and copy */
567 pw
= Unicode
->Buffer
;
569 while (i
--) *p
++ = (CHAR
)*pw
++;
571 /* Null terminate and return */
578 KiDumpParameterImages(IN PCHAR Message
,
579 IN PULONG_PTR Parameters
,
580 IN ULONG ParameterCount
,
581 IN PKE_BUGCHECK_UNICODE_TO_ANSI ConversionRoutine
)
585 PLDR_DATA_TABLE_ENTRY LdrEntry
;
587 PUNICODE_STRING DriverName
;
589 PIMAGE_NT_HEADERS NtHeader
;
591 BOOLEAN FirstRun
= TRUE
;
593 /* Loop parameters */
594 for (i
= 0; i
< ParameterCount
; i
++)
596 /* Get the base for this parameter */
597 ImageBase
= KiPcToFileHeader((PVOID
)Parameters
[i
],
603 /* Driver wasn't found, check for unloaded driver */
604 DriverName
= NULL
; // FIXME: ROS can't
605 if (!DriverName
) continue;
607 /* Convert the driver name */
608 ImageBase
= (PVOID
)Parameters
[i
];
609 ConversionRoutine(DriverName
, AnsiName
, sizeof(AnsiName
));
613 /* Get the NT Headers and Timestamp */
614 NtHeader
= RtlImageNtHeader(LdrEntry
->DllBase
);
615 TimeStamp
= NtHeader
->FileHeader
.TimeDateStamp
;
617 /* Convert the driver name */
618 DriverName
= &LdrEntry
->BaseDllName
;
619 ConversionRoutine(&LdrEntry
->BaseDllName
,
624 /* Format driver name */
626 "%s** %12s - Address %p base at %p, DateStamp %08lx\n",
627 FirstRun
? "\r\n*":"*",
629 (PVOID
)Parameters
[i
],
633 /* Check if we only had one parameter */
634 if (ParameterCount
<= 1)
636 /* Then just save the name */
637 KiBugCheckDriver
= DriverName
;
641 /* Otherwise, display the message */
642 InbvDisplayString(Message
);
652 KiDisplayBlueScreen(IN ULONG MessageId
,
653 IN BOOLEAN IsHardError
,
654 IN PCHAR HardErrCaption OPTIONAL
,
655 IN PCHAR HardErrMessage OPTIONAL
,
660 /* Check if bootvid is installed */
661 if (InbvIsBootDriverInstalled())
663 /* Acquire ownership and reset the display */
664 InbvAcquireDisplayOwnership();
667 /* Display blue screen */
668 InbvSolidColorFill(0, 0, 639, 479, 4);
669 InbvSetTextColor(15);
670 InbvInstallDisplayStringFilter(NULL
);
671 InbvEnableDisplayString(TRUE
);
672 InbvSetScrollRegion(0, 0, 639, 479);
675 /* Check if this is a hard error */
678 /* Display caption and message */
679 if (HardErrCaption
) InbvDisplayString(HardErrCaption
);
680 if (HardErrMessage
) InbvDisplayString(HardErrMessage
);
683 /* Begin the display */
684 InbvDisplayString("\r\n");
686 /* Print out initial message */
687 KeGetBugMessageText(BUGCHECK_MESSAGE_INTRO
, NULL
);
688 InbvDisplayString("\r\n\r\n");
690 /* Check if we have a driver */
691 if (KiBugCheckDriver
)
693 /* Print out into to driver name */
694 KeGetBugMessageText(BUGCODE_ID_DRIVER
, NULL
);
696 /* Convert and print out driver name */
697 KeBugCheckUnicodeToAnsi(KiBugCheckDriver
, AnsiName
, sizeof(AnsiName
));
698 InbvDisplayString(" ");
699 InbvDisplayString(AnsiName
);
700 InbvDisplayString("\r\n\r\n");
703 /* Check if this is the generic message */
704 if (MessageId
== BUGCODE_PSS_MESSAGE
)
706 /* It is, so get the bug code string as well */
707 KeGetBugMessageText(KiBugCheckData
[0], NULL
);
708 InbvDisplayString("\r\n\r\n");
711 /* Print second introduction message */
712 KeGetBugMessageText(PSS_MESSAGE_INTRO
, NULL
);
713 InbvDisplayString("\r\n\r\n");
715 /* Get the bug code string */
716 KeGetBugMessageText(MessageId
, NULL
);
717 InbvDisplayString("\r\n\r\n");
719 /* Print message for technical information */
720 KeGetBugMessageText(BUGCHECK_TECH_INFO
, NULL
);
722 /* Show the technical Data */
724 "\r\n\r\n*** STOP: 0x%08lX (0x%p,0x%p,0x%p,0x%p)\r\n\r\n",
726 (PVOID
)KiBugCheckData
[1],
727 (PVOID
)KiBugCheckData
[2],
728 (PVOID
)KiBugCheckData
[3],
729 (PVOID
)KiBugCheckData
[4]);
730 InbvDisplayString(AnsiName
);
732 /* Check if we have a driver*/
733 if (KiBugCheckDriver
)
735 /* Display technical driver data */
736 InbvDisplayString(Message
);
740 /* Dump parameter information */
741 KiDumpParameterImages(Message
,
742 (PVOID
)&KiBugCheckData
[1],
744 KeBugCheckUnicodeToAnsi
);
750 KeBugCheckWithTf(IN ULONG BugCheckCode
,
751 IN ULONG_PTR BugCheckParameter1
,
752 IN ULONG_PTR BugCheckParameter2
,
753 IN ULONG_PTR BugCheckParameter3
,
754 IN ULONG_PTR BugCheckParameter4
,
755 IN PKTRAP_FRAME TrapFrame
)
757 PKPRCB Prcb
= KeGetCurrentPrcb();
761 BOOLEAN IsSystem
, IsHardError
= FALSE
, Reboot
= FALSE
;
762 PCHAR HardErrCaption
= NULL
, HardErrMessage
= NULL
;
763 PVOID Eip
= NULL
, Memory
;
765 PLDR_DATA_TABLE_ENTRY LdrEntry
;
766 PULONG_PTR HardErrorParameters
;
771 /* Set active bugcheck */
772 KeBugCheckActive
= TRUE
;
773 KiBugCheckDriver
= NULL
;
775 /* Check if this is power failure simulation */
776 if (BugCheckCode
== POWER_FAILURE_SIMULATE
)
778 /* Call the Callbacks and reboot */;
779 KiDoBugCheckCallbacks();
780 HalReturnToFirmware(HalRebootRoutine
);
783 /* Save the IRQL and set hardware trigger */
784 Prcb
->DebuggerSavedIRQL
= KeGetCurrentIrql();
785 InterlockedIncrement((PLONG
)&KiHardwareTrigger
);
787 /* Capture the CPU Context */
788 RtlCaptureContext(&Prcb
->ProcessorState
.ContextFrame
);
789 KiSaveProcessorControlState(&Prcb
->ProcessorState
);
790 Context
= Prcb
->ProcessorState
.ContextFrame
;
792 /* FIXME: Call the Watchdog if it's registered */
794 /* Check which bugcode this is */
795 switch (BugCheckCode
)
797 /* These bug checks already have detailed messages, keep them */
798 case UNEXPECTED_KERNEL_MODE_TRAP
:
799 case DRIVER_CORRUPTED_EXPOOL
:
800 case ACPI_BIOS_ERROR
:
801 case ACPI_BIOS_FATAL_ERROR
:
802 case THREAD_STUCK_IN_DEVICE_DRIVER
:
804 case FAT_FILE_SYSTEM
:
805 case NO_MORE_SYSTEM_PTES
:
806 case INACCESSIBLE_BOOT_DEVICE
:
808 /* Keep the same code */
809 MessageId
= BugCheckCode
;
812 /* Check if this is a kernel-mode exception */
813 case KERNEL_MODE_EXCEPTION_NOT_HANDLED
:
814 //case SYSTEM_THREAD_EXCEPTION_NOT_HANDLED:
815 case KMODE_EXCEPTION_NOT_HANDLED
:
817 /* Use the generic text message */
818 MessageId
= KMODE_EXCEPTION_NOT_HANDLED
;
821 /* File-system errors */
822 case NTFS_FILE_SYSTEM
:
824 /* Use the generic message for FAT */
825 MessageId
= FAT_FILE_SYSTEM
;
828 /* Check if this is a coruption of the Mm's Pool */
829 case DRIVER_CORRUPTED_MMPOOL
:
831 /* Use generic corruption message */
832 MessageId
= DRIVER_CORRUPTED_EXPOOL
;
835 /* Check if this is a signature check failure */
836 case STATUS_SYSTEM_IMAGE_BAD_SIGNATURE
:
838 /* Use the generic corruption message */
839 MessageId
= BUGCODE_PSS_MESSAGE_SIGNATURE
;
842 /* All other codes */
845 /* Use the default bugcheck message */
846 MessageId
= BUGCODE_PSS_MESSAGE
;
850 /* Save bugcheck data */
851 KiBugCheckData
[0] = BugCheckCode
;
852 KiBugCheckData
[1] = BugCheckParameter1
;
853 KiBugCheckData
[2] = BugCheckParameter2
;
854 KiBugCheckData
[3] = BugCheckParameter3
;
855 KiBugCheckData
[4] = BugCheckParameter4
;
857 /* Now check what bugcheck this is */
858 switch (BugCheckCode
)
860 /* Invalid access to R/O memory or Unhandled KM Exception */
861 case KERNEL_MODE_EXCEPTION_NOT_HANDLED
:
862 case ATTEMPTED_WRITE_TO_READONLY_MEMORY
:
863 case ATTEMPTED_EXECUTE_OF_NOEXECUTE_MEMORY
:
865 /* Check if we have a trap frame */
868 /* Use parameter 3 as a trap frame, if it exists */
869 if (BugCheckParameter3
) TrapFrame
= (PVOID
)BugCheckParameter3
;
872 /* Check if we got one now and if we need to get EIP */
874 (BugCheckCode
!= KERNEL_MODE_EXCEPTION_NOT_HANDLED
))
878 Eip
= (PVOID
)TrapFrame
->Eip
;
879 #elif defined(_M_PPC)
880 Eip
= (PVOID
)TrapFrame
->Dr0
; /* srr0 */
886 case IRQL_NOT_LESS_OR_EQUAL
:
889 * The NT kernel has 3 special sections:
890 * MISYSPTE, POOLMI and POOLCODE. The bug check code can
891 * determine in which of these sections this bugcode happened
892 * and provide a more detailed analysis. For now, we don't.
895 /* Eip is in parameter 4 */
896 Eip
= (PVOID
)BugCheckParameter4
;
898 /* Get the driver base */
899 DriverBase
= KiPcToFileHeader(Eip
, &LdrEntry
, FALSE
, &IsSystem
);
903 * The error happened inside the kernel or HAL.
904 * Get the memory address that was being referenced.
906 Memory
= (PVOID
)BugCheckParameter1
;
908 /* Find to which driver it belongs */
909 DriverBase
= KiPcToFileHeader(Memory
,
915 /* Get the driver name and update the bug code */
916 KiBugCheckDriver
= &LdrEntry
->BaseDllName
;
917 KiBugCheckData
[0] = DRIVER_PORTION_MUST_BE_NONPAGED
;
921 /* Find the driver that unloaded at this address */
922 KiBugCheckDriver
= NULL
; // FIXME: ROS can't locate
924 /* Check if the cause was an unloaded driver */
925 if (KiBugCheckDriver
)
927 /* Update bug check code */
929 SYSTEM_SCAN_AT_RAISED_IRQL_CAUGHT_IMPROPER_DRIVER_UNLOAD
;
935 /* Update the bug check code */
936 KiBugCheckData
[0] = DRIVER_IRQL_NOT_LESS_OR_EQUAL
;
939 /* Clear EIP so we don't look it up later */
944 case FATAL_UNHANDLED_HARD_ERROR
:
946 /* Copy bug check data from hard error */
947 HardErrorParameters
= (PULONG_PTR
)BugCheckParameter2
;
948 KiBugCheckData
[0] = BugCheckParameter1
;
949 KiBugCheckData
[1] = HardErrorParameters
[0];
950 KiBugCheckData
[2] = HardErrorParameters
[1];
951 KiBugCheckData
[3] = HardErrorParameters
[2];
952 KiBugCheckData
[4] = HardErrorParameters
[3];
954 /* Remember that this is hard error and set the caption/message */
956 HardErrCaption
= (PCHAR
)BugCheckParameter3
;
957 HardErrMessage
= (PCHAR
)BugCheckParameter4
;
961 case PAGE_FAULT_IN_NONPAGED_AREA
:
963 /* Assume no driver */
966 /* Check if we have a trap frame */
969 /* We don't, use parameter 3 if possible */
970 if (BugCheckParameter3
) TrapFrame
= (PVOID
)BugCheckParameter3
;
973 /* Check if we have a frame now */
978 Eip
= (PVOID
)TrapFrame
->Eip
;
979 KiBugCheckData
[3] = (ULONG
)Eip
;
980 #elif defined(_M_PPC)
981 Eip
= (PVOID
)TrapFrame
->Dr0
; /* srr0 */
982 KiBugCheckData
[3] = (ULONG
)Eip
;
985 /* Find out if was in the kernel or drivers */
986 DriverBase
= KiPcToFileHeader(Eip
,
993 * Now we should check if this happened in:
994 * 1) Special Pool 2) Free Special Pool 3) Session Pool
995 * and update the bugcheck code appropriately.
998 /* Check if we didn't have a driver base */
1001 /* Find the driver that unloaded at this address */
1002 KiBugCheckDriver
= NULL
; // FIXME: ROS can't locate
1004 /* Check if the cause was an unloaded driver */
1005 if (KiBugCheckDriver
)
1008 DRIVER_UNLOADED_WITHOUT_CANCELLING_PENDING_OPERATIONS
;
1013 /* Check if the driver forgot to unlock pages */
1014 case DRIVER_LEFT_LOCKED_PAGES_IN_PROCESS
:
1016 /* EIP is in parameter 1 */
1017 Eip
= (PVOID
)BugCheckParameter1
;
1020 /* Check if the driver consumed too many PTEs */
1021 case DRIVER_USED_EXCESSIVE_PTES
:
1023 /* Loader entry is in parameter 1 */
1024 LdrEntry
= (PVOID
)BugCheckParameter1
;
1025 KiBugCheckDriver
= &LdrEntry
->BaseDllName
;
1028 /* Check if the driver has a stuck thread */
1029 case THREAD_STUCK_IN_DEVICE_DRIVER
:
1031 /* The name is in Parameter 3 */
1032 KiBugCheckDriver
= (PVOID
)BugCheckParameter3
;
1040 /* Do we have a driver name? */
1041 if (KiBugCheckDriver
)
1043 /* Convert it to ANSI */
1044 KeBugCheckUnicodeToAnsi(KiBugCheckDriver
, AnsiName
, sizeof(AnsiName
));
1048 /* Do we have an EIP? */
1051 /* Dump image name */
1052 KiDumpParameterImages(AnsiName
,
1055 KeBugCheckUnicodeToAnsi
);
1059 /* Check if we need to save the context for KD */
1061 if (!KdPitchDebugger
) KdDebuggerDataBlock
.SavedContext
= (ULONG
)&Context
;
1064 /* Check if a debugger is connected */
1065 if ((BugCheckCode
!= MANUALLY_INITIATED_CRASH
) && (KdDebuggerEnabled
))
1067 /* Crash on the debugger console */
1068 DbgPrint("\n*** Fatal System Error: 0x%08lx\n"
1069 " (0x%p,0x%p,0x%p,0x%p)\n\n",
1076 /* Check if the debugger isn't currently connected */
1077 if (!KdDebuggerNotPresent
)
1079 /* Check if we have a driver to blame */
1080 if (KiBugCheckDriver
)
1083 DbgPrint("Driver at fault: %s.\n", AnsiName
);
1086 /* Check if this was a hard error */
1089 /* Print caption and message */
1090 if (HardErrCaption
) DbgPrint(HardErrCaption
);
1091 if (HardErrMessage
) DbgPrint(HardErrMessage
);
1094 /* Break in the debugger */
1095 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_FIRST
);
1101 * Ok, so debugging is enabled, but KDBG isn't there.
1102 * We'll manually dump the stack for the user.
1104 KeRosDumpStackFrames(NULL
, 0);
1106 /* ROS HACK 2: Generate something useful for Bugzilla */
1107 KeRosDumpTriageForBugZillaReport();
1111 /* Raise IRQL to HIGH_LEVEL */
1113 KfRaiseIrql(HIGH_LEVEL
);
1115 /* Avoid recursion */
1116 if (!InterlockedDecrement((PLONG
)&KeBugCheckCount
))
1119 /* Set CPU that is bug checking now */
1120 KeBugCheckOwner
= Prcb
->Number
;
1122 /* Freeze the other CPUs */
1123 for (i
= 0; i
< KeNumberProcessors
; i
++)
1125 if (i
!= (LONG
)KeGetCurrentProcessorNumber())
1127 /* Send the IPI and give them one second to catch up */
1128 KiIpiSendRequest(1 << i
, IPI_FREEZE
);
1129 KeStallExecutionProcessor(1000000);
1134 /* Display the BSOD */
1135 KfLowerIrql(APC_LEVEL
); // This is a nastier hack than any ever before
1136 KiDisplayBlueScreen(MessageId
,
1141 KfRaiseIrql(HIGH_LEVEL
);
1143 /* Check if the debugger is disabled but we can enable it */
1144 if (!(KdDebuggerEnabled
) && !(KdPitchDebugger
))
1148 KdEnableDebuggerWithLock(FALSE
);
1153 /* Otherwise, print the last line */
1154 InbvDisplayString("\r\n");
1157 /* Save the context */
1158 Prcb
->ProcessorState
.ContextFrame
= Context
;
1160 /* FIXME: Support Triage Dump */
1162 /* Write the crash dump */
1163 MmDumpToPagingFile(KiBugCheckData
[4],
1172 /* Increase recursion count */
1173 KeBugCheckOwnerRecursionCount
++;
1174 if (KeBugCheckOwnerRecursionCount
== 2)
1176 /* Break in the debugger */
1177 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_SECOND
);
1179 else if (KeBugCheckOwnerRecursionCount
> 2)
1182 for (;;) KeArchHaltProcessor();
1186 /* Call the Callbacks */
1187 KiDoBugCheckCallbacks();
1189 /* FIXME: Call Watchdog if enabled */
1191 /* Check if we have to reboot */
1194 /* Unload symbols */
1195 DbgUnLoadImageSymbols(NULL
, NtCurrentProcess(), 0);
1196 HalReturnToFirmware(HalRebootRoutine
);
1199 /* Attempt to break in the debugger (otherwise halt CPU) */
1200 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_SECOND
);
1203 /* PUBLIC FUNCTIONS **********************************************************/
1210 KeDeregisterBugCheckCallback(IN PKBUGCHECK_CALLBACK_RECORD CallbackRecord
)
1213 BOOLEAN Status
= FALSE
;
1215 /* Raise IRQL to High */
1216 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1218 /* Check the Current State */
1219 if (CallbackRecord
->State
== BufferInserted
)
1221 /* Reset state and remove from list */
1222 CallbackRecord
->State
= BufferEmpty
;
1223 RemoveEntryList(&CallbackRecord
->Entry
);
1227 /* Lower IRQL and return */
1228 KeLowerIrql(OldIrql
);
1237 KeDeregisterBugCheckReasonCallback(
1238 IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord
)
1241 BOOLEAN Status
= FALSE
;
1243 /* Raise IRQL to High */
1244 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1246 /* Check the Current State */
1247 if (CallbackRecord
->State
== BufferInserted
)
1249 /* Reset state and remove from list */
1250 CallbackRecord
->State
= BufferEmpty
;
1251 RemoveEntryList(&CallbackRecord
->Entry
);
1255 /* Lower IRQL and return */
1256 KeLowerIrql(OldIrql
);
1265 KeRegisterBugCheckCallback(IN PKBUGCHECK_CALLBACK_RECORD CallbackRecord
,
1266 IN PKBUGCHECK_CALLBACK_ROUTINE CallbackRoutine
,
1269 IN PUCHAR Component
)
1272 BOOLEAN Status
= FALSE
;
1274 /* Raise IRQL to High */
1275 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1277 /* Check the Current State first so we don't double-register */
1278 if (CallbackRecord
->State
== BufferEmpty
)
1280 /* Set the Callback Settings and insert into the list */
1281 CallbackRecord
->Length
= Length
;
1282 CallbackRecord
->Buffer
= Buffer
;
1283 CallbackRecord
->Component
= Component
;
1284 CallbackRecord
->CallbackRoutine
= CallbackRoutine
;
1285 CallbackRecord
->State
= BufferInserted
;
1286 InsertTailList(&KeBugcheckCallbackListHead
, &CallbackRecord
->Entry
);
1290 /* Lower IRQL and return */
1291 KeLowerIrql(OldIrql
);
1300 KeRegisterBugCheckReasonCallback(
1301 IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord
,
1302 IN PKBUGCHECK_REASON_CALLBACK_ROUTINE CallbackRoutine
,
1303 IN KBUGCHECK_CALLBACK_REASON Reason
,
1304 IN PUCHAR Component
)
1307 BOOLEAN Status
= FALSE
;
1309 /* Raise IRQL to High */
1310 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1312 /* Check the Current State first so we don't double-register */
1313 if (CallbackRecord
->State
== BufferEmpty
)
1315 /* Set the Callback Settings and insert into the list */
1316 CallbackRecord
->Component
= Component
;
1317 CallbackRecord
->CallbackRoutine
= CallbackRoutine
;
1318 CallbackRecord
->State
= BufferInserted
;
1319 CallbackRecord
->Reason
= Reason
;
1320 InsertTailList(&KeBugcheckReasonCallbackListHead
,
1321 &CallbackRecord
->Entry
);
1325 /* Lower IRQL and return */
1326 KeLowerIrql(OldIrql
);
1335 KeBugCheckEx(IN ULONG BugCheckCode
,
1336 IN ULONG_PTR BugCheckParameter1
,
1337 IN ULONG_PTR BugCheckParameter2
,
1338 IN ULONG_PTR BugCheckParameter3
,
1339 IN ULONG_PTR BugCheckParameter4
)
1341 /* Call the internal API */
1342 KeBugCheckWithTf(BugCheckCode
,
1355 KeBugCheck(ULONG BugCheckCode
)
1357 /* Call the internal API */
1358 KeBugCheckWithTf(BugCheckCode
, 0, 0, 0, 0, NULL
);
1366 KeEnterKernelDebugger(VOID
)
1368 /* Disable interrupts */
1369 KiHardwareTrigger
= 1;
1372 /* Check the bugcheck count */
1373 if (!InterlockedDecrement((PLONG
)&KeBugCheckCount
))
1375 /* There was only one, is the debugger disabled? */
1376 if (!(KdDebuggerEnabled
) && !(KdPitchDebugger
))
1378 /* Enable the debugger */
1379 KdInitSystem(0, NULL
);
1384 KiBugCheckDebugBreak(DBG_STATUS_FATAL
);