credui: Add the Romanian translation.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #ifdef __powerpc__
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <assert.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
37 #endif
38 #ifdef HAVE_SYSCALL_H
39 # include <syscall.h>
40 #else
41 # ifdef HAVE_SYS_SYSCALL_H
42 #  include <sys/syscall.h>
43 # endif
44 #endif
45
46 #ifdef HAVE_SYS_VM86_H
47 # include <sys/vm86.h>
48 #endif
49
50 #ifdef HAVE_SYS_SIGNAL_H
51 # include <sys/signal.h>
52 #endif
53
54 #include "windef.h"
55 #include "winternl.h"
56 #include "wine/library.h"
57 #include "wine/exception.h"
58 #include "ntdll_misc.h"
59 #include "wine/debug.h"
60
61 WINE_DEFAULT_DEBUG_CHANNEL(seh);
62
63 static pthread_key_t teb_key;
64
65 /***********************************************************************
66  * signal context platform-specific definitions
67  */
68 #ifdef linux
69
70 typedef struct ucontext SIGCONTEXT;
71
72 # define HANDLER_DEF(name) void name( int __signal, struct siginfo *__siginfo, SIGCONTEXT *__context )
73 # define HANDLER_CONTEXT (__context)
74
75 /* All Registers access - only for local access */
76 # define REG_sig(reg_name, context)             ((context)->uc_mcontext.regs->reg_name)
77
78
79 /* Gpr Registers access  */
80 # define GPR_sig(reg_num, context)              REG_sig(gpr[reg_num], context)
81
82 # define IAR_sig(context)                       REG_sig(nip, context)   /* Program counter */
83 # define MSR_sig(context)                       REG_sig(msr, context)   /* Machine State Register (Supervisor) */
84 # define CTR_sig(context)                       REG_sig(ctr, context)   /* Count register */
85
86 # define XER_sig(context)                       REG_sig(xer, context) /* User's integer exception register */
87 # define LR_sig(context)                        REG_sig(link, context) /* Link register */
88 # define CR_sig(context)                        REG_sig(ccr, context) /* Condition register */
89
90 /* Float Registers access  */
91 # define FLOAT_sig(reg_num, context)            (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
92
93 # define FPSCR_sig(context)                     (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
94
95 /* Exception Registers access */
96 # define DAR_sig(context)                       REG_sig(dar, context)
97 # define DSISR_sig(context)                     REG_sig(dsisr, context)
98 # define TRAP_sig(context)                      REG_sig(trap, context)
99
100 #endif /* linux */
101
102 #ifdef __APPLE__
103
104 # include <sys/ucontext.h>
105
106 # include <sys/types.h>
107 typedef siginfo_t siginfo;
108
109 typedef struct ucontext SIGCONTEXT;
110
111
112 # define HANDLER_DEF(name) void name( int __signal, siginfo *__siginfo, SIGCONTEXT *__context )
113 # define HANDLER_CONTEXT (__context)
114
115 /* All Registers access - only for local access */
116 # define REG_sig(reg_name, context)             ((context)->uc_mcontext->ss.reg_name)
117 # define FLOATREG_sig(reg_name, context)        ((context)->uc_mcontext->fs.reg_name)
118 # define EXCEPREG_sig(reg_name, context)        ((context)->uc_mcontext->es.reg_name)
119 # define VECREG_sig(reg_name, context)          ((context)->uc_mcontext->vs.reg_name)
120
121 /* Gpr Registers access */
122 # define GPR_sig(reg_num, context)              REG_sig(r##reg_num, context)
123
124 # define IAR_sig(context)                       REG_sig(srr0, context)  /* Program counter */
125 # define MSR_sig(context)                       REG_sig(srr1, context)  /* Machine State Register (Supervisor) */
126 # define CTR_sig(context)                       REG_sig(ctr, context)
127
128 # define XER_sig(context)                       REG_sig(xer, context) /* Link register */
129 # define LR_sig(context)                        REG_sig(lr, context)  /* User's integer exception register */
130 # define CR_sig(context)                        REG_sig(cr, context)  /* Condition register */
131
132 /* Float Registers access */
133 # define FLOAT_sig(reg_num, context)            FLOATREG_sig(fpregs[reg_num], context)
134
135 # define FPSCR_sig(context)                     FLOATREG_sig(fpscr, context)
136
137 /* Exception Registers access */
138 # define DAR_sig(context)                       EXCEPREG_sig(dar, context)     /* Fault registers for coredump */
139 # define DSISR_sig(context)                     EXCEPREG_sig(dsisr, context)
140 # define TRAP_sig(context)                      EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
141
142 /* Signal defs : Those are undefined on darwin
143 SIGBUS
144 #undef BUS_ADRERR
145 #undef BUS_OBJERR
146 SIGILL
147 #undef ILL_ILLOPN
148 #undef ILL_ILLTRP
149 #undef ILL_ILLADR
150 #undef ILL_COPROC
151 #undef ILL_PRVREG
152 #undef ILL_BADSTK
153 SIGTRAP
154 #undef TRAP_BRKPT
155 #undef TRAP_TRACE
156 SIGFPE
157 */
158
159 #endif /* __APPLE__ */
160
161
162
163 typedef int (*wine_signal_handler)(unsigned int sig);
164
165 static wine_signal_handler handlers[256];
166
167 /***********************************************************************
168  *           dispatch_signal
169  */
170 static inline int dispatch_signal(unsigned int sig)
171 {
172     if (handlers[sig] == NULL) return 0;
173     return handlers[sig](sig);
174 }
175
176 /***********************************************************************
177  *           save_context
178  *
179  * Set the register values from a sigcontext.
180  */
181 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
182 {
183
184 #define C(x) context->Gpr##x = GPR_sig(x,sigcontext)
185         /* Save Gpr registers */
186         C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
187         C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
188         C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
189         C(31);
190 #undef C
191
192         context->Iar = IAR_sig(sigcontext);  /* Program Counter */
193         context->Msr = MSR_sig(sigcontext);  /* Machine State Register (Supervisor) */
194         context->Ctr = CTR_sig(sigcontext);
195         
196         context->Xer = XER_sig(sigcontext);
197         context->Lr  = LR_sig(sigcontext);
198         context->Cr  = CR_sig(sigcontext);
199         
200         /* Saving Exception regs */
201         context->Dar   = DAR_sig(sigcontext);
202         context->Dsisr = DSISR_sig(sigcontext);
203         context->Trap  = TRAP_sig(sigcontext);
204 }
205
206
207 /***********************************************************************
208  *           restore_context
209  *
210  * Build a sigcontext from the register values.
211  */
212 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
213 {
214
215 #define C(x)  GPR_sig(x,sigcontext) = context->Gpr##x
216         C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
217         C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
218         C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
219         C(31);
220 #undef C
221
222         IAR_sig(sigcontext) = context->Iar;  /* Program Counter */
223         MSR_sig(sigcontext) = context->Msr;  /* Machine State Register (Supervisor) */
224         CTR_sig(sigcontext) = context->Ctr;
225         
226         XER_sig(sigcontext) = context->Xer;
227         LR_sig(sigcontext) = context->Lr;
228         CR_sig(sigcontext) = context->Cr;
229         
230         /* Setting Exception regs */
231         DAR_sig(sigcontext) = context->Dar;
232         DSISR_sig(sigcontext) = context->Dsisr;
233         TRAP_sig(sigcontext) = context->Trap;
234 }
235
236
237 /***********************************************************************
238  *           save_fpu
239  *
240  * Set the FPU context from a sigcontext.
241  */
242 static inline void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
243 {
244 #define C(x)   context->Fpr##x = FLOAT_sig(x,sigcontext)
245         C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
246         C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
247         C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
248         C(31);
249 #undef C
250         context->Fpscr = FPSCR_sig(sigcontext);
251 }
252
253
254 /***********************************************************************
255  *           restore_fpu
256  *
257  * Restore the FPU context to a sigcontext.
258  */
259 static inline void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
260 {
261 #define C(x)  FLOAT_sig(x,sigcontext) = context->Fpr##x
262         C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
263         C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
264         C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
265         C(31);
266 #undef C
267         FPSCR_sig(sigcontext) = context->Fpscr;
268 }
269
270
271 /***********************************************************************
272  *              RtlCaptureContext (NTDLL.@)
273  */
274 void WINAPI RtlCaptureContext( CONTEXT *context )
275 {
276     FIXME("not implemented\n");
277     memset( context, 0, sizeof(*context) );
278 }
279
280
281 /***********************************************************************
282  *           set_cpu_context
283  *
284  * Set the new CPU context.
285  */
286 void set_cpu_context( const CONTEXT *context )
287 {
288     FIXME("not implemented\n");
289 }
290
291
292 /***********************************************************************
293  *           copy_context
294  *
295  * Copy a register context according to the flags.
296  */
297 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
298 {
299     if (flags & CONTEXT_CONTROL)
300     {
301         to->Msr = from->Msr;
302         to->Ctr = from->Ctr;
303         to->Iar = from->Iar;
304     }
305     if (flags & CONTEXT_INTEGER)
306     {
307         to->Gpr0  = from->Gpr0;
308         to->Gpr1  = from->Gpr1;
309         to->Gpr2  = from->Gpr2;
310         to->Gpr3  = from->Gpr3;
311         to->Gpr4  = from->Gpr4;
312         to->Gpr5  = from->Gpr5;
313         to->Gpr6  = from->Gpr6;
314         to->Gpr7  = from->Gpr7;
315         to->Gpr8  = from->Gpr8;
316         to->Gpr9  = from->Gpr9;
317         to->Gpr10 = from->Gpr10;
318         to->Gpr11 = from->Gpr11;
319         to->Gpr12 = from->Gpr12;
320         to->Gpr13 = from->Gpr13;
321         to->Gpr14 = from->Gpr14;
322         to->Gpr15 = from->Gpr15;
323         to->Gpr16 = from->Gpr16;
324         to->Gpr17 = from->Gpr17;
325         to->Gpr18 = from->Gpr18;
326         to->Gpr19 = from->Gpr19;
327         to->Gpr20 = from->Gpr20;
328         to->Gpr21 = from->Gpr21;
329         to->Gpr22 = from->Gpr22;
330         to->Gpr23 = from->Gpr23;
331         to->Gpr24 = from->Gpr24;
332         to->Gpr25 = from->Gpr25;
333         to->Gpr26 = from->Gpr26;
334         to->Gpr27 = from->Gpr27;
335         to->Gpr28 = from->Gpr28;
336         to->Gpr29 = from->Gpr29;
337         to->Gpr30 = from->Gpr30;
338         to->Gpr31 = from->Gpr31;
339         to->Xer   = from->Xer;
340         to->Cr    = from->Cr;
341     }
342     if (flags & CONTEXT_FLOATING_POINT)
343     {
344         to->Fpr0  = from->Fpr0;
345         to->Fpr1  = from->Fpr1;
346         to->Fpr2  = from->Fpr2;
347         to->Fpr3  = from->Fpr3;
348         to->Fpr4  = from->Fpr4;
349         to->Fpr5  = from->Fpr5;
350         to->Fpr6  = from->Fpr6;
351         to->Fpr7  = from->Fpr7;
352         to->Fpr8  = from->Fpr8;
353         to->Fpr9  = from->Fpr9;
354         to->Fpr10 = from->Fpr10;
355         to->Fpr11 = from->Fpr11;
356         to->Fpr12 = from->Fpr12;
357         to->Fpr13 = from->Fpr13;
358         to->Fpr14 = from->Fpr14;
359         to->Fpr15 = from->Fpr15;
360         to->Fpr16 = from->Fpr16;
361         to->Fpr17 = from->Fpr17;
362         to->Fpr18 = from->Fpr18;
363         to->Fpr19 = from->Fpr19;
364         to->Fpr20 = from->Fpr20;
365         to->Fpr21 = from->Fpr21;
366         to->Fpr22 = from->Fpr22;
367         to->Fpr23 = from->Fpr23;
368         to->Fpr24 = from->Fpr24;
369         to->Fpr25 = from->Fpr25;
370         to->Fpr26 = from->Fpr26;
371         to->Fpr27 = from->Fpr27;
372         to->Fpr28 = from->Fpr28;
373         to->Fpr29 = from->Fpr29;
374         to->Fpr30 = from->Fpr30;
375         to->Fpr31 = from->Fpr31;
376         to->Fpscr = from->Fpscr;
377     }
378 }
379
380
381 /**********************************************************************
382  *              get_fpu_code
383  *
384  * Get the FPU exception code from the FPU status.
385  */
386 static inline DWORD get_fpu_code( const CONTEXT *context )
387 {
388     DWORD status  = context->Fpscr;
389
390     if (status & 0x01)  /* IE */
391     {
392         if (status & 0x40)  /* SF */
393             return EXCEPTION_FLT_STACK_CHECK;
394         else
395             return EXCEPTION_FLT_INVALID_OPERATION;
396     }
397     if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND;  /* DE flag */
398     if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO;    /* ZE flag */
399     if (status & 0x08) return EXCEPTION_FLT_OVERFLOW;          /* OE flag */
400     if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW;         /* UE flag */
401     if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT;    /* PE flag */
402     return EXCEPTION_FLT_INVALID_OPERATION;  /* generic error */
403 }
404
405 /**********************************************************************
406  *              do_segv
407  *
408  * Implementation of SIGSEGV handler.
409  */
410 static void do_segv( CONTEXT *context, int trap, int err, int code, void * addr )
411 {
412     EXCEPTION_RECORD rec;
413
414     rec.ExceptionRecord  = NULL;
415     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
416     rec.ExceptionAddress = addr;
417     rec.NumberParameters = 0;
418     
419     switch (trap) {
420     case SIGSEGV:
421         switch ( code & 0xffff ) {
422         case SEGV_MAPERR:
423         case SEGV_ACCERR:
424                 rec.NumberParameters = 2;
425                 rec.ExceptionInformation[0] = 0; /* FIXME ? */
426                 rec.ExceptionInformation[1] = (ULONG_PTR)addr;
427                 if (!(rec.ExceptionCode = virtual_handle_fault(addr, rec.ExceptionInformation[0])))
428                         return;
429                 break;
430         default:FIXME("Unhandled SIGSEGV/%x\n",code);
431                 break;
432         }
433         break;
434     case SIGBUS:
435         switch ( code & 0xffff ) {
436         case BUS_ADRALN:
437                 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
438                 break;
439 #ifdef BUS_ADRERR
440         case BUS_ADRERR:
441 #endif
442 #ifdef BUS_OBJERR
443         case BUS_OBJERR:
444                 /* FIXME: correct for all cases ? */
445                 rec.NumberParameters = 2;
446                 rec.ExceptionInformation[0] = 0; /* FIXME ? */
447                 rec.ExceptionInformation[1] = (ULONG_PTR)addr;
448                 if (!(rec.ExceptionCode = virtual_handle_fault(addr, rec.ExceptionInformation[0])))
449                         return;
450                 break;
451 #endif
452         default:FIXME("Unhandled SIGBUS/%x\n",code);
453                 break;
454         }
455         break;
456     case SIGILL:
457         switch ( code & 0xffff ) {
458         case ILL_ILLOPC: /* illegal opcode */
459 #ifdef ILL_ILLOPN
460         case ILL_ILLOPN: /* illegal operand */
461 #endif
462 #ifdef ILL_ILLADR
463         case ILL_ILLADR: /* illegal addressing mode */
464 #endif
465 #ifdef ILL_ILLTRP
466         case ILL_ILLTRP: /* illegal trap */
467 #endif
468 #ifdef ILL_COPROC
469         case ILL_COPROC: /* coprocessor error */
470 #endif
471                 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
472                 break;
473         case ILL_PRVOPC: /* privileged opcode */
474 #ifdef ILL_PRVREG
475         case ILL_PRVREG: /* privileged register */
476 #endif
477                 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
478                 break;
479 #ifdef ILL_BADSTK
480         case ILL_BADSTK: /* internal stack error */
481                 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
482                 break;
483 #endif
484         default:FIXME("Unhandled SIGILL/%x\n", code);
485                 break;
486         }
487         break;
488     }
489     __regs_RtlRaiseException( &rec, context );
490 }
491
492 /**********************************************************************
493  *              do_trap
494  *
495  * Implementation of SIGTRAP handler.
496  */
497 static void do_trap( CONTEXT *context, int code, void * addr )
498 {
499     EXCEPTION_RECORD rec;
500
501     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
502     rec.ExceptionRecord  = NULL;
503     rec.ExceptionAddress = addr;
504     rec.NumberParameters = 0;
505
506     /* FIXME: check if we might need to modify PC */
507     switch (code & 0xffff) {
508 #ifdef TRAP_BRKPT
509     case TRAP_BRKPT:
510         rec.ExceptionCode = EXCEPTION_BREAKPOINT;
511         break;
512 #endif
513 #ifdef TRAP_TRACE
514     case TRAP_TRACE:
515         rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
516         break;
517 #endif
518     default:FIXME("Unhandled SIGTRAP/%x\n", code);
519                 break;
520     }
521     __regs_RtlRaiseException( &rec, context );
522 }
523
524 /**********************************************************************
525  *              do_trap
526  *
527  * Implementation of SIGFPE handler.
528  */
529 static void do_fpe( CONTEXT *context, int code, void * addr )
530 {
531     EXCEPTION_RECORD rec;
532
533     switch ( code  & 0xffff ) {
534 #ifdef FPE_FLTSUB
535     case FPE_FLTSUB:
536         rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
537         break;
538 #endif
539 #ifdef FPE_INTDIV
540     case FPE_INTDIV:
541         rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
542         break;
543 #endif
544 #ifdef FPE_INTOVF
545     case FPE_INTOVF:
546         rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
547         break;
548 #endif
549 #ifdef FPE_FLTDIV
550     case FPE_FLTDIV:
551         rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
552         break;
553 #endif
554 #ifdef FPE_FLTOVF
555     case FPE_FLTOVF:
556         rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
557         break;
558 #endif
559 #ifdef FPE_FLTUND
560     case FPE_FLTUND:
561         rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
562         break;
563 #endif
564 #ifdef FPE_FLTRES
565     case FPE_FLTRES:
566         rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
567         break;
568 #endif
569 #ifdef FPE_FLTINV
570     case FPE_FLTINV:
571 #endif
572     default:
573         rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
574         break;
575     }
576     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
577     rec.ExceptionRecord  = NULL;
578     rec.ExceptionAddress = addr;
579     rec.NumberParameters = 0;
580     __regs_RtlRaiseException( &rec, context );
581 }
582
583 /**********************************************************************
584  *              segv_handler
585  *
586  * Handler for SIGSEGV and related errors.
587  */
588 static HANDLER_DEF(segv_handler)
589 {
590     CONTEXT context;
591     save_context( &context, HANDLER_CONTEXT );
592     do_segv( &context, __siginfo->si_signo, __siginfo->si_errno, __siginfo->si_code, __siginfo->si_addr );
593     restore_context( &context, HANDLER_CONTEXT );
594 }
595
596 /**********************************************************************
597  *              trap_handler
598  *
599  * Handler for SIGTRAP.
600  */
601 static HANDLER_DEF(trap_handler)
602 {
603     CONTEXT context;
604     save_context( &context, HANDLER_CONTEXT );
605     do_trap( &context, __siginfo->si_code, __siginfo->si_addr );
606     restore_context( &context, HANDLER_CONTEXT );
607 }
608
609 /**********************************************************************
610  *              fpe_handler
611  *
612  * Handler for SIGFPE.
613  */
614 static HANDLER_DEF(fpe_handler)
615 {
616     CONTEXT context;
617     save_fpu( &context, HANDLER_CONTEXT );
618     save_context( &context, HANDLER_CONTEXT );
619     do_fpe( &context,  __siginfo->si_code, __siginfo->si_addr );
620     restore_context( &context, HANDLER_CONTEXT );
621     restore_fpu( &context, HANDLER_CONTEXT );
622 }
623
624 /**********************************************************************
625  *              int_handler
626  *
627  * Handler for SIGINT.
628  */
629 static HANDLER_DEF(int_handler)
630 {
631     if (!dispatch_signal(SIGINT))
632     {
633         EXCEPTION_RECORD rec;
634         CONTEXT context;
635
636         save_context( &context, HANDLER_CONTEXT );
637         rec.ExceptionCode    = CONTROL_C_EXIT;
638         rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
639         rec.ExceptionRecord  = NULL;
640         rec.ExceptionAddress = (LPVOID)context.Iar;
641         rec.NumberParameters = 0;
642         __regs_RtlRaiseException( &rec, &context );
643         restore_context( &context, HANDLER_CONTEXT );
644     }
645 }
646
647
648 /**********************************************************************
649  *              abrt_handler
650  *
651  * Handler for SIGABRT.
652  */
653 static HANDLER_DEF(abrt_handler)
654 {
655     EXCEPTION_RECORD rec;
656     CONTEXT context;
657
658     save_context( &context, HANDLER_CONTEXT );
659     rec.ExceptionCode    = EXCEPTION_WINE_ASSERTION;
660     rec.ExceptionFlags   = EH_NONCONTINUABLE;
661     rec.ExceptionRecord  = NULL;
662     rec.ExceptionAddress = (LPVOID)context.Iar;
663     rec.NumberParameters = 0;
664     __regs_RtlRaiseException( &rec, &context ); /* Should never return.. */
665     restore_context( &context, HANDLER_CONTEXT );
666 }
667
668
669 /**********************************************************************
670  *              quit_handler
671  *
672  * Handler for SIGQUIT.
673  */
674 static HANDLER_DEF(quit_handler)
675 {
676     abort_thread(0);
677 }
678
679
680 /**********************************************************************
681  *              usr1_handler
682  *
683  * Handler for SIGUSR1, used to signal a thread that it got suspended.
684  */
685 static HANDLER_DEF(usr1_handler)
686 {
687     CONTEXT context;
688
689     save_context( &context, HANDLER_CONTEXT );
690     wait_suspend( &context );
691     restore_context( &context, HANDLER_CONTEXT );
692 }
693
694
695 /**********************************************************************
696  *              get_signal_stack_total_size
697  *
698  * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
699  * Must be a power of two.
700  */
701 size_t get_signal_stack_total_size(void)
702 {
703     assert( sizeof(TEB) <= getpagesize() );
704     return getpagesize();  /* this is just for the TEB, we don't need a signal stack */
705 }
706
707
708 /***********************************************************************
709  *           set_handler
710  *
711  * Set a signal handler
712  */
713 static int set_handler( int sig, void (*func)() )
714 {
715     struct sigaction sig_act;
716
717     sig_act.sa_sigaction = func;
718     sig_act.sa_mask = server_block_set;
719     sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
720     return sigaction( sig, &sig_act, NULL );
721 }
722
723
724 /***********************************************************************
725  *           __wine_set_signal_handler   (NTDLL.@)
726  */
727 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
728 {
729     if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
730     if (handlers[sig] != NULL) return -2;
731     handlers[sig] = wsh;
732     return 0;
733 }
734
735
736 /**********************************************************************
737  *              signal_init_thread
738  */
739 void signal_init_thread( TEB *teb )
740 {
741     static int init_done;
742
743     if (!init_done)
744     {
745         pthread_key_create( &teb_key, NULL );
746         init_done = 1;
747     }
748     pthread_setspecific( teb_key, teb );
749 }
750
751
752 /**********************************************************************
753  *              signal_init_process
754  */
755 void signal_init_process(void)
756 {
757     if (set_handler( SIGINT,  (void (*)())int_handler ) == -1) goto error;
758     if (set_handler( SIGFPE,  (void (*)())fpe_handler ) == -1) goto error;
759     if (set_handler( SIGSEGV, (void (*)())segv_handler ) == -1) goto error;
760     if (set_handler( SIGILL,  (void (*)())segv_handler ) == -1) goto error;
761     if (set_handler( SIGABRT, (void (*)())abrt_handler ) == -1) goto error;
762     if (set_handler( SIGQUIT, (void (*)())quit_handler ) == -1) goto error;
763     if (set_handler( SIGUSR1, (void (*)())usr1_handler ) == -1) goto error;
764 #ifdef SIGBUS
765     if (set_handler( SIGBUS,  (void (*)())segv_handler ) == -1) goto error;
766 #endif
767 #ifdef SIGTRAP
768     if (set_handler( SIGTRAP, (void (*)())trap_handler ) == -1) goto error;
769 #endif
770     return;
771
772  error:
773     perror("sigaction");
774     exit(1);
775 }
776
777
778 /**********************************************************************
779  *              __wine_enter_vm86   (NTDLL.@)
780  */
781 void __wine_enter_vm86( CONTEXT *context )
782 {
783     MESSAGE("vm86 mode not supported on this platform\n");
784 }
785
786 /**********************************************************************
787  *              DbgBreakPoint   (NTDLL.@)
788  */
789 void WINAPI DbgBreakPoint(void)
790 {
791      kill(getpid(), SIGTRAP);
792 }
793
794 /**********************************************************************
795  *              DbgUserBreakPoint   (NTDLL.@)
796  */
797 void WINAPI DbgUserBreakPoint(void)
798 {
799      kill(getpid(), SIGTRAP);
800 }
801
802 /**********************************************************************
803  *           NtCurrentTeb   (NTDLL.@)
804  */
805 TEB * WINAPI NtCurrentTeb(void)
806 {
807     return pthread_getspecific( teb_key );
808 }
809
810 #endif  /* __powerpc__ */