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 */ 1914a48c1dSMarkus Armbruster 202231197cSPeter Maydell #include "qemu/osdep.h" 21a8d25326SMarkus Armbruster #include "qemu-common.h" 2266d26ddbSPhilippe Mathieu-Daudé #include "qemu/units.h" 23940e43aaSClaudio Fontana #include "qemu/accel.h" 2414a48c1dSMarkus Armbruster #include "sysemu/tcg.h" 258c1c230aSEd Maste #include "qemu-version.h" 2684778508Sblueswir1 #include <machine/trap.h> 2784778508Sblueswir1 28daa76aa4SMarkus Armbruster #include "qapi/error.h" 2984778508Sblueswir1 #include "qemu.h" 306913e79cSLluís Vilanova #include "qemu/config-file.h" 31f5852efaSChristophe Fergeau #include "qemu/error-report.h" 32f348b6d1SVeronia Bahaa #include "qemu/path.h" 33f348b6d1SVeronia Bahaa #include "qemu/help_option.h" 340b8fa32fSMarkus Armbruster #include "qemu/module.h" 352b41f10eSBlue Swirl #include "cpu.h" 3663c91552SPaolo Bonzini #include "exec/exec-all.h" 37dcb32f1dSPhilippe Mathieu-Daudé #include "tcg/tcg.h" 381de7afc9SPaolo Bonzini #include "qemu/timer.h" 391de7afc9SPaolo Bonzini #include "qemu/envlist.h" 40508127e2SPaolo Bonzini #include "exec/log.h" 416913e79cSLluís Vilanova #include "trace/control.h" 42fc0d96b4SBlue Swirl 431b530a6dSaurel32 int singlestep; 442fa5d9baSBlue Swirl unsigned long mmap_min_addr; 455ca870b9SRichard Henderson uintptr_t guest_base; 46e307c192SRichard Henderson bool have_guest_base; 47d6ef40bfSPeter Maydell unsigned long reserved_va; 481b530a6dSaurel32 497ee2822cSPaolo Bonzini static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX; 50fccae322SPeter Maydell const char *qemu_uname_release; 5184778508Sblueswir1 extern char **environ; 5278cfb07fSJuergen Lock enum BSDType bsd_type; 5384778508Sblueswir1 5484778508Sblueswir1 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so 5584778508Sblueswir1 we allocate a bigger stack. Need a better solution, for example 5684778508Sblueswir1 by remapping the process stack directly at the right place */ 5784778508Sblueswir1 unsigned long x86_stack_size = 512 * 1024; 5884778508Sblueswir1 5984778508Sblueswir1 void gemu_log(const char *fmt, ...) 6084778508Sblueswir1 { 6184778508Sblueswir1 va_list ap; 6284778508Sblueswir1 6384778508Sblueswir1 va_start(ap, fmt); 6484778508Sblueswir1 vfprintf(stderr, fmt, ap); 6584778508Sblueswir1 va_end(ap); 6684778508Sblueswir1 } 679399f095Sblueswir1 6831fc12dfSblueswir1 #if defined(TARGET_I386) 69b98e9ca8SAndreas Färber int cpu_get_pic_interrupt(CPUX86State *env) 7031fc12dfSblueswir1 { 7131fc12dfSblueswir1 return -1; 7231fc12dfSblueswir1 } 7331fc12dfSblueswir1 #endif 7431fc12dfSblueswir1 759399f095Sblueswir1 void fork_start(void) 769399f095Sblueswir1 { 779399f095Sblueswir1 } 789399f095Sblueswir1 799399f095Sblueswir1 void fork_end(int child) 809399f095Sblueswir1 { 819399f095Sblueswir1 if (child) { 82f7ec7f7bSPeter Crosthwaite gdbserver_fork(thread_cpu); 839399f095Sblueswir1 } 849399f095Sblueswir1 } 859399f095Sblueswir1 8631fc12dfSblueswir1 #ifdef TARGET_I386 8731fc12dfSblueswir1 /***********************************************************/ 8831fc12dfSblueswir1 /* CPUX86 core interface */ 8931fc12dfSblueswir1 9031fc12dfSblueswir1 uint64_t cpu_get_tsc(CPUX86State *env) 9131fc12dfSblueswir1 { 924a7428c5SChristopher Covington return cpu_get_host_ticks(); 9331fc12dfSblueswir1 } 9431fc12dfSblueswir1 9531fc12dfSblueswir1 static void write_dt(void *ptr, unsigned long addr, unsigned long limit, 9631fc12dfSblueswir1 int flags) 9731fc12dfSblueswir1 { 9831fc12dfSblueswir1 unsigned int e1, e2; 9931fc12dfSblueswir1 uint32_t *p; 10031fc12dfSblueswir1 e1 = (addr << 16) | (limit & 0xffff); 10131fc12dfSblueswir1 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000); 10231fc12dfSblueswir1 e2 |= flags; 10331fc12dfSblueswir1 p = ptr; 10431fc12dfSblueswir1 p[0] = tswap32(e1); 10531fc12dfSblueswir1 p[1] = tswap32(e2); 10631fc12dfSblueswir1 } 10731fc12dfSblueswir1 10831fc12dfSblueswir1 static uint64_t *idt_table; 10931fc12dfSblueswir1 #ifdef TARGET_X86_64 11031fc12dfSblueswir1 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl, 11131fc12dfSblueswir1 uint64_t addr, unsigned int sel) 11231fc12dfSblueswir1 { 11331fc12dfSblueswir1 uint32_t *p, e1, e2; 11431fc12dfSblueswir1 e1 = (addr & 0xffff) | (sel << 16); 11531fc12dfSblueswir1 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); 11631fc12dfSblueswir1 p = ptr; 11731fc12dfSblueswir1 p[0] = tswap32(e1); 11831fc12dfSblueswir1 p[1] = tswap32(e2); 11931fc12dfSblueswir1 p[2] = tswap32(addr >> 32); 12031fc12dfSblueswir1 p[3] = 0; 12131fc12dfSblueswir1 } 12231fc12dfSblueswir1 /* only dpl matters as we do only user space emulation */ 12331fc12dfSblueswir1 static void set_idt(int n, unsigned int dpl) 12431fc12dfSblueswir1 { 12531fc12dfSblueswir1 set_gate64(idt_table + n * 2, 0, dpl, 0, 0); 12631fc12dfSblueswir1 } 12731fc12dfSblueswir1 #else 12831fc12dfSblueswir1 static void set_gate(void *ptr, unsigned int type, unsigned int dpl, 12931fc12dfSblueswir1 uint32_t addr, unsigned int sel) 13031fc12dfSblueswir1 { 13131fc12dfSblueswir1 uint32_t *p, e1, e2; 13231fc12dfSblueswir1 e1 = (addr & 0xffff) | (sel << 16); 13331fc12dfSblueswir1 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); 13431fc12dfSblueswir1 p = ptr; 13531fc12dfSblueswir1 p[0] = tswap32(e1); 13631fc12dfSblueswir1 p[1] = tswap32(e2); 13731fc12dfSblueswir1 } 13831fc12dfSblueswir1 13931fc12dfSblueswir1 /* only dpl matters as we do only user space emulation */ 14031fc12dfSblueswir1 static void set_idt(int n, unsigned int dpl) 14131fc12dfSblueswir1 { 14231fc12dfSblueswir1 set_gate(idt_table + n, 0, dpl, 0, 0); 14331fc12dfSblueswir1 } 14431fc12dfSblueswir1 #endif 14531fc12dfSblueswir1 14678cfb07fSJuergen Lock void cpu_loop(CPUX86State *env) 14731fc12dfSblueswir1 { 1486aa9e42fSRichard Henderson CPUState *cs = env_cpu(env); 14931fc12dfSblueswir1 int trapnr; 15031fc12dfSblueswir1 abi_ulong pc; 15131fc12dfSblueswir1 //target_siginfo_t info; 15231fc12dfSblueswir1 15331fc12dfSblueswir1 for(;;) { 154d148d90eSSergey Fedorov cpu_exec_start(cs); 155ded554cdSSean Bruno trapnr = cpu_exec(cs); 156d148d90eSSergey Fedorov cpu_exec_end(cs); 157d148d90eSSergey Fedorov process_queued_cpu_work(cs); 158d148d90eSSergey Fedorov 15931fc12dfSblueswir1 switch(trapnr) { 16031fc12dfSblueswir1 case 0x80: 16131fc12dfSblueswir1 /* syscall from int $0x80 */ 16278cfb07fSJuergen Lock if (bsd_type == target_freebsd) { 16378cfb07fSJuergen Lock abi_ulong params = (abi_ulong) env->regs[R_ESP] + 16478cfb07fSJuergen Lock sizeof(int32_t); 16578cfb07fSJuergen Lock int32_t syscall_nr = env->regs[R_EAX]; 16678cfb07fSJuergen Lock int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8; 16778cfb07fSJuergen Lock 16878cfb07fSJuergen Lock if (syscall_nr == TARGET_FREEBSD_NR_syscall) { 16978cfb07fSJuergen Lock get_user_s32(syscall_nr, params); 17078cfb07fSJuergen Lock params += sizeof(int32_t); 17178cfb07fSJuergen Lock } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) { 17278cfb07fSJuergen Lock get_user_s32(syscall_nr, params); 17378cfb07fSJuergen Lock params += sizeof(int64_t); 17478cfb07fSJuergen Lock } 17578cfb07fSJuergen Lock get_user_s32(arg1, params); 17678cfb07fSJuergen Lock params += sizeof(int32_t); 17778cfb07fSJuergen Lock get_user_s32(arg2, params); 17878cfb07fSJuergen Lock params += sizeof(int32_t); 17978cfb07fSJuergen Lock get_user_s32(arg3, params); 18078cfb07fSJuergen Lock params += sizeof(int32_t); 18178cfb07fSJuergen Lock get_user_s32(arg4, params); 18278cfb07fSJuergen Lock params += sizeof(int32_t); 18378cfb07fSJuergen Lock get_user_s32(arg5, params); 18478cfb07fSJuergen Lock params += sizeof(int32_t); 18578cfb07fSJuergen Lock get_user_s32(arg6, params); 18678cfb07fSJuergen Lock params += sizeof(int32_t); 18778cfb07fSJuergen Lock get_user_s32(arg7, params); 18878cfb07fSJuergen Lock params += sizeof(int32_t); 18978cfb07fSJuergen Lock get_user_s32(arg8, params); 19078cfb07fSJuergen Lock env->regs[R_EAX] = do_freebsd_syscall(env, 19178cfb07fSJuergen Lock syscall_nr, 19278cfb07fSJuergen Lock arg1, 19378cfb07fSJuergen Lock arg2, 19478cfb07fSJuergen Lock arg3, 19578cfb07fSJuergen Lock arg4, 19678cfb07fSJuergen Lock arg5, 19778cfb07fSJuergen Lock arg6, 19878cfb07fSJuergen Lock arg7, 19978cfb07fSJuergen Lock arg8); 20078cfb07fSJuergen Lock } else { //if (bsd_type == target_openbsd) 20131fc12dfSblueswir1 env->regs[R_EAX] = do_openbsd_syscall(env, 20231fc12dfSblueswir1 env->regs[R_EAX], 20331fc12dfSblueswir1 env->regs[R_EBX], 20431fc12dfSblueswir1 env->regs[R_ECX], 20531fc12dfSblueswir1 env->regs[R_EDX], 20631fc12dfSblueswir1 env->regs[R_ESI], 20731fc12dfSblueswir1 env->regs[R_EDI], 20831fc12dfSblueswir1 env->regs[R_EBP]); 20978cfb07fSJuergen Lock } 21078cfb07fSJuergen Lock if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) { 21178cfb07fSJuergen Lock env->regs[R_EAX] = -env->regs[R_EAX]; 21278cfb07fSJuergen Lock env->eflags |= CC_C; 21378cfb07fSJuergen Lock } else { 21478cfb07fSJuergen Lock env->eflags &= ~CC_C; 21578cfb07fSJuergen Lock } 21631fc12dfSblueswir1 break; 21731fc12dfSblueswir1 #ifndef TARGET_ABI32 21831fc12dfSblueswir1 case EXCP_SYSCALL: 2195ba18547SStefan Weil /* syscall from syscall instruction */ 22078cfb07fSJuergen Lock if (bsd_type == target_freebsd) 22178cfb07fSJuergen Lock env->regs[R_EAX] = do_freebsd_syscall(env, 22278cfb07fSJuergen Lock env->regs[R_EAX], 22378cfb07fSJuergen Lock env->regs[R_EDI], 22478cfb07fSJuergen Lock env->regs[R_ESI], 22578cfb07fSJuergen Lock env->regs[R_EDX], 22678cfb07fSJuergen Lock env->regs[R_ECX], 22778cfb07fSJuergen Lock env->regs[8], 22878cfb07fSJuergen Lock env->regs[9], 0, 0); 22978cfb07fSJuergen Lock else { //if (bsd_type == target_openbsd) 23031fc12dfSblueswir1 env->regs[R_EAX] = do_openbsd_syscall(env, 23131fc12dfSblueswir1 env->regs[R_EAX], 23231fc12dfSblueswir1 env->regs[R_EDI], 23331fc12dfSblueswir1 env->regs[R_ESI], 23431fc12dfSblueswir1 env->regs[R_EDX], 23531fc12dfSblueswir1 env->regs[10], 23631fc12dfSblueswir1 env->regs[8], 23731fc12dfSblueswir1 env->regs[9]); 23878cfb07fSJuergen Lock } 23931fc12dfSblueswir1 env->eip = env->exception_next_eip; 24078cfb07fSJuergen Lock if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) { 24178cfb07fSJuergen Lock env->regs[R_EAX] = -env->regs[R_EAX]; 24278cfb07fSJuergen Lock env->eflags |= CC_C; 24378cfb07fSJuergen Lock } else { 24478cfb07fSJuergen Lock env->eflags &= ~CC_C; 24578cfb07fSJuergen Lock } 24631fc12dfSblueswir1 break; 24731fc12dfSblueswir1 #endif 24831fc12dfSblueswir1 #if 0 24931fc12dfSblueswir1 case EXCP0B_NOSEG: 25031fc12dfSblueswir1 case EXCP0C_STACK: 25131fc12dfSblueswir1 info.si_signo = SIGBUS; 25231fc12dfSblueswir1 info.si_errno = 0; 25331fc12dfSblueswir1 info.si_code = TARGET_SI_KERNEL; 25431fc12dfSblueswir1 info._sifields._sigfault._addr = 0; 25531fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 25631fc12dfSblueswir1 break; 25731fc12dfSblueswir1 case EXCP0D_GPF: 25831fc12dfSblueswir1 /* XXX: potential problem if ABI32 */ 25931fc12dfSblueswir1 #ifndef TARGET_X86_64 26031fc12dfSblueswir1 if (env->eflags & VM_MASK) { 26131fc12dfSblueswir1 handle_vm86_fault(env); 26231fc12dfSblueswir1 } else 26331fc12dfSblueswir1 #endif 26431fc12dfSblueswir1 { 26531fc12dfSblueswir1 info.si_signo = SIGSEGV; 26631fc12dfSblueswir1 info.si_errno = 0; 26731fc12dfSblueswir1 info.si_code = TARGET_SI_KERNEL; 26831fc12dfSblueswir1 info._sifields._sigfault._addr = 0; 26931fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 27031fc12dfSblueswir1 } 27131fc12dfSblueswir1 break; 27231fc12dfSblueswir1 case EXCP0E_PAGE: 27331fc12dfSblueswir1 info.si_signo = SIGSEGV; 27431fc12dfSblueswir1 info.si_errno = 0; 27531fc12dfSblueswir1 if (!(env->error_code & 1)) 27631fc12dfSblueswir1 info.si_code = TARGET_SEGV_MAPERR; 27731fc12dfSblueswir1 else 27831fc12dfSblueswir1 info.si_code = TARGET_SEGV_ACCERR; 27931fc12dfSblueswir1 info._sifields._sigfault._addr = env->cr[2]; 28031fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 28131fc12dfSblueswir1 break; 28231fc12dfSblueswir1 case EXCP00_DIVZ: 28331fc12dfSblueswir1 #ifndef TARGET_X86_64 28431fc12dfSblueswir1 if (env->eflags & VM_MASK) { 28531fc12dfSblueswir1 handle_vm86_trap(env, trapnr); 28631fc12dfSblueswir1 } else 28731fc12dfSblueswir1 #endif 28831fc12dfSblueswir1 { 28931fc12dfSblueswir1 /* division by zero */ 29031fc12dfSblueswir1 info.si_signo = SIGFPE; 29131fc12dfSblueswir1 info.si_errno = 0; 29231fc12dfSblueswir1 info.si_code = TARGET_FPE_INTDIV; 29331fc12dfSblueswir1 info._sifields._sigfault._addr = env->eip; 29431fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 29531fc12dfSblueswir1 } 29631fc12dfSblueswir1 break; 29731fc12dfSblueswir1 case EXCP01_DB: 29831fc12dfSblueswir1 case EXCP03_INT3: 29931fc12dfSblueswir1 #ifndef TARGET_X86_64 30031fc12dfSblueswir1 if (env->eflags & VM_MASK) { 30131fc12dfSblueswir1 handle_vm86_trap(env, trapnr); 30231fc12dfSblueswir1 } else 30331fc12dfSblueswir1 #endif 30431fc12dfSblueswir1 { 30531fc12dfSblueswir1 info.si_signo = SIGTRAP; 30631fc12dfSblueswir1 info.si_errno = 0; 30731fc12dfSblueswir1 if (trapnr == EXCP01_DB) { 30831fc12dfSblueswir1 info.si_code = TARGET_TRAP_BRKPT; 30931fc12dfSblueswir1 info._sifields._sigfault._addr = env->eip; 31031fc12dfSblueswir1 } else { 31131fc12dfSblueswir1 info.si_code = TARGET_SI_KERNEL; 31231fc12dfSblueswir1 info._sifields._sigfault._addr = 0; 31331fc12dfSblueswir1 } 31431fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 31531fc12dfSblueswir1 } 31631fc12dfSblueswir1 break; 31731fc12dfSblueswir1 case EXCP04_INTO: 31831fc12dfSblueswir1 case EXCP05_BOUND: 31931fc12dfSblueswir1 #ifndef TARGET_X86_64 32031fc12dfSblueswir1 if (env->eflags & VM_MASK) { 32131fc12dfSblueswir1 handle_vm86_trap(env, trapnr); 32231fc12dfSblueswir1 } else 32331fc12dfSblueswir1 #endif 32431fc12dfSblueswir1 { 32531fc12dfSblueswir1 info.si_signo = SIGSEGV; 32631fc12dfSblueswir1 info.si_errno = 0; 32731fc12dfSblueswir1 info.si_code = TARGET_SI_KERNEL; 32831fc12dfSblueswir1 info._sifields._sigfault._addr = 0; 32931fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 33031fc12dfSblueswir1 } 33131fc12dfSblueswir1 break; 33231fc12dfSblueswir1 case EXCP06_ILLOP: 33331fc12dfSblueswir1 info.si_signo = SIGILL; 33431fc12dfSblueswir1 info.si_errno = 0; 33531fc12dfSblueswir1 info.si_code = TARGET_ILL_ILLOPN; 33631fc12dfSblueswir1 info._sifields._sigfault._addr = env->eip; 33731fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 33831fc12dfSblueswir1 break; 33931fc12dfSblueswir1 #endif 34031fc12dfSblueswir1 case EXCP_INTERRUPT: 34131fc12dfSblueswir1 /* just indicate that signals should be handled asap */ 34231fc12dfSblueswir1 break; 34331fc12dfSblueswir1 #if 0 34431fc12dfSblueswir1 case EXCP_DEBUG: 34531fc12dfSblueswir1 { 34631fc12dfSblueswir1 int sig; 34731fc12dfSblueswir1 34831fc12dfSblueswir1 sig = gdb_handlesig (env, TARGET_SIGTRAP); 34931fc12dfSblueswir1 if (sig) 35031fc12dfSblueswir1 { 35131fc12dfSblueswir1 info.si_signo = sig; 35231fc12dfSblueswir1 info.si_errno = 0; 35331fc12dfSblueswir1 info.si_code = TARGET_TRAP_BRKPT; 35431fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 35531fc12dfSblueswir1 } 35631fc12dfSblueswir1 } 35731fc12dfSblueswir1 break; 35831fc12dfSblueswir1 #endif 35931fc12dfSblueswir1 default: 36031fc12dfSblueswir1 pc = env->segs[R_CS].base + env->eip; 36131fc12dfSblueswir1 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n", 36231fc12dfSblueswir1 (long)pc, trapnr); 36331fc12dfSblueswir1 abort(); 36431fc12dfSblueswir1 } 36531fc12dfSblueswir1 process_pending_signals(env); 36631fc12dfSblueswir1 } 36731fc12dfSblueswir1 } 36831fc12dfSblueswir1 #endif 36931fc12dfSblueswir1 37084778508Sblueswir1 #ifdef TARGET_SPARC 37184778508Sblueswir1 #define SPARC64_STACK_BIAS 2047 37284778508Sblueswir1 37384778508Sblueswir1 //#define DEBUG_WIN 37484778508Sblueswir1 /* WARNING: dealing with register windows _is_ complicated. More info 37584778508Sblueswir1 can be found at http://www.sics.se/~psm/sparcstack.html */ 37684778508Sblueswir1 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index) 37784778508Sblueswir1 { 37884778508Sblueswir1 index = (index + cwp * 16) % (16 * env->nwindows); 37984778508Sblueswir1 /* wrap handling : if cwp is on the last window, then we use the 38084778508Sblueswir1 registers 'after' the end */ 38184778508Sblueswir1 if (index < 8 && env->cwp == env->nwindows - 1) 38284778508Sblueswir1 index += 16 * env->nwindows; 38384778508Sblueswir1 return index; 38484778508Sblueswir1 } 38584778508Sblueswir1 38684778508Sblueswir1 /* save the register window 'cwp1' */ 38784778508Sblueswir1 static inline void save_window_offset(CPUSPARCState *env, int cwp1) 38884778508Sblueswir1 { 38984778508Sblueswir1 unsigned int i; 39084778508Sblueswir1 abi_ulong sp_ptr; 39184778508Sblueswir1 39284778508Sblueswir1 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)]; 39384778508Sblueswir1 #ifdef TARGET_SPARC64 39484778508Sblueswir1 if (sp_ptr & 3) 39584778508Sblueswir1 sp_ptr += SPARC64_STACK_BIAS; 39684778508Sblueswir1 #endif 39784778508Sblueswir1 #if defined(DEBUG_WIN) 39884778508Sblueswir1 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n", 39984778508Sblueswir1 sp_ptr, cwp1); 40084778508Sblueswir1 #endif 40184778508Sblueswir1 for(i = 0; i < 16; i++) { 40284778508Sblueswir1 /* FIXME - what to do if put_user() fails? */ 40384778508Sblueswir1 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr); 40484778508Sblueswir1 sp_ptr += sizeof(abi_ulong); 40584778508Sblueswir1 } 40684778508Sblueswir1 } 40784778508Sblueswir1 40884778508Sblueswir1 static void save_window(CPUSPARCState *env) 40984778508Sblueswir1 { 41084778508Sblueswir1 #ifndef TARGET_SPARC64 41184778508Sblueswir1 unsigned int new_wim; 41284778508Sblueswir1 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) & 41384778508Sblueswir1 ((1LL << env->nwindows) - 1); 41484778508Sblueswir1 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2)); 41584778508Sblueswir1 env->wim = new_wim; 41684778508Sblueswir1 #else 417d43624c4SGiuseppe Musacchio /* 418d43624c4SGiuseppe Musacchio * cansave is zero if the spill trap handler is triggered by `save` and 419d43624c4SGiuseppe Musacchio * nonzero if triggered by a `flushw` 420d43624c4SGiuseppe Musacchio */ 421d43624c4SGiuseppe Musacchio save_window_offset(env, cpu_cwp_dec(env, env->cwp - env->cansave - 2)); 42284778508Sblueswir1 env->cansave++; 42384778508Sblueswir1 env->canrestore--; 42484778508Sblueswir1 #endif 42584778508Sblueswir1 } 42684778508Sblueswir1 42784778508Sblueswir1 static void restore_window(CPUSPARCState *env) 42884778508Sblueswir1 { 42984778508Sblueswir1 #ifndef TARGET_SPARC64 43084778508Sblueswir1 unsigned int new_wim; 43184778508Sblueswir1 #endif 43284778508Sblueswir1 unsigned int i, cwp1; 43384778508Sblueswir1 abi_ulong sp_ptr; 43484778508Sblueswir1 43584778508Sblueswir1 #ifndef TARGET_SPARC64 43684778508Sblueswir1 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) & 43784778508Sblueswir1 ((1LL << env->nwindows) - 1); 43884778508Sblueswir1 #endif 43984778508Sblueswir1 44084778508Sblueswir1 /* restore the invalid window */ 44184778508Sblueswir1 cwp1 = cpu_cwp_inc(env, env->cwp + 1); 44284778508Sblueswir1 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)]; 44384778508Sblueswir1 #ifdef TARGET_SPARC64 44484778508Sblueswir1 if (sp_ptr & 3) 44584778508Sblueswir1 sp_ptr += SPARC64_STACK_BIAS; 44684778508Sblueswir1 #endif 44784778508Sblueswir1 #if defined(DEBUG_WIN) 44884778508Sblueswir1 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n", 44984778508Sblueswir1 sp_ptr, cwp1); 45084778508Sblueswir1 #endif 45184778508Sblueswir1 for(i = 0; i < 16; i++) { 45284778508Sblueswir1 /* FIXME - what to do if get_user() fails? */ 45384778508Sblueswir1 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr); 45484778508Sblueswir1 sp_ptr += sizeof(abi_ulong); 45584778508Sblueswir1 } 45684778508Sblueswir1 #ifdef TARGET_SPARC64 45784778508Sblueswir1 env->canrestore++; 45884778508Sblueswir1 if (env->cleanwin < env->nwindows - 1) 45984778508Sblueswir1 env->cleanwin++; 46084778508Sblueswir1 env->cansave--; 46184778508Sblueswir1 #else 46284778508Sblueswir1 env->wim = new_wim; 46384778508Sblueswir1 #endif 46484778508Sblueswir1 } 46584778508Sblueswir1 46684778508Sblueswir1 static void flush_windows(CPUSPARCState *env) 46784778508Sblueswir1 { 46884778508Sblueswir1 int offset, cwp1; 46984778508Sblueswir1 47084778508Sblueswir1 offset = 1; 47184778508Sblueswir1 for(;;) { 47284778508Sblueswir1 /* if restore would invoke restore_window(), then we can stop */ 47384778508Sblueswir1 cwp1 = cpu_cwp_inc(env, env->cwp + offset); 47484778508Sblueswir1 #ifndef TARGET_SPARC64 47584778508Sblueswir1 if (env->wim & (1 << cwp1)) 47684778508Sblueswir1 break; 47784778508Sblueswir1 #else 47884778508Sblueswir1 if (env->canrestore == 0) 47984778508Sblueswir1 break; 48084778508Sblueswir1 env->cansave++; 48184778508Sblueswir1 env->canrestore--; 48284778508Sblueswir1 #endif 48384778508Sblueswir1 save_window_offset(env, cwp1); 48484778508Sblueswir1 offset++; 48584778508Sblueswir1 } 48684778508Sblueswir1 cwp1 = cpu_cwp_inc(env, env->cwp + 1); 48784778508Sblueswir1 #ifndef TARGET_SPARC64 48884778508Sblueswir1 /* set wim so that restore will reload the registers */ 48984778508Sblueswir1 env->wim = 1 << cwp1; 49084778508Sblueswir1 #endif 49184778508Sblueswir1 #if defined(DEBUG_WIN) 49284778508Sblueswir1 printf("flush_windows: nb=%d\n", offset - 1); 49384778508Sblueswir1 #endif 49484778508Sblueswir1 } 49584778508Sblueswir1 49678cfb07fSJuergen Lock void cpu_loop(CPUSPARCState *env) 49784778508Sblueswir1 { 4985a59fbceSRichard Henderson CPUState *cs = env_cpu(env); 49984778508Sblueswir1 int trapnr, ret, syscall_nr; 50084778508Sblueswir1 //target_siginfo_t info; 50184778508Sblueswir1 50284778508Sblueswir1 while (1) { 503d148d90eSSergey Fedorov cpu_exec_start(cs); 504ded554cdSSean Bruno trapnr = cpu_exec(cs); 505d148d90eSSergey Fedorov cpu_exec_end(cs); 506d148d90eSSergey Fedorov process_queued_cpu_work(cs); 50784778508Sblueswir1 50884778508Sblueswir1 switch (trapnr) { 50977b9435fSblueswir1 #ifndef TARGET_SPARC64 51077b9435fSblueswir1 case 0x80: 51177b9435fSblueswir1 #else 51278cfb07fSJuergen Lock /* FreeBSD uses 0x141 for syscalls too */ 51378cfb07fSJuergen Lock case 0x141: 51478cfb07fSJuergen Lock if (bsd_type != target_freebsd) 51578cfb07fSJuergen Lock goto badtrap; 516484bed05SThomas Huth /* fallthrough */ 51784778508Sblueswir1 case 0x100: 51877b9435fSblueswir1 #endif 51984778508Sblueswir1 syscall_nr = env->gregs[1]; 52084778508Sblueswir1 if (bsd_type == target_freebsd) 52184778508Sblueswir1 ret = do_freebsd_syscall(env, syscall_nr, 52284778508Sblueswir1 env->regwptr[0], env->regwptr[1], 52384778508Sblueswir1 env->regwptr[2], env->regwptr[3], 52478cfb07fSJuergen Lock env->regwptr[4], env->regwptr[5], 0, 0); 52584778508Sblueswir1 else if (bsd_type == target_netbsd) 52684778508Sblueswir1 ret = do_netbsd_syscall(env, syscall_nr, 52784778508Sblueswir1 env->regwptr[0], env->regwptr[1], 52884778508Sblueswir1 env->regwptr[2], env->regwptr[3], 52984778508Sblueswir1 env->regwptr[4], env->regwptr[5]); 530cdba95bdSblueswir1 else { //if (bsd_type == target_openbsd) 531cdba95bdSblueswir1 #if defined(TARGET_SPARC64) 532cdba95bdSblueswir1 syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG | 533cdba95bdSblueswir1 TARGET_OPENBSD_SYSCALL_G2RFLAG); 534cdba95bdSblueswir1 #endif 53584778508Sblueswir1 ret = do_openbsd_syscall(env, syscall_nr, 53684778508Sblueswir1 env->regwptr[0], env->regwptr[1], 53784778508Sblueswir1 env->regwptr[2], env->regwptr[3], 53884778508Sblueswir1 env->regwptr[4], env->regwptr[5]); 539cdba95bdSblueswir1 } 54084778508Sblueswir1 if ((unsigned int)ret >= (unsigned int)(-515)) { 54178cfb07fSJuergen Lock ret = -ret; 54284778508Sblueswir1 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) 54384778508Sblueswir1 env->xcc |= PSR_CARRY; 54484778508Sblueswir1 #else 54584778508Sblueswir1 env->psr |= PSR_CARRY; 54684778508Sblueswir1 #endif 54784778508Sblueswir1 } else { 54884778508Sblueswir1 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) 54984778508Sblueswir1 env->xcc &= ~PSR_CARRY; 55084778508Sblueswir1 #else 55184778508Sblueswir1 env->psr &= ~PSR_CARRY; 55284778508Sblueswir1 #endif 55384778508Sblueswir1 } 55484778508Sblueswir1 env->regwptr[0] = ret; 55584778508Sblueswir1 /* next instruction */ 556cdba95bdSblueswir1 #if defined(TARGET_SPARC64) 557cdba95bdSblueswir1 if (bsd_type == target_openbsd && 558cdba95bdSblueswir1 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) { 55984778508Sblueswir1 env->pc = env->gregs[2]; 56084778508Sblueswir1 env->npc = env->pc + 4; 561cdba95bdSblueswir1 } else if (bsd_type == target_openbsd && 562cdba95bdSblueswir1 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) { 56384778508Sblueswir1 env->pc = env->gregs[7]; 56484778508Sblueswir1 env->npc = env->pc + 4; 56584778508Sblueswir1 } else { 56684778508Sblueswir1 env->pc = env->npc; 56784778508Sblueswir1 env->npc = env->npc + 4; 56884778508Sblueswir1 } 56984778508Sblueswir1 #else 57084778508Sblueswir1 env->pc = env->npc; 57184778508Sblueswir1 env->npc = env->npc + 4; 57284778508Sblueswir1 #endif 57384778508Sblueswir1 break; 57484778508Sblueswir1 case 0x83: /* flush windows */ 57584778508Sblueswir1 #ifdef TARGET_ABI32 57684778508Sblueswir1 case 0x103: 57784778508Sblueswir1 #endif 57884778508Sblueswir1 flush_windows(env); 57984778508Sblueswir1 /* next instruction */ 58084778508Sblueswir1 env->pc = env->npc; 58184778508Sblueswir1 env->npc = env->npc + 4; 58284778508Sblueswir1 break; 58384778508Sblueswir1 #ifndef TARGET_SPARC64 58484778508Sblueswir1 case TT_WIN_OVF: /* window overflow */ 58584778508Sblueswir1 save_window(env); 58684778508Sblueswir1 break; 58784778508Sblueswir1 case TT_WIN_UNF: /* window underflow */ 58884778508Sblueswir1 restore_window(env); 58984778508Sblueswir1 break; 59084778508Sblueswir1 case TT_TFAULT: 59184778508Sblueswir1 case TT_DFAULT: 59284778508Sblueswir1 #if 0 59384778508Sblueswir1 { 59484778508Sblueswir1 info.si_signo = SIGSEGV; 59584778508Sblueswir1 info.si_errno = 0; 59684778508Sblueswir1 /* XXX: check env->error_code */ 59784778508Sblueswir1 info.si_code = TARGET_SEGV_MAPERR; 59884778508Sblueswir1 info._sifields._sigfault._addr = env->mmuregs[4]; 59984778508Sblueswir1 queue_signal(env, info.si_signo, &info); 60084778508Sblueswir1 } 60184778508Sblueswir1 #endif 60284778508Sblueswir1 break; 60384778508Sblueswir1 #else 60484778508Sblueswir1 case TT_SPILL: /* window overflow */ 60584778508Sblueswir1 save_window(env); 60684778508Sblueswir1 break; 60784778508Sblueswir1 case TT_FILL: /* window underflow */ 60884778508Sblueswir1 restore_window(env); 60984778508Sblueswir1 break; 61084778508Sblueswir1 case TT_TFAULT: 61184778508Sblueswir1 case TT_DFAULT: 61284778508Sblueswir1 #if 0 61384778508Sblueswir1 { 61484778508Sblueswir1 info.si_signo = SIGSEGV; 61584778508Sblueswir1 info.si_errno = 0; 61684778508Sblueswir1 /* XXX: check env->error_code */ 61784778508Sblueswir1 info.si_code = TARGET_SEGV_MAPERR; 61884778508Sblueswir1 if (trapnr == TT_DFAULT) 61984778508Sblueswir1 info._sifields._sigfault._addr = env->dmmuregs[4]; 62084778508Sblueswir1 else 62184778508Sblueswir1 info._sifields._sigfault._addr = env->tsptr->tpc; 62284778508Sblueswir1 //queue_signal(env, info.si_signo, &info); 62384778508Sblueswir1 } 62484778508Sblueswir1 #endif 62584778508Sblueswir1 break; 62684778508Sblueswir1 #endif 62784778508Sblueswir1 case EXCP_INTERRUPT: 62884778508Sblueswir1 /* just indicate that signals should be handled asap */ 62984778508Sblueswir1 break; 63084778508Sblueswir1 case EXCP_DEBUG: 63184778508Sblueswir1 { 632e4335180SPeter Maydell #if 0 633e4335180SPeter Maydell int sig = 634e4335180SPeter Maydell #endif 635e4335180SPeter Maydell gdb_handlesig(cs, TARGET_SIGTRAP); 63684778508Sblueswir1 #if 0 63784778508Sblueswir1 if (sig) 63884778508Sblueswir1 { 63984778508Sblueswir1 info.si_signo = sig; 64084778508Sblueswir1 info.si_errno = 0; 64184778508Sblueswir1 info.si_code = TARGET_TRAP_BRKPT; 64284778508Sblueswir1 //queue_signal(env, info.si_signo, &info); 64384778508Sblueswir1 } 64484778508Sblueswir1 #endif 64584778508Sblueswir1 } 64684778508Sblueswir1 break; 64784778508Sblueswir1 default: 64878cfb07fSJuergen Lock #ifdef TARGET_SPARC64 64978cfb07fSJuergen Lock badtrap: 65078cfb07fSJuergen Lock #endif 65184778508Sblueswir1 printf ("Unhandled trap: 0x%x\n", trapnr); 65290c84c56SMarkus Armbruster cpu_dump_state(cs, stderr, 0); 65384778508Sblueswir1 exit (1); 65484778508Sblueswir1 } 65584778508Sblueswir1 process_pending_signals (env); 65684778508Sblueswir1 } 65784778508Sblueswir1 } 65884778508Sblueswir1 65984778508Sblueswir1 #endif 66084778508Sblueswir1 66184778508Sblueswir1 static void usage(void) 66284778508Sblueswir1 { 6637e563bfbSThomas Huth printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION 6640781dd6eSThomas Huth "\n" QEMU_COPYRIGHT "\n" 6652e59915dSPaolo Bonzini "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n" 66684778508Sblueswir1 "BSD CPU emulator (compiled for %s emulation)\n" 66784778508Sblueswir1 "\n" 66884778508Sblueswir1 "Standard options:\n" 66984778508Sblueswir1 "-h print this help\n" 67084778508Sblueswir1 "-g port wait gdb connection to port\n" 67184778508Sblueswir1 "-L path set the elf interpreter prefix (default=%s)\n" 67284778508Sblueswir1 "-s size set the stack size in bytes (default=%ld)\n" 673c8057f95SPeter Maydell "-cpu model select CPU (-cpu help for list)\n" 67484778508Sblueswir1 "-drop-ld-preload drop LD_PRELOAD for target process\n" 675fc0d96b4SBlue Swirl "-E var=value sets/modifies targets environment variable(s)\n" 676fc0d96b4SBlue Swirl "-U var unsets targets environment variable(s)\n" 6772fa5d9baSBlue Swirl "-B address set guest_base address to address\n" 67884778508Sblueswir1 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n" 67984778508Sblueswir1 "\n" 68084778508Sblueswir1 "Debug options:\n" 681989b697dSPeter Maydell "-d item1[,...] enable logging of specified items\n" 682989b697dSPeter Maydell " (use '-d help' for a list of log items)\n" 683989b697dSPeter Maydell "-D logfile write logs to 'logfile' (default stderr)\n" 68484778508Sblueswir1 "-p pagesize set the host page size to 'pagesize'\n" 6851b530a6dSaurel32 "-singlestep always run in singlestep mode\n" 68684778508Sblueswir1 "-strace log system calls\n" 6876913e79cSLluís Vilanova "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n" 6886913e79cSLluís Vilanova " specify tracing options\n" 68984778508Sblueswir1 "\n" 69084778508Sblueswir1 "Environment variables:\n" 69184778508Sblueswir1 "QEMU_STRACE Print system calls and arguments similar to the\n" 69284778508Sblueswir1 " 'strace' program. Enable by setting to any value.\n" 693fc0d96b4SBlue Swirl "You can use -E and -U options to set/unset environment variables\n" 694fc0d96b4SBlue Swirl "for target process. It is possible to provide several variables\n" 695fc0d96b4SBlue Swirl "by repeating the option. For example:\n" 696fc0d96b4SBlue Swirl " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n" 697fc0d96b4SBlue Swirl "Note that if you provide several changes to single variable\n" 698fc0d96b4SBlue Swirl "last change will stay in effect.\n" 699f5048cb7SEric Blake "\n" 700f5048cb7SEric Blake QEMU_HELP_BOTTOM "\n" 70184778508Sblueswir1 , 7022e59915dSPaolo Bonzini TARGET_NAME, 70384778508Sblueswir1 interp_prefix, 704989b697dSPeter Maydell x86_stack_size); 7052d18e637Sblueswir1 exit(1); 70684778508Sblueswir1 } 70784778508Sblueswir1 708dca1173cSAndreas Färber THREAD CPUState *thread_cpu; 70984778508Sblueswir1 71048f59211SEd Maste bool qemu_cpu_is_self(CPUState *cpu) 71148f59211SEd Maste { 71248f59211SEd Maste return thread_cpu == cpu; 71348f59211SEd Maste } 71448f59211SEd Maste 71548f59211SEd Maste void qemu_cpu_kick(CPUState *cpu) 71648f59211SEd Maste { 71748f59211SEd Maste cpu_exit(cpu); 71848f59211SEd Maste } 71948f59211SEd Maste 72084778508Sblueswir1 /* Assumes contents are already zeroed. */ 72184778508Sblueswir1 void init_task_state(TaskState *ts) 72284778508Sblueswir1 { 72384778508Sblueswir1 int i; 72484778508Sblueswir1 72584778508Sblueswir1 ts->used = 1; 72684778508Sblueswir1 ts->first_free = ts->sigqueue_table; 72784778508Sblueswir1 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) { 72884778508Sblueswir1 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1]; 72984778508Sblueswir1 } 73084778508Sblueswir1 ts->sigqueue_table[i].next = NULL; 73184778508Sblueswir1 } 73284778508Sblueswir1 73384778508Sblueswir1 int main(int argc, char **argv) 73484778508Sblueswir1 { 73584778508Sblueswir1 const char *filename; 73684778508Sblueswir1 const char *cpu_model; 7372278b939SIgor Mammedov const char *cpu_type; 738989b697dSPeter Maydell const char *log_file = NULL; 739c235d738SMatthew Fernandez const char *log_mask = NULL; 74084778508Sblueswir1 struct target_pt_regs regs1, *regs = ®s1; 74184778508Sblueswir1 struct image_info info1, *info = &info1; 74284778508Sblueswir1 TaskState ts1, *ts = &ts1; 7439349b4f9SAndreas Färber CPUArchState *env; 744db6b81d4SAndreas Färber CPUState *cpu; 74584778508Sblueswir1 int optind; 74684778508Sblueswir1 const char *r; 747fcedd920SAlex Bennée const char *gdbstub = NULL; 748fc0d96b4SBlue Swirl char **target_environ, **wrk; 749fc0d96b4SBlue Swirl envlist_t *envlist = NULL; 75078cfb07fSJuergen Lock bsd_type = target_openbsd; 75184778508Sblueswir1 75284778508Sblueswir1 if (argc <= 1) 75384778508Sblueswir1 usage(); 75484778508Sblueswir1 755f5852efaSChristophe Fergeau error_init(argv[0]); 756fe4db84dSDaniel P. Berrange module_call_init(MODULE_INIT_TRACE); 757267f685bSPaolo Bonzini qemu_init_cpu_list(); 758ce008c1fSAndreas Färber module_call_init(MODULE_INIT_QOM); 759ce008c1fSAndreas Färber 760ec45bbe5SSaurav Sachidanand envlist = envlist_create(); 761fc0d96b4SBlue Swirl 762fc0d96b4SBlue Swirl /* add current environment into the list */ 763fc0d96b4SBlue Swirl for (wrk = environ; *wrk != NULL; wrk++) { 764fc0d96b4SBlue Swirl (void) envlist_setenv(envlist, *wrk); 765fc0d96b4SBlue Swirl } 766fc0d96b4SBlue Swirl 76784778508Sblueswir1 cpu_model = NULL; 7680c62de2fSJuergen Lock 7696913e79cSLluís Vilanova qemu_add_opts(&qemu_trace_opts); 7706913e79cSLluís Vilanova 77184778508Sblueswir1 optind = 1; 77284778508Sblueswir1 for (;;) { 77384778508Sblueswir1 if (optind >= argc) 77484778508Sblueswir1 break; 77584778508Sblueswir1 r = argv[optind]; 77684778508Sblueswir1 if (r[0] != '-') 77784778508Sblueswir1 break; 77884778508Sblueswir1 optind++; 77984778508Sblueswir1 r++; 78084778508Sblueswir1 if (!strcmp(r, "-")) { 78184778508Sblueswir1 break; 78284778508Sblueswir1 } else if (!strcmp(r, "d")) { 783c235d738SMatthew Fernandez if (optind >= argc) { 78484778508Sblueswir1 break; 78584778508Sblueswir1 } 786c235d738SMatthew Fernandez log_mask = argv[optind++]; 787c235d738SMatthew Fernandez } else if (!strcmp(r, "D")) { 788c235d738SMatthew Fernandez if (optind >= argc) { 789c235d738SMatthew Fernandez break; 79084778508Sblueswir1 } 791c235d738SMatthew Fernandez log_file = argv[optind++]; 792fc0d96b4SBlue Swirl } else if (!strcmp(r, "E")) { 793fc0d96b4SBlue Swirl r = argv[optind++]; 794fc0d96b4SBlue Swirl if (envlist_setenv(envlist, r) != 0) 795fc0d96b4SBlue Swirl usage(); 796f66724c9SStefan Weil } else if (!strcmp(r, "ignore-environment")) { 797f66724c9SStefan Weil envlist_free(envlist); 798ec45bbe5SSaurav Sachidanand envlist = envlist_create(); 799fc0d96b4SBlue Swirl } else if (!strcmp(r, "U")) { 800fc0d96b4SBlue Swirl r = argv[optind++]; 801fc0d96b4SBlue Swirl if (envlist_unsetenv(envlist, r) != 0) 802fc0d96b4SBlue Swirl usage(); 80384778508Sblueswir1 } else if (!strcmp(r, "s")) { 80484778508Sblueswir1 r = argv[optind++]; 80584778508Sblueswir1 x86_stack_size = strtol(r, (char **)&r, 0); 80684778508Sblueswir1 if (x86_stack_size <= 0) 80784778508Sblueswir1 usage(); 80884778508Sblueswir1 if (*r == 'M') 80966d26ddbSPhilippe Mathieu-Daudé x86_stack_size *= MiB; 81084778508Sblueswir1 else if (*r == 'k' || *r == 'K') 81166d26ddbSPhilippe Mathieu-Daudé x86_stack_size *= KiB; 81284778508Sblueswir1 } else if (!strcmp(r, "L")) { 81384778508Sblueswir1 interp_prefix = argv[optind++]; 81484778508Sblueswir1 } else if (!strcmp(r, "p")) { 81584778508Sblueswir1 qemu_host_page_size = atoi(argv[optind++]); 81684778508Sblueswir1 if (qemu_host_page_size == 0 || 81784778508Sblueswir1 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) { 81884778508Sblueswir1 fprintf(stderr, "page size must be a power of two\n"); 81984778508Sblueswir1 exit(1); 82084778508Sblueswir1 } 82184778508Sblueswir1 } else if (!strcmp(r, "g")) { 822fcedd920SAlex Bennée gdbstub = g_strdup(argv[optind++]); 82384778508Sblueswir1 } else if (!strcmp(r, "r")) { 82484778508Sblueswir1 qemu_uname_release = argv[optind++]; 82584778508Sblueswir1 } else if (!strcmp(r, "cpu")) { 82684778508Sblueswir1 cpu_model = argv[optind++]; 827c8057f95SPeter Maydell if (is_help_option(cpu_model)) { 82884778508Sblueswir1 /* XXX: implement xxx_cpu_list for targets that still miss it */ 82984778508Sblueswir1 #if defined(cpu_list) 8300442428aSMarkus Armbruster cpu_list(); 83184778508Sblueswir1 #endif 8322d18e637Sblueswir1 exit(1); 83384778508Sblueswir1 } 8342fa5d9baSBlue Swirl } else if (!strcmp(r, "B")) { 8352fa5d9baSBlue Swirl guest_base = strtol(argv[optind++], NULL, 0); 836e307c192SRichard Henderson have_guest_base = true; 83784778508Sblueswir1 } else if (!strcmp(r, "drop-ld-preload")) { 838fc0d96b4SBlue Swirl (void) envlist_unsetenv(envlist, "LD_PRELOAD"); 83984778508Sblueswir1 } else if (!strcmp(r, "bsd")) { 84084778508Sblueswir1 if (!strcasecmp(argv[optind], "freebsd")) { 84184778508Sblueswir1 bsd_type = target_freebsd; 84284778508Sblueswir1 } else if (!strcasecmp(argv[optind], "netbsd")) { 84384778508Sblueswir1 bsd_type = target_netbsd; 84484778508Sblueswir1 } else if (!strcasecmp(argv[optind], "openbsd")) { 84584778508Sblueswir1 bsd_type = target_openbsd; 84684778508Sblueswir1 } else { 84784778508Sblueswir1 usage(); 84884778508Sblueswir1 } 84984778508Sblueswir1 optind++; 8501b530a6dSaurel32 } else if (!strcmp(r, "singlestep")) { 8511b530a6dSaurel32 singlestep = 1; 85284778508Sblueswir1 } else if (!strcmp(r, "strace")) { 85384778508Sblueswir1 do_strace = 1; 8546913e79cSLluís Vilanova } else if (!strcmp(r, "trace")) { 85592eecfffSPaolo Bonzini trace_opt_parse(optarg); 8566913e79cSLluís Vilanova } else { 85784778508Sblueswir1 usage(); 85884778508Sblueswir1 } 85984778508Sblueswir1 } 86084778508Sblueswir1 861c235d738SMatthew Fernandez /* init debug */ 862f2937a33SPaolo Bonzini qemu_log_needs_buffers(); 863daa76aa4SMarkus Armbruster qemu_set_log_filename(log_file, &error_fatal); 864c235d738SMatthew Fernandez if (log_mask) { 865c235d738SMatthew Fernandez int mask; 866c235d738SMatthew Fernandez 8674fde1ebaSPeter Maydell mask = qemu_str_to_log_mask(log_mask); 868c235d738SMatthew Fernandez if (!mask) { 86959a6fa6eSPeter Maydell qemu_print_log_usage(stdout); 870c235d738SMatthew Fernandez exit(1); 871c235d738SMatthew Fernandez } 87224537a01SPeter Maydell qemu_set_log(mask); 873c235d738SMatthew Fernandez } 874c235d738SMatthew Fernandez 8754b5dfd82SPeter Maydell if (optind >= argc) { 8764b5dfd82SPeter Maydell usage(); 8774b5dfd82SPeter Maydell } 8784b5dfd82SPeter Maydell filename = argv[optind]; 8794b5dfd82SPeter Maydell 8806913e79cSLluís Vilanova if (!trace_init_backends()) { 8816913e79cSLluís Vilanova exit(1); 8826913e79cSLluís Vilanova } 88392eecfffSPaolo Bonzini trace_init_file(); 8846913e79cSLluís Vilanova 88584778508Sblueswir1 /* Zero out regs */ 88684778508Sblueswir1 memset(regs, 0, sizeof(struct target_pt_regs)); 88784778508Sblueswir1 88884778508Sblueswir1 /* Zero out image_info */ 88984778508Sblueswir1 memset(info, 0, sizeof(struct image_info)); 89084778508Sblueswir1 89184778508Sblueswir1 /* Scan interp_prefix dir for replacement files. */ 89284778508Sblueswir1 init_paths(interp_prefix); 89384778508Sblueswir1 89484778508Sblueswir1 if (cpu_model == NULL) { 89531fc12dfSblueswir1 #if defined(TARGET_I386) 89631fc12dfSblueswir1 #ifdef TARGET_X86_64 89731fc12dfSblueswir1 cpu_model = "qemu64"; 89831fc12dfSblueswir1 #else 89931fc12dfSblueswir1 cpu_model = "qemu32"; 90031fc12dfSblueswir1 #endif 90131fc12dfSblueswir1 #elif defined(TARGET_SPARC) 90284778508Sblueswir1 #ifdef TARGET_SPARC64 90384778508Sblueswir1 cpu_model = "TI UltraSparc II"; 90484778508Sblueswir1 #else 90584778508Sblueswir1 cpu_model = "Fujitsu MB86904"; 90684778508Sblueswir1 #endif 90784778508Sblueswir1 #else 90884778508Sblueswir1 cpu_model = "any"; 90984778508Sblueswir1 #endif 91084778508Sblueswir1 } 9112b5249b8SIgor Mammedov 912b86f59c7SClaudio Fontana cpu_type = parse_cpu_option(cpu_model); 9132b5249b8SIgor Mammedov /* init tcg before creating CPUs and to get qemu_host_page_size */ 914940e43aaSClaudio Fontana { 915940e43aaSClaudio Fontana AccelClass *ac = ACCEL_GET_CLASS(current_accel()); 9162b5249b8SIgor Mammedov 917940e43aaSClaudio Fontana ac->init_machine(NULL); 918b86f59c7SClaudio Fontana accel_init_interfaces(ac); 919940e43aaSClaudio Fontana } 9202278b939SIgor Mammedov cpu = cpu_create(cpu_type); 9212994fd96SEduardo Habkost env = cpu->env_ptr; 92277868120SAndreas Färber #if defined(TARGET_SPARC) || defined(TARGET_PPC) 923db6b81d4SAndreas Färber cpu_reset(cpu); 924b55a37c9SBlue Swirl #endif 925db6b81d4SAndreas Färber thread_cpu = cpu; 92684778508Sblueswir1 92784778508Sblueswir1 if (getenv("QEMU_STRACE")) { 92884778508Sblueswir1 do_strace = 1; 92984778508Sblueswir1 } 93084778508Sblueswir1 931fc0d96b4SBlue Swirl target_environ = envlist_to_environ(envlist, NULL); 932fc0d96b4SBlue Swirl envlist_free(envlist); 93384778508Sblueswir1 9342fa5d9baSBlue Swirl /* 9352b5249b8SIgor Mammedov * Now that page sizes are configured in tcg_exec_init() we can do 9362fa5d9baSBlue Swirl * proper page alignment for guest_base. 9372fa5d9baSBlue Swirl */ 9382fa5d9baSBlue Swirl guest_base = HOST_PAGE_ALIGN(guest_base); 9392fa5d9baSBlue Swirl 9402fa5d9baSBlue Swirl /* 9412fa5d9baSBlue Swirl * Read in mmap_min_addr kernel parameter. This value is used 9422fa5d9baSBlue Swirl * When loading the ELF image to determine whether guest_base 9432fa5d9baSBlue Swirl * is needed. 9442fa5d9baSBlue Swirl * 9452fa5d9baSBlue Swirl * When user has explicitly set the quest base, we skip this 9462fa5d9baSBlue Swirl * test. 9472fa5d9baSBlue Swirl */ 9482fa5d9baSBlue Swirl if (!have_guest_base) { 9492fa5d9baSBlue Swirl FILE *fp; 9502fa5d9baSBlue Swirl 9512fa5d9baSBlue Swirl if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) { 9522fa5d9baSBlue Swirl unsigned long tmp; 9532fa5d9baSBlue Swirl if (fscanf(fp, "%lu", &tmp) == 1) { 9542fa5d9baSBlue Swirl mmap_min_addr = tmp; 95513829020SPaolo Bonzini qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n", mmap_min_addr); 9562fa5d9baSBlue Swirl } 9572fa5d9baSBlue Swirl fclose(fp); 9582fa5d9baSBlue Swirl } 9592fa5d9baSBlue Swirl } 96084778508Sblueswir1 96184778508Sblueswir1 if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) { 96284778508Sblueswir1 printf("Error loading %s\n", filename); 96384778508Sblueswir1 _exit(1); 96484778508Sblueswir1 } 96584778508Sblueswir1 96684778508Sblueswir1 for (wrk = target_environ; *wrk; wrk++) { 967ec45bbe5SSaurav Sachidanand g_free(*wrk); 96884778508Sblueswir1 } 96984778508Sblueswir1 970ec45bbe5SSaurav Sachidanand g_free(target_environ); 97184778508Sblueswir1 97213829020SPaolo Bonzini if (qemu_loglevel_mask(CPU_LOG_PAGE)) { 9735ca870b9SRichard Henderson qemu_log("guest_base %p\n", (void *)guest_base); 97410d0d505SAlex Bennée log_page_dump("binary load"); 97584778508Sblueswir1 97693fcfe39Saliguori qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk); 97793fcfe39Saliguori qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code); 97893fcfe39Saliguori qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n", 97984778508Sblueswir1 info->start_code); 98093fcfe39Saliguori qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n", 98184778508Sblueswir1 info->start_data); 98293fcfe39Saliguori qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data); 98393fcfe39Saliguori qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n", 98484778508Sblueswir1 info->start_stack); 98593fcfe39Saliguori qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk); 98693fcfe39Saliguori qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry); 9872e77eac6Sblueswir1 } 98884778508Sblueswir1 98984778508Sblueswir1 target_set_brk(info->brk); 99084778508Sblueswir1 syscall_init(); 99184778508Sblueswir1 signal_init(); 99284778508Sblueswir1 9939002ec79SRichard Henderson /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay 9949002ec79SRichard Henderson generating the prologue until now so that the prologue can take 9959002ec79SRichard Henderson the real value of GUEST_BASE into account. */ 996b1311c4aSEmilio G. Cota tcg_prologue_init(tcg_ctx); 997e8feb96fSEmilio G. Cota tcg_region_init(); 9989002ec79SRichard Henderson 99984778508Sblueswir1 /* build Task State */ 100084778508Sblueswir1 memset(ts, 0, sizeof(TaskState)); 100184778508Sblueswir1 init_task_state(ts); 100284778508Sblueswir1 ts->info = info; 10030429a971SAndreas Färber cpu->opaque = ts; 100484778508Sblueswir1 100531fc12dfSblueswir1 #if defined(TARGET_I386) 100631fc12dfSblueswir1 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK; 1007b98dbc90SPaolo Bonzini env->hflags |= HF_PE_MASK | HF_CPL_MASK; 10080514ef2fSEduardo Habkost if (env->features[FEAT_1_EDX] & CPUID_SSE) { 100931fc12dfSblueswir1 env->cr[4] |= CR4_OSFXSR_MASK; 101031fc12dfSblueswir1 env->hflags |= HF_OSFXSR_MASK; 101131fc12dfSblueswir1 } 101231fc12dfSblueswir1 #ifndef TARGET_ABI32 101331fc12dfSblueswir1 /* enable 64 bit mode if possible */ 10140514ef2fSEduardo Habkost if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) { 101531fc12dfSblueswir1 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n"); 101631fc12dfSblueswir1 exit(1); 101731fc12dfSblueswir1 } 101831fc12dfSblueswir1 env->cr[4] |= CR4_PAE_MASK; 101931fc12dfSblueswir1 env->efer |= MSR_EFER_LMA | MSR_EFER_LME; 102031fc12dfSblueswir1 env->hflags |= HF_LMA_MASK; 102131fc12dfSblueswir1 #endif 102231fc12dfSblueswir1 102331fc12dfSblueswir1 /* flags setup : we activate the IRQs by default as in user mode */ 102431fc12dfSblueswir1 env->eflags |= IF_MASK; 102531fc12dfSblueswir1 102631fc12dfSblueswir1 /* linux register setup */ 102731fc12dfSblueswir1 #ifndef TARGET_ABI32 102831fc12dfSblueswir1 env->regs[R_EAX] = regs->rax; 102931fc12dfSblueswir1 env->regs[R_EBX] = regs->rbx; 103031fc12dfSblueswir1 env->regs[R_ECX] = regs->rcx; 103131fc12dfSblueswir1 env->regs[R_EDX] = regs->rdx; 103231fc12dfSblueswir1 env->regs[R_ESI] = regs->rsi; 103331fc12dfSblueswir1 env->regs[R_EDI] = regs->rdi; 103431fc12dfSblueswir1 env->regs[R_EBP] = regs->rbp; 103531fc12dfSblueswir1 env->regs[R_ESP] = regs->rsp; 103631fc12dfSblueswir1 env->eip = regs->rip; 103731fc12dfSblueswir1 #else 103831fc12dfSblueswir1 env->regs[R_EAX] = regs->eax; 103931fc12dfSblueswir1 env->regs[R_EBX] = regs->ebx; 104031fc12dfSblueswir1 env->regs[R_ECX] = regs->ecx; 104131fc12dfSblueswir1 env->regs[R_EDX] = regs->edx; 104231fc12dfSblueswir1 env->regs[R_ESI] = regs->esi; 104331fc12dfSblueswir1 env->regs[R_EDI] = regs->edi; 104431fc12dfSblueswir1 env->regs[R_EBP] = regs->ebp; 104531fc12dfSblueswir1 env->regs[R_ESP] = regs->esp; 104631fc12dfSblueswir1 env->eip = regs->eip; 104731fc12dfSblueswir1 #endif 104831fc12dfSblueswir1 104931fc12dfSblueswir1 /* linux interrupt setup */ 105031fc12dfSblueswir1 #ifndef TARGET_ABI32 105131fc12dfSblueswir1 env->idt.limit = 511; 105231fc12dfSblueswir1 #else 105331fc12dfSblueswir1 env->idt.limit = 255; 105431fc12dfSblueswir1 #endif 105531fc12dfSblueswir1 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1), 105631fc12dfSblueswir1 PROT_READ|PROT_WRITE, 105731fc12dfSblueswir1 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); 1058*3e8f1628SRichard Henderson idt_table = g2h_untagged(env->idt.base); 105931fc12dfSblueswir1 set_idt(0, 0); 106031fc12dfSblueswir1 set_idt(1, 0); 106131fc12dfSblueswir1 set_idt(2, 0); 106231fc12dfSblueswir1 set_idt(3, 3); 106331fc12dfSblueswir1 set_idt(4, 3); 106431fc12dfSblueswir1 set_idt(5, 0); 106531fc12dfSblueswir1 set_idt(6, 0); 106631fc12dfSblueswir1 set_idt(7, 0); 106731fc12dfSblueswir1 set_idt(8, 0); 106831fc12dfSblueswir1 set_idt(9, 0); 106931fc12dfSblueswir1 set_idt(10, 0); 107031fc12dfSblueswir1 set_idt(11, 0); 107131fc12dfSblueswir1 set_idt(12, 0); 107231fc12dfSblueswir1 set_idt(13, 0); 107331fc12dfSblueswir1 set_idt(14, 0); 107431fc12dfSblueswir1 set_idt(15, 0); 107531fc12dfSblueswir1 set_idt(16, 0); 107631fc12dfSblueswir1 set_idt(17, 0); 107731fc12dfSblueswir1 set_idt(18, 0); 107831fc12dfSblueswir1 set_idt(19, 0); 107931fc12dfSblueswir1 set_idt(0x80, 3); 108031fc12dfSblueswir1 108131fc12dfSblueswir1 /* linux segment setup */ 108231fc12dfSblueswir1 { 108331fc12dfSblueswir1 uint64_t *gdt_table; 108431fc12dfSblueswir1 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES, 108531fc12dfSblueswir1 PROT_READ|PROT_WRITE, 108631fc12dfSblueswir1 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); 108731fc12dfSblueswir1 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1; 1088*3e8f1628SRichard Henderson gdt_table = g2h_untagged(env->gdt.base); 108931fc12dfSblueswir1 #ifdef TARGET_ABI32 109031fc12dfSblueswir1 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff, 109131fc12dfSblueswir1 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 109231fc12dfSblueswir1 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT)); 109331fc12dfSblueswir1 #else 109431fc12dfSblueswir1 /* 64 bit code segment */ 109531fc12dfSblueswir1 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff, 109631fc12dfSblueswir1 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 109731fc12dfSblueswir1 DESC_L_MASK | 109831fc12dfSblueswir1 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT)); 109931fc12dfSblueswir1 #endif 110031fc12dfSblueswir1 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff, 110131fc12dfSblueswir1 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 110231fc12dfSblueswir1 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT)); 110331fc12dfSblueswir1 } 110431fc12dfSblueswir1 110531fc12dfSblueswir1 cpu_x86_load_seg(env, R_CS, __USER_CS); 110631fc12dfSblueswir1 cpu_x86_load_seg(env, R_SS, __USER_DS); 110731fc12dfSblueswir1 #ifdef TARGET_ABI32 110831fc12dfSblueswir1 cpu_x86_load_seg(env, R_DS, __USER_DS); 110931fc12dfSblueswir1 cpu_x86_load_seg(env, R_ES, __USER_DS); 111031fc12dfSblueswir1 cpu_x86_load_seg(env, R_FS, __USER_DS); 111131fc12dfSblueswir1 cpu_x86_load_seg(env, R_GS, __USER_DS); 111231fc12dfSblueswir1 /* This hack makes Wine work... */ 111331fc12dfSblueswir1 env->segs[R_FS].selector = 0; 111431fc12dfSblueswir1 #else 111531fc12dfSblueswir1 cpu_x86_load_seg(env, R_DS, 0); 111631fc12dfSblueswir1 cpu_x86_load_seg(env, R_ES, 0); 111731fc12dfSblueswir1 cpu_x86_load_seg(env, R_FS, 0); 111831fc12dfSblueswir1 cpu_x86_load_seg(env, R_GS, 0); 111931fc12dfSblueswir1 #endif 112031fc12dfSblueswir1 #elif defined(TARGET_SPARC) 112184778508Sblueswir1 { 112284778508Sblueswir1 int i; 112384778508Sblueswir1 env->pc = regs->pc; 112484778508Sblueswir1 env->npc = regs->npc; 112584778508Sblueswir1 env->y = regs->y; 112684778508Sblueswir1 for(i = 0; i < 8; i++) 112784778508Sblueswir1 env->gregs[i] = regs->u_regs[i]; 112884778508Sblueswir1 for(i = 0; i < 8; i++) 112984778508Sblueswir1 env->regwptr[i] = regs->u_regs[i + 8]; 113084778508Sblueswir1 } 113184778508Sblueswir1 #else 113284778508Sblueswir1 #error unsupported target CPU 113384778508Sblueswir1 #endif 113484778508Sblueswir1 1135fcedd920SAlex Bennée if (gdbstub) { 1136fcedd920SAlex Bennée gdbserver_start(gdbstub); 1137db6b81d4SAndreas Färber gdb_handlesig(cpu, 0); 113884778508Sblueswir1 } 113978cfb07fSJuergen Lock cpu_loop(env); 114084778508Sblueswir1 /* never exits */ 114184778508Sblueswir1 return 0; 114284778508Sblueswir1 } 1143