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
31 #include <sys/ucontext.h>
37 #include "wine/exception.h"
39 #include "stackframe.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(seh);
45 #define HANDLER_DEF(name) void name( int __signal, struct siginfo *__siginfo, ucontext_t *__context )
46 #define HANDLER_CONTEXT (__context)
48 typedef int (*wine_signal_handler)(unsigned int sig);
50 static wine_signal_handler handlers[256];
52 static sigset_t all_sigs;
55 /***********************************************************************
58 inline static int dispatch_signal(unsigned int sig)
60 if (handlers[sig] == NULL) return 0;
61 return handlers[sig](sig);
66 * FIXME: All this works only on Solaris for now
69 /**********************************************************************
72 static void save_context( CONTEXT *context, ucontext_t *ucontext )
74 /* Special registers */
75 context->psr = ucontext->uc_mcontext.gregs[REG_PSR];
76 context->pc = ucontext->uc_mcontext.gregs[REG_PC];
77 context->npc = ucontext->uc_mcontext.gregs[REG_nPC];
78 context->y = ucontext->uc_mcontext.gregs[REG_Y];
79 context->wim = 0; /* FIXME */
80 context->tbr = 0; /* FIXME */
82 /* Global registers */
83 context->g0 = 0; /* always */
84 context->g1 = ucontext->uc_mcontext.gregs[REG_G1];
85 context->g2 = ucontext->uc_mcontext.gregs[REG_G2];
86 context->g3 = ucontext->uc_mcontext.gregs[REG_G3];
87 context->g4 = ucontext->uc_mcontext.gregs[REG_G4];
88 context->g5 = ucontext->uc_mcontext.gregs[REG_G5];
89 context->g6 = ucontext->uc_mcontext.gregs[REG_G6];
90 context->g7 = ucontext->uc_mcontext.gregs[REG_G7];
92 /* Current 'out' registers */
93 context->o0 = ucontext->uc_mcontext.gregs[REG_O0];
94 context->o1 = ucontext->uc_mcontext.gregs[REG_O1];
95 context->o2 = ucontext->uc_mcontext.gregs[REG_O2];
96 context->o3 = ucontext->uc_mcontext.gregs[REG_O3];
97 context->o4 = ucontext->uc_mcontext.gregs[REG_O4];
98 context->o5 = ucontext->uc_mcontext.gregs[REG_O5];
99 context->o6 = ucontext->uc_mcontext.gregs[REG_O6];
100 context->o7 = ucontext->uc_mcontext.gregs[REG_O7];
102 /* FIXME: what if the current register window isn't saved? */
103 if ( ucontext->uc_mcontext.gwins && ucontext->uc_mcontext.gwins->wbcnt > 0 )
105 /* Current 'local' registers from first register window */
106 context->l0 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[0];
107 context->l1 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[1];
108 context->l2 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[2];
109 context->l3 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[3];
110 context->l4 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[4];
111 context->l5 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[5];
112 context->l6 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[6];
113 context->l7 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[7];
115 /* Current 'in' registers from first register window */
116 context->i0 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[0];
117 context->i1 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[1];
118 context->i2 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[2];
119 context->i3 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[3];
120 context->i4 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[4];
121 context->i5 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[5];
122 context->i6 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[6];
123 context->i7 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[7];
127 /**********************************************************************
130 static void restore_context( CONTEXT *context, ucontext_t *ucontext )
135 /**********************************************************************
138 static void save_fpu( CONTEXT *context, ucontext_t *ucontext )
143 /**********************************************************************
146 static void restore_fpu( CONTEXT *context, ucontext_t *ucontext )
152 /**********************************************************************
155 * Handler for SIGSEGV.
157 static void segv_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
159 EXCEPTION_RECORD rec;
162 /* we want the page-fault case to be fast */
163 if ( info->si_code == SEGV_ACCERR )
164 if (VIRTUAL_HandleFault( (LPVOID)info->si_addr )) return;
166 save_context( &context, ucontext );
167 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
168 rec.ExceptionRecord = NULL;
169 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
170 rec.ExceptionAddress = (LPVOID)context.pc;
171 rec.NumberParameters = 2;
172 rec.ExceptionInformation[0] = 0; /* FIXME: read/write access ? */
173 rec.ExceptionInformation[1] = (DWORD)info->si_addr;
175 EXC_RtlRaiseException( &rec, &context );
176 restore_context( &context, ucontext );
179 /**********************************************************************
182 * Handler for SIGBUS.
184 static void bus_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
186 EXCEPTION_RECORD rec;
189 save_context( &context, ucontext );
190 rec.ExceptionRecord = NULL;
191 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
192 rec.ExceptionAddress = (LPVOID)context.pc;
193 rec.NumberParameters = 0;
195 if ( info->si_code == BUS_ADRALN )
196 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
198 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
200 EXC_RtlRaiseException( &rec, &context );
201 restore_context( &context, ucontext );
204 /**********************************************************************
207 * Handler for SIGILL.
209 static void ill_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
211 EXCEPTION_RECORD rec;
214 switch ( info->si_code )
221 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
226 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
230 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
234 save_context( &context, ucontext );
235 rec.ExceptionRecord = NULL;
236 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
237 rec.ExceptionAddress = (LPVOID)context.pc;
238 rec.NumberParameters = 0;
239 EXC_RtlRaiseException( &rec, &context );
240 restore_context( &context, ucontext );
244 /**********************************************************************
247 * Handler for SIGTRAP.
249 static void trap_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
251 EXCEPTION_RECORD rec;
254 switch ( info->si_code )
257 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
261 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
265 save_context( &context, ucontext );
266 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
267 rec.ExceptionRecord = NULL;
268 rec.ExceptionAddress = (LPVOID)context.pc;
269 rec.NumberParameters = 0;
270 EXC_RtlRaiseException( &rec, &context );
271 restore_context( &context, ucontext );
275 /**********************************************************************
278 * Handler for SIGFPE.
280 static void fpe_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
282 EXCEPTION_RECORD rec;
285 switch ( info->si_code )
288 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
291 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
294 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
297 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
300 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
303 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
306 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
310 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
314 save_context( &context, ucontext );
315 save_fpu( &context, ucontext );
316 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
317 rec.ExceptionRecord = NULL;
318 rec.ExceptionAddress = (LPVOID)context.pc;
319 rec.NumberParameters = 0;
320 EXC_RtlRaiseException( &rec, &context );
321 restore_context( &context, ucontext );
322 restore_fpu( &context, ucontext );
326 /**********************************************************************
329 * Handler for SIGINT.
331 static void int_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
333 if (!dispatch_signal(SIGINT))
335 EXCEPTION_RECORD rec;
338 save_context( &context, ucontext );
339 rec.ExceptionCode = CONTROL_C_EXIT;
340 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
341 rec.ExceptionRecord = NULL;
342 rec.ExceptionAddress = (LPVOID)context.pc;
343 rec.NumberParameters = 0;
344 EXC_RtlRaiseException( &rec, &context );
345 restore_context( &context, ucontext );
349 /**********************************************************************
352 * Handler for SIGABRT.
354 static HANDLER_DEF(abrt_handler)
356 EXCEPTION_RECORD rec;
359 save_context( &context, HANDLER_CONTEXT );
360 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
361 rec.ExceptionFlags = EH_NONCONTINUABLE;
362 rec.ExceptionRecord = NULL;
363 rec.ExceptionAddress = (LPVOID)context.pc;
364 rec.NumberParameters = 0;
365 EXC_RtlRaiseException( &rec, &context ); /* Should never return.. */
366 restore_context( &context, HANDLER_CONTEXT );
369 /***********************************************************************
372 * Set a signal handler
374 static int set_handler( int sig, void (*func)() )
376 struct sigaction sig_act;
378 sig_act.sa_handler = NULL;
379 sig_act.sa_sigaction = func;
380 sigemptyset( &sig_act.sa_mask );
381 sig_act.sa_flags = SA_SIGINFO;
383 return sigaction( sig, &sig_act, NULL );
387 /***********************************************************************
388 * __wine_set_signal_handler (NTDLL.@)
390 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
392 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
393 if (handlers[sig] != NULL) return -2;
399 /***********************************************************************
402 * Unblock signals. Called from EXC_RtlRaiseException.
404 void SIGNAL_Unblock( void )
406 sigprocmask( SIG_UNBLOCK, &all_sigs, NULL );
410 /**********************************************************************
413 BOOL SIGNAL_Init(void)
415 sigfillset( &all_sigs );
417 if (set_handler( SIGINT, (void (*)())int_handler ) == -1) goto error;
418 if (set_handler( SIGFPE, (void (*)())fpe_handler ) == -1) goto error;
419 if (set_handler( SIGSEGV, (void (*)())segv_handler ) == -1) goto error;
420 if (set_handler( SIGILL, (void (*)())ill_handler ) == -1) goto error;
421 if (set_handler( SIGBUS, (void (*)())bus_handler ) == -1) goto error;
422 if (set_handler( SIGTRAP, (void (*)())trap_handler ) == -1) goto error;
423 if (set_handler( SIGABRT, (void (*)())abrt_handler ) == -1) goto error;
432 /**********************************************************************
435 void SIGNAL_Reset(void)
439 /* block the async signals */
440 sigemptyset( &block_set );
441 sigaddset( &block_set, SIGALRM );
442 sigaddset( &block_set, SIGIO );
443 sigaddset( &block_set, SIGHUP );
444 sigaddset( &block_set, SIGUSR2 );
445 sigprocmask( SIG_BLOCK, &block_set, NULL );
447 /* restore default handlers */
448 signal( SIGINT, SIG_DFL );
449 signal( SIGFPE, SIG_DFL );
450 signal( SIGSEGV, SIG_DFL );
451 signal( SIGILL, SIG_DFL );
452 signal( SIGBUS, SIG_DFL );
453 signal( SIGTRAP, SIG_DFL );
457 /**********************************************************************
460 void __wine_enter_vm86( CONTEXT *context )
462 MESSAGE("vm86 mode not supported on this platform\n");
465 /**********************************************************************
466 * DbgBreakPoint (NTDLL.@)
468 void WINAPI DbgBreakPoint(void)
470 kill(getpid(), SIGTRAP);
473 /**********************************************************************
474 * DbgUserBreakPoint (NTDLL.@)
476 void WINAPI DbgUserBreakPoint(void)
478 kill(getpid(), SIGTRAP);
481 #endif /* __sparc__ */