Release 940510
[wine/multimedia.git] / loader / signal.c
blob92aecd5eacab0c071016ca5dbbac8884559a85ed
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 #ifdef linux
46 static void win_fault(int signal, struct sigcontext_struct context)
48 struct sigcontext_struct *scp = &context;
49 #else
50 static void win_fault(int signal, int code, struct sigcontext *scp)
52 #endif
53 unsigned char * instr;
54 unsigned int * dump;
55 int i;
57 /* First take care of a few preliminaries */
58 #ifdef linux
59 if(signal != SIGSEGV && signal != SIGTRAP)
60 exit(1);
62 /* And back up over the int3 instruction. */
63 if(signal == SIGTRAP) {
64 scp->sc_eip--;
65 goto oops;
68 if((scp->sc_cs & 7) != 7)
70 #endif
71 #if defined(__NetBSD__) || defined(__FreeBSD__)
72 /* set_es(0x27); set_ds(0x27); */
73 if(signal != SIGBUS)
74 exit(1);
75 if(scp->sc_cs == 0x1f)
77 #endif
78 fprintf(stderr,
79 "Segmentation fault in Wine program (%x:%x)."
80 " Please debug\n",
81 scp->sc_cs, scp->sc_eip);
82 goto oops;
85 /* Now take a look at the actual instruction where the program
86 bombed */
87 instr = (unsigned char *) SAFEMAKEPTR(scp->sc_cs, scp->sc_eip);
89 switch(*instr)
91 case 0xcd: /* int <XX> */
92 instr++;
93 switch(*instr)
95 case 0x10:
96 if(!do_int10(scp))
97 goto oops;
98 break;
100 case 0x11:
101 scp->sc_eax = (scp->sc_eax & 0xffff0000L) | DOS_GetEquipment();
102 break;
104 case 0x12:
105 scp->sc_eax = (scp->sc_eax & 0xffff0000L) | 640L;
106 break; /* get base mem size */
108 case 0x1A:
109 if(!do_int1A(scp))
110 goto oops;
111 break;
113 case 0x21:
114 if (!do_int21(scp))
115 goto oops;
116 break;
118 case 0x22:
119 scp->sc_eax = 0x1234;
120 scp->sc_ebx = 0x5678;
121 scp->sc_ecx = 0x9abc;
122 scp->sc_edx = 0xdef0;
123 break;
125 case 0x25:
126 if (!do_int25(scp))
127 goto oops;
128 break;
130 case 0x26:
131 if (!do_int26(scp))
132 goto oops;
133 break;
135 default:
136 fprintf(stderr,"Unexpected Windows interrupt %x\n", *instr);
137 goto oops;
139 scp->sc_eip += 2; /* Bypass the int instruction */
140 break;
142 case 0xec: /* inb al,dx */
143 inportb(scp);
144 scp->sc_eip++;
145 break;
147 case 0xed: /* in ax,dx */
148 inport(scp);
149 scp->sc_eip++;
150 break;
152 case 0xee: /* outb dx,al */
153 outportb(scp);
154 scp->sc_eip++;
155 break;
157 case 0xef: /* out dx,ax */
158 outport(scp);
159 scp->sc_eip++;
160 break;
162 default:
163 fprintf(stderr, "Unexpected Windows program segfault"
164 " - opcode = %x\n", *instr);
165 goto oops;
168 /* OK, done handling the interrupt */
170 return;
172 oops:
173 XUngrabPointer(display, CurrentTime);
174 XUngrabServer(display);
175 XFlush(display);
176 fprintf(stderr,"In win_fault %x:%x\n", scp->sc_cs, scp->sc_eip);
177 #ifdef linux
178 wine_debug(signal, scp); /* Enter our debugger */
179 #else
180 fprintf(stderr,"Stack: %x:%x\n", scp->sc_ss, scp->sc_esp);
181 dump = (int*) scp;
182 for(i=0; i<22; i++)
184 fprintf(stderr," %8.8x", *dump++);
185 if ((i % 8) == 7)
186 fprintf(stderr,"\n");
188 fprintf(stderr,"\n");
189 exit(1);
190 #endif
193 int init_wine_signals(void)
195 #ifdef linux
196 segv_act.sa_handler = (__sighandler_t) win_fault;
197 /* Point to the top of the stack, minus 4 just in case, and make
198 it aligned */
199 segv_act.sa_restorer =
200 (void (*)()) (((unsigned int)(cstack) + sizeof(cstack) - 4) & ~3);
201 wine_sigaction(SIGSEGV, &segv_act, NULL);
202 wine_sigaction(SIGTRAP, &segv_act, NULL); /* For breakpoints */
203 #endif
204 #if defined(__NetBSD__) || defined(__FreeBSD__)
205 struct sigstack ss;
206 sigset_t sig_mask;
208 ss.ss_sp = (char *) (((unsigned int)(cstack) + sizeof(cstack) - 4) & ~3);
209 ss.ss_onstack = 0;
210 if (sigstack(&ss, NULL) < 0) {
211 perror("sigstack");
212 exit(1);
214 sigemptyset(&sig_mask);
215 segv_act.sa_handler = (__sighandler_t) win_fault;
216 segv_act.sa_flags = SA_ONSTACK;
217 segv_act.sa_mask = sig_mask;
218 if (sigaction(SIGBUS, &segv_act, NULL) < 0) {
219 perror("sigaction");
220 exit(1);
222 #endif
225 #endif /* ifndef WINELIB */