3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/ke/bug.c
6 * PURPOSE: Graceful system shutdown if a bug is detected
8 * PROGRAMMERS: David Welch (welch@cwcom.net)
12 /* INCLUDES *****************************************************************/
15 #include <ntos/bootvid.h>
16 #include <internal/debug.h>
17 #include "../../hal/halx86/include/hal.h"
19 /* GLOBALS ******************************************************************/
21 static LIST_ENTRY BugcheckCallbackListHead
= {NULL
,NULL
};
22 static ULONG InBugCheck
;
24 /* FUNCTIONS *****************************************************************/
27 KeInitializeBugCheck(VOID
)
29 InitializeListHead(&BugcheckCallbackListHead
);
37 KeDeregisterBugCheckCallback(PKBUGCHECK_CALLBACK_RECORD CallbackRecord
)
39 /* Check the Current State */
40 if (CallbackRecord
->State
== BufferInserted
) {
41 CallbackRecord
->State
= BufferEmpty
;
42 RemoveEntryList(&CallbackRecord
->Entry
);
46 /* The callback wasn't registered */
54 KeRegisterBugCheckCallback(PKBUGCHECK_CALLBACK_RECORD CallbackRecord
,
55 PKBUGCHECK_CALLBACK_ROUTINE CallbackRoutine
,
61 /* Check the Current State first so we don't double-register */
62 if (CallbackRecord
->State
== BufferEmpty
) {
63 CallbackRecord
->Length
= Length
;
64 CallbackRecord
->Buffer
= Buffer
;
65 CallbackRecord
->Component
= Component
;
66 CallbackRecord
->CallbackRoutine
= CallbackRoutine
;
67 CallbackRecord
->State
= BufferInserted
;
68 InsertTailList(&BugcheckCallbackListHead
, &CallbackRecord
->Entry
);
73 /* The Callback was already registered */
78 KeBugCheckWithTf(ULONG BugCheckCode
,
79 ULONG BugCheckParameter1
,
80 ULONG BugCheckParameter2
,
81 ULONG BugCheckParameter3
,
82 ULONG BugCheckParameter4
,
85 PRTL_MESSAGE_RESOURCE_ENTRY Message
;
90 /* Make sure we're switching back to the blue screen and print messages on it */
91 HalReleaseDisplayOwnership();
92 if (0 == (KdDebugState
& KD_DEBUG_GDB
))
94 KdDebugState
|= KD_DEBUG_SCREEN
;
97 Ke386DisableInterrupts();
98 DebugLogDumpMessages();
100 if (MmGetKernelAddressSpace()->Lock
.Owner
== KeGetCurrentThread())
102 MmUnlockAddressSpace(MmGetKernelAddressSpace());
105 if (KeGetCurrentIrql() < DISPATCH_LEVEL
)
107 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
109 DbgPrint("Bug detected (code %x param %x %x %x %x)\n",
116 Status
= RtlFindMessage((PVOID
)KERNEL_BASE
, //0xC0000000,
117 11, //RT_MESSAGETABLE,
121 if (NT_SUCCESS(Status
))
123 if (Message
->Flags
== 0)
124 DbgPrint(" %s\n", Message
->Text
);
126 DbgPrint(" %S\n", (PWSTR
)Message
->Text
);
130 DbgPrint(" No message text found!\n\n");
132 Mask
= 1 << KeGetCurrentProcessorNumber();
133 if (InBugCheck
& Mask
)
136 DbgPrint("Recursive bug check on CPU%d, halting now\n", KeGetCurrentProcessorNumber());
139 * Send an ipi to all other processors which halt them too.
142 DbgPrint("Recursive bug check halting now\n");
144 Ke386HaltProcessor();
148 * Use InterlockedOr or InterlockedBitSet.
153 KiDumpTrapFrame(Tf
, BugCheckParameter1
, BugCheckParameter2
);
157 #if defined(__GNUC__)
158 KeDumpStackFrames((PULONG
)__builtin_frame_address(0));
159 #elif defined(_MSC_VER)
161 __asm call KeDumpStackFrames
164 #error Unknown compiler for inline assembler
167 MmDumpToPagingFile(BugCheckCode
, BugCheckParameter1
,
168 BugCheckParameter2
, BugCheckParameter3
,
169 BugCheckParameter4
, Tf
);
171 if (KdDebuggerEnabled
)
173 Ke386EnableInterrupts();
174 DbgBreakPointNoBugCheck();
175 Ke386DisableInterrupts();
182 * Send an ipi to all other processors which halt them too.
184 Ke386HaltProcessor();
192 KeBugCheckEx(ULONG BugCheckCode
,
193 ULONG BugCheckParameter1
,
194 ULONG BugCheckParameter2
,
195 ULONG BugCheckParameter3
,
196 ULONG BugCheckParameter4
)
198 * FUNCTION: Brings the system down in a controlled manner when an
199 * inconsistency that might otherwise cause corruption has been detected
201 * BugCheckCode = Specifies the reason for the bug check
202 * BugCheckParameter[1-4] = Additional information about bug
206 KeBugCheckWithTf(BugCheckCode
, BugCheckParameter1
, BugCheckParameter2
,
207 BugCheckParameter3
, BugCheckParameter4
, NULL
);
214 KeBugCheck(ULONG BugCheckCode
)
216 * FUNCTION: Brings the system down in a controlled manner when an
217 * inconsistency that might otherwise cause corruption has been detected
219 * BugCheckCode = Specifies the reason for the bug check
223 KeBugCheckEx(BugCheckCode
, 0, 0, 0, 0);