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 struct termios term, oldterm;
92 static int disk_fd[NDISKS];
94 static int consin_fd, consout_fd;
95 static int hostbase_fd = -1;
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;
107 static uint64_t gdtbase, cr3, rsp;
109 static void cb_exit(void *arg, int v);
112 * Console i/o callbacks
116 cb_putc(void *arg __unused, int ch)
120 (void) write(consout_fd, &c, 1);
124 cb_getc(void *arg __unused)
128 if (read(consin_fd, &c, 1) == 1)
134 cb_poll(void *arg __unused)
138 if (ioctl(consin_fd, FIONREAD, &n) >= 0)
144 * Host filesystem i/o callbacks
158 cb_open(void *arg __unused, const char *filename, void **hp)
166 flags = O_RDONLY | O_RESOLVE_BENEATH;
167 if (hostbase_fd == -1)
170 /* Absolute paths are relative to our hostbase, chop off leading /. */
171 if (filename[0] == '/')
174 /* Lookup of /, use . instead. */
175 if (filename[0] == '\0')
178 if (fstatat(hostbase_fd, filename, &sb, AT_RESOLVE_BENEATH) < 0)
181 if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode))
184 if (S_ISDIR(sb.st_mode))
185 flags |= O_DIRECTORY;
187 /* May be opening the root dir */
188 fd = openat(hostbase_fd, filename, flags);
192 cf = malloc(sizeof(struct cb_file));
199 cf->cf_size = cf->cf_stat.st_size;
201 if (S_ISDIR(cf->cf_stat.st_mode)) {
203 cf->cf_u.dir = fdopendir(fd);
204 if (cf->cf_u.dir == NULL) {
210 assert(S_ISREG(cf->cf_stat.st_mode));
219 cb_close(void *arg __unused, void *h)
221 struct cb_file *cf = h;
224 closedir(cf->cf_u.dir);
233 cb_isdir(void *arg __unused, void *h)
235 struct cb_file *cf = h;
237 return (cf->cf_isdir);
241 cb_read(void *arg __unused, void *h, void *buf, size_t size, size_t *resid)
243 struct cb_file *cf = h;
248 sz = read(cf->cf_u.fd, buf, size);
256 cb_readdir(void *arg __unused, void *h, uint32_t *fileno_return,
257 uint8_t *type_return, size_t *namelen_return, char *name)
259 struct cb_file *cf = h;
265 dp = readdir(cf->cf_u.dir);
270 * Note: d_namlen is in the range 0..255 and therefore less
271 * than PATH_MAX so we don't need to test before copying.
273 *fileno_return = dp->d_fileno;
274 *type_return = dp->d_type;
275 *namelen_return = dp->d_namlen;
276 memcpy(name, dp->d_name, dp->d_namlen);
277 name[dp->d_namlen] = 0;
283 cb_seek(void *arg __unused, void *h, uint64_t offset, int whence)
285 struct cb_file *cf = h;
289 if (lseek(cf->cf_u.fd, offset, whence) < 0)
295 cb_stat(void *arg __unused, void *h, struct stat *sbp)
297 struct cb_file *cf = h;
299 memset(sbp, 0, sizeof(struct stat));
300 sbp->st_mode = cf->cf_stat.st_mode;
301 sbp->st_uid = cf->cf_stat.st_uid;
302 sbp->st_gid = cf->cf_stat.st_gid;
303 sbp->st_size = cf->cf_stat.st_size;
304 sbp->st_mtime = cf->cf_stat.st_mtime;
305 sbp->st_dev = cf->cf_stat.st_dev;
306 sbp->st_ino = cf->cf_stat.st_ino;
312 * Disk image i/o callbacks
316 cb_diskread(void *arg __unused, int unit, uint64_t from, void *to, size_t size,
321 if (unit < 0 || unit >= ndisks)
323 n = pread(disk_fd[unit], to, size, from);
331 cb_diskwrite(void *arg __unused, int unit, uint64_t offset, void *src,
332 size_t size, size_t *resid)
336 if (unit < 0 || unit >= ndisks)
338 n = pwrite(disk_fd[unit], src, size, offset);
346 cb_diskioctl(void *arg __unused, int unit, u_long cmd, void *data)
350 if (unit < 0 || unit >= ndisks)
354 case DIOCGSECTORSIZE:
355 *(u_int *)data = 512;
358 if (fstat(disk_fd[unit], &sb) != 0)
360 if (S_ISCHR(sb.st_mode) &&
361 ioctl(disk_fd[unit], DIOCGMEDIASIZE, &sb.st_size) != 0)
363 *(off_t *)data = sb.st_size;
373 * Guest virtual machine i/o callbacks
376 cb_copyin(void *arg __unused, const void *from, uint64_t to, size_t size)
382 ptr = vm_map_gpa(ctx, to, size);
386 memcpy(ptr, from, size);
391 cb_copyout(void *arg __unused, uint64_t from, void *to, size_t size)
397 ptr = vm_map_gpa(ctx, from, size);
401 memcpy(to, ptr, size);
406 cb_setreg(void *arg __unused, int r, uint64_t v)
409 enum vm_reg_name vmreg;
415 vmreg = VM_REG_GUEST_RSP;
422 if (vmreg == VM_REG_LAST) {
423 printf("test_setreg(%d): not implemented\n", r);
424 cb_exit(NULL, USERBOOT_EXIT_QUIT);
427 error = vm_set_register(ctx, BSP, vmreg, v);
429 perror("vm_set_register");
430 cb_exit(NULL, USERBOOT_EXIT_QUIT);
435 cb_setmsr(void *arg __unused, int r, uint64_t v)
438 enum vm_reg_name vmreg;
444 vmreg = VM_REG_GUEST_EFER;
450 if (vmreg == VM_REG_LAST) {
451 printf("test_setmsr(%d): not implemented\n", r);
452 cb_exit(NULL, USERBOOT_EXIT_QUIT);
455 error = vm_set_register(ctx, BSP, vmreg, v);
457 perror("vm_set_msr");
458 cb_exit(NULL, USERBOOT_EXIT_QUIT);
463 cb_setcr(void *arg __unused, int r, uint64_t v)
466 enum vm_reg_name vmreg;
472 vmreg = VM_REG_GUEST_CR0;
475 vmreg = VM_REG_GUEST_CR3;
479 vmreg = VM_REG_GUEST_CR4;
485 if (vmreg == VM_REG_LAST) {
486 printf("test_setcr(%d): not implemented\n", r);
487 cb_exit(NULL, USERBOOT_EXIT_QUIT);
490 error = vm_set_register(ctx, BSP, vmreg, v);
493 cb_exit(NULL, USERBOOT_EXIT_QUIT);
498 cb_setgdt(void *arg __unused, uint64_t base, size_t size)
502 error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
504 perror("vm_set_desc(gdt)");
505 cb_exit(NULL, USERBOOT_EXIT_QUIT);
512 cb_exec(void *arg __unused, uint64_t rip)
517 error = vm_setup_freebsd_registers_i386(ctx, BSP, rip, gdtbase,
520 error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase,
523 perror("vm_setup_freebsd_registers");
524 cb_exit(NULL, USERBOOT_EXIT_QUIT);
535 cb_delay(void *arg __unused, int usec)
542 cb_exit(void *arg __unused, int v)
545 tcsetattr(consout_fd, TCSAFLUSH, &oldterm);
550 cb_getmem(void *arg __unused, uint64_t *ret_lowmem, uint64_t *ret_highmem)
553 *ret_lowmem = vm_get_lowmem_size(ctx);
554 *ret_highmem = vm_get_highmem_size(ctx);
558 char *str; /* name=value */
559 SLIST_ENTRY(env) next;
562 static SLIST_HEAD(envhead, env) envhead;
565 addenv(const char *str)
569 env = malloc(sizeof(struct env));
571 err(EX_OSERR, "malloc");
572 env->str = strdup(str);
573 if (env->str == NULL)
574 err(EX_OSERR, "strdup");
575 SLIST_INSERT_HEAD(&envhead, env, next);
579 cb_getenv(void *arg __unused, int num)
585 SLIST_FOREACH(env, &envhead, next) {
595 cb_vm_set_register(void *arg __unused, int vcpu, int reg, uint64_t val)
598 return (vm_set_register(ctx, vcpu, reg, val));
602 cb_vm_set_desc(void *arg __unused, int vcpu, int reg, uint64_t base,
603 u_int limit, u_int access)
606 return (vm_set_desc(ctx, vcpu, reg, base, limit, access));
610 cb_swap_interpreter(void *arg __unused, const char *interp_req)
614 * If the user specified a loader but we detected a mismatch, we should
615 * not try to pivot to a different loader on them.
618 if (explicit_loader == 1) {
619 perror("requested loader interpreter does not match guest userboot");
622 if (interp_req == NULL || *interp_req == '\0') {
623 perror("guest failed to request an interpreter");
627 if (asprintf(&loader, "/boot/userboot_%s.so", interp_req) == -1)
628 err(EX_OSERR, "malloc");
633 static struct loader_callbacks cb = {
642 .readdir = cb_readdir,
646 .diskread = cb_diskread,
647 .diskwrite = cb_diskwrite,
648 .diskioctl = cb_diskioctl,
651 .copyout = cb_copyout,
664 /* Version 4 additions */
665 .vm_set_register = cb_vm_set_register,
666 .vm_set_desc = cb_vm_set_desc,
668 /* Version 5 additions */
669 .swap_interpreter = cb_swap_interpreter,
673 altcons_open(char *path)
680 * Allow stdio to be passed in so that the same string
681 * can be used for the bhyveload console and bhyve com-port
684 if (!strcmp(path, "stdio"))
687 err = stat(path, &sb);
689 if (!S_ISCHR(sb.st_mode))
692 fd = open(path, O_RDWR | O_NONBLOCK);
696 consin_fd = consout_fd = fd;
704 disk_open(char *path)
708 if (ndisks >= NDISKS)
711 fd = open(path, O_RDWR);
715 disk_fd[ndisks] = fd;
726 "usage: %s [-S][-c <console-device>] [-d <disk-path>] [-e <name=value>]\n"
727 " %*s [-h <host-path>] [-m memsize[K|k|M|m|G|g|T|t]] <vmname>\n",
729 (int)strlen(progname), "");
734 hostbase_open(const char *base)
737 if (hostbase_fd != -1)
739 hostbase_fd = open(base, O_DIRECTORY | O_PATH);
740 if (hostbase_fd == -1)
741 err(EX_OSERR, "open");
745 main(int argc, char** argv)
747 void (*func)(struct loader_callbacks *, void *, int, int);
749 int opt, error, memflags;
751 progname = basename(argv[0]);
756 consin_fd = STDIN_FILENO;
757 consout_fd = STDOUT_FILENO;
759 while ((opt = getopt(argc, argv, "CSc:d:e:h:l:m:")) != -1) {
762 error = altcons_open(optarg);
764 errx(EX_USAGE, "Could not open '%s'", optarg);
768 error = disk_open(optarg);
770 errx(EX_USAGE, "Could not open '%s'", optarg);
778 hostbase_open(optarg);
783 errx(EX_USAGE, "-l can only be given once");
784 loader = strdup(optarg);
786 err(EX_OSERR, "malloc");
791 error = vm_parse_memsize(optarg, &mem_size);
793 errx(EX_USAGE, "Invalid memsize '%s'", optarg);
796 memflags |= VM_MEM_F_INCORE;
799 memflags |= VM_MEM_F_WIRED;
815 error = vm_create(vmname);
817 if (errno != EEXIST) {
824 ctx = vm_open(vmname);
831 * setjmp in the case the guest wants to swap out interpreter,
832 * cb_swap_interpreter will swap out loader as appropriate and set
833 * need_reinit so that we end up in a clean state once again.
838 error = vm_reinit(ctx);
845 vm_set_memflags(ctx, memflags);
846 error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
848 perror("vm_setup_memory");
852 if (loader == NULL) {
853 loader = strdup("/boot/userboot.so");
855 err(EX_OSERR, "malloc");
857 if (loader_hdl != NULL)
859 loader_hdl = dlopen(loader, RTLD_LOCAL);
861 printf("%s\n", dlerror());
865 func = dlsym(loader_hdl, "loader_main");
867 printf("%s\n", dlerror());
872 tcgetattr(consout_fd, &term);
875 term.c_cflag |= CLOCAL;
877 tcsetattr(consout_fd, TCSAFLUSH, &term);
879 addenv("smbios.bios.vendor=BHYVE");
880 addenv("boot_serial=1");
882 func(&cb, NULL, USERBOOT_VERSION_5, ndisks);