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.31 2003/04/24 16:55:08 hbirr 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>
39 #include <internal/debug.h>
41 /* FUNCTIONS ****************************************************************/
44 RtlpDispatchException(IN PEXCEPTION_RECORD ExceptionRecord
,
48 KiDispatchException(PEXCEPTION_RECORD ExceptionRecord
,
51 KPROCESSOR_MODE PreviousMode
,
54 EXCEPTION_DISPOSITION Value
;
56 KD_CONTINUE_TYPE Action
= kdContinue
;
58 DPRINT("KiDispatchException() called\n");
60 /* PCR->KeExceptionDispatchCount++; */
64 TContext
.ContextFlags
= CONTEXT_FULL
;
65 if (PreviousMode
== UserMode
)
67 TContext
.ContextFlags
= TContext
.ContextFlags
| CONTEXT_DEBUGGER
;
70 KeTrapFrameToContext(Tf
, &TContext
);
76 if (ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
)
82 if (KdDebuggerEnabled
&& KdDebugState
& KD_DEBUG_GDB
)
84 Action
= KdEnterDebuggerException (ExceptionRecord
, Context
, Tf
);
87 else if (KdDebuggerEnabled
&& KdDebugState
& KD_DEBUG_KDB
)
89 Action
= KdbEnterDebuggerException (ExceptionRecord
, Context
, Tf
);
92 if (Action
!= kdHandleException
)
94 if (PreviousMode
== UserMode
)
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
109 (12 + sizeof(EXCEPTION_RECORD
) + sizeof(CONTEXT
));
110 Stack
= (PULONG
)Tf
->Esp
;
111 CDest
= 3 + (ROUND_UP(sizeof(EXCEPTION_RECORD
), 4) / 4);
114 /* Pointer to EXCEPTION_RECORD structure */
115 Stack
[1] = (ULONG
)&Stack
[3];
116 /* Pointer to CONTEXT structure */
117 Stack
[2] = (ULONG
)&Stack
[CDest
];
118 memcpy(&Stack
[3], ExceptionRecord
, sizeof(EXCEPTION_RECORD
));
119 memcpy(&Stack
[CDest
], Context
, sizeof(CONTEXT
));
121 Tf
->Eip
= (ULONG
)LdrpGetSystemDllExceptionDispatcher();
125 /* FIXME: Forward the exception to the debugger */
127 /* FIXME: Forward the exception to the process exception port */
129 /* Terminate the offending thread */
130 DPRINT1("Unhandled UserMode exception, terminating thread\n");
131 ZwTerminateThread(NtCurrentThread(), ExceptionRecord
->ExceptionCode
);
133 /* If that fails then bugcheck */
134 DPRINT1("Could not terminate thread\n");
135 KeBugCheck(KMODE_EXCEPTION_NOT_HANDLED
);
139 /* PreviousMode == KernelMode */
140 Value
= RtlpDispatchException (ExceptionRecord
, Context
);
142 DPRINT("RtlpDispatchException() returned with 0x%X\n", Value
);
144 * If RtlpDispatchException() does not handle the exception then
147 if (Value
!= ExceptionContinueExecution
||
148 0 != (ExceptionRecord
->ExceptionFlags
& EXCEPTION_NONCONTINUABLE
))
150 KeBugCheckWithTf(KMODE_EXCEPTION_NOT_HANDLED
, 0, 0, 0, 0, Tf
);
156 KeContextToTrapFrame (Context
, KeGetCurrentThread()->TrapFrame
);
161 ExRaiseAccessViolation (VOID
)
163 ExRaiseStatus (STATUS_ACCESS_VIOLATION
);
167 ExRaiseDatatypeMisalignment (VOID
)
169 ExRaiseStatus (STATUS_DATATYPE_MISALIGNMENT
);
173 ExRaiseStatus (IN NTSTATUS Status
)
175 EXCEPTION_RECORD ExceptionRecord
;
177 DPRINT("ExRaiseStatus(%x)\n", Status
);
179 ExceptionRecord
.ExceptionRecord
= NULL
;
180 ExceptionRecord
.NumberParameters
= 0;
181 ExceptionRecord
.ExceptionCode
= Status
;
182 ExceptionRecord
.ExceptionFlags
= 0;
184 RtlRaiseException(&ExceptionRecord
);
189 NtRaiseException (IN PEXCEPTION_RECORD ExceptionRecord
,
191 IN BOOLEAN SearchFrames
)
193 KiDispatchException(ExceptionRecord
,
195 PsGetCurrentThread()->Tcb
.TrapFrame
,
198 return(STATUS_SUCCESS
);
203 RtlRaiseException(PEXCEPTION_RECORD ExceptionRecord
)
205 ZwRaiseException(ExceptionRecord
, NULL
, TRUE
);