]>
Commit | Line | Data |
---|---|---|
31e31b8a | 1 | /* |
3ef693a0 | 2 | * qemu main |
31e31b8a FB |
3 | * |
4 | * Copyright (c) 2003 Fabrice Bellard | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | #include <stdlib.h> | |
21 | #include <stdio.h> | |
22 | #include <stdarg.h> | |
04369ff2 | 23 | #include <string.h> |
31e31b8a | 24 | #include <errno.h> |
0ecfa993 | 25 | #include <unistd.h> |
31e31b8a | 26 | |
3ef693a0 | 27 | #include "qemu.h" |
31e31b8a | 28 | |
0ecfa993 | 29 | #include "cpu-i386.h" |
31e31b8a | 30 | |
3ef693a0 | 31 | #define DEBUG_LOGFILE "/tmp/qemu.log" |
586314f2 FB |
32 | |
33 | FILE *logfile = NULL; | |
34 | int loglevel; | |
d691f669 | 35 | const char *interp_prefix = CONFIG_QEMU_PREFIX "/qemu-i386"; |
586314f2 | 36 | |
9de5e440 FB |
37 | /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so |
38 | we allocate a bigger stack. Need a better solution, for example | |
39 | by remapping the process stack directly at the right place */ | |
40 | unsigned long x86_stack_size = 512 * 1024; | |
31e31b8a FB |
41 | unsigned long stktop; |
42 | ||
43 | void gemu_log(const char *fmt, ...) | |
44 | { | |
45 | va_list ap; | |
46 | ||
47 | va_start(ap, fmt); | |
48 | vfprintf(stderr, fmt, ap); | |
49 | va_end(ap); | |
50 | } | |
51 | ||
31e31b8a | 52 | /***********************************************************/ |
0ecfa993 | 53 | /* CPUX86 core interface */ |
367e86e8 | 54 | |
ba1c6e37 | 55 | void cpu_x86_outb(int addr, int val) |
367e86e8 FB |
56 | { |
57 | fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val); | |
58 | } | |
59 | ||
ba1c6e37 | 60 | void cpu_x86_outw(int addr, int val) |
367e86e8 FB |
61 | { |
62 | fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val); | |
63 | } | |
64 | ||
ba1c6e37 | 65 | void cpu_x86_outl(int addr, int val) |
367e86e8 FB |
66 | { |
67 | fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val); | |
68 | } | |
69 | ||
ba1c6e37 | 70 | int cpu_x86_inb(int addr) |
367e86e8 FB |
71 | { |
72 | fprintf(stderr, "inb: port=0x%04x\n", addr); | |
73 | return 0; | |
74 | } | |
75 | ||
ba1c6e37 | 76 | int cpu_x86_inw(int addr) |
367e86e8 FB |
77 | { |
78 | fprintf(stderr, "inw: port=0x%04x\n", addr); | |
79 | return 0; | |
80 | } | |
81 | ||
ba1c6e37 | 82 | int cpu_x86_inl(int addr) |
367e86e8 FB |
83 | { |
84 | fprintf(stderr, "inl: port=0x%04x\n", addr); | |
85 | return 0; | |
86 | } | |
87 | ||
6dbad63e FB |
88 | void write_dt(void *ptr, unsigned long addr, unsigned long limit, |
89 | int seg32_bit) | |
90 | { | |
91 | unsigned int e1, e2, limit_in_pages; | |
92 | limit_in_pages = 0; | |
93 | if (limit > 0xffff) { | |
94 | limit = limit >> 12; | |
95 | limit_in_pages = 1; | |
96 | } | |
97 | e1 = (addr << 16) | (limit & 0xffff); | |
98 | e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000); | |
99 | e2 |= limit_in_pages << 23; /* byte granularity */ | |
100 | e2 |= seg32_bit << 22; /* 32 bit segment */ | |
101 | stl((uint8_t *)ptr, e1); | |
102 | stl((uint8_t *)ptr + 4, e2); | |
103 | } | |
104 | ||
105 | uint64_t gdt_table[6]; | |
31e31b8a | 106 | |
851e67a1 FB |
107 | //#define DEBUG_VM86 |
108 | ||
bc8a22cc FB |
109 | static inline int is_revectored(int nr, struct target_revectored_struct *bitmap) |
110 | { | |
111 | return (tswap32(bitmap->__map[nr >> 5]) >> (nr & 0x1f)) & 1; | |
112 | } | |
113 | ||
114 | static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg) | |
115 | { | |
116 | return (uint8_t *)((seg << 4) + (reg & 0xffff)); | |
117 | } | |
118 | ||
119 | static inline void pushw(CPUX86State *env, int val) | |
120 | { | |
121 | env->regs[R_ESP] = (env->regs[R_ESP] & ~0xffff) | | |
122 | ((env->regs[R_ESP] - 2) & 0xffff); | |
123 | *(uint16_t *)seg_to_linear(env->segs[R_SS], env->regs[R_ESP]) = val; | |
124 | } | |
125 | ||
126 | static inline unsigned int get_vflags(CPUX86State *env) | |
127 | { | |
128 | unsigned int eflags; | |
129 | eflags = env->eflags & ~(VM_MASK | RF_MASK | IF_MASK); | |
130 | if (eflags & VIF_MASK) | |
131 | eflags |= IF_MASK; | |
132 | return eflags; | |
133 | } | |
134 | ||
135 | void save_v86_state(CPUX86State *env) | |
136 | { | |
137 | TaskState *ts = env->opaque; | |
138 | #ifdef DEBUG_VM86 | |
139 | printf("save_v86_state\n"); | |
140 | #endif | |
141 | ||
142 | /* put the VM86 registers in the userspace register structure */ | |
143 | ts->target_v86->regs.eax = tswap32(env->regs[R_EAX]); | |
144 | ts->target_v86->regs.ebx = tswap32(env->regs[R_EBX]); | |
145 | ts->target_v86->regs.ecx = tswap32(env->regs[R_ECX]); | |
146 | ts->target_v86->regs.edx = tswap32(env->regs[R_EDX]); | |
147 | ts->target_v86->regs.esi = tswap32(env->regs[R_ESI]); | |
148 | ts->target_v86->regs.edi = tswap32(env->regs[R_EDI]); | |
149 | ts->target_v86->regs.ebp = tswap32(env->regs[R_EBP]); | |
150 | ts->target_v86->regs.esp = tswap32(env->regs[R_ESP]); | |
151 | ts->target_v86->regs.eip = tswap32(env->eip); | |
152 | ts->target_v86->regs.cs = tswap16(env->segs[R_CS]); | |
153 | ts->target_v86->regs.ss = tswap16(env->segs[R_SS]); | |
154 | ts->target_v86->regs.ds = tswap16(env->segs[R_DS]); | |
155 | ts->target_v86->regs.es = tswap16(env->segs[R_ES]); | |
156 | ts->target_v86->regs.fs = tswap16(env->segs[R_FS]); | |
157 | ts->target_v86->regs.gs = tswap16(env->segs[R_GS]); | |
158 | ts->target_v86->regs.eflags = tswap32(env->eflags); | |
159 | ||
160 | /* restore 32 bit registers */ | |
161 | env->regs[R_EAX] = ts->vm86_saved_regs.eax; | |
162 | env->regs[R_EBX] = ts->vm86_saved_regs.ebx; | |
163 | env->regs[R_ECX] = ts->vm86_saved_regs.ecx; | |
164 | env->regs[R_EDX] = ts->vm86_saved_regs.edx; | |
165 | env->regs[R_ESI] = ts->vm86_saved_regs.esi; | |
166 | env->regs[R_EDI] = ts->vm86_saved_regs.edi; | |
167 | env->regs[R_EBP] = ts->vm86_saved_regs.ebp; | |
168 | env->regs[R_ESP] = ts->vm86_saved_regs.esp; | |
169 | env->eflags = ts->vm86_saved_regs.eflags; | |
170 | env->eip = ts->vm86_saved_regs.eip; | |
171 | ||
172 | cpu_x86_load_seg(env, R_CS, ts->vm86_saved_regs.cs); | |
173 | cpu_x86_load_seg(env, R_SS, ts->vm86_saved_regs.ss); | |
174 | cpu_x86_load_seg(env, R_DS, ts->vm86_saved_regs.ds); | |
175 | cpu_x86_load_seg(env, R_ES, ts->vm86_saved_regs.es); | |
176 | cpu_x86_load_seg(env, R_FS, ts->vm86_saved_regs.fs); | |
177 | cpu_x86_load_seg(env, R_GS, ts->vm86_saved_regs.gs); | |
178 | } | |
179 | ||
180 | /* return from vm86 mode to 32 bit. The vm86() syscall will return | |
181 | 'retval' */ | |
182 | static inline void return_to_32bit(CPUX86State *env, int retval) | |
183 | { | |
184 | #ifdef DEBUG_VM86 | |
185 | printf("return_to_32bit: ret=0x%x\n", retval); | |
186 | #endif | |
187 | save_v86_state(env); | |
188 | env->regs[R_EAX] = retval; | |
189 | } | |
190 | ||
191 | /* handle VM86 interrupt (NOTE: the CPU core currently does not | |
192 | support TSS interrupt revectoring, so this code is always executed) */ | |
193 | static void do_int(CPUX86State *env, int intno) | |
194 | { | |
195 | TaskState *ts = env->opaque; | |
196 | uint32_t *int_ptr, segoffs; | |
197 | ||
198 | if (env->segs[R_CS] == TARGET_BIOSSEG) | |
199 | goto cannot_handle; /* XXX: I am not sure this is really useful */ | |
200 | if (is_revectored(intno, &ts->target_v86->int_revectored)) | |
201 | goto cannot_handle; | |
202 | if (intno == 0x21 && is_revectored((env->regs[R_EAX] >> 8) & 0xff, | |
203 | &ts->target_v86->int21_revectored)) | |
204 | goto cannot_handle; | |
205 | int_ptr = (uint32_t *)(intno << 2); | |
206 | segoffs = tswap32(*int_ptr); | |
207 | if ((segoffs >> 16) == TARGET_BIOSSEG) | |
208 | goto cannot_handle; | |
209 | #ifdef DEBUG_VM86 | |
210 | printf("VM86: emulating int 0x%x. CS:IP=%04x:%04x\n", | |
211 | intno, segoffs >> 16, segoffs & 0xffff); | |
212 | #endif | |
213 | /* save old state */ | |
214 | pushw(env, get_vflags(env)); | |
215 | pushw(env, env->segs[R_CS]); | |
216 | pushw(env, env->eip); | |
217 | /* goto interrupt handler */ | |
218 | env->eip = segoffs & 0xffff; | |
219 | cpu_x86_load_seg(env, R_CS, segoffs >> 16); | |
220 | env->eflags &= ~(VIF_MASK | TF_MASK); | |
221 | return; | |
222 | cannot_handle: | |
223 | #ifdef DEBUG_VM86 | |
224 | printf("VM86: return to 32 bits int 0x%x\n", intno); | |
225 | #endif | |
226 | return_to_32bit(env, TARGET_VM86_INTx | (intno << 8)); | |
227 | } | |
228 | ||
1b6b029e FB |
229 | void cpu_loop(struct CPUX86State *env) |
230 | { | |
bc8a22cc | 231 | int trapnr; |
9de5e440 FB |
232 | uint8_t *pc; |
233 | target_siginfo_t info; | |
851e67a1 | 234 | |
1b6b029e | 235 | for(;;) { |
bc8a22cc | 236 | trapnr = cpu_x86_exec(env); |
1b6b029e | 237 | pc = env->seg_cache[R_CS].base + env->eip; |
bc8a22cc | 238 | switch(trapnr) { |
1b6b029e | 239 | case EXCP0D_GPF: |
851e67a1 | 240 | if (env->eflags & VM_MASK) { |
851e67a1 | 241 | #ifdef DEBUG_VM86 |
bc8a22cc FB |
242 | printf("VM86 exception %04x:%08x %02x %02x\n", |
243 | env->segs[R_CS], env->eip, pc[0], pc[1]); | |
851e67a1 FB |
244 | #endif |
245 | /* VM86 mode */ | |
851e67a1 FB |
246 | switch(pc[0]) { |
247 | case 0xcd: /* int */ | |
248 | env->eip += 2; | |
bc8a22cc FB |
249 | do_int(env, pc[1]); |
250 | break; | |
251 | case 0x66: | |
252 | switch(pc[1]) { | |
253 | case 0xfb: /* sti */ | |
254 | case 0x9d: /* popf */ | |
255 | case 0xcf: /* iret */ | |
256 | env->eip += 2; | |
257 | return_to_32bit(env, TARGET_VM86_STI); | |
258 | break; | |
259 | default: | |
260 | goto vm86_gpf; | |
261 | } | |
262 | break; | |
263 | case 0xfb: /* sti */ | |
264 | case 0x9d: /* popf */ | |
265 | case 0xcf: /* iret */ | |
266 | env->eip++; | |
267 | return_to_32bit(env, TARGET_VM86_STI); | |
851e67a1 FB |
268 | break; |
269 | default: | |
bc8a22cc | 270 | vm86_gpf: |
851e67a1 | 271 | /* real VM86 GPF exception */ |
bc8a22cc | 272 | return_to_32bit(env, TARGET_VM86_UNKNOWN); |
851e67a1 FB |
273 | break; |
274 | } | |
1b6b029e | 275 | } else { |
851e67a1 FB |
276 | if (pc[0] == 0xcd && pc[1] == 0x80) { |
277 | /* syscall */ | |
278 | env->eip += 2; | |
279 | env->regs[R_EAX] = do_syscall(env, | |
280 | env->regs[R_EAX], | |
281 | env->regs[R_EBX], | |
282 | env->regs[R_ECX], | |
283 | env->regs[R_EDX], | |
284 | env->regs[R_ESI], | |
285 | env->regs[R_EDI], | |
286 | env->regs[R_EBP]); | |
287 | } else { | |
288 | /* XXX: more precise info */ | |
289 | info.si_signo = SIGSEGV; | |
290 | info.si_errno = 0; | |
291 | info.si_code = 0; | |
292 | info._sifields._sigfault._addr = 0; | |
293 | queue_signal(info.si_signo, &info); | |
294 | } | |
1b6b029e FB |
295 | } |
296 | break; | |
9de5e440 | 297 | case EXCP00_DIVZ: |
bc8a22cc FB |
298 | if (env->eflags & VM_MASK) { |
299 | do_int(env, trapnr); | |
300 | } else { | |
301 | /* division by zero */ | |
302 | info.si_signo = SIGFPE; | |
303 | info.si_errno = 0; | |
304 | info.si_code = TARGET_FPE_INTDIV; | |
305 | info._sifields._sigfault._addr = env->eip; | |
306 | queue_signal(info.si_signo, &info); | |
307 | } | |
9de5e440 FB |
308 | break; |
309 | case EXCP04_INTO: | |
310 | case EXCP05_BOUND: | |
bc8a22cc FB |
311 | if (env->eflags & VM_MASK) { |
312 | do_int(env, trapnr); | |
313 | } else { | |
314 | info.si_signo = SIGSEGV; | |
315 | info.si_errno = 0; | |
316 | info.si_code = 0; | |
317 | info._sifields._sigfault._addr = 0; | |
318 | queue_signal(info.si_signo, &info); | |
319 | } | |
9de5e440 FB |
320 | break; |
321 | case EXCP06_ILLOP: | |
322 | info.si_signo = SIGILL; | |
323 | info.si_errno = 0; | |
324 | info.si_code = TARGET_ILL_ILLOPN; | |
325 | info._sifields._sigfault._addr = env->eip; | |
326 | queue_signal(info.si_signo, &info); | |
327 | break; | |
328 | case EXCP_INTERRUPT: | |
329 | /* just indicate that signals should be handled asap */ | |
330 | break; | |
1b6b029e | 331 | default: |
bc8a22cc FB |
332 | fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n", |
333 | (long)pc, trapnr); | |
1b6b029e FB |
334 | abort(); |
335 | } | |
66fb9763 | 336 | process_pending_signals(env); |
1b6b029e FB |
337 | } |
338 | } | |
339 | ||
31e31b8a FB |
340 | void usage(void) |
341 | { | |
3ef693a0 | 342 | printf("qemu version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n" |
d691f669 | 343 | "usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]\n" |
31e31b8a | 344 | "Linux x86 emulator\n" |
d691f669 FB |
345 | "\n" |
346 | "-h print this help\n" | |
347 | "-d activate log (logfile=%s)\n" | |
348 | "-L path set the x86 elf interpreter prefix (default=%s)\n" | |
349 | "-s size set the x86 stack size in bytes (default=%ld)\n", | |
350 | DEBUG_LOGFILE, | |
351 | interp_prefix, | |
352 | x86_stack_size); | |
31e31b8a FB |
353 | exit(1); |
354 | } | |
355 | ||
9de5e440 FB |
356 | /* XXX: currently only used for async signals (see signal.c) */ |
357 | CPUX86State *global_env; | |
851e67a1 FB |
358 | /* used to free thread contexts */ |
359 | TaskState *first_task_state; | |
9de5e440 | 360 | |
31e31b8a FB |
361 | int main(int argc, char **argv) |
362 | { | |
363 | const char *filename; | |
01ffc75b | 364 | struct target_pt_regs regs1, *regs = ®s1; |
31e31b8a | 365 | struct image_info info1, *info = &info1; |
851e67a1 | 366 | TaskState ts1, *ts = &ts1; |
0ecfa993 | 367 | CPUX86State *env; |
586314f2 | 368 | int optind; |
d691f669 FB |
369 | const char *r; |
370 | ||
31e31b8a FB |
371 | if (argc <= 1) |
372 | usage(); | |
586314f2 FB |
373 | loglevel = 0; |
374 | optind = 1; | |
d691f669 FB |
375 | for(;;) { |
376 | if (optind >= argc) | |
377 | break; | |
378 | r = argv[optind]; | |
379 | if (r[0] != '-') | |
380 | break; | |
586314f2 | 381 | optind++; |
d691f669 FB |
382 | r++; |
383 | if (!strcmp(r, "-")) { | |
384 | break; | |
385 | } else if (!strcmp(r, "d")) { | |
386 | loglevel = 1; | |
387 | } else if (!strcmp(r, "s")) { | |
388 | r = argv[optind++]; | |
389 | x86_stack_size = strtol(r, (char **)&r, 0); | |
390 | if (x86_stack_size <= 0) | |
391 | usage(); | |
392 | if (*r == 'M') | |
393 | x86_stack_size *= 1024 * 1024; | |
394 | else if (*r == 'k' || *r == 'K') | |
395 | x86_stack_size *= 1024; | |
396 | } else if (!strcmp(r, "L")) { | |
397 | interp_prefix = argv[optind++]; | |
398 | } else { | |
399 | usage(); | |
400 | } | |
586314f2 | 401 | } |
d691f669 FB |
402 | if (optind >= argc) |
403 | usage(); | |
586314f2 FB |
404 | filename = argv[optind]; |
405 | ||
406 | /* init debug */ | |
407 | if (loglevel) { | |
408 | logfile = fopen(DEBUG_LOGFILE, "w"); | |
409 | if (!logfile) { | |
410 | perror(DEBUG_LOGFILE); | |
411 | exit(1); | |
412 | } | |
413 | setvbuf(logfile, NULL, _IOLBF, 0); | |
414 | } | |
31e31b8a FB |
415 | |
416 | /* Zero out regs */ | |
01ffc75b | 417 | memset(regs, 0, sizeof(struct target_pt_regs)); |
31e31b8a FB |
418 | |
419 | /* Zero out image_info */ | |
420 | memset(info, 0, sizeof(struct image_info)); | |
421 | ||
d691f669 | 422 | if(elf_exec(interp_prefix, filename, argv+optind, environ, regs, info) != 0) { |
31e31b8a FB |
423 | printf("Error loading %s\n", filename); |
424 | exit(1); | |
425 | } | |
426 | ||
4b74fe1f FB |
427 | if (loglevel) { |
428 | fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk); | |
429 | fprintf(logfile, "end_code 0x%08lx\n" , info->end_code); | |
430 | fprintf(logfile, "start_code 0x%08lx\n" , info->start_code); | |
431 | fprintf(logfile, "end_data 0x%08lx\n" , info->end_data); | |
432 | fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack); | |
433 | fprintf(logfile, "brk 0x%08lx\n" , info->brk); | |
434 | fprintf(logfile, "esp 0x%08lx\n" , regs->esp); | |
435 | fprintf(logfile, "eip 0x%08lx\n" , regs->eip); | |
436 | } | |
31e31b8a FB |
437 | |
438 | target_set_brk((char *)info->brk); | |
439 | syscall_init(); | |
66fb9763 | 440 | signal_init(); |
31e31b8a | 441 | |
0ecfa993 | 442 | env = cpu_x86_init(); |
9de5e440 | 443 | global_env = env; |
0ecfa993 | 444 | |
851e67a1 FB |
445 | /* build Task State */ |
446 | memset(ts, 0, sizeof(TaskState)); | |
447 | env->opaque = ts; | |
448 | ts->used = 1; | |
449 | ||
6dbad63e | 450 | /* linux register setup */ |
0ecfa993 FB |
451 | env->regs[R_EAX] = regs->eax; |
452 | env->regs[R_EBX] = regs->ebx; | |
453 | env->regs[R_ECX] = regs->ecx; | |
454 | env->regs[R_EDX] = regs->edx; | |
455 | env->regs[R_ESI] = regs->esi; | |
456 | env->regs[R_EDI] = regs->edi; | |
457 | env->regs[R_EBP] = regs->ebp; | |
458 | env->regs[R_ESP] = regs->esp; | |
dab2ed99 | 459 | env->eip = regs->eip; |
31e31b8a | 460 | |
6dbad63e FB |
461 | /* linux segment setup */ |
462 | env->gdt.base = (void *)gdt_table; | |
463 | env->gdt.limit = sizeof(gdt_table) - 1; | |
464 | write_dt(&gdt_table[__USER_CS >> 3], 0, 0xffffffff, 1); | |
465 | write_dt(&gdt_table[__USER_DS >> 3], 0, 0xffffffff, 1); | |
466 | cpu_x86_load_seg(env, R_CS, __USER_CS); | |
467 | cpu_x86_load_seg(env, R_DS, __USER_DS); | |
468 | cpu_x86_load_seg(env, R_ES, __USER_DS); | |
469 | cpu_x86_load_seg(env, R_SS, __USER_DS); | |
470 | cpu_x86_load_seg(env, R_FS, __USER_DS); | |
471 | cpu_x86_load_seg(env, R_GS, __USER_DS); | |
31e31b8a | 472 | |
1b6b029e FB |
473 | cpu_loop(env); |
474 | /* never exits */ | |
31e31b8a FB |
475 | return 0; |
476 | } |