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