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)
91 static char *host_base;
92 static struct termios term, oldterm;
93 static int disk_fd = -1;
94 static int consin_fd, consout_fd;
96 static char *vmname, *progname;
97 static struct vmctx *ctx;
99 static uint64_t gdtbase, cr3, rsp;
101 static void cb_exit(void *arg, int v);
104 * Console i/o callbacks
108 cb_putc(void *arg, int ch)
112 (void) write(consout_fd, &c, 1);
120 if (read(consin_fd, &c, 1) == 1)
130 if (ioctl(consin_fd, FIONREAD, &n) >= 0)
136 * Host filesystem i/o callbacks
150 cb_open(void *arg, const char *filename, void **hp)
159 strlcpy(path, host_base, PATH_MAX);
160 if (path[strlen(path) - 1] == '/')
161 path[strlen(path) - 1] = 0;
162 strlcat(path, filename, PATH_MAX);
163 cf = malloc(sizeof(struct cb_file));
164 if (stat(path, &cf->cf_stat) < 0) {
169 cf->cf_size = st.st_size;
170 if (S_ISDIR(cf->cf_stat.st_mode)) {
172 cf->cf_u.dir = opendir(path);
178 if (S_ISREG(cf->cf_stat.st_mode)) {
180 cf->cf_u.fd = open(path, O_RDONLY);
193 cb_close(void *arg, void *h)
195 struct cb_file *cf = h;
198 closedir(cf->cf_u.dir);
207 cb_isdir(void *arg, void *h)
209 struct cb_file *cf = h;
211 return (cf->cf_isdir);
215 cb_read(void *arg, void *h, void *buf, size_t size, size_t *resid)
217 struct cb_file *cf = h;
222 sz = read(cf->cf_u.fd, buf, size);
230 cb_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
231 size_t *namelen_return, char *name)
233 struct cb_file *cf = h;
239 dp = readdir(cf->cf_u.dir);
244 * Note: d_namlen is in the range 0..255 and therefore less
245 * than PATH_MAX so we don't need to test before copying.
247 *fileno_return = dp->d_fileno;
248 *type_return = dp->d_type;
249 *namelen_return = dp->d_namlen;
250 memcpy(name, dp->d_name, dp->d_namlen);
251 name[dp->d_namlen] = 0;
257 cb_seek(void *arg, void *h, uint64_t offset, int whence)
259 struct cb_file *cf = h;
263 if (lseek(cf->cf_u.fd, offset, whence) < 0)
269 cb_stat(void *arg, void *h, int *mode, int *uid, int *gid, uint64_t *size)
271 struct cb_file *cf = h;
273 *mode = cf->cf_stat.st_mode;
274 *uid = cf->cf_stat.st_uid;
275 *gid = cf->cf_stat.st_gid;
276 *size = cf->cf_stat.st_size;
281 * Disk image i/o callbacks
285 cb_diskread(void *arg, int unit, uint64_t from, void *to, size_t size,
290 if (unit != 0 || disk_fd == -1)
292 n = pread(disk_fd, to, size, from);
300 cb_diskioctl(void *arg, int unit, u_long cmd, void *data)
304 if (unit != 0 || disk_fd == -1)
308 case DIOCGSECTORSIZE:
309 *(u_int *)data = 512;
312 if (fstat(disk_fd, &sb) == 0)
313 *(off_t *)data = sb.st_size;
325 * Guest virtual machine i/o callbacks
328 cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
334 ptr = vm_map_gpa(ctx, to, size);
338 memcpy(ptr, from, size);
343 cb_copyout(void *arg, uint64_t from, void *to, size_t size)
349 ptr = vm_map_gpa(ctx, from, size);
353 memcpy(to, ptr, size);
358 cb_setreg(void *arg, int r, uint64_t v)
361 enum vm_reg_name vmreg;
367 vmreg = VM_REG_GUEST_RSP;
374 if (vmreg == VM_REG_LAST) {
375 printf("test_setreg(%d): not implemented\n", r);
376 cb_exit(NULL, USERBOOT_EXIT_QUIT);
379 error = vm_set_register(ctx, BSP, vmreg, v);
381 perror("vm_set_register");
382 cb_exit(NULL, USERBOOT_EXIT_QUIT);
387 cb_setmsr(void *arg, int r, uint64_t v)
390 enum vm_reg_name vmreg;
396 vmreg = VM_REG_GUEST_EFER;
402 if (vmreg == VM_REG_LAST) {
403 printf("test_setmsr(%d): not implemented\n", r);
404 cb_exit(NULL, USERBOOT_EXIT_QUIT);
407 error = vm_set_register(ctx, BSP, vmreg, v);
409 perror("vm_set_msr");
410 cb_exit(NULL, USERBOOT_EXIT_QUIT);
415 cb_setcr(void *arg, int r, uint64_t v)
418 enum vm_reg_name vmreg;
424 vmreg = VM_REG_GUEST_CR0;
427 vmreg = VM_REG_GUEST_CR3;
431 vmreg = VM_REG_GUEST_CR4;
437 if (vmreg == VM_REG_LAST) {
438 printf("test_setcr(%d): not implemented\n", r);
439 cb_exit(NULL, USERBOOT_EXIT_QUIT);
442 error = vm_set_register(ctx, BSP, vmreg, v);
445 cb_exit(NULL, USERBOOT_EXIT_QUIT);
450 cb_setgdt(void *arg, uint64_t base, size_t size)
454 error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
456 perror("vm_set_desc(gdt)");
457 cb_exit(NULL, USERBOOT_EXIT_QUIT);
464 cb_exec(void *arg, uint64_t rip)
468 error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase, rsp);
470 perror("vm_setup_freebsd_registers");
471 cb_exit(NULL, USERBOOT_EXIT_QUIT);
482 cb_delay(void *arg, int usec)
489 cb_exit(void *arg, int v)
492 tcsetattr(consout_fd, TCSAFLUSH, &oldterm);
497 cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
500 vm_get_memory_seg(ctx, 0, ret_lowmem, NULL);
501 vm_get_memory_seg(ctx, 4 * GB, ret_highmem, NULL);
505 const char *str; /* name=value */
506 SLIST_ENTRY(env) next;
509 static SLIST_HEAD(envhead, env) envhead;
512 addenv(const char *str)
516 env = malloc(sizeof(struct env));
518 SLIST_INSERT_HEAD(&envhead, env, next);
522 cb_getenv(void *arg, int num)
528 SLIST_FOREACH(env, &envhead, next) {
537 static struct loader_callbacks cb = {
546 .readdir = cb_readdir,
550 .diskread = cb_diskread,
551 .diskioctl = cb_diskioctl,
554 .copyout = cb_copyout,
569 altcons_open(char *path)
576 * Allow stdio to be passed in so that the same string
577 * can be used for the bhyveload console and bhyve com-port
580 if (!strcmp(path, "stdio"))
583 err = stat(path, &sb);
585 if (!S_ISCHR(sb.st_mode))
588 fd = open(path, O_RDWR | O_NONBLOCK);
592 consin_fd = consout_fd = fd;
604 "usage: %s [-m mem-size] [-d <disk-path>] [-h <host-path>]\n"
605 " %*s [-e <name=value>] [-c <console-device>] <vmname>\n",
607 (int)strlen(progname), "");
612 main(int argc, char** argv)
615 void (*func)(struct loader_callbacks *, void *, int, int);
620 progname = basename(argv[0]);
625 consin_fd = STDIN_FILENO;
626 consout_fd = STDOUT_FILENO;
628 while ((opt = getopt(argc, argv, "c:d:e:h:m:")) != -1) {
631 error = altcons_open(optarg);
633 errx(EX_USAGE, "Could not open '%s'", optarg);
648 error = vm_parse_memsize(optarg, &mem_size);
650 errx(EX_USAGE, "Invalid memsize '%s'", optarg);
665 error = vm_create(vmname);
666 if (error != 0 && errno != EEXIST) {
672 ctx = vm_open(vmname);
678 error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
680 perror("vm_setup_memory");
684 tcgetattr(consout_fd, &term);
687 term.c_cflag |= CLOCAL;
689 tcsetattr(consout_fd, TCSAFLUSH, &term);
691 h = dlopen("/boot/userboot.so", RTLD_LOCAL);
693 printf("%s\n", dlerror());
696 func = dlsym(h, "loader_main");
698 printf("%s\n", dlerror());
703 disk_fd = open(disk_image, O_RDONLY);
706 addenv("smbios.bios.vendor=BHYVE");
707 addenv("boot_serial=1");
709 func(&cb, NULL, USERBOOT_VERSION_3, disk_fd >= 0);