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