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.
29 #define DEBUG_LOGFILE "/tmp/qemu.log"
33 static const char *interp_prefix = CONFIG_QEMU_PREFIX;
36 /* Force usage of an ELF interpreter even if it is an ELF shared
38 const char interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
41 /* for recent libc, we add these dummy symbols which are not declared
42 when generating a linked object (bug in ld ?) */
43 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
44 long __init_array_start[0];
45 long __init_array_end[0];
46 long __fini_array_start[0];
47 long __fini_array_end[0];
50 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
51 we allocate a bigger stack. Need a better solution, for example
52 by remapping the process stack directly at the right place */
53 unsigned long x86_stack_size = 512 * 1024;
55 void gemu_log(const char *fmt, ...)
60 vfprintf(stderr, fmt, ap);
65 /***********************************************************/
66 /* CPUX86 core interface */
68 void cpu_x86_outb(CPUX86State *env, int addr, int val)
70 fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
73 void cpu_x86_outw(CPUX86State *env, int addr, int val)
75 fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
78 void cpu_x86_outl(CPUX86State *env, int addr, int val)
80 fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
83 int cpu_x86_inb(CPUX86State *env, int addr)
85 fprintf(stderr, "inb: port=0x%04x\n", addr);
89 int cpu_x86_inw(CPUX86State *env, int addr)
91 fprintf(stderr, "inw: port=0x%04x\n", addr);
95 int cpu_x86_inl(CPUX86State *env, int addr)
97 fprintf(stderr, "inl: port=0x%04x\n", addr);
101 int cpu_x86_get_pic_interrupt(CPUX86State *env)
106 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
110 e1 = (addr << 16) | (limit & 0xffff);
111 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
113 stl((uint8_t *)ptr, e1);
114 stl((uint8_t *)ptr + 4, e2);
117 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
118 unsigned long addr, unsigned int sel)
121 e1 = (addr & 0xffff) | (sel << 16);
122 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
123 stl((uint8_t *)ptr, e1);
124 stl((uint8_t *)ptr + 4, e2);
127 uint64_t gdt_table[6];
128 uint64_t idt_table[256];
130 /* only dpl matters as we do only user space emulation */
131 static void set_idt(int n, unsigned int dpl)
133 set_gate(idt_table + n, 0, dpl, 0, 0);
136 void cpu_loop(CPUX86State *env)
140 target_siginfo_t info;
143 trapnr = cpu_x86_exec(env);
147 env->regs[R_EAX] = do_syscall(env,
158 info.si_signo = SIGBUS;
160 info.si_code = TARGET_SI_KERNEL;
161 info._sifields._sigfault._addr = 0;
162 queue_signal(info.si_signo, &info);
165 if (env->eflags & VM_MASK) {
166 handle_vm86_fault(env);
168 info.si_signo = SIGSEGV;
170 info.si_code = TARGET_SI_KERNEL;
171 info._sifields._sigfault._addr = 0;
172 queue_signal(info.si_signo, &info);
176 info.si_signo = SIGSEGV;
178 if (!(env->error_code & 1))
179 info.si_code = TARGET_SEGV_MAPERR;
181 info.si_code = TARGET_SEGV_ACCERR;
182 info._sifields._sigfault._addr = env->cr[2];
183 queue_signal(info.si_signo, &info);
186 if (env->eflags & VM_MASK) {
187 handle_vm86_trap(env, trapnr);
189 /* division by zero */
190 info.si_signo = SIGFPE;
192 info.si_code = TARGET_FPE_INTDIV;
193 info._sifields._sigfault._addr = env->eip;
194 queue_signal(info.si_signo, &info);
199 if (env->eflags & VM_MASK) {
200 handle_vm86_trap(env, trapnr);
202 info.si_signo = SIGTRAP;
204 if (trapnr == EXCP01_SSTP) {
205 info.si_code = TARGET_TRAP_BRKPT;
206 info._sifields._sigfault._addr = env->eip;
208 info.si_code = TARGET_SI_KERNEL;
209 info._sifields._sigfault._addr = 0;
211 queue_signal(info.si_signo, &info);
216 if (env->eflags & VM_MASK) {
217 handle_vm86_trap(env, trapnr);
219 info.si_signo = SIGSEGV;
221 info.si_code = TARGET_SI_KERNEL;
222 info._sifields._sigfault._addr = 0;
223 queue_signal(info.si_signo, &info);
227 info.si_signo = SIGILL;
229 info.si_code = TARGET_ILL_ILLOPN;
230 info._sifields._sigfault._addr = env->eip;
231 queue_signal(info.si_signo, &info);
234 /* just indicate that signals should be handled asap */
237 pc = env->segs[R_CS].base + env->eip;
238 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
242 process_pending_signals(env);
249 void cpu_loop(CPUARMState *env)
252 unsigned int n, insn;
253 target_siginfo_t info;
256 trapnr = cpu_arm_exec(env);
259 info.si_signo = SIGILL;
261 info.si_code = TARGET_ILL_ILLOPN;
262 info._sifields._sigfault._addr = env->regs[15];
263 queue_signal(info.si_signo, &info);
268 insn = ldl((void *)(env->regs[15] - 4));
270 if (n >= ARM_SYSCALL_BASE) {
272 n -= ARM_SYSCALL_BASE;
273 env->regs[0] = do_syscall(env,
287 /* just indicate that signals should be handled asap */
291 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
293 cpu_arm_dump_state(env, stderr, 0);
296 process_pending_signals(env);
304 void cpu_loop (CPUSPARCState *env)
309 trapnr = cpu_sparc_exec (env);
313 env->regwptr[0] = do_syscall (env, env->gregs[1],
314 env->regwptr[0], env->regwptr[1], env->regwptr[2],
315 env->regwptr[3], env->regwptr[4], env->regwptr[13]);
316 if (env->regwptr[0] >= 0xffffffe0)
317 env->psr |= PSR_CARRY;
320 printf ("Invalid trap: %d\n", trapnr);
323 process_pending_signals (env);
331 printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
332 "usage: qemu-" TARGET_ARCH " [-h] [-d] [-L path] [-s size] program [arguments...]\n"
333 "Linux CPU emulator (compiled for %s emulation)\n"
335 "-h print this help\n"
336 "-L path set the elf interpreter prefix (default=%s)\n"
337 "-s size set the stack size in bytes (default=%ld)\n"
340 "-d activate log (logfile=%s)\n"
341 "-p pagesize set the host page size to 'pagesize'\n",
349 /* XXX: currently only used for async signals (see signal.c) */
350 CPUState *global_env;
351 /* used only if single thread */
352 CPUState *cpu_single_env = NULL;
354 /* used to free thread contexts */
355 TaskState *first_task_state;
357 int main(int argc, char **argv)
359 const char *filename;
360 struct target_pt_regs regs1, *regs = ®s1;
361 struct image_info info1, *info = &info1;
362 TaskState ts1, *ts = &ts1;
380 if (!strcmp(r, "-")) {
382 } else if (!strcmp(r, "d")) {
384 } else if (!strcmp(r, "s")) {
386 x86_stack_size = strtol(r, (char **)&r, 0);
387 if (x86_stack_size <= 0)
390 x86_stack_size *= 1024 * 1024;
391 else if (*r == 'k' || *r == 'K')
392 x86_stack_size *= 1024;
393 } else if (!strcmp(r, "L")) {
394 interp_prefix = argv[optind++];
395 } else if (!strcmp(r, "p")) {
396 host_page_size = atoi(argv[optind++]);
397 if (host_page_size == 0 ||
398 (host_page_size & (host_page_size - 1)) != 0) {
399 fprintf(stderr, "page size must be a power of two\n");
408 filename = argv[optind];
412 logfile = fopen(DEBUG_LOGFILE, "w");
414 perror(DEBUG_LOGFILE);
417 setvbuf(logfile, NULL, _IOLBF, 0);
421 memset(regs, 0, sizeof(struct target_pt_regs));
423 /* Zero out image_info */
424 memset(info, 0, sizeof(struct image_info));
426 /* Scan interp_prefix dir for replacement files. */
427 init_paths(interp_prefix);
429 /* NOTE: we need to init the CPU at this stage to get the
433 if (elf_exec(filename, argv+optind, environ, regs, info) != 0) {
434 printf("Error loading %s\n", filename);
441 fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk);
442 fprintf(logfile, "end_code 0x%08lx\n" , info->end_code);
443 fprintf(logfile, "start_code 0x%08lx\n" , info->start_code);
444 fprintf(logfile, "end_data 0x%08lx\n" , info->end_data);
445 fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
446 fprintf(logfile, "brk 0x%08lx\n" , info->brk);
447 fprintf(logfile, "entry 0x%08lx\n" , info->entry);
450 target_set_brk((char *)info->brk);
456 /* build Task State */
457 memset(ts, 0, sizeof(TaskState));
460 env->user_mode_only = 1;
462 #if defined(TARGET_I386)
463 cpu_x86_set_cpl(env, 3);
465 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
467 /* linux register setup */
468 env->regs[R_EAX] = regs->eax;
469 env->regs[R_EBX] = regs->ebx;
470 env->regs[R_ECX] = regs->ecx;
471 env->regs[R_EDX] = regs->edx;
472 env->regs[R_ESI] = regs->esi;
473 env->regs[R_EDI] = regs->edi;
474 env->regs[R_EBP] = regs->ebp;
475 env->regs[R_ESP] = regs->esp;
476 env->eip = regs->eip;
478 /* linux interrupt setup */
479 env->idt.base = (void *)idt_table;
480 env->idt.limit = sizeof(idt_table) - 1;
503 /* linux segment setup */
504 env->gdt.base = (void *)gdt_table;
505 env->gdt.limit = sizeof(gdt_table) - 1;
506 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
507 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
508 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
509 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
510 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
511 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
512 cpu_x86_load_seg(env, R_CS, __USER_CS);
513 cpu_x86_load_seg(env, R_DS, __USER_DS);
514 cpu_x86_load_seg(env, R_ES, __USER_DS);
515 cpu_x86_load_seg(env, R_SS, __USER_DS);
516 cpu_x86_load_seg(env, R_FS, __USER_DS);
517 cpu_x86_load_seg(env, R_GS, __USER_DS);
519 #elif defined(TARGET_ARM)
522 for(i = 0; i < 16; i++) {
523 env->regs[i] = regs->uregs[i];
525 env->cpsr = regs->uregs[16];
527 #elif defined(TARGET_SPARC)
528 env->pc = regs->u_regs[0];
529 env->regwptr[6] = regs->u_regs[1]-0x40;
531 #error unsupported target CPU