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 static sigset_t all_sigs
;
46 * FIXME: All this works only on Solaris for now
49 /**********************************************************************
52 static void save_context( CONTEXT
*context
, ucontext_t
*ucontext
)
54 /* Special registers */
55 context
->psr
= ucontext
->uc_mcontext
.gregs
[REG_PSR
];
56 context
->pc
= ucontext
->uc_mcontext
.gregs
[REG_PC
];
57 context
->npc
= ucontext
->uc_mcontext
.gregs
[REG_nPC
];
58 context
->y
= ucontext
->uc_mcontext
.gregs
[REG_Y
];
59 context
->wim
= 0; /* FIXME */
60 context
->tbr
= 0; /* FIXME */
62 /* Global registers */
63 context
->g0
= 0; /* always */
64 context
->g1
= ucontext
->uc_mcontext
.gregs
[REG_G1
];
65 context
->g2
= ucontext
->uc_mcontext
.gregs
[REG_G2
];
66 context
->g3
= ucontext
->uc_mcontext
.gregs
[REG_G3
];
67 context
->g4
= ucontext
->uc_mcontext
.gregs
[REG_G4
];
68 context
->g5
= ucontext
->uc_mcontext
.gregs
[REG_G5
];
69 context
->g6
= ucontext
->uc_mcontext
.gregs
[REG_G6
];
70 context
->g7
= ucontext
->uc_mcontext
.gregs
[REG_G7
];
72 /* Current 'out' registers */
73 context
->o0
= ucontext
->uc_mcontext
.gregs
[REG_O0
];
74 context
->o1
= ucontext
->uc_mcontext
.gregs
[REG_O1
];
75 context
->o2
= ucontext
->uc_mcontext
.gregs
[REG_O2
];
76 context
->o3
= ucontext
->uc_mcontext
.gregs
[REG_O3
];
77 context
->o4
= ucontext
->uc_mcontext
.gregs
[REG_O4
];
78 context
->o5
= ucontext
->uc_mcontext
.gregs
[REG_O5
];
79 context
->o6
= ucontext
->uc_mcontext
.gregs
[REG_O6
];
80 context
->o7
= ucontext
->uc_mcontext
.gregs
[REG_O7
];
82 /* FIXME: what if the current register window isn't saved? */
83 if ( ucontext
->uc_mcontext
.gwins
&& ucontext
->uc_mcontext
.gwins
->wbcnt
> 0 )
85 /* Current 'local' registers from first register window */
86 context
->l0
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[0];
87 context
->l1
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[1];
88 context
->l2
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[2];
89 context
->l3
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[3];
90 context
->l4
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[4];
91 context
->l5
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[5];
92 context
->l6
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[6];
93 context
->l7
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_local
[7];
95 /* Current 'in' registers from first register window */
96 context
->i0
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[0];
97 context
->i1
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[1];
98 context
->i2
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[2];
99 context
->i3
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[3];
100 context
->i4
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[4];
101 context
->i5
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[5];
102 context
->i6
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[6];
103 context
->i7
= ucontext
->uc_mcontext
.gwins
->wbuf
[0].rw_in
[7];
107 /**********************************************************************
110 static void restore_context( CONTEXT
*context
, ucontext_t
*ucontext
)
115 /**********************************************************************
118 static void save_fpu( CONTEXT
*context
, ucontext_t
*ucontext
)
123 /**********************************************************************
126 static void restore_fpu( CONTEXT
*context
, ucontext_t
*ucontext
)
132 /**********************************************************************
135 * Handler for SIGSEGV.
137 static void segv_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
139 EXCEPTION_RECORD rec
;
142 /* we want the page-fault case to be fast */
143 if ( info
->si_code
== SEGV_ACCERR
)
144 if (VIRTUAL_HandleFault( (LPVOID
)info
->si_addr
)) return;
146 save_context( &context
, ucontext
);
147 rec
.ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
148 rec
.ExceptionRecord
= NULL
;
149 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
150 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
151 rec
.NumberParameters
= 2;
152 rec
.ExceptionInformation
[0] = 0; /* FIXME: read/write access ? */
153 rec
.ExceptionInformation
[1] = (DWORD
)info
->si_addr
;
155 EXC_RtlRaiseException( &rec
, &context
);
156 restore_context( &context
, ucontext
);
159 /**********************************************************************
162 * Handler for SIGBUS.
164 static void bus_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
166 EXCEPTION_RECORD rec
;
169 save_context( &context
, ucontext
);
170 rec
.ExceptionRecord
= NULL
;
171 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
172 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
173 rec
.NumberParameters
= 0;
175 if ( info
->si_code
== BUS_ADRALN
)
176 rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
178 rec
.ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
180 EXC_RtlRaiseException( &rec
, &context
);
181 restore_context( &context
, ucontext
);
184 /**********************************************************************
187 * Handler for SIGILL.
189 static void ill_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
191 EXCEPTION_RECORD rec
;
194 switch ( info
->si_code
)
201 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
206 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
210 rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
214 save_context( &context
, ucontext
);
215 rec
.ExceptionRecord
= NULL
;
216 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
217 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
218 rec
.NumberParameters
= 0;
219 EXC_RtlRaiseException( &rec
, &context
);
220 restore_context( &context
, ucontext
);
224 /**********************************************************************
227 * Handler for SIGTRAP.
229 static void trap_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
231 EXCEPTION_RECORD rec
;
234 switch ( info
->si_code
)
237 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
241 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
245 save_context( &context
, ucontext
);
246 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
247 rec
.ExceptionRecord
= NULL
;
248 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
249 rec
.NumberParameters
= 0;
250 EXC_RtlRaiseException( &rec
, &context
);
251 restore_context( &context
, ucontext
);
255 /**********************************************************************
258 * Handler for SIGFPE.
260 static void fpe_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
262 EXCEPTION_RECORD rec
;
265 switch ( info
->si_code
)
268 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
271 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
274 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
277 rec
.ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
280 rec
.ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
283 rec
.ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
286 rec
.ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
290 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
294 save_context( &context
, ucontext
);
295 save_fpu( &context
, ucontext
);
296 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
297 rec
.ExceptionRecord
= NULL
;
298 rec
.ExceptionAddress
= (LPVOID
)context
.pc
;
299 rec
.NumberParameters
= 0;
300 EXC_RtlRaiseException( &rec
, &context
);
301 restore_context( &context
, ucontext
);
302 restore_fpu( &context
, ucontext
);
306 /**********************************************************************
309 * Handler for SIGINT.
311 static void int_handler( int signal
, siginfo_t
*info
, ucontext_t
*ucontext
)
313 EXCEPTION_RECORD rec
;
316 save_context( &context
, ucontext
);
317 rec
.ExceptionCode
= CONTROL_C_EXIT
;
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
);
327 /***********************************************************************
330 * Set a signal handler
332 static int set_handler( int sig
, void (*func
)() )
334 struct sigaction sig_act
;
336 sig_act
.sa_handler
= NULL
;
337 sig_act
.sa_sigaction
= func
;
338 sigemptyset( &sig_act
.sa_mask
);
339 sig_act
.sa_flags
= SA_SIGINFO
;
341 return sigaction( sig
, &sig_act
, NULL
);
345 /***********************************************************************
348 * Unblock signals. Called from EXC_RtlRaiseException.
350 void SIGNAL_Unblock( void )
352 sigprocmask( SIG_UNBLOCK
, &all_sigs
, NULL
);
356 /**********************************************************************
359 BOOL
SIGNAL_Init(void)
361 /* ignore SIGPIPE so that WINSOCK can get a EPIPE error instead */
362 signal( SIGPIPE
, SIG_IGN
);
363 /* automatic child reaping to avoid zombies */
364 signal( SIGCHLD
, SIG_IGN
);
366 sigfillset( &all_sigs
);
368 if (set_handler( SIGINT
, (void (*)())int_handler
) == -1) goto error
;
369 if (set_handler( SIGFPE
, (void (*)())fpe_handler
) == -1) goto error
;
370 if (set_handler( SIGSEGV
, (void (*)())segv_handler
) == -1) goto error
;
371 if (set_handler( SIGILL
, (void (*)())ill_handler
) == -1) goto error
;
372 if (set_handler( SIGBUS
, (void (*)())bus_handler
) == -1) goto error
;
373 if (set_handler( SIGTRAP
, (void (*)())trap_handler
) == -1) goto error
;
382 /**********************************************************************
385 void SIGNAL_Reset(void)
389 /* block the async signals */
390 sigemptyset( &block_set
);
391 sigaddset( &block_set
, SIGALRM
);
392 sigaddset( &block_set
, SIGIO
);
393 sigaddset( &block_set
, SIGHUP
);
394 sigaddset( &block_set
, SIGUSR2
);
395 sigprocmask( SIG_BLOCK
, &block_set
, NULL
);
397 /* restore default handlers */
398 signal( SIGINT
, SIG_DFL
);
399 signal( SIGFPE
, SIG_DFL
);
400 signal( SIGSEGV
, SIG_DFL
);
401 signal( SIGILL
, SIG_DFL
);
402 signal( SIGBUS
, SIG_DFL
);
403 signal( SIGTRAP
, SIG_DFL
);
407 /**********************************************************************
410 void __wine_enter_vm86( CONTEXT
*context
)
412 MESSAGE("vm86 mode not supported on this platform\n");
415 /**********************************************************************
416 * DbgBreakPoint (NTDLL.@)
418 void WINAPI
DbgBreakPoint(void)
423 /**********************************************************************
424 * DbgUserBreakPoint (NTDLL.@)
426 void WINAPI
DbgUserBreakPoint(void)
431 #endif /* __sparc__ */