2 * Simple example of use of vm86: launch a basic .com DOS executable
13 #include <linux/unistd.h>
16 extern int vm86 (unsigned long int subfunction
,
17 struct vm86plus_struct
*info
);
19 #define VIF_MASK 0x00080000
23 #define COM_BASE_ADDR 0x10100
25 static void usage(void)
27 printf("runcom version 0.1 (c) 2003 Fabrice Bellard\n"
28 "usage: runcom file.com\n"
29 "VM86 Run simple .com DOS executables (linux vm86 test mode)\n");
33 static inline void set_bit(uint8_t *a
, unsigned int bit
)
35 a
[bit
/ 8] |= (1 << (bit
% 8));
38 static inline uint8_t *seg_to_linear(unsigned int seg
, unsigned int reg
)
40 return (uint8_t *)((seg
<< 4) + (reg
& 0xffff));
43 static inline void pushw(struct vm86_regs
*r
, int val
)
45 r
->esp
= (r
->esp
& ~0xffff) | ((r
->esp
- 2) & 0xffff);
46 *(uint16_t *)seg_to_linear(r
->ss
, r
->esp
) = val
;
49 void dump_regs(struct vm86_regs
*r
)
52 "EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
53 "ESI=%08lx EDI=%08lx EBP=%08lx ESP=%08lx\n"
54 "EIP=%08lx EFL=%08lx\n"
55 "CS=%04x DS=%04x ES=%04x SS=%04x FS=%04x GS=%04x\n",
56 r
->eax
, r
->ebx
, r
->ecx
, r
->edx
, r
->esi
, r
->edi
, r
->ebp
, r
->esp
,
58 r
->cs
, r
->ds
, r
->es
, r
->ss
, r
->fs
, r
->gs
);
62 void alarm_handler(int sig
)
64 fprintf(stderr
, "alarm signal=%d\n", sig
);
69 int main(int argc
, char **argv
)
74 struct vm86plus_struct ctx
;
81 vm86_mem
= mmap((void *)0x00000000, 0x110000,
82 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
83 MAP_FIXED
| MAP_ANON
| MAP_PRIVATE
, -1, 0);
84 if (vm86_mem
== MAP_FAILED
) {
92 act
.sa_handler
= alarm_handler
;
93 sigemptyset(&act
.sa_mask
);
95 sigaction(SIGALRM
, &act
, NULL
);
100 /* load the MSDOS .com executable */
101 fd
= open(filename
, O_RDONLY
);
106 ret
= read(fd
, vm86_mem
+ COM_BASE_ADDR
, 65536 - 256);
113 memset(&ctx
, 0, sizeof(ctx
));
114 /* init basic registers */
118 seg
= (COM_BASE_ADDR
- 0x100) >> 4;
125 r
->eflags
= VIF_MASK
;
127 /* put return code */
128 set_bit((uint8_t *)&ctx
.int_revectored
, 0x21);
129 *seg_to_linear(r
->cs
, 0) = 0xb4; /* mov ah, $0 */
130 *seg_to_linear(r
->cs
, 1) = 0x00;
131 *seg_to_linear(r
->cs
, 2) = 0xcd; /* int $0x21 */
132 *seg_to_linear(r
->cs
, 3) = 0x21;
133 pushw(&ctx
.regs
, 0x0000);
135 /* the value of these registers seem to be assumed by pi_10.com */
142 ret
= vm86(VM86_ENTER
, &ctx
);
143 switch(VM86_TYPE(ret
)) {
148 int_num
= VM86_ARG(ret
);
151 ah
= (r
->eax
>> 8) & 0xff;
153 case 0x00: /* exit */
155 case 0x02: /* write char */
161 case 0x09: /* write string */
165 c
= *seg_to_linear(r
->ds
, r
->edx
);
170 r
->eax
= (r
->eax
& ~0xff) | '$';
175 fprintf(stderr
, "unsupported int 0x%02x\n", int_num
);
176 dump_regs(&ctx
.regs
);
182 /* a signal came, we just ignore that */
187 fprintf(stderr
, "unhandled vm86 return code (0x%x)\n", ret
);
188 dump_regs(&ctx
.regs
);