2 * Copyright (c) 2011 NetApp, Inc.
4 * Copyright (c) 2018 Joyent, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
40 #include <sys/sysctl.h>
45 #include <machine/psl.h>
46 #include <machine/cpufunc.h>
47 #include <machine/md_var.h>
48 #include <machine/reg.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);
190 static int guest_l1d_flush;
191 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, l1d_flush, CTLFLAG_RD,
192 &guest_l1d_flush, 0, NULL);
193 static int guest_l1d_flush_sw;
194 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, l1d_flush_sw, CTLFLAG_RD,
195 &guest_l1d_flush_sw, 0, NULL);
197 static struct msr_entry msr_load_list[1] __aligned(16);
200 * Use the last page below 4GB as the APIC access address. This address is
201 * occupied by the boot firmware so it is guaranteed that it will not conflict
202 * with a page in system memory.
204 #define APIC_ACCESS_ADDRESS 0xFFFFF000
206 static int vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc);
207 static int vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval);
208 static int vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val);
209 static void vmx_inject_pir(struct vlapic *vlapic);
213 exit_reason_to_str(int reason)
215 static char reasonbuf[32];
218 case EXIT_REASON_EXCEPTION:
220 case EXIT_REASON_EXT_INTR:
222 case EXIT_REASON_TRIPLE_FAULT:
223 return "triplefault";
224 case EXIT_REASON_INIT:
226 case EXIT_REASON_SIPI:
228 case EXIT_REASON_IO_SMI:
230 case EXIT_REASON_SMI:
232 case EXIT_REASON_INTR_WINDOW:
234 case EXIT_REASON_NMI_WINDOW:
236 case EXIT_REASON_TASK_SWITCH:
238 case EXIT_REASON_CPUID:
240 case EXIT_REASON_GETSEC:
242 case EXIT_REASON_HLT:
244 case EXIT_REASON_INVD:
246 case EXIT_REASON_INVLPG:
248 case EXIT_REASON_RDPMC:
250 case EXIT_REASON_RDTSC:
252 case EXIT_REASON_RSM:
254 case EXIT_REASON_VMCALL:
256 case EXIT_REASON_VMCLEAR:
258 case EXIT_REASON_VMLAUNCH:
260 case EXIT_REASON_VMPTRLD:
262 case EXIT_REASON_VMPTRST:
264 case EXIT_REASON_VMREAD:
266 case EXIT_REASON_VMRESUME:
268 case EXIT_REASON_VMWRITE:
270 case EXIT_REASON_VMXOFF:
272 case EXIT_REASON_VMXON:
274 case EXIT_REASON_CR_ACCESS:
276 case EXIT_REASON_DR_ACCESS:
278 case EXIT_REASON_INOUT:
280 case EXIT_REASON_RDMSR:
282 case EXIT_REASON_WRMSR:
284 case EXIT_REASON_INVAL_VMCS:
286 case EXIT_REASON_INVAL_MSR:
288 case EXIT_REASON_MWAIT:
290 case EXIT_REASON_MTF:
292 case EXIT_REASON_MONITOR:
294 case EXIT_REASON_PAUSE:
296 case EXIT_REASON_MCE_DURING_ENTRY:
297 return "mce-during-entry";
298 case EXIT_REASON_TPR:
300 case EXIT_REASON_APIC_ACCESS:
301 return "apic-access";
302 case EXIT_REASON_GDTR_IDTR:
304 case EXIT_REASON_LDTR_TR:
306 case EXIT_REASON_EPT_FAULT:
308 case EXIT_REASON_EPT_MISCONFIG:
309 return "eptmisconfig";
310 case EXIT_REASON_INVEPT:
312 case EXIT_REASON_RDTSCP:
314 case EXIT_REASON_VMX_PREEMPT:
316 case EXIT_REASON_INVVPID:
318 case EXIT_REASON_WBINVD:
320 case EXIT_REASON_XSETBV:
322 case EXIT_REASON_APIC_WRITE:
325 snprintf(reasonbuf, sizeof(reasonbuf), "%d", reason);
332 vmx_allow_x2apic_msrs(struct vmx *vmx)
339 * Allow readonly access to the following x2APIC MSRs from the guest.
341 error += guest_msr_ro(vmx, MSR_APIC_ID);
342 error += guest_msr_ro(vmx, MSR_APIC_VERSION);
343 error += guest_msr_ro(vmx, MSR_APIC_LDR);
344 error += guest_msr_ro(vmx, MSR_APIC_SVR);
346 for (i = 0; i < 8; i++)
347 error += guest_msr_ro(vmx, MSR_APIC_ISR0 + i);
349 for (i = 0; i < 8; i++)
350 error += guest_msr_ro(vmx, MSR_APIC_TMR0 + i);
352 for (i = 0; i < 8; i++)
353 error += guest_msr_ro(vmx, MSR_APIC_IRR0 + i);
355 error += guest_msr_ro(vmx, MSR_APIC_ESR);
356 error += guest_msr_ro(vmx, MSR_APIC_LVT_TIMER);
357 error += guest_msr_ro(vmx, MSR_APIC_LVT_THERMAL);
358 error += guest_msr_ro(vmx, MSR_APIC_LVT_PCINT);
359 error += guest_msr_ro(vmx, MSR_APIC_LVT_LINT0);
360 error += guest_msr_ro(vmx, MSR_APIC_LVT_LINT1);
361 error += guest_msr_ro(vmx, MSR_APIC_LVT_ERROR);
362 error += guest_msr_ro(vmx, MSR_APIC_ICR_TIMER);
363 error += guest_msr_ro(vmx, MSR_APIC_DCR_TIMER);
364 error += guest_msr_ro(vmx, MSR_APIC_ICR);
367 * Allow TPR, EOI and SELF_IPI MSRs to be read and written by the guest.
369 * These registers get special treatment described in the section
370 * "Virtualizing MSR-Based APIC Accesses".
372 error += guest_msr_rw(vmx, MSR_APIC_TPR);
373 error += guest_msr_rw(vmx, MSR_APIC_EOI);
374 error += guest_msr_rw(vmx, MSR_APIC_SELF_IPI);
380 vmx_fix_cr0(u_long cr0)
383 return ((cr0 | cr0_ones_mask) & ~cr0_zeros_mask);
387 vmx_fix_cr4(u_long cr4)
390 return ((cr4 | cr4_ones_mask) & ~cr4_zeros_mask);
396 if (vpid < 0 || vpid > 0xffff)
397 panic("vpid_free: invalid vpid %d", vpid);
400 * VPIDs [0,VM_MAXCPU] are special and are not allocated from
401 * the unit number allocator.
404 if (vpid > VM_MAXCPU)
405 free_unr(vpid_unr, vpid);
409 vpid_alloc(uint16_t *vpid, int num)
413 if (num <= 0 || num > VM_MAXCPU)
414 panic("invalid number of vpids requested: %d", num);
417 * If the "enable vpid" execution control is not enabled then the
418 * VPID is required to be 0 for all vcpus.
420 if ((procbased_ctls2 & PROCBASED2_ENABLE_VPID) == 0) {
421 for (i = 0; i < num; i++)
427 * Allocate a unique VPID for each vcpu from the unit number allocator.
429 for (i = 0; i < num; i++) {
430 x = alloc_unr(vpid_unr);
438 atomic_add_int(&vpid_alloc_failed, 1);
441 * If the unit number allocator does not have enough unique
442 * VPIDs then we need to allocate from the [1,VM_MAXCPU] range.
444 * These VPIDs are not be unique across VMs but this does not
445 * affect correctness because the combined mappings are also
446 * tagged with the EP4TA which is unique for each VM.
448 * It is still sub-optimal because the invvpid will invalidate
449 * combined mappings for a particular VPID across all EP4TAs.
454 for (i = 0; i < num; i++)
463 * VPID 0 is required when the "enable VPID" execution control is
466 * VPIDs [1,VM_MAXCPU] are used as the "overflow namespace" when the
467 * unit number allocator does not have sufficient unique VPIDs to
468 * satisfy the allocation.
470 * The remaining VPIDs are managed by the unit number allocator.
472 vpid_unr = new_unrhdr(VM_MAXCPU + 1, 0xffff, NULL);
476 vmx_disable(void *arg __unused)
478 struct invvpid_desc invvpid_desc = { 0 };
479 struct invept_desc invept_desc = { 0 };
481 if (vmxon_enabled[curcpu]) {
483 * See sections 25.3.3.3 and 25.3.3.4 in Intel Vol 3b.
485 * VMXON or VMXOFF are not required to invalidate any TLB
486 * caching structures. This prevents potential retention of
487 * cached information in the TLB between distinct VMX episodes.
489 invvpid(INVVPID_TYPE_ALL_CONTEXTS, invvpid_desc);
490 invept(INVEPT_TYPE_ALL_CONTEXTS, invept_desc);
493 load_cr4(rcr4() & ~CR4_VMXE);
501 lapic_ipi_free(pirvec);
503 if (vpid_unr != NULL) {
504 delete_unrhdr(vpid_unr);
508 if (nmi_flush_l1d_sw == 1)
509 nmi_flush_l1d_sw = 0;
511 smp_rendezvous(NULL, vmx_disable, NULL, NULL);
517 vmx_enable(void *arg __unused)
520 uint64_t feature_control;
522 feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
523 if ((feature_control & IA32_FEATURE_CONTROL_LOCK) == 0 ||
524 (feature_control & IA32_FEATURE_CONTROL_VMX_EN) == 0) {
525 wrmsr(MSR_IA32_FEATURE_CONTROL,
526 feature_control | IA32_FEATURE_CONTROL_VMX_EN |
527 IA32_FEATURE_CONTROL_LOCK);
530 load_cr4(rcr4() | CR4_VMXE);
532 *(uint32_t *)vmxon_region[curcpu] = vmx_revision();
533 error = vmxon(vmxon_region[curcpu]);
535 vmxon_enabled[curcpu] = 1;
542 if (vmxon_enabled[curcpu])
543 vmxon(vmxon_region[curcpu]);
549 int error, use_tpr_shadow;
550 uint64_t basic, fixed0, fixed1, feature_control;
551 uint32_t tmp, procbased2_vid_bits;
553 /* CPUID.1:ECX[bit 5] must be 1 for processor to support VMX */
554 if (!(cpu_feature2 & CPUID2_VMX)) {
555 printf("vmx_init: processor does not support VMX operation\n");
560 * Verify that MSR_IA32_FEATURE_CONTROL lock and VMXON enable bits
561 * are set (bits 0 and 2 respectively).
563 feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
564 if ((feature_control & IA32_FEATURE_CONTROL_LOCK) == 1 &&
565 (feature_control & IA32_FEATURE_CONTROL_VMX_EN) == 0) {
566 printf("vmx_init: VMX operation disabled by BIOS\n");
571 * Verify capabilities MSR_VMX_BASIC:
572 * - bit 54 indicates support for INS/OUTS decoding
574 basic = rdmsr(MSR_VMX_BASIC);
575 if ((basic & (1UL << 54)) == 0) {
576 printf("vmx_init: processor does not support desired basic "
581 /* Check support for primary processor-based VM-execution controls */
582 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
583 MSR_VMX_TRUE_PROCBASED_CTLS,
584 PROCBASED_CTLS_ONE_SETTING,
585 PROCBASED_CTLS_ZERO_SETTING, &procbased_ctls);
587 printf("vmx_init: processor does not support desired primary "
588 "processor-based controls\n");
592 /* Clear the processor-based ctl bits that are set on demand */
593 procbased_ctls &= ~PROCBASED_CTLS_WINDOW_SETTING;
595 /* Check support for secondary processor-based VM-execution controls */
596 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
597 MSR_VMX_PROCBASED_CTLS2,
598 PROCBASED_CTLS2_ONE_SETTING,
599 PROCBASED_CTLS2_ZERO_SETTING, &procbased_ctls2);
601 printf("vmx_init: processor does not support desired secondary "
602 "processor-based controls\n");
606 /* Check support for VPID */
607 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2,
608 PROCBASED2_ENABLE_VPID, 0, &tmp);
610 procbased_ctls2 |= PROCBASED2_ENABLE_VPID;
612 /* Check support for pin-based VM-execution controls */
613 error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS,
614 MSR_VMX_TRUE_PINBASED_CTLS,
615 PINBASED_CTLS_ONE_SETTING,
616 PINBASED_CTLS_ZERO_SETTING, &pinbased_ctls);
618 printf("vmx_init: processor does not support desired "
619 "pin-based controls\n");
623 /* Check support for VM-exit controls */
624 error = vmx_set_ctlreg(MSR_VMX_EXIT_CTLS, MSR_VMX_TRUE_EXIT_CTLS,
625 VM_EXIT_CTLS_ONE_SETTING,
626 VM_EXIT_CTLS_ZERO_SETTING,
629 printf("vmx_init: processor does not support desired "
634 /* Check support for VM-entry controls */
635 error = vmx_set_ctlreg(MSR_VMX_ENTRY_CTLS, MSR_VMX_TRUE_ENTRY_CTLS,
636 VM_ENTRY_CTLS_ONE_SETTING, VM_ENTRY_CTLS_ZERO_SETTING,
639 printf("vmx_init: processor does not support desired "
645 * Check support for optional features by testing them
648 cap_halt_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
649 MSR_VMX_TRUE_PROCBASED_CTLS,
650 PROCBASED_HLT_EXITING, 0,
653 cap_monitor_trap = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
654 MSR_VMX_PROCBASED_CTLS,
658 cap_pause_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
659 MSR_VMX_TRUE_PROCBASED_CTLS,
660 PROCBASED_PAUSE_EXITING, 0,
663 cap_unrestricted_guest = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
664 MSR_VMX_PROCBASED_CTLS2,
665 PROCBASED2_UNRESTRICTED_GUEST, 0,
668 cap_invpcid = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
669 MSR_VMX_PROCBASED_CTLS2, PROCBASED2_ENABLE_INVPCID, 0,
673 * Check support for virtual interrupt delivery.
675 procbased2_vid_bits = (PROCBASED2_VIRTUALIZE_APIC_ACCESSES |
676 PROCBASED2_VIRTUALIZE_X2APIC_MODE |
677 PROCBASED2_APIC_REGISTER_VIRTUALIZATION |
678 PROCBASED2_VIRTUAL_INTERRUPT_DELIVERY);
680 use_tpr_shadow = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
681 MSR_VMX_TRUE_PROCBASED_CTLS, PROCBASED_USE_TPR_SHADOW, 0,
684 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2,
685 procbased2_vid_bits, 0, &tmp);
686 if (error == 0 && use_tpr_shadow) {
687 virtual_interrupt_delivery = 1;
688 TUNABLE_INT_FETCH("hw.vmm.vmx.use_apic_vid",
689 &virtual_interrupt_delivery);
692 if (virtual_interrupt_delivery) {
693 procbased_ctls |= PROCBASED_USE_TPR_SHADOW;
694 procbased_ctls2 |= procbased2_vid_bits;
695 procbased_ctls2 &= ~PROCBASED2_VIRTUALIZE_X2APIC_MODE;
698 * No need to emulate accesses to %CR8 if virtual
699 * interrupt delivery is enabled.
701 procbased_ctls &= ~PROCBASED_CR8_LOAD_EXITING;
702 procbased_ctls &= ~PROCBASED_CR8_STORE_EXITING;
705 * Check for Posted Interrupts only if Virtual Interrupt
706 * Delivery is enabled.
708 error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS,
709 MSR_VMX_TRUE_PINBASED_CTLS, PINBASED_POSTED_INTERRUPT, 0,
712 pirvec = lapic_ipi_alloc(pti ? &IDTVEC(justreturn1_pti) :
713 &IDTVEC(justreturn));
716 printf("vmx_init: unable to allocate "
717 "posted interrupt vector\n");
720 posted_interrupts = 1;
721 TUNABLE_INT_FETCH("hw.vmm.vmx.use_apic_pir",
727 if (posted_interrupts)
728 pinbased_ctls |= PINBASED_POSTED_INTERRUPT;
731 error = ept_init(ipinum);
733 printf("vmx_init: ept initialization failed (%d)\n", error);
737 guest_l1d_flush = (cpu_ia32_arch_caps &
738 IA32_ARCH_CAP_SKIP_L1DFL_VMENTRY) == 0;
739 TUNABLE_INT_FETCH("hw.vmm.l1d_flush", &guest_l1d_flush);
742 * L1D cache flush is enabled. Use IA32_FLUSH_CMD MSR when
743 * available. Otherwise fall back to the software flush
744 * method which loads enough data from the kernel text to
745 * flush existing L1D content, both on VMX entry and on NMI
748 if (guest_l1d_flush) {
749 if ((cpu_stdext_feature3 & CPUID_STDEXT3_L1D_FLUSH) == 0) {
750 guest_l1d_flush_sw = 1;
751 TUNABLE_INT_FETCH("hw.vmm.l1d_flush_sw",
752 &guest_l1d_flush_sw);
754 if (guest_l1d_flush_sw) {
755 if (nmi_flush_l1d_sw <= 1)
756 nmi_flush_l1d_sw = 1;
758 msr_load_list[0].index = MSR_IA32_FLUSH_CMD;
759 msr_load_list[0].val = IA32_FLUSH_CMD_L1D;
764 * Stash the cr0 and cr4 bits that must be fixed to 0 or 1
766 fixed0 = rdmsr(MSR_VMX_CR0_FIXED0);
767 fixed1 = rdmsr(MSR_VMX_CR0_FIXED1);
768 cr0_ones_mask = fixed0 & fixed1;
769 cr0_zeros_mask = ~fixed0 & ~fixed1;
772 * CR0_PE and CR0_PG can be set to zero in VMX non-root operation
773 * if unrestricted guest execution is allowed.
775 if (cap_unrestricted_guest)
776 cr0_ones_mask &= ~(CR0_PG | CR0_PE);
779 * Do not allow the guest to set CR0_NW or CR0_CD.
781 cr0_zeros_mask |= (CR0_NW | CR0_CD);
783 fixed0 = rdmsr(MSR_VMX_CR4_FIXED0);
784 fixed1 = rdmsr(MSR_VMX_CR4_FIXED1);
785 cr4_ones_mask = fixed0 & fixed1;
786 cr4_zeros_mask = ~fixed0 & ~fixed1;
792 /* enable VMX operation */
793 smp_rendezvous(NULL, vmx_enable, NULL, NULL);
801 vmx_trigger_hostintr(int vector)
804 struct gate_descriptor *gd;
808 KASSERT(vector >= 32 && vector <= 255, ("vmx_trigger_hostintr: "
809 "invalid vector %d", vector));
810 KASSERT(gd->gd_p == 1, ("gate descriptor for vector %d not present",
812 KASSERT(gd->gd_type == SDT_SYSIGT, ("gate descriptor for vector %d "
813 "has invalid type %d", vector, gd->gd_type));
814 KASSERT(gd->gd_dpl == SEL_KPL, ("gate descriptor for vector %d "
815 "has invalid dpl %d", vector, gd->gd_dpl));
816 KASSERT(gd->gd_selector == GSEL(GCODE_SEL, SEL_KPL), ("gate descriptor "
817 "for vector %d has invalid selector %d", vector, gd->gd_selector));
818 KASSERT(gd->gd_ist == 0, ("gate descriptor for vector %d has invalid "
819 "IST %d", vector, gd->gd_ist));
821 func = ((long)gd->gd_hioffset << 16 | gd->gd_looffset);
826 vmx_setup_cr_shadow(int which, struct vmcs *vmcs, uint32_t initial)
828 int error, mask_ident, shadow_ident;
831 if (which != 0 && which != 4)
832 panic("vmx_setup_cr_shadow: unknown cr%d", which);
835 mask_ident = VMCS_CR0_MASK;
836 mask_value = cr0_ones_mask | cr0_zeros_mask;
837 shadow_ident = VMCS_CR0_SHADOW;
839 mask_ident = VMCS_CR4_MASK;
840 mask_value = cr4_ones_mask | cr4_zeros_mask;
841 shadow_ident = VMCS_CR4_SHADOW;
844 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(mask_ident), mask_value);
848 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(shadow_ident), initial);
854 #define vmx_setup_cr0_shadow(vmcs,init) vmx_setup_cr_shadow(0, (vmcs), (init))
855 #define vmx_setup_cr4_shadow(vmcs,init) vmx_setup_cr_shadow(4, (vmcs), (init))
858 vmx_vminit(struct vm *vm, pmap_t pmap)
860 uint16_t vpid[VM_MAXCPU];
866 vmx = malloc(sizeof(struct vmx), M_VMX, M_WAITOK | M_ZERO);
867 if ((uintptr_t)vmx & PAGE_MASK) {
868 panic("malloc of struct vmx not aligned on %d byte boundary",
873 vmx->eptp = eptp(vtophys((vm_offset_t)pmap->pm_pml4));
876 * Clean up EPTP-tagged guest physical and combined mappings
878 * VMX transitions are not required to invalidate any guest physical
879 * mappings. So, it may be possible for stale guest physical mappings
880 * to be present in the processor TLBs.
882 * Combined mappings for this EP4TA are also invalidated for all VPIDs.
884 ept_invalidate_mappings(vmx->eptp);
886 msr_bitmap_initialize(vmx->msr_bitmap);
889 * It is safe to allow direct access to MSR_GSBASE and MSR_FSBASE.
890 * The guest FSBASE and GSBASE are saved and restored during
891 * vm-exit and vm-entry respectively. The host FSBASE and GSBASE are
892 * always restored from the vmcs host state area on vm-exit.
894 * The SYSENTER_CS/ESP/EIP MSRs are identical to FS/GSBASE in
895 * how they are saved/restored so can be directly accessed by the
898 * MSR_EFER is saved and restored in the guest VMCS area on a
899 * VM exit and entry respectively. It is also restored from the
900 * host VMCS area on a VM exit.
902 * The TSC MSR is exposed read-only. Writes are disallowed as
903 * that will impact the host TSC. If the guest does a write
904 * the "use TSC offsetting" execution control is enabled and the
905 * difference between the host TSC and the guest TSC is written
906 * into the TSC offset in the VMCS.
908 if (guest_msr_rw(vmx, MSR_GSBASE) ||
909 guest_msr_rw(vmx, MSR_FSBASE) ||
910 guest_msr_rw(vmx, MSR_SYSENTER_CS_MSR) ||
911 guest_msr_rw(vmx, MSR_SYSENTER_ESP_MSR) ||
912 guest_msr_rw(vmx, MSR_SYSENTER_EIP_MSR) ||
913 guest_msr_rw(vmx, MSR_EFER) ||
914 guest_msr_ro(vmx, MSR_TSC))
915 panic("vmx_vminit: error setting guest msr access");
917 vpid_alloc(vpid, VM_MAXCPU);
919 if (virtual_interrupt_delivery) {
920 error = vm_map_mmio(vm, DEFAULT_APIC_BASE, PAGE_SIZE,
921 APIC_ACCESS_ADDRESS);
922 /* XXX this should really return an error to the caller */
923 KASSERT(error == 0, ("vm_map_mmio(apicbase) error %d", error));
926 for (i = 0; i < VM_MAXCPU; i++) {
927 vmcs = &vmx->vmcs[i];
928 vmcs->identifier = vmx_revision();
929 error = vmclear(vmcs);
931 panic("vmx_vminit: vmclear error %d on vcpu %d\n",
935 vmx_msr_guest_init(vmx, i);
937 error = vmcs_init(vmcs);
938 KASSERT(error == 0, ("vmcs_init error %d", error));
942 error += vmwrite(VMCS_HOST_RSP, (u_long)&vmx->ctx[i]);
943 error += vmwrite(VMCS_EPTP, vmx->eptp);
944 error += vmwrite(VMCS_PIN_BASED_CTLS, pinbased_ctls);
945 error += vmwrite(VMCS_PRI_PROC_BASED_CTLS, procbased_ctls);
946 error += vmwrite(VMCS_SEC_PROC_BASED_CTLS, procbased_ctls2);
947 error += vmwrite(VMCS_EXIT_CTLS, exit_ctls);
948 error += vmwrite(VMCS_ENTRY_CTLS, entry_ctls);
949 error += vmwrite(VMCS_MSR_BITMAP, vtophys(vmx->msr_bitmap));
950 error += vmwrite(VMCS_VPID, vpid[i]);
952 if (guest_l1d_flush && !guest_l1d_flush_sw) {
953 vmcs_write(VMCS_ENTRY_MSR_LOAD, pmap_kextract(
954 (vm_offset_t)&msr_load_list[0]));
955 vmcs_write(VMCS_ENTRY_MSR_LOAD_COUNT,
956 nitems(msr_load_list));
957 vmcs_write(VMCS_EXIT_MSR_STORE, 0);
958 vmcs_write(VMCS_EXIT_MSR_STORE_COUNT, 0);
961 /* exception bitmap */
962 if (vcpu_trace_exceptions(vm, i))
963 exc_bitmap = 0xffffffff;
965 exc_bitmap = 1 << IDT_MC;
966 error += vmwrite(VMCS_EXCEPTION_BITMAP, exc_bitmap);
968 vmx->ctx[i].guest_dr6 = DBREG_DR6_RESERVED1;
969 error += vmwrite(VMCS_GUEST_DR7, DBREG_DR7_RESERVED1);
971 if (virtual_interrupt_delivery) {
972 error += vmwrite(VMCS_APIC_ACCESS, APIC_ACCESS_ADDRESS);
973 error += vmwrite(VMCS_VIRTUAL_APIC,
974 vtophys(&vmx->apic_page[i]));
975 error += vmwrite(VMCS_EOI_EXIT0, 0);
976 error += vmwrite(VMCS_EOI_EXIT1, 0);
977 error += vmwrite(VMCS_EOI_EXIT2, 0);
978 error += vmwrite(VMCS_EOI_EXIT3, 0);
980 if (posted_interrupts) {
981 error += vmwrite(VMCS_PIR_VECTOR, pirvec);
982 error += vmwrite(VMCS_PIR_DESC,
983 vtophys(&vmx->pir_desc[i]));
986 KASSERT(error == 0, ("vmx_vminit: error customizing the vmcs"));
989 vmx->cap[i].proc_ctls = procbased_ctls;
990 vmx->cap[i].proc_ctls2 = procbased_ctls2;
992 vmx->state[i].nextrip = ~0;
993 vmx->state[i].lastcpu = NOCPU;
994 vmx->state[i].vpid = vpid[i];
997 * Set up the CR0/4 shadows, and init the read shadow
998 * to the power-on register value from the Intel Sys Arch.
1002 error = vmx_setup_cr0_shadow(vmcs, 0x60000010);
1004 panic("vmx_setup_cr0_shadow %d", error);
1006 error = vmx_setup_cr4_shadow(vmcs, 0);
1008 panic("vmx_setup_cr4_shadow %d", error);
1010 vmx->ctx[i].pmap = pmap;
1017 vmx_handle_cpuid(struct vm *vm, int vcpu, struct vmxctx *vmxctx)
1021 func = vmxctx->guest_rax;
1023 handled = x86_emulate_cpuid(vm, vcpu,
1024 (uint32_t*)(&vmxctx->guest_rax),
1025 (uint32_t*)(&vmxctx->guest_rbx),
1026 (uint32_t*)(&vmxctx->guest_rcx),
1027 (uint32_t*)(&vmxctx->guest_rdx));
1031 static __inline void
1032 vmx_run_trace(struct vmx *vmx, int vcpu)
1035 VCPU_CTR1(vmx->vm, vcpu, "Resume execution at %#lx", vmcs_guest_rip());
1039 static __inline void
1040 vmx_exit_trace(struct vmx *vmx, int vcpu, uint64_t rip, uint32_t exit_reason,
1044 VCPU_CTR3(vmx->vm, vcpu, "%s %s vmexit at 0x%0lx",
1045 handled ? "handled" : "unhandled",
1046 exit_reason_to_str(exit_reason), rip);
1050 static __inline void
1051 vmx_astpending_trace(struct vmx *vmx, int vcpu, uint64_t rip)
1054 VCPU_CTR1(vmx->vm, vcpu, "astpending vmexit at 0x%0lx", rip);
1058 static VMM_STAT_INTEL(VCPU_INVVPID_SAVED, "Number of vpid invalidations saved");
1059 static VMM_STAT_INTEL(VCPU_INVVPID_DONE, "Number of vpid invalidations done");
1062 * Invalidate guest mappings identified by its vpid from the TLB.
1064 static __inline void
1065 vmx_invvpid(struct vmx *vmx, int vcpu, pmap_t pmap, int running)
1067 struct vmxstate *vmxstate;
1068 struct invvpid_desc invvpid_desc;
1070 vmxstate = &vmx->state[vcpu];
1071 if (vmxstate->vpid == 0)
1076 * Set the 'lastcpu' to an invalid host cpu.
1078 * This will invalidate TLB entries tagged with the vcpu's
1079 * vpid the next time it runs via vmx_set_pcpu_defaults().
1081 vmxstate->lastcpu = NOCPU;
1085 KASSERT(curthread->td_critnest > 0, ("%s: vcpu %d running outside "
1086 "critical section", __func__, vcpu));
1089 * Invalidate all mappings tagged with 'vpid'
1091 * We do this because this vcpu was executing on a different host
1092 * cpu when it last ran. We do not track whether it invalidated
1093 * mappings associated with its 'vpid' during that run. So we must
1094 * assume that the mappings associated with 'vpid' on 'curcpu' are
1095 * stale and invalidate them.
1097 * Note that we incur this penalty only when the scheduler chooses to
1098 * move the thread associated with this vcpu between host cpus.
1100 * Note also that this will invalidate mappings tagged with 'vpid'
1103 if (pmap->pm_eptgen == vmx->eptgen[curcpu]) {
1104 invvpid_desc._res1 = 0;
1105 invvpid_desc._res2 = 0;
1106 invvpid_desc.vpid = vmxstate->vpid;
1107 invvpid_desc.linear_addr = 0;
1108 invvpid(INVVPID_TYPE_SINGLE_CONTEXT, invvpid_desc);
1109 vmm_stat_incr(vmx->vm, vcpu, VCPU_INVVPID_DONE, 1);
1112 * The invvpid can be skipped if an invept is going to
1113 * be performed before entering the guest. The invept
1114 * will invalidate combined mappings tagged with
1115 * 'vmx->eptp' for all vpids.
1117 vmm_stat_incr(vmx->vm, vcpu, VCPU_INVVPID_SAVED, 1);
1122 vmx_set_pcpu_defaults(struct vmx *vmx, int vcpu, pmap_t pmap)
1124 struct vmxstate *vmxstate;
1126 vmxstate = &vmx->state[vcpu];
1127 if (vmxstate->lastcpu == curcpu)
1130 vmxstate->lastcpu = curcpu;
1132 vmm_stat_incr(vmx->vm, vcpu, VCPU_MIGRATIONS, 1);
1134 vmcs_write(VMCS_HOST_TR_BASE, vmm_get_host_trbase());
1135 vmcs_write(VMCS_HOST_GDTR_BASE, vmm_get_host_gdtrbase());
1136 vmcs_write(VMCS_HOST_GS_BASE, vmm_get_host_gsbase());
1137 vmx_invvpid(vmx, vcpu, pmap, 1);
1141 * We depend on 'procbased_ctls' to have the Interrupt Window Exiting bit set.
1143 CTASSERT((PROCBASED_CTLS_ONE_SETTING & PROCBASED_INT_WINDOW_EXITING) != 0);
1145 static void __inline
1146 vmx_set_int_window_exiting(struct vmx *vmx, int vcpu)
1149 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) == 0) {
1150 vmx->cap[vcpu].proc_ctls |= PROCBASED_INT_WINDOW_EXITING;
1151 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1152 VCPU_CTR0(vmx->vm, vcpu, "Enabling interrupt window exiting");
1156 static void __inline
1157 vmx_clear_int_window_exiting(struct vmx *vmx, int vcpu)
1160 KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0,
1161 ("intr_window_exiting not set: %#x", vmx->cap[vcpu].proc_ctls));
1162 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_INT_WINDOW_EXITING;
1163 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1164 VCPU_CTR0(vmx->vm, vcpu, "Disabling interrupt window exiting");
1167 static void __inline
1168 vmx_set_nmi_window_exiting(struct vmx *vmx, int vcpu)
1171 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) == 0) {
1172 vmx->cap[vcpu].proc_ctls |= PROCBASED_NMI_WINDOW_EXITING;
1173 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1174 VCPU_CTR0(vmx->vm, vcpu, "Enabling NMI window exiting");
1178 static void __inline
1179 vmx_clear_nmi_window_exiting(struct vmx *vmx, int vcpu)
1182 KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) != 0,
1183 ("nmi_window_exiting not set %#x", vmx->cap[vcpu].proc_ctls));
1184 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_NMI_WINDOW_EXITING;
1185 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1186 VCPU_CTR0(vmx->vm, vcpu, "Disabling NMI window exiting");
1190 vmx_set_tsc_offset(struct vmx *vmx, int vcpu, uint64_t offset)
1194 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_TSC_OFFSET) == 0) {
1195 vmx->cap[vcpu].proc_ctls |= PROCBASED_TSC_OFFSET;
1196 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1197 VCPU_CTR0(vmx->vm, vcpu, "Enabling TSC offsetting");
1200 error = vmwrite(VMCS_TSC_OFFSET, offset);
1205 #define NMI_BLOCKING (VMCS_INTERRUPTIBILITY_NMI_BLOCKING | \
1206 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)
1207 #define HWINTR_BLOCKING (VMCS_INTERRUPTIBILITY_STI_BLOCKING | \
1208 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)
1211 vmx_inject_nmi(struct vmx *vmx, int vcpu)
1215 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1216 KASSERT((gi & NMI_BLOCKING) == 0, ("vmx_inject_nmi: invalid guest "
1217 "interruptibility-state %#x", gi));
1219 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1220 KASSERT((info & VMCS_INTR_VALID) == 0, ("vmx_inject_nmi: invalid "
1221 "VM-entry interruption information %#x", info));
1224 * Inject the virtual NMI. The vector must be the NMI IDT entry
1225 * or the VMCS entry check will fail.
1227 info = IDT_NMI | VMCS_INTR_T_NMI | VMCS_INTR_VALID;
1228 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1230 VCPU_CTR0(vmx->vm, vcpu, "Injecting vNMI");
1232 /* Clear the request */
1233 vm_nmi_clear(vmx->vm, vcpu);
1237 vmx_inject_interrupts(struct vmx *vmx, int vcpu, struct vlapic *vlapic,
1240 int vector, need_nmi_exiting, extint_pending;
1241 uint64_t rflags, entryinfo;
1244 if (vmx->state[vcpu].nextrip != guestrip) {
1245 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1246 if (gi & HWINTR_BLOCKING) {
1247 VCPU_CTR2(vmx->vm, vcpu, "Guest interrupt blocking "
1248 "cleared due to rip change: %#lx/%#lx",
1249 vmx->state[vcpu].nextrip, guestrip);
1250 gi &= ~HWINTR_BLOCKING;
1251 vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1255 if (vm_entry_intinfo(vmx->vm, vcpu, &entryinfo)) {
1256 KASSERT((entryinfo & VMCS_INTR_VALID) != 0, ("%s: entry "
1257 "intinfo is not valid: %#lx", __func__, entryinfo));
1259 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1260 KASSERT((info & VMCS_INTR_VALID) == 0, ("%s: cannot inject "
1261 "pending exception: %#lx/%#x", __func__, entryinfo, info));
1264 vector = info & 0xff;
1265 if (vector == IDT_BP || vector == IDT_OF) {
1267 * VT-x requires #BP and #OF to be injected as software
1270 info &= ~VMCS_INTR_T_MASK;
1271 info |= VMCS_INTR_T_SWEXCEPTION;
1274 if (info & VMCS_INTR_DEL_ERRCODE)
1275 vmcs_write(VMCS_ENTRY_EXCEPTION_ERROR, entryinfo >> 32);
1277 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1280 if (vm_nmi_pending(vmx->vm, vcpu)) {
1282 * If there are no conditions blocking NMI injection then
1283 * inject it directly here otherwise enable "NMI window
1284 * exiting" to inject it as soon as we can.
1286 * We also check for STI_BLOCKING because some implementations
1287 * don't allow NMI injection in this case. If we are running
1288 * on a processor that doesn't have this restriction it will
1289 * immediately exit and the NMI will be injected in the
1290 * "NMI window exiting" handler.
1292 need_nmi_exiting = 1;
1293 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1294 if ((gi & (HWINTR_BLOCKING | NMI_BLOCKING)) == 0) {
1295 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1296 if ((info & VMCS_INTR_VALID) == 0) {
1297 vmx_inject_nmi(vmx, vcpu);
1298 need_nmi_exiting = 0;
1300 VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI "
1301 "due to VM-entry intr info %#x", info);
1304 VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI due to "
1305 "Guest Interruptibility-state %#x", gi);
1308 if (need_nmi_exiting)
1309 vmx_set_nmi_window_exiting(vmx, vcpu);
1312 extint_pending = vm_extint_pending(vmx->vm, vcpu);
1314 if (!extint_pending && virtual_interrupt_delivery) {
1315 vmx_inject_pir(vlapic);
1320 * If interrupt-window exiting is already in effect then don't bother
1321 * checking for pending interrupts. This is just an optimization and
1322 * not needed for correctness.
1324 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0) {
1325 VCPU_CTR0(vmx->vm, vcpu, "Skip interrupt injection due to "
1326 "pending int_window_exiting");
1330 if (!extint_pending) {
1331 /* Ask the local apic for a vector to inject */
1332 if (!vlapic_pending_intr(vlapic, &vector))
1336 * From the Intel SDM, Volume 3, Section "Maskable
1337 * Hardware Interrupts":
1338 * - maskable interrupt vectors [16,255] can be delivered
1339 * through the local APIC.
1341 KASSERT(vector >= 16 && vector <= 255,
1342 ("invalid vector %d from local APIC", vector));
1344 /* Ask the legacy pic for a vector to inject */
1345 vatpic_pending_intr(vmx->vm, &vector);
1348 * From the Intel SDM, Volume 3, Section "Maskable
1349 * Hardware Interrupts":
1350 * - maskable interrupt vectors [0,255] can be delivered
1351 * through the INTR pin.
1353 KASSERT(vector >= 0 && vector <= 255,
1354 ("invalid vector %d from INTR", vector));
1357 /* Check RFLAGS.IF and the interruptibility state of the guest */
1358 rflags = vmcs_read(VMCS_GUEST_RFLAGS);
1359 if ((rflags & PSL_I) == 0) {
1360 VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1361 "rflags %#lx", vector, rflags);
1365 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1366 if (gi & HWINTR_BLOCKING) {
1367 VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1368 "Guest Interruptibility-state %#x", vector, gi);
1372 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1373 if (info & VMCS_INTR_VALID) {
1375 * This is expected and could happen for multiple reasons:
1376 * - A vectoring VM-entry was aborted due to astpending
1377 * - A VM-exit happened during event injection.
1378 * - An exception was injected above.
1379 * - An NMI was injected above or after "NMI window exiting"
1381 VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1382 "VM-entry intr info %#x", vector, info);
1386 /* Inject the interrupt */
1387 info = VMCS_INTR_T_HWINTR | VMCS_INTR_VALID;
1389 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1391 if (!extint_pending) {
1392 /* Update the Local APIC ISR */
1393 vlapic_intr_accepted(vlapic, vector);
1395 vm_extint_clear(vmx->vm, vcpu);
1396 vatpic_intr_accepted(vmx->vm, vector);
1399 * After we accepted the current ExtINT the PIC may
1400 * have posted another one. If that is the case, set
1401 * the Interrupt Window Exiting execution control so
1402 * we can inject that one too.
1404 * Also, interrupt window exiting allows us to inject any
1405 * pending APIC vector that was preempted by the ExtINT
1406 * as soon as possible. This applies both for the software
1407 * emulated vlapic and the hardware assisted virtual APIC.
1409 vmx_set_int_window_exiting(vmx, vcpu);
1412 VCPU_CTR1(vmx->vm, vcpu, "Injecting hwintr at vector %d", vector);
1418 * Set the Interrupt Window Exiting execution control so we can inject
1419 * the interrupt as soon as blocking condition goes away.
1421 vmx_set_int_window_exiting(vmx, vcpu);
1425 * If the Virtual NMIs execution control is '1' then the logical processor
1426 * tracks virtual-NMI blocking in the Guest Interruptibility-state field of
1427 * the VMCS. An IRET instruction in VMX non-root operation will remove any
1428 * virtual-NMI blocking.
1430 * This unblocking occurs even if the IRET causes a fault. In this case the
1431 * hypervisor needs to restore virtual-NMI blocking before resuming the guest.
1434 vmx_restore_nmi_blocking(struct vmx *vmx, int vcpuid)
1438 VCPU_CTR0(vmx->vm, vcpuid, "Restore Virtual-NMI blocking");
1439 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1440 gi |= VMCS_INTERRUPTIBILITY_NMI_BLOCKING;
1441 vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1445 vmx_clear_nmi_blocking(struct vmx *vmx, int vcpuid)
1449 VCPU_CTR0(vmx->vm, vcpuid, "Clear Virtual-NMI blocking");
1450 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1451 gi &= ~VMCS_INTERRUPTIBILITY_NMI_BLOCKING;
1452 vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1456 vmx_assert_nmi_blocking(struct vmx *vmx, int vcpuid)
1460 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1461 KASSERT(gi & VMCS_INTERRUPTIBILITY_NMI_BLOCKING,
1462 ("NMI blocking is not in effect %#x", gi));
1466 vmx_emulate_xsetbv(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
1468 struct vmxctx *vmxctx;
1470 const struct xsave_limits *limits;
1472 vmxctx = &vmx->ctx[vcpu];
1473 limits = vmm_get_xsave_limits();
1476 * Note that the processor raises a GP# fault on its own if
1477 * xsetbv is executed for CPL != 0, so we do not have to
1478 * emulate that fault here.
1481 /* Only xcr0 is supported. */
1482 if (vmxctx->guest_rcx != 0) {
1483 vm_inject_gp(vmx->vm, vcpu);
1487 /* We only handle xcr0 if both the host and guest have XSAVE enabled. */
1488 if (!limits->xsave_enabled || !(vmcs_read(VMCS_GUEST_CR4) & CR4_XSAVE)) {
1489 vm_inject_ud(vmx->vm, vcpu);
1493 xcrval = vmxctx->guest_rdx << 32 | (vmxctx->guest_rax & 0xffffffff);
1494 if ((xcrval & ~limits->xcr0_allowed) != 0) {
1495 vm_inject_gp(vmx->vm, vcpu);
1499 if (!(xcrval & XFEATURE_ENABLED_X87)) {
1500 vm_inject_gp(vmx->vm, vcpu);
1504 /* AVX (YMM_Hi128) requires SSE. */
1505 if (xcrval & XFEATURE_ENABLED_AVX &&
1506 (xcrval & XFEATURE_AVX) != XFEATURE_AVX) {
1507 vm_inject_gp(vmx->vm, vcpu);
1512 * AVX512 requires base AVX (YMM_Hi128) as well as OpMask,
1513 * ZMM_Hi256, and Hi16_ZMM.
1515 if (xcrval & XFEATURE_AVX512 &&
1516 (xcrval & (XFEATURE_AVX512 | XFEATURE_AVX)) !=
1517 (XFEATURE_AVX512 | XFEATURE_AVX)) {
1518 vm_inject_gp(vmx->vm, vcpu);
1523 * Intel MPX requires both bound register state flags to be
1526 if (((xcrval & XFEATURE_ENABLED_BNDREGS) != 0) !=
1527 ((xcrval & XFEATURE_ENABLED_BNDCSR) != 0)) {
1528 vm_inject_gp(vmx->vm, vcpu);
1533 * This runs "inside" vmrun() with the guest's FPU state, so
1534 * modifying xcr0 directly modifies the guest's xcr0, not the
1537 load_xcr(0, xcrval);
1542 vmx_get_guest_reg(struct vmx *vmx, int vcpu, int ident)
1544 const struct vmxctx *vmxctx;
1546 vmxctx = &vmx->ctx[vcpu];
1550 return (vmxctx->guest_rax);
1552 return (vmxctx->guest_rcx);
1554 return (vmxctx->guest_rdx);
1556 return (vmxctx->guest_rbx);
1558 return (vmcs_read(VMCS_GUEST_RSP));
1560 return (vmxctx->guest_rbp);
1562 return (vmxctx->guest_rsi);
1564 return (vmxctx->guest_rdi);
1566 return (vmxctx->guest_r8);
1568 return (vmxctx->guest_r9);
1570 return (vmxctx->guest_r10);
1572 return (vmxctx->guest_r11);
1574 return (vmxctx->guest_r12);
1576 return (vmxctx->guest_r13);
1578 return (vmxctx->guest_r14);
1580 return (vmxctx->guest_r15);
1582 panic("invalid vmx register %d", ident);
1587 vmx_set_guest_reg(struct vmx *vmx, int vcpu, int ident, uint64_t regval)
1589 struct vmxctx *vmxctx;
1591 vmxctx = &vmx->ctx[vcpu];
1595 vmxctx->guest_rax = regval;
1598 vmxctx->guest_rcx = regval;
1601 vmxctx->guest_rdx = regval;
1604 vmxctx->guest_rbx = regval;
1607 vmcs_write(VMCS_GUEST_RSP, regval);
1610 vmxctx->guest_rbp = regval;
1613 vmxctx->guest_rsi = regval;
1616 vmxctx->guest_rdi = regval;
1619 vmxctx->guest_r8 = regval;
1622 vmxctx->guest_r9 = regval;
1625 vmxctx->guest_r10 = regval;
1628 vmxctx->guest_r11 = regval;
1631 vmxctx->guest_r12 = regval;
1634 vmxctx->guest_r13 = regval;
1637 vmxctx->guest_r14 = regval;
1640 vmxctx->guest_r15 = regval;
1643 panic("invalid vmx register %d", ident);
1648 vmx_emulate_cr0_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1650 uint64_t crval, regval;
1652 /* We only handle mov to %cr0 at this time */
1653 if ((exitqual & 0xf0) != 0x00)
1656 regval = vmx_get_guest_reg(vmx, vcpu, (exitqual >> 8) & 0xf);
1658 vmcs_write(VMCS_CR0_SHADOW, regval);
1660 crval = regval | cr0_ones_mask;
1661 crval &= ~cr0_zeros_mask;
1662 vmcs_write(VMCS_GUEST_CR0, crval);
1664 if (regval & CR0_PG) {
1665 uint64_t efer, entry_ctls;
1668 * If CR0.PG is 1 and EFER.LME is 1 then EFER.LMA and
1669 * the "IA-32e mode guest" bit in VM-entry control must be
1672 efer = vmcs_read(VMCS_GUEST_IA32_EFER);
1673 if (efer & EFER_LME) {
1675 vmcs_write(VMCS_GUEST_IA32_EFER, efer);
1676 entry_ctls = vmcs_read(VMCS_ENTRY_CTLS);
1677 entry_ctls |= VM_ENTRY_GUEST_LMA;
1678 vmcs_write(VMCS_ENTRY_CTLS, entry_ctls);
1686 vmx_emulate_cr4_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1688 uint64_t crval, regval;
1690 /* We only handle mov to %cr4 at this time */
1691 if ((exitqual & 0xf0) != 0x00)
1694 regval = vmx_get_guest_reg(vmx, vcpu, (exitqual >> 8) & 0xf);
1696 vmcs_write(VMCS_CR4_SHADOW, regval);
1698 crval = regval | cr4_ones_mask;
1699 crval &= ~cr4_zeros_mask;
1700 vmcs_write(VMCS_GUEST_CR4, crval);
1706 vmx_emulate_cr8_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1708 struct vlapic *vlapic;
1712 /* We only handle mov %cr8 to/from a register at this time. */
1713 if ((exitqual & 0xe0) != 0x00) {
1717 vlapic = vm_lapic(vmx->vm, vcpu);
1718 regnum = (exitqual >> 8) & 0xf;
1719 if (exitqual & 0x10) {
1720 cr8 = vlapic_get_cr8(vlapic);
1721 vmx_set_guest_reg(vmx, vcpu, regnum, cr8);
1723 cr8 = vmx_get_guest_reg(vmx, vcpu, regnum);
1724 vlapic_set_cr8(vlapic, cr8);
1731 * From section "Guest Register State" in the Intel SDM: CPL = SS.DPL
1738 ssar = vmcs_read(VMCS_GUEST_SS_ACCESS_RIGHTS);
1739 return ((ssar >> 5) & 0x3);
1742 static enum vm_cpu_mode
1747 if (vmcs_read(VMCS_GUEST_IA32_EFER) & EFER_LMA) {
1748 csar = vmcs_read(VMCS_GUEST_CS_ACCESS_RIGHTS);
1750 return (CPU_MODE_64BIT); /* CS.L = 1 */
1752 return (CPU_MODE_COMPATIBILITY);
1753 } else if (vmcs_read(VMCS_GUEST_CR0) & CR0_PE) {
1754 return (CPU_MODE_PROTECTED);
1756 return (CPU_MODE_REAL);
1760 static enum vm_paging_mode
1761 vmx_paging_mode(void)
1764 if (!(vmcs_read(VMCS_GUEST_CR0) & CR0_PG))
1765 return (PAGING_MODE_FLAT);
1766 if (!(vmcs_read(VMCS_GUEST_CR4) & CR4_PAE))
1767 return (PAGING_MODE_32);
1768 if (vmcs_read(VMCS_GUEST_IA32_EFER) & EFER_LME)
1769 return (PAGING_MODE_64);
1771 return (PAGING_MODE_PAE);
1775 inout_str_index(struct vmx *vmx, int vcpuid, int in)
1779 enum vm_reg_name reg;
1781 reg = in ? VM_REG_GUEST_RDI : VM_REG_GUEST_RSI;
1782 error = vmx_getreg(vmx, vcpuid, reg, &val);
1783 KASSERT(error == 0, ("%s: vmx_getreg error %d", __func__, error));
1788 inout_str_count(struct vmx *vmx, int vcpuid, int rep)
1794 error = vmx_getreg(vmx, vcpuid, VM_REG_GUEST_RCX, &val);
1795 KASSERT(!error, ("%s: vmx_getreg error %d", __func__, error));
1803 inout_str_addrsize(uint32_t inst_info)
1807 size = (inst_info >> 7) & 0x7;
1810 return (2); /* 16 bit */
1812 return (4); /* 32 bit */
1814 return (8); /* 64 bit */
1816 panic("%s: invalid size encoding %d", __func__, size);
1821 inout_str_seginfo(struct vmx *vmx, int vcpuid, uint32_t inst_info, int in,
1822 struct vm_inout_str *vis)
1827 vis->seg_name = VM_REG_GUEST_ES;
1829 s = (inst_info >> 15) & 0x7;
1830 vis->seg_name = vm_segment_name(s);
1833 error = vmx_getdesc(vmx, vcpuid, vis->seg_name, &vis->seg_desc);
1834 KASSERT(error == 0, ("%s: vmx_getdesc error %d", __func__, error));
1838 vmx_paging_info(struct vm_guest_paging *paging)
1840 paging->cr3 = vmcs_guest_cr3();
1841 paging->cpl = vmx_cpl();
1842 paging->cpu_mode = vmx_cpu_mode();
1843 paging->paging_mode = vmx_paging_mode();
1847 vmexit_inst_emul(struct vm_exit *vmexit, uint64_t gpa, uint64_t gla)
1849 struct vm_guest_paging *paging;
1852 paging = &vmexit->u.inst_emul.paging;
1854 vmexit->exitcode = VM_EXITCODE_INST_EMUL;
1855 vmexit->inst_length = 0;
1856 vmexit->u.inst_emul.gpa = gpa;
1857 vmexit->u.inst_emul.gla = gla;
1858 vmx_paging_info(paging);
1859 switch (paging->cpu_mode) {
1861 vmexit->u.inst_emul.cs_base = vmcs_read(VMCS_GUEST_CS_BASE);
1862 vmexit->u.inst_emul.cs_d = 0;
1864 case CPU_MODE_PROTECTED:
1865 case CPU_MODE_COMPATIBILITY:
1866 vmexit->u.inst_emul.cs_base = vmcs_read(VMCS_GUEST_CS_BASE);
1867 csar = vmcs_read(VMCS_GUEST_CS_ACCESS_RIGHTS);
1868 vmexit->u.inst_emul.cs_d = SEG_DESC_DEF32(csar);
1871 vmexit->u.inst_emul.cs_base = 0;
1872 vmexit->u.inst_emul.cs_d = 0;
1875 vie_init(&vmexit->u.inst_emul.vie, NULL, 0);
1879 ept_fault_type(uint64_t ept_qual)
1883 if (ept_qual & EPT_VIOLATION_DATA_WRITE)
1884 fault_type = VM_PROT_WRITE;
1885 else if (ept_qual & EPT_VIOLATION_INST_FETCH)
1886 fault_type = VM_PROT_EXECUTE;
1888 fault_type= VM_PROT_READ;
1890 return (fault_type);
1894 ept_emulation_fault(uint64_t ept_qual)
1898 /* EPT fault on an instruction fetch doesn't make sense here */
1899 if (ept_qual & EPT_VIOLATION_INST_FETCH)
1902 /* EPT fault must be a read fault or a write fault */
1903 read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0;
1904 write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0;
1905 if ((read | write) == 0)
1909 * The EPT violation must have been caused by accessing a
1910 * guest-physical address that is a translation of a guest-linear
1913 if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 ||
1914 (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) {
1922 apic_access_virtualization(struct vmx *vmx, int vcpuid)
1924 uint32_t proc_ctls2;
1926 proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
1927 return ((proc_ctls2 & PROCBASED2_VIRTUALIZE_APIC_ACCESSES) ? 1 : 0);
1931 x2apic_virtualization(struct vmx *vmx, int vcpuid)
1933 uint32_t proc_ctls2;
1935 proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
1936 return ((proc_ctls2 & PROCBASED2_VIRTUALIZE_X2APIC_MODE) ? 1 : 0);
1940 vmx_handle_apic_write(struct vmx *vmx, int vcpuid, struct vlapic *vlapic,
1943 int error, handled, offset;
1944 uint32_t *apic_regs, vector;
1948 offset = APIC_WRITE_OFFSET(qual);
1950 if (!apic_access_virtualization(vmx, vcpuid)) {
1952 * In general there should not be any APIC write VM-exits
1953 * unless APIC-access virtualization is enabled.
1955 * However self-IPI virtualization can legitimately trigger
1956 * an APIC-write VM-exit so treat it specially.
1958 if (x2apic_virtualization(vmx, vcpuid) &&
1959 offset == APIC_OFFSET_SELF_IPI) {
1960 apic_regs = (uint32_t *)(vlapic->apic_page);
1961 vector = apic_regs[APIC_OFFSET_SELF_IPI / 4];
1962 vlapic_self_ipi_handler(vlapic, vector);
1969 case APIC_OFFSET_ID:
1970 vlapic_id_write_handler(vlapic);
1972 case APIC_OFFSET_LDR:
1973 vlapic_ldr_write_handler(vlapic);
1975 case APIC_OFFSET_DFR:
1976 vlapic_dfr_write_handler(vlapic);
1978 case APIC_OFFSET_SVR:
1979 vlapic_svr_write_handler(vlapic);
1981 case APIC_OFFSET_ESR:
1982 vlapic_esr_write_handler(vlapic);
1984 case APIC_OFFSET_ICR_LOW:
1986 error = vlapic_icrlo_write_handler(vlapic, &retu);
1987 if (error != 0 || retu)
1988 handled = UNHANDLED;
1990 case APIC_OFFSET_CMCI_LVT:
1991 case APIC_OFFSET_TIMER_LVT ... APIC_OFFSET_ERROR_LVT:
1992 vlapic_lvt_write_handler(vlapic, offset);
1994 case APIC_OFFSET_TIMER_ICR:
1995 vlapic_icrtmr_write_handler(vlapic);
1997 case APIC_OFFSET_TIMER_DCR:
1998 vlapic_dcr_write_handler(vlapic);
2001 handled = UNHANDLED;
2008 apic_access_fault(struct vmx *vmx, int vcpuid, uint64_t gpa)
2011 if (apic_access_virtualization(vmx, vcpuid) &&
2012 (gpa >= DEFAULT_APIC_BASE && gpa < DEFAULT_APIC_BASE + PAGE_SIZE))
2019 vmx_handle_apic_access(struct vmx *vmx, int vcpuid, struct vm_exit *vmexit)
2022 int access_type, offset, allowed;
2024 if (!apic_access_virtualization(vmx, vcpuid))
2027 qual = vmexit->u.vmx.exit_qualification;
2028 access_type = APIC_ACCESS_TYPE(qual);
2029 offset = APIC_ACCESS_OFFSET(qual);
2032 if (access_type == 0) {
2034 * Read data access to the following registers is expected.
2037 case APIC_OFFSET_APR:
2038 case APIC_OFFSET_PPR:
2039 case APIC_OFFSET_RRR:
2040 case APIC_OFFSET_CMCI_LVT:
2041 case APIC_OFFSET_TIMER_CCR:
2047 } else if (access_type == 1) {
2049 * Write data access to the following registers is expected.
2052 case APIC_OFFSET_VER:
2053 case APIC_OFFSET_APR:
2054 case APIC_OFFSET_PPR:
2055 case APIC_OFFSET_RRR:
2056 case APIC_OFFSET_ISR0 ... APIC_OFFSET_ISR7:
2057 case APIC_OFFSET_TMR0 ... APIC_OFFSET_TMR7:
2058 case APIC_OFFSET_IRR0 ... APIC_OFFSET_IRR7:
2059 case APIC_OFFSET_CMCI_LVT:
2060 case APIC_OFFSET_TIMER_CCR:
2069 vmexit_inst_emul(vmexit, DEFAULT_APIC_BASE + offset,
2074 * Regardless of whether the APIC-access is allowed this handler
2075 * always returns UNHANDLED:
2076 * - if the access is allowed then it is handled by emulating the
2077 * instruction that caused the VM-exit (outside the critical section)
2078 * - if the access is not allowed then it will be converted to an
2079 * exitcode of VM_EXITCODE_VMX and will be dealt with in userland.
2084 static enum task_switch_reason
2085 vmx_task_switch_reason(uint64_t qual)
2089 reason = (qual >> 30) & 0x3;
2098 return (TSR_IDT_GATE);
2100 panic("%s: invalid reason %d", __func__, reason);
2105 emulate_wrmsr(struct vmx *vmx, int vcpuid, u_int num, uint64_t val, bool *retu)
2110 error = lapic_wrmsr(vmx->vm, vcpuid, num, val, retu);
2112 error = vmx_wrmsr(vmx, vcpuid, num, val, retu);
2118 emulate_rdmsr(struct vmx *vmx, int vcpuid, u_int num, bool *retu)
2120 struct vmxctx *vmxctx;
2126 error = lapic_rdmsr(vmx->vm, vcpuid, num, &result, retu);
2128 error = vmx_rdmsr(vmx, vcpuid, num, &result, retu);
2132 vmxctx = &vmx->ctx[vcpuid];
2133 error = vmxctx_setreg(vmxctx, VM_REG_GUEST_RAX, eax);
2134 KASSERT(error == 0, ("vmxctx_setreg(rax) error %d", error));
2137 error = vmxctx_setreg(vmxctx, VM_REG_GUEST_RDX, edx);
2138 KASSERT(error == 0, ("vmxctx_setreg(rdx) error %d", error));
2145 vmx_exit_process(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
2147 int error, errcode, errcode_valid, handled, in;
2148 struct vmxctx *vmxctx;
2149 struct vlapic *vlapic;
2150 struct vm_inout_str *vis;
2151 struct vm_task_switch *ts;
2152 uint32_t eax, ecx, edx, idtvec_info, idtvec_err, intr_info, inst_info;
2153 uint32_t intr_type, intr_vec, reason;
2154 uint64_t exitintinfo, qual, gpa;
2157 CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_VIRTUAL_NMI) != 0);
2158 CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_NMI_EXITING) != 0);
2160 handled = UNHANDLED;
2161 vmxctx = &vmx->ctx[vcpu];
2163 qual = vmexit->u.vmx.exit_qualification;
2164 reason = vmexit->u.vmx.exit_reason;
2165 vmexit->exitcode = VM_EXITCODE_BOGUS;
2167 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_COUNT, 1);
2170 * VM-entry failures during or after loading guest state.
2172 * These VM-exits are uncommon but must be handled specially
2173 * as most VM-exit fields are not populated as usual.
2175 if (__predict_false(reason == EXIT_REASON_MCE_DURING_ENTRY)) {
2176 VCPU_CTR0(vmx->vm, vcpu, "Handling MCE during VM-entry");
2177 __asm __volatile("int $18");
2182 * VM exits that can be triggered during event delivery need to
2183 * be handled specially by re-injecting the event if the IDT
2184 * vectoring information field's valid bit is set.
2186 * See "Information for VM Exits During Event Delivery" in Intel SDM
2189 idtvec_info = vmcs_idt_vectoring_info();
2190 if (idtvec_info & VMCS_IDT_VEC_VALID) {
2191 idtvec_info &= ~(1 << 12); /* clear undefined bit */
2192 exitintinfo = idtvec_info;
2193 if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) {
2194 idtvec_err = vmcs_idt_vectoring_err();
2195 exitintinfo |= (uint64_t)idtvec_err << 32;
2197 error = vm_exit_intinfo(vmx->vm, vcpu, exitintinfo);
2198 KASSERT(error == 0, ("%s: vm_set_intinfo error %d",
2202 * If 'virtual NMIs' are being used and the VM-exit
2203 * happened while injecting an NMI during the previous
2204 * VM-entry, then clear "blocking by NMI" in the
2205 * Guest Interruptibility-State so the NMI can be
2206 * reinjected on the subsequent VM-entry.
2208 * However, if the NMI was being delivered through a task
2209 * gate, then the new task must start execution with NMIs
2210 * blocked so don't clear NMI blocking in this case.
2212 intr_type = idtvec_info & VMCS_INTR_T_MASK;
2213 if (intr_type == VMCS_INTR_T_NMI) {
2214 if (reason != EXIT_REASON_TASK_SWITCH)
2215 vmx_clear_nmi_blocking(vmx, vcpu);
2217 vmx_assert_nmi_blocking(vmx, vcpu);
2221 * Update VM-entry instruction length if the event being
2222 * delivered was a software interrupt or software exception.
2224 if (intr_type == VMCS_INTR_T_SWINTR ||
2225 intr_type == VMCS_INTR_T_PRIV_SWEXCEPTION ||
2226 intr_type == VMCS_INTR_T_SWEXCEPTION) {
2227 vmcs_write(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length);
2232 case EXIT_REASON_TASK_SWITCH:
2233 ts = &vmexit->u.task_switch;
2234 ts->tsssel = qual & 0xffff;
2235 ts->reason = vmx_task_switch_reason(qual);
2237 ts->errcode_valid = 0;
2238 vmx_paging_info(&ts->paging);
2240 * If the task switch was due to a CALL, JMP, IRET, software
2241 * interrupt (INT n) or software exception (INT3, INTO),
2242 * then the saved %rip references the instruction that caused
2243 * the task switch. The instruction length field in the VMCS
2244 * is valid in this case.
2246 * In all other cases (e.g., NMI, hardware exception) the
2247 * saved %rip is one that would have been saved in the old TSS
2248 * had the task switch completed normally so the instruction
2249 * length field is not needed in this case and is explicitly
2252 if (ts->reason == TSR_IDT_GATE) {
2253 KASSERT(idtvec_info & VMCS_IDT_VEC_VALID,
2254 ("invalid idtvec_info %#x for IDT task switch",
2256 intr_type = idtvec_info & VMCS_INTR_T_MASK;
2257 if (intr_type != VMCS_INTR_T_SWINTR &&
2258 intr_type != VMCS_INTR_T_SWEXCEPTION &&
2259 intr_type != VMCS_INTR_T_PRIV_SWEXCEPTION) {
2260 /* Task switch triggered by external event */
2262 vmexit->inst_length = 0;
2263 if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) {
2264 ts->errcode_valid = 1;
2265 ts->errcode = vmcs_idt_vectoring_err();
2269 vmexit->exitcode = VM_EXITCODE_TASK_SWITCH;
2270 VCPU_CTR4(vmx->vm, vcpu, "task switch reason %d, tss 0x%04x, "
2271 "%s errcode 0x%016lx", ts->reason, ts->tsssel,
2272 ts->ext ? "external" : "internal",
2273 ((uint64_t)ts->errcode << 32) | ts->errcode_valid);
2275 case EXIT_REASON_CR_ACCESS:
2276 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CR_ACCESS, 1);
2277 switch (qual & 0xf) {
2279 handled = vmx_emulate_cr0_access(vmx, vcpu, qual);
2282 handled = vmx_emulate_cr4_access(vmx, vcpu, qual);
2285 handled = vmx_emulate_cr8_access(vmx, vcpu, qual);
2289 case EXIT_REASON_RDMSR:
2290 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_RDMSR, 1);
2292 ecx = vmxctx->guest_rcx;
2293 VCPU_CTR1(vmx->vm, vcpu, "rdmsr 0x%08x", ecx);
2294 error = emulate_rdmsr(vmx, vcpu, ecx, &retu);
2296 vmexit->exitcode = VM_EXITCODE_RDMSR;
2297 vmexit->u.msr.code = ecx;
2301 /* Return to userspace with a valid exitcode */
2302 KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
2303 ("emulate_rdmsr retu with bogus exitcode"));
2306 case EXIT_REASON_WRMSR:
2307 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_WRMSR, 1);
2309 eax = vmxctx->guest_rax;
2310 ecx = vmxctx->guest_rcx;
2311 edx = vmxctx->guest_rdx;
2312 VCPU_CTR2(vmx->vm, vcpu, "wrmsr 0x%08x value 0x%016lx",
2313 ecx, (uint64_t)edx << 32 | eax);
2314 error = emulate_wrmsr(vmx, vcpu, ecx,
2315 (uint64_t)edx << 32 | eax, &retu);
2317 vmexit->exitcode = VM_EXITCODE_WRMSR;
2318 vmexit->u.msr.code = ecx;
2319 vmexit->u.msr.wval = (uint64_t)edx << 32 | eax;
2323 /* Return to userspace with a valid exitcode */
2324 KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
2325 ("emulate_wrmsr retu with bogus exitcode"));
2328 case EXIT_REASON_HLT:
2329 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_HLT, 1);
2330 vmexit->exitcode = VM_EXITCODE_HLT;
2331 vmexit->u.hlt.rflags = vmcs_read(VMCS_GUEST_RFLAGS);
2332 if (virtual_interrupt_delivery)
2333 vmexit->u.hlt.intr_status =
2334 vmcs_read(VMCS_GUEST_INTR_STATUS);
2336 vmexit->u.hlt.intr_status = 0;
2338 case EXIT_REASON_MTF:
2339 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_MTRAP, 1);
2340 vmexit->exitcode = VM_EXITCODE_MTRAP;
2341 vmexit->inst_length = 0;
2343 case EXIT_REASON_PAUSE:
2344 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_PAUSE, 1);
2345 vmexit->exitcode = VM_EXITCODE_PAUSE;
2347 case EXIT_REASON_INTR_WINDOW:
2348 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INTR_WINDOW, 1);
2349 vmx_clear_int_window_exiting(vmx, vcpu);
2351 case EXIT_REASON_EXT_INTR:
2353 * External interrupts serve only to cause VM exits and allow
2354 * the host interrupt handler to run.
2356 * If this external interrupt triggers a virtual interrupt
2357 * to a VM, then that state will be recorded by the
2358 * host interrupt handler in the VM's softc. We will inject
2359 * this virtual interrupt during the subsequent VM enter.
2361 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
2364 * XXX: Ignore this exit if VMCS_INTR_VALID is not set.
2365 * This appears to be a bug in VMware Fusion?
2367 if (!(intr_info & VMCS_INTR_VALID))
2369 KASSERT((intr_info & VMCS_INTR_VALID) != 0 &&
2370 (intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_HWINTR,
2371 ("VM exit interruption info invalid: %#x", intr_info));
2372 vmx_trigger_hostintr(intr_info & 0xff);
2375 * This is special. We want to treat this as an 'handled'
2376 * VM-exit but not increment the instruction pointer.
2378 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXTINT, 1);
2380 case EXIT_REASON_NMI_WINDOW:
2381 /* Exit to allow the pending virtual NMI to be injected */
2382 if (vm_nmi_pending(vmx->vm, vcpu))
2383 vmx_inject_nmi(vmx, vcpu);
2384 vmx_clear_nmi_window_exiting(vmx, vcpu);
2385 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NMI_WINDOW, 1);
2387 case EXIT_REASON_INOUT:
2388 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INOUT, 1);
2389 vmexit->exitcode = VM_EXITCODE_INOUT;
2390 vmexit->u.inout.bytes = (qual & 0x7) + 1;
2391 vmexit->u.inout.in = in = (qual & 0x8) ? 1 : 0;
2392 vmexit->u.inout.string = (qual & 0x10) ? 1 : 0;
2393 vmexit->u.inout.rep = (qual & 0x20) ? 1 : 0;
2394 vmexit->u.inout.port = (uint16_t)(qual >> 16);
2395 vmexit->u.inout.eax = (uint32_t)(vmxctx->guest_rax);
2396 if (vmexit->u.inout.string) {
2397 inst_info = vmcs_read(VMCS_EXIT_INSTRUCTION_INFO);
2398 vmexit->exitcode = VM_EXITCODE_INOUT_STR;
2399 vis = &vmexit->u.inout_str;
2400 vmx_paging_info(&vis->paging);
2401 vis->rflags = vmcs_read(VMCS_GUEST_RFLAGS);
2402 vis->cr0 = vmcs_read(VMCS_GUEST_CR0);
2403 vis->index = inout_str_index(vmx, vcpu, in);
2404 vis->count = inout_str_count(vmx, vcpu, vis->inout.rep);
2405 vis->addrsize = inout_str_addrsize(inst_info);
2406 inout_str_seginfo(vmx, vcpu, inst_info, in, vis);
2409 case EXIT_REASON_CPUID:
2410 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CPUID, 1);
2411 handled = vmx_handle_cpuid(vmx->vm, vcpu, vmxctx);
2413 case EXIT_REASON_EXCEPTION:
2414 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXCEPTION, 1);
2415 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
2416 KASSERT((intr_info & VMCS_INTR_VALID) != 0,
2417 ("VM exit interruption info invalid: %#x", intr_info));
2419 intr_vec = intr_info & 0xff;
2420 intr_type = intr_info & VMCS_INTR_T_MASK;
2423 * If Virtual NMIs control is 1 and the VM-exit is due to a
2424 * fault encountered during the execution of IRET then we must
2425 * restore the state of "virtual-NMI blocking" before resuming
2428 * See "Resuming Guest Software after Handling an Exception".
2429 * See "Information for VM Exits Due to Vectored Events".
2431 if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 &&
2432 (intr_vec != IDT_DF) &&
2433 (intr_info & EXIT_QUAL_NMIUDTI) != 0)
2434 vmx_restore_nmi_blocking(vmx, vcpu);
2437 * The NMI has already been handled in vmx_exit_handle_nmi().
2439 if (intr_type == VMCS_INTR_T_NMI)
2443 * Call the machine check handler by hand. Also don't reflect
2444 * the machine check back into the guest.
2446 if (intr_vec == IDT_MC) {
2447 VCPU_CTR0(vmx->vm, vcpu, "Vectoring to MCE handler");
2448 __asm __volatile("int $18");
2452 if (intr_vec == IDT_PF) {
2453 error = vmxctx_setreg(vmxctx, VM_REG_GUEST_CR2, qual);
2454 KASSERT(error == 0, ("%s: vmxctx_setreg(cr2) error %d",
2459 * Software exceptions exhibit trap-like behavior. This in
2460 * turn requires populating the VM-entry instruction length
2461 * so that the %rip in the trap frame is past the INT3/INTO
2464 if (intr_type == VMCS_INTR_T_SWEXCEPTION)
2465 vmcs_write(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length);
2467 /* Reflect all other exceptions back into the guest */
2468 errcode_valid = errcode = 0;
2469 if (intr_info & VMCS_INTR_DEL_ERRCODE) {
2471 errcode = vmcs_read(VMCS_EXIT_INTR_ERRCODE);
2473 VCPU_CTR2(vmx->vm, vcpu, "Reflecting exception %d/%#x into "
2474 "the guest", intr_vec, errcode);
2475 error = vm_inject_exception(vmx->vm, vcpu, intr_vec,
2476 errcode_valid, errcode, 0);
2477 KASSERT(error == 0, ("%s: vm_inject_exception error %d",
2481 case EXIT_REASON_EPT_FAULT:
2483 * If 'gpa' lies within the address space allocated to
2484 * memory then this must be a nested page fault otherwise
2485 * this must be an instruction that accesses MMIO space.
2488 if (vm_mem_allocated(vmx->vm, vcpu, gpa) ||
2489 apic_access_fault(vmx, vcpu, gpa)) {
2490 vmexit->exitcode = VM_EXITCODE_PAGING;
2491 vmexit->inst_length = 0;
2492 vmexit->u.paging.gpa = gpa;
2493 vmexit->u.paging.fault_type = ept_fault_type(qual);
2494 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NESTED_FAULT, 1);
2495 } else if (ept_emulation_fault(qual)) {
2496 vmexit_inst_emul(vmexit, gpa, vmcs_gla());
2497 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INST_EMUL, 1);
2500 * If Virtual NMIs control is 1 and the VM-exit is due to an
2501 * EPT fault during the execution of IRET then we must restore
2502 * the state of "virtual-NMI blocking" before resuming.
2504 * See description of "NMI unblocking due to IRET" in
2505 * "Exit Qualification for EPT Violations".
2507 if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 &&
2508 (qual & EXIT_QUAL_NMIUDTI) != 0)
2509 vmx_restore_nmi_blocking(vmx, vcpu);
2511 case EXIT_REASON_VIRTUALIZED_EOI:
2512 vmexit->exitcode = VM_EXITCODE_IOAPIC_EOI;
2513 vmexit->u.ioapic_eoi.vector = qual & 0xFF;
2514 vmexit->inst_length = 0; /* trap-like */
2516 case EXIT_REASON_APIC_ACCESS:
2517 handled = vmx_handle_apic_access(vmx, vcpu, vmexit);
2519 case EXIT_REASON_APIC_WRITE:
2521 * APIC-write VM exit is trap-like so the %rip is already
2522 * pointing to the next instruction.
2524 vmexit->inst_length = 0;
2525 vlapic = vm_lapic(vmx->vm, vcpu);
2526 handled = vmx_handle_apic_write(vmx, vcpu, vlapic, qual);
2528 case EXIT_REASON_XSETBV:
2529 handled = vmx_emulate_xsetbv(vmx, vcpu, vmexit);
2531 case EXIT_REASON_MONITOR:
2532 vmexit->exitcode = VM_EXITCODE_MONITOR;
2534 case EXIT_REASON_MWAIT:
2535 vmexit->exitcode = VM_EXITCODE_MWAIT;
2538 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_UNKNOWN, 1);
2544 * It is possible that control is returned to userland
2545 * even though we were able to handle the VM exit in the
2548 * In such a case we want to make sure that the userland
2549 * restarts guest execution at the instruction *after*
2550 * the one we just processed. Therefore we update the
2551 * guest rip in the VMCS and in 'vmexit'.
2553 vmexit->rip += vmexit->inst_length;
2554 vmexit->inst_length = 0;
2555 vmcs_write(VMCS_GUEST_RIP, vmexit->rip);
2557 if (vmexit->exitcode == VM_EXITCODE_BOGUS) {
2559 * If this VM exit was not claimed by anybody then
2560 * treat it as a generic VMX exit.
2562 vmexit->exitcode = VM_EXITCODE_VMX;
2563 vmexit->u.vmx.status = VM_SUCCESS;
2564 vmexit->u.vmx.inst_type = 0;
2565 vmexit->u.vmx.inst_error = 0;
2568 * The exitcode and collateral have been populated.
2569 * The VM exit will be processed further in userland.
2576 static __inline void
2577 vmx_exit_inst_error(struct vmxctx *vmxctx, int rc, struct vm_exit *vmexit)
2580 KASSERT(vmxctx->inst_fail_status != VM_SUCCESS,
2581 ("vmx_exit_inst_error: invalid inst_fail_status %d",
2582 vmxctx->inst_fail_status));
2584 vmexit->inst_length = 0;
2585 vmexit->exitcode = VM_EXITCODE_VMX;
2586 vmexit->u.vmx.status = vmxctx->inst_fail_status;
2587 vmexit->u.vmx.inst_error = vmcs_instruction_error();
2588 vmexit->u.vmx.exit_reason = ~0;
2589 vmexit->u.vmx.exit_qualification = ~0;
2592 case VMX_VMRESUME_ERROR:
2593 case VMX_VMLAUNCH_ERROR:
2594 case VMX_INVEPT_ERROR:
2595 vmexit->u.vmx.inst_type = rc;
2598 panic("vm_exit_inst_error: vmx_enter_guest returned %d", rc);
2603 * If the NMI-exiting VM execution control is set to '1' then an NMI in
2604 * non-root operation causes a VM-exit. NMI blocking is in effect so it is
2605 * sufficient to simply vector to the NMI handler via a software interrupt.
2606 * However, this must be done before maskable interrupts are enabled
2607 * otherwise the "iret" issued by an interrupt handler will incorrectly
2608 * clear NMI blocking.
2610 static __inline void
2611 vmx_exit_handle_nmi(struct vmx *vmx, int vcpuid, struct vm_exit *vmexit)
2615 KASSERT((read_rflags() & PSL_I) == 0, ("interrupts enabled"));
2617 if (vmexit->u.vmx.exit_reason != EXIT_REASON_EXCEPTION)
2620 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
2621 KASSERT((intr_info & VMCS_INTR_VALID) != 0,
2622 ("VM exit interruption info invalid: %#x", intr_info));
2624 if ((intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_NMI) {
2625 KASSERT((intr_info & 0xff) == IDT_NMI, ("VM exit due "
2626 "to NMI has invalid vector: %#x", intr_info));
2627 VCPU_CTR0(vmx->vm, vcpuid, "Vectoring to NMI handler");
2628 __asm __volatile("int $2");
2632 static __inline void
2633 vmx_dr_enter_guest(struct vmxctx *vmxctx)
2637 /* Save host control debug registers. */
2638 vmxctx->host_dr7 = rdr7();
2639 vmxctx->host_debugctl = rdmsr(MSR_DEBUGCTLMSR);
2642 * Disable debugging in DR7 and DEBUGCTL to avoid triggering
2643 * exceptions in the host based on the guest DRx values. The
2644 * guest DR7 and DEBUGCTL are saved/restored in the VMCS.
2647 wrmsr(MSR_DEBUGCTLMSR, 0);
2650 * Disable single stepping the kernel to avoid corrupting the
2651 * guest DR6. A debugger might still be able to corrupt the
2652 * guest DR6 by setting a breakpoint after this point and then
2655 rflags = read_rflags();
2656 vmxctx->host_tf = rflags & PSL_T;
2657 write_rflags(rflags & ~PSL_T);
2659 /* Save host debug registers. */
2660 vmxctx->host_dr0 = rdr0();
2661 vmxctx->host_dr1 = rdr1();
2662 vmxctx->host_dr2 = rdr2();
2663 vmxctx->host_dr3 = rdr3();
2664 vmxctx->host_dr6 = rdr6();
2666 /* Restore guest debug registers. */
2667 load_dr0(vmxctx->guest_dr0);
2668 load_dr1(vmxctx->guest_dr1);
2669 load_dr2(vmxctx->guest_dr2);
2670 load_dr3(vmxctx->guest_dr3);
2671 load_dr6(vmxctx->guest_dr6);
2674 static __inline void
2675 vmx_dr_leave_guest(struct vmxctx *vmxctx)
2678 /* Save guest debug registers. */
2679 vmxctx->guest_dr0 = rdr0();
2680 vmxctx->guest_dr1 = rdr1();
2681 vmxctx->guest_dr2 = rdr2();
2682 vmxctx->guest_dr3 = rdr3();
2683 vmxctx->guest_dr6 = rdr6();
2686 * Restore host debug registers. Restore DR7, DEBUGCTL, and
2689 load_dr0(vmxctx->host_dr0);
2690 load_dr1(vmxctx->host_dr1);
2691 load_dr2(vmxctx->host_dr2);
2692 load_dr3(vmxctx->host_dr3);
2693 load_dr6(vmxctx->host_dr6);
2694 wrmsr(MSR_DEBUGCTLMSR, vmxctx->host_debugctl);
2695 load_dr7(vmxctx->host_dr7);
2696 write_rflags(read_rflags() | vmxctx->host_tf);
2700 vmx_run(void *arg, int vcpu, register_t rip, pmap_t pmap,
2701 struct vm_eventinfo *evinfo)
2703 int rc, handled, launched;
2706 struct vmxctx *vmxctx;
2708 struct vm_exit *vmexit;
2709 struct vlapic *vlapic;
2710 uint32_t exit_reason;
2711 struct region_descriptor gdtr, idtr;
2716 vmcs = &vmx->vmcs[vcpu];
2717 vmxctx = &vmx->ctx[vcpu];
2718 vlapic = vm_lapic(vm, vcpu);
2719 vmexit = vm_exitinfo(vm, vcpu);
2722 KASSERT(vmxctx->pmap == pmap,
2723 ("pmap %p different than ctx pmap %p", pmap, vmxctx->pmap));
2725 vmx_msr_guest_enter(vmx, vcpu);
2731 * We do this every time because we may setup the virtual machine
2732 * from a different process than the one that actually runs it.
2734 * If the life of a virtual machine was spent entirely in the context
2735 * of a single process we could do this once in vmx_vminit().
2737 vmcs_write(VMCS_HOST_CR3, rcr3());
2739 vmcs_write(VMCS_GUEST_RIP, rip);
2740 vmx_set_pcpu_defaults(vmx, vcpu, pmap);
2742 KASSERT(vmcs_guest_rip() == rip, ("%s: vmcs guest rip mismatch "
2743 "%#lx/%#lx", __func__, vmcs_guest_rip(), rip));
2745 handled = UNHANDLED;
2747 * Interrupts are disabled from this point on until the
2748 * guest starts executing. This is done for the following
2751 * If an AST is asserted on this thread after the check below,
2752 * then the IPI_AST notification will not be lost, because it
2753 * will cause a VM exit due to external interrupt as soon as
2754 * the guest state is loaded.
2756 * A posted interrupt after 'vmx_inject_interrupts()' will
2757 * not be "lost" because it will be held pending in the host
2758 * APIC because interrupts are disabled. The pending interrupt
2759 * will be recognized as soon as the guest state is loaded.
2761 * The same reasoning applies to the IPI generated by
2762 * pmap_invalidate_ept().
2765 vmx_inject_interrupts(vmx, vcpu, vlapic, rip);
2768 * Check for vcpu suspension after injecting events because
2769 * vmx_inject_interrupts() can suspend the vcpu due to a
2772 if (vcpu_suspended(evinfo)) {
2774 vm_exit_suspended(vmx->vm, vcpu, rip);
2778 if (vcpu_rendezvous_pending(evinfo)) {
2780 vm_exit_rendezvous(vmx->vm, vcpu, rip);
2784 if (vcpu_reqidle(evinfo)) {
2786 vm_exit_reqidle(vmx->vm, vcpu, rip);
2790 if (vcpu_should_yield(vm, vcpu)) {
2792 vm_exit_astpending(vmx->vm, vcpu, rip);
2793 vmx_astpending_trace(vmx, vcpu, rip);
2799 * VM exits restore the base address but not the
2800 * limits of GDTR and IDTR. The VMCS only stores the
2801 * base address, so VM exits set the limits to 0xffff.
2802 * Save and restore the full GDTR and IDTR to restore
2805 * The VMCS does not save the LDTR at all, and VM
2806 * exits clear LDTR as if a NULL selector were loaded.
2807 * The userspace hypervisor probably doesn't use a
2808 * LDT, but save and restore it to be safe.
2814 vmx_run_trace(vmx, vcpu);
2815 vmx_dr_enter_guest(vmxctx);
2816 rc = vmx_enter_guest(vmxctx, vmx, launched);
2817 vmx_dr_leave_guest(vmxctx);
2823 /* Collect some information for VM exit processing */
2824 vmexit->rip = rip = vmcs_guest_rip();
2825 vmexit->inst_length = vmexit_instruction_length();
2826 vmexit->u.vmx.exit_reason = exit_reason = vmcs_exit_reason();
2827 vmexit->u.vmx.exit_qualification = vmcs_exit_qualification();
2829 /* Update 'nextrip' */
2830 vmx->state[vcpu].nextrip = rip;
2832 if (rc == VMX_GUEST_VMEXIT) {
2833 vmx_exit_handle_nmi(vmx, vcpu, vmexit);
2835 handled = vmx_exit_process(vmx, vcpu, vmexit);
2838 vmx_exit_inst_error(vmxctx, rc, vmexit);
2841 vmx_exit_trace(vmx, vcpu, rip, exit_reason, handled);
2846 * If a VM exit has been handled then the exitcode must be BOGUS
2847 * If a VM exit is not handled then the exitcode must not be BOGUS
2849 if ((handled && vmexit->exitcode != VM_EXITCODE_BOGUS) ||
2850 (!handled && vmexit->exitcode == VM_EXITCODE_BOGUS)) {
2851 panic("Mismatch between handled (%d) and exitcode (%d)",
2852 handled, vmexit->exitcode);
2856 vmm_stat_incr(vm, vcpu, VMEXIT_USERSPACE, 1);
2858 VCPU_CTR1(vm, vcpu, "returning from vmx_run: exitcode %d",
2862 vmx_msr_guest_exit(vmx, vcpu);
2868 vmx_vmcleanup(void *arg)
2871 struct vmx *vmx = arg;
2873 if (apic_access_virtualization(vmx, 0))
2874 vm_unmap_mmio(vmx->vm, DEFAULT_APIC_BASE, PAGE_SIZE);
2876 for (i = 0; i < VM_MAXCPU; i++)
2877 vpid_free(vmx->state[i].vpid);
2885 vmxctx_regptr(struct vmxctx *vmxctx, int reg)
2889 case VM_REG_GUEST_RAX:
2890 return (&vmxctx->guest_rax);
2891 case VM_REG_GUEST_RBX:
2892 return (&vmxctx->guest_rbx);
2893 case VM_REG_GUEST_RCX:
2894 return (&vmxctx->guest_rcx);
2895 case VM_REG_GUEST_RDX:
2896 return (&vmxctx->guest_rdx);
2897 case VM_REG_GUEST_RSI:
2898 return (&vmxctx->guest_rsi);
2899 case VM_REG_GUEST_RDI:
2900 return (&vmxctx->guest_rdi);
2901 case VM_REG_GUEST_RBP:
2902 return (&vmxctx->guest_rbp);
2903 case VM_REG_GUEST_R8:
2904 return (&vmxctx->guest_r8);
2905 case VM_REG_GUEST_R9:
2906 return (&vmxctx->guest_r9);
2907 case VM_REG_GUEST_R10:
2908 return (&vmxctx->guest_r10);
2909 case VM_REG_GUEST_R11:
2910 return (&vmxctx->guest_r11);
2911 case VM_REG_GUEST_R12:
2912 return (&vmxctx->guest_r12);
2913 case VM_REG_GUEST_R13:
2914 return (&vmxctx->guest_r13);
2915 case VM_REG_GUEST_R14:
2916 return (&vmxctx->guest_r14);
2917 case VM_REG_GUEST_R15:
2918 return (&vmxctx->guest_r15);
2919 case VM_REG_GUEST_CR2:
2920 return (&vmxctx->guest_cr2);
2921 case VM_REG_GUEST_DR0:
2922 return (&vmxctx->guest_dr0);
2923 case VM_REG_GUEST_DR1:
2924 return (&vmxctx->guest_dr1);
2925 case VM_REG_GUEST_DR2:
2926 return (&vmxctx->guest_dr2);
2927 case VM_REG_GUEST_DR3:
2928 return (&vmxctx->guest_dr3);
2929 case VM_REG_GUEST_DR6:
2930 return (&vmxctx->guest_dr6);
2938 vmxctx_getreg(struct vmxctx *vmxctx, int reg, uint64_t *retval)
2942 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
2950 vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val)
2954 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
2962 vmx_get_intr_shadow(struct vmx *vmx, int vcpu, int running, uint64_t *retval)
2967 error = vmcs_getreg(&vmx->vmcs[vcpu], running,
2968 VMCS_IDENT(VMCS_GUEST_INTERRUPTIBILITY), &gi);
2969 *retval = (gi & HWINTR_BLOCKING) ? 1 : 0;
2974 vmx_modify_intr_shadow(struct vmx *vmx, int vcpu, int running, uint64_t val)
2981 * Forcing the vcpu into an interrupt shadow is not supported.
2988 vmcs = &vmx->vmcs[vcpu];
2989 ident = VMCS_IDENT(VMCS_GUEST_INTERRUPTIBILITY);
2990 error = vmcs_getreg(vmcs, running, ident, &gi);
2992 gi &= ~HWINTR_BLOCKING;
2993 error = vmcs_setreg(vmcs, running, ident, gi);
2996 VCPU_CTR2(vmx->vm, vcpu, "Setting intr_shadow to %#lx %s", val,
2997 error ? "failed" : "succeeded");
3002 vmx_shadow_reg(int reg)
3009 case VM_REG_GUEST_CR0:
3010 shreg = VMCS_CR0_SHADOW;
3012 case VM_REG_GUEST_CR4:
3013 shreg = VMCS_CR4_SHADOW;
3023 vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval)
3025 int running, hostcpu;
3026 struct vmx *vmx = arg;
3028 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
3029 if (running && hostcpu != curcpu)
3030 panic("vmx_getreg: %s%d is running", vm_name(vmx->vm), vcpu);
3032 if (reg == VM_REG_GUEST_INTR_SHADOW)
3033 return (vmx_get_intr_shadow(vmx, vcpu, running, retval));
3035 if (vmxctx_getreg(&vmx->ctx[vcpu], reg, retval) == 0)
3038 return (vmcs_getreg(&vmx->vmcs[vcpu], running, reg, retval));
3042 vmx_setreg(void *arg, int vcpu, int reg, uint64_t val)
3044 int error, hostcpu, running, shadow;
3047 struct vmx *vmx = arg;
3049 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
3050 if (running && hostcpu != curcpu)
3051 panic("vmx_setreg: %s%d is running", vm_name(vmx->vm), vcpu);
3053 if (reg == VM_REG_GUEST_INTR_SHADOW)
3054 return (vmx_modify_intr_shadow(vmx, vcpu, running, val));
3056 if (vmxctx_setreg(&vmx->ctx[vcpu], reg, val) == 0)
3059 error = vmcs_setreg(&vmx->vmcs[vcpu], running, reg, val);
3063 * If the "load EFER" VM-entry control is 1 then the
3064 * value of EFER.LMA must be identical to "IA-32e mode guest"
3065 * bit in the VM-entry control.
3067 if ((entry_ctls & VM_ENTRY_LOAD_EFER) != 0 &&
3068 (reg == VM_REG_GUEST_EFER)) {
3069 vmcs_getreg(&vmx->vmcs[vcpu], running,
3070 VMCS_IDENT(VMCS_ENTRY_CTLS), &ctls);
3072 ctls |= VM_ENTRY_GUEST_LMA;
3074 ctls &= ~VM_ENTRY_GUEST_LMA;
3075 vmcs_setreg(&vmx->vmcs[vcpu], running,
3076 VMCS_IDENT(VMCS_ENTRY_CTLS), ctls);
3079 shadow = vmx_shadow_reg(reg);
3082 * Store the unmodified value in the shadow
3084 error = vmcs_setreg(&vmx->vmcs[vcpu], running,
3085 VMCS_IDENT(shadow), val);
3088 if (reg == VM_REG_GUEST_CR3) {
3090 * Invalidate the guest vcpu's TLB mappings to emulate
3091 * the behavior of updating %cr3.
3093 * XXX the processor retains global mappings when %cr3
3094 * is updated but vmx_invvpid() does not.
3096 pmap = vmx->ctx[vcpu].pmap;
3097 vmx_invvpid(vmx, vcpu, pmap, running);
3105 vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
3107 int hostcpu, running;
3108 struct vmx *vmx = arg;
3110 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
3111 if (running && hostcpu != curcpu)
3112 panic("vmx_getdesc: %s%d is running", vm_name(vmx->vm), vcpu);
3114 return (vmcs_getdesc(&vmx->vmcs[vcpu], running, reg, desc));
3118 vmx_setdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
3120 int hostcpu, running;
3121 struct vmx *vmx = arg;
3123 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
3124 if (running && hostcpu != curcpu)
3125 panic("vmx_setdesc: %s%d is running", vm_name(vmx->vm), vcpu);
3127 return (vmcs_setdesc(&vmx->vmcs[vcpu], running, reg, desc));
3131 vmx_getcap(void *arg, int vcpu, int type, int *retval)
3133 struct vmx *vmx = arg;
3139 vcap = vmx->cap[vcpu].set;
3142 case VM_CAP_HALT_EXIT:
3146 case VM_CAP_PAUSE_EXIT:
3150 case VM_CAP_MTRAP_EXIT:
3151 if (cap_monitor_trap)
3154 case VM_CAP_UNRESTRICTED_GUEST:
3155 if (cap_unrestricted_guest)
3158 case VM_CAP_ENABLE_INVPCID:
3167 *retval = (vcap & (1 << type)) ? 1 : 0;
3173 vmx_setcap(void *arg, int vcpu, int type, int val)
3175 struct vmx *vmx = arg;
3176 struct vmcs *vmcs = &vmx->vmcs[vcpu];
3188 case VM_CAP_HALT_EXIT:
3189 if (cap_halt_exit) {
3191 pptr = &vmx->cap[vcpu].proc_ctls;
3193 flag = PROCBASED_HLT_EXITING;
3194 reg = VMCS_PRI_PROC_BASED_CTLS;
3197 case VM_CAP_MTRAP_EXIT:
3198 if (cap_monitor_trap) {
3200 pptr = &vmx->cap[vcpu].proc_ctls;
3202 flag = PROCBASED_MTF;
3203 reg = VMCS_PRI_PROC_BASED_CTLS;
3206 case VM_CAP_PAUSE_EXIT:
3207 if (cap_pause_exit) {
3209 pptr = &vmx->cap[vcpu].proc_ctls;
3211 flag = PROCBASED_PAUSE_EXITING;
3212 reg = VMCS_PRI_PROC_BASED_CTLS;
3215 case VM_CAP_UNRESTRICTED_GUEST:
3216 if (cap_unrestricted_guest) {
3218 pptr = &vmx->cap[vcpu].proc_ctls2;
3220 flag = PROCBASED2_UNRESTRICTED_GUEST;
3221 reg = VMCS_SEC_PROC_BASED_CTLS;
3224 case VM_CAP_ENABLE_INVPCID:
3227 pptr = &vmx->cap[vcpu].proc_ctls2;
3229 flag = PROCBASED2_ENABLE_INVPCID;
3230 reg = VMCS_SEC_PROC_BASED_CTLS;
3244 error = vmwrite(reg, baseval);
3251 * Update optional stored flags, and record
3259 vmx->cap[vcpu].set |= (1 << type);
3261 vmx->cap[vcpu].set &= ~(1 << type);
3270 struct vlapic vlapic;
3271 struct pir_desc *pir_desc;
3276 #define VPR_PRIO_BIT(vpr) (1 << ((vpr) >> 4))
3278 #define VMX_CTR_PIR(vm, vcpuid, pir_desc, notify, vector, level, msg) \
3280 VCPU_CTR2(vm, vcpuid, msg " assert %s-triggered vector %d", \
3281 level ? "level" : "edge", vector); \
3282 VCPU_CTR1(vm, vcpuid, msg " pir0 0x%016lx", pir_desc->pir[0]); \
3283 VCPU_CTR1(vm, vcpuid, msg " pir1 0x%016lx", pir_desc->pir[1]); \
3284 VCPU_CTR1(vm, vcpuid, msg " pir2 0x%016lx", pir_desc->pir[2]); \
3285 VCPU_CTR1(vm, vcpuid, msg " pir3 0x%016lx", pir_desc->pir[3]); \
3286 VCPU_CTR1(vm, vcpuid, msg " notify: %s", notify ? "yes" : "no");\
3290 * vlapic->ops handlers that utilize the APICv hardware assist described in
3291 * Chapter 29 of the Intel SDM.
3294 vmx_set_intr_ready(struct vlapic *vlapic, int vector, bool level)
3296 struct vlapic_vtx *vlapic_vtx;
3297 struct pir_desc *pir_desc;
3299 int idx, notify = 0;
3301 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3302 pir_desc = vlapic_vtx->pir_desc;
3305 * Keep track of interrupt requests in the PIR descriptor. This is
3306 * because the virtual APIC page pointed to by the VMCS cannot be
3307 * modified if the vcpu is running.
3310 mask = 1UL << (vector % 64);
3311 atomic_set_long(&pir_desc->pir[idx], mask);
3314 * A notification is required whenever the 'pending' bit makes a
3315 * transition from 0->1.
3317 * Even if the 'pending' bit is already asserted, notification about
3318 * the incoming interrupt may still be necessary. For example, if a
3319 * vCPU is HLTed with a high PPR, a low priority interrupt would cause
3320 * the 0->1 'pending' transition with a notification, but the vCPU
3321 * would ignore the interrupt for the time being. The same vCPU would
3322 * need to then be notified if a high-priority interrupt arrived which
3323 * satisfied the PPR.
3325 * The priorities of interrupts injected while 'pending' is asserted
3326 * are tracked in a custom bitfield 'pending_prio'. Should the
3327 * to-be-injected interrupt exceed the priorities already present, the
3328 * notification is sent. The priorities recorded in 'pending_prio' are
3329 * cleared whenever the 'pending' bit makes another 0->1 transition.
3331 if (atomic_cmpset_long(&pir_desc->pending, 0, 1) != 0) {
3333 vlapic_vtx->pending_prio = 0;
3335 const u_int old_prio = vlapic_vtx->pending_prio;
3336 const u_int prio_bit = VPR_PRIO_BIT(vector & APIC_TPR_INT);
3338 if ((old_prio & prio_bit) == 0 && prio_bit > old_prio) {
3339 atomic_set_int(&vlapic_vtx->pending_prio, prio_bit);
3344 VMX_CTR_PIR(vlapic->vm, vlapic->vcpuid, pir_desc, notify, vector,
3345 level, "vmx_set_intr_ready");
3350 vmx_pending_intr(struct vlapic *vlapic, int *vecptr)
3352 struct vlapic_vtx *vlapic_vtx;
3353 struct pir_desc *pir_desc;
3354 struct LAPIC *lapic;
3355 uint64_t pending, pirval;
3360 * This function is only expected to be called from the 'HLT' exit
3361 * handler which does not care about the vector that is pending.
3363 KASSERT(vecptr == NULL, ("vmx_pending_intr: vecptr must be NULL"));
3365 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3366 pir_desc = vlapic_vtx->pir_desc;
3368 pending = atomic_load_acq_long(&pir_desc->pending);
3371 * While a virtual interrupt may have already been
3372 * processed the actual delivery maybe pending the
3373 * interruptibility of the guest. Recognize a pending
3374 * interrupt by reevaluating virtual interrupts
3375 * following Section 29.2.1 in the Intel SDM Volume 3.
3377 struct vm_exit *vmexit;
3380 vmexit = vm_exitinfo(vlapic->vm, vlapic->vcpuid);
3381 KASSERT(vmexit->exitcode == VM_EXITCODE_HLT,
3382 ("vmx_pending_intr: exitcode not 'HLT'"));
3383 rvi = vmexit->u.hlt.intr_status & APIC_TPR_INT;
3384 lapic = vlapic->apic_page;
3385 ppr = lapic->ppr & APIC_TPR_INT;
3394 * If there is an interrupt pending then it will be recognized only
3395 * if its priority is greater than the processor priority.
3397 * Special case: if the processor priority is zero then any pending
3398 * interrupt will be recognized.
3400 lapic = vlapic->apic_page;
3401 ppr = lapic->ppr & APIC_TPR_INT;
3405 VCPU_CTR1(vlapic->vm, vlapic->vcpuid, "HLT with non-zero PPR %d",
3409 for (i = 3; i >= 0; i--) {
3410 pirval = pir_desc->pir[i];
3412 vpr = (i * 64 + flsl(pirval) - 1) & APIC_TPR_INT;
3418 * If the highest-priority pending interrupt falls short of the
3419 * processor priority of this vCPU, ensure that 'pending_prio' does not
3420 * have any stale bits which would preclude a higher-priority interrupt
3421 * from incurring a notification later.
3424 const u_int prio_bit = VPR_PRIO_BIT(vpr);
3425 const u_int old = vlapic_vtx->pending_prio;
3427 if (old > prio_bit && (old & prio_bit) == 0) {
3428 vlapic_vtx->pending_prio = prio_bit;
3436 vmx_intr_accepted(struct vlapic *vlapic, int vector)
3439 panic("vmx_intr_accepted: not expected to be called");
3443 vmx_set_tmr(struct vlapic *vlapic, int vector, bool level)
3445 struct vlapic_vtx *vlapic_vtx;
3450 KASSERT(vector >= 0 && vector <= 255, ("invalid vector %d", vector));
3451 KASSERT(!vcpu_is_running(vlapic->vm, vlapic->vcpuid, NULL),
3452 ("vmx_set_tmr: vcpu cannot be running"));
3454 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3455 vmx = vlapic_vtx->vmx;
3456 vmcs = &vmx->vmcs[vlapic->vcpuid];
3457 mask = 1UL << (vector % 64);
3460 val = vmcs_read(VMCS_EOI_EXIT(vector));
3465 vmcs_write(VMCS_EOI_EXIT(vector), val);
3470 vmx_enable_x2apic_mode(struct vlapic *vlapic)
3474 uint32_t proc_ctls2;
3477 vcpuid = vlapic->vcpuid;
3478 vmx = ((struct vlapic_vtx *)vlapic)->vmx;
3479 vmcs = &vmx->vmcs[vcpuid];
3481 proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
3482 KASSERT((proc_ctls2 & PROCBASED2_VIRTUALIZE_APIC_ACCESSES) != 0,
3483 ("%s: invalid proc_ctls2 %#x", __func__, proc_ctls2));
3485 proc_ctls2 &= ~PROCBASED2_VIRTUALIZE_APIC_ACCESSES;
3486 proc_ctls2 |= PROCBASED2_VIRTUALIZE_X2APIC_MODE;
3487 vmx->cap[vcpuid].proc_ctls2 = proc_ctls2;
3490 vmcs_write(VMCS_SEC_PROC_BASED_CTLS, proc_ctls2);
3493 if (vlapic->vcpuid == 0) {
3495 * The nested page table mappings are shared by all vcpus
3496 * so unmap the APIC access page just once.
3498 error = vm_unmap_mmio(vmx->vm, DEFAULT_APIC_BASE, PAGE_SIZE);
3499 KASSERT(error == 0, ("%s: vm_unmap_mmio error %d",
3503 * The MSR bitmap is shared by all vcpus so modify it only
3504 * once in the context of vcpu 0.
3506 error = vmx_allow_x2apic_msrs(vmx);
3507 KASSERT(error == 0, ("%s: vmx_allow_x2apic_msrs error %d",
3513 vmx_post_intr(struct vlapic *vlapic, int hostcpu)
3516 ipi_cpu(hostcpu, pirvec);
3520 * Transfer the pending interrupts in the PIR descriptor to the IRR
3521 * in the virtual APIC page.
3524 vmx_inject_pir(struct vlapic *vlapic)
3526 struct vlapic_vtx *vlapic_vtx;
3527 struct pir_desc *pir_desc;
3528 struct LAPIC *lapic;
3529 uint64_t val, pirval;
3530 int rvi, pirbase = -1;
3531 uint16_t intr_status_old, intr_status_new;
3533 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3534 pir_desc = vlapic_vtx->pir_desc;
3535 if (atomic_cmpset_long(&pir_desc->pending, 1, 0) == 0) {
3536 VCPU_CTR0(vlapic->vm, vlapic->vcpuid, "vmx_inject_pir: "
3537 "no posted interrupt pending");
3543 lapic = vlapic->apic_page;
3545 val = atomic_readandclear_long(&pir_desc->pir[0]);
3548 lapic->irr1 |= val >> 32;
3553 val = atomic_readandclear_long(&pir_desc->pir[1]);
3556 lapic->irr3 |= val >> 32;
3561 val = atomic_readandclear_long(&pir_desc->pir[2]);
3564 lapic->irr5 |= val >> 32;
3569 val = atomic_readandclear_long(&pir_desc->pir[3]);
3572 lapic->irr7 |= val >> 32;
3577 VLAPIC_CTR_IRR(vlapic, "vmx_inject_pir");
3580 * Update RVI so the processor can evaluate pending virtual
3581 * interrupts on VM-entry.
3583 * It is possible for pirval to be 0 here, even though the
3584 * pending bit has been set. The scenario is:
3585 * CPU-Y is sending a posted interrupt to CPU-X, which
3586 * is running a guest and processing posted interrupts in h/w.
3587 * CPU-X will eventually exit and the state seen in s/w is
3588 * the pending bit set, but no PIR bits set.
3591 * (vm running) (host running)
3592 * rx posted interrupt
3595 * READ/CLEAR PIR bits
3598 * pending bit set, PIR 0
3601 rvi = pirbase + flsl(pirval) - 1;
3602 intr_status_old = vmcs_read(VMCS_GUEST_INTR_STATUS);
3603 intr_status_new = (intr_status_old & 0xFF00) | rvi;
3604 if (intr_status_new > intr_status_old) {
3605 vmcs_write(VMCS_GUEST_INTR_STATUS, intr_status_new);
3606 VCPU_CTR2(vlapic->vm, vlapic->vcpuid, "vmx_inject_pir: "
3607 "guest_intr_status changed from 0x%04x to 0x%04x",
3608 intr_status_old, intr_status_new);
3613 static struct vlapic *
3614 vmx_vlapic_init(void *arg, int vcpuid)
3617 struct vlapic *vlapic;
3618 struct vlapic_vtx *vlapic_vtx;
3622 vlapic = malloc(sizeof(struct vlapic_vtx), M_VLAPIC, M_WAITOK | M_ZERO);
3623 vlapic->vm = vmx->vm;
3624 vlapic->vcpuid = vcpuid;
3625 vlapic->apic_page = (struct LAPIC *)&vmx->apic_page[vcpuid];
3627 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3628 vlapic_vtx->pir_desc = &vmx->pir_desc[vcpuid];
3629 vlapic_vtx->vmx = vmx;
3631 if (virtual_interrupt_delivery) {
3632 vlapic->ops.set_intr_ready = vmx_set_intr_ready;
3633 vlapic->ops.pending_intr = vmx_pending_intr;
3634 vlapic->ops.intr_accepted = vmx_intr_accepted;
3635 vlapic->ops.set_tmr = vmx_set_tmr;
3636 vlapic->ops.enable_x2apic_mode = vmx_enable_x2apic_mode;
3639 if (posted_interrupts)
3640 vlapic->ops.post_intr = vmx_post_intr;
3642 vlapic_init(vlapic);
3648 vmx_vlapic_cleanup(void *arg, struct vlapic *vlapic)
3651 vlapic_cleanup(vlapic);
3652 free(vlapic, M_VLAPIC);
3655 struct vmm_ops vmm_ops_intel = {