root / tests / runcom.c @ fe1e3ce3
History | View | Annotate | Download (5 kB)
1 | 62296fe3 | bellard | /*
|
---|---|---|---|
2 | 62296fe3 | bellard | * Simple example of use of vm86: launch a basic .com DOS executable
|
3 | 62296fe3 | bellard | */
|
4 | 62296fe3 | bellard | #include <stdlib.h> |
5 | 62296fe3 | bellard | #include <stdio.h> |
6 | 7fb9a24e | bellard | #include <string.h> |
7 | 62296fe3 | bellard | #include <inttypes.h> |
8 | 62296fe3 | bellard | #include <unistd.h> |
9 | 62296fe3 | bellard | #include <fcntl.h> |
10 | 62296fe3 | bellard | #include <sys/mman.h> |
11 | 62296fe3 | bellard | #include <signal.h> |
12 | 62296fe3 | bellard | |
13 | 62296fe3 | bellard | #include <linux/unistd.h> |
14 | 62296fe3 | bellard | #include <asm/vm86.h> |
15 | 62296fe3 | bellard | |
16 | 62296fe3 | bellard | //#define SIGTEST
|
17 | 62296fe3 | bellard | |
18 | 7fb9a24e | bellard | #undef __syscall_return
|
19 | 7fb9a24e | bellard | #define __syscall_return(type, res) \
|
20 | 7fb9a24e | bellard | do { \
|
21 | 7fb9a24e | bellard | return (type) (res); \
|
22 | 7fb9a24e | bellard | } while (0) |
23 | 7fb9a24e | bellard | |
24 | 62296fe3 | bellard | _syscall2(int, vm86, int, func, struct vm86plus_struct *, v86) |
25 | 62296fe3 | bellard | |
26 | 62296fe3 | bellard | #define COM_BASE_ADDR 0x10100 |
27 | 62296fe3 | bellard | |
28 | 62296fe3 | bellard | void usage(void) |
29 | 62296fe3 | bellard | { |
30 | 62296fe3 | bellard | printf("runcom version 0.1 (c) 2003 Fabrice Bellard\n"
|
31 | 62296fe3 | bellard | "usage: runcom file.com\n"
|
32 | 62296fe3 | bellard | "VM86 Run simple .com DOS executables (linux vm86 test mode)\n");
|
33 | 62296fe3 | bellard | exit(1);
|
34 | 62296fe3 | bellard | } |
35 | 62296fe3 | bellard | |
36 | 62296fe3 | bellard | static inline void set_bit(uint8_t *a, unsigned int bit) |
37 | 62296fe3 | bellard | { |
38 | 62296fe3 | bellard | a[bit / 8] |= (1 << (bit % 8)); |
39 | 62296fe3 | bellard | } |
40 | 62296fe3 | bellard | |
41 | 62296fe3 | bellard | static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg) |
42 | 62296fe3 | bellard | { |
43 | 62296fe3 | bellard | return (uint8_t *)((seg << 4) + (reg & 0xffff)); |
44 | 62296fe3 | bellard | } |
45 | 62296fe3 | bellard | |
46 | 62296fe3 | bellard | static inline void pushw(struct vm86_regs *r, int val) |
47 | 62296fe3 | bellard | { |
48 | 62296fe3 | bellard | r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff); |
49 | 62296fe3 | bellard | *(uint16_t *)seg_to_linear(r->ss, r->esp) = val; |
50 | 62296fe3 | bellard | } |
51 | 62296fe3 | bellard | |
52 | 62296fe3 | bellard | void dump_regs(struct vm86_regs *r) |
53 | 62296fe3 | bellard | { |
54 | 62296fe3 | bellard | fprintf(stderr, |
55 | 62296fe3 | bellard | "EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
|
56 | 62296fe3 | bellard | "ESI=%08lx EDI=%08lx EBP=%08lx ESP=%08lx\n"
|
57 | 62296fe3 | bellard | "EIP=%08lx EFL=%08lx\n"
|
58 | 62296fe3 | bellard | "CS=%04x DS=%04x ES=%04x SS=%04x FS=%04x GS=%04x\n",
|
59 | 62296fe3 | bellard | r->eax, r->ebx, r->ecx, r->edx, r->esi, r->edi, r->ebp, r->esp, |
60 | 62296fe3 | bellard | r->eip, r->eflags, |
61 | 62296fe3 | bellard | r->cs, r->ds, r->es, r->ss, r->fs, r->gs); |
62 | 62296fe3 | bellard | } |
63 | 62296fe3 | bellard | |
64 | 62296fe3 | bellard | #ifdef SIGTEST
|
65 | 62296fe3 | bellard | void alarm_handler(int sig) |
66 | 62296fe3 | bellard | { |
67 | 62296fe3 | bellard | fprintf(stderr, "alarm signal=%d\n", sig);
|
68 | 62296fe3 | bellard | alarm(1);
|
69 | 62296fe3 | bellard | } |
70 | 62296fe3 | bellard | #endif
|
71 | 62296fe3 | bellard | |
72 | 62296fe3 | bellard | int main(int argc, char **argv) |
73 | 62296fe3 | bellard | { |
74 | 62296fe3 | bellard | uint8_t *vm86_mem; |
75 | 62296fe3 | bellard | const char *filename; |
76 | 62296fe3 | bellard | int fd, ret, seg;
|
77 | 62296fe3 | bellard | struct vm86plus_struct ctx;
|
78 | 62296fe3 | bellard | struct vm86_regs *r;
|
79 | 62296fe3 | bellard | |
80 | 62296fe3 | bellard | if (argc != 2) |
81 | 62296fe3 | bellard | usage(); |
82 | 62296fe3 | bellard | filename = argv[1];
|
83 | 62296fe3 | bellard | |
84 | 62296fe3 | bellard | vm86_mem = mmap((void *)0x00000000, 0x110000, |
85 | 62296fe3 | bellard | PROT_WRITE | PROT_READ | PROT_EXEC, |
86 | 62296fe3 | bellard | MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); |
87 | 62296fe3 | bellard | if (vm86_mem == MAP_FAILED) {
|
88 | 62296fe3 | bellard | perror("mmap");
|
89 | 62296fe3 | bellard | exit(1);
|
90 | 62296fe3 | bellard | } |
91 | 62296fe3 | bellard | #ifdef SIGTEST
|
92 | 62296fe3 | bellard | { |
93 | 62296fe3 | bellard | struct sigaction act;
|
94 | 62296fe3 | bellard | |
95 | 62296fe3 | bellard | act.sa_handler = alarm_handler; |
96 | 62296fe3 | bellard | sigemptyset(&act.sa_mask); |
97 | 62296fe3 | bellard | act.sa_flags = 0;
|
98 | 62296fe3 | bellard | sigaction(SIGALRM, &act, NULL);
|
99 | 62296fe3 | bellard | alarm(1);
|
100 | 62296fe3 | bellard | } |
101 | 62296fe3 | bellard | #endif
|
102 | 62296fe3 | bellard | |
103 | 62296fe3 | bellard | /* load the MSDOS .com executable */
|
104 | 62296fe3 | bellard | fd = open(filename, O_RDONLY); |
105 | 62296fe3 | bellard | if (fd < 0) { |
106 | 62296fe3 | bellard | perror(filename); |
107 | 62296fe3 | bellard | exit(1);
|
108 | 62296fe3 | bellard | } |
109 | 62296fe3 | bellard | ret = read(fd, vm86_mem + COM_BASE_ADDR, 65536 - 256); |
110 | 62296fe3 | bellard | if (ret < 0) { |
111 | 62296fe3 | bellard | perror("read");
|
112 | 62296fe3 | bellard | exit(1);
|
113 | 62296fe3 | bellard | } |
114 | 62296fe3 | bellard | close(fd); |
115 | 62296fe3 | bellard | |
116 | 62296fe3 | bellard | memset(&ctx, 0, sizeof(ctx)); |
117 | 62296fe3 | bellard | /* init basic registers */
|
118 | 62296fe3 | bellard | r = &ctx.regs; |
119 | 62296fe3 | bellard | r->eip = 0x100;
|
120 | 62296fe3 | bellard | r->esp = 0xfffe;
|
121 | 62296fe3 | bellard | seg = (COM_BASE_ADDR - 0x100) >> 4; |
122 | 62296fe3 | bellard | r->cs = seg; |
123 | 62296fe3 | bellard | r->ss = seg; |
124 | 62296fe3 | bellard | r->ds = seg; |
125 | 62296fe3 | bellard | r->es = seg; |
126 | 62296fe3 | bellard | r->fs = seg; |
127 | 62296fe3 | bellard | r->gs = seg; |
128 | 62296fe3 | bellard | r->eflags = (IF_MASK | IOPL_MASK); |
129 | 62296fe3 | bellard | |
130 | 62296fe3 | bellard | /* put return code */
|
131 | 62296fe3 | bellard | set_bit((uint8_t *)&ctx.int_revectored, 0x21);
|
132 | 62296fe3 | bellard | *seg_to_linear(r->cs, 0) = 0xb4; /* mov ah, $0 */ |
133 | 62296fe3 | bellard | *seg_to_linear(r->cs, 1) = 0x00; |
134 | 62296fe3 | bellard | *seg_to_linear(r->cs, 2) = 0xcd; /* int $0x21 */ |
135 | 62296fe3 | bellard | *seg_to_linear(r->cs, 3) = 0x21; |
136 | 62296fe3 | bellard | pushw(&ctx.regs, 0x0000);
|
137 | 62296fe3 | bellard | |
138 | 62296fe3 | bellard | /* the value of these registers seem to be assumed by pi_10.com */
|
139 | 62296fe3 | bellard | r->esi = 0x100;
|
140 | 62296fe3 | bellard | r->ecx = 0xff;
|
141 | 62296fe3 | bellard | r->ebp = 0x0900;
|
142 | 62296fe3 | bellard | r->edi = 0xfffe;
|
143 | 62296fe3 | bellard | |
144 | 62296fe3 | bellard | for(;;) {
|
145 | 62296fe3 | bellard | ret = vm86(VM86_ENTER, &ctx); |
146 | 62296fe3 | bellard | switch(VM86_TYPE(ret)) {
|
147 | 62296fe3 | bellard | case VM86_INTx:
|
148 | 62296fe3 | bellard | { |
149 | 62296fe3 | bellard | int int_num, ah;
|
150 | 62296fe3 | bellard | |
151 | 62296fe3 | bellard | int_num = VM86_ARG(ret); |
152 | 62296fe3 | bellard | if (int_num != 0x21) |
153 | 62296fe3 | bellard | goto unknown_int;
|
154 | 62296fe3 | bellard | ah = (r->eax >> 8) & 0xff; |
155 | 62296fe3 | bellard | switch(ah) {
|
156 | 62296fe3 | bellard | case 0x00: /* exit */ |
157 | 62296fe3 | bellard | exit(0);
|
158 | 62296fe3 | bellard | case 0x02: /* write char */ |
159 | 62296fe3 | bellard | { |
160 | 62296fe3 | bellard | uint8_t c = r->edx; |
161 | 62296fe3 | bellard | write(1, &c, 1); |
162 | 62296fe3 | bellard | } |
163 | 62296fe3 | bellard | break;
|
164 | 62296fe3 | bellard | case 0x09: /* write string */ |
165 | 62296fe3 | bellard | { |
166 | 62296fe3 | bellard | uint8_t c; |
167 | 62296fe3 | bellard | for(;;) {
|
168 | 62296fe3 | bellard | c = *seg_to_linear(r->ds, r->edx); |
169 | 62296fe3 | bellard | if (c == '$') |
170 | 62296fe3 | bellard | break;
|
171 | 62296fe3 | bellard | write(1, &c, 1); |
172 | 62296fe3 | bellard | } |
173 | 62296fe3 | bellard | r->eax = (r->eax & ~0xff) | '$'; |
174 | 62296fe3 | bellard | } |
175 | 62296fe3 | bellard | break;
|
176 | 62296fe3 | bellard | default:
|
177 | 62296fe3 | bellard | unknown_int:
|
178 | 62296fe3 | bellard | fprintf(stderr, "unsupported int 0x%02x\n", int_num);
|
179 | 62296fe3 | bellard | dump_regs(&ctx.regs); |
180 | 62296fe3 | bellard | // exit(1);
|
181 | 62296fe3 | bellard | } |
182 | 62296fe3 | bellard | } |
183 | 62296fe3 | bellard | break;
|
184 | 62296fe3 | bellard | case VM86_SIGNAL:
|
185 | 62296fe3 | bellard | /* a signal came, we just ignore that */
|
186 | 62296fe3 | bellard | break;
|
187 | 62296fe3 | bellard | case VM86_STI:
|
188 | 62296fe3 | bellard | break;
|
189 | 62296fe3 | bellard | default:
|
190 | 62296fe3 | bellard | fprintf(stderr, "unhandled vm86 return code (0x%x)\n", ret);
|
191 | 62296fe3 | bellard | dump_regs(&ctx.regs); |
192 | 62296fe3 | bellard | exit(1);
|
193 | 62296fe3 | bellard | } |
194 | 62296fe3 | bellard | } |
195 | 62296fe3 | bellard | } |