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 *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 char *trace_file = NULL; 739 bsd_type = target_openbsd; 740 741 if (argc <= 1) 742 usage(); 743 744 module_call_init(MODULE_INIT_TRACE); 745 qemu_init_cpu_list(); 746 module_call_init(MODULE_INIT_QOM); 747 748 envlist = envlist_create(); 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 757 qemu_add_opts(&qemu_trace_opts); 758 759 optind = 1; 760 for (;;) { 761 if (optind >= argc) 762 break; 763 r = argv[optind]; 764 if (r[0] != '-') 765 break; 766 optind++; 767 r++; 768 if (!strcmp(r, "-")) { 769 break; 770 } else if (!strcmp(r, "d")) { 771 if (optind >= argc) { 772 break; 773 } 774 log_mask = argv[optind++]; 775 } else if (!strcmp(r, "D")) { 776 if (optind >= argc) { 777 break; 778 } 779 log_file = argv[optind++]; 780 } else if (!strcmp(r, "E")) { 781 r = argv[optind++]; 782 if (envlist_setenv(envlist, r) != 0) 783 usage(); 784 } else if (!strcmp(r, "ignore-environment")) { 785 envlist_free(envlist); 786 envlist = envlist_create(); 787 } else if (!strcmp(r, "U")) { 788 r = argv[optind++]; 789 if (envlist_unsetenv(envlist, r) != 0) 790 usage(); 791 } else if (!strcmp(r, "s")) { 792 r = argv[optind++]; 793 x86_stack_size = strtol(r, (char **)&r, 0); 794 if (x86_stack_size <= 0) 795 usage(); 796 if (*r == 'M') 797 x86_stack_size *= 1024 * 1024; 798 else if (*r == 'k' || *r == 'K') 799 x86_stack_size *= 1024; 800 } else if (!strcmp(r, "L")) { 801 interp_prefix = argv[optind++]; 802 } else if (!strcmp(r, "p")) { 803 qemu_host_page_size = atoi(argv[optind++]); 804 if (qemu_host_page_size == 0 || 805 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) { 806 fprintf(stderr, "page size must be a power of two\n"); 807 exit(1); 808 } 809 } else if (!strcmp(r, "g")) { 810 gdbstub_port = atoi(argv[optind++]); 811 } else if (!strcmp(r, "r")) { 812 qemu_uname_release = argv[optind++]; 813 } else if (!strcmp(r, "cpu")) { 814 cpu_model = argv[optind++]; 815 if (is_help_option(cpu_model)) { 816 /* XXX: implement xxx_cpu_list for targets that still miss it */ 817 #if defined(cpu_list) 818 cpu_list(stdout, &fprintf); 819 #endif 820 exit(1); 821 } 822 } else if (!strcmp(r, "B")) { 823 guest_base = strtol(argv[optind++], NULL, 0); 824 have_guest_base = 1; 825 } else if (!strcmp(r, "drop-ld-preload")) { 826 (void) envlist_unsetenv(envlist, "LD_PRELOAD"); 827 } else if (!strcmp(r, "bsd")) { 828 if (!strcasecmp(argv[optind], "freebsd")) { 829 bsd_type = target_freebsd; 830 } else if (!strcasecmp(argv[optind], "netbsd")) { 831 bsd_type = target_netbsd; 832 } else if (!strcasecmp(argv[optind], "openbsd")) { 833 bsd_type = target_openbsd; 834 } else { 835 usage(); 836 } 837 optind++; 838 } else if (!strcmp(r, "singlestep")) { 839 singlestep = 1; 840 } else if (!strcmp(r, "strace")) { 841 do_strace = 1; 842 } else if (!strcmp(r, "trace")) { 843 g_free(trace_file); 844 trace_file = trace_opt_parse(optarg); 845 } else { 846 usage(); 847 } 848 } 849 850 /* init debug */ 851 qemu_log_needs_buffers(); 852 qemu_set_log_filename(log_file, &error_fatal); 853 if (log_mask) { 854 int mask; 855 856 mask = qemu_str_to_log_mask(log_mask); 857 if (!mask) { 858 qemu_print_log_usage(stdout); 859 exit(1); 860 } 861 qemu_set_log(mask); 862 } 863 864 if (optind >= argc) { 865 usage(); 866 } 867 filename = argv[optind]; 868 869 if (!trace_init_backends()) { 870 exit(1); 871 } 872 trace_init_file(trace_file); 873 874 /* Zero out regs */ 875 memset(regs, 0, sizeof(struct target_pt_regs)); 876 877 /* Zero out image_info */ 878 memset(info, 0, sizeof(struct image_info)); 879 880 /* Scan interp_prefix dir for replacement files. */ 881 init_paths(interp_prefix); 882 883 if (cpu_model == NULL) { 884 #if defined(TARGET_I386) 885 #ifdef TARGET_X86_64 886 cpu_model = "qemu64"; 887 #else 888 cpu_model = "qemu32"; 889 #endif 890 #elif defined(TARGET_SPARC) 891 #ifdef TARGET_SPARC64 892 cpu_model = "TI UltraSparc II"; 893 #else 894 cpu_model = "Fujitsu MB86904"; 895 #endif 896 #else 897 cpu_model = "any"; 898 #endif 899 } 900 tcg_exec_init(0); 901 /* NOTE: we need to init the CPU at this stage to get 902 qemu_host_page_size */ 903 cpu = cpu_init(cpu_model); 904 env = cpu->env_ptr; 905 #if defined(TARGET_SPARC) || defined(TARGET_PPC) 906 cpu_reset(cpu); 907 #endif 908 thread_cpu = cpu; 909 910 if (getenv("QEMU_STRACE")) { 911 do_strace = 1; 912 } 913 914 target_environ = envlist_to_environ(envlist, NULL); 915 envlist_free(envlist); 916 917 /* 918 * Now that page sizes are configured in cpu_init() we can do 919 * proper page alignment for guest_base. 920 */ 921 guest_base = HOST_PAGE_ALIGN(guest_base); 922 923 /* 924 * Read in mmap_min_addr kernel parameter. This value is used 925 * When loading the ELF image to determine whether guest_base 926 * is needed. 927 * 928 * When user has explicitly set the quest base, we skip this 929 * test. 930 */ 931 if (!have_guest_base) { 932 FILE *fp; 933 934 if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) { 935 unsigned long tmp; 936 if (fscanf(fp, "%lu", &tmp) == 1) { 937 mmap_min_addr = tmp; 938 qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n", mmap_min_addr); 939 } 940 fclose(fp); 941 } 942 } 943 944 if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) { 945 printf("Error loading %s\n", filename); 946 _exit(1); 947 } 948 949 for (wrk = target_environ; *wrk; wrk++) { 950 g_free(*wrk); 951 } 952 953 g_free(target_environ); 954 955 if (qemu_loglevel_mask(CPU_LOG_PAGE)) { 956 qemu_log("guest_base 0x%lx\n", guest_base); 957 log_page_dump(); 958 959 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk); 960 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code); 961 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n", 962 info->start_code); 963 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n", 964 info->start_data); 965 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data); 966 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n", 967 info->start_stack); 968 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk); 969 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry); 970 } 971 972 target_set_brk(info->brk); 973 syscall_init(); 974 signal_init(); 975 976 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay 977 generating the prologue until now so that the prologue can take 978 the real value of GUEST_BASE into account. */ 979 tcg_prologue_init(tcg_ctx); 980 tcg_region_init(); 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