Raise an exception if any Wine or Winelib code does an assert.
[wine/multimedia.git] / dlls / ntdll / signal_powerpc.c
blob30acb6238f72baf4e0d7ec7bad197aa8fc0d7bdd
1 /*
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
21 #ifdef __powerpc__
23 #include "config.h"
24 #include "wine/port.h"
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #ifdef HAVE_SYS_PARAM_H
35 # include <sys/param.h>
36 #endif
37 #ifdef HAVE_SYSCALL_H
38 # include <syscall.h>
39 #else
40 # ifdef HAVE_SYS_SYSCALL_H
41 # include <sys/syscall.h>
42 # endif
43 #endif
45 #ifdef HAVE_SYS_VM86_H
46 # include <sys/vm86.h>
47 #endif
49 #ifdef HAVE_SYS_SIGNAL_H
50 # include <sys/signal.h>
51 #endif
53 #include "winternl.h"
54 #include "winnt.h"
55 #include "wine/library.h"
56 #include "wine/exception.h"
57 #include "selectors.h"
58 #include "stackframe.h"
59 #include "global.h"
60 #include "miscemu.h"
61 #include "syslevel.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 /***********************************************************************
84 * dispatch_signal
86 inline static int dispatch_signal(unsigned int sig)
88 if (handlers[sig] == NULL) return 0;
89 return handlers[sig](sig);
92 /***********************************************************************
93 * save_context
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);
104 C(31);
106 CX(Iar,nip);
107 CX(Msr,msr);
108 CX(Ctr,ctr);
109 #undef CX
110 /* FIXME: fp regs? */
112 /* FIXME: missing pt_regs ...
113 unsigned long link;
114 unsigned long xer;
115 unsigned long ccr;
116 unsigned long mq;
118 unsigned long trap;
119 unsigned long dar;
120 unsigned long dsisr;
126 /***********************************************************************
127 * restore_context
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);
137 C(31);
139 CX(Iar,nip);
140 CX(Msr,msr);
141 CX(Ctr,ctr);
142 #undef CX
146 /***********************************************************************
147 * save_fpu
149 * Set the FPU context from a sigcontext.
151 inline static void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
153 /* FIXME? */
157 /***********************************************************************
158 * restore_fpu
160 * Restore the FPU context to a sigcontext.
162 inline static void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
164 /* FIXME? */
168 /**********************************************************************
169 * get_fpu_code
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;
181 else
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 /***********************************************************************
194 * SIGNAL_Unblock
196 * Unblock signals. Called from EXC_RtlRaiseException.
198 void SIGNAL_Unblock( void )
200 sigprocmask( SIG_UNBLOCK, &all_sigs, NULL );
203 /**********************************************************************
204 * segv_handler
206 * Handler for SIGSEGV and related errors.
208 static HANDLER_DEF(segv_handler)
210 CONTEXT context;
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) {
221 case SIGSEGV:
222 switch ( __siginfo->si_code & 0xffff ) {
223 case SEGV_MAPERR:
224 case SEGV_ACCERR:
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)))
229 return;
230 rec.ExceptionCode = page_fault_code;
231 break;
232 default:FIXME("Unhandled SIGSEGV/%x\n",__siginfo->si_code);
233 break;
235 break;
236 case SIGBUS:
237 switch ( __siginfo->si_code & 0xffff ) {
238 case BUS_ADRALN:
239 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
240 break;
241 case BUS_ADRERR:
242 case BUS_OBJERR:
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)))
248 return;
249 rec.ExceptionCode = page_fault_code;
250 break;
251 default:FIXME("Unhandled SIGBUS/%x\n",__siginfo->si_code);
252 break;
254 break;
255 case SIGILL:
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;
263 break;
264 case ILL_PRVOPC: /* privileged opcode */
265 case ILL_PRVREG: /* privileged register */
266 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
267 break;
268 case ILL_BADSTK: /* internal stack error */
269 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
270 break;
271 default:FIXME("Unhandled SIGILL/%x\n",__siginfo->si_code);
272 break;
274 break;
276 EXC_RtlRaiseException( &rec, &context );
277 restore_context( &context, HANDLER_CONTEXT );
280 /**********************************************************************
281 * trap_handler
283 * Handler for SIGTRAP.
285 static HANDLER_DEF(trap_handler)
287 CONTEXT context;
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) {
299 case TRAP_BRKPT:
300 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
301 break;
302 case TRAP_TRACE:
303 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
304 break;
306 EXC_RtlRaiseException( &rec, &context );
307 restore_context( &context, HANDLER_CONTEXT );
311 /**********************************************************************
312 * fpe_handler
314 * Handler for SIGFPE.
316 static HANDLER_DEF(fpe_handler)
318 CONTEXT context;
319 EXCEPTION_RECORD rec;
321 /*save_fpu( &context, HANDLER_CONTEXT );*/
322 save_context( &context, HANDLER_CONTEXT );
324 switch ( __siginfo->si_code & 0xffff ) {
325 case FPE_FLTSUB:
326 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
327 break;
328 case FPE_INTDIV:
329 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
330 break;
331 case FPE_INTOVF:
332 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
333 break;
334 case FPE_FLTDIV:
335 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
336 break;
337 case FPE_FLTOVF:
338 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
339 break;
340 case FPE_FLTUND:
341 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
342 break;
343 case FPE_FLTRES:
344 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
345 break;
346 case FPE_FLTINV:
347 default:
348 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
349 break;
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 /**********************************************************************
362 * int_handler
364 * Handler for SIGINT.
366 static HANDLER_DEF(int_handler)
368 if (!dispatch_signal(SIGINT))
370 EXCEPTION_RECORD rec;
371 CONTEXT context;
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 );
384 /**********************************************************************
385 * abrt_handler
387 * Handler for SIGABRT.
389 static HANDLER_DEF(abrt_handler)
391 EXCEPTION_RECORD rec;
392 CONTEXT context;
394 save_context( &context, HANDLER_CONTEXT );
395 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
396 rec.ExceptionFlags = EH_NONCONTINUABLE;
397 rec.ExceptionRecord = NULL;
398 rec.ExceptionAddress = (LPVOID)context.Eip;
399 rec.NumberParameters = 0;
400 EXC_RtlRaiseException( &rec, &context ); /* Should never return.. */
401 restore_context( &context, HANDLER_CONTEXT );
405 /***********************************************************************
406 * set_handler
408 * Set a signal handler
410 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
412 struct sigaction sig_act;
414 sig_act.sa_sigaction = func;
415 sigemptyset( &sig_act.sa_mask );
416 sigaddset( &sig_act.sa_mask, SIGINT );
417 sigaddset( &sig_act.sa_mask, SIGALRM );
419 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
421 #ifdef SA_ONSTACK
422 if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
423 #endif
424 return sigaction( sig, &sig_act, NULL );
428 /***********************************************************************
429 * __wine_set_signal_handler (NTDLL.@)
431 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
433 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
434 if (handlers[sig] != NULL) return -2;
435 handlers[sig] = wsh;
436 return 0;
440 /**********************************************************************
441 * SIGNAL_Init
443 BOOL SIGNAL_Init(void)
445 int have_sigaltstack = 0;
447 #ifdef HAVE_SIGALTSTACK
448 struct sigaltstack ss;
449 if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
451 ss.ss_size = SIGNAL_STACK_SIZE;
452 ss.ss_flags = 0;
453 if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
455 #endif /* HAVE_SIGALTSTACK */
457 sigfillset( &all_sigs );
459 if (set_handler( SIGINT, have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
460 if (set_handler( SIGFPE, have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
461 if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
462 if (set_handler( SIGILL, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
463 if (set_handler( SIGABRT, have_sigaltstack, (void (*)())abrt_handler ) == -1) goto error;
464 #ifdef SIGBUS
465 if (set_handler( SIGBUS, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
466 #endif
467 #ifdef SIGTRAP
468 if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
469 #endif
471 return TRUE;
473 error:
474 perror("sigaction");
475 return FALSE;
479 /**********************************************************************
480 * SIGNAL_Reset
482 void SIGNAL_Reset(void)
484 sigset_t block_set;
486 /* block the async signals */
487 sigemptyset( &block_set );
488 sigaddset( &block_set, SIGALRM );
489 sigaddset( &block_set, SIGIO );
490 sigaddset( &block_set, SIGHUP );
491 sigaddset( &block_set, SIGUSR2 );
492 sigprocmask( SIG_BLOCK, &block_set, NULL );
494 /* restore default handlers */
495 signal( SIGINT, SIG_DFL );
496 signal( SIGFPE, SIG_DFL );
497 signal( SIGSEGV, SIG_DFL );
498 signal( SIGILL, SIG_DFL );
499 #ifdef SIGBUS
500 signal( SIGBUS, SIG_DFL );
501 #endif
502 #ifdef SIGTRAP
503 signal( SIGTRAP, SIG_DFL );
504 #endif
507 /**********************************************************************
508 * __wine_enter_vm86 (NTDLL.@)
510 void __wine_enter_vm86( CONTEXT *context )
512 MESSAGE("vm86 mode not supported on this platform\n");
515 /**********************************************************************
516 * DbgBreakPoint (NTDLL.@)
518 void WINAPI DbgBreakPoint(void)
520 kill(getpid(), SIGTRAP);
523 /**********************************************************************
524 * DbgUserBreakPoint (NTDLL.@)
526 void WINAPI DbgUserBreakPoint(void)
528 kill(getpid(), SIGTRAP);
531 #endif /* __powerpc__ */