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, *membase;
92 static uint64_t lowmem, highmem;
93 static struct vmctx *ctx;
95 static uint64_t gdtbase, cr3, rsp;
97 static void cb_exit(void *arg, int v);
100 * Console i/o callbacks
104 cb_putc(void *arg, int ch)
116 if (read(0, &c, 1) == 1)
126 if (ioctl(0, FIONREAD, &n) >= 0)
132 * Host filesystem i/o callbacks
146 cb_open(void *arg, const char *filename, void **hp)
155 strlcpy(path, host_base, PATH_MAX);
156 if (path[strlen(path) - 1] == '/')
157 path[strlen(path) - 1] = 0;
158 strlcat(path, filename, PATH_MAX);
159 cf = malloc(sizeof(struct cb_file));
160 if (stat(path, &cf->cf_stat) < 0) {
165 cf->cf_size = st.st_size;
166 if (S_ISDIR(cf->cf_stat.st_mode)) {
168 cf->cf_u.dir = opendir(path);
174 if (S_ISREG(cf->cf_stat.st_mode)) {
176 cf->cf_u.fd = open(path, O_RDONLY);
189 cb_close(void *arg, void *h)
191 struct cb_file *cf = h;
194 closedir(cf->cf_u.dir);
203 cb_isdir(void *arg, void *h)
205 struct cb_file *cf = h;
207 return (cf->cf_isdir);
211 cb_read(void *arg, void *h, void *buf, size_t size, size_t *resid)
213 struct cb_file *cf = h;
218 sz = read(cf->cf_u.fd, buf, size);
226 cb_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
227 size_t *namelen_return, char *name)
229 struct cb_file *cf = h;
235 dp = readdir(cf->cf_u.dir);
240 * Note: d_namlen is in the range 0..255 and therefore less
241 * than PATH_MAX so we don't need to test before copying.
243 *fileno_return = dp->d_fileno;
244 *type_return = dp->d_type;
245 *namelen_return = dp->d_namlen;
246 memcpy(name, dp->d_name, dp->d_namlen);
247 name[dp->d_namlen] = 0;
253 cb_seek(void *arg, void *h, uint64_t offset, int whence)
255 struct cb_file *cf = h;
259 if (lseek(cf->cf_u.fd, offset, whence) < 0)
265 cb_stat(void *arg, void *h, int *mode, int *uid, int *gid, uint64_t *size)
267 struct cb_file *cf = h;
269 *mode = cf->cf_stat.st_mode;
270 *uid = cf->cf_stat.st_uid;
271 *gid = cf->cf_stat.st_gid;
272 *size = cf->cf_stat.st_size;
277 * Disk image i/o callbacks
281 cb_diskread(void *arg, int unit, uint64_t from, void *to, size_t size,
286 if (unit != 0 || disk_fd == -1)
288 n = pread(disk_fd, to, size, from);
296 cb_diskioctl(void *arg, int unit, u_long cmd, void *data)
300 if (unit != 0 || disk_fd == -1)
304 case DIOCGSECTORSIZE:
305 *(u_int *)data = 512;
308 if (fstat(disk_fd, &sb) == 0)
309 *(off_t *)data = sb.st_size;
321 * Guest virtual machine i/o callbacks
324 cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
330 if (to + size > lowmem)
333 memcpy(&membase[to], from, size);
339 cb_copyout(void *arg, uint64_t from, void *to, size_t size)
345 if (from + size > lowmem)
346 size = lowmem - from;
348 memcpy(to, &membase[from], size);
354 cb_setreg(void *arg, int r, uint64_t v)
357 enum vm_reg_name vmreg;
363 vmreg = VM_REG_GUEST_RSP;
370 if (vmreg == VM_REG_LAST) {
371 printf("test_setreg(%d): not implemented\n", r);
372 cb_exit(NULL, USERBOOT_EXIT_QUIT);
375 error = vm_set_register(ctx, BSP, vmreg, v);
377 perror("vm_set_register");
378 cb_exit(NULL, USERBOOT_EXIT_QUIT);
383 cb_setmsr(void *arg, int r, uint64_t v)
386 enum vm_reg_name vmreg;
392 vmreg = VM_REG_GUEST_EFER;
398 if (vmreg == VM_REG_LAST) {
399 printf("test_setmsr(%d): not implemented\n", r);
400 cb_exit(NULL, USERBOOT_EXIT_QUIT);
403 error = vm_set_register(ctx, BSP, vmreg, v);
405 perror("vm_set_msr");
406 cb_exit(NULL, USERBOOT_EXIT_QUIT);
411 cb_setcr(void *arg, int r, uint64_t v)
414 enum vm_reg_name vmreg;
420 vmreg = VM_REG_GUEST_CR0;
423 vmreg = VM_REG_GUEST_CR3;
427 vmreg = VM_REG_GUEST_CR4;
433 if (vmreg == VM_REG_LAST) {
434 printf("test_setcr(%d): not implemented\n", r);
435 cb_exit(NULL, USERBOOT_EXIT_QUIT);
438 error = vm_set_register(ctx, BSP, vmreg, v);
441 cb_exit(NULL, USERBOOT_EXIT_QUIT);
446 cb_setgdt(void *arg, uint64_t base, size_t size)
450 error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
452 perror("vm_set_desc(gdt)");
453 cb_exit(NULL, USERBOOT_EXIT_QUIT);
460 cb_exec(void *arg, uint64_t rip)
464 error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase, rsp);
466 perror("vm_setup_freebsd_registers");
467 cb_exit(NULL, USERBOOT_EXIT_QUIT);
478 cb_delay(void *arg, int usec)
485 cb_exit(void *arg, int v)
488 tcsetattr(0, TCSAFLUSH, &oldterm);
493 cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
496 *ret_lowmem = lowmem;
497 *ret_highmem = highmem;
501 cb_getenv(void *arg, int num)
505 static const char * var[] = {
506 "smbios.bios.vendor=BHYVE",
511 max = sizeof(var) / sizeof(var[0]);
519 static struct loader_callbacks cb = {
528 .readdir = cb_readdir,
532 .diskread = cb_diskread,
533 .diskioctl = cb_diskioctl,
536 .copyout = cb_copyout,
554 printf("usage: %s [-d <disk image path>] [-h <host filesystem path>] "
555 "[-m <lowmem>][-M <highmem>] "
556 "<vmname>\n", progname);
561 main(int argc, char** argv)
564 void (*func)(struct loader_callbacks *, void *, int, int);
574 while ((opt = getopt(argc, argv, "d:h:m:M:")) != -1) {
585 lowmem = strtoul(optarg, NULL, 0) * MB;
589 highmem = strtoul(optarg, NULL, 0) * MB;
605 error = vm_create(vmname);
606 if (error != 0 && errno != EEXIST) {
612 ctx = vm_open(vmname);
618 error = vm_setup_memory(ctx, 0, lowmem, &membase);
620 perror("vm_setup_memory(lowmem)");
625 error = vm_setup_memory(ctx, 4 * GB, highmem, NULL);
627 perror("vm_setup_memory(highmem)");
634 term.c_lflag &= ~(ICANON|ECHO);
635 term.c_iflag &= ~ICRNL;
636 tcsetattr(0, TCSAFLUSH, &term);
637 h = dlopen("/boot/userboot.so", RTLD_LOCAL);
639 printf("%s\n", dlerror());
642 func = dlsym(h, "loader_main");
644 printf("%s\n", dlerror());
649 disk_fd = open(disk_image, O_RDONLY);
651 func(&cb, NULL, USERBOOT_VERSION_3, disk_fd >= 0);