Added macros to header file for easier function calling.
[wine] / loader / dos / dosmod.c
1 /*
2  * DOS Virtual Machine
3  *
4  * Copyright 1998 Ove Kåven
5  */
6
7 #if defined(linux) && defined(__i386__)
8
9 /* apparently ELF images are usually loaded high anyway */
10 #ifndef __ELF__
11 /* if not, force dosmod at high addresses */
12 asm(".org 0x110000");
13 #endif __ELF__
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <sys/vm86.h>
25 #include <sys/time.h>
26 #include <sys/types.h>
27 #include <sys/ptrace.h>
28 #include <sys/wait.h>
29 #include "dosmod.h"
30
31  /* FIXME: hack because libc vm86 may be the old syscall version */
32
33 #define SYS_vm86   166
34
35 static __inline__ int vm86plus( int func, struct vm86plus_struct *ptr )
36 {
37     int res;
38 #ifdef __PIC__
39     __asm__ __volatile__( "pushl %%ebx\n\t"
40                           "movl %2,%%ebx\n\t"
41                           "int $0x80\n\t"
42                           "popl %%ebx"
43                           : "=a" (res)
44                           : "0" (SYS_vm86),
45                             "g" (func),
46                             "c" (ptr) );
47 #else
48     __asm__ __volatile__("int $0x80"
49                          : "=a" (res)
50                          : "0" (SYS_vm86),
51                            "b" (func),
52                            "c" (ptr) );
53 #endif  /* __PIC__ */
54     if (res >= 0) return res;
55     errno = -res;
56     return -1;
57 }
58
59 void set_timer(struct timeval*tim)
60 {
61  struct itimerval cur;
62
63  cur.it_interval=*tim;
64  cur.it_value=*tim;
65  setitimer(ITIMER_REAL,&cur,NULL);
66 }
67
68 volatile int sig_pend,sig_fatal=0;
69 void*img;
70 struct vm86plus_struct VM86;
71
72 void sig_handler(int sig)
73 {
74  if (sig_pend) fprintf(stderr,"DOSMOD previous signal %d lost\n",sig_pend);
75  sig_pend=sig;
76  signal(sig,sig_handler);
77 }
78
79 void bad_handler(int sig)
80 {
81  fprintf(stderr,"DOSMOD caught fatal signal %d\n",sig);
82  fprintf(stderr,"(Last known VM86 CS:IP was %04x:%04lx)\n",VM86.regs.cs,VM86.regs.eip);
83  sig_pend=sig; sig_fatal++;
84  signal(sig,bad_handler);
85 }
86
87 int main(int argc,char**argv)
88 {
89  int mfd=open(argv[0],O_RDWR);
90  struct timeval tim;
91  int func,ret;
92  off_t fofs=0;
93  pid_t ppid=getppid();
94  
95 /* fprintf(stderr,"main is at %08lx, file is %s, fd=%d\n",(unsigned long)&main,argv[0],mfd); */
96  if (mfd<0) return 1;
97 /* Map in our DOS image at the start of the process address space */
98  if (argv[1]) {
99   /* Ulrich Weigand suggested mapping in the DOS image directly from the Wine
100      address space */
101   fofs=atol(argv[1]);
102   /* linux currently only allows mapping a process memory if it's being ptraced */
103   /* Linus doesn't like it, so this probably won't work in the future */
104   /* it doesn't even work for me right now */
105   kill(ppid,SIGSTOP);
106   ptrace(PTRACE_ATTACH,ppid,0,0);
107   waitpid(ppid,NULL,0);
108  }
109  img=mmap(NULL,0x110000,PROT_EXEC|PROT_READ|PROT_WRITE,MAP_FIXED|MAP_SHARED,mfd,fofs);
110  if (argv[1]) {
111   ptrace(PTRACE_DETACH,ppid,0,0);
112   kill(ppid,SIGCONT);
113  }
114  if (img==(void*)-1) {
115   fprintf(stderr,"DOS memory map failed, error=%s\n",strerror(errno));
116   fprintf(stderr,"in attempt to map %s, offset %08lX, length 110000, to offset 0\n",argv[0],fofs);
117   return 1;
118  }
119 /* initialize signals and system timer */
120  signal(SIGHUP,sig_handler);
121  signal(SIGINT,sig_handler);
122  signal(SIGUSR1,sig_handler);
123  signal(SIGUSR2,sig_handler);
124  signal(SIGALRM,sig_handler);
125
126  signal(SIGQUIT,bad_handler);
127  signal(SIGILL,bad_handler);
128  signal(SIGBUS,bad_handler);
129  signal(SIGFPE,bad_handler);
130  signal(SIGSEGV,bad_handler);
131  signal(SIGTERM,bad_handler);
132 #if 0
133  tim.tv_sec=0; tim.tv_usec=54925;
134  set_timer(&tim);
135 #endif
136 /* report back to the main program that we're ready */
137  ret=1; /* dosmod protocol revision 1 */
138  write(1,&ret,sizeof(ret));
139 /* context exchange loop */
140  do {
141   if (read(0,&func,sizeof(func))!=sizeof(func)) return 1;
142   if (func<0) break;
143   switch (func) {
144    case DOSMOD_SET_TIMER:
145     if (read(0,&tim,sizeof(tim))!=sizeof(tim)) return 1;
146     set_timer(&tim);
147     /* no response */
148     break;
149    case DOSMOD_ENTER:
150    default:
151     if (read(0,&VM86,sizeof(VM86))!=sizeof(VM86)) return 1;
152     if (sig_pend) ret=DOSMOD_SIGNAL; else
153      ret=vm86plus(func,&VM86);
154     if (write(1,&ret,sizeof(ret))!=sizeof(ret)) return 1;
155     if (write(1,&VM86,sizeof(VM86))!=sizeof(VM86)) return 1;
156     switch (ret&0xff) {
157      case DOSMOD_SIGNAL:
158       ret=sig_pend; sig_pend=0;
159       if (write(1,&ret,sizeof(ret))!=sizeof(ret)) return 1;
160       if (sig_fatal) return 1;
161       break;
162     }
163   }
164  } while (1);
165  return 0;
166 }
167
168 #else /* !linux-i386 */
169 int main(void) {return 1;}
170 #endif