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 ******************************************************************/
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);
233 KeRosDumpStackFrameArray(IN PULONG Frames
,
239 PLDR_DATA_TABLE_ENTRY LdrEntry
;
242 for (i
= 0; i
< FrameCount
; i
++)
251 /* Get the base for this file */
252 if (Addr
> (ULONG_PTR
)MmHighestUserAddress
)
254 /* We are in kernel */
255 p
= KiPcToFileHeader((PVOID
)Addr
, &LdrEntry
, FALSE
, &InSystem
);
259 /* We are in user land */
260 p
= KiRosPcToUserFileHeader((PVOID
)Addr
, &LdrEntry
);
265 if (!KdbSymPrintAddress((PVOID
)Addr
))
268 /* Print out the module name */
269 Addr
-= (ULONG_PTR
)LdrEntry
->DllBase
;
270 DbgPrint("<%wZ: %x>\n", &LdrEntry
->FullDllName
, Addr
);
275 /* Print only the address */
276 DbgPrint("<%x>\n", Addr
);
279 /* Go to the next frame */
286 KeRosDumpStackFrames(IN PULONG Frame OPTIONAL
,
287 IN ULONG FrameCount OPTIONAL
)
290 ULONG RealFrameCount
;
292 /* If the caller didn't ask, assume 32 frames */
293 if (!FrameCount
|| FrameCount
> 32) FrameCount
= 32;
298 KeRosDumpStackFrameArray(Frame
, FrameCount
);
302 /* Get the current frames (skip the two. One for the dumper, one for the caller) */
303 RealFrameCount
= RtlCaptureStackBackTrace(2, FrameCount
, (PVOID
*)Frames
, NULL
);
306 KeRosDumpStackFrameArray(Frames
, RealFrameCount
);
308 /* Count left for user mode? */
309 if (FrameCount
- RealFrameCount
> 0)
311 /* Get the current frames */
312 RealFrameCount
= KeRosCaptureUserStackBackTrace(-1, FrameCount
- RealFrameCount
, (PVOID
*)Frames
, NULL
);
315 KeRosDumpStackFrameArray(Frames
, RealFrameCount
);
322 KeRosDumpTriageForBugZillaReport(VOID
)
325 extern BOOLEAN KiFastSystemCallDisable
, KiSMTProcessorsPresent
;
326 extern ULONG KeI386MachineType
, MxcsrFeatureMask
;
327 extern BOOLEAN Ke386Pae
, Ke386NoExecute
;
329 DbgPrint("ReactOS has crashed! Please go to http://www.reactos.org/bugzilla/enter_bug.cgi to file a bug!\n");
330 DbgPrint("\nHardware Information\n");
331 DbgPrint("Processor Architecture: %d\n"
333 "System Call Disabled: %d\n"
336 "MXCsr Feature Mask: %d\n"
343 "Active Processors: %d\n"
344 "Pentium LOCK Bug: %d\n"
345 "Hyperthreading: %d\n"
346 "CPU Manufacturer: %s\n"
354 "BIOS Version: %wZ\n"
355 "Video BIOS Date: %wZ\n"
356 "Video BIOS Version: %wZ\n"
358 KeProcessorArchitecture
,
360 KiFastSystemCallDisable
,
371 KiI386PentiumLockErrataPresent
,
372 KiSMTProcessorsPresent
,
373 KeGetCurrentPrcb()->VendorString
,
375 KeGetCurrentPrcb()->CpuID
,
376 KeGetCurrentPrcb()->CpuType
,
377 KeGetCurrentPrcb()->CpuStep
,
378 KeGetCurrentPrcb()->MHz
,
379 ((PKIPCR
)KeGetPcr())->SecondLevelCacheSize
,
383 &KeRosVideoBiosVersion
,
384 MmNumberOfPhysicalPages
* PAGE_SIZE
);
391 KiInitializeBugCheck(VOID
)
393 PRTL_MESSAGE_RESOURCE_DATA BugCheckData
;
394 LDR_RESOURCE_INFO ResourceInfo
;
395 PIMAGE_RESOURCE_DATA_ENTRY ResourceDataEntry
;
397 PLDR_DATA_TABLE_ENTRY LdrEntry
;
399 /* Get the kernel entry */
400 LdrEntry
= CONTAINING_RECORD(KeLoaderBlock
->LoadOrderListHead
.Flink
,
401 LDR_DATA_TABLE_ENTRY
,
404 /* Cache the Bugcheck Message Strings. Prepare the Lookup Data */
405 ResourceInfo
.Type
= 11;
406 ResourceInfo
.Name
= 1;
407 ResourceInfo
.Language
= 9;
410 Status
= LdrFindResource_U(LdrEntry
->DllBase
,
415 /* Make sure it worked */
416 if (NT_SUCCESS(Status
))
418 /* Now actually get a pointer to it */
419 Status
= LdrAccessResource(LdrEntry
->DllBase
,
421 (PVOID
*)&BugCheckData
,
423 if (NT_SUCCESS(Status
)) KiBugCodeMessages
= BugCheckData
;
429 KeGetBugMessageText(IN ULONG BugCheckCode
,
430 OUT PANSI_STRING OutputString OPTIONAL
)
434 ULONG_PTR MessageEntry
;
436 BOOLEAN Result
= FALSE
;
438 /* Make sure we're not bugchecking too early */
439 if (!KiBugCodeMessages
) return Result
;
441 /* Find the message. This code is based on RtlFindMesssage */
442 for (i
= 0; i
< KiBugCodeMessages
->NumberOfBlocks
; i
++)
444 /* Check if the ID Matches */
445 if ((BugCheckCode
>= KiBugCodeMessages
->Blocks
[i
].LowId
) &&
446 (BugCheckCode
<= KiBugCodeMessages
->Blocks
[i
].HighId
))
448 /* Get Offset to Entry */
449 MessageEntry
= KiBugCodeMessages
->Blocks
[i
].OffsetToEntries
+
450 (ULONG_PTR
)KiBugCodeMessages
;
451 IdOffset
= BugCheckCode
- KiBugCodeMessages
->Blocks
[i
].LowId
;
453 /* Get offset to ID */
454 for (i
= 0; i
< IdOffset
; i
++)
456 /* Advance in the Entries */
457 MessageEntry
+= ((PRTL_MESSAGE_RESOURCE_ENTRY
)MessageEntry
)->
461 /* Get the final Code */
462 BugCode
= ((PRTL_MESSAGE_RESOURCE_ENTRY
)MessageEntry
)->Text
;
465 /* Handle newlines */
466 while ((i
> 0) && ((BugCode
[i
] == '\n') ||
467 (BugCode
[i
] == '\r') ||
468 (BugCode
[i
] == ANSI_NULL
)))
470 /* Check if we have a string to return */
471 if (!OutputString
) BugCode
[i
] = ANSI_NULL
;
475 /* Check if caller wants an output string */
478 /* Return it in the OutputString */
479 OutputString
->Buffer
= BugCode
;
480 OutputString
->Length
= (USHORT
)i
+ 1;
481 OutputString
->MaximumLength
= (USHORT
)i
+ 1;
485 /* Direct Output to Screen */
486 InbvDisplayString(BugCode
);
487 InbvDisplayString("\r");
496 /* Return the result */
502 KiDoBugCheckCallbacks(VOID
)
504 PKBUGCHECK_CALLBACK_RECORD CurrentRecord
;
505 PLIST_ENTRY ListHead
, NextEntry
, LastEntry
;
508 /* First make sure that the list is Initialized... it might not be */
509 ListHead
= &KeBugcheckCallbackListHead
;
510 if ((ListHead
->Flink
) && (ListHead
->Blink
))
513 LastEntry
= ListHead
;
514 NextEntry
= ListHead
->Flink
;
515 while (NextEntry
!= ListHead
)
518 CurrentRecord
= CONTAINING_RECORD(NextEntry
,
519 KBUGCHECK_CALLBACK_RECORD
,
523 if (CurrentRecord
->Entry
.Blink
!= LastEntry
) return;
524 Checksum
= (ULONG_PTR
)CurrentRecord
->CallbackRoutine
;
525 Checksum
+= (ULONG_PTR
)CurrentRecord
->Buffer
;
526 Checksum
+= (ULONG_PTR
)CurrentRecord
->Length
;
527 Checksum
+= (ULONG_PTR
)CurrentRecord
->Component
;
529 /* Make sure it's inserted and valitdated */
530 if ((CurrentRecord
->State
== BufferInserted
) &&
531 (CurrentRecord
->Checksum
== Checksum
))
533 /* Call the routine */
534 CurrentRecord
->State
= BufferStarted
;
535 (CurrentRecord
->CallbackRoutine
)(CurrentRecord
->Buffer
,
536 CurrentRecord
->Length
);
537 CurrentRecord
->State
= BufferFinished
;
540 /* Go to the next entry */
541 LastEntry
= NextEntry
;
542 NextEntry
= NextEntry
->Flink
;
549 KiBugCheckDebugBreak(IN ULONG StatusCode
)
551 /* If KDBG isn't connected, freeze the CPU, otherwise, break */
552 if (KdDebuggerNotPresent
) for (;;) KeArchHaltProcessor();
553 DbgBreakPointWithStatus(StatusCode
);
558 KeBugCheckUnicodeToAnsi(IN PUNICODE_STRING Unicode
,
566 /* Set length and normalize it */
567 i
= Unicode
->Length
/ sizeof(WCHAR
);
568 i
= min(i
, Length
- 1);
570 /* Set source and destination, and copy */
571 pw
= Unicode
->Buffer
;
573 while (i
--) *p
++ = (CHAR
)*pw
++;
575 /* Null terminate and return */
582 KiDumpParameterImages(IN PCHAR Message
,
583 IN PULONG_PTR Parameters
,
584 IN ULONG ParameterCount
,
585 IN PKE_BUGCHECK_UNICODE_TO_ANSI ConversionRoutine
)
589 PLDR_DATA_TABLE_ENTRY LdrEntry
;
591 PUNICODE_STRING DriverName
;
593 PIMAGE_NT_HEADERS NtHeader
;
595 BOOLEAN FirstRun
= TRUE
;
597 /* Loop parameters */
598 for (i
= 0; i
< ParameterCount
; i
++)
600 /* Get the base for this parameter */
601 ImageBase
= KiPcToFileHeader((PVOID
)Parameters
[i
],
607 /* Driver wasn't found, check for unloaded driver */
608 DriverName
= NULL
; // FIXME: ROS can't
609 if (!DriverName
) continue;
611 /* Convert the driver name */
612 ImageBase
= (PVOID
)Parameters
[i
];
613 ConversionRoutine(DriverName
, AnsiName
, sizeof(AnsiName
));
617 /* Get the NT Headers and Timestamp */
618 NtHeader
= RtlImageNtHeader(LdrEntry
->DllBase
);
619 TimeStamp
= NtHeader
->FileHeader
.TimeDateStamp
;
621 /* Convert the driver name */
622 DriverName
= &LdrEntry
->BaseDllName
;
623 ConversionRoutine(&LdrEntry
->BaseDllName
,
628 /* Format driver name */
630 "%s** %12s - Address %p base at %p, DateStamp %08lx\n",
631 FirstRun
? "\r\n*":"*",
633 (PVOID
)Parameters
[i
],
637 /* Check if we only had one parameter */
638 if (ParameterCount
<= 1)
640 /* Then just save the name */
641 KiBugCheckDriver
= DriverName
;
645 /* Otherwise, display the message */
646 InbvDisplayString(Message
);
656 KiDisplayBlueScreen(IN ULONG MessageId
,
657 IN BOOLEAN IsHardError
,
658 IN PCHAR HardErrCaption OPTIONAL
,
659 IN PCHAR HardErrMessage OPTIONAL
,
664 /* Check if bootvid is installed */
665 if (InbvIsBootDriverInstalled())
667 /* Acquire ownership and reset the display */
668 InbvAcquireDisplayOwnership();
671 /* Display blue screen */
672 InbvSolidColorFill(0, 0, 639, 479, 4);
673 InbvSetTextColor(15);
674 InbvInstallDisplayStringFilter(NULL
);
675 InbvEnableDisplayString(TRUE
);
676 InbvSetScrollRegion(0, 0, 639, 479);
679 /* Check if this is a hard error */
682 /* Display caption and message */
683 if (HardErrCaption
) InbvDisplayString(HardErrCaption
);
684 if (HardErrMessage
) InbvDisplayString(HardErrMessage
);
687 /* Begin the display */
688 InbvDisplayString("\r\n");
690 /* Print out initial message */
691 KeGetBugMessageText(BUGCHECK_MESSAGE_INTRO
, NULL
);
692 InbvDisplayString("\r\n\r\n");
694 /* Check if we have a driver */
695 if (KiBugCheckDriver
)
697 /* Print out into to driver name */
698 KeGetBugMessageText(BUGCODE_ID_DRIVER
, NULL
);
700 /* Convert and print out driver name */
701 KeBugCheckUnicodeToAnsi(KiBugCheckDriver
, AnsiName
, sizeof(AnsiName
));
702 InbvDisplayString(" ");
703 InbvDisplayString(AnsiName
);
704 InbvDisplayString("\r\n\r\n");
707 /* Check if this is the generic message */
708 if (MessageId
== BUGCODE_PSS_MESSAGE
)
710 /* It is, so get the bug code string as well */
711 KeGetBugMessageText(KiBugCheckData
[0], NULL
);
712 InbvDisplayString("\r\n\r\n");
715 /* Print second introduction message */
716 KeGetBugMessageText(PSS_MESSAGE_INTRO
, NULL
);
717 InbvDisplayString("\r\n\r\n");
719 /* Get the bug code string */
720 KeGetBugMessageText(MessageId
, NULL
);
721 InbvDisplayString("\r\n\r\n");
723 /* Print message for technical information */
724 KeGetBugMessageText(BUGCHECK_TECH_INFO
, NULL
);
726 /* Show the technical Data */
728 "\r\n\r\n*** STOP: 0x%08lX (0x%p,0x%p,0x%p,0x%p)\r\n\r\n",
730 (PVOID
)KiBugCheckData
[1],
731 (PVOID
)KiBugCheckData
[2],
732 (PVOID
)KiBugCheckData
[3],
733 (PVOID
)KiBugCheckData
[4]);
734 InbvDisplayString(AnsiName
);
736 /* Check if we have a driver*/
737 if (KiBugCheckDriver
)
739 /* Display technical driver data */
740 InbvDisplayString(Message
);
744 /* Dump parameter information */
745 KiDumpParameterImages(Message
,
746 (PVOID
)&KiBugCheckData
[1],
748 KeBugCheckUnicodeToAnsi
);
754 KeBugCheckWithTf(IN ULONG BugCheckCode
,
755 IN ULONG_PTR BugCheckParameter1
,
756 IN ULONG_PTR BugCheckParameter2
,
757 IN ULONG_PTR BugCheckParameter3
,
758 IN ULONG_PTR BugCheckParameter4
,
759 IN PKTRAP_FRAME TrapFrame
)
761 PKPRCB Prcb
= KeGetCurrentPrcb();
765 BOOLEAN IsSystem
, IsHardError
= FALSE
, Reboot
= FALSE
;
766 PCHAR HardErrCaption
= NULL
, HardErrMessage
= NULL
;
767 PVOID Eip
= NULL
, Memory
;
769 PLDR_DATA_TABLE_ENTRY LdrEntry
;
770 PULONG_PTR HardErrorParameters
;
776 /* Set active bugcheck */
777 KeBugCheckActive
= TRUE
;
778 KiBugCheckDriver
= NULL
;
780 /* Check if this is power failure simulation */
781 if (BugCheckCode
== POWER_FAILURE_SIMULATE
)
783 /* Call the Callbacks and reboot */;
784 KiDoBugCheckCallbacks();
785 HalReturnToFirmware(HalRebootRoutine
);
788 /* Save the IRQL and set hardware trigger */
789 Prcb
->DebuggerSavedIRQL
= KeGetCurrentIrql();
790 InterlockedIncrement((PLONG
)&KiHardwareTrigger
);
792 /* Capture the CPU Context */
793 RtlCaptureContext(&Prcb
->ProcessorState
.ContextFrame
);
794 KiSaveProcessorControlState(&Prcb
->ProcessorState
);
795 Context
= Prcb
->ProcessorState
.ContextFrame
;
797 /* FIXME: Call the Watchdog if it's registered */
799 /* Check which bugcode this is */
800 switch (BugCheckCode
)
802 /* These bug checks already have detailed messages, keep them */
803 case UNEXPECTED_KERNEL_MODE_TRAP
:
804 case DRIVER_CORRUPTED_EXPOOL
:
805 case ACPI_BIOS_ERROR
:
806 case ACPI_BIOS_FATAL_ERROR
:
807 case THREAD_STUCK_IN_DEVICE_DRIVER
:
809 case FAT_FILE_SYSTEM
:
810 case NO_MORE_SYSTEM_PTES
:
811 case INACCESSIBLE_BOOT_DEVICE
:
813 /* Keep the same code */
814 MessageId
= BugCheckCode
;
817 /* Check if this is a kernel-mode exception */
818 case KERNEL_MODE_EXCEPTION_NOT_HANDLED
:
819 //case SYSTEM_THREAD_EXCEPTION_NOT_HANDLED:
820 case KMODE_EXCEPTION_NOT_HANDLED
:
822 /* Use the generic text message */
823 MessageId
= KMODE_EXCEPTION_NOT_HANDLED
;
826 /* File-system errors */
827 case NTFS_FILE_SYSTEM
:
829 /* Use the generic message for FAT */
830 MessageId
= FAT_FILE_SYSTEM
;
833 /* Check if this is a coruption of the Mm's Pool */
834 case DRIVER_CORRUPTED_MMPOOL
:
836 /* Use generic corruption message */
837 MessageId
= DRIVER_CORRUPTED_EXPOOL
;
840 /* Check if this is a signature check failure */
841 case STATUS_SYSTEM_IMAGE_BAD_SIGNATURE
:
843 /* Use the generic corruption message */
844 MessageId
= BUGCODE_PSS_MESSAGE_SIGNATURE
;
847 /* All other codes */
850 /* Use the default bugcheck message */
851 MessageId
= BUGCODE_PSS_MESSAGE
;
855 /* Save bugcheck data */
856 KiBugCheckData
[0] = BugCheckCode
;
857 KiBugCheckData
[1] = BugCheckParameter1
;
858 KiBugCheckData
[2] = BugCheckParameter2
;
859 KiBugCheckData
[3] = BugCheckParameter3
;
860 KiBugCheckData
[4] = BugCheckParameter4
;
862 /* Now check what bugcheck this is */
863 switch (BugCheckCode
)
865 /* Invalid access to R/O memory or Unhandled KM Exception */
866 case KERNEL_MODE_EXCEPTION_NOT_HANDLED
:
867 case ATTEMPTED_WRITE_TO_READONLY_MEMORY
:
868 case ATTEMPTED_EXECUTE_OF_NOEXECUTE_MEMORY
:
870 /* Check if we have a trap frame */
873 /* Use parameter 3 as a trap frame, if it exists */
874 if (BugCheckParameter3
) TrapFrame
= (PVOID
)BugCheckParameter3
;
877 /* Check if we got one now and if we need to get EIP */
879 (BugCheckCode
!= KERNEL_MODE_EXCEPTION_NOT_HANDLED
))
883 Eip
= (PVOID
)TrapFrame
->Eip
;
884 #elif defined(_M_PPC)
885 Eip
= (PVOID
)TrapFrame
->Dr0
; /* srr0 */
891 case IRQL_NOT_LESS_OR_EQUAL
:
894 * The NT kernel has 3 special sections:
895 * MISYSPTE, POOLMI and POOLCODE. The bug check code can
896 * determine in which of these sections this bugcode happened
897 * and provide a more detailed analysis. For now, we don't.
900 /* Eip is in parameter 4 */
901 Eip
= (PVOID
)BugCheckParameter4
;
903 /* Get the driver base */
904 DriverBase
= KiPcToFileHeader(Eip
, &LdrEntry
, FALSE
, &IsSystem
);
908 * The error happened inside the kernel or HAL.
909 * Get the memory address that was being referenced.
911 Memory
= (PVOID
)BugCheckParameter1
;
913 /* Find to which driver it belongs */
914 DriverBase
= KiPcToFileHeader(Memory
,
920 /* Get the driver name and update the bug code */
921 KiBugCheckDriver
= &LdrEntry
->BaseDllName
;
922 KiBugCheckData
[0] = DRIVER_PORTION_MUST_BE_NONPAGED
;
926 /* Find the driver that unloaded at this address */
927 KiBugCheckDriver
= NULL
; // FIXME: ROS can't locate
929 /* Check if the cause was an unloaded driver */
930 if (KiBugCheckDriver
)
932 /* Update bug check code */
934 SYSTEM_SCAN_AT_RAISED_IRQL_CAUGHT_IMPROPER_DRIVER_UNLOAD
;
940 /* Update the bug check code */
941 KiBugCheckData
[0] = DRIVER_IRQL_NOT_LESS_OR_EQUAL
;
944 /* Clear EIP so we don't look it up later */
949 case FATAL_UNHANDLED_HARD_ERROR
:
951 /* Copy bug check data from hard error */
952 HardErrorParameters
= (PULONG_PTR
)BugCheckParameter2
;
953 KiBugCheckData
[0] = BugCheckParameter1
;
954 KiBugCheckData
[1] = HardErrorParameters
[0];
955 KiBugCheckData
[2] = HardErrorParameters
[1];
956 KiBugCheckData
[3] = HardErrorParameters
[2];
957 KiBugCheckData
[4] = HardErrorParameters
[3];
959 /* Remember that this is hard error and set the caption/message */
961 HardErrCaption
= (PCHAR
)BugCheckParameter3
;
962 HardErrMessage
= (PCHAR
)BugCheckParameter4
;
966 case PAGE_FAULT_IN_NONPAGED_AREA
:
968 /* Assume no driver */
971 /* Check if we have a trap frame */
974 /* We don't, use parameter 3 if possible */
975 if (BugCheckParameter3
) TrapFrame
= (PVOID
)BugCheckParameter3
;
978 /* Check if we have a frame now */
983 Eip
= (PVOID
)TrapFrame
->Eip
;
984 KiBugCheckData
[3] = (ULONG
)Eip
;
985 #elif defined(_M_PPC)
986 Eip
= (PVOID
)TrapFrame
->Dr0
; /* srr0 */
987 KiBugCheckData
[3] = (ULONG
)Eip
;
990 /* Find out if was in the kernel or drivers */
991 DriverBase
= KiPcToFileHeader(Eip
,
998 * Now we should check if this happened in:
999 * 1) Special Pool 2) Free Special Pool 3) Session Pool
1000 * and update the bugcheck code appropriately.
1003 /* Check if we didn't have a driver base */
1006 /* Find the driver that unloaded at this address */
1007 KiBugCheckDriver
= NULL
; // FIXME: ROS can't locate
1009 /* Check if the cause was an unloaded driver */
1010 if (KiBugCheckDriver
)
1013 DRIVER_UNLOADED_WITHOUT_CANCELLING_PENDING_OPERATIONS
;
1018 /* Check if the driver forgot to unlock pages */
1019 case DRIVER_LEFT_LOCKED_PAGES_IN_PROCESS
:
1021 /* EIP is in parameter 1 */
1022 Eip
= (PVOID
)BugCheckParameter1
;
1025 /* Check if the driver consumed too many PTEs */
1026 case DRIVER_USED_EXCESSIVE_PTES
:
1028 /* Loader entry is in parameter 1 */
1029 LdrEntry
= (PVOID
)BugCheckParameter1
;
1030 KiBugCheckDriver
= &LdrEntry
->BaseDllName
;
1033 /* Check if the driver has a stuck thread */
1034 case THREAD_STUCK_IN_DEVICE_DRIVER
:
1036 /* The name is in Parameter 3 */
1037 KiBugCheckDriver
= (PVOID
)BugCheckParameter3
;
1045 /* Do we have a driver name? */
1046 if (KiBugCheckDriver
)
1048 /* Convert it to ANSI */
1049 KeBugCheckUnicodeToAnsi(KiBugCheckDriver
, AnsiName
, sizeof(AnsiName
));
1053 /* Do we have an EIP? */
1056 /* Dump image name */
1057 KiDumpParameterImages(AnsiName
,
1060 KeBugCheckUnicodeToAnsi
);
1064 /* Check if we need to save the context for KD */
1066 if (!KdPitchDebugger
) KdDebuggerDataBlock
.SavedContext
= (ULONG
)&Context
;
1069 /* Check if a debugger is connected */
1070 if ((BugCheckCode
!= MANUALLY_INITIATED_CRASH
) && (KdDebuggerEnabled
))
1072 /* Crash on the debugger console */
1073 DbgPrint("\n*** Fatal System Error: 0x%08lx\n"
1074 " (0x%p,0x%p,0x%p,0x%p)\n\n",
1081 /* Check if the debugger isn't currently connected */
1082 if (!KdDebuggerNotPresent
)
1084 /* Check if we have a driver to blame */
1085 if (KiBugCheckDriver
)
1088 DbgPrint("Driver at fault: %s.\n", AnsiName
);
1091 /* Check if this was a hard error */
1094 /* Print caption and message */
1095 if (HardErrCaption
) DbgPrint(HardErrCaption
);
1096 if (HardErrMessage
) DbgPrint(HardErrMessage
);
1099 /* Break in the debugger */
1100 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_FIRST
);
1106 * Ok, so debugging is enabled, but KDBG isn't there.
1107 * We'll manually dump the stack for the user.
1109 KeRosDumpStackFrames(NULL
, 0);
1111 /* ROS HACK 2: Generate something useful for Bugzilla */
1112 KeRosDumpTriageForBugZillaReport();
1116 /* Raise IRQL to HIGH_LEVEL */
1118 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1120 /* Avoid recursion */
1121 if (!InterlockedDecrement((PLONG
)&KeBugCheckCount
))
1124 /* Set CPU that is bug checking now */
1125 KeBugCheckOwner
= Prcb
->Number
;
1127 /* Freeze the other CPUs */
1128 for (i
= 0; i
< KeNumberProcessors
; i
++)
1130 if (i
!= (LONG
)KeGetCurrentProcessorNumber())
1132 /* Send the IPI and give them one second to catch up */
1133 KiIpiSendRequest(1 << i
, IPI_FREEZE
);
1134 KeStallExecutionProcessor(1000000);
1139 /* Display the BSOD */
1140 KeLowerIrql(APC_LEVEL
); // This is a nastier hack than any ever before
1141 KiDisplayBlueScreen(MessageId
,
1146 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1148 /* Check if the debugger is disabled but we can enable it */
1149 if (!(KdDebuggerEnabled
) && !(KdPitchDebugger
))
1153 KdEnableDebuggerWithLock(FALSE
);
1158 /* Otherwise, print the last line */
1159 InbvDisplayString("\r\n");
1162 /* Save the context */
1163 Prcb
->ProcessorState
.ContextFrame
= Context
;
1165 /* FIXME: Support Triage Dump */
1167 /* Write the crash dump */
1168 MmDumpToPagingFile(KiBugCheckData
[4],
1177 /* Increase recursion count */
1178 KeBugCheckOwnerRecursionCount
++;
1179 if (KeBugCheckOwnerRecursionCount
== 2)
1181 /* Break in the debugger */
1182 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_SECOND
);
1184 else if (KeBugCheckOwnerRecursionCount
> 2)
1187 for (;;) KeArchHaltProcessor();
1191 /* Call the Callbacks */
1192 KiDoBugCheckCallbacks();
1194 /* FIXME: Call Watchdog if enabled */
1196 /* Check if we have to reboot */
1199 /* Unload symbols */
1200 DbgUnLoadImageSymbols(NULL
, NtCurrentProcess(), 0);
1201 HalReturnToFirmware(HalRebootRoutine
);
1204 /* Attempt to break in the debugger (otherwise halt CPU) */
1205 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_SECOND
);
1208 /* PUBLIC FUNCTIONS **********************************************************/
1215 KeInitializeCrashDumpHeader(IN ULONG Type
,
1218 IN ULONG BufferSize
,
1219 OUT ULONG BufferNeeded OPTIONAL
)
1222 return STATUS_UNSUCCESSFUL
;
1230 KeDeregisterBugCheckCallback(IN PKBUGCHECK_CALLBACK_RECORD CallbackRecord
)
1233 BOOLEAN Status
= FALSE
;
1235 /* Raise IRQL to High */
1236 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1238 /* Check the Current State */
1239 if (CallbackRecord
->State
== BufferInserted
)
1241 /* Reset state and remove from list */
1242 CallbackRecord
->State
= BufferEmpty
;
1243 RemoveEntryList(&CallbackRecord
->Entry
);
1247 /* Lower IRQL and return */
1248 KeLowerIrql(OldIrql
);
1257 KeDeregisterBugCheckReasonCallback(
1258 IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord
)
1261 BOOLEAN Status
= FALSE
;
1263 /* Raise IRQL to High */
1264 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1266 /* Check the Current State */
1267 if (CallbackRecord
->State
== BufferInserted
)
1269 /* Reset state and remove from list */
1270 CallbackRecord
->State
= BufferEmpty
;
1271 RemoveEntryList(&CallbackRecord
->Entry
);
1275 /* Lower IRQL and return */
1276 KeLowerIrql(OldIrql
);
1285 KeDeregisterNmiCallback(PVOID Handle
)
1288 return STATUS_UNSUCCESSFUL
;
1296 KeRegisterBugCheckCallback(IN PKBUGCHECK_CALLBACK_RECORD CallbackRecord
,
1297 IN PKBUGCHECK_CALLBACK_ROUTINE CallbackRoutine
,
1300 IN PUCHAR Component
)
1303 BOOLEAN Status
= FALSE
;
1305 /* Raise IRQL to High */
1306 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1308 /* Check the Current State first so we don't double-register */
1309 if (CallbackRecord
->State
== BufferEmpty
)
1311 /* Set the Callback Settings and insert into the list */
1312 CallbackRecord
->Length
= Length
;
1313 CallbackRecord
->Buffer
= Buffer
;
1314 CallbackRecord
->Component
= Component
;
1315 CallbackRecord
->CallbackRoutine
= CallbackRoutine
;
1316 CallbackRecord
->State
= BufferInserted
;
1317 InsertTailList(&KeBugcheckCallbackListHead
, &CallbackRecord
->Entry
);
1321 /* Lower IRQL and return */
1322 KeLowerIrql(OldIrql
);
1331 KeRegisterBugCheckReasonCallback(
1332 IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord
,
1333 IN PKBUGCHECK_REASON_CALLBACK_ROUTINE CallbackRoutine
,
1334 IN KBUGCHECK_CALLBACK_REASON Reason
,
1335 IN PUCHAR Component
)
1338 BOOLEAN Status
= FALSE
;
1340 /* Raise IRQL to High */
1341 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1343 /* Check the Current State first so we don't double-register */
1344 if (CallbackRecord
->State
== BufferEmpty
)
1346 /* Set the Callback Settings and insert into the list */
1347 CallbackRecord
->Component
= Component
;
1348 CallbackRecord
->CallbackRoutine
= CallbackRoutine
;
1349 CallbackRecord
->State
= BufferInserted
;
1350 CallbackRecord
->Reason
= Reason
;
1351 InsertTailList(&KeBugcheckReasonCallbackListHead
,
1352 &CallbackRecord
->Entry
);
1356 /* Lower IRQL and return */
1357 KeLowerIrql(OldIrql
);
1366 KeRegisterNmiCallback(IN PNMI_CALLBACK CallbackRoutine
,
1378 KeBugCheckEx(IN ULONG BugCheckCode
,
1379 IN ULONG_PTR BugCheckParameter1
,
1380 IN ULONG_PTR BugCheckParameter2
,
1381 IN ULONG_PTR BugCheckParameter3
,
1382 IN ULONG_PTR BugCheckParameter4
)
1384 /* Call the internal API */
1385 KeBugCheckWithTf(BugCheckCode
,
1398 KeBugCheck(ULONG BugCheckCode
)
1400 /* Call the internal API */
1401 KeBugCheckWithTf(BugCheckCode
, 0, 0, 0, 0, NULL
);
1409 KeEnterKernelDebugger(VOID
)
1411 /* Disable interrupts */
1412 KiHardwareTrigger
= 1;
1415 /* Check the bugcheck count */
1416 if (!InterlockedDecrement((PLONG
)&KeBugCheckCount
))
1418 /* There was only one, is the debugger disabled? */
1419 if (!(KdDebuggerEnabled
) && !(KdPitchDebugger
))
1421 /* Enable the debugger */
1422 KdInitSystem(0, NULL
);
1427 KiBugCheckDebugBreak(DBG_STATUS_FATAL
);