+FSCTL_DISMOUNT_VOLUME define
[reactos.git] / 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$
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 <ddk/ntddk.h>
34 #include <internal/ke.h>
35 #include <internal/ps.h>
36
37 #include <internal/debug.h>
38
39 /* GLOBALS ******************************************************************/
40
41 static LIST_ENTRY BugcheckCallbackListHead = {NULL,NULL};
42 static ULONG InBugCheck;
43
44 VOID PsDumpThreads(VOID);
45
46 /* FUNCTIONS *****************************************************************/
47
48 VOID
49 KeInitializeBugCheck(VOID)
50 {
51   InitializeListHead(&BugcheckCallbackListHead);
52   InBugCheck = 0;
53 }
54
55 BOOLEAN STDCALL
56 KeDeregisterBugCheckCallback(PKBUGCHECK_CALLBACK_RECORD CallbackRecord)
57 {
58   UNIMPLEMENTED;
59   return FALSE;
60 }
61
62 BOOLEAN STDCALL
63 KeRegisterBugCheckCallback(PKBUGCHECK_CALLBACK_RECORD CallbackRecord,
64                            PKBUGCHECK_CALLBACK_ROUTINE  CallbackRoutine,
65                            PVOID Buffer,
66                            ULONG Length,
67                            PUCHAR Component)
68 {
69   InsertTailList(&BugcheckCallbackListHead, &CallbackRecord->Entry);
70   CallbackRecord->Length = Length;
71   CallbackRecord->Buffer = Buffer;
72   CallbackRecord->Component = Component;
73   CallbackRecord->CallbackRoutine = CallbackRoutine;
74   return(TRUE);
75 }
76
77 VOID STDCALL
78 KeBugCheckWithTf(ULONG BugCheckCode,         
79                  ULONG BugCheckParameter1,
80                  ULONG BugCheckParameter2,
81                  ULONG BugCheckParameter3,
82                  ULONG BugCheckParameter4,
83                  PKTRAP_FRAME Tf)
84 {
85   PRTL_MESSAGE_RESOURCE_ENTRY Message;
86   NTSTATUS Status;
87   
88   __asm__("cli\n\t");
89   DbgPrint("Bug detected (code %x param %x %x %x %x)\n",
90            BugCheckCode,
91            BugCheckParameter1,
92            BugCheckParameter2,
93            BugCheckParameter3,
94            BugCheckParameter4);
95
96   Status = RtlFindMessage((PVOID)KERNEL_BASE, //0xC0000000,
97                           11, //RT_MESSAGETABLE,
98                           0x09, //0x409,
99                           BugCheckCode,
100                           &Message);
101   if (NT_SUCCESS(Status))
102     {
103       if (Message->Flags == 0)
104         DbgPrint("  %s\n", Message->Text);
105       else
106         DbgPrint("  %S\n", (PWSTR)Message->Text);
107     }
108   else
109     {
110       DbgPrint("  No message text found!\n\n");
111     }
112
113   if (InBugCheck == 1)
114     {
115       DbgPrint("Recursive bug check halting now\n");
116       for (;;)
117         {
118           __asm__ ("hlt\n\t");
119         }
120     }
121   InBugCheck = 1;
122   KiDumpTrapFrame(Tf, BugCheckParameter1, BugCheckParameter2);
123   MmDumpToPagingFile(BugCheckCode, BugCheckParameter1, 
124                      BugCheckParameter2, BugCheckParameter3,
125                      BugCheckParameter4, Tf);
126
127   if (KdDebuggerEnabled)
128     {
129       __asm__("sti\n\t");
130       DbgBreakPoint();
131       __asm__("cli\n\t");
132     }
133
134   for (;;)
135     {
136       __asm__("hlt\n\t");
137     }
138 }
139
140 VOID STDCALL
141 KeBugCheckEx(ULONG BugCheckCode,
142              ULONG BugCheckParameter1,
143              ULONG BugCheckParameter2,
144              ULONG BugCheckParameter3,
145              ULONG BugCheckParameter4)
146 /*
147  * FUNCTION: Brings the system down in a controlled manner when an 
148  * inconsistency that might otherwise cause corruption has been detected
149  * ARGUMENTS:
150  *           BugCheckCode = Specifies the reason for the bug check
151  *           BugCheckParameter[1-4] = Additional information about bug
152  * RETURNS: Doesn't
153  */
154 {
155   PRTL_MESSAGE_RESOURCE_ENTRY Message;
156   NTSTATUS Status;
157   
158   __asm__("cli\n\t");
159   DbgPrint("Bug detected (code %x param %x %x %x %x)\n",
160            BugCheckCode,
161            BugCheckParameter1,
162            BugCheckParameter2,
163            BugCheckParameter3,
164            BugCheckParameter4);
165
166   Status = RtlFindMessage((PVOID)KERNEL_BASE, //0xC0000000,
167                           11, //RT_MESSAGETABLE,
168                           0x09, //0x409,
169                           BugCheckCode,
170                           &Message);
171   if (NT_SUCCESS(Status))
172     {
173       if (Message->Flags == 0)
174         DbgPrint("  %s\n", Message->Text);
175       else
176         DbgPrint("  %S\n", (PWSTR)Message->Text);
177     }
178   else
179     {
180       DbgPrint("  No message text found!\n\n");
181     }
182
183   if (InBugCheck == 1)
184     {
185       DbgPrint("Recursive bug check halting now\n");
186       for (;;)
187         {
188           __asm__("hlt\n\t");
189         }
190     }
191   InBugCheck = 1;
192   if (PsGetCurrentProcess() != NULL)
193     {
194       DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId);
195       DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName);
196     }
197   if (PsGetCurrentThread() != NULL)
198     {
199       DbgPrint("Thrd: %x Tid: %x\n",
200                PsGetCurrentThread(),
201                PsGetCurrentThread()->Cid.UniqueThread);
202     }
203   KeDumpStackFrames((PULONG)__builtin_frame_address(0));
204   MmDumpToPagingFile(BugCheckCode, BugCheckParameter1, 
205                      BugCheckParameter2, BugCheckParameter3,
206                      BugCheckParameter4, NULL);
207
208   if (KdDebuggerEnabled)
209     {
210       __asm__("sti\n\t");
211       DbgBreakPoint();
212       __asm__("cli\n\t");
213     }
214
215   for (;;)
216     {
217       __asm__("hlt\n\t");
218     }
219 }
220
221 VOID STDCALL
222 KeBugCheck(ULONG BugCheckCode)
223 /*
224  * FUNCTION: Brings the system down in a controlled manner when an 
225  * inconsistency that might otherwise cause corruption has been detected
226  * ARGUMENTS:
227  *           BugCheckCode = Specifies the reason for the bug check
228  * RETURNS: Doesn't
229  */
230 {
231   KeBugCheckEx(BugCheckCode, 0, 0, 0, 0);
232 }
233
234 /* EOF */