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