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)
161 strlcpy(path, host_base, PATH_MAX);
162 if (path[strlen(path) - 1] == '/')
163 path[strlen(path) - 1] = 0;
164 strlcat(path, filename, PATH_MAX);
165 cf = malloc(sizeof(struct cb_file));
166 if (stat(path, &cf->cf_stat) < 0) {
171 cf->cf_size = cf->cf_stat.st_size;
172 if (S_ISDIR(cf->cf_stat.st_mode)) {
174 cf->cf_u.dir = opendir(path);
180 if (S_ISREG(cf->cf_stat.st_mode)) {
182 cf->cf_u.fd = open(path, O_RDONLY);
195 cb_close(void *arg, void *h)
197 struct cb_file *cf = h;
200 closedir(cf->cf_u.dir);
209 cb_isdir(void *arg, void *h)
211 struct cb_file *cf = h;
213 return (cf->cf_isdir);
217 cb_read(void *arg, void *h, void *buf, size_t size, size_t *resid)
219 struct cb_file *cf = h;
224 sz = read(cf->cf_u.fd, buf, size);
232 cb_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
233 size_t *namelen_return, char *name)
235 struct cb_file *cf = h;
241 dp = readdir(cf->cf_u.dir);
246 * Note: d_namlen is in the range 0..255 and therefore less
247 * than PATH_MAX so we don't need to test before copying.
249 *fileno_return = dp->d_fileno;
250 *type_return = dp->d_type;
251 *namelen_return = dp->d_namlen;
252 memcpy(name, dp->d_name, dp->d_namlen);
253 name[dp->d_namlen] = 0;
259 cb_seek(void *arg, void *h, uint64_t offset, int whence)
261 struct cb_file *cf = h;
265 if (lseek(cf->cf_u.fd, offset, whence) < 0)
271 cb_stat(void *arg, void *h, int *mode, int *uid, int *gid, uint64_t *size)
273 struct cb_file *cf = h;
275 *mode = cf->cf_stat.st_mode;
276 *uid = cf->cf_stat.st_uid;
277 *gid = cf->cf_stat.st_gid;
278 *size = cf->cf_stat.st_size;
283 * Disk image i/o callbacks
287 cb_diskread(void *arg, int unit, uint64_t from, void *to, size_t size,
292 if (unit < 0 || unit >= ndisks )
294 n = pread(disk_fd[unit], to, size, from);
302 cb_diskioctl(void *arg, int unit, u_long cmd, void *data)
306 if (unit < 0 || unit >= ndisks)
310 case DIOCGSECTORSIZE:
311 *(u_int *)data = 512;
314 if (fstat(disk_fd[unit], &sb) == 0)
315 *(off_t *)data = sb.st_size;
327 * Guest virtual machine i/o callbacks
330 cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
336 ptr = vm_map_gpa(ctx, to, size);
340 memcpy(ptr, from, size);
345 cb_copyout(void *arg, uint64_t from, void *to, size_t size)
351 ptr = vm_map_gpa(ctx, from, size);
355 memcpy(to, ptr, size);
360 cb_setreg(void *arg, int r, uint64_t v)
363 enum vm_reg_name vmreg;
369 vmreg = VM_REG_GUEST_RSP;
376 if (vmreg == VM_REG_LAST) {
377 printf("test_setreg(%d): not implemented\n", r);
378 cb_exit(NULL, USERBOOT_EXIT_QUIT);
381 error = vm_set_register(ctx, BSP, vmreg, v);
383 perror("vm_set_register");
384 cb_exit(NULL, USERBOOT_EXIT_QUIT);
389 cb_setmsr(void *arg, int r, uint64_t v)
392 enum vm_reg_name vmreg;
398 vmreg = VM_REG_GUEST_EFER;
404 if (vmreg == VM_REG_LAST) {
405 printf("test_setmsr(%d): not implemented\n", r);
406 cb_exit(NULL, USERBOOT_EXIT_QUIT);
409 error = vm_set_register(ctx, BSP, vmreg, v);
411 perror("vm_set_msr");
412 cb_exit(NULL, USERBOOT_EXIT_QUIT);
417 cb_setcr(void *arg, int r, uint64_t v)
420 enum vm_reg_name vmreg;
426 vmreg = VM_REG_GUEST_CR0;
429 vmreg = VM_REG_GUEST_CR3;
433 vmreg = VM_REG_GUEST_CR4;
439 if (vmreg == VM_REG_LAST) {
440 printf("test_setcr(%d): not implemented\n", r);
441 cb_exit(NULL, USERBOOT_EXIT_QUIT);
444 error = vm_set_register(ctx, BSP, vmreg, v);
447 cb_exit(NULL, USERBOOT_EXIT_QUIT);
452 cb_setgdt(void *arg, uint64_t base, size_t size)
456 error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
458 perror("vm_set_desc(gdt)");
459 cb_exit(NULL, USERBOOT_EXIT_QUIT);
466 cb_exec(void *arg, uint64_t rip)
471 error = vm_setup_freebsd_registers_i386(ctx, BSP, rip, gdtbase,
474 error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase,
477 perror("vm_setup_freebsd_registers");
478 cb_exit(NULL, USERBOOT_EXIT_QUIT);
489 cb_delay(void *arg, int usec)
496 cb_exit(void *arg, int v)
499 tcsetattr(consout_fd, TCSAFLUSH, &oldterm);
504 cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
507 *ret_lowmem = vm_get_lowmem_size(ctx);
508 *ret_highmem = vm_get_highmem_size(ctx);
512 const char *str; /* name=value */
513 SLIST_ENTRY(env) next;
516 static SLIST_HEAD(envhead, env) envhead;
519 addenv(const char *str)
523 env = malloc(sizeof(struct env));
525 SLIST_INSERT_HEAD(&envhead, env, next);
529 cb_getenv(void *arg, int num)
535 SLIST_FOREACH(env, &envhead, next) {
545 cb_vm_set_register(void *arg, int vcpu, int reg, uint64_t val)
548 return (vm_set_register(ctx, vcpu, reg, val));
552 cb_vm_set_desc(void *arg, int vcpu, int reg, uint64_t base, u_int limit,
556 return (vm_set_desc(ctx, vcpu, reg, base, limit, access));
559 static struct loader_callbacks cb = {
568 .readdir = cb_readdir,
572 .diskread = cb_diskread,
573 .diskioctl = cb_diskioctl,
576 .copyout = cb_copyout,
589 /* Version 4 additions */
590 .vm_set_register = cb_vm_set_register,
591 .vm_set_desc = cb_vm_set_desc,
595 altcons_open(char *path)
602 * Allow stdio to be passed in so that the same string
603 * can be used for the bhyveload console and bhyve com-port
606 if (!strcmp(path, "stdio"))
609 err = stat(path, &sb);
611 if (!S_ISCHR(sb.st_mode))
614 fd = open(path, O_RDWR | O_NONBLOCK);
618 consin_fd = consout_fd = fd;
626 disk_open(char *path)
630 if (ndisks >= NDISKS)
634 fd = open(path, O_RDONLY);
637 disk_fd[ndisks] = fd;
650 "usage: %s [-S][-c <console-device>] [-d <disk-path>] [-e <name=value>]\n"
651 " %*s [-h <host-path>] [-m memsize[K|k|M|m|G|g|T|t]] <vmname>\n",
653 (int)strlen(progname), "");
658 main(int argc, char** argv)
662 void (*func)(struct loader_callbacks *, void *, int, int);
664 int opt, error, need_reinit, memflags;
666 progname = basename(argv[0]);
673 consin_fd = STDIN_FILENO;
674 consout_fd = STDOUT_FILENO;
676 while ((opt = getopt(argc, argv, "CSc:d:e:h:l:m:")) != -1) {
679 error = altcons_open(optarg);
681 errx(EX_USAGE, "Could not open '%s'", optarg);
685 error = disk_open(optarg);
687 errx(EX_USAGE, "Could not open '%s'", optarg);
700 errx(EX_USAGE, "-l can only be given once");
701 loader = strdup(optarg);
703 err(EX_OSERR, "malloc");
707 error = vm_parse_memsize(optarg, &mem_size);
709 errx(EX_USAGE, "Invalid memsize '%s'", optarg);
712 memflags |= VM_MEM_F_INCORE;
715 memflags |= VM_MEM_F_WIRED;
731 error = vm_create(vmname);
733 if (errno != EEXIST) {
740 ctx = vm_open(vmname);
747 error = vm_reinit(ctx);
754 vm_set_memflags(ctx, memflags);
755 error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
757 perror("vm_setup_memory");
761 if (loader == NULL) {
762 loader = strdup("/boot/userboot.so");
764 err(EX_OSERR, "malloc");
766 h = dlopen(loader, RTLD_LOCAL);
768 printf("%s\n", dlerror());
772 func = dlsym(h, "loader_main");
774 printf("%s\n", dlerror());
779 tcgetattr(consout_fd, &term);
782 term.c_cflag |= CLOCAL;
784 tcsetattr(consout_fd, TCSAFLUSH, &term);
786 addenv("smbios.bios.vendor=BHYVE");
787 addenv("boot_serial=1");
789 func(&cb, NULL, USERBOOT_VERSION_4, ndisks);