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.
|
|
|
|
*
|
2013-01-20 03:42:49 +00:00
|
|
|
* $FreeBSD$
|
2011-05-13 04:54:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _VMM_H_
|
|
|
|
#define _VMM_H_
|
|
|
|
|
|
|
|
#ifdef _KERNEL
|
|
|
|
|
|
|
|
#define VM_MAX_NAMELEN 32
|
|
|
|
|
|
|
|
struct vm;
|
|
|
|
struct vm_memory_segment;
|
|
|
|
struct seg_desc;
|
|
|
|
struct vm_exit;
|
|
|
|
struct vm_run;
|
|
|
|
struct vlapic;
|
|
|
|
|
2012-09-25 19:08:51 +00:00
|
|
|
enum x2apic_state;
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
typedef int (*vmm_init_func_t)(void);
|
|
|
|
typedef int (*vmm_cleanup_func_t)(void);
|
|
|
|
typedef void * (*vmi_init_func_t)(struct vm *vm); /* instance specific apis */
|
2012-09-24 19:32:24 +00:00
|
|
|
typedef int (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip);
|
2011-05-13 04:54:01 +00:00
|
|
|
typedef void (*vmi_cleanup_func_t)(void *vmi);
|
2012-10-03 00:46:30 +00:00
|
|
|
typedef int (*vmi_mmap_set_func_t)(void *vmi, vm_paddr_t gpa,
|
|
|
|
vm_paddr_t hpa, size_t length,
|
|
|
|
vm_memattr_t attr, int prot,
|
|
|
|
boolean_t superpages_ok);
|
|
|
|
typedef vm_paddr_t (*vmi_mmap_get_func_t)(void *vmi, vm_paddr_t gpa);
|
2011-05-13 04:54:01 +00:00
|
|
|
typedef int (*vmi_get_register_t)(void *vmi, int vcpu, int num,
|
|
|
|
uint64_t *retval);
|
|
|
|
typedef int (*vmi_set_register_t)(void *vmi, int vcpu, int num,
|
|
|
|
uint64_t val);
|
|
|
|
typedef int (*vmi_get_desc_t)(void *vmi, int vcpu, int num,
|
|
|
|
struct seg_desc *desc);
|
|
|
|
typedef int (*vmi_set_desc_t)(void *vmi, int vcpu, int num,
|
|
|
|
struct seg_desc *desc);
|
|
|
|
typedef int (*vmi_inject_event_t)(void *vmi, int vcpu,
|
|
|
|
int type, int vector,
|
|
|
|
uint32_t code, int code_valid);
|
|
|
|
typedef int (*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval);
|
|
|
|
typedef int (*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val);
|
|
|
|
|
|
|
|
struct vmm_ops {
|
|
|
|
vmm_init_func_t init; /* module wide initialization */
|
|
|
|
vmm_cleanup_func_t cleanup;
|
|
|
|
|
|
|
|
vmi_init_func_t vminit; /* vm-specific initialization */
|
|
|
|
vmi_run_func_t vmrun;
|
|
|
|
vmi_cleanup_func_t vmcleanup;
|
2012-10-03 00:46:30 +00:00
|
|
|
vmi_mmap_set_func_t vmmmap_set;
|
|
|
|
vmi_mmap_get_func_t vmmmap_get;
|
2011-05-13 04:54:01 +00:00
|
|
|
vmi_get_register_t vmgetreg;
|
|
|
|
vmi_set_register_t vmsetreg;
|
|
|
|
vmi_get_desc_t vmgetdesc;
|
|
|
|
vmi_set_desc_t vmsetdesc;
|
|
|
|
vmi_inject_event_t vminject;
|
|
|
|
vmi_get_cap_t vmgetcap;
|
|
|
|
vmi_set_cap_t vmsetcap;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern struct vmm_ops vmm_ops_intel;
|
|
|
|
extern struct vmm_ops vmm_ops_amd;
|
|
|
|
|
2013-04-12 01:16:52 +00:00
|
|
|
int vm_create(const char *name, struct vm **retvm);
|
2011-05-13 04:54:01 +00:00
|
|
|
void vm_destroy(struct vm *vm);
|
|
|
|
const char *vm_name(struct vm *vm);
|
2012-09-29 01:15:45 +00:00
|
|
|
int vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len);
|
2011-05-13 04:54:01 +00:00
|
|
|
int vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
|
|
|
|
int vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
|
|
|
|
vm_paddr_t vm_gpa2hpa(struct vm *vm, vm_paddr_t gpa, size_t size);
|
|
|
|
int vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase,
|
|
|
|
struct vm_memory_segment *seg);
|
|
|
|
int vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval);
|
|
|
|
int vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val);
|
|
|
|
int vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
|
|
|
|
struct seg_desc *ret_desc);
|
|
|
|
int vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
|
|
|
|
struct seg_desc *desc);
|
|
|
|
int vm_run(struct vm *vm, struct vm_run *vmrun);
|
|
|
|
int vm_inject_event(struct vm *vm, int vcpu, int type,
|
|
|
|
int vector, uint32_t error_code, int error_code_valid);
|
|
|
|
int vm_inject_nmi(struct vm *vm, int vcpu);
|
2012-10-24 02:54:21 +00:00
|
|
|
int vm_nmi_pending(struct vm *vm, int vcpuid);
|
|
|
|
void vm_nmi_clear(struct vm *vm, int vcpuid);
|
2011-05-13 04:54:01 +00:00
|
|
|
uint64_t *vm_guest_msrs(struct vm *vm, int cpu);
|
|
|
|
struct vlapic *vm_lapic(struct vm *vm, int cpu);
|
|
|
|
int vm_get_capability(struct vm *vm, int vcpu, int type, int *val);
|
|
|
|
int vm_set_capability(struct vm *vm, int vcpu, int type, int val);
|
2012-09-25 19:08:51 +00:00
|
|
|
int vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state);
|
|
|
|
int vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state);
|
2011-05-13 04:54:01 +00:00
|
|
|
void vm_activate_cpu(struct vm *vm, int vcpu);
|
2011-06-28 06:26:03 +00:00
|
|
|
cpuset_t vm_active_cpus(struct vm *vm);
|
2012-09-24 19:32:24 +00:00
|
|
|
struct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return 1 if device indicated by bus/slot/func is supposed to be a
|
|
|
|
* pci passthrough device.
|
|
|
|
*
|
|
|
|
* Return 0 otherwise.
|
|
|
|
*/
|
|
|
|
int vmm_is_pptdev(int bus, int slot, int func);
|
|
|
|
|
|
|
|
void *vm_iommu_domain(struct vm *vm);
|
|
|
|
|
2012-10-12 18:32:44 +00:00
|
|
|
enum vcpu_state {
|
|
|
|
VCPU_IDLE,
|
|
|
|
VCPU_RUNNING,
|
|
|
|
VCPU_CANNOT_RUN,
|
|
|
|
};
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2012-10-12 18:32:44 +00:00
|
|
|
int vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state);
|
2013-04-25 04:56:43 +00:00
|
|
|
enum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
static int __inline
|
2013-04-25 04:56:43 +00:00
|
|
|
vcpu_is_running(struct vm *vm, int vcpu, int *hostcpu)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
2013-04-25 04:56:43 +00:00
|
|
|
return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
2012-10-12 18:32:44 +00:00
|
|
|
void *vcpu_stats(struct vm *vm, int vcpu);
|
|
|
|
void vm_interrupt_hostcpu(struct vm *vm, int vcpu);
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
#endif /* KERNEL */
|
|
|
|
|
2012-11-28 00:02:17 +00:00
|
|
|
#include <machine/vmm_instruction_emul.h>
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
#define VM_MAXCPU 8 /* maximum virtual cpus */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Identifiers for events that can be injected into the VM
|
|
|
|
*/
|
|
|
|
enum vm_event_type {
|
|
|
|
VM_EVENT_NONE,
|
|
|
|
VM_HW_INTR,
|
|
|
|
VM_NMI,
|
|
|
|
VM_HW_EXCEPTION,
|
|
|
|
VM_SW_INTR,
|
|
|
|
VM_PRIV_SW_EXCEPTION,
|
|
|
|
VM_SW_EXCEPTION,
|
|
|
|
VM_EVENT_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Identifiers for architecturally defined registers.
|
|
|
|
*/
|
|
|
|
enum vm_reg_name {
|
|
|
|
VM_REG_GUEST_RAX,
|
|
|
|
VM_REG_GUEST_RBX,
|
|
|
|
VM_REG_GUEST_RCX,
|
|
|
|
VM_REG_GUEST_RDX,
|
|
|
|
VM_REG_GUEST_RSI,
|
|
|
|
VM_REG_GUEST_RDI,
|
|
|
|
VM_REG_GUEST_RBP,
|
|
|
|
VM_REG_GUEST_R8,
|
|
|
|
VM_REG_GUEST_R9,
|
|
|
|
VM_REG_GUEST_R10,
|
|
|
|
VM_REG_GUEST_R11,
|
|
|
|
VM_REG_GUEST_R12,
|
|
|
|
VM_REG_GUEST_R13,
|
|
|
|
VM_REG_GUEST_R14,
|
|
|
|
VM_REG_GUEST_R15,
|
|
|
|
VM_REG_GUEST_CR0,
|
|
|
|
VM_REG_GUEST_CR3,
|
|
|
|
VM_REG_GUEST_CR4,
|
|
|
|
VM_REG_GUEST_DR7,
|
|
|
|
VM_REG_GUEST_RSP,
|
|
|
|
VM_REG_GUEST_RIP,
|
|
|
|
VM_REG_GUEST_RFLAGS,
|
|
|
|
VM_REG_GUEST_ES,
|
|
|
|
VM_REG_GUEST_CS,
|
|
|
|
VM_REG_GUEST_SS,
|
|
|
|
VM_REG_GUEST_DS,
|
|
|
|
VM_REG_GUEST_FS,
|
|
|
|
VM_REG_GUEST_GS,
|
|
|
|
VM_REG_GUEST_LDTR,
|
|
|
|
VM_REG_GUEST_TR,
|
|
|
|
VM_REG_GUEST_IDTR,
|
|
|
|
VM_REG_GUEST_GDTR,
|
|
|
|
VM_REG_GUEST_EFER,
|
|
|
|
VM_REG_LAST
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Identifiers for optional vmm capabilities
|
|
|
|
*/
|
|
|
|
enum vm_cap_type {
|
|
|
|
VM_CAP_HALT_EXIT,
|
|
|
|
VM_CAP_MTRAP_EXIT,
|
|
|
|
VM_CAP_PAUSE_EXIT,
|
|
|
|
VM_CAP_UNRESTRICTED_GUEST,
|
|
|
|
VM_CAP_MAX
|
|
|
|
};
|
|
|
|
|
2012-09-25 19:08:51 +00:00
|
|
|
enum x2apic_state {
|
|
|
|
X2APIC_ENABLED,
|
|
|
|
X2APIC_AVAILABLE,
|
|
|
|
X2APIC_DISABLED,
|
|
|
|
X2APIC_STATE_LAST
|
|
|
|
};
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
/*
|
|
|
|
* The 'access' field has the format specified in Table 21-2 of the Intel
|
|
|
|
* Architecture Manual vol 3b.
|
|
|
|
*
|
|
|
|
* XXX The contents of the 'access' field are architecturally defined except
|
|
|
|
* bit 16 - Segment Unusable.
|
|
|
|
*/
|
|
|
|
struct seg_desc {
|
|
|
|
uint64_t base;
|
|
|
|
uint32_t limit;
|
|
|
|
uint32_t access;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum vm_exitcode {
|
|
|
|
VM_EXITCODE_INOUT,
|
|
|
|
VM_EXITCODE_VMX,
|
|
|
|
VM_EXITCODE_BOGUS,
|
|
|
|
VM_EXITCODE_RDMSR,
|
|
|
|
VM_EXITCODE_WRMSR,
|
|
|
|
VM_EXITCODE_HLT,
|
|
|
|
VM_EXITCODE_MTRAP,
|
|
|
|
VM_EXITCODE_PAUSE,
|
2012-04-28 16:28:00 +00:00
|
|
|
VM_EXITCODE_PAGING,
|
2012-09-25 02:33:25 +00:00
|
|
|
VM_EXITCODE_SPINUP_AP,
|
2012-04-28 16:28:00 +00:00
|
|
|
VM_EXITCODE_MAX
|
2011-05-13 04:54:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct vm_exit {
|
|
|
|
enum vm_exitcode exitcode;
|
|
|
|
int inst_length; /* 0 means unknown */
|
|
|
|
uint64_t rip;
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
uint16_t bytes:3; /* 1 or 2 or 4 */
|
|
|
|
uint16_t in:1; /* out is 0, in is 1 */
|
|
|
|
uint16_t string:1;
|
|
|
|
uint16_t rep:1;
|
|
|
|
uint16_t port;
|
|
|
|
uint32_t eax; /* valid for out */
|
|
|
|
} inout;
|
2012-04-28 16:28:00 +00:00
|
|
|
struct {
|
2012-10-12 23:12:19 +00:00
|
|
|
uint64_t gpa;
|
2012-11-28 00:02:17 +00:00
|
|
|
struct vie vie;
|
2012-04-28 16:28:00 +00:00
|
|
|
} paging;
|
2011-05-13 04:54:01 +00:00
|
|
|
/*
|
|
|
|
* VMX specific payload. Used when there is no "better"
|
|
|
|
* exitcode to represent the VM-exit.
|
|
|
|
*/
|
|
|
|
struct {
|
|
|
|
int error; /* vmx inst error */
|
|
|
|
uint32_t exit_reason;
|
|
|
|
uint64_t exit_qualification;
|
|
|
|
} vmx;
|
|
|
|
struct {
|
|
|
|
uint32_t code; /* ecx value */
|
|
|
|
uint64_t wval;
|
|
|
|
} msr;
|
2012-09-25 02:33:25 +00:00
|
|
|
struct {
|
|
|
|
int vcpu;
|
|
|
|
uint64_t rip;
|
|
|
|
} spinup_ap;
|
2011-05-13 04:54:01 +00:00
|
|
|
} u;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _VMM_H_ */
|