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.45 2004/07/02 12:43:38 royce 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 *****************************************************************/
30 #include <ddk/ntddk.h>
31 #include <reactos/bugcodes.h>
33 #include <internal/ke.h>
34 #include <internal/ldr.h>
35 #include <internal/ps.h>
36 #include <internal/kd.h>
37 #include <internal/safe.h>
40 #include <internal/debug.h>
42 /* FUNCTIONS ****************************************************************/
45 RtlpDispatchException(IN PEXCEPTION_RECORD ExceptionRecord
,
49 KiDispatchException(PEXCEPTION_RECORD ExceptionRecord
,
52 KPROCESSOR_MODE PreviousMode
,
55 EXCEPTION_DISPOSITION Value
;
57 KD_CONTINUE_TYPE Action
= kdContinue
;
59 DPRINT("KiDispatchException() called\n");
61 /* PCR->KeExceptionDispatchCount++; */
65 TContext
.ContextFlags
= CONTEXT_FULL
;
66 if (PreviousMode
== UserMode
)
68 TContext
.ContextFlags
= TContext
.ContextFlags
| CONTEXT_DEBUGGER
;
71 KeTrapFrameToContext(Tf
, &TContext
);
77 if (ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
)
83 if (KdDebuggerEnabled
&& KdDebugState
& KD_DEBUG_GDB
)
85 Action
= KdEnterDebuggerException (ExceptionRecord
, Context
, Tf
);
88 else if (KdDebuggerEnabled
&& KdDebugState
& KD_DEBUG_KDB
)
90 Action
= KdbEnterDebuggerException (ExceptionRecord
, Context
, Tf
);
91 if (Action
== kdContinue
)
97 if (Action
!= kdHandleException
)
99 if (PreviousMode
== UserMode
)
105 char temp_space
[12 + sizeof(EXCEPTION_RECORD
) + sizeof(CONTEXT
)]; // FIXME: HACKHACK
106 PULONG pNewUserStack
= (PULONG
)(Tf
->Esp
- (12 + sizeof(EXCEPTION_RECORD
) + sizeof(CONTEXT
)));
107 NTSTATUS StatusOfCopy
;
109 /* FIXME: Forward exception to user mode debugger */
111 /* FIXME: Check user mode stack for enough space */
114 * Let usermode try and handle the exception
116 Stack
= (PULONG
)temp_space
;
117 CDest
= 3 + (ROUND_UP(sizeof(EXCEPTION_RECORD
), 4) / 4);
120 /* Pointer to EXCEPTION_RECORD structure */
121 Stack
[1] = (ULONG
)&pNewUserStack
[3];
122 /* Pointer to CONTEXT structure */
123 Stack
[2] = (ULONG
)&pNewUserStack
[CDest
];
124 memcpy(&Stack
[3], ExceptionRecord
, sizeof(EXCEPTION_RECORD
));
125 memcpy(&Stack
[CDest
], Context
, sizeof(CONTEXT
));
127 StatusOfCopy
= MmCopyToCaller(pNewUserStack
,
129 (12 + sizeof(EXCEPTION_RECORD
) + sizeof(CONTEXT
)));
130 if (NT_SUCCESS(StatusOfCopy
))
132 Tf
->Esp
= (ULONG
)pNewUserStack
;
136 // Now it really hit the ventilation device. Sorry,
137 // can do nothing but kill the sucker.
138 ZwTerminateThread(NtCurrentThread(), ExceptionRecord
->ExceptionCode
);
139 DPRINT1("User-mode stack was invalid. Terminating target thread\nn");
141 Tf
->Eip
= (ULONG
)LdrpGetSystemDllExceptionDispatcher();
145 /* FIXME: Forward the exception to the debugger */
147 /* FIXME: Forward the exception to the process exception port */
149 /* Terminate the offending thread */
150 DPRINT1("Unhandled UserMode exception, terminating thread\n");
151 ZwTerminateThread(NtCurrentThread(), ExceptionRecord
->ExceptionCode
);
153 /* If that fails then bugcheck */
154 DPRINT1("Could not terminate thread\n");
155 KEBUGCHECK(KMODE_EXCEPTION_NOT_HANDLED
);
159 /* PreviousMode == KernelMode */
160 Value
= RtlpDispatchException (ExceptionRecord
, Context
);
162 DPRINT("RtlpDispatchException() returned with 0x%X\n", Value
);
164 * If RtlpDispatchException() does not handle the exception then
167 if (Value
!= ExceptionContinueExecution
||
168 0 != (ExceptionRecord
->ExceptionFlags
& EXCEPTION_NONCONTINUABLE
))
170 DPRINT("ExceptionRecord->ExceptionAddress = 0x%x\n",
171 ExceptionRecord
->ExceptionAddress
);
172 KEBUGCHECKWITHTF(KMODE_EXCEPTION_NOT_HANDLED
, 0, 0, 0, 0, Tf
);
178 KeContextToTrapFrame (Context
, KeGetCurrentThread()->TrapFrame
);
186 ExRaiseAccessViolation (VOID
)
188 ExRaiseStatus (STATUS_ACCESS_VIOLATION
);
195 ExRaiseDatatypeMisalignment (VOID
)
197 ExRaiseStatus (STATUS_DATATYPE_MISALIGNMENT
);
204 ExRaiseStatus (IN NTSTATUS Status
)
206 EXCEPTION_RECORD ExceptionRecord
;
208 DPRINT("ExRaiseStatus(%x)\n", Status
);
210 ExceptionRecord
.ExceptionRecord
= NULL
;
211 ExceptionRecord
.NumberParameters
= 0;
212 ExceptionRecord
.ExceptionCode
= Status
;
213 ExceptionRecord
.ExceptionFlags
= 0;
215 RtlRaiseException(&ExceptionRecord
);
225 PEXCEPTION_RECORD pExcptRec
236 IN NTSTATUS ErrorStatus
,
237 IN ULONG NumberOfParameters
,
238 IN PUNICODE_STRING UnicodeStringParameterMask OPTIONAL
,
239 IN PVOID
*Parameters
,
240 IN HARDERROR_RESPONSE_OPTION ResponseOption
,
241 OUT PHARDERROR_RESPONSE Response
253 ExSystemExceptionFilter()
264 KeDeregisterBugCheckReasonCallback(
265 IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord
277 KeGetRecommendedSharedDataAlignment(
290 KeRegisterBugCheckReasonCallback(
291 IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord
,
292 IN PKBUGCHECK_REASON_CALLBACK_ROUTINE CallbackRoutine
,
293 IN KBUGCHECK_CALLBACK_REASON Reason
,
305 RtlRaiseException(PEXCEPTION_RECORD ExceptionRecord
)
307 ZwRaiseException(ExceptionRecord
, NULL
, TRUE
);