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/gdbstub.h"
44 #include "gdbstub/user.h"
45 #include "accel/accel-ops.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 "user/cpu_loop.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 unsigned long opt_tb_size;
75 static const char *argv0;
76 static const char *gdbstub;
77 static envlist_t *envlist;
78 static const char *cpu_model;
79 static const char *cpu_type;
80 static const char *seed_optarg;
81 unsigned long mmap_min_addr;
82 uintptr_t guest_base;
83 bool have_guest_base;
84
85 /*
86 * Used to implement backwards-compatibility for the `-strace`, and
87 * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
88 * -strace, or vice versa.
89 */
90 static bool enable_strace;
91
92 /*
93 * The last log mask given by the user in an environment variable or argument.
94 * Used to support command line arguments overriding environment variables.
95 */
96 static int last_log_mask;
97 static const char *last_log_filename;
98
99 /*
100 * When running 32-on-64 we should make sure we can fit all of the possible
101 * guest address space into a contiguous chunk of virtual host memory.
102 *
103 * This way we will never overlap with our own libraries or binaries or stack
104 * or anything else that QEMU maps.
105 *
106 * Many cpus reserve the high bit (or more than one for some 64-bit cpus)
107 * of the address for the kernel. Some cpus rely on this and user space
108 * uses the high bit(s) for pointer tagging and the like. For them, we
109 * must preserve the expected address space.
110 */
111 #ifndef MAX_RESERVED_VA
112 # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
113 # if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
114 (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
115 # define MAX_RESERVED_VA(CPU) 0xfffffffful
116 # else
117 # define MAX_RESERVED_VA(CPU) ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1)
118 # endif
119 # else
120 # define MAX_RESERVED_VA(CPU) 0
121 # endif
122 #endif
123
124 unsigned long reserved_va;
125 unsigned long guest_addr_max;
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 clone_fork_start();
149 mmap_fork_start();
150 cpu_list_lock();
151 qemu_plugin_user_prefork_lock();
152 gdbserver_fork_start();
153 fd_trans_prefork();
154 }
155
fork_end(pid_t pid)156 void fork_end(pid_t pid)
157 {
158 bool child = pid == 0;
159
160 fd_trans_postfork();
161 qemu_plugin_user_postfork(child);
162 mmap_fork_end(child);
163 if (child) {
164 CPUState *cpu, *next_cpu;
165 /* Child processes created by fork() only have a single thread.
166 Discard information about the parent threads. */
167 CPU_FOREACH_SAFE(cpu, next_cpu) {
168 if (cpu != thread_cpu) {
169 QTAILQ_REMOVE_RCU(&cpus_queue, cpu, node);
170 }
171 }
172 qemu_init_cpu_list();
173 get_task_state(thread_cpu)->ts_tid = qemu_get_thread_id();
174 } else {
175 cpu_list_unlock();
176 }
177 gdbserver_fork_end(thread_cpu, pid);
178 clone_fork_end(child);
179 /*
180 * qemu_init_cpu_list() reinitialized the child exclusive state, but we
181 * also need to keep current_cpu consistent, so call end_exclusive() for
182 * both child and parent.
183 */
184 end_exclusive();
185 }
186
187 __thread CPUState *thread_cpu;
188
qemu_cpu_is_self(CPUState * cpu)189 bool qemu_cpu_is_self(CPUState *cpu)
190 {
191 return thread_cpu == cpu;
192 }
193
task_settid(TaskState * ts)194 void task_settid(TaskState *ts)
195 {
196 if (ts->ts_tid == 0) {
197 ts->ts_tid = (pid_t)syscall(SYS_gettid);
198 }
199 }
200
stop_all_tasks(void)201 void stop_all_tasks(void)
202 {
203 /*
204 * We trust that when using NPTL, start_exclusive()
205 * handles thread stopping correctly.
206 */
207 start_exclusive();
208 }
209
210 /* Assumes contents are already zeroed. */
init_task_state(TaskState * ts)211 void init_task_state(TaskState *ts)
212 {
213 long ticks_per_sec;
214 struct timespec bt;
215
216 ts->used = 1;
217 ts->sigaltstack_used = (struct target_sigaltstack) {
218 .ss_sp = 0,
219 .ss_size = 0,
220 .ss_flags = TARGET_SS_DISABLE,
221 };
222
223 /* Capture task start time relative to system boot */
224
225 ticks_per_sec = sysconf(_SC_CLK_TCK);
226
227 if ((ticks_per_sec > 0) && !clock_gettime(CLOCK_BOOTTIME, &bt)) {
228 /* start_boottime is expressed in clock ticks */
229 ts->start_boottime = bt.tv_sec * (uint64_t) ticks_per_sec;
230 ts->start_boottime += bt.tv_nsec * (uint64_t) ticks_per_sec /
231 NANOSECONDS_PER_SECOND;
232 }
233
234 ts->sys_dispatch_len = -1;
235 }
236
cpu_copy(CPUArchState * env)237 CPUArchState *cpu_copy(CPUArchState *env)
238 {
239 CPUState *cpu = env_cpu(env);
240 CPUState *new_cpu = cpu_create(cpu_type);
241 CPUArchState *new_env = cpu_env(new_cpu);
242 CPUBreakpoint *bp;
243
244 /* Reset non arch specific state */
245 cpu_reset(new_cpu);
246
247 new_cpu->tcg_cflags = cpu->tcg_cflags;
248 memcpy(new_env, env, sizeof(CPUArchState));
249 #if defined(TARGET_I386) || defined(TARGET_X86_64)
250 new_env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
251 PROT_READ | PROT_WRITE,
252 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
253 memcpy(g2h_untagged(new_env->gdt.base), g2h_untagged(env->gdt.base),
254 sizeof(uint64_t) * TARGET_GDT_ENTRIES);
255 OBJECT(new_cpu)->free = OBJECT(cpu)->free;
256 #endif
257
258 /* Clone all break/watchpoints.
259 Note: Once we support ptrace with hw-debug register access, make sure
260 BP_CPU break/watchpoints are handled correctly on clone. */
261 QTAILQ_INIT(&new_cpu->breakpoints);
262 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
263 cpu_breakpoint_insert(new_cpu, bp->pc, bp->flags, NULL);
264 }
265
266 return new_env;
267 }
268
handle_arg_help(const char * arg)269 static void handle_arg_help(const char *arg)
270 {
271 usage(EXIT_SUCCESS);
272 }
273
handle_arg_log(const char * arg)274 static void handle_arg_log(const char *arg)
275 {
276 last_log_mask = qemu_str_to_log_mask(arg);
277 if (!last_log_mask) {
278 qemu_print_log_usage(stdout);
279 exit(EXIT_FAILURE);
280 }
281 }
282
handle_arg_dfilter(const char * arg)283 static void handle_arg_dfilter(const char *arg)
284 {
285 qemu_set_dfilter_ranges(arg, &error_fatal);
286 }
287
handle_arg_log_filename(const char * arg)288 static void handle_arg_log_filename(const char *arg)
289 {
290 last_log_filename = arg;
291 }
292
handle_arg_set_env(const char * arg)293 static void handle_arg_set_env(const char *arg)
294 {
295 char *r, *p, *token;
296 r = p = strdup(arg);
297 while ((token = strsep(&p, ",")) != NULL) {
298 if (envlist_setenv(envlist, token) != 0) {
299 usage(EXIT_FAILURE);
300 }
301 }
302 free(r);
303 }
304
handle_arg_unset_env(const char * arg)305 static void handle_arg_unset_env(const char *arg)
306 {
307 char *r, *p, *token;
308 r = p = strdup(arg);
309 while ((token = strsep(&p, ",")) != NULL) {
310 if (envlist_unsetenv(envlist, token) != 0) {
311 usage(EXIT_FAILURE);
312 }
313 }
314 free(r);
315 }
316
handle_arg_argv0(const char * arg)317 static void handle_arg_argv0(const char *arg)
318 {
319 argv0 = strdup(arg);
320 }
321
handle_arg_stack_size(const char * arg)322 static void handle_arg_stack_size(const char *arg)
323 {
324 char *p;
325 guest_stack_size = strtoul(arg, &p, 0);
326 if (guest_stack_size == 0) {
327 usage(EXIT_FAILURE);
328 }
329
330 if (*p == 'M') {
331 guest_stack_size *= MiB;
332 } else if (*p == 'k' || *p == 'K') {
333 guest_stack_size *= KiB;
334 }
335 }
336
handle_arg_ld_prefix(const char * arg)337 static void handle_arg_ld_prefix(const char *arg)
338 {
339 interp_prefix = strdup(arg);
340 }
341
handle_arg_seed(const char * arg)342 static void handle_arg_seed(const char *arg)
343 {
344 seed_optarg = arg;
345 }
346
handle_arg_gdb(const char * arg)347 static void handle_arg_gdb(const char *arg)
348 {
349 gdbstub = g_strdup(arg);
350 }
351
handle_arg_uname(const char * arg)352 static void handle_arg_uname(const char *arg)
353 {
354 qemu_uname_release = strdup(arg);
355 }
356
handle_arg_cpu(const char * arg)357 static void handle_arg_cpu(const char *arg)
358 {
359 cpu_model = strdup(arg);
360 if (cpu_model == NULL || is_help_option(cpu_model)) {
361 list_cpus();
362 exit(EXIT_FAILURE);
363 }
364 }
365
handle_arg_guest_base(const char * arg)366 static void handle_arg_guest_base(const char *arg)
367 {
368 guest_base = strtol(arg, NULL, 0);
369 have_guest_base = true;
370 }
371
handle_arg_reserved_va(const char * arg)372 static void handle_arg_reserved_va(const char *arg)
373 {
374 char *p;
375 int shift = 0;
376 unsigned long val;
377
378 val = strtoul(arg, &p, 0);
379 switch (*p) {
380 case 'k':
381 case 'K':
382 shift = 10;
383 break;
384 case 'M':
385 shift = 20;
386 break;
387 case 'G':
388 shift = 30;
389 break;
390 }
391 if (shift) {
392 unsigned long unshifted = val;
393 p++;
394 val <<= shift;
395 if (val >> shift != unshifted) {
396 fprintf(stderr, "Reserved virtual address too big\n");
397 exit(EXIT_FAILURE);
398 }
399 }
400 if (*p) {
401 fprintf(stderr, "Unrecognised -R size suffix '%s'\n", p);
402 exit(EXIT_FAILURE);
403 }
404 /* The representation is size - 1, with 0 remaining "default". */
405 reserved_va = val ? val - 1 : 0;
406 }
407
408 static const char *rtsig_map = CONFIG_QEMU_RTSIG_MAP;
409
handle_arg_rtsig_map(const char * arg)410 static void handle_arg_rtsig_map(const char *arg)
411 {
412 rtsig_map = arg;
413 }
414
handle_arg_one_insn_per_tb(const char * arg)415 static void handle_arg_one_insn_per_tb(const char *arg)
416 {
417 opt_one_insn_per_tb = true;
418 }
419
handle_arg_tb_size(const char * arg)420 static void handle_arg_tb_size(const char *arg)
421 {
422 if (qemu_strtoul(arg, NULL, 0, &opt_tb_size)) {
423 usage(EXIT_FAILURE);
424 }
425 }
426
handle_arg_strace(const char * arg)427 static void handle_arg_strace(const char *arg)
428 {
429 enable_strace = true;
430 }
431
handle_arg_version(const char * arg)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
handle_arg_trace(const char * arg)439 static void handle_arg_trace(const char *arg)
440 {
441 trace_opt_parse(arg);
442 }
443
444 #if defined(TARGET_XTENSA)
handle_arg_abi_call0(const char * arg)445 static void handle_arg_abi_call0(const char *arg)
446 {
447 xtensa_set_abi_call0();
448 }
449 #endif
450
handle_arg_perfmap(const char * arg)451 static void handle_arg_perfmap(const char *arg)
452 {
453 perf_enable_perfmap();
454 }
455
handle_arg_jitdump(const char * arg)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
handle_arg_plugin(const char * arg)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 {"one-insn-per-tb",
515 "QEMU_ONE_INSN_PER_TB", false, handle_arg_one_insn_per_tb,
516 "", "run with one guest instruction per emulated TB"},
517 {"tb-size", "QEMU_TB_SIZE", true, handle_arg_tb_size,
518 "size", "TCG translation block cache size"},
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
usage(int exitcode)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
parse_args(int argc,char ** argv)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
main(int argc,char ** argv,char ** envp)684 int main(int argc, char **argv, char **envp)
685 {
686 struct image_info info1, *info = &info1;
687 struct linux_binprm bprm;
688 TaskState *ts;
689 CPUArchState *env;
690 CPUState *cpu;
691 int optind;
692 char **target_environ, **wrk;
693 char **target_argv;
694 int target_argc;
695 int i;
696 int ret;
697 int execfd;
698 int host_page_size;
699 unsigned long max_reserved_va;
700 bool preserve_argv0;
701
702 error_init(argv[0]);
703 module_call_init(MODULE_INIT_TRACE);
704 qemu_init_cpu_list();
705 module_call_init(MODULE_INIT_QOM);
706
707 envlist = envlist_create();
708
709 /*
710 * add current environment into the list
711 * envlist_setenv adds to the front of the list; to preserve environ
712 * order add from back to front
713 */
714 for (wrk = environ; *wrk != NULL; wrk++) {
715 continue;
716 }
717 while (wrk != environ) {
718 wrk--;
719 (void) envlist_setenv(envlist, *wrk);
720 }
721
722 /* Read the stack limit from the kernel. If it's "unlimited",
723 then we can do little else besides use the default. */
724 {
725 struct rlimit lim;
726 if (getrlimit(RLIMIT_STACK, &lim) == 0
727 && lim.rlim_cur != RLIM_INFINITY
728 && lim.rlim_cur == (target_long)lim.rlim_cur
729 && lim.rlim_cur > guest_stack_size) {
730 guest_stack_size = lim.rlim_cur;
731 }
732 }
733
734 cpu_model = NULL;
735
736 qemu_add_opts(&qemu_trace_opts);
737 qemu_plugin_add_opts();
738
739 optind = parse_args(argc, argv);
740
741 qemu_set_log_filename_flags(last_log_filename,
742 last_log_mask | (enable_strace * LOG_STRACE),
743 &error_fatal);
744
745 if (!trace_init_backends()) {
746 exit(1);
747 }
748 trace_init_file();
749 qemu_plugin_load_list(&plugins, &error_fatal);
750
751 /* Zero out image_info */
752 memset(info, 0, sizeof(struct image_info));
753
754 memset(&bprm, 0, sizeof (bprm));
755
756 /* Scan interp_prefix dir for replacement files. */
757 init_paths(interp_prefix);
758
759 init_qemu_uname_release();
760
761 /*
762 * Manage binfmt-misc open-binary flag
763 */
764 errno = 0;
765 execfd = qemu_getauxval(AT_EXECFD);
766 if (errno != 0) {
767 execfd = open(exec_path, O_RDONLY);
768 if (execfd < 0) {
769 printf("Error while loading %s: %s\n", exec_path, strerror(errno));
770 _exit(EXIT_FAILURE);
771 }
772 }
773
774 /* Resolve executable file name to full path name */
775 if (realpath(exec_path, real_exec_path)) {
776 exec_path = real_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 /*
916 * Read in mmap_min_addr kernel parameter. This value is used
917 * When loading the ELF image to determine whether guest_base
918 * is needed. It is also used in mmap_find_vma.
919 */
920 {
921 FILE *fp;
922
923 if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
924 unsigned long tmp;
925 if (fscanf(fp, "%lu", &tmp) == 1 && tmp != 0) {
926 mmap_min_addr = MAX(tmp, host_page_size);
927 qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n",
928 mmap_min_addr);
929 }
930 fclose(fp);
931 }
932 }
933
934 /*
935 * We prefer to not make NULL pointers accessible to QEMU.
936 * If we're in a chroot with no /proc, fall back to 1 page.
937 */
938 if (mmap_min_addr == 0) {
939 mmap_min_addr = host_page_size;
940 qemu_log_mask(CPU_LOG_PAGE,
941 "host mmap_min_addr=0x%lx (fallback)\n",
942 mmap_min_addr);
943 }
944
945 /*
946 * Prepare copy of argv vector for target.
947 */
948 target_argc = argc - optind;
949 target_argv = g_new0(char *, target_argc + 1);
950
951 /*
952 * If argv0 is specified (using '-0' switch) we replace
953 * argv[0] pointer with the given one.
954 */
955 i = 0;
956 if (argv0 != NULL) {
957 target_argv[i++] = strdup(argv0);
958 }
959 for (; i < target_argc; i++) {
960 target_argv[i] = strdup(argv[optind + i]);
961 }
962 target_argv[target_argc] = NULL;
963
964 ts = g_new0(TaskState, 1);
965 init_task_state(ts);
966 /* build Task State */
967 ts->info = info;
968 ts->bprm = &bprm;
969 cpu->opaque = ts;
970 task_settid(ts);
971
972 fd_trans_init();
973
974 ret = loader_exec(execfd, exec_path, target_argv, target_environ,
975 info, &bprm);
976 if (ret != 0) {
977 printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
978 _exit(EXIT_FAILURE);
979 }
980
981 for (wrk = target_environ; *wrk; wrk++) {
982 g_free(*wrk);
983 }
984
985 g_free(target_environ);
986
987 if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
988 FILE *f = qemu_log_trylock();
989 if (f) {
990 fprintf(f, "guest_base %p\n", (void *)guest_base);
991 fprintf(f, "page layout changed following binary load\n");
992 page_dump(f);
993
994 fprintf(f, "end_code 0x" TARGET_ABI_FMT_lx "\n",
995 info->end_code);
996 fprintf(f, "start_code 0x" TARGET_ABI_FMT_lx "\n",
997 info->start_code);
998 fprintf(f, "start_data 0x" TARGET_ABI_FMT_lx "\n",
999 info->start_data);
1000 fprintf(f, "end_data 0x" TARGET_ABI_FMT_lx "\n",
1001 info->end_data);
1002 fprintf(f, "start_stack 0x" TARGET_ABI_FMT_lx "\n",
1003 info->start_stack);
1004 fprintf(f, "brk 0x" TARGET_ABI_FMT_lx "\n",
1005 info->brk);
1006 fprintf(f, "entry 0x" TARGET_ABI_FMT_lx "\n",
1007 info->entry);
1008 fprintf(f, "argv_start 0x" TARGET_ABI_FMT_lx "\n",
1009 info->argv);
1010 fprintf(f, "env_start 0x" TARGET_ABI_FMT_lx "\n",
1011 info->envp);
1012 fprintf(f, "auxv_start 0x" TARGET_ABI_FMT_lx "\n",
1013 info->saved_auxv);
1014 qemu_log_unlock(f);
1015 }
1016 }
1017
1018 target_set_brk(info->brk);
1019 syscall_init();
1020 signal_init(rtsig_map);
1021
1022 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
1023 generating the prologue until now so that the prologue can take
1024 the real value of GUEST_BASE into account. */
1025 tcg_prologue_init();
1026
1027 init_main_thread(cpu, info);
1028
1029 if (gdbstub) {
1030 gdbserver_start(gdbstub, &error_fatal);
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