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);
64 /***********************************************************************
65 * signal context platform-specific definitions
69 typedef struct ucontext SIGCONTEXT;
71 # define HANDLER_DEF(name) void name( int __signal, struct siginfo *__siginfo, SIGCONTEXT *__context )
72 # define HANDLER_CONTEXT (__context)
74 /* All Registers access - only for local access */
75 # define REG_sig(reg_name, context) ((context)->uc_mcontext.regs->reg_name)
78 /* Gpr Registers access */
79 # define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
81 # define IAR_sig(context) REG_sig(nip, context) /* Program counter */
82 # define MSR_sig(context) REG_sig(msr, context) /* Machine State Register (Supervisor) */
83 # define CTR_sig(context) REG_sig(ctr, context) /* Count register */
85 # define XER_sig(context) REG_sig(xer, context) /* User's integer exception register */
86 # define LR_sig(context) REG_sig(link, context) /* Link register */
87 # define CR_sig(context) REG_sig(ccr, context) /* Condition register */
89 /* Float Registers access */
90 # define FLOAT_sig(reg_num, context) (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
92 # define FPSCR_sig(context) (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
94 /* Exception Registers access */
95 # define DAR_sig(context) REG_sig(dar, context)
96 # define DSISR_sig(context) REG_sig(dsisr, context)
97 # define TRAP_sig(context) REG_sig(trap, context)
103 # include <sys/ucontext.h>
105 # include <sys/types.h>
106 typedef siginfo_t siginfo;
108 typedef struct ucontext SIGCONTEXT;
111 # define HANDLER_DEF(name) void name( int __signal, siginfo *__siginfo, SIGCONTEXT *__context )
112 # define HANDLER_CONTEXT (__context)
114 /* All Registers access - only for local access */
115 # define REG_sig(reg_name, context) ((context)->uc_mcontext->ss.reg_name)
116 # define FLOATREG_sig(reg_name, context) ((context)->uc_mcontext->fs.reg_name)
117 # define EXCEPREG_sig(reg_name, context) ((context)->uc_mcontext->es.reg_name)
118 # define VECREG_sig(reg_name, context) ((context)->uc_mcontext->vs.reg_name)
120 /* Gpr Registers access */
121 # define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
123 # define IAR_sig(context) REG_sig(srr0, context) /* Program counter */
124 # define MSR_sig(context) REG_sig(srr1, context) /* Machine State Register (Supervisor) */
125 # define CTR_sig(context) REG_sig(ctr, context)
127 # define XER_sig(context) REG_sig(xer, context) /* Link register */
128 # define LR_sig(context) REG_sig(lr, context) /* User's integer exception register */
129 # define CR_sig(context) REG_sig(cr, context) /* Condition register */
131 /* Float Registers access */
132 # define FLOAT_sig(reg_num, context) FLOATREG_sig(fpregs[reg_num], context)
134 # define FPSCR_sig(context) FLOATREG_sig(fpscr, context)
136 /* Exception Registers access */
137 # define DAR_sig(context) EXCEPREG_sig(dar, context) /* Fault registers for coredump */
138 # define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
139 # define TRAP_sig(context) EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
141 /* Signal defs : Those are undefined on darwin
158 #endif /* __APPLE__ */
162 typedef int (*wine_signal_handler)(unsigned int sig);
164 static wine_signal_handler handlers[256];
166 /***********************************************************************
169 static inline int dispatch_signal(unsigned int sig)
171 if (handlers[sig] == NULL) return 0;
172 return handlers[sig](sig);
175 /***********************************************************************
178 * Set the register values from a sigcontext.
180 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
183 #define C(x) context->Gpr##x = GPR_sig(x,sigcontext)
184 /* Save Gpr registers */
185 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
186 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
187 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
191 context->Iar = IAR_sig(sigcontext); /* Program Counter */
192 context->Msr = MSR_sig(sigcontext); /* Machine State Register (Supervisor) */
193 context->Ctr = CTR_sig(sigcontext);
195 context->Xer = XER_sig(sigcontext);
196 context->Lr = LR_sig(sigcontext);
197 context->Cr = CR_sig(sigcontext);
199 /* Saving Exception regs */
200 context->Dar = DAR_sig(sigcontext);
201 context->Dsisr = DSISR_sig(sigcontext);
202 context->Trap = TRAP_sig(sigcontext);
206 /***********************************************************************
209 * Build a sigcontext from the register values.
211 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
214 #define C(x) GPR_sig(x,sigcontext) = context->Gpr##x
215 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
216 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
217 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
221 IAR_sig(sigcontext) = context->Iar; /* Program Counter */
222 MSR_sig(sigcontext) = context->Msr; /* Machine State Register (Supervisor) */
223 CTR_sig(sigcontext) = context->Ctr;
225 XER_sig(sigcontext) = context->Xer;
226 LR_sig(sigcontext) = context->Lr;
227 CR_sig(sigcontext) = context->Cr;
229 /* Setting Exception regs */
230 DAR_sig(sigcontext) = context->Dar;
231 DSISR_sig(sigcontext) = context->Dsisr;
232 TRAP_sig(sigcontext) = context->Trap;
236 /***********************************************************************
239 * Set the FPU context from a sigcontext.
241 static inline void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
243 #define C(x) context->Fpr##x = FLOAT_sig(x,sigcontext)
244 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
245 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
246 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
249 context->Fpscr = FPSCR_sig(sigcontext);
253 /***********************************************************************
256 * Restore the FPU context to a sigcontext.
258 static inline void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
260 #define C(x) FLOAT_sig(x,sigcontext) = context->Fpr##x
261 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
262 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
263 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
266 FPSCR_sig(sigcontext) = context->Fpscr;
270 /***********************************************************************
271 * RtlCaptureContext (NTDLL.@)
273 void WINAPI RtlCaptureContext( CONTEXT *context )
275 FIXME("not implemented\n");
276 memset( context, 0, sizeof(*context) );
280 /***********************************************************************
283 * Set the new CPU context.
285 void set_cpu_context( const CONTEXT *context )
287 FIXME("not implemented\n");
291 /**********************************************************************
294 * Get the FPU exception code from the FPU status.
296 static inline DWORD get_fpu_code( const CONTEXT *context )
298 DWORD status = context->Fpscr;
300 if (status & 0x01) /* IE */
302 if (status & 0x40) /* SF */
303 return EXCEPTION_FLT_STACK_CHECK;
305 return EXCEPTION_FLT_INVALID_OPERATION;
307 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
308 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
309 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
310 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
311 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
312 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
315 /**********************************************************************
318 * Implementation of SIGSEGV handler.
320 static void do_segv( CONTEXT *context, int trap, int err, int code, void * addr )
322 EXCEPTION_RECORD rec;
324 rec.ExceptionRecord = NULL;
325 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
326 rec.ExceptionAddress = addr;
327 rec.NumberParameters = 0;
331 switch ( code & 0xffff ) {
334 rec.NumberParameters = 2;
335 rec.ExceptionInformation[0] = 0; /* FIXME ? */
336 rec.ExceptionInformation[1] = (ULONG_PTR)addr;
337 if (!(rec.ExceptionCode = virtual_handle_fault(addr, rec.ExceptionInformation[0])))
340 default:FIXME("Unhandled SIGSEGV/%x\n",code);
345 switch ( code & 0xffff ) {
347 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
354 /* FIXME: correct for all cases ? */
355 rec.NumberParameters = 2;
356 rec.ExceptionInformation[0] = 0; /* FIXME ? */
357 rec.ExceptionInformation[1] = (ULONG_PTR)addr;
358 if (!(rec.ExceptionCode = virtual_handle_fault(addr, rec.ExceptionInformation[0])))
362 default:FIXME("Unhandled SIGBUS/%x\n",code);
367 switch ( code & 0xffff ) {
368 case ILL_ILLOPC: /* illegal opcode */
370 case ILL_ILLOPN: /* illegal operand */
373 case ILL_ILLADR: /* illegal addressing mode */
376 case ILL_ILLTRP: /* illegal trap */
379 case ILL_COPROC: /* coprocessor error */
381 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
383 case ILL_PRVOPC: /* privileged opcode */
385 case ILL_PRVREG: /* privileged register */
387 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
390 case ILL_BADSTK: /* internal stack error */
391 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
394 default:FIXME("Unhandled SIGILL/%x\n", code);
399 __regs_RtlRaiseException( &rec, context );
402 /**********************************************************************
405 * Implementation of SIGTRAP handler.
407 static void do_trap( CONTEXT *context, int code, void * addr )
409 EXCEPTION_RECORD rec;
411 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
412 rec.ExceptionRecord = NULL;
413 rec.ExceptionAddress = addr;
414 rec.NumberParameters = 0;
416 /* FIXME: check if we might need to modify PC */
417 switch (code & 0xffff) {
420 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
425 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
428 default:FIXME("Unhandled SIGTRAP/%x\n", code);
431 __regs_RtlRaiseException( &rec, context );
434 /**********************************************************************
437 * Implementation of SIGFPE handler.
439 static void do_fpe( CONTEXT *context, int code, void * addr )
441 EXCEPTION_RECORD rec;
443 switch ( code & 0xffff ) {
446 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
451 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
456 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
461 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
466 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
471 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
476 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
483 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
486 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
487 rec.ExceptionRecord = NULL;
488 rec.ExceptionAddress = addr;
489 rec.NumberParameters = 0;
490 __regs_RtlRaiseException( &rec, context );
493 /**********************************************************************
496 * Handler for SIGSEGV and related errors.
498 static HANDLER_DEF(segv_handler)
501 save_context( &context, HANDLER_CONTEXT );
502 do_segv( &context, __siginfo->si_signo, __siginfo->si_errno, __siginfo->si_code, __siginfo->si_addr );
503 restore_context( &context, HANDLER_CONTEXT );
506 /**********************************************************************
509 * Handler for SIGTRAP.
511 static HANDLER_DEF(trap_handler)
514 save_context( &context, HANDLER_CONTEXT );
515 do_trap( &context, __siginfo->si_code, __siginfo->si_addr );
516 restore_context( &context, HANDLER_CONTEXT );
519 /**********************************************************************
522 * Handler for SIGFPE.
524 static HANDLER_DEF(fpe_handler)
527 save_fpu( &context, HANDLER_CONTEXT );
528 save_context( &context, HANDLER_CONTEXT );
529 do_fpe( &context, __siginfo->si_code, __siginfo->si_addr );
530 restore_context( &context, HANDLER_CONTEXT );
531 restore_fpu( &context, HANDLER_CONTEXT );
534 /**********************************************************************
537 * Handler for SIGINT.
539 static HANDLER_DEF(int_handler)
541 if (!dispatch_signal(SIGINT))
543 EXCEPTION_RECORD rec;
546 save_context( &context, HANDLER_CONTEXT );
547 rec.ExceptionCode = CONTROL_C_EXIT;
548 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
549 rec.ExceptionRecord = NULL;
550 rec.ExceptionAddress = (LPVOID)context.Iar;
551 rec.NumberParameters = 0;
552 __regs_RtlRaiseException( &rec, &context );
553 restore_context( &context, HANDLER_CONTEXT );
558 /**********************************************************************
561 * Handler for SIGABRT.
563 static HANDLER_DEF(abrt_handler)
565 EXCEPTION_RECORD rec;
568 save_context( &context, HANDLER_CONTEXT );
569 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
570 rec.ExceptionFlags = EH_NONCONTINUABLE;
571 rec.ExceptionRecord = NULL;
572 rec.ExceptionAddress = (LPVOID)context.Iar;
573 rec.NumberParameters = 0;
574 __regs_RtlRaiseException( &rec, &context ); /* Should never return.. */
575 restore_context( &context, HANDLER_CONTEXT );
579 /**********************************************************************
582 * Handler for SIGQUIT.
584 static HANDLER_DEF(quit_handler)
586 server_abort_thread(0);
590 /**********************************************************************
593 * Handler for SIGUSR1, used to signal a thread that it got suspended.
595 static HANDLER_DEF(usr1_handler)
599 save_context( &context, HANDLER_CONTEXT );
600 wait_suspend( &context );
601 restore_context( &context, HANDLER_CONTEXT );
605 /**********************************************************************
606 * get_signal_stack_total_size
608 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
609 * Must be a power of two.
611 size_t get_signal_stack_total_size(void)
613 assert( sizeof(TEB) <= getpagesize() );
614 return getpagesize(); /* this is just for the TEB, we don't need a signal stack */
618 /***********************************************************************
621 * Set a signal handler
623 static int set_handler( int sig, void (*func)() )
625 struct sigaction sig_act;
627 sig_act.sa_sigaction = func;
628 sig_act.sa_mask = server_block_set;
629 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
630 return sigaction( sig, &sig_act, NULL );
634 /***********************************************************************
635 * __wine_set_signal_handler (NTDLL.@)
637 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
639 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
640 if (handlers[sig] != NULL) return -2;
646 /**********************************************************************
649 void signal_init_thread(void)
653 /**********************************************************************
654 * signal_init_process
656 void signal_init_process(void)
658 if (set_handler( SIGINT, (void (*)())int_handler ) == -1) goto error;
659 if (set_handler( SIGFPE, (void (*)())fpe_handler ) == -1) goto error;
660 if (set_handler( SIGSEGV, (void (*)())segv_handler ) == -1) goto error;
661 if (set_handler( SIGILL, (void (*)())segv_handler ) == -1) goto error;
662 if (set_handler( SIGABRT, (void (*)())abrt_handler ) == -1) goto error;
663 if (set_handler( SIGQUIT, (void (*)())quit_handler ) == -1) goto error;
664 if (set_handler( SIGUSR1, (void (*)())usr1_handler ) == -1) goto error;
666 if (set_handler( SIGBUS, (void (*)())segv_handler ) == -1) goto error;
669 if (set_handler( SIGTRAP, (void (*)())trap_handler ) == -1) goto error;
671 signal_init_thread();
680 /**********************************************************************
681 * __wine_enter_vm86 (NTDLL.@)
683 void __wine_enter_vm86( CONTEXT *context )
685 MESSAGE("vm86 mode not supported on this platform\n");
688 /**********************************************************************
689 * DbgBreakPoint (NTDLL.@)
691 void WINAPI DbgBreakPoint(void)
693 kill(getpid(), SIGTRAP);
696 /**********************************************************************
697 * DbgUserBreakPoint (NTDLL.@)
699 void WINAPI DbgUserBreakPoint(void)
701 kill(getpid(), SIGTRAP);
704 #endif /* __powerpc__ */