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