2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * 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
57 #include <sys/cdefs.h>
58 __FBSDID("$FreeBSD$");
60 #include <sys/ioctl.h>
63 #include <sys/queue.h>
65 #include <machine/specialreg.h>
66 #include <machine/vmm.h>
87 #define MB (1024 * 1024UL)
88 #define GB (1024 * 1024 * 1024UL)
93 static char *host_base;
94 static struct termios term, oldterm;
95 static int disk_fd[NDISKS];
97 static int consin_fd, consout_fd;
99 static char *vmname, *progname;
100 static struct vmctx *ctx;
102 static uint64_t gdtbase, cr3, rsp;
104 static void cb_exit(void *arg, int v);
107 * Console i/o callbacks
111 cb_putc(void *arg, int ch)
115 (void) write(consout_fd, &c, 1);
123 if (read(consin_fd, &c, 1) == 1)
133 if (ioctl(consin_fd, FIONREAD, &n) >= 0)
139 * Host filesystem i/o callbacks
153 cb_open(void *arg, const char *filename, void **hp)
162 strlcpy(path, host_base, PATH_MAX);
163 if (path[strlen(path) - 1] == '/')
164 path[strlen(path) - 1] = 0;
165 strlcat(path, filename, PATH_MAX);
166 cf = malloc(sizeof(struct cb_file));
167 if (stat(path, &cf->cf_stat) < 0) {
172 cf->cf_size = st.st_size;
173 if (S_ISDIR(cf->cf_stat.st_mode)) {
175 cf->cf_u.dir = opendir(path);
181 if (S_ISREG(cf->cf_stat.st_mode)) {
183 cf->cf_u.fd = open(path, O_RDONLY);
196 cb_close(void *arg, void *h)
198 struct cb_file *cf = h;
201 closedir(cf->cf_u.dir);
210 cb_isdir(void *arg, void *h)
212 struct cb_file *cf = h;
214 return (cf->cf_isdir);
218 cb_read(void *arg, void *h, void *buf, size_t size, size_t *resid)
220 struct cb_file *cf = h;
225 sz = read(cf->cf_u.fd, buf, size);
233 cb_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
234 size_t *namelen_return, char *name)
236 struct cb_file *cf = h;
242 dp = readdir(cf->cf_u.dir);
247 * Note: d_namlen is in the range 0..255 and therefore less
248 * than PATH_MAX so we don't need to test before copying.
250 *fileno_return = dp->d_fileno;
251 *type_return = dp->d_type;
252 *namelen_return = dp->d_namlen;
253 memcpy(name, dp->d_name, dp->d_namlen);
254 name[dp->d_namlen] = 0;
260 cb_seek(void *arg, void *h, uint64_t offset, int whence)
262 struct cb_file *cf = h;
266 if (lseek(cf->cf_u.fd, offset, whence) < 0)
272 cb_stat(void *arg, void *h, int *mode, int *uid, int *gid, uint64_t *size)
274 struct cb_file *cf = h;
276 *mode = cf->cf_stat.st_mode;
277 *uid = cf->cf_stat.st_uid;
278 *gid = cf->cf_stat.st_gid;
279 *size = cf->cf_stat.st_size;
284 * Disk image i/o callbacks
288 cb_diskread(void *arg, int unit, uint64_t from, void *to, size_t size,
293 if (unit < 0 || unit >= ndisks )
295 n = pread(disk_fd[unit], to, size, from);
303 cb_diskioctl(void *arg, int unit, u_long cmd, void *data)
307 if (unit < 0 || unit >= ndisks)
311 case DIOCGSECTORSIZE:
312 *(u_int *)data = 512;
315 if (fstat(disk_fd[unit], &sb) == 0)
316 *(off_t *)data = sb.st_size;
328 * Guest virtual machine i/o callbacks
331 cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
337 ptr = vm_map_gpa(ctx, to, size);
341 memcpy(ptr, from, size);
346 cb_copyout(void *arg, uint64_t from, void *to, size_t size)
352 ptr = vm_map_gpa(ctx, from, size);
356 memcpy(to, ptr, size);
361 cb_setreg(void *arg, int r, uint64_t v)
364 enum vm_reg_name vmreg;
370 vmreg = VM_REG_GUEST_RSP;
377 if (vmreg == VM_REG_LAST) {
378 printf("test_setreg(%d): not implemented\n", r);
379 cb_exit(NULL, USERBOOT_EXIT_QUIT);
382 error = vm_set_register(ctx, BSP, vmreg, v);
384 perror("vm_set_register");
385 cb_exit(NULL, USERBOOT_EXIT_QUIT);
390 cb_setmsr(void *arg, int r, uint64_t v)
393 enum vm_reg_name vmreg;
399 vmreg = VM_REG_GUEST_EFER;
405 if (vmreg == VM_REG_LAST) {
406 printf("test_setmsr(%d): not implemented\n", r);
407 cb_exit(NULL, USERBOOT_EXIT_QUIT);
410 error = vm_set_register(ctx, BSP, vmreg, v);
412 perror("vm_set_msr");
413 cb_exit(NULL, USERBOOT_EXIT_QUIT);
418 cb_setcr(void *arg, int r, uint64_t v)
421 enum vm_reg_name vmreg;
427 vmreg = VM_REG_GUEST_CR0;
430 vmreg = VM_REG_GUEST_CR3;
434 vmreg = VM_REG_GUEST_CR4;
440 if (vmreg == VM_REG_LAST) {
441 printf("test_setcr(%d): not implemented\n", r);
442 cb_exit(NULL, USERBOOT_EXIT_QUIT);
445 error = vm_set_register(ctx, BSP, vmreg, v);
448 cb_exit(NULL, USERBOOT_EXIT_QUIT);
453 cb_setgdt(void *arg, uint64_t base, size_t size)
457 error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
459 perror("vm_set_desc(gdt)");
460 cb_exit(NULL, USERBOOT_EXIT_QUIT);
467 cb_exec(void *arg, uint64_t rip)
472 error = vm_setup_freebsd_registers_i386(ctx, BSP, rip, gdtbase,
475 error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase,
478 perror("vm_setup_freebsd_registers");
479 cb_exit(NULL, USERBOOT_EXIT_QUIT);
490 cb_delay(void *arg, int usec)
497 cb_exit(void *arg, int v)
500 tcsetattr(consout_fd, TCSAFLUSH, &oldterm);
505 cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
508 *ret_lowmem = vm_get_lowmem_size(ctx);
509 *ret_highmem = vm_get_highmem_size(ctx);
513 const char *str; /* name=value */
514 SLIST_ENTRY(env) next;
517 static SLIST_HEAD(envhead, env) envhead;
520 addenv(const char *str)
524 env = malloc(sizeof(struct env));
526 SLIST_INSERT_HEAD(&envhead, env, next);
530 cb_getenv(void *arg, int num)
536 SLIST_FOREACH(env, &envhead, next) {
545 static struct loader_callbacks cb = {
554 .readdir = cb_readdir,
558 .diskread = cb_diskread,
559 .diskioctl = cb_diskioctl,
562 .copyout = cb_copyout,
577 altcons_open(char *path)
584 * Allow stdio to be passed in so that the same string
585 * can be used for the bhyveload console and bhyve com-port
588 if (!strcmp(path, "stdio"))
591 err = stat(path, &sb);
593 if (!S_ISCHR(sb.st_mode))
596 fd = open(path, O_RDWR | O_NONBLOCK);
600 consin_fd = consout_fd = fd;
608 disk_open(char *path)
612 if (ndisks >= NDISKS)
616 fd = open(path, O_RDONLY);
619 disk_fd[ndisks] = fd;
632 "usage: %s [-S][-c <console-device>] [-d <disk-path>] [-e <name=value>]\n"
633 " %*s [-h <host-path>] [-m mem-size] <vmname>\n",
635 (int)strlen(progname), "");
640 main(int argc, char** argv)
644 void (*func)(struct loader_callbacks *, void *, int, int);
646 int opt, error, need_reinit, memflags;
648 progname = basename(argv[0]);
655 consin_fd = STDIN_FILENO;
656 consout_fd = STDOUT_FILENO;
658 while ((opt = getopt(argc, argv, "Sc:d:e:h:l:m:")) != -1) {
661 error = altcons_open(optarg);
663 errx(EX_USAGE, "Could not open '%s'", optarg);
667 error = disk_open(optarg);
669 errx(EX_USAGE, "Could not open '%s'", optarg);
682 errx(EX_USAGE, "-l can only be given once");
683 loader = strdup(optarg);
685 err(EX_OSERR, "malloc");
689 error = vm_parse_memsize(optarg, &mem_size);
691 errx(EX_USAGE, "Invalid memsize '%s'", optarg);
694 memflags |= VM_MEM_F_WIRED;
710 error = vm_create(vmname);
712 if (errno != EEXIST) {
719 ctx = vm_open(vmname);
726 error = vm_reinit(ctx);
733 vm_set_memflags(ctx, memflags);
734 error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
736 perror("vm_setup_memory");
740 if (loader == NULL) {
741 loader = strdup("/boot/userboot.so");
743 err(EX_OSERR, "malloc");
745 h = dlopen(loader, RTLD_LOCAL);
747 printf("%s\n", dlerror());
751 func = dlsym(h, "loader_main");
753 printf("%s\n", dlerror());
758 tcgetattr(consout_fd, &term);
761 term.c_cflag |= CLOCAL;
763 tcsetattr(consout_fd, TCSAFLUSH, &term);
765 addenv("smbios.bios.vendor=BHYVE");
766 addenv("boot_serial=1");
768 func(&cb, NULL, USERBOOT_VERSION_3, ndisks);