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