vmm.h revision 271451
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 271451 2014-09-12 06:15:20Z neel $ 27221828Sgrehan */ 28221828Sgrehan 29221828Sgrehan#ifndef _VMM_H_ 30221828Sgrehan#define _VMM_H_ 31221828Sgrehan 32269042Sneel#include <x86/segments.h> 33269042Sneel 34265062Sneelenum vm_suspend_how { 35265062Sneel VM_SUSPEND_NONE, 36265062Sneel VM_SUSPEND_RESET, 37265062Sneel VM_SUSPEND_POWEROFF, 38265203Sneel VM_SUSPEND_HALT, 39268889Sneel VM_SUSPEND_TRIPLEFAULT, 40265062Sneel VM_SUSPEND_LAST 41265062Sneel}; 42265062Sneel 43267338Stychon/* 44267338Stychon * Identifiers for architecturally defined registers. 45267338Stychon */ 46267338Stychonenum vm_reg_name { 47267338Stychon VM_REG_GUEST_RAX, 48267338Stychon VM_REG_GUEST_RBX, 49267338Stychon VM_REG_GUEST_RCX, 50267338Stychon VM_REG_GUEST_RDX, 51267338Stychon VM_REG_GUEST_RSI, 52267338Stychon VM_REG_GUEST_RDI, 53267338Stychon VM_REG_GUEST_RBP, 54267338Stychon VM_REG_GUEST_R8, 55267338Stychon VM_REG_GUEST_R9, 56267338Stychon VM_REG_GUEST_R10, 57267338Stychon VM_REG_GUEST_R11, 58267338Stychon VM_REG_GUEST_R12, 59267338Stychon VM_REG_GUEST_R13, 60267338Stychon VM_REG_GUEST_R14, 61267338Stychon VM_REG_GUEST_R15, 62267338Stychon VM_REG_GUEST_CR0, 63267338Stychon VM_REG_GUEST_CR3, 64267338Stychon VM_REG_GUEST_CR4, 65267338Stychon VM_REG_GUEST_DR7, 66267338Stychon VM_REG_GUEST_RSP, 67267338Stychon VM_REG_GUEST_RIP, 68267338Stychon VM_REG_GUEST_RFLAGS, 69267338Stychon VM_REG_GUEST_ES, 70267338Stychon VM_REG_GUEST_CS, 71267338Stychon VM_REG_GUEST_SS, 72267338Stychon VM_REG_GUEST_DS, 73267338Stychon VM_REG_GUEST_FS, 74267338Stychon VM_REG_GUEST_GS, 75267338Stychon VM_REG_GUEST_LDTR, 76267338Stychon VM_REG_GUEST_TR, 77267338Stychon VM_REG_GUEST_IDTR, 78267338Stychon VM_REG_GUEST_GDTR, 79267338Stychon VM_REG_GUEST_EFER, 80267338Stychon VM_REG_GUEST_CR2, 81268777Sneel VM_REG_GUEST_PDPTE0, 82268777Sneel VM_REG_GUEST_PDPTE1, 83268777Sneel VM_REG_GUEST_PDPTE2, 84268777Sneel VM_REG_GUEST_PDPTE3, 85271451Sneel VM_REG_GUEST_INTR_SHADOW, 86267338Stychon VM_REG_LAST 87267338Stychon}; 88267338Stychon 89267338Stychonenum x2apic_state { 90267338Stychon X2APIC_DISABLED, 91267338Stychon X2APIC_ENABLED, 92267338Stychon X2APIC_STATE_LAST 93267338Stychon}; 94267338Stychon 95268889Sneel#define VM_INTINFO_VECTOR(info) ((info) & 0xff) 96268889Sneel#define VM_INTINFO_DEL_ERRCODE 0x800 97268889Sneel#define VM_INTINFO_RSVD 0x7ffff000 98268889Sneel#define VM_INTINFO_VALID 0x80000000 99268889Sneel#define VM_INTINFO_TYPE 0x700 100268889Sneel#define VM_INTINFO_HWINTR (0 << 8) 101268889Sneel#define VM_INTINFO_NMI (2 << 8) 102268889Sneel#define VM_INTINFO_HWEXCEPTION (3 << 8) 103268889Sneel#define VM_INTINFO_SWINTR (4 << 8) 104268889Sneel 105221828Sgrehan#ifdef _KERNEL 106221828Sgrehan 107221828Sgrehan#define VM_MAX_NAMELEN 32 108221828Sgrehan 109221828Sgrehanstruct vm; 110262506Sneelstruct vm_exception; 111221828Sgrehanstruct vm_memory_segment; 112221828Sgrehanstruct seg_desc; 113221828Sgrehanstruct vm_exit; 114221828Sgrehanstruct vm_run; 115258579Sneelstruct vhpet; 116258075Sneelstruct vioapic; 117221828Sgrehanstruct vlapic; 118256072Sneelstruct vmspace; 119256072Sneelstruct vm_object; 120269008Sneelstruct vm_guest_paging; 121256072Sneelstruct pmap; 122221828Sgrehan 123260466Sneeltypedef int (*vmm_init_func_t)(int ipinum); 124221828Sgrehantypedef int (*vmm_cleanup_func_t)(void); 125259782Sjhbtypedef void (*vmm_resume_func_t)(void); 126256072Sneeltypedef void * (*vmi_init_func_t)(struct vm *vm, struct pmap *pmap); 127256072Sneeltypedef int (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip, 128263780Sneel struct pmap *pmap, void *rendezvous_cookie, 129263780Sneel void *suspend_cookie); 130221828Sgrehantypedef void (*vmi_cleanup_func_t)(void *vmi); 131221828Sgrehantypedef int (*vmi_get_register_t)(void *vmi, int vcpu, int num, 132221828Sgrehan uint64_t *retval); 133221828Sgrehantypedef int (*vmi_set_register_t)(void *vmi, int vcpu, int num, 134221828Sgrehan uint64_t val); 135221828Sgrehantypedef int (*vmi_get_desc_t)(void *vmi, int vcpu, int num, 136221828Sgrehan struct seg_desc *desc); 137221828Sgrehantypedef int (*vmi_set_desc_t)(void *vmi, int vcpu, int num, 138221828Sgrehan struct seg_desc *desc); 139221828Sgrehantypedef int (*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval); 140221828Sgrehantypedef int (*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val); 141256072Sneeltypedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max); 142256072Sneeltypedef void (*vmi_vmspace_free)(struct vmspace *vmspace); 143259863Sneeltypedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu); 144259863Sneeltypedef void (*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic); 145221828Sgrehan 146221828Sgrehanstruct vmm_ops { 147221828Sgrehan vmm_init_func_t init; /* module wide initialization */ 148221828Sgrehan vmm_cleanup_func_t cleanup; 149259782Sjhb vmm_resume_func_t resume; 150221828Sgrehan 151221828Sgrehan vmi_init_func_t vminit; /* vm-specific initialization */ 152221828Sgrehan vmi_run_func_t vmrun; 153221828Sgrehan vmi_cleanup_func_t vmcleanup; 154221828Sgrehan vmi_get_register_t vmgetreg; 155221828Sgrehan vmi_set_register_t vmsetreg; 156221828Sgrehan vmi_get_desc_t vmgetdesc; 157221828Sgrehan vmi_set_desc_t vmsetdesc; 158221828Sgrehan vmi_get_cap_t vmgetcap; 159221828Sgrehan vmi_set_cap_t vmsetcap; 160256072Sneel vmi_vmspace_alloc vmspace_alloc; 161256072Sneel vmi_vmspace_free vmspace_free; 162259863Sneel vmi_vlapic_init vlapic_init; 163259863Sneel vmi_vlapic_cleanup vlapic_cleanup; 164221828Sgrehan}; 165221828Sgrehan 166221828Sgrehanextern struct vmm_ops vmm_ops_intel; 167221828Sgrehanextern struct vmm_ops vmm_ops_amd; 168221828Sgrehan 169249396Sneelint vm_create(const char *name, struct vm **retvm); 170221828Sgrehanvoid vm_destroy(struct vm *vm); 171267216Sneelint vm_reinit(struct vm *vm); 172221828Sgrehanconst char *vm_name(struct vm *vm); 173241041Sneelint vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len); 174221828Sgrehanint vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa); 175221828Sgrehanint vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len); 176256072Sneelvoid *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot, 177256072Sneel void **cookie); 178256072Sneelvoid vm_gpa_release(void *cookie); 179221828Sgrehanint vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, 180221828Sgrehan struct vm_memory_segment *seg); 181256072Sneelint vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len, 182256072Sneel vm_offset_t *offset, struct vm_object **object); 183256072Sneelboolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa); 184221828Sgrehanint vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval); 185221828Sgrehanint vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val); 186221828Sgrehanint vm_get_seg_desc(struct vm *vm, int vcpu, int reg, 187221828Sgrehan struct seg_desc *ret_desc); 188221828Sgrehanint vm_set_seg_desc(struct vm *vm, int vcpu, int reg, 189221828Sgrehan struct seg_desc *desc); 190221828Sgrehanint vm_run(struct vm *vm, struct vm_run *vmrun); 191265062Sneelint vm_suspend(struct vm *vm, enum vm_suspend_how how); 192221828Sgrehanint vm_inject_nmi(struct vm *vm, int vcpu); 193241982Sneelint vm_nmi_pending(struct vm *vm, int vcpuid); 194241982Sneelvoid vm_nmi_clear(struct vm *vm, int vcpuid); 195263211Stychonint vm_inject_extint(struct vm *vm, int vcpu); 196263211Stychonint vm_extint_pending(struct vm *vm, int vcpuid); 197263211Stychonvoid vm_extint_clear(struct vm *vm, int vcpuid); 198221828Sgrehanuint64_t *vm_guest_msrs(struct vm *vm, int cpu); 199221828Sgrehanstruct vlapic *vm_lapic(struct vm *vm, int cpu); 200258075Sneelstruct vioapic *vm_ioapic(struct vm *vm); 201258579Sneelstruct vhpet *vm_hpet(struct vm *vm); 202221828Sgrehanint vm_get_capability(struct vm *vm, int vcpu, int type, int *val); 203221828Sgrehanint vm_set_capability(struct vm *vm, int vcpu, int type, int val); 204240922Sneelint vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state); 205240922Sneelint vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state); 206258075Sneelint vm_apicid2vcpuid(struct vm *vm, int apicid); 207266933Sneelint vm_activate_cpu(struct vm *vm, int vcpu); 208223621Sgrehancpuset_t vm_active_cpus(struct vm *vm); 209266933Sneelcpuset_t vm_suspended_cpus(struct vm *vm); 210240894Sneelstruct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid); 211265062Sneelvoid vm_exit_suspended(struct vm *vm, int vcpuid, uint64_t rip); 212267330Sneelvoid vm_exit_rendezvous(struct vm *vm, int vcpuid, uint64_t rip); 213267330Sneelvoid vm_exit_astpending(struct vm *vm, int vcpuid, uint64_t rip); 214221828Sgrehan 215221828Sgrehan/* 216260619Sneel * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'. 217260619Sneel * The rendezvous 'func(arg)' is not allowed to do anything that will 218260619Sneel * cause the thread to be put to sleep. 219260619Sneel * 220260619Sneel * If the rendezvous is being initiated from a vcpu context then the 221260619Sneel * 'vcpuid' must refer to that vcpu, otherwise it should be set to -1. 222260619Sneel * 223260619Sneel * The caller cannot hold any locks when initiating the rendezvous. 224260619Sneel * 225260619Sneel * The implementation of this API may cause vcpus other than those specified 226260619Sneel * by 'dest' to be stalled. The caller should not rely on any vcpus making 227260619Sneel * forward progress when the rendezvous is in progress. 228260619Sneel */ 229260619Sneeltypedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg); 230260619Sneelvoid vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest, 231260619Sneel vm_rendezvous_func_t func, void *arg); 232260619Sneel 233260619Sneelstatic __inline int 234260619Sneelvcpu_rendezvous_pending(void *rendezvous_cookie) 235260619Sneel{ 236260619Sneel 237260619Sneel return (*(uintptr_t *)rendezvous_cookie != 0); 238260619Sneel} 239260619Sneel 240263780Sneelstatic __inline int 241263780Sneelvcpu_suspended(void *suspend_cookie) 242263780Sneel{ 243263780Sneel 244263780Sneel return (*(int *)suspend_cookie); 245263780Sneel} 246263780Sneel 247260619Sneel/* 248221828Sgrehan * Return 1 if device indicated by bus/slot/func is supposed to be a 249221828Sgrehan * pci passthrough device. 250221828Sgrehan * 251221828Sgrehan * Return 0 otherwise. 252221828Sgrehan */ 253221828Sgrehanint vmm_is_pptdev(int bus, int slot, int func); 254221828Sgrehan 255221828Sgrehanvoid *vm_iommu_domain(struct vm *vm); 256221828Sgrehan 257241489Sneelenum vcpu_state { 258241489Sneel VCPU_IDLE, 259256072Sneel VCPU_FROZEN, 260241489Sneel VCPU_RUNNING, 261256072Sneel VCPU_SLEEPING, 262241489Sneel}; 263221828Sgrehan 264259737Sneelint vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state, 265259737Sneel bool from_idle); 266249879Sgrehanenum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu); 267221828Sgrehan 268221828Sgrehanstatic int __inline 269249879Sgrehanvcpu_is_running(struct vm *vm, int vcpu, int *hostcpu) 270221828Sgrehan{ 271249879Sgrehan return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING); 272221828Sgrehan} 273221828Sgrehan 274269109Sneel#ifdef _SYS_PROC_H_ 275269109Sneelstatic int __inline 276269109Sneelvcpu_should_yield(struct vm *vm, int vcpu) 277269109Sneel{ 278269109Sneel return (curthread->td_flags & (TDF_ASTPENDING | TDF_NEEDRESCHED)); 279269109Sneel} 280269109Sneel#endif 281269109Sneel 282241489Sneelvoid *vcpu_stats(struct vm *vm, int vcpu); 283259863Sneelvoid vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr); 284256072Sneelstruct vmspace *vm_get_vmspace(struct vm *vm); 285256072Sneelint vm_assign_pptdev(struct vm *vm, int bus, int slot, int func); 286256072Sneelint vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func); 287263035Stychonstruct vatpic *vm_atpic(struct vm *vm); 288263744Stychonstruct vatpit *vm_atpit(struct vm *vm); 289262506Sneel 290262506Sneel/* 291262506Sneel * Inject exception 'vme' into the guest vcpu. This function returns 0 on 292262506Sneel * success and non-zero on failure. 293262506Sneel * 294262506Sneel * Wrapper functions like 'vm_inject_gp()' should be preferred to calling 295262506Sneel * this function directly because they enforce the trap-like or fault-like 296262506Sneel * behavior of an exception. 297262506Sneel * 298262506Sneel * This function should only be called in the context of the thread that is 299262506Sneel * executing this vcpu. 300262506Sneel */ 301262506Sneelint vm_inject_exception(struct vm *vm, int vcpuid, struct vm_exception *vme); 302262506Sneel 303262506Sneel/* 304268889Sneel * This function is called after a VM-exit that occurred during exception or 305268889Sneel * interrupt delivery through the IDT. The format of 'intinfo' is described 306268889Sneel * in Figure 15-1, "EXITINTINFO for All Intercepts", APM, Vol 2. 307262506Sneel * 308268889Sneel * If a VM-exit handler completes the event delivery successfully then it 309268889Sneel * should call vm_exit_intinfo() to extinguish the pending event. For e.g., 310268889Sneel * if the task switch emulation is triggered via a task gate then it should 311268889Sneel * call this function with 'intinfo=0' to indicate that the external event 312268889Sneel * is not pending anymore. 313268889Sneel * 314268889Sneel * Return value is 0 on success and non-zero on failure. 315262506Sneel */ 316268889Sneelint vm_exit_intinfo(struct vm *vm, int vcpuid, uint64_t intinfo); 317262506Sneel 318268889Sneel/* 319268889Sneel * This function is called before every VM-entry to retrieve a pending 320268889Sneel * event that should be injected into the guest. This function combines 321268889Sneel * nested events into a double or triple fault. 322268889Sneel * 323268889Sneel * Returns 0 if there are no events that need to be injected into the guest 324268889Sneel * and non-zero otherwise. 325268889Sneel */ 326268889Sneelint vm_entry_intinfo(struct vm *vm, int vcpuid, uint64_t *info); 327268889Sneel 328268889Sneelint vm_get_intinfo(struct vm *vm, int vcpuid, uint64_t *info1, uint64_t *info2); 329268889Sneel 330266573Sneelenum vm_reg_name vm_segment_name(int seg_encoding); 331266573Sneel 332269008Sneelstruct vm_copyinfo { 333269008Sneel uint64_t gpa; 334269008Sneel size_t len; 335269008Sneel void *hva; 336269008Sneel void *cookie; 337269008Sneel}; 338269008Sneel 339269008Sneel/* 340269008Sneel * Set up 'copyinfo[]' to copy to/from guest linear address space starting 341269008Sneel * at 'gla' and 'len' bytes long. The 'prot' should be set to PROT_READ for 342269008Sneel * a copyin or PROT_WRITE for a copyout. 343269008Sneel * 344269008Sneel * Returns 0 on success. 345269008Sneel * Returns 1 if an exception was injected into the guest. 346269008Sneel * Returns -1 otherwise. 347269008Sneel * 348269008Sneel * The 'copyinfo[]' can be passed to 'vm_copyin()' or 'vm_copyout()' only if 349269008Sneel * the return value is 0. The 'copyinfo[]' resources should be freed by calling 350269008Sneel * 'vm_copy_teardown()' after the copy is done. 351269008Sneel */ 352269008Sneelint vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging, 353269008Sneel uint64_t gla, size_t len, int prot, struct vm_copyinfo *copyinfo, 354269008Sneel int num_copyinfo); 355269008Sneelvoid vm_copy_teardown(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo, 356269008Sneel int num_copyinfo); 357269008Sneelvoid vm_copyin(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo, 358269008Sneel void *kaddr, size_t len); 359269008Sneelvoid vm_copyout(struct vm *vm, int vcpuid, const void *kaddr, 360269008Sneel struct vm_copyinfo *copyinfo, size_t len); 361221828Sgrehan#endif /* KERNEL */ 362221828Sgrehan 363255438Sgrehan#define VM_MAXCPU 16 /* maximum virtual cpus */ 364221828Sgrehan 365221828Sgrehan/* 366221828Sgrehan * Identifiers for optional vmm capabilities 367221828Sgrehan */ 368221828Sgrehanenum vm_cap_type { 369221828Sgrehan VM_CAP_HALT_EXIT, 370221828Sgrehan VM_CAP_MTRAP_EXIT, 371221828Sgrehan VM_CAP_PAUSE_EXIT, 372221828Sgrehan VM_CAP_UNRESTRICTED_GUEST, 373256645Sneel VM_CAP_ENABLE_INVPCID, 374221828Sgrehan VM_CAP_MAX 375221828Sgrehan}; 376221828Sgrehan 377266125Sjhbenum vm_intr_trigger { 378266125Sjhb EDGE_TRIGGER, 379266125Sjhb LEVEL_TRIGGER 380266125Sjhb}; 381266125Sjhb 382221828Sgrehan/* 383221828Sgrehan * The 'access' field has the format specified in Table 21-2 of the Intel 384221828Sgrehan * Architecture Manual vol 3b. 385221828Sgrehan * 386221828Sgrehan * XXX The contents of the 'access' field are architecturally defined except 387221828Sgrehan * bit 16 - Segment Unusable. 388221828Sgrehan */ 389221828Sgrehanstruct seg_desc { 390221828Sgrehan uint64_t base; 391221828Sgrehan uint32_t limit; 392221828Sgrehan uint32_t access; 393221828Sgrehan}; 394268701Sneel#define SEG_DESC_TYPE(access) ((access) & 0x001f) 395268777Sneel#define SEG_DESC_DPL(access) (((access) >> 5) & 0x3) 396268701Sneel#define SEG_DESC_PRESENT(access) (((access) & 0x0080) ? 1 : 0) 397268701Sneel#define SEG_DESC_DEF32(access) (((access) & 0x4000) ? 1 : 0) 398268701Sneel#define SEG_DESC_GRANULARITY(access) (((access) & 0x8000) ? 1 : 0) 399268701Sneel#define SEG_DESC_UNUSABLE(access) (((access) & 0x10000) ? 1 : 0) 400221828Sgrehan 401266627Sneelenum vm_cpu_mode { 402268428Sneel CPU_MODE_REAL, 403268428Sneel CPU_MODE_PROTECTED, 404266627Sneel CPU_MODE_COMPATIBILITY, /* IA-32E mode (CS.L = 0) */ 405266627Sneel CPU_MODE_64BIT, /* IA-32E mode (CS.L = 1) */ 406266627Sneel}; 407266627Sneel 408266627Sneelenum vm_paging_mode { 409266627Sneel PAGING_MODE_FLAT, 410266627Sneel PAGING_MODE_32, 411266627Sneel PAGING_MODE_PAE, 412266627Sneel PAGING_MODE_64, 413266627Sneel}; 414266627Sneel 415266627Sneelstruct vm_guest_paging { 416266627Sneel uint64_t cr3; 417266627Sneel int cpl; 418266627Sneel enum vm_cpu_mode cpu_mode; 419266627Sneel enum vm_paging_mode paging_mode; 420266627Sneel}; 421266627Sneel 422266627Sneel/* 423266627Sneel * The data structures 'vie' and 'vie_op' are meant to be opaque to the 424266627Sneel * consumers of instruction decoding. The only reason why their contents 425266627Sneel * need to be exposed is because they are part of the 'vm_exit' structure. 426266627Sneel */ 427266627Sneelstruct vie_op { 428266627Sneel uint8_t op_byte; /* actual opcode byte */ 429266627Sneel uint8_t op_type; /* type of operation (e.g. MOV) */ 430266627Sneel uint16_t op_flags; 431266627Sneel}; 432266627Sneel 433266627Sneel#define VIE_INST_SIZE 15 434266627Sneelstruct vie { 435266627Sneel uint8_t inst[VIE_INST_SIZE]; /* instruction bytes */ 436266627Sneel uint8_t num_valid; /* size of the instruction */ 437266627Sneel uint8_t num_processed; 438266627Sneel 439268701Sneel uint8_t addrsize:4, opsize:4; /* address and operand sizes */ 440266627Sneel uint8_t rex_w:1, /* REX prefix */ 441266627Sneel rex_r:1, 442266627Sneel rex_x:1, 443266627Sneel rex_b:1, 444268701Sneel rex_present:1, 445268701Sneel opsize_override:1, /* Operand size override */ 446268701Sneel addrsize_override:1; /* Address size override */ 447266627Sneel 448266627Sneel uint8_t mod:2, /* ModRM byte */ 449266627Sneel reg:4, 450266627Sneel rm:4; 451266627Sneel 452266627Sneel uint8_t ss:2, /* SIB byte */ 453266627Sneel index:4, 454266627Sneel base:4; 455266627Sneel 456266627Sneel uint8_t disp_bytes; 457266627Sneel uint8_t imm_bytes; 458266627Sneel 459266627Sneel uint8_t scale; 460266627Sneel int base_register; /* VM_REG_GUEST_xyz */ 461266627Sneel int index_register; /* VM_REG_GUEST_xyz */ 462266627Sneel 463266627Sneel int64_t displacement; /* optional addr displacement */ 464266627Sneel int64_t immediate; /* optional immediate operand */ 465266627Sneel 466266627Sneel uint8_t decoded; /* set to 1 if successfully decoded */ 467266627Sneel 468266627Sneel struct vie_op op; /* opcode description */ 469266627Sneel}; 470266627Sneel 471221828Sgrehanenum vm_exitcode { 472221828Sgrehan VM_EXITCODE_INOUT, 473221828Sgrehan VM_EXITCODE_VMX, 474221828Sgrehan VM_EXITCODE_BOGUS, 475221828Sgrehan VM_EXITCODE_RDMSR, 476221828Sgrehan VM_EXITCODE_WRMSR, 477221828Sgrehan VM_EXITCODE_HLT, 478221828Sgrehan VM_EXITCODE_MTRAP, 479221828Sgrehan VM_EXITCODE_PAUSE, 480234761Sgrehan VM_EXITCODE_PAGING, 481256072Sneel VM_EXITCODE_INST_EMUL, 482240912Sneel VM_EXITCODE_SPINUP_AP, 483265101Sneel VM_EXITCODE_DEPRECATED1, /* used to be SPINDOWN_CPU */ 484260619Sneel VM_EXITCODE_RENDEZVOUS, 485261170Sneel VM_EXITCODE_IOAPIC_EOI, 486263780Sneel VM_EXITCODE_SUSPENDED, 487266573Sneel VM_EXITCODE_INOUT_STR, 488268777Sneel VM_EXITCODE_TASK_SWITCH, 489234761Sgrehan VM_EXITCODE_MAX 490221828Sgrehan}; 491221828Sgrehan 492266573Sneelstruct vm_inout { 493266573Sneel uint16_t bytes:3; /* 1 or 2 or 4 */ 494266573Sneel uint16_t in:1; 495266573Sneel uint16_t string:1; 496266573Sneel uint16_t rep:1; 497266573Sneel uint16_t port; 498266573Sneel uint32_t eax; /* valid for out */ 499266573Sneel}; 500266573Sneel 501266573Sneelstruct vm_inout_str { 502266573Sneel struct vm_inout inout; /* must be the first element */ 503266627Sneel struct vm_guest_paging paging; 504266573Sneel uint64_t rflags; 505266573Sneel uint64_t cr0; 506266573Sneel uint64_t index; 507266573Sneel uint64_t count; /* rep=1 (%rcx), rep=0 (1) */ 508266573Sneel int addrsize; 509266573Sneel enum vm_reg_name seg_name; 510266573Sneel struct seg_desc seg_desc; 511266573Sneel}; 512266573Sneel 513268777Sneelenum task_switch_reason { 514268777Sneel TSR_CALL, 515268777Sneel TSR_IRET, 516268777Sneel TSR_JMP, 517268777Sneel TSR_IDT_GATE, /* task gate in IDT */ 518268777Sneel}; 519268777Sneel 520268777Sneelstruct vm_task_switch { 521268777Sneel uint16_t tsssel; /* new TSS selector */ 522268777Sneel int ext; /* task switch due to external event */ 523268777Sneel uint32_t errcode; 524268777Sneel int errcode_valid; /* push 'errcode' on the new stack */ 525268777Sneel enum task_switch_reason reason; 526268777Sneel struct vm_guest_paging paging; 527268777Sneel}; 528268777Sneel 529221828Sgrehanstruct vm_exit { 530221828Sgrehan enum vm_exitcode exitcode; 531221828Sgrehan int inst_length; /* 0 means unknown */ 532221828Sgrehan uint64_t rip; 533221828Sgrehan union { 534266573Sneel struct vm_inout inout; 535266573Sneel struct vm_inout_str inout_str; 536221828Sgrehan struct { 537241497Sgrehan uint64_t gpa; 538256072Sneel int fault_type; 539256072Sneel } paging; 540256072Sneel struct { 541256072Sneel uint64_t gpa; 542256072Sneel uint64_t gla; 543268701Sneel int cs_d; /* CS.D */ 544266627Sneel struct vm_guest_paging paging; 545243640Sneel struct vie vie; 546256072Sneel } inst_emul; 547221828Sgrehan /* 548221828Sgrehan * VMX specific payload. Used when there is no "better" 549221828Sgrehan * exitcode to represent the VM-exit. 550221828Sgrehan */ 551221828Sgrehan struct { 552260167Sneel int status; /* vmx inst status */ 553260167Sneel /* 554260167Sneel * 'exit_reason' and 'exit_qualification' are valid 555260167Sneel * only if 'status' is zero. 556260167Sneel */ 557221828Sgrehan uint32_t exit_reason; 558221828Sgrehan uint64_t exit_qualification; 559260167Sneel /* 560260167Sneel * 'inst_error' and 'inst_type' are valid 561260167Sneel * only if 'status' is non-zero. 562260167Sneel */ 563260167Sneel int inst_type; 564260167Sneel int inst_error; 565221828Sgrehan } vmx; 566221828Sgrehan struct { 567221828Sgrehan uint32_t code; /* ecx value */ 568221828Sgrehan uint64_t wval; 569221828Sgrehan } msr; 570240912Sneel struct { 571240912Sneel int vcpu; 572240912Sneel uint64_t rip; 573240912Sneel } spinup_ap; 574259081Sneel struct { 575259081Sneel uint64_t rflags; 576259081Sneel } hlt; 577261170Sneel struct { 578261170Sneel int vector; 579261170Sneel } ioapic_eoi; 580265062Sneel struct { 581265062Sneel enum vm_suspend_how how; 582265062Sneel } suspended; 583268777Sneel struct vm_task_switch task_switch; 584221828Sgrehan } u; 585221828Sgrehan}; 586221828Sgrehan 587269042Sneel/* APIs to inject faults into the guest */ 588269042Sneelvoid vm_inject_fault(void *vm, int vcpuid, int vector, int errcode_valid, 589269042Sneel int errcode); 590269042Sneel 591270438Sgrehanstatic __inline void 592269042Sneelvm_inject_ud(void *vm, int vcpuid) 593269042Sneel{ 594269042Sneel vm_inject_fault(vm, vcpuid, IDT_UD, 0, 0); 595269042Sneel} 596269042Sneel 597270438Sgrehanstatic __inline void 598269042Sneelvm_inject_gp(void *vm, int vcpuid) 599269042Sneel{ 600269042Sneel vm_inject_fault(vm, vcpuid, IDT_GP, 1, 0); 601269042Sneel} 602269042Sneel 603270438Sgrehanstatic __inline void 604269042Sneelvm_inject_ac(void *vm, int vcpuid, int errcode) 605269042Sneel{ 606269042Sneel vm_inject_fault(vm, vcpuid, IDT_AC, 1, errcode); 607269042Sneel} 608269042Sneel 609270438Sgrehanstatic __inline void 610269042Sneelvm_inject_ss(void *vm, int vcpuid, int errcode) 611269042Sneel{ 612269042Sneel vm_inject_fault(vm, vcpuid, IDT_SS, 1, errcode); 613269042Sneel} 614269042Sneel 615269042Sneelvoid vm_inject_pf(void *vm, int vcpuid, int error_code, uint64_t cr2); 616269042Sneel 617221828Sgrehan#endif /* _VMM_H_ */ 618