2 * Sparc signal handling routines
4 * Copyright 1999 Ulrich Weigand
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"
34 #include <sys/ucontext.h>
37 #define WIN32_NO_STATUS
42 #include "wine/exception.h"
43 #include "ntdll_misc.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
49 static pthread_key_t teb_key
;
51 #define HANDLER_DEF(name) void name( int __signal, struct siginfo *__siginfo, ucontext_t *__context )
52 #define HANDLER_CONTEXT (__context)
54 typedef int (*wine_signal_handler
)(unsigned int sig
);
56 static wine_signal_handler handlers
[256];
58 /***********************************************************************
61 static inline int dispatch_signal(unsigned int sig
)
63 if (handlers
[sig
] == NULL
) return 0;
64 return handlers
[sig
](sig
);
69 * FIXME: All this works only on Solaris for now
72 /**********************************************************************
75 static void save_context( CONTEXT
*context
, ucontext_t
*ucontext
)
77 /* Special registers */
78 context
->psr
= ucontext
->uc_mcontext
.gregs
[REG_PSR
];
79 context
->pc
= ucontext
->uc_mcontext
.gregs
[REG_PC
];
80 context
->npc
= ucontext
->uc_mcontext
.gregs
[REG_nPC
];
81 context
->y
= ucontext
->uc_mcontext
.gregs
[REG_Y
];
82 context
->wim
= 0; /* FIXME */
83 context
->tbr
= 0; /* FIXME */
85 /* Global registers */
86 context
->g0
= 0; /* always */
87 context
->g1
= ucontext
->uc_mcontext
.gregs
[REG_G1
];
88 context
->g2
= ucontext
->uc_mcontext
.gregs
[REG_G2
];
89 context
->g3
= ucontext
->uc_mcontext
.gregs
[REG_G3
];
90 context
->g4
= ucontext
->uc_mcontext
.gregs
[REG_G4
];
91 context
->g5
= ucontext
->uc_mcontext
.gregs
[REG_G5
];
92 context
->g6
= ucontext
->uc_mcontext
.gregs
[REG_G6
];
93 context
->g7
= ucontext
->uc_mcontext
.gregs
[REG_G7
];
95 /* Current 'out' registers */
96 context
->o0
= ucontext
->uc_mcontext
.gregs
[REG_O0
];
97 context
->o1
= ucontext
->uc_mcontext
.gregs
[REG_O1
];
98 context
->o2
= ucontext
->uc_mcontext
.gregs
[REG_O2
];
99 context
->o3
= ucontext
->uc_mcontext
.gregs
[REG_O3
];
100 context
->o4
= ucontext
->uc_mcontext
.gregs
[REG_O4
];
101 context
->o5
= ucontext
->uc_mcontext
.gregs
[REG_O5
];
102 context
->o6
= ucontext
->uc_mcontext
.gregs
[REG_O6
];
103 context
->o7
= ucontext
->uc_mcontext
.gregs
[REG_O7
];
105 /* FIXME: what if the current register window isn't saved? */
106 if ( ucontext
->uc_mcontext
.gwins
&& ucontext
->uc_mcontext
.gwins
->wbcnt
> 0 )
108 /* Current 'local' registers from first register window */
109 context
->l0
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[0];
110 context
->l1
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[1];
111 context
->l2
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[2];
112 context
->l3
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[3];
113 context
->l4
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[4];
114 context
->l5
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[5];
115 context
->l6
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[6];
116 context
->l7
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[7];
118 /* Current 'in' registers from first register window */
119 context
->i0
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[0];
120 context
->i1
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[1];
121 context
->i2
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[2];
122 context
->i3
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[3];
123 context
->i4
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[4];
124 context
->i5
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[5];
125 context
->i6
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[6];
126 context
->i7
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[7];
130 /**********************************************************************
133 static void restore_context( CONTEXT
*context
, ucontext_t
*ucontext
)
138 /**********************************************************************
141 static void save_fpu( CONTEXT
*context
, ucontext_t
*ucontext
)
146 /**********************************************************************
149 static void restore_fpu( CONTEXT
*context
, ucontext_t
*ucontext
)
155 /***********************************************************************
156 * RtlCaptureContext (NTDLL.@)
158 void WINAPI
RtlCaptureContext( CONTEXT
*context
)
160 FIXME("not implemented\n");
161 memset( context
, 0, sizeof(*context
) );
165 /***********************************************************************
168 * Set the new CPU context.
170 void set_cpu_context( const CONTEXT
*context
)
172 FIXME("not implemented\n");
176 /***********************************************************************
179 * Copy a register context according to the flags.
181 void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
183 flags
&= ~CONTEXT_SPARC
; /* get rid of CPU id */
184 if (flags
& CONTEXT_CONTROL
)
193 if (flags
& CONTEXT_INTEGER
)
228 if (flags
& CONTEXT_FLOATING_POINT
)
235 /***********************************************************************
238 * Convert a register context to the server format.
240 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
242 DWORD flags
= from
->ContextFlags
& ~CONTEXT_SPARC
; /* get rid of CPU id */
244 memset( to
, 0, sizeof(*to
) );
247 if (flags
& CONTEXT_CONTROL
)
249 to
->flags
|= SERVER_CTX_CONTROL
;
250 to
->ctl
.sparc_regs
.psr
= from
->psr
;
251 to
->ctl
.sparc_regs
.pc
= from
->pc
;
252 to
->ctl
.sparc_regs
.npc
= from
->npc
;
253 to
->ctl
.sparc_regs
.y
= from
->y
;
254 to
->ctl
.sparc_regs
.wim
= from
->wim
;
255 to
->ctl
.sparc_regs
.tbr
= from
->tbr
;
257 if (flags
& CONTEXT_INTEGER
)
259 to
->flags
|= SERVER_CTX_INTEGER
;
260 to
->integer
.sparc_regs
.g
[0] = from
->g0
;
261 to
->integer
.sparc_regs
.g
[1] = from
->g1
;
262 to
->integer
.sparc_regs
.g
[2] = from
->g2
;
263 to
->integer
.sparc_regs
.g
[3] = from
->g3
;
264 to
->integer
.sparc_regs
.g
[4] = from
->g4
;
265 to
->integer
.sparc_regs
.g
[5] = from
->g5
;
266 to
->integer
.sparc_regs
.g
[6] = from
->g6
;
267 to
->integer
.sparc_regs
.g
[7] = from
->g7
;
268 to
->integer
.sparc_regs
.o
[0] = from
->o0
;
269 to
->integer
.sparc_regs
.o
[1] = from
->o1
;
270 to
->integer
.sparc_regs
.o
[2] = from
->o2
;
271 to
->integer
.sparc_regs
.o
[3] = from
->o3
;
272 to
->integer
.sparc_regs
.o
[4] = from
->o4
;
273 to
->integer
.sparc_regs
.o
[5] = from
->o5
;
274 to
->integer
.sparc_regs
.o
[6] = from
->o6
;
275 to
->integer
.sparc_regs
.o
[7] = from
->o7
;
276 to
->integer
.sparc_regs
.l
[0] = from
->l0
;
277 to
->integer
.sparc_regs
.l
[1] = from
->l1
;
278 to
->integer
.sparc_regs
.l
[2] = from
->l2
;
279 to
->integer
.sparc_regs
.l
[3] = from
->l3
;
280 to
->integer
.sparc_regs
.l
[4] = from
->l4
;
281 to
->integer
.sparc_regs
.l
[5] = from
->l5
;
282 to
->integer
.sparc_regs
.l
[6] = from
->l6
;
283 to
->integer
.sparc_regs
.l
[7] = from
->l7
;
284 to
->integer
.sparc_regs
.i
[0] = from
->i0
;
285 to
->integer
.sparc_regs
.i
[1] = from
->i1
;
286 to
->integer
.sparc_regs
.i
[2] = from
->i2
;
287 to
->integer
.sparc_regs
.i
[3] = from
->i3
;
288 to
->integer
.sparc_regs
.i
[4] = from
->i4
;
289 to
->integer
.sparc_regs
.i
[5] = from
->i5
;
290 to
->integer
.sparc_regs
.i
[6] = from
->i6
;
291 to
->integer
.sparc_regs
.i
[7] = from
->i7
;
293 if (flags
& CONTEXT_FLOATING_POINT
)
297 return STATUS_SUCCESS
;
301 /***********************************************************************
302 * context_from_server
304 * Convert a register context from the server format.
306 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
308 if (from
->cpu
!= CPU_SPARC
) return STATUS_INVALID_PARAMETER
;
310 to
->ContextFlags
= CONTEXT_SPARC
;
311 if (from
->flags
& SERVER_CTX_CONTROL
)
313 to
->ContextFlags
|= CONTEXT_CONTROL
;
314 to
->psr
= from
->ctl
.sparc_regs
.psr
;
315 to
->pc
= from
->ctl
.sparc_regs
.pc
;
316 to
->npc
= from
->ctl
.sparc_regs
.npc
;
317 to
->y
= from
->ctl
.sparc_regs
.y
;
318 to
->wim
= from
->ctl
.sparc_regs
.wim
;
319 to
->tbr
= from
->ctl
.sparc_regs
.tbr
;
321 if (from
->flags
& SERVER_CTX_INTEGER
)
323 to
->ContextFlags
|= CONTEXT_INTEGER
;
324 to
->g0
= from
->integer
.sparc_regs
.g
[0];
325 to
->g1
= from
->integer
.sparc_regs
.g
[1];
326 to
->g2
= from
->integer
.sparc_regs
.g
[2];
327 to
->g3
= from
->integer
.sparc_regs
.g
[3];
328 to
->g4
= from
->integer
.sparc_regs
.g
[4];
329 to
->g5
= from
->integer
.sparc_regs
.g
[5];
330 to
->g6
= from
->integer
.sparc_regs
.g
[6];
331 to
->g7
= from
->integer
.sparc_regs
.g
[7];
332 to
->o0
= from
->integer
.sparc_regs
.o
[0];
333 to
->o1
= from
->integer
.sparc_regs
.o
[1];
334 to
->o2
= from
->integer
.sparc_regs
.o
[2];
335 to
->o3
= from
->integer
.sparc_regs
.o
[3];
336 to
->o4
= from
->integer
.sparc_regs
.o
[4];
337 to
->o5
= from
->integer
.sparc_regs
.o
[5];
338 to
->o6
= from
->integer
.sparc_regs
.o
[6];
339 to
->o7
= from
->integer
.sparc_regs
.o
[7];
340 to
->l0
= from
->integer
.sparc_regs
.l
[0];
341 to
->l1
= from
->integer
.sparc_regs
.l
[1];
342 to
->l2
= from
->integer
.sparc_regs
.l
[2];
343 to
->l3
= from
->integer
.sparc_regs
.l
[3];
344 to
->l4
= from
->integer
.sparc_regs
.l
[4];
345 to
->l5
= from
->integer
.sparc_regs
.l
[5];
346 to
->l6
= from
->integer
.sparc_regs
.l
[6];
347 to
->l7
= from
->integer
.sparc_regs
.l
[7];
348 to
->i0
= from
->integer
.sparc_regs
.i
[0];
349 to
->i1
= from
->integer
.sparc_regs
.i
[1];
350 to
->i2
= from
->integer
.sparc_regs
.i
[2];
351 to
->i3
= from
->integer
.sparc_regs
.i
[3];
352 to
->i4
= from
->integer
.sparc_regs
.i
[4];
353 to
->i5
= from
->integer
.sparc_regs
.i
[5];
354 to
->i6
= from
->integer
.sparc_regs
.i
[6];
355 to
->i7
= from
->integer
.sparc_regs
.i
[7];
357 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
361 return STATUS_SUCCESS
;
365 /**********************************************************************
368 * Handler for SIGSEGV.
370 static void segv_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
372 EXCEPTION_RECORD rec
;
375 rec
.ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
377 /* we want the page-fault case to be fast */
378 if ( info
->si_code
== SEGV_ACCERR
)
379 if (!(rec
.ExceptionCode
= virtual_handle_fault( info
->si_addr
, 0 ))) return;
381 save_context( &context
, ucontext
);
382 rec
.ExceptionRecord
= NULL
;
383 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
384 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
385 rec
.NumberParameters
= 2;
386 rec
.ExceptionInformation
[0] = 0; /* FIXME: read/write access ? */
387 rec
.ExceptionInformation
[1] = (ULONG_PTR
)info
->si_addr
;
389 __regs_RtlRaiseException( &rec
, &context
);
390 restore_context( &context
, ucontext
);
393 /**********************************************************************
396 * Handler for SIGBUS.
398 static void bus_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
400 EXCEPTION_RECORD rec
;
403 save_context( &context
, ucontext
);
404 rec
.ExceptionRecord
= NULL
;
405 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
406 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
407 rec
.NumberParameters
= 0;
409 if ( info
->si_code
== BUS_ADRALN
)
410 rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
412 rec
.ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
414 __regs_RtlRaiseException( &rec
, &context
);
415 restore_context( &context
, ucontext
);
418 /**********************************************************************
421 * Handler for SIGILL.
423 static void ill_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
425 EXCEPTION_RECORD rec
;
428 switch ( info
->si_code
)
435 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
440 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
444 rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
448 save_context( &context
, ucontext
);
449 rec
.ExceptionRecord
= NULL
;
450 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
451 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
452 rec
.NumberParameters
= 0;
453 __regs_RtlRaiseException( &rec
, &context
);
454 restore_context( &context
, ucontext
);
458 /**********************************************************************
461 * Handler for SIGTRAP.
463 static void trap_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
465 EXCEPTION_RECORD rec
;
468 switch ( info
->si_code
)
471 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
475 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
479 save_context( &context
, ucontext
);
480 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
481 rec
.ExceptionRecord
= NULL
;
482 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
483 rec
.NumberParameters
= 0;
484 __regs_RtlRaiseException( &rec
, &context
);
485 restore_context( &context
, ucontext
);
489 /**********************************************************************
492 * Handler for SIGFPE.
494 static void fpe_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
496 EXCEPTION_RECORD rec
;
499 switch ( info
->si_code
)
502 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
505 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
508 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
511 rec
.ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
514 rec
.ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
517 rec
.ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
520 rec
.ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
524 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
528 save_context( &context
, ucontext
);
529 save_fpu( &context
, ucontext
);
530 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
531 rec
.ExceptionRecord
= NULL
;
532 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
533 rec
.NumberParameters
= 0;
534 __regs_RtlRaiseException( &rec
, &context
);
535 restore_context( &context
, ucontext
);
536 restore_fpu( &context
, ucontext
);
540 /**********************************************************************
543 * Handler for SIGINT.
545 static void int_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
547 if (!dispatch_signal(SIGINT
))
549 EXCEPTION_RECORD rec
;
552 save_context( &context
, ucontext
);
553 rec
.ExceptionCode
= CONTROL_C_EXIT
;
554 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
555 rec
.ExceptionRecord
= NULL
;
556 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
557 rec
.NumberParameters
= 0;
558 __regs_RtlRaiseException( &rec
, &context
);
559 restore_context( &context
, ucontext
);
563 /**********************************************************************
566 * Handler for SIGABRT.
568 static HANDLER_DEF(abrt_handler
)
570 EXCEPTION_RECORD rec
;
573 save_context( &context
, HANDLER_CONTEXT
);
574 rec
.ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
575 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
576 rec
.ExceptionRecord
= NULL
;
577 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
578 rec
.NumberParameters
= 0;
579 __regs_RtlRaiseException( &rec
, &context
); /* Should never return.. */
580 restore_context( &context
, HANDLER_CONTEXT
);
584 /**********************************************************************
587 * Handler for SIGQUIT.
589 static HANDLER_DEF(quit_handler
)
595 /**********************************************************************
598 * Handler for SIGUSR1, used to signal a thread that it got suspended.
600 static HANDLER_DEF(usr1_handler
)
604 save_context( &context
, HANDLER_CONTEXT
);
605 wait_suspend( &context
);
606 restore_context( &context
, HANDLER_CONTEXT
);
610 /**********************************************************************
611 * get_signal_stack_total_size
613 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
614 * Must be a power of two.
616 size_t get_signal_stack_total_size(void)
618 assert( sizeof(TEB
) <= getpagesize() );
619 return getpagesize(); /* this is just for the TEB, we don't need a signal stack */
623 /***********************************************************************
626 * Set a signal handler
628 static int set_handler( int sig
, void (*func
)() )
630 struct sigaction sig_act
;
632 sig_act
.sa_sigaction
= func
;
633 sig_act
.sa_mask
= server_block_set
;
634 sig_act
.sa_flags
= SA_SIGINFO
;
636 return sigaction( sig
, &sig_act
, NULL
);
640 /***********************************************************************
641 * __wine_set_signal_handler (NTDLL.@)
643 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
645 if (sig
> sizeof(handlers
) / sizeof(handlers
[0])) return -1;
646 if (handlers
[sig
] != NULL
) return -2;
652 /**********************************************************************
655 void signal_init_thread( TEB
*teb
)
657 static int init_done
;
661 pthread_key_create( &teb_key
, NULL
);
664 pthread_setspecific( teb_key
, teb
);
668 /**********************************************************************
669 * signal_init_process
671 void signal_init_process(void)
673 if (set_handler( SIGINT
, (void (*)())int_handler
) == -1) goto error
;
674 if (set_handler( SIGFPE
, (void (*)())fpe_handler
) == -1) goto error
;
675 if (set_handler( SIGSEGV
, (void (*)())segv_handler
) == -1) goto error
;
676 if (set_handler( SIGILL
, (void (*)())ill_handler
) == -1) goto error
;
677 if (set_handler( SIGBUS
, (void (*)())bus_handler
) == -1) goto error
;
678 if (set_handler( SIGTRAP
, (void (*)())trap_handler
) == -1) goto error
;
679 if (set_handler( SIGABRT
, (void (*)())abrt_handler
) == -1) goto error
;
680 if (set_handler( SIGQUIT
, (void (*)())quit_handler
) == -1) goto error
;
681 if (set_handler( SIGUSR1
, (void (*)())usr1_handler
) == -1) goto error
;
682 /* 'ta 6' tells the kernel to synthesize any unaligned accesses this
683 process makes, instead of just signalling an error and terminating
684 the process. wine-devel did not reach a conclusion on whether
685 this is correct, because that is what x86 does, or it is harmful
686 because it could obscure problems in user code */
687 asm("ta 6"); /* 6 == ST_FIX_ALIGN defined in sys/trap.h */
688 signal_init_thread();
697 /**********************************************************************
700 void __wine_enter_vm86( CONTEXT
*context
)
702 MESSAGE("vm86 mode not supported on this platform\n");
705 /**********************************************************************
706 * DbgBreakPoint (NTDLL.@)
708 void WINAPI
DbgBreakPoint(void)
710 kill(getpid(), SIGTRAP
);
713 /**********************************************************************
714 * DbgUserBreakPoint (NTDLL.@)
716 void WINAPI
DbgUserBreakPoint(void)
718 kill(getpid(), SIGTRAP
);
721 /**********************************************************************
722 * NtCurrentTeb (NTDLL.@)
724 TEB
* WINAPI
NtCurrentTeb(void)
726 return pthread_getspecific( teb_key
);
729 #endif /* __sparc__ */