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>
90 #define MB (1024 * 1024UL)
91 #define GB (1024 * 1024 * 1024UL)
96 static char *host_base;
97 static struct termios term, oldterm;
98 static int disk_fd[NDISKS];
100 static int consin_fd, consout_fd;
102 static int need_reinit;
104 static void *loader_hdl;
106 static int explicit_loader;
109 static char *vmname, *progname;
110 static struct vmctx *ctx;
112 static uint64_t gdtbase, cr3, rsp;
114 static void cb_exit(void *arg, int v);
117 * Console i/o callbacks
121 cb_putc(void *arg, int ch)
125 (void) write(consout_fd, &c, 1);
133 if (read(consin_fd, &c, 1) == 1)
143 if (ioctl(consin_fd, FIONREAD, &n) >= 0)
149 * Host filesystem i/o callbacks
163 cb_open(void *arg, const char *filename, void **hp)
171 strlcpy(path, host_base, PATH_MAX);
172 if (path[strlen(path) - 1] == '/')
173 path[strlen(path) - 1] = 0;
174 strlcat(path, filename, PATH_MAX);
175 cf = malloc(sizeof(struct cb_file));
176 if (stat(path, &cf->cf_stat) < 0) {
181 cf->cf_size = cf->cf_stat.st_size;
182 if (S_ISDIR(cf->cf_stat.st_mode)) {
184 cf->cf_u.dir = opendir(path);
190 if (S_ISREG(cf->cf_stat.st_mode)) {
192 cf->cf_u.fd = open(path, O_RDONLY);
205 cb_close(void *arg, void *h)
207 struct cb_file *cf = h;
210 closedir(cf->cf_u.dir);
219 cb_isdir(void *arg, void *h)
221 struct cb_file *cf = h;
223 return (cf->cf_isdir);
227 cb_read(void *arg, void *h, void *buf, size_t size, size_t *resid)
229 struct cb_file *cf = h;
234 sz = read(cf->cf_u.fd, buf, size);
242 cb_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
243 size_t *namelen_return, char *name)
245 struct cb_file *cf = h;
251 dp = readdir(cf->cf_u.dir);
256 * Note: d_namlen is in the range 0..255 and therefore less
257 * than PATH_MAX so we don't need to test before copying.
259 *fileno_return = dp->d_fileno;
260 *type_return = dp->d_type;
261 *namelen_return = dp->d_namlen;
262 memcpy(name, dp->d_name, dp->d_namlen);
263 name[dp->d_namlen] = 0;
269 cb_seek(void *arg, void *h, uint64_t offset, int whence)
271 struct cb_file *cf = h;
275 if (lseek(cf->cf_u.fd, offset, whence) < 0)
281 cb_stat(void *arg, void *h, struct stat *sbp)
283 struct cb_file *cf = h;
285 memset(sbp, 0, sizeof(struct stat));
286 sbp->st_mode = cf->cf_stat.st_mode;
287 sbp->st_uid = cf->cf_stat.st_uid;
288 sbp->st_gid = cf->cf_stat.st_gid;
289 sbp->st_size = cf->cf_stat.st_size;
290 sbp->st_mtime = cf->cf_stat.st_mtime;
291 sbp->st_dev = cf->cf_stat.st_dev;
292 sbp->st_ino = cf->cf_stat.st_ino;
298 * Disk image i/o callbacks
302 cb_diskread(void *arg, int unit, uint64_t from, void *to, size_t size,
307 if (unit < 0 || unit >= ndisks )
309 n = pread(disk_fd[unit], to, size, from);
317 cb_diskioctl(void *arg, int unit, u_long cmd, void *data)
321 if (unit < 0 || unit >= ndisks)
325 case DIOCGSECTORSIZE:
326 *(u_int *)data = 512;
329 if (fstat(disk_fd[unit], &sb) != 0)
331 if (S_ISCHR(sb.st_mode) &&
332 ioctl(disk_fd[unit], DIOCGMEDIASIZE, &sb.st_size) != 0)
334 *(off_t *)data = sb.st_size;
344 * Guest virtual machine i/o callbacks
347 cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
353 ptr = vm_map_gpa(ctx, to, size);
357 memcpy(ptr, from, size);
362 cb_copyout(void *arg, uint64_t from, void *to, size_t size)
368 ptr = vm_map_gpa(ctx, from, size);
372 memcpy(to, ptr, size);
377 cb_setreg(void *arg, int r, uint64_t v)
380 enum vm_reg_name vmreg;
386 vmreg = VM_REG_GUEST_RSP;
393 if (vmreg == VM_REG_LAST) {
394 printf("test_setreg(%d): not implemented\n", r);
395 cb_exit(NULL, USERBOOT_EXIT_QUIT);
398 error = vm_set_register(ctx, BSP, vmreg, v);
400 perror("vm_set_register");
401 cb_exit(NULL, USERBOOT_EXIT_QUIT);
406 cb_setmsr(void *arg, int r, uint64_t v)
409 enum vm_reg_name vmreg;
415 vmreg = VM_REG_GUEST_EFER;
421 if (vmreg == VM_REG_LAST) {
422 printf("test_setmsr(%d): not implemented\n", r);
423 cb_exit(NULL, USERBOOT_EXIT_QUIT);
426 error = vm_set_register(ctx, BSP, vmreg, v);
428 perror("vm_set_msr");
429 cb_exit(NULL, USERBOOT_EXIT_QUIT);
434 cb_setcr(void *arg, int r, uint64_t v)
437 enum vm_reg_name vmreg;
443 vmreg = VM_REG_GUEST_CR0;
446 vmreg = VM_REG_GUEST_CR3;
450 vmreg = VM_REG_GUEST_CR4;
456 if (vmreg == VM_REG_LAST) {
457 printf("test_setcr(%d): not implemented\n", r);
458 cb_exit(NULL, USERBOOT_EXIT_QUIT);
461 error = vm_set_register(ctx, BSP, vmreg, v);
464 cb_exit(NULL, USERBOOT_EXIT_QUIT);
469 cb_setgdt(void *arg, uint64_t base, size_t size)
473 error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
475 perror("vm_set_desc(gdt)");
476 cb_exit(NULL, USERBOOT_EXIT_QUIT);
483 cb_exec(void *arg, uint64_t rip)
488 error = vm_setup_freebsd_registers_i386(ctx, BSP, rip, gdtbase,
491 error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase,
494 perror("vm_setup_freebsd_registers");
495 cb_exit(NULL, USERBOOT_EXIT_QUIT);
506 cb_delay(void *arg, int usec)
513 cb_exit(void *arg, int v)
516 tcsetattr(consout_fd, TCSAFLUSH, &oldterm);
521 cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
524 *ret_lowmem = vm_get_lowmem_size(ctx);
525 *ret_highmem = vm_get_highmem_size(ctx);
529 char *str; /* name=value */
530 SLIST_ENTRY(env) next;
533 static SLIST_HEAD(envhead, env) envhead;
540 env = malloc(sizeof(struct env));
542 SLIST_INSERT_HEAD(&envhead, env, next);
546 cb_getenv(void *arg, int num)
552 SLIST_FOREACH(env, &envhead, next) {
562 cb_vm_set_register(void *arg, int vcpu, int reg, uint64_t val)
565 return (vm_set_register(ctx, vcpu, reg, val));
569 cb_vm_set_desc(void *arg, int vcpu, int reg, uint64_t base, u_int limit,
573 return (vm_set_desc(ctx, vcpu, reg, base, limit, access));
577 cb_swap_interpreter(void *arg, const char *interp_req)
581 * If the user specified a loader but we detected a mismatch, we should
582 * not try to pivot to a different loader on them.
585 if (explicit_loader == 1) {
586 perror("requested loader interpreter does not match guest userboot");
589 if (interp_req == NULL || *interp_req == '\0') {
590 perror("guest failed to request an interpreter");
594 if (asprintf(&loader, "/boot/userboot_%s.so", interp_req) == -1)
595 err(EX_OSERR, "malloc");
600 static struct loader_callbacks cb = {
609 .readdir = cb_readdir,
613 .diskread = cb_diskread,
614 .diskioctl = cb_diskioctl,
617 .copyout = cb_copyout,
630 /* Version 4 additions */
631 .vm_set_register = cb_vm_set_register,
632 .vm_set_desc = cb_vm_set_desc,
634 /* Version 5 additions */
635 .swap_interpreter = cb_swap_interpreter,
639 altcons_open(char *path)
646 * Allow stdio to be passed in so that the same string
647 * can be used for the bhyveload console and bhyve com-port
650 if (!strcmp(path, "stdio"))
653 err = stat(path, &sb);
655 if (!S_ISCHR(sb.st_mode))
658 fd = open(path, O_RDWR | O_NONBLOCK);
662 consin_fd = consout_fd = fd;
670 disk_open(char *path)
674 if (ndisks >= NDISKS)
677 fd = open(path, O_RDONLY);
681 disk_fd[ndisks] = fd;
692 "usage: %s [-S][-c <console-device>] [-d <disk-path>] [-e <name=value>]\n"
693 " %*s [-h <host-path>] [-m memsize[K|k|M|m|G|g|T|t]] <vmname>\n",
695 (int)strlen(progname), "");
700 main(int argc, char** argv)
702 void (*func)(struct loader_callbacks *, void *, int, int);
704 int opt, error, memflags;
706 progname = basename(argv[0]);
711 consin_fd = STDIN_FILENO;
712 consout_fd = STDOUT_FILENO;
714 while ((opt = getopt(argc, argv, "CSc:d:e:h:l:m:")) != -1) {
717 error = altcons_open(optarg);
719 errx(EX_USAGE, "Could not open '%s'", optarg);
723 error = disk_open(optarg);
725 errx(EX_USAGE, "Could not open '%s'", optarg);
738 errx(EX_USAGE, "-l can only be given once");
739 loader = strdup(optarg);
741 err(EX_OSERR, "malloc");
746 error = vm_parse_memsize(optarg, &mem_size);
748 errx(EX_USAGE, "Invalid memsize '%s'", optarg);
751 memflags |= VM_MEM_F_INCORE;
754 memflags |= VM_MEM_F_WIRED;
770 error = vm_create(vmname);
772 if (errno != EEXIST) {
779 ctx = vm_open(vmname);
786 * setjmp in the case the guest wants to swap out interpreter,
787 * cb_swap_interpreter will swap out loader as appropriate and set
788 * need_reinit so that we end up in a clean state once again.
793 error = vm_reinit(ctx);
800 vm_set_memflags(ctx, memflags);
801 error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
803 perror("vm_setup_memory");
807 if (loader == NULL) {
808 loader = strdup("/boot/userboot.so");
810 err(EX_OSERR, "malloc");
812 if (loader_hdl != NULL)
814 loader_hdl = dlopen(loader, RTLD_LOCAL);
816 printf("%s\n", dlerror());
820 func = dlsym(loader_hdl, "loader_main");
822 printf("%s\n", dlerror());
827 tcgetattr(consout_fd, &term);
830 term.c_cflag |= CLOCAL;
832 tcsetattr(consout_fd, TCSAFLUSH, &term);
834 addenv("smbios.bios.vendor=BHYVE");
835 addenv("boot_serial=1");
837 func(&cb, NULL, USERBOOT_VERSION_5, ndisks);