vmm.h revision 258075
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 258075 2013-11-12 22:51:03Z 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; 41258075Sneelstruct vioapic; 42221828Sgrehanstruct vlapic; 43256072Sneelstruct vmspace; 44256072Sneelstruct vm_object; 45256072Sneelstruct pmap; 46221828Sgrehan 47240922Sneelenum x2apic_state; 48240922Sneel 49221828Sgrehantypedef int (*vmm_init_func_t)(void); 50221828Sgrehantypedef int (*vmm_cleanup_func_t)(void); 51256072Sneeltypedef void * (*vmi_init_func_t)(struct vm *vm, struct pmap *pmap); 52256072Sneeltypedef int (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip, 53256072Sneel struct pmap *pmap); 54221828Sgrehantypedef void (*vmi_cleanup_func_t)(void *vmi); 55221828Sgrehantypedef int (*vmi_get_register_t)(void *vmi, int vcpu, int num, 56221828Sgrehan uint64_t *retval); 57221828Sgrehantypedef int (*vmi_set_register_t)(void *vmi, int vcpu, int num, 58221828Sgrehan uint64_t val); 59221828Sgrehantypedef int (*vmi_get_desc_t)(void *vmi, int vcpu, int num, 60221828Sgrehan struct seg_desc *desc); 61221828Sgrehantypedef int (*vmi_set_desc_t)(void *vmi, int vcpu, int num, 62221828Sgrehan struct seg_desc *desc); 63221828Sgrehantypedef int (*vmi_inject_event_t)(void *vmi, int vcpu, 64221828Sgrehan int type, int vector, 65221828Sgrehan uint32_t code, int code_valid); 66221828Sgrehantypedef int (*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval); 67221828Sgrehantypedef int (*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val); 68256072Sneeltypedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max); 69256072Sneeltypedef void (*vmi_vmspace_free)(struct vmspace *vmspace); 70221828Sgrehan 71221828Sgrehanstruct vmm_ops { 72221828Sgrehan vmm_init_func_t init; /* module wide initialization */ 73221828Sgrehan vmm_cleanup_func_t cleanup; 74221828Sgrehan 75221828Sgrehan vmi_init_func_t vminit; /* vm-specific initialization */ 76221828Sgrehan vmi_run_func_t vmrun; 77221828Sgrehan vmi_cleanup_func_t vmcleanup; 78221828Sgrehan vmi_get_register_t vmgetreg; 79221828Sgrehan vmi_set_register_t vmsetreg; 80221828Sgrehan vmi_get_desc_t vmgetdesc; 81221828Sgrehan vmi_set_desc_t vmsetdesc; 82221828Sgrehan vmi_inject_event_t vminject; 83221828Sgrehan vmi_get_cap_t vmgetcap; 84221828Sgrehan vmi_set_cap_t vmsetcap; 85256072Sneel vmi_vmspace_alloc vmspace_alloc; 86256072Sneel vmi_vmspace_free vmspace_free; 87221828Sgrehan}; 88221828Sgrehan 89221828Sgrehanextern struct vmm_ops vmm_ops_intel; 90221828Sgrehanextern struct vmm_ops vmm_ops_amd; 91221828Sgrehan 92249396Sneelint vm_create(const char *name, struct vm **retvm); 93221828Sgrehanvoid vm_destroy(struct vm *vm); 94221828Sgrehanconst char *vm_name(struct vm *vm); 95241041Sneelint vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len); 96221828Sgrehanint vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa); 97221828Sgrehanint vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len); 98256072Sneelvoid *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot, 99256072Sneel void **cookie); 100256072Sneelvoid vm_gpa_release(void *cookie); 101221828Sgrehanint vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, 102221828Sgrehan struct vm_memory_segment *seg); 103256072Sneelint vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len, 104256072Sneel vm_offset_t *offset, struct vm_object **object); 105256072Sneelboolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa); 106221828Sgrehanint vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval); 107221828Sgrehanint vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val); 108221828Sgrehanint vm_get_seg_desc(struct vm *vm, int vcpu, int reg, 109221828Sgrehan struct seg_desc *ret_desc); 110221828Sgrehanint vm_set_seg_desc(struct vm *vm, int vcpu, int reg, 111221828Sgrehan struct seg_desc *desc); 112221828Sgrehanint vm_run(struct vm *vm, struct vm_run *vmrun); 113221828Sgrehanint vm_inject_event(struct vm *vm, int vcpu, int type, 114221828Sgrehan int vector, uint32_t error_code, int error_code_valid); 115221828Sgrehanint vm_inject_nmi(struct vm *vm, int vcpu); 116241982Sneelint vm_nmi_pending(struct vm *vm, int vcpuid); 117241982Sneelvoid vm_nmi_clear(struct vm *vm, int vcpuid); 118221828Sgrehanuint64_t *vm_guest_msrs(struct vm *vm, int cpu); 119221828Sgrehanstruct vlapic *vm_lapic(struct vm *vm, int cpu); 120258075Sneelstruct vioapic *vm_ioapic(struct vm *vm); 121221828Sgrehanint vm_get_capability(struct vm *vm, int vcpu, int type, int *val); 122221828Sgrehanint vm_set_capability(struct vm *vm, int vcpu, int type, int val); 123240922Sneelint vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state); 124240922Sneelint vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state); 125258075Sneelint vm_apicid2vcpuid(struct vm *vm, int apicid); 126221828Sgrehanvoid vm_activate_cpu(struct vm *vm, int vcpu); 127223621Sgrehancpuset_t vm_active_cpus(struct vm *vm); 128240894Sneelstruct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid); 129221828Sgrehan 130221828Sgrehan/* 131221828Sgrehan * Return 1 if device indicated by bus/slot/func is supposed to be a 132221828Sgrehan * pci passthrough device. 133221828Sgrehan * 134221828Sgrehan * Return 0 otherwise. 135221828Sgrehan */ 136221828Sgrehanint vmm_is_pptdev(int bus, int slot, int func); 137221828Sgrehan 138221828Sgrehanvoid *vm_iommu_domain(struct vm *vm); 139221828Sgrehan 140241489Sneelenum vcpu_state { 141241489Sneel VCPU_IDLE, 142256072Sneel VCPU_FROZEN, 143241489Sneel VCPU_RUNNING, 144256072Sneel VCPU_SLEEPING, 145241489Sneel}; 146221828Sgrehan 147241489Sneelint vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state); 148249879Sgrehanenum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu); 149221828Sgrehan 150221828Sgrehanstatic int __inline 151249879Sgrehanvcpu_is_running(struct vm *vm, int vcpu, int *hostcpu) 152221828Sgrehan{ 153249879Sgrehan return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING); 154221828Sgrehan} 155221828Sgrehan 156241489Sneelvoid *vcpu_stats(struct vm *vm, int vcpu); 157241489Sneelvoid vm_interrupt_hostcpu(struct vm *vm, int vcpu); 158256072Sneelstruct vmspace *vm_get_vmspace(struct vm *vm); 159256072Sneelint vm_assign_pptdev(struct vm *vm, int bus, int slot, int func); 160256072Sneelint vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func); 161221828Sgrehan#endif /* KERNEL */ 162221828Sgrehan 163243640Sneel#include <machine/vmm_instruction_emul.h> 164243640Sneel 165255438Sgrehan#define VM_MAXCPU 16 /* maximum virtual cpus */ 166221828Sgrehan 167221828Sgrehan/* 168221828Sgrehan * Identifiers for events that can be injected into the VM 169221828Sgrehan */ 170221828Sgrehanenum vm_event_type { 171221828Sgrehan VM_EVENT_NONE, 172221828Sgrehan VM_HW_INTR, 173221828Sgrehan VM_NMI, 174221828Sgrehan VM_HW_EXCEPTION, 175221828Sgrehan VM_SW_INTR, 176221828Sgrehan VM_PRIV_SW_EXCEPTION, 177221828Sgrehan VM_SW_EXCEPTION, 178221828Sgrehan VM_EVENT_MAX 179221828Sgrehan}; 180221828Sgrehan 181221828Sgrehan/* 182221828Sgrehan * Identifiers for architecturally defined registers. 183221828Sgrehan */ 184221828Sgrehanenum vm_reg_name { 185221828Sgrehan VM_REG_GUEST_RAX, 186221828Sgrehan VM_REG_GUEST_RBX, 187221828Sgrehan VM_REG_GUEST_RCX, 188221828Sgrehan VM_REG_GUEST_RDX, 189221828Sgrehan VM_REG_GUEST_RSI, 190221828Sgrehan VM_REG_GUEST_RDI, 191221828Sgrehan VM_REG_GUEST_RBP, 192221828Sgrehan VM_REG_GUEST_R8, 193221828Sgrehan VM_REG_GUEST_R9, 194221828Sgrehan VM_REG_GUEST_R10, 195221828Sgrehan VM_REG_GUEST_R11, 196221828Sgrehan VM_REG_GUEST_R12, 197221828Sgrehan VM_REG_GUEST_R13, 198221828Sgrehan VM_REG_GUEST_R14, 199221828Sgrehan VM_REG_GUEST_R15, 200221828Sgrehan VM_REG_GUEST_CR0, 201221828Sgrehan VM_REG_GUEST_CR3, 202221828Sgrehan VM_REG_GUEST_CR4, 203221828Sgrehan VM_REG_GUEST_DR7, 204221828Sgrehan VM_REG_GUEST_RSP, 205221828Sgrehan VM_REG_GUEST_RIP, 206221828Sgrehan VM_REG_GUEST_RFLAGS, 207221828Sgrehan VM_REG_GUEST_ES, 208221828Sgrehan VM_REG_GUEST_CS, 209221828Sgrehan VM_REG_GUEST_SS, 210221828Sgrehan VM_REG_GUEST_DS, 211221828Sgrehan VM_REG_GUEST_FS, 212221828Sgrehan VM_REG_GUEST_GS, 213221828Sgrehan VM_REG_GUEST_LDTR, 214221828Sgrehan VM_REG_GUEST_TR, 215221828Sgrehan VM_REG_GUEST_IDTR, 216221828Sgrehan VM_REG_GUEST_GDTR, 217221828Sgrehan VM_REG_GUEST_EFER, 218221828Sgrehan VM_REG_LAST 219221828Sgrehan}; 220221828Sgrehan 221221828Sgrehan/* 222221828Sgrehan * Identifiers for optional vmm capabilities 223221828Sgrehan */ 224221828Sgrehanenum vm_cap_type { 225221828Sgrehan VM_CAP_HALT_EXIT, 226221828Sgrehan VM_CAP_MTRAP_EXIT, 227221828Sgrehan VM_CAP_PAUSE_EXIT, 228221828Sgrehan VM_CAP_UNRESTRICTED_GUEST, 229256645Sneel VM_CAP_ENABLE_INVPCID, 230221828Sgrehan VM_CAP_MAX 231221828Sgrehan}; 232221828Sgrehan 233240922Sneelenum x2apic_state { 234240922Sneel X2APIC_ENABLED, 235240922Sneel X2APIC_AVAILABLE, 236240922Sneel X2APIC_DISABLED, 237240922Sneel X2APIC_STATE_LAST 238240922Sneel}; 239240922Sneel 240221828Sgrehan/* 241221828Sgrehan * The 'access' field has the format specified in Table 21-2 of the Intel 242221828Sgrehan * Architecture Manual vol 3b. 243221828Sgrehan * 244221828Sgrehan * XXX The contents of the 'access' field are architecturally defined except 245221828Sgrehan * bit 16 - Segment Unusable. 246221828Sgrehan */ 247221828Sgrehanstruct seg_desc { 248221828Sgrehan uint64_t base; 249221828Sgrehan uint32_t limit; 250221828Sgrehan uint32_t access; 251221828Sgrehan}; 252221828Sgrehan 253221828Sgrehanenum vm_exitcode { 254221828Sgrehan VM_EXITCODE_INOUT, 255221828Sgrehan VM_EXITCODE_VMX, 256221828Sgrehan VM_EXITCODE_BOGUS, 257221828Sgrehan VM_EXITCODE_RDMSR, 258221828Sgrehan VM_EXITCODE_WRMSR, 259221828Sgrehan VM_EXITCODE_HLT, 260221828Sgrehan VM_EXITCODE_MTRAP, 261221828Sgrehan VM_EXITCODE_PAUSE, 262234761Sgrehan VM_EXITCODE_PAGING, 263256072Sneel VM_EXITCODE_INST_EMUL, 264240912Sneel VM_EXITCODE_SPINUP_AP, 265234761Sgrehan VM_EXITCODE_MAX 266221828Sgrehan}; 267221828Sgrehan 268221828Sgrehanstruct vm_exit { 269221828Sgrehan enum vm_exitcode exitcode; 270221828Sgrehan int inst_length; /* 0 means unknown */ 271221828Sgrehan uint64_t rip; 272221828Sgrehan union { 273221828Sgrehan struct { 274221828Sgrehan uint16_t bytes:3; /* 1 or 2 or 4 */ 275221828Sgrehan uint16_t in:1; /* out is 0, in is 1 */ 276221828Sgrehan uint16_t string:1; 277221828Sgrehan uint16_t rep:1; 278221828Sgrehan uint16_t port; 279221828Sgrehan uint32_t eax; /* valid for out */ 280221828Sgrehan } inout; 281234761Sgrehan struct { 282241497Sgrehan uint64_t gpa; 283256072Sneel int fault_type; 284256072Sneel int protection; 285256072Sneel } paging; 286256072Sneel struct { 287256072Sneel uint64_t gpa; 288256072Sneel uint64_t gla; 289256072Sneel uint64_t cr3; 290243640Sneel struct vie vie; 291256072Sneel } inst_emul; 292221828Sgrehan /* 293221828Sgrehan * VMX specific payload. Used when there is no "better" 294221828Sgrehan * exitcode to represent the VM-exit. 295221828Sgrehan */ 296221828Sgrehan struct { 297221828Sgrehan int error; /* vmx inst error */ 298221828Sgrehan uint32_t exit_reason; 299221828Sgrehan uint64_t exit_qualification; 300221828Sgrehan } vmx; 301221828Sgrehan struct { 302221828Sgrehan uint32_t code; /* ecx value */ 303221828Sgrehan uint64_t wval; 304221828Sgrehan } msr; 305240912Sneel struct { 306240912Sneel int vcpu; 307240912Sneel uint64_t rip; 308240912Sneel } spinup_ap; 309221828Sgrehan } u; 310221828Sgrehan}; 311221828Sgrehan 312221828Sgrehan#endif /* _VMM_H_ */ 313