ntdll: tape.c portability fixes.
[wine] / dlls / ntdll / signal_sparc.c
1 /*
2  * Sparc signal handling routines
3  *
4  * Copyright 1999 Ulrich Weigand
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #ifdef __sparc__
22
23 #include "config.h"
24 #include "wine/port.h"
25
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>
35
36 #include "windef.h"
37 #include "winternl.h"
38 #include "winnt.h"
39
40 #include "wine/exception.h"
41 #include "ntdll_misc.h"
42
43 #include "wine/debug.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(seh);
46
47 #define HANDLER_DEF(name) void name( int __signal, struct siginfo *__siginfo, ucontext_t *__context )
48 #define HANDLER_CONTEXT (__context)
49
50 typedef int (*wine_signal_handler)(unsigned int sig);
51
52 static wine_signal_handler handlers[256];
53
54 /***********************************************************************
55  *           dispatch_signal
56  */
57 inline static int dispatch_signal(unsigned int sig)
58 {
59     if (handlers[sig] == NULL) return 0;
60     return handlers[sig](sig);
61 }
62
63
64 /*
65  * FIXME:  All this works only on Solaris for now
66  */
67
68 /**********************************************************************
69  *              save_context
70  */
71 static void save_context( CONTEXT *context, ucontext_t *ucontext )
72 {
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 */
80
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];
90
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];
100
101     /* FIXME: what if the current register window isn't saved? */
102     if ( ucontext->uc_mcontext.gwins && ucontext->uc_mcontext.gwins->wbcnt > 0 )
103     {
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];
113
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];
123     }
124 }
125
126 /**********************************************************************
127  *              restore_context
128  */
129 static void restore_context( CONTEXT *context, ucontext_t *ucontext )
130 {
131    /* FIXME */
132 }
133
134 /**********************************************************************
135  *              save_fpu
136  */
137 static void save_fpu( CONTEXT *context, ucontext_t *ucontext )
138 {
139    /* FIXME */
140 }
141
142 /**********************************************************************
143  *              restore_fpu
144  */
145 static void restore_fpu( CONTEXT *context, ucontext_t *ucontext )
146 {
147    /* FIXME */
148 }
149
150
151 /***********************************************************************
152  *           set_cpu_context
153  *
154  * Set the new CPU context.
155  */
156 void set_cpu_context( const CONTEXT *context )
157 {
158     FIXME("not implemented\n");
159 }
160
161
162 /**********************************************************************
163  *              segv_handler
164  *
165  * Handler for SIGSEGV.
166  */
167 static void segv_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
168 {
169     EXCEPTION_RECORD rec;
170     CONTEXT context;
171
172     /* we want the page-fault case to be fast */
173     if ( info->si_code == SEGV_ACCERR )
174         if (VIRTUAL_HandleFault( (LPVOID)info->si_addr )) return;
175
176     save_context( &context, ucontext );
177     rec.ExceptionCode    = EXCEPTION_ACCESS_VIOLATION;
178     rec.ExceptionRecord  = NULL;
179     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
180     rec.ExceptionAddress = (LPVOID)context.pc;
181     rec.NumberParameters = 2;
182     rec.ExceptionInformation[0] = 0;  /* FIXME: read/write access ? */
183     rec.ExceptionInformation[1] = (ULONG_PTR)info->si_addr;
184
185     __regs_RtlRaiseException( &rec, &context );
186     restore_context( &context, ucontext );
187 }
188
189 /**********************************************************************
190  *              bus_handler
191  *
192  * Handler for SIGBUS.
193  */
194 static void bus_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
195 {
196     EXCEPTION_RECORD rec;
197     CONTEXT context;
198
199     save_context( &context, ucontext );
200     rec.ExceptionRecord  = NULL;
201     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
202     rec.ExceptionAddress = (LPVOID)context.pc;
203     rec.NumberParameters = 0;
204
205     if ( info->si_code == BUS_ADRALN )
206         rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
207     else
208         rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
209
210     __regs_RtlRaiseException( &rec, &context );
211     restore_context( &context, ucontext );
212 }
213
214 /**********************************************************************
215  *              ill_handler
216  *
217  * Handler for SIGILL.
218  */
219 static void ill_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
220 {
221     EXCEPTION_RECORD rec;
222     CONTEXT context;
223
224     switch ( info->si_code )
225     {
226     default:
227     case ILL_ILLOPC:
228     case ILL_ILLOPN:
229     case ILL_ILLADR:
230     case ILL_ILLTRP:
231         rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
232         break;
233
234     case ILL_PRVOPC:
235     case ILL_PRVREG:
236         rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
237         break;
238
239     case ILL_BADSTK:
240         rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
241         break;
242     }
243
244     save_context( &context, ucontext );
245     rec.ExceptionRecord  = NULL;
246     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
247     rec.ExceptionAddress = (LPVOID)context.pc;
248     rec.NumberParameters = 0;
249     __regs_RtlRaiseException( &rec, &context );
250     restore_context( &context, ucontext );
251 }
252
253
254 /**********************************************************************
255  *              trap_handler
256  *
257  * Handler for SIGTRAP.
258  */
259 static void trap_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
260 {
261     EXCEPTION_RECORD rec;
262     CONTEXT context;
263
264     switch ( info->si_code )
265     {
266     case TRAP_TRACE:
267         rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
268         break;
269     case TRAP_BRKPT:
270     default:
271         rec.ExceptionCode = EXCEPTION_BREAKPOINT;
272         break;
273     }
274
275     save_context( &context, ucontext );
276     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
277     rec.ExceptionRecord  = NULL;
278     rec.ExceptionAddress = (LPVOID)context.pc;
279     rec.NumberParameters = 0;
280     __regs_RtlRaiseException( &rec, &context );
281     restore_context( &context, ucontext );
282 }
283
284
285 /**********************************************************************
286  *              fpe_handler
287  *
288  * Handler for SIGFPE.
289  */
290 static void fpe_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
291 {
292     EXCEPTION_RECORD rec;
293     CONTEXT context;
294
295     switch ( info->si_code )
296     {
297     case FPE_FLTSUB:
298         rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
299         break;
300     case FPE_INTDIV:
301         rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
302         break;
303     case FPE_INTOVF:
304         rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
305         break;
306     case FPE_FLTDIV:
307         rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
308         break;
309     case FPE_FLTOVF:
310         rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
311         break;
312     case FPE_FLTUND:
313         rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
314         break;
315     case FPE_FLTRES:
316         rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
317         break;
318     case FPE_FLTINV:
319     default:
320         rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
321         break;
322     }
323
324     save_context( &context, ucontext );
325     save_fpu( &context, ucontext );
326     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
327     rec.ExceptionRecord  = NULL;
328     rec.ExceptionAddress = (LPVOID)context.pc;
329     rec.NumberParameters = 0;
330     __regs_RtlRaiseException( &rec, &context );
331     restore_context( &context, ucontext );
332     restore_fpu( &context, ucontext );
333 }
334
335
336 /**********************************************************************
337  *              int_handler
338  *
339  * Handler for SIGINT.
340  */
341 static void int_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
342 {
343     if (!dispatch_signal(SIGINT))
344     {
345         EXCEPTION_RECORD rec;
346         CONTEXT context;
347
348         save_context( &context, ucontext );
349         rec.ExceptionCode    = CONTROL_C_EXIT;
350         rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
351         rec.ExceptionRecord  = NULL;
352         rec.ExceptionAddress = (LPVOID)context.pc;
353         rec.NumberParameters = 0;
354         __regs_RtlRaiseException( &rec, &context );
355         restore_context( &context, ucontext );
356     }
357 }
358
359 /**********************************************************************
360  *              abrt_handler
361  *
362  * Handler for SIGABRT.
363  */
364 static HANDLER_DEF(abrt_handler)
365 {
366     EXCEPTION_RECORD rec;
367     CONTEXT context;
368
369     save_context( &context, HANDLER_CONTEXT );
370     rec.ExceptionCode    = EXCEPTION_WINE_ASSERTION;
371     rec.ExceptionFlags   = EH_NONCONTINUABLE;
372     rec.ExceptionRecord  = NULL;
373     rec.ExceptionAddress = (LPVOID)context.pc;
374     rec.NumberParameters = 0;
375     __regs_RtlRaiseException( &rec, &context ); /* Should never return.. */
376     restore_context( &context, HANDLER_CONTEXT );
377 }
378
379
380 /**********************************************************************
381  *              term_handler
382  *
383  * Handler for SIGTERM.
384  */
385 static HANDLER_DEF(term_handler)
386 {
387     server_abort_thread(0);
388 }
389
390
391 /**********************************************************************
392  *              usr1_handler
393  *
394  * Handler for SIGUSR1, used to signal a thread that it got suspended.
395  */
396 static HANDLER_DEF(usr1_handler)
397 {
398     CONTEXT context;
399
400     save_context( &context, HANDLER_CONTEXT );
401     wait_suspend( &context );
402     restore_context( &context, HANDLER_CONTEXT );
403 }
404
405
406 /**********************************************************************
407  *              get_signal_stack_total_size
408  *
409  * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
410  * Must be a power of two.
411  */
412 size_t get_signal_stack_total_size(void)
413 {
414     assert( sizeof(TEB) <= getpagesize() );
415     return getpagesize();  /* this is just for the TEB, we don't need a signal stack */
416 }
417
418
419 /***********************************************************************
420  *           set_handler
421  *
422  * Set a signal handler
423  */
424 static int set_handler( int sig, void (*func)() )
425 {
426     struct sigaction sig_act;
427
428     sig_act.sa_handler = NULL;
429     sig_act.sa_sigaction = func;
430     sigemptyset( &sig_act.sa_mask );
431     sig_act.sa_flags = SA_SIGINFO;
432
433     return sigaction( sig, &sig_act, NULL );
434 }
435
436
437 /***********************************************************************
438  *           __wine_set_signal_handler   (NTDLL.@)
439  */
440 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
441 {
442     if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
443     if (handlers[sig] != NULL) return -2;
444     handlers[sig] = wsh;
445     return 0;
446 }
447
448
449 /**********************************************************************
450  *              SIGNAL_Init
451  */
452 BOOL SIGNAL_Init(void)
453 {
454     if (set_handler( SIGINT,  (void (*)())int_handler  ) == -1) goto error;
455     if (set_handler( SIGFPE,  (void (*)())fpe_handler  ) == -1) goto error;
456     if (set_handler( SIGSEGV, (void (*)())segv_handler ) == -1) goto error;
457     if (set_handler( SIGILL,  (void (*)())ill_handler  ) == -1) goto error;
458     if (set_handler( SIGBUS,  (void (*)())bus_handler  ) == -1) goto error;
459     if (set_handler( SIGTRAP, (void (*)())trap_handler ) == -1) goto error;
460     if (set_handler( SIGABRT, (void (*)())abrt_handler ) == -1) goto error;
461     if (set_handler( SIGTERM, (void (*)())term_handler ) == -1) goto error;
462     if (set_handler( SIGUSR1, (void (*)())usr1_handler ) == -1) goto error;
463     /* 'ta 6' tells the kernel to synthesize any unaligned accesses this 
464        process makes, instead of just signalling an error and terminating
465        the process.  wine-devel did not reach a conclusion on whether
466        this is correct, because that is what x86 does, or it is harmful 
467        because it could obscure problems in user code */
468     asm("ta 6"); /* 6 == ST_FIX_ALIGN defined in sys/trap.h */
469    return TRUE;
470
471  error:
472     perror("sigaction");
473     return FALSE;
474 }
475
476
477 /**********************************************************************
478  *              __wine_enter_vm86
479  */
480 void __wine_enter_vm86( CONTEXT *context )
481 {
482     MESSAGE("vm86 mode not supported on this platform\n");
483 }
484
485 /**********************************************************************
486  *              DbgBreakPoint   (NTDLL.@)
487  */
488 void WINAPI DbgBreakPoint(void)
489 {
490      kill(getpid(), SIGTRAP);
491 }
492
493 /**********************************************************************
494  *              DbgUserBreakPoint   (NTDLL.@)
495  */
496 void WINAPI DbgUserBreakPoint(void)
497 {
498      kill(getpid(), SIGTRAP);
499 }
500
501 #endif  /* __sparc__ */