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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
34 #ifdef HAVE_SYS_PARAM_H
35 # include <sys/param.h>
40 # ifdef HAVE_SYS_SYSCALL_H
41 # include <sys/syscall.h>
45 #ifdef HAVE_SYS_VM86_H
46 # include <sys/vm86.h>
49 #ifdef HAVE_SYS_SIGNAL_H
50 # include <sys/signal.h>
55 #include "wine/library.h"
57 #include "selectors.h"
59 /***********************************************************************
60 * signal context platform-specific definitions
63 typedef struct ucontext SIGCONTEXT;
65 #define HANDLER_DEF(name) void name( int __signal, struct siginfo *__siginfo, SIGCONTEXT *__context )
66 #define HANDLER_CONTEXT (__context)
68 #include "wine/exception.h"
70 #include "stackframe.h"
75 #include "wine/debug.h"
77 WINE_DEFAULT_DEBUG_CHANNEL(seh);
79 typedef int (*wine_signal_handler)(unsigned sig);
81 static wine_signal_handler handlers[256];
83 static sigset_t all_sigs;
86 /***********************************************************************
89 inline static int dispatch_signal(unsigned sig)
91 if (handlers[sig] == NULL) return 0;
92 return handlers[sig](sig);
95 /***********************************************************************
98 * Set the register values from a sigcontext.
100 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
102 #define CX(x,y) context->x = sigcontext->uc_mcontext.regs->y
103 #define C(x) CX(Gpr##x,gpr[x])
104 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
105 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
106 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
113 /* FIXME: fp regs? */
115 /* FIXME: missing pt_regs ...
129 /***********************************************************************
132 * Build a sigcontext from the register values.
134 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
136 #define CX(x,y) sigcontext->uc_mcontext.regs->y = context->x
137 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
138 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
139 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
149 /***********************************************************************
152 * Set the FPU context from a sigcontext.
154 inline static void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
160 /***********************************************************************
163 * Restore the FPU context to a sigcontext.
165 inline static void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
171 /**********************************************************************
174 * Get the FPU exception code from the FPU status.
176 static inline DWORD get_fpu_code( const CONTEXT *context )
178 DWORD status = 0 /* FIXME */;
180 if (status & 0x01) /* IE */
182 if (status & 0x40) /* SF */
183 return EXCEPTION_FLT_STACK_CHECK;
185 return EXCEPTION_FLT_INVALID_OPERATION;
187 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
188 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
189 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
190 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
191 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
192 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
196 /***********************************************************************
199 * Unblock signals. Called from EXC_RtlRaiseException.
201 void SIGNAL_Unblock( void )
203 sigprocmask( SIG_UNBLOCK, &all_sigs, NULL );
206 /**********************************************************************
209 * Handler for SIGSEGV and related errors.
211 static HANDLER_DEF(segv_handler)
214 EXCEPTION_RECORD rec;
215 DWORD page_fault_code = EXCEPTION_ACCESS_VIOLATION;
217 save_context( &context, HANDLER_CONTEXT );
219 rec.ExceptionRecord = NULL;
220 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
221 rec.ExceptionAddress = (LPVOID)HANDLER_CONTEXT->uc_mcontext.regs->nip;
222 rec.NumberParameters = 0;
223 switch (__siginfo->si_signo) {
225 switch (__siginfo->si_code) {
228 rec.NumberParameters = 2;
229 rec.ExceptionInformation[0] = 0; /* FIXME ? */
230 rec.ExceptionInformation[1] = (DWORD)__siginfo->si_addr;
231 if (!(page_fault_code=VIRTUAL_HandleFault(__siginfo->si_addr)))
233 rec.ExceptionCode = page_fault_code;
235 default:FIXME("Unhandled SIGSEGV/%x\n",__siginfo->si_code);
240 switch (__siginfo->si_code) {
242 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
246 /* FIXME: correct for all cases ? */
247 rec.NumberParameters = 2;
248 rec.ExceptionInformation[0] = 0; /* FIXME ? */
249 rec.ExceptionInformation[1] = (DWORD)__siginfo->si_addr;
250 if (!(page_fault_code=VIRTUAL_HandleFault(__siginfo->si_addr)))
252 rec.ExceptionCode = page_fault_code;
254 default:FIXME("Unhandled SIGBUS/%x\n",__siginfo->si_code);
259 switch (__siginfo->si_code) {
260 case ILL_ILLOPC: /* illegal opcode */
261 case ILL_ILLOPN: /* illegal operand */
262 case ILL_ILLADR: /* illegal addressing mode */
263 case ILL_ILLTRP: /* illegal trap */
264 case ILL_COPROC: /* coprocessor error */
265 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
267 case ILL_PRVOPC: /* privileged opcode */
268 case ILL_PRVREG: /* privileged register */
269 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
271 case ILL_BADSTK: /* internal stack error */
272 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
274 default:FIXME("Unhandled SIGILL/%x\n",__siginfo->si_code);
279 EXC_RtlRaiseException( &rec, &context );
280 restore_context( &context, HANDLER_CONTEXT );
283 /**********************************************************************
286 * Handler for SIGTRAP.
288 static HANDLER_DEF(trap_handler)
291 EXCEPTION_RECORD rec;
293 save_context( &context, HANDLER_CONTEXT );
295 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
296 rec.ExceptionRecord = NULL;
297 rec.ExceptionAddress = (LPVOID)__context->uc_mcontext.regs->nip;
298 rec.NumberParameters = 0;
300 /* FIXME: check if we might need to modify PC */
301 switch (__siginfo->si_code) {
303 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
306 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
309 EXC_RtlRaiseException( &rec, &context );
310 restore_context( &context, HANDLER_CONTEXT );
314 /**********************************************************************
317 * Handler for SIGFPE.
319 static HANDLER_DEF(fpe_handler)
322 EXCEPTION_RECORD rec;
324 /*save_fpu( &context, HANDLER_CONTEXT );*/
325 save_context( &context, HANDLER_CONTEXT );
327 switch ( __siginfo->si_code ) {
329 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
332 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
335 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
338 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
341 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
344 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
347 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
351 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
354 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
355 rec.ExceptionRecord = NULL;
356 rec.ExceptionAddress = (LPVOID)__context->uc_mcontext.regs->nip;
357 rec.NumberParameters = 0;
358 EXC_RtlRaiseException( &rec, &context );
359 restore_context( &context, HANDLER_CONTEXT );
360 /*restore_fpu( &context, HANDLER_CONTEXT );*/
364 /**********************************************************************
367 * Handler for SIGINT.
369 static HANDLER_DEF(int_handler)
371 if (!dispatch_signal(SIGINT))
373 EXCEPTION_RECORD rec;
376 save_context( &context, HANDLER_CONTEXT );
377 rec.ExceptionCode = CONTROL_C_EXIT;
378 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
379 rec.ExceptionRecord = NULL;
380 rec.ExceptionAddress = (LPVOID)context.Fill[0];
381 rec.NumberParameters = 0;
382 EXC_RtlRaiseException( &rec, &context );
383 restore_context( &context, HANDLER_CONTEXT );
388 /***********************************************************************
391 * Set a signal handler
393 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
395 struct sigaction sig_act;
397 sig_act.sa_sigaction = func;
398 sigemptyset( &sig_act.sa_mask );
399 sigaddset( &sig_act.sa_mask, SIGINT );
400 sigaddset( &sig_act.sa_mask, SIGALRM );
402 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
405 if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
407 return sigaction( sig, &sig_act, NULL );
411 /***********************************************************************
412 * __wine_set_signal_handler (NTDLL.@)
414 int __wine_set_signal_handler(unsigned sig, wine_signal_handler wsh)
416 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
417 if (handlers[sig] != NULL) return -2;
423 /**********************************************************************
426 BOOL SIGNAL_Init(void)
428 int have_sigaltstack = 0;
430 #ifdef HAVE_SIGALTSTACK
431 struct sigaltstack ss;
432 if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
434 ss.ss_size = SIGNAL_STACK_SIZE;
436 if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
438 #endif /* HAVE_SIGALTSTACK */
440 sigfillset( &all_sigs );
442 if (set_handler( SIGINT, have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
443 if (set_handler( SIGFPE, have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
444 if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
445 if (set_handler( SIGILL, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
447 if (set_handler( SIGBUS, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
450 if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
461 /**********************************************************************
464 void SIGNAL_Reset(void)
468 /* block the async signals */
469 sigemptyset( &block_set );
470 sigaddset( &block_set, SIGALRM );
471 sigaddset( &block_set, SIGIO );
472 sigaddset( &block_set, SIGHUP );
473 sigaddset( &block_set, SIGUSR2 );
474 sigprocmask( SIG_BLOCK, &block_set, NULL );
476 /* restore default handlers */
477 signal( SIGINT, SIG_DFL );
478 signal( SIGFPE, SIG_DFL );
479 signal( SIGSEGV, SIG_DFL );
480 signal( SIGILL, SIG_DFL );
482 signal( SIGBUS, SIG_DFL );
485 signal( SIGTRAP, SIG_DFL );
489 /**********************************************************************
490 * __wine_enter_vm86 (NTDLL.@)
492 void __wine_enter_vm86( CONTEXT *context )
494 MESSAGE("vm86 mode not supported on this platform\n");
497 /**********************************************************************
498 * DbgBreakPoint (NTDLL.@)
500 void WINAPI DbgBreakPoint(void)
502 kill(getpid(), SIGTRAP);
505 /**********************************************************************
506 * DbgUserBreakPoint (NTDLL.@)
508 void WINAPI DbgUserBreakPoint(void)
510 kill(getpid(), SIGTRAP);
513 #endif /* __powerpc__ */