Release 950901
[wine] / loader / signal.c
1 #ifndef WINELIB
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <signal.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <time.h>
8 #include <setjmp.h>
9
10 #if defined(__NetBSD__) || defined(__FreeBSD__)
11 #include <sys/syscall.h>
12 #include <sys/param.h>
13 #else
14 #include <syscall.h>
15 #endif
16
17 #include "debugger.h"
18 #include "miscemu.h"
19 #include "registers.h"
20 #include "win.h"
21
22 #if !defined(BSD4_4) || defined(linux) || defined(__FreeBSD__)
23 char * cstack[4096];
24 #endif
25 struct sigaction segv_act;
26 struct sigaction usr2_act;
27
28 #ifdef linux
29 extern void ___sig_restore();
30 extern void ___masksig_restore();
31
32 /* Similar to the sigaction function in libc, except it leaves alone the
33    restorer field */
34
35 static int
36 wine_sigaction(int sig,struct sigaction * new, struct sigaction * old)
37 {
38         __asm__("int $0x80":"=a" (sig)
39                 :"0" (SYS_sigaction),"b" (sig),"c" (new),"d" (old));
40         if (sig>=0)
41                 return 0;
42         errno = -sig;
43         return -1;
44 }
45 #endif
46
47
48 #ifdef linux
49 static void win_fault(int signal, struct sigcontext_struct context_struct)
50 {
51     struct sigcontext_struct *context = &context_struct;
52 #else
53 static void win_fault(int signal, int code, struct sigcontext *context)
54 {
55 #endif
56     if (signal != SIGTRAP)
57     {
58         if (CS_reg(context) == WINE_CODE_SELECTOR)
59         {
60             fprintf(stderr, "Segmentation fault in Wine program (%x:%lx)."
61                             "  Please debug\n",
62                             CS_reg(context), EIP_reg(context) );
63         }
64         else if (INSTR_EmulateInstruction( context )) return;
65         fprintf( stderr,"In win_fault %x:%lx\n",
66                  CS_reg(context), EIP_reg(context) );
67     }
68     XUngrabPointer(display, CurrentTime);
69     XUngrabServer(display);
70     XFlush(display);
71     wine_debug( signal, context );  /* Enter our debugger */
72 }
73
74 void init_wine_signals(void)
75 {
76         extern void stop_wait(int a);
77 #ifdef linux
78         segv_act.sa_handler = (__sighandler_t) win_fault;
79         /* Point to the top of the stack, minus 4 just in case, and make
80            it aligned  */
81         segv_act.sa_restorer = 
82             (void (*)()) (((unsigned int)(cstack) + sizeof(cstack) - 4) & ~3);
83         /* Point to the top of the stack, minus 4 just in case, and make
84            it aligned  */
85         wine_sigaction(SIGSEGV, &segv_act, NULL);
86         wine_sigaction(SIGILL, &segv_act, NULL);
87         wine_sigaction(SIGFPE, &segv_act, NULL);
88 #ifdef SIGBUS
89         wine_sigaction(SIGBUS, &segv_act, NULL);
90 #endif
91         wine_sigaction(SIGTRAP, &segv_act, NULL); /* For breakpoints */
92 #ifdef CONFIG_IPC
93         usr2_act.sa_restorer= segv_act.sa_restorer;
94         usr2_act.sa_handler = (__sighandler_t) stop_wait;
95         wine_sigaction(SIGUSR2, &usr2_act, NULL);
96 #endif  /* CONFIG_IPC */
97 #endif  /* linux */
98 #if defined(__NetBSD__) || defined(__FreeBSD__)
99         sigset_t sig_mask;
100         struct sigaltstack ss;
101         
102 #if !defined (__FreeBSD__)
103         if ((ss.ss_base = malloc(MINSIGSTKSZ)) == NULL) {
104 #else
105         if ((ss.ss_sp = malloc(MINSIGSTKSZ)) == NULL) {
106 #endif
107                 fprintf(stderr, "Unable to allocate signal stack (%d bytes)\n",
108                         MINSIGSTKSZ);
109                 exit(1);
110         }
111         ss.ss_size = MINSIGSTKSZ;
112         ss.ss_flags = 0;
113         if (sigaltstack(&ss, NULL) < 0) {
114                 perror("sigstack");
115                 exit(1);
116         }
117         sigemptyset(&sig_mask);
118         segv_act.sa_handler = (void (*)) win_fault;
119         segv_act.sa_flags = SA_ONSTACK;
120         segv_act.sa_mask = sig_mask;
121         if (sigaction(SIGBUS, &segv_act, NULL) < 0) {
122                 perror("sigaction: SIGBUS");
123                 exit(1);
124         }
125         segv_act.sa_handler = (void (*)) win_fault;
126         segv_act.sa_flags = SA_ONSTACK;
127         segv_act.sa_mask = sig_mask;
128         if (sigaction(SIGSEGV, &segv_act, NULL) < 0) {
129                 perror("sigaction: SIGSEGV");
130                 exit(1);
131         }
132         segv_act.sa_handler = (void (*)) win_fault; /* For breakpoints */
133         segv_act.sa_flags = SA_ONSTACK;
134         segv_act.sa_mask = sig_mask;
135         if (sigaction(SIGTRAP, &segv_act, NULL) < 0) {
136                 perror("sigaction: SIGTRAP");
137                 exit(1);
138         }
139 #ifdef CONFIG_IPC
140         usr2_act.sa_handler = (void (*)) stop_wait; /* For breakpoints */
141         usr2_act.sa_flags = SA_ONSTACK;
142         usr2_act.sa_mask = sig_mask;
143         if (sigaction(SIGUSR2, &usr2_act, NULL) < 0) {
144                 perror("sigaction: SIGUSR2");
145                 exit(1);
146         }
147 #endif  /* CONFIG_IPC */
148 #endif  /* __FreeBSD__ || __NetBSD__ */
149 }
150
151 #endif /* ifndef WINELIB */