4 * Copyright (c) 2003-2008 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., 51 Franklin Street - Fifth Floor, Boston,
28 #include <sys/syscall.h>
31 #include "qemu-common.h"
32 #include "cache-utils.h"
39 #define DEBUG_LOGFILE "/tmp/qemu.log"
44 #if defined(CONFIG_USE_GUEST_BASE)
45 unsigned long mmap_min_addr = 0;
46 unsigned long guest_base = 0;
49 static const char *interp_prefix = CONFIG_QEMU_PREFIX;
50 const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
52 #if defined(__i386__) && !defined(CONFIG_STATIC)
53 /* Force usage of an ELF interpreter even if it is an ELF shared
55 const char interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
58 /* for recent libc, we add these dummy symbols which are not declared
59 when generating a linked object (bug in ld ?) */
60 #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined(CONFIG_STATIC)
61 asm(".globl __preinit_array_start\n"
62 ".globl __preinit_array_end\n"
63 ".globl __init_array_start\n"
64 ".globl __init_array_end\n"
65 ".globl __fini_array_start\n"
66 ".globl __fini_array_end\n"
67 ".section \".rodata\"\n"
68 "__preinit_array_start:\n"
69 "__preinit_array_end:\n"
70 "__init_array_start:\n"
72 "__fini_array_start:\n"
78 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
79 we allocate a bigger stack. Need a better solution, for example
80 by remapping the process stack directly at the right place */
81 unsigned long x86_stack_size = 512 * 1024;
83 void gemu_log(const char *fmt, ...)
88 vfprintf(stderr, fmt, ap);
92 void cpu_outb(CPUState *env, int addr, int val)
94 fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
97 void cpu_outw(CPUState *env, int addr, int val)
99 fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
102 void cpu_outl(CPUState *env, int addr, int val)
104 fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
107 int cpu_inb(CPUState *env, int addr)
109 fprintf(stderr, "inb: port=0x%04x\n", addr);
113 int cpu_inw(CPUState *env, int addr)
115 fprintf(stderr, "inw: port=0x%04x\n", addr);
119 int cpu_inl(CPUState *env, int addr)
121 fprintf(stderr, "inl: port=0x%04x\n", addr);
125 #if defined(TARGET_I386)
126 int cpu_get_pic_interrupt(CPUState *env)
132 /* timers for rdtsc */
136 static uint64_t emu_time;
138 int64_t cpu_get_real_ticks(void)
145 #if defined(USE_NPTL)
146 /***********************************************************/
147 /* Helper routines for implementing atomic operations. */
149 /* To implement exclusive operations we force all cpus to syncronise.
150 We don't require a full sync, only that no cpus are executing guest code.
151 The alternative is to map target atomic ops onto host equivalents,
152 which requires quite a lot of per host/target work. */
153 static pthread_mutex_t cpu_list_mutex = PTHREAD_MUTEX_INITIALIZER;
154 static pthread_mutex_t exclusive_lock = PTHREAD_MUTEX_INITIALIZER;
155 static pthread_cond_t exclusive_cond = PTHREAD_COND_INITIALIZER;
156 static pthread_cond_t exclusive_resume = PTHREAD_COND_INITIALIZER;
157 static int pending_cpus;
159 /* Make sure everything is in a consistent state for calling fork(). */
160 void fork_start(void)
163 pthread_mutex_lock(&tb_lock);
164 pthread_mutex_lock(&exclusive_lock);
167 void fork_end(int child)
170 /* Child processes created by fork() only have a single thread.
171 Discard information about the parent threads. */
172 first_cpu = thread_env;
173 thread_env->next_cpu = NULL;
175 pthread_mutex_init(&exclusive_lock, NULL);
176 pthread_mutex_init(&cpu_list_mutex, NULL);
177 pthread_cond_init(&exclusive_cond, NULL);
178 pthread_cond_init(&exclusive_resume, NULL);
179 pthread_mutex_init(&tb_lock, NULL);
180 gdbserver_fork(thread_env);
182 pthread_mutex_unlock(&exclusive_lock);
183 pthread_mutex_unlock(&tb_lock);
185 mmap_fork_end(child);
188 /* Wait for pending exclusive operations to complete. The exclusive lock
190 static inline void exclusive_idle(void)
192 while (pending_cpus) {
193 pthread_cond_wait(&exclusive_resume, &exclusive_lock);
197 /* Start an exclusive operation.
198 Must only be called from outside cpu_arm_exec. */
199 static inline void start_exclusive(void)
202 pthread_mutex_lock(&exclusive_lock);
206 /* Make all other cpus stop executing. */
207 for (other = first_cpu; other; other = other->next_cpu) {
208 if (other->running) {
213 if (pending_cpus > 1) {
214 pthread_cond_wait(&exclusive_cond, &exclusive_lock);
218 /* Finish an exclusive operation. */
219 static inline void end_exclusive(void)
222 pthread_cond_broadcast(&exclusive_resume);
223 pthread_mutex_unlock(&exclusive_lock);
226 /* Wait for exclusive ops to finish, and begin cpu execution. */
227 static inline void cpu_exec_start(CPUState *env)
229 pthread_mutex_lock(&exclusive_lock);
232 pthread_mutex_unlock(&exclusive_lock);
235 /* Mark cpu as not executing, and release pending exclusive ops. */
236 static inline void cpu_exec_end(CPUState *env)
238 pthread_mutex_lock(&exclusive_lock);
240 if (pending_cpus > 1) {
242 if (pending_cpus == 1) {
243 pthread_cond_signal(&exclusive_cond);
247 pthread_mutex_unlock(&exclusive_lock);
250 void cpu_list_lock(void)
252 pthread_mutex_lock(&cpu_list_mutex);
255 void cpu_list_unlock(void)
257 pthread_mutex_unlock(&cpu_list_mutex);
259 #else /* if !USE_NPTL */
260 /* These are no-ops because we are not threadsafe. */
261 static inline void cpu_exec_start(CPUState *env)
265 static inline void cpu_exec_end(CPUState *env)
269 static inline void start_exclusive(void)
273 static inline void end_exclusive(void)
277 void fork_start(void)
281 void fork_end(int child)
284 gdbserver_fork(thread_env);
288 void cpu_list_lock(void)
292 void cpu_list_unlock(void)
299 /***********************************************************/
300 /* CPUX86 core interface */
302 void cpu_smm_update(CPUState *env)
306 uint64_t cpu_get_tsc(CPUX86State *env)
308 return cpu_get_real_ticks();
311 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
316 e1 = (addr << 16) | (limit & 0xffff);
317 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
324 static uint64_t *idt_table;
326 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
327 uint64_t addr, unsigned int sel)
330 e1 = (addr & 0xffff) | (sel << 16);
331 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
335 p[2] = tswap32(addr >> 32);
338 /* only dpl matters as we do only user space emulation */
339 static void set_idt(int n, unsigned int dpl)
341 set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
344 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
345 uint32_t addr, unsigned int sel)
348 e1 = (addr & 0xffff) | (sel << 16);
349 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
355 /* only dpl matters as we do only user space emulation */
356 static void set_idt(int n, unsigned int dpl)
358 set_gate(idt_table + n, 0, dpl, 0, 0);
362 void cpu_loop(CPUX86State *env)
366 target_siginfo_t info;
369 trapnr = cpu_x86_exec(env);
372 /* linux syscall from int $0x80 */
373 env->regs[R_EAX] = do_syscall(env,
384 /* linux syscall from syscall intruction */
385 env->regs[R_EAX] = do_syscall(env,
393 env->eip = env->exception_next_eip;
398 info.si_signo = SIGBUS;
400 info.si_code = TARGET_SI_KERNEL;
401 info._sifields._sigfault._addr = 0;
402 queue_signal(env, info.si_signo, &info);
405 /* XXX: potential problem if ABI32 */
406 #ifndef TARGET_X86_64
407 if (env->eflags & VM_MASK) {
408 handle_vm86_fault(env);
412 info.si_signo = SIGSEGV;
414 info.si_code = TARGET_SI_KERNEL;
415 info._sifields._sigfault._addr = 0;
416 queue_signal(env, info.si_signo, &info);
420 info.si_signo = SIGSEGV;
422 if (!(env->error_code & 1))
423 info.si_code = TARGET_SEGV_MAPERR;
425 info.si_code = TARGET_SEGV_ACCERR;
426 info._sifields._sigfault._addr = env->cr[2];
427 queue_signal(env, info.si_signo, &info);
430 #ifndef TARGET_X86_64
431 if (env->eflags & VM_MASK) {
432 handle_vm86_trap(env, trapnr);
436 /* division by zero */
437 info.si_signo = SIGFPE;
439 info.si_code = TARGET_FPE_INTDIV;
440 info._sifields._sigfault._addr = env->eip;
441 queue_signal(env, info.si_signo, &info);
446 #ifndef TARGET_X86_64
447 if (env->eflags & VM_MASK) {
448 handle_vm86_trap(env, trapnr);
452 info.si_signo = SIGTRAP;
454 if (trapnr == EXCP01_DB) {
455 info.si_code = TARGET_TRAP_BRKPT;
456 info._sifields._sigfault._addr = env->eip;
458 info.si_code = TARGET_SI_KERNEL;
459 info._sifields._sigfault._addr = 0;
461 queue_signal(env, info.si_signo, &info);
466 #ifndef TARGET_X86_64
467 if (env->eflags & VM_MASK) {
468 handle_vm86_trap(env, trapnr);
472 info.si_signo = SIGSEGV;
474 info.si_code = TARGET_SI_KERNEL;
475 info._sifields._sigfault._addr = 0;
476 queue_signal(env, info.si_signo, &info);
480 info.si_signo = SIGILL;
482 info.si_code = TARGET_ILL_ILLOPN;
483 info._sifields._sigfault._addr = env->eip;
484 queue_signal(env, info.si_signo, &info);
487 /* just indicate that signals should be handled asap */
493 sig = gdb_handlesig (env, TARGET_SIGTRAP);
498 info.si_code = TARGET_TRAP_BRKPT;
499 queue_signal(env, info.si_signo, &info);
504 pc = env->segs[R_CS].base + env->eip;
505 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
509 process_pending_signals(env);
516 static void arm_cache_flush(abi_ulong start, abi_ulong last)
518 abi_ulong addr, last1;
524 last1 = ((addr + TARGET_PAGE_SIZE) & TARGET_PAGE_MASK) - 1;
527 tb_invalidate_page_range(addr, last1 + 1);
534 /* Handle a jump to the kernel code page. */
536 do_kernel_trap(CPUARMState *env)
542 switch (env->regs[15]) {
543 case 0xffff0fa0: /* __kernel_memory_barrier */
544 /* ??? No-op. Will need to do better for SMP. */
546 case 0xffff0fc0: /* __kernel_cmpxchg */
547 /* XXX: This only works between threads, not between processes.
548 It's probably possible to implement this with native host
549 operations. However things like ldrex/strex are much harder so
550 there's not much point trying. */
552 cpsr = cpsr_read(env);
554 /* FIXME: This should SEGV if the access fails. */
555 if (get_user_u32(val, addr))
557 if (val == env->regs[0]) {
559 /* FIXME: Check for segfaults. */
560 put_user_u32(val, addr);
567 cpsr_write(env, cpsr, CPSR_C);
570 case 0xffff0fe0: /* __kernel_get_tls */
571 env->regs[0] = env->cp15.c13_tls2;
576 /* Jump back to the caller. */
577 addr = env->regs[14];
582 env->regs[15] = addr;
587 void cpu_loop(CPUARMState *env)
590 unsigned int n, insn;
591 target_siginfo_t info;
596 trapnr = cpu_arm_exec(env);
601 TaskState *ts = env->opaque;
605 /* we handle the FPU emulation here, as Linux */
606 /* we get the opcode */
607 /* FIXME - what to do if get_user() fails? */
608 get_user_u32(opcode, env->regs[15]);
610 rc = EmulateAll(opcode, &ts->fpa, env);
611 if (rc == 0) { /* illegal instruction */
612 info.si_signo = SIGILL;
614 info.si_code = TARGET_ILL_ILLOPN;
615 info._sifields._sigfault._addr = env->regs[15];
616 queue_signal(env, info.si_signo, &info);
617 } else if (rc < 0) { /* FP exception */
620 /* translate softfloat flags to FPSR flags */
621 if (-rc & float_flag_invalid)
623 if (-rc & float_flag_divbyzero)
625 if (-rc & float_flag_overflow)
627 if (-rc & float_flag_underflow)
629 if (-rc & float_flag_inexact)
632 FPSR fpsr = ts->fpa.fpsr;
633 //printf("fpsr 0x%x, arm_fpe 0x%x\n",fpsr,arm_fpe);
635 if (fpsr & (arm_fpe << 16)) { /* exception enabled? */
636 info.si_signo = SIGFPE;
639 /* ordered by priority, least first */
640 if (arm_fpe & BIT_IXC) info.si_code = TARGET_FPE_FLTRES;
641 if (arm_fpe & BIT_UFC) info.si_code = TARGET_FPE_FLTUND;
642 if (arm_fpe & BIT_OFC) info.si_code = TARGET_FPE_FLTOVF;
643 if (arm_fpe & BIT_DZC) info.si_code = TARGET_FPE_FLTDIV;
644 if (arm_fpe & BIT_IOC) info.si_code = TARGET_FPE_FLTINV;
646 info._sifields._sigfault._addr = env->regs[15];
647 queue_signal(env, info.si_signo, &info);
652 /* accumulate unenabled exceptions */
653 if ((!(fpsr & BIT_IXE)) && (arm_fpe & BIT_IXC))
655 if ((!(fpsr & BIT_UFE)) && (arm_fpe & BIT_UFC))
657 if ((!(fpsr & BIT_OFE)) && (arm_fpe & BIT_OFC))
659 if ((!(fpsr & BIT_DZE)) && (arm_fpe & BIT_DZC))
661 if ((!(fpsr & BIT_IOE)) && (arm_fpe & BIT_IOC))
664 } else { /* everything OK */
675 if (trapnr == EXCP_BKPT) {
677 /* FIXME - what to do if get_user() fails? */
678 get_user_u16(insn, env->regs[15]);
682 /* FIXME - what to do if get_user() fails? */
683 get_user_u32(insn, env->regs[15]);
684 n = (insn & 0xf) | ((insn >> 4) & 0xff0);
689 /* FIXME - what to do if get_user() fails? */
690 get_user_u16(insn, env->regs[15] - 2);
693 /* FIXME - what to do if get_user() fails? */
694 get_user_u32(insn, env->regs[15] - 4);
699 if (n == ARM_NR_cacheflush) {
700 arm_cache_flush(env->regs[0], env->regs[1]);
701 } else if (n == ARM_NR_semihosting
702 || n == ARM_NR_thumb_semihosting) {
703 env->regs[0] = do_arm_semihosting (env);
704 } else if (n == 0 || n >= ARM_SYSCALL_BASE
705 || (env->thumb && n == ARM_THUMB_SYSCALL)) {
707 if (env->thumb || n == 0) {
710 n -= ARM_SYSCALL_BASE;
713 if ( n > ARM_NR_BASE) {
715 case ARM_NR_cacheflush:
716 arm_cache_flush(env->regs[0], env->regs[1]);
719 cpu_set_tls(env, env->regs[0]);
723 gemu_log("qemu: Unsupported ARM syscall: 0x%x\n",
725 env->regs[0] = -TARGET_ENOSYS;
729 env->regs[0] = do_syscall(env,
744 /* just indicate that signals should be handled asap */
746 case EXCP_PREFETCH_ABORT:
747 addr = env->cp15.c6_insn;
749 case EXCP_DATA_ABORT:
750 addr = env->cp15.c6_data;
754 info.si_signo = SIGSEGV;
756 /* XXX: check env->error_code */
757 info.si_code = TARGET_SEGV_MAPERR;
758 info._sifields._sigfault._addr = addr;
759 queue_signal(env, info.si_signo, &info);
766 sig = gdb_handlesig (env, TARGET_SIGTRAP);
771 info.si_code = TARGET_TRAP_BRKPT;
772 queue_signal(env, info.si_signo, &info);
776 case EXCP_KERNEL_TRAP:
777 if (do_kernel_trap(env))
782 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
784 cpu_dump_state(env, stderr, fprintf, 0);
787 process_pending_signals(env);
794 #define SPARC64_STACK_BIAS 2047
798 /* WARNING: dealing with register windows _is_ complicated. More info
799 can be found at http://www.sics.se/~psm/sparcstack.html */
800 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
802 index = (index + cwp * 16) % (16 * env->nwindows);
803 /* wrap handling : if cwp is on the last window, then we use the
804 registers 'after' the end */
805 if (index < 8 && env->cwp == env->nwindows - 1)
806 index += 16 * env->nwindows;
810 /* save the register window 'cwp1' */
811 static inline void save_window_offset(CPUSPARCState *env, int cwp1)
816 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
817 #ifdef TARGET_SPARC64
819 sp_ptr += SPARC64_STACK_BIAS;
821 #if defined(DEBUG_WIN)
822 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
825 for(i = 0; i < 16; i++) {
826 /* FIXME - what to do if put_user() fails? */
827 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
828 sp_ptr += sizeof(abi_ulong);
832 static void save_window(CPUSPARCState *env)
834 #ifndef TARGET_SPARC64
835 unsigned int new_wim;
836 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
837 ((1LL << env->nwindows) - 1);
838 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
841 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
847 static void restore_window(CPUSPARCState *env)
849 #ifndef TARGET_SPARC64
850 unsigned int new_wim;
852 unsigned int i, cwp1;
855 #ifndef TARGET_SPARC64
856 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
857 ((1LL << env->nwindows) - 1);
860 /* restore the invalid window */
861 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
862 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
863 #ifdef TARGET_SPARC64
865 sp_ptr += SPARC64_STACK_BIAS;
867 #if defined(DEBUG_WIN)
868 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
871 for(i = 0; i < 16; i++) {
872 /* FIXME - what to do if get_user() fails? */
873 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
874 sp_ptr += sizeof(abi_ulong);
876 #ifdef TARGET_SPARC64
878 if (env->cleanwin < env->nwindows - 1)
886 static void flush_windows(CPUSPARCState *env)
892 /* if restore would invoke restore_window(), then we can stop */
893 cwp1 = cpu_cwp_inc(env, env->cwp + offset);
894 #ifndef TARGET_SPARC64
895 if (env->wim & (1 << cwp1))
898 if (env->canrestore == 0)
903 save_window_offset(env, cwp1);
906 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
907 #ifndef TARGET_SPARC64
908 /* set wim so that restore will reload the registers */
909 env->wim = 1 << cwp1;
911 #if defined(DEBUG_WIN)
912 printf("flush_windows: nb=%d\n", offset - 1);
916 void cpu_loop (CPUSPARCState *env)
919 target_siginfo_t info;
922 trapnr = cpu_sparc_exec (env);
925 #ifndef TARGET_SPARC64
932 ret = do_syscall (env, env->gregs[1],
933 env->regwptr[0], env->regwptr[1],
934 env->regwptr[2], env->regwptr[3],
935 env->regwptr[4], env->regwptr[5]);
936 if ((unsigned int)ret >= (unsigned int)(-515)) {
937 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
938 env->xcc |= PSR_CARRY;
940 env->psr |= PSR_CARRY;
944 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
945 env->xcc &= ~PSR_CARRY;
947 env->psr &= ~PSR_CARRY;
950 env->regwptr[0] = ret;
951 /* next instruction */
953 env->npc = env->npc + 4;
955 case 0x83: /* flush windows */
960 /* next instruction */
962 env->npc = env->npc + 4;
964 #ifndef TARGET_SPARC64
965 case TT_WIN_OVF: /* window overflow */
968 case TT_WIN_UNF: /* window underflow */
974 info.si_signo = SIGSEGV;
976 /* XXX: check env->error_code */
977 info.si_code = TARGET_SEGV_MAPERR;
978 info._sifields._sigfault._addr = env->mmuregs[4];
979 queue_signal(env, info.si_signo, &info);
983 case TT_SPILL: /* window overflow */
986 case TT_FILL: /* window underflow */
992 info.si_signo = SIGSEGV;
994 /* XXX: check env->error_code */
995 info.si_code = TARGET_SEGV_MAPERR;
996 if (trapnr == TT_DFAULT)
997 info._sifields._sigfault._addr = env->dmmuregs[4];
999 info._sifields._sigfault._addr = env->tsptr->tpc;
1000 queue_signal(env, info.si_signo, &info);
1003 #ifndef TARGET_ABI32
1006 sparc64_get_context(env);
1010 sparc64_set_context(env);
1014 case EXCP_INTERRUPT:
1015 /* just indicate that signals should be handled asap */
1021 sig = gdb_handlesig (env, TARGET_SIGTRAP);
1024 info.si_signo = sig;
1026 info.si_code = TARGET_TRAP_BRKPT;
1027 queue_signal(env, info.si_signo, &info);
1032 printf ("Unhandled trap: 0x%x\n", trapnr);
1033 cpu_dump_state(env, stderr, fprintf, 0);
1036 process_pending_signals (env);
1043 static inline uint64_t cpu_ppc_get_tb (CPUState *env)
1049 uint32_t cpu_ppc_load_tbl (CPUState *env)
1051 return cpu_ppc_get_tb(env) & 0xFFFFFFFF;
1054 uint32_t cpu_ppc_load_tbu (CPUState *env)
1056 return cpu_ppc_get_tb(env) >> 32;
1059 uint32_t cpu_ppc_load_atbl (CPUState *env)
1061 return cpu_ppc_get_tb(env) & 0xFFFFFFFF;
1064 uint32_t cpu_ppc_load_atbu (CPUState *env)
1066 return cpu_ppc_get_tb(env) >> 32;
1069 uint32_t cpu_ppc601_load_rtcu (CPUState *env)
1070 __attribute__ (( alias ("cpu_ppc_load_tbu") ));
1072 uint32_t cpu_ppc601_load_rtcl (CPUState *env)
1074 return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
1077 /* XXX: to be fixed */
1078 int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, target_ulong *valp)
1083 int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, target_ulong val)
1088 #define EXCP_DUMP(env, fmt, args...) \
1090 fprintf(stderr, fmt , ##args); \
1091 cpu_dump_state(env, stderr, fprintf, 0); \
1092 qemu_log(fmt, ##args); \
1093 log_cpu_state(env, 0); \
1096 void cpu_loop(CPUPPCState *env)
1098 target_siginfo_t info;
1103 trapnr = cpu_ppc_exec(env);
1105 case POWERPC_EXCP_NONE:
1108 case POWERPC_EXCP_CRITICAL: /* Critical input */
1109 cpu_abort(env, "Critical interrupt while in user mode. "
1112 case POWERPC_EXCP_MCHECK: /* Machine check exception */
1113 cpu_abort(env, "Machine check exception while in user mode. "
1116 case POWERPC_EXCP_DSI: /* Data storage exception */
1117 EXCP_DUMP(env, "Invalid data memory access: 0x" ADDRX "\n",
1119 /* XXX: check this. Seems bugged */
1120 switch (env->error_code & 0xFF000000) {
1122 info.si_signo = TARGET_SIGSEGV;
1124 info.si_code = TARGET_SEGV_MAPERR;
1127 info.si_signo = TARGET_SIGILL;
1129 info.si_code = TARGET_ILL_ILLADR;
1132 info.si_signo = TARGET_SIGSEGV;
1134 info.si_code = TARGET_SEGV_ACCERR;
1137 /* Let's send a regular segfault... */
1138 EXCP_DUMP(env, "Invalid segfault errno (%02x)\n",
1140 info.si_signo = TARGET_SIGSEGV;
1142 info.si_code = TARGET_SEGV_MAPERR;
1145 info._sifields._sigfault._addr = env->nip;
1146 queue_signal(env, info.si_signo, &info);
1148 case POWERPC_EXCP_ISI: /* Instruction storage exception */
1149 EXCP_DUMP(env, "Invalid instruction fetch: 0x\n" ADDRX "\n",
1150 env->spr[SPR_SRR0]);
1151 /* XXX: check this */
1152 switch (env->error_code & 0xFF000000) {
1154 info.si_signo = TARGET_SIGSEGV;
1156 info.si_code = TARGET_SEGV_MAPERR;
1160 info.si_signo = TARGET_SIGSEGV;
1162 info.si_code = TARGET_SEGV_ACCERR;
1165 /* Let's send a regular segfault... */
1166 EXCP_DUMP(env, "Invalid segfault errno (%02x)\n",
1168 info.si_signo = TARGET_SIGSEGV;
1170 info.si_code = TARGET_SEGV_MAPERR;
1173 info._sifields._sigfault._addr = env->nip - 4;
1174 queue_signal(env, info.si_signo, &info);
1176 case POWERPC_EXCP_EXTERNAL: /* External input */
1177 cpu_abort(env, "External interrupt while in user mode. "
1180 case POWERPC_EXCP_ALIGN: /* Alignment exception */
1181 EXCP_DUMP(env, "Unaligned memory access\n");
1182 /* XXX: check this */
1183 info.si_signo = TARGET_SIGBUS;
1185 info.si_code = TARGET_BUS_ADRALN;
1186 info._sifields._sigfault._addr = env->nip - 4;
1187 queue_signal(env, info.si_signo, &info);
1189 case POWERPC_EXCP_PROGRAM: /* Program exception */
1190 /* XXX: check this */
1191 switch (env->error_code & ~0xF) {
1192 case POWERPC_EXCP_FP:
1193 EXCP_DUMP(env, "Floating point program exception\n");
1194 info.si_signo = TARGET_SIGFPE;
1196 switch (env->error_code & 0xF) {
1197 case POWERPC_EXCP_FP_OX:
1198 info.si_code = TARGET_FPE_FLTOVF;
1200 case POWERPC_EXCP_FP_UX:
1201 info.si_code = TARGET_FPE_FLTUND;
1203 case POWERPC_EXCP_FP_ZX:
1204 case POWERPC_EXCP_FP_VXZDZ:
1205 info.si_code = TARGET_FPE_FLTDIV;
1207 case POWERPC_EXCP_FP_XX:
1208 info.si_code = TARGET_FPE_FLTRES;
1210 case POWERPC_EXCP_FP_VXSOFT:
1211 info.si_code = TARGET_FPE_FLTINV;
1213 case POWERPC_EXCP_FP_VXSNAN:
1214 case POWERPC_EXCP_FP_VXISI:
1215 case POWERPC_EXCP_FP_VXIDI:
1216 case POWERPC_EXCP_FP_VXIMZ:
1217 case POWERPC_EXCP_FP_VXVC:
1218 case POWERPC_EXCP_FP_VXSQRT:
1219 case POWERPC_EXCP_FP_VXCVI:
1220 info.si_code = TARGET_FPE_FLTSUB;
1223 EXCP_DUMP(env, "Unknown floating point exception (%02x)\n",
1228 case POWERPC_EXCP_INVAL:
1229 EXCP_DUMP(env, "Invalid instruction\n");
1230 info.si_signo = TARGET_SIGILL;
1232 switch (env->error_code & 0xF) {
1233 case POWERPC_EXCP_INVAL_INVAL:
1234 info.si_code = TARGET_ILL_ILLOPC;
1236 case POWERPC_EXCP_INVAL_LSWX:
1237 info.si_code = TARGET_ILL_ILLOPN;
1239 case POWERPC_EXCP_INVAL_SPR:
1240 info.si_code = TARGET_ILL_PRVREG;
1242 case POWERPC_EXCP_INVAL_FP:
1243 info.si_code = TARGET_ILL_COPROC;
1246 EXCP_DUMP(env, "Unknown invalid operation (%02x)\n",
1247 env->error_code & 0xF);
1248 info.si_code = TARGET_ILL_ILLADR;
1252 case POWERPC_EXCP_PRIV:
1253 EXCP_DUMP(env, "Privilege violation\n");
1254 info.si_signo = TARGET_SIGILL;
1256 switch (env->error_code & 0xF) {
1257 case POWERPC_EXCP_PRIV_OPC:
1258 info.si_code = TARGET_ILL_PRVOPC;
1260 case POWERPC_EXCP_PRIV_REG:
1261 info.si_code = TARGET_ILL_PRVREG;
1264 EXCP_DUMP(env, "Unknown privilege violation (%02x)\n",
1265 env->error_code & 0xF);
1266 info.si_code = TARGET_ILL_PRVOPC;
1270 case POWERPC_EXCP_TRAP:
1271 cpu_abort(env, "Tried to call a TRAP\n");
1274 /* Should not happen ! */
1275 cpu_abort(env, "Unknown program exception (%02x)\n",
1279 info._sifields._sigfault._addr = env->nip - 4;
1280 queue_signal(env, info.si_signo, &info);
1282 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
1283 EXCP_DUMP(env, "No floating point allowed\n");
1284 info.si_signo = TARGET_SIGILL;
1286 info.si_code = TARGET_ILL_COPROC;
1287 info._sifields._sigfault._addr = env->nip - 4;
1288 queue_signal(env, info.si_signo, &info);
1290 case POWERPC_EXCP_SYSCALL: /* System call exception */
1291 cpu_abort(env, "Syscall exception while in user mode. "
1294 case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */
1295 EXCP_DUMP(env, "No APU instruction allowed\n");
1296 info.si_signo = TARGET_SIGILL;
1298 info.si_code = TARGET_ILL_COPROC;
1299 info._sifields._sigfault._addr = env->nip - 4;
1300 queue_signal(env, info.si_signo, &info);
1302 case POWERPC_EXCP_DECR: /* Decrementer exception */
1303 cpu_abort(env, "Decrementer interrupt while in user mode. "
1306 case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */
1307 cpu_abort(env, "Fix interval timer interrupt while in user mode. "
1310 case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */
1311 cpu_abort(env, "Watchdog timer interrupt while in user mode. "
1314 case POWERPC_EXCP_DTLB: /* Data TLB error */
1315 cpu_abort(env, "Data TLB exception while in user mode. "
1318 case POWERPC_EXCP_ITLB: /* Instruction TLB error */
1319 cpu_abort(env, "Instruction TLB exception while in user mode. "
1322 case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavail. */
1323 EXCP_DUMP(env, "No SPE/floating-point instruction allowed\n");
1324 info.si_signo = TARGET_SIGILL;
1326 info.si_code = TARGET_ILL_COPROC;
1327 info._sifields._sigfault._addr = env->nip - 4;
1328 queue_signal(env, info.si_signo, &info);
1330 case POWERPC_EXCP_EFPDI: /* Embedded floating-point data IRQ */
1331 cpu_abort(env, "Embedded floating-point data IRQ not handled\n");
1333 case POWERPC_EXCP_EFPRI: /* Embedded floating-point round IRQ */
1334 cpu_abort(env, "Embedded floating-point round IRQ not handled\n");
1336 case POWERPC_EXCP_EPERFM: /* Embedded performance monitor IRQ */
1337 cpu_abort(env, "Performance monitor exception not handled\n");
1339 case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */
1340 cpu_abort(env, "Doorbell interrupt while in user mode. "
1343 case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */
1344 cpu_abort(env, "Doorbell critical interrupt while in user mode. "
1347 case POWERPC_EXCP_RESET: /* System reset exception */
1348 cpu_abort(env, "Reset interrupt while in user mode. "
1351 case POWERPC_EXCP_DSEG: /* Data segment exception */
1352 cpu_abort(env, "Data segment exception while in user mode. "
1355 case POWERPC_EXCP_ISEG: /* Instruction segment exception */
1356 cpu_abort(env, "Instruction segment exception "
1357 "while in user mode. Aborting\n");
1359 /* PowerPC 64 with hypervisor mode support */
1360 case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */
1361 cpu_abort(env, "Hypervisor decrementer interrupt "
1362 "while in user mode. Aborting\n");
1364 case POWERPC_EXCP_TRACE: /* Trace exception */
1366 * we use this exception to emulate step-by-step execution mode.
1369 /* PowerPC 64 with hypervisor mode support */
1370 case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */
1371 cpu_abort(env, "Hypervisor data storage exception "
1372 "while in user mode. Aborting\n");
1374 case POWERPC_EXCP_HISI: /* Hypervisor instruction storage excp */
1375 cpu_abort(env, "Hypervisor instruction storage exception "
1376 "while in user mode. Aborting\n");
1378 case POWERPC_EXCP_HDSEG: /* Hypervisor data segment exception */
1379 cpu_abort(env, "Hypervisor data segment exception "
1380 "while in user mode. Aborting\n");
1382 case POWERPC_EXCP_HISEG: /* Hypervisor instruction segment excp */
1383 cpu_abort(env, "Hypervisor instruction segment exception "
1384 "while in user mode. Aborting\n");
1386 case POWERPC_EXCP_VPU: /* Vector unavailable exception */
1387 EXCP_DUMP(env, "No Altivec instructions allowed\n");
1388 info.si_signo = TARGET_SIGILL;
1390 info.si_code = TARGET_ILL_COPROC;
1391 info._sifields._sigfault._addr = env->nip - 4;
1392 queue_signal(env, info.si_signo, &info);
1394 case POWERPC_EXCP_PIT: /* Programmable interval timer IRQ */
1395 cpu_abort(env, "Programable interval timer interrupt "
1396 "while in user mode. Aborting\n");
1398 case POWERPC_EXCP_IO: /* IO error exception */
1399 cpu_abort(env, "IO error exception while in user mode. "
1402 case POWERPC_EXCP_RUNM: /* Run mode exception */
1403 cpu_abort(env, "Run mode exception while in user mode. "
1406 case POWERPC_EXCP_EMUL: /* Emulation trap exception */
1407 cpu_abort(env, "Emulation trap exception not handled\n");
1409 case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */
1410 cpu_abort(env, "Instruction fetch TLB exception "
1411 "while in user-mode. Aborting");
1413 case POWERPC_EXCP_DLTLB: /* Data load TLB miss */
1414 cpu_abort(env, "Data load TLB exception while in user-mode. "
1417 case POWERPC_EXCP_DSTLB: /* Data store TLB miss */
1418 cpu_abort(env, "Data store TLB exception while in user-mode. "
1421 case POWERPC_EXCP_FPA: /* Floating-point assist exception */
1422 cpu_abort(env, "Floating-point assist exception not handled\n");
1424 case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
1425 cpu_abort(env, "Instruction address breakpoint exception "
1428 case POWERPC_EXCP_SMI: /* System management interrupt */
1429 cpu_abort(env, "System management interrupt while in user mode. "
1432 case POWERPC_EXCP_THERM: /* Thermal interrupt */
1433 cpu_abort(env, "Thermal interrupt interrupt while in user mode. "
1436 case POWERPC_EXCP_PERFM: /* Embedded performance monitor IRQ */
1437 cpu_abort(env, "Performance monitor exception not handled\n");
1439 case POWERPC_EXCP_VPUA: /* Vector assist exception */
1440 cpu_abort(env, "Vector assist exception not handled\n");
1442 case POWERPC_EXCP_SOFTP: /* Soft patch exception */
1443 cpu_abort(env, "Soft patch exception not handled\n");
1445 case POWERPC_EXCP_MAINT: /* Maintenance exception */
1446 cpu_abort(env, "Maintenance exception while in user mode. "
1449 case POWERPC_EXCP_STOP: /* stop translation */
1450 /* We did invalidate the instruction cache. Go on */
1452 case POWERPC_EXCP_BRANCH: /* branch instruction: */
1453 /* We just stopped because of a branch. Go on */
1455 case POWERPC_EXCP_SYSCALL_USER:
1456 /* system call in user-mode emulation */
1458 * PPC ABI uses overflow flag in cr0 to signal an error
1462 printf("syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n", env->gpr[0],
1463 env->gpr[3], env->gpr[4], env->gpr[5], env->gpr[6]);
1465 env->crf[0] &= ~0x1;
1466 ret = do_syscall(env, env->gpr[0], env->gpr[3], env->gpr[4],
1467 env->gpr[5], env->gpr[6], env->gpr[7],
1469 if (ret > (uint32_t)(-515)) {
1475 printf("syscall returned 0x%08x (%d)\n", ret, ret);
1482 sig = gdb_handlesig(env, TARGET_SIGTRAP);
1484 info.si_signo = sig;
1486 info.si_code = TARGET_TRAP_BRKPT;
1487 queue_signal(env, info.si_signo, &info);
1491 case EXCP_INTERRUPT:
1492 /* just indicate that signals should be handled asap */
1495 cpu_abort(env, "Unknown exception 0x%d. Aborting\n", trapnr);
1498 process_pending_signals(env);
1505 #define MIPS_SYS(name, args) args,
1507 static const uint8_t mips_syscall_args[] = {
1508 MIPS_SYS(sys_syscall , 0) /* 4000 */
1509 MIPS_SYS(sys_exit , 1)
1510 MIPS_SYS(sys_fork , 0)
1511 MIPS_SYS(sys_read , 3)
1512 MIPS_SYS(sys_write , 3)
1513 MIPS_SYS(sys_open , 3) /* 4005 */
1514 MIPS_SYS(sys_close , 1)
1515 MIPS_SYS(sys_waitpid , 3)
1516 MIPS_SYS(sys_creat , 2)
1517 MIPS_SYS(sys_link , 2)
1518 MIPS_SYS(sys_unlink , 1) /* 4010 */
1519 MIPS_SYS(sys_execve , 0)
1520 MIPS_SYS(sys_chdir , 1)
1521 MIPS_SYS(sys_time , 1)
1522 MIPS_SYS(sys_mknod , 3)
1523 MIPS_SYS(sys_chmod , 2) /* 4015 */
1524 MIPS_SYS(sys_lchown , 3)
1525 MIPS_SYS(sys_ni_syscall , 0)
1526 MIPS_SYS(sys_ni_syscall , 0) /* was sys_stat */
1527 MIPS_SYS(sys_lseek , 3)
1528 MIPS_SYS(sys_getpid , 0) /* 4020 */
1529 MIPS_SYS(sys_mount , 5)
1530 MIPS_SYS(sys_oldumount , 1)
1531 MIPS_SYS(sys_setuid , 1)
1532 MIPS_SYS(sys_getuid , 0)
1533 MIPS_SYS(sys_stime , 1) /* 4025 */
1534 MIPS_SYS(sys_ptrace , 4)
1535 MIPS_SYS(sys_alarm , 1)
1536 MIPS_SYS(sys_ni_syscall , 0) /* was sys_fstat */
1537 MIPS_SYS(sys_pause , 0)
1538 MIPS_SYS(sys_utime , 2) /* 4030 */
1539 MIPS_SYS(sys_ni_syscall , 0)
1540 MIPS_SYS(sys_ni_syscall , 0)
1541 MIPS_SYS(sys_access , 2)
1542 MIPS_SYS(sys_nice , 1)
1543 MIPS_SYS(sys_ni_syscall , 0) /* 4035 */
1544 MIPS_SYS(sys_sync , 0)
1545 MIPS_SYS(sys_kill , 2)
1546 MIPS_SYS(sys_rename , 2)
1547 MIPS_SYS(sys_mkdir , 2)
1548 MIPS_SYS(sys_rmdir , 1) /* 4040 */
1549 MIPS_SYS(sys_dup , 1)
1550 MIPS_SYS(sys_pipe , 0)
1551 MIPS_SYS(sys_times , 1)
1552 MIPS_SYS(sys_ni_syscall , 0)
1553 MIPS_SYS(sys_brk , 1) /* 4045 */
1554 MIPS_SYS(sys_setgid , 1)
1555 MIPS_SYS(sys_getgid , 0)
1556 MIPS_SYS(sys_ni_syscall , 0) /* was signal(2) */
1557 MIPS_SYS(sys_geteuid , 0)
1558 MIPS_SYS(sys_getegid , 0) /* 4050 */
1559 MIPS_SYS(sys_acct , 0)
1560 MIPS_SYS(sys_umount , 2)
1561 MIPS_SYS(sys_ni_syscall , 0)
1562 MIPS_SYS(sys_ioctl , 3)
1563 MIPS_SYS(sys_fcntl , 3) /* 4055 */
1564 MIPS_SYS(sys_ni_syscall , 2)
1565 MIPS_SYS(sys_setpgid , 2)
1566 MIPS_SYS(sys_ni_syscall , 0)
1567 MIPS_SYS(sys_olduname , 1)
1568 MIPS_SYS(sys_umask , 1) /* 4060 */
1569 MIPS_SYS(sys_chroot , 1)
1570 MIPS_SYS(sys_ustat , 2)
1571 MIPS_SYS(sys_dup2 , 2)
1572 MIPS_SYS(sys_getppid , 0)
1573 MIPS_SYS(sys_getpgrp , 0) /* 4065 */
1574 MIPS_SYS(sys_setsid , 0)
1575 MIPS_SYS(sys_sigaction , 3)
1576 MIPS_SYS(sys_sgetmask , 0)
1577 MIPS_SYS(sys_ssetmask , 1)
1578 MIPS_SYS(sys_setreuid , 2) /* 4070 */
1579 MIPS_SYS(sys_setregid , 2)
1580 MIPS_SYS(sys_sigsuspend , 0)
1581 MIPS_SYS(sys_sigpending , 1)
1582 MIPS_SYS(sys_sethostname , 2)
1583 MIPS_SYS(sys_setrlimit , 2) /* 4075 */
1584 MIPS_SYS(sys_getrlimit , 2)
1585 MIPS_SYS(sys_getrusage , 2)
1586 MIPS_SYS(sys_gettimeofday, 2)
1587 MIPS_SYS(sys_settimeofday, 2)
1588 MIPS_SYS(sys_getgroups , 2) /* 4080 */
1589 MIPS_SYS(sys_setgroups , 2)
1590 MIPS_SYS(sys_ni_syscall , 0) /* old_select */
1591 MIPS_SYS(sys_symlink , 2)
1592 MIPS_SYS(sys_ni_syscall , 0) /* was sys_lstat */
1593 MIPS_SYS(sys_readlink , 3) /* 4085 */
1594 MIPS_SYS(sys_uselib , 1)
1595 MIPS_SYS(sys_swapon , 2)
1596 MIPS_SYS(sys_reboot , 3)
1597 MIPS_SYS(old_readdir , 3)
1598 MIPS_SYS(old_mmap , 6) /* 4090 */
1599 MIPS_SYS(sys_munmap , 2)
1600 MIPS_SYS(sys_truncate , 2)
1601 MIPS_SYS(sys_ftruncate , 2)
1602 MIPS_SYS(sys_fchmod , 2)
1603 MIPS_SYS(sys_fchown , 3) /* 4095 */
1604 MIPS_SYS(sys_getpriority , 2)
1605 MIPS_SYS(sys_setpriority , 3)
1606 MIPS_SYS(sys_ni_syscall , 0)
1607 MIPS_SYS(sys_statfs , 2)
1608 MIPS_SYS(sys_fstatfs , 2) /* 4100 */
1609 MIPS_SYS(sys_ni_syscall , 0) /* was ioperm(2) */
1610 MIPS_SYS(sys_socketcall , 2)
1611 MIPS_SYS(sys_syslog , 3)
1612 MIPS_SYS(sys_setitimer , 3)
1613 MIPS_SYS(sys_getitimer , 2) /* 4105 */
1614 MIPS_SYS(sys_newstat , 2)
1615 MIPS_SYS(sys_newlstat , 2)
1616 MIPS_SYS(sys_newfstat , 2)
1617 MIPS_SYS(sys_uname , 1)
1618 MIPS_SYS(sys_ni_syscall , 0) /* 4110 was iopl(2) */
1619 MIPS_SYS(sys_vhangup , 0)
1620 MIPS_SYS(sys_ni_syscall , 0) /* was sys_idle() */
1621 MIPS_SYS(sys_ni_syscall , 0) /* was sys_vm86 */
1622 MIPS_SYS(sys_wait4 , 4)
1623 MIPS_SYS(sys_swapoff , 1) /* 4115 */
1624 MIPS_SYS(sys_sysinfo , 1)
1625 MIPS_SYS(sys_ipc , 6)
1626 MIPS_SYS(sys_fsync , 1)
1627 MIPS_SYS(sys_sigreturn , 0)
1628 MIPS_SYS(sys_clone , 0) /* 4120 */
1629 MIPS_SYS(sys_setdomainname, 2)
1630 MIPS_SYS(sys_newuname , 1)
1631 MIPS_SYS(sys_ni_syscall , 0) /* sys_modify_ldt */
1632 MIPS_SYS(sys_adjtimex , 1)
1633 MIPS_SYS(sys_mprotect , 3) /* 4125 */
1634 MIPS_SYS(sys_sigprocmask , 3)
1635 MIPS_SYS(sys_ni_syscall , 0) /* was create_module */
1636 MIPS_SYS(sys_init_module , 5)
1637 MIPS_SYS(sys_delete_module, 1)
1638 MIPS_SYS(sys_ni_syscall , 0) /* 4130 was get_kernel_syms */
1639 MIPS_SYS(sys_quotactl , 0)
1640 MIPS_SYS(sys_getpgid , 1)
1641 MIPS_SYS(sys_fchdir , 1)
1642 MIPS_SYS(sys_bdflush , 2)
1643 MIPS_SYS(sys_sysfs , 3) /* 4135 */
1644 MIPS_SYS(sys_personality , 1)
1645 MIPS_SYS(sys_ni_syscall , 0) /* for afs_syscall */
1646 MIPS_SYS(sys_setfsuid , 1)
1647 MIPS_SYS(sys_setfsgid , 1)
1648 MIPS_SYS(sys_llseek , 5) /* 4140 */
1649 MIPS_SYS(sys_getdents , 3)
1650 MIPS_SYS(sys_select , 5)
1651 MIPS_SYS(sys_flock , 2)
1652 MIPS_SYS(sys_msync , 3)
1653 MIPS_SYS(sys_readv , 3) /* 4145 */
1654 MIPS_SYS(sys_writev , 3)
1655 MIPS_SYS(sys_cacheflush , 3)
1656 MIPS_SYS(sys_cachectl , 3)
1657 MIPS_SYS(sys_sysmips , 4)
1658 MIPS_SYS(sys_ni_syscall , 0) /* 4150 */
1659 MIPS_SYS(sys_getsid , 1)
1660 MIPS_SYS(sys_fdatasync , 0)
1661 MIPS_SYS(sys_sysctl , 1)
1662 MIPS_SYS(sys_mlock , 2)
1663 MIPS_SYS(sys_munlock , 2) /* 4155 */
1664 MIPS_SYS(sys_mlockall , 1)
1665 MIPS_SYS(sys_munlockall , 0)
1666 MIPS_SYS(sys_sched_setparam, 2)
1667 MIPS_SYS(sys_sched_getparam, 2)
1668 MIPS_SYS(sys_sched_setscheduler, 3) /* 4160 */
1669 MIPS_SYS(sys_sched_getscheduler, 1)
1670 MIPS_SYS(sys_sched_yield , 0)
1671 MIPS_SYS(sys_sched_get_priority_max, 1)
1672 MIPS_SYS(sys_sched_get_priority_min, 1)
1673 MIPS_SYS(sys_sched_rr_get_interval, 2) /* 4165 */
1674 MIPS_SYS(sys_nanosleep, 2)
1675 MIPS_SYS(sys_mremap , 4)
1676 MIPS_SYS(sys_accept , 3)
1677 MIPS_SYS(sys_bind , 3)
1678 MIPS_SYS(sys_connect , 3) /* 4170 */
1679 MIPS_SYS(sys_getpeername , 3)
1680 MIPS_SYS(sys_getsockname , 3)
1681 MIPS_SYS(sys_getsockopt , 5)
1682 MIPS_SYS(sys_listen , 2)
1683 MIPS_SYS(sys_recv , 4) /* 4175 */
1684 MIPS_SYS(sys_recvfrom , 6)
1685 MIPS_SYS(sys_recvmsg , 3)
1686 MIPS_SYS(sys_send , 4)
1687 MIPS_SYS(sys_sendmsg , 3)
1688 MIPS_SYS(sys_sendto , 6) /* 4180 */
1689 MIPS_SYS(sys_setsockopt , 5)
1690 MIPS_SYS(sys_shutdown , 2)
1691 MIPS_SYS(sys_socket , 3)
1692 MIPS_SYS(sys_socketpair , 4)
1693 MIPS_SYS(sys_setresuid , 3) /* 4185 */
1694 MIPS_SYS(sys_getresuid , 3)
1695 MIPS_SYS(sys_ni_syscall , 0) /* was sys_query_module */
1696 MIPS_SYS(sys_poll , 3)
1697 MIPS_SYS(sys_nfsservctl , 3)
1698 MIPS_SYS(sys_setresgid , 3) /* 4190 */
1699 MIPS_SYS(sys_getresgid , 3)
1700 MIPS_SYS(sys_prctl , 5)
1701 MIPS_SYS(sys_rt_sigreturn, 0)
1702 MIPS_SYS(sys_rt_sigaction, 4)
1703 MIPS_SYS(sys_rt_sigprocmask, 4) /* 4195 */
1704 MIPS_SYS(sys_rt_sigpending, 2)
1705 MIPS_SYS(sys_rt_sigtimedwait, 4)
1706 MIPS_SYS(sys_rt_sigqueueinfo, 3)
1707 MIPS_SYS(sys_rt_sigsuspend, 0)
1708 MIPS_SYS(sys_pread64 , 6) /* 4200 */
1709 MIPS_SYS(sys_pwrite64 , 6)
1710 MIPS_SYS(sys_chown , 3)
1711 MIPS_SYS(sys_getcwd , 2)
1712 MIPS_SYS(sys_capget , 2)
1713 MIPS_SYS(sys_capset , 2) /* 4205 */
1714 MIPS_SYS(sys_sigaltstack , 0)
1715 MIPS_SYS(sys_sendfile , 4)
1716 MIPS_SYS(sys_ni_syscall , 0)
1717 MIPS_SYS(sys_ni_syscall , 0)
1718 MIPS_SYS(sys_mmap2 , 6) /* 4210 */
1719 MIPS_SYS(sys_truncate64 , 4)
1720 MIPS_SYS(sys_ftruncate64 , 4)
1721 MIPS_SYS(sys_stat64 , 2)
1722 MIPS_SYS(sys_lstat64 , 2)
1723 MIPS_SYS(sys_fstat64 , 2) /* 4215 */
1724 MIPS_SYS(sys_pivot_root , 2)
1725 MIPS_SYS(sys_mincore , 3)
1726 MIPS_SYS(sys_madvise , 3)
1727 MIPS_SYS(sys_getdents64 , 3)
1728 MIPS_SYS(sys_fcntl64 , 3) /* 4220 */
1729 MIPS_SYS(sys_ni_syscall , 0)
1730 MIPS_SYS(sys_gettid , 0)
1731 MIPS_SYS(sys_readahead , 5)
1732 MIPS_SYS(sys_setxattr , 5)
1733 MIPS_SYS(sys_lsetxattr , 5) /* 4225 */
1734 MIPS_SYS(sys_fsetxattr , 5)
1735 MIPS_SYS(sys_getxattr , 4)
1736 MIPS_SYS(sys_lgetxattr , 4)
1737 MIPS_SYS(sys_fgetxattr , 4)
1738 MIPS_SYS(sys_listxattr , 3) /* 4230 */
1739 MIPS_SYS(sys_llistxattr , 3)
1740 MIPS_SYS(sys_flistxattr , 3)
1741 MIPS_SYS(sys_removexattr , 2)
1742 MIPS_SYS(sys_lremovexattr, 2)
1743 MIPS_SYS(sys_fremovexattr, 2) /* 4235 */
1744 MIPS_SYS(sys_tkill , 2)
1745 MIPS_SYS(sys_sendfile64 , 5)
1746 MIPS_SYS(sys_futex , 2)
1747 MIPS_SYS(sys_sched_setaffinity, 3)
1748 MIPS_SYS(sys_sched_getaffinity, 3) /* 4240 */
1749 MIPS_SYS(sys_io_setup , 2)
1750 MIPS_SYS(sys_io_destroy , 1)
1751 MIPS_SYS(sys_io_getevents, 5)
1752 MIPS_SYS(sys_io_submit , 3)
1753 MIPS_SYS(sys_io_cancel , 3) /* 4245 */
1754 MIPS_SYS(sys_exit_group , 1)
1755 MIPS_SYS(sys_lookup_dcookie, 3)
1756 MIPS_SYS(sys_epoll_create, 1)
1757 MIPS_SYS(sys_epoll_ctl , 4)
1758 MIPS_SYS(sys_epoll_wait , 3) /* 4250 */
1759 MIPS_SYS(sys_remap_file_pages, 5)
1760 MIPS_SYS(sys_set_tid_address, 1)
1761 MIPS_SYS(sys_restart_syscall, 0)
1762 MIPS_SYS(sys_fadvise64_64, 7)
1763 MIPS_SYS(sys_statfs64 , 3) /* 4255 */
1764 MIPS_SYS(sys_fstatfs64 , 2)
1765 MIPS_SYS(sys_timer_create, 3)
1766 MIPS_SYS(sys_timer_settime, 4)
1767 MIPS_SYS(sys_timer_gettime, 2)
1768 MIPS_SYS(sys_timer_getoverrun, 1) /* 4260 */
1769 MIPS_SYS(sys_timer_delete, 1)
1770 MIPS_SYS(sys_clock_settime, 2)
1771 MIPS_SYS(sys_clock_gettime, 2)
1772 MIPS_SYS(sys_clock_getres, 2)
1773 MIPS_SYS(sys_clock_nanosleep, 4) /* 4265 */
1774 MIPS_SYS(sys_tgkill , 3)
1775 MIPS_SYS(sys_utimes , 2)
1776 MIPS_SYS(sys_mbind , 4)
1777 MIPS_SYS(sys_ni_syscall , 0) /* sys_get_mempolicy */
1778 MIPS_SYS(sys_ni_syscall , 0) /* 4270 sys_set_mempolicy */
1779 MIPS_SYS(sys_mq_open , 4)
1780 MIPS_SYS(sys_mq_unlink , 1)
1781 MIPS_SYS(sys_mq_timedsend, 5)
1782 MIPS_SYS(sys_mq_timedreceive, 5)
1783 MIPS_SYS(sys_mq_notify , 2) /* 4275 */
1784 MIPS_SYS(sys_mq_getsetattr, 3)
1785 MIPS_SYS(sys_ni_syscall , 0) /* sys_vserver */
1786 MIPS_SYS(sys_waitid , 4)
1787 MIPS_SYS(sys_ni_syscall , 0) /* available, was setaltroot */
1788 MIPS_SYS(sys_add_key , 5)
1789 MIPS_SYS(sys_request_key, 4)
1790 MIPS_SYS(sys_keyctl , 5)
1791 MIPS_SYS(sys_set_thread_area, 1)
1792 MIPS_SYS(sys_inotify_init, 0)
1793 MIPS_SYS(sys_inotify_add_watch, 3) /* 4285 */
1794 MIPS_SYS(sys_inotify_rm_watch, 2)
1795 MIPS_SYS(sys_migrate_pages, 4)
1796 MIPS_SYS(sys_openat, 4)
1797 MIPS_SYS(sys_mkdirat, 3)
1798 MIPS_SYS(sys_mknodat, 4) /* 4290 */
1799 MIPS_SYS(sys_fchownat, 5)
1800 MIPS_SYS(sys_futimesat, 3)
1801 MIPS_SYS(sys_fstatat64, 4)
1802 MIPS_SYS(sys_unlinkat, 3)
1803 MIPS_SYS(sys_renameat, 4) /* 4295 */
1804 MIPS_SYS(sys_linkat, 5)
1805 MIPS_SYS(sys_symlinkat, 3)
1806 MIPS_SYS(sys_readlinkat, 4)
1807 MIPS_SYS(sys_fchmodat, 3)
1808 MIPS_SYS(sys_faccessat, 3) /* 4300 */
1809 MIPS_SYS(sys_pselect6, 6)
1810 MIPS_SYS(sys_ppoll, 5)
1811 MIPS_SYS(sys_unshare, 1)
1812 MIPS_SYS(sys_splice, 4)
1813 MIPS_SYS(sys_sync_file_range, 7) /* 4305 */
1814 MIPS_SYS(sys_tee, 4)
1815 MIPS_SYS(sys_vmsplice, 4)
1816 MIPS_SYS(sys_move_pages, 6)
1817 MIPS_SYS(sys_set_robust_list, 2)
1818 MIPS_SYS(sys_get_robust_list, 3) /* 4310 */
1819 MIPS_SYS(sys_kexec_load, 4)
1820 MIPS_SYS(sys_getcpu, 3)
1821 MIPS_SYS(sys_epoll_pwait, 6)
1822 MIPS_SYS(sys_ioprio_set, 3)
1823 MIPS_SYS(sys_ioprio_get, 2)
1828 void cpu_loop(CPUMIPSState *env)
1830 target_siginfo_t info;
1832 unsigned int syscall_num;
1835 trapnr = cpu_mips_exec(env);
1838 syscall_num = env->active_tc.gpr[2] - 4000;
1839 env->active_tc.PC += 4;
1840 if (syscall_num >= sizeof(mips_syscall_args)) {
1845 abi_ulong arg5 = 0, arg6 = 0, arg7 = 0, arg8 = 0;
1847 nb_args = mips_syscall_args[syscall_num];
1848 sp_reg = env->active_tc.gpr[29];
1850 /* these arguments are taken from the stack */
1851 /* FIXME - what to do if get_user() fails? */
1852 case 8: get_user_ual(arg8, sp_reg + 28);
1853 case 7: get_user_ual(arg7, sp_reg + 24);
1854 case 6: get_user_ual(arg6, sp_reg + 20);
1855 case 5: get_user_ual(arg5, sp_reg + 16);
1859 ret = do_syscall(env, env->active_tc.gpr[2],
1860 env->active_tc.gpr[4],
1861 env->active_tc.gpr[5],
1862 env->active_tc.gpr[6],
1863 env->active_tc.gpr[7],
1864 arg5, arg6/*, arg7, arg8*/);
1866 if ((unsigned int)ret >= (unsigned int)(-1133)) {
1867 env->active_tc.gpr[7] = 1; /* error flag */
1870 env->active_tc.gpr[7] = 0; /* error flag */
1872 env->active_tc.gpr[2] = ret;
1878 info.si_signo = TARGET_SIGILL;
1881 queue_signal(env, info.si_signo, &info);
1883 case EXCP_INTERRUPT:
1884 /* just indicate that signals should be handled asap */
1890 sig = gdb_handlesig (env, TARGET_SIGTRAP);
1893 info.si_signo = sig;
1895 info.si_code = TARGET_TRAP_BRKPT;
1896 queue_signal(env, info.si_signo, &info);
1902 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
1904 cpu_dump_state(env, stderr, fprintf, 0);
1907 process_pending_signals(env);
1913 void cpu_loop (CPUState *env)
1916 target_siginfo_t info;
1919 trapnr = cpu_sh4_exec (env);
1924 ret = do_syscall(env,
1932 env->gregs[0] = ret;
1934 case EXCP_INTERRUPT:
1935 /* just indicate that signals should be handled asap */
1941 sig = gdb_handlesig (env, TARGET_SIGTRAP);
1944 info.si_signo = sig;
1946 info.si_code = TARGET_TRAP_BRKPT;
1947 queue_signal(env, info.si_signo, &info);
1953 info.si_signo = SIGSEGV;
1955 info.si_code = TARGET_SEGV_MAPERR;
1956 info._sifields._sigfault._addr = env->tea;
1957 queue_signal(env, info.si_signo, &info);
1961 printf ("Unhandled trap: 0x%x\n", trapnr);
1962 cpu_dump_state(env, stderr, fprintf, 0);
1965 process_pending_signals (env);
1971 void cpu_loop (CPUState *env)
1974 target_siginfo_t info;
1977 trapnr = cpu_cris_exec (env);
1981 info.si_signo = SIGSEGV;
1983 /* XXX: check env->error_code */
1984 info.si_code = TARGET_SEGV_MAPERR;
1985 info._sifields._sigfault._addr = env->pregs[PR_EDA];
1986 queue_signal(env, info.si_signo, &info);
1989 case EXCP_INTERRUPT:
1990 /* just indicate that signals should be handled asap */
1993 ret = do_syscall(env,
2001 env->regs[10] = ret;
2007 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2010 info.si_signo = sig;
2012 info.si_code = TARGET_TRAP_BRKPT;
2013 queue_signal(env, info.si_signo, &info);
2018 printf ("Unhandled trap: 0x%x\n", trapnr);
2019 cpu_dump_state(env, stderr, fprintf, 0);
2022 process_pending_signals (env);
2029 void cpu_loop(CPUM68KState *env)
2033 target_siginfo_t info;
2034 TaskState *ts = env->opaque;
2037 trapnr = cpu_m68k_exec(env);
2041 if (ts->sim_syscalls) {
2043 nr = lduw(env->pc + 2);
2045 do_m68k_simcall(env, nr);
2051 case EXCP_HALT_INSN:
2052 /* Semihosing syscall. */
2054 do_m68k_semihosting(env, env->dregs[0]);
2058 case EXCP_UNSUPPORTED:
2060 info.si_signo = SIGILL;
2062 info.si_code = TARGET_ILL_ILLOPN;
2063 info._sifields._sigfault._addr = env->pc;
2064 queue_signal(env, info.si_signo, &info);
2068 ts->sim_syscalls = 0;
2071 env->dregs[0] = do_syscall(env,
2081 case EXCP_INTERRUPT:
2082 /* just indicate that signals should be handled asap */
2086 info.si_signo = SIGSEGV;
2088 /* XXX: check env->error_code */
2089 info.si_code = TARGET_SEGV_MAPERR;
2090 info._sifields._sigfault._addr = env->mmu.ar;
2091 queue_signal(env, info.si_signo, &info);
2098 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2101 info.si_signo = sig;
2103 info.si_code = TARGET_TRAP_BRKPT;
2104 queue_signal(env, info.si_signo, &info);
2109 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
2111 cpu_dump_state(env, stderr, fprintf, 0);
2114 process_pending_signals(env);
2117 #endif /* TARGET_M68K */
2120 void cpu_loop (CPUState *env)
2123 target_siginfo_t info;
2126 trapnr = cpu_alpha_exec (env);
2130 fprintf(stderr, "Reset requested. Exit\n");
2134 fprintf(stderr, "Machine check exception. Exit\n");
2138 fprintf(stderr, "Arithmetic trap.\n");
2141 case EXCP_HW_INTERRUPT:
2142 fprintf(stderr, "External interrupt. Exit\n");
2146 fprintf(stderr, "MMU data fault\n");
2149 case EXCP_DTB_MISS_PAL:
2150 fprintf(stderr, "MMU data TLB miss in PALcode\n");
2154 fprintf(stderr, "MMU instruction TLB miss\n");
2158 fprintf(stderr, "MMU instruction access violation\n");
2161 case EXCP_DTB_MISS_NATIVE:
2162 fprintf(stderr, "MMU data TLB miss\n");
2166 fprintf(stderr, "Unaligned access\n");
2170 fprintf(stderr, "Invalid instruction\n");
2174 fprintf(stderr, "Floating-point not allowed\n");
2177 case EXCP_CALL_PAL ... (EXCP_CALL_PALP - 1):
2178 call_pal(env, (trapnr >> 6) | 0x80);
2180 case EXCP_CALL_PALP ... (EXCP_CALL_PALE - 1):
2181 fprintf(stderr, "Privileged call to PALcode\n");
2188 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2191 info.si_signo = sig;
2193 info.si_code = TARGET_TRAP_BRKPT;
2194 queue_signal(env, info.si_signo, &info);
2199 printf ("Unhandled trap: 0x%x\n", trapnr);
2200 cpu_dump_state(env, stderr, fprintf, 0);
2203 process_pending_signals (env);
2206 #endif /* TARGET_ALPHA */
2208 static void usage(void)
2210 printf("qemu-" TARGET_ARCH " version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
2211 "usage: qemu-" TARGET_ARCH " [options] program [arguments...]\n"
2212 "Linux CPU emulator (compiled for %s emulation)\n"
2214 "Standard options:\n"
2215 "-h print this help\n"
2216 "-g port wait gdb connection to port\n"
2217 "-L path set the elf interpreter prefix (default=%s)\n"
2218 "-s size set the stack size in bytes (default=%ld)\n"
2219 "-cpu model select CPU (-cpu ? for list)\n"
2220 "-drop-ld-preload drop LD_PRELOAD for target process\n"
2221 "-E var=value sets/modifies targets environment variable(s)\n"
2222 "-U var unsets targets environment variable(s)\n"
2223 "-0 argv0 forces target process argv[0] to be argv0\n"
2224 #if defined(CONFIG_USE_GUEST_BASE)
2225 "-B address set guest_base address to address\n"
2229 "-d options activate log (logfile=%s)\n"
2230 "-p pagesize set the host page size to 'pagesize'\n"
2231 "-singlestep always run in singlestep mode\n"
2232 "-strace log system calls\n"
2234 "Environment variables:\n"
2235 "QEMU_STRACE Print system calls and arguments similar to the\n"
2236 " 'strace' program. Enable by setting to any value.\n"
2237 "You can use -E and -U options to set/unset environment variables\n"
2238 "for target process. It is possible to provide several variables\n"
2239 "by repeating the option. For example:\n"
2240 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
2241 "Note that if you provide several changes to single variable\n"
2242 "last change will stay in effect.\n"
2243 #if defined(CONFIG_USE_GUEST_BASE)
2245 "You can use -B option to load target binary into different\n"
2246 "address that is specified in elf headers. This can be useful\n"
2247 "when target binary would be loaded to low addresses and\n"
2248 "/proc/sys/vm/mmap_min_addr is set to higher. For example\n"
2249 " qemu-" TARGET_ARCH " -B 0x100000 ...\n"
2250 "loads target binary starting from the first meg.\n"
2260 THREAD CPUState *thread_env;
2262 void task_settid(TaskState *ts)
2264 if (ts->ts_tid == 0) {
2266 ts->ts_tid = (pid_t)syscall(SYS_gettid);
2268 /* when no threads are used, tid becomes pid */
2269 ts->ts_tid = getpid();
2274 void stop_all_tasks(void)
2277 * We trust that when using NPTL, start_exclusive()
2278 * handles thread stopping correctly.
2283 /* Assumes contents are already zeroed. */
2284 void init_task_state(TaskState *ts)
2289 ts->first_free = ts->sigqueue_table;
2290 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
2291 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
2293 ts->sigqueue_table[i].next = NULL;
2296 int main(int argc, char **argv, char **envp)
2298 char *filename = NULL;
2299 const char *cpu_model;
2300 struct target_pt_regs regs1, *regs = ®s1;
2301 struct image_info info1, *info = &info1;
2302 struct linux_binprm bprm;
2303 TaskState ts1, *ts = &ts1;
2307 int gdbstub_port = 0;
2308 char **target_environ, **wrk;
2311 int drop_ld_preload = 0;
2312 envlist_t *envlist = NULL;
2313 const char *argv0 = NULL;
2320 qemu_cache_utils_init(envp);
2323 cpu_set_log_filename(DEBUG_LOGFILE);
2325 if ((envlist = envlist_create()) == NULL) {
2326 (void) fprintf(stderr, "Unable to allocate envlist\n");
2330 /* add current environment into the list */
2331 for (wrk = environ; *wrk != NULL; wrk++) {
2332 (void) envlist_setenv(envlist, *wrk);
2345 if (!strcmp(r, "-")) {
2347 } else if (!strcmp(r, "d")) {
2349 const CPULogItem *item;
2355 mask = cpu_str_to_log_mask(r);
2357 printf("Log items (comma separated):\n");
2358 for(item = cpu_log_items; item->mask != 0; item++) {
2359 printf("%-10s %s\n", item->name, item->help);
2364 } else if (!strcmp(r, "E")) {
2366 if (envlist_setenv(envlist, r) != 0)
2368 } else if (!strcmp(r, "U")) {
2370 if (envlist_unsetenv(envlist, r) != 0)
2372 } else if (!strcmp(r, "0")) {
2375 } else if (!strcmp(r,"-sbox-call")) {
2377 } else if (!strcmp(r, "s")) {
2381 x86_stack_size = strtol(r, (char **)&r, 0);
2382 if (x86_stack_size <= 0)
2385 x86_stack_size *= 1024 * 1024;
2386 else if (*r == 'k' || *r == 'K')
2387 x86_stack_size *= 1024;
2388 } else if (!strcmp(r, "L")) {
2389 interp_prefix = argv[optind++];
2390 } else if (!strcmp(r, "p")) {
2393 qemu_host_page_size = atoi(argv[optind++]);
2394 if (qemu_host_page_size == 0 ||
2395 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
2396 fprintf(stderr, "page size must be a power of two\n");
2399 } else if (!strcmp(r, "g")) {
2402 gdbstub_port = atoi(argv[optind++]);
2403 } else if (!strcmp(r, "r")) {
2404 qemu_uname_release = argv[optind++];
2405 } else if (!strcmp(r, "cpu")) {
2406 cpu_model = argv[optind++];
2407 if (cpu_model == NULL || strcmp(cpu_model, "?") == 0) {
2408 /* XXX: implement xxx_cpu_list for targets that still miss it */
2409 #if defined(cpu_list)
2410 cpu_list(stdout, &fprintf);
2414 #if defined(CONFIG_USE_GUEST_BASE)
2415 } else if (!strcmp(r, "B")) {
2416 guest_base = strtol(argv[optind++], NULL, 0);
2418 } else if (!strcmp(r, "drop-ld-preload")) {
2419 drop_ld_preload = 1;
2420 } else if (!strcmp(r, "keep-ld-preload")) {
2421 drop_ld_preload = 0;
2422 } else if (!strcmp(r, "singlestep")) {
2424 } else if (!strcmp(r, "strace")) {
2433 if (filename == NULL) {
2434 filename = argv[optind];
2435 exec_path = argv[optind];
2437 argv0 = argv[optind];
2439 if (drop_ld_preload) {
2440 (void) envlist_unsetenv(envlist, "LD_PRELOAD");
2444 memset(regs, 0, sizeof(struct target_pt_regs));
2446 /* Zero out image_info */
2447 memset(info, 0, sizeof(struct image_info));
2449 memset(&bprm, 0, sizeof (bprm));
2451 /* Scan interp_prefix dir for replacement files. */
2452 init_paths(interp_prefix);
2454 if (cpu_model == NULL) {
2455 #if defined(TARGET_I386)
2456 #ifdef TARGET_X86_64
2457 cpu_model = "qemu64";
2459 cpu_model = "qemu32";
2461 #elif defined(TARGET_ARM)
2463 #elif defined(TARGET_M68K)
2465 #elif defined(TARGET_SPARC)
2466 #ifdef TARGET_SPARC64
2467 cpu_model = "TI UltraSparc II";
2469 cpu_model = "Fujitsu MB86904";
2471 #elif defined(TARGET_MIPS)
2472 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
2477 #elif defined(TARGET_PPC)
2487 cpu_exec_init_all(0);
2488 /* NOTE: we need to init the CPU at this stage to get
2489 qemu_host_page_size */
2490 env = cpu_init(cpu_model);
2492 fprintf(stderr, "Unable to find CPU definition\n");
2497 if (getenv("QEMU_STRACE")) {
2501 target_environ = envlist_to_environ(envlist, NULL);
2502 envlist_free(envlist);
2504 #if defined(CONFIG_USE_GUEST_BASE)
2506 * Now that page sizes are configured in cpu_init() we can do
2507 * proper page alignment for guest_base.
2509 guest_base = HOST_PAGE_ALIGN(guest_base);
2512 * Read in mmap_min_addr kernel parameter and check
2513 * whether it is set to some value > 0. This value is used
2514 * later on when doing mmap(2)s to calculate where guest_base
2515 * is to set, if needed.
2517 * When user has explicitly set the quest base, we skip this
2520 if (guest_base == 0) {
2523 if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
2525 if (fscanf(fp, "%lu", &tmp) == 1) {
2526 mmap_min_addr = tmp;
2527 qemu_log("kernel mmap_min_addr=%lu\n", mmap_min_addr);
2532 #endif /* CONFIG_USE_GUEST_BASE */
2535 * Prepare copy of argv vector for target.
2537 target_argc = argc - optind;
2538 target_argv = calloc(target_argc + 1, sizeof (char *));
2539 if (target_argv == NULL) {
2540 (void) fprintf(stderr, "Unable to allocate memory for target_argv\n");
2545 * If argv0 is specified (using '-0' switch) we replace
2546 * argv[0] pointer with the given one.
2549 if (argv0 != NULL) {
2550 target_argv[i++] = strdup(argv0);
2552 for (; i < target_argc; i++) {
2553 target_argv[i] = strdup(argv[optind + i]);
2555 target_argv[target_argc] = NULL;
2557 memset(ts, 0, sizeof(TaskState));
2558 init_task_state(ts);
2559 /* build Task State */
2565 if (loader_exec(filename, target_argv+argskip, target_environ, regs,
2566 info, &bprm) != 0) {
2567 printf("Error loading %s\n", filename);
2571 for (i = 0; i < target_argc; i++) {
2572 free(target_argv[i]);
2576 for (wrk = target_environ; *wrk; wrk++) {
2580 free(target_environ);
2582 if (qemu_log_enabled()) {
2583 #if defined(CONFIG_USE_GUEST_BASE)
2584 if (guest_base > 0) {
2585 qemu_log("guest_base is set to 0x%lx\n", guest_base);
2587 "==========================================================\n"
2588 "Note that all target addresses below are given in target\n"
2589 "address space which is different from host by guest_base.\n"
2590 "For example: target address 0x%lx becomes 0x%lx and so on.\n"
2591 "==========================================================\n",
2592 (uintptr_t)0x8000, (uintptr_t)g2h(0x8000));
2597 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
2598 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
2599 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
2601 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
2603 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
2604 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
2606 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
2607 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
2610 target_set_brk(info->brk);
2614 #if defined(TARGET_I386)
2615 cpu_x86_set_cpl(env, 3);
2617 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
2618 env->hflags |= HF_PE_MASK;
2619 if (env->cpuid_features & CPUID_SSE) {
2620 env->cr[4] |= CR4_OSFXSR_MASK;
2621 env->hflags |= HF_OSFXSR_MASK;
2623 #ifndef TARGET_ABI32
2624 /* enable 64 bit mode if possible */
2625 if (!(env->cpuid_ext2_features & CPUID_EXT2_LM)) {
2626 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
2629 env->cr[4] |= CR4_PAE_MASK;
2630 env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
2631 env->hflags |= HF_LMA_MASK;
2634 /* flags setup : we activate the IRQs by default as in user mode */
2635 env->eflags |= IF_MASK;
2637 /* linux register setup */
2638 #ifndef TARGET_ABI32
2639 env->regs[R_EAX] = regs->rax;
2640 env->regs[R_EBX] = regs->rbx;
2641 env->regs[R_ECX] = regs->rcx;
2642 env->regs[R_EDX] = regs->rdx;
2643 env->regs[R_ESI] = regs->rsi;
2644 env->regs[R_EDI] = regs->rdi;
2645 env->regs[R_EBP] = regs->rbp;
2646 env->regs[R_ESP] = regs->rsp;
2647 env->eip = regs->rip;
2649 env->regs[R_EAX] = regs->eax;
2650 env->regs[R_EBX] = regs->ebx;
2651 env->regs[R_ECX] = regs->ecx;
2652 env->regs[R_EDX] = regs->edx;
2653 env->regs[R_ESI] = regs->esi;
2654 env->regs[R_EDI] = regs->edi;
2655 env->regs[R_EBP] = regs->ebp;
2656 env->regs[R_ESP] = regs->esp;
2657 env->eip = regs->eip;
2660 /* linux interrupt setup */
2661 #ifndef TARGET_ABI32
2662 env->idt.limit = 511;
2664 env->idt.limit = 255;
2666 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
2667 PROT_READ|PROT_WRITE,
2668 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
2669 idt_table = g2h(env->idt.base);
2692 /* linux segment setup */
2694 uint64_t *gdt_table;
2695 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
2696 PROT_READ|PROT_WRITE,
2697 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
2698 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
2699 gdt_table = g2h(env->gdt.base);
2701 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
2702 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
2703 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
2705 /* 64 bit code segment */
2706 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
2707 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
2709 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
2711 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
2712 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
2713 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
2715 cpu_x86_load_seg(env, R_CS, __USER_CS);
2716 cpu_x86_load_seg(env, R_SS, __USER_DS);
2718 cpu_x86_load_seg(env, R_DS, __USER_DS);
2719 cpu_x86_load_seg(env, R_ES, __USER_DS);
2720 cpu_x86_load_seg(env, R_FS, __USER_DS);
2721 cpu_x86_load_seg(env, R_GS, __USER_DS);
2722 /* This hack makes Wine work... */
2723 env->segs[R_FS].selector = 0;
2725 cpu_x86_load_seg(env, R_DS, 0);
2726 cpu_x86_load_seg(env, R_ES, 0);
2727 cpu_x86_load_seg(env, R_FS, 0);
2728 cpu_x86_load_seg(env, R_GS, 0);
2730 #elif defined(TARGET_ARM)
2733 cpsr_write(env, regs->uregs[16], 0xffffffff);
2734 for(i = 0; i < 16; i++) {
2735 env->regs[i] = regs->uregs[i];
2738 #elif defined(TARGET_SPARC)
2742 env->npc = regs->npc;
2744 for(i = 0; i < 8; i++)
2745 env->gregs[i] = regs->u_regs[i];
2746 for(i = 0; i < 8; i++)
2747 env->regwptr[i] = regs->u_regs[i + 8];
2749 #elif defined(TARGET_PPC)
2753 #if defined(TARGET_PPC64)
2754 #if defined(TARGET_ABI32)
2755 env->msr &= ~((target_ulong)1 << MSR_SF);
2757 env->msr |= (target_ulong)1 << MSR_SF;
2760 env->nip = regs->nip;
2761 for(i = 0; i < 32; i++) {
2762 env->gpr[i] = regs->gpr[i];
2765 #elif defined(TARGET_M68K)
2768 env->dregs[0] = regs->d0;
2769 env->dregs[1] = regs->d1;
2770 env->dregs[2] = regs->d2;
2771 env->dregs[3] = regs->d3;
2772 env->dregs[4] = regs->d4;
2773 env->dregs[5] = regs->d5;
2774 env->dregs[6] = regs->d6;
2775 env->dregs[7] = regs->d7;
2776 env->aregs[0] = regs->a0;
2777 env->aregs[1] = regs->a1;
2778 env->aregs[2] = regs->a2;
2779 env->aregs[3] = regs->a3;
2780 env->aregs[4] = regs->a4;
2781 env->aregs[5] = regs->a5;
2782 env->aregs[6] = regs->a6;
2783 env->aregs[7] = regs->usp;
2785 ts->sim_syscalls = 1;
2787 #elif defined(TARGET_MIPS)
2791 for(i = 0; i < 32; i++) {
2792 env->active_tc.gpr[i] = regs->regs[i];
2794 env->active_tc.PC = regs->cp0_epc;
2796 #elif defined(TARGET_SH4)
2800 for(i = 0; i < 16; i++) {
2801 env->gregs[i] = regs->regs[i];
2805 #elif defined(TARGET_ALPHA)
2809 for(i = 0; i < 28; i++) {
2810 env->ir[i] = ((abi_ulong *)regs)[i];
2812 env->ipr[IPR_USP] = regs->usp;
2813 env->ir[30] = regs->usp;
2815 env->unique = regs->unique;
2817 #elif defined(TARGET_CRIS)
2819 env->regs[0] = regs->r0;
2820 env->regs[1] = regs->r1;
2821 env->regs[2] = regs->r2;
2822 env->regs[3] = regs->r3;
2823 env->regs[4] = regs->r4;
2824 env->regs[5] = regs->r5;
2825 env->regs[6] = regs->r6;
2826 env->regs[7] = regs->r7;
2827 env->regs[8] = regs->r8;
2828 env->regs[9] = regs->r9;
2829 env->regs[10] = regs->r10;
2830 env->regs[11] = regs->r11;
2831 env->regs[12] = regs->r12;
2832 env->regs[13] = regs->r13;
2833 env->regs[14] = info->start_stack;
2834 env->regs[15] = regs->acr;
2835 env->pc = regs->erp;
2838 #error unsupported target CPU
2841 #if defined(TARGET_ARM) || defined(TARGET_M68K)
2842 ts->stack_base = info->start_stack;
2843 ts->heap_base = info->brk;
2844 /* This will be filled in on the first SYS_HEAPINFO call. */
2849 gdbserver_start (gdbstub_port);
2850 gdb_handlesig(env, 0);