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 */ 1984778508Sblueswir1 #include <stdlib.h> 2084778508Sblueswir1 #include <stdio.h> 2184778508Sblueswir1 #include <stdarg.h> 2284778508Sblueswir1 #include <string.h> 2384778508Sblueswir1 #include <errno.h> 2484778508Sblueswir1 #include <unistd.h> 2584778508Sblueswir1 #include <machine/trap.h> 2631fc12dfSblueswir1 #include <sys/types.h> 2731fc12dfSblueswir1 #include <sys/mman.h> 2884778508Sblueswir1 2984778508Sblueswir1 #include "qemu.h" 3084778508Sblueswir1 #include "qemu-common.h" 3184778508Sblueswir1 /* For tb_lock */ 322b41f10eSBlue Swirl #include "cpu.h" 339002ec79SRichard Henderson #include "tcg.h" 341de7afc9SPaolo Bonzini #include "qemu/timer.h" 351de7afc9SPaolo Bonzini #include "qemu/envlist.h" 36fc0d96b4SBlue Swirl 371b530a6dSaurel32 int singlestep; 382fa5d9baSBlue Swirl #if defined(CONFIG_USE_GUEST_BASE) 392fa5d9baSBlue Swirl unsigned long mmap_min_addr; 402fa5d9baSBlue Swirl unsigned long guest_base; 412fa5d9baSBlue Swirl int have_guest_base; 42d6ef40bfSPeter Maydell unsigned long reserved_va; 432fa5d9baSBlue Swirl #endif 441b530a6dSaurel32 457ee2822cSPaolo Bonzini static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX; 46fccae322SPeter Maydell const char *qemu_uname_release; 4784778508Sblueswir1 extern char **environ; 4878cfb07fSJuergen Lock enum BSDType bsd_type; 4984778508Sblueswir1 5084778508Sblueswir1 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so 5184778508Sblueswir1 we allocate a bigger stack. Need a better solution, for example 5284778508Sblueswir1 by remapping the process stack directly at the right place */ 5384778508Sblueswir1 unsigned long x86_stack_size = 512 * 1024; 5484778508Sblueswir1 5584778508Sblueswir1 void gemu_log(const char *fmt, ...) 5684778508Sblueswir1 { 5784778508Sblueswir1 va_list ap; 5884778508Sblueswir1 5984778508Sblueswir1 va_start(ap, fmt); 6084778508Sblueswir1 vfprintf(stderr, fmt, ap); 6184778508Sblueswir1 va_end(ap); 6284778508Sblueswir1 } 639399f095Sblueswir1 6431fc12dfSblueswir1 #if defined(TARGET_I386) 65b98e9ca8SAndreas Färber int cpu_get_pic_interrupt(CPUX86State *env) 6631fc12dfSblueswir1 { 6731fc12dfSblueswir1 return -1; 6831fc12dfSblueswir1 } 6931fc12dfSblueswir1 #endif 7031fc12dfSblueswir1 719399f095Sblueswir1 /* These are no-ops because we are not threadsafe. */ 729349b4f9SAndreas Färber static inline void cpu_exec_start(CPUArchState *env) 739399f095Sblueswir1 { 749399f095Sblueswir1 } 759399f095Sblueswir1 769349b4f9SAndreas Färber static inline void cpu_exec_end(CPUArchState *env) 779399f095Sblueswir1 { 789399f095Sblueswir1 } 799399f095Sblueswir1 809399f095Sblueswir1 static inline void start_exclusive(void) 819399f095Sblueswir1 { 829399f095Sblueswir1 } 839399f095Sblueswir1 849399f095Sblueswir1 static inline void end_exclusive(void) 859399f095Sblueswir1 { 869399f095Sblueswir1 } 879399f095Sblueswir1 889399f095Sblueswir1 void fork_start(void) 899399f095Sblueswir1 { 909399f095Sblueswir1 } 919399f095Sblueswir1 929399f095Sblueswir1 void fork_end(int child) 939399f095Sblueswir1 { 949399f095Sblueswir1 if (child) { 95f7ec7f7bSPeter Crosthwaite gdbserver_fork(thread_cpu); 969399f095Sblueswir1 } 979399f095Sblueswir1 } 989399f095Sblueswir1 999399f095Sblueswir1 void cpu_list_lock(void) 1009399f095Sblueswir1 { 1019399f095Sblueswir1 } 1029399f095Sblueswir1 1039399f095Sblueswir1 void cpu_list_unlock(void) 1049399f095Sblueswir1 { 1059399f095Sblueswir1 } 1069399f095Sblueswir1 10731fc12dfSblueswir1 #ifdef TARGET_I386 10831fc12dfSblueswir1 /***********************************************************/ 10931fc12dfSblueswir1 /* CPUX86 core interface */ 11031fc12dfSblueswir1 11131fc12dfSblueswir1 uint64_t cpu_get_tsc(CPUX86State *env) 11231fc12dfSblueswir1 { 11331fc12dfSblueswir1 return cpu_get_real_ticks(); 11431fc12dfSblueswir1 } 11531fc12dfSblueswir1 11631fc12dfSblueswir1 static void write_dt(void *ptr, unsigned long addr, unsigned long limit, 11731fc12dfSblueswir1 int flags) 11831fc12dfSblueswir1 { 11931fc12dfSblueswir1 unsigned int e1, e2; 12031fc12dfSblueswir1 uint32_t *p; 12131fc12dfSblueswir1 e1 = (addr << 16) | (limit & 0xffff); 12231fc12dfSblueswir1 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000); 12331fc12dfSblueswir1 e2 |= flags; 12431fc12dfSblueswir1 p = ptr; 12531fc12dfSblueswir1 p[0] = tswap32(e1); 12631fc12dfSblueswir1 p[1] = tswap32(e2); 12731fc12dfSblueswir1 } 12831fc12dfSblueswir1 12931fc12dfSblueswir1 static uint64_t *idt_table; 13031fc12dfSblueswir1 #ifdef TARGET_X86_64 13131fc12dfSblueswir1 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl, 13231fc12dfSblueswir1 uint64_t addr, unsigned int sel) 13331fc12dfSblueswir1 { 13431fc12dfSblueswir1 uint32_t *p, e1, e2; 13531fc12dfSblueswir1 e1 = (addr & 0xffff) | (sel << 16); 13631fc12dfSblueswir1 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); 13731fc12dfSblueswir1 p = ptr; 13831fc12dfSblueswir1 p[0] = tswap32(e1); 13931fc12dfSblueswir1 p[1] = tswap32(e2); 14031fc12dfSblueswir1 p[2] = tswap32(addr >> 32); 14131fc12dfSblueswir1 p[3] = 0; 14231fc12dfSblueswir1 } 14331fc12dfSblueswir1 /* only dpl matters as we do only user space emulation */ 14431fc12dfSblueswir1 static void set_idt(int n, unsigned int dpl) 14531fc12dfSblueswir1 { 14631fc12dfSblueswir1 set_gate64(idt_table + n * 2, 0, dpl, 0, 0); 14731fc12dfSblueswir1 } 14831fc12dfSblueswir1 #else 14931fc12dfSblueswir1 static void set_gate(void *ptr, unsigned int type, unsigned int dpl, 15031fc12dfSblueswir1 uint32_t addr, unsigned int sel) 15131fc12dfSblueswir1 { 15231fc12dfSblueswir1 uint32_t *p, e1, e2; 15331fc12dfSblueswir1 e1 = (addr & 0xffff) | (sel << 16); 15431fc12dfSblueswir1 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); 15531fc12dfSblueswir1 p = ptr; 15631fc12dfSblueswir1 p[0] = tswap32(e1); 15731fc12dfSblueswir1 p[1] = tswap32(e2); 15831fc12dfSblueswir1 } 15931fc12dfSblueswir1 16031fc12dfSblueswir1 /* only dpl matters as we do only user space emulation */ 16131fc12dfSblueswir1 static void set_idt(int n, unsigned int dpl) 16231fc12dfSblueswir1 { 16331fc12dfSblueswir1 set_gate(idt_table + n, 0, dpl, 0, 0); 16431fc12dfSblueswir1 } 16531fc12dfSblueswir1 #endif 16631fc12dfSblueswir1 16778cfb07fSJuergen Lock void cpu_loop(CPUX86State *env) 16831fc12dfSblueswir1 { 169*ea3e9847SPeter Crosthwaite X86CPU *cpu = x86_env_get_cpu(env); 170*ea3e9847SPeter Crosthwaite CPUState *cs = CPU(cpu); 17131fc12dfSblueswir1 int trapnr; 17231fc12dfSblueswir1 abi_ulong pc; 17331fc12dfSblueswir1 //target_siginfo_t info; 17431fc12dfSblueswir1 17531fc12dfSblueswir1 for(;;) { 17631fc12dfSblueswir1 trapnr = cpu_x86_exec(env); 17731fc12dfSblueswir1 switch(trapnr) { 17831fc12dfSblueswir1 case 0x80: 17931fc12dfSblueswir1 /* syscall from int $0x80 */ 18078cfb07fSJuergen Lock if (bsd_type == target_freebsd) { 18178cfb07fSJuergen Lock abi_ulong params = (abi_ulong) env->regs[R_ESP] + 18278cfb07fSJuergen Lock sizeof(int32_t); 18378cfb07fSJuergen Lock int32_t syscall_nr = env->regs[R_EAX]; 18478cfb07fSJuergen Lock int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8; 18578cfb07fSJuergen Lock 18678cfb07fSJuergen Lock if (syscall_nr == TARGET_FREEBSD_NR_syscall) { 18778cfb07fSJuergen Lock get_user_s32(syscall_nr, params); 18878cfb07fSJuergen Lock params += sizeof(int32_t); 18978cfb07fSJuergen Lock } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) { 19078cfb07fSJuergen Lock get_user_s32(syscall_nr, params); 19178cfb07fSJuergen Lock params += sizeof(int64_t); 19278cfb07fSJuergen Lock } 19378cfb07fSJuergen Lock get_user_s32(arg1, params); 19478cfb07fSJuergen Lock params += sizeof(int32_t); 19578cfb07fSJuergen Lock get_user_s32(arg2, params); 19678cfb07fSJuergen Lock params += sizeof(int32_t); 19778cfb07fSJuergen Lock get_user_s32(arg3, params); 19878cfb07fSJuergen Lock params += sizeof(int32_t); 19978cfb07fSJuergen Lock get_user_s32(arg4, params); 20078cfb07fSJuergen Lock params += sizeof(int32_t); 20178cfb07fSJuergen Lock get_user_s32(arg5, params); 20278cfb07fSJuergen Lock params += sizeof(int32_t); 20378cfb07fSJuergen Lock get_user_s32(arg6, params); 20478cfb07fSJuergen Lock params += sizeof(int32_t); 20578cfb07fSJuergen Lock get_user_s32(arg7, params); 20678cfb07fSJuergen Lock params += sizeof(int32_t); 20778cfb07fSJuergen Lock get_user_s32(arg8, params); 20878cfb07fSJuergen Lock env->regs[R_EAX] = do_freebsd_syscall(env, 20978cfb07fSJuergen Lock syscall_nr, 21078cfb07fSJuergen Lock arg1, 21178cfb07fSJuergen Lock arg2, 21278cfb07fSJuergen Lock arg3, 21378cfb07fSJuergen Lock arg4, 21478cfb07fSJuergen Lock arg5, 21578cfb07fSJuergen Lock arg6, 21678cfb07fSJuergen Lock arg7, 21778cfb07fSJuergen Lock arg8); 21878cfb07fSJuergen Lock } else { //if (bsd_type == target_openbsd) 21931fc12dfSblueswir1 env->regs[R_EAX] = do_openbsd_syscall(env, 22031fc12dfSblueswir1 env->regs[R_EAX], 22131fc12dfSblueswir1 env->regs[R_EBX], 22231fc12dfSblueswir1 env->regs[R_ECX], 22331fc12dfSblueswir1 env->regs[R_EDX], 22431fc12dfSblueswir1 env->regs[R_ESI], 22531fc12dfSblueswir1 env->regs[R_EDI], 22631fc12dfSblueswir1 env->regs[R_EBP]); 22778cfb07fSJuergen Lock } 22878cfb07fSJuergen Lock if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) { 22978cfb07fSJuergen Lock env->regs[R_EAX] = -env->regs[R_EAX]; 23078cfb07fSJuergen Lock env->eflags |= CC_C; 23178cfb07fSJuergen Lock } else { 23278cfb07fSJuergen Lock env->eflags &= ~CC_C; 23378cfb07fSJuergen Lock } 23431fc12dfSblueswir1 break; 23531fc12dfSblueswir1 #ifndef TARGET_ABI32 23631fc12dfSblueswir1 case EXCP_SYSCALL: 2375ba18547SStefan Weil /* syscall from syscall instruction */ 23878cfb07fSJuergen Lock if (bsd_type == target_freebsd) 23978cfb07fSJuergen Lock env->regs[R_EAX] = do_freebsd_syscall(env, 24078cfb07fSJuergen Lock env->regs[R_EAX], 24178cfb07fSJuergen Lock env->regs[R_EDI], 24278cfb07fSJuergen Lock env->regs[R_ESI], 24378cfb07fSJuergen Lock env->regs[R_EDX], 24478cfb07fSJuergen Lock env->regs[R_ECX], 24578cfb07fSJuergen Lock env->regs[8], 24678cfb07fSJuergen Lock env->regs[9], 0, 0); 24778cfb07fSJuergen Lock else { //if (bsd_type == target_openbsd) 24831fc12dfSblueswir1 env->regs[R_EAX] = do_openbsd_syscall(env, 24931fc12dfSblueswir1 env->regs[R_EAX], 25031fc12dfSblueswir1 env->regs[R_EDI], 25131fc12dfSblueswir1 env->regs[R_ESI], 25231fc12dfSblueswir1 env->regs[R_EDX], 25331fc12dfSblueswir1 env->regs[10], 25431fc12dfSblueswir1 env->regs[8], 25531fc12dfSblueswir1 env->regs[9]); 25678cfb07fSJuergen Lock } 25731fc12dfSblueswir1 env->eip = env->exception_next_eip; 25878cfb07fSJuergen Lock if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) { 25978cfb07fSJuergen Lock env->regs[R_EAX] = -env->regs[R_EAX]; 26078cfb07fSJuergen Lock env->eflags |= CC_C; 26178cfb07fSJuergen Lock } else { 26278cfb07fSJuergen Lock env->eflags &= ~CC_C; 26378cfb07fSJuergen Lock } 26431fc12dfSblueswir1 break; 26531fc12dfSblueswir1 #endif 26631fc12dfSblueswir1 #if 0 26731fc12dfSblueswir1 case EXCP0B_NOSEG: 26831fc12dfSblueswir1 case EXCP0C_STACK: 26931fc12dfSblueswir1 info.si_signo = SIGBUS; 27031fc12dfSblueswir1 info.si_errno = 0; 27131fc12dfSblueswir1 info.si_code = TARGET_SI_KERNEL; 27231fc12dfSblueswir1 info._sifields._sigfault._addr = 0; 27331fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 27431fc12dfSblueswir1 break; 27531fc12dfSblueswir1 case EXCP0D_GPF: 27631fc12dfSblueswir1 /* XXX: potential problem if ABI32 */ 27731fc12dfSblueswir1 #ifndef TARGET_X86_64 27831fc12dfSblueswir1 if (env->eflags & VM_MASK) { 27931fc12dfSblueswir1 handle_vm86_fault(env); 28031fc12dfSblueswir1 } else 28131fc12dfSblueswir1 #endif 28231fc12dfSblueswir1 { 28331fc12dfSblueswir1 info.si_signo = SIGSEGV; 28431fc12dfSblueswir1 info.si_errno = 0; 28531fc12dfSblueswir1 info.si_code = TARGET_SI_KERNEL; 28631fc12dfSblueswir1 info._sifields._sigfault._addr = 0; 28731fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 28831fc12dfSblueswir1 } 28931fc12dfSblueswir1 break; 29031fc12dfSblueswir1 case EXCP0E_PAGE: 29131fc12dfSblueswir1 info.si_signo = SIGSEGV; 29231fc12dfSblueswir1 info.si_errno = 0; 29331fc12dfSblueswir1 if (!(env->error_code & 1)) 29431fc12dfSblueswir1 info.si_code = TARGET_SEGV_MAPERR; 29531fc12dfSblueswir1 else 29631fc12dfSblueswir1 info.si_code = TARGET_SEGV_ACCERR; 29731fc12dfSblueswir1 info._sifields._sigfault._addr = env->cr[2]; 29831fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 29931fc12dfSblueswir1 break; 30031fc12dfSblueswir1 case EXCP00_DIVZ: 30131fc12dfSblueswir1 #ifndef TARGET_X86_64 30231fc12dfSblueswir1 if (env->eflags & VM_MASK) { 30331fc12dfSblueswir1 handle_vm86_trap(env, trapnr); 30431fc12dfSblueswir1 } else 30531fc12dfSblueswir1 #endif 30631fc12dfSblueswir1 { 30731fc12dfSblueswir1 /* division by zero */ 30831fc12dfSblueswir1 info.si_signo = SIGFPE; 30931fc12dfSblueswir1 info.si_errno = 0; 31031fc12dfSblueswir1 info.si_code = TARGET_FPE_INTDIV; 31131fc12dfSblueswir1 info._sifields._sigfault._addr = env->eip; 31231fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 31331fc12dfSblueswir1 } 31431fc12dfSblueswir1 break; 31531fc12dfSblueswir1 case EXCP01_DB: 31631fc12dfSblueswir1 case EXCP03_INT3: 31731fc12dfSblueswir1 #ifndef TARGET_X86_64 31831fc12dfSblueswir1 if (env->eflags & VM_MASK) { 31931fc12dfSblueswir1 handle_vm86_trap(env, trapnr); 32031fc12dfSblueswir1 } else 32131fc12dfSblueswir1 #endif 32231fc12dfSblueswir1 { 32331fc12dfSblueswir1 info.si_signo = SIGTRAP; 32431fc12dfSblueswir1 info.si_errno = 0; 32531fc12dfSblueswir1 if (trapnr == EXCP01_DB) { 32631fc12dfSblueswir1 info.si_code = TARGET_TRAP_BRKPT; 32731fc12dfSblueswir1 info._sifields._sigfault._addr = env->eip; 32831fc12dfSblueswir1 } else { 32931fc12dfSblueswir1 info.si_code = TARGET_SI_KERNEL; 33031fc12dfSblueswir1 info._sifields._sigfault._addr = 0; 33131fc12dfSblueswir1 } 33231fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 33331fc12dfSblueswir1 } 33431fc12dfSblueswir1 break; 33531fc12dfSblueswir1 case EXCP04_INTO: 33631fc12dfSblueswir1 case EXCP05_BOUND: 33731fc12dfSblueswir1 #ifndef TARGET_X86_64 33831fc12dfSblueswir1 if (env->eflags & VM_MASK) { 33931fc12dfSblueswir1 handle_vm86_trap(env, trapnr); 34031fc12dfSblueswir1 } else 34131fc12dfSblueswir1 #endif 34231fc12dfSblueswir1 { 34331fc12dfSblueswir1 info.si_signo = SIGSEGV; 34431fc12dfSblueswir1 info.si_errno = 0; 34531fc12dfSblueswir1 info.si_code = TARGET_SI_KERNEL; 34631fc12dfSblueswir1 info._sifields._sigfault._addr = 0; 34731fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 34831fc12dfSblueswir1 } 34931fc12dfSblueswir1 break; 35031fc12dfSblueswir1 case EXCP06_ILLOP: 35131fc12dfSblueswir1 info.si_signo = SIGILL; 35231fc12dfSblueswir1 info.si_errno = 0; 35331fc12dfSblueswir1 info.si_code = TARGET_ILL_ILLOPN; 35431fc12dfSblueswir1 info._sifields._sigfault._addr = env->eip; 35531fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 35631fc12dfSblueswir1 break; 35731fc12dfSblueswir1 #endif 35831fc12dfSblueswir1 case EXCP_INTERRUPT: 35931fc12dfSblueswir1 /* just indicate that signals should be handled asap */ 36031fc12dfSblueswir1 break; 36131fc12dfSblueswir1 #if 0 36231fc12dfSblueswir1 case EXCP_DEBUG: 36331fc12dfSblueswir1 { 36431fc12dfSblueswir1 int sig; 36531fc12dfSblueswir1 36631fc12dfSblueswir1 sig = gdb_handlesig (env, TARGET_SIGTRAP); 36731fc12dfSblueswir1 if (sig) 36831fc12dfSblueswir1 { 36931fc12dfSblueswir1 info.si_signo = sig; 37031fc12dfSblueswir1 info.si_errno = 0; 37131fc12dfSblueswir1 info.si_code = TARGET_TRAP_BRKPT; 37231fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 37331fc12dfSblueswir1 } 37431fc12dfSblueswir1 } 37531fc12dfSblueswir1 break; 37631fc12dfSblueswir1 #endif 37731fc12dfSblueswir1 default: 37831fc12dfSblueswir1 pc = env->segs[R_CS].base + env->eip; 37931fc12dfSblueswir1 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n", 38031fc12dfSblueswir1 (long)pc, trapnr); 38131fc12dfSblueswir1 abort(); 38231fc12dfSblueswir1 } 38331fc12dfSblueswir1 process_pending_signals(env); 38431fc12dfSblueswir1 } 38531fc12dfSblueswir1 } 38631fc12dfSblueswir1 #endif 38731fc12dfSblueswir1 38884778508Sblueswir1 #ifdef TARGET_SPARC 38984778508Sblueswir1 #define SPARC64_STACK_BIAS 2047 39084778508Sblueswir1 39184778508Sblueswir1 //#define DEBUG_WIN 39284778508Sblueswir1 /* WARNING: dealing with register windows _is_ complicated. More info 39384778508Sblueswir1 can be found at http://www.sics.se/~psm/sparcstack.html */ 39484778508Sblueswir1 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index) 39584778508Sblueswir1 { 39684778508Sblueswir1 index = (index + cwp * 16) % (16 * env->nwindows); 39784778508Sblueswir1 /* wrap handling : if cwp is on the last window, then we use the 39884778508Sblueswir1 registers 'after' the end */ 39984778508Sblueswir1 if (index < 8 && env->cwp == env->nwindows - 1) 40084778508Sblueswir1 index += 16 * env->nwindows; 40184778508Sblueswir1 return index; 40284778508Sblueswir1 } 40384778508Sblueswir1 40484778508Sblueswir1 /* save the register window 'cwp1' */ 40584778508Sblueswir1 static inline void save_window_offset(CPUSPARCState *env, int cwp1) 40684778508Sblueswir1 { 40784778508Sblueswir1 unsigned int i; 40884778508Sblueswir1 abi_ulong sp_ptr; 40984778508Sblueswir1 41084778508Sblueswir1 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)]; 41184778508Sblueswir1 #ifdef TARGET_SPARC64 41284778508Sblueswir1 if (sp_ptr & 3) 41384778508Sblueswir1 sp_ptr += SPARC64_STACK_BIAS; 41484778508Sblueswir1 #endif 41584778508Sblueswir1 #if defined(DEBUG_WIN) 41684778508Sblueswir1 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n", 41784778508Sblueswir1 sp_ptr, cwp1); 41884778508Sblueswir1 #endif 41984778508Sblueswir1 for(i = 0; i < 16; i++) { 42084778508Sblueswir1 /* FIXME - what to do if put_user() fails? */ 42184778508Sblueswir1 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr); 42284778508Sblueswir1 sp_ptr += sizeof(abi_ulong); 42384778508Sblueswir1 } 42484778508Sblueswir1 } 42584778508Sblueswir1 42684778508Sblueswir1 static void save_window(CPUSPARCState *env) 42784778508Sblueswir1 { 42884778508Sblueswir1 #ifndef TARGET_SPARC64 42984778508Sblueswir1 unsigned int new_wim; 43084778508Sblueswir1 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) & 43184778508Sblueswir1 ((1LL << env->nwindows) - 1); 43284778508Sblueswir1 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2)); 43384778508Sblueswir1 env->wim = new_wim; 43484778508Sblueswir1 #else 43584778508Sblueswir1 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2)); 43684778508Sblueswir1 env->cansave++; 43784778508Sblueswir1 env->canrestore--; 43884778508Sblueswir1 #endif 43984778508Sblueswir1 } 44084778508Sblueswir1 44184778508Sblueswir1 static void restore_window(CPUSPARCState *env) 44284778508Sblueswir1 { 44384778508Sblueswir1 #ifndef TARGET_SPARC64 44484778508Sblueswir1 unsigned int new_wim; 44584778508Sblueswir1 #endif 44684778508Sblueswir1 unsigned int i, cwp1; 44784778508Sblueswir1 abi_ulong sp_ptr; 44884778508Sblueswir1 44984778508Sblueswir1 #ifndef TARGET_SPARC64 45084778508Sblueswir1 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) & 45184778508Sblueswir1 ((1LL << env->nwindows) - 1); 45284778508Sblueswir1 #endif 45384778508Sblueswir1 45484778508Sblueswir1 /* restore the invalid window */ 45584778508Sblueswir1 cwp1 = cpu_cwp_inc(env, env->cwp + 1); 45684778508Sblueswir1 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)]; 45784778508Sblueswir1 #ifdef TARGET_SPARC64 45884778508Sblueswir1 if (sp_ptr & 3) 45984778508Sblueswir1 sp_ptr += SPARC64_STACK_BIAS; 46084778508Sblueswir1 #endif 46184778508Sblueswir1 #if defined(DEBUG_WIN) 46284778508Sblueswir1 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n", 46384778508Sblueswir1 sp_ptr, cwp1); 46484778508Sblueswir1 #endif 46584778508Sblueswir1 for(i = 0; i < 16; i++) { 46684778508Sblueswir1 /* FIXME - what to do if get_user() fails? */ 46784778508Sblueswir1 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr); 46884778508Sblueswir1 sp_ptr += sizeof(abi_ulong); 46984778508Sblueswir1 } 47084778508Sblueswir1 #ifdef TARGET_SPARC64 47184778508Sblueswir1 env->canrestore++; 47284778508Sblueswir1 if (env->cleanwin < env->nwindows - 1) 47384778508Sblueswir1 env->cleanwin++; 47484778508Sblueswir1 env->cansave--; 47584778508Sblueswir1 #else 47684778508Sblueswir1 env->wim = new_wim; 47784778508Sblueswir1 #endif 47884778508Sblueswir1 } 47984778508Sblueswir1 48084778508Sblueswir1 static void flush_windows(CPUSPARCState *env) 48184778508Sblueswir1 { 48284778508Sblueswir1 int offset, cwp1; 48384778508Sblueswir1 48484778508Sblueswir1 offset = 1; 48584778508Sblueswir1 for(;;) { 48684778508Sblueswir1 /* if restore would invoke restore_window(), then we can stop */ 48784778508Sblueswir1 cwp1 = cpu_cwp_inc(env, env->cwp + offset); 48884778508Sblueswir1 #ifndef TARGET_SPARC64 48984778508Sblueswir1 if (env->wim & (1 << cwp1)) 49084778508Sblueswir1 break; 49184778508Sblueswir1 #else 49284778508Sblueswir1 if (env->canrestore == 0) 49384778508Sblueswir1 break; 49484778508Sblueswir1 env->cansave++; 49584778508Sblueswir1 env->canrestore--; 49684778508Sblueswir1 #endif 49784778508Sblueswir1 save_window_offset(env, cwp1); 49884778508Sblueswir1 offset++; 49984778508Sblueswir1 } 50084778508Sblueswir1 cwp1 = cpu_cwp_inc(env, env->cwp + 1); 50184778508Sblueswir1 #ifndef TARGET_SPARC64 50284778508Sblueswir1 /* set wim so that restore will reload the registers */ 50384778508Sblueswir1 env->wim = 1 << cwp1; 50484778508Sblueswir1 #endif 50584778508Sblueswir1 #if defined(DEBUG_WIN) 50684778508Sblueswir1 printf("flush_windows: nb=%d\n", offset - 1); 50784778508Sblueswir1 #endif 50884778508Sblueswir1 } 50984778508Sblueswir1 51078cfb07fSJuergen Lock void cpu_loop(CPUSPARCState *env) 51184778508Sblueswir1 { 512878096eeSAndreas Färber CPUState *cs = CPU(sparc_env_get_cpu(env)); 51384778508Sblueswir1 int trapnr, ret, syscall_nr; 51484778508Sblueswir1 //target_siginfo_t info; 51584778508Sblueswir1 51684778508Sblueswir1 while (1) { 517*ea3e9847SPeter Crosthwaite trapnr = cpu_sparc_exec(cs); 51884778508Sblueswir1 51984778508Sblueswir1 switch (trapnr) { 52077b9435fSblueswir1 #ifndef TARGET_SPARC64 52177b9435fSblueswir1 case 0x80: 52277b9435fSblueswir1 #else 52378cfb07fSJuergen Lock /* FreeBSD uses 0x141 for syscalls too */ 52478cfb07fSJuergen Lock case 0x141: 52578cfb07fSJuergen Lock if (bsd_type != target_freebsd) 52678cfb07fSJuergen Lock goto badtrap; 52784778508Sblueswir1 case 0x100: 52877b9435fSblueswir1 #endif 52984778508Sblueswir1 syscall_nr = env->gregs[1]; 53084778508Sblueswir1 if (bsd_type == target_freebsd) 53184778508Sblueswir1 ret = do_freebsd_syscall(env, syscall_nr, 53284778508Sblueswir1 env->regwptr[0], env->regwptr[1], 53384778508Sblueswir1 env->regwptr[2], env->regwptr[3], 53478cfb07fSJuergen Lock env->regwptr[4], env->regwptr[5], 0, 0); 53584778508Sblueswir1 else if (bsd_type == target_netbsd) 53684778508Sblueswir1 ret = do_netbsd_syscall(env, syscall_nr, 53784778508Sblueswir1 env->regwptr[0], env->regwptr[1], 53884778508Sblueswir1 env->regwptr[2], env->regwptr[3], 53984778508Sblueswir1 env->regwptr[4], env->regwptr[5]); 540cdba95bdSblueswir1 else { //if (bsd_type == target_openbsd) 541cdba95bdSblueswir1 #if defined(TARGET_SPARC64) 542cdba95bdSblueswir1 syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG | 543cdba95bdSblueswir1 TARGET_OPENBSD_SYSCALL_G2RFLAG); 544cdba95bdSblueswir1 #endif 54584778508Sblueswir1 ret = do_openbsd_syscall(env, syscall_nr, 54684778508Sblueswir1 env->regwptr[0], env->regwptr[1], 54784778508Sblueswir1 env->regwptr[2], env->regwptr[3], 54884778508Sblueswir1 env->regwptr[4], env->regwptr[5]); 549cdba95bdSblueswir1 } 55084778508Sblueswir1 if ((unsigned int)ret >= (unsigned int)(-515)) { 55178cfb07fSJuergen Lock ret = -ret; 55284778508Sblueswir1 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) 55384778508Sblueswir1 env->xcc |= PSR_CARRY; 55484778508Sblueswir1 #else 55584778508Sblueswir1 env->psr |= PSR_CARRY; 55684778508Sblueswir1 #endif 55784778508Sblueswir1 } else { 55884778508Sblueswir1 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) 55984778508Sblueswir1 env->xcc &= ~PSR_CARRY; 56084778508Sblueswir1 #else 56184778508Sblueswir1 env->psr &= ~PSR_CARRY; 56284778508Sblueswir1 #endif 56384778508Sblueswir1 } 56484778508Sblueswir1 env->regwptr[0] = ret; 56584778508Sblueswir1 /* next instruction */ 566cdba95bdSblueswir1 #if defined(TARGET_SPARC64) 567cdba95bdSblueswir1 if (bsd_type == target_openbsd && 568cdba95bdSblueswir1 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) { 56984778508Sblueswir1 env->pc = env->gregs[2]; 57084778508Sblueswir1 env->npc = env->pc + 4; 571cdba95bdSblueswir1 } else if (bsd_type == target_openbsd && 572cdba95bdSblueswir1 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) { 57384778508Sblueswir1 env->pc = env->gregs[7]; 57484778508Sblueswir1 env->npc = env->pc + 4; 57584778508Sblueswir1 } else { 57684778508Sblueswir1 env->pc = env->npc; 57784778508Sblueswir1 env->npc = env->npc + 4; 57884778508Sblueswir1 } 57984778508Sblueswir1 #else 58084778508Sblueswir1 env->pc = env->npc; 58184778508Sblueswir1 env->npc = env->npc + 4; 58284778508Sblueswir1 #endif 58384778508Sblueswir1 break; 58484778508Sblueswir1 case 0x83: /* flush windows */ 58584778508Sblueswir1 #ifdef TARGET_ABI32 58684778508Sblueswir1 case 0x103: 58784778508Sblueswir1 #endif 58884778508Sblueswir1 flush_windows(env); 58984778508Sblueswir1 /* next instruction */ 59084778508Sblueswir1 env->pc = env->npc; 59184778508Sblueswir1 env->npc = env->npc + 4; 59284778508Sblueswir1 break; 59384778508Sblueswir1 #ifndef TARGET_SPARC64 59484778508Sblueswir1 case TT_WIN_OVF: /* window overflow */ 59584778508Sblueswir1 save_window(env); 59684778508Sblueswir1 break; 59784778508Sblueswir1 case TT_WIN_UNF: /* window underflow */ 59884778508Sblueswir1 restore_window(env); 59984778508Sblueswir1 break; 60084778508Sblueswir1 case TT_TFAULT: 60184778508Sblueswir1 case TT_DFAULT: 60284778508Sblueswir1 #if 0 60384778508Sblueswir1 { 60484778508Sblueswir1 info.si_signo = SIGSEGV; 60584778508Sblueswir1 info.si_errno = 0; 60684778508Sblueswir1 /* XXX: check env->error_code */ 60784778508Sblueswir1 info.si_code = TARGET_SEGV_MAPERR; 60884778508Sblueswir1 info._sifields._sigfault._addr = env->mmuregs[4]; 60984778508Sblueswir1 queue_signal(env, info.si_signo, &info); 61084778508Sblueswir1 } 61184778508Sblueswir1 #endif 61284778508Sblueswir1 break; 61384778508Sblueswir1 #else 61484778508Sblueswir1 case TT_SPILL: /* window overflow */ 61584778508Sblueswir1 save_window(env); 61684778508Sblueswir1 break; 61784778508Sblueswir1 case TT_FILL: /* window underflow */ 61884778508Sblueswir1 restore_window(env); 61984778508Sblueswir1 break; 62084778508Sblueswir1 case TT_TFAULT: 62184778508Sblueswir1 case TT_DFAULT: 62284778508Sblueswir1 #if 0 62384778508Sblueswir1 { 62484778508Sblueswir1 info.si_signo = SIGSEGV; 62584778508Sblueswir1 info.si_errno = 0; 62684778508Sblueswir1 /* XXX: check env->error_code */ 62784778508Sblueswir1 info.si_code = TARGET_SEGV_MAPERR; 62884778508Sblueswir1 if (trapnr == TT_DFAULT) 62984778508Sblueswir1 info._sifields._sigfault._addr = env->dmmuregs[4]; 63084778508Sblueswir1 else 63184778508Sblueswir1 info._sifields._sigfault._addr = env->tsptr->tpc; 63284778508Sblueswir1 //queue_signal(env, info.si_signo, &info); 63384778508Sblueswir1 } 63484778508Sblueswir1 #endif 63584778508Sblueswir1 break; 63684778508Sblueswir1 #endif 63784778508Sblueswir1 case EXCP_INTERRUPT: 63884778508Sblueswir1 /* just indicate that signals should be handled asap */ 63984778508Sblueswir1 break; 64084778508Sblueswir1 case EXCP_DEBUG: 64184778508Sblueswir1 { 64284778508Sblueswir1 int sig; 64384778508Sblueswir1 644db6b81d4SAndreas Färber sig = gdb_handlesig(cs, TARGET_SIGTRAP); 64584778508Sblueswir1 #if 0 64684778508Sblueswir1 if (sig) 64784778508Sblueswir1 { 64884778508Sblueswir1 info.si_signo = sig; 64984778508Sblueswir1 info.si_errno = 0; 65084778508Sblueswir1 info.si_code = TARGET_TRAP_BRKPT; 65184778508Sblueswir1 //queue_signal(env, info.si_signo, &info); 65284778508Sblueswir1 } 65384778508Sblueswir1 #endif 65484778508Sblueswir1 } 65584778508Sblueswir1 break; 65684778508Sblueswir1 default: 65778cfb07fSJuergen Lock #ifdef TARGET_SPARC64 65878cfb07fSJuergen Lock badtrap: 65978cfb07fSJuergen Lock #endif 66084778508Sblueswir1 printf ("Unhandled trap: 0x%x\n", trapnr); 661878096eeSAndreas Färber cpu_dump_state(cs, stderr, fprintf, 0); 66284778508Sblueswir1 exit (1); 66384778508Sblueswir1 } 66484778508Sblueswir1 process_pending_signals (env); 66584778508Sblueswir1 } 66684778508Sblueswir1 } 66784778508Sblueswir1 66884778508Sblueswir1 #endif 66984778508Sblueswir1 67084778508Sblueswir1 static void usage(void) 67184778508Sblueswir1 { 6722e59915dSPaolo Bonzini printf("qemu-" TARGET_NAME " version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n" 6732e59915dSPaolo Bonzini "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n" 67484778508Sblueswir1 "BSD CPU emulator (compiled for %s emulation)\n" 67584778508Sblueswir1 "\n" 67684778508Sblueswir1 "Standard options:\n" 67784778508Sblueswir1 "-h print this help\n" 67884778508Sblueswir1 "-g port wait gdb connection to port\n" 67984778508Sblueswir1 "-L path set the elf interpreter prefix (default=%s)\n" 68084778508Sblueswir1 "-s size set the stack size in bytes (default=%ld)\n" 681c8057f95SPeter Maydell "-cpu model select CPU (-cpu help for list)\n" 68284778508Sblueswir1 "-drop-ld-preload drop LD_PRELOAD for target process\n" 683fc0d96b4SBlue Swirl "-E var=value sets/modifies targets environment variable(s)\n" 684fc0d96b4SBlue Swirl "-U var unsets targets environment variable(s)\n" 6852fa5d9baSBlue Swirl #if defined(CONFIG_USE_GUEST_BASE) 6862fa5d9baSBlue Swirl "-B address set guest_base address to address\n" 6872fa5d9baSBlue Swirl #endif 68884778508Sblueswir1 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n" 68984778508Sblueswir1 "\n" 69084778508Sblueswir1 "Debug options:\n" 691989b697dSPeter Maydell "-d item1[,...] enable logging of specified items\n" 692989b697dSPeter Maydell " (use '-d help' for a list of log items)\n" 693989b697dSPeter Maydell "-D logfile write logs to 'logfile' (default stderr)\n" 69484778508Sblueswir1 "-p pagesize set the host page size to 'pagesize'\n" 6951b530a6dSaurel32 "-singlestep always run in singlestep mode\n" 69684778508Sblueswir1 "-strace log system calls\n" 69784778508Sblueswir1 "\n" 69884778508Sblueswir1 "Environment variables:\n" 69984778508Sblueswir1 "QEMU_STRACE Print system calls and arguments similar to the\n" 70084778508Sblueswir1 " 'strace' program. Enable by setting to any value.\n" 701fc0d96b4SBlue Swirl "You can use -E and -U options to set/unset environment variables\n" 702fc0d96b4SBlue Swirl "for target process. It is possible to provide several variables\n" 703fc0d96b4SBlue Swirl "by repeating the option. For example:\n" 704fc0d96b4SBlue Swirl " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n" 705fc0d96b4SBlue Swirl "Note that if you provide several changes to single variable\n" 706fc0d96b4SBlue Swirl "last change will stay in effect.\n" 70784778508Sblueswir1 , 7082e59915dSPaolo Bonzini TARGET_NAME, 70984778508Sblueswir1 interp_prefix, 710989b697dSPeter Maydell x86_stack_size); 7112d18e637Sblueswir1 exit(1); 71284778508Sblueswir1 } 71384778508Sblueswir1 714dca1173cSAndreas Färber THREAD CPUState *thread_cpu; 71584778508Sblueswir1 71684778508Sblueswir1 /* Assumes contents are already zeroed. */ 71784778508Sblueswir1 void init_task_state(TaskState *ts) 71884778508Sblueswir1 { 71984778508Sblueswir1 int i; 72084778508Sblueswir1 72184778508Sblueswir1 ts->used = 1; 72284778508Sblueswir1 ts->first_free = ts->sigqueue_table; 72384778508Sblueswir1 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) { 72484778508Sblueswir1 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1]; 72584778508Sblueswir1 } 72684778508Sblueswir1 ts->sigqueue_table[i].next = NULL; 72784778508Sblueswir1 } 72884778508Sblueswir1 72984778508Sblueswir1 int main(int argc, char **argv) 73084778508Sblueswir1 { 73184778508Sblueswir1 const char *filename; 73284778508Sblueswir1 const char *cpu_model; 733989b697dSPeter Maydell const char *log_file = NULL; 734c235d738SMatthew Fernandez const char *log_mask = NULL; 73584778508Sblueswir1 struct target_pt_regs regs1, *regs = ®s1; 73684778508Sblueswir1 struct image_info info1, *info = &info1; 73784778508Sblueswir1 TaskState ts1, *ts = &ts1; 7389349b4f9SAndreas Färber CPUArchState *env; 739db6b81d4SAndreas Färber CPUState *cpu; 74084778508Sblueswir1 int optind; 74184778508Sblueswir1 const char *r; 74284778508Sblueswir1 int gdbstub_port = 0; 743fc0d96b4SBlue Swirl char **target_environ, **wrk; 744fc0d96b4SBlue Swirl envlist_t *envlist = 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 #if defined(cpudef_setup) 7640c62de2fSJuergen Lock cpudef_setup(); /* parse cpu definitions in target config file (TBD) */ 7650c62de2fSJuergen Lock #endif 7660c62de2fSJuergen Lock 76784778508Sblueswir1 optind = 1; 76884778508Sblueswir1 for(;;) { 76984778508Sblueswir1 if (optind >= argc) 77084778508Sblueswir1 break; 77184778508Sblueswir1 r = argv[optind]; 77284778508Sblueswir1 if (r[0] != '-') 77384778508Sblueswir1 break; 77484778508Sblueswir1 optind++; 77584778508Sblueswir1 r++; 77684778508Sblueswir1 if (!strcmp(r, "-")) { 77784778508Sblueswir1 break; 77884778508Sblueswir1 } else if (!strcmp(r, "d")) { 779c235d738SMatthew Fernandez if (optind >= argc) { 78084778508Sblueswir1 break; 78184778508Sblueswir1 } 782c235d738SMatthew Fernandez log_mask = argv[optind++]; 783c235d738SMatthew Fernandez } else if (!strcmp(r, "D")) { 784c235d738SMatthew Fernandez if (optind >= argc) { 785c235d738SMatthew Fernandez break; 78684778508Sblueswir1 } 787c235d738SMatthew Fernandez log_file = argv[optind++]; 788fc0d96b4SBlue Swirl } else if (!strcmp(r, "E")) { 789fc0d96b4SBlue Swirl r = argv[optind++]; 790fc0d96b4SBlue Swirl if (envlist_setenv(envlist, r) != 0) 791fc0d96b4SBlue Swirl usage(); 792f66724c9SStefan Weil } else if (!strcmp(r, "ignore-environment")) { 793f66724c9SStefan Weil envlist_free(envlist); 794f66724c9SStefan Weil if ((envlist = envlist_create()) == NULL) { 795f66724c9SStefan Weil (void) fprintf(stderr, "Unable to allocate envlist\n"); 796f66724c9SStefan Weil exit(1); 797f66724c9SStefan Weil } 798fc0d96b4SBlue Swirl } else if (!strcmp(r, "U")) { 799fc0d96b4SBlue Swirl r = argv[optind++]; 800fc0d96b4SBlue Swirl if (envlist_unsetenv(envlist, r) != 0) 801fc0d96b4SBlue Swirl usage(); 80284778508Sblueswir1 } else if (!strcmp(r, "s")) { 80384778508Sblueswir1 r = argv[optind++]; 80484778508Sblueswir1 x86_stack_size = strtol(r, (char **)&r, 0); 80584778508Sblueswir1 if (x86_stack_size <= 0) 80684778508Sblueswir1 usage(); 80784778508Sblueswir1 if (*r == 'M') 80884778508Sblueswir1 x86_stack_size *= 1024 * 1024; 80984778508Sblueswir1 else if (*r == 'k' || *r == 'K') 81084778508Sblueswir1 x86_stack_size *= 1024; 81184778508Sblueswir1 } else if (!strcmp(r, "L")) { 81284778508Sblueswir1 interp_prefix = argv[optind++]; 81384778508Sblueswir1 } else if (!strcmp(r, "p")) { 81484778508Sblueswir1 qemu_host_page_size = atoi(argv[optind++]); 81584778508Sblueswir1 if (qemu_host_page_size == 0 || 81684778508Sblueswir1 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) { 81784778508Sblueswir1 fprintf(stderr, "page size must be a power of two\n"); 81884778508Sblueswir1 exit(1); 81984778508Sblueswir1 } 82084778508Sblueswir1 } else if (!strcmp(r, "g")) { 82184778508Sblueswir1 gdbstub_port = atoi(argv[optind++]); 82284778508Sblueswir1 } else if (!strcmp(r, "r")) { 82384778508Sblueswir1 qemu_uname_release = argv[optind++]; 82484778508Sblueswir1 } else if (!strcmp(r, "cpu")) { 82584778508Sblueswir1 cpu_model = argv[optind++]; 826c8057f95SPeter Maydell if (is_help_option(cpu_model)) { 82784778508Sblueswir1 /* XXX: implement xxx_cpu_list for targets that still miss it */ 82884778508Sblueswir1 #if defined(cpu_list) 82984778508Sblueswir1 cpu_list(stdout, &fprintf); 83084778508Sblueswir1 #endif 8312d18e637Sblueswir1 exit(1); 83284778508Sblueswir1 } 8332fa5d9baSBlue Swirl #if defined(CONFIG_USE_GUEST_BASE) 8342fa5d9baSBlue Swirl } else if (!strcmp(r, "B")) { 8352fa5d9baSBlue Swirl guest_base = strtol(argv[optind++], NULL, 0); 8362fa5d9baSBlue Swirl have_guest_base = 1; 8372fa5d9baSBlue Swirl #endif 83884778508Sblueswir1 } else if (!strcmp(r, "drop-ld-preload")) { 839fc0d96b4SBlue Swirl (void) envlist_unsetenv(envlist, "LD_PRELOAD"); 84084778508Sblueswir1 } else if (!strcmp(r, "bsd")) { 84184778508Sblueswir1 if (!strcasecmp(argv[optind], "freebsd")) { 84284778508Sblueswir1 bsd_type = target_freebsd; 84384778508Sblueswir1 } else if (!strcasecmp(argv[optind], "netbsd")) { 84484778508Sblueswir1 bsd_type = target_netbsd; 84584778508Sblueswir1 } else if (!strcasecmp(argv[optind], "openbsd")) { 84684778508Sblueswir1 bsd_type = target_openbsd; 84784778508Sblueswir1 } else { 84884778508Sblueswir1 usage(); 84984778508Sblueswir1 } 85084778508Sblueswir1 optind++; 8511b530a6dSaurel32 } else if (!strcmp(r, "singlestep")) { 8521b530a6dSaurel32 singlestep = 1; 85384778508Sblueswir1 } else if (!strcmp(r, "strace")) { 85484778508Sblueswir1 do_strace = 1; 85584778508Sblueswir1 } else 85684778508Sblueswir1 { 85784778508Sblueswir1 usage(); 85884778508Sblueswir1 } 85984778508Sblueswir1 } 86084778508Sblueswir1 861c235d738SMatthew Fernandez /* init debug */ 8629a7e5424SPeter Maydell qemu_set_log_filename(log_file); 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 87984778508Sblueswir1 /* Zero out regs */ 88084778508Sblueswir1 memset(regs, 0, sizeof(struct target_pt_regs)); 88184778508Sblueswir1 88284778508Sblueswir1 /* Zero out image_info */ 88384778508Sblueswir1 memset(info, 0, sizeof(struct image_info)); 88484778508Sblueswir1 88584778508Sblueswir1 /* Scan interp_prefix dir for replacement files. */ 88684778508Sblueswir1 init_paths(interp_prefix); 88784778508Sblueswir1 88884778508Sblueswir1 if (cpu_model == NULL) { 88931fc12dfSblueswir1 #if defined(TARGET_I386) 89031fc12dfSblueswir1 #ifdef TARGET_X86_64 89131fc12dfSblueswir1 cpu_model = "qemu64"; 89231fc12dfSblueswir1 #else 89331fc12dfSblueswir1 cpu_model = "qemu32"; 89431fc12dfSblueswir1 #endif 89531fc12dfSblueswir1 #elif defined(TARGET_SPARC) 89684778508Sblueswir1 #ifdef TARGET_SPARC64 89784778508Sblueswir1 cpu_model = "TI UltraSparc II"; 89884778508Sblueswir1 #else 89984778508Sblueswir1 cpu_model = "Fujitsu MB86904"; 90084778508Sblueswir1 #endif 90184778508Sblueswir1 #else 90284778508Sblueswir1 cpu_model = "any"; 90384778508Sblueswir1 #endif 90484778508Sblueswir1 } 905d5ab9713SJan Kiszka tcg_exec_init(0); 90684778508Sblueswir1 /* NOTE: we need to init the CPU at this stage to get 90784778508Sblueswir1 qemu_host_page_size */ 9082994fd96SEduardo Habkost cpu = cpu_init(cpu_model); 9092994fd96SEduardo Habkost if (!cpu) { 91084778508Sblueswir1 fprintf(stderr, "Unable to find CPU definition\n"); 91184778508Sblueswir1 exit(1); 91284778508Sblueswir1 } 9132994fd96SEduardo Habkost env = cpu->env_ptr; 91477868120SAndreas Färber #if defined(TARGET_SPARC) || defined(TARGET_PPC) 915db6b81d4SAndreas Färber cpu_reset(cpu); 916b55a37c9SBlue Swirl #endif 917db6b81d4SAndreas Färber thread_cpu = cpu; 91884778508Sblueswir1 91984778508Sblueswir1 if (getenv("QEMU_STRACE")) { 92084778508Sblueswir1 do_strace = 1; 92184778508Sblueswir1 } 92284778508Sblueswir1 923fc0d96b4SBlue Swirl target_environ = envlist_to_environ(envlist, NULL); 924fc0d96b4SBlue Swirl envlist_free(envlist); 92584778508Sblueswir1 9262fa5d9baSBlue Swirl #if defined(CONFIG_USE_GUEST_BASE) 9272fa5d9baSBlue Swirl /* 9282fa5d9baSBlue Swirl * Now that page sizes are configured in cpu_init() we can do 9292fa5d9baSBlue Swirl * proper page alignment for guest_base. 9302fa5d9baSBlue Swirl */ 9312fa5d9baSBlue Swirl guest_base = HOST_PAGE_ALIGN(guest_base); 9322fa5d9baSBlue Swirl 9332fa5d9baSBlue Swirl /* 9342fa5d9baSBlue Swirl * Read in mmap_min_addr kernel parameter. This value is used 9352fa5d9baSBlue Swirl * When loading the ELF image to determine whether guest_base 9362fa5d9baSBlue Swirl * is needed. 9372fa5d9baSBlue Swirl * 9382fa5d9baSBlue Swirl * When user has explicitly set the quest base, we skip this 9392fa5d9baSBlue Swirl * test. 9402fa5d9baSBlue Swirl */ 9412fa5d9baSBlue Swirl if (!have_guest_base) { 9422fa5d9baSBlue Swirl FILE *fp; 9432fa5d9baSBlue Swirl 9442fa5d9baSBlue Swirl if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) { 9452fa5d9baSBlue Swirl unsigned long tmp; 9462fa5d9baSBlue Swirl if (fscanf(fp, "%lu", &tmp) == 1) { 9472fa5d9baSBlue Swirl mmap_min_addr = tmp; 9482fa5d9baSBlue Swirl qemu_log("host mmap_min_addr=0x%lx\n", mmap_min_addr); 9492fa5d9baSBlue Swirl } 9502fa5d9baSBlue Swirl fclose(fp); 9512fa5d9baSBlue Swirl } 9522fa5d9baSBlue Swirl } 9532fa5d9baSBlue Swirl #endif /* CONFIG_USE_GUEST_BASE */ 95484778508Sblueswir1 95584778508Sblueswir1 if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) { 95684778508Sblueswir1 printf("Error loading %s\n", filename); 95784778508Sblueswir1 _exit(1); 95884778508Sblueswir1 } 95984778508Sblueswir1 96084778508Sblueswir1 for (wrk = target_environ; *wrk; wrk++) { 96184778508Sblueswir1 free(*wrk); 96284778508Sblueswir1 } 96384778508Sblueswir1 96484778508Sblueswir1 free(target_environ); 96584778508Sblueswir1 9662e77eac6Sblueswir1 if (qemu_log_enabled()) { 9672fa5d9baSBlue Swirl #if defined(CONFIG_USE_GUEST_BASE) 9682fa5d9baSBlue Swirl qemu_log("guest_base 0x%lx\n", guest_base); 9692fa5d9baSBlue Swirl #endif 97093fcfe39Saliguori log_page_dump(); 97184778508Sblueswir1 97293fcfe39Saliguori qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk); 97393fcfe39Saliguori qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code); 97493fcfe39Saliguori qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n", 97584778508Sblueswir1 info->start_code); 97693fcfe39Saliguori qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n", 97784778508Sblueswir1 info->start_data); 97893fcfe39Saliguori qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data); 97993fcfe39Saliguori qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n", 98084778508Sblueswir1 info->start_stack); 98193fcfe39Saliguori qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk); 98293fcfe39Saliguori qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry); 9832e77eac6Sblueswir1 } 98484778508Sblueswir1 98584778508Sblueswir1 target_set_brk(info->brk); 98684778508Sblueswir1 syscall_init(); 98784778508Sblueswir1 signal_init(); 98884778508Sblueswir1 9899002ec79SRichard Henderson #if defined(CONFIG_USE_GUEST_BASE) 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 #endif 9959002ec79SRichard Henderson 99684778508Sblueswir1 /* build Task State */ 99784778508Sblueswir1 memset(ts, 0, sizeof(TaskState)); 99884778508Sblueswir1 init_task_state(ts); 99984778508Sblueswir1 ts->info = info; 10000429a971SAndreas Färber cpu->opaque = ts; 100184778508Sblueswir1 100231fc12dfSblueswir1 #if defined(TARGET_I386) 100331fc12dfSblueswir1 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK; 1004b98dbc90SPaolo Bonzini env->hflags |= HF_PE_MASK | HF_CPL_MASK; 10050514ef2fSEduardo Habkost if (env->features[FEAT_1_EDX] & CPUID_SSE) { 100631fc12dfSblueswir1 env->cr[4] |= CR4_OSFXSR_MASK; 100731fc12dfSblueswir1 env->hflags |= HF_OSFXSR_MASK; 100831fc12dfSblueswir1 } 100931fc12dfSblueswir1 #ifndef TARGET_ABI32 101031fc12dfSblueswir1 /* enable 64 bit mode if possible */ 10110514ef2fSEduardo Habkost if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) { 101231fc12dfSblueswir1 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n"); 101331fc12dfSblueswir1 exit(1); 101431fc12dfSblueswir1 } 101531fc12dfSblueswir1 env->cr[4] |= CR4_PAE_MASK; 101631fc12dfSblueswir1 env->efer |= MSR_EFER_LMA | MSR_EFER_LME; 101731fc12dfSblueswir1 env->hflags |= HF_LMA_MASK; 101831fc12dfSblueswir1 #endif 101931fc12dfSblueswir1 102031fc12dfSblueswir1 /* flags setup : we activate the IRQs by default as in user mode */ 102131fc12dfSblueswir1 env->eflags |= IF_MASK; 102231fc12dfSblueswir1 102331fc12dfSblueswir1 /* linux register setup */ 102431fc12dfSblueswir1 #ifndef TARGET_ABI32 102531fc12dfSblueswir1 env->regs[R_EAX] = regs->rax; 102631fc12dfSblueswir1 env->regs[R_EBX] = regs->rbx; 102731fc12dfSblueswir1 env->regs[R_ECX] = regs->rcx; 102831fc12dfSblueswir1 env->regs[R_EDX] = regs->rdx; 102931fc12dfSblueswir1 env->regs[R_ESI] = regs->rsi; 103031fc12dfSblueswir1 env->regs[R_EDI] = regs->rdi; 103131fc12dfSblueswir1 env->regs[R_EBP] = regs->rbp; 103231fc12dfSblueswir1 env->regs[R_ESP] = regs->rsp; 103331fc12dfSblueswir1 env->eip = regs->rip; 103431fc12dfSblueswir1 #else 103531fc12dfSblueswir1 env->regs[R_EAX] = regs->eax; 103631fc12dfSblueswir1 env->regs[R_EBX] = regs->ebx; 103731fc12dfSblueswir1 env->regs[R_ECX] = regs->ecx; 103831fc12dfSblueswir1 env->regs[R_EDX] = regs->edx; 103931fc12dfSblueswir1 env->regs[R_ESI] = regs->esi; 104031fc12dfSblueswir1 env->regs[R_EDI] = regs->edi; 104131fc12dfSblueswir1 env->regs[R_EBP] = regs->ebp; 104231fc12dfSblueswir1 env->regs[R_ESP] = regs->esp; 104331fc12dfSblueswir1 env->eip = regs->eip; 104431fc12dfSblueswir1 #endif 104531fc12dfSblueswir1 104631fc12dfSblueswir1 /* linux interrupt setup */ 104731fc12dfSblueswir1 #ifndef TARGET_ABI32 104831fc12dfSblueswir1 env->idt.limit = 511; 104931fc12dfSblueswir1 #else 105031fc12dfSblueswir1 env->idt.limit = 255; 105131fc12dfSblueswir1 #endif 105231fc12dfSblueswir1 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1), 105331fc12dfSblueswir1 PROT_READ|PROT_WRITE, 105431fc12dfSblueswir1 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); 105531fc12dfSblueswir1 idt_table = g2h(env->idt.base); 105631fc12dfSblueswir1 set_idt(0, 0); 105731fc12dfSblueswir1 set_idt(1, 0); 105831fc12dfSblueswir1 set_idt(2, 0); 105931fc12dfSblueswir1 set_idt(3, 3); 106031fc12dfSblueswir1 set_idt(4, 3); 106131fc12dfSblueswir1 set_idt(5, 0); 106231fc12dfSblueswir1 set_idt(6, 0); 106331fc12dfSblueswir1 set_idt(7, 0); 106431fc12dfSblueswir1 set_idt(8, 0); 106531fc12dfSblueswir1 set_idt(9, 0); 106631fc12dfSblueswir1 set_idt(10, 0); 106731fc12dfSblueswir1 set_idt(11, 0); 106831fc12dfSblueswir1 set_idt(12, 0); 106931fc12dfSblueswir1 set_idt(13, 0); 107031fc12dfSblueswir1 set_idt(14, 0); 107131fc12dfSblueswir1 set_idt(15, 0); 107231fc12dfSblueswir1 set_idt(16, 0); 107331fc12dfSblueswir1 set_idt(17, 0); 107431fc12dfSblueswir1 set_idt(18, 0); 107531fc12dfSblueswir1 set_idt(19, 0); 107631fc12dfSblueswir1 set_idt(0x80, 3); 107731fc12dfSblueswir1 107831fc12dfSblueswir1 /* linux segment setup */ 107931fc12dfSblueswir1 { 108031fc12dfSblueswir1 uint64_t *gdt_table; 108131fc12dfSblueswir1 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES, 108231fc12dfSblueswir1 PROT_READ|PROT_WRITE, 108331fc12dfSblueswir1 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); 108431fc12dfSblueswir1 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1; 108531fc12dfSblueswir1 gdt_table = g2h(env->gdt.base); 108631fc12dfSblueswir1 #ifdef TARGET_ABI32 108731fc12dfSblueswir1 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff, 108831fc12dfSblueswir1 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 108931fc12dfSblueswir1 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT)); 109031fc12dfSblueswir1 #else 109131fc12dfSblueswir1 /* 64 bit code segment */ 109231fc12dfSblueswir1 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff, 109331fc12dfSblueswir1 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 109431fc12dfSblueswir1 DESC_L_MASK | 109531fc12dfSblueswir1 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT)); 109631fc12dfSblueswir1 #endif 109731fc12dfSblueswir1 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff, 109831fc12dfSblueswir1 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 109931fc12dfSblueswir1 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT)); 110031fc12dfSblueswir1 } 110131fc12dfSblueswir1 110231fc12dfSblueswir1 cpu_x86_load_seg(env, R_CS, __USER_CS); 110331fc12dfSblueswir1 cpu_x86_load_seg(env, R_SS, __USER_DS); 110431fc12dfSblueswir1 #ifdef TARGET_ABI32 110531fc12dfSblueswir1 cpu_x86_load_seg(env, R_DS, __USER_DS); 110631fc12dfSblueswir1 cpu_x86_load_seg(env, R_ES, __USER_DS); 110731fc12dfSblueswir1 cpu_x86_load_seg(env, R_FS, __USER_DS); 110831fc12dfSblueswir1 cpu_x86_load_seg(env, R_GS, __USER_DS); 110931fc12dfSblueswir1 /* This hack makes Wine work... */ 111031fc12dfSblueswir1 env->segs[R_FS].selector = 0; 111131fc12dfSblueswir1 #else 111231fc12dfSblueswir1 cpu_x86_load_seg(env, R_DS, 0); 111331fc12dfSblueswir1 cpu_x86_load_seg(env, R_ES, 0); 111431fc12dfSblueswir1 cpu_x86_load_seg(env, R_FS, 0); 111531fc12dfSblueswir1 cpu_x86_load_seg(env, R_GS, 0); 111631fc12dfSblueswir1 #endif 111731fc12dfSblueswir1 #elif defined(TARGET_SPARC) 111884778508Sblueswir1 { 111984778508Sblueswir1 int i; 112084778508Sblueswir1 env->pc = regs->pc; 112184778508Sblueswir1 env->npc = regs->npc; 112284778508Sblueswir1 env->y = regs->y; 112384778508Sblueswir1 for(i = 0; i < 8; i++) 112484778508Sblueswir1 env->gregs[i] = regs->u_regs[i]; 112584778508Sblueswir1 for(i = 0; i < 8; i++) 112684778508Sblueswir1 env->regwptr[i] = regs->u_regs[i + 8]; 112784778508Sblueswir1 } 112884778508Sblueswir1 #else 112984778508Sblueswir1 #error unsupported target CPU 113084778508Sblueswir1 #endif 113184778508Sblueswir1 113284778508Sblueswir1 if (gdbstub_port) { 113384778508Sblueswir1 gdbserver_start (gdbstub_port); 1134db6b81d4SAndreas Färber gdb_handlesig(cpu, 0); 113584778508Sblueswir1 } 113678cfb07fSJuergen Lock cpu_loop(env); 113784778508Sblueswir1 /* never exits */ 113884778508Sblueswir1 return 0; 113984778508Sblueswir1 } 1140