2 * Sparc signal handling routines
4 * Copyright 1999 Ulrich Weigand
15 #include <sys/ucontext.h>
21 #include "wine/exception.h"
23 #include "stackframe.h"
25 #include "debugtools.h"
27 DEFAULT_DEBUG_CHANNEL(seh);
29 static sigset_t all_sigs;
32 * FIXME: All this works only on Solaris for now
35 /**********************************************************************
38 static void save_context( CONTEXT *context, ucontext_t *ucontext )
40 /* Special registers */
41 context->psr = ucontext->uc_mcontext.gregs[REG_PSR];
42 context->pc = ucontext->uc_mcontext.gregs[REG_PC];
43 context->npc = ucontext->uc_mcontext.gregs[REG_nPC];
44 context->y = ucontext->uc_mcontext.gregs[REG_Y];
45 context->wim = 0; /* FIXME */
46 context->tbr = 0; /* FIXME */
48 /* Global registers */
49 context->g0 = 0; /* always */
50 context->g1 = ucontext->uc_mcontext.gregs[REG_G1];
51 context->g2 = ucontext->uc_mcontext.gregs[REG_G2];
52 context->g3 = ucontext->uc_mcontext.gregs[REG_G3];
53 context->g4 = ucontext->uc_mcontext.gregs[REG_G4];
54 context->g5 = ucontext->uc_mcontext.gregs[REG_G5];
55 context->g6 = ucontext->uc_mcontext.gregs[REG_G6];
56 context->g7 = ucontext->uc_mcontext.gregs[REG_G7];
58 /* Current 'out' registers */
59 context->o0 = ucontext->uc_mcontext.gregs[REG_O0];
60 context->o1 = ucontext->uc_mcontext.gregs[REG_O1];
61 context->o2 = ucontext->uc_mcontext.gregs[REG_O2];
62 context->o3 = ucontext->uc_mcontext.gregs[REG_O3];
63 context->o4 = ucontext->uc_mcontext.gregs[REG_O4];
64 context->o5 = ucontext->uc_mcontext.gregs[REG_O5];
65 context->o6 = ucontext->uc_mcontext.gregs[REG_O6];
66 context->o7 = ucontext->uc_mcontext.gregs[REG_O7];
68 /* FIXME: what if the current register window isn't saved? */
69 if ( ucontext->uc_mcontext.gwins && ucontext->uc_mcontext.gwins->wbcnt > 0 )
71 /* Current 'local' registers from first register window */
72 context->l0 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[0];
73 context->l1 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[1];
74 context->l2 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[2];
75 context->l3 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[3];
76 context->l4 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[4];
77 context->l5 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[5];
78 context->l6 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[6];
79 context->l7 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[7];
81 /* Current 'in' registers from first register window */
82 context->i0 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[0];
83 context->i1 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[1];
84 context->i2 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[2];
85 context->i3 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[3];
86 context->i4 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[4];
87 context->i5 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[5];
88 context->i6 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[6];
89 context->i7 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[7];
93 /**********************************************************************
96 static void restore_context( CONTEXT *context, ucontext_t *ucontext )
101 /**********************************************************************
104 static void save_fpu( CONTEXT *context, ucontext_t *ucontext )
109 /**********************************************************************
112 static void restore_fpu( CONTEXT *context, ucontext_t *ucontext )
118 /**********************************************************************
121 * Handler for SIGSEGV.
123 static void segv_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
125 EXCEPTION_RECORD rec;
128 /* we want the page-fault case to be fast */
129 if ( info->si_code == SEGV_ACCERR )
130 if (VIRTUAL_HandleFault( (LPVOID)info->si_addr )) return;
132 save_context( &context, ucontext );
133 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
134 rec.ExceptionRecord = NULL;
135 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
136 rec.ExceptionAddress = (LPVOID)context.pc;
137 rec.NumberParameters = 2;
138 rec.ExceptionInformation[0] = 0; /* FIXME: read/write access ? */
139 rec.ExceptionInformation[1] = (DWORD)info->si_addr;
141 EXC_RtlRaiseException( &rec, &context );
142 restore_context( &context, ucontext );
145 /**********************************************************************
148 * Handler for SIGBUS.
150 static void bus_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
152 EXCEPTION_RECORD rec;
155 save_context( &context, ucontext );
156 rec.ExceptionRecord = NULL;
157 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
158 rec.ExceptionAddress = (LPVOID)context.pc;
159 rec.NumberParameters = 0;
161 if ( info->si_code == BUS_ADRALN )
162 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
164 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
166 EXC_RtlRaiseException( &rec, &context );
167 restore_context( &context, ucontext );
170 /**********************************************************************
173 * Handler for SIGILL.
175 static void ill_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
177 EXCEPTION_RECORD rec;
180 switch ( info->si_code )
187 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
192 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
196 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
200 save_context( &context, ucontext );
201 rec.ExceptionRecord = NULL;
202 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
203 rec.ExceptionAddress = (LPVOID)context.pc;
204 rec.NumberParameters = 0;
205 EXC_RtlRaiseException( &rec, &context );
206 restore_context( &context, ucontext );
210 /**********************************************************************
213 * Handler for SIGTRAP.
215 static void trap_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
217 EXCEPTION_RECORD rec;
220 switch ( info->si_code )
223 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
227 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
231 save_context( &context, ucontext );
232 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
233 rec.ExceptionRecord = NULL;
234 rec.ExceptionAddress = (LPVOID)context.pc;
235 rec.NumberParameters = 0;
236 EXC_RtlRaiseException( &rec, &context );
237 restore_context( &context, ucontext );
241 /**********************************************************************
244 * Handler for SIGFPE.
246 static void fpe_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
248 EXCEPTION_RECORD rec;
251 switch ( info->si_code )
254 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
257 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
260 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
263 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
266 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
269 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
272 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
276 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
280 save_context( &context, ucontext );
281 save_fpu( &context, ucontext );
282 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
283 rec.ExceptionRecord = NULL;
284 rec.ExceptionAddress = (LPVOID)context.pc;
285 rec.NumberParameters = 0;
286 EXC_RtlRaiseException( &rec, &context );
287 restore_context( &context, ucontext );
288 restore_fpu( &context, ucontext );
292 /**********************************************************************
295 * Handler for SIGINT.
297 static void int_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
299 EXCEPTION_RECORD rec;
302 save_context( &context, ucontext );
303 rec.ExceptionCode = CONTROL_C_EXIT;
304 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
305 rec.ExceptionRecord = NULL;
306 rec.ExceptionAddress = (LPVOID)context.pc;
307 rec.NumberParameters = 0;
308 EXC_RtlRaiseException( &rec, &context );
309 restore_context( &context, ucontext );
313 /***********************************************************************
316 * Set a signal handler
318 static int set_handler( int sig, void (*func)() )
320 struct sigaction sig_act;
322 sig_act.sa_handler = NULL;
323 sig_act.sa_sigaction = func;
324 sigemptyset( &sig_act.sa_mask );
325 sig_act.sa_flags = SA_SIGINFO;
327 return sigaction( sig, &sig_act, NULL );
331 /***********************************************************************
334 * Unblock signals. Called from EXC_RtlRaiseException.
336 void SIGNAL_Unblock( void )
338 sigprocmask( SIG_UNBLOCK, &all_sigs, NULL );
342 /**********************************************************************
345 BOOL SIGNAL_Init(void)
347 /* ignore SIGPIPE so that WINSOCK can get a EPIPE error instead */
348 signal( SIGPIPE, SIG_IGN );
349 /* automatic child reaping to avoid zombies */
350 signal( SIGCHLD, SIG_IGN );
352 sigfillset( &all_sigs );
354 if (set_handler( SIGINT, (void (*)())int_handler ) == -1) goto error;
355 if (set_handler( SIGFPE, (void (*)())fpe_handler ) == -1) goto error;
356 if (set_handler( SIGSEGV, (void (*)())segv_handler ) == -1) goto error;
357 if (set_handler( SIGILL, (void (*)())ill_handler ) == -1) goto error;
358 if (set_handler( SIGBUS, (void (*)())bus_handler ) == -1) goto error;
359 if (set_handler( SIGTRAP, (void (*)())trap_handler ) == -1) goto error;
367 /**********************************************************************
370 void __wine_enter_vm86( CONTEXT *context )
372 MESSAGE("vm86 mode not supported on this platform\n");
375 /**********************************************************************
376 * DbgBreakPoint (NTDLL.@)
378 void WINAPI DbgBreakPoint(void)
383 /**********************************************************************
384 * DbgUserBreakPoint (NTDLL.@)
386 void WINAPI DbgUserBreakPoint(void)
391 #endif /* __sparc__ */