Release 940614
[wine/multimedia.git] / loader / signal.c
blob861c04e74224cf8d79d823f6f0af15e60425808e
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 && signal != SIGTRAP)
94 exit(1);
96 /* And back up over the int3 instruction. */
97 if(signal == SIGTRAP) {
98 scp->sc_eip--;
99 goto oops;
102 if((scp->sc_cs & 7) != 7)
104 #endif
105 #if defined(__NetBSD__) || defined(__FreeBSD__)
106 /* set_es(0x27); set_ds(0x27); */
107 if(signal != SIGBUS)
108 exit(1);
109 if(scp->sc_cs == 0x1f)
111 #endif
112 fprintf(stderr,
113 "Segmentation fault in Wine program (%x:%x)."
114 " Please debug\n",
115 scp->sc_cs, scp->sc_eip);
116 goto oops;
119 /* Now take a look at the actual instruction where the program
120 bombed */
121 instr = (unsigned char *) SAFEMAKEPTR(scp->sc_cs, scp->sc_eip);
123 switch(*instr)
125 case 0xcd: /* int <XX> */
126 instr++;
127 if (!do_int(*instr, scp)) {
128 fprintf(stderr,"Unexpected Windows interrupt %x\n", *instr);
129 goto oops;
131 scp->sc_eip += 2; /* Bypass the int instruction */
132 break;
134 case 0xec: /* inb al,dx */
135 inportb(scp);
136 scp->sc_eip++;
137 break;
139 case 0xed: /* in ax,dx */
140 inport(scp);
141 scp->sc_eip++;
142 break;
144 case 0xee: /* outb dx,al */
145 outportb(scp);
146 scp->sc_eip++;
147 break;
149 case 0xef: /* out dx,ax */
150 outport(scp);
151 scp->sc_eip++;
152 break;
154 default:
155 fprintf(stderr, "Unexpected Windows program segfault"
156 " - opcode = %x\n", *instr);
157 goto oops;
160 /* OK, done handling the interrupt */
162 return;
164 oops:
165 XUngrabPointer(display, CurrentTime);
166 XUngrabServer(display);
167 XFlush(display);
168 fprintf(stderr,"In win_fault %x:%x\n", scp->sc_cs, scp->sc_eip);
169 #ifdef linux
170 wine_debug(signal, scp); /* Enter our debugger */
171 #else
172 fprintf(stderr,"Stack: %x:%x\n", scp->sc_ss, scp->sc_esp);
173 dump = (int*) scp;
174 for(i=0; i<22; i++)
176 fprintf(stderr," %8.8x", *dump++);
177 if ((i % 8) == 7)
178 fprintf(stderr,"\n");
180 fprintf(stderr,"\n");
181 exit(1);
182 #endif
185 int init_wine_signals(void)
187 #ifdef linux
188 segv_act.sa_handler = (__sighandler_t) win_fault;
189 /* Point to the top of the stack, minus 4 just in case, and make
190 it aligned */
191 segv_act.sa_restorer =
192 (void (*)()) (((unsigned int)(cstack) + sizeof(cstack) - 4) & ~3);
193 wine_sigaction(SIGSEGV, &segv_act, NULL);
194 wine_sigaction(SIGTRAP, &segv_act, NULL); /* For breakpoints */
195 #endif
196 #if defined(__NetBSD__) || defined(__FreeBSD__)
197 struct sigstack ss;
198 sigset_t sig_mask;
200 ss.ss_sp = (char *) (((unsigned int)(cstack) + sizeof(cstack) - 4) & ~3);
201 ss.ss_onstack = 0;
202 if (sigstack(&ss, NULL) < 0) {
203 perror("sigstack");
204 exit(1);
206 sigemptyset(&sig_mask);
207 segv_act.sa_handler = (__sighandler_t) win_fault;
208 segv_act.sa_flags = SA_ONSTACK;
209 segv_act.sa_mask = sig_mask;
210 if (sigaction(SIGBUS, &segv_act, NULL) < 0) {
211 perror("sigaction");
212 exit(1);
214 #endif
217 #endif /* ifndef WINELIB */