xref: /openbmc/qemu/linux-user/main.c (revision 2113aed687cb0b84ad512c440c1edf6eea8fcde2)
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-common.h"
22 #include "qemu/units.h"
23 #include "qemu/accel.h"
24 #include "sysemu/tcg.h"
25 #include "qemu-version.h"
26 #include <sys/syscall.h>
27 #include <sys/resource.h>
28 #include <sys/shm.h>
29 #include <linux/binfmts.h>
30 
31 #include "qapi/error.h"
32 #include "qemu.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 "exec/exec-all.h"
42 #include "tcg/tcg.h"
43 #include "qemu/timer.h"
44 #include "qemu/envlist.h"
45 #include "qemu/guest-random.h"
46 #include "elf.h"
47 #include "trace/control.h"
48 #include "target_elf.h"
49 #include "cpu_loop-common.h"
50 #include "crypto/init.h"
51 #include "fd-trans.h"
52 #include "signal-common.h"
53 
54 #ifndef AT_FLAGS_PRESERVE_ARGV0
55 #define AT_FLAGS_PRESERVE_ARGV0_BIT 0
56 #define AT_FLAGS_PRESERVE_ARGV0 (1 << AT_FLAGS_PRESERVE_ARGV0_BIT)
57 #endif
58 
59 char *exec_path;
60 
61 int singlestep;
62 static const char *argv0;
63 static const char *gdbstub;
64 static envlist_t *envlist;
65 static const char *cpu_model;
66 static const char *cpu_type;
67 static const char *seed_optarg;
68 unsigned long mmap_min_addr;
69 uintptr_t guest_base;
70 bool have_guest_base;
71 
72 /*
73  * Used to implement backwards-compatibility for the `-strace`, and
74  * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
75  * -strace, or vice versa.
76  */
77 static bool enable_strace;
78 
79 /*
80  * The last log mask given by the user in an environment variable or argument.
81  * Used to support command line arguments overriding environment variables.
82  */
83 static int last_log_mask;
84 
85 /*
86  * When running 32-on-64 we should make sure we can fit all of the possible
87  * guest address space into a contiguous chunk of virtual host memory.
88  *
89  * This way we will never overlap with our own libraries or binaries or stack
90  * or anything else that QEMU maps.
91  *
92  * Many cpus reserve the high bit (or more than one for some 64-bit cpus)
93  * of the address for the kernel.  Some cpus rely on this and user space
94  * uses the high bit(s) for pointer tagging and the like.  For them, we
95  * must preserve the expected address space.
96  */
97 #ifndef MAX_RESERVED_VA
98 # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
99 #  if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
100       (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
101 /* There are a number of places where we assign reserved_va to a variable
102    of type abi_ulong and expect it to fit.  Avoid the last page.  */
103 #   define MAX_RESERVED_VA(CPU)  (0xfffffffful & TARGET_PAGE_MASK)
104 #  else
105 #   define MAX_RESERVED_VA(CPU)  (1ul << TARGET_VIRT_ADDR_SPACE_BITS)
106 #  endif
107 # else
108 #  define MAX_RESERVED_VA(CPU)  0
109 # endif
110 #endif
111 
112 unsigned long reserved_va;
113 
114 static void usage(int exitcode);
115 
116 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
117 const char *qemu_uname_release;
118 
119 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
120    we allocate a bigger stack. Need a better solution, for example
121    by remapping the process stack directly at the right place */
122 unsigned long guest_stack_size = 8 * 1024 * 1024UL;
123 
124 #if defined(TARGET_I386)
125 int cpu_get_pic_interrupt(CPUX86State *env)
126 {
127     return -1;
128 }
129 #endif
130 
131 /***********************************************************/
132 /* Helper routines for implementing atomic operations.  */
133 
134 /* Make sure everything is in a consistent state for calling fork().  */
135 void fork_start(void)
136 {
137     start_exclusive();
138     mmap_fork_start();
139     cpu_list_lock();
140 }
141 
142 void fork_end(int child)
143 {
144     mmap_fork_end(child);
145     if (child) {
146         CPUState *cpu, *next_cpu;
147         /* Child processes created by fork() only have a single thread.
148            Discard information about the parent threads.  */
149         CPU_FOREACH_SAFE(cpu, next_cpu) {
150             if (cpu != thread_cpu) {
151                 QTAILQ_REMOVE_RCU(&cpus, cpu, node);
152             }
153         }
154         qemu_init_cpu_list();
155         gdbserver_fork(thread_cpu);
156         /* qemu_init_cpu_list() takes care of reinitializing the
157          * exclusive state, so we don't need to end_exclusive() here.
158          */
159     } else {
160         cpu_list_unlock();
161         end_exclusive();
162     }
163 }
164 
165 __thread CPUState *thread_cpu;
166 
167 bool qemu_cpu_is_self(CPUState *cpu)
168 {
169     return thread_cpu == cpu;
170 }
171 
172 void qemu_cpu_kick(CPUState *cpu)
173 {
174     cpu_exit(cpu);
175 }
176 
177 void task_settid(TaskState *ts)
178 {
179     if (ts->ts_tid == 0) {
180         ts->ts_tid = (pid_t)syscall(SYS_gettid);
181     }
182 }
183 
184 void stop_all_tasks(void)
185 {
186     /*
187      * We trust that when using NPTL, start_exclusive()
188      * handles thread stopping correctly.
189      */
190     start_exclusive();
191 }
192 
193 /* Assumes contents are already zeroed.  */
194 void init_task_state(TaskState *ts)
195 {
196     ts->used = 1;
197     ts->sigaltstack_used = (struct target_sigaltstack) {
198         .ss_sp = 0,
199         .ss_size = 0,
200         .ss_flags = TARGET_SS_DISABLE,
201     };
202 }
203 
204 CPUArchState *cpu_copy(CPUArchState *env)
205 {
206     CPUState *cpu = env_cpu(env);
207     CPUState *new_cpu = cpu_create(cpu_type);
208     CPUArchState *new_env = new_cpu->env_ptr;
209     CPUBreakpoint *bp;
210 
211     /* Reset non arch specific state */
212     cpu_reset(new_cpu);
213 
214     new_cpu->tcg_cflags = cpu->tcg_cflags;
215     memcpy(new_env, env, sizeof(CPUArchState));
216 
217     /* Clone all break/watchpoints.
218        Note: Once we support ptrace with hw-debug register access, make sure
219        BP_CPU break/watchpoints are handled correctly on clone. */
220     QTAILQ_INIT(&new_cpu->breakpoints);
221     QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
222         cpu_breakpoint_insert(new_cpu, bp->pc, bp->flags, NULL);
223     }
224 
225     return new_env;
226 }
227 
228 static void handle_arg_help(const char *arg)
229 {
230     usage(EXIT_SUCCESS);
231 }
232 
233 static void handle_arg_log(const char *arg)
234 {
235     last_log_mask = qemu_str_to_log_mask(arg);
236     if (!last_log_mask) {
237         qemu_print_log_usage(stdout);
238         exit(EXIT_FAILURE);
239     }
240 }
241 
242 static void handle_arg_dfilter(const char *arg)
243 {
244     qemu_set_dfilter_ranges(arg, &error_fatal);
245 }
246 
247 static void handle_arg_log_filename(const char *arg)
248 {
249     qemu_set_log_filename(arg, &error_fatal);
250 }
251 
252 static void handle_arg_set_env(const char *arg)
253 {
254     char *r, *p, *token;
255     r = p = strdup(arg);
256     while ((token = strsep(&p, ",")) != NULL) {
257         if (envlist_setenv(envlist, token) != 0) {
258             usage(EXIT_FAILURE);
259         }
260     }
261     free(r);
262 }
263 
264 static void handle_arg_unset_env(const char *arg)
265 {
266     char *r, *p, *token;
267     r = p = strdup(arg);
268     while ((token = strsep(&p, ",")) != NULL) {
269         if (envlist_unsetenv(envlist, token) != 0) {
270             usage(EXIT_FAILURE);
271         }
272     }
273     free(r);
274 }
275 
276 static void handle_arg_argv0(const char *arg)
277 {
278     argv0 = strdup(arg);
279 }
280 
281 static void handle_arg_stack_size(const char *arg)
282 {
283     char *p;
284     guest_stack_size = strtoul(arg, &p, 0);
285     if (guest_stack_size == 0) {
286         usage(EXIT_FAILURE);
287     }
288 
289     if (*p == 'M') {
290         guest_stack_size *= MiB;
291     } else if (*p == 'k' || *p == 'K') {
292         guest_stack_size *= KiB;
293     }
294 }
295 
296 static void handle_arg_ld_prefix(const char *arg)
297 {
298     interp_prefix = strdup(arg);
299 }
300 
301 static void handle_arg_pagesize(const char *arg)
302 {
303     qemu_host_page_size = atoi(arg);
304     if (qemu_host_page_size == 0 ||
305         (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
306         fprintf(stderr, "page size must be a power of two\n");
307         exit(EXIT_FAILURE);
308     }
309 }
310 
311 static void handle_arg_seed(const char *arg)
312 {
313     seed_optarg = arg;
314 }
315 
316 static void handle_arg_gdb(const char *arg)
317 {
318     gdbstub = g_strdup(arg);
319 }
320 
321 static void handle_arg_uname(const char *arg)
322 {
323     qemu_uname_release = strdup(arg);
324 }
325 
326 static void handle_arg_cpu(const char *arg)
327 {
328     cpu_model = strdup(arg);
329     if (cpu_model == NULL || is_help_option(cpu_model)) {
330         /* XXX: implement xxx_cpu_list for targets that still miss it */
331 #if defined(cpu_list)
332         cpu_list();
333 #endif
334         exit(EXIT_FAILURE);
335     }
336 }
337 
338 static void handle_arg_guest_base(const char *arg)
339 {
340     guest_base = strtol(arg, NULL, 0);
341     have_guest_base = true;
342 }
343 
344 static void handle_arg_reserved_va(const char *arg)
345 {
346     char *p;
347     int shift = 0;
348     reserved_va = strtoul(arg, &p, 0);
349     switch (*p) {
350     case 'k':
351     case 'K':
352         shift = 10;
353         break;
354     case 'M':
355         shift = 20;
356         break;
357     case 'G':
358         shift = 30;
359         break;
360     }
361     if (shift) {
362         unsigned long unshifted = reserved_va;
363         p++;
364         reserved_va <<= shift;
365         if (reserved_va >> shift != unshifted) {
366             fprintf(stderr, "Reserved virtual address too big\n");
367             exit(EXIT_FAILURE);
368         }
369     }
370     if (*p) {
371         fprintf(stderr, "Unrecognised -R size suffix '%s'\n", p);
372         exit(EXIT_FAILURE);
373     }
374 }
375 
376 static void handle_arg_singlestep(const char *arg)
377 {
378     singlestep = 1;
379 }
380 
381 static void handle_arg_strace(const char *arg)
382 {
383     enable_strace = true;
384 }
385 
386 static void handle_arg_version(const char *arg)
387 {
388     printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
389            "\n" QEMU_COPYRIGHT "\n");
390     exit(EXIT_SUCCESS);
391 }
392 
393 static void handle_arg_trace(const char *arg)
394 {
395     trace_opt_parse(arg);
396 }
397 
398 #if defined(TARGET_XTENSA)
399 static void handle_arg_abi_call0(const char *arg)
400 {
401     xtensa_set_abi_call0();
402 }
403 #endif
404 
405 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
406 
407 #ifdef CONFIG_PLUGIN
408 static void handle_arg_plugin(const char *arg)
409 {
410     qemu_plugin_opt_parse(arg, &plugins);
411 }
412 #endif
413 
414 struct qemu_argument {
415     const char *argv;
416     const char *env;
417     bool has_arg;
418     void (*handle_opt)(const char *arg);
419     const char *example;
420     const char *help;
421 };
422 
423 static const struct qemu_argument arg_table[] = {
424     {"h",          "",                 false, handle_arg_help,
425      "",           "print this help"},
426     {"help",       "",                 false, handle_arg_help,
427      "",           ""},
428     {"g",          "QEMU_GDB",         true,  handle_arg_gdb,
429      "port",       "wait gdb connection to 'port'"},
430     {"L",          "QEMU_LD_PREFIX",   true,  handle_arg_ld_prefix,
431      "path",       "set the elf interpreter prefix to 'path'"},
432     {"s",          "QEMU_STACK_SIZE",  true,  handle_arg_stack_size,
433      "size",       "set the stack size to 'size' bytes"},
434     {"cpu",        "QEMU_CPU",         true,  handle_arg_cpu,
435      "model",      "select CPU (-cpu help for list)"},
436     {"E",          "QEMU_SET_ENV",     true,  handle_arg_set_env,
437      "var=value",  "sets targets environment variable (see below)"},
438     {"U",          "QEMU_UNSET_ENV",   true,  handle_arg_unset_env,
439      "var",        "unsets targets environment variable (see below)"},
440     {"0",          "QEMU_ARGV0",       true,  handle_arg_argv0,
441      "argv0",      "forces target process argv[0] to be 'argv0'"},
442     {"r",          "QEMU_UNAME",       true,  handle_arg_uname,
443      "uname",      "set qemu uname release string to 'uname'"},
444     {"B",          "QEMU_GUEST_BASE",  true,  handle_arg_guest_base,
445      "address",    "set guest_base address to 'address'"},
446     {"R",          "QEMU_RESERVED_VA", true,  handle_arg_reserved_va,
447      "size",       "reserve 'size' bytes for guest virtual address space"},
448     {"d",          "QEMU_LOG",         true,  handle_arg_log,
449      "item[,...]", "enable logging of specified items "
450      "(use '-d help' for a list of items)"},
451     {"dfilter",    "QEMU_DFILTER",     true,  handle_arg_dfilter,
452      "range[,...]","filter logging based on address range"},
453     {"D",          "QEMU_LOG_FILENAME", true, handle_arg_log_filename,
454      "logfile",     "write logs to 'logfile' (default stderr)"},
455     {"p",          "QEMU_PAGESIZE",    true,  handle_arg_pagesize,
456      "pagesize",   "set the host page size to 'pagesize'"},
457     {"singlestep", "QEMU_SINGLESTEP",  false, handle_arg_singlestep,
458      "",           "run in singlestep mode"},
459     {"strace",     "QEMU_STRACE",      false, handle_arg_strace,
460      "",           "log system calls"},
461     {"seed",       "QEMU_RAND_SEED",   true,  handle_arg_seed,
462      "",           "Seed for pseudo-random number generator"},
463     {"trace",      "QEMU_TRACE",       true,  handle_arg_trace,
464      "",           "[[enable=]<pattern>][,events=<file>][,file=<file>]"},
465 #ifdef CONFIG_PLUGIN
466     {"plugin",     "QEMU_PLUGIN",      true,  handle_arg_plugin,
467      "",           "[file=]<file>[,<argname>=<argvalue>]"},
468 #endif
469     {"version",    "QEMU_VERSION",     false, handle_arg_version,
470      "",           "display version information and exit"},
471 #if defined(TARGET_XTENSA)
472     {"xtensa-abi-call0", "QEMU_XTENSA_ABI_CALL0", false, handle_arg_abi_call0,
473      "",           "assume CALL0 Xtensa ABI"},
474 #endif
475     {NULL, NULL, false, NULL, NULL, NULL}
476 };
477 
478 static void usage(int exitcode)
479 {
480     const struct qemu_argument *arginfo;
481     int maxarglen;
482     int maxenvlen;
483 
484     printf("usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
485            "Linux CPU emulator (compiled for " TARGET_NAME " emulation)\n"
486            "\n"
487            "Options and associated environment variables:\n"
488            "\n");
489 
490     /* Calculate column widths. We must always have at least enough space
491      * for the column header.
492      */
493     maxarglen = strlen("Argument");
494     maxenvlen = strlen("Env-variable");
495 
496     for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
497         int arglen = strlen(arginfo->argv);
498         if (arginfo->has_arg) {
499             arglen += strlen(arginfo->example) + 1;
500         }
501         if (strlen(arginfo->env) > maxenvlen) {
502             maxenvlen = strlen(arginfo->env);
503         }
504         if (arglen > maxarglen) {
505             maxarglen = arglen;
506         }
507     }
508 
509     printf("%-*s %-*s Description\n", maxarglen+1, "Argument",
510             maxenvlen, "Env-variable");
511 
512     for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
513         if (arginfo->has_arg) {
514             printf("-%s %-*s %-*s %s\n", arginfo->argv,
515                    (int)(maxarglen - strlen(arginfo->argv) - 1),
516                    arginfo->example, maxenvlen, arginfo->env, arginfo->help);
517         } else {
518             printf("-%-*s %-*s %s\n", maxarglen, arginfo->argv,
519                     maxenvlen, arginfo->env,
520                     arginfo->help);
521         }
522     }
523 
524     printf("\n"
525            "Defaults:\n"
526            "QEMU_LD_PREFIX  = %s\n"
527            "QEMU_STACK_SIZE = %ld byte\n",
528            interp_prefix,
529            guest_stack_size);
530 
531     printf("\n"
532            "You can use -E and -U options or the QEMU_SET_ENV and\n"
533            "QEMU_UNSET_ENV environment variables to set and unset\n"
534            "environment variables for the target process.\n"
535            "It is possible to provide several variables by separating them\n"
536            "by commas in getsubopt(3) style. Additionally it is possible to\n"
537            "provide the -E and -U options multiple times.\n"
538            "The following lines are equivalent:\n"
539            "    -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
540            "    -E var1=val2,var2=val2 -U LD_PRELOAD,LD_DEBUG\n"
541            "    QEMU_SET_ENV=var1=val2,var2=val2 QEMU_UNSET_ENV=LD_PRELOAD,LD_DEBUG\n"
542            "Note that if you provide several changes to a single variable\n"
543            "the last change will stay in effect.\n"
544            "\n"
545            QEMU_HELP_BOTTOM "\n");
546 
547     exit(exitcode);
548 }
549 
550 static int parse_args(int argc, char **argv)
551 {
552     const char *r;
553     int optind;
554     const struct qemu_argument *arginfo;
555 
556     for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
557         if (arginfo->env == NULL) {
558             continue;
559         }
560 
561         r = getenv(arginfo->env);
562         if (r != NULL) {
563             arginfo->handle_opt(r);
564         }
565     }
566 
567     optind = 1;
568     for (;;) {
569         if (optind >= argc) {
570             break;
571         }
572         r = argv[optind];
573         if (r[0] != '-') {
574             break;
575         }
576         optind++;
577         r++;
578         if (!strcmp(r, "-")) {
579             break;
580         }
581         /* Treat --foo the same as -foo.  */
582         if (r[0] == '-') {
583             r++;
584         }
585 
586         for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
587             if (!strcmp(r, arginfo->argv)) {
588                 if (arginfo->has_arg) {
589                     if (optind >= argc) {
590                         (void) fprintf(stderr,
591                             "qemu: missing argument for option '%s'\n", r);
592                         exit(EXIT_FAILURE);
593                     }
594                     arginfo->handle_opt(argv[optind]);
595                     optind++;
596                 } else {
597                     arginfo->handle_opt(NULL);
598                 }
599                 break;
600             }
601         }
602 
603         /* no option matched the current argv */
604         if (arginfo->handle_opt == NULL) {
605             (void) fprintf(stderr, "qemu: unknown option '%s'\n", r);
606             exit(EXIT_FAILURE);
607         }
608     }
609 
610     if (optind >= argc) {
611         (void) fprintf(stderr, "qemu: no user program specified\n");
612         exit(EXIT_FAILURE);
613     }
614 
615     exec_path = argv[optind];
616 
617     return optind;
618 }
619 
620 int main(int argc, char **argv, char **envp)
621 {
622     struct target_pt_regs regs1, *regs = &regs1;
623     struct image_info info1, *info = &info1;
624     struct linux_binprm bprm;
625     TaskState *ts;
626     CPUArchState *env;
627     CPUState *cpu;
628     int optind;
629     char **target_environ, **wrk;
630     char **target_argv;
631     int target_argc;
632     int i;
633     int ret;
634     int execfd;
635     int log_mask;
636     unsigned long max_reserved_va;
637     bool preserve_argv0;
638 
639     error_init(argv[0]);
640     module_call_init(MODULE_INIT_TRACE);
641     qemu_init_cpu_list();
642     module_call_init(MODULE_INIT_QOM);
643 
644     envlist = envlist_create();
645 
646     /* add current environment into the list */
647     for (wrk = environ; *wrk != NULL; wrk++) {
648         (void) envlist_setenv(envlist, *wrk);
649     }
650 
651     /* Read the stack limit from the kernel.  If it's "unlimited",
652        then we can do little else besides use the default.  */
653     {
654         struct rlimit lim;
655         if (getrlimit(RLIMIT_STACK, &lim) == 0
656             && lim.rlim_cur != RLIM_INFINITY
657             && lim.rlim_cur == (target_long)lim.rlim_cur) {
658             guest_stack_size = lim.rlim_cur;
659         }
660     }
661 
662     cpu_model = NULL;
663 
664     qemu_add_opts(&qemu_trace_opts);
665     qemu_plugin_add_opts();
666 
667     optind = parse_args(argc, argv);
668 
669     log_mask = last_log_mask | (enable_strace ? LOG_STRACE : 0);
670     if (log_mask) {
671         qemu_log_needs_buffers();
672         qemu_set_log(log_mask);
673     }
674 
675     if (!trace_init_backends()) {
676         exit(1);
677     }
678     trace_init_file();
679     qemu_plugin_load_list(&plugins, &error_fatal);
680 
681     /* Zero out regs */
682     memset(regs, 0, sizeof(struct target_pt_regs));
683 
684     /* Zero out image_info */
685     memset(info, 0, sizeof(struct image_info));
686 
687     memset(&bprm, 0, sizeof (bprm));
688 
689     /* Scan interp_prefix dir for replacement files. */
690     init_paths(interp_prefix);
691 
692     init_qemu_uname_release();
693 
694     /*
695      * Manage binfmt-misc open-binary flag
696      */
697     execfd = qemu_getauxval(AT_EXECFD);
698     if (execfd == 0) {
699         execfd = open(exec_path, O_RDONLY);
700         if (execfd < 0) {
701             printf("Error while loading %s: %s\n", exec_path, strerror(errno));
702             _exit(EXIT_FAILURE);
703         }
704     }
705 
706     /*
707      * get binfmt_misc flags
708      */
709     preserve_argv0 = !!(qemu_getauxval(AT_FLAGS) & AT_FLAGS_PRESERVE_ARGV0);
710 
711     /*
712      * Manage binfmt-misc preserve-arg[0] flag
713      *    argv[optind]     full path to the binary
714      *    argv[optind + 1] original argv[0]
715      */
716     if (optind + 1 < argc && preserve_argv0) {
717         optind++;
718     }
719 
720     if (cpu_model == NULL) {
721         cpu_model = cpu_get_model(get_elf_eflags(execfd));
722     }
723     cpu_type = parse_cpu_option(cpu_model);
724 
725     /* init tcg before creating CPUs and to get qemu_host_page_size */
726     {
727         AccelClass *ac = ACCEL_GET_CLASS(current_accel());
728 
729         accel_init_interfaces(ac);
730         ac->init_machine(NULL);
731     }
732     cpu = cpu_create(cpu_type);
733     env = cpu->env_ptr;
734     cpu_reset(cpu);
735     thread_cpu = cpu;
736 
737     /*
738      * Reserving too much vm space via mmap can run into problems
739      * with rlimits, oom due to page table creation, etc.  We will
740      * still try it, if directed by the command-line option, but
741      * not by default.
742      */
743     max_reserved_va = MAX_RESERVED_VA(cpu);
744     if (reserved_va != 0) {
745         if (max_reserved_va && reserved_va > max_reserved_va) {
746             fprintf(stderr, "Reserved virtual address too big\n");
747             exit(EXIT_FAILURE);
748         }
749     } else if (HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32) {
750         /*
751          * reserved_va must be aligned with the host page size
752          * as it is used with mmap()
753          */
754         reserved_va = max_reserved_va & qemu_host_page_mask;
755     }
756 
757     {
758         Error *err = NULL;
759         if (seed_optarg != NULL) {
760             qemu_guest_random_seed_main(seed_optarg, &err);
761         } else {
762             qcrypto_init(&err);
763         }
764         if (err) {
765             error_reportf_err(err, "cannot initialize crypto: ");
766             exit(1);
767         }
768     }
769 
770     target_environ = envlist_to_environ(envlist, NULL);
771     envlist_free(envlist);
772 
773     /*
774      * Read in mmap_min_addr kernel parameter.  This value is used
775      * When loading the ELF image to determine whether guest_base
776      * is needed.  It is also used in mmap_find_vma.
777      */
778     {
779         FILE *fp;
780 
781         if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
782             unsigned long tmp;
783             if (fscanf(fp, "%lu", &tmp) == 1 && tmp != 0) {
784                 mmap_min_addr = tmp;
785                 qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n",
786                               mmap_min_addr);
787             }
788             fclose(fp);
789         }
790     }
791 
792     /*
793      * We prefer to not make NULL pointers accessible to QEMU.
794      * If we're in a chroot with no /proc, fall back to 1 page.
795      */
796     if (mmap_min_addr == 0) {
797         mmap_min_addr = qemu_host_page_size;
798         qemu_log_mask(CPU_LOG_PAGE,
799                       "host mmap_min_addr=0x%lx (fallback)\n",
800                       mmap_min_addr);
801     }
802 
803     /*
804      * Prepare copy of argv vector for target.
805      */
806     target_argc = argc - optind;
807     target_argv = calloc(target_argc + 1, sizeof (char *));
808     if (target_argv == NULL) {
809         (void) fprintf(stderr, "Unable to allocate memory for target_argv\n");
810         exit(EXIT_FAILURE);
811     }
812 
813     /*
814      * If argv0 is specified (using '-0' switch) we replace
815      * argv[0] pointer with the given one.
816      */
817     i = 0;
818     if (argv0 != NULL) {
819         target_argv[i++] = strdup(argv0);
820     }
821     for (; i < target_argc; i++) {
822         target_argv[i] = strdup(argv[optind + i]);
823     }
824     target_argv[target_argc] = NULL;
825 
826     ts = g_new0(TaskState, 1);
827     init_task_state(ts);
828     /* build Task State */
829     ts->info = info;
830     ts->bprm = &bprm;
831     cpu->opaque = ts;
832     task_settid(ts);
833 
834     fd_trans_init();
835 
836     ret = loader_exec(execfd, exec_path, target_argv, target_environ, regs,
837         info, &bprm);
838     if (ret != 0) {
839         printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
840         _exit(EXIT_FAILURE);
841     }
842 
843     for (wrk = target_environ; *wrk; wrk++) {
844         g_free(*wrk);
845     }
846 
847     g_free(target_environ);
848 
849     if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
850         qemu_log("guest_base  %p\n", (void *)guest_base);
851         log_page_dump("binary load");
852 
853         qemu_log("start_brk   0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
854         qemu_log("end_code    0x" TARGET_ABI_FMT_lx "\n", info->end_code);
855         qemu_log("start_code  0x" TARGET_ABI_FMT_lx "\n", info->start_code);
856         qemu_log("start_data  0x" TARGET_ABI_FMT_lx "\n", info->start_data);
857         qemu_log("end_data    0x" TARGET_ABI_FMT_lx "\n", info->end_data);
858         qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n", info->start_stack);
859         qemu_log("brk         0x" TARGET_ABI_FMT_lx "\n", info->brk);
860         qemu_log("entry       0x" TARGET_ABI_FMT_lx "\n", info->entry);
861         qemu_log("argv_start  0x" TARGET_ABI_FMT_lx "\n", info->arg_start);
862         qemu_log("env_start   0x" TARGET_ABI_FMT_lx "\n",
863                  info->arg_end + (abi_ulong)sizeof(abi_ulong));
864         qemu_log("auxv_start  0x" TARGET_ABI_FMT_lx "\n", info->saved_auxv);
865     }
866 
867     target_set_brk(info->brk);
868     syscall_init();
869     signal_init();
870 
871     /* Now that we've loaded the binary, GUEST_BASE is fixed.  Delay
872        generating the prologue until now so that the prologue can take
873        the real value of GUEST_BASE into account.  */
874     tcg_prologue_init(tcg_ctx);
875 
876     target_cpu_copy_regs(env, regs);
877 
878     if (gdbstub) {
879         if (gdbserver_start(gdbstub) < 0) {
880             fprintf(stderr, "qemu: could not open gdbserver on %s\n",
881                     gdbstub);
882             exit(EXIT_FAILURE);
883         }
884         gdb_handlesig(cpu, 0);
885     }
886     cpu_loop(env);
887     /* never exits */
888     return 0;
889 }
890