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
32 #include <sys/ucontext.h>
38 #include "wine/exception.h"
39 #include "ntdll_misc.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 /***********************************************************************
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 /**********************************************************************
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 /**********************************************************************
127 static void restore_context( CONTEXT *context, ucontext_t *ucontext )
132 /**********************************************************************
135 static void save_fpu( CONTEXT *context, ucontext_t *ucontext )
140 /**********************************************************************
143 static void restore_fpu( CONTEXT *context, ucontext_t *ucontext )
149 /**********************************************************************
152 * Handler for SIGSEGV.
154 static void segv_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
156 EXCEPTION_RECORD rec;
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] = (ULONG_PTR)info->si_addr;
172 __regs_RtlRaiseException( &rec, &context );
173 restore_context( &context, ucontext );
176 /**********************************************************************
179 * Handler for SIGBUS.
181 static void bus_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
183 EXCEPTION_RECORD rec;
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;
195 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
197 __regs_RtlRaiseException( &rec, &context );
198 restore_context( &context, ucontext );
201 /**********************************************************************
204 * Handler for SIGILL.
206 static void ill_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
208 EXCEPTION_RECORD rec;
211 switch ( info->si_code )
218 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
223 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
227 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
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 __regs_RtlRaiseException( &rec, &context );
237 restore_context( &context, ucontext );
241 /**********************************************************************
244 * Handler for SIGTRAP.
246 static void trap_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
248 EXCEPTION_RECORD rec;
251 switch ( info->si_code )
254 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
258 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
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 __regs_RtlRaiseException( &rec, &context );
268 restore_context( &context, ucontext );
272 /**********************************************************************
275 * Handler for SIGFPE.
277 static void fpe_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
279 EXCEPTION_RECORD rec;
282 switch ( info->si_code )
285 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
288 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
291 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
294 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
297 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
300 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
303 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
307 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
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 __regs_RtlRaiseException( &rec, &context );
318 restore_context( &context, ucontext );
319 restore_fpu( &context, ucontext );
323 /**********************************************************************
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;
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 __regs_RtlRaiseException( &rec, &context );
342 restore_context( &context, ucontext );
346 /**********************************************************************
349 * Handler for SIGABRT.
351 static HANDLER_DEF(abrt_handler)
353 EXCEPTION_RECORD rec;
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.pc;
361 rec.NumberParameters = 0;
362 __regs_RtlRaiseException( &rec, &context ); /* Should never return.. */
363 restore_context( &context, HANDLER_CONTEXT );
367 /**********************************************************************
370 * Handler for SIGTERM.
372 static HANDLER_DEF(term_handler)
374 server_abort_thread(0);
378 /**********************************************************************
381 * Handler for SIGUSR1, used to signal a thread that it got suspended.
383 static HANDLER_DEF(usr1_handler)
385 LARGE_INTEGER timeout;
387 /* wait with 0 timeout, will only return once the thread is no longer suspended */
388 timeout.QuadPart = 0;
389 NTDLL_wait_for_multiple_objects( 0, NULL, 0, &timeout );
393 /***********************************************************************
396 * Set a signal handler
398 static int set_handler( int sig, void (*func)() )
400 struct sigaction sig_act;
402 sig_act.sa_handler = NULL;
403 sig_act.sa_sigaction = func;
404 sigemptyset( &sig_act.sa_mask );
405 sig_act.sa_flags = SA_SIGINFO;
407 return sigaction( sig, &sig_act, NULL );
411 /***********************************************************************
412 * __wine_set_signal_handler (NTDLL.@)
414 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
416 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
417 if (handlers[sig] != NULL) return -2;
423 /**********************************************************************
426 BOOL SIGNAL_Init(void)
428 if (set_handler( SIGINT, (void (*)())int_handler ) == -1) goto error;
429 if (set_handler( SIGFPE, (void (*)())fpe_handler ) == -1) goto error;
430 if (set_handler( SIGSEGV, (void (*)())segv_handler ) == -1) goto error;
431 if (set_handler( SIGILL, (void (*)())ill_handler ) == -1) goto error;
432 if (set_handler( SIGBUS, (void (*)())bus_handler ) == -1) goto error;
433 if (set_handler( SIGTRAP, (void (*)())trap_handler ) == -1) goto error;
434 if (set_handler( SIGABRT, (void (*)())abrt_handler ) == -1) goto error;
435 if (set_handler( SIGTERM, (void (*)())term_handler ) == -1) goto error;
436 if (set_handler( SIGUSR1, (void (*)())usr1_handler ) == -1) goto error;
437 /* 'ta 6' tells the kernel to synthesize any unaligned accesses this
438 process makes, instead of just signalling an error and terminating
439 the process. wine-devel did not reach a conclusion on whether
440 this is correct, because that is what x86 does, or it is harmful
441 because it could obscure problems in user code */
442 asm("ta 6"); /* 6 == ST_FIX_ALIGN defined in sys/trap.h */
451 /**********************************************************************
454 void __wine_enter_vm86( CONTEXT *context )
456 MESSAGE("vm86 mode not supported on this platform\n");
459 /**********************************************************************
460 * DbgBreakPoint (NTDLL.@)
462 void WINAPI DbgBreakPoint(void)
464 kill(getpid(), SIGTRAP);
467 /**********************************************************************
468 * DbgUserBreakPoint (NTDLL.@)
470 void WINAPI DbgUserBreakPoint(void)
472 kill(getpid(), SIGTRAP);
475 #endif /* __sparc__ */