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)
317 if (S_ISCHR(sb.st_mode) &&
318 ioctl(disk_fd[unit], DIOCGMEDIASIZE, &sb.st_size) != 0)
320 *(off_t *)data = sb.st_size;
330 * Guest virtual machine i/o callbacks
333 cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
339 ptr = vm_map_gpa(ctx, to, size);
343 memcpy(ptr, from, size);
348 cb_copyout(void *arg, uint64_t from, void *to, size_t size)
354 ptr = vm_map_gpa(ctx, from, size);
358 memcpy(to, ptr, size);
363 cb_setreg(void *arg, int r, uint64_t v)
366 enum vm_reg_name vmreg;
372 vmreg = VM_REG_GUEST_RSP;
379 if (vmreg == VM_REG_LAST) {
380 printf("test_setreg(%d): not implemented\n", r);
381 cb_exit(NULL, USERBOOT_EXIT_QUIT);
384 error = vm_set_register(ctx, BSP, vmreg, v);
386 perror("vm_set_register");
387 cb_exit(NULL, USERBOOT_EXIT_QUIT);
392 cb_setmsr(void *arg, int r, uint64_t v)
395 enum vm_reg_name vmreg;
401 vmreg = VM_REG_GUEST_EFER;
407 if (vmreg == VM_REG_LAST) {
408 printf("test_setmsr(%d): not implemented\n", r);
409 cb_exit(NULL, USERBOOT_EXIT_QUIT);
412 error = vm_set_register(ctx, BSP, vmreg, v);
414 perror("vm_set_msr");
415 cb_exit(NULL, USERBOOT_EXIT_QUIT);
420 cb_setcr(void *arg, int r, uint64_t v)
423 enum vm_reg_name vmreg;
429 vmreg = VM_REG_GUEST_CR0;
432 vmreg = VM_REG_GUEST_CR3;
436 vmreg = VM_REG_GUEST_CR4;
442 if (vmreg == VM_REG_LAST) {
443 printf("test_setcr(%d): not implemented\n", r);
444 cb_exit(NULL, USERBOOT_EXIT_QUIT);
447 error = vm_set_register(ctx, BSP, vmreg, v);
450 cb_exit(NULL, USERBOOT_EXIT_QUIT);
455 cb_setgdt(void *arg, uint64_t base, size_t size)
459 error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
461 perror("vm_set_desc(gdt)");
462 cb_exit(NULL, USERBOOT_EXIT_QUIT);
469 cb_exec(void *arg, uint64_t rip)
474 error = vm_setup_freebsd_registers_i386(ctx, BSP, rip, gdtbase,
477 error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase,
480 perror("vm_setup_freebsd_registers");
481 cb_exit(NULL, USERBOOT_EXIT_QUIT);
492 cb_delay(void *arg, int usec)
499 cb_exit(void *arg, int v)
502 tcsetattr(consout_fd, TCSAFLUSH, &oldterm);
507 cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
510 *ret_lowmem = vm_get_lowmem_size(ctx);
511 *ret_highmem = vm_get_highmem_size(ctx);
515 const char *str; /* name=value */
516 SLIST_ENTRY(env) next;
519 static SLIST_HEAD(envhead, env) envhead;
522 addenv(const char *str)
526 env = malloc(sizeof(struct env));
528 SLIST_INSERT_HEAD(&envhead, env, next);
532 cb_getenv(void *arg, int num)
538 SLIST_FOREACH(env, &envhead, next) {
547 static struct loader_callbacks cb = {
556 .readdir = cb_readdir,
560 .diskread = cb_diskread,
561 .diskioctl = cb_diskioctl,
564 .copyout = cb_copyout,
579 altcons_open(char *path)
586 * Allow stdio to be passed in so that the same string
587 * can be used for the bhyveload console and bhyve com-port
590 if (!strcmp(path, "stdio"))
593 err = stat(path, &sb);
595 if (!S_ISCHR(sb.st_mode))
598 fd = open(path, O_RDWR | O_NONBLOCK);
602 consin_fd = consout_fd = fd;
610 disk_open(char *path)
614 if (ndisks >= NDISKS)
618 fd = open(path, O_RDONLY);
621 disk_fd[ndisks] = fd;
634 "usage: %s [-S][-c <console-device>] [-d <disk-path>] [-e <name=value>]\n"
635 " %*s [-h <host-path>] [-m mem-size] <vmname>\n",
637 (int)strlen(progname), "");
642 main(int argc, char** argv)
646 void (*func)(struct loader_callbacks *, void *, int, int);
648 int opt, error, need_reinit, memflags;
650 progname = basename(argv[0]);
657 consin_fd = STDIN_FILENO;
658 consout_fd = STDOUT_FILENO;
660 while ((opt = getopt(argc, argv, "Sc:d:e:h:l:m:")) != -1) {
663 error = altcons_open(optarg);
665 errx(EX_USAGE, "Could not open '%s'", optarg);
669 error = disk_open(optarg);
671 errx(EX_USAGE, "Could not open '%s'", optarg);
684 errx(EX_USAGE, "-l can only be given once");
685 loader = strdup(optarg);
687 err(EX_OSERR, "malloc");
691 error = vm_parse_memsize(optarg, &mem_size);
693 errx(EX_USAGE, "Invalid memsize '%s'", optarg);
696 memflags |= VM_MEM_F_WIRED;
712 error = vm_create(vmname);
714 if (errno != EEXIST) {
721 ctx = vm_open(vmname);
728 error = vm_reinit(ctx);
735 vm_set_memflags(ctx, memflags);
736 error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
738 perror("vm_setup_memory");
742 if (loader == NULL) {
743 loader = strdup("/boot/userboot.so");
745 err(EX_OSERR, "malloc");
747 h = dlopen(loader, RTLD_LOCAL);
749 printf("%s\n", dlerror());
753 func = dlsym(h, "loader_main");
755 printf("%s\n", dlerror());
760 tcgetattr(consout_fd, &term);
763 term.c_cflag |= CLOCAL;
765 tcsetattr(consout_fd, TCSAFLUSH, &term);
767 addenv("smbios.bios.vendor=BHYVE");
768 addenv("boot_serial=1");
770 func(&cb, NULL, USERBOOT_VERSION_3, ndisks);