2 * Simple example of use of vm86: launch a basic .com DOS executable
13 #include <linux/unistd.h>
18 #undef __syscall_return
19 #define __syscall_return(type, res) \
21 return (type) (res); \
24 _syscall2(int, vm86
, int, func
, struct vm86plus_struct
*, v86
)
26 #define COM_BASE_ADDR 0x10100
30 printf("runcom version 0.1 (c) 2003 Fabrice Bellard\n"
31 "usage: runcom file.com\n"
32 "VM86 Run simple .com DOS executables (linux vm86 test mode)\n");
36 static inline void set_bit(uint8_t *a
, unsigned int bit
)
38 a
[bit
/ 8] |= (1 << (bit
% 8));
41 static inline uint8_t *seg_to_linear(unsigned int seg
, unsigned int reg
)
43 return (uint8_t *)((seg
<< 4) + (reg
& 0xffff));
46 static inline void pushw(struct vm86_regs
*r
, int val
)
48 r
->esp
= (r
->esp
& ~0xffff) | ((r
->esp
- 2) & 0xffff);
49 *(uint16_t *)seg_to_linear(r
->ss
, r
->esp
) = val
;
52 void dump_regs(struct vm86_regs
*r
)
55 "EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
56 "ESI=%08lx EDI=%08lx EBP=%08lx ESP=%08lx\n"
57 "EIP=%08lx EFL=%08lx\n"
58 "CS=%04x DS=%04x ES=%04x SS=%04x FS=%04x GS=%04x\n",
59 r
->eax
, r
->ebx
, r
->ecx
, r
->edx
, r
->esi
, r
->edi
, r
->ebp
, r
->esp
,
61 r
->cs
, r
->ds
, r
->es
, r
->ss
, r
->fs
, r
->gs
);
65 void alarm_handler(int sig
)
67 fprintf(stderr
, "alarm signal=%d\n", sig
);
72 int main(int argc
, char **argv
)
77 struct vm86plus_struct ctx
;
84 vm86_mem
= mmap((void *)0x00000000, 0x110000,
85 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
86 MAP_FIXED
| MAP_ANON
| MAP_PRIVATE
, -1, 0);
87 if (vm86_mem
== MAP_FAILED
) {
95 act
.sa_handler
= alarm_handler
;
96 sigemptyset(&act
.sa_mask
);
98 sigaction(SIGALRM
, &act
, NULL
);
103 /* load the MSDOS .com executable */
104 fd
= open(filename
, O_RDONLY
);
109 ret
= read(fd
, vm86_mem
+ COM_BASE_ADDR
, 65536 - 256);
116 memset(&ctx
, 0, sizeof(ctx
));
117 /* init basic registers */
121 seg
= (COM_BASE_ADDR
- 0x100) >> 4;
128 r
->eflags
= VIF_MASK
;
130 /* put return code */
131 set_bit((uint8_t *)&ctx
.int_revectored
, 0x21);
132 *seg_to_linear(r
->cs
, 0) = 0xb4; /* mov ah, $0 */
133 *seg_to_linear(r
->cs
, 1) = 0x00;
134 *seg_to_linear(r
->cs
, 2) = 0xcd; /* int $0x21 */
135 *seg_to_linear(r
->cs
, 3) = 0x21;
136 pushw(&ctx
.regs
, 0x0000);
138 /* the value of these registers seem to be assumed by pi_10.com */
145 ret
= vm86(VM86_ENTER
, &ctx
);
146 switch(VM86_TYPE(ret
)) {
151 int_num
= VM86_ARG(ret
);
154 ah
= (r
->eax
>> 8) & 0xff;
156 case 0x00: /* exit */
158 case 0x02: /* write char */
164 case 0x09: /* write string */
168 c
= *seg_to_linear(r
->ds
, r
->edx
);
173 r
->eax
= (r
->eax
& ~0xff) | '$';
178 fprintf(stderr
, "unsupported int 0x%02x\n", int_num
);
179 dump_regs(&ctx
.regs
);
185 /* a signal came, we just ignore that */
190 fprintf(stderr
, "unhandled vm86 return code (0x%x)\n", ret
);
191 dump_regs(&ctx
.regs
);