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.39 2003/12/30 18:52:04 fireball 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
);
93 if (Action
!= kdHandleException
)
95 if (PreviousMode
== UserMode
)
101 char temp_space
[12 + sizeof(EXCEPTION_RECORD
) + sizeof(CONTEXT
)]; // FIXME: HACKHACK
102 PULONG pNewUserStack
= (PULONG
)(Tf
->Esp
- (12 + sizeof(EXCEPTION_RECORD
) + sizeof(CONTEXT
)));
103 NTSTATUS StatusOfCopy
;
105 /* FIXME: Forward exception to user mode debugger */
107 /* FIXME: Check user mode stack for enough space */
110 * Let usermode try and handle the exception
112 Stack
= (PULONG
)temp_space
;
113 CDest
= 3 + (ROUND_UP(sizeof(EXCEPTION_RECORD
), 4) / 4);
116 /* Pointer to EXCEPTION_RECORD structure */
117 Stack
[1] = (ULONG
)&pNewUserStack
[3];
118 /* Pointer to CONTEXT structure */
119 Stack
[2] = (ULONG
)&pNewUserStack
[CDest
];
120 memcpy(&Stack
[3], ExceptionRecord
, sizeof(EXCEPTION_RECORD
));
121 memcpy(&Stack
[CDest
], Context
, sizeof(CONTEXT
));
123 StatusOfCopy
= MmCopyToCaller(pNewUserStack
,
125 (12 + sizeof(EXCEPTION_RECORD
) + sizeof(CONTEXT
)));
126 if (NT_SUCCESS(StatusOfCopy
))
128 Tf
->Esp
= (ULONG
)pNewUserStack
;
132 // Now it really hit the ventilation device. Sorry,
133 // can do nothing but kill the sucker.
134 ZwTerminateThread(NtCurrentThread(), ExceptionRecord
->ExceptionCode
);
135 DPRINT1("User-mode stack was invalid. Terminating target thread\nn");
137 Tf
->Eip
= (ULONG
)LdrpGetSystemDllExceptionDispatcher();
141 /* FIXME: Forward the exception to the debugger */
143 /* FIXME: Forward the exception to the process exception port */
145 /* Terminate the offending thread */
146 DPRINT1("Unhandled UserMode exception, terminating thread\n");
147 ZwTerminateThread(NtCurrentThread(), ExceptionRecord
->ExceptionCode
);
149 /* If that fails then bugcheck */
150 DPRINT1("Could not terminate thread\n");
151 KEBUGCHECK(KMODE_EXCEPTION_NOT_HANDLED
);
155 /* PreviousMode == KernelMode */
156 Value
= RtlpDispatchException (ExceptionRecord
, Context
);
158 DPRINT("RtlpDispatchException() returned with 0x%X\n", Value
);
160 * If RtlpDispatchException() does not handle the exception then
163 if (Value
!= ExceptionContinueExecution
||
164 0 != (ExceptionRecord
->ExceptionFlags
& EXCEPTION_NONCONTINUABLE
))
166 DbgPrint("ExceptionRecord->ExceptionAddress = 0x%x\n",
167 ExceptionRecord
->ExceptionAddress
);
168 KEBUGCHECKWITHTF(KMODE_EXCEPTION_NOT_HANDLED
, 0, 0, 0, 0, Tf
);
174 KeContextToTrapFrame (Context
, KeGetCurrentThread()->TrapFrame
);
182 ExRaiseAccessViolation (VOID
)
184 ExRaiseStatus (STATUS_ACCESS_VIOLATION
);
191 ExRaiseDatatypeMisalignment (VOID
)
193 ExRaiseStatus (STATUS_DATATYPE_MISALIGNMENT
);
200 ExRaiseStatus (IN NTSTATUS Status
)
202 EXCEPTION_RECORD ExceptionRecord
;
204 DPRINT("ExRaiseStatus(%x)\n", Status
);
206 ExceptionRecord
.ExceptionRecord
= NULL
;
207 ExceptionRecord
.NumberParameters
= 0;
208 ExceptionRecord
.ExceptionCode
= Status
;
209 ExceptionRecord
.ExceptionFlags
= 0;
211 RtlRaiseException(&ExceptionRecord
);
216 NtRaiseException (IN PEXCEPTION_RECORD ExceptionRecord
,
218 IN BOOLEAN SearchFrames
)
220 KiDispatchException(ExceptionRecord
,
222 PsGetCurrentThread()->Tcb
.TrapFrame
,
223 (KPROCESSOR_MODE
)ExGetPreviousMode(),
225 return(STATUS_SUCCESS
);
233 RtlRaiseException(PEXCEPTION_RECORD ExceptionRecord
)
235 ZwRaiseException(ExceptionRecord
, NULL
, TRUE
);