Release 940405
[wine/gsoc-2012-control.git] / loader / signal.c
blobd788cc06c01e82af56459f2b97e850e802d8d8f6
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"
22 char * cstack[4096];
23 struct sigaction segv_act;
25 #ifdef linux
26 extern void ___sig_restore();
27 extern void ___masksig_restore();
28 #endif
30 /* Similar to the sigaction function in libc, except it leaves alone the
31 restorer field */
33 static int
34 wine_sigaction(int sig,struct sigaction * new, struct sigaction * old)
36 __asm__("int $0x80":"=a" (sig)
37 :"0" (SYS_sigaction),"b" (sig),"c" (new),"d" (old));
38 if (sig>=0)
39 return 0;
40 errno = -sig;
41 return -1;
44 #ifdef linux
45 static void win_fault(int signal, struct sigcontext_struct context)
47 struct sigcontext_struct *scp = &context;
48 #else
49 static void win_fault(int signal, int code, struct sigcontext *scp)
51 #endif
52 unsigned char * instr;
53 unsigned int * dump;
54 int i;
56 /* First take care of a few preliminaries */
57 #ifdef linux
58 if(signal != SIGSEGV)
59 exit(1);
60 if((scp->sc_cs & 7) != 7)
62 #endif
63 #if defined(__NetBSD__) || defined(__FreeBSD__)
64 /* set_es(0x27); set_ds(0x27); */
65 if(signal != SIGBUS)
66 exit(1);
67 if(scp->sc_cs == 0x1f)
69 #endif
70 fprintf(stderr,
71 "Segmentation fault in Wine program (%x:%x)."
72 " Please debug\n",
73 scp->sc_cs, scp->sc_eip);
74 goto oops;
77 /* Now take a look at the actual instruction where the program
78 bombed */
79 instr = (unsigned char *) SAFEMAKEPTR(scp->sc_cs, scp->sc_eip);
81 switch(*instr)
83 case 0xcd: /* int <XX> */
84 instr++;
85 switch(*instr)
87 case 0x10:
88 if(!do_int10(scp))
89 goto oops;
90 break;
92 case 0x11:
93 scp->sc_eax = (scp->sc_eax & 0xffff0000L) | DOS_GetEquipment();
94 break;
96 case 0x12:
97 scp->sc_eax = (scp->sc_eax & 0xffff0000L) | 640L;
98 break; /* get base mem size */
100 case 0x1A:
101 if(!do_int1A(scp))
102 goto oops;
103 break;
105 case 0x21:
106 if (!do_int21(scp))
107 goto oops;
108 break;
110 case 0x22:
111 scp->sc_eax = 0x1234;
112 scp->sc_ebx = 0x5678;
113 scp->sc_ecx = 0x9abc;
114 scp->sc_edx = 0xdef0;
115 break;
117 case 0x25:
118 if (!do_int25(scp))
119 goto oops;
120 break;
122 case 0x26:
123 if (!do_int26(scp))
124 goto oops;
125 break;
127 default:
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 fprintf(stderr,"In win_fault %x:%x\n", scp->sc_cs, scp->sc_eip);
166 #ifdef linux
167 wine_debug(scp); /* Enter our debugger */
168 #else
169 fprintf(stderr,"Stack: %x:%x\n", scp->sc_ss, scp->sc_esp);
170 dump = (int*) scp;
171 for(i=0; i<22; i++)
173 fprintf(stderr," %8.8x", *dump++);
174 if ((i % 8) == 7)
175 fprintf(stderr,"\n");
177 fprintf(stderr,"\n");
178 exit(1);
179 #endif
182 int init_wine_signals(void)
184 #ifdef linux
185 segv_act.sa_handler = (__sighandler_t) win_fault;
186 /* Point to the top of the stack, minus 4 just in case, and make
187 it aligned */
188 segv_act.sa_restorer =
189 (void (*)()) (((unsigned int)(cstack) + sizeof(cstack) - 4) & ~3);
190 wine_sigaction(SIGSEGV, &segv_act, NULL);
191 #endif
192 #if defined(__NetBSD__) || defined(__FreeBSD__)
193 struct sigstack ss;
194 sigset_t sig_mask;
196 ss.ss_sp = (char *) (((unsigned int)(cstack) + sizeof(cstack) - 4) & ~3);
197 ss.ss_onstack = 0;
198 if (sigstack(&ss, NULL) < 0) {
199 perror("sigstack");
200 exit(1);
202 sigemptyset(&sig_mask);
203 segv_act.sa_handler = (__sighandler_t) win_fault;
204 segv_act.sa_flags = SA_ONSTACK;
205 segv_act.sa_mask = sig_mask;
206 if (sigaction(SIGBUS, &segv_act, NULL) < 0) {
207 perror("sigaction");
208 exit(1);
210 #endif
213 #endif /* ifndef WINELIB */