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 /***********************************************************************
280 * Copy a register context according to the flags.
282 static void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
284 if (flags
& CONTEXT_CONTROL
)
291 to
->Dsisr
= from
->Dsisr
;
292 to
->Trap
= from
->Trap
;
294 if (flags
& CONTEXT_INTEGER
)
296 to
->Gpr0
= from
->Gpr0
;
297 to
->Gpr1
= from
->Gpr1
;
298 to
->Gpr2
= from
->Gpr2
;
299 to
->Gpr3
= from
->Gpr3
;
300 to
->Gpr4
= from
->Gpr4
;
301 to
->Gpr5
= from
->Gpr5
;
302 to
->Gpr6
= from
->Gpr6
;
303 to
->Gpr7
= from
->Gpr7
;
304 to
->Gpr8
= from
->Gpr8
;
305 to
->Gpr9
= from
->Gpr9
;
306 to
->Gpr10
= from
->Gpr10
;
307 to
->Gpr11
= from
->Gpr11
;
308 to
->Gpr12
= from
->Gpr12
;
309 to
->Gpr13
= from
->Gpr13
;
310 to
->Gpr14
= from
->Gpr14
;
311 to
->Gpr15
= from
->Gpr15
;
312 to
->Gpr16
= from
->Gpr16
;
313 to
->Gpr17
= from
->Gpr17
;
314 to
->Gpr18
= from
->Gpr18
;
315 to
->Gpr19
= from
->Gpr19
;
316 to
->Gpr20
= from
->Gpr20
;
317 to
->Gpr21
= from
->Gpr21
;
318 to
->Gpr22
= from
->Gpr22
;
319 to
->Gpr23
= from
->Gpr23
;
320 to
->Gpr24
= from
->Gpr24
;
321 to
->Gpr25
= from
->Gpr25
;
322 to
->Gpr26
= from
->Gpr26
;
323 to
->Gpr27
= from
->Gpr27
;
324 to
->Gpr28
= from
->Gpr28
;
325 to
->Gpr29
= from
->Gpr29
;
326 to
->Gpr30
= from
->Gpr30
;
327 to
->Gpr31
= from
->Gpr31
;
331 if (flags
& CONTEXT_FLOATING_POINT
)
333 to
->Fpr0
= from
->Fpr0
;
334 to
->Fpr1
= from
->Fpr1
;
335 to
->Fpr2
= from
->Fpr2
;
336 to
->Fpr3
= from
->Fpr3
;
337 to
->Fpr4
= from
->Fpr4
;
338 to
->Fpr5
= from
->Fpr5
;
339 to
->Fpr6
= from
->Fpr6
;
340 to
->Fpr7
= from
->Fpr7
;
341 to
->Fpr8
= from
->Fpr8
;
342 to
->Fpr9
= from
->Fpr9
;
343 to
->Fpr10
= from
->Fpr10
;
344 to
->Fpr11
= from
->Fpr11
;
345 to
->Fpr12
= from
->Fpr12
;
346 to
->Fpr13
= from
->Fpr13
;
347 to
->Fpr14
= from
->Fpr14
;
348 to
->Fpr15
= from
->Fpr15
;
349 to
->Fpr16
= from
->Fpr16
;
350 to
->Fpr17
= from
->Fpr17
;
351 to
->Fpr18
= from
->Fpr18
;
352 to
->Fpr19
= from
->Fpr19
;
353 to
->Fpr20
= from
->Fpr20
;
354 to
->Fpr21
= from
->Fpr21
;
355 to
->Fpr22
= from
->Fpr22
;
356 to
->Fpr23
= from
->Fpr23
;
357 to
->Fpr24
= from
->Fpr24
;
358 to
->Fpr25
= from
->Fpr25
;
359 to
->Fpr26
= from
->Fpr26
;
360 to
->Fpr27
= from
->Fpr27
;
361 to
->Fpr28
= from
->Fpr28
;
362 to
->Fpr29
= from
->Fpr29
;
363 to
->Fpr30
= from
->Fpr30
;
364 to
->Fpr31
= from
->Fpr31
;
365 to
->Fpscr
= from
->Fpscr
;
370 /***********************************************************************
373 * Convert a register context to the server format.
375 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
377 DWORD flags
= from
->ContextFlags
; /* no CPU id? */
379 memset( to
, 0, sizeof(*to
) );
380 to
->cpu
= CPU_POWERPC
;
382 if (flags
& CONTEXT_CONTROL
)
384 to
->flags
|= SERVER_CTX_CONTROL
;
385 to
->ctl
.powerpc_regs
.iar
= from
->Iar
;
386 to
->ctl
.powerpc_regs
.msr
= from
->Msr
;
387 to
->ctl
.powerpc_regs
.ctr
= from
->Ctr
;
388 to
->ctl
.powerpc_regs
.lr
= from
->Lr
;
389 to
->ctl
.powerpc_regs
.dar
= from
->Dar
;
390 to
->ctl
.powerpc_regs
.dsisr
= from
->Dsisr
;
391 to
->ctl
.powerpc_regs
.trap
= from
->Trap
;
393 if (flags
& CONTEXT_INTEGER
)
395 to
->flags
|= SERVER_CTX_INTEGER
;
396 to
->integer
.powerpc_regs
.gpr
[0] = from
->Gpr0
;
397 to
->integer
.powerpc_regs
.gpr
[1] = from
->Gpr1
;
398 to
->integer
.powerpc_regs
.gpr
[2] = from
->Gpr2
;
399 to
->integer
.powerpc_regs
.gpr
[3] = from
->Gpr3
;
400 to
->integer
.powerpc_regs
.gpr
[4] = from
->Gpr4
;
401 to
->integer
.powerpc_regs
.gpr
[5] = from
->Gpr5
;
402 to
->integer
.powerpc_regs
.gpr
[6] = from
->Gpr6
;
403 to
->integer
.powerpc_regs
.gpr
[7] = from
->Gpr7
;
404 to
->integer
.powerpc_regs
.gpr
[8] = from
->Gpr8
;
405 to
->integer
.powerpc_regs
.gpr
[9] = from
->Gpr9
;
406 to
->integer
.powerpc_regs
.gpr
[10] = from
->Gpr10
;
407 to
->integer
.powerpc_regs
.gpr
[11] = from
->Gpr11
;
408 to
->integer
.powerpc_regs
.gpr
[12] = from
->Gpr12
;
409 to
->integer
.powerpc_regs
.gpr
[13] = from
->Gpr13
;
410 to
->integer
.powerpc_regs
.gpr
[14] = from
->Gpr14
;
411 to
->integer
.powerpc_regs
.gpr
[15] = from
->Gpr15
;
412 to
->integer
.powerpc_regs
.gpr
[16] = from
->Gpr16
;
413 to
->integer
.powerpc_regs
.gpr
[17] = from
->Gpr17
;
414 to
->integer
.powerpc_regs
.gpr
[18] = from
->Gpr18
;
415 to
->integer
.powerpc_regs
.gpr
[19] = from
->Gpr19
;
416 to
->integer
.powerpc_regs
.gpr
[20] = from
->Gpr20
;
417 to
->integer
.powerpc_regs
.gpr
[21] = from
->Gpr21
;
418 to
->integer
.powerpc_regs
.gpr
[22] = from
->Gpr22
;
419 to
->integer
.powerpc_regs
.gpr
[23] = from
->Gpr23
;
420 to
->integer
.powerpc_regs
.gpr
[24] = from
->Gpr24
;
421 to
->integer
.powerpc_regs
.gpr
[25] = from
->Gpr25
;
422 to
->integer
.powerpc_regs
.gpr
[26] = from
->Gpr26
;
423 to
->integer
.powerpc_regs
.gpr
[27] = from
->Gpr27
;
424 to
->integer
.powerpc_regs
.gpr
[28] = from
->Gpr28
;
425 to
->integer
.powerpc_regs
.gpr
[29] = from
->Gpr29
;
426 to
->integer
.powerpc_regs
.gpr
[30] = from
->Gpr30
;
427 to
->integer
.powerpc_regs
.gpr
[31] = from
->Gpr31
;
428 to
->integer
.powerpc_regs
.xer
= from
->Xer
;
429 to
->integer
.powerpc_regs
.cr
= from
->Cr
;
431 if (flags
& CONTEXT_FLOATING_POINT
)
433 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
434 to
->fp
.powerpc_regs
.fpr
[0] = from
->Fpr0
;
435 to
->fp
.powerpc_regs
.fpr
[1] = from
->Fpr1
;
436 to
->fp
.powerpc_regs
.fpr
[2] = from
->Fpr2
;
437 to
->fp
.powerpc_regs
.fpr
[3] = from
->Fpr3
;
438 to
->fp
.powerpc_regs
.fpr
[4] = from
->Fpr4
;
439 to
->fp
.powerpc_regs
.fpr
[5] = from
->Fpr5
;
440 to
->fp
.powerpc_regs
.fpr
[6] = from
->Fpr6
;
441 to
->fp
.powerpc_regs
.fpr
[7] = from
->Fpr7
;
442 to
->fp
.powerpc_regs
.fpr
[8] = from
->Fpr8
;
443 to
->fp
.powerpc_regs
.fpr
[9] = from
->Fpr9
;
444 to
->fp
.powerpc_regs
.fpr
[10] = from
->Fpr10
;
445 to
->fp
.powerpc_regs
.fpr
[11] = from
->Fpr11
;
446 to
->fp
.powerpc_regs
.fpr
[12] = from
->Fpr12
;
447 to
->fp
.powerpc_regs
.fpr
[13] = from
->Fpr13
;
448 to
->fp
.powerpc_regs
.fpr
[14] = from
->Fpr14
;
449 to
->fp
.powerpc_regs
.fpr
[15] = from
->Fpr15
;
450 to
->fp
.powerpc_regs
.fpr
[16] = from
->Fpr16
;
451 to
->fp
.powerpc_regs
.fpr
[17] = from
->Fpr17
;
452 to
->fp
.powerpc_regs
.fpr
[18] = from
->Fpr18
;
453 to
->fp
.powerpc_regs
.fpr
[19] = from
->Fpr19
;
454 to
->fp
.powerpc_regs
.fpr
[20] = from
->Fpr20
;
455 to
->fp
.powerpc_regs
.fpr
[21] = from
->Fpr21
;
456 to
->fp
.powerpc_regs
.fpr
[22] = from
->Fpr22
;
457 to
->fp
.powerpc_regs
.fpr
[23] = from
->Fpr23
;
458 to
->fp
.powerpc_regs
.fpr
[24] = from
->Fpr24
;
459 to
->fp
.powerpc_regs
.fpr
[25] = from
->Fpr25
;
460 to
->fp
.powerpc_regs
.fpr
[26] = from
->Fpr26
;
461 to
->fp
.powerpc_regs
.fpr
[27] = from
->Fpr27
;
462 to
->fp
.powerpc_regs
.fpr
[28] = from
->Fpr28
;
463 to
->fp
.powerpc_regs
.fpr
[29] = from
->Fpr29
;
464 to
->fp
.powerpc_regs
.fpr
[30] = from
->Fpr30
;
465 to
->fp
.powerpc_regs
.fpr
[31] = from
->Fpr31
;
466 to
->fp
.powerpc_regs
.fpscr
= from
->Fpscr
;
468 return STATUS_SUCCESS
;
472 /***********************************************************************
473 * context_from_server
475 * Convert a register context from the server format.
477 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
479 if (from
->cpu
!= CPU_POWERPC
) return STATUS_INVALID_PARAMETER
;
481 to
->ContextFlags
= 0; /* no CPU id? */
482 if (from
->flags
& SERVER_CTX_CONTROL
)
484 to
->ContextFlags
|= CONTEXT_CONTROL
;
485 to
->Msr
= from
->ctl
.powerpc_regs
.msr
;
486 to
->Ctr
= from
->ctl
.powerpc_regs
.ctr
;
487 to
->Iar
= from
->ctl
.powerpc_regs
.iar
;
488 to
->Lr
= from
->ctl
.powerpc_regs
.lr
;
489 to
->Dar
= from
->ctl
.powerpc_regs
.dar
;
490 to
->Dsisr
= from
->ctl
.powerpc_regs
.dsisr
;
491 to
->Trap
= from
->ctl
.powerpc_regs
.trap
;
493 if (from
->flags
& SERVER_CTX_INTEGER
)
495 to
->ContextFlags
|= CONTEXT_INTEGER
;
496 to
->Gpr0
= from
->integer
.powerpc_regs
.gpr
[0];
497 to
->Gpr1
= from
->integer
.powerpc_regs
.gpr
[1];
498 to
->Gpr2
= from
->integer
.powerpc_regs
.gpr
[2];
499 to
->Gpr3
= from
->integer
.powerpc_regs
.gpr
[3];
500 to
->Gpr4
= from
->integer
.powerpc_regs
.gpr
[4];
501 to
->Gpr5
= from
->integer
.powerpc_regs
.gpr
[5];
502 to
->Gpr6
= from
->integer
.powerpc_regs
.gpr
[6];
503 to
->Gpr7
= from
->integer
.powerpc_regs
.gpr
[7];
504 to
->Gpr8
= from
->integer
.powerpc_regs
.gpr
[8];
505 to
->Gpr9
= from
->integer
.powerpc_regs
.gpr
[9];
506 to
->Gpr10
= from
->integer
.powerpc_regs
.gpr
[10];
507 to
->Gpr11
= from
->integer
.powerpc_regs
.gpr
[11];
508 to
->Gpr12
= from
->integer
.powerpc_regs
.gpr
[12];
509 to
->Gpr13
= from
->integer
.powerpc_regs
.gpr
[13];
510 to
->Gpr14
= from
->integer
.powerpc_regs
.gpr
[14];
511 to
->Gpr15
= from
->integer
.powerpc_regs
.gpr
[15];
512 to
->Gpr16
= from
->integer
.powerpc_regs
.gpr
[16];
513 to
->Gpr17
= from
->integer
.powerpc_regs
.gpr
[17];
514 to
->Gpr18
= from
->integer
.powerpc_regs
.gpr
[18];
515 to
->Gpr19
= from
->integer
.powerpc_regs
.gpr
[19];
516 to
->Gpr20
= from
->integer
.powerpc_regs
.gpr
[20];
517 to
->Gpr21
= from
->integer
.powerpc_regs
.gpr
[21];
518 to
->Gpr22
= from
->integer
.powerpc_regs
.gpr
[22];
519 to
->Gpr23
= from
->integer
.powerpc_regs
.gpr
[23];
520 to
->Gpr24
= from
->integer
.powerpc_regs
.gpr
[24];
521 to
->Gpr25
= from
->integer
.powerpc_regs
.gpr
[25];
522 to
->Gpr26
= from
->integer
.powerpc_regs
.gpr
[26];
523 to
->Gpr27
= from
->integer
.powerpc_regs
.gpr
[27];
524 to
->Gpr28
= from
->integer
.powerpc_regs
.gpr
[28];
525 to
->Gpr29
= from
->integer
.powerpc_regs
.gpr
[29];
526 to
->Gpr30
= from
->integer
.powerpc_regs
.gpr
[30];
527 to
->Gpr31
= from
->integer
.powerpc_regs
.gpr
[31];
528 to
->Xer
= from
->integer
.powerpc_regs
.xer
;
529 to
->Cr
= from
->integer
.powerpc_regs
.cr
;
531 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
533 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
534 to
->Fpr0
= from
->fp
.powerpc_regs
.fpr
[0];
535 to
->Fpr1
= from
->fp
.powerpc_regs
.fpr
[1];
536 to
->Fpr2
= from
->fp
.powerpc_regs
.fpr
[2];
537 to
->Fpr3
= from
->fp
.powerpc_regs
.fpr
[3];
538 to
->Fpr4
= from
->fp
.powerpc_regs
.fpr
[4];
539 to
->Fpr5
= from
->fp
.powerpc_regs
.fpr
[5];
540 to
->Fpr6
= from
->fp
.powerpc_regs
.fpr
[6];
541 to
->Fpr7
= from
->fp
.powerpc_regs
.fpr
[7];
542 to
->Fpr8
= from
->fp
.powerpc_regs
.fpr
[8];
543 to
->Fpr9
= from
->fp
.powerpc_regs
.fpr
[9];
544 to
->Fpr10
= from
->fp
.powerpc_regs
.fpr
[10];
545 to
->Fpr11
= from
->fp
.powerpc_regs
.fpr
[11];
546 to
->Fpr12
= from
->fp
.powerpc_regs
.fpr
[12];
547 to
->Fpr13
= from
->fp
.powerpc_regs
.fpr
[13];
548 to
->Fpr14
= from
->fp
.powerpc_regs
.fpr
[14];
549 to
->Fpr15
= from
->fp
.powerpc_regs
.fpr
[15];
550 to
->Fpr16
= from
->fp
.powerpc_regs
.fpr
[16];
551 to
->Fpr17
= from
->fp
.powerpc_regs
.fpr
[17];
552 to
->Fpr18
= from
->fp
.powerpc_regs
.fpr
[18];
553 to
->Fpr19
= from
->fp
.powerpc_regs
.fpr
[19];
554 to
->Fpr20
= from
->fp
.powerpc_regs
.fpr
[20];
555 to
->Fpr21
= from
->fp
.powerpc_regs
.fpr
[21];
556 to
->Fpr22
= from
->fp
.powerpc_regs
.fpr
[22];
557 to
->Fpr23
= from
->fp
.powerpc_regs
.fpr
[23];
558 to
->Fpr24
= from
->fp
.powerpc_regs
.fpr
[24];
559 to
->Fpr25
= from
->fp
.powerpc_regs
.fpr
[25];
560 to
->Fpr26
= from
->fp
.powerpc_regs
.fpr
[26];
561 to
->Fpr27
= from
->fp
.powerpc_regs
.fpr
[27];
562 to
->Fpr28
= from
->fp
.powerpc_regs
.fpr
[28];
563 to
->Fpr29
= from
->fp
.powerpc_regs
.fpr
[29];
564 to
->Fpr30
= from
->fp
.powerpc_regs
.fpr
[30];
565 to
->Fpr31
= from
->fp
.powerpc_regs
.fpr
[31];
566 to
->Fpscr
= from
->fp
.powerpc_regs
.fpscr
;
568 return STATUS_SUCCESS
;
572 /***********************************************************************
573 * NtSetContextThread (NTDLL.@)
574 * ZwSetContextThread (NTDLL.@)
576 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
581 ret
= set_thread_context( handle
, context
, &self
);
582 if (self
&& ret
== STATUS_SUCCESS
) set_cpu_context( context
);
587 /***********************************************************************
588 * NtGetContextThread (NTDLL.@)
589 * ZwGetContextThread (NTDLL.@)
591 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
594 DWORD needed_flags
= context
->ContextFlags
;
595 BOOL self
= (handle
== GetCurrentThread());
599 if ((ret
= get_thread_context( handle
, context
, &self
))) return ret
;
600 needed_flags
&= ~context
->ContextFlags
;
603 if (self
&& needed_flags
)
606 RtlCaptureContext( &ctx
);
607 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
608 context
->ContextFlags
|= ctx
.ContextFlags
& needed_flags
;
610 return STATUS_SUCCESS
;
614 /**********************************************************************
615 * call_stack_handlers
617 * Call the stack handlers chain.
619 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
621 EXCEPTION_POINTERS ptrs
;
623 FIXME( "not implemented on PowerPC\n" );
625 /* hack: call unhandled exception filter directly */
626 ptrs
.ExceptionRecord
= rec
;
627 ptrs
.ContextRecord
= context
;
628 unhandled_exception_filter( &ptrs
);
629 return STATUS_UNHANDLED_EXCEPTION
;
633 /*******************************************************************
636 * Implementation of NtRaiseException.
638 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
646 TRACE( "code=%x flags=%x addr=%p ip=%x tid=%04x\n",
647 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
648 context
->Iar
, GetCurrentThreadId() );
649 for (c
= 0; c
< rec
->NumberParameters
; c
++)
650 TRACE( " info[%d]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
651 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
653 if (rec
->ExceptionInformation
[1] >> 16)
654 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
655 rec
->ExceptionAddress
,
656 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
658 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
659 rec
->ExceptionAddress
,
660 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
664 /* FIXME: dump context */
667 status
= send_debug_event( rec
, TRUE
, context
);
668 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
669 return STATUS_SUCCESS
;
671 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
672 return STATUS_SUCCESS
;
674 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
678 /* last chance exception */
680 status
= send_debug_event( rec
, FALSE
, context
);
681 if (status
!= DBG_CONTINUE
)
683 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
684 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
685 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
686 ERR("Process attempted to continue execution after noncontinuable exception.\n");
688 ERR("Unhandled exception code %x flags %x addr %p\n",
689 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
690 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
692 return STATUS_SUCCESS
;
696 /**********************************************************************
699 * Handler for SIGSEGV and related errors.
701 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
703 EXCEPTION_RECORD rec
;
707 save_context( &context
, sigcontext
);
709 rec
.ExceptionRecord
= NULL
;
710 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
711 rec
.ExceptionAddress
= (LPVOID
)context
.Iar
;
712 rec
.NumberParameters
= 0;
717 switch (siginfo
->si_code
& 0xffff)
721 rec
.NumberParameters
= 2;
722 rec
.ExceptionInformation
[0] = 0; /* FIXME ? */
723 rec
.ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
724 if (!(rec
.ExceptionCode
= virtual_handle_fault(siginfo
->si_addr
, rec
.ExceptionInformation
[0], FALSE
)))
728 FIXME("Unhandled SIGSEGV/%x\n",siginfo
->si_code
);
733 switch (siginfo
->si_code
& 0xffff)
736 rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
743 /* FIXME: correct for all cases ? */
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
)))
752 FIXME("Unhandled SIGBUS/%x\n",siginfo
->si_code
);
757 switch (siginfo
->si_code
& 0xffff)
759 case ILL_ILLOPC
: /* illegal opcode */
761 case ILL_ILLOPN
: /* illegal operand */
764 case ILL_ILLADR
: /* illegal addressing mode */
767 case ILL_ILLTRP
: /* illegal trap */
770 case ILL_COPROC
: /* coprocessor error */
772 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
774 case ILL_PRVOPC
: /* privileged opcode */
776 case ILL_PRVREG
: /* privileged register */
778 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
781 case ILL_BADSTK
: /* internal stack error */
782 rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
786 FIXME("Unhandled SIGILL/%x\n", siginfo
->si_code
);
791 status
= raise_exception( &rec
, &context
, TRUE
);
792 if (status
) raise_status( status
, &rec
);
794 restore_context( &context
, sigcontext
);
797 /**********************************************************************
800 * Handler for SIGTRAP.
802 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
804 EXCEPTION_RECORD rec
;
808 save_context( &context
, sigcontext
);
810 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
811 rec
.ExceptionRecord
= NULL
;
812 rec
.ExceptionAddress
= (LPVOID
)context
.Iar
;
813 rec
.NumberParameters
= 0;
815 /* FIXME: check if we might need to modify PC */
816 switch (siginfo
->si_code
& 0xffff)
820 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
825 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
829 FIXME("Unhandled SIGTRAP/%x\n", siginfo
->si_code
);
832 status
= raise_exception( &rec
, &context
, TRUE
);
833 if (status
) raise_status( status
, &rec
);
834 restore_context( &context
, sigcontext
);
837 /**********************************************************************
840 * Handler for SIGFPE.
842 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
844 EXCEPTION_RECORD rec
;
848 save_fpu( &context
, sigcontext
);
849 save_context( &context
, sigcontext
);
851 switch (siginfo
->si_code
& 0xffff )
855 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
860 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
865 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
870 rec
.ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
875 rec
.ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
880 rec
.ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
885 rec
.ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
892 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
895 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
896 rec
.ExceptionRecord
= NULL
;
897 rec
.ExceptionAddress
= (LPVOID
)context
.Iar
;
898 rec
.NumberParameters
= 0;
899 status
= raise_exception( &rec
, &context
, TRUE
);
900 if (status
) raise_status( status
, &rec
);
902 restore_context( &context
, sigcontext
);
903 restore_fpu( &context
, sigcontext
);
906 /**********************************************************************
909 * Handler for SIGINT.
911 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
913 if (!dispatch_signal(SIGINT
))
915 EXCEPTION_RECORD rec
;
919 save_context( &context
, sigcontext
);
920 rec
.ExceptionCode
= CONTROL_C_EXIT
;
921 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
922 rec
.ExceptionRecord
= NULL
;
923 rec
.ExceptionAddress
= (LPVOID
)context
.Iar
;
924 rec
.NumberParameters
= 0;
925 status
= raise_exception( &rec
, &context
, TRUE
);
926 if (status
) raise_status( status
, &rec
);
927 restore_context( &context
, sigcontext
);
932 /**********************************************************************
935 * Handler for SIGABRT.
937 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
939 EXCEPTION_RECORD rec
;
943 save_context( &context
, sigcontext
);
944 rec
.ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
945 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
946 rec
.ExceptionRecord
= NULL
;
947 rec
.ExceptionAddress
= (LPVOID
)context
.Iar
;
948 rec
.NumberParameters
= 0;
949 status
= raise_exception( &rec
, &context
, TRUE
);
950 if (status
) raise_status( status
, &rec
);
951 restore_context( &context
, sigcontext
);
955 /**********************************************************************
958 * Handler for SIGQUIT.
960 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
966 /**********************************************************************
969 * Handler for SIGUSR1, used to signal a thread that it got suspended.
971 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
975 save_context( &context
, sigcontext
);
976 wait_suspend( &context
);
977 restore_context( &context
, sigcontext
);
981 /***********************************************************************
982 * __wine_set_signal_handler (NTDLL.@)
984 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
986 if (sig
>= sizeof(handlers
) / sizeof(handlers
[0])) return -1;
987 if (handlers
[sig
] != NULL
) return -2;
993 /**********************************************************************
994 * signal_alloc_thread
996 NTSTATUS
signal_alloc_thread( TEB
**teb
)
998 static size_t sigstack_zero_bits
;
1002 if (!sigstack_zero_bits
)
1004 size_t min_size
= page_size
; /* this is just for the TEB, we don't use a signal stack yet */
1005 /* find the first power of two not smaller than min_size */
1006 while ((1u << sigstack_zero_bits
) < min_size
) sigstack_zero_bits
++;
1007 assert( sizeof(TEB
) <= min_size
);
1010 size
= 1 << sigstack_zero_bits
;
1012 if (!(status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb
, sigstack_zero_bits
,
1013 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
1015 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
1016 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
1022 /**********************************************************************
1023 * signal_free_thread
1025 void signal_free_thread( TEB
*teb
)
1029 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
1033 /**********************************************************************
1034 * signal_init_thread
1036 void signal_init_thread( TEB
*teb
)
1038 static BOOL init_done
;
1042 pthread_key_create( &teb_key
, NULL
);
1045 pthread_setspecific( teb_key
, teb
);
1049 /**********************************************************************
1050 * signal_init_process
1052 void signal_init_process(void)
1054 struct sigaction sig_act
;
1056 sig_act
.sa_mask
= server_block_set
;
1057 sig_act
.sa_flags
= SA_RESTART
| SA_SIGINFO
;
1059 sig_act
.sa_sigaction
= int_handler
;
1060 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
1061 sig_act
.sa_sigaction
= fpe_handler
;
1062 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
1063 sig_act
.sa_sigaction
= abrt_handler
;
1064 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
1065 sig_act
.sa_sigaction
= quit_handler
;
1066 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
1067 sig_act
.sa_sigaction
= usr1_handler
;
1068 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
1070 sig_act
.sa_sigaction
= segv_handler
;
1071 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
1072 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
1074 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
1078 sig_act
.sa_sigaction
= trap_handler
;
1079 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
1084 perror("sigaction");
1089 /***********************************************************************
1090 * RtlUnwind (NTDLL.@)
1092 void WINAPI
RtlUnwind( PVOID pEndFrame
, PVOID targetIp
, PEXCEPTION_RECORD pRecord
, PVOID retval
)
1094 FIXME( "Not implemented on PowerPC\n" );
1097 /*******************************************************************
1098 * NtRaiseException (NTDLL.@)
1100 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
1102 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
1103 if (status
== STATUS_SUCCESS
) NtSetContextThread( GetCurrentThread(), context
);
1107 /***********************************************************************
1108 * RtlRaiseException (NTDLL.@)
1110 void WINAPI
RtlRaiseException( EXCEPTION_RECORD
*rec
)
1115 RtlCaptureContext( &context
);
1116 rec
->ExceptionAddress
= (void *)context
.Iar
;
1117 status
= raise_exception( rec
, &context
, TRUE
);
1118 if (status
) raise_status( status
, rec
);
1121 /*************************************************************************
1122 * RtlCaptureStackBackTrace (NTDLL.@)
1124 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
1126 FIXME( "(%d, %d, %p, %p) stub!\n", skip
, count
, buffer
, hash
);
1130 /***********************************************************************
1131 * call_thread_entry_point
1133 static void WINAPI
call_thread_entry_point( LPTHREAD_START_ROUTINE entry
, void *arg
)
1137 TRACE_(relay
)( "\1Starting thread proc %p (arg=%p)\n", entry
, arg
);
1138 RtlExitUserThread( entry( arg
));
1140 __EXCEPT(unhandled_exception_filter
)
1142 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
1145 abort(); /* should not be reached */
1148 typedef void (WINAPI
*thread_start_func
)(LPTHREAD_START_ROUTINE
,void *);
1152 thread_start_func start
;
1153 LPTHREAD_START_ROUTINE entry
;
1158 /***********************************************************************
1161 static void thread_startup( void *param
)
1163 CONTEXT context
= { 0 };
1164 struct startup_info
*info
= param
;
1166 /* build the initial context */
1167 context
.ContextFlags
= CONTEXT_FULL
;
1168 context
.Gpr1
= (DWORD
)NtCurrentTeb()->Tib
.StackBase
;
1169 context
.Gpr3
= (DWORD
)info
->entry
;
1170 context
.Gpr4
= (DWORD
)info
->arg
;
1171 context
.Iar
= (DWORD
)info
->start
;
1173 if (info
->suspend
) wait_suspend( &context
);
1174 attach_dlls( &context
);
1176 ((thread_start_func
)context
.Iar
)( (LPTHREAD_START_ROUTINE
)context
.Gpr3
, (void *)context
.Gpr4
);
1179 /***********************************************************************
1180 * signal_start_thread
1182 * Thread startup sequence:
1183 * signal_start_thread()
1184 * -> thread_startup()
1185 * -> call_thread_entry_point()
1187 void signal_start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
)
1189 struct startup_info info
= { call_thread_entry_point
, entry
, arg
, suspend
};
1190 wine_switch_to_stack( thread_startup
, &info
, NtCurrentTeb()->Tib
.StackBase
);
1193 /**********************************************************************
1194 * signal_start_process
1196 * Process startup sequence:
1197 * signal_start_process()
1198 * -> thread_startup()
1199 * -> kernel32_start_process()
1201 void signal_start_process( LPTHREAD_START_ROUTINE entry
, BOOL suspend
)
1203 struct startup_info info
= { kernel32_start_process
, entry
, NtCurrentTeb()->Peb
, suspend
};
1204 wine_switch_to_stack( thread_startup
, &info
, NtCurrentTeb()->Tib
.StackBase
);
1207 /***********************************************************************
1208 * signal_exit_thread
1210 void signal_exit_thread( int status
)
1212 exit_thread( status
);
1215 /***********************************************************************
1216 * signal_exit_process
1218 void signal_exit_process( int status
)
1223 /**********************************************************************
1224 * DbgBreakPoint (NTDLL.@)
1226 void WINAPI
DbgBreakPoint(void)
1228 kill(getpid(), SIGTRAP
);
1231 /**********************************************************************
1232 * DbgUserBreakPoint (NTDLL.@)
1234 void WINAPI
DbgUserBreakPoint(void)
1236 kill(getpid(), SIGTRAP
);
1239 /**********************************************************************
1240 * NtCurrentTeb (NTDLL.@)
1242 TEB
* WINAPI
NtCurrentTeb(void)
1244 return pthread_getspecific( teb_key
);
1247 #endif /* __powerpc__ */