vmm.h revision 258579
1221828Sgrehan/*- 2221828Sgrehan * Copyright (c) 2011 NetApp, Inc. 3221828Sgrehan * All rights reserved. 4221828Sgrehan * 5221828Sgrehan * Redistribution and use in source and binary forms, with or without 6221828Sgrehan * modification, are permitted provided that the following conditions 7221828Sgrehan * are met: 8221828Sgrehan * 1. Redistributions of source code must retain the above copyright 9221828Sgrehan * notice, this list of conditions and the following disclaimer. 10221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright 11221828Sgrehan * notice, this list of conditions and the following disclaimer in the 12221828Sgrehan * documentation and/or other materials provided with the distribution. 13221828Sgrehan * 14221828Sgrehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 15221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17221828Sgrehan * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 18221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20221828Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24221828Sgrehan * SUCH DAMAGE. 25221828Sgrehan * 26245678Sneel * $FreeBSD: head/sys/amd64/include/vmm.h 258579 2013-11-25 19:04:51Z neel $ 27221828Sgrehan */ 28221828Sgrehan 29221828Sgrehan#ifndef _VMM_H_ 30221828Sgrehan#define _VMM_H_ 31221828Sgrehan 32221828Sgrehan#ifdef _KERNEL 33221828Sgrehan 34221828Sgrehan#define VM_MAX_NAMELEN 32 35221828Sgrehan 36221828Sgrehanstruct vm; 37221828Sgrehanstruct vm_memory_segment; 38221828Sgrehanstruct seg_desc; 39221828Sgrehanstruct vm_exit; 40221828Sgrehanstruct vm_run; 41258579Sneelstruct vhpet; 42258075Sneelstruct vioapic; 43221828Sgrehanstruct vlapic; 44256072Sneelstruct vmspace; 45256072Sneelstruct vm_object; 46256072Sneelstruct pmap; 47221828Sgrehan 48240922Sneelenum x2apic_state; 49240922Sneel 50221828Sgrehantypedef int (*vmm_init_func_t)(void); 51221828Sgrehantypedef int (*vmm_cleanup_func_t)(void); 52256072Sneeltypedef void * (*vmi_init_func_t)(struct vm *vm, struct pmap *pmap); 53256072Sneeltypedef int (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip, 54256072Sneel struct pmap *pmap); 55221828Sgrehantypedef void (*vmi_cleanup_func_t)(void *vmi); 56221828Sgrehantypedef int (*vmi_get_register_t)(void *vmi, int vcpu, int num, 57221828Sgrehan uint64_t *retval); 58221828Sgrehantypedef int (*vmi_set_register_t)(void *vmi, int vcpu, int num, 59221828Sgrehan uint64_t val); 60221828Sgrehantypedef int (*vmi_get_desc_t)(void *vmi, int vcpu, int num, 61221828Sgrehan struct seg_desc *desc); 62221828Sgrehantypedef int (*vmi_set_desc_t)(void *vmi, int vcpu, int num, 63221828Sgrehan struct seg_desc *desc); 64221828Sgrehantypedef int (*vmi_inject_event_t)(void *vmi, int vcpu, 65221828Sgrehan int type, int vector, 66221828Sgrehan uint32_t code, int code_valid); 67221828Sgrehantypedef int (*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval); 68221828Sgrehantypedef int (*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val); 69256072Sneeltypedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max); 70256072Sneeltypedef void (*vmi_vmspace_free)(struct vmspace *vmspace); 71221828Sgrehan 72221828Sgrehanstruct vmm_ops { 73221828Sgrehan vmm_init_func_t init; /* module wide initialization */ 74221828Sgrehan vmm_cleanup_func_t cleanup; 75221828Sgrehan 76221828Sgrehan vmi_init_func_t vminit; /* vm-specific initialization */ 77221828Sgrehan vmi_run_func_t vmrun; 78221828Sgrehan vmi_cleanup_func_t vmcleanup; 79221828Sgrehan vmi_get_register_t vmgetreg; 80221828Sgrehan vmi_set_register_t vmsetreg; 81221828Sgrehan vmi_get_desc_t vmgetdesc; 82221828Sgrehan vmi_set_desc_t vmsetdesc; 83221828Sgrehan vmi_inject_event_t vminject; 84221828Sgrehan vmi_get_cap_t vmgetcap; 85221828Sgrehan vmi_set_cap_t vmsetcap; 86256072Sneel vmi_vmspace_alloc vmspace_alloc; 87256072Sneel vmi_vmspace_free vmspace_free; 88221828Sgrehan}; 89221828Sgrehan 90221828Sgrehanextern struct vmm_ops vmm_ops_intel; 91221828Sgrehanextern struct vmm_ops vmm_ops_amd; 92221828Sgrehan 93249396Sneelint vm_create(const char *name, struct vm **retvm); 94221828Sgrehanvoid vm_destroy(struct vm *vm); 95221828Sgrehanconst char *vm_name(struct vm *vm); 96241041Sneelint vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len); 97221828Sgrehanint vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa); 98221828Sgrehanint vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len); 99256072Sneelvoid *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot, 100256072Sneel void **cookie); 101256072Sneelvoid vm_gpa_release(void *cookie); 102221828Sgrehanint vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, 103221828Sgrehan struct vm_memory_segment *seg); 104256072Sneelint vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len, 105256072Sneel vm_offset_t *offset, struct vm_object **object); 106256072Sneelboolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa); 107221828Sgrehanint vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval); 108221828Sgrehanint vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val); 109221828Sgrehanint vm_get_seg_desc(struct vm *vm, int vcpu, int reg, 110221828Sgrehan struct seg_desc *ret_desc); 111221828Sgrehanint vm_set_seg_desc(struct vm *vm, int vcpu, int reg, 112221828Sgrehan struct seg_desc *desc); 113221828Sgrehanint vm_run(struct vm *vm, struct vm_run *vmrun); 114221828Sgrehanint vm_inject_event(struct vm *vm, int vcpu, int type, 115221828Sgrehan int vector, uint32_t error_code, int error_code_valid); 116221828Sgrehanint vm_inject_nmi(struct vm *vm, int vcpu); 117241982Sneelint vm_nmi_pending(struct vm *vm, int vcpuid); 118241982Sneelvoid vm_nmi_clear(struct vm *vm, int vcpuid); 119221828Sgrehanuint64_t *vm_guest_msrs(struct vm *vm, int cpu); 120221828Sgrehanstruct vlapic *vm_lapic(struct vm *vm, int cpu); 121258075Sneelstruct vioapic *vm_ioapic(struct vm *vm); 122258579Sneelstruct vhpet *vm_hpet(struct vm *vm); 123221828Sgrehanint vm_get_capability(struct vm *vm, int vcpu, int type, int *val); 124221828Sgrehanint vm_set_capability(struct vm *vm, int vcpu, int type, int val); 125240922Sneelint vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state); 126240922Sneelint vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state); 127258075Sneelint vm_apicid2vcpuid(struct vm *vm, int apicid); 128221828Sgrehanvoid vm_activate_cpu(struct vm *vm, int vcpu); 129223621Sgrehancpuset_t vm_active_cpus(struct vm *vm); 130240894Sneelstruct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid); 131221828Sgrehan 132221828Sgrehan/* 133221828Sgrehan * Return 1 if device indicated by bus/slot/func is supposed to be a 134221828Sgrehan * pci passthrough device. 135221828Sgrehan * 136221828Sgrehan * Return 0 otherwise. 137221828Sgrehan */ 138221828Sgrehanint vmm_is_pptdev(int bus, int slot, int func); 139221828Sgrehan 140221828Sgrehanvoid *vm_iommu_domain(struct vm *vm); 141221828Sgrehan 142241489Sneelenum vcpu_state { 143241489Sneel VCPU_IDLE, 144256072Sneel VCPU_FROZEN, 145241489Sneel VCPU_RUNNING, 146256072Sneel VCPU_SLEEPING, 147241489Sneel}; 148221828Sgrehan 149241489Sneelint vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state); 150249879Sgrehanenum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu); 151221828Sgrehan 152221828Sgrehanstatic int __inline 153249879Sgrehanvcpu_is_running(struct vm *vm, int vcpu, int *hostcpu) 154221828Sgrehan{ 155249879Sgrehan return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING); 156221828Sgrehan} 157221828Sgrehan 158241489Sneelvoid *vcpu_stats(struct vm *vm, int vcpu); 159241489Sneelvoid vm_interrupt_hostcpu(struct vm *vm, int vcpu); 160256072Sneelstruct vmspace *vm_get_vmspace(struct vm *vm); 161256072Sneelint vm_assign_pptdev(struct vm *vm, int bus, int slot, int func); 162256072Sneelint vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func); 163221828Sgrehan#endif /* KERNEL */ 164221828Sgrehan 165243640Sneel#include <machine/vmm_instruction_emul.h> 166243640Sneel 167255438Sgrehan#define VM_MAXCPU 16 /* maximum virtual cpus */ 168221828Sgrehan 169221828Sgrehan/* 170221828Sgrehan * Identifiers for events that can be injected into the VM 171221828Sgrehan */ 172221828Sgrehanenum vm_event_type { 173221828Sgrehan VM_EVENT_NONE, 174221828Sgrehan VM_HW_INTR, 175221828Sgrehan VM_NMI, 176221828Sgrehan VM_HW_EXCEPTION, 177221828Sgrehan VM_SW_INTR, 178221828Sgrehan VM_PRIV_SW_EXCEPTION, 179221828Sgrehan VM_SW_EXCEPTION, 180221828Sgrehan VM_EVENT_MAX 181221828Sgrehan}; 182221828Sgrehan 183221828Sgrehan/* 184221828Sgrehan * Identifiers for architecturally defined registers. 185221828Sgrehan */ 186221828Sgrehanenum vm_reg_name { 187221828Sgrehan VM_REG_GUEST_RAX, 188221828Sgrehan VM_REG_GUEST_RBX, 189221828Sgrehan VM_REG_GUEST_RCX, 190221828Sgrehan VM_REG_GUEST_RDX, 191221828Sgrehan VM_REG_GUEST_RSI, 192221828Sgrehan VM_REG_GUEST_RDI, 193221828Sgrehan VM_REG_GUEST_RBP, 194221828Sgrehan VM_REG_GUEST_R8, 195221828Sgrehan VM_REG_GUEST_R9, 196221828Sgrehan VM_REG_GUEST_R10, 197221828Sgrehan VM_REG_GUEST_R11, 198221828Sgrehan VM_REG_GUEST_R12, 199221828Sgrehan VM_REG_GUEST_R13, 200221828Sgrehan VM_REG_GUEST_R14, 201221828Sgrehan VM_REG_GUEST_R15, 202221828Sgrehan VM_REG_GUEST_CR0, 203221828Sgrehan VM_REG_GUEST_CR3, 204221828Sgrehan VM_REG_GUEST_CR4, 205221828Sgrehan VM_REG_GUEST_DR7, 206221828Sgrehan VM_REG_GUEST_RSP, 207221828Sgrehan VM_REG_GUEST_RIP, 208221828Sgrehan VM_REG_GUEST_RFLAGS, 209221828Sgrehan VM_REG_GUEST_ES, 210221828Sgrehan VM_REG_GUEST_CS, 211221828Sgrehan VM_REG_GUEST_SS, 212221828Sgrehan VM_REG_GUEST_DS, 213221828Sgrehan VM_REG_GUEST_FS, 214221828Sgrehan VM_REG_GUEST_GS, 215221828Sgrehan VM_REG_GUEST_LDTR, 216221828Sgrehan VM_REG_GUEST_TR, 217221828Sgrehan VM_REG_GUEST_IDTR, 218221828Sgrehan VM_REG_GUEST_GDTR, 219221828Sgrehan VM_REG_GUEST_EFER, 220221828Sgrehan VM_REG_LAST 221221828Sgrehan}; 222221828Sgrehan 223221828Sgrehan/* 224221828Sgrehan * Identifiers for optional vmm capabilities 225221828Sgrehan */ 226221828Sgrehanenum vm_cap_type { 227221828Sgrehan VM_CAP_HALT_EXIT, 228221828Sgrehan VM_CAP_MTRAP_EXIT, 229221828Sgrehan VM_CAP_PAUSE_EXIT, 230221828Sgrehan VM_CAP_UNRESTRICTED_GUEST, 231256645Sneel VM_CAP_ENABLE_INVPCID, 232221828Sgrehan VM_CAP_MAX 233221828Sgrehan}; 234221828Sgrehan 235240922Sneelenum x2apic_state { 236240922Sneel X2APIC_ENABLED, 237240922Sneel X2APIC_AVAILABLE, 238240922Sneel X2APIC_DISABLED, 239240922Sneel X2APIC_STATE_LAST 240240922Sneel}; 241240922Sneel 242221828Sgrehan/* 243221828Sgrehan * The 'access' field has the format specified in Table 21-2 of the Intel 244221828Sgrehan * Architecture Manual vol 3b. 245221828Sgrehan * 246221828Sgrehan * XXX The contents of the 'access' field are architecturally defined except 247221828Sgrehan * bit 16 - Segment Unusable. 248221828Sgrehan */ 249221828Sgrehanstruct seg_desc { 250221828Sgrehan uint64_t base; 251221828Sgrehan uint32_t limit; 252221828Sgrehan uint32_t access; 253221828Sgrehan}; 254221828Sgrehan 255221828Sgrehanenum vm_exitcode { 256221828Sgrehan VM_EXITCODE_INOUT, 257221828Sgrehan VM_EXITCODE_VMX, 258221828Sgrehan VM_EXITCODE_BOGUS, 259221828Sgrehan VM_EXITCODE_RDMSR, 260221828Sgrehan VM_EXITCODE_WRMSR, 261221828Sgrehan VM_EXITCODE_HLT, 262221828Sgrehan VM_EXITCODE_MTRAP, 263221828Sgrehan VM_EXITCODE_PAUSE, 264234761Sgrehan VM_EXITCODE_PAGING, 265256072Sneel VM_EXITCODE_INST_EMUL, 266240912Sneel VM_EXITCODE_SPINUP_AP, 267234761Sgrehan VM_EXITCODE_MAX 268221828Sgrehan}; 269221828Sgrehan 270221828Sgrehanstruct vm_exit { 271221828Sgrehan enum vm_exitcode exitcode; 272221828Sgrehan int inst_length; /* 0 means unknown */ 273221828Sgrehan uint64_t rip; 274221828Sgrehan union { 275221828Sgrehan struct { 276221828Sgrehan uint16_t bytes:3; /* 1 or 2 or 4 */ 277221828Sgrehan uint16_t in:1; /* out is 0, in is 1 */ 278221828Sgrehan uint16_t string:1; 279221828Sgrehan uint16_t rep:1; 280221828Sgrehan uint16_t port; 281221828Sgrehan uint32_t eax; /* valid for out */ 282221828Sgrehan } inout; 283234761Sgrehan struct { 284241497Sgrehan uint64_t gpa; 285256072Sneel int fault_type; 286256072Sneel int protection; 287256072Sneel } paging; 288256072Sneel struct { 289256072Sneel uint64_t gpa; 290256072Sneel uint64_t gla; 291256072Sneel uint64_t cr3; 292243640Sneel struct vie vie; 293256072Sneel } inst_emul; 294221828Sgrehan /* 295221828Sgrehan * VMX specific payload. Used when there is no "better" 296221828Sgrehan * exitcode to represent the VM-exit. 297221828Sgrehan */ 298221828Sgrehan struct { 299221828Sgrehan int error; /* vmx inst error */ 300221828Sgrehan uint32_t exit_reason; 301221828Sgrehan uint64_t exit_qualification; 302221828Sgrehan } vmx; 303221828Sgrehan struct { 304221828Sgrehan uint32_t code; /* ecx value */ 305221828Sgrehan uint64_t wval; 306221828Sgrehan } msr; 307240912Sneel struct { 308240912Sneel int vcpu; 309240912Sneel uint64_t rip; 310240912Sneel } spinup_ap; 311221828Sgrehan } u; 312221828Sgrehan}; 313221828Sgrehan 314221828Sgrehan#endif /* _VMM_H_ */ 315