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>
85 #define MB (1024 * 1024UL)
86 #define GB (1024 * 1024 * 1024UL)
89 static char *host_base = "/";
90 static struct termios term, oldterm;
91 static int disk_fd = -1;
93 static char *vmname, *progname;
94 static struct vmctx *ctx;
96 static uint64_t gdtbase, cr3, rsp;
98 static void cb_exit(void *arg, int v);
101 * Console i/o callbacks
105 cb_putc(void *arg, int ch)
117 if (read(0, &c, 1) == 1)
127 if (ioctl(0, FIONREAD, &n) >= 0)
133 * Host filesystem i/o callbacks
147 cb_open(void *arg, const char *filename, void **hp)
156 strlcpy(path, host_base, PATH_MAX);
157 if (path[strlen(path) - 1] == '/')
158 path[strlen(path) - 1] = 0;
159 strlcat(path, filename, PATH_MAX);
160 cf = malloc(sizeof(struct cb_file));
161 if (stat(path, &cf->cf_stat) < 0) {
166 cf->cf_size = st.st_size;
167 if (S_ISDIR(cf->cf_stat.st_mode)) {
169 cf->cf_u.dir = opendir(path);
175 if (S_ISREG(cf->cf_stat.st_mode)) {
177 cf->cf_u.fd = open(path, O_RDONLY);
190 cb_close(void *arg, void *h)
192 struct cb_file *cf = h;
195 closedir(cf->cf_u.dir);
204 cb_isdir(void *arg, void *h)
206 struct cb_file *cf = h;
208 return (cf->cf_isdir);
212 cb_read(void *arg, void *h, void *buf, size_t size, size_t *resid)
214 struct cb_file *cf = h;
219 sz = read(cf->cf_u.fd, buf, size);
227 cb_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
228 size_t *namelen_return, char *name)
230 struct cb_file *cf = h;
236 dp = readdir(cf->cf_u.dir);
241 * Note: d_namlen is in the range 0..255 and therefore less
242 * than PATH_MAX so we don't need to test before copying.
244 *fileno_return = dp->d_fileno;
245 *type_return = dp->d_type;
246 *namelen_return = dp->d_namlen;
247 memcpy(name, dp->d_name, dp->d_namlen);
248 name[dp->d_namlen] = 0;
254 cb_seek(void *arg, void *h, uint64_t offset, int whence)
256 struct cb_file *cf = h;
260 if (lseek(cf->cf_u.fd, offset, whence) < 0)
266 cb_stat(void *arg, void *h, int *mode, int *uid, int *gid, uint64_t *size)
268 struct cb_file *cf = h;
270 *mode = cf->cf_stat.st_mode;
271 *uid = cf->cf_stat.st_uid;
272 *gid = cf->cf_stat.st_gid;
273 *size = cf->cf_stat.st_size;
278 * Disk image i/o callbacks
282 cb_diskread(void *arg, int unit, uint64_t from, void *to, size_t size,
287 if (unit != 0 || disk_fd == -1)
289 n = pread(disk_fd, to, size, from);
297 cb_diskioctl(void *arg, int unit, u_long cmd, void *data)
301 if (unit != 0 || disk_fd == -1)
305 case DIOCGSECTORSIZE:
306 *(u_int *)data = 512;
309 if (fstat(disk_fd, &sb) == 0)
310 *(off_t *)data = sb.st_size;
322 * Guest virtual machine i/o callbacks
325 cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
331 ptr = vm_map_gpa(ctx, to, size);
335 memcpy(ptr, from, size);
340 cb_copyout(void *arg, uint64_t from, void *to, size_t size)
346 ptr = vm_map_gpa(ctx, from, size);
350 memcpy(to, ptr, size);
355 cb_setreg(void *arg, int r, uint64_t v)
358 enum vm_reg_name vmreg;
364 vmreg = VM_REG_GUEST_RSP;
371 if (vmreg == VM_REG_LAST) {
372 printf("test_setreg(%d): not implemented\n", r);
373 cb_exit(NULL, USERBOOT_EXIT_QUIT);
376 error = vm_set_register(ctx, BSP, vmreg, v);
378 perror("vm_set_register");
379 cb_exit(NULL, USERBOOT_EXIT_QUIT);
384 cb_setmsr(void *arg, int r, uint64_t v)
387 enum vm_reg_name vmreg;
393 vmreg = VM_REG_GUEST_EFER;
399 if (vmreg == VM_REG_LAST) {
400 printf("test_setmsr(%d): not implemented\n", r);
401 cb_exit(NULL, USERBOOT_EXIT_QUIT);
404 error = vm_set_register(ctx, BSP, vmreg, v);
406 perror("vm_set_msr");
407 cb_exit(NULL, USERBOOT_EXIT_QUIT);
412 cb_setcr(void *arg, int r, uint64_t v)
415 enum vm_reg_name vmreg;
421 vmreg = VM_REG_GUEST_CR0;
424 vmreg = VM_REG_GUEST_CR3;
428 vmreg = VM_REG_GUEST_CR4;
434 if (vmreg == VM_REG_LAST) {
435 printf("test_setcr(%d): not implemented\n", r);
436 cb_exit(NULL, USERBOOT_EXIT_QUIT);
439 error = vm_set_register(ctx, BSP, vmreg, v);
442 cb_exit(NULL, USERBOOT_EXIT_QUIT);
447 cb_setgdt(void *arg, uint64_t base, size_t size)
451 error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
453 perror("vm_set_desc(gdt)");
454 cb_exit(NULL, USERBOOT_EXIT_QUIT);
461 cb_exec(void *arg, uint64_t rip)
465 error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase, rsp);
467 perror("vm_setup_freebsd_registers");
468 cb_exit(NULL, USERBOOT_EXIT_QUIT);
479 cb_delay(void *arg, int usec)
486 cb_exit(void *arg, int v)
489 tcsetattr(0, TCSAFLUSH, &oldterm);
494 cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
497 vm_get_memory_seg(ctx, 0, ret_lowmem, NULL);
498 vm_get_memory_seg(ctx, 4 * GB, ret_highmem, NULL);
502 cb_getenv(void *arg, int num)
506 static const char * var[] = {
507 "smbios.bios.vendor=BHYVE",
512 max = sizeof(var) / sizeof(var[0]);
520 static struct loader_callbacks cb = {
529 .readdir = cb_readdir,
533 .diskread = cb_diskread,
534 .diskioctl = cb_diskioctl,
537 .copyout = cb_copyout,
556 "usage: %s [-m mem-size][-d <disk-path>] [-h <host-path>] "
557 "<vmname>\n", progname);
562 main(int argc, char** argv)
565 void (*func)(struct loader_callbacks *, void *, int, int);
575 while ((opt = getopt(argc, argv, "d:h:m:")) != -1) {
586 error = vm_parse_memsize(optarg, &mem_size);
588 errx(EX_USAGE, "Invalid memsize '%s'", optarg);
603 error = vm_create(vmname);
604 if (error != 0 && errno != EEXIST) {
610 ctx = vm_open(vmname);
616 error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
618 perror("vm_setup_memory");
624 term.c_lflag &= ~(ICANON|ECHO);
625 term.c_iflag &= ~ICRNL;
626 tcsetattr(0, TCSAFLUSH, &term);
627 h = dlopen("/boot/userboot.so", RTLD_LOCAL);
629 printf("%s\n", dlerror());
632 func = dlsym(h, "loader_main");
634 printf("%s\n", dlerror());
639 disk_fd = open(disk_image, O_RDONLY);
641 func(&cb, NULL, USERBOOT_VERSION_3, disk_fd >= 0);