Release 0.5
[wine/multimedia.git] / loader / signal.c
blobb2a492e221fc4c5adc4508107b4426e9795b2e62
1 #include <signal.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <time.h>
6 #ifdef __NetBSD__
7 #include <sys/syscall.h>
8 #else
9 #include <syscall.h>
10 #endif
11 #include <signal.h>
12 #include <errno.h>
13 #ifdef linux
14 #include <linux/sched.h>
15 #include <asm/system.h>
16 #endif
17 #include "wine.h"
18 #include "segmem.h"
20 char * cstack[4096];
21 struct sigaction segv_act;
23 #ifdef linux
24 extern void ___sig_restore();
25 extern void ___masksig_restore();
26 #endif
28 /* Similar to the sigaction function in libc, except it leaves alone the
29 restorer field */
31 static int
32 wine_sigaction(int sig,struct sigaction * new, struct sigaction * old)
34 __asm__("int $0x80":"=a" (sig)
35 :"0" (SYS_sigaction),"b" (sig),"c" (new),"d" (old));
36 if (sig>=0)
37 return 0;
38 errno = -sig;
39 return -1;
42 #ifdef linux
43 static void win_fault(int signal, struct sigcontext_struct context){
44 struct sigcontext_struct *scp = &context;
45 #else
46 static void win_fault(int signal, int code, struct sigcontext *scp){
47 #endif
48 unsigned char * instr;
49 unsigned char intno;
50 unsigned int * dump;
51 int i;
53 /* First take care of a few preliminaries */
54 #ifdef linux
55 if(signal != SIGSEGV) exit(1);
56 if((scp->sc_cs & 7) != 7){
57 #endif
58 #ifdef __NetBSD__
59 /* set_es(0x27); set_ds(0x27); */
60 if(signal != SIGBUS) exit(1);
61 if(scp->sc_cs == 0x1f){
62 #endif
63 fprintf(stderr,
64 "Segmentation fault in Wine program (%x:%x)."
65 " Please debug\n",
66 scp->sc_cs, scp->sc_eip);
67 goto oops;
70 /* Now take a look at the actual instruction where the program
71 bombed */
72 instr = (char *) SAFEMAKEPTR(scp->sc_cs, scp->sc_eip);
74 if(*instr != 0xcd) {
75 fprintf(stderr,
76 "Unexpected Windows program segfault"
77 " - opcode = %x\n", *instr);
78 #if 0
79 return;
80 #else
81 goto oops;
82 #endif
85 instr++;
86 intno = *instr;
87 switch(intno){
88 case 0x21:
89 if(!do_int21(scp)) goto oops;
90 break;
91 case 0x11:
92 scp->sc_eax = (scp->sc_eax & 0xffff0000L) | DOS_GetEquipment();
93 break;
94 case 0x12:
95 scp->sc_eax = (scp->sc_eax & 0xffff0000L) | 640L;
96 break; /* get base mem size */
97 case 0x1A:
98 if(!do_int1A(scp)) goto oops;
99 break;
100 default:
101 fprintf(stderr,"Unexpected Windows interrupt %x\n", intno);
102 goto oops;
105 /* OK, done handling the interrupt */
107 scp->sc_eip += 2; /* Bypass the int instruction */
108 return;
109 oops:
110 fprintf(stderr,"In win_fault %x:%x\n", scp->sc_cs, scp->sc_eip);
111 #ifdef linux
112 wine_debug(scp); /* Enter our debugger */
113 #else
114 fprintf(stderr,"Stack: %x:%x\n", scp->sc_ss, scp->sc_esp);
115 dump = (int*) scp;
116 for(i=0; i<22; i++)
118 fprintf(stderr," %8.8x", *dump++);
119 if ((i % 8) == 7)
120 fprintf(stderr,"\n");
122 fprintf(stderr,"\n");
123 exit(1);
124 #endif
128 init_wine_signals(){
129 #ifdef linux
130 segv_act.sa_handler = (__sighandler_t) win_fault;
131 /* Point to the top of the stack, minus 4 just in case, and make
132 it aligned */
133 segv_act.sa_restorer =
134 (void (*)()) (((unsigned int)(cstack) + sizeof(cstack) - 4) & ~3);
135 wine_sigaction(SIGSEGV, &segv_act, NULL);
136 #endif
137 #ifdef __NetBSD__
138 struct sigstack ss;
139 sigset_t sig_mask;
141 ss.ss_sp = (char *) (((unsigned int)(cstack) + sizeof(cstack) - 4) & ~3);
142 ss.ss_onstack = 0;
143 if (sigstack(&ss, NULL) < 0) {
144 perror("sigstack");
145 exit(1);
147 sigemptyset(&sig_mask);
148 segv_act.sa_handler = (__sighandler_t) win_fault;
149 segv_act.sa_flags = SA_ONSTACK;
150 segv_act.sa_mask = sig_mask;
151 if (sigaction(SIGBUS, &segv_act, NULL) < 0) {
152 perror("sigaction");
153 exit(1);
155 #endif