]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/main.c
added CPL/IOPL support - fixed subtle inc/dec flag optimisation bug - added HLT instr...
[mirror_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
b689bc57 70void cpu_x86_outb(CPUX86State *env, int addr, int val)
367e86e8
FB
71{
72 fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
73}
74
b689bc57 75void cpu_x86_outw(CPUX86State *env, int addr, int val)
367e86e8
FB
76{
77 fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
78}
79
b689bc57 80void cpu_x86_outl(CPUX86State *env, int addr, int val)
367e86e8
FB
81{
82 fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
83}
84
b689bc57 85int cpu_x86_inb(CPUX86State *env, int addr)
367e86e8
FB
86{
87 fprintf(stderr, "inb: port=0x%04x\n", addr);
88 return 0;
89}
90
b689bc57 91int cpu_x86_inw(CPUX86State *env, int addr)
367e86e8
FB
92{
93 fprintf(stderr, "inw: port=0x%04x\n", addr);
94 return 0;
95}
96
b689bc57 97int cpu_x86_inl(CPUX86State *env, 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;
b689bc57 306 info.si_code = TARGET_SI_KERNEL;
851e67a1
FB
307 info._sifields._sigfault._addr = 0;
308 queue_signal(info.si_signo, &info);
309 }
1b6b029e
FB
310 }
311 break;
b689bc57
FB
312 case EXCP0E_PAGE:
313 info.si_signo = SIGSEGV;
314 info.si_errno = 0;
315 if (!(env->error_code & 1))
316 info.si_code = TARGET_SEGV_MAPERR;
317 else
318 info.si_code = TARGET_SEGV_ACCERR;
319 info._sifields._sigfault._addr = env->cr2;
320 queue_signal(info.si_signo, &info);
321 break;
9de5e440 322 case EXCP00_DIVZ:
bc8a22cc
FB
323 if (env->eflags & VM_MASK) {
324 do_int(env, trapnr);
325 } else {
326 /* division by zero */
327 info.si_signo = SIGFPE;
328 info.si_errno = 0;
329 info.si_code = TARGET_FPE_INTDIV;
330 info._sifields._sigfault._addr = env->eip;
331 queue_signal(info.si_signo, &info);
332 }
9de5e440
FB
333 break;
334 case EXCP04_INTO:
335 case EXCP05_BOUND:
bc8a22cc
FB
336 if (env->eflags & VM_MASK) {
337 do_int(env, trapnr);
338 } else {
339 info.si_signo = SIGSEGV;
340 info.si_errno = 0;
b689bc57 341 info.si_code = TARGET_SI_KERNEL;
bc8a22cc
FB
342 info._sifields._sigfault._addr = 0;
343 queue_signal(info.si_signo, &info);
344 }
9de5e440
FB
345 break;
346 case EXCP06_ILLOP:
347 info.si_signo = SIGILL;
348 info.si_errno = 0;
349 info.si_code = TARGET_ILL_ILLOPN;
350 info._sifields._sigfault._addr = env->eip;
351 queue_signal(info.si_signo, &info);
352 break;
353 case EXCP_INTERRUPT:
354 /* just indicate that signals should be handled asap */
355 break;
1b6b029e 356 default:
bc8a22cc
FB
357 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
358 (long)pc, trapnr);
1b6b029e
FB
359 abort();
360 }
66fb9763 361 process_pending_signals(env);
1b6b029e
FB
362 }
363}
364
31e31b8a
FB
365void usage(void)
366{
3ef693a0 367 printf("qemu version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
d691f669 368 "usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]\n"
31e31b8a 369 "Linux x86 emulator\n"
d691f669
FB
370 "\n"
371 "-h print this help\n"
372 "-d activate log (logfile=%s)\n"
373 "-L path set the x86 elf interpreter prefix (default=%s)\n"
374 "-s size set the x86 stack size in bytes (default=%ld)\n",
375 DEBUG_LOGFILE,
376 interp_prefix,
377 x86_stack_size);
74cd30b8 378 _exit(1);
31e31b8a
FB
379}
380
9de5e440
FB
381/* XXX: currently only used for async signals (see signal.c) */
382CPUX86State *global_env;
851e67a1
FB
383/* used to free thread contexts */
384TaskState *first_task_state;
9de5e440 385
31e31b8a
FB
386int main(int argc, char **argv)
387{
388 const char *filename;
01ffc75b 389 struct target_pt_regs regs1, *regs = &regs1;
31e31b8a 390 struct image_info info1, *info = &info1;
851e67a1 391 TaskState ts1, *ts = &ts1;
0ecfa993 392 CPUX86State *env;
586314f2 393 int optind;
d691f669
FB
394 const char *r;
395
31e31b8a
FB
396 if (argc <= 1)
397 usage();
f801f97e 398
586314f2
FB
399 loglevel = 0;
400 optind = 1;
d691f669
FB
401 for(;;) {
402 if (optind >= argc)
403 break;
404 r = argv[optind];
405 if (r[0] != '-')
406 break;
586314f2 407 optind++;
d691f669
FB
408 r++;
409 if (!strcmp(r, "-")) {
410 break;
411 } else if (!strcmp(r, "d")) {
412 loglevel = 1;
413 } else if (!strcmp(r, "s")) {
414 r = argv[optind++];
415 x86_stack_size = strtol(r, (char **)&r, 0);
416 if (x86_stack_size <= 0)
417 usage();
418 if (*r == 'M')
419 x86_stack_size *= 1024 * 1024;
420 else if (*r == 'k' || *r == 'K')
421 x86_stack_size *= 1024;
422 } else if (!strcmp(r, "L")) {
423 interp_prefix = argv[optind++];
424 } else {
425 usage();
426 }
586314f2 427 }
d691f669
FB
428 if (optind >= argc)
429 usage();
586314f2
FB
430 filename = argv[optind];
431
432 /* init debug */
433 if (loglevel) {
434 logfile = fopen(DEBUG_LOGFILE, "w");
435 if (!logfile) {
436 perror(DEBUG_LOGFILE);
74cd30b8 437 _exit(1);
586314f2
FB
438 }
439 setvbuf(logfile, NULL, _IOLBF, 0);
440 }
31e31b8a
FB
441
442 /* Zero out regs */
01ffc75b 443 memset(regs, 0, sizeof(struct target_pt_regs));
31e31b8a
FB
444
445 /* Zero out image_info */
446 memset(info, 0, sizeof(struct image_info));
447
74cd30b8
FB
448 /* Scan interp_prefix dir for replacement files. */
449 init_paths(interp_prefix);
450
451 if (elf_exec(filename, argv+optind, environ, regs, info) != 0) {
31e31b8a 452 printf("Error loading %s\n", filename);
74cd30b8 453 _exit(1);
31e31b8a
FB
454 }
455
4b74fe1f
FB
456 if (loglevel) {
457 fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk);
458 fprintf(logfile, "end_code 0x%08lx\n" , info->end_code);
459 fprintf(logfile, "start_code 0x%08lx\n" , info->start_code);
460 fprintf(logfile, "end_data 0x%08lx\n" , info->end_data);
461 fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
462 fprintf(logfile, "brk 0x%08lx\n" , info->brk);
463 fprintf(logfile, "esp 0x%08lx\n" , regs->esp);
464 fprintf(logfile, "eip 0x%08lx\n" , regs->eip);
465 }
31e31b8a
FB
466
467 target_set_brk((char *)info->brk);
468 syscall_init();
66fb9763 469 signal_init();
31e31b8a 470
0ecfa993 471 env = cpu_x86_init();
9de5e440 472 global_env = env;
0ecfa993 473
851e67a1
FB
474 /* build Task State */
475 memset(ts, 0, sizeof(TaskState));
476 env->opaque = ts;
477 ts->used = 1;
478
6dbad63e 479 /* linux register setup */
0ecfa993
FB
480 env->regs[R_EAX] = regs->eax;
481 env->regs[R_EBX] = regs->ebx;
482 env->regs[R_ECX] = regs->ecx;
483 env->regs[R_EDX] = regs->edx;
484 env->regs[R_ESI] = regs->esi;
485 env->regs[R_EDI] = regs->edi;
486 env->regs[R_EBP] = regs->ebp;
487 env->regs[R_ESP] = regs->esp;
dab2ed99 488 env->eip = regs->eip;
31e31b8a 489
6dbad63e
FB
490 /* linux segment setup */
491 env->gdt.base = (void *)gdt_table;
492 env->gdt.limit = sizeof(gdt_table) - 1;
493 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xffffffff, 1);
494 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xffffffff, 1);
495 cpu_x86_load_seg(env, R_CS, __USER_CS);
496 cpu_x86_load_seg(env, R_DS, __USER_DS);
497 cpu_x86_load_seg(env, R_ES, __USER_DS);
498 cpu_x86_load_seg(env, R_SS, __USER_DS);
499 cpu_x86_load_seg(env, R_FS, __USER_DS);
500 cpu_x86_load_seg(env, R_GS, __USER_DS);
31e31b8a 501
1b6b029e
FB
502 cpu_loop(env);
503 /* never exits */
31e31b8a
FB
504 return 0;
505}