184778508Sblueswir1 /* 284778508Sblueswir1 * qemu user main 384778508Sblueswir1 * 484778508Sblueswir1 * Copyright (c) 2003-2008 Fabrice Bellard 584778508Sblueswir1 * 684778508Sblueswir1 * This program is free software; you can redistribute it and/or modify 784778508Sblueswir1 * it under the terms of the GNU General Public License as published by 884778508Sblueswir1 * the Free Software Foundation; either version 2 of the License, or 984778508Sblueswir1 * (at your option) any later version. 1084778508Sblueswir1 * 1184778508Sblueswir1 * This program is distributed in the hope that it will be useful, 1284778508Sblueswir1 * but WITHOUT ANY WARRANTY; without even the implied warranty of 1384778508Sblueswir1 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1484778508Sblueswir1 * GNU General Public License for more details. 1584778508Sblueswir1 * 1684778508Sblueswir1 * You should have received a copy of the GNU General Public License 178167ee88SBlue Swirl * along with this program; if not, see <http://www.gnu.org/licenses/>. 1884778508Sblueswir1 */ 192231197cSPeter Maydell #include "qemu/osdep.h" 2084778508Sblueswir1 #include <machine/trap.h> 2184778508Sblueswir1 22daa76aa4SMarkus Armbruster #include "qapi/error.h" 2384778508Sblueswir1 #include "qemu.h" 246913e79cSLluís Vilanova #include "qemu/config-file.h" 25f348b6d1SVeronia Bahaa #include "qemu/path.h" 26f348b6d1SVeronia Bahaa #include "qemu/help_option.h" 2784778508Sblueswir1 /* For tb_lock */ 282b41f10eSBlue Swirl #include "cpu.h" 2963c91552SPaolo Bonzini #include "exec/exec-all.h" 309002ec79SRichard Henderson #include "tcg.h" 311de7afc9SPaolo Bonzini #include "qemu/timer.h" 321de7afc9SPaolo Bonzini #include "qemu/envlist.h" 33508127e2SPaolo Bonzini #include "exec/log.h" 346913e79cSLluís Vilanova #include "trace/control.h" 356913e79cSLluís Vilanova #include "glib-compat.h" 36fc0d96b4SBlue Swirl 371b530a6dSaurel32 int singlestep; 382fa5d9baSBlue Swirl unsigned long mmap_min_addr; 392fa5d9baSBlue Swirl unsigned long guest_base; 402fa5d9baSBlue Swirl int have_guest_base; 41d6ef40bfSPeter Maydell unsigned long reserved_va; 421b530a6dSaurel32 437ee2822cSPaolo Bonzini static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX; 44fccae322SPeter Maydell const char *qemu_uname_release; 4584778508Sblueswir1 extern char **environ; 4678cfb07fSJuergen Lock enum BSDType bsd_type; 4784778508Sblueswir1 4884778508Sblueswir1 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so 4984778508Sblueswir1 we allocate a bigger stack. Need a better solution, for example 5084778508Sblueswir1 by remapping the process stack directly at the right place */ 5184778508Sblueswir1 unsigned long x86_stack_size = 512 * 1024; 5284778508Sblueswir1 5384778508Sblueswir1 void gemu_log(const char *fmt, ...) 5484778508Sblueswir1 { 5584778508Sblueswir1 va_list ap; 5684778508Sblueswir1 5784778508Sblueswir1 va_start(ap, fmt); 5884778508Sblueswir1 vfprintf(stderr, fmt, ap); 5984778508Sblueswir1 va_end(ap); 6084778508Sblueswir1 } 619399f095Sblueswir1 6231fc12dfSblueswir1 #if defined(TARGET_I386) 63b98e9ca8SAndreas Färber int cpu_get_pic_interrupt(CPUX86State *env) 6431fc12dfSblueswir1 { 6531fc12dfSblueswir1 return -1; 6631fc12dfSblueswir1 } 6731fc12dfSblueswir1 #endif 6831fc12dfSblueswir1 699399f095Sblueswir1 /* These are no-ops because we are not threadsafe. */ 709349b4f9SAndreas Färber static inline void cpu_exec_start(CPUArchState *env) 719399f095Sblueswir1 { 729399f095Sblueswir1 } 739399f095Sblueswir1 749349b4f9SAndreas Färber static inline void cpu_exec_end(CPUArchState *env) 759399f095Sblueswir1 { 769399f095Sblueswir1 } 779399f095Sblueswir1 789399f095Sblueswir1 static inline void start_exclusive(void) 799399f095Sblueswir1 { 809399f095Sblueswir1 } 819399f095Sblueswir1 829399f095Sblueswir1 static inline void end_exclusive(void) 839399f095Sblueswir1 { 849399f095Sblueswir1 } 859399f095Sblueswir1 869399f095Sblueswir1 void fork_start(void) 879399f095Sblueswir1 { 889399f095Sblueswir1 } 899399f095Sblueswir1 909399f095Sblueswir1 void fork_end(int child) 919399f095Sblueswir1 { 929399f095Sblueswir1 if (child) { 93f7ec7f7bSPeter Crosthwaite gdbserver_fork(thread_cpu); 949399f095Sblueswir1 } 959399f095Sblueswir1 } 969399f095Sblueswir1 979399f095Sblueswir1 void cpu_list_lock(void) 989399f095Sblueswir1 { 999399f095Sblueswir1 } 1009399f095Sblueswir1 1019399f095Sblueswir1 void cpu_list_unlock(void) 1029399f095Sblueswir1 { 1039399f095Sblueswir1 } 1049399f095Sblueswir1 10531fc12dfSblueswir1 #ifdef TARGET_I386 10631fc12dfSblueswir1 /***********************************************************/ 10731fc12dfSblueswir1 /* CPUX86 core interface */ 10831fc12dfSblueswir1 10931fc12dfSblueswir1 uint64_t cpu_get_tsc(CPUX86State *env) 11031fc12dfSblueswir1 { 1114a7428c5SChristopher Covington return cpu_get_host_ticks(); 11231fc12dfSblueswir1 } 11331fc12dfSblueswir1 11431fc12dfSblueswir1 static void write_dt(void *ptr, unsigned long addr, unsigned long limit, 11531fc12dfSblueswir1 int flags) 11631fc12dfSblueswir1 { 11731fc12dfSblueswir1 unsigned int e1, e2; 11831fc12dfSblueswir1 uint32_t *p; 11931fc12dfSblueswir1 e1 = (addr << 16) | (limit & 0xffff); 12031fc12dfSblueswir1 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000); 12131fc12dfSblueswir1 e2 |= flags; 12231fc12dfSblueswir1 p = ptr; 12331fc12dfSblueswir1 p[0] = tswap32(e1); 12431fc12dfSblueswir1 p[1] = tswap32(e2); 12531fc12dfSblueswir1 } 12631fc12dfSblueswir1 12731fc12dfSblueswir1 static uint64_t *idt_table; 12831fc12dfSblueswir1 #ifdef TARGET_X86_64 12931fc12dfSblueswir1 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl, 13031fc12dfSblueswir1 uint64_t addr, unsigned int sel) 13131fc12dfSblueswir1 { 13231fc12dfSblueswir1 uint32_t *p, e1, e2; 13331fc12dfSblueswir1 e1 = (addr & 0xffff) | (sel << 16); 13431fc12dfSblueswir1 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); 13531fc12dfSblueswir1 p = ptr; 13631fc12dfSblueswir1 p[0] = tswap32(e1); 13731fc12dfSblueswir1 p[1] = tswap32(e2); 13831fc12dfSblueswir1 p[2] = tswap32(addr >> 32); 13931fc12dfSblueswir1 p[3] = 0; 14031fc12dfSblueswir1 } 14131fc12dfSblueswir1 /* only dpl matters as we do only user space emulation */ 14231fc12dfSblueswir1 static void set_idt(int n, unsigned int dpl) 14331fc12dfSblueswir1 { 14431fc12dfSblueswir1 set_gate64(idt_table + n * 2, 0, dpl, 0, 0); 14531fc12dfSblueswir1 } 14631fc12dfSblueswir1 #else 14731fc12dfSblueswir1 static void set_gate(void *ptr, unsigned int type, unsigned int dpl, 14831fc12dfSblueswir1 uint32_t addr, unsigned int sel) 14931fc12dfSblueswir1 { 15031fc12dfSblueswir1 uint32_t *p, e1, e2; 15131fc12dfSblueswir1 e1 = (addr & 0xffff) | (sel << 16); 15231fc12dfSblueswir1 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); 15331fc12dfSblueswir1 p = ptr; 15431fc12dfSblueswir1 p[0] = tswap32(e1); 15531fc12dfSblueswir1 p[1] = tswap32(e2); 15631fc12dfSblueswir1 } 15731fc12dfSblueswir1 15831fc12dfSblueswir1 /* only dpl matters as we do only user space emulation */ 15931fc12dfSblueswir1 static void set_idt(int n, unsigned int dpl) 16031fc12dfSblueswir1 { 16131fc12dfSblueswir1 set_gate(idt_table + n, 0, dpl, 0, 0); 16231fc12dfSblueswir1 } 16331fc12dfSblueswir1 #endif 16431fc12dfSblueswir1 16578cfb07fSJuergen Lock void cpu_loop(CPUX86State *env) 16631fc12dfSblueswir1 { 167ea3e9847SPeter Crosthwaite X86CPU *cpu = x86_env_get_cpu(env); 168ea3e9847SPeter Crosthwaite CPUState *cs = CPU(cpu); 16931fc12dfSblueswir1 int trapnr; 17031fc12dfSblueswir1 abi_ulong pc; 17131fc12dfSblueswir1 //target_siginfo_t info; 17231fc12dfSblueswir1 17331fc12dfSblueswir1 for(;;) { 174ded554cdSSean Bruno trapnr = cpu_exec(cs); 17531fc12dfSblueswir1 switch(trapnr) { 17631fc12dfSblueswir1 case 0x80: 17731fc12dfSblueswir1 /* syscall from int $0x80 */ 17878cfb07fSJuergen Lock if (bsd_type == target_freebsd) { 17978cfb07fSJuergen Lock abi_ulong params = (abi_ulong) env->regs[R_ESP] + 18078cfb07fSJuergen Lock sizeof(int32_t); 18178cfb07fSJuergen Lock int32_t syscall_nr = env->regs[R_EAX]; 18278cfb07fSJuergen Lock int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8; 18378cfb07fSJuergen Lock 18478cfb07fSJuergen Lock if (syscall_nr == TARGET_FREEBSD_NR_syscall) { 18578cfb07fSJuergen Lock get_user_s32(syscall_nr, params); 18678cfb07fSJuergen Lock params += sizeof(int32_t); 18778cfb07fSJuergen Lock } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) { 18878cfb07fSJuergen Lock get_user_s32(syscall_nr, params); 18978cfb07fSJuergen Lock params += sizeof(int64_t); 19078cfb07fSJuergen Lock } 19178cfb07fSJuergen Lock get_user_s32(arg1, params); 19278cfb07fSJuergen Lock params += sizeof(int32_t); 19378cfb07fSJuergen Lock get_user_s32(arg2, params); 19478cfb07fSJuergen Lock params += sizeof(int32_t); 19578cfb07fSJuergen Lock get_user_s32(arg3, params); 19678cfb07fSJuergen Lock params += sizeof(int32_t); 19778cfb07fSJuergen Lock get_user_s32(arg4, params); 19878cfb07fSJuergen Lock params += sizeof(int32_t); 19978cfb07fSJuergen Lock get_user_s32(arg5, params); 20078cfb07fSJuergen Lock params += sizeof(int32_t); 20178cfb07fSJuergen Lock get_user_s32(arg6, params); 20278cfb07fSJuergen Lock params += sizeof(int32_t); 20378cfb07fSJuergen Lock get_user_s32(arg7, params); 20478cfb07fSJuergen Lock params += sizeof(int32_t); 20578cfb07fSJuergen Lock get_user_s32(arg8, params); 20678cfb07fSJuergen Lock env->regs[R_EAX] = do_freebsd_syscall(env, 20778cfb07fSJuergen Lock syscall_nr, 20878cfb07fSJuergen Lock arg1, 20978cfb07fSJuergen Lock arg2, 21078cfb07fSJuergen Lock arg3, 21178cfb07fSJuergen Lock arg4, 21278cfb07fSJuergen Lock arg5, 21378cfb07fSJuergen Lock arg6, 21478cfb07fSJuergen Lock arg7, 21578cfb07fSJuergen Lock arg8); 21678cfb07fSJuergen Lock } else { //if (bsd_type == target_openbsd) 21731fc12dfSblueswir1 env->regs[R_EAX] = do_openbsd_syscall(env, 21831fc12dfSblueswir1 env->regs[R_EAX], 21931fc12dfSblueswir1 env->regs[R_EBX], 22031fc12dfSblueswir1 env->regs[R_ECX], 22131fc12dfSblueswir1 env->regs[R_EDX], 22231fc12dfSblueswir1 env->regs[R_ESI], 22331fc12dfSblueswir1 env->regs[R_EDI], 22431fc12dfSblueswir1 env->regs[R_EBP]); 22578cfb07fSJuergen Lock } 22678cfb07fSJuergen Lock if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) { 22778cfb07fSJuergen Lock env->regs[R_EAX] = -env->regs[R_EAX]; 22878cfb07fSJuergen Lock env->eflags |= CC_C; 22978cfb07fSJuergen Lock } else { 23078cfb07fSJuergen Lock env->eflags &= ~CC_C; 23178cfb07fSJuergen Lock } 23231fc12dfSblueswir1 break; 23331fc12dfSblueswir1 #ifndef TARGET_ABI32 23431fc12dfSblueswir1 case EXCP_SYSCALL: 2355ba18547SStefan Weil /* syscall from syscall instruction */ 23678cfb07fSJuergen Lock if (bsd_type == target_freebsd) 23778cfb07fSJuergen Lock env->regs[R_EAX] = do_freebsd_syscall(env, 23878cfb07fSJuergen Lock env->regs[R_EAX], 23978cfb07fSJuergen Lock env->regs[R_EDI], 24078cfb07fSJuergen Lock env->regs[R_ESI], 24178cfb07fSJuergen Lock env->regs[R_EDX], 24278cfb07fSJuergen Lock env->regs[R_ECX], 24378cfb07fSJuergen Lock env->regs[8], 24478cfb07fSJuergen Lock env->regs[9], 0, 0); 24578cfb07fSJuergen Lock else { //if (bsd_type == target_openbsd) 24631fc12dfSblueswir1 env->regs[R_EAX] = do_openbsd_syscall(env, 24731fc12dfSblueswir1 env->regs[R_EAX], 24831fc12dfSblueswir1 env->regs[R_EDI], 24931fc12dfSblueswir1 env->regs[R_ESI], 25031fc12dfSblueswir1 env->regs[R_EDX], 25131fc12dfSblueswir1 env->regs[10], 25231fc12dfSblueswir1 env->regs[8], 25331fc12dfSblueswir1 env->regs[9]); 25478cfb07fSJuergen Lock } 25531fc12dfSblueswir1 env->eip = env->exception_next_eip; 25678cfb07fSJuergen Lock if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) { 25778cfb07fSJuergen Lock env->regs[R_EAX] = -env->regs[R_EAX]; 25878cfb07fSJuergen Lock env->eflags |= CC_C; 25978cfb07fSJuergen Lock } else { 26078cfb07fSJuergen Lock env->eflags &= ~CC_C; 26178cfb07fSJuergen Lock } 26231fc12dfSblueswir1 break; 26331fc12dfSblueswir1 #endif 26431fc12dfSblueswir1 #if 0 26531fc12dfSblueswir1 case EXCP0B_NOSEG: 26631fc12dfSblueswir1 case EXCP0C_STACK: 26731fc12dfSblueswir1 info.si_signo = SIGBUS; 26831fc12dfSblueswir1 info.si_errno = 0; 26931fc12dfSblueswir1 info.si_code = TARGET_SI_KERNEL; 27031fc12dfSblueswir1 info._sifields._sigfault._addr = 0; 27131fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 27231fc12dfSblueswir1 break; 27331fc12dfSblueswir1 case EXCP0D_GPF: 27431fc12dfSblueswir1 /* XXX: potential problem if ABI32 */ 27531fc12dfSblueswir1 #ifndef TARGET_X86_64 27631fc12dfSblueswir1 if (env->eflags & VM_MASK) { 27731fc12dfSblueswir1 handle_vm86_fault(env); 27831fc12dfSblueswir1 } else 27931fc12dfSblueswir1 #endif 28031fc12dfSblueswir1 { 28131fc12dfSblueswir1 info.si_signo = SIGSEGV; 28231fc12dfSblueswir1 info.si_errno = 0; 28331fc12dfSblueswir1 info.si_code = TARGET_SI_KERNEL; 28431fc12dfSblueswir1 info._sifields._sigfault._addr = 0; 28531fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 28631fc12dfSblueswir1 } 28731fc12dfSblueswir1 break; 28831fc12dfSblueswir1 case EXCP0E_PAGE: 28931fc12dfSblueswir1 info.si_signo = SIGSEGV; 29031fc12dfSblueswir1 info.si_errno = 0; 29131fc12dfSblueswir1 if (!(env->error_code & 1)) 29231fc12dfSblueswir1 info.si_code = TARGET_SEGV_MAPERR; 29331fc12dfSblueswir1 else 29431fc12dfSblueswir1 info.si_code = TARGET_SEGV_ACCERR; 29531fc12dfSblueswir1 info._sifields._sigfault._addr = env->cr[2]; 29631fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 29731fc12dfSblueswir1 break; 29831fc12dfSblueswir1 case EXCP00_DIVZ: 29931fc12dfSblueswir1 #ifndef TARGET_X86_64 30031fc12dfSblueswir1 if (env->eflags & VM_MASK) { 30131fc12dfSblueswir1 handle_vm86_trap(env, trapnr); 30231fc12dfSblueswir1 } else 30331fc12dfSblueswir1 #endif 30431fc12dfSblueswir1 { 30531fc12dfSblueswir1 /* division by zero */ 30631fc12dfSblueswir1 info.si_signo = SIGFPE; 30731fc12dfSblueswir1 info.si_errno = 0; 30831fc12dfSblueswir1 info.si_code = TARGET_FPE_INTDIV; 30931fc12dfSblueswir1 info._sifields._sigfault._addr = env->eip; 31031fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 31131fc12dfSblueswir1 } 31231fc12dfSblueswir1 break; 31331fc12dfSblueswir1 case EXCP01_DB: 31431fc12dfSblueswir1 case EXCP03_INT3: 31531fc12dfSblueswir1 #ifndef TARGET_X86_64 31631fc12dfSblueswir1 if (env->eflags & VM_MASK) { 31731fc12dfSblueswir1 handle_vm86_trap(env, trapnr); 31831fc12dfSblueswir1 } else 31931fc12dfSblueswir1 #endif 32031fc12dfSblueswir1 { 32131fc12dfSblueswir1 info.si_signo = SIGTRAP; 32231fc12dfSblueswir1 info.si_errno = 0; 32331fc12dfSblueswir1 if (trapnr == EXCP01_DB) { 32431fc12dfSblueswir1 info.si_code = TARGET_TRAP_BRKPT; 32531fc12dfSblueswir1 info._sifields._sigfault._addr = env->eip; 32631fc12dfSblueswir1 } else { 32731fc12dfSblueswir1 info.si_code = TARGET_SI_KERNEL; 32831fc12dfSblueswir1 info._sifields._sigfault._addr = 0; 32931fc12dfSblueswir1 } 33031fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 33131fc12dfSblueswir1 } 33231fc12dfSblueswir1 break; 33331fc12dfSblueswir1 case EXCP04_INTO: 33431fc12dfSblueswir1 case EXCP05_BOUND: 33531fc12dfSblueswir1 #ifndef TARGET_X86_64 33631fc12dfSblueswir1 if (env->eflags & VM_MASK) { 33731fc12dfSblueswir1 handle_vm86_trap(env, trapnr); 33831fc12dfSblueswir1 } else 33931fc12dfSblueswir1 #endif 34031fc12dfSblueswir1 { 34131fc12dfSblueswir1 info.si_signo = SIGSEGV; 34231fc12dfSblueswir1 info.si_errno = 0; 34331fc12dfSblueswir1 info.si_code = TARGET_SI_KERNEL; 34431fc12dfSblueswir1 info._sifields._sigfault._addr = 0; 34531fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 34631fc12dfSblueswir1 } 34731fc12dfSblueswir1 break; 34831fc12dfSblueswir1 case EXCP06_ILLOP: 34931fc12dfSblueswir1 info.si_signo = SIGILL; 35031fc12dfSblueswir1 info.si_errno = 0; 35131fc12dfSblueswir1 info.si_code = TARGET_ILL_ILLOPN; 35231fc12dfSblueswir1 info._sifields._sigfault._addr = env->eip; 35331fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 35431fc12dfSblueswir1 break; 35531fc12dfSblueswir1 #endif 35631fc12dfSblueswir1 case EXCP_INTERRUPT: 35731fc12dfSblueswir1 /* just indicate that signals should be handled asap */ 35831fc12dfSblueswir1 break; 35931fc12dfSblueswir1 #if 0 36031fc12dfSblueswir1 case EXCP_DEBUG: 36131fc12dfSblueswir1 { 36231fc12dfSblueswir1 int sig; 36331fc12dfSblueswir1 36431fc12dfSblueswir1 sig = gdb_handlesig (env, TARGET_SIGTRAP); 36531fc12dfSblueswir1 if (sig) 36631fc12dfSblueswir1 { 36731fc12dfSblueswir1 info.si_signo = sig; 36831fc12dfSblueswir1 info.si_errno = 0; 36931fc12dfSblueswir1 info.si_code = TARGET_TRAP_BRKPT; 37031fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 37131fc12dfSblueswir1 } 37231fc12dfSblueswir1 } 37331fc12dfSblueswir1 break; 37431fc12dfSblueswir1 #endif 37531fc12dfSblueswir1 default: 37631fc12dfSblueswir1 pc = env->segs[R_CS].base + env->eip; 37731fc12dfSblueswir1 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n", 37831fc12dfSblueswir1 (long)pc, trapnr); 37931fc12dfSblueswir1 abort(); 38031fc12dfSblueswir1 } 38131fc12dfSblueswir1 process_pending_signals(env); 38231fc12dfSblueswir1 } 38331fc12dfSblueswir1 } 38431fc12dfSblueswir1 #endif 38531fc12dfSblueswir1 38684778508Sblueswir1 #ifdef TARGET_SPARC 38784778508Sblueswir1 #define SPARC64_STACK_BIAS 2047 38884778508Sblueswir1 38984778508Sblueswir1 //#define DEBUG_WIN 39084778508Sblueswir1 /* WARNING: dealing with register windows _is_ complicated. More info 39184778508Sblueswir1 can be found at http://www.sics.se/~psm/sparcstack.html */ 39284778508Sblueswir1 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index) 39384778508Sblueswir1 { 39484778508Sblueswir1 index = (index + cwp * 16) % (16 * env->nwindows); 39584778508Sblueswir1 /* wrap handling : if cwp is on the last window, then we use the 39684778508Sblueswir1 registers 'after' the end */ 39784778508Sblueswir1 if (index < 8 && env->cwp == env->nwindows - 1) 39884778508Sblueswir1 index += 16 * env->nwindows; 39984778508Sblueswir1 return index; 40084778508Sblueswir1 } 40184778508Sblueswir1 40284778508Sblueswir1 /* save the register window 'cwp1' */ 40384778508Sblueswir1 static inline void save_window_offset(CPUSPARCState *env, int cwp1) 40484778508Sblueswir1 { 40584778508Sblueswir1 unsigned int i; 40684778508Sblueswir1 abi_ulong sp_ptr; 40784778508Sblueswir1 40884778508Sblueswir1 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)]; 40984778508Sblueswir1 #ifdef TARGET_SPARC64 41084778508Sblueswir1 if (sp_ptr & 3) 41184778508Sblueswir1 sp_ptr += SPARC64_STACK_BIAS; 41284778508Sblueswir1 #endif 41384778508Sblueswir1 #if defined(DEBUG_WIN) 41484778508Sblueswir1 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n", 41584778508Sblueswir1 sp_ptr, cwp1); 41684778508Sblueswir1 #endif 41784778508Sblueswir1 for(i = 0; i < 16; i++) { 41884778508Sblueswir1 /* FIXME - what to do if put_user() fails? */ 41984778508Sblueswir1 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr); 42084778508Sblueswir1 sp_ptr += sizeof(abi_ulong); 42184778508Sblueswir1 } 42284778508Sblueswir1 } 42384778508Sblueswir1 42484778508Sblueswir1 static void save_window(CPUSPARCState *env) 42584778508Sblueswir1 { 42684778508Sblueswir1 #ifndef TARGET_SPARC64 42784778508Sblueswir1 unsigned int new_wim; 42884778508Sblueswir1 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) & 42984778508Sblueswir1 ((1LL << env->nwindows) - 1); 43084778508Sblueswir1 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2)); 43184778508Sblueswir1 env->wim = new_wim; 43284778508Sblueswir1 #else 43384778508Sblueswir1 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2)); 43484778508Sblueswir1 env->cansave++; 43584778508Sblueswir1 env->canrestore--; 43684778508Sblueswir1 #endif 43784778508Sblueswir1 } 43884778508Sblueswir1 43984778508Sblueswir1 static void restore_window(CPUSPARCState *env) 44084778508Sblueswir1 { 44184778508Sblueswir1 #ifndef TARGET_SPARC64 44284778508Sblueswir1 unsigned int new_wim; 44384778508Sblueswir1 #endif 44484778508Sblueswir1 unsigned int i, cwp1; 44584778508Sblueswir1 abi_ulong sp_ptr; 44684778508Sblueswir1 44784778508Sblueswir1 #ifndef TARGET_SPARC64 44884778508Sblueswir1 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) & 44984778508Sblueswir1 ((1LL << env->nwindows) - 1); 45084778508Sblueswir1 #endif 45184778508Sblueswir1 45284778508Sblueswir1 /* restore the invalid window */ 45384778508Sblueswir1 cwp1 = cpu_cwp_inc(env, env->cwp + 1); 45484778508Sblueswir1 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)]; 45584778508Sblueswir1 #ifdef TARGET_SPARC64 45684778508Sblueswir1 if (sp_ptr & 3) 45784778508Sblueswir1 sp_ptr += SPARC64_STACK_BIAS; 45884778508Sblueswir1 #endif 45984778508Sblueswir1 #if defined(DEBUG_WIN) 46084778508Sblueswir1 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n", 46184778508Sblueswir1 sp_ptr, cwp1); 46284778508Sblueswir1 #endif 46384778508Sblueswir1 for(i = 0; i < 16; i++) { 46484778508Sblueswir1 /* FIXME - what to do if get_user() fails? */ 46584778508Sblueswir1 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr); 46684778508Sblueswir1 sp_ptr += sizeof(abi_ulong); 46784778508Sblueswir1 } 46884778508Sblueswir1 #ifdef TARGET_SPARC64 46984778508Sblueswir1 env->canrestore++; 47084778508Sblueswir1 if (env->cleanwin < env->nwindows - 1) 47184778508Sblueswir1 env->cleanwin++; 47284778508Sblueswir1 env->cansave--; 47384778508Sblueswir1 #else 47484778508Sblueswir1 env->wim = new_wim; 47584778508Sblueswir1 #endif 47684778508Sblueswir1 } 47784778508Sblueswir1 47884778508Sblueswir1 static void flush_windows(CPUSPARCState *env) 47984778508Sblueswir1 { 48084778508Sblueswir1 int offset, cwp1; 48184778508Sblueswir1 48284778508Sblueswir1 offset = 1; 48384778508Sblueswir1 for(;;) { 48484778508Sblueswir1 /* if restore would invoke restore_window(), then we can stop */ 48584778508Sblueswir1 cwp1 = cpu_cwp_inc(env, env->cwp + offset); 48684778508Sblueswir1 #ifndef TARGET_SPARC64 48784778508Sblueswir1 if (env->wim & (1 << cwp1)) 48884778508Sblueswir1 break; 48984778508Sblueswir1 #else 49084778508Sblueswir1 if (env->canrestore == 0) 49184778508Sblueswir1 break; 49284778508Sblueswir1 env->cansave++; 49384778508Sblueswir1 env->canrestore--; 49484778508Sblueswir1 #endif 49584778508Sblueswir1 save_window_offset(env, cwp1); 49684778508Sblueswir1 offset++; 49784778508Sblueswir1 } 49884778508Sblueswir1 cwp1 = cpu_cwp_inc(env, env->cwp + 1); 49984778508Sblueswir1 #ifndef TARGET_SPARC64 50084778508Sblueswir1 /* set wim so that restore will reload the registers */ 50184778508Sblueswir1 env->wim = 1 << cwp1; 50284778508Sblueswir1 #endif 50384778508Sblueswir1 #if defined(DEBUG_WIN) 50484778508Sblueswir1 printf("flush_windows: nb=%d\n", offset - 1); 50584778508Sblueswir1 #endif 50684778508Sblueswir1 } 50784778508Sblueswir1 50878cfb07fSJuergen Lock void cpu_loop(CPUSPARCState *env) 50984778508Sblueswir1 { 510878096eeSAndreas Färber CPUState *cs = CPU(sparc_env_get_cpu(env)); 51184778508Sblueswir1 int trapnr, ret, syscall_nr; 51284778508Sblueswir1 //target_siginfo_t info; 51384778508Sblueswir1 51484778508Sblueswir1 while (1) { 515ded554cdSSean Bruno trapnr = cpu_exec(cs); 51684778508Sblueswir1 51784778508Sblueswir1 switch (trapnr) { 51877b9435fSblueswir1 #ifndef TARGET_SPARC64 51977b9435fSblueswir1 case 0x80: 52077b9435fSblueswir1 #else 52178cfb07fSJuergen Lock /* FreeBSD uses 0x141 for syscalls too */ 52278cfb07fSJuergen Lock case 0x141: 52378cfb07fSJuergen Lock if (bsd_type != target_freebsd) 52478cfb07fSJuergen Lock goto badtrap; 52584778508Sblueswir1 case 0x100: 52677b9435fSblueswir1 #endif 52784778508Sblueswir1 syscall_nr = env->gregs[1]; 52884778508Sblueswir1 if (bsd_type == target_freebsd) 52984778508Sblueswir1 ret = do_freebsd_syscall(env, syscall_nr, 53084778508Sblueswir1 env->regwptr[0], env->regwptr[1], 53184778508Sblueswir1 env->regwptr[2], env->regwptr[3], 53278cfb07fSJuergen Lock env->regwptr[4], env->regwptr[5], 0, 0); 53384778508Sblueswir1 else if (bsd_type == target_netbsd) 53484778508Sblueswir1 ret = do_netbsd_syscall(env, syscall_nr, 53584778508Sblueswir1 env->regwptr[0], env->regwptr[1], 53684778508Sblueswir1 env->regwptr[2], env->regwptr[3], 53784778508Sblueswir1 env->regwptr[4], env->regwptr[5]); 538cdba95bdSblueswir1 else { //if (bsd_type == target_openbsd) 539cdba95bdSblueswir1 #if defined(TARGET_SPARC64) 540cdba95bdSblueswir1 syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG | 541cdba95bdSblueswir1 TARGET_OPENBSD_SYSCALL_G2RFLAG); 542cdba95bdSblueswir1 #endif 54384778508Sblueswir1 ret = do_openbsd_syscall(env, syscall_nr, 54484778508Sblueswir1 env->regwptr[0], env->regwptr[1], 54584778508Sblueswir1 env->regwptr[2], env->regwptr[3], 54684778508Sblueswir1 env->regwptr[4], env->regwptr[5]); 547cdba95bdSblueswir1 } 54884778508Sblueswir1 if ((unsigned int)ret >= (unsigned int)(-515)) { 54978cfb07fSJuergen Lock ret = -ret; 55084778508Sblueswir1 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) 55184778508Sblueswir1 env->xcc |= PSR_CARRY; 55284778508Sblueswir1 #else 55384778508Sblueswir1 env->psr |= PSR_CARRY; 55484778508Sblueswir1 #endif 55584778508Sblueswir1 } else { 55684778508Sblueswir1 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) 55784778508Sblueswir1 env->xcc &= ~PSR_CARRY; 55884778508Sblueswir1 #else 55984778508Sblueswir1 env->psr &= ~PSR_CARRY; 56084778508Sblueswir1 #endif 56184778508Sblueswir1 } 56284778508Sblueswir1 env->regwptr[0] = ret; 56384778508Sblueswir1 /* next instruction */ 564cdba95bdSblueswir1 #if defined(TARGET_SPARC64) 565cdba95bdSblueswir1 if (bsd_type == target_openbsd && 566cdba95bdSblueswir1 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) { 56784778508Sblueswir1 env->pc = env->gregs[2]; 56884778508Sblueswir1 env->npc = env->pc + 4; 569cdba95bdSblueswir1 } else if (bsd_type == target_openbsd && 570cdba95bdSblueswir1 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) { 57184778508Sblueswir1 env->pc = env->gregs[7]; 57284778508Sblueswir1 env->npc = env->pc + 4; 57384778508Sblueswir1 } else { 57484778508Sblueswir1 env->pc = env->npc; 57584778508Sblueswir1 env->npc = env->npc + 4; 57684778508Sblueswir1 } 57784778508Sblueswir1 #else 57884778508Sblueswir1 env->pc = env->npc; 57984778508Sblueswir1 env->npc = env->npc + 4; 58084778508Sblueswir1 #endif 58184778508Sblueswir1 break; 58284778508Sblueswir1 case 0x83: /* flush windows */ 58384778508Sblueswir1 #ifdef TARGET_ABI32 58484778508Sblueswir1 case 0x103: 58584778508Sblueswir1 #endif 58684778508Sblueswir1 flush_windows(env); 58784778508Sblueswir1 /* next instruction */ 58884778508Sblueswir1 env->pc = env->npc; 58984778508Sblueswir1 env->npc = env->npc + 4; 59084778508Sblueswir1 break; 59184778508Sblueswir1 #ifndef TARGET_SPARC64 59284778508Sblueswir1 case TT_WIN_OVF: /* window overflow */ 59384778508Sblueswir1 save_window(env); 59484778508Sblueswir1 break; 59584778508Sblueswir1 case TT_WIN_UNF: /* window underflow */ 59684778508Sblueswir1 restore_window(env); 59784778508Sblueswir1 break; 59884778508Sblueswir1 case TT_TFAULT: 59984778508Sblueswir1 case TT_DFAULT: 60084778508Sblueswir1 #if 0 60184778508Sblueswir1 { 60284778508Sblueswir1 info.si_signo = SIGSEGV; 60384778508Sblueswir1 info.si_errno = 0; 60484778508Sblueswir1 /* XXX: check env->error_code */ 60584778508Sblueswir1 info.si_code = TARGET_SEGV_MAPERR; 60684778508Sblueswir1 info._sifields._sigfault._addr = env->mmuregs[4]; 60784778508Sblueswir1 queue_signal(env, info.si_signo, &info); 60884778508Sblueswir1 } 60984778508Sblueswir1 #endif 61084778508Sblueswir1 break; 61184778508Sblueswir1 #else 61284778508Sblueswir1 case TT_SPILL: /* window overflow */ 61384778508Sblueswir1 save_window(env); 61484778508Sblueswir1 break; 61584778508Sblueswir1 case TT_FILL: /* window underflow */ 61684778508Sblueswir1 restore_window(env); 61784778508Sblueswir1 break; 61884778508Sblueswir1 case TT_TFAULT: 61984778508Sblueswir1 case TT_DFAULT: 62084778508Sblueswir1 #if 0 62184778508Sblueswir1 { 62284778508Sblueswir1 info.si_signo = SIGSEGV; 62384778508Sblueswir1 info.si_errno = 0; 62484778508Sblueswir1 /* XXX: check env->error_code */ 62584778508Sblueswir1 info.si_code = TARGET_SEGV_MAPERR; 62684778508Sblueswir1 if (trapnr == TT_DFAULT) 62784778508Sblueswir1 info._sifields._sigfault._addr = env->dmmuregs[4]; 62884778508Sblueswir1 else 62984778508Sblueswir1 info._sifields._sigfault._addr = env->tsptr->tpc; 63084778508Sblueswir1 //queue_signal(env, info.si_signo, &info); 63184778508Sblueswir1 } 63284778508Sblueswir1 #endif 63384778508Sblueswir1 break; 63484778508Sblueswir1 #endif 63584778508Sblueswir1 case EXCP_INTERRUPT: 63684778508Sblueswir1 /* just indicate that signals should be handled asap */ 63784778508Sblueswir1 break; 63884778508Sblueswir1 case EXCP_DEBUG: 63984778508Sblueswir1 { 64084778508Sblueswir1 int sig; 64184778508Sblueswir1 642db6b81d4SAndreas Färber sig = gdb_handlesig(cs, TARGET_SIGTRAP); 64384778508Sblueswir1 #if 0 64484778508Sblueswir1 if (sig) 64584778508Sblueswir1 { 64684778508Sblueswir1 info.si_signo = sig; 64784778508Sblueswir1 info.si_errno = 0; 64884778508Sblueswir1 info.si_code = TARGET_TRAP_BRKPT; 64984778508Sblueswir1 //queue_signal(env, info.si_signo, &info); 65084778508Sblueswir1 } 65184778508Sblueswir1 #endif 65284778508Sblueswir1 } 65384778508Sblueswir1 break; 65484778508Sblueswir1 default: 65578cfb07fSJuergen Lock #ifdef TARGET_SPARC64 65678cfb07fSJuergen Lock badtrap: 65778cfb07fSJuergen Lock #endif 65884778508Sblueswir1 printf ("Unhandled trap: 0x%x\n", trapnr); 659878096eeSAndreas Färber cpu_dump_state(cs, stderr, fprintf, 0); 66084778508Sblueswir1 exit (1); 66184778508Sblueswir1 } 66284778508Sblueswir1 process_pending_signals (env); 66384778508Sblueswir1 } 66484778508Sblueswir1 } 66584778508Sblueswir1 66684778508Sblueswir1 #endif 66784778508Sblueswir1 66884778508Sblueswir1 static void usage(void) 66984778508Sblueswir1 { 670*d915b7bbSPeter Maydell printf("qemu-" TARGET_NAME " version " QEMU_VERSION QEMU_PKGVERSION 671*d915b7bbSPeter Maydell ", " QEMU_COPYRIGHT "\n" 6722e59915dSPaolo Bonzini "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n" 67384778508Sblueswir1 "BSD CPU emulator (compiled for %s emulation)\n" 67484778508Sblueswir1 "\n" 67584778508Sblueswir1 "Standard options:\n" 67684778508Sblueswir1 "-h print this help\n" 67784778508Sblueswir1 "-g port wait gdb connection to port\n" 67884778508Sblueswir1 "-L path set the elf interpreter prefix (default=%s)\n" 67984778508Sblueswir1 "-s size set the stack size in bytes (default=%ld)\n" 680c8057f95SPeter Maydell "-cpu model select CPU (-cpu help for list)\n" 68184778508Sblueswir1 "-drop-ld-preload drop LD_PRELOAD for target process\n" 682fc0d96b4SBlue Swirl "-E var=value sets/modifies targets environment variable(s)\n" 683fc0d96b4SBlue Swirl "-U var unsets targets environment variable(s)\n" 6842fa5d9baSBlue Swirl "-B address set guest_base address to address\n" 68584778508Sblueswir1 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n" 68684778508Sblueswir1 "\n" 68784778508Sblueswir1 "Debug options:\n" 688989b697dSPeter Maydell "-d item1[,...] enable logging of specified items\n" 689989b697dSPeter Maydell " (use '-d help' for a list of log items)\n" 690989b697dSPeter Maydell "-D logfile write logs to 'logfile' (default stderr)\n" 69184778508Sblueswir1 "-p pagesize set the host page size to 'pagesize'\n" 6921b530a6dSaurel32 "-singlestep always run in singlestep mode\n" 69384778508Sblueswir1 "-strace log system calls\n" 6946913e79cSLluís Vilanova "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n" 6956913e79cSLluís Vilanova " specify tracing options\n" 69684778508Sblueswir1 "\n" 69784778508Sblueswir1 "Environment variables:\n" 69884778508Sblueswir1 "QEMU_STRACE Print system calls and arguments similar to the\n" 69984778508Sblueswir1 " 'strace' program. Enable by setting to any value.\n" 700fc0d96b4SBlue Swirl "You can use -E and -U options to set/unset environment variables\n" 701fc0d96b4SBlue Swirl "for target process. It is possible to provide several variables\n" 702fc0d96b4SBlue Swirl "by repeating the option. For example:\n" 703fc0d96b4SBlue Swirl " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n" 704fc0d96b4SBlue Swirl "Note that if you provide several changes to single variable\n" 705fc0d96b4SBlue Swirl "last change will stay in effect.\n" 70684778508Sblueswir1 , 7072e59915dSPaolo Bonzini TARGET_NAME, 70884778508Sblueswir1 interp_prefix, 709989b697dSPeter Maydell x86_stack_size); 7102d18e637Sblueswir1 exit(1); 71184778508Sblueswir1 } 71284778508Sblueswir1 713dca1173cSAndreas Färber THREAD CPUState *thread_cpu; 71484778508Sblueswir1 71584778508Sblueswir1 /* Assumes contents are already zeroed. */ 71684778508Sblueswir1 void init_task_state(TaskState *ts) 71784778508Sblueswir1 { 71884778508Sblueswir1 int i; 71984778508Sblueswir1 72084778508Sblueswir1 ts->used = 1; 72184778508Sblueswir1 ts->first_free = ts->sigqueue_table; 72284778508Sblueswir1 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) { 72384778508Sblueswir1 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1]; 72484778508Sblueswir1 } 72584778508Sblueswir1 ts->sigqueue_table[i].next = NULL; 72684778508Sblueswir1 } 72784778508Sblueswir1 72884778508Sblueswir1 int main(int argc, char **argv) 72984778508Sblueswir1 { 73084778508Sblueswir1 const char *filename; 73184778508Sblueswir1 const char *cpu_model; 732989b697dSPeter Maydell const char *log_file = NULL; 733c235d738SMatthew Fernandez const char *log_mask = NULL; 73484778508Sblueswir1 struct target_pt_regs regs1, *regs = ®s1; 73584778508Sblueswir1 struct image_info info1, *info = &info1; 73684778508Sblueswir1 TaskState ts1, *ts = &ts1; 7379349b4f9SAndreas Färber CPUArchState *env; 738db6b81d4SAndreas Färber CPUState *cpu; 73984778508Sblueswir1 int optind; 74084778508Sblueswir1 const char *r; 74184778508Sblueswir1 int gdbstub_port = 0; 742fc0d96b4SBlue Swirl char **target_environ, **wrk; 743fc0d96b4SBlue Swirl envlist_t *envlist = NULL; 7446913e79cSLluís Vilanova char *trace_file = NULL; 74578cfb07fSJuergen Lock bsd_type = target_openbsd; 74684778508Sblueswir1 74784778508Sblueswir1 if (argc <= 1) 74884778508Sblueswir1 usage(); 74984778508Sblueswir1 750ce008c1fSAndreas Färber module_call_init(MODULE_INIT_QOM); 751ce008c1fSAndreas Färber 752fc0d96b4SBlue Swirl if ((envlist = envlist_create()) == NULL) { 753fc0d96b4SBlue Swirl (void) fprintf(stderr, "Unable to allocate envlist\n"); 754fc0d96b4SBlue Swirl exit(1); 755fc0d96b4SBlue Swirl } 756fc0d96b4SBlue Swirl 757fc0d96b4SBlue Swirl /* add current environment into the list */ 758fc0d96b4SBlue Swirl for (wrk = environ; *wrk != NULL; wrk++) { 759fc0d96b4SBlue Swirl (void) envlist_setenv(envlist, *wrk); 760fc0d96b4SBlue Swirl } 761fc0d96b4SBlue Swirl 76284778508Sblueswir1 cpu_model = NULL; 7630c62de2fSJuergen Lock 7646913e79cSLluís Vilanova qemu_add_opts(&qemu_trace_opts); 7656913e79cSLluís Vilanova 76684778508Sblueswir1 optind = 1; 76784778508Sblueswir1 for (;;) { 76884778508Sblueswir1 if (optind >= argc) 76984778508Sblueswir1 break; 77084778508Sblueswir1 r = argv[optind]; 77184778508Sblueswir1 if (r[0] != '-') 77284778508Sblueswir1 break; 77384778508Sblueswir1 optind++; 77484778508Sblueswir1 r++; 77584778508Sblueswir1 if (!strcmp(r, "-")) { 77684778508Sblueswir1 break; 77784778508Sblueswir1 } else if (!strcmp(r, "d")) { 778c235d738SMatthew Fernandez if (optind >= argc) { 77984778508Sblueswir1 break; 78084778508Sblueswir1 } 781c235d738SMatthew Fernandez log_mask = argv[optind++]; 782c235d738SMatthew Fernandez } else if (!strcmp(r, "D")) { 783c235d738SMatthew Fernandez if (optind >= argc) { 784c235d738SMatthew Fernandez break; 78584778508Sblueswir1 } 786c235d738SMatthew Fernandez log_file = argv[optind++]; 787fc0d96b4SBlue Swirl } else if (!strcmp(r, "E")) { 788fc0d96b4SBlue Swirl r = argv[optind++]; 789fc0d96b4SBlue Swirl if (envlist_setenv(envlist, r) != 0) 790fc0d96b4SBlue Swirl usage(); 791f66724c9SStefan Weil } else if (!strcmp(r, "ignore-environment")) { 792f66724c9SStefan Weil envlist_free(envlist); 793f66724c9SStefan Weil if ((envlist = envlist_create()) == NULL) { 794f66724c9SStefan Weil (void) fprintf(stderr, "Unable to allocate envlist\n"); 795f66724c9SStefan Weil exit(1); 796f66724c9SStefan Weil } 797fc0d96b4SBlue Swirl } else if (!strcmp(r, "U")) { 798fc0d96b4SBlue Swirl r = argv[optind++]; 799fc0d96b4SBlue Swirl if (envlist_unsetenv(envlist, r) != 0) 800fc0d96b4SBlue Swirl usage(); 80184778508Sblueswir1 } else if (!strcmp(r, "s")) { 80284778508Sblueswir1 r = argv[optind++]; 80384778508Sblueswir1 x86_stack_size = strtol(r, (char **)&r, 0); 80484778508Sblueswir1 if (x86_stack_size <= 0) 80584778508Sblueswir1 usage(); 80684778508Sblueswir1 if (*r == 'M') 80784778508Sblueswir1 x86_stack_size *= 1024 * 1024; 80884778508Sblueswir1 else if (*r == 'k' || *r == 'K') 80984778508Sblueswir1 x86_stack_size *= 1024; 81084778508Sblueswir1 } else if (!strcmp(r, "L")) { 81184778508Sblueswir1 interp_prefix = argv[optind++]; 81284778508Sblueswir1 } else if (!strcmp(r, "p")) { 81384778508Sblueswir1 qemu_host_page_size = atoi(argv[optind++]); 81484778508Sblueswir1 if (qemu_host_page_size == 0 || 81584778508Sblueswir1 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) { 81684778508Sblueswir1 fprintf(stderr, "page size must be a power of two\n"); 81784778508Sblueswir1 exit(1); 81884778508Sblueswir1 } 81984778508Sblueswir1 } else if (!strcmp(r, "g")) { 82084778508Sblueswir1 gdbstub_port = atoi(argv[optind++]); 82184778508Sblueswir1 } else if (!strcmp(r, "r")) { 82284778508Sblueswir1 qemu_uname_release = argv[optind++]; 82384778508Sblueswir1 } else if (!strcmp(r, "cpu")) { 82484778508Sblueswir1 cpu_model = argv[optind++]; 825c8057f95SPeter Maydell if (is_help_option(cpu_model)) { 82684778508Sblueswir1 /* XXX: implement xxx_cpu_list for targets that still miss it */ 82784778508Sblueswir1 #if defined(cpu_list) 82884778508Sblueswir1 cpu_list(stdout, &fprintf); 82984778508Sblueswir1 #endif 8302d18e637Sblueswir1 exit(1); 83184778508Sblueswir1 } 8322fa5d9baSBlue Swirl } else if (!strcmp(r, "B")) { 8332fa5d9baSBlue Swirl guest_base = strtol(argv[optind++], NULL, 0); 8342fa5d9baSBlue Swirl have_guest_base = 1; 83584778508Sblueswir1 } else if (!strcmp(r, "drop-ld-preload")) { 836fc0d96b4SBlue Swirl (void) envlist_unsetenv(envlist, "LD_PRELOAD"); 83784778508Sblueswir1 } else if (!strcmp(r, "bsd")) { 83884778508Sblueswir1 if (!strcasecmp(argv[optind], "freebsd")) { 83984778508Sblueswir1 bsd_type = target_freebsd; 84084778508Sblueswir1 } else if (!strcasecmp(argv[optind], "netbsd")) { 84184778508Sblueswir1 bsd_type = target_netbsd; 84284778508Sblueswir1 } else if (!strcasecmp(argv[optind], "openbsd")) { 84384778508Sblueswir1 bsd_type = target_openbsd; 84484778508Sblueswir1 } else { 84584778508Sblueswir1 usage(); 84684778508Sblueswir1 } 84784778508Sblueswir1 optind++; 8481b530a6dSaurel32 } else if (!strcmp(r, "singlestep")) { 8491b530a6dSaurel32 singlestep = 1; 85084778508Sblueswir1 } else if (!strcmp(r, "strace")) { 85184778508Sblueswir1 do_strace = 1; 8526913e79cSLluís Vilanova } else if (!strcmp(r, "trace")) { 8536913e79cSLluís Vilanova g_free(trace_file); 8546913e79cSLluís Vilanova trace_file = trace_opt_parse(optarg); 8556913e79cSLluís Vilanova } else { 85684778508Sblueswir1 usage(); 85784778508Sblueswir1 } 85884778508Sblueswir1 } 85984778508Sblueswir1 860c235d738SMatthew Fernandez /* init debug */ 861f2937a33SPaolo Bonzini qemu_log_needs_buffers(); 862daa76aa4SMarkus Armbruster qemu_set_log_filename(log_file, &error_fatal); 863c235d738SMatthew Fernandez if (log_mask) { 864c235d738SMatthew Fernandez int mask; 865c235d738SMatthew Fernandez 8664fde1ebaSPeter Maydell mask = qemu_str_to_log_mask(log_mask); 867c235d738SMatthew Fernandez if (!mask) { 86859a6fa6eSPeter Maydell qemu_print_log_usage(stdout); 869c235d738SMatthew Fernandez exit(1); 870c235d738SMatthew Fernandez } 87124537a01SPeter Maydell qemu_set_log(mask); 872c235d738SMatthew Fernandez } 873c235d738SMatthew Fernandez 8744b5dfd82SPeter Maydell if (optind >= argc) { 8754b5dfd82SPeter Maydell usage(); 8764b5dfd82SPeter Maydell } 8774b5dfd82SPeter Maydell filename = argv[optind]; 8784b5dfd82SPeter Maydell 8796913e79cSLluís Vilanova if (!trace_init_backends()) { 8806913e79cSLluís Vilanova exit(1); 8816913e79cSLluís Vilanova } 8826913e79cSLluís Vilanova trace_init_file(trace_file); 8836913e79cSLluís Vilanova 88484778508Sblueswir1 /* Zero out regs */ 88584778508Sblueswir1 memset(regs, 0, sizeof(struct target_pt_regs)); 88684778508Sblueswir1 88784778508Sblueswir1 /* Zero out image_info */ 88884778508Sblueswir1 memset(info, 0, sizeof(struct image_info)); 88984778508Sblueswir1 89084778508Sblueswir1 /* Scan interp_prefix dir for replacement files. */ 89184778508Sblueswir1 init_paths(interp_prefix); 89284778508Sblueswir1 89384778508Sblueswir1 if (cpu_model == NULL) { 89431fc12dfSblueswir1 #if defined(TARGET_I386) 89531fc12dfSblueswir1 #ifdef TARGET_X86_64 89631fc12dfSblueswir1 cpu_model = "qemu64"; 89731fc12dfSblueswir1 #else 89831fc12dfSblueswir1 cpu_model = "qemu32"; 89931fc12dfSblueswir1 #endif 90031fc12dfSblueswir1 #elif defined(TARGET_SPARC) 90184778508Sblueswir1 #ifdef TARGET_SPARC64 90284778508Sblueswir1 cpu_model = "TI UltraSparc II"; 90384778508Sblueswir1 #else 90484778508Sblueswir1 cpu_model = "Fujitsu MB86904"; 90584778508Sblueswir1 #endif 90684778508Sblueswir1 #else 90784778508Sblueswir1 cpu_model = "any"; 90884778508Sblueswir1 #endif 90984778508Sblueswir1 } 910d5ab9713SJan Kiszka tcg_exec_init(0); 91184778508Sblueswir1 /* NOTE: we need to init the CPU at this stage to get 91284778508Sblueswir1 qemu_host_page_size */ 9132994fd96SEduardo Habkost cpu = cpu_init(cpu_model); 9142994fd96SEduardo Habkost if (!cpu) { 91584778508Sblueswir1 fprintf(stderr, "Unable to find CPU definition\n"); 91684778508Sblueswir1 exit(1); 91784778508Sblueswir1 } 9182994fd96SEduardo Habkost env = cpu->env_ptr; 91977868120SAndreas Färber #if defined(TARGET_SPARC) || defined(TARGET_PPC) 920db6b81d4SAndreas Färber cpu_reset(cpu); 921b55a37c9SBlue Swirl #endif 922db6b81d4SAndreas Färber thread_cpu = cpu; 92384778508Sblueswir1 92484778508Sblueswir1 if (getenv("QEMU_STRACE")) { 92584778508Sblueswir1 do_strace = 1; 92684778508Sblueswir1 } 92784778508Sblueswir1 928fc0d96b4SBlue Swirl target_environ = envlist_to_environ(envlist, NULL); 929fc0d96b4SBlue Swirl envlist_free(envlist); 93084778508Sblueswir1 9312fa5d9baSBlue Swirl /* 9322fa5d9baSBlue Swirl * Now that page sizes are configured in cpu_init() we can do 9332fa5d9baSBlue Swirl * proper page alignment for guest_base. 9342fa5d9baSBlue Swirl */ 9352fa5d9baSBlue Swirl guest_base = HOST_PAGE_ALIGN(guest_base); 9362fa5d9baSBlue Swirl 9372fa5d9baSBlue Swirl /* 9382fa5d9baSBlue Swirl * Read in mmap_min_addr kernel parameter. This value is used 9392fa5d9baSBlue Swirl * When loading the ELF image to determine whether guest_base 9402fa5d9baSBlue Swirl * is needed. 9412fa5d9baSBlue Swirl * 9422fa5d9baSBlue Swirl * When user has explicitly set the quest base, we skip this 9432fa5d9baSBlue Swirl * test. 9442fa5d9baSBlue Swirl */ 9452fa5d9baSBlue Swirl if (!have_guest_base) { 9462fa5d9baSBlue Swirl FILE *fp; 9472fa5d9baSBlue Swirl 9482fa5d9baSBlue Swirl if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) { 9492fa5d9baSBlue Swirl unsigned long tmp; 9502fa5d9baSBlue Swirl if (fscanf(fp, "%lu", &tmp) == 1) { 9512fa5d9baSBlue Swirl mmap_min_addr = tmp; 95213829020SPaolo Bonzini qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n", mmap_min_addr); 9532fa5d9baSBlue Swirl } 9542fa5d9baSBlue Swirl fclose(fp); 9552fa5d9baSBlue Swirl } 9562fa5d9baSBlue Swirl } 95784778508Sblueswir1 95884778508Sblueswir1 if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) { 95984778508Sblueswir1 printf("Error loading %s\n", filename); 96084778508Sblueswir1 _exit(1); 96184778508Sblueswir1 } 96284778508Sblueswir1 96384778508Sblueswir1 for (wrk = target_environ; *wrk; wrk++) { 96484778508Sblueswir1 free(*wrk); 96584778508Sblueswir1 } 96684778508Sblueswir1 96784778508Sblueswir1 free(target_environ); 96884778508Sblueswir1 96913829020SPaolo Bonzini if (qemu_loglevel_mask(CPU_LOG_PAGE)) { 9702fa5d9baSBlue Swirl qemu_log("guest_base 0x%lx\n", guest_base); 97193fcfe39Saliguori log_page_dump(); 97284778508Sblueswir1 97393fcfe39Saliguori qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk); 97493fcfe39Saliguori qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code); 97593fcfe39Saliguori qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n", 97684778508Sblueswir1 info->start_code); 97793fcfe39Saliguori qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n", 97884778508Sblueswir1 info->start_data); 97993fcfe39Saliguori qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data); 98093fcfe39Saliguori qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n", 98184778508Sblueswir1 info->start_stack); 98293fcfe39Saliguori qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk); 98393fcfe39Saliguori qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry); 9842e77eac6Sblueswir1 } 98584778508Sblueswir1 98684778508Sblueswir1 target_set_brk(info->brk); 98784778508Sblueswir1 syscall_init(); 98884778508Sblueswir1 signal_init(); 98984778508Sblueswir1 9909002ec79SRichard Henderson /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay 9919002ec79SRichard Henderson generating the prologue until now so that the prologue can take 9929002ec79SRichard Henderson the real value of GUEST_BASE into account. */ 9939002ec79SRichard Henderson tcg_prologue_init(&tcg_ctx); 9949002ec79SRichard Henderson 99584778508Sblueswir1 /* build Task State */ 99684778508Sblueswir1 memset(ts, 0, sizeof(TaskState)); 99784778508Sblueswir1 init_task_state(ts); 99884778508Sblueswir1 ts->info = info; 9990429a971SAndreas Färber cpu->opaque = ts; 100084778508Sblueswir1 100131fc12dfSblueswir1 #if defined(TARGET_I386) 100231fc12dfSblueswir1 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK; 1003b98dbc90SPaolo Bonzini env->hflags |= HF_PE_MASK | HF_CPL_MASK; 10040514ef2fSEduardo Habkost if (env->features[FEAT_1_EDX] & CPUID_SSE) { 100531fc12dfSblueswir1 env->cr[4] |= CR4_OSFXSR_MASK; 100631fc12dfSblueswir1 env->hflags |= HF_OSFXSR_MASK; 100731fc12dfSblueswir1 } 100831fc12dfSblueswir1 #ifndef TARGET_ABI32 100931fc12dfSblueswir1 /* enable 64 bit mode if possible */ 10100514ef2fSEduardo Habkost if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) { 101131fc12dfSblueswir1 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n"); 101231fc12dfSblueswir1 exit(1); 101331fc12dfSblueswir1 } 101431fc12dfSblueswir1 env->cr[4] |= CR4_PAE_MASK; 101531fc12dfSblueswir1 env->efer |= MSR_EFER_LMA | MSR_EFER_LME; 101631fc12dfSblueswir1 env->hflags |= HF_LMA_MASK; 101731fc12dfSblueswir1 #endif 101831fc12dfSblueswir1 101931fc12dfSblueswir1 /* flags setup : we activate the IRQs by default as in user mode */ 102031fc12dfSblueswir1 env->eflags |= IF_MASK; 102131fc12dfSblueswir1 102231fc12dfSblueswir1 /* linux register setup */ 102331fc12dfSblueswir1 #ifndef TARGET_ABI32 102431fc12dfSblueswir1 env->regs[R_EAX] = regs->rax; 102531fc12dfSblueswir1 env->regs[R_EBX] = regs->rbx; 102631fc12dfSblueswir1 env->regs[R_ECX] = regs->rcx; 102731fc12dfSblueswir1 env->regs[R_EDX] = regs->rdx; 102831fc12dfSblueswir1 env->regs[R_ESI] = regs->rsi; 102931fc12dfSblueswir1 env->regs[R_EDI] = regs->rdi; 103031fc12dfSblueswir1 env->regs[R_EBP] = regs->rbp; 103131fc12dfSblueswir1 env->regs[R_ESP] = regs->rsp; 103231fc12dfSblueswir1 env->eip = regs->rip; 103331fc12dfSblueswir1 #else 103431fc12dfSblueswir1 env->regs[R_EAX] = regs->eax; 103531fc12dfSblueswir1 env->regs[R_EBX] = regs->ebx; 103631fc12dfSblueswir1 env->regs[R_ECX] = regs->ecx; 103731fc12dfSblueswir1 env->regs[R_EDX] = regs->edx; 103831fc12dfSblueswir1 env->regs[R_ESI] = regs->esi; 103931fc12dfSblueswir1 env->regs[R_EDI] = regs->edi; 104031fc12dfSblueswir1 env->regs[R_EBP] = regs->ebp; 104131fc12dfSblueswir1 env->regs[R_ESP] = regs->esp; 104231fc12dfSblueswir1 env->eip = regs->eip; 104331fc12dfSblueswir1 #endif 104431fc12dfSblueswir1 104531fc12dfSblueswir1 /* linux interrupt setup */ 104631fc12dfSblueswir1 #ifndef TARGET_ABI32 104731fc12dfSblueswir1 env->idt.limit = 511; 104831fc12dfSblueswir1 #else 104931fc12dfSblueswir1 env->idt.limit = 255; 105031fc12dfSblueswir1 #endif 105131fc12dfSblueswir1 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1), 105231fc12dfSblueswir1 PROT_READ|PROT_WRITE, 105331fc12dfSblueswir1 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); 105431fc12dfSblueswir1 idt_table = g2h(env->idt.base); 105531fc12dfSblueswir1 set_idt(0, 0); 105631fc12dfSblueswir1 set_idt(1, 0); 105731fc12dfSblueswir1 set_idt(2, 0); 105831fc12dfSblueswir1 set_idt(3, 3); 105931fc12dfSblueswir1 set_idt(4, 3); 106031fc12dfSblueswir1 set_idt(5, 0); 106131fc12dfSblueswir1 set_idt(6, 0); 106231fc12dfSblueswir1 set_idt(7, 0); 106331fc12dfSblueswir1 set_idt(8, 0); 106431fc12dfSblueswir1 set_idt(9, 0); 106531fc12dfSblueswir1 set_idt(10, 0); 106631fc12dfSblueswir1 set_idt(11, 0); 106731fc12dfSblueswir1 set_idt(12, 0); 106831fc12dfSblueswir1 set_idt(13, 0); 106931fc12dfSblueswir1 set_idt(14, 0); 107031fc12dfSblueswir1 set_idt(15, 0); 107131fc12dfSblueswir1 set_idt(16, 0); 107231fc12dfSblueswir1 set_idt(17, 0); 107331fc12dfSblueswir1 set_idt(18, 0); 107431fc12dfSblueswir1 set_idt(19, 0); 107531fc12dfSblueswir1 set_idt(0x80, 3); 107631fc12dfSblueswir1 107731fc12dfSblueswir1 /* linux segment setup */ 107831fc12dfSblueswir1 { 107931fc12dfSblueswir1 uint64_t *gdt_table; 108031fc12dfSblueswir1 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES, 108131fc12dfSblueswir1 PROT_READ|PROT_WRITE, 108231fc12dfSblueswir1 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); 108331fc12dfSblueswir1 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1; 108431fc12dfSblueswir1 gdt_table = g2h(env->gdt.base); 108531fc12dfSblueswir1 #ifdef TARGET_ABI32 108631fc12dfSblueswir1 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff, 108731fc12dfSblueswir1 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 108831fc12dfSblueswir1 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT)); 108931fc12dfSblueswir1 #else 109031fc12dfSblueswir1 /* 64 bit code segment */ 109131fc12dfSblueswir1 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff, 109231fc12dfSblueswir1 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 109331fc12dfSblueswir1 DESC_L_MASK | 109431fc12dfSblueswir1 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT)); 109531fc12dfSblueswir1 #endif 109631fc12dfSblueswir1 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff, 109731fc12dfSblueswir1 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 109831fc12dfSblueswir1 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT)); 109931fc12dfSblueswir1 } 110031fc12dfSblueswir1 110131fc12dfSblueswir1 cpu_x86_load_seg(env, R_CS, __USER_CS); 110231fc12dfSblueswir1 cpu_x86_load_seg(env, R_SS, __USER_DS); 110331fc12dfSblueswir1 #ifdef TARGET_ABI32 110431fc12dfSblueswir1 cpu_x86_load_seg(env, R_DS, __USER_DS); 110531fc12dfSblueswir1 cpu_x86_load_seg(env, R_ES, __USER_DS); 110631fc12dfSblueswir1 cpu_x86_load_seg(env, R_FS, __USER_DS); 110731fc12dfSblueswir1 cpu_x86_load_seg(env, R_GS, __USER_DS); 110831fc12dfSblueswir1 /* This hack makes Wine work... */ 110931fc12dfSblueswir1 env->segs[R_FS].selector = 0; 111031fc12dfSblueswir1 #else 111131fc12dfSblueswir1 cpu_x86_load_seg(env, R_DS, 0); 111231fc12dfSblueswir1 cpu_x86_load_seg(env, R_ES, 0); 111331fc12dfSblueswir1 cpu_x86_load_seg(env, R_FS, 0); 111431fc12dfSblueswir1 cpu_x86_load_seg(env, R_GS, 0); 111531fc12dfSblueswir1 #endif 111631fc12dfSblueswir1 #elif defined(TARGET_SPARC) 111784778508Sblueswir1 { 111884778508Sblueswir1 int i; 111984778508Sblueswir1 env->pc = regs->pc; 112084778508Sblueswir1 env->npc = regs->npc; 112184778508Sblueswir1 env->y = regs->y; 112284778508Sblueswir1 for(i = 0; i < 8; i++) 112384778508Sblueswir1 env->gregs[i] = regs->u_regs[i]; 112484778508Sblueswir1 for(i = 0; i < 8; i++) 112584778508Sblueswir1 env->regwptr[i] = regs->u_regs[i + 8]; 112684778508Sblueswir1 } 112784778508Sblueswir1 #else 112884778508Sblueswir1 #error unsupported target CPU 112984778508Sblueswir1 #endif 113084778508Sblueswir1 113184778508Sblueswir1 if (gdbstub_port) { 113284778508Sblueswir1 gdbserver_start (gdbstub_port); 1133db6b81d4SAndreas Färber gdb_handlesig(cpu, 0); 113484778508Sblueswir1 } 113548151859SLluís Vilanova trace_init_vcpu_events(); 113678cfb07fSJuergen Lock cpu_loop(env); 113784778508Sblueswir1 /* never exits */ 113884778508Sblueswir1 return 0; 113984778508Sblueswir1 } 1140