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 dummies symbol 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 printf("qemu version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
305 "usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]\n"
306 "Linux CPU emulator (compiled for %s emulation)\n"
308 "-h print this help\n"
309 "-L path set the elf interpreter prefix (default=%s)\n"
310 "-s size set the stack size in bytes (default=%ld)\n"
313 "-d activate log (logfile=%s)\n"
314 "-p pagesize set the host page size to 'pagesize'\n",
322 /* XXX: currently only used for async signals (see signal.c) */
323 CPUState *global_env;
324 /* used only if single thread */
325 CPUState *cpu_single_env = NULL;
327 /* used to free thread contexts */
328 TaskState *first_task_state;
330 int main(int argc, char **argv)
332 const char *filename;
333 struct target_pt_regs regs1, *regs = ®s1;
334 struct image_info info1, *info = &info1;
335 TaskState ts1, *ts = &ts1;
353 if (!strcmp(r, "-")) {
355 } else if (!strcmp(r, "d")) {
357 } else if (!strcmp(r, "s")) {
359 x86_stack_size = strtol(r, (char **)&r, 0);
360 if (x86_stack_size <= 0)
363 x86_stack_size *= 1024 * 1024;
364 else if (*r == 'k' || *r == 'K')
365 x86_stack_size *= 1024;
366 } else if (!strcmp(r, "L")) {
367 interp_prefix = argv[optind++];
368 } else if (!strcmp(r, "p")) {
369 host_page_size = atoi(argv[optind++]);
370 if (host_page_size == 0 ||
371 (host_page_size & (host_page_size - 1)) != 0) {
372 fprintf(stderr, "page size must be a power of two\n");
381 filename = argv[optind];
385 logfile = fopen(DEBUG_LOGFILE, "w");
387 perror(DEBUG_LOGFILE);
390 setvbuf(logfile, NULL, _IOLBF, 0);
394 memset(regs, 0, sizeof(struct target_pt_regs));
396 /* Zero out image_info */
397 memset(info, 0, sizeof(struct image_info));
399 /* Scan interp_prefix dir for replacement files. */
400 init_paths(interp_prefix);
402 /* NOTE: we need to init the CPU at this stage to get the
406 if (elf_exec(filename, argv+optind, environ, regs, info) != 0) {
407 printf("Error loading %s\n", filename);
414 fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk);
415 fprintf(logfile, "end_code 0x%08lx\n" , info->end_code);
416 fprintf(logfile, "start_code 0x%08lx\n" , info->start_code);
417 fprintf(logfile, "end_data 0x%08lx\n" , info->end_data);
418 fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
419 fprintf(logfile, "brk 0x%08lx\n" , info->brk);
420 fprintf(logfile, "entry 0x%08lx\n" , info->entry);
423 target_set_brk((char *)info->brk);
429 /* build Task State */
430 memset(ts, 0, sizeof(TaskState));
433 env->user_mode_only = 1;
435 #if defined(TARGET_I386)
436 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
438 /* linux register setup */
439 env->regs[R_EAX] = regs->eax;
440 env->regs[R_EBX] = regs->ebx;
441 env->regs[R_ECX] = regs->ecx;
442 env->regs[R_EDX] = regs->edx;
443 env->regs[R_ESI] = regs->esi;
444 env->regs[R_EDI] = regs->edi;
445 env->regs[R_EBP] = regs->ebp;
446 env->regs[R_ESP] = regs->esp;
447 env->eip = regs->eip;
449 /* linux interrupt setup */
450 env->idt.base = (void *)idt_table;
451 env->idt.limit = sizeof(idt_table) - 1;
474 /* linux segment setup */
475 env->gdt.base = (void *)gdt_table;
476 env->gdt.limit = sizeof(gdt_table) - 1;
477 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
478 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
479 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
480 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
481 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
482 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
483 cpu_x86_load_seg(env, R_CS, __USER_CS);
484 cpu_x86_load_seg(env, R_DS, __USER_DS);
485 cpu_x86_load_seg(env, R_ES, __USER_DS);
486 cpu_x86_load_seg(env, R_SS, __USER_DS);
487 cpu_x86_load_seg(env, R_FS, __USER_DS);
488 cpu_x86_load_seg(env, R_GS, __USER_DS);
490 #elif defined(TARGET_ARM)
493 for(i = 0; i < 16; i++) {
494 env->regs[i] = regs->uregs[i];
496 env->cpsr = regs->uregs[16];
499 #error unsupported target CPU