ntdll: Split the signal setup into process-wide and thread-specific routines.
[wine/wine64.git] / dlls / ntdll / signal_sparc.c
blobd3896f36f47e04c62b6a7b2622f0b89b0274f2ba
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #ifdef __sparc__
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <sys/ucontext.h>
36 #include "windef.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 /***********************************************************************
55 * dispatch_signal
57 static inline int dispatch_signal(unsigned int sig)
59 if (handlers[sig] == NULL) return 0;
60 return handlers[sig](sig);
65 * FIXME: All this works only on Solaris for now
68 /**********************************************************************
69 * save_context
71 static void save_context( CONTEXT *context, ucontext_t *ucontext )
73 /* Special registers */
74 context->psr = ucontext->uc_mcontext.gregs[REG_PSR];
75 context->pc = ucontext->uc_mcontext.gregs[REG_PC];
76 context->npc = ucontext->uc_mcontext.gregs[REG_nPC];
77 context->y = ucontext->uc_mcontext.gregs[REG_Y];
78 context->wim = 0; /* FIXME */
79 context->tbr = 0; /* FIXME */
81 /* Global registers */
82 context->g0 = 0; /* always */
83 context->g1 = ucontext->uc_mcontext.gregs[REG_G1];
84 context->g2 = ucontext->uc_mcontext.gregs[REG_G2];
85 context->g3 = ucontext->uc_mcontext.gregs[REG_G3];
86 context->g4 = ucontext->uc_mcontext.gregs[REG_G4];
87 context->g5 = ucontext->uc_mcontext.gregs[REG_G5];
88 context->g6 = ucontext->uc_mcontext.gregs[REG_G6];
89 context->g7 = ucontext->uc_mcontext.gregs[REG_G7];
91 /* Current 'out' registers */
92 context->o0 = ucontext->uc_mcontext.gregs[REG_O0];
93 context->o1 = ucontext->uc_mcontext.gregs[REG_O1];
94 context->o2 = ucontext->uc_mcontext.gregs[REG_O2];
95 context->o3 = ucontext->uc_mcontext.gregs[REG_O3];
96 context->o4 = ucontext->uc_mcontext.gregs[REG_O4];
97 context->o5 = ucontext->uc_mcontext.gregs[REG_O5];
98 context->o6 = ucontext->uc_mcontext.gregs[REG_O6];
99 context->o7 = ucontext->uc_mcontext.gregs[REG_O7];
101 /* FIXME: what if the current register window isn't saved? */
102 if ( ucontext->uc_mcontext.gwins && ucontext->uc_mcontext.gwins->wbcnt > 0 )
104 /* Current 'local' registers from first register window */
105 context->l0 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[0];
106 context->l1 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[1];
107 context->l2 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[2];
108 context->l3 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[3];
109 context->l4 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[4];
110 context->l5 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[5];
111 context->l6 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[6];
112 context->l7 = ucontext->uc_mcontext.gwins->wbuf[0].rw_local[7];
114 /* Current 'in' registers from first register window */
115 context->i0 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[0];
116 context->i1 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[1];
117 context->i2 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[2];
118 context->i3 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[3];
119 context->i4 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[4];
120 context->i5 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[5];
121 context->i6 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[6];
122 context->i7 = ucontext->uc_mcontext.gwins->wbuf[0].rw_in[7];
126 /**********************************************************************
127 * restore_context
129 static void restore_context( CONTEXT *context, ucontext_t *ucontext )
131 /* FIXME */
134 /**********************************************************************
135 * save_fpu
137 static void save_fpu( CONTEXT *context, ucontext_t *ucontext )
139 /* FIXME */
142 /**********************************************************************
143 * restore_fpu
145 static void restore_fpu( CONTEXT *context, ucontext_t *ucontext )
147 /* FIXME */
151 /***********************************************************************
152 * get_cpu_context
154 * Get the context of the current thread.
156 void get_cpu_context( CONTEXT *context )
158 FIXME("not implemented\n");
162 /***********************************************************************
163 * set_cpu_context
165 * Set the new CPU context.
167 void set_cpu_context( const CONTEXT *context )
169 FIXME("not implemented\n");
173 /**********************************************************************
174 * segv_handler
176 * Handler for SIGSEGV.
178 static void segv_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
180 EXCEPTION_RECORD rec;
181 CONTEXT context;
183 /* we want the page-fault case to be fast */
184 if ( info->si_code == SEGV_ACCERR )
185 if (VIRTUAL_HandleFault( (LPVOID)info->si_addr )) return;
187 save_context( &context, ucontext );
188 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
189 rec.ExceptionRecord = NULL;
190 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
191 rec.ExceptionAddress = (LPVOID)context.pc;
192 rec.NumberParameters = 2;
193 rec.ExceptionInformation[0] = 0; /* FIXME: read/write access ? */
194 rec.ExceptionInformation[1] = (ULONG_PTR)info->si_addr;
196 __regs_RtlRaiseException( &rec, &context );
197 restore_context( &context, ucontext );
200 /**********************************************************************
201 * bus_handler
203 * Handler for SIGBUS.
205 static void bus_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
207 EXCEPTION_RECORD rec;
208 CONTEXT context;
210 save_context( &context, ucontext );
211 rec.ExceptionRecord = NULL;
212 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
213 rec.ExceptionAddress = (LPVOID)context.pc;
214 rec.NumberParameters = 0;
216 if ( info->si_code == BUS_ADRALN )
217 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
218 else
219 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
221 __regs_RtlRaiseException( &rec, &context );
222 restore_context( &context, ucontext );
225 /**********************************************************************
226 * ill_handler
228 * Handler for SIGILL.
230 static void ill_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
232 EXCEPTION_RECORD rec;
233 CONTEXT context;
235 switch ( info->si_code )
237 default:
238 case ILL_ILLOPC:
239 case ILL_ILLOPN:
240 case ILL_ILLADR:
241 case ILL_ILLTRP:
242 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
243 break;
245 case ILL_PRVOPC:
246 case ILL_PRVREG:
247 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
248 break;
250 case ILL_BADSTK:
251 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
252 break;
255 save_context( &context, ucontext );
256 rec.ExceptionRecord = NULL;
257 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
258 rec.ExceptionAddress = (LPVOID)context.pc;
259 rec.NumberParameters = 0;
260 __regs_RtlRaiseException( &rec, &context );
261 restore_context( &context, ucontext );
265 /**********************************************************************
266 * trap_handler
268 * Handler for SIGTRAP.
270 static void trap_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
272 EXCEPTION_RECORD rec;
273 CONTEXT context;
275 switch ( info->si_code )
277 case TRAP_TRACE:
278 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
279 break;
280 case TRAP_BRKPT:
281 default:
282 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
283 break;
286 save_context( &context, ucontext );
287 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
288 rec.ExceptionRecord = NULL;
289 rec.ExceptionAddress = (LPVOID)context.pc;
290 rec.NumberParameters = 0;
291 __regs_RtlRaiseException( &rec, &context );
292 restore_context( &context, ucontext );
296 /**********************************************************************
297 * fpe_handler
299 * Handler for SIGFPE.
301 static void fpe_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
303 EXCEPTION_RECORD rec;
304 CONTEXT context;
306 switch ( info->si_code )
308 case FPE_FLTSUB:
309 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
310 break;
311 case FPE_INTDIV:
312 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
313 break;
314 case FPE_INTOVF:
315 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
316 break;
317 case FPE_FLTDIV:
318 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
319 break;
320 case FPE_FLTOVF:
321 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
322 break;
323 case FPE_FLTUND:
324 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
325 break;
326 case FPE_FLTRES:
327 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
328 break;
329 case FPE_FLTINV:
330 default:
331 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
332 break;
335 save_context( &context, ucontext );
336 save_fpu( &context, ucontext );
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 );
343 restore_fpu( &context, ucontext );
347 /**********************************************************************
348 * int_handler
350 * Handler for SIGINT.
352 static void int_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
354 if (!dispatch_signal(SIGINT))
356 EXCEPTION_RECORD rec;
357 CONTEXT context;
359 save_context( &context, ucontext );
360 rec.ExceptionCode = CONTROL_C_EXIT;
361 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
362 rec.ExceptionRecord = NULL;
363 rec.ExceptionAddress = (LPVOID)context.pc;
364 rec.NumberParameters = 0;
365 __regs_RtlRaiseException( &rec, &context );
366 restore_context( &context, ucontext );
370 /**********************************************************************
371 * abrt_handler
373 * Handler for SIGABRT.
375 static HANDLER_DEF(abrt_handler)
377 EXCEPTION_RECORD rec;
378 CONTEXT context;
380 save_context( &context, HANDLER_CONTEXT );
381 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
382 rec.ExceptionFlags = EH_NONCONTINUABLE;
383 rec.ExceptionRecord = NULL;
384 rec.ExceptionAddress = (LPVOID)context.pc;
385 rec.NumberParameters = 0;
386 __regs_RtlRaiseException( &rec, &context ); /* Should never return.. */
387 restore_context( &context, HANDLER_CONTEXT );
391 /**********************************************************************
392 * quit_handler
394 * Handler for SIGQUIT.
396 static HANDLER_DEF(quit_handler)
398 server_abort_thread(0);
402 /**********************************************************************
403 * usr1_handler
405 * Handler for SIGUSR1, used to signal a thread that it got suspended.
407 static HANDLER_DEF(usr1_handler)
409 CONTEXT context;
411 save_context( &context, HANDLER_CONTEXT );
412 wait_suspend( &context );
413 restore_context( &context, HANDLER_CONTEXT );
417 /**********************************************************************
418 * get_signal_stack_total_size
420 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
421 * Must be a power of two.
423 size_t get_signal_stack_total_size(void)
425 assert( sizeof(TEB) <= getpagesize() );
426 return getpagesize(); /* this is just for the TEB, we don't need a signal stack */
430 /***********************************************************************
431 * set_handler
433 * Set a signal handler
435 static int set_handler( int sig, void (*func)() )
437 struct sigaction sig_act;
439 sig_act.sa_sigaction = func;
440 sig_act.sa_mask = server_block_set;
441 sig_act.sa_flags = SA_SIGINFO;
443 return sigaction( sig, &sig_act, NULL );
447 /***********************************************************************
448 * __wine_set_signal_handler (NTDLL.@)
450 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
452 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
453 if (handlers[sig] != NULL) return -2;
454 handlers[sig] = wsh;
455 return 0;
459 /**********************************************************************
460 * signal_init_thread
462 void signal_init_thread(void)
466 /**********************************************************************
467 * signal_init_process
469 void signal_init_process(void)
471 if (set_handler( SIGINT, (void (*)())int_handler ) == -1) goto error;
472 if (set_handler( SIGFPE, (void (*)())fpe_handler ) == -1) goto error;
473 if (set_handler( SIGSEGV, (void (*)())segv_handler ) == -1) goto error;
474 if (set_handler( SIGILL, (void (*)())ill_handler ) == -1) goto error;
475 if (set_handler( SIGBUS, (void (*)())bus_handler ) == -1) goto error;
476 if (set_handler( SIGTRAP, (void (*)())trap_handler ) == -1) goto error;
477 if (set_handler( SIGABRT, (void (*)())abrt_handler ) == -1) goto error;
478 if (set_handler( SIGQUIT, (void (*)())quit_handler ) == -1) goto error;
479 if (set_handler( SIGUSR1, (void (*)())usr1_handler ) == -1) goto error;
480 /* 'ta 6' tells the kernel to synthesize any unaligned accesses this
481 process makes, instead of just signalling an error and terminating
482 the process. wine-devel did not reach a conclusion on whether
483 this is correct, because that is what x86 does, or it is harmful
484 because it could obscure problems in user code */
485 asm("ta 6"); /* 6 == ST_FIX_ALIGN defined in sys/trap.h */
486 signal_init_thread();
487 return;
489 error:
490 perror("sigaction");
491 exit(1);
495 /**********************************************************************
496 * __wine_enter_vm86
498 void __wine_enter_vm86( CONTEXT *context )
500 MESSAGE("vm86 mode not supported on this platform\n");
503 /**********************************************************************
504 * DbgBreakPoint (NTDLL.@)
506 void WINAPI DbgBreakPoint(void)
508 kill(getpid(), SIGTRAP);
511 /**********************************************************************
512 * DbgUserBreakPoint (NTDLL.@)
514 void WINAPI DbgUserBreakPoint(void)
516 kill(getpid(), SIGTRAP);
519 #endif /* __sparc__ */