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