Fixed some issues found by winapi_check.
[wine/multimedia.git] / dlls / ntdll / signal_sparc.c
blobdaf94faf672dcfe7833bb8947aff23ff5ecfb3d2
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 #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 /***********************************************************************
56 * dispatch_signal
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 /**********************************************************************
70 * save_context
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 /**********************************************************************
128 * restore_context
130 static void restore_context( CONTEXT *context, ucontext_t *ucontext )
132 /* FIXME */
135 /**********************************************************************
136 * save_fpu
138 static void save_fpu( CONTEXT *context, ucontext_t *ucontext )
140 /* FIXME */
143 /**********************************************************************
144 * restore_fpu
146 static void restore_fpu( CONTEXT *context, ucontext_t *ucontext )
148 /* FIXME */
152 /**********************************************************************
153 * segv_handler
155 * Handler for SIGSEGV.
157 static void segv_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
159 EXCEPTION_RECORD rec;
160 CONTEXT context;
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 /**********************************************************************
180 * bus_handler
182 * Handler for SIGBUS.
184 static void bus_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
186 EXCEPTION_RECORD rec;
187 CONTEXT context;
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;
197 else
198 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
200 EXC_RtlRaiseException( &rec, &context );
201 restore_context( &context, ucontext );
204 /**********************************************************************
205 * ill_handler
207 * Handler for SIGILL.
209 static void ill_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
211 EXCEPTION_RECORD rec;
212 CONTEXT context;
214 switch ( info->si_code )
216 default:
217 case ILL_ILLOPC:
218 case ILL_ILLOPN:
219 case ILL_ILLADR:
220 case ILL_ILLTRP:
221 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
222 break;
224 case ILL_PRVOPC:
225 case ILL_PRVREG:
226 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
227 break;
229 case ILL_BADSTK:
230 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
231 break;
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 /**********************************************************************
245 * trap_handler
247 * Handler for SIGTRAP.
249 static void trap_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
251 EXCEPTION_RECORD rec;
252 CONTEXT context;
254 switch ( info->si_code )
256 case TRAP_TRACE:
257 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
258 break;
259 case TRAP_BRKPT:
260 default:
261 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
262 break;
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 /**********************************************************************
276 * fpe_handler
278 * Handler for SIGFPE.
280 static void fpe_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
282 EXCEPTION_RECORD rec;
283 CONTEXT context;
285 switch ( info->si_code )
287 case FPE_FLTSUB:
288 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
289 break;
290 case FPE_INTDIV:
291 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
292 break;
293 case FPE_INTOVF:
294 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
295 break;
296 case FPE_FLTDIV:
297 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
298 break;
299 case FPE_FLTOVF:
300 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
301 break;
302 case FPE_FLTUND:
303 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
304 break;
305 case FPE_FLTRES:
306 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
307 break;
308 case FPE_FLTINV:
309 default:
310 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
311 break;
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 /**********************************************************************
327 * int_handler
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;
336 CONTEXT context;
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 /**********************************************************************
350 * abrt_handler
352 * Handler for SIGABRT.
354 static HANDLER_DEF(abrt_handler)
356 EXCEPTION_RECORD rec;
357 CONTEXT context;
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 /***********************************************************************
370 * set_handler
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;
394 handlers[sig] = wsh;
395 return 0;
399 /***********************************************************************
400 * SIGNAL_Unblock
402 * Unblock signals. Called from EXC_RtlRaiseException.
404 void SIGNAL_Unblock( void )
406 sigprocmask( SIG_UNBLOCK, &all_sigs, NULL );
410 /**********************************************************************
411 * SIGNAL_Init
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;
424 return TRUE;
426 error:
427 perror("sigaction");
428 return FALSE;
432 /**********************************************************************
433 * SIGNAL_Reset
435 void SIGNAL_Reset(void)
437 sigset_t block_set;
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 /**********************************************************************
458 * __wine_enter_vm86
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__ */