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