qemu/bsd-user/main.c
<<
>>
Prefs
   1/*
   2 *  qemu user main
   3 *
   4 *  Copyright (c) 2003-2008 Fabrice Bellard
   5 *
   6 *  This program is free software; you can redistribute it and/or modify
   7 *  it under the terms of the GNU General Public License as published by
   8 *  the Free Software Foundation; either version 2 of the License, or
   9 *  (at your option) any later version.
  10 *
  11 *  This program is distributed in the hope that it will be useful,
  12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 *  GNU General Public License for more details.
  15 *
  16 *  You should have received a copy of the GNU General Public License
  17 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  18 */
  19#include "qemu/osdep.h"
  20#include "qemu-version.h"
  21#include <machine/trap.h>
  22
  23#include "qapi/error.h"
  24#include "qemu.h"
  25#include "qemu/config-file.h"
  26#include "qemu/path.h"
  27#include "qemu/help_option.h"
  28/* For tb_lock */
  29#include "cpu.h"
  30#include "exec/exec-all.h"
  31#include "tcg.h"
  32#include "qemu/timer.h"
  33#include "qemu/envlist.h"
  34#include "exec/log.h"
  35#include "trace/control.h"
  36#include "glib-compat.h"
  37
  38int singlestep;
  39unsigned long mmap_min_addr;
  40unsigned long guest_base;
  41int have_guest_base;
  42unsigned long reserved_va;
  43
  44static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
  45const char *qemu_uname_release;
  46extern char **environ;
  47enum BSDType bsd_type;
  48
  49/* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
  50   we allocate a bigger stack. Need a better solution, for example
  51   by remapping the process stack directly at the right place */
  52unsigned long x86_stack_size = 512 * 1024;
  53
  54void gemu_log(const char *fmt, ...)
  55{
  56    va_list ap;
  57
  58    va_start(ap, fmt);
  59    vfprintf(stderr, fmt, ap);
  60    va_end(ap);
  61}
  62
  63#if defined(TARGET_I386)
  64int cpu_get_pic_interrupt(CPUX86State *env)
  65{
  66    return -1;
  67}
  68#endif
  69
  70/* These are no-ops because we are not threadsafe.  */
  71static inline void cpu_exec_start(CPUArchState *env)
  72{
  73}
  74
  75static inline void cpu_exec_end(CPUArchState *env)
  76{
  77}
  78
  79static inline void start_exclusive(void)
  80{
  81}
  82
  83static inline void end_exclusive(void)
  84{
  85}
  86
  87void fork_start(void)
  88{
  89}
  90
  91void fork_end(int child)
  92{
  93    if (child) {
  94        gdbserver_fork(thread_cpu);
  95    }
  96}
  97
  98void cpu_list_lock(void)
  99{
 100}
 101
 102void cpu_list_unlock(void)
 103{
 104}
 105
 106#ifdef TARGET_I386
 107/***********************************************************/
 108/* CPUX86 core interface */
 109
 110uint64_t cpu_get_tsc(CPUX86State *env)
 111{
 112    return cpu_get_host_ticks();
 113}
 114
 115static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
 116                     int flags)
 117{
 118    unsigned int e1, e2;
 119    uint32_t *p;
 120    e1 = (addr << 16) | (limit & 0xffff);
 121    e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
 122    e2 |= flags;
 123    p = ptr;
 124    p[0] = tswap32(e1);
 125    p[1] = tswap32(e2);
 126}
 127
 128static uint64_t *idt_table;
 129#ifdef TARGET_X86_64
 130static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
 131                       uint64_t addr, unsigned int sel)
 132{
 133    uint32_t *p, e1, e2;
 134    e1 = (addr & 0xffff) | (sel << 16);
 135    e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
 136    p = ptr;
 137    p[0] = tswap32(e1);
 138    p[1] = tswap32(e2);
 139    p[2] = tswap32(addr >> 32);
 140    p[3] = 0;
 141}
 142/* only dpl matters as we do only user space emulation */
 143static void set_idt(int n, unsigned int dpl)
 144{
 145    set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
 146}
 147#else
 148static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
 149                     uint32_t addr, unsigned int sel)
 150{
 151    uint32_t *p, e1, e2;
 152    e1 = (addr & 0xffff) | (sel << 16);
 153    e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
 154    p = ptr;
 155    p[0] = tswap32(e1);
 156    p[1] = tswap32(e2);
 157}
 158
 159/* only dpl matters as we do only user space emulation */
 160static void set_idt(int n, unsigned int dpl)
 161{
 162    set_gate(idt_table + n, 0, dpl, 0, 0);
 163}
 164#endif
 165
 166void cpu_loop(CPUX86State *env)
 167{
 168    X86CPU *cpu = x86_env_get_cpu(env);
 169    CPUState *cs = CPU(cpu);
 170    int trapnr;
 171    abi_ulong pc;
 172    //target_siginfo_t info;
 173
 174    for(;;) {
 175        trapnr = cpu_exec(cs);
 176        switch(trapnr) {
 177        case 0x80:
 178            /* syscall from int $0x80 */
 179            if (bsd_type == target_freebsd) {
 180                abi_ulong params = (abi_ulong) env->regs[R_ESP] +
 181                    sizeof(int32_t);
 182                int32_t syscall_nr = env->regs[R_EAX];
 183                int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
 184
 185                if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
 186                    get_user_s32(syscall_nr, params);
 187                    params += sizeof(int32_t);
 188                } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
 189                    get_user_s32(syscall_nr, params);
 190                    params += sizeof(int64_t);
 191                }
 192                get_user_s32(arg1, params);
 193                params += sizeof(int32_t);
 194                get_user_s32(arg2, params);
 195                params += sizeof(int32_t);
 196                get_user_s32(arg3, params);
 197                params += sizeof(int32_t);
 198                get_user_s32(arg4, params);
 199                params += sizeof(int32_t);
 200                get_user_s32(arg5, params);
 201                params += sizeof(int32_t);
 202                get_user_s32(arg6, params);
 203                params += sizeof(int32_t);
 204                get_user_s32(arg7, params);
 205                params += sizeof(int32_t);
 206                get_user_s32(arg8, params);
 207                env->regs[R_EAX] = do_freebsd_syscall(env,
 208                                                      syscall_nr,
 209                                                      arg1,
 210                                                      arg2,
 211                                                      arg3,
 212                                                      arg4,
 213                                                      arg5,
 214                                                      arg6,
 215                                                      arg7,
 216                                                      arg8);
 217            } else { //if (bsd_type == target_openbsd)
 218                env->regs[R_EAX] = do_openbsd_syscall(env,
 219                                                      env->regs[R_EAX],
 220                                                      env->regs[R_EBX],
 221                                                      env->regs[R_ECX],
 222                                                      env->regs[R_EDX],
 223                                                      env->regs[R_ESI],
 224                                                      env->regs[R_EDI],
 225                                                      env->regs[R_EBP]);
 226            }
 227            if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
 228                env->regs[R_EAX] = -env->regs[R_EAX];
 229                env->eflags |= CC_C;
 230            } else {
 231                env->eflags &= ~CC_C;
 232            }
 233            break;
 234#ifndef TARGET_ABI32
 235        case EXCP_SYSCALL:
 236            /* syscall from syscall instruction */
 237            if (bsd_type == target_freebsd)
 238                env->regs[R_EAX] = do_freebsd_syscall(env,
 239                                                      env->regs[R_EAX],
 240                                                      env->regs[R_EDI],
 241                                                      env->regs[R_ESI],
 242                                                      env->regs[R_EDX],
 243                                                      env->regs[R_ECX],
 244                                                      env->regs[8],
 245                                                      env->regs[9], 0, 0);
 246            else { //if (bsd_type == target_openbsd)
 247                env->regs[R_EAX] = do_openbsd_syscall(env,
 248                                                      env->regs[R_EAX],
 249                                                      env->regs[R_EDI],
 250                                                      env->regs[R_ESI],
 251                                                      env->regs[R_EDX],
 252                                                      env->regs[10],
 253                                                      env->regs[8],
 254                                                      env->regs[9]);
 255            }
 256            env->eip = env->exception_next_eip;
 257            if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
 258                env->regs[R_EAX] = -env->regs[R_EAX];
 259                env->eflags |= CC_C;
 260            } else {
 261                env->eflags &= ~CC_C;
 262            }
 263            break;
 264#endif
 265#if 0
 266        case EXCP0B_NOSEG:
 267        case EXCP0C_STACK:
 268            info.si_signo = SIGBUS;
 269            info.si_errno = 0;
 270            info.si_code = TARGET_SI_KERNEL;
 271            info._sifields._sigfault._addr = 0;
 272            queue_signal(env, info.si_signo, &info);
 273            break;
 274        case EXCP0D_GPF:
 275            /* XXX: potential problem if ABI32 */
 276#ifndef TARGET_X86_64
 277            if (env->eflags & VM_MASK) {
 278                handle_vm86_fault(env);
 279            } else
 280#endif
 281            {
 282                info.si_signo = SIGSEGV;
 283                info.si_errno = 0;
 284                info.si_code = TARGET_SI_KERNEL;
 285                info._sifields._sigfault._addr = 0;
 286                queue_signal(env, info.si_signo, &info);
 287            }
 288            break;
 289        case EXCP0E_PAGE:
 290            info.si_signo = SIGSEGV;
 291            info.si_errno = 0;
 292            if (!(env->error_code & 1))
 293                info.si_code = TARGET_SEGV_MAPERR;
 294            else
 295                info.si_code = TARGET_SEGV_ACCERR;
 296            info._sifields._sigfault._addr = env->cr[2];
 297            queue_signal(env, info.si_signo, &info);
 298            break;
 299        case EXCP00_DIVZ:
 300#ifndef TARGET_X86_64
 301            if (env->eflags & VM_MASK) {
 302                handle_vm86_trap(env, trapnr);
 303            } else
 304#endif
 305            {
 306                /* division by zero */
 307                info.si_signo = SIGFPE;
 308                info.si_errno = 0;
 309                info.si_code = TARGET_FPE_INTDIV;
 310                info._sifields._sigfault._addr = env->eip;
 311                queue_signal(env, info.si_signo, &info);
 312            }
 313            break;
 314        case EXCP01_DB:
 315        case EXCP03_INT3:
 316#ifndef TARGET_X86_64
 317            if (env->eflags & VM_MASK) {
 318                handle_vm86_trap(env, trapnr);
 319            } else
 320#endif
 321            {
 322                info.si_signo = SIGTRAP;
 323                info.si_errno = 0;
 324                if (trapnr == EXCP01_DB) {
 325                    info.si_code = TARGET_TRAP_BRKPT;
 326                    info._sifields._sigfault._addr = env->eip;
 327                } else {
 328                    info.si_code = TARGET_SI_KERNEL;
 329                    info._sifields._sigfault._addr = 0;
 330                }
 331                queue_signal(env, info.si_signo, &info);
 332            }
 333            break;
 334        case EXCP04_INTO:
 335        case EXCP05_BOUND:
 336#ifndef TARGET_X86_64
 337            if (env->eflags & VM_MASK) {
 338                handle_vm86_trap(env, trapnr);
 339            } else
 340#endif
 341            {
 342                info.si_signo = SIGSEGV;
 343                info.si_errno = 0;
 344                info.si_code = TARGET_SI_KERNEL;
 345                info._sifields._sigfault._addr = 0;
 346                queue_signal(env, info.si_signo, &info);
 347            }
 348            break;
 349        case EXCP06_ILLOP:
 350            info.si_signo = SIGILL;
 351            info.si_errno = 0;
 352            info.si_code = TARGET_ILL_ILLOPN;
 353            info._sifields._sigfault._addr = env->eip;
 354            queue_signal(env, info.si_signo, &info);
 355            break;
 356#endif
 357        case EXCP_INTERRUPT:
 358            /* just indicate that signals should be handled asap */
 359            break;
 360#if 0
 361        case EXCP_DEBUG:
 362            {
 363                int sig;
 364
 365                sig = gdb_handlesig (env, TARGET_SIGTRAP);
 366                if (sig)
 367                  {
 368                    info.si_signo = sig;
 369                    info.si_errno = 0;
 370                    info.si_code = TARGET_TRAP_BRKPT;
 371                    queue_signal(env, info.si_signo, &info);
 372                  }
 373            }
 374            break;
 375#endif
 376        default:
 377            pc = env->segs[R_CS].base + env->eip;
 378            fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
 379                    (long)pc, trapnr);
 380            abort();
 381        }
 382        process_pending_signals(env);
 383    }
 384}
 385#endif
 386
 387#ifdef TARGET_SPARC
 388#define SPARC64_STACK_BIAS 2047
 389
 390//#define DEBUG_WIN
 391/* WARNING: dealing with register windows _is_ complicated. More info
 392   can be found at http://www.sics.se/~psm/sparcstack.html */
 393static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
 394{
 395    index = (index + cwp * 16) % (16 * env->nwindows);
 396    /* wrap handling : if cwp is on the last window, then we use the
 397       registers 'after' the end */
 398    if (index < 8 && env->cwp == env->nwindows - 1)
 399        index += 16 * env->nwindows;
 400    return index;
 401}
 402
 403/* save the register window 'cwp1' */
 404static inline void save_window_offset(CPUSPARCState *env, int cwp1)
 405{
 406    unsigned int i;
 407    abi_ulong sp_ptr;
 408
 409    sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
 410#ifdef TARGET_SPARC64
 411    if (sp_ptr & 3)
 412        sp_ptr += SPARC64_STACK_BIAS;
 413#endif
 414#if defined(DEBUG_WIN)
 415    printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
 416           sp_ptr, cwp1);
 417#endif
 418    for(i = 0; i < 16; i++) {
 419        /* FIXME - what to do if put_user() fails? */
 420        put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
 421        sp_ptr += sizeof(abi_ulong);
 422    }
 423}
 424
 425static void save_window(CPUSPARCState *env)
 426{
 427#ifndef TARGET_SPARC64
 428    unsigned int new_wim;
 429    new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
 430        ((1LL << env->nwindows) - 1);
 431    save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
 432    env->wim = new_wim;
 433#else
 434    save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
 435    env->cansave++;
 436    env->canrestore--;
 437#endif
 438}
 439
 440static void restore_window(CPUSPARCState *env)
 441{
 442#ifndef TARGET_SPARC64
 443    unsigned int new_wim;
 444#endif
 445    unsigned int i, cwp1;
 446    abi_ulong sp_ptr;
 447
 448#ifndef TARGET_SPARC64
 449    new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
 450        ((1LL << env->nwindows) - 1);
 451#endif
 452
 453    /* restore the invalid window */
 454    cwp1 = cpu_cwp_inc(env, env->cwp + 1);
 455    sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
 456#ifdef TARGET_SPARC64
 457    if (sp_ptr & 3)
 458        sp_ptr += SPARC64_STACK_BIAS;
 459#endif
 460#if defined(DEBUG_WIN)
 461    printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
 462           sp_ptr, cwp1);
 463#endif
 464    for(i = 0; i < 16; i++) {
 465        /* FIXME - what to do if get_user() fails? */
 466        get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
 467        sp_ptr += sizeof(abi_ulong);
 468    }
 469#ifdef TARGET_SPARC64
 470    env->canrestore++;
 471    if (env->cleanwin < env->nwindows - 1)
 472        env->cleanwin++;
 473    env->cansave--;
 474#else
 475    env->wim = new_wim;
 476#endif
 477}
 478
 479static void flush_windows(CPUSPARCState *env)
 480{
 481    int offset, cwp1;
 482
 483    offset = 1;
 484    for(;;) {
 485        /* if restore would invoke restore_window(), then we can stop */
 486        cwp1 = cpu_cwp_inc(env, env->cwp + offset);
 487#ifndef TARGET_SPARC64
 488        if (env->wim & (1 << cwp1))
 489            break;
 490#else
 491        if (env->canrestore == 0)
 492            break;
 493        env->cansave++;
 494        env->canrestore--;
 495#endif
 496        save_window_offset(env, cwp1);
 497        offset++;
 498    }
 499    cwp1 = cpu_cwp_inc(env, env->cwp + 1);
 500#ifndef TARGET_SPARC64
 501    /* set wim so that restore will reload the registers */
 502    env->wim = 1 << cwp1;
 503#endif
 504#if defined(DEBUG_WIN)
 505    printf("flush_windows: nb=%d\n", offset - 1);
 506#endif
 507}
 508
 509void cpu_loop(CPUSPARCState *env)
 510{
 511    CPUState *cs = CPU(sparc_env_get_cpu(env));
 512    int trapnr, ret, syscall_nr;
 513    //target_siginfo_t info;
 514
 515    while (1) {
 516        trapnr = cpu_exec(cs);
 517
 518        switch (trapnr) {
 519#ifndef TARGET_SPARC64
 520        case 0x80:
 521#else
 522        /* FreeBSD uses 0x141 for syscalls too */
 523        case 0x141:
 524            if (bsd_type != target_freebsd)
 525                goto badtrap;
 526        case 0x100:
 527#endif
 528            syscall_nr = env->gregs[1];
 529            if (bsd_type == target_freebsd)
 530                ret = do_freebsd_syscall(env, syscall_nr,
 531                                         env->regwptr[0], env->regwptr[1],
 532                                         env->regwptr[2], env->regwptr[3],
 533                                         env->regwptr[4], env->regwptr[5], 0, 0);
 534            else if (bsd_type == target_netbsd)
 535                ret = do_netbsd_syscall(env, syscall_nr,
 536                                        env->regwptr[0], env->regwptr[1],
 537                                        env->regwptr[2], env->regwptr[3],
 538                                        env->regwptr[4], env->regwptr[5]);
 539            else { //if (bsd_type == target_openbsd)
 540#if defined(TARGET_SPARC64)
 541                syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG |
 542                                TARGET_OPENBSD_SYSCALL_G2RFLAG);
 543#endif
 544                ret = do_openbsd_syscall(env, syscall_nr,
 545                                         env->regwptr[0], env->regwptr[1],
 546                                         env->regwptr[2], env->regwptr[3],
 547                                         env->regwptr[4], env->regwptr[5]);
 548            }
 549            if ((unsigned int)ret >= (unsigned int)(-515)) {
 550                ret = -ret;
 551#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
 552                env->xcc |= PSR_CARRY;
 553#else
 554                env->psr |= PSR_CARRY;
 555#endif
 556            } else {
 557#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
 558                env->xcc &= ~PSR_CARRY;
 559#else
 560                env->psr &= ~PSR_CARRY;
 561#endif
 562            }
 563            env->regwptr[0] = ret;
 564            /* next instruction */
 565#if defined(TARGET_SPARC64)
 566            if (bsd_type == target_openbsd &&
 567                env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) {
 568                env->pc = env->gregs[2];
 569                env->npc = env->pc + 4;
 570            } else if (bsd_type == target_openbsd &&
 571                       env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) {
 572                env->pc = env->gregs[7];
 573                env->npc = env->pc + 4;
 574            } else {
 575                env->pc = env->npc;
 576                env->npc = env->npc + 4;
 577            }
 578#else
 579            env->pc = env->npc;
 580            env->npc = env->npc + 4;
 581#endif
 582            break;
 583        case 0x83: /* flush windows */
 584#ifdef TARGET_ABI32
 585        case 0x103:
 586#endif
 587            flush_windows(env);
 588            /* next instruction */
 589            env->pc = env->npc;
 590            env->npc = env->npc + 4;
 591            break;
 592#ifndef TARGET_SPARC64
 593        case TT_WIN_OVF: /* window overflow */
 594            save_window(env);
 595            break;
 596        case TT_WIN_UNF: /* window underflow */
 597            restore_window(env);
 598            break;
 599        case TT_TFAULT:
 600        case TT_DFAULT:
 601#if 0
 602            {
 603                info.si_signo = SIGSEGV;
 604                info.si_errno = 0;
 605                /* XXX: check env->error_code */
 606                info.si_code = TARGET_SEGV_MAPERR;
 607                info._sifields._sigfault._addr = env->mmuregs[4];
 608                queue_signal(env, info.si_signo, &info);
 609            }
 610#endif
 611            break;
 612#else
 613        case TT_SPILL: /* window overflow */
 614            save_window(env);
 615            break;
 616        case TT_FILL: /* window underflow */
 617            restore_window(env);
 618            break;
 619        case TT_TFAULT:
 620        case TT_DFAULT:
 621#if 0
 622            {
 623                info.si_signo = SIGSEGV;
 624                info.si_errno = 0;
 625                /* XXX: check env->error_code */
 626                info.si_code = TARGET_SEGV_MAPERR;
 627                if (trapnr == TT_DFAULT)
 628                    info._sifields._sigfault._addr = env->dmmuregs[4];
 629                else
 630                    info._sifields._sigfault._addr = env->tsptr->tpc;
 631                //queue_signal(env, info.si_signo, &info);
 632            }
 633#endif
 634            break;
 635#endif
 636        case EXCP_INTERRUPT:
 637            /* just indicate that signals should be handled asap */
 638            break;
 639        case EXCP_DEBUG:
 640            {
 641                int sig;
 642
 643                sig = gdb_handlesig(cs, TARGET_SIGTRAP);
 644#if 0
 645                if (sig)
 646                  {
 647                    info.si_signo = sig;
 648                    info.si_errno = 0;
 649                    info.si_code = TARGET_TRAP_BRKPT;
 650                    //queue_signal(env, info.si_signo, &info);
 651                  }
 652#endif
 653            }
 654            break;
 655        default:
 656#ifdef TARGET_SPARC64
 657        badtrap:
 658#endif
 659            printf ("Unhandled trap: 0x%x\n", trapnr);
 660            cpu_dump_state(cs, stderr, fprintf, 0);
 661            exit (1);
 662        }
 663        process_pending_signals (env);
 664    }
 665}
 666
 667#endif
 668
 669static void usage(void)
 670{
 671    printf("qemu-" TARGET_NAME " version " QEMU_VERSION QEMU_PKGVERSION
 672           ", " QEMU_COPYRIGHT "\n"
 673           "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
 674           "BSD CPU emulator (compiled for %s emulation)\n"
 675           "\n"
 676           "Standard options:\n"
 677           "-h                print this help\n"
 678           "-g port           wait gdb connection to port\n"
 679           "-L path           set the elf interpreter prefix (default=%s)\n"
 680           "-s size           set the stack size in bytes (default=%ld)\n"
 681           "-cpu model        select CPU (-cpu help for list)\n"
 682           "-drop-ld-preload  drop LD_PRELOAD for target process\n"
 683           "-E var=value      sets/modifies targets environment variable(s)\n"
 684           "-U var            unsets targets environment variable(s)\n"
 685           "-B address        set guest_base address to address\n"
 686           "-bsd type         select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
 687           "\n"
 688           "Debug options:\n"
 689           "-d item1[,...]    enable logging of specified items\n"
 690           "                  (use '-d help' for a list of log items)\n"
 691           "-D logfile        write logs to 'logfile' (default stderr)\n"
 692           "-p pagesize       set the host page size to 'pagesize'\n"
 693           "-singlestep       always run in singlestep mode\n"
 694           "-strace           log system calls\n"
 695           "-trace            [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
 696           "                  specify tracing options\n"
 697           "\n"
 698           "Environment variables:\n"
 699           "QEMU_STRACE       Print system calls and arguments similar to the\n"
 700           "                  'strace' program.  Enable by setting to any value.\n"
 701           "You can use -E and -U options to set/unset environment variables\n"
 702           "for target process.  It is possible to provide several variables\n"
 703           "by repeating the option.  For example:\n"
 704           "    -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
 705           "Note that if you provide several changes to single variable\n"
 706           "last change will stay in effect.\n"
 707           ,
 708           TARGET_NAME,
 709           interp_prefix,
 710           x86_stack_size);
 711    exit(1);
 712}
 713
 714THREAD CPUState *thread_cpu;
 715
 716/* Assumes contents are already zeroed.  */
 717void init_task_state(TaskState *ts)
 718{
 719    int i;
 720
 721    ts->used = 1;
 722    ts->first_free = ts->sigqueue_table;
 723    for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
 724        ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
 725    }
 726    ts->sigqueue_table[i].next = NULL;
 727}
 728
 729int main(int argc, char **argv)
 730{
 731    const char *filename;
 732    const char *cpu_model;
 733    const char *log_file = NULL;
 734    const char *log_mask = NULL;
 735    struct target_pt_regs regs1, *regs = &regs1;
 736    struct image_info info1, *info = &info1;
 737    TaskState ts1, *ts = &ts1;
 738    CPUArchState *env;
 739    CPUState *cpu;
 740    int optind;
 741    const char *r;
 742    int gdbstub_port = 0;
 743    char **target_environ, **wrk;
 744    envlist_t *envlist = NULL;
 745    char *trace_file = NULL;
 746    bsd_type = target_openbsd;
 747
 748    if (argc <= 1)
 749        usage();
 750
 751    module_call_init(MODULE_INIT_QOM);
 752
 753    if ((envlist = envlist_create()) == NULL) {
 754        (void) fprintf(stderr, "Unable to allocate envlist\n");
 755        exit(1);
 756    }
 757
 758    /* add current environment into the list */
 759    for (wrk = environ; *wrk != NULL; wrk++) {
 760        (void) envlist_setenv(envlist, *wrk);
 761    }
 762
 763    cpu_model = NULL;
 764
 765    qemu_add_opts(&qemu_trace_opts);
 766
 767    optind = 1;
 768    for (;;) {
 769        if (optind >= argc)
 770            break;
 771        r = argv[optind];
 772        if (r[0] != '-')
 773            break;
 774        optind++;
 775        r++;
 776        if (!strcmp(r, "-")) {
 777            break;
 778        } else if (!strcmp(r, "d")) {
 779            if (optind >= argc) {
 780                break;
 781            }
 782            log_mask = argv[optind++];
 783        } else if (!strcmp(r, "D")) {
 784            if (optind >= argc) {
 785                break;
 786            }
 787            log_file = argv[optind++];
 788        } else if (!strcmp(r, "E")) {
 789            r = argv[optind++];
 790            if (envlist_setenv(envlist, r) != 0)
 791                usage();
 792        } else if (!strcmp(r, "ignore-environment")) {
 793            envlist_free(envlist);
 794            if ((envlist = envlist_create()) == NULL) {
 795                (void) fprintf(stderr, "Unable to allocate envlist\n");
 796                exit(1);
 797            }
 798        } else if (!strcmp(r, "U")) {
 799            r = argv[optind++];
 800            if (envlist_unsetenv(envlist, r) != 0)
 801                usage();
 802        } else if (!strcmp(r, "s")) {
 803            r = argv[optind++];
 804            x86_stack_size = strtol(r, (char **)&r, 0);
 805            if (x86_stack_size <= 0)
 806                usage();
 807            if (*r == 'M')
 808                x86_stack_size *= 1024 * 1024;
 809            else if (*r == 'k' || *r == 'K')
 810                x86_stack_size *= 1024;
 811        } else if (!strcmp(r, "L")) {
 812            interp_prefix = argv[optind++];
 813        } else if (!strcmp(r, "p")) {
 814            qemu_host_page_size = atoi(argv[optind++]);
 815            if (qemu_host_page_size == 0 ||
 816                (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
 817                fprintf(stderr, "page size must be a power of two\n");
 818                exit(1);
 819            }
 820        } else if (!strcmp(r, "g")) {
 821            gdbstub_port = atoi(argv[optind++]);
 822        } else if (!strcmp(r, "r")) {
 823            qemu_uname_release = argv[optind++];
 824        } else if (!strcmp(r, "cpu")) {
 825            cpu_model = argv[optind++];
 826            if (is_help_option(cpu_model)) {
 827/* XXX: implement xxx_cpu_list for targets that still miss it */
 828#if defined(cpu_list)
 829                    cpu_list(stdout, &fprintf);
 830#endif
 831                exit(1);
 832            }
 833        } else if (!strcmp(r, "B")) {
 834           guest_base = strtol(argv[optind++], NULL, 0);
 835           have_guest_base = 1;
 836        } else if (!strcmp(r, "drop-ld-preload")) {
 837            (void) envlist_unsetenv(envlist, "LD_PRELOAD");
 838        } else if (!strcmp(r, "bsd")) {
 839            if (!strcasecmp(argv[optind], "freebsd")) {
 840                bsd_type = target_freebsd;
 841            } else if (!strcasecmp(argv[optind], "netbsd")) {
 842                bsd_type = target_netbsd;
 843            } else if (!strcasecmp(argv[optind], "openbsd")) {
 844                bsd_type = target_openbsd;
 845            } else {
 846                usage();
 847            }
 848            optind++;
 849        } else if (!strcmp(r, "singlestep")) {
 850            singlestep = 1;
 851        } else if (!strcmp(r, "strace")) {
 852            do_strace = 1;
 853        } else if (!strcmp(r, "trace")) {
 854            g_free(trace_file);
 855            trace_file = trace_opt_parse(optarg);
 856        } else {
 857            usage();
 858        }
 859    }
 860
 861    /* init debug */
 862    qemu_log_needs_buffers();
 863    qemu_set_log_filename(log_file, &error_fatal);
 864    if (log_mask) {
 865        int mask;
 866
 867        mask = qemu_str_to_log_mask(log_mask);
 868        if (!mask) {
 869            qemu_print_log_usage(stdout);
 870            exit(1);
 871        }
 872        qemu_set_log(mask);
 873    }
 874
 875    if (optind >= argc) {
 876        usage();
 877    }
 878    filename = argv[optind];
 879
 880    if (!trace_init_backends()) {
 881        exit(1);
 882    }
 883    trace_init_file(trace_file);
 884
 885    /* Zero out regs */
 886    memset(regs, 0, sizeof(struct target_pt_regs));
 887
 888    /* Zero out image_info */
 889    memset(info, 0, sizeof(struct image_info));
 890
 891    /* Scan interp_prefix dir for replacement files. */
 892    init_paths(interp_prefix);
 893
 894    if (cpu_model == NULL) {
 895#if defined(TARGET_I386)
 896#ifdef TARGET_X86_64
 897        cpu_model = "qemu64";
 898#else
 899        cpu_model = "qemu32";
 900#endif
 901#elif defined(TARGET_SPARC)
 902#ifdef TARGET_SPARC64
 903        cpu_model = "TI UltraSparc II";
 904#else
 905        cpu_model = "Fujitsu MB86904";
 906#endif
 907#else
 908        cpu_model = "any";
 909#endif
 910    }
 911    tcg_exec_init(0);
 912    /* NOTE: we need to init the CPU at this stage to get
 913       qemu_host_page_size */
 914    cpu = cpu_init(cpu_model);
 915    if (!cpu) {
 916        fprintf(stderr, "Unable to find CPU definition\n");
 917        exit(1);
 918    }
 919    env = cpu->env_ptr;
 920#if defined(TARGET_SPARC) || defined(TARGET_PPC)
 921    cpu_reset(cpu);
 922#endif
 923    thread_cpu = cpu;
 924
 925    if (getenv("QEMU_STRACE")) {
 926        do_strace = 1;
 927    }
 928
 929    target_environ = envlist_to_environ(envlist, NULL);
 930    envlist_free(envlist);
 931
 932    /*
 933     * Now that page sizes are configured in cpu_init() we can do
 934     * proper page alignment for guest_base.
 935     */
 936    guest_base = HOST_PAGE_ALIGN(guest_base);
 937
 938    /*
 939     * Read in mmap_min_addr kernel parameter.  This value is used
 940     * When loading the ELF image to determine whether guest_base
 941     * is needed.
 942     *
 943     * When user has explicitly set the quest base, we skip this
 944     * test.
 945     */
 946    if (!have_guest_base) {
 947        FILE *fp;
 948
 949        if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
 950            unsigned long tmp;
 951            if (fscanf(fp, "%lu", &tmp) == 1) {
 952                mmap_min_addr = tmp;
 953                qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n", mmap_min_addr);
 954            }
 955            fclose(fp);
 956        }
 957    }
 958
 959    if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
 960        printf("Error loading %s\n", filename);
 961        _exit(1);
 962    }
 963
 964    for (wrk = target_environ; *wrk; wrk++) {
 965        free(*wrk);
 966    }
 967
 968    free(target_environ);
 969
 970    if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
 971        qemu_log("guest_base  0x%lx\n", guest_base);
 972        log_page_dump();
 973
 974        qemu_log("start_brk   0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
 975        qemu_log("end_code    0x" TARGET_ABI_FMT_lx "\n", info->end_code);
 976        qemu_log("start_code  0x" TARGET_ABI_FMT_lx "\n",
 977                 info->start_code);
 978        qemu_log("start_data  0x" TARGET_ABI_FMT_lx "\n",
 979                 info->start_data);
 980        qemu_log("end_data    0x" TARGET_ABI_FMT_lx "\n", info->end_data);
 981        qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
 982                 info->start_stack);
 983        qemu_log("brk         0x" TARGET_ABI_FMT_lx "\n", info->brk);
 984        qemu_log("entry       0x" TARGET_ABI_FMT_lx "\n", info->entry);
 985    }
 986
 987    target_set_brk(info->brk);
 988    syscall_init();
 989    signal_init();
 990
 991    /* Now that we've loaded the binary, GUEST_BASE is fixed.  Delay
 992       generating the prologue until now so that the prologue can take
 993       the real value of GUEST_BASE into account.  */
 994    tcg_prologue_init(&tcg_ctx);
 995
 996    /* build Task State */
 997    memset(ts, 0, sizeof(TaskState));
 998    init_task_state(ts);
 999    ts->info = info;
