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>
53 #include "vmm_lapic.h"
60 #include "vmx_cpufunc.h"
63 #include "vmx_controls.h"
65 #define PINBASED_CTLS_ONE_SETTING \
66 (PINBASED_EXTINT_EXITING | \
67 PINBASED_NMI_EXITING | \
69 #define PINBASED_CTLS_ZERO_SETTING 0
71 #define PROCBASED_CTLS_WINDOW_SETTING \
72 (PROCBASED_INT_WINDOW_EXITING | \
73 PROCBASED_NMI_WINDOW_EXITING)
75 #define PROCBASED_CTLS_ONE_SETTING \
76 (PROCBASED_SECONDARY_CONTROLS | \
77 PROCBASED_IO_EXITING | \
78 PROCBASED_MSR_BITMAPS | \
79 PROCBASED_CTLS_WINDOW_SETTING)
80 #define PROCBASED_CTLS_ZERO_SETTING \
81 (PROCBASED_CR3_LOAD_EXITING | \
82 PROCBASED_CR3_STORE_EXITING | \
85 #define PROCBASED_CTLS2_ONE_SETTING PROCBASED2_ENABLE_EPT
86 #define PROCBASED_CTLS2_ZERO_SETTING 0
88 #define VM_EXIT_CTLS_ONE_SETTING_NO_PAT \
93 #define VM_EXIT_CTLS_ONE_SETTING \
94 (VM_EXIT_CTLS_ONE_SETTING_NO_PAT | \
97 #define VM_EXIT_CTLS_ZERO_SETTING VM_EXIT_SAVE_DEBUG_CONTROLS
99 #define VM_ENTRY_CTLS_ONE_SETTING_NO_PAT VM_ENTRY_LOAD_EFER
101 #define VM_ENTRY_CTLS_ONE_SETTING \
102 (VM_ENTRY_CTLS_ONE_SETTING_NO_PAT | \
104 #define VM_ENTRY_CTLS_ZERO_SETTING \
105 (VM_ENTRY_LOAD_DEBUG_CONTROLS | \
106 VM_ENTRY_INTO_SMM | \
107 VM_ENTRY_DEACTIVATE_DUAL_MONITOR)
109 #define guest_msr_rw(vmx, msr) \
110 msr_bitmap_change_access((vmx)->msr_bitmap, (msr), MSR_BITMAP_ACCESS_RW)
115 MALLOC_DEFINE(M_VMX, "vmx", "vmx");
117 SYSCTL_DECL(_hw_vmm);
118 SYSCTL_NODE(_hw_vmm, OID_AUTO, vmx, CTLFLAG_RW, NULL, NULL);
120 int vmxon_enabled[MAXCPU];
121 static char vmxon_region[MAXCPU][PAGE_SIZE] __aligned(PAGE_SIZE);
123 static uint32_t pinbased_ctls, procbased_ctls, procbased_ctls2;
124 static uint32_t exit_ctls, entry_ctls;
126 static uint64_t cr0_ones_mask, cr0_zeros_mask;
127 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_ones_mask, CTLFLAG_RD,
128 &cr0_ones_mask, 0, NULL);
129 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_zeros_mask, CTLFLAG_RD,
130 &cr0_zeros_mask, 0, NULL);
132 static uint64_t cr4_ones_mask, cr4_zeros_mask;
133 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_ones_mask, CTLFLAG_RD,
134 &cr4_ones_mask, 0, NULL);
135 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_zeros_mask, CTLFLAG_RD,
136 &cr4_zeros_mask, 0, NULL);
138 static int vmx_no_patmsr;
140 static int vmx_initialized;
141 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, initialized, CTLFLAG_RD,
142 &vmx_initialized, 0, "Intel VMX initialized");
145 * Virtual NMI blocking conditions.
147 * Some processor implementations also require NMI to be blocked if
148 * the STI_BLOCKING bit is set. It is possible to detect this at runtime
149 * based on the (exit_reason,exit_qual) tuple being set to
150 * (EXIT_REASON_INVAL_VMCS, EXIT_QUAL_NMI_WHILE_STI_BLOCKING).
152 * We take the easy way out and also include STI_BLOCKING as one of the
153 * gating items for vNMI injection.
155 static uint64_t nmi_blocking_bits = VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING |
156 VMCS_INTERRUPTIBILITY_NMI_BLOCKING |
157 VMCS_INTERRUPTIBILITY_STI_BLOCKING;
160 * Optional capabilities
162 static int cap_halt_exit;
163 static int cap_pause_exit;
164 static int cap_unrestricted_guest;
165 static int cap_monitor_trap;
166 static int cap_invpcid;
168 static struct unrhdr *vpid_unr;
169 static u_int vpid_alloc_failed;
170 SYSCTL_UINT(_hw_vmm_vmx, OID_AUTO, vpid_alloc_failed, CTLFLAG_RD,
171 &vpid_alloc_failed, 0, NULL);
175 exit_reason_to_str(int reason)
177 static char reasonbuf[32];
180 case EXIT_REASON_EXCEPTION:
182 case EXIT_REASON_EXT_INTR:
184 case EXIT_REASON_TRIPLE_FAULT:
185 return "triplefault";
186 case EXIT_REASON_INIT:
188 case EXIT_REASON_SIPI:
190 case EXIT_REASON_IO_SMI:
192 case EXIT_REASON_SMI:
194 case EXIT_REASON_INTR_WINDOW:
196 case EXIT_REASON_NMI_WINDOW:
198 case EXIT_REASON_TASK_SWITCH:
200 case EXIT_REASON_CPUID:
202 case EXIT_REASON_GETSEC:
204 case EXIT_REASON_HLT:
206 case EXIT_REASON_INVD:
208 case EXIT_REASON_INVLPG:
210 case EXIT_REASON_RDPMC:
212 case EXIT_REASON_RDTSC:
214 case EXIT_REASON_RSM:
216 case EXIT_REASON_VMCALL:
218 case EXIT_REASON_VMCLEAR:
220 case EXIT_REASON_VMLAUNCH:
222 case EXIT_REASON_VMPTRLD:
224 case EXIT_REASON_VMPTRST:
226 case EXIT_REASON_VMREAD:
228 case EXIT_REASON_VMRESUME:
230 case EXIT_REASON_VMWRITE:
232 case EXIT_REASON_VMXOFF:
234 case EXIT_REASON_VMXON:
236 case EXIT_REASON_CR_ACCESS:
238 case EXIT_REASON_DR_ACCESS:
240 case EXIT_REASON_INOUT:
242 case EXIT_REASON_RDMSR:
244 case EXIT_REASON_WRMSR:
246 case EXIT_REASON_INVAL_VMCS:
248 case EXIT_REASON_INVAL_MSR:
250 case EXIT_REASON_MWAIT:
252 case EXIT_REASON_MTF:
254 case EXIT_REASON_MONITOR:
256 case EXIT_REASON_PAUSE:
258 case EXIT_REASON_MCE:
260 case EXIT_REASON_TPR:
262 case EXIT_REASON_APIC:
264 case EXIT_REASON_GDTR_IDTR:
266 case EXIT_REASON_LDTR_TR:
268 case EXIT_REASON_EPT_FAULT:
270 case EXIT_REASON_EPT_MISCONFIG:
271 return "eptmisconfig";
272 case EXIT_REASON_INVEPT:
274 case EXIT_REASON_RDTSCP:
276 case EXIT_REASON_VMX_PREEMPT:
278 case EXIT_REASON_INVVPID:
280 case EXIT_REASON_WBINVD:
282 case EXIT_REASON_XSETBV:
285 snprintf(reasonbuf, sizeof(reasonbuf), "%d", reason);
292 vmx_setjmp_rc2str(int rc)
295 case VMX_RETURN_DIRECT:
297 case VMX_RETURN_LONGJMP:
299 case VMX_RETURN_VMRESUME:
301 case VMX_RETURN_VMLAUNCH:
310 #define SETJMP_TRACE(vmx, vcpu, vmxctx, regname) \
311 VCPU_CTR1((vmx)->vm, (vcpu), "setjmp trace " #regname " 0x%016lx", \
315 vmx_setjmp_trace(struct vmx *vmx, int vcpu, struct vmxctx *vmxctx, int rc)
317 uint64_t host_rip, host_rsp;
319 if (vmxctx != &vmx->ctx[vcpu])
320 panic("vmx_setjmp_trace: invalid vmxctx %p; should be %p",
321 vmxctx, &vmx->ctx[vcpu]);
323 VCPU_CTR1((vmx)->vm, (vcpu), "vmxctx = %p", vmxctx);
324 VCPU_CTR2((vmx)->vm, (vcpu), "setjmp return code %s(%d)",
325 vmx_setjmp_rc2str(rc), rc);
327 host_rip = vmcs_read(VMCS_HOST_RIP);
328 host_rsp = vmcs_read(VMCS_HOST_RSP);
329 VCPU_CTR2((vmx)->vm, (vcpu), "vmcs host_rip 0x%016lx, host_rsp %#lx",
332 SETJMP_TRACE(vmx, vcpu, vmxctx, host_r15);
333 SETJMP_TRACE(vmx, vcpu, vmxctx, host_r14);
334 SETJMP_TRACE(vmx, vcpu, vmxctx, host_r13);
335 SETJMP_TRACE(vmx, vcpu, vmxctx, host_r12);
336 SETJMP_TRACE(vmx, vcpu, vmxctx, host_rbp);
337 SETJMP_TRACE(vmx, vcpu, vmxctx, host_rsp);
338 SETJMP_TRACE(vmx, vcpu, vmxctx, host_rbx);
339 SETJMP_TRACE(vmx, vcpu, vmxctx, host_rip);
341 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rdi);
342 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rsi);
343 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rdx);
344 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rcx);
345 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r8);
346 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r9);
347 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rax);
348 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rbx);
349 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rbp);
350 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r10);
351 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r11);
352 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r12);
353 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r13);
354 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r14);
355 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r15);
356 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_cr2);
361 vmx_setjmp_trace(struct vmx *vmx, int vcpu, struct vmxctx *vmxctx, int rc)
368 vmx_fix_cr0(u_long cr0)
371 return ((cr0 | cr0_ones_mask) & ~cr0_zeros_mask);
375 vmx_fix_cr4(u_long cr4)
378 return ((cr4 | cr4_ones_mask) & ~cr4_zeros_mask);
384 if (vpid < 0 || vpid > 0xffff)
385 panic("vpid_free: invalid vpid %d", vpid);
388 * VPIDs [0,VM_MAXCPU] are special and are not allocated from
389 * the unit number allocator.
392 if (vpid > VM_MAXCPU)
393 free_unr(vpid_unr, vpid);
397 vpid_alloc(uint16_t *vpid, int num)
401 if (num <= 0 || num > VM_MAXCPU)
402 panic("invalid number of vpids requested: %d", num);
405 * If the "enable vpid" execution control is not enabled then the
406 * VPID is required to be 0 for all vcpus.
408 if ((procbased_ctls2 & PROCBASED2_ENABLE_VPID) == 0) {
409 for (i = 0; i < num; i++)
415 * Allocate a unique VPID for each vcpu from the unit number allocator.
417 for (i = 0; i < num; i++) {
418 x = alloc_unr(vpid_unr);
426 atomic_add_int(&vpid_alloc_failed, 1);
429 * If the unit number allocator does not have enough unique
430 * VPIDs then we need to allocate from the [1,VM_MAXCPU] range.
432 * These VPIDs are not be unique across VMs but this does not
433 * affect correctness because the combined mappings are also
434 * tagged with the EP4TA which is unique for each VM.
436 * It is still sub-optimal because the invvpid will invalidate
437 * combined mappings for a particular VPID across all EP4TAs.
442 for (i = 0; i < num; i++)
451 * VPID 0 is required when the "enable VPID" execution control is
454 * VPIDs [1,VM_MAXCPU] are used as the "overflow namespace" when the
455 * unit number allocator does not have sufficient unique VPIDs to
456 * satisfy the allocation.
458 * The remaining VPIDs are managed by the unit number allocator.
460 vpid_unr = new_unrhdr(VM_MAXCPU + 1, 0xffff, NULL);
464 msr_save_area_init(struct msr_entry *g_area, int *g_count)
468 static struct msr_entry guest_msrs[] = {
469 { MSR_KGSBASE, 0, 0 },
472 cnt = sizeof(guest_msrs) / sizeof(guest_msrs[0]);
473 if (cnt > GUEST_MSR_MAX_ENTRIES)
474 panic("guest msr save area overrun");
475 bcopy(guest_msrs, g_area, sizeof(guest_msrs));
480 vmx_disable(void *arg __unused)
482 struct invvpid_desc invvpid_desc = { 0 };
483 struct invept_desc invept_desc = { 0 };
485 if (vmxon_enabled[curcpu]) {
487 * See sections 25.3.3.3 and 25.3.3.4 in Intel Vol 3b.
489 * VMXON or VMXOFF are not required to invalidate any TLB
490 * caching structures. This prevents potential retention of
491 * cached information in the TLB between distinct VMX episodes.
493 invvpid(INVVPID_TYPE_ALL_CONTEXTS, invvpid_desc);
494 invept(INVEPT_TYPE_ALL_CONTEXTS, invept_desc);
497 load_cr4(rcr4() & ~CR4_VMXE);
504 if (vpid_unr != NULL) {
505 delete_unrhdr(vpid_unr);
509 smp_rendezvous(NULL, vmx_disable, NULL, NULL);
515 vmx_enable(void *arg __unused)
519 load_cr4(rcr4() | CR4_VMXE);
521 *(uint32_t *)vmxon_region[curcpu] = vmx_revision();
522 error = vmxon(vmxon_region[curcpu]);
524 vmxon_enabled[curcpu] = 1;
531 uint64_t fixed0, fixed1, feature_control;
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,
670 printf("vmx_init: ept initialization failed (%d)\n", error);
675 * Stash the cr0 and cr4 bits that must be fixed to 0 or 1
677 fixed0 = rdmsr(MSR_VMX_CR0_FIXED0);
678 fixed1 = rdmsr(MSR_VMX_CR0_FIXED1);
679 cr0_ones_mask = fixed0 & fixed1;
680 cr0_zeros_mask = ~fixed0 & ~fixed1;
683 * CR0_PE and CR0_PG can be set to zero in VMX non-root operation
684 * if unrestricted guest execution is allowed.
686 if (cap_unrestricted_guest)
687 cr0_ones_mask &= ~(CR0_PG | CR0_PE);
690 * Do not allow the guest to set CR0_NW or CR0_CD.
692 cr0_zeros_mask |= (CR0_NW | CR0_CD);
694 fixed0 = rdmsr(MSR_VMX_CR4_FIXED0);
695 fixed1 = rdmsr(MSR_VMX_CR4_FIXED1);
696 cr4_ones_mask = fixed0 & fixed1;
697 cr4_zeros_mask = ~fixed0 & ~fixed1;
701 /* enable VMX operation */
702 smp_rendezvous(NULL, vmx_enable, NULL, NULL);
710 vmx_setup_cr_shadow(int which, struct vmcs *vmcs, uint32_t initial)
712 int error, mask_ident, shadow_ident;
715 if (which != 0 && which != 4)
716 panic("vmx_setup_cr_shadow: unknown cr%d", which);
719 mask_ident = VMCS_CR0_MASK;
720 mask_value = cr0_ones_mask | cr0_zeros_mask;
721 shadow_ident = VMCS_CR0_SHADOW;
723 mask_ident = VMCS_CR4_MASK;
724 mask_value = cr4_ones_mask | cr4_zeros_mask;
725 shadow_ident = VMCS_CR4_SHADOW;
728 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(mask_ident), mask_value);
732 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(shadow_ident), initial);
738 #define vmx_setup_cr0_shadow(vmcs,init) vmx_setup_cr_shadow(0, (vmcs), (init))
739 #define vmx_setup_cr4_shadow(vmcs,init) vmx_setup_cr_shadow(4, (vmcs), (init))
742 vmx_vminit(struct vm *vm, pmap_t pmap)
744 uint16_t vpid[VM_MAXCPU];
745 int i, error, guest_msr_count;
748 vmx = malloc(sizeof(struct vmx), M_VMX, M_WAITOK | M_ZERO);
749 if ((uintptr_t)vmx & PAGE_MASK) {
750 panic("malloc of struct vmx not aligned on %d byte boundary",
755 vmx->eptp = eptp(vtophys((vm_offset_t)pmap->pm_pml4));
758 * Clean up EPTP-tagged guest physical and combined mappings
760 * VMX transitions are not required to invalidate any guest physical
761 * mappings. So, it may be possible for stale guest physical mappings
762 * to be present in the processor TLBs.
764 * Combined mappings for this EP4TA are also invalidated for all VPIDs.
766 ept_invalidate_mappings(vmx->eptp);
768 msr_bitmap_initialize(vmx->msr_bitmap);
771 * It is safe to allow direct access to MSR_GSBASE and MSR_FSBASE.
772 * The guest FSBASE and GSBASE are saved and restored during
773 * vm-exit and vm-entry respectively. The host FSBASE and GSBASE are
774 * always restored from the vmcs host state area on vm-exit.
776 * The SYSENTER_CS/ESP/EIP MSRs are identical to FS/GSBASE in
777 * how they are saved/restored so can be directly accessed by the
780 * Guest KGSBASE is saved and restored in the guest MSR save area.
781 * Host KGSBASE is restored before returning to userland from the pcb.
782 * There will be a window of time when we are executing in the host
783 * kernel context with a value of KGSBASE from the guest. This is ok
784 * because the value of KGSBASE is inconsequential in kernel context.
786 * MSR_EFER is saved and restored in the guest VMCS area on a
787 * VM exit and entry respectively. It is also restored from the
788 * host VMCS area on a VM exit.
790 if (guest_msr_rw(vmx, MSR_GSBASE) ||
791 guest_msr_rw(vmx, MSR_FSBASE) ||
792 guest_msr_rw(vmx, MSR_SYSENTER_CS_MSR) ||
793 guest_msr_rw(vmx, MSR_SYSENTER_ESP_MSR) ||
794 guest_msr_rw(vmx, MSR_SYSENTER_EIP_MSR) ||
795 guest_msr_rw(vmx, MSR_KGSBASE) ||
796 guest_msr_rw(vmx, MSR_EFER))
797 panic("vmx_vminit: error setting guest msr access");
800 * MSR_PAT is saved and restored in the guest VMCS are on a VM exit
801 * and entry respectively. It is also restored from the host VMCS
802 * area on a VM exit. However, if running on a system with no
803 * MSR_PAT save/restore support, leave access disabled so accesses
806 if (!vmx_no_patmsr && guest_msr_rw(vmx, MSR_PAT))
807 panic("vmx_vminit: error setting guest pat msr access");
809 vpid_alloc(vpid, VM_MAXCPU);
811 for (i = 0; i < VM_MAXCPU; i++) {
812 vmx->vmcs[i].identifier = vmx_revision();
813 error = vmclear(&vmx->vmcs[i]);
815 panic("vmx_vminit: vmclear error %d on vcpu %d\n",
819 error = vmcs_set_defaults(&vmx->vmcs[i],
821 (u_long)&vmx->ctx[i],
826 exit_ctls, entry_ctls,
827 vtophys(vmx->msr_bitmap),
831 panic("vmx_vminit: vmcs_set_defaults error %d", error);
834 vmx->cap[i].proc_ctls = procbased_ctls;
835 vmx->cap[i].proc_ctls2 = procbased_ctls2;
837 vmx->state[i].lastcpu = -1;
838 vmx->state[i].vpid = vpid[i];
840 msr_save_area_init(vmx->guest_msrs[i], &guest_msr_count);
842 error = vmcs_set_msr_save(&vmx->vmcs[i],
843 vtophys(vmx->guest_msrs[i]),
846 panic("vmcs_set_msr_save error %d", error);
849 * Set up the CR0/4 shadows, and init the read shadow
850 * to the power-on register value from the Intel Sys Arch.
854 error = vmx_setup_cr0_shadow(&vmx->vmcs[i], 0x60000010);
856 panic("vmx_setup_cr0_shadow %d", error);
858 error = vmx_setup_cr4_shadow(&vmx->vmcs[i], 0);
860 panic("vmx_setup_cr4_shadow %d", error);
862 vmx->ctx[i].pmap = pmap;
863 vmx->ctx[i].eptp = vmx->eptp;
870 vmx_handle_cpuid(struct vm *vm, int vcpu, struct vmxctx *vmxctx)
874 func = vmxctx->guest_rax;
876 handled = x86_emulate_cpuid(vm, vcpu,
877 (uint32_t*)(&vmxctx->guest_rax),
878 (uint32_t*)(&vmxctx->guest_rbx),
879 (uint32_t*)(&vmxctx->guest_rcx),
880 (uint32_t*)(&vmxctx->guest_rdx));
885 vmx_run_trace(struct vmx *vmx, int vcpu)
888 VCPU_CTR1(vmx->vm, vcpu, "Resume execution at %#lx", vmcs_guest_rip());
893 vmx_exit_trace(struct vmx *vmx, int vcpu, uint64_t rip, uint32_t exit_reason,
897 VCPU_CTR3(vmx->vm, vcpu, "%s %s vmexit at 0x%0lx",
898 handled ? "handled" : "unhandled",
899 exit_reason_to_str(exit_reason), rip);
904 vmx_astpending_trace(struct vmx *vmx, int vcpu, uint64_t rip)
907 VCPU_CTR1(vmx->vm, vcpu, "astpending vmexit at 0x%0lx", rip);
912 vmx_set_pcpu_defaults(struct vmx *vmx, int vcpu)
915 struct vmxstate *vmxstate;
916 struct invvpid_desc invvpid_desc = { 0 };
918 vmxstate = &vmx->state[vcpu];
919 lastcpu = vmxstate->lastcpu;
920 vmxstate->lastcpu = curcpu;
922 if (lastcpu == curcpu)
925 vmm_stat_incr(vmx->vm, vcpu, VCPU_MIGRATIONS, 1);
927 vmcs_write(VMCS_HOST_TR_BASE, vmm_get_host_trbase());
928 vmcs_write(VMCS_HOST_GDTR_BASE, vmm_get_host_gdtrbase());
929 vmcs_write(VMCS_HOST_GS_BASE, vmm_get_host_gsbase());
932 * If we are using VPIDs then invalidate all mappings tagged with 'vpid'
934 * We do this because this vcpu was executing on a different host
935 * cpu when it last ran. We do not track whether it invalidated
936 * mappings associated with its 'vpid' during that run. So we must
937 * assume that the mappings associated with 'vpid' on 'curcpu' are
938 * stale and invalidate them.
940 * Note that we incur this penalty only when the scheduler chooses to
941 * move the thread associated with this vcpu between host cpus.
943 * Note also that this will invalidate mappings tagged with 'vpid'
946 if (vmxstate->vpid != 0) {
947 invvpid_desc.vpid = vmxstate->vpid;
948 invvpid(INVVPID_TYPE_SINGLE_CONTEXT, invvpid_desc);
953 * We depend on 'procbased_ctls' to have the Interrupt Window Exiting bit set.
955 CTASSERT((PROCBASED_CTLS_ONE_SETTING & PROCBASED_INT_WINDOW_EXITING) != 0);
958 vmx_set_int_window_exiting(struct vmx *vmx, int vcpu)
961 vmx->cap[vcpu].proc_ctls |= PROCBASED_INT_WINDOW_EXITING;
962 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
966 vmx_clear_int_window_exiting(struct vmx *vmx, int vcpu)
969 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_INT_WINDOW_EXITING;
970 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
974 vmx_set_nmi_window_exiting(struct vmx *vmx, int vcpu)
977 vmx->cap[vcpu].proc_ctls |= PROCBASED_NMI_WINDOW_EXITING;
978 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
982 vmx_clear_nmi_window_exiting(struct vmx *vmx, int vcpu)
985 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_NMI_WINDOW_EXITING;
986 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
990 vmx_inject_nmi(struct vmx *vmx, int vcpu)
992 uint64_t info, interruptibility;
994 /* Bail out if no NMI requested */
995 if (!vm_nmi_pending(vmx->vm, vcpu))
998 interruptibility = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
999 if (interruptibility & nmi_blocking_bits)
1003 * Inject the virtual NMI. The vector must be the NMI IDT entry
1004 * or the VMCS entry check will fail.
1006 info = VMCS_INTERRUPTION_INFO_NMI | VMCS_INTERRUPTION_INFO_VALID;
1008 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1010 VCPU_CTR0(vmx->vm, vcpu, "Injecting vNMI");
1012 /* Clear the request */
1013 vm_nmi_clear(vmx->vm, vcpu);
1018 * Set the NMI Window Exiting execution control so we can inject
1019 * the virtual NMI as soon as blocking condition goes away.
1021 vmx_set_nmi_window_exiting(vmx, vcpu);
1023 VCPU_CTR0(vmx->vm, vcpu, "Enabling NMI window exiting");
1028 vmx_inject_interrupts(struct vmx *vmx, int vcpu)
1031 uint64_t info, rflags, interruptibility;
1033 const int HWINTR_BLOCKED = VMCS_INTERRUPTIBILITY_STI_BLOCKING |
1034 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING;
1037 * If there is already an interrupt pending then just return.
1039 * This could happen if an interrupt was injected on a prior
1040 * VM entry but the actual entry into guest mode was aborted
1041 * because of a pending AST.
1043 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1044 if (info & VMCS_INTERRUPTION_INFO_VALID)
1048 * NMI injection has priority so deal with those first
1050 if (vmx_inject_nmi(vmx, vcpu))
1053 /* Ask the local apic for a vector to inject */
1054 vector = lapic_pending_intr(vmx->vm, vcpu);
1058 if (vector < 32 || vector > 255)
1059 panic("vmx_inject_interrupts: invalid vector %d\n", vector);
1061 /* Check RFLAGS.IF and the interruptibility state of the guest */
1062 rflags = vmcs_read(VMCS_GUEST_RFLAGS);
1063 if ((rflags & PSL_I) == 0)
1066 interruptibility = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1067 if (interruptibility & HWINTR_BLOCKED)
1070 /* Inject the interrupt */
1071 info = VMCS_INTERRUPTION_INFO_HW_INTR | VMCS_INTERRUPTION_INFO_VALID;
1073 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1075 /* Update the Local APIC ISR */
1076 lapic_intr_accepted(vmx->vm, vcpu, vector);
1078 VCPU_CTR1(vmx->vm, vcpu, "Injecting hwintr at vector %d", vector);
1084 * Set the Interrupt Window Exiting execution control so we can inject
1085 * the interrupt as soon as blocking condition goes away.
1087 vmx_set_int_window_exiting(vmx, vcpu);
1089 VCPU_CTR0(vmx->vm, vcpu, "Enabling interrupt window exiting");
1093 vmx_emulate_cr_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1095 int cr, vmcs_guest_cr, vmcs_shadow_cr;
1096 uint64_t crval, regval, ones_mask, zeros_mask;
1097 const struct vmxctx *vmxctx;
1099 /* We only handle mov to %cr0 or %cr4 at this time */
1100 if ((exitqual & 0xf0) != 0x00)
1103 cr = exitqual & 0xf;
1104 if (cr != 0 && cr != 4)
1107 vmxctx = &vmx->ctx[vcpu];
1110 * We must use vmcs_write() directly here because vmcs_setreg() will
1111 * call vmclear(vmcs) as a side-effect which we certainly don't want.
1113 switch ((exitqual >> 8) & 0xf) {
1115 regval = vmxctx->guest_rax;
1118 regval = vmxctx->guest_rcx;
1121 regval = vmxctx->guest_rdx;
1124 regval = vmxctx->guest_rbx;
1127 regval = vmcs_read(VMCS_GUEST_RSP);
1130 regval = vmxctx->guest_rbp;
1133 regval = vmxctx->guest_rsi;
1136 regval = vmxctx->guest_rdi;
1139 regval = vmxctx->guest_r8;
1142 regval = vmxctx->guest_r9;
1145 regval = vmxctx->guest_r10;
1148 regval = vmxctx->guest_r11;
1151 regval = vmxctx->guest_r12;
1154 regval = vmxctx->guest_r13;
1157 regval = vmxctx->guest_r14;
1160 regval = vmxctx->guest_r15;
1165 ones_mask = cr0_ones_mask;
1166 zeros_mask = cr0_zeros_mask;
1167 vmcs_guest_cr = VMCS_GUEST_CR0;
1168 vmcs_shadow_cr = VMCS_CR0_SHADOW;
1170 ones_mask = cr4_ones_mask;
1171 zeros_mask = cr4_zeros_mask;
1172 vmcs_guest_cr = VMCS_GUEST_CR4;
1173 vmcs_shadow_cr = VMCS_CR4_SHADOW;
1175 vmcs_write(vmcs_shadow_cr, regval);
1177 crval = regval | ones_mask;
1178 crval &= ~zeros_mask;
1179 vmcs_write(vmcs_guest_cr, crval);
1181 if (cr == 0 && regval & CR0_PG) {
1182 uint64_t efer, entry_ctls;
1185 * If CR0.PG is 1 and EFER.LME is 1 then EFER.LMA and
1186 * the "IA-32e mode guest" bit in VM-entry control must be
1189 efer = vmcs_read(VMCS_GUEST_IA32_EFER);
1190 if (efer & EFER_LME) {
1192 vmcs_write(VMCS_GUEST_IA32_EFER, efer);
1193 entry_ctls = vmcs_read(VMCS_ENTRY_CTLS);
1194 entry_ctls |= VM_ENTRY_GUEST_LMA;
1195 vmcs_write(VMCS_ENTRY_CTLS, entry_ctls);
1203 ept_fault_type(uint64_t ept_qual)
1207 if (ept_qual & EPT_VIOLATION_DATA_WRITE)
1208 fault_type = VM_PROT_WRITE;
1209 else if (ept_qual & EPT_VIOLATION_INST_FETCH)
1210 fault_type = VM_PROT_EXECUTE;
1212 fault_type= VM_PROT_READ;
1214 return (fault_type);
1218 ept_emulation_fault(uint64_t ept_qual)
1222 /* EPT fault on an instruction fetch doesn't make sense here */
1223 if (ept_qual & EPT_VIOLATION_INST_FETCH)
1226 /* EPT fault must be a read fault or a write fault */
1227 read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0;
1228 write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0;
1229 if ((read | write) == 0)
1233 * The EPT violation must have been caused by accessing a
1234 * guest-physical address that is a translation of a guest-linear
1237 if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 ||
1238 (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) {
1246 vmx_exit_process(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
1250 struct vmxctx *vmxctx;
1251 uint32_t eax, ecx, edx, idtvec_info, idtvec_err, reason;
1256 vmcs = &vmx->vmcs[vcpu];
1257 vmxctx = &vmx->ctx[vcpu];
1258 qual = vmexit->u.vmx.exit_qualification;
1259 reason = vmexit->u.vmx.exit_reason;
1260 vmexit->exitcode = VM_EXITCODE_BOGUS;
1262 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_COUNT, 1);
1265 * VM exits that could be triggered during event injection on the
1266 * previous VM entry need to be handled specially by re-injecting
1269 * See "Information for VM Exits During Event Delivery" in Intel SDM
1273 case EXIT_REASON_EPT_FAULT:
1274 case EXIT_REASON_EPT_MISCONFIG:
1275 case EXIT_REASON_APIC:
1276 case EXIT_REASON_TASK_SWITCH:
1277 case EXIT_REASON_EXCEPTION:
1278 idtvec_info = vmcs_idt_vectoring_info();
1279 if (idtvec_info & VMCS_IDT_VEC_VALID) {
1280 idtvec_info &= ~(1 << 12); /* clear undefined bit */
1281 vmcs_write(VMCS_ENTRY_INTR_INFO, idtvec_info);
1282 if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) {
1283 idtvec_err = vmcs_idt_vectoring_err();
1284 vmcs_write(VMCS_ENTRY_EXCEPTION_ERROR,
1287 vmcs_write(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length);
1294 case EXIT_REASON_CR_ACCESS:
1295 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CR_ACCESS, 1);
1296 handled = vmx_emulate_cr_access(vmx, vcpu, qual);
1298 case EXIT_REASON_RDMSR:
1299 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_RDMSR, 1);
1301 ecx = vmxctx->guest_rcx;
1302 error = emulate_rdmsr(vmx->vm, vcpu, ecx, &retu);
1304 vmexit->exitcode = VM_EXITCODE_RDMSR;
1305 vmexit->u.msr.code = ecx;
1309 /* Return to userspace with a valid exitcode */
1310 KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
1311 ("emulate_wrmsr retu with bogus exitcode"));
1314 case EXIT_REASON_WRMSR:
1315 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_WRMSR, 1);
1317 eax = vmxctx->guest_rax;
1318 ecx = vmxctx->guest_rcx;
1319 edx = vmxctx->guest_rdx;
1320 error = emulate_wrmsr(vmx->vm, vcpu, ecx,
1321 (uint64_t)edx << 32 | eax, &retu);
1323 vmexit->exitcode = VM_EXITCODE_WRMSR;
1324 vmexit->u.msr.code = ecx;
1325 vmexit->u.msr.wval = (uint64_t)edx << 32 | eax;
1329 /* Return to userspace with a valid exitcode */
1330 KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
1331 ("emulate_wrmsr retu with bogus exitcode"));
1334 case EXIT_REASON_HLT:
1335 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_HLT, 1);
1336 vmexit->exitcode = VM_EXITCODE_HLT;
1337 vmexit->u.hlt.rflags = vmcs_read(VMCS_GUEST_RFLAGS);
1339 case EXIT_REASON_MTF:
1340 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_MTRAP, 1);
1341 vmexit->exitcode = VM_EXITCODE_MTRAP;
1343 case EXIT_REASON_PAUSE:
1344 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_PAUSE, 1);
1345 vmexit->exitcode = VM_EXITCODE_PAUSE;
1347 case EXIT_REASON_INTR_WINDOW:
1348 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INTR_WINDOW, 1);
1349 vmx_clear_int_window_exiting(vmx, vcpu);
1350 VCPU_CTR0(vmx->vm, vcpu, "Disabling interrupt window exiting");
1352 case EXIT_REASON_EXT_INTR:
1354 * External interrupts serve only to cause VM exits and allow
1355 * the host interrupt handler to run.
1357 * If this external interrupt triggers a virtual interrupt
1358 * to a VM, then that state will be recorded by the
1359 * host interrupt handler in the VM's softc. We will inject
1360 * this virtual interrupt during the subsequent VM enter.
1364 * This is special. We want to treat this as an 'handled'
1365 * VM-exit but not increment the instruction pointer.
1367 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXTINT, 1);
1369 case EXIT_REASON_NMI_WINDOW:
1370 /* Exit to allow the pending virtual NMI to be injected */
1371 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NMI_WINDOW, 1);
1372 vmx_clear_nmi_window_exiting(vmx, vcpu);
1373 VCPU_CTR0(vmx->vm, vcpu, "Disabling NMI window exiting");
1375 case EXIT_REASON_INOUT:
1376 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INOUT, 1);
1377 vmexit->exitcode = VM_EXITCODE_INOUT;
1378 vmexit->u.inout.bytes = (qual & 0x7) + 1;
1379 vmexit->u.inout.in = (qual & 0x8) ? 1 : 0;
1380 vmexit->u.inout.string = (qual & 0x10) ? 1 : 0;
1381 vmexit->u.inout.rep = (qual & 0x20) ? 1 : 0;
1382 vmexit->u.inout.port = (uint16_t)(qual >> 16);
1383 vmexit->u.inout.eax = (uint32_t)(vmxctx->guest_rax);
1385 case EXIT_REASON_CPUID:
1386 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CPUID, 1);
1387 handled = vmx_handle_cpuid(vmx->vm, vcpu, vmxctx);
1389 case EXIT_REASON_EPT_FAULT:
1390 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EPT_FAULT, 1);
1392 * If 'gpa' lies within the address space allocated to
1393 * memory then this must be a nested page fault otherwise
1394 * this must be an instruction that accesses MMIO space.
1397 if (vm_mem_allocated(vmx->vm, gpa)) {
1398 vmexit->exitcode = VM_EXITCODE_PAGING;
1399 vmexit->u.paging.gpa = gpa;
1400 vmexit->u.paging.fault_type = ept_fault_type(qual);
1401 } else if (ept_emulation_fault(qual)) {
1402 vmexit->exitcode = VM_EXITCODE_INST_EMUL;
1403 vmexit->u.inst_emul.gpa = gpa;
1404 vmexit->u.inst_emul.gla = vmcs_gla();
1405 vmexit->u.inst_emul.cr3 = vmcs_guest_cr3();
1409 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_UNKNOWN, 1);
1415 * It is possible that control is returned to userland
1416 * even though we were able to handle the VM exit in the
1419 * In such a case we want to make sure that the userland
1420 * restarts guest execution at the instruction *after*
1421 * the one we just processed. Therefore we update the
1422 * guest rip in the VMCS and in 'vmexit'.
1424 vmexit->rip += vmexit->inst_length;
1425 vmexit->inst_length = 0;
1426 vmcs_write(VMCS_GUEST_RIP, vmexit->rip);
1428 if (vmexit->exitcode == VM_EXITCODE_BOGUS) {
1430 * If this VM exit was not claimed by anybody then
1431 * treat it as a generic VMX exit.
1433 vmexit->exitcode = VM_EXITCODE_VMX;
1434 vmexit->u.vmx.error = 0;
1437 * The exitcode and collateral have been populated.
1438 * The VM exit will be processed further in userland.
1446 vmx_run(void *arg, int vcpu, register_t rip, pmap_t pmap)
1448 int vie, rc, handled, astpending;
1449 uint32_t exit_reason;
1451 struct vmxctx *vmxctx;
1453 struct vm_exit *vmexit;
1456 vmcs = &vmx->vmcs[vcpu];
1457 vmxctx = &vmx->ctx[vcpu];
1458 vmxctx->launched = 0;
1461 vmexit = vm_exitinfo(vmx->vm, vcpu);
1463 KASSERT(vmxctx->pmap == pmap,
1464 ("pmap %p different than ctx pmap %p", pmap, vmxctx->pmap));
1465 KASSERT(vmxctx->eptp == vmx->eptp,
1466 ("eptp %p different than ctx eptp %#lx", eptp, vmxctx->eptp));
1469 * XXX Can we avoid doing this every time we do a vm run?
1475 * We do this every time because we may setup the virtual machine
1476 * from a different process than the one that actually runs it.
1478 * If the life of a virtual machine was spent entirely in the context
1479 * of a single process we could do this once in vmcs_set_defaults().
1481 vmcs_write(VMCS_HOST_CR3, rcr3());
1482 vmcs_write(VMCS_GUEST_RIP, rip);
1483 vmx_set_pcpu_defaults(vmx, vcpu);
1486 vmx_inject_interrupts(vmx, vcpu);
1487 vmx_run_trace(vmx, vcpu);
1488 rc = vmx_setjmp(vmxctx);
1490 vmx_setjmp_trace(vmx, vcpu, vmxctx, rc);
1493 case VMX_RETURN_DIRECT:
1494 if (vmxctx->launched == 0) {
1495 vmxctx->launched = 1;
1499 panic("vmx_launch/resume should not return");
1501 case VMX_RETURN_LONGJMP:
1502 break; /* vm exit */
1503 case VMX_RETURN_AST:
1506 case VMX_RETURN_VMRESUME:
1507 vie = vmcs_instruction_error();
1508 if (vmxctx->launch_error == VM_FAIL_INVALID ||
1509 vie != VMRESUME_WITH_NON_LAUNCHED_VMCS) {
1510 printf("vmresume error %d vmcs inst error %d\n",
1511 vmxctx->launch_error, vie);
1514 vmx_launch(vmxctx); /* try to launch the guest */
1515 panic("vmx_launch should not return");
1517 case VMX_RETURN_VMLAUNCH:
1518 vie = vmcs_instruction_error();
1520 printf("vmlaunch error %d vmcs inst error %d\n",
1521 vmxctx->launch_error, vie);
1524 case VMX_RETURN_INVEPT:
1525 panic("vm %s:%d invept error %d",
1526 vm_name(vmx->vm), vcpu, vmxctx->launch_error);
1528 panic("vmx_setjmp returned %d", rc);
1531 /* enable interrupts */
1534 /* collect some basic information for VM exit processing */
1535 vmexit->rip = rip = vmcs_guest_rip();
1536 vmexit->inst_length = vmexit_instruction_length();
1537 vmexit->u.vmx.exit_reason = exit_reason = vmcs_exit_reason();
1538 vmexit->u.vmx.exit_qualification = vmcs_exit_qualification();
1542 vmexit->inst_length = 0;
1543 vmexit->exitcode = VM_EXITCODE_BOGUS;
1544 vmx_astpending_trace(vmx, vcpu, rip);
1545 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_ASTPENDING, 1);
1549 handled = vmx_exit_process(vmx, vcpu, vmexit);
1550 vmx_exit_trace(vmx, vcpu, rip, exit_reason, handled);
1555 * If a VM exit has been handled then the exitcode must be BOGUS
1556 * If a VM exit is not handled then the exitcode must not be BOGUS
1558 if ((handled && vmexit->exitcode != VM_EXITCODE_BOGUS) ||
1559 (!handled && vmexit->exitcode == VM_EXITCODE_BOGUS)) {
1560 panic("Mismatch between handled (%d) and exitcode (%d)",
1561 handled, vmexit->exitcode);
1565 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_USERSPACE, 1);
1567 VCPU_CTR1(vmx->vm, vcpu, "goto userland: exitcode %d",vmexit->exitcode);
1571 * We need to do this to ensure that any VMCS state cached by the
1572 * processor is flushed to memory. We need to do this in case the
1573 * VM moves to a different cpu the next time it runs.
1575 * Can we avoid doing this?
1581 vmexit->exitcode = VM_EXITCODE_VMX;
1582 vmexit->u.vmx.exit_reason = (uint32_t)-1;
1583 vmexit->u.vmx.exit_qualification = (uint32_t)-1;
1584 vmexit->u.vmx.error = vie;
1590 vmx_vmcleanup(void *arg)
1593 struct vmx *vmx = arg;
1595 for (i = 0; i < VM_MAXCPU; i++)
1596 vpid_free(vmx->state[i].vpid);
1599 * XXXSMP we also need to clear the VMCS active on the other vcpus.
1601 error = vmclear(&vmx->vmcs[0]);
1603 panic("vmx_vmcleanup: vmclear error %d on vcpu 0", error);
1611 vmxctx_regptr(struct vmxctx *vmxctx, int reg)
1615 case VM_REG_GUEST_RAX:
1616 return (&vmxctx->guest_rax);
1617 case VM_REG_GUEST_RBX:
1618 return (&vmxctx->guest_rbx);
1619 case VM_REG_GUEST_RCX:
1620 return (&vmxctx->guest_rcx);
1621 case VM_REG_GUEST_RDX:
1622 return (&vmxctx->guest_rdx);
1623 case VM_REG_GUEST_RSI:
1624 return (&vmxctx->guest_rsi);
1625 case VM_REG_GUEST_RDI:
1626 return (&vmxctx->guest_rdi);
1627 case VM_REG_GUEST_RBP:
1628 return (&vmxctx->guest_rbp);
1629 case VM_REG_GUEST_R8:
1630 return (&vmxctx->guest_r8);
1631 case VM_REG_GUEST_R9:
1632 return (&vmxctx->guest_r9);
1633 case VM_REG_GUEST_R10:
1634 return (&vmxctx->guest_r10);
1635 case VM_REG_GUEST_R11:
1636 return (&vmxctx->guest_r11);
1637 case VM_REG_GUEST_R12:
1638 return (&vmxctx->guest_r12);
1639 case VM_REG_GUEST_R13:
1640 return (&vmxctx->guest_r13);
1641 case VM_REG_GUEST_R14:
1642 return (&vmxctx->guest_r14);
1643 case VM_REG_GUEST_R15:
1644 return (&vmxctx->guest_r15);
1652 vmxctx_getreg(struct vmxctx *vmxctx, int reg, uint64_t *retval)
1656 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
1664 vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val)
1668 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
1676 vmx_shadow_reg(int reg)
1683 case VM_REG_GUEST_CR0:
1684 shreg = VMCS_CR0_SHADOW;
1686 case VM_REG_GUEST_CR4:
1687 shreg = VMCS_CR4_SHADOW;
1697 vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval)
1699 int running, hostcpu;
1700 struct vmx *vmx = arg;
1702 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
1703 if (running && hostcpu != curcpu)
1704 panic("vmx_getreg: %s%d is running", vm_name(vmx->vm), vcpu);
1706 if (vmxctx_getreg(&vmx->ctx[vcpu], reg, retval) == 0)
1709 return (vmcs_getreg(&vmx->vmcs[vcpu], running, reg, retval));
1713 vmx_setreg(void *arg, int vcpu, int reg, uint64_t val)
1715 int error, hostcpu, running, shadow;
1717 struct vmx *vmx = arg;
1719 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
1720 if (running && hostcpu != curcpu)
1721 panic("vmx_setreg: %s%d is running", vm_name(vmx->vm), vcpu);
1723 if (vmxctx_setreg(&vmx->ctx[vcpu], reg, val) == 0)
1726 error = vmcs_setreg(&vmx->vmcs[vcpu], running, reg, val);
1730 * If the "load EFER" VM-entry control is 1 then the
1731 * value of EFER.LMA must be identical to "IA-32e mode guest"
1732 * bit in the VM-entry control.
1734 if ((entry_ctls & VM_ENTRY_LOAD_EFER) != 0 &&
1735 (reg == VM_REG_GUEST_EFER)) {
1736 vmcs_getreg(&vmx->vmcs[vcpu], running,
1737 VMCS_IDENT(VMCS_ENTRY_CTLS), &ctls);
1739 ctls |= VM_ENTRY_GUEST_LMA;
1741 ctls &= ~VM_ENTRY_GUEST_LMA;
1742 vmcs_setreg(&vmx->vmcs[vcpu], running,
1743 VMCS_IDENT(VMCS_ENTRY_CTLS), ctls);
1746 shadow = vmx_shadow_reg(reg);
1749 * Store the unmodified value in the shadow
1751 error = vmcs_setreg(&vmx->vmcs[vcpu], running,
1752 VMCS_IDENT(shadow), val);
1760 vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
1762 struct vmx *vmx = arg;
1764 return (vmcs_getdesc(&vmx->vmcs[vcpu], reg, desc));
1768 vmx_setdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
1770 struct vmx *vmx = arg;
1772 return (vmcs_setdesc(&vmx->vmcs[vcpu], reg, desc));
1776 vmx_inject(void *arg, int vcpu, int type, int vector, uint32_t code,
1781 struct vmx *vmx = arg;
1782 struct vmcs *vmcs = &vmx->vmcs[vcpu];
1784 static uint32_t type_map[VM_EVENT_MAX] = {
1785 0x1, /* VM_EVENT_NONE */
1786 0x0, /* VM_HW_INTR */
1788 0x3, /* VM_HW_EXCEPTION */
1789 0x4, /* VM_SW_INTR */
1790 0x5, /* VM_PRIV_SW_EXCEPTION */
1791 0x6, /* VM_SW_EXCEPTION */
1795 * If there is already an exception pending to be delivered to the
1796 * vcpu then just return.
1798 error = vmcs_getreg(vmcs, 0, VMCS_IDENT(VMCS_ENTRY_INTR_INFO), &info);
1802 if (info & VMCS_INTERRUPTION_INFO_VALID)
1805 info = vector | (type_map[type] << 8) | (code_valid ? 1 << 11 : 0);
1806 info |= VMCS_INTERRUPTION_INFO_VALID;
1807 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(VMCS_ENTRY_INTR_INFO), info);
1812 error = vmcs_setreg(vmcs, 0,
1813 VMCS_IDENT(VMCS_ENTRY_EXCEPTION_ERROR),
1820 vmx_getcap(void *arg, int vcpu, int type, int *retval)
1822 struct vmx *vmx = arg;
1828 vcap = vmx->cap[vcpu].set;
1831 case VM_CAP_HALT_EXIT:
1835 case VM_CAP_PAUSE_EXIT:
1839 case VM_CAP_MTRAP_EXIT:
1840 if (cap_monitor_trap)
1843 case VM_CAP_UNRESTRICTED_GUEST:
1844 if (cap_unrestricted_guest)
1847 case VM_CAP_ENABLE_INVPCID:
1856 *retval = (vcap & (1 << type)) ? 1 : 0;
1862 vmx_setcap(void *arg, int vcpu, int type, int val)
1864 struct vmx *vmx = arg;
1865 struct vmcs *vmcs = &vmx->vmcs[vcpu];
1877 case VM_CAP_HALT_EXIT:
1878 if (cap_halt_exit) {
1880 pptr = &vmx->cap[vcpu].proc_ctls;
1882 flag = PROCBASED_HLT_EXITING;
1883 reg = VMCS_PRI_PROC_BASED_CTLS;
1886 case VM_CAP_MTRAP_EXIT:
1887 if (cap_monitor_trap) {
1889 pptr = &vmx->cap[vcpu].proc_ctls;
1891 flag = PROCBASED_MTF;
1892 reg = VMCS_PRI_PROC_BASED_CTLS;
1895 case VM_CAP_PAUSE_EXIT:
1896 if (cap_pause_exit) {
1898 pptr = &vmx->cap[vcpu].proc_ctls;
1900 flag = PROCBASED_PAUSE_EXITING;
1901 reg = VMCS_PRI_PROC_BASED_CTLS;
1904 case VM_CAP_UNRESTRICTED_GUEST:
1905 if (cap_unrestricted_guest) {
1907 pptr = &vmx->cap[vcpu].proc_ctls2;
1909 flag = PROCBASED2_UNRESTRICTED_GUEST;
1910 reg = VMCS_SEC_PROC_BASED_CTLS;
1913 case VM_CAP_ENABLE_INVPCID:
1916 pptr = &vmx->cap[vcpu].proc_ctls2;
1918 flag = PROCBASED2_ENABLE_INVPCID;
1919 reg = VMCS_SEC_PROC_BASED_CTLS;
1933 error = vmwrite(reg, baseval);
1940 * Update optional stored flags, and record
1948 vmx->cap[vcpu].set |= (1 << type);
1950 vmx->cap[vcpu].set &= ~(1 << type);
1958 struct vmm_ops vmm_ops_intel = {