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