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 */
61 DPRINT("KiDispatchException() called\n");
63 /* PCR->KeExceptionDispatchCount++; */
68 TContext.ContextFlags = CONTEXT_FULL;
69 if (PreviousMode == UserMode)
71 TContext.ContextFlags = TContext.ContextFlags | CONTEXT_DEBUGGER;
74 KeTrapFrameToContext(Tf, &TContext);
78 #endif /* LIBCAPTIVE */
81 if (ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT)
86 if (PreviousMode == UserMode)
90 #else /* !LIBCAPTIVE */
96 /* FIXME: Give the kernel debugger a chance */
98 /* FIXME: Forward exception to user mode debugger */
100 /* FIXME: Check user mode stack for enough space */
104 * Let usermode try and handle the exception
107 (12 + sizeof(EXCEPTION_RECORD) + sizeof(CONTEXT));
108 Stack = (PULONG)Tf->Esp;
109 CDest = 3 + (ROUND_UP(sizeof(EXCEPTION_RECORD), 4) / 4);
112 /* Pointer to EXCEPTION_RECORD structure */
113 Stack[1] = (ULONG)&Stack[3];
114 /* Pointer to CONTEXT structure */
115 Stack[2] = (ULONG)&Stack[CDest];
116 memcpy(&Stack[3], ExceptionRecord, sizeof(EXCEPTION_RECORD));
117 memcpy(&Stack[CDest], Context, sizeof(CONTEXT));
119 Tf->Eip = (ULONG)LdrpGetSystemDllExceptionDispatcher();
123 /* FIXME: Forward the exception to the debugger */
125 /* FIXME: Forward the exception to the process exception port */
127 /* Terminate the offending thread */
128 ZwTerminateThread(NtCurrentThread(), ExceptionRecord->ExceptionCode);
130 /* If that fails then bugcheck */
131 DbgPrint("Could not terminate thread\n");
132 KeBugCheck(KMODE_EXCEPTION_NOT_HANDLED);
133 #endif /* LIBCAPTIVE */
137 KD_CONTINUE_TYPE Action = kdContinue;
139 /* PreviousMode == KernelMode */
142 if (KdDebuggerEnabled && KdDebugState & KD_DEBUG_GDB)
144 Action = KdEnterDebuggerException (ExceptionRecord, Context, Tf);
147 else if (KdDebuggerEnabled && KdDebugState & KD_DEBUG_KDB)
149 Action = KdbEnterDebuggerException (ExceptionRecord, Context, Tf);
152 #endif /* LIBCAPTIVE */
153 if (Action != kdHandleException)
155 Value = RtlpDispatchException (ExceptionRecord, Context);
157 DPRINT("RtlpDispatchException() returned with 0x%X\n", Value);
159 * If RtlpDispatchException() does not handle the exception then
162 if (Value != ExceptionContinueExecution)
165 KeBugCheck (KMODE_EXCEPTION_NOT_HANDLED);
166 #else /* !LIBCAPTIVE */
168 #endif /* LIBCAPTIVE */
174 KeContextToTrapFrame (Context, KeGetCurrentThread()->TrapFrame);
175 #else /* !LIBCAPTIVE */
177 #endif /* LIBCAPTIVE */
185 ExRaiseAccessViolation (VOID)
187 ExRaiseStatus (STATUS_ACCESS_VIOLATION);
191 ExRaiseDatatypeMisalignment (VOID)
193 ExRaiseStatus (STATUS_DATATYPE_MISALIGNMENT);
197 ExRaiseStatus (IN NTSTATUS Status)
199 EXCEPTION_RECORD ExceptionRecord;
201 DPRINT("ExRaiseStatus(%x)\n", Status);
203 ExceptionRecord.ExceptionRecord = NULL;
204 ExceptionRecord.NumberParameters = 0;
205 ExceptionRecord.ExceptionCode = Status;
206 ExceptionRecord.ExceptionFlags = 0;
208 RtlRaiseException(&ExceptionRecord);
211 #endif /* LIBCAPTIVE */
214 NtRaiseException (IN PEXCEPTION_RECORD ExceptionRecord,
216 IN BOOLEAN SearchFrames)
218 KiDispatchException(ExceptionRecord,
220 PsGetCurrentThread()->Tcb.TrapFrame,
223 #else /* !LIBCAPTIVE */
225 #endif /* LIBCAPTIVE */
227 return(STATUS_SUCCESS);
232 RtlRaiseException(PEXCEPTION_RECORD ExceptionRecord)
235 ZwRaiseException(ExceptionRecord, NULL, TRUE);
236 #else /* !LIBCAPTIVE */
237 CONTEXT *LocalContext;
239 /* Do not use local variable to benefit from ElectricFence boundary checks */
240 LocalContext=ExAllocatePool(PagedPool,sizeof(LocalContext->ContextFlags));
241 LocalContext->ContextFlags=0;
243 NtRaiseException(ExceptionRecord, LocalContext, TRUE);
245 ExFreePool(LocalContext);
246 #endif /* LIBCAPTIVE */