Raise an exception if any Wine or Winelib code does an assert.
[wine/multimedia.git] / dlls / ntdll / signal_sparc.c
blob5d8d211ead0e5ddf4e962fb943aa397e94879a4d
1 /*
2 * Sparc signal handling routines
4 * Copyright 1999 Ulrich Weigand
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifdef __sparc__
23 #include "config.h"
25 #include <signal.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <stdio.h>
31 #include <sys/ucontext.h>
33 #include "winternl.h"
34 #include "winbase.h"
35 #include "winnt.h"
37 #include "wine/exception.h"
38 #include "global.h"
39 #include "stackframe.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(seh);
45 typedef int (*wine_signal_handler)(unsigned int sig);
47 static wine_signal_handler handlers[256];
49 static sigset_t all_sigs;
52 /***********************************************************************
53 * dispatch_signal
55 inline static int dispatch_signal(unsigned int sig)
57 if (handlers[sig] == NULL) return 0;
58 return handlers[sig](sig);
63 * FIXME: All this works only on Solaris for now
66 /**********************************************************************
67 * save_context
69 static void save_context( CONTEXT *context, ucontext_t *ucontext )
71 /* Special registers */
72 context->psr = ucontext->uc_mcontext.gregs[REG_PSR];
73 context->pc = ucontext->uc_mcontext.gregs[REG_PC];
74 context->npc = ucontext->uc_mcontext.gregs[REG_nPC];
75 context->y = ucontext->uc_mcontext.gregs[REG_Y];
76 context->wim = 0; /* FIXME */
77 context->tbr = 0; /* FIXME */
79 /* Global registers */
80 context->g0 = 0; /* always */
81 context->g1 = ucontext->uc_mcontext.gregs[REG_G1];
82 context->g2 = ucontext->uc_mcontext.gregs[REG_G2];
83 context->g3 = ucontext->uc_mcontext.gregs[REG_G3];
84 context->g4 = ucontext->uc_mcontext.gregs[REG_G4];
85 context->g5 = ucontext->uc_mcontext.gregs[REG_G5];
86 context->g6 = ucontext->uc_mcontext.gregs[REG_G6];
87 context->g7 = ucontext->uc_mcontext.gregs[REG_G7];
89 /* Current 'out' registers */
90 context->o0 = ucontext->uc_mcontext.gregs[REG_O0];
91 context->o1 = ucontext->uc_mcontext.gregs[REG_O1];
92 context->o2 = ucontext->uc_mcontext.gregs[REG_O2];
93 context->o3 = ucontext->uc_mcontext.gregs[REG_O3];
94 context->o4 = ucontext->uc_mcontext.gregs[REG_O4];
95 context->o5 = ucontext->uc_mcontext.gregs[REG_O5];
96 context->o6 = ucontext->uc_mcontext.gregs[REG_O6];
97 context->o7 = ucontext->uc_mcontext.gregs[REG_O7];
99 /* FIXME: what if the current register window isn't saved? */
100 if ( ucontext->uc_mcontext.gwins && ucontext->uc_mcontext.gwins->wbcnt > 0 )
102 /* Current 'local' registers from first register window */
103 context->l0 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[0];
104 context->l1 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[1];
105 context->l2 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[2];
106 context->l3 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[3];
107 context->l4 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[4];
108 context->l5 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[5];
109 context->l6 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[6];
110 context->l7 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[7];
112 /* Current 'in' registers from first register window */
113 context->i0 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[0];
114 context->i1 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[1];
115 context->i2 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[2];
116 context->i3 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[3];
117 context->i4 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[4];
118 context->i5 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[5];
119 context->i6 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[6];
120 context->i7 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[7];
124 /**********************************************************************
125 * restore_context
127 static void restore_context( CONTEXT *context, ucontext_t *ucontext )
129 /* FIXME */
132 /**********************************************************************
133 * save_fpu
135 static void save_fpu( CONTEXT *context, ucontext_t *ucontext )
137 /* FIXME */
140 /**********************************************************************
141 * restore_fpu
143 static void restore_fpu( CONTEXT *context, ucontext_t *ucontext )
145 /* FIXME */
149 /**********************************************************************
150 * segv_handler
152 * Handler for SIGSEGV.
154 static void segv_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
156 EXCEPTION_RECORD rec;
157 CONTEXT context;
159 /* we want the page-fault case to be fast */
160 if ( info->si_code == SEGV_ACCERR )
161 if (VIRTUAL_HandleFault( (LPVOID)info->si_addr )) return;
163 save_context( &context, ucontext );
164 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
165 rec.ExceptionRecord = NULL;
166 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
167 rec.ExceptionAddress = (LPVOID)context.pc;
168 rec.NumberParameters = 2;
169 rec.ExceptionInformation[0] = 0; /* FIXME: read/write access ? */
170 rec.ExceptionInformation[1] = (DWORD)info->si_addr;
172 EXC_RtlRaiseException( &rec, &context );
173 restore_context( &context, ucontext );
176 /**********************************************************************
177 * bus_handler
179 * Handler for SIGBUS.
181 static void bus_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
183 EXCEPTION_RECORD rec;
184 CONTEXT context;
186 save_context( &context, ucontext );
187 rec.ExceptionRecord = NULL;
188 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
189 rec.ExceptionAddress = (LPVOID)context.pc;
190 rec.NumberParameters = 0;
192 if ( info->si_code == BUS_ADRALN )
193 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
194 else
195 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
197 EXC_RtlRaiseException( &rec, &context );
198 restore_context( &context, ucontext );
201 /**********************************************************************
202 * ill_handler
204 * Handler for SIGILL.
206 static void ill_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
208 EXCEPTION_RECORD rec;
209 CONTEXT context;
211 switch ( info->si_code )
213 default:
214 case ILL_ILLOPC:
215 case ILL_ILLOPN:
216 case ILL_ILLADR:
217 case ILL_ILLTRP:
218 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
219 break;
221 case ILL_PRVOPC:
222 case ILL_PRVREG:
223 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
224 break;
226 case ILL_BADSTK:
227 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
228 break;
231 save_context( &context, ucontext );
232 rec.ExceptionRecord = NULL;
233 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
234 rec.ExceptionAddress = (LPVOID)context.pc;
235 rec.NumberParameters = 0;
236 EXC_RtlRaiseException( &rec, &context );
237 restore_context( &context, ucontext );
241 /**********************************************************************
242 * trap_handler
244 * Handler for SIGTRAP.
246 static void trap_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
248 EXCEPTION_RECORD rec;
249 CONTEXT context;
251 switch ( info->si_code )
253 case TRAP_TRACE:
254 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
255 break;
256 case TRAP_BRKPT:
257 default:
258 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
259 break;
262 save_context( &context, ucontext );
263 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
264 rec.ExceptionRecord = NULL;
265 rec.ExceptionAddress = (LPVOID)context.pc;
266 rec.NumberParameters = 0;
267 EXC_RtlRaiseException( &rec, &context );
268 restore_context( &context, ucontext );
272 /**********************************************************************
273 * fpe_handler
275 * Handler for SIGFPE.
277 static void fpe_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
279 EXCEPTION_RECORD rec;
280 CONTEXT context;
282 switch ( info->si_code )
284 case FPE_FLTSUB:
285 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
286 break;
287 case FPE_INTDIV:
288 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
289 break;
290 case FPE_INTOVF:
291 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
292 break;
293 case FPE_FLTDIV:
294 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
295 break;
296 case FPE_FLTOVF:
297 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
298 break;
299 case FPE_FLTUND:
300 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
301 break;
302 case FPE_FLTRES:
303 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
304 break;
305 case FPE_FLTINV:
306 default:
307 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
308 break;
311 save_context( &context, ucontext );
312 save_fpu( &context, ucontext );
313 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
314 rec.ExceptionRecord = NULL;
315 rec.ExceptionAddress = (LPVOID)context.pc;
316 rec.NumberParameters = 0;
317 EXC_RtlRaiseException( &rec, &context );
318 restore_context( &context, ucontext );
319 restore_fpu( &context, ucontext );
323 /**********************************************************************
324 * int_handler
326 * Handler for SIGINT.
328 static void int_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
330 if (!dispatch_signal(SIGINT))
332 EXCEPTION_RECORD rec;
333 CONTEXT context;
335 save_context( &context, ucontext );
336 rec.ExceptionCode = CONTROL_C_EXIT;
337 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
338 rec.ExceptionRecord = NULL;
339 rec.ExceptionAddress = (LPVOID)context.pc;
340 rec.NumberParameters = 0;
341 EXC_RtlRaiseException( &rec, &context );
342 restore_context( &context, ucontext );
346 /**********************************************************************
347 * abrt_handler
349 * Handler for SIGABRT.
351 static HANDLER_DEF(abrt_handler)
353 EXCEPTION_RECORD rec;
354 CONTEXT context;
356 save_context( &context, HANDLER_CONTEXT );
357 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
358 rec.ExceptionFlags = EH_NONCONTINUABLE;
359 rec.ExceptionRecord = NULL;
360 rec.ExceptionAddress = (LPVOID)context.Eip;
361 rec.NumberParameters = 0;
362 EXC_RtlRaiseException( &rec, &context ); /* Should never return.. */
363 restore_context( &context, HANDLER_CONTEXT );
366 /***********************************************************************
367 * set_handler
369 * Set a signal handler
371 static int set_handler( int sig, void (*func)() )
373 struct sigaction sig_act;
375 sig_act.sa_handler = NULL;
376 sig_act.sa_sigaction = func;
377 sigemptyset( &sig_act.sa_mask );
378 sig_act.sa_flags = SA_SIGINFO;
380 return sigaction( sig, &sig_act, NULL );
384 /***********************************************************************
385 * __wine_set_signal_handler (NTDLL.@)
387 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
389 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
390 if (handlers[sig] != NULL) return -2;
391 handlers[sig] = wsh;
392 return 0;
396 /***********************************************************************
397 * SIGNAL_Unblock
399 * Unblock signals. Called from EXC_RtlRaiseException.
401 void SIGNAL_Unblock( void )
403 sigprocmask( SIG_UNBLOCK, &all_sigs, NULL );
407 /**********************************************************************
408 * SIGNAL_Init
410 BOOL SIGNAL_Init(void)
412 sigfillset( &all_sigs );
414 if (set_handler( SIGINT, (void (*)())int_handler ) == -1) goto error;
415 if (set_handler( SIGFPE, (void (*)())fpe_handler ) == -1) goto error;
416 if (set_handler( SIGSEGV, (void (*)())segv_handler ) == -1) goto error;
417 if (set_handler( SIGILL, (void (*)())ill_handler ) == -1) goto error;
418 if (set_handler( SIGBUS, (void (*)())bus_handler ) == -1) goto error;
419 if (set_handler( SIGTRAP, (void (*)())trap_handler ) == -1) goto error;
420 if (set_handler( SIGABRT, (void (*)())abrt_handler ) == -1) goto error;
421 return TRUE;
423 error:
424 perror("sigaction");
425 return FALSE;
429 /**********************************************************************
430 * SIGNAL_Reset
432 void SIGNAL_Reset(void)
434 sigset_t block_set;
436 /* block the async signals */
437 sigemptyset( &block_set );
438 sigaddset( &block_set, SIGALRM );
439 sigaddset( &block_set, SIGIO );
440 sigaddset( &block_set, SIGHUP );
441 sigaddset( &block_set, SIGUSR2 );
442 sigprocmask( SIG_BLOCK, &block_set, NULL );
444 /* restore default handlers */
445 signal( SIGINT, SIG_DFL );
446 signal( SIGFPE, SIG_DFL );
447 signal( SIGSEGV, SIG_DFL );
448 signal( SIGILL, SIG_DFL );
449 signal( SIGBUS, SIG_DFL );
450 signal( SIGTRAP, SIG_DFL );
454 /**********************************************************************
455 * __wine_enter_vm86
457 void __wine_enter_vm86( CONTEXT *context )
459 MESSAGE("vm86 mode not supported on this platform\n");
462 /**********************************************************************
463 * DbgBreakPoint (NTDLL.@)
465 void WINAPI DbgBreakPoint(void)
467 kill(getpid(), SIGTRAP);
470 /**********************************************************************
471 * DbgUserBreakPoint (NTDLL.@)
473 void WINAPI DbgUserBreakPoint(void)
475 kill(getpid(), SIGTRAP);
478 #endif /* __sparc__ */