vmm.h revision 266125
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 266125 2014-05-15 14:16:55Z jhb $ 27221828Sgrehan */ 28221828Sgrehan 29221828Sgrehan#ifndef _VMM_H_ 30221828Sgrehan#define _VMM_H_ 31221828Sgrehan 32265062Sneelenum vm_suspend_how { 33265062Sneel VM_SUSPEND_NONE, 34265062Sneel VM_SUSPEND_RESET, 35265062Sneel VM_SUSPEND_POWEROFF, 36265203Sneel VM_SUSPEND_HALT, 37265062Sneel VM_SUSPEND_LAST 38265062Sneel}; 39265062Sneel 40221828Sgrehan#ifdef _KERNEL 41221828Sgrehan 42221828Sgrehan#define VM_MAX_NAMELEN 32 43221828Sgrehan 44221828Sgrehanstruct vm; 45262506Sneelstruct vm_exception; 46221828Sgrehanstruct vm_memory_segment; 47221828Sgrehanstruct seg_desc; 48221828Sgrehanstruct vm_exit; 49221828Sgrehanstruct vm_run; 50258579Sneelstruct vhpet; 51258075Sneelstruct vioapic; 52221828Sgrehanstruct vlapic; 53256072Sneelstruct vmspace; 54256072Sneelstruct vm_object; 55256072Sneelstruct pmap; 56221828Sgrehan 57240922Sneelenum x2apic_state; 58240922Sneel 59260466Sneeltypedef int (*vmm_init_func_t)(int ipinum); 60221828Sgrehantypedef int (*vmm_cleanup_func_t)(void); 61259782Sjhbtypedef void (*vmm_resume_func_t)(void); 62256072Sneeltypedef void * (*vmi_init_func_t)(struct vm *vm, struct pmap *pmap); 63256072Sneeltypedef int (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip, 64263780Sneel struct pmap *pmap, void *rendezvous_cookie, 65263780Sneel void *suspend_cookie); 66221828Sgrehantypedef void (*vmi_cleanup_func_t)(void *vmi); 67221828Sgrehantypedef int (*vmi_get_register_t)(void *vmi, int vcpu, int num, 68221828Sgrehan uint64_t *retval); 69221828Sgrehantypedef int (*vmi_set_register_t)(void *vmi, int vcpu, int num, 70221828Sgrehan uint64_t val); 71221828Sgrehantypedef int (*vmi_get_desc_t)(void *vmi, int vcpu, int num, 72221828Sgrehan struct seg_desc *desc); 73221828Sgrehantypedef int (*vmi_set_desc_t)(void *vmi, int vcpu, int num, 74221828Sgrehan struct seg_desc *desc); 75221828Sgrehantypedef int (*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval); 76221828Sgrehantypedef int (*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val); 77256072Sneeltypedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max); 78256072Sneeltypedef void (*vmi_vmspace_free)(struct vmspace *vmspace); 79259863Sneeltypedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu); 80259863Sneeltypedef void (*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic); 81221828Sgrehan 82221828Sgrehanstruct vmm_ops { 83221828Sgrehan vmm_init_func_t init; /* module wide initialization */ 84221828Sgrehan vmm_cleanup_func_t cleanup; 85259782Sjhb vmm_resume_func_t resume; 86221828Sgrehan 87221828Sgrehan vmi_init_func_t vminit; /* vm-specific initialization */ 88221828Sgrehan vmi_run_func_t vmrun; 89221828Sgrehan vmi_cleanup_func_t vmcleanup; 90221828Sgrehan vmi_get_register_t vmgetreg; 91221828Sgrehan vmi_set_register_t vmsetreg; 92221828Sgrehan vmi_get_desc_t vmgetdesc; 93221828Sgrehan vmi_set_desc_t vmsetdesc; 94221828Sgrehan vmi_get_cap_t vmgetcap; 95221828Sgrehan vmi_set_cap_t vmsetcap; 96256072Sneel vmi_vmspace_alloc vmspace_alloc; 97256072Sneel vmi_vmspace_free vmspace_free; 98259863Sneel vmi_vlapic_init vlapic_init; 99259863Sneel vmi_vlapic_cleanup vlapic_cleanup; 100221828Sgrehan}; 101221828Sgrehan 102221828Sgrehanextern struct vmm_ops vmm_ops_intel; 103221828Sgrehanextern struct vmm_ops vmm_ops_amd; 104221828Sgrehan 105249396Sneelint vm_create(const char *name, struct vm **retvm); 106221828Sgrehanvoid vm_destroy(struct vm *vm); 107221828Sgrehanconst char *vm_name(struct vm *vm); 108241041Sneelint vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len); 109221828Sgrehanint vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa); 110221828Sgrehanint vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len); 111256072Sneelvoid *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot, 112256072Sneel void **cookie); 113256072Sneelvoid vm_gpa_release(void *cookie); 114221828Sgrehanint vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, 115221828Sgrehan struct vm_memory_segment *seg); 116256072Sneelint vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len, 117256072Sneel vm_offset_t *offset, struct vm_object **object); 118256072Sneelboolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa); 119221828Sgrehanint vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval); 120221828Sgrehanint vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val); 121221828Sgrehanint vm_get_seg_desc(struct vm *vm, int vcpu, int reg, 122221828Sgrehan struct seg_desc *ret_desc); 123221828Sgrehanint vm_set_seg_desc(struct vm *vm, int vcpu, int reg, 124221828Sgrehan struct seg_desc *desc); 125221828Sgrehanint vm_run(struct vm *vm, struct vm_run *vmrun); 126265062Sneelint vm_suspend(struct vm *vm, enum vm_suspend_how how); 127221828Sgrehanint vm_inject_nmi(struct vm *vm, int vcpu); 128241982Sneelint vm_nmi_pending(struct vm *vm, int vcpuid); 129241982Sneelvoid vm_nmi_clear(struct vm *vm, int vcpuid); 130263211Stychonint vm_inject_extint(struct vm *vm, int vcpu); 131263211Stychonint vm_extint_pending(struct vm *vm, int vcpuid); 132263211Stychonvoid vm_extint_clear(struct vm *vm, int vcpuid); 133221828Sgrehanuint64_t *vm_guest_msrs(struct vm *vm, int cpu); 134221828Sgrehanstruct vlapic *vm_lapic(struct vm *vm, int cpu); 135258075Sneelstruct vioapic *vm_ioapic(struct vm *vm); 136258579Sneelstruct vhpet *vm_hpet(struct vm *vm); 137221828Sgrehanint vm_get_capability(struct vm *vm, int vcpu, int type, int *val); 138221828Sgrehanint vm_set_capability(struct vm *vm, int vcpu, int type, int val); 139240922Sneelint vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state); 140240922Sneelint vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state); 141258075Sneelint vm_apicid2vcpuid(struct vm *vm, int apicid); 142221828Sgrehanvoid vm_activate_cpu(struct vm *vm, int vcpu); 143223621Sgrehancpuset_t vm_active_cpus(struct vm *vm); 144240894Sneelstruct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid); 145265062Sneelvoid vm_exit_suspended(struct vm *vm, int vcpuid, uint64_t rip); 146221828Sgrehan 147221828Sgrehan/* 148260619Sneel * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'. 149260619Sneel * The rendezvous 'func(arg)' is not allowed to do anything that will 150260619Sneel * cause the thread to be put to sleep. 151260619Sneel * 152260619Sneel * If the rendezvous is being initiated from a vcpu context then the 153260619Sneel * 'vcpuid' must refer to that vcpu, otherwise it should be set to -1. 154260619Sneel * 155260619Sneel * The caller cannot hold any locks when initiating the rendezvous. 156260619Sneel * 157260619Sneel * The implementation of this API may cause vcpus other than those specified 158260619Sneel * by 'dest' to be stalled. The caller should not rely on any vcpus making 159260619Sneel * forward progress when the rendezvous is in progress. 160260619Sneel */ 161260619Sneeltypedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg); 162260619Sneelvoid vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest, 163260619Sneel vm_rendezvous_func_t func, void *arg); 164260619Sneel 165260619Sneelstatic __inline int 166260619Sneelvcpu_rendezvous_pending(void *rendezvous_cookie) 167260619Sneel{ 168260619Sneel 169260619Sneel return (*(uintptr_t *)rendezvous_cookie != 0); 170260619Sneel} 171260619Sneel 172263780Sneelstatic __inline int 173263780Sneelvcpu_suspended(void *suspend_cookie) 174263780Sneel{ 175263780Sneel 176263780Sneel return (*(int *)suspend_cookie); 177263780Sneel} 178263780Sneel 179260619Sneel/* 180221828Sgrehan * Return 1 if device indicated by bus/slot/func is supposed to be a 181221828Sgrehan * pci passthrough device. 182221828Sgrehan * 183221828Sgrehan * Return 0 otherwise. 184221828Sgrehan */ 185221828Sgrehanint vmm_is_pptdev(int bus, int slot, int func); 186221828Sgrehan 187221828Sgrehanvoid *vm_iommu_domain(struct vm *vm); 188221828Sgrehan 189241489Sneelenum vcpu_state { 190241489Sneel VCPU_IDLE, 191256072Sneel VCPU_FROZEN, 192241489Sneel VCPU_RUNNING, 193256072Sneel VCPU_SLEEPING, 194241489Sneel}; 195221828Sgrehan 196259737Sneelint vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state, 197259737Sneel bool from_idle); 198249879Sgrehanenum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu); 199221828Sgrehan 200221828Sgrehanstatic int __inline 201249879Sgrehanvcpu_is_running(struct vm *vm, int vcpu, int *hostcpu) 202221828Sgrehan{ 203249879Sgrehan return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING); 204221828Sgrehan} 205221828Sgrehan 206241489Sneelvoid *vcpu_stats(struct vm *vm, int vcpu); 207259863Sneelvoid vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr); 208256072Sneelstruct vmspace *vm_get_vmspace(struct vm *vm); 209256072Sneelint vm_assign_pptdev(struct vm *vm, int bus, int slot, int func); 210256072Sneelint vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func); 211263035Stychonstruct vatpic *vm_atpic(struct vm *vm); 212263744Stychonstruct vatpit *vm_atpit(struct vm *vm); 213262506Sneel 214262506Sneel/* 215262506Sneel * Inject exception 'vme' into the guest vcpu. This function returns 0 on 216262506Sneel * success and non-zero on failure. 217262506Sneel * 218262506Sneel * Wrapper functions like 'vm_inject_gp()' should be preferred to calling 219262506Sneel * this function directly because they enforce the trap-like or fault-like 220262506Sneel * behavior of an exception. 221262506Sneel * 222262506Sneel * This function should only be called in the context of the thread that is 223262506Sneel * executing this vcpu. 224262506Sneel */ 225262506Sneelint vm_inject_exception(struct vm *vm, int vcpuid, struct vm_exception *vme); 226262506Sneel 227262506Sneel/* 228262506Sneel * Returns 0 if there is no exception pending for this vcpu. Returns 1 if an 229262506Sneel * exception is pending and also updates 'vme'. The pending exception is 230262506Sneel * cleared when this function returns. 231262506Sneel * 232262506Sneel * This function should only be called in the context of the thread that is 233262506Sneel * executing this vcpu. 234262506Sneel */ 235262506Sneelint vm_exception_pending(struct vm *vm, int vcpuid, struct vm_exception *vme); 236262506Sneel 237262506Sneelvoid vm_inject_gp(struct vm *vm, int vcpuid); /* general protection fault */ 238262506Sneelvoid vm_inject_ud(struct vm *vm, int vcpuid); /* undefined instruction fault */ 239262506Sneel 240221828Sgrehan#endif /* KERNEL */ 241221828Sgrehan 242243640Sneel#include <machine/vmm_instruction_emul.h> 243243640Sneel 244255438Sgrehan#define VM_MAXCPU 16 /* maximum virtual cpus */ 245221828Sgrehan 246221828Sgrehan/* 247221828Sgrehan * Identifiers for architecturally defined registers. 248221828Sgrehan */ 249221828Sgrehanenum vm_reg_name { 250221828Sgrehan VM_REG_GUEST_RAX, 251221828Sgrehan VM_REG_GUEST_RBX, 252221828Sgrehan VM_REG_GUEST_RCX, 253221828Sgrehan VM_REG_GUEST_RDX, 254221828Sgrehan VM_REG_GUEST_RSI, 255221828Sgrehan VM_REG_GUEST_RDI, 256221828Sgrehan VM_REG_GUEST_RBP, 257221828Sgrehan VM_REG_GUEST_R8, 258221828Sgrehan VM_REG_GUEST_R9, 259221828Sgrehan VM_REG_GUEST_R10, 260221828Sgrehan VM_REG_GUEST_R11, 261221828Sgrehan VM_REG_GUEST_R12, 262221828Sgrehan VM_REG_GUEST_R13, 263221828Sgrehan VM_REG_GUEST_R14, 264221828Sgrehan VM_REG_GUEST_R15, 265221828Sgrehan VM_REG_GUEST_CR0, 266221828Sgrehan VM_REG_GUEST_CR3, 267221828Sgrehan VM_REG_GUEST_CR4, 268221828Sgrehan VM_REG_GUEST_DR7, 269221828Sgrehan VM_REG_GUEST_RSP, 270221828Sgrehan VM_REG_GUEST_RIP, 271221828Sgrehan VM_REG_GUEST_RFLAGS, 272221828Sgrehan VM_REG_GUEST_ES, 273221828Sgrehan VM_REG_GUEST_CS, 274221828Sgrehan VM_REG_GUEST_SS, 275221828Sgrehan VM_REG_GUEST_DS, 276221828Sgrehan VM_REG_GUEST_FS, 277221828Sgrehan VM_REG_GUEST_GS, 278221828Sgrehan VM_REG_GUEST_LDTR, 279221828Sgrehan VM_REG_GUEST_TR, 280221828Sgrehan VM_REG_GUEST_IDTR, 281221828Sgrehan VM_REG_GUEST_GDTR, 282221828Sgrehan VM_REG_GUEST_EFER, 283221828Sgrehan VM_REG_LAST 284221828Sgrehan}; 285221828Sgrehan 286221828Sgrehan/* 287221828Sgrehan * Identifiers for optional vmm capabilities 288221828Sgrehan */ 289221828Sgrehanenum vm_cap_type { 290221828Sgrehan VM_CAP_HALT_EXIT, 291221828Sgrehan VM_CAP_MTRAP_EXIT, 292221828Sgrehan VM_CAP_PAUSE_EXIT, 293221828Sgrehan VM_CAP_UNRESTRICTED_GUEST, 294256645Sneel VM_CAP_ENABLE_INVPCID, 295221828Sgrehan VM_CAP_MAX 296221828Sgrehan}; 297221828Sgrehan 298240922Sneelenum x2apic_state { 299262236Sneel X2APIC_DISABLED, 300240922Sneel X2APIC_ENABLED, 301240922Sneel X2APIC_STATE_LAST 302240922Sneel}; 303240922Sneel 304266125Sjhbenum vm_intr_trigger { 305266125Sjhb EDGE_TRIGGER, 306266125Sjhb LEVEL_TRIGGER 307266125Sjhb}; 308266125Sjhb 309221828Sgrehan/* 310221828Sgrehan * The 'access' field has the format specified in Table 21-2 of the Intel 311221828Sgrehan * Architecture Manual vol 3b. 312221828Sgrehan * 313221828Sgrehan * XXX The contents of the 'access' field are architecturally defined except 314221828Sgrehan * bit 16 - Segment Unusable. 315221828Sgrehan */ 316221828Sgrehanstruct seg_desc { 317221828Sgrehan uint64_t base; 318221828Sgrehan uint32_t limit; 319221828Sgrehan uint32_t access; 320221828Sgrehan}; 321221828Sgrehan 322221828Sgrehanenum vm_exitcode { 323221828Sgrehan VM_EXITCODE_INOUT, 324221828Sgrehan VM_EXITCODE_VMX, 325221828Sgrehan VM_EXITCODE_BOGUS, 326221828Sgrehan VM_EXITCODE_RDMSR, 327221828Sgrehan VM_EXITCODE_WRMSR, 328221828Sgrehan VM_EXITCODE_HLT, 329221828Sgrehan VM_EXITCODE_MTRAP, 330221828Sgrehan VM_EXITCODE_PAUSE, 331234761Sgrehan VM_EXITCODE_PAGING, 332256072Sneel VM_EXITCODE_INST_EMUL, 333240912Sneel VM_EXITCODE_SPINUP_AP, 334265101Sneel VM_EXITCODE_DEPRECATED1, /* used to be SPINDOWN_CPU */ 335260619Sneel VM_EXITCODE_RENDEZVOUS, 336261170Sneel VM_EXITCODE_IOAPIC_EOI, 337263780Sneel VM_EXITCODE_SUSPENDED, 338234761Sgrehan VM_EXITCODE_MAX 339221828Sgrehan}; 340221828Sgrehan 341221828Sgrehanstruct vm_exit { 342221828Sgrehan enum vm_exitcode exitcode; 343221828Sgrehan int inst_length; /* 0 means unknown */ 344221828Sgrehan uint64_t rip; 345221828Sgrehan union { 346221828Sgrehan struct { 347221828Sgrehan uint16_t bytes:3; /* 1 or 2 or 4 */ 348221828Sgrehan uint16_t in:1; /* out is 0, in is 1 */ 349221828Sgrehan uint16_t string:1; 350221828Sgrehan uint16_t rep:1; 351221828Sgrehan uint16_t port; 352221828Sgrehan uint32_t eax; /* valid for out */ 353221828Sgrehan } inout; 354234761Sgrehan struct { 355241497Sgrehan uint64_t gpa; 356256072Sneel int fault_type; 357256072Sneel } paging; 358256072Sneel struct { 359256072Sneel uint64_t gpa; 360256072Sneel uint64_t gla; 361256072Sneel uint64_t cr3; 362261504Sjhb enum vie_cpu_mode cpu_mode; 363261504Sjhb enum vie_paging_mode paging_mode; 364243640Sneel struct vie vie; 365256072Sneel } inst_emul; 366221828Sgrehan /* 367221828Sgrehan * VMX specific payload. Used when there is no "better" 368221828Sgrehan * exitcode to represent the VM-exit. 369221828Sgrehan */ 370221828Sgrehan struct { 371260167Sneel int status; /* vmx inst status */ 372260167Sneel /* 373260167Sneel * 'exit_reason' and 'exit_qualification' are valid 374260167Sneel * only if 'status' is zero. 375260167Sneel */ 376221828Sgrehan uint32_t exit_reason; 377221828Sgrehan uint64_t exit_qualification; 378260167Sneel /* 379260167Sneel * 'inst_error' and 'inst_type' are valid 380260167Sneel * only if 'status' is non-zero. 381260167Sneel */ 382260167Sneel int inst_type; 383260167Sneel int inst_error; 384221828Sgrehan } vmx; 385221828Sgrehan struct { 386221828Sgrehan uint32_t code; /* ecx value */ 387221828Sgrehan uint64_t wval; 388221828Sgrehan } msr; 389240912Sneel struct { 390240912Sneel int vcpu; 391240912Sneel uint64_t rip; 392240912Sneel } spinup_ap; 393259081Sneel struct { 394259081Sneel uint64_t rflags; 395259081Sneel } hlt; 396261170Sneel struct { 397261170Sneel int vector; 398261170Sneel } ioapic_eoi; 399265062Sneel struct { 400265062Sneel enum vm_suspend_how how; 401265062Sneel } suspended; 402221828Sgrehan } u; 403221828Sgrehan}; 404221828Sgrehan 405221828Sgrehan#endif /* _VMM_H_ */ 406