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