vmm.h revision 263780
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 263780 2014-03-26 23:34:27Z 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; 37262506Sneelstruct vm_exception; 38221828Sgrehanstruct vm_memory_segment; 39221828Sgrehanstruct seg_desc; 40221828Sgrehanstruct vm_exit; 41221828Sgrehanstruct vm_run; 42258579Sneelstruct vhpet; 43258075Sneelstruct vioapic; 44221828Sgrehanstruct vlapic; 45256072Sneelstruct vmspace; 46256072Sneelstruct vm_object; 47256072Sneelstruct pmap; 48221828Sgrehan 49240922Sneelenum x2apic_state; 50240922Sneel 51260466Sneeltypedef int (*vmm_init_func_t)(int ipinum); 52221828Sgrehantypedef int (*vmm_cleanup_func_t)(void); 53259782Sjhbtypedef void (*vmm_resume_func_t)(void); 54256072Sneeltypedef void * (*vmi_init_func_t)(struct vm *vm, struct pmap *pmap); 55256072Sneeltypedef int (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip, 56263780Sneel struct pmap *pmap, void *rendezvous_cookie, 57263780Sneel void *suspend_cookie); 58221828Sgrehantypedef void (*vmi_cleanup_func_t)(void *vmi); 59221828Sgrehantypedef int (*vmi_get_register_t)(void *vmi, int vcpu, int num, 60221828Sgrehan uint64_t *retval); 61221828Sgrehantypedef int (*vmi_set_register_t)(void *vmi, int vcpu, int num, 62221828Sgrehan uint64_t val); 63221828Sgrehantypedef int (*vmi_get_desc_t)(void *vmi, int vcpu, int num, 64221828Sgrehan struct seg_desc *desc); 65221828Sgrehantypedef int (*vmi_set_desc_t)(void *vmi, int vcpu, int num, 66221828Sgrehan struct seg_desc *desc); 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); 71259863Sneeltypedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu); 72259863Sneeltypedef void (*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic); 73221828Sgrehan 74221828Sgrehanstruct vmm_ops { 75221828Sgrehan vmm_init_func_t init; /* module wide initialization */ 76221828Sgrehan vmm_cleanup_func_t cleanup; 77259782Sjhb vmm_resume_func_t resume; 78221828Sgrehan 79221828Sgrehan vmi_init_func_t vminit; /* vm-specific initialization */ 80221828Sgrehan vmi_run_func_t vmrun; 81221828Sgrehan vmi_cleanup_func_t vmcleanup; 82221828Sgrehan vmi_get_register_t vmgetreg; 83221828Sgrehan vmi_set_register_t vmsetreg; 84221828Sgrehan vmi_get_desc_t vmgetdesc; 85221828Sgrehan vmi_set_desc_t vmsetdesc; 86221828Sgrehan vmi_get_cap_t vmgetcap; 87221828Sgrehan vmi_set_cap_t vmsetcap; 88256072Sneel vmi_vmspace_alloc vmspace_alloc; 89256072Sneel vmi_vmspace_free vmspace_free; 90259863Sneel vmi_vlapic_init vlapic_init; 91259863Sneel vmi_vlapic_cleanup vlapic_cleanup; 92221828Sgrehan}; 93221828Sgrehan 94221828Sgrehanextern struct vmm_ops vmm_ops_intel; 95221828Sgrehanextern struct vmm_ops vmm_ops_amd; 96221828Sgrehan 97249396Sneelint vm_create(const char *name, struct vm **retvm); 98221828Sgrehanvoid vm_destroy(struct vm *vm); 99221828Sgrehanconst char *vm_name(struct vm *vm); 100241041Sneelint vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len); 101221828Sgrehanint vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa); 102221828Sgrehanint vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len); 103256072Sneelvoid *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot, 104256072Sneel void **cookie); 105256072Sneelvoid vm_gpa_release(void *cookie); 106221828Sgrehanint vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, 107221828Sgrehan struct vm_memory_segment *seg); 108256072Sneelint vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len, 109256072Sneel vm_offset_t *offset, struct vm_object **object); 110256072Sneelboolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa); 111221828Sgrehanint vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval); 112221828Sgrehanint vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val); 113221828Sgrehanint vm_get_seg_desc(struct vm *vm, int vcpu, int reg, 114221828Sgrehan struct seg_desc *ret_desc); 115221828Sgrehanint vm_set_seg_desc(struct vm *vm, int vcpu, int reg, 116221828Sgrehan struct seg_desc *desc); 117221828Sgrehanint vm_run(struct vm *vm, struct vm_run *vmrun); 118263780Sneelint vm_suspend(struct vm *vm); 119221828Sgrehanint vm_inject_nmi(struct vm *vm, int vcpu); 120241982Sneelint vm_nmi_pending(struct vm *vm, int vcpuid); 121241982Sneelvoid vm_nmi_clear(struct vm *vm, int vcpuid); 122263211Stychonint vm_inject_extint(struct vm *vm, int vcpu); 123263211Stychonint vm_extint_pending(struct vm *vm, int vcpuid); 124263211Stychonvoid vm_extint_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 163263780Sneelstatic __inline int 164263780Sneelvcpu_suspended(void *suspend_cookie) 165263780Sneel{ 166263780Sneel 167263780Sneel return (*(int *)suspend_cookie); 168263780Sneel} 169263780Sneel 170260619Sneel/* 171221828Sgrehan * Return 1 if device indicated by bus/slot/func is supposed to be a 172221828Sgrehan * pci passthrough device. 173221828Sgrehan * 174221828Sgrehan * Return 0 otherwise. 175221828Sgrehan */ 176221828Sgrehanint vmm_is_pptdev(int bus, int slot, int func); 177221828Sgrehan 178221828Sgrehanvoid *vm_iommu_domain(struct vm *vm); 179221828Sgrehan 180241489Sneelenum vcpu_state { 181241489Sneel VCPU_IDLE, 182256072Sneel VCPU_FROZEN, 183241489Sneel VCPU_RUNNING, 184256072Sneel VCPU_SLEEPING, 185241489Sneel}; 186221828Sgrehan 187259737Sneelint vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state, 188259737Sneel bool from_idle); 189249879Sgrehanenum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu); 190221828Sgrehan 191221828Sgrehanstatic int __inline 192249879Sgrehanvcpu_is_running(struct vm *vm, int vcpu, int *hostcpu) 193221828Sgrehan{ 194249879Sgrehan return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING); 195221828Sgrehan} 196221828Sgrehan 197241489Sneelvoid *vcpu_stats(struct vm *vm, int vcpu); 198259863Sneelvoid vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr); 199256072Sneelstruct vmspace *vm_get_vmspace(struct vm *vm); 200256072Sneelint vm_assign_pptdev(struct vm *vm, int bus, int slot, int func); 201256072Sneelint vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func); 202263035Stychonstruct vatpic *vm_atpic(struct vm *vm); 203263744Stychonstruct vatpit *vm_atpit(struct vm *vm); 204262506Sneel 205262506Sneel/* 206262506Sneel * Inject exception 'vme' into the guest vcpu. This function returns 0 on 207262506Sneel * success and non-zero on failure. 208262506Sneel * 209262506Sneel * Wrapper functions like 'vm_inject_gp()' should be preferred to calling 210262506Sneel * this function directly because they enforce the trap-like or fault-like 211262506Sneel * behavior of an exception. 212262506Sneel * 213262506Sneel * This function should only be called in the context of the thread that is 214262506Sneel * executing this vcpu. 215262506Sneel */ 216262506Sneelint vm_inject_exception(struct vm *vm, int vcpuid, struct vm_exception *vme); 217262506Sneel 218262506Sneel/* 219262506Sneel * Returns 0 if there is no exception pending for this vcpu. Returns 1 if an 220262506Sneel * exception is pending and also updates 'vme'. The pending exception is 221262506Sneel * cleared when this function returns. 222262506Sneel * 223262506Sneel * This function should only be called in the context of the thread that is 224262506Sneel * executing this vcpu. 225262506Sneel */ 226262506Sneelint vm_exception_pending(struct vm *vm, int vcpuid, struct vm_exception *vme); 227262506Sneel 228262506Sneelvoid vm_inject_gp(struct vm *vm, int vcpuid); /* general protection fault */ 229262506Sneelvoid vm_inject_ud(struct vm *vm, int vcpuid); /* undefined instruction fault */ 230262506Sneel 231221828Sgrehan#endif /* KERNEL */ 232221828Sgrehan 233243640Sneel#include <machine/vmm_instruction_emul.h> 234243640Sneel 235255438Sgrehan#define VM_MAXCPU 16 /* maximum virtual cpus */ 236221828Sgrehan 237221828Sgrehan/* 238221828Sgrehan * Identifiers for architecturally defined registers. 239221828Sgrehan */ 240221828Sgrehanenum vm_reg_name { 241221828Sgrehan VM_REG_GUEST_RAX, 242221828Sgrehan VM_REG_GUEST_RBX, 243221828Sgrehan VM_REG_GUEST_RCX, 244221828Sgrehan VM_REG_GUEST_RDX, 245221828Sgrehan VM_REG_GUEST_RSI, 246221828Sgrehan VM_REG_GUEST_RDI, 247221828Sgrehan VM_REG_GUEST_RBP, 248221828Sgrehan VM_REG_GUEST_R8, 249221828Sgrehan VM_REG_GUEST_R9, 250221828Sgrehan VM_REG_GUEST_R10, 251221828Sgrehan VM_REG_GUEST_R11, 252221828Sgrehan VM_REG_GUEST_R12, 253221828Sgrehan VM_REG_GUEST_R13, 254221828Sgrehan VM_REG_GUEST_R14, 255221828Sgrehan VM_REG_GUEST_R15, 256221828Sgrehan VM_REG_GUEST_CR0, 257221828Sgrehan VM_REG_GUEST_CR3, 258221828Sgrehan VM_REG_GUEST_CR4, 259221828Sgrehan VM_REG_GUEST_DR7, 260221828Sgrehan VM_REG_GUEST_RSP, 261221828Sgrehan VM_REG_GUEST_RIP, 262221828Sgrehan VM_REG_GUEST_RFLAGS, 263221828Sgrehan VM_REG_GUEST_ES, 264221828Sgrehan VM_REG_GUEST_CS, 265221828Sgrehan VM_REG_GUEST_SS, 266221828Sgrehan VM_REG_GUEST_DS, 267221828Sgrehan VM_REG_GUEST_FS, 268221828Sgrehan VM_REG_GUEST_GS, 269221828Sgrehan VM_REG_GUEST_LDTR, 270221828Sgrehan VM_REG_GUEST_TR, 271221828Sgrehan VM_REG_GUEST_IDTR, 272221828Sgrehan VM_REG_GUEST_GDTR, 273221828Sgrehan VM_REG_GUEST_EFER, 274221828Sgrehan VM_REG_LAST 275221828Sgrehan}; 276221828Sgrehan 277221828Sgrehan/* 278221828Sgrehan * Identifiers for optional vmm capabilities 279221828Sgrehan */ 280221828Sgrehanenum vm_cap_type { 281221828Sgrehan VM_CAP_HALT_EXIT, 282221828Sgrehan VM_CAP_MTRAP_EXIT, 283221828Sgrehan VM_CAP_PAUSE_EXIT, 284221828Sgrehan VM_CAP_UNRESTRICTED_GUEST, 285256645Sneel VM_CAP_ENABLE_INVPCID, 286221828Sgrehan VM_CAP_MAX 287221828Sgrehan}; 288221828Sgrehan 289240922Sneelenum x2apic_state { 290262236Sneel X2APIC_DISABLED, 291240922Sneel X2APIC_ENABLED, 292240922Sneel X2APIC_STATE_LAST 293240922Sneel}; 294240922Sneel 295221828Sgrehan/* 296221828Sgrehan * The 'access' field has the format specified in Table 21-2 of the Intel 297221828Sgrehan * Architecture Manual vol 3b. 298221828Sgrehan * 299221828Sgrehan * XXX The contents of the 'access' field are architecturally defined except 300221828Sgrehan * bit 16 - Segment Unusable. 301221828Sgrehan */ 302221828Sgrehanstruct seg_desc { 303221828Sgrehan uint64_t base; 304221828Sgrehan uint32_t limit; 305221828Sgrehan uint32_t access; 306221828Sgrehan}; 307221828Sgrehan 308221828Sgrehanenum vm_exitcode { 309221828Sgrehan VM_EXITCODE_INOUT, 310221828Sgrehan VM_EXITCODE_VMX, 311221828Sgrehan VM_EXITCODE_BOGUS, 312221828Sgrehan VM_EXITCODE_RDMSR, 313221828Sgrehan VM_EXITCODE_WRMSR, 314221828Sgrehan VM_EXITCODE_HLT, 315221828Sgrehan VM_EXITCODE_MTRAP, 316221828Sgrehan VM_EXITCODE_PAUSE, 317234761Sgrehan VM_EXITCODE_PAGING, 318256072Sneel VM_EXITCODE_INST_EMUL, 319240912Sneel VM_EXITCODE_SPINUP_AP, 320259081Sneel VM_EXITCODE_SPINDOWN_CPU, 321260619Sneel VM_EXITCODE_RENDEZVOUS, 322261170Sneel VM_EXITCODE_IOAPIC_EOI, 323263780Sneel VM_EXITCODE_SUSPENDED, 324234761Sgrehan VM_EXITCODE_MAX 325221828Sgrehan}; 326221828Sgrehan 327221828Sgrehanstruct vm_exit { 328221828Sgrehan enum vm_exitcode exitcode; 329221828Sgrehan int inst_length; /* 0 means unknown */ 330221828Sgrehan uint64_t rip; 331221828Sgrehan union { 332221828Sgrehan struct { 333221828Sgrehan uint16_t bytes:3; /* 1 or 2 or 4 */ 334221828Sgrehan uint16_t in:1; /* out is 0, in is 1 */ 335221828Sgrehan uint16_t string:1; 336221828Sgrehan uint16_t rep:1; 337221828Sgrehan uint16_t port; 338221828Sgrehan uint32_t eax; /* valid for out */ 339221828Sgrehan } inout; 340234761Sgrehan struct { 341241497Sgrehan uint64_t gpa; 342256072Sneel int fault_type; 343256072Sneel } paging; 344256072Sneel struct { 345256072Sneel uint64_t gpa; 346256072Sneel uint64_t gla; 347256072Sneel uint64_t cr3; 348261504Sjhb enum vie_cpu_mode cpu_mode; 349261504Sjhb enum vie_paging_mode paging_mode; 350243640Sneel struct vie vie; 351256072Sneel } inst_emul; 352221828Sgrehan /* 353221828Sgrehan * VMX specific payload. Used when there is no "better" 354221828Sgrehan * exitcode to represent the VM-exit. 355221828Sgrehan */ 356221828Sgrehan struct { 357260167Sneel int status; /* vmx inst status */ 358260167Sneel /* 359260167Sneel * 'exit_reason' and 'exit_qualification' are valid 360260167Sneel * only if 'status' is zero. 361260167Sneel */ 362221828Sgrehan uint32_t exit_reason; 363221828Sgrehan uint64_t exit_qualification; 364260167Sneel /* 365260167Sneel * 'inst_error' and 'inst_type' are valid 366260167Sneel * only if 'status' is non-zero. 367260167Sneel */ 368260167Sneel int inst_type; 369260167Sneel int inst_error; 370221828Sgrehan } vmx; 371221828Sgrehan struct { 372221828Sgrehan uint32_t code; /* ecx value */ 373221828Sgrehan uint64_t wval; 374221828Sgrehan } msr; 375240912Sneel struct { 376240912Sneel int vcpu; 377240912Sneel uint64_t rip; 378240912Sneel } spinup_ap; 379259081Sneel struct { 380259081Sneel uint64_t rflags; 381259081Sneel } hlt; 382261170Sneel struct { 383261170Sneel int vector; 384261170Sneel } ioapic_eoi; 385221828Sgrehan } u; 386221828Sgrehan}; 387221828Sgrehan 388221828Sgrehan#endif /* _VMM_H_ */ 389