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 <reactos/bugcodes.h>
33 #endif /* LIBCAPTIVE */
35 #include <internal/ke.h>
36 #include <internal/ldr.h>
37 #include <internal/ps.h>
38 #include <internal/kd.h>
41 #include <internal/debug.h>
43 /* FUNCTIONS ****************************************************************/
46 RtlpDispatchException(IN PEXCEPTION_RECORD ExceptionRecord,
50 KiDispatchException(PEXCEPTION_RECORD ExceptionRecord,
53 KPROCESSOR_MODE PreviousMode,
56 EXCEPTION_DISPOSITION Value;
59 #endif /* LIBCAPTIVE */
60 KD_CONTINUE_TYPE Action = kdContinue;
62 DPRINT("KiDispatchException() called\n");
64 /* PCR->KeExceptionDispatchCount++; */
69 TContext.ContextFlags = CONTEXT_FULL;
70 if (PreviousMode == UserMode)
72 TContext.ContextFlags = TContext.ContextFlags | CONTEXT_DEBUGGER;
75 KeTrapFrameToContext(Tf, &TContext);
79 #endif /* LIBCAPTIVE */
82 if (ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT)
89 if (KdDebuggerEnabled && KdDebugState & KD_DEBUG_GDB)
91 Action = KdEnterDebuggerException (ExceptionRecord, Context, Tf);
94 else if (KdDebuggerEnabled && KdDebugState & KD_DEBUG_KDB)
96 Action = KdbEnterDebuggerException (ExceptionRecord, Context, Tf);
99 #endif /* LIBCAPTIVE */
100 if (Action != kdHandleException)
102 if (PreviousMode == UserMode)
106 #else /* !LIBCAPTIVE */
112 /* FIXME: Forward exception to user mode debugger */
114 /* FIXME: Check user mode stack for enough space */
117 * Let usermode try and handle the exception
120 (12 + sizeof(EXCEPTION_RECORD) + sizeof(CONTEXT));
121 Stack = (PULONG)Tf->Esp;
122 CDest = 3 + (ROUND_UP(sizeof(EXCEPTION_RECORD), 4) / 4);
125 /* Pointer to EXCEPTION_RECORD structure */
126 Stack[1] = (ULONG)&Stack[3];
127 /* Pointer to CONTEXT structure */
128 Stack[2] = (ULONG)&Stack[CDest];
129 memcpy(&Stack[3], ExceptionRecord, sizeof(EXCEPTION_RECORD));
130 memcpy(&Stack[CDest], Context, sizeof(CONTEXT));
132 Tf->Eip = (ULONG)LdrpGetSystemDllExceptionDispatcher();
136 /* FIXME: Forward the exception to the debugger */
138 /* FIXME: Forward the exception to the process exception port */
140 /* Terminate the offending thread */
141 DPRINT1("Unhandled UserMode exception, terminating thread\n");
142 ZwTerminateThread(NtCurrentThread(), ExceptionRecord->ExceptionCode);
144 /* If that fails then bugcheck */
145 DPRINT1("Could not terminate thread\n");
146 KeBugCheck(KMODE_EXCEPTION_NOT_HANDLED);
147 #endif /* LIBCAPTIVE */
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))
163 KeBugCheckWithTf(KMODE_EXCEPTION_NOT_HANDLED, 0, 0, 0, 0, Tf);
164 #else /* !LIBCAPTIVE */
166 #endif /* LIBCAPTIVE */
173 KeContextToTrapFrame (Context, KeGetCurrentThread()->TrapFrame);
174 #else /* !LIBCAPTIVE */
176 #endif /* LIBCAPTIVE */
183 ExRaiseAccessViolation (VOID)
185 ExRaiseStatus (STATUS_ACCESS_VIOLATION);
189 ExRaiseDatatypeMisalignment (VOID)
191 ExRaiseStatus (STATUS_DATATYPE_MISALIGNMENT);
195 ExRaiseStatus (IN NTSTATUS Status)
197 EXCEPTION_RECORD ExceptionRecord;
199 DPRINT("ExRaiseStatus(%x)\n", Status);
201 ExceptionRecord.ExceptionRecord = NULL;
202 ExceptionRecord.NumberParameters = 0;
203 ExceptionRecord.ExceptionCode = Status;
204 ExceptionRecord.ExceptionFlags = 0;
206 RtlRaiseException(&ExceptionRecord);
209 #endif /* LIBCAPTIVE */
212 NtRaiseException (IN PEXCEPTION_RECORD ExceptionRecord,
214 IN BOOLEAN SearchFrames)
216 KiDispatchException(ExceptionRecord,
218 PsGetCurrentThread()->Tcb.TrapFrame,
221 #else /* !LIBCAPTIVE */
223 #endif /* LIBCAPTIVE */
225 return(STATUS_SUCCESS);
230 RtlRaiseException(PEXCEPTION_RECORD ExceptionRecord)
233 ZwRaiseException(ExceptionRecord, NULL, TRUE);
234 #else /* !LIBCAPTIVE */
235 CONTEXT *LocalContext;
237 /* Do not use local variable to benefit from ElectricFence boundary checks */
238 LocalContext=ExAllocatePool(PagedPool,sizeof(LocalContext->ContextFlags));
239 LocalContext->ContextFlags=0;
241 NtRaiseException(ExceptionRecord, LocalContext, TRUE);
243 ExFreePool(LocalContext);
244 #endif /* LIBCAPTIVE */