1000    cpu->opaque = ts;
1001
1002#if defined(TARGET_I386)
1003    env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
1004    env->hflags |= HF_PE_MASK | HF_CPL_MASK;
1005    if (env->features[FEAT_1_EDX] & CPUID_SSE) {
1006        env->cr[4] |= CR4_OSFXSR_MASK;
1007        env->hflags |= HF_OSFXSR_MASK;
1008    }
1009#ifndef TARGET_ABI32
1010    /* enable 64 bit mode if possible */
1011    if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) {
1012        fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
1013        exit(1);
1014    }
1015    env->cr[4] |= CR4_PAE_MASK;
1016    env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
1017    env->hflags |= HF_LMA_MASK;
1018#endif
1019
1020    /* flags setup : we activate the IRQs by default as in user mode */
1021    env->eflags |= IF_MASK;
1022
1023    /* linux register setup */
1024#ifndef TARGET_ABI32
1025    env->regs[R_EAX] = regs->rax;
1026    env->regs[R_EBX] = regs->rbx;
1027    env->regs[R_ECX] = regs->rcx;
1028    env->regs[R_EDX] = regs->rdx;
1029    env->regs[R_ESI] = regs->rsi;
1030    env->regs[R_EDI] = regs->rdi;
1031    env->regs[R_EBP] = regs->rbp;
1032    env->regs[R_ESP] = regs->rsp;
1033    env->eip = regs->rip;
1034#else
1035    env->regs[R_EAX] = regs->eax;
1036    env->regs[R_EBX] = regs->ebx;
1037    env->regs[R_ECX] = regs->ecx;
1038    env->regs[R_EDX] = regs->edx;
1039    env->regs[R_ESI] = regs->esi;
1040    env->regs[R_EDI] = regs->edi;
1041    env->regs[R_EBP] = regs->ebp;
1042    env->regs[R_ESP] = regs->esp;
1043    env->eip = regs->eip;
1044#endif
1045
1046    /* linux interrupt setup */
1047#ifndef TARGET_ABI32
1048    env->idt.limit = 511;
1049#else
1050    env->idt.limit = 255;
1051#endif
1052    env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
1053                                PROT_READ|PROT_WRITE,
1054                                MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
1055    idt_table = g2h(env->idt.base);
1056    set_idt(0, 0);
1057    set_idt(1, 0);
1058    set_idt(2, 0);
1059    set_idt(3, 3);
1060    set_idt(4, 3);
1061    set_idt(5, 0);
1062    set_idt(6, 0);
1063    set_idt(7, 0);
1064    set_idt(8, 0);
1065    set_idt(9, 0);
1066    set_idt(10, 0);
1067    set_idt(11, 0);
1068    set_idt(12, 0);
1069    set_idt(13, 0);
1070    set_idt(14, 0);
1071    set_idt(15, 0);
1072    set_idt(16, 0);
1073    set_idt(17, 0);
1074    set_idt(18, 0);
1075    set_idt(19, 0);
1076    set_idt(0x80, 3);
1077
1078    /* linux segment setup */
1079    {
1080        uint64_t *gdt_table;
1081        env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
1082                                    PROT_READ|PROT_WRITE,
1083                                    MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
1084        env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
1085        gdt_table = g2h(env->gdt.base);
1086#ifdef TARGET_ABI32
1087        write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
1088                 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1089                 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
1090#else
1091        /* 64 bit code segment */
1092        write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
1093                 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1094                 DESC_L_MASK |
1095                 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
1096#endif
1097        write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
1098                 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1099                 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
1100    }
1101
1102    cpu_x86_load_seg(env, R_CS, __USER_CS);
1103    cpu_x86_load_seg(env, R_SS, __USER_DS);
1104#ifdef TARGET_ABI32
1105    cpu_x86_load_seg(env, R_DS, __USER_DS);
1106    cpu_x86_load_seg(env, R_ES, __USER_DS);
1107    cpu_x86_load_seg(env, R_FS, __USER_DS);
1108    cpu_x86_load_seg(env, R_GS, __USER_DS);
1109    /* This hack makes Wine work... */
1110    env->segs[R_FS].selector = 0;
1111#else
1112    cpu_x86_load_seg(env, R_DS, 0);
1113    cpu_x86_load_seg(env, R_ES, 0);
1114    cpu_x86_load_seg(env, R_FS, 0);
1115    cpu_x86_load_seg(env, R_GS, 0);
1116#endif
1117#elif defined(TARGET_SPARC)
1118    {
1119        int i;
1120        env->pc = regs->pc;
1121        env->npc = regs->npc;
1122        env->y = regs->y;
1123        for(i = 0; i < 8; i++)
1124            env->gregs[i] = regs->u_regs[i];
1125        for(i = 0; i < 8; i++)
1126            env->regwptr[i] = regs->u_regs[i + 8];
1127    }
1128#else
1129#error unsupported target CPU
1130#endif
1131
1132    if (gdbstub_port) {
1133        gdbserver_start (gdbstub_port);
1134        gdb_handlesig(cpu, 0);
1135    }
1136    trace_init_vcpu_events();
1137    cpu_loop(env);
1138    /* never exits */
1139    return 0;
1140}
1141