2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
41 #include <sys/sysctl.h>
46 #include <machine/psl.h>
47 #include <machine/cpufunc.h>
48 #include <machine/md_var.h>
49 #include <machine/segments.h>
50 #include <machine/smp.h>
51 #include <machine/specialreg.h>
52 #include <machine/vmparam.h>
54 #include <machine/vmm.h>
55 #include <machine/vmm_dev.h>
56 #include <machine/vmm_instruction_emul.h>
57 #include "vmm_lapic.h"
59 #include "vmm_ioport.h"
64 #include "vlapic_priv.h"
67 #include "vmx_cpufunc.h"
71 #include "vmx_controls.h"
73 #define PINBASED_CTLS_ONE_SETTING \
74 (PINBASED_EXTINT_EXITING | \
75 PINBASED_NMI_EXITING | \
77 #define PINBASED_CTLS_ZERO_SETTING 0
79 #define PROCBASED_CTLS_WINDOW_SETTING \
80 (PROCBASED_INT_WINDOW_EXITING | \
81 PROCBASED_NMI_WINDOW_EXITING)
83 #define PROCBASED_CTLS_ONE_SETTING \
84 (PROCBASED_SECONDARY_CONTROLS | \
85 PROCBASED_MWAIT_EXITING | \
86 PROCBASED_MONITOR_EXITING | \
87 PROCBASED_IO_EXITING | \
88 PROCBASED_MSR_BITMAPS | \
89 PROCBASED_CTLS_WINDOW_SETTING | \
90 PROCBASED_CR8_LOAD_EXITING | \
91 PROCBASED_CR8_STORE_EXITING)
92 #define PROCBASED_CTLS_ZERO_SETTING \
93 (PROCBASED_CR3_LOAD_EXITING | \
94 PROCBASED_CR3_STORE_EXITING | \
97 #define PROCBASED_CTLS2_ONE_SETTING PROCBASED2_ENABLE_EPT
98 #define PROCBASED_CTLS2_ZERO_SETTING 0
100 #define VM_EXIT_CTLS_ONE_SETTING \
101 (VM_EXIT_SAVE_DEBUG_CONTROLS | \
103 VM_EXIT_SAVE_EFER | \
104 VM_EXIT_LOAD_EFER | \
105 VM_EXIT_ACKNOWLEDGE_INTERRUPT)
107 #define VM_EXIT_CTLS_ZERO_SETTING 0
109 #define VM_ENTRY_CTLS_ONE_SETTING \
110 (VM_ENTRY_LOAD_DEBUG_CONTROLS | \
113 #define VM_ENTRY_CTLS_ZERO_SETTING \
114 (VM_ENTRY_INTO_SMM | \
115 VM_ENTRY_DEACTIVATE_DUAL_MONITOR)
120 static MALLOC_DEFINE(M_VMX, "vmx", "vmx");
121 static MALLOC_DEFINE(M_VLAPIC, "vlapic", "vlapic");
123 SYSCTL_DECL(_hw_vmm);
124 SYSCTL_NODE(_hw_vmm, OID_AUTO, vmx, CTLFLAG_RW, NULL, NULL);
126 int vmxon_enabled[MAXCPU];
127 static char vmxon_region[MAXCPU][PAGE_SIZE] __aligned(PAGE_SIZE);
129 static uint32_t pinbased_ctls, procbased_ctls, procbased_ctls2;
130 static uint32_t exit_ctls, entry_ctls;
132 static uint64_t cr0_ones_mask, cr0_zeros_mask;
133 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_ones_mask, CTLFLAG_RD,
134 &cr0_ones_mask, 0, NULL);
135 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_zeros_mask, CTLFLAG_RD,
136 &cr0_zeros_mask, 0, NULL);
138 static uint64_t cr4_ones_mask, cr4_zeros_mask;
139 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_ones_mask, CTLFLAG_RD,
140 &cr4_ones_mask, 0, NULL);
141 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_zeros_mask, CTLFLAG_RD,
142 &cr4_zeros_mask, 0, NULL);
144 static int vmx_initialized;
145 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, initialized, CTLFLAG_RD,
146 &vmx_initialized, 0, "Intel VMX initialized");
149 * Optional capabilities
151 static SYSCTL_NODE(_hw_vmm_vmx, OID_AUTO, cap, CTLFLAG_RW, NULL, NULL);
153 static int cap_halt_exit;
154 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, halt_exit, CTLFLAG_RD, &cap_halt_exit, 0,
155 "HLT triggers a VM-exit");
157 static int cap_pause_exit;
158 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, pause_exit, CTLFLAG_RD, &cap_pause_exit,
159 0, "PAUSE triggers a VM-exit");
161 static int cap_unrestricted_guest;
162 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, unrestricted_guest, CTLFLAG_RD,
163 &cap_unrestricted_guest, 0, "Unrestricted guests");
165 static int cap_monitor_trap;
166 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, monitor_trap, CTLFLAG_RD,
167 &cap_monitor_trap, 0, "Monitor trap flag");
169 static int cap_invpcid;
170 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, invpcid, CTLFLAG_RD, &cap_invpcid,
171 0, "Guests are allowed to use INVPCID");
173 static int virtual_interrupt_delivery;
174 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, virtual_interrupt_delivery, CTLFLAG_RD,
175 &virtual_interrupt_delivery, 0, "APICv virtual interrupt delivery support");
177 static int posted_interrupts;
178 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, posted_interrupts, CTLFLAG_RD,
179 &posted_interrupts, 0, "APICv posted interrupt support");
181 static int pirvec = -1;
182 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, posted_interrupt_vector, CTLFLAG_RD,
183 &pirvec, 0, "APICv posted interrupt vector");
185 static struct unrhdr *vpid_unr;
186 static u_int vpid_alloc_failed;
187 SYSCTL_UINT(_hw_vmm_vmx, OID_AUTO, vpid_alloc_failed, CTLFLAG_RD,
188 &vpid_alloc_failed, 0, NULL);
191 * Use the last page below 4GB as the APIC access address. This address is
192 * occupied by the boot firmware so it is guaranteed that it will not conflict
193 * with a page in system memory.
195 #define APIC_ACCESS_ADDRESS 0xFFFFF000
197 static int vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc);
198 static int vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval);
199 static int vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val);
200 static void vmx_inject_pir(struct vlapic *vlapic);
204 exit_reason_to_str(int reason)
206 static char reasonbuf[32];
209 case EXIT_REASON_EXCEPTION:
211 case EXIT_REASON_EXT_INTR:
213 case EXIT_REASON_TRIPLE_FAULT:
214 return "triplefault";
215 case EXIT_REASON_INIT:
217 case EXIT_REASON_SIPI:
219 case EXIT_REASON_IO_SMI:
221 case EXIT_REASON_SMI:
223 case EXIT_REASON_INTR_WINDOW:
225 case EXIT_REASON_NMI_WINDOW:
227 case EXIT_REASON_TASK_SWITCH:
229 case EXIT_REASON_CPUID:
231 case EXIT_REASON_GETSEC:
233 case EXIT_REASON_HLT:
235 case EXIT_REASON_INVD:
237 case EXIT_REASON_INVLPG:
239 case EXIT_REASON_RDPMC:
241 case EXIT_REASON_RDTSC:
243 case EXIT_REASON_RSM:
245 case EXIT_REASON_VMCALL:
247 case EXIT_REASON_VMCLEAR:
249 case EXIT_REASON_VMLAUNCH:
251 case EXIT_REASON_VMPTRLD:
253 case EXIT_REASON_VMPTRST:
255 case EXIT_REASON_VMREAD:
257 case EXIT_REASON_VMRESUME:
259 case EXIT_REASON_VMWRITE:
261 case EXIT_REASON_VMXOFF:
263 case EXIT_REASON_VMXON:
265 case EXIT_REASON_CR_ACCESS:
267 case EXIT_REASON_DR_ACCESS:
269 case EXIT_REASON_INOUT:
271 case EXIT_REASON_RDMSR:
273 case EXIT_REASON_WRMSR:
275 case EXIT_REASON_INVAL_VMCS:
277 case EXIT_REASON_INVAL_MSR:
279 case EXIT_REASON_MWAIT:
281 case EXIT_REASON_MTF:
283 case EXIT_REASON_MONITOR:
285 case EXIT_REASON_PAUSE:
287 case EXIT_REASON_MCE_DURING_ENTRY:
288 return "mce-during-entry";
289 case EXIT_REASON_TPR:
291 case EXIT_REASON_APIC_ACCESS:
292 return "apic-access";
293 case EXIT_REASON_GDTR_IDTR:
295 case EXIT_REASON_LDTR_TR:
297 case EXIT_REASON_EPT_FAULT:
299 case EXIT_REASON_EPT_MISCONFIG:
300 return "eptmisconfig";
301 case EXIT_REASON_INVEPT:
303 case EXIT_REASON_RDTSCP:
305 case EXIT_REASON_VMX_PREEMPT:
307 case EXIT_REASON_INVVPID:
309 case EXIT_REASON_WBINVD:
311 case EXIT_REASON_XSETBV:
313 case EXIT_REASON_APIC_WRITE:
316 snprintf(reasonbuf, sizeof(reasonbuf), "%d", reason);
323 vmx_allow_x2apic_msrs(struct vmx *vmx)
330 * Allow readonly access to the following x2APIC MSRs from the guest.
332 error += guest_msr_ro(vmx, MSR_APIC_ID);
333 error += guest_msr_ro(vmx, MSR_APIC_VERSION);
334 error += guest_msr_ro(vmx, MSR_APIC_LDR);
335 error += guest_msr_ro(vmx, MSR_APIC_SVR);
337 for (i = 0; i < 8; i++)
338 error += guest_msr_ro(vmx, MSR_APIC_ISR0 + i);
340 for (i = 0; i < 8; i++)
341 error += guest_msr_ro(vmx, MSR_APIC_TMR0 + i);
343 for (i = 0; i < 8; i++)
344 error += guest_msr_ro(vmx, MSR_APIC_IRR0 + i);
346 error += guest_msr_ro(vmx, MSR_APIC_ESR);
347 error += guest_msr_ro(vmx, MSR_APIC_LVT_TIMER);
348 error += guest_msr_ro(vmx, MSR_APIC_LVT_THERMAL);
349 error += guest_msr_ro(vmx, MSR_APIC_LVT_PCINT);
350 error += guest_msr_ro(vmx, MSR_APIC_LVT_LINT0);
351 error += guest_msr_ro(vmx, MSR_APIC_LVT_LINT1);
352 error += guest_msr_ro(vmx, MSR_APIC_LVT_ERROR);
353 error += guest_msr_ro(vmx, MSR_APIC_ICR_TIMER);
354 error += guest_msr_ro(vmx, MSR_APIC_DCR_TIMER);
355 error += guest_msr_ro(vmx, MSR_APIC_ICR);
358 * Allow TPR, EOI and SELF_IPI MSRs to be read and written by the guest.
360 * These registers get special treatment described in the section
361 * "Virtualizing MSR-Based APIC Accesses".
363 error += guest_msr_rw(vmx, MSR_APIC_TPR);
364 error += guest_msr_rw(vmx, MSR_APIC_EOI);
365 error += guest_msr_rw(vmx, MSR_APIC_SELF_IPI);
371 vmx_fix_cr0(u_long cr0)
374 return ((cr0 | cr0_ones_mask) & ~cr0_zeros_mask);
378 vmx_fix_cr4(u_long cr4)
381 return ((cr4 | cr4_ones_mask) & ~cr4_zeros_mask);
387 if (vpid < 0 || vpid > 0xffff)
388 panic("vpid_free: invalid vpid %d", vpid);
391 * VPIDs [0,VM_MAXCPU] are special and are not allocated from
392 * the unit number allocator.
395 if (vpid > VM_MAXCPU)
396 free_unr(vpid_unr, vpid);
400 vpid_alloc(uint16_t *vpid, int num)
404 if (num <= 0 || num > VM_MAXCPU)
405 panic("invalid number of vpids requested: %d", num);
408 * If the "enable vpid" execution control is not enabled then the
409 * VPID is required to be 0 for all vcpus.
411 if ((procbased_ctls2 & PROCBASED2_ENABLE_VPID) == 0) {
412 for (i = 0; i < num; i++)
418 * Allocate a unique VPID for each vcpu from the unit number allocator.
420 for (i = 0; i < num; i++) {
421 x = alloc_unr(vpid_unr);
429 atomic_add_int(&vpid_alloc_failed, 1);
432 * If the unit number allocator does not have enough unique
433 * VPIDs then we need to allocate from the [1,VM_MAXCPU] range.
435 * These VPIDs are not be unique across VMs but this does not
436 * affect correctness because the combined mappings are also
437 * tagged with the EP4TA which is unique for each VM.
439 * It is still sub-optimal because the invvpid will invalidate
440 * combined mappings for a particular VPID across all EP4TAs.
445 for (i = 0; i < num; i++)
454 * VPID 0 is required when the "enable VPID" execution control is
457 * VPIDs [1,VM_MAXCPU] are used as the "overflow namespace" when the
458 * unit number allocator does not have sufficient unique VPIDs to
459 * satisfy the allocation.
461 * The remaining VPIDs are managed by the unit number allocator.
463 vpid_unr = new_unrhdr(VM_MAXCPU + 1, 0xffff, NULL);
467 vmx_disable(void *arg __unused)
469 struct invvpid_desc invvpid_desc = { 0 };
470 struct invept_desc invept_desc = { 0 };
472 if (vmxon_enabled[curcpu]) {
474 * See sections 25.3.3.3 and 25.3.3.4 in Intel Vol 3b.
476 * VMXON or VMXOFF are not required to invalidate any TLB
477 * caching structures. This prevents potential retention of
478 * cached information in the TLB between distinct VMX episodes.
480 invvpid(INVVPID_TYPE_ALL_CONTEXTS, invvpid_desc);
481 invept(INVEPT_TYPE_ALL_CONTEXTS, invept_desc);
484 load_cr4(rcr4() & ~CR4_VMXE);
492 lapic_ipi_free(pirvec);
494 if (vpid_unr != NULL) {
495 delete_unrhdr(vpid_unr);
499 smp_rendezvous(NULL, vmx_disable, NULL, NULL);
505 vmx_enable(void *arg __unused)
508 uint64_t feature_control;
510 feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
511 if ((feature_control & IA32_FEATURE_CONTROL_LOCK) == 0 ||
512 (feature_control & IA32_FEATURE_CONTROL_VMX_EN) == 0) {
513 wrmsr(MSR_IA32_FEATURE_CONTROL,
514 feature_control | IA32_FEATURE_CONTROL_VMX_EN |
515 IA32_FEATURE_CONTROL_LOCK);
518 load_cr4(rcr4() | CR4_VMXE);
520 *(uint32_t *)vmxon_region[curcpu] = vmx_revision();
521 error = vmxon(vmxon_region[curcpu]);
523 vmxon_enabled[curcpu] = 1;
530 if (vmxon_enabled[curcpu])
531 vmxon(vmxon_region[curcpu]);
537 int error, use_tpr_shadow;
538 uint64_t basic, fixed0, fixed1, feature_control;
539 uint32_t tmp, procbased2_vid_bits;
541 /* CPUID.1:ECX[bit 5] must be 1 for processor to support VMX */
542 if (!(cpu_feature2 & CPUID2_VMX)) {
543 printf("vmx_init: processor does not support VMX operation\n");
548 * Verify that MSR_IA32_FEATURE_CONTROL lock and VMXON enable bits
549 * are set (bits 0 and 2 respectively).
551 feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
552 if ((feature_control & IA32_FEATURE_CONTROL_LOCK) == 1 &&
553 (feature_control & IA32_FEATURE_CONTROL_VMX_EN) == 0) {
554 printf("vmx_init: VMX operation disabled by BIOS\n");
559 * Verify capabilities MSR_VMX_BASIC:
560 * - bit 54 indicates support for INS/OUTS decoding
562 basic = rdmsr(MSR_VMX_BASIC);
563 if ((basic & (1UL << 54)) == 0) {
564 printf("vmx_init: processor does not support desired basic "
569 /* Check support for primary processor-based VM-execution controls */
570 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
571 MSR_VMX_TRUE_PROCBASED_CTLS,
572 PROCBASED_CTLS_ONE_SETTING,
573 PROCBASED_CTLS_ZERO_SETTING, &procbased_ctls);
575 printf("vmx_init: processor does not support desired primary "
576 "processor-based controls\n");
580 /* Clear the processor-based ctl bits that are set on demand */
581 procbased_ctls &= ~PROCBASED_CTLS_WINDOW_SETTING;
583 /* Check support for secondary processor-based VM-execution controls */
584 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
585 MSR_VMX_PROCBASED_CTLS2,
586 PROCBASED_CTLS2_ONE_SETTING,
587 PROCBASED_CTLS2_ZERO_SETTING, &procbased_ctls2);
589 printf("vmx_init: processor does not support desired secondary "
590 "processor-based controls\n");
594 /* Check support for VPID */
595 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2,
596 PROCBASED2_ENABLE_VPID, 0, &tmp);
598 procbased_ctls2 |= PROCBASED2_ENABLE_VPID;
600 /* Check support for pin-based VM-execution controls */
601 error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS,
602 MSR_VMX_TRUE_PINBASED_CTLS,
603 PINBASED_CTLS_ONE_SETTING,
604 PINBASED_CTLS_ZERO_SETTING, &pinbased_ctls);
606 printf("vmx_init: processor does not support desired "
607 "pin-based controls\n");
611 /* Check support for VM-exit controls */
612 error = vmx_set_ctlreg(MSR_VMX_EXIT_CTLS, MSR_VMX_TRUE_EXIT_CTLS,
613 VM_EXIT_CTLS_ONE_SETTING,
614 VM_EXIT_CTLS_ZERO_SETTING,
617 printf("vmx_init: processor does not support desired "
622 /* Check support for VM-entry controls */
623 error = vmx_set_ctlreg(MSR_VMX_ENTRY_CTLS, MSR_VMX_TRUE_ENTRY_CTLS,
624 VM_ENTRY_CTLS_ONE_SETTING, VM_ENTRY_CTLS_ZERO_SETTING,
627 printf("vmx_init: processor does not support desired "
633 * Check support for optional features by testing them
636 cap_halt_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
637 MSR_VMX_TRUE_PROCBASED_CTLS,
638 PROCBASED_HLT_EXITING, 0,
641 cap_monitor_trap = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
642 MSR_VMX_PROCBASED_CTLS,
646 cap_pause_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
647 MSR_VMX_TRUE_PROCBASED_CTLS,
648 PROCBASED_PAUSE_EXITING, 0,
651 cap_unrestricted_guest = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
652 MSR_VMX_PROCBASED_CTLS2,
653 PROCBASED2_UNRESTRICTED_GUEST, 0,
656 cap_invpcid = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
657 MSR_VMX_PROCBASED_CTLS2, PROCBASED2_ENABLE_INVPCID, 0,
661 * Check support for virtual interrupt delivery.
663 procbased2_vid_bits = (PROCBASED2_VIRTUALIZE_APIC_ACCESSES |
664 PROCBASED2_VIRTUALIZE_X2APIC_MODE |
665 PROCBASED2_APIC_REGISTER_VIRTUALIZATION |
666 PROCBASED2_VIRTUAL_INTERRUPT_DELIVERY);
668 use_tpr_shadow = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
669 MSR_VMX_TRUE_PROCBASED_CTLS, PROCBASED_USE_TPR_SHADOW, 0,
672 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2,
673 procbased2_vid_bits, 0, &tmp);
674 if (error == 0 && use_tpr_shadow) {
675 virtual_interrupt_delivery = 1;
676 TUNABLE_INT_FETCH("hw.vmm.vmx.use_apic_vid",
677 &virtual_interrupt_delivery);
680 if (virtual_interrupt_delivery) {
681 procbased_ctls |= PROCBASED_USE_TPR_SHADOW;
682 procbased_ctls2 |= procbased2_vid_bits;
683 procbased_ctls2 &= ~PROCBASED2_VIRTUALIZE_X2APIC_MODE;
686 * No need to emulate accesses to %CR8 if virtual
687 * interrupt delivery is enabled.
689 procbased_ctls &= ~PROCBASED_CR8_LOAD_EXITING;
690 procbased_ctls &= ~PROCBASED_CR8_STORE_EXITING;
693 * Check for Posted Interrupts only if Virtual Interrupt
694 * Delivery is enabled.
696 error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS,
697 MSR_VMX_TRUE_PINBASED_CTLS, PINBASED_POSTED_INTERRUPT, 0,
700 pirvec = lapic_ipi_alloc(pti ? &IDTVEC(justreturn1_pti) :
701 &IDTVEC(justreturn));
704 printf("vmx_init: unable to allocate "
705 "posted interrupt vector\n");
708 posted_interrupts = 1;
709 TUNABLE_INT_FETCH("hw.vmm.vmx.use_apic_pir",
715 if (posted_interrupts)
716 pinbased_ctls |= PINBASED_POSTED_INTERRUPT;
719 error = ept_init(ipinum);
721 printf("vmx_init: ept initialization failed (%d)\n", error);
726 * Stash the cr0 and cr4 bits that must be fixed to 0 or 1
728 fixed0 = rdmsr(MSR_VMX_CR0_FIXED0);
729 fixed1 = rdmsr(MSR_VMX_CR0_FIXED1);
730 cr0_ones_mask = fixed0 & fixed1;
731 cr0_zeros_mask = ~fixed0 & ~fixed1;
734 * CR0_PE and CR0_PG can be set to zero in VMX non-root operation
735 * if unrestricted guest execution is allowed.
737 if (cap_unrestricted_guest)
738 cr0_ones_mask &= ~(CR0_PG | CR0_PE);
741 * Do not allow the guest to set CR0_NW or CR0_CD.
743 cr0_zeros_mask |= (CR0_NW | CR0_CD);
745 fixed0 = rdmsr(MSR_VMX_CR4_FIXED0);
746 fixed1 = rdmsr(MSR_VMX_CR4_FIXED1);
747 cr4_ones_mask = fixed0 & fixed1;
748 cr4_zeros_mask = ~fixed0 & ~fixed1;
754 /* enable VMX operation */
755 smp_rendezvous(NULL, vmx_enable, NULL, NULL);
763 vmx_trigger_hostintr(int vector)
766 struct gate_descriptor *gd;
770 KASSERT(vector >= 32 && vector <= 255, ("vmx_trigger_hostintr: "
771 "invalid vector %d", vector));
772 KASSERT(gd->gd_p == 1, ("gate descriptor for vector %d not present",
774 KASSERT(gd->gd_type == SDT_SYSIGT, ("gate descriptor for vector %d "
775 "has invalid type %d", vector, gd->gd_type));
776 KASSERT(gd->gd_dpl == SEL_KPL, ("gate descriptor for vector %d "
777 "has invalid dpl %d", vector, gd->gd_dpl));
778 KASSERT(gd->gd_selector == GSEL(GCODE_SEL, SEL_KPL), ("gate descriptor "
779 "for vector %d has invalid selector %d", vector, gd->gd_selector));
780 KASSERT(gd->gd_ist == 0, ("gate descriptor for vector %d has invalid "
781 "IST %d", vector, gd->gd_ist));
783 func = ((long)gd->gd_hioffset << 16 | gd->gd_looffset);
788 vmx_setup_cr_shadow(int which, struct vmcs *vmcs, uint32_t initial)
790 int error, mask_ident, shadow_ident;
793 if (which != 0 && which != 4)
794 panic("vmx_setup_cr_shadow: unknown cr%d", which);
797 mask_ident = VMCS_CR0_MASK;
798 mask_value = cr0_ones_mask | cr0_zeros_mask;
799 shadow_ident = VMCS_CR0_SHADOW;
801 mask_ident = VMCS_CR4_MASK;
802 mask_value = cr4_ones_mask | cr4_zeros_mask;
803 shadow_ident = VMCS_CR4_SHADOW;
806 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(mask_ident), mask_value);
810 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(shadow_ident), initial);
816 #define vmx_setup_cr0_shadow(vmcs,init) vmx_setup_cr_shadow(0, (vmcs), (init))
817 #define vmx_setup_cr4_shadow(vmcs,init) vmx_setup_cr_shadow(4, (vmcs), (init))
820 vmx_vminit(struct vm *vm, pmap_t pmap)
822 uint16_t vpid[VM_MAXCPU];
828 vmx = malloc(sizeof(struct vmx), M_VMX, M_WAITOK | M_ZERO);
829 if ((uintptr_t)vmx & PAGE_MASK) {
830 panic("malloc of struct vmx not aligned on %d byte boundary",
835 vmx->eptp = eptp(vtophys((vm_offset_t)pmap->pm_pml4));
838 * Clean up EPTP-tagged guest physical and combined mappings
840 * VMX transitions are not required to invalidate any guest physical
841 * mappings. So, it may be possible for stale guest physical mappings
842 * to be present in the processor TLBs.
844 * Combined mappings for this EP4TA are also invalidated for all VPIDs.
846 ept_invalidate_mappings(vmx->eptp);
848 msr_bitmap_initialize(vmx->msr_bitmap);
851 * It is safe to allow direct access to MSR_GSBASE and MSR_FSBASE.
852 * The guest FSBASE and GSBASE are saved and restored during
853 * vm-exit and vm-entry respectively. The host FSBASE and GSBASE are
854 * always restored from the vmcs host state area on vm-exit.
856 * The SYSENTER_CS/ESP/EIP MSRs are identical to FS/GSBASE in
857 * how they are saved/restored so can be directly accessed by the
860 * MSR_EFER is saved and restored in the guest VMCS area on a
861 * VM exit and entry respectively. It is also restored from the
862 * host VMCS area on a VM exit.
864 * The TSC MSR is exposed read-only. Writes are disallowed as
865 * that will impact the host TSC. If the guest does a write
866 * the "use TSC offsetting" execution control is enabled and the
867 * difference between the host TSC and the guest TSC is written
868 * into the TSC offset in the VMCS.
870 if (guest_msr_rw(vmx, MSR_GSBASE) ||
871 guest_msr_rw(vmx, MSR_FSBASE) ||
872 guest_msr_rw(vmx, MSR_SYSENTER_CS_MSR) ||
873 guest_msr_rw(vmx, MSR_SYSENTER_ESP_MSR) ||
874 guest_msr_rw(vmx, MSR_SYSENTER_EIP_MSR) ||
875 guest_msr_rw(vmx, MSR_EFER) ||
876 guest_msr_ro(vmx, MSR_TSC))
877 panic("vmx_vminit: error setting guest msr access");
879 vpid_alloc(vpid, VM_MAXCPU);
881 if (virtual_interrupt_delivery) {
882 error = vm_map_mmio(vm, DEFAULT_APIC_BASE, PAGE_SIZE,
883 APIC_ACCESS_ADDRESS);
884 /* XXX this should really return an error to the caller */
885 KASSERT(error == 0, ("vm_map_mmio(apicbase) error %d", error));
888 for (i = 0; i < VM_MAXCPU; i++) {
889 vmcs = &vmx->vmcs[i];
890 vmcs->identifier = vmx_revision();
891 error = vmclear(vmcs);
893 panic("vmx_vminit: vmclear error %d on vcpu %d\n",
897 vmx_msr_guest_init(vmx, i);
899 error = vmcs_init(vmcs);
900 KASSERT(error == 0, ("vmcs_init error %d", error));
904 error += vmwrite(VMCS_HOST_RSP, (u_long)&vmx->ctx[i]);
905 error += vmwrite(VMCS_EPTP, vmx->eptp);
906 error += vmwrite(VMCS_PIN_BASED_CTLS, pinbased_ctls);
907 error += vmwrite(VMCS_PRI_PROC_BASED_CTLS, procbased_ctls);
908 error += vmwrite(VMCS_SEC_PROC_BASED_CTLS, procbased_ctls2);
909 error += vmwrite(VMCS_EXIT_CTLS, exit_ctls);
910 error += vmwrite(VMCS_ENTRY_CTLS, entry_ctls);
911 error += vmwrite(VMCS_MSR_BITMAP, vtophys(vmx->msr_bitmap));
912 error += vmwrite(VMCS_VPID, vpid[i]);
914 /* exception bitmap */
915 if (vcpu_trace_exceptions(vm, i))
916 exc_bitmap = 0xffffffff;
918 exc_bitmap = 1 << IDT_MC;
919 error += vmwrite(VMCS_EXCEPTION_BITMAP, exc_bitmap);
921 vmx->ctx[i].guest_dr6 = 0xffff0ff0;
922 error += vmwrite(VMCS_GUEST_DR7, 0x400);
924 if (virtual_interrupt_delivery) {
925 error += vmwrite(VMCS_APIC_ACCESS, APIC_ACCESS_ADDRESS);
926 error += vmwrite(VMCS_VIRTUAL_APIC,
927 vtophys(&vmx->apic_page[i]));
928 error += vmwrite(VMCS_EOI_EXIT0, 0);
929 error += vmwrite(VMCS_EOI_EXIT1, 0);
930 error += vmwrite(VMCS_EOI_EXIT2, 0);
931 error += vmwrite(VMCS_EOI_EXIT3, 0);
933 if (posted_interrupts) {
934 error += vmwrite(VMCS_PIR_VECTOR, pirvec);
935 error += vmwrite(VMCS_PIR_DESC,
936 vtophys(&vmx->pir_desc[i]));
939 KASSERT(error == 0, ("vmx_vminit: error customizing the vmcs"));
942 vmx->cap[i].proc_ctls = procbased_ctls;
943 vmx->cap[i].proc_ctls2 = procbased_ctls2;
945 vmx->state[i].nextrip = ~0;
946 vmx->state[i].lastcpu = NOCPU;
947 vmx->state[i].vpid = vpid[i];
950 * Set up the CR0/4 shadows, and init the read shadow
951 * to the power-on register value from the Intel Sys Arch.
955 error = vmx_setup_cr0_shadow(vmcs, 0x60000010);
957 panic("vmx_setup_cr0_shadow %d", error);
959 error = vmx_setup_cr4_shadow(vmcs, 0);
961 panic("vmx_setup_cr4_shadow %d", error);
963 vmx->ctx[i].pmap = pmap;
970 vmx_handle_cpuid(struct vm *vm, int vcpu, struct vmxctx *vmxctx)
974 func = vmxctx->guest_rax;
976 handled = x86_emulate_cpuid(vm, vcpu,
977 (uint32_t*)(&vmxctx->guest_rax),
978 (uint32_t*)(&vmxctx->guest_rbx),
979 (uint32_t*)(&vmxctx->guest_rcx),
980 (uint32_t*)(&vmxctx->guest_rdx));
985 vmx_run_trace(struct vmx *vmx, int vcpu)
988 VCPU_CTR1(vmx->vm, vcpu, "Resume execution at %#lx", vmcs_guest_rip());
993 vmx_exit_trace(struct vmx *vmx, int vcpu, uint64_t rip, uint32_t exit_reason,
997 VCPU_CTR3(vmx->vm, vcpu, "%s %s vmexit at 0x%0lx",
998 handled ? "handled" : "unhandled",
999 exit_reason_to_str(exit_reason), rip);
1003 static __inline void
1004 vmx_astpending_trace(struct vmx *vmx, int vcpu, uint64_t rip)
1007 VCPU_CTR1(vmx->vm, vcpu, "astpending vmexit at 0x%0lx", rip);
1011 static VMM_STAT_INTEL(VCPU_INVVPID_SAVED, "Number of vpid invalidations saved");
1012 static VMM_STAT_INTEL(VCPU_INVVPID_DONE, "Number of vpid invalidations done");
1015 * Invalidate guest mappings identified by its vpid from the TLB.
1017 static __inline void
1018 vmx_invvpid(struct vmx *vmx, int vcpu, pmap_t pmap, int running)
1020 struct vmxstate *vmxstate;
1021 struct invvpid_desc invvpid_desc;
1023 vmxstate = &vmx->state[vcpu];
1024 if (vmxstate->vpid == 0)
1029 * Set the 'lastcpu' to an invalid host cpu.
1031 * This will invalidate TLB entries tagged with the vcpu's
1032 * vpid the next time it runs via vmx_set_pcpu_defaults().
1034 vmxstate->lastcpu = NOCPU;
1038 KASSERT(curthread->td_critnest > 0, ("%s: vcpu %d running outside "
1039 "critical section", __func__, vcpu));
1042 * Invalidate all mappings tagged with 'vpid'
1044 * We do this because this vcpu was executing on a different host
1045 * cpu when it last ran. We do not track whether it invalidated
1046 * mappings associated with its 'vpid' during that run. So we must
1047 * assume that the mappings associated with 'vpid' on 'curcpu' are
1048 * stale and invalidate them.
1050 * Note that we incur this penalty only when the scheduler chooses to
1051 * move the thread associated with this vcpu between host cpus.
1053 * Note also that this will invalidate mappings tagged with 'vpid'
1056 if (pmap->pm_eptgen == vmx->eptgen[curcpu]) {
1057 invvpid_desc._res1 = 0;
1058 invvpid_desc._res2 = 0;
1059 invvpid_desc.vpid = vmxstate->vpid;
1060 invvpid_desc.linear_addr = 0;
1061 invvpid(INVVPID_TYPE_SINGLE_CONTEXT, invvpid_desc);
1062 vmm_stat_incr(vmx->vm, vcpu, VCPU_INVVPID_DONE, 1);
1065 * The invvpid can be skipped if an invept is going to
1066 * be performed before entering the guest. The invept
1067 * will invalidate combined mappings tagged with
1068 * 'vmx->eptp' for all vpids.
1070 vmm_stat_incr(vmx->vm, vcpu, VCPU_INVVPID_SAVED, 1);
1075 vmx_set_pcpu_defaults(struct vmx *vmx, int vcpu, pmap_t pmap)
1077 struct vmxstate *vmxstate;
1079 vmxstate = &vmx->state[vcpu];
1080 if (vmxstate->lastcpu == curcpu)
1083 vmxstate->lastcpu = curcpu;
1085 vmm_stat_incr(vmx->vm, vcpu, VCPU_MIGRATIONS, 1);
1087 vmcs_write(VMCS_HOST_TR_BASE, vmm_get_host_trbase());
1088 vmcs_write(VMCS_HOST_GDTR_BASE, vmm_get_host_gdtrbase());
1089 vmcs_write(VMCS_HOST_GS_BASE, vmm_get_host_gsbase());
1090 vmx_invvpid(vmx, vcpu, pmap, 1);
1094 * We depend on 'procbased_ctls' to have the Interrupt Window Exiting bit set.
1096 CTASSERT((PROCBASED_CTLS_ONE_SETTING & PROCBASED_INT_WINDOW_EXITING) != 0);
1098 static void __inline
1099 vmx_set_int_window_exiting(struct vmx *vmx, int vcpu)
1102 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) == 0) {
1103 vmx->cap[vcpu].proc_ctls |= PROCBASED_INT_WINDOW_EXITING;
1104 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1105 VCPU_CTR0(vmx->vm, vcpu, "Enabling interrupt window exiting");
1109 static void __inline
1110 vmx_clear_int_window_exiting(struct vmx *vmx, int vcpu)
1113 KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0,
1114 ("intr_window_exiting not set: %#x", vmx->cap[vcpu].proc_ctls));
1115 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_INT_WINDOW_EXITING;
1116 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1117 VCPU_CTR0(vmx->vm, vcpu, "Disabling interrupt window exiting");
1120 static void __inline
1121 vmx_set_nmi_window_exiting(struct vmx *vmx, int vcpu)
1124 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) == 0) {
1125 vmx->cap[vcpu].proc_ctls |= PROCBASED_NMI_WINDOW_EXITING;
1126 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1127 VCPU_CTR0(vmx->vm, vcpu, "Enabling NMI window exiting");
1131 static void __inline
1132 vmx_clear_nmi_window_exiting(struct vmx *vmx, int vcpu)
1135 KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) != 0,
1136 ("nmi_window_exiting not set %#x", vmx->cap[vcpu].proc_ctls));
1137 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_NMI_WINDOW_EXITING;
1138 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1139 VCPU_CTR0(vmx->vm, vcpu, "Disabling NMI window exiting");
1143 vmx_set_tsc_offset(struct vmx *vmx, int vcpu, uint64_t offset)
1147 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_TSC_OFFSET) == 0) {
1148 vmx->cap[vcpu].proc_ctls |= PROCBASED_TSC_OFFSET;
1149 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1150 VCPU_CTR0(vmx->vm, vcpu, "Enabling TSC offsetting");
1153 error = vmwrite(VMCS_TSC_OFFSET, offset);
1158 #define NMI_BLOCKING (VMCS_INTERRUPTIBILITY_NMI_BLOCKING | \
1159 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)
1160 #define HWINTR_BLOCKING (VMCS_INTERRUPTIBILITY_STI_BLOCKING | \
1161 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)
1164 vmx_inject_nmi(struct vmx *vmx, int vcpu)
1168 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1169 KASSERT((gi & NMI_BLOCKING) == 0, ("vmx_inject_nmi: invalid guest "
1170 "interruptibility-state %#x", gi));
1172 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1173 KASSERT((info & VMCS_INTR_VALID) == 0, ("vmx_inject_nmi: invalid "
1174 "VM-entry interruption information %#x", info));
1177 * Inject the virtual NMI. The vector must be the NMI IDT entry
1178 * or the VMCS entry check will fail.
1180 info = IDT_NMI | VMCS_INTR_T_NMI | VMCS_INTR_VALID;
1181 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1183 VCPU_CTR0(vmx->vm, vcpu, "Injecting vNMI");
1185 /* Clear the request */
1186 vm_nmi_clear(vmx->vm, vcpu);
1190 vmx_inject_interrupts(struct vmx *vmx, int vcpu, struct vlapic *vlapic,
1193 int vector, need_nmi_exiting, extint_pending;
1194 uint64_t rflags, entryinfo;
1197 if (vmx->state[vcpu].nextrip != guestrip) {
1198 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1199 if (gi & HWINTR_BLOCKING) {
1200 VCPU_CTR2(vmx->vm, vcpu, "Guest interrupt blocking "
1201 "cleared due to rip change: %#lx/%#lx",
1202 vmx->state[vcpu].nextrip, guestrip);
1203 gi &= ~HWINTR_BLOCKING;
1204 vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1208 if (vm_entry_intinfo(vmx->vm, vcpu, &entryinfo)) {
1209 KASSERT((entryinfo & VMCS_INTR_VALID) != 0, ("%s: entry "
1210 "intinfo is not valid: %#lx", __func__, entryinfo));
1212 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1213 KASSERT((info & VMCS_INTR_VALID) == 0, ("%s: cannot inject "
1214 "pending exception: %#lx/%#x", __func__, entryinfo, info));
1217 vector = info & 0xff;
1218 if (vector == IDT_BP || vector == IDT_OF) {
1220 * VT-x requires #BP and #OF to be injected as software
1223 info &= ~VMCS_INTR_T_MASK;
1224 info |= VMCS_INTR_T_SWEXCEPTION;
1227 if (info & VMCS_INTR_DEL_ERRCODE)
1228 vmcs_write(VMCS_ENTRY_EXCEPTION_ERROR, entryinfo >> 32);
1230 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1233 if (vm_nmi_pending(vmx->vm, vcpu)) {
1235 * If there are no conditions blocking NMI injection then
1236 * inject it directly here otherwise enable "NMI window
1237 * exiting" to inject it as soon as we can.
1239 * We also check for STI_BLOCKING because some implementations
1240 * don't allow NMI injection in this case. If we are running
1241 * on a processor that doesn't have this restriction it will
1242 * immediately exit and the NMI will be injected in the
1243 * "NMI window exiting" handler.
1245 need_nmi_exiting = 1;
1246 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1247 if ((gi & (HWINTR_BLOCKING | NMI_BLOCKING)) == 0) {
1248 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1249 if ((info & VMCS_INTR_VALID) == 0) {
1250 vmx_inject_nmi(vmx, vcpu);
1251 need_nmi_exiting = 0;
1253 VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI "
1254 "due to VM-entry intr info %#x", info);
1257 VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI due to "
1258 "Guest Interruptibility-state %#x", gi);
1261 if (need_nmi_exiting)
1262 vmx_set_nmi_window_exiting(vmx, vcpu);
1265 extint_pending = vm_extint_pending(vmx->vm, vcpu);
1267 if (!extint_pending && virtual_interrupt_delivery) {
1268 vmx_inject_pir(vlapic);
1273 * If interrupt-window exiting is already in effect then don't bother
1274 * checking for pending interrupts. This is just an optimization and
1275 * not needed for correctness.
1277 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0) {
1278 VCPU_CTR0(vmx->vm, vcpu, "Skip interrupt injection due to "
1279 "pending int_window_exiting");
1283 if (!extint_pending) {
1284 /* Ask the local apic for a vector to inject */
1285 if (!vlapic_pending_intr(vlapic, &vector))
1289 * From the Intel SDM, Volume 3, Section "Maskable
1290 * Hardware Interrupts":
1291 * - maskable interrupt vectors [16,255] can be delivered
1292 * through the local APIC.
1294 KASSERT(vector >= 16 && vector <= 255,
1295 ("invalid vector %d from local APIC", vector));
1297 /* Ask the legacy pic for a vector to inject */
1298 vatpic_pending_intr(vmx->vm, &vector);
1301 * From the Intel SDM, Volume 3, Section "Maskable
1302 * Hardware Interrupts":
1303 * - maskable interrupt vectors [0,255] can be delivered
1304 * through the INTR pin.
1306 KASSERT(vector >= 0 && vector <= 255,
1307 ("invalid vector %d from INTR", vector));
1310 /* Check RFLAGS.IF and the interruptibility state of the guest */
1311 rflags = vmcs_read(VMCS_GUEST_RFLAGS);
1312 if ((rflags & PSL_I) == 0) {
1313 VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1314 "rflags %#lx", vector, rflags);
1318 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1319 if (gi & HWINTR_BLOCKING) {
1320 VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1321 "Guest Interruptibility-state %#x", vector, gi);
1325 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1326 if (info & VMCS_INTR_VALID) {
1328 * This is expected and could happen for multiple reasons:
1329 * - A vectoring VM-entry was aborted due to astpending
1330 * - A VM-exit happened during event injection.
1331 * - An exception was injected above.
1332 * - An NMI was injected above or after "NMI window exiting"
1334 VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1335 "VM-entry intr info %#x", vector, info);
1339 /* Inject the interrupt */
1340 info = VMCS_INTR_T_HWINTR | VMCS_INTR_VALID;
1342 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1344 if (!extint_pending) {
1345 /* Update the Local APIC ISR */
1346 vlapic_intr_accepted(vlapic, vector);
1348 vm_extint_clear(vmx->vm, vcpu);
1349 vatpic_intr_accepted(vmx->vm, vector);
1352 * After we accepted the current ExtINT the PIC may
1353 * have posted another one. If that is the case, set
1354 * the Interrupt Window Exiting execution control so
1355 * we can inject that one too.
1357 * Also, interrupt window exiting allows us to inject any
1358 * pending APIC vector that was preempted by the ExtINT
1359 * as soon as possible. This applies both for the software
1360 * emulated vlapic and the hardware assisted virtual APIC.
1362 vmx_set_int_window_exiting(vmx, vcpu);
1365 VCPU_CTR1(vmx->vm, vcpu, "Injecting hwintr at vector %d", vector);
1371 * Set the Interrupt Window Exiting execution control so we can inject
1372 * the interrupt as soon as blocking condition goes away.
1374 vmx_set_int_window_exiting(vmx, vcpu);
1378 * If the Virtual NMIs execution control is '1' then the logical processor
1379 * tracks virtual-NMI blocking in the Guest Interruptibility-state field of
1380 * the VMCS. An IRET instruction in VMX non-root operation will remove any
1381 * virtual-NMI blocking.
1383 * This unblocking occurs even if the IRET causes a fault. In this case the
1384 * hypervisor needs to restore virtual-NMI blocking before resuming the guest.
1387 vmx_restore_nmi_blocking(struct vmx *vmx, int vcpuid)
1391 VCPU_CTR0(vmx->vm, vcpuid, "Restore Virtual-NMI blocking");
1392 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1393 gi |= VMCS_INTERRUPTIBILITY_NMI_BLOCKING;
1394 vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1398 vmx_clear_nmi_blocking(struct vmx *vmx, int vcpuid)
1402 VCPU_CTR0(vmx->vm, vcpuid, "Clear Virtual-NMI blocking");
1403 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1404 gi &= ~VMCS_INTERRUPTIBILITY_NMI_BLOCKING;
1405 vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1409 vmx_assert_nmi_blocking(struct vmx *vmx, int vcpuid)
1413 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1414 KASSERT(gi & VMCS_INTERRUPTIBILITY_NMI_BLOCKING,
1415 ("NMI blocking is not in effect %#x", gi));
1419 vmx_emulate_xsetbv(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
1421 struct vmxctx *vmxctx;
1423 const struct xsave_limits *limits;
1425 vmxctx = &vmx->ctx[vcpu];
1426 limits = vmm_get_xsave_limits();
1429 * Note that the processor raises a GP# fault on its own if
1430 * xsetbv is executed for CPL != 0, so we do not have to
1431 * emulate that fault here.
1434 /* Only xcr0 is supported. */
1435 if (vmxctx->guest_rcx != 0) {
1436 vm_inject_gp(vmx->vm, vcpu);
1440 /* We only handle xcr0 if both the host and guest have XSAVE enabled. */
1441 if (!limits->xsave_enabled || !(vmcs_read(VMCS_GUEST_CR4) & CR4_XSAVE)) {
1442 vm_inject_ud(vmx->vm, vcpu);
1446 xcrval = vmxctx->guest_rdx << 32 | (vmxctx->guest_rax & 0xffffffff);
1447 if ((xcrval & ~limits->xcr0_allowed) != 0) {
1448 vm_inject_gp(vmx->vm, vcpu);
1452 if (!(xcrval & XFEATURE_ENABLED_X87)) {
1453 vm_inject_gp(vmx->vm, vcpu);
1457 /* AVX (YMM_Hi128) requires SSE. */
1458 if (xcrval & XFEATURE_ENABLED_AVX &&
1459 (xcrval & XFEATURE_AVX) != XFEATURE_AVX) {
1460 vm_inject_gp(vmx->vm, vcpu);
1465 * AVX512 requires base AVX (YMM_Hi128) as well as OpMask,
1466 * ZMM_Hi256, and Hi16_ZMM.
1468 if (xcrval & XFEATURE_AVX512 &&
1469 (xcrval & (XFEATURE_AVX512 | XFEATURE_AVX)) !=
1470 (XFEATURE_AVX512 | XFEATURE_AVX)) {
1471 vm_inject_gp(vmx->vm, vcpu);
1476 * Intel MPX requires both bound register state flags to be
1479 if (((xcrval & XFEATURE_ENABLED_BNDREGS) != 0) !=
1480 ((xcrval & XFEATURE_ENABLED_BNDCSR) != 0)) {
1481 vm_inject_gp(vmx->vm, vcpu);
1486 * This runs "inside" vmrun() with the guest's FPU state, so
1487 * modifying xcr0 directly modifies the guest's xcr0, not the
1490 load_xcr(0, xcrval);
1495 vmx_get_guest_reg(struct vmx *vmx, int vcpu, int ident)
1497 const struct vmxctx *vmxctx;
1499 vmxctx = &vmx->ctx[vcpu];
1503 return (vmxctx->guest_rax);
1505 return (vmxctx->guest_rcx);
1507 return (vmxctx->guest_rdx);
1509 return (vmxctx->guest_rbx);
1511 return (vmcs_read(VMCS_GUEST_RSP));
1513 return (vmxctx->guest_rbp);
1515 return (vmxctx->guest_rsi);
1517 return (vmxctx->guest_rdi);
1519 return (vmxctx->guest_r8);
1521 return (vmxctx->guest_r9);
1523 return (vmxctx->guest_r10);
1525 return (vmxctx->guest_r11);
1527 return (vmxctx->guest_r12);
1529 return (vmxctx->guest_r13);
1531 return (vmxctx->guest_r14);
1533 return (vmxctx->guest_r15);
1535 panic("invalid vmx register %d", ident);
1540 vmx_set_guest_reg(struct vmx *vmx, int vcpu, int ident, uint64_t regval)
1542 struct vmxctx *vmxctx;
1544 vmxctx = &vmx->ctx[vcpu];
1548 vmxctx->guest_rax = regval;
1551 vmxctx->guest_rcx = regval;
1554 vmxctx->guest_rdx = regval;
1557 vmxctx->guest_rbx = regval;
1560 vmcs_write(VMCS_GUEST_RSP, regval);
1563 vmxctx->guest_rbp = regval;
1566 vmxctx->guest_rsi = regval;
1569 vmxctx->guest_rdi = regval;
1572 vmxctx->guest_r8 = regval;
1575 vmxctx->guest_r9 = regval;
1578 vmxctx->guest_r10 = regval;
1581 vmxctx->guest_r11 = regval;
1584 vmxctx->guest_r12 = regval;
1587 vmxctx->guest_r13 = regval;
1590 vmxctx->guest_r14 = regval;
1593 vmxctx->guest_r15 = regval;
1596 panic("invalid vmx register %d", ident);
1601 vmx_emulate_cr0_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1603 uint64_t crval, regval;
1605 /* We only handle mov to %cr0 at this time */
1606 if ((exitqual & 0xf0) != 0x00)
1609 regval = vmx_get_guest_reg(vmx, vcpu, (exitqual >> 8) & 0xf);
1611 vmcs_write(VMCS_CR0_SHADOW, regval);
1613 crval = regval | cr0_ones_mask;
1614 crval &= ~cr0_zeros_mask;
1615 vmcs_write(VMCS_GUEST_CR0, crval);
1617 if (regval & CR0_PG) {
1618 uint64_t efer, entry_ctls;
1621 * If CR0.PG is 1 and EFER.LME is 1 then EFER.LMA and
1622 * the "IA-32e mode guest" bit in VM-entry control must be
1625 efer = vmcs_read(VMCS_GUEST_IA32_EFER);
1626 if (efer & EFER_LME) {
1628 vmcs_write(VMCS_GUEST_IA32_EFER, efer);
1629 entry_ctls = vmcs_read(VMCS_ENTRY_CTLS);
1630 entry_ctls |= VM_ENTRY_GUEST_LMA;
1631 vmcs_write(VMCS_ENTRY_CTLS, entry_ctls);
1639 vmx_emulate_cr4_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1641 uint64_t crval, regval;
1643 /* We only handle mov to %cr4 at this time */
1644 if ((exitqual & 0xf0) != 0x00)
1647 regval = vmx_get_guest_reg(vmx, vcpu, (exitqual >> 8) & 0xf);
1649 vmcs_write(VMCS_CR4_SHADOW, regval);
1651 crval = regval | cr4_ones_mask;
1652 crval &= ~cr4_zeros_mask;
1653 vmcs_write(VMCS_GUEST_CR4, crval);
1659 vmx_emulate_cr8_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1661 struct vlapic *vlapic;
1665 /* We only handle mov %cr8 to/from a register at this time. */
1666 if ((exitqual & 0xe0) != 0x00) {
1670 vlapic = vm_lapic(vmx->vm, vcpu);
1671 regnum = (exitqual >> 8) & 0xf;
1672 if (exitqual & 0x10) {
1673 cr8 = vlapic_get_cr8(vlapic);
1674 vmx_set_guest_reg(vmx, vcpu, regnum, cr8);
1676 cr8 = vmx_get_guest_reg(vmx, vcpu, regnum);
1677 vlapic_set_cr8(vlapic, cr8);
1684 * From section "Guest Register State" in the Intel SDM: CPL = SS.DPL
1691 ssar = vmcs_read(VMCS_GUEST_SS_ACCESS_RIGHTS);
1692 return ((ssar >> 5) & 0x3);
1695 static enum vm_cpu_mode
1700 if (vmcs_read(VMCS_GUEST_IA32_EFER) & EFER_LMA) {
1701 csar = vmcs_read(VMCS_GUEST_CS_ACCESS_RIGHTS);
1703 return (CPU_MODE_64BIT); /* CS.L = 1 */
1705 return (CPU_MODE_COMPATIBILITY);
1706 } else if (vmcs_read(VMCS_GUEST_CR0) & CR0_PE) {
1707 return (CPU_MODE_PROTECTED);
1709 return (CPU_MODE_REAL);
1713 static enum vm_paging_mode
1714 vmx_paging_mode(void)
1717 if (!(vmcs_read(VMCS_GUEST_CR0) & CR0_PG))
1718 return (PAGING_MODE_FLAT);
1719 if (!(vmcs_read(VMCS_GUEST_CR4) & CR4_PAE))
1720 return (PAGING_MODE_32);
1721 if (vmcs_read(VMCS_GUEST_IA32_EFER) & EFER_LME)
1722 return (PAGING_MODE_64);
1724 return (PAGING_MODE_PAE);
1728 inout_str_index(struct vmx *vmx, int vcpuid, int in)
1732 enum vm_reg_name reg;
1734 reg = in ? VM_REG_GUEST_RDI : VM_REG_GUEST_RSI;
1735 error = vmx_getreg(vmx, vcpuid, reg, &val);
1736 KASSERT(error == 0, ("%s: vmx_getreg error %d", __func__, error));
1741 inout_str_count(struct vmx *vmx, int vcpuid, int rep)
1747 error = vmx_getreg(vmx, vcpuid, VM_REG_GUEST_RCX, &val);
1748 KASSERT(!error, ("%s: vmx_getreg error %d", __func__, error));
1756 inout_str_addrsize(uint32_t inst_info)
1760 size = (inst_info >> 7) & 0x7;
1763 return (2); /* 16 bit */
1765 return (4); /* 32 bit */
1767 return (8); /* 64 bit */
1769 panic("%s: invalid size encoding %d", __func__, size);
1774 inout_str_seginfo(struct vmx *vmx, int vcpuid, uint32_t inst_info, int in,
1775 struct vm_inout_str *vis)
1780 vis->seg_name = VM_REG_GUEST_ES;
1782 s = (inst_info >> 15) & 0x7;
1783 vis->seg_name = vm_segment_name(s);
1786 error = vmx_getdesc(vmx, vcpuid, vis->seg_name, &vis->seg_desc);
1787 KASSERT(error == 0, ("%s: vmx_getdesc error %d", __func__, error));
1791 vmx_paging_info(struct vm_guest_paging *paging)
1793 paging->cr3 = vmcs_guest_cr3();
1794 paging->cpl = vmx_cpl();
1795 paging->cpu_mode = vmx_cpu_mode();
1796 paging->paging_mode = vmx_paging_mode();
1800 vmexit_inst_emul(struct vm_exit *vmexit, uint64_t gpa, uint64_t gla)
1802 struct vm_guest_paging *paging;
1805 paging = &vmexit->u.inst_emul.paging;
1807 vmexit->exitcode = VM_EXITCODE_INST_EMUL;
1808 vmexit->inst_length = 0;
1809 vmexit->u.inst_emul.gpa = gpa;
1810 vmexit->u.inst_emul.gla = gla;
1811 vmx_paging_info(paging);
1812 switch (paging->cpu_mode) {
1814 vmexit->u.inst_emul.cs_base = vmcs_read(VMCS_GUEST_CS_BASE);
1815 vmexit->u.inst_emul.cs_d = 0;
1817 case CPU_MODE_PROTECTED:
1818 case CPU_MODE_COMPATIBILITY:
1819 vmexit->u.inst_emul.cs_base = vmcs_read(VMCS_GUEST_CS_BASE);
1820 csar = vmcs_read(VMCS_GUEST_CS_ACCESS_RIGHTS);
1821 vmexit->u.inst_emul.cs_d = SEG_DESC_DEF32(csar);
1824 vmexit->u.inst_emul.cs_base = 0;
1825 vmexit->u.inst_emul.cs_d = 0;
1828 vie_init(&vmexit->u.inst_emul.vie, NULL, 0);
1832 ept_fault_type(uint64_t ept_qual)
1836 if (ept_qual & EPT_VIOLATION_DATA_WRITE)
1837 fault_type = VM_PROT_WRITE;
1838 else if (ept_qual & EPT_VIOLATION_INST_FETCH)
1839 fault_type = VM_PROT_EXECUTE;
1841 fault_type= VM_PROT_READ;
1843 return (fault_type);
1847 ept_emulation_fault(uint64_t ept_qual)
1851 /* EPT fault on an instruction fetch doesn't make sense here */
1852 if (ept_qual & EPT_VIOLATION_INST_FETCH)
1855 /* EPT fault must be a read fault or a write fault */
1856 read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0;
1857 write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0;
1858 if ((read | write) == 0)
1862 * The EPT violation must have been caused by accessing a
1863 * guest-physical address that is a translation of a guest-linear
1866 if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 ||
1867 (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) {
1875 apic_access_virtualization(struct vmx *vmx, int vcpuid)
1877 uint32_t proc_ctls2;
1879 proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
1880 return ((proc_ctls2 & PROCBASED2_VIRTUALIZE_APIC_ACCESSES) ? 1 : 0);
1884 x2apic_virtualization(struct vmx *vmx, int vcpuid)
1886 uint32_t proc_ctls2;
1888 proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
1889 return ((proc_ctls2 & PROCBASED2_VIRTUALIZE_X2APIC_MODE) ? 1 : 0);
1893 vmx_handle_apic_write(struct vmx *vmx, int vcpuid, struct vlapic *vlapic,
1896 int error, handled, offset;
1897 uint32_t *apic_regs, vector;
1901 offset = APIC_WRITE_OFFSET(qual);
1903 if (!apic_access_virtualization(vmx, vcpuid)) {
1905 * In general there should not be any APIC write VM-exits
1906 * unless APIC-access virtualization is enabled.
1908 * However self-IPI virtualization can legitimately trigger
1909 * an APIC-write VM-exit so treat it specially.
1911 if (x2apic_virtualization(vmx, vcpuid) &&
1912 offset == APIC_OFFSET_SELF_IPI) {
1913 apic_regs = (uint32_t *)(vlapic->apic_page);
1914 vector = apic_regs[APIC_OFFSET_SELF_IPI / 4];
1915 vlapic_self_ipi_handler(vlapic, vector);
1922 case APIC_OFFSET_ID:
1923 vlapic_id_write_handler(vlapic);
1925 case APIC_OFFSET_LDR:
1926 vlapic_ldr_write_handler(vlapic);
1928 case APIC_OFFSET_DFR:
1929 vlapic_dfr_write_handler(vlapic);
1931 case APIC_OFFSET_SVR:
1932 vlapic_svr_write_handler(vlapic);
1934 case APIC_OFFSET_ESR:
1935 vlapic_esr_write_handler(vlapic);
1937 case APIC_OFFSET_ICR_LOW:
1939 error = vlapic_icrlo_write_handler(vlapic, &retu);
1940 if (error != 0 || retu)
1941 handled = UNHANDLED;
1943 case APIC_OFFSET_CMCI_LVT:
1944 case APIC_OFFSET_TIMER_LVT ... APIC_OFFSET_ERROR_LVT:
1945 vlapic_lvt_write_handler(vlapic, offset);
1947 case APIC_OFFSET_TIMER_ICR:
1948 vlapic_icrtmr_write_handler(vlapic);
1950 case APIC_OFFSET_TIMER_DCR:
1951 vlapic_dcr_write_handler(vlapic);
1954 handled = UNHANDLED;
1961 apic_access_fault(struct vmx *vmx, int vcpuid, uint64_t gpa)
1964 if (apic_access_virtualization(vmx, vcpuid) &&
1965 (gpa >= DEFAULT_APIC_BASE && gpa < DEFAULT_APIC_BASE + PAGE_SIZE))
1972 vmx_handle_apic_access(struct vmx *vmx, int vcpuid, struct vm_exit *vmexit)
1975 int access_type, offset, allowed;
1977 if (!apic_access_virtualization(vmx, vcpuid))
1980 qual = vmexit->u.vmx.exit_qualification;
1981 access_type = APIC_ACCESS_TYPE(qual);
1982 offset = APIC_ACCESS_OFFSET(qual);
1985 if (access_type == 0) {
1987 * Read data access to the following registers is expected.
1990 case APIC_OFFSET_APR:
1991 case APIC_OFFSET_PPR:
1992 case APIC_OFFSET_RRR:
1993 case APIC_OFFSET_CMCI_LVT:
1994 case APIC_OFFSET_TIMER_CCR:
2000 } else if (access_type == 1) {
2002 * Write data access to the following registers is expected.
2005 case APIC_OFFSET_VER:
2006 case APIC_OFFSET_APR:
2007 case APIC_OFFSET_PPR:
2008 case APIC_OFFSET_RRR:
2009 case APIC_OFFSET_ISR0 ... APIC_OFFSET_ISR7:
2010 case APIC_OFFSET_TMR0 ... APIC_OFFSET_TMR7:
2011 case APIC_OFFSET_IRR0 ... APIC_OFFSET_IRR7:
2012 case APIC_OFFSET_CMCI_LVT:
2013 case APIC_OFFSET_TIMER_CCR:
2022 vmexit_inst_emul(vmexit, DEFAULT_APIC_BASE + offset,
2027 * Regardless of whether the APIC-access is allowed this handler
2028 * always returns UNHANDLED:
2029 * - if the access is allowed then it is handled by emulating the
2030 * instruction that caused the VM-exit (outside the critical section)
2031 * - if the access is not allowed then it will be converted to an
2032 * exitcode of VM_EXITCODE_VMX and will be dealt with in userland.
2037 static enum task_switch_reason
2038 vmx_task_switch_reason(uint64_t qual)
2042 reason = (qual >> 30) & 0x3;
2051 return (TSR_IDT_GATE);
2053 panic("%s: invalid reason %d", __func__, reason);
2058 emulate_wrmsr(struct vmx *vmx, int vcpuid, u_int num, uint64_t val, bool *retu)
2063 error = lapic_wrmsr(vmx->vm, vcpuid, num, val, retu);
2065 error = vmx_wrmsr(vmx, vcpuid, num, val, retu);
2071 emulate_rdmsr(struct vmx *vmx, int vcpuid, u_int num, bool *retu)
2073 struct vmxctx *vmxctx;
2079 error = lapic_rdmsr(vmx->vm, vcpuid, num, &result, retu);
2081 error = vmx_rdmsr(vmx, vcpuid, num, &result, retu);
2085 vmxctx = &vmx->ctx[vcpuid];
2086 error = vmxctx_setreg(vmxctx, VM_REG_GUEST_RAX, eax);
2087 KASSERT(error == 0, ("vmxctx_setreg(rax) error %d", error));
2090 error = vmxctx_setreg(vmxctx, VM_REG_GUEST_RDX, edx);
2091 KASSERT(error == 0, ("vmxctx_setreg(rdx) error %d", error));
2098 vmx_exit_process(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
2100 int error, errcode, errcode_valid, handled, in;
2101 struct vmxctx *vmxctx;
2102 struct vlapic *vlapic;
2103 struct vm_inout_str *vis;
2104 struct vm_task_switch *ts;
2105 uint32_t eax, ecx, edx, idtvec_info, idtvec_err, intr_info, inst_info;
2106 uint32_t intr_type, intr_vec, reason;
2107 uint64_t exitintinfo, qual, gpa;
2110 CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_VIRTUAL_NMI) != 0);
2111 CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_NMI_EXITING) != 0);
2113 handled = UNHANDLED;
2114 vmxctx = &vmx->ctx[vcpu];
2116 qual = vmexit->u.vmx.exit_qualification;
2117 reason = vmexit->u.vmx.exit_reason;
2118 vmexit->exitcode = VM_EXITCODE_BOGUS;
2120 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_COUNT, 1);
2123 * VM-entry failures during or after loading guest state.
2125 * These VM-exits are uncommon but must be handled specially
2126 * as most VM-exit fields are not populated as usual.
2128 if (__predict_false(reason == EXIT_REASON_MCE_DURING_ENTRY)) {
2129 VCPU_CTR0(vmx->vm, vcpu, "Handling MCE during VM-entry");
2130 __asm __volatile("int $18");
2135 * VM exits that can be triggered during event delivery need to
2136 * be handled specially by re-injecting the event if the IDT
2137 * vectoring information field's valid bit is set.
2139 * See "Information for VM Exits During Event Delivery" in Intel SDM
2142 idtvec_info = vmcs_idt_vectoring_info();
2143 if (idtvec_info & VMCS_IDT_VEC_VALID) {
2144 idtvec_info &= ~(1 << 12); /* clear undefined bit */
2145 exitintinfo = idtvec_info;
2146 if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) {
2147 idtvec_err = vmcs_idt_vectoring_err();
2148 exitintinfo |= (uint64_t)idtvec_err << 32;
2150 error = vm_exit_intinfo(vmx->vm, vcpu, exitintinfo);
2151 KASSERT(error == 0, ("%s: vm_set_intinfo error %d",
2155 * If 'virtual NMIs' are being used and the VM-exit
2156 * happened while injecting an NMI during the previous
2157 * VM-entry, then clear "blocking by NMI" in the
2158 * Guest Interruptibility-State so the NMI can be
2159 * reinjected on the subsequent VM-entry.
2161 * However, if the NMI was being delivered through a task
2162 * gate, then the new task must start execution with NMIs
2163 * blocked so don't clear NMI blocking in this case.
2165 intr_type = idtvec_info & VMCS_INTR_T_MASK;
2166 if (intr_type == VMCS_INTR_T_NMI) {
2167 if (reason != EXIT_REASON_TASK_SWITCH)
2168 vmx_clear_nmi_blocking(vmx, vcpu);
2170 vmx_assert_nmi_blocking(vmx, vcpu);
2174 * Update VM-entry instruction length if the event being
2175 * delivered was a software interrupt or software exception.
2177 if (intr_type == VMCS_INTR_T_SWINTR ||
2178 intr_type == VMCS_INTR_T_PRIV_SWEXCEPTION ||
2179 intr_type == VMCS_INTR_T_SWEXCEPTION) {
2180 vmcs_write(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length);
2185 case EXIT_REASON_TASK_SWITCH:
2186 ts = &vmexit->u.task_switch;
2187 ts->tsssel = qual & 0xffff;
2188 ts->reason = vmx_task_switch_reason(qual);
2190 ts->errcode_valid = 0;
2191 vmx_paging_info(&ts->paging);
2193 * If the task switch was due to a CALL, JMP, IRET, software
2194 * interrupt (INT n) or software exception (INT3, INTO),
2195 * then the saved %rip references the instruction that caused
2196 * the task switch. The instruction length field in the VMCS
2197 * is valid in this case.
2199 * In all other cases (e.g., NMI, hardware exception) the
2200 * saved %rip is one that would have been saved in the old TSS
2201 * had the task switch completed normally so the instruction
2202 * length field is not needed in this case and is explicitly
2205 if (ts->reason == TSR_IDT_GATE) {
2206 KASSERT(idtvec_info & VMCS_IDT_VEC_VALID,
2207 ("invalid idtvec_info %#x for IDT task switch",
2209 intr_type = idtvec_info & VMCS_INTR_T_MASK;
2210 if (intr_type != VMCS_INTR_T_SWINTR &&
2211 intr_type != VMCS_INTR_T_SWEXCEPTION &&
2212 intr_type != VMCS_INTR_T_PRIV_SWEXCEPTION) {
2213 /* Task switch triggered by external event */
2215 vmexit->inst_length = 0;
2216 if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) {
2217 ts->errcode_valid = 1;
2218 ts->errcode = vmcs_idt_vectoring_err();
2222 vmexit->exitcode = VM_EXITCODE_TASK_SWITCH;
2223 VCPU_CTR4(vmx->vm, vcpu, "task switch reason %d, tss 0x%04x, "
2224 "%s errcode 0x%016lx", ts->reason, ts->tsssel,
2225 ts->ext ? "external" : "internal",
2226 ((uint64_t)ts->errcode << 32) | ts->errcode_valid);
2228 case EXIT_REASON_CR_ACCESS:
2229 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CR_ACCESS, 1);
2230 switch (qual & 0xf) {
2232 handled = vmx_emulate_cr0_access(vmx, vcpu, qual);
2235 handled = vmx_emulate_cr4_access(vmx, vcpu, qual);
2238 handled = vmx_emulate_cr8_access(vmx, vcpu, qual);
2242 case EXIT_REASON_RDMSR:
2243 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_RDMSR, 1);
2245 ecx = vmxctx->guest_rcx;
2246 VCPU_CTR1(vmx->vm, vcpu, "rdmsr 0x%08x", ecx);
2247 error = emulate_rdmsr(vmx, vcpu, ecx, &retu);
2249 vmexit->exitcode = VM_EXITCODE_RDMSR;
2250 vmexit->u.msr.code = ecx;
2254 /* Return to userspace with a valid exitcode */
2255 KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
2256 ("emulate_rdmsr retu with bogus exitcode"));
2259 case EXIT_REASON_WRMSR:
2260 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_WRMSR, 1);
2262 eax = vmxctx->guest_rax;
2263 ecx = vmxctx->guest_rcx;
2264 edx = vmxctx->guest_rdx;
2265 VCPU_CTR2(vmx->vm, vcpu, "wrmsr 0x%08x value 0x%016lx",
2266 ecx, (uint64_t)edx << 32 | eax);
2267 error = emulate_wrmsr(vmx, vcpu, ecx,
2268 (uint64_t)edx << 32 | eax, &retu);
2270 vmexit->exitcode = VM_EXITCODE_WRMSR;
2271 vmexit->u.msr.code = ecx;
2272 vmexit->u.msr.wval = (uint64_t)edx << 32 | eax;
2276 /* Return to userspace with a valid exitcode */
2277 KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
2278 ("emulate_wrmsr retu with bogus exitcode"));
2281 case EXIT_REASON_HLT:
2282 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_HLT, 1);
2283 vmexit->exitcode = VM_EXITCODE_HLT;
2284 vmexit->u.hlt.rflags = vmcs_read(VMCS_GUEST_RFLAGS);
2285 if (virtual_interrupt_delivery)
2286 vmexit->u.hlt.intr_status =
2287 vmcs_read(VMCS_GUEST_INTR_STATUS);
2289 vmexit->u.hlt.intr_status = 0;
2291 case EXIT_REASON_MTF:
2292 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_MTRAP, 1);
2293 vmexit->exitcode = VM_EXITCODE_MTRAP;
2294 vmexit->inst_length = 0;
2296 case EXIT_REASON_PAUSE:
2297 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_PAUSE, 1);
2298 vmexit->exitcode = VM_EXITCODE_PAUSE;
2300 case EXIT_REASON_INTR_WINDOW:
2301 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INTR_WINDOW, 1);
2302 vmx_clear_int_window_exiting(vmx, vcpu);
2304 case EXIT_REASON_EXT_INTR:
2306 * External interrupts serve only to cause VM exits and allow
2307 * the host interrupt handler to run.
2309 * If this external interrupt triggers a virtual interrupt
2310 * to a VM, then that state will be recorded by the
2311 * host interrupt handler in the VM's softc. We will inject
2312 * this virtual interrupt during the subsequent VM enter.
2314 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
2317 * XXX: Ignore this exit if VMCS_INTR_VALID is not set.
2318 * This appears to be a bug in VMware Fusion?
2320 if (!(intr_info & VMCS_INTR_VALID))
2322 KASSERT((intr_info & VMCS_INTR_VALID) != 0 &&
2323 (intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_HWINTR,
2324 ("VM exit interruption info invalid: %#x", intr_info));
2325 vmx_trigger_hostintr(intr_info & 0xff);
2328 * This is special. We want to treat this as an 'handled'
2329 * VM-exit but not increment the instruction pointer.
2331 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXTINT, 1);
2333 case EXIT_REASON_NMI_WINDOW:
2334 /* Exit to allow the pending virtual NMI to be injected */
2335 if (vm_nmi_pending(vmx->vm, vcpu))
2336 vmx_inject_nmi(vmx, vcpu);
2337 vmx_clear_nmi_window_exiting(vmx, vcpu);
2338 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NMI_WINDOW, 1);
2340 case EXIT_REASON_INOUT:
2341 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INOUT, 1);
2342 vmexit->exitcode = VM_EXITCODE_INOUT;
2343 vmexit->u.inout.bytes = (qual & 0x7) + 1;
2344 vmexit->u.inout.in = in = (qual & 0x8) ? 1 : 0;
2345 vmexit->u.inout.string = (qual & 0x10) ? 1 : 0;
2346 vmexit->u.inout.rep = (qual & 0x20) ? 1 : 0;
2347 vmexit->u.inout.port = (uint16_t)(qual >> 16);
2348 vmexit->u.inout.eax = (uint32_t)(vmxctx->guest_rax);
2349 if (vmexit->u.inout.string) {
2350 inst_info = vmcs_read(VMCS_EXIT_INSTRUCTION_INFO);
2351 vmexit->exitcode = VM_EXITCODE_INOUT_STR;
2352 vis = &vmexit->u.inout_str;
2353 vmx_paging_info(&vis->paging);
2354 vis->rflags = vmcs_read(VMCS_GUEST_RFLAGS);
2355 vis->cr0 = vmcs_read(VMCS_GUEST_CR0);
2356 vis->index = inout_str_index(vmx, vcpu, in);
2357 vis->count = inout_str_count(vmx, vcpu, vis->inout.rep);
2358 vis->addrsize = inout_str_addrsize(inst_info);
2359 inout_str_seginfo(vmx, vcpu, inst_info, in, vis);
2362 case EXIT_REASON_CPUID:
2363 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CPUID, 1);
2364 handled = vmx_handle_cpuid(vmx->vm, vcpu, vmxctx);
2366 case EXIT_REASON_EXCEPTION:
2367 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXCEPTION, 1);
2368 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
2369 KASSERT((intr_info & VMCS_INTR_VALID) != 0,
2370 ("VM exit interruption info invalid: %#x", intr_info));
2372 intr_vec = intr_info & 0xff;
2373 intr_type = intr_info & VMCS_INTR_T_MASK;
2376 * If Virtual NMIs control is 1 and the VM-exit is due to a
2377 * fault encountered during the execution of IRET then we must
2378 * restore the state of "virtual-NMI blocking" before resuming
2381 * See "Resuming Guest Software after Handling an Exception".
2382 * See "Information for VM Exits Due to Vectored Events".
2384 if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 &&
2385 (intr_vec != IDT_DF) &&
2386 (intr_info & EXIT_QUAL_NMIUDTI) != 0)
2387 vmx_restore_nmi_blocking(vmx, vcpu);
2390 * The NMI has already been handled in vmx_exit_handle_nmi().
2392 if (intr_type == VMCS_INTR_T_NMI)
2396 * Call the machine check handler by hand. Also don't reflect
2397 * the machine check back into the guest.
2399 if (intr_vec == IDT_MC) {
2400 VCPU_CTR0(vmx->vm, vcpu, "Vectoring to MCE handler");
2401 __asm __volatile("int $18");
2405 if (intr_vec == IDT_PF) {
2406 error = vmxctx_setreg(vmxctx, VM_REG_GUEST_CR2, qual);
2407 KASSERT(error == 0, ("%s: vmxctx_setreg(cr2) error %d",
2412 * Software exceptions exhibit trap-like behavior. This in
2413 * turn requires populating the VM-entry instruction length
2414 * so that the %rip in the trap frame is past the INT3/INTO
2417 if (intr_type == VMCS_INTR_T_SWEXCEPTION)
2418 vmcs_write(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length);
2420 /* Reflect all other exceptions back into the guest */
2421 errcode_valid = errcode = 0;
2422 if (intr_info & VMCS_INTR_DEL_ERRCODE) {
2424 errcode = vmcs_read(VMCS_EXIT_INTR_ERRCODE);
2426 VCPU_CTR2(vmx->vm, vcpu, "Reflecting exception %d/%#x into "
2427 "the guest", intr_vec, errcode);
2428 error = vm_inject_exception(vmx->vm, vcpu, intr_vec,
2429 errcode_valid, errcode, 0);
2430 KASSERT(error == 0, ("%s: vm_inject_exception error %d",
2434 case EXIT_REASON_EPT_FAULT:
2436 * If 'gpa' lies within the address space allocated to
2437 * memory then this must be a nested page fault otherwise
2438 * this must be an instruction that accesses MMIO space.
2441 if (vm_mem_allocated(vmx->vm, vcpu, gpa) ||
2442 apic_access_fault(vmx, vcpu, gpa)) {
2443 vmexit->exitcode = VM_EXITCODE_PAGING;
2444 vmexit->inst_length = 0;
2445 vmexit->u.paging.gpa = gpa;
2446 vmexit->u.paging.fault_type = ept_fault_type(qual);
2447 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NESTED_FAULT, 1);
2448 } else if (ept_emulation_fault(qual)) {
2449 vmexit_inst_emul(vmexit, gpa, vmcs_gla());
2450 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INST_EMUL, 1);
2453 * If Virtual NMIs control is 1 and the VM-exit is due to an
2454 * EPT fault during the execution of IRET then we must restore
2455 * the state of "virtual-NMI blocking" before resuming.
2457 * See description of "NMI unblocking due to IRET" in
2458 * "Exit Qualification for EPT Violations".
2460 if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 &&
2461 (qual & EXIT_QUAL_NMIUDTI) != 0)
2462 vmx_restore_nmi_blocking(vmx, vcpu);
2464 case EXIT_REASON_VIRTUALIZED_EOI:
2465 vmexit->exitcode = VM_EXITCODE_IOAPIC_EOI;
2466 vmexit->u.ioapic_eoi.vector = qual & 0xFF;
2467 vmexit->inst_length = 0; /* trap-like */
2469 case EXIT_REASON_APIC_ACCESS:
2470 handled = vmx_handle_apic_access(vmx, vcpu, vmexit);
2472 case EXIT_REASON_APIC_WRITE:
2474 * APIC-write VM exit is trap-like so the %rip is already
2475 * pointing to the next instruction.
2477 vmexit->inst_length = 0;
2478 vlapic = vm_lapic(vmx->vm, vcpu);
2479 handled = vmx_handle_apic_write(vmx, vcpu, vlapic, qual);
2481 case EXIT_REASON_XSETBV:
2482 handled = vmx_emulate_xsetbv(vmx, vcpu, vmexit);
2484 case EXIT_REASON_MONITOR:
2485 vmexit->exitcode = VM_EXITCODE_MONITOR;
2487 case EXIT_REASON_MWAIT:
2488 vmexit->exitcode = VM_EXITCODE_MWAIT;
2491 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_UNKNOWN, 1);
2497 * It is possible that control is returned to userland
2498 * even though we were able to handle the VM exit in the
2501 * In such a case we want to make sure that the userland
2502 * restarts guest execution at the instruction *after*
2503 * the one we just processed. Therefore we update the
2504 * guest rip in the VMCS and in 'vmexit'.
2506 vmexit->rip += vmexit->inst_length;
2507 vmexit->inst_length = 0;
2508 vmcs_write(VMCS_GUEST_RIP, vmexit->rip);
2510 if (vmexit->exitcode == VM_EXITCODE_BOGUS) {
2512 * If this VM exit was not claimed by anybody then
2513 * treat it as a generic VMX exit.
2515 vmexit->exitcode = VM_EXITCODE_VMX;
2516 vmexit->u.vmx.status = VM_SUCCESS;
2517 vmexit->u.vmx.inst_type = 0;
2518 vmexit->u.vmx.inst_error = 0;
2521 * The exitcode and collateral have been populated.
2522 * The VM exit will be processed further in userland.
2529 static __inline void
2530 vmx_exit_inst_error(struct vmxctx *vmxctx, int rc, struct vm_exit *vmexit)
2533 KASSERT(vmxctx->inst_fail_status != VM_SUCCESS,
2534 ("vmx_exit_inst_error: invalid inst_fail_status %d",
2535 vmxctx->inst_fail_status));
2537 vmexit->inst_length = 0;
2538 vmexit->exitcode = VM_EXITCODE_VMX;
2539 vmexit->u.vmx.status = vmxctx->inst_fail_status;
2540 vmexit->u.vmx.inst_error = vmcs_instruction_error();
2541 vmexit->u.vmx.exit_reason = ~0;
2542 vmexit->u.vmx.exit_qualification = ~0;
2545 case VMX_VMRESUME_ERROR:
2546 case VMX_VMLAUNCH_ERROR:
2547 case VMX_INVEPT_ERROR:
2548 vmexit->u.vmx.inst_type = rc;
2551 panic("vm_exit_inst_error: vmx_enter_guest returned %d", rc);
2556 * If the NMI-exiting VM execution control is set to '1' then an NMI in
2557 * non-root operation causes a VM-exit. NMI blocking is in effect so it is
2558 * sufficient to simply vector to the NMI handler via a software interrupt.
2559 * However, this must be done before maskable interrupts are enabled
2560 * otherwise the "iret" issued by an interrupt handler will incorrectly
2561 * clear NMI blocking.
2563 static __inline void
2564 vmx_exit_handle_nmi(struct vmx *vmx, int vcpuid, struct vm_exit *vmexit)
2568 KASSERT((read_rflags() & PSL_I) == 0, ("interrupts enabled"));
2570 if (vmexit->u.vmx.exit_reason != EXIT_REASON_EXCEPTION)
2573 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
2574 KASSERT((intr_info & VMCS_INTR_VALID) != 0,
2575 ("VM exit interruption info invalid: %#x", intr_info));
2577 if ((intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_NMI) {
2578 KASSERT((intr_info & 0xff) == IDT_NMI, ("VM exit due "
2579 "to NMI has invalid vector: %#x", intr_info));
2580 VCPU_CTR0(vmx->vm, vcpuid, "Vectoring to NMI handler");
2581 __asm __volatile("int $2");
2585 static __inline void
2586 vmx_dr_enter_guest(struct vmxctx *vmxctx)
2590 /* Save host control debug registers. */
2591 vmxctx->host_dr7 = rdr7();
2592 vmxctx->host_debugctl = rdmsr(MSR_DEBUGCTLMSR);
2595 * Disable debugging in DR7 and DEBUGCTL to avoid triggering
2596 * exceptions in the host based on the guest DRx values. The
2597 * guest DR7 and DEBUGCTL are saved/restored in the VMCS.
2600 wrmsr(MSR_DEBUGCTLMSR, 0);
2603 * Disable single stepping the kernel to avoid corrupting the
2604 * guest DR6. A debugger might still be able to corrupt the
2605 * guest DR6 by setting a breakpoint after this point and then
2608 rflags = read_rflags();
2609 vmxctx->host_tf = rflags & PSL_T;
2610 write_rflags(rflags & ~PSL_T);
2612 /* Save host debug registers. */
2613 vmxctx->host_dr0 = rdr0();
2614 vmxctx->host_dr1 = rdr1();
2615 vmxctx->host_dr2 = rdr2();
2616 vmxctx->host_dr3 = rdr3();
2617 vmxctx->host_dr6 = rdr6();
2619 /* Restore guest debug registers. */
2620 load_dr0(vmxctx->guest_dr0);
2621 load_dr1(vmxctx->guest_dr1);
2622 load_dr2(vmxctx->guest_dr2);
2623 load_dr3(vmxctx->guest_dr3);
2624 load_dr6(vmxctx->guest_dr6);
2627 static __inline void
2628 vmx_dr_leave_guest(struct vmxctx *vmxctx)
2631 /* Save guest debug registers. */
2632 vmxctx->guest_dr0 = rdr0();
2633 vmxctx->guest_dr1 = rdr1();
2634 vmxctx->guest_dr2 = rdr2();
2635 vmxctx->guest_dr3 = rdr3();
2636 vmxctx->guest_dr6 = rdr6();
2639 * Restore host debug registers. Restore DR7, DEBUGCTL, and
2642 load_dr0(vmxctx->host_dr0);
2643 load_dr1(vmxctx->host_dr1);
2644 load_dr2(vmxctx->host_dr2);
2645 load_dr3(vmxctx->host_dr3);
2646 load_dr6(vmxctx->host_dr6);
2647 wrmsr(MSR_DEBUGCTLMSR, vmxctx->host_debugctl);
2648 load_dr7(vmxctx->host_dr7);
2649 write_rflags(read_rflags() | vmxctx->host_tf);
2653 vmx_run(void *arg, int vcpu, register_t rip, pmap_t pmap,
2654 struct vm_eventinfo *evinfo)
2656 int rc, handled, launched;
2659 struct vmxctx *vmxctx;
2661 struct vm_exit *vmexit;
2662 struct vlapic *vlapic;
2663 uint32_t exit_reason;
2667 vmcs = &vmx->vmcs[vcpu];
2668 vmxctx = &vmx->ctx[vcpu];
2669 vlapic = vm_lapic(vm, vcpu);
2670 vmexit = vm_exitinfo(vm, vcpu);
2673 KASSERT(vmxctx->pmap == pmap,
2674 ("pmap %p different than ctx pmap %p", pmap, vmxctx->pmap));
2676 vmx_msr_guest_enter(vmx, vcpu);
2682 * We do this every time because we may setup the virtual machine
2683 * from a different process than the one that actually runs it.
2685 * If the life of a virtual machine was spent entirely in the context
2686 * of a single process we could do this once in vmx_vminit().
2688 vmcs_write(VMCS_HOST_CR3, rcr3());
2690 vmcs_write(VMCS_GUEST_RIP, rip);
2691 vmx_set_pcpu_defaults(vmx, vcpu, pmap);
2693 KASSERT(vmcs_guest_rip() == rip, ("%s: vmcs guest rip mismatch "
2694 "%#lx/%#lx", __func__, vmcs_guest_rip(), rip));
2696 handled = UNHANDLED;
2698 * Interrupts are disabled from this point on until the
2699 * guest starts executing. This is done for the following
2702 * If an AST is asserted on this thread after the check below,
2703 * then the IPI_AST notification will not be lost, because it
2704 * will cause a VM exit due to external interrupt as soon as
2705 * the guest state is loaded.
2707 * A posted interrupt after 'vmx_inject_interrupts()' will
2708 * not be "lost" because it will be held pending in the host
2709 * APIC because interrupts are disabled. The pending interrupt
2710 * will be recognized as soon as the guest state is loaded.
2712 * The same reasoning applies to the IPI generated by
2713 * pmap_invalidate_ept().
2716 vmx_inject_interrupts(vmx, vcpu, vlapic, rip);
2719 * Check for vcpu suspension after injecting events because
2720 * vmx_inject_interrupts() can suspend the vcpu due to a
2723 if (vcpu_suspended(evinfo)) {
2725 vm_exit_suspended(vmx->vm, vcpu, rip);
2729 if (vcpu_rendezvous_pending(evinfo)) {
2731 vm_exit_rendezvous(vmx->vm, vcpu, rip);
2735 if (vcpu_reqidle(evinfo)) {
2737 vm_exit_reqidle(vmx->vm, vcpu, rip);
2741 if (vcpu_should_yield(vm, vcpu)) {
2743 vm_exit_astpending(vmx->vm, vcpu, rip);
2744 vmx_astpending_trace(vmx, vcpu, rip);
2749 vmx_run_trace(vmx, vcpu);
2750 vmx_dr_enter_guest(vmxctx);
2751 rc = vmx_enter_guest(vmxctx, vmx, launched);
2752 vmx_dr_leave_guest(vmxctx);
2754 /* Collect some information for VM exit processing */
2755 vmexit->rip = rip = vmcs_guest_rip();
2756 vmexit->inst_length = vmexit_instruction_length();
2757 vmexit->u.vmx.exit_reason = exit_reason = vmcs_exit_reason();
2758 vmexit->u.vmx.exit_qualification = vmcs_exit_qualification();
2760 /* Update 'nextrip' */
2761 vmx->state[vcpu].nextrip = rip;
2763 if (rc == VMX_GUEST_VMEXIT) {
2764 vmx_exit_handle_nmi(vmx, vcpu, vmexit);
2766 handled = vmx_exit_process(vmx, vcpu, vmexit);
2769 vmx_exit_inst_error(vmxctx, rc, vmexit);
2772 vmx_exit_trace(vmx, vcpu, rip, exit_reason, handled);
2777 * If a VM exit has been handled then the exitcode must be BOGUS
2778 * If a VM exit is not handled then the exitcode must not be BOGUS
2780 if ((handled && vmexit->exitcode != VM_EXITCODE_BOGUS) ||
2781 (!handled && vmexit->exitcode == VM_EXITCODE_BOGUS)) {
2782 panic("Mismatch between handled (%d) and exitcode (%d)",
2783 handled, vmexit->exitcode);
2787 vmm_stat_incr(vm, vcpu, VMEXIT_USERSPACE, 1);
2789 VCPU_CTR1(vm, vcpu, "returning from vmx_run: exitcode %d",
2793 vmx_msr_guest_exit(vmx, vcpu);
2799 vmx_vmcleanup(void *arg)
2802 struct vmx *vmx = arg;
2804 if (apic_access_virtualization(vmx, 0))
2805 vm_unmap_mmio(vmx->vm, DEFAULT_APIC_BASE, PAGE_SIZE);
2807 for (i = 0; i < VM_MAXCPU; i++)
2808 vpid_free(vmx->state[i].vpid);
2816 vmxctx_regptr(struct vmxctx *vmxctx, int reg)
2820 case VM_REG_GUEST_RAX:
2821 return (&vmxctx->guest_rax);
2822 case VM_REG_GUEST_RBX:
2823 return (&vmxctx->guest_rbx);
2824 case VM_REG_GUEST_RCX:
2825 return (&vmxctx->guest_rcx);
2826 case VM_REG_GUEST_RDX:
2827 return (&vmxctx->guest_rdx);
2828 case VM_REG_GUEST_RSI:
2829 return (&vmxctx->guest_rsi);
2830 case VM_REG_GUEST_RDI:
2831 return (&vmxctx->guest_rdi);
2832 case VM_REG_GUEST_RBP:
2833 return (&vmxctx->guest_rbp);
2834 case VM_REG_GUEST_R8:
2835 return (&vmxctx->guest_r8);
2836 case VM_REG_GUEST_R9:
2837 return (&vmxctx->guest_r9);
2838 case VM_REG_GUEST_R10:
2839 return (&vmxctx->guest_r10);
2840 case VM_REG_GUEST_R11:
2841 return (&vmxctx->guest_r11);
2842 case VM_REG_GUEST_R12:
2843 return (&vmxctx->guest_r12);
2844 case VM_REG_GUEST_R13:
2845 return (&vmxctx->guest_r13);
2846 case VM_REG_GUEST_R14:
2847 return (&vmxctx->guest_r14);
2848 case VM_REG_GUEST_R15:
2849 return (&vmxctx->guest_r15);
2850 case VM_REG_GUEST_CR2:
2851 return (&vmxctx->guest_cr2);
2852 case VM_REG_GUEST_DR0:
2853 return (&vmxctx->guest_dr0);
2854 case VM_REG_GUEST_DR1:
2855 return (&vmxctx->guest_dr1);
2856 case VM_REG_GUEST_DR2:
2857 return (&vmxctx->guest_dr2);
2858 case VM_REG_GUEST_DR3:
2859 return (&vmxctx->guest_dr3);
2860 case VM_REG_GUEST_DR6:
2861 return (&vmxctx->guest_dr6);
2869 vmxctx_getreg(struct vmxctx *vmxctx, int reg, uint64_t *retval)
2873 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
2881 vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val)
2885 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
2893 vmx_get_intr_shadow(struct vmx *vmx, int vcpu, int running, uint64_t *retval)
2898 error = vmcs_getreg(&vmx->vmcs[vcpu], running,
2899 VMCS_IDENT(VMCS_GUEST_INTERRUPTIBILITY), &gi);
2900 *retval = (gi & HWINTR_BLOCKING) ? 1 : 0;
2905 vmx_modify_intr_shadow(struct vmx *vmx, int vcpu, int running, uint64_t val)
2912 * Forcing the vcpu into an interrupt shadow is not supported.
2919 vmcs = &vmx->vmcs[vcpu];
2920 ident = VMCS_IDENT(VMCS_GUEST_INTERRUPTIBILITY);
2921 error = vmcs_getreg(vmcs, running, ident, &gi);
2923 gi &= ~HWINTR_BLOCKING;
2924 error = vmcs_setreg(vmcs, running, ident, gi);
2927 VCPU_CTR2(vmx->vm, vcpu, "Setting intr_shadow to %#lx %s", val,
2928 error ? "failed" : "succeeded");
2933 vmx_shadow_reg(int reg)
2940 case VM_REG_GUEST_CR0:
2941 shreg = VMCS_CR0_SHADOW;
2943 case VM_REG_GUEST_CR4:
2944 shreg = VMCS_CR4_SHADOW;
2954 vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval)
2956 int running, hostcpu;
2957 struct vmx *vmx = arg;
2959 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
2960 if (running && hostcpu != curcpu)
2961 panic("vmx_getreg: %s%d is running", vm_name(vmx->vm), vcpu);
2963 if (reg == VM_REG_GUEST_INTR_SHADOW)
2964 return (vmx_get_intr_shadow(vmx, vcpu, running, retval));
2966 if (vmxctx_getreg(&vmx->ctx[vcpu], reg, retval) == 0)
2969 return (vmcs_getreg(&vmx->vmcs[vcpu], running, reg, retval));
2973 vmx_setreg(void *arg, int vcpu, int reg, uint64_t val)
2975 int error, hostcpu, running, shadow;
2978 struct vmx *vmx = arg;
2980 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
2981 if (running && hostcpu != curcpu)
2982 panic("vmx_setreg: %s%d is running", vm_name(vmx->vm), vcpu);
2984 if (reg == VM_REG_GUEST_INTR_SHADOW)
2985 return (vmx_modify_intr_shadow(vmx, vcpu, running, val));
2987 if (vmxctx_setreg(&vmx->ctx[vcpu], reg, val) == 0)
2990 error = vmcs_setreg(&vmx->vmcs[vcpu], running, reg, val);
2994 * If the "load EFER" VM-entry control is 1 then the
2995 * value of EFER.LMA must be identical to "IA-32e mode guest"
2996 * bit in the VM-entry control.
2998 if ((entry_ctls & VM_ENTRY_LOAD_EFER) != 0 &&
2999 (reg == VM_REG_GUEST_EFER)) {
3000 vmcs_getreg(&vmx->vmcs[vcpu], running,
3001 VMCS_IDENT(VMCS_ENTRY_CTLS), &ctls);
3003 ctls |= VM_ENTRY_GUEST_LMA;
3005 ctls &= ~VM_ENTRY_GUEST_LMA;
3006 vmcs_setreg(&vmx->vmcs[vcpu], running,
3007 VMCS_IDENT(VMCS_ENTRY_CTLS), ctls);
3010 shadow = vmx_shadow_reg(reg);
3013 * Store the unmodified value in the shadow
3015 error = vmcs_setreg(&vmx->vmcs[vcpu], running,
3016 VMCS_IDENT(shadow), val);
3019 if (reg == VM_REG_GUEST_CR3) {
3021 * Invalidate the guest vcpu's TLB mappings to emulate
3022 * the behavior of updating %cr3.
3024 * XXX the processor retains global mappings when %cr3
3025 * is updated but vmx_invvpid() does not.
3027 pmap = vmx->ctx[vcpu].pmap;
3028 vmx_invvpid(vmx, vcpu, pmap, running);
3036 vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
3038 int hostcpu, running;
3039 struct vmx *vmx = arg;
3041 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
3042 if (running && hostcpu != curcpu)
3043 panic("vmx_getdesc: %s%d is running", vm_name(vmx->vm), vcpu);
3045 return (vmcs_getdesc(&vmx->vmcs[vcpu], running, reg, desc));
3049 vmx_setdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
3051 int hostcpu, running;
3052 struct vmx *vmx = arg;
3054 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
3055 if (running && hostcpu != curcpu)
3056 panic("vmx_setdesc: %s%d is running", vm_name(vmx->vm), vcpu);
3058 return (vmcs_setdesc(&vmx->vmcs[vcpu], running, reg, desc));
3062 vmx_getcap(void *arg, int vcpu, int type, int *retval)
3064 struct vmx *vmx = arg;
3070 vcap = vmx->cap[vcpu].set;
3073 case VM_CAP_HALT_EXIT:
3077 case VM_CAP_PAUSE_EXIT:
3081 case VM_CAP_MTRAP_EXIT:
3082 if (cap_monitor_trap)
3085 case VM_CAP_UNRESTRICTED_GUEST:
3086 if (cap_unrestricted_guest)
3089 case VM_CAP_ENABLE_INVPCID:
3098 *retval = (vcap & (1 << type)) ? 1 : 0;
3104 vmx_setcap(void *arg, int vcpu, int type, int val)
3106 struct vmx *vmx = arg;
3107 struct vmcs *vmcs = &vmx->vmcs[vcpu];
3119 case VM_CAP_HALT_EXIT:
3120 if (cap_halt_exit) {
3122 pptr = &vmx->cap[vcpu].proc_ctls;
3124 flag = PROCBASED_HLT_EXITING;
3125 reg = VMCS_PRI_PROC_BASED_CTLS;
3128 case VM_CAP_MTRAP_EXIT:
3129 if (cap_monitor_trap) {
3131 pptr = &vmx->cap[vcpu].proc_ctls;
3133 flag = PROCBASED_MTF;
3134 reg = VMCS_PRI_PROC_BASED_CTLS;
3137 case VM_CAP_PAUSE_EXIT:
3138 if (cap_pause_exit) {
3140 pptr = &vmx->cap[vcpu].proc_ctls;
3142 flag = PROCBASED_PAUSE_EXITING;
3143 reg = VMCS_PRI_PROC_BASED_CTLS;
3146 case VM_CAP_UNRESTRICTED_GUEST:
3147 if (cap_unrestricted_guest) {
3149 pptr = &vmx->cap[vcpu].proc_ctls2;
3151 flag = PROCBASED2_UNRESTRICTED_GUEST;
3152 reg = VMCS_SEC_PROC_BASED_CTLS;
3155 case VM_CAP_ENABLE_INVPCID:
3158 pptr = &vmx->cap[vcpu].proc_ctls2;
3160 flag = PROCBASED2_ENABLE_INVPCID;
3161 reg = VMCS_SEC_PROC_BASED_CTLS;
3175 error = vmwrite(reg, baseval);
3182 * Update optional stored flags, and record
3190 vmx->cap[vcpu].set |= (1 << type);
3192 vmx->cap[vcpu].set &= ~(1 << type);
3201 struct vlapic vlapic;
3202 struct pir_desc *pir_desc;
3206 #define VMX_CTR_PIR(vm, vcpuid, pir_desc, notify, vector, level, msg) \
3208 VCPU_CTR2(vm, vcpuid, msg " assert %s-triggered vector %d", \
3209 level ? "level" : "edge", vector); \
3210 VCPU_CTR1(vm, vcpuid, msg " pir0 0x%016lx", pir_desc->pir[0]); \
3211 VCPU_CTR1(vm, vcpuid, msg " pir1 0x%016lx", pir_desc->pir[1]); \
3212 VCPU_CTR1(vm, vcpuid, msg " pir2 0x%016lx", pir_desc->pir[2]); \
3213 VCPU_CTR1(vm, vcpuid, msg " pir3 0x%016lx", pir_desc->pir[3]); \
3214 VCPU_CTR1(vm, vcpuid, msg " notify: %s", notify ? "yes" : "no");\
3218 * vlapic->ops handlers that utilize the APICv hardware assist described in
3219 * Chapter 29 of the Intel SDM.
3222 vmx_set_intr_ready(struct vlapic *vlapic, int vector, bool level)
3224 struct vlapic_vtx *vlapic_vtx;
3225 struct pir_desc *pir_desc;
3229 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3230 pir_desc = vlapic_vtx->pir_desc;
3233 * Keep track of interrupt requests in the PIR descriptor. This is
3234 * because the virtual APIC page pointed to by the VMCS cannot be
3235 * modified if the vcpu is running.
3238 mask = 1UL << (vector % 64);
3239 atomic_set_long(&pir_desc->pir[idx], mask);
3240 notify = atomic_cmpset_long(&pir_desc->pending, 0, 1);
3242 VMX_CTR_PIR(vlapic->vm, vlapic->vcpuid, pir_desc, notify, vector,
3243 level, "vmx_set_intr_ready");
3248 vmx_pending_intr(struct vlapic *vlapic, int *vecptr)
3250 struct vlapic_vtx *vlapic_vtx;
3251 struct pir_desc *pir_desc;
3252 struct LAPIC *lapic;
3253 uint64_t pending, pirval;
3258 * This function is only expected to be called from the 'HLT' exit
3259 * handler which does not care about the vector that is pending.
3261 KASSERT(vecptr == NULL, ("vmx_pending_intr: vecptr must be NULL"));
3263 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3264 pir_desc = vlapic_vtx->pir_desc;
3266 pending = atomic_load_acq_long(&pir_desc->pending);
3269 * While a virtual interrupt may have already been
3270 * processed the actual delivery maybe pending the
3271 * interruptibility of the guest. Recognize a pending
3272 * interrupt by reevaluating virtual interrupts
3273 * following Section 29.2.1 in the Intel SDM Volume 3.
3275 struct vm_exit *vmexit;
3278 vmexit = vm_exitinfo(vlapic->vm, vlapic->vcpuid);
3279 KASSERT(vmexit->exitcode == VM_EXITCODE_HLT,
3280 ("vmx_pending_intr: exitcode not 'HLT'"));
3281 rvi = vmexit->u.hlt.intr_status & APIC_TPR_INT;
3282 lapic = vlapic->apic_page;
3283 ppr = lapic->ppr & APIC_TPR_INT;
3292 * If there is an interrupt pending then it will be recognized only
3293 * if its priority is greater than the processor priority.
3295 * Special case: if the processor priority is zero then any pending
3296 * interrupt will be recognized.
3298 lapic = vlapic->apic_page;
3299 ppr = lapic->ppr & APIC_TPR_INT;
3303 VCPU_CTR1(vlapic->vm, vlapic->vcpuid, "HLT with non-zero PPR %d",
3306 for (i = 3; i >= 0; i--) {
3307 pirval = pir_desc->pir[i];
3309 vpr = (i * 64 + flsl(pirval) - 1) & APIC_TPR_INT;
3317 vmx_intr_accepted(struct vlapic *vlapic, int vector)
3320 panic("vmx_intr_accepted: not expected to be called");
3324 vmx_set_tmr(struct vlapic *vlapic, int vector, bool level)
3326 struct vlapic_vtx *vlapic_vtx;
3331 KASSERT(vector >= 0 && vector <= 255, ("invalid vector %d", vector));
3332 KASSERT(!vcpu_is_running(vlapic->vm, vlapic->vcpuid, NULL),
3333 ("vmx_set_tmr: vcpu cannot be running"));
3335 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3336 vmx = vlapic_vtx->vmx;
3337 vmcs = &vmx->vmcs[vlapic->vcpuid];
3338 mask = 1UL << (vector % 64);
3341 val = vmcs_read(VMCS_EOI_EXIT(vector));
3346 vmcs_write(VMCS_EOI_EXIT(vector), val);
3351 vmx_enable_x2apic_mode(struct vlapic *vlapic)
3355 uint32_t proc_ctls2;
3358 vcpuid = vlapic->vcpuid;
3359 vmx = ((struct vlapic_vtx *)vlapic)->vmx;
3360 vmcs = &vmx->vmcs[vcpuid];
3362 proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
3363 KASSERT((proc_ctls2 & PROCBASED2_VIRTUALIZE_APIC_ACCESSES) != 0,
3364 ("%s: invalid proc_ctls2 %#x", __func__, proc_ctls2));
3366 proc_ctls2 &= ~PROCBASED2_VIRTUALIZE_APIC_ACCESSES;
3367 proc_ctls2 |= PROCBASED2_VIRTUALIZE_X2APIC_MODE;
3368 vmx->cap[vcpuid].proc_ctls2 = proc_ctls2;
3371 vmcs_write(VMCS_SEC_PROC_BASED_CTLS, proc_ctls2);
3374 if (vlapic->vcpuid == 0) {
3376 * The nested page table mappings are shared by all vcpus
3377 * so unmap the APIC access page just once.
3379 error = vm_unmap_mmio(vmx->vm, DEFAULT_APIC_BASE, PAGE_SIZE);
3380 KASSERT(error == 0, ("%s: vm_unmap_mmio error %d",
3384 * The MSR bitmap is shared by all vcpus so modify it only
3385 * once in the context of vcpu 0.
3387 error = vmx_allow_x2apic_msrs(vmx);
3388 KASSERT(error == 0, ("%s: vmx_allow_x2apic_msrs error %d",
3394 vmx_post_intr(struct vlapic *vlapic, int hostcpu)
3397 ipi_cpu(hostcpu, pirvec);
3401 * Transfer the pending interrupts in the PIR descriptor to the IRR
3402 * in the virtual APIC page.
3405 vmx_inject_pir(struct vlapic *vlapic)
3407 struct vlapic_vtx *vlapic_vtx;
3408 struct pir_desc *pir_desc;
3409 struct LAPIC *lapic;
3410 uint64_t val, pirval;
3411 int rvi, pirbase = -1;
3412 uint16_t intr_status_old, intr_status_new;
3414 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3415 pir_desc = vlapic_vtx->pir_desc;
3416 if (atomic_cmpset_long(&pir_desc->pending, 1, 0) == 0) {
3417 VCPU_CTR0(vlapic->vm, vlapic->vcpuid, "vmx_inject_pir: "
3418 "no posted interrupt pending");
3424 lapic = vlapic->apic_page;
3426 val = atomic_readandclear_long(&pir_desc->pir[0]);
3429 lapic->irr1 |= val >> 32;
3434 val = atomic_readandclear_long(&pir_desc->pir[1]);
3437 lapic->irr3 |= val >> 32;
3442 val = atomic_readandclear_long(&pir_desc->pir[2]);
3445 lapic->irr5 |= val >> 32;
3450 val = atomic_readandclear_long(&pir_desc->pir[3]);
3453 lapic->irr7 |= val >> 32;
3458 VLAPIC_CTR_IRR(vlapic, "vmx_inject_pir");
3461 * Update RVI so the processor can evaluate pending virtual
3462 * interrupts on VM-entry.
3464 * It is possible for pirval to be 0 here, even though the
3465 * pending bit has been set. The scenario is:
3466 * CPU-Y is sending a posted interrupt to CPU-X, which
3467 * is running a guest and processing posted interrupts in h/w.
3468 * CPU-X will eventually exit and the state seen in s/w is
3469 * the pending bit set, but no PIR bits set.
3472 * (vm running) (host running)
3473 * rx posted interrupt
3476 * READ/CLEAR PIR bits
3479 * pending bit set, PIR 0
3482 rvi = pirbase + flsl(pirval) - 1;
3483 intr_status_old = vmcs_read(VMCS_GUEST_INTR_STATUS);
3484 intr_status_new = (intr_status_old & 0xFF00) | rvi;
3485 if (intr_status_new > intr_status_old) {
3486 vmcs_write(VMCS_GUEST_INTR_STATUS, intr_status_new);
3487 VCPU_CTR2(vlapic->vm, vlapic->vcpuid, "vmx_inject_pir: "
3488 "guest_intr_status changed from 0x%04x to 0x%04x",
3489 intr_status_old, intr_status_new);
3494 static struct vlapic *
3495 vmx_vlapic_init(void *arg, int vcpuid)
3498 struct vlapic *vlapic;
3499 struct vlapic_vtx *vlapic_vtx;
3503 vlapic = malloc(sizeof(struct vlapic_vtx), M_VLAPIC, M_WAITOK | M_ZERO);
3504 vlapic->vm = vmx->vm;
3505 vlapic->vcpuid = vcpuid;
3506 vlapic->apic_page = (struct LAPIC *)&vmx->apic_page[vcpuid];
3508 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3509 vlapic_vtx->pir_desc = &vmx->pir_desc[vcpuid];
3510 vlapic_vtx->vmx = vmx;
3512 if (virtual_interrupt_delivery) {
3513 vlapic->ops.set_intr_ready = vmx_set_intr_ready;
3514 vlapic->ops.pending_intr = vmx_pending_intr;
3515 vlapic->ops.intr_accepted = vmx_intr_accepted;
3516 vlapic->ops.set_tmr = vmx_set_tmr;
3517 vlapic->ops.enable_x2apic_mode = vmx_enable_x2apic_mode;
3520 if (posted_interrupts)
3521 vlapic->ops.post_intr = vmx_post_intr;
3523 vlapic_init(vlapic);
3529 vmx_vlapic_cleanup(void *arg, struct vlapic *vlapic)
3532 vlapic_cleanup(vlapic);
3533 free(vlapic, M_VLAPIC);
3536 struct vmm_ops vmm_ops_intel = {