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
29 #include <sys/ucontext.h>
35 #include "wine/exception.h"
37 #include "stackframe.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
43 typedef int (*wine_signal_handler
)(unsigned int sig
);
45 static wine_signal_handler handlers
[256];
47 static sigset_t all_sigs
;
50 /***********************************************************************
53 inline static int dispatch_signal(unsigned int sig
)
55 if (handlers
[sig
] == NULL
) return 0;
56 return handlers
[sig
](sig
);
61 * FIXME: All this works only on Solaris for now
64 /**********************************************************************
67 static void save_context( CONTEXT
*context
, ucontext_t
*ucontext
)
69 /* Special registers */
70 context
->psr
= ucontext
->uc_mcontext
.gregs
[REG_PSR
];
71 context
->pc
= ucontext
->uc_mcontext
.gregs
[REG_PC
];
72 context
->npc
= ucontext
->uc_mcontext
.gregs
[REG_nPC
];
73 context
->y
= ucontext
->uc_mcontext
.gregs
[REG_Y
];
74 context
->wim
= 0; /* FIXME */
75 context
->tbr
= 0; /* FIXME */
77 /* Global registers */
78 context
->g0
= 0; /* always */
79 context
->g1
= ucontext
->uc_mcontext
.gregs
[REG_G1
];
80 context
->g2
= ucontext
->uc_mcontext
.gregs
[REG_G2
];
81 context
->g3
= ucontext
->uc_mcontext
.gregs
[REG_G3
];
82 context
->g4
= ucontext
->uc_mcontext
.gregs
[REG_G4
];
83 context
->g5
= ucontext
->uc_mcontext
.gregs
[REG_G5
];
84 context
->g6
= ucontext
->uc_mcontext
.gregs
[REG_G6
];
85 context
->g7
= ucontext
->uc_mcontext
.gregs
[REG_G7
];
87 /* Current 'out' registers */
88 context
->o0
= ucontext
->uc_mcontext
.gregs
[REG_O0
];
89 context
->o1
= ucontext
->uc_mcontext
.gregs
[REG_O1
];
90 context
->o2
= ucontext
->uc_mcontext
.gregs
[REG_O2
];
91 context
->o3
= ucontext
->uc_mcontext
.gregs
[REG_O3
];
92 context
->o4
= ucontext
->uc_mcontext
.gregs
[REG_O4
];
93 context
->o5
= ucontext
->uc_mcontext
.gregs
[REG_O5
];
94 context
->o6
= ucontext
->uc_mcontext
.gregs
[REG_O6
];
95 context
->o7
= ucontext
->uc_mcontext
.gregs
[REG_O7
];
97 /* FIXME: what if the current register window isn't saved? */
98 if ( ucontext
->uc_mcontext
.gwins
&& ucontext
->uc_mcontext
.gwins
->wbcnt
> 0 )
100 /* Current 'local' registers from first register window */
101 context
->l0
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[0];
102 context
->l1
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[1];
103 context
->l2
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[2];
104 context
->l3
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[3];
105 context
->l4
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[4];
106 context
->l5
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[5];
107 context
->l6
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[6];
108 context
->l7
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[7];
110 /* Current 'in' registers from first register window */
111 context
->i0
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[0];
112 context
->i1
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[1];
113 context
->i2
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[2];
114 context
->i3
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[3];
115 context
->i4
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[4];
116 context
->i5
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[5];
117 context
->i6
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[6];
118 context
->i7
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[7];
122 /**********************************************************************
125 static void restore_context( CONTEXT
*context
, ucontext_t
*ucontext
)
130 /**********************************************************************
133 static void save_fpu( CONTEXT
*context
, ucontext_t
*ucontext
)
138 /**********************************************************************
141 static void restore_fpu( CONTEXT
*context
, ucontext_t
*ucontext
)
147 /**********************************************************************
150 * Handler for SIGSEGV.
152 static void segv_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
154 EXCEPTION_RECORD rec
;
157 /* we want the page-fault case to be fast */
158 if ( info
->si_code
== SEGV_ACCERR
)
159 if (VIRTUAL_HandleFault( (LPVOID
)info
->si_addr
)) return;
161 save_context( &context
, ucontext
);
162 rec
.ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
163 rec
.ExceptionRecord
= NULL
;
164 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
165 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
166 rec
.NumberParameters
= 2;
167 rec
.ExceptionInformation
[0] = 0; /* FIXME: read/write access ? */
168 rec
.ExceptionInformation
[1] = (DWORD
)info
->si_addr
;
170 EXC_RtlRaiseException( &rec
, &context
);
171 restore_context( &context
, ucontext
);
174 /**********************************************************************
177 * Handler for SIGBUS.
179 static void bus_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
181 EXCEPTION_RECORD rec
;
184 save_context( &context
, ucontext
);
185 rec
.ExceptionRecord
= NULL
;
186 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
187 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
188 rec
.NumberParameters
= 0;
190 if ( info
->si_code
== BUS_ADRALN
)
191 rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
193 rec
.ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
195 EXC_RtlRaiseException( &rec
, &context
);
196 restore_context( &context
, ucontext
);
199 /**********************************************************************
202 * Handler for SIGILL.
204 static void ill_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
206 EXCEPTION_RECORD rec
;
209 switch ( info
->si_code
)
216 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
221 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
225 rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
229 save_context( &context
, ucontext
);
230 rec
.ExceptionRecord
= NULL
;
231 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
232 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
233 rec
.NumberParameters
= 0;
234 EXC_RtlRaiseException( &rec
, &context
);
235 restore_context( &context
, ucontext
);
239 /**********************************************************************
242 * Handler for SIGTRAP.
244 static void trap_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
246 EXCEPTION_RECORD rec
;
249 switch ( info
->si_code
)
252 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
256 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
260 save_context( &context
, ucontext
);
261 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
262 rec
.ExceptionRecord
= NULL
;
263 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
264 rec
.NumberParameters
= 0;
265 EXC_RtlRaiseException( &rec
, &context
);
266 restore_context( &context
, ucontext
);
270 /**********************************************************************
273 * Handler for SIGFPE.
275 static void fpe_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
277 EXCEPTION_RECORD rec
;
280 switch ( info
->si_code
)
283 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
286 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
289 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
292 rec
.ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
295 rec
.ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
298 rec
.ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
301 rec
.ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
305 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
309 save_context( &context
, ucontext
);
310 save_fpu( &context
, ucontext
);
311 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
312 rec
.ExceptionRecord
= NULL
;
313 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
314 rec
.NumberParameters
= 0;
315 EXC_RtlRaiseException( &rec
, &context
);
316 restore_context( &context
, ucontext
);
317 restore_fpu( &context
, ucontext
);
321 /**********************************************************************
324 * Handler for SIGINT.
326 static void int_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
328 if (!dispatch_signal(SIGINT
))
330 EXCEPTION_RECORD rec
;
333 save_context( &context
, ucontext
);
334 rec
.ExceptionCode
= CONTROL_C_EXIT
;
335 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
336 rec
.ExceptionRecord
= NULL
;
337 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
338 rec
.NumberParameters
= 0;
339 EXC_RtlRaiseException( &rec
, &context
);
340 restore_context( &context
, ucontext
);
345 /***********************************************************************
348 * Set a signal handler
350 static int set_handler( int sig
, void (*func
)() )
352 struct sigaction sig_act
;
354 sig_act
.sa_handler
= NULL
;
355 sig_act
.sa_sigaction
= func
;
356 sigemptyset( &sig_act
.sa_mask
);
357 sig_act
.sa_flags
= SA_SIGINFO
;
359 return sigaction( sig
, &sig_act
, NULL
);
363 /***********************************************************************
364 * __wine_set_signal_handler (NTDLL.@)
366 int __wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
368 if (sig
> sizeof(handlers
) / sizeof(handlers
[0])) return -1;
369 if (handlers
[sig
] != NULL
) return -2;
375 /***********************************************************************
378 * Unblock signals. Called from EXC_RtlRaiseException.
380 void SIGNAL_Unblock( void )
382 sigprocmask( SIG_UNBLOCK
, &all_sigs
, NULL
);
386 /**********************************************************************
389 BOOL
SIGNAL_Init(void)
391 sigfillset( &all_sigs
);
393 if (set_handler( SIGINT
, (void (*)())int_handler
) == -1) goto error
;
394 if (set_handler( SIGFPE
, (void (*)())fpe_handler
) == -1) goto error
;
395 if (set_handler( SIGSEGV
, (void (*)())segv_handler
) == -1) goto error
;
396 if (set_handler( SIGILL
, (void (*)())ill_handler
) == -1) goto error
;
397 if (set_handler( SIGBUS
, (void (*)())bus_handler
) == -1) goto error
;
398 if (set_handler( SIGTRAP
, (void (*)())trap_handler
) == -1) goto error
;
407 /**********************************************************************
410 void SIGNAL_Reset(void)
414 /* block the async signals */
415 sigemptyset( &block_set
);
416 sigaddset( &block_set
, SIGALRM
);
417 sigaddset( &block_set
, SIGIO
);
418 sigaddset( &block_set
, SIGHUP
);
419 sigaddset( &block_set
, SIGUSR2
);
420 sigprocmask( SIG_BLOCK
, &block_set
, NULL
);
422 /* restore default handlers */
423 signal( SIGINT
, SIG_DFL
);
424 signal( SIGFPE
, SIG_DFL
);
425 signal( SIGSEGV
, SIG_DFL
);
426 signal( SIGILL
, SIG_DFL
);
427 signal( SIGBUS
, SIG_DFL
);
428 signal( SIGTRAP
, SIG_DFL
);
432 /**********************************************************************
435 void __wine_enter_vm86( CONTEXT
*context
)
437 MESSAGE("vm86 mode not supported on this platform\n");
440 /**********************************************************************
441 * DbgBreakPoint (NTDLL.@)
443 void WINAPI
DbgBreakPoint(void)
445 kill(getpid(), SIGTRAP
);
448 /**********************************************************************
449 * DbgUserBreakPoint (NTDLL.@)
451 void WINAPI
DbgUserBreakPoint(void)
453 kill(getpid(), SIGTRAP
);
456 #endif /* __sparc__ */