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