1c487da1eSNeel Natu /*- 2c487da1eSNeel Natu * Copyright (c) 2011 NetApp, Inc. 3c487da1eSNeel Natu * All rights reserved. 4c487da1eSNeel Natu * 5c487da1eSNeel Natu * Redistribution and use in source and binary forms, with or without 6c487da1eSNeel Natu * modification, are permitted provided that the following conditions 7c487da1eSNeel Natu * are met: 8c487da1eSNeel Natu * 1. Redistributions of source code must retain the above copyright 9c487da1eSNeel Natu * notice, this list of conditions and the following disclaimer. 10c487da1eSNeel Natu * 2. Redistributions in binary form must reproduce the above copyright 11c487da1eSNeel Natu * notice, this list of conditions and the following disclaimer in the 12c487da1eSNeel Natu * documentation and/or other materials provided with the distribution. 13c487da1eSNeel Natu * 14c487da1eSNeel Natu * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 15c487da1eSNeel Natu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16c487da1eSNeel Natu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17c487da1eSNeel Natu * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 18c487da1eSNeel Natu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19c487da1eSNeel Natu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20c487da1eSNeel Natu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21c487da1eSNeel Natu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22c487da1eSNeel Natu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23c487da1eSNeel Natu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24c487da1eSNeel Natu * SUCH DAMAGE. 25c487da1eSNeel Natu * 26c487da1eSNeel Natu * $FreeBSD$ 27c487da1eSNeel Natu */ 28c487da1eSNeel Natu 29c487da1eSNeel Natu /*- 30c487da1eSNeel Natu * Copyright (c) 2011 Google, Inc. 31c487da1eSNeel Natu * All rights reserved. 32c487da1eSNeel Natu * 33c487da1eSNeel Natu * Redistribution and use in source and binary forms, with or without 34c487da1eSNeel Natu * modification, are permitted provided that the following conditions 35c487da1eSNeel Natu * are met: 36c487da1eSNeel Natu * 1. Redistributions of source code must retain the above copyright 37c487da1eSNeel Natu * notice, this list of conditions and the following disclaimer. 38c487da1eSNeel Natu * 2. Redistributions in binary form must reproduce the above copyright 39c487da1eSNeel Natu * notice, this list of conditions and the following disclaimer in the 40c487da1eSNeel Natu * documentation and/or other materials provided with the distribution. 41c487da1eSNeel Natu * 42c487da1eSNeel Natu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 43c487da1eSNeel Natu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 44c487da1eSNeel Natu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 45c487da1eSNeel Natu * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 46c487da1eSNeel Natu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 47c487da1eSNeel Natu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 48c487da1eSNeel Natu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 49c487da1eSNeel Natu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 50c487da1eSNeel Natu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 51c487da1eSNeel Natu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52c487da1eSNeel Natu * SUCH DAMAGE. 53c487da1eSNeel Natu * 54c487da1eSNeel Natu * $FreeBSD$ 55c487da1eSNeel Natu */ 56c487da1eSNeel Natu 57c487da1eSNeel Natu #include <sys/cdefs.h> 58c487da1eSNeel Natu __FBSDID("$FreeBSD$"); 59c487da1eSNeel Natu 60c487da1eSNeel Natu #include <sys/ioctl.h> 61c487da1eSNeel Natu #include <sys/stat.h> 62a10c6f55SNeel Natu #include <sys/disk.h> 63b6afa84bSNeel Natu #include <sys/queue.h> 64c487da1eSNeel Natu 65c487da1eSNeel Natu #include <machine/specialreg.h> 66c487da1eSNeel Natu #include <machine/vmm.h> 67c487da1eSNeel Natu 68c487da1eSNeel Natu #include <dirent.h> 69c487da1eSNeel Natu #include <dlfcn.h> 70c487da1eSNeel Natu #include <errno.h> 71200758f1SNeel Natu #include <err.h> 72c487da1eSNeel Natu #include <fcntl.h> 73c487da1eSNeel Natu #include <getopt.h> 74*b5331f4dSNeel Natu #include <libgen.h> 75c487da1eSNeel Natu #include <limits.h> 76c487da1eSNeel Natu #include <stdio.h> 77c487da1eSNeel Natu #include <stdlib.h> 78c487da1eSNeel Natu #include <string.h> 79200758f1SNeel Natu #include <sysexits.h> 80c487da1eSNeel Natu #include <termios.h> 81c487da1eSNeel Natu #include <unistd.h> 82c487da1eSNeel Natu 83c487da1eSNeel Natu #include <vmmapi.h> 84c487da1eSNeel Natu 85c487da1eSNeel Natu #include "userboot.h" 86c487da1eSNeel Natu 87c487da1eSNeel Natu #define MB (1024 * 1024UL) 88c487da1eSNeel Natu #define GB (1024 * 1024 * 1024UL) 89c487da1eSNeel Natu #define BSP 0 90c487da1eSNeel Natu 91c487da1eSNeel Natu static char *host_base = "/"; 92c487da1eSNeel Natu static struct termios term, oldterm; 93c487da1eSNeel Natu static int disk_fd = -1; 94c487da1eSNeel Natu 95b060ba50SNeel Natu static char *vmname, *progname; 96c487da1eSNeel Natu static struct vmctx *ctx; 97c487da1eSNeel Natu 98c487da1eSNeel Natu static uint64_t gdtbase, cr3, rsp; 99c487da1eSNeel Natu 100c487da1eSNeel Natu static void cb_exit(void *arg, int v); 101c487da1eSNeel Natu 102c487da1eSNeel Natu /* 103c487da1eSNeel Natu * Console i/o callbacks 104c487da1eSNeel Natu */ 105c487da1eSNeel Natu 106c487da1eSNeel Natu static void 107c487da1eSNeel Natu cb_putc(void *arg, int ch) 108c487da1eSNeel Natu { 109c487da1eSNeel Natu char c = ch; 110c487da1eSNeel Natu 111c487da1eSNeel Natu write(1, &c, 1); 112c487da1eSNeel Natu } 113c487da1eSNeel Natu 114c487da1eSNeel Natu static int 115c487da1eSNeel Natu cb_getc(void *arg) 116c487da1eSNeel Natu { 117c487da1eSNeel Natu char c; 118c487da1eSNeel Natu 119c487da1eSNeel Natu if (read(0, &c, 1) == 1) 120c487da1eSNeel Natu return (c); 121c487da1eSNeel Natu return (-1); 122c487da1eSNeel Natu } 123c487da1eSNeel Natu 124c487da1eSNeel Natu static int 125c487da1eSNeel Natu cb_poll(void *arg) 126c487da1eSNeel Natu { 127c487da1eSNeel Natu int n; 128c487da1eSNeel Natu 129c487da1eSNeel Natu if (ioctl(0, FIONREAD, &n) >= 0) 130c487da1eSNeel Natu return (n > 0); 131c487da1eSNeel Natu return (0); 132c487da1eSNeel Natu } 133c487da1eSNeel Natu 134c487da1eSNeel Natu /* 135c487da1eSNeel Natu * Host filesystem i/o callbacks 136c487da1eSNeel Natu */ 137c487da1eSNeel Natu 138c487da1eSNeel Natu struct cb_file { 139c487da1eSNeel Natu int cf_isdir; 140c487da1eSNeel Natu size_t cf_size; 141c487da1eSNeel Natu struct stat cf_stat; 142c487da1eSNeel Natu union { 143c487da1eSNeel Natu int fd; 144c487da1eSNeel Natu DIR *dir; 145c487da1eSNeel Natu } cf_u; 146c487da1eSNeel Natu }; 147c487da1eSNeel Natu 148c487da1eSNeel Natu static int 149c487da1eSNeel Natu cb_open(void *arg, const char *filename, void **hp) 150c487da1eSNeel Natu { 151c487da1eSNeel Natu struct stat st; 152c487da1eSNeel Natu struct cb_file *cf; 153c487da1eSNeel Natu char path[PATH_MAX]; 154c487da1eSNeel Natu 155c487da1eSNeel Natu if (!host_base) 156c487da1eSNeel Natu return (ENOENT); 157c487da1eSNeel Natu 158c487da1eSNeel Natu strlcpy(path, host_base, PATH_MAX); 159c487da1eSNeel Natu if (path[strlen(path) - 1] == '/') 160c487da1eSNeel Natu path[strlen(path) - 1] = 0; 161c487da1eSNeel Natu strlcat(path, filename, PATH_MAX); 162c487da1eSNeel Natu cf = malloc(sizeof(struct cb_file)); 163c487da1eSNeel Natu if (stat(path, &cf->cf_stat) < 0) { 164c487da1eSNeel Natu free(cf); 165c487da1eSNeel Natu return (errno); 166c487da1eSNeel Natu } 167c487da1eSNeel Natu 168c487da1eSNeel Natu cf->cf_size = st.st_size; 169c487da1eSNeel Natu if (S_ISDIR(cf->cf_stat.st_mode)) { 170c487da1eSNeel Natu cf->cf_isdir = 1; 171c487da1eSNeel Natu cf->cf_u.dir = opendir(path); 172c487da1eSNeel Natu if (!cf->cf_u.dir) 173c487da1eSNeel Natu goto out; 174c487da1eSNeel Natu *hp = cf; 175c487da1eSNeel Natu return (0); 176c487da1eSNeel Natu } 177c487da1eSNeel Natu if (S_ISREG(cf->cf_stat.st_mode)) { 178c487da1eSNeel Natu cf->cf_isdir = 0; 179c487da1eSNeel Natu cf->cf_u.fd = open(path, O_RDONLY); 180c487da1eSNeel Natu if (cf->cf_u.fd < 0) 181c487da1eSNeel Natu goto out; 182c487da1eSNeel Natu *hp = cf; 183c487da1eSNeel Natu return (0); 184c487da1eSNeel Natu } 185c487da1eSNeel Natu 186c487da1eSNeel Natu out: 187c487da1eSNeel Natu free(cf); 188c487da1eSNeel Natu return (EINVAL); 189c487da1eSNeel Natu } 190c487da1eSNeel Natu 191c487da1eSNeel Natu static int 192c487da1eSNeel Natu cb_close(void *arg, void *h) 193c487da1eSNeel Natu { 194c487da1eSNeel Natu struct cb_file *cf = h; 195c487da1eSNeel Natu 196c487da1eSNeel Natu if (cf->cf_isdir) 197c487da1eSNeel Natu closedir(cf->cf_u.dir); 198c487da1eSNeel Natu else 199c487da1eSNeel Natu close(cf->cf_u.fd); 200c487da1eSNeel Natu free(cf); 201c487da1eSNeel Natu 202c487da1eSNeel Natu return (0); 203c487da1eSNeel Natu } 204c487da1eSNeel Natu 205c487da1eSNeel Natu static int 206c487da1eSNeel Natu cb_isdir(void *arg, void *h) 207c487da1eSNeel Natu { 208c487da1eSNeel Natu struct cb_file *cf = h; 209c487da1eSNeel Natu 210c487da1eSNeel Natu return (cf->cf_isdir); 211c487da1eSNeel Natu } 212c487da1eSNeel Natu 213c487da1eSNeel Natu static int 214c487da1eSNeel Natu cb_read(void *arg, void *h, void *buf, size_t size, size_t *resid) 215c487da1eSNeel Natu { 216c487da1eSNeel Natu struct cb_file *cf = h; 217c487da1eSNeel Natu ssize_t sz; 218c487da1eSNeel Natu 219c487da1eSNeel Natu if (cf->cf_isdir) 220c487da1eSNeel Natu return (EINVAL); 221c487da1eSNeel Natu sz = read(cf->cf_u.fd, buf, size); 222c487da1eSNeel Natu if (sz < 0) 223c487da1eSNeel Natu return (EINVAL); 224c487da1eSNeel Natu *resid = size - sz; 225c487da1eSNeel Natu return (0); 226c487da1eSNeel Natu } 227c487da1eSNeel Natu 228c487da1eSNeel Natu static int 229c487da1eSNeel Natu cb_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return, 230c487da1eSNeel Natu size_t *namelen_return, char *name) 231c487da1eSNeel Natu { 232c487da1eSNeel Natu struct cb_file *cf = h; 233c487da1eSNeel Natu struct dirent *dp; 234c487da1eSNeel Natu 235c487da1eSNeel Natu if (!cf->cf_isdir) 236c487da1eSNeel Natu return (EINVAL); 237c487da1eSNeel Natu 238c487da1eSNeel Natu dp = readdir(cf->cf_u.dir); 239c487da1eSNeel Natu if (!dp) 240c487da1eSNeel Natu return (ENOENT); 241c487da1eSNeel Natu 242c487da1eSNeel Natu /* 243c487da1eSNeel Natu * Note: d_namlen is in the range 0..255 and therefore less 244c487da1eSNeel Natu * than PATH_MAX so we don't need to test before copying. 245c487da1eSNeel Natu */ 246c487da1eSNeel Natu *fileno_return = dp->d_fileno; 247c487da1eSNeel Natu *type_return = dp->d_type; 248c487da1eSNeel Natu *namelen_return = dp->d_namlen; 249c487da1eSNeel Natu memcpy(name, dp->d_name, dp->d_namlen); 250c487da1eSNeel Natu name[dp->d_namlen] = 0; 251c487da1eSNeel Natu 252c487da1eSNeel Natu return (0); 253c487da1eSNeel Natu } 254c487da1eSNeel Natu 255c487da1eSNeel Natu static int 256c487da1eSNeel Natu cb_seek(void *arg, void *h, uint64_t offset, int whence) 257c487da1eSNeel Natu { 258c487da1eSNeel Natu struct cb_file *cf = h; 259c487da1eSNeel Natu 260c487da1eSNeel Natu if (cf->cf_isdir) 261c487da1eSNeel Natu return (EINVAL); 262c487da1eSNeel Natu if (lseek(cf->cf_u.fd, offset, whence) < 0) 263c487da1eSNeel Natu return (errno); 264c487da1eSNeel Natu return (0); 265c487da1eSNeel Natu } 266c487da1eSNeel Natu 267c487da1eSNeel Natu static int 268c487da1eSNeel Natu cb_stat(void *arg, void *h, int *mode, int *uid, int *gid, uint64_t *size) 269c487da1eSNeel Natu { 270c487da1eSNeel Natu struct cb_file *cf = h; 271c487da1eSNeel Natu 272c487da1eSNeel Natu *mode = cf->cf_stat.st_mode; 273c487da1eSNeel Natu *uid = cf->cf_stat.st_uid; 274c487da1eSNeel Natu *gid = cf->cf_stat.st_gid; 275c487da1eSNeel Natu *size = cf->cf_stat.st_size; 276c487da1eSNeel Natu return (0); 277c487da1eSNeel Natu } 278c487da1eSNeel Natu 279c487da1eSNeel Natu /* 280c487da1eSNeel Natu * Disk image i/o callbacks 281c487da1eSNeel Natu */ 282c487da1eSNeel Natu 283c487da1eSNeel Natu static int 284c487da1eSNeel Natu cb_diskread(void *arg, int unit, uint64_t from, void *to, size_t size, 285c487da1eSNeel Natu size_t *resid) 286c487da1eSNeel Natu { 287c487da1eSNeel Natu ssize_t n; 288c487da1eSNeel Natu 289c487da1eSNeel Natu if (unit != 0 || disk_fd == -1) 290c487da1eSNeel Natu return (EIO); 291c487da1eSNeel Natu n = pread(disk_fd, to, size, from); 292c487da1eSNeel Natu if (n < 0) 293c487da1eSNeel Natu return (errno); 294c487da1eSNeel Natu *resid = size - n; 295c487da1eSNeel Natu return (0); 296c487da1eSNeel Natu } 297c487da1eSNeel Natu 298a10c6f55SNeel Natu static int 299a10c6f55SNeel Natu cb_diskioctl(void *arg, int unit, u_long cmd, void *data) 300a10c6f55SNeel Natu { 301a10c6f55SNeel Natu struct stat sb; 302a10c6f55SNeel Natu 303a10c6f55SNeel Natu if (unit != 0 || disk_fd == -1) 304a10c6f55SNeel Natu return (EBADF); 305a10c6f55SNeel Natu 306a10c6f55SNeel Natu switch (cmd) { 307a10c6f55SNeel Natu case DIOCGSECTORSIZE: 308a10c6f55SNeel Natu *(u_int *)data = 512; 309a10c6f55SNeel Natu break; 310a10c6f55SNeel Natu case DIOCGMEDIASIZE: 311a10c6f55SNeel Natu if (fstat(disk_fd, &sb) == 0) 312a10c6f55SNeel Natu *(off_t *)data = sb.st_size; 313a10c6f55SNeel Natu else 314a10c6f55SNeel Natu return (ENOTTY); 315a10c6f55SNeel Natu break; 316a10c6f55SNeel Natu default: 317a10c6f55SNeel Natu return (ENOTTY); 318a10c6f55SNeel Natu } 319a10c6f55SNeel Natu 320a10c6f55SNeel Natu return (0); 321a10c6f55SNeel Natu } 322a10c6f55SNeel Natu 323c487da1eSNeel Natu /* 324c487da1eSNeel Natu * Guest virtual machine i/o callbacks 325c487da1eSNeel Natu */ 326c487da1eSNeel Natu static int 327c487da1eSNeel Natu cb_copyin(void *arg, const void *from, uint64_t to, size_t size) 328c487da1eSNeel Natu { 329b060ba50SNeel Natu char *ptr; 330c487da1eSNeel Natu 331c487da1eSNeel Natu to &= 0x7fffffff; 332b060ba50SNeel Natu 333b060ba50SNeel Natu ptr = vm_map_gpa(ctx, to, size); 334b060ba50SNeel Natu if (ptr == NULL) 335c487da1eSNeel Natu return (EFAULT); 336c487da1eSNeel Natu 337b060ba50SNeel Natu memcpy(ptr, from, size); 338c487da1eSNeel Natu return (0); 339c487da1eSNeel Natu } 340c487da1eSNeel Natu 341c487da1eSNeel Natu static int 342c487da1eSNeel Natu cb_copyout(void *arg, uint64_t from, void *to, size_t size) 343c487da1eSNeel Natu { 344b060ba50SNeel Natu char *ptr; 345c487da1eSNeel Natu 346c487da1eSNeel Natu from &= 0x7fffffff; 347b060ba50SNeel Natu 348b060ba50SNeel Natu ptr = vm_map_gpa(ctx, from, size); 349b060ba50SNeel Natu if (ptr == NULL) 350c487da1eSNeel Natu return (EFAULT); 351c487da1eSNeel Natu 352b060ba50SNeel Natu memcpy(to, ptr, size); 353c487da1eSNeel Natu return (0); 354c487da1eSNeel Natu } 355c487da1eSNeel Natu 356c487da1eSNeel Natu static void 357c487da1eSNeel Natu cb_setreg(void *arg, int r, uint64_t v) 358c487da1eSNeel Natu { 359c487da1eSNeel Natu int error; 360c487da1eSNeel Natu enum vm_reg_name vmreg; 361c487da1eSNeel Natu 362c487da1eSNeel Natu vmreg = VM_REG_LAST; 363c487da1eSNeel Natu 364c487da1eSNeel Natu switch (r) { 365c487da1eSNeel Natu case 4: 366c487da1eSNeel Natu vmreg = VM_REG_GUEST_RSP; 367c487da1eSNeel Natu rsp = v; 368c487da1eSNeel Natu break; 369c487da1eSNeel Natu default: 370c487da1eSNeel Natu break; 371c487da1eSNeel Natu } 372c487da1eSNeel Natu 373c487da1eSNeel Natu if (vmreg == VM_REG_LAST) { 374c487da1eSNeel Natu printf("test_setreg(%d): not implemented\n", r); 375c487da1eSNeel Natu cb_exit(NULL, USERBOOT_EXIT_QUIT); 376c487da1eSNeel Natu } 377c487da1eSNeel Natu 378c487da1eSNeel Natu error = vm_set_register(ctx, BSP, vmreg, v); 379c487da1eSNeel Natu if (error) { 380c487da1eSNeel Natu perror("vm_set_register"); 381c487da1eSNeel Natu cb_exit(NULL, USERBOOT_EXIT_QUIT); 382c487da1eSNeel Natu } 383c487da1eSNeel Natu } 384c487da1eSNeel Natu 385c487da1eSNeel Natu static void 386c487da1eSNeel Natu cb_setmsr(void *arg, int r, uint64_t v) 387c487da1eSNeel Natu { 388c487da1eSNeel Natu int error; 389c487da1eSNeel Natu enum vm_reg_name vmreg; 390c487da1eSNeel Natu 391c487da1eSNeel Natu vmreg = VM_REG_LAST; 392c487da1eSNeel Natu 393c487da1eSNeel Natu switch (r) { 394c487da1eSNeel Natu case MSR_EFER: 395c487da1eSNeel Natu vmreg = VM_REG_GUEST_EFER; 396c487da1eSNeel Natu break; 397c487da1eSNeel Natu default: 398c487da1eSNeel Natu break; 399c487da1eSNeel Natu } 400c487da1eSNeel Natu 401c487da1eSNeel Natu if (vmreg == VM_REG_LAST) { 402c487da1eSNeel Natu printf("test_setmsr(%d): not implemented\n", r); 403c487da1eSNeel Natu cb_exit(NULL, USERBOOT_EXIT_QUIT); 404c487da1eSNeel Natu } 405c487da1eSNeel Natu 406c487da1eSNeel Natu error = vm_set_register(ctx, BSP, vmreg, v); 407c487da1eSNeel Natu if (error) { 408c487da1eSNeel Natu perror("vm_set_msr"); 409c487da1eSNeel Natu cb_exit(NULL, USERBOOT_EXIT_QUIT); 410c487da1eSNeel Natu } 411c487da1eSNeel Natu } 412c487da1eSNeel Natu 413c487da1eSNeel Natu static void 414c487da1eSNeel Natu cb_setcr(void *arg, int r, uint64_t v) 415c487da1eSNeel Natu { 416c487da1eSNeel Natu int error; 417c487da1eSNeel Natu enum vm_reg_name vmreg; 418c487da1eSNeel Natu 419c487da1eSNeel Natu vmreg = VM_REG_LAST; 420c487da1eSNeel Natu 421c487da1eSNeel Natu switch (r) { 422c487da1eSNeel Natu case 0: 423c487da1eSNeel Natu vmreg = VM_REG_GUEST_CR0; 424c487da1eSNeel Natu break; 425c487da1eSNeel Natu case 3: 426c487da1eSNeel Natu vmreg = VM_REG_GUEST_CR3; 427c487da1eSNeel Natu cr3 = v; 428c487da1eSNeel Natu break; 429c487da1eSNeel Natu case 4: 430c487da1eSNeel Natu vmreg = VM_REG_GUEST_CR4; 431c487da1eSNeel Natu break; 432c487da1eSNeel Natu default: 433c487da1eSNeel Natu break; 434c487da1eSNeel Natu } 435c487da1eSNeel Natu 436c487da1eSNeel Natu if (vmreg == VM_REG_LAST) { 437c487da1eSNeel Natu printf("test_setcr(%d): not implemented\n", r); 438c487da1eSNeel Natu cb_exit(NULL, USERBOOT_EXIT_QUIT); 439c487da1eSNeel Natu } 440c487da1eSNeel Natu 441c487da1eSNeel Natu error = vm_set_register(ctx, BSP, vmreg, v); 442c487da1eSNeel Natu if (error) { 443c487da1eSNeel Natu perror("vm_set_cr"); 444c487da1eSNeel Natu cb_exit(NULL, USERBOOT_EXIT_QUIT); 445c487da1eSNeel Natu } 446c487da1eSNeel Natu } 447c487da1eSNeel Natu 448c487da1eSNeel Natu static void 449c487da1eSNeel Natu cb_setgdt(void *arg, uint64_t base, size_t size) 450c487da1eSNeel Natu { 451c487da1eSNeel Natu int error; 452c487da1eSNeel Natu 453c487da1eSNeel Natu error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0); 454c487da1eSNeel Natu if (error != 0) { 455c487da1eSNeel Natu perror("vm_set_desc(gdt)"); 456c487da1eSNeel Natu cb_exit(NULL, USERBOOT_EXIT_QUIT); 457c487da1eSNeel Natu } 458c487da1eSNeel Natu 459c487da1eSNeel Natu gdtbase = base; 460c487da1eSNeel Natu } 461c487da1eSNeel Natu 462c487da1eSNeel Natu static void 463c487da1eSNeel Natu cb_exec(void *arg, uint64_t rip) 464c487da1eSNeel Natu { 465c487da1eSNeel Natu int error; 466c487da1eSNeel Natu 467c487da1eSNeel Natu error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase, rsp); 468c487da1eSNeel Natu if (error) { 469c487da1eSNeel Natu perror("vm_setup_freebsd_registers"); 470c487da1eSNeel Natu cb_exit(NULL, USERBOOT_EXIT_QUIT); 471c487da1eSNeel Natu } 472c487da1eSNeel Natu 473c487da1eSNeel Natu cb_exit(NULL, 0); 474c487da1eSNeel Natu } 475c487da1eSNeel Natu 476c487da1eSNeel Natu /* 477c487da1eSNeel Natu * Misc 478c487da1eSNeel Natu */ 479c487da1eSNeel Natu 480c487da1eSNeel Natu static void 481c487da1eSNeel Natu cb_delay(void *arg, int usec) 482c487da1eSNeel Natu { 483c487da1eSNeel Natu 484c487da1eSNeel Natu usleep(usec); 485c487da1eSNeel Natu } 486c487da1eSNeel Natu 487c487da1eSNeel Natu static void 488c487da1eSNeel Natu cb_exit(void *arg, int v) 489c487da1eSNeel Natu { 490c487da1eSNeel Natu 491c487da1eSNeel Natu tcsetattr(0, TCSAFLUSH, &oldterm); 492c487da1eSNeel Natu exit(v); 493c487da1eSNeel Natu } 494c487da1eSNeel Natu 495c487da1eSNeel Natu static void 496c487da1eSNeel Natu cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem) 497c487da1eSNeel Natu { 498c487da1eSNeel Natu 499318224bbSNeel Natu vm_get_memory_seg(ctx, 0, ret_lowmem, NULL); 500318224bbSNeel Natu vm_get_memory_seg(ctx, 4 * GB, ret_highmem, NULL); 501c487da1eSNeel Natu } 502c487da1eSNeel Natu 503b6afa84bSNeel Natu struct env { 504b6afa84bSNeel Natu const char *str; /* name=value */ 505b6afa84bSNeel Natu SLIST_ENTRY(env) next; 506b6afa84bSNeel Natu }; 507b6afa84bSNeel Natu 508b6afa84bSNeel Natu static SLIST_HEAD(envhead, env) envhead; 509b6afa84bSNeel Natu 510b6afa84bSNeel Natu static void 511b6afa84bSNeel Natu addenv(const char *str) 512b6afa84bSNeel Natu { 513b6afa84bSNeel Natu struct env *env; 514b6afa84bSNeel Natu 515b6afa84bSNeel Natu env = malloc(sizeof(struct env)); 516b6afa84bSNeel Natu env->str = str; 517b6afa84bSNeel Natu SLIST_INSERT_HEAD(&envhead, env, next); 518b6afa84bSNeel Natu } 519b6afa84bSNeel Natu 520c3e9ce33SNeel Natu static const char * 521c3e9ce33SNeel Natu cb_getenv(void *arg, int num) 522c3e9ce33SNeel Natu { 523b6afa84bSNeel Natu int i; 524b6afa84bSNeel Natu struct env *env; 525c3e9ce33SNeel Natu 526b6afa84bSNeel Natu i = 0; 527b6afa84bSNeel Natu SLIST_FOREACH(env, &envhead, next) { 528b6afa84bSNeel Natu if (i == num) 529b6afa84bSNeel Natu return (env->str); 530b6afa84bSNeel Natu i++; 531b6afa84bSNeel Natu } 532c3e9ce33SNeel Natu 533c3e9ce33SNeel Natu return (NULL); 534c3e9ce33SNeel Natu } 535c3e9ce33SNeel Natu 536a10c6f55SNeel Natu static struct loader_callbacks cb = { 537c487da1eSNeel Natu .getc = cb_getc, 538c487da1eSNeel Natu .putc = cb_putc, 539c487da1eSNeel Natu .poll = cb_poll, 540c487da1eSNeel Natu 541c487da1eSNeel Natu .open = cb_open, 542c487da1eSNeel Natu .close = cb_close, 543c487da1eSNeel Natu .isdir = cb_isdir, 544c487da1eSNeel Natu .read = cb_read, 545c487da1eSNeel Natu .readdir = cb_readdir, 546c487da1eSNeel Natu .seek = cb_seek, 547c487da1eSNeel Natu .stat = cb_stat, 548c487da1eSNeel Natu 549c487da1eSNeel Natu .diskread = cb_diskread, 550a10c6f55SNeel Natu .diskioctl = cb_diskioctl, 551c487da1eSNeel Natu 552c487da1eSNeel Natu .copyin = cb_copyin, 553c487da1eSNeel Natu .copyout = cb_copyout, 554c487da1eSNeel Natu .setreg = cb_setreg, 555c487da1eSNeel Natu .setmsr = cb_setmsr, 556c487da1eSNeel Natu .setcr = cb_setcr, 557c487da1eSNeel Natu .setgdt = cb_setgdt, 558c487da1eSNeel Natu .exec = cb_exec, 559c487da1eSNeel Natu 560c487da1eSNeel Natu .delay = cb_delay, 561c487da1eSNeel Natu .exit = cb_exit, 562c487da1eSNeel Natu .getmem = cb_getmem, 563c3e9ce33SNeel Natu 564c3e9ce33SNeel Natu .getenv = cb_getenv, 565c487da1eSNeel Natu }; 566c487da1eSNeel Natu 567c487da1eSNeel Natu static void 568c487da1eSNeel Natu usage(void) 569c487da1eSNeel Natu { 570c487da1eSNeel Natu 571b060ba50SNeel Natu fprintf(stderr, 572*b5331f4dSNeel Natu "usage: %s [-m mem-size] [-d <disk-path>] [-h <host-path>]\n" 573*b5331f4dSNeel Natu " %*s [-e <name=value>] <vmname>\n", progname, 574*b5331f4dSNeel Natu (int)strlen(progname), ""); 575c487da1eSNeel Natu exit(1); 576c487da1eSNeel Natu } 577c487da1eSNeel Natu 578c487da1eSNeel Natu int 579c487da1eSNeel Natu main(int argc, char** argv) 580c487da1eSNeel Natu { 581c487da1eSNeel Natu void *h; 582a10c6f55SNeel Natu void (*func)(struct loader_callbacks *, void *, int, int); 583b060ba50SNeel Natu uint64_t mem_size; 584c487da1eSNeel Natu int opt, error; 585c487da1eSNeel Natu char *disk_image; 586c487da1eSNeel Natu 587*b5331f4dSNeel Natu progname = basename(argv[0]); 588c487da1eSNeel Natu 589b060ba50SNeel Natu mem_size = 256 * MB; 590c487da1eSNeel Natu disk_image = NULL; 591c487da1eSNeel Natu 592b6afa84bSNeel Natu while ((opt = getopt(argc, argv, "d:e:h:m:")) != -1) { 593c487da1eSNeel Natu switch (opt) { 594c487da1eSNeel Natu case 'd': 595c487da1eSNeel Natu disk_image = optarg; 596c487da1eSNeel Natu break; 597c487da1eSNeel Natu 598b6afa84bSNeel Natu case 'e': 599b6afa84bSNeel Natu addenv(optarg); 600b6afa84bSNeel Natu break; 601b6afa84bSNeel Natu 602c487da1eSNeel Natu case 'h': 603c487da1eSNeel Natu host_base = optarg; 604c487da1eSNeel Natu break; 605c487da1eSNeel Natu 606c487da1eSNeel Natu case 'm': 607200758f1SNeel Natu error = vm_parse_memsize(optarg, &mem_size); 608200758f1SNeel Natu if (error != 0) 609200758f1SNeel Natu errx(EX_USAGE, "Invalid memsize '%s'", optarg); 610c487da1eSNeel Natu break; 611c487da1eSNeel Natu case '?': 612c487da1eSNeel Natu usage(); 613c487da1eSNeel Natu } 614c487da1eSNeel Natu } 615c487da1eSNeel Natu 616c487da1eSNeel Natu argc -= optind; 617c487da1eSNeel Natu argv += optind; 618c487da1eSNeel Natu 619c487da1eSNeel Natu if (argc != 1) 620c487da1eSNeel Natu usage(); 621c487da1eSNeel Natu 622c487da1eSNeel Natu vmname = argv[0]; 623c487da1eSNeel Natu 624c487da1eSNeel Natu error = vm_create(vmname); 625c487da1eSNeel Natu if (error != 0 && errno != EEXIST) { 626c487da1eSNeel Natu perror("vm_create"); 627c487da1eSNeel Natu exit(1); 628c487da1eSNeel Natu 629c487da1eSNeel Natu } 630c487da1eSNeel Natu 631c487da1eSNeel Natu ctx = vm_open(vmname); 632c487da1eSNeel Natu if (ctx == NULL) { 633c487da1eSNeel Natu perror("vm_open"); 634c487da1eSNeel Natu exit(1); 635c487da1eSNeel Natu } 636c487da1eSNeel Natu 637b060ba50SNeel Natu error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL); 638c487da1eSNeel Natu if (error) { 639b060ba50SNeel Natu perror("vm_setup_memory"); 640c487da1eSNeel Natu exit(1); 641c487da1eSNeel Natu } 642c487da1eSNeel Natu 643c487da1eSNeel Natu tcgetattr(0, &term); 644c487da1eSNeel Natu oldterm = term; 645c487da1eSNeel Natu term.c_lflag &= ~(ICANON|ECHO); 646c487da1eSNeel Natu term.c_iflag &= ~ICRNL; 647c487da1eSNeel Natu tcsetattr(0, TCSAFLUSH, &term); 64838f1b189SPeter Grehan h = dlopen("/boot/userboot.so", RTLD_LOCAL); 649c487da1eSNeel Natu if (!h) { 650c487da1eSNeel Natu printf("%s\n", dlerror()); 651c487da1eSNeel Natu return (1); 652c487da1eSNeel Natu } 653c487da1eSNeel Natu func = dlsym(h, "loader_main"); 654c487da1eSNeel Natu if (!func) { 655c487da1eSNeel Natu printf("%s\n", dlerror()); 656c487da1eSNeel Natu return (1); 657c487da1eSNeel Natu } 658c487da1eSNeel Natu 659c487da1eSNeel Natu if (disk_image) { 660c487da1eSNeel Natu disk_fd = open(disk_image, O_RDONLY); 661c487da1eSNeel Natu } 662b6afa84bSNeel Natu 663b6afa84bSNeel Natu addenv("smbios.bios.vendor=BHYVE"); 664b6afa84bSNeel Natu addenv("boot_serial=1"); 665b6afa84bSNeel Natu 666c3e9ce33SNeel Natu func(&cb, NULL, USERBOOT_VERSION_3, disk_fd >= 0); 667c487da1eSNeel Natu } 668