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
24 #include "wine/port.h"
31 #include <sys/types.h>
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
41 # ifdef HAVE_SYS_SYSCALL_H
42 # include <sys/syscall.h>
45 #ifdef HAVE_SYS_SIGNAL_H
46 # include <sys/signal.h>
48 #ifdef HAVE_SYS_UCONTEXT_H
49 # include <sys/ucontext.h>
53 #define WIN32_NO_STATUS
56 #include "wine/library.h"
57 #include "wine/exception.h"
58 #include "ntdll_misc.h"
59 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
62 WINE_DECLARE_DEBUG_CHANNEL(relay
);
64 static pthread_key_t teb_key
;
66 /***********************************************************************
67 * signal context platform-specific definitions
71 /* All Registers access - only for local access */
72 # define REG_sig(reg_name, context) ((context)->uc_mcontext.regs->reg_name)
75 /* Gpr Registers access */
76 # define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
78 # define IAR_sig(context) REG_sig(nip, context) /* Program counter */
79 # define MSR_sig(context) REG_sig(msr, context) /* Machine State Register (Supervisor) */
80 # define CTR_sig(context) REG_sig(ctr, context) /* Count register */
82 # define XER_sig(context) REG_sig(xer, context) /* User's integer exception register */
83 # define LR_sig(context) REG_sig(link, context) /* Link register */
84 # define CR_sig(context) REG_sig(ccr, context) /* Condition register */
86 /* Float Registers access */
87 # define FLOAT_sig(reg_num, context) (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
89 # define FPSCR_sig(context) (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
91 /* Exception Registers access */
92 # define DAR_sig(context) REG_sig(dar, context)
93 # define DSISR_sig(context) REG_sig(dsisr, context)
94 # define TRAP_sig(context) REG_sig(trap, context)
100 /* All Registers access - only for local access */
101 # define REG_sig(reg_name, context) ((context)->uc_mcontext->ss.reg_name)
102 # define FLOATREG_sig(reg_name, context) ((context)->uc_mcontext->fs.reg_name)
103 # define EXCEPREG_sig(reg_name, context) ((context)->uc_mcontext->es.reg_name)
104 # define VECREG_sig(reg_name, context) ((context)->uc_mcontext->vs.reg_name)
106 /* Gpr Registers access */
107 # define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
109 # define IAR_sig(context) REG_sig(srr0, context) /* Program counter */
110 # define MSR_sig(context) REG_sig(srr1, context) /* Machine State Register (Supervisor) */
111 # define CTR_sig(context) REG_sig(ctr, context)
113 # define XER_sig(context) REG_sig(xer, context) /* Link register */
114 # define LR_sig(context) REG_sig(lr, context) /* User's integer exception register */
115 # define CR_sig(context) REG_sig(cr, context) /* Condition register */
117 /* Float Registers access */
118 # define FLOAT_sig(reg_num, context) FLOATREG_sig(fpregs[reg_num], context)
120 # define FPSCR_sig(context) FLOATREG_sig(fpscr, context)
122 /* Exception Registers access */
123 # define DAR_sig(context) EXCEPREG_sig(dar, context) /* Fault registers for coredump */
124 # define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
125 # define TRAP_sig(context) EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
127 /* Signal defs : Those are undefined on darwin
144 #endif /* __APPLE__ */
148 typedef int (*wine_signal_handler
)(unsigned int sig
);
150 static wine_signal_handler handlers
[256];
152 /***********************************************************************
155 static inline int dispatch_signal(unsigned int sig
)
157 if (handlers
[sig
] == NULL
) return 0;
158 return handlers
[sig
](sig
);
161 /***********************************************************************
164 * Set the register values from a sigcontext.
166 static void save_context( CONTEXT
*context
, const ucontext_t
*sigcontext
)
169 #define C(x) context->Gpr##x = GPR_sig(x,sigcontext)
170 /* Save Gpr registers */
171 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
172 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
173 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
177 context
->Iar
= IAR_sig(sigcontext
); /* Program Counter */
178 context
->Msr
= MSR_sig(sigcontext
); /* Machine State Register (Supervisor) */
179 context
->Ctr
= CTR_sig(sigcontext
);
181 context
->Xer
= XER_sig(sigcontext
);
182 context
->Lr
= LR_sig(sigcontext
);
183 context
->Cr
= CR_sig(sigcontext
);
185 /* Saving Exception regs */
186 context
->Dar
= DAR_sig(sigcontext
);
187 context
->Dsisr
= DSISR_sig(sigcontext
);
188 context
->Trap
= TRAP_sig(sigcontext
);
192 /***********************************************************************
195 * Build a sigcontext from the register values.
197 static void restore_context( const CONTEXT
*context
, ucontext_t
*sigcontext
)
200 #define C(x) GPR_sig(x,sigcontext) = context->Gpr##x
201 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
202 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
203 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
207 IAR_sig(sigcontext
) = context
->Iar
; /* Program Counter */
208 MSR_sig(sigcontext
) = context
->Msr
; /* Machine State Register (Supervisor) */
209 CTR_sig(sigcontext
) = context
->Ctr
;
211 XER_sig(sigcontext
) = context
->Xer
;
212 LR_sig(sigcontext
) = context
->Lr
;
213 CR_sig(sigcontext
) = context
->Cr
;
215 /* Setting Exception regs */
216 DAR_sig(sigcontext
) = context
->Dar
;
217 DSISR_sig(sigcontext
) = context
->Dsisr
;
218 TRAP_sig(sigcontext
) = context
->Trap
;
222 /***********************************************************************
225 * Set the FPU context from a sigcontext.
227 static inline void save_fpu( CONTEXT
*context
, const ucontext_t
*sigcontext
)
229 #define C(x) context->Fpr##x = FLOAT_sig(x,sigcontext)
230 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
231 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
232 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
235 context
->Fpscr
= FPSCR_sig(sigcontext
);
239 /***********************************************************************
242 * Restore the FPU context to a sigcontext.
244 static inline void restore_fpu( CONTEXT
*context
, const ucontext_t
*sigcontext
)
246 #define C(x) FLOAT_sig(x,sigcontext) = context->Fpr##x
247 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
248 C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19); C(20);
249 C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28); C(29); C(30);
252 FPSCR_sig(sigcontext
) = context
->Fpscr
;
256 /***********************************************************************
257 * RtlCaptureContext (NTDLL.@)
259 void WINAPI
RtlCaptureContext( CONTEXT
*context
)
261 FIXME("not implemented\n");
262 memset( context
, 0, sizeof(*context
) );
266 /***********************************************************************
269 * Set the new CPU context.
271 static void set_cpu_context( const CONTEXT
*context
)
273 FIXME("not implemented\n");
277 /***********************************************************************
278 * get_server_context_flags
280 * Convert CPU-specific flags to generic server flags
282 static unsigned int get_server_context_flags( DWORD flags
)
284 unsigned int ret
= 0;
286 if (flags
& CONTEXT_CONTROL
) ret
|= SERVER_CTX_CONTROL
;
287 if (flags
& CONTEXT_INTEGER
) ret
|= SERVER_CTX_INTEGER
;
288 if (flags
& CONTEXT_FLOATING_POINT
) ret
|= SERVER_CTX_FLOATING_POINT
;
289 if (flags
& CONTEXT_DEBUG_REGISTERS
) ret
|= SERVER_CTX_DEBUG_REGISTERS
;
294 /***********************************************************************
297 * Copy a register context according to the flags.
299 static void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
301 if (flags
& CONTEXT_CONTROL
)
308 to
->Dsisr
= from
->Dsisr
;
309 to
->Trap
= from
->Trap
;
311 if (flags
& CONTEXT_INTEGER
)
313 to
->Gpr0
= from
->Gpr0
;
314 to
->Gpr1
= from
->Gpr1
;
315 to
->Gpr2
= from
->Gpr2
;
316 to
->Gpr3
= from
->Gpr3
;
317 to
->Gpr4
= from
->Gpr4
;
318 to
->Gpr5
= from
->Gpr5
;
319 to
->Gpr6
= from
->Gpr6
;
320 to
->Gpr7
= from
->Gpr7
;
321 to
->Gpr8
= from
->Gpr8
;
322 to
->Gpr9
= from
->Gpr9
;
323 to
->Gpr10
= from
->Gpr10
;
324 to
->Gpr11
= from
->Gpr11
;
325 to
->Gpr12
= from
->Gpr12
;
326 to
->Gpr13
= from
->Gpr13
;
327 to
->Gpr14
= from
->Gpr14
;
328 to
->Gpr15
= from
->Gpr15
;
329 to
->Gpr16
= from
->Gpr16
;
330 to
->Gpr17
= from
->Gpr17
;
331 to
->Gpr18
= from
->Gpr18
;
332 to
->Gpr19
= from
->Gpr19
;
333 to
->Gpr20
= from
->Gpr20
;
334 to
->Gpr21
= from
->Gpr21
;
335 to
->Gpr22
= from
->Gpr22
;
336 to
->Gpr23
= from
->Gpr23
;
337 to
->Gpr24
= from
->Gpr24
;
338 to
->Gpr25
= from
->Gpr25
;
339 to
->Gpr26
= from
->Gpr26
;
340 to
->Gpr27
= from
->Gpr27
;
341 to
->Gpr28
= from
->Gpr28
;
342 to
->Gpr29
= from
->Gpr29
;
343 to
->Gpr30
= from
->Gpr30
;
344 to
->Gpr31
= from
->Gpr31
;
348 if (flags
& CONTEXT_FLOATING_POINT
)
350 to
->Fpr0
= from
->Fpr0
;
351 to
->Fpr1
= from
->Fpr1
;
352 to
->Fpr2
= from
->Fpr2
;
353 to
->Fpr3
= from
->Fpr3
;
354 to
->Fpr4
= from
->Fpr4
;
355 to
->Fpr5
= from
->Fpr5
;
356 to
->Fpr6
= from
->Fpr6
;
357 to
->Fpr7
= from
->Fpr7
;
358 to
->Fpr8
= from
->Fpr8
;
359 to
->Fpr9
= from
->Fpr9
;
360 to
->Fpr10
= from
->Fpr10
;
361 to
->Fpr11
= from
->Fpr11
;
362 to
->Fpr12
= from
->Fpr12
;
363 to
->Fpr13
= from
->Fpr13
;
364 to
->Fpr14
= from
->Fpr14
;
365 to
->Fpr15
= from
->Fpr15
;
366 to
->Fpr16
= from
->Fpr16
;
367 to
->Fpr17
= from
->Fpr17
;
368 to
->Fpr18
= from
->Fpr18
;
369 to
->Fpr19
= from
->Fpr19
;
370 to
->Fpr20
= from
->Fpr20
;
371 to
->Fpr21
= from
->Fpr21
;
372 to
->Fpr22
= from
->Fpr22
;
373 to
->Fpr23
= from
->Fpr23
;
374 to
->Fpr24
= from
->Fpr24
;
375 to
->Fpr25
= from
->Fpr25
;
376 to
->Fpr26
= from
->Fpr26
;
377 to
->Fpr27
= from
->Fpr27
;
378 to
->Fpr28
= from
->Fpr28
;
379 to
->Fpr29
= from
->Fpr29
;
380 to
->Fpr30
= from
->Fpr30
;
381 to
->Fpr31
= from
->Fpr31
;
382 to
->Fpscr
= from
->Fpscr
;
387 /***********************************************************************
390 * Convert a register context to the server format.
392 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
394 DWORD flags
= from
->ContextFlags
; /* no CPU id? */
396 memset( to
, 0, sizeof(*to
) );
397 to
->cpu
= CPU_POWERPC
;
399 if (flags
& CONTEXT_CONTROL
)
401 to
->flags
|= SERVER_CTX_CONTROL
;
402 to
->ctl
.powerpc_regs
.iar
= from
->Iar
;
403 to
->ctl
.powerpc_regs
.msr
= from
->Msr
;
404 to
->ctl
.powerpc_regs
.ctr
= from
->Ctr
;
405 to
->ctl
.powerpc_regs
.lr
= from
->Lr
;
406 to
->ctl
.powerpc_regs
.dar
= from
->Dar
;
407 to
->ctl
.powerpc_regs
.dsisr
= from
->Dsisr
;
408 to
->ctl
.powerpc_regs
.trap
= from
->Trap
;
410 if (flags
& CONTEXT_INTEGER
)
412 to
->flags
|= SERVER_CTX_INTEGER
;
413 to
->integer
.powerpc_regs
.gpr
[0] = from
->Gpr0
;
414 to
->integer
.powerpc_regs
.gpr
[1] = from
->Gpr1
;
415 to
->integer
.powerpc_regs
.gpr
[2] = from
->Gpr2
;
416 to
->integer
.powerpc_regs
.gpr
[3] = from
->Gpr3
;
417 to
->integer
.powerpc_regs
.gpr
[4] = from
->Gpr4
;
418 to
->integer
.powerpc_regs
.gpr
[5] = from
->Gpr5
;
419 to
->integer
.powerpc_regs
.gpr
[6] = from
->Gpr6
;
420 to
->integer
.powerpc_regs
.gpr
[7] = from
->Gpr7
;
421 to
->integer
.powerpc_regs
.gpr
[8] = from
->Gpr8
;
422 to
->integer
.powerpc_regs
.gpr
[9] = from
->Gpr9
;
423 to
->integer
.powerpc_regs
.gpr
[10] = from
->Gpr10
;
424 to
->integer
.powerpc_regs
.gpr
[11] = from
->Gpr11
;
425 to
->integer
.powerpc_regs
.gpr
[12] = from
->Gpr12
;
426 to
->integer
.powerpc_regs
.gpr
[13] = from
->Gpr13
;
427 to
->integer
.powerpc_regs
.gpr
[14] = from
->Gpr14
;
428 to
->integer
.powerpc_regs
.gpr
[15] = from
->Gpr15
;
429 to
->integer
.powerpc_regs
.gpr
[16] = from
->Gpr16
;
430 to
->integer
.powerpc_regs
.gpr
[17] = from
->Gpr17
;
431 to
->integer
.powerpc_regs
.gpr
[18] = from
->Gpr18
;
432 to
->integer
.powerpc_regs
.gpr
[19] = from
->Gpr19
;
433 to
->integer
.powerpc_regs
.gpr
[20] = from
->Gpr20
;
434 to
->integer
.powerpc_regs
.gpr
[21] = from
->Gpr21
;
435 to
->integer
.powerpc_regs
.gpr
[22] = from
->Gpr22
;
436 to
->integer
.powerpc_regs
.gpr
[23] = from
->Gpr23
;
437 to
->integer
.powerpc_regs
.gpr
[24] = from
->Gpr24
;
438 to
->integer
.powerpc_regs
.gpr
[25] = from
->Gpr25
;
439 to
->integer
.powerpc_regs
.gpr
[26] = from
->Gpr26
;
440 to
->integer
.powerpc_regs
.gpr
[27] = from
->Gpr27
;
441 to
->integer
.powerpc_regs
.gpr
[28] = from
->Gpr28
;
442 to
->integer
.powerpc_regs
.gpr
[29] = from
->Gpr29
;
443 to
->integer
.powerpc_regs
.gpr
[30] = from
->Gpr30
;
444 to
->integer
.powerpc_regs
.gpr
[31] = from
->Gpr31
;
445 to
->integer
.powerpc_regs
.xer
= from
->Xer
;
446 to
->integer
.powerpc_regs
.cr
= from
->Cr
;
448 if (flags
& CONTEXT_FLOATING_POINT
)
450 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
451 to
->fp
.powerpc_regs
.fpr
[0] = from
->Fpr0
;
452 to
->fp
.powerpc_regs
.fpr
[1] = from
->Fpr1
;
453 to
->fp
.powerpc_regs
.fpr
[2] = from
->Fpr2
;
454 to
->fp
.powerpc_regs
.fpr
[3] = from
->Fpr3
;
455 to
->fp
.powerpc_regs
.fpr
[4] = from
->Fpr4
;
456 to
->fp
.powerpc_regs
.fpr
[5] = from
->Fpr5
;
457 to
->fp
.powerpc_regs
.fpr
[6] = from
->Fpr6
;
458 to
->fp
.powerpc_regs
.fpr
[7] = from
->Fpr7
;
459 to
->fp
.powerpc_regs
.fpr
[8] = from
->Fpr8
;
460 to
->fp
.powerpc_regs
.fpr
[9] = from
->Fpr9
;
461 to
->fp
.powerpc_regs
.fpr
[10] = from
->Fpr10
;
462 to
->fp
.powerpc_regs
.fpr
[11] = from
->Fpr11
;
463 to
->fp
.powerpc_regs
.fpr
[12] = from
->Fpr12
;
464 to
->fp
.powerpc_regs
.fpr
[13] = from
->Fpr13
;
465 to
->fp
.powerpc_regs
.fpr
[14] = from
->Fpr14
;
466 to
->fp
.powerpc_regs
.fpr
[15] = from
->Fpr15
;
467 to
->fp
.powerpc_regs
.fpr
[16] = from
->Fpr16
;
468 to
->fp
.powerpc_regs
.fpr
[17] = from
->Fpr17
;
469 to
->fp
.powerpc_regs
.fpr
[18] = from
->Fpr18
;
470 to
->fp
.powerpc_regs
.fpr
[19] = from
->Fpr19
;
471 to
->fp
.powerpc_regs
.fpr
[20] = from
->Fpr20
;
472 to
->fp
.powerpc_regs
.fpr
[21] = from
->Fpr21
;
473 to
->fp
.powerpc_regs
.fpr
[22] = from
->Fpr22
;
474 to
->fp
.powerpc_regs
.fpr
[23] = from
->Fpr23
;
475 to
->fp
.powerpc_regs
.fpr
[24] = from
->Fpr24
;
476 to
->fp
.powerpc_regs
.fpr
[25] = from
->Fpr25
;
477 to
->fp
.powerpc_regs
.fpr
[26] = from
->Fpr26
;
478 to
->fp
.powerpc_regs
.fpr
[27] = from
->Fpr27
;
479 to
->fp
.powerpc_regs
.fpr
[28] = from
->Fpr28
;
480 to
->fp
.powerpc_regs
.fpr
[29] = from
->Fpr29
;
481 to
->fp
.powerpc_regs
.fpr
[30] = from
->Fpr30
;
482 to
->fp
.powerpc_regs
.fpr
[31] = from
->Fpr31
;
483 to
->fp
.powerpc_regs
.fpscr
= from
->Fpscr
;
485 return STATUS_SUCCESS
;
489 /***********************************************************************
490 * context_from_server
492 * Convert a register context from the server format.
494 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
496 if (from
->cpu
!= CPU_POWERPC
) return STATUS_INVALID_PARAMETER
;
498 to
->ContextFlags
= 0; /* no CPU id? */
499 if (from
->flags
& SERVER_CTX_CONTROL
)
501 to
->ContextFlags
|= CONTEXT_CONTROL
;
502 to
->Msr
= from
->ctl
.powerpc_regs
.msr
;
503 to
->Ctr
= from
->ctl
.powerpc_regs
.ctr
;
504 to
->Iar
= from
->ctl
.powerpc_regs
.iar
;
505 to
->Lr
= from
->ctl
.powerpc_regs
.lr
;
506 to
->Dar
= from
->ctl
.powerpc_regs
.dar
;
507 to
->Dsisr
= from
->ctl
.powerpc_regs
.dsisr
;
508 to
->Trap
= from
->ctl
.powerpc_regs
.trap
;
510 if (from
->flags
& SERVER_CTX_INTEGER
)
512 to
->ContextFlags
|= CONTEXT_INTEGER
;
513 to
->Gpr0
= from
->integer
.powerpc_regs
.gpr
[0];
514 to
->Gpr1
= from
->integer
.powerpc_regs
.gpr
[1];
515 to
->Gpr2
= from
->integer
.powerpc_regs
.gpr
[2];
516 to
->Gpr3
= from
->integer
.powerpc_regs
.gpr
[3];
517 to
->Gpr4
= from
->integer
.powerpc_regs
.gpr
[4];
518 to
->Gpr5
= from
->integer
.powerpc_regs
.gpr
[5];
519 to
->Gpr6
= from
->integer
.powerpc_regs
.gpr
[6];
520 to
->Gpr7
= from
->integer
.powerpc_regs
.gpr
[7];
521 to
->Gpr8
= from
->integer
.powerpc_regs
.gpr
[8];
522 to
->Gpr9
= from
->integer
.powerpc_regs
.gpr
[9];
523 to
->Gpr10
= from
->integer
.powerpc_regs
.gpr
[10];
524 to
->Gpr11
= from
->integer
.powerpc_regs
.gpr
[11];
525 to
->Gpr12
= from
->integer
.powerpc_regs
.gpr
[12];
526 to
->Gpr13
= from
->integer
.powerpc_regs
.gpr
[13];
527 to
->Gpr14
= from
->integer
.powerpc_regs
.gpr
[14];
528 to
->Gpr15
= from
->integer
.powerpc_regs
.gpr
[15];
529 to
->Gpr16
= from
->integer
.powerpc_regs
.gpr
[16];
530 to
->Gpr17
= from
->integer
.powerpc_regs
.gpr
[17];
531 to
->Gpr18
= from
->integer
.powerpc_regs
.gpr
[18];
532 to
->Gpr19
= from
->integer
.powerpc_regs
.gpr
[19];
533 to
->Gpr20
= from
->integer
.powerpc_regs
.gpr
[20];
534 to
->Gpr21
= from
->integer
.powerpc_regs
.gpr
[21];
535 to
->Gpr22
= from
->integer
.powerpc_regs
.gpr
[22];
536 to
->Gpr23
= from
->integer
.powerpc_regs
.gpr
[23];
537 to
->Gpr24
= from
->integer
.powerpc_regs
.gpr
[24];
538 to
->Gpr25
= from
->integer
.powerpc_regs
.gpr
[25];
539 to
->Gpr26
= from
->integer
.powerpc_regs
.gpr
[26];
540 to
->Gpr27
= from
->integer
.powerpc_regs
.gpr
[27];
541 to
->Gpr28
= from
->integer
.powerpc_regs
.gpr
[28];
542 to
->Gpr29
= from
->integer
.powerpc_regs
.gpr
[29];
543 to
->Gpr30
= from
->integer
.powerpc_regs
.gpr
[30];
544 to
->Gpr31
= from
->integer
.powerpc_regs
.gpr
[31];
545 to
->Xer
= from
->integer
.powerpc_regs
.xer
;
546 to
->Cr
= from
->integer
.powerpc_regs
.cr
;
548 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
550 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
551 to
->Fpr0
= from
->fp
.powerpc_regs
.fpr
[0];
552 to
->Fpr1
= from
->fp
.powerpc_regs
.fpr
[1];
553 to
->Fpr2
= from
->fp
.powerpc_regs
.fpr
[2];
554 to
->Fpr3
= from
->fp
.powerpc_regs
.fpr
[3];
555 to
->Fpr4
= from
->fp
.powerpc_regs
.fpr
[4];
556 to
->Fpr5
= from
->fp
.powerpc_regs
.fpr
[5];
557 to
->Fpr6
= from
->fp
.powerpc_regs
.fpr
[6];
558 to
->Fpr7
= from
->fp
.powerpc_regs
.fpr
[7];
559 to
->Fpr8
= from
->fp
.powerpc_regs
.fpr
[8];
560 to
->Fpr9
= from
->fp
.powerpc_regs
.fpr
[9];
561 to
->Fpr10
= from
->fp
.powerpc_regs
.fpr
[10];
562 to
->Fpr11
= from
->fp
.powerpc_regs
.fpr
[11];
563 to
->Fpr12
= from
->fp
.powerpc_regs
.fpr
[12];
564 to
->Fpr13
= from
->fp
.powerpc_regs
.fpr
[13];
565 to
->Fpr14
= from
->fp
.powerpc_regs
.fpr
[14];
566 to
->Fpr15
= from
->fp
.powerpc_regs
.fpr
[15];
567 to
->Fpr16
= from
->fp
.powerpc_regs
.fpr
[16];
568 to
->Fpr17
= from
->fp
.powerpc_regs
.fpr
[17];
569 to
->Fpr18
= from
->fp
.powerpc_regs
.fpr
[18];
570 to
->Fpr19
= from
->fp
.powerpc_regs
.fpr
[19];
571 to
->Fpr20
= from
->fp
.powerpc_regs
.fpr
[20];
572 to
->Fpr21
= from
->fp
.powerpc_regs
.fpr
[21];
573 to
->Fpr22
= from
->fp
.powerpc_regs
.fpr
[22];
574 to
->Fpr23
= from
->fp
.powerpc_regs
.fpr
[23];
575 to
->Fpr24
= from
->fp
.powerpc_regs
.fpr
[24];
576 to
->Fpr25
= from
->fp
.powerpc_regs
.fpr
[25];
577 to
->Fpr26
= from
->fp
.powerpc_regs
.fpr
[26];
578 to
->Fpr27
= from
->fp
.powerpc_regs
.fpr
[27];
579 to
->Fpr28
= from
->fp
.powerpc_regs
.fpr
[28];
580 to
->Fpr29
= from
->fp
.powerpc_regs
.fpr
[29];
581 to
->Fpr30
= from
->fp
.powerpc_regs
.fpr
[30];
582 to
->Fpr31
= from
->fp
.powerpc_regs
.fpr
[31];
583 to
->Fpscr
= from
->fp
.powerpc_regs
.fpscr
;
585 return STATUS_SUCCESS
;
589 /***********************************************************************
590 * NtSetContextThread (NTDLL.@)
591 * ZwSetContextThread (NTDLL.@)
593 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
597 context_t server_context
;
599 context_to_server( &server_context
, context
);
600 ret
= set_thread_context( handle
, &server_context
, &self
);
601 if (self
&& ret
== STATUS_SUCCESS
) set_cpu_context( context
);
606 /***********************************************************************
607 * NtGetContextThread (NTDLL.@)
608 * ZwGetContextThread (NTDLL.@)
610 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
613 DWORD needed_flags
= context
->ContextFlags
;
614 BOOL self
= (handle
== GetCurrentThread());
618 context_t server_context
;
619 unsigned int server_flags
= get_server_context_flags( context
->ContextFlags
);
621 if ((ret
= get_thread_context( handle
, &server_context
, server_flags
, &self
))) return ret
;
622 if ((ret
= context_from_server( context
, &server_context
))) return ret
;
623 needed_flags
&= ~context
->ContextFlags
;
626 if (self
&& needed_flags
)
629 RtlCaptureContext( &ctx
);
630 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
631 context
->ContextFlags
|= ctx
.ContextFlags
& needed_flags
;
633 return STATUS_SUCCESS
;
637 /**********************************************************************
638 * call_stack_handlers
640 * Call the stack handlers chain.
642 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
644 EXCEPTION_POINTERS ptrs
;
646 FIXME( "not implemented on PowerPC\n" );
648 /* hack: call unhandled exception filter directly */
649 ptrs
.ExceptionRecord
= rec
;
650 ptrs
.ContextRecord
= context
;
651 call_unhandled_exception_filter( &ptrs
);
652 return STATUS_UNHANDLED_EXCEPTION
;
656 /*******************************************************************
659 * Implementation of NtRaiseException.
661 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
669 TRACE( "code=%x flags=%x addr=%p ip=%x tid=%04x\n",
670 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
671 context
->Iar
, GetCurrentThreadId() );
672 for (c
= 0; c
< rec
->NumberParameters
; c
++)
673 TRACE( " info[%d]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
674 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
676 if (rec
->ExceptionInformation
[1] >> 16)
677 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
678 rec
->ExceptionAddress
,
679 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
681 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
682 rec
->ExceptionAddress
,
683 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
687 /* FIXME: dump context */
690 status
= send_debug_event( rec
, TRUE
, context
);
691 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
692 return STATUS_SUCCESS
;
694 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
695 return STATUS_SUCCESS
;
697 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
701 /* last chance exception */
703 status
= send_debug_event( rec
, FALSE
, context
);
704 if (status
!= DBG_CONTINUE
)
706 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
707 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
708 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
709 ERR("Process attempted to continue execution after noncontinuable exception.\n");
711 ERR("Unhandled exception code %x flags %x addr %p\n",
712 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
713 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
715 return STATUS_SUCCESS
;
719 /**********************************************************************
722 * Handler for SIGSEGV and related errors.
724 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
726 EXCEPTION_RECORD rec
;
730 save_context( &context
, sigcontext
);
732 rec
.ExceptionRecord
= NULL
;
733 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
734 rec
.ExceptionAddress
= (LPVOID
)context
.Iar
;
735 rec
.NumberParameters
= 0;
740 switch (siginfo
->si_code
& 0xffff)
744 rec
.NumberParameters
= 2;
745 rec
.ExceptionInformation
[0] = 0; /* FIXME ? */
746 rec
.ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
747 if (!(rec
.ExceptionCode
= virtual_handle_fault(siginfo
->si_addr
, rec
.ExceptionInformation
[0], FALSE
)))
751 FIXME("Unhandled SIGSEGV/%x\n",siginfo
->si_code
);
756 switch (siginfo
->si_code
& 0xffff)
759 rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
766 /* FIXME: correct for all cases ? */
767 rec
.NumberParameters
= 2;
768 rec
.ExceptionInformation
[0] = 0; /* FIXME ? */
769 rec
.ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
770 if (!(rec
.ExceptionCode
= virtual_handle_fault(siginfo
->si_addr
, rec
.ExceptionInformation
[0], FALSE
)))
775 FIXME("Unhandled SIGBUS/%x\n",siginfo
->si_code
);
780 switch (siginfo
->si_code
& 0xffff)
782 case ILL_ILLOPC
: /* illegal opcode */
784 case ILL_ILLOPN
: /* illegal operand */
787 case ILL_ILLADR
: /* illegal addressing mode */
790 case ILL_ILLTRP
: /* illegal trap */
793 case ILL_COPROC
: /* coprocessor error */
795 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
797 case ILL_PRVOPC
: /* privileged opcode */
799 case ILL_PRVREG
: /* privileged register */
801 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
804 case ILL_BADSTK
: /* internal stack error */
805 rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
809 FIXME("Unhandled SIGILL/%x\n", siginfo
->si_code
);
814 status
= raise_exception( &rec
, &context
, TRUE
);
815 if (status
) raise_status( status
, &rec
);
817 restore_context( &context
, sigcontext
);
820 /**********************************************************************
823 * Handler for SIGTRAP.
825 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
827 EXCEPTION_RECORD rec
;
831 save_context( &context
, sigcontext
);
833 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
834 rec
.ExceptionRecord
= NULL
;
835 rec
.ExceptionAddress
= (LPVOID
)context
.Iar
;
836 rec
.NumberParameters
= 0;
838 /* FIXME: check if we might need to modify PC */
839 switch (siginfo
->si_code
& 0xffff)
843 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
848 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
852 FIXME("Unhandled SIGTRAP/%x\n", siginfo
->si_code
);
855 status
= raise_exception( &rec
, &context
, TRUE
);
856 if (status
) raise_status( status
, &rec
);
857 restore_context( &context
, sigcontext
);
860 /**********************************************************************
863 * Handler for SIGFPE.
865 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
867 EXCEPTION_RECORD rec
;
871 save_fpu( &context
, sigcontext
);
872 save_context( &context
, sigcontext
);
874 switch (siginfo
->si_code
& 0xffff )
878 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
883 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
888 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
893 rec
.ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
898 rec
.ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
903 rec
.ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
908 rec
.ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
915 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
918 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
919 rec
.ExceptionRecord
= NULL
;
920 rec
.ExceptionAddress
= (LPVOID
)context
.Iar
;
921 rec
.NumberParameters
= 0;
922 status
= raise_exception( &rec
, &context
, TRUE
);
923 if (status
) raise_status( status
, &rec
);
925 restore_context( &context
, sigcontext
);
926 restore_fpu( &context
, sigcontext
);
929 /**********************************************************************
932 * Handler for SIGINT.
934 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
936 if (!dispatch_signal(SIGINT
))
938 EXCEPTION_RECORD rec
;
942 save_context( &context
, sigcontext
);
943 rec
.ExceptionCode
= CONTROL_C_EXIT
;
944 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
945 rec
.ExceptionRecord
= NULL
;
946 rec
.ExceptionAddress
= (LPVOID
)context
.Iar
;
947 rec
.NumberParameters
= 0;
948 status
= raise_exception( &rec
, &context
, TRUE
);
949 if (status
) raise_status( status
, &rec
);
950 restore_context( &context
, sigcontext
);
955 /**********************************************************************
958 * Handler for SIGABRT.
960 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
962 EXCEPTION_RECORD rec
;
966 save_context( &context
, sigcontext
);
967 rec
.ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
968 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
969 rec
.ExceptionRecord
= NULL
;
970 rec
.ExceptionAddress
= (LPVOID
)context
.Iar
;
971 rec
.NumberParameters
= 0;
972 status
= raise_exception( &rec
, &context
, TRUE
);
973 if (status
) raise_status( status
, &rec
);
974 restore_context( &context
, sigcontext
);
978 /**********************************************************************
981 * Handler for SIGQUIT.
983 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
989 /**********************************************************************
992 * Handler for SIGUSR1, used to signal a thread that it got suspended.
994 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
998 save_context( &context
, sigcontext
);
999 wait_suspend( &context
);
1000 restore_context( &context
, sigcontext
);
1004 /***********************************************************************
1005 * __wine_set_signal_handler (NTDLL.@)
1007 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
1009 if (sig
>= ARRAY_SIZE(handlers
)) return -1;
1010 if (handlers
[sig
] != NULL
) return -2;
1011 handlers
[sig
] = wsh
;
1016 /**********************************************************************
1017 * signal_alloc_thread
1019 NTSTATUS
signal_alloc_thread( TEB
**teb
)
1021 static size_t sigstack_alignment
;
1025 if (!sigstack_alignment
)
1027 size_t min_size
= page_size
; /* this is just for the TEB, we don't use a signal stack yet */
1028 /* find the first power of two not smaller than min_size */
1029 while ((1u << sigstack_alignment
) < min_size
) sigstack_alignment
++;
1030 assert( sizeof(TEB
) <= min_size
);
1033 size
= 1 << sigstack_alignment
;
1035 if (!(status
= virtual_alloc_aligned( (void **)teb
, 0, &size
, MEM_COMMIT
| MEM_TOP_DOWN
,
1036 PAGE_READWRITE
, sigstack_alignment
)))
1038 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
1039 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
1045 /**********************************************************************
1046 * signal_free_thread
1048 void signal_free_thread( TEB
*teb
)
1052 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
1056 /**********************************************************************
1057 * signal_init_thread
1059 void signal_init_thread( TEB
*teb
)
1061 static BOOL init_done
;
1065 pthread_key_create( &teb_key
, NULL
);
1068 pthread_setspecific( teb_key
, teb
);
1072 /**********************************************************************
1073 * signal_init_process
1075 void signal_init_process(void)
1077 struct sigaction sig_act
;
1079 sig_act
.sa_mask
= server_block_set
;
1080 sig_act
.sa_flags
= SA_RESTART
| SA_SIGINFO
;
1082 sig_act
.sa_sigaction
= int_handler
;
1083 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
1084 sig_act
.sa_sigaction
= fpe_handler
;
1085 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
1086 sig_act
.sa_sigaction
= abrt_handler
;
1087 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
1088 sig_act
.sa_sigaction
= quit_handler
;
1089 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
1090 sig_act
.sa_sigaction
= usr1_handler
;
1091 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
1093 sig_act
.sa_sigaction
= segv_handler
;
1094 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
1095 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
1097 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
1101 sig_act
.sa_sigaction
= trap_handler
;
1102 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
1107 perror("sigaction");
1112 /***********************************************************************
1113 * RtlUnwind (NTDLL.@)
1115 void WINAPI
RtlUnwind( PVOID pEndFrame
, PVOID targetIp
, PEXCEPTION_RECORD pRecord
, PVOID retval
)
1117 FIXME( "Not implemented on PowerPC\n" );
1120 /*******************************************************************
1121 * NtRaiseException (NTDLL.@)
1123 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
1125 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
1126 if (status
== STATUS_SUCCESS
) NtSetContextThread( GetCurrentThread(), context
);
1130 /***********************************************************************
1131 * RtlRaiseException (NTDLL.@)
1133 void WINAPI
RtlRaiseException( EXCEPTION_RECORD
*rec
)
1138 RtlCaptureContext( &context
);
1139 rec
->ExceptionAddress
= (void *)context
.Iar
;
1140 status
= raise_exception( rec
, &context
, TRUE
);
1141 if (status
) raise_status( status
, rec
);
1144 /*************************************************************************
1145 * RtlCaptureStackBackTrace (NTDLL.@)
1147 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
1149 FIXME( "(%d, %d, %p, %p) stub!\n", skip
, count
, buffer
, hash
);
1153 /***********************************************************************
1154 * call_thread_entry_point
1156 static void WINAPI
call_thread_entry_point( LPTHREAD_START_ROUTINE entry
, void *arg
)
1160 TRACE_(relay
)( "\1Starting thread proc %p (arg=%p)\n", entry
, arg
);
1161 RtlExitUserThread( entry( arg
));
1163 __EXCEPT(call_unhandled_exception_filter
)
1165 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
1168 abort(); /* should not be reached */
1171 typedef void (WINAPI
*thread_start_func
)(LPTHREAD_START_ROUTINE
,void *);
1175 thread_start_func start
;
1176 LPTHREAD_START_ROUTINE entry
;
1181 /***********************************************************************
1184 static void thread_startup( void *param
)
1186 CONTEXT context
= { 0 };
1187 struct startup_info
*info
= param
;
1189 /* build the initial context */
1190 context
.ContextFlags
= CONTEXT_FULL
;
1191 context
.Gpr1
= (DWORD
)NtCurrentTeb()->Tib
.StackBase
;
1192 context
.Gpr3
= (DWORD
)info
->entry
;
1193 context
.Gpr4
= (DWORD
)info
->arg
;
1194 context
.Iar
= (DWORD
)info
->start
;
1196 if (info
->suspend
) wait_suspend( &context
);
1197 LdrInitializeThunk( &context
, (void **)&context
.Gpr3
, 0, 0 );
1199 ((thread_start_func
)context
.Iar
)( (LPTHREAD_START_ROUTINE
)context
.Gpr3
, (void *)context
.Gpr4
);
1202 /***********************************************************************
1203 * signal_start_thread
1205 * Thread startup sequence:
1206 * signal_start_thread()
1207 * -> thread_startup()
1208 * -> call_thread_entry_point()
1210 void signal_start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
)
1212 struct startup_info info
= { call_thread_entry_point
, entry
, arg
, suspend
};
1213 wine_switch_to_stack( thread_startup
, &info
, NtCurrentTeb()->Tib
.StackBase
);
1216 /**********************************************************************
1217 * signal_start_process
1219 * Process startup sequence:
1220 * signal_start_process()
1221 * -> thread_startup()
1222 * -> kernel32_start_process()
1224 void signal_start_process( LPTHREAD_START_ROUTINE entry
, BOOL suspend
)
1226 struct startup_info info
= { kernel32_start_process
, entry
, NtCurrentTeb()->Peb
, suspend
};
1227 wine_switch_to_stack( thread_startup
, &info
, NtCurrentTeb()->Tib
.StackBase
);
1230 /***********************************************************************
1231 * signal_exit_thread
1233 void signal_exit_thread( int status
)
1235 exit_thread( status
);
1238 /***********************************************************************
1239 * signal_exit_process
1241 void signal_exit_process( int status
)
1246 /**********************************************************************
1247 * DbgBreakPoint (NTDLL.@)
1249 void WINAPI
DbgBreakPoint(void)
1251 kill(getpid(), SIGTRAP
);
1254 /**********************************************************************
1255 * DbgUserBreakPoint (NTDLL.@)
1257 void WINAPI
DbgUserBreakPoint(void)
1259 kill(getpid(), SIGTRAP
);
1262 /**********************************************************************
1263 * NtCurrentTeb (NTDLL.@)
1265 TEB
* WINAPI
NtCurrentTeb(void)
1267 return pthread_getspecific( teb_key
);
1270 #endif /* __powerpc__ */