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_diskwrite(void *arg, int unit, uint64_t offset, void *src, size_t size,
322 if (unit < 0 || unit >= ndisks)
324 n = pwrite(disk_fd[unit], src, size, offset);
332 cb_diskioctl(void *arg, int unit, u_long cmd, void *data)
336 if (unit < 0 || unit >= ndisks)
340 case DIOCGSECTORSIZE:
341 *(u_int *)data = 512;
344 if (fstat(disk_fd[unit], &sb) != 0)
346 if (S_ISCHR(sb.st_mode) &&
347 ioctl(disk_fd[unit], DIOCGMEDIASIZE, &sb.st_size) != 0)
349 *(off_t *)data = sb.st_size;
359 * Guest virtual machine i/o callbacks
362 cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
368 ptr = vm_map_gpa(ctx, to, size);
372 memcpy(ptr, from, size);
377 cb_copyout(void *arg, uint64_t from, void *to, size_t size)
383 ptr = vm_map_gpa(ctx, from, size);
387 memcpy(to, ptr, size);
392 cb_setreg(void *arg, int r, uint64_t v)
395 enum vm_reg_name vmreg;
401 vmreg = VM_REG_GUEST_RSP;
408 if (vmreg == VM_REG_LAST) {
409 printf("test_setreg(%d): not implemented\n", r);
410 cb_exit(NULL, USERBOOT_EXIT_QUIT);
413 error = vm_set_register(ctx, BSP, vmreg, v);
415 perror("vm_set_register");
416 cb_exit(NULL, USERBOOT_EXIT_QUIT);
421 cb_setmsr(void *arg, int r, uint64_t v)
424 enum vm_reg_name vmreg;
430 vmreg = VM_REG_GUEST_EFER;
436 if (vmreg == VM_REG_LAST) {
437 printf("test_setmsr(%d): not implemented\n", r);
438 cb_exit(NULL, USERBOOT_EXIT_QUIT);
441 error = vm_set_register(ctx, BSP, vmreg, v);
443 perror("vm_set_msr");
444 cb_exit(NULL, USERBOOT_EXIT_QUIT);
449 cb_setcr(void *arg, int r, uint64_t v)
452 enum vm_reg_name vmreg;
458 vmreg = VM_REG_GUEST_CR0;
461 vmreg = VM_REG_GUEST_CR3;
465 vmreg = VM_REG_GUEST_CR4;
471 if (vmreg == VM_REG_LAST) {
472 printf("test_setcr(%d): not implemented\n", r);
473 cb_exit(NULL, USERBOOT_EXIT_QUIT);
476 error = vm_set_register(ctx, BSP, vmreg, v);
479 cb_exit(NULL, USERBOOT_EXIT_QUIT);
484 cb_setgdt(void *arg, uint64_t base, size_t size)
488 error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
490 perror("vm_set_desc(gdt)");
491 cb_exit(NULL, USERBOOT_EXIT_QUIT);
498 cb_exec(void *arg, uint64_t rip)
503 error = vm_setup_freebsd_registers_i386(ctx, BSP, rip, gdtbase,
506 error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase,
509 perror("vm_setup_freebsd_registers");
510 cb_exit(NULL, USERBOOT_EXIT_QUIT);
521 cb_delay(void *arg, int usec)
528 cb_exit(void *arg, int v)
531 tcsetattr(consout_fd, TCSAFLUSH, &oldterm);
536 cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
539 *ret_lowmem = vm_get_lowmem_size(ctx);
540 *ret_highmem = vm_get_highmem_size(ctx);
544 char *str; /* name=value */
545 SLIST_ENTRY(env) next;
548 static SLIST_HEAD(envhead, env) envhead;
555 env = malloc(sizeof(struct env));
557 SLIST_INSERT_HEAD(&envhead, env, next);
561 cb_getenv(void *arg, int num)
567 SLIST_FOREACH(env, &envhead, next) {
577 cb_vm_set_register(void *arg, int vcpu, int reg, uint64_t val)
580 return (vm_set_register(ctx, vcpu, reg, val));
584 cb_vm_set_desc(void *arg, int vcpu, int reg, uint64_t base, u_int limit,
588 return (vm_set_desc(ctx, vcpu, reg, base, limit, access));
592 cb_swap_interpreter(void *arg, const char *interp_req)
596 * If the user specified a loader but we detected a mismatch, we should
597 * not try to pivot to a different loader on them.
600 if (explicit_loader == 1) {
601 perror("requested loader interpreter does not match guest userboot");
604 if (interp_req == NULL || *interp_req == '\0') {
605 perror("guest failed to request an interpreter");
609 if (asprintf(&loader, "/boot/userboot_%s.so", interp_req) == -1)
610 err(EX_OSERR, "malloc");
615 static struct loader_callbacks cb = {
624 .readdir = cb_readdir,
628 .diskread = cb_diskread,
629 .diskwrite = cb_diskwrite,
630 .diskioctl = cb_diskioctl,
633 .copyout = cb_copyout,
646 /* Version 4 additions */
647 .vm_set_register = cb_vm_set_register,
648 .vm_set_desc = cb_vm_set_desc,
650 /* Version 5 additions */
651 .swap_interpreter = cb_swap_interpreter,
655 altcons_open(char *path)
662 * Allow stdio to be passed in so that the same string
663 * can be used for the bhyveload console and bhyve com-port
666 if (!strcmp(path, "stdio"))
669 err = stat(path, &sb);
671 if (!S_ISCHR(sb.st_mode))
674 fd = open(path, O_RDWR | O_NONBLOCK);
678 consin_fd = consout_fd = fd;
686 disk_open(char *path)
690 if (ndisks >= NDISKS)
693 fd = open(path, O_RDONLY);
697 disk_fd[ndisks] = fd;
708 "usage: %s [-S][-c <console-device>] [-d <disk-path>] [-e <name=value>]\n"
709 " %*s [-h <host-path>] [-m memsize[K|k|M|m|G|g|T|t]] <vmname>\n",
711 (int)strlen(progname), "");
716 main(int argc, char** argv)
718 void (*func)(struct loader_callbacks *, void *, int, int);
720 int opt, error, memflags;
722 progname = basename(argv[0]);
727 consin_fd = STDIN_FILENO;
728 consout_fd = STDOUT_FILENO;
730 while ((opt = getopt(argc, argv, "CSc:d:e:h:l:m:")) != -1) {
733 error = altcons_open(optarg);
735 errx(EX_USAGE, "Could not open '%s'", optarg);
739 error = disk_open(optarg);
741 errx(EX_USAGE, "Could not open '%s'", optarg);
754 errx(EX_USAGE, "-l can only be given once");
755 loader = strdup(optarg);
757 err(EX_OSERR, "malloc");
762 error = vm_parse_memsize(optarg, &mem_size);
764 errx(EX_USAGE, "Invalid memsize '%s'", optarg);
767 memflags |= VM_MEM_F_INCORE;
770 memflags |= VM_MEM_F_WIRED;
786 error = vm_create(vmname);
788 if (errno != EEXIST) {
795 ctx = vm_open(vmname);
802 * setjmp in the case the guest wants to swap out interpreter,
803 * cb_swap_interpreter will swap out loader as appropriate and set
804 * need_reinit so that we end up in a clean state once again.
809 error = vm_reinit(ctx);
816 vm_set_memflags(ctx, memflags);
817 error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
819 perror("vm_setup_memory");
823 if (loader == NULL) {
824 loader = strdup("/boot/userboot.so");
826 err(EX_OSERR, "malloc");
828 if (loader_hdl != NULL)
830 loader_hdl = dlopen(loader, RTLD_LOCAL);
832 printf("%s\n", dlerror());
836 func = dlsym(loader_hdl, "loader_main");
838 printf("%s\n", dlerror());
843 tcgetattr(consout_fd, &term);
846 term.c_cflag |= CLOCAL;
848 tcsetattr(consout_fd, TCSAFLUSH, &term);
850 addenv("smbios.bios.vendor=BHYVE");
851 addenv("boot_serial=1");
853 func(&cb, NULL, USERBOOT_VERSION_5, ndisks);