#include <sys/types.h>
#ifndef WITHOUT_CAPSICUM
#include <sys/capsicum.h>
#endif
#include <sys/mman.h>
#include <sys/time.h>
#ifdef __FreeBSD__
#include <amd64/vmm/intel/vmcs.h>
#else
#include <sys/cpuset.h>
#include <intel/vmcs.h>
#endif
#include <machine/atomic.h>
#ifndef WITHOUT_CAPSICUM
#include <capsicum_helpers.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <errno.h>
#include <libgen.h>
#include <unistd.h>
#include <assert.h>
#include <pthread.h>
#include <pthread_np.h>
#include <sysexits.h>
#include <stdbool.h>
#include <stdint.h>
#include <machine/vmm.h>
#ifndef WITHOUT_CAPSICUM
#include <machine/vmm_dev.h>
#endif
#ifdef __FreeBSD__
#include <machine/vmm_instruction_emul.h>
#endif
#include <vmmapi.h>
#include "acpi.h"
#include "bhyverun.h"
#include "bootrom.h"
#include "config.h"
#include "debug.h"
#include "gdb.h"
#include "ioapic.h"
#include "mem.h"
#include "mevent.h"
#include "pci_emul.h"
#include "pci_lpc.h"
#include "qemu_fwcfg.h"
#include "tpm_device.h"
#include "spinup_ap.h"
#include "vmgenc.h"
#include "vmexit.h"
#ifndef __FreeBSD__
#include "smbiostbl.h"
#include "privileges.h"
#endif
#define MB (1024UL * 1024)
#define GB (1024UL * MB)
int guest_ncpus;
uint16_t cpu_cores, cpu_sockets, cpu_threads;
int raw_stdio = 0;
static const int BSP = 0;
static cpuset_t cpumask;
static void vm_loop(struct vmctx *ctx, struct vcpu *vcpu);
static struct vcpu_info {
struct vmctx *ctx;
struct vcpu *vcpu;
int vcpuid;
} *vcpu_info;
#ifdef __FreeBSD__
static cpuset_t **vcpumap;
#endif
int
bhyve_topology_parse(const char *opt)
{
char *cp, *str, *tofree;
if (*opt == '\0') {
set_config_value("sockets", "1");
set_config_value("cores", "1");
set_config_value("threads", "1");
set_config_value("cpus", "1");
return (0);
}
tofree = str = strdup(opt);
if (str == NULL)
errx(4, "Failed to allocate memory");
while ((cp = strsep(&str, ",")) != NULL) {
if (strncmp(cp, "cpus=", strlen("cpus=")) == 0)
set_config_value("cpus", cp + strlen("cpus="));
else if (strncmp(cp, "sockets=", strlen("sockets=")) == 0)
set_config_value("sockets", cp + strlen("sockets="));
else if (strncmp(cp, "cores=", strlen("cores=")) == 0)
set_config_value("cores", cp + strlen("cores="));
else if (strncmp(cp, "threads=", strlen("threads=")) == 0)
set_config_value("threads", cp + strlen("threads="));
else if (strchr(cp, '=') != NULL)
goto out;
else
set_config_value("cpus", cp);
}
free(tofree);
return (0);
out:
free(tofree);
return (-1);
}
static int
parse_int_value(const char *key, const char *value, int minval, int maxval)
{
char *cp;
long lval;
errno = 0;
lval = strtol(value, &cp, 0);
if (errno != 0 || *cp != '\0' || cp == value || lval < minval ||
lval > maxval)
errx(4, "Invalid value for %s: '%s'", key, value);
return (lval);
}
static void
calc_topology(void)
{
const char *value;
bool explicit_cpus;
uint64_t ncpus;
value = get_config_value("cpus");
if (value != NULL) {
guest_ncpus = parse_int_value("cpus", value, 1, UINT16_MAX);
explicit_cpus = true;
} else {
guest_ncpus = 1;
explicit_cpus = false;
}
value = get_config_value("cores");
if (value != NULL)
cpu_cores = parse_int_value("cores", value, 1, UINT16_MAX);
else
cpu_cores = 1;
value = get_config_value("threads");
if (value != NULL)
cpu_threads = parse_int_value("threads", value, 1, UINT16_MAX);
else
cpu_threads = 1;
value = get_config_value("sockets");
if (value != NULL)
cpu_sockets = parse_int_value("sockets", value, 1, UINT16_MAX);
else
cpu_sockets = guest_ncpus;
ncpus = (uint64_t)cpu_sockets * cpu_cores * cpu_threads;
if (ncpus > UINT16_MAX)
errx(4, "Computed number of vCPUs too high: %ju",
(uintmax_t)ncpus);
if (explicit_cpus) {
if (guest_ncpus != (int)ncpus)
errx(4, "Topology (%d sockets, %d cores, %d threads) "
"does not match %d vCPUs",
cpu_sockets, cpu_cores, cpu_threads,
guest_ncpus);
} else
guest_ncpus = ncpus;
}
#ifdef __FreeBSD__
int
bhyve_pincpu_parse(const char *opt)
{
int vcpu, pcpu;
const char *value;
char *newval;
char key[16];
if (sscanf(opt, "%d:%d", &vcpu, &pcpu) != 2) {
fprintf(stderr, "invalid format: %s\n", opt);
return (-1);
}
if (vcpu < 0) {
fprintf(stderr, "invalid vcpu '%d'\n", vcpu);
return (-1);
}
if (pcpu < 0 || pcpu >= CPU_SETSIZE) {
fprintf(stderr, "hostcpu '%d' outside valid range from "
"0 to %d\n", pcpu, CPU_SETSIZE - 1);
return (-1);
}
snprintf(key, sizeof(key), "vcpu.%d.cpuset", vcpu);
value = get_config_value(key);
if (asprintf(&newval, "%s%s%d", value != NULL ? value : "",
value != NULL ? "," : "", pcpu) == -1) {
perror("failed to build new cpuset string");
return (-1);
}
set_config_value(key, newval);
free(newval);
return (0);
}
static void
parse_cpuset(int vcpu, const char *list, cpuset_t *set)
{
char *cp, *token;
int pcpu, start;
CPU_ZERO(set);
start = -1;
token = __DECONST(char *, list);
for (;;) {
pcpu = strtoul(token, &cp, 0);
if (cp == token)
errx(4, "invalid cpuset for vcpu %d: '%s'", vcpu, list);
if (pcpu < 0 || pcpu >= CPU_SETSIZE)
errx(4, "hostcpu '%d' outside valid range from 0 to %d",
pcpu, CPU_SETSIZE - 1);
switch (*cp) {
case ',':
case '\0':
if (start >= 0) {
if (start > pcpu)
errx(4, "Invalid hostcpu range %d-%d",
start, pcpu);
while (start < pcpu) {
CPU_SET(start, set);
start++;
}
start = -1;
}
CPU_SET(pcpu, set);
break;
case '-':
if (start >= 0)
errx(4, "invalid cpuset for vcpu %d: '%s'",
vcpu, list);
start = pcpu;
break;
default:
errx(4, "invalid cpuset for vcpu %d: '%s'", vcpu, list);
}
if (*cp == '\0')
break;
token = cp + 1;
}
}
static void
build_vcpumaps(void)
{
char key[16];
const char *value;
int vcpu;
vcpumap = calloc(guest_ncpus, sizeof(*vcpumap));
for (vcpu = 0; vcpu < guest_ncpus; vcpu++) {
snprintf(key, sizeof(key), "vcpu.%d.cpuset", vcpu);
value = get_config_value(key);
if (value == NULL)
continue;
vcpumap[vcpu] = malloc(sizeof(cpuset_t));
if (vcpumap[vcpu] == NULL)
err(4, "Failed to allocate cpuset for vcpu %d", vcpu);
parse_cpuset(vcpu, value, vcpumap[vcpu]);
}
}
#endif
void *
paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
{
return (vm_map_gpa(ctx, gaddr, len));
}
bool
fbsdrun_virtio_msix(void)
{
return (get_config_bool_default("virtio.msix", true) &&
get_config_bool_default("virtio_msix", true));
}
struct vcpu *
fbsdrun_vcpu(int vcpuid)
{
return (vcpu_info[vcpuid].vcpu);
}
static void *
fbsdrun_start_thread(void *param)
{
char tname[MAXCOMLEN + 1];
struct vcpu_info *vi = param;
#ifdef __FreeBSD__
int error;
#endif
snprintf(tname, sizeof(tname), "vcpu %d", vi->vcpuid);
pthread_set_name_np(pthread_self(), tname);
#ifdef __FreeBSD__
if (vcpumap[vi->vcpuid] != NULL) {
error = pthread_setaffinity_np(pthread_self(),
sizeof(cpuset_t), vcpumap[vi->vcpuid]);
assert(error == 0);
}
#endif
gdb_cpu_add(vi->vcpu);
vm_loop(vi->ctx, vi->vcpu);
exit(1);
return (NULL);
}
void
fbsdrun_addcpu(int vcpuid, bool suspend)
{
struct vcpu_info *vi;
pthread_t thr;
int error;
vi = &vcpu_info[vcpuid];
error = vm_activate_cpu(vi->vcpu);
if (error != 0)
err(EX_OSERR, "could not activate CPU %d", vi->vcpuid);
CPU_SET_ATOMIC(vcpuid, &cpumask);
if (suspend) {
error = vm_suspend_cpu(vi->vcpu);
assert(error == 0);
}
error = pthread_create(&thr, NULL, fbsdrun_start_thread, vi);
assert(error == 0);
}
void
fbsdrun_deletecpu(int vcpu)
{
static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_lock(&resetcpu_mtx);
if (!CPU_ISSET(vcpu, &cpumask)) {
EPRINTLN("Attempting to delete unknown cpu %d", vcpu);
exit(4);
}
CPU_CLR(vcpu, &cpumask);
if (vcpu != BSP) {
pthread_cond_signal(&resetcpu_cond);
pthread_mutex_unlock(&resetcpu_mtx);
pthread_exit(NULL);
}
while (!CPU_EMPTY(&cpumask)) {
pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx);
}
pthread_mutex_unlock(&resetcpu_mtx);
}
static void
vm_loop(struct vmctx *ctx, struct vcpu *vcpu)
{
struct vm_exit vme;
int error, rc;
enum vm_exitcode exitcode;
cpuset_t active_cpus;
struct vm_entry *ventry;
error = vm_active_cpus(ctx, &active_cpus);
assert(CPU_ISSET(vcpu_id(vcpu), &active_cpus));
ventry = vmentry_vcpu(vcpu_id(vcpu));
while (1) {
error = vm_run(vcpu, ventry, &vme);
if (error != 0)
break;
if (ventry->cmd != VEC_DEFAULT) {
bzero(ventry, sizeof (*ventry));
}
exitcode = vme.exitcode;
if (exitcode >= VM_EXITCODE_MAX ||
vmexit_handlers[exitcode] == NULL) {
fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
exitcode);
exit(4);
}
rc = (*vmexit_handlers[exitcode])(ctx, vcpu, &vme);
switch (rc) {
case VMEXIT_CONTINUE:
break;
case VMEXIT_ABORT:
abort();
default:
exit(4);
}
}
EPRINTLN("vm_run error %d, errno %d", error, errno);
}
static int
num_vcpus_allowed(struct vmctx *ctx, struct vcpu *vcpu)
{
uint16_t sockets, cores, threads, maxcpus;
#ifdef __FreeBSD__
int tmp, error;
error = vm_get_capability(vcpu, VM_CAP_UNRESTRICTED_GUEST, &tmp);
if (error != 0)
return (1);
#else
int error;
#endif
error = vm_get_topology(ctx, &sockets, &cores, &threads, &maxcpus);
if (error == 0)
return (maxcpus);
else
return (1);
}
static struct vmctx *
do_open(const char *vmname)
{
struct vmctx *ctx;
int error;
bool reinit, romboot;
reinit = romboot = false;
romboot = bootrom_boot();
#ifndef __FreeBSD__
uint64_t create_flags = 0;
if (get_config_bool_default("memory.use_reservoir", false)) {
create_flags |= VCF_RESERVOIR_MEM;
}
error = vm_create(vmname, create_flags);
#else
error = vm_create(vmname);
#endif
if (error) {
if (errno == EEXIST) {
if (romboot) {
reinit = true;
} else {
}
} else {
perror("vm_create");
exit(4);
}
} else {
if (!romboot) {
fprintf(stderr, "virtual machine cannot be booted\n");
exit(4);
}
}
ctx = vm_open(vmname);
if (ctx == NULL) {
perror("vm_open");
#ifndef __FreeBSD__
if (errno == ENOENT) {
fprintf(stderr,
"Does the VM already exist in the global or "
"another zone?\n");
}
#endif
exit(4);
}
#ifndef WITHOUT_CAPSICUM
if (vm_limit_rights(ctx) != 0)
err(EX_OSERR, "vm_limit_rights");
#endif
if (reinit) {
#ifndef __FreeBSD__
error = vm_reinit(ctx, 0);
#else
error = vm_reinit(ctx);
#endif
if (error) {
perror("vm_reinit");
exit(4);
}
}
error = vm_set_topology(ctx, cpu_sockets, cpu_cores, cpu_threads, 0);
if (error)
errx(EX_OSERR, "vm_set_topology");
return (ctx);
}
bool
bhyve_parse_config_option(const char *option)
{
const char *value;
char *path;
value = strchr(option, '=');
if (value == NULL || value[1] == '\0')
return (false);
path = strndup(option, value - option);
if (path == NULL)
err(4, "Failed to allocate memory");
set_config_value(path, value + 1);
free(path);
return (true);
}
void
bhyve_parse_simple_config_file(const char *path)
{
FILE *fp;
char *line, *cp;
size_t linecap;
unsigned int lineno;
fp = fopen(path, "r");
if (fp == NULL)
err(4, "Failed to open configuration file %s", path);
line = NULL;
linecap = 0;
lineno = 1;
for (lineno = 1; getline(&line, &linecap, fp) > 0; lineno++) {
if (*line == '#' || *line == '\n')
continue;
cp = strchr(line, '\n');
if (cp != NULL)
*cp = '\0';
if (!bhyve_parse_config_option(line))
errx(4, "%s line %u: invalid config option '%s'", path,
lineno, line);
}
free(line);
fclose(fp);
}
void
bhyve_parse_gdb_options(const char *opt)
{
const char *sport;
char *colon;
if (opt[0] == 'w') {
set_config_bool("gdb.wait", true);
opt++;
}
colon = strrchr(opt, ':');
if (colon == NULL) {
sport = opt;
} else {
*colon = '\0';
colon++;
sport = colon;
set_config_value("gdb.address", opt);
}
set_config_value("gdb.port", sport);
}
int
main(int argc, char *argv[])
{
int error;
int max_vcpus, memflags;
struct vcpu *bsp;
struct vmctx *ctx;
size_t memsize;
const char *value, *vmname;
bhyve_init_config();
bhyve_optparse(argc, argv);
argc -= optind;
argv += optind;
if (argc > 1)
bhyve_usage(1);
if (argc == 1)
set_config_value("name", argv[0]);
vmname = get_config_value("name");
if (vmname == NULL)
bhyve_usage(1);
if (get_config_bool_default("config.dump", false)) {
dump_config();
exit(1);
}
#ifndef __FreeBSD__
illumos_priv_init();
#endif
calc_topology();
#ifdef __FreeBSD__
build_vcpumaps();
#endif
value = get_config_value("memory.size");
error = vm_parse_memsize(value, &memsize);
if (error)
errx(EX_USAGE, "invalid memsize '%s'", value);
ctx = do_open(vmname);
bsp = vm_vcpu_open(ctx, BSP);
max_vcpus = num_vcpus_allowed(ctx, bsp);
if (guest_ncpus > max_vcpus) {
fprintf(stderr, "%d vCPUs requested but only %d available\n",
guest_ncpus, max_vcpus);
exit(4);
}
bhyve_init_vcpu(bsp);
vcpu_info = calloc(guest_ncpus, sizeof(*vcpu_info));
for (int vcpuid = 0; vcpuid < guest_ncpus; vcpuid++) {
vcpu_info[vcpuid].ctx = ctx;
vcpu_info[vcpuid].vcpuid = vcpuid;
if (vcpuid == BSP)
vcpu_info[vcpuid].vcpu = bsp;
else
vcpu_info[vcpuid].vcpu = vm_vcpu_open(ctx, vcpuid);
}
memflags = 0;
if (get_config_bool_default("memory.wired", false))
memflags |= VM_MEM_F_WIRED;
if (get_config_bool_default("memory.guest_in_core", false))
memflags |= VM_MEM_F_INCORE;
vm_set_memflags(ctx, memflags);
#ifdef __FreeBSD__
error = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
#else
int _errno;
do {
errno = 0;
error = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
_errno = errno;
if (error != 0 && _errno == ENOMEM) {
(void) fprintf(stderr, "Unable to allocate memory "
"(%llu), retrying in 1 second\n", memsize);
sleep(1);
}
} while (_errno == ENOMEM);
#endif
if (error) {
fprintf(stderr, "Unable to set up memory (%d)\n", errno);
exit(4);
}
init_mem(guest_ncpus);
init_bootrom(ctx);
if (bhyve_init_platform(ctx, bsp) != 0)
exit(4);
if (qemu_fwcfg_init(ctx) != 0) {
fprintf(stderr, "qemu fwcfg initialization error\n");
exit(4);
}
if (qemu_fwcfg_add_file("opt/bhyve/hw.ncpu", sizeof(guest_ncpus),
&guest_ncpus) != 0) {
fprintf(stderr, "Could not add qemu fwcfg opt/bhyve/hw.ncpu\n");
exit(4);
}
if (init_pci(ctx) != 0) {
EPRINTLN("Device emulation initialization error: %s",
strerror(errno));
exit(4);
}
if (init_tpm(ctx) != 0) {
EPRINTLN("Failed to init TPM device");
exit(4);
}
if (get_config_bool("acpi_tables"))
vmgenc_init(ctx);
#ifdef __FreeBSD__
init_gdb(ctx);
#else
if (value != NULL) {
int port = atoi(value);
if (port < 0)
init_mdb(ctx);
else
init_gdb(ctx);
}
#endif
if (bootrom_boot()) {
#ifdef __FreeBSD__
if (vm_set_capability(bsp, VM_CAP_UNRESTRICTED_GUEST, 1)) {
fprintf(stderr, "ROM boot failed: unrestricted guest "
"capability not available\n");
exit(4);
}
#else
#endif
error = vcpu_reset(bsp);
assert(error == 0);
}
if (bhyve_init_platform_late(ctx, bsp) != 0)
exit(4);
setproctitle("%s", vmname);
#ifndef WITHOUT_CAPSICUM
caph_cache_catpages();
if (caph_limit_stdout() == -1 || caph_limit_stderr() == -1)
errx(EX_OSERR, "Unable to apply rights for sandbox");
if (caph_enter() == -1)
errx(EX_OSERR, "cap_enter() failed");
#endif
#ifndef __FreeBSD__
illumos_priv_lock();
#endif
#ifndef __FreeBSD__
if (vmentry_init(guest_ncpus) != 0)
err(EX_OSERR, "vmentry_init() failed");
#endif
for (int vcpuid = 0; vcpuid < guest_ncpus; vcpuid++) {
#ifdef __FreeBSD__
bool suspend = (vcpuid != BSP);
#else
bool suspend = vcpuid == BSP &&
get_config_bool_default("suspend_at_boot", false);
#endif
bhyve_start_vcpu(vcpu_info[vcpuid].vcpu, vcpuid == BSP,
suspend);
}
mevent_dispatch();
exit(4);
}