vmm.h revision 260619
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 260619 2014-01-14 01:55:58Z 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 50260466Sneeltypedef int (*vmm_init_func_t)(int ipinum); 51221828Sgrehantypedef int (*vmm_cleanup_func_t)(void); 52259782Sjhbtypedef void (*vmm_resume_func_t)(void); 53256072Sneeltypedef void * (*vmi_init_func_t)(struct vm *vm, struct pmap *pmap); 54256072Sneeltypedef int (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip, 55260619Sneel struct pmap *pmap, void *rendezvous_cookie); 56221828Sgrehantypedef void (*vmi_cleanup_func_t)(void *vmi); 57221828Sgrehantypedef int (*vmi_get_register_t)(void *vmi, int vcpu, int num, 58221828Sgrehan uint64_t *retval); 59221828Sgrehantypedef int (*vmi_set_register_t)(void *vmi, int vcpu, int num, 60221828Sgrehan uint64_t val); 61221828Sgrehantypedef int (*vmi_get_desc_t)(void *vmi, int vcpu, int num, 62221828Sgrehan struct seg_desc *desc); 63221828Sgrehantypedef int (*vmi_set_desc_t)(void *vmi, int vcpu, int num, 64221828Sgrehan struct seg_desc *desc); 65221828Sgrehantypedef int (*vmi_inject_event_t)(void *vmi, int vcpu, 66221828Sgrehan int type, int vector, 67221828Sgrehan uint32_t code, int code_valid); 68221828Sgrehantypedef int (*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval); 69221828Sgrehantypedef int (*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val); 70256072Sneeltypedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max); 71256072Sneeltypedef void (*vmi_vmspace_free)(struct vmspace *vmspace); 72259863Sneeltypedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu); 73259863Sneeltypedef void (*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic); 74221828Sgrehan 75221828Sgrehanstruct vmm_ops { 76221828Sgrehan vmm_init_func_t init; /* module wide initialization */ 77221828Sgrehan vmm_cleanup_func_t cleanup; 78259782Sjhb vmm_resume_func_t resume; 79221828Sgrehan 80221828Sgrehan vmi_init_func_t vminit; /* vm-specific initialization */ 81221828Sgrehan vmi_run_func_t vmrun; 82221828Sgrehan vmi_cleanup_func_t vmcleanup; 83221828Sgrehan vmi_get_register_t vmgetreg; 84221828Sgrehan vmi_set_register_t vmsetreg; 85221828Sgrehan vmi_get_desc_t vmgetdesc; 86221828Sgrehan vmi_set_desc_t vmsetdesc; 87221828Sgrehan vmi_inject_event_t vminject; 88221828Sgrehan vmi_get_cap_t vmgetcap; 89221828Sgrehan vmi_set_cap_t vmsetcap; 90256072Sneel vmi_vmspace_alloc vmspace_alloc; 91256072Sneel vmi_vmspace_free vmspace_free; 92259863Sneel vmi_vlapic_init vlapic_init; 93259863Sneel vmi_vlapic_cleanup vlapic_cleanup; 94221828Sgrehan}; 95221828Sgrehan 96221828Sgrehanextern struct vmm_ops vmm_ops_intel; 97221828Sgrehanextern struct vmm_ops vmm_ops_amd; 98221828Sgrehan 99249396Sneelint vm_create(const char *name, struct vm **retvm); 100221828Sgrehanvoid vm_destroy(struct vm *vm); 101221828Sgrehanconst char *vm_name(struct vm *vm); 102241041Sneelint vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len); 103221828Sgrehanint vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa); 104221828Sgrehanint vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len); 105256072Sneelvoid *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot, 106256072Sneel void **cookie); 107256072Sneelvoid vm_gpa_release(void *cookie); 108221828Sgrehanint vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, 109221828Sgrehan struct vm_memory_segment *seg); 110256072Sneelint vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len, 111256072Sneel vm_offset_t *offset, struct vm_object **object); 112256072Sneelboolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa); 113221828Sgrehanint vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval); 114221828Sgrehanint vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val); 115221828Sgrehanint vm_get_seg_desc(struct vm *vm, int vcpu, int reg, 116221828Sgrehan struct seg_desc *ret_desc); 117221828Sgrehanint vm_set_seg_desc(struct vm *vm, int vcpu, int reg, 118221828Sgrehan struct seg_desc *desc); 119221828Sgrehanint vm_run(struct vm *vm, struct vm_run *vmrun); 120221828Sgrehanint vm_inject_event(struct vm *vm, int vcpu, int type, 121221828Sgrehan int vector, uint32_t error_code, int error_code_valid); 122221828Sgrehanint vm_inject_nmi(struct vm *vm, int vcpu); 123241982Sneelint vm_nmi_pending(struct vm *vm, int vcpuid); 124241982Sneelvoid vm_nmi_clear(struct vm *vm, int vcpuid); 125221828Sgrehanuint64_t *vm_guest_msrs(struct vm *vm, int cpu); 126221828Sgrehanstruct vlapic *vm_lapic(struct vm *vm, int cpu); 127258075Sneelstruct vioapic *vm_ioapic(struct vm *vm); 128258579Sneelstruct vhpet *vm_hpet(struct vm *vm); 129221828Sgrehanint vm_get_capability(struct vm *vm, int vcpu, int type, int *val); 130221828Sgrehanint vm_set_capability(struct vm *vm, int vcpu, int type, int val); 131240922Sneelint vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state); 132240922Sneelint vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state); 133258075Sneelint vm_apicid2vcpuid(struct vm *vm, int apicid); 134221828Sgrehanvoid vm_activate_cpu(struct vm *vm, int vcpu); 135223621Sgrehancpuset_t vm_active_cpus(struct vm *vm); 136240894Sneelstruct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid); 137221828Sgrehan 138221828Sgrehan/* 139260619Sneel * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'. 140260619Sneel * The rendezvous 'func(arg)' is not allowed to do anything that will 141260619Sneel * cause the thread to be put to sleep. 142260619Sneel * 143260619Sneel * If the rendezvous is being initiated from a vcpu context then the 144260619Sneel * 'vcpuid' must refer to that vcpu, otherwise it should be set to -1. 145260619Sneel * 146260619Sneel * The caller cannot hold any locks when initiating the rendezvous. 147260619Sneel * 148260619Sneel * The implementation of this API may cause vcpus other than those specified 149260619Sneel * by 'dest' to be stalled. The caller should not rely on any vcpus making 150260619Sneel * forward progress when the rendezvous is in progress. 151260619Sneel */ 152260619Sneeltypedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg); 153260619Sneelvoid vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest, 154260619Sneel vm_rendezvous_func_t func, void *arg); 155260619Sneel 156260619Sneelstatic __inline int 157260619Sneelvcpu_rendezvous_pending(void *rendezvous_cookie) 158260619Sneel{ 159260619Sneel 160260619Sneel return (*(uintptr_t *)rendezvous_cookie != 0); 161260619Sneel} 162260619Sneel 163260619Sneel/* 164221828Sgrehan * Return 1 if device indicated by bus/slot/func is supposed to be a 165221828Sgrehan * pci passthrough device. 166221828Sgrehan * 167221828Sgrehan * Return 0 otherwise. 168221828Sgrehan */ 169221828Sgrehanint vmm_is_pptdev(int bus, int slot, int func); 170221828Sgrehan 171221828Sgrehanvoid *vm_iommu_domain(struct vm *vm); 172221828Sgrehan 173241489Sneelenum vcpu_state { 174241489Sneel VCPU_IDLE, 175256072Sneel VCPU_FROZEN, 176241489Sneel VCPU_RUNNING, 177256072Sneel VCPU_SLEEPING, 178241489Sneel}; 179221828Sgrehan 180259737Sneelint vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state, 181259737Sneel bool from_idle); 182249879Sgrehanenum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu); 183221828Sgrehan 184221828Sgrehanstatic int __inline 185249879Sgrehanvcpu_is_running(struct vm *vm, int vcpu, int *hostcpu) 186221828Sgrehan{ 187249879Sgrehan return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING); 188221828Sgrehan} 189221828Sgrehan 190241489Sneelvoid *vcpu_stats(struct vm *vm, int vcpu); 191259863Sneelvoid vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr); 192256072Sneelstruct vmspace *vm_get_vmspace(struct vm *vm); 193256072Sneelint vm_assign_pptdev(struct vm *vm, int bus, int slot, int func); 194256072Sneelint vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func); 195221828Sgrehan#endif /* KERNEL */ 196221828Sgrehan 197243640Sneel#include <machine/vmm_instruction_emul.h> 198243640Sneel 199255438Sgrehan#define VM_MAXCPU 16 /* maximum virtual cpus */ 200221828Sgrehan 201221828Sgrehan/* 202221828Sgrehan * Identifiers for events that can be injected into the VM 203221828Sgrehan */ 204221828Sgrehanenum vm_event_type { 205221828Sgrehan VM_EVENT_NONE, 206221828Sgrehan VM_HW_INTR, 207221828Sgrehan VM_NMI, 208221828Sgrehan VM_HW_EXCEPTION, 209221828Sgrehan VM_SW_INTR, 210221828Sgrehan VM_PRIV_SW_EXCEPTION, 211221828Sgrehan VM_SW_EXCEPTION, 212221828Sgrehan VM_EVENT_MAX 213221828Sgrehan}; 214221828Sgrehan 215221828Sgrehan/* 216221828Sgrehan * Identifiers for architecturally defined registers. 217221828Sgrehan */ 218221828Sgrehanenum vm_reg_name { 219221828Sgrehan VM_REG_GUEST_RAX, 220221828Sgrehan VM_REG_GUEST_RBX, 221221828Sgrehan VM_REG_GUEST_RCX, 222221828Sgrehan VM_REG_GUEST_RDX, 223221828Sgrehan VM_REG_GUEST_RSI, 224221828Sgrehan VM_REG_GUEST_RDI, 225221828Sgrehan VM_REG_GUEST_RBP, 226221828Sgrehan VM_REG_GUEST_R8, 227221828Sgrehan VM_REG_GUEST_R9, 228221828Sgrehan VM_REG_GUEST_R10, 229221828Sgrehan VM_REG_GUEST_R11, 230221828Sgrehan VM_REG_GUEST_R12, 231221828Sgrehan VM_REG_GUEST_R13, 232221828Sgrehan VM_REG_GUEST_R14, 233221828Sgrehan VM_REG_GUEST_R15, 234221828Sgrehan VM_REG_GUEST_CR0, 235221828Sgrehan VM_REG_GUEST_CR3, 236221828Sgrehan VM_REG_GUEST_CR4, 237221828Sgrehan VM_REG_GUEST_DR7, 238221828Sgrehan VM_REG_GUEST_RSP, 239221828Sgrehan VM_REG_GUEST_RIP, 240221828Sgrehan VM_REG_GUEST_RFLAGS, 241221828Sgrehan VM_REG_GUEST_ES, 242221828Sgrehan VM_REG_GUEST_CS, 243221828Sgrehan VM_REG_GUEST_SS, 244221828Sgrehan VM_REG_GUEST_DS, 245221828Sgrehan VM_REG_GUEST_FS, 246221828Sgrehan VM_REG_GUEST_GS, 247221828Sgrehan VM_REG_GUEST_LDTR, 248221828Sgrehan VM_REG_GUEST_TR, 249221828Sgrehan VM_REG_GUEST_IDTR, 250221828Sgrehan VM_REG_GUEST_GDTR, 251221828Sgrehan VM_REG_GUEST_EFER, 252221828Sgrehan VM_REG_LAST 253221828Sgrehan}; 254221828Sgrehan 255221828Sgrehan/* 256221828Sgrehan * Identifiers for optional vmm capabilities 257221828Sgrehan */ 258221828Sgrehanenum vm_cap_type { 259221828Sgrehan VM_CAP_HALT_EXIT, 260221828Sgrehan VM_CAP_MTRAP_EXIT, 261221828Sgrehan VM_CAP_PAUSE_EXIT, 262221828Sgrehan VM_CAP_UNRESTRICTED_GUEST, 263256645Sneel VM_CAP_ENABLE_INVPCID, 264221828Sgrehan VM_CAP_MAX 265221828Sgrehan}; 266221828Sgrehan 267240922Sneelenum x2apic_state { 268240922Sneel X2APIC_ENABLED, 269240922Sneel X2APIC_AVAILABLE, 270240922Sneel X2APIC_DISABLED, 271240922Sneel X2APIC_STATE_LAST 272240922Sneel}; 273240922Sneel 274221828Sgrehan/* 275221828Sgrehan * The 'access' field has the format specified in Table 21-2 of the Intel 276221828Sgrehan * Architecture Manual vol 3b. 277221828Sgrehan * 278221828Sgrehan * XXX The contents of the 'access' field are architecturally defined except 279221828Sgrehan * bit 16 - Segment Unusable. 280221828Sgrehan */ 281221828Sgrehanstruct seg_desc { 282221828Sgrehan uint64_t base; 283221828Sgrehan uint32_t limit; 284221828Sgrehan uint32_t access; 285221828Sgrehan}; 286221828Sgrehan 287221828Sgrehanenum vm_exitcode { 288221828Sgrehan VM_EXITCODE_INOUT, 289221828Sgrehan VM_EXITCODE_VMX, 290221828Sgrehan VM_EXITCODE_BOGUS, 291221828Sgrehan VM_EXITCODE_RDMSR, 292221828Sgrehan VM_EXITCODE_WRMSR, 293221828Sgrehan VM_EXITCODE_HLT, 294221828Sgrehan VM_EXITCODE_MTRAP, 295221828Sgrehan VM_EXITCODE_PAUSE, 296234761Sgrehan VM_EXITCODE_PAGING, 297256072Sneel VM_EXITCODE_INST_EMUL, 298240912Sneel VM_EXITCODE_SPINUP_AP, 299259081Sneel VM_EXITCODE_SPINDOWN_CPU, 300260619Sneel VM_EXITCODE_RENDEZVOUS, 301234761Sgrehan VM_EXITCODE_MAX 302221828Sgrehan}; 303221828Sgrehan 304221828Sgrehanstruct vm_exit { 305221828Sgrehan enum vm_exitcode exitcode; 306221828Sgrehan int inst_length; /* 0 means unknown */ 307221828Sgrehan uint64_t rip; 308221828Sgrehan union { 309221828Sgrehan struct { 310221828Sgrehan uint16_t bytes:3; /* 1 or 2 or 4 */ 311221828Sgrehan uint16_t in:1; /* out is 0, in is 1 */ 312221828Sgrehan uint16_t string:1; 313221828Sgrehan uint16_t rep:1; 314221828Sgrehan uint16_t port; 315221828Sgrehan uint32_t eax; /* valid for out */ 316221828Sgrehan } inout; 317234761Sgrehan struct { 318241497Sgrehan uint64_t gpa; 319256072Sneel int fault_type; 320256072Sneel } paging; 321256072Sneel struct { 322256072Sneel uint64_t gpa; 323256072Sneel uint64_t gla; 324256072Sneel uint64_t cr3; 325243640Sneel struct vie vie; 326256072Sneel } inst_emul; 327221828Sgrehan /* 328221828Sgrehan * VMX specific payload. Used when there is no "better" 329221828Sgrehan * exitcode to represent the VM-exit. 330221828Sgrehan */ 331221828Sgrehan struct { 332260167Sneel int status; /* vmx inst status */ 333260167Sneel /* 334260167Sneel * 'exit_reason' and 'exit_qualification' are valid 335260167Sneel * only if 'status' is zero. 336260167Sneel */ 337221828Sgrehan uint32_t exit_reason; 338221828Sgrehan uint64_t exit_qualification; 339260167Sneel /* 340260167Sneel * 'inst_error' and 'inst_type' are valid 341260167Sneel * only if 'status' is non-zero. 342260167Sneel */ 343260167Sneel int inst_type; 344260167Sneel int inst_error; 345221828Sgrehan } vmx; 346221828Sgrehan struct { 347221828Sgrehan uint32_t code; /* ecx value */ 348221828Sgrehan uint64_t wval; 349221828Sgrehan } msr; 350240912Sneel struct { 351240912Sneel int vcpu; 352240912Sneel uint64_t rip; 353240912Sneel } spinup_ap; 354259081Sneel struct { 355259081Sneel uint64_t rflags; 356259081Sneel } hlt; 357221828Sgrehan } u; 358221828Sgrehan}; 359221828Sgrehan 360221828Sgrehan#endif /* _VMM_H_ */ 361