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>
89 #define MB (1024 * 1024UL)
90 #define GB (1024 * 1024 * 1024UL)
95 static char *host_base;
96 static struct termios term, oldterm;
97 static int disk_fd[NDISKS];
99 static int consin_fd, consout_fd;
101 static char *vmname, *progname;
102 static struct vmctx *ctx;
104 static uint64_t gdtbase, cr3, rsp;
106 static void cb_exit(void *arg, int v);
109 * Console i/o callbacks
113 cb_putc(void *arg, int ch)
117 (void) write(consout_fd, &c, 1);
125 if (read(consin_fd, &c, 1) == 1)
135 if (ioctl(consin_fd, FIONREAD, &n) >= 0)
141 * Host filesystem i/o callbacks
155 cb_open(void *arg, const char *filename, void **hp)
163 strlcpy(path, host_base, PATH_MAX);
164 if (path[strlen(path) - 1] == '/')
165 path[strlen(path) - 1] = 0;
166 strlcat(path, filename, PATH_MAX);
167 cf = malloc(sizeof(struct cb_file));
168 if (stat(path, &cf->cf_stat) < 0) {
173 cf->cf_size = cf->cf_stat.st_size;
174 if (S_ISDIR(cf->cf_stat.st_mode)) {
176 cf->cf_u.dir = opendir(path);
182 if (S_ISREG(cf->cf_stat.st_mode)) {
184 cf->cf_u.fd = open(path, O_RDONLY);
197 cb_close(void *arg, void *h)
199 struct cb_file *cf = h;
202 closedir(cf->cf_u.dir);
211 cb_isdir(void *arg, void *h)
213 struct cb_file *cf = h;
215 return (cf->cf_isdir);
219 cb_read(void *arg, void *h, void *buf, size_t size, size_t *resid)
221 struct cb_file *cf = h;
226 sz = read(cf->cf_u.fd, buf, size);
234 cb_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
235 size_t *namelen_return, char *name)
237 struct cb_file *cf = h;
243 dp = readdir(cf->cf_u.dir);
248 * Note: d_namlen is in the range 0..255 and therefore less
249 * than PATH_MAX so we don't need to test before copying.
251 *fileno_return = dp->d_fileno;
252 *type_return = dp->d_type;
253 *namelen_return = dp->d_namlen;
254 memcpy(name, dp->d_name, dp->d_namlen);
255 name[dp->d_namlen] = 0;
261 cb_seek(void *arg, void *h, uint64_t offset, int whence)
263 struct cb_file *cf = h;
267 if (lseek(cf->cf_u.fd, offset, whence) < 0)
273 cb_stat(void *arg, void *h, int *mode, int *uid, int *gid, uint64_t *size)
275 struct cb_file *cf = h;
277 *mode = cf->cf_stat.st_mode;
278 *uid = cf->cf_stat.st_uid;
279 *gid = cf->cf_stat.st_gid;
280 *size = cf->cf_stat.st_size;
285 * Disk image i/o callbacks
289 cb_diskread(void *arg, int unit, uint64_t from, void *to, size_t size,
294 if (unit < 0 || unit >= ndisks )
296 n = pread(disk_fd[unit], to, size, from);
304 cb_diskioctl(void *arg, int unit, u_long cmd, void *data)
308 if (unit < 0 || unit >= ndisks)
312 case DIOCGSECTORSIZE:
313 *(u_int *)data = 512;
316 if (fstat(disk_fd[unit], &sb) != 0)
318 if (S_ISCHR(sb.st_mode) &&
319 ioctl(disk_fd[unit], DIOCGMEDIASIZE, &sb.st_size) != 0)
321 *(off_t *)data = sb.st_size;
331 * Guest virtual machine i/o callbacks
334 cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
340 ptr = vm_map_gpa(ctx, to, size);
344 memcpy(ptr, from, size);
349 cb_copyout(void *arg, uint64_t from, void *to, size_t size)
355 ptr = vm_map_gpa(ctx, from, size);
359 memcpy(to, ptr, size);
364 cb_setreg(void *arg, int r, uint64_t v)
367 enum vm_reg_name vmreg;
373 vmreg = VM_REG_GUEST_RSP;
380 if (vmreg == VM_REG_LAST) {
381 printf("test_setreg(%d): not implemented\n", r);
382 cb_exit(NULL, USERBOOT_EXIT_QUIT);
385 error = vm_set_register(ctx, BSP, vmreg, v);
387 perror("vm_set_register");
388 cb_exit(NULL, USERBOOT_EXIT_QUIT);
393 cb_setmsr(void *arg, int r, uint64_t v)
396 enum vm_reg_name vmreg;
402 vmreg = VM_REG_GUEST_EFER;
408 if (vmreg == VM_REG_LAST) {
409 printf("test_setmsr(%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_msr");
416 cb_exit(NULL, USERBOOT_EXIT_QUIT);
421 cb_setcr(void *arg, int r, uint64_t v)
424 enum vm_reg_name vmreg;
430 vmreg = VM_REG_GUEST_CR0;
433 vmreg = VM_REG_GUEST_CR3;
437 vmreg = VM_REG_GUEST_CR4;
443 if (vmreg == VM_REG_LAST) {
444 printf("test_setcr(%d): not implemented\n", r);
445 cb_exit(NULL, USERBOOT_EXIT_QUIT);
448 error = vm_set_register(ctx, BSP, vmreg, v);
451 cb_exit(NULL, USERBOOT_EXIT_QUIT);
456 cb_setgdt(void *arg, uint64_t base, size_t size)
460 error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
462 perror("vm_set_desc(gdt)");
463 cb_exit(NULL, USERBOOT_EXIT_QUIT);
470 cb_exec(void *arg, uint64_t rip)
475 error = vm_setup_freebsd_registers_i386(ctx, BSP, rip, gdtbase,
478 error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase,
481 perror("vm_setup_freebsd_registers");
482 cb_exit(NULL, USERBOOT_EXIT_QUIT);
493 cb_delay(void *arg, int usec)
500 cb_exit(void *arg, int v)
503 tcsetattr(consout_fd, TCSAFLUSH, &oldterm);
508 cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
511 *ret_lowmem = vm_get_lowmem_size(ctx);
512 *ret_highmem = vm_get_highmem_size(ctx);
516 char *str; /* name=value */
517 SLIST_ENTRY(env) next;
520 static SLIST_HEAD(envhead, env) envhead;
527 env = malloc(sizeof(struct env));
529 SLIST_INSERT_HEAD(&envhead, env, next);
533 cb_getenv(void *arg, int num)
539 SLIST_FOREACH(env, &envhead, next) {
549 cb_vm_set_register(void *arg, int vcpu, int reg, uint64_t val)
552 return (vm_set_register(ctx, vcpu, reg, val));
556 cb_vm_set_desc(void *arg, int vcpu, int reg, uint64_t base, u_int limit,
560 return (vm_set_desc(ctx, vcpu, reg, base, limit, access));
563 static struct loader_callbacks cb = {
572 .readdir = cb_readdir,
576 .diskread = cb_diskread,
577 .diskioctl = cb_diskioctl,
580 .copyout = cb_copyout,
593 /* Version 4 additions */
594 .vm_set_register = cb_vm_set_register,
595 .vm_set_desc = cb_vm_set_desc,
599 altcons_open(char *path)
606 * Allow stdio to be passed in so that the same string
607 * can be used for the bhyveload console and bhyve com-port
610 if (!strcmp(path, "stdio"))
613 err = stat(path, &sb);
615 if (!S_ISCHR(sb.st_mode))
618 fd = open(path, O_RDWR | O_NONBLOCK);
622 consin_fd = consout_fd = fd;
630 disk_open(char *path)
634 if (ndisks >= NDISKS)
638 fd = open(path, O_RDONLY);
641 disk_fd[ndisks] = fd;
654 "usage: %s [-S][-c <console-device>] [-d <disk-path>] [-e <name=value>]\n"
655 " %*s [-h <host-path>] [-m memsize[K|k|M|m|G|g|T|t]] <vmname>\n",
657 (int)strlen(progname), "");
662 main(int argc, char** argv)
666 void (*func)(struct loader_callbacks *, void *, int, int);
668 int opt, error, need_reinit, memflags;
670 progname = basename(argv[0]);
677 consin_fd = STDIN_FILENO;
678 consout_fd = STDOUT_FILENO;
680 while ((opt = getopt(argc, argv, "CSc:d:e:h:l:m:")) != -1) {
683 error = altcons_open(optarg);
685 errx(EX_USAGE, "Could not open '%s'", optarg);
689 error = disk_open(optarg);
691 errx(EX_USAGE, "Could not open '%s'", optarg);
704 errx(EX_USAGE, "-l can only be given once");
705 loader = strdup(optarg);
707 err(EX_OSERR, "malloc");
711 error = vm_parse_memsize(optarg, &mem_size);
713 errx(EX_USAGE, "Invalid memsize '%s'", optarg);
716 memflags |= VM_MEM_F_INCORE;
719 memflags |= VM_MEM_F_WIRED;
735 error = vm_create(vmname);
737 if (errno != EEXIST) {
744 ctx = vm_open(vmname);
751 error = vm_reinit(ctx);
758 vm_set_memflags(ctx, memflags);
759 error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
761 perror("vm_setup_memory");
765 if (loader == NULL) {
766 loader = strdup("/boot/userboot.so");
768 err(EX_OSERR, "malloc");
770 h = dlopen(loader, RTLD_LOCAL);
772 printf("%s\n", dlerror());
776 func = dlsym(h, "loader_main");
778 printf("%s\n", dlerror());
783 tcgetattr(consout_fd, &term);
786 term.c_cflag |= CLOCAL;
788 tcsetattr(consout_fd, TCSAFLUSH, &term);
790 addenv("smbios.bios.vendor=BHYVE");
791 addenv("boot_serial=1");
793 func(&cb, NULL, USERBOOT_VERSION_4, ndisks);