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