Fixed some issues found by winapi_check.
[wine/dcerpc.git] / dlls / ntdll / signal_sparc.c
blob66f49cf0b1bc67e8483ac0b94cefd78aa7ac3c62
1 /*
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
21 #ifdef __sparc__
23 #include "config.h"
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <sys/ucontext.h>
31 #include "ntddk.h"
32 #include "winbase.h"
33 #include "winnt.h"
35 #include "wine/exception.h"
36 #include "global.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 /***********************************************************************
51 * dispatch_signal
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 /**********************************************************************
65 * save_context
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 /**********************************************************************
123 * restore_context
125 static void restore_context( CONTEXT *context, ucontext_t *ucontext )
127 /* FIXME */
130 /**********************************************************************
131 * save_fpu
133 static void save_fpu( CONTEXT *context, ucontext_t *ucontext )
135 /* FIXME */
138 /**********************************************************************
139 * restore_fpu
141 static void restore_fpu( CONTEXT *context, ucontext_t *ucontext )
143 /* FIXME */
147 /**********************************************************************
148 * segv_handler
150 * Handler for SIGSEGV.
152 static void segv_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
154 EXCEPTION_RECORD rec;
155 CONTEXT context;
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 /**********************************************************************
175 * bus_handler
177 * Handler for SIGBUS.
179 static void bus_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
181 EXCEPTION_RECORD rec;
182 CONTEXT context;
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;
192 else
193 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
195 EXC_RtlRaiseException( &rec, &context );
196 restore_context( &context, ucontext );
199 /**********************************************************************
200 * ill_handler
202 * Handler for SIGILL.
204 static void ill_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
206 EXCEPTION_RECORD rec;
207 CONTEXT context;
209 switch ( info->si_code )
211 default:
212 case ILL_ILLOPC:
213 case ILL_ILLOPN:
214 case ILL_ILLADR:
215 case ILL_ILLTRP:
216 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
217 break;
219 case ILL_PRVOPC:
220 case ILL_PRVREG:
221 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
222 break;
224 case ILL_BADSTK:
225 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
226 break;
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 /**********************************************************************
240 * trap_handler
242 * Handler for SIGTRAP.
244 static void trap_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
246 EXCEPTION_RECORD rec;
247 CONTEXT context;
249 switch ( info->si_code )
251 case TRAP_TRACE:
252 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
253 break;
254 case TRAP_BRKPT:
255 default:
256 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
257 break;
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 /**********************************************************************
271 * fpe_handler
273 * Handler for SIGFPE.
275 static void fpe_handler( int signal, siginfo_t *info, ucontext_t *ucontext )
277 EXCEPTION_RECORD rec;
278 CONTEXT context;
280 switch ( info->si_code )
282 case FPE_FLTSUB:
283 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
284 break;
285 case FPE_INTDIV:
286 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
287 break;
288 case FPE_INTOVF:
289 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
290 break;
291 case FPE_FLTDIV:
292 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
293 break;
294 case FPE_FLTOVF:
295 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
296 break;
297 case FPE_FLTUND:
298 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
299 break;
300 case FPE_FLTRES:
301 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
302 break;
303 case FPE_FLTINV:
304 default:
305 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
306 break;
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 /**********************************************************************
322 * int_handler
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;
331 CONTEXT context;
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 /***********************************************************************
346 * set_handler
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;
370 handlers[sig] = wsh;
371 return 0;
375 /***********************************************************************
376 * SIGNAL_Unblock
378 * Unblock signals. Called from EXC_RtlRaiseException.
380 void SIGNAL_Unblock( void )
382 sigprocmask( SIG_UNBLOCK, &all_sigs, NULL );
386 /**********************************************************************
387 * SIGNAL_Init
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;
399 return TRUE;
401 error:
402 perror("sigaction");
403 return FALSE;
407 /**********************************************************************
408 * SIGNAL_Reset
410 void SIGNAL_Reset(void)
412 sigset_t block_set;
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 /**********************************************************************
433 * __wine_enter_vm86
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__ */