2011-07-06 22:38:09 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2011 NetApp, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2011 Google, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
2012-11-11 03:26:14 +00:00
|
|
|
#include <sys/disk.h>
|
2013-10-17 00:28:35 +00:00
|
|
|
#include <sys/queue.h>
|
2011-07-06 22:38:09 +00:00
|
|
|
|
|
|
|
#include <machine/specialreg.h>
|
|
|
|
#include <machine/vmm.h>
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <errno.h>
|
2013-10-09 03:56:07 +00:00
|
|
|
#include <err.h>
|
2011-07-06 22:38:09 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
2013-10-23 21:42:53 +00:00
|
|
|
#include <libgen.h>
|
2011-07-06 22:38:09 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-10-09 03:56:07 +00:00
|
|
|
#include <sysexits.h>
|
2011-07-06 22:38:09 +00:00
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <vmmapi.h>
|
|
|
|
|
|
|
|
#include "userboot.h"
|
|
|
|
|
|
|
|
#define MB (1024 * 1024UL)
|
|
|
|
#define GB (1024 * 1024 * 1024UL)
|
|
|
|
#define BSP 0
|
|
|
|
|
2014-02-22 07:18:06 +00:00
|
|
|
#define NDISKS 32
|
|
|
|
|
2013-11-27 06:07:03 +00:00
|
|
|
static char *host_base;
|
2011-07-06 22:38:09 +00:00
|
|
|
static struct termios term, oldterm;
|
2014-02-22 07:18:06 +00:00
|
|
|
static int disk_fd[NDISKS];
|
|
|
|
static int ndisks;
|
2013-11-27 00:21:37 +00:00
|
|
|
static int consin_fd, consout_fd;
|
2011-07-06 22:38:09 +00:00
|
|
|
|
2013-03-18 22:38:30 +00:00
|
|
|
static char *vmname, *progname;
|
2011-07-06 22:38:09 +00:00
|
|
|
static struct vmctx *ctx;
|
|
|
|
|
|
|
|
static uint64_t gdtbase, cr3, rsp;
|
|
|
|
|
|
|
|
static void cb_exit(void *arg, int v);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Console i/o callbacks
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
cb_putc(void *arg, int ch)
|
|
|
|
{
|
|
|
|
char c = ch;
|
|
|
|
|
2013-11-27 00:21:37 +00:00
|
|
|
(void) write(consout_fd, &c, 1);
|
2011-07-06 22:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cb_getc(void *arg)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
2013-11-27 00:21:37 +00:00
|
|
|
if (read(consin_fd, &c, 1) == 1)
|
2011-07-06 22:38:09 +00:00
|
|
|
return (c);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cb_poll(void *arg)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
2013-11-27 00:21:37 +00:00
|
|
|
if (ioctl(consin_fd, FIONREAD, &n) >= 0)
|
2011-07-06 22:38:09 +00:00
|
|
|
return (n > 0);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Host filesystem i/o callbacks
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct cb_file {
|
|
|
|
int cf_isdir;
|
|
|
|
size_t cf_size;
|
|
|
|
struct stat cf_stat;
|
|
|
|
union {
|
|
|
|
int fd;
|
|
|
|
DIR *dir;
|
|
|
|
} cf_u;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
cb_open(void *arg, const char *filename, void **hp)
|
|
|
|
{
|
|
|
|
struct cb_file *cf;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
|
|
|
if (!host_base)
|
|
|
|
return (ENOENT);
|
|
|
|
|
|
|
|
strlcpy(path, host_base, PATH_MAX);
|
|
|
|
if (path[strlen(path) - 1] == '/')
|
|
|
|
path[strlen(path) - 1] = 0;
|
|
|
|
strlcat(path, filename, PATH_MAX);
|
|
|
|
cf = malloc(sizeof(struct cb_file));
|
|
|
|
if (stat(path, &cf->cf_stat) < 0) {
|
|
|
|
free(cf);
|
|
|
|
return (errno);
|
|
|
|
}
|
|
|
|
|
2016-04-06 00:01:03 +00:00
|
|
|
cf->cf_size = cf->cf_stat.st_size;
|
2011-07-06 22:38:09 +00:00
|
|
|
if (S_ISDIR(cf->cf_stat.st_mode)) {
|
|
|
|
cf->cf_isdir = 1;
|
|
|
|
cf->cf_u.dir = opendir(path);
|
|
|
|
if (!cf->cf_u.dir)
|
|
|
|
goto out;
|
|
|
|
*hp = cf;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (S_ISREG(cf->cf_stat.st_mode)) {
|
|
|
|
cf->cf_isdir = 0;
|
|
|
|
cf->cf_u.fd = open(path, O_RDONLY);
|
|
|
|
if (cf->cf_u.fd < 0)
|
|
|
|
goto out;
|
|
|
|
*hp = cf;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(cf);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cb_close(void *arg, void *h)
|
|
|
|
{
|
|
|
|
struct cb_file *cf = h;
|
|
|
|
|
|
|
|
if (cf->cf_isdir)
|
|
|
|
closedir(cf->cf_u.dir);
|
|
|
|
else
|
|
|
|
close(cf->cf_u.fd);
|
|
|
|
free(cf);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cb_isdir(void *arg, void *h)
|
|
|
|
{
|
|
|
|
struct cb_file *cf = h;
|
|
|
|
|
|
|
|
return (cf->cf_isdir);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cb_read(void *arg, void *h, void *buf, size_t size, size_t *resid)
|
|
|
|
{
|
|
|
|
struct cb_file *cf = h;
|
|
|
|
ssize_t sz;
|
|
|
|
|
|
|
|
if (cf->cf_isdir)
|
|
|
|
return (EINVAL);
|
|
|
|
sz = read(cf->cf_u.fd, buf, size);
|
|
|
|
if (sz < 0)
|
|
|
|
return (EINVAL);
|
|
|
|
*resid = size - sz;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cb_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
|
|
|
|
size_t *namelen_return, char *name)
|
|
|
|
{
|
|
|
|
struct cb_file *cf = h;
|
|
|
|
struct dirent *dp;
|
|
|
|
|
|
|
|
if (!cf->cf_isdir)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
dp = readdir(cf->cf_u.dir);
|
|
|
|
if (!dp)
|
|
|
|
return (ENOENT);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: d_namlen is in the range 0..255 and therefore less
|
|
|
|
* than PATH_MAX so we don't need to test before copying.
|
|
|
|
*/
|
|
|
|
*fileno_return = dp->d_fileno;
|
|
|
|
*type_return = dp->d_type;
|
|
|
|
*namelen_return = dp->d_namlen;
|
|
|
|
memcpy(name, dp->d_name, dp->d_namlen);
|
|
|
|
name[dp->d_namlen] = 0;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cb_seek(void *arg, void *h, uint64_t offset, int whence)
|
|
|
|
{
|
|
|
|
struct cb_file *cf = h;
|
|
|
|
|
|
|
|
if (cf->cf_isdir)
|
|
|
|
return (EINVAL);
|
|
|
|
if (lseek(cf->cf_u.fd, offset, whence) < 0)
|
|
|
|
return (errno);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cb_stat(void *arg, void *h, int *mode, int *uid, int *gid, uint64_t *size)
|
|
|
|
{
|
|
|
|
struct cb_file *cf = h;
|
|
|
|
|
|
|
|
*mode = cf->cf_stat.st_mode;
|
|
|
|
*uid = cf->cf_stat.st_uid;
|
|
|
|
*gid = cf->cf_stat.st_gid;
|
|
|
|
*size = cf->cf_stat.st_size;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disk image i/o callbacks
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
cb_diskread(void *arg, int unit, uint64_t from, void *to, size_t size,
|
|
|
|
size_t *resid)
|
|
|
|
{
|
|
|
|
ssize_t n;
|
|
|
|
|
2014-02-22 07:18:06 +00:00
|
|
|
if (unit < 0 || unit >= ndisks )
|
2011-07-06 22:38:09 +00:00
|
|
|
return (EIO);
|
2014-02-22 07:18:06 +00:00
|
|
|
n = pread(disk_fd[unit], to, size, from);
|
2011-07-06 22:38:09 +00:00
|
|
|
if (n < 0)
|
|
|
|
return (errno);
|
|
|
|
*resid = size - n;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2012-11-11 03:26:14 +00:00
|
|
|
static int
|
|
|
|
cb_diskioctl(void *arg, int unit, u_long cmd, void *data)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
|
2014-02-22 07:18:06 +00:00
|
|
|
if (unit < 0 || unit >= ndisks)
|
2012-11-11 03:26:14 +00:00
|
|
|
return (EBADF);
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case DIOCGSECTORSIZE:
|
|
|
|
*(u_int *)data = 512;
|
|
|
|
break;
|
|
|
|
case DIOCGMEDIASIZE:
|
2014-02-22 07:18:06 +00:00
|
|
|
if (fstat(disk_fd[unit], &sb) == 0)
|
2012-11-11 03:26:14 +00:00
|
|
|
*(off_t *)data = sb.st_size;
|
|
|
|
else
|
|
|
|
return (ENOTTY);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (ENOTTY);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2011-07-06 22:38:09 +00:00
|
|
|
/*
|
|
|
|
* Guest virtual machine i/o callbacks
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
|
|
|
|
{
|
2013-03-18 22:38:30 +00:00
|
|
|
char *ptr;
|
2011-07-06 22:38:09 +00:00
|
|
|
|
|
|
|
to &= 0x7fffffff;
|
|
|
|
|
2013-03-18 22:38:30 +00:00
|
|
|
ptr = vm_map_gpa(ctx, to, size);
|
|
|
|
if (ptr == NULL)
|
|
|
|
return (EFAULT);
|
2011-07-06 22:38:09 +00:00
|
|
|
|
2013-03-18 22:38:30 +00:00
|
|
|
memcpy(ptr, from, size);
|
2011-07-06 22:38:09 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cb_copyout(void *arg, uint64_t from, void *to, size_t size)
|
|
|
|
{
|
2013-03-18 22:38:30 +00:00
|
|
|
char *ptr;
|
2011-07-06 22:38:09 +00:00
|
|
|
|
|
|
|
from &= 0x7fffffff;
|
|
|
|
|
2013-03-18 22:38:30 +00:00
|
|
|
ptr = vm_map_gpa(ctx, from, size);
|
|
|
|
if (ptr == NULL)
|
|
|
|
return (EFAULT);
|
2011-07-06 22:38:09 +00:00
|
|
|
|
2013-03-18 22:38:30 +00:00
|
|
|
memcpy(to, ptr, size);
|
2011-07-06 22:38:09 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cb_setreg(void *arg, int r, uint64_t v)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
enum vm_reg_name vmreg;
|
|
|
|
|
|
|
|
vmreg = VM_REG_LAST;
|
|
|
|
|
|
|
|
switch (r) {
|
|
|
|
case 4:
|
|
|
|
vmreg = VM_REG_GUEST_RSP;
|
|
|
|
rsp = v;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vmreg == VM_REG_LAST) {
|
|
|
|
printf("test_setreg(%d): not implemented\n", r);
|
|
|
|
cb_exit(NULL, USERBOOT_EXIT_QUIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = vm_set_register(ctx, BSP, vmreg, v);
|
|
|
|
if (error) {
|
|
|
|
perror("vm_set_register");
|
|
|
|
cb_exit(NULL, USERBOOT_EXIT_QUIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cb_setmsr(void *arg, int r, uint64_t v)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
enum vm_reg_name vmreg;
|
|
|
|
|
|
|
|
vmreg = VM_REG_LAST;
|
|
|
|
|
|
|
|
switch (r) {
|
|
|
|
case MSR_EFER:
|
|
|
|
vmreg = VM_REG_GUEST_EFER;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vmreg == VM_REG_LAST) {
|
|
|
|
printf("test_setmsr(%d): not implemented\n", r);
|
|
|
|
cb_exit(NULL, USERBOOT_EXIT_QUIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = vm_set_register(ctx, BSP, vmreg, v);
|
|
|
|
if (error) {
|
|
|
|
perror("vm_set_msr");
|
|
|
|
cb_exit(NULL, USERBOOT_EXIT_QUIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cb_setcr(void *arg, int r, uint64_t v)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
enum vm_reg_name vmreg;
|
|
|
|
|
|
|
|
vmreg = VM_REG_LAST;
|
|
|
|
|
|
|
|
switch (r) {
|
|
|
|
case 0:
|
|
|
|
vmreg = VM_REG_GUEST_CR0;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
vmreg = VM_REG_GUEST_CR3;
|
|
|
|
cr3 = v;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
vmreg = VM_REG_GUEST_CR4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vmreg == VM_REG_LAST) {
|
|
|
|
printf("test_setcr(%d): not implemented\n", r);
|
|
|
|
cb_exit(NULL, USERBOOT_EXIT_QUIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = vm_set_register(ctx, BSP, vmreg, v);
|
|
|
|
if (error) {
|
|
|
|
perror("vm_set_cr");
|
|
|
|
cb_exit(NULL, USERBOOT_EXIT_QUIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cb_setgdt(void *arg, uint64_t base, size_t size)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
|
|
|
|
if (error != 0) {
|
|
|
|
perror("vm_set_desc(gdt)");
|
|
|
|
cb_exit(NULL, USERBOOT_EXIT_QUIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
gdtbase = base;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cb_exec(void *arg, uint64_t rip)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2014-02-05 04:39:03 +00:00
|
|
|
if (cr3 == 0)
|
|
|
|
error = vm_setup_freebsd_registers_i386(ctx, BSP, rip, gdtbase,
|
|
|
|
rsp);
|
|
|
|
else
|
|
|
|
error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase,
|
|
|
|
rsp);
|
2011-07-06 22:38:09 +00:00
|
|
|
if (error) {
|
|
|
|
perror("vm_setup_freebsd_registers");
|
|
|
|
cb_exit(NULL, USERBOOT_EXIT_QUIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
cb_exit(NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Misc
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
cb_delay(void *arg, int usec)
|
|
|
|
{
|
|
|
|
|
|
|
|
usleep(usec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cb_exit(void *arg, int v)
|
|
|
|
{
|
|
|
|
|
2013-11-27 00:21:37 +00:00
|
|
|
tcsetattr(consout_fd, TCSAFLUSH, &oldterm);
|
2011-07-06 22:38:09 +00:00
|
|
|
exit(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
|
|
|
|
{
|
|
|
|
|
2014-06-24 02:02:51 +00:00
|
|
|
*ret_lowmem = vm_get_lowmem_size(ctx);
|
|
|
|
*ret_highmem = vm_get_highmem_size(ctx);
|
2011-07-06 22:38:09 +00:00
|
|
|
}
|
|
|
|
|
2013-10-17 00:28:35 +00:00
|
|
|
struct env {
|
|
|
|
const char *str; /* name=value */
|
|
|
|
SLIST_ENTRY(env) next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static SLIST_HEAD(envhead, env) envhead;
|
|
|
|
|
|
|
|
static void
|
|
|
|
addenv(const char *str)
|
2012-11-06 21:48:45 +00:00
|
|
|
{
|
2013-10-17 00:28:35 +00:00
|
|
|
struct env *env;
|
2012-11-06 21:48:45 +00:00
|
|
|
|
2013-10-17 00:28:35 +00:00
|
|
|
env = malloc(sizeof(struct env));
|
|
|
|
env->str = str;
|
|
|
|
SLIST_INSERT_HEAD(&envhead, env, next);
|
|
|
|
}
|
2012-11-06 21:48:45 +00:00
|
|
|
|
2013-10-17 00:28:35 +00:00
|
|
|
static const char *
|
|
|
|
cb_getenv(void *arg, int num)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct env *env;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
SLIST_FOREACH(env, &envhead, next) {
|
|
|
|
if (i == num)
|
|
|
|
return (env->str);
|
|
|
|
i++;
|
|
|
|
}
|
2012-11-06 21:48:45 +00:00
|
|
|
|
2013-10-17 00:28:35 +00:00
|
|
|
return (NULL);
|
2012-11-06 21:48:45 +00:00
|
|
|
}
|
|
|
|
|
2016-02-26 16:12:20 +00:00
|
|
|
static int
|
|
|
|
cb_vm_set_register(void *arg, int vcpu, int reg, uint64_t val)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (vm_set_register(ctx, vcpu, reg, val));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cb_vm_set_desc(void *arg, int vcpu, int reg, uint64_t base, u_int limit,
|
|
|
|
u_int access)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (vm_set_desc(ctx, vcpu, reg, base, limit, access));
|
|
|
|
}
|
|
|
|
|
2012-11-11 03:26:14 +00:00
|
|
|
static struct loader_callbacks cb = {
|
2011-07-06 22:38:09 +00:00
|
|
|
.getc = cb_getc,
|
|
|
|
.putc = cb_putc,
|
|
|
|
.poll = cb_poll,
|
|
|
|
|
|
|
|
.open = cb_open,
|
|
|
|
.close = cb_close,
|
|
|
|
.isdir = cb_isdir,
|
|
|
|
.read = cb_read,
|
|
|
|
.readdir = cb_readdir,
|
|
|
|
.seek = cb_seek,
|
|
|
|
.stat = cb_stat,
|
|
|
|
|
|
|
|
.diskread = cb_diskread,
|
2012-11-11 03:26:14 +00:00
|
|
|
.diskioctl = cb_diskioctl,
|
2011-07-06 22:38:09 +00:00
|
|
|
|
|
|
|
.copyin = cb_copyin,
|
|
|
|
.copyout = cb_copyout,
|
|
|
|
.setreg = cb_setreg,
|
|
|
|
.setmsr = cb_setmsr,
|
|
|
|
.setcr = cb_setcr,
|
|
|
|
.setgdt = cb_setgdt,
|
|
|
|
.exec = cb_exec,
|
|
|
|
|
|
|
|
.delay = cb_delay,
|
|
|
|
.exit = cb_exit,
|
|
|
|
.getmem = cb_getmem,
|
2012-11-06 21:48:45 +00:00
|
|
|
|
|
|
|
.getenv = cb_getenv,
|
2016-02-26 16:12:20 +00:00
|
|
|
|
|
|
|
/* Version 4 additions */
|
|
|
|
.vm_set_register = cb_vm_set_register,
|
|
|
|
.vm_set_desc = cb_vm_set_desc,
|
2011-07-06 22:38:09 +00:00
|
|
|
};
|
|
|
|
|
2013-11-27 00:21:37 +00:00
|
|
|
static int
|
|
|
|
altcons_open(char *path)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
int err;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allow stdio to be passed in so that the same string
|
|
|
|
* can be used for the bhyveload console and bhyve com-port
|
|
|
|
* parameters
|
|
|
|
*/
|
|
|
|
if (!strcmp(path, "stdio"))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
err = stat(path, &sb);
|
|
|
|
if (err == 0) {
|
|
|
|
if (!S_ISCHR(sb.st_mode))
|
|
|
|
err = ENOTSUP;
|
|
|
|
else {
|
|
|
|
fd = open(path, O_RDWR | O_NONBLOCK);
|
|
|
|
if (fd < 0)
|
|
|
|
err = errno;
|
|
|
|
else
|
|
|
|
consin_fd = consout_fd = fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
2014-02-22 07:18:06 +00:00
|
|
|
static int
|
|
|
|
disk_open(char *path)
|
|
|
|
{
|
|
|
|
int err, fd;
|
|
|
|
|
2015-05-18 19:45:46 +00:00
|
|
|
if (ndisks >= NDISKS)
|
2014-02-22 07:18:06 +00:00
|
|
|
return (ERANGE);
|
|
|
|
|
|
|
|
err = 0;
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
|
|
|
|
if (fd > 0) {
|
|
|
|
disk_fd[ndisks] = fd;
|
|
|
|
ndisks++;
|
|
|
|
} else
|
|
|
|
err = errno;
|
|
|
|
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
2011-07-06 22:38:09 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
2013-03-18 22:38:30 +00:00
|
|
|
fprintf(stderr,
|
2015-06-18 06:00:17 +00:00
|
|
|
"usage: %s [-S][-c <console-device>] [-d <disk-path>] [-e <name=value>]\n"
|
2016-06-26 14:44:01 +00:00
|
|
|
" %*s [-h <host-path>] [-m memsize[K|k|M|m|G|g|T|t]] <vmname>\n",
|
2013-11-27 00:21:37 +00:00
|
|
|
progname,
|
2013-10-23 21:42:53 +00:00
|
|
|
(int)strlen(progname), "");
|
2011-07-06 22:38:09 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char** argv)
|
|
|
|
{
|
2015-10-08 02:28:22 +00:00
|
|
|
char *loader;
|
2011-07-06 22:38:09 +00:00
|
|
|
void *h;
|
2012-11-11 03:26:14 +00:00
|
|
|
void (*func)(struct loader_callbacks *, void *, int, int);
|
2013-03-18 22:38:30 +00:00
|
|
|
uint64_t mem_size;
|
2015-06-18 06:00:17 +00:00
|
|
|
int opt, error, need_reinit, memflags;
|
2011-07-06 22:38:09 +00:00
|
|
|
|
2013-10-23 21:42:53 +00:00
|
|
|
progname = basename(argv[0]);
|
2011-07-06 22:38:09 +00:00
|
|
|
|
2015-10-08 02:28:22 +00:00
|
|
|
loader = NULL;
|
|
|
|
|
2015-06-18 06:00:17 +00:00
|
|
|
memflags = 0;
|
2013-03-18 22:38:30 +00:00
|
|
|
mem_size = 256 * MB;
|
2011-07-06 22:38:09 +00:00
|
|
|
|
2013-11-27 00:21:37 +00:00
|
|
|
consin_fd = STDIN_FILENO;
|
|
|
|
consout_fd = STDOUT_FILENO;
|
|
|
|
|
2016-02-26 16:15:02 +00:00
|
|
|
while ((opt = getopt(argc, argv, "CSc:d:e:h:l:m:")) != -1) {
|
2011-07-06 22:38:09 +00:00
|
|
|
switch (opt) {
|
2013-11-27 00:21:37 +00:00
|
|
|
case 'c':
|
|
|
|
error = altcons_open(optarg);
|
|
|
|
if (error != 0)
|
|
|
|
errx(EX_USAGE, "Could not open '%s'", optarg);
|
|
|
|
break;
|
2014-02-22 07:18:06 +00:00
|
|
|
|
2011-07-06 22:38:09 +00:00
|
|
|
case 'd':
|
2014-02-22 07:18:06 +00:00
|
|
|
error = disk_open(optarg);
|
|
|
|
if (error != 0)
|
|
|
|
errx(EX_USAGE, "Could not open '%s'", optarg);
|
2011-07-06 22:38:09 +00:00
|
|
|
break;
|
|
|
|
|
2013-10-17 00:28:35 +00:00
|
|
|
case 'e':
|
|
|
|
addenv(optarg);
|
|
|
|
break;
|
|
|
|
|
2011-07-06 22:38:09 +00:00
|
|
|
case 'h':
|
|
|
|
host_base = optarg;
|
|
|
|
break;
|
|
|
|
|
2015-10-08 02:28:22 +00:00
|
|
|
case 'l':
|
|
|
|
if (loader != NULL)
|
|
|
|
errx(EX_USAGE, "-l can only be given once");
|
|
|
|
loader = strdup(optarg);
|
|
|
|
if (loader == NULL)
|
|
|
|
err(EX_OSERR, "malloc");
|
|
|
|
break;
|
|
|
|
|
2011-07-06 22:38:09 +00:00
|
|
|
case 'm':
|
2013-10-09 03:56:07 +00:00
|
|
|
error = vm_parse_memsize(optarg, &mem_size);
|
|
|
|
if (error != 0)
|
|
|
|
errx(EX_USAGE, "Invalid memsize '%s'", optarg);
|
2011-07-06 22:38:09 +00:00
|
|
|
break;
|
2016-02-26 16:15:02 +00:00
|
|
|
case 'C':
|
|
|
|
memflags |= VM_MEM_F_INCORE;
|
|
|
|
break;
|
2015-06-18 06:00:17 +00:00
|
|
|
case 'S':
|
|
|
|
memflags |= VM_MEM_F_WIRED;
|
|
|
|
break;
|
2011-07-06 22:38:09 +00:00
|
|
|
case '?':
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc != 1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
vmname = argv[0];
|
|
|
|
|
2014-06-07 21:36:52 +00:00
|
|
|
need_reinit = 0;
|
2011-07-06 22:38:09 +00:00
|
|
|
error = vm_create(vmname);
|
2014-06-07 21:36:52 +00:00
|
|
|
if (error) {
|
|
|
|
if (errno != EEXIST) {
|
|
|
|
perror("vm_create");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
need_reinit = 1;
|
2011-07-06 22:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx = vm_open(vmname);
|
|
|
|
if (ctx == NULL) {
|
|
|
|
perror("vm_open");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2014-06-07 21:36:52 +00:00
|
|
|
if (need_reinit) {
|
|
|
|
error = vm_reinit(ctx);
|
|
|
|
if (error) {
|
|
|
|
perror("vm_reinit");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 06:00:17 +00:00
|
|
|
vm_set_memflags(ctx, memflags);
|
2013-03-18 22:38:30 +00:00
|
|
|
error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
|
2011-07-06 22:38:09 +00:00
|
|
|
if (error) {
|
2013-03-18 22:38:30 +00:00
|
|
|
perror("vm_setup_memory");
|
2011-07-06 22:38:09 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-10-08 02:28:22 +00:00
|
|
|
if (loader == NULL) {
|
|
|
|
loader = strdup("/boot/userboot.so");
|
|
|
|
if (loader == NULL)
|
|
|
|
err(EX_OSERR, "malloc");
|
|
|
|
}
|
|
|
|
h = dlopen(loader, RTLD_LOCAL);
|
2011-07-06 22:38:09 +00:00
|
|
|
if (!h) {
|
|
|
|
printf("%s\n", dlerror());
|
2015-10-08 02:28:22 +00:00
|
|
|
free(loader);
|
2011-07-06 22:38:09 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
func = dlsym(h, "loader_main");
|
|
|
|
if (!func) {
|
|
|
|
printf("%s\n", dlerror());
|
2015-10-08 02:28:22 +00:00
|
|
|
free(loader);
|
2011-07-06 22:38:09 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2015-10-08 02:28:22 +00:00
|
|
|
tcgetattr(consout_fd, &term);
|
|
|
|
oldterm = term;
|
|
|
|
cfmakeraw(&term);
|
|
|
|
term.c_cflag |= CLOCAL;
|
|
|
|
|
|
|
|
tcsetattr(consout_fd, TCSAFLUSH, &term);
|
|
|
|
|
2013-10-17 00:28:35 +00:00
|
|
|
addenv("smbios.bios.vendor=BHYVE");
|
|
|
|
addenv("boot_serial=1");
|
|
|
|
|
2016-02-26 16:12:20 +00:00
|
|
|
func(&cb, NULL, USERBOOT_VERSION_4, ndisks);
|
2015-10-08 02:28:22 +00:00
|
|
|
|
|
|
|
free(loader);
|
|
|
|
return (0);
|
2011-07-06 22:38:09 +00:00
|
|
|
}
|