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];
31 PKNMI_HANDLER_CALLBACK KiNmiCallbackListHead
;
32 KSPIN_LOCK KiNmiCallbackListLock
;
34 /* Bugzilla Reporting */
35 UNICODE_STRING KeRosProcessorName
, KeRosBiosDate
, KeRosBiosVersion
;
36 UNICODE_STRING KeRosVideoBiosDate
, KeRosVideoBiosVersion
;
38 /* PRIVATE FUNCTIONS *********************************************************/
42 KiPcToFileHeader(IN PVOID Pc
,
43 OUT PLDR_DATA_TABLE_ENTRY
*LdrEntry
,
44 IN BOOLEAN DriversOnly
,
45 OUT PBOOLEAN InKernel
)
48 PVOID ImageBase
, PcBase
= NULL
;
49 PLDR_DATA_TABLE_ENTRY Entry
;
50 PLIST_ENTRY ListHead
, NextEntry
;
52 /* Check which list we should use */
53 ListHead
= (KeLoaderBlock
) ? &KeLoaderBlock
->LoadOrderListHead
:
59 /* Set list pointers and make sure it's valid */
60 NextEntry
= ListHead
->Flink
;
64 while (NextEntry
!= ListHead
)
69 /* Check if this is a kernel entry and we only want drivers */
70 if ((i
<= 2) && (DriversOnly
== TRUE
))
73 NextEntry
= NextEntry
->Flink
;
77 /* Get the loader entry */
78 Entry
= CONTAINING_RECORD(NextEntry
,
82 /* Move to the next entry */
83 NextEntry
= NextEntry
->Flink
;
84 ImageBase
= Entry
->DllBase
;
86 /* Check if this is the right one */
87 if (((ULONG_PTR
)Pc
>= (ULONG_PTR
)Entry
->DllBase
) &&
88 ((ULONG_PTR
)Pc
< ((ULONG_PTR
)Entry
->DllBase
+ Entry
->SizeOfImage
)))
90 /* Return this entry */
94 /* Check if this was a kernel or HAL entry */
95 if (i
<= 2) *InKernel
= TRUE
;
101 /* Return the base address */
107 KiRosPrintAddress(PVOID address
)
109 PLIST_ENTRY current_entry
;
110 PLDR_DATA_TABLE_ENTRY current
;
111 extern LIST_ENTRY PsLoadedModuleList
;
112 ULONG_PTR RelativeAddress
;
117 current_entry
= PsLoadedModuleList
.Flink
;
119 while (current_entry
!= &PsLoadedModuleList
)
121 current
= CONTAINING_RECORD(current_entry
,
122 LDR_DATA_TABLE_ENTRY
,
125 if (address
>= (PVOID
)current
->DllBase
&&
126 address
< (PVOID
)((ULONG_PTR
)current
->DllBase
+
127 current
->SizeOfImage
))
129 RelativeAddress
= (ULONG_PTR
)address
-
130 (ULONG_PTR
)current
->DllBase
;
131 DbgPrint("<%wZ: %x>", ¤t
->FullDllName
, RelativeAddress
);
134 current_entry
= current_entry
->Flink
;
143 KiRosPcToUserFileHeader(IN PVOID Pc
,
144 OUT PLDR_DATA_TABLE_ENTRY
*LdrEntry
)
146 PVOID ImageBase
, PcBase
= NULL
;
147 PLDR_DATA_TABLE_ENTRY Entry
;
148 PLIST_ENTRY ListHead
, NextEntry
;
151 * We know this is valid because we should only be called after a
152 * succesfull address from RtlWalkFrameChain for UserMode, which
153 * validates everything for us.
155 ListHead
= &KeGetCurrentThread()->
156 Teb
->ProcessEnvironmentBlock
->Ldr
->InLoadOrderModuleList
;
158 /* Set list pointers and make sure it's valid */
159 NextEntry
= ListHead
->Flink
;
163 while (NextEntry
!= ListHead
)
165 /* Get the loader entry */
166 Entry
= CONTAINING_RECORD(NextEntry
,
167 LDR_DATA_TABLE_ENTRY
,
170 /* Move to the next entry */
171 NextEntry
= NextEntry
->Flink
;
172 ImageBase
= Entry
->DllBase
;
174 /* Check if this is the right one */
175 if (((ULONG_PTR
)Pc
>= (ULONG_PTR
)Entry
->DllBase
) &&
176 ((ULONG_PTR
)Pc
< ((ULONG_PTR
)Entry
->DllBase
+ Entry
->SizeOfImage
)))
178 /* Return this entry */
186 /* Return the base address */
192 KeRosCaptureUserStackBackTrace(IN ULONG FramesToSkip
,
193 IN ULONG FramesToCapture
,
194 OUT PVOID
*BackTrace
,
195 OUT PULONG BackTraceHash OPTIONAL
)
197 PVOID Frames
[2 * 64];
201 /* Skip a frame for the caller */
204 /* Don't go past the limit */
205 if ((FramesToCapture
+ FramesToSkip
) >= 128) return 0;
207 /* Do the back trace */
208 FrameCount
= RtlWalkFrameChain(Frames
, FramesToCapture
+ FramesToSkip
, 1);
210 /* Make sure we're not skipping all of them */
211 if (FrameCount
<= FramesToSkip
) return 0;
213 /* Loop all the frames */
214 for (i
= 0; i
< FramesToCapture
; i
++)
216 /* Don't go past the limit */
217 if ((FramesToSkip
+ i
) >= FrameCount
) break;
219 /* Save this entry and hash it */
220 BackTrace
[i
] = Frames
[FramesToSkip
+ i
];
221 Hash
+= PtrToUlong(BackTrace
[i
]);
225 if (BackTraceHash
) *BackTraceHash
= Hash
;
227 /* Clear the other entries and return count */
228 RtlFillMemoryUlong(Frames
, 128, 0);
235 KeRosDumpStackFrameArray(IN PULONG Frames
,
242 /* GCC complaints that it may be used uninitialized */
243 PLDR_DATA_TABLE_ENTRY LdrEntry
= NULL
;
246 for (i
= 0; i
< FrameCount
; i
++)
255 /* Get the base for this file */
256 if (Addr
> (ULONG_PTR
)MmHighestUserAddress
)
258 /* We are in kernel */
259 p
= KiPcToFileHeader((PVOID
)Addr
, &LdrEntry
, FALSE
, &InSystem
);
263 /* We are in user land */
264 p
= KiRosPcToUserFileHeader((PVOID
)Addr
, &LdrEntry
);
269 if (!KdbSymPrintAddress((PVOID
)Addr
))
272 /* Print out the module name */
273 Addr
-= (ULONG_PTR
)LdrEntry
->DllBase
;
274 DbgPrint("<%wZ: %x>\n", &LdrEntry
->FullDllName
, Addr
);
279 /* Print only the address */
280 DbgPrint("<%x>\n", Addr
);
283 /* Go to the next frame */
290 KeRosDumpStackFrames(IN PULONG Frame OPTIONAL
,
291 IN ULONG FrameCount OPTIONAL
)
294 ULONG RealFrameCount
;
296 /* If the caller didn't ask, assume 32 frames */
297 if (!FrameCount
|| FrameCount
> 32) FrameCount
= 32;
302 KeRosDumpStackFrameArray(Frame
, FrameCount
);
306 /* Get the current frames (skip the two. One for the dumper, one for the caller) */
307 RealFrameCount
= RtlCaptureStackBackTrace(2, FrameCount
, (PVOID
*)Frames
, NULL
);
310 KeRosDumpStackFrameArray(Frames
, RealFrameCount
);
312 /* Count left for user mode? */
313 if (FrameCount
- RealFrameCount
> 0)
315 /* Get the current frames */
316 RealFrameCount
= KeRosCaptureUserStackBackTrace(-1, FrameCount
- RealFrameCount
, (PVOID
*)Frames
, NULL
);
319 KeRosDumpStackFrameArray(Frames
, RealFrameCount
);
326 KeRosDumpTriageForBugZillaReport(VOID
)
329 extern BOOLEAN KiFastSystemCallDisable
, KiSMTProcessorsPresent
;
330 extern ULONG KeI386MachineType
, MxcsrFeatureMask
;
331 extern BOOLEAN Ke386Pae
, Ke386NoExecute
;
333 DbgPrint("ReactOS has crashed! Please go to http://www.reactos.org/bugzilla/enter_bug.cgi to file a bug!\n");
334 DbgPrint("\nHardware Information\n");
335 DbgPrint("Processor Architecture: %d\n"
337 "System Call Disabled: %d\n"
340 "MXCsr Feature Mask: %d\n"
347 "Active Processors: %d\n"
348 "Pentium LOCK Bug: %d\n"
349 "Hyperthreading: %d\n"
350 "CPU Manufacturer: %s\n"
358 "BIOS Version: %wZ\n"
359 "Video BIOS Date: %wZ\n"
360 "Video BIOS Version: %wZ\n"
362 KeProcessorArchitecture
,
364 KiFastSystemCallDisable
,
375 KiI386PentiumLockErrataPresent
,
376 KiSMTProcessorsPresent
,
377 KeGetCurrentPrcb()->VendorString
,
379 KeGetCurrentPrcb()->CpuID
,
380 KeGetCurrentPrcb()->CpuType
,
381 KeGetCurrentPrcb()->CpuStep
,
382 KeGetCurrentPrcb()->MHz
,
383 ((PKIPCR
)KeGetPcr())->SecondLevelCacheSize
,
387 &KeRosVideoBiosVersion
,
388 MmNumberOfPhysicalPages
* PAGE_SIZE
);
395 KiInitializeBugCheck(VOID
)
397 PRTL_MESSAGE_RESOURCE_DATA BugCheckData
;
398 LDR_RESOURCE_INFO ResourceInfo
;
399 PIMAGE_RESOURCE_DATA_ENTRY ResourceDataEntry
;
401 PLDR_DATA_TABLE_ENTRY LdrEntry
;
403 /* Get the kernel entry */
404 LdrEntry
= CONTAINING_RECORD(KeLoaderBlock
->LoadOrderListHead
.Flink
,
405 LDR_DATA_TABLE_ENTRY
,
408 /* Cache the Bugcheck Message Strings. Prepare the Lookup Data */
409 ResourceInfo
.Type
= 11;
410 ResourceInfo
.Name
= 1;
411 ResourceInfo
.Language
= 9;
414 Status
= LdrFindResource_U(LdrEntry
->DllBase
,
419 /* Make sure it worked */
420 if (NT_SUCCESS(Status
))
422 /* Now actually get a pointer to it */
423 Status
= LdrAccessResource(LdrEntry
->DllBase
,
425 (PVOID
*)&BugCheckData
,
427 if (NT_SUCCESS(Status
)) KiBugCodeMessages
= BugCheckData
;
433 KeGetBugMessageText(IN ULONG BugCheckCode
,
434 OUT PANSI_STRING OutputString OPTIONAL
)
438 ULONG_PTR MessageEntry
;
440 BOOLEAN Result
= FALSE
;
442 /* Make sure we're not bugchecking too early */
443 if (!KiBugCodeMessages
) return Result
;
445 /* Find the message. This code is based on RtlFindMesssage */
446 for (i
= 0; i
< KiBugCodeMessages
->NumberOfBlocks
; i
++)
448 /* Check if the ID Matches */
449 if ((BugCheckCode
>= KiBugCodeMessages
->Blocks
[i
].LowId
) &&
450 (BugCheckCode
<= KiBugCodeMessages
->Blocks
[i
].HighId
))
452 /* Get Offset to Entry */
453 MessageEntry
= KiBugCodeMessages
->Blocks
[i
].OffsetToEntries
+
454 (ULONG_PTR
)KiBugCodeMessages
;
455 IdOffset
= BugCheckCode
- KiBugCodeMessages
->Blocks
[i
].LowId
;
457 /* Get offset to ID */
458 for (i
= 0; i
< IdOffset
; i
++)
460 /* Advance in the Entries */
461 MessageEntry
+= ((PRTL_MESSAGE_RESOURCE_ENTRY
)MessageEntry
)->
465 /* Get the final Code */
466 BugCode
= ((PRTL_MESSAGE_RESOURCE_ENTRY
)MessageEntry
)->Text
;
469 /* Handle newlines */
470 while ((i
> 0) && ((BugCode
[i
] == '\n') ||
471 (BugCode
[i
] == '\r') ||
472 (BugCode
[i
] == ANSI_NULL
)))
474 /* Check if we have a string to return */
475 if (!OutputString
) BugCode
[i
] = ANSI_NULL
;
479 /* Check if caller wants an output string */
482 /* Return it in the OutputString */
483 OutputString
->Buffer
= BugCode
;
484 OutputString
->Length
= (USHORT
)i
+ 1;
485 OutputString
->MaximumLength
= (USHORT
)i
+ 1;
489 /* Direct Output to Screen */
490 InbvDisplayString(BugCode
);
491 InbvDisplayString("\r");
500 /* Return the result */
506 KiDoBugCheckCallbacks(VOID
)
508 PKBUGCHECK_CALLBACK_RECORD CurrentRecord
;
509 PLIST_ENTRY ListHead
, NextEntry
, LastEntry
;
512 /* First make sure that the list is Initialized... it might not be */
513 ListHead
= &KeBugcheckCallbackListHead
;
514 if ((ListHead
->Flink
) && (ListHead
->Blink
))
517 LastEntry
= ListHead
;
518 NextEntry
= ListHead
->Flink
;
519 while (NextEntry
!= ListHead
)
522 CurrentRecord
= CONTAINING_RECORD(NextEntry
,
523 KBUGCHECK_CALLBACK_RECORD
,
527 if (CurrentRecord
->Entry
.Blink
!= LastEntry
) return;
528 Checksum
= (ULONG_PTR
)CurrentRecord
->CallbackRoutine
;
529 Checksum
+= (ULONG_PTR
)CurrentRecord
->Buffer
;
530 Checksum
+= (ULONG_PTR
)CurrentRecord
->Length
;
531 Checksum
+= (ULONG_PTR
)CurrentRecord
->Component
;
533 /* Make sure it's inserted and valitdated */
534 if ((CurrentRecord
->State
== BufferInserted
) &&
535 (CurrentRecord
->Checksum
== Checksum
))
537 /* Call the routine */
538 CurrentRecord
->State
= BufferStarted
;
539 (CurrentRecord
->CallbackRoutine
)(CurrentRecord
->Buffer
,
540 CurrentRecord
->Length
);
541 CurrentRecord
->State
= BufferFinished
;
544 /* Go to the next entry */
545 LastEntry
= NextEntry
;
546 NextEntry
= NextEntry
->Flink
;
553 KiBugCheckDebugBreak(IN ULONG StatusCode
)
556 * Wrap this in SEH so we don't crash if
557 * there is no debugger or if it disconnected
563 DbgBreakPointWithStatus(StatusCode
);
565 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
567 /* No debugger, halt the CPU */
572 /* Break again if this wasn't first try */
573 if (StatusCode
!= DBG_STATUS_BUGCHECK_FIRST
) goto DoBreak
;
578 KeBugCheckUnicodeToAnsi(IN PUNICODE_STRING Unicode
,
586 /* Set length and normalize it */
587 i
= Unicode
->Length
/ sizeof(WCHAR
);
588 i
= min(i
, Length
- 1);
590 /* Set source and destination, and copy */
591 pw
= Unicode
->Buffer
;
593 while (i
--) *p
++ = (CHAR
)*pw
++;
595 /* Null terminate and return */
602 KiDumpParameterImages(IN PCHAR Message
,
603 IN PULONG_PTR Parameters
,
604 IN ULONG ParameterCount
,
605 IN PKE_BUGCHECK_UNICODE_TO_ANSI ConversionRoutine
)
609 PLDR_DATA_TABLE_ENTRY LdrEntry
;
611 PUNICODE_STRING DriverName
;
613 PIMAGE_NT_HEADERS NtHeader
;
615 BOOLEAN FirstRun
= TRUE
;
617 /* Loop parameters */
618 for (i
= 0; i
< ParameterCount
; i
++)
620 /* Get the base for this parameter */
621 ImageBase
= KiPcToFileHeader((PVOID
)Parameters
[i
],
627 /* FIXME: Add code to check for unloaded drivers */
628 DPRINT1("Potentially unloaded driver!\n");
633 /* Get the NT Headers and Timestamp */
634 NtHeader
= RtlImageNtHeader(LdrEntry
->DllBase
);
635 TimeStamp
= NtHeader
->FileHeader
.TimeDateStamp
;
637 /* Convert the driver name */
638 DriverName
= &LdrEntry
->BaseDllName
;
639 ConversionRoutine(&LdrEntry
->BaseDllName
,
644 /* Format driver name */
646 "%s** %12s - Address %p base at %p, DateStamp %08lx\n",
647 FirstRun
? "\r\n*":"*",
649 (PVOID
)Parameters
[i
],
653 /* Check if we only had one parameter */
654 if (ParameterCount
<= 1)
656 /* Then just save the name */
657 KiBugCheckDriver
= DriverName
;
661 /* Otherwise, display the message */
662 InbvDisplayString(Message
);
672 KiDisplayBlueScreen(IN ULONG MessageId
,
673 IN BOOLEAN IsHardError
,
674 IN PCHAR HardErrCaption OPTIONAL
,
675 IN PCHAR HardErrMessage OPTIONAL
,
680 /* Check if bootvid is installed */
681 if (InbvIsBootDriverInstalled())
683 /* Acquire ownership and reset the display */
684 InbvAcquireDisplayOwnership();
687 /* Display blue screen */
688 InbvSolidColorFill(0, 0, 639, 479, 4);
689 InbvSetTextColor(15);
690 InbvInstallDisplayStringFilter(NULL
);
691 InbvEnableDisplayString(TRUE
);
692 InbvSetScrollRegion(0, 0, 639, 479);
695 /* Check if this is a hard error */
698 /* Display caption and message */
699 if (HardErrCaption
) InbvDisplayString(HardErrCaption
);
700 if (HardErrMessage
) InbvDisplayString(HardErrMessage
);
703 /* Begin the display */
704 InbvDisplayString("\r\n");
706 /* Print out initial message */
707 KeGetBugMessageText(BUGCHECK_MESSAGE_INTRO
, NULL
);
708 InbvDisplayString("\r\n\r\n");
710 /* Check if we have a driver */
711 if (KiBugCheckDriver
)
713 /* Print out into to driver name */
714 KeGetBugMessageText(BUGCODE_ID_DRIVER
, NULL
);
716 /* Convert and print out driver name */
717 KeBugCheckUnicodeToAnsi(KiBugCheckDriver
, AnsiName
, sizeof(AnsiName
));
718 InbvDisplayString(" ");
719 InbvDisplayString(AnsiName
);
720 InbvDisplayString("\r\n\r\n");
723 /* Check if this is the generic message */
724 if (MessageId
== BUGCODE_PSS_MESSAGE
)
726 /* It is, so get the bug code string as well */
727 KeGetBugMessageText(KiBugCheckData
[0], NULL
);
728 InbvDisplayString("\r\n\r\n");
731 /* Print second introduction message */
732 KeGetBugMessageText(PSS_MESSAGE_INTRO
, NULL
);
733 InbvDisplayString("\r\n\r\n");
735 /* Get the bug code string */
736 KeGetBugMessageText(MessageId
, NULL
);
737 InbvDisplayString("\r\n\r\n");
739 /* Print message for technical information */
740 KeGetBugMessageText(BUGCHECK_TECH_INFO
, NULL
);
742 /* Show the technical Data */
744 "\r\n\r\n*** STOP: 0x%08lX (0x%p,0x%p,0x%p,0x%p)\r\n\r\n",
746 (PVOID
)KiBugCheckData
[1],
747 (PVOID
)KiBugCheckData
[2],
748 (PVOID
)KiBugCheckData
[3],
749 (PVOID
)KiBugCheckData
[4]);
750 InbvDisplayString(AnsiName
);
752 /* Check if we have a driver*/
753 if (KiBugCheckDriver
)
755 /* Display technical driver data */
756 InbvDisplayString(Message
);
760 /* Dump parameter information */
761 KiDumpParameterImages(Message
,
762 (PVOID
)&KiBugCheckData
[1],
764 KeBugCheckUnicodeToAnsi
);
770 KeBugCheckWithTf(IN ULONG BugCheckCode
,
771 IN ULONG_PTR BugCheckParameter1
,
772 IN ULONG_PTR BugCheckParameter2
,
773 IN ULONG_PTR BugCheckParameter3
,
774 IN ULONG_PTR BugCheckParameter4
,
775 IN PKTRAP_FRAME TrapFrame
)
777 PKPRCB Prcb
= KeGetCurrentPrcb();
781 BOOLEAN IsSystem
, IsHardError
= FALSE
, Reboot
= FALSE
;
782 PCHAR HardErrCaption
= NULL
, HardErrMessage
= NULL
;
783 PVOID Pc
= NULL
, Memory
;
785 PLDR_DATA_TABLE_ENTRY LdrEntry
;
786 PULONG_PTR HardErrorParameters
;
792 /* Set active bugcheck */
793 KeBugCheckActive
= TRUE
;
794 KiBugCheckDriver
= NULL
;
796 /* Check if this is power failure simulation */
797 if (BugCheckCode
== POWER_FAILURE_SIMULATE
)
799 /* Call the Callbacks and reboot */;
800 KiDoBugCheckCallbacks();
801 HalReturnToFirmware(HalRebootRoutine
);
804 /* Save the IRQL and set hardware trigger */
805 Prcb
->DebuggerSavedIRQL
= KeGetCurrentIrql();
806 InterlockedIncrement((PLONG
)&KiHardwareTrigger
);
808 /* Capture the CPU Context */
809 RtlCaptureContext(&Prcb
->ProcessorState
.ContextFrame
);
810 KiSaveProcessorControlState(&Prcb
->ProcessorState
);
811 Context
= Prcb
->ProcessorState
.ContextFrame
;
813 /* FIXME: Call the Watchdog if it's registered */
815 /* Check which bugcode this is */
816 switch (BugCheckCode
)
818 /* These bug checks already have detailed messages, keep them */
819 case UNEXPECTED_KERNEL_MODE_TRAP
:
820 case DRIVER_CORRUPTED_EXPOOL
:
821 case ACPI_BIOS_ERROR
:
822 case ACPI_BIOS_FATAL_ERROR
:
823 case THREAD_STUCK_IN_DEVICE_DRIVER
:
825 case FAT_FILE_SYSTEM
:
826 case NO_MORE_SYSTEM_PTES
:
827 case INACCESSIBLE_BOOT_DEVICE
:
829 /* Keep the same code */
830 MessageId
= BugCheckCode
;
833 /* Check if this is a kernel-mode exception */
834 case KERNEL_MODE_EXCEPTION_NOT_HANDLED
:
835 case SYSTEM_THREAD_EXCEPTION_NOT_HANDLED
:
836 case KMODE_EXCEPTION_NOT_HANDLED
:
838 /* Use the generic text message */
839 MessageId
= KMODE_EXCEPTION_NOT_HANDLED
;
842 /* File-system errors */
843 case NTFS_FILE_SYSTEM
:
845 /* Use the generic message for FAT */
846 MessageId
= FAT_FILE_SYSTEM
;
849 /* Check if this is a coruption of the Mm's Pool */
850 case DRIVER_CORRUPTED_MMPOOL
:
852 /* Use generic corruption message */
853 MessageId
= DRIVER_CORRUPTED_EXPOOL
;
856 /* Check if this is a signature check failure */
857 case STATUS_SYSTEM_IMAGE_BAD_SIGNATURE
:
859 /* Use the generic corruption message */
860 MessageId
= BUGCODE_PSS_MESSAGE_SIGNATURE
;
863 /* All other codes */
866 /* Use the default bugcheck message */
867 MessageId
= BUGCODE_PSS_MESSAGE
;
871 /* Save bugcheck data */
872 KiBugCheckData
[0] = BugCheckCode
;
873 KiBugCheckData
[1] = BugCheckParameter1
;
874 KiBugCheckData
[2] = BugCheckParameter2
;
875 KiBugCheckData
[3] = BugCheckParameter3
;
876 KiBugCheckData
[4] = BugCheckParameter4
;
878 /* Now check what bugcheck this is */
879 switch (BugCheckCode
)
881 /* Invalid access to R/O memory or Unhandled KM Exception */
882 case KERNEL_MODE_EXCEPTION_NOT_HANDLED
:
883 case ATTEMPTED_WRITE_TO_READONLY_MEMORY
:
884 case ATTEMPTED_EXECUTE_OF_NOEXECUTE_MEMORY
:
886 /* Check if we have a trap frame */
889 /* Use parameter 3 as a trap frame, if it exists */
890 if (BugCheckParameter3
) TrapFrame
= (PVOID
)BugCheckParameter3
;
893 /* Check if we got one now and if we need to get the Program Counter */
895 (BugCheckCode
!= KERNEL_MODE_EXCEPTION_NOT_HANDLED
))
897 /* Get the Program Counter */
898 Pc
= (PVOID
)KeGetTrapFramePc(TrapFrame
);
903 case IRQL_NOT_LESS_OR_EQUAL
:
906 * The NT kernel has 3 special sections:
907 * MISYSPTE, POOLMI and POOLCODE. The bug check code can
908 * determine in which of these sections this bugcode happened
909 * and provide a more detailed analysis. For now, we don't.
912 /* Program Counter is in parameter 4 */
913 Pc
= (PVOID
)BugCheckParameter4
;
915 /* Get the driver base */
916 DriverBase
= KiPcToFileHeader(Pc
,
923 * The error happened inside the kernel or HAL.
924 * Get the memory address that was being referenced.
926 Memory
= (PVOID
)BugCheckParameter1
;
928 /* Find to which driver it belongs */
929 DriverBase
= KiPcToFileHeader(Memory
,
935 /* Get the driver name and update the bug code */
936 KiBugCheckDriver
= &LdrEntry
->BaseDllName
;
937 KiBugCheckData
[0] = DRIVER_PORTION_MUST_BE_NONPAGED
;
941 /* Find the driver that unloaded at this address */
942 KiBugCheckDriver
= NULL
; // FIXME: ROS can't locate
944 /* Check if the cause was an unloaded driver */
945 if (KiBugCheckDriver
)
947 /* Update bug check code */
949 SYSTEM_SCAN_AT_RAISED_IRQL_CAUGHT_IMPROPER_DRIVER_UNLOAD
;
955 /* Update the bug check code */
956 KiBugCheckData
[0] = DRIVER_IRQL_NOT_LESS_OR_EQUAL
;
959 /* Clear Pc so we don't look it up later */
964 case FATAL_UNHANDLED_HARD_ERROR
:
966 /* Copy bug check data from hard error */
967 HardErrorParameters
= (PULONG_PTR
)BugCheckParameter2
;
968 KiBugCheckData
[0] = BugCheckParameter1
;
969 KiBugCheckData
[1] = HardErrorParameters
[0];
970 KiBugCheckData
[2] = HardErrorParameters
[1];
971 KiBugCheckData
[3] = HardErrorParameters
[2];
972 KiBugCheckData
[4] = HardErrorParameters
[3];
974 /* Remember that this is hard error and set the caption/message */
976 HardErrCaption
= (PCHAR
)BugCheckParameter3
;
977 HardErrMessage
= (PCHAR
)BugCheckParameter4
;
981 case PAGE_FAULT_IN_NONPAGED_AREA
:
983 /* Assume no driver */
986 /* Check if we have a trap frame */
989 /* We don't, use parameter 3 if possible */
990 if (BugCheckParameter3
) TrapFrame
= (PVOID
)BugCheckParameter3
;
993 /* Check if we have a frame now */
996 /* Get the Program Counter */
997 Pc
= (PVOID
)KeGetTrapFramePc(TrapFrame
);
998 KiBugCheckData
[3] = (ULONG_PTR
)Pc
;
1000 /* Find out if was in the kernel or drivers */
1001 DriverBase
= KiPcToFileHeader(Pc
,
1008 * Now we should check if this happened in:
1009 * 1) Special Pool 2) Free Special Pool 3) Session Pool
1010 * and update the bugcheck code appropriately.
1013 /* Check if we didn't have a driver base */
1016 /* Find the driver that unloaded at this address */
1017 KiBugCheckDriver
= NULL
; // FIXME: ROS can't locate
1019 /* Check if the cause was an unloaded driver */
1020 if (KiBugCheckDriver
)
1023 DRIVER_UNLOADED_WITHOUT_CANCELLING_PENDING_OPERATIONS
;
1028 /* Check if the driver forgot to unlock pages */
1029 case DRIVER_LEFT_LOCKED_PAGES_IN_PROCESS
:
1031 /* Program Counter is in parameter 1 */
1032 Pc
= (PVOID
)BugCheckParameter1
;
1035 /* Check if the driver consumed too many PTEs */
1036 case DRIVER_USED_EXCESSIVE_PTES
:
1038 /* Loader entry is in parameter 1 */
1039 LdrEntry
= (PVOID
)BugCheckParameter1
;
1040 KiBugCheckDriver
= &LdrEntry
->BaseDllName
;
1043 /* Check if the driver has a stuck thread */
1044 case THREAD_STUCK_IN_DEVICE_DRIVER
:
1046 /* The name is in Parameter 3 */
1047 KiBugCheckDriver
= (PVOID
)BugCheckParameter3
;
1055 /* Do we have a driver name? */
1056 if (KiBugCheckDriver
)
1058 /* Convert it to ANSI */
1059 KeBugCheckUnicodeToAnsi(KiBugCheckDriver
, AnsiName
, sizeof(AnsiName
));
1063 /* Do we have a Program Counter? */
1066 /* Dump image name */
1067 KiDumpParameterImages(AnsiName
,
1070 KeBugCheckUnicodeToAnsi
);
1074 /* Check if we need to save the context for KD */
1076 if (!KdPitchDebugger
) KdDebuggerDataBlock
.SavedContext
= (ULONG_PTR
)&Context
;
1079 /* Check if a debugger is connected */
1080 if ((BugCheckCode
!= MANUALLY_INITIATED_CRASH
) && (KdDebuggerEnabled
))
1082 /* Crash on the debugger console */
1083 DbgPrint("\n*** Fatal System Error: 0x%08lx\n"
1084 " (0x%p,0x%p,0x%p,0x%p)\n\n",
1091 /* Check if the debugger isn't currently connected */
1092 if (!KdDebuggerNotPresent
)
1094 /* Check if we have a driver to blame */
1095 if (KiBugCheckDriver
)
1098 DbgPrint("Driver at fault: %s.\n", AnsiName
);
1101 /* Check if this was a hard error */
1104 /* Print caption and message */
1105 if (HardErrCaption
) DbgPrint(HardErrCaption
);
1106 if (HardErrMessage
) DbgPrint(HardErrMessage
);
1109 /* Break in the debugger */
1110 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_FIRST
);
1116 * Ok, so debugging is enabled, but KDBG isn't there.
1117 * We'll manually dump the stack for the user.
1119 KeRosDumpStackFrames(NULL
, 0);
1121 /* ROS HACK 2: Generate something useful for Bugzilla */
1122 KeRosDumpTriageForBugZillaReport();
1126 /* Raise IRQL to HIGH_LEVEL */
1128 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1130 /* Avoid recursion */
1131 if (!InterlockedDecrement((PLONG
)&KeBugCheckCount
))
1134 /* Set CPU that is bug checking now */
1135 KeBugCheckOwner
= Prcb
->Number
;
1137 /* Freeze the other CPUs */
1138 for (i
= 0; i
< KeNumberProcessors
; i
++)
1140 if (i
!= (LONG
)KeGetCurrentProcessorNumber())
1142 /* Send the IPI and give them one second to catch up */
1143 KiIpiSend(1 << i
, IPI_FREEZE
);
1144 KeStallExecutionProcessor(1000000);
1149 /* Display the BSOD */
1150 KiDisplayBlueScreen(MessageId
,
1156 /* Check if the debugger is disabled but we can enable it */
1157 if (!(KdDebuggerEnabled
) && !(KdPitchDebugger
))
1161 KdEnableDebuggerWithLock(FALSE
);
1166 /* Otherwise, print the last line */
1167 InbvDisplayString("\r\n");
1170 /* Save the context */
1171 Prcb
->ProcessorState
.ContextFrame
= Context
;
1173 /* FIXME: Support Triage Dump */
1175 /* FIXME: Write the crash dump */
1179 /* Increase recursion count */
1180 KeBugCheckOwnerRecursionCount
++;
1181 if (KeBugCheckOwnerRecursionCount
== 2)
1183 /* Break in the debugger */
1184 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_SECOND
);
1186 else if (KeBugCheckOwnerRecursionCount
> 2)
1188 /* Halt execution */
1193 /* Call the Callbacks */
1194 KiDoBugCheckCallbacks();
1196 /* FIXME: Call Watchdog if enabled */
1198 /* Check if we have to reboot */
1201 /* Unload symbols */
1202 DbgUnLoadImageSymbols(NULL
, (PVOID
)MAXULONG_PTR
, 0);
1203 HalReturnToFirmware(HalRebootRoutine
);
1206 /* Attempt to break in the debugger (otherwise halt CPU) */
1207 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_SECOND
);
1209 /* Shouldn't get here */
1217 BOOLEAN Handled
= FALSE
;
1218 PKNMI_HANDLER_CALLBACK NmiData
;
1221 // Parse the list of callbacks
1223 NmiData
= KiNmiCallbackListHead
;
1227 // Save if this callback has handled it -- all it takes is one
1229 Handled
|= NmiData
->Callback(NmiData
->Context
, Handled
);
1230 NmiData
= NmiData
->Next
;
1234 // Has anyone handled this?
1239 /* PUBLIC FUNCTIONS **********************************************************/
1246 KeInitializeCrashDumpHeader(IN ULONG Type
,
1249 IN ULONG BufferSize
,
1250 OUT ULONG BufferNeeded OPTIONAL
)
1253 return STATUS_UNSUCCESSFUL
;
1261 KeDeregisterBugCheckCallback(IN PKBUGCHECK_CALLBACK_RECORD CallbackRecord
)
1264 BOOLEAN Status
= FALSE
;
1266 /* Raise IRQL to High */
1267 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1269 /* Check the Current State */
1270 if (CallbackRecord
->State
== BufferInserted
)
1272 /* Reset state and remove from list */
1273 CallbackRecord
->State
= BufferEmpty
;
1274 RemoveEntryList(&CallbackRecord
->Entry
);
1278 /* Lower IRQL and return */
1279 KeLowerIrql(OldIrql
);
1288 KeDeregisterBugCheckReasonCallback(
1289 IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord
)
1292 BOOLEAN Status
= FALSE
;
1294 /* Raise IRQL to High */
1295 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1297 /* Check the Current State */
1298 if (CallbackRecord
->State
== BufferInserted
)
1300 /* Reset state and remove from list */
1301 CallbackRecord
->State
= BufferEmpty
;
1302 RemoveEntryList(&CallbackRecord
->Entry
);
1306 /* Lower IRQL and return */
1307 KeLowerIrql(OldIrql
);
1316 KeRegisterBugCheckCallback(IN PKBUGCHECK_CALLBACK_RECORD CallbackRecord
,
1317 IN PKBUGCHECK_CALLBACK_ROUTINE CallbackRoutine
,
1320 IN PUCHAR Component
)
1323 BOOLEAN Status
= FALSE
;
1325 /* Raise IRQL to High */
1326 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1328 /* Check the Current State first so we don't double-register */
1329 if (CallbackRecord
->State
== BufferEmpty
)
1331 /* Set the Callback Settings and insert into the list */
1332 CallbackRecord
->Length
= Length
;
1333 CallbackRecord
->Buffer
= Buffer
;
1334 CallbackRecord
->Component
= Component
;
1335 CallbackRecord
->CallbackRoutine
= CallbackRoutine
;
1336 CallbackRecord
->State
= BufferInserted
;
1337 InsertTailList(&KeBugcheckCallbackListHead
, &CallbackRecord
->Entry
);
1341 /* Lower IRQL and return */
1342 KeLowerIrql(OldIrql
);
1351 KeRegisterBugCheckReasonCallback(
1352 IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord
,
1353 IN PKBUGCHECK_REASON_CALLBACK_ROUTINE CallbackRoutine
,
1354 IN KBUGCHECK_CALLBACK_REASON Reason
,
1355 IN PUCHAR Component
)
1358 BOOLEAN Status
= FALSE
;
1360 /* Raise IRQL to High */
1361 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1363 /* Check the Current State first so we don't double-register */
1364 if (CallbackRecord
->State
== BufferEmpty
)
1366 /* Set the Callback Settings and insert into the list */
1367 CallbackRecord
->Component
= Component
;
1368 CallbackRecord
->CallbackRoutine
= CallbackRoutine
;
1369 CallbackRecord
->State
= BufferInserted
;
1370 CallbackRecord
->Reason
= Reason
;
1371 InsertTailList(&KeBugcheckReasonCallbackListHead
,
1372 &CallbackRecord
->Entry
);
1376 /* Lower IRQL and return */
1377 KeLowerIrql(OldIrql
);
1386 KeRegisterNmiCallback(IN PNMI_CALLBACK CallbackRoutine
,
1390 PKNMI_HANDLER_CALLBACK NmiData
, Next
;
1391 ASSERT_IRQL_LESS_OR_EQUAL(DISPATCH_LEVEL
);
1394 // Allocate NMI callback data
1396 NmiData
= ExAllocatePoolWithTag(NonPagedPool
,
1397 sizeof(KNMI_HANDLER_CALLBACK
),
1399 if (!NmiData
) return NULL
;
1402 // Fill in the information
1404 NmiData
->Callback
= CallbackRoutine
;
1405 NmiData
->Context
= Context
;
1406 NmiData
->Handle
= NmiData
;
1409 // Insert it into NMI callback list
1411 KiAcquireNmiListLock(&OldIrql
);
1412 NmiData
->Next
= KiNmiCallbackListHead
;
1413 Next
= InterlockedCompareExchangePointer(&KiNmiCallbackListHead
,
1416 ASSERT(Next
== NmiData
->Next
);
1417 KiReleaseNmiListLock(OldIrql
);
1420 // Return the opaque "handle"
1422 return NmiData
->Handle
;
1430 KeDeregisterNmiCallback(PVOID Handle
)
1433 return STATUS_UNSUCCESSFUL
;
1441 KeBugCheckEx(IN ULONG BugCheckCode
,
1442 IN ULONG_PTR BugCheckParameter1
,
1443 IN ULONG_PTR BugCheckParameter2
,
1444 IN ULONG_PTR BugCheckParameter3
,
1445 IN ULONG_PTR BugCheckParameter4
)
1447 /* Call the internal API */
1448 KeBugCheckWithTf(BugCheckCode
,
1461 KeBugCheck(ULONG BugCheckCode
)
1463 /* Call the internal API */
1464 KeBugCheckWithTf(BugCheckCode
, 0, 0, 0, 0, NULL
);
1472 KeEnterKernelDebugger(VOID
)
1474 /* Disable interrupts */
1475 KiHardwareTrigger
= 1;
1478 /* Check the bugcheck count */
1479 if (!InterlockedDecrement((PLONG
)&KeBugCheckCount
))
1481 /* There was only one, is the debugger disabled? */
1482 if (!(KdDebuggerEnabled
) && !(KdPitchDebugger
))
1484 /* Enable the debugger */
1485 KdInitSystem(0, NULL
);
1489 /* Break in the debugger */
1490 KiBugCheckDebugBreak(DBG_STATUS_FATAL
);