winecfg: Update Portuguese translation.
[wine/multimedia.git] / dlls / ntdll / signal_powerpc.c
blob662d97ba91d15dddf0bdbdd9f23ba379328e050c
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #ifdef __powerpc__
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
37 #endif
38 #ifdef HAVE_SYSCALL_H
39 # include <syscall.h>
40 #else
41 # ifdef HAVE_SYS_SYSCALL_H
42 # include <sys/syscall.h>
43 # endif
44 #endif
46 #ifdef HAVE_SYS_VM86_H
47 # include <sys/vm86.h>
48 #endif
50 #ifdef HAVE_SYS_SIGNAL_H
51 # include <sys/signal.h>
52 #endif
54 #include "ntstatus.h"
55 #define WIN32_NO_STATUS
56 #include "windef.h"
57 #include "winternl.h"
58 #include "wine/library.h"
59 #include "wine/exception.h"
60 #include "ntdll_misc.h"
61 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(seh);
65 static pthread_key_t teb_key;
67 /***********************************************************************
68 * signal context platform-specific definitions
70 #ifdef linux
72 /* All Registers access - only for local access */
73 # define REG_sig(reg_name, context) ((context)->uc_mcontext.regs->reg_name)
76 /* Gpr Registers access */
77 # define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
79 # define IAR_sig(context) REG_sig(nip, context) /* Program counter */
80 # define MSR_sig(context) REG_sig(msr, context) /* Machine State Register (Supervisor) */
81 # define CTR_sig(context) REG_sig(ctr, context) /* Count register */
83 # define XER_sig(context) REG_sig(xer, context) /* User's integer exception register */
84 # define LR_sig(context) REG_sig(link, context) /* Link register */
85 # define CR_sig(context) REG_sig(ccr, context) /* Condition register */
87 /* Float Registers access */
88 # define FLOAT_sig(reg_num, context) (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
90 # define FPSCR_sig(context) (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
92 /* Exception Registers access */
93 # define DAR_sig(context) REG_sig(dar, context)
94 # define DSISR_sig(context) REG_sig(dsisr, context)
95 # define TRAP_sig(context) REG_sig(trap, context)
97 #endif /* linux */
99 #ifdef __APPLE__
101 # include <sys/ucontext.h>
102 # include <sys/types.h>
104 /* All Registers access - only for local access */
105 # define REG_sig(reg_name, context) ((context)->uc_mcontext->ss.reg_name)
106 # define FLOATREG_sig(reg_name, context) ((context)->uc_mcontext->fs.reg_name)
107 # define EXCEPREG_sig(reg_name, context) ((context)->uc_mcontext->es.reg_name)
108 # define VECREG_sig(reg_name, context) ((context)->uc_mcontext->vs.reg_name)
110 /* Gpr Registers access */
111 # define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
113 # define IAR_sig(context) REG_sig(srr0, context) /* Program counter */
114 # define MSR_sig(context) REG_sig(srr1, context) /* Machine State Register (Supervisor) */
115 # define CTR_sig(context) REG_sig(ctr, context)
117 # define XER_sig(context) REG_sig(xer, context) /* Link register */
118 # define LR_sig(context) REG_sig(lr, context) /* User's integer exception register */
119 # define CR_sig(context) REG_sig(cr, context) /* Condition register */
121 /* Float Registers access */
122 # define FLOAT_sig(reg_num, context) FLOATREG_sig(fpregs[reg_num], context)
124 # define FPSCR_sig(context) FLOATREG_sig(fpscr, context)
126 /* Exception Registers access */
127 # define DAR_sig(context) EXCEPREG_sig(dar, context) /* Fault registers for coredump */
128 # define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
129 # define TRAP_sig(context) EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
131 /* Signal defs : Those are undefined on darwin
132 SIGBUS
133 #undef BUS_ADRERR
134 #undef BUS_OBJERR
135 SIGILL
136 #undef ILL_ILLOPN
137 #undef ILL_ILLTRP
138 #undef ILL_ILLADR
139 #undef ILL_COPROC
140 #undef ILL_PRVREG
141 #undef ILL_BADSTK
142 SIGTRAP
143 #undef TRAP_BRKPT
144 #undef TRAP_TRACE
145 SIGFPE
148 #endif /* __APPLE__ */
152 typedef int (*wine_signal_handler)(unsigned int sig);
154 static wine_signal_handler handlers[256];
156 /***********************************************************************
157 * dispatch_signal
159 static inline int dispatch_signal(unsigned int sig)
161 if (handlers[sig] == NULL) return 0;
162 return handlers[sig](sig);
165 /***********************************************************************
166 * save_context
168 * Set the register values from a sigcontext.
170 static void save_context( CONTEXT *context, const ucontext_t *sigcontext )
173 #define C(x) context->Gpr##x = GPR_sig(x,sigcontext)
174 /* Save Gpr registers */
175 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
176 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
177 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
178 C(31);
179 #undef C
181 context->Iar = IAR_sig(sigcontext); /* Program Counter */
182 context->Msr = MSR_sig(sigcontext); /* Machine State Register (Supervisor) */
183 context->Ctr = CTR_sig(sigcontext);
185 context->Xer = XER_sig(sigcontext);
186 context->Lr = LR_sig(sigcontext);
187 context->Cr = CR_sig(sigcontext);
189 /* Saving Exception regs */
190 context->Dar = DAR_sig(sigcontext);
191 context->Dsisr = DSISR_sig(sigcontext);
192 context->Trap = TRAP_sig(sigcontext);
196 /***********************************************************************
197 * restore_context
199 * Build a sigcontext from the register values.
201 static void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
204 #define C(x) GPR_sig(x,sigcontext) = context->Gpr##x
205 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
206 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
207 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
208 C(31);
209 #undef C
211 IAR_sig(sigcontext) = context->Iar; /* Program Counter */
212 MSR_sig(sigcontext) = context->Msr; /* Machine State Register (Supervisor) */
213 CTR_sig(sigcontext) = context->Ctr;
215 XER_sig(sigcontext) = context->Xer;
216 LR_sig(sigcontext) = context->Lr;
217 CR_sig(sigcontext) = context->Cr;
219 /* Setting Exception regs */
220 DAR_sig(sigcontext) = context->Dar;
221 DSISR_sig(sigcontext) = context->Dsisr;
222 TRAP_sig(sigcontext) = context->Trap;
226 /***********************************************************************
227 * save_fpu
229 * Set the FPU context from a sigcontext.
231 static inline void save_fpu( CONTEXT *context, const ucontext_t *sigcontext )
233 #define C(x) context->Fpr##x = FLOAT_sig(x,sigcontext)
234 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
235 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
236 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
237 C(31);
238 #undef C
239 context->Fpscr = FPSCR_sig(sigcontext);
243 /***********************************************************************
244 * restore_fpu
246 * Restore the FPU context to a sigcontext.
248 static inline void restore_fpu( CONTEXT *context, const ucontext_t *sigcontext )
250 #define C(x) FLOAT_sig(x,sigcontext) = context->Fpr##x
251 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
252 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
253 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
254 C(31);
255 #undef C
256 FPSCR_sig(sigcontext) = context->Fpscr;
260 /***********************************************************************
261 * RtlCaptureContext (NTDLL.@)
263 void WINAPI RtlCaptureContext( CONTEXT *context )
265 FIXME("not implemented\n");
266 memset( context, 0, sizeof(*context) );
270 /***********************************************************************
271 * set_cpu_context
273 * Set the new CPU context.
275 void set_cpu_context( const CONTEXT *context )
277 FIXME("not implemented\n");
281 /***********************************************************************
282 * copy_context
284 * Copy a register context according to the flags.
286 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
288 if (flags & CONTEXT_CONTROL)
290 to->Msr = from->Msr;
291 to->Ctr = from->Ctr;
292 to->Iar = from->Iar;
293 to->Lr = from->Lr;
294 to->Dar = from->Dar;
295 to->Dsisr = from->Dsisr;
296 to->Trap = from->Trap;
298 if (flags & CONTEXT_INTEGER)
300 to->Gpr0 = from->Gpr0;
301 to->Gpr1 = from->Gpr1;
302 to->Gpr2 = from->Gpr2;
303 to->Gpr3 = from->Gpr3;
304 to->Gpr4 = from->Gpr4;
305 to->Gpr5 = from->Gpr5;
306 to->Gpr6 = from->Gpr6;
307 to->Gpr7 = from->Gpr7;
308 to->Gpr8 = from->Gpr8;
309 to->Gpr9 = from->Gpr9;
310 to->Gpr10 = from->Gpr10;
311 to->Gpr11 = from->Gpr11;
312 to->Gpr12 = from->Gpr12;
313 to->Gpr13 = from->Gpr13;
314 to->Gpr14 = from->Gpr14;
315 to->Gpr15 = from->Gpr15;
316 to->Gpr16 = from->Gpr16;
317 to->Gpr17 = from->Gpr17;
318 to->Gpr18 = from->Gpr18;
319 to->Gpr19 = from->Gpr19;
320 to->Gpr20 = from->Gpr20;
321 to->Gpr21 = from->Gpr21;
322 to->Gpr22 = from->Gpr22;
323 to->Gpr23 = from->Gpr23;
324 to->Gpr24 = from->Gpr24;
325 to->Gpr25 = from->Gpr25;
326 to->Gpr26 = from->Gpr26;
327 to->Gpr27 = from->Gpr27;
328 to->Gpr28 = from->Gpr28;
329 to->Gpr29 = from->Gpr29;
330 to->Gpr30 = from->Gpr30;
331 to->Gpr31 = from->Gpr31;
332 to->Xer = from->Xer;
333 to->Cr = from->Cr;
335 if (flags & CONTEXT_FLOATING_POINT)
337 to->Fpr0 = from->Fpr0;
338 to->Fpr1 = from->Fpr1;
339 to->Fpr2 = from->Fpr2;
340 to->Fpr3 = from->Fpr3;
341 to->Fpr4 = from->Fpr4;
342 to->Fpr5 = from->Fpr5;
343 to->Fpr6 = from->Fpr6;
344 to->Fpr7 = from->Fpr7;
345 to->Fpr8 = from->Fpr8;
346 to->Fpr9 = from->Fpr9;
347 to->Fpr10 = from->Fpr10;
348 to->Fpr11 = from->Fpr11;
349 to->Fpr12 = from->Fpr12;
350 to->Fpr13 = from->Fpr13;
351 to->Fpr14 = from->Fpr14;
352 to->Fpr15 = from->Fpr15;
353 to->Fpr16 = from->Fpr16;
354 to->Fpr17 = from->Fpr17;
355 to->Fpr18 = from->Fpr18;
356 to->Fpr19 = from->Fpr19;
357 to->Fpr20 = from->Fpr20;
358 to->Fpr21 = from->Fpr21;
359 to->Fpr22 = from->Fpr22;
360 to->Fpr23 = from->Fpr23;
361 to->Fpr24 = from->Fpr24;
362 to->Fpr25 = from->Fpr25;
363 to->Fpr26 = from->Fpr26;
364 to->Fpr27 = from->Fpr27;
365 to->Fpr28 = from->Fpr28;
366 to->Fpr29 = from->Fpr29;
367 to->Fpr30 = from->Fpr30;
368 to->Fpr31 = from->Fpr31;
369 to->Fpscr = from->Fpscr;
374 /***********************************************************************
375 * context_to_server
377 * Convert a register context to the server format.
379 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
381 DWORD flags = from->ContextFlags; /* no CPU id? */
383 memset( to, 0, sizeof(*to) );
384 to->cpu = CPU_POWERPC;
386 if (flags & CONTEXT_CONTROL)
388 to->flags |= SERVER_CTX_CONTROL;
389 to->ctl.powerpc_regs.iar = from->Iar;
390 to->ctl.powerpc_regs.msr = from->Msr;
391 to->ctl.powerpc_regs.ctr = from->Ctr;
392 to->ctl.powerpc_regs.lr = from->Lr;
393 to->ctl.powerpc_regs.dar = from->Dar;
394 to->ctl.powerpc_regs.dsisr = from->Dsisr;
395 to->ctl.powerpc_regs.trap = from->Trap;
397 if (flags & CONTEXT_INTEGER)
399 to->flags |= SERVER_CTX_INTEGER;
400 to->integer.powerpc_regs.gpr[0] = from->Gpr0;
401 to->integer.powerpc_regs.gpr[1] = from->Gpr1;
402 to->integer.powerpc_regs.gpr[2] = from->Gpr2;
403 to->integer.powerpc_regs.gpr[3] = from->Gpr3;
404 to->integer.powerpc_regs.gpr[4] = from->Gpr4;
405 to->integer.powerpc_regs.gpr[5] = from->Gpr5;
406 to->integer.powerpc_regs.gpr[6] = from->Gpr6;
407 to->integer.powerpc_regs.gpr[7] = from->Gpr7;
408 to->integer.powerpc_regs.gpr[8] = from->Gpr8;
409 to->integer.powerpc_regs.gpr[9] = from->Gpr9;
410 to->integer.powerpc_regs.gpr[10] = from->Gpr10;
411 to->integer.powerpc_regs.gpr[11] = from->Gpr11;
412 to->integer.powerpc_regs.gpr[12] = from->Gpr12;
413 to->integer.powerpc_regs.gpr[13] = from->Gpr13;
414 to->integer.powerpc_regs.gpr[14] = from->Gpr14;
415 to->integer.powerpc_regs.gpr[15] = from->Gpr15;
416 to->integer.powerpc_regs.gpr[16] = from->Gpr16;
417 to->integer.powerpc_regs.gpr[17] = from->Gpr17;
418 to->integer.powerpc_regs.gpr[18] = from->Gpr18;
419 to->integer.powerpc_regs.gpr[19] = from->Gpr19;
420 to->integer.powerpc_regs.gpr[20] = from->Gpr20;
421 to->integer.powerpc_regs.gpr[21] = from->Gpr21;
422 to->integer.powerpc_regs.gpr[22] = from->Gpr22;
423 to->integer.powerpc_regs.gpr[23] = from->Gpr23;
424 to->integer.powerpc_regs.gpr[24] = from->Gpr24;
425 to->integer.powerpc_regs.gpr[25] = from->Gpr25;
426 to->integer.powerpc_regs.gpr[26] = from->Gpr26;
427 to->integer.powerpc_regs.gpr[27] = from->Gpr27;
428 to->integer.powerpc_regs.gpr[28] = from->Gpr28;
429 to->integer.powerpc_regs.gpr[29] = from->Gpr29;
430 to->integer.powerpc_regs.gpr[30] = from->Gpr30;
431 to->integer.powerpc_regs.gpr[31] = from->Gpr31;
432 to->integer.powerpc_regs.xer = from->Xer;
433 to->integer.powerpc_regs.cr = from->Cr;
435 if (flags & CONTEXT_FLOATING_POINT)
437 to->flags |= SERVER_CTX_FLOATING_POINT;
438 to->fp.powerpc_regs.fpr[0] = from->Fpr0;
439 to->fp.powerpc_regs.fpr[1] = from->Fpr1;
440 to->fp.powerpc_regs.fpr[2] = from->Fpr2;
441 to->fp.powerpc_regs.fpr[3] = from->Fpr3;
442 to->fp.powerpc_regs.fpr[4] = from->Fpr4;
443 to->fp.powerpc_regs.fpr[5] = from->Fpr5;
444 to->fp.powerpc_regs.fpr[6] = from->Fpr6;
445 to->fp.powerpc_regs.fpr[7] = from->Fpr7;
446 to->fp.powerpc_regs.fpr[8] = from->Fpr8;
447 to->fp.powerpc_regs.fpr[9] = from->Fpr9;
448 to->fp.powerpc_regs.fpr[10] = from->Fpr10;
449 to->fp.powerpc_regs.fpr[11] = from->Fpr11;
450 to->fp.powerpc_regs.fpr[12] = from->Fpr12;
451 to->fp.powerpc_regs.fpr[13] = from->Fpr13;
452 to->fp.powerpc_regs.fpr[14] = from->Fpr14;
453 to->fp.powerpc_regs.fpr[15] = from->Fpr15;
454 to->fp.powerpc_regs.fpr[16] = from->Fpr16;
455 to->fp.powerpc_regs.fpr[17] = from->Fpr17;
456 to->fp.powerpc_regs.fpr[18] = from->Fpr18;
457 to->fp.powerpc_regs.fpr[19] = from->Fpr19;
458 to->fp.powerpc_regs.fpr[20] = from->Fpr20;
459 to->fp.powerpc_regs.fpr[21] = from->Fpr21;
460 to->fp.powerpc_regs.fpr[22] = from->Fpr22;
461 to->fp.powerpc_regs.fpr[23] = from->Fpr23;
462 to->fp.powerpc_regs.fpr[24] = from->Fpr24;
463 to->fp.powerpc_regs.fpr[25] = from->Fpr25;
464 to->fp.powerpc_regs.fpr[26] = from->Fpr26;
465 to->fp.powerpc_regs.fpr[27] = from->Fpr27;
466 to->fp.powerpc_regs.fpr[28] = from->Fpr28;
467 to->fp.powerpc_regs.fpr[29] = from->Fpr29;
468 to->fp.powerpc_regs.fpr[30] = from->Fpr30;
469 to->fp.powerpc_regs.fpr[31] = from->Fpr31;
470 to->fp.powerpc_regs.fpscr = from->Fpscr;
472 return STATUS_SUCCESS;
476 /***********************************************************************
477 * context_from_server
479 * Convert a register context from the server format.
481 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
483 if (from->cpu != CPU_POWERPC) return STATUS_INVALID_PARAMETER;
485 to->ContextFlags = 0; /* no CPU id? */
486 if (from->flags & SERVER_CTX_CONTROL)
488 to->ContextFlags |= CONTEXT_CONTROL;
489 to->Msr = from->ctl.powerpc_regs.msr;
490 to->Ctr = from->ctl.powerpc_regs.ctr;
491 to->Iar = from->ctl.powerpc_regs.iar;
492 to->Lr = from->ctl.powerpc_regs.lr;
493 to->Dar = from->ctl.powerpc_regs.dar;
494 to->Dsisr = from->ctl.powerpc_regs.dsisr;
495 to->Trap = from->ctl.powerpc_regs.trap;
497 if (from->flags & SERVER_CTX_INTEGER)
499 to->ContextFlags |= CONTEXT_INTEGER;
500 to->Gpr0 = from->integer.powerpc_regs.gpr[0];
501 to->Gpr1 = from->integer.powerpc_regs.gpr[1];
502 to->Gpr2 = from->integer.powerpc_regs.gpr[2];
503 to->Gpr3 = from->integer.powerpc_regs.gpr[3];
504 to->Gpr4 = from->integer.powerpc_regs.gpr[4];
505 to->Gpr5 = from->integer.powerpc_regs.gpr[5];
506 to->Gpr6 = from->integer.powerpc_regs.gpr[6];
507 to->Gpr7 = from->integer.powerpc_regs.gpr[7];
508 to->Gpr8 = from->integer.powerpc_regs.gpr[8];
509 to->Gpr9 = from->integer.powerpc_regs.gpr[9];
510 to->Gpr10 = from->integer.powerpc_regs.gpr[10];
511 to->Gpr11 = from->integer.powerpc_regs.gpr[11];
512 to->Gpr12 = from->integer.powerpc_regs.gpr[12];
513 to->Gpr13 = from->integer.powerpc_regs.gpr[13];
514 to->Gpr14 = from->integer.powerpc_regs.gpr[14];
515 to->Gpr15 = from->integer.powerpc_regs.gpr[15];
516 to->Gpr16 = from->integer.powerpc_regs.gpr[16];
517 to->Gpr17 = from->integer.powerpc_regs.gpr[17];
518 to->Gpr18 = from->integer.powerpc_regs.gpr[18];
519 to->Gpr19 = from->integer.powerpc_regs.gpr[19];
520 to->Gpr20 = from->integer.powerpc_regs.gpr[20];
521 to->Gpr21 = from->integer.powerpc_regs.gpr[21];
522 to->Gpr22 = from->integer.powerpc_regs.gpr[22];
523 to->Gpr23 = from->integer.powerpc_regs.gpr[23];
524 to->Gpr24 = from->integer.powerpc_regs.gpr[24];
525 to->Gpr25 = from->integer.powerpc_regs.gpr[25];
526 to->Gpr26 = from->integer.powerpc_regs.gpr[26];
527 to->Gpr27 = from->integer.powerpc_regs.gpr[27];
528 to->Gpr28 = from->integer.powerpc_regs.gpr[28];
529 to->Gpr29 = from->integer.powerpc_regs.gpr[29];
530 to->Gpr30 = from->integer.powerpc_regs.gpr[30];
531 to->Gpr31 = from->integer.powerpc_regs.gpr[31];
532 to->Xer = from->integer.powerpc_regs.xer;
533 to->Cr = from->integer.powerpc_regs.cr;
535 if (from->flags & SERVER_CTX_FLOATING_POINT)
537 to->ContextFlags |= CONTEXT_FLOATING_POINT;
538 to->Fpr0 = from->fp.powerpc_regs.fpr[0];
539 to->Fpr1 = from->fp.powerpc_regs.fpr[1];
540 to->Fpr2 = from->fp.powerpc_regs.fpr[2];
541 to->Fpr3 = from->fp.powerpc_regs.fpr[3];
542 to->Fpr4 = from->fp.powerpc_regs.fpr[4];
543 to->Fpr5 = from->fp.powerpc_regs.fpr[5];
544 to->Fpr6 = from->fp.powerpc_regs.fpr[6];
545 to->Fpr7 = from->fp.powerpc_regs.fpr[7];
546 to->Fpr8 = from->fp.powerpc_regs.fpr[8];
547 to->Fpr9 = from->fp.powerpc_regs.fpr[9];
548 to->Fpr10 = from->fp.powerpc_regs.fpr[10];
549 to->Fpr11 = from->fp.powerpc_regs.fpr[11];
550 to->Fpr12 = from->fp.powerpc_regs.fpr[12];
551 to->Fpr13 = from->fp.powerpc_regs.fpr[13];
552 to->Fpr14 = from->fp.powerpc_regs.fpr[14];
553 to->Fpr15 = from->fp.powerpc_regs.fpr[15];
554 to->Fpr16 = from->fp.powerpc_regs.fpr[16];
555 to->Fpr17 = from->fp.powerpc_regs.fpr[17];
556 to->Fpr18 = from->fp.powerpc_regs.fpr[18];
557 to->Fpr19 = from->fp.powerpc_regs.fpr[19];
558 to->Fpr20 = from->fp.powerpc_regs.fpr[20];
559 to->Fpr21 = from->fp.powerpc_regs.fpr[21];
560 to->Fpr22 = from->fp.powerpc_regs.fpr[22];
561 to->Fpr23 = from->fp.powerpc_regs.fpr[23];
562 to->Fpr24 = from->fp.powerpc_regs.fpr[24];
563 to->Fpr25 = from->fp.powerpc_regs.fpr[25];
564 to->Fpr26 = from->fp.powerpc_regs.fpr[26];
565 to->Fpr27 = from->fp.powerpc_regs.fpr[27];
566 to->Fpr28 = from->fp.powerpc_regs.fpr[28];
567 to->Fpr29 = from->fp.powerpc_regs.fpr[29];
568 to->Fpr30 = from->fp.powerpc_regs.fpr[30];
569 to->Fpr31 = from->fp.powerpc_regs.fpr[31];
570 to->Fpscr = from->fp.powerpc_regs.fpscr;
572 return STATUS_SUCCESS;
576 /**********************************************************************
577 * get_fpu_code
579 * Get the FPU exception code from the FPU status.
581 static inline DWORD get_fpu_code( const CONTEXT *context )
583 DWORD status = context->Fpscr;
585 if (status & 0x01) /* IE */
587 if (status & 0x40) /* SF */
588 return EXCEPTION_FLT_STACK_CHECK;
589 else
590 return EXCEPTION_FLT_INVALID_OPERATION;
592 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
593 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
594 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
595 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
596 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
597 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
601 /**********************************************************************
602 * call_stack_handlers
604 * Call the stack handlers chain.
606 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
608 EXCEPTION_POINTERS ptrs;
610 FIXME( "not implemented on PowerPC\n" );
612 /* hack: call unhandled exception filter directly */
613 ptrs.ExceptionRecord = rec;
614 ptrs.ContextRecord = context;
615 unhandled_exception_filter( &ptrs );
616 return STATUS_UNHANDLED_EXCEPTION;
620 /*******************************************************************
621 * raise_exception
623 * Implementation of NtRaiseException.
625 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
627 NTSTATUS status;
629 if (first_chance)
631 DWORD c;
633 TRACE( "code=%x flags=%x addr=%p ip=%x tid=%04x\n",
634 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
635 context->Iar, GetCurrentThreadId() );
636 for (c = 0; c < rec->NumberParameters; c++)
637 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
638 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
640 if (rec->ExceptionInformation[1] >> 16)
641 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
642 rec->ExceptionAddress,
643 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
644 else
645 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
646 rec->ExceptionAddress,
647 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
649 else
651 /* FIXME: dump context */
654 status = send_debug_event( rec, TRUE, context );
655 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
656 return STATUS_SUCCESS;
658 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
659 return STATUS_SUCCESS;
661 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
662 return status;
665 /* last chance exception */
667 status = send_debug_event( rec, FALSE, context );
668 if (status != DBG_CONTINUE)
670 if (rec->ExceptionFlags & EH_STACK_INVALID)
671 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
672 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
673 ERR("Process attempted to continue execution after noncontinuable exception.\n");
674 else
675 ERR("Unhandled exception code %x flags %x addr %p\n",
676 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
677 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
679 return STATUS_SUCCESS;
683 /**********************************************************************
684 * segv_handler
686 * Handler for SIGSEGV and related errors.
688 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
690 EXCEPTION_RECORD rec;
691 CONTEXT context;
692 NTSTATUS status;
694 save_context( &context, sigcontext );
696 rec.ExceptionRecord = NULL;
697 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
698 rec.ExceptionAddress = (LPVOID)context.Iar;
699 rec.NumberParameters = 0;
701 switch (signal)
703 case SIGSEGV:
704 switch (siginfo->si_code & 0xffff)
706 case SEGV_MAPERR:
707 case SEGV_ACCERR:
708 rec.NumberParameters = 2;
709 rec.ExceptionInformation[0] = 0; /* FIXME ? */
710 rec.ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
711 if (!(rec.ExceptionCode = virtual_handle_fault(siginfo->si_addr, rec.ExceptionInformation[0])))
712 goto done;
713 break;
714 default:
715 FIXME("Unhandled SIGSEGV/%x\n",siginfo->si_code);
716 break;
718 break;
719 case SIGBUS:
720 switch (siginfo->si_code & 0xffff)
722 case BUS_ADRALN:
723 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
724 break;
725 #ifdef BUS_ADRERR
726 case BUS_ADRERR:
727 #endif
728 #ifdef BUS_OBJERR
729 case BUS_OBJERR:
730 /* FIXME: correct for all cases ? */
731 rec.NumberParameters = 2;
732 rec.ExceptionInformation[0] = 0; /* FIXME ? */
733 rec.ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
734 if (!(rec.ExceptionCode = virtual_handle_fault(siginfo->si_addr, rec.ExceptionInformation[0])))
735 goto done;
736 break;
737 #endif
738 default:
739 FIXME("Unhandled SIGBUS/%x\n",siginfo->si_code);
740 break;
742 break;
743 case SIGILL:
744 switch (siginfo->si_code & 0xffff)
746 case ILL_ILLOPC: /* illegal opcode */
747 #ifdef ILL_ILLOPN
748 case ILL_ILLOPN: /* illegal operand */
749 #endif
750 #ifdef ILL_ILLADR
751 case ILL_ILLADR: /* illegal addressing mode */
752 #endif
753 #ifdef ILL_ILLTRP
754 case ILL_ILLTRP: /* illegal trap */
755 #endif
756 #ifdef ILL_COPROC
757 case ILL_COPROC: /* coprocessor error */
758 #endif
759 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
760 break;
761 case ILL_PRVOPC: /* privileged opcode */
762 #ifdef ILL_PRVREG
763 case ILL_PRVREG: /* privileged register */
764 #endif
765 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
766 break;
767 #ifdef ILL_BADSTK
768 case ILL_BADSTK: /* internal stack error */
769 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
770 break;
771 #endif
772 default:
773 FIXME("Unhandled SIGILL/%x\n", siginfo->si_code);
774 break;
776 break;
778 status = raise_exception( &rec, &context, TRUE );
779 if (status) raise_status( status, &rec );
780 done:
781 restore_context( &context, sigcontext );
784 /**********************************************************************
785 * trap_handler
787 * Handler for SIGTRAP.
789 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
791 EXCEPTION_RECORD rec;
792 CONTEXT context;
793 NTSTATUS status;
795 save_context( &context, sigcontext );
797 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
798 rec.ExceptionRecord = NULL;
799 rec.ExceptionAddress = (LPVOID)context.Iar;
800 rec.NumberParameters = 0;
802 /* FIXME: check if we might need to modify PC */
803 switch (siginfo->si_code & 0xffff)
805 #ifdef TRAP_BRKPT
806 case TRAP_BRKPT:
807 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
808 break;
809 #endif
810 #ifdef TRAP_TRACE
811 case TRAP_TRACE:
812 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
813 break;
814 #endif
815 default:
816 FIXME("Unhandled SIGTRAP/%x\n", siginfo->si_code);
817 break;
819 status = raise_exception( &rec, &context, TRUE );
820 if (status) raise_status( status, &rec );
821 restore_context( &context, sigcontext );
824 /**********************************************************************
825 * fpe_handler
827 * Handler for SIGFPE.
829 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
831 EXCEPTION_RECORD rec;
832 CONTEXT context;
833 NTSTATUS status;
835 save_fpu( &context, sigcontext );
836 save_context( &context, sigcontext );
838 switch (siginfo->si_code & 0xffff )
840 #ifdef FPE_FLTSUB
841 case FPE_FLTSUB:
842 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
843 break;
844 #endif
845 #ifdef FPE_INTDIV
846 case FPE_INTDIV:
847 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
848 break;
849 #endif
850 #ifdef FPE_INTOVF
851 case FPE_INTOVF:
852 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
853 break;
854 #endif
855 #ifdef FPE_FLTDIV
856 case FPE_FLTDIV:
857 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
858 break;
859 #endif
860 #ifdef FPE_FLTOVF
861 case FPE_FLTOVF:
862 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
863 break;
864 #endif
865 #ifdef FPE_FLTUND
866 case FPE_FLTUND:
867 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
868 break;
869 #endif
870 #ifdef FPE_FLTRES
871 case FPE_FLTRES:
872 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
873 break;
874 #endif
875 #ifdef FPE_FLTINV
876 case FPE_FLTINV:
877 #endif
878 default:
879 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
880 break;
882 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
883 rec.ExceptionRecord = NULL;
884 rec.ExceptionAddress = (LPVOID)context.Iar;
885 rec.NumberParameters = 0;
886 status = raise_exception( &rec, &context, TRUE );
887 if (status) raise_status( status, &rec );
889 restore_context( &context, sigcontext );
890 restore_fpu( &context, sigcontext );
893 /**********************************************************************
894 * int_handler
896 * Handler for SIGINT.
898 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
900 if (!dispatch_signal(SIGINT))
902 EXCEPTION_RECORD rec;
903 CONTEXT context;
904 NTSTATUS status;
906 save_context( &context, sigcontext );
907 rec.ExceptionCode = CONTROL_C_EXIT;
908 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
909 rec.ExceptionRecord = NULL;
910 rec.ExceptionAddress = (LPVOID)context.Iar;
911 rec.NumberParameters = 0;
912 status = raise_exception( &rec, &context, TRUE );
913 if (status) raise_status( status, &rec );
914 restore_context( &context, sigcontext );
919 /**********************************************************************
920 * abrt_handler
922 * Handler for SIGABRT.
924 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
926 EXCEPTION_RECORD rec;
927 CONTEXT context;
928 NTSTATUS status;
930 save_context( &context, sigcontext );
931 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
932 rec.ExceptionFlags = EH_NONCONTINUABLE;
933 rec.ExceptionRecord = NULL;
934 rec.ExceptionAddress = (LPVOID)context.Iar;
935 rec.NumberParameters = 0;
936 status = raise_exception( &rec, &context, TRUE );
937 if (status) raise_status( status, &rec );
938 restore_context( &context, sigcontext );
942 /**********************************************************************
943 * quit_handler
945 * Handler for SIGQUIT.
947 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
949 abort_thread(0);
953 /**********************************************************************
954 * usr1_handler
956 * Handler for SIGUSR1, used to signal a thread that it got suspended.
958 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
960 CONTEXT context;
962 save_context( &context, sigcontext );
963 wait_suspend( &context );
964 restore_context( &context, sigcontext );
968 /***********************************************************************
969 * __wine_set_signal_handler (NTDLL.@)
971 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
973 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
974 if (handlers[sig] != NULL) return -2;
975 handlers[sig] = wsh;
976 return 0;
980 /**********************************************************************
981 * signal_alloc_thread
983 NTSTATUS signal_alloc_thread( TEB **teb )
985 static size_t sigstack_zero_bits;
986 SIZE_T size;
987 NTSTATUS status;
989 if (!sigstack_zero_bits)
991 size_t min_size = getpagesize(); /* this is just for the TEB, we don't use a signal stack yet */
992 /* find the first power of two not smaller than min_size */
993 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
994 assert( sizeof(TEB) <= min_size );
997 size = 1 << sigstack_zero_bits;
998 *teb = NULL;
999 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
1000 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
1002 (*teb)->Tib.Self = &(*teb)->Tib;
1003 (*teb)->Tib.ExceptionList = (void *)~0UL;
1005 return status;
1009 /**********************************************************************
1010 * signal_free_thread
1012 void signal_free_thread( TEB *teb )
1014 SIZE_T size;
1016 if (teb->DeallocationStack)
1018 size = 0;
1019 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
1021 size = 0;
1022 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
1026 /**********************************************************************
1027 * signal_init_thread
1029 void signal_init_thread( TEB *teb )
1031 static int init_done;
1033 if (!init_done)
1035 pthread_key_create( &teb_key, NULL );
1036 init_done = 1;
1038 pthread_setspecific( teb_key, teb );
1042 /**********************************************************************
1043 * signal_init_process
1045 void signal_init_process(void)
1047 struct sigaction sig_act;
1049 sig_act.sa_mask = server_block_set;
1050 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
1052 sig_act.sa_sigaction = int_handler;
1053 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
1054 sig_act.sa_sigaction = fpe_handler;
1055 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
1056 sig_act.sa_sigaction = abrt_handler;
1057 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
1058 sig_act.sa_sigaction = quit_handler;
1059 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
1060 sig_act.sa_sigaction = usr1_handler;
1061 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
1063 sig_act.sa_sigaction = segv_handler;
1064 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
1065 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
1066 #ifdef SIGBUS
1067 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
1068 #endif
1070 #ifdef SIGTRAP
1071 sig_act.sa_sigaction = trap_handler;
1072 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
1073 #endif
1074 return;
1076 error:
1077 perror("sigaction");
1078 exit(1);
1082 /**********************************************************************
1083 * __wine_enter_vm86 (NTDLL.@)
1085 void __wine_enter_vm86( CONTEXT *context )
1087 MESSAGE("vm86 mode not supported on this platform\n");
1090 /***********************************************************************
1091 * RtlUnwind (NTDLL.@)
1093 void WINAPI RtlUnwind( PVOID pEndFrame, PVOID targetIp, PEXCEPTION_RECORD pRecord, PVOID retval )
1095 FIXME( "Not implemented on PowerPC\n" );
1098 /*******************************************************************
1099 * NtRaiseException (NTDLL.@)
1101 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
1103 NTSTATUS status = raise_exception( rec, context, first_chance );
1104 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
1105 return status;
1108 /***********************************************************************
1109 * RtlRaiseException (NTDLL.@)
1111 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
1113 CONTEXT context;
1114 NTSTATUS status;
1116 RtlCaptureContext( &context );
1117 rec->ExceptionAddress = (void *)context.Iar;
1118 status = raise_exception( rec, &context, TRUE );
1119 if (status) raise_status( status, rec );
1122 /*************************************************************************
1123 * RtlCaptureStackBackTrace (NTDLL.@)
1125 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
1127 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
1128 return 0;
1131 /***********************************************************************
1132 * call_thread_entry_point
1134 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg )
1136 __TRY
1138 exit_thread( entry( arg ));
1140 __EXCEPT(unhandled_exception_filter)
1142 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
1144 __ENDTRY
1145 abort(); /* should not be reached */
1148 /***********************************************************************
1149 * RtlExitUserThread (NTDLL.@)
1151 void WINAPI RtlExitUserThread( ULONG status )
1153 exit_thread( status );
1156 /***********************************************************************
1157 * abort_thread
1159 void abort_thread( int status )
1161 terminate_thread( status );
1164 /**********************************************************************
1165 * DbgBreakPoint (NTDLL.@)
1167 void WINAPI DbgBreakPoint(void)
1169 kill(getpid(), SIGTRAP);
1172 /**********************************************************************
1173 * DbgUserBreakPoint (NTDLL.@)
1175 void WINAPI DbgUserBreakPoint(void)
1177 kill(getpid(), SIGTRAP);
1180 /**********************************************************************
1181 * NtCurrentTeb (NTDLL.@)
1183 TEB * WINAPI NtCurrentTeb(void)
1185 return pthread_getspecific( teb_key );
1188 #endif /* __powerpc__ */