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 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
107 e1 = (addr << 16) | (limit & 0xffff);
108 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
110 stl((uint8_t *)ptr, e1);
111 stl((uint8_t *)ptr + 4, e2);
114 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
115 unsigned long addr, unsigned int sel)
118 e1 = (addr & 0xffff) | (sel << 16);
119 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
120 stl((uint8_t *)ptr, e1);
121 stl((uint8_t *)ptr + 4, e2);
124 uint64_t gdt_table[6];
125 uint64_t idt_table[256];
127 /* only dpl matters as we do only user space emulation */
128 static void set_idt(int n, unsigned int dpl)
130 set_gate(idt_table + n, 0, dpl, 0, 0);
133 void cpu_loop(CPUX86State *env)
137 target_siginfo_t info;
140 trapnr = cpu_x86_exec(env);
144 env->regs[R_EAX] = do_syscall(env,
155 info.si_signo = SIGBUS;
157 info.si_code = TARGET_SI_KERNEL;
158 info._sifields._sigfault._addr = 0;
159 queue_signal(info.si_signo, &info);
162 if (env->eflags & VM_MASK) {
163 handle_vm86_fault(env);
165 info.si_signo = SIGSEGV;
167 info.si_code = TARGET_SI_KERNEL;
168 info._sifields._sigfault._addr = 0;
169 queue_signal(info.si_signo, &info);
173 info.si_signo = SIGSEGV;
175 if (!(env->error_code & 1))
176 info.si_code = TARGET_SEGV_MAPERR;
178 info.si_code = TARGET_SEGV_ACCERR;
179 info._sifields._sigfault._addr = env->cr2;
180 queue_signal(info.si_signo, &info);
183 if (env->eflags & VM_MASK) {
184 handle_vm86_trap(env, trapnr);
186 /* division by zero */
187 info.si_signo = SIGFPE;
189 info.si_code = TARGET_FPE_INTDIV;
190 info._sifields._sigfault._addr = env->eip;
191 queue_signal(info.si_signo, &info);
196 if (env->eflags & VM_MASK) {
197 handle_vm86_trap(env, trapnr);
199 info.si_signo = SIGTRAP;
201 if (trapnr == EXCP01_SSTP) {
202 info.si_code = TARGET_TRAP_BRKPT;
203 info._sifields._sigfault._addr = env->eip;
205 info.si_code = TARGET_SI_KERNEL;
206 info._sifields._sigfault._addr = 0;
208 queue_signal(info.si_signo, &info);
213 if (env->eflags & VM_MASK) {
214 handle_vm86_trap(env, trapnr);
216 info.si_signo = SIGSEGV;
218 info.si_code = TARGET_SI_KERNEL;
219 info._sifields._sigfault._addr = 0;
220 queue_signal(info.si_signo, &info);
224 info.si_signo = SIGILL;
226 info.si_code = TARGET_ILL_ILLOPN;
227 info._sifields._sigfault._addr = env->eip;
228 queue_signal(info.si_signo, &info);
231 /* just indicate that signals should be handled asap */
234 pc = env->seg_cache[R_CS].base + env->eip;
235 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
239 process_pending_signals(env);
246 #define ARM_SYSCALL_BASE 0x900000
248 void cpu_loop(CPUARMState *env)
251 unsigned int n, insn;
252 target_siginfo_t info;
255 trapnr = cpu_arm_exec(env);
258 info.si_signo = SIGILL;
260 info.si_code = TARGET_ILL_ILLOPN;
261 info._sifields._sigfault._addr = env->regs[15];
262 queue_signal(info.si_signo, &info);
267 insn = ldl((void *)(env->regs[15] - 4));
269 if (n >= ARM_SYSCALL_BASE) {
271 n -= ARM_SYSCALL_BASE;
272 env->regs[0] = do_syscall(env,
287 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
289 cpu_arm_dump_state(env, stderr, 0);
292 process_pending_signals(env);
300 printf("qemu version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
301 "usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]\n"
302 "Linux CPU emulator (compiled for %s emulation)\n"
304 "-h print this help\n"
305 "-L path set the elf interpreter prefix (default=%s)\n"
306 "-s size set the stack size in bytes (default=%ld)\n"
309 "-d activate log (logfile=%s)\n"
310 "-p pagesize set the host page size to 'pagesize'\n",
318 /* XXX: currently only used for async signals (see signal.c) */
319 CPUState *global_env;
320 /* used to free thread contexts */
321 TaskState *first_task_state;
323 int main(int argc, char **argv)
325 const char *filename;
326 struct target_pt_regs regs1, *regs = ®s1;
327 struct image_info info1, *info = &info1;
328 TaskState ts1, *ts = &ts1;
346 if (!strcmp(r, "-")) {
348 } else if (!strcmp(r, "d")) {
350 } else if (!strcmp(r, "s")) {
352 x86_stack_size = strtol(r, (char **)&r, 0);
353 if (x86_stack_size <= 0)
356 x86_stack_size *= 1024 * 1024;
357 else if (*r == 'k' || *r == 'K')
358 x86_stack_size *= 1024;
359 } else if (!strcmp(r, "L")) {
360 interp_prefix = argv[optind++];
361 } else if (!strcmp(r, "p")) {
362 host_page_size = atoi(argv[optind++]);
363 if (host_page_size == 0 ||
364 (host_page_size & (host_page_size - 1)) != 0) {
365 fprintf(stderr, "page size must be a power of two\n");
374 filename = argv[optind];
378 logfile = fopen(DEBUG_LOGFILE, "w");
380 perror(DEBUG_LOGFILE);
383 setvbuf(logfile, NULL, _IOLBF, 0);
387 memset(regs, 0, sizeof(struct target_pt_regs));
389 /* Zero out image_info */
390 memset(info, 0, sizeof(struct image_info));
392 /* Scan interp_prefix dir for replacement files. */
393 init_paths(interp_prefix);
395 /* NOTE: we need to init the CPU at this stage to get the
399 if (elf_exec(filename, argv+optind, environ, regs, info) != 0) {
400 printf("Error loading %s\n", filename);
407 fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk);
408 fprintf(logfile, "end_code 0x%08lx\n" , info->end_code);
409 fprintf(logfile, "start_code 0x%08lx\n" , info->start_code);
410 fprintf(logfile, "end_data 0x%08lx\n" , info->end_data);
411 fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
412 fprintf(logfile, "brk 0x%08lx\n" , info->brk);
413 fprintf(logfile, "entry 0x%08lx\n" , info->entry);
416 target_set_brk((char *)info->brk);
422 /* build Task State */
423 memset(ts, 0, sizeof(TaskState));
427 #if defined(TARGET_I386)
428 /* linux register setup */
429 env->regs[R_EAX] = regs->eax;
430 env->regs[R_EBX] = regs->ebx;
431 env->regs[R_ECX] = regs->ecx;
432 env->regs[R_EDX] = regs->edx;
433 env->regs[R_ESI] = regs->esi;
434 env->regs[R_EDI] = regs->edi;
435 env->regs[R_EBP] = regs->ebp;
436 env->regs[R_ESP] = regs->esp;
437 env->eip = regs->eip;
439 /* linux interrupt setup */
440 env->idt.base = (void *)idt_table;
441 env->idt.limit = sizeof(idt_table) - 1;
464 /* linux segment setup */
465 env->gdt.base = (void *)gdt_table;
466 env->gdt.limit = sizeof(gdt_table) - 1;
467 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
468 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
469 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
470 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
471 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
472 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
473 cpu_x86_load_seg(env, R_CS, __USER_CS);
474 cpu_x86_load_seg(env, R_DS, __USER_DS);
475 cpu_x86_load_seg(env, R_ES, __USER_DS);
476 cpu_x86_load_seg(env, R_SS, __USER_DS);
477 cpu_x86_load_seg(env, R_FS, __USER_DS);
478 cpu_x86_load_seg(env, R_GS, __USER_DS);
479 #elif defined(TARGET_ARM)
482 for(i = 0; i < 16; i++) {
483 env->regs[i] = regs->uregs[i];
485 env->cpsr = regs->uregs[16];
488 #error unsupported target CPU