Initialize .COM stack.
[wine/multimedia.git] / dlls / ntdll / signal_powerpc.c
bloba0b6eb0b843d13feb623705639278a8c174e7a68
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 <signal.h>
27 #include <stdlib.h>
28 #include <stdarg.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 "windef.h"
54 #include "winbase.h"
55 #include "winreg.h"
56 #include "winternl.h"
57 #include "wine/library.h"
58 #include "wine/exception.h"
59 #include "ntdll_misc.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(seh);
65 /***********************************************************************
66 * signal context platform-specific definitions
68 #ifdef linux
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)
100 #endif /* linux */
102 #ifdef __APPLE__
104 # include <sys/ucontext.h>
106 # include <sys/types.h>
107 # include <signal.h>
108 typedef siginfo_t siginfo;
110 typedef struct ucontext SIGCONTEXT;
113 # define HANDLER_DEF(name) void name( int __signal, siginfo *__siginfo, SIGCONTEXT *__context )
114 # define HANDLER_CONTEXT (__context)
116 /* All Registers access - only for local access */
117 # define REG_sig(reg_name, context) ((context)->uc_mcontext->ss.reg_name)
118 # define FLOATREG_sig(reg_name, context) ((context)->uc_mcontext->fs.reg_name)
119 # define EXCEPREG_sig(reg_name, context) ((context)->uc_mcontext->es.reg_name)
120 # define VECREG_sig(reg_name, context) ((context)->uc_mcontext->vs.reg_name)
122 /* Gpr Registers access */
123 # define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
125 # define IAR_sig(context) REG_sig(srr0, context) /* Program counter */
126 # define MSR_sig(context) REG_sig(srr1, context) /* Machine State Register (Supervisor) */
127 # define CTR_sig(context) REG_sig(ctr, context)
129 # define XER_sig(context) REG_sig(xer, context) /* Link register */
130 # define LR_sig(context) REG_sig(lr, context) /* User's integer exception register */
131 # define CR_sig(context) REG_sig(cr, context) /* Condition register */
133 /* Float Registers access */
134 # define FLOAT_sig(reg_num, context) FLOATREG_sig(fpregs[reg_num], context)
136 # define FPSCR_sig(context) ((double)FLOATREG_sig(fpscr, context))
138 /* Exception Registers access */
139 # define DAR_sig(context) EXCEPREG_sig(dar, context) /* Fault registers for coredump */
140 # define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
141 # define TRAP_sig(context) EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
143 /* Signal defs : Those are undefined on darwin
144 SIGBUS
145 #undef BUS_ADRERR
146 #undef BUS_OBJERR
147 SIGILL
148 #undef ILL_ILLOPN
149 #undef ILL_ILLTRP
150 #undef ILL_ILLADR
151 #undef ILL_COPROC
152 #undef ILL_PRVREG
153 #undef ILL_BADSTK
154 SIGTRAP
155 #undef TRAP_BRKPT
156 #undef TRAP_TRACE
157 SIGFPE
160 #endif /* __APPLE__ */
164 typedef int (*wine_signal_handler)(unsigned int sig);
166 static wine_signal_handler handlers[256];
168 extern void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
170 /***********************************************************************
171 * dispatch_signal
173 inline static int dispatch_signal(unsigned int sig)
175 if (handlers[sig] == NULL) return 0;
176 return handlers[sig](sig);
179 /***********************************************************************
180 * save_context
182 * Set the register values from a sigcontext.
184 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
187 #define C(x) context->Gpr##x = GPR_sig(x,sigcontext)
188 /* Save Gpr registers */
189 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
190 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
191 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
192 C(31);
193 #undef C
195 context->Iar = IAR_sig(sigcontext); /* Program Counter */
196 context->Msr = MSR_sig(sigcontext); /* Machine State Register (Supervisor) */
197 context->Ctr = CTR_sig(sigcontext);
199 context->Xer = XER_sig(sigcontext);
200 context->Lr = LR_sig(sigcontext);
201 context->Cr = CR_sig(sigcontext);
203 /* Saving Exception regs */
204 context->Dar = DAR_sig(sigcontext);
205 context->Dsisr = DSISR_sig(sigcontext);
206 context->Trap = TRAP_sig(sigcontext);
210 /***********************************************************************
211 * restore_context
213 * Build a sigcontext from the register values.
215 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
218 #define C(x) GPR_sig(x,sigcontext) = context->Gpr##x
219 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
220 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
221 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
222 C(31);
223 #undef C
225 IAR_sig(sigcontext) = context->Iar; /* Program Counter */
226 MSR_sig(sigcontext) = context->Msr; /* Machine State Register (Supervisor) */
227 CTR_sig(sigcontext) = context->Ctr;
229 XER_sig(sigcontext) = context->Xer;
230 LR_sig(sigcontext) = context->Lr;
231 CR_sig(sigcontext) = context->Cr;
233 /* Setting Exception regs */
234 DAR_sig(sigcontext) = context->Dar;
235 DSISR_sig(sigcontext) = context->Dsisr;
236 TRAP_sig(sigcontext) = context->Trap;
240 /***********************************************************************
241 * save_fpu
243 * Set the FPU context from a sigcontext.
245 inline static void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
247 #define C(x) context->Fpr##x = FLOAT_sig(x,sigcontext)
248 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
249 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
250 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
251 C(31);
252 #undef C
253 context->Fpscr = FPSCR_sig(sigcontext);
257 /***********************************************************************
258 * restore_fpu
260 * Restore the FPU context to a sigcontext.
262 inline static void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
264 #define C(x) FLOAT_sig(x,sigcontext) = context->Fpr##x
265 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
266 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
267 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
268 C(31);
269 #undef C
270 FPSCR_sig(sigcontext) = context->Fpscr;
274 /**********************************************************************
275 * get_fpu_code
277 * Get the FPU exception code from the FPU status.
279 static inline DWORD get_fpu_code( const CONTEXT *context )
281 DWORD status = context->Fpscr;
283 if (status & 0x01) /* IE */
285 if (status & 0x40) /* SF */
286 return EXCEPTION_FLT_STACK_CHECK;
287 else
288 return EXCEPTION_FLT_INVALID_OPERATION;
290 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
291 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
292 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
293 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
294 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
295 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
298 /**********************************************************************
299 * do_segv
301 * Implementation of SIGSEGV handler.
303 static void do_segv( CONTEXT *context, int trap, int err, int code, void * addr )
305 EXCEPTION_RECORD rec;
306 DWORD page_fault_code = EXCEPTION_ACCESS_VIOLATION;
308 rec.ExceptionRecord = NULL;
309 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
310 rec.ExceptionAddress = addr;
311 rec.NumberParameters = 0;
313 switch (trap) {
314 case SIGSEGV:
315 switch ( code & 0xffff ) {
316 case SEGV_MAPERR:
317 case SEGV_ACCERR:
318 rec.NumberParameters = 2;
319 rec.ExceptionInformation[0] = 0; /* FIXME ? */
320 rec.ExceptionInformation[1] = (DWORD)addr;
321 if (!(page_fault_code=VIRTUAL_HandleFault(addr)))
322 return;
323 rec.ExceptionCode = page_fault_code;
324 break;
325 default:FIXME("Unhandled SIGSEGV/%x\n",code);
326 break;
328 break;
329 case SIGBUS:
330 switch ( code & 0xffff ) {
331 case BUS_ADRALN:
332 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
333 break;
334 #ifdef BUS_ADRERR
335 case BUS_ADRERR:
336 #endif
337 #ifdef BUS_OBJERR
338 case BUS_OBJERR:
339 /* FIXME: correct for all cases ? */
340 rec.NumberParameters = 2;
341 rec.ExceptionInformation[0] = 0; /* FIXME ? */
342 rec.ExceptionInformation[1] = (DWORD)addr;
343 if (!(page_fault_code=VIRTUAL_HandleFault(addr)))
344 return;
345 rec.ExceptionCode = page_fault_code;
346 break;
347 #endif
348 default:FIXME("Unhandled SIGBUS/%x\n",code);
349 break;
351 break;
352 case SIGILL:
353 switch ( code & 0xffff ) {
354 case ILL_ILLOPC: /* illegal opcode */
355 #ifdef ILL_ILLOPN
356 case ILL_ILLOPN: /* illegal operand */
357 #endif
358 #ifdef ILL_ILLADR
359 case ILL_ILLADR: /* illegal addressing mode */
360 #endif
361 #ifdef ILL_ILLTRP
362 case ILL_ILLTRP: /* illegal trap */
363 #endif
364 #ifdef ILL_COPROC
365 case ILL_COPROC: /* coprocessor error */
366 #endif
367 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
368 break;
369 case ILL_PRVOPC: /* privileged opcode */
370 #ifdef ILL_PRVREG
371 case ILL_PRVREG: /* privileged register */
372 #endif
373 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
374 break;
375 #ifdef ILL_BADSTK
376 case ILL_BADSTK: /* internal stack error */
377 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
378 break;
379 #endif
380 default:FIXME("Unhandled SIGILL/%x\n", code);
381 break;
383 break;
385 EXC_RtlRaiseException( &rec, context );
388 /**********************************************************************
389 * do_trap
391 * Implementation of SIGTRAP handler.
393 static void do_trap( CONTEXT *context, int code, void * addr )
395 EXCEPTION_RECORD rec;
397 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
398 rec.ExceptionRecord = NULL;
399 rec.ExceptionAddress = addr;
400 rec.NumberParameters = 0;
402 /* FIXME: check if we might need to modify PC */
403 switch (code & 0xffff) {
404 #ifdef TRAP_BRKPT
405 case TRAP_BRKPT:
406 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
407 break;
408 #endif
409 #ifdef TRAP_TRACE
410 case TRAP_TRACE:
411 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
412 break;
413 #endif
414 default:FIXME("Unhandled SIGTRAP/%x\n", code);
415 break;
417 EXC_RtlRaiseException( &rec, context );
420 /**********************************************************************
421 * do_trap
423 * Implementation of SIGFPE handler.
425 static void do_fpe( CONTEXT *context, int code, void * addr )
427 EXCEPTION_RECORD rec;
429 switch ( code & 0xffff ) {
430 #ifdef FPE_FLTSUB
431 case FPE_FLTSUB:
432 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
433 break;
434 #endif
435 #ifdef FPE_INTDIV
436 case FPE_INTDIV:
437 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
438 break;
439 #endif
440 #ifdef FPE_INTOVF
441 case FPE_INTOVF:
442 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
443 break;
444 #endif
445 #ifdef FPE_FLTDIV
446 case FPE_FLTDIV:
447 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
448 break;
449 #endif
450 #ifdef FPE_FLTOVF
451 case FPE_FLTOVF:
452 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
453 break;
454 #endif
455 #ifdef FPE_FLTUND
456 case FPE_FLTUND:
457 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
458 break;
459 #endif
460 #ifdef FPE_FLTRES
461 case FPE_FLTRES:
462 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
463 break;
464 #endif
465 #ifdef FPE_FLTINV
466 case FPE_FLTINV:
467 #endif
468 default:
469 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
470 break;
472 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
473 rec.ExceptionRecord = NULL;
474 rec.ExceptionAddress = addr;
475 rec.NumberParameters = 0;
476 EXC_RtlRaiseException( &rec, context );
479 /**********************************************************************
480 * segv_handler
482 * Handler for SIGSEGV and related errors.
484 static HANDLER_DEF(segv_handler)
486 CONTEXT context;
487 save_context( &context, HANDLER_CONTEXT );
488 do_segv( &context, __siginfo->si_signo, __siginfo->si_errno, __siginfo->si_code, __siginfo->si_addr );
489 restore_context( &context, HANDLER_CONTEXT );
492 /**********************************************************************
493 * trap_handler
495 * Handler for SIGTRAP.
497 static HANDLER_DEF(trap_handler)
499 CONTEXT context;
500 save_context( &context, HANDLER_CONTEXT );
501 do_trap( &context, __siginfo->si_code, __siginfo->si_addr );
502 restore_context( &context, HANDLER_CONTEXT );
505 /**********************************************************************
506 * fpe_handler
508 * Handler for SIGFPE.
510 static HANDLER_DEF(fpe_handler)
512 CONTEXT context;
513 save_fpu( &context, HANDLER_CONTEXT );
514 save_context( &context, HANDLER_CONTEXT );
515 do_fpe( &context, __siginfo->si_code, __siginfo->si_addr );
516 restore_context( &context, HANDLER_CONTEXT );
517 restore_fpu( &context, HANDLER_CONTEXT );
520 /**********************************************************************
521 * int_handler
523 * Handler for SIGINT.
525 static HANDLER_DEF(int_handler)
527 if (!dispatch_signal(SIGINT))
529 EXCEPTION_RECORD rec;
530 CONTEXT context;
532 save_context( &context, HANDLER_CONTEXT );
533 rec.ExceptionCode = CONTROL_C_EXIT;
534 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
535 rec.ExceptionRecord = NULL;
536 rec.ExceptionAddress = (LPVOID)context.Iar;
537 rec.NumberParameters = 0;
538 EXC_RtlRaiseException( &rec, &context );
539 restore_context( &context, HANDLER_CONTEXT );
544 /**********************************************************************
545 * abrt_handler
547 * Handler for SIGABRT.
549 static HANDLER_DEF(abrt_handler)
551 EXCEPTION_RECORD rec;
552 CONTEXT context;
554 save_context( &context, HANDLER_CONTEXT );
555 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
556 rec.ExceptionFlags = EH_NONCONTINUABLE;
557 rec.ExceptionRecord = NULL;
558 rec.ExceptionAddress = (LPVOID)context.Iar;
559 rec.NumberParameters = 0;
560 EXC_RtlRaiseException( &rec, &context ); /* Should never return.. */
561 restore_context( &context, HANDLER_CONTEXT );
565 /**********************************************************************
566 * term_handler
568 * Handler for SIGTERM.
570 static HANDLER_DEF(term_handler)
572 server_abort_thread(0);
576 /**********************************************************************
577 * usr1_handler
579 * Handler for SIGUSR1, used to signal a thread that it got suspended.
581 static HANDLER_DEF(usr1_handler)
583 LARGE_INTEGER timeout;
585 /* wait with 0 timeout, will only return once the thread is no longer suspended */
586 timeout.QuadPart = 0;
587 NTDLL_wait_for_multiple_objects( 0, NULL, 0, &timeout );
591 /***********************************************************************
592 * set_handler
594 * Set a signal handler
596 static int set_handler( int sig, void (*func)() )
598 struct sigaction sig_act;
600 sig_act.sa_sigaction = func;
601 sigemptyset( &sig_act.sa_mask );
602 sigaddset( &sig_act.sa_mask, SIGINT );
603 sigaddset( &sig_act.sa_mask, SIGALRM );
605 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
606 return sigaction( sig, &sig_act, NULL );
610 /***********************************************************************
611 * __wine_set_signal_handler (NTDLL.@)
613 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
615 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
616 if (handlers[sig] != NULL) return -2;
617 handlers[sig] = wsh;
618 return 0;
622 /**********************************************************************
623 * SIGNAL_Init
625 BOOL SIGNAL_Init(void)
627 if (set_handler( SIGINT, (void (*)())int_handler ) == -1) goto error;
628 if (set_handler( SIGFPE, (void (*)())fpe_handler ) == -1) goto error;
629 if (set_handler( SIGSEGV, (void (*)())segv_handler ) == -1) goto error;
630 if (set_handler( SIGILL, (void (*)())segv_handler ) == -1) goto error;
631 if (set_handler( SIGABRT, (void (*)())abrt_handler ) == -1) goto error;
632 if (set_handler( SIGTERM, (void (*)())term_handler ) == -1) goto error;
633 if (set_handler( SIGUSR1, (void (*)())usr1_handler ) == -1) goto error;
634 #ifdef SIGBUS
635 if (set_handler( SIGBUS, (void (*)())segv_handler ) == -1) goto error;
636 #endif
637 #ifdef SIGTRAP
638 if (set_handler( SIGTRAP, (void (*)())trap_handler ) == -1) goto error;
639 #endif
641 return TRUE;
643 error:
644 perror("sigaction");
645 return FALSE;
649 /**********************************************************************
650 * __wine_enter_vm86 (NTDLL.@)
652 void __wine_enter_vm86( CONTEXT *context )
654 MESSAGE("vm86 mode not supported on this platform\n");
657 /**********************************************************************
658 * DbgBreakPoint (NTDLL.@)
660 void WINAPI DbgBreakPoint(void)
662 kill(getpid(), SIGTRAP);
665 /**********************************************************************
666 * DbgUserBreakPoint (NTDLL.@)
668 void WINAPI DbgUserBreakPoint(void)
670 kill(getpid(), SIGTRAP);
673 #endif /* __powerpc__ */