3 * Copyright (C) 2000 ReactOS Team
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.
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.
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.
19 /* $Id: catch.c,v 1.47 2004/08/15 16:39:05 chorns Exp $
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/ke/catch.c
23 * PURPOSE: Exception handling
24 * PROGRAMMER: David Welch (welch@mcmail.com)
25 * Casper S. Hornstrup (chorns@users.sourceforge.net)
28 /* INCLUDES *****************************************************************/
32 #include <internal/debug.h>
34 /* FUNCTIONS ****************************************************************/
37 RtlpDispatchException(IN PEXCEPTION_RECORD ExceptionRecord
,
41 KiDispatchException(PEXCEPTION_RECORD ExceptionRecord
,
44 KPROCESSOR_MODE PreviousMode
,
47 EXCEPTION_DISPOSITION Value
;
49 KD_CONTINUE_TYPE Action
= kdContinue
;
51 DPRINT("KiDispatchException() called\n");
53 /* PCR->KeExceptionDispatchCount++; */
57 TContext
.ContextFlags
= CONTEXT_FULL
;
58 if (PreviousMode
== UserMode
)
60 TContext
.ContextFlags
= TContext
.ContextFlags
| CONTEXT_DEBUGGER
;
63 KeTrapFrameToContext(Tf
, &TContext
);
69 if (ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
)
75 if (KdDebuggerEnabled
&& KdDebugState
& KD_DEBUG_GDB
)
77 Action
= KdEnterDebuggerException (ExceptionRecord
, Context
, Tf
);
80 else if (KdDebuggerEnabled
&& KdDebugState
& KD_DEBUG_KDB
)
82 Action
= KdbEnterDebuggerException (ExceptionRecord
, Context
, Tf
);
83 if (Action
== kdContinue
)
89 if (Action
!= kdHandleException
)
91 if (PreviousMode
== UserMode
)
97 char temp_space
[12 + sizeof(EXCEPTION_RECORD
) + sizeof(CONTEXT
)]; // FIXME: HACKHACK
98 PULONG pNewUserStack
= (PULONG
)(Tf
->Esp
- (12 + sizeof(EXCEPTION_RECORD
) + sizeof(CONTEXT
)));
99 NTSTATUS StatusOfCopy
;
101 /* FIXME: Forward exception to user mode debugger */
103 /* FIXME: Check user mode stack for enough space */
106 * Let usermode try and handle the exception
108 Stack
= (PULONG
)temp_space
;
109 CDest
= 3 + (ROUND_UP(sizeof(EXCEPTION_RECORD
), 4) / 4);
112 /* Pointer to EXCEPTION_RECORD structure */
113 Stack
[1] = (ULONG
)&pNewUserStack
[3];
114 /* Pointer to CONTEXT structure */
115 Stack
[2] = (ULONG
)&pNewUserStack
[CDest
];
116 memcpy(&Stack
[3], ExceptionRecord
, sizeof(EXCEPTION_RECORD
));
117 memcpy(&Stack
[CDest
], Context
, sizeof(CONTEXT
));
119 StatusOfCopy
= MmCopyToCaller(pNewUserStack
,
121 (12 + sizeof(EXCEPTION_RECORD
) + sizeof(CONTEXT
)));
122 if (NT_SUCCESS(StatusOfCopy
))
124 Tf
->Esp
= (ULONG
)pNewUserStack
;
128 // Now it really hit the ventilation device. Sorry,
129 // can do nothing but kill the sucker.
130 ZwTerminateThread(NtCurrentThread(), ExceptionRecord
->ExceptionCode
);
131 DPRINT1("User-mode stack was invalid. Terminating target thread\nn");
133 Tf
->Eip
= (ULONG
)LdrpGetSystemDllExceptionDispatcher();
137 /* FIXME: Forward the exception to the debugger */
139 /* FIXME: Forward the exception to the process exception port */
141 /* Terminate the offending thread */
142 DPRINT1("Unhandled UserMode exception, terminating thread\n");
143 ZwTerminateThread(NtCurrentThread(), ExceptionRecord
->ExceptionCode
);
145 /* If that fails then bugcheck */
146 DPRINT1("Could not terminate thread\n");
147 KEBUGCHECK(KMODE_EXCEPTION_NOT_HANDLED
);
151 /* PreviousMode == KernelMode */
152 Value
= RtlpDispatchException (ExceptionRecord
, Context
);
154 DPRINT("RtlpDispatchException() returned with 0x%X\n", Value
);
156 * If RtlpDispatchException() does not handle the exception then
159 if (Value
!= ExceptionContinueExecution
||
160 0 != (ExceptionRecord
->ExceptionFlags
& EXCEPTION_NONCONTINUABLE
))
162 DPRINT("ExceptionRecord->ExceptionAddress = 0x%x\n",
163 ExceptionRecord
->ExceptionAddress
);
164 KEBUGCHECKWITHTF(KMODE_EXCEPTION_NOT_HANDLED
, 0, 0, 0, 0, Tf
);
170 KeContextToTrapFrame (Context
, KeGetCurrentThread()->TrapFrame
);
178 ExRaiseAccessViolation (VOID
)
180 ExRaiseStatus (STATUS_ACCESS_VIOLATION
);
187 ExRaiseDatatypeMisalignment (VOID
)
189 ExRaiseStatus (STATUS_DATATYPE_MISALIGNMENT
);
196 ExRaiseStatus (IN NTSTATUS Status
)
198 EXCEPTION_RECORD ExceptionRecord
;
200 DPRINT("ExRaiseStatus(%x)\n", Status
);
202 ExceptionRecord
.ExceptionRecord
= NULL
;
203 ExceptionRecord
.NumberParameters
= 0;
204 ExceptionRecord
.ExceptionCode
= Status
;
205 ExceptionRecord
.ExceptionFlags
= 0;
207 RtlRaiseException(&ExceptionRecord
);
217 PEXCEPTION_RECORD pExcptRec
228 IN NTSTATUS ErrorStatus
,
229 IN ULONG NumberOfParameters
,
230 IN PUNICODE_STRING UnicodeStringParameterMask OPTIONAL
,
231 IN PVOID
*Parameters
,
232 IN HARDERROR_RESPONSE_OPTION ResponseOption
,
233 OUT PHARDERROR_RESPONSE Response
245 ExSystemExceptionFilter()
256 KeDeregisterBugCheckReasonCallback(
257 IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord
269 KeGetRecommendedSharedDataAlignment(
282 KeRegisterBugCheckReasonCallback(
283 IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord
,
284 IN PKBUGCHECK_REASON_CALLBACK_ROUTINE CallbackRoutine
,
285 IN KBUGCHECK_CALLBACK_REASON Reason
,