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 Pc
,
41 OUT PLDR_DATA_TABLE_ENTRY
*LdrEntry
,
42 IN BOOLEAN DriversOnly
,
43 OUT PBOOLEAN InKernel
)
46 PVOID ImageBase
, PcBase
= 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
)Pc
>= (ULONG_PTR
)Entry
->DllBase
) &&
86 ((ULONG_PTR
)Pc
< ((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 Pc
,
142 OUT PLDR_DATA_TABLE_ENTRY
*LdrEntry
)
144 PVOID ImageBase
, PcBase
= 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
)Pc
>= (ULONG_PTR
)Entry
->DllBase
) &&
174 ((ULONG_PTR
)Pc
< ((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
,
240 /* GCC complaints that it may be used uninitialized */
241 PLDR_DATA_TABLE_ENTRY LdrEntry
= NULL
;
244 for (i
= 0; i
< FrameCount
; i
++)
253 /* Get the base for this file */
254 if (Addr
> (ULONG_PTR
)MmHighestUserAddress
)
256 /* We are in kernel */
257 p
= KiPcToFileHeader((PVOID
)Addr
, &LdrEntry
, FALSE
, &InSystem
);
261 /* We are in user land */
262 p
= KiRosPcToUserFileHeader((PVOID
)Addr
, &LdrEntry
);
267 if (!KdbSymPrintAddress((PVOID
)Addr
))
270 /* Print out the module name */
271 Addr
-= (ULONG_PTR
)LdrEntry
->DllBase
;
272 DbgPrint("<%wZ: %x>\n", &LdrEntry
->FullDllName
, Addr
);
277 /* Print only the address */
278 DbgPrint("<%x>\n", Addr
);
281 /* Go to the next frame */
288 KeRosDumpStackFrames(IN PULONG Frame OPTIONAL
,
289 IN ULONG FrameCount OPTIONAL
)
292 ULONG RealFrameCount
;
294 /* If the caller didn't ask, assume 32 frames */
295 if (!FrameCount
|| FrameCount
> 32) FrameCount
= 32;
300 KeRosDumpStackFrameArray(Frame
, FrameCount
);
304 /* Get the current frames (skip the two. One for the dumper, one for the caller) */
305 RealFrameCount
= RtlCaptureStackBackTrace(2, FrameCount
, (PVOID
*)Frames
, NULL
);
308 KeRosDumpStackFrameArray(Frames
, RealFrameCount
);
310 /* Count left for user mode? */
311 if (FrameCount
- RealFrameCount
> 0)
313 /* Get the current frames */
314 RealFrameCount
= KeRosCaptureUserStackBackTrace(-1, FrameCount
- RealFrameCount
, (PVOID
*)Frames
, NULL
);
317 KeRosDumpStackFrameArray(Frames
, RealFrameCount
);
324 KeRosDumpTriageForBugZillaReport(VOID
)
327 extern BOOLEAN KiFastSystemCallDisable
, KiSMTProcessorsPresent
;
328 extern ULONG KeI386MachineType
, MxcsrFeatureMask
;
329 extern BOOLEAN Ke386Pae
, Ke386NoExecute
;
331 DbgPrint("ReactOS has crashed! Please go to http://www.reactos.org/bugzilla/enter_bug.cgi to file a bug!\n");
332 DbgPrint("\nHardware Information\n");
333 DbgPrint("Processor Architecture: %d\n"
335 "System Call Disabled: %d\n"
338 "MXCsr Feature Mask: %d\n"
345 "Active Processors: %d\n"
346 "Pentium LOCK Bug: %d\n"
347 "Hyperthreading: %d\n"
348 "CPU Manufacturer: %s\n"
356 "BIOS Version: %wZ\n"
357 "Video BIOS Date: %wZ\n"
358 "Video BIOS Version: %wZ\n"
360 KeProcessorArchitecture
,
362 KiFastSystemCallDisable
,
373 KiI386PentiumLockErrataPresent
,
374 KiSMTProcessorsPresent
,
375 KeGetCurrentPrcb()->VendorString
,
377 KeGetCurrentPrcb()->CpuID
,
378 KeGetCurrentPrcb()->CpuType
,
379 KeGetCurrentPrcb()->CpuStep
,
380 KeGetCurrentPrcb()->MHz
,
381 ((PKIPCR
)KeGetPcr())->SecondLevelCacheSize
,
385 &KeRosVideoBiosVersion
,
386 MmNumberOfPhysicalPages
* PAGE_SIZE
);
393 KiInitializeBugCheck(VOID
)
395 PRTL_MESSAGE_RESOURCE_DATA BugCheckData
;
396 LDR_RESOURCE_INFO ResourceInfo
;
397 PIMAGE_RESOURCE_DATA_ENTRY ResourceDataEntry
;
399 PLDR_DATA_TABLE_ENTRY LdrEntry
;
401 /* Get the kernel entry */
402 LdrEntry
= CONTAINING_RECORD(KeLoaderBlock
->LoadOrderListHead
.Flink
,
403 LDR_DATA_TABLE_ENTRY
,
406 /* Cache the Bugcheck Message Strings. Prepare the Lookup Data */
407 ResourceInfo
.Type
= 11;
408 ResourceInfo
.Name
= 1;
409 ResourceInfo
.Language
= 9;
412 Status
= LdrFindResource_U(LdrEntry
->DllBase
,
417 /* Make sure it worked */
418 if (NT_SUCCESS(Status
))
420 /* Now actually get a pointer to it */
421 Status
= LdrAccessResource(LdrEntry
->DllBase
,
423 (PVOID
*)&BugCheckData
,
425 if (NT_SUCCESS(Status
)) KiBugCodeMessages
= BugCheckData
;
431 KeGetBugMessageText(IN ULONG BugCheckCode
,
432 OUT PANSI_STRING OutputString OPTIONAL
)
436 ULONG_PTR MessageEntry
;
438 BOOLEAN Result
= FALSE
;
440 /* Make sure we're not bugchecking too early */
441 if (!KiBugCodeMessages
) return Result
;
443 /* Find the message. This code is based on RtlFindMesssage */
444 for (i
= 0; i
< KiBugCodeMessages
->NumberOfBlocks
; i
++)
446 /* Check if the ID Matches */
447 if ((BugCheckCode
>= KiBugCodeMessages
->Blocks
[i
].LowId
) &&
448 (BugCheckCode
<= KiBugCodeMessages
->Blocks
[i
].HighId
))
450 /* Get Offset to Entry */
451 MessageEntry
= KiBugCodeMessages
->Blocks
[i
].OffsetToEntries
+
452 (ULONG_PTR
)KiBugCodeMessages
;
453 IdOffset
= BugCheckCode
- KiBugCodeMessages
->Blocks
[i
].LowId
;
455 /* Get offset to ID */
456 for (i
= 0; i
< IdOffset
; i
++)
458 /* Advance in the Entries */
459 MessageEntry
+= ((PRTL_MESSAGE_RESOURCE_ENTRY
)MessageEntry
)->
463 /* Get the final Code */
464 BugCode
= ((PRTL_MESSAGE_RESOURCE_ENTRY
)MessageEntry
)->Text
;
467 /* Handle newlines */
468 while ((i
> 0) && ((BugCode
[i
] == '\n') ||
469 (BugCode
[i
] == '\r') ||
470 (BugCode
[i
] == ANSI_NULL
)))
472 /* Check if we have a string to return */
473 if (!OutputString
) BugCode
[i
] = ANSI_NULL
;
477 /* Check if caller wants an output string */
480 /* Return it in the OutputString */
481 OutputString
->Buffer
= BugCode
;
482 OutputString
->Length
= (USHORT
)i
+ 1;
483 OutputString
->MaximumLength
= (USHORT
)i
+ 1;
487 /* Direct Output to Screen */
488 InbvDisplayString(BugCode
);
489 InbvDisplayString("\r");
498 /* Return the result */
504 KiDoBugCheckCallbacks(VOID
)
506 PKBUGCHECK_CALLBACK_RECORD CurrentRecord
;
507 PLIST_ENTRY ListHead
, NextEntry
, LastEntry
;
510 /* First make sure that the list is Initialized... it might not be */
511 ListHead
= &KeBugcheckCallbackListHead
;
512 if ((ListHead
->Flink
) && (ListHead
->Blink
))
515 LastEntry
= ListHead
;
516 NextEntry
= ListHead
->Flink
;
517 while (NextEntry
!= ListHead
)
520 CurrentRecord
= CONTAINING_RECORD(NextEntry
,
521 KBUGCHECK_CALLBACK_RECORD
,
525 if (CurrentRecord
->Entry
.Blink
!= LastEntry
) return;
526 Checksum
= (ULONG_PTR
)CurrentRecord
->CallbackRoutine
;
527 Checksum
+= (ULONG_PTR
)CurrentRecord
->Buffer
;
528 Checksum
+= (ULONG_PTR
)CurrentRecord
->Length
;
529 Checksum
+= (ULONG_PTR
)CurrentRecord
->Component
;
531 /* Make sure it's inserted and valitdated */
532 if ((CurrentRecord
->State
== BufferInserted
) &&
533 (CurrentRecord
->Checksum
== Checksum
))
535 /* Call the routine */
536 CurrentRecord
->State
= BufferStarted
;
537 (CurrentRecord
->CallbackRoutine
)(CurrentRecord
->Buffer
,
538 CurrentRecord
->Length
);
539 CurrentRecord
->State
= BufferFinished
;
542 /* Go to the next entry */
543 LastEntry
= NextEntry
;
544 NextEntry
= NextEntry
->Flink
;
551 KiBugCheckDebugBreak(IN ULONG StatusCode
)
554 * Wrap this in SEH so we don't crash if
555 * there is no debugger or if it disconnected
561 DbgBreakPointWithStatus(StatusCode
);
563 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
565 /* No debugger, halt the CPU */
570 /* Break again if this wasn't first try */
571 if (StatusCode
!= DBG_STATUS_BUGCHECK_FIRST
) goto DoBreak
;
576 KeBugCheckUnicodeToAnsi(IN PUNICODE_STRING Unicode
,
584 /* Set length and normalize it */
585 i
= Unicode
->Length
/ sizeof(WCHAR
);
586 i
= min(i
, Length
- 1);
588 /* Set source and destination, and copy */
589 pw
= Unicode
->Buffer
;
591 while (i
--) *p
++ = (CHAR
)*pw
++;
593 /* Null terminate and return */
600 KiDumpParameterImages(IN PCHAR Message
,
601 IN PULONG_PTR Parameters
,
602 IN ULONG ParameterCount
,
603 IN PKE_BUGCHECK_UNICODE_TO_ANSI ConversionRoutine
)
607 PLDR_DATA_TABLE_ENTRY LdrEntry
;
609 PUNICODE_STRING DriverName
;
611 PIMAGE_NT_HEADERS NtHeader
;
613 BOOLEAN FirstRun
= TRUE
;
615 /* Loop parameters */
616 for (i
= 0; i
< ParameterCount
; i
++)
618 /* Get the base for this parameter */
619 ImageBase
= KiPcToFileHeader((PVOID
)Parameters
[i
],
625 /* FIXME: Add code to check for unloaded drivers */
626 DPRINT1("Potentially unloaded driver!\n");
631 /* Get the NT Headers and Timestamp */
632 NtHeader
= RtlImageNtHeader(LdrEntry
->DllBase
);
633 TimeStamp
= NtHeader
->FileHeader
.TimeDateStamp
;
635 /* Convert the driver name */
636 DriverName
= &LdrEntry
->BaseDllName
;
637 ConversionRoutine(&LdrEntry
->BaseDllName
,
642 /* Format driver name */
644 "%s** %12s - Address %p base at %p, DateStamp %08lx\n",
645 FirstRun
? "\r\n*":"*",
647 (PVOID
)Parameters
[i
],
651 /* Check if we only had one parameter */
652 if (ParameterCount
<= 1)
654 /* Then just save the name */
655 KiBugCheckDriver
= DriverName
;
659 /* Otherwise, display the message */
660 InbvDisplayString(Message
);
670 KiDisplayBlueScreen(IN ULONG MessageId
,
671 IN BOOLEAN IsHardError
,
672 IN PCHAR HardErrCaption OPTIONAL
,
673 IN PCHAR HardErrMessage OPTIONAL
,
678 /* Check if bootvid is installed */
679 if (InbvIsBootDriverInstalled())
681 /* Acquire ownership and reset the display */
682 InbvAcquireDisplayOwnership();
685 /* Display blue screen */
686 InbvSolidColorFill(0, 0, 639, 479, 4);
687 InbvSetTextColor(15);
688 InbvInstallDisplayStringFilter(NULL
);
689 InbvEnableDisplayString(TRUE
);
690 InbvSetScrollRegion(0, 0, 639, 479);
693 /* Check if this is a hard error */
696 /* Display caption and message */
697 if (HardErrCaption
) InbvDisplayString(HardErrCaption
);
698 if (HardErrMessage
) InbvDisplayString(HardErrMessage
);
701 /* Begin the display */
702 InbvDisplayString("\r\n");
704 /* Print out initial message */
705 KeGetBugMessageText(BUGCHECK_MESSAGE_INTRO
, NULL
);
706 InbvDisplayString("\r\n\r\n");
708 /* Check if we have a driver */
709 if (KiBugCheckDriver
)
711 /* Print out into to driver name */
712 KeGetBugMessageText(BUGCODE_ID_DRIVER
, NULL
);
714 /* Convert and print out driver name */
715 KeBugCheckUnicodeToAnsi(KiBugCheckDriver
, AnsiName
, sizeof(AnsiName
));
716 InbvDisplayString(" ");
717 InbvDisplayString(AnsiName
);
718 InbvDisplayString("\r\n\r\n");
721 /* Check if this is the generic message */
722 if (MessageId
== BUGCODE_PSS_MESSAGE
)
724 /* It is, so get the bug code string as well */
725 KeGetBugMessageText(KiBugCheckData
[0], NULL
);
726 InbvDisplayString("\r\n\r\n");
729 /* Print second introduction message */
730 KeGetBugMessageText(PSS_MESSAGE_INTRO
, NULL
);
731 InbvDisplayString("\r\n\r\n");
733 /* Get the bug code string */
734 KeGetBugMessageText(MessageId
, NULL
);
735 InbvDisplayString("\r\n\r\n");
737 /* Print message for technical information */
738 KeGetBugMessageText(BUGCHECK_TECH_INFO
, NULL
);
740 /* Show the technical Data */
742 "\r\n\r\n*** STOP: 0x%08lX (0x%p,0x%p,0x%p,0x%p)\r\n\r\n",
744 (PVOID
)KiBugCheckData
[1],
745 (PVOID
)KiBugCheckData
[2],
746 (PVOID
)KiBugCheckData
[3],
747 (PVOID
)KiBugCheckData
[4]);
748 InbvDisplayString(AnsiName
);
750 /* Check if we have a driver*/
751 if (KiBugCheckDriver
)
753 /* Display technical driver data */
754 InbvDisplayString(Message
);
758 /* Dump parameter information */
759 KiDumpParameterImages(Message
,
760 (PVOID
)&KiBugCheckData
[1],
762 KeBugCheckUnicodeToAnsi
);
768 KeBugCheckWithTf(IN ULONG BugCheckCode
,
769 IN ULONG_PTR BugCheckParameter1
,
770 IN ULONG_PTR BugCheckParameter2
,
771 IN ULONG_PTR BugCheckParameter3
,
772 IN ULONG_PTR BugCheckParameter4
,
773 IN PKTRAP_FRAME TrapFrame
)
775 PKPRCB Prcb
= KeGetCurrentPrcb();
779 BOOLEAN IsSystem
, IsHardError
= FALSE
, Reboot
= FALSE
;
780 PCHAR HardErrCaption
= NULL
, HardErrMessage
= NULL
;
781 PVOID Pc
= NULL
, Memory
;
783 PLDR_DATA_TABLE_ENTRY LdrEntry
;
784 PULONG_PTR HardErrorParameters
;
790 /* Set active bugcheck */
791 KeBugCheckActive
= TRUE
;
792 KiBugCheckDriver
= NULL
;
794 /* Check if this is power failure simulation */
795 if (BugCheckCode
== POWER_FAILURE_SIMULATE
)
797 /* Call the Callbacks and reboot */;
798 KiDoBugCheckCallbacks();
799 HalReturnToFirmware(HalRebootRoutine
);
802 /* Save the IRQL and set hardware trigger */
803 Prcb
->DebuggerSavedIRQL
= KeGetCurrentIrql();
804 InterlockedIncrement((PLONG
)&KiHardwareTrigger
);
806 /* Capture the CPU Context */
807 RtlCaptureContext(&Prcb
->ProcessorState
.ContextFrame
);
808 KiSaveProcessorControlState(&Prcb
->ProcessorState
);
809 Context
= Prcb
->ProcessorState
.ContextFrame
;
811 /* FIXME: Call the Watchdog if it's registered */
813 /* Check which bugcode this is */
814 switch (BugCheckCode
)
816 /* These bug checks already have detailed messages, keep them */
817 case UNEXPECTED_KERNEL_MODE_TRAP
:
818 case DRIVER_CORRUPTED_EXPOOL
:
819 case ACPI_BIOS_ERROR
:
820 case ACPI_BIOS_FATAL_ERROR
:
821 case THREAD_STUCK_IN_DEVICE_DRIVER
:
823 case FAT_FILE_SYSTEM
:
824 case NO_MORE_SYSTEM_PTES
:
825 case INACCESSIBLE_BOOT_DEVICE
:
827 /* Keep the same code */
828 MessageId
= BugCheckCode
;
831 /* Check if this is a kernel-mode exception */
832 case KERNEL_MODE_EXCEPTION_NOT_HANDLED
:
833 //case SYSTEM_THREAD_EXCEPTION_NOT_HANDLED:
834 case KMODE_EXCEPTION_NOT_HANDLED
:
836 /* Use the generic text message */
837 MessageId
= KMODE_EXCEPTION_NOT_HANDLED
;
840 /* File-system errors */
841 case NTFS_FILE_SYSTEM
:
843 /* Use the generic message for FAT */
844 MessageId
= FAT_FILE_SYSTEM
;
847 /* Check if this is a coruption of the Mm's Pool */
848 case DRIVER_CORRUPTED_MMPOOL
:
850 /* Use generic corruption message */
851 MessageId
= DRIVER_CORRUPTED_EXPOOL
;
854 /* Check if this is a signature check failure */
855 case STATUS_SYSTEM_IMAGE_BAD_SIGNATURE
:
857 /* Use the generic corruption message */
858 MessageId
= BUGCODE_PSS_MESSAGE_SIGNATURE
;
861 /* All other codes */
864 /* Use the default bugcheck message */
865 MessageId
= BUGCODE_PSS_MESSAGE
;
869 /* Save bugcheck data */
870 KiBugCheckData
[0] = BugCheckCode
;
871 KiBugCheckData
[1] = BugCheckParameter1
;
872 KiBugCheckData
[2] = BugCheckParameter2
;
873 KiBugCheckData
[3] = BugCheckParameter3
;
874 KiBugCheckData
[4] = BugCheckParameter4
;
876 /* Now check what bugcheck this is */
877 switch (BugCheckCode
)
879 /* Invalid access to R/O memory or Unhandled KM Exception */
880 case KERNEL_MODE_EXCEPTION_NOT_HANDLED
:
881 case ATTEMPTED_WRITE_TO_READONLY_MEMORY
:
882 case ATTEMPTED_EXECUTE_OF_NOEXECUTE_MEMORY
:
884 /* Check if we have a trap frame */
887 /* Use parameter 3 as a trap frame, if it exists */
888 if (BugCheckParameter3
) TrapFrame
= (PVOID
)BugCheckParameter3
;
891 /* Check if we got one now and if we need to get the Program Counter */
893 (BugCheckCode
!= KERNEL_MODE_EXCEPTION_NOT_HANDLED
))
895 /* Get the Program Counter */
896 Pc
= (PVOID
)KeGetTrapFramePc(TrapFrame
);
901 case IRQL_NOT_LESS_OR_EQUAL
:
904 * The NT kernel has 3 special sections:
905 * MISYSPTE, POOLMI and POOLCODE. The bug check code can
906 * determine in which of these sections this bugcode happened
907 * and provide a more detailed analysis. For now, we don't.
910 /* Program Counter is in parameter 4 */
911 Pc
= (PVOID
)BugCheckParameter4
;
913 /* Get the driver base */
914 DriverBase
= KiPcToFileHeader(Pc
,
921 * The error happened inside the kernel or HAL.
922 * Get the memory address that was being referenced.
924 Memory
= (PVOID
)BugCheckParameter1
;
926 /* Find to which driver it belongs */
927 DriverBase
= KiPcToFileHeader(Memory
,
933 /* Get the driver name and update the bug code */
934 KiBugCheckDriver
= &LdrEntry
->BaseDllName
;
935 KiBugCheckData
[0] = DRIVER_PORTION_MUST_BE_NONPAGED
;
939 /* Find the driver that unloaded at this address */
940 KiBugCheckDriver
= NULL
; // FIXME: ROS can't locate
942 /* Check if the cause was an unloaded driver */
943 if (KiBugCheckDriver
)
945 /* Update bug check code */
947 SYSTEM_SCAN_AT_RAISED_IRQL_CAUGHT_IMPROPER_DRIVER_UNLOAD
;
953 /* Update the bug check code */
954 KiBugCheckData
[0] = DRIVER_IRQL_NOT_LESS_OR_EQUAL
;
957 /* Clear Pc so we don't look it up later */
962 case FATAL_UNHANDLED_HARD_ERROR
:
964 /* Copy bug check data from hard error */
965 HardErrorParameters
= (PULONG_PTR
)BugCheckParameter2
;
966 KiBugCheckData
[0] = BugCheckParameter1
;
967 KiBugCheckData
[1] = HardErrorParameters
[0];
968 KiBugCheckData
[2] = HardErrorParameters
[1];
969 KiBugCheckData
[3] = HardErrorParameters
[2];
970 KiBugCheckData
[4] = HardErrorParameters
[3];
972 /* Remember that this is hard error and set the caption/message */
974 HardErrCaption
= (PCHAR
)BugCheckParameter3
;
975 HardErrMessage
= (PCHAR
)BugCheckParameter4
;
979 case PAGE_FAULT_IN_NONPAGED_AREA
:
981 /* Assume no driver */
984 /* Check if we have a trap frame */
987 /* We don't, use parameter 3 if possible */
988 if (BugCheckParameter3
) TrapFrame
= (PVOID
)BugCheckParameter3
;
991 /* Check if we have a frame now */
994 /* Get the Program Counter */
995 Pc
= (PVOID
)KeGetTrapFramePc(TrapFrame
);
996 KiBugCheckData
[3] = (ULONG_PTR
)Pc
;
998 /* Find out if was in the kernel or drivers */
999 DriverBase
= KiPcToFileHeader(Pc
,
1006 * Now we should check if this happened in:
1007 * 1) Special Pool 2) Free Special Pool 3) Session Pool
1008 * and update the bugcheck code appropriately.
1011 /* Check if we didn't have a driver base */
1014 /* Find the driver that unloaded at this address */
1015 KiBugCheckDriver
= NULL
; // FIXME: ROS can't locate
1017 /* Check if the cause was an unloaded driver */
1018 if (KiBugCheckDriver
)
1021 DRIVER_UNLOADED_WITHOUT_CANCELLING_PENDING_OPERATIONS
;
1026 /* Check if the driver forgot to unlock pages */
1027 case DRIVER_LEFT_LOCKED_PAGES_IN_PROCESS
:
1029 /* Program Counter is in parameter 1 */
1030 Pc
= (PVOID
)BugCheckParameter1
;
1033 /* Check if the driver consumed too many PTEs */
1034 case DRIVER_USED_EXCESSIVE_PTES
:
1036 /* Loader entry is in parameter 1 */
1037 LdrEntry
= (PVOID
)BugCheckParameter1
;
1038 KiBugCheckDriver
= &LdrEntry
->BaseDllName
;
1041 /* Check if the driver has a stuck thread */
1042 case THREAD_STUCK_IN_DEVICE_DRIVER
:
1044 /* The name is in Parameter 3 */
1045 KiBugCheckDriver
= (PVOID
)BugCheckParameter3
;
1053 /* Do we have a driver name? */
1054 if (KiBugCheckDriver
)
1056 /* Convert it to ANSI */
1057 KeBugCheckUnicodeToAnsi(KiBugCheckDriver
, AnsiName
, sizeof(AnsiName
));
1061 /* Do we have a Program Counter? */
1064 /* Dump image name */
1065 KiDumpParameterImages(AnsiName
,
1068 KeBugCheckUnicodeToAnsi
);
1072 /* Check if we need to save the context for KD */
1074 if (!KdPitchDebugger
) KdDebuggerDataBlock
.SavedContext
= (ULONG_PTR
)&Context
;
1077 /* Check if a debugger is connected */
1078 if ((BugCheckCode
!= MANUALLY_INITIATED_CRASH
) && (KdDebuggerEnabled
))
1080 /* Crash on the debugger console */
1081 DbgPrint("\n*** Fatal System Error: 0x%08lx\n"
1082 " (0x%p,0x%p,0x%p,0x%p)\n\n",
1089 /* Check if the debugger isn't currently connected */
1090 if (!KdDebuggerNotPresent
)
1092 /* Check if we have a driver to blame */
1093 if (KiBugCheckDriver
)
1096 DbgPrint("Driver at fault: %s.\n", AnsiName
);
1099 /* Check if this was a hard error */
1102 /* Print caption and message */
1103 if (HardErrCaption
) DbgPrint(HardErrCaption
);
1104 if (HardErrMessage
) DbgPrint(HardErrMessage
);
1107 /* Break in the debugger */
1108 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_FIRST
);
1114 * Ok, so debugging is enabled, but KDBG isn't there.
1115 * We'll manually dump the stack for the user.
1117 KeRosDumpStackFrames(NULL
, 0);
1119 /* ROS HACK 2: Generate something useful for Bugzilla */
1120 KeRosDumpTriageForBugZillaReport();
1124 /* Raise IRQL to HIGH_LEVEL */
1126 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1128 /* Avoid recursion */
1129 if (!InterlockedDecrement((PLONG
)&KeBugCheckCount
))
1132 /* Set CPU that is bug checking now */
1133 KeBugCheckOwner
= Prcb
->Number
;
1135 /* Freeze the other CPUs */
1136 for (i
= 0; i
< KeNumberProcessors
; i
++)
1138 if (i
!= (LONG
)KeGetCurrentProcessorNumber())
1140 /* Send the IPI and give them one second to catch up */
1141 KiIpiSend(1 << i
, IPI_FREEZE
);
1142 KeStallExecutionProcessor(1000000);
1147 /* Display the BSOD */
1148 KiDisplayBlueScreen(MessageId
,
1154 /* Check if the debugger is disabled but we can enable it */
1155 if (!(KdDebuggerEnabled
) && !(KdPitchDebugger
))
1159 KdEnableDebuggerWithLock(FALSE
);
1164 /* Otherwise, print the last line */
1165 InbvDisplayString("\r\n");
1168 /* Save the context */
1169 Prcb
->ProcessorState
.ContextFrame
= Context
;
1171 /* FIXME: Support Triage Dump */
1173 /* FIXME: Write the crash dump */
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)
1186 /* Halt execution */
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
, (PVOID
)MAXULONG_PTR
, 0);
1201 HalReturnToFirmware(HalRebootRoutine
);
1204 /* Attempt to break in the debugger (otherwise halt CPU) */
1205 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_SECOND
);
1207 /* Shouldn't get here */
1211 /* PUBLIC FUNCTIONS **********************************************************/
1218 KeInitializeCrashDumpHeader(IN ULONG Type
,
1221 IN ULONG BufferSize
,
1222 OUT ULONG BufferNeeded OPTIONAL
)
1225 return STATUS_UNSUCCESSFUL
;
1233 KeDeregisterBugCheckCallback(IN PKBUGCHECK_CALLBACK_RECORD CallbackRecord
)
1236 BOOLEAN Status
= FALSE
;
1238 /* Raise IRQL to High */
1239 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1241 /* Check the Current State */
1242 if (CallbackRecord
->State
== BufferInserted
)
1244 /* Reset state and remove from list */
1245 CallbackRecord
->State
= BufferEmpty
;
1246 RemoveEntryList(&CallbackRecord
->Entry
);
1250 /* Lower IRQL and return */
1251 KeLowerIrql(OldIrql
);
1260 KeDeregisterBugCheckReasonCallback(
1261 IN PKBUGCHECK_REASON_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 KeDeregisterNmiCallback(PVOID Handle
)
1291 return STATUS_UNSUCCESSFUL
;
1299 KeRegisterBugCheckCallback(IN PKBUGCHECK_CALLBACK_RECORD CallbackRecord
,
1300 IN PKBUGCHECK_CALLBACK_ROUTINE CallbackRoutine
,
1303 IN PUCHAR Component
)
1306 BOOLEAN Status
= FALSE
;
1308 /* Raise IRQL to High */
1309 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1311 /* Check the Current State first so we don't double-register */
1312 if (CallbackRecord
->State
== BufferEmpty
)
1314 /* Set the Callback Settings and insert into the list */
1315 CallbackRecord
->Length
= Length
;
1316 CallbackRecord
->Buffer
= Buffer
;
1317 CallbackRecord
->Component
= Component
;
1318 CallbackRecord
->CallbackRoutine
= CallbackRoutine
;
1319 CallbackRecord
->State
= BufferInserted
;
1320 InsertTailList(&KeBugcheckCallbackListHead
, &CallbackRecord
->Entry
);
1324 /* Lower IRQL and return */
1325 KeLowerIrql(OldIrql
);
1334 KeRegisterBugCheckReasonCallback(
1335 IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord
,
1336 IN PKBUGCHECK_REASON_CALLBACK_ROUTINE CallbackRoutine
,
1337 IN KBUGCHECK_CALLBACK_REASON Reason
,
1338 IN PUCHAR Component
)
1341 BOOLEAN Status
= FALSE
;
1343 /* Raise IRQL to High */
1344 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1346 /* Check the Current State first so we don't double-register */
1347 if (CallbackRecord
->State
== BufferEmpty
)
1349 /* Set the Callback Settings and insert into the list */
1350 CallbackRecord
->Component
= Component
;
1351 CallbackRecord
->CallbackRoutine
= CallbackRoutine
;
1352 CallbackRecord
->State
= BufferInserted
;
1353 CallbackRecord
->Reason
= Reason
;
1354 InsertTailList(&KeBugcheckReasonCallbackListHead
,
1355 &CallbackRecord
->Entry
);
1359 /* Lower IRQL and return */
1360 KeLowerIrql(OldIrql
);
1369 KeRegisterNmiCallback(IN PNMI_CALLBACK CallbackRoutine
,
1381 KeBugCheckEx(IN ULONG BugCheckCode
,
1382 IN ULONG_PTR BugCheckParameter1
,
1383 IN ULONG_PTR BugCheckParameter2
,
1384 IN ULONG_PTR BugCheckParameter3
,
1385 IN ULONG_PTR BugCheckParameter4
)
1387 /* Call the internal API */
1388 KeBugCheckWithTf(BugCheckCode
,
1401 KeBugCheck(ULONG BugCheckCode
)
1403 /* Call the internal API */
1404 KeBugCheckWithTf(BugCheckCode
, 0, 0, 0, 0, NULL
);
1412 KeEnterKernelDebugger(VOID
)
1414 /* Disable interrupts */
1415 KiHardwareTrigger
= 1;
1418 /* Check the bugcheck count */
1419 if (!InterlockedDecrement((PLONG
)&KeBugCheckCount
))
1421 /* There was only one, is the debugger disabled? */
1422 if (!(KdDebuggerEnabled
) && !(KdPitchDebugger
))
1424 /* Enable the debugger */
1425 KdInitSystem(0, NULL
);
1429 /* Break in the debugger */
1430 KiBugCheckDebugBreak(DBG_STATUS_FATAL
);