xref: /openbmc/qemu/bsd-user/main.c (revision b1311c4a)
184778508Sblueswir1 /*
284778508Sblueswir1  *  qemu user main
384778508Sblueswir1  *
484778508Sblueswir1  *  Copyright (c) 2003-2008 Fabrice Bellard
584778508Sblueswir1  *
684778508Sblueswir1  *  This program is free software; you can redistribute it and/or modify
784778508Sblueswir1  *  it under the terms of the GNU General Public License as published by
884778508Sblueswir1  *  the Free Software Foundation; either version 2 of the License, or
984778508Sblueswir1  *  (at your option) any later version.
1084778508Sblueswir1  *
1184778508Sblueswir1  *  This program is distributed in the hope that it will be useful,
1284778508Sblueswir1  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
1384778508Sblueswir1  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1484778508Sblueswir1  *  GNU General Public License for more details.
1584778508Sblueswir1  *
1684778508Sblueswir1  *  You should have received a copy of the GNU General Public License
178167ee88SBlue Swirl  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
1884778508Sblueswir1  */
192231197cSPeter Maydell #include "qemu/osdep.h"
208c1c230aSEd Maste #include "qemu-version.h"
2184778508Sblueswir1 #include <machine/trap.h>
2284778508Sblueswir1 
23daa76aa4SMarkus Armbruster #include "qapi/error.h"
2484778508Sblueswir1 #include "qemu.h"
256913e79cSLluís Vilanova #include "qemu/config-file.h"
26f348b6d1SVeronia Bahaa #include "qemu/path.h"
27f348b6d1SVeronia Bahaa #include "qemu/help_option.h"
282b41f10eSBlue Swirl #include "cpu.h"
2963c91552SPaolo Bonzini #include "exec/exec-all.h"
309002ec79SRichard Henderson #include "tcg.h"
311de7afc9SPaolo Bonzini #include "qemu/timer.h"
321de7afc9SPaolo Bonzini #include "qemu/envlist.h"
33508127e2SPaolo Bonzini #include "exec/log.h"
346913e79cSLluís Vilanova #include "trace/control.h"
356913e79cSLluís Vilanova #include "glib-compat.h"
36fc0d96b4SBlue Swirl 
371b530a6dSaurel32 int singlestep;
382fa5d9baSBlue Swirl unsigned long mmap_min_addr;
392fa5d9baSBlue Swirl unsigned long guest_base;
402fa5d9baSBlue Swirl int have_guest_base;
41d6ef40bfSPeter Maydell unsigned long reserved_va;
421b530a6dSaurel32 
437ee2822cSPaolo Bonzini static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
44fccae322SPeter Maydell const char *qemu_uname_release;
4584778508Sblueswir1 extern char **environ;
4678cfb07fSJuergen Lock enum BSDType bsd_type;
4784778508Sblueswir1 
4884778508Sblueswir1 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
4984778508Sblueswir1    we allocate a bigger stack. Need a better solution, for example
5084778508Sblueswir1    by remapping the process stack directly at the right place */
5184778508Sblueswir1 unsigned long x86_stack_size = 512 * 1024;
5284778508Sblueswir1 
5384778508Sblueswir1 void gemu_log(const char *fmt, ...)
5484778508Sblueswir1 {
5584778508Sblueswir1     va_list ap;
5684778508Sblueswir1 
5784778508Sblueswir1     va_start(ap, fmt);
5884778508Sblueswir1     vfprintf(stderr, fmt, ap);
5984778508Sblueswir1     va_end(ap);
6084778508Sblueswir1 }
619399f095Sblueswir1 
6231fc12dfSblueswir1 #if defined(TARGET_I386)
63b98e9ca8SAndreas Färber int cpu_get_pic_interrupt(CPUX86State *env)
6431fc12dfSblueswir1 {
6531fc12dfSblueswir1     return -1;
6631fc12dfSblueswir1 }
6731fc12dfSblueswir1 #endif
6831fc12dfSblueswir1 
699399f095Sblueswir1 void fork_start(void)
709399f095Sblueswir1 {
719399f095Sblueswir1 }
729399f095Sblueswir1 
739399f095Sblueswir1 void fork_end(int child)
749399f095Sblueswir1 {
759399f095Sblueswir1     if (child) {
76f7ec7f7bSPeter Crosthwaite         gdbserver_fork(thread_cpu);
779399f095Sblueswir1     }
789399f095Sblueswir1 }
799399f095Sblueswir1 
8031fc12dfSblueswir1 #ifdef TARGET_I386
8131fc12dfSblueswir1 /***********************************************************/
8231fc12dfSblueswir1 /* CPUX86 core interface */
8331fc12dfSblueswir1 
8431fc12dfSblueswir1 uint64_t cpu_get_tsc(CPUX86State *env)
8531fc12dfSblueswir1 {
864a7428c5SChristopher Covington     return cpu_get_host_ticks();
8731fc12dfSblueswir1 }
8831fc12dfSblueswir1 
8931fc12dfSblueswir1 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
9031fc12dfSblueswir1                      int flags)
9131fc12dfSblueswir1 {
9231fc12dfSblueswir1     unsigned int e1, e2;
9331fc12dfSblueswir1     uint32_t *p;
9431fc12dfSblueswir1     e1 = (addr << 16) | (limit & 0xffff);
9531fc12dfSblueswir1     e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
9631fc12dfSblueswir1     e2 |= flags;
9731fc12dfSblueswir1     p = ptr;
9831fc12dfSblueswir1     p[0] = tswap32(e1);
9931fc12dfSblueswir1     p[1] = tswap32(e2);
10031fc12dfSblueswir1 }
10131fc12dfSblueswir1 
10231fc12dfSblueswir1 static uint64_t *idt_table;
10331fc12dfSblueswir1 #ifdef TARGET_X86_64
10431fc12dfSblueswir1 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
10531fc12dfSblueswir1                        uint64_t addr, unsigned int sel)
10631fc12dfSblueswir1 {
10731fc12dfSblueswir1     uint32_t *p, e1, e2;
10831fc12dfSblueswir1     e1 = (addr & 0xffff) | (sel << 16);
10931fc12dfSblueswir1     e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
11031fc12dfSblueswir1     p = ptr;
11131fc12dfSblueswir1     p[0] = tswap32(e1);
11231fc12dfSblueswir1     p[1] = tswap32(e2);
11331fc12dfSblueswir1     p[2] = tswap32(addr >> 32);
11431fc12dfSblueswir1     p[3] = 0;
11531fc12dfSblueswir1 }
11631fc12dfSblueswir1 /* only dpl matters as we do only user space emulation */
11731fc12dfSblueswir1 static void set_idt(int n, unsigned int dpl)
11831fc12dfSblueswir1 {
11931fc12dfSblueswir1     set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
12031fc12dfSblueswir1 }
12131fc12dfSblueswir1 #else
12231fc12dfSblueswir1 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
12331fc12dfSblueswir1                      uint32_t addr, unsigned int sel)
12431fc12dfSblueswir1 {
12531fc12dfSblueswir1     uint32_t *p, e1, e2;
12631fc12dfSblueswir1     e1 = (addr & 0xffff) | (sel << 16);
12731fc12dfSblueswir1     e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
12831fc12dfSblueswir1     p = ptr;
12931fc12dfSblueswir1     p[0] = tswap32(e1);
13031fc12dfSblueswir1     p[1] = tswap32(e2);
13131fc12dfSblueswir1 }
13231fc12dfSblueswir1 
13331fc12dfSblueswir1 /* only dpl matters as we do only user space emulation */
13431fc12dfSblueswir1 static void set_idt(int n, unsigned int dpl)
13531fc12dfSblueswir1 {
13631fc12dfSblueswir1     set_gate(idt_table + n, 0, dpl, 0, 0);
13731fc12dfSblueswir1 }
13831fc12dfSblueswir1 #endif
13931fc12dfSblueswir1 
14078cfb07fSJuergen Lock void cpu_loop(CPUX86State *env)
14131fc12dfSblueswir1 {
142ea3e9847SPeter Crosthwaite     X86CPU *cpu = x86_env_get_cpu(env);
143ea3e9847SPeter Crosthwaite     CPUState *cs = CPU(cpu);
14431fc12dfSblueswir1     int trapnr;
14531fc12dfSblueswir1     abi_ulong pc;
14631fc12dfSblueswir1     //target_siginfo_t info;
14731fc12dfSblueswir1 
14831fc12dfSblueswir1     for(;;) {
149d148d90eSSergey Fedorov         cpu_exec_start(cs);
150ded554cdSSean Bruno         trapnr = cpu_exec(cs);
151d148d90eSSergey Fedorov         cpu_exec_end(cs);
152d148d90eSSergey Fedorov         process_queued_cpu_work(cs);
153d148d90eSSergey Fedorov 
15431fc12dfSblueswir1         switch(trapnr) {
15531fc12dfSblueswir1         case 0x80:
15631fc12dfSblueswir1             /* syscall from int $0x80 */
15778cfb07fSJuergen Lock             if (bsd_type == target_freebsd) {
15878cfb07fSJuergen Lock                 abi_ulong params = (abi_ulong) env->regs[R_ESP] +
15978cfb07fSJuergen Lock                     sizeof(int32_t);
16078cfb07fSJuergen Lock                 int32_t syscall_nr = env->regs[R_EAX];
16178cfb07fSJuergen Lock                 int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
16278cfb07fSJuergen Lock 
16378cfb07fSJuergen Lock                 if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
16478cfb07fSJuergen Lock                     get_user_s32(syscall_nr, params);
16578cfb07fSJuergen Lock                     params += sizeof(int32_t);
16678cfb07fSJuergen Lock                 } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
16778cfb07fSJuergen Lock                     get_user_s32(syscall_nr, params);
16878cfb07fSJuergen Lock                     params += sizeof(int64_t);
16978cfb07fSJuergen Lock                 }
17078cfb07fSJuergen Lock                 get_user_s32(arg1, params);
17178cfb07fSJuergen Lock                 params += sizeof(int32_t);
17278cfb07fSJuergen Lock                 get_user_s32(arg2, params);
17378cfb07fSJuergen Lock                 params += sizeof(int32_t);
17478cfb07fSJuergen Lock                 get_user_s32(arg3, params);
17578cfb07fSJuergen Lock                 params += sizeof(int32_t);
17678cfb07fSJuergen Lock                 get_user_s32(arg4, params);
17778cfb07fSJuergen Lock                 params += sizeof(int32_t);
17878cfb07fSJuergen Lock                 get_user_s32(arg5, params);
17978cfb07fSJuergen Lock                 params += sizeof(int32_t);
18078cfb07fSJuergen Lock                 get_user_s32(arg6, params);
18178cfb07fSJuergen Lock                 params += sizeof(int32_t);
18278cfb07fSJuergen Lock                 get_user_s32(arg7, params);
18378cfb07fSJuergen Lock                 params += sizeof(int32_t);
18478cfb07fSJuergen Lock                 get_user_s32(arg8, params);
18578cfb07fSJuergen Lock                 env->regs[R_EAX] = do_freebsd_syscall(env,
18678cfb07fSJuergen Lock                                                       syscall_nr,
18778cfb07fSJuergen Lock                                                       arg1,
18878cfb07fSJuergen Lock                                                       arg2,
18978cfb07fSJuergen Lock                                                       arg3,
19078cfb07fSJuergen Lock                                                       arg4,
19178cfb07fSJuergen Lock                                                       arg5,
19278cfb07fSJuergen Lock                                                       arg6,
19378cfb07fSJuergen Lock                                                       arg7,
19478cfb07fSJuergen Lock                                                       arg8);
19578cfb07fSJuergen Lock             } else { //if (bsd_type == target_openbsd)
19631fc12dfSblueswir1                 env->regs[R_EAX] = do_openbsd_syscall(env,
19731fc12dfSblueswir1                                                       env->regs[R_EAX],
19831fc12dfSblueswir1                                                       env->regs[R_EBX],
19931fc12dfSblueswir1                                                       env->regs[R_ECX],
20031fc12dfSblueswir1                                                       env->regs[R_EDX],
20131fc12dfSblueswir1                                                       env->regs[R_ESI],
20231fc12dfSblueswir1                                                       env->regs[R_EDI],
20331fc12dfSblueswir1                                                       env->regs[R_EBP]);
20478cfb07fSJuergen Lock             }
20578cfb07fSJuergen Lock             if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
20678cfb07fSJuergen Lock                 env->regs[R_EAX] = -env->regs[R_EAX];
20778cfb07fSJuergen Lock                 env->eflags |= CC_C;
20878cfb07fSJuergen Lock             } else {
20978cfb07fSJuergen Lock                 env->eflags &= ~CC_C;
21078cfb07fSJuergen Lock             }
21131fc12dfSblueswir1             break;
21231fc12dfSblueswir1 #ifndef TARGET_ABI32
21331fc12dfSblueswir1         case EXCP_SYSCALL:
2145ba18547SStefan Weil             /* syscall from syscall instruction */
21578cfb07fSJuergen Lock             if (bsd_type == target_freebsd)
21678cfb07fSJuergen Lock                 env->regs[R_EAX] = do_freebsd_syscall(env,
21778cfb07fSJuergen Lock                                                       env->regs[R_EAX],
21878cfb07fSJuergen Lock                                                       env->regs[R_EDI],
21978cfb07fSJuergen Lock                                                       env->regs[R_ESI],
22078cfb07fSJuergen Lock                                                       env->regs[R_EDX],
22178cfb07fSJuergen Lock                                                       env->regs[R_ECX],
22278cfb07fSJuergen Lock                                                       env->regs[8],
22378cfb07fSJuergen Lock                                                       env->regs[9], 0, 0);
22478cfb07fSJuergen Lock             else { //if (bsd_type == target_openbsd)
22531fc12dfSblueswir1                 env->regs[R_EAX] = do_openbsd_syscall(env,
22631fc12dfSblueswir1                                                       env->regs[R_EAX],
22731fc12dfSblueswir1                                                       env->regs[R_EDI],
22831fc12dfSblueswir1                                                       env->regs[R_ESI],
22931fc12dfSblueswir1                                                       env->regs[R_EDX],
23031fc12dfSblueswir1                                                       env->regs[10],
23131fc12dfSblueswir1                                                       env->regs[8],
23231fc12dfSblueswir1                                                       env->regs[9]);
23378cfb07fSJuergen Lock             }
23431fc12dfSblueswir1             env->eip = env->exception_next_eip;
23578cfb07fSJuergen Lock             if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
23678cfb07fSJuergen Lock                 env->regs[R_EAX] = -env->regs[R_EAX];
23778cfb07fSJuergen Lock                 env->eflags |= CC_C;
23878cfb07fSJuergen Lock             } else {
23978cfb07fSJuergen Lock                 env->eflags &= ~CC_C;
24078cfb07fSJuergen Lock             }
24131fc12dfSblueswir1             break;
24231fc12dfSblueswir1 #endif
24331fc12dfSblueswir1 #if 0
24431fc12dfSblueswir1         case EXCP0B_NOSEG:
24531fc12dfSblueswir1         case EXCP0C_STACK:
24631fc12dfSblueswir1             info.si_signo = SIGBUS;
24731fc12dfSblueswir1             info.si_errno = 0;
24831fc12dfSblueswir1             info.si_code = TARGET_SI_KERNEL;
24931fc12dfSblueswir1             info._sifields._sigfault._addr = 0;
25031fc12dfSblueswir1             queue_signal(env, info.si_signo, &info);
25131fc12dfSblueswir1             break;
25231fc12dfSblueswir1         case EXCP0D_GPF:
25331fc12dfSblueswir1             /* XXX: potential problem if ABI32 */
25431fc12dfSblueswir1 #ifndef TARGET_X86_64
25531fc12dfSblueswir1             if (env->eflags & VM_MASK) {
25631fc12dfSblueswir1                 handle_vm86_fault(env);
25731fc12dfSblueswir1             } else
25831fc12dfSblueswir1 #endif
25931fc12dfSblueswir1             {
26031fc12dfSblueswir1                 info.si_signo = SIGSEGV;
26131fc12dfSblueswir1                 info.si_errno = 0;
26231fc12dfSblueswir1                 info.si_code = TARGET_SI_KERNEL;
26331fc12dfSblueswir1                 info._sifields._sigfault._addr = 0;
26431fc12dfSblueswir1                 queue_signal(env, info.si_signo, &info);
26531fc12dfSblueswir1             }
26631fc12dfSblueswir1             break;
26731fc12dfSblueswir1         case EXCP0E_PAGE:
26831fc12dfSblueswir1             info.si_signo = SIGSEGV;
26931fc12dfSblueswir1             info.si_errno = 0;
27031fc12dfSblueswir1             if (!(env->error_code & 1))
27131fc12dfSblueswir1                 info.si_code = TARGET_SEGV_MAPERR;
27231fc12dfSblueswir1             else
27331fc12dfSblueswir1                 info.si_code = TARGET_SEGV_ACCERR;
27431fc12dfSblueswir1             info._sifields._sigfault._addr = env->cr[2];
27531fc12dfSblueswir1             queue_signal(env, info.si_signo, &info);
27631fc12dfSblueswir1             break;
27731fc12dfSblueswir1         case EXCP00_DIVZ:
27831fc12dfSblueswir1 #ifndef TARGET_X86_64
27931fc12dfSblueswir1             if (env->eflags & VM_MASK) {
28031fc12dfSblueswir1                 handle_vm86_trap(env, trapnr);
28131fc12dfSblueswir1             } else
28231fc12dfSblueswir1 #endif
28331fc12dfSblueswir1             {
28431fc12dfSblueswir1                 /* division by zero */
28531fc12dfSblueswir1                 info.si_signo = SIGFPE;
28631fc12dfSblueswir1                 info.si_errno = 0;
28731fc12dfSblueswir1                 info.si_code = TARGET_FPE_INTDIV;
28831fc12dfSblueswir1                 info._sifields._sigfault._addr = env->eip;
28931fc12dfSblueswir1                 queue_signal(env, info.si_signo, &info);
29031fc12dfSblueswir1             }
29131fc12dfSblueswir1             break;
29231fc12dfSblueswir1         case EXCP01_DB:
29331fc12dfSblueswir1         case EXCP03_INT3:
29431fc12dfSblueswir1 #ifndef TARGET_X86_64
29531fc12dfSblueswir1             if (env->eflags & VM_MASK) {
29631fc12dfSblueswir1                 handle_vm86_trap(env, trapnr);
29731fc12dfSblueswir1             } else
29831fc12dfSblueswir1 #endif
29931fc12dfSblueswir1             {
30031fc12dfSblueswir1                 info.si_signo = SIGTRAP;
30131fc12dfSblueswir1                 info.si_errno = 0;
30231fc12dfSblueswir1                 if (trapnr == EXCP01_DB) {
30331fc12dfSblueswir1                     info.si_code = TARGET_TRAP_BRKPT;
30431fc12dfSblueswir1                     info._sifields._sigfault._addr = env->eip;
30531fc12dfSblueswir1                 } else {
30631fc12dfSblueswir1                     info.si_code = TARGET_SI_KERNEL;
30731fc12dfSblueswir1                     info._sifields._sigfault._addr = 0;
30831fc12dfSblueswir1                 }
30931fc12dfSblueswir1                 queue_signal(env, info.si_signo, &info);
31031fc12dfSblueswir1             }
31131fc12dfSblueswir1             break;
31231fc12dfSblueswir1         case EXCP04_INTO:
31331fc12dfSblueswir1         case EXCP05_BOUND:
31431fc12dfSblueswir1 #ifndef TARGET_X86_64
31531fc12dfSblueswir1             if (env->eflags & VM_MASK) {
31631fc12dfSblueswir1                 handle_vm86_trap(env, trapnr);
31731fc12dfSblueswir1             } else
31831fc12dfSblueswir1 #endif
31931fc12dfSblueswir1             {
32031fc12dfSblueswir1                 info.si_signo = SIGSEGV;
32131fc12dfSblueswir1                 info.si_errno = 0;
32231fc12dfSblueswir1                 info.si_code = TARGET_SI_KERNEL;
32331fc12dfSblueswir1                 info._sifields._sigfault._addr = 0;
32431fc12dfSblueswir1                 queue_signal(env, info.si_signo, &info);
32531fc12dfSblueswir1             }
32631fc12dfSblueswir1             break;
32731fc12dfSblueswir1         case EXCP06_ILLOP:
32831fc12dfSblueswir1             info.si_signo = SIGILL;
32931fc12dfSblueswir1             info.si_errno = 0;
33031fc12dfSblueswir1             info.si_code = TARGET_ILL_ILLOPN;
33131fc12dfSblueswir1             info._sifields._sigfault._addr = env->eip;
33231fc12dfSblueswir1             queue_signal(env, info.si_signo, &info);
33331fc12dfSblueswir1             break;
33431fc12dfSblueswir1 #endif
33531fc12dfSblueswir1         case EXCP_INTERRUPT:
33631fc12dfSblueswir1             /* just indicate that signals should be handled asap */
33731fc12dfSblueswir1             break;
33831fc12dfSblueswir1 #if 0
33931fc12dfSblueswir1         case EXCP_DEBUG:
34031fc12dfSblueswir1             {
34131fc12dfSblueswir1                 int sig;
34231fc12dfSblueswir1 
34331fc12dfSblueswir1                 sig = gdb_handlesig (env, TARGET_SIGTRAP);
34431fc12dfSblueswir1                 if (sig)
34531fc12dfSblueswir1                   {
34631fc12dfSblueswir1                     info.si_signo = sig;
34731fc12dfSblueswir1                     info.si_errno = 0;
34831fc12dfSblueswir1                     info.si_code = TARGET_TRAP_BRKPT;
34931fc12dfSblueswir1                     queue_signal(env, info.si_signo, &info);
35031fc12dfSblueswir1                   }
35131fc12dfSblueswir1             }
35231fc12dfSblueswir1             break;
35331fc12dfSblueswir1 #endif
35431fc12dfSblueswir1         default:
35531fc12dfSblueswir1             pc = env->segs[R_CS].base + env->eip;
35631fc12dfSblueswir1             fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
35731fc12dfSblueswir1                     (long)pc, trapnr);
35831fc12dfSblueswir1             abort();
35931fc12dfSblueswir1         }
36031fc12dfSblueswir1         process_pending_signals(env);
36131fc12dfSblueswir1     }
36231fc12dfSblueswir1 }
36331fc12dfSblueswir1 #endif
36431fc12dfSblueswir1 
36584778508Sblueswir1 #ifdef TARGET_SPARC
36684778508Sblueswir1 #define SPARC64_STACK_BIAS 2047
36784778508Sblueswir1 
36884778508Sblueswir1 //#define DEBUG_WIN
36984778508Sblueswir1 /* WARNING: dealing with register windows _is_ complicated. More info
37084778508Sblueswir1    can be found at http://www.sics.se/~psm/sparcstack.html */
37184778508Sblueswir1 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
37284778508Sblueswir1 {
37384778508Sblueswir1     index = (index + cwp * 16) % (16 * env->nwindows);
37484778508Sblueswir1     /* wrap handling : if cwp is on the last window, then we use the
37584778508Sblueswir1        registers 'after' the end */
37684778508Sblueswir1     if (index < 8 && env->cwp == env->nwindows - 1)
37784778508Sblueswir1         index += 16 * env->nwindows;
37884778508Sblueswir1     return index;
37984778508Sblueswir1 }
38084778508Sblueswir1 
38184778508Sblueswir1 /* save the register window 'cwp1' */
38284778508Sblueswir1 static inline void save_window_offset(CPUSPARCState *env, int cwp1)
38384778508Sblueswir1 {
38484778508Sblueswir1     unsigned int i;
38584778508Sblueswir1     abi_ulong sp_ptr;
38684778508Sblueswir1 
38784778508Sblueswir1     sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
38884778508Sblueswir1 #ifdef TARGET_SPARC64
38984778508Sblueswir1     if (sp_ptr & 3)
39084778508Sblueswir1         sp_ptr += SPARC64_STACK_BIAS;
39184778508Sblueswir1 #endif
39284778508Sblueswir1 #if defined(DEBUG_WIN)
39384778508Sblueswir1     printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
39484778508Sblueswir1            sp_ptr, cwp1);
39584778508Sblueswir1 #endif
39684778508Sblueswir1     for(i = 0; i < 16; i++) {
39784778508Sblueswir1         /* FIXME - what to do if put_user() fails? */
39884778508Sblueswir1         put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
39984778508Sblueswir1         sp_ptr += sizeof(abi_ulong);
40084778508Sblueswir1     }
40184778508Sblueswir1 }
40284778508Sblueswir1 
40384778508Sblueswir1 static void save_window(CPUSPARCState *env)
40484778508Sblueswir1 {
40584778508Sblueswir1 #ifndef TARGET_SPARC64
40684778508Sblueswir1     unsigned int new_wim;
40784778508Sblueswir1     new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
40884778508Sblueswir1         ((1LL << env->nwindows) - 1);
40984778508Sblueswir1     save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
41084778508Sblueswir1     env->wim = new_wim;
41184778508Sblueswir1 #else
41284778508Sblueswir1     save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
41384778508Sblueswir1     env->cansave++;
41484778508Sblueswir1     env->canrestore--;
41584778508Sblueswir1 #endif
41684778508Sblueswir1 }
41784778508Sblueswir1 
41884778508Sblueswir1 static void restore_window(CPUSPARCState *env)
41984778508Sblueswir1 {
42084778508Sblueswir1 #ifndef TARGET_SPARC64
42184778508Sblueswir1     unsigned int new_wim;
42284778508Sblueswir1 #endif
42384778508Sblueswir1     unsigned int i, cwp1;
42484778508Sblueswir1     abi_ulong sp_ptr;
42584778508Sblueswir1 
42684778508Sblueswir1 #ifndef TARGET_SPARC64
42784778508Sblueswir1     new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
42884778508Sblueswir1         ((1LL << env->nwindows) - 1);
42984778508Sblueswir1 #endif
43084778508Sblueswir1 
43184778508Sblueswir1     /* restore the invalid window */
43284778508Sblueswir1     cwp1 = cpu_cwp_inc(env, env->cwp + 1);
43384778508Sblueswir1     sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
43484778508Sblueswir1 #ifdef TARGET_SPARC64
43584778508Sblueswir1     if (sp_ptr & 3)
43684778508Sblueswir1         sp_ptr += SPARC64_STACK_BIAS;
43784778508Sblueswir1 #endif
43884778508Sblueswir1 #if defined(DEBUG_WIN)
43984778508Sblueswir1     printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
44084778508Sblueswir1            sp_ptr, cwp1);
44184778508Sblueswir1 #endif
44284778508Sblueswir1     for(i = 0; i < 16; i++) {
44384778508Sblueswir1         /* FIXME - what to do if get_user() fails? */
44484778508Sblueswir1         get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
44584778508Sblueswir1         sp_ptr += sizeof(abi_ulong);
44684778508Sblueswir1     }
44784778508Sblueswir1 #ifdef TARGET_SPARC64
44884778508Sblueswir1     env->canrestore++;
44984778508Sblueswir1     if (env->cleanwin < env->nwindows - 1)
45084778508Sblueswir1         env->cleanwin++;
45184778508Sblueswir1     env->cansave--;
45284778508Sblueswir1 #else
45384778508Sblueswir1     env->wim = new_wim;
45484778508Sblueswir1 #endif
45584778508Sblueswir1 }
45684778508Sblueswir1 
45784778508Sblueswir1 static void flush_windows(CPUSPARCState *env)
45884778508Sblueswir1 {
45984778508Sblueswir1     int offset, cwp1;
46084778508Sblueswir1 
46184778508Sblueswir1     offset = 1;
46284778508Sblueswir1     for(;;) {
46384778508Sblueswir1         /* if restore would invoke restore_window(), then we can stop */
46484778508Sblueswir1         cwp1 = cpu_cwp_inc(env, env->cwp + offset);
46584778508Sblueswir1 #ifndef TARGET_SPARC64
46684778508Sblueswir1         if (env->wim & (1 << cwp1))
46784778508Sblueswir1             break;
46884778508Sblueswir1 #else
46984778508Sblueswir1         if (env->canrestore == 0)
47084778508Sblueswir1             break;
47184778508Sblueswir1         env->cansave++;
47284778508Sblueswir1         env->canrestore--;
47384778508Sblueswir1 #endif
47484778508Sblueswir1         save_window_offset(env, cwp1);
47584778508Sblueswir1         offset++;
47684778508Sblueswir1     }
47784778508Sblueswir1     cwp1 = cpu_cwp_inc(env, env->cwp + 1);
47884778508Sblueswir1 #ifndef TARGET_SPARC64
47984778508Sblueswir1     /* set wim so that restore will reload the registers */
48084778508Sblueswir1     env->wim = 1 << cwp1;
48184778508Sblueswir1 #endif
48284778508Sblueswir1 #if defined(DEBUG_WIN)
48384778508Sblueswir1     printf("flush_windows: nb=%d\n", offset - 1);
48484778508Sblueswir1 #endif
48584778508Sblueswir1 }
48684778508Sblueswir1 
48778cfb07fSJuergen Lock void cpu_loop(CPUSPARCState *env)
48884778508Sblueswir1 {
489878096eeSAndreas Färber     CPUState *cs = CPU(sparc_env_get_cpu(env));
49084778508Sblueswir1     int trapnr, ret, syscall_nr;
49184778508Sblueswir1     //target_siginfo_t info;
49284778508Sblueswir1 
49384778508Sblueswir1     while (1) {
494d148d90eSSergey Fedorov         cpu_exec_start(cs);
495ded554cdSSean Bruno         trapnr = cpu_exec(cs);
496d148d90eSSergey Fedorov         cpu_exec_end(cs);
497d148d90eSSergey Fedorov         process_queued_cpu_work(cs);
49884778508Sblueswir1 
49984778508Sblueswir1         switch (trapnr) {
50077b9435fSblueswir1 #ifndef TARGET_SPARC64
50177b9435fSblueswir1         case 0x80:
50277b9435fSblueswir1 #else
50378cfb07fSJuergen Lock         /* FreeBSD uses 0x141 for syscalls too */
50478cfb07fSJuergen Lock         case 0x141:
50578cfb07fSJuergen Lock             if (bsd_type != target_freebsd)
50678cfb07fSJuergen Lock                 goto badtrap;
50784778508Sblueswir1         case 0x100:
50877b9435fSblueswir1 #endif
50984778508Sblueswir1             syscall_nr = env->gregs[1];
51084778508Sblueswir1             if (bsd_type == target_freebsd)
51184778508Sblueswir1                 ret = do_freebsd_syscall(env, syscall_nr,
51284778508Sblueswir1                                          env->regwptr[0], env->regwptr[1],
51384778508Sblueswir1                                          env->regwptr[2], env->regwptr[3],
51478cfb07fSJuergen Lock                                          env->regwptr[4], env->regwptr[5], 0, 0);
51584778508Sblueswir1             else if (bsd_type == target_netbsd)
51684778508Sblueswir1                 ret = do_netbsd_syscall(env, syscall_nr,
51784778508Sblueswir1                                         env->regwptr[0], env->regwptr[1],
51884778508Sblueswir1                                         env->regwptr[2], env->regwptr[3],
51984778508Sblueswir1                                         env->regwptr[4], env->regwptr[5]);
520cdba95bdSblueswir1             else { //if (bsd_type == target_openbsd)
521cdba95bdSblueswir1 #if defined(TARGET_SPARC64)
522cdba95bdSblueswir1                 syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG |
523cdba95bdSblueswir1                                 TARGET_OPENBSD_SYSCALL_G2RFLAG);
524cdba95bdSblueswir1 #endif
52584778508Sblueswir1                 ret = do_openbsd_syscall(env, syscall_nr,
52684778508Sblueswir1                                          env->regwptr[0], env->regwptr[1],
52784778508Sblueswir1                                          env->regwptr[2], env->regwptr[3],
52884778508Sblueswir1                                          env->regwptr[4], env->regwptr[5]);
529cdba95bdSblueswir1             }
53084778508Sblueswir1             if ((unsigned int)ret >= (unsigned int)(-515)) {
53178cfb07fSJuergen Lock                 ret = -ret;
53284778508Sblueswir1 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
53384778508Sblueswir1                 env->xcc |= PSR_CARRY;
53484778508Sblueswir1 #else
53584778508Sblueswir1                 env->psr |= PSR_CARRY;
53684778508Sblueswir1 #endif
53784778508Sblueswir1             } else {
53884778508Sblueswir1 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
53984778508Sblueswir1                 env->xcc &= ~PSR_CARRY;
54084778508Sblueswir1 #else
54184778508Sblueswir1                 env->psr &= ~PSR_CARRY;
54284778508Sblueswir1 #endif
54384778508Sblueswir1             }
54484778508Sblueswir1             env->regwptr[0] = ret;
54584778508Sblueswir1             /* next instruction */
546cdba95bdSblueswir1 #if defined(TARGET_SPARC64)
547cdba95bdSblueswir1             if (bsd_type == target_openbsd &&
548cdba95bdSblueswir1                 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) {
54984778508Sblueswir1                 env->pc = env->gregs[2];
55084778508Sblueswir1                 env->npc = env->pc + 4;
551cdba95bdSblueswir1             } else if (bsd_type == target_openbsd &&
552cdba95bdSblueswir1                        env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) {
55384778508Sblueswir1                 env->pc = env->gregs[7];
55484778508Sblueswir1                 env->npc = env->pc + 4;
55584778508Sblueswir1             } else {
55684778508Sblueswir1                 env->pc = env->npc;
55784778508Sblueswir1                 env->npc = env->npc + 4;
55884778508Sblueswir1             }
55984778508Sblueswir1 #else
56084778508Sblueswir1             env->pc = env->npc;
56184778508Sblueswir1             env->npc = env->npc + 4;
56284778508Sblueswir1 #endif
56384778508Sblueswir1             break;
56484778508Sblueswir1         case 0x83: /* flush windows */
56584778508Sblueswir1 #ifdef TARGET_ABI32
56684778508Sblueswir1         case 0x103:
56784778508Sblueswir1 #endif
56884778508Sblueswir1             flush_windows(env);
56984778508Sblueswir1             /* next instruction */
57084778508Sblueswir1             env->pc = env->npc;
57184778508Sblueswir1             env->npc = env->npc + 4;
57284778508Sblueswir1             break;
57384778508Sblueswir1 #ifndef TARGET_SPARC64
57484778508Sblueswir1         case TT_WIN_OVF: /* window overflow */
57584778508Sblueswir1             save_window(env);
57684778508Sblueswir1             break;
57784778508Sblueswir1         case TT_WIN_UNF: /* window underflow */
57884778508Sblueswir1             restore_window(env);
57984778508Sblueswir1             break;
58084778508Sblueswir1         case TT_TFAULT:
58184778508Sblueswir1         case TT_DFAULT:
58284778508Sblueswir1 #if 0
58384778508Sblueswir1             {
58484778508Sblueswir1                 info.si_signo = SIGSEGV;
58584778508Sblueswir1                 info.si_errno = 0;
58684778508Sblueswir1                 /* XXX: check env->error_code */
58784778508Sblueswir1                 info.si_code = TARGET_SEGV_MAPERR;
58884778508Sblueswir1                 info._sifields._sigfault._addr = env->mmuregs[4];
58984778508Sblueswir1                 queue_signal(env, info.si_signo, &info);
59084778508Sblueswir1             }
59184778508Sblueswir1 #endif
59284778508Sblueswir1             break;
59384778508Sblueswir1 #else
59484778508Sblueswir1         case TT_SPILL: /* window overflow */
59584778508Sblueswir1             save_window(env);
59684778508Sblueswir1             break;
59784778508Sblueswir1         case TT_FILL: /* window underflow */
59884778508Sblueswir1             restore_window(env);
59984778508Sblueswir1             break;
60084778508Sblueswir1         case TT_TFAULT:
60184778508Sblueswir1         case TT_DFAULT:
60284778508Sblueswir1 #if 0
60384778508Sblueswir1             {
60484778508Sblueswir1                 info.si_signo = SIGSEGV;
60584778508Sblueswir1                 info.si_errno = 0;
60684778508Sblueswir1                 /* XXX: check env->error_code */
60784778508Sblueswir1                 info.si_code = TARGET_SEGV_MAPERR;
60884778508Sblueswir1                 if (trapnr == TT_DFAULT)
60984778508Sblueswir1                     info._sifields._sigfault._addr = env->dmmuregs[4];
61084778508Sblueswir1                 else
61184778508Sblueswir1                     info._sifields._sigfault._addr = env->tsptr->tpc;
61284778508Sblueswir1                 //queue_signal(env, info.si_signo, &info);
61384778508Sblueswir1             }
61484778508Sblueswir1 #endif
61584778508Sblueswir1             break;
61684778508Sblueswir1 #endif
61784778508Sblueswir1         case EXCP_INTERRUPT:
61884778508Sblueswir1             /* just indicate that signals should be handled asap */
61984778508Sblueswir1             break;
62084778508Sblueswir1         case EXCP_DEBUG:
62184778508Sblueswir1             {
622e4335180SPeter Maydell #if 0
623e4335180SPeter Maydell                 int sig =
624e4335180SPeter Maydell #endif
625e4335180SPeter Maydell                 gdb_handlesig(cs, TARGET_SIGTRAP);
62684778508Sblueswir1 #if 0
62784778508Sblueswir1                 if (sig)
62884778508Sblueswir1                   {
62984778508Sblueswir1                     info.si_signo = sig;
63084778508Sblueswir1                     info.si_errno = 0;
63184778508Sblueswir1                     info.si_code = TARGET_TRAP_BRKPT;
63284778508Sblueswir1                     //queue_signal(env, info.si_signo, &info);
63384778508Sblueswir1                   }
63484778508Sblueswir1 #endif
63584778508Sblueswir1             }
63684778508Sblueswir1             break;
63784778508Sblueswir1         default:
63878cfb07fSJuergen Lock #ifdef TARGET_SPARC64
63978cfb07fSJuergen Lock         badtrap:
64078cfb07fSJuergen Lock #endif
64184778508Sblueswir1             printf ("Unhandled trap: 0x%x\n", trapnr);
642878096eeSAndreas Färber             cpu_dump_state(cs, stderr, fprintf, 0);
64384778508Sblueswir1             exit (1);
64484778508Sblueswir1         }
64584778508Sblueswir1         process_pending_signals (env);
64684778508Sblueswir1     }
64784778508Sblueswir1 }
64884778508Sblueswir1 
64984778508Sblueswir1 #endif
65084778508Sblueswir1 
65184778508Sblueswir1 static void usage(void)
65284778508Sblueswir1 {
653d915b7bbSPeter Maydell     printf("qemu-" TARGET_NAME " version " QEMU_VERSION QEMU_PKGVERSION
6540781dd6eSThomas Huth            "\n" QEMU_COPYRIGHT "\n"
6552e59915dSPaolo Bonzini            "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
65684778508Sblueswir1            "BSD CPU emulator (compiled for %s emulation)\n"
65784778508Sblueswir1            "\n"
65884778508Sblueswir1            "Standard options:\n"
65984778508Sblueswir1            "-h                print this help\n"
66084778508Sblueswir1            "-g port           wait gdb connection to port\n"
66184778508Sblueswir1            "-L path           set the elf interpreter prefix (default=%s)\n"
66284778508Sblueswir1            "-s size           set the stack size in bytes (default=%ld)\n"
663c8057f95SPeter Maydell            "-cpu model        select CPU (-cpu help for list)\n"
66484778508Sblueswir1            "-drop-ld-preload  drop LD_PRELOAD for target process\n"
665fc0d96b4SBlue Swirl            "-E var=value      sets/modifies targets environment variable(s)\n"
666fc0d96b4SBlue Swirl            "-U var            unsets targets environment variable(s)\n"
6672fa5d9baSBlue Swirl            "-B address        set guest_base address to address\n"
66884778508Sblueswir1            "-bsd type         select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
66984778508Sblueswir1            "\n"
67084778508Sblueswir1            "Debug options:\n"
671989b697dSPeter Maydell            "-d item1[,...]    enable logging of specified items\n"
672989b697dSPeter Maydell            "                  (use '-d help' for a list of log items)\n"
673989b697dSPeter Maydell            "-D logfile        write logs to 'logfile' (default stderr)\n"
67484778508Sblueswir1            "-p pagesize       set the host page size to 'pagesize'\n"
6751b530a6dSaurel32            "-singlestep       always run in singlestep mode\n"
67684778508Sblueswir1            "-strace           log system calls\n"
6776913e79cSLluís Vilanova            "-trace            [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
6786913e79cSLluís Vilanova            "                  specify tracing options\n"
67984778508Sblueswir1            "\n"
68084778508Sblueswir1            "Environment variables:\n"
68184778508Sblueswir1            "QEMU_STRACE       Print system calls and arguments similar to the\n"
68284778508Sblueswir1            "                  'strace' program.  Enable by setting to any value.\n"
683fc0d96b4SBlue Swirl            "You can use -E and -U options to set/unset environment variables\n"
684fc0d96b4SBlue Swirl            "for target process.  It is possible to provide several variables\n"
685fc0d96b4SBlue Swirl            "by repeating the option.  For example:\n"
686fc0d96b4SBlue Swirl            "    -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
687fc0d96b4SBlue Swirl            "Note that if you provide several changes to single variable\n"
688fc0d96b4SBlue Swirl            "last change will stay in effect.\n"
689f5048cb7SEric Blake            "\n"
690f5048cb7SEric Blake            QEMU_HELP_BOTTOM "\n"
69184778508Sblueswir1            ,
6922e59915dSPaolo Bonzini            TARGET_NAME,
69384778508Sblueswir1            interp_prefix,
694989b697dSPeter Maydell            x86_stack_size);
6952d18e637Sblueswir1     exit(1);
69684778508Sblueswir1 }
69784778508Sblueswir1 
698dca1173cSAndreas Färber THREAD CPUState *thread_cpu;
69984778508Sblueswir1 
70048f59211SEd Maste bool qemu_cpu_is_self(CPUState *cpu)
70148f59211SEd Maste {
70248f59211SEd Maste     return thread_cpu == cpu;
70348f59211SEd Maste }
70448f59211SEd Maste 
70548f59211SEd Maste void qemu_cpu_kick(CPUState *cpu)
70648f59211SEd Maste {
70748f59211SEd Maste     cpu_exit(cpu);
70848f59211SEd Maste }
70948f59211SEd Maste 
71084778508Sblueswir1 /* Assumes contents are already zeroed.  */
71184778508Sblueswir1 void init_task_state(TaskState *ts)
71284778508Sblueswir1 {
71384778508Sblueswir1     int i;
71484778508Sblueswir1 
71584778508Sblueswir1     ts->used = 1;
71684778508Sblueswir1     ts->first_free = ts->sigqueue_table;
71784778508Sblueswir1     for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
71884778508Sblueswir1         ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
71984778508Sblueswir1     }
72084778508Sblueswir1     ts->sigqueue_table[i].next = NULL;
72184778508Sblueswir1 }
72284778508Sblueswir1 
72384778508Sblueswir1 int main(int argc, char **argv)
72484778508Sblueswir1 {
72584778508Sblueswir1     const char *filename;
72684778508Sblueswir1     const char *cpu_model;
727989b697dSPeter Maydell     const char *log_file = NULL;
728c235d738SMatthew Fernandez     const char *log_mask = NULL;
72984778508Sblueswir1     struct target_pt_regs regs1, *regs = &regs1;
73084778508Sblueswir1     struct image_info info1, *info = &info1;
73184778508Sblueswir1     TaskState ts1, *ts = &ts1;
7329349b4f9SAndreas Färber     CPUArchState *env;
733db6b81d4SAndreas Färber     CPUState *cpu;
73484778508Sblueswir1     int optind;
73584778508Sblueswir1     const char *r;
73684778508Sblueswir1     int gdbstub_port = 0;
737fc0d96b4SBlue Swirl     char **target_environ, **wrk;
738fc0d96b4SBlue Swirl     envlist_t *envlist = NULL;
7396913e79cSLluís Vilanova     char *trace_file = NULL;
74078cfb07fSJuergen Lock     bsd_type = target_openbsd;
74184778508Sblueswir1 
74284778508Sblueswir1     if (argc <= 1)
74384778508Sblueswir1         usage();
74484778508Sblueswir1 
745fe4db84dSDaniel P. Berrange     module_call_init(MODULE_INIT_TRACE);
746267f685bSPaolo Bonzini     qemu_init_cpu_list();
747ce008c1fSAndreas Färber     module_call_init(MODULE_INIT_QOM);
748ce008c1fSAndreas Färber 
749ec45bbe5SSaurav Sachidanand     envlist = envlist_create();
750fc0d96b4SBlue Swirl 
751fc0d96b4SBlue Swirl     /* add current environment into the list */
752fc0d96b4SBlue Swirl     for (wrk = environ; *wrk != NULL; wrk++) {
753fc0d96b4SBlue Swirl         (void) envlist_setenv(envlist, *wrk);
754fc0d96b4SBlue Swirl     }
755fc0d96b4SBlue Swirl 
75684778508Sblueswir1     cpu_model = NULL;
7570c62de2fSJuergen Lock 
7586913e79cSLluís Vilanova     qemu_add_opts(&qemu_trace_opts);
7596913e79cSLluís Vilanova 
76084778508Sblueswir1     optind = 1;
76184778508Sblueswir1     for (;;) {
76284778508Sblueswir1         if (optind >= argc)
76384778508Sblueswir1             break;
76484778508Sblueswir1         r = argv[optind];
76584778508Sblueswir1         if (r[0] != '-')
76684778508Sblueswir1             break;
76784778508Sblueswir1         optind++;
76884778508Sblueswir1         r++;
76984778508Sblueswir1         if (!strcmp(r, "-")) {
77084778508Sblueswir1             break;
77184778508Sblueswir1         } else if (!strcmp(r, "d")) {
772c235d738SMatthew Fernandez             if (optind >= argc) {
77384778508Sblueswir1                 break;
77484778508Sblueswir1             }
775c235d738SMatthew Fernandez             log_mask = argv[optind++];
776c235d738SMatthew Fernandez         } else if (!strcmp(r, "D")) {
777c235d738SMatthew Fernandez             if (optind >= argc) {
778c235d738SMatthew Fernandez                 break;
77984778508Sblueswir1             }
780c235d738SMatthew Fernandez             log_file = argv[optind++];
781fc0d96b4SBlue Swirl         } else if (!strcmp(r, "E")) {
782fc0d96b4SBlue Swirl             r = argv[optind++];
783fc0d96b4SBlue Swirl             if (envlist_setenv(envlist, r) != 0)
784fc0d96b4SBlue Swirl                 usage();
785f66724c9SStefan Weil         } else if (!strcmp(r, "ignore-environment")) {
786f66724c9SStefan Weil             envlist_free(envlist);
787ec45bbe5SSaurav Sachidanand             envlist = envlist_create();
788fc0d96b4SBlue Swirl         } else if (!strcmp(r, "U")) {
789fc0d96b4SBlue Swirl             r = argv[optind++];
790fc0d96b4SBlue Swirl             if (envlist_unsetenv(envlist, r) != 0)
791fc0d96b4SBlue Swirl                 usage();
79284778508Sblueswir1         } else if (!strcmp(r, "s")) {
79384778508Sblueswir1             r = argv[optind++];
79484778508Sblueswir1             x86_stack_size = strtol(r, (char **)&r, 0);
79584778508Sblueswir1             if (x86_stack_size <= 0)
79684778508Sblueswir1                 usage();
79784778508Sblueswir1             if (*r == 'M')
79884778508Sblueswir1                 x86_stack_size *= 1024 * 1024;
79984778508Sblueswir1             else if (*r == 'k' || *r == 'K')
80084778508Sblueswir1                 x86_stack_size *= 1024;
80184778508Sblueswir1         } else if (!strcmp(r, "L")) {
80284778508Sblueswir1             interp_prefix = argv[optind++];
80384778508Sblueswir1         } else if (!strcmp(r, "p")) {
80484778508Sblueswir1             qemu_host_page_size = atoi(argv[optind++]);
80584778508Sblueswir1             if (qemu_host_page_size == 0 ||
80684778508Sblueswir1                 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
80784778508Sblueswir1                 fprintf(stderr, "page size must be a power of two\n");
80884778508Sblueswir1                 exit(1);
80984778508Sblueswir1             }
81084778508Sblueswir1         } else if (!strcmp(r, "g")) {
81184778508Sblueswir1             gdbstub_port = atoi(argv[optind++]);
81284778508Sblueswir1         } else if (!strcmp(r, "r")) {
81384778508Sblueswir1             qemu_uname_release = argv[optind++];
81484778508Sblueswir1         } else if (!strcmp(r, "cpu")) {
81584778508Sblueswir1             cpu_model = argv[optind++];
816c8057f95SPeter Maydell             if (is_help_option(cpu_model)) {
81784778508Sblueswir1 /* XXX: implement xxx_cpu_list for targets that still miss it */
81884778508Sblueswir1 #if defined(cpu_list)
81984778508Sblueswir1                     cpu_list(stdout, &fprintf);
82084778508Sblueswir1 #endif
8212d18e637Sblueswir1                 exit(1);
82284778508Sblueswir1             }
8232fa5d9baSBlue Swirl         } else if (!strcmp(r, "B")) {
8242fa5d9baSBlue Swirl            guest_base = strtol(argv[optind++], NULL, 0);
8252fa5d9baSBlue Swirl            have_guest_base = 1;
82684778508Sblueswir1         } else if (!strcmp(r, "drop-ld-preload")) {
827fc0d96b4SBlue Swirl             (void) envlist_unsetenv(envlist, "LD_PRELOAD");
82884778508Sblueswir1         } else if (!strcmp(r, "bsd")) {
82984778508Sblueswir1             if (!strcasecmp(argv[optind], "freebsd")) {
83084778508Sblueswir1                 bsd_type = target_freebsd;
83184778508Sblueswir1             } else if (!strcasecmp(argv[optind], "netbsd")) {
83284778508Sblueswir1                 bsd_type = target_netbsd;
83384778508Sblueswir1             } else if (!strcasecmp(argv[optind], "openbsd")) {
83484778508Sblueswir1                 bsd_type = target_openbsd;
83584778508Sblueswir1             } else {
83684778508Sblueswir1                 usage();
83784778508Sblueswir1             }
83884778508Sblueswir1             optind++;
8391b530a6dSaurel32         } else if (!strcmp(r, "singlestep")) {
8401b530a6dSaurel32             singlestep = 1;
84184778508Sblueswir1         } else if (!strcmp(r, "strace")) {
84284778508Sblueswir1             do_strace = 1;
8436913e79cSLluís Vilanova         } else if (!strcmp(r, "trace")) {
8446913e79cSLluís Vilanova             g_free(trace_file);
8456913e79cSLluís Vilanova             trace_file = trace_opt_parse(optarg);
8466913e79cSLluís Vilanova         } else {
84784778508Sblueswir1             usage();
84884778508Sblueswir1         }
84984778508Sblueswir1     }
85084778508Sblueswir1 
851c235d738SMatthew Fernandez     /* init debug */
852f2937a33SPaolo Bonzini     qemu_log_needs_buffers();
853daa76aa4SMarkus Armbruster     qemu_set_log_filename(log_file, &error_fatal);
854c235d738SMatthew Fernandez     if (log_mask) {
855c235d738SMatthew Fernandez         int mask;
856c235d738SMatthew Fernandez 
8574fde1ebaSPeter Maydell         mask = qemu_str_to_log_mask(log_mask);
858c235d738SMatthew Fernandez         if (!mask) {
85959a6fa6eSPeter Maydell             qemu_print_log_usage(stdout);
860c235d738SMatthew Fernandez             exit(1);
861c235d738SMatthew Fernandez         }
86224537a01SPeter Maydell         qemu_set_log(mask);
863c235d738SMatthew Fernandez     }
864c235d738SMatthew Fernandez 
8654b5dfd82SPeter Maydell     if (optind >= argc) {
8664b5dfd82SPeter Maydell         usage();
8674b5dfd82SPeter Maydell     }
8684b5dfd82SPeter Maydell     filename = argv[optind];
8694b5dfd82SPeter Maydell 
8706913e79cSLluís Vilanova     if (!trace_init_backends()) {
8716913e79cSLluís Vilanova         exit(1);
8726913e79cSLluís Vilanova     }
8736913e79cSLluís Vilanova     trace_init_file(trace_file);
8746913e79cSLluís Vilanova 
87584778508Sblueswir1     /* Zero out regs */
87684778508Sblueswir1     memset(regs, 0, sizeof(struct target_pt_regs));
87784778508Sblueswir1 
87884778508Sblueswir1     /* Zero out image_info */
87984778508Sblueswir1     memset(info, 0, sizeof(struct image_info));
88084778508Sblueswir1 
88184778508Sblueswir1     /* Scan interp_prefix dir for replacement files. */
88284778508Sblueswir1     init_paths(interp_prefix);
88384778508Sblueswir1 
88484778508Sblueswir1     if (cpu_model == NULL) {
88531fc12dfSblueswir1 #if defined(TARGET_I386)
88631fc12dfSblueswir1 #ifdef TARGET_X86_64
88731fc12dfSblueswir1         cpu_model = "qemu64";
88831fc12dfSblueswir1 #else
88931fc12dfSblueswir1         cpu_model = "qemu32";
89031fc12dfSblueswir1 #endif
89131fc12dfSblueswir1 #elif defined(TARGET_SPARC)
89284778508Sblueswir1 #ifdef TARGET_SPARC64
89384778508Sblueswir1         cpu_model = "TI UltraSparc II";
89484778508Sblueswir1 #else
89584778508Sblueswir1         cpu_model = "Fujitsu MB86904";
89684778508Sblueswir1 #endif
89784778508Sblueswir1 #else
89884778508Sblueswir1         cpu_model = "any";
89984778508Sblueswir1 #endif
90084778508Sblueswir1     }
901d5ab9713SJan Kiszka     tcg_exec_init(0);
90284778508Sblueswir1     /* NOTE: we need to init the CPU at this stage to get
90384778508Sblueswir1        qemu_host_page_size */
9042994fd96SEduardo Habkost     cpu = cpu_init(cpu_model);
9052994fd96SEduardo Habkost     env = cpu->env_ptr;
90677868120SAndreas Färber #if defined(TARGET_SPARC) || defined(TARGET_PPC)
907db6b81d4SAndreas Färber     cpu_reset(cpu);
908b55a37c9SBlue Swirl #endif
909db6b81d4SAndreas Färber     thread_cpu = cpu;
91084778508Sblueswir1 
91184778508Sblueswir1     if (getenv("QEMU_STRACE")) {
91284778508Sblueswir1         do_strace = 1;
91384778508Sblueswir1     }
91484778508Sblueswir1 
915fc0d96b4SBlue Swirl     target_environ = envlist_to_environ(envlist, NULL);
916fc0d96b4SBlue Swirl     envlist_free(envlist);
91784778508Sblueswir1 
9182fa5d9baSBlue Swirl     /*
9192fa5d9baSBlue Swirl      * Now that page sizes are configured in cpu_init() we can do
9202fa5d9baSBlue Swirl      * proper page alignment for guest_base.
9212fa5d9baSBlue Swirl      */
9222fa5d9baSBlue Swirl     guest_base = HOST_PAGE_ALIGN(guest_base);
9232fa5d9baSBlue Swirl 
9242fa5d9baSBlue Swirl     /*
9252fa5d9baSBlue Swirl      * Read in mmap_min_addr kernel parameter.  This value is used
9262fa5d9baSBlue Swirl      * When loading the ELF image to determine whether guest_base
9272fa5d9baSBlue Swirl      * is needed.
9282fa5d9baSBlue Swirl      *
9292fa5d9baSBlue Swirl      * When user has explicitly set the quest base, we skip this
9302fa5d9baSBlue Swirl      * test.
9312fa5d9baSBlue Swirl      */
9322fa5d9baSBlue Swirl     if (!have_guest_base) {
9332fa5d9baSBlue Swirl         FILE *fp;
9342fa5d9baSBlue Swirl 
9352fa5d9baSBlue Swirl         if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
9362fa5d9baSBlue Swirl             unsigned long tmp;
9372fa5d9baSBlue Swirl             if (fscanf(fp, "%lu", &tmp) == 1) {
9382fa5d9baSBlue Swirl                 mmap_min_addr = tmp;
93913829020SPaolo Bonzini                 qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n", mmap_min_addr);
9402fa5d9baSBlue Swirl             }
9412fa5d9baSBlue Swirl             fclose(fp);
9422fa5d9baSBlue Swirl         }
9432fa5d9baSBlue Swirl     }
94484778508Sblueswir1 
94584778508Sblueswir1     if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
94684778508Sblueswir1         printf("Error loading %s\n", filename);
94784778508Sblueswir1         _exit(1);
94884778508Sblueswir1     }
94984778508Sblueswir1 
95084778508Sblueswir1     for (wrk = target_environ; *wrk; wrk++) {
951ec45bbe5SSaurav Sachidanand         g_free(*wrk);
95284778508Sblueswir1     }
95384778508Sblueswir1 
954ec45bbe5SSaurav Sachidanand     g_free(target_environ);
95584778508Sblueswir1 
95613829020SPaolo Bonzini     if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
9572fa5d9baSBlue Swirl         qemu_log("guest_base  0x%lx\n", guest_base);
95893fcfe39Saliguori         log_page_dump();
95984778508Sblueswir1 
96093fcfe39Saliguori         qemu_log("start_brk   0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
96193fcfe39Saliguori         qemu_log("end_code    0x" TARGET_ABI_FMT_lx "\n", info->end_code);
96293fcfe39Saliguori         qemu_log("start_code  0x" TARGET_ABI_FMT_lx "\n",
96384778508Sblueswir1                  info->start_code);
96493fcfe39Saliguori         qemu_log("start_data  0x" TARGET_ABI_FMT_lx "\n",
96584778508Sblueswir1                  info->start_data);
96693fcfe39Saliguori         qemu_log("end_data    0x" TARGET_ABI_FMT_lx "\n", info->end_data);
96793fcfe39Saliguori         qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
96884778508Sblueswir1                  info->start_stack);
96993fcfe39Saliguori         qemu_log("brk         0x" TARGET_ABI_FMT_lx "\n", info->brk);
97093fcfe39Saliguori         qemu_log("entry       0x" TARGET_ABI_FMT_lx "\n", info->entry);
9712e77eac6Sblueswir1     }
97284778508Sblueswir1 
97384778508Sblueswir1     target_set_brk(info->brk);
97484778508Sblueswir1     syscall_init();
97584778508Sblueswir1     signal_init();
97684778508Sblueswir1 
9779002ec79SRichard Henderson     /* Now that we've loaded the binary, GUEST_BASE is fixed.  Delay
9789002ec79SRichard Henderson        generating the prologue until now so that the prologue can take
9799002ec79SRichard Henderson        the real value of GUEST_BASE into account.  */
980*b1311c4aSEmilio G. Cota     tcg_prologue_init(tcg_ctx);
9819002ec79SRichard Henderson 
98284778508Sblueswir1     /* build Task State */
98384778508Sblueswir1     memset(ts, 0, sizeof(TaskState));
98484778508Sblueswir1     init_task_state(ts);
98584778508Sblueswir1     ts->info = info;
9860429a971SAndreas Färber     cpu->opaque = ts;
98784778508Sblueswir1 
98831fc12dfSblueswir1 #if defined(TARGET_I386)
98931fc12dfSblueswir1     env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
990b98dbc90SPaolo Bonzini     env->hflags |= HF_PE_MASK | HF_CPL_MASK;
9910514ef2fSEduardo Habkost     if (env->features[FEAT_1_EDX] & CPUID_SSE) {
99231fc12dfSblueswir1         env->cr[4] |= CR4_OSFXSR_MASK;
99331fc12dfSblueswir1         env->hflags |= HF_OSFXSR_MASK;
99431fc12dfSblueswir1     }
99531fc12dfSblueswir1 #ifndef TARGET_ABI32
99631fc12dfSblueswir1     /* enable 64 bit mode if possible */
9970514ef2fSEduardo Habkost     if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) {
99831fc12dfSblueswir1         fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
99931fc12dfSblueswir1         exit(1);
100031fc12dfSblueswir1     }
100131fc12dfSblueswir1     env->cr[4] |= CR4_PAE_MASK;
100231fc12dfSblueswir1     env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
100331fc12dfSblueswir1     env->hflags |= HF_LMA_MASK;
100431fc12dfSblueswir1 #endif
100531fc12dfSblueswir1 
100631fc12dfSblueswir1     /* flags setup : we activate the IRQs by default as in user mode */
100731fc12dfSblueswir1     env->eflags |= IF_MASK;
100831fc12dfSblueswir1 
100931fc12dfSblueswir1     /* linux register setup */
101031fc12dfSblueswir1 #ifndef TARGET_ABI32
101131fc12dfSblueswir1     env->regs[R_EAX] = regs->rax;
101231fc12dfSblueswir1     env->regs[R_EBX] = regs->rbx;
101331fc12dfSblueswir1     env->regs[R_ECX] = regs->rcx;
101431fc12dfSblueswir1     env->regs[R_EDX] = regs->rdx;
101531fc12dfSblueswir1     env->regs[R_ESI] = regs->rsi;
101631fc12dfSblueswir1     env->regs[R_EDI] = regs->rdi;
101731fc12dfSblueswir1     env->regs[R_EBP] = regs->rbp;
101831fc12dfSblueswir1     env->regs[R_ESP] = regs->rsp;
101931fc12dfSblueswir1     env->eip = regs->rip;
102031fc12dfSblueswir1 #else
102131fc12dfSblueswir1     env->regs[R_EAX] = regs->eax;
102231fc12dfSblueswir1     env->regs[R_EBX] = regs->ebx;
102331fc12dfSblueswir1     env->regs[R_ECX] = regs->ecx;
102431fc12dfSblueswir1     env->regs[R_EDX] = regs->edx;
102531fc12dfSblueswir1     env->regs[R_ESI] = regs->esi;
102631fc12dfSblueswir1     env->regs[R_EDI] = regs->edi;
102731fc12dfSblueswir1     env->regs[R_EBP] = regs->ebp;
102831fc12dfSblueswir1     env->regs[R_ESP] = regs->esp;
102931fc12dfSblueswir1     env->eip = regs->eip;
103031fc12dfSblueswir1 #endif
103131fc12dfSblueswir1 
103231fc12dfSblueswir1     /* linux interrupt setup */
103331fc12dfSblueswir1 #ifndef TARGET_ABI32
103431fc12dfSblueswir1     env->idt.limit = 511;
103531fc12dfSblueswir1 #else
103631fc12dfSblueswir1     env->idt.limit = 255;
103731fc12dfSblueswir1 #endif
103831fc12dfSblueswir1     env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
103931fc12dfSblueswir1                                 PROT_READ|PROT_WRITE,
104031fc12dfSblueswir1                                 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
104131fc12dfSblueswir1     idt_table = g2h(env->idt.base);
104231fc12dfSblueswir1     set_idt(0, 0);
104331fc12dfSblueswir1     set_idt(1, 0);
104431fc12dfSblueswir1     set_idt(2, 0);
104531fc12dfSblueswir1     set_idt(3, 3);
104631fc12dfSblueswir1     set_idt(4, 3);
104731fc12dfSblueswir1     set_idt(5, 0);
104831fc12dfSblueswir1     set_idt(6, 0);
104931fc12dfSblueswir1     set_idt(7, 0);
105031fc12dfSblueswir1     set_idt(8, 0);
105131fc12dfSblueswir1     set_idt(9, 0);
105231fc12dfSblueswir1     set_idt(10, 0);
105331fc12dfSblueswir1     set_idt(11, 0);
105431fc12dfSblueswir1     set_idt(12, 0);
105531fc12dfSblueswir1     set_idt(13, 0);
105631fc12dfSblueswir1     set_idt(14, 0);
105731fc12dfSblueswir1     set_idt(15, 0);
105831fc12dfSblueswir1     set_idt(16, 0);
105931fc12dfSblueswir1     set_idt(17, 0);
106031fc12dfSblueswir1     set_idt(18, 0);
106131fc12dfSblueswir1     set_idt(19, 0);
106231fc12dfSblueswir1     set_idt(0x80, 3);
106331fc12dfSblueswir1 
106431fc12dfSblueswir1     /* linux segment setup */
106531fc12dfSblueswir1     {
106631fc12dfSblueswir1         uint64_t *gdt_table;
106731fc12dfSblueswir1         env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
106831fc12dfSblueswir1                                     PROT_READ|PROT_WRITE,
106931fc12dfSblueswir1                                     MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
107031fc12dfSblueswir1         env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
107131fc12dfSblueswir1         gdt_table = g2h(env->gdt.base);
107231fc12dfSblueswir1 #ifdef TARGET_ABI32
107331fc12dfSblueswir1         write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
107431fc12dfSblueswir1                  DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
107531fc12dfSblueswir1                  (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
107631fc12dfSblueswir1 #else
107731fc12dfSblueswir1         /* 64 bit code segment */
107831fc12dfSblueswir1         write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
107931fc12dfSblueswir1                  DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
108031fc12dfSblueswir1                  DESC_L_MASK |
108131fc12dfSblueswir1                  (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
108231fc12dfSblueswir1 #endif
108331fc12dfSblueswir1         write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
108431fc12dfSblueswir1                  DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
108531fc12dfSblueswir1                  (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
108631fc12dfSblueswir1     }
108731fc12dfSblueswir1 
108831fc12dfSblueswir1     cpu_x86_load_seg(env, R_CS, __USER_CS);
108931fc12dfSblueswir1     cpu_x86_load_seg(env, R_SS, __USER_DS);
109031fc12dfSblueswir1 #ifdef TARGET_ABI32
109131fc12dfSblueswir1     cpu_x86_load_seg(env, R_DS, __USER_DS);
109231fc12dfSblueswir1     cpu_x86_load_seg(env, R_ES, __USER_DS);
109331fc12dfSblueswir1     cpu_x86_load_seg(env, R_FS, __USER_DS);
109431fc12dfSblueswir1     cpu_x86_load_seg(env, R_GS, __USER_DS);
109531fc12dfSblueswir1     /* This hack makes Wine work... */
109631fc12dfSblueswir1     env->segs[R_FS].selector = 0;
109731fc12dfSblueswir1 #else
109831fc12dfSblueswir1     cpu_x86_load_seg(env, R_DS, 0);
109931fc12dfSblueswir1     cpu_x86_load_seg(env, R_ES, 0);
110031fc12dfSblueswir1     cpu_x86_load_seg(env, R_FS, 0);
110131fc12dfSblueswir1     cpu_x86_load_seg(env, R_GS, 0);
110231fc12dfSblueswir1 #endif
110331fc12dfSblueswir1 #elif defined(TARGET_SPARC)
110484778508Sblueswir1     {
110584778508Sblueswir1         int i;
110684778508Sblueswir1         env->pc = regs->pc;
110784778508Sblueswir1         env->npc = regs->npc;
110884778508Sblueswir1         env->y = regs->y;
110984778508Sblueswir1         for(i = 0; i < 8; i++)
111084778508Sblueswir1             env->gregs[i] = regs->u_regs[i];
111184778508Sblueswir1         for(i = 0; i < 8; i++)
111284778508Sblueswir1             env->regwptr[i] = regs->u_regs[i + 8];
111384778508Sblueswir1     }
111484778508Sblueswir1 #else
111584778508Sblueswir1 #error unsupported target CPU
111684778508Sblueswir1 #endif
111784778508Sblueswir1 
111884778508Sblueswir1     if (gdbstub_port) {
111984778508Sblueswir1         gdbserver_start (gdbstub_port);
1120db6b81d4SAndreas Färber         gdb_handlesig(cpu, 0);
112184778508Sblueswir1     }
112278cfb07fSJuergen Lock     cpu_loop(env);
112384778508Sblueswir1     /* never exits */
112484778508Sblueswir1     return 0;
112584778508Sblueswir1 }
1126