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