xref: /openbmc/qemu/linux-user/main.c (revision 84307cd6027c4602913177ff09aeefa4743b7234)
1 /*
2  *  qemu user main
3  *
4  *  Copyright (c) 2003-2008 Fabrice Bellard
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "qemu/help-texts.h"
22 #include "qemu/units.h"
23 #include "qemu/accel.h"
24 #include "qemu-version.h"
25 #include <sys/syscall.h>
26 #include <sys/resource.h>
27 #include <sys/shm.h>
28 #include <linux/binfmts.h>
29 
30 #include "qapi/error.h"
31 #include "qemu.h"
32 #include "user-internals.h"
33 #include "qemu/path.h"
34 #include "qemu/queue.h"
35 #include "qemu/config-file.h"
36 #include "qemu/cutils.h"
37 #include "qemu/error-report.h"
38 #include "qemu/help_option.h"
39 #include "qemu/module.h"
40 #include "qemu/plugin.h"
41 #include "user/guest-base.h"
42 #include "user/page-protection.h"
43 #include "exec/gdbstub.h"
44 #include "gdbstub/user.h"
45 #include "tcg/startup.h"
46 #include "qemu/timer.h"
47 #include "qemu/envlist.h"
48 #include "qemu/guest-random.h"
49 #include "elf.h"
50 #include "trace/control.h"
51 #include "target_elf.h"
52 #include "user/cpu_loop.h"
53 #include "crypto/init.h"
54 #include "fd-trans.h"
55 #include "signal-common.h"
56 #include "loader.h"
57 #include "user-mmap.h"
58 #include "tcg/perf.h"
59 #include "exec/page-vary.h"
60 
61 #ifdef CONFIG_SEMIHOSTING
62 #include "semihosting/semihost.h"
63 #endif
64 
65 #ifndef AT_FLAGS_PRESERVE_ARGV0
66 #define AT_FLAGS_PRESERVE_ARGV0_BIT 0
67 #define AT_FLAGS_PRESERVE_ARGV0 (1 << AT_FLAGS_PRESERVE_ARGV0_BIT)
68 #endif
69 
70 char *exec_path;
71 char real_exec_path[PATH_MAX];
72 
73 static bool opt_one_insn_per_tb;
74 static unsigned long opt_tb_size;
75 static const char *argv0;
76 static const char *gdbstub;
77 static envlist_t *envlist;
78 static const char *cpu_model;
79 static const char *cpu_type;
80 static const char *seed_optarg;
81 unsigned long mmap_min_addr;
82 uintptr_t guest_base;
83 bool have_guest_base;
84 
85 /*
86  * Used to implement backwards-compatibility for the `-strace`, and
87  * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
88  * -strace, or vice versa.
89  */
90 static bool enable_strace;
91 
92 /*
93  * The last log mask given by the user in an environment variable or argument.
94  * Used to support command line arguments overriding environment variables.
95  */
96 static int last_log_mask;
97 static const char *last_log_filename;
98 
99 /*
100  * When running 32-on-64 we should make sure we can fit all of the possible
101  * guest address space into a contiguous chunk of virtual host memory.
102  *
103  * This way we will never overlap with our own libraries or binaries or stack
104  * or anything else that QEMU maps.
105  *
106  * Many cpus reserve the high bit (or more than one for some 64-bit cpus)
107  * of the address for the kernel.  Some cpus rely on this and user space
108  * uses the high bit(s) for pointer tagging and the like.  For them, we
109  * must preserve the expected address space.
110  */
111 #ifndef MAX_RESERVED_VA
112 # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
113 #  if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
114       (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
115 #   define MAX_RESERVED_VA(CPU)  0xfffffffful
116 #  else
117 #   define MAX_RESERVED_VA(CPU)  ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1)
118 #  endif
119 # else
120 #  define MAX_RESERVED_VA(CPU)  0
121 # endif
122 #endif
123 
124 unsigned long reserved_va;
125 
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_tb_size(const char *arg)
429 {
430     if (qemu_strtoul(arg, NULL, 0, &opt_tb_size)) {
431         usage(EXIT_FAILURE);
432     }
433 }
434 
435 static void handle_arg_strace(const char *arg)
436 {
437     enable_strace = true;
438 }
439 
440 static void handle_arg_version(const char *arg)
441 {
442     printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
443            "\n" QEMU_COPYRIGHT "\n");
444     exit(EXIT_SUCCESS);
445 }
446 
447 static void handle_arg_trace(const char *arg)
448 {
449     trace_opt_parse(arg);
450 }
451 
452 #if defined(TARGET_XTENSA)
453 static void handle_arg_abi_call0(const char *arg)
454 {
455     xtensa_set_abi_call0();
456 }
457 #endif
458 
459 static void handle_arg_perfmap(const char *arg)
460 {
461     perf_enable_perfmap();
462 }
463 
464 static void handle_arg_jitdump(const char *arg)
465 {
466     perf_enable_jitdump();
467 }
468 
469 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
470 
471 #ifdef CONFIG_PLUGIN
472 static void handle_arg_plugin(const char *arg)
473 {
474     qemu_plugin_opt_parse(arg, &plugins);
475 }
476 #endif
477 
478 struct qemu_argument {
479     const char *argv;
480     const char *env;
481     bool has_arg;
482     void (*handle_opt)(const char *arg);
483     const char *example;
484     const char *help;
485 };
486 
487 static const struct qemu_argument arg_table[] = {
488     {"h",          "",                 false, handle_arg_help,
489      "",           "print this help"},
490     {"help",       "",                 false, handle_arg_help,
491      "",           ""},
492     {"g",          "QEMU_GDB",         true,  handle_arg_gdb,
493      "port",       "wait gdb connection to 'port'"},
494     {"L",          "QEMU_LD_PREFIX",   true,  handle_arg_ld_prefix,
495      "path",       "set the elf interpreter prefix to 'path'"},
496     {"s",          "QEMU_STACK_SIZE",  true,  handle_arg_stack_size,
497      "size",       "set the stack size to 'size' bytes"},
498     {"cpu",        "QEMU_CPU",         true,  handle_arg_cpu,
499      "model",      "select CPU (-cpu help for list)"},
500     {"E",          "QEMU_SET_ENV",     true,  handle_arg_set_env,
501      "var=value",  "sets targets environment variable (see below)"},
502     {"U",          "QEMU_UNSET_ENV",   true,  handle_arg_unset_env,
503      "var",        "unsets targets environment variable (see below)"},
504     {"0",          "QEMU_ARGV0",       true,  handle_arg_argv0,
505      "argv0",      "forces target process argv[0] to be 'argv0'"},
506     {"r",          "QEMU_UNAME",       true,  handle_arg_uname,
507      "uname",      "set qemu uname release string to 'uname'"},
508     {"B",          "QEMU_GUEST_BASE",  true,  handle_arg_guest_base,
509      "address",    "set guest_base address to 'address'"},
510     {"R",          "QEMU_RESERVED_VA", true,  handle_arg_reserved_va,
511      "size",       "reserve 'size' bytes for guest virtual address space"},
512     {"t",          "QEMU_RTSIG_MAP",   true,  handle_arg_rtsig_map,
513      "tsig hsig n[,...]",
514                    "map target rt signals [tsig,tsig+n) to [hsig,hsig+n]"},
515     {"d",          "QEMU_LOG",         true,  handle_arg_log,
516      "item[,...]", "enable logging of specified items "
517      "(use '-d help' for a list of items)"},
518     {"dfilter",    "QEMU_DFILTER",     true,  handle_arg_dfilter,
519      "range[,...]","filter logging based on address range"},
520     {"D",          "QEMU_LOG_FILENAME", true, handle_arg_log_filename,
521      "logfile",     "write logs to 'logfile' (default stderr)"},
522     {"p",          "QEMU_PAGESIZE",    true,  handle_arg_pagesize,
523      "pagesize",   "deprecated change to host page size"},
524     {"one-insn-per-tb",
525                    "QEMU_ONE_INSN_PER_TB",  false, handle_arg_one_insn_per_tb,
526      "",           "run with one guest instruction per emulated TB"},
527     {"tb-size",    "QEMU_TB_SIZE",     true,  handle_arg_tb_size,
528      "size",       "TCG translation block cache size"},
529     {"strace",     "QEMU_STRACE",      false, handle_arg_strace,
530      "",           "log system calls"},
531     {"seed",       "QEMU_RAND_SEED",   true,  handle_arg_seed,
532      "",           "Seed for pseudo-random number generator"},
533     {"trace",      "QEMU_TRACE",       true,  handle_arg_trace,
534      "",           "[[enable=]<pattern>][,events=<file>][,file=<file>]"},
535 #ifdef CONFIG_PLUGIN
536     {"plugin",     "QEMU_PLUGIN",      true,  handle_arg_plugin,
537      "",           "[file=]<file>[,<argname>=<argvalue>]"},
538 #endif
539     {"version",    "QEMU_VERSION",     false, handle_arg_version,
540      "",           "display version information and exit"},
541 #if defined(TARGET_XTENSA)
542     {"xtensa-abi-call0", "QEMU_XTENSA_ABI_CALL0", false, handle_arg_abi_call0,
543      "",           "assume CALL0 Xtensa ABI"},
544 #endif
545     {"perfmap",    "QEMU_PERFMAP",     false, handle_arg_perfmap,
546      "",           "Generate a /tmp/perf-${pid}.map file for perf"},
547     {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
548      "",           "Generate a jit-${pid}.dump file for perf"},
549     {NULL, NULL, false, NULL, NULL, NULL}
550 };
551 
552 static void usage(int exitcode)
553 {
554     const struct qemu_argument *arginfo;
555     int maxarglen;
556     int maxenvlen;
557 
558     printf("usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
559            "Linux CPU emulator (compiled for " TARGET_NAME " emulation)\n"
560            "\n"
561            "Options and associated environment variables:\n"
562            "\n");
563 
564     /* Calculate column widths. We must always have at least enough space
565      * for the column header.
566      */
567     maxarglen = strlen("Argument");
568     maxenvlen = strlen("Env-variable");
569 
570     for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
571         int arglen = strlen(arginfo->argv);
572         if (arginfo->has_arg) {
573             arglen += strlen(arginfo->example) + 1;
574         }
575         if (strlen(arginfo->env) > maxenvlen) {
576             maxenvlen = strlen(arginfo->env);
577         }
578         if (arglen > maxarglen) {
579             maxarglen = arglen;
580         }
581     }
582 
583     printf("%-*s %-*s Description\n", maxarglen+1, "Argument",
584             maxenvlen, "Env-variable");
585 
586     for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
587         if (arginfo->has_arg) {
588             printf("-%s %-*s %-*s %s\n", arginfo->argv,
589                    (int)(maxarglen - strlen(arginfo->argv) - 1),
590                    arginfo->example, maxenvlen, arginfo->env, arginfo->help);
591         } else {
592             printf("-%-*s %-*s %s\n", maxarglen, arginfo->argv,
593                     maxenvlen, arginfo->env,
594                     arginfo->help);
595         }
596     }
597 
598     printf("\n"
599            "Defaults:\n"
600            "QEMU_LD_PREFIX  = %s\n"
601            "QEMU_STACK_SIZE = %ld byte\n",
602            interp_prefix,
603            guest_stack_size);
604 
605     printf("\n"
606            "You can use -E and -U options or the QEMU_SET_ENV and\n"
607            "QEMU_UNSET_ENV environment variables to set and unset\n"
608            "environment variables for the target process.\n"
609            "It is possible to provide several variables by separating them\n"
610            "by commas in getsubopt(3) style. Additionally it is possible to\n"
611            "provide the -E and -U options multiple times.\n"
612            "The following lines are equivalent:\n"
613            "    -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
614            "    -E var1=val2,var2=val2 -U LD_PRELOAD,LD_DEBUG\n"
615            "    QEMU_SET_ENV=var1=val2,var2=val2 QEMU_UNSET_ENV=LD_PRELOAD,LD_DEBUG\n"
616            "Note that if you provide several changes to a single variable\n"
617            "the last change will stay in effect.\n"
618            "\n"
619            QEMU_HELP_BOTTOM "\n");
620 
621     exit(exitcode);
622 }
623 
624 static int parse_args(int argc, char **argv)
625 {
626     const char *r;
627     int optind;
628     const struct qemu_argument *arginfo;
629 
630     for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
631         if (arginfo->env == NULL) {
632             continue;
633         }
634 
635         r = getenv(arginfo->env);
636         if (r != NULL) {
637             arginfo->handle_opt(r);
638         }
639     }
640 
641     optind = 1;
642     for (;;) {
643         if (optind >= argc) {
644             break;
645         }
646         r = argv[optind];
647         if (r[0] != '-') {
648             break;
649         }
650         optind++;
651         r++;
652         if (!strcmp(r, "-")) {
653             break;
654         }
655         /* Treat --foo the same as -foo.  */
656         if (r[0] == '-') {
657             r++;
658         }
659 
660         for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
661             if (!strcmp(r, arginfo->argv)) {
662                 if (arginfo->has_arg) {
663                     if (optind >= argc) {
664                         (void) fprintf(stderr,
665                             "qemu: missing argument for option '%s'\n", r);
666                         exit(EXIT_FAILURE);
667                     }
668                     arginfo->handle_opt(argv[optind]);
669                     optind++;
670                 } else {
671                     arginfo->handle_opt(NULL);
672                 }
673                 break;
674             }
675         }
676 
677         /* no option matched the current argv */
678         if (arginfo->handle_opt == NULL) {
679             (void) fprintf(stderr, "qemu: unknown option '%s'\n", r);
680             exit(EXIT_FAILURE);
681         }
682     }
683 
684     if (optind >= argc) {
685         (void) fprintf(stderr, "qemu: no user program specified\n");
686         exit(EXIT_FAILURE);
687     }
688 
689     exec_path = argv[optind];
690 
691     return optind;
692 }
693 
694 int main(int argc, char **argv, char **envp)
695 {
696     struct target_pt_regs regs1, *regs = &regs1;
697     struct image_info info1, *info = &info1;
698     struct linux_binprm bprm;
699     TaskState *ts;
700     CPUArchState *env;
701     CPUState *cpu;
702     int optind;
703     char **target_environ, **wrk;
704     char **target_argv;
705     int target_argc;
706     int i;
707     int ret;
708     int execfd;
709     int host_page_size;
710     unsigned long max_reserved_va;
711     bool preserve_argv0;
712 
713     error_init(argv[0]);
714     module_call_init(MODULE_INIT_TRACE);
715     qemu_init_cpu_list();
716     module_call_init(MODULE_INIT_QOM);
717 
718     envlist = envlist_create();
719 
720     /*
721      * add current environment into the list
722      * envlist_setenv adds to the front of the list; to preserve environ
723      * order add from back to front
724      */
725     for (wrk = environ; *wrk != NULL; wrk++) {
726         continue;
727     }
728     while (wrk != environ) {
729         wrk--;
730         (void) envlist_setenv(envlist, *wrk);
731     }
732 
733     /* Read the stack limit from the kernel.  If it's "unlimited",
734        then we can do little else besides use the default.  */
735     {
736         struct rlimit lim;
737         if (getrlimit(RLIMIT_STACK, &lim) == 0
738             && lim.rlim_cur != RLIM_INFINITY
739             && lim.rlim_cur == (target_long)lim.rlim_cur
740             && lim.rlim_cur > guest_stack_size) {
741             guest_stack_size = lim.rlim_cur;
742         }
743     }
744 
745     cpu_model = NULL;
746 
747     qemu_add_opts(&qemu_trace_opts);
748     qemu_plugin_add_opts();
749 
750     optind = parse_args(argc, argv);
751 
752     qemu_set_log_filename_flags(last_log_filename,
753                                 last_log_mask | (enable_strace * LOG_STRACE),
754                                 &error_fatal);
755 
756     if (!trace_init_backends()) {
757         exit(1);
758     }
759     trace_init_file();
760     qemu_plugin_load_list(&plugins, &error_fatal);
761 
762     /* Zero out regs */
763     memset(regs, 0, sizeof(struct target_pt_regs));
764 
765     /* Zero out image_info */
766     memset(info, 0, sizeof(struct image_info));
767 
768     memset(&bprm, 0, sizeof (bprm));
769 
770     /* Scan interp_prefix dir for replacement files. */
771     init_paths(interp_prefix);
772 
773     init_qemu_uname_release();
774 
775     /*
776      * Manage binfmt-misc open-binary flag
777      */
778     errno = 0;
779     execfd = qemu_getauxval(AT_EXECFD);
780     if (errno != 0) {
781         execfd = open(exec_path, O_RDONLY);
782         if (execfd < 0) {
783             printf("Error while loading %s: %s\n", exec_path, strerror(errno));
784             _exit(EXIT_FAILURE);
785         }
786     }
787 
788     /* Resolve executable file name to full path name */
789     if (realpath(exec_path, real_exec_path)) {
790         exec_path = real_exec_path;
791     }
792 
793     /*
794      * get binfmt_misc flags
795      */
796     preserve_argv0 = !!(qemu_getauxval(AT_FLAGS) & AT_FLAGS_PRESERVE_ARGV0);
797 
798     /*
799      * Manage binfmt-misc preserve-arg[0] flag
800      *    argv[optind]     full path to the binary
801      *    argv[optind + 1] original argv[0]
802      */
803     if (optind + 1 < argc && preserve_argv0) {
804         optind++;
805     }
806 
807     if (cpu_model == NULL) {
808         cpu_model = cpu_get_model(get_elf_eflags(execfd));
809     }
810     cpu_type = parse_cpu_option(cpu_model);
811 
812     /* init tcg before creating CPUs */
813     {
814         AccelState *accel = current_accel();
815         AccelClass *ac = ACCEL_GET_CLASS(accel);
816 
817         accel_init_interfaces(ac);
818         object_property_set_bool(OBJECT(accel), "one-insn-per-tb",
819                                  opt_one_insn_per_tb, &error_abort);
820         object_property_set_int(OBJECT(accel), "tb-size",
821                                 opt_tb_size, &error_abort);
822         ac->init_machine(NULL);
823     }
824 
825     /*
826      * Finalize page size before creating CPUs.
827      * This will do nothing if !TARGET_PAGE_BITS_VARY.
828      * The most efficient setting is to match the host.
829      */
830     host_page_size = qemu_real_host_page_size();
831     set_preferred_target_page_bits(ctz32(host_page_size));
832     finalize_target_page_bits();
833 
834     cpu = cpu_create(cpu_type);
835     env = cpu_env(cpu);
836     cpu_reset(cpu);
837     thread_cpu = cpu;
838 
839     /*
840      * Reserving too much vm space via mmap can run into problems with rlimits,
841      * oom due to page table creation, etc.  We will still try it, if directed
842      * by the command-line option, but not by default. Unless we're running a
843      * target address space of 32 or fewer bits on a host with 64 bits.
844      */
845     max_reserved_va = MAX_RESERVED_VA(cpu);
846     if (reserved_va != 0) {
847         if ((reserved_va + 1) % host_page_size) {
848             char *s = size_to_str(host_page_size);
849             fprintf(stderr, "Reserved virtual address not aligned mod %s\n", s);
850             g_free(s);
851             exit(EXIT_FAILURE);
852         }
853         if (max_reserved_va && reserved_va > max_reserved_va) {
854             fprintf(stderr, "Reserved virtual address too big\n");
855             exit(EXIT_FAILURE);
856         }
857     } else if (HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32) {
858         /* MAX_RESERVED_VA + 1 is a large power of 2, so is aligned. */
859         reserved_va = max_reserved_va;
860     }
861 
862     /*
863      * Temporarily disable
864      *   "comparison is always false due to limited range of data type"
865      * due to comparison between (possible) uint64_t and uintptr_t.
866      */
867 #pragma GCC diagnostic push
868 #pragma GCC diagnostic ignored "-Wtype-limits"
869 #pragma GCC diagnostic ignored "-Wtautological-compare"
870 
871     /*
872      * Select an initial value for task_unmapped_base that is in range.
873      */
874     if (reserved_va) {
875         if (TASK_UNMAPPED_BASE < reserved_va) {
876             task_unmapped_base = TASK_UNMAPPED_BASE;
877         } else {
878             /* The most common default formula is TASK_SIZE / 3. */
879             task_unmapped_base = TARGET_PAGE_ALIGN(reserved_va / 3);
880         }
881     } else if (TASK_UNMAPPED_BASE < UINTPTR_MAX) {
882         task_unmapped_base = TASK_UNMAPPED_BASE;
883     } else {
884         /* 32-bit host: pick something medium size. */
885         task_unmapped_base = 0x10000000;
886     }
887     mmap_next_start = task_unmapped_base;
888 
889     /* Similarly for elf_et_dyn_base. */
890     if (reserved_va) {
891         if (ELF_ET_DYN_BASE < reserved_va) {
892             elf_et_dyn_base = ELF_ET_DYN_BASE;
893         } else {
894             /* The most common default formula is TASK_SIZE / 3 * 2. */
895             elf_et_dyn_base = TARGET_PAGE_ALIGN(reserved_va / 3) * 2;
896         }
897     } else if (ELF_ET_DYN_BASE < UINTPTR_MAX) {
898         elf_et_dyn_base = ELF_ET_DYN_BASE;
899     } else {
900         /* 32-bit host: pick something medium size. */
901         elf_et_dyn_base = 0x18000000;
902     }
903 
904 #pragma GCC diagnostic pop
905 
906     {
907         Error *err = NULL;
908         if (seed_optarg != NULL) {
909             qemu_guest_random_seed_main(seed_optarg, &err);
910         } else {
911             qcrypto_init(&err);
912         }
913         if (err) {
914             error_reportf_err(err, "cannot initialize crypto: ");
915             exit(1);
916         }
917     }
918 
919     target_environ = envlist_to_environ(envlist, NULL);
920     envlist_free(envlist);
921 
922     /*
923      * Read in mmap_min_addr kernel parameter.  This value is used
924      * When loading the ELF image to determine whether guest_base
925      * is needed.  It is also used in mmap_find_vma.
926      */
927     {
928         FILE *fp;
929 
930         if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
931             unsigned long tmp;
932             if (fscanf(fp, "%lu", &tmp) == 1 && tmp != 0) {
933                 mmap_min_addr = MAX(tmp, host_page_size);
934                 qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n",
935                               mmap_min_addr);
936             }
937             fclose(fp);
938         }
939     }
940 
941     /*
942      * We prefer to not make NULL pointers accessible to QEMU.
943      * If we're in a chroot with no /proc, fall back to 1 page.
944      */
945     if (mmap_min_addr == 0) {
946         mmap_min_addr = host_page_size;
947         qemu_log_mask(CPU_LOG_PAGE,
948                       "host mmap_min_addr=0x%lx (fallback)\n",
949                       mmap_min_addr);
950     }
951 
952     /*
953      * Prepare copy of argv vector for target.
954      */
955     target_argc = argc - optind;
956     target_argv = g_new0(char *, target_argc + 1);
957 
958     /*
959      * If argv0 is specified (using '-0' switch) we replace
960      * argv[0] pointer with the given one.
961      */
962     i = 0;
963     if (argv0 != NULL) {
964         target_argv[i++] = strdup(argv0);
965     }
966     for (; i < target_argc; i++) {
967         target_argv[i] = strdup(argv[optind + i]);
968     }
969     target_argv[target_argc] = NULL;
970 
971     ts = g_new0(TaskState, 1);
972     init_task_state(ts);
973     /* build Task State */
974     ts->info = info;
975     ts->bprm = &bprm;
976     cpu->opaque = ts;
977     task_settid(ts);
978 
979     fd_trans_init();
980 
981     ret = loader_exec(execfd, exec_path, target_argv, target_environ, regs,
982         info, &bprm);
983     if (ret != 0) {
984         printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
985         _exit(EXIT_FAILURE);
986     }
987 
988     for (wrk = target_environ; *wrk; wrk++) {
989         g_free(*wrk);
990     }
991 
992     g_free(target_environ);
993 
994     if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
995         FILE *f = qemu_log_trylock();
996         if (f) {
997             fprintf(f, "guest_base  %p\n", (void *)guest_base);
998             fprintf(f, "page layout changed following binary load\n");
999             page_dump(f);
1000 
1001             fprintf(f, "end_code    0x" TARGET_ABI_FMT_lx "\n",
1002                     info->end_code);
1003             fprintf(f, "start_code  0x" TARGET_ABI_FMT_lx "\n",
1004                     info->start_code);
1005             fprintf(f, "start_data  0x" TARGET_ABI_FMT_lx "\n",
1006                     info->start_data);
1007             fprintf(f, "end_data    0x" TARGET_ABI_FMT_lx "\n",
1008                     info->end_data);
1009             fprintf(f, "start_stack 0x" TARGET_ABI_FMT_lx "\n",
1010                     info->start_stack);
1011             fprintf(f, "brk         0x" TARGET_ABI_FMT_lx "\n",
1012                     info->brk);
1013             fprintf(f, "entry       0x" TARGET_ABI_FMT_lx "\n",
1014                     info->entry);
1015             fprintf(f, "argv_start  0x" TARGET_ABI_FMT_lx "\n",
1016                     info->argv);
1017             fprintf(f, "env_start   0x" TARGET_ABI_FMT_lx "\n",
1018                     info->envp);
1019             fprintf(f, "auxv_start  0x" TARGET_ABI_FMT_lx "\n",
1020                     info->saved_auxv);
1021             qemu_log_unlock(f);
1022         }
1023     }
1024 
1025     target_set_brk(info->brk);
1026     syscall_init();
1027     signal_init(rtsig_map);
1028 
1029     /* Now that we've loaded the binary, GUEST_BASE is fixed.  Delay
1030        generating the prologue until now so that the prologue can take
1031        the real value of GUEST_BASE into account.  */
1032     tcg_prologue_init();
1033 
1034     target_cpu_copy_regs(env, regs);
1035 
1036     if (gdbstub) {
1037         gdbserver_start(gdbstub, &error_fatal);
1038     }
1039 
1040 #ifdef CONFIG_SEMIHOSTING
1041     qemu_semihosting_guestfd_init();
1042 #endif
1043 
1044     cpu_loop(env);
1045     /* never exits */
1046     return 0;
1047 }
1048