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