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, int *mode, int *uid, int *gid, uint64_t *size)
283 struct cb_file *cf = h;
285 *mode = cf->cf_stat.st_mode;
286 *uid = cf->cf_stat.st_uid;
287 *gid = cf->cf_stat.st_gid;
288 *size = cf->cf_stat.st_size;
293 * Disk image i/o callbacks
297 cb_diskread(void *arg, int unit, uint64_t from, void *to, size_t size,
302 if (unit < 0 || unit >= ndisks )
304 n = pread(disk_fd[unit], to, size, from);
312 cb_diskioctl(void *arg, int unit, u_long cmd, void *data)
316 if (unit < 0 || unit >= ndisks)
320 case DIOCGSECTORSIZE:
321 *(u_int *)data = 512;
324 if (fstat(disk_fd[unit], &sb) != 0)
326 if (S_ISCHR(sb.st_mode) &&
327 ioctl(disk_fd[unit], DIOCGMEDIASIZE, &sb.st_size) != 0)
329 *(off_t *)data = sb.st_size;
339 * Guest virtual machine i/o callbacks
342 cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
348 ptr = vm_map_gpa(ctx, to, size);
352 memcpy(ptr, from, size);
357 cb_copyout(void *arg, uint64_t from, void *to, size_t size)
363 ptr = vm_map_gpa(ctx, from, size);
367 memcpy(to, ptr, size);
372 cb_setreg(void *arg, int r, uint64_t v)
375 enum vm_reg_name vmreg;
381 vmreg = VM_REG_GUEST_RSP;
388 if (vmreg == VM_REG_LAST) {
389 printf("test_setreg(%d): not implemented\n", r);
390 cb_exit(NULL, USERBOOT_EXIT_QUIT);
393 error = vm_set_register(ctx, BSP, vmreg, v);
395 perror("vm_set_register");
396 cb_exit(NULL, USERBOOT_EXIT_QUIT);
401 cb_setmsr(void *arg, int r, uint64_t v)
404 enum vm_reg_name vmreg;
410 vmreg = VM_REG_GUEST_EFER;
416 if (vmreg == VM_REG_LAST) {
417 printf("test_setmsr(%d): not implemented\n", r);
418 cb_exit(NULL, USERBOOT_EXIT_QUIT);
421 error = vm_set_register(ctx, BSP, vmreg, v);
423 perror("vm_set_msr");
424 cb_exit(NULL, USERBOOT_EXIT_QUIT);
429 cb_setcr(void *arg, int r, uint64_t v)
432 enum vm_reg_name vmreg;
438 vmreg = VM_REG_GUEST_CR0;
441 vmreg = VM_REG_GUEST_CR3;
445 vmreg = VM_REG_GUEST_CR4;
451 if (vmreg == VM_REG_LAST) {
452 printf("test_setcr(%d): not implemented\n", r);
453 cb_exit(NULL, USERBOOT_EXIT_QUIT);
456 error = vm_set_register(ctx, BSP, vmreg, v);
459 cb_exit(NULL, USERBOOT_EXIT_QUIT);
464 cb_setgdt(void *arg, uint64_t base, size_t size)
468 error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
470 perror("vm_set_desc(gdt)");
471 cb_exit(NULL, USERBOOT_EXIT_QUIT);
478 cb_exec(void *arg, uint64_t rip)
483 error = vm_setup_freebsd_registers_i386(ctx, BSP, rip, gdtbase,
486 error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase,
489 perror("vm_setup_freebsd_registers");
490 cb_exit(NULL, USERBOOT_EXIT_QUIT);
501 cb_delay(void *arg, int usec)
508 cb_exit(void *arg, int v)
511 tcsetattr(consout_fd, TCSAFLUSH, &oldterm);
516 cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
519 *ret_lowmem = vm_get_lowmem_size(ctx);
520 *ret_highmem = vm_get_highmem_size(ctx);
524 char *str; /* name=value */
525 SLIST_ENTRY(env) next;
528 static SLIST_HEAD(envhead, env) envhead;
535 env = malloc(sizeof(struct env));
537 SLIST_INSERT_HEAD(&envhead, env, next);
541 cb_getenv(void *arg, int num)
547 SLIST_FOREACH(env, &envhead, next) {
557 cb_vm_set_register(void *arg, int vcpu, int reg, uint64_t val)
560 return (vm_set_register(ctx, vcpu, reg, val));
564 cb_vm_set_desc(void *arg, int vcpu, int reg, uint64_t base, u_int limit,
568 return (vm_set_desc(ctx, vcpu, reg, base, limit, access));
572 cb_swap_interpreter(void *arg, const char *interp_req)
576 * If the user specified a loader but we detected a mismatch, we should
577 * not try to pivot to a different loader on them.
580 if (explicit_loader == 1) {
581 perror("requested loader interpreter does not match guest userboot");
584 if (interp_req == NULL || *interp_req == '\0') {
585 perror("guest failed to request an interpreter");
589 if (asprintf(&loader, "/boot/userboot_%s.so", interp_req) == -1)
590 err(EX_OSERR, "malloc");
595 static struct loader_callbacks cb = {
604 .readdir = cb_readdir,
608 .diskread = cb_diskread,
609 .diskioctl = cb_diskioctl,
612 .copyout = cb_copyout,
625 /* Version 4 additions */
626 .vm_set_register = cb_vm_set_register,
627 .vm_set_desc = cb_vm_set_desc,
629 /* Version 5 additions */
630 .swap_interpreter = cb_swap_interpreter,
634 altcons_open(char *path)
641 * Allow stdio to be passed in so that the same string
642 * can be used for the bhyveload console and bhyve com-port
645 if (!strcmp(path, "stdio"))
648 err = stat(path, &sb);
650 if (!S_ISCHR(sb.st_mode))
653 fd = open(path, O_RDWR | O_NONBLOCK);
657 consin_fd = consout_fd = fd;
665 disk_open(char *path)
669 if (ndisks >= NDISKS)
673 fd = open(path, O_RDONLY);
676 disk_fd[ndisks] = fd;
689 "usage: %s [-S][-c <console-device>] [-d <disk-path>] [-e <name=value>]\n"
690 " %*s [-h <host-path>] [-m memsize[K|k|M|m|G|g|T|t]] <vmname>\n",
692 (int)strlen(progname), "");
697 main(int argc, char** argv)
699 void (*func)(struct loader_callbacks *, void *, int, int);
701 int opt, error, memflags;
703 progname = basename(argv[0]);
708 consin_fd = STDIN_FILENO;
709 consout_fd = STDOUT_FILENO;
711 while ((opt = getopt(argc, argv, "CSc:d:e:h:l:m:")) != -1) {
714 error = altcons_open(optarg);
716 errx(EX_USAGE, "Could not open '%s'", optarg);
720 error = disk_open(optarg);
722 errx(EX_USAGE, "Could not open '%s'", optarg);
735 errx(EX_USAGE, "-l can only be given once");
736 loader = strdup(optarg);
738 err(EX_OSERR, "malloc");
743 error = vm_parse_memsize(optarg, &mem_size);
745 errx(EX_USAGE, "Invalid memsize '%s'", optarg);
748 memflags |= VM_MEM_F_INCORE;
751 memflags |= VM_MEM_F_WIRED;
767 error = vm_create(vmname);
769 if (errno != EEXIST) {
776 ctx = vm_open(vmname);
783 * setjmp in the case the guest wants to swap out interpreter,
784 * cb_swap_interpreter will swap out loader as appropriate and set
785 * need_reinit so that we end up in a clean state once again.
790 error = vm_reinit(ctx);
797 vm_set_memflags(ctx, memflags);
798 error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
800 perror("vm_setup_memory");
804 if (loader == NULL) {
805 loader = strdup("/boot/userboot.so");
807 err(EX_OSERR, "malloc");
809 if (loader_hdl != NULL)
811 loader_hdl = dlopen(loader, RTLD_LOCAL);
813 printf("%s\n", dlerror());
817 func = dlsym(loader_hdl, "loader_main");
819 printf("%s\n", dlerror());
824 tcgetattr(consout_fd, &term);
827 term.c_cflag |= CLOCAL;
829 tcsetattr(consout_fd, TCSAFLUSH, &term);
831 addenv("smbios.bios.vendor=BHYVE");
832 addenv("boot_serial=1");
834 func(&cb, NULL, USERBOOT_VERSION_5, ndisks);