2003-08-11 Casper S. Hornstrup <chorns@users.sourceforge.net>
[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.35 2003/08/11 18:50:12 chorns 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 <roskrnl.h>
34 #include <ntos/bootvid.h>
35 #include <internal/kd.h>
36 #include <internal/ke.h>
37 #include <internal/ps.h>
38
39 #include <internal/debug.h>
40
41 #include "../../hal/halx86/include/hal.h"
42
43 /* GLOBALS ******************************************************************/
44
45 static LIST_ENTRY BugcheckCallbackListHead = {NULL,NULL};
46 static ULONG InBugCheck;
47
48 VOID PsDumpThreads(VOID);
49
50 /* FUNCTIONS *****************************************************************/
51
52 VOID
53 KeInitializeBugCheck(VOID)
54 {
55 InitializeListHead(&BugcheckCallbackListHead);
56 InBugCheck = 0;
57 }
58
59 /*
60 * @unimplemented
61 */
62 BOOLEAN STDCALL
63 KeDeregisterBugCheckCallback(PKBUGCHECK_CALLBACK_RECORD CallbackRecord)
64 {
65 UNIMPLEMENTED;
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 InsertTailList(&BugcheckCallbackListHead, &CallbackRecord->Entry);
80 CallbackRecord->Length = Length;
81 CallbackRecord->Buffer = Buffer;
82 CallbackRecord->Component = Component;
83 CallbackRecord->CallbackRoutine = CallbackRoutine;
84 return(TRUE);
85 }
86
87 VOID STDCALL
88 KeBugCheckWithTf(ULONG BugCheckCode,
89 ULONG BugCheckParameter1,
90 ULONG BugCheckParameter2,
91 ULONG BugCheckParameter3,
92 ULONG BugCheckParameter4,
93 PKTRAP_FRAME Tf)
94 {
95 PRTL_MESSAGE_RESOURCE_ENTRY Message;
96 NTSTATUS Status;
97
98 /*
99 * The bug check may have happened while the bootscreen image was displayed.
100 * If this happened, switch back to text mode.
101 */
102 if (InbvIsBootDriverInstalled())
103 {
104 InbvEnableBootDriver(FALSE);
105 }
106
107 /* Make sure we're switching back to the blue screen and print messages on it */
108 HalReleaseDisplayOwnership();
109 KdDebugState |= KD_DEBUG_SCREEN;
110
111 __asm__("cli\n\t");
112 DbgPrint("Bug detected (code %x param %x %x %x %x)\n",
113 BugCheckCode,
114 BugCheckParameter1,
115 BugCheckParameter2,
116 BugCheckParameter3,
117 BugCheckParameter4);
118
119 Status = RtlFindMessage((PVOID)KERNEL_BASE, //0xC0000000,
120 11, //RT_MESSAGETABLE,
121 0x09, //0x409,
122 BugCheckCode,
123 &Message);
124 if (NT_SUCCESS(Status))
125 {
126 if (Message->Flags == 0)
127 DbgPrint(" %s\n", Message->Text);
128 else
129 DbgPrint(" %S\n", (PWSTR)Message->Text);
130 }
131 else
132 {
133 DbgPrint(" No message text found!\n\n");
134 }
135
136 if (InBugCheck == 1)
137 {
138 DbgPrint("Recursive bug check halting now\n");
139 for (;;)
140 {
141 __asm__ ("hlt\n\t");
142 }
143 }
144 InBugCheck = 1;
145 KiDumpTrapFrame(Tf, BugCheckParameter1, BugCheckParameter2);
146 MmDumpToPagingFile(BugCheckCode, BugCheckParameter1,
147 BugCheckParameter2, BugCheckParameter3,
148 BugCheckParameter4, Tf);
149
150 if (KdDebuggerEnabled)
151 {
152 __asm__("sti\n\t");
153 DbgBreakPoint();
154 __asm__("cli\n\t");
155 }
156
157 for (;;)
158 {
159 __asm__("hlt\n\t");
160 }
161 }
162
163 /*
164 * @implemented
165 */
166 VOID STDCALL
167 KeBugCheckEx(ULONG BugCheckCode,
168 ULONG BugCheckParameter1,
169 ULONG BugCheckParameter2,
170 ULONG BugCheckParameter3,
171 ULONG BugCheckParameter4)
172 /*
173 * FUNCTION: Brings the system down in a controlled manner when an
174 * inconsistency that might otherwise cause corruption has been detected
175 * ARGUMENTS:
176 * BugCheckCode = Specifies the reason for the bug check
177 * BugCheckParameter[1-4] = Additional information about bug
178 * RETURNS: Doesn't
179 */
180 {
181 PRTL_MESSAGE_RESOURCE_ENTRY Message;
182 NTSTATUS Status;
183
184 /*
185 * The bug check may have happened while the bootscreen image was displayed.
186 * If this happened, switch back to text mode.
187 */
188 if (InbvIsBootDriverInstalled())
189 {
190 InbvEnableBootDriver(FALSE);
191 }
192
193 /* Make sure we're switching back to the blue screen and print messages on it */
194 HalReleaseDisplayOwnership();
195 KdDebugState |= KD_DEBUG_SCREEN;
196
197 __asm__("cli\n\t");
198 DbgPrint("Bug detected (code %x param %x %x %x %x)\n",
199 BugCheckCode,
200 BugCheckParameter1,
201 BugCheckParameter2,
202 BugCheckParameter3,
203 BugCheckParameter4);
204
205 Status = RtlFindMessage((PVOID)KERNEL_BASE, //0xC0000000,
206 11, //RT_MESSAGETABLE,
207 0x09, //0x409,
208 BugCheckCode,
209 &Message);
210 if (NT_SUCCESS(Status))
211 {
212 if (Message->Flags == 0)
213 DbgPrint(" %s\n", Message->Text);
214 else
215 DbgPrint(" %S\n", (PWSTR)Message->Text);
216 }
217 else
218 {
219 DbgPrint(" No message text found!\n\n");
220 }
221
222 if (InBugCheck == 1)
223 {
224 DbgPrint("Recursive bug check halting now\n");
225 for (;;)
226 {
227 __asm__("hlt\n\t");
228 }
229 }
230 InBugCheck = 1;
231 if (PsGetCurrentProcess() != NULL)
232 {
233 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId);
234 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName);
235 }
236 if (PsGetCurrentThread() != NULL)
237 {
238 DbgPrint("Thrd: %x Tid: %x\n",
239 PsGetCurrentThread(),
240 PsGetCurrentThread()->Cid.UniqueThread);
241 }
242 KeDumpStackFrames((PULONG)__builtin_frame_address(0));
243 MmDumpToPagingFile(BugCheckCode, BugCheckParameter1,
244 BugCheckParameter2, BugCheckParameter3,
245 BugCheckParameter4, NULL);
246
247 if (KdDebuggerEnabled)
248 {
249 __asm__("sti\n\t");
250 DbgBreakPoint();
251 __asm__("cli\n\t");
252 }
253
254 for (;;)
255 {
256 __asm__("hlt\n\t");
257 }
258 }
259
260 /*
261 * @implemented
262 */
263 VOID STDCALL
264 KeBugCheck(ULONG BugCheckCode)
265 /*
266 * FUNCTION: Brings the system down in a controlled manner when an
267 * inconsistency that might otherwise cause corruption has been detected
268 * ARGUMENTS:
269 * BugCheckCode = Specifies the reason for the bug check
270 * RETURNS: Doesn't
271 */
272 {
273 KeBugCheckEx(BugCheckCode, 0, 0, 0, 0);
274 }
275
276 /* EOF */