xref: /openbmc/qemu/bsd-user/main.c (revision 52a523af)
1 /*
2  *  qemu bsd user main
3  *
4  *  Copyright (c) 2003-2008 Fabrice Bellard
5  *  Copyright (c) 2013-14 Stacey Son
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "qemu/osdep.h"
22 #include <sys/resource.h>
23 #include <sys/sysctl.h>
24 
25 #include "qemu/help-texts.h"
26 #include "qemu/units.h"
27 #include "qemu/accel.h"
28 #include "qemu-version.h"
29 #include <machine/trap.h>
30 
31 #include "qapi/error.h"
32 #include "qemu.h"
33 #include "qemu/config-file.h"
34 #include "qemu/error-report.h"
35 #include "qemu/path.h"
36 #include "qemu/help_option.h"
37 #include "qemu/module.h"
38 #include "qemu/plugin.h"
39 #include "exec/exec-all.h"
40 #include "user/guest-base.h"
41 #include "tcg/startup.h"
42 #include "qemu/timer.h"
43 #include "qemu/envlist.h"
44 #include "qemu/cutils.h"
45 #include "exec/log.h"
46 #include "trace/control.h"
47 #include "crypto/init.h"
48 #include "qemu/guest-random.h"
49 #include "gdbstub/user.h"
50 #include "exec/page-vary.h"
51 
52 #include "host-os.h"
53 #include "target_arch_cpu.h"
54 
55 
56 /*
57  * TODO: Remove these and rely only on qemu_real_host_page_size().
58  */
59 uintptr_t qemu_host_page_size;
60 intptr_t qemu_host_page_mask;
61 
62 static bool opt_one_insn_per_tb;
63 static unsigned long opt_tb_size;
64 uintptr_t guest_base;
65 bool have_guest_base;
66 /*
67  * When running 32-on-64 we should make sure we can fit all of the possible
68  * guest address space into a contiguous chunk of virtual host memory.
69  *
70  * This way we will never overlap with our own libraries or binaries or stack
71  * or anything else that QEMU maps.
72  *
73  * Many cpus reserve the high bit (or more than one for some 64-bit cpus)
74  * of the address for the kernel.  Some cpus rely on this and user space
75  * uses the high bit(s) for pointer tagging and the like.  For them, we
76  * must preserve the expected address space.
77  */
78 #ifndef MAX_RESERVED_VA
79 # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
80 #  if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
81       (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
82 #   define MAX_RESERVED_VA(CPU)  0xfffffffful
83 #  else
84 #   define MAX_RESERVED_VA(CPU)  ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1)
85 #  endif
86 # else
87 #  define MAX_RESERVED_VA(CPU)  0
88 # endif
89 #endif
90 
91 unsigned long reserved_va;
92 
93 const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
94 const char *qemu_uname_release;
95 
96 unsigned long target_maxtsiz = TARGET_MAXTSIZ;   /* max text size */
97 unsigned long target_dfldsiz = TARGET_DFLDSIZ;   /* initial data size limit */
98 unsigned long target_maxdsiz = TARGET_MAXDSIZ;   /* max data size */
99 unsigned long target_dflssiz = TARGET_DFLSSIZ;   /* initial data size limit */
100 unsigned long target_maxssiz = TARGET_MAXSSIZ;   /* max stack size */
101 unsigned long target_sgrowsiz = TARGET_SGROWSIZ; /* amount to grow stack */
102 
103 /* Helper routines for implementing atomic operations. */
104 
fork_start(void)105 void fork_start(void)
106 {
107     start_exclusive();
108     mmap_fork_start();
109     cpu_list_lock();
110     qemu_plugin_user_prefork_lock();
111     gdbserver_fork_start();
112 }
113 
fork_end(pid_t pid)114 void fork_end(pid_t pid)
115 {
116     bool child = pid == 0;
117 
118     qemu_plugin_user_postfork(child);
119     mmap_fork_end(child);
120     if (child) {
121         CPUState *cpu, *next_cpu;
122         /*
123          * Child processes created by fork() only have a single thread.
124          * Discard information about the parent threads.
125          */
126         CPU_FOREACH_SAFE(cpu, next_cpu) {
127             if (cpu != thread_cpu) {
128                 QTAILQ_REMOVE_RCU(&cpus_queue, cpu, node);
129             }
130         }
131         qemu_init_cpu_list();
132         get_task_state(thread_cpu)->ts_tid = qemu_get_thread_id();
133     } else {
134         cpu_list_unlock();
135     }
136     gdbserver_fork_end(thread_cpu, pid);
137     /*
138      * qemu_init_cpu_list() reinitialized the child exclusive state, but we
139      * also need to keep current_cpu consistent, so call end_exclusive() for
140      * both child and parent.
141      */
142     end_exclusive();
143 }
144 
cpu_loop(CPUArchState * env)145 void cpu_loop(CPUArchState *env)
146 {
147     target_cpu_loop(env);
148 }
149 
usage(void)150 static void usage(void)
151 {
152     printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
153            "\n" QEMU_COPYRIGHT "\n"
154            "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
155            "BSD CPU emulator (compiled for %s emulation)\n"
156            "\n"
157            "Standard options:\n"
158            "-h                print this help\n"
159            "-g port           wait gdb connection to port\n"
160            "-L path           set the elf interpreter prefix (default=%s)\n"
161            "-s size           set the stack size in bytes (default=%ld)\n"
162            "-cpu model        select CPU (-cpu help for list)\n"
163            "-drop-ld-preload  drop LD_PRELOAD for target process\n"
164            "-E var=value      sets/modifies targets environment variable(s)\n"
165            "-U var            unsets targets environment variable(s)\n"
166            "-B address        set guest_base address to address\n"
167            "\n"
168            "Debug options:\n"
169            "-d item1[,...]    enable logging of specified items\n"
170            "                  (use '-d help' for a list of log items)\n"
171            "-D logfile        write logs to 'logfile' (default stderr)\n"
172            "-one-insn-per-tb  run with one guest instruction per emulated TB\n"
173            "-tb-size size     TCG translation block cache size\n"
174            "-strace           log system calls\n"
175            "-trace            [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
176            "                  specify tracing options\n"
177            "\n"
178            "Environment variables:\n"
179            "QEMU_STRACE       Print system calls and arguments similar to the\n"
180            "                  'strace' program.  Enable by setting to any value.\n"
181            "You can use -E and -U options to set/unset environment variables\n"
182            "for target process.  It is possible to provide several variables\n"
183            "by repeating the option.  For example:\n"
184            "    -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
185            "Note that if you provide several changes to single variable\n"
186            "last change will stay in effect.\n"
187            "\n"
188            QEMU_HELP_BOTTOM "\n"
189            ,
190            TARGET_NAME,
191            interp_prefix,
192            target_dflssiz);
193     exit(1);
194 }
195 
196 __thread CPUState *thread_cpu;
197 
stop_all_tasks(void)198 void stop_all_tasks(void)
199 {
200     /*
201      * We trust when using NPTL (pthreads) start_exclusive() handles thread
202      * stopping correctly.
203      */
204     start_exclusive();
205 }
206 
qemu_cpu_is_self(CPUState * cpu)207 bool qemu_cpu_is_self(CPUState *cpu)
208 {
209     return thread_cpu == cpu;
210 }
211 
qemu_cpu_kick(CPUState * cpu)212 void qemu_cpu_kick(CPUState *cpu)
213 {
214     cpu_exit(cpu);
215 }
216 
217 /* Assumes contents are already zeroed.  */
init_task_state(TaskState * ts)218 static void init_task_state(TaskState *ts)
219 {
220     ts->sigaltstack_used = (struct target_sigaltstack) {
221         .ss_sp = 0,
222         .ss_size = 0,
223         .ss_flags = TARGET_SS_DISABLE,
224     };
225 }
226 
gemu_log(const char * fmt,...)227 void gemu_log(const char *fmt, ...)
228 {
229     va_list ap;
230 
231     va_start(ap, fmt);
232     vfprintf(stderr, fmt, ap);
233     va_end(ap);
234 }
235 
236 static void
adjust_ssize(void)237 adjust_ssize(void)
238 {
239     struct rlimit rl;
240 
241     if (getrlimit(RLIMIT_STACK, &rl) != 0) {
242         return;
243     }
244 
245     target_maxssiz = MIN(target_maxssiz, rl.rlim_max);
246     target_dflssiz = MIN(MAX(target_dflssiz, rl.rlim_cur), target_maxssiz);
247 
248     rl.rlim_max = target_maxssiz;
249     rl.rlim_cur = target_dflssiz;
250     setrlimit(RLIMIT_STACK, &rl);
251 }
252 
main(int argc,char ** argv)253 int main(int argc, char **argv)
254 {
255     const char *filename;
256     const char *cpu_model;
257     const char *cpu_type;
258     const char *log_file = NULL;
259     const char *log_mask = NULL;
260     const char *seed_optarg = NULL;
261     struct target_pt_regs regs1, *regs = &regs1;
262     struct image_info info1, *info = &info1;
263     struct bsd_binprm bprm;
264     TaskState *ts;
265     CPUArchState *env;
266     CPUState *cpu;
267     int optind, rv;
268     const char *r;
269     const char *gdbstub = NULL;
270     char **target_environ, **wrk;
271     envlist_t *envlist = NULL;
272     char *argv0 = NULL;
273     int host_page_size;
274     unsigned long max_reserved_va;
275 
276     adjust_ssize();
277 
278     if (argc <= 1) {
279         usage();
280     }
281 
282 
283     error_init(argv[0]);
284     module_call_init(MODULE_INIT_TRACE);
285     qemu_init_cpu_list();
286     module_call_init(MODULE_INIT_QOM);
287 
288     envlist = envlist_create();
289 
290     /*
291      * add current environment into the list
292      * envlist_setenv adds to the front of the list; to preserve environ
293      * order add from back to front
294      */
295     for (wrk = environ; *wrk != NULL; wrk++) {
296         continue;
297     }
298     while (wrk != environ) {
299         wrk--;
300         (void) envlist_setenv(envlist, *wrk);
301     }
302 
303     qemu_host_page_size = getpagesize();
304     qemu_host_page_size = MAX(qemu_host_page_size, TARGET_PAGE_SIZE);
305 
306     cpu_model = NULL;
307 
308     qemu_add_opts(&qemu_trace_opts);
309 
310     optind = 1;
311     for (;;) {
312         if (optind >= argc) {
313             break;
314         }
315         r = argv[optind];
316         if (r[0] != '-') {
317             break;
318         }
319         optind++;
320         r++;
321         if (!strcmp(r, "-")) {
322             break;
323         } else if (!strcmp(r, "d")) {
324             if (optind >= argc) {
325                 break;
326             }
327             log_mask = argv[optind++];
328         } else if (!strcmp(r, "D")) {
329             if (optind >= argc) {
330                 break;
331             }
332             log_file = argv[optind++];
333         } else if (!strcmp(r, "E")) {
334             r = argv[optind++];
335             if (envlist_setenv(envlist, r) != 0) {
336                 usage();
337             }
338         } else if (!strcmp(r, "ignore-environment")) {
339             envlist_free(envlist);
340             envlist = envlist_create();
341         } else if (!strcmp(r, "U")) {
342             r = argv[optind++];
343             if (envlist_unsetenv(envlist, r) != 0) {
344                 usage();
345             }
346         } else if (!strcmp(r, "s")) {
347             r = argv[optind++];
348             rv = qemu_strtoul(r, &r, 0, &target_dflssiz);
349             if (rv < 0 || target_dflssiz <= 0) {
350                 usage();
351             }
352             if (*r == 'M') {
353                 target_dflssiz *= 1024 * 1024;
354             } else if (*r == 'k' || *r == 'K') {
355                 target_dflssiz *= 1024;
356             }
357             if (target_dflssiz > target_maxssiz) {
358                 usage();
359             }
360         } else if (!strcmp(r, "L")) {
361             interp_prefix = argv[optind++];
362         } else if (!strcmp(r, "p")) {
363             unsigned size, want = qemu_real_host_page_size();
364 
365             r = argv[optind++];
366             if (qemu_strtoui(r, NULL, 10, &size) || size != want) {
367                 warn_report("Deprecated page size option cannot "
368                             "change host page size (%u)", want);
369             }
370         } else if (!strcmp(r, "g")) {
371             gdbstub = g_strdup(argv[optind++]);
372         } else if (!strcmp(r, "r")) {
373             qemu_uname_release = argv[optind++];
374         } else if (!strcmp(r, "cpu")) {
375             cpu_model = argv[optind++];
376             if (is_help_option(cpu_model)) {
377                 list_cpus();
378                 exit(1);
379             }
380         } else if (!strcmp(r, "B")) {
381             rv = qemu_strtoul(argv[optind++], NULL, 0, &guest_base);
382             if (rv < 0) {
383                 usage();
384             }
385             have_guest_base = true;
386         } else if (!strcmp(r, "drop-ld-preload")) {
387             (void) envlist_unsetenv(envlist, "LD_PRELOAD");
388         } else if (!strcmp(r, "seed")) {
389             seed_optarg = optarg;
390         } else if (!strcmp(r, "one-insn-per-tb")) {
391             opt_one_insn_per_tb = true;
392         } else if (!strcmp(r, "tb-size")) {
393             r = argv[optind++];
394             if (qemu_strtoul(r, NULL, 0, &opt_tb_size)) {
395                 usage();
396             }
397         } else if (!strcmp(r, "strace")) {
398             do_strace = 1;
399         } else if (!strcmp(r, "trace")) {
400             trace_opt_parse(optarg);
401         } else if (!strcmp(r, "0")) {
402             argv0 = argv[optind++];
403         } else {
404             usage();
405         }
406     }
407 
408     qemu_host_page_mask = -qemu_host_page_size;
409 
410     /* init debug */
411     {
412         int mask = 0;
413         if (log_mask) {
414             mask = qemu_str_to_log_mask(log_mask);
415             if (!mask) {
416                 qemu_print_log_usage(stdout);
417                 exit(1);
418             }
419         }
420         qemu_set_log_filename_flags(log_file, mask, &error_fatal);
421     }
422 
423     if (optind >= argc) {
424         usage();
425     }
426     filename = argv[optind];
427     if (argv0) {
428         argv[optind] = argv0;
429     }
430 
431     if (!trace_init_backends()) {
432         exit(1);
433     }
434     trace_init_file();
435 
436     /* Zero out regs */
437     memset(regs, 0, sizeof(struct target_pt_regs));
438 
439     /* Zero bsd params */
440     memset(&bprm, 0, sizeof(bprm));
441 
442     /* Zero out image_info */
443     memset(info, 0, sizeof(struct image_info));
444 
445     /* Scan interp_prefix dir for replacement files. */
446     init_paths(interp_prefix);
447 
448     if (cpu_model == NULL) {
449         cpu_model = TARGET_DEFAULT_CPU_MODEL;
450     }
451 
452     cpu_type = parse_cpu_option(cpu_model);
453 
454     /* init tcg before creating CPUs and to get qemu_host_page_size */
455     {
456         AccelState *accel = current_accel();
457         AccelClass *ac = ACCEL_GET_CLASS(accel);
458 
459         accel_init_interfaces(ac);
460         object_property_set_bool(OBJECT(accel), "one-insn-per-tb",
461                                  opt_one_insn_per_tb, &error_abort);
462         object_property_set_int(OBJECT(accel), "tb-size",
463                                 opt_tb_size, &error_abort);
464         ac->init_machine(NULL);
465     }
466 
467     /*
468      * Finalize page size before creating CPUs.
469      * This will do nothing if !TARGET_PAGE_BITS_VARY.
470      * The most efficient setting is to match the host.
471      */
472     host_page_size = qemu_real_host_page_size();
473     set_preferred_target_page_bits(ctz32(host_page_size));
474     finalize_target_page_bits();
475 
476     cpu = cpu_create(cpu_type);
477     env = cpu_env(cpu);
478     cpu_reset(cpu);
479     thread_cpu = cpu;
480 
481     /*
482      * Reserving too much vm space via mmap can run into problems with rlimits,
483      * oom due to page table creation, etc.  We will still try it, if directed
484      * by the command-line option, but not by default. Unless we're running a
485      * target address space of 32 or fewer bits on a host with 64 bits.
486      */
487     max_reserved_va = MAX_RESERVED_VA(cpu);
488     if (reserved_va != 0) {
489         if ((reserved_va + 1) % host_page_size) {
490             char *s = size_to_str(host_page_size);
491             fprintf(stderr, "Reserved virtual address not aligned mod %s\n", s);
492             g_free(s);
493             exit(EXIT_FAILURE);
494         }
495         if (max_reserved_va && reserved_va > max_reserved_va) {
496             fprintf(stderr, "Reserved virtual address too big\n");
497             exit(EXIT_FAILURE);
498         }
499     } else if (HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32) {
500         /* MAX_RESERVED_VA + 1 is a large power of 2, so is aligned. */
501         reserved_va = max_reserved_va;
502     }
503 
504     if (getenv("QEMU_STRACE")) {
505         do_strace = 1;
506     }
507 
508     target_environ = envlist_to_environ(envlist, NULL);
509     envlist_free(envlist);
510 
511     {
512         Error *err = NULL;
513         if (seed_optarg != NULL) {
514             qemu_guest_random_seed_main(seed_optarg, &err);
515         } else {
516             qcrypto_init(&err);
517         }
518         if (err) {
519             error_reportf_err(err, "cannot initialize crypto: ");
520             exit(1);
521         }
522     }
523 
524     /*
525      * Now that page sizes are configured we can do
526      * proper page alignment for guest_base.
527      */
528     if (have_guest_base) {
529         if (guest_base & ~qemu_host_page_mask) {
530             error_report("Selected guest base not host page aligned");
531             exit(1);
532         }
533     }
534 
535     /*
536      * If reserving host virtual address space, do so now.
537      * Combined with '-B', ensure that the chosen range is free.
538      */
539     if (reserved_va) {
540         void *p;
541 
542         if (have_guest_base) {
543             p = mmap((void *)guest_base, reserved_va + 1, PROT_NONE,
544                      MAP_ANON | MAP_PRIVATE | MAP_FIXED | MAP_EXCL, -1, 0);
545         } else {
546             p = mmap(NULL, reserved_va + 1, PROT_NONE,
547                      MAP_ANON | MAP_PRIVATE, -1, 0);
548         }
549         if (p == MAP_FAILED) {
550             const char *err = strerror(errno);
551             char *sz = size_to_str(reserved_va + 1);
552 
553             if (have_guest_base) {
554                 error_report("Cannot allocate %s bytes at -B %p for guest "
555                              "address space: %s", sz, (void *)guest_base, err);
556             } else {
557                 error_report("Cannot allocate %s bytes for guest "
558                              "address space: %s", sz, err);
559             }
560             exit(1);
561         }
562         guest_base = (uintptr_t)p;
563         have_guest_base = true;
564 
565         /* Ensure that mmap_next_start is within range. */
566         if (reserved_va <= mmap_next_start) {
567             mmap_next_start = (reserved_va / 4 * 3)
568                               & TARGET_PAGE_MASK & qemu_host_page_mask;
569         }
570     }
571 
572     if (loader_exec(filename, argv + optind, target_environ, regs, info,
573                     &bprm) != 0) {
574         printf("Error loading %s\n", filename);
575         _exit(1);
576     }
577 
578     for (wrk = target_environ; *wrk; wrk++) {
579         g_free(*wrk);
580     }
581 
582     g_free(target_environ);
583 
584     if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
585         FILE *f = qemu_log_trylock();
586         if (f) {
587             fprintf(f, "guest_base  %p\n", (void *)guest_base);
588             fprintf(f, "page layout changed following binary load\n");
589             page_dump(f);
590 
591             fprintf(f, "end_code    0x" TARGET_ABI_FMT_lx "\n",
592                     info->end_code);
593             fprintf(f, "start_code  0x" TARGET_ABI_FMT_lx "\n",
594                     info->start_code);
595             fprintf(f, "start_data  0x" TARGET_ABI_FMT_lx "\n",
596                     info->start_data);
597             fprintf(f, "end_data    0x" TARGET_ABI_FMT_lx "\n",
598                     info->end_data);
599             fprintf(f, "start_stack 0x" TARGET_ABI_FMT_lx "\n",
600                     info->start_stack);
601             fprintf(f, "brk         0x" TARGET_ABI_FMT_lx "\n", info->brk);
602             fprintf(f, "entry       0x" TARGET_ABI_FMT_lx "\n", info->entry);
603 
604             qemu_log_unlock(f);
605         }
606     }
607 
608     /* build Task State */
609     ts = g_new0(TaskState, 1);
610     init_task_state(ts);
611     ts->info = info;
612     ts->bprm = &bprm;
613     ts->ts_tid = qemu_get_thread_id();
614     cpu->opaque = ts;
615 
616     target_set_brk(info->brk);
617     syscall_init();
618     signal_init();
619 
620     /*
621      * Now that we've loaded the binary, GUEST_BASE is fixed.  Delay
622      * generating the prologue until now so that the prologue can take
623      * the real value of GUEST_BASE into account.
624      */
625     tcg_prologue_init();
626 
627     target_cpu_init(env, regs);
628 
629     if (gdbstub) {
630         gdbserver_start(gdbstub);
631         gdb_handlesig(cpu, 0, NULL, NULL, 0);
632     }
633     cpu_loop(env);
634     /* never exits */
635     return 0;
636 }
637