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"
56 #include "wine/exception.h"
57 #include "selectors.h"
58 #include "stackframe.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(seh);
67 /***********************************************************************
68 * signal context platform-specific definitions
71 typedef struct ucontext SIGCONTEXT;
73 #define HANDLER_DEF(name) void name( int __signal, struct siginfo *__siginfo, SIGCONTEXT *__context )
74 #define HANDLER_CONTEXT (__context)
76 typedef int (*wine_signal_handler)(unsigned int sig);
78 static wine_signal_handler handlers[256];
80 static sigset_t all_sigs;
83 /***********************************************************************
86 inline static int dispatch_signal(unsigned int sig)
88 if (handlers[sig] == NULL) return 0;
89 return handlers[sig](sig);
92 /***********************************************************************
95 * Set the register values from a sigcontext.
97 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
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);
110 /* FIXME: fp regs? */
112 /* FIXME: missing pt_regs ...
126 /***********************************************************************
129 * Build a sigcontext from the register values.
131 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
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);
146 /***********************************************************************
149 * Set the FPU context from a sigcontext.
151 inline static void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
157 /***********************************************************************
160 * Restore the FPU context to a sigcontext.
162 inline static void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
168 /**********************************************************************
171 * Get the FPU exception code from the FPU status.
173 static inline DWORD get_fpu_code( const CONTEXT *context )
175 DWORD status = 0 /* FIXME */;
177 if (status & 0x01) /* IE */
179 if (status & 0x40) /* SF */
180 return EXCEPTION_FLT_STACK_CHECK;
182 return EXCEPTION_FLT_INVALID_OPERATION;
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 */
193 /***********************************************************************
196 * Unblock signals. Called from EXC_RtlRaiseException.
198 void SIGNAL_Unblock( void )
200 sigprocmask( SIG_UNBLOCK, &all_sigs, NULL );
203 /**********************************************************************
206 * Handler for SIGSEGV and related errors.
208 static HANDLER_DEF(segv_handler)
211 EXCEPTION_RECORD rec;
212 DWORD page_fault_code = EXCEPTION_ACCESS_VIOLATION;
214 save_context( &context, HANDLER_CONTEXT );
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) {
222 switch ( __siginfo->si_code & 0xffff ) {
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)))
230 rec.ExceptionCode = page_fault_code;
232 default:FIXME("Unhandled SIGSEGV/%x\n",__siginfo->si_code);
237 switch ( __siginfo->si_code & 0xffff ) {
239 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
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)))
249 rec.ExceptionCode = page_fault_code;
251 default:FIXME("Unhandled SIGBUS/%x\n",__siginfo->si_code);
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;
264 case ILL_PRVOPC: /* privileged opcode */
265 case ILL_PRVREG: /* privileged register */
266 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
268 case ILL_BADSTK: /* internal stack error */
269 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
271 default:FIXME("Unhandled SIGILL/%x\n",__siginfo->si_code);
276 EXC_RtlRaiseException( &rec, &context );
277 restore_context( &context, HANDLER_CONTEXT );
280 /**********************************************************************
283 * Handler for SIGTRAP.
285 static HANDLER_DEF(trap_handler)
288 EXCEPTION_RECORD rec;
290 save_context( &context, HANDLER_CONTEXT );
292 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
293 rec.ExceptionRecord = NULL;
294 rec.ExceptionAddress = (LPVOID)__context->uc_mcontext.regs->nip;
295 rec.NumberParameters = 0;
297 /* FIXME: check if we might need to modify PC */
298 switch (__siginfo->si_code & 0xffff) {
300 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
303 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
306 EXC_RtlRaiseException( &rec, &context );
307 restore_context( &context, HANDLER_CONTEXT );
311 /**********************************************************************
314 * Handler for SIGFPE.
316 static HANDLER_DEF(fpe_handler)
319 EXCEPTION_RECORD rec;
321 /*save_fpu( &context, HANDLER_CONTEXT );*/
322 save_context( &context, HANDLER_CONTEXT );
324 switch ( __siginfo->si_code & 0xffff ) {
326 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
329 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
332 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
335 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
338 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
341 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
344 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
348 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
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 );*/
361 /**********************************************************************
364 * Handler for SIGINT.
366 static HANDLER_DEF(int_handler)
368 if (!dispatch_signal(SIGINT))
370 EXCEPTION_RECORD rec;
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 );
385 /***********************************************************************
388 * Set a signal handler
390 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
392 struct sigaction sig_act;
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 );
399 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
402 if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
404 return sigaction( sig, &sig_act, NULL );
408 /***********************************************************************
409 * __wine_set_signal_handler (NTDLL.@)
411 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
413 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
414 if (handlers[sig] != NULL) return -2;
420 /**********************************************************************
423 BOOL SIGNAL_Init(void)
425 int have_sigaltstack = 0;
427 #ifdef HAVE_SIGALTSTACK
428 struct sigaltstack ss;
429 if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
431 ss.ss_size = SIGNAL_STACK_SIZE;
433 if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
435 #endif /* HAVE_SIGALTSTACK */
437 sigfillset( &all_sigs );
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;
444 if (set_handler( SIGBUS, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
447 if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
458 /**********************************************************************
461 void SIGNAL_Reset(void)
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 );
473 /* restore default handlers */
474 signal( SIGINT, SIG_DFL );
475 signal( SIGFPE, SIG_DFL );
476 signal( SIGSEGV, SIG_DFL );
477 signal( SIGILL, SIG_DFL );
479 signal( SIGBUS, SIG_DFL );
482 signal( SIGTRAP, SIG_DFL );
486 /**********************************************************************
487 * __wine_enter_vm86 (NTDLL.@)
489 void __wine_enter_vm86( CONTEXT *context )
491 MESSAGE("vm86 mode not supported on this platform\n");
494 /**********************************************************************
495 * DbgBreakPoint (NTDLL.@)
497 void WINAPI DbgBreakPoint(void)
499 kill(getpid(), SIGTRAP);
502 /**********************************************************************
503 * DbgUserBreakPoint (NTDLL.@)
505 void WINAPI DbgUserBreakPoint(void)
507 kill(getpid(), SIGTRAP);
510 #endif /* __powerpc__ */