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/specialreg.h>
49 #include <machine/vmparam.h>
51 #include <machine/vmm.h>
57 #include "vlapic_priv.h"
61 #include "vmx_cpufunc.h"
64 #include "vmx_controls.h"
66 #define PINBASED_CTLS_ONE_SETTING \
67 (PINBASED_EXTINT_EXITING | \
68 PINBASED_NMI_EXITING | \
70 #define PINBASED_CTLS_ZERO_SETTING 0
72 #define PROCBASED_CTLS_WINDOW_SETTING \
73 (PROCBASED_INT_WINDOW_EXITING | \
74 PROCBASED_NMI_WINDOW_EXITING)
76 #define PROCBASED_CTLS_ONE_SETTING \
77 (PROCBASED_SECONDARY_CONTROLS | \
78 PROCBASED_IO_EXITING | \
79 PROCBASED_MSR_BITMAPS | \
80 PROCBASED_CTLS_WINDOW_SETTING)
81 #define PROCBASED_CTLS_ZERO_SETTING \
82 (PROCBASED_CR3_LOAD_EXITING | \
83 PROCBASED_CR3_STORE_EXITING | \
86 #define PROCBASED_CTLS2_ONE_SETTING PROCBASED2_ENABLE_EPT
87 #define PROCBASED_CTLS2_ZERO_SETTING 0
89 #define VM_EXIT_CTLS_ONE_SETTING_NO_PAT \
94 #define VM_EXIT_CTLS_ONE_SETTING \
95 (VM_EXIT_CTLS_ONE_SETTING_NO_PAT | \
98 #define VM_EXIT_CTLS_ZERO_SETTING VM_EXIT_SAVE_DEBUG_CONTROLS
100 #define VM_ENTRY_CTLS_ONE_SETTING_NO_PAT VM_ENTRY_LOAD_EFER
102 #define VM_ENTRY_CTLS_ONE_SETTING \
103 (VM_ENTRY_CTLS_ONE_SETTING_NO_PAT | \
105 #define VM_ENTRY_CTLS_ZERO_SETTING \
106 (VM_ENTRY_LOAD_DEBUG_CONTROLS | \
107 VM_ENTRY_INTO_SMM | \
108 VM_ENTRY_DEACTIVATE_DUAL_MONITOR)
110 #define guest_msr_rw(vmx, msr) \
111 msr_bitmap_change_access((vmx)->msr_bitmap, (msr), MSR_BITMAP_ACCESS_RW)
116 static MALLOC_DEFINE(M_VMX, "vmx", "vmx");
117 static MALLOC_DEFINE(M_VLAPIC, "vlapic", "vlapic");
119 SYSCTL_DECL(_hw_vmm);
120 SYSCTL_NODE(_hw_vmm, OID_AUTO, vmx, CTLFLAG_RW, NULL, NULL);
122 int vmxon_enabled[MAXCPU];
123 static char vmxon_region[MAXCPU][PAGE_SIZE] __aligned(PAGE_SIZE);
125 static uint32_t pinbased_ctls, procbased_ctls, procbased_ctls2;
126 static uint32_t exit_ctls, entry_ctls;
128 static uint64_t cr0_ones_mask, cr0_zeros_mask;
129 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_ones_mask, CTLFLAG_RD,
130 &cr0_ones_mask, 0, NULL);
131 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_zeros_mask, CTLFLAG_RD,
132 &cr0_zeros_mask, 0, NULL);
134 static uint64_t cr4_ones_mask, cr4_zeros_mask;
135 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_ones_mask, CTLFLAG_RD,
136 &cr4_ones_mask, 0, NULL);
137 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_zeros_mask, CTLFLAG_RD,
138 &cr4_zeros_mask, 0, NULL);
140 static int vmx_no_patmsr;
142 static int vmx_initialized;
143 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, initialized, CTLFLAG_RD,
144 &vmx_initialized, 0, "Intel VMX initialized");
147 * Virtual NMI blocking conditions.
149 * Some processor implementations also require NMI to be blocked if
150 * the STI_BLOCKING bit is set. It is possible to detect this at runtime
151 * based on the (exit_reason,exit_qual) tuple being set to
152 * (EXIT_REASON_INVAL_VMCS, EXIT_QUAL_NMI_WHILE_STI_BLOCKING).
154 * We take the easy way out and also include STI_BLOCKING as one of the
155 * gating items for vNMI injection.
157 static uint64_t nmi_blocking_bits = VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING |
158 VMCS_INTERRUPTIBILITY_NMI_BLOCKING |
159 VMCS_INTERRUPTIBILITY_STI_BLOCKING;
162 * Optional capabilities
164 static int cap_halt_exit;
165 static int cap_pause_exit;
166 static int cap_unrestricted_guest;
167 static int cap_monitor_trap;
168 static int cap_invpcid;
170 static struct unrhdr *vpid_unr;
171 static u_int vpid_alloc_failed;
172 SYSCTL_UINT(_hw_vmm_vmx, OID_AUTO, vpid_alloc_failed, CTLFLAG_RD,
173 &vpid_alloc_failed, 0, NULL);
177 exit_reason_to_str(int reason)
179 static char reasonbuf[32];
182 case EXIT_REASON_EXCEPTION:
184 case EXIT_REASON_EXT_INTR:
186 case EXIT_REASON_TRIPLE_FAULT:
187 return "triplefault";
188 case EXIT_REASON_INIT:
190 case EXIT_REASON_SIPI:
192 case EXIT_REASON_IO_SMI:
194 case EXIT_REASON_SMI:
196 case EXIT_REASON_INTR_WINDOW:
198 case EXIT_REASON_NMI_WINDOW:
200 case EXIT_REASON_TASK_SWITCH:
202 case EXIT_REASON_CPUID:
204 case EXIT_REASON_GETSEC:
206 case EXIT_REASON_HLT:
208 case EXIT_REASON_INVD:
210 case EXIT_REASON_INVLPG:
212 case EXIT_REASON_RDPMC:
214 case EXIT_REASON_RDTSC:
216 case EXIT_REASON_RSM:
218 case EXIT_REASON_VMCALL:
220 case EXIT_REASON_VMCLEAR:
222 case EXIT_REASON_VMLAUNCH:
224 case EXIT_REASON_VMPTRLD:
226 case EXIT_REASON_VMPTRST:
228 case EXIT_REASON_VMREAD:
230 case EXIT_REASON_VMRESUME:
232 case EXIT_REASON_VMWRITE:
234 case EXIT_REASON_VMXOFF:
236 case EXIT_REASON_VMXON:
238 case EXIT_REASON_CR_ACCESS:
240 case EXIT_REASON_DR_ACCESS:
242 case EXIT_REASON_INOUT:
244 case EXIT_REASON_RDMSR:
246 case EXIT_REASON_WRMSR:
248 case EXIT_REASON_INVAL_VMCS:
250 case EXIT_REASON_INVAL_MSR:
252 case EXIT_REASON_MWAIT:
254 case EXIT_REASON_MTF:
256 case EXIT_REASON_MONITOR:
258 case EXIT_REASON_PAUSE:
260 case EXIT_REASON_MCE:
262 case EXIT_REASON_TPR:
264 case EXIT_REASON_APIC:
266 case EXIT_REASON_GDTR_IDTR:
268 case EXIT_REASON_LDTR_TR:
270 case EXIT_REASON_EPT_FAULT:
272 case EXIT_REASON_EPT_MISCONFIG:
273 return "eptmisconfig";
274 case EXIT_REASON_INVEPT:
276 case EXIT_REASON_RDTSCP:
278 case EXIT_REASON_VMX_PREEMPT:
280 case EXIT_REASON_INVVPID:
282 case EXIT_REASON_WBINVD:
284 case EXIT_REASON_XSETBV:
287 snprintf(reasonbuf, sizeof(reasonbuf), "%d", reason);
294 vmx_setjmp_rc2str(int rc)
297 case VMX_RETURN_DIRECT:
299 case VMX_RETURN_LONGJMP:
301 case VMX_RETURN_VMRESUME:
303 case VMX_RETURN_VMLAUNCH:
312 #define SETJMP_TRACE(vmx, vcpu, vmxctx, regname) \
313 VCPU_CTR1((vmx)->vm, (vcpu), "setjmp trace " #regname " 0x%016lx", \
317 vmx_setjmp_trace(struct vmx *vmx, int vcpu, struct vmxctx *vmxctx, int rc)
319 uint64_t host_rip, host_rsp;
321 if (vmxctx != &vmx->ctx[vcpu])
322 panic("vmx_setjmp_trace: invalid vmxctx %p; should be %p",
323 vmxctx, &vmx->ctx[vcpu]);
325 VCPU_CTR1((vmx)->vm, (vcpu), "vmxctx = %p", vmxctx);
326 VCPU_CTR2((vmx)->vm, (vcpu), "setjmp return code %s(%d)",
327 vmx_setjmp_rc2str(rc), rc);
329 host_rip = vmcs_read(VMCS_HOST_RIP);
330 host_rsp = vmcs_read(VMCS_HOST_RSP);
331 VCPU_CTR2((vmx)->vm, (vcpu), "vmcs host_rip 0x%016lx, host_rsp %#lx",
334 SETJMP_TRACE(vmx, vcpu, vmxctx, host_r15);
335 SETJMP_TRACE(vmx, vcpu, vmxctx, host_r14);
336 SETJMP_TRACE(vmx, vcpu, vmxctx, host_r13);
337 SETJMP_TRACE(vmx, vcpu, vmxctx, host_r12);
338 SETJMP_TRACE(vmx, vcpu, vmxctx, host_rbp);
339 SETJMP_TRACE(vmx, vcpu, vmxctx, host_rsp);
340 SETJMP_TRACE(vmx, vcpu, vmxctx, host_rbx);
341 SETJMP_TRACE(vmx, vcpu, vmxctx, host_rip);
343 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rdi);
344 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rsi);
345 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rdx);
346 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rcx);
347 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r8);
348 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r9);
349 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rax);
350 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rbx);
351 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rbp);
352 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r10);
353 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r11);
354 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r12);
355 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r13);
356 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r14);
357 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r15);
358 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_cr2);
363 vmx_setjmp_trace(struct vmx *vmx, int vcpu, struct vmxctx *vmxctx, int rc)
370 vmx_fix_cr0(u_long cr0)
373 return ((cr0 | cr0_ones_mask) & ~cr0_zeros_mask);
377 vmx_fix_cr4(u_long cr4)
380 return ((cr4 | cr4_ones_mask) & ~cr4_zeros_mask);
386 if (vpid < 0 || vpid > 0xffff)
387 panic("vpid_free: invalid vpid %d", vpid);
390 * VPIDs [0,VM_MAXCPU] are special and are not allocated from
391 * the unit number allocator.
394 if (vpid > VM_MAXCPU)
395 free_unr(vpid_unr, vpid);
399 vpid_alloc(uint16_t *vpid, int num)
403 if (num <= 0 || num > VM_MAXCPU)
404 panic("invalid number of vpids requested: %d", num);
407 * If the "enable vpid" execution control is not enabled then the
408 * VPID is required to be 0 for all vcpus.
410 if ((procbased_ctls2 & PROCBASED2_ENABLE_VPID) == 0) {
411 for (i = 0; i < num; i++)
417 * Allocate a unique VPID for each vcpu from the unit number allocator.
419 for (i = 0; i < num; i++) {
420 x = alloc_unr(vpid_unr);
428 atomic_add_int(&vpid_alloc_failed, 1);
431 * If the unit number allocator does not have enough unique
432 * VPIDs then we need to allocate from the [1,VM_MAXCPU] range.
434 * These VPIDs are not be unique across VMs but this does not
435 * affect correctness because the combined mappings are also
436 * tagged with the EP4TA which is unique for each VM.
438 * It is still sub-optimal because the invvpid will invalidate
439 * combined mappings for a particular VPID across all EP4TAs.
444 for (i = 0; i < num; i++)
453 * VPID 0 is required when the "enable VPID" execution control is
456 * VPIDs [1,VM_MAXCPU] are used as the "overflow namespace" when the
457 * unit number allocator does not have sufficient unique VPIDs to
458 * satisfy the allocation.
460 * The remaining VPIDs are managed by the unit number allocator.
462 vpid_unr = new_unrhdr(VM_MAXCPU + 1, 0xffff, NULL);
466 msr_save_area_init(struct msr_entry *g_area, int *g_count)
470 static struct msr_entry guest_msrs[] = {
471 { MSR_KGSBASE, 0, 0 },
474 cnt = sizeof(guest_msrs) / sizeof(guest_msrs[0]);
475 if (cnt > GUEST_MSR_MAX_ENTRIES)
476 panic("guest msr save area overrun");
477 bcopy(guest_msrs, g_area, sizeof(guest_msrs));
482 vmx_disable(void *arg __unused)
484 struct invvpid_desc invvpid_desc = { 0 };
485 struct invept_desc invept_desc = { 0 };
487 if (vmxon_enabled[curcpu]) {
489 * See sections 25.3.3.3 and 25.3.3.4 in Intel Vol 3b.
491 * VMXON or VMXOFF are not required to invalidate any TLB
492 * caching structures. This prevents potential retention of
493 * cached information in the TLB between distinct VMX episodes.
495 invvpid(INVVPID_TYPE_ALL_CONTEXTS, invvpid_desc);
496 invept(INVEPT_TYPE_ALL_CONTEXTS, invept_desc);
499 load_cr4(rcr4() & ~CR4_VMXE);
506 if (vpid_unr != NULL) {
507 delete_unrhdr(vpid_unr);
511 smp_rendezvous(NULL, vmx_disable, NULL, NULL);
517 vmx_enable(void *arg __unused)
521 load_cr4(rcr4() | CR4_VMXE);
523 *(uint32_t *)vmxon_region[curcpu] = vmx_revision();
524 error = vmxon(vmxon_region[curcpu]);
526 vmxon_enabled[curcpu] = 1;
533 if (vmxon_enabled[curcpu])
534 vmxon(vmxon_region[curcpu]);
541 uint64_t fixed0, fixed1, feature_control;
544 /* CPUID.1:ECX[bit 5] must be 1 for processor to support VMX */
545 if (!(cpu_feature2 & CPUID2_VMX)) {
546 printf("vmx_init: processor does not support VMX operation\n");
551 * Verify that MSR_IA32_FEATURE_CONTROL lock and VMXON enable bits
552 * are set (bits 0 and 2 respectively).
554 feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
555 if ((feature_control & IA32_FEATURE_CONTROL_LOCK) == 0 ||
556 (feature_control & IA32_FEATURE_CONTROL_VMX_EN) == 0) {
557 printf("vmx_init: VMX operation disabled by BIOS\n");
561 /* Check support for primary processor-based VM-execution controls */
562 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
563 MSR_VMX_TRUE_PROCBASED_CTLS,
564 PROCBASED_CTLS_ONE_SETTING,
565 PROCBASED_CTLS_ZERO_SETTING, &procbased_ctls);
567 printf("vmx_init: processor does not support desired primary "
568 "processor-based controls\n");
572 /* Clear the processor-based ctl bits that are set on demand */
573 procbased_ctls &= ~PROCBASED_CTLS_WINDOW_SETTING;
575 /* Check support for secondary processor-based VM-execution controls */
576 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
577 MSR_VMX_PROCBASED_CTLS2,
578 PROCBASED_CTLS2_ONE_SETTING,
579 PROCBASED_CTLS2_ZERO_SETTING, &procbased_ctls2);
581 printf("vmx_init: processor does not support desired secondary "
582 "processor-based controls\n");
586 /* Check support for VPID */
587 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2,
588 PROCBASED2_ENABLE_VPID, 0, &tmp);
590 procbased_ctls2 |= PROCBASED2_ENABLE_VPID;
592 /* Check support for pin-based VM-execution controls */
593 error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS,
594 MSR_VMX_TRUE_PINBASED_CTLS,
595 PINBASED_CTLS_ONE_SETTING,
596 PINBASED_CTLS_ZERO_SETTING, &pinbased_ctls);
598 printf("vmx_init: processor does not support desired "
599 "pin-based controls\n");
603 /* Check support for VM-exit controls */
604 error = vmx_set_ctlreg(MSR_VMX_EXIT_CTLS, MSR_VMX_TRUE_EXIT_CTLS,
605 VM_EXIT_CTLS_ONE_SETTING,
606 VM_EXIT_CTLS_ZERO_SETTING,
609 /* Try again without the PAT MSR bits */
610 error = vmx_set_ctlreg(MSR_VMX_EXIT_CTLS,
611 MSR_VMX_TRUE_EXIT_CTLS,
612 VM_EXIT_CTLS_ONE_SETTING_NO_PAT,
613 VM_EXIT_CTLS_ZERO_SETTING,
616 printf("vmx_init: processor does not support desired "
621 printf("vmm: PAT MSR access not supported\n");
622 guest_msr_valid(MSR_PAT);
627 /* Check support for VM-entry controls */
628 if (!vmx_no_patmsr) {
629 error = vmx_set_ctlreg(MSR_VMX_ENTRY_CTLS,
630 MSR_VMX_TRUE_ENTRY_CTLS,
631 VM_ENTRY_CTLS_ONE_SETTING,
632 VM_ENTRY_CTLS_ZERO_SETTING,
635 error = vmx_set_ctlreg(MSR_VMX_ENTRY_CTLS,
636 MSR_VMX_TRUE_ENTRY_CTLS,
637 VM_ENTRY_CTLS_ONE_SETTING_NO_PAT,
638 VM_ENTRY_CTLS_ZERO_SETTING,
643 printf("vmx_init: processor does not support desired "
649 * Check support for optional features by testing them
652 cap_halt_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
653 MSR_VMX_TRUE_PROCBASED_CTLS,
654 PROCBASED_HLT_EXITING, 0,
657 cap_monitor_trap = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
658 MSR_VMX_PROCBASED_CTLS,
662 cap_pause_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
663 MSR_VMX_TRUE_PROCBASED_CTLS,
664 PROCBASED_PAUSE_EXITING, 0,
667 cap_unrestricted_guest = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
668 MSR_VMX_PROCBASED_CTLS2,
669 PROCBASED2_UNRESTRICTED_GUEST, 0,
672 cap_invpcid = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
673 MSR_VMX_PROCBASED_CTLS2, PROCBASED2_ENABLE_INVPCID, 0,
680 printf("vmx_init: ept initialization failed (%d)\n", error);
685 * Stash the cr0 and cr4 bits that must be fixed to 0 or 1
687 fixed0 = rdmsr(MSR_VMX_CR0_FIXED0);
688 fixed1 = rdmsr(MSR_VMX_CR0_FIXED1);
689 cr0_ones_mask = fixed0 & fixed1;
690 cr0_zeros_mask = ~fixed0 & ~fixed1;
693 * CR0_PE and CR0_PG can be set to zero in VMX non-root operation
694 * if unrestricted guest execution is allowed.
696 if (cap_unrestricted_guest)
697 cr0_ones_mask &= ~(CR0_PG | CR0_PE);
700 * Do not allow the guest to set CR0_NW or CR0_CD.
702 cr0_zeros_mask |= (CR0_NW | CR0_CD);
704 fixed0 = rdmsr(MSR_VMX_CR4_FIXED0);
705 fixed1 = rdmsr(MSR_VMX_CR4_FIXED1);
706 cr4_ones_mask = fixed0 & fixed1;
707 cr4_zeros_mask = ~fixed0 & ~fixed1;
711 /* enable VMX operation */
712 smp_rendezvous(NULL, vmx_enable, NULL, NULL);
720 vmx_setup_cr_shadow(int which, struct vmcs *vmcs, uint32_t initial)
722 int error, mask_ident, shadow_ident;
725 if (which != 0 && which != 4)
726 panic("vmx_setup_cr_shadow: unknown cr%d", which);
729 mask_ident = VMCS_CR0_MASK;
730 mask_value = cr0_ones_mask | cr0_zeros_mask;
731 shadow_ident = VMCS_CR0_SHADOW;
733 mask_ident = VMCS_CR4_MASK;
734 mask_value = cr4_ones_mask | cr4_zeros_mask;
735 shadow_ident = VMCS_CR4_SHADOW;
738 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(mask_ident), mask_value);
742 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(shadow_ident), initial);
748 #define vmx_setup_cr0_shadow(vmcs,init) vmx_setup_cr_shadow(0, (vmcs), (init))
749 #define vmx_setup_cr4_shadow(vmcs,init) vmx_setup_cr_shadow(4, (vmcs), (init))
752 vmx_vminit(struct vm *vm, pmap_t pmap)
754 uint16_t vpid[VM_MAXCPU];
755 int i, error, guest_msr_count;
758 vmx = malloc(sizeof(struct vmx), M_VMX, M_WAITOK | M_ZERO);
759 if ((uintptr_t)vmx & PAGE_MASK) {
760 panic("malloc of struct vmx not aligned on %d byte boundary",
765 vmx->eptp = eptp(vtophys((vm_offset_t)pmap->pm_pml4));
768 * Clean up EPTP-tagged guest physical and combined mappings
770 * VMX transitions are not required to invalidate any guest physical
771 * mappings. So, it may be possible for stale guest physical mappings
772 * to be present in the processor TLBs.
774 * Combined mappings for this EP4TA are also invalidated for all VPIDs.
776 ept_invalidate_mappings(vmx->eptp);
778 msr_bitmap_initialize(vmx->msr_bitmap);
781 * It is safe to allow direct access to MSR_GSBASE and MSR_FSBASE.
782 * The guest FSBASE and GSBASE are saved and restored during
783 * vm-exit and vm-entry respectively. The host FSBASE and GSBASE are
784 * always restored from the vmcs host state area on vm-exit.
786 * The SYSENTER_CS/ESP/EIP MSRs are identical to FS/GSBASE in
787 * how they are saved/restored so can be directly accessed by the
790 * Guest KGSBASE is saved and restored in the guest MSR save area.
791 * Host KGSBASE is restored before returning to userland from the pcb.
792 * There will be a window of time when we are executing in the host
793 * kernel context with a value of KGSBASE from the guest. This is ok
794 * because the value of KGSBASE is inconsequential in kernel context.
796 * MSR_EFER is saved and restored in the guest VMCS area on a
797 * VM exit and entry respectively. It is also restored from the
798 * host VMCS area on a VM exit.
800 if (guest_msr_rw(vmx, MSR_GSBASE) ||
801 guest_msr_rw(vmx, MSR_FSBASE) ||
802 guest_msr_rw(vmx, MSR_SYSENTER_CS_MSR) ||
803 guest_msr_rw(vmx, MSR_SYSENTER_ESP_MSR) ||
804 guest_msr_rw(vmx, MSR_SYSENTER_EIP_MSR) ||
805 guest_msr_rw(vmx, MSR_KGSBASE) ||
806 guest_msr_rw(vmx, MSR_EFER))
807 panic("vmx_vminit: error setting guest msr access");
810 * MSR_PAT is saved and restored in the guest VMCS are on a VM exit
811 * and entry respectively. It is also restored from the host VMCS
812 * area on a VM exit. However, if running on a system with no
813 * MSR_PAT save/restore support, leave access disabled so accesses
816 if (!vmx_no_patmsr && guest_msr_rw(vmx, MSR_PAT))
817 panic("vmx_vminit: error setting guest pat msr access");
819 vpid_alloc(vpid, VM_MAXCPU);
821 for (i = 0; i < VM_MAXCPU; i++) {
822 vmx->vmcs[i].identifier = vmx_revision();
823 error = vmclear(&vmx->vmcs[i]);
825 panic("vmx_vminit: vmclear error %d on vcpu %d\n",
829 error = vmcs_set_defaults(&vmx->vmcs[i],
831 (u_long)&vmx->ctx[i],
836 exit_ctls, entry_ctls,
837 vtophys(vmx->msr_bitmap),
841 panic("vmx_vminit: vmcs_set_defaults error %d", error);
844 vmx->cap[i].proc_ctls = procbased_ctls;
845 vmx->cap[i].proc_ctls2 = procbased_ctls2;
847 vmx->state[i].lastcpu = -1;
848 vmx->state[i].vpid = vpid[i];
850 msr_save_area_init(vmx->guest_msrs[i], &guest_msr_count);
852 error = vmcs_set_msr_save(&vmx->vmcs[i],
853 vtophys(vmx->guest_msrs[i]),
856 panic("vmcs_set_msr_save error %d", error);
859 * Set up the CR0/4 shadows, and init the read shadow
860 * to the power-on register value from the Intel Sys Arch.
864 error = vmx_setup_cr0_shadow(&vmx->vmcs[i], 0x60000010);
866 panic("vmx_setup_cr0_shadow %d", error);
868 error = vmx_setup_cr4_shadow(&vmx->vmcs[i], 0);
870 panic("vmx_setup_cr4_shadow %d", error);
872 vmx->ctx[i].pmap = pmap;
873 vmx->ctx[i].eptp = vmx->eptp;
880 vmx_handle_cpuid(struct vm *vm, int vcpu, struct vmxctx *vmxctx)
884 func = vmxctx->guest_rax;
886 handled = x86_emulate_cpuid(vm, vcpu,
887 (uint32_t*)(&vmxctx->guest_rax),
888 (uint32_t*)(&vmxctx->guest_rbx),
889 (uint32_t*)(&vmxctx->guest_rcx),
890 (uint32_t*)(&vmxctx->guest_rdx));
895 vmx_run_trace(struct vmx *vmx, int vcpu)
898 VCPU_CTR1(vmx->vm, vcpu, "Resume execution at %#lx", vmcs_guest_rip());
903 vmx_exit_trace(struct vmx *vmx, int vcpu, uint64_t rip, uint32_t exit_reason,
907 VCPU_CTR3(vmx->vm, vcpu, "%s %s vmexit at 0x%0lx",
908 handled ? "handled" : "unhandled",
909 exit_reason_to_str(exit_reason), rip);
914 vmx_astpending_trace(struct vmx *vmx, int vcpu, uint64_t rip)
917 VCPU_CTR1(vmx->vm, vcpu, "astpending vmexit at 0x%0lx", rip);
922 vmx_set_pcpu_defaults(struct vmx *vmx, int vcpu)
925 struct vmxstate *vmxstate;
926 struct invvpid_desc invvpid_desc = { 0 };
928 vmxstate = &vmx->state[vcpu];
929 lastcpu = vmxstate->lastcpu;
930 vmxstate->lastcpu = curcpu;
932 if (lastcpu == curcpu)
935 vmm_stat_incr(vmx->vm, vcpu, VCPU_MIGRATIONS, 1);
937 vmcs_write(VMCS_HOST_TR_BASE, vmm_get_host_trbase());
938 vmcs_write(VMCS_HOST_GDTR_BASE, vmm_get_host_gdtrbase());
939 vmcs_write(VMCS_HOST_GS_BASE, vmm_get_host_gsbase());
942 * If we are using VPIDs then invalidate all mappings tagged with 'vpid'
944 * We do this because this vcpu was executing on a different host
945 * cpu when it last ran. We do not track whether it invalidated
946 * mappings associated with its 'vpid' during that run. So we must
947 * assume that the mappings associated with 'vpid' on 'curcpu' are
948 * stale and invalidate them.
950 * Note that we incur this penalty only when the scheduler chooses to
951 * move the thread associated with this vcpu between host cpus.
953 * Note also that this will invalidate mappings tagged with 'vpid'
956 if (vmxstate->vpid != 0) {
957 invvpid_desc.vpid = vmxstate->vpid;
958 invvpid(INVVPID_TYPE_SINGLE_CONTEXT, invvpid_desc);
963 * We depend on 'procbased_ctls' to have the Interrupt Window Exiting bit set.
965 CTASSERT((PROCBASED_CTLS_ONE_SETTING & PROCBASED_INT_WINDOW_EXITING) != 0);
968 vmx_set_int_window_exiting(struct vmx *vmx, int vcpu)
971 vmx->cap[vcpu].proc_ctls |= PROCBASED_INT_WINDOW_EXITING;
972 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
976 vmx_clear_int_window_exiting(struct vmx *vmx, int vcpu)
979 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_INT_WINDOW_EXITING;
980 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
984 vmx_set_nmi_window_exiting(struct vmx *vmx, int vcpu)
987 vmx->cap[vcpu].proc_ctls |= PROCBASED_NMI_WINDOW_EXITING;
988 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
992 vmx_clear_nmi_window_exiting(struct vmx *vmx, int vcpu)
995 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_NMI_WINDOW_EXITING;
996 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1000 vmx_inject_nmi(struct vmx *vmx, int vcpu)
1002 uint64_t info, interruptibility;
1004 /* Bail out if no NMI requested */
1005 if (!vm_nmi_pending(vmx->vm, vcpu))
1008 interruptibility = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1009 if (interruptibility & nmi_blocking_bits)
1013 * Inject the virtual NMI. The vector must be the NMI IDT entry
1014 * or the VMCS entry check will fail.
1016 info = VMCS_INTERRUPTION_INFO_NMI | VMCS_INTERRUPTION_INFO_VALID;
1018 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1020 VCPU_CTR0(vmx->vm, vcpu, "Injecting vNMI");
1022 /* Clear the request */
1023 vm_nmi_clear(vmx->vm, vcpu);
1028 * Set the NMI Window Exiting execution control so we can inject
1029 * the virtual NMI as soon as blocking condition goes away.
1031 vmx_set_nmi_window_exiting(vmx, vcpu);
1033 VCPU_CTR0(vmx->vm, vcpu, "Enabling NMI window exiting");
1038 vmx_inject_interrupts(struct vmx *vmx, int vcpu, struct vlapic *vlapic)
1041 uint64_t info, rflags, interruptibility;
1043 const int HWINTR_BLOCKED = VMCS_INTERRUPTIBILITY_STI_BLOCKING |
1044 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING;
1047 * If there is already an interrupt pending then just return.
1049 * This could happen if an interrupt was injected on a prior
1050 * VM entry but the actual entry into guest mode was aborted
1051 * because of a pending AST.
1053 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1054 if (info & VMCS_INTERRUPTION_INFO_VALID)
1058 * NMI injection has priority so deal with those first
1060 if (vmx_inject_nmi(vmx, vcpu))
1063 /* Ask the local apic for a vector to inject */
1064 vector = vlapic_pending_intr(vlapic);
1068 if (vector < 32 || vector > 255)
1069 panic("vmx_inject_interrupts: invalid vector %d\n", vector);
1071 /* Check RFLAGS.IF and the interruptibility state of the guest */
1072 rflags = vmcs_read(VMCS_GUEST_RFLAGS);
1073 if ((rflags & PSL_I) == 0)
1076 interruptibility = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1077 if (interruptibility & HWINTR_BLOCKED)
1080 /* Inject the interrupt */
1081 info = VMCS_INTERRUPTION_INFO_HW_INTR | VMCS_INTERRUPTION_INFO_VALID;
1083 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1085 /* Update the Local APIC ISR */
1086 vlapic_intr_accepted(vlapic, vector);
1088 VCPU_CTR1(vmx->vm, vcpu, "Injecting hwintr at vector %d", vector);
1094 * Set the Interrupt Window Exiting execution control so we can inject
1095 * the interrupt as soon as blocking condition goes away.
1097 vmx_set_int_window_exiting(vmx, vcpu);
1099 VCPU_CTR0(vmx->vm, vcpu, "Enabling interrupt window exiting");
1103 vmx_emulate_cr_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1105 int cr, vmcs_guest_cr, vmcs_shadow_cr;
1106 uint64_t crval, regval, ones_mask, zeros_mask;
1107 const struct vmxctx *vmxctx;
1109 /* We only handle mov to %cr0 or %cr4 at this time */
1110 if ((exitqual & 0xf0) != 0x00)
1113 cr = exitqual & 0xf;
1114 if (cr != 0 && cr != 4)
1117 vmxctx = &vmx->ctx[vcpu];
1120 * We must use vmcs_write() directly here because vmcs_setreg() will
1121 * call vmclear(vmcs) as a side-effect which we certainly don't want.
1123 switch ((exitqual >> 8) & 0xf) {
1125 regval = vmxctx->guest_rax;
1128 regval = vmxctx->guest_rcx;
1131 regval = vmxctx->guest_rdx;
1134 regval = vmxctx->guest_rbx;
1137 regval = vmcs_read(VMCS_GUEST_RSP);
1140 regval = vmxctx->guest_rbp;
1143 regval = vmxctx->guest_rsi;
1146 regval = vmxctx->guest_rdi;
1149 regval = vmxctx->guest_r8;
1152 regval = vmxctx->guest_r9;
1155 regval = vmxctx->guest_r10;
1158 regval = vmxctx->guest_r11;
1161 regval = vmxctx->guest_r12;
1164 regval = vmxctx->guest_r13;
1167 regval = vmxctx->guest_r14;
1170 regval = vmxctx->guest_r15;
1175 ones_mask = cr0_ones_mask;
1176 zeros_mask = cr0_zeros_mask;
1177 vmcs_guest_cr = VMCS_GUEST_CR0;
1178 vmcs_shadow_cr = VMCS_CR0_SHADOW;
1180 ones_mask = cr4_ones_mask;
1181 zeros_mask = cr4_zeros_mask;
1182 vmcs_guest_cr = VMCS_GUEST_CR4;
1183 vmcs_shadow_cr = VMCS_CR4_SHADOW;
1185 vmcs_write(vmcs_shadow_cr, regval);
1187 crval = regval | ones_mask;
1188 crval &= ~zeros_mask;
1189 vmcs_write(vmcs_guest_cr, crval);
1191 if (cr == 0 && regval & CR0_PG) {
1192 uint64_t efer, entry_ctls;
1195 * If CR0.PG is 1 and EFER.LME is 1 then EFER.LMA and
1196 * the "IA-32e mode guest" bit in VM-entry control must be
1199 efer = vmcs_read(VMCS_GUEST_IA32_EFER);
1200 if (efer & EFER_LME) {
1202 vmcs_write(VMCS_GUEST_IA32_EFER, efer);
1203 entry_ctls = vmcs_read(VMCS_ENTRY_CTLS);
1204 entry_ctls |= VM_ENTRY_GUEST_LMA;
1205 vmcs_write(VMCS_ENTRY_CTLS, entry_ctls);
1213 ept_fault_type(uint64_t ept_qual)
1217 if (ept_qual & EPT_VIOLATION_DATA_WRITE)
1218 fault_type = VM_PROT_WRITE;
1219 else if (ept_qual & EPT_VIOLATION_INST_FETCH)
1220 fault_type = VM_PROT_EXECUTE;
1222 fault_type= VM_PROT_READ;
1224 return (fault_type);
1228 ept_emulation_fault(uint64_t ept_qual)
1232 /* EPT fault on an instruction fetch doesn't make sense here */
1233 if (ept_qual & EPT_VIOLATION_INST_FETCH)
1236 /* EPT fault must be a read fault or a write fault */
1237 read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0;
1238 write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0;
1239 if ((read | write) == 0)
1243 * The EPT violation must have been caused by accessing a
1244 * guest-physical address that is a translation of a guest-linear
1247 if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 ||
1248 (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) {
1256 vmx_exit_process(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
1260 struct vmxctx *vmxctx;
1261 uint32_t eax, ecx, edx, idtvec_info, idtvec_err, reason;
1266 vmcs = &vmx->vmcs[vcpu];
1267 vmxctx = &vmx->ctx[vcpu];
1268 qual = vmexit->u.vmx.exit_qualification;
1269 reason = vmexit->u.vmx.exit_reason;
1270 vmexit->exitcode = VM_EXITCODE_BOGUS;
1272 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_COUNT, 1);
1275 * VM exits that could be triggered during event injection on the
1276 * previous VM entry need to be handled specially by re-injecting
1279 * See "Information for VM Exits During Event Delivery" in Intel SDM
1283 case EXIT_REASON_EPT_FAULT:
1284 case EXIT_REASON_EPT_MISCONFIG:
1285 case EXIT_REASON_APIC:
1286 case EXIT_REASON_TASK_SWITCH:
1287 case EXIT_REASON_EXCEPTION:
1288 idtvec_info = vmcs_idt_vectoring_info();
1289 if (idtvec_info & VMCS_IDT_VEC_VALID) {
1290 idtvec_info &= ~(1 << 12); /* clear undefined bit */
1291 vmcs_write(VMCS_ENTRY_INTR_INFO, idtvec_info);
1292 if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) {
1293 idtvec_err = vmcs_idt_vectoring_err();
1294 vmcs_write(VMCS_ENTRY_EXCEPTION_ERROR,
1297 vmcs_write(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length);
1304 case EXIT_REASON_CR_ACCESS:
1305 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CR_ACCESS, 1);
1306 handled = vmx_emulate_cr_access(vmx, vcpu, qual);
1308 case EXIT_REASON_RDMSR:
1309 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_RDMSR, 1);
1311 ecx = vmxctx->guest_rcx;
1312 error = emulate_rdmsr(vmx->vm, vcpu, ecx, &retu);
1314 vmexit->exitcode = VM_EXITCODE_RDMSR;
1315 vmexit->u.msr.code = ecx;
1319 /* Return to userspace with a valid exitcode */
1320 KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
1321 ("emulate_wrmsr retu with bogus exitcode"));
1324 case EXIT_REASON_WRMSR:
1325 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_WRMSR, 1);
1327 eax = vmxctx->guest_rax;
1328 ecx = vmxctx->guest_rcx;
1329 edx = vmxctx->guest_rdx;
1330 error = emulate_wrmsr(vmx->vm, vcpu, ecx,
1331 (uint64_t)edx << 32 | eax, &retu);
1333 vmexit->exitcode = VM_EXITCODE_WRMSR;
1334 vmexit->u.msr.code = ecx;
1335 vmexit->u.msr.wval = (uint64_t)edx << 32 | eax;
1339 /* Return to userspace with a valid exitcode */
1340 KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
1341 ("emulate_wrmsr retu with bogus exitcode"));
1344 case EXIT_REASON_HLT:
1345 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_HLT, 1);
1346 vmexit->exitcode = VM_EXITCODE_HLT;
1347 vmexit->u.hlt.rflags = vmcs_read(VMCS_GUEST_RFLAGS);
1349 case EXIT_REASON_MTF:
1350 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_MTRAP, 1);
1351 vmexit->exitcode = VM_EXITCODE_MTRAP;
1353 case EXIT_REASON_PAUSE:
1354 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_PAUSE, 1);
1355 vmexit->exitcode = VM_EXITCODE_PAUSE;
1357 case EXIT_REASON_INTR_WINDOW:
1358 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INTR_WINDOW, 1);
1359 vmx_clear_int_window_exiting(vmx, vcpu);
1360 VCPU_CTR0(vmx->vm, vcpu, "Disabling interrupt window exiting");
1362 case EXIT_REASON_EXT_INTR:
1364 * External interrupts serve only to cause VM exits and allow
1365 * the host interrupt handler to run.
1367 * If this external interrupt triggers a virtual interrupt
1368 * to a VM, then that state will be recorded by the
1369 * host interrupt handler in the VM's softc. We will inject
1370 * this virtual interrupt during the subsequent VM enter.
1374 * This is special. We want to treat this as an 'handled'
1375 * VM-exit but not increment the instruction pointer.
1377 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXTINT, 1);
1379 case EXIT_REASON_NMI_WINDOW:
1380 /* Exit to allow the pending virtual NMI to be injected */
1381 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NMI_WINDOW, 1);
1382 vmx_clear_nmi_window_exiting(vmx, vcpu);
1383 VCPU_CTR0(vmx->vm, vcpu, "Disabling NMI window exiting");
1385 case EXIT_REASON_INOUT:
1386 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INOUT, 1);
1387 vmexit->exitcode = VM_EXITCODE_INOUT;
1388 vmexit->u.inout.bytes = (qual & 0x7) + 1;
1389 vmexit->u.inout.in = (qual & 0x8) ? 1 : 0;
1390 vmexit->u.inout.string = (qual & 0x10) ? 1 : 0;
1391 vmexit->u.inout.rep = (qual & 0x20) ? 1 : 0;
1392 vmexit->u.inout.port = (uint16_t)(qual >> 16);
1393 vmexit->u.inout.eax = (uint32_t)(vmxctx->guest_rax);
1395 case EXIT_REASON_CPUID:
1396 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CPUID, 1);
1397 handled = vmx_handle_cpuid(vmx->vm, vcpu, vmxctx);
1399 case EXIT_REASON_EPT_FAULT:
1400 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EPT_FAULT, 1);
1402 * If 'gpa' lies within the address space allocated to
1403 * memory then this must be a nested page fault otherwise
1404 * this must be an instruction that accesses MMIO space.
1407 if (vm_mem_allocated(vmx->vm, gpa)) {
1408 vmexit->exitcode = VM_EXITCODE_PAGING;
1409 vmexit->u.paging.gpa = gpa;
1410 vmexit->u.paging.fault_type = ept_fault_type(qual);
1411 } else if (ept_emulation_fault(qual)) {
1412 vmexit->exitcode = VM_EXITCODE_INST_EMUL;
1413 vmexit->u.inst_emul.gpa = gpa;
1414 vmexit->u.inst_emul.gla = vmcs_gla();
1415 vmexit->u.inst_emul.cr3 = vmcs_guest_cr3();
1419 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_UNKNOWN, 1);
1425 * It is possible that control is returned to userland
1426 * even though we were able to handle the VM exit in the
1429 * In such a case we want to make sure that the userland
1430 * restarts guest execution at the instruction *after*
1431 * the one we just processed. Therefore we update the
1432 * guest rip in the VMCS and in 'vmexit'.
1434 vmexit->rip += vmexit->inst_length;
1435 vmexit->inst_length = 0;
1436 vmcs_write(VMCS_GUEST_RIP, vmexit->rip);
1438 if (vmexit->exitcode == VM_EXITCODE_BOGUS) {
1440 * If this VM exit was not claimed by anybody then
1441 * treat it as a generic VMX exit.
1443 vmexit->exitcode = VM_EXITCODE_VMX;
1444 vmexit->u.vmx.error = 0;
1447 * The exitcode and collateral have been populated.
1448 * The VM exit will be processed further in userland.
1456 vmx_run(void *arg, int vcpu, register_t rip, pmap_t pmap)
1458 int vie, rc, handled, astpending;
1459 uint32_t exit_reason;
1461 struct vmxctx *vmxctx;
1463 struct vm_exit *vmexit;
1464 struct vlapic *vlapic;
1467 vmcs = &vmx->vmcs[vcpu];
1468 vmxctx = &vmx->ctx[vcpu];
1469 vmxctx->launched = 0;
1470 vlapic = vm_lapic(vmx->vm, vcpu);
1473 vmexit = vm_exitinfo(vmx->vm, vcpu);
1475 KASSERT(vmxctx->pmap == pmap,
1476 ("pmap %p different than ctx pmap %p", pmap, vmxctx->pmap));
1477 KASSERT(vmxctx->eptp == vmx->eptp,
1478 ("eptp %p different than ctx eptp %#lx", eptp, vmxctx->eptp));
1481 * XXX Can we avoid doing this every time we do a vm run?
1487 * We do this every time because we may setup the virtual machine
1488 * from a different process than the one that actually runs it.
1490 * If the life of a virtual machine was spent entirely in the context
1491 * of a single process we could do this once in vmcs_set_defaults().
1493 vmcs_write(VMCS_HOST_CR3, rcr3());
1494 vmcs_write(VMCS_GUEST_RIP, rip);
1495 vmx_set_pcpu_defaults(vmx, vcpu);
1498 vmx_inject_interrupts(vmx, vcpu, vlapic);
1499 vmx_run_trace(vmx, vcpu);
1500 rc = vmx_setjmp(vmxctx);
1502 vmx_setjmp_trace(vmx, vcpu, vmxctx, rc);
1505 case VMX_RETURN_DIRECT:
1506 if (vmxctx->launched == 0) {
1507 vmxctx->launched = 1;
1511 panic("vmx_launch/resume should not return");
1513 case VMX_RETURN_LONGJMP:
1514 break; /* vm exit */
1515 case VMX_RETURN_AST:
1518 case VMX_RETURN_VMRESUME:
1519 vie = vmcs_instruction_error();
1520 if (vmxctx->launch_error == VM_FAIL_INVALID ||
1521 vie != VMRESUME_WITH_NON_LAUNCHED_VMCS) {
1522 printf("vmresume error %d vmcs inst error %d\n",
1523 vmxctx->launch_error, vie);
1526 vmx_launch(vmxctx); /* try to launch the guest */
1527 panic("vmx_launch should not return");
1529 case VMX_RETURN_VMLAUNCH:
1530 vie = vmcs_instruction_error();
1532 printf("vmlaunch error %d vmcs inst error %d\n",
1533 vmxctx->launch_error, vie);
1536 case VMX_RETURN_INVEPT:
1537 panic("vm %s:%d invept error %d",
1538 vm_name(vmx->vm), vcpu, vmxctx->launch_error);
1540 panic("vmx_setjmp returned %d", rc);
1543 /* enable interrupts */
1546 /* collect some basic information for VM exit processing */
1547 vmexit->rip = rip = vmcs_guest_rip();
1548 vmexit->inst_length = vmexit_instruction_length();
1549 vmexit->u.vmx.exit_reason = exit_reason = vmcs_exit_reason();
1550 vmexit->u.vmx.exit_qualification = vmcs_exit_qualification();
1554 vmexit->inst_length = 0;
1555 vmexit->exitcode = VM_EXITCODE_BOGUS;
1556 vmx_astpending_trace(vmx, vcpu, rip);
1557 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_ASTPENDING, 1);
1561 handled = vmx_exit_process(vmx, vcpu, vmexit);
1562 vmx_exit_trace(vmx, vcpu, rip, exit_reason, handled);
1567 * If a VM exit has been handled then the exitcode must be BOGUS
1568 * If a VM exit is not handled then the exitcode must not be BOGUS
1570 if ((handled && vmexit->exitcode != VM_EXITCODE_BOGUS) ||
1571 (!handled && vmexit->exitcode == VM_EXITCODE_BOGUS)) {
1572 panic("Mismatch between handled (%d) and exitcode (%d)",
1573 handled, vmexit->exitcode);
1577 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_USERSPACE, 1);
1579 VCPU_CTR1(vmx->vm, vcpu, "goto userland: exitcode %d",vmexit->exitcode);
1583 * We need to do this to ensure that any VMCS state cached by the
1584 * processor is flushed to memory. We need to do this in case the
1585 * VM moves to a different cpu the next time it runs.
1587 * Can we avoid doing this?
1593 vmexit->exitcode = VM_EXITCODE_VMX;
1594 vmexit->u.vmx.exit_reason = (uint32_t)-1;
1595 vmexit->u.vmx.exit_qualification = (uint32_t)-1;
1596 vmexit->u.vmx.error = vie;
1602 vmx_vmcleanup(void *arg)
1605 struct vmx *vmx = arg;
1607 for (i = 0; i < VM_MAXCPU; i++)
1608 vpid_free(vmx->state[i].vpid);
1611 * XXXSMP we also need to clear the VMCS active on the other vcpus.
1613 error = vmclear(&vmx->vmcs[0]);
1615 panic("vmx_vmcleanup: vmclear error %d on vcpu 0", error);
1623 vmxctx_regptr(struct vmxctx *vmxctx, int reg)
1627 case VM_REG_GUEST_RAX:
1628 return (&vmxctx->guest_rax);
1629 case VM_REG_GUEST_RBX:
1630 return (&vmxctx->guest_rbx);
1631 case VM_REG_GUEST_RCX:
1632 return (&vmxctx->guest_rcx);
1633 case VM_REG_GUEST_RDX:
1634 return (&vmxctx->guest_rdx);
1635 case VM_REG_GUEST_RSI:
1636 return (&vmxctx->guest_rsi);
1637 case VM_REG_GUEST_RDI:
1638 return (&vmxctx->guest_rdi);
1639 case VM_REG_GUEST_RBP:
1640 return (&vmxctx->guest_rbp);
1641 case VM_REG_GUEST_R8:
1642 return (&vmxctx->guest_r8);
1643 case VM_REG_GUEST_R9:
1644 return (&vmxctx->guest_r9);
1645 case VM_REG_GUEST_R10:
1646 return (&vmxctx->guest_r10);
1647 case VM_REG_GUEST_R11:
1648 return (&vmxctx->guest_r11);
1649 case VM_REG_GUEST_R12:
1650 return (&vmxctx->guest_r12);
1651 case VM_REG_GUEST_R13:
1652 return (&vmxctx->guest_r13);
1653 case VM_REG_GUEST_R14:
1654 return (&vmxctx->guest_r14);
1655 case VM_REG_GUEST_R15:
1656 return (&vmxctx->guest_r15);
1664 vmxctx_getreg(struct vmxctx *vmxctx, int reg, uint64_t *retval)
1668 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
1676 vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val)
1680 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
1688 vmx_shadow_reg(int reg)
1695 case VM_REG_GUEST_CR0:
1696 shreg = VMCS_CR0_SHADOW;
1698 case VM_REG_GUEST_CR4:
1699 shreg = VMCS_CR4_SHADOW;
1709 vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval)
1711 int running, hostcpu;
1712 struct vmx *vmx = arg;
1714 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
1715 if (running && hostcpu != curcpu)
1716 panic("vmx_getreg: %s%d is running", vm_name(vmx->vm), vcpu);
1718 if (vmxctx_getreg(&vmx->ctx[vcpu], reg, retval) == 0)
1721 return (vmcs_getreg(&vmx->vmcs[vcpu], running, reg, retval));
1725 vmx_setreg(void *arg, int vcpu, int reg, uint64_t val)
1727 int error, hostcpu, running, shadow;
1729 struct vmx *vmx = arg;
1731 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
1732 if (running && hostcpu != curcpu)
1733 panic("vmx_setreg: %s%d is running", vm_name(vmx->vm), vcpu);
1735 if (vmxctx_setreg(&vmx->ctx[vcpu], reg, val) == 0)
1738 error = vmcs_setreg(&vmx->vmcs[vcpu], running, reg, val);
1742 * If the "load EFER" VM-entry control is 1 then the
1743 * value of EFER.LMA must be identical to "IA-32e mode guest"
1744 * bit in the VM-entry control.
1746 if ((entry_ctls & VM_ENTRY_LOAD_EFER) != 0 &&
1747 (reg == VM_REG_GUEST_EFER)) {
1748 vmcs_getreg(&vmx->vmcs[vcpu], running,
1749 VMCS_IDENT(VMCS_ENTRY_CTLS), &ctls);
1751 ctls |= VM_ENTRY_GUEST_LMA;
1753 ctls &= ~VM_ENTRY_GUEST_LMA;
1754 vmcs_setreg(&vmx->vmcs[vcpu], running,
1755 VMCS_IDENT(VMCS_ENTRY_CTLS), ctls);
1758 shadow = vmx_shadow_reg(reg);
1761 * Store the unmodified value in the shadow
1763 error = vmcs_setreg(&vmx->vmcs[vcpu], running,
1764 VMCS_IDENT(shadow), val);
1772 vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
1774 struct vmx *vmx = arg;
1776 return (vmcs_getdesc(&vmx->vmcs[vcpu], reg, desc));
1780 vmx_setdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
1782 struct vmx *vmx = arg;
1784 return (vmcs_setdesc(&vmx->vmcs[vcpu], reg, desc));
1788 vmx_inject(void *arg, int vcpu, int type, int vector, uint32_t code,
1793 struct vmx *vmx = arg;
1794 struct vmcs *vmcs = &vmx->vmcs[vcpu];
1796 static uint32_t type_map[VM_EVENT_MAX] = {
1797 0x1, /* VM_EVENT_NONE */
1798 0x0, /* VM_HW_INTR */
1800 0x3, /* VM_HW_EXCEPTION */
1801 0x4, /* VM_SW_INTR */
1802 0x5, /* VM_PRIV_SW_EXCEPTION */
1803 0x6, /* VM_SW_EXCEPTION */
1807 * If there is already an exception pending to be delivered to the
1808 * vcpu then just return.
1810 error = vmcs_getreg(vmcs, 0, VMCS_IDENT(VMCS_ENTRY_INTR_INFO), &info);
1814 if (info & VMCS_INTERRUPTION_INFO_VALID)
1817 info = vector | (type_map[type] << 8) | (code_valid ? 1 << 11 : 0);
1818 info |= VMCS_INTERRUPTION_INFO_VALID;
1819 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(VMCS_ENTRY_INTR_INFO), info);
1824 error = vmcs_setreg(vmcs, 0,
1825 VMCS_IDENT(VMCS_ENTRY_EXCEPTION_ERROR),
1832 vmx_getcap(void *arg, int vcpu, int type, int *retval)
1834 struct vmx *vmx = arg;
1840 vcap = vmx->cap[vcpu].set;
1843 case VM_CAP_HALT_EXIT:
1847 case VM_CAP_PAUSE_EXIT:
1851 case VM_CAP_MTRAP_EXIT:
1852 if (cap_monitor_trap)
1855 case VM_CAP_UNRESTRICTED_GUEST:
1856 if (cap_unrestricted_guest)
1859 case VM_CAP_ENABLE_INVPCID:
1868 *retval = (vcap & (1 << type)) ? 1 : 0;
1874 vmx_setcap(void *arg, int vcpu, int type, int val)
1876 struct vmx *vmx = arg;
1877 struct vmcs *vmcs = &vmx->vmcs[vcpu];
1889 case VM_CAP_HALT_EXIT:
1890 if (cap_halt_exit) {
1892 pptr = &vmx->cap[vcpu].proc_ctls;
1894 flag = PROCBASED_HLT_EXITING;
1895 reg = VMCS_PRI_PROC_BASED_CTLS;
1898 case VM_CAP_MTRAP_EXIT:
1899 if (cap_monitor_trap) {
1901 pptr = &vmx->cap[vcpu].proc_ctls;
1903 flag = PROCBASED_MTF;
1904 reg = VMCS_PRI_PROC_BASED_CTLS;
1907 case VM_CAP_PAUSE_EXIT:
1908 if (cap_pause_exit) {
1910 pptr = &vmx->cap[vcpu].proc_ctls;
1912 flag = PROCBASED_PAUSE_EXITING;
1913 reg = VMCS_PRI_PROC_BASED_CTLS;
1916 case VM_CAP_UNRESTRICTED_GUEST:
1917 if (cap_unrestricted_guest) {
1919 pptr = &vmx->cap[vcpu].proc_ctls2;
1921 flag = PROCBASED2_UNRESTRICTED_GUEST;
1922 reg = VMCS_SEC_PROC_BASED_CTLS;
1925 case VM_CAP_ENABLE_INVPCID:
1928 pptr = &vmx->cap[vcpu].proc_ctls2;
1930 flag = PROCBASED2_ENABLE_INVPCID;
1931 reg = VMCS_SEC_PROC_BASED_CTLS;
1945 error = vmwrite(reg, baseval);
1952 * Update optional stored flags, and record
1960 vmx->cap[vcpu].set |= (1 << type);
1962 vmx->cap[vcpu].set &= ~(1 << type);
1970 static struct vlapic *
1971 vmx_vlapic_init(void *arg, int vcpuid)
1974 struct vlapic *vlapic;
1978 vlapic = malloc(sizeof(struct vlapic), M_VLAPIC, M_WAITOK | M_ZERO);
1979 vlapic->vm = vmx->vm;
1980 vlapic->vcpuid = vcpuid;
1981 vlapic->apic_page = (struct LAPIC *)&vmx->apic_page[vcpuid];
1983 vlapic_init(vlapic);
1989 vmx_vlapic_cleanup(void *arg, struct vlapic *vlapic)
1992 vlapic_cleanup(vlapic);
1993 free(vlapic, M_VLAPIC);
1996 struct vmm_ops vmm_ops_intel = {