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 <machine/specialreg.h>
64 #include <machine/vmm.h>
82 #define MB (1024 * 1024UL)
83 #define GB (1024 * 1024 * 1024UL)
86 static char *host_base = "/";
87 static struct termios term, oldterm;
88 static int disk_fd = -1;
90 static char *vmname, *progname, *membase;
91 static uint64_t lowmem, highmem;
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 * Guest virtual machine i/o callbacks
298 cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
304 if (to + size > lowmem)
307 memcpy(&membase[to], from, size);
313 cb_copyout(void *arg, uint64_t from, void *to, size_t size)
319 if (from + size > lowmem)
320 size = lowmem - from;
322 memcpy(to, &membase[from], size);
328 cb_setreg(void *arg, int r, uint64_t v)
331 enum vm_reg_name vmreg;
337 vmreg = VM_REG_GUEST_RSP;
344 if (vmreg == VM_REG_LAST) {
345 printf("test_setreg(%d): not implemented\n", r);
346 cb_exit(NULL, USERBOOT_EXIT_QUIT);
349 error = vm_set_register(ctx, BSP, vmreg, v);
351 perror("vm_set_register");
352 cb_exit(NULL, USERBOOT_EXIT_QUIT);
357 cb_setmsr(void *arg, int r, uint64_t v)
360 enum vm_reg_name vmreg;
366 vmreg = VM_REG_GUEST_EFER;
372 if (vmreg == VM_REG_LAST) {
373 printf("test_setmsr(%d): not implemented\n", r);
374 cb_exit(NULL, USERBOOT_EXIT_QUIT);
377 error = vm_set_register(ctx, BSP, vmreg, v);
379 perror("vm_set_msr");
380 cb_exit(NULL, USERBOOT_EXIT_QUIT);
385 cb_setcr(void *arg, int r, uint64_t v)
388 enum vm_reg_name vmreg;
394 vmreg = VM_REG_GUEST_CR0;
397 vmreg = VM_REG_GUEST_CR3;
401 vmreg = VM_REG_GUEST_CR4;
407 if (vmreg == VM_REG_LAST) {
408 printf("test_setcr(%d): not implemented\n", r);
409 cb_exit(NULL, USERBOOT_EXIT_QUIT);
412 error = vm_set_register(ctx, BSP, vmreg, v);
415 cb_exit(NULL, USERBOOT_EXIT_QUIT);
420 cb_setgdt(void *arg, uint64_t base, size_t size)
424 error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
426 perror("vm_set_desc(gdt)");
427 cb_exit(NULL, USERBOOT_EXIT_QUIT);
434 cb_exec(void *arg, uint64_t rip)
438 error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase, rsp);
440 perror("vm_setup_freebsd_registers");
441 cb_exit(NULL, USERBOOT_EXIT_QUIT);
452 cb_delay(void *arg, int usec)
459 cb_exit(void *arg, int v)
462 tcsetattr(0, TCSAFLUSH, &oldterm);
467 cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
470 *ret_lowmem = lowmem;
471 *ret_highmem = highmem;
474 static struct loader_callbacks_v1 cb = {
483 .readdir = cb_readdir,
487 .diskread = cb_diskread,
490 .copyout = cb_copyout,
506 printf("usage: %s [-d <disk image path>] [-h <host filesystem path>] "
507 "[-m <lowmem>][-M <highmem>] "
508 "<vmname>\n", progname);
513 main(int argc, char** argv)
516 void (*func)(struct loader_callbacks_v1 *, void *, int, int);
526 while ((opt = getopt(argc, argv, "d:h:m:M:")) != -1) {
537 lowmem = strtoul(optarg, NULL, 0) * MB;
541 highmem = strtoul(optarg, NULL, 0) * MB;
557 error = vm_create(vmname);
558 if (error != 0 && errno != EEXIST) {
564 ctx = vm_open(vmname);
570 error = vm_setup_memory(ctx, 0, lowmem, &membase);
572 perror("vm_setup_memory(lowmem)");
577 error = vm_setup_memory(ctx, 4 * GB, highmem, NULL);
579 perror("vm_setup_memory(highmem)");
586 term.c_lflag &= ~(ICANON|ECHO);
587 term.c_iflag &= ~ICRNL;
588 tcsetattr(0, TCSAFLUSH, &term);
589 h = dlopen("/boot/userboot.so", RTLD_LOCAL);
591 printf("%s\n", dlerror());
594 func = dlsym(h, "loader_main");
596 printf("%s\n", dlerror());
601 disk_fd = open(disk_image, O_RDONLY);
603 func(&cb, NULL, USERBOOT_VERSION_1, disk_fd >= 0);