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