root / tests / qruncom.c @ 0443eaf6
History | View | Annotate | Download (8 kB)
1 | 039d3da3 | bellard | /*
|
---|---|---|---|
2 | 039d3da3 | bellard | * Example of use of user mode libqemu: launch a basic .com DOS
|
3 | 039d3da3 | bellard | * executable
|
4 | 039d3da3 | bellard | */
|
5 | 039d3da3 | bellard | #include <stdlib.h> |
6 | 039d3da3 | bellard | #include <stdio.h> |
7 | 039d3da3 | bellard | #include <string.h> |
8 | 039d3da3 | bellard | #include <inttypes.h> |
9 | 039d3da3 | bellard | #include <unistd.h> |
10 | 039d3da3 | bellard | #include <fcntl.h> |
11 | 039d3da3 | bellard | #include <sys/mman.h> |
12 | 039d3da3 | bellard | #include <signal.h> |
13 | 039d3da3 | bellard | |
14 | 039d3da3 | bellard | #include "cpu.h" |
15 | 039d3da3 | bellard | |
16 | 039d3da3 | bellard | //#define SIGTEST
|
17 | 039d3da3 | bellard | |
18 | 039d3da3 | bellard | CPUState *cpu_single_env = NULL;
|
19 | 039d3da3 | bellard | |
20 | 039d3da3 | bellard | void cpu_outb(CPUState *env, int addr, int val) |
21 | 039d3da3 | bellard | { |
22 | 039d3da3 | bellard | fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
|
23 | 039d3da3 | bellard | } |
24 | 039d3da3 | bellard | |
25 | 039d3da3 | bellard | void cpu_outw(CPUState *env, int addr, int val) |
26 | 039d3da3 | bellard | { |
27 | 039d3da3 | bellard | fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
|
28 | 039d3da3 | bellard | } |
29 | 039d3da3 | bellard | |
30 | 039d3da3 | bellard | void cpu_outl(CPUState *env, int addr, int val) |
31 | 039d3da3 | bellard | { |
32 | 039d3da3 | bellard | fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
|
33 | 039d3da3 | bellard | } |
34 | 039d3da3 | bellard | |
35 | 039d3da3 | bellard | int cpu_inb(CPUState *env, int addr) |
36 | 039d3da3 | bellard | { |
37 | 039d3da3 | bellard | fprintf(stderr, "inb: port=0x%04x\n", addr);
|
38 | 039d3da3 | bellard | return 0; |
39 | 039d3da3 | bellard | } |
40 | 039d3da3 | bellard | |
41 | 039d3da3 | bellard | int cpu_inw(CPUState *env, int addr) |
42 | 039d3da3 | bellard | { |
43 | 039d3da3 | bellard | fprintf(stderr, "inw: port=0x%04x\n", addr);
|
44 | 039d3da3 | bellard | return 0; |
45 | 039d3da3 | bellard | } |
46 | 039d3da3 | bellard | |
47 | 039d3da3 | bellard | int cpu_inl(CPUState *env, int addr) |
48 | 039d3da3 | bellard | { |
49 | 039d3da3 | bellard | fprintf(stderr, "inl: port=0x%04x\n", addr);
|
50 | 039d3da3 | bellard | return 0; |
51 | 039d3da3 | bellard | } |
52 | 039d3da3 | bellard | |
53 | 039d3da3 | bellard | int cpu_get_pic_interrupt(CPUState *env)
|
54 | 039d3da3 | bellard | { |
55 | 039d3da3 | bellard | return -1; |
56 | 039d3da3 | bellard | } |
57 | 039d3da3 | bellard | |
58 | 28ab0e2e | bellard | uint64_t cpu_get_tsc(CPUState *env) |
59 | 28ab0e2e | bellard | { |
60 | 28ab0e2e | bellard | return 0; |
61 | 28ab0e2e | bellard | } |
62 | 28ab0e2e | bellard | |
63 | 039d3da3 | bellard | static void set_gate(void *ptr, unsigned int type, unsigned int dpl, |
64 | 039d3da3 | bellard | unsigned long addr, unsigned int sel) |
65 | 039d3da3 | bellard | { |
66 | 039d3da3 | bellard | unsigned int e1, e2; |
67 | 039d3da3 | bellard | e1 = (addr & 0xffff) | (sel << 16); |
68 | 039d3da3 | bellard | e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); |
69 | 039d3da3 | bellard | stl((uint8_t *)ptr, e1); |
70 | 039d3da3 | bellard | stl((uint8_t *)ptr + 4, e2);
|
71 | 039d3da3 | bellard | } |
72 | 039d3da3 | bellard | |
73 | 039d3da3 | bellard | uint64_t idt_table[256];
|
74 | 039d3da3 | bellard | |
75 | 039d3da3 | bellard | /* only dpl matters as we do only user space emulation */
|
76 | 039d3da3 | bellard | static void set_idt(int n, unsigned int dpl) |
77 | 039d3da3 | bellard | { |
78 | 039d3da3 | bellard | set_gate(idt_table + n, 0, dpl, 0, 0); |
79 | 039d3da3 | bellard | } |
80 | 039d3da3 | bellard | |
81 | 039d3da3 | bellard | void qemu_free(void *ptr) |
82 | 039d3da3 | bellard | { |
83 | 039d3da3 | bellard | free(ptr); |
84 | 039d3da3 | bellard | } |
85 | 039d3da3 | bellard | |
86 | 039d3da3 | bellard | void *qemu_malloc(size_t size)
|
87 | 039d3da3 | bellard | { |
88 | 039d3da3 | bellard | return malloc(size);
|
89 | 039d3da3 | bellard | } |
90 | 039d3da3 | bellard | |
91 | 039d3da3 | bellard | void qemu_printf(const char *fmt, ...) |
92 | 039d3da3 | bellard | { |
93 | 039d3da3 | bellard | va_list ap; |
94 | 039d3da3 | bellard | va_start(ap, fmt); |
95 | 039d3da3 | bellard | vprintf(fmt, ap); |
96 | 039d3da3 | bellard | va_end(ap); |
97 | 039d3da3 | bellard | } |
98 | 039d3da3 | bellard | |
99 | 039d3da3 | bellard | /* XXX: this is a bug in helper2.c */
|
100 | 039d3da3 | bellard | int errno;
|
101 | 039d3da3 | bellard | |
102 | 039d3da3 | bellard | /**********************************************/
|
103 | 039d3da3 | bellard | |
104 | 039d3da3 | bellard | #define COM_BASE_ADDR 0x10100 |
105 | 039d3da3 | bellard | |
106 | 039d3da3 | bellard | void usage(void) |
107 | 039d3da3 | bellard | { |
108 | 039d3da3 | bellard | printf("qruncom version 0.1 (c) 2003 Fabrice Bellard\n"
|
109 | 039d3da3 | bellard | "usage: qruncom file.com\n"
|
110 | 039d3da3 | bellard | "user mode libqemu demo: run simple .com DOS executables\n");
|
111 | 039d3da3 | bellard | exit(1);
|
112 | 039d3da3 | bellard | } |
113 | 039d3da3 | bellard | |
114 | 039d3da3 | bellard | static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg) |
115 | 039d3da3 | bellard | { |
116 | 039d3da3 | bellard | return (uint8_t *)((seg << 4) + (reg & 0xffff)); |
117 | 039d3da3 | bellard | } |
118 | 039d3da3 | bellard | |
119 | 039d3da3 | bellard | static inline void pushw(CPUState *env, int val) |
120 | 039d3da3 | bellard | { |
121 | 039d3da3 | bellard | env->regs[R_ESP] = (env->regs[R_ESP] & ~0xffff) | ((env->regs[R_ESP] - 2) & 0xffff); |
122 | 039d3da3 | bellard | *(uint16_t *)seg_to_linear(env->segs[R_SS].selector, env->regs[R_ESP]) = val; |
123 | 039d3da3 | bellard | } |
124 | 039d3da3 | bellard | |
125 | 039d3da3 | bellard | static void host_segv_handler(int host_signum, siginfo_t *info, |
126 | 039d3da3 | bellard | void *puc)
|
127 | 039d3da3 | bellard | { |
128 | 039d3da3 | bellard | if (cpu_signal_handler(host_signum, info, puc)) {
|
129 | 039d3da3 | bellard | return;
|
130 | 039d3da3 | bellard | } |
131 | 039d3da3 | bellard | abort(); |
132 | 039d3da3 | bellard | } |
133 | 039d3da3 | bellard | |
134 | 039d3da3 | bellard | int main(int argc, char **argv) |
135 | 039d3da3 | bellard | { |
136 | 039d3da3 | bellard | uint8_t *vm86_mem; |
137 | 039d3da3 | bellard | const char *filename; |
138 | 039d3da3 | bellard | int fd, ret, seg;
|
139 | 039d3da3 | bellard | CPUState *env; |
140 | 039d3da3 | bellard | |
141 | 039d3da3 | bellard | if (argc != 2) |
142 | 039d3da3 | bellard | usage(); |
143 | 039d3da3 | bellard | filename = argv[1];
|
144 | 039d3da3 | bellard | |
145 | 039d3da3 | bellard | vm86_mem = mmap((void *)0x00000000, 0x110000, |
146 | 039d3da3 | bellard | PROT_WRITE | PROT_READ | PROT_EXEC, |
147 | 039d3da3 | bellard | MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); |
148 | 039d3da3 | bellard | if (vm86_mem == MAP_FAILED) {
|
149 | 039d3da3 | bellard | perror("mmap");
|
150 | 039d3da3 | bellard | exit(1);
|
151 | 039d3da3 | bellard | } |
152 | 039d3da3 | bellard | |
153 | 039d3da3 | bellard | /* load the MSDOS .com executable */
|
154 | 039d3da3 | bellard | fd = open(filename, O_RDONLY); |
155 | 039d3da3 | bellard | if (fd < 0) { |
156 | 039d3da3 | bellard | perror(filename); |
157 | 039d3da3 | bellard | exit(1);
|
158 | 039d3da3 | bellard | } |
159 | 039d3da3 | bellard | ret = read(fd, vm86_mem + COM_BASE_ADDR, 65536 - 256); |
160 | 039d3da3 | bellard | if (ret < 0) { |
161 | 039d3da3 | bellard | perror("read");
|
162 | 039d3da3 | bellard | exit(1);
|
163 | 039d3da3 | bellard | } |
164 | 039d3da3 | bellard | close(fd); |
165 | 039d3da3 | bellard | |
166 | 039d3da3 | bellard | /* install exception handler for CPU emulator */
|
167 | 039d3da3 | bellard | { |
168 | 039d3da3 | bellard | struct sigaction act;
|
169 | 039d3da3 | bellard | |
170 | 039d3da3 | bellard | sigfillset(&act.sa_mask); |
171 | 039d3da3 | bellard | act.sa_flags = SA_SIGINFO; |
172 | 039d3da3 | bellard | // act.sa_flags |= SA_ONSTACK;
|
173 | 039d3da3 | bellard | |
174 | 039d3da3 | bellard | act.sa_sigaction = host_segv_handler; |
175 | 039d3da3 | bellard | sigaction(SIGSEGV, &act, NULL);
|
176 | 039d3da3 | bellard | sigaction(SIGBUS, &act, NULL);
|
177 | 039d3da3 | bellard | #if defined (TARGET_I386) && defined(USE_CODE_COPY)
|
178 | 039d3da3 | bellard | sigaction(SIGFPE, &act, NULL);
|
179 | 039d3da3 | bellard | #endif
|
180 | 039d3da3 | bellard | } |
181 | 039d3da3 | bellard | |
182 | 039d3da3 | bellard | // cpu_set_log(CPU_LOG_TB_IN_ASM | CPU_LOG_TB_OUT_ASM | CPU_LOG_EXEC);
|
183 | 039d3da3 | bellard | |
184 | 039d3da3 | bellard | env = cpu_init(); |
185 | 039d3da3 | bellard | |
186 | 039d3da3 | bellard | /* disable code copy to simplify debugging */
|
187 | 039d3da3 | bellard | code_copy_enabled = 0;
|
188 | 039d3da3 | bellard | |
189 | 039d3da3 | bellard | /* set user mode state (XXX: should be done automatically by
|
190 | 039d3da3 | bellard | cpu_init ?) */
|
191 | 039d3da3 | bellard | env->user_mode_only = 1;
|
192 | 039d3da3 | bellard | |
193 | 039d3da3 | bellard | cpu_x86_set_cpl(env, 3);
|
194 | 039d3da3 | bellard | |
195 | 039d3da3 | bellard | env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
|
196 | 039d3da3 | bellard | /* NOTE: hflags duplicates some of the virtual CPU state */
|
197 | 039d3da3 | bellard | env->hflags |= HF_PE_MASK | VM_MASK; |
198 | 039d3da3 | bellard | |
199 | 039d3da3 | bellard | /* flags setup : we activate the IRQs by default as in user
|
200 | 039d3da3 | bellard | mode. We also activate the VM86 flag to run DOS code */
|
201 | 039d3da3 | bellard | env->eflags |= IF_MASK | VM_MASK; |
202 | 039d3da3 | bellard | |
203 | 039d3da3 | bellard | /* init basic registers */
|
204 | 039d3da3 | bellard | env->eip = 0x100;
|
205 | 039d3da3 | bellard | env->regs[R_ESP] = 0xfffe;
|
206 | 039d3da3 | bellard | seg = (COM_BASE_ADDR - 0x100) >> 4; |
207 | 039d3da3 | bellard | |
208 | 039d3da3 | bellard | cpu_x86_load_seg_cache(env, R_CS, seg, |
209 | 039d3da3 | bellard | (uint8_t *)(seg << 4), 0xffff, 0); |
210 | 039d3da3 | bellard | cpu_x86_load_seg_cache(env, R_SS, seg, |
211 | 039d3da3 | bellard | (uint8_t *)(seg << 4), 0xffff, 0); |
212 | 039d3da3 | bellard | cpu_x86_load_seg_cache(env, R_DS, seg, |
213 | 039d3da3 | bellard | (uint8_t *)(seg << 4), 0xffff, 0); |
214 | 039d3da3 | bellard | cpu_x86_load_seg_cache(env, R_ES, seg, |
215 | 039d3da3 | bellard | (uint8_t *)(seg << 4), 0xffff, 0); |
216 | 039d3da3 | bellard | cpu_x86_load_seg_cache(env, R_FS, seg, |
217 | 039d3da3 | bellard | (uint8_t *)(seg << 4), 0xffff, 0); |
218 | 039d3da3 | bellard | cpu_x86_load_seg_cache(env, R_GS, seg, |
219 | 039d3da3 | bellard | (uint8_t *)(seg << 4), 0xffff, 0); |
220 | 039d3da3 | bellard | |
221 | 039d3da3 | bellard | /* exception support */
|
222 | 039d3da3 | bellard | env->idt.base = (void *)idt_table;
|
223 | 039d3da3 | bellard | env->idt.limit = sizeof(idt_table) - 1; |
224 | 039d3da3 | bellard | set_idt(0, 0); |
225 | 039d3da3 | bellard | set_idt(1, 0); |
226 | 039d3da3 | bellard | set_idt(2, 0); |
227 | 039d3da3 | bellard | set_idt(3, 3); |
228 | 039d3da3 | bellard | set_idt(4, 3); |
229 | 039d3da3 | bellard | set_idt(5, 3); |
230 | 039d3da3 | bellard | set_idt(6, 0); |
231 | 039d3da3 | bellard | set_idt(7, 0); |
232 | 039d3da3 | bellard | set_idt(8, 0); |
233 | 039d3da3 | bellard | set_idt(9, 0); |
234 | 039d3da3 | bellard | set_idt(10, 0); |
235 | 039d3da3 | bellard | set_idt(11, 0); |
236 | 039d3da3 | bellard | set_idt(12, 0); |
237 | 039d3da3 | bellard | set_idt(13, 0); |
238 | 039d3da3 | bellard | set_idt(14, 0); |
239 | 039d3da3 | bellard | set_idt(15, 0); |
240 | 039d3da3 | bellard | set_idt(16, 0); |
241 | 039d3da3 | bellard | set_idt(17, 0); |
242 | 039d3da3 | bellard | set_idt(18, 0); |
243 | 039d3da3 | bellard | set_idt(19, 0); |
244 | 039d3da3 | bellard | |
245 | 039d3da3 | bellard | /* put return code */
|
246 | 039d3da3 | bellard | *seg_to_linear(env->segs[R_CS].selector, 0) = 0xb4; /* mov ah, $0 */ |
247 | 039d3da3 | bellard | *seg_to_linear(env->segs[R_CS].selector, 1) = 0x00; |
248 | 039d3da3 | bellard | *seg_to_linear(env->segs[R_CS].selector, 2) = 0xcd; /* int $0x21 */ |
249 | 039d3da3 | bellard | *seg_to_linear(env->segs[R_CS].selector, 3) = 0x21; |
250 | 039d3da3 | bellard | pushw(env, 0x0000);
|
251 | 039d3da3 | bellard | |
252 | 039d3da3 | bellard | /* the value of these registers seem to be assumed by pi_10.com */
|
253 | 039d3da3 | bellard | env->regs[R_ESI] = 0x100;
|
254 | 039d3da3 | bellard | env->regs[R_ECX] = 0xff;
|
255 | 039d3da3 | bellard | env->regs[R_EBP] = 0x0900;
|
256 | 039d3da3 | bellard | env->regs[R_EDI] = 0xfffe;
|
257 | 039d3da3 | bellard | |
258 | 039d3da3 | bellard | /* inform the emulator of the mmaped memory */
|
259 | 039d3da3 | bellard | page_set_flags(0x00000000, 0x110000, |
260 | 039d3da3 | bellard | PAGE_WRITE | PAGE_READ | PAGE_EXEC | PAGE_VALID); |
261 | 039d3da3 | bellard | |
262 | 039d3da3 | bellard | for(;;) {
|
263 | 039d3da3 | bellard | ret = cpu_x86_exec(env); |
264 | 039d3da3 | bellard | switch(ret) {
|
265 | 039d3da3 | bellard | case EXCP0D_GPF:
|
266 | 039d3da3 | bellard | { |
267 | 039d3da3 | bellard | int int_num, ah;
|
268 | 039d3da3 | bellard | int_num = *(env->segs[R_CS].base + env->eip + 1);
|
269 | 039d3da3 | bellard | if (int_num != 0x21) |
270 | 039d3da3 | bellard | goto unknown_int;
|
271 | 039d3da3 | bellard | ah = (env->regs[R_EAX] >> 8) & 0xff; |
272 | 039d3da3 | bellard | switch(ah) {
|
273 | 039d3da3 | bellard | case 0x00: /* exit */ |
274 | 039d3da3 | bellard | exit(0);
|
275 | 039d3da3 | bellard | case 0x02: /* write char */ |
276 | 039d3da3 | bellard | { |
277 | 039d3da3 | bellard | uint8_t c = env->regs[R_EDX]; |
278 | 039d3da3 | bellard | write(1, &c, 1); |
279 | 039d3da3 | bellard | } |
280 | 039d3da3 | bellard | break;
|
281 | 039d3da3 | bellard | case 0x09: /* write string */ |
282 | 039d3da3 | bellard | { |
283 | 039d3da3 | bellard | uint8_t c; |
284 | 039d3da3 | bellard | for(;;) {
|
285 | 039d3da3 | bellard | c = *seg_to_linear(env->segs[R_DS].selector, env->regs[R_EAX]); |
286 | 039d3da3 | bellard | if (c == '$') |
287 | 039d3da3 | bellard | break;
|
288 | 039d3da3 | bellard | write(1, &c, 1); |
289 | 039d3da3 | bellard | } |
290 | 039d3da3 | bellard | env->regs[R_EAX] = (env->regs[R_EAX] & ~0xff) | '$'; |
291 | 039d3da3 | bellard | } |
292 | 039d3da3 | bellard | break;
|
293 | 039d3da3 | bellard | default:
|
294 | 039d3da3 | bellard | unknown_int:
|
295 | 039d3da3 | bellard | fprintf(stderr, "unsupported int 0x%02x\n", int_num);
|
296 | 039d3da3 | bellard | cpu_dump_state(env, stderr, 0);
|
297 | 039d3da3 | bellard | // exit(1);
|
298 | 039d3da3 | bellard | } |
299 | 039d3da3 | bellard | env->eip += 2;
|
300 | 039d3da3 | bellard | } |
301 | 039d3da3 | bellard | break;
|
302 | 039d3da3 | bellard | default:
|
303 | 039d3da3 | bellard | fprintf(stderr, "unhandled cpu_exec return code (0x%x)\n", ret);
|
304 | 039d3da3 | bellard | cpu_dump_state(env, stderr, 0);
|
305 | 039d3da3 | bellard | exit(1);
|
306 | 039d3da3 | bellard | } |
307 | 039d3da3 | bellard | } |
308 | 039d3da3 | bellard | } |