Implemented KeSetEventBoostPriority, KeFindConfigurationEntry, KeDeregisterBugCheckCa...
[reactos.git] / reactos / ntoskrnl / ke / bug.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id: bug.c,v 1.47 2004/10/13 22:27:03 ion Exp $
20 *
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/ke/bug.c
23 * PURPOSE: Graceful system shutdown if a bug is detected
24 * PROGRAMMER: David Welch (welch@cwcom.net)
25 * PORTABILITY: Unchecked
26 * UPDATE HISTORY:
27 * Created 22/05/98
28 * Phillip Susi: 12/8/99: Minor fix
29 */
30
31 /* INCLUDES *****************************************************************/
32
33 #include <ntoskrnl.h>
34 #include <ntos/bootvid.h>
35 #include <internal/debug.h>
36 #include "../../hal/halx86/include/hal.h"
37
38 /* GLOBALS ******************************************************************/
39
40 static LIST_ENTRY BugcheckCallbackListHead = {NULL,NULL};
41 static ULONG InBugCheck;
42
43 /* FUNCTIONS *****************************************************************/
44
45 VOID INIT_FUNCTION
46 KeInitializeBugCheck(VOID)
47 {
48 InitializeListHead(&BugcheckCallbackListHead);
49 InBugCheck = 0;
50 }
51
52 /*
53 * @implemented
54 */
55 BOOLEAN STDCALL
56 KeDeregisterBugCheckCallback(PKBUGCHECK_CALLBACK_RECORD CallbackRecord)
57 {
58 /* Check the Current State */
59 if (CallbackRecord->State == BufferInserted) {
60 CallbackRecord->State = BufferEmpty;
61 RemoveEntryList(&CallbackRecord->Entry);
62 return TRUE;
63 }
64
65 /* The callback wasn't registered */
66 return FALSE;
67 }
68
69 /*
70 * @implemented
71 */
72 BOOLEAN STDCALL
73 KeRegisterBugCheckCallback(PKBUGCHECK_CALLBACK_RECORD CallbackRecord,
74 PKBUGCHECK_CALLBACK_ROUTINE CallbackRoutine,
75 PVOID Buffer,
76 ULONG Length,
77 PUCHAR Component)
78 {
79
80 /* Check the Current State first so we don't double-register */
81 if (CallbackRecord->State == BufferEmpty) {
82 CallbackRecord->Length = Length;
83 CallbackRecord->Buffer = Buffer;
84 CallbackRecord->Component = Component;
85 CallbackRecord->CallbackRoutine = CallbackRoutine;
86 CallbackRecord->State = BufferInserted;
87 InsertTailList(&BugcheckCallbackListHead, &CallbackRecord->Entry);
88
89 return TRUE;
90 }
91
92 /* The Callback was already registered */
93 return(FALSE);
94 }
95
96 VOID STDCALL
97 KeBugCheckWithTf(ULONG BugCheckCode,
98 ULONG BugCheckParameter1,
99 ULONG BugCheckParameter2,
100 ULONG BugCheckParameter3,
101 ULONG BugCheckParameter4,
102 PKTRAP_FRAME Tf)
103 {
104 PRTL_MESSAGE_RESOURCE_ENTRY Message;
105 NTSTATUS Status;
106 KIRQL OldIrql;
107
108 /* Make sure we're switching back to the blue screen and print messages on it */
109 HalReleaseDisplayOwnership();
110 if (0 == (KdDebugState & KD_DEBUG_GDB))
111 {
112 KdDebugState |= KD_DEBUG_SCREEN;
113 }
114
115 Ke386DisableInterrupts();
116 DebugLogDumpMessages();
117
118 if (KeGetCurrentIrql() < DISPATCH_LEVEL)
119 {
120 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
121 }
122 DbgPrint("Bug detected (code %x param %x %x %x %x)\n",
123 BugCheckCode,
124 BugCheckParameter1,
125 BugCheckParameter2,
126 BugCheckParameter3,
127 BugCheckParameter4);
128
129 Status = RtlFindMessage((PVOID)KERNEL_BASE, //0xC0000000,
130 11, //RT_MESSAGETABLE,
131 0x09, //0x409,
132 BugCheckCode,
133 &Message);
134 if (NT_SUCCESS(Status))
135 {
136 if (Message->Flags == 0)
137 DbgPrint(" %s\n", Message->Text);
138 else
139 DbgPrint(" %S\n", (PWSTR)Message->Text);
140 }
141 else
142 {
143 DbgPrint(" No message text found!\n\n");
144 }
145
146 if (InBugCheck == 1)
147 {
148 DbgPrint("Recursive bug check halting now\n");
149 Ke386HaltProcessor();
150 }
151 InBugCheck = 1;
152 if (Tf != NULL)
153 {
154 KiDumpTrapFrame(Tf, BugCheckParameter1, BugCheckParameter2);
155 }
156 else
157 {
158 #if defined(__GNUC__)
159 KeDumpStackFrames((PULONG)__builtin_frame_address(0));
160 #elif defined(_MSC_VER)
161 __asm push ebp
162 __asm call KeDumpStackFrames
163 __asm add esp, 4
164 #else
165 #error Unknown compiler for inline assembler
166 #endif
167 }
168 MmDumpToPagingFile(BugCheckCode, BugCheckParameter1,
169 BugCheckParameter2, BugCheckParameter3,
170 BugCheckParameter4, Tf);
171
172 if (KdDebuggerEnabled)
173 {
174 Ke386EnableInterrupts();
175 DbgBreakPointNoBugCheck();
176 Ke386DisableInterrupts();
177 }
178
179 for (;;)
180 {
181 Ke386HaltProcessor();
182 }
183 }
184
185 /*
186 * @implemented
187 */
188 VOID STDCALL
189 KeBugCheckEx(ULONG BugCheckCode,
190 ULONG BugCheckParameter1,
191 ULONG BugCheckParameter2,
192 ULONG BugCheckParameter3,
193 ULONG BugCheckParameter4)
194 /*
195 * FUNCTION: Brings the system down in a controlled manner when an
196 * inconsistency that might otherwise cause corruption has been detected
197 * ARGUMENTS:
198 * BugCheckCode = Specifies the reason for the bug check
199 * BugCheckParameter[1-4] = Additional information about bug
200 * RETURNS: Doesn't
201 */
202 {
203 KeBugCheckWithTf(BugCheckCode, BugCheckParameter1, BugCheckParameter2,
204 BugCheckParameter3, BugCheckParameter4, NULL);
205 }
206
207 /*
208 * @implemented
209 */
210 VOID STDCALL
211 KeBugCheck(ULONG BugCheckCode)
212 /*
213 * FUNCTION: Brings the system down in a controlled manner when an
214 * inconsistency that might otherwise cause corruption has been detected
215 * ARGUMENTS:
216 * BugCheckCode = Specifies the reason for the bug check
217 * RETURNS: Doesn't
218 */
219 {
220 KeBugCheckEx(BugCheckCode, 0, 0, 0, 0);
221 }
222
223 /* EOF */