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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <sys/ucontext.h>
37 #include "wine/exception.h"
39 #include "stackframe.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
45 #define HANDLER_DEF(name) void name( int __signal, struct siginfo *__siginfo, ucontext_t *__context )
46 #define HANDLER_CONTEXT (__context)
48 typedef int (*wine_signal_handler
)(unsigned int sig
);
50 static wine_signal_handler handlers
[256];
52 extern void WINAPI
EXC_RtlRaiseException( PEXCEPTION_RECORD
, PCONTEXT
);
54 /***********************************************************************
57 inline static int dispatch_signal(unsigned int sig
)
59 if (handlers
[sig
] == NULL
) return 0;
60 return handlers
[sig
](sig
);
65 * FIXME: All this works only on Solaris for now
68 /**********************************************************************
71 static void save_context( CONTEXT
*context
, ucontext_t
*ucontext
)
73 /* Special registers */
74 context
->psr
= ucontext
->uc_mcontext
.gregs
[REG_PSR
];
75 context
->pc
= ucontext
->uc_mcontext
.gregs
[REG_PC
];
76 context
->npc
= ucontext
->uc_mcontext
.gregs
[REG_nPC
];
77 context
->y
= ucontext
->uc_mcontext
.gregs
[REG_Y
];
78 context
->wim
= 0; /* FIXME */
79 context
->tbr
= 0; /* FIXME */
81 /* Global registers */
82 context
->g0
= 0; /* always */
83 context
->g1
= ucontext
->uc_mcontext
.gregs
[REG_G1
];
84 context
->g2
= ucontext
->uc_mcontext
.gregs
[REG_G2
];
85 context
->g3
= ucontext
->uc_mcontext
.gregs
[REG_G3
];
86 context
->g4
= ucontext
->uc_mcontext
.gregs
[REG_G4
];
87 context
->g5
= ucontext
->uc_mcontext
.gregs
[REG_G5
];
88 context
->g6
= ucontext
->uc_mcontext
.gregs
[REG_G6
];
89 context
->g7
= ucontext
->uc_mcontext
.gregs
[REG_G7
];
91 /* Current 'out' registers */
92 context
->o0
= ucontext
->uc_mcontext
.gregs
[REG_O0
];
93 context
->o1
= ucontext
->uc_mcontext
.gregs
[REG_O1
];
94 context
->o2
= ucontext
->uc_mcontext
.gregs
[REG_O2
];
95 context
->o3
= ucontext
->uc_mcontext
.gregs
[REG_O3
];
96 context
->o4
= ucontext
->uc_mcontext
.gregs
[REG_O4
];
97 context
->o5
= ucontext
->uc_mcontext
.gregs
[REG_O5
];
98 context
->o6
= ucontext
->uc_mcontext
.gregs
[REG_O6
];
99 context
->o7
= ucontext
->uc_mcontext
.gregs
[REG_O7
];
101 /* FIXME: what if the current register window isn't saved? */
102 if ( ucontext
->uc_mcontext
.gwins
&& ucontext
->uc_mcontext
.gwins
->wbcnt
> 0 )
104 /* Current 'local' registers from first register window */
105 context
->l0
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[0];
106 context
->l1
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[1];
107 context
->l2
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[2];
108 context
->l3
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[3];
109 context
->l4
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[4];
110 context
->l5
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[5];
111 context
->l6
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[6];
112 context
->l7
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[7];
114 /* Current 'in' registers from first register window */
115 context
->i0
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[0];
116 context
->i1
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[1];
117 context
->i2
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[2];
118 context
->i3
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[3];
119 context
->i4
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[4];
120 context
->i5
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[5];
121 context
->i6
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[6];
122 context
->i7
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[7];
126 /**********************************************************************
129 static void restore_context( CONTEXT
*context
, ucontext_t
*ucontext
)
134 /**********************************************************************
137 static void save_fpu( CONTEXT
*context
, ucontext_t
*ucontext
)
142 /**********************************************************************
145 static void restore_fpu( CONTEXT
*context
, ucontext_t
*ucontext
)
151 /**********************************************************************
154 * Handler for SIGSEGV.
156 static void segv_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
158 EXCEPTION_RECORD rec
;
161 /* we want the page-fault case to be fast */
162 if ( info
->si_code
== SEGV_ACCERR
)
163 if (VIRTUAL_HandleFault( (LPVOID
)info
->si_addr
)) return;
165 save_context( &context
, ucontext
);
166 rec
.ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
167 rec
.ExceptionRecord
= NULL
;
168 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
169 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
170 rec
.NumberParameters
= 2;
171 rec
.ExceptionInformation
[0] = 0; /* FIXME: read/write access ? */
172 rec
.ExceptionInformation
[1] = (DWORD
)info
->si_addr
;
174 EXC_RtlRaiseException( &rec
, &context
);
175 restore_context( &context
, ucontext
);
178 /**********************************************************************
181 * Handler for SIGBUS.
183 static void bus_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
185 EXCEPTION_RECORD rec
;
188 save_context( &context
, ucontext
);
189 rec
.ExceptionRecord
= NULL
;
190 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
191 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
192 rec
.NumberParameters
= 0;
194 if ( info
->si_code
== BUS_ADRALN
)
195 rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
197 rec
.ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
199 EXC_RtlRaiseException( &rec
, &context
);
200 restore_context( &context
, ucontext
);
203 /**********************************************************************
206 * Handler for SIGILL.
208 static void ill_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
210 EXCEPTION_RECORD rec
;
213 switch ( info
->si_code
)
220 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
225 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
229 rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
233 save_context( &context
, ucontext
);
234 rec
.ExceptionRecord
= NULL
;
235 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
236 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
237 rec
.NumberParameters
= 0;
238 EXC_RtlRaiseException( &rec
, &context
);
239 restore_context( &context
, ucontext
);
243 /**********************************************************************
246 * Handler for SIGTRAP.
248 static void trap_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
250 EXCEPTION_RECORD rec
;
253 switch ( info
->si_code
)
256 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
260 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
264 save_context( &context
, ucontext
);
265 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
266 rec
.ExceptionRecord
= NULL
;
267 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
268 rec
.NumberParameters
= 0;
269 EXC_RtlRaiseException( &rec
, &context
);
270 restore_context( &context
, ucontext
);
274 /**********************************************************************
277 * Handler for SIGFPE.
279 static void fpe_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
281 EXCEPTION_RECORD rec
;
284 switch ( info
->si_code
)
287 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
290 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
293 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
296 rec
.ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
299 rec
.ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
302 rec
.ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
305 rec
.ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
309 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
313 save_context( &context
, ucontext
);
314 save_fpu( &context
, ucontext
);
315 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
316 rec
.ExceptionRecord
= NULL
;
317 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
318 rec
.NumberParameters
= 0;
319 EXC_RtlRaiseException( &rec
, &context
);
320 restore_context( &context
, ucontext
);
321 restore_fpu( &context
, ucontext
);
325 /**********************************************************************
328 * Handler for SIGINT.
330 static void int_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
332 if (!dispatch_signal(SIGINT
))
334 EXCEPTION_RECORD rec
;
337 save_context( &context
, ucontext
);
338 rec
.ExceptionCode
= CONTROL_C_EXIT
;
339 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
340 rec
.ExceptionRecord
= NULL
;
341 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
342 rec
.NumberParameters
= 0;
343 EXC_RtlRaiseException( &rec
, &context
);
344 restore_context( &context
, ucontext
);
348 /**********************************************************************
351 * Handler for SIGABRT.
353 static HANDLER_DEF(abrt_handler
)
355 EXCEPTION_RECORD rec
;
358 save_context( &context
, HANDLER_CONTEXT
);
359 rec
.ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
360 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
361 rec
.ExceptionRecord
= NULL
;
362 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
363 rec
.NumberParameters
= 0;
364 EXC_RtlRaiseException( &rec
, &context
); /* Should never return.. */
365 restore_context( &context
, HANDLER_CONTEXT
);
369 /**********************************************************************
372 * Handler for SIGTERM.
374 static HANDLER_DEF(term_handler
)
376 SYSDEPS_AbortThread(0);
380 /**********************************************************************
383 * Handler for SIGUSR1, used to signal a thread that it got suspended.
385 static HANDLER_DEF(usr1_handler
)
387 LARGE_INTEGER timeout
;
389 /* wait with 0 timeout, will only return once the thread is no longer suspended */
390 timeout
.QuadPart
= 0;
391 NtWaitForMultipleObjects( 0, NULL
, FALSE
, FALSE
, &timeout
);
395 /***********************************************************************
398 * Set a signal handler
400 static int set_handler( int sig
, void (*func
)() )
402 struct sigaction sig_act
;
404 sig_act
.sa_handler
= NULL
;
405 sig_act
.sa_sigaction
= func
;
406 sigemptyset( &sig_act
.sa_mask
);
407 sig_act
.sa_flags
= SA_SIGINFO
;
409 return sigaction( sig
, &sig_act
, NULL
);
413 /***********************************************************************
414 * __wine_set_signal_handler (NTDLL.@)
416 int __wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
418 if (sig
> sizeof(handlers
) / sizeof(handlers
[0])) return -1;
419 if (handlers
[sig
] != NULL
) return -2;
425 /**********************************************************************
428 BOOL
SIGNAL_Init(void)
430 sigfillset( &all_sigs
);
432 if (set_handler( SIGINT
, (void (*)())int_handler
) == -1) goto error
;
433 if (set_handler( SIGFPE
, (void (*)())fpe_handler
) == -1) goto error
;
434 if (set_handler( SIGSEGV
, (void (*)())segv_handler
) == -1) goto error
;
435 if (set_handler( SIGILL
, (void (*)())ill_handler
) == -1) goto error
;
436 if (set_handler( SIGBUS
, (void (*)())bus_handler
) == -1) goto error
;
437 if (set_handler( SIGTRAP
, (void (*)())trap_handler
) == -1) goto error
;
438 if (set_handler( SIGABRT
, (void (*)())abrt_handler
) == -1) goto error
;
439 if (set_handler( SIGTERM
, (void (*)())term_handler
) == -1) goto error
;
440 if (set_handler( SIGUSR1
, (void (*)())usr1_handler
) == -1) goto error
;
449 /**********************************************************************
452 * Block the async signals.
454 void SIGNAL_Block(void)
458 sigemptyset( &block_set
);
459 sigaddset( &block_set
, SIGALRM
);
460 sigaddset( &block_set
, SIGIO
);
461 sigaddset( &block_set
, SIGHUP
);
462 sigaddset( &block_set
, SIGUSR1
);
463 sigaddset( &block_set
, SIGUSR2
);
464 sigprocmask( SIG_BLOCK
, &block_set
, NULL
);
468 /***********************************************************************
471 * Unblock signals. Called from EXC_RtlRaiseException.
473 void SIGNAL_Unblock(void)
477 sigfillset( &all_sigs
);
478 sigprocmask( SIG_UNBLOCK
, &all_sigs
, NULL
);
482 /**********************************************************************
485 * Restore the default handlers.
487 void SIGNAL_Reset(void)
489 signal( SIGINT
, SIG_DFL
);
490 signal( SIGFPE
, SIG_DFL
);
491 signal( SIGSEGV
, SIG_DFL
);
492 signal( SIGILL
, SIG_DFL
);
493 signal( SIGBUS
, SIG_DFL
);
494 signal( SIGTRAP
, SIG_DFL
);
495 signal( SIGABRT
, SIG_DFL
);
496 signal( SIGTERM
, SIG_DFL
);
500 /**********************************************************************
503 void __wine_enter_vm86( CONTEXT
*context
)
505 MESSAGE("vm86 mode not supported on this platform\n");
508 /**********************************************************************
509 * DbgBreakPoint (NTDLL.@)
511 void WINAPI
DbgBreakPoint(void)
513 kill(getpid(), SIGTRAP
);
516 /**********************************************************************
517 * DbgUserBreakPoint (NTDLL.@)
519 void WINAPI
DbgUserBreakPoint(void)
521 kill(getpid(), SIGTRAP
);
524 #endif /* __sparc__ */