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-common.h" 22 #include "qemu/units.h" 23 #include "qemu/accel.h" 24 #include "sysemu/tcg.h" 25 #include "qemu-version.h" 26 #include <sys/syscall.h> 27 #include <sys/resource.h> 28 #include <sys/shm.h> 29 #include <linux/binfmts.h> 30 31 #include "qapi/error.h" 32 #include "qemu.h" 33 #include "qemu/path.h" 34 #include "qemu/queue.h" 35 #include "qemu/config-file.h" 36 #include "qemu/cutils.h" 37 #include "qemu/error-report.h" 38 #include "qemu/help_option.h" 39 #include "qemu/module.h" 40 #include "qemu/plugin.h" 41 #include "exec/exec-all.h" 42 #include "tcg/tcg.h" 43 #include "qemu/timer.h" 44 #include "qemu/envlist.h" 45 #include "qemu/guest-random.h" 46 #include "elf.h" 47 #include "trace/control.h" 48 #include "target_elf.h" 49 #include "cpu_loop-common.h" 50 #include "crypto/init.h" 51 #include "fd-trans.h" 52 53 #ifndef AT_FLAGS_PRESERVE_ARGV0 54 #define AT_FLAGS_PRESERVE_ARGV0_BIT 0 55 #define AT_FLAGS_PRESERVE_ARGV0 (1 << AT_FLAGS_PRESERVE_ARGV0_BIT) 56 #endif 57 58 char *exec_path; 59 60 int singlestep; 61 static const char *argv0; 62 static const char *gdbstub; 63 static envlist_t *envlist; 64 static const char *cpu_model; 65 static const char *cpu_type; 66 static const char *seed_optarg; 67 unsigned long mmap_min_addr; 68 uintptr_t guest_base; 69 bool have_guest_base; 70 71 /* 72 * Used to implement backwards-compatibility for the `-strace`, and 73 * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by 74 * -strace, or vice versa. 75 */ 76 static bool enable_strace; 77 78 /* 79 * The last log mask given by the user in an environment variable or argument. 80 * Used to support command line arguments overriding environment variables. 81 */ 82 static int last_log_mask; 83 84 /* 85 * When running 32-on-64 we should make sure we can fit all of the possible 86 * guest address space into a contiguous chunk of virtual host memory. 87 * 88 * This way we will never overlap with our own libraries or binaries or stack 89 * or anything else that QEMU maps. 90 * 91 * Many cpus reserve the high bit (or more than one for some 64-bit cpus) 92 * of the address for the kernel. Some cpus rely on this and user space 93 * uses the high bit(s) for pointer tagging and the like. For them, we 94 * must preserve the expected address space. 95 */ 96 #ifndef MAX_RESERVED_VA 97 # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS 98 # if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \ 99 (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32)) 100 /* There are a number of places where we assign reserved_va to a variable 101 of type abi_ulong and expect it to fit. Avoid the last page. */ 102 # define MAX_RESERVED_VA(CPU) (0xfffffffful & TARGET_PAGE_MASK) 103 # else 104 # define MAX_RESERVED_VA(CPU) (1ul << TARGET_VIRT_ADDR_SPACE_BITS) 105 # endif 106 # else 107 # define MAX_RESERVED_VA(CPU) 0 108 # endif 109 #endif 110 111 unsigned long reserved_va; 112 113 static void usage(int exitcode); 114 115 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX; 116 const char *qemu_uname_release; 117 118 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so 119 we allocate a bigger stack. Need a better solution, for example 120 by remapping the process stack directly at the right place */ 121 unsigned long guest_stack_size = 8 * 1024 * 1024UL; 122 123 /***********************************************************/ 124 /* Helper routines for implementing atomic operations. */ 125 126 /* Make sure everything is in a consistent state for calling fork(). */ 127 void fork_start(void) 128 { 129 start_exclusive(); 130 mmap_fork_start(); 131 cpu_list_lock(); 132 } 133 134 void fork_end(int child) 135 { 136 mmap_fork_end(child); 137 if (child) { 138 CPUState *cpu, *next_cpu; 139 /* Child processes created by fork() only have a single thread. 140 Discard information about the parent threads. */ 141 CPU_FOREACH_SAFE(cpu, next_cpu) { 142 if (cpu != thread_cpu) { 143 QTAILQ_REMOVE_RCU(&cpus, cpu, node); 144 } 145 } 146 qemu_init_cpu_list(); 147 gdbserver_fork(thread_cpu); 148 /* qemu_init_cpu_list() takes care of reinitializing the 149 * exclusive state, so we don't need to end_exclusive() here. 150 */ 151 } else { 152 cpu_list_unlock(); 153 end_exclusive(); 154 } 155 } 156 157 __thread CPUState *thread_cpu; 158 159 bool qemu_cpu_is_self(CPUState *cpu) 160 { 161 return thread_cpu == cpu; 162 } 163 164 void qemu_cpu_kick(CPUState *cpu) 165 { 166 cpu_exit(cpu); 167 } 168 169 void task_settid(TaskState *ts) 170 { 171 if (ts->ts_tid == 0) { 172 ts->ts_tid = (pid_t)syscall(SYS_gettid); 173 } 174 } 175 176 void stop_all_tasks(void) 177 { 178 /* 179 * We trust that when using NPTL, start_exclusive() 180 * handles thread stopping correctly. 181 */ 182 start_exclusive(); 183 } 184 185 /* Assumes contents are already zeroed. */ 186 void init_task_state(TaskState *ts) 187 { 188 ts->used = 1; 189 ts->sigaltstack_used = (struct target_sigaltstack) { 190 .ss_sp = 0, 191 .ss_size = 0, 192 .ss_flags = TARGET_SS_DISABLE, 193 }; 194 } 195 196 CPUArchState *cpu_copy(CPUArchState *env) 197 { 198 CPUState *cpu = env_cpu(env); 199 CPUState *new_cpu = cpu_create(cpu_type); 200 CPUArchState *new_env = new_cpu->env_ptr; 201 CPUBreakpoint *bp; 202 203 /* Reset non arch specific state */ 204 cpu_reset(new_cpu); 205 206 new_cpu->tcg_cflags = cpu->tcg_cflags; 207 memcpy(new_env, env, sizeof(CPUArchState)); 208 209 /* Clone all break/watchpoints. 210 Note: Once we support ptrace with hw-debug register access, make sure 211 BP_CPU break/watchpoints are handled correctly on clone. */ 212 QTAILQ_INIT(&new_cpu->breakpoints); 213 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) { 214 cpu_breakpoint_insert(new_cpu, bp->pc, bp->flags, NULL); 215 } 216 217 return new_env; 218 } 219 220 static void handle_arg_help(const char *arg) 221 { 222 usage(EXIT_SUCCESS); 223 } 224 225 static void handle_arg_log(const char *arg) 226 { 227 last_log_mask = qemu_str_to_log_mask(arg); 228 if (!last_log_mask) { 229 qemu_print_log_usage(stdout); 230 exit(EXIT_FAILURE); 231 } 232 } 233 234 static void handle_arg_dfilter(const char *arg) 235 { 236 qemu_set_dfilter_ranges(arg, &error_fatal); 237 } 238 239 static void handle_arg_log_filename(const char *arg) 240 { 241 qemu_set_log_filename(arg, &error_fatal); 242 } 243 244 static void handle_arg_set_env(const char *arg) 245 { 246 char *r, *p, *token; 247 r = p = strdup(arg); 248 while ((token = strsep(&p, ",")) != NULL) { 249 if (envlist_setenv(envlist, token) != 0) { 250 usage(EXIT_FAILURE); 251 } 252 } 253 free(r); 254 } 255 256 static void handle_arg_unset_env(const char *arg) 257 { 258 char *r, *p, *token; 259 r = p = strdup(arg); 260 while ((token = strsep(&p, ",")) != NULL) { 261 if (envlist_unsetenv(envlist, token) != 0) { 262 usage(EXIT_FAILURE); 263 } 264 } 265 free(r); 266 } 267 268 static void handle_arg_argv0(const char *arg) 269 { 270 argv0 = strdup(arg); 271 } 272 273 static void handle_arg_stack_size(const char *arg) 274 { 275 char *p; 276 guest_stack_size = strtoul(arg, &p, 0); 277 if (guest_stack_size == 0) { 278 usage(EXIT_FAILURE); 279 } 280 281 if (*p == 'M') { 282 guest_stack_size *= MiB; 283 } else if (*p == 'k' || *p == 'K') { 284 guest_stack_size *= KiB; 285 } 286 } 287 288 static void handle_arg_ld_prefix(const char *arg) 289 { 290 interp_prefix = strdup(arg); 291 } 292 293 static void handle_arg_pagesize(const char *arg) 294 { 295 qemu_host_page_size = atoi(arg); 296 if (qemu_host_page_size == 0 || 297 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) { 298 fprintf(stderr, "page size must be a power of two\n"); 299 exit(EXIT_FAILURE); 300 } 301 } 302 303 static void handle_arg_seed(const char *arg) 304 { 305 seed_optarg = arg; 306 } 307 308 static void handle_arg_gdb(const char *arg) 309 { 310 gdbstub = g_strdup(arg); 311 } 312 313 static void handle_arg_uname(const char *arg) 314 { 315 qemu_uname_release = strdup(arg); 316 } 317 318 static void handle_arg_cpu(const char *arg) 319 { 320 cpu_model = strdup(arg); 321 if (cpu_model == NULL || is_help_option(cpu_model)) { 322 /* XXX: implement xxx_cpu_list for targets that still miss it */ 323 #if defined(cpu_list) 324 cpu_list(); 325 #endif 326 exit(EXIT_FAILURE); 327 } 328 } 329 330 static void handle_arg_guest_base(const char *arg) 331 { 332 guest_base = strtol(arg, NULL, 0); 333 have_guest_base = true; 334 } 335 336 static void handle_arg_reserved_va(const char *arg) 337 { 338 char *p; 339 int shift = 0; 340 reserved_va = strtoul(arg, &p, 0); 341 switch (*p) { 342 case 'k': 343 case 'K': 344 shift = 10; 345 break; 346 case 'M': 347 shift = 20; 348 break; 349 case 'G': 350 shift = 30; 351 break; 352 } 353 if (shift) { 354 unsigned long unshifted = reserved_va; 355 p++; 356 reserved_va <<= shift; 357 if (reserved_va >> shift != unshifted) { 358 fprintf(stderr, "Reserved virtual address too big\n"); 359 exit(EXIT_FAILURE); 360 } 361 } 362 if (*p) { 363 fprintf(stderr, "Unrecognised -R size suffix '%s'\n", p); 364 exit(EXIT_FAILURE); 365 } 366 } 367 368 static void handle_arg_singlestep(const char *arg) 369 { 370 singlestep = 1; 371 } 372 373 static void handle_arg_strace(const char *arg) 374 { 375 enable_strace = true; 376 } 377 378 static void handle_arg_version(const char *arg) 379 { 380 printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION 381 "\n" QEMU_COPYRIGHT "\n"); 382 exit(EXIT_SUCCESS); 383 } 384 385 static void handle_arg_trace(const char *arg) 386 { 387 trace_opt_parse(arg); 388 } 389 390 #if defined(TARGET_XTENSA) 391 static void handle_arg_abi_call0(const char *arg) 392 { 393 xtensa_set_abi_call0(); 394 } 395 #endif 396 397 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins); 398 399 #ifdef CONFIG_PLUGIN 400 static void handle_arg_plugin(const char *arg) 401 { 402 qemu_plugin_opt_parse(arg, &plugins); 403 } 404 #endif 405 406 struct qemu_argument { 407 const char *argv; 408 const char *env; 409 bool has_arg; 410 void (*handle_opt)(const char *arg); 411 const char *example; 412 const char *help; 413 }; 414 415 static const struct qemu_argument arg_table[] = { 416 {"h", "", false, handle_arg_help, 417 "", "print this help"}, 418 {"help", "", false, handle_arg_help, 419 "", ""}, 420 {"g", "QEMU_GDB", true, handle_arg_gdb, 421 "port", "wait gdb connection to 'port'"}, 422 {"L", "QEMU_LD_PREFIX", true, handle_arg_ld_prefix, 423 "path", "set the elf interpreter prefix to 'path'"}, 424 {"s", "QEMU_STACK_SIZE", true, handle_arg_stack_size, 425 "size", "set the stack size to 'size' bytes"}, 426 {"cpu", "QEMU_CPU", true, handle_arg_cpu, 427 "model", "select CPU (-cpu help for list)"}, 428 {"E", "QEMU_SET_ENV", true, handle_arg_set_env, 429 "var=value", "sets targets environment variable (see below)"}, 430 {"U", "QEMU_UNSET_ENV", true, handle_arg_unset_env, 431 "var", "unsets targets environment variable (see below)"}, 432 {"0", "QEMU_ARGV0", true, handle_arg_argv0, 433 "argv0", "forces target process argv[0] to be 'argv0'"}, 434 {"r", "QEMU_UNAME", true, handle_arg_uname, 435 "uname", "set qemu uname release string to 'uname'"}, 436 {"B", "QEMU_GUEST_BASE", true, handle_arg_guest_base, 437 "address", "set guest_base address to 'address'"}, 438 {"R", "QEMU_RESERVED_VA", true, handle_arg_reserved_va, 439 "size", "reserve 'size' bytes for guest virtual address space"}, 440 {"d", "QEMU_LOG", true, handle_arg_log, 441 "item[,...]", "enable logging of specified items " 442 "(use '-d help' for a list of items)"}, 443 {"dfilter", "QEMU_DFILTER", true, handle_arg_dfilter, 444 "range[,...]","filter logging based on address range"}, 445 {"D", "QEMU_LOG_FILENAME", true, handle_arg_log_filename, 446 "logfile", "write logs to 'logfile' (default stderr)"}, 447 {"p", "QEMU_PAGESIZE", true, handle_arg_pagesize, 448 "pagesize", "set the host page size to 'pagesize'"}, 449 {"singlestep", "QEMU_SINGLESTEP", false, handle_arg_singlestep, 450 "", "run in singlestep mode"}, 451 {"strace", "QEMU_STRACE", false, handle_arg_strace, 452 "", "log system calls"}, 453 {"seed", "QEMU_RAND_SEED", true, handle_arg_seed, 454 "", "Seed for pseudo-random number generator"}, 455 {"trace", "QEMU_TRACE", true, handle_arg_trace, 456 "", "[[enable=]<pattern>][,events=<file>][,file=<file>]"}, 457 #ifdef CONFIG_PLUGIN 458 {"plugin", "QEMU_PLUGIN", true, handle_arg_plugin, 459 "", "[file=]<file>[,<argname>=<argvalue>]"}, 460 #endif 461 {"version", "QEMU_VERSION", false, handle_arg_version, 462 "", "display version information and exit"}, 463 #if defined(TARGET_XTENSA) 464 {"xtensa-abi-call0", "QEMU_XTENSA_ABI_CALL0", false, handle_arg_abi_call0, 465 "", "assume CALL0 Xtensa ABI"}, 466 #endif 467 {NULL, NULL, false, NULL, NULL, NULL} 468 }; 469 470 static void usage(int exitcode) 471 { 472 const struct qemu_argument *arginfo; 473 int maxarglen; 474 int maxenvlen; 475 476 printf("usage: qemu-" TARGET_NAME " [options] program [arguments...]\n" 477 "Linux CPU emulator (compiled for " TARGET_NAME " emulation)\n" 478 "\n" 479 "Options and associated environment variables:\n" 480 "\n"); 481 482 /* Calculate column widths. We must always have at least enough space 483 * for the column header. 484 */ 485 maxarglen = strlen("Argument"); 486 maxenvlen = strlen("Env-variable"); 487 488 for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) { 489 int arglen = strlen(arginfo->argv); 490 if (arginfo->has_arg) { 491 arglen += strlen(arginfo->example) + 1; 492 } 493 if (strlen(arginfo->env) > maxenvlen) { 494 maxenvlen = strlen(arginfo->env); 495 } 496 if (arglen > maxarglen) { 497 maxarglen = arglen; 498 } 499 } 500 501 printf("%-*s %-*s Description\n", maxarglen+1, "Argument", 502 maxenvlen, "Env-variable"); 503 504 for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) { 505 if (arginfo->has_arg) { 506 printf("-%s %-*s %-*s %s\n", arginfo->argv, 507 (int)(maxarglen - strlen(arginfo->argv) - 1), 508 arginfo->example, maxenvlen, arginfo->env, arginfo->help); 509 } else { 510 printf("-%-*s %-*s %s\n", maxarglen, arginfo->argv, 511 maxenvlen, arginfo->env, 512 arginfo->help); 513 } 514 } 515 516 printf("\n" 517 "Defaults:\n" 518 "QEMU_LD_PREFIX = %s\n" 519 "QEMU_STACK_SIZE = %ld byte\n", 520 interp_prefix, 521 guest_stack_size); 522 523 printf("\n" 524 "You can use -E and -U options or the QEMU_SET_ENV and\n" 525 "QEMU_UNSET_ENV environment variables to set and unset\n" 526 "environment variables for the target process.\n" 527 "It is possible to provide several variables by separating them\n" 528 "by commas in getsubopt(3) style. Additionally it is possible to\n" 529 "provide the -E and -U options multiple times.\n" 530 "The following lines are equivalent:\n" 531 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n" 532 " -E var1=val2,var2=val2 -U LD_PRELOAD,LD_DEBUG\n" 533 " QEMU_SET_ENV=var1=val2,var2=val2 QEMU_UNSET_ENV=LD_PRELOAD,LD_DEBUG\n" 534 "Note that if you provide several changes to a single variable\n" 535 "the last change will stay in effect.\n" 536 "\n" 537 QEMU_HELP_BOTTOM "\n"); 538 539 exit(exitcode); 540 } 541 542 static int parse_args(int argc, char **argv) 543 { 544 const char *r; 545 int optind; 546 const struct qemu_argument *arginfo; 547 548 for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) { 549 if (arginfo->env == NULL) { 550 continue; 551 } 552 553 r = getenv(arginfo->env); 554 if (r != NULL) { 555 arginfo->handle_opt(r); 556 } 557 } 558 559 optind = 1; 560 for (;;) { 561 if (optind >= argc) { 562 break; 563 } 564 r = argv[optind]; 565 if (r[0] != '-') { 566 break; 567 } 568 optind++; 569 r++; 570 if (!strcmp(r, "-")) { 571 break; 572 } 573 /* Treat --foo the same as -foo. */ 574 if (r[0] == '-') { 575 r++; 576 } 577 578 for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) { 579 if (!strcmp(r, arginfo->argv)) { 580 if (arginfo->has_arg) { 581 if (optind >= argc) { 582 (void) fprintf(stderr, 583 "qemu: missing argument for option '%s'\n", r); 584 exit(EXIT_FAILURE); 585 } 586 arginfo->handle_opt(argv[optind]); 587 optind++; 588 } else { 589 arginfo->handle_opt(NULL); 590 } 591 break; 592 } 593 } 594 595 /* no option matched the current argv */ 596 if (arginfo->handle_opt == NULL) { 597 (void) fprintf(stderr, "qemu: unknown option '%s'\n", r); 598 exit(EXIT_FAILURE); 599 } 600 } 601 602 if (optind >= argc) { 603 (void) fprintf(stderr, "qemu: no user program specified\n"); 604 exit(EXIT_FAILURE); 605 } 606 607 exec_path = argv[optind]; 608 609 return optind; 610 } 611 612 int main(int argc, char **argv, char **envp) 613 { 614 struct target_pt_regs regs1, *regs = ®s1; 615 struct image_info info1, *info = &info1; 616 struct linux_binprm bprm; 617 TaskState *ts; 618 CPUArchState *env; 619 CPUState *cpu; 620 int optind; 621 char **target_environ, **wrk; 622 char **target_argv; 623 int target_argc; 624 int i; 625 int ret; 626 int execfd; 627 int log_mask; 628 unsigned long max_reserved_va; 629 bool preserve_argv0; 630 631 error_init(argv[0]); 632 module_call_init(MODULE_INIT_TRACE); 633 qemu_init_cpu_list(); 634 module_call_init(MODULE_INIT_QOM); 635 636 envlist = envlist_create(); 637 638 /* add current environment into the list */ 639 for (wrk = environ; *wrk != NULL; wrk++) { 640 (void) envlist_setenv(envlist, *wrk); 641 } 642 643 /* Read the stack limit from the kernel. If it's "unlimited", 644 then we can do little else besides use the default. */ 645 { 646 struct rlimit lim; 647 if (getrlimit(RLIMIT_STACK, &lim) == 0 648 && lim.rlim_cur != RLIM_INFINITY 649 && lim.rlim_cur == (target_long)lim.rlim_cur) { 650 guest_stack_size = lim.rlim_cur; 651 } 652 } 653 654 cpu_model = NULL; 655 656 qemu_add_opts(&qemu_trace_opts); 657 qemu_plugin_add_opts(); 658 659 optind = parse_args(argc, argv); 660 661 log_mask = last_log_mask | (enable_strace ? LOG_STRACE : 0); 662 if (log_mask) { 663 qemu_log_needs_buffers(); 664 qemu_set_log(log_mask); 665 } 666 667 if (!trace_init_backends()) { 668 exit(1); 669 } 670 trace_init_file(); 671 qemu_plugin_load_list(&plugins, &error_fatal); 672 673 /* Zero out regs */ 674 memset(regs, 0, sizeof(struct target_pt_regs)); 675 676 /* Zero out image_info */ 677 memset(info, 0, sizeof(struct image_info)); 678 679 memset(&bprm, 0, sizeof (bprm)); 680 681 /* Scan interp_prefix dir for replacement files. */ 682 init_paths(interp_prefix); 683 684 init_qemu_uname_release(); 685 686 /* 687 * Manage binfmt-misc open-binary flag 688 */ 689 execfd = qemu_getauxval(AT_EXECFD); 690 if (execfd == 0) { 691 execfd = open(exec_path, O_RDONLY); 692 if (execfd < 0) { 693 printf("Error while loading %s: %s\n", exec_path, strerror(errno)); 694 _exit(EXIT_FAILURE); 695 } 696 } 697 698 /* 699 * get binfmt_misc flags 700 */ 701 preserve_argv0 = !!(qemu_getauxval(AT_FLAGS) & AT_FLAGS_PRESERVE_ARGV0); 702 703 /* 704 * Manage binfmt-misc preserve-arg[0] flag 705 * argv[optind] full path to the binary 706 * argv[optind + 1] original argv[0] 707 */ 708 if (optind + 1 < argc && preserve_argv0) { 709 optind++; 710 } 711 712 if (cpu_model == NULL) { 713 cpu_model = cpu_get_model(get_elf_eflags(execfd)); 714 } 715 cpu_type = parse_cpu_option(cpu_model); 716 717 /* init tcg before creating CPUs and to get qemu_host_page_size */ 718 { 719 AccelClass *ac = ACCEL_GET_CLASS(current_accel()); 720 721 accel_init_interfaces(ac); 722 ac->init_machine(NULL); 723 } 724 cpu = cpu_create(cpu_type); 725 env = cpu->env_ptr; 726 cpu_reset(cpu); 727 thread_cpu = cpu; 728 729 /* 730 * Reserving too much vm space via mmap can run into problems 731 * with rlimits, oom due to page table creation, etc. We will 732 * still try it, if directed by the command-line option, but 733 * not by default. 734 */ 735 max_reserved_va = MAX_RESERVED_VA(cpu); 736 if (reserved_va != 0) { 737 if (max_reserved_va && reserved_va > max_reserved_va) { 738 fprintf(stderr, "Reserved virtual address too big\n"); 739 exit(EXIT_FAILURE); 740 } 741 } else if (HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32) { 742 /* 743 * reserved_va must be aligned with the host page size 744 * as it is used with mmap() 745 */ 746 reserved_va = max_reserved_va & qemu_host_page_mask; 747 } 748 749 { 750 Error *err = NULL; 751 if (seed_optarg != NULL) { 752 qemu_guest_random_seed_main(seed_optarg, &err); 753 } else { 754 qcrypto_init(&err); 755 } 756 if (err) { 757 error_reportf_err(err, "cannot initialize crypto: "); 758 exit(1); 759 } 760 } 761 762 target_environ = envlist_to_environ(envlist, NULL); 763 envlist_free(envlist); 764 765 /* 766 * Read in mmap_min_addr kernel parameter. This value is used 767 * When loading the ELF image to determine whether guest_base 768 * is needed. It is also used in mmap_find_vma. 769 */ 770 { 771 FILE *fp; 772 773 if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) { 774 unsigned long tmp; 775 if (fscanf(fp, "%lu", &tmp) == 1 && tmp != 0) { 776 mmap_min_addr = tmp; 777 qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n", 778 mmap_min_addr); 779 } 780 fclose(fp); 781 } 782 } 783 784 /* 785 * We prefer to not make NULL pointers accessible to QEMU. 786 * If we're in a chroot with no /proc, fall back to 1 page. 787 */ 788 if (mmap_min_addr == 0) { 789 mmap_min_addr = qemu_host_page_size; 790 qemu_log_mask(CPU_LOG_PAGE, 791 "host mmap_min_addr=0x%lx (fallback)\n", 792 mmap_min_addr); 793 } 794 795 /* 796 * Prepare copy of argv vector for target. 797 */ 798 target_argc = argc - optind; 799 target_argv = calloc(target_argc + 1, sizeof (char *)); 800 if (target_argv == NULL) { 801 (void) fprintf(stderr, "Unable to allocate memory for target_argv\n"); 802 exit(EXIT_FAILURE); 803 } 804 805 /* 806 * If argv0 is specified (using '-0' switch) we replace 807 * argv[0] pointer with the given one. 808 */ 809 i = 0; 810 if (argv0 != NULL) { 811 target_argv[i++] = strdup(argv0); 812 } 813 for (; i < target_argc; i++) { 814 target_argv[i] = strdup(argv[optind + i]); 815 } 816 target_argv[target_argc] = NULL; 817 818 ts = g_new0(TaskState, 1); 819 init_task_state(ts); 820 /* build Task State */ 821 ts->info = info; 822 ts->bprm = &bprm; 823 cpu->opaque = ts; 824 task_settid(ts); 825 826 fd_trans_init(); 827 828 ret = loader_exec(execfd, exec_path, target_argv, target_environ, regs, 829 info, &bprm); 830 if (ret != 0) { 831 printf("Error while loading %s: %s\n", exec_path, strerror(-ret)); 832 _exit(EXIT_FAILURE); 833 } 834 835 for (wrk = target_environ; *wrk; wrk++) { 836 g_free(*wrk); 837 } 838 839 g_free(target_environ); 840 841 if (qemu_loglevel_mask(CPU_LOG_PAGE)) { 842 qemu_log("guest_base %p\n", (void *)guest_base); 843 log_page_dump("binary load"); 844 845 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk); 846 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code); 847 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n", info->start_code); 848 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n", info->start_data); 849 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data); 850 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n", info->start_stack); 851 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk); 852 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry); 853 qemu_log("argv_start 0x" TARGET_ABI_FMT_lx "\n", info->arg_start); 854 qemu_log("env_start 0x" TARGET_ABI_FMT_lx "\n", 855 info->arg_end + (abi_ulong)sizeof(abi_ulong)); 856 qemu_log("auxv_start 0x" TARGET_ABI_FMT_lx "\n", info->saved_auxv); 857 } 858 859 target_set_brk(info->brk); 860 syscall_init(); 861 signal_init(); 862 863 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay 864 generating the prologue until now so that the prologue can take 865 the real value of GUEST_BASE into account. */ 866 tcg_prologue_init(tcg_ctx); 867 868 target_cpu_copy_regs(env, regs); 869 870 if (gdbstub) { 871 if (gdbserver_start(gdbstub) < 0) { 872 fprintf(stderr, "qemu: could not open gdbserver on %s\n", 873 gdbstub); 874 exit(EXIT_FAILURE); 875 } 876 gdb_handlesig(cpu, 0); 877 } 878 cpu_loop(env); 879 /* never exits */ 880 return 0; 881 } 882