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 */ 3284778508Sblueswir1 #include "exec-all.h" 3384778508Sblueswir1 34*2ae144c9SBlue Swirl #include "qemu-timer.h" 35fc0d96b4SBlue Swirl #include "envlist.h" 36fc0d96b4SBlue Swirl 3784778508Sblueswir1 #define DEBUG_LOGFILE "/tmp/qemu.log" 3884778508Sblueswir1 391b530a6dSaurel32 int singlestep; 402fa5d9baSBlue Swirl #if defined(CONFIG_USE_GUEST_BASE) 412fa5d9baSBlue Swirl unsigned long mmap_min_addr; 422fa5d9baSBlue Swirl unsigned long guest_base; 432fa5d9baSBlue Swirl int have_guest_base; 442fa5d9baSBlue Swirl #endif 451b530a6dSaurel32 4684778508Sblueswir1 static const char *interp_prefix = CONFIG_QEMU_PREFIX; 4784778508Sblueswir1 const char *qemu_uname_release = CONFIG_UNAME_RELEASE; 4884778508Sblueswir1 extern char **environ; 4978cfb07fSJuergen Lock enum BSDType bsd_type; 5084778508Sblueswir1 5184778508Sblueswir1 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so 5284778508Sblueswir1 we allocate a bigger stack. Need a better solution, for example 5384778508Sblueswir1 by remapping the process stack directly at the right place */ 5484778508Sblueswir1 unsigned long x86_stack_size = 512 * 1024; 5584778508Sblueswir1 5684778508Sblueswir1 void gemu_log(const char *fmt, ...) 5784778508Sblueswir1 { 5884778508Sblueswir1 va_list ap; 5984778508Sblueswir1 6084778508Sblueswir1 va_start(ap, fmt); 6184778508Sblueswir1 vfprintf(stderr, fmt, ap); 6284778508Sblueswir1 va_end(ap); 6384778508Sblueswir1 } 649399f095Sblueswir1 6531fc12dfSblueswir1 #if defined(TARGET_I386) 6631fc12dfSblueswir1 int cpu_get_pic_interrupt(CPUState *env) 6731fc12dfSblueswir1 { 6831fc12dfSblueswir1 return -1; 6931fc12dfSblueswir1 } 7031fc12dfSblueswir1 #endif 7131fc12dfSblueswir1 729399f095Sblueswir1 /* These are no-ops because we are not threadsafe. */ 739399f095Sblueswir1 static inline void cpu_exec_start(CPUState *env) 749399f095Sblueswir1 { 759399f095Sblueswir1 } 769399f095Sblueswir1 779399f095Sblueswir1 static inline void cpu_exec_end(CPUState *env) 789399f095Sblueswir1 { 799399f095Sblueswir1 } 809399f095Sblueswir1 819399f095Sblueswir1 static inline void start_exclusive(void) 829399f095Sblueswir1 { 839399f095Sblueswir1 } 849399f095Sblueswir1 859399f095Sblueswir1 static inline void end_exclusive(void) 869399f095Sblueswir1 { 879399f095Sblueswir1 } 889399f095Sblueswir1 899399f095Sblueswir1 void fork_start(void) 909399f095Sblueswir1 { 919399f095Sblueswir1 } 929399f095Sblueswir1 939399f095Sblueswir1 void fork_end(int child) 949399f095Sblueswir1 { 959399f095Sblueswir1 if (child) { 969399f095Sblueswir1 gdbserver_fork(thread_env); 979399f095Sblueswir1 } 989399f095Sblueswir1 } 999399f095Sblueswir1 1009399f095Sblueswir1 void cpu_list_lock(void) 1019399f095Sblueswir1 { 1029399f095Sblueswir1 } 1039399f095Sblueswir1 1049399f095Sblueswir1 void cpu_list_unlock(void) 1059399f095Sblueswir1 { 1069399f095Sblueswir1 } 1079399f095Sblueswir1 10831fc12dfSblueswir1 #ifdef TARGET_I386 10931fc12dfSblueswir1 /***********************************************************/ 11031fc12dfSblueswir1 /* CPUX86 core interface */ 11131fc12dfSblueswir1 11231fc12dfSblueswir1 void cpu_smm_update(CPUState *env) 11331fc12dfSblueswir1 { 11431fc12dfSblueswir1 } 11531fc12dfSblueswir1 11631fc12dfSblueswir1 uint64_t cpu_get_tsc(CPUX86State *env) 11731fc12dfSblueswir1 { 11831fc12dfSblueswir1 return cpu_get_real_ticks(); 11931fc12dfSblueswir1 } 12031fc12dfSblueswir1 12131fc12dfSblueswir1 static void write_dt(void *ptr, unsigned long addr, unsigned long limit, 12231fc12dfSblueswir1 int flags) 12331fc12dfSblueswir1 { 12431fc12dfSblueswir1 unsigned int e1, e2; 12531fc12dfSblueswir1 uint32_t *p; 12631fc12dfSblueswir1 e1 = (addr << 16) | (limit & 0xffff); 12731fc12dfSblueswir1 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000); 12831fc12dfSblueswir1 e2 |= flags; 12931fc12dfSblueswir1 p = ptr; 13031fc12dfSblueswir1 p[0] = tswap32(e1); 13131fc12dfSblueswir1 p[1] = tswap32(e2); 13231fc12dfSblueswir1 } 13331fc12dfSblueswir1 13431fc12dfSblueswir1 static uint64_t *idt_table; 13531fc12dfSblueswir1 #ifdef TARGET_X86_64 13631fc12dfSblueswir1 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl, 13731fc12dfSblueswir1 uint64_t addr, unsigned int sel) 13831fc12dfSblueswir1 { 13931fc12dfSblueswir1 uint32_t *p, e1, e2; 14031fc12dfSblueswir1 e1 = (addr & 0xffff) | (sel << 16); 14131fc12dfSblueswir1 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); 14231fc12dfSblueswir1 p = ptr; 14331fc12dfSblueswir1 p[0] = tswap32(e1); 14431fc12dfSblueswir1 p[1] = tswap32(e2); 14531fc12dfSblueswir1 p[2] = tswap32(addr >> 32); 14631fc12dfSblueswir1 p[3] = 0; 14731fc12dfSblueswir1 } 14831fc12dfSblueswir1 /* only dpl matters as we do only user space emulation */ 14931fc12dfSblueswir1 static void set_idt(int n, unsigned int dpl) 15031fc12dfSblueswir1 { 15131fc12dfSblueswir1 set_gate64(idt_table + n * 2, 0, dpl, 0, 0); 15231fc12dfSblueswir1 } 15331fc12dfSblueswir1 #else 15431fc12dfSblueswir1 static void set_gate(void *ptr, unsigned int type, unsigned int dpl, 15531fc12dfSblueswir1 uint32_t addr, unsigned int sel) 15631fc12dfSblueswir1 { 15731fc12dfSblueswir1 uint32_t *p, e1, e2; 15831fc12dfSblueswir1 e1 = (addr & 0xffff) | (sel << 16); 15931fc12dfSblueswir1 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); 16031fc12dfSblueswir1 p = ptr; 16131fc12dfSblueswir1 p[0] = tswap32(e1); 16231fc12dfSblueswir1 p[1] = tswap32(e2); 16331fc12dfSblueswir1 } 16431fc12dfSblueswir1 16531fc12dfSblueswir1 /* only dpl matters as we do only user space emulation */ 16631fc12dfSblueswir1 static void set_idt(int n, unsigned int dpl) 16731fc12dfSblueswir1 { 16831fc12dfSblueswir1 set_gate(idt_table + n, 0, dpl, 0, 0); 16931fc12dfSblueswir1 } 17031fc12dfSblueswir1 #endif 17131fc12dfSblueswir1 17278cfb07fSJuergen Lock void cpu_loop(CPUX86State *env) 17331fc12dfSblueswir1 { 17431fc12dfSblueswir1 int trapnr; 17531fc12dfSblueswir1 abi_ulong pc; 17631fc12dfSblueswir1 //target_siginfo_t info; 17731fc12dfSblueswir1 17831fc12dfSblueswir1 for(;;) { 17931fc12dfSblueswir1 trapnr = cpu_x86_exec(env); 18031fc12dfSblueswir1 switch(trapnr) { 18131fc12dfSblueswir1 case 0x80: 18231fc12dfSblueswir1 /* syscall from int $0x80 */ 18378cfb07fSJuergen Lock if (bsd_type == target_freebsd) { 18478cfb07fSJuergen Lock abi_ulong params = (abi_ulong) env->regs[R_ESP] + 18578cfb07fSJuergen Lock sizeof(int32_t); 18678cfb07fSJuergen Lock int32_t syscall_nr = env->regs[R_EAX]; 18778cfb07fSJuergen Lock int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8; 18878cfb07fSJuergen Lock 18978cfb07fSJuergen Lock if (syscall_nr == TARGET_FREEBSD_NR_syscall) { 19078cfb07fSJuergen Lock get_user_s32(syscall_nr, params); 19178cfb07fSJuergen Lock params += sizeof(int32_t); 19278cfb07fSJuergen Lock } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) { 19378cfb07fSJuergen Lock get_user_s32(syscall_nr, params); 19478cfb07fSJuergen Lock params += sizeof(int64_t); 19578cfb07fSJuergen Lock } 19678cfb07fSJuergen Lock get_user_s32(arg1, params); 19778cfb07fSJuergen Lock params += sizeof(int32_t); 19878cfb07fSJuergen Lock get_user_s32(arg2, params); 19978cfb07fSJuergen Lock params += sizeof(int32_t); 20078cfb07fSJuergen Lock get_user_s32(arg3, params); 20178cfb07fSJuergen Lock params += sizeof(int32_t); 20278cfb07fSJuergen Lock get_user_s32(arg4, params); 20378cfb07fSJuergen Lock params += sizeof(int32_t); 20478cfb07fSJuergen Lock get_user_s32(arg5, params); 20578cfb07fSJuergen Lock params += sizeof(int32_t); 20678cfb07fSJuergen Lock get_user_s32(arg6, params); 20778cfb07fSJuergen Lock params += sizeof(int32_t); 20878cfb07fSJuergen Lock get_user_s32(arg7, params); 20978cfb07fSJuergen Lock params += sizeof(int32_t); 21078cfb07fSJuergen Lock get_user_s32(arg8, params); 21178cfb07fSJuergen Lock env->regs[R_EAX] = do_freebsd_syscall(env, 21278cfb07fSJuergen Lock syscall_nr, 21378cfb07fSJuergen Lock arg1, 21478cfb07fSJuergen Lock arg2, 21578cfb07fSJuergen Lock arg3, 21678cfb07fSJuergen Lock arg4, 21778cfb07fSJuergen Lock arg5, 21878cfb07fSJuergen Lock arg6, 21978cfb07fSJuergen Lock arg7, 22078cfb07fSJuergen Lock arg8); 22178cfb07fSJuergen Lock } else { //if (bsd_type == target_openbsd) 22231fc12dfSblueswir1 env->regs[R_EAX] = do_openbsd_syscall(env, 22331fc12dfSblueswir1 env->regs[R_EAX], 22431fc12dfSblueswir1 env->regs[R_EBX], 22531fc12dfSblueswir1 env->regs[R_ECX], 22631fc12dfSblueswir1 env->regs[R_EDX], 22731fc12dfSblueswir1 env->regs[R_ESI], 22831fc12dfSblueswir1 env->regs[R_EDI], 22931fc12dfSblueswir1 env->regs[R_EBP]); 23078cfb07fSJuergen Lock } 23178cfb07fSJuergen Lock if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) { 23278cfb07fSJuergen Lock env->regs[R_EAX] = -env->regs[R_EAX]; 23378cfb07fSJuergen Lock env->eflags |= CC_C; 23478cfb07fSJuergen Lock } else { 23578cfb07fSJuergen Lock env->eflags &= ~CC_C; 23678cfb07fSJuergen Lock } 23731fc12dfSblueswir1 break; 23831fc12dfSblueswir1 #ifndef TARGET_ABI32 23931fc12dfSblueswir1 case EXCP_SYSCALL: 24078cfb07fSJuergen Lock /* syscall from syscall intruction */ 24178cfb07fSJuergen Lock if (bsd_type == target_freebsd) 24278cfb07fSJuergen Lock env->regs[R_EAX] = do_freebsd_syscall(env, 24378cfb07fSJuergen Lock env->regs[R_EAX], 24478cfb07fSJuergen Lock env->regs[R_EDI], 24578cfb07fSJuergen Lock env->regs[R_ESI], 24678cfb07fSJuergen Lock env->regs[R_EDX], 24778cfb07fSJuergen Lock env->regs[R_ECX], 24878cfb07fSJuergen Lock env->regs[8], 24978cfb07fSJuergen Lock env->regs[9], 0, 0); 25078cfb07fSJuergen Lock else { //if (bsd_type == target_openbsd) 25131fc12dfSblueswir1 env->regs[R_EAX] = do_openbsd_syscall(env, 25231fc12dfSblueswir1 env->regs[R_EAX], 25331fc12dfSblueswir1 env->regs[R_EDI], 25431fc12dfSblueswir1 env->regs[R_ESI], 25531fc12dfSblueswir1 env->regs[R_EDX], 25631fc12dfSblueswir1 env->regs[10], 25731fc12dfSblueswir1 env->regs[8], 25831fc12dfSblueswir1 env->regs[9]); 25978cfb07fSJuergen Lock } 26031fc12dfSblueswir1 env->eip = env->exception_next_eip; 26178cfb07fSJuergen Lock if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) { 26278cfb07fSJuergen Lock env->regs[R_EAX] = -env->regs[R_EAX]; 26378cfb07fSJuergen Lock env->eflags |= CC_C; 26478cfb07fSJuergen Lock } else { 26578cfb07fSJuergen Lock env->eflags &= ~CC_C; 26678cfb07fSJuergen Lock } 26731fc12dfSblueswir1 break; 26831fc12dfSblueswir1 #endif 26931fc12dfSblueswir1 #if 0 27031fc12dfSblueswir1 case EXCP0B_NOSEG: 27131fc12dfSblueswir1 case EXCP0C_STACK: 27231fc12dfSblueswir1 info.si_signo = SIGBUS; 27331fc12dfSblueswir1 info.si_errno = 0; 27431fc12dfSblueswir1 info.si_code = TARGET_SI_KERNEL; 27531fc12dfSblueswir1 info._sifields._sigfault._addr = 0; 27631fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 27731fc12dfSblueswir1 break; 27831fc12dfSblueswir1 case EXCP0D_GPF: 27931fc12dfSblueswir1 /* XXX: potential problem if ABI32 */ 28031fc12dfSblueswir1 #ifndef TARGET_X86_64 28131fc12dfSblueswir1 if (env->eflags & VM_MASK) { 28231fc12dfSblueswir1 handle_vm86_fault(env); 28331fc12dfSblueswir1 } else 28431fc12dfSblueswir1 #endif 28531fc12dfSblueswir1 { 28631fc12dfSblueswir1 info.si_signo = SIGSEGV; 28731fc12dfSblueswir1 info.si_errno = 0; 28831fc12dfSblueswir1 info.si_code = TARGET_SI_KERNEL; 28931fc12dfSblueswir1 info._sifields._sigfault._addr = 0; 29031fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 29131fc12dfSblueswir1 } 29231fc12dfSblueswir1 break; 29331fc12dfSblueswir1 case EXCP0E_PAGE: 29431fc12dfSblueswir1 info.si_signo = SIGSEGV; 29531fc12dfSblueswir1 info.si_errno = 0; 29631fc12dfSblueswir1 if (!(env->error_code & 1)) 29731fc12dfSblueswir1 info.si_code = TARGET_SEGV_MAPERR; 29831fc12dfSblueswir1 else 29931fc12dfSblueswir1 info.si_code = TARGET_SEGV_ACCERR; 30031fc12dfSblueswir1 info._sifields._sigfault._addr = env->cr[2]; 30131fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 30231fc12dfSblueswir1 break; 30331fc12dfSblueswir1 case EXCP00_DIVZ: 30431fc12dfSblueswir1 #ifndef TARGET_X86_64 30531fc12dfSblueswir1 if (env->eflags & VM_MASK) { 30631fc12dfSblueswir1 handle_vm86_trap(env, trapnr); 30731fc12dfSblueswir1 } else 30831fc12dfSblueswir1 #endif 30931fc12dfSblueswir1 { 31031fc12dfSblueswir1 /* division by zero */ 31131fc12dfSblueswir1 info.si_signo = SIGFPE; 31231fc12dfSblueswir1 info.si_errno = 0; 31331fc12dfSblueswir1 info.si_code = TARGET_FPE_INTDIV; 31431fc12dfSblueswir1 info._sifields._sigfault._addr = env->eip; 31531fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 31631fc12dfSblueswir1 } 31731fc12dfSblueswir1 break; 31831fc12dfSblueswir1 case EXCP01_DB: 31931fc12dfSblueswir1 case EXCP03_INT3: 32031fc12dfSblueswir1 #ifndef TARGET_X86_64 32131fc12dfSblueswir1 if (env->eflags & VM_MASK) { 32231fc12dfSblueswir1 handle_vm86_trap(env, trapnr); 32331fc12dfSblueswir1 } else 32431fc12dfSblueswir1 #endif 32531fc12dfSblueswir1 { 32631fc12dfSblueswir1 info.si_signo = SIGTRAP; 32731fc12dfSblueswir1 info.si_errno = 0; 32831fc12dfSblueswir1 if (trapnr == EXCP01_DB) { 32931fc12dfSblueswir1 info.si_code = TARGET_TRAP_BRKPT; 33031fc12dfSblueswir1 info._sifields._sigfault._addr = env->eip; 33131fc12dfSblueswir1 } else { 33231fc12dfSblueswir1 info.si_code = TARGET_SI_KERNEL; 33331fc12dfSblueswir1 info._sifields._sigfault._addr = 0; 33431fc12dfSblueswir1 } 33531fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 33631fc12dfSblueswir1 } 33731fc12dfSblueswir1 break; 33831fc12dfSblueswir1 case EXCP04_INTO: 33931fc12dfSblueswir1 case EXCP05_BOUND: 34031fc12dfSblueswir1 #ifndef TARGET_X86_64 34131fc12dfSblueswir1 if (env->eflags & VM_MASK) { 34231fc12dfSblueswir1 handle_vm86_trap(env, trapnr); 34331fc12dfSblueswir1 } else 34431fc12dfSblueswir1 #endif 34531fc12dfSblueswir1 { 34631fc12dfSblueswir1 info.si_signo = SIGSEGV; 34731fc12dfSblueswir1 info.si_errno = 0; 34831fc12dfSblueswir1 info.si_code = TARGET_SI_KERNEL; 34931fc12dfSblueswir1 info._sifields._sigfault._addr = 0; 35031fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 35131fc12dfSblueswir1 } 35231fc12dfSblueswir1 break; 35331fc12dfSblueswir1 case EXCP06_ILLOP: 35431fc12dfSblueswir1 info.si_signo = SIGILL; 35531fc12dfSblueswir1 info.si_errno = 0; 35631fc12dfSblueswir1 info.si_code = TARGET_ILL_ILLOPN; 35731fc12dfSblueswir1 info._sifields._sigfault._addr = env->eip; 35831fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 35931fc12dfSblueswir1 break; 36031fc12dfSblueswir1 #endif 36131fc12dfSblueswir1 case EXCP_INTERRUPT: 36231fc12dfSblueswir1 /* just indicate that signals should be handled asap */ 36331fc12dfSblueswir1 break; 36431fc12dfSblueswir1 #if 0 36531fc12dfSblueswir1 case EXCP_DEBUG: 36631fc12dfSblueswir1 { 36731fc12dfSblueswir1 int sig; 36831fc12dfSblueswir1 36931fc12dfSblueswir1 sig = gdb_handlesig (env, TARGET_SIGTRAP); 37031fc12dfSblueswir1 if (sig) 37131fc12dfSblueswir1 { 37231fc12dfSblueswir1 info.si_signo = sig; 37331fc12dfSblueswir1 info.si_errno = 0; 37431fc12dfSblueswir1 info.si_code = TARGET_TRAP_BRKPT; 37531fc12dfSblueswir1 queue_signal(env, info.si_signo, &info); 37631fc12dfSblueswir1 } 37731fc12dfSblueswir1 } 37831fc12dfSblueswir1 break; 37931fc12dfSblueswir1 #endif 38031fc12dfSblueswir1 default: 38131fc12dfSblueswir1 pc = env->segs[R_CS].base + env->eip; 38231fc12dfSblueswir1 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n", 38331fc12dfSblueswir1 (long)pc, trapnr); 38431fc12dfSblueswir1 abort(); 38531fc12dfSblueswir1 } 38631fc12dfSblueswir1 process_pending_signals(env); 38731fc12dfSblueswir1 } 38831fc12dfSblueswir1 } 38931fc12dfSblueswir1 #endif 39031fc12dfSblueswir1 39184778508Sblueswir1 #ifdef TARGET_SPARC 39284778508Sblueswir1 #define SPARC64_STACK_BIAS 2047 39384778508Sblueswir1 39484778508Sblueswir1 //#define DEBUG_WIN 39584778508Sblueswir1 /* WARNING: dealing with register windows _is_ complicated. More info 39684778508Sblueswir1 can be found at http://www.sics.se/~psm/sparcstack.html */ 39784778508Sblueswir1 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index) 39884778508Sblueswir1 { 39984778508Sblueswir1 index = (index + cwp * 16) % (16 * env->nwindows); 40084778508Sblueswir1 /* wrap handling : if cwp is on the last window, then we use the 40184778508Sblueswir1 registers 'after' the end */ 40284778508Sblueswir1 if (index < 8 && env->cwp == env->nwindows - 1) 40384778508Sblueswir1 index += 16 * env->nwindows; 40484778508Sblueswir1 return index; 40584778508Sblueswir1 } 40684778508Sblueswir1 40784778508Sblueswir1 /* save the register window 'cwp1' */ 40884778508Sblueswir1 static inline void save_window_offset(CPUSPARCState *env, int cwp1) 40984778508Sblueswir1 { 41084778508Sblueswir1 unsigned int i; 41184778508Sblueswir1 abi_ulong sp_ptr; 41284778508Sblueswir1 41384778508Sblueswir1 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)]; 41484778508Sblueswir1 #ifdef TARGET_SPARC64 41584778508Sblueswir1 if (sp_ptr & 3) 41684778508Sblueswir1 sp_ptr += SPARC64_STACK_BIAS; 41784778508Sblueswir1 #endif 41884778508Sblueswir1 #if defined(DEBUG_WIN) 41984778508Sblueswir1 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n", 42084778508Sblueswir1 sp_ptr, cwp1); 42184778508Sblueswir1 #endif 42284778508Sblueswir1 for(i = 0; i < 16; i++) { 42384778508Sblueswir1 /* FIXME - what to do if put_user() fails? */ 42484778508Sblueswir1 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr); 42584778508Sblueswir1 sp_ptr += sizeof(abi_ulong); 42684778508Sblueswir1 } 42784778508Sblueswir1 } 42884778508Sblueswir1 42984778508Sblueswir1 static void save_window(CPUSPARCState *env) 43084778508Sblueswir1 { 43184778508Sblueswir1 #ifndef TARGET_SPARC64 43284778508Sblueswir1 unsigned int new_wim; 43384778508Sblueswir1 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) & 43484778508Sblueswir1 ((1LL << env->nwindows) - 1); 43584778508Sblueswir1 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2)); 43684778508Sblueswir1 env->wim = new_wim; 43784778508Sblueswir1 #else 43884778508Sblueswir1 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2)); 43984778508Sblueswir1 env->cansave++; 44084778508Sblueswir1 env->canrestore--; 44184778508Sblueswir1 #endif 44284778508Sblueswir1 } 44384778508Sblueswir1 44484778508Sblueswir1 static void restore_window(CPUSPARCState *env) 44584778508Sblueswir1 { 44684778508Sblueswir1 #ifndef TARGET_SPARC64 44784778508Sblueswir1 unsigned int new_wim; 44884778508Sblueswir1 #endif 44984778508Sblueswir1 unsigned int i, cwp1; 45084778508Sblueswir1 abi_ulong sp_ptr; 45184778508Sblueswir1 45284778508Sblueswir1 #ifndef TARGET_SPARC64 45384778508Sblueswir1 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) & 45484778508Sblueswir1 ((1LL << env->nwindows) - 1); 45584778508Sblueswir1 #endif 45684778508Sblueswir1 45784778508Sblueswir1 /* restore the invalid window */ 45884778508Sblueswir1 cwp1 = cpu_cwp_inc(env, env->cwp + 1); 45984778508Sblueswir1 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)]; 46084778508Sblueswir1 #ifdef TARGET_SPARC64 46184778508Sblueswir1 if (sp_ptr & 3) 46284778508Sblueswir1 sp_ptr += SPARC64_STACK_BIAS; 46384778508Sblueswir1 #endif 46484778508Sblueswir1 #if defined(DEBUG_WIN) 46584778508Sblueswir1 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n", 46684778508Sblueswir1 sp_ptr, cwp1); 46784778508Sblueswir1 #endif 46884778508Sblueswir1 for(i = 0; i < 16; i++) { 46984778508Sblueswir1 /* FIXME - what to do if get_user() fails? */ 47084778508Sblueswir1 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr); 47184778508Sblueswir1 sp_ptr += sizeof(abi_ulong); 47284778508Sblueswir1 } 47384778508Sblueswir1 #ifdef TARGET_SPARC64 47484778508Sblueswir1 env->canrestore++; 47584778508Sblueswir1 if (env->cleanwin < env->nwindows - 1) 47684778508Sblueswir1 env->cleanwin++; 47784778508Sblueswir1 env->cansave--; 47884778508Sblueswir1 #else 47984778508Sblueswir1 env->wim = new_wim; 48084778508Sblueswir1 #endif 48184778508Sblueswir1 } 48284778508Sblueswir1 48384778508Sblueswir1 static void flush_windows(CPUSPARCState *env) 48484778508Sblueswir1 { 48584778508Sblueswir1 int offset, cwp1; 48684778508Sblueswir1 48784778508Sblueswir1 offset = 1; 48884778508Sblueswir1 for(;;) { 48984778508Sblueswir1 /* if restore would invoke restore_window(), then we can stop */ 49084778508Sblueswir1 cwp1 = cpu_cwp_inc(env, env->cwp + offset); 49184778508Sblueswir1 #ifndef TARGET_SPARC64 49284778508Sblueswir1 if (env->wim & (1 << cwp1)) 49384778508Sblueswir1 break; 49484778508Sblueswir1 #else 49584778508Sblueswir1 if (env->canrestore == 0) 49684778508Sblueswir1 break; 49784778508Sblueswir1 env->cansave++; 49884778508Sblueswir1 env->canrestore--; 49984778508Sblueswir1 #endif 50084778508Sblueswir1 save_window_offset(env, cwp1); 50184778508Sblueswir1 offset++; 50284778508Sblueswir1 } 50384778508Sblueswir1 cwp1 = cpu_cwp_inc(env, env->cwp + 1); 50484778508Sblueswir1 #ifndef TARGET_SPARC64 50584778508Sblueswir1 /* set wim so that restore will reload the registers */ 50684778508Sblueswir1 env->wim = 1 << cwp1; 50784778508Sblueswir1 #endif 50884778508Sblueswir1 #if defined(DEBUG_WIN) 50984778508Sblueswir1 printf("flush_windows: nb=%d\n", offset - 1); 51084778508Sblueswir1 #endif 51184778508Sblueswir1 } 51284778508Sblueswir1 51378cfb07fSJuergen Lock void cpu_loop(CPUSPARCState *env) 51484778508Sblueswir1 { 51584778508Sblueswir1 int trapnr, ret, syscall_nr; 51684778508Sblueswir1 //target_siginfo_t info; 51784778508Sblueswir1 51884778508Sblueswir1 while (1) { 51984778508Sblueswir1 trapnr = cpu_sparc_exec (env); 52084778508Sblueswir1 52184778508Sblueswir1 switch (trapnr) { 52277b9435fSblueswir1 #ifndef TARGET_SPARC64 52377b9435fSblueswir1 case 0x80: 52477b9435fSblueswir1 #else 52578cfb07fSJuergen Lock /* FreeBSD uses 0x141 for syscalls too */ 52678cfb07fSJuergen Lock case 0x141: 52778cfb07fSJuergen Lock if (bsd_type != target_freebsd) 52878cfb07fSJuergen Lock goto badtrap; 52984778508Sblueswir1 case 0x100: 53077b9435fSblueswir1 #endif 53184778508Sblueswir1 syscall_nr = env->gregs[1]; 53284778508Sblueswir1 if (bsd_type == target_freebsd) 53384778508Sblueswir1 ret = do_freebsd_syscall(env, syscall_nr, 53484778508Sblueswir1 env->regwptr[0], env->regwptr[1], 53584778508Sblueswir1 env->regwptr[2], env->regwptr[3], 53678cfb07fSJuergen Lock env->regwptr[4], env->regwptr[5], 0, 0); 53784778508Sblueswir1 else if (bsd_type == target_netbsd) 53884778508Sblueswir1 ret = do_netbsd_syscall(env, syscall_nr, 53984778508Sblueswir1 env->regwptr[0], env->regwptr[1], 54084778508Sblueswir1 env->regwptr[2], env->regwptr[3], 54184778508Sblueswir1 env->regwptr[4], env->regwptr[5]); 542cdba95bdSblueswir1 else { //if (bsd_type == target_openbsd) 543cdba95bdSblueswir1 #if defined(TARGET_SPARC64) 544cdba95bdSblueswir1 syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG | 545cdba95bdSblueswir1 TARGET_OPENBSD_SYSCALL_G2RFLAG); 546cdba95bdSblueswir1 #endif 54784778508Sblueswir1 ret = do_openbsd_syscall(env, syscall_nr, 54884778508Sblueswir1 env->regwptr[0], env->regwptr[1], 54984778508Sblueswir1 env->regwptr[2], env->regwptr[3], 55084778508Sblueswir1 env->regwptr[4], env->regwptr[5]); 551cdba95bdSblueswir1 } 55284778508Sblueswir1 if ((unsigned int)ret >= (unsigned int)(-515)) { 55378cfb07fSJuergen Lock ret = -ret; 55484778508Sblueswir1 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) 55584778508Sblueswir1 env->xcc |= PSR_CARRY; 55684778508Sblueswir1 #else 55784778508Sblueswir1 env->psr |= PSR_CARRY; 55884778508Sblueswir1 #endif 55984778508Sblueswir1 } else { 56084778508Sblueswir1 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) 56184778508Sblueswir1 env->xcc &= ~PSR_CARRY; 56284778508Sblueswir1 #else 56384778508Sblueswir1 env->psr &= ~PSR_CARRY; 56484778508Sblueswir1 #endif 56584778508Sblueswir1 } 56684778508Sblueswir1 env->regwptr[0] = ret; 56784778508Sblueswir1 /* next instruction */ 568cdba95bdSblueswir1 #if defined(TARGET_SPARC64) 569cdba95bdSblueswir1 if (bsd_type == target_openbsd && 570cdba95bdSblueswir1 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) { 57184778508Sblueswir1 env->pc = env->gregs[2]; 57284778508Sblueswir1 env->npc = env->pc + 4; 573cdba95bdSblueswir1 } else if (bsd_type == target_openbsd && 574cdba95bdSblueswir1 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) { 57584778508Sblueswir1 env->pc = env->gregs[7]; 57684778508Sblueswir1 env->npc = env->pc + 4; 57784778508Sblueswir1 } else { 57884778508Sblueswir1 env->pc = env->npc; 57984778508Sblueswir1 env->npc = env->npc + 4; 58084778508Sblueswir1 } 58184778508Sblueswir1 #else 58284778508Sblueswir1 env->pc = env->npc; 58384778508Sblueswir1 env->npc = env->npc + 4; 58484778508Sblueswir1 #endif 58584778508Sblueswir1 break; 58684778508Sblueswir1 case 0x83: /* flush windows */ 58784778508Sblueswir1 #ifdef TARGET_ABI32 58884778508Sblueswir1 case 0x103: 58984778508Sblueswir1 #endif 59084778508Sblueswir1 flush_windows(env); 59184778508Sblueswir1 /* next instruction */ 59284778508Sblueswir1 env->pc = env->npc; 59384778508Sblueswir1 env->npc = env->npc + 4; 59484778508Sblueswir1 break; 59584778508Sblueswir1 #ifndef TARGET_SPARC64 59684778508Sblueswir1 case TT_WIN_OVF: /* window overflow */ 59784778508Sblueswir1 save_window(env); 59884778508Sblueswir1 break; 59984778508Sblueswir1 case TT_WIN_UNF: /* window underflow */ 60084778508Sblueswir1 restore_window(env); 60184778508Sblueswir1 break; 60284778508Sblueswir1 case TT_TFAULT: 60384778508Sblueswir1 case TT_DFAULT: 60484778508Sblueswir1 #if 0 60584778508Sblueswir1 { 60684778508Sblueswir1 info.si_signo = SIGSEGV; 60784778508Sblueswir1 info.si_errno = 0; 60884778508Sblueswir1 /* XXX: check env->error_code */ 60984778508Sblueswir1 info.si_code = TARGET_SEGV_MAPERR; 61084778508Sblueswir1 info._sifields._sigfault._addr = env->mmuregs[4]; 61184778508Sblueswir1 queue_signal(env, info.si_signo, &info); 61284778508Sblueswir1 } 61384778508Sblueswir1 #endif 61484778508Sblueswir1 break; 61584778508Sblueswir1 #else 61684778508Sblueswir1 case TT_SPILL: /* window overflow */ 61784778508Sblueswir1 save_window(env); 61884778508Sblueswir1 break; 61984778508Sblueswir1 case TT_FILL: /* window underflow */ 62084778508Sblueswir1 restore_window(env); 62184778508Sblueswir1 break; 62284778508Sblueswir1 case TT_TFAULT: 62384778508Sblueswir1 case TT_DFAULT: 62484778508Sblueswir1 #if 0 62584778508Sblueswir1 { 62684778508Sblueswir1 info.si_signo = SIGSEGV; 62784778508Sblueswir1 info.si_errno = 0; 62884778508Sblueswir1 /* XXX: check env->error_code */ 62984778508Sblueswir1 info.si_code = TARGET_SEGV_MAPERR; 63084778508Sblueswir1 if (trapnr == TT_DFAULT) 63184778508Sblueswir1 info._sifields._sigfault._addr = env->dmmuregs[4]; 63284778508Sblueswir1 else 63384778508Sblueswir1 info._sifields._sigfault._addr = env->tsptr->tpc; 63484778508Sblueswir1 //queue_signal(env, info.si_signo, &info); 63584778508Sblueswir1 } 63684778508Sblueswir1 #endif 63784778508Sblueswir1 break; 63884778508Sblueswir1 #endif 63984778508Sblueswir1 case EXCP_INTERRUPT: 64084778508Sblueswir1 /* just indicate that signals should be handled asap */ 64184778508Sblueswir1 break; 64284778508Sblueswir1 case EXCP_DEBUG: 64384778508Sblueswir1 { 64484778508Sblueswir1 int sig; 64584778508Sblueswir1 64684778508Sblueswir1 sig = gdb_handlesig (env, TARGET_SIGTRAP); 64784778508Sblueswir1 #if 0 64884778508Sblueswir1 if (sig) 64984778508Sblueswir1 { 65084778508Sblueswir1 info.si_signo = sig; 65184778508Sblueswir1 info.si_errno = 0; 65284778508Sblueswir1 info.si_code = TARGET_TRAP_BRKPT; 65384778508Sblueswir1 //queue_signal(env, info.si_signo, &info); 65484778508Sblueswir1 } 65584778508Sblueswir1 #endif 65684778508Sblueswir1 } 65784778508Sblueswir1 break; 65884778508Sblueswir1 default: 65978cfb07fSJuergen Lock #ifdef TARGET_SPARC64 66078cfb07fSJuergen Lock badtrap: 66178cfb07fSJuergen Lock #endif 66284778508Sblueswir1 printf ("Unhandled trap: 0x%x\n", trapnr); 66384778508Sblueswir1 cpu_dump_state(env, stderr, fprintf, 0); 66484778508Sblueswir1 exit (1); 66584778508Sblueswir1 } 66684778508Sblueswir1 process_pending_signals (env); 66784778508Sblueswir1 } 66884778508Sblueswir1 } 66984778508Sblueswir1 67084778508Sblueswir1 #endif 67184778508Sblueswir1 67284778508Sblueswir1 static void usage(void) 67384778508Sblueswir1 { 67484778508Sblueswir1 printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n" 67584778508Sblueswir1 "usage: qemu-" TARGET_ARCH " [options] program [arguments...]\n" 67684778508Sblueswir1 "BSD CPU emulator (compiled for %s emulation)\n" 67784778508Sblueswir1 "\n" 67884778508Sblueswir1 "Standard options:\n" 67984778508Sblueswir1 "-h print this help\n" 68084778508Sblueswir1 "-g port wait gdb connection to port\n" 68184778508Sblueswir1 "-L path set the elf interpreter prefix (default=%s)\n" 68284778508Sblueswir1 "-s size set the stack size in bytes (default=%ld)\n" 68384778508Sblueswir1 "-cpu model select CPU (-cpu ? for list)\n" 68484778508Sblueswir1 "-drop-ld-preload drop LD_PRELOAD for target process\n" 685fc0d96b4SBlue Swirl "-E var=value sets/modifies targets environment variable(s)\n" 686fc0d96b4SBlue Swirl "-U var unsets targets environment variable(s)\n" 6872fa5d9baSBlue Swirl #if defined(CONFIG_USE_GUEST_BASE) 6882fa5d9baSBlue Swirl "-B address set guest_base address to address\n" 6892fa5d9baSBlue Swirl #endif 69084778508Sblueswir1 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n" 69184778508Sblueswir1 "\n" 69284778508Sblueswir1 "Debug options:\n" 69384778508Sblueswir1 "-d options activate log (logfile=%s)\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 , 70884778508Sblueswir1 TARGET_ARCH, 70984778508Sblueswir1 interp_prefix, 71084778508Sblueswir1 x86_stack_size, 71184778508Sblueswir1 DEBUG_LOGFILE); 7122d18e637Sblueswir1 exit(1); 71384778508Sblueswir1 } 71484778508Sblueswir1 71584778508Sblueswir1 THREAD CPUState *thread_env; 71684778508Sblueswir1 71784778508Sblueswir1 /* Assumes contents are already zeroed. */ 71884778508Sblueswir1 void init_task_state(TaskState *ts) 71984778508Sblueswir1 { 72084778508Sblueswir1 int i; 72184778508Sblueswir1 72284778508Sblueswir1 ts->used = 1; 72384778508Sblueswir1 ts->first_free = ts->sigqueue_table; 72484778508Sblueswir1 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) { 72584778508Sblueswir1 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1]; 72684778508Sblueswir1 } 72784778508Sblueswir1 ts->sigqueue_table[i].next = NULL; 72884778508Sblueswir1 } 72984778508Sblueswir1 73084778508Sblueswir1 int main(int argc, char **argv) 73184778508Sblueswir1 { 73284778508Sblueswir1 const char *filename; 73384778508Sblueswir1 const char *cpu_model; 73484778508Sblueswir1 struct target_pt_regs regs1, *regs = ®s1; 73584778508Sblueswir1 struct image_info info1, *info = &info1; 73684778508Sblueswir1 TaskState ts1, *ts = &ts1; 73784778508Sblueswir1 CPUState *env; 73884778508Sblueswir1 int optind; 73984778508Sblueswir1 const char *r; 74084778508Sblueswir1 int gdbstub_port = 0; 741fc0d96b4SBlue Swirl char **target_environ, **wrk; 742fc0d96b4SBlue Swirl envlist_t *envlist = NULL; 74378cfb07fSJuergen Lock bsd_type = target_openbsd; 74484778508Sblueswir1 74584778508Sblueswir1 if (argc <= 1) 74684778508Sblueswir1 usage(); 74784778508Sblueswir1 74884778508Sblueswir1 /* init debug */ 74984778508Sblueswir1 cpu_set_log_filename(DEBUG_LOGFILE); 75084778508Sblueswir1 751fc0d96b4SBlue Swirl if ((envlist = envlist_create()) == NULL) { 752fc0d96b4SBlue Swirl (void) fprintf(stderr, "Unable to allocate envlist\n"); 753fc0d96b4SBlue Swirl exit(1); 754fc0d96b4SBlue Swirl } 755fc0d96b4SBlue Swirl 756fc0d96b4SBlue Swirl /* add current environment into the list */ 757fc0d96b4SBlue Swirl for (wrk = environ; *wrk != NULL; wrk++) { 758fc0d96b4SBlue Swirl (void) envlist_setenv(envlist, *wrk); 759fc0d96b4SBlue Swirl } 760fc0d96b4SBlue Swirl 76184778508Sblueswir1 cpu_model = NULL; 7620c62de2fSJuergen Lock #if defined(cpudef_setup) 7630c62de2fSJuergen Lock cpudef_setup(); /* parse cpu definitions in target config file (TBD) */ 7640c62de2fSJuergen Lock #endif 7650c62de2fSJuergen Lock 76684778508Sblueswir1 optind = 1; 76784778508Sblueswir1 for(;;) { 76884778508Sblueswir1 if (optind >= argc) 76984778508Sblueswir1 break; 77084778508Sblueswir1 r = argv[optind]; 77184778508Sblueswir1 if (r[0] != '-') 77284778508Sblueswir1 break; 77384778508Sblueswir1 optind++; 77484778508Sblueswir1 r++; 77584778508Sblueswir1 if (!strcmp(r, "-")) { 77684778508Sblueswir1 break; 77784778508Sblueswir1 } else if (!strcmp(r, "d")) { 77884778508Sblueswir1 int mask; 77984778508Sblueswir1 const CPULogItem *item; 78084778508Sblueswir1 78184778508Sblueswir1 if (optind >= argc) 78284778508Sblueswir1 break; 78384778508Sblueswir1 78484778508Sblueswir1 r = argv[optind++]; 78584778508Sblueswir1 mask = cpu_str_to_log_mask(r); 78684778508Sblueswir1 if (!mask) { 78784778508Sblueswir1 printf("Log items (comma separated):\n"); 78884778508Sblueswir1 for(item = cpu_log_items; item->mask != 0; item++) { 78984778508Sblueswir1 printf("%-10s %s\n", item->name, item->help); 79084778508Sblueswir1 } 79184778508Sblueswir1 exit(1); 79284778508Sblueswir1 } 79384778508Sblueswir1 cpu_set_log(mask); 794fc0d96b4SBlue Swirl } else if (!strcmp(r, "E")) { 795fc0d96b4SBlue Swirl r = argv[optind++]; 796fc0d96b4SBlue Swirl if (envlist_setenv(envlist, r) != 0) 797fc0d96b4SBlue Swirl usage(); 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++]; 82684778508Sblueswir1 if (strcmp(cpu_model, "?") == 0) { 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 if (optind >= argc) 86184778508Sblueswir1 usage(); 86284778508Sblueswir1 filename = argv[optind]; 86384778508Sblueswir1 86484778508Sblueswir1 /* Zero out regs */ 86584778508Sblueswir1 memset(regs, 0, sizeof(struct target_pt_regs)); 86684778508Sblueswir1 86784778508Sblueswir1 /* Zero out image_info */ 86884778508Sblueswir1 memset(info, 0, sizeof(struct image_info)); 86984778508Sblueswir1 87084778508Sblueswir1 /* Scan interp_prefix dir for replacement files. */ 87184778508Sblueswir1 init_paths(interp_prefix); 87284778508Sblueswir1 87384778508Sblueswir1 if (cpu_model == NULL) { 87431fc12dfSblueswir1 #if defined(TARGET_I386) 87531fc12dfSblueswir1 #ifdef TARGET_X86_64 87631fc12dfSblueswir1 cpu_model = "qemu64"; 87731fc12dfSblueswir1 #else 87831fc12dfSblueswir1 cpu_model = "qemu32"; 87931fc12dfSblueswir1 #endif 88031fc12dfSblueswir1 #elif defined(TARGET_SPARC) 88184778508Sblueswir1 #ifdef TARGET_SPARC64 88284778508Sblueswir1 cpu_model = "TI UltraSparc II"; 88384778508Sblueswir1 #else 88484778508Sblueswir1 cpu_model = "Fujitsu MB86904"; 88584778508Sblueswir1 #endif 88684778508Sblueswir1 #else 88784778508Sblueswir1 cpu_model = "any"; 88884778508Sblueswir1 #endif 88984778508Sblueswir1 } 89084778508Sblueswir1 cpu_exec_init_all(0); 89184778508Sblueswir1 /* NOTE: we need to init the CPU at this stage to get 89284778508Sblueswir1 qemu_host_page_size */ 89384778508Sblueswir1 env = cpu_init(cpu_model); 89484778508Sblueswir1 if (!env) { 89584778508Sblueswir1 fprintf(stderr, "Unable to find CPU definition\n"); 89684778508Sblueswir1 exit(1); 89784778508Sblueswir1 } 898b55a37c9SBlue Swirl #if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC) 899b55a37c9SBlue Swirl cpu_reset(env); 900b55a37c9SBlue Swirl #endif 90184778508Sblueswir1 thread_env = env; 90284778508Sblueswir1 90384778508Sblueswir1 if (getenv("QEMU_STRACE")) { 90484778508Sblueswir1 do_strace = 1; 90584778508Sblueswir1 } 90684778508Sblueswir1 907fc0d96b4SBlue Swirl target_environ = envlist_to_environ(envlist, NULL); 908fc0d96b4SBlue Swirl envlist_free(envlist); 90984778508Sblueswir1 9102fa5d9baSBlue Swirl #if defined(CONFIG_USE_GUEST_BASE) 9112fa5d9baSBlue Swirl /* 9122fa5d9baSBlue Swirl * Now that page sizes are configured in cpu_init() we can do 9132fa5d9baSBlue Swirl * proper page alignment for guest_base. 9142fa5d9baSBlue Swirl */ 9152fa5d9baSBlue Swirl guest_base = HOST_PAGE_ALIGN(guest_base); 9162fa5d9baSBlue Swirl 9172fa5d9baSBlue Swirl /* 9182fa5d9baSBlue Swirl * Read in mmap_min_addr kernel parameter. This value is used 9192fa5d9baSBlue Swirl * When loading the ELF image to determine whether guest_base 9202fa5d9baSBlue Swirl * is needed. 9212fa5d9baSBlue Swirl * 9222fa5d9baSBlue Swirl * When user has explicitly set the quest base, we skip this 9232fa5d9baSBlue Swirl * test. 9242fa5d9baSBlue Swirl */ 9252fa5d9baSBlue Swirl if (!have_guest_base) { 9262fa5d9baSBlue Swirl FILE *fp; 9272fa5d9baSBlue Swirl 9282fa5d9baSBlue Swirl if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) { 9292fa5d9baSBlue Swirl unsigned long tmp; 9302fa5d9baSBlue Swirl if (fscanf(fp, "%lu", &tmp) == 1) { 9312fa5d9baSBlue Swirl mmap_min_addr = tmp; 9322fa5d9baSBlue Swirl qemu_log("host mmap_min_addr=0x%lx\n", mmap_min_addr); 9332fa5d9baSBlue Swirl } 9342fa5d9baSBlue Swirl fclose(fp); 9352fa5d9baSBlue Swirl } 9362fa5d9baSBlue Swirl } 9372fa5d9baSBlue Swirl #endif /* CONFIG_USE_GUEST_BASE */ 93884778508Sblueswir1 93984778508Sblueswir1 if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) { 94084778508Sblueswir1 printf("Error loading %s\n", filename); 94184778508Sblueswir1 _exit(1); 94284778508Sblueswir1 } 94384778508Sblueswir1 94484778508Sblueswir1 for (wrk = target_environ; *wrk; wrk++) { 94584778508Sblueswir1 free(*wrk); 94684778508Sblueswir1 } 94784778508Sblueswir1 94884778508Sblueswir1 free(target_environ); 94984778508Sblueswir1 9502e77eac6Sblueswir1 if (qemu_log_enabled()) { 9512fa5d9baSBlue Swirl #if defined(CONFIG_USE_GUEST_BASE) 9522fa5d9baSBlue Swirl qemu_log("guest_base 0x%lx\n", guest_base); 9532fa5d9baSBlue Swirl #endif 95493fcfe39Saliguori log_page_dump(); 95584778508Sblueswir1 95693fcfe39Saliguori qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk); 95793fcfe39Saliguori qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code); 95893fcfe39Saliguori qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n", 95984778508Sblueswir1 info->start_code); 96093fcfe39Saliguori qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n", 96184778508Sblueswir1 info->start_data); 96293fcfe39Saliguori qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data); 96393fcfe39Saliguori qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n", 96484778508Sblueswir1 info->start_stack); 96593fcfe39Saliguori qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk); 96693fcfe39Saliguori qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry); 9672e77eac6Sblueswir1 } 96884778508Sblueswir1 96984778508Sblueswir1 target_set_brk(info->brk); 97084778508Sblueswir1 syscall_init(); 97184778508Sblueswir1 signal_init(); 97284778508Sblueswir1 97384778508Sblueswir1 /* build Task State */ 97484778508Sblueswir1 memset(ts, 0, sizeof(TaskState)); 97584778508Sblueswir1 init_task_state(ts); 97684778508Sblueswir1 ts->info = info; 97784778508Sblueswir1 env->opaque = ts; 97884778508Sblueswir1 97931fc12dfSblueswir1 #if defined(TARGET_I386) 98031fc12dfSblueswir1 cpu_x86_set_cpl(env, 3); 98131fc12dfSblueswir1 98231fc12dfSblueswir1 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK; 98331fc12dfSblueswir1 env->hflags |= HF_PE_MASK; 98431fc12dfSblueswir1 if (env->cpuid_features & CPUID_SSE) { 98531fc12dfSblueswir1 env->cr[4] |= CR4_OSFXSR_MASK; 98631fc12dfSblueswir1 env->hflags |= HF_OSFXSR_MASK; 98731fc12dfSblueswir1 } 98831fc12dfSblueswir1 #ifndef TARGET_ABI32 98931fc12dfSblueswir1 /* enable 64 bit mode if possible */ 99031fc12dfSblueswir1 if (!(env->cpuid_ext2_features & CPUID_EXT2_LM)) { 99131fc12dfSblueswir1 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n"); 99231fc12dfSblueswir1 exit(1); 99331fc12dfSblueswir1 } 99431fc12dfSblueswir1 env->cr[4] |= CR4_PAE_MASK; 99531fc12dfSblueswir1 env->efer |= MSR_EFER_LMA | MSR_EFER_LME; 99631fc12dfSblueswir1 env->hflags |= HF_LMA_MASK; 99731fc12dfSblueswir1 #endif 99831fc12dfSblueswir1 99931fc12dfSblueswir1 /* flags setup : we activate the IRQs by default as in user mode */ 100031fc12dfSblueswir1 env->eflags |= IF_MASK; 100131fc12dfSblueswir1 100231fc12dfSblueswir1 /* linux register setup */ 100331fc12dfSblueswir1 #ifndef TARGET_ABI32 100431fc12dfSblueswir1 env->regs[R_EAX] = regs->rax; 100531fc12dfSblueswir1 env->regs[R_EBX] = regs->rbx; 100631fc12dfSblueswir1 env->regs[R_ECX] = regs->rcx; 100731fc12dfSblueswir1 env->regs[R_EDX] = regs->rdx; 100831fc12dfSblueswir1 env->regs[R_ESI] = regs->rsi; 100931fc12dfSblueswir1 env->regs[R_EDI] = regs->rdi; 101031fc12dfSblueswir1 env->regs[R_EBP] = regs->rbp; 101131fc12dfSblueswir1 env->regs[R_ESP] = regs->rsp; 101231fc12dfSblueswir1 env->eip = regs->rip; 101331fc12dfSblueswir1 #else 101431fc12dfSblueswir1 env->regs[R_EAX] = regs->eax; 101531fc12dfSblueswir1 env->regs[R_EBX] = regs->ebx; 101631fc12dfSblueswir1 env->regs[R_ECX] = regs->ecx; 101731fc12dfSblueswir1 env->regs[R_EDX] = regs->edx; 101831fc12dfSblueswir1 env->regs[R_ESI] = regs->esi; 101931fc12dfSblueswir1 env->regs[R_EDI] = regs->edi; 102031fc12dfSblueswir1 env->regs[R_EBP] = regs->ebp; 102131fc12dfSblueswir1 env->regs[R_ESP] = regs->esp; 102231fc12dfSblueswir1 env->eip = regs->eip; 102331fc12dfSblueswir1 #endif 102431fc12dfSblueswir1 102531fc12dfSblueswir1 /* linux interrupt setup */ 102631fc12dfSblueswir1 #ifndef TARGET_ABI32 102731fc12dfSblueswir1 env->idt.limit = 511; 102831fc12dfSblueswir1 #else 102931fc12dfSblueswir1 env->idt.limit = 255; 103031fc12dfSblueswir1 #endif 103131fc12dfSblueswir1 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1), 103231fc12dfSblueswir1 PROT_READ|PROT_WRITE, 103331fc12dfSblueswir1 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); 103431fc12dfSblueswir1 idt_table = g2h(env->idt.base); 103531fc12dfSblueswir1 set_idt(0, 0); 103631fc12dfSblueswir1 set_idt(1, 0); 103731fc12dfSblueswir1 set_idt(2, 0); 103831fc12dfSblueswir1 set_idt(3, 3); 103931fc12dfSblueswir1 set_idt(4, 3); 104031fc12dfSblueswir1 set_idt(5, 0); 104131fc12dfSblueswir1 set_idt(6, 0); 104231fc12dfSblueswir1 set_idt(7, 0); 104331fc12dfSblueswir1 set_idt(8, 0); 104431fc12dfSblueswir1 set_idt(9, 0); 104531fc12dfSblueswir1 set_idt(10, 0); 104631fc12dfSblueswir1 set_idt(11, 0); 104731fc12dfSblueswir1 set_idt(12, 0); 104831fc12dfSblueswir1 set_idt(13, 0); 104931fc12dfSblueswir1 set_idt(14, 0); 105031fc12dfSblueswir1 set_idt(15, 0); 105131fc12dfSblueswir1 set_idt(16, 0); 105231fc12dfSblueswir1 set_idt(17, 0); 105331fc12dfSblueswir1 set_idt(18, 0); 105431fc12dfSblueswir1 set_idt(19, 0); 105531fc12dfSblueswir1 set_idt(0x80, 3); 105631fc12dfSblueswir1 105731fc12dfSblueswir1 /* linux segment setup */ 105831fc12dfSblueswir1 { 105931fc12dfSblueswir1 uint64_t *gdt_table; 106031fc12dfSblueswir1 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES, 106131fc12dfSblueswir1 PROT_READ|PROT_WRITE, 106231fc12dfSblueswir1 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); 106331fc12dfSblueswir1 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1; 106431fc12dfSblueswir1 gdt_table = g2h(env->gdt.base); 106531fc12dfSblueswir1 #ifdef TARGET_ABI32 106631fc12dfSblueswir1 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff, 106731fc12dfSblueswir1 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 106831fc12dfSblueswir1 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT)); 106931fc12dfSblueswir1 #else 107031fc12dfSblueswir1 /* 64 bit code segment */ 107131fc12dfSblueswir1 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff, 107231fc12dfSblueswir1 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 107331fc12dfSblueswir1 DESC_L_MASK | 107431fc12dfSblueswir1 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT)); 107531fc12dfSblueswir1 #endif 107631fc12dfSblueswir1 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff, 107731fc12dfSblueswir1 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 107831fc12dfSblueswir1 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT)); 107931fc12dfSblueswir1 } 108031fc12dfSblueswir1 108131fc12dfSblueswir1 cpu_x86_load_seg(env, R_CS, __USER_CS); 108231fc12dfSblueswir1 cpu_x86_load_seg(env, R_SS, __USER_DS); 108331fc12dfSblueswir1 #ifdef TARGET_ABI32 108431fc12dfSblueswir1 cpu_x86_load_seg(env, R_DS, __USER_DS); 108531fc12dfSblueswir1 cpu_x86_load_seg(env, R_ES, __USER_DS); 108631fc12dfSblueswir1 cpu_x86_load_seg(env, R_FS, __USER_DS); 108731fc12dfSblueswir1 cpu_x86_load_seg(env, R_GS, __USER_DS); 108831fc12dfSblueswir1 /* This hack makes Wine work... */ 108931fc12dfSblueswir1 env->segs[R_FS].selector = 0; 109031fc12dfSblueswir1 #else 109131fc12dfSblueswir1 cpu_x86_load_seg(env, R_DS, 0); 109231fc12dfSblueswir1 cpu_x86_load_seg(env, R_ES, 0); 109331fc12dfSblueswir1 cpu_x86_load_seg(env, R_FS, 0); 109431fc12dfSblueswir1 cpu_x86_load_seg(env, R_GS, 0); 109531fc12dfSblueswir1 #endif 109631fc12dfSblueswir1 #elif defined(TARGET_SPARC) 109784778508Sblueswir1 { 109884778508Sblueswir1 int i; 109984778508Sblueswir1 env->pc = regs->pc; 110084778508Sblueswir1 env->npc = regs->npc; 110184778508Sblueswir1 env->y = regs->y; 110284778508Sblueswir1 for(i = 0; i < 8; i++) 110384778508Sblueswir1 env->gregs[i] = regs->u_regs[i]; 110484778508Sblueswir1 for(i = 0; i < 8; i++) 110584778508Sblueswir1 env->regwptr[i] = regs->u_regs[i + 8]; 110684778508Sblueswir1 } 110784778508Sblueswir1 #else 110884778508Sblueswir1 #error unsupported target CPU 110984778508Sblueswir1 #endif 111084778508Sblueswir1 111184778508Sblueswir1 if (gdbstub_port) { 111284778508Sblueswir1 gdbserver_start (gdbstub_port); 111384778508Sblueswir1 gdb_handlesig(env, 0); 111484778508Sblueswir1 } 111578cfb07fSJuergen Lock cpu_loop(env); 111684778508Sblueswir1 /* never exits */ 111784778508Sblueswir1 return 0; 111884778508Sblueswir1 } 1119