4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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.
31 #define DEBUG_LOGFILE "/tmp/qemu.log"
35 static const char *interp_prefix = CONFIG_QEMU_PREFIX;
38 /* Force usage of an ELF interpreter even if it is an ELF shared
40 const char interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
43 /* for recent libc, we add these dummies symbol which are not declared
44 when generating a linked object (bug in ld ?) */
45 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
46 long __init_array_start[0];
47 long __init_array_end[0];
48 long __fini_array_start[0];
49 long __fini_array_end[0];
52 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
53 we allocate a bigger stack. Need a better solution, for example
54 by remapping the process stack directly at the right place */
55 unsigned long x86_stack_size = 512 * 1024;
57 void gemu_log(const char *fmt, ...)
62 vfprintf(stderr, fmt, ap);
67 /***********************************************************/
68 /* CPUX86 core interface */
70 void cpu_x86_outb(CPUX86State *env, int addr, int val)
72 fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
75 void cpu_x86_outw(CPUX86State *env, int addr, int val)
77 fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
80 void cpu_x86_outl(CPUX86State *env, int addr, int val)
82 fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
85 int cpu_x86_inb(CPUX86State *env, int addr)
87 fprintf(stderr, "inb: port=0x%04x\n", addr);
91 int cpu_x86_inw(CPUX86State *env, int addr)
93 fprintf(stderr, "inw: port=0x%04x\n", addr);
97 int cpu_x86_inl(CPUX86State *env, int addr)
99 fprintf(stderr, "inl: port=0x%04x\n", addr);
103 int cpu_x86_get_pic_interrupt(CPUX86State *env)
108 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
112 e1 = (addr << 16) | (limit & 0xffff);
113 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
115 stl((uint8_t *)ptr, e1);
116 stl((uint8_t *)ptr + 4, e2);
119 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
120 unsigned long addr, unsigned int sel)
123 e1 = (addr & 0xffff) | (sel << 16);
124 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
125 stl((uint8_t *)ptr, e1);
126 stl((uint8_t *)ptr + 4, e2);
129 uint64_t gdt_table[6];
130 uint64_t idt_table[256];
132 /* only dpl matters as we do only user space emulation */
133 static void set_idt(int n, unsigned int dpl)
135 set_gate(idt_table + n, 0, dpl, 0, 0);
138 void cpu_loop(CPUX86State *env)
142 target_siginfo_t info;
145 trapnr = cpu_x86_exec(env);
149 env->regs[R_EAX] = do_syscall(env,
160 info.si_signo = SIGBUS;
162 info.si_code = TARGET_SI_KERNEL;
163 info._sifields._sigfault._addr = 0;
164 queue_signal(info.si_signo, &info);
167 if (env->eflags & VM_MASK) {
168 handle_vm86_fault(env);
170 info.si_signo = SIGSEGV;
172 info.si_code = TARGET_SI_KERNEL;
173 info._sifields._sigfault._addr = 0;
174 queue_signal(info.si_signo, &info);
178 info.si_signo = SIGSEGV;
180 if (!(env->error_code & 1))
181 info.si_code = TARGET_SEGV_MAPERR;
183 info.si_code = TARGET_SEGV_ACCERR;
184 info._sifields._sigfault._addr = env->cr[2];
185 queue_signal(info.si_signo, &info);
188 if (env->eflags & VM_MASK) {
189 handle_vm86_trap(env, trapnr);
191 /* division by zero */
192 info.si_signo = SIGFPE;
194 info.si_code = TARGET_FPE_INTDIV;
195 info._sifields._sigfault._addr = env->eip;
196 queue_signal(info.si_signo, &info);
201 if (env->eflags & VM_MASK) {
202 handle_vm86_trap(env, trapnr);
204 info.si_signo = SIGTRAP;
206 if (trapnr == EXCP01_SSTP) {
207 info.si_code = TARGET_TRAP_BRKPT;
208 info._sifields._sigfault._addr = env->eip;
210 info.si_code = TARGET_SI_KERNEL;
211 info._sifields._sigfault._addr = 0;
213 queue_signal(info.si_signo, &info);
218 if (env->eflags & VM_MASK) {
219 handle_vm86_trap(env, trapnr);
221 info.si_signo = SIGSEGV;
223 info.si_code = TARGET_SI_KERNEL;
224 info._sifields._sigfault._addr = 0;
225 queue_signal(info.si_signo, &info);
229 info.si_signo = SIGILL;
231 info.si_code = TARGET_ILL_ILLOPN;
232 info._sifields._sigfault._addr = env->eip;
233 queue_signal(info.si_signo, &info);
236 /* just indicate that signals should be handled asap */
239 pc = env->segs[R_CS].base + env->eip;
240 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
244 process_pending_signals(env);
251 #define ARM_SYSCALL_BASE 0x900000
253 void cpu_loop(CPUARMState *env)
256 unsigned int n, insn;
257 target_siginfo_t info;
260 trapnr = cpu_arm_exec(env);
263 info.si_signo = SIGILL;
265 info.si_code = TARGET_ILL_ILLOPN;
266 info._sifields._sigfault._addr = env->regs[15];
267 queue_signal(info.si_signo, &info);
272 insn = ldl((void *)(env->regs[15] - 4));
274 if (n >= ARM_SYSCALL_BASE) {
276 n -= ARM_SYSCALL_BASE;
277 env->regs[0] = do_syscall(env,
292 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
294 cpu_arm_dump_state(env, stderr, 0);
297 process_pending_signals(env);
305 printf("qemu version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
306 "usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]\n"
307 "Linux CPU emulator (compiled for %s emulation)\n"
309 "-h print this help\n"
310 "-L path set the elf interpreter prefix (default=%s)\n"
311 "-s size set the stack size in bytes (default=%ld)\n"
314 "-d activate log (logfile=%s)\n"
315 "-p pagesize set the host page size to 'pagesize'\n",
323 /* XXX: currently only used for async signals (see signal.c) */
324 CPUState *global_env;
325 /* used to free thread contexts */
326 TaskState *first_task_state;
328 int main(int argc, char **argv)
330 const char *filename;
331 struct target_pt_regs regs1, *regs = ®s1;
332 struct image_info info1, *info = &info1;
333 TaskState ts1, *ts = &ts1;
351 if (!strcmp(r, "-")) {
353 } else if (!strcmp(r, "d")) {
355 } else if (!strcmp(r, "s")) {
357 x86_stack_size = strtol(r, (char **)&r, 0);
358 if (x86_stack_size <= 0)
361 x86_stack_size *= 1024 * 1024;
362 else if (*r == 'k' || *r == 'K')
363 x86_stack_size *= 1024;
364 } else if (!strcmp(r, "L")) {
365 interp_prefix = argv[optind++];
366 } else if (!strcmp(r, "p")) {
367 host_page_size = atoi(argv[optind++]);
368 if (host_page_size == 0 ||
369 (host_page_size & (host_page_size - 1)) != 0) {
370 fprintf(stderr, "page size must be a power of two\n");
379 filename = argv[optind];
383 logfile = fopen(DEBUG_LOGFILE, "w");
385 perror(DEBUG_LOGFILE);
388 setvbuf(logfile, NULL, _IOLBF, 0);
392 memset(regs, 0, sizeof(struct target_pt_regs));
394 /* Zero out image_info */
395 memset(info, 0, sizeof(struct image_info));
397 /* Scan interp_prefix dir for replacement files. */
398 init_paths(interp_prefix);
400 /* NOTE: we need to init the CPU at this stage to get the
404 if (elf_exec(filename, argv+optind, environ, regs, info) != 0) {
405 printf("Error loading %s\n", filename);
412 fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk);
413 fprintf(logfile, "end_code 0x%08lx\n" , info->end_code);
414 fprintf(logfile, "start_code 0x%08lx\n" , info->start_code);
415 fprintf(logfile, "end_data 0x%08lx\n" , info->end_data);
416 fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
417 fprintf(logfile, "brk 0x%08lx\n" , info->brk);
418 fprintf(logfile, "entry 0x%08lx\n" , info->entry);
421 target_set_brk((char *)info->brk);
427 /* build Task State */
428 memset(ts, 0, sizeof(TaskState));
432 #if defined(TARGET_I386)
433 /* linux register setup */
434 env->regs[R_EAX] = regs->eax;
435 env->regs[R_EBX] = regs->ebx;
436 env->regs[R_ECX] = regs->ecx;
437 env->regs[R_EDX] = regs->edx;
438 env->regs[R_ESI] = regs->esi;
439 env->regs[R_EDI] = regs->edi;
440 env->regs[R_EBP] = regs->ebp;
441 env->regs[R_ESP] = regs->esp;
442 env->eip = regs->eip;
444 /* linux interrupt setup */
445 env->idt.base = (void *)idt_table;
446 env->idt.limit = sizeof(idt_table) - 1;
469 /* linux segment setup */
470 env->gdt.base = (void *)gdt_table;
471 env->gdt.limit = sizeof(gdt_table) - 1;
472 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
473 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
474 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
475 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
476 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
477 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
478 cpu_x86_load_seg(env, R_CS, __USER_CS);
479 cpu_x86_load_seg(env, R_DS, __USER_DS);
480 cpu_x86_load_seg(env, R_ES, __USER_DS);
481 cpu_x86_load_seg(env, R_SS, __USER_DS);
482 cpu_x86_load_seg(env, R_FS, __USER_DS);
483 cpu_x86_load_seg(env, R_GS, __USER_DS);
484 env->user_mode_only = 1;
486 #elif defined(TARGET_ARM)
489 for(i = 0; i < 16; i++) {
490 env->regs[i] = regs->uregs[i];
492 env->cpsr = regs->uregs[16];
495 #error unsupported target CPU