Release 940714
[wine/multimedia.git] / loader / signal.c
blobc0c52fb0f3ce784d275cc9da0c11dcdbc4771e31
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 #else
11 #include <syscall.h>
12 #endif
13 #ifdef linux
14 #include <linux/sched.h>
15 #include <asm/system.h>
16 #endif
18 #include "wine.h"
19 #include "segmem.h"
20 #include "prototypes.h"
21 #include "win.h"
23 char * cstack[4096];
24 struct sigaction segv_act;
26 #ifdef linux
27 extern void ___sig_restore();
28 extern void ___masksig_restore();
29 #endif
31 /* Similar to the sigaction function in libc, except it leaves alone the
32 restorer field */
34 static int
35 wine_sigaction(int sig,struct sigaction * new, struct sigaction * old)
37 __asm__("int $0x80":"=a" (sig)
38 :"0" (SYS_sigaction),"b" (sig),"c" (new),"d" (old));
39 if (sig>=0)
40 return 0;
41 errno = -sig;
42 return -1;
45 int do_int(int intnum, struct sigcontext_struct *scp)
47 switch(intnum)
49 case 0x10: return do_int10(scp);
51 case 0x11:
52 scp->sc_eax = (scp->sc_eax & 0xffff0000L) | DOS_GetEquipment();
53 return 1;
55 case 0x12:
56 scp->sc_eax = (scp->sc_eax & 0xffff0000L) | 640L;
57 return 1; /* get base mem size */
59 case 0x15: return do_int15(scp);
60 case 0x16: return do_int16(scp);
61 case 0x1A: return do_int1A(scp);
62 case 0x21: return do_int21(scp);
64 case 0x22:
65 scp->sc_eax = 0x1234;
66 scp->sc_ebx = 0x5678;
67 scp->sc_ecx = 0x9abc;
68 scp->sc_edx = 0xdef0;
69 return 1;
71 case 0x25: return do_int25(scp);
72 case 0x26: return do_int26(scp);
73 case 0x2f: return do_int2f(scp);
74 case 0x31: return do_int31(scp);
76 return 0;
79 #ifdef linux
80 static void win_fault(int signal, struct sigcontext_struct context)
82 struct sigcontext_struct *scp = &context;
83 #else
84 static void win_fault(int signal, int code, struct sigcontext *scp)
86 #endif
87 unsigned char * instr;
88 unsigned int * dump;
89 int i;
91 /* First take care of a few preliminaries */
92 #ifdef linux
93 if(signal != SIGSEGV
94 && signal != SIGILL
95 #ifdef SIGBUS
96 && signal != SIGBUS
97 #endif
98 && signal != SIGTRAP)
100 exit(1);
103 /* And back up over the int3 instruction. */
104 if(signal == SIGTRAP) {
105 scp->sc_eip--;
106 goto oops;
109 if((scp->sc_cs & 7) != 7)
111 #endif
112 #if defined(__NetBSD__) || defined(__FreeBSD__)
113 /* set_es(0x27); set_ds(0x27); */
114 if(signal != SIGBUS)
115 exit(1);
116 if(scp->sc_cs == 0x1f)
118 #endif
119 fprintf(stderr,
120 "Segmentation fault in Wine program (%x:%x)."
121 " Please debug\n",
122 scp->sc_cs, scp->sc_eip);
123 goto oops;
126 /* Now take a look at the actual instruction where the program
127 bombed */
128 instr = (unsigned char *) SAFEMAKEPTR(scp->sc_cs, scp->sc_eip);
130 switch(*instr)
132 case 0xcd: /* int <XX> */
133 instr++;
134 if (!do_int(*instr, scp)) {
135 fprintf(stderr,"Unexpected Windows interrupt %x\n", *instr);
136 goto oops;
138 scp->sc_eip += 2; /* Bypass the int instruction */
139 break;
141 case 0xec: /* inb al,dx */
142 inportb(scp);
143 scp->sc_eip++;
144 break;
146 case 0xed: /* in ax,dx */
147 inport(scp);
148 scp->sc_eip++;
149 break;
151 case 0xee: /* outb dx,al */
152 outportb(scp);
153 scp->sc_eip++;
154 break;
156 case 0xef: /* out dx,ax */
157 outport(scp);
158 scp->sc_eip++;
159 break;
161 case 0xfa: /* cli, ignored */
162 scp->sc_eip++;
163 break;
165 case 0xfb: /* sti, ignored */
166 scp->sc_eip++;
167 break;
169 default:
170 fprintf(stderr, "Unexpected Windows program segfault"
171 " - opcode = %x\n", *instr);
172 goto oops;
175 /* OK, done handling the interrupt */
177 return;
179 oops:
180 XUngrabPointer(display, CurrentTime);
181 XUngrabServer(display);
182 XFlush(display);
183 fprintf(stderr,"In win_fault %x:%x\n", scp->sc_cs, scp->sc_eip);
184 #ifdef linux
185 wine_debug(signal, scp); /* Enter our debugger */
186 #else
187 fprintf(stderr,"Stack: %x:%x\n", scp->sc_ss, scp->sc_esp);
188 dump = (int*) scp;
189 for(i=0; i<22; i++)
191 fprintf(stderr," %8.8x", *dump++);
192 if ((i % 8) == 7)
193 fprintf(stderr,"\n");
195 fprintf(stderr,"\n");
196 exit(1);
197 #endif
200 int init_wine_signals(void)
202 #ifdef linux
203 segv_act.sa_handler = (__sighandler_t) win_fault;
204 /* Point to the top of the stack, minus 4 just in case, and make
205 it aligned */
206 segv_act.sa_restorer =
207 (void (*)()) (((unsigned int)(cstack) + sizeof(cstack) - 4) & ~3);
208 wine_sigaction(SIGSEGV, &segv_act, NULL);
209 wine_sigaction(SIGILL, &segv_act, NULL);
210 #ifdef SIGBUS
211 wine_sigaction(SIGBUS, &segv_act, NULL);
212 #endif
213 wine_sigaction(SIGTRAP, &segv_act, NULL); /* For breakpoints */
214 #endif
215 #if defined(__NetBSD__) || defined(__FreeBSD__)
216 struct sigstack ss;
217 sigset_t sig_mask;
219 ss.ss_sp = (char *) (((unsigned int)(cstack) + sizeof(cstack) - 4) & ~3);
220 ss.ss_onstack = 0;
221 if (sigstack(&ss, NULL) < 0) {
222 perror("sigstack");
223 exit(1);
225 sigemptyset(&sig_mask);
226 segv_act.sa_handler = (__sighandler_t) win_fault;
227 segv_act.sa_flags = SA_ONSTACK;
228 segv_act.sa_mask = sig_mask;
229 if (sigaction(SIGBUS, &segv_act, NULL) < 0) {
230 perror("sigaction");
231 exit(1);
233 #endif
236 #endif /* ifndef WINELIB */