2 * PowerPC signal handling routines
4 * Copyright 2002 Marcus Meissner, SuSE Linux AG
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
24 #include "wine/port.h"
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
41 # ifdef HAVE_SYS_SYSCALL_H
42 # include <sys/syscall.h>
46 #ifdef HAVE_SYS_VM86_H
47 # include <sys/vm86.h>
50 #ifdef HAVE_SYS_SIGNAL_H
51 # include <sys/signal.h>
56 #include "wine/library.h"
57 #include "wine/exception.h"
58 #include "ntdll_misc.h"
59 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(seh);
63 static pthread_key_t teb_key;
65 /***********************************************************************
66 * signal context platform-specific definitions
70 typedef struct ucontext SIGCONTEXT;
72 # define HANDLER_DEF(name) void name( int __signal, struct siginfo *__siginfo, SIGCONTEXT *__context )
73 # define HANDLER_CONTEXT (__context)
75 /* All Registers access - only for local access */
76 # define REG_sig(reg_name, context) ((context)->uc_mcontext.regs->reg_name)
79 /* Gpr Registers access */
80 # define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
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 */
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 */
90 /* Float Registers access */
91 # define FLOAT_sig(reg_num, context) (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
93 # define FPSCR_sig(context) (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
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)
104 # include <sys/ucontext.h>
106 # include <sys/types.h>
107 typedef siginfo_t siginfo;
109 typedef struct ucontext SIGCONTEXT;
112 # define HANDLER_DEF(name) void name( int __signal, siginfo *__siginfo, SIGCONTEXT *__context )
113 # define HANDLER_CONTEXT (__context)
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)
121 /* Gpr Registers access */
122 # define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
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)
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 */
132 /* Float Registers access */
133 # define FLOAT_sig(reg_num, context) FLOATREG_sig(fpregs[reg_num], context)
135 # define FPSCR_sig(context) FLOATREG_sig(fpscr, context)
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 */
142 /* Signal defs : Those are undefined on darwin
159 #endif /* __APPLE__ */
163 typedef int (*wine_signal_handler)(unsigned int sig);
165 static wine_signal_handler handlers[256];
167 /***********************************************************************
170 static inline int dispatch_signal(unsigned int sig)
172 if (handlers[sig] == NULL) return 0;
173 return handlers[sig](sig);
176 /***********************************************************************
179 * Set the register values from a sigcontext.
181 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
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);
192 context->Iar = IAR_sig(sigcontext); /* Program Counter */
193 context->Msr = MSR_sig(sigcontext); /* Machine State Register (Supervisor) */
194 context->Ctr = CTR_sig(sigcontext);
196 context->Xer = XER_sig(sigcontext);
197 context->Lr = LR_sig(sigcontext);
198 context->Cr = CR_sig(sigcontext);
200 /* Saving Exception regs */
201 context->Dar = DAR_sig(sigcontext);
202 context->Dsisr = DSISR_sig(sigcontext);
203 context->Trap = TRAP_sig(sigcontext);
207 /***********************************************************************
210 * Build a sigcontext from the register values.
212 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
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);
222 IAR_sig(sigcontext) = context->Iar; /* Program Counter */
223 MSR_sig(sigcontext) = context->Msr; /* Machine State Register (Supervisor) */
224 CTR_sig(sigcontext) = context->Ctr;
226 XER_sig(sigcontext) = context->Xer;
227 LR_sig(sigcontext) = context->Lr;
228 CR_sig(sigcontext) = context->Cr;
230 /* Setting Exception regs */
231 DAR_sig(sigcontext) = context->Dar;
232 DSISR_sig(sigcontext) = context->Dsisr;
233 TRAP_sig(sigcontext) = context->Trap;
237 /***********************************************************************
240 * Set the FPU context from a sigcontext.
242 static inline void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
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);
250 context->Fpscr = FPSCR_sig(sigcontext);
254 /***********************************************************************
257 * Restore the FPU context to a sigcontext.
259 static inline void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
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);
267 FPSCR_sig(sigcontext) = context->Fpscr;
271 /***********************************************************************
272 * RtlCaptureContext (NTDLL.@)
274 void WINAPI RtlCaptureContext( CONTEXT *context )
276 FIXME("not implemented\n");
277 memset( context, 0, sizeof(*context) );
281 /***********************************************************************
284 * Set the new CPU context.
286 void set_cpu_context( const CONTEXT *context )
288 FIXME("not implemented\n");
292 /***********************************************************************
295 * Copy a register context according to the flags.
297 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
299 if (flags & CONTEXT_CONTROL)
305 if (flags & CONTEXT_INTEGER)
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;
342 if (flags & CONTEXT_FLOATING_POINT)
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;
381 /**********************************************************************
384 * Get the FPU exception code from the FPU status.
386 static inline DWORD get_fpu_code( const CONTEXT *context )
388 DWORD status = context->Fpscr;
390 if (status & 0x01) /* IE */
392 if (status & 0x40) /* SF */
393 return EXCEPTION_FLT_STACK_CHECK;
395 return EXCEPTION_FLT_INVALID_OPERATION;
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 */
405 /**********************************************************************
408 * Implementation of SIGSEGV handler.
410 static void do_segv( CONTEXT *context, int trap, int err, int code, void * addr )
412 EXCEPTION_RECORD rec;
414 rec.ExceptionRecord = NULL;
415 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
416 rec.ExceptionAddress = addr;
417 rec.NumberParameters = 0;
421 switch ( code & 0xffff ) {
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])))
430 default:FIXME("Unhandled SIGSEGV/%x\n",code);
435 switch ( code & 0xffff ) {
437 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
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])))
452 default:FIXME("Unhandled SIGBUS/%x\n",code);
457 switch ( code & 0xffff ) {
458 case ILL_ILLOPC: /* illegal opcode */
460 case ILL_ILLOPN: /* illegal operand */
463 case ILL_ILLADR: /* illegal addressing mode */
466 case ILL_ILLTRP: /* illegal trap */
469 case ILL_COPROC: /* coprocessor error */
471 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
473 case ILL_PRVOPC: /* privileged opcode */
475 case ILL_PRVREG: /* privileged register */
477 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
480 case ILL_BADSTK: /* internal stack error */
481 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
484 default:FIXME("Unhandled SIGILL/%x\n", code);
489 __regs_RtlRaiseException( &rec, context );
492 /**********************************************************************
495 * Implementation of SIGTRAP handler.
497 static void do_trap( CONTEXT *context, int code, void * addr )
499 EXCEPTION_RECORD rec;
501 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
502 rec.ExceptionRecord = NULL;
503 rec.ExceptionAddress = addr;
504 rec.NumberParameters = 0;
506 /* FIXME: check if we might need to modify PC */
507 switch (code & 0xffff) {
510 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
515 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
518 default:FIXME("Unhandled SIGTRAP/%x\n", code);
521 __regs_RtlRaiseException( &rec, context );
524 /**********************************************************************
527 * Implementation of SIGFPE handler.
529 static void do_fpe( CONTEXT *context, int code, void * addr )
531 EXCEPTION_RECORD rec;
533 switch ( code & 0xffff ) {
536 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
541 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
546 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
551 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
556 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
561 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
566 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
573 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
576 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
577 rec.ExceptionRecord = NULL;
578 rec.ExceptionAddress = addr;
579 rec.NumberParameters = 0;
580 __regs_RtlRaiseException( &rec, context );
583 /**********************************************************************
586 * Handler for SIGSEGV and related errors.
588 static HANDLER_DEF(segv_handler)
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 );
596 /**********************************************************************
599 * Handler for SIGTRAP.
601 static HANDLER_DEF(trap_handler)
604 save_context( &context, HANDLER_CONTEXT );
605 do_trap( &context, __siginfo->si_code, __siginfo->si_addr );
606 restore_context( &context, HANDLER_CONTEXT );
609 /**********************************************************************
612 * Handler for SIGFPE.
614 static HANDLER_DEF(fpe_handler)
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 );
624 /**********************************************************************
627 * Handler for SIGINT.
629 static HANDLER_DEF(int_handler)
631 if (!dispatch_signal(SIGINT))
633 EXCEPTION_RECORD rec;
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 );
648 /**********************************************************************
651 * Handler for SIGABRT.
653 static HANDLER_DEF(abrt_handler)
655 EXCEPTION_RECORD rec;
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 );
669 /**********************************************************************
672 * Handler for SIGQUIT.
674 static HANDLER_DEF(quit_handler)
680 /**********************************************************************
683 * Handler for SIGUSR1, used to signal a thread that it got suspended.
685 static HANDLER_DEF(usr1_handler)
689 save_context( &context, HANDLER_CONTEXT );
690 wait_suspend( &context );
691 restore_context( &context, HANDLER_CONTEXT );
695 /**********************************************************************
696 * get_signal_stack_total_size
698 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
699 * Must be a power of two.
701 size_t get_signal_stack_total_size(void)
703 assert( sizeof(TEB) <= getpagesize() );
704 return getpagesize(); /* this is just for the TEB, we don't need a signal stack */
708 /***********************************************************************
711 * Set a signal handler
713 static int set_handler( int sig, void (*func)() )
715 struct sigaction sig_act;
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 );
724 /***********************************************************************
725 * __wine_set_signal_handler (NTDLL.@)
727 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
729 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
730 if (handlers[sig] != NULL) return -2;
736 /**********************************************************************
739 void signal_init_thread( TEB *teb )
741 static int init_done;
745 pthread_key_create( &teb_key, NULL );
748 pthread_setspecific( teb_key, teb );
752 /**********************************************************************
753 * signal_init_process
755 void signal_init_process(void)
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;
765 if (set_handler( SIGBUS, (void (*)())segv_handler ) == -1) goto error;
768 if (set_handler( SIGTRAP, (void (*)())trap_handler ) == -1) goto error;
778 /**********************************************************************
779 * __wine_enter_vm86 (NTDLL.@)
781 void __wine_enter_vm86( CONTEXT *context )
783 MESSAGE("vm86 mode not supported on this platform\n");
786 /**********************************************************************
787 * DbgBreakPoint (NTDLL.@)
789 void WINAPI DbgBreakPoint(void)
791 kill(getpid(), SIGTRAP);
794 /**********************************************************************
795 * DbgUserBreakPoint (NTDLL.@)
797 void WINAPI DbgUserBreakPoint(void)
799 kill(getpid(), SIGTRAP);
802 /**********************************************************************
803 * NtCurrentTeb (NTDLL.@)
805 TEB * WINAPI NtCurrentTeb(void)
807 return pthread_getspecific( teb_key );
810 #endif /* __powerpc__ */