vmm.h revision 348271
1/*- 2 * Copyright (c) 2011 NetApp, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: stable/11/sys/amd64/include/vmm.h 348271 2019-05-25 11:27:56Z rgrimes $ 27 */ 28 29#ifndef _VMM_H_ 30#define _VMM_H_ 31 32#include <sys/sdt.h> 33#include <x86/segments.h> 34 35#ifdef _KERNEL 36SDT_PROVIDER_DECLARE(vmm); 37#endif 38 39enum vm_suspend_how { 40 VM_SUSPEND_NONE, 41 VM_SUSPEND_RESET, 42 VM_SUSPEND_POWEROFF, 43 VM_SUSPEND_HALT, 44 VM_SUSPEND_TRIPLEFAULT, 45 VM_SUSPEND_LAST 46}; 47 48/* 49 * Identifiers for architecturally defined registers. 50 */ 51enum vm_reg_name { 52 VM_REG_GUEST_RAX, 53 VM_REG_GUEST_RBX, 54 VM_REG_GUEST_RCX, 55 VM_REG_GUEST_RDX, 56 VM_REG_GUEST_RSI, 57 VM_REG_GUEST_RDI, 58 VM_REG_GUEST_RBP, 59 VM_REG_GUEST_R8, 60 VM_REG_GUEST_R9, 61 VM_REG_GUEST_R10, 62 VM_REG_GUEST_R11, 63 VM_REG_GUEST_R12, 64 VM_REG_GUEST_R13, 65 VM_REG_GUEST_R14, 66 VM_REG_GUEST_R15, 67 VM_REG_GUEST_CR0, 68 VM_REG_GUEST_CR3, 69 VM_REG_GUEST_CR4, 70 VM_REG_GUEST_DR7, 71 VM_REG_GUEST_RSP, 72 VM_REG_GUEST_RIP, 73 VM_REG_GUEST_RFLAGS, 74 VM_REG_GUEST_ES, 75 VM_REG_GUEST_CS, 76 VM_REG_GUEST_SS, 77 VM_REG_GUEST_DS, 78 VM_REG_GUEST_FS, 79 VM_REG_GUEST_GS, 80 VM_REG_GUEST_LDTR, 81 VM_REG_GUEST_TR, 82 VM_REG_GUEST_IDTR, 83 VM_REG_GUEST_GDTR, 84 VM_REG_GUEST_EFER, 85 VM_REG_GUEST_CR2, 86 VM_REG_GUEST_PDPTE0, 87 VM_REG_GUEST_PDPTE1, 88 VM_REG_GUEST_PDPTE2, 89 VM_REG_GUEST_PDPTE3, 90 VM_REG_GUEST_INTR_SHADOW, 91 VM_REG_GUEST_DR0, 92 VM_REG_GUEST_DR1, 93 VM_REG_GUEST_DR2, 94 VM_REG_GUEST_DR3, 95 VM_REG_GUEST_DR6, 96 VM_REG_LAST 97}; 98 99enum x2apic_state { 100 X2APIC_DISABLED, 101 X2APIC_ENABLED, 102 X2APIC_STATE_LAST 103}; 104 105#define VM_INTINFO_VECTOR(info) ((info) & 0xff) 106#define VM_INTINFO_DEL_ERRCODE 0x800 107#define VM_INTINFO_RSVD 0x7ffff000 108#define VM_INTINFO_VALID 0x80000000 109#define VM_INTINFO_TYPE 0x700 110#define VM_INTINFO_HWINTR (0 << 8) 111#define VM_INTINFO_NMI (2 << 8) 112#define VM_INTINFO_HWEXCEPTION (3 << 8) 113#define VM_INTINFO_SWINTR (4 << 8) 114 115#ifdef _KERNEL 116 117#define VM_MAX_NAMELEN 32 118 119struct vm; 120struct vm_exception; 121struct seg_desc; 122struct vm_exit; 123struct vm_run; 124struct vhpet; 125struct vioapic; 126struct vlapic; 127struct vmspace; 128struct vm_object; 129struct vm_guest_paging; 130struct pmap; 131 132struct vm_eventinfo { 133 void *rptr; /* rendezvous cookie */ 134 int *sptr; /* suspend cookie */ 135 int *iptr; /* reqidle cookie */ 136}; 137 138typedef int (*vmm_init_func_t)(int ipinum); 139typedef int (*vmm_cleanup_func_t)(void); 140typedef void (*vmm_resume_func_t)(void); 141typedef void * (*vmi_init_func_t)(struct vm *vm, struct pmap *pmap); 142typedef int (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip, 143 struct pmap *pmap, struct vm_eventinfo *info); 144typedef void (*vmi_cleanup_func_t)(void *vmi); 145typedef int (*vmi_get_register_t)(void *vmi, int vcpu, int num, 146 uint64_t *retval); 147typedef int (*vmi_set_register_t)(void *vmi, int vcpu, int num, 148 uint64_t val); 149typedef int (*vmi_get_desc_t)(void *vmi, int vcpu, int num, 150 struct seg_desc *desc); 151typedef int (*vmi_set_desc_t)(void *vmi, int vcpu, int num, 152 struct seg_desc *desc); 153typedef int (*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval); 154typedef int (*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val); 155typedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max); 156typedef void (*vmi_vmspace_free)(struct vmspace *vmspace); 157typedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu); 158typedef void (*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic); 159 160struct vmm_ops { 161 vmm_init_func_t init; /* module wide initialization */ 162 vmm_cleanup_func_t cleanup; 163 vmm_resume_func_t resume; 164 165 vmi_init_func_t vminit; /* vm-specific initialization */ 166 vmi_run_func_t vmrun; 167 vmi_cleanup_func_t vmcleanup; 168 vmi_get_register_t vmgetreg; 169 vmi_set_register_t vmsetreg; 170 vmi_get_desc_t vmgetdesc; 171 vmi_set_desc_t vmsetdesc; 172 vmi_get_cap_t vmgetcap; 173 vmi_set_cap_t vmsetcap; 174 vmi_vmspace_alloc vmspace_alloc; 175 vmi_vmspace_free vmspace_free; 176 vmi_vlapic_init vlapic_init; 177 vmi_vlapic_cleanup vlapic_cleanup; 178}; 179 180extern struct vmm_ops vmm_ops_intel; 181extern struct vmm_ops vmm_ops_amd; 182 183int vm_create(const char *name, struct vm **retvm); 184void vm_destroy(struct vm *vm); 185int vm_reinit(struct vm *vm); 186const char *vm_name(struct vm *vm); 187uint16_t vm_get_maxcpus(struct vm *vm); 188void vm_get_topology(struct vm *vm, uint16_t *sockets, uint16_t *cores, 189 uint16_t *threads, uint16_t *maxcpus); 190int vm_set_topology(struct vm *vm, uint16_t sockets, uint16_t cores, 191 uint16_t threads, uint16_t maxcpus); 192 193/* 194 * APIs that modify the guest memory map require all vcpus to be frozen. 195 */ 196int vm_mmap_memseg(struct vm *vm, vm_paddr_t gpa, int segid, vm_ooffset_t off, 197 size_t len, int prot, int flags); 198int vm_alloc_memseg(struct vm *vm, int ident, size_t len, bool sysmem); 199void vm_free_memseg(struct vm *vm, int ident); 200int vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa); 201int vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len); 202int vm_assign_pptdev(struct vm *vm, int bus, int slot, int func); 203int vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func); 204 205/* 206 * APIs that inspect the guest memory map require only a *single* vcpu to 207 * be frozen. This acts like a read lock on the guest memory map since any 208 * modification requires *all* vcpus to be frozen. 209 */ 210int vm_mmap_getnext(struct vm *vm, vm_paddr_t *gpa, int *segid, 211 vm_ooffset_t *segoff, size_t *len, int *prot, int *flags); 212int vm_get_memseg(struct vm *vm, int ident, size_t *len, bool *sysmem, 213 struct vm_object **objptr); 214vm_paddr_t vmm_sysmem_maxaddr(struct vm *vm); 215void *vm_gpa_hold(struct vm *, int vcpuid, vm_paddr_t gpa, size_t len, 216 int prot, void **cookie); 217void vm_gpa_release(void *cookie); 218bool vm_mem_allocated(struct vm *vm, int vcpuid, vm_paddr_t gpa); 219 220int vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval); 221int vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val); 222int vm_get_seg_desc(struct vm *vm, int vcpu, int reg, 223 struct seg_desc *ret_desc); 224int vm_set_seg_desc(struct vm *vm, int vcpu, int reg, 225 struct seg_desc *desc); 226int vm_run(struct vm *vm, struct vm_run *vmrun); 227int vm_suspend(struct vm *vm, enum vm_suspend_how how); 228int vm_inject_nmi(struct vm *vm, int vcpu); 229int vm_nmi_pending(struct vm *vm, int vcpuid); 230void vm_nmi_clear(struct vm *vm, int vcpuid); 231int vm_inject_extint(struct vm *vm, int vcpu); 232int vm_extint_pending(struct vm *vm, int vcpuid); 233void vm_extint_clear(struct vm *vm, int vcpuid); 234struct vlapic *vm_lapic(struct vm *vm, int cpu); 235struct vioapic *vm_ioapic(struct vm *vm); 236struct vhpet *vm_hpet(struct vm *vm); 237int vm_get_capability(struct vm *vm, int vcpu, int type, int *val); 238int vm_set_capability(struct vm *vm, int vcpu, int type, int val); 239int vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state); 240int vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state); 241int vm_apicid2vcpuid(struct vm *vm, int apicid); 242int vm_activate_cpu(struct vm *vm, int vcpu); 243struct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid); 244void vm_exit_suspended(struct vm *vm, int vcpuid, uint64_t rip); 245void vm_exit_rendezvous(struct vm *vm, int vcpuid, uint64_t rip); 246void vm_exit_astpending(struct vm *vm, int vcpuid, uint64_t rip); 247void vm_exit_reqidle(struct vm *vm, int vcpuid, uint64_t rip); 248 249#ifdef _SYS__CPUSET_H_ 250/* 251 * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'. 252 * The rendezvous 'func(arg)' is not allowed to do anything that will 253 * cause the thread to be put to sleep. 254 * 255 * If the rendezvous is being initiated from a vcpu context then the 256 * 'vcpuid' must refer to that vcpu, otherwise it should be set to -1. 257 * 258 * The caller cannot hold any locks when initiating the rendezvous. 259 * 260 * The implementation of this API may cause vcpus other than those specified 261 * by 'dest' to be stalled. The caller should not rely on any vcpus making 262 * forward progress when the rendezvous is in progress. 263 */ 264typedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg); 265void vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest, 266 vm_rendezvous_func_t func, void *arg); 267cpuset_t vm_active_cpus(struct vm *vm); 268cpuset_t vm_suspended_cpus(struct vm *vm); 269#endif /* _SYS__CPUSET_H_ */ 270 271static __inline int 272vcpu_rendezvous_pending(struct vm_eventinfo *info) 273{ 274 275 return (*((uintptr_t *)(info->rptr)) != 0); 276} 277 278static __inline int 279vcpu_suspended(struct vm_eventinfo *info) 280{ 281 282 return (*info->sptr); 283} 284 285static __inline int 286vcpu_reqidle(struct vm_eventinfo *info) 287{ 288 289 return (*info->iptr); 290} 291 292/* 293 * Return 1 if device indicated by bus/slot/func is supposed to be a 294 * pci passthrough device. 295 * 296 * Return 0 otherwise. 297 */ 298int vmm_is_pptdev(int bus, int slot, int func); 299 300void *vm_iommu_domain(struct vm *vm); 301 302enum vcpu_state { 303 VCPU_IDLE, 304 VCPU_FROZEN, 305 VCPU_RUNNING, 306 VCPU_SLEEPING, 307}; 308 309int vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state, 310 bool from_idle); 311enum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu); 312 313static int __inline 314vcpu_is_running(struct vm *vm, int vcpu, int *hostcpu) 315{ 316 return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING); 317} 318 319#ifdef _SYS_PROC_H_ 320static int __inline 321vcpu_should_yield(struct vm *vm, int vcpu) 322{ 323 324 if (curthread->td_flags & (TDF_ASTPENDING | TDF_NEEDRESCHED)) 325 return (1); 326 else if (curthread->td_owepreempt) 327 return (1); 328 else 329 return (0); 330} 331#endif 332 333void *vcpu_stats(struct vm *vm, int vcpu); 334void vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr); 335struct vmspace *vm_get_vmspace(struct vm *vm); 336struct vatpic *vm_atpic(struct vm *vm); 337struct vatpit *vm_atpit(struct vm *vm); 338struct vpmtmr *vm_pmtmr(struct vm *vm); 339struct vrtc *vm_rtc(struct vm *vm); 340 341/* 342 * Inject exception 'vector' into the guest vcpu. This function returns 0 on 343 * success and non-zero on failure. 344 * 345 * Wrapper functions like 'vm_inject_gp()' should be preferred to calling 346 * this function directly because they enforce the trap-like or fault-like 347 * behavior of an exception. 348 * 349 * This function should only be called in the context of the thread that is 350 * executing this vcpu. 351 */ 352int vm_inject_exception(struct vm *vm, int vcpuid, int vector, int err_valid, 353 uint32_t errcode, int restart_instruction); 354 355/* 356 * This function is called after a VM-exit that occurred during exception or 357 * interrupt delivery through the IDT. The format of 'intinfo' is described 358 * in Figure 15-1, "EXITINTINFO for All Intercepts", APM, Vol 2. 359 * 360 * If a VM-exit handler completes the event delivery successfully then it 361 * should call vm_exit_intinfo() to extinguish the pending event. For e.g., 362 * if the task switch emulation is triggered via a task gate then it should 363 * call this function with 'intinfo=0' to indicate that the external event 364 * is not pending anymore. 365 * 366 * Return value is 0 on success and non-zero on failure. 367 */ 368int vm_exit_intinfo(struct vm *vm, int vcpuid, uint64_t intinfo); 369 370/* 371 * This function is called before every VM-entry to retrieve a pending 372 * event that should be injected into the guest. This function combines 373 * nested events into a double or triple fault. 374 * 375 * Returns 0 if there are no events that need to be injected into the guest 376 * and non-zero otherwise. 377 */ 378int vm_entry_intinfo(struct vm *vm, int vcpuid, uint64_t *info); 379 380int vm_get_intinfo(struct vm *vm, int vcpuid, uint64_t *info1, uint64_t *info2); 381 382enum vm_reg_name vm_segment_name(int seg_encoding); 383 384struct vm_copyinfo { 385 uint64_t gpa; 386 size_t len; 387 void *hva; 388 void *cookie; 389}; 390 391/* 392 * Set up 'copyinfo[]' to copy to/from guest linear address space starting 393 * at 'gla' and 'len' bytes long. The 'prot' should be set to PROT_READ for 394 * a copyin or PROT_WRITE for a copyout. 395 * 396 * retval is_fault Interpretation 397 * 0 0 Success 398 * 0 1 An exception was injected into the guest 399 * EFAULT N/A Unrecoverable error 400 * 401 * The 'copyinfo[]' can be passed to 'vm_copyin()' or 'vm_copyout()' only if 402 * the return value is 0. The 'copyinfo[]' resources should be freed by calling 403 * 'vm_copy_teardown()' after the copy is done. 404 */ 405int vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging, 406 uint64_t gla, size_t len, int prot, struct vm_copyinfo *copyinfo, 407 int num_copyinfo, int *is_fault); 408void vm_copy_teardown(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo, 409 int num_copyinfo); 410void vm_copyin(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo, 411 void *kaddr, size_t len); 412void vm_copyout(struct vm *vm, int vcpuid, const void *kaddr, 413 struct vm_copyinfo *copyinfo, size_t len); 414 415int vcpu_trace_exceptions(struct vm *vm, int vcpuid); 416#endif /* KERNEL */ 417 418#define VM_MAXCPU 16 /* maximum virtual cpus */ 419 420/* 421 * Identifiers for optional vmm capabilities 422 */ 423enum vm_cap_type { 424 VM_CAP_HALT_EXIT, 425 VM_CAP_MTRAP_EXIT, 426 VM_CAP_PAUSE_EXIT, 427 VM_CAP_UNRESTRICTED_GUEST, 428 VM_CAP_ENABLE_INVPCID, 429 VM_CAP_MAX 430}; 431 432enum vm_intr_trigger { 433 EDGE_TRIGGER, 434 LEVEL_TRIGGER 435}; 436 437/* 438 * The 'access' field has the format specified in Table 21-2 of the Intel 439 * Architecture Manual vol 3b. 440 * 441 * XXX The contents of the 'access' field are architecturally defined except 442 * bit 16 - Segment Unusable. 443 */ 444struct seg_desc { 445 uint64_t base; 446 uint32_t limit; 447 uint32_t access; 448}; 449#define SEG_DESC_TYPE(access) ((access) & 0x001f) 450#define SEG_DESC_DPL(access) (((access) >> 5) & 0x3) 451#define SEG_DESC_PRESENT(access) (((access) & 0x0080) ? 1 : 0) 452#define SEG_DESC_DEF32(access) (((access) & 0x4000) ? 1 : 0) 453#define SEG_DESC_GRANULARITY(access) (((access) & 0x8000) ? 1 : 0) 454#define SEG_DESC_UNUSABLE(access) (((access) & 0x10000) ? 1 : 0) 455 456enum vm_cpu_mode { 457 CPU_MODE_REAL, 458 CPU_MODE_PROTECTED, 459 CPU_MODE_COMPATIBILITY, /* IA-32E mode (CS.L = 0) */ 460 CPU_MODE_64BIT, /* IA-32E mode (CS.L = 1) */ 461}; 462 463enum vm_paging_mode { 464 PAGING_MODE_FLAT, 465 PAGING_MODE_32, 466 PAGING_MODE_PAE, 467 PAGING_MODE_64, 468}; 469 470struct vm_guest_paging { 471 uint64_t cr3; 472 int cpl; 473 enum vm_cpu_mode cpu_mode; 474 enum vm_paging_mode paging_mode; 475}; 476 477/* 478 * The data structures 'vie' and 'vie_op' are meant to be opaque to the 479 * consumers of instruction decoding. The only reason why their contents 480 * need to be exposed is because they are part of the 'vm_exit' structure. 481 */ 482struct vie_op { 483 uint8_t op_byte; /* actual opcode byte */ 484 uint8_t op_type; /* type of operation (e.g. MOV) */ 485 uint16_t op_flags; 486}; 487 488#define VIE_INST_SIZE 15 489struct vie { 490 uint8_t inst[VIE_INST_SIZE]; /* instruction bytes */ 491 uint8_t num_valid; /* size of the instruction */ 492 uint8_t num_processed; 493 494 uint8_t addrsize:4, opsize:4; /* address and operand sizes */ 495 uint8_t rex_w:1, /* REX prefix */ 496 rex_r:1, 497 rex_x:1, 498 rex_b:1, 499 rex_present:1, 500 repz_present:1, /* REP/REPE/REPZ prefix */ 501 repnz_present:1, /* REPNE/REPNZ prefix */ 502 opsize_override:1, /* Operand size override */ 503 addrsize_override:1, /* Address size override */ 504 segment_override:1; /* Segment override */ 505 506 uint8_t mod:2, /* ModRM byte */ 507 reg:4, 508 rm:4; 509 510 uint8_t ss:2, /* SIB byte */ 511 index:4, 512 base:4; 513 514 uint8_t disp_bytes; 515 uint8_t imm_bytes; 516 517 uint8_t scale; 518 int base_register; /* VM_REG_GUEST_xyz */ 519 int index_register; /* VM_REG_GUEST_xyz */ 520 int segment_register; /* VM_REG_GUEST_xyz */ 521 522 int64_t displacement; /* optional addr displacement */ 523 int64_t immediate; /* optional immediate operand */ 524 525 uint8_t decoded; /* set to 1 if successfully decoded */ 526 527 struct vie_op op; /* opcode description */ 528}; 529 530enum vm_exitcode { 531 VM_EXITCODE_INOUT, 532 VM_EXITCODE_VMX, 533 VM_EXITCODE_BOGUS, 534 VM_EXITCODE_RDMSR, 535 VM_EXITCODE_WRMSR, 536 VM_EXITCODE_HLT, 537 VM_EXITCODE_MTRAP, 538 VM_EXITCODE_PAUSE, 539 VM_EXITCODE_PAGING, 540 VM_EXITCODE_INST_EMUL, 541 VM_EXITCODE_SPINUP_AP, 542 VM_EXITCODE_DEPRECATED1, /* used to be SPINDOWN_CPU */ 543 VM_EXITCODE_RENDEZVOUS, 544 VM_EXITCODE_IOAPIC_EOI, 545 VM_EXITCODE_SUSPENDED, 546 VM_EXITCODE_INOUT_STR, 547 VM_EXITCODE_TASK_SWITCH, 548 VM_EXITCODE_MONITOR, 549 VM_EXITCODE_MWAIT, 550 VM_EXITCODE_SVM, 551 VM_EXITCODE_REQIDLE, 552 VM_EXITCODE_VMINSN, 553 VM_EXITCODE_MAX 554}; 555 556struct vm_inout { 557 uint16_t bytes:3; /* 1 or 2 or 4 */ 558 uint16_t in:1; 559 uint16_t string:1; 560 uint16_t rep:1; 561 uint16_t port; 562 uint32_t eax; /* valid for out */ 563}; 564 565struct vm_inout_str { 566 struct vm_inout inout; /* must be the first element */ 567 struct vm_guest_paging paging; 568 uint64_t rflags; 569 uint64_t cr0; 570 uint64_t index; 571 uint64_t count; /* rep=1 (%rcx), rep=0 (1) */ 572 int addrsize; 573 enum vm_reg_name seg_name; 574 struct seg_desc seg_desc; 575}; 576 577enum task_switch_reason { 578 TSR_CALL, 579 TSR_IRET, 580 TSR_JMP, 581 TSR_IDT_GATE, /* task gate in IDT */ 582}; 583 584struct vm_task_switch { 585 uint16_t tsssel; /* new TSS selector */ 586 int ext; /* task switch due to external event */ 587 uint32_t errcode; 588 int errcode_valid; /* push 'errcode' on the new stack */ 589 enum task_switch_reason reason; 590 struct vm_guest_paging paging; 591}; 592 593struct vm_exit { 594 enum vm_exitcode exitcode; 595 int inst_length; /* 0 means unknown */ 596 uint64_t rip; 597 union { 598 struct vm_inout inout; 599 struct vm_inout_str inout_str; 600 struct { 601 uint64_t gpa; 602 int fault_type; 603 } paging; 604 struct { 605 uint64_t gpa; 606 uint64_t gla; 607 uint64_t cs_base; 608 int cs_d; /* CS.D */ 609 struct vm_guest_paging paging; 610 struct vie vie; 611 } inst_emul; 612 /* 613 * VMX specific payload. Used when there is no "better" 614 * exitcode to represent the VM-exit. 615 */ 616 struct { 617 int status; /* vmx inst status */ 618 /* 619 * 'exit_reason' and 'exit_qualification' are valid 620 * only if 'status' is zero. 621 */ 622 uint32_t exit_reason; 623 uint64_t exit_qualification; 624 /* 625 * 'inst_error' and 'inst_type' are valid 626 * only if 'status' is non-zero. 627 */ 628 int inst_type; 629 int inst_error; 630 } vmx; 631 /* 632 * SVM specific payload. 633 */ 634 struct { 635 uint64_t exitcode; 636 uint64_t exitinfo1; 637 uint64_t exitinfo2; 638 } svm; 639 struct { 640 uint32_t code; /* ecx value */ 641 uint64_t wval; 642 } msr; 643 struct { 644 int vcpu; 645 uint64_t rip; 646 } spinup_ap; 647 struct { 648 uint64_t rflags; 649 uint64_t intr_status; 650 } hlt; 651 struct { 652 int vector; 653 } ioapic_eoi; 654 struct { 655 enum vm_suspend_how how; 656 } suspended; 657 struct vm_task_switch task_switch; 658 } u; 659}; 660 661/* APIs to inject faults into the guest */ 662void vm_inject_fault(void *vm, int vcpuid, int vector, int errcode_valid, 663 int errcode); 664 665static __inline void 666vm_inject_ud(void *vm, int vcpuid) 667{ 668 vm_inject_fault(vm, vcpuid, IDT_UD, 0, 0); 669} 670 671static __inline void 672vm_inject_gp(void *vm, int vcpuid) 673{ 674 vm_inject_fault(vm, vcpuid, IDT_GP, 1, 0); 675} 676 677static __inline void 678vm_inject_ac(void *vm, int vcpuid, int errcode) 679{ 680 vm_inject_fault(vm, vcpuid, IDT_AC, 1, errcode); 681} 682 683static __inline void 684vm_inject_ss(void *vm, int vcpuid, int errcode) 685{ 686 vm_inject_fault(vm, vcpuid, IDT_SS, 1, errcode); 687} 688 689void vm_inject_pf(void *vm, int vcpuid, int error_code, uint64_t cr2); 690 691int vm_restart_instruction(void *vm, int vcpuid); 692 693#endif /* _VMM_H_ */ 694