2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause
4 * Copyright (c) 2011 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Copyright (c) 2011 Google, Inc.
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
62 #include <sys/ioctl.h>
65 #include <sys/queue.h>
67 #include <machine/specialreg.h>
68 #include <machine/vmm.h>
91 #define MB (1024 * 1024UL)
92 #define GB (1024 * 1024 * 1024UL)
97 static char *host_base;
98 static struct termios term, oldterm;
99 static int disk_fd[NDISKS];
101 static int consin_fd, consout_fd;
103 static int need_reinit;
105 static void *loader_hdl;
107 static int explicit_loader;
110 static char *vmname, *progname;
111 static struct vmctx *ctx;
112 static struct vcpu *vcpu;
114 static uint64_t gdtbase, cr3, rsp;
116 static void cb_exit(void *arg, int v);
119 * Console i/o callbacks
123 cb_putc(void *arg __unused, int ch)
127 (void) write(consout_fd, &c, 1);
131 cb_getc(void *arg __unused)
135 if (read(consin_fd, &c, 1) == 1)
141 cb_poll(void *arg __unused)
145 if (ioctl(consin_fd, FIONREAD, &n) >= 0)
151 * Host filesystem i/o callbacks
165 cb_open(void *arg __unused, const char *filename, void **hp)
173 strlcpy(path, host_base, PATH_MAX);
174 if (path[strlen(path) - 1] == '/')
175 path[strlen(path) - 1] = 0;
176 strlcat(path, filename, PATH_MAX);
177 cf = malloc(sizeof(struct cb_file));
178 if (stat(path, &cf->cf_stat) < 0) {
183 cf->cf_size = cf->cf_stat.st_size;
184 if (S_ISDIR(cf->cf_stat.st_mode)) {
186 cf->cf_u.dir = opendir(path);
192 if (S_ISREG(cf->cf_stat.st_mode)) {
194 cf->cf_u.fd = open(path, O_RDONLY);
207 cb_close(void *arg __unused, void *h)
209 struct cb_file *cf = h;
212 closedir(cf->cf_u.dir);
221 cb_isdir(void *arg __unused, void *h)
223 struct cb_file *cf = h;
225 return (cf->cf_isdir);
229 cb_read(void *arg __unused, void *h, void *buf, size_t size, size_t *resid)
231 struct cb_file *cf = h;
236 sz = read(cf->cf_u.fd, buf, size);
244 cb_readdir(void *arg __unused, void *h, uint32_t *fileno_return,
245 uint8_t *type_return, size_t *namelen_return, char *name)
247 struct cb_file *cf = h;
253 dp = readdir(cf->cf_u.dir);
258 * Note: d_namlen is in the range 0..255 and therefore less
259 * than PATH_MAX so we don't need to test before copying.
261 *fileno_return = dp->d_fileno;
262 *type_return = dp->d_type;
263 *namelen_return = dp->d_namlen;
264 memcpy(name, dp->d_name, dp->d_namlen);
265 name[dp->d_namlen] = 0;
271 cb_seek(void *arg __unused, void *h, uint64_t offset, int whence)
273 struct cb_file *cf = h;
277 if (lseek(cf->cf_u.fd, offset, whence) < 0)
283 cb_stat(void *arg __unused, void *h, struct stat *sbp)
285 struct cb_file *cf = h;
287 memset(sbp, 0, sizeof(struct stat));
288 sbp->st_mode = cf->cf_stat.st_mode;
289 sbp->st_uid = cf->cf_stat.st_uid;
290 sbp->st_gid = cf->cf_stat.st_gid;
291 sbp->st_size = cf->cf_stat.st_size;
292 sbp->st_mtime = cf->cf_stat.st_mtime;
293 sbp->st_dev = cf->cf_stat.st_dev;
294 sbp->st_ino = cf->cf_stat.st_ino;
300 * Disk image i/o callbacks
304 cb_diskread(void *arg __unused, int unit, uint64_t from, void *to, size_t size,
309 if (unit < 0 || unit >= ndisks)
311 n = pread(disk_fd[unit], to, size, from);
319 cb_diskwrite(void *arg __unused, int unit, uint64_t offset, void *src,
320 size_t size, size_t *resid)
324 if (unit < 0 || unit >= ndisks)
326 n = pwrite(disk_fd[unit], src, size, offset);
334 cb_diskioctl(void *arg __unused, int unit, u_long cmd, void *data)
338 if (unit < 0 || unit >= ndisks)
342 case DIOCGSECTORSIZE:
343 *(u_int *)data = 512;
346 if (fstat(disk_fd[unit], &sb) != 0)
348 if (S_ISCHR(sb.st_mode) &&
349 ioctl(disk_fd[unit], DIOCGMEDIASIZE, &sb.st_size) != 0)
351 *(off_t *)data = sb.st_size;
361 * Guest virtual machine i/o callbacks
364 cb_copyin(void *arg __unused, const void *from, uint64_t to, size_t size)
370 ptr = vm_map_gpa(ctx, to, size);
374 memcpy(ptr, from, size);
379 cb_copyout(void *arg __unused, uint64_t from, void *to, size_t size)
385 ptr = vm_map_gpa(ctx, from, size);
389 memcpy(to, ptr, size);
394 cb_setreg(void *arg __unused, int r, uint64_t v)
397 enum vm_reg_name vmreg;
403 vmreg = VM_REG_GUEST_RSP;
410 if (vmreg == VM_REG_LAST) {
411 printf("test_setreg(%d): not implemented\n", r);
412 cb_exit(NULL, USERBOOT_EXIT_QUIT);
415 error = vm_set_register(vcpu, vmreg, v);
417 perror("vm_set_register");
418 cb_exit(NULL, USERBOOT_EXIT_QUIT);
423 cb_setmsr(void *arg __unused, int r, uint64_t v)
426 enum vm_reg_name vmreg;
432 vmreg = VM_REG_GUEST_EFER;
438 if (vmreg == VM_REG_LAST) {
439 printf("test_setmsr(%d): not implemented\n", r);
440 cb_exit(NULL, USERBOOT_EXIT_QUIT);
443 error = vm_set_register(vcpu, vmreg, v);
445 perror("vm_set_msr");
446 cb_exit(NULL, USERBOOT_EXIT_QUIT);
451 cb_setcr(void *arg __unused, int r, uint64_t v)
454 enum vm_reg_name vmreg;
460 vmreg = VM_REG_GUEST_CR0;
463 vmreg = VM_REG_GUEST_CR3;
467 vmreg = VM_REG_GUEST_CR4;
473 if (vmreg == VM_REG_LAST) {
474 printf("test_setcr(%d): not implemented\n", r);
475 cb_exit(NULL, USERBOOT_EXIT_QUIT);
478 error = vm_set_register(vcpu, vmreg, v);
481 cb_exit(NULL, USERBOOT_EXIT_QUIT);
486 cb_setgdt(void *arg __unused, uint64_t base, size_t size)
490 error = vm_set_desc(vcpu, VM_REG_GUEST_GDTR, base, size - 1, 0);
492 perror("vm_set_desc(gdt)");
493 cb_exit(NULL, USERBOOT_EXIT_QUIT);
500 cb_exec(void *arg __unused, uint64_t rip)
505 error = vm_setup_freebsd_registers_i386(vcpu, rip, gdtbase,
508 error = vm_setup_freebsd_registers(vcpu, rip, cr3, gdtbase,
511 perror("vm_setup_freebsd_registers");
512 cb_exit(NULL, USERBOOT_EXIT_QUIT);
523 cb_delay(void *arg __unused, int usec)
530 cb_exit(void *arg __unused, int v)
533 tcsetattr(consout_fd, TCSAFLUSH, &oldterm);
538 cb_getmem(void *arg __unused, uint64_t *ret_lowmem, uint64_t *ret_highmem)
541 *ret_lowmem = vm_get_lowmem_size(ctx);
542 *ret_highmem = vm_get_highmem_size(ctx);
546 char *str; /* name=value */
547 SLIST_ENTRY(env) next;
550 static SLIST_HEAD(envhead, env) envhead;
553 addenv(const char *str)
557 env = malloc(sizeof(struct env));
559 err(EX_OSERR, "malloc");
560 env->str = strdup(str);
561 if (env->str == NULL)
562 err(EX_OSERR, "strdup");
563 SLIST_INSERT_HEAD(&envhead, env, next);
567 cb_getenv(void *arg __unused, int num)
573 SLIST_FOREACH(env, &envhead, next) {
583 cb_vm_set_register(void *arg __unused, int vcpuid, int reg, uint64_t val)
586 assert(vcpuid == BSP);
587 return (vm_set_register(vcpu, reg, val));
591 cb_vm_set_desc(void *arg __unused, int vcpuid, int reg, uint64_t base,
592 u_int limit, u_int access)
595 assert(vcpuid == BSP);
596 return (vm_set_desc(vcpu, reg, base, limit, access));
600 cb_swap_interpreter(void *arg __unused, const char *interp_req)
604 * If the user specified a loader but we detected a mismatch, we should
605 * not try to pivot to a different loader on them.
608 if (explicit_loader == 1) {
609 perror("requested loader interpreter does not match guest userboot");
612 if (interp_req == NULL || *interp_req == '\0') {
613 perror("guest failed to request an interpreter");
617 if (asprintf(&loader, "/boot/userboot_%s.so", interp_req) == -1)
618 err(EX_OSERR, "malloc");
623 static struct loader_callbacks cb = {
632 .readdir = cb_readdir,
636 .diskread = cb_diskread,
637 .diskwrite = cb_diskwrite,
638 .diskioctl = cb_diskioctl,
641 .copyout = cb_copyout,
654 /* Version 4 additions */
655 .vm_set_register = cb_vm_set_register,
656 .vm_set_desc = cb_vm_set_desc,
658 /* Version 5 additions */
659 .swap_interpreter = cb_swap_interpreter,
663 altcons_open(char *path)
670 * Allow stdio to be passed in so that the same string
671 * can be used for the bhyveload console and bhyve com-port
674 if (!strcmp(path, "stdio"))
677 err = stat(path, &sb);
679 if (!S_ISCHR(sb.st_mode))
682 fd = open(path, O_RDWR | O_NONBLOCK);
686 consin_fd = consout_fd = fd;
694 disk_open(char *path)
698 if (ndisks >= NDISKS)
701 fd = open(path, O_RDWR);
705 disk_fd[ndisks] = fd;
716 "usage: %s [-S][-c <console-device>] [-d <disk-path>] [-e <name=value>]\n"
717 " %*s [-h <host-path>] [-m memsize[K|k|M|m|G|g|T|t]] <vmname>\n",
719 (int)strlen(progname), "");
724 main(int argc, char** argv)
726 void (*func)(struct loader_callbacks *, void *, int, int);
728 int opt, error, memflags;
730 progname = basename(argv[0]);
735 consin_fd = STDIN_FILENO;
736 consout_fd = STDOUT_FILENO;
738 while ((opt = getopt(argc, argv, "CSc:d:e:h:l:m:")) != -1) {
741 error = altcons_open(optarg);
743 errx(EX_USAGE, "Could not open '%s'", optarg);
747 error = disk_open(optarg);
749 errx(EX_USAGE, "Could not open '%s'", optarg);
762 errx(EX_USAGE, "-l can only be given once");
763 loader = strdup(optarg);
765 err(EX_OSERR, "malloc");
770 error = vm_parse_memsize(optarg, &mem_size);
772 errx(EX_USAGE, "Invalid memsize '%s'", optarg);
775 memflags |= VM_MEM_F_INCORE;
778 memflags |= VM_MEM_F_WIRED;
794 error = vm_create(vmname);
796 if (errno != EEXIST) {
803 ctx = vm_open(vmname);
809 vcpu = vm_vcpu_open(ctx, BSP);
812 * setjmp in the case the guest wants to swap out interpreter,
813 * cb_swap_interpreter will swap out loader as appropriate and set
814 * need_reinit so that we end up in a clean state once again.
819 error = vm_reinit(ctx);
826 vm_set_memflags(ctx, memflags);
827 error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
829 perror("vm_setup_memory");
833 if (loader == NULL) {
834 loader = strdup("/boot/userboot.so");
836 err(EX_OSERR, "malloc");
838 if (loader_hdl != NULL)
840 loader_hdl = dlopen(loader, RTLD_LOCAL);
842 printf("%s\n", dlerror());
846 func = dlsym(loader_hdl, "loader_main");
848 printf("%s\n", dlerror());
853 tcgetattr(consout_fd, &term);
856 term.c_cflag |= CLOCAL;
858 tcsetattr(consout_fd, TCSAFLUSH, &term);
860 addenv("smbios.bios.vendor=BHYVE");
861 addenv("boot_serial=1");
863 func(&cb, NULL, USERBOOT_VERSION_5, ndisks);