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>
64 #include <machine/specialreg.h>
65 #include <machine/vmm.h>
83 #define MB (1024 * 1024UL)
84 #define GB (1024 * 1024 * 1024UL)
87 static char *host_base = "/";
88 static struct termios term, oldterm;
89 static int disk_fd = -1;
91 static char *vmname, *progname;
92 static struct vmctx *ctx;
94 static uint64_t gdtbase, cr3, rsp;
96 static void cb_exit(void *arg, int v);
99 * Console i/o callbacks
103 cb_putc(void *arg, int ch)
115 if (read(0, &c, 1) == 1)
125 if (ioctl(0, FIONREAD, &n) >= 0)
131 * Host filesystem i/o callbacks
145 cb_open(void *arg, const char *filename, void **hp)
154 strlcpy(path, host_base, PATH_MAX);
155 if (path[strlen(path) - 1] == '/')
156 path[strlen(path) - 1] = 0;
157 strlcat(path, filename, PATH_MAX);
158 cf = malloc(sizeof(struct cb_file));
159 if (stat(path, &cf->cf_stat) < 0) {
164 cf->cf_size = st.st_size;
165 if (S_ISDIR(cf->cf_stat.st_mode)) {
167 cf->cf_u.dir = opendir(path);
173 if (S_ISREG(cf->cf_stat.st_mode)) {
175 cf->cf_u.fd = open(path, O_RDONLY);
188 cb_close(void *arg, void *h)
190 struct cb_file *cf = h;
193 closedir(cf->cf_u.dir);
202 cb_isdir(void *arg, void *h)
204 struct cb_file *cf = h;
206 return (cf->cf_isdir);
210 cb_read(void *arg, void *h, void *buf, size_t size, size_t *resid)
212 struct cb_file *cf = h;
217 sz = read(cf->cf_u.fd, buf, size);
225 cb_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
226 size_t *namelen_return, char *name)
228 struct cb_file *cf = h;
234 dp = readdir(cf->cf_u.dir);
239 * Note: d_namlen is in the range 0..255 and therefore less
240 * than PATH_MAX so we don't need to test before copying.
242 *fileno_return = dp->d_fileno;
243 *type_return = dp->d_type;
244 *namelen_return = dp->d_namlen;
245 memcpy(name, dp->d_name, dp->d_namlen);
246 name[dp->d_namlen] = 0;
252 cb_seek(void *arg, void *h, uint64_t offset, int whence)
254 struct cb_file *cf = h;
258 if (lseek(cf->cf_u.fd, offset, whence) < 0)
264 cb_stat(void *arg, void *h, int *mode, int *uid, int *gid, uint64_t *size)
266 struct cb_file *cf = h;
268 *mode = cf->cf_stat.st_mode;
269 *uid = cf->cf_stat.st_uid;
270 *gid = cf->cf_stat.st_gid;
271 *size = cf->cf_stat.st_size;
276 * Disk image i/o callbacks
280 cb_diskread(void *arg, int unit, uint64_t from, void *to, size_t size,
285 if (unit != 0 || disk_fd == -1)
287 n = pread(disk_fd, to, size, from);
295 cb_diskioctl(void *arg, int unit, u_long cmd, void *data)
299 if (unit != 0 || disk_fd == -1)
303 case DIOCGSECTORSIZE:
304 *(u_int *)data = 512;
307 if (fstat(disk_fd, &sb) == 0)
308 *(off_t *)data = sb.st_size;
320 * Guest virtual machine i/o callbacks
323 cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
329 ptr = vm_map_gpa(ctx, to, size);
333 memcpy(ptr, from, size);
338 cb_copyout(void *arg, uint64_t from, void *to, size_t size)
344 ptr = vm_map_gpa(ctx, from, size);
348 memcpy(to, ptr, size);
353 cb_setreg(void *arg, int r, uint64_t v)
356 enum vm_reg_name vmreg;
362 vmreg = VM_REG_GUEST_RSP;
369 if (vmreg == VM_REG_LAST) {
370 printf("test_setreg(%d): not implemented\n", r);
371 cb_exit(NULL, USERBOOT_EXIT_QUIT);
374 error = vm_set_register(ctx, BSP, vmreg, v);
376 perror("vm_set_register");
377 cb_exit(NULL, USERBOOT_EXIT_QUIT);
382 cb_setmsr(void *arg, int r, uint64_t v)
385 enum vm_reg_name vmreg;
391 vmreg = VM_REG_GUEST_EFER;
397 if (vmreg == VM_REG_LAST) {
398 printf("test_setmsr(%d): not implemented\n", r);
399 cb_exit(NULL, USERBOOT_EXIT_QUIT);
402 error = vm_set_register(ctx, BSP, vmreg, v);
404 perror("vm_set_msr");
405 cb_exit(NULL, USERBOOT_EXIT_QUIT);
410 cb_setcr(void *arg, int r, uint64_t v)
413 enum vm_reg_name vmreg;
419 vmreg = VM_REG_GUEST_CR0;
422 vmreg = VM_REG_GUEST_CR3;
426 vmreg = VM_REG_GUEST_CR4;
432 if (vmreg == VM_REG_LAST) {
433 printf("test_setcr(%d): not implemented\n", r);
434 cb_exit(NULL, USERBOOT_EXIT_QUIT);
437 error = vm_set_register(ctx, BSP, vmreg, v);
440 cb_exit(NULL, USERBOOT_EXIT_QUIT);
445 cb_setgdt(void *arg, uint64_t base, size_t size)
449 error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
451 perror("vm_set_desc(gdt)");
452 cb_exit(NULL, USERBOOT_EXIT_QUIT);
459 cb_exec(void *arg, uint64_t rip)
463 error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase, rsp);
465 perror("vm_setup_freebsd_registers");
466 cb_exit(NULL, USERBOOT_EXIT_QUIT);
477 cb_delay(void *arg, int usec)
484 cb_exit(void *arg, int v)
487 tcsetattr(0, TCSAFLUSH, &oldterm);
492 cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
495 vm_get_memory_seg(ctx, 0, ret_lowmem);
496 vm_get_memory_seg(ctx, 4 * GB, ret_highmem);
500 cb_getenv(void *arg, int num)
504 static const char * var[] = {
505 "smbios.bios.vendor=BHYVE",
510 max = sizeof(var) / sizeof(var[0]);
518 static struct loader_callbacks cb = {
527 .readdir = cb_readdir,
531 .diskread = cb_diskread,
532 .diskioctl = cb_diskioctl,
535 .copyout = cb_copyout,
554 "usage: %s [-m mem-size][-d <disk-path>] [-h <host-path>] "
555 "<vmname>\n", progname);
560 main(int argc, char** argv)
563 void (*func)(struct loader_callbacks *, void *, int, int);
573 while ((opt = getopt(argc, argv, "d:h:m:")) != -1) {
584 mem_size = strtoul(optarg, NULL, 0) * MB;
600 error = vm_create(vmname);
601 if (error != 0 && errno != EEXIST) {
607 ctx = vm_open(vmname);
613 error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
615 perror("vm_setup_memory");
621 term.c_lflag &= ~(ICANON|ECHO);
622 term.c_iflag &= ~ICRNL;
623 tcsetattr(0, TCSAFLUSH, &term);
624 h = dlopen("/boot/userboot.so", RTLD_LOCAL);
626 printf("%s\n", dlerror());
629 func = dlsym(h, "loader_main");
631 printf("%s\n", dlerror());
636 disk_fd = open(disk_image, O_RDONLY);
638 func(&cb, NULL, USERBOOT_VERSION_3, disk_fd >= 0);