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 PMESSAGE_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 KiRosPcToUserFileHeader(IN PVOID Pc
,
108 OUT PLDR_DATA_TABLE_ENTRY
*LdrEntry
)
110 PVOID ImageBase
, PcBase
= NULL
;
111 PLDR_DATA_TABLE_ENTRY Entry
;
112 PLIST_ENTRY ListHead
, NextEntry
;
115 * We know this is valid because we should only be called after a
116 * succesfull address from RtlWalkFrameChain for UserMode, which
117 * validates everything for us.
119 ListHead
= &KeGetCurrentThread()->
120 Teb
->ProcessEnvironmentBlock
->Ldr
->InLoadOrderModuleList
;
122 /* Set list pointers and make sure it's valid */
123 NextEntry
= ListHead
->Flink
;
127 while (NextEntry
!= ListHead
)
129 /* Get the loader entry */
130 Entry
= CONTAINING_RECORD(NextEntry
,
131 LDR_DATA_TABLE_ENTRY
,
134 /* Move to the next entry */
135 NextEntry
= NextEntry
->Flink
;
136 ImageBase
= Entry
->DllBase
;
138 /* Check if this is the right one */
139 if (((ULONG_PTR
)Pc
>= (ULONG_PTR
)Entry
->DllBase
) &&
140 ((ULONG_PTR
)Pc
< ((ULONG_PTR
)Entry
->DllBase
+ Entry
->SizeOfImage
)))
142 /* Return this entry */
150 /* Return the base address */
156 KeRosCaptureUserStackBackTrace(IN ULONG FramesToSkip
,
157 IN ULONG FramesToCapture
,
158 OUT PVOID
*BackTrace
,
159 OUT PULONG BackTraceHash OPTIONAL
)
161 PVOID Frames
[2 * 64];
165 /* Skip a frame for the caller */
168 /* Don't go past the limit */
169 if ((FramesToCapture
+ FramesToSkip
) >= 128) return 0;
171 /* Do the back trace */
172 FrameCount
= RtlWalkFrameChain(Frames
, FramesToCapture
+ FramesToSkip
, 1);
174 /* Make sure we're not skipping all of them */
175 if (FrameCount
<= FramesToSkip
) return 0;
177 /* Loop all the frames */
178 for (i
= 0; i
< FramesToCapture
; i
++)
180 /* Don't go past the limit */
181 if ((FramesToSkip
+ i
) >= FrameCount
) break;
183 /* Save this entry and hash it */
184 BackTrace
[i
] = Frames
[FramesToSkip
+ i
];
185 Hash
+= PtrToUlong(BackTrace
[i
]);
189 if (BackTraceHash
) *BackTraceHash
= Hash
;
191 /* Clear the other entries and return count */
192 RtlFillMemoryUlong(Frames
, 128, 0);
199 KeRosDumpStackFrameArray(IN PULONG_PTR Frames
,
207 /* GCC complaints that it may be used uninitialized */
208 PLDR_DATA_TABLE_ENTRY LdrEntry
= NULL
;
211 for (i
= 0; i
< FrameCount
; i
++)
220 /* Get the base for this file */
221 if (Addr
> (ULONG_PTR
)MmHighestUserAddress
)
223 /* We are in kernel */
224 p
= KiPcToFileHeader((PVOID
)Addr
, &LdrEntry
, FALSE
, &InSystem
);
228 /* We are in user land */
229 p
= KiRosPcToUserFileHeader((PVOID
)Addr
, &LdrEntry
);
234 if (!KdbSymPrintAddress((PVOID
)Addr
, NULL
))
239 /* Convert module name to ANSI and print it */
240 KeBugCheckUnicodeToAnsi(&LdrEntry
->BaseDllName
,
243 Addr
-= (ULONG_PTR
)LdrEntry
->DllBase
;
244 DbgPrint("<%s: %p>", AnsiName
, (PVOID
)Addr
);
249 /* Print only the address */
250 DbgPrint("<%p>", (PVOID
)Addr
);
253 /* Go to the next frame */
260 KeRosDumpStackFrames(IN PULONG_PTR Frame OPTIONAL
,
261 IN ULONG FrameCount OPTIONAL
)
263 ULONG_PTR Frames
[32];
264 ULONG RealFrameCount
;
266 /* If the caller didn't ask, assume 32 frames */
267 if (!FrameCount
|| FrameCount
> 32) FrameCount
= 32;
272 KeRosDumpStackFrameArray(Frame
, FrameCount
);
276 /* Get the current frames (skip the two. One for the dumper, one for the caller) */
277 RealFrameCount
= RtlCaptureStackBackTrace(2, FrameCount
, (PVOID
*)Frames
, NULL
);
280 KeRosDumpStackFrameArray(Frames
, RealFrameCount
);
282 /* Count left for user mode? */
283 if (FrameCount
- RealFrameCount
> 0)
285 /* Get the current frames */
286 RealFrameCount
= KeRosCaptureUserStackBackTrace(-1, FrameCount
- RealFrameCount
, (PVOID
*)Frames
, NULL
);
289 KeRosDumpStackFrameArray(Frames
, RealFrameCount
);
296 KeRosDumpTriageForBugZillaReport(VOID
)
299 extern BOOLEAN KiFastSystemCallDisable
, KiSMTProcessorsPresent
;
300 extern ULONG KeI386MachineType
, MxcsrFeatureMask
;
301 extern BOOLEAN Ke386Pae
, Ke386NoExecute
;
303 DbgPrint("ReactOS has crashed! Please go to http://www.reactos.org/bugzilla/enter_bug.cgi to file a bug!\n");
304 DbgPrint("\nHardware Information\n");
305 DbgPrint("Processor Architecture: %d\n"
307 "System Call Disabled: %d\n"
310 "MXCsr Feature Mask: %d\n"
317 "Active Processors: %d\n"
318 "Pentium LOCK Bug: %d\n"
319 "Hyperthreading: %d\n"
320 "CPU Manufacturer: %s\n"
328 "BIOS Version: %wZ\n"
329 "Video BIOS Date: %wZ\n"
330 "Video BIOS Version: %wZ\n"
332 KeProcessorArchitecture
,
334 KiFastSystemCallDisable
,
345 KiI386PentiumLockErrataPresent
,
346 KiSMTProcessorsPresent
,
347 KeGetCurrentPrcb()->VendorString
,
349 KeGetCurrentPrcb()->CpuID
,
350 KeGetCurrentPrcb()->CpuType
,
351 KeGetCurrentPrcb()->CpuStep
,
352 KeGetCurrentPrcb()->MHz
,
353 ((PKIPCR
)KeGetPcr())->SecondLevelCacheSize
,
357 &KeRosVideoBiosVersion
,
358 MmNumberOfPhysicalPages
* PAGE_SIZE
);
365 KiInitializeBugCheck(VOID
)
367 PMESSAGE_RESOURCE_DATA BugCheckData
;
368 LDR_RESOURCE_INFO ResourceInfo
;
369 PIMAGE_RESOURCE_DATA_ENTRY ResourceDataEntry
;
371 PLDR_DATA_TABLE_ENTRY LdrEntry
;
373 /* Get the kernel entry */
374 LdrEntry
= CONTAINING_RECORD(KeLoaderBlock
->LoadOrderListHead
.Flink
,
375 LDR_DATA_TABLE_ENTRY
,
378 /* Cache the Bugcheck Message Strings. Prepare the Lookup Data */
379 ResourceInfo
.Type
= 11;
380 ResourceInfo
.Name
= 1;
381 ResourceInfo
.Language
= 9;
384 Status
= LdrFindResource_U(LdrEntry
->DllBase
,
389 /* Make sure it worked */
390 if (NT_SUCCESS(Status
))
392 /* Now actually get a pointer to it */
393 Status
= LdrAccessResource(LdrEntry
->DllBase
,
395 (PVOID
*)&BugCheckData
,
397 if (NT_SUCCESS(Status
)) KiBugCodeMessages
= BugCheckData
;
403 KeGetBugMessageText(IN ULONG BugCheckCode
,
404 OUT PANSI_STRING OutputString OPTIONAL
)
408 ULONG_PTR MessageEntry
;
410 BOOLEAN Result
= FALSE
;
413 /* Make sure we're not bugchecking too early */
414 if (!KiBugCodeMessages
) return Result
;
416 /* Find the message. This code is based on RtlFindMesssage */
417 for (i
= 0; i
< KiBugCodeMessages
->NumberOfBlocks
; i
++)
419 /* Check if the ID Matches */
420 if ((BugCheckCode
>= KiBugCodeMessages
->Blocks
[i
].LowId
) &&
421 (BugCheckCode
<= KiBugCodeMessages
->Blocks
[i
].HighId
))
423 /* Get Offset to Entry */
424 MessageEntry
= KiBugCodeMessages
->Blocks
[i
].OffsetToEntries
+
425 (ULONG_PTR
)KiBugCodeMessages
;
426 IdOffset
= BugCheckCode
- KiBugCodeMessages
->Blocks
[i
].LowId
;
428 /* Get offset to ID */
429 for (j
= 0; j
< IdOffset
; j
++)
431 /* Advance in the Entries */
432 MessageEntry
+= ((PMESSAGE_RESOURCE_ENTRY
)MessageEntry
)->
436 /* Get the final Code */
437 BugCode
= (PCHAR
)((PMESSAGE_RESOURCE_ENTRY
)MessageEntry
)->Text
;
438 Length
= (USHORT
)strlen(BugCode
);
440 /* Handle trailing newlines */
441 while ((Length
> 0) && ((BugCode
[Length
] == '\n') ||
442 (BugCode
[Length
] == '\r') ||
443 (BugCode
[Length
] == ANSI_NULL
)))
445 /* Check if we have a string to return */
446 if (!OutputString
) BugCode
[Length
] = ANSI_NULL
;
450 /* Check if caller wants an output string */
453 /* Return it in the OutputString */
454 OutputString
->Buffer
= BugCode
;
455 OutputString
->Length
= Length
+ 1;
456 OutputString
->MaximumLength
= Length
+ 1;
460 /* Direct Output to Screen */
461 InbvDisplayString(BugCode
);
462 InbvDisplayString("\r");
471 /* Return the result */
477 KiDoBugCheckCallbacks(VOID
)
479 PKBUGCHECK_CALLBACK_RECORD CurrentRecord
;
480 PLIST_ENTRY ListHead
, NextEntry
, LastEntry
;
483 /* First make sure that the list is Initialized... it might not be */
484 ListHead
= &KeBugcheckCallbackListHead
;
485 if ((ListHead
->Flink
) && (ListHead
->Blink
))
488 LastEntry
= ListHead
;
489 NextEntry
= ListHead
->Flink
;
490 while (NextEntry
!= ListHead
)
493 CurrentRecord
= CONTAINING_RECORD(NextEntry
,
494 KBUGCHECK_CALLBACK_RECORD
,
498 if (CurrentRecord
->Entry
.Blink
!= LastEntry
) return;
499 Checksum
= (ULONG_PTR
)CurrentRecord
->CallbackRoutine
;
500 Checksum
+= (ULONG_PTR
)CurrentRecord
->Buffer
;
501 Checksum
+= (ULONG_PTR
)CurrentRecord
->Length
;
502 Checksum
+= (ULONG_PTR
)CurrentRecord
->Component
;
504 /* Make sure it's inserted and valitdated */
505 if ((CurrentRecord
->State
== BufferInserted
) &&
506 (CurrentRecord
->Checksum
== Checksum
))
508 /* Call the routine */
509 CurrentRecord
->State
= BufferStarted
;
510 (CurrentRecord
->CallbackRoutine
)(CurrentRecord
->Buffer
,
511 CurrentRecord
->Length
);
512 CurrentRecord
->State
= BufferFinished
;
515 /* Go to the next entry */
516 LastEntry
= NextEntry
;
517 NextEntry
= NextEntry
->Flink
;
524 KiBugCheckDebugBreak(IN ULONG StatusCode
)
527 * Wrap this in SEH so we don't crash if
528 * there is no debugger or if it disconnected
534 DbgBreakPointWithStatus(StatusCode
);
536 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
538 /* No debugger, halt the CPU */
543 /* Break again if this wasn't first try */
544 if (StatusCode
!= DBG_STATUS_BUGCHECK_FIRST
) goto DoBreak
;
549 KeBugCheckUnicodeToAnsi(IN PUNICODE_STRING Unicode
,
557 /* Set length and normalize it */
558 i
= Unicode
->Length
/ sizeof(WCHAR
);
559 i
= min(i
, Length
- 1);
561 /* Set source and destination, and copy */
562 pw
= Unicode
->Buffer
;
564 while (i
--) *p
++ = (CHAR
)*pw
++;
566 /* Null terminate and return */
573 KiDumpParameterImages(IN PCHAR Message
,
574 IN PULONG_PTR Parameters
,
575 IN ULONG ParameterCount
,
576 IN PKE_BUGCHECK_UNICODE_TO_ANSI ConversionRoutine
)
580 PLDR_DATA_TABLE_ENTRY LdrEntry
;
582 PUNICODE_STRING DriverName
;
584 PIMAGE_NT_HEADERS NtHeader
;
586 BOOLEAN FirstRun
= TRUE
;
588 /* Loop parameters */
589 for (i
= 0; i
< ParameterCount
; i
++)
591 /* Get the base for this parameter */
592 ImageBase
= KiPcToFileHeader((PVOID
)Parameters
[i
],
598 /* FIXME: Add code to check for unloaded drivers */
599 DPRINT1("Potentially unloaded driver!\n");
604 /* Get the NT Headers and Timestamp */
605 NtHeader
= RtlImageNtHeader(LdrEntry
->DllBase
);
606 TimeStamp
= NtHeader
->FileHeader
.TimeDateStamp
;
608 /* Convert the driver name */
609 DriverName
= &LdrEntry
->BaseDllName
;
610 ConversionRoutine(&LdrEntry
->BaseDllName
,
615 /* Format driver name */
617 "%s** %12s - Address %p base at %p, DateStamp %08lx\n",
618 FirstRun
? "\r\n*":"*",
620 (PVOID
)Parameters
[i
],
624 /* Check if we only had one parameter */
625 if (ParameterCount
<= 1)
627 /* Then just save the name */
628 KiBugCheckDriver
= DriverName
;
632 /* Otherwise, display the message */
633 InbvDisplayString(Message
);
643 KiDisplayBlueScreen(IN ULONG MessageId
,
644 IN BOOLEAN IsHardError
,
645 IN PCHAR HardErrCaption OPTIONAL
,
646 IN PCHAR HardErrMessage OPTIONAL
,
651 /* Check if bootvid is installed */
652 if (InbvIsBootDriverInstalled())
654 /* Acquire ownership and reset the display */
655 InbvAcquireDisplayOwnership();
658 /* Display blue screen */
659 InbvSolidColorFill(0, 0, 639, 479, 4);
660 InbvSetTextColor(15);
661 InbvInstallDisplayStringFilter(NULL
);
662 InbvEnableDisplayString(TRUE
);
663 InbvSetScrollRegion(0, 0, 639, 479);
666 /* Check if this is a hard error */
669 /* Display caption and message */
670 if (HardErrCaption
) InbvDisplayString(HardErrCaption
);
671 if (HardErrMessage
) InbvDisplayString(HardErrMessage
);
674 /* Begin the display */
675 InbvDisplayString("\r\n");
677 /* Print out initial message */
678 KeGetBugMessageText(BUGCHECK_MESSAGE_INTRO
, NULL
);
679 InbvDisplayString("\r\n\r\n");
681 /* Check if we have a driver */
682 if (KiBugCheckDriver
)
684 /* Print out into to driver name */
685 KeGetBugMessageText(BUGCODE_ID_DRIVER
, NULL
);
687 /* Convert and print out driver name */
688 KeBugCheckUnicodeToAnsi(KiBugCheckDriver
, AnsiName
, sizeof(AnsiName
));
689 InbvDisplayString(" ");
690 InbvDisplayString(AnsiName
);
691 InbvDisplayString("\r\n\r\n");
694 /* Check if this is the generic message */
695 if (MessageId
== BUGCODE_PSS_MESSAGE
)
697 /* It is, so get the bug code string as well */
698 KeGetBugMessageText((ULONG
)KiBugCheckData
[0], NULL
);
699 InbvDisplayString("\r\n\r\n");
702 /* Print second introduction message */
703 KeGetBugMessageText(PSS_MESSAGE_INTRO
, NULL
);
704 InbvDisplayString("\r\n\r\n");
706 /* Get the bug code string */
707 KeGetBugMessageText(MessageId
, NULL
);
708 InbvDisplayString("\r\n\r\n");
710 /* Print message for technical information */
711 KeGetBugMessageText(BUGCHECK_TECH_INFO
, NULL
);
713 /* Show the technical Data */
715 "\r\n\r\n*** STOP: 0x%08lX (0x%p,0x%p,0x%p,0x%p)\r\n\r\n",
717 (PVOID
)KiBugCheckData
[1],
718 (PVOID
)KiBugCheckData
[2],
719 (PVOID
)KiBugCheckData
[3],
720 (PVOID
)KiBugCheckData
[4]);
721 InbvDisplayString(AnsiName
);
723 /* Check if we have a driver*/
724 if (KiBugCheckDriver
)
726 /* Display technical driver data */
727 InbvDisplayString(Message
);
731 /* Dump parameter information */
732 KiDumpParameterImages(Message
,
733 (PVOID
)&KiBugCheckData
[1],
735 KeBugCheckUnicodeToAnsi
);
741 KeBugCheckWithTf(IN ULONG BugCheckCode
,
742 IN ULONG_PTR BugCheckParameter1
,
743 IN ULONG_PTR BugCheckParameter2
,
744 IN ULONG_PTR BugCheckParameter3
,
745 IN ULONG_PTR BugCheckParameter4
,
746 IN PKTRAP_FRAME TrapFrame
)
748 PKPRCB Prcb
= KeGetCurrentPrcb();
752 BOOLEAN IsSystem
, IsHardError
= FALSE
, Reboot
= FALSE
;
753 PCHAR HardErrCaption
= NULL
, HardErrMessage
= NULL
;
754 PVOID Pc
= NULL
, Memory
;
756 PLDR_DATA_TABLE_ENTRY LdrEntry
;
757 PULONG_PTR HardErrorParameters
;
763 /* Set active bugcheck */
764 KeBugCheckActive
= TRUE
;
765 KiBugCheckDriver
= NULL
;
767 /* Check if this is power failure simulation */
768 if (BugCheckCode
== POWER_FAILURE_SIMULATE
)
770 /* Call the Callbacks and reboot */
771 KiDoBugCheckCallbacks();
772 HalReturnToFirmware(HalRebootRoutine
);
775 /* Save the IRQL and set hardware trigger */
776 Prcb
->DebuggerSavedIRQL
= KeGetCurrentIrql();
777 InterlockedIncrement((PLONG
)&KiHardwareTrigger
);
779 /* Capture the CPU Context */
780 RtlCaptureContext(&Prcb
->ProcessorState
.ContextFrame
);
781 KiSaveProcessorControlState(&Prcb
->ProcessorState
);
782 Context
= Prcb
->ProcessorState
.ContextFrame
;
784 /* FIXME: Call the Watchdog if it's registered */
786 /* Check which bugcode this is */
787 switch (BugCheckCode
)
789 /* These bug checks already have detailed messages, keep them */
790 case UNEXPECTED_KERNEL_MODE_TRAP
:
791 case DRIVER_CORRUPTED_EXPOOL
:
792 case ACPI_BIOS_ERROR
:
793 case ACPI_BIOS_FATAL_ERROR
:
794 case THREAD_STUCK_IN_DEVICE_DRIVER
:
796 case FAT_FILE_SYSTEM
:
797 case NO_MORE_SYSTEM_PTES
:
798 case INACCESSIBLE_BOOT_DEVICE
:
800 /* Keep the same code */
801 MessageId
= BugCheckCode
;
804 /* Check if this is a kernel-mode exception */
805 case KERNEL_MODE_EXCEPTION_NOT_HANDLED
:
806 case SYSTEM_THREAD_EXCEPTION_NOT_HANDLED
:
807 case KMODE_EXCEPTION_NOT_HANDLED
:
809 /* Use the generic text message */
810 MessageId
= KMODE_EXCEPTION_NOT_HANDLED
;
813 /* File-system errors */
814 case NTFS_FILE_SYSTEM
:
816 /* Use the generic message for FAT */
817 MessageId
= FAT_FILE_SYSTEM
;
820 /* Check if this is a coruption of the Mm's Pool */
821 case DRIVER_CORRUPTED_MMPOOL
:
823 /* Use generic corruption message */
824 MessageId
= DRIVER_CORRUPTED_EXPOOL
;
827 /* Check if this is a signature check failure */
828 case STATUS_SYSTEM_IMAGE_BAD_SIGNATURE
:
830 /* Use the generic corruption message */
831 MessageId
= BUGCODE_PSS_MESSAGE_SIGNATURE
;
834 /* All other codes */
837 /* Use the default bugcheck message */
838 MessageId
= BUGCODE_PSS_MESSAGE
;
842 /* Save bugcheck data */
843 KiBugCheckData
[0] = BugCheckCode
;
844 KiBugCheckData
[1] = BugCheckParameter1
;
845 KiBugCheckData
[2] = BugCheckParameter2
;
846 KiBugCheckData
[3] = BugCheckParameter3
;
847 KiBugCheckData
[4] = BugCheckParameter4
;
849 /* Now check what bugcheck this is */
850 switch (BugCheckCode
)
852 /* Invalid access to R/O memory or Unhandled KM Exception */
853 case KERNEL_MODE_EXCEPTION_NOT_HANDLED
:
854 case ATTEMPTED_WRITE_TO_READONLY_MEMORY
:
855 case ATTEMPTED_EXECUTE_OF_NOEXECUTE_MEMORY
:
857 /* Check if we have a trap frame */
860 /* Use parameter 3 as a trap frame, if it exists */
861 if (BugCheckParameter3
) TrapFrame
= (PVOID
)BugCheckParameter3
;
864 /* Check if we got one now and if we need to get the Program Counter */
866 (BugCheckCode
!= KERNEL_MODE_EXCEPTION_NOT_HANDLED
))
868 /* Get the Program Counter */
869 Pc
= (PVOID
)KeGetTrapFramePc(TrapFrame
);
874 case IRQL_NOT_LESS_OR_EQUAL
:
877 * The NT kernel has 3 special sections:
878 * MISYSPTE, POOLMI and POOLCODE. The bug check code can
879 * determine in which of these sections this bugcode happened
880 * and provide a more detailed analysis. For now, we don't.
883 /* Program Counter is in parameter 4 */
884 Pc
= (PVOID
)BugCheckParameter4
;
886 /* Get the driver base */
887 DriverBase
= KiPcToFileHeader(Pc
,
894 * The error happened inside the kernel or HAL.
895 * Get the memory address that was being referenced.
897 Memory
= (PVOID
)BugCheckParameter1
;
899 /* Find to which driver it belongs */
900 DriverBase
= KiPcToFileHeader(Memory
,
906 /* Get the driver name and update the bug code */
907 KiBugCheckDriver
= &LdrEntry
->BaseDllName
;
908 KiBugCheckData
[0] = DRIVER_PORTION_MUST_BE_NONPAGED
;
912 /* Find the driver that unloaded at this address */
913 KiBugCheckDriver
= NULL
; // FIXME: ROS can't locate
915 /* Check if the cause was an unloaded driver */
916 if (KiBugCheckDriver
)
918 /* Update bug check code */
920 SYSTEM_SCAN_AT_RAISED_IRQL_CAUGHT_IMPROPER_DRIVER_UNLOAD
;
926 /* Update the bug check code */
927 KiBugCheckData
[0] = DRIVER_IRQL_NOT_LESS_OR_EQUAL
;
930 /* Clear Pc so we don't look it up later */
935 case FATAL_UNHANDLED_HARD_ERROR
:
937 /* Copy bug check data from hard error */
938 HardErrorParameters
= (PULONG_PTR
)BugCheckParameter2
;
939 KiBugCheckData
[0] = BugCheckParameter1
;
940 KiBugCheckData
[1] = HardErrorParameters
[0];
941 KiBugCheckData
[2] = HardErrorParameters
[1];
942 KiBugCheckData
[3] = HardErrorParameters
[2];
943 KiBugCheckData
[4] = HardErrorParameters
[3];
945 /* Remember that this is hard error and set the caption/message */
947 HardErrCaption
= (PCHAR
)BugCheckParameter3
;
948 HardErrMessage
= (PCHAR
)BugCheckParameter4
;
952 case PAGE_FAULT_IN_NONPAGED_AREA
:
954 /* Assume no driver */
957 /* Check if we have a trap frame */
960 /* We don't, use parameter 3 if possible */
961 if (BugCheckParameter3
) TrapFrame
= (PVOID
)BugCheckParameter3
;
964 /* Check if we have a frame now */
967 /* Get the Program Counter */
968 Pc
= (PVOID
)KeGetTrapFramePc(TrapFrame
);
969 KiBugCheckData
[3] = (ULONG_PTR
)Pc
;
971 /* Find out if was in the kernel or drivers */
972 DriverBase
= KiPcToFileHeader(Pc
,
979 * Now we should check if this happened in:
980 * 1) Special Pool 2) Free Special Pool 3) Session Pool
981 * and update the bugcheck code appropriately.
984 /* Check if we didn't have a driver base */
987 /* Find the driver that unloaded at this address */
988 KiBugCheckDriver
= NULL
; // FIXME: ROS can't locate
990 /* Check if the cause was an unloaded driver */
991 if (KiBugCheckDriver
)
994 DRIVER_UNLOADED_WITHOUT_CANCELLING_PENDING_OPERATIONS
;
999 /* Check if the driver forgot to unlock pages */
1000 case DRIVER_LEFT_LOCKED_PAGES_IN_PROCESS
:
1002 /* Program Counter is in parameter 1 */
1003 Pc
= (PVOID
)BugCheckParameter1
;
1006 /* Check if the driver consumed too many PTEs */
1007 case DRIVER_USED_EXCESSIVE_PTES
:
1009 /* Loader entry is in parameter 1 */
1010 LdrEntry
= (PVOID
)BugCheckParameter1
;
1011 KiBugCheckDriver
= &LdrEntry
->BaseDllName
;
1014 /* Check if the driver has a stuck thread */
1015 case THREAD_STUCK_IN_DEVICE_DRIVER
:
1017 /* The name is in Parameter 3 */
1018 KiBugCheckDriver
= (PVOID
)BugCheckParameter3
;
1026 /* Do we have a driver name? */
1027 if (KiBugCheckDriver
)
1029 /* Convert it to ANSI */
1030 KeBugCheckUnicodeToAnsi(KiBugCheckDriver
, AnsiName
, sizeof(AnsiName
));
1034 /* Do we have a Program Counter? */
1037 /* Dump image name */
1038 KiDumpParameterImages(AnsiName
,
1041 KeBugCheckUnicodeToAnsi
);
1045 /* Check if we need to save the context for KD */
1047 if (!KdPitchDebugger
) KdDebuggerDataBlock
.SavedContext
= (ULONG_PTR
)&Context
;
1050 /* Check if a debugger is connected */
1051 if ((BugCheckCode
!= MANUALLY_INITIATED_CRASH
) && (KdDebuggerEnabled
))
1053 /* Crash on the debugger console */
1054 DbgPrint("\n*** Fatal System Error: 0x%08lx\n"
1055 " (0x%p,0x%p,0x%p,0x%p)\n\n",
1062 /* Check if the debugger isn't currently connected */
1063 if (!KdDebuggerNotPresent
)
1065 /* Check if we have a driver to blame */
1066 if (KiBugCheckDriver
)
1069 DbgPrint("Driver at fault: %s.\n", AnsiName
);
1072 /* Check if this was a hard error */
1075 /* Print caption and message */
1076 if (HardErrCaption
) DbgPrint(HardErrCaption
);
1077 if (HardErrMessage
) DbgPrint(HardErrMessage
);
1080 /* Break in the debugger */
1081 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_FIRST
);
1087 * Ok, so debugging is enabled, but KDBG isn't there.
1088 * We'll manually dump the stack for the user.
1090 KeRosDumpStackFrames(NULL
, 0);
1092 /* ROS HACK 2: Generate something useful for Bugzilla */
1093 KeRosDumpTriageForBugZillaReport();
1097 /* Raise IRQL to HIGH_LEVEL */
1099 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1101 /* Avoid recursion */
1102 if (!InterlockedDecrement((PLONG
)&KeBugCheckCount
))
1105 /* Set CPU that is bug checking now */
1106 KeBugCheckOwner
= Prcb
->Number
;
1108 /* Freeze the other CPUs */
1109 for (i
= 0; i
< KeNumberProcessors
; i
++)
1111 if (i
!= (LONG
)KeGetCurrentProcessorNumber())
1113 /* Send the IPI and give them one second to catch up */
1114 KiIpiSend(1 << i
, IPI_FREEZE
);
1115 KeStallExecutionProcessor(1000000);
1120 /* Display the BSOD */
1121 KiDisplayBlueScreen(MessageId
,
1127 /* Check if the debugger is disabled but we can enable it */
1128 if (!(KdDebuggerEnabled
) && !(KdPitchDebugger
))
1132 KdEnableDebuggerWithLock(FALSE
);
1137 /* Otherwise, print the last line */
1138 InbvDisplayString("\r\n");
1141 /* Save the context */
1142 Prcb
->ProcessorState
.ContextFrame
= Context
;
1144 /* FIXME: Support Triage Dump */
1146 /* FIXME: Write the crash dump */
1150 /* Increase recursion count */
1151 KeBugCheckOwnerRecursionCount
++;
1152 if (KeBugCheckOwnerRecursionCount
== 2)
1154 /* Break in the debugger */
1155 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_SECOND
);
1157 else if (KeBugCheckOwnerRecursionCount
> 2)
1159 /* Halt execution */
1164 /* Call the Callbacks */
1165 KiDoBugCheckCallbacks();
1167 /* FIXME: Call Watchdog if enabled */
1169 /* Check if we have to reboot */
1172 /* Unload symbols */
1173 DbgUnLoadImageSymbols(NULL
, (PVOID
)MAXULONG_PTR
, 0);
1174 HalReturnToFirmware(HalRebootRoutine
);
1177 /* Attempt to break in the debugger (otherwise halt CPU) */
1178 KiBugCheckDebugBreak(DBG_STATUS_BUGCHECK_SECOND
);
1180 /* Shouldn't get here */
1188 BOOLEAN Handled
= FALSE
;
1189 PKNMI_HANDLER_CALLBACK NmiData
;
1191 /* Parse the list of callbacks */
1192 NmiData
= KiNmiCallbackListHead
;
1195 /* Save if this callback has handled it -- all it takes is one */
1196 Handled
|= NmiData
->Callback(NmiData
->Context
, Handled
);
1197 NmiData
= NmiData
->Next
;
1200 /* Has anyone handled this? */
1204 /* PUBLIC FUNCTIONS **********************************************************/
1211 KeInitializeCrashDumpHeader(IN ULONG Type
,
1214 IN ULONG BufferSize
,
1215 OUT ULONG BufferNeeded OPTIONAL
)
1218 return STATUS_UNSUCCESSFUL
;
1226 KeDeregisterBugCheckCallback(IN PKBUGCHECK_CALLBACK_RECORD CallbackRecord
)
1229 BOOLEAN Status
= FALSE
;
1231 /* Raise IRQL to High */
1232 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1234 /* Check the Current State */
1235 if (CallbackRecord
->State
== BufferInserted
)
1237 /* Reset state and remove from list */
1238 CallbackRecord
->State
= BufferEmpty
;
1239 RemoveEntryList(&CallbackRecord
->Entry
);
1243 /* Lower IRQL and return */
1244 KeLowerIrql(OldIrql
);
1253 KeDeregisterBugCheckReasonCallback(
1254 IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord
)
1257 BOOLEAN Status
= FALSE
;
1259 /* Raise IRQL to High */
1260 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1262 /* Check the Current State */
1263 if (CallbackRecord
->State
== BufferInserted
)
1265 /* Reset state and remove from list */
1266 CallbackRecord
->State
= BufferEmpty
;
1267 RemoveEntryList(&CallbackRecord
->Entry
);
1271 /* Lower IRQL and return */
1272 KeLowerIrql(OldIrql
);
1281 KeRegisterBugCheckCallback(IN PKBUGCHECK_CALLBACK_RECORD CallbackRecord
,
1282 IN PKBUGCHECK_CALLBACK_ROUTINE CallbackRoutine
,
1285 IN PUCHAR Component
)
1288 BOOLEAN Status
= FALSE
;
1290 /* Raise IRQL to High */
1291 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1293 /* Check the Current State first so we don't double-register */
1294 if (CallbackRecord
->State
== BufferEmpty
)
1296 /* Set the Callback Settings and insert into the list */
1297 CallbackRecord
->Length
= Length
;
1298 CallbackRecord
->Buffer
= Buffer
;
1299 CallbackRecord
->Component
= Component
;
1300 CallbackRecord
->CallbackRoutine
= CallbackRoutine
;
1301 CallbackRecord
->State
= BufferInserted
;
1302 InsertTailList(&KeBugcheckCallbackListHead
, &CallbackRecord
->Entry
);
1306 /* Lower IRQL and return */
1307 KeLowerIrql(OldIrql
);
1316 KeRegisterBugCheckReasonCallback(
1317 IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord
,
1318 IN PKBUGCHECK_REASON_CALLBACK_ROUTINE CallbackRoutine
,
1319 IN KBUGCHECK_CALLBACK_REASON Reason
,
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
->Component
= Component
;
1333 CallbackRecord
->CallbackRoutine
= CallbackRoutine
;
1334 CallbackRecord
->State
= BufferInserted
;
1335 CallbackRecord
->Reason
= Reason
;
1336 InsertTailList(&KeBugcheckReasonCallbackListHead
,
1337 &CallbackRecord
->Entry
);
1341 /* Lower IRQL and return */
1342 KeLowerIrql(OldIrql
);
1351 KeRegisterNmiCallback(IN PNMI_CALLBACK CallbackRoutine
,
1355 PKNMI_HANDLER_CALLBACK NmiData
, Next
;
1356 ASSERT_IRQL_LESS_OR_EQUAL(DISPATCH_LEVEL
);
1358 /* Allocate NMI callback data */
1359 NmiData
= ExAllocatePoolWithTag(NonPagedPool
,
1360 sizeof(KNMI_HANDLER_CALLBACK
),
1362 if (!NmiData
) return NULL
;
1364 /* Fill in the information */
1365 NmiData
->Callback
= CallbackRoutine
;
1366 NmiData
->Context
= Context
;
1367 NmiData
->Handle
= NmiData
;
1369 /* Insert it into NMI callback list */
1370 KiAcquireNmiListLock(&OldIrql
);
1371 NmiData
->Next
= KiNmiCallbackListHead
;
1372 Next
= InterlockedCompareExchangePointer((PVOID
*)&KiNmiCallbackListHead
,
1375 ASSERT(Next
== NmiData
->Next
);
1376 KiReleaseNmiListLock(OldIrql
);
1378 /* Return the opaque "handle" */
1379 return NmiData
->Handle
;
1387 KeDeregisterNmiCallback(PVOID Handle
)
1390 return STATUS_UNSUCCESSFUL
;
1398 KeBugCheckEx(IN ULONG BugCheckCode
,
1399 IN ULONG_PTR BugCheckParameter1
,
1400 IN ULONG_PTR BugCheckParameter2
,
1401 IN ULONG_PTR BugCheckParameter3
,
1402 IN ULONG_PTR BugCheckParameter4
)
1404 /* Call the internal API */
1405 KeBugCheckWithTf(BugCheckCode
,
1418 KeBugCheck(ULONG BugCheckCode
)
1420 /* Call the internal API */
1421 KeBugCheckWithTf(BugCheckCode
, 0, 0, 0, 0, NULL
);
1429 KeEnterKernelDebugger(VOID
)
1431 /* Disable interrupts */
1432 KiHardwareTrigger
= 1;
1435 /* Check the bugcheck count */
1436 if (!InterlockedDecrement((PLONG
)&KeBugCheckCount
))
1438 /* There was only one, is the debugger disabled? */
1439 if (!(KdDebuggerEnabled
) && !(KdPitchDebugger
))
1441 /* Enable the debugger */
1442 KdInitSystem(0, NULL
);
1446 /* Break in the debugger */
1447 KiBugCheckDebugBreak(DBG_STATUS_FATAL
);