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 * The definitions of SDT probes for VMX.
203 SDT_PROBE_DEFINE3(vmm, vmx, exit, entry,
204 "struct vmx *", "int", "struct vm_exit *");
206 SDT_PROBE_DEFINE4(vmm, vmx, exit, taskswitch,
207 "struct vmx *", "int", "struct vm_exit *", "struct vm_task_switch *");
209 SDT_PROBE_DEFINE4(vmm, vmx, exit, craccess,
210 "struct vmx *", "int", "struct vm_exit *", "uint64_t");
212 SDT_PROBE_DEFINE4(vmm, vmx, exit, rdmsr,
213 "struct vmx *", "int", "struct vm_exit *", "uint32_t");
215 SDT_PROBE_DEFINE5(vmm, vmx, exit, wrmsr,
216 "struct vmx *", "int", "struct vm_exit *", "uint32_t", "uint64_t");
218 SDT_PROBE_DEFINE3(vmm, vmx, exit, halt,
219 "struct vmx *", "int", "struct vm_exit *");
221 SDT_PROBE_DEFINE3(vmm, vmx, exit, mtrap,
222 "struct vmx *", "int", "struct vm_exit *");
224 SDT_PROBE_DEFINE3(vmm, vmx, exit, pause,
225 "struct vmx *", "int", "struct vm_exit *");
227 SDT_PROBE_DEFINE3(vmm, vmx, exit, intrwindow,
228 "struct vmx *", "int", "struct vm_exit *");
230 SDT_PROBE_DEFINE4(vmm, vmx, exit, interrupt,
231 "struct vmx *", "int", "struct vm_exit *", "uint32_t");
233 SDT_PROBE_DEFINE3(vmm, vmx, exit, nmiwindow,
234 "struct vmx *", "int", "struct vm_exit *");
236 SDT_PROBE_DEFINE3(vmm, vmx, exit, inout,
237 "struct vmx *", "int", "struct vm_exit *");
239 SDT_PROBE_DEFINE3(vmm, vmx, exit, cpuid,
240 "struct vmx *", "int", "struct vm_exit *");
242 SDT_PROBE_DEFINE5(vmm, vmx, exit, exception,
243 "struct vmx *", "int", "struct vm_exit *", "uint32_t", "int");
245 SDT_PROBE_DEFINE5(vmm, vmx, exit, nestedfault,
246 "struct vmx *", "int", "struct vm_exit *", "uint64_t", "uint64_t");
248 SDT_PROBE_DEFINE4(vmm, vmx, exit, mmiofault,
249 "struct vmx *", "int", "struct vm_exit *", "uint64_t");
251 SDT_PROBE_DEFINE3(vmm, vmx, exit, eoi,
252 "struct vmx *", "int", "struct vm_exit *");
254 SDT_PROBE_DEFINE3(vmm, vmx, exit, apicaccess,
255 "struct vmx *", "int", "struct vm_exit *");
257 SDT_PROBE_DEFINE4(vmm, vmx, exit, apicwrite,
258 "struct vmx *", "int", "struct vm_exit *", "struct vlapic *");
260 SDT_PROBE_DEFINE3(vmm, vmx, exit, xsetbv,
261 "struct vmx *", "int", "struct vm_exit *");
263 SDT_PROBE_DEFINE3(vmm, vmx, exit, monitor,
264 "struct vmx *", "int", "struct vm_exit *");
266 SDT_PROBE_DEFINE3(vmm, vmx, exit, mwait,
267 "struct vmx *", "int", "struct vm_exit *");
269 SDT_PROBE_DEFINE3(vmm, vmx, exit, vminsn,
270 "struct vmx *", "int", "struct vm_exit *");
272 SDT_PROBE_DEFINE4(vmm, vmx, exit, unknown,
273 "struct vmx *", "int", "struct vm_exit *", "uint32_t");
275 SDT_PROBE_DEFINE4(vmm, vmx, exit, return,
276 "struct vmx *", "int", "struct vm_exit *", "int");
279 * Use the last page below 4GB as the APIC access address. This address is
280 * occupied by the boot firmware so it is guaranteed that it will not conflict
281 * with a page in system memory.
283 #define APIC_ACCESS_ADDRESS 0xFFFFF000
285 static int vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc);
286 static int vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval);
287 static int vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val);
288 static void vmx_inject_pir(struct vlapic *vlapic);
292 exit_reason_to_str(int reason)
294 static char reasonbuf[32];
297 case EXIT_REASON_EXCEPTION:
299 case EXIT_REASON_EXT_INTR:
301 case EXIT_REASON_TRIPLE_FAULT:
302 return "triplefault";
303 case EXIT_REASON_INIT:
305 case EXIT_REASON_SIPI:
307 case EXIT_REASON_IO_SMI:
309 case EXIT_REASON_SMI:
311 case EXIT_REASON_INTR_WINDOW:
313 case EXIT_REASON_NMI_WINDOW:
315 case EXIT_REASON_TASK_SWITCH:
317 case EXIT_REASON_CPUID:
319 case EXIT_REASON_GETSEC:
321 case EXIT_REASON_HLT:
323 case EXIT_REASON_INVD:
325 case EXIT_REASON_INVLPG:
327 case EXIT_REASON_RDPMC:
329 case EXIT_REASON_RDTSC:
331 case EXIT_REASON_RSM:
333 case EXIT_REASON_VMCALL:
335 case EXIT_REASON_VMCLEAR:
337 case EXIT_REASON_VMLAUNCH:
339 case EXIT_REASON_VMPTRLD:
341 case EXIT_REASON_VMPTRST:
343 case EXIT_REASON_VMREAD:
345 case EXIT_REASON_VMRESUME:
347 case EXIT_REASON_VMWRITE:
349 case EXIT_REASON_VMXOFF:
351 case EXIT_REASON_VMXON:
353 case EXIT_REASON_CR_ACCESS:
355 case EXIT_REASON_DR_ACCESS:
357 case EXIT_REASON_INOUT:
359 case EXIT_REASON_RDMSR:
361 case EXIT_REASON_WRMSR:
363 case EXIT_REASON_INVAL_VMCS:
365 case EXIT_REASON_INVAL_MSR:
367 case EXIT_REASON_MWAIT:
369 case EXIT_REASON_MTF:
371 case EXIT_REASON_MONITOR:
373 case EXIT_REASON_PAUSE:
375 case EXIT_REASON_MCE_DURING_ENTRY:
376 return "mce-during-entry";
377 case EXIT_REASON_TPR:
379 case EXIT_REASON_APIC_ACCESS:
380 return "apic-access";
381 case EXIT_REASON_GDTR_IDTR:
383 case EXIT_REASON_LDTR_TR:
385 case EXIT_REASON_EPT_FAULT:
387 case EXIT_REASON_EPT_MISCONFIG:
388 return "eptmisconfig";
389 case EXIT_REASON_INVEPT:
391 case EXIT_REASON_RDTSCP:
393 case EXIT_REASON_VMX_PREEMPT:
395 case EXIT_REASON_INVVPID:
397 case EXIT_REASON_WBINVD:
399 case EXIT_REASON_XSETBV:
401 case EXIT_REASON_APIC_WRITE:
404 snprintf(reasonbuf, sizeof(reasonbuf), "%d", reason);
411 vmx_allow_x2apic_msrs(struct vmx *vmx)
418 * Allow readonly access to the following x2APIC MSRs from the guest.
420 error += guest_msr_ro(vmx, MSR_APIC_ID);
421 error += guest_msr_ro(vmx, MSR_APIC_VERSION);
422 error += guest_msr_ro(vmx, MSR_APIC_LDR);
423 error += guest_msr_ro(vmx, MSR_APIC_SVR);
425 for (i = 0; i < 8; i++)
426 error += guest_msr_ro(vmx, MSR_APIC_ISR0 + i);
428 for (i = 0; i < 8; i++)
429 error += guest_msr_ro(vmx, MSR_APIC_TMR0 + i);
431 for (i = 0; i < 8; i++)
432 error += guest_msr_ro(vmx, MSR_APIC_IRR0 + i);
434 error += guest_msr_ro(vmx, MSR_APIC_ESR);
435 error += guest_msr_ro(vmx, MSR_APIC_LVT_TIMER);
436 error += guest_msr_ro(vmx, MSR_APIC_LVT_THERMAL);
437 error += guest_msr_ro(vmx, MSR_APIC_LVT_PCINT);
438 error += guest_msr_ro(vmx, MSR_APIC_LVT_LINT0);
439 error += guest_msr_ro(vmx, MSR_APIC_LVT_LINT1);
440 error += guest_msr_ro(vmx, MSR_APIC_LVT_ERROR);
441 error += guest_msr_ro(vmx, MSR_APIC_ICR_TIMER);
442 error += guest_msr_ro(vmx, MSR_APIC_DCR_TIMER);
443 error += guest_msr_ro(vmx, MSR_APIC_ICR);
446 * Allow TPR, EOI and SELF_IPI MSRs to be read and written by the guest.
448 * These registers get special treatment described in the section
449 * "Virtualizing MSR-Based APIC Accesses".
451 error += guest_msr_rw(vmx, MSR_APIC_TPR);
452 error += guest_msr_rw(vmx, MSR_APIC_EOI);
453 error += guest_msr_rw(vmx, MSR_APIC_SELF_IPI);
459 vmx_fix_cr0(u_long cr0)
462 return ((cr0 | cr0_ones_mask) & ~cr0_zeros_mask);
466 vmx_fix_cr4(u_long cr4)
469 return ((cr4 | cr4_ones_mask) & ~cr4_zeros_mask);
475 if (vpid < 0 || vpid > 0xffff)
476 panic("vpid_free: invalid vpid %d", vpid);
479 * VPIDs [0,VM_MAXCPU] are special and are not allocated from
480 * the unit number allocator.
483 if (vpid > VM_MAXCPU)
484 free_unr(vpid_unr, vpid);
488 vpid_alloc(uint16_t *vpid, int num)
492 if (num <= 0 || num > VM_MAXCPU)
493 panic("invalid number of vpids requested: %d", num);
496 * If the "enable vpid" execution control is not enabled then the
497 * VPID is required to be 0 for all vcpus.
499 if ((procbased_ctls2 & PROCBASED2_ENABLE_VPID) == 0) {
500 for (i = 0; i < num; i++)
506 * Allocate a unique VPID for each vcpu from the unit number allocator.
508 for (i = 0; i < num; i++) {
509 x = alloc_unr(vpid_unr);
517 atomic_add_int(&vpid_alloc_failed, 1);
520 * If the unit number allocator does not have enough unique
521 * VPIDs then we need to allocate from the [1,VM_MAXCPU] range.
523 * These VPIDs are not be unique across VMs but this does not
524 * affect correctness because the combined mappings are also
525 * tagged with the EP4TA which is unique for each VM.
527 * It is still sub-optimal because the invvpid will invalidate
528 * combined mappings for a particular VPID across all EP4TAs.
533 for (i = 0; i < num; i++)
542 * VPID 0 is required when the "enable VPID" execution control is
545 * VPIDs [1,VM_MAXCPU] are used as the "overflow namespace" when the
546 * unit number allocator does not have sufficient unique VPIDs to
547 * satisfy the allocation.
549 * The remaining VPIDs are managed by the unit number allocator.
551 vpid_unr = new_unrhdr(VM_MAXCPU + 1, 0xffff, NULL);
555 vmx_disable(void *arg __unused)
557 struct invvpid_desc invvpid_desc = { 0 };
558 struct invept_desc invept_desc = { 0 };
560 if (vmxon_enabled[curcpu]) {
562 * See sections 25.3.3.3 and 25.3.3.4 in Intel Vol 3b.
564 * VMXON or VMXOFF are not required to invalidate any TLB
565 * caching structures. This prevents potential retention of
566 * cached information in the TLB between distinct VMX episodes.
568 invvpid(INVVPID_TYPE_ALL_CONTEXTS, invvpid_desc);
569 invept(INVEPT_TYPE_ALL_CONTEXTS, invept_desc);
572 load_cr4(rcr4() & ~CR4_VMXE);
580 lapic_ipi_free(pirvec);
582 if (vpid_unr != NULL) {
583 delete_unrhdr(vpid_unr);
587 if (nmi_flush_l1d_sw == 1)
588 nmi_flush_l1d_sw = 0;
590 smp_rendezvous(NULL, vmx_disable, NULL, NULL);
596 vmx_enable(void *arg __unused)
599 uint64_t feature_control;
601 feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
602 if ((feature_control & IA32_FEATURE_CONTROL_LOCK) == 0 ||
603 (feature_control & IA32_FEATURE_CONTROL_VMX_EN) == 0) {
604 wrmsr(MSR_IA32_FEATURE_CONTROL,
605 feature_control | IA32_FEATURE_CONTROL_VMX_EN |
606 IA32_FEATURE_CONTROL_LOCK);
609 load_cr4(rcr4() | CR4_VMXE);
611 *(uint32_t *)vmxon_region[curcpu] = vmx_revision();
612 error = vmxon(vmxon_region[curcpu]);
614 vmxon_enabled[curcpu] = 1;
621 if (vmxon_enabled[curcpu])
622 vmxon(vmxon_region[curcpu]);
628 int error, use_tpr_shadow;
629 uint64_t basic, fixed0, fixed1, feature_control;
630 uint32_t tmp, procbased2_vid_bits;
632 /* CPUID.1:ECX[bit 5] must be 1 for processor to support VMX */
633 if (!(cpu_feature2 & CPUID2_VMX)) {
634 printf("vmx_init: processor does not support VMX operation\n");
639 * Verify that MSR_IA32_FEATURE_CONTROL lock and VMXON enable bits
640 * are set (bits 0 and 2 respectively).
642 feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
643 if ((feature_control & IA32_FEATURE_CONTROL_LOCK) == 1 &&
644 (feature_control & IA32_FEATURE_CONTROL_VMX_EN) == 0) {
645 printf("vmx_init: VMX operation disabled by BIOS\n");
650 * Verify capabilities MSR_VMX_BASIC:
651 * - bit 54 indicates support for INS/OUTS decoding
653 basic = rdmsr(MSR_VMX_BASIC);
654 if ((basic & (1UL << 54)) == 0) {
655 printf("vmx_init: processor does not support desired basic "
660 /* Check support for primary processor-based VM-execution controls */
661 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
662 MSR_VMX_TRUE_PROCBASED_CTLS,
663 PROCBASED_CTLS_ONE_SETTING,
664 PROCBASED_CTLS_ZERO_SETTING, &procbased_ctls);
666 printf("vmx_init: processor does not support desired primary "
667 "processor-based controls\n");
671 /* Clear the processor-based ctl bits that are set on demand */
672 procbased_ctls &= ~PROCBASED_CTLS_WINDOW_SETTING;
674 /* Check support for secondary processor-based VM-execution controls */
675 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
676 MSR_VMX_PROCBASED_CTLS2,
677 PROCBASED_CTLS2_ONE_SETTING,
678 PROCBASED_CTLS2_ZERO_SETTING, &procbased_ctls2);
680 printf("vmx_init: processor does not support desired secondary "
681 "processor-based controls\n");
685 /* Check support for VPID */
686 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2,
687 PROCBASED2_ENABLE_VPID, 0, &tmp);
689 procbased_ctls2 |= PROCBASED2_ENABLE_VPID;
691 /* Check support for pin-based VM-execution controls */
692 error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS,
693 MSR_VMX_TRUE_PINBASED_CTLS,
694 PINBASED_CTLS_ONE_SETTING,
695 PINBASED_CTLS_ZERO_SETTING, &pinbased_ctls);
697 printf("vmx_init: processor does not support desired "
698 "pin-based controls\n");
702 /* Check support for VM-exit controls */
703 error = vmx_set_ctlreg(MSR_VMX_EXIT_CTLS, MSR_VMX_TRUE_EXIT_CTLS,
704 VM_EXIT_CTLS_ONE_SETTING,
705 VM_EXIT_CTLS_ZERO_SETTING,
708 printf("vmx_init: processor does not support desired "
713 /* Check support for VM-entry controls */
714 error = vmx_set_ctlreg(MSR_VMX_ENTRY_CTLS, MSR_VMX_TRUE_ENTRY_CTLS,
715 VM_ENTRY_CTLS_ONE_SETTING, VM_ENTRY_CTLS_ZERO_SETTING,
718 printf("vmx_init: processor does not support desired "
724 * Check support for optional features by testing them
727 cap_halt_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
728 MSR_VMX_TRUE_PROCBASED_CTLS,
729 PROCBASED_HLT_EXITING, 0,
732 cap_monitor_trap = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
733 MSR_VMX_PROCBASED_CTLS,
737 cap_pause_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
738 MSR_VMX_TRUE_PROCBASED_CTLS,
739 PROCBASED_PAUSE_EXITING, 0,
742 cap_unrestricted_guest = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
743 MSR_VMX_PROCBASED_CTLS2,
744 PROCBASED2_UNRESTRICTED_GUEST, 0,
747 cap_invpcid = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
748 MSR_VMX_PROCBASED_CTLS2, PROCBASED2_ENABLE_INVPCID, 0,
752 * Check support for virtual interrupt delivery.
754 procbased2_vid_bits = (PROCBASED2_VIRTUALIZE_APIC_ACCESSES |
755 PROCBASED2_VIRTUALIZE_X2APIC_MODE |
756 PROCBASED2_APIC_REGISTER_VIRTUALIZATION |
757 PROCBASED2_VIRTUAL_INTERRUPT_DELIVERY);
759 use_tpr_shadow = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
760 MSR_VMX_TRUE_PROCBASED_CTLS, PROCBASED_USE_TPR_SHADOW, 0,
763 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2,
764 procbased2_vid_bits, 0, &tmp);
765 if (error == 0 && use_tpr_shadow) {
766 virtual_interrupt_delivery = 1;
767 TUNABLE_INT_FETCH("hw.vmm.vmx.use_apic_vid",
768 &virtual_interrupt_delivery);
771 if (virtual_interrupt_delivery) {
772 procbased_ctls |= PROCBASED_USE_TPR_SHADOW;
773 procbased_ctls2 |= procbased2_vid_bits;
774 procbased_ctls2 &= ~PROCBASED2_VIRTUALIZE_X2APIC_MODE;
777 * No need to emulate accesses to %CR8 if virtual
778 * interrupt delivery is enabled.
780 procbased_ctls &= ~PROCBASED_CR8_LOAD_EXITING;
781 procbased_ctls &= ~PROCBASED_CR8_STORE_EXITING;
784 * Check for Posted Interrupts only if Virtual Interrupt
785 * Delivery is enabled.
787 error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS,
788 MSR_VMX_TRUE_PINBASED_CTLS, PINBASED_POSTED_INTERRUPT, 0,
791 pirvec = lapic_ipi_alloc(pti ? &IDTVEC(justreturn1_pti) :
792 &IDTVEC(justreturn));
795 printf("vmx_init: unable to allocate "
796 "posted interrupt vector\n");
799 posted_interrupts = 1;
800 TUNABLE_INT_FETCH("hw.vmm.vmx.use_apic_pir",
806 if (posted_interrupts)
807 pinbased_ctls |= PINBASED_POSTED_INTERRUPT;
810 error = ept_init(ipinum);
812 printf("vmx_init: ept initialization failed (%d)\n", error);
816 guest_l1d_flush = (cpu_ia32_arch_caps &
817 IA32_ARCH_CAP_SKIP_L1DFL_VMENTRY) == 0;
818 TUNABLE_INT_FETCH("hw.vmm.l1d_flush", &guest_l1d_flush);
821 * L1D cache flush is enabled. Use IA32_FLUSH_CMD MSR when
822 * available. Otherwise fall back to the software flush
823 * method which loads enough data from the kernel text to
824 * flush existing L1D content, both on VMX entry and on NMI
827 if (guest_l1d_flush) {
828 if ((cpu_stdext_feature3 & CPUID_STDEXT3_L1D_FLUSH) == 0) {
829 guest_l1d_flush_sw = 1;
830 TUNABLE_INT_FETCH("hw.vmm.l1d_flush_sw",
831 &guest_l1d_flush_sw);
833 if (guest_l1d_flush_sw) {
834 if (nmi_flush_l1d_sw <= 1)
835 nmi_flush_l1d_sw = 1;
837 msr_load_list[0].index = MSR_IA32_FLUSH_CMD;
838 msr_load_list[0].val = IA32_FLUSH_CMD_L1D;
843 * Stash the cr0 and cr4 bits that must be fixed to 0 or 1
845 fixed0 = rdmsr(MSR_VMX_CR0_FIXED0);
846 fixed1 = rdmsr(MSR_VMX_CR0_FIXED1);
847 cr0_ones_mask = fixed0 & fixed1;
848 cr0_zeros_mask = ~fixed0 & ~fixed1;
851 * CR0_PE and CR0_PG can be set to zero in VMX non-root operation
852 * if unrestricted guest execution is allowed.
854 if (cap_unrestricted_guest)
855 cr0_ones_mask &= ~(CR0_PG | CR0_PE);
858 * Do not allow the guest to set CR0_NW or CR0_CD.
860 cr0_zeros_mask |= (CR0_NW | CR0_CD);
862 fixed0 = rdmsr(MSR_VMX_CR4_FIXED0);
863 fixed1 = rdmsr(MSR_VMX_CR4_FIXED1);
864 cr4_ones_mask = fixed0 & fixed1;
865 cr4_zeros_mask = ~fixed0 & ~fixed1;
871 /* enable VMX operation */
872 smp_rendezvous(NULL, vmx_enable, NULL, NULL);
880 vmx_trigger_hostintr(int vector)
883 struct gate_descriptor *gd;
887 KASSERT(vector >= 32 && vector <= 255, ("vmx_trigger_hostintr: "
888 "invalid vector %d", vector));
889 KASSERT(gd->gd_p == 1, ("gate descriptor for vector %d not present",
891 KASSERT(gd->gd_type == SDT_SYSIGT, ("gate descriptor for vector %d "
892 "has invalid type %d", vector, gd->gd_type));
893 KASSERT(gd->gd_dpl == SEL_KPL, ("gate descriptor for vector %d "
894 "has invalid dpl %d", vector, gd->gd_dpl));
895 KASSERT(gd->gd_selector == GSEL(GCODE_SEL, SEL_KPL), ("gate descriptor "
896 "for vector %d has invalid selector %d", vector, gd->gd_selector));
897 KASSERT(gd->gd_ist == 0, ("gate descriptor for vector %d has invalid "
898 "IST %d", vector, gd->gd_ist));
900 func = ((long)gd->gd_hioffset << 16 | gd->gd_looffset);
905 vmx_setup_cr_shadow(int which, struct vmcs *vmcs, uint32_t initial)
907 int error, mask_ident, shadow_ident;
910 if (which != 0 && which != 4)
911 panic("vmx_setup_cr_shadow: unknown cr%d", which);
914 mask_ident = VMCS_CR0_MASK;
915 mask_value = cr0_ones_mask | cr0_zeros_mask;
916 shadow_ident = VMCS_CR0_SHADOW;
918 mask_ident = VMCS_CR4_MASK;
919 mask_value = cr4_ones_mask | cr4_zeros_mask;
920 shadow_ident = VMCS_CR4_SHADOW;
923 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(mask_ident), mask_value);
927 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(shadow_ident), initial);
933 #define vmx_setup_cr0_shadow(vmcs,init) vmx_setup_cr_shadow(0, (vmcs), (init))
934 #define vmx_setup_cr4_shadow(vmcs,init) vmx_setup_cr_shadow(4, (vmcs), (init))
937 vmx_vminit(struct vm *vm, pmap_t pmap)
939 uint16_t vpid[VM_MAXCPU];
946 vmx = malloc(sizeof(struct vmx), M_VMX, M_WAITOK | M_ZERO);
947 if ((uintptr_t)vmx & PAGE_MASK) {
948 panic("malloc of struct vmx not aligned on %d byte boundary",
953 vmx->eptp = eptp(vtophys((vm_offset_t)pmap->pm_pml4));
956 * Clean up EPTP-tagged guest physical and combined mappings
958 * VMX transitions are not required to invalidate any guest physical
959 * mappings. So, it may be possible for stale guest physical mappings
960 * to be present in the processor TLBs.
962 * Combined mappings for this EP4TA are also invalidated for all VPIDs.
964 ept_invalidate_mappings(vmx->eptp);
966 msr_bitmap_initialize(vmx->msr_bitmap);
969 * It is safe to allow direct access to MSR_GSBASE and MSR_FSBASE.
970 * The guest FSBASE and GSBASE are saved and restored during
971 * vm-exit and vm-entry respectively. The host FSBASE and GSBASE are
972 * always restored from the vmcs host state area on vm-exit.
974 * The SYSENTER_CS/ESP/EIP MSRs are identical to FS/GSBASE in
975 * how they are saved/restored so can be directly accessed by the
978 * MSR_EFER is saved and restored in the guest VMCS area on a
979 * VM exit and entry respectively. It is also restored from the
980 * host VMCS area on a VM exit.
982 * The TSC MSR is exposed read-only. Writes are disallowed as
983 * that will impact the host TSC. If the guest does a write
984 * the "use TSC offsetting" execution control is enabled and the
985 * difference between the host TSC and the guest TSC is written
986 * into the TSC offset in the VMCS.
988 if (guest_msr_rw(vmx, MSR_GSBASE) ||
989 guest_msr_rw(vmx, MSR_FSBASE) ||
990 guest_msr_rw(vmx, MSR_SYSENTER_CS_MSR) ||
991 guest_msr_rw(vmx, MSR_SYSENTER_ESP_MSR) ||
992 guest_msr_rw(vmx, MSR_SYSENTER_EIP_MSR) ||
993 guest_msr_rw(vmx, MSR_EFER) ||
994 guest_msr_ro(vmx, MSR_TSC))
995 panic("vmx_vminit: error setting guest msr access");
997 vpid_alloc(vpid, VM_MAXCPU);
999 if (virtual_interrupt_delivery) {
1000 error = vm_map_mmio(vm, DEFAULT_APIC_BASE, PAGE_SIZE,
1001 APIC_ACCESS_ADDRESS);
1002 /* XXX this should really return an error to the caller */
1003 KASSERT(error == 0, ("vm_map_mmio(apicbase) error %d", error));
1006 maxcpus = vm_get_maxcpus(vm);
1007 for (i = 0; i < maxcpus; i++) {
1008 vmcs = &vmx->vmcs[i];
1009 vmcs->identifier = vmx_revision();
1010 error = vmclear(vmcs);
1012 panic("vmx_vminit: vmclear error %d on vcpu %d\n",
1016 vmx_msr_guest_init(vmx, i);
1018 error = vmcs_init(vmcs);
1019 KASSERT(error == 0, ("vmcs_init error %d", error));
1023 error += vmwrite(VMCS_HOST_RSP, (u_long)&vmx->ctx[i]);
1024 error += vmwrite(VMCS_EPTP, vmx->eptp);
1025 error += vmwrite(VMCS_PIN_BASED_CTLS, pinbased_ctls);
1026 error += vmwrite(VMCS_PRI_PROC_BASED_CTLS, procbased_ctls);
1027 error += vmwrite(VMCS_SEC_PROC_BASED_CTLS, procbased_ctls2);
1028 error += vmwrite(VMCS_EXIT_CTLS, exit_ctls);
1029 error += vmwrite(VMCS_ENTRY_CTLS, entry_ctls);
1030 error += vmwrite(VMCS_MSR_BITMAP, vtophys(vmx->msr_bitmap));
1031 error += vmwrite(VMCS_VPID, vpid[i]);
1033 if (guest_l1d_flush && !guest_l1d_flush_sw) {
1034 vmcs_write(VMCS_ENTRY_MSR_LOAD, pmap_kextract(
1035 (vm_offset_t)&msr_load_list[0]));
1036 vmcs_write(VMCS_ENTRY_MSR_LOAD_COUNT,
1037 nitems(msr_load_list));
1038 vmcs_write(VMCS_EXIT_MSR_STORE, 0);
1039 vmcs_write(VMCS_EXIT_MSR_STORE_COUNT, 0);
1042 /* exception bitmap */
1043 if (vcpu_trace_exceptions(vm, i))
1044 exc_bitmap = 0xffffffff;
1046 exc_bitmap = 1 << IDT_MC;
1047 error += vmwrite(VMCS_EXCEPTION_BITMAP, exc_bitmap);
1049 vmx->ctx[i].guest_dr6 = DBREG_DR6_RESERVED1;
1050 error += vmwrite(VMCS_GUEST_DR7, DBREG_DR7_RESERVED1);
1052 if (virtual_interrupt_delivery) {
1053 error += vmwrite(VMCS_APIC_ACCESS, APIC_ACCESS_ADDRESS);
1054 error += vmwrite(VMCS_VIRTUAL_APIC,
1055 vtophys(&vmx->apic_page[i]));
1056 error += vmwrite(VMCS_EOI_EXIT0, 0);
1057 error += vmwrite(VMCS_EOI_EXIT1, 0);
1058 error += vmwrite(VMCS_EOI_EXIT2, 0);
1059 error += vmwrite(VMCS_EOI_EXIT3, 0);
1061 if (posted_interrupts) {
1062 error += vmwrite(VMCS_PIR_VECTOR, pirvec);
1063 error += vmwrite(VMCS_PIR_DESC,
1064 vtophys(&vmx->pir_desc[i]));
1067 KASSERT(error == 0, ("vmx_vminit: error customizing the vmcs"));
1069 vmx->cap[i].set = 0;
1070 vmx->cap[i].proc_ctls = procbased_ctls;
1071 vmx->cap[i].proc_ctls2 = procbased_ctls2;
1073 vmx->state[i].nextrip = ~0;
1074 vmx->state[i].lastcpu = NOCPU;
1075 vmx->state[i].vpid = vpid[i];
1078 * Set up the CR0/4 shadows, and init the read shadow
1079 * to the power-on register value from the Intel Sys Arch.
1083 error = vmx_setup_cr0_shadow(vmcs, 0x60000010);
1085 panic("vmx_setup_cr0_shadow %d", error);
1087 error = vmx_setup_cr4_shadow(vmcs, 0);
1089 panic("vmx_setup_cr4_shadow %d", error);
1091 vmx->ctx[i].pmap = pmap;
1098 vmx_handle_cpuid(struct vm *vm, int vcpu, struct vmxctx *vmxctx)
1102 func = vmxctx->guest_rax;
1104 handled = x86_emulate_cpuid(vm, vcpu,
1105 (uint32_t*)(&vmxctx->guest_rax),
1106 (uint32_t*)(&vmxctx->guest_rbx),
1107 (uint32_t*)(&vmxctx->guest_rcx),
1108 (uint32_t*)(&vmxctx->guest_rdx));
1112 static __inline void
1113 vmx_run_trace(struct vmx *vmx, int vcpu)
1116 VCPU_CTR1(vmx->vm, vcpu, "Resume execution at %#lx", vmcs_guest_rip());
1120 static __inline void
1121 vmx_exit_trace(struct vmx *vmx, int vcpu, uint64_t rip, uint32_t exit_reason,
1125 VCPU_CTR3(vmx->vm, vcpu, "%s %s vmexit at 0x%0lx",
1126 handled ? "handled" : "unhandled",
1127 exit_reason_to_str(exit_reason), rip);
1131 static __inline void
1132 vmx_astpending_trace(struct vmx *vmx, int vcpu, uint64_t rip)
1135 VCPU_CTR1(vmx->vm, vcpu, "astpending vmexit at 0x%0lx", rip);
1139 static VMM_STAT_INTEL(VCPU_INVVPID_SAVED, "Number of vpid invalidations saved");
1140 static VMM_STAT_INTEL(VCPU_INVVPID_DONE, "Number of vpid invalidations done");
1143 * Invalidate guest mappings identified by its vpid from the TLB.
1145 static __inline void
1146 vmx_invvpid(struct vmx *vmx, int vcpu, pmap_t pmap, int running)
1148 struct vmxstate *vmxstate;
1149 struct invvpid_desc invvpid_desc;
1151 vmxstate = &vmx->state[vcpu];
1152 if (vmxstate->vpid == 0)
1157 * Set the 'lastcpu' to an invalid host cpu.
1159 * This will invalidate TLB entries tagged with the vcpu's
1160 * vpid the next time it runs via vmx_set_pcpu_defaults().
1162 vmxstate->lastcpu = NOCPU;
1166 KASSERT(curthread->td_critnest > 0, ("%s: vcpu %d running outside "
1167 "critical section", __func__, vcpu));
1170 * Invalidate all mappings tagged with 'vpid'
1172 * We do this because this vcpu was executing on a different host
1173 * cpu when it last ran. We do not track whether it invalidated
1174 * mappings associated with its 'vpid' during that run. So we must
1175 * assume that the mappings associated with 'vpid' on 'curcpu' are
1176 * stale and invalidate them.
1178 * Note that we incur this penalty only when the scheduler chooses to
1179 * move the thread associated with this vcpu between host cpus.
1181 * Note also that this will invalidate mappings tagged with 'vpid'
1184 if (pmap->pm_eptgen == vmx->eptgen[curcpu]) {
1185 invvpid_desc._res1 = 0;
1186 invvpid_desc._res2 = 0;
1187 invvpid_desc.vpid = vmxstate->vpid;
1188 invvpid_desc.linear_addr = 0;
1189 invvpid(INVVPID_TYPE_SINGLE_CONTEXT, invvpid_desc);
1190 vmm_stat_incr(vmx->vm, vcpu, VCPU_INVVPID_DONE, 1);
1193 * The invvpid can be skipped if an invept is going to
1194 * be performed before entering the guest. The invept
1195 * will invalidate combined mappings tagged with
1196 * 'vmx->eptp' for all vpids.
1198 vmm_stat_incr(vmx->vm, vcpu, VCPU_INVVPID_SAVED, 1);
1203 vmx_set_pcpu_defaults(struct vmx *vmx, int vcpu, pmap_t pmap)
1205 struct vmxstate *vmxstate;
1207 vmxstate = &vmx->state[vcpu];
1208 if (vmxstate->lastcpu == curcpu)
1211 vmxstate->lastcpu = curcpu;
1213 vmm_stat_incr(vmx->vm, vcpu, VCPU_MIGRATIONS, 1);
1215 vmcs_write(VMCS_HOST_TR_BASE, vmm_get_host_trbase());
1216 vmcs_write(VMCS_HOST_GDTR_BASE, vmm_get_host_gdtrbase());
1217 vmcs_write(VMCS_HOST_GS_BASE, vmm_get_host_gsbase());
1218 vmx_invvpid(vmx, vcpu, pmap, 1);
1222 * We depend on 'procbased_ctls' to have the Interrupt Window Exiting bit set.
1224 CTASSERT((PROCBASED_CTLS_ONE_SETTING & PROCBASED_INT_WINDOW_EXITING) != 0);
1226 static void __inline
1227 vmx_set_int_window_exiting(struct vmx *vmx, int vcpu)
1230 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) == 0) {
1231 vmx->cap[vcpu].proc_ctls |= PROCBASED_INT_WINDOW_EXITING;
1232 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1233 VCPU_CTR0(vmx->vm, vcpu, "Enabling interrupt window exiting");
1237 static void __inline
1238 vmx_clear_int_window_exiting(struct vmx *vmx, int vcpu)
1241 KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0,
1242 ("intr_window_exiting not set: %#x", vmx->cap[vcpu].proc_ctls));
1243 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_INT_WINDOW_EXITING;
1244 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1245 VCPU_CTR0(vmx->vm, vcpu, "Disabling interrupt window exiting");
1248 static void __inline
1249 vmx_set_nmi_window_exiting(struct vmx *vmx, int vcpu)
1252 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) == 0) {
1253 vmx->cap[vcpu].proc_ctls |= PROCBASED_NMI_WINDOW_EXITING;
1254 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1255 VCPU_CTR0(vmx->vm, vcpu, "Enabling NMI window exiting");
1259 static void __inline
1260 vmx_clear_nmi_window_exiting(struct vmx *vmx, int vcpu)
1263 KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) != 0,
1264 ("nmi_window_exiting not set %#x", vmx->cap[vcpu].proc_ctls));
1265 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_NMI_WINDOW_EXITING;
1266 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1267 VCPU_CTR0(vmx->vm, vcpu, "Disabling NMI window exiting");
1271 vmx_set_tsc_offset(struct vmx *vmx, int vcpu, uint64_t offset)
1275 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_TSC_OFFSET) == 0) {
1276 vmx->cap[vcpu].proc_ctls |= PROCBASED_TSC_OFFSET;
1277 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1278 VCPU_CTR0(vmx->vm, vcpu, "Enabling TSC offsetting");
1281 error = vmwrite(VMCS_TSC_OFFSET, offset);
1286 #define NMI_BLOCKING (VMCS_INTERRUPTIBILITY_NMI_BLOCKING | \
1287 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)
1288 #define HWINTR_BLOCKING (VMCS_INTERRUPTIBILITY_STI_BLOCKING | \
1289 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)
1292 vmx_inject_nmi(struct vmx *vmx, int vcpu)
1296 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1297 KASSERT((gi & NMI_BLOCKING) == 0, ("vmx_inject_nmi: invalid guest "
1298 "interruptibility-state %#x", gi));
1300 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1301 KASSERT((info & VMCS_INTR_VALID) == 0, ("vmx_inject_nmi: invalid "
1302 "VM-entry interruption information %#x", info));
1305 * Inject the virtual NMI. The vector must be the NMI IDT entry
1306 * or the VMCS entry check will fail.
1308 info = IDT_NMI | VMCS_INTR_T_NMI | VMCS_INTR_VALID;
1309 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1311 VCPU_CTR0(vmx->vm, vcpu, "Injecting vNMI");
1313 /* Clear the request */
1314 vm_nmi_clear(vmx->vm, vcpu);
1318 vmx_inject_interrupts(struct vmx *vmx, int vcpu, struct vlapic *vlapic,
1321 int vector, need_nmi_exiting, extint_pending;
1322 uint64_t rflags, entryinfo;
1325 if (vmx->state[vcpu].nextrip != guestrip) {
1326 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1327 if (gi & HWINTR_BLOCKING) {
1328 VCPU_CTR2(vmx->vm, vcpu, "Guest interrupt blocking "
1329 "cleared due to rip change: %#lx/%#lx",
1330 vmx->state[vcpu].nextrip, guestrip);
1331 gi &= ~HWINTR_BLOCKING;
1332 vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1336 if (vm_entry_intinfo(vmx->vm, vcpu, &entryinfo)) {
1337 KASSERT((entryinfo & VMCS_INTR_VALID) != 0, ("%s: entry "
1338 "intinfo is not valid: %#lx", __func__, entryinfo));
1340 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1341 KASSERT((info & VMCS_INTR_VALID) == 0, ("%s: cannot inject "
1342 "pending exception: %#lx/%#x", __func__, entryinfo, info));
1345 vector = info & 0xff;
1346 if (vector == IDT_BP || vector == IDT_OF) {
1348 * VT-x requires #BP and #OF to be injected as software
1351 info &= ~VMCS_INTR_T_MASK;
1352 info |= VMCS_INTR_T_SWEXCEPTION;
1355 if (info & VMCS_INTR_DEL_ERRCODE)
1356 vmcs_write(VMCS_ENTRY_EXCEPTION_ERROR, entryinfo >> 32);
1358 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1361 if (vm_nmi_pending(vmx->vm, vcpu)) {
1363 * If there are no conditions blocking NMI injection then
1364 * inject it directly here otherwise enable "NMI window
1365 * exiting" to inject it as soon as we can.
1367 * We also check for STI_BLOCKING because some implementations
1368 * don't allow NMI injection in this case. If we are running
1369 * on a processor that doesn't have this restriction it will
1370 * immediately exit and the NMI will be injected in the
1371 * "NMI window exiting" handler.
1373 need_nmi_exiting = 1;
1374 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1375 if ((gi & (HWINTR_BLOCKING | NMI_BLOCKING)) == 0) {
1376 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1377 if ((info & VMCS_INTR_VALID) == 0) {
1378 vmx_inject_nmi(vmx, vcpu);
1379 need_nmi_exiting = 0;
1381 VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI "
1382 "due to VM-entry intr info %#x", info);
1385 VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI due to "
1386 "Guest Interruptibility-state %#x", gi);
1389 if (need_nmi_exiting)
1390 vmx_set_nmi_window_exiting(vmx, vcpu);
1393 extint_pending = vm_extint_pending(vmx->vm, vcpu);
1395 if (!extint_pending && virtual_interrupt_delivery) {
1396 vmx_inject_pir(vlapic);
1401 * If interrupt-window exiting is already in effect then don't bother
1402 * checking for pending interrupts. This is just an optimization and
1403 * not needed for correctness.
1405 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0) {
1406 VCPU_CTR0(vmx->vm, vcpu, "Skip interrupt injection due to "
1407 "pending int_window_exiting");
1411 if (!extint_pending) {
1412 /* Ask the local apic for a vector to inject */
1413 if (!vlapic_pending_intr(vlapic, &vector))
1417 * From the Intel SDM, Volume 3, Section "Maskable
1418 * Hardware Interrupts":
1419 * - maskable interrupt vectors [16,255] can be delivered
1420 * through the local APIC.
1422 KASSERT(vector >= 16 && vector <= 255,
1423 ("invalid vector %d from local APIC", vector));
1425 /* Ask the legacy pic for a vector to inject */
1426 vatpic_pending_intr(vmx->vm, &vector);
1429 * From the Intel SDM, Volume 3, Section "Maskable
1430 * Hardware Interrupts":
1431 * - maskable interrupt vectors [0,255] can be delivered
1432 * through the INTR pin.
1434 KASSERT(vector >= 0 && vector <= 255,
1435 ("invalid vector %d from INTR", vector));
1438 /* Check RFLAGS.IF and the interruptibility state of the guest */
1439 rflags = vmcs_read(VMCS_GUEST_RFLAGS);
1440 if ((rflags & PSL_I) == 0) {
1441 VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1442 "rflags %#lx", vector, rflags);
1446 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1447 if (gi & HWINTR_BLOCKING) {
1448 VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1449 "Guest Interruptibility-state %#x", vector, gi);
1453 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1454 if (info & VMCS_INTR_VALID) {
1456 * This is expected and could happen for multiple reasons:
1457 * - A vectoring VM-entry was aborted due to astpending
1458 * - A VM-exit happened during event injection.
1459 * - An exception was injected above.
1460 * - An NMI was injected above or after "NMI window exiting"
1462 VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1463 "VM-entry intr info %#x", vector, info);
1467 /* Inject the interrupt */
1468 info = VMCS_INTR_T_HWINTR | VMCS_INTR_VALID;
1470 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1472 if (!extint_pending) {
1473 /* Update the Local APIC ISR */
1474 vlapic_intr_accepted(vlapic, vector);
1476 vm_extint_clear(vmx->vm, vcpu);
1477 vatpic_intr_accepted(vmx->vm, vector);
1480 * After we accepted the current ExtINT the PIC may
1481 * have posted another one. If that is the case, set
1482 * the Interrupt Window Exiting execution control so
1483 * we can inject that one too.
1485 * Also, interrupt window exiting allows us to inject any
1486 * pending APIC vector that was preempted by the ExtINT
1487 * as soon as possible. This applies both for the software
1488 * emulated vlapic and the hardware assisted virtual APIC.
1490 vmx_set_int_window_exiting(vmx, vcpu);
1493 VCPU_CTR1(vmx->vm, vcpu, "Injecting hwintr at vector %d", vector);
1499 * Set the Interrupt Window Exiting execution control so we can inject
1500 * the interrupt as soon as blocking condition goes away.
1502 vmx_set_int_window_exiting(vmx, vcpu);
1506 * If the Virtual NMIs execution control is '1' then the logical processor
1507 * tracks virtual-NMI blocking in the Guest Interruptibility-state field of
1508 * the VMCS. An IRET instruction in VMX non-root operation will remove any
1509 * virtual-NMI blocking.
1511 * This unblocking occurs even if the IRET causes a fault. In this case the
1512 * hypervisor needs to restore virtual-NMI blocking before resuming the guest.
1515 vmx_restore_nmi_blocking(struct vmx *vmx, int vcpuid)
1519 VCPU_CTR0(vmx->vm, vcpuid, "Restore Virtual-NMI blocking");
1520 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1521 gi |= VMCS_INTERRUPTIBILITY_NMI_BLOCKING;
1522 vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1526 vmx_clear_nmi_blocking(struct vmx *vmx, int vcpuid)
1530 VCPU_CTR0(vmx->vm, vcpuid, "Clear Virtual-NMI blocking");
1531 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1532 gi &= ~VMCS_INTERRUPTIBILITY_NMI_BLOCKING;
1533 vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1537 vmx_assert_nmi_blocking(struct vmx *vmx, int vcpuid)
1541 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1542 KASSERT(gi & VMCS_INTERRUPTIBILITY_NMI_BLOCKING,
1543 ("NMI blocking is not in effect %#x", gi));
1547 vmx_emulate_xsetbv(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
1549 struct vmxctx *vmxctx;
1551 const struct xsave_limits *limits;
1553 vmxctx = &vmx->ctx[vcpu];
1554 limits = vmm_get_xsave_limits();
1557 * Note that the processor raises a GP# fault on its own if
1558 * xsetbv is executed for CPL != 0, so we do not have to
1559 * emulate that fault here.
1562 /* Only xcr0 is supported. */
1563 if (vmxctx->guest_rcx != 0) {
1564 vm_inject_gp(vmx->vm, vcpu);
1568 /* We only handle xcr0 if both the host and guest have XSAVE enabled. */
1569 if (!limits->xsave_enabled || !(vmcs_read(VMCS_GUEST_CR4) & CR4_XSAVE)) {
1570 vm_inject_ud(vmx->vm, vcpu);
1574 xcrval = vmxctx->guest_rdx << 32 | (vmxctx->guest_rax & 0xffffffff);
1575 if ((xcrval & ~limits->xcr0_allowed) != 0) {
1576 vm_inject_gp(vmx->vm, vcpu);
1580 if (!(xcrval & XFEATURE_ENABLED_X87)) {
1581 vm_inject_gp(vmx->vm, vcpu);
1585 /* AVX (YMM_Hi128) requires SSE. */
1586 if (xcrval & XFEATURE_ENABLED_AVX &&
1587 (xcrval & XFEATURE_AVX) != XFEATURE_AVX) {
1588 vm_inject_gp(vmx->vm, vcpu);
1593 * AVX512 requires base AVX (YMM_Hi128) as well as OpMask,
1594 * ZMM_Hi256, and Hi16_ZMM.
1596 if (xcrval & XFEATURE_AVX512 &&
1597 (xcrval & (XFEATURE_AVX512 | XFEATURE_AVX)) !=
1598 (XFEATURE_AVX512 | XFEATURE_AVX)) {
1599 vm_inject_gp(vmx->vm, vcpu);
1604 * Intel MPX requires both bound register state flags to be
1607 if (((xcrval & XFEATURE_ENABLED_BNDREGS) != 0) !=
1608 ((xcrval & XFEATURE_ENABLED_BNDCSR) != 0)) {
1609 vm_inject_gp(vmx->vm, vcpu);
1614 * This runs "inside" vmrun() with the guest's FPU state, so
1615 * modifying xcr0 directly modifies the guest's xcr0, not the
1618 load_xcr(0, xcrval);
1623 vmx_get_guest_reg(struct vmx *vmx, int vcpu, int ident)
1625 const struct vmxctx *vmxctx;
1627 vmxctx = &vmx->ctx[vcpu];
1631 return (vmxctx->guest_rax);
1633 return (vmxctx->guest_rcx);
1635 return (vmxctx->guest_rdx);
1637 return (vmxctx->guest_rbx);
1639 return (vmcs_read(VMCS_GUEST_RSP));
1641 return (vmxctx->guest_rbp);
1643 return (vmxctx->guest_rsi);
1645 return (vmxctx->guest_rdi);
1647 return (vmxctx->guest_r8);
1649 return (vmxctx->guest_r9);
1651 return (vmxctx->guest_r10);
1653 return (vmxctx->guest_r11);
1655 return (vmxctx->guest_r12);
1657 return (vmxctx->guest_r13);
1659 return (vmxctx->guest_r14);
1661 return (vmxctx->guest_r15);
1663 panic("invalid vmx register %d", ident);
1668 vmx_set_guest_reg(struct vmx *vmx, int vcpu, int ident, uint64_t regval)
1670 struct vmxctx *vmxctx;
1672 vmxctx = &vmx->ctx[vcpu];
1676 vmxctx->guest_rax = regval;
1679 vmxctx->guest_rcx = regval;
1682 vmxctx->guest_rdx = regval;
1685 vmxctx->guest_rbx = regval;
1688 vmcs_write(VMCS_GUEST_RSP, regval);
1691 vmxctx->guest_rbp = regval;
1694 vmxctx->guest_rsi = regval;
1697 vmxctx->guest_rdi = regval;
1700 vmxctx->guest_r8 = regval;
1703 vmxctx->guest_r9 = regval;
1706 vmxctx->guest_r10 = regval;
1709 vmxctx->guest_r11 = regval;
1712 vmxctx->guest_r12 = regval;
1715 vmxctx->guest_r13 = regval;
1718 vmxctx->guest_r14 = regval;
1721 vmxctx->guest_r15 = regval;
1724 panic("invalid vmx register %d", ident);
1729 vmx_emulate_cr0_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1731 uint64_t crval, regval;
1733 /* We only handle mov to %cr0 at this time */
1734 if ((exitqual & 0xf0) != 0x00)
1737 regval = vmx_get_guest_reg(vmx, vcpu, (exitqual >> 8) & 0xf);
1739 vmcs_write(VMCS_CR0_SHADOW, regval);
1741 crval = regval | cr0_ones_mask;
1742 crval &= ~cr0_zeros_mask;
1743 vmcs_write(VMCS_GUEST_CR0, crval);
1745 if (regval & CR0_PG) {
1746 uint64_t efer, entry_ctls;
1749 * If CR0.PG is 1 and EFER.LME is 1 then EFER.LMA and
1750 * the "IA-32e mode guest" bit in VM-entry control must be
1753 efer = vmcs_read(VMCS_GUEST_IA32_EFER);
1754 if (efer & EFER_LME) {
1756 vmcs_write(VMCS_GUEST_IA32_EFER, efer);
1757 entry_ctls = vmcs_read(VMCS_ENTRY_CTLS);
1758 entry_ctls |= VM_ENTRY_GUEST_LMA;
1759 vmcs_write(VMCS_ENTRY_CTLS, entry_ctls);
1767 vmx_emulate_cr4_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1769 uint64_t crval, regval;
1771 /* We only handle mov to %cr4 at this time */
1772 if ((exitqual & 0xf0) != 0x00)
1775 regval = vmx_get_guest_reg(vmx, vcpu, (exitqual >> 8) & 0xf);
1777 vmcs_write(VMCS_CR4_SHADOW, regval);
1779 crval = regval | cr4_ones_mask;
1780 crval &= ~cr4_zeros_mask;
1781 vmcs_write(VMCS_GUEST_CR4, crval);
1787 vmx_emulate_cr8_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1789 struct vlapic *vlapic;
1793 /* We only handle mov %cr8 to/from a register at this time. */
1794 if ((exitqual & 0xe0) != 0x00) {
1798 vlapic = vm_lapic(vmx->vm, vcpu);
1799 regnum = (exitqual >> 8) & 0xf;
1800 if (exitqual & 0x10) {
1801 cr8 = vlapic_get_cr8(vlapic);
1802 vmx_set_guest_reg(vmx, vcpu, regnum, cr8);
1804 cr8 = vmx_get_guest_reg(vmx, vcpu, regnum);
1805 vlapic_set_cr8(vlapic, cr8);
1812 * From section "Guest Register State" in the Intel SDM: CPL = SS.DPL
1819 ssar = vmcs_read(VMCS_GUEST_SS_ACCESS_RIGHTS);
1820 return ((ssar >> 5) & 0x3);
1823 static enum vm_cpu_mode
1828 if (vmcs_read(VMCS_GUEST_IA32_EFER) & EFER_LMA) {
1829 csar = vmcs_read(VMCS_GUEST_CS_ACCESS_RIGHTS);
1831 return (CPU_MODE_64BIT); /* CS.L = 1 */
1833 return (CPU_MODE_COMPATIBILITY);
1834 } else if (vmcs_read(VMCS_GUEST_CR0) & CR0_PE) {
1835 return (CPU_MODE_PROTECTED);
1837 return (CPU_MODE_REAL);
1841 static enum vm_paging_mode
1842 vmx_paging_mode(void)
1845 if (!(vmcs_read(VMCS_GUEST_CR0) & CR0_PG))
1846 return (PAGING_MODE_FLAT);
1847 if (!(vmcs_read(VMCS_GUEST_CR4) & CR4_PAE))
1848 return (PAGING_MODE_32);
1849 if (vmcs_read(VMCS_GUEST_IA32_EFER) & EFER_LME)
1850 return (PAGING_MODE_64);
1852 return (PAGING_MODE_PAE);
1856 inout_str_index(struct vmx *vmx, int vcpuid, int in)
1860 enum vm_reg_name reg;
1862 reg = in ? VM_REG_GUEST_RDI : VM_REG_GUEST_RSI;
1863 error = vmx_getreg(vmx, vcpuid, reg, &val);
1864 KASSERT(error == 0, ("%s: vmx_getreg error %d", __func__, error));
1869 inout_str_count(struct vmx *vmx, int vcpuid, int rep)
1875 error = vmx_getreg(vmx, vcpuid, VM_REG_GUEST_RCX, &val);
1876 KASSERT(!error, ("%s: vmx_getreg error %d", __func__, error));
1884 inout_str_addrsize(uint32_t inst_info)
1888 size = (inst_info >> 7) & 0x7;
1891 return (2); /* 16 bit */
1893 return (4); /* 32 bit */
1895 return (8); /* 64 bit */
1897 panic("%s: invalid size encoding %d", __func__, size);
1902 inout_str_seginfo(struct vmx *vmx, int vcpuid, uint32_t inst_info, int in,
1903 struct vm_inout_str *vis)
1908 vis->seg_name = VM_REG_GUEST_ES;
1910 s = (inst_info >> 15) & 0x7;
1911 vis->seg_name = vm_segment_name(s);
1914 error = vmx_getdesc(vmx, vcpuid, vis->seg_name, &vis->seg_desc);
1915 KASSERT(error == 0, ("%s: vmx_getdesc error %d", __func__, error));
1919 vmx_paging_info(struct vm_guest_paging *paging)
1921 paging->cr3 = vmcs_guest_cr3();
1922 paging->cpl = vmx_cpl();
1923 paging->cpu_mode = vmx_cpu_mode();
1924 paging->paging_mode = vmx_paging_mode();
1928 vmexit_inst_emul(struct vm_exit *vmexit, uint64_t gpa, uint64_t gla)
1930 struct vm_guest_paging *paging;
1933 paging = &vmexit->u.inst_emul.paging;
1935 vmexit->exitcode = VM_EXITCODE_INST_EMUL;
1936 vmexit->inst_length = 0;
1937 vmexit->u.inst_emul.gpa = gpa;
1938 vmexit->u.inst_emul.gla = gla;
1939 vmx_paging_info(paging);
1940 switch (paging->cpu_mode) {
1942 vmexit->u.inst_emul.cs_base = vmcs_read(VMCS_GUEST_CS_BASE);
1943 vmexit->u.inst_emul.cs_d = 0;
1945 case CPU_MODE_PROTECTED:
1946 case CPU_MODE_COMPATIBILITY:
1947 vmexit->u.inst_emul.cs_base = vmcs_read(VMCS_GUEST_CS_BASE);
1948 csar = vmcs_read(VMCS_GUEST_CS_ACCESS_RIGHTS);
1949 vmexit->u.inst_emul.cs_d = SEG_DESC_DEF32(csar);
1952 vmexit->u.inst_emul.cs_base = 0;
1953 vmexit->u.inst_emul.cs_d = 0;
1956 vie_init(&vmexit->u.inst_emul.vie, NULL, 0);
1960 ept_fault_type(uint64_t ept_qual)
1964 if (ept_qual & EPT_VIOLATION_DATA_WRITE)
1965 fault_type = VM_PROT_WRITE;
1966 else if (ept_qual & EPT_VIOLATION_INST_FETCH)
1967 fault_type = VM_PROT_EXECUTE;
1969 fault_type= VM_PROT_READ;
1971 return (fault_type);
1975 ept_emulation_fault(uint64_t ept_qual)
1979 /* EPT fault on an instruction fetch doesn't make sense here */
1980 if (ept_qual & EPT_VIOLATION_INST_FETCH)
1983 /* EPT fault must be a read fault or a write fault */
1984 read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0;
1985 write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0;
1986 if ((read | write) == 0)
1990 * The EPT violation must have been caused by accessing a
1991 * guest-physical address that is a translation of a guest-linear
1994 if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 ||
1995 (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) {
2003 apic_access_virtualization(struct vmx *vmx, int vcpuid)
2005 uint32_t proc_ctls2;
2007 proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
2008 return ((proc_ctls2 & PROCBASED2_VIRTUALIZE_APIC_ACCESSES) ? 1 : 0);
2012 x2apic_virtualization(struct vmx *vmx, int vcpuid)
2014 uint32_t proc_ctls2;
2016 proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
2017 return ((proc_ctls2 & PROCBASED2_VIRTUALIZE_X2APIC_MODE) ? 1 : 0);
2021 vmx_handle_apic_write(struct vmx *vmx, int vcpuid, struct vlapic *vlapic,
2024 int error, handled, offset;
2025 uint32_t *apic_regs, vector;
2029 offset = APIC_WRITE_OFFSET(qual);
2031 if (!apic_access_virtualization(vmx, vcpuid)) {
2033 * In general there should not be any APIC write VM-exits
2034 * unless APIC-access virtualization is enabled.
2036 * However self-IPI virtualization can legitimately trigger
2037 * an APIC-write VM-exit so treat it specially.
2039 if (x2apic_virtualization(vmx, vcpuid) &&
2040 offset == APIC_OFFSET_SELF_IPI) {
2041 apic_regs = (uint32_t *)(vlapic->apic_page);
2042 vector = apic_regs[APIC_OFFSET_SELF_IPI / 4];
2043 vlapic_self_ipi_handler(vlapic, vector);
2050 case APIC_OFFSET_ID:
2051 vlapic_id_write_handler(vlapic);
2053 case APIC_OFFSET_LDR:
2054 vlapic_ldr_write_handler(vlapic);
2056 case APIC_OFFSET_DFR:
2057 vlapic_dfr_write_handler(vlapic);
2059 case APIC_OFFSET_SVR:
2060 vlapic_svr_write_handler(vlapic);
2062 case APIC_OFFSET_ESR:
2063 vlapic_esr_write_handler(vlapic);
2065 case APIC_OFFSET_ICR_LOW:
2067 error = vlapic_icrlo_write_handler(vlapic, &retu);
2068 if (error != 0 || retu)
2069 handled = UNHANDLED;
2071 case APIC_OFFSET_CMCI_LVT:
2072 case APIC_OFFSET_TIMER_LVT ... APIC_OFFSET_ERROR_LVT:
2073 vlapic_lvt_write_handler(vlapic, offset);
2075 case APIC_OFFSET_TIMER_ICR:
2076 vlapic_icrtmr_write_handler(vlapic);
2078 case APIC_OFFSET_TIMER_DCR:
2079 vlapic_dcr_write_handler(vlapic);
2082 handled = UNHANDLED;
2089 apic_access_fault(struct vmx *vmx, int vcpuid, uint64_t gpa)
2092 if (apic_access_virtualization(vmx, vcpuid) &&
2093 (gpa >= DEFAULT_APIC_BASE && gpa < DEFAULT_APIC_BASE + PAGE_SIZE))
2100 vmx_handle_apic_access(struct vmx *vmx, int vcpuid, struct vm_exit *vmexit)
2103 int access_type, offset, allowed;
2105 if (!apic_access_virtualization(vmx, vcpuid))
2108 qual = vmexit->u.vmx.exit_qualification;
2109 access_type = APIC_ACCESS_TYPE(qual);
2110 offset = APIC_ACCESS_OFFSET(qual);
2113 if (access_type == 0) {
2115 * Read data access to the following registers is expected.
2118 case APIC_OFFSET_APR:
2119 case APIC_OFFSET_PPR:
2120 case APIC_OFFSET_RRR:
2121 case APIC_OFFSET_CMCI_LVT:
2122 case APIC_OFFSET_TIMER_CCR:
2128 } else if (access_type == 1) {
2130 * Write data access to the following registers is expected.
2133 case APIC_OFFSET_VER:
2134 case APIC_OFFSET_APR:
2135 case APIC_OFFSET_PPR:
2136 case APIC_OFFSET_RRR:
2137 case APIC_OFFSET_ISR0 ... APIC_OFFSET_ISR7:
2138 case APIC_OFFSET_TMR0 ... APIC_OFFSET_TMR7:
2139 case APIC_OFFSET_IRR0 ... APIC_OFFSET_IRR7:
2140 case APIC_OFFSET_CMCI_LVT:
2141 case APIC_OFFSET_TIMER_CCR:
2150 vmexit_inst_emul(vmexit, DEFAULT_APIC_BASE + offset,
2155 * Regardless of whether the APIC-access is allowed this handler
2156 * always returns UNHANDLED:
2157 * - if the access is allowed then it is handled by emulating the
2158 * instruction that caused the VM-exit (outside the critical section)
2159 * - if the access is not allowed then it will be converted to an
2160 * exitcode of VM_EXITCODE_VMX and will be dealt with in userland.
2165 static enum task_switch_reason
2166 vmx_task_switch_reason(uint64_t qual)
2170 reason = (qual >> 30) & 0x3;
2179 return (TSR_IDT_GATE);
2181 panic("%s: invalid reason %d", __func__, reason);
2186 emulate_wrmsr(struct vmx *vmx, int vcpuid, u_int num, uint64_t val, bool *retu)
2191 error = lapic_wrmsr(vmx->vm, vcpuid, num, val, retu);
2193 error = vmx_wrmsr(vmx, vcpuid, num, val, retu);
2199 emulate_rdmsr(struct vmx *vmx, int vcpuid, u_int num, bool *retu)
2201 struct vmxctx *vmxctx;
2207 error = lapic_rdmsr(vmx->vm, vcpuid, num, &result, retu);
2209 error = vmx_rdmsr(vmx, vcpuid, num, &result, retu);
2213 vmxctx = &vmx->ctx[vcpuid];
2214 error = vmxctx_setreg(vmxctx, VM_REG_GUEST_RAX, eax);
2215 KASSERT(error == 0, ("vmxctx_setreg(rax) error %d", error));
2218 error = vmxctx_setreg(vmxctx, VM_REG_GUEST_RDX, edx);
2219 KASSERT(error == 0, ("vmxctx_setreg(rdx) error %d", error));
2226 vmx_exit_process(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
2228 int error, errcode, errcode_valid, handled, in;
2229 struct vmxctx *vmxctx;
2230 struct vlapic *vlapic;
2231 struct vm_inout_str *vis;
2232 struct vm_task_switch *ts;
2233 uint32_t eax, ecx, edx, idtvec_info, idtvec_err, intr_info, inst_info;
2234 uint32_t intr_type, intr_vec, reason;
2235 uint64_t exitintinfo, qual, gpa;
2238 CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_VIRTUAL_NMI) != 0);
2239 CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_NMI_EXITING) != 0);
2241 handled = UNHANDLED;
2242 vmxctx = &vmx->ctx[vcpu];
2244 qual = vmexit->u.vmx.exit_qualification;
2245 reason = vmexit->u.vmx.exit_reason;
2246 vmexit->exitcode = VM_EXITCODE_BOGUS;
2248 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_COUNT, 1);
2249 SDT_PROBE3(vmm, vmx, exit, entry, vmx, vcpu, vmexit);
2252 * VM-entry failures during or after loading guest state.
2254 * These VM-exits are uncommon but must be handled specially
2255 * as most VM-exit fields are not populated as usual.
2257 if (__predict_false(reason == EXIT_REASON_MCE_DURING_ENTRY)) {
2258 VCPU_CTR0(vmx->vm, vcpu, "Handling MCE during VM-entry");
2259 __asm __volatile("int $18");
2264 * VM exits that can be triggered during event delivery need to
2265 * be handled specially by re-injecting the event if the IDT
2266 * vectoring information field's valid bit is set.
2268 * See "Information for VM Exits During Event Delivery" in Intel SDM
2271 idtvec_info = vmcs_idt_vectoring_info();
2272 if (idtvec_info & VMCS_IDT_VEC_VALID) {
2273 idtvec_info &= ~(1 << 12); /* clear undefined bit */
2274 exitintinfo = idtvec_info;
2275 if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) {
2276 idtvec_err = vmcs_idt_vectoring_err();
2277 exitintinfo |= (uint64_t)idtvec_err << 32;
2279 error = vm_exit_intinfo(vmx->vm, vcpu, exitintinfo);
2280 KASSERT(error == 0, ("%s: vm_set_intinfo error %d",
2284 * If 'virtual NMIs' are being used and the VM-exit
2285 * happened while injecting an NMI during the previous
2286 * VM-entry, then clear "blocking by NMI" in the
2287 * Guest Interruptibility-State so the NMI can be
2288 * reinjected on the subsequent VM-entry.
2290 * However, if the NMI was being delivered through a task
2291 * gate, then the new task must start execution with NMIs
2292 * blocked so don't clear NMI blocking in this case.
2294 intr_type = idtvec_info & VMCS_INTR_T_MASK;
2295 if (intr_type == VMCS_INTR_T_NMI) {
2296 if (reason != EXIT_REASON_TASK_SWITCH)
2297 vmx_clear_nmi_blocking(vmx, vcpu);
2299 vmx_assert_nmi_blocking(vmx, vcpu);
2303 * Update VM-entry instruction length if the event being
2304 * delivered was a software interrupt or software exception.
2306 if (intr_type == VMCS_INTR_T_SWINTR ||
2307 intr_type == VMCS_INTR_T_PRIV_SWEXCEPTION ||
2308 intr_type == VMCS_INTR_T_SWEXCEPTION) {
2309 vmcs_write(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length);
2314 case EXIT_REASON_TASK_SWITCH:
2315 ts = &vmexit->u.task_switch;
2316 ts->tsssel = qual & 0xffff;
2317 ts->reason = vmx_task_switch_reason(qual);
2319 ts->errcode_valid = 0;
2320 vmx_paging_info(&ts->paging);
2322 * If the task switch was due to a CALL, JMP, IRET, software
2323 * interrupt (INT n) or software exception (INT3, INTO),
2324 * then the saved %rip references the instruction that caused
2325 * the task switch. The instruction length field in the VMCS
2326 * is valid in this case.
2328 * In all other cases (e.g., NMI, hardware exception) the
2329 * saved %rip is one that would have been saved in the old TSS
2330 * had the task switch completed normally so the instruction
2331 * length field is not needed in this case and is explicitly
2334 if (ts->reason == TSR_IDT_GATE) {
2335 KASSERT(idtvec_info & VMCS_IDT_VEC_VALID,
2336 ("invalid idtvec_info %#x for IDT task switch",
2338 intr_type = idtvec_info & VMCS_INTR_T_MASK;
2339 if (intr_type != VMCS_INTR_T_SWINTR &&
2340 intr_type != VMCS_INTR_T_SWEXCEPTION &&
2341 intr_type != VMCS_INTR_T_PRIV_SWEXCEPTION) {
2342 /* Task switch triggered by external event */
2344 vmexit->inst_length = 0;
2345 if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) {
2346 ts->errcode_valid = 1;
2347 ts->errcode = vmcs_idt_vectoring_err();
2351 vmexit->exitcode = VM_EXITCODE_TASK_SWITCH;
2352 SDT_PROBE4(vmm, vmx, exit, taskswitch, vmx, vcpu, vmexit, ts);
2353 VCPU_CTR4(vmx->vm, vcpu, "task switch reason %d, tss 0x%04x, "
2354 "%s errcode 0x%016lx", ts->reason, ts->tsssel,
2355 ts->ext ? "external" : "internal",
2356 ((uint64_t)ts->errcode << 32) | ts->errcode_valid);
2358 case EXIT_REASON_CR_ACCESS:
2359 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CR_ACCESS, 1);
2360 SDT_PROBE4(vmm, vmx, exit, craccess, vmx, vcpu, vmexit, qual);
2361 switch (qual & 0xf) {
2363 handled = vmx_emulate_cr0_access(vmx, vcpu, qual);
2366 handled = vmx_emulate_cr4_access(vmx, vcpu, qual);
2369 handled = vmx_emulate_cr8_access(vmx, vcpu, qual);
2373 case EXIT_REASON_RDMSR:
2374 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_RDMSR, 1);
2376 ecx = vmxctx->guest_rcx;
2377 VCPU_CTR1(vmx->vm, vcpu, "rdmsr 0x%08x", ecx);
2378 SDT_PROBE4(vmm, vmx, exit, rdmsr, vmx, vcpu, vmexit, ecx);
2379 error = emulate_rdmsr(vmx, vcpu, ecx, &retu);
2381 vmexit->exitcode = VM_EXITCODE_RDMSR;
2382 vmexit->u.msr.code = ecx;
2386 /* Return to userspace with a valid exitcode */
2387 KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
2388 ("emulate_rdmsr retu with bogus exitcode"));
2391 case EXIT_REASON_WRMSR:
2392 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_WRMSR, 1);
2394 eax = vmxctx->guest_rax;
2395 ecx = vmxctx->guest_rcx;
2396 edx = vmxctx->guest_rdx;
2397 VCPU_CTR2(vmx->vm, vcpu, "wrmsr 0x%08x value 0x%016lx",
2398 ecx, (uint64_t)edx << 32 | eax);
2399 SDT_PROBE5(vmm, vmx, exit, wrmsr, vmx, vmexit, vcpu, ecx,
2400 (uint64_t)edx << 32 | eax);
2401 error = emulate_wrmsr(vmx, vcpu, ecx,
2402 (uint64_t)edx << 32 | eax, &retu);
2404 vmexit->exitcode = VM_EXITCODE_WRMSR;
2405 vmexit->u.msr.code = ecx;
2406 vmexit->u.msr.wval = (uint64_t)edx << 32 | eax;
2410 /* Return to userspace with a valid exitcode */
2411 KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
2412 ("emulate_wrmsr retu with bogus exitcode"));
2415 case EXIT_REASON_HLT:
2416 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_HLT, 1);
2417 SDT_PROBE3(vmm, vmx, exit, halt, vmx, vcpu, vmexit);
2418 vmexit->exitcode = VM_EXITCODE_HLT;
2419 vmexit->u.hlt.rflags = vmcs_read(VMCS_GUEST_RFLAGS);
2420 if (virtual_interrupt_delivery)
2421 vmexit->u.hlt.intr_status =
2422 vmcs_read(VMCS_GUEST_INTR_STATUS);
2424 vmexit->u.hlt.intr_status = 0;
2426 case EXIT_REASON_MTF:
2427 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_MTRAP, 1);
2428 SDT_PROBE3(vmm, vmx, exit, mtrap, vmx, vcpu, vmexit);
2429 vmexit->exitcode = VM_EXITCODE_MTRAP;
2430 vmexit->inst_length = 0;
2432 case EXIT_REASON_PAUSE:
2433 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_PAUSE, 1);
2434 SDT_PROBE3(vmm, vmx, exit, pause, vmx, vcpu, vmexit);
2435 vmexit->exitcode = VM_EXITCODE_PAUSE;
2437 case EXIT_REASON_INTR_WINDOW:
2438 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INTR_WINDOW, 1);
2439 SDT_PROBE3(vmm, vmx, exit, intrwindow, vmx, vcpu, vmexit);
2440 vmx_clear_int_window_exiting(vmx, vcpu);
2442 case EXIT_REASON_EXT_INTR:
2444 * External interrupts serve only to cause VM exits and allow
2445 * the host interrupt handler to run.
2447 * If this external interrupt triggers a virtual interrupt
2448 * to a VM, then that state will be recorded by the
2449 * host interrupt handler in the VM's softc. We will inject
2450 * this virtual interrupt during the subsequent VM enter.
2452 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
2453 SDT_PROBE4(vmm, vmx, exit, interrupt,
2454 vmx, vcpu, vmexit, intr_info);
2457 * XXX: Ignore this exit if VMCS_INTR_VALID is not set.
2458 * This appears to be a bug in VMware Fusion?
2460 if (!(intr_info & VMCS_INTR_VALID))
2462 KASSERT((intr_info & VMCS_INTR_VALID) != 0 &&
2463 (intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_HWINTR,
2464 ("VM exit interruption info invalid: %#x", intr_info));
2465 vmx_trigger_hostintr(intr_info & 0xff);
2468 * This is special. We want to treat this as an 'handled'
2469 * VM-exit but not increment the instruction pointer.
2471 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXTINT, 1);
2473 case EXIT_REASON_NMI_WINDOW:
2474 SDT_PROBE3(vmm, vmx, exit, nmiwindow, vmx, vcpu, vmexit);
2475 /* Exit to allow the pending virtual NMI to be injected */
2476 if (vm_nmi_pending(vmx->vm, vcpu))
2477 vmx_inject_nmi(vmx, vcpu);
2478 vmx_clear_nmi_window_exiting(vmx, vcpu);
2479 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NMI_WINDOW, 1);
2481 case EXIT_REASON_INOUT:
2482 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INOUT, 1);
2483 vmexit->exitcode = VM_EXITCODE_INOUT;
2484 vmexit->u.inout.bytes = (qual & 0x7) + 1;
2485 vmexit->u.inout.in = in = (qual & 0x8) ? 1 : 0;
2486 vmexit->u.inout.string = (qual & 0x10) ? 1 : 0;
2487 vmexit->u.inout.rep = (qual & 0x20) ? 1 : 0;
2488 vmexit->u.inout.port = (uint16_t)(qual >> 16);
2489 vmexit->u.inout.eax = (uint32_t)(vmxctx->guest_rax);
2490 if (vmexit->u.inout.string) {
2491 inst_info = vmcs_read(VMCS_EXIT_INSTRUCTION_INFO);
2492 vmexit->exitcode = VM_EXITCODE_INOUT_STR;
2493 vis = &vmexit->u.inout_str;
2494 vmx_paging_info(&vis->paging);
2495 vis->rflags = vmcs_read(VMCS_GUEST_RFLAGS);
2496 vis->cr0 = vmcs_read(VMCS_GUEST_CR0);
2497 vis->index = inout_str_index(vmx, vcpu, in);
2498 vis->count = inout_str_count(vmx, vcpu, vis->inout.rep);
2499 vis->addrsize = inout_str_addrsize(inst_info);
2500 inout_str_seginfo(vmx, vcpu, inst_info, in, vis);
2502 SDT_PROBE3(vmm, vmx, exit, inout, vmx, vcpu, vmexit);
2504 case EXIT_REASON_CPUID:
2505 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CPUID, 1);
2506 SDT_PROBE3(vmm, vmx, exit, cpuid, vmx, vcpu, vmexit);
2507 handled = vmx_handle_cpuid(vmx->vm, vcpu, vmxctx);
2509 case EXIT_REASON_EXCEPTION:
2510 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXCEPTION, 1);
2511 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
2512 KASSERT((intr_info & VMCS_INTR_VALID) != 0,
2513 ("VM exit interruption info invalid: %#x", intr_info));
2515 intr_vec = intr_info & 0xff;
2516 intr_type = intr_info & VMCS_INTR_T_MASK;
2519 * If Virtual NMIs control is 1 and the VM-exit is due to a
2520 * fault encountered during the execution of IRET then we must
2521 * restore the state of "virtual-NMI blocking" before resuming
2524 * See "Resuming Guest Software after Handling an Exception".
2525 * See "Information for VM Exits Due to Vectored Events".
2527 if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 &&
2528 (intr_vec != IDT_DF) &&
2529 (intr_info & EXIT_QUAL_NMIUDTI) != 0)
2530 vmx_restore_nmi_blocking(vmx, vcpu);
2533 * The NMI has already been handled in vmx_exit_handle_nmi().
2535 if (intr_type == VMCS_INTR_T_NMI)
2539 * Call the machine check handler by hand. Also don't reflect
2540 * the machine check back into the guest.
2542 if (intr_vec == IDT_MC) {
2543 VCPU_CTR0(vmx->vm, vcpu, "Vectoring to MCE handler");
2544 __asm __volatile("int $18");
2548 if (intr_vec == IDT_PF) {
2549 error = vmxctx_setreg(vmxctx, VM_REG_GUEST_CR2, qual);
2550 KASSERT(error == 0, ("%s: vmxctx_setreg(cr2) error %d",
2555 * Software exceptions exhibit trap-like behavior. This in
2556 * turn requires populating the VM-entry instruction length
2557 * so that the %rip in the trap frame is past the INT3/INTO
2560 if (intr_type == VMCS_INTR_T_SWEXCEPTION)
2561 vmcs_write(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length);
2563 /* Reflect all other exceptions back into the guest */
2564 errcode_valid = errcode = 0;
2565 if (intr_info & VMCS_INTR_DEL_ERRCODE) {
2567 errcode = vmcs_read(VMCS_EXIT_INTR_ERRCODE);
2569 VCPU_CTR2(vmx->vm, vcpu, "Reflecting exception %d/%#x into "
2570 "the guest", intr_vec, errcode);
2571 SDT_PROBE5(vmm, vmx, exit, exception,
2572 vmx, vcpu, vmexit, intr_vec, errcode);
2573 error = vm_inject_exception(vmx->vm, vcpu, intr_vec,
2574 errcode_valid, errcode, 0);
2575 KASSERT(error == 0, ("%s: vm_inject_exception error %d",
2579 case EXIT_REASON_EPT_FAULT:
2581 * If 'gpa' lies within the address space allocated to
2582 * memory then this must be a nested page fault otherwise
2583 * this must be an instruction that accesses MMIO space.
2586 if (vm_mem_allocated(vmx->vm, vcpu, gpa) ||
2587 apic_access_fault(vmx, vcpu, gpa)) {
2588 vmexit->exitcode = VM_EXITCODE_PAGING;
2589 vmexit->inst_length = 0;
2590 vmexit->u.paging.gpa = gpa;
2591 vmexit->u.paging.fault_type = ept_fault_type(qual);
2592 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NESTED_FAULT, 1);
2593 SDT_PROBE5(vmm, vmx, exit, nestedfault,
2594 vmx, vcpu, vmexit, gpa, qual);
2595 } else if (ept_emulation_fault(qual)) {
2596 vmexit_inst_emul(vmexit, gpa, vmcs_gla());
2597 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INST_EMUL, 1);
2598 SDT_PROBE4(vmm, vmx, exit, mmiofault,
2599 vmx, vcpu, vmexit, gpa);
2602 * If Virtual NMIs control is 1 and the VM-exit is due to an
2603 * EPT fault during the execution of IRET then we must restore
2604 * the state of "virtual-NMI blocking" before resuming.
2606 * See description of "NMI unblocking due to IRET" in
2607 * "Exit Qualification for EPT Violations".
2609 if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 &&
2610 (qual & EXIT_QUAL_NMIUDTI) != 0)
2611 vmx_restore_nmi_blocking(vmx, vcpu);
2613 case EXIT_REASON_VIRTUALIZED_EOI:
2614 vmexit->exitcode = VM_EXITCODE_IOAPIC_EOI;
2615 vmexit->u.ioapic_eoi.vector = qual & 0xFF;
2616 SDT_PROBE3(vmm, vmx, exit, eoi, vmx, vcpu, vmexit);
2617 vmexit->inst_length = 0; /* trap-like */
2619 case EXIT_REASON_APIC_ACCESS:
2620 SDT_PROBE3(vmm, vmx, exit, apicaccess, vmx, vcpu, vmexit);
2621 handled = vmx_handle_apic_access(vmx, vcpu, vmexit);
2623 case EXIT_REASON_APIC_WRITE:
2625 * APIC-write VM exit is trap-like so the %rip is already
2626 * pointing to the next instruction.
2628 vmexit->inst_length = 0;
2629 vlapic = vm_lapic(vmx->vm, vcpu);
2630 SDT_PROBE4(vmm, vmx, exit, apicwrite,
2631 vmx, vcpu, vmexit, vlapic);
2632 handled = vmx_handle_apic_write(vmx, vcpu, vlapic, qual);
2634 case EXIT_REASON_XSETBV:
2635 SDT_PROBE3(vmm, vmx, exit, xsetbv, vmx, vcpu, vmexit);
2636 handled = vmx_emulate_xsetbv(vmx, vcpu, vmexit);
2638 case EXIT_REASON_MONITOR:
2639 SDT_PROBE3(vmm, vmx, exit, monitor, vmx, vcpu, vmexit);
2640 vmexit->exitcode = VM_EXITCODE_MONITOR;
2642 case EXIT_REASON_MWAIT:
2643 SDT_PROBE3(vmm, vmx, exit, mwait, vmx, vcpu, vmexit);
2644 vmexit->exitcode = VM_EXITCODE_MWAIT;
2646 case EXIT_REASON_VMCALL:
2647 case EXIT_REASON_VMCLEAR:
2648 case EXIT_REASON_VMLAUNCH:
2649 case EXIT_REASON_VMPTRLD:
2650 case EXIT_REASON_VMPTRST:
2651 case EXIT_REASON_VMREAD:
2652 case EXIT_REASON_VMRESUME:
2653 case EXIT_REASON_VMWRITE:
2654 case EXIT_REASON_VMXOFF:
2655 case EXIT_REASON_VMXON:
2656 SDT_PROBE3(vmm, vmx, exit, vminsn, vmx, vcpu, vmexit);
2657 vmexit->exitcode = VM_EXITCODE_VMINSN;
2660 SDT_PROBE4(vmm, vmx, exit, unknown,
2661 vmx, vcpu, vmexit, reason);
2662 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_UNKNOWN, 1);
2668 * It is possible that control is returned to userland
2669 * even though we were able to handle the VM exit in the
2672 * In such a case we want to make sure that the userland
2673 * restarts guest execution at the instruction *after*
2674 * the one we just processed. Therefore we update the
2675 * guest rip in the VMCS and in 'vmexit'.
2677 vmexit->rip += vmexit->inst_length;
2678 vmexit->inst_length = 0;
2679 vmcs_write(VMCS_GUEST_RIP, vmexit->rip);
2681 if (vmexit->exitcode == VM_EXITCODE_BOGUS) {
2683 * If this VM exit was not claimed by anybody then
2684 * treat it as a generic VMX exit.
2686 vmexit->exitcode = VM_EXITCODE_VMX;
2687 vmexit->u.vmx.status = VM_SUCCESS;
2688 vmexit->u.vmx.inst_type = 0;
2689 vmexit->u.vmx.inst_error = 0;
2692 * The exitcode and collateral have been populated.
2693 * The VM exit will be processed further in userland.
2698 SDT_PROBE4(vmm, vmx, exit, return,
2699 vmx, vcpu, vmexit, handled);
2703 static __inline void
2704 vmx_exit_inst_error(struct vmxctx *vmxctx, int rc, struct vm_exit *vmexit)
2707 KASSERT(vmxctx->inst_fail_status != VM_SUCCESS,
2708 ("vmx_exit_inst_error: invalid inst_fail_status %d",
2709 vmxctx->inst_fail_status));
2711 vmexit->inst_length = 0;
2712 vmexit->exitcode = VM_EXITCODE_VMX;
2713 vmexit->u.vmx.status = vmxctx->inst_fail_status;
2714 vmexit->u.vmx.inst_error = vmcs_instruction_error();
2715 vmexit->u.vmx.exit_reason = ~0;
2716 vmexit->u.vmx.exit_qualification = ~0;
2719 case VMX_VMRESUME_ERROR:
2720 case VMX_VMLAUNCH_ERROR:
2721 case VMX_INVEPT_ERROR:
2722 vmexit->u.vmx.inst_type = rc;
2725 panic("vm_exit_inst_error: vmx_enter_guest returned %d", rc);
2730 * If the NMI-exiting VM execution control is set to '1' then an NMI in
2731 * non-root operation causes a VM-exit. NMI blocking is in effect so it is
2732 * sufficient to simply vector to the NMI handler via a software interrupt.
2733 * However, this must be done before maskable interrupts are enabled
2734 * otherwise the "iret" issued by an interrupt handler will incorrectly
2735 * clear NMI blocking.
2737 static __inline void
2738 vmx_exit_handle_nmi(struct vmx *vmx, int vcpuid, struct vm_exit *vmexit)
2742 KASSERT((read_rflags() & PSL_I) == 0, ("interrupts enabled"));
2744 if (vmexit->u.vmx.exit_reason != EXIT_REASON_EXCEPTION)
2747 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
2748 KASSERT((intr_info & VMCS_INTR_VALID) != 0,
2749 ("VM exit interruption info invalid: %#x", intr_info));
2751 if ((intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_NMI) {
2752 KASSERT((intr_info & 0xff) == IDT_NMI, ("VM exit due "
2753 "to NMI has invalid vector: %#x", intr_info));
2754 VCPU_CTR0(vmx->vm, vcpuid, "Vectoring to NMI handler");
2755 __asm __volatile("int $2");
2759 static __inline void
2760 vmx_dr_enter_guest(struct vmxctx *vmxctx)
2764 /* Save host control debug registers. */
2765 vmxctx->host_dr7 = rdr7();
2766 vmxctx->host_debugctl = rdmsr(MSR_DEBUGCTLMSR);
2769 * Disable debugging in DR7 and DEBUGCTL to avoid triggering
2770 * exceptions in the host based on the guest DRx values. The
2771 * guest DR7 and DEBUGCTL are saved/restored in the VMCS.
2774 wrmsr(MSR_DEBUGCTLMSR, 0);
2777 * Disable single stepping the kernel to avoid corrupting the
2778 * guest DR6. A debugger might still be able to corrupt the
2779 * guest DR6 by setting a breakpoint after this point and then
2782 rflags = read_rflags();
2783 vmxctx->host_tf = rflags & PSL_T;
2784 write_rflags(rflags & ~PSL_T);
2786 /* Save host debug registers. */
2787 vmxctx->host_dr0 = rdr0();
2788 vmxctx->host_dr1 = rdr1();
2789 vmxctx->host_dr2 = rdr2();
2790 vmxctx->host_dr3 = rdr3();
2791 vmxctx->host_dr6 = rdr6();
2793 /* Restore guest debug registers. */
2794 load_dr0(vmxctx->guest_dr0);
2795 load_dr1(vmxctx->guest_dr1);
2796 load_dr2(vmxctx->guest_dr2);
2797 load_dr3(vmxctx->guest_dr3);
2798 load_dr6(vmxctx->guest_dr6);
2801 static __inline void
2802 vmx_dr_leave_guest(struct vmxctx *vmxctx)
2805 /* Save guest debug registers. */
2806 vmxctx->guest_dr0 = rdr0();
2807 vmxctx->guest_dr1 = rdr1();
2808 vmxctx->guest_dr2 = rdr2();
2809 vmxctx->guest_dr3 = rdr3();
2810 vmxctx->guest_dr6 = rdr6();
2813 * Restore host debug registers. Restore DR7, DEBUGCTL, and
2816 load_dr0(vmxctx->host_dr0);
2817 load_dr1(vmxctx->host_dr1);
2818 load_dr2(vmxctx->host_dr2);
2819 load_dr3(vmxctx->host_dr3);
2820 load_dr6(vmxctx->host_dr6);
2821 wrmsr(MSR_DEBUGCTLMSR, vmxctx->host_debugctl);
2822 load_dr7(vmxctx->host_dr7);
2823 write_rflags(read_rflags() | vmxctx->host_tf);
2827 vmx_run(void *arg, int vcpu, register_t rip, pmap_t pmap,
2828 struct vm_eventinfo *evinfo)
2830 int rc, handled, launched;
2833 struct vmxctx *vmxctx;
2835 struct vm_exit *vmexit;
2836 struct vlapic *vlapic;
2837 uint32_t exit_reason;
2838 struct region_descriptor gdtr, idtr;
2843 vmcs = &vmx->vmcs[vcpu];
2844 vmxctx = &vmx->ctx[vcpu];
2845 vlapic = vm_lapic(vm, vcpu);
2846 vmexit = vm_exitinfo(vm, vcpu);
2849 KASSERT(vmxctx->pmap == pmap,
2850 ("pmap %p different than ctx pmap %p", pmap, vmxctx->pmap));
2852 vmx_msr_guest_enter(vmx, vcpu);
2858 * We do this every time because we may setup the virtual machine
2859 * from a different process than the one that actually runs it.
2861 * If the life of a virtual machine was spent entirely in the context
2862 * of a single process we could do this once in vmx_vminit().
2864 vmcs_write(VMCS_HOST_CR3, rcr3());
2866 vmcs_write(VMCS_GUEST_RIP, rip);
2867 vmx_set_pcpu_defaults(vmx, vcpu, pmap);
2869 KASSERT(vmcs_guest_rip() == rip, ("%s: vmcs guest rip mismatch "
2870 "%#lx/%#lx", __func__, vmcs_guest_rip(), rip));
2872 handled = UNHANDLED;
2874 * Interrupts are disabled from this point on until the
2875 * guest starts executing. This is done for the following
2878 * If an AST is asserted on this thread after the check below,
2879 * then the IPI_AST notification will not be lost, because it
2880 * will cause a VM exit due to external interrupt as soon as
2881 * the guest state is loaded.
2883 * A posted interrupt after 'vmx_inject_interrupts()' will
2884 * not be "lost" because it will be held pending in the host
2885 * APIC because interrupts are disabled. The pending interrupt
2886 * will be recognized as soon as the guest state is loaded.
2888 * The same reasoning applies to the IPI generated by
2889 * pmap_invalidate_ept().
2892 vmx_inject_interrupts(vmx, vcpu, vlapic, rip);
2895 * Check for vcpu suspension after injecting events because
2896 * vmx_inject_interrupts() can suspend the vcpu due to a
2899 if (vcpu_suspended(evinfo)) {
2901 vm_exit_suspended(vmx->vm, vcpu, rip);
2905 if (vcpu_rendezvous_pending(evinfo)) {
2907 vm_exit_rendezvous(vmx->vm, vcpu, rip);
2911 if (vcpu_reqidle(evinfo)) {
2913 vm_exit_reqidle(vmx->vm, vcpu, rip);
2917 if (vcpu_should_yield(vm, vcpu)) {
2919 vm_exit_astpending(vmx->vm, vcpu, rip);
2920 vmx_astpending_trace(vmx, vcpu, rip);
2926 * VM exits restore the base address but not the
2927 * limits of GDTR and IDTR. The VMCS only stores the
2928 * base address, so VM exits set the limits to 0xffff.
2929 * Save and restore the full GDTR and IDTR to restore
2932 * The VMCS does not save the LDTR at all, and VM
2933 * exits clear LDTR as if a NULL selector were loaded.
2934 * The userspace hypervisor probably doesn't use a
2935 * LDT, but save and restore it to be safe.
2941 vmx_run_trace(vmx, vcpu);
2942 vmx_dr_enter_guest(vmxctx);
2943 rc = vmx_enter_guest(vmxctx, vmx, launched);
2944 vmx_dr_leave_guest(vmxctx);
2950 /* Collect some information for VM exit processing */
2951 vmexit->rip = rip = vmcs_guest_rip();
2952 vmexit->inst_length = vmexit_instruction_length();
2953 vmexit->u.vmx.exit_reason = exit_reason = vmcs_exit_reason();
2954 vmexit->u.vmx.exit_qualification = vmcs_exit_qualification();
2956 /* Update 'nextrip' */
2957 vmx->state[vcpu].nextrip = rip;
2959 if (rc == VMX_GUEST_VMEXIT) {
2960 vmx_exit_handle_nmi(vmx, vcpu, vmexit);
2962 handled = vmx_exit_process(vmx, vcpu, vmexit);
2965 vmx_exit_inst_error(vmxctx, rc, vmexit);
2968 vmx_exit_trace(vmx, vcpu, rip, exit_reason, handled);
2973 * If a VM exit has been handled then the exitcode must be BOGUS
2974 * If a VM exit is not handled then the exitcode must not be BOGUS
2976 if ((handled && vmexit->exitcode != VM_EXITCODE_BOGUS) ||
2977 (!handled && vmexit->exitcode == VM_EXITCODE_BOGUS)) {
2978 panic("Mismatch between handled (%d) and exitcode (%d)",
2979 handled, vmexit->exitcode);
2983 vmm_stat_incr(vm, vcpu, VMEXIT_USERSPACE, 1);
2985 VCPU_CTR1(vm, vcpu, "returning from vmx_run: exitcode %d",
2989 vmx_msr_guest_exit(vmx, vcpu);
2995 vmx_vmcleanup(void *arg)
2998 struct vmx *vmx = arg;
3001 if (apic_access_virtualization(vmx, 0))
3002 vm_unmap_mmio(vmx->vm, DEFAULT_APIC_BASE, PAGE_SIZE);
3004 maxcpus = vm_get_maxcpus(vmx->vm);
3005 for (i = 0; i < maxcpus; i++)
3006 vpid_free(vmx->state[i].vpid);
3014 vmxctx_regptr(struct vmxctx *vmxctx, int reg)
3018 case VM_REG_GUEST_RAX:
3019 return (&vmxctx->guest_rax);
3020 case VM_REG_GUEST_RBX:
3021 return (&vmxctx->guest_rbx);
3022 case VM_REG_GUEST_RCX:
3023 return (&vmxctx->guest_rcx);
3024 case VM_REG_GUEST_RDX:
3025 return (&vmxctx->guest_rdx);
3026 case VM_REG_GUEST_RSI:
3027 return (&vmxctx->guest_rsi);
3028 case VM_REG_GUEST_RDI:
3029 return (&vmxctx->guest_rdi);
3030 case VM_REG_GUEST_RBP:
3031 return (&vmxctx->guest_rbp);
3032 case VM_REG_GUEST_R8:
3033 return (&vmxctx->guest_r8);
3034 case VM_REG_GUEST_R9:
3035 return (&vmxctx->guest_r9);
3036 case VM_REG_GUEST_R10:
3037 return (&vmxctx->guest_r10);
3038 case VM_REG_GUEST_R11:
3039 return (&vmxctx->guest_r11);
3040 case VM_REG_GUEST_R12:
3041 return (&vmxctx->guest_r12);
3042 case VM_REG_GUEST_R13:
3043 return (&vmxctx->guest_r13);
3044 case VM_REG_GUEST_R14:
3045 return (&vmxctx->guest_r14);
3046 case VM_REG_GUEST_R15:
3047 return (&vmxctx->guest_r15);
3048 case VM_REG_GUEST_CR2:
3049 return (&vmxctx->guest_cr2);
3050 case VM_REG_GUEST_DR0:
3051 return (&vmxctx->guest_dr0);
3052 case VM_REG_GUEST_DR1:
3053 return (&vmxctx->guest_dr1);
3054 case VM_REG_GUEST_DR2:
3055 return (&vmxctx->guest_dr2);
3056 case VM_REG_GUEST_DR3:
3057 return (&vmxctx->guest_dr3);
3058 case VM_REG_GUEST_DR6:
3059 return (&vmxctx->guest_dr6);
3067 vmxctx_getreg(struct vmxctx *vmxctx, int reg, uint64_t *retval)
3071 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
3079 vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val)
3083 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
3091 vmx_get_intr_shadow(struct vmx *vmx, int vcpu, int running, uint64_t *retval)
3096 error = vmcs_getreg(&vmx->vmcs[vcpu], running,
3097 VMCS_IDENT(VMCS_GUEST_INTERRUPTIBILITY), &gi);
3098 *retval = (gi & HWINTR_BLOCKING) ? 1 : 0;
3103 vmx_modify_intr_shadow(struct vmx *vmx, int vcpu, int running, uint64_t val)
3110 * Forcing the vcpu into an interrupt shadow is not supported.
3117 vmcs = &vmx->vmcs[vcpu];
3118 ident = VMCS_IDENT(VMCS_GUEST_INTERRUPTIBILITY);
3119 error = vmcs_getreg(vmcs, running, ident, &gi);
3121 gi &= ~HWINTR_BLOCKING;
3122 error = vmcs_setreg(vmcs, running, ident, gi);
3125 VCPU_CTR2(vmx->vm, vcpu, "Setting intr_shadow to %#lx %s", val,
3126 error ? "failed" : "succeeded");
3131 vmx_shadow_reg(int reg)
3138 case VM_REG_GUEST_CR0:
3139 shreg = VMCS_CR0_SHADOW;
3141 case VM_REG_GUEST_CR4:
3142 shreg = VMCS_CR4_SHADOW;
3152 vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval)
3154 int running, hostcpu;
3155 struct vmx *vmx = arg;
3157 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
3158 if (running && hostcpu != curcpu)
3159 panic("vmx_getreg: %s%d is running", vm_name(vmx->vm), vcpu);
3161 if (reg == VM_REG_GUEST_INTR_SHADOW)
3162 return (vmx_get_intr_shadow(vmx, vcpu, running, retval));
3164 if (vmxctx_getreg(&vmx->ctx[vcpu], reg, retval) == 0)
3167 return (vmcs_getreg(&vmx->vmcs[vcpu], running, reg, retval));
3171 vmx_setreg(void *arg, int vcpu, int reg, uint64_t val)
3173 int error, hostcpu, running, shadow;
3176 struct vmx *vmx = arg;
3178 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
3179 if (running && hostcpu != curcpu)
3180 panic("vmx_setreg: %s%d is running", vm_name(vmx->vm), vcpu);
3182 if (reg == VM_REG_GUEST_INTR_SHADOW)
3183 return (vmx_modify_intr_shadow(vmx, vcpu, running, val));
3185 if (vmxctx_setreg(&vmx->ctx[vcpu], reg, val) == 0)
3188 /* Do not permit user write access to VMCS fields by offset. */
3192 error = vmcs_setreg(&vmx->vmcs[vcpu], running, reg, val);
3196 * If the "load EFER" VM-entry control is 1 then the
3197 * value of EFER.LMA must be identical to "IA-32e mode guest"
3198 * bit in the VM-entry control.
3200 if ((entry_ctls & VM_ENTRY_LOAD_EFER) != 0 &&
3201 (reg == VM_REG_GUEST_EFER)) {
3202 vmcs_getreg(&vmx->vmcs[vcpu], running,
3203 VMCS_IDENT(VMCS_ENTRY_CTLS), &ctls);
3205 ctls |= VM_ENTRY_GUEST_LMA;
3207 ctls &= ~VM_ENTRY_GUEST_LMA;
3208 vmcs_setreg(&vmx->vmcs[vcpu], running,
3209 VMCS_IDENT(VMCS_ENTRY_CTLS), ctls);
3212 shadow = vmx_shadow_reg(reg);
3215 * Store the unmodified value in the shadow
3217 error = vmcs_setreg(&vmx->vmcs[vcpu], running,
3218 VMCS_IDENT(shadow), val);
3221 if (reg == VM_REG_GUEST_CR3) {
3223 * Invalidate the guest vcpu's TLB mappings to emulate
3224 * the behavior of updating %cr3.
3226 * XXX the processor retains global mappings when %cr3
3227 * is updated but vmx_invvpid() does not.
3229 pmap = vmx->ctx[vcpu].pmap;
3230 vmx_invvpid(vmx, vcpu, pmap, running);
3238 vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
3240 int hostcpu, running;
3241 struct vmx *vmx = arg;
3243 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
3244 if (running && hostcpu != curcpu)
3245 panic("vmx_getdesc: %s%d is running", vm_name(vmx->vm), vcpu);
3247 return (vmcs_getdesc(&vmx->vmcs[vcpu], running, reg, desc));
3251 vmx_setdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
3253 int hostcpu, running;
3254 struct vmx *vmx = arg;
3256 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
3257 if (running && hostcpu != curcpu)
3258 panic("vmx_setdesc: %s%d is running", vm_name(vmx->vm), vcpu);
3260 return (vmcs_setdesc(&vmx->vmcs[vcpu], running, reg, desc));
3264 vmx_getcap(void *arg, int vcpu, int type, int *retval)
3266 struct vmx *vmx = arg;
3272 vcap = vmx->cap[vcpu].set;
3275 case VM_CAP_HALT_EXIT:
3279 case VM_CAP_PAUSE_EXIT:
3283 case VM_CAP_MTRAP_EXIT:
3284 if (cap_monitor_trap)
3287 case VM_CAP_UNRESTRICTED_GUEST:
3288 if (cap_unrestricted_guest)
3291 case VM_CAP_ENABLE_INVPCID:
3300 *retval = (vcap & (1 << type)) ? 1 : 0;
3306 vmx_setcap(void *arg, int vcpu, int type, int val)
3308 struct vmx *vmx = arg;
3309 struct vmcs *vmcs = &vmx->vmcs[vcpu];
3321 case VM_CAP_HALT_EXIT:
3322 if (cap_halt_exit) {
3324 pptr = &vmx->cap[vcpu].proc_ctls;
3326 flag = PROCBASED_HLT_EXITING;
3327 reg = VMCS_PRI_PROC_BASED_CTLS;
3330 case VM_CAP_MTRAP_EXIT:
3331 if (cap_monitor_trap) {
3333 pptr = &vmx->cap[vcpu].proc_ctls;
3335 flag = PROCBASED_MTF;
3336 reg = VMCS_PRI_PROC_BASED_CTLS;
3339 case VM_CAP_PAUSE_EXIT:
3340 if (cap_pause_exit) {
3342 pptr = &vmx->cap[vcpu].proc_ctls;
3344 flag = PROCBASED_PAUSE_EXITING;
3345 reg = VMCS_PRI_PROC_BASED_CTLS;
3348 case VM_CAP_UNRESTRICTED_GUEST:
3349 if (cap_unrestricted_guest) {
3351 pptr = &vmx->cap[vcpu].proc_ctls2;
3353 flag = PROCBASED2_UNRESTRICTED_GUEST;
3354 reg = VMCS_SEC_PROC_BASED_CTLS;
3357 case VM_CAP_ENABLE_INVPCID:
3360 pptr = &vmx->cap[vcpu].proc_ctls2;
3362 flag = PROCBASED2_ENABLE_INVPCID;
3363 reg = VMCS_SEC_PROC_BASED_CTLS;
3377 error = vmwrite(reg, baseval);
3384 * Update optional stored flags, and record
3392 vmx->cap[vcpu].set |= (1 << type);
3394 vmx->cap[vcpu].set &= ~(1 << type);
3403 struct vlapic vlapic;
3404 struct pir_desc *pir_desc;
3409 #define VPR_PRIO_BIT(vpr) (1 << ((vpr) >> 4))
3411 #define VMX_CTR_PIR(vm, vcpuid, pir_desc, notify, vector, level, msg) \
3413 VCPU_CTR2(vm, vcpuid, msg " assert %s-triggered vector %d", \
3414 level ? "level" : "edge", vector); \
3415 VCPU_CTR1(vm, vcpuid, msg " pir0 0x%016lx", pir_desc->pir[0]); \
3416 VCPU_CTR1(vm, vcpuid, msg " pir1 0x%016lx", pir_desc->pir[1]); \
3417 VCPU_CTR1(vm, vcpuid, msg " pir2 0x%016lx", pir_desc->pir[2]); \
3418 VCPU_CTR1(vm, vcpuid, msg " pir3 0x%016lx", pir_desc->pir[3]); \
3419 VCPU_CTR1(vm, vcpuid, msg " notify: %s", notify ? "yes" : "no");\
3423 * vlapic->ops handlers that utilize the APICv hardware assist described in
3424 * Chapter 29 of the Intel SDM.
3427 vmx_set_intr_ready(struct vlapic *vlapic, int vector, bool level)
3429 struct vlapic_vtx *vlapic_vtx;
3430 struct pir_desc *pir_desc;
3432 int idx, notify = 0;
3434 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3435 pir_desc = vlapic_vtx->pir_desc;
3438 * Keep track of interrupt requests in the PIR descriptor. This is
3439 * because the virtual APIC page pointed to by the VMCS cannot be
3440 * modified if the vcpu is running.
3443 mask = 1UL << (vector % 64);
3444 atomic_set_long(&pir_desc->pir[idx], mask);
3447 * A notification is required whenever the 'pending' bit makes a
3448 * transition from 0->1.
3450 * Even if the 'pending' bit is already asserted, notification about
3451 * the incoming interrupt may still be necessary. For example, if a
3452 * vCPU is HLTed with a high PPR, a low priority interrupt would cause
3453 * the 0->1 'pending' transition with a notification, but the vCPU
3454 * would ignore the interrupt for the time being. The same vCPU would
3455 * need to then be notified if a high-priority interrupt arrived which
3456 * satisfied the PPR.
3458 * The priorities of interrupts injected while 'pending' is asserted
3459 * are tracked in a custom bitfield 'pending_prio'. Should the
3460 * to-be-injected interrupt exceed the priorities already present, the
3461 * notification is sent. The priorities recorded in 'pending_prio' are
3462 * cleared whenever the 'pending' bit makes another 0->1 transition.
3464 if (atomic_cmpset_long(&pir_desc->pending, 0, 1) != 0) {
3466 vlapic_vtx->pending_prio = 0;
3468 const u_int old_prio = vlapic_vtx->pending_prio;
3469 const u_int prio_bit = VPR_PRIO_BIT(vector & APIC_TPR_INT);
3471 if ((old_prio & prio_bit) == 0 && prio_bit > old_prio) {
3472 atomic_set_int(&vlapic_vtx->pending_prio, prio_bit);
3477 VMX_CTR_PIR(vlapic->vm, vlapic->vcpuid, pir_desc, notify, vector,
3478 level, "vmx_set_intr_ready");
3483 vmx_pending_intr(struct vlapic *vlapic, int *vecptr)
3485 struct vlapic_vtx *vlapic_vtx;
3486 struct pir_desc *pir_desc;
3487 struct LAPIC *lapic;
3488 uint64_t pending, pirval;
3493 * This function is only expected to be called from the 'HLT' exit
3494 * handler which does not care about the vector that is pending.
3496 KASSERT(vecptr == NULL, ("vmx_pending_intr: vecptr must be NULL"));
3498 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3499 pir_desc = vlapic_vtx->pir_desc;
3501 pending = atomic_load_acq_long(&pir_desc->pending);
3504 * While a virtual interrupt may have already been
3505 * processed the actual delivery maybe pending the
3506 * interruptibility of the guest. Recognize a pending
3507 * interrupt by reevaluating virtual interrupts
3508 * following Section 29.2.1 in the Intel SDM Volume 3.
3510 struct vm_exit *vmexit;
3513 vmexit = vm_exitinfo(vlapic->vm, vlapic->vcpuid);
3514 KASSERT(vmexit->exitcode == VM_EXITCODE_HLT,
3515 ("vmx_pending_intr: exitcode not 'HLT'"));
3516 rvi = vmexit->u.hlt.intr_status & APIC_TPR_INT;
3517 lapic = vlapic->apic_page;
3518 ppr = lapic->ppr & APIC_TPR_INT;
3527 * If there is an interrupt pending then it will be recognized only
3528 * if its priority is greater than the processor priority.
3530 * Special case: if the processor priority is zero then any pending
3531 * interrupt will be recognized.
3533 lapic = vlapic->apic_page;
3534 ppr = lapic->ppr & APIC_TPR_INT;
3538 VCPU_CTR1(vlapic->vm, vlapic->vcpuid, "HLT with non-zero PPR %d",
3542 for (i = 3; i >= 0; i--) {
3543 pirval = pir_desc->pir[i];
3545 vpr = (i * 64 + flsl(pirval) - 1) & APIC_TPR_INT;
3551 * If the highest-priority pending interrupt falls short of the
3552 * processor priority of this vCPU, ensure that 'pending_prio' does not
3553 * have any stale bits which would preclude a higher-priority interrupt
3554 * from incurring a notification later.
3557 const u_int prio_bit = VPR_PRIO_BIT(vpr);
3558 const u_int old = vlapic_vtx->pending_prio;
3560 if (old > prio_bit && (old & prio_bit) == 0) {
3561 vlapic_vtx->pending_prio = prio_bit;
3569 vmx_intr_accepted(struct vlapic *vlapic, int vector)
3572 panic("vmx_intr_accepted: not expected to be called");
3576 vmx_set_tmr(struct vlapic *vlapic, int vector, bool level)
3578 struct vlapic_vtx *vlapic_vtx;
3583 KASSERT(vector >= 0 && vector <= 255, ("invalid vector %d", vector));
3584 KASSERT(!vcpu_is_running(vlapic->vm, vlapic->vcpuid, NULL),
3585 ("vmx_set_tmr: vcpu cannot be running"));
3587 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3588 vmx = vlapic_vtx->vmx;
3589 vmcs = &vmx->vmcs[vlapic->vcpuid];
3590 mask = 1UL << (vector % 64);
3593 val = vmcs_read(VMCS_EOI_EXIT(vector));
3598 vmcs_write(VMCS_EOI_EXIT(vector), val);
3603 vmx_enable_x2apic_mode(struct vlapic *vlapic)
3607 uint32_t proc_ctls2;
3610 vcpuid = vlapic->vcpuid;
3611 vmx = ((struct vlapic_vtx *)vlapic)->vmx;
3612 vmcs = &vmx->vmcs[vcpuid];
3614 proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
3615 KASSERT((proc_ctls2 & PROCBASED2_VIRTUALIZE_APIC_ACCESSES) != 0,
3616 ("%s: invalid proc_ctls2 %#x", __func__, proc_ctls2));
3618 proc_ctls2 &= ~PROCBASED2_VIRTUALIZE_APIC_ACCESSES;
3619 proc_ctls2 |= PROCBASED2_VIRTUALIZE_X2APIC_MODE;
3620 vmx->cap[vcpuid].proc_ctls2 = proc_ctls2;
3623 vmcs_write(VMCS_SEC_PROC_BASED_CTLS, proc_ctls2);
3626 if (vlapic->vcpuid == 0) {
3628 * The nested page table mappings are shared by all vcpus
3629 * so unmap the APIC access page just once.
3631 error = vm_unmap_mmio(vmx->vm, DEFAULT_APIC_BASE, PAGE_SIZE);
3632 KASSERT(error == 0, ("%s: vm_unmap_mmio error %d",
3636 * The MSR bitmap is shared by all vcpus so modify it only
3637 * once in the context of vcpu 0.
3639 error = vmx_allow_x2apic_msrs(vmx);
3640 KASSERT(error == 0, ("%s: vmx_allow_x2apic_msrs error %d",
3646 vmx_post_intr(struct vlapic *vlapic, int hostcpu)
3649 ipi_cpu(hostcpu, pirvec);
3653 * Transfer the pending interrupts in the PIR descriptor to the IRR
3654 * in the virtual APIC page.
3657 vmx_inject_pir(struct vlapic *vlapic)
3659 struct vlapic_vtx *vlapic_vtx;
3660 struct pir_desc *pir_desc;
3661 struct LAPIC *lapic;
3662 uint64_t val, pirval;
3663 int rvi, pirbase = -1;
3664 uint16_t intr_status_old, intr_status_new;
3666 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3667 pir_desc = vlapic_vtx->pir_desc;
3668 if (atomic_cmpset_long(&pir_desc->pending, 1, 0) == 0) {
3669 VCPU_CTR0(vlapic->vm, vlapic->vcpuid, "vmx_inject_pir: "
3670 "no posted interrupt pending");
3676 lapic = vlapic->apic_page;
3678 val = atomic_readandclear_long(&pir_desc->pir[0]);
3681 lapic->irr1 |= val >> 32;
3686 val = atomic_readandclear_long(&pir_desc->pir[1]);
3689 lapic->irr3 |= val >> 32;
3694 val = atomic_readandclear_long(&pir_desc->pir[2]);
3697 lapic->irr5 |= val >> 32;
3702 val = atomic_readandclear_long(&pir_desc->pir[3]);
3705 lapic->irr7 |= val >> 32;
3710 VLAPIC_CTR_IRR(vlapic, "vmx_inject_pir");
3713 * Update RVI so the processor can evaluate pending virtual
3714 * interrupts on VM-entry.
3716 * It is possible for pirval to be 0 here, even though the
3717 * pending bit has been set. The scenario is:
3718 * CPU-Y is sending a posted interrupt to CPU-X, which
3719 * is running a guest and processing posted interrupts in h/w.
3720 * CPU-X will eventually exit and the state seen in s/w is
3721 * the pending bit set, but no PIR bits set.
3724 * (vm running) (host running)
3725 * rx posted interrupt
3728 * READ/CLEAR PIR bits
3731 * pending bit set, PIR 0
3734 rvi = pirbase + flsl(pirval) - 1;
3735 intr_status_old = vmcs_read(VMCS_GUEST_INTR_STATUS);
3736 intr_status_new = (intr_status_old & 0xFF00) | rvi;
3737 if (intr_status_new > intr_status_old) {
3738 vmcs_write(VMCS_GUEST_INTR_STATUS, intr_status_new);
3739 VCPU_CTR2(vlapic->vm, vlapic->vcpuid, "vmx_inject_pir: "
3740 "guest_intr_status changed from 0x%04x to 0x%04x",
3741 intr_status_old, intr_status_new);
3746 static struct vlapic *
3747 vmx_vlapic_init(void *arg, int vcpuid)
3750 struct vlapic *vlapic;
3751 struct vlapic_vtx *vlapic_vtx;
3755 vlapic = malloc(sizeof(struct vlapic_vtx), M_VLAPIC, M_WAITOK | M_ZERO);
3756 vlapic->vm = vmx->vm;
3757 vlapic->vcpuid = vcpuid;
3758 vlapic->apic_page = (struct LAPIC *)&vmx->apic_page[vcpuid];
3760 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3761 vlapic_vtx->pir_desc = &vmx->pir_desc[vcpuid];
3762 vlapic_vtx->vmx = vmx;
3764 if (virtual_interrupt_delivery) {
3765 vlapic->ops.set_intr_ready = vmx_set_intr_ready;
3766 vlapic->ops.pending_intr = vmx_pending_intr;
3767 vlapic->ops.intr_accepted = vmx_intr_accepted;
3768 vlapic->ops.set_tmr = vmx_set_tmr;
3769 vlapic->ops.enable_x2apic_mode = vmx_enable_x2apic_mode;
3772 if (posted_interrupts)
3773 vlapic->ops.post_intr = vmx_post_intr;
3775 vlapic_init(vlapic);
3781 vmx_vlapic_cleanup(void *arg, struct vlapic *vlapic)
3784 vlapic_cleanup(vlapic);
3785 free(vlapic, M_VLAPIC);
3788 struct vmm_ops vmm_ops_intel = {