2 * SPDX-License-Identifier: 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
30 * Copyright (c) 2011 Google, Inc.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 #include <sys/cdefs.h>
56 #include <sys/ioctl.h>
59 #include <sys/queue.h>
61 #include <machine/specialreg.h>
62 #include <machine/vmm.h>
85 #define MB (1024 * 1024UL)
86 #define GB (1024 * 1024 * 1024UL)
91 static char *host_base;
92 static struct termios term, oldterm;
93 static int disk_fd[NDISKS];
95 static int consin_fd, consout_fd;
97 static int need_reinit;
99 static void *loader_hdl;
101 static int explicit_loader;
104 static char *vmname, *progname;
105 static struct vmctx *ctx;
106 static struct vcpu *vcpu;
108 static uint64_t gdtbase, cr3, rsp;
110 static void cb_exit(void *arg, int v);
113 * Console i/o callbacks
117 cb_putc(void *arg __unused, int ch)
121 (void) write(consout_fd, &c, 1);
125 cb_getc(void *arg __unused)
129 if (read(consin_fd, &c, 1) == 1)
135 cb_poll(void *arg __unused)
139 if (ioctl(consin_fd, FIONREAD, &n) >= 0)
145 * Host filesystem i/o callbacks
159 cb_open(void *arg __unused, const char *filename, void **hp)
167 strlcpy(path, host_base, PATH_MAX);
168 if (path[strlen(path) - 1] == '/')
169 path[strlen(path) - 1] = 0;
170 strlcat(path, filename, PATH_MAX);
171 cf = malloc(sizeof(struct cb_file));
172 if (stat(path, &cf->cf_stat) < 0) {
177 cf->cf_size = cf->cf_stat.st_size;
178 if (S_ISDIR(cf->cf_stat.st_mode)) {
180 cf->cf_u.dir = opendir(path);
186 if (S_ISREG(cf->cf_stat.st_mode)) {
188 cf->cf_u.fd = open(path, O_RDONLY);
201 cb_close(void *arg __unused, void *h)
203 struct cb_file *cf = h;
206 closedir(cf->cf_u.dir);
215 cb_isdir(void *arg __unused, void *h)
217 struct cb_file *cf = h;
219 return (cf->cf_isdir);
223 cb_read(void *arg __unused, void *h, void *buf, size_t size, size_t *resid)
225 struct cb_file *cf = h;
230 sz = read(cf->cf_u.fd, buf, size);
238 cb_readdir(void *arg __unused, void *h, uint32_t *fileno_return,
239 uint8_t *type_return, size_t *namelen_return, char *name)
241 struct cb_file *cf = h;
247 dp = readdir(cf->cf_u.dir);
252 * Note: d_namlen is in the range 0..255 and therefore less
253 * than PATH_MAX so we don't need to test before copying.
255 *fileno_return = dp->d_fileno;
256 *type_return = dp->d_type;
257 *namelen_return = dp->d_namlen;
258 memcpy(name, dp->d_name, dp->d_namlen);
259 name[dp->d_namlen] = 0;
265 cb_seek(void *arg __unused, void *h, uint64_t offset, int whence)
267 struct cb_file *cf = h;
271 if (lseek(cf->cf_u.fd, offset, whence) < 0)
277 cb_stat(void *arg __unused, void *h, struct stat *sbp)
279 struct cb_file *cf = h;
281 memset(sbp, 0, sizeof(struct stat));
282 sbp->st_mode = cf->cf_stat.st_mode;
283 sbp->st_uid = cf->cf_stat.st_uid;
284 sbp->st_gid = cf->cf_stat.st_gid;
285 sbp->st_size = cf->cf_stat.st_size;
286 sbp->st_mtime = cf->cf_stat.st_mtime;
287 sbp->st_dev = cf->cf_stat.st_dev;
288 sbp->st_ino = cf->cf_stat.st_ino;
294 * Disk image i/o callbacks
298 cb_diskread(void *arg __unused, int unit, uint64_t from, void *to, size_t size,
303 if (unit < 0 || unit >= ndisks)
305 n = pread(disk_fd[unit], to, size, from);
313 cb_diskwrite(void *arg __unused, int unit, uint64_t offset, void *src,
314 size_t size, size_t *resid)
318 if (unit < 0 || unit >= ndisks)
320 n = pwrite(disk_fd[unit], src, size, offset);
328 cb_diskioctl(void *arg __unused, int unit, u_long cmd, void *data)
332 if (unit < 0 || unit >= ndisks)
336 case DIOCGSECTORSIZE:
337 *(u_int *)data = 512;
340 if (fstat(disk_fd[unit], &sb) != 0)
342 if (S_ISCHR(sb.st_mode) &&
343 ioctl(disk_fd[unit], DIOCGMEDIASIZE, &sb.st_size) != 0)
345 *(off_t *)data = sb.st_size;
355 * Guest virtual machine i/o callbacks
358 cb_copyin(void *arg __unused, const void *from, uint64_t to, size_t size)
364 ptr = vm_map_gpa(ctx, to, size);
368 memcpy(ptr, from, size);
373 cb_copyout(void *arg __unused, uint64_t from, void *to, size_t size)
379 ptr = vm_map_gpa(ctx, from, size);
383 memcpy(to, ptr, size);
388 cb_setreg(void *arg __unused, int r, uint64_t v)
391 enum vm_reg_name vmreg;
397 vmreg = VM_REG_GUEST_RSP;
404 if (vmreg == VM_REG_LAST) {
405 printf("test_setreg(%d): not implemented\n", r);
406 cb_exit(NULL, USERBOOT_EXIT_QUIT);
409 error = vm_set_register(vcpu, vmreg, v);
411 perror("vm_set_register");
412 cb_exit(NULL, USERBOOT_EXIT_QUIT);
417 cb_setmsr(void *arg __unused, int r, uint64_t v)
420 enum vm_reg_name vmreg;
426 vmreg = VM_REG_GUEST_EFER;
432 if (vmreg == VM_REG_LAST) {
433 printf("test_setmsr(%d): not implemented\n", r);
434 cb_exit(NULL, USERBOOT_EXIT_QUIT);
437 error = vm_set_register(vcpu, vmreg, v);
439 perror("vm_set_msr");
440 cb_exit(NULL, USERBOOT_EXIT_QUIT);
445 cb_setcr(void *arg __unused, int r, uint64_t v)
448 enum vm_reg_name vmreg;
454 vmreg = VM_REG_GUEST_CR0;
457 vmreg = VM_REG_GUEST_CR3;
461 vmreg = VM_REG_GUEST_CR4;
467 if (vmreg == VM_REG_LAST) {
468 printf("test_setcr(%d): not implemented\n", r);
469 cb_exit(NULL, USERBOOT_EXIT_QUIT);
472 error = vm_set_register(vcpu, vmreg, v);
475 cb_exit(NULL, USERBOOT_EXIT_QUIT);
480 cb_setgdt(void *arg __unused, uint64_t base, size_t size)
484 error = vm_set_desc(vcpu, VM_REG_GUEST_GDTR, base, size - 1, 0);
486 perror("vm_set_desc(gdt)");
487 cb_exit(NULL, USERBOOT_EXIT_QUIT);
494 cb_exec(void *arg __unused, uint64_t rip)
499 error = vm_setup_freebsd_registers_i386(vcpu, rip, gdtbase,
502 error = vm_setup_freebsd_registers(vcpu, rip, cr3, gdtbase,
505 perror("vm_setup_freebsd_registers");
506 cb_exit(NULL, USERBOOT_EXIT_QUIT);
517 cb_delay(void *arg __unused, int usec)
524 cb_exit(void *arg __unused, int v)
527 tcsetattr(consout_fd, TCSAFLUSH, &oldterm);
532 cb_getmem(void *arg __unused, uint64_t *ret_lowmem, uint64_t *ret_highmem)
535 *ret_lowmem = vm_get_lowmem_size(ctx);
536 *ret_highmem = vm_get_highmem_size(ctx);
540 char *str; /* name=value */
541 SLIST_ENTRY(env) next;
544 static SLIST_HEAD(envhead, env) envhead;
547 addenv(const char *str)
551 env = malloc(sizeof(struct env));
553 err(EX_OSERR, "malloc");
554 env->str = strdup(str);
555 if (env->str == NULL)
556 err(EX_OSERR, "strdup");
557 SLIST_INSERT_HEAD(&envhead, env, next);
561 cb_getenv(void *arg __unused, int num)
567 SLIST_FOREACH(env, &envhead, next) {
577 cb_vm_set_register(void *arg __unused, int vcpuid, int reg, uint64_t val)
580 assert(vcpuid == BSP);
581 return (vm_set_register(vcpu, reg, val));
585 cb_vm_set_desc(void *arg __unused, int vcpuid, int reg, uint64_t base,
586 u_int limit, u_int access)
589 assert(vcpuid == BSP);
590 return (vm_set_desc(vcpu, reg, base, limit, access));
594 cb_swap_interpreter(void *arg __unused, const char *interp_req)
598 * If the user specified a loader but we detected a mismatch, we should
599 * not try to pivot to a different loader on them.
602 if (explicit_loader == 1) {
603 perror("requested loader interpreter does not match guest userboot");
606 if (interp_req == NULL || *interp_req == '\0') {
607 perror("guest failed to request an interpreter");
611 if (asprintf(&loader, "/boot/userboot_%s.so", interp_req) == -1)
612 err(EX_OSERR, "malloc");
617 static struct loader_callbacks cb = {
626 .readdir = cb_readdir,
630 .diskread = cb_diskread,
631 .diskwrite = cb_diskwrite,
632 .diskioctl = cb_diskioctl,
635 .copyout = cb_copyout,
648 /* Version 4 additions */
649 .vm_set_register = cb_vm_set_register,
650 .vm_set_desc = cb_vm_set_desc,
652 /* Version 5 additions */
653 .swap_interpreter = cb_swap_interpreter,
657 altcons_open(char *path)
664 * Allow stdio to be passed in so that the same string
665 * can be used for the bhyveload console and bhyve com-port
668 if (!strcmp(path, "stdio"))
671 err = stat(path, &sb);
673 if (!S_ISCHR(sb.st_mode))
676 fd = open(path, O_RDWR | O_NONBLOCK);
680 consin_fd = consout_fd = fd;
688 disk_open(char *path)
692 if (ndisks >= NDISKS)
695 fd = open(path, O_RDWR);
699 disk_fd[ndisks] = fd;
710 "usage: %s [-S][-c <console-device>] [-d <disk-path>] [-e <name=value>]\n"
711 " %*s [-h <host-path>] [-m memsize[K|k|M|m|G|g|T|t]] <vmname>\n",
713 (int)strlen(progname), "");
718 main(int argc, char** argv)
720 void (*func)(struct loader_callbacks *, void *, int, int);
722 int opt, error, memflags;
724 progname = basename(argv[0]);
729 consin_fd = STDIN_FILENO;
730 consout_fd = STDOUT_FILENO;
732 while ((opt = getopt(argc, argv, "CSc:d:e:h:l:m:")) != -1) {
735 error = altcons_open(optarg);
737 errx(EX_USAGE, "Could not open '%s'", optarg);
741 error = disk_open(optarg);
743 errx(EX_USAGE, "Could not open '%s'", optarg);
756 errx(EX_USAGE, "-l can only be given once");
757 loader = strdup(optarg);
759 err(EX_OSERR, "malloc");
764 error = vm_parse_memsize(optarg, &mem_size);
766 errx(EX_USAGE, "Invalid memsize '%s'", optarg);
769 memflags |= VM_MEM_F_INCORE;
772 memflags |= VM_MEM_F_WIRED;
788 error = vm_create(vmname);
790 if (errno != EEXIST) {
797 ctx = vm_open(vmname);
803 vcpu = vm_vcpu_open(ctx, BSP);
806 * setjmp in the case the guest wants to swap out interpreter,
807 * cb_swap_interpreter will swap out loader as appropriate and set
808 * need_reinit so that we end up in a clean state once again.
813 error = vm_reinit(ctx);
820 vm_set_memflags(ctx, memflags);
821 error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
823 perror("vm_setup_memory");
827 if (loader == NULL) {
828 loader = strdup("/boot/userboot.so");
830 err(EX_OSERR, "malloc");
832 if (loader_hdl != NULL)
834 loader_hdl = dlopen(loader, RTLD_LOCAL);
836 printf("%s\n", dlerror());
840 func = dlsym(loader_hdl, "loader_main");
842 printf("%s\n", dlerror());
847 tcgetattr(consout_fd, &term);
850 term.c_cflag |= CLOCAL;
852 tcsetattr(consout_fd, TCSAFLUSH, &term);
854 addenv("smbios.bios.vendor=BHYVE");
855 addenv("boot_serial=1");
857 func(&cb, NULL, USERBOOT_VERSION_5, ndisks);