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/pmap.h>
48 #include <machine/segments.h>
49 #include <machine/specialreg.h>
50 #include <machine/vmparam.h>
52 #include <machine/vmm.h>
54 #include "vmm_lapic.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 MALLOC_DEFINE(M_VMX, "vmx", "vmx");
118 SYSCTL_DECL(_hw_vmm);
119 SYSCTL_NODE(_hw_vmm, OID_AUTO, vmx, CTLFLAG_RW, NULL, NULL);
121 int vmxon_enabled[MAXCPU];
122 static char vmxon_region[MAXCPU][PAGE_SIZE] __aligned(PAGE_SIZE);
124 static uint32_t pinbased_ctls, procbased_ctls, procbased_ctls2;
125 static uint32_t exit_ctls, entry_ctls;
127 static uint64_t cr0_ones_mask, cr0_zeros_mask;
128 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_ones_mask, CTLFLAG_RD,
129 &cr0_ones_mask, 0, NULL);
130 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_zeros_mask, CTLFLAG_RD,
131 &cr0_zeros_mask, 0, NULL);
133 static uint64_t cr4_ones_mask, cr4_zeros_mask;
134 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_ones_mask, CTLFLAG_RD,
135 &cr4_ones_mask, 0, NULL);
136 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_zeros_mask, CTLFLAG_RD,
137 &cr4_zeros_mask, 0, NULL);
139 static int vmx_no_patmsr;
141 static int vmx_initialized;
142 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, initialized, CTLFLAG_RD,
143 &vmx_initialized, 0, "Intel VMX initialized");
146 * Virtual NMI blocking conditions.
148 * Some processor implementations also require NMI to be blocked if
149 * the STI_BLOCKING bit is set. It is possible to detect this at runtime
150 * based on the (exit_reason,exit_qual) tuple being set to
151 * (EXIT_REASON_INVAL_VMCS, EXIT_QUAL_NMI_WHILE_STI_BLOCKING).
153 * We take the easy way out and also include STI_BLOCKING as one of the
154 * gating items for vNMI injection.
156 static uint64_t nmi_blocking_bits = VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING |
157 VMCS_INTERRUPTIBILITY_NMI_BLOCKING |
158 VMCS_INTERRUPTIBILITY_STI_BLOCKING;
161 * Optional capabilities
163 static int cap_halt_exit;
164 static int cap_pause_exit;
165 static int cap_unrestricted_guest;
166 static int cap_monitor_trap;
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 VMM_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 VMM_CTR1((vmx)->vm, (vcpu), "vmxctx = %p", vmxctx);
324 VMM_CTR2((vmx)->vm, (vcpu), "setjmp return code %s(%d)",
325 vmx_setjmp_rc2str(rc), rc);
327 host_rsp = host_rip = ~0;
328 vmread(VMCS_HOST_RIP, &host_rip);
329 vmread(VMCS_HOST_RSP, &host_rsp);
330 VMM_CTR2((vmx)->vm, (vcpu), "vmcs host_rip 0x%016lx, host_rsp 0x%016lx",
333 SETJMP_TRACE(vmx, vcpu, vmxctx, host_r15);
334 SETJMP_TRACE(vmx, vcpu, vmxctx, host_r14);
335 SETJMP_TRACE(vmx, vcpu, vmxctx, host_r13);
336 SETJMP_TRACE(vmx, vcpu, vmxctx, host_r12);
337 SETJMP_TRACE(vmx, vcpu, vmxctx, host_rbp);
338 SETJMP_TRACE(vmx, vcpu, vmxctx, host_rsp);
339 SETJMP_TRACE(vmx, vcpu, vmxctx, host_rbx);
340 SETJMP_TRACE(vmx, vcpu, vmxctx, host_rip);
342 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rdi);
343 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rsi);
344 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rdx);
345 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rcx);
346 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r8);
347 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r9);
348 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rax);
349 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rbx);
350 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_rbp);
351 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r10);
352 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r11);
353 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r12);
354 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r13);
355 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r14);
356 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_r15);
357 SETJMP_TRACE(vmx, vcpu, vmxctx, guest_cr2);
362 vmx_setjmp_trace(struct vmx *vmx, int vcpu, struct vmxctx *vmxctx, int rc)
369 vmx_fix_cr0(u_long cr0)
372 return ((cr0 | cr0_ones_mask) & ~cr0_zeros_mask);
376 vmx_fix_cr4(u_long cr4)
379 return ((cr4 | cr4_ones_mask) & ~cr4_zeros_mask);
385 if (vpid < 0 || vpid > 0xffff)
386 panic("vpid_free: invalid vpid %d", vpid);
389 * VPIDs [0,VM_MAXCPU] are special and are not allocated from
390 * the unit number allocator.
393 if (vpid > VM_MAXCPU)
394 free_unr(vpid_unr, vpid);
398 vpid_alloc(uint16_t *vpid, int num)
402 if (num <= 0 || num > VM_MAXCPU)
403 panic("invalid number of vpids requested: %d", num);
406 * If the "enable vpid" execution control is not enabled then the
407 * VPID is required to be 0 for all vcpus.
409 if ((procbased_ctls2 & PROCBASED2_ENABLE_VPID) == 0) {
410 for (i = 0; i < num; i++)
416 * Allocate a unique VPID for each vcpu from the unit number allocator.
418 for (i = 0; i < num; i++) {
419 x = alloc_unr(vpid_unr);
427 atomic_add_int(&vpid_alloc_failed, 1);
430 * If the unit number allocator does not have enough unique
431 * VPIDs then we need to allocate from the [1,VM_MAXCPU] range.
433 * These VPIDs are not be unique across VMs but this does not
434 * affect correctness because the combined mappings are also
435 * tagged with the EP4TA which is unique for each VM.
437 * It is still sub-optimal because the invvpid will invalidate
438 * combined mappings for a particular VPID across all EP4TAs.
443 for (i = 0; i < num; i++)
452 * VPID 0 is required when the "enable VPID" execution control is
455 * VPIDs [1,VM_MAXCPU] are used as the "overflow namespace" when the
456 * unit number allocator does not have sufficient unique VPIDs to
457 * satisfy the allocation.
459 * The remaining VPIDs are managed by the unit number allocator.
461 vpid_unr = new_unrhdr(VM_MAXCPU + 1, 0xffff, NULL);
465 msr_save_area_init(struct msr_entry *g_area, int *g_count)
469 static struct msr_entry guest_msrs[] = {
470 { MSR_KGSBASE, 0, 0 },
473 cnt = sizeof(guest_msrs) / sizeof(guest_msrs[0]);
474 if (cnt > GUEST_MSR_MAX_ENTRIES)
475 panic("guest msr save area overrun");
476 bcopy(guest_msrs, g_area, sizeof(guest_msrs));
481 vmx_disable(void *arg __unused)
483 struct invvpid_desc invvpid_desc = { 0 };
484 struct invept_desc invept_desc = { 0 };
486 if (vmxon_enabled[curcpu]) {
488 * See sections 25.3.3.3 and 25.3.3.4 in Intel Vol 3b.
490 * VMXON or VMXOFF are not required to invalidate any TLB
491 * caching structures. This prevents potential retention of
492 * cached information in the TLB between distinct VMX episodes.
494 invvpid(INVVPID_TYPE_ALL_CONTEXTS, invvpid_desc);
495 invept(INVEPT_TYPE_ALL_CONTEXTS, invept_desc);
498 load_cr4(rcr4() & ~CR4_VMXE);
505 if (vpid_unr != NULL) {
506 delete_unrhdr(vpid_unr);
510 smp_rendezvous(NULL, vmx_disable, NULL, NULL);
516 vmx_enable(void *arg __unused)
520 load_cr4(rcr4() | CR4_VMXE);
522 *(uint32_t *)vmxon_region[curcpu] = vmx_revision();
523 error = vmxon(vmxon_region[curcpu]);
525 vmxon_enabled[curcpu] = 1;
532 uint64_t fixed0, fixed1, feature_control;
535 /* CPUID.1:ECX[bit 5] must be 1 for processor to support VMX */
536 if (!(cpu_feature2 & CPUID2_VMX)) {
537 printf("vmx_init: processor does not support VMX operation\n");
542 * Verify that MSR_IA32_FEATURE_CONTROL lock and VMXON enable bits
543 * are set (bits 0 and 2 respectively).
545 feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
546 if ((feature_control & IA32_FEATURE_CONTROL_LOCK) == 0 ||
547 (feature_control & IA32_FEATURE_CONTROL_VMX_EN) == 0) {
548 printf("vmx_init: VMX operation disabled by BIOS\n");
552 /* Check support for primary processor-based VM-execution controls */
553 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
554 MSR_VMX_TRUE_PROCBASED_CTLS,
555 PROCBASED_CTLS_ONE_SETTING,
556 PROCBASED_CTLS_ZERO_SETTING, &procbased_ctls);
558 printf("vmx_init: processor does not support desired primary "
559 "processor-based controls\n");
563 /* Clear the processor-based ctl bits that are set on demand */
564 procbased_ctls &= ~PROCBASED_CTLS_WINDOW_SETTING;
566 /* Check support for secondary processor-based VM-execution controls */
567 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
568 MSR_VMX_PROCBASED_CTLS2,
569 PROCBASED_CTLS2_ONE_SETTING,
570 PROCBASED_CTLS2_ZERO_SETTING, &procbased_ctls2);
572 printf("vmx_init: processor does not support desired secondary "
573 "processor-based controls\n");
577 /* Check support for VPID */
578 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2,
579 PROCBASED2_ENABLE_VPID, 0, &tmp);
581 procbased_ctls2 |= PROCBASED2_ENABLE_VPID;
583 /* Check support for pin-based VM-execution controls */
584 error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS,
585 MSR_VMX_TRUE_PINBASED_CTLS,
586 PINBASED_CTLS_ONE_SETTING,
587 PINBASED_CTLS_ZERO_SETTING, &pinbased_ctls);
589 printf("vmx_init: processor does not support desired "
590 "pin-based controls\n");
594 /* Check support for VM-exit controls */
595 error = vmx_set_ctlreg(MSR_VMX_EXIT_CTLS, MSR_VMX_TRUE_EXIT_CTLS,
596 VM_EXIT_CTLS_ONE_SETTING,
597 VM_EXIT_CTLS_ZERO_SETTING,
600 /* Try again without the PAT MSR bits */
601 error = vmx_set_ctlreg(MSR_VMX_EXIT_CTLS,
602 MSR_VMX_TRUE_EXIT_CTLS,
603 VM_EXIT_CTLS_ONE_SETTING_NO_PAT,
604 VM_EXIT_CTLS_ZERO_SETTING,
607 printf("vmx_init: processor does not support desired "
612 printf("vmm: PAT MSR access not supported\n");
613 guest_msr_valid(MSR_PAT);
618 /* Check support for VM-entry controls */
619 if (!vmx_no_patmsr) {
620 error = vmx_set_ctlreg(MSR_VMX_ENTRY_CTLS,
621 MSR_VMX_TRUE_ENTRY_CTLS,
622 VM_ENTRY_CTLS_ONE_SETTING,
623 VM_ENTRY_CTLS_ZERO_SETTING,
626 error = vmx_set_ctlreg(MSR_VMX_ENTRY_CTLS,
627 MSR_VMX_TRUE_ENTRY_CTLS,
628 VM_ENTRY_CTLS_ONE_SETTING_NO_PAT,
629 VM_ENTRY_CTLS_ZERO_SETTING,
634 printf("vmx_init: processor does not support desired "
640 * Check support for optional features by testing them
643 cap_halt_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
644 MSR_VMX_TRUE_PROCBASED_CTLS,
645 PROCBASED_HLT_EXITING, 0,
648 cap_monitor_trap = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
649 MSR_VMX_PROCBASED_CTLS,
653 cap_pause_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
654 MSR_VMX_TRUE_PROCBASED_CTLS,
655 PROCBASED_PAUSE_EXITING, 0,
658 cap_unrestricted_guest = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
659 MSR_VMX_PROCBASED_CTLS2,
660 PROCBASED2_UNRESTRICTED_GUEST, 0,
666 printf("vmx_init: ept initialization failed (%d)\n", error);
671 * Stash the cr0 and cr4 bits that must be fixed to 0 or 1
673 fixed0 = rdmsr(MSR_VMX_CR0_FIXED0);
674 fixed1 = rdmsr(MSR_VMX_CR0_FIXED1);
675 cr0_ones_mask = fixed0 & fixed1;
676 cr0_zeros_mask = ~fixed0 & ~fixed1;
679 * CR0_PE and CR0_PG can be set to zero in VMX non-root operation
680 * if unrestricted guest execution is allowed.
682 if (cap_unrestricted_guest)
683 cr0_ones_mask &= ~(CR0_PG | CR0_PE);
686 * Do not allow the guest to set CR0_NW or CR0_CD.
688 cr0_zeros_mask |= (CR0_NW | CR0_CD);
690 fixed0 = rdmsr(MSR_VMX_CR4_FIXED0);
691 fixed1 = rdmsr(MSR_VMX_CR4_FIXED1);
692 cr4_ones_mask = fixed0 & fixed1;
693 cr4_zeros_mask = ~fixed0 & ~fixed1;
697 /* enable VMX operation */
698 smp_rendezvous(NULL, vmx_enable, NULL, NULL);
706 vmx_setup_cr_shadow(int which, struct vmcs *vmcs, uint32_t initial)
708 int error, mask_ident, shadow_ident;
711 if (which != 0 && which != 4)
712 panic("vmx_setup_cr_shadow: unknown cr%d", which);
715 mask_ident = VMCS_CR0_MASK;
716 mask_value = cr0_ones_mask | cr0_zeros_mask;
717 shadow_ident = VMCS_CR0_SHADOW;
719 mask_ident = VMCS_CR4_MASK;
720 mask_value = cr4_ones_mask | cr4_zeros_mask;
721 shadow_ident = VMCS_CR4_SHADOW;
724 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(mask_ident), mask_value);
728 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(shadow_ident), initial);
734 #define vmx_setup_cr0_shadow(vmcs,init) vmx_setup_cr_shadow(0, (vmcs), (init))
735 #define vmx_setup_cr4_shadow(vmcs,init) vmx_setup_cr_shadow(4, (vmcs), (init))
738 vmx_vminit(struct vm *vm, pmap_t pmap)
740 uint16_t vpid[VM_MAXCPU];
741 int i, error, guest_msr_count;
744 vmx = malloc(sizeof(struct vmx), M_VMX, M_WAITOK | M_ZERO);
745 if ((uintptr_t)vmx & PAGE_MASK) {
746 panic("malloc of struct vmx not aligned on %d byte boundary",
751 vmx->eptp = eptp(vtophys((vm_offset_t)pmap->pm_pml4));
754 * Clean up EPTP-tagged guest physical and combined mappings
756 * VMX transitions are not required to invalidate any guest physical
757 * mappings. So, it may be possible for stale guest physical mappings
758 * to be present in the processor TLBs.
760 * Combined mappings for this EP4TA are also invalidated for all VPIDs.
762 ept_invalidate_mappings(vmx->eptp);
764 msr_bitmap_initialize(vmx->msr_bitmap);
767 * It is safe to allow direct access to MSR_GSBASE and MSR_FSBASE.
768 * The guest FSBASE and GSBASE are saved and restored during
769 * vm-exit and vm-entry respectively. The host FSBASE and GSBASE are
770 * always restored from the vmcs host state area on vm-exit.
772 * The SYSENTER_CS/ESP/EIP MSRs are identical to FS/GSBASE in
773 * how they are saved/restored so can be directly accessed by the
776 * Guest KGSBASE is saved and restored in the guest MSR save area.
777 * Host KGSBASE is restored before returning to userland from the pcb.
778 * There will be a window of time when we are executing in the host
779 * kernel context with a value of KGSBASE from the guest. This is ok
780 * because the value of KGSBASE is inconsequential in kernel context.
782 * MSR_EFER is saved and restored in the guest VMCS area on a
783 * VM exit and entry respectively. It is also restored from the
784 * host VMCS area on a VM exit.
786 if (guest_msr_rw(vmx, MSR_GSBASE) ||
787 guest_msr_rw(vmx, MSR_FSBASE) ||
788 guest_msr_rw(vmx, MSR_SYSENTER_CS_MSR) ||
789 guest_msr_rw(vmx, MSR_SYSENTER_ESP_MSR) ||
790 guest_msr_rw(vmx, MSR_SYSENTER_EIP_MSR) ||
791 guest_msr_rw(vmx, MSR_KGSBASE) ||
792 guest_msr_rw(vmx, MSR_EFER))
793 panic("vmx_vminit: error setting guest msr access");
796 * MSR_PAT is saved and restored in the guest VMCS are on a VM exit
797 * and entry respectively. It is also restored from the host VMCS
798 * area on a VM exit. However, if running on a system with no
799 * MSR_PAT save/restore support, leave access disabled so accesses
802 if (!vmx_no_patmsr && guest_msr_rw(vmx, MSR_PAT))
803 panic("vmx_vminit: error setting guest pat msr access");
805 vpid_alloc(vpid, VM_MAXCPU);
807 for (i = 0; i < VM_MAXCPU; i++) {
808 vmx->vmcs[i].identifier = vmx_revision();
809 error = vmclear(&vmx->vmcs[i]);
811 panic("vmx_vminit: vmclear error %d on vcpu %d\n",
815 error = vmcs_set_defaults(&vmx->vmcs[i],
817 (u_long)&vmx->ctx[i],
822 exit_ctls, entry_ctls,
823 vtophys(vmx->msr_bitmap),
827 panic("vmx_vminit: vmcs_set_defaults error %d", error);
830 vmx->cap[i].proc_ctls = procbased_ctls;
832 vmx->state[i].lastcpu = -1;
833 vmx->state[i].vpid = vpid[i];
835 msr_save_area_init(vmx->guest_msrs[i], &guest_msr_count);
837 error = vmcs_set_msr_save(&vmx->vmcs[i],
838 vtophys(vmx->guest_msrs[i]),
841 panic("vmcs_set_msr_save error %d", error);
844 * Set up the CR0/4 shadows, and init the read shadow
845 * to the power-on register value from the Intel Sys Arch.
849 error = vmx_setup_cr0_shadow(&vmx->vmcs[i], 0x60000010);
851 panic("vmx_setup_cr0_shadow %d", error);
853 error = vmx_setup_cr4_shadow(&vmx->vmcs[i], 0);
855 panic("vmx_setup_cr4_shadow %d", error);
857 vmx->ctx[i].pmap = pmap;
858 vmx->ctx[i].eptp = vmx->eptp;
865 vmx_handle_cpuid(struct vm *vm, int vcpu, struct vmxctx *vmxctx)
869 func = vmxctx->guest_rax;
871 handled = x86_emulate_cpuid(vm, vcpu,
872 (uint32_t*)(&vmxctx->guest_rax),
873 (uint32_t*)(&vmxctx->guest_rbx),
874 (uint32_t*)(&vmxctx->guest_rcx),
875 (uint32_t*)(&vmxctx->guest_rdx));
880 vmx_run_trace(struct vmx *vmx, int vcpu)
883 VMM_CTR1(vmx->vm, vcpu, "Resume execution at 0x%0lx", vmcs_guest_rip());
888 vmx_exit_trace(struct vmx *vmx, int vcpu, uint64_t rip, uint32_t exit_reason,
892 VMM_CTR3(vmx->vm, vcpu, "%s %s vmexit at 0x%0lx",
893 handled ? "handled" : "unhandled",
894 exit_reason_to_str(exit_reason), rip);
899 vmx_astpending_trace(struct vmx *vmx, int vcpu, uint64_t rip)
902 VMM_CTR1(vmx->vm, vcpu, "astpending vmexit at 0x%0lx", rip);
907 vmx_set_pcpu_defaults(struct vmx *vmx, int vcpu)
910 struct vmxstate *vmxstate;
911 struct invvpid_desc invvpid_desc = { 0 };
913 vmxstate = &vmx->state[vcpu];
914 lastcpu = vmxstate->lastcpu;
915 vmxstate->lastcpu = curcpu;
917 if (lastcpu == curcpu) {
922 vmm_stat_incr(vmx->vm, vcpu, VCPU_MIGRATIONS, 1);
924 error = vmwrite(VMCS_HOST_TR_BASE, vmm_get_host_trbase());
928 error = vmwrite(VMCS_HOST_GDTR_BASE, vmm_get_host_gdtrbase());
932 error = vmwrite(VMCS_HOST_GS_BASE, vmm_get_host_gsbase());
937 * If we are using VPIDs then invalidate all mappings tagged with 'vpid'
939 * We do this because this vcpu was executing on a different host
940 * cpu when it last ran. We do not track whether it invalidated
941 * mappings associated with its 'vpid' during that run. So we must
942 * assume that the mappings associated with 'vpid' on 'curcpu' are
943 * stale and invalidate them.
945 * Note that we incur this penalty only when the scheduler chooses to
946 * move the thread associated with this vcpu between host cpus.
948 * Note also that this will invalidate mappings tagged with 'vpid'
951 if (vmxstate->vpid != 0) {
952 invvpid_desc.vpid = vmxstate->vpid;
953 invvpid(INVVPID_TYPE_SINGLE_CONTEXT, invvpid_desc);
960 vm_exit_update_rip(struct vm_exit *vmexit)
964 error = vmwrite(VMCS_GUEST_RIP, vmexit->rip + vmexit->inst_length);
966 panic("vmx_run: error %d writing to VMCS_GUEST_RIP", error);
970 * We depend on 'procbased_ctls' to have the Interrupt Window Exiting bit set.
972 CTASSERT((PROCBASED_CTLS_ONE_SETTING & PROCBASED_INT_WINDOW_EXITING) != 0);
975 vmx_set_int_window_exiting(struct vmx *vmx, int vcpu)
979 vmx->cap[vcpu].proc_ctls |= PROCBASED_INT_WINDOW_EXITING;
981 error = vmwrite(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
983 panic("vmx_set_int_window_exiting: vmwrite error %d", error);
987 vmx_clear_int_window_exiting(struct vmx *vmx, int vcpu)
991 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_INT_WINDOW_EXITING;
993 error = vmwrite(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
995 panic("vmx_clear_int_window_exiting: vmwrite error %d", error);
999 vmx_set_nmi_window_exiting(struct vmx *vmx, int vcpu)
1003 vmx->cap[vcpu].proc_ctls |= PROCBASED_NMI_WINDOW_EXITING;
1005 error = vmwrite(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1007 panic("vmx_set_nmi_window_exiting: vmwrite error %d", error);
1010 static void __inline
1011 vmx_clear_nmi_window_exiting(struct vmx *vmx, int vcpu)
1015 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_NMI_WINDOW_EXITING;
1017 error = vmwrite(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1019 panic("vmx_clear_nmi_window_exiting: vmwrite error %d", error);
1023 vmx_inject_nmi(struct vmx *vmx, int vcpu)
1026 uint64_t info, interruptibility;
1028 /* Bail out if no NMI requested */
1029 if (!vm_nmi_pending(vmx->vm, vcpu))
1032 error = vmread(VMCS_GUEST_INTERRUPTIBILITY, &interruptibility);
1034 panic("vmx_inject_nmi: vmread(interruptibility) %d",
1037 if (interruptibility & nmi_blocking_bits)
1041 * Inject the virtual NMI. The vector must be the NMI IDT entry
1042 * or the VMCS entry check will fail.
1044 info = VMCS_INTERRUPTION_INFO_NMI | VMCS_INTERRUPTION_INFO_VALID;
1047 error = vmwrite(VMCS_ENTRY_INTR_INFO, info);
1049 panic("vmx_inject_nmi: vmwrite(intrinfo) %d", error);
1051 VMM_CTR0(vmx->vm, vcpu, "Injecting vNMI");
1053 /* Clear the request */
1054 vm_nmi_clear(vmx->vm, vcpu);
1059 * Set the NMI Window Exiting execution control so we can inject
1060 * the virtual NMI as soon as blocking condition goes away.
1062 vmx_set_nmi_window_exiting(vmx, vcpu);
1064 VMM_CTR0(vmx->vm, vcpu, "Enabling NMI window exiting");
1069 vmx_inject_interrupts(struct vmx *vmx, int vcpu)
1072 uint64_t info, rflags, interruptibility;
1074 const int HWINTR_BLOCKED = VMCS_INTERRUPTIBILITY_STI_BLOCKING |
1075 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING;
1078 * If there is already an interrupt pending then just return.
1080 * This could happen if an interrupt was injected on a prior
1081 * VM entry but the actual entry into guest mode was aborted
1082 * because of a pending AST.
1084 error = vmread(VMCS_ENTRY_INTR_INFO, &info);
1086 panic("vmx_inject_interrupts: vmread(intrinfo) %d", error);
1087 if (info & VMCS_INTERRUPTION_INFO_VALID)
1091 * NMI injection has priority so deal with those first
1093 if (vmx_inject_nmi(vmx, vcpu))
1096 /* Ask the local apic for a vector to inject */
1097 vector = lapic_pending_intr(vmx->vm, vcpu);
1101 if (vector < 32 || vector > 255)
1102 panic("vmx_inject_interrupts: invalid vector %d\n", vector);
1104 /* Check RFLAGS.IF and the interruptibility state of the guest */
1105 error = vmread(VMCS_GUEST_RFLAGS, &rflags);
1107 panic("vmx_inject_interrupts: vmread(rflags) %d", error);
1109 if ((rflags & PSL_I) == 0)
1112 error = vmread(VMCS_GUEST_INTERRUPTIBILITY, &interruptibility);
1114 panic("vmx_inject_interrupts: vmread(interruptibility) %d",
1117 if (interruptibility & HWINTR_BLOCKED)
1120 /* Inject the interrupt */
1121 info = VMCS_INTERRUPTION_INFO_HW_INTR | VMCS_INTERRUPTION_INFO_VALID;
1123 error = vmwrite(VMCS_ENTRY_INTR_INFO, info);
1125 panic("vmx_inject_interrupts: vmwrite(intrinfo) %d", error);
1127 /* Update the Local APIC ISR */
1128 lapic_intr_accepted(vmx->vm, vcpu, vector);
1130 VMM_CTR1(vmx->vm, vcpu, "Injecting hwintr at vector %d", vector);
1136 * Set the Interrupt Window Exiting execution control so we can inject
1137 * the interrupt as soon as blocking condition goes away.
1139 vmx_set_int_window_exiting(vmx, vcpu);
1141 VMM_CTR0(vmx->vm, vcpu, "Enabling interrupt window exiting");
1145 vmx_emulate_cr_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1147 int error, cr, vmcs_guest_cr, vmcs_shadow_cr;
1148 uint64_t crval, regval, ones_mask, zeros_mask;
1149 const struct vmxctx *vmxctx;
1151 /* We only handle mov to %cr0 or %cr4 at this time */
1152 if ((exitqual & 0xf0) != 0x00)
1155 cr = exitqual & 0xf;
1156 if (cr != 0 && cr != 4)
1159 vmxctx = &vmx->ctx[vcpu];
1162 * We must use vmwrite() directly here because vmcs_setreg() will
1163 * call vmclear(vmcs) as a side-effect which we certainly don't want.
1165 switch ((exitqual >> 8) & 0xf) {
1167 regval = vmxctx->guest_rax;
1170 regval = vmxctx->guest_rcx;
1173 regval = vmxctx->guest_rdx;
1176 regval = vmxctx->guest_rbx;
1179 error = vmread(VMCS_GUEST_RSP, ®val);
1181 panic("vmx_emulate_cr_access: "
1182 "error %d reading guest rsp", error);
1186 regval = vmxctx->guest_rbp;
1189 regval = vmxctx->guest_rsi;
1192 regval = vmxctx->guest_rdi;
1195 regval = vmxctx->guest_r8;
1198 regval = vmxctx->guest_r9;
1201 regval = vmxctx->guest_r10;
1204 regval = vmxctx->guest_r11;
1207 regval = vmxctx->guest_r12;
1210 regval = vmxctx->guest_r13;
1213 regval = vmxctx->guest_r14;
1216 regval = vmxctx->guest_r15;
1221 ones_mask = cr0_ones_mask;
1222 zeros_mask = cr0_zeros_mask;
1223 vmcs_guest_cr = VMCS_GUEST_CR0;
1224 vmcs_shadow_cr = VMCS_CR0_SHADOW;
1226 ones_mask = cr4_ones_mask;
1227 zeros_mask = cr4_zeros_mask;
1228 vmcs_guest_cr = VMCS_GUEST_CR4;
1229 vmcs_shadow_cr = VMCS_CR4_SHADOW;
1232 error = vmwrite(vmcs_shadow_cr, regval);
1234 panic("vmx_emulate_cr_access: error %d writing cr%d shadow",
1238 crval = regval | ones_mask;
1239 crval &= ~zeros_mask;
1240 error = vmwrite(vmcs_guest_cr, crval);
1242 panic("vmx_emulate_cr_access: error %d writing cr%d",
1246 if (cr == 0 && regval & CR0_PG) {
1247 uint64_t efer, entry_ctls;
1250 * If CR0.PG is 1 and EFER.LME is 1 then EFER.LMA and
1251 * the "IA-32e mode guest" bit in VM-entry control must be
1254 error = vmread(VMCS_GUEST_IA32_EFER, &efer);
1256 panic("vmx_emulate_cr_access: error %d efer read",
1259 if (efer & EFER_LME) {
1261 error = vmwrite(VMCS_GUEST_IA32_EFER, efer);
1263 panic("vmx_emulate_cr_access: error %d"
1264 " efer write", error);
1266 error = vmread(VMCS_ENTRY_CTLS, &entry_ctls);
1268 panic("vmx_emulate_cr_access: error %d"
1269 " entry ctls read", error);
1271 entry_ctls |= VM_ENTRY_GUEST_LMA;
1272 error = vmwrite(VMCS_ENTRY_CTLS, entry_ctls);
1274 panic("vmx_emulate_cr_access: error %d"
1275 " entry ctls write", error);
1284 ept_fault_type(uint64_t ept_qual)
1288 if (ept_qual & EPT_VIOLATION_DATA_WRITE)
1289 fault_type = VM_PROT_WRITE;
1290 else if (ept_qual & EPT_VIOLATION_INST_FETCH)
1291 fault_type = VM_PROT_EXECUTE;
1293 fault_type= VM_PROT_READ;
1295 return (fault_type);
1299 ept_protection(uint64_t ept_qual)
1303 if (ept_qual & EPT_VIOLATION_GPA_READABLE)
1304 prot |= VM_PROT_READ;
1305 if (ept_qual & EPT_VIOLATION_GPA_WRITEABLE)
1306 prot |= VM_PROT_WRITE;
1307 if (ept_qual & EPT_VIOLATION_GPA_EXECUTABLE)
1308 prot |= VM_PROT_EXECUTE;
1314 ept_emulation_fault(uint64_t ept_qual)
1318 /* EPT fault on an instruction fetch doesn't make sense here */
1319 if (ept_qual & EPT_VIOLATION_INST_FETCH)
1322 /* EPT fault must be a read fault or a write fault */
1323 read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0;
1324 write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0;
1325 if ((read | write) == 0)
1329 * The EPT violation must have been caused by accessing a
1330 * guest-physical address that is a translation of a guest-linear
1333 if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 ||
1334 (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) {
1342 vmx_exit_process(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
1346 struct vmxctx *vmxctx;
1347 uint32_t eax, ecx, edx, idtvec_info, idtvec_err, reason;
1351 vmcs = &vmx->vmcs[vcpu];
1352 vmxctx = &vmx->ctx[vcpu];
1353 qual = vmexit->u.vmx.exit_qualification;
1354 reason = vmexit->u.vmx.exit_reason;
1355 vmexit->exitcode = VM_EXITCODE_BOGUS;
1357 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_COUNT, 1);
1360 * VM exits that could be triggered during event injection on the
1361 * previous VM entry need to be handled specially by re-injecting
1364 * See "Information for VM Exits During Event Delivery" in Intel SDM
1368 case EXIT_REASON_EPT_FAULT:
1369 case EXIT_REASON_EPT_MISCONFIG:
1370 case EXIT_REASON_APIC:
1371 case EXIT_REASON_TASK_SWITCH:
1372 case EXIT_REASON_EXCEPTION:
1373 idtvec_info = vmcs_idt_vectoring_info();
1374 if (idtvec_info & VMCS_IDT_VEC_VALID) {
1375 idtvec_info &= ~(1 << 12); /* clear undefined bit */
1376 vmwrite(VMCS_ENTRY_INTR_INFO, idtvec_info);
1377 if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) {
1378 idtvec_err = vmcs_idt_vectoring_err();
1379 vmwrite(VMCS_ENTRY_EXCEPTION_ERROR, idtvec_err);
1381 vmwrite(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length);
1388 case EXIT_REASON_CR_ACCESS:
1389 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CR_ACCESS, 1);
1390 handled = vmx_emulate_cr_access(vmx, vcpu, qual);
1392 case EXIT_REASON_RDMSR:
1393 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_RDMSR, 1);
1394 ecx = vmxctx->guest_rcx;
1395 error = emulate_rdmsr(vmx->vm, vcpu, ecx);
1397 vmexit->exitcode = VM_EXITCODE_RDMSR;
1398 vmexit->u.msr.code = ecx;
1402 case EXIT_REASON_WRMSR:
1403 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_WRMSR, 1);
1404 eax = vmxctx->guest_rax;
1405 ecx = vmxctx->guest_rcx;
1406 edx = vmxctx->guest_rdx;
1407 error = emulate_wrmsr(vmx->vm, vcpu, ecx,
1408 (uint64_t)edx << 32 | eax);
1410 vmexit->exitcode = VM_EXITCODE_WRMSR;
1411 vmexit->u.msr.code = ecx;
1412 vmexit->u.msr.wval = (uint64_t)edx << 32 | eax;
1416 case EXIT_REASON_HLT:
1417 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_HLT, 1);
1418 vmexit->exitcode = VM_EXITCODE_HLT;
1420 case EXIT_REASON_MTF:
1421 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_MTRAP, 1);
1422 vmexit->exitcode = VM_EXITCODE_MTRAP;
1424 case EXIT_REASON_PAUSE:
1425 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_PAUSE, 1);
1426 vmexit->exitcode = VM_EXITCODE_PAUSE;
1428 case EXIT_REASON_INTR_WINDOW:
1429 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INTR_WINDOW, 1);
1430 vmx_clear_int_window_exiting(vmx, vcpu);
1431 VMM_CTR0(vmx->vm, vcpu, "Disabling interrupt window exiting");
1433 case EXIT_REASON_EXT_INTR:
1435 * External interrupts serve only to cause VM exits and allow
1436 * the host interrupt handler to run.
1438 * If this external interrupt triggers a virtual interrupt
1439 * to a VM, then that state will be recorded by the
1440 * host interrupt handler in the VM's softc. We will inject
1441 * this virtual interrupt during the subsequent VM enter.
1445 * This is special. We want to treat this as an 'handled'
1446 * VM-exit but not increment the instruction pointer.
1448 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXTINT, 1);
1450 case EXIT_REASON_NMI_WINDOW:
1451 /* Exit to allow the pending virtual NMI to be injected */
1452 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NMI_WINDOW, 1);
1453 vmx_clear_nmi_window_exiting(vmx, vcpu);
1454 VMM_CTR0(vmx->vm, vcpu, "Disabling NMI window exiting");
1456 case EXIT_REASON_INOUT:
1457 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INOUT, 1);
1458 vmexit->exitcode = VM_EXITCODE_INOUT;
1459 vmexit->u.inout.bytes = (qual & 0x7) + 1;
1460 vmexit->u.inout.in = (qual & 0x8) ? 1 : 0;
1461 vmexit->u.inout.string = (qual & 0x10) ? 1 : 0;
1462 vmexit->u.inout.rep = (qual & 0x20) ? 1 : 0;
1463 vmexit->u.inout.port = (uint16_t)(qual >> 16);
1464 vmexit->u.inout.eax = (uint32_t)(vmxctx->guest_rax);
1466 case EXIT_REASON_CPUID:
1467 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CPUID, 1);
1468 handled = vmx_handle_cpuid(vmx->vm, vcpu, vmxctx);
1470 case EXIT_REASON_EPT_FAULT:
1471 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EPT_FAULT, 1);
1473 * If 'gpa' lies within the address space allocated to
1474 * memory then this must be a nested page fault otherwise
1475 * this must be an instruction that accesses MMIO space.
1478 if (vm_mem_allocated(vmx->vm, gpa)) {
1479 vmexit->exitcode = VM_EXITCODE_PAGING;
1480 vmexit->u.paging.gpa = gpa;
1481 vmexit->u.paging.fault_type = ept_fault_type(qual);
1482 vmexit->u.paging.protection = ept_protection(qual);
1483 } else if (ept_emulation_fault(qual)) {
1484 vmexit->exitcode = VM_EXITCODE_INST_EMUL;
1485 vmexit->u.inst_emul.gpa = gpa;
1486 vmexit->u.inst_emul.gla = vmcs_gla();
1487 vmexit->u.inst_emul.cr3 = vmcs_guest_cr3();
1491 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_UNKNOWN, 1);
1497 * It is possible that control is returned to userland
1498 * even though we were able to handle the VM exit in the
1501 * In such a case we want to make sure that the userland
1502 * restarts guest execution at the instruction *after*
1503 * the one we just processed. Therefore we update the
1504 * guest rip in the VMCS and in 'vmexit'.
1506 vm_exit_update_rip(vmexit);
1507 vmexit->rip += vmexit->inst_length;
1508 vmexit->inst_length = 0;
1510 if (vmexit->exitcode == VM_EXITCODE_BOGUS) {
1512 * If this VM exit was not claimed by anybody then
1513 * treat it as a generic VMX exit.
1515 vmexit->exitcode = VM_EXITCODE_VMX;
1516 vmexit->u.vmx.error = 0;
1519 * The exitcode and collateral have been populated.
1520 * The VM exit will be processed further in userland.
1528 vmx_run(void *arg, int vcpu, register_t rip, pmap_t pmap)
1530 int error, vie, rc, handled, astpending;
1531 uint32_t exit_reason;
1533 struct vmxctx *vmxctx;
1535 struct vm_exit *vmexit;
1538 vmcs = &vmx->vmcs[vcpu];
1539 vmxctx = &vmx->ctx[vcpu];
1540 vmxctx->launched = 0;
1543 vmexit = vm_exitinfo(vmx->vm, vcpu);
1545 KASSERT(vmxctx->pmap == pmap,
1546 ("pmap %p different than ctx pmap %p", pmap, vmxctx->pmap));
1547 KASSERT(vmxctx->eptp == vmx->eptp,
1548 ("eptp %p different than ctx eptp %#lx", eptp, vmxctx->eptp));
1551 * XXX Can we avoid doing this every time we do a vm run?
1557 * We do this every time because we may setup the virtual machine
1558 * from a different process than the one that actually runs it.
1560 * If the life of a virtual machine was spent entirely in the context
1561 * of a single process we could do this once in vmcs_set_defaults().
1563 if ((error = vmwrite(VMCS_HOST_CR3, rcr3())) != 0)
1564 panic("vmx_run: error %d writing to VMCS_HOST_CR3", error);
1566 if ((error = vmwrite(VMCS_GUEST_RIP, rip)) != 0)
1567 panic("vmx_run: error %d writing to VMCS_GUEST_RIP", error);
1569 if ((error = vmx_set_pcpu_defaults(vmx, vcpu)) != 0)
1570 panic("vmx_run: error %d setting up pcpu defaults", error);
1573 lapic_timer_tick(vmx->vm, vcpu);
1574 vmx_inject_interrupts(vmx, vcpu);
1575 vmx_run_trace(vmx, vcpu);
1576 rc = vmx_setjmp(vmxctx);
1578 vmx_setjmp_trace(vmx, vcpu, vmxctx, rc);
1581 case VMX_RETURN_DIRECT:
1582 if (vmxctx->launched == 0) {
1583 vmxctx->launched = 1;
1587 panic("vmx_launch/resume should not return");
1589 case VMX_RETURN_LONGJMP:
1590 break; /* vm exit */
1591 case VMX_RETURN_AST:
1594 case VMX_RETURN_VMRESUME:
1595 vie = vmcs_instruction_error();
1596 if (vmxctx->launch_error == VM_FAIL_INVALID ||
1597 vie != VMRESUME_WITH_NON_LAUNCHED_VMCS) {
1598 printf("vmresume error %d vmcs inst error %d\n",
1599 vmxctx->launch_error, vie);
1602 vmx_launch(vmxctx); /* try to launch the guest */
1603 panic("vmx_launch should not return");
1605 case VMX_RETURN_VMLAUNCH:
1606 vie = vmcs_instruction_error();
1608 printf("vmlaunch error %d vmcs inst error %d\n",
1609 vmxctx->launch_error, vie);
1612 case VMX_RETURN_INVEPT:
1613 panic("vm %s:%d invept error %d",
1614 vm_name(vmx->vm), vcpu, vmxctx->launch_error);
1616 panic("vmx_setjmp returned %d", rc);
1619 /* enable interrupts */
1622 /* collect some basic information for VM exit processing */
1623 vmexit->rip = rip = vmcs_guest_rip();
1624 vmexit->inst_length = vmexit_instruction_length();
1625 vmexit->u.vmx.exit_reason = exit_reason = vmcs_exit_reason();
1626 vmexit->u.vmx.exit_qualification = vmcs_exit_qualification();
1630 vmexit->inst_length = 0;
1631 vmexit->exitcode = VM_EXITCODE_BOGUS;
1632 vmx_astpending_trace(vmx, vcpu, rip);
1633 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_ASTPENDING, 1);
1637 handled = vmx_exit_process(vmx, vcpu, vmexit);
1638 vmx_exit_trace(vmx, vcpu, rip, exit_reason, handled);
1643 * If a VM exit has been handled then the exitcode must be BOGUS
1644 * If a VM exit is not handled then the exitcode must not be BOGUS
1646 if ((handled && vmexit->exitcode != VM_EXITCODE_BOGUS) ||
1647 (!handled && vmexit->exitcode == VM_EXITCODE_BOGUS)) {
1648 panic("Mismatch between handled (%d) and exitcode (%d)",
1649 handled, vmexit->exitcode);
1653 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_USERSPACE, 1);
1655 VMM_CTR1(vmx->vm, vcpu, "goto userland: exitcode %d",vmexit->exitcode);
1659 * We need to do this to ensure that any VMCS state cached by the
1660 * processor is flushed to memory. We need to do this in case the
1661 * VM moves to a different cpu the next time it runs.
1663 * Can we avoid doing this?
1669 vmexit->exitcode = VM_EXITCODE_VMX;
1670 vmexit->u.vmx.exit_reason = (uint32_t)-1;
1671 vmexit->u.vmx.exit_qualification = (uint32_t)-1;
1672 vmexit->u.vmx.error = vie;
1678 vmx_vmcleanup(void *arg)
1681 struct vmx *vmx = arg;
1683 for (i = 0; i < VM_MAXCPU; i++)
1684 vpid_free(vmx->state[i].vpid);
1687 * XXXSMP we also need to clear the VMCS active on the other vcpus.
1689 error = vmclear(&vmx->vmcs[0]);
1691 panic("vmx_vmcleanup: vmclear error %d on vcpu 0", error);
1699 vmxctx_regptr(struct vmxctx *vmxctx, int reg)
1703 case VM_REG_GUEST_RAX:
1704 return (&vmxctx->guest_rax);
1705 case VM_REG_GUEST_RBX:
1706 return (&vmxctx->guest_rbx);
1707 case VM_REG_GUEST_RCX:
1708 return (&vmxctx->guest_rcx);
1709 case VM_REG_GUEST_RDX:
1710 return (&vmxctx->guest_rdx);
1711 case VM_REG_GUEST_RSI:
1712 return (&vmxctx->guest_rsi);
1713 case VM_REG_GUEST_RDI:
1714 return (&vmxctx->guest_rdi);
1715 case VM_REG_GUEST_RBP:
1716 return (&vmxctx->guest_rbp);
1717 case VM_REG_GUEST_R8:
1718 return (&vmxctx->guest_r8);
1719 case VM_REG_GUEST_R9:
1720 return (&vmxctx->guest_r9);
1721 case VM_REG_GUEST_R10:
1722 return (&vmxctx->guest_r10);
1723 case VM_REG_GUEST_R11:
1724 return (&vmxctx->guest_r11);
1725 case VM_REG_GUEST_R12:
1726 return (&vmxctx->guest_r12);
1727 case VM_REG_GUEST_R13:
1728 return (&vmxctx->guest_r13);
1729 case VM_REG_GUEST_R14:
1730 return (&vmxctx->guest_r14);
1731 case VM_REG_GUEST_R15:
1732 return (&vmxctx->guest_r15);
1740 vmxctx_getreg(struct vmxctx *vmxctx, int reg, uint64_t *retval)
1744 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
1752 vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val)
1756 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
1764 vmx_shadow_reg(int reg)
1771 case VM_REG_GUEST_CR0:
1772 shreg = VMCS_CR0_SHADOW;
1774 case VM_REG_GUEST_CR4:
1775 shreg = VMCS_CR4_SHADOW;
1785 vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval)
1787 int running, hostcpu;
1788 struct vmx *vmx = arg;
1790 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
1791 if (running && hostcpu != curcpu)
1792 panic("vmx_getreg: %s%d is running", vm_name(vmx->vm), vcpu);
1794 if (vmxctx_getreg(&vmx->ctx[vcpu], reg, retval) == 0)
1797 return (vmcs_getreg(&vmx->vmcs[vcpu], running, reg, retval));
1801 vmx_setreg(void *arg, int vcpu, int reg, uint64_t val)
1803 int error, hostcpu, running, shadow;
1805 struct vmx *vmx = arg;
1807 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
1808 if (running && hostcpu != curcpu)
1809 panic("vmx_setreg: %s%d is running", vm_name(vmx->vm), vcpu);
1811 if (vmxctx_setreg(&vmx->ctx[vcpu], reg, val) == 0)
1814 error = vmcs_setreg(&vmx->vmcs[vcpu], running, reg, val);
1818 * If the "load EFER" VM-entry control is 1 then the
1819 * value of EFER.LMA must be identical to "IA-32e mode guest"
1820 * bit in the VM-entry control.
1822 if ((entry_ctls & VM_ENTRY_LOAD_EFER) != 0 &&
1823 (reg == VM_REG_GUEST_EFER)) {
1824 vmcs_getreg(&vmx->vmcs[vcpu], running,
1825 VMCS_IDENT(VMCS_ENTRY_CTLS), &ctls);
1827 ctls |= VM_ENTRY_GUEST_LMA;
1829 ctls &= ~VM_ENTRY_GUEST_LMA;
1830 vmcs_setreg(&vmx->vmcs[vcpu], running,
1831 VMCS_IDENT(VMCS_ENTRY_CTLS), ctls);
1834 shadow = vmx_shadow_reg(reg);
1837 * Store the unmodified value in the shadow
1839 error = vmcs_setreg(&vmx->vmcs[vcpu], running,
1840 VMCS_IDENT(shadow), val);
1848 vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
1850 struct vmx *vmx = arg;
1852 return (vmcs_getdesc(&vmx->vmcs[vcpu], reg, desc));
1856 vmx_setdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
1858 struct vmx *vmx = arg;
1860 return (vmcs_setdesc(&vmx->vmcs[vcpu], reg, desc));
1864 vmx_inject(void *arg, int vcpu, int type, int vector, uint32_t code,
1869 struct vmx *vmx = arg;
1870 struct vmcs *vmcs = &vmx->vmcs[vcpu];
1872 static uint32_t type_map[VM_EVENT_MAX] = {
1873 0x1, /* VM_EVENT_NONE */
1874 0x0, /* VM_HW_INTR */
1876 0x3, /* VM_HW_EXCEPTION */
1877 0x4, /* VM_SW_INTR */
1878 0x5, /* VM_PRIV_SW_EXCEPTION */
1879 0x6, /* VM_SW_EXCEPTION */
1883 * If there is already an exception pending to be delivered to the
1884 * vcpu then just return.
1886 error = vmcs_getreg(vmcs, 0, VMCS_IDENT(VMCS_ENTRY_INTR_INFO), &info);
1890 if (info & VMCS_INTERRUPTION_INFO_VALID)
1893 info = vector | (type_map[type] << 8) | (code_valid ? 1 << 11 : 0);
1894 info |= VMCS_INTERRUPTION_INFO_VALID;
1895 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(VMCS_ENTRY_INTR_INFO), info);
1900 error = vmcs_setreg(vmcs, 0,
1901 VMCS_IDENT(VMCS_ENTRY_EXCEPTION_ERROR),
1908 vmx_getcap(void *arg, int vcpu, int type, int *retval)
1910 struct vmx *vmx = arg;
1916 vcap = vmx->cap[vcpu].set;
1919 case VM_CAP_HALT_EXIT:
1923 case VM_CAP_PAUSE_EXIT:
1927 case VM_CAP_MTRAP_EXIT:
1928 if (cap_monitor_trap)
1931 case VM_CAP_UNRESTRICTED_GUEST:
1932 if (cap_unrestricted_guest)
1940 *retval = (vcap & (1 << type)) ? 1 : 0;
1946 vmx_setcap(void *arg, int vcpu, int type, int val)
1948 struct vmx *vmx = arg;
1949 struct vmcs *vmcs = &vmx->vmcs[vcpu];
1961 case VM_CAP_HALT_EXIT:
1962 if (cap_halt_exit) {
1964 pptr = &vmx->cap[vcpu].proc_ctls;
1966 flag = PROCBASED_HLT_EXITING;
1967 reg = VMCS_PRI_PROC_BASED_CTLS;
1970 case VM_CAP_MTRAP_EXIT:
1971 if (cap_monitor_trap) {
1973 pptr = &vmx->cap[vcpu].proc_ctls;
1975 flag = PROCBASED_MTF;
1976 reg = VMCS_PRI_PROC_BASED_CTLS;
1979 case VM_CAP_PAUSE_EXIT:
1980 if (cap_pause_exit) {
1982 pptr = &vmx->cap[vcpu].proc_ctls;
1984 flag = PROCBASED_PAUSE_EXITING;
1985 reg = VMCS_PRI_PROC_BASED_CTLS;
1988 case VM_CAP_UNRESTRICTED_GUEST:
1989 if (cap_unrestricted_guest) {
1991 baseval = procbased_ctls2;
1992 flag = PROCBASED2_UNRESTRICTED_GUEST;
1993 reg = VMCS_SEC_PROC_BASED_CTLS;
2007 error = vmwrite(reg, baseval);
2014 * Update optional stored flags, and record
2022 vmx->cap[vcpu].set |= (1 << type);
2024 vmx->cap[vcpu].set &= ~(1 << type);
2032 struct vmm_ops vmm_ops_intel = {