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