vmm.h revision 259863
1209878Snwhitehorn/*- 2209878Snwhitehorn * Copyright (c) 2011 NetApp, Inc. 3209878Snwhitehorn * All rights reserved. 4209878Snwhitehorn * 5209878Snwhitehorn * Redistribution and use in source and binary forms, with or without 6209878Snwhitehorn * modification, are permitted provided that the following conditions 7209878Snwhitehorn * are met: 8209878Snwhitehorn * 1. Redistributions of source code must retain the above copyright 9209878Snwhitehorn * notice, this list of conditions and the following disclaimer. 10209878Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright 11209878Snwhitehorn * notice, this list of conditions and the following disclaimer in the 12209878Snwhitehorn * documentation and/or other materials provided with the distribution. 13209878Snwhitehorn * 14209878Snwhitehorn * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 15209878Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16209878Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17209878Snwhitehorn * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 18209878Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19209878Snwhitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20209878Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21209878Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22209878Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23209878Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24209878Snwhitehorn * SUCH DAMAGE. 25209878Snwhitehorn * 26209878Snwhitehorn * $FreeBSD: head/sys/amd64/include/vmm.h 259863 2013-12-25 06:46:31Z neel $ 27209878Snwhitehorn */ 28209878Snwhitehorn 29209878Snwhitehorn#ifndef _VMM_H_ 30209878Snwhitehorn#define _VMM_H_ 31209878Snwhitehorn 32209878Snwhitehorn#ifdef _KERNEL 33209878Snwhitehorn 34209878Snwhitehorn#define VM_MAX_NAMELEN 32 35209878Snwhitehorn 36209878Snwhitehornstruct vm; 37209878Snwhitehornstruct vm_memory_segment; 38209878Snwhitehornstruct seg_desc; 39209878Snwhitehornstruct vm_exit; 40209878Snwhitehornstruct vm_run; 41209878Snwhitehornstruct vhpet; 42209878Snwhitehornstruct vioapic; 43209878Snwhitehornstruct vlapic; 44209878Snwhitehornstruct vmspace; 45209878Snwhitehornstruct vm_object; 46209878Snwhitehornstruct pmap; 47209878Snwhitehorn 48209878Snwhitehornenum x2apic_state; 49209878Snwhitehorn 50209878Snwhitehorntypedef int (*vmm_init_func_t)(void); 51209878Snwhitehorntypedef int (*vmm_cleanup_func_t)(void); 52209878Snwhitehorntypedef void (*vmm_resume_func_t)(void); 53209878Snwhitehorntypedef void * (*vmi_init_func_t)(struct vm *vm, struct pmap *pmap); 54209878Snwhitehorntypedef int (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip, 55209878Snwhitehorn struct pmap *pmap); 56209878Snwhitehorntypedef void (*vmi_cleanup_func_t)(void *vmi); 57209878Snwhitehorntypedef int (*vmi_get_register_t)(void *vmi, int vcpu, int num, 58209878Snwhitehorn uint64_t *retval); 59209878Snwhitehorntypedef int (*vmi_set_register_t)(void *vmi, int vcpu, int num, 60209878Snwhitehorn uint64_t val); 61209878Snwhitehorntypedef int (*vmi_get_desc_t)(void *vmi, int vcpu, int num, 62209878Snwhitehorn struct seg_desc *desc); 63209878Snwhitehorntypedef int (*vmi_set_desc_t)(void *vmi, int vcpu, int num, 64209878Snwhitehorn struct seg_desc *desc); 65209878Snwhitehorntypedef int (*vmi_inject_event_t)(void *vmi, int vcpu, 66209878Snwhitehorn int type, int vector, 67209878Snwhitehorn uint32_t code, int code_valid); 68209878Snwhitehorntypedef int (*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval); 69209878Snwhitehorntypedef int (*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val); 70209878Snwhitehorntypedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max); 71209878Snwhitehorntypedef void (*vmi_vmspace_free)(struct vmspace *vmspace); 72209878Snwhitehorntypedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu); 73209878Snwhitehorntypedef void (*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic); 74209878Snwhitehorn 75209878Snwhitehornstruct vmm_ops { 76209878Snwhitehorn vmm_init_func_t init; /* module wide initialization */ 77209878Snwhitehorn vmm_cleanup_func_t cleanup; 78209878Snwhitehorn vmm_resume_func_t resume; 79209878Snwhitehorn 80209878Snwhitehorn vmi_init_func_t vminit; /* vm-specific initialization */ 81209878Snwhitehorn vmi_run_func_t vmrun; 82209878Snwhitehorn vmi_cleanup_func_t vmcleanup; 83209878Snwhitehorn vmi_get_register_t vmgetreg; 84209878Snwhitehorn vmi_set_register_t vmsetreg; 85209878Snwhitehorn vmi_get_desc_t vmgetdesc; 86209878Snwhitehorn vmi_set_desc_t vmsetdesc; 87209878Snwhitehorn vmi_inject_event_t vminject; 88209878Snwhitehorn vmi_get_cap_t vmgetcap; 89209878Snwhitehorn vmi_set_cap_t vmsetcap; 90209878Snwhitehorn vmi_vmspace_alloc vmspace_alloc; 91209878Snwhitehorn vmi_vmspace_free vmspace_free; 92209878Snwhitehorn vmi_vlapic_init vlapic_init; 93209878Snwhitehorn vmi_vlapic_cleanup vlapic_cleanup; 94209878Snwhitehorn}; 95209878Snwhitehorn 96209878Snwhitehornextern struct vmm_ops vmm_ops_intel; 97209878Snwhitehornextern struct vmm_ops vmm_ops_amd; 98209878Snwhitehorn 99209878Snwhitehornint vm_create(const char *name, struct vm **retvm); 100209878Snwhitehornvoid vm_destroy(struct vm *vm); 101209878Snwhitehornconst char *vm_name(struct vm *vm); 102209878Snwhitehornint vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len); 103209878Snwhitehornint vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa); 104209878Snwhitehornint vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len); 105209878Snwhitehornvoid *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot, 106209878Snwhitehorn void **cookie); 107209878Snwhitehornvoid vm_gpa_release(void *cookie); 108209878Snwhitehornint vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, 109209878Snwhitehorn struct vm_memory_segment *seg); 110209878Snwhitehornint vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len, 111209878Snwhitehorn vm_offset_t *offset, struct vm_object **object); 112209878Snwhitehornboolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa); 113209878Snwhitehornint vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval); 114234003Snwhitehornint vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val); 115209878Snwhitehornint vm_get_seg_desc(struct vm *vm, int vcpu, int reg, 116209878Snwhitehorn struct seg_desc *ret_desc); 117209878Snwhitehornint vm_set_seg_desc(struct vm *vm, int vcpu, int reg, 118209878Snwhitehorn struct seg_desc *desc); 119int vm_run(struct vm *vm, struct vm_run *vmrun); 120int vm_inject_event(struct vm *vm, int vcpu, int type, 121 int vector, uint32_t error_code, int error_code_valid); 122int vm_inject_nmi(struct vm *vm, int vcpu); 123int vm_nmi_pending(struct vm *vm, int vcpuid); 124void vm_nmi_clear(struct vm *vm, int vcpuid); 125uint64_t *vm_guest_msrs(struct vm *vm, int cpu); 126struct vlapic *vm_lapic(struct vm *vm, int cpu); 127struct vioapic *vm_ioapic(struct vm *vm); 128struct vhpet *vm_hpet(struct vm *vm); 129int vm_get_capability(struct vm *vm, int vcpu, int type, int *val); 130int vm_set_capability(struct vm *vm, int vcpu, int type, int val); 131int vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state); 132int vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state); 133int vm_apicid2vcpuid(struct vm *vm, int apicid); 134void vm_activate_cpu(struct vm *vm, int vcpu); 135cpuset_t vm_active_cpus(struct vm *vm); 136struct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid); 137 138/* 139 * Return 1 if device indicated by bus/slot/func is supposed to be a 140 * pci passthrough device. 141 * 142 * Return 0 otherwise. 143 */ 144int vmm_is_pptdev(int bus, int slot, int func); 145 146void *vm_iommu_domain(struct vm *vm); 147 148enum vcpu_state { 149 VCPU_IDLE, 150 VCPU_FROZEN, 151 VCPU_RUNNING, 152 VCPU_SLEEPING, 153}; 154 155int vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state, 156 bool from_idle); 157enum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu); 158 159static int __inline 160vcpu_is_running(struct vm *vm, int vcpu, int *hostcpu) 161{ 162 return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING); 163} 164 165void *vcpu_stats(struct vm *vm, int vcpu); 166void vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr); 167struct vmspace *vm_get_vmspace(struct vm *vm); 168int vm_assign_pptdev(struct vm *vm, int bus, int slot, int func); 169int vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func); 170#endif /* KERNEL */ 171 172#include <machine/vmm_instruction_emul.h> 173 174#define VM_MAXCPU 16 /* maximum virtual cpus */ 175 176/* 177 * Identifiers for events that can be injected into the VM 178 */ 179enum vm_event_type { 180 VM_EVENT_NONE, 181 VM_HW_INTR, 182 VM_NMI, 183 VM_HW_EXCEPTION, 184 VM_SW_INTR, 185 VM_PRIV_SW_EXCEPTION, 186 VM_SW_EXCEPTION, 187 VM_EVENT_MAX 188}; 189 190/* 191 * Identifiers for architecturally defined registers. 192 */ 193enum vm_reg_name { 194 VM_REG_GUEST_RAX, 195 VM_REG_GUEST_RBX, 196 VM_REG_GUEST_RCX, 197 VM_REG_GUEST_RDX, 198 VM_REG_GUEST_RSI, 199 VM_REG_GUEST_RDI, 200 VM_REG_GUEST_RBP, 201 VM_REG_GUEST_R8, 202 VM_REG_GUEST_R9, 203 VM_REG_GUEST_R10, 204 VM_REG_GUEST_R11, 205 VM_REG_GUEST_R12, 206 VM_REG_GUEST_R13, 207 VM_REG_GUEST_R14, 208 VM_REG_GUEST_R15, 209 VM_REG_GUEST_CR0, 210 VM_REG_GUEST_CR3, 211 VM_REG_GUEST_CR4, 212 VM_REG_GUEST_DR7, 213 VM_REG_GUEST_RSP, 214 VM_REG_GUEST_RIP, 215 VM_REG_GUEST_RFLAGS, 216 VM_REG_GUEST_ES, 217 VM_REG_GUEST_CS, 218 VM_REG_GUEST_SS, 219 VM_REG_GUEST_DS, 220 VM_REG_GUEST_FS, 221 VM_REG_GUEST_GS, 222 VM_REG_GUEST_LDTR, 223 VM_REG_GUEST_TR, 224 VM_REG_GUEST_IDTR, 225 VM_REG_GUEST_GDTR, 226 VM_REG_GUEST_EFER, 227 VM_REG_LAST 228}; 229 230/* 231 * Identifiers for optional vmm capabilities 232 */ 233enum vm_cap_type { 234 VM_CAP_HALT_EXIT, 235 VM_CAP_MTRAP_EXIT, 236 VM_CAP_PAUSE_EXIT, 237 VM_CAP_UNRESTRICTED_GUEST, 238 VM_CAP_ENABLE_INVPCID, 239 VM_CAP_MAX 240}; 241 242enum x2apic_state { 243 X2APIC_ENABLED, 244 X2APIC_AVAILABLE, 245 X2APIC_DISABLED, 246 X2APIC_STATE_LAST 247}; 248 249/* 250 * The 'access' field has the format specified in Table 21-2 of the Intel 251 * Architecture Manual vol 3b. 252 * 253 * XXX The contents of the 'access' field are architecturally defined except 254 * bit 16 - Segment Unusable. 255 */ 256struct seg_desc { 257 uint64_t base; 258 uint32_t limit; 259 uint32_t access; 260}; 261 262enum vm_exitcode { 263 VM_EXITCODE_INOUT, 264 VM_EXITCODE_VMX, 265 VM_EXITCODE_BOGUS, 266 VM_EXITCODE_RDMSR, 267 VM_EXITCODE_WRMSR, 268 VM_EXITCODE_HLT, 269 VM_EXITCODE_MTRAP, 270 VM_EXITCODE_PAUSE, 271 VM_EXITCODE_PAGING, 272 VM_EXITCODE_INST_EMUL, 273 VM_EXITCODE_SPINUP_AP, 274 VM_EXITCODE_SPINDOWN_CPU, 275 VM_EXITCODE_MAX 276}; 277 278struct vm_exit { 279 enum vm_exitcode exitcode; 280 int inst_length; /* 0 means unknown */ 281 uint64_t rip; 282 union { 283 struct { 284 uint16_t bytes:3; /* 1 or 2 or 4 */ 285 uint16_t in:1; /* out is 0, in is 1 */ 286 uint16_t string:1; 287 uint16_t rep:1; 288 uint16_t port; 289 uint32_t eax; /* valid for out */ 290 } inout; 291 struct { 292 uint64_t gpa; 293 int fault_type; 294 } paging; 295 struct { 296 uint64_t gpa; 297 uint64_t gla; 298 uint64_t cr3; 299 struct vie vie; 300 } inst_emul; 301 /* 302 * VMX specific payload. Used when there is no "better" 303 * exitcode to represent the VM-exit. 304 */ 305 struct { 306 int error; /* vmx inst error */ 307 uint32_t exit_reason; 308 uint64_t exit_qualification; 309 } vmx; 310 struct { 311 uint32_t code; /* ecx value */ 312 uint64_t wval; 313 } msr; 314 struct { 315 int vcpu; 316 uint64_t rip; 317 } spinup_ap; 318 struct { 319 uint64_t rflags; 320 } hlt; 321 } u; 322}; 323 324#endif /* _VMM_H_ */ 325