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> 63c487da1eSNeel Natu 64c487da1eSNeel Natu #include <machine/specialreg.h> 65c487da1eSNeel Natu #include <machine/vmm.h> 66c487da1eSNeel Natu 67c487da1eSNeel Natu #include <dirent.h> 68c487da1eSNeel Natu #include <dlfcn.h> 69c487da1eSNeel Natu #include <errno.h> 70c487da1eSNeel Natu #include <fcntl.h> 71c487da1eSNeel Natu #include <getopt.h> 72c487da1eSNeel Natu #include <limits.h> 73c487da1eSNeel Natu #include <stdio.h> 74c487da1eSNeel Natu #include <stdlib.h> 75c487da1eSNeel Natu #include <string.h> 76c487da1eSNeel Natu #include <termios.h> 77c487da1eSNeel Natu #include <unistd.h> 78c487da1eSNeel Natu 79c487da1eSNeel Natu #include <vmmapi.h> 80c487da1eSNeel Natu 81c487da1eSNeel Natu #include "userboot.h" 82c487da1eSNeel Natu 83c487da1eSNeel Natu #define MB (1024 * 1024UL) 84c487da1eSNeel Natu #define GB (1024 * 1024 * 1024UL) 85c487da1eSNeel Natu #define BSP 0 86c487da1eSNeel Natu 87c487da1eSNeel Natu static char *host_base = "/"; 88c487da1eSNeel Natu static struct termios term, oldterm; 89c487da1eSNeel Natu static int disk_fd = -1; 90c487da1eSNeel Natu 91b060ba50SNeel Natu static char *vmname, *progname; 92c487da1eSNeel Natu static struct vmctx *ctx; 93c487da1eSNeel Natu 94c487da1eSNeel Natu static uint64_t gdtbase, cr3, rsp; 95c487da1eSNeel Natu 96c487da1eSNeel Natu static void cb_exit(void *arg, int v); 97c487da1eSNeel Natu 98c487da1eSNeel Natu /* 99c487da1eSNeel Natu * Console i/o callbacks 100c487da1eSNeel Natu */ 101c487da1eSNeel Natu 102c487da1eSNeel Natu static void 103c487da1eSNeel Natu cb_putc(void *arg, int ch) 104c487da1eSNeel Natu { 105c487da1eSNeel Natu char c = ch; 106c487da1eSNeel Natu 107c487da1eSNeel Natu write(1, &c, 1); 108c487da1eSNeel Natu } 109c487da1eSNeel Natu 110c487da1eSNeel Natu static int 111c487da1eSNeel Natu cb_getc(void *arg) 112c487da1eSNeel Natu { 113c487da1eSNeel Natu char c; 114c487da1eSNeel Natu 115c487da1eSNeel Natu if (read(0, &c, 1) == 1) 116c487da1eSNeel Natu return (c); 117c487da1eSNeel Natu return (-1); 118c487da1eSNeel Natu } 119c487da1eSNeel Natu 120c487da1eSNeel Natu static int 121c487da1eSNeel Natu cb_poll(void *arg) 122c487da1eSNeel Natu { 123c487da1eSNeel Natu int n; 124c487da1eSNeel Natu 125c487da1eSNeel Natu if (ioctl(0, FIONREAD, &n) >= 0) 126c487da1eSNeel Natu return (n > 0); 127c487da1eSNeel Natu return (0); 128c487da1eSNeel Natu } 129c487da1eSNeel Natu 130c487da1eSNeel Natu /* 131c487da1eSNeel Natu * Host filesystem i/o callbacks 132c487da1eSNeel Natu */ 133c487da1eSNeel Natu 134c487da1eSNeel Natu struct cb_file { 135c487da1eSNeel Natu int cf_isdir; 136c487da1eSNeel Natu size_t cf_size; 137c487da1eSNeel Natu struct stat cf_stat; 138c487da1eSNeel Natu union { 139c487da1eSNeel Natu int fd; 140c487da1eSNeel Natu DIR *dir; 141c487da1eSNeel Natu } cf_u; 142c487da1eSNeel Natu }; 143c487da1eSNeel Natu 144c487da1eSNeel Natu static int 145c487da1eSNeel Natu cb_open(void *arg, const char *filename, void **hp) 146c487da1eSNeel Natu { 147c487da1eSNeel Natu struct stat st; 148c487da1eSNeel Natu struct cb_file *cf; 149c487da1eSNeel Natu char path[PATH_MAX]; 150c487da1eSNeel Natu 151c487da1eSNeel Natu if (!host_base) 152c487da1eSNeel Natu return (ENOENT); 153c487da1eSNeel Natu 154c487da1eSNeel Natu strlcpy(path, host_base, PATH_MAX); 155c487da1eSNeel Natu if (path[strlen(path) - 1] == '/') 156c487da1eSNeel Natu path[strlen(path) - 1] = 0; 157c487da1eSNeel Natu strlcat(path, filename, PATH_MAX); 158c487da1eSNeel Natu cf = malloc(sizeof(struct cb_file)); 159c487da1eSNeel Natu if (stat(path, &cf->cf_stat) < 0) { 160c487da1eSNeel Natu free(cf); 161c487da1eSNeel Natu return (errno); 162c487da1eSNeel Natu } 163c487da1eSNeel Natu 164c487da1eSNeel Natu cf->cf_size = st.st_size; 165c487da1eSNeel Natu if (S_ISDIR(cf->cf_stat.st_mode)) { 166c487da1eSNeel Natu cf->cf_isdir = 1; 167c487da1eSNeel Natu cf->cf_u.dir = opendir(path); 168c487da1eSNeel Natu if (!cf->cf_u.dir) 169c487da1eSNeel Natu goto out; 170c487da1eSNeel Natu *hp = cf; 171c487da1eSNeel Natu return (0); 172c487da1eSNeel Natu } 173c487da1eSNeel Natu if (S_ISREG(cf->cf_stat.st_mode)) { 174c487da1eSNeel Natu cf->cf_isdir = 0; 175c487da1eSNeel Natu cf->cf_u.fd = open(path, O_RDONLY); 176c487da1eSNeel Natu if (cf->cf_u.fd < 0) 177c487da1eSNeel Natu goto out; 178c487da1eSNeel Natu *hp = cf; 179c487da1eSNeel Natu return (0); 180c487da1eSNeel Natu } 181c487da1eSNeel Natu 182c487da1eSNeel Natu out: 183c487da1eSNeel Natu free(cf); 184c487da1eSNeel Natu return (EINVAL); 185c487da1eSNeel Natu } 186c487da1eSNeel Natu 187c487da1eSNeel Natu static int 188c487da1eSNeel Natu cb_close(void *arg, void *h) 189c487da1eSNeel Natu { 190c487da1eSNeel Natu struct cb_file *cf = h; 191c487da1eSNeel Natu 192c487da1eSNeel Natu if (cf->cf_isdir) 193c487da1eSNeel Natu closedir(cf->cf_u.dir); 194c487da1eSNeel Natu else 195c487da1eSNeel Natu close(cf->cf_u.fd); 196c487da1eSNeel Natu free(cf); 197c487da1eSNeel Natu 198c487da1eSNeel Natu return (0); 199c487da1eSNeel Natu } 200c487da1eSNeel Natu 201c487da1eSNeel Natu static int 202c487da1eSNeel Natu cb_isdir(void *arg, void *h) 203c487da1eSNeel Natu { 204c487da1eSNeel Natu struct cb_file *cf = h; 205c487da1eSNeel Natu 206c487da1eSNeel Natu return (cf->cf_isdir); 207c487da1eSNeel Natu } 208c487da1eSNeel Natu 209c487da1eSNeel Natu static int 210c487da1eSNeel Natu cb_read(void *arg, void *h, void *buf, size_t size, size_t *resid) 211c487da1eSNeel Natu { 212c487da1eSNeel Natu struct cb_file *cf = h; 213c487da1eSNeel Natu ssize_t sz; 214c487da1eSNeel Natu 215c487da1eSNeel Natu if (cf->cf_isdir) 216c487da1eSNeel Natu return (EINVAL); 217c487da1eSNeel Natu sz = read(cf->cf_u.fd, buf, size); 218c487da1eSNeel Natu if (sz < 0) 219c487da1eSNeel Natu return (EINVAL); 220c487da1eSNeel Natu *resid = size - sz; 221c487da1eSNeel Natu return (0); 222c487da1eSNeel Natu } 223c487da1eSNeel Natu 224c487da1eSNeel Natu static int 225c487da1eSNeel Natu cb_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return, 226c487da1eSNeel Natu size_t *namelen_return, char *name) 227c487da1eSNeel Natu { 228c487da1eSNeel Natu struct cb_file *cf = h; 229c487da1eSNeel Natu struct dirent *dp; 230c487da1eSNeel Natu 231c487da1eSNeel Natu if (!cf->cf_isdir) 232c487da1eSNeel Natu return (EINVAL); 233c487da1eSNeel Natu 234c487da1eSNeel Natu dp = readdir(cf->cf_u.dir); 235c487da1eSNeel Natu if (!dp) 236c487da1eSNeel Natu return (ENOENT); 237c487da1eSNeel Natu 238c487da1eSNeel Natu /* 239c487da1eSNeel Natu * Note: d_namlen is in the range 0..255 and therefore less 240c487da1eSNeel Natu * than PATH_MAX so we don't need to test before copying. 241c487da1eSNeel Natu */ 242c487da1eSNeel Natu *fileno_return = dp->d_fileno; 243c487da1eSNeel Natu *type_return = dp->d_type; 244c487da1eSNeel Natu *namelen_return = dp->d_namlen; 245c487da1eSNeel Natu memcpy(name, dp->d_name, dp->d_namlen); 246c487da1eSNeel Natu name[dp->d_namlen] = 0; 247c487da1eSNeel Natu 248c487da1eSNeel Natu return (0); 249c487da1eSNeel Natu } 250c487da1eSNeel Natu 251c487da1eSNeel Natu static int 252c487da1eSNeel Natu cb_seek(void *arg, void *h, uint64_t offset, int whence) 253c487da1eSNeel Natu { 254c487da1eSNeel Natu struct cb_file *cf = h; 255c487da1eSNeel Natu 256c487da1eSNeel Natu if (cf->cf_isdir) 257c487da1eSNeel Natu return (EINVAL); 258c487da1eSNeel Natu if (lseek(cf->cf_u.fd, offset, whence) < 0) 259c487da1eSNeel Natu return (errno); 260c487da1eSNeel Natu return (0); 261c487da1eSNeel Natu } 262c487da1eSNeel Natu 263c487da1eSNeel Natu static int 264c487da1eSNeel Natu cb_stat(void *arg, void *h, int *mode, int *uid, int *gid, uint64_t *size) 265c487da1eSNeel Natu { 266c487da1eSNeel Natu struct cb_file *cf = h; 267c487da1eSNeel Natu 268c487da1eSNeel Natu *mode = cf->cf_stat.st_mode; 269c487da1eSNeel Natu *uid = cf->cf_stat.st_uid; 270c487da1eSNeel Natu *gid = cf->cf_stat.st_gid; 271c487da1eSNeel Natu *size = cf->cf_stat.st_size; 272c487da1eSNeel Natu return (0); 273c487da1eSNeel Natu } 274c487da1eSNeel Natu 275c487da1eSNeel Natu /* 276c487da1eSNeel Natu * Disk image i/o callbacks 277c487da1eSNeel Natu */ 278c487da1eSNeel Natu 279c487da1eSNeel Natu static int 280c487da1eSNeel Natu cb_diskread(void *arg, int unit, uint64_t from, void *to, size_t size, 281c487da1eSNeel Natu size_t *resid) 282c487da1eSNeel Natu { 283c487da1eSNeel Natu ssize_t n; 284c487da1eSNeel Natu 285c487da1eSNeel Natu if (unit != 0 || disk_fd == -1) 286c487da1eSNeel Natu return (EIO); 287c487da1eSNeel Natu n = pread(disk_fd, to, size, from); 288c487da1eSNeel Natu if (n < 0) 289c487da1eSNeel Natu return (errno); 290c487da1eSNeel Natu *resid = size - n; 291c487da1eSNeel Natu return (0); 292c487da1eSNeel Natu } 293c487da1eSNeel Natu 294a10c6f55SNeel Natu static int 295a10c6f55SNeel Natu cb_diskioctl(void *arg, int unit, u_long cmd, void *data) 296a10c6f55SNeel Natu { 297a10c6f55SNeel Natu struct stat sb; 298a10c6f55SNeel Natu 299a10c6f55SNeel Natu if (unit != 0 || disk_fd == -1) 300a10c6f55SNeel Natu return (EBADF); 301a10c6f55SNeel Natu 302a10c6f55SNeel Natu switch (cmd) { 303a10c6f55SNeel Natu case DIOCGSECTORSIZE: 304a10c6f55SNeel Natu *(u_int *)data = 512; 305a10c6f55SNeel Natu break; 306a10c6f55SNeel Natu case DIOCGMEDIASIZE: 307a10c6f55SNeel Natu if (fstat(disk_fd, &sb) == 0) 308a10c6f55SNeel Natu *(off_t *)data = sb.st_size; 309a10c6f55SNeel Natu else 310a10c6f55SNeel Natu return (ENOTTY); 311a10c6f55SNeel Natu break; 312a10c6f55SNeel Natu default: 313a10c6f55SNeel Natu return (ENOTTY); 314a10c6f55SNeel Natu } 315a10c6f55SNeel Natu 316a10c6f55SNeel Natu return (0); 317a10c6f55SNeel Natu } 318a10c6f55SNeel Natu 319c487da1eSNeel Natu /* 320c487da1eSNeel Natu * Guest virtual machine i/o callbacks 321c487da1eSNeel Natu */ 322c487da1eSNeel Natu static int 323c487da1eSNeel Natu cb_copyin(void *arg, const void *from, uint64_t to, size_t size) 324c487da1eSNeel Natu { 325b060ba50SNeel Natu char *ptr; 326c487da1eSNeel Natu 327c487da1eSNeel Natu to &= 0x7fffffff; 328b060ba50SNeel Natu 329b060ba50SNeel Natu ptr = vm_map_gpa(ctx, to, size); 330b060ba50SNeel Natu if (ptr == NULL) 331c487da1eSNeel Natu return (EFAULT); 332c487da1eSNeel Natu 333b060ba50SNeel Natu memcpy(ptr, from, size); 334c487da1eSNeel Natu return (0); 335c487da1eSNeel Natu } 336c487da1eSNeel Natu 337c487da1eSNeel Natu static int 338c487da1eSNeel Natu cb_copyout(void *arg, uint64_t from, void *to, size_t size) 339c487da1eSNeel Natu { 340b060ba50SNeel Natu char *ptr; 341c487da1eSNeel Natu 342c487da1eSNeel Natu from &= 0x7fffffff; 343b060ba50SNeel Natu 344b060ba50SNeel Natu ptr = vm_map_gpa(ctx, from, size); 345b060ba50SNeel Natu if (ptr == NULL) 346c487da1eSNeel Natu return (EFAULT); 347c487da1eSNeel Natu 348b060ba50SNeel Natu memcpy(to, ptr, size); 349c487da1eSNeel Natu return (0); 350c487da1eSNeel Natu } 351c487da1eSNeel Natu 352c487da1eSNeel Natu static void 353c487da1eSNeel Natu cb_setreg(void *arg, int r, uint64_t v) 354c487da1eSNeel Natu { 355c487da1eSNeel Natu int error; 356c487da1eSNeel Natu enum vm_reg_name vmreg; 357c487da1eSNeel Natu 358c487da1eSNeel Natu vmreg = VM_REG_LAST; 359c487da1eSNeel Natu 360c487da1eSNeel Natu switch (r) { 361c487da1eSNeel Natu case 4: 362c487da1eSNeel Natu vmreg = VM_REG_GUEST_RSP; 363c487da1eSNeel Natu rsp = v; 364c487da1eSNeel Natu break; 365c487da1eSNeel Natu default: 366c487da1eSNeel Natu break; 367c487da1eSNeel Natu } 368c487da1eSNeel Natu 369c487da1eSNeel Natu if (vmreg == VM_REG_LAST) { 370c487da1eSNeel Natu printf("test_setreg(%d): not implemented\n", r); 371c487da1eSNeel Natu cb_exit(NULL, USERBOOT_EXIT_QUIT); 372c487da1eSNeel Natu } 373c487da1eSNeel Natu 374c487da1eSNeel Natu error = vm_set_register(ctx, BSP, vmreg, v); 375c487da1eSNeel Natu if (error) { 376c487da1eSNeel Natu perror("vm_set_register"); 377c487da1eSNeel Natu cb_exit(NULL, USERBOOT_EXIT_QUIT); 378c487da1eSNeel Natu } 379c487da1eSNeel Natu } 380c487da1eSNeel Natu 381c487da1eSNeel Natu static void 382c487da1eSNeel Natu cb_setmsr(void *arg, int r, uint64_t v) 383c487da1eSNeel Natu { 384c487da1eSNeel Natu int error; 385c487da1eSNeel Natu enum vm_reg_name vmreg; 386c487da1eSNeel Natu 387c487da1eSNeel Natu vmreg = VM_REG_LAST; 388c487da1eSNeel Natu 389c487da1eSNeel Natu switch (r) { 390c487da1eSNeel Natu case MSR_EFER: 391c487da1eSNeel Natu vmreg = VM_REG_GUEST_EFER; 392c487da1eSNeel Natu break; 393c487da1eSNeel Natu default: 394c487da1eSNeel Natu break; 395c487da1eSNeel Natu } 396c487da1eSNeel Natu 397c487da1eSNeel Natu if (vmreg == VM_REG_LAST) { 398c487da1eSNeel Natu printf("test_setmsr(%d): not implemented\n", r); 399c487da1eSNeel Natu cb_exit(NULL, USERBOOT_EXIT_QUIT); 400c487da1eSNeel Natu } 401c487da1eSNeel Natu 402c487da1eSNeel Natu error = vm_set_register(ctx, BSP, vmreg, v); 403c487da1eSNeel Natu if (error) { 404c487da1eSNeel Natu perror("vm_set_msr"); 405c487da1eSNeel Natu cb_exit(NULL, USERBOOT_EXIT_QUIT); 406c487da1eSNeel Natu } 407c487da1eSNeel Natu } 408c487da1eSNeel Natu 409c487da1eSNeel Natu static void 410c487da1eSNeel Natu cb_setcr(void *arg, int r, uint64_t v) 411c487da1eSNeel Natu { 412c487da1eSNeel Natu int error; 413c487da1eSNeel Natu enum vm_reg_name vmreg; 414c487da1eSNeel Natu 415c487da1eSNeel Natu vmreg = VM_REG_LAST; 416c487da1eSNeel Natu 417c487da1eSNeel Natu switch (r) { 418c487da1eSNeel Natu case 0: 419c487da1eSNeel Natu vmreg = VM_REG_GUEST_CR0; 420c487da1eSNeel Natu break; 421c487da1eSNeel Natu case 3: 422c487da1eSNeel Natu vmreg = VM_REG_GUEST_CR3; 423c487da1eSNeel Natu cr3 = v; 424c487da1eSNeel Natu break; 425c487da1eSNeel Natu case 4: 426c487da1eSNeel Natu vmreg = VM_REG_GUEST_CR4; 427c487da1eSNeel Natu break; 428c487da1eSNeel Natu default: 429c487da1eSNeel Natu break; 430c487da1eSNeel Natu } 431c487da1eSNeel Natu 432c487da1eSNeel Natu if (vmreg == VM_REG_LAST) { 433c487da1eSNeel Natu printf("test_setcr(%d): not implemented\n", r); 434c487da1eSNeel Natu cb_exit(NULL, USERBOOT_EXIT_QUIT); 435c487da1eSNeel Natu } 436c487da1eSNeel Natu 437c487da1eSNeel Natu error = vm_set_register(ctx, BSP, vmreg, v); 438c487da1eSNeel Natu if (error) { 439c487da1eSNeel Natu perror("vm_set_cr"); 440c487da1eSNeel Natu cb_exit(NULL, USERBOOT_EXIT_QUIT); 441c487da1eSNeel Natu } 442c487da1eSNeel Natu } 443c487da1eSNeel Natu 444c487da1eSNeel Natu static void 445c487da1eSNeel Natu cb_setgdt(void *arg, uint64_t base, size_t size) 446c487da1eSNeel Natu { 447c487da1eSNeel Natu int error; 448c487da1eSNeel Natu 449c487da1eSNeel Natu error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0); 450c487da1eSNeel Natu if (error != 0) { 451c487da1eSNeel Natu perror("vm_set_desc(gdt)"); 452c487da1eSNeel Natu cb_exit(NULL, USERBOOT_EXIT_QUIT); 453c487da1eSNeel Natu } 454c487da1eSNeel Natu 455c487da1eSNeel Natu gdtbase = base; 456c487da1eSNeel Natu } 457c487da1eSNeel Natu 458c487da1eSNeel Natu static void 459c487da1eSNeel Natu cb_exec(void *arg, uint64_t rip) 460c487da1eSNeel Natu { 461c487da1eSNeel Natu int error; 462c487da1eSNeel Natu 463c487da1eSNeel Natu error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase, rsp); 464c487da1eSNeel Natu if (error) { 465c487da1eSNeel Natu perror("vm_setup_freebsd_registers"); 466c487da1eSNeel Natu cb_exit(NULL, USERBOOT_EXIT_QUIT); 467c487da1eSNeel Natu } 468c487da1eSNeel Natu 469c487da1eSNeel Natu cb_exit(NULL, 0); 470c487da1eSNeel Natu } 471c487da1eSNeel Natu 472c487da1eSNeel Natu /* 473c487da1eSNeel Natu * Misc 474c487da1eSNeel Natu */ 475c487da1eSNeel Natu 476c487da1eSNeel Natu static void 477c487da1eSNeel Natu cb_delay(void *arg, int usec) 478c487da1eSNeel Natu { 479c487da1eSNeel Natu 480c487da1eSNeel Natu usleep(usec); 481c487da1eSNeel Natu } 482c487da1eSNeel Natu 483c487da1eSNeel Natu static void 484c487da1eSNeel Natu cb_exit(void *arg, int v) 485c487da1eSNeel Natu { 486c487da1eSNeel Natu 487c487da1eSNeel Natu tcsetattr(0, TCSAFLUSH, &oldterm); 488c487da1eSNeel Natu exit(v); 489c487da1eSNeel Natu } 490c487da1eSNeel Natu 491c487da1eSNeel Natu static void 492c487da1eSNeel Natu cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem) 493c487da1eSNeel Natu { 494c487da1eSNeel Natu 495*318224bbSNeel Natu vm_get_memory_seg(ctx, 0, ret_lowmem, NULL); 496*318224bbSNeel Natu vm_get_memory_seg(ctx, 4 * GB, ret_highmem, NULL); 497c487da1eSNeel Natu } 498c487da1eSNeel Natu 499c3e9ce33SNeel Natu static const char * 500c3e9ce33SNeel Natu cb_getenv(void *arg, int num) 501c3e9ce33SNeel Natu { 502c3e9ce33SNeel Natu int max; 503c3e9ce33SNeel Natu 504c3e9ce33SNeel Natu static const char * var[] = { 505c3e9ce33SNeel Natu "smbios.bios.vendor=BHYVE", 506c3e9ce33SNeel Natu "boot_serial=1", 507c3e9ce33SNeel Natu NULL 508c3e9ce33SNeel Natu }; 509c3e9ce33SNeel Natu 510c3e9ce33SNeel Natu max = sizeof(var) / sizeof(var[0]); 511c3e9ce33SNeel Natu 512c3e9ce33SNeel Natu if (num < max) 513c3e9ce33SNeel Natu return (var[num]); 514c3e9ce33SNeel Natu else 515c3e9ce33SNeel Natu return (NULL); 516c3e9ce33SNeel Natu } 517c3e9ce33SNeel Natu 518a10c6f55SNeel Natu static struct loader_callbacks cb = { 519c487da1eSNeel Natu .getc = cb_getc, 520c487da1eSNeel Natu .putc = cb_putc, 521c487da1eSNeel Natu .poll = cb_poll, 522c487da1eSNeel Natu 523c487da1eSNeel Natu .open = cb_open, 524c487da1eSNeel Natu .close = cb_close, 525c487da1eSNeel Natu .isdir = cb_isdir, 526c487da1eSNeel Natu .read = cb_read, 527c487da1eSNeel Natu .readdir = cb_readdir, 528c487da1eSNeel Natu .seek = cb_seek, 529c487da1eSNeel Natu .stat = cb_stat, 530c487da1eSNeel Natu 531c487da1eSNeel Natu .diskread = cb_diskread, 532a10c6f55SNeel Natu .diskioctl = cb_diskioctl, 533c487da1eSNeel Natu 534c487da1eSNeel Natu .copyin = cb_copyin, 535c487da1eSNeel Natu .copyout = cb_copyout, 536c487da1eSNeel Natu .setreg = cb_setreg, 537c487da1eSNeel Natu .setmsr = cb_setmsr, 538c487da1eSNeel Natu .setcr = cb_setcr, 539c487da1eSNeel Natu .setgdt = cb_setgdt, 540c487da1eSNeel Natu .exec = cb_exec, 541c487da1eSNeel Natu 542c487da1eSNeel Natu .delay = cb_delay, 543c487da1eSNeel Natu .exit = cb_exit, 544c487da1eSNeel Natu .getmem = cb_getmem, 545c3e9ce33SNeel Natu 546c3e9ce33SNeel Natu .getenv = cb_getenv, 547c487da1eSNeel Natu }; 548c487da1eSNeel Natu 549c487da1eSNeel Natu static void 550c487da1eSNeel Natu usage(void) 551c487da1eSNeel Natu { 552c487da1eSNeel Natu 553b060ba50SNeel Natu fprintf(stderr, 554b060ba50SNeel Natu "usage: %s [-m mem-size][-d <disk-path>] [-h <host-path>] " 555c487da1eSNeel Natu "<vmname>\n", progname); 556c487da1eSNeel Natu exit(1); 557c487da1eSNeel Natu } 558c487da1eSNeel Natu 559c487da1eSNeel Natu int 560c487da1eSNeel Natu main(int argc, char** argv) 561c487da1eSNeel Natu { 562c487da1eSNeel Natu void *h; 563a10c6f55SNeel Natu void (*func)(struct loader_callbacks *, void *, int, int); 564b060ba50SNeel Natu uint64_t mem_size; 565c487da1eSNeel Natu int opt, error; 566c487da1eSNeel Natu char *disk_image; 567c487da1eSNeel Natu 568c487da1eSNeel Natu progname = argv[0]; 569c487da1eSNeel Natu 570b060ba50SNeel Natu mem_size = 256 * MB; 571c487da1eSNeel Natu disk_image = NULL; 572c487da1eSNeel Natu 573b060ba50SNeel Natu while ((opt = getopt(argc, argv, "d:h:m:")) != -1) { 574c487da1eSNeel Natu switch (opt) { 575c487da1eSNeel Natu case 'd': 576c487da1eSNeel Natu disk_image = optarg; 577c487da1eSNeel Natu break; 578c487da1eSNeel Natu 579c487da1eSNeel Natu case 'h': 580c487da1eSNeel Natu host_base = optarg; 581c487da1eSNeel Natu break; 582c487da1eSNeel Natu 583c487da1eSNeel Natu case 'm': 584b060ba50SNeel Natu mem_size = strtoul(optarg, NULL, 0) * MB; 585c487da1eSNeel Natu break; 586c487da1eSNeel Natu 587c487da1eSNeel Natu case '?': 588c487da1eSNeel Natu usage(); 589c487da1eSNeel Natu } 590c487da1eSNeel Natu } 591c487da1eSNeel Natu 592c487da1eSNeel Natu argc -= optind; 593c487da1eSNeel Natu argv += optind; 594c487da1eSNeel Natu 595c487da1eSNeel Natu if (argc != 1) 596c487da1eSNeel Natu usage(); 597c487da1eSNeel Natu 598c487da1eSNeel Natu vmname = argv[0]; 599c487da1eSNeel Natu 600c487da1eSNeel Natu error = vm_create(vmname); 601c487da1eSNeel Natu if (error != 0 && errno != EEXIST) { 602c487da1eSNeel Natu perror("vm_create"); 603c487da1eSNeel Natu exit(1); 604c487da1eSNeel Natu 605c487da1eSNeel Natu } 606c487da1eSNeel Natu 607c487da1eSNeel Natu ctx = vm_open(vmname); 608c487da1eSNeel Natu if (ctx == NULL) { 609c487da1eSNeel Natu perror("vm_open"); 610c487da1eSNeel Natu exit(1); 611c487da1eSNeel Natu } 612c487da1eSNeel Natu 613b060ba50SNeel Natu error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL); 614c487da1eSNeel Natu if (error) { 615b060ba50SNeel Natu perror("vm_setup_memory"); 616c487da1eSNeel Natu exit(1); 617c487da1eSNeel Natu } 618c487da1eSNeel Natu 619c487da1eSNeel Natu tcgetattr(0, &term); 620c487da1eSNeel Natu oldterm = term; 621c487da1eSNeel Natu term.c_lflag &= ~(ICANON|ECHO); 622c487da1eSNeel Natu term.c_iflag &= ~ICRNL; 623c487da1eSNeel Natu tcsetattr(0, TCSAFLUSH, &term); 62438f1b189SPeter Grehan h = dlopen("/boot/userboot.so", RTLD_LOCAL); 625c487da1eSNeel Natu if (!h) { 626c487da1eSNeel Natu printf("%s\n", dlerror()); 627c487da1eSNeel Natu return (1); 628c487da1eSNeel Natu } 629c487da1eSNeel Natu func = dlsym(h, "loader_main"); 630c487da1eSNeel Natu if (!func) { 631c487da1eSNeel Natu printf("%s\n", dlerror()); 632c487da1eSNeel Natu return (1); 633c487da1eSNeel Natu } 634c487da1eSNeel Natu 635c487da1eSNeel Natu if (disk_image) { 636c487da1eSNeel Natu disk_fd = open(disk_image, O_RDONLY); 637c487da1eSNeel Natu } 638c3e9ce33SNeel Natu func(&cb, NULL, USERBOOT_VERSION_3, disk_fd >= 0); 639c487da1eSNeel Natu } 640