2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
39 #include <sys/sysctl.h>
44 #include <machine/psl.h>
45 #include <machine/cpufunc.h>
46 #include <machine/md_var.h>
47 #include <machine/segments.h>
48 #include <machine/smp.h>
49 #include <machine/specialreg.h>
50 #include <machine/vmparam.h>
52 #include <machine/vmm.h>
53 #include <machine/vmm_dev.h>
60 #include "vlapic_priv.h"
64 #include "vmx_cpufunc.h"
67 #include "vmx_controls.h"
69 #define PINBASED_CTLS_ONE_SETTING \
70 (PINBASED_EXTINT_EXITING | \
71 PINBASED_NMI_EXITING | \
73 #define PINBASED_CTLS_ZERO_SETTING 0
75 #define PROCBASED_CTLS_WINDOW_SETTING \
76 (PROCBASED_INT_WINDOW_EXITING | \
77 PROCBASED_NMI_WINDOW_EXITING)
79 #define PROCBASED_CTLS_ONE_SETTING \
80 (PROCBASED_SECONDARY_CONTROLS | \
81 PROCBASED_IO_EXITING | \
82 PROCBASED_MSR_BITMAPS | \
83 PROCBASED_CTLS_WINDOW_SETTING)
84 #define PROCBASED_CTLS_ZERO_SETTING \
85 (PROCBASED_CR3_LOAD_EXITING | \
86 PROCBASED_CR3_STORE_EXITING | \
89 #define PROCBASED_CTLS2_ONE_SETTING PROCBASED2_ENABLE_EPT
90 #define PROCBASED_CTLS2_ZERO_SETTING 0
92 #define VM_EXIT_CTLS_ONE_SETTING_NO_PAT \
97 #define VM_EXIT_CTLS_ONE_SETTING \
98 (VM_EXIT_CTLS_ONE_SETTING_NO_PAT | \
99 VM_EXIT_ACKNOWLEDGE_INTERRUPT | \
102 #define VM_EXIT_CTLS_ZERO_SETTING VM_EXIT_SAVE_DEBUG_CONTROLS
104 #define VM_ENTRY_CTLS_ONE_SETTING_NO_PAT VM_ENTRY_LOAD_EFER
106 #define VM_ENTRY_CTLS_ONE_SETTING \
107 (VM_ENTRY_CTLS_ONE_SETTING_NO_PAT | \
109 #define VM_ENTRY_CTLS_ZERO_SETTING \
110 (VM_ENTRY_LOAD_DEBUG_CONTROLS | \
111 VM_ENTRY_INTO_SMM | \
112 VM_ENTRY_DEACTIVATE_DUAL_MONITOR)
114 #define guest_msr_rw(vmx, msr) \
115 msr_bitmap_change_access((vmx)->msr_bitmap, (msr), MSR_BITMAP_ACCESS_RW)
117 #define guest_msr_ro(vmx, msr) \
118 msr_bitmap_change_access((vmx)->msr_bitmap, (msr), MSR_BITMAP_ACCESS_READ)
123 static MALLOC_DEFINE(M_VMX, "vmx", "vmx");
124 static MALLOC_DEFINE(M_VLAPIC, "vlapic", "vlapic");
126 SYSCTL_DECL(_hw_vmm);
127 SYSCTL_NODE(_hw_vmm, OID_AUTO, vmx, CTLFLAG_RW, NULL, NULL);
129 int vmxon_enabled[MAXCPU];
130 static char vmxon_region[MAXCPU][PAGE_SIZE] __aligned(PAGE_SIZE);
132 static uint32_t pinbased_ctls, procbased_ctls, procbased_ctls2;
133 static uint32_t exit_ctls, entry_ctls;
135 static uint64_t cr0_ones_mask, cr0_zeros_mask;
136 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_ones_mask, CTLFLAG_RD,
137 &cr0_ones_mask, 0, NULL);
138 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_zeros_mask, CTLFLAG_RD,
139 &cr0_zeros_mask, 0, NULL);
141 static uint64_t cr4_ones_mask, cr4_zeros_mask;
142 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_ones_mask, CTLFLAG_RD,
143 &cr4_ones_mask, 0, NULL);
144 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_zeros_mask, CTLFLAG_RD,
145 &cr4_zeros_mask, 0, NULL);
147 static int vmx_no_patmsr;
149 static int vmx_initialized;
150 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, initialized, CTLFLAG_RD,
151 &vmx_initialized, 0, "Intel VMX initialized");
154 * Optional capabilities
156 static int cap_halt_exit;
157 static int cap_pause_exit;
158 static int cap_unrestricted_guest;
159 static int cap_monitor_trap;
160 static int cap_invpcid;
162 static int virtual_interrupt_delivery;
163 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, virtual_interrupt_delivery, CTLFLAG_RD,
164 &virtual_interrupt_delivery, 0, "APICv virtual interrupt delivery support");
166 static int posted_interrupts;
167 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, posted_interrupts, CTLFLAG_RD,
168 &posted_interrupts, 0, "APICv posted interrupt support");
171 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, posted_interrupt_vector, CTLFLAG_RD,
172 &pirvec, 0, "APICv posted interrupt vector");
174 static struct unrhdr *vpid_unr;
175 static u_int vpid_alloc_failed;
176 SYSCTL_UINT(_hw_vmm_vmx, OID_AUTO, vpid_alloc_failed, CTLFLAG_RD,
177 &vpid_alloc_failed, 0, NULL);
180 * Use the last page below 4GB as the APIC access address. This address is
181 * occupied by the boot firmware so it is guaranteed that it will not conflict
182 * with a page in system memory.
184 #define APIC_ACCESS_ADDRESS 0xFFFFF000
186 static void vmx_inject_pir(struct vlapic *vlapic);
190 exit_reason_to_str(int reason)
192 static char reasonbuf[32];
195 case EXIT_REASON_EXCEPTION:
197 case EXIT_REASON_EXT_INTR:
199 case EXIT_REASON_TRIPLE_FAULT:
200 return "triplefault";
201 case EXIT_REASON_INIT:
203 case EXIT_REASON_SIPI:
205 case EXIT_REASON_IO_SMI:
207 case EXIT_REASON_SMI:
209 case EXIT_REASON_INTR_WINDOW:
211 case EXIT_REASON_NMI_WINDOW:
213 case EXIT_REASON_TASK_SWITCH:
215 case EXIT_REASON_CPUID:
217 case EXIT_REASON_GETSEC:
219 case EXIT_REASON_HLT:
221 case EXIT_REASON_INVD:
223 case EXIT_REASON_INVLPG:
225 case EXIT_REASON_RDPMC:
227 case EXIT_REASON_RDTSC:
229 case EXIT_REASON_RSM:
231 case EXIT_REASON_VMCALL:
233 case EXIT_REASON_VMCLEAR:
235 case EXIT_REASON_VMLAUNCH:
237 case EXIT_REASON_VMPTRLD:
239 case EXIT_REASON_VMPTRST:
241 case EXIT_REASON_VMREAD:
243 case EXIT_REASON_VMRESUME:
245 case EXIT_REASON_VMWRITE:
247 case EXIT_REASON_VMXOFF:
249 case EXIT_REASON_VMXON:
251 case EXIT_REASON_CR_ACCESS:
253 case EXIT_REASON_DR_ACCESS:
255 case EXIT_REASON_INOUT:
257 case EXIT_REASON_RDMSR:
259 case EXIT_REASON_WRMSR:
261 case EXIT_REASON_INVAL_VMCS:
263 case EXIT_REASON_INVAL_MSR:
265 case EXIT_REASON_MWAIT:
267 case EXIT_REASON_MTF:
269 case EXIT_REASON_MONITOR:
271 case EXIT_REASON_PAUSE:
273 case EXIT_REASON_MCE:
275 case EXIT_REASON_TPR:
277 case EXIT_REASON_APIC_ACCESS:
278 return "apic-access";
279 case EXIT_REASON_GDTR_IDTR:
281 case EXIT_REASON_LDTR_TR:
283 case EXIT_REASON_EPT_FAULT:
285 case EXIT_REASON_EPT_MISCONFIG:
286 return "eptmisconfig";
287 case EXIT_REASON_INVEPT:
289 case EXIT_REASON_RDTSCP:
291 case EXIT_REASON_VMX_PREEMPT:
293 case EXIT_REASON_INVVPID:
295 case EXIT_REASON_WBINVD:
297 case EXIT_REASON_XSETBV:
299 case EXIT_REASON_APIC_WRITE:
302 snprintf(reasonbuf, sizeof(reasonbuf), "%d", reason);
309 vmx_allow_x2apic_msrs(struct vmx *vmx)
316 * Allow readonly access to the following x2APIC MSRs from the guest.
318 error += guest_msr_ro(vmx, MSR_APIC_ID);
319 error += guest_msr_ro(vmx, MSR_APIC_VERSION);
320 error += guest_msr_ro(vmx, MSR_APIC_LDR);
321 error += guest_msr_ro(vmx, MSR_APIC_SVR);
323 for (i = 0; i < 8; i++)
324 error += guest_msr_ro(vmx, MSR_APIC_ISR0 + i);
326 for (i = 0; i < 8; i++)
327 error += guest_msr_ro(vmx, MSR_APIC_TMR0 + i);
329 for (i = 0; i < 8; i++)
330 error += guest_msr_ro(vmx, MSR_APIC_IRR0 + i);
332 error += guest_msr_ro(vmx, MSR_APIC_ESR);
333 error += guest_msr_ro(vmx, MSR_APIC_LVT_TIMER);
334 error += guest_msr_ro(vmx, MSR_APIC_LVT_THERMAL);
335 error += guest_msr_ro(vmx, MSR_APIC_LVT_PCINT);
336 error += guest_msr_ro(vmx, MSR_APIC_LVT_LINT0);
337 error += guest_msr_ro(vmx, MSR_APIC_LVT_LINT1);
338 error += guest_msr_ro(vmx, MSR_APIC_LVT_ERROR);
339 error += guest_msr_ro(vmx, MSR_APIC_ICR_TIMER);
340 error += guest_msr_ro(vmx, MSR_APIC_DCR_TIMER);
341 error += guest_msr_ro(vmx, MSR_APIC_ICR);
344 * Allow TPR, EOI and SELF_IPI MSRs to be read and written by the guest.
346 * These registers get special treatment described in the section
347 * "Virtualizing MSR-Based APIC Accesses".
349 error += guest_msr_rw(vmx, MSR_APIC_TPR);
350 error += guest_msr_rw(vmx, MSR_APIC_EOI);
351 error += guest_msr_rw(vmx, MSR_APIC_SELF_IPI);
357 vmx_fix_cr0(u_long cr0)
360 return ((cr0 | cr0_ones_mask) & ~cr0_zeros_mask);
364 vmx_fix_cr4(u_long cr4)
367 return ((cr4 | cr4_ones_mask) & ~cr4_zeros_mask);
373 if (vpid < 0 || vpid > 0xffff)
374 panic("vpid_free: invalid vpid %d", vpid);
377 * VPIDs [0,VM_MAXCPU] are special and are not allocated from
378 * the unit number allocator.
381 if (vpid > VM_MAXCPU)
382 free_unr(vpid_unr, vpid);
386 vpid_alloc(uint16_t *vpid, int num)
390 if (num <= 0 || num > VM_MAXCPU)
391 panic("invalid number of vpids requested: %d", num);
394 * If the "enable vpid" execution control is not enabled then the
395 * VPID is required to be 0 for all vcpus.
397 if ((procbased_ctls2 & PROCBASED2_ENABLE_VPID) == 0) {
398 for (i = 0; i < num; i++)
404 * Allocate a unique VPID for each vcpu from the unit number allocator.
406 for (i = 0; i < num; i++) {
407 x = alloc_unr(vpid_unr);
415 atomic_add_int(&vpid_alloc_failed, 1);
418 * If the unit number allocator does not have enough unique
419 * VPIDs then we need to allocate from the [1,VM_MAXCPU] range.
421 * These VPIDs are not be unique across VMs but this does not
422 * affect correctness because the combined mappings are also
423 * tagged with the EP4TA which is unique for each VM.
425 * It is still sub-optimal because the invvpid will invalidate
426 * combined mappings for a particular VPID across all EP4TAs.
431 for (i = 0; i < num; i++)
440 * VPID 0 is required when the "enable VPID" execution control is
443 * VPIDs [1,VM_MAXCPU] are used as the "overflow namespace" when the
444 * unit number allocator does not have sufficient unique VPIDs to
445 * satisfy the allocation.
447 * The remaining VPIDs are managed by the unit number allocator.
449 vpid_unr = new_unrhdr(VM_MAXCPU + 1, 0xffff, NULL);
453 msr_save_area_init(struct msr_entry *g_area, int *g_count)
457 static struct msr_entry guest_msrs[] = {
458 { MSR_KGSBASE, 0, 0 },
461 cnt = sizeof(guest_msrs) / sizeof(guest_msrs[0]);
462 if (cnt > GUEST_MSR_MAX_ENTRIES)
463 panic("guest msr save area overrun");
464 bcopy(guest_msrs, g_area, sizeof(guest_msrs));
469 vmx_disable(void *arg __unused)
471 struct invvpid_desc invvpid_desc = { 0 };
472 struct invept_desc invept_desc = { 0 };
474 if (vmxon_enabled[curcpu]) {
476 * See sections 25.3.3.3 and 25.3.3.4 in Intel Vol 3b.
478 * VMXON or VMXOFF are not required to invalidate any TLB
479 * caching structures. This prevents potential retention of
480 * cached information in the TLB between distinct VMX episodes.
482 invvpid(INVVPID_TYPE_ALL_CONTEXTS, invvpid_desc);
483 invept(INVEPT_TYPE_ALL_CONTEXTS, invept_desc);
486 load_cr4(rcr4() & ~CR4_VMXE);
494 vmm_ipi_free(pirvec);
496 if (vpid_unr != NULL) {
497 delete_unrhdr(vpid_unr);
501 smp_rendezvous(NULL, vmx_disable, NULL, NULL);
507 vmx_enable(void *arg __unused)
511 load_cr4(rcr4() | CR4_VMXE);
513 *(uint32_t *)vmxon_region[curcpu] = vmx_revision();
514 error = vmxon(vmxon_region[curcpu]);
516 vmxon_enabled[curcpu] = 1;
523 if (vmxon_enabled[curcpu])
524 vmxon(vmxon_region[curcpu]);
530 int error, use_tpr_shadow;
531 uint64_t fixed0, fixed1, feature_control;
532 uint32_t tmp, procbased2_vid_bits;
534 /* CPUID.1:ECX[bit 5] must be 1 for processor to support VMX */
535 if (!(cpu_feature2 & CPUID2_VMX)) {
536 printf("vmx_init: processor does not support VMX operation\n");
541 * Verify that MSR_IA32_FEATURE_CONTROL lock and VMXON enable bits
542 * are set (bits 0 and 2 respectively).
544 feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
545 if ((feature_control & IA32_FEATURE_CONTROL_LOCK) == 0 ||
546 (feature_control & IA32_FEATURE_CONTROL_VMX_EN) == 0) {
547 printf("vmx_init: VMX operation disabled by BIOS\n");
551 /* Check support for primary processor-based VM-execution controls */
552 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
553 MSR_VMX_TRUE_PROCBASED_CTLS,
554 PROCBASED_CTLS_ONE_SETTING,
555 PROCBASED_CTLS_ZERO_SETTING, &procbased_ctls);
557 printf("vmx_init: processor does not support desired primary "
558 "processor-based controls\n");
562 /* Clear the processor-based ctl bits that are set on demand */
563 procbased_ctls &= ~PROCBASED_CTLS_WINDOW_SETTING;
565 /* Check support for secondary processor-based VM-execution controls */
566 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
567 MSR_VMX_PROCBASED_CTLS2,
568 PROCBASED_CTLS2_ONE_SETTING,
569 PROCBASED_CTLS2_ZERO_SETTING, &procbased_ctls2);
571 printf("vmx_init: processor does not support desired secondary "
572 "processor-based controls\n");
576 /* Check support for VPID */
577 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2,
578 PROCBASED2_ENABLE_VPID, 0, &tmp);
580 procbased_ctls2 |= PROCBASED2_ENABLE_VPID;
582 /* Check support for pin-based VM-execution controls */
583 error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS,
584 MSR_VMX_TRUE_PINBASED_CTLS,
585 PINBASED_CTLS_ONE_SETTING,
586 PINBASED_CTLS_ZERO_SETTING, &pinbased_ctls);
588 printf("vmx_init: processor does not support desired "
589 "pin-based controls\n");
593 /* Check support for VM-exit controls */
594 error = vmx_set_ctlreg(MSR_VMX_EXIT_CTLS, MSR_VMX_TRUE_EXIT_CTLS,
595 VM_EXIT_CTLS_ONE_SETTING,
596 VM_EXIT_CTLS_ZERO_SETTING,
599 /* Try again without the PAT MSR bits */
600 error = vmx_set_ctlreg(MSR_VMX_EXIT_CTLS,
601 MSR_VMX_TRUE_EXIT_CTLS,
602 VM_EXIT_CTLS_ONE_SETTING_NO_PAT,
603 VM_EXIT_CTLS_ZERO_SETTING,
606 printf("vmx_init: processor does not support desired "
611 printf("vmm: PAT MSR access not supported\n");
612 guest_msr_valid(MSR_PAT);
617 /* Check support for VM-entry controls */
618 if (!vmx_no_patmsr) {
619 error = vmx_set_ctlreg(MSR_VMX_ENTRY_CTLS,
620 MSR_VMX_TRUE_ENTRY_CTLS,
621 VM_ENTRY_CTLS_ONE_SETTING,
622 VM_ENTRY_CTLS_ZERO_SETTING,
625 error = vmx_set_ctlreg(MSR_VMX_ENTRY_CTLS,
626 MSR_VMX_TRUE_ENTRY_CTLS,
627 VM_ENTRY_CTLS_ONE_SETTING_NO_PAT,
628 VM_ENTRY_CTLS_ZERO_SETTING,
633 printf("vmx_init: processor does not support desired "
639 * Check support for optional features by testing them
642 cap_halt_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
643 MSR_VMX_TRUE_PROCBASED_CTLS,
644 PROCBASED_HLT_EXITING, 0,
647 cap_monitor_trap = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
648 MSR_VMX_PROCBASED_CTLS,
652 cap_pause_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
653 MSR_VMX_TRUE_PROCBASED_CTLS,
654 PROCBASED_PAUSE_EXITING, 0,
657 cap_unrestricted_guest = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
658 MSR_VMX_PROCBASED_CTLS2,
659 PROCBASED2_UNRESTRICTED_GUEST, 0,
662 cap_invpcid = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
663 MSR_VMX_PROCBASED_CTLS2, PROCBASED2_ENABLE_INVPCID, 0,
667 * Check support for virtual interrupt delivery.
669 procbased2_vid_bits = (PROCBASED2_VIRTUALIZE_APIC_ACCESSES |
670 PROCBASED2_VIRTUALIZE_X2APIC_MODE |
671 PROCBASED2_APIC_REGISTER_VIRTUALIZATION |
672 PROCBASED2_VIRTUAL_INTERRUPT_DELIVERY);
674 use_tpr_shadow = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
675 MSR_VMX_TRUE_PROCBASED_CTLS, PROCBASED_USE_TPR_SHADOW, 0,
678 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2,
679 procbased2_vid_bits, 0, &tmp);
680 if (error == 0 && use_tpr_shadow) {
681 virtual_interrupt_delivery = 1;
682 TUNABLE_INT_FETCH("hw.vmm.vmx.use_apic_vid",
683 &virtual_interrupt_delivery);
686 if (virtual_interrupt_delivery) {
687 procbased_ctls |= PROCBASED_USE_TPR_SHADOW;
688 procbased_ctls2 |= procbased2_vid_bits;
689 procbased_ctls2 &= ~PROCBASED2_VIRTUALIZE_X2APIC_MODE;
692 * Check for Posted Interrupts only if Virtual Interrupt
693 * Delivery is enabled.
695 error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS,
696 MSR_VMX_TRUE_PINBASED_CTLS, PINBASED_POSTED_INTERRUPT, 0,
699 pirvec = vmm_ipi_alloc();
702 printf("vmx_init: unable to allocate "
703 "posted interrupt vector\n");
706 posted_interrupts = 1;
707 TUNABLE_INT_FETCH("hw.vmm.vmx.use_apic_pir",
713 if (posted_interrupts)
714 pinbased_ctls |= PINBASED_POSTED_INTERRUPT;
717 error = ept_init(ipinum);
719 printf("vmx_init: ept initialization failed (%d)\n", error);
724 * Stash the cr0 and cr4 bits that must be fixed to 0 or 1
726 fixed0 = rdmsr(MSR_VMX_CR0_FIXED0);
727 fixed1 = rdmsr(MSR_VMX_CR0_FIXED1);
728 cr0_ones_mask = fixed0 & fixed1;
729 cr0_zeros_mask = ~fixed0 & ~fixed1;
732 * CR0_PE and CR0_PG can be set to zero in VMX non-root operation
733 * if unrestricted guest execution is allowed.
735 if (cap_unrestricted_guest)
736 cr0_ones_mask &= ~(CR0_PG | CR0_PE);
739 * Do not allow the guest to set CR0_NW or CR0_CD.
741 cr0_zeros_mask |= (CR0_NW | CR0_CD);
743 fixed0 = rdmsr(MSR_VMX_CR4_FIXED0);
744 fixed1 = rdmsr(MSR_VMX_CR4_FIXED1);
745 cr4_ones_mask = fixed0 & fixed1;
746 cr4_zeros_mask = ~fixed0 & ~fixed1;
750 /* enable VMX operation */
751 smp_rendezvous(NULL, vmx_enable, NULL, NULL);
759 vmx_trigger_hostintr(int vector)
762 struct gate_descriptor *gd;
766 KASSERT(vector >= 32 && vector <= 255, ("vmx_trigger_hostintr: "
767 "invalid vector %d", vector));
768 KASSERT(gd->gd_p == 1, ("gate descriptor for vector %d not present",
770 KASSERT(gd->gd_type == SDT_SYSIGT, ("gate descriptor for vector %d "
771 "has invalid type %d", vector, gd->gd_type));
772 KASSERT(gd->gd_dpl == SEL_KPL, ("gate descriptor for vector %d "
773 "has invalid dpl %d", vector, gd->gd_dpl));
774 KASSERT(gd->gd_selector == GSEL(GCODE_SEL, SEL_KPL), ("gate descriptor "
775 "for vector %d has invalid selector %d", vector, gd->gd_selector));
776 KASSERT(gd->gd_ist == 0, ("gate descriptor for vector %d has invalid "
777 "IST %d", vector, gd->gd_ist));
779 func = ((long)gd->gd_hioffset << 16 | gd->gd_looffset);
784 vmx_setup_cr_shadow(int which, struct vmcs *vmcs, uint32_t initial)
786 int error, mask_ident, shadow_ident;
789 if (which != 0 && which != 4)
790 panic("vmx_setup_cr_shadow: unknown cr%d", which);
793 mask_ident = VMCS_CR0_MASK;
794 mask_value = cr0_ones_mask | cr0_zeros_mask;
795 shadow_ident = VMCS_CR0_SHADOW;
797 mask_ident = VMCS_CR4_MASK;
798 mask_value = cr4_ones_mask | cr4_zeros_mask;
799 shadow_ident = VMCS_CR4_SHADOW;
802 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(mask_ident), mask_value);
806 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(shadow_ident), initial);
812 #define vmx_setup_cr0_shadow(vmcs,init) vmx_setup_cr_shadow(0, (vmcs), (init))
813 #define vmx_setup_cr4_shadow(vmcs,init) vmx_setup_cr_shadow(4, (vmcs), (init))
816 vmx_vminit(struct vm *vm, pmap_t pmap)
818 uint16_t vpid[VM_MAXCPU];
819 int i, error, guest_msr_count;
823 vmx = malloc(sizeof(struct vmx), M_VMX, M_WAITOK | M_ZERO);
824 if ((uintptr_t)vmx & PAGE_MASK) {
825 panic("malloc of struct vmx not aligned on %d byte boundary",
830 vmx->eptp = eptp(vtophys((vm_offset_t)pmap->pm_pml4));
833 * Clean up EPTP-tagged guest physical and combined mappings
835 * VMX transitions are not required to invalidate any guest physical
836 * mappings. So, it may be possible for stale guest physical mappings
837 * to be present in the processor TLBs.
839 * Combined mappings for this EP4TA are also invalidated for all VPIDs.
841 ept_invalidate_mappings(vmx->eptp);
843 msr_bitmap_initialize(vmx->msr_bitmap);
846 * It is safe to allow direct access to MSR_GSBASE and MSR_FSBASE.
847 * The guest FSBASE and GSBASE are saved and restored during
848 * vm-exit and vm-entry respectively. The host FSBASE and GSBASE are
849 * always restored from the vmcs host state area on vm-exit.
851 * The SYSENTER_CS/ESP/EIP MSRs are identical to FS/GSBASE in
852 * how they are saved/restored so can be directly accessed by the
855 * Guest KGSBASE is saved and restored in the guest MSR save area.
856 * Host KGSBASE is restored before returning to userland from the pcb.
857 * There will be a window of time when we are executing in the host
858 * kernel context with a value of KGSBASE from the guest. This is ok
859 * because the value of KGSBASE is inconsequential in kernel context.
861 * MSR_EFER is saved and restored in the guest VMCS area on a
862 * VM exit and entry respectively. It is also restored from the
863 * host VMCS area on a VM exit.
865 if (guest_msr_rw(vmx, MSR_GSBASE) ||
866 guest_msr_rw(vmx, MSR_FSBASE) ||
867 guest_msr_rw(vmx, MSR_SYSENTER_CS_MSR) ||
868 guest_msr_rw(vmx, MSR_SYSENTER_ESP_MSR) ||
869 guest_msr_rw(vmx, MSR_SYSENTER_EIP_MSR) ||
870 guest_msr_rw(vmx, MSR_KGSBASE) ||
871 guest_msr_rw(vmx, MSR_EFER))
872 panic("vmx_vminit: error setting guest msr access");
875 * MSR_PAT is saved and restored in the guest VMCS are on a VM exit
876 * and entry respectively. It is also restored from the host VMCS
877 * area on a VM exit. However, if running on a system with no
878 * MSR_PAT save/restore support, leave access disabled so accesses
881 if (!vmx_no_patmsr && guest_msr_rw(vmx, MSR_PAT))
882 panic("vmx_vminit: error setting guest pat msr access");
884 vpid_alloc(vpid, VM_MAXCPU);
886 if (virtual_interrupt_delivery) {
887 error = vm_map_mmio(vm, DEFAULT_APIC_BASE, PAGE_SIZE,
888 APIC_ACCESS_ADDRESS);
889 /* XXX this should really return an error to the caller */
890 KASSERT(error == 0, ("vm_map_mmio(apicbase) error %d", error));
893 for (i = 0; i < VM_MAXCPU; i++) {
894 vmcs = &vmx->vmcs[i];
895 vmcs->identifier = vmx_revision();
896 error = vmclear(vmcs);
898 panic("vmx_vminit: vmclear error %d on vcpu %d\n",
902 error = vmcs_init(vmcs);
903 KASSERT(error == 0, ("vmcs_init error %d", error));
907 error += vmwrite(VMCS_HOST_RSP, (u_long)&vmx->ctx[i]);
908 error += vmwrite(VMCS_EPTP, vmx->eptp);
909 error += vmwrite(VMCS_PIN_BASED_CTLS, pinbased_ctls);
910 error += vmwrite(VMCS_PRI_PROC_BASED_CTLS, procbased_ctls);
911 error += vmwrite(VMCS_SEC_PROC_BASED_CTLS, procbased_ctls2);
912 error += vmwrite(VMCS_EXIT_CTLS, exit_ctls);
913 error += vmwrite(VMCS_ENTRY_CTLS, entry_ctls);
914 error += vmwrite(VMCS_MSR_BITMAP, vtophys(vmx->msr_bitmap));
915 error += vmwrite(VMCS_VPID, vpid[i]);
916 if (virtual_interrupt_delivery) {
917 error += vmwrite(VMCS_APIC_ACCESS, APIC_ACCESS_ADDRESS);
918 error += vmwrite(VMCS_VIRTUAL_APIC,
919 vtophys(&vmx->apic_page[i]));
920 error += vmwrite(VMCS_EOI_EXIT0, 0);
921 error += vmwrite(VMCS_EOI_EXIT1, 0);
922 error += vmwrite(VMCS_EOI_EXIT2, 0);
923 error += vmwrite(VMCS_EOI_EXIT3, 0);
925 if (posted_interrupts) {
926 error += vmwrite(VMCS_PIR_VECTOR, pirvec);
927 error += vmwrite(VMCS_PIR_DESC,
928 vtophys(&vmx->pir_desc[i]));
931 KASSERT(error == 0, ("vmx_vminit: error customizing the vmcs"));
934 vmx->cap[i].proc_ctls = procbased_ctls;
935 vmx->cap[i].proc_ctls2 = procbased_ctls2;
937 vmx->state[i].lastcpu = -1;
938 vmx->state[i].vpid = vpid[i];
940 msr_save_area_init(vmx->guest_msrs[i], &guest_msr_count);
942 error = vmcs_set_msr_save(vmcs, vtophys(vmx->guest_msrs[i]),
945 panic("vmcs_set_msr_save error %d", error);
948 * Set up the CR0/4 shadows, and init the read shadow
949 * to the power-on register value from the Intel Sys Arch.
953 error = vmx_setup_cr0_shadow(vmcs, 0x60000010);
955 panic("vmx_setup_cr0_shadow %d", error);
957 error = vmx_setup_cr4_shadow(vmcs, 0);
959 panic("vmx_setup_cr4_shadow %d", error);
961 vmx->ctx[i].pmap = pmap;
968 vmx_handle_cpuid(struct vm *vm, int vcpu, struct vmxctx *vmxctx)
972 func = vmxctx->guest_rax;
974 handled = x86_emulate_cpuid(vm, vcpu,
975 (uint32_t*)(&vmxctx->guest_rax),
976 (uint32_t*)(&vmxctx->guest_rbx),
977 (uint32_t*)(&vmxctx->guest_rcx),
978 (uint32_t*)(&vmxctx->guest_rdx));
983 vmx_run_trace(struct vmx *vmx, int vcpu)
986 VCPU_CTR1(vmx->vm, vcpu, "Resume execution at %#lx", vmcs_guest_rip());
991 vmx_exit_trace(struct vmx *vmx, int vcpu, uint64_t rip, uint32_t exit_reason,
995 VCPU_CTR3(vmx->vm, vcpu, "%s %s vmexit at 0x%0lx",
996 handled ? "handled" : "unhandled",
997 exit_reason_to_str(exit_reason), rip);
1001 static __inline void
1002 vmx_astpending_trace(struct vmx *vmx, int vcpu, uint64_t rip)
1005 VCPU_CTR1(vmx->vm, vcpu, "astpending vmexit at 0x%0lx", rip);
1009 static VMM_STAT_INTEL(VCPU_INVVPID_SAVED, "Number of vpid invalidations saved");
1012 vmx_set_pcpu_defaults(struct vmx *vmx, int vcpu, pmap_t pmap)
1014 struct vmxstate *vmxstate;
1015 struct invvpid_desc invvpid_desc;
1017 vmxstate = &vmx->state[vcpu];
1018 if (vmxstate->lastcpu == curcpu)
1021 vmxstate->lastcpu = curcpu;
1023 vmm_stat_incr(vmx->vm, vcpu, VCPU_MIGRATIONS, 1);
1025 vmcs_write(VMCS_HOST_TR_BASE, vmm_get_host_trbase());
1026 vmcs_write(VMCS_HOST_GDTR_BASE, vmm_get_host_gdtrbase());
1027 vmcs_write(VMCS_HOST_GS_BASE, vmm_get_host_gsbase());
1030 * If we are using VPIDs then invalidate all mappings tagged with 'vpid'
1032 * We do this because this vcpu was executing on a different host
1033 * cpu when it last ran. We do not track whether it invalidated
1034 * mappings associated with its 'vpid' during that run. So we must
1035 * assume that the mappings associated with 'vpid' on 'curcpu' are
1036 * stale and invalidate them.
1038 * Note that we incur this penalty only when the scheduler chooses to
1039 * move the thread associated with this vcpu between host cpus.
1041 * Note also that this will invalidate mappings tagged with 'vpid'
1044 if (vmxstate->vpid != 0) {
1045 if (pmap->pm_eptgen == vmx->eptgen[curcpu]) {
1046 invvpid_desc._res1 = 0;
1047 invvpid_desc._res2 = 0;
1048 invvpid_desc.vpid = vmxstate->vpid;
1049 invvpid(INVVPID_TYPE_SINGLE_CONTEXT, invvpid_desc);
1052 * The invvpid can be skipped if an invept is going to
1053 * be performed before entering the guest. The invept
1054 * will invalidate combined mappings tagged with
1055 * 'vmx->eptp' for all vpids.
1057 vmm_stat_incr(vmx->vm, vcpu, VCPU_INVVPID_SAVED, 1);
1063 * We depend on 'procbased_ctls' to have the Interrupt Window Exiting bit set.
1065 CTASSERT((PROCBASED_CTLS_ONE_SETTING & PROCBASED_INT_WINDOW_EXITING) != 0);
1067 static void __inline
1068 vmx_set_int_window_exiting(struct vmx *vmx, int vcpu)
1071 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) == 0) {
1072 vmx->cap[vcpu].proc_ctls |= PROCBASED_INT_WINDOW_EXITING;
1073 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1074 VCPU_CTR0(vmx->vm, vcpu, "Enabling interrupt window exiting");
1078 static void __inline
1079 vmx_clear_int_window_exiting(struct vmx *vmx, int vcpu)
1082 KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0,
1083 ("intr_window_exiting not set: %#x", vmx->cap[vcpu].proc_ctls));
1084 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_INT_WINDOW_EXITING;
1085 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1086 VCPU_CTR0(vmx->vm, vcpu, "Disabling interrupt window exiting");
1089 static void __inline
1090 vmx_set_nmi_window_exiting(struct vmx *vmx, int vcpu)
1093 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) == 0) {
1094 vmx->cap[vcpu].proc_ctls |= PROCBASED_NMI_WINDOW_EXITING;
1095 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1096 VCPU_CTR0(vmx->vm, vcpu, "Enabling NMI window exiting");
1100 static void __inline
1101 vmx_clear_nmi_window_exiting(struct vmx *vmx, int vcpu)
1104 KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) != 0,
1105 ("nmi_window_exiting not set %#x", vmx->cap[vcpu].proc_ctls));
1106 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_NMI_WINDOW_EXITING;
1107 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1108 VCPU_CTR0(vmx->vm, vcpu, "Disabling NMI window exiting");
1111 #define NMI_BLOCKING (VMCS_INTERRUPTIBILITY_NMI_BLOCKING | \
1112 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)
1113 #define HWINTR_BLOCKING (VMCS_INTERRUPTIBILITY_STI_BLOCKING | \
1114 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)
1117 vmx_inject_nmi(struct vmx *vmx, int vcpu)
1121 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1122 KASSERT((gi & NMI_BLOCKING) == 0, ("vmx_inject_nmi: invalid guest "
1123 "interruptibility-state %#x", gi));
1125 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1126 KASSERT((info & VMCS_INTR_VALID) == 0, ("vmx_inject_nmi: invalid "
1127 "VM-entry interruption information %#x", info));
1130 * Inject the virtual NMI. The vector must be the NMI IDT entry
1131 * or the VMCS entry check will fail.
1133 info = IDT_NMI | VMCS_INTR_T_NMI | VMCS_INTR_VALID;
1134 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1136 VCPU_CTR0(vmx->vm, vcpu, "Injecting vNMI");
1138 /* Clear the request */
1139 vm_nmi_clear(vmx->vm, vcpu);
1143 vmx_inject_interrupts(struct vmx *vmx, int vcpu, struct vlapic *vlapic)
1145 struct vm_exception exc;
1146 int vector, need_nmi_exiting;
1150 if (vm_exception_pending(vmx->vm, vcpu, &exc)) {
1151 KASSERT(exc.vector >= 0 && exc.vector < 32,
1152 ("%s: invalid exception vector %d", __func__, exc.vector));
1154 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1155 KASSERT((info & VMCS_INTR_VALID) == 0, ("%s: cannot inject "
1156 "pending exception %d: %#x", __func__, exc.vector, info));
1158 info = exc.vector | VMCS_INTR_T_HWEXCEPTION | VMCS_INTR_VALID;
1159 if (exc.error_code_valid) {
1160 info |= VMCS_INTR_DEL_ERRCODE;
1161 vmcs_write(VMCS_ENTRY_EXCEPTION_ERROR, exc.error_code);
1163 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1166 if (vm_nmi_pending(vmx->vm, vcpu)) {
1168 * If there are no conditions blocking NMI injection then
1169 * inject it directly here otherwise enable "NMI window
1170 * exiting" to inject it as soon as we can.
1172 * We also check for STI_BLOCKING because some implementations
1173 * don't allow NMI injection in this case. If we are running
1174 * on a processor that doesn't have this restriction it will
1175 * immediately exit and the NMI will be injected in the
1176 * "NMI window exiting" handler.
1178 need_nmi_exiting = 1;
1179 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1180 if ((gi & (HWINTR_BLOCKING | NMI_BLOCKING)) == 0) {
1181 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1182 if ((info & VMCS_INTR_VALID) == 0) {
1183 vmx_inject_nmi(vmx, vcpu);
1184 need_nmi_exiting = 0;
1186 VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI "
1187 "due to VM-entry intr info %#x", info);
1190 VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI due to "
1191 "Guest Interruptibility-state %#x", gi);
1194 if (need_nmi_exiting)
1195 vmx_set_nmi_window_exiting(vmx, vcpu);
1198 if (virtual_interrupt_delivery) {
1199 vmx_inject_pir(vlapic);
1204 * If interrupt-window exiting is already in effect then don't bother
1205 * checking for pending interrupts. This is just an optimization and
1206 * not needed for correctness.
1208 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0) {
1209 VCPU_CTR0(vmx->vm, vcpu, "Skip interrupt injection due to "
1210 "pending int_window_exiting");
1214 /* Ask the local apic for a vector to inject */
1215 if (!vlapic_pending_intr(vlapic, &vector))
1218 KASSERT(vector >= 32 && vector <= 255, ("invalid vector %d", vector));
1220 /* Check RFLAGS.IF and the interruptibility state of the guest */
1221 rflags = vmcs_read(VMCS_GUEST_RFLAGS);
1222 if ((rflags & PSL_I) == 0) {
1223 VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1224 "rflags %#lx", vector, rflags);
1228 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1229 if (gi & HWINTR_BLOCKING) {
1230 VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1231 "Guest Interruptibility-state %#x", vector, gi);
1235 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1236 if (info & VMCS_INTR_VALID) {
1238 * This is expected and could happen for multiple reasons:
1239 * - A vectoring VM-entry was aborted due to astpending
1240 * - A VM-exit happened during event injection.
1241 * - An exception was injected above.
1242 * - An NMI was injected above or after "NMI window exiting"
1244 VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1245 "VM-entry intr info %#x", vector, info);
1249 /* Inject the interrupt */
1250 info = VMCS_INTR_T_HWINTR | VMCS_INTR_VALID;
1252 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1254 /* Update the Local APIC ISR */
1255 vlapic_intr_accepted(vlapic, vector);
1257 VCPU_CTR1(vmx->vm, vcpu, "Injecting hwintr at vector %d", vector);
1263 * Set the Interrupt Window Exiting execution control so we can inject
1264 * the interrupt as soon as blocking condition goes away.
1266 vmx_set_int_window_exiting(vmx, vcpu);
1270 * If the Virtual NMIs execution control is '1' then the logical processor
1271 * tracks virtual-NMI blocking in the Guest Interruptibility-state field of
1272 * the VMCS. An IRET instruction in VMX non-root operation will remove any
1273 * virtual-NMI blocking.
1275 * This unblocking occurs even if the IRET causes a fault. In this case the
1276 * hypervisor needs to restore virtual-NMI blocking before resuming the guest.
1279 vmx_restore_nmi_blocking(struct vmx *vmx, int vcpuid)
1283 VCPU_CTR0(vmx->vm, vcpuid, "Restore Virtual-NMI blocking");
1284 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1285 gi |= VMCS_INTERRUPTIBILITY_NMI_BLOCKING;
1286 vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1290 vmx_clear_nmi_blocking(struct vmx *vmx, int vcpuid)
1294 VCPU_CTR0(vmx->vm, vcpuid, "Clear Virtual-NMI blocking");
1295 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1296 gi &= ~VMCS_INTERRUPTIBILITY_NMI_BLOCKING;
1297 vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1301 vmx_emulate_xsetbv(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
1303 struct vmxctx *vmxctx;
1305 const struct xsave_limits *limits;
1307 vmxctx = &vmx->ctx[vcpu];
1308 limits = vmm_get_xsave_limits();
1311 * Note that the processor raises a GP# fault on its own if
1312 * xsetbv is executed for CPL != 0, so we do not have to
1313 * emulate that fault here.
1316 /* Only xcr0 is supported. */
1317 if (vmxctx->guest_rcx != 0) {
1318 vm_inject_gp(vmx->vm, vcpu);
1322 /* We only handle xcr0 if both the host and guest have XSAVE enabled. */
1323 if (!limits->xsave_enabled || !(vmcs_read(VMCS_GUEST_CR4) & CR4_XSAVE)) {
1324 vm_inject_ud(vmx->vm, vcpu);
1328 xcrval = vmxctx->guest_rdx << 32 | (vmxctx->guest_rax & 0xffffffff);
1329 if ((xcrval & ~limits->xcr0_allowed) != 0) {
1330 vm_inject_gp(vmx->vm, vcpu);
1334 if (!(xcrval & XFEATURE_ENABLED_X87)) {
1335 vm_inject_gp(vmx->vm, vcpu);
1339 if ((xcrval & (XFEATURE_ENABLED_AVX | XFEATURE_ENABLED_SSE)) ==
1340 XFEATURE_ENABLED_AVX) {
1341 vm_inject_gp(vmx->vm, vcpu);
1346 * This runs "inside" vmrun() with the guest's FPU state, so
1347 * modifying xcr0 directly modifies the guest's xcr0, not the
1350 load_xcr(0, xcrval);
1355 vmx_emulate_cr_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1357 int cr, vmcs_guest_cr, vmcs_shadow_cr;
1358 uint64_t crval, regval, ones_mask, zeros_mask;
1359 const struct vmxctx *vmxctx;
1361 /* We only handle mov to %cr0 or %cr4 at this time */
1362 if ((exitqual & 0xf0) != 0x00)
1365 cr = exitqual & 0xf;
1366 if (cr != 0 && cr != 4)
1369 regval = 0; /* silence gcc */
1370 vmxctx = &vmx->ctx[vcpu];
1373 * We must use vmcs_write() directly here because vmcs_setreg() will
1374 * call vmclear(vmcs) as a side-effect which we certainly don't want.
1376 switch ((exitqual >> 8) & 0xf) {
1378 regval = vmxctx->guest_rax;
1381 regval = vmxctx->guest_rcx;
1384 regval = vmxctx->guest_rdx;
1387 regval = vmxctx->guest_rbx;
1390 regval = vmcs_read(VMCS_GUEST_RSP);
1393 regval = vmxctx->guest_rbp;
1396 regval = vmxctx->guest_rsi;
1399 regval = vmxctx->guest_rdi;
1402 regval = vmxctx->guest_r8;
1405 regval = vmxctx->guest_r9;
1408 regval = vmxctx->guest_r10;
1411 regval = vmxctx->guest_r11;
1414 regval = vmxctx->guest_r12;
1417 regval = vmxctx->guest_r13;
1420 regval = vmxctx->guest_r14;
1423 regval = vmxctx->guest_r15;
1428 ones_mask = cr0_ones_mask;
1429 zeros_mask = cr0_zeros_mask;
1430 vmcs_guest_cr = VMCS_GUEST_CR0;
1431 vmcs_shadow_cr = VMCS_CR0_SHADOW;
1433 ones_mask = cr4_ones_mask;
1434 zeros_mask = cr4_zeros_mask;
1435 vmcs_guest_cr = VMCS_GUEST_CR4;
1436 vmcs_shadow_cr = VMCS_CR4_SHADOW;
1438 vmcs_write(vmcs_shadow_cr, regval);
1440 crval = regval | ones_mask;
1441 crval &= ~zeros_mask;
1442 vmcs_write(vmcs_guest_cr, crval);
1444 if (cr == 0 && regval & CR0_PG) {
1445 uint64_t efer, entry_ctls;
1448 * If CR0.PG is 1 and EFER.LME is 1 then EFER.LMA and
1449 * the "IA-32e mode guest" bit in VM-entry control must be
1452 efer = vmcs_read(VMCS_GUEST_IA32_EFER);
1453 if (efer & EFER_LME) {
1455 vmcs_write(VMCS_GUEST_IA32_EFER, efer);
1456 entry_ctls = vmcs_read(VMCS_ENTRY_CTLS);
1457 entry_ctls |= VM_ENTRY_GUEST_LMA;
1458 vmcs_write(VMCS_ENTRY_CTLS, entry_ctls);
1465 static enum vie_cpu_mode
1469 if (vmcs_read(VMCS_GUEST_IA32_EFER) & EFER_LMA)
1470 return (CPU_MODE_64BIT);
1472 return (CPU_MODE_COMPATIBILITY);
1475 static enum vie_paging_mode
1476 vmx_paging_mode(void)
1479 if (!(vmcs_read(VMCS_GUEST_CR0) & CR0_PG))
1480 return (PAGING_MODE_FLAT);
1481 if (!(vmcs_read(VMCS_GUEST_CR4) & CR4_PAE))
1482 return (PAGING_MODE_32);
1483 if (vmcs_read(VMCS_GUEST_IA32_EFER) & EFER_LME)
1484 return (PAGING_MODE_64);
1486 return (PAGING_MODE_PAE);
1490 ept_fault_type(uint64_t ept_qual)
1494 if (ept_qual & EPT_VIOLATION_DATA_WRITE)
1495 fault_type = VM_PROT_WRITE;
1496 else if (ept_qual & EPT_VIOLATION_INST_FETCH)
1497 fault_type = VM_PROT_EXECUTE;
1499 fault_type= VM_PROT_READ;
1501 return (fault_type);
1505 ept_emulation_fault(uint64_t ept_qual)
1509 /* EPT fault on an instruction fetch doesn't make sense here */
1510 if (ept_qual & EPT_VIOLATION_INST_FETCH)
1513 /* EPT fault must be a read fault or a write fault */
1514 read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0;
1515 write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0;
1516 if ((read | write) == 0)
1520 * The EPT violation must have been caused by accessing a
1521 * guest-physical address that is a translation of a guest-linear
1524 if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 ||
1525 (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) {
1533 apic_access_virtualization(struct vmx *vmx, int vcpuid)
1535 uint32_t proc_ctls2;
1537 proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
1538 return ((proc_ctls2 & PROCBASED2_VIRTUALIZE_APIC_ACCESSES) ? 1 : 0);
1542 x2apic_virtualization(struct vmx *vmx, int vcpuid)
1544 uint32_t proc_ctls2;
1546 proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
1547 return ((proc_ctls2 & PROCBASED2_VIRTUALIZE_X2APIC_MODE) ? 1 : 0);
1551 vmx_handle_apic_write(struct vmx *vmx, int vcpuid, struct vlapic *vlapic,
1554 int error, handled, offset;
1555 uint32_t *apic_regs, vector;
1559 offset = APIC_WRITE_OFFSET(qual);
1561 if (!apic_access_virtualization(vmx, vcpuid)) {
1563 * In general there should not be any APIC write VM-exits
1564 * unless APIC-access virtualization is enabled.
1566 * However self-IPI virtualization can legitimately trigger
1567 * an APIC-write VM-exit so treat it specially.
1569 if (x2apic_virtualization(vmx, vcpuid) &&
1570 offset == APIC_OFFSET_SELF_IPI) {
1571 apic_regs = (uint32_t *)(vlapic->apic_page);
1572 vector = apic_regs[APIC_OFFSET_SELF_IPI / 4];
1573 vlapic_self_ipi_handler(vlapic, vector);
1580 case APIC_OFFSET_ID:
1581 vlapic_id_write_handler(vlapic);
1583 case APIC_OFFSET_LDR:
1584 vlapic_ldr_write_handler(vlapic);
1586 case APIC_OFFSET_DFR:
1587 vlapic_dfr_write_handler(vlapic);
1589 case APIC_OFFSET_SVR:
1590 vlapic_svr_write_handler(vlapic);
1592 case APIC_OFFSET_ESR:
1593 vlapic_esr_write_handler(vlapic);
1595 case APIC_OFFSET_ICR_LOW:
1597 error = vlapic_icrlo_write_handler(vlapic, &retu);
1598 if (error != 0 || retu)
1599 handled = UNHANDLED;
1601 case APIC_OFFSET_CMCI_LVT:
1602 case APIC_OFFSET_TIMER_LVT ... APIC_OFFSET_ERROR_LVT:
1603 vlapic_lvt_write_handler(vlapic, offset);
1605 case APIC_OFFSET_TIMER_ICR:
1606 vlapic_icrtmr_write_handler(vlapic);
1608 case APIC_OFFSET_TIMER_DCR:
1609 vlapic_dcr_write_handler(vlapic);
1612 handled = UNHANDLED;
1619 apic_access_fault(struct vmx *vmx, int vcpuid, uint64_t gpa)
1622 if (apic_access_virtualization(vmx, vcpuid) &&
1623 (gpa >= DEFAULT_APIC_BASE && gpa < DEFAULT_APIC_BASE + PAGE_SIZE))
1630 vmx_handle_apic_access(struct vmx *vmx, int vcpuid, struct vm_exit *vmexit)
1633 int access_type, offset, allowed;
1635 if (!apic_access_virtualization(vmx, vcpuid))
1638 qual = vmexit->u.vmx.exit_qualification;
1639 access_type = APIC_ACCESS_TYPE(qual);
1640 offset = APIC_ACCESS_OFFSET(qual);
1643 if (access_type == 0) {
1645 * Read data access to the following registers is expected.
1648 case APIC_OFFSET_APR:
1649 case APIC_OFFSET_PPR:
1650 case APIC_OFFSET_RRR:
1651 case APIC_OFFSET_CMCI_LVT:
1652 case APIC_OFFSET_TIMER_CCR:
1658 } else if (access_type == 1) {
1660 * Write data access to the following registers is expected.
1663 case APIC_OFFSET_VER:
1664 case APIC_OFFSET_APR:
1665 case APIC_OFFSET_PPR:
1666 case APIC_OFFSET_RRR:
1667 case APIC_OFFSET_ISR0 ... APIC_OFFSET_ISR7:
1668 case APIC_OFFSET_TMR0 ... APIC_OFFSET_TMR7:
1669 case APIC_OFFSET_IRR0 ... APIC_OFFSET_IRR7:
1670 case APIC_OFFSET_CMCI_LVT:
1671 case APIC_OFFSET_TIMER_CCR:
1680 vmexit->exitcode = VM_EXITCODE_INST_EMUL;
1681 vmexit->u.inst_emul.gpa = DEFAULT_APIC_BASE + offset;
1682 vmexit->u.inst_emul.gla = VIE_INVALID_GLA;
1683 vmexit->u.inst_emul.cr3 = vmcs_guest_cr3();
1684 vmexit->u.inst_emul.cpu_mode = vmx_cpu_mode();
1685 vmexit->u.inst_emul.paging_mode = vmx_paging_mode();
1689 * Regardless of whether the APIC-access is allowed this handler
1690 * always returns UNHANDLED:
1691 * - if the access is allowed then it is handled by emulating the
1692 * instruction that caused the VM-exit (outside the critical section)
1693 * - if the access is not allowed then it will be converted to an
1694 * exitcode of VM_EXITCODE_VMX and will be dealt with in userland.
1700 vmx_exit_process(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
1703 struct vmxctx *vmxctx;
1704 struct vlapic *vlapic;
1705 uint32_t eax, ecx, edx, idtvec_info, idtvec_err, intr_info, reason;
1709 CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_VIRTUAL_NMI) != 0);
1710 CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_NMI_EXITING) != 0);
1712 handled = UNHANDLED;
1713 vmxctx = &vmx->ctx[vcpu];
1715 qual = vmexit->u.vmx.exit_qualification;
1716 reason = vmexit->u.vmx.exit_reason;
1717 vmexit->exitcode = VM_EXITCODE_BOGUS;
1719 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_COUNT, 1);
1722 * VM exits that could be triggered during event injection on the
1723 * previous VM entry need to be handled specially by re-injecting
1726 * See "Information for VM Exits During Event Delivery" in Intel SDM
1730 case EXIT_REASON_EPT_FAULT:
1731 case EXIT_REASON_EPT_MISCONFIG:
1732 case EXIT_REASON_APIC_ACCESS:
1733 case EXIT_REASON_TASK_SWITCH:
1734 case EXIT_REASON_EXCEPTION:
1735 idtvec_info = vmcs_idt_vectoring_info();
1736 if (idtvec_info & VMCS_IDT_VEC_VALID) {
1737 idtvec_info &= ~(1 << 12); /* clear undefined bit */
1738 vmcs_write(VMCS_ENTRY_INTR_INFO, idtvec_info);
1739 if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) {
1740 idtvec_err = vmcs_idt_vectoring_err();
1741 vmcs_write(VMCS_ENTRY_EXCEPTION_ERROR,
1745 * If 'virtual NMIs' are being used and the VM-exit
1746 * happened while injecting an NMI during the previous
1747 * VM-entry, then clear "blocking by NMI" in the Guest
1748 * Interruptibility-state.
1750 if ((idtvec_info & VMCS_INTR_T_MASK) ==
1752 vmx_clear_nmi_blocking(vmx, vcpu);
1754 vmcs_write(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length);
1762 case EXIT_REASON_CR_ACCESS:
1763 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CR_ACCESS, 1);
1764 handled = vmx_emulate_cr_access(vmx, vcpu, qual);
1766 case EXIT_REASON_RDMSR:
1767 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_RDMSR, 1);
1769 ecx = vmxctx->guest_rcx;
1770 error = emulate_rdmsr(vmx->vm, vcpu, ecx, &retu);
1772 vmexit->exitcode = VM_EXITCODE_RDMSR;
1773 vmexit->u.msr.code = ecx;
1777 /* Return to userspace with a valid exitcode */
1778 KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
1779 ("emulate_wrmsr retu with bogus exitcode"));
1782 case EXIT_REASON_WRMSR:
1783 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_WRMSR, 1);
1785 eax = vmxctx->guest_rax;
1786 ecx = vmxctx->guest_rcx;
1787 edx = vmxctx->guest_rdx;
1788 error = emulate_wrmsr(vmx->vm, vcpu, ecx,
1789 (uint64_t)edx << 32 | eax, &retu);
1791 vmexit->exitcode = VM_EXITCODE_WRMSR;
1792 vmexit->u.msr.code = ecx;
1793 vmexit->u.msr.wval = (uint64_t)edx << 32 | eax;
1797 /* Return to userspace with a valid exitcode */
1798 KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
1799 ("emulate_wrmsr retu with bogus exitcode"));
1802 case EXIT_REASON_HLT:
1803 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_HLT, 1);
1804 vmexit->exitcode = VM_EXITCODE_HLT;
1805 vmexit->u.hlt.rflags = vmcs_read(VMCS_GUEST_RFLAGS);
1807 case EXIT_REASON_MTF:
1808 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_MTRAP, 1);
1809 vmexit->exitcode = VM_EXITCODE_MTRAP;
1811 case EXIT_REASON_PAUSE:
1812 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_PAUSE, 1);
1813 vmexit->exitcode = VM_EXITCODE_PAUSE;
1815 case EXIT_REASON_INTR_WINDOW:
1816 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INTR_WINDOW, 1);
1817 vmx_clear_int_window_exiting(vmx, vcpu);
1819 case EXIT_REASON_EXT_INTR:
1821 * External interrupts serve only to cause VM exits and allow
1822 * the host interrupt handler to run.
1824 * If this external interrupt triggers a virtual interrupt
1825 * to a VM, then that state will be recorded by the
1826 * host interrupt handler in the VM's softc. We will inject
1827 * this virtual interrupt during the subsequent VM enter.
1829 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
1832 * XXX: Ignore this exit if VMCS_INTR_VALID is not set.
1833 * This appears to be a bug in VMware Fusion?
1835 if (!(intr_info & VMCS_INTR_VALID))
1837 KASSERT((intr_info & VMCS_INTR_VALID) != 0 &&
1838 (intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_HWINTR,
1839 ("VM exit interruption info invalid: %#x", intr_info));
1840 vmx_trigger_hostintr(intr_info & 0xff);
1843 * This is special. We want to treat this as an 'handled'
1844 * VM-exit but not increment the instruction pointer.
1846 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXTINT, 1);
1848 case EXIT_REASON_NMI_WINDOW:
1849 /* Exit to allow the pending virtual NMI to be injected */
1850 if (vm_nmi_pending(vmx->vm, vcpu))
1851 vmx_inject_nmi(vmx, vcpu);
1852 vmx_clear_nmi_window_exiting(vmx, vcpu);
1853 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NMI_WINDOW, 1);
1855 case EXIT_REASON_INOUT:
1856 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INOUT, 1);
1857 vmexit->exitcode = VM_EXITCODE_INOUT;
1858 vmexit->u.inout.bytes = (qual & 0x7) + 1;
1859 vmexit->u.inout.in = (qual & 0x8) ? 1 : 0;
1860 vmexit->u.inout.string = (qual & 0x10) ? 1 : 0;
1861 vmexit->u.inout.rep = (qual & 0x20) ? 1 : 0;
1862 vmexit->u.inout.port = (uint16_t)(qual >> 16);
1863 vmexit->u.inout.eax = (uint32_t)(vmxctx->guest_rax);
1865 case EXIT_REASON_CPUID:
1866 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CPUID, 1);
1867 handled = vmx_handle_cpuid(vmx->vm, vcpu, vmxctx);
1869 case EXIT_REASON_EXCEPTION:
1870 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXCEPTION, 1);
1871 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
1872 KASSERT((intr_info & VMCS_INTR_VALID) != 0,
1873 ("VM exit interruption info invalid: %#x", intr_info));
1876 * If Virtual NMIs control is 1 and the VM-exit is due to a
1877 * fault encountered during the execution of IRET then we must
1878 * restore the state of "virtual-NMI blocking" before resuming
1881 * See "Resuming Guest Software after Handling an Exception".
1883 if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 &&
1884 (intr_info & 0xff) != IDT_DF &&
1885 (intr_info & EXIT_QUAL_NMIUDTI) != 0)
1886 vmx_restore_nmi_blocking(vmx, vcpu);
1889 * The NMI has already been handled in vmx_exit_handle_nmi().
1891 if ((intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_NMI)
1894 case EXIT_REASON_EPT_FAULT:
1896 * If 'gpa' lies within the address space allocated to
1897 * memory then this must be a nested page fault otherwise
1898 * this must be an instruction that accesses MMIO space.
1901 if (vm_mem_allocated(vmx->vm, gpa) ||
1902 apic_access_fault(vmx, vcpu, gpa)) {
1903 vmexit->exitcode = VM_EXITCODE_PAGING;
1904 vmexit->u.paging.gpa = gpa;
1905 vmexit->u.paging.fault_type = ept_fault_type(qual);
1906 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NESTED_FAULT, 1);
1907 } else if (ept_emulation_fault(qual)) {
1908 vmexit->exitcode = VM_EXITCODE_INST_EMUL;
1909 vmexit->u.inst_emul.gpa = gpa;
1910 vmexit->u.inst_emul.gla = vmcs_gla();
1911 vmexit->u.inst_emul.cr3 = vmcs_guest_cr3();
1912 vmexit->u.inst_emul.cpu_mode = vmx_cpu_mode();
1913 vmexit->u.inst_emul.paging_mode = vmx_paging_mode();
1914 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INST_EMUL, 1);
1917 * If Virtual NMIs control is 1 and the VM-exit is due to an
1918 * EPT fault during the execution of IRET then we must restore
1919 * the state of "virtual-NMI blocking" before resuming.
1921 * See description of "NMI unblocking due to IRET" in
1922 * "Exit Qualification for EPT Violations".
1924 if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 &&
1925 (qual & EXIT_QUAL_NMIUDTI) != 0)
1926 vmx_restore_nmi_blocking(vmx, vcpu);
1928 case EXIT_REASON_VIRTUALIZED_EOI:
1929 vmexit->exitcode = VM_EXITCODE_IOAPIC_EOI;
1930 vmexit->u.ioapic_eoi.vector = qual & 0xFF;
1931 vmexit->inst_length = 0; /* trap-like */
1933 case EXIT_REASON_APIC_ACCESS:
1934 handled = vmx_handle_apic_access(vmx, vcpu, vmexit);
1936 case EXIT_REASON_APIC_WRITE:
1938 * APIC-write VM exit is trap-like so the %rip is already
1939 * pointing to the next instruction.
1941 vmexit->inst_length = 0;
1942 vlapic = vm_lapic(vmx->vm, vcpu);
1943 handled = vmx_handle_apic_write(vmx, vcpu, vlapic, qual);
1945 case EXIT_REASON_XSETBV:
1946 handled = vmx_emulate_xsetbv(vmx, vcpu, vmexit);
1949 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_UNKNOWN, 1);
1955 * It is possible that control is returned to userland
1956 * even though we were able to handle the VM exit in the
1959 * In such a case we want to make sure that the userland
1960 * restarts guest execution at the instruction *after*
1961 * the one we just processed. Therefore we update the
1962 * guest rip in the VMCS and in 'vmexit'.
1964 vmexit->rip += vmexit->inst_length;
1965 vmexit->inst_length = 0;
1966 vmcs_write(VMCS_GUEST_RIP, vmexit->rip);
1968 if (vmexit->exitcode == VM_EXITCODE_BOGUS) {
1970 * If this VM exit was not claimed by anybody then
1971 * treat it as a generic VMX exit.
1973 vmexit->exitcode = VM_EXITCODE_VMX;
1974 vmexit->u.vmx.status = VM_SUCCESS;
1975 vmexit->u.vmx.inst_type = 0;
1976 vmexit->u.vmx.inst_error = 0;
1979 * The exitcode and collateral have been populated.
1980 * The VM exit will be processed further in userland.
1988 vmx_exit_astpending(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
1991 vmexit->rip = vmcs_guest_rip();
1992 vmexit->inst_length = 0;
1993 vmexit->exitcode = VM_EXITCODE_BOGUS;
1994 vmx_astpending_trace(vmx, vcpu, vmexit->rip);
1995 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_ASTPENDING, 1);
2001 vmx_exit_rendezvous(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
2004 vmexit->rip = vmcs_guest_rip();
2005 vmexit->inst_length = 0;
2006 vmexit->exitcode = VM_EXITCODE_RENDEZVOUS;
2007 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_RENDEZVOUS, 1);
2013 vmx_exit_inst_error(struct vmxctx *vmxctx, int rc, struct vm_exit *vmexit)
2016 KASSERT(vmxctx->inst_fail_status != VM_SUCCESS,
2017 ("vmx_exit_inst_error: invalid inst_fail_status %d",
2018 vmxctx->inst_fail_status));
2020 vmexit->inst_length = 0;
2021 vmexit->exitcode = VM_EXITCODE_VMX;
2022 vmexit->u.vmx.status = vmxctx->inst_fail_status;
2023 vmexit->u.vmx.inst_error = vmcs_instruction_error();
2024 vmexit->u.vmx.exit_reason = ~0;
2025 vmexit->u.vmx.exit_qualification = ~0;
2028 case VMX_VMRESUME_ERROR:
2029 case VMX_VMLAUNCH_ERROR:
2030 case VMX_INVEPT_ERROR:
2031 vmexit->u.vmx.inst_type = rc;
2034 panic("vm_exit_inst_error: vmx_enter_guest returned %d", rc);
2041 * If the NMI-exiting VM execution control is set to '1' then an NMI in
2042 * non-root operation causes a VM-exit. NMI blocking is in effect so it is
2043 * sufficient to simply vector to the NMI handler via a software interrupt.
2044 * However, this must be done before maskable interrupts are enabled
2045 * otherwise the "iret" issued by an interrupt handler will incorrectly
2046 * clear NMI blocking.
2048 static __inline void
2049 vmx_exit_handle_nmi(struct vmx *vmx, int vcpuid, struct vm_exit *vmexit)
2053 KASSERT((read_rflags() & PSL_I) == 0, ("interrupts enabled"));
2055 if (vmexit->u.vmx.exit_reason != EXIT_REASON_EXCEPTION)
2058 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
2059 KASSERT((intr_info & VMCS_INTR_VALID) != 0,
2060 ("VM exit interruption info invalid: %#x", intr_info));
2062 if ((intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_NMI) {
2063 KASSERT((intr_info & 0xff) == IDT_NMI, ("VM exit due "
2064 "to NMI has invalid vector: %#x", intr_info));
2065 VCPU_CTR0(vmx->vm, vcpuid, "Vectoring to NMI handler");
2066 __asm __volatile("int $2");
2071 vmx_run(void *arg, int vcpu, register_t startrip, pmap_t pmap,
2072 void *rendezvous_cookie)
2074 int rc, handled, launched;
2077 struct vmxctx *vmxctx;
2079 struct vm_exit *vmexit;
2080 struct vlapic *vlapic;
2082 uint32_t exit_reason;
2086 vmcs = &vmx->vmcs[vcpu];
2087 vmxctx = &vmx->ctx[vcpu];
2088 vlapic = vm_lapic(vm, vcpu);
2089 vmexit = vm_exitinfo(vm, vcpu);
2092 KASSERT(vmxctx->pmap == pmap,
2093 ("pmap %p different than ctx pmap %p", pmap, vmxctx->pmap));
2099 * We do this every time because we may setup the virtual machine
2100 * from a different process than the one that actually runs it.
2102 * If the life of a virtual machine was spent entirely in the context
2103 * of a single process we could do this once in vmx_vminit().
2105 vmcs_write(VMCS_HOST_CR3, rcr3());
2107 vmcs_write(VMCS_GUEST_RIP, startrip);
2108 vmx_set_pcpu_defaults(vmx, vcpu, pmap);
2111 * Interrupts are disabled from this point on until the
2112 * guest starts executing. This is done for the following
2115 * If an AST is asserted on this thread after the check below,
2116 * then the IPI_AST notification will not be lost, because it
2117 * will cause a VM exit due to external interrupt as soon as
2118 * the guest state is loaded.
2120 * A posted interrupt after 'vmx_inject_interrupts()' will
2121 * not be "lost" because it will be held pending in the host
2122 * APIC because interrupts are disabled. The pending interrupt
2123 * will be recognized as soon as the guest state is loaded.
2125 * The same reasoning applies to the IPI generated by
2126 * pmap_invalidate_ept().
2129 if (curthread->td_flags & (TDF_ASTPENDING | TDF_NEEDRESCHED)) {
2131 handled = vmx_exit_astpending(vmx, vcpu, vmexit);
2135 if (vcpu_rendezvous_pending(rendezvous_cookie)) {
2137 handled = vmx_exit_rendezvous(vmx, vcpu, vmexit);
2141 vmx_inject_interrupts(vmx, vcpu, vlapic);
2142 vmx_run_trace(vmx, vcpu);
2143 rc = vmx_enter_guest(vmxctx, vmx, launched);
2145 /* Collect some information for VM exit processing */
2146 vmexit->rip = rip = vmcs_guest_rip();
2147 vmexit->inst_length = vmexit_instruction_length();
2148 vmexit->u.vmx.exit_reason = exit_reason = vmcs_exit_reason();
2149 vmexit->u.vmx.exit_qualification = vmcs_exit_qualification();
2151 if (rc == VMX_GUEST_VMEXIT) {
2152 vmx_exit_handle_nmi(vmx, vcpu, vmexit);
2154 handled = vmx_exit_process(vmx, vcpu, vmexit);
2157 handled = vmx_exit_inst_error(vmxctx, rc, vmexit);
2160 vmx_exit_trace(vmx, vcpu, rip, exit_reason, handled);
2164 * If a VM exit has been handled then the exitcode must be BOGUS
2165 * If a VM exit is not handled then the exitcode must not be BOGUS
2167 if ((handled && vmexit->exitcode != VM_EXITCODE_BOGUS) ||
2168 (!handled && vmexit->exitcode == VM_EXITCODE_BOGUS)) {
2169 panic("Mismatch between handled (%d) and exitcode (%d)",
2170 handled, vmexit->exitcode);
2174 vmm_stat_incr(vm, vcpu, VMEXIT_USERSPACE, 1);
2176 VCPU_CTR1(vm, vcpu, "returning from vmx_run: exitcode %d",
2184 vmx_vmcleanup(void *arg)
2187 struct vmx *vmx = arg;
2189 if (apic_access_virtualization(vmx, 0))
2190 vm_unmap_mmio(vmx->vm, DEFAULT_APIC_BASE, PAGE_SIZE);
2192 for (i = 0; i < VM_MAXCPU; i++)
2193 vpid_free(vmx->state[i].vpid);
2196 * XXXSMP we also need to clear the VMCS active on the other vcpus.
2198 error = vmclear(&vmx->vmcs[0]);
2200 panic("vmx_vmcleanup: vmclear error %d on vcpu 0", error);
2208 vmxctx_regptr(struct vmxctx *vmxctx, int reg)
2212 case VM_REG_GUEST_RAX:
2213 return (&vmxctx->guest_rax);
2214 case VM_REG_GUEST_RBX:
2215 return (&vmxctx->guest_rbx);
2216 case VM_REG_GUEST_RCX:
2217 return (&vmxctx->guest_rcx);
2218 case VM_REG_GUEST_RDX:
2219 return (&vmxctx->guest_rdx);
2220 case VM_REG_GUEST_RSI:
2221 return (&vmxctx->guest_rsi);
2222 case VM_REG_GUEST_RDI:
2223 return (&vmxctx->guest_rdi);
2224 case VM_REG_GUEST_RBP:
2225 return (&vmxctx->guest_rbp);
2226 case VM_REG_GUEST_R8:
2227 return (&vmxctx->guest_r8);
2228 case VM_REG_GUEST_R9:
2229 return (&vmxctx->guest_r9);
2230 case VM_REG_GUEST_R10:
2231 return (&vmxctx->guest_r10);
2232 case VM_REG_GUEST_R11:
2233 return (&vmxctx->guest_r11);
2234 case VM_REG_GUEST_R12:
2235 return (&vmxctx->guest_r12);
2236 case VM_REG_GUEST_R13:
2237 return (&vmxctx->guest_r13);
2238 case VM_REG_GUEST_R14:
2239 return (&vmxctx->guest_r14);
2240 case VM_REG_GUEST_R15:
2241 return (&vmxctx->guest_r15);
2249 vmxctx_getreg(struct vmxctx *vmxctx, int reg, uint64_t *retval)
2253 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
2261 vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val)
2265 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
2273 vmx_shadow_reg(int reg)
2280 case VM_REG_GUEST_CR0:
2281 shreg = VMCS_CR0_SHADOW;
2283 case VM_REG_GUEST_CR4:
2284 shreg = VMCS_CR4_SHADOW;
2294 vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval)
2296 int running, hostcpu;
2297 struct vmx *vmx = arg;
2299 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
2300 if (running && hostcpu != curcpu)
2301 panic("vmx_getreg: %s%d is running", vm_name(vmx->vm), vcpu);
2303 if (vmxctx_getreg(&vmx->ctx[vcpu], reg, retval) == 0)
2306 return (vmcs_getreg(&vmx->vmcs[vcpu], running, reg, retval));
2310 vmx_setreg(void *arg, int vcpu, int reg, uint64_t val)
2312 int error, hostcpu, running, shadow;
2314 struct vmx *vmx = arg;
2316 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
2317 if (running && hostcpu != curcpu)
2318 panic("vmx_setreg: %s%d is running", vm_name(vmx->vm), vcpu);
2320 if (vmxctx_setreg(&vmx->ctx[vcpu], reg, val) == 0)
2323 error = vmcs_setreg(&vmx->vmcs[vcpu], running, reg, val);
2327 * If the "load EFER" VM-entry control is 1 then the
2328 * value of EFER.LMA must be identical to "IA-32e mode guest"
2329 * bit in the VM-entry control.
2331 if ((entry_ctls & VM_ENTRY_LOAD_EFER) != 0 &&
2332 (reg == VM_REG_GUEST_EFER)) {
2333 vmcs_getreg(&vmx->vmcs[vcpu], running,
2334 VMCS_IDENT(VMCS_ENTRY_CTLS), &ctls);
2336 ctls |= VM_ENTRY_GUEST_LMA;
2338 ctls &= ~VM_ENTRY_GUEST_LMA;
2339 vmcs_setreg(&vmx->vmcs[vcpu], running,
2340 VMCS_IDENT(VMCS_ENTRY_CTLS), ctls);
2343 shadow = vmx_shadow_reg(reg);
2346 * Store the unmodified value in the shadow
2348 error = vmcs_setreg(&vmx->vmcs[vcpu], running,
2349 VMCS_IDENT(shadow), val);
2357 vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
2359 struct vmx *vmx = arg;
2361 return (vmcs_getdesc(&vmx->vmcs[vcpu], reg, desc));
2365 vmx_setdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
2367 struct vmx *vmx = arg;
2369 return (vmcs_setdesc(&vmx->vmcs[vcpu], reg, desc));
2373 vmx_getcap(void *arg, int vcpu, int type, int *retval)
2375 struct vmx *vmx = arg;
2381 vcap = vmx->cap[vcpu].set;
2384 case VM_CAP_HALT_EXIT:
2388 case VM_CAP_PAUSE_EXIT:
2392 case VM_CAP_MTRAP_EXIT:
2393 if (cap_monitor_trap)
2396 case VM_CAP_UNRESTRICTED_GUEST:
2397 if (cap_unrestricted_guest)
2400 case VM_CAP_ENABLE_INVPCID:
2409 *retval = (vcap & (1 << type)) ? 1 : 0;
2415 vmx_setcap(void *arg, int vcpu, int type, int val)
2417 struct vmx *vmx = arg;
2418 struct vmcs *vmcs = &vmx->vmcs[vcpu];
2430 case VM_CAP_HALT_EXIT:
2431 if (cap_halt_exit) {
2433 pptr = &vmx->cap[vcpu].proc_ctls;
2435 flag = PROCBASED_HLT_EXITING;
2436 reg = VMCS_PRI_PROC_BASED_CTLS;
2439 case VM_CAP_MTRAP_EXIT:
2440 if (cap_monitor_trap) {
2442 pptr = &vmx->cap[vcpu].proc_ctls;
2444 flag = PROCBASED_MTF;
2445 reg = VMCS_PRI_PROC_BASED_CTLS;
2448 case VM_CAP_PAUSE_EXIT:
2449 if (cap_pause_exit) {
2451 pptr = &vmx->cap[vcpu].proc_ctls;
2453 flag = PROCBASED_PAUSE_EXITING;
2454 reg = VMCS_PRI_PROC_BASED_CTLS;
2457 case VM_CAP_UNRESTRICTED_GUEST:
2458 if (cap_unrestricted_guest) {
2460 pptr = &vmx->cap[vcpu].proc_ctls2;
2462 flag = PROCBASED2_UNRESTRICTED_GUEST;
2463 reg = VMCS_SEC_PROC_BASED_CTLS;
2466 case VM_CAP_ENABLE_INVPCID:
2469 pptr = &vmx->cap[vcpu].proc_ctls2;
2471 flag = PROCBASED2_ENABLE_INVPCID;
2472 reg = VMCS_SEC_PROC_BASED_CTLS;
2486 error = vmwrite(reg, baseval);
2493 * Update optional stored flags, and record
2501 vmx->cap[vcpu].set |= (1 << type);
2503 vmx->cap[vcpu].set &= ~(1 << type);
2512 struct vlapic vlapic;
2513 struct pir_desc *pir_desc;
2517 #define VMX_CTR_PIR(vm, vcpuid, pir_desc, notify, vector, level, msg) \
2519 VCPU_CTR2(vm, vcpuid, msg " assert %s-triggered vector %d", \
2520 level ? "level" : "edge", vector); \
2521 VCPU_CTR1(vm, vcpuid, msg " pir0 0x%016lx", pir_desc->pir[0]); \
2522 VCPU_CTR1(vm, vcpuid, msg " pir1 0x%016lx", pir_desc->pir[1]); \
2523 VCPU_CTR1(vm, vcpuid, msg " pir2 0x%016lx", pir_desc->pir[2]); \
2524 VCPU_CTR1(vm, vcpuid, msg " pir3 0x%016lx", pir_desc->pir[3]); \
2525 VCPU_CTR1(vm, vcpuid, msg " notify: %s", notify ? "yes" : "no");\
2529 * vlapic->ops handlers that utilize the APICv hardware assist described in
2530 * Chapter 29 of the Intel SDM.
2533 vmx_set_intr_ready(struct vlapic *vlapic, int vector, bool level)
2535 struct vlapic_vtx *vlapic_vtx;
2536 struct pir_desc *pir_desc;
2540 vlapic_vtx = (struct vlapic_vtx *)vlapic;
2541 pir_desc = vlapic_vtx->pir_desc;
2544 * Keep track of interrupt requests in the PIR descriptor. This is
2545 * because the virtual APIC page pointed to by the VMCS cannot be
2546 * modified if the vcpu is running.
2549 mask = 1UL << (vector % 64);
2550 atomic_set_long(&pir_desc->pir[idx], mask);
2551 notify = atomic_cmpset_long(&pir_desc->pending, 0, 1);
2553 VMX_CTR_PIR(vlapic->vm, vlapic->vcpuid, pir_desc, notify, vector,
2554 level, "vmx_set_intr_ready");
2559 vmx_pending_intr(struct vlapic *vlapic, int *vecptr)
2561 struct vlapic_vtx *vlapic_vtx;
2562 struct pir_desc *pir_desc;
2563 struct LAPIC *lapic;
2564 uint64_t pending, pirval;
2569 * This function is only expected to be called from the 'HLT' exit
2570 * handler which does not care about the vector that is pending.
2572 KASSERT(vecptr == NULL, ("vmx_pending_intr: vecptr must be NULL"));
2574 vlapic_vtx = (struct vlapic_vtx *)vlapic;
2575 pir_desc = vlapic_vtx->pir_desc;
2577 pending = atomic_load_acq_long(&pir_desc->pending);
2579 return (0); /* common case */
2582 * If there is an interrupt pending then it will be recognized only
2583 * if its priority is greater than the processor priority.
2585 * Special case: if the processor priority is zero then any pending
2586 * interrupt will be recognized.
2588 lapic = vlapic->apic_page;
2589 ppr = lapic->ppr & 0xf0;
2593 VCPU_CTR1(vlapic->vm, vlapic->vcpuid, "HLT with non-zero PPR %d",
2596 for (i = 3; i >= 0; i--) {
2597 pirval = pir_desc->pir[i];
2599 vpr = (i * 64 + flsl(pirval) - 1) & 0xf0;
2607 vmx_intr_accepted(struct vlapic *vlapic, int vector)
2610 panic("vmx_intr_accepted: not expected to be called");
2614 vmx_set_tmr(struct vlapic *vlapic, int vector, bool level)
2616 struct vlapic_vtx *vlapic_vtx;
2621 KASSERT(vector >= 0 && vector <= 255, ("invalid vector %d", vector));
2622 KASSERT(!vcpu_is_running(vlapic->vm, vlapic->vcpuid, NULL),
2623 ("vmx_set_tmr: vcpu cannot be running"));
2625 vlapic_vtx = (struct vlapic_vtx *)vlapic;
2626 vmx = vlapic_vtx->vmx;
2627 vmcs = &vmx->vmcs[vlapic->vcpuid];
2628 mask = 1UL << (vector % 64);
2631 val = vmcs_read(VMCS_EOI_EXIT(vector));
2636 vmcs_write(VMCS_EOI_EXIT(vector), val);
2641 vmx_enable_x2apic_mode(struct vlapic *vlapic)
2645 uint32_t proc_ctls2;
2648 vcpuid = vlapic->vcpuid;
2649 vmx = ((struct vlapic_vtx *)vlapic)->vmx;
2650 vmcs = &vmx->vmcs[vcpuid];
2652 proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
2653 KASSERT((proc_ctls2 & PROCBASED2_VIRTUALIZE_APIC_ACCESSES) != 0,
2654 ("%s: invalid proc_ctls2 %#x", __func__, proc_ctls2));
2656 proc_ctls2 &= ~PROCBASED2_VIRTUALIZE_APIC_ACCESSES;
2657 proc_ctls2 |= PROCBASED2_VIRTUALIZE_X2APIC_MODE;
2658 vmx->cap[vcpuid].proc_ctls2 = proc_ctls2;
2661 vmcs_write(VMCS_SEC_PROC_BASED_CTLS, proc_ctls2);
2664 if (vlapic->vcpuid == 0) {
2666 * The nested page table mappings are shared by all vcpus
2667 * so unmap the APIC access page just once.
2669 error = vm_unmap_mmio(vmx->vm, DEFAULT_APIC_BASE, PAGE_SIZE);
2670 KASSERT(error == 0, ("%s: vm_unmap_mmio error %d",
2674 * The MSR bitmap is shared by all vcpus so modify it only
2675 * once in the context of vcpu 0.
2677 error = vmx_allow_x2apic_msrs(vmx);
2678 KASSERT(error == 0, ("%s: vmx_allow_x2apic_msrs error %d",
2684 vmx_post_intr(struct vlapic *vlapic, int hostcpu)
2687 ipi_cpu(hostcpu, pirvec);
2691 * Transfer the pending interrupts in the PIR descriptor to the IRR
2692 * in the virtual APIC page.
2695 vmx_inject_pir(struct vlapic *vlapic)
2697 struct vlapic_vtx *vlapic_vtx;
2698 struct pir_desc *pir_desc;
2699 struct LAPIC *lapic;
2700 uint64_t val, pirval;
2702 uint16_t intr_status_old, intr_status_new;
2704 vlapic_vtx = (struct vlapic_vtx *)vlapic;
2705 pir_desc = vlapic_vtx->pir_desc;
2706 if (atomic_cmpset_long(&pir_desc->pending, 1, 0) == 0) {
2707 VCPU_CTR0(vlapic->vm, vlapic->vcpuid, "vmx_inject_pir: "
2708 "no posted interrupt pending");
2713 lapic = vlapic->apic_page;
2715 val = atomic_readandclear_long(&pir_desc->pir[0]);
2718 lapic->irr1 |= val >> 32;
2723 val = atomic_readandclear_long(&pir_desc->pir[1]);
2726 lapic->irr3 |= val >> 32;
2731 val = atomic_readandclear_long(&pir_desc->pir[2]);
2734 lapic->irr5 |= val >> 32;
2739 val = atomic_readandclear_long(&pir_desc->pir[3]);
2742 lapic->irr7 |= val >> 32;
2746 VLAPIC_CTR_IRR(vlapic, "vmx_inject_pir");
2749 * Update RVI so the processor can evaluate pending virtual
2750 * interrupts on VM-entry.
2753 rvi = pirbase + flsl(pirval) - 1;
2754 intr_status_old = vmcs_read(VMCS_GUEST_INTR_STATUS);
2755 intr_status_new = (intr_status_old & 0xFF00) | rvi;
2756 if (intr_status_new > intr_status_old) {
2757 vmcs_write(VMCS_GUEST_INTR_STATUS, intr_status_new);
2758 VCPU_CTR2(vlapic->vm, vlapic->vcpuid, "vmx_inject_pir: "
2759 "guest_intr_status changed from 0x%04x to 0x%04x",
2760 intr_status_old, intr_status_new);
2765 static struct vlapic *
2766 vmx_vlapic_init(void *arg, int vcpuid)
2769 struct vlapic *vlapic;
2770 struct vlapic_vtx *vlapic_vtx;
2774 vlapic = malloc(sizeof(struct vlapic_vtx), M_VLAPIC, M_WAITOK | M_ZERO);
2775 vlapic->vm = vmx->vm;
2776 vlapic->vcpuid = vcpuid;
2777 vlapic->apic_page = (struct LAPIC *)&vmx->apic_page[vcpuid];
2779 vlapic_vtx = (struct vlapic_vtx *)vlapic;
2780 vlapic_vtx->pir_desc = &vmx->pir_desc[vcpuid];
2781 vlapic_vtx->vmx = vmx;
2783 if (virtual_interrupt_delivery) {
2784 vlapic->ops.set_intr_ready = vmx_set_intr_ready;
2785 vlapic->ops.pending_intr = vmx_pending_intr;
2786 vlapic->ops.intr_accepted = vmx_intr_accepted;
2787 vlapic->ops.set_tmr = vmx_set_tmr;
2788 vlapic->ops.enable_x2apic_mode = vmx_enable_x2apic_mode;
2791 if (posted_interrupts)
2792 vlapic->ops.post_intr = vmx_post_intr;
2794 vlapic_init(vlapic);
2800 vmx_vlapic_cleanup(void *arg, struct vlapic *vlapic)
2803 vlapic_cleanup(vlapic);
2804 free(vlapic, M_VLAPIC);
2807 struct vmm_ops vmm_ops_intel = {