- call CertFreeCertificateContext from CertDeleteCertificateFromStore
[wine/multimedia.git] / dlls / ntdll / signal_powerpc.c
blob1e71edbce4e7ad8a07edb881036aa6f7256d6bcd
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 "winternl.h"
55 #include "wine/library.h"
56 #include "wine/exception.h"
57 #include "ntdll_misc.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(seh);
63 /***********************************************************************
64 * signal context platform-specific definitions
66 #ifdef linux
68 typedef struct ucontext SIGCONTEXT;
70 # define HANDLER_DEF(name) void name( int __signal, struct siginfo *__siginfo, SIGCONTEXT *__context )
71 # define HANDLER_CONTEXT (__context)
73 /* All Registers access - only for local access */
74 # define REG_sig(reg_name, context) ((context)->uc_mcontext.regs->reg_name)
77 /* Gpr Registers access */
78 # define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
80 # define IAR_sig(context) REG_sig(nip, context) /* Program counter */
81 # define MSR_sig(context) REG_sig(msr, context) /* Machine State Register (Supervisor) */
82 # define CTR_sig(context) REG_sig(ctr, context) /* Count register */
84 # define XER_sig(context) REG_sig(xer, context) /* User's integer exception register */
85 # define LR_sig(context) REG_sig(link, context) /* Link register */
86 # define CR_sig(context) REG_sig(ccr, context) /* Condition register */
88 /* Float Registers access */
89 # define FLOAT_sig(reg_num, context) (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
91 # define FPSCR_sig(context) (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
93 /* Exception Registers access */
94 # define DAR_sig(context) REG_sig(dar, context)
95 # define DSISR_sig(context) REG_sig(dsisr, context)
96 # define TRAP_sig(context) REG_sig(trap, context)
98 #endif /* linux */
100 #ifdef __APPLE__
102 # include <sys/ucontext.h>
104 # include <sys/types.h>
105 # include <signal.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
142 SIGBUS
143 #undef BUS_ADRERR
144 #undef BUS_OBJERR
145 SIGILL
146 #undef ILL_ILLOPN
147 #undef ILL_ILLTRP
148 #undef ILL_ILLADR
149 #undef ILL_COPROC
150 #undef ILL_PRVREG
151 #undef ILL_BADSTK
152 SIGTRAP
153 #undef TRAP_BRKPT
154 #undef TRAP_TRACE
155 SIGFPE
158 #endif /* __APPLE__ */
162 typedef int (*wine_signal_handler)(unsigned int sig);
164 static wine_signal_handler handlers[256];
166 /***********************************************************************
167 * dispatch_signal
169 inline static int dispatch_signal(unsigned int sig)
171 if (handlers[sig] == NULL) return 0;
172 return handlers[sig](sig);
175 /***********************************************************************
176 * save_context
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);
188 C(31);
189 #undef C
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 /***********************************************************************
207 * restore_context
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);
218 C(31);
219 #undef C
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 /***********************************************************************
237 * save_fpu
239 * Set the FPU context from a sigcontext.
241 inline static 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);
247 C(31);
248 #undef C
249 context->Fpscr = FPSCR_sig(sigcontext);
253 /***********************************************************************
254 * restore_fpu
256 * Restore the FPU context to a sigcontext.
258 inline static 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);
264 C(31);
265 #undef C
266 FPSCR_sig(sigcontext) = context->Fpscr;
270 /**********************************************************************
271 * get_fpu_code
273 * Get the FPU exception code from the FPU status.
275 static inline DWORD get_fpu_code( const CONTEXT *context )
277 DWORD status = context->Fpscr;
279 if (status & 0x01) /* IE */
281 if (status & 0x40) /* SF */
282 return EXCEPTION_FLT_STACK_CHECK;
283 else
284 return EXCEPTION_FLT_INVALID_OPERATION;
286 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
287 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
288 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
289 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
290 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
291 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
294 /**********************************************************************
295 * do_segv
297 * Implementation of SIGSEGV handler.
299 static void do_segv( CONTEXT *context, int trap, int err, int code, void * addr )
301 EXCEPTION_RECORD rec;
302 DWORD page_fault_code = EXCEPTION_ACCESS_VIOLATION;
304 rec.ExceptionRecord = NULL;
305 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
306 rec.ExceptionAddress = addr;
307 rec.NumberParameters = 0;
309 switch (trap) {
310 case SIGSEGV:
311 switch ( code & 0xffff ) {
312 case SEGV_MAPERR:
313 case SEGV_ACCERR:
314 rec.NumberParameters = 2;
315 rec.ExceptionInformation[0] = 0; /* FIXME ? */
316 rec.ExceptionInformation[1] = (ULONG_PTR)addr;
317 if (!(page_fault_code=VIRTUAL_HandleFault(addr)))
318 return;
319 rec.ExceptionCode = page_fault_code;
320 break;
321 default:FIXME("Unhandled SIGSEGV/%x\n",code);
322 break;
324 break;
325 case SIGBUS:
326 switch ( code & 0xffff ) {
327 case BUS_ADRALN:
328 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
329 break;
330 #ifdef BUS_ADRERR
331 case BUS_ADRERR:
332 #endif
333 #ifdef BUS_OBJERR
334 case BUS_OBJERR:
335 /* FIXME: correct for all cases ? */
336 rec.NumberParameters = 2;
337 rec.ExceptionInformation[0] = 0; /* FIXME ? */
338 rec.ExceptionInformation[1] = (ULONG_PTR)addr;
339 if (!(page_fault_code=VIRTUAL_HandleFault(addr)))
340 return;
341 rec.ExceptionCode = page_fault_code;
342 break;
343 #endif
344 default:FIXME("Unhandled SIGBUS/%x\n",code);
345 break;
347 break;
348 case SIGILL:
349 switch ( code & 0xffff ) {
350 case ILL_ILLOPC: /* illegal opcode */
351 #ifdef ILL_ILLOPN
352 case ILL_ILLOPN: /* illegal operand */
353 #endif
354 #ifdef ILL_ILLADR
355 case ILL_ILLADR: /* illegal addressing mode */
356 #endif
357 #ifdef ILL_ILLTRP
358 case ILL_ILLTRP: /* illegal trap */
359 #endif
360 #ifdef ILL_COPROC
361 case ILL_COPROC: /* coprocessor error */
362 #endif
363 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
364 break;
365 case ILL_PRVOPC: /* privileged opcode */
366 #ifdef ILL_PRVREG
367 case ILL_PRVREG: /* privileged register */
368 #endif
369 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
370 break;
371 #ifdef ILL_BADSTK
372 case ILL_BADSTK: /* internal stack error */
373 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
374 break;
375 #endif
376 default:FIXME("Unhandled SIGILL/%x\n", code);
377 break;
379 break;
381 __regs_RtlRaiseException( &rec, context );
384 /**********************************************************************
385 * do_trap
387 * Implementation of SIGTRAP handler.
389 static void do_trap( CONTEXT *context, int code, void * addr )
391 EXCEPTION_RECORD rec;
393 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
394 rec.ExceptionRecord = NULL;
395 rec.ExceptionAddress = addr;
396 rec.NumberParameters = 0;
398 /* FIXME: check if we might need to modify PC */
399 switch (code & 0xffff) {
400 #ifdef TRAP_BRKPT
401 case TRAP_BRKPT:
402 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
403 break;
404 #endif
405 #ifdef TRAP_TRACE
406 case TRAP_TRACE:
407 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
408 break;
409 #endif
410 default:FIXME("Unhandled SIGTRAP/%x\n", code);
411 break;
413 __regs_RtlRaiseException( &rec, context );
416 /**********************************************************************
417 * do_trap
419 * Implementation of SIGFPE handler.
421 static void do_fpe( CONTEXT *context, int code, void * addr )
423 EXCEPTION_RECORD rec;
425 switch ( code & 0xffff ) {
426 #ifdef FPE_FLTSUB
427 case FPE_FLTSUB:
428 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
429 break;
430 #endif
431 #ifdef FPE_INTDIV
432 case FPE_INTDIV:
433 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
434 break;
435 #endif
436 #ifdef FPE_INTOVF
437 case FPE_INTOVF:
438 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
439 break;
440 #endif
441 #ifdef FPE_FLTDIV
442 case FPE_FLTDIV:
443 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
444 break;
445 #endif
446 #ifdef FPE_FLTOVF
447 case FPE_FLTOVF:
448 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
449 break;
450 #endif
451 #ifdef FPE_FLTUND
452 case FPE_FLTUND:
453 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
454 break;
455 #endif
456 #ifdef FPE_FLTRES
457 case FPE_FLTRES:
458 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
459 break;
460 #endif
461 #ifdef FPE_FLTINV
462 case FPE_FLTINV:
463 #endif
464 default:
465 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
466 break;
468 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
469 rec.ExceptionRecord = NULL;
470 rec.ExceptionAddress = addr;
471 rec.NumberParameters = 0;
472 __regs_RtlRaiseException( &rec, context );
475 /**********************************************************************
476 * segv_handler
478 * Handler for SIGSEGV and related errors.
480 static HANDLER_DEF(segv_handler)
482 CONTEXT context;
483 save_context( &context, HANDLER_CONTEXT );
484 do_segv( &context, __siginfo->si_signo, __siginfo->si_errno, __siginfo->si_code, __siginfo->si_addr );
485 restore_context( &context, HANDLER_CONTEXT );
488 /**********************************************************************
489 * trap_handler
491 * Handler for SIGTRAP.
493 static HANDLER_DEF(trap_handler)
495 CONTEXT context;
496 save_context( &context, HANDLER_CONTEXT );
497 do_trap( &context, __siginfo->si_code, __siginfo->si_addr );
498 restore_context( &context, HANDLER_CONTEXT );
501 /**********************************************************************
502 * fpe_handler
504 * Handler for SIGFPE.
506 static HANDLER_DEF(fpe_handler)
508 CONTEXT context;
509 save_fpu( &context, HANDLER_CONTEXT );
510 save_context( &context, HANDLER_CONTEXT );
511 do_fpe( &context, __siginfo->si_code, __siginfo->si_addr );
512 restore_context( &context, HANDLER_CONTEXT );
513 restore_fpu( &context, HANDLER_CONTEXT );
516 /**********************************************************************
517 * int_handler
519 * Handler for SIGINT.
521 static HANDLER_DEF(int_handler)
523 if (!dispatch_signal(SIGINT))
525 EXCEPTION_RECORD rec;
526 CONTEXT context;
528 save_context( &context, HANDLER_CONTEXT );
529 rec.ExceptionCode = CONTROL_C_EXIT;
530 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
531 rec.ExceptionRecord = NULL;
532 rec.ExceptionAddress = (LPVOID)context.Iar;
533 rec.NumberParameters = 0;
534 __regs_RtlRaiseException( &rec, &context );
535 restore_context( &context, HANDLER_CONTEXT );
540 /**********************************************************************
541 * abrt_handler
543 * Handler for SIGABRT.
545 static HANDLER_DEF(abrt_handler)
547 EXCEPTION_RECORD rec;
548 CONTEXT context;
550 save_context( &context, HANDLER_CONTEXT );
551 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
552 rec.ExceptionFlags = EH_NONCONTINUABLE;
553 rec.ExceptionRecord = NULL;
554 rec.ExceptionAddress = (LPVOID)context.Iar;
555 rec.NumberParameters = 0;
556 __regs_RtlRaiseException( &rec, &context ); /* Should never return.. */
557 restore_context( &context, HANDLER_CONTEXT );
561 /**********************************************************************
562 * term_handler
564 * Handler for SIGTERM.
566 static HANDLER_DEF(term_handler)
568 server_abort_thread(0);
572 /**********************************************************************
573 * usr1_handler
575 * Handler for SIGUSR1, used to signal a thread that it got suspended.
577 static HANDLER_DEF(usr1_handler)
579 LARGE_INTEGER timeout;
581 /* wait with 0 timeout, will only return once the thread is no longer suspended */
582 timeout.QuadPart = 0;
583 NTDLL_wait_for_multiple_objects( 0, NULL, 0, &timeout, 0 );
587 /***********************************************************************
588 * set_handler
590 * Set a signal handler
592 static int set_handler( int sig, void (*func)() )
594 struct sigaction sig_act;
596 sig_act.sa_sigaction = func;
597 sigemptyset( &sig_act.sa_mask );
598 sigaddset( &sig_act.sa_mask, SIGINT );
599 sigaddset( &sig_act.sa_mask, SIGALRM );
601 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
602 return sigaction( sig, &sig_act, NULL );
606 /***********************************************************************
607 * __wine_set_signal_handler (NTDLL.@)
609 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
611 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
612 if (handlers[sig] != NULL) return -2;
613 handlers[sig] = wsh;
614 return 0;
618 /**********************************************************************
619 * SIGNAL_Init
621 BOOL SIGNAL_Init(void)
623 if (set_handler( SIGINT, (void (*)())int_handler ) == -1) goto error;
624 if (set_handler( SIGFPE, (void (*)())fpe_handler ) == -1) goto error;
625 if (set_handler( SIGSEGV, (void (*)())segv_handler ) == -1) goto error;
626 if (set_handler( SIGILL, (void (*)())segv_handler ) == -1) goto error;
627 if (set_handler( SIGABRT, (void (*)())abrt_handler ) == -1) goto error;
628 if (set_handler( SIGTERM, (void (*)())term_handler ) == -1) goto error;
629 if (set_handler( SIGUSR1, (void (*)())usr1_handler ) == -1) goto error;
630 #ifdef SIGBUS
631 if (set_handler( SIGBUS, (void (*)())segv_handler ) == -1) goto error;
632 #endif
633 #ifdef SIGTRAP
634 if (set_handler( SIGTRAP, (void (*)())trap_handler ) == -1) goto error;
635 #endif
637 return TRUE;
639 error:
640 perror("sigaction");
641 return FALSE;
645 /**********************************************************************
646 * __wine_enter_vm86 (NTDLL.@)
648 void __wine_enter_vm86( CONTEXT *context )
650 MESSAGE("vm86 mode not supported on this platform\n");
653 /**********************************************************************
654 * DbgBreakPoint (NTDLL.@)
656 void WINAPI DbgBreakPoint(void)
658 kill(getpid(), SIGTRAP);
661 /**********************************************************************
662 * DbgUserBreakPoint (NTDLL.@)
664 void WINAPI DbgUserBreakPoint(void)
666 kill(getpid(), SIGTRAP);
669 #endif /* __powerpc__ */