vmm.h revision 241041
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 * 26221828Sgrehan * $FreeBSD: vmm.h 482 2011-05-09 21:22:43Z grehan $ 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; 41221828Sgrehanstruct vlapic; 42221828Sgrehan 43240922Sneelenum x2apic_state; 44240922Sneel 45221828Sgrehantypedef int (*vmm_init_func_t)(void); 46221828Sgrehantypedef int (*vmm_cleanup_func_t)(void); 47221828Sgrehantypedef void * (*vmi_init_func_t)(struct vm *vm); /* instance specific apis */ 48240894Sneeltypedef int (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip); 49221828Sgrehantypedef void (*vmi_cleanup_func_t)(void *vmi); 50221828Sgrehantypedef int (*vmi_mmap_func_t)(void *vmi, vm_paddr_t gpa, vm_paddr_t hpa, 51221828Sgrehan size_t length, vm_memattr_t attr, 52221828Sgrehan int prot, boolean_t superpages_ok); 53221828Sgrehantypedef int (*vmi_get_register_t)(void *vmi, int vcpu, int num, 54221828Sgrehan uint64_t *retval); 55221828Sgrehantypedef int (*vmi_set_register_t)(void *vmi, int vcpu, int num, 56221828Sgrehan uint64_t val); 57221828Sgrehantypedef int (*vmi_get_desc_t)(void *vmi, int vcpu, int num, 58221828Sgrehan struct seg_desc *desc); 59221828Sgrehantypedef int (*vmi_set_desc_t)(void *vmi, int vcpu, int num, 60221828Sgrehan struct seg_desc *desc); 61221828Sgrehantypedef int (*vmi_inject_event_t)(void *vmi, int vcpu, 62221828Sgrehan int type, int vector, 63221828Sgrehan uint32_t code, int code_valid); 64221828Sgrehantypedef int (*vmi_inject_nmi_t)(void *vmi, int vcpu); 65221828Sgrehantypedef int (*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval); 66221828Sgrehantypedef int (*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val); 67221828Sgrehan 68221828Sgrehanstruct vmm_ops { 69221828Sgrehan vmm_init_func_t init; /* module wide initialization */ 70221828Sgrehan vmm_cleanup_func_t cleanup; 71221828Sgrehan 72221828Sgrehan vmi_init_func_t vminit; /* vm-specific initialization */ 73221828Sgrehan vmi_run_func_t vmrun; 74221828Sgrehan vmi_cleanup_func_t vmcleanup; 75221828Sgrehan vmi_mmap_func_t vmmmap; 76221828Sgrehan vmi_get_register_t vmgetreg; 77221828Sgrehan vmi_set_register_t vmsetreg; 78221828Sgrehan vmi_get_desc_t vmgetdesc; 79221828Sgrehan vmi_set_desc_t vmsetdesc; 80221828Sgrehan vmi_inject_event_t vminject; 81221828Sgrehan vmi_inject_nmi_t vmnmi; 82221828Sgrehan vmi_get_cap_t vmgetcap; 83221828Sgrehan vmi_set_cap_t vmsetcap; 84221828Sgrehan}; 85221828Sgrehan 86221828Sgrehanextern struct vmm_ops vmm_ops_intel; 87221828Sgrehanextern struct vmm_ops vmm_ops_amd; 88221828Sgrehan 89221828Sgrehanstruct vm *vm_create(const char *name); 90221828Sgrehanvoid vm_destroy(struct vm *vm); 91221828Sgrehanconst char *vm_name(struct vm *vm); 92241041Sneelint vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len); 93221828Sgrehanint vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa); 94221828Sgrehanint vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len); 95221828Sgrehanvm_paddr_t vm_gpa2hpa(struct vm *vm, vm_paddr_t gpa, size_t size); 96221828Sgrehanint vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, 97221828Sgrehan struct vm_memory_segment *seg); 98221828Sgrehanint vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval); 99221828Sgrehanint vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val); 100221828Sgrehanint vm_get_seg_desc(struct vm *vm, int vcpu, int reg, 101221828Sgrehan struct seg_desc *ret_desc); 102221828Sgrehanint vm_set_seg_desc(struct vm *vm, int vcpu, int reg, 103221828Sgrehan struct seg_desc *desc); 104221828Sgrehanint vm_get_pinning(struct vm *vm, int vcpu, int *cpuid); 105221828Sgrehanint vm_set_pinning(struct vm *vm, int vcpu, int cpuid); 106221828Sgrehanint vm_run(struct vm *vm, struct vm_run *vmrun); 107221828Sgrehanint vm_inject_event(struct vm *vm, int vcpu, int type, 108221828Sgrehan int vector, uint32_t error_code, int error_code_valid); 109221828Sgrehanint vm_inject_nmi(struct vm *vm, int vcpu); 110221828Sgrehanuint64_t *vm_guest_msrs(struct vm *vm, int cpu); 111221828Sgrehanstruct vlapic *vm_lapic(struct vm *vm, int cpu); 112221828Sgrehanint vm_get_capability(struct vm *vm, int vcpu, int type, int *val); 113221828Sgrehanint vm_set_capability(struct vm *vm, int vcpu, int type, int val); 114240922Sneelint vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state); 115240922Sneelint vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state); 116221828Sgrehanvoid vm_activate_cpu(struct vm *vm, int vcpu); 117223621Sgrehancpuset_t vm_active_cpus(struct vm *vm); 118240894Sneelstruct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid); 119221828Sgrehan 120221828Sgrehan/* 121221828Sgrehan * Return 1 if device indicated by bus/slot/func is supposed to be a 122221828Sgrehan * pci passthrough device. 123221828Sgrehan * 124221828Sgrehan * Return 0 otherwise. 125221828Sgrehan */ 126221828Sgrehanint vmm_is_pptdev(int bus, int slot, int func); 127221828Sgrehan 128221828Sgrehanvoid *vm_iommu_domain(struct vm *vm); 129221828Sgrehan 130221828Sgrehan#define VCPU_STOPPED 0 131221828Sgrehan#define VCPU_RUNNING 1 132221828Sgrehanvoid vm_set_run_state(struct vm *vm, int vcpu, int running); 133221828Sgrehanint vm_get_run_state(struct vm *vm, int vcpu, int *hostcpu); 134221828Sgrehan 135221828Sgrehanvoid *vcpu_stats(struct vm *vm, int vcpu); 136221828Sgrehan 137221828Sgrehanstatic int __inline 138221828Sgrehanvcpu_is_running(struct vm *vm, int vcpu, int *hostcpu) 139221828Sgrehan{ 140221828Sgrehan return (vm_get_run_state(vm, vcpu, hostcpu) == VCPU_RUNNING); 141221828Sgrehan} 142221828Sgrehan 143221828Sgrehan#endif /* KERNEL */ 144221828Sgrehan 145221828Sgrehan#define VM_MAXCPU 8 /* maximum virtual cpus */ 146221828Sgrehan 147221828Sgrehan/* 148221828Sgrehan * Identifiers for events that can be injected into the VM 149221828Sgrehan */ 150221828Sgrehanenum vm_event_type { 151221828Sgrehan VM_EVENT_NONE, 152221828Sgrehan VM_HW_INTR, 153221828Sgrehan VM_NMI, 154221828Sgrehan VM_HW_EXCEPTION, 155221828Sgrehan VM_SW_INTR, 156221828Sgrehan VM_PRIV_SW_EXCEPTION, 157221828Sgrehan VM_SW_EXCEPTION, 158221828Sgrehan VM_EVENT_MAX 159221828Sgrehan}; 160221828Sgrehan 161221828Sgrehan/* 162221828Sgrehan * Identifiers for architecturally defined registers. 163221828Sgrehan */ 164221828Sgrehanenum vm_reg_name { 165221828Sgrehan VM_REG_GUEST_RAX, 166221828Sgrehan VM_REG_GUEST_RBX, 167221828Sgrehan VM_REG_GUEST_RCX, 168221828Sgrehan VM_REG_GUEST_RDX, 169221828Sgrehan VM_REG_GUEST_RSI, 170221828Sgrehan VM_REG_GUEST_RDI, 171221828Sgrehan VM_REG_GUEST_RBP, 172221828Sgrehan VM_REG_GUEST_R8, 173221828Sgrehan VM_REG_GUEST_R9, 174221828Sgrehan VM_REG_GUEST_R10, 175221828Sgrehan VM_REG_GUEST_R11, 176221828Sgrehan VM_REG_GUEST_R12, 177221828Sgrehan VM_REG_GUEST_R13, 178221828Sgrehan VM_REG_GUEST_R14, 179221828Sgrehan VM_REG_GUEST_R15, 180221828Sgrehan VM_REG_GUEST_CR0, 181221828Sgrehan VM_REG_GUEST_CR3, 182221828Sgrehan VM_REG_GUEST_CR4, 183221828Sgrehan VM_REG_GUEST_DR7, 184221828Sgrehan VM_REG_GUEST_RSP, 185221828Sgrehan VM_REG_GUEST_RIP, 186221828Sgrehan VM_REG_GUEST_RFLAGS, 187221828Sgrehan VM_REG_GUEST_ES, 188221828Sgrehan VM_REG_GUEST_CS, 189221828Sgrehan VM_REG_GUEST_SS, 190221828Sgrehan VM_REG_GUEST_DS, 191221828Sgrehan VM_REG_GUEST_FS, 192221828Sgrehan VM_REG_GUEST_GS, 193221828Sgrehan VM_REG_GUEST_LDTR, 194221828Sgrehan VM_REG_GUEST_TR, 195221828Sgrehan VM_REG_GUEST_IDTR, 196221828Sgrehan VM_REG_GUEST_GDTR, 197221828Sgrehan VM_REG_GUEST_EFER, 198221828Sgrehan VM_REG_LAST 199221828Sgrehan}; 200221828Sgrehan 201221828Sgrehan/* 202221828Sgrehan * Identifiers for optional vmm capabilities 203221828Sgrehan */ 204221828Sgrehanenum vm_cap_type { 205221828Sgrehan VM_CAP_HALT_EXIT, 206221828Sgrehan VM_CAP_MTRAP_EXIT, 207221828Sgrehan VM_CAP_PAUSE_EXIT, 208221828Sgrehan VM_CAP_UNRESTRICTED_GUEST, 209221828Sgrehan VM_CAP_MAX 210221828Sgrehan}; 211221828Sgrehan 212240922Sneelenum x2apic_state { 213240922Sneel X2APIC_ENABLED, 214240922Sneel X2APIC_AVAILABLE, 215240922Sneel X2APIC_DISABLED, 216240922Sneel X2APIC_STATE_LAST 217240922Sneel}; 218240922Sneel 219221828Sgrehan/* 220221828Sgrehan * The 'access' field has the format specified in Table 21-2 of the Intel 221221828Sgrehan * Architecture Manual vol 3b. 222221828Sgrehan * 223221828Sgrehan * XXX The contents of the 'access' field are architecturally defined except 224221828Sgrehan * bit 16 - Segment Unusable. 225221828Sgrehan */ 226221828Sgrehanstruct seg_desc { 227221828Sgrehan uint64_t base; 228221828Sgrehan uint32_t limit; 229221828Sgrehan uint32_t access; 230221828Sgrehan}; 231221828Sgrehan 232221828Sgrehanenum vm_exitcode { 233221828Sgrehan VM_EXITCODE_INOUT, 234221828Sgrehan VM_EXITCODE_VMX, 235221828Sgrehan VM_EXITCODE_BOGUS, 236221828Sgrehan VM_EXITCODE_RDMSR, 237221828Sgrehan VM_EXITCODE_WRMSR, 238221828Sgrehan VM_EXITCODE_HLT, 239221828Sgrehan VM_EXITCODE_MTRAP, 240221828Sgrehan VM_EXITCODE_PAUSE, 241234761Sgrehan VM_EXITCODE_PAGING, 242240912Sneel VM_EXITCODE_SPINUP_AP, 243234761Sgrehan VM_EXITCODE_MAX 244221828Sgrehan}; 245221828Sgrehan 246221828Sgrehanstruct vm_exit { 247221828Sgrehan enum vm_exitcode exitcode; 248221828Sgrehan int inst_length; /* 0 means unknown */ 249221828Sgrehan uint64_t rip; 250221828Sgrehan union { 251221828Sgrehan struct { 252221828Sgrehan uint16_t bytes:3; /* 1 or 2 or 4 */ 253221828Sgrehan uint16_t in:1; /* out is 0, in is 1 */ 254221828Sgrehan uint16_t string:1; 255221828Sgrehan uint16_t rep:1; 256221828Sgrehan uint16_t port; 257221828Sgrehan uint32_t eax; /* valid for out */ 258221828Sgrehan } inout; 259234761Sgrehan struct { 260234761Sgrehan uint64_t cr3; 261234761Sgrehan } paging; 262221828Sgrehan /* 263221828Sgrehan * VMX specific payload. Used when there is no "better" 264221828Sgrehan * exitcode to represent the VM-exit. 265221828Sgrehan */ 266221828Sgrehan struct { 267221828Sgrehan int error; /* vmx inst error */ 268221828Sgrehan uint32_t exit_reason; 269221828Sgrehan uint64_t exit_qualification; 270221828Sgrehan } vmx; 271221828Sgrehan struct { 272221828Sgrehan uint32_t code; /* ecx value */ 273221828Sgrehan uint64_t wval; 274221828Sgrehan } msr; 275240912Sneel struct { 276240912Sneel int vcpu; 277240912Sneel uint64_t rip; 278240912Sneel } spinup_ap; 279221828Sgrehan } u; 280221828Sgrehan}; 281221828Sgrehan 282221828Sgrehan#endif /* _VMM_H_ */ 283