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.
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>
32 #include <internal/ke.h>
33 #include <internal/ldr.h>
34 #include <internal/ps.h>
35 #include <internal/kd.h>
38 #include <internal/debug.h>
40 /* FUNCTIONS ****************************************************************/
43 RtlpDispatchException(IN PEXCEPTION_RECORD ExceptionRecord,
47 KiDispatchException(PEXCEPTION_RECORD ExceptionRecord,
50 KPROCESSOR_MODE PreviousMode,
53 EXCEPTION_DISPOSITION Value;
56 DPRINT("KiDispatchException() called\n");
58 /* PCR->KeExceptionDispatchCount++; */
62 TContext.ContextFlags = CONTEXT_FULL;
63 if (PreviousMode == UserMode)
65 TContext.ContextFlags = TContext.ContextFlags | CONTEXT_DEBUGGER;
68 KeTrapFrameToContext(Tf, &TContext);
74 if (ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT)
79 if (PreviousMode == UserMode)
86 /* FIXME: Give the kernel debugger a chance */
88 /* FIXME: Forward exception to user mode debugger */
90 /* FIXME: Check user mode stack for enough space */
94 * Let usermode try and handle the exception
97 (12 + sizeof(EXCEPTION_RECORD) + sizeof(CONTEXT));
98 Stack = (PULONG)Tf->Esp;
99 CDest = 3 + (ROUND_UP(sizeof(EXCEPTION_RECORD), 4) / 4);
102 /* Pointer to EXCEPTION_RECORD structure */
103 Stack[1] = (ULONG)&Stack[3];
104 /* Pointer to CONTEXT structure */
105 Stack[2] = (ULONG)&Stack[CDest];
106 memcpy(&Stack[3], ExceptionRecord, sizeof(EXCEPTION_RECORD));
107 memcpy(&Stack[CDest], Context, sizeof(CONTEXT));
109 Tf->Eip = (ULONG)LdrpGetSystemDllExceptionDispatcher();
113 /* FIXME: Forward the exception to the debugger */
115 /* FIXME: Forward the exception to the process exception port */
117 /* Terminate the offending thread */
118 ZwTerminateThread(NtCurrentThread(), ExceptionRecord->ExceptionCode);
120 /* If that fails then bugcheck */
121 DbgPrint("Could not terminate thread\n");
122 KeBugCheck(KMODE_EXCEPTION_NOT_HANDLED);
126 KD_CONTINUE_TYPE Action = kdContinue;
128 /* PreviousMode == KernelMode */
130 if (KdDebuggerEnabled && KdDebugState & KD_DEBUG_GDB)
132 Action = KdEnterDebuggerException (ExceptionRecord, Context, Tf);
135 else if (KdDebuggerEnable && KdDebugState & KD_DEBUG_KDB)
137 Action = KdbEnterDebuggerException (ExceptionRecord, Context, Tf);
140 if (Action != kdHandleException)
142 Value = RtlpDispatchException (ExceptionRecord, Context);
144 DPRINT("RtlpDispatchException() returned with 0x%X\n", Value);
146 * If RtlpDispatchException() does not handle the exception then
149 if (Value != ExceptionContinueExecution)
151 KeBugCheck (KMODE_EXCEPTION_NOT_HANDLED);
156 KeContextToTrapFrame (Context, KeGetCurrentThread()->TrapFrame);
162 ExRaiseAccessViolation (VOID)
164 ExRaiseStatus (STATUS_ACCESS_VIOLATION);
168 ExRaiseDatatypeMisalignment (VOID)
170 ExRaiseStatus (STATUS_DATATYPE_MISALIGNMENT);
174 ExRaiseStatus (IN NTSTATUS Status)
176 EXCEPTION_RECORD ExceptionRecord;
178 DPRINT("ExRaiseStatus(%x)\n", Status);
180 ExceptionRecord.ExceptionRecord = NULL;
181 ExceptionRecord.NumberParameters = 0;
182 ExceptionRecord.ExceptionCode = Status;
183 ExceptionRecord.ExceptionFlags = 0;
185 RtlRaiseException(&ExceptionRecord);
190 NtRaiseException (IN PEXCEPTION_RECORD ExceptionRecord,
192 IN BOOLEAN SearchFrames)
194 KiDispatchException(ExceptionRecord,
196 PsGetCurrentThread()->Tcb.TrapFrame,
199 return(STATUS_SUCCESS);
204 RtlRaiseException(PEXCEPTION_RECORD ExceptionRecord)
206 ZwRaiseException(ExceptionRecord, NULL, TRUE);