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