Clean up devenum and properly implement DllCanUnloadNow ref counting.
[wine/multimedia.git] / dlls / ntdll / signal_sparc.c
blob36b42b9820f8f06c510042cb837ed910205fa144
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 <stdarg.h>
31 #include <stdio.h>
32 #include <sys/ucontext.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winreg.h"
37 #include "winternl.h"
38 #include "winnt.h"
40 #include "wine/exception.h"
41 #include "ntdll_misc.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(seh);
47 #define HANDLER_DEF(name) void name( int __signal, struct siginfo *__siginfo, ucontext_t *__context )
48 #define HANDLER_CONTEXT (__context)
50 typedef int (*wine_signal_handler)(unsigned int sig);
52 static wine_signal_handler handlers[256];
54 extern void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
56 /***********************************************************************
57 * dispatch_signal
59 inline static int dispatch_signal(unsigned int sig)
61 if (handlers[sig] == NULL) return 0;
62 return handlers[sig](sig);
67 * FIXME: All this works only on Solaris for now
70 /**********************************************************************
71 * save_context
73 static void save_context( CONTEXT *context, ucontext_t *ucontext )
75 /* Special registers */
76 context->psr = ucontext->uc_mcontext.gregs[REG_PSR];
77 context->pc = ucontext->uc_mcontext.gregs[REG_PC];
78 context->npc = ucontext->uc_mcontext.gregs[REG_nPC];
79 context->y = ucontext->uc_mcontext.gregs[REG_Y];
80 context->wim = 0; /* FIXME */
81 context->tbr = 0; /* FIXME */
83 /* Global registers */
84 context->g0 = 0; /* always */
85 context->g1 = ucontext->uc_mcontext.gregs[REG_G1];
86 context->g2 = ucontext->uc_mcontext.gregs[REG_G2];
87 context->g3 = ucontext->uc_mcontext.gregs[REG_G3];
88 context->g4 = ucontext->uc_mcontext.gregs[REG_G4];
89 context->g5 = ucontext->uc_mcontext.gregs[REG_G5];
90 context->g6 = ucontext->uc_mcontext.gregs[REG_G6];
91 context->g7 = ucontext->uc_mcontext.gregs[REG_G7];
93 /* Current 'out' registers */
94 context->o0 = ucontext->uc_mcontext.gregs[REG_O0];
95 context->o1 = ucontext->uc_mcontext.gregs[REG_O1];
96 context->o2 = ucontext->uc_mcontext.gregs[REG_O2];
97 context->o3 = ucontext->uc_mcontext.gregs[REG_O3];
98 context->o4 = ucontext->uc_mcontext.gregs[REG_O4];
99 context->o5 = ucontext->uc_mcontext.gregs[REG_O5];
100 context->o6 = ucontext->uc_mcontext.gregs[REG_O6];
101 context->o7 = ucontext->uc_mcontext.gregs[REG_O7];
103 /* FIXME: what if the current register window isn't saved? */
104 if ( ucontext->uc_mcontext.gwins && ucontext->uc_mcontext.gwins->wbcnt > 0 )
106 /* Current 'local' registers from first register window */
107 context->l0 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[0];
108 context->l1 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[1];
109 context->l2 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[2];
110 context->l3 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[3];
111 context->l4 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[4];
112 context->l5 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[5];
113 context->l6 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[6];
114 context->l7 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[7];
116 /* Current 'in' registers from first register window */
117 context->i0 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[0];
118 context->i1 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[1];
119 context->i2 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[2];
120 context->i3 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[3];
121 context->i4 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[4];
122 context->i5 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[5];
123 context->i6 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[6];
124 context->i7 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[7];
128 /**********************************************************************
129 * restore_context
131 static void restore_context( CONTEXT *context, ucontext_t *ucontext )
133 /* FIXME */
136 /**********************************************************************
137 * save_fpu
139 static void save_fpu( CONTEXT *context, ucontext_t *ucontext )
141 /* FIXME */
144 /**********************************************************************
145 * restore_fpu
147 static void restore_fpu( CONTEXT *context, ucontext_t *ucontext )
149 /* FIXME */
153 /**********************************************************************
154 * segv_handler
156 * Handler for SIGSEGV.
158 static void segv_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
160 EXCEPTION_RECORD rec;
161 CONTEXT context;
163 /* we want the page-fault case to be fast */
164 if ( info->si_code == SEGV_ACCERR )
165 if (VIRTUAL_HandleFault( (LPVOID)info->si_addr )) return;
167 save_context( &context, ucontext );
168 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
169 rec.ExceptionRecord = NULL;
170 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
171 rec.ExceptionAddress = (LPVOID)context.pc;
172 rec.NumberParameters = 2;
173 rec.ExceptionInformation[0] = 0; /* FIXME: read/write access ? */
174 rec.ExceptionInformation[1] = (DWORD)info->si_addr;
176 EXC_RtlRaiseException( &rec, &context );
177 restore_context( &context, ucontext );
180 /**********************************************************************
181 * bus_handler
183 * Handler for SIGBUS.
185 static void bus_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
187 EXCEPTION_RECORD rec;
188 CONTEXT context;
190 save_context( &context, ucontext );
191 rec.ExceptionRecord = NULL;
192 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
193 rec.ExceptionAddress = (LPVOID)context.pc;
194 rec.NumberParameters = 0;
196 if ( info->si_code == BUS_ADRALN )
197 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
198 else
199 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
201 EXC_RtlRaiseException( &rec, &context );
202 restore_context( &context, ucontext );
205 /**********************************************************************
206 * ill_handler
208 * Handler for SIGILL.
210 static void ill_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
212 EXCEPTION_RECORD rec;
213 CONTEXT context;
215 switch ( info->si_code )
217 default:
218 case ILL_ILLOPC:
219 case ILL_ILLOPN:
220 case ILL_ILLADR:
221 case ILL_ILLTRP:
222 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
223 break;
225 case ILL_PRVOPC:
226 case ILL_PRVREG:
227 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
228 break;
230 case ILL_BADSTK:
231 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
232 break;
235 save_context( &context, ucontext );
236 rec.ExceptionRecord = NULL;
237 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
238 rec.ExceptionAddress = (LPVOID)context.pc;
239 rec.NumberParameters = 0;
240 EXC_RtlRaiseException( &rec, &context );
241 restore_context( &context, ucontext );
245 /**********************************************************************
246 * trap_handler
248 * Handler for SIGTRAP.
250 static void trap_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
252 EXCEPTION_RECORD rec;
253 CONTEXT context;
255 switch ( info->si_code )
257 case TRAP_TRACE:
258 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
259 break;
260 case TRAP_BRKPT:
261 default:
262 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
263 break;
266 save_context( &context, ucontext );
267 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
268 rec.ExceptionRecord = NULL;
269 rec.ExceptionAddress = (LPVOID)context.pc;
270 rec.NumberParameters = 0;
271 EXC_RtlRaiseException( &rec, &context );
272 restore_context( &context, ucontext );
276 /**********************************************************************
277 * fpe_handler
279 * Handler for SIGFPE.
281 static void fpe_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
283 EXCEPTION_RECORD rec;
284 CONTEXT context;
286 switch ( info->si_code )
288 case FPE_FLTSUB:
289 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
290 break;
291 case FPE_INTDIV:
292 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
293 break;
294 case FPE_INTOVF:
295 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
296 break;
297 case FPE_FLTDIV:
298 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
299 break;
300 case FPE_FLTOVF:
301 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
302 break;
303 case FPE_FLTUND:
304 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
305 break;
306 case FPE_FLTRES:
307 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
308 break;
309 case FPE_FLTINV:
310 default:
311 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
312 break;
315 save_context( &context, ucontext );
316 save_fpu( &context, ucontext );
317 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
318 rec.ExceptionRecord = NULL;
319 rec.ExceptionAddress = (LPVOID)context.pc;
320 rec.NumberParameters = 0;
321 EXC_RtlRaiseException( &rec, &context );
322 restore_context( &context, ucontext );
323 restore_fpu( &context, ucontext );
327 /**********************************************************************
328 * int_handler
330 * Handler for SIGINT.
332 static void int_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
334 if (!dispatch_signal(SIGINT))
336 EXCEPTION_RECORD rec;
337 CONTEXT context;
339 save_context( &context, ucontext );
340 rec.ExceptionCode = CONTROL_C_EXIT;
341 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
342 rec.ExceptionRecord = NULL;
343 rec.ExceptionAddress = (LPVOID)context.pc;
344 rec.NumberParameters = 0;
345 EXC_RtlRaiseException( &rec, &context );
346 restore_context( &context, ucontext );
350 /**********************************************************************
351 * abrt_handler
353 * Handler for SIGABRT.
355 static HANDLER_DEF(abrt_handler)
357 EXCEPTION_RECORD rec;
358 CONTEXT context;
360 save_context( &context, HANDLER_CONTEXT );
361 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
362 rec.ExceptionFlags = EH_NONCONTINUABLE;
363 rec.ExceptionRecord = NULL;
364 rec.ExceptionAddress = (LPVOID)context.pc;
365 rec.NumberParameters = 0;
366 EXC_RtlRaiseException( &rec, &context ); /* Should never return.. */
367 restore_context( &context, HANDLER_CONTEXT );
371 /**********************************************************************
372 * term_handler
374 * Handler for SIGTERM.
376 static HANDLER_DEF(term_handler)
378 server_abort_thread(0);
382 /**********************************************************************
383 * usr1_handler
385 * Handler for SIGUSR1, used to signal a thread that it got suspended.
387 static HANDLER_DEF(usr1_handler)
389 LARGE_INTEGER timeout;
391 /* wait with 0 timeout, will only return once the thread is no longer suspended */
392 timeout.QuadPart = 0;
393 NTDLL_wait_for_multiple_objects( 0, NULL, 0, &timeout );
397 /***********************************************************************
398 * set_handler
400 * Set a signal handler
402 static int set_handler( int sig, void (*func)() )
404 struct sigaction sig_act;
406 sig_act.sa_handler = NULL;
407 sig_act.sa_sigaction = func;
408 sigemptyset( &sig_act.sa_mask );
409 sig_act.sa_flags = SA_SIGINFO;
411 return sigaction( sig, &sig_act, NULL );
415 /***********************************************************************
416 * __wine_set_signal_handler (NTDLL.@)
418 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
420 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
421 if (handlers[sig] != NULL) return -2;
422 handlers[sig] = wsh;
423 return 0;
427 /**********************************************************************
428 * SIGNAL_Init
430 BOOL SIGNAL_Init(void)
432 if (set_handler( SIGINT, (void (*)())int_handler ) == -1) goto error;
433 if (set_handler( SIGFPE, (void (*)())fpe_handler ) == -1) goto error;
434 if (set_handler( SIGSEGV, (void (*)())segv_handler ) == -1) goto error;
435 if (set_handler( SIGILL, (void (*)())ill_handler ) == -1) goto error;
436 if (set_handler( SIGBUS, (void (*)())bus_handler ) == -1) goto error;
437 if (set_handler( SIGTRAP, (void (*)())trap_handler ) == -1) goto error;
438 if (set_handler( SIGABRT, (void (*)())abrt_handler ) == -1) goto error;
439 if (set_handler( SIGTERM, (void (*)())term_handler ) == -1) goto error;
440 if (set_handler( SIGUSR1, (void (*)())usr1_handler ) == -1) goto error;
441 /* 'ta 6' tells the kernel to synthesize any unaligned accesses this
442 process makes, instead of just signalling an error and terminating
443 the process. wine-devel did not reach a conclusion on whether
444 this is correct, because that is what x86 does, or it is harmful
445 because it could obscure problems in user code */
446 asm("ta 6"); /* 6 == ST_FIX_ALIGN defined in sys/trap.h */
447 return TRUE;
449 error:
450 perror("sigaction");
451 return FALSE;
455 /**********************************************************************
456 * __wine_enter_vm86
458 void __wine_enter_vm86( CONTEXT *context )
460 MESSAGE("vm86 mode not supported on this platform\n");
463 /**********************************************************************
464 * DbgBreakPoint (NTDLL.@)
466 void WINAPI DbgBreakPoint(void)
468 kill(getpid(), SIGTRAP);
471 /**********************************************************************
472 * DbgUserBreakPoint (NTDLL.@)
474 void WINAPI DbgUserBreakPoint(void)
476 kill(getpid(), SIGTRAP);
479 #endif /* __sparc__ */