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
32 #include <sys/ucontext.h>
40 #include "wine/exception.h"
42 #include "ntdll_misc.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
48 #define HANDLER_DEF(name) void name( int __signal, struct siginfo *__siginfo, ucontext_t *__context )
49 #define HANDLER_CONTEXT (__context)
51 typedef int (*wine_signal_handler
)(unsigned int sig
);
53 static wine_signal_handler handlers
[256];
55 extern void WINAPI
EXC_RtlRaiseException( PEXCEPTION_RECORD
, PCONTEXT
);
57 /***********************************************************************
60 inline static int dispatch_signal(unsigned int sig
)
62 if (handlers
[sig
] == NULL
) return 0;
63 return handlers
[sig
](sig
);
68 * FIXME: All this works only on Solaris for now
71 /**********************************************************************
74 static void save_context( CONTEXT
*context
, ucontext_t
*ucontext
)
76 /* Special registers */
77 context
->psr
= ucontext
->uc_mcontext
.gregs
[REG_PSR
];
78 context
->pc
= ucontext
->uc_mcontext
.gregs
[REG_PC
];
79 context
->npc
= ucontext
->uc_mcontext
.gregs
[REG_nPC
];
80 context
->y
= ucontext
->uc_mcontext
.gregs
[REG_Y
];
81 context
->wim
= 0; /* FIXME */
82 context
->tbr
= 0; /* FIXME */
84 /* Global registers */
85 context
->g0
= 0; /* always */
86 context
->g1
= ucontext
->uc_mcontext
.gregs
[REG_G1
];
87 context
->g2
= ucontext
->uc_mcontext
.gregs
[REG_G2
];
88 context
->g3
= ucontext
->uc_mcontext
.gregs
[REG_G3
];
89 context
->g4
= ucontext
->uc_mcontext
.gregs
[REG_G4
];
90 context
->g5
= ucontext
->uc_mcontext
.gregs
[REG_G5
];
91 context
->g6
= ucontext
->uc_mcontext
.gregs
[REG_G6
];
92 context
->g7
= ucontext
->uc_mcontext
.gregs
[REG_G7
];
94 /* Current 'out' registers */
95 context
->o0
= ucontext
->uc_mcontext
.gregs
[REG_O0
];
96 context
->o1
= ucontext
->uc_mcontext
.gregs
[REG_O1
];
97 context
->o2
= ucontext
->uc_mcontext
.gregs
[REG_O2
];
98 context
->o3
= ucontext
->uc_mcontext
.gregs
[REG_O3
];
99 context
->o4
= ucontext
->uc_mcontext
.gregs
[REG_O4
];
100 context
->o5
= ucontext
->uc_mcontext
.gregs
[REG_O5
];
101 context
->o6
= ucontext
->uc_mcontext
.gregs
[REG_O6
];
102 context
->o7
= ucontext
->uc_mcontext
.gregs
[REG_O7
];
104 /* FIXME: what if the current register window isn't saved? */
105 if ( ucontext
->uc_mcontext
.gwins
&& ucontext
->uc_mcontext
.gwins
->wbcnt
> 0 )
107 /* Current 'local' registers from first register window */
108 context
->l0
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[0];
109 context
->l1
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[1];
110 context
->l2
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[2];
111 context
->l3
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[3];
112 context
->l4
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[4];
113 context
->l5
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[5];
114 context
->l6
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[6];
115 context
->l7
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[7];
117 /* Current 'in' registers from first register window */
118 context
->i0
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[0];
119 context
->i1
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[1];
120 context
->i2
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[2];
121 context
->i3
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[3];
122 context
->i4
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[4];
123 context
->i5
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[5];
124 context
->i6
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[6];
125 context
->i7
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[7];
129 /**********************************************************************
132 static void restore_context( CONTEXT
*context
, ucontext_t
*ucontext
)
137 /**********************************************************************
140 static void save_fpu( CONTEXT
*context
, ucontext_t
*ucontext
)
145 /**********************************************************************
148 static void restore_fpu( CONTEXT
*context
, ucontext_t
*ucontext
)
154 /**********************************************************************
157 * Handler for SIGSEGV.
159 static void segv_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
161 EXCEPTION_RECORD rec
;
164 /* we want the page-fault case to be fast */
165 if ( info
->si_code
== SEGV_ACCERR
)
166 if (VIRTUAL_HandleFault( (LPVOID
)info
->si_addr
)) return;
168 save_context( &context
, ucontext
);
169 rec
.ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
170 rec
.ExceptionRecord
= NULL
;
171 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
172 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
173 rec
.NumberParameters
= 2;
174 rec
.ExceptionInformation
[0] = 0; /* FIXME: read/write access ? */
175 rec
.ExceptionInformation
[1] = (DWORD
)info
->si_addr
;
177 EXC_RtlRaiseException( &rec
, &context
);
178 restore_context( &context
, ucontext
);
181 /**********************************************************************
184 * Handler for SIGBUS.
186 static void bus_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
188 EXCEPTION_RECORD rec
;
191 save_context( &context
, ucontext
);
192 rec
.ExceptionRecord
= NULL
;
193 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
194 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
195 rec
.NumberParameters
= 0;
197 if ( info
->si_code
== BUS_ADRALN
)
198 rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
200 rec
.ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
202 EXC_RtlRaiseException( &rec
, &context
);
203 restore_context( &context
, ucontext
);
206 /**********************************************************************
209 * Handler for SIGILL.
211 static void ill_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
213 EXCEPTION_RECORD rec
;
216 switch ( info
->si_code
)
223 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
228 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
232 rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
236 save_context( &context
, ucontext
);
237 rec
.ExceptionRecord
= NULL
;
238 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
239 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
240 rec
.NumberParameters
= 0;
241 EXC_RtlRaiseException( &rec
, &context
);
242 restore_context( &context
, ucontext
);
246 /**********************************************************************
249 * Handler for SIGTRAP.
251 static void trap_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
253 EXCEPTION_RECORD rec
;
256 switch ( info
->si_code
)
259 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
263 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
267 save_context( &context
, ucontext
);
268 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
269 rec
.ExceptionRecord
= NULL
;
270 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
271 rec
.NumberParameters
= 0;
272 EXC_RtlRaiseException( &rec
, &context
);
273 restore_context( &context
, ucontext
);
277 /**********************************************************************
280 * Handler for SIGFPE.
282 static void fpe_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
284 EXCEPTION_RECORD rec
;
287 switch ( info
->si_code
)
290 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
293 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
296 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
299 rec
.ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
302 rec
.ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
305 rec
.ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
308 rec
.ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
312 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
316 save_context( &context
, ucontext
);
317 save_fpu( &context
, ucontext
);
318 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
319 rec
.ExceptionRecord
= NULL
;
320 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
321 rec
.NumberParameters
= 0;
322 EXC_RtlRaiseException( &rec
, &context
);
323 restore_context( &context
, ucontext
);
324 restore_fpu( &context
, ucontext
);
328 /**********************************************************************
331 * Handler for SIGINT.
333 static void int_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
335 if (!dispatch_signal(SIGINT
))
337 EXCEPTION_RECORD rec
;
340 save_context( &context
, ucontext
);
341 rec
.ExceptionCode
= CONTROL_C_EXIT
;
342 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
343 rec
.ExceptionRecord
= NULL
;
344 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
345 rec
.NumberParameters
= 0;
346 EXC_RtlRaiseException( &rec
, &context
);
347 restore_context( &context
, ucontext
);
351 /**********************************************************************
354 * Handler for SIGABRT.
356 static HANDLER_DEF(abrt_handler
)
358 EXCEPTION_RECORD rec
;
361 save_context( &context
, HANDLER_CONTEXT
);
362 rec
.ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
363 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
364 rec
.ExceptionRecord
= NULL
;
365 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
366 rec
.NumberParameters
= 0;
367 EXC_RtlRaiseException( &rec
, &context
); /* Should never return.. */
368 restore_context( &context
, HANDLER_CONTEXT
);
372 /**********************************************************************
375 * Handler for SIGTERM.
377 static HANDLER_DEF(term_handler
)
379 SYSDEPS_AbortThread(0);
383 /**********************************************************************
386 * Handler for SIGUSR1, used to signal a thread that it got suspended.
388 static HANDLER_DEF(usr1_handler
)
390 LARGE_INTEGER timeout
;
392 /* wait with 0 timeout, will only return once the thread is no longer suspended */
393 timeout
.QuadPart
= 0;
394 NTDLL_wait_for_multiple_objects( 0, NULL
, 0, &timeout
);
398 /***********************************************************************
401 * Set a signal handler
403 static int set_handler( int sig
, void (*func
)() )
405 struct sigaction sig_act
;
407 sig_act
.sa_handler
= NULL
;
408 sig_act
.sa_sigaction
= func
;
409 sigemptyset( &sig_act
.sa_mask
);
410 sig_act
.sa_flags
= SA_SIGINFO
;
412 return sigaction( sig
, &sig_act
, NULL
);
416 /***********************************************************************
417 * __wine_set_signal_handler (NTDLL.@)
419 int __wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
421 if (sig
> sizeof(handlers
) / sizeof(handlers
[0])) return -1;
422 if (handlers
[sig
] != NULL
) return -2;
428 /**********************************************************************
431 BOOL
SIGNAL_Init(void)
433 if (set_handler( SIGINT
, (void (*)())int_handler
) == -1) goto error
;
434 if (set_handler( SIGFPE
, (void (*)())fpe_handler
) == -1) goto error
;
435 if (set_handler( SIGSEGV
, (void (*)())segv_handler
) == -1) goto error
;
436 if (set_handler( SIGILL
, (void (*)())ill_handler
) == -1) goto error
;
437 if (set_handler( SIGBUS
, (void (*)())bus_handler
) == -1) goto error
;
438 if (set_handler( SIGTRAP
, (void (*)())trap_handler
) == -1) goto error
;
439 if (set_handler( SIGABRT
, (void (*)())abrt_handler
) == -1) goto error
;
440 if (set_handler( SIGTERM
, (void (*)())term_handler
) == -1) goto error
;
441 if (set_handler( SIGUSR1
, (void (*)())usr1_handler
) == -1) goto error
;
450 /**********************************************************************
453 * Block the async signals.
455 void SIGNAL_Block(void)
459 sigemptyset( &block_set
);
460 sigaddset( &block_set
, SIGALRM
);
461 sigaddset( &block_set
, SIGIO
);
462 sigaddset( &block_set
, SIGHUP
);
463 sigaddset( &block_set
, SIGUSR1
);
464 sigaddset( &block_set
, SIGUSR2
);
465 sigprocmask( SIG_BLOCK
, &block_set
, NULL
);
469 /***********************************************************************
472 * Unblock signals. Called from EXC_RtlRaiseException.
474 void SIGNAL_Unblock(void)
478 sigfillset( &all_sigs
);
479 sigprocmask( SIG_UNBLOCK
, &all_sigs
, NULL
);
483 /**********************************************************************
486 * Restore the default handlers.
488 void SIGNAL_Reset(void)
490 signal( SIGINT
, SIG_DFL
);
491 signal( SIGFPE
, SIG_DFL
);
492 signal( SIGSEGV
, SIG_DFL
);
493 signal( SIGILL
, SIG_DFL
);
494 signal( SIGBUS
, SIG_DFL
);
495 signal( SIGTRAP
, SIG_DFL
);
496 signal( SIGABRT
, SIG_DFL
);
497 signal( SIGTERM
, SIG_DFL
);
501 /**********************************************************************
504 void __wine_enter_vm86( CONTEXT
*context
)
506 MESSAGE("vm86 mode not supported on this platform\n");
509 /**********************************************************************
510 * DbgBreakPoint (NTDLL.@)
512 void WINAPI
DbgBreakPoint(void)
514 kill(getpid(), SIGTRAP
);
517 /**********************************************************************
518 * DbgUserBreakPoint (NTDLL.@)
520 void WINAPI
DbgUserBreakPoint(void)
522 kill(getpid(), SIGTRAP
);
525 #endif /* __sparc__ */