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