2011-05-13 04:54:01 +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$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2013-12-07 22:18:36 +00:00
|
|
|
#include <machine/atomic.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
#include <machine/segments.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-10-23 21:42:53 +00:00
|
|
|
#include <string.h>
|
2013-10-09 03:56:07 +00:00
|
|
|
#include <err.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
2012-10-31 19:17:55 +00:00
|
|
|
#include <pthread_np.h>
|
2013-10-09 03:56:07 +00:00
|
|
|
#include <sysexits.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
#include <machine/vmm.h>
|
|
|
|
#include <vmmapi.h>
|
|
|
|
|
2012-12-13 01:58:11 +00:00
|
|
|
#include "bhyverun.h"
|
2012-11-20 07:01:26 +00:00
|
|
|
#include "acpi.h"
|
2011-05-13 04:54:01 +00:00
|
|
|
#include "inout.h"
|
|
|
|
#include "dbgport.h"
|
2014-01-29 14:56:48 +00:00
|
|
|
#include "ioapic.h"
|
2012-10-19 18:11:17 +00:00
|
|
|
#include "mem.h"
|
2011-05-13 04:54:01 +00:00
|
|
|
#include "mevent.h"
|
2012-10-26 13:40:12 +00:00
|
|
|
#include "mptbl.h"
|
2011-05-13 04:54:01 +00:00
|
|
|
#include "pci_emul.h"
|
2013-10-29 00:18:11 +00:00
|
|
|
#include "pci_lpc.h"
|
2014-03-04 17:12:06 +00:00
|
|
|
#include "smbiostbl.h"
|
2011-05-13 04:54:01 +00:00
|
|
|
#include "xmsr.h"
|
2012-09-25 02:33:25 +00:00
|
|
|
#include "spinup_ap.h"
|
2013-07-11 03:54:35 +00:00
|
|
|
#include "rtc.h"
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
#define GUEST_NIO_PORT 0x488 /* guest upcalls via i/o port */
|
|
|
|
|
|
|
|
#define VMEXIT_SWITCH 0 /* force vcpu switch in mux mode */
|
|
|
|
#define VMEXIT_CONTINUE 1 /* continue from next instruction */
|
|
|
|
#define VMEXIT_RESTART 2 /* restart current instruction */
|
|
|
|
#define VMEXIT_ABORT 3 /* abort the vm run loop */
|
|
|
|
#define VMEXIT_RESET 4 /* guest machine has reset */
|
2013-12-24 16:14:19 +00:00
|
|
|
#define VMEXIT_POWEROFF 5 /* guest machine has powered off */
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
#define MB (1024UL * 1024)
|
|
|
|
#define GB (1024UL * MB)
|
|
|
|
|
|
|
|
typedef int (*vmexit_handler_t)(struct vmctx *, struct vm_exit *, int *vcpu);
|
|
|
|
|
|
|
|
char *vmname;
|
|
|
|
|
|
|
|
int guest_ncpus;
|
2014-03-04 17:12:06 +00:00
|
|
|
char *guest_uuid_str;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
static int pincpu = -1;
|
2014-02-20 01:48:25 +00:00
|
|
|
static int guest_vmexit_on_hlt, guest_vmexit_on_pause;
|
2013-10-17 22:01:17 +00:00
|
|
|
static int virtio_msix = 1;
|
2014-02-20 01:48:25 +00:00
|
|
|
static int x2apic_mode = 0; /* default is xAPIC */
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2011-05-19 21:53:25 +00:00
|
|
|
static int strictio;
|
2013-12-19 22:27:28 +00:00
|
|
|
static int strictmsr = 1;
|
2011-05-19 21:53:25 +00:00
|
|
|
|
2012-11-20 07:01:26 +00:00
|
|
|
static int acpi;
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static char *progname;
|
|
|
|
static const int BSP = 0;
|
|
|
|
|
2014-03-20 18:15:37 +00:00
|
|
|
static cpuset_t cpumask;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
static void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip);
|
|
|
|
|
|
|
|
struct vm_exit vmexit[VM_MAXCPU];
|
|
|
|
|
2013-10-04 23:29:07 +00:00
|
|
|
struct bhyvestats {
|
2011-05-13 04:54:01 +00:00
|
|
|
uint64_t vmexit_bogus;
|
|
|
|
uint64_t vmexit_bogus_switch;
|
|
|
|
uint64_t vmexit_hlt;
|
|
|
|
uint64_t vmexit_pause;
|
|
|
|
uint64_t vmexit_mtrap;
|
2013-10-05 21:22:35 +00:00
|
|
|
uint64_t vmexit_inst_emul;
|
2011-05-13 04:54:01 +00:00
|
|
|
uint64_t cpu_switch_rotate;
|
|
|
|
uint64_t cpu_switch_direct;
|
|
|
|
int io_reset;
|
2014-04-29 18:42:56 +00:00
|
|
|
int io_poweroff;
|
2011-05-13 04:54:01 +00:00
|
|
|
} stats;
|
|
|
|
|
|
|
|
struct mt_vmm_info {
|
|
|
|
pthread_t mt_thr;
|
|
|
|
struct vmctx *mt_ctx;
|
|
|
|
int mt_vcpu;
|
|
|
|
} mt_vmm_info[VM_MAXCPU];
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(int code)
|
|
|
|
{
|
|
|
|
|
|
|
|
fprintf(stderr,
|
2014-01-27 22:26:15 +00:00
|
|
|
"Usage: %s [-aehwAHIPW] [-g <gdb port>] [-s <pci>]\n"
|
2013-10-29 00:18:11 +00:00
|
|
|
" %*s [-c vcpus] [-p pincpu] [-m mem] [-l <lpc>] <vm>\n"
|
2014-02-20 01:48:25 +00:00
|
|
|
" -a: local apic is in xAPIC mode (deprecated)\n"
|
2012-11-20 07:01:26 +00:00
|
|
|
" -A: create an ACPI table\n"
|
2013-10-08 16:36:17 +00:00
|
|
|
" -g: gdb port\n"
|
2011-05-13 04:54:01 +00:00
|
|
|
" -c: # cpus (default 1)\n"
|
|
|
|
" -p: pin vcpu 'n' to host cpu 'pincpu + n'\n"
|
|
|
|
" -H: vmexit from the guest on hlt\n"
|
|
|
|
" -P: vmexit from the guest on pause\n"
|
2013-10-23 21:42:53 +00:00
|
|
|
" -W: force virtio to use single-vector MSI\n"
|
|
|
|
" -e: exit on unhandled I/O access\n"
|
2011-05-13 04:54:01 +00:00
|
|
|
" -h: help\n"
|
|
|
|
" -s: <slot,driver,configinfo> PCI slot config\n"
|
2013-10-29 00:18:11 +00:00
|
|
|
" -l: LPC device configuration\n"
|
2013-12-19 22:27:28 +00:00
|
|
|
" -m: memory size in MB\n"
|
2014-02-20 01:48:25 +00:00
|
|
|
" -w: ignore unimplemented MSRs\n"
|
2014-03-04 17:12:06 +00:00
|
|
|
" -x: local apic is in x2APIC mode\n"
|
|
|
|
" -U: uuid\n",
|
2013-10-23 21:42:53 +00:00
|
|
|
progname, (int)strlen(progname), "");
|
2013-10-04 23:29:07 +00:00
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
exit(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2013-03-18 22:38:30 +00:00
|
|
|
paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
|
|
|
|
2013-03-18 22:38:30 +00:00
|
|
|
return (vm_map_gpa(ctx, gaddr, len));
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fbsdrun_vmexit_on_pause(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (guest_vmexit_on_pause);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fbsdrun_vmexit_on_hlt(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (guest_vmexit_on_hlt);
|
|
|
|
}
|
|
|
|
|
2013-10-17 22:01:17 +00:00
|
|
|
int
|
|
|
|
fbsdrun_virtio_msix(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (virtio_msix);
|
|
|
|
}
|
|
|
|
|
2011-05-15 04:03:11 +00:00
|
|
|
static void *
|
2011-05-13 04:54:01 +00:00
|
|
|
fbsdrun_start_thread(void *param)
|
|
|
|
{
|
2012-10-31 19:17:55 +00:00
|
|
|
char tname[MAXCOMLEN + 1];
|
|
|
|
struct mt_vmm_info *mtp;
|
2011-05-13 04:54:01 +00:00
|
|
|
int vcpu;
|
|
|
|
|
2012-10-31 19:17:55 +00:00
|
|
|
mtp = param;
|
2011-05-13 04:54:01 +00:00
|
|
|
vcpu = mtp->mt_vcpu;
|
2012-10-31 19:17:55 +00:00
|
|
|
|
2013-11-06 00:25:17 +00:00
|
|
|
snprintf(tname, sizeof(tname), "vcpu %d", vcpu);
|
2012-10-31 19:17:55 +00:00
|
|
|
pthread_set_name_np(mtp->mt_thr, tname);
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
vm_loop(mtp->mt_ctx, vcpu, vmexit[vcpu].rip);
|
|
|
|
|
|
|
|
/* not reached */
|
|
|
|
exit(1);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-03-20 18:15:37 +00:00
|
|
|
fbsdrun_addcpu(struct vmctx *ctx, int fromcpu, int newcpu, uint64_t rip)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2014-03-20 18:15:37 +00:00
|
|
|
assert(fromcpu == BSP);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2014-03-20 18:15:37 +00:00
|
|
|
CPU_SET_ATOMIC(newcpu, &cpumask);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the vmexit struct to allow execution to start
|
|
|
|
* at the given RIP
|
|
|
|
*/
|
2014-03-20 18:15:37 +00:00
|
|
|
vmexit[newcpu].rip = rip;
|
|
|
|
vmexit[newcpu].inst_length = 0;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2014-03-20 18:15:37 +00:00
|
|
|
mt_vmm_info[newcpu].mt_ctx = ctx;
|
|
|
|
mt_vmm_info[newcpu].mt_vcpu = newcpu;
|
2013-10-05 21:22:35 +00:00
|
|
|
|
2014-03-20 18:15:37 +00:00
|
|
|
error = pthread_create(&mt_vmm_info[newcpu].mt_thr, NULL,
|
|
|
|
fbsdrun_start_thread, &mt_vmm_info[newcpu]);
|
2013-10-05 21:22:35 +00:00
|
|
|
assert(error == 0);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
2013-12-07 22:18:36 +00:00
|
|
|
static int
|
|
|
|
fbsdrun_deletecpu(struct vmctx *ctx, int vcpu)
|
|
|
|
{
|
|
|
|
|
2014-03-20 18:15:37 +00:00
|
|
|
if (!CPU_ISSET(vcpu, &cpumask)) {
|
2013-12-07 22:18:36 +00:00
|
|
|
fprintf(stderr, "addcpu: attempting to delete unknown cpu %d\n",
|
|
|
|
vcpu);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2014-03-20 18:15:37 +00:00
|
|
|
CPU_CLR_ATOMIC(vcpu, &cpumask);
|
|
|
|
return (CPU_EMPTY(&cpumask));
|
2013-12-07 22:18:36 +00:00
|
|
|
}
|
|
|
|
|
2011-05-15 04:03:11 +00:00
|
|
|
static int
|
2011-05-13 04:54:01 +00:00
|
|
|
vmexit_catch_inout(void)
|
|
|
|
{
|
|
|
|
return (VMEXIT_ABORT);
|
|
|
|
}
|
|
|
|
|
2011-05-15 04:03:11 +00:00
|
|
|
static int
|
2011-05-13 04:54:01 +00:00
|
|
|
vmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu,
|
|
|
|
uint32_t eax)
|
|
|
|
{
|
2013-10-04 23:29:07 +00:00
|
|
|
#if BHYVE_DEBUG
|
|
|
|
/*
|
|
|
|
* put guest-driven debug here
|
|
|
|
*/
|
2011-05-13 04:54:01 +00:00
|
|
|
#endif
|
|
|
|
return (VMEXIT_CONTINUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
int bytes, port, in, out;
|
|
|
|
uint32_t eax;
|
|
|
|
int vcpu;
|
|
|
|
|
|
|
|
vcpu = *pvcpu;
|
|
|
|
|
|
|
|
port = vme->u.inout.port;
|
|
|
|
bytes = vme->u.inout.bytes;
|
|
|
|
eax = vme->u.inout.eax;
|
|
|
|
in = vme->u.inout.in;
|
|
|
|
out = !in;
|
|
|
|
|
|
|
|
/* We don't deal with these */
|
|
|
|
if (vme->u.inout.string || vme->u.inout.rep)
|
|
|
|
return (VMEXIT_ABORT);
|
|
|
|
|
|
|
|
/* Extra-special case of host notifications */
|
|
|
|
if (out && port == GUEST_NIO_PORT)
|
|
|
|
return (vmexit_handle_notify(ctx, vme, pvcpu, eax));
|
|
|
|
|
2011-05-19 21:53:25 +00:00
|
|
|
error = emulate_inout(ctx, vcpu, in, port, bytes, &eax, strictio);
|
2013-12-24 16:14:19 +00:00
|
|
|
if (error == INOUT_OK && in)
|
2011-05-13 04:54:01 +00:00
|
|
|
error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX, eax);
|
|
|
|
|
2013-12-24 16:14:19 +00:00
|
|
|
switch (error) {
|
|
|
|
case INOUT_OK:
|
2011-05-13 04:54:01 +00:00
|
|
|
return (VMEXIT_CONTINUE);
|
2013-12-24 16:14:19 +00:00
|
|
|
case INOUT_RESET:
|
2014-04-29 18:42:56 +00:00
|
|
|
stats.io_reset++;
|
2013-12-24 16:14:19 +00:00
|
|
|
return (VMEXIT_RESET);
|
|
|
|
case INOUT_POWEROFF:
|
2014-04-29 18:42:56 +00:00
|
|
|
stats.io_poweroff++;
|
2013-12-24 16:14:19 +00:00
|
|
|
return (VMEXIT_POWEROFF);
|
|
|
|
default:
|
2011-05-13 04:54:01 +00:00
|
|
|
fprintf(stderr, "Unhandled %s%c 0x%04x\n",
|
|
|
|
in ? "in" : "out",
|
|
|
|
bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'), port);
|
|
|
|
return (vmexit_catch_inout());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
|
|
|
|
{
|
2013-12-19 22:27:28 +00:00
|
|
|
uint64_t val;
|
|
|
|
uint32_t eax, edx;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
val = 0;
|
|
|
|
error = emulate_rdmsr(ctx, *pvcpu, vme->u.msr.code, &val);
|
|
|
|
if (error != 0) {
|
|
|
|
fprintf(stderr, "rdmsr to register %#x on vcpu %d\n",
|
|
|
|
vme->u.msr.code, *pvcpu);
|
2014-02-26 00:52:05 +00:00
|
|
|
if (strictmsr) {
|
|
|
|
error = vm_inject_exception2(ctx, *pvcpu, IDT_GP, 0);
|
|
|
|
assert(error == 0);
|
|
|
|
return (VMEXIT_RESTART);
|
|
|
|
}
|
2013-12-19 22:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eax = val;
|
|
|
|
error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RAX, eax);
|
|
|
|
assert(error == 0);
|
|
|
|
|
|
|
|
edx = val >> 32;
|
|
|
|
error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RDX, edx);
|
|
|
|
assert(error == 0);
|
|
|
|
|
|
|
|
return (VMEXIT_CONTINUE);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
|
|
|
|
{
|
2013-12-19 22:27:28 +00:00
|
|
|
int error;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2013-12-19 22:27:28 +00:00
|
|
|
error = emulate_wrmsr(ctx, *pvcpu, vme->u.msr.code, vme->u.msr.wval);
|
|
|
|
if (error != 0) {
|
|
|
|
fprintf(stderr, "wrmsr to register %#x(%#lx) on vcpu %d\n",
|
|
|
|
vme->u.msr.code, vme->u.msr.wval, *pvcpu);
|
2014-02-26 00:52:05 +00:00
|
|
|
if (strictmsr) {
|
|
|
|
error = vm_inject_exception2(ctx, *pvcpu, IDT_GP, 0);
|
|
|
|
assert(error == 0);
|
|
|
|
return (VMEXIT_RESTART);
|
|
|
|
}
|
2013-12-19 22:27:28 +00:00
|
|
|
}
|
|
|
|
return (VMEXIT_CONTINUE);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
2012-09-25 02:33:25 +00:00
|
|
|
static int
|
|
|
|
vmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
|
|
|
|
{
|
|
|
|
int newcpu;
|
|
|
|
int retval = VMEXIT_CONTINUE;
|
|
|
|
|
|
|
|
newcpu = spinup_ap(ctx, *pvcpu,
|
|
|
|
vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip);
|
|
|
|
|
|
|
|
return (retval);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static int
|
|
|
|
vmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
|
|
|
|
2012-10-31 03:29:52 +00:00
|
|
|
fprintf(stderr, "vm exit[%d]\n", *pvcpu);
|
|
|
|
fprintf(stderr, "\treason\t\tVMX\n");
|
|
|
|
fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip);
|
|
|
|
fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length);
|
2014-01-01 21:17:08 +00:00
|
|
|
fprintf(stderr, "\tstatus\t\t%d\n", vmexit->u.vmx.status);
|
2012-10-31 03:29:52 +00:00
|
|
|
fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason);
|
|
|
|
fprintf(stderr, "\tqualification\t0x%016lx\n",
|
|
|
|
vmexit->u.vmx.exit_qualification);
|
2014-01-01 21:17:08 +00:00
|
|
|
fprintf(stderr, "\tinst_type\t\t%d\n", vmexit->u.vmx.inst_type);
|
|
|
|
fprintf(stderr, "\tinst_error\t\t%d\n", vmexit->u.vmx.inst_error);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
return (VMEXIT_ABORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
2013-10-04 23:29:07 +00:00
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
stats.vmexit_bogus++;
|
|
|
|
|
2013-10-04 23:29:07 +00:00
|
|
|
return (VMEXIT_RESTART);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
2013-10-04 23:29:07 +00:00
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
stats.vmexit_hlt++;
|
|
|
|
|
2013-10-04 23:29:07 +00:00
|
|
|
/*
|
|
|
|
* Just continue execution with the next instruction. We use
|
|
|
|
* the HLT VM exit as a way to be friendly with the host
|
|
|
|
* scheduler.
|
|
|
|
*/
|
|
|
|
return (VMEXIT_CONTINUE);
|
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
2013-10-04 23:29:07 +00:00
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
stats.vmexit_pause++;
|
|
|
|
|
2013-10-04 23:29:07 +00:00
|
|
|
return (VMEXIT_CONTINUE);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
2013-10-04 23:29:07 +00:00
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
stats.vmexit_mtrap++;
|
|
|
|
|
|
|
|
return (VMEXIT_RESTART);
|
|
|
|
}
|
|
|
|
|
2012-04-28 16:28:00 +00:00
|
|
|
static int
|
2013-10-05 21:22:35 +00:00
|
|
|
vmexit_inst_emul(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
2012-04-28 16:28:00 +00:00
|
|
|
{
|
2012-10-19 18:11:17 +00:00
|
|
|
int err;
|
2013-10-05 21:22:35 +00:00
|
|
|
stats.vmexit_inst_emul++;
|
2012-04-28 16:28:00 +00:00
|
|
|
|
2013-10-05 21:22:35 +00:00
|
|
|
err = emulate_mem(ctx, *pvcpu, vmexit->u.inst_emul.gpa,
|
|
|
|
&vmexit->u.inst_emul.vie);
|
2012-10-19 18:11:17 +00:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
if (err == EINVAL) {
|
2012-10-31 03:29:52 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Failed to emulate instruction at 0x%lx\n",
|
|
|
|
vmexit->rip);
|
2012-10-19 18:11:17 +00:00
|
|
|
} else if (err == ESRCH) {
|
2012-10-31 03:29:52 +00:00
|
|
|
fprintf(stderr, "Unhandled memory access to 0x%lx\n",
|
2013-10-05 21:22:35 +00:00
|
|
|
vmexit->u.inst_emul.gpa);
|
2012-10-19 18:11:17 +00:00
|
|
|
}
|
|
|
|
|
2012-04-28 16:28:00 +00:00
|
|
|
return (VMEXIT_ABORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (VMEXIT_CONTINUE);
|
|
|
|
}
|
|
|
|
|
2014-03-26 23:34:27 +00:00
|
|
|
static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER;
|
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_suspend(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
2014-04-28 22:06:40 +00:00
|
|
|
enum vm_suspend_how how;
|
|
|
|
|
|
|
|
how = vmexit->u.suspended.how;
|
2014-03-26 23:34:27 +00:00
|
|
|
|
|
|
|
fbsdrun_deletecpu(ctx, *pvcpu);
|
|
|
|
|
2014-04-28 22:06:40 +00:00
|
|
|
if (*pvcpu != BSP) {
|
2014-03-26 23:34:27 +00:00
|
|
|
pthread_mutex_lock(&resetcpu_mtx);
|
|
|
|
pthread_cond_signal(&resetcpu_cond);
|
|
|
|
pthread_mutex_unlock(&resetcpu_mtx);
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&resetcpu_mtx);
|
|
|
|
while (!CPU_EMPTY(&cpumask)) {
|
|
|
|
pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&resetcpu_mtx);
|
2014-04-28 22:06:40 +00:00
|
|
|
|
2014-05-02 00:33:56 +00:00
|
|
|
switch (how) {
|
|
|
|
case VM_SUSPEND_RESET:
|
2014-04-28 22:06:40 +00:00
|
|
|
exit(0);
|
2014-05-02 00:33:56 +00:00
|
|
|
case VM_SUSPEND_POWEROFF:
|
2014-04-28 22:06:40 +00:00
|
|
|
exit(1);
|
2014-05-02 00:33:56 +00:00
|
|
|
case VM_SUSPEND_HALT:
|
|
|
|
exit(2);
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "vmexit_suspend: invalid reason %d\n", how);
|
|
|
|
exit(100);
|
|
|
|
}
|
2014-04-28 22:06:40 +00:00
|
|
|
return (0); /* NOTREACHED */
|
2014-03-26 23:34:27 +00:00
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static vmexit_handler_t handler[VM_EXITCODE_MAX] = {
|
2012-04-28 16:28:00 +00:00
|
|
|
[VM_EXITCODE_INOUT] = vmexit_inout,
|
|
|
|
[VM_EXITCODE_VMX] = vmexit_vmx,
|
|
|
|
[VM_EXITCODE_BOGUS] = vmexit_bogus,
|
|
|
|
[VM_EXITCODE_RDMSR] = vmexit_rdmsr,
|
|
|
|
[VM_EXITCODE_WRMSR] = vmexit_wrmsr,
|
|
|
|
[VM_EXITCODE_MTRAP] = vmexit_mtrap,
|
2013-10-05 21:22:35 +00:00
|
|
|
[VM_EXITCODE_INST_EMUL] = vmexit_inst_emul,
|
2012-09-25 02:33:25 +00:00
|
|
|
[VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap,
|
2014-03-26 23:34:27 +00:00
|
|
|
[VM_EXITCODE_SUSPENDED] = vmexit_suspend
|
2011-05-13 04:54:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip)
|
|
|
|
{
|
2013-02-11 20:36:07 +00:00
|
|
|
cpuset_t mask;
|
2011-05-13 04:54:01 +00:00
|
|
|
int error, rc, prevcpu;
|
2013-07-18 18:40:54 +00:00
|
|
|
enum vm_exitcode exitcode;
|
2014-04-28 22:06:40 +00:00
|
|
|
enum vm_suspend_how how;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
if (pincpu >= 0) {
|
2013-02-11 20:36:07 +00:00
|
|
|
CPU_ZERO(&mask);
|
|
|
|
CPU_SET(pincpu + vcpu, &mask);
|
|
|
|
error = pthread_setaffinity_np(pthread_self(),
|
|
|
|
sizeof(mask), &mask);
|
2011-05-13 04:54:01 +00:00
|
|
|
assert(error == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
error = vm_run(ctx, vcpu, rip, &vmexit[vcpu]);
|
2013-12-22 20:29:59 +00:00
|
|
|
if (error != 0)
|
|
|
|
break;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
prevcpu = vcpu;
|
2013-07-18 18:40:54 +00:00
|
|
|
|
|
|
|
exitcode = vmexit[vcpu].exitcode;
|
|
|
|
if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) {
|
|
|
|
fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
|
|
|
|
exitcode);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu);
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
switch (rc) {
|
|
|
|
case VMEXIT_CONTINUE:
|
|
|
|
rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
|
|
|
|
break;
|
|
|
|
case VMEXIT_RESTART:
|
|
|
|
rip = vmexit[vcpu].rip;
|
|
|
|
break;
|
|
|
|
case VMEXIT_RESET:
|
2014-04-28 22:06:40 +00:00
|
|
|
case VMEXIT_POWEROFF:
|
|
|
|
if (rc == VMEXIT_RESET)
|
|
|
|
how = VM_SUSPEND_RESET;
|
|
|
|
else
|
|
|
|
how = VM_SUSPEND_POWEROFF;
|
|
|
|
error = vm_suspend(ctx, how);
|
|
|
|
assert(error == 0 || errno == EALREADY);
|
2014-03-26 23:34:27 +00:00
|
|
|
rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
|
|
|
|
break;
|
2011-05-13 04:54:01 +00:00
|
|
|
default:
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
|
|
|
|
}
|
|
|
|
|
2013-01-04 02:04:41 +00:00
|
|
|
static int
|
|
|
|
num_vcpus_allowed(struct vmctx *ctx)
|
|
|
|
{
|
|
|
|
int tmp, error;
|
|
|
|
|
|
|
|
error = vm_get_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, &tmp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The guest is allowed to spinup more than one processor only if the
|
|
|
|
* UNRESTRICTED_GUEST capability is available.
|
|
|
|
*/
|
|
|
|
if (error == 0)
|
|
|
|
return (VM_MAXCPU);
|
|
|
|
else
|
|
|
|
return (1);
|
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2013-10-16 18:20:27 +00:00
|
|
|
void
|
|
|
|
fbsdrun_set_capabilities(struct vmctx *ctx, int cpu)
|
|
|
|
{
|
|
|
|
int err, tmp;
|
|
|
|
|
|
|
|
if (fbsdrun_vmexit_on_hlt()) {
|
|
|
|
err = vm_get_capability(ctx, cpu, VM_CAP_HALT_EXIT, &tmp);
|
|
|
|
if (err < 0) {
|
|
|
|
fprintf(stderr, "VM exit on HLT not supported\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
vm_set_capability(ctx, cpu, VM_CAP_HALT_EXIT, 1);
|
|
|
|
if (cpu == BSP)
|
|
|
|
handler[VM_EXITCODE_HLT] = vmexit_hlt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fbsdrun_vmexit_on_pause()) {
|
|
|
|
/*
|
|
|
|
* pause exit support required for this mode
|
|
|
|
*/
|
|
|
|
err = vm_get_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, &tmp);
|
|
|
|
if (err < 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"SMP mux requested, no pause support\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
vm_set_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, 1);
|
|
|
|
if (cpu == BSP)
|
|
|
|
handler[VM_EXITCODE_PAUSE] = vmexit_pause;
|
|
|
|
}
|
|
|
|
|
2014-02-20 01:48:25 +00:00
|
|
|
if (x2apic_mode)
|
2013-10-16 18:20:27 +00:00
|
|
|
err = vm_set_x2apic_state(ctx, cpu, X2APIC_ENABLED);
|
2014-02-20 01:48:25 +00:00
|
|
|
else
|
|
|
|
err = vm_set_x2apic_state(ctx, cpu, X2APIC_DISABLED);
|
2013-10-16 18:20:27 +00:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
fprintf(stderr, "Unable to set x2apic state (%d)\n", err);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
vm_set_capability(ctx, cpu, VM_CAP_ENABLE_INVPCID, 1);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2013-10-31 05:44:45 +00:00
|
|
|
int c, error, gdb_port, err, bvmcons;
|
2013-01-04 02:04:41 +00:00
|
|
|
int max_vcpus;
|
2011-05-13 04:54:01 +00:00
|
|
|
struct vmctx *ctx;
|
|
|
|
uint64_t rip;
|
2013-03-18 22:38:30 +00:00
|
|
|
size_t memsize;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2012-10-27 22:33:23 +00:00
|
|
|
bvmcons = 0;
|
2011-05-13 04:54:01 +00:00
|
|
|
progname = basename(argv[0]);
|
2013-10-08 16:36:17 +00:00
|
|
|
gdb_port = 0;
|
2011-05-13 04:54:01 +00:00
|
|
|
guest_ncpus = 1;
|
2013-03-18 22:38:30 +00:00
|
|
|
memsize = 256 * MB;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2014-03-04 17:12:06 +00:00
|
|
|
while ((c = getopt(argc, argv, "abehwxAHIPWp:g:c:s:m:l:U:")) != -1) {
|
2011-05-13 04:54:01 +00:00
|
|
|
switch (c) {
|
2012-09-26 00:06:17 +00:00
|
|
|
case 'a':
|
2014-02-20 01:48:25 +00:00
|
|
|
x2apic_mode = 0;
|
2012-09-26 00:06:17 +00:00
|
|
|
break;
|
2012-11-20 07:01:26 +00:00
|
|
|
case 'A':
|
|
|
|
acpi = 1;
|
|
|
|
break;
|
2012-10-27 22:33:23 +00:00
|
|
|
case 'b':
|
|
|
|
bvmcons = 1;
|
|
|
|
break;
|
2011-05-13 04:54:01 +00:00
|
|
|
case 'p':
|
|
|
|
pincpu = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
guest_ncpus = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
gdb_port = atoi(optarg);
|
|
|
|
break;
|
2013-10-29 00:18:11 +00:00
|
|
|
case 'l':
|
|
|
|
if (lpc_device_parse(optarg) != 0) {
|
|
|
|
errx(EX_USAGE, "invalid lpc device "
|
|
|
|
"configuration '%s'", optarg);
|
|
|
|
}
|
|
|
|
break;
|
2011-05-13 04:54:01 +00:00
|
|
|
case 's':
|
2014-01-27 22:26:15 +00:00
|
|
|
if (pci_parse_slot(optarg) != 0)
|
2013-04-26 02:24:50 +00:00
|
|
|
exit(1);
|
|
|
|
else
|
|
|
|
break;
|
2011-05-13 04:54:01 +00:00
|
|
|
case 'm':
|
2013-10-09 03:56:07 +00:00
|
|
|
error = vm_parse_memsize(optarg, &memsize);
|
|
|
|
if (error)
|
|
|
|
errx(EX_USAGE, "invalid memsize '%s'", optarg);
|
2011-05-13 04:54:01 +00:00
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
guest_vmexit_on_hlt = 1;
|
|
|
|
break;
|
2012-08-04 22:48:04 +00:00
|
|
|
case 'I':
|
2013-10-31 05:44:45 +00:00
|
|
|
/*
|
|
|
|
* The "-I" option was used to add an ioapic to the
|
|
|
|
* virtual machine.
|
|
|
|
*
|
|
|
|
* An ioapic is now provided unconditionally for each
|
|
|
|
* virtual machine and this option is now deprecated.
|
|
|
|
*/
|
2012-08-04 22:48:04 +00:00
|
|
|
break;
|
2011-05-13 04:54:01 +00:00
|
|
|
case 'P':
|
|
|
|
guest_vmexit_on_pause = 1;
|
|
|
|
break;
|
2011-05-19 21:53:25 +00:00
|
|
|
case 'e':
|
|
|
|
strictio = 1;
|
|
|
|
break;
|
2014-03-04 17:12:06 +00:00
|
|
|
case 'U':
|
|
|
|
guest_uuid_str = optarg;
|
|
|
|
break;
|
2013-12-19 22:27:28 +00:00
|
|
|
case 'w':
|
|
|
|
strictmsr = 0;
|
|
|
|
break;
|
2013-10-17 22:01:17 +00:00
|
|
|
case 'W':
|
|
|
|
virtio_msix = 0;
|
|
|
|
break;
|
2014-02-20 01:48:25 +00:00
|
|
|
case 'x':
|
|
|
|
x2apic_mode = 1;
|
|
|
|
break;
|
2011-05-13 04:54:01 +00:00
|
|
|
case 'h':
|
|
|
|
usage(0);
|
|
|
|
default:
|
|
|
|
usage(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc != 1)
|
|
|
|
usage(1);
|
|
|
|
|
|
|
|
vmname = argv[0];
|
|
|
|
|
|
|
|
ctx = vm_open(vmname);
|
|
|
|
if (ctx == NULL) {
|
|
|
|
perror("vm_open");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-01-04 02:04:41 +00:00
|
|
|
max_vcpus = num_vcpus_allowed(ctx);
|
|
|
|
if (guest_ncpus > max_vcpus) {
|
|
|
|
fprintf(stderr, "%d vCPUs requested but only %d available\n",
|
|
|
|
guest_ncpus, max_vcpus);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-10-16 18:20:27 +00:00
|
|
|
fbsdrun_set_capabilities(ctx, BSP);
|
2012-09-26 00:06:17 +00:00
|
|
|
|
2013-03-18 22:38:30 +00:00
|
|
|
err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
|
|
|
|
if (err) {
|
|
|
|
fprintf(stderr, "Unable to setup memory (%d)\n", err);
|
|
|
|
exit(1);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 18:59:20 +00:00
|
|
|
init_mem();
|
2011-05-13 04:54:01 +00:00
|
|
|
init_inout();
|
2014-01-29 14:56:48 +00:00
|
|
|
ioapic_init(ctx);
|
2013-07-04 05:35:56 +00:00
|
|
|
|
2013-07-11 03:54:35 +00:00
|
|
|
rtc_init(ctx);
|
|
|
|
|
2013-07-04 05:35:56 +00:00
|
|
|
/*
|
|
|
|
* Exit if a device emulation finds an error in it's initilization
|
|
|
|
*/
|
|
|
|
if (init_pci(ctx) != 0)
|
|
|
|
exit(1);
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
if (gdb_port != 0)
|
|
|
|
init_dbgport(gdb_port);
|
|
|
|
|
2012-10-27 22:33:23 +00:00
|
|
|
if (bvmcons)
|
|
|
|
init_bvmcons();
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip);
|
|
|
|
assert(error == 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* build the guest tables, MP etc.
|
|
|
|
*/
|
2013-10-31 05:44:45 +00:00
|
|
|
mptable_build(ctx, guest_ncpus);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2014-03-04 17:12:06 +00:00
|
|
|
error = smbios_build(ctx);
|
|
|
|
assert(error == 0);
|
|
|
|
|
2012-11-20 07:01:26 +00:00
|
|
|
if (acpi) {
|
2013-10-31 05:44:45 +00:00
|
|
|
error = acpi_build(ctx, guest_ncpus);
|
2012-11-20 07:01:26 +00:00
|
|
|
assert(error == 0);
|
|
|
|
}
|
|
|
|
|
2013-11-06 00:25:17 +00:00
|
|
|
/*
|
|
|
|
* Change the proc title to include the VM name.
|
|
|
|
*/
|
|
|
|
setproctitle("%s", vmname);
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
/*
|
|
|
|
* Add CPU 0
|
|
|
|
*/
|
2014-03-20 18:15:37 +00:00
|
|
|
fbsdrun_addcpu(ctx, BSP, BSP, rip);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Head off to the main event dispatch loop
|
|
|
|
*/
|
|
|
|
mevent_dispatch();
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|