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>
|
|
|
|
|
|
|
|
#include <machine/segments.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <pthread.h>
|
2012-10-31 19:17:55 +00:00
|
|
|
#include <pthread_np.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"
|
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"
|
|
|
|
#include "xmsr.h"
|
2012-08-05 00:00:52 +00:00
|
|
|
#include "ioapic.h"
|
2012-09-25 02:33:25 +00:00
|
|
|
#include "spinup_ap.h"
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
#define DEFAULT_GUEST_HZ 100
|
|
|
|
#define DEFAULT_GUEST_TSLICE 200
|
|
|
|
|
|
|
|
#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 */
|
|
|
|
|
|
|
|
#define MB (1024UL * 1024)
|
|
|
|
#define GB (1024UL * MB)
|
|
|
|
|
|
|
|
typedef int (*vmexit_handler_t)(struct vmctx *, struct vm_exit *, int *vcpu);
|
|
|
|
|
|
|
|
int guest_tslice = DEFAULT_GUEST_TSLICE;
|
|
|
|
int guest_hz = DEFAULT_GUEST_HZ;
|
|
|
|
char *vmname;
|
|
|
|
|
|
|
|
int guest_ncpus;
|
|
|
|
|
|
|
|
static int pincpu = -1;
|
|
|
|
static int guest_vcpu_mux;
|
2012-09-26 00:06:17 +00:00
|
|
|
static int guest_vmexit_on_hlt, guest_vmexit_on_pause, disable_x2apic;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
static int foundcpus;
|
|
|
|
|
2011-05-19 21:53:25 +00:00
|
|
|
static int strictio;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
static int cpumask;
|
|
|
|
|
|
|
|
static void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip);
|
|
|
|
|
|
|
|
struct vm_exit vmexit[VM_MAXCPU];
|
|
|
|
|
|
|
|
struct fbsdstats {
|
|
|
|
uint64_t vmexit_bogus;
|
|
|
|
uint64_t vmexit_bogus_switch;
|
|
|
|
uint64_t vmexit_hlt;
|
|
|
|
uint64_t vmexit_pause;
|
|
|
|
uint64_t vmexit_mtrap;
|
2012-04-28 16:28:00 +00:00
|
|
|
uint64_t vmexit_paging;
|
2011-05-13 04:54:01 +00:00
|
|
|
uint64_t cpu_switch_rotate;
|
|
|
|
uint64_t cpu_switch_direct;
|
|
|
|
int io_reset;
|
|
|
|
} 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,
|
2012-11-20 07:01:26 +00:00
|
|
|
"Usage: %s [-aehABHIP][-g <gdb port>][-z <hz>][-s <pci>]"
|
2013-01-20 03:47:13 +00:00
|
|
|
"[-S <pci>][-p pincpu][-n <pci>][-m lowmem][-M highmem]"
|
|
|
|
" <vmname>\n"
|
2012-09-26 00:06:17 +00:00
|
|
|
" -a: local apic is in XAPIC mode (default is X2APIC)\n"
|
2012-11-20 07:01:26 +00:00
|
|
|
" -A: create an ACPI table\n"
|
2011-05-13 04:54:01 +00:00
|
|
|
" -g: gdb port (default is %d and 0 means don't open)\n"
|
|
|
|
" -c: # cpus (default 1)\n"
|
|
|
|
" -p: pin vcpu 'n' to host cpu 'pincpu + n'\n"
|
|
|
|
" -B: inject breakpoint exception on vm entry\n"
|
|
|
|
" -H: vmexit from the guest on hlt\n"
|
2012-08-04 22:48:04 +00:00
|
|
|
" -I: present an ioapic to the guest\n"
|
2011-05-13 04:54:01 +00:00
|
|
|
" -P: vmexit from the guest on pause\n"
|
2011-05-19 21:53:25 +00:00
|
|
|
" -e: exit on unhandled i/o access\n"
|
2011-05-13 04:54:01 +00:00
|
|
|
" -h: help\n"
|
|
|
|
" -z: guest hz (default is %d)\n"
|
|
|
|
" -s: <slot,driver,configinfo> PCI slot config\n"
|
2012-05-03 03:11:27 +00:00
|
|
|
" -S: <slot,driver,configinfo> legacy PCI slot config\n"
|
2013-03-18 22:38:30 +00:00
|
|
|
" -m: memory size in MB\n"
|
2011-05-13 04:54:01 +00:00
|
|
|
" -x: mux vcpus to 1 hcpu\n"
|
|
|
|
" -t: mux vcpu timeslice hz (default %d)\n",
|
|
|
|
progname, DEFAULT_GDB_PORT, DEFAULT_GUEST_HZ,
|
|
|
|
DEFAULT_GUEST_TSLICE);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-09-26 00:06:17 +00:00
|
|
|
int
|
|
|
|
fbsdrun_disable_x2apic(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (disable_x2apic);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fbsdrun_muxed(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (guest_vcpu_mux);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
snprintf(tname, sizeof(tname), "%s vcpu %d", vmname, vcpu);
|
|
|
|
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
|
|
|
|
fbsdrun_addcpu(struct vmctx *ctx, int vcpu, uint64_t rip)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (cpumask & (1 << vcpu)) {
|
2012-10-31 03:29:52 +00:00
|
|
|
fprintf(stderr, "addcpu: attempting to add existing cpu %d\n",
|
|
|
|
vcpu);
|
2011-05-13 04:54:01 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
cpumask |= 1 << vcpu;
|
|
|
|
foundcpus++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the vmexit struct to allow execution to start
|
|
|
|
* at the given RIP
|
|
|
|
*/
|
|
|
|
vmexit[vcpu].rip = rip;
|
|
|
|
vmexit[vcpu].inst_length = 0;
|
|
|
|
|
|
|
|
if (vcpu == BSP || !guest_vcpu_mux){
|
|
|
|
mt_vmm_info[vcpu].mt_ctx = ctx;
|
|
|
|
mt_vmm_info[vcpu].mt_vcpu = vcpu;
|
|
|
|
|
|
|
|
error = pthread_create(&mt_vmm_info[vcpu].mt_thr, NULL,
|
|
|
|
fbsdrun_start_thread, &mt_vmm_info[vcpu]);
|
|
|
|
assert(error == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fbsdrun_get_next_cpu(int curcpu)
|
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the next available CPU. Assumes they arrive
|
|
|
|
* in ascending order with no gaps.
|
|
|
|
*/
|
|
|
|
return ((curcpu + 1) % foundcpus);
|
|
|
|
}
|
|
|
|
|
2011-05-15 04:03:11 +00:00
|
|
|
static int
|
2011-05-13 04:54:01 +00:00
|
|
|
vmexit_catch_reset(void)
|
|
|
|
{
|
|
|
|
stats.io_reset++;
|
|
|
|
return (VMEXIT_RESET);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
#if PG_DEBUG /* put all types of debug here */
|
|
|
|
if (eax == 0) {
|
|
|
|
pause_noswitch = 1;
|
|
|
|
} else if (eax == 1) {
|
|
|
|
pause_noswitch = 0;
|
|
|
|
} else {
|
|
|
|
pause_noswitch = 0;
|
|
|
|
if (eax == 5) {
|
|
|
|
vm_set_capability(ctx, *pvcpu, VM_CAP_MTRAP_EXIT, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#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);
|
|
|
|
|
|
|
|
/* Special case of guest reset */
|
|
|
|
if (out && port == 0x64 && (uint8_t)eax == 0xFE)
|
|
|
|
return (vmexit_catch_reset());
|
|
|
|
|
|
|
|
/* 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);
|
2011-05-13 04:54:01 +00:00
|
|
|
if (error == 0 && in)
|
|
|
|
error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX, eax);
|
|
|
|
|
|
|
|
if (error == 0)
|
|
|
|
return (VMEXIT_CONTINUE);
|
|
|
|
else {
|
|
|
|
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)
|
|
|
|
{
|
2012-10-31 03:29:52 +00:00
|
|
|
fprintf(stderr, "vm exit rdmsr 0x%x, cpu %d\n", vme->u.msr.code,
|
|
|
|
*pvcpu);
|
2011-05-13 04:54:01 +00:00
|
|
|
return (VMEXIT_ABORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
|
|
|
|
{
|
|
|
|
int newcpu;
|
|
|
|
int retval = VMEXIT_CONTINUE;
|
|
|
|
|
|
|
|
newcpu = emulate_wrmsr(ctx, *pvcpu, vme->u.msr.code,vme->u.msr.wval);
|
|
|
|
|
|
|
|
if (guest_vcpu_mux && *pvcpu != newcpu) {
|
|
|
|
retval = VMEXIT_SWITCH;
|
|
|
|
*pvcpu = newcpu;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (retval);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (guest_vcpu_mux && *pvcpu != newcpu) {
|
|
|
|
retval = VMEXIT_SWITCH;
|
|
|
|
*pvcpu = newcpu;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
fprintf(stderr, "\terror\t\t%d\n", vmexit->u.vmx.error);
|
|
|
|
fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason);
|
|
|
|
fprintf(stderr, "\tqualification\t0x%016lx\n",
|
|
|
|
vmexit->u.vmx.exit_qualification);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
return (VMEXIT_ABORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bogus_noswitch = 1;
|
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
|
|
|
stats.vmexit_bogus++;
|
|
|
|
|
|
|
|
if (!guest_vcpu_mux || guest_ncpus == 1 || bogus_noswitch) {
|
|
|
|
return (VMEXIT_RESTART);
|
|
|
|
} else {
|
|
|
|
stats.vmexit_bogus_switch++;
|
|
|
|
vmexit->inst_length = 0;
|
|
|
|
*pvcpu = -1;
|
|
|
|
return (VMEXIT_SWITCH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
|
|
|
stats.vmexit_hlt++;
|
|
|
|
if (fbsdrun_muxed()) {
|
|
|
|
*pvcpu = -1;
|
|
|
|
return (VMEXIT_SWITCH);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pause_noswitch;
|
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
|
|
|
stats.vmexit_pause++;
|
|
|
|
|
|
|
|
if (fbsdrun_muxed() && !pause_noswitch) {
|
|
|
|
*pvcpu = -1;
|
|
|
|
return (VMEXIT_SWITCH);
|
|
|
|
} else {
|
|
|
|
return (VMEXIT_CONTINUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
|
|
|
stats.vmexit_mtrap++;
|
|
|
|
|
|
|
|
return (VMEXIT_RESTART);
|
|
|
|
}
|
|
|
|
|
2012-04-28 16:28:00 +00:00
|
|
|
static int
|
|
|
|
vmexit_paging(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
2012-10-19 18:11:17 +00:00
|
|
|
int err;
|
2012-04-28 16:28:00 +00:00
|
|
|
stats.vmexit_paging++;
|
|
|
|
|
2012-11-28 13:34:44 +00:00
|
|
|
err = emulate_mem(ctx, *pvcpu, vmexit->u.paging.gpa,
|
2012-11-28 00:02:17 +00:00
|
|
|
&vmexit->u.paging.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",
|
|
|
|
vmexit->u.paging.gpa);
|
2012-10-19 18:11:17 +00:00
|
|
|
}
|
|
|
|
|
2012-04-28 16:28:00 +00:00
|
|
|
return (VMEXIT_ABORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (VMEXIT_CONTINUE);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static void
|
|
|
|
sigalrm(int sig)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setup_timeslice(void)
|
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
struct itimerval itv;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup a realtime timer to generate a SIGALRM at a
|
|
|
|
* frequency of 'guest_tslice' ticks per second.
|
|
|
|
*/
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sa.sa_handler = sigalrm;
|
|
|
|
|
|
|
|
error = sigaction(SIGALRM, &sa, NULL);
|
|
|
|
assert(error == 0);
|
|
|
|
|
|
|
|
itv.it_interval.tv_sec = 0;
|
|
|
|
itv.it_interval.tv_usec = 1000000 / guest_tslice;
|
|
|
|
itv.it_value.tv_sec = 0;
|
|
|
|
itv.it_value.tv_usec = 1000000 / guest_tslice;
|
|
|
|
|
|
|
|
error = setitimer(ITIMER_REAL, &itv, NULL);
|
|
|
|
assert(error == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2012-09-25 02:33:25 +00:00
|
|
|
[VM_EXITCODE_PAGING] = vmexit_paging,
|
|
|
|
[VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap,
|
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;
|
|
|
|
|
|
|
|
if (guest_vcpu_mux)
|
|
|
|
setup_timeslice();
|
|
|
|
|
|
|
|
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]);
|
2012-10-12 18:49:07 +00:00
|
|
|
if (error != 0) {
|
|
|
|
/*
|
|
|
|
* It is possible that 'vmmctl' or some other process
|
|
|
|
* has transitioned the vcpu to CANNOT_RUN state right
|
|
|
|
* before we tried to transition it to RUNNING.
|
|
|
|
*
|
|
|
|
* This is expected to be temporary so just retry.
|
|
|
|
*/
|
|
|
|
if (errno == EBUSY)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
prevcpu = vcpu;
|
|
|
|
rc = (*handler[vmexit[vcpu].exitcode])(ctx, &vmexit[vcpu],
|
|
|
|
&vcpu);
|
|
|
|
switch (rc) {
|
|
|
|
case VMEXIT_SWITCH:
|
|
|
|
assert(guest_vcpu_mux);
|
|
|
|
if (vcpu == -1) {
|
|
|
|
stats.cpu_switch_rotate++;
|
|
|
|
vcpu = fbsdrun_get_next_cpu(prevcpu);
|
|
|
|
} else {
|
|
|
|
stats.cpu_switch_direct++;
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
case VMEXIT_CONTINUE:
|
|
|
|
rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
|
|
|
|
break;
|
|
|
|
case VMEXIT_RESTART:
|
|
|
|
rip = vmexit[vcpu].rip;
|
|
|
|
break;
|
|
|
|
case VMEXIT_RESET:
|
|
|
|
exit(0);
|
|
|
|
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
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2012-10-27 22:33:23 +00:00
|
|
|
int c, error, gdb_port, inject_bkpt, tmp, err, ioapic, 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
|
|
|
inject_bkpt = 0;
|
|
|
|
progname = basename(argv[0]);
|
|
|
|
gdb_port = DEFAULT_GDB_PORT;
|
|
|
|
guest_ncpus = 1;
|
2012-08-04 22:48:04 +00:00
|
|
|
ioapic = 0;
|
2013-03-18 22:38:30 +00:00
|
|
|
memsize = 256 * MB;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2013-03-18 22:38:30 +00:00
|
|
|
while ((c = getopt(argc, argv, "abehABHIPxp:g:c:z:s:S:n:m:")) != -1) {
|
2011-05-13 04:54:01 +00:00
|
|
|
switch (c) {
|
2012-09-26 00:06:17 +00:00
|
|
|
case 'a':
|
|
|
|
disable_x2apic = 1;
|
|
|
|
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 'B':
|
|
|
|
inject_bkpt = 1;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
guest_vcpu_mux = 1;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
pincpu = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
guest_ncpus = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
gdb_port = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
guest_hz = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
guest_tslice = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 's':
|
2012-05-03 03:11:27 +00:00
|
|
|
pci_parse_slot(optarg, 0);
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
pci_parse_slot(optarg, 1);
|
2011-05-13 04:54:01 +00:00
|
|
|
break;
|
|
|
|
case 'm':
|
2013-03-18 22:38:30 +00:00
|
|
|
memsize = strtoul(optarg, NULL, 0) * MB;
|
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':
|
|
|
|
ioapic = 1;
|
|
|
|
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;
|
2011-05-13 04:54:01 +00:00
|
|
|
case 'h':
|
|
|
|
usage(0);
|
|
|
|
default:
|
|
|
|
usage(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc != 1)
|
|
|
|
usage(1);
|
|
|
|
|
|
|
|
/* No need to mux if guest is uni-processor */
|
|
|
|
if (guest_ncpus <= 1)
|
|
|
|
guest_vcpu_mux = 0;
|
|
|
|
|
|
|
|
/* vmexit on hlt if guest is muxed */
|
|
|
|
if (guest_vcpu_mux) {
|
|
|
|
guest_vmexit_on_hlt = 1;
|
|
|
|
guest_vmexit_on_pause = 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);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
if (fbsdrun_vmexit_on_hlt()) {
|
|
|
|
err = vm_get_capability(ctx, BSP, VM_CAP_HALT_EXIT, &tmp);
|
|
|
|
if (err < 0) {
|
2012-10-31 03:29:52 +00:00
|
|
|
fprintf(stderr, "VM exit on HLT not supported\n");
|
2011-05-13 04:54:01 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
vm_set_capability(ctx, BSP, VM_CAP_HALT_EXIT, 1);
|
|
|
|
handler[VM_EXITCODE_HLT] = vmexit_hlt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fbsdrun_vmexit_on_pause()) {
|
|
|
|
/*
|
|
|
|
* pause exit support required for this mode
|
|
|
|
*/
|
|
|
|
err = vm_get_capability(ctx, BSP, VM_CAP_PAUSE_EXIT, &tmp);
|
|
|
|
if (err < 0) {
|
2012-10-31 03:29:52 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"SMP mux requested, no pause support\n");
|
2011-05-13 04:54:01 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
vm_set_capability(ctx, BSP, VM_CAP_PAUSE_EXIT, 1);
|
|
|
|
handler[VM_EXITCODE_PAUSE] = vmexit_pause;
|
|
|
|
}
|
|
|
|
|
2012-09-26 00:06:17 +00:00
|
|
|
if (fbsdrun_disable_x2apic())
|
|
|
|
err = vm_set_x2apic_state(ctx, BSP, X2APIC_DISABLED);
|
|
|
|
else
|
|
|
|
err = vm_set_x2apic_state(ctx, BSP, X2APIC_ENABLED);
|
|
|
|
|
|
|
|
if (err) {
|
2012-10-31 03:29:52 +00:00
|
|
|
fprintf(stderr, "Unable to set x2apic state (%d)\n", err);
|
2012-09-26 00:06:17 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
init_pci(ctx);
|
2012-08-05 00:00:52 +00:00
|
|
|
if (ioapic)
|
|
|
|
ioapic_init(0);
|
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);
|
|
|
|
|
|
|
|
if (inject_bkpt) {
|
|
|
|
error = vm_inject_event(ctx, BSP, VM_HW_EXCEPTION, IDT_BP);
|
|
|
|
assert(error == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* build the guest tables, MP etc.
|
|
|
|
*/
|
2012-10-26 13:40:12 +00:00
|
|
|
mptable_build(ctx, guest_ncpus, ioapic);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2012-11-20 07:01:26 +00:00
|
|
|
if (acpi) {
|
|
|
|
error = acpi_build(ctx, guest_ncpus, ioapic);
|
|
|
|
assert(error == 0);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
/*
|
|
|
|
* Add CPU 0
|
|
|
|
*/
|
|
|
|
fbsdrun_addcpu(ctx, BSP, rip);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Head off to the main event dispatch loop
|
|
|
|
*/
|
|
|
|
mevent_dispatch();
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|