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