xref: /openbmc/qemu/bsd-user/main.c (revision dca1173c)
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;
4684778508Sblueswir1 const char *qemu_uname_release = CONFIG_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) {
95*dca1173cSAndreas Färber         gdbserver_fork((CPUArchState *)thread_cpu->env_ptr);
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 
111b98e9ca8SAndreas Färber void cpu_smm_update(CPUX86State *env)
11231fc12dfSblueswir1 {
11331fc12dfSblueswir1 }
11431fc12dfSblueswir1 
11531fc12dfSblueswir1 uint64_t cpu_get_tsc(CPUX86State *env)
11631fc12dfSblueswir1 {
11731fc12dfSblueswir1     return cpu_get_real_ticks();
11831fc12dfSblueswir1 }
11931fc12dfSblueswir1 
12031fc12dfSblueswir1 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
12131fc12dfSblueswir1                      int flags)
12231fc12dfSblueswir1 {
12331fc12dfSblueswir1     unsigned int e1, e2;
12431fc12dfSblueswir1     uint32_t *p;
12531fc12dfSblueswir1     e1 = (addr << 16) | (limit & 0xffff);
12631fc12dfSblueswir1     e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
12731fc12dfSblueswir1     e2 |= flags;
12831fc12dfSblueswir1     p = ptr;
12931fc12dfSblueswir1     p[0] = tswap32(e1);
13031fc12dfSblueswir1     p[1] = tswap32(e2);
13131fc12dfSblueswir1 }
13231fc12dfSblueswir1 
13331fc12dfSblueswir1 static uint64_t *idt_table;
13431fc12dfSblueswir1 #ifdef TARGET_X86_64
13531fc12dfSblueswir1 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
13631fc12dfSblueswir1                        uint64_t addr, unsigned int sel)
13731fc12dfSblueswir1 {
13831fc12dfSblueswir1     uint32_t *p, e1, e2;
13931fc12dfSblueswir1     e1 = (addr & 0xffff) | (sel << 16);
14031fc12dfSblueswir1     e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
14131fc12dfSblueswir1     p = ptr;
14231fc12dfSblueswir1     p[0] = tswap32(e1);
14331fc12dfSblueswir1     p[1] = tswap32(e2);
14431fc12dfSblueswir1     p[2] = tswap32(addr >> 32);
14531fc12dfSblueswir1     p[3] = 0;
14631fc12dfSblueswir1 }
14731fc12dfSblueswir1 /* only dpl matters as we do only user space emulation */
14831fc12dfSblueswir1 static void set_idt(int n, unsigned int dpl)
14931fc12dfSblueswir1 {
15031fc12dfSblueswir1     set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
15131fc12dfSblueswir1 }
15231fc12dfSblueswir1 #else
15331fc12dfSblueswir1 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
15431fc12dfSblueswir1                      uint32_t addr, unsigned int sel)
15531fc12dfSblueswir1 {
15631fc12dfSblueswir1     uint32_t *p, e1, e2;
15731fc12dfSblueswir1     e1 = (addr & 0xffff) | (sel << 16);
15831fc12dfSblueswir1     e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
15931fc12dfSblueswir1     p = ptr;
16031fc12dfSblueswir1     p[0] = tswap32(e1);
16131fc12dfSblueswir1     p[1] = tswap32(e2);
16231fc12dfSblueswir1 }
16331fc12dfSblueswir1 
16431fc12dfSblueswir1 /* only dpl matters as we do only user space emulation */
16531fc12dfSblueswir1 static void set_idt(int n, unsigned int dpl)
16631fc12dfSblueswir1 {
16731fc12dfSblueswir1     set_gate(idt_table + n, 0, dpl, 0, 0);
16831fc12dfSblueswir1 }
16931fc12dfSblueswir1 #endif
17031fc12dfSblueswir1 
17178cfb07fSJuergen Lock void cpu_loop(CPUX86State *env)
17231fc12dfSblueswir1 {
17331fc12dfSblueswir1     int trapnr;
17431fc12dfSblueswir1     abi_ulong pc;
17531fc12dfSblueswir1     //target_siginfo_t info;
17631fc12dfSblueswir1 
17731fc12dfSblueswir1     for(;;) {
17831fc12dfSblueswir1         trapnr = cpu_x86_exec(env);
17931fc12dfSblueswir1         switch(trapnr) {
18031fc12dfSblueswir1         case 0x80:
18131fc12dfSblueswir1             /* syscall from int $0x80 */
18278cfb07fSJuergen Lock             if (bsd_type == target_freebsd) {
18378cfb07fSJuergen Lock                 abi_ulong params = (abi_ulong) env->regs[R_ESP] +
18478cfb07fSJuergen Lock                     sizeof(int32_t);
18578cfb07fSJuergen Lock                 int32_t syscall_nr = env->regs[R_EAX];
18678cfb07fSJuergen Lock                 int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
18778cfb07fSJuergen Lock 
18878cfb07fSJuergen Lock                 if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
18978cfb07fSJuergen Lock                     get_user_s32(syscall_nr, params);
19078cfb07fSJuergen Lock                     params += sizeof(int32_t);
19178cfb07fSJuergen Lock                 } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
19278cfb07fSJuergen Lock                     get_user_s32(syscall_nr, params);
19378cfb07fSJuergen Lock                     params += sizeof(int64_t);
19478cfb07fSJuergen Lock                 }
19578cfb07fSJuergen Lock                 get_user_s32(arg1, params);
19678cfb07fSJuergen Lock                 params += sizeof(int32_t);
19778cfb07fSJuergen Lock                 get_user_s32(arg2, params);
19878cfb07fSJuergen Lock                 params += sizeof(int32_t);
19978cfb07fSJuergen Lock                 get_user_s32(arg3, params);
20078cfb07fSJuergen Lock                 params += sizeof(int32_t);
20178cfb07fSJuergen Lock                 get_user_s32(arg4, params);
20278cfb07fSJuergen Lock                 params += sizeof(int32_t);
20378cfb07fSJuergen Lock                 get_user_s32(arg5, params);
20478cfb07fSJuergen Lock                 params += sizeof(int32_t);
20578cfb07fSJuergen Lock                 get_user_s32(arg6, params);
20678cfb07fSJuergen Lock                 params += sizeof(int32_t);
20778cfb07fSJuergen Lock                 get_user_s32(arg7, params);
20878cfb07fSJuergen Lock                 params += sizeof(int32_t);
20978cfb07fSJuergen Lock                 get_user_s32(arg8, params);
21078cfb07fSJuergen Lock                 env->regs[R_EAX] = do_freebsd_syscall(env,
21178cfb07fSJuergen Lock                                                       syscall_nr,
21278cfb07fSJuergen Lock                                                       arg1,
21378cfb07fSJuergen Lock                                                       arg2,
21478cfb07fSJuergen Lock                                                       arg3,
21578cfb07fSJuergen Lock                                                       arg4,
21678cfb07fSJuergen Lock                                                       arg5,
21778cfb07fSJuergen Lock                                                       arg6,
21878cfb07fSJuergen Lock                                                       arg7,
21978cfb07fSJuergen Lock                                                       arg8);
22078cfb07fSJuergen Lock             } else { //if (bsd_type == target_openbsd)
22131fc12dfSblueswir1                 env->regs[R_EAX] = do_openbsd_syscall(env,
22231fc12dfSblueswir1                                                       env->regs[R_EAX],
22331fc12dfSblueswir1                                                       env->regs[R_EBX],
22431fc12dfSblueswir1                                                       env->regs[R_ECX],
22531fc12dfSblueswir1                                                       env->regs[R_EDX],
22631fc12dfSblueswir1                                                       env->regs[R_ESI],
22731fc12dfSblueswir1                                                       env->regs[R_EDI],
22831fc12dfSblueswir1                                                       env->regs[R_EBP]);
22978cfb07fSJuergen Lock             }
23078cfb07fSJuergen Lock             if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
23178cfb07fSJuergen Lock                 env->regs[R_EAX] = -env->regs[R_EAX];
23278cfb07fSJuergen Lock                 env->eflags |= CC_C;
23378cfb07fSJuergen Lock             } else {
23478cfb07fSJuergen Lock                 env->eflags &= ~CC_C;
23578cfb07fSJuergen Lock             }
23631fc12dfSblueswir1             break;
23731fc12dfSblueswir1 #ifndef TARGET_ABI32
23831fc12dfSblueswir1         case EXCP_SYSCALL:
2395ba18547SStefan Weil             /* syscall from syscall instruction */
24078cfb07fSJuergen Lock             if (bsd_type == target_freebsd)
24178cfb07fSJuergen Lock                 env->regs[R_EAX] = do_freebsd_syscall(env,
24278cfb07fSJuergen Lock                                                       env->regs[R_EAX],
24378cfb07fSJuergen Lock                                                       env->regs[R_EDI],
24478cfb07fSJuergen Lock                                                       env->regs[R_ESI],
24578cfb07fSJuergen Lock                                                       env->regs[R_EDX],
24678cfb07fSJuergen Lock                                                       env->regs[R_ECX],
24778cfb07fSJuergen Lock                                                       env->regs[8],
24878cfb07fSJuergen Lock                                                       env->regs[9], 0, 0);
24978cfb07fSJuergen Lock             else { //if (bsd_type == target_openbsd)
25031fc12dfSblueswir1                 env->regs[R_EAX] = do_openbsd_syscall(env,
25131fc12dfSblueswir1                                                       env->regs[R_EAX],
25231fc12dfSblueswir1                                                       env->regs[R_EDI],
25331fc12dfSblueswir1                                                       env->regs[R_ESI],
25431fc12dfSblueswir1                                                       env->regs[R_EDX],
25531fc12dfSblueswir1                                                       env->regs[10],
25631fc12dfSblueswir1                                                       env->regs[8],
25731fc12dfSblueswir1                                                       env->regs[9]);
25878cfb07fSJuergen Lock             }
25931fc12dfSblueswir1             env->eip = env->exception_next_eip;
26078cfb07fSJuergen Lock             if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
26178cfb07fSJuergen Lock                 env->regs[R_EAX] = -env->regs[R_EAX];
26278cfb07fSJuergen Lock                 env->eflags |= CC_C;
26378cfb07fSJuergen Lock             } else {
26478cfb07fSJuergen Lock                 env->eflags &= ~CC_C;
26578cfb07fSJuergen Lock             }
26631fc12dfSblueswir1             break;
26731fc12dfSblueswir1 #endif
26831fc12dfSblueswir1 #if 0
26931fc12dfSblueswir1         case EXCP0B_NOSEG:
27031fc12dfSblueswir1         case EXCP0C_STACK:
27131fc12dfSblueswir1             info.si_signo = SIGBUS;
27231fc12dfSblueswir1             info.si_errno = 0;
27331fc12dfSblueswir1             info.si_code = TARGET_SI_KERNEL;
27431fc12dfSblueswir1             info._sifields._sigfault._addr = 0;
27531fc12dfSblueswir1             queue_signal(env, info.si_signo, &info);
27631fc12dfSblueswir1             break;
27731fc12dfSblueswir1         case EXCP0D_GPF:
27831fc12dfSblueswir1             /* XXX: potential problem if ABI32 */
27931fc12dfSblueswir1 #ifndef TARGET_X86_64
28031fc12dfSblueswir1             if (env->eflags & VM_MASK) {
28131fc12dfSblueswir1                 handle_vm86_fault(env);
28231fc12dfSblueswir1             } else
28331fc12dfSblueswir1 #endif
28431fc12dfSblueswir1             {
28531fc12dfSblueswir1                 info.si_signo = SIGSEGV;
28631fc12dfSblueswir1                 info.si_errno = 0;
28731fc12dfSblueswir1                 info.si_code = TARGET_SI_KERNEL;
28831fc12dfSblueswir1                 info._sifields._sigfault._addr = 0;
28931fc12dfSblueswir1                 queue_signal(env, info.si_signo, &info);
29031fc12dfSblueswir1             }
29131fc12dfSblueswir1             break;
29231fc12dfSblueswir1         case EXCP0E_PAGE:
29331fc12dfSblueswir1             info.si_signo = SIGSEGV;
29431fc12dfSblueswir1             info.si_errno = 0;
29531fc12dfSblueswir1             if (!(env->error_code & 1))
29631fc12dfSblueswir1                 info.si_code = TARGET_SEGV_MAPERR;
29731fc12dfSblueswir1             else
29831fc12dfSblueswir1                 info.si_code = TARGET_SEGV_ACCERR;
29931fc12dfSblueswir1             info._sifields._sigfault._addr = env->cr[2];
30031fc12dfSblueswir1             queue_signal(env, info.si_signo, &info);
30131fc12dfSblueswir1             break;
30231fc12dfSblueswir1         case EXCP00_DIVZ:
30331fc12dfSblueswir1 #ifndef TARGET_X86_64
30431fc12dfSblueswir1             if (env->eflags & VM_MASK) {
30531fc12dfSblueswir1                 handle_vm86_trap(env, trapnr);
30631fc12dfSblueswir1             } else
30731fc12dfSblueswir1 #endif
30831fc12dfSblueswir1             {
30931fc12dfSblueswir1                 /* division by zero */
31031fc12dfSblueswir1                 info.si_signo = SIGFPE;
31131fc12dfSblueswir1                 info.si_errno = 0;
31231fc12dfSblueswir1                 info.si_code = TARGET_FPE_INTDIV;
31331fc12dfSblueswir1                 info._sifields._sigfault._addr = env->eip;
31431fc12dfSblueswir1                 queue_signal(env, info.si_signo, &info);
31531fc12dfSblueswir1             }
31631fc12dfSblueswir1             break;
31731fc12dfSblueswir1         case EXCP01_DB:
31831fc12dfSblueswir1         case EXCP03_INT3:
31931fc12dfSblueswir1 #ifndef TARGET_X86_64
32031fc12dfSblueswir1             if (env->eflags & VM_MASK) {
32131fc12dfSblueswir1                 handle_vm86_trap(env, trapnr);
32231fc12dfSblueswir1             } else
32331fc12dfSblueswir1 #endif
32431fc12dfSblueswir1             {
32531fc12dfSblueswir1                 info.si_signo = SIGTRAP;
32631fc12dfSblueswir1                 info.si_errno = 0;
32731fc12dfSblueswir1                 if (trapnr == EXCP01_DB) {
32831fc12dfSblueswir1                     info.si_code = TARGET_TRAP_BRKPT;
32931fc12dfSblueswir1                     info._sifields._sigfault._addr = env->eip;
33031fc12dfSblueswir1                 } else {
33131fc12dfSblueswir1                     info.si_code = TARGET_SI_KERNEL;
33231fc12dfSblueswir1                     info._sifields._sigfault._addr = 0;
33331fc12dfSblueswir1                 }
33431fc12dfSblueswir1                 queue_signal(env, info.si_signo, &info);
33531fc12dfSblueswir1             }
33631fc12dfSblueswir1             break;
33731fc12dfSblueswir1         case EXCP04_INTO:
33831fc12dfSblueswir1         case EXCP05_BOUND:
33931fc12dfSblueswir1 #ifndef TARGET_X86_64
34031fc12dfSblueswir1             if (env->eflags & VM_MASK) {
34131fc12dfSblueswir1                 handle_vm86_trap(env, trapnr);
34231fc12dfSblueswir1             } else
34331fc12dfSblueswir1 #endif
34431fc12dfSblueswir1             {
34531fc12dfSblueswir1                 info.si_signo = SIGSEGV;
34631fc12dfSblueswir1                 info.si_errno = 0;
34731fc12dfSblueswir1                 info.si_code = TARGET_SI_KERNEL;
34831fc12dfSblueswir1                 info._sifields._sigfault._addr = 0;
34931fc12dfSblueswir1                 queue_signal(env, info.si_signo, &info);
35031fc12dfSblueswir1             }
35131fc12dfSblueswir1             break;
35231fc12dfSblueswir1         case EXCP06_ILLOP:
35331fc12dfSblueswir1             info.si_signo = SIGILL;
35431fc12dfSblueswir1             info.si_errno = 0;
35531fc12dfSblueswir1             info.si_code = TARGET_ILL_ILLOPN;
35631fc12dfSblueswir1             info._sifields._sigfault._addr = env->eip;
35731fc12dfSblueswir1             queue_signal(env, info.si_signo, &info);
35831fc12dfSblueswir1             break;
35931fc12dfSblueswir1 #endif
36031fc12dfSblueswir1         case EXCP_INTERRUPT:
36131fc12dfSblueswir1             /* just indicate that signals should be handled asap */
36231fc12dfSblueswir1             break;
36331fc12dfSblueswir1 #if 0
36431fc12dfSblueswir1         case EXCP_DEBUG:
36531fc12dfSblueswir1             {
36631fc12dfSblueswir1                 int sig;
36731fc12dfSblueswir1 
36831fc12dfSblueswir1                 sig = gdb_handlesig (env, TARGET_SIGTRAP);
36931fc12dfSblueswir1                 if (sig)
37031fc12dfSblueswir1                   {
37131fc12dfSblueswir1                     info.si_signo = sig;
37231fc12dfSblueswir1                     info.si_errno = 0;
37331fc12dfSblueswir1                     info.si_code = TARGET_TRAP_BRKPT;
37431fc12dfSblueswir1                     queue_signal(env, info.si_signo, &info);
37531fc12dfSblueswir1                   }
37631fc12dfSblueswir1             }
37731fc12dfSblueswir1             break;
37831fc12dfSblueswir1 #endif
37931fc12dfSblueswir1         default:
38031fc12dfSblueswir1             pc = env->segs[R_CS].base + env->eip;
38131fc12dfSblueswir1             fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
38231fc12dfSblueswir1                     (long)pc, trapnr);
38331fc12dfSblueswir1             abort();
38431fc12dfSblueswir1         }
38531fc12dfSblueswir1         process_pending_signals(env);
38631fc12dfSblueswir1     }
38731fc12dfSblueswir1 }
38831fc12dfSblueswir1 #endif
38931fc12dfSblueswir1 
39084778508Sblueswir1 #ifdef TARGET_SPARC
39184778508Sblueswir1 #define SPARC64_STACK_BIAS 2047
39284778508Sblueswir1 
39384778508Sblueswir1 //#define DEBUG_WIN
39484778508Sblueswir1 /* WARNING: dealing with register windows _is_ complicated. More info
39584778508Sblueswir1    can be found at http://www.sics.se/~psm/sparcstack.html */
39684778508Sblueswir1 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
39784778508Sblueswir1 {
39884778508Sblueswir1     index = (index + cwp * 16) % (16 * env->nwindows);
39984778508Sblueswir1     /* wrap handling : if cwp is on the last window, then we use the
40084778508Sblueswir1        registers 'after' the end */
40184778508Sblueswir1     if (index < 8 && env->cwp == env->nwindows - 1)
40284778508Sblueswir1         index += 16 * env->nwindows;
40384778508Sblueswir1     return index;
40484778508Sblueswir1 }
40584778508Sblueswir1 
40684778508Sblueswir1 /* save the register window 'cwp1' */
40784778508Sblueswir1 static inline void save_window_offset(CPUSPARCState *env, int cwp1)
40884778508Sblueswir1 {
40984778508Sblueswir1     unsigned int i;
41084778508Sblueswir1     abi_ulong sp_ptr;
41184778508Sblueswir1 
41284778508Sblueswir1     sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
41384778508Sblueswir1 #ifdef TARGET_SPARC64
41484778508Sblueswir1     if (sp_ptr & 3)
41584778508Sblueswir1         sp_ptr += SPARC64_STACK_BIAS;
41684778508Sblueswir1 #endif
41784778508Sblueswir1 #if defined(DEBUG_WIN)
41884778508Sblueswir1     printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
41984778508Sblueswir1            sp_ptr, cwp1);
42084778508Sblueswir1 #endif
42184778508Sblueswir1     for(i = 0; i < 16; i++) {
42284778508Sblueswir1         /* FIXME - what to do if put_user() fails? */
42384778508Sblueswir1         put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
42484778508Sblueswir1         sp_ptr += sizeof(abi_ulong);
42584778508Sblueswir1     }
42684778508Sblueswir1 }
42784778508Sblueswir1 
42884778508Sblueswir1 static void save_window(CPUSPARCState *env)
42984778508Sblueswir1 {
43084778508Sblueswir1 #ifndef TARGET_SPARC64
43184778508Sblueswir1     unsigned int new_wim;
43284778508Sblueswir1     new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
43384778508Sblueswir1         ((1LL << env->nwindows) - 1);
43484778508Sblueswir1     save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
43584778508Sblueswir1     env->wim = new_wim;
43684778508Sblueswir1 #else
43784778508Sblueswir1     save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
43884778508Sblueswir1     env->cansave++;
43984778508Sblueswir1     env->canrestore--;
44084778508Sblueswir1 #endif
44184778508Sblueswir1 }
44284778508Sblueswir1 
44384778508Sblueswir1 static void restore_window(CPUSPARCState *env)
44484778508Sblueswir1 {
44584778508Sblueswir1 #ifndef TARGET_SPARC64
44684778508Sblueswir1     unsigned int new_wim;
44784778508Sblueswir1 #endif
44884778508Sblueswir1     unsigned int i, cwp1;
44984778508Sblueswir1     abi_ulong sp_ptr;
45084778508Sblueswir1 
45184778508Sblueswir1 #ifndef TARGET_SPARC64
45284778508Sblueswir1     new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
45384778508Sblueswir1         ((1LL << env->nwindows) - 1);
45484778508Sblueswir1 #endif
45584778508Sblueswir1 
45684778508Sblueswir1     /* restore the invalid window */
45784778508Sblueswir1     cwp1 = cpu_cwp_inc(env, env->cwp + 1);
45884778508Sblueswir1     sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
45984778508Sblueswir1 #ifdef TARGET_SPARC64
46084778508Sblueswir1     if (sp_ptr & 3)
46184778508Sblueswir1         sp_ptr += SPARC64_STACK_BIAS;
46284778508Sblueswir1 #endif
46384778508Sblueswir1 #if defined(DEBUG_WIN)
46484778508Sblueswir1     printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
46584778508Sblueswir1            sp_ptr, cwp1);
46684778508Sblueswir1 #endif
46784778508Sblueswir1     for(i = 0; i < 16; i++) {
46884778508Sblueswir1         /* FIXME - what to do if get_user() fails? */
46984778508Sblueswir1         get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
47084778508Sblueswir1         sp_ptr += sizeof(abi_ulong);
47184778508Sblueswir1     }
47284778508Sblueswir1 #ifdef TARGET_SPARC64
47384778508Sblueswir1     env->canrestore++;
47484778508Sblueswir1     if (env->cleanwin < env->nwindows - 1)
47584778508Sblueswir1         env->cleanwin++;
47684778508Sblueswir1     env->cansave--;
47784778508Sblueswir1 #else
47884778508Sblueswir1     env->wim = new_wim;
47984778508Sblueswir1 #endif
48084778508Sblueswir1 }
48184778508Sblueswir1 
48284778508Sblueswir1 static void flush_windows(CPUSPARCState *env)
48384778508Sblueswir1 {
48484778508Sblueswir1     int offset, cwp1;
48584778508Sblueswir1 
48684778508Sblueswir1     offset = 1;
48784778508Sblueswir1     for(;;) {
48884778508Sblueswir1         /* if restore would invoke restore_window(), then we can stop */
48984778508Sblueswir1         cwp1 = cpu_cwp_inc(env, env->cwp + offset);
49084778508Sblueswir1 #ifndef TARGET_SPARC64
49184778508Sblueswir1         if (env->wim & (1 << cwp1))
49284778508Sblueswir1             break;
49384778508Sblueswir1 #else
49484778508Sblueswir1         if (env->canrestore == 0)
49584778508Sblueswir1             break;
49684778508Sblueswir1         env->cansave++;
49784778508Sblueswir1         env->canrestore--;
49884778508Sblueswir1 #endif
49984778508Sblueswir1         save_window_offset(env, cwp1);
50084778508Sblueswir1         offset++;
50184778508Sblueswir1     }
50284778508Sblueswir1     cwp1 = cpu_cwp_inc(env, env->cwp + 1);
50384778508Sblueswir1 #ifndef TARGET_SPARC64
50484778508Sblueswir1     /* set wim so that restore will reload the registers */
50584778508Sblueswir1     env->wim = 1 << cwp1;
50684778508Sblueswir1 #endif
50784778508Sblueswir1 #if defined(DEBUG_WIN)
50884778508Sblueswir1     printf("flush_windows: nb=%d\n", offset - 1);
50984778508Sblueswir1 #endif
51084778508Sblueswir1 }
51184778508Sblueswir1 
51278cfb07fSJuergen Lock void cpu_loop(CPUSPARCState *env)
51384778508Sblueswir1 {
514878096eeSAndreas Färber     CPUState *cs = CPU(sparc_env_get_cpu(env));
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);
663878096eeSAndreas Färber             cpu_dump_state(cs, stderr, fprintf, 0);
66484778508Sblueswir1             exit (1);
66584778508Sblueswir1         }
66684778508Sblueswir1         process_pending_signals (env);
66784778508Sblueswir1     }
66884778508Sblueswir1 }
66984778508Sblueswir1 
67084778508Sblueswir1 #endif
67184778508Sblueswir1 
67284778508Sblueswir1 static void usage(void)
67384778508Sblueswir1 {
6742e59915dSPaolo Bonzini     printf("qemu-" TARGET_NAME " version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
6752e59915dSPaolo Bonzini            "usage: qemu-" TARGET_NAME " [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"
683c8057f95SPeter Maydell            "-cpu model        select CPU (-cpu help 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"
693989b697dSPeter Maydell            "-d item1[,...]    enable logging of specified items\n"
694989b697dSPeter Maydell            "                  (use '-d help' for a list of log items)\n"
695989b697dSPeter Maydell            "-D logfile        write logs to 'logfile' (default stderr)\n"
69684778508Sblueswir1            "-p pagesize       set the host page size to 'pagesize'\n"
6971b530a6dSaurel32            "-singlestep       always run in singlestep mode\n"
69884778508Sblueswir1            "-strace           log system calls\n"
69984778508Sblueswir1            "\n"
70084778508Sblueswir1            "Environment variables:\n"
70184778508Sblueswir1            "QEMU_STRACE       Print system calls and arguments similar to the\n"
70284778508Sblueswir1            "                  'strace' program.  Enable by setting to any value.\n"
703fc0d96b4SBlue Swirl            "You can use -E and -U options to set/unset environment variables\n"
704fc0d96b4SBlue Swirl            "for target process.  It is possible to provide several variables\n"
705fc0d96b4SBlue Swirl            "by repeating the option.  For example:\n"
706fc0d96b4SBlue Swirl            "    -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
707fc0d96b4SBlue Swirl            "Note that if you provide several changes to single variable\n"
708fc0d96b4SBlue Swirl            "last change will stay in effect.\n"
70984778508Sblueswir1            ,
7102e59915dSPaolo Bonzini            TARGET_NAME,
71184778508Sblueswir1            interp_prefix,
712989b697dSPeter Maydell            x86_stack_size);
7132d18e637Sblueswir1     exit(1);
71484778508Sblueswir1 }
71584778508Sblueswir1 
716*dca1173cSAndreas Färber THREAD CPUState *thread_cpu;
71784778508Sblueswir1 
71884778508Sblueswir1 /* Assumes contents are already zeroed.  */
71984778508Sblueswir1 void init_task_state(TaskState *ts)
72084778508Sblueswir1 {
72184778508Sblueswir1     int i;
72284778508Sblueswir1 
72384778508Sblueswir1     ts->used = 1;
72484778508Sblueswir1     ts->first_free = ts->sigqueue_table;
72584778508Sblueswir1     for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
72684778508Sblueswir1         ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
72784778508Sblueswir1     }
72884778508Sblueswir1     ts->sigqueue_table[i].next = NULL;
72984778508Sblueswir1 }
73084778508Sblueswir1 
73184778508Sblueswir1 int main(int argc, char **argv)
73284778508Sblueswir1 {
73384778508Sblueswir1     const char *filename;
73484778508Sblueswir1     const char *cpu_model;
735989b697dSPeter Maydell     const char *log_file = NULL;
736c235d738SMatthew Fernandez     const char *log_mask = NULL;
73784778508Sblueswir1     struct target_pt_regs regs1, *regs = &regs1;
73884778508Sblueswir1     struct image_info info1, *info = &info1;
73984778508Sblueswir1     TaskState ts1, *ts = &ts1;
7409349b4f9SAndreas Färber     CPUArchState *env;
74184778508Sblueswir1     int optind;
74284778508Sblueswir1     const char *r;
74384778508Sblueswir1     int gdbstub_port = 0;
744fc0d96b4SBlue Swirl     char **target_environ, **wrk;
745fc0d96b4SBlue Swirl     envlist_t *envlist = NULL;
74678cfb07fSJuergen Lock     bsd_type = target_openbsd;
74784778508Sblueswir1 
74884778508Sblueswir1     if (argc <= 1)
74984778508Sblueswir1         usage();
75084778508Sblueswir1 
751ce008c1fSAndreas Färber     module_call_init(MODULE_INIT_QOM);
752ce008c1fSAndreas Färber 
753fc0d96b4SBlue Swirl     if ((envlist = envlist_create()) == NULL) {
754fc0d96b4SBlue Swirl         (void) fprintf(stderr, "Unable to allocate envlist\n");
755fc0d96b4SBlue Swirl         exit(1);
756fc0d96b4SBlue Swirl     }
757fc0d96b4SBlue Swirl 
758fc0d96b4SBlue Swirl     /* add current environment into the list */
759fc0d96b4SBlue Swirl     for (wrk = environ; *wrk != NULL; wrk++) {
760fc0d96b4SBlue Swirl         (void) envlist_setenv(envlist, *wrk);
761fc0d96b4SBlue Swirl     }
762fc0d96b4SBlue Swirl 
76384778508Sblueswir1     cpu_model = NULL;
7640c62de2fSJuergen Lock #if defined(cpudef_setup)
7650c62de2fSJuergen Lock     cpudef_setup(); /* parse cpu definitions in target config file (TBD) */
7660c62de2fSJuergen Lock #endif
7670c62de2fSJuergen Lock 
76884778508Sblueswir1     optind = 1;
76984778508Sblueswir1     for(;;) {
77084778508Sblueswir1         if (optind >= argc)
77184778508Sblueswir1             break;
77284778508Sblueswir1         r = argv[optind];
77384778508Sblueswir1         if (r[0] != '-')
77484778508Sblueswir1             break;
77584778508Sblueswir1         optind++;
77684778508Sblueswir1         r++;
77784778508Sblueswir1         if (!strcmp(r, "-")) {
77884778508Sblueswir1             break;
77984778508Sblueswir1         } else if (!strcmp(r, "d")) {
780c235d738SMatthew Fernandez             if (optind >= argc) {
78184778508Sblueswir1                 break;
78284778508Sblueswir1             }
783c235d738SMatthew Fernandez             log_mask = argv[optind++];
784c235d738SMatthew Fernandez         } else if (!strcmp(r, "D")) {
785c235d738SMatthew Fernandez             if (optind >= argc) {
786c235d738SMatthew Fernandez                 break;
78784778508Sblueswir1             }
788c235d738SMatthew Fernandez             log_file = argv[optind++];
789fc0d96b4SBlue Swirl         } else if (!strcmp(r, "E")) {
790fc0d96b4SBlue Swirl             r = argv[optind++];
791fc0d96b4SBlue Swirl             if (envlist_setenv(envlist, r) != 0)
792fc0d96b4SBlue Swirl                 usage();
793f66724c9SStefan Weil         } else if (!strcmp(r, "ignore-environment")) {
794f66724c9SStefan Weil             envlist_free(envlist);
795f66724c9SStefan Weil             if ((envlist = envlist_create()) == NULL) {
796f66724c9SStefan Weil                 (void) fprintf(stderr, "Unable to allocate envlist\n");
797f66724c9SStefan Weil                 exit(1);
798f66724c9SStefan Weil             }
799fc0d96b4SBlue Swirl         } else if (!strcmp(r, "U")) {
800fc0d96b4SBlue Swirl             r = argv[optind++];
801fc0d96b4SBlue Swirl             if (envlist_unsetenv(envlist, r) != 0)
802fc0d96b4SBlue Swirl                 usage();
80384778508Sblueswir1         } else if (!strcmp(r, "s")) {
80484778508Sblueswir1             r = argv[optind++];
80584778508Sblueswir1             x86_stack_size = strtol(r, (char **)&r, 0);
80684778508Sblueswir1             if (x86_stack_size <= 0)
80784778508Sblueswir1                 usage();
80884778508Sblueswir1             if (*r == 'M')
80984778508Sblueswir1                 x86_stack_size *= 1024 * 1024;
81084778508Sblueswir1             else if (*r == 'k' || *r == 'K')
81184778508Sblueswir1                 x86_stack_size *= 1024;
81284778508Sblueswir1         } else if (!strcmp(r, "L")) {
81384778508Sblueswir1             interp_prefix = argv[optind++];
81484778508Sblueswir1         } else if (!strcmp(r, "p")) {
81584778508Sblueswir1             qemu_host_page_size = atoi(argv[optind++]);
81684778508Sblueswir1             if (qemu_host_page_size == 0 ||
81784778508Sblueswir1                 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
81884778508Sblueswir1                 fprintf(stderr, "page size must be a power of two\n");
81984778508Sblueswir1                 exit(1);
82084778508Sblueswir1             }
82184778508Sblueswir1         } else if (!strcmp(r, "g")) {
82284778508Sblueswir1             gdbstub_port = atoi(argv[optind++]);
82384778508Sblueswir1         } else if (!strcmp(r, "r")) {
82484778508Sblueswir1             qemu_uname_release = argv[optind++];
82584778508Sblueswir1         } else if (!strcmp(r, "cpu")) {
82684778508Sblueswir1             cpu_model = argv[optind++];
827c8057f95SPeter Maydell             if (is_help_option(cpu_model)) {
82884778508Sblueswir1 /* XXX: implement xxx_cpu_list for targets that still miss it */
82984778508Sblueswir1 #if defined(cpu_list)
83084778508Sblueswir1                     cpu_list(stdout, &fprintf);
83184778508Sblueswir1 #endif
8322d18e637Sblueswir1                 exit(1);
83384778508Sblueswir1             }
8342fa5d9baSBlue Swirl #if defined(CONFIG_USE_GUEST_BASE)
8352fa5d9baSBlue Swirl         } else if (!strcmp(r, "B")) {
8362fa5d9baSBlue Swirl            guest_base = strtol(argv[optind++], NULL, 0);
8372fa5d9baSBlue Swirl            have_guest_base = 1;
8382fa5d9baSBlue Swirl #endif
83984778508Sblueswir1         } else if (!strcmp(r, "drop-ld-preload")) {
840fc0d96b4SBlue Swirl             (void) envlist_unsetenv(envlist, "LD_PRELOAD");
84184778508Sblueswir1         } else if (!strcmp(r, "bsd")) {
84284778508Sblueswir1             if (!strcasecmp(argv[optind], "freebsd")) {
84384778508Sblueswir1                 bsd_type = target_freebsd;
84484778508Sblueswir1             } else if (!strcasecmp(argv[optind], "netbsd")) {
84584778508Sblueswir1                 bsd_type = target_netbsd;
84684778508Sblueswir1             } else if (!strcasecmp(argv[optind], "openbsd")) {
84784778508Sblueswir1                 bsd_type = target_openbsd;
84884778508Sblueswir1             } else {
84984778508Sblueswir1                 usage();
85084778508Sblueswir1             }
85184778508Sblueswir1             optind++;
8521b530a6dSaurel32         } else if (!strcmp(r, "singlestep")) {
8531b530a6dSaurel32             singlestep = 1;
85484778508Sblueswir1         } else if (!strcmp(r, "strace")) {
85584778508Sblueswir1             do_strace = 1;
85684778508Sblueswir1         } else
85784778508Sblueswir1         {
85884778508Sblueswir1             usage();
85984778508Sblueswir1         }
86084778508Sblueswir1     }
86184778508Sblueswir1 
862c235d738SMatthew Fernandez     /* init debug */
8639a7e5424SPeter Maydell     qemu_set_log_filename(log_file);
864c235d738SMatthew Fernandez     if (log_mask) {
865c235d738SMatthew Fernandez         int mask;
866c235d738SMatthew Fernandez 
8674fde1ebaSPeter Maydell         mask = qemu_str_to_log_mask(log_mask);
868c235d738SMatthew Fernandez         if (!mask) {
86959a6fa6eSPeter Maydell             qemu_print_log_usage(stdout);
870c235d738SMatthew Fernandez             exit(1);
871c235d738SMatthew Fernandez         }
87224537a01SPeter Maydell         qemu_set_log(mask);
873c235d738SMatthew Fernandez     }
874c235d738SMatthew Fernandez 
8754b5dfd82SPeter Maydell     if (optind >= argc) {
8764b5dfd82SPeter Maydell         usage();
8774b5dfd82SPeter Maydell     }
8784b5dfd82SPeter Maydell     filename = argv[optind];
8794b5dfd82SPeter Maydell 
88084778508Sblueswir1     /* Zero out regs */
88184778508Sblueswir1     memset(regs, 0, sizeof(struct target_pt_regs));
88284778508Sblueswir1 
88384778508Sblueswir1     /* Zero out image_info */
88484778508Sblueswir1     memset(info, 0, sizeof(struct image_info));
88584778508Sblueswir1 
88684778508Sblueswir1     /* Scan interp_prefix dir for replacement files. */
88784778508Sblueswir1     init_paths(interp_prefix);
88884778508Sblueswir1 
88984778508Sblueswir1     if (cpu_model == NULL) {
89031fc12dfSblueswir1 #if defined(TARGET_I386)
89131fc12dfSblueswir1 #ifdef TARGET_X86_64
89231fc12dfSblueswir1         cpu_model = "qemu64";
89331fc12dfSblueswir1 #else
89431fc12dfSblueswir1         cpu_model = "qemu32";
89531fc12dfSblueswir1 #endif
89631fc12dfSblueswir1 #elif defined(TARGET_SPARC)
89784778508Sblueswir1 #ifdef TARGET_SPARC64
89884778508Sblueswir1         cpu_model = "TI UltraSparc II";
89984778508Sblueswir1 #else
90084778508Sblueswir1         cpu_model = "Fujitsu MB86904";
90184778508Sblueswir1 #endif
90284778508Sblueswir1 #else
90384778508Sblueswir1         cpu_model = "any";
90484778508Sblueswir1 #endif
90584778508Sblueswir1     }
906d5ab9713SJan Kiszka     tcg_exec_init(0);
907d5ab9713SJan Kiszka     cpu_exec_init_all();
90884778508Sblueswir1     /* NOTE: we need to init the CPU at this stage to get
90984778508Sblueswir1        qemu_host_page_size */
91084778508Sblueswir1     env = cpu_init(cpu_model);
91184778508Sblueswir1     if (!env) {
91284778508Sblueswir1         fprintf(stderr, "Unable to find CPU definition\n");
91384778508Sblueswir1         exit(1);
91484778508Sblueswir1     }
91577868120SAndreas Färber #if defined(TARGET_SPARC) || defined(TARGET_PPC)
916fdcb4fb6SAndreas Färber     cpu_reset(ENV_GET_CPU(env));
917b55a37c9SBlue Swirl #endif
918*dca1173cSAndreas Färber     thread_cpu = ENV_GET_CPU(env);
91984778508Sblueswir1 
92084778508Sblueswir1     if (getenv("QEMU_STRACE")) {
92184778508Sblueswir1         do_strace = 1;
92284778508Sblueswir1     }
92384778508Sblueswir1 
924fc0d96b4SBlue Swirl     target_environ = envlist_to_environ(envlist, NULL);
925fc0d96b4SBlue Swirl     envlist_free(envlist);
92684778508Sblueswir1 
9272fa5d9baSBlue Swirl #if defined(CONFIG_USE_GUEST_BASE)
9282fa5d9baSBlue Swirl     /*
9292fa5d9baSBlue Swirl      * Now that page sizes are configured in cpu_init() we can do
9302fa5d9baSBlue Swirl      * proper page alignment for guest_base.
9312fa5d9baSBlue Swirl      */
9322fa5d9baSBlue Swirl     guest_base = HOST_PAGE_ALIGN(guest_base);
9332fa5d9baSBlue Swirl 
9342fa5d9baSBlue Swirl     /*
9352fa5d9baSBlue Swirl      * Read in mmap_min_addr kernel parameter.  This value is used
9362fa5d9baSBlue Swirl      * When loading the ELF image to determine whether guest_base
9372fa5d9baSBlue Swirl      * is needed.
9382fa5d9baSBlue Swirl      *
9392fa5d9baSBlue Swirl      * When user has explicitly set the quest base, we skip this
9402fa5d9baSBlue Swirl      * test.
9412fa5d9baSBlue Swirl      */
9422fa5d9baSBlue Swirl     if (!have_guest_base) {
9432fa5d9baSBlue Swirl         FILE *fp;
9442fa5d9baSBlue Swirl 
9452fa5d9baSBlue Swirl         if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
9462fa5d9baSBlue Swirl             unsigned long tmp;
9472fa5d9baSBlue Swirl             if (fscanf(fp, "%lu", &tmp) == 1) {
9482fa5d9baSBlue Swirl                 mmap_min_addr = tmp;
9492fa5d9baSBlue Swirl                 qemu_log("host mmap_min_addr=0x%lx\n", mmap_min_addr);
9502fa5d9baSBlue Swirl             }
9512fa5d9baSBlue Swirl             fclose(fp);
9522fa5d9baSBlue Swirl         }
9532fa5d9baSBlue Swirl     }
9542fa5d9baSBlue Swirl #endif /* CONFIG_USE_GUEST_BASE */
95584778508Sblueswir1 
95684778508Sblueswir1     if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
95784778508Sblueswir1         printf("Error loading %s\n", filename);
95884778508Sblueswir1         _exit(1);
95984778508Sblueswir1     }
96084778508Sblueswir1 
96184778508Sblueswir1     for (wrk = target_environ; *wrk; wrk++) {
96284778508Sblueswir1         free(*wrk);
96384778508Sblueswir1     }
96484778508Sblueswir1 
96584778508Sblueswir1     free(target_environ);
96684778508Sblueswir1 
9672e77eac6Sblueswir1     if (qemu_log_enabled()) {
9682fa5d9baSBlue Swirl #if defined(CONFIG_USE_GUEST_BASE)
9692fa5d9baSBlue Swirl         qemu_log("guest_base  0x%lx\n", guest_base);
9702fa5d9baSBlue Swirl #endif
97193fcfe39Saliguori         log_page_dump();
97284778508Sblueswir1 
97393fcfe39Saliguori         qemu_log("start_brk   0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
97493fcfe39Saliguori         qemu_log("end_code    0x" TARGET_ABI_FMT_lx "\n", info->end_code);
97593fcfe39Saliguori         qemu_log("start_code  0x" TARGET_ABI_FMT_lx "\n",
97684778508Sblueswir1                  info->start_code);
97793fcfe39Saliguori         qemu_log("start_data  0x" TARGET_ABI_FMT_lx "\n",
97884778508Sblueswir1                  info->start_data);
97993fcfe39Saliguori         qemu_log("end_data    0x" TARGET_ABI_FMT_lx "\n", info->end_data);
98093fcfe39Saliguori         qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
98184778508Sblueswir1                  info->start_stack);
98293fcfe39Saliguori         qemu_log("brk         0x" TARGET_ABI_FMT_lx "\n", info->brk);
98393fcfe39Saliguori         qemu_log("entry       0x" TARGET_ABI_FMT_lx "\n", info->entry);
9842e77eac6Sblueswir1     }
98584778508Sblueswir1 
98684778508Sblueswir1     target_set_brk(info->brk);
98784778508Sblueswir1     syscall_init();
98884778508Sblueswir1     signal_init();
98984778508Sblueswir1 
9909002ec79SRichard Henderson #if defined(CONFIG_USE_GUEST_BASE)
9919002ec79SRichard Henderson     /* Now that we've loaded the binary, GUEST_BASE is fixed.  Delay
9929002ec79SRichard Henderson        generating the prologue until now so that the prologue can take
9939002ec79SRichard Henderson        the real value of GUEST_BASE into account.  */
9949002ec79SRichard Henderson     tcg_prologue_init(&tcg_ctx);
9959002ec79SRichard Henderson #endif
9969002ec79SRichard Henderson 
99784778508Sblueswir1     /* build Task State */
99884778508Sblueswir1     memset(ts, 0, sizeof(TaskState));
99984778508Sblueswir1     init_task_state(ts);
100084778508Sblueswir1     ts->info = info;
100184778508Sblueswir1     env->opaque = ts;
100284778508Sblueswir1 
100331fc12dfSblueswir1 #if defined(TARGET_I386)
100431fc12dfSblueswir1     cpu_x86_set_cpl(env, 3);
100531fc12dfSblueswir1 
100631fc12dfSblueswir1     env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
100731fc12dfSblueswir1     env->hflags |= HF_PE_MASK;
10080514ef2fSEduardo Habkost     if (env->features[FEAT_1_EDX] & CPUID_SSE) {
100931fc12dfSblueswir1         env->cr[4] |= CR4_OSFXSR_MASK;
101031fc12dfSblueswir1         env->hflags |= HF_OSFXSR_MASK;
101131fc12dfSblueswir1     }
101231fc12dfSblueswir1 #ifndef TARGET_ABI32
101331fc12dfSblueswir1     /* enable 64 bit mode if possible */
10140514ef2fSEduardo Habkost     if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) {
101531fc12dfSblueswir1         fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
101631fc12dfSblueswir1         exit(1);
101731fc12dfSblueswir1     }
101831fc12dfSblueswir1     env->cr[4] |= CR4_PAE_MASK;
101931fc12dfSblueswir1     env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
102031fc12dfSblueswir1     env->hflags |= HF_LMA_MASK;
102131fc12dfSblueswir1 #endif
102231fc12dfSblueswir1 
102331fc12dfSblueswir1     /* flags setup : we activate the IRQs by default as in user mode */
102431fc12dfSblueswir1     env->eflags |= IF_MASK;
102531fc12dfSblueswir1 
102631fc12dfSblueswir1     /* linux register setup */
102731fc12dfSblueswir1 #ifndef TARGET_ABI32
102831fc12dfSblueswir1     env->regs[R_EAX] = regs->rax;
102931fc12dfSblueswir1     env->regs[R_EBX] = regs->rbx;
103031fc12dfSblueswir1     env->regs[R_ECX] = regs->rcx;
103131fc12dfSblueswir1     env->regs[R_EDX] = regs->rdx;
103231fc12dfSblueswir1     env->regs[R_ESI] = regs->rsi;
103331fc12dfSblueswir1     env->regs[R_EDI] = regs->rdi;
103431fc12dfSblueswir1     env->regs[R_EBP] = regs->rbp;
103531fc12dfSblueswir1     env->regs[R_ESP] = regs->rsp;
103631fc12dfSblueswir1     env->eip = regs->rip;
103731fc12dfSblueswir1 #else
103831fc12dfSblueswir1     env->regs[R_EAX] = regs->eax;
103931fc12dfSblueswir1     env->regs[R_EBX] = regs->ebx;
104031fc12dfSblueswir1     env->regs[R_ECX] = regs->ecx;
104131fc12dfSblueswir1     env->regs[R_EDX] = regs->edx;
104231fc12dfSblueswir1     env->regs[R_ESI] = regs->esi;
104331fc12dfSblueswir1     env->regs[R_EDI] = regs->edi;
104431fc12dfSblueswir1     env->regs[R_EBP] = regs->ebp;
104531fc12dfSblueswir1     env->regs[R_ESP] = regs->esp;
104631fc12dfSblueswir1     env->eip = regs->eip;
104731fc12dfSblueswir1 #endif
104831fc12dfSblueswir1 
104931fc12dfSblueswir1     /* linux interrupt setup */
105031fc12dfSblueswir1 #ifndef TARGET_ABI32
105131fc12dfSblueswir1     env->idt.limit = 511;
105231fc12dfSblueswir1 #else
105331fc12dfSblueswir1     env->idt.limit = 255;
105431fc12dfSblueswir1 #endif
105531fc12dfSblueswir1     env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
105631fc12dfSblueswir1                                 PROT_READ|PROT_WRITE,
105731fc12dfSblueswir1                                 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
105831fc12dfSblueswir1     idt_table = g2h(env->idt.base);
105931fc12dfSblueswir1     set_idt(0, 0);
106031fc12dfSblueswir1     set_idt(1, 0);
106131fc12dfSblueswir1     set_idt(2, 0);
106231fc12dfSblueswir1     set_idt(3, 3);
106331fc12dfSblueswir1     set_idt(4, 3);
106431fc12dfSblueswir1     set_idt(5, 0);
106531fc12dfSblueswir1     set_idt(6, 0);
106631fc12dfSblueswir1     set_idt(7, 0);
106731fc12dfSblueswir1     set_idt(8, 0);
106831fc12dfSblueswir1     set_idt(9, 0);
106931fc12dfSblueswir1     set_idt(10, 0);
107031fc12dfSblueswir1     set_idt(11, 0);
107131fc12dfSblueswir1     set_idt(12, 0);
107231fc12dfSblueswir1     set_idt(13, 0);
107331fc12dfSblueswir1     set_idt(14, 0);
107431fc12dfSblueswir1     set_idt(15, 0);
107531fc12dfSblueswir1     set_idt(16, 0);
107631fc12dfSblueswir1     set_idt(17, 0);
107731fc12dfSblueswir1     set_idt(18, 0);
107831fc12dfSblueswir1     set_idt(19, 0);
107931fc12dfSblueswir1     set_idt(0x80, 3);
108031fc12dfSblueswir1 
108131fc12dfSblueswir1     /* linux segment setup */
108231fc12dfSblueswir1     {
108331fc12dfSblueswir1         uint64_t *gdt_table;
108431fc12dfSblueswir1         env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
108531fc12dfSblueswir1                                     PROT_READ|PROT_WRITE,
108631fc12dfSblueswir1                                     MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
108731fc12dfSblueswir1         env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
108831fc12dfSblueswir1         gdt_table = g2h(env->gdt.base);
108931fc12dfSblueswir1 #ifdef TARGET_ABI32
109031fc12dfSblueswir1         write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
109131fc12dfSblueswir1                  DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
109231fc12dfSblueswir1                  (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
109331fc12dfSblueswir1 #else
109431fc12dfSblueswir1         /* 64 bit code segment */
109531fc12dfSblueswir1         write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
109631fc12dfSblueswir1                  DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
109731fc12dfSblueswir1                  DESC_L_MASK |
109831fc12dfSblueswir1                  (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
109931fc12dfSblueswir1 #endif
110031fc12dfSblueswir1         write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
110131fc12dfSblueswir1                  DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
110231fc12dfSblueswir1                  (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
110331fc12dfSblueswir1     }
110431fc12dfSblueswir1 
110531fc12dfSblueswir1     cpu_x86_load_seg(env, R_CS, __USER_CS);
110631fc12dfSblueswir1     cpu_x86_load_seg(env, R_SS, __USER_DS);
110731fc12dfSblueswir1 #ifdef TARGET_ABI32
110831fc12dfSblueswir1     cpu_x86_load_seg(env, R_DS, __USER_DS);
110931fc12dfSblueswir1     cpu_x86_load_seg(env, R_ES, __USER_DS);
111031fc12dfSblueswir1     cpu_x86_load_seg(env, R_FS, __USER_DS);
111131fc12dfSblueswir1     cpu_x86_load_seg(env, R_GS, __USER_DS);
111231fc12dfSblueswir1     /* This hack makes Wine work... */
111331fc12dfSblueswir1     env->segs[R_FS].selector = 0;
111431fc12dfSblueswir1 #else
111531fc12dfSblueswir1     cpu_x86_load_seg(env, R_DS, 0);
111631fc12dfSblueswir1     cpu_x86_load_seg(env, R_ES, 0);
111731fc12dfSblueswir1     cpu_x86_load_seg(env, R_FS, 0);
111831fc12dfSblueswir1     cpu_x86_load_seg(env, R_GS, 0);
111931fc12dfSblueswir1 #endif
112031fc12dfSblueswir1 #elif defined(TARGET_SPARC)
112184778508Sblueswir1     {
112284778508Sblueswir1         int i;
112384778508Sblueswir1         env->pc = regs->pc;
112484778508Sblueswir1         env->npc = regs->npc;
112584778508Sblueswir1         env->y = regs->y;
112684778508Sblueswir1         for(i = 0; i < 8; i++)
112784778508Sblueswir1             env->gregs[i] = regs->u_regs[i];
112884778508Sblueswir1         for(i = 0; i < 8; i++)
112984778508Sblueswir1             env->regwptr[i] = regs->u_regs[i + 8];
113084778508Sblueswir1     }
113184778508Sblueswir1 #else
113284778508Sblueswir1 #error unsupported target CPU
113384778508Sblueswir1 #endif
113484778508Sblueswir1 
113584778508Sblueswir1     if (gdbstub_port) {
113684778508Sblueswir1         gdbserver_start (gdbstub_port);
113784778508Sblueswir1         gdb_handlesig(env, 0);
113884778508Sblueswir1     }
113978cfb07fSJuergen Lock     cpu_loop(env);
114084778508Sblueswir1     /* never exits */
114184778508Sblueswir1     return 0;
114284778508Sblueswir1 }
1143