Add some more Alpha asm code and Alpha-specific parts.
[wine/multimedia.git] / dlls / ntdll / exception.c
blobe4822fbe2f34a885aa22989387287520881911dc
1 /*
2 * NT exception handling routines
4 * Copyright 1999 Turchanov Sergey
5 * Copyright 1999 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <signal.h>
27 #include <stdarg.h>
29 #include "ntstatus.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "thread.h"
33 #include "winternl.h"
34 #include "wine/exception.h"
35 #include "wine/server.h"
36 #include "wine/list.h"
37 #include "wine/debug.h"
38 #include "excpt.h"
39 #include "ntdll_misc.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(seh);
43 /* Exception record for handling exceptions happening inside exception handlers */
44 typedef struct
46 EXCEPTION_REGISTRATION_RECORD frame;
47 EXCEPTION_REGISTRATION_RECORD *prevFrame;
48 } EXC_NESTED_FRAME;
50 typedef struct
52 struct list entry;
53 PVECTORED_EXCEPTION_HANDLER func;
54 } VECTORED_HANDLER;
56 static struct list vectored_handlers = LIST_INIT(vectored_handlers);
58 static CRITICAL_SECTION vectored_handlers_section;
59 static CRITICAL_SECTION_DEBUG critsect_debug =
61 0, 0, &vectored_handlers_section,
62 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
63 0, 0, { 0, (DWORD)(__FILE__ ": vectored_handlers_section") }
65 static CRITICAL_SECTION vectored_handlers_section = { &critsect_debug, -1, 0, 0, 0, 0 };
67 #ifdef __i386__
68 # define GET_IP(context) ((LPVOID)(context)->Eip)
69 #elif defined(__sparc__)
70 # define GET_IP(context) ((LPVOID)(context)->pc)
71 #elif defined(__powerpc__)
72 # define GET_IP(context) ((LPVOID)(context)->Iar)
73 #elif defined(__ALPHA__)
74 # define GET_IP(context) ((LPVOID)(context)->Fir)
75 #else
76 # error You must define GET_IP for this CPU
77 #endif
80 /*******************************************************************
81 * EXC_RaiseHandler
83 * Handler for exceptions happening inside a handler.
85 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
86 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
88 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
89 return ExceptionContinueSearch;
90 /* We shouldn't get here so we store faulty frame in dispatcher */
91 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
92 return ExceptionNestedException;
96 /*******************************************************************
97 * EXC_UnwindHandler
99 * Handler for exceptions happening inside an unwind handler.
101 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
102 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
104 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
105 return ExceptionContinueSearch;
106 /* We shouldn't get here so we store faulty frame in dispatcher */
107 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
108 return ExceptionCollidedUnwind;
112 /*******************************************************************
113 * EXC_CallHandler
115 * Call an exception handler, setting up an exception frame to catch exceptions
116 * happening during the handler execution.
117 * Please do not change the first 4 parameters order in any way - some exceptions handlers
118 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
120 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
121 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher,
122 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
124 EXC_NESTED_FRAME newframe;
125 DWORD ret;
127 newframe.frame.Handler = nested_handler;
128 newframe.prevFrame = frame;
129 __wine_push_frame( &newframe.frame );
130 TRACE( "calling handler at %p code=%lx flags=%lx\n",
131 handler, record->ExceptionCode, record->ExceptionFlags );
132 ret = handler( record, frame, context, dispatcher );
133 TRACE( "handler returned %lx\n", ret );
134 __wine_pop_frame( &newframe.frame );
135 return ret;
139 /**********************************************************************
140 * send_debug_event
142 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
144 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
146 int ret;
147 HANDLE handle = 0;
149 if (!NtCurrentTeb()->Peb->BeingDebugged) return 0; /* no debugger present */
151 SERVER_START_REQ( queue_exception_event )
153 req->first = first_chance;
154 wine_server_add_data( req, context, sizeof(*context) );
155 wine_server_add_data( req, rec, sizeof(*rec) );
156 if (!wine_server_call( req )) handle = reply->handle;
158 SERVER_END_REQ;
159 if (!handle) return 0;
161 /* No need to wait on the handle since the process gets suspended
162 * once the event is passed to the debugger, so when we get back
163 * here the event has been continued already.
165 SERVER_START_REQ( get_exception_status )
167 req->handle = handle;
168 wine_server_set_reply( req, context, sizeof(*context) );
169 wine_server_call( req );
170 ret = reply->status;
172 SERVER_END_REQ;
173 NtClose( handle );
174 return ret;
178 /**********************************************************************
179 * call_vectored_handlers
181 * Call the vectored handlers chain.
183 static LONG call_vectored_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
185 struct list *ptr;
186 LONG ret = EXCEPTION_CONTINUE_SEARCH;
187 EXCEPTION_POINTERS except_ptrs;
189 except_ptrs.ExceptionRecord = rec;
190 except_ptrs.ContextRecord = context;
192 RtlEnterCriticalSection( &vectored_handlers_section );
193 LIST_FOR_EACH( ptr, &vectored_handlers )
195 VECTORED_HANDLER *handler = LIST_ENTRY( ptr, VECTORED_HANDLER, entry );
196 ret = handler->func( &except_ptrs );
197 if (ret == EXCEPTION_CONTINUE_EXECUTION) break;
199 RtlLeaveCriticalSection( &vectored_handlers_section );
200 return ret;
204 /*******************************************************************
205 * EXC_DefaultHandling
207 * Default handling for exceptions. Called when we didn't find a suitable handler.
209 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
211 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
213 if (rec->ExceptionFlags & EH_STACK_INVALID)
214 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
215 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
216 ERR("Process attempted to continue execution after noncontinuable exception.\n");
217 else
218 ERR("Unhandled exception code %lx flags %lx addr %p\n",
219 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
220 NtTerminateProcess( NtCurrentProcess(), 1 );
224 /***********************************************************************
225 * RtlRaiseException (NTDLL.@)
227 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
229 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
230 EXCEPTION_RECORD newrec;
231 DWORD res, c;
233 TRACE( "code=%lx flags=%lx addr=%p\n", rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
234 for (c=0; c<rec->NumberParameters; c++) TRACE(" info[%ld]=%08lx\n", c, rec->ExceptionInformation[c]);
235 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
236 FIXME( "call to unimplemented function %s.%s\n",
237 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
238 #ifdef __i386__
239 else
241 TRACE(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
242 context->Eax, context->Ebx, context->Ecx,
243 context->Edx, context->Esi, context->Edi );
244 TRACE(" ebp=%08lx esp=%08lx cs=%04lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
245 context->Ebp, context->Esp, context->SegCs, context->SegDs,
246 context->SegEs, context->SegFs, context->SegGs, context->EFlags );
248 #endif
250 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
252 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION) return;
254 frame = NtCurrentTeb()->Tib.ExceptionList;
255 nested_frame = NULL;
256 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
258 /* Check frame address */
259 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
260 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
261 (int)frame & 3)
263 rec->ExceptionFlags |= EH_STACK_INVALID;
264 break;
267 /* Call handler */
268 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
269 if (frame == nested_frame)
271 /* no longer nested */
272 nested_frame = NULL;
273 rec->ExceptionFlags &= ~EH_NESTED_CALL;
276 switch(res)
278 case ExceptionContinueExecution:
279 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
280 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
281 newrec.ExceptionFlags = EH_NONCONTINUABLE;
282 newrec.ExceptionRecord = rec;
283 newrec.NumberParameters = 0;
284 RtlRaiseException( &newrec ); /* never returns */
285 break;
286 case ExceptionContinueSearch:
287 break;
288 case ExceptionNestedException:
289 if (nested_frame < dispatch) nested_frame = dispatch;
290 rec->ExceptionFlags |= EH_NESTED_CALL;
291 break;
292 default:
293 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
294 newrec.ExceptionFlags = EH_NONCONTINUABLE;
295 newrec.ExceptionRecord = rec;
296 newrec.NumberParameters = 0;
297 RtlRaiseException( &newrec ); /* never returns */
298 break;
300 frame = frame->Prev;
302 EXC_DefaultHandling( rec, context );
305 /**********************************************************************/
307 #ifdef DEFINE_REGS_ENTRYPOINT
308 DEFINE_REGS_ENTRYPOINT( RtlRaiseException, EXC_RtlRaiseException, 4, 4 );
309 #else
310 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
312 CONTEXT context;
313 memset( &context, 0, sizeof(context) );
314 EXC_RtlRaiseException( rec, &context );
316 #endif
319 /*******************************************************************
320 * RtlUnwind (NTDLL.@)
322 void WINAPI EXC_RtlUnwind( EXCEPTION_REGISTRATION_RECORD* pEndFrame, PVOID unusedEip,
323 PEXCEPTION_RECORD pRecord, PVOID returnEax, CONTEXT *context )
325 EXCEPTION_RECORD record, newrec;
326 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
328 #ifdef __i386__
329 context->Eax = (DWORD)returnEax;
330 #endif
332 /* build an exception record, if we do not have one */
333 if (!pRecord)
335 record.ExceptionCode = STATUS_UNWIND;
336 record.ExceptionFlags = 0;
337 record.ExceptionRecord = NULL;
338 record.ExceptionAddress = GET_IP(context);
339 record.NumberParameters = 0;
340 pRecord = &record;
343 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
345 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
347 /* get chain of exception frames */
348 frame = NtCurrentTeb()->Tib.ExceptionList;
349 while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != pEndFrame))
351 /* Check frame address */
352 if (pEndFrame && (frame > pEndFrame))
354 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
355 newrec.ExceptionFlags = EH_NONCONTINUABLE;
356 newrec.ExceptionRecord = pRecord;
357 newrec.NumberParameters = 0;
358 RtlRaiseException( &newrec ); /* never returns */
360 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
361 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
362 (int)frame & 3)
364 newrec.ExceptionCode = STATUS_BAD_STACK;
365 newrec.ExceptionFlags = EH_NONCONTINUABLE;
366 newrec.ExceptionRecord = pRecord;
367 newrec.NumberParameters = 0;
368 RtlRaiseException( &newrec ); /* never returns */
371 /* Call handler */
372 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
373 frame->Handler, EXC_UnwindHandler ))
375 case ExceptionContinueSearch:
376 break;
377 case ExceptionCollidedUnwind:
378 frame = dispatch;
379 break;
380 default:
381 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
382 newrec.ExceptionFlags = EH_NONCONTINUABLE;
383 newrec.ExceptionRecord = pRecord;
384 newrec.NumberParameters = 0;
385 RtlRaiseException( &newrec ); /* never returns */
386 break;
388 frame = __wine_pop_frame( frame );
392 /**********************************************************************/
394 #ifdef DEFINE_REGS_ENTRYPOINT
395 DEFINE_REGS_ENTRYPOINT( RtlUnwind, EXC_RtlUnwind, 16, 16 );
396 #else
397 void WINAPI RtlUnwind( PVOID pEndFrame, PVOID unusedEip,
398 PEXCEPTION_RECORD pRecord, PVOID returnEax )
400 CONTEXT context;
401 memset( &context, 0, sizeof(context) );
402 EXC_RtlUnwind( pEndFrame, unusedEip, pRecord, returnEax, &context );
404 #endif
407 /*******************************************************************
408 * NtRaiseException (NTDLL.@)
410 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
411 BOOL first, CONTEXT *context )
413 EXC_RtlRaiseException( rec, ctx );
414 *context = *ctx;
417 #ifdef DEFINE_REGS_ENTRYPOINT
418 DEFINE_REGS_ENTRYPOINT( NtRaiseException, EXC_NtRaiseException, 12, 12 );
419 #else
420 void WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first )
422 CONTEXT context;
423 memset( &context, 0, sizeof(context) );
424 EXC_NtRaiseException( rec, ctx, first, &context );
426 #endif
429 /***********************************************************************
430 * RtlRaiseStatus (NTDLL.@)
432 * Raise an exception with ExceptionCode = status
434 void WINAPI RtlRaiseStatus( NTSTATUS status )
436 EXCEPTION_RECORD ExceptionRec;
438 ExceptionRec.ExceptionCode = status;
439 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
440 ExceptionRec.ExceptionRecord = NULL;
441 ExceptionRec.NumberParameters = 0;
442 RtlRaiseException( &ExceptionRec );
446 /*******************************************************************
447 * RtlAddVectoredExceptionHandler (NTDLL.@)
449 PVOID WINAPI RtlAddVectoredExceptionHandler( ULONG first, PVECTORED_EXCEPTION_HANDLER func )
451 VECTORED_HANDLER *handler = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler) );
452 if (handler)
454 handler->func = func;
455 RtlEnterCriticalSection( &vectored_handlers_section );
456 if (first) list_add_head( &vectored_handlers, &handler->entry );
457 else list_add_tail( &vectored_handlers, &handler->entry );
458 RtlLeaveCriticalSection( &vectored_handlers_section );
460 return handler;
464 /*******************************************************************
465 * RtlRemoveVectoredExceptionHandler (NTDLL.@)
467 ULONG WINAPI RtlRemoveVectoredExceptionHandler( PVOID handler )
469 struct list *ptr;
470 ULONG ret = FALSE;
472 RtlEnterCriticalSection( &vectored_handlers_section );
473 LIST_FOR_EACH( ptr, &vectored_handlers )
475 if (ptr == &((VECTORED_HANDLER *)handler)->entry)
477 list_remove( ptr );
478 ret = TRUE;
479 break;
482 RtlLeaveCriticalSection( &vectored_handlers_section );
483 if (ret) RtlFreeHeap( GetProcessHeap(), 0, handler );
484 return ret;
488 /*************************************************************
489 * __wine_exception_handler (NTDLL.@)
491 * Exception handler for exception blocks declared in Wine code.
493 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
494 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
496 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
498 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
499 return ExceptionContinueSearch;
500 if (wine_frame->u.filter)
502 EXCEPTION_POINTERS ptrs;
503 ptrs.ExceptionRecord = record;
504 ptrs.ContextRecord = context;
505 switch(wine_frame->u.filter( &ptrs ))
507 case EXCEPTION_CONTINUE_SEARCH:
508 return ExceptionContinueSearch;
509 case EXCEPTION_CONTINUE_EXECUTION:
510 return ExceptionContinueExecution;
511 case EXCEPTION_EXECUTE_HANDLER:
512 break;
513 default:
514 MESSAGE( "Invalid return value from exception filter\n" );
515 assert( FALSE );
518 /* hack to make GetExceptionCode() work in handler */
519 wine_frame->ExceptionCode = record->ExceptionCode;
520 wine_frame->ExceptionRecord = wine_frame;
522 RtlUnwind( frame, 0, record, 0 );
523 __wine_pop_frame( frame );
524 siglongjmp( wine_frame->jmp, 1 );
528 /*************************************************************
529 * __wine_finally_handler (NTDLL.@)
531 * Exception handler for try/finally blocks declared in Wine code.
533 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
534 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
536 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
538 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
539 wine_frame->u.finally_func( FALSE );
541 return ExceptionContinueSearch;