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, ...) \
1090 fprintf(stderr, fmt , ## __VA_ARGS__); \
1091 cpu_dump_state(env, stderr, fprintf, 0); \
1092 qemu_log(fmt, ## __VA_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)(-TARGET_QEMU_ESIGRETURN)) {
1470 /* Returning from a successful sigreturn syscall.
1471 Avoid corrupting register state. */
1474 if (ret > (uint32_t)(-515)) {
1480 printf("syscall returned 0x%08x (%d)\n", ret, ret);
1487 sig = gdb_handlesig(env, TARGET_SIGTRAP);
1489 info.si_signo = sig;
1491 info.si_code = TARGET_TRAP_BRKPT;
1492 queue_signal(env, info.si_signo, &info);
1496 case EXCP_INTERRUPT:
1497 /* just indicate that signals should be handled asap */
1500 cpu_abort(env, "Unknown exception 0x%d. Aborting\n", trapnr);
1503 process_pending_signals(env);
1510 #define MIPS_SYS(name, args) args,
1512 static const uint8_t mips_syscall_args[] = {
1513 MIPS_SYS(sys_syscall , 0) /* 4000 */
1514 MIPS_SYS(sys_exit , 1)
1515 MIPS_SYS(sys_fork , 0)
1516 MIPS_SYS(sys_read , 3)
1517 MIPS_SYS(sys_write , 3)
1518 MIPS_SYS(sys_open , 3) /* 4005 */
1519 MIPS_SYS(sys_close , 1)
1520 MIPS_SYS(sys_waitpid , 3)
1521 MIPS_SYS(sys_creat , 2)
1522 MIPS_SYS(sys_link , 2)
1523 MIPS_SYS(sys_unlink , 1) /* 4010 */
1524 MIPS_SYS(sys_execve , 0)
1525 MIPS_SYS(sys_chdir , 1)
1526 MIPS_SYS(sys_time , 1)
1527 MIPS_SYS(sys_mknod , 3)
1528 MIPS_SYS(sys_chmod , 2) /* 4015 */
1529 MIPS_SYS(sys_lchown , 3)
1530 MIPS_SYS(sys_ni_syscall , 0)
1531 MIPS_SYS(sys_ni_syscall , 0) /* was sys_stat */
1532 MIPS_SYS(sys_lseek , 3)
1533 MIPS_SYS(sys_getpid , 0) /* 4020 */
1534 MIPS_SYS(sys_mount , 5)
1535 MIPS_SYS(sys_oldumount , 1)
1536 MIPS_SYS(sys_setuid , 1)
1537 MIPS_SYS(sys_getuid , 0)
1538 MIPS_SYS(sys_stime , 1) /* 4025 */
1539 MIPS_SYS(sys_ptrace , 4)
1540 MIPS_SYS(sys_alarm , 1)
1541 MIPS_SYS(sys_ni_syscall , 0) /* was sys_fstat */
1542 MIPS_SYS(sys_pause , 0)
1543 MIPS_SYS(sys_utime , 2) /* 4030 */
1544 MIPS_SYS(sys_ni_syscall , 0)
1545 MIPS_SYS(sys_ni_syscall , 0)
1546 MIPS_SYS(sys_access , 2)
1547 MIPS_SYS(sys_nice , 1)
1548 MIPS_SYS(sys_ni_syscall , 0) /* 4035 */
1549 MIPS_SYS(sys_sync , 0)
1550 MIPS_SYS(sys_kill , 2)
1551 MIPS_SYS(sys_rename , 2)
1552 MIPS_SYS(sys_mkdir , 2)
1553 MIPS_SYS(sys_rmdir , 1) /* 4040 */
1554 MIPS_SYS(sys_dup , 1)
1555 MIPS_SYS(sys_pipe , 0)
1556 MIPS_SYS(sys_times , 1)
1557 MIPS_SYS(sys_ni_syscall , 0)
1558 MIPS_SYS(sys_brk , 1) /* 4045 */
1559 MIPS_SYS(sys_setgid , 1)
1560 MIPS_SYS(sys_getgid , 0)
1561 MIPS_SYS(sys_ni_syscall , 0) /* was signal(2) */
1562 MIPS_SYS(sys_geteuid , 0)
1563 MIPS_SYS(sys_getegid , 0) /* 4050 */
1564 MIPS_SYS(sys_acct , 0)
1565 MIPS_SYS(sys_umount , 2)
1566 MIPS_SYS(sys_ni_syscall , 0)
1567 MIPS_SYS(sys_ioctl , 3)
1568 MIPS_SYS(sys_fcntl , 3) /* 4055 */
1569 MIPS_SYS(sys_ni_syscall , 2)
1570 MIPS_SYS(sys_setpgid , 2)
1571 MIPS_SYS(sys_ni_syscall , 0)
1572 MIPS_SYS(sys_olduname , 1)
1573 MIPS_SYS(sys_umask , 1) /* 4060 */
1574 MIPS_SYS(sys_chroot , 1)
1575 MIPS_SYS(sys_ustat , 2)
1576 MIPS_SYS(sys_dup2 , 2)
1577 MIPS_SYS(sys_getppid , 0)
1578 MIPS_SYS(sys_getpgrp , 0) /* 4065 */
1579 MIPS_SYS(sys_setsid , 0)
1580 MIPS_SYS(sys_sigaction , 3)
1581 MIPS_SYS(sys_sgetmask , 0)
1582 MIPS_SYS(sys_ssetmask , 1)
1583 MIPS_SYS(sys_setreuid , 2) /* 4070 */
1584 MIPS_SYS(sys_setregid , 2)
1585 MIPS_SYS(sys_sigsuspend , 0)
1586 MIPS_SYS(sys_sigpending , 1)
1587 MIPS_SYS(sys_sethostname , 2)
1588 MIPS_SYS(sys_setrlimit , 2) /* 4075 */
1589 MIPS_SYS(sys_getrlimit , 2)
1590 MIPS_SYS(sys_getrusage , 2)
1591 MIPS_SYS(sys_gettimeofday, 2)
1592 MIPS_SYS(sys_settimeofday, 2)
1593 MIPS_SYS(sys_getgroups , 2) /* 4080 */
1594 MIPS_SYS(sys_setgroups , 2)
1595 MIPS_SYS(sys_ni_syscall , 0) /* old_select */
1596 MIPS_SYS(sys_symlink , 2)
1597 MIPS_SYS(sys_ni_syscall , 0) /* was sys_lstat */
1598 MIPS_SYS(sys_readlink , 3) /* 4085 */
1599 MIPS_SYS(sys_uselib , 1)
1600 MIPS_SYS(sys_swapon , 2)
1601 MIPS_SYS(sys_reboot , 3)
1602 MIPS_SYS(old_readdir , 3)
1603 MIPS_SYS(old_mmap , 6) /* 4090 */
1604 MIPS_SYS(sys_munmap , 2)
1605 MIPS_SYS(sys_truncate , 2)
1606 MIPS_SYS(sys_ftruncate , 2)
1607 MIPS_SYS(sys_fchmod , 2)
1608 MIPS_SYS(sys_fchown , 3) /* 4095 */
1609 MIPS_SYS(sys_getpriority , 2)
1610 MIPS_SYS(sys_setpriority , 3)
1611 MIPS_SYS(sys_ni_syscall , 0)
1612 MIPS_SYS(sys_statfs , 2)
1613 MIPS_SYS(sys_fstatfs , 2) /* 4100 */
1614 MIPS_SYS(sys_ni_syscall , 0) /* was ioperm(2) */
1615 MIPS_SYS(sys_socketcall , 2)
1616 MIPS_SYS(sys_syslog , 3)
1617 MIPS_SYS(sys_setitimer , 3)
1618 MIPS_SYS(sys_getitimer , 2) /* 4105 */
1619 MIPS_SYS(sys_newstat , 2)
1620 MIPS_SYS(sys_newlstat , 2)
1621 MIPS_SYS(sys_newfstat , 2)
1622 MIPS_SYS(sys_uname , 1)
1623 MIPS_SYS(sys_ni_syscall , 0) /* 4110 was iopl(2) */
1624 MIPS_SYS(sys_vhangup , 0)
1625 MIPS_SYS(sys_ni_syscall , 0) /* was sys_idle() */
1626 MIPS_SYS(sys_ni_syscall , 0) /* was sys_vm86 */
1627 MIPS_SYS(sys_wait4 , 4)
1628 MIPS_SYS(sys_swapoff , 1) /* 4115 */
1629 MIPS_SYS(sys_sysinfo , 1)
1630 MIPS_SYS(sys_ipc , 6)
1631 MIPS_SYS(sys_fsync , 1)
1632 MIPS_SYS(sys_sigreturn , 0)
1633 MIPS_SYS(sys_clone , 0) /* 4120 */
1634 MIPS_SYS(sys_setdomainname, 2)
1635 MIPS_SYS(sys_newuname , 1)
1636 MIPS_SYS(sys_ni_syscall , 0) /* sys_modify_ldt */
1637 MIPS_SYS(sys_adjtimex , 1)
1638 MIPS_SYS(sys_mprotect , 3) /* 4125 */
1639 MIPS_SYS(sys_sigprocmask , 3)
1640 MIPS_SYS(sys_ni_syscall , 0) /* was create_module */
1641 MIPS_SYS(sys_init_module , 5)
1642 MIPS_SYS(sys_delete_module, 1)
1643 MIPS_SYS(sys_ni_syscall , 0) /* 4130 was get_kernel_syms */
1644 MIPS_SYS(sys_quotactl , 0)
1645 MIPS_SYS(sys_getpgid , 1)
1646 MIPS_SYS(sys_fchdir , 1)
1647 MIPS_SYS(sys_bdflush , 2)
1648 MIPS_SYS(sys_sysfs , 3) /* 4135 */
1649 MIPS_SYS(sys_personality , 1)
1650 MIPS_SYS(sys_ni_syscall , 0) /* for afs_syscall */
1651 MIPS_SYS(sys_setfsuid , 1)
1652 MIPS_SYS(sys_setfsgid , 1)
1653 MIPS_SYS(sys_llseek , 5) /* 4140 */
1654 MIPS_SYS(sys_getdents , 3)
1655 MIPS_SYS(sys_select , 5)
1656 MIPS_SYS(sys_flock , 2)
1657 MIPS_SYS(sys_msync , 3)
1658 MIPS_SYS(sys_readv , 3) /* 4145 */
1659 MIPS_SYS(sys_writev , 3)
1660 MIPS_SYS(sys_cacheflush , 3)
1661 MIPS_SYS(sys_cachectl , 3)
1662 MIPS_SYS(sys_sysmips , 4)
1663 MIPS_SYS(sys_ni_syscall , 0) /* 4150 */
1664 MIPS_SYS(sys_getsid , 1)
1665 MIPS_SYS(sys_fdatasync , 0)
1666 MIPS_SYS(sys_sysctl , 1)
1667 MIPS_SYS(sys_mlock , 2)
1668 MIPS_SYS(sys_munlock , 2) /* 4155 */
1669 MIPS_SYS(sys_mlockall , 1)
1670 MIPS_SYS(sys_munlockall , 0)
1671 MIPS_SYS(sys_sched_setparam, 2)
1672 MIPS_SYS(sys_sched_getparam, 2)
1673 MIPS_SYS(sys_sched_setscheduler, 3) /* 4160 */
1674 MIPS_SYS(sys_sched_getscheduler, 1)
1675 MIPS_SYS(sys_sched_yield , 0)
1676 MIPS_SYS(sys_sched_get_priority_max, 1)
1677 MIPS_SYS(sys_sched_get_priority_min, 1)
1678 MIPS_SYS(sys_sched_rr_get_interval, 2) /* 4165 */
1679 MIPS_SYS(sys_nanosleep, 2)
1680 MIPS_SYS(sys_mremap , 4)
1681 MIPS_SYS(sys_accept , 3)
1682 MIPS_SYS(sys_bind , 3)
1683 MIPS_SYS(sys_connect , 3) /* 4170 */
1684 MIPS_SYS(sys_getpeername , 3)
1685 MIPS_SYS(sys_getsockname , 3)
1686 MIPS_SYS(sys_getsockopt , 5)
1687 MIPS_SYS(sys_listen , 2)
1688 MIPS_SYS(sys_recv , 4) /* 4175 */
1689 MIPS_SYS(sys_recvfrom , 6)
1690 MIPS_SYS(sys_recvmsg , 3)
1691 MIPS_SYS(sys_send , 4)
1692 MIPS_SYS(sys_sendmsg , 3)
1693 MIPS_SYS(sys_sendto , 6) /* 4180 */
1694 MIPS_SYS(sys_setsockopt , 5)
1695 MIPS_SYS(sys_shutdown , 2)
1696 MIPS_SYS(sys_socket , 3)
1697 MIPS_SYS(sys_socketpair , 4)
1698 MIPS_SYS(sys_setresuid , 3) /* 4185 */
1699 MIPS_SYS(sys_getresuid , 3)
1700 MIPS_SYS(sys_ni_syscall , 0) /* was sys_query_module */
1701 MIPS_SYS(sys_poll , 3)
1702 MIPS_SYS(sys_nfsservctl , 3)
1703 MIPS_SYS(sys_setresgid , 3) /* 4190 */
1704 MIPS_SYS(sys_getresgid , 3)
1705 MIPS_SYS(sys_prctl , 5)
1706 MIPS_SYS(sys_rt_sigreturn, 0)
1707 MIPS_SYS(sys_rt_sigaction, 4)
1708 MIPS_SYS(sys_rt_sigprocmask, 4) /* 4195 */
1709 MIPS_SYS(sys_rt_sigpending, 2)
1710 MIPS_SYS(sys_rt_sigtimedwait, 4)
1711 MIPS_SYS(sys_rt_sigqueueinfo, 3)
1712 MIPS_SYS(sys_rt_sigsuspend, 0)
1713 MIPS_SYS(sys_pread64 , 6) /* 4200 */
1714 MIPS_SYS(sys_pwrite64 , 6)
1715 MIPS_SYS(sys_chown , 3)
1716 MIPS_SYS(sys_getcwd , 2)
1717 MIPS_SYS(sys_capget , 2)
1718 MIPS_SYS(sys_capset , 2) /* 4205 */
1719 MIPS_SYS(sys_sigaltstack , 0)
1720 MIPS_SYS(sys_sendfile , 4)
1721 MIPS_SYS(sys_ni_syscall , 0)
1722 MIPS_SYS(sys_ni_syscall , 0)
1723 MIPS_SYS(sys_mmap2 , 6) /* 4210 */
1724 MIPS_SYS(sys_truncate64 , 4)
1725 MIPS_SYS(sys_ftruncate64 , 4)
1726 MIPS_SYS(sys_stat64 , 2)
1727 MIPS_SYS(sys_lstat64 , 2)
1728 MIPS_SYS(sys_fstat64 , 2) /* 4215 */
1729 MIPS_SYS(sys_pivot_root , 2)
1730 MIPS_SYS(sys_mincore , 3)
1731 MIPS_SYS(sys_madvise , 3)
1732 MIPS_SYS(sys_getdents64 , 3)
1733 MIPS_SYS(sys_fcntl64 , 3) /* 4220 */
1734 MIPS_SYS(sys_ni_syscall , 0)
1735 MIPS_SYS(sys_gettid , 0)
1736 MIPS_SYS(sys_readahead , 5)
1737 MIPS_SYS(sys_setxattr , 5)
1738 MIPS_SYS(sys_lsetxattr , 5) /* 4225 */
1739 MIPS_SYS(sys_fsetxattr , 5)
1740 MIPS_SYS(sys_getxattr , 4)
1741 MIPS_SYS(sys_lgetxattr , 4)
1742 MIPS_SYS(sys_fgetxattr , 4)
1743 MIPS_SYS(sys_listxattr , 3) /* 4230 */
1744 MIPS_SYS(sys_llistxattr , 3)
1745 MIPS_SYS(sys_flistxattr , 3)
1746 MIPS_SYS(sys_removexattr , 2)
1747 MIPS_SYS(sys_lremovexattr, 2)
1748 MIPS_SYS(sys_fremovexattr, 2) /* 4235 */
1749 MIPS_SYS(sys_tkill , 2)
1750 MIPS_SYS(sys_sendfile64 , 5)
1751 MIPS_SYS(sys_futex , 2)
1752 MIPS_SYS(sys_sched_setaffinity, 3)
1753 MIPS_SYS(sys_sched_getaffinity, 3) /* 4240 */
1754 MIPS_SYS(sys_io_setup , 2)
1755 MIPS_SYS(sys_io_destroy , 1)
1756 MIPS_SYS(sys_io_getevents, 5)
1757 MIPS_SYS(sys_io_submit , 3)
1758 MIPS_SYS(sys_io_cancel , 3) /* 4245 */
1759 MIPS_SYS(sys_exit_group , 1)
1760 MIPS_SYS(sys_lookup_dcookie, 3)
1761 MIPS_SYS(sys_epoll_create, 1)
1762 MIPS_SYS(sys_epoll_ctl , 4)
1763 MIPS_SYS(sys_epoll_wait , 3) /* 4250 */
1764 MIPS_SYS(sys_remap_file_pages, 5)
1765 MIPS_SYS(sys_set_tid_address, 1)
1766 MIPS_SYS(sys_restart_syscall, 0)
1767 MIPS_SYS(sys_fadvise64_64, 7)
1768 MIPS_SYS(sys_statfs64 , 3) /* 4255 */
1769 MIPS_SYS(sys_fstatfs64 , 2)
1770 MIPS_SYS(sys_timer_create, 3)
1771 MIPS_SYS(sys_timer_settime, 4)
1772 MIPS_SYS(sys_timer_gettime, 2)
1773 MIPS_SYS(sys_timer_getoverrun, 1) /* 4260 */
1774 MIPS_SYS(sys_timer_delete, 1)
1775 MIPS_SYS(sys_clock_settime, 2)
1776 MIPS_SYS(sys_clock_gettime, 2)
1777 MIPS_SYS(sys_clock_getres, 2)
1778 MIPS_SYS(sys_clock_nanosleep, 4) /* 4265 */
1779 MIPS_SYS(sys_tgkill , 3)
1780 MIPS_SYS(sys_utimes , 2)
1781 MIPS_SYS(sys_mbind , 4)
1782 MIPS_SYS(sys_ni_syscall , 0) /* sys_get_mempolicy */
1783 MIPS_SYS(sys_ni_syscall , 0) /* 4270 sys_set_mempolicy */
1784 MIPS_SYS(sys_mq_open , 4)
1785 MIPS_SYS(sys_mq_unlink , 1)
1786 MIPS_SYS(sys_mq_timedsend, 5)
1787 MIPS_SYS(sys_mq_timedreceive, 5)
1788 MIPS_SYS(sys_mq_notify , 2) /* 4275 */
1789 MIPS_SYS(sys_mq_getsetattr, 3)
1790 MIPS_SYS(sys_ni_syscall , 0) /* sys_vserver */
1791 MIPS_SYS(sys_waitid , 4)
1792 MIPS_SYS(sys_ni_syscall , 0) /* available, was setaltroot */
1793 MIPS_SYS(sys_add_key , 5)
1794 MIPS_SYS(sys_request_key, 4)
1795 MIPS_SYS(sys_keyctl , 5)
1796 MIPS_SYS(sys_set_thread_area, 1)
1797 MIPS_SYS(sys_inotify_init, 0)
1798 MIPS_SYS(sys_inotify_add_watch, 3) /* 4285 */
1799 MIPS_SYS(sys_inotify_rm_watch, 2)
1800 MIPS_SYS(sys_migrate_pages, 4)
1801 MIPS_SYS(sys_openat, 4)
1802 MIPS_SYS(sys_mkdirat, 3)
1803 MIPS_SYS(sys_mknodat, 4) /* 4290 */
1804 MIPS_SYS(sys_fchownat, 5)
1805 MIPS_SYS(sys_futimesat, 3)
1806 MIPS_SYS(sys_fstatat64, 4)
1807 MIPS_SYS(sys_unlinkat, 3)
1808 MIPS_SYS(sys_renameat, 4) /* 4295 */
1809 MIPS_SYS(sys_linkat, 5)
1810 MIPS_SYS(sys_symlinkat, 3)
1811 MIPS_SYS(sys_readlinkat, 4)
1812 MIPS_SYS(sys_fchmodat, 3)
1813 MIPS_SYS(sys_faccessat, 3) /* 4300 */
1814 MIPS_SYS(sys_pselect6, 6)
1815 MIPS_SYS(sys_ppoll, 5)
1816 MIPS_SYS(sys_unshare, 1)
1817 MIPS_SYS(sys_splice, 4)
1818 MIPS_SYS(sys_sync_file_range, 7) /* 4305 */
1819 MIPS_SYS(sys_tee, 4)
1820 MIPS_SYS(sys_vmsplice, 4)
1821 MIPS_SYS(sys_move_pages, 6)
1822 MIPS_SYS(sys_set_robust_list, 2)
1823 MIPS_SYS(sys_get_robust_list, 3) /* 4310 */
1824 MIPS_SYS(sys_kexec_load, 4)
1825 MIPS_SYS(sys_getcpu, 3)
1826 MIPS_SYS(sys_epoll_pwait, 6)
1827 MIPS_SYS(sys_ioprio_set, 3)
1828 MIPS_SYS(sys_ioprio_get, 2)
1833 void cpu_loop(CPUMIPSState *env)
1835 target_siginfo_t info;
1837 unsigned int syscall_num;
1840 trapnr = cpu_mips_exec(env);
1843 syscall_num = env->active_tc.gpr[2] - 4000;
1844 env->active_tc.PC += 4;
1845 if (syscall_num >= sizeof(mips_syscall_args)) {
1850 abi_ulong arg5 = 0, arg6 = 0, arg7 = 0, arg8 = 0;
1852 nb_args = mips_syscall_args[syscall_num];
1853 sp_reg = env->active_tc.gpr[29];
1855 /* these arguments are taken from the stack */
1856 /* FIXME - what to do if get_user() fails? */
1857 case 8: get_user_ual(arg8, sp_reg + 28);
1858 case 7: get_user_ual(arg7, sp_reg + 24);
1859 case 6: get_user_ual(arg6, sp_reg + 20);
1860 case 5: get_user_ual(arg5, sp_reg + 16);
1864 ret = do_syscall(env, env->active_tc.gpr[2],
1865 env->active_tc.gpr[4],
1866 env->active_tc.gpr[5],
1867 env->active_tc.gpr[6],
1868 env->active_tc.gpr[7],
1869 arg5, arg6/*, arg7, arg8*/);
1871 if (ret == -TARGET_QEMU_ESIGRETURN) {
1872 /* Returning from a successful sigreturn syscall.
1873 Avoid clobbering register state. */
1876 if ((unsigned int)ret >= (unsigned int)(-1133)) {
1877 env->active_tc.gpr[7] = 1; /* error flag */
1880 env->active_tc.gpr[7] = 0; /* error flag */
1882 env->active_tc.gpr[2] = ret;
1886 info.si_signo = TARGET_SIGSEGV;
1888 /* XXX: check env->error_code */
1889 info.si_code = TARGET_SEGV_MAPERR;
1890 info._sifields._sigfault._addr = env->CP0_BadVAddr;
1891 queue_signal(env, info.si_signo, &info);
1895 info.si_signo = TARGET_SIGILL;
1898 queue_signal(env, info.si_signo, &info);
1900 case EXCP_INTERRUPT:
1901 /* just indicate that signals should be handled asap */
1907 sig = gdb_handlesig (env, TARGET_SIGTRAP);
1910 info.si_signo = sig;
1912 info.si_code = TARGET_TRAP_BRKPT;
1913 queue_signal(env, info.si_signo, &info);
1919 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
1921 cpu_dump_state(env, stderr, fprintf, 0);
1924 process_pending_signals(env);
1930 void cpu_loop (CPUState *env)
1933 target_siginfo_t info;
1936 trapnr = cpu_sh4_exec (env);
1941 ret = do_syscall(env,
1949 env->gregs[0] = ret;
1951 case EXCP_INTERRUPT:
1952 /* just indicate that signals should be handled asap */
1958 sig = gdb_handlesig (env, TARGET_SIGTRAP);
1961 info.si_signo = sig;
1963 info.si_code = TARGET_TRAP_BRKPT;
1964 queue_signal(env, info.si_signo, &info);
1970 info.si_signo = SIGSEGV;
1972 info.si_code = TARGET_SEGV_MAPERR;
1973 info._sifields._sigfault._addr = env->tea;
1974 queue_signal(env, info.si_signo, &info);
1978 printf ("Unhandled trap: 0x%x\n", trapnr);
1979 cpu_dump_state(env, stderr, fprintf, 0);
1982 process_pending_signals (env);
1988 void cpu_loop (CPUState *env)
1991 target_siginfo_t info;
1994 trapnr = cpu_cris_exec (env);
1998 info.si_signo = SIGSEGV;
2000 /* XXX: check env->error_code */
2001 info.si_code = TARGET_SEGV_MAPERR;
2002 info._sifields._sigfault._addr = env->pregs[PR_EDA];
2003 queue_signal(env, info.si_signo, &info);
2006 case EXCP_INTERRUPT:
2007 /* just indicate that signals should be handled asap */
2010 ret = do_syscall(env,
2018 env->regs[10] = ret;
2024 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2027 info.si_signo = sig;
2029 info.si_code = TARGET_TRAP_BRKPT;
2030 queue_signal(env, info.si_signo, &info);
2035 printf ("Unhandled trap: 0x%x\n", trapnr);
2036 cpu_dump_state(env, stderr, fprintf, 0);
2039 process_pending_signals (env);
2044 #ifdef TARGET_MICROBLAZE
2045 void cpu_loop (CPUState *env)
2048 target_siginfo_t info;
2051 trapnr = cpu_mb_exec (env);
2055 info.si_signo = SIGSEGV;
2057 /* XXX: check env->error_code */
2058 info.si_code = TARGET_SEGV_MAPERR;
2059 info._sifields._sigfault._addr = 0;
2060 queue_signal(env, info.si_signo, &info);
2063 case EXCP_INTERRUPT:
2064 /* just indicate that signals should be handled asap */
2067 /* Return address is 4 bytes after the call. */
2069 ret = do_syscall(env,
2078 env->sregs[SR_PC] = env->regs[14];
2084 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2087 info.si_signo = sig;
2089 info.si_code = TARGET_TRAP_BRKPT;
2090 queue_signal(env, info.si_signo, &info);
2095 printf ("Unhandled trap: 0x%x\n", trapnr);
2096 cpu_dump_state(env, stderr, fprintf, 0);
2099 process_pending_signals (env);
2106 void cpu_loop(CPUM68KState *env)
2110 target_siginfo_t info;
2111 TaskState *ts = env->opaque;
2114 trapnr = cpu_m68k_exec(env);
2118 if (ts->sim_syscalls) {
2120 nr = lduw(env->pc + 2);
2122 do_m68k_simcall(env, nr);
2128 case EXCP_HALT_INSN:
2129 /* Semihosing syscall. */
2131 do_m68k_semihosting(env, env->dregs[0]);
2135 case EXCP_UNSUPPORTED:
2137 info.si_signo = SIGILL;
2139 info.si_code = TARGET_ILL_ILLOPN;
2140 info._sifields._sigfault._addr = env->pc;
2141 queue_signal(env, info.si_signo, &info);
2145 ts->sim_syscalls = 0;
2148 env->dregs[0] = do_syscall(env,
2158 case EXCP_INTERRUPT:
2159 /* just indicate that signals should be handled asap */
2163 info.si_signo = SIGSEGV;
2165 /* XXX: check env->error_code */
2166 info.si_code = TARGET_SEGV_MAPERR;
2167 info._sifields._sigfault._addr = env->mmu.ar;
2168 queue_signal(env, info.si_signo, &info);
2175 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2178 info.si_signo = sig;
2180 info.si_code = TARGET_TRAP_BRKPT;
2181 queue_signal(env, info.si_signo, &info);
2186 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
2188 cpu_dump_state(env, stderr, fprintf, 0);
2191 process_pending_signals(env);
2194 #endif /* TARGET_M68K */
2197 void cpu_loop (CPUState *env)
2200 target_siginfo_t info;
2203 trapnr = cpu_alpha_exec (env);
2207 fprintf(stderr, "Reset requested. Exit\n");
2211 fprintf(stderr, "Machine check exception. Exit\n");
2215 fprintf(stderr, "Arithmetic trap.\n");
2218 case EXCP_HW_INTERRUPT:
2219 fprintf(stderr, "External interrupt. Exit\n");
2223 fprintf(stderr, "MMU data fault\n");
2226 case EXCP_DTB_MISS_PAL:
2227 fprintf(stderr, "MMU data TLB miss in PALcode\n");
2231 fprintf(stderr, "MMU instruction TLB miss\n");
2235 fprintf(stderr, "MMU instruction access violation\n");
2238 case EXCP_DTB_MISS_NATIVE:
2239 fprintf(stderr, "MMU data TLB miss\n");
2243 fprintf(stderr, "Unaligned access\n");
2247 fprintf(stderr, "Invalid instruction\n");
2251 fprintf(stderr, "Floating-point not allowed\n");
2254 case EXCP_CALL_PAL ... (EXCP_CALL_PALP - 1):
2255 call_pal(env, (trapnr >> 6) | 0x80);
2257 case EXCP_CALL_PALP ... (EXCP_CALL_PALE - 1):
2258 fprintf(stderr, "Privileged call to PALcode\n");
2265 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2268 info.si_signo = sig;
2270 info.si_code = TARGET_TRAP_BRKPT;
2271 queue_signal(env, info.si_signo, &info);
2276 printf ("Unhandled trap: 0x%x\n", trapnr);
2277 cpu_dump_state(env, stderr, fprintf, 0);
2280 process_pending_signals (env);
2283 #endif /* TARGET_ALPHA */
2285 static void usage(void)
2287 printf("qemu-" TARGET_ARCH " version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
2288 "usage: qemu-" TARGET_ARCH " [options] program [arguments...]\n"
2289 "Linux CPU emulator (compiled for %s emulation)\n"
2291 "Standard options:\n"
2292 "-h print this help\n"
2293 "-g port wait gdb connection to port\n"
2294 "-L path set the elf interpreter prefix (default=%s)\n"
2295 "-s size set the stack size in bytes (default=%ld)\n"
2296 "-cpu model select CPU (-cpu ? for list)\n"
2297 "-drop-ld-preload drop LD_PRELOAD for target process\n"
2298 "-E var=value sets/modifies targets environment variable(s)\n"
2299 "-U var unsets targets environment variable(s)\n"
2300 "-0 argv0 forces target process argv[0] to be argv0\n"
2301 #if defined(CONFIG_USE_GUEST_BASE)
2302 "-B address set guest_base address to address\n"
2306 "-d options activate log (logfile=%s)\n"
2307 "-p pagesize set the host page size to 'pagesize'\n"
2308 "-singlestep always run in singlestep mode\n"
2309 "-strace log system calls\n"
2311 "Environment variables:\n"
2312 "QEMU_STRACE Print system calls and arguments similar to the\n"
2313 " 'strace' program. Enable by setting to any value.\n"
2314 "You can use -E and -U options to set/unset environment variables\n"
2315 "for target process. It is possible to provide several variables\n"
2316 "by repeating the option. For example:\n"
2317 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
2318 "Note that if you provide several changes to single variable\n"
2319 "last change will stay in effect.\n"
2320 #if defined(CONFIG_USE_GUEST_BASE)
2322 "You can use -B option to load target binary into different\n"
2323 "address that is specified in elf headers. This can be useful\n"
2324 "when target binary would be loaded to low addresses and\n"
2325 "/proc/sys/vm/mmap_min_addr is set to higher. For example\n"
2326 " qemu-" TARGET_ARCH " -B 0x100000 ...\n"
2327 "loads target binary starting from the first meg.\n"
2337 THREAD CPUState *thread_env;
2339 void task_settid(TaskState *ts)
2341 if (ts->ts_tid == 0) {
2343 ts->ts_tid = (pid_t)syscall(SYS_gettid);
2345 /* when no threads are used, tid becomes pid */
2346 ts->ts_tid = getpid();
2351 void stop_all_tasks(void)
2354 * We trust that when using NPTL, start_exclusive()
2355 * handles thread stopping correctly.
2360 /* Assumes contents are already zeroed. */
2361 void init_task_state(TaskState *ts)
2366 ts->first_free = ts->sigqueue_table;
2367 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
2368 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
2370 ts->sigqueue_table[i].next = NULL;
2373 int main(int argc, char **argv, char **envp)
2375 char *filename = NULL;
2376 const char *cpu_model;
2377 struct target_pt_regs regs1, *regs = ®s1;
2378 struct image_info info1, *info = &info1;
2379 struct linux_binprm bprm;
2380 TaskState ts1, *ts = &ts1;
2384 int gdbstub_port = 0;
2385 char **target_environ, **wrk;
2388 int drop_ld_preload = 0;
2389 envlist_t *envlist = NULL;
2390 const char *argv0 = NULL;
2397 qemu_cache_utils_init(envp);
2400 cpu_set_log_filename(DEBUG_LOGFILE);
2402 if ((envlist = envlist_create()) == NULL) {
2403 (void) fprintf(stderr, "Unable to allocate envlist\n");
2407 /* add current environment into the list */
2408 for (wrk = environ; *wrk != NULL; wrk++) {
2409 (void) envlist_setenv(envlist, *wrk);
2422 if (!strcmp(r, "-")) {
2424 } else if (!strcmp(r, "d")) {
2426 const CPULogItem *item;
2432 mask = cpu_str_to_log_mask(r);
2434 printf("Log items (comma separated):\n");
2435 for(item = cpu_log_items; item->mask != 0; item++) {
2436 printf("%-10s %s\n", item->name, item->help);
2441 } else if (!strcmp(r, "E")) {
2443 if (envlist_setenv(envlist, r) != 0)
2445 } else if (!strcmp(r, "U")) {
2447 if (envlist_unsetenv(envlist, r) != 0)
2449 } else if (!strcmp(r, "0")) {
2452 } else if (!strcmp(r,"-sbox-call")) {
2454 } else if (!strcmp(r, "s")) {
2458 x86_stack_size = strtol(r, (char **)&r, 0);
2459 if (x86_stack_size <= 0)
2462 x86_stack_size *= 1024 * 1024;
2463 else if (*r == 'k' || *r == 'K')
2464 x86_stack_size *= 1024;
2465 } else if (!strcmp(r, "L")) {
2466 interp_prefix = argv[optind++];
2467 } else if (!strcmp(r, "p")) {
2470 qemu_host_page_size = atoi(argv[optind++]);
2471 if (qemu_host_page_size == 0 ||
2472 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
2473 fprintf(stderr, "page size must be a power of two\n");
2476 } else if (!strcmp(r, "g")) {
2479 gdbstub_port = atoi(argv[optind++]);
2480 } else if (!strcmp(r, "r")) {
2481 qemu_uname_release = argv[optind++];
2482 } else if (!strcmp(r, "cpu")) {
2483 cpu_model = argv[optind++];
2484 if (cpu_model == NULL || strcmp(cpu_model, "?") == 0) {
2485 /* XXX: implement xxx_cpu_list for targets that still miss it */
2486 #if defined(cpu_list)
2487 cpu_list(stdout, &fprintf);
2491 #if defined(CONFIG_USE_GUEST_BASE)
2492 } else if (!strcmp(r, "B")) {
2493 guest_base = strtol(argv[optind++], NULL, 0);
2495 } else if (!strcmp(r, "drop-ld-preload")) {
2496 drop_ld_preload = 1;
2497 } else if (!strcmp(r, "keep-ld-preload")) {
2498 drop_ld_preload = 0;
2499 } else if (!strcmp(r, "singlestep")) {
2501 } else if (!strcmp(r, "strace")) {
2510 if (filename == NULL) {
2511 filename = argv[optind];
2512 exec_path = argv[optind];
2514 argv0 = argv[optind];
2516 if (drop_ld_preload) {
2517 (void) envlist_unsetenv(envlist, "LD_PRELOAD");
2521 memset(regs, 0, sizeof(struct target_pt_regs));
2523 /* Zero out image_info */
2524 memset(info, 0, sizeof(struct image_info));
2526 memset(&bprm, 0, sizeof (bprm));
2528 /* Scan interp_prefix dir for replacement files. */
2529 init_paths(interp_prefix);
2531 if (cpu_model == NULL) {
2532 #if defined(TARGET_I386)
2533 #ifdef TARGET_X86_64
2534 cpu_model = "qemu64";
2536 cpu_model = "qemu32";
2538 #elif defined(TARGET_ARM)
2540 #elif defined(TARGET_M68K)
2542 #elif defined(TARGET_SPARC)
2543 #ifdef TARGET_SPARC64
2544 cpu_model = "TI UltraSparc II";
2546 cpu_model = "Fujitsu MB86904";
2548 #elif defined(TARGET_MIPS)
2549 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
2554 #elif defined(TARGET_PPC)
2564 cpu_exec_init_all(0);
2565 /* NOTE: we need to init the CPU at this stage to get
2566 qemu_host_page_size */
2567 env = cpu_init(cpu_model);
2569 fprintf(stderr, "Unable to find CPU definition\n");
2574 if (getenv("QEMU_STRACE")) {
2578 target_environ = envlist_to_environ(envlist, NULL);
2579 envlist_free(envlist);
2581 #if defined(CONFIG_USE_GUEST_BASE)
2583 * Now that page sizes are configured in cpu_init() we can do
2584 * proper page alignment for guest_base.
2586 guest_base = HOST_PAGE_ALIGN(guest_base);
2589 * Read in mmap_min_addr kernel parameter and check
2590 * whether it is set to some value > 0. This value is used
2591 * later on when doing mmap(2)s to calculate where guest_base
2592 * is to set, if needed.
2594 * When user has explicitly set the quest base, we skip this
2597 if (guest_base == 0) {
2600 if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
2602 if (fscanf(fp, "%lu", &tmp) == 1) {
2603 mmap_min_addr = tmp;
2604 qemu_log("kernel mmap_min_addr=%lu\n", mmap_min_addr);
2609 #endif /* CONFIG_USE_GUEST_BASE */
2612 * Prepare copy of argv vector for target.
2614 target_argc = argc - optind;
2615 target_argv = calloc(target_argc + 1, sizeof (char *));
2616 if (target_argv == NULL) {
2617 (void) fprintf(stderr, "Unable to allocate memory for target_argv\n");
2622 * If argv0 is specified (using '-0' switch) we replace
2623 * argv[0] pointer with the given one.
2626 if (argv0 != NULL) {
2627 target_argv[i++] = strdup(argv0);
2629 for (; i < target_argc; i++) {
2630 target_argv[i] = strdup(argv[optind + i]);
2632 target_argv[target_argc] = NULL;
2634 memset(ts, 0, sizeof(TaskState));
2635 init_task_state(ts);
2636 /* build Task State */
2642 if (loader_exec(filename, target_argv+argskip, target_environ, regs,
2643 info, &bprm) != 0) {
2644 printf("Error loading %s\n", filename);
2648 for (i = 0; i < target_argc; i++) {
2649 free(target_argv[i]);
2653 for (wrk = target_environ; *wrk; wrk++) {
2657 free(target_environ);
2659 if (qemu_log_enabled()) {
2660 #if defined(CONFIG_USE_GUEST_BASE)
2661 if (guest_base > 0) {
2662 qemu_log("guest_base is set to 0x%lx\n", guest_base);
2664 "==========================================================\n"
2665 "Note that all target addresses below are given in target\n"
2666 "address space which is different from host by guest_base.\n"
2667 "For example: target address 0x%lx becomes 0x%lx and so on.\n"
2668 "==========================================================\n",
2669 (uintptr_t)0x8000, (uintptr_t)g2h(0x8000));
2674 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
2675 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
2676 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
2678 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
2680 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
2681 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
2683 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
2684 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
2687 target_set_brk(info->brk);
2691 #if defined(TARGET_I386)
2692 cpu_x86_set_cpl(env, 3);
2694 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
2695 env->hflags |= HF_PE_MASK;
2696 if (env->cpuid_features & CPUID_SSE) {
2697 env->cr[4] |= CR4_OSFXSR_MASK;
2698 env->hflags |= HF_OSFXSR_MASK;
2700 #ifndef TARGET_ABI32
2701 /* enable 64 bit mode if possible */
2702 if (!(env->cpuid_ext2_features & CPUID_EXT2_LM)) {
2703 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
2706 env->cr[4] |= CR4_PAE_MASK;
2707 env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
2708 env->hflags |= HF_LMA_MASK;
2711 /* flags setup : we activate the IRQs by default as in user mode */
2712 env->eflags |= IF_MASK;
2714 /* linux register setup */
2715 #ifndef TARGET_ABI32
2716 env->regs[R_EAX] = regs->rax;
2717 env->regs[R_EBX] = regs->rbx;
2718 env->regs[R_ECX] = regs->rcx;
2719 env->regs[R_EDX] = regs->rdx;
2720 env->regs[R_ESI] = regs->rsi;
2721 env->regs[R_EDI] = regs->rdi;
2722 env->regs[R_EBP] = regs->rbp;
2723 env->regs[R_ESP] = regs->rsp;
2724 env->eip = regs->rip;
2726 env->regs[R_EAX] = regs->eax;
2727 env->regs[R_EBX] = regs->ebx;
2728 env->regs[R_ECX] = regs->ecx;
2729 env->regs[R_EDX] = regs->edx;
2730 env->regs[R_ESI] = regs->esi;
2731 env->regs[R_EDI] = regs->edi;
2732 env->regs[R_EBP] = regs->ebp;
2733 env->regs[R_ESP] = regs->esp;
2734 env->eip = regs->eip;
2737 /* linux interrupt setup */
2738 #ifndef TARGET_ABI32
2739 env->idt.limit = 511;
2741 env->idt.limit = 255;
2743 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
2744 PROT_READ|PROT_WRITE,
2745 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
2746 idt_table = g2h(env->idt.base);
2769 /* linux segment setup */
2771 uint64_t *gdt_table;
2772 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
2773 PROT_READ|PROT_WRITE,
2774 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
2775 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
2776 gdt_table = g2h(env->gdt.base);
2778 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
2779 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
2780 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
2782 /* 64 bit code segment */
2783 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
2784 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
2786 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
2788 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
2789 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
2790 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
2792 cpu_x86_load_seg(env, R_CS, __USER_CS);
2793 cpu_x86_load_seg(env, R_SS, __USER_DS);
2795 cpu_x86_load_seg(env, R_DS, __USER_DS);
2796 cpu_x86_load_seg(env, R_ES, __USER_DS);
2797 cpu_x86_load_seg(env, R_FS, __USER_DS);
2798 cpu_x86_load_seg(env, R_GS, __USER_DS);
2799 /* This hack makes Wine work... */
2800 env->segs[R_FS].selector = 0;
2802 cpu_x86_load_seg(env, R_DS, 0);
2803 cpu_x86_load_seg(env, R_ES, 0);
2804 cpu_x86_load_seg(env, R_FS, 0);
2805 cpu_x86_load_seg(env, R_GS, 0);
2807 #elif defined(TARGET_ARM)
2810 cpsr_write(env, regs->uregs[16], 0xffffffff);
2811 for(i = 0; i < 16; i++) {
2812 env->regs[i] = regs->uregs[i];
2815 #elif defined(TARGET_SPARC)
2819 env->npc = regs->npc;
2821 for(i = 0; i < 8; i++)
2822 env->gregs[i] = regs->u_regs[i];
2823 for(i = 0; i < 8; i++)
2824 env->regwptr[i] = regs->u_regs[i + 8];
2826 #elif defined(TARGET_PPC)
2830 #if defined(TARGET_PPC64)
2831 #if defined(TARGET_ABI32)
2832 env->msr &= ~((target_ulong)1 << MSR_SF);
2834 env->msr |= (target_ulong)1 << MSR_SF;
2837 env->nip = regs->nip;
2838 for(i = 0; i < 32; i++) {
2839 env->gpr[i] = regs->gpr[i];
2842 #elif defined(TARGET_M68K)
2845 env->dregs[0] = regs->d0;
2846 env->dregs[1] = regs->d1;
2847 env->dregs[2] = regs->d2;
2848 env->dregs[3] = regs->d3;
2849 env->dregs[4] = regs->d4;
2850 env->dregs[5] = regs->d5;
2851 env->dregs[6] = regs->d6;
2852 env->dregs[7] = regs->d7;
2853 env->aregs[0] = regs->a0;
2854 env->aregs[1] = regs->a1;
2855 env->aregs[2] = regs->a2;
2856 env->aregs[3] = regs->a3;
2857 env->aregs[4] = regs->a4;
2858 env->aregs[5] = regs->a5;
2859 env->aregs[6] = regs->a6;
2860 env->aregs[7] = regs->usp;
2862 ts->sim_syscalls = 1;
2864 #elif defined(TARGET_MICROBLAZE)
2866 env->regs[0] = regs->r0;
2867 env->regs[1] = regs->r1;
2868 env->regs[2] = regs->r2;
2869 env->regs[3] = regs->r3;
2870 env->regs[4] = regs->r4;
2871 env->regs[5] = regs->r5;
2872 env->regs[6] = regs->r6;
2873 env->regs[7] = regs->r7;
2874 env->regs[8] = regs->r8;
2875 env->regs[9] = regs->r9;
2876 env->regs[10] = regs->r10;
2877 env->regs[11] = regs->r11;
2878 env->regs[12] = regs->r12;
2879 env->regs[13] = regs->r13;
2880 env->regs[14] = regs->r14;
2881 env->regs[15] = regs->r15;
2882 env->regs[16] = regs->r16;
2883 env->regs[17] = regs->r17;
2884 env->regs[18] = regs->r18;
2885 env->regs[19] = regs->r19;
2886 env->regs[20] = regs->r20;
2887 env->regs[21] = regs->r21;
2888 env->regs[22] = regs->r22;
2889 env->regs[23] = regs->r23;
2890 env->regs[24] = regs->r24;
2891 env->regs[25] = regs->r25;
2892 env->regs[26] = regs->r26;
2893 env->regs[27] = regs->r27;
2894 env->regs[28] = regs->r28;
2895 env->regs[29] = regs->r29;
2896 env->regs[30] = regs->r30;
2897 env->regs[31] = regs->r31;
2898 env->sregs[SR_PC] = regs->pc;
2900 #elif defined(TARGET_MIPS)
2904 for(i = 0; i < 32; i++) {
2905 env->active_tc.gpr[i] = regs->regs[i];
2907 env->active_tc.PC = regs->cp0_epc;
2909 #elif defined(TARGET_SH4)
2913 for(i = 0; i < 16; i++) {
2914 env->gregs[i] = regs->regs[i];
2918 #elif defined(TARGET_ALPHA)
2922 for(i = 0; i < 28; i++) {
2923 env->ir[i] = ((abi_ulong *)regs)[i];
2925 env->ipr[IPR_USP] = regs->usp;
2926 env->ir[30] = regs->usp;
2928 env->unique = regs->unique;
2930 #elif defined(TARGET_CRIS)
2932 env->regs[0] = regs->r0;
2933 env->regs[1] = regs->r1;
2934 env->regs[2] = regs->r2;
2935 env->regs[3] = regs->r3;
2936 env->regs[4] = regs->r4;
2937 env->regs[5] = regs->r5;
2938 env->regs[6] = regs->r6;
2939 env->regs[7] = regs->r7;
2940 env->regs[8] = regs->r8;
2941 env->regs[9] = regs->r9;
2942 env->regs[10] = regs->r10;
2943 env->regs[11] = regs->r11;
2944 env->regs[12] = regs->r12;
2945 env->regs[13] = regs->r13;
2946 env->regs[14] = info->start_stack;
2947 env->regs[15] = regs->acr;
2948 env->pc = regs->erp;
2951 #error unsupported target CPU
2954 #if defined(TARGET_ARM) || defined(TARGET_M68K)
2955 ts->stack_base = info->start_stack;
2956 ts->heap_base = info->brk;
2957 /* This will be filled in on the first SYS_HEAPINFO call. */
2962 gdbserver_start (gdbstub_port);
2963 gdb_handlesig(env, 0);