Updated the generated tests.
[wine] / dlls / ntdll / signal_powerpc.c
1 /*
2  * PowerPC signal handling routines
3  *
4  * Copyright 2002 Marcus Meissner, SuSE Linux AG
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 __powerpc__
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33
34 #ifdef HAVE_SYS_PARAM_H
35 # include <sys/param.h>
36 #endif
37 #ifdef HAVE_SYSCALL_H
38 # include <syscall.h>
39 #else
40 # ifdef HAVE_SYS_SYSCALL_H
41 #  include <sys/syscall.h>
42 # endif
43 #endif
44
45 #ifdef HAVE_SYS_VM86_H
46 # include <sys/vm86.h>
47 #endif
48
49 #ifdef HAVE_SYS_SIGNAL_H
50 # include <sys/signal.h>
51 #endif
52
53 #include "winternl.h"
54 #include "winnt.h"
55 #include "wine/library.h"
56 #include "wine/exception.h"
57 #include "selectors.h"
58 #include "stackframe.h"
59 #include "global.h"
60 #include "miscemu.h"
61 #include "syslevel.h"
62 #include "wine/debug.h"
63
64 WINE_DEFAULT_DEBUG_CHANNEL(seh);
65
66
67 /***********************************************************************
68  * signal context platform-specific definitions
69  */
70
71 typedef struct ucontext SIGCONTEXT;
72
73 #define HANDLER_DEF(name) void name( int __signal, struct siginfo *__siginfo, SIGCONTEXT *__context )
74 #define HANDLER_CONTEXT (__context)
75
76 typedef int (*wine_signal_handler)(unsigned int sig);
77
78 static wine_signal_handler handlers[256];
79
80 static sigset_t all_sigs;
81
82
83 /***********************************************************************
84  *           dispatch_signal
85  */
86 inline static int dispatch_signal(unsigned int sig)
87 {
88     if (handlers[sig] == NULL) return 0;
89     return handlers[sig](sig);
90 }
91
92 /***********************************************************************
93  *           save_context
94  *
95  * Set the register values from a sigcontext.
96  */
97 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
98 {
99 #define CX(x,y) context->x = sigcontext->uc_mcontext.regs->y
100 #define C(x) CX(Gpr##x,gpr[x])
101         C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
102         C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
103         C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
104         C(31);
105
106         CX(Iar,nip);
107         CX(Msr,msr);
108         CX(Ctr,ctr);
109 #undef CX
110         /* FIXME: fp regs? */
111
112         /* FIXME: missing pt_regs ...
113         unsigned long link;
114         unsigned long xer;
115         unsigned long ccr;
116         unsigned long mq;
117
118         unsigned long trap;
119         unsigned long dar;
120         unsigned long dsisr;
121
122         */
123 }
124
125
126 /***********************************************************************
127  *           restore_context
128  *
129  * Build a sigcontext from the register values.
130  */
131 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
132 {
133 #define CX(x,y) sigcontext->uc_mcontext.regs->y = context->x
134         C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
135         C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
136         C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
137         C(31);
138
139         CX(Iar,nip);
140         CX(Msr,msr);
141         CX(Ctr,ctr);
142 #undef CX
143 }
144
145
146 /***********************************************************************
147  *           save_fpu
148  *
149  * Set the FPU context from a sigcontext.
150  */
151 inline static void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
152 {
153         /* FIXME? */
154 }
155
156
157 /***********************************************************************
158  *           restore_fpu
159  *
160  * Restore the FPU context to a sigcontext.
161  */
162 inline static void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
163 {
164         /* FIXME? */
165 }
166
167
168 /**********************************************************************
169  *              get_fpu_code
170  *
171  * Get the FPU exception code from the FPU status.
172  */
173 static inline DWORD get_fpu_code( const CONTEXT *context )
174 {
175     DWORD status  = 0 /* FIXME */;
176
177     if (status & 0x01)  /* IE */
178     {
179         if (status & 0x40)  /* SF */
180             return EXCEPTION_FLT_STACK_CHECK;
181         else
182             return EXCEPTION_FLT_INVALID_OPERATION;
183     }
184     if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND;  /* DE flag */
185     if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO;    /* ZE flag */
186     if (status & 0x08) return EXCEPTION_FLT_OVERFLOW;          /* OE flag */
187     if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW;         /* UE flag */
188     if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT;    /* PE flag */
189     return EXCEPTION_FLT_INVALID_OPERATION;  /* generic error */
190 }
191
192
193 /***********************************************************************
194  *           SIGNAL_Unblock
195  *
196  * Unblock signals. Called from EXC_RtlRaiseException.
197  */
198 void SIGNAL_Unblock( void )
199 {
200     sigprocmask( SIG_UNBLOCK, &all_sigs, NULL );
201 }
202
203 /**********************************************************************
204  *              segv_handler
205  *
206  * Handler for SIGSEGV and related errors.
207  */
208 static HANDLER_DEF(segv_handler)
209 {
210     CONTEXT context;
211     EXCEPTION_RECORD rec;
212     DWORD page_fault_code = EXCEPTION_ACCESS_VIOLATION;
213
214     save_context( &context, HANDLER_CONTEXT );
215
216     rec.ExceptionRecord  = NULL;
217     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
218     rec.ExceptionAddress = (LPVOID)HANDLER_CONTEXT->uc_mcontext.regs->nip;
219     rec.NumberParameters = 0;
220     switch (__siginfo->si_signo) {
221     case SIGSEGV:
222         switch ( __siginfo->si_code & 0xffff ) {
223         case SEGV_MAPERR:
224         case SEGV_ACCERR:
225                 rec.NumberParameters = 2;
226                 rec.ExceptionInformation[0] = 0; /* FIXME ? */
227                 rec.ExceptionInformation[1] = (DWORD)__siginfo->si_addr;
228                 if (!(page_fault_code=VIRTUAL_HandleFault(__siginfo->si_addr)))
229                         return;
230                 rec.ExceptionCode = page_fault_code;
231                 break;
232         default:FIXME("Unhandled SIGSEGV/%x\n",__siginfo->si_code);
233                 break;
234         }
235         break;
236     case SIGBUS:
237         switch ( __siginfo->si_code & 0xffff ) {
238         case BUS_ADRALN:
239                 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
240                 break;
241         case BUS_ADRERR:
242         case BUS_OBJERR:
243                 /* FIXME: correct for all cases ? */
244                 rec.NumberParameters = 2;
245                 rec.ExceptionInformation[0] = 0; /* FIXME ? */
246                 rec.ExceptionInformation[1] = (DWORD)__siginfo->si_addr;
247                 if (!(page_fault_code=VIRTUAL_HandleFault(__siginfo->si_addr)))
248                         return;
249                 rec.ExceptionCode = page_fault_code;
250                 break;
251         default:FIXME("Unhandled SIGBUS/%x\n",__siginfo->si_code);
252                 break;
253         }
254         break;
255     case SIGILL:
256         switch ( __siginfo->si_code & 0xffff ) {
257         case ILL_ILLOPC: /* illegal opcode */
258         case ILL_ILLOPN: /* illegal operand */
259         case ILL_ILLADR: /* illegal addressing mode */
260         case ILL_ILLTRP: /* illegal trap */
261         case ILL_COPROC: /* coprocessor error */
262                 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
263                 break;
264         case ILL_PRVOPC: /* privileged opcode */
265         case ILL_PRVREG: /* privileged register */
266                 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
267                 break;
268         case ILL_BADSTK: /* internal stack error */
269                 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
270                 break;
271         default:FIXME("Unhandled SIGILL/%x\n",__siginfo->si_code);
272                 break;
273         }
274         break;
275     }
276     EXC_RtlRaiseException( &rec, &context );
277     restore_context( &context, HANDLER_CONTEXT );
278 }
279
280 /**********************************************************************
281  *              trap_handler
282  *
283  * Handler for SIGTRAP.
284  */
285 static HANDLER_DEF(trap_handler)
286 {
287     CONTEXT context;
288     EXCEPTION_RECORD rec;
289
290     save_context( &context, HANDLER_CONTEXT );
291
292     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
293     rec.ExceptionRecord  = NULL;
294     rec.ExceptionAddress = (LPVOID)__context->uc_mcontext.regs->nip;
295     rec.NumberParameters = 0;
296
297     /* FIXME: check if we might need to modify PC */
298     switch (__siginfo->si_code & 0xffff) {
299     case TRAP_BRKPT:
300         rec.ExceptionCode = EXCEPTION_BREAKPOINT;
301         break;
302     case TRAP_TRACE:
303         rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
304         break;
305     }
306     EXC_RtlRaiseException( &rec, &context );
307     restore_context( &context, HANDLER_CONTEXT );
308 }
309
310
311 /**********************************************************************
312  *              fpe_handler
313  *
314  * Handler for SIGFPE.
315  */
316 static HANDLER_DEF(fpe_handler)
317 {
318     CONTEXT context;
319     EXCEPTION_RECORD rec;
320
321     /*save_fpu( &context, HANDLER_CONTEXT );*/
322     save_context( &context, HANDLER_CONTEXT );
323
324     switch ( __siginfo->si_code  & 0xffff ) {
325     case FPE_FLTSUB:
326         rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
327         break;
328     case FPE_INTDIV:
329         rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
330         break;
331     case FPE_INTOVF:
332         rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
333         break;
334     case FPE_FLTDIV:
335         rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
336         break;
337     case FPE_FLTOVF:
338         rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
339         break;
340     case FPE_FLTUND:
341         rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
342         break;
343     case FPE_FLTRES:
344         rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
345         break;
346     case FPE_FLTINV:
347     default:
348         rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
349         break;
350     }
351     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
352     rec.ExceptionRecord  = NULL;
353     rec.ExceptionAddress = (LPVOID)__context->uc_mcontext.regs->nip;
354     rec.NumberParameters = 0;
355     EXC_RtlRaiseException( &rec, &context );
356     restore_context( &context, HANDLER_CONTEXT );
357     /*restore_fpu( &context, HANDLER_CONTEXT );*/
358 }
359
360
361 /**********************************************************************
362  *              int_handler
363  *
364  * Handler for SIGINT.
365  */
366 static HANDLER_DEF(int_handler)
367 {
368     if (!dispatch_signal(SIGINT))
369     {
370         EXCEPTION_RECORD rec;
371         CONTEXT context;
372
373         save_context( &context, HANDLER_CONTEXT );
374         rec.ExceptionCode    = CONTROL_C_EXIT;
375         rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
376         rec.ExceptionRecord  = NULL;
377         rec.ExceptionAddress = (LPVOID)context.Iar;
378         rec.NumberParameters = 0;
379         EXC_RtlRaiseException( &rec, &context );
380         restore_context( &context, HANDLER_CONTEXT );
381     }
382 }
383
384
385 /***********************************************************************
386  *           set_handler
387  *
388  * Set a signal handler
389  */
390 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
391 {
392     struct sigaction sig_act;
393
394     sig_act.sa_sigaction = func;
395     sigemptyset( &sig_act.sa_mask );
396     sigaddset( &sig_act.sa_mask, SIGINT );
397     sigaddset( &sig_act.sa_mask, SIGALRM );
398
399     sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
400
401 #ifdef SA_ONSTACK
402     if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
403 #endif
404     return sigaction( sig, &sig_act, NULL );
405 }
406
407
408 /***********************************************************************
409  *           __wine_set_signal_handler   (NTDLL.@)
410  */
411 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
412 {
413     if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
414     if (handlers[sig] != NULL) return -2;
415     handlers[sig] = wsh;
416     return 0;
417 }
418
419
420 /**********************************************************************
421  *              SIGNAL_Init
422  */
423 BOOL SIGNAL_Init(void)
424 {
425     int have_sigaltstack = 0;
426
427 #ifdef HAVE_SIGALTSTACK
428     struct sigaltstack ss;
429     if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
430     {
431         ss.ss_size  = SIGNAL_STACK_SIZE;
432         ss.ss_flags = 0;
433         if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
434     }
435 #endif  /* HAVE_SIGALTSTACK */
436
437     sigfillset( &all_sigs );
438
439     if (set_handler( SIGINT,  have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
440     if (set_handler( SIGFPE,  have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
441     if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
442     if (set_handler( SIGILL,  have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
443 #ifdef SIGBUS
444     if (set_handler( SIGBUS,  have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
445 #endif
446 #ifdef SIGTRAP
447     if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
448 #endif
449
450     return TRUE;
451
452  error:
453     perror("sigaction");
454     return FALSE;
455 }
456
457
458 /**********************************************************************
459  *              SIGNAL_Reset
460  */
461 void SIGNAL_Reset(void)
462 {
463     sigset_t block_set;
464
465     /* block the async signals */
466     sigemptyset( &block_set );
467     sigaddset( &block_set, SIGALRM );
468     sigaddset( &block_set, SIGIO );
469     sigaddset( &block_set, SIGHUP );
470     sigaddset( &block_set, SIGUSR2 );
471     sigprocmask( SIG_BLOCK, &block_set, NULL );
472
473     /* restore default handlers */
474     signal( SIGINT, SIG_DFL );
475     signal( SIGFPE, SIG_DFL );
476     signal( SIGSEGV, SIG_DFL );
477     signal( SIGILL, SIG_DFL );
478 #ifdef SIGBUS
479     signal( SIGBUS, SIG_DFL );
480 #endif
481 #ifdef SIGTRAP
482     signal( SIGTRAP, SIG_DFL );
483 #endif
484 }
485
486 /**********************************************************************
487  *              __wine_enter_vm86   (NTDLL.@)
488  */
489 void __wine_enter_vm86( CONTEXT *context )
490 {
491     MESSAGE("vm86 mode not supported on this platform\n");
492 }
493
494 /**********************************************************************
495  *              DbgBreakPoint   (NTDLL.@)
496  */
497 void WINAPI DbgBreakPoint(void)
498 {
499      kill(getpid(), SIGTRAP);
500 }
501
502 /**********************************************************************
503  *              DbgUserBreakPoint   (NTDLL.@)
504  */
505 void WINAPI DbgUserBreakPoint(void)
506 {
507      kill(getpid(), SIGTRAP);
508 }
509
510 #endif  /* __powerpc__ */