Release 940722
[wine/multimedia.git] / loader / signal.c
blob9b8e851e1483b63f44b600489521f8feb1b0a347
1 #ifndef WINELIB
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <signal.h>
5 #include <errno.h>
6 #include <time.h>
8 #if defined(__NetBSD__) || defined(__FreeBSD__)
9 #include <sys/syscall.h>
10 #include <sys/param.h>
11 #else
12 #include <syscall.h>
13 #endif
14 #ifdef linux
15 #include <linux/sched.h>
16 #include <asm/system.h>
17 #endif
19 #include "wine.h"
20 #include "segmem.h"
21 #include "prototypes.h"
22 #include "win.h"
24 #if !defined(BSD4_4) || defined(linux)
25 char * cstack[4096];
26 #endif
27 struct sigaction segv_act;
29 #ifdef linux
30 extern void ___sig_restore();
31 extern void ___masksig_restore();
33 /* Similar to the sigaction function in libc, except it leaves alone the
34 restorer field */
36 static int
37 wine_sigaction(int sig,struct sigaction * new, struct sigaction * old)
39 __asm__("int $0x80":"=a" (sig)
40 :"0" (SYS_sigaction),"b" (sig),"c" (new),"d" (old));
41 if (sig>=0)
42 return 0;
43 errno = -sig;
44 return -1;
46 #endif
48 int do_int(int intnum, struct sigcontext_struct *scp)
50 switch(intnum)
52 case 0x10: return do_int10(scp);
54 case 0x11:
55 scp->sc_eax = (scp->sc_eax & 0xffff0000L) | DOS_GetEquipment();
56 return 1;
58 case 0x12:
59 scp->sc_eax = (scp->sc_eax & 0xffff0000L) | 640L;
60 return 1; /* get base mem size */
62 case 0x15: return do_int15(scp);
63 case 0x16: return do_int16(scp);
64 case 0x1A: return do_int1A(scp);
65 case 0x21: return do_int21(scp);
67 case 0x22:
68 scp->sc_eax = 0x1234;
69 scp->sc_ebx = 0x5678;
70 scp->sc_ecx = 0x9abc;
71 scp->sc_edx = 0xdef0;
72 return 1;
74 case 0x25: return do_int25(scp);
75 case 0x26: return do_int26(scp);
76 case 0x2f: return do_int2f(scp);
77 case 0x31: return do_int31(scp);
79 return 0;
82 #ifdef linux
83 static void win_fault(int signal, struct sigcontext_struct context)
85 struct sigcontext_struct *scp = &context;
86 #else
87 static void win_fault(int signal, int code, struct sigcontext *scp)
89 #endif
90 unsigned char * instr;
91 unsigned int * dump;
92 int i;
94 /* First take care of a few preliminaries */
95 #ifdef linux
96 if(signal != SIGSEGV
97 && signal != SIGILL
98 #ifdef SIGBUS
99 && signal != SIGBUS
100 #endif
101 && signal != SIGTRAP)
103 exit(1);
106 /* And back up over the int3 instruction. */
107 if(signal == SIGTRAP) {
108 scp->sc_eip--;
109 goto oops;
112 if((scp->sc_cs & 7) != 7)
114 #endif
115 #if defined(__NetBSD__) || defined(__FreeBSD__)
116 /* set_es(0x27); set_ds(0x27); */
117 if(signal != SIGBUS && signal != SIGSEGV && signal != SIGTRAP)
118 exit(1);
119 if(scp->sc_cs == 0x1f)
121 #endif
122 fprintf(stderr,
123 "Segmentation fault in Wine program (%x:%x)."
124 " Please debug\n",
125 scp->sc_cs, scp->sc_eip);
126 goto oops;
129 /* Now take a look at the actual instruction where the program
130 bombed */
131 instr = (unsigned char *) SAFEMAKEPTR(scp->sc_cs, scp->sc_eip);
133 switch(*instr)
135 case 0xcd: /* int <XX> */
136 instr++;
137 if (!do_int(*instr, scp)) {
138 fprintf(stderr,"Unexpected Windows interrupt %x\n", *instr);
139 goto oops;
141 scp->sc_eip += 2; /* Bypass the int instruction */
142 break;
144 case 0xec: /* inb al,dx */
145 inportb(scp);
146 scp->sc_eip++;
147 break;
149 case 0xed: /* in ax,dx */
150 inport(scp);
151 scp->sc_eip++;
152 break;
154 case 0xee: /* outb dx,al */
155 outportb(scp);
156 scp->sc_eip++;
157 break;
159 case 0xef: /* out dx,ax */
160 outport(scp);
161 scp->sc_eip++;
162 break;
164 case 0xfa: /* cli, ignored */
165 scp->sc_eip++;
166 break;
168 case 0xfb: /* sti, ignored */
169 scp->sc_eip++;
170 break;
172 default:
173 fprintf(stderr, "Unexpected Windows program segfault"
174 " - opcode = %x\n", *instr);
175 goto oops;
178 /* OK, done handling the interrupt */
180 return;
182 oops:
183 XUngrabPointer(display, CurrentTime);
184 XUngrabServer(display);
185 XFlush(display);
186 fprintf(stderr,"In win_fault %x:%x\n", scp->sc_cs, scp->sc_eip);
187 #if defined(linux) || defined(__NetBSD__)
188 wine_debug(signal, scp); /* Enter our debugger */
189 #else
190 fprintf(stderr,"Stack: %x:%x\n", scp->sc_ss, scp->sc_esp);
191 dump = (int*) scp;
192 for(i=0; i<22; i++)
194 fprintf(stderr," %8.8x", *dump++);
195 if ((i % 8) == 7)
196 fprintf(stderr,"\n");
198 fprintf(stderr,"\n");
199 exit(1);
200 #endif
203 int init_wine_signals(void)
205 #ifdef linux
206 segv_act.sa_handler = (__sighandler_t) win_fault;
207 /* Point to the top of the stack, minus 4 just in case, and make
208 it aligned */
209 segv_act.sa_restorer =
210 (void (*)()) (((unsigned int)(cstack) + sizeof(cstack) - 4) & ~3);
211 wine_sigaction(SIGSEGV, &segv_act, NULL);
212 wine_sigaction(SIGILL, &segv_act, NULL);
213 #ifdef SIGBUS
214 wine_sigaction(SIGBUS, &segv_act, NULL);
215 #endif
216 wine_sigaction(SIGTRAP, &segv_act, NULL); /* For breakpoints */
217 #endif
218 #if defined(__NetBSD__) || defined(__FreeBSD__)
219 sigset_t sig_mask;
220 #ifdef BSD4_4
221 struct sigaltstack ss;
223 if ((ss.ss_base = malloc(MINSIGSTKSZ)) == NULL) {
224 fprintf(stderr, "Unable to allocate signal stack (%d bytes)\n",
225 MINSIGSTKSZ);
226 exit(1);
228 ss.ss_size = MINSIGSTKSZ;
229 ss.ss_flags = 0;
230 if (sigaltstack(&ss, NULL) < 0) {
231 perror("sigstack");
232 exit(1);
234 #else
235 struct sigstack ss;
237 ss.ss_sp = (char *) (((unsigned int)(cstack) + sizeof(cstack) - 4) & ~3);
238 ss.ss_onstack = 0;
239 if (sigstack(&ss, NULL) < 0) {
240 perror("sigstack");
241 exit(1);
243 #endif
244 sigemptyset(&sig_mask);
245 segv_act.sa_handler = (void (*)) win_fault;
246 segv_act.sa_flags = SA_ONSTACK;
247 segv_act.sa_mask = sig_mask;
248 if (sigaction(SIGBUS, &segv_act, NULL) < 0) {
249 perror("sigaction: SIGBUS");
250 exit(1);
252 segv_act.sa_handler = (void (*)) win_fault;
253 segv_act.sa_flags = SA_ONSTACK;
254 segv_act.sa_mask = sig_mask;
255 if (sigaction(SIGSEGV, &segv_act, NULL) < 0) {
256 perror("sigaction: SIGSEGV");
257 exit(1);
259 segv_act.sa_handler = (void (*)) win_fault; /* For breakpoints */
260 segv_act.sa_flags = SA_ONSTACK;
261 segv_act.sa_mask = sig_mask;
262 if (sigaction(SIGTRAP, &segv_act, NULL) < 0) {
263 perror("sigaction: SIGTRAP");
264 exit(1);
266 #endif
269 #endif /* ifndef WINELIB */