2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
6 * Copyright (c) 2018 Joyent, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_bhyve_snapshot.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
45 #include <sys/sysctl.h>
50 #include <machine/psl.h>
51 #include <machine/cpufunc.h>
52 #include <machine/md_var.h>
53 #include <machine/reg.h>
54 #include <machine/segments.h>
55 #include <machine/smp.h>
56 #include <machine/specialreg.h>
57 #include <machine/vmparam.h>
59 #include <machine/vmm.h>
60 #include <machine/vmm_dev.h>
61 #include <machine/vmm_instruction_emul.h>
62 #include <machine/vmm_snapshot.h>
64 #include "vmm_lapic.h"
66 #include "vmm_ioport.h"
71 #include "vlapic_priv.h"
74 #include "vmx_cpufunc.h"
78 #include "vmx_controls.h"
80 #define PINBASED_CTLS_ONE_SETTING \
81 (PINBASED_EXTINT_EXITING | \
82 PINBASED_NMI_EXITING | \
84 #define PINBASED_CTLS_ZERO_SETTING 0
86 #define PROCBASED_CTLS_WINDOW_SETTING \
87 (PROCBASED_INT_WINDOW_EXITING | \
88 PROCBASED_NMI_WINDOW_EXITING)
90 #define PROCBASED_CTLS_ONE_SETTING \
91 (PROCBASED_SECONDARY_CONTROLS | \
92 PROCBASED_MWAIT_EXITING | \
93 PROCBASED_MONITOR_EXITING | \
94 PROCBASED_IO_EXITING | \
95 PROCBASED_MSR_BITMAPS | \
96 PROCBASED_CTLS_WINDOW_SETTING | \
97 PROCBASED_CR8_LOAD_EXITING | \
98 PROCBASED_CR8_STORE_EXITING)
99 #define PROCBASED_CTLS_ZERO_SETTING \
100 (PROCBASED_CR3_LOAD_EXITING | \
101 PROCBASED_CR3_STORE_EXITING | \
102 PROCBASED_IO_BITMAPS)
104 #define PROCBASED_CTLS2_ONE_SETTING PROCBASED2_ENABLE_EPT
105 #define PROCBASED_CTLS2_ZERO_SETTING 0
107 #define VM_EXIT_CTLS_ONE_SETTING \
108 (VM_EXIT_SAVE_DEBUG_CONTROLS | \
110 VM_EXIT_SAVE_EFER | \
111 VM_EXIT_LOAD_EFER | \
112 VM_EXIT_ACKNOWLEDGE_INTERRUPT)
114 #define VM_EXIT_CTLS_ZERO_SETTING 0
116 #define VM_ENTRY_CTLS_ONE_SETTING \
117 (VM_ENTRY_LOAD_DEBUG_CONTROLS | \
120 #define VM_ENTRY_CTLS_ZERO_SETTING \
121 (VM_ENTRY_INTO_SMM | \
122 VM_ENTRY_DEACTIVATE_DUAL_MONITOR)
127 static MALLOC_DEFINE(M_VMX, "vmx", "vmx");
128 static MALLOC_DEFINE(M_VLAPIC, "vlapic", "vlapic");
130 SYSCTL_DECL(_hw_vmm);
131 SYSCTL_NODE(_hw_vmm, OID_AUTO, vmx, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
134 int vmxon_enabled[MAXCPU];
135 static char vmxon_region[MAXCPU][PAGE_SIZE] __aligned(PAGE_SIZE);
137 static uint32_t pinbased_ctls, procbased_ctls, procbased_ctls2;
138 static uint32_t exit_ctls, entry_ctls;
140 static uint64_t cr0_ones_mask, cr0_zeros_mask;
141 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_ones_mask, CTLFLAG_RD,
142 &cr0_ones_mask, 0, NULL);
143 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_zeros_mask, CTLFLAG_RD,
144 &cr0_zeros_mask, 0, NULL);
146 static uint64_t cr4_ones_mask, cr4_zeros_mask;
147 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_ones_mask, CTLFLAG_RD,
148 &cr4_ones_mask, 0, NULL);
149 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_zeros_mask, CTLFLAG_RD,
150 &cr4_zeros_mask, 0, NULL);
152 static int vmx_initialized;
153 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, initialized, CTLFLAG_RD,
154 &vmx_initialized, 0, "Intel VMX initialized");
157 * Optional capabilities
159 static SYSCTL_NODE(_hw_vmm_vmx, OID_AUTO, cap,
160 CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
163 static int cap_halt_exit;
164 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, halt_exit, CTLFLAG_RD, &cap_halt_exit, 0,
165 "HLT triggers a VM-exit");
167 static int cap_pause_exit;
168 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, pause_exit, CTLFLAG_RD, &cap_pause_exit,
169 0, "PAUSE triggers a VM-exit");
171 static int cap_rdpid;
172 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, rdpid, CTLFLAG_RD, &cap_rdpid, 0,
173 "Guests are allowed to use RDPID");
175 static int cap_rdtscp;
176 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, rdtscp, CTLFLAG_RD, &cap_rdtscp, 0,
177 "Guests are allowed to use RDTSCP");
179 static int cap_unrestricted_guest;
180 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, unrestricted_guest, CTLFLAG_RD,
181 &cap_unrestricted_guest, 0, "Unrestricted guests");
183 static int cap_monitor_trap;
184 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, monitor_trap, CTLFLAG_RD,
185 &cap_monitor_trap, 0, "Monitor trap flag");
187 static int cap_invpcid;
188 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, invpcid, CTLFLAG_RD, &cap_invpcid,
189 0, "Guests are allowed to use INVPCID");
191 static int tpr_shadowing;
192 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, tpr_shadowing, CTLFLAG_RD,
193 &tpr_shadowing, 0, "TPR shadowing support");
195 static int virtual_interrupt_delivery;
196 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, virtual_interrupt_delivery, CTLFLAG_RD,
197 &virtual_interrupt_delivery, 0, "APICv virtual interrupt delivery support");
199 static int posted_interrupts;
200 SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, posted_interrupts, CTLFLAG_RD,
201 &posted_interrupts, 0, "APICv posted interrupt support");
203 static int pirvec = -1;
204 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, posted_interrupt_vector, CTLFLAG_RD,
205 &pirvec, 0, "APICv posted interrupt vector");
207 static struct unrhdr *vpid_unr;
208 static u_int vpid_alloc_failed;
209 SYSCTL_UINT(_hw_vmm_vmx, OID_AUTO, vpid_alloc_failed, CTLFLAG_RD,
210 &vpid_alloc_failed, 0, NULL);
213 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, l1d_flush, CTLFLAG_RD,
214 &guest_l1d_flush, 0, NULL);
215 int guest_l1d_flush_sw;
216 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, l1d_flush_sw, CTLFLAG_RD,
217 &guest_l1d_flush_sw, 0, NULL);
219 static struct msr_entry msr_load_list[1] __aligned(16);
222 * The definitions of SDT probes for VMX.
225 SDT_PROBE_DEFINE3(vmm, vmx, exit, entry,
226 "struct vmx *", "int", "struct vm_exit *");
228 SDT_PROBE_DEFINE4(vmm, vmx, exit, taskswitch,
229 "struct vmx *", "int", "struct vm_exit *", "struct vm_task_switch *");
231 SDT_PROBE_DEFINE4(vmm, vmx, exit, craccess,
232 "struct vmx *", "int", "struct vm_exit *", "uint64_t");
234 SDT_PROBE_DEFINE4(vmm, vmx, exit, rdmsr,
235 "struct vmx *", "int", "struct vm_exit *", "uint32_t");
237 SDT_PROBE_DEFINE5(vmm, vmx, exit, wrmsr,
238 "struct vmx *", "int", "struct vm_exit *", "uint32_t", "uint64_t");
240 SDT_PROBE_DEFINE3(vmm, vmx, exit, halt,
241 "struct vmx *", "int", "struct vm_exit *");
243 SDT_PROBE_DEFINE3(vmm, vmx, exit, mtrap,
244 "struct vmx *", "int", "struct vm_exit *");
246 SDT_PROBE_DEFINE3(vmm, vmx, exit, pause,
247 "struct vmx *", "int", "struct vm_exit *");
249 SDT_PROBE_DEFINE3(vmm, vmx, exit, intrwindow,
250 "struct vmx *", "int", "struct vm_exit *");
252 SDT_PROBE_DEFINE4(vmm, vmx, exit, interrupt,
253 "struct vmx *", "int", "struct vm_exit *", "uint32_t");
255 SDT_PROBE_DEFINE3(vmm, vmx, exit, nmiwindow,
256 "struct vmx *", "int", "struct vm_exit *");
258 SDT_PROBE_DEFINE3(vmm, vmx, exit, inout,
259 "struct vmx *", "int", "struct vm_exit *");
261 SDT_PROBE_DEFINE3(vmm, vmx, exit, cpuid,
262 "struct vmx *", "int", "struct vm_exit *");
264 SDT_PROBE_DEFINE5(vmm, vmx, exit, exception,
265 "struct vmx *", "int", "struct vm_exit *", "uint32_t", "int");
267 SDT_PROBE_DEFINE5(vmm, vmx, exit, nestedfault,
268 "struct vmx *", "int", "struct vm_exit *", "uint64_t", "uint64_t");
270 SDT_PROBE_DEFINE4(vmm, vmx, exit, mmiofault,
271 "struct vmx *", "int", "struct vm_exit *", "uint64_t");
273 SDT_PROBE_DEFINE3(vmm, vmx, exit, eoi,
274 "struct vmx *", "int", "struct vm_exit *");
276 SDT_PROBE_DEFINE3(vmm, vmx, exit, apicaccess,
277 "struct vmx *", "int", "struct vm_exit *");
279 SDT_PROBE_DEFINE4(vmm, vmx, exit, apicwrite,
280 "struct vmx *", "int", "struct vm_exit *", "struct vlapic *");
282 SDT_PROBE_DEFINE3(vmm, vmx, exit, xsetbv,
283 "struct vmx *", "int", "struct vm_exit *");
285 SDT_PROBE_DEFINE3(vmm, vmx, exit, monitor,
286 "struct vmx *", "int", "struct vm_exit *");
288 SDT_PROBE_DEFINE3(vmm, vmx, exit, mwait,
289 "struct vmx *", "int", "struct vm_exit *");
291 SDT_PROBE_DEFINE3(vmm, vmx, exit, vminsn,
292 "struct vmx *", "int", "struct vm_exit *");
294 SDT_PROBE_DEFINE4(vmm, vmx, exit, unknown,
295 "struct vmx *", "int", "struct vm_exit *", "uint32_t");
297 SDT_PROBE_DEFINE4(vmm, vmx, exit, return,
298 "struct vmx *", "int", "struct vm_exit *", "int");
301 * Use the last page below 4GB as the APIC access address. This address is
302 * occupied by the boot firmware so it is guaranteed that it will not conflict
303 * with a page in system memory.
305 #define APIC_ACCESS_ADDRESS 0xFFFFF000
307 static int vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc);
308 static int vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval);
309 static int vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val);
310 static void vmx_inject_pir(struct vlapic *vlapic);
311 #ifdef BHYVE_SNAPSHOT
312 static int vmx_restore_tsc(void *arg, int vcpu, uint64_t now);
318 return ((cpu_stdext_feature2 & CPUID_STDEXT2_RDPID) != 0);
322 host_has_rdtscp(void)
324 return ((amd_feature & AMDID_RDTSCP) != 0);
329 exit_reason_to_str(int reason)
331 static char reasonbuf[32];
334 case EXIT_REASON_EXCEPTION:
336 case EXIT_REASON_EXT_INTR:
338 case EXIT_REASON_TRIPLE_FAULT:
339 return "triplefault";
340 case EXIT_REASON_INIT:
342 case EXIT_REASON_SIPI:
344 case EXIT_REASON_IO_SMI:
346 case EXIT_REASON_SMI:
348 case EXIT_REASON_INTR_WINDOW:
350 case EXIT_REASON_NMI_WINDOW:
352 case EXIT_REASON_TASK_SWITCH:
354 case EXIT_REASON_CPUID:
356 case EXIT_REASON_GETSEC:
358 case EXIT_REASON_HLT:
360 case EXIT_REASON_INVD:
362 case EXIT_REASON_INVLPG:
364 case EXIT_REASON_RDPMC:
366 case EXIT_REASON_RDTSC:
368 case EXIT_REASON_RSM:
370 case EXIT_REASON_VMCALL:
372 case EXIT_REASON_VMCLEAR:
374 case EXIT_REASON_VMLAUNCH:
376 case EXIT_REASON_VMPTRLD:
378 case EXIT_REASON_VMPTRST:
380 case EXIT_REASON_VMREAD:
382 case EXIT_REASON_VMRESUME:
384 case EXIT_REASON_VMWRITE:
386 case EXIT_REASON_VMXOFF:
388 case EXIT_REASON_VMXON:
390 case EXIT_REASON_CR_ACCESS:
392 case EXIT_REASON_DR_ACCESS:
394 case EXIT_REASON_INOUT:
396 case EXIT_REASON_RDMSR:
398 case EXIT_REASON_WRMSR:
400 case EXIT_REASON_INVAL_VMCS:
402 case EXIT_REASON_INVAL_MSR:
404 case EXIT_REASON_MWAIT:
406 case EXIT_REASON_MTF:
408 case EXIT_REASON_MONITOR:
410 case EXIT_REASON_PAUSE:
412 case EXIT_REASON_MCE_DURING_ENTRY:
413 return "mce-during-entry";
414 case EXIT_REASON_TPR:
416 case EXIT_REASON_APIC_ACCESS:
417 return "apic-access";
418 case EXIT_REASON_GDTR_IDTR:
420 case EXIT_REASON_LDTR_TR:
422 case EXIT_REASON_EPT_FAULT:
424 case EXIT_REASON_EPT_MISCONFIG:
425 return "eptmisconfig";
426 case EXIT_REASON_INVEPT:
428 case EXIT_REASON_RDTSCP:
430 case EXIT_REASON_VMX_PREEMPT:
432 case EXIT_REASON_INVVPID:
434 case EXIT_REASON_WBINVD:
436 case EXIT_REASON_XSETBV:
438 case EXIT_REASON_APIC_WRITE:
441 snprintf(reasonbuf, sizeof(reasonbuf), "%d", reason);
448 vmx_allow_x2apic_msrs(struct vmx *vmx)
455 * Allow readonly access to the following x2APIC MSRs from the guest.
457 error += guest_msr_ro(vmx, MSR_APIC_ID);
458 error += guest_msr_ro(vmx, MSR_APIC_VERSION);
459 error += guest_msr_ro(vmx, MSR_APIC_LDR);
460 error += guest_msr_ro(vmx, MSR_APIC_SVR);
462 for (i = 0; i < 8; i++)
463 error += guest_msr_ro(vmx, MSR_APIC_ISR0 + i);
465 for (i = 0; i < 8; i++)
466 error += guest_msr_ro(vmx, MSR_APIC_TMR0 + i);
468 for (i = 0; i < 8; i++)
469 error += guest_msr_ro(vmx, MSR_APIC_IRR0 + i);
471 error += guest_msr_ro(vmx, MSR_APIC_ESR);
472 error += guest_msr_ro(vmx, MSR_APIC_LVT_TIMER);
473 error += guest_msr_ro(vmx, MSR_APIC_LVT_THERMAL);
474 error += guest_msr_ro(vmx, MSR_APIC_LVT_PCINT);
475 error += guest_msr_ro(vmx, MSR_APIC_LVT_LINT0);
476 error += guest_msr_ro(vmx, MSR_APIC_LVT_LINT1);
477 error += guest_msr_ro(vmx, MSR_APIC_LVT_ERROR);
478 error += guest_msr_ro(vmx, MSR_APIC_ICR_TIMER);
479 error += guest_msr_ro(vmx, MSR_APIC_DCR_TIMER);
480 error += guest_msr_ro(vmx, MSR_APIC_ICR);
483 * Allow TPR, EOI and SELF_IPI MSRs to be read and written by the guest.
485 * These registers get special treatment described in the section
486 * "Virtualizing MSR-Based APIC Accesses".
488 error += guest_msr_rw(vmx, MSR_APIC_TPR);
489 error += guest_msr_rw(vmx, MSR_APIC_EOI);
490 error += guest_msr_rw(vmx, MSR_APIC_SELF_IPI);
496 vmx_fix_cr0(u_long cr0)
499 return ((cr0 | cr0_ones_mask) & ~cr0_zeros_mask);
503 vmx_fix_cr4(u_long cr4)
506 return ((cr4 | cr4_ones_mask) & ~cr4_zeros_mask);
512 if (vpid < 0 || vpid > 0xffff)
513 panic("vpid_free: invalid vpid %d", vpid);
516 * VPIDs [0,VM_MAXCPU] are special and are not allocated from
517 * the unit number allocator.
520 if (vpid > VM_MAXCPU)
521 free_unr(vpid_unr, vpid);
525 vpid_alloc(uint16_t *vpid, int num)
529 if (num <= 0 || num > VM_MAXCPU)
530 panic("invalid number of vpids requested: %d", num);
533 * If the "enable vpid" execution control is not enabled then the
534 * VPID is required to be 0 for all vcpus.
536 if ((procbased_ctls2 & PROCBASED2_ENABLE_VPID) == 0) {
537 for (i = 0; i < num; i++)
543 * Allocate a unique VPID for each vcpu from the unit number allocator.
545 for (i = 0; i < num; i++) {
546 x = alloc_unr(vpid_unr);
554 atomic_add_int(&vpid_alloc_failed, 1);
557 * If the unit number allocator does not have enough unique
558 * VPIDs then we need to allocate from the [1,VM_MAXCPU] range.
560 * These VPIDs are not be unique across VMs but this does not
561 * affect correctness because the combined mappings are also
562 * tagged with the EP4TA which is unique for each VM.
564 * It is still sub-optimal because the invvpid will invalidate
565 * combined mappings for a particular VPID across all EP4TAs.
570 for (i = 0; i < num; i++)
579 * VPID 0 is required when the "enable VPID" execution control is
582 * VPIDs [1,VM_MAXCPU] are used as the "overflow namespace" when the
583 * unit number allocator does not have sufficient unique VPIDs to
584 * satisfy the allocation.
586 * The remaining VPIDs are managed by the unit number allocator.
588 vpid_unr = new_unrhdr(VM_MAXCPU + 1, 0xffff, NULL);
592 vmx_disable(void *arg __unused)
594 struct invvpid_desc invvpid_desc = { 0 };
595 struct invept_desc invept_desc = { 0 };
597 if (vmxon_enabled[curcpu]) {
599 * See sections 25.3.3.3 and 25.3.3.4 in Intel Vol 3b.
601 * VMXON or VMXOFF are not required to invalidate any TLB
602 * caching structures. This prevents potential retention of
603 * cached information in the TLB between distinct VMX episodes.
605 invvpid(INVVPID_TYPE_ALL_CONTEXTS, invvpid_desc);
606 invept(INVEPT_TYPE_ALL_CONTEXTS, invept_desc);
609 load_cr4(rcr4() & ~CR4_VMXE);
617 lapic_ipi_free(pirvec);
619 if (vpid_unr != NULL) {
620 delete_unrhdr(vpid_unr);
624 if (nmi_flush_l1d_sw == 1)
625 nmi_flush_l1d_sw = 0;
627 smp_rendezvous(NULL, vmx_disable, NULL, NULL);
633 vmx_enable(void *arg __unused)
636 uint64_t feature_control;
638 feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
639 if ((feature_control & IA32_FEATURE_CONTROL_LOCK) == 0 ||
640 (feature_control & IA32_FEATURE_CONTROL_VMX_EN) == 0) {
641 wrmsr(MSR_IA32_FEATURE_CONTROL,
642 feature_control | IA32_FEATURE_CONTROL_VMX_EN |
643 IA32_FEATURE_CONTROL_LOCK);
646 load_cr4(rcr4() | CR4_VMXE);
648 *(uint32_t *)vmxon_region[curcpu] = vmx_revision();
649 error = vmxon(vmxon_region[curcpu]);
651 vmxon_enabled[curcpu] = 1;
658 if (vmxon_enabled[curcpu])
659 vmxon(vmxon_region[curcpu]);
663 vmx_modinit(int ipinum)
666 uint64_t basic, fixed0, fixed1, feature_control;
667 uint32_t tmp, procbased2_vid_bits;
669 /* CPUID.1:ECX[bit 5] must be 1 for processor to support VMX */
670 if (!(cpu_feature2 & CPUID2_VMX)) {
671 printf("vmx_modinit: processor does not support VMX "
677 * Verify that MSR_IA32_FEATURE_CONTROL lock and VMXON enable bits
678 * are set (bits 0 and 2 respectively).
680 feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
681 if ((feature_control & IA32_FEATURE_CONTROL_LOCK) == 1 &&
682 (feature_control & IA32_FEATURE_CONTROL_VMX_EN) == 0) {
683 printf("vmx_modinit: VMX operation disabled by BIOS\n");
688 * Verify capabilities MSR_VMX_BASIC:
689 * - bit 54 indicates support for INS/OUTS decoding
691 basic = rdmsr(MSR_VMX_BASIC);
692 if ((basic & (1UL << 54)) == 0) {
693 printf("vmx_modinit: processor does not support desired basic "
698 /* Check support for primary processor-based VM-execution controls */
699 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
700 MSR_VMX_TRUE_PROCBASED_CTLS,
701 PROCBASED_CTLS_ONE_SETTING,
702 PROCBASED_CTLS_ZERO_SETTING, &procbased_ctls);
704 printf("vmx_modinit: processor does not support desired "
705 "primary processor-based controls\n");
709 /* Clear the processor-based ctl bits that are set on demand */
710 procbased_ctls &= ~PROCBASED_CTLS_WINDOW_SETTING;
712 /* Check support for secondary processor-based VM-execution controls */
713 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
714 MSR_VMX_PROCBASED_CTLS2,
715 PROCBASED_CTLS2_ONE_SETTING,
716 PROCBASED_CTLS2_ZERO_SETTING, &procbased_ctls2);
718 printf("vmx_modinit: processor does not support desired "
719 "secondary processor-based controls\n");
723 /* Check support for VPID */
724 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2,
725 PROCBASED2_ENABLE_VPID, 0, &tmp);
727 procbased_ctls2 |= PROCBASED2_ENABLE_VPID;
729 /* Check support for pin-based VM-execution controls */
730 error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS,
731 MSR_VMX_TRUE_PINBASED_CTLS,
732 PINBASED_CTLS_ONE_SETTING,
733 PINBASED_CTLS_ZERO_SETTING, &pinbased_ctls);
735 printf("vmx_modinit: processor does not support desired "
736 "pin-based controls\n");
740 /* Check support for VM-exit controls */
741 error = vmx_set_ctlreg(MSR_VMX_EXIT_CTLS, MSR_VMX_TRUE_EXIT_CTLS,
742 VM_EXIT_CTLS_ONE_SETTING,
743 VM_EXIT_CTLS_ZERO_SETTING,
746 printf("vmx_modinit: processor does not support desired "
751 /* Check support for VM-entry controls */
752 error = vmx_set_ctlreg(MSR_VMX_ENTRY_CTLS, MSR_VMX_TRUE_ENTRY_CTLS,
753 VM_ENTRY_CTLS_ONE_SETTING, VM_ENTRY_CTLS_ZERO_SETTING,
756 printf("vmx_modinit: processor does not support desired "
762 * Check support for optional features by testing them
765 cap_halt_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
766 MSR_VMX_TRUE_PROCBASED_CTLS,
767 PROCBASED_HLT_EXITING, 0,
770 cap_monitor_trap = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
771 MSR_VMX_PROCBASED_CTLS,
775 cap_pause_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
776 MSR_VMX_TRUE_PROCBASED_CTLS,
777 PROCBASED_PAUSE_EXITING, 0,
781 * Check support for RDPID and/or RDTSCP.
783 * Support a pass-through-based implementation of these via the
784 * "enable RDTSCP" VM-execution control and the "RDTSC exiting"
785 * VM-execution control.
787 * The "enable RDTSCP" VM-execution control applies to both RDPID
788 * and RDTSCP (see SDM volume 3, section 25.3, "Changes to
789 * Instruction Behavior in VMX Non-root operation"); this is why
790 * only this VM-execution control needs to be enabled in order to
791 * enable passing through whichever of RDPID and/or RDTSCP are
792 * supported by the host.
794 * The "RDTSC exiting" VM-execution control applies to both RDTSC
795 * and RDTSCP (again, per SDM volume 3, section 25.3), and is
796 * already set up for RDTSC and RDTSCP pass-through by the current
797 * implementation of RDTSC.
799 * Although RDPID and RDTSCP are optional capabilities, since there
800 * does not currently seem to be a use case for enabling/disabling
801 * these via libvmmapi, choose not to support this and, instead,
802 * just statically always enable or always disable this support
803 * across all vCPUs on all VMs. (Note that there may be some
804 * complications to providing this functionality, e.g., the MSR
805 * bitmap is currently per-VM rather than per-vCPU while the
806 * capability API wants to be able to control capabilities on a
809 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
810 MSR_VMX_PROCBASED_CTLS2,
811 PROCBASED2_ENABLE_RDTSCP, 0, &tmp);
812 cap_rdpid = error == 0 && host_has_rdpid();
813 cap_rdtscp = error == 0 && host_has_rdtscp();
814 if (cap_rdpid || cap_rdtscp)
815 procbased_ctls2 |= PROCBASED2_ENABLE_RDTSCP;
817 cap_unrestricted_guest = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
818 MSR_VMX_PROCBASED_CTLS2,
819 PROCBASED2_UNRESTRICTED_GUEST, 0,
822 cap_invpcid = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
823 MSR_VMX_PROCBASED_CTLS2, PROCBASED2_ENABLE_INVPCID, 0,
827 * Check support for TPR shadow.
829 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
830 MSR_VMX_TRUE_PROCBASED_CTLS, PROCBASED_USE_TPR_SHADOW, 0,
834 TUNABLE_INT_FETCH("hw.vmm.vmx.use_tpr_shadowing",
839 procbased_ctls |= PROCBASED_USE_TPR_SHADOW;
840 procbased_ctls &= ~PROCBASED_CR8_LOAD_EXITING;
841 procbased_ctls &= ~PROCBASED_CR8_STORE_EXITING;
845 * Check support for virtual interrupt delivery.
847 procbased2_vid_bits = (PROCBASED2_VIRTUALIZE_APIC_ACCESSES |
848 PROCBASED2_VIRTUALIZE_X2APIC_MODE |
849 PROCBASED2_APIC_REGISTER_VIRTUALIZATION |
850 PROCBASED2_VIRTUAL_INTERRUPT_DELIVERY);
852 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2,
853 procbased2_vid_bits, 0, &tmp);
854 if (error == 0 && tpr_shadowing) {
855 virtual_interrupt_delivery = 1;
856 TUNABLE_INT_FETCH("hw.vmm.vmx.use_apic_vid",
857 &virtual_interrupt_delivery);
860 if (virtual_interrupt_delivery) {
861 procbased_ctls |= PROCBASED_USE_TPR_SHADOW;
862 procbased_ctls2 |= procbased2_vid_bits;
863 procbased_ctls2 &= ~PROCBASED2_VIRTUALIZE_X2APIC_MODE;
866 * Check for Posted Interrupts only if Virtual Interrupt
867 * Delivery is enabled.
869 error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS,
870 MSR_VMX_TRUE_PINBASED_CTLS, PINBASED_POSTED_INTERRUPT, 0,
873 pirvec = lapic_ipi_alloc(pti ? &IDTVEC(justreturn1_pti) :
874 &IDTVEC(justreturn));
877 printf("vmx_modinit: unable to "
878 "allocate posted interrupt "
882 posted_interrupts = 1;
883 TUNABLE_INT_FETCH("hw.vmm.vmx.use_apic_pir",
889 if (posted_interrupts)
890 pinbased_ctls |= PINBASED_POSTED_INTERRUPT;
893 error = ept_init(ipinum);
895 printf("vmx_modinit: ept initialization failed (%d)\n", error);
899 guest_l1d_flush = (cpu_ia32_arch_caps &
900 IA32_ARCH_CAP_SKIP_L1DFL_VMENTRY) == 0;
901 TUNABLE_INT_FETCH("hw.vmm.l1d_flush", &guest_l1d_flush);
904 * L1D cache flush is enabled. Use IA32_FLUSH_CMD MSR when
905 * available. Otherwise fall back to the software flush
906 * method which loads enough data from the kernel text to
907 * flush existing L1D content, both on VMX entry and on NMI
910 if (guest_l1d_flush) {
911 if ((cpu_stdext_feature3 & CPUID_STDEXT3_L1D_FLUSH) == 0) {
912 guest_l1d_flush_sw = 1;
913 TUNABLE_INT_FETCH("hw.vmm.l1d_flush_sw",
914 &guest_l1d_flush_sw);
916 if (guest_l1d_flush_sw) {
917 if (nmi_flush_l1d_sw <= 1)
918 nmi_flush_l1d_sw = 1;
920 msr_load_list[0].index = MSR_IA32_FLUSH_CMD;
921 msr_load_list[0].val = IA32_FLUSH_CMD_L1D;
926 * Stash the cr0 and cr4 bits that must be fixed to 0 or 1
928 fixed0 = rdmsr(MSR_VMX_CR0_FIXED0);
929 fixed1 = rdmsr(MSR_VMX_CR0_FIXED1);
930 cr0_ones_mask = fixed0 & fixed1;
931 cr0_zeros_mask = ~fixed0 & ~fixed1;
934 * CR0_PE and CR0_PG can be set to zero in VMX non-root operation
935 * if unrestricted guest execution is allowed.
937 if (cap_unrestricted_guest)
938 cr0_ones_mask &= ~(CR0_PG | CR0_PE);
941 * Do not allow the guest to set CR0_NW or CR0_CD.
943 cr0_zeros_mask |= (CR0_NW | CR0_CD);
945 fixed0 = rdmsr(MSR_VMX_CR4_FIXED0);
946 fixed1 = rdmsr(MSR_VMX_CR4_FIXED1);
947 cr4_ones_mask = fixed0 & fixed1;
948 cr4_zeros_mask = ~fixed0 & ~fixed1;
954 /* enable VMX operation */
955 smp_rendezvous(NULL, vmx_enable, NULL, NULL);
963 vmx_trigger_hostintr(int vector)
966 struct gate_descriptor *gd;
970 KASSERT(vector >= 32 && vector <= 255, ("vmx_trigger_hostintr: "
971 "invalid vector %d", vector));
972 KASSERT(gd->gd_p == 1, ("gate descriptor for vector %d not present",
974 KASSERT(gd->gd_type == SDT_SYSIGT, ("gate descriptor for vector %d "
975 "has invalid type %d", vector, gd->gd_type));
976 KASSERT(gd->gd_dpl == SEL_KPL, ("gate descriptor for vector %d "
977 "has invalid dpl %d", vector, gd->gd_dpl));
978 KASSERT(gd->gd_selector == GSEL(GCODE_SEL, SEL_KPL), ("gate descriptor "
979 "for vector %d has invalid selector %d", vector, gd->gd_selector));
980 KASSERT(gd->gd_ist == 0, ("gate descriptor for vector %d has invalid "
981 "IST %d", vector, gd->gd_ist));
983 func = ((long)gd->gd_hioffset << 16 | gd->gd_looffset);
988 vmx_setup_cr_shadow(int which, struct vmcs *vmcs, uint32_t initial)
990 int error, mask_ident, shadow_ident;
993 if (which != 0 && which != 4)
994 panic("vmx_setup_cr_shadow: unknown cr%d", which);
997 mask_ident = VMCS_CR0_MASK;
998 mask_value = cr0_ones_mask | cr0_zeros_mask;
999 shadow_ident = VMCS_CR0_SHADOW;
1001 mask_ident = VMCS_CR4_MASK;
1002 mask_value = cr4_ones_mask | cr4_zeros_mask;
1003 shadow_ident = VMCS_CR4_SHADOW;
1006 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(mask_ident), mask_value);
1010 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(shadow_ident), initial);
1016 #define vmx_setup_cr0_shadow(vmcs,init) vmx_setup_cr_shadow(0, (vmcs), (init))
1017 #define vmx_setup_cr4_shadow(vmcs,init) vmx_setup_cr_shadow(4, (vmcs), (init))
1020 vmx_init(struct vm *vm, pmap_t pmap)
1022 uint16_t vpid[VM_MAXCPU];
1026 uint32_t exc_bitmap;
1029 vmx = malloc(sizeof(struct vmx), M_VMX, M_WAITOK | M_ZERO);
1030 if ((uintptr_t)vmx & PAGE_MASK) {
1031 panic("malloc of struct vmx not aligned on %d byte boundary",
1036 vmx->eptp = eptp(vtophys((vm_offset_t)pmap->pm_pmltop));
1039 * Clean up EPTP-tagged guest physical and combined mappings
1041 * VMX transitions are not required to invalidate any guest physical
1042 * mappings. So, it may be possible for stale guest physical mappings
1043 * to be present in the processor TLBs.
1045 * Combined mappings for this EP4TA are also invalidated for all VPIDs.
1047 ept_invalidate_mappings(vmx->eptp);
1049 msr_bitmap_initialize(vmx->msr_bitmap);
1052 * It is safe to allow direct access to MSR_GSBASE and MSR_FSBASE.
1053 * The guest FSBASE and GSBASE are saved and restored during
1054 * vm-exit and vm-entry respectively. The host FSBASE and GSBASE are
1055 * always restored from the vmcs host state area on vm-exit.
1057 * The SYSENTER_CS/ESP/EIP MSRs are identical to FS/GSBASE in
1058 * how they are saved/restored so can be directly accessed by the
1061 * MSR_EFER is saved and restored in the guest VMCS area on a
1062 * VM exit and entry respectively. It is also restored from the
1063 * host VMCS area on a VM exit.
1065 * The TSC MSR is exposed read-only. Writes are disallowed as
1066 * that will impact the host TSC. If the guest does a write
1067 * the "use TSC offsetting" execution control is enabled and the
1068 * difference between the host TSC and the guest TSC is written
1069 * into the TSC offset in the VMCS.
1071 * Guest TSC_AUX support is enabled if any of guest RDPID and/or
1072 * guest RDTSCP support are enabled (since, as per Table 2-2 in SDM
1073 * volume 4, TSC_AUX is supported if any of RDPID and/or RDTSCP are
1074 * supported). If guest TSC_AUX support is enabled, TSC_AUX is
1075 * exposed read-only so that the VMM can do one fewer MSR read per
1076 * exit than if this register were exposed read-write; the guest
1077 * restore value can be updated during guest writes (expected to be
1078 * rare) instead of during all exits (common).
1080 if (guest_msr_rw(vmx, MSR_GSBASE) ||
1081 guest_msr_rw(vmx, MSR_FSBASE) ||
1082 guest_msr_rw(vmx, MSR_SYSENTER_CS_MSR) ||
1083 guest_msr_rw(vmx, MSR_SYSENTER_ESP_MSR) ||
1084 guest_msr_rw(vmx, MSR_SYSENTER_EIP_MSR) ||
1085 guest_msr_rw(vmx, MSR_EFER) ||
1086 guest_msr_ro(vmx, MSR_TSC) ||
1087 ((cap_rdpid || cap_rdtscp) && guest_msr_ro(vmx, MSR_TSC_AUX)))
1088 panic("vmx_init: error setting guest msr access");
1090 vpid_alloc(vpid, VM_MAXCPU);
1092 if (virtual_interrupt_delivery) {
1093 error = vm_map_mmio(vm, DEFAULT_APIC_BASE, PAGE_SIZE,
1094 APIC_ACCESS_ADDRESS);
1095 /* XXX this should really return an error to the caller */
1096 KASSERT(error == 0, ("vm_map_mmio(apicbase) error %d", error));
1099 maxcpus = vm_get_maxcpus(vm);
1100 for (i = 0; i < maxcpus; i++) {
1101 vmcs = &vmx->vmcs[i];
1102 vmcs->identifier = vmx_revision();
1103 error = vmclear(vmcs);
1105 panic("vmx_init: vmclear error %d on vcpu %d\n",
1109 vmx_msr_guest_init(vmx, i);
1111 error = vmcs_init(vmcs);
1112 KASSERT(error == 0, ("vmcs_init error %d", error));
1116 error += vmwrite(VMCS_HOST_RSP, (u_long)&vmx->ctx[i]);
1117 error += vmwrite(VMCS_EPTP, vmx->eptp);
1118 error += vmwrite(VMCS_PIN_BASED_CTLS, pinbased_ctls);
1119 error += vmwrite(VMCS_PRI_PROC_BASED_CTLS, procbased_ctls);
1120 error += vmwrite(VMCS_SEC_PROC_BASED_CTLS, procbased_ctls2);
1121 error += vmwrite(VMCS_EXIT_CTLS, exit_ctls);
1122 error += vmwrite(VMCS_ENTRY_CTLS, entry_ctls);
1123 error += vmwrite(VMCS_MSR_BITMAP, vtophys(vmx->msr_bitmap));
1124 error += vmwrite(VMCS_VPID, vpid[i]);
1126 if (guest_l1d_flush && !guest_l1d_flush_sw) {
1127 vmcs_write(VMCS_ENTRY_MSR_LOAD, pmap_kextract(
1128 (vm_offset_t)&msr_load_list[0]));
1129 vmcs_write(VMCS_ENTRY_MSR_LOAD_COUNT,
1130 nitems(msr_load_list));
1131 vmcs_write(VMCS_EXIT_MSR_STORE, 0);
1132 vmcs_write(VMCS_EXIT_MSR_STORE_COUNT, 0);
1135 /* exception bitmap */
1136 if (vcpu_trace_exceptions(vm, i))
1137 exc_bitmap = 0xffffffff;
1139 exc_bitmap = 1 << IDT_MC;
1140 error += vmwrite(VMCS_EXCEPTION_BITMAP, exc_bitmap);
1142 vmx->ctx[i].guest_dr6 = DBREG_DR6_RESERVED1;
1143 error += vmwrite(VMCS_GUEST_DR7, DBREG_DR7_RESERVED1);
1145 if (tpr_shadowing) {
1146 error += vmwrite(VMCS_VIRTUAL_APIC,
1147 vtophys(&vmx->apic_page[i]));
1150 if (virtual_interrupt_delivery) {
1151 error += vmwrite(VMCS_APIC_ACCESS, APIC_ACCESS_ADDRESS);
1152 error += vmwrite(VMCS_EOI_EXIT0, 0);
1153 error += vmwrite(VMCS_EOI_EXIT1, 0);
1154 error += vmwrite(VMCS_EOI_EXIT2, 0);
1155 error += vmwrite(VMCS_EOI_EXIT3, 0);
1157 if (posted_interrupts) {
1158 error += vmwrite(VMCS_PIR_VECTOR, pirvec);
1159 error += vmwrite(VMCS_PIR_DESC,
1160 vtophys(&vmx->pir_desc[i]));
1163 KASSERT(error == 0, ("vmx_init: error customizing the vmcs"));
1165 vmx->cap[i].set = 0;
1166 vmx->cap[i].set |= cap_rdpid != 0 ? 1 << VM_CAP_RDPID : 0;
1167 vmx->cap[i].set |= cap_rdtscp != 0 ? 1 << VM_CAP_RDTSCP : 0;
1168 vmx->cap[i].proc_ctls = procbased_ctls;
1169 vmx->cap[i].proc_ctls2 = procbased_ctls2;
1170 vmx->cap[i].exc_bitmap = exc_bitmap;
1172 vmx->state[i].nextrip = ~0;
1173 vmx->state[i].lastcpu = NOCPU;
1174 vmx->state[i].vpid = vpid[i];
1177 * Set up the CR0/4 shadows, and init the read shadow
1178 * to the power-on register value from the Intel Sys Arch.
1182 error = vmx_setup_cr0_shadow(vmcs, 0x60000010);
1184 panic("vmx_setup_cr0_shadow %d", error);
1186 error = vmx_setup_cr4_shadow(vmcs, 0);
1188 panic("vmx_setup_cr4_shadow %d", error);
1190 vmx->ctx[i].pmap = pmap;
1197 vmx_handle_cpuid(struct vm *vm, int vcpu, struct vmxctx *vmxctx)
1201 handled = x86_emulate_cpuid(vm, vcpu, (uint64_t *)&vmxctx->guest_rax,
1202 (uint64_t *)&vmxctx->guest_rbx, (uint64_t *)&vmxctx->guest_rcx,
1203 (uint64_t *)&vmxctx->guest_rdx);
1207 static __inline void
1208 vmx_run_trace(struct vmx *vmx, int vcpu)
1211 VCPU_CTR1(vmx->vm, vcpu, "Resume execution at %#lx", vmcs_guest_rip());
1215 static __inline void
1216 vmx_exit_trace(struct vmx *vmx, int vcpu, uint64_t rip, uint32_t exit_reason,
1220 VCPU_CTR3(vmx->vm, vcpu, "%s %s vmexit at 0x%0lx",
1221 handled ? "handled" : "unhandled",
1222 exit_reason_to_str(exit_reason), rip);
1226 static __inline void
1227 vmx_astpending_trace(struct vmx *vmx, int vcpu, uint64_t rip)
1230 VCPU_CTR1(vmx->vm, vcpu, "astpending vmexit at 0x%0lx", rip);
1234 static VMM_STAT_INTEL(VCPU_INVVPID_SAVED, "Number of vpid invalidations saved");
1235 static VMM_STAT_INTEL(VCPU_INVVPID_DONE, "Number of vpid invalidations done");
1238 * Invalidate guest mappings identified by its vpid from the TLB.
1240 static __inline void
1241 vmx_invvpid(struct vmx *vmx, int vcpu, pmap_t pmap, int running)
1243 struct vmxstate *vmxstate;
1244 struct invvpid_desc invvpid_desc;
1246 vmxstate = &vmx->state[vcpu];
1247 if (vmxstate->vpid == 0)
1252 * Set the 'lastcpu' to an invalid host cpu.
1254 * This will invalidate TLB entries tagged with the vcpu's
1255 * vpid the next time it runs via vmx_set_pcpu_defaults().
1257 vmxstate->lastcpu = NOCPU;
1261 KASSERT(curthread->td_critnest > 0, ("%s: vcpu %d running outside "
1262 "critical section", __func__, vcpu));
1265 * Invalidate all mappings tagged with 'vpid'
1267 * We do this because this vcpu was executing on a different host
1268 * cpu when it last ran. We do not track whether it invalidated
1269 * mappings associated with its 'vpid' during that run. So we must
1270 * assume that the mappings associated with 'vpid' on 'curcpu' are
1271 * stale and invalidate them.
1273 * Note that we incur this penalty only when the scheduler chooses to
1274 * move the thread associated with this vcpu between host cpus.
1276 * Note also that this will invalidate mappings tagged with 'vpid'
1279 if (atomic_load_long(&pmap->pm_eptgen) == vmx->eptgen[curcpu]) {
1280 invvpid_desc._res1 = 0;
1281 invvpid_desc._res2 = 0;
1282 invvpid_desc.vpid = vmxstate->vpid;
1283 invvpid_desc.linear_addr = 0;
1284 invvpid(INVVPID_TYPE_SINGLE_CONTEXT, invvpid_desc);
1285 vmm_stat_incr(vmx->vm, vcpu, VCPU_INVVPID_DONE, 1);
1288 * The invvpid can be skipped if an invept is going to
1289 * be performed before entering the guest. The invept
1290 * will invalidate combined mappings tagged with
1291 * 'vmx->eptp' for all vpids.
1293 vmm_stat_incr(vmx->vm, vcpu, VCPU_INVVPID_SAVED, 1);
1298 vmx_set_pcpu_defaults(struct vmx *vmx, int vcpu, pmap_t pmap)
1300 struct vmxstate *vmxstate;
1302 vmxstate = &vmx->state[vcpu];
1303 if (vmxstate->lastcpu == curcpu)
1306 vmxstate->lastcpu = curcpu;
1308 vmm_stat_incr(vmx->vm, vcpu, VCPU_MIGRATIONS, 1);
1310 vmcs_write(VMCS_HOST_TR_BASE, vmm_get_host_trbase());
1311 vmcs_write(VMCS_HOST_GDTR_BASE, vmm_get_host_gdtrbase());
1312 vmcs_write(VMCS_HOST_GS_BASE, vmm_get_host_gsbase());
1313 vmx_invvpid(vmx, vcpu, pmap, 1);
1317 * We depend on 'procbased_ctls' to have the Interrupt Window Exiting bit set.
1319 CTASSERT((PROCBASED_CTLS_ONE_SETTING & PROCBASED_INT_WINDOW_EXITING) != 0);
1321 static void __inline
1322 vmx_set_int_window_exiting(struct vmx *vmx, int vcpu)
1325 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) == 0) {
1326 vmx->cap[vcpu].proc_ctls |= PROCBASED_INT_WINDOW_EXITING;
1327 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1328 VCPU_CTR0(vmx->vm, vcpu, "Enabling interrupt window exiting");
1332 static void __inline
1333 vmx_clear_int_window_exiting(struct vmx *vmx, int vcpu)
1336 KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0,
1337 ("intr_window_exiting not set: %#x", vmx->cap[vcpu].proc_ctls));
1338 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_INT_WINDOW_EXITING;
1339 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1340 VCPU_CTR0(vmx->vm, vcpu, "Disabling interrupt window exiting");
1343 static void __inline
1344 vmx_set_nmi_window_exiting(struct vmx *vmx, int vcpu)
1347 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) == 0) {
1348 vmx->cap[vcpu].proc_ctls |= PROCBASED_NMI_WINDOW_EXITING;
1349 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1350 VCPU_CTR0(vmx->vm, vcpu, "Enabling NMI window exiting");
1354 static void __inline
1355 vmx_clear_nmi_window_exiting(struct vmx *vmx, int vcpu)
1358 KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) != 0,
1359 ("nmi_window_exiting not set %#x", vmx->cap[vcpu].proc_ctls));
1360 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_NMI_WINDOW_EXITING;
1361 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1362 VCPU_CTR0(vmx->vm, vcpu, "Disabling NMI window exiting");
1366 vmx_set_tsc_offset(struct vmx *vmx, int vcpu, uint64_t offset)
1370 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_TSC_OFFSET) == 0) {
1371 vmx->cap[vcpu].proc_ctls |= PROCBASED_TSC_OFFSET;
1372 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1373 VCPU_CTR0(vmx->vm, vcpu, "Enabling TSC offsetting");
1376 error = vmwrite(VMCS_TSC_OFFSET, offset);
1377 #ifdef BHYVE_SNAPSHOT
1379 error = vm_set_tsc_offset(vmx->vm, vcpu, offset);
1384 #define NMI_BLOCKING (VMCS_INTERRUPTIBILITY_NMI_BLOCKING | \
1385 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)
1386 #define HWINTR_BLOCKING (VMCS_INTERRUPTIBILITY_STI_BLOCKING | \
1387 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)
1390 vmx_inject_nmi(struct vmx *vmx, int vcpu)
1394 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1395 KASSERT((gi & NMI_BLOCKING) == 0, ("vmx_inject_nmi: invalid guest "
1396 "interruptibility-state %#x", gi));
1398 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1399 KASSERT((info & VMCS_INTR_VALID) == 0, ("vmx_inject_nmi: invalid "
1400 "VM-entry interruption information %#x", info));
1403 * Inject the virtual NMI. The vector must be the NMI IDT entry
1404 * or the VMCS entry check will fail.
1406 info = IDT_NMI | VMCS_INTR_T_NMI | VMCS_INTR_VALID;
1407 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1409 VCPU_CTR0(vmx->vm, vcpu, "Injecting vNMI");
1411 /* Clear the request */
1412 vm_nmi_clear(vmx->vm, vcpu);
1416 vmx_inject_interrupts(struct vmx *vmx, int vcpu, struct vlapic *vlapic,
1419 int vector, need_nmi_exiting, extint_pending;
1420 uint64_t rflags, entryinfo;
1423 if (vmx->state[vcpu].nextrip != guestrip) {
1424 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1425 if (gi & HWINTR_BLOCKING) {
1426 VCPU_CTR2(vmx->vm, vcpu, "Guest interrupt blocking "
1427 "cleared due to rip change: %#lx/%#lx",
1428 vmx->state[vcpu].nextrip, guestrip);
1429 gi &= ~HWINTR_BLOCKING;
1430 vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1434 if (vm_entry_intinfo(vmx->vm, vcpu, &entryinfo)) {
1435 KASSERT((entryinfo & VMCS_INTR_VALID) != 0, ("%s: entry "
1436 "intinfo is not valid: %#lx", __func__, entryinfo));
1438 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1439 KASSERT((info & VMCS_INTR_VALID) == 0, ("%s: cannot inject "
1440 "pending exception: %#lx/%#x", __func__, entryinfo, info));
1443 vector = info & 0xff;
1444 if (vector == IDT_BP || vector == IDT_OF) {
1446 * VT-x requires #BP and #OF to be injected as software
1449 info &= ~VMCS_INTR_T_MASK;
1450 info |= VMCS_INTR_T_SWEXCEPTION;
1453 if (info & VMCS_INTR_DEL_ERRCODE)
1454 vmcs_write(VMCS_ENTRY_EXCEPTION_ERROR, entryinfo >> 32);
1456 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1459 if (vm_nmi_pending(vmx->vm, vcpu)) {
1461 * If there are no conditions blocking NMI injection then
1462 * inject it directly here otherwise enable "NMI window
1463 * exiting" to inject it as soon as we can.
1465 * We also check for STI_BLOCKING because some implementations
1466 * don't allow NMI injection in this case. If we are running
1467 * on a processor that doesn't have this restriction it will
1468 * immediately exit and the NMI will be injected in the
1469 * "NMI window exiting" handler.
1471 need_nmi_exiting = 1;
1472 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1473 if ((gi & (HWINTR_BLOCKING | NMI_BLOCKING)) == 0) {
1474 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1475 if ((info & VMCS_INTR_VALID) == 0) {
1476 vmx_inject_nmi(vmx, vcpu);
1477 need_nmi_exiting = 0;
1479 VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI "
1480 "due to VM-entry intr info %#x", info);
1483 VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI due to "
1484 "Guest Interruptibility-state %#x", gi);
1487 if (need_nmi_exiting)
1488 vmx_set_nmi_window_exiting(vmx, vcpu);
1491 extint_pending = vm_extint_pending(vmx->vm, vcpu);
1493 if (!extint_pending && virtual_interrupt_delivery) {
1494 vmx_inject_pir(vlapic);
1499 * If interrupt-window exiting is already in effect then don't bother
1500 * checking for pending interrupts. This is just an optimization and
1501 * not needed for correctness.
1503 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0) {
1504 VCPU_CTR0(vmx->vm, vcpu, "Skip interrupt injection due to "
1505 "pending int_window_exiting");
1509 if (!extint_pending) {
1510 /* Ask the local apic for a vector to inject */
1511 if (!vlapic_pending_intr(vlapic, &vector))
1515 * From the Intel SDM, Volume 3, Section "Maskable
1516 * Hardware Interrupts":
1517 * - maskable interrupt vectors [16,255] can be delivered
1518 * through the local APIC.
1520 KASSERT(vector >= 16 && vector <= 255,
1521 ("invalid vector %d from local APIC", vector));
1523 /* Ask the legacy pic for a vector to inject */
1524 vatpic_pending_intr(vmx->vm, &vector);
1527 * From the Intel SDM, Volume 3, Section "Maskable
1528 * Hardware Interrupts":
1529 * - maskable interrupt vectors [0,255] can be delivered
1530 * through the INTR pin.
1532 KASSERT(vector >= 0 && vector <= 255,
1533 ("invalid vector %d from INTR", vector));
1536 /* Check RFLAGS.IF and the interruptibility state of the guest */
1537 rflags = vmcs_read(VMCS_GUEST_RFLAGS);
1538 if ((rflags & PSL_I) == 0) {
1539 VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1540 "rflags %#lx", vector, rflags);
1544 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1545 if (gi & HWINTR_BLOCKING) {
1546 VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1547 "Guest Interruptibility-state %#x", vector, gi);
1551 info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1552 if (info & VMCS_INTR_VALID) {
1554 * This is expected and could happen for multiple reasons:
1555 * - A vectoring VM-entry was aborted due to astpending
1556 * - A VM-exit happened during event injection.
1557 * - An exception was injected above.
1558 * - An NMI was injected above or after "NMI window exiting"
1560 VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1561 "VM-entry intr info %#x", vector, info);
1565 /* Inject the interrupt */
1566 info = VMCS_INTR_T_HWINTR | VMCS_INTR_VALID;
1568 vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1570 if (!extint_pending) {
1571 /* Update the Local APIC ISR */
1572 vlapic_intr_accepted(vlapic, vector);
1574 vm_extint_clear(vmx->vm, vcpu);
1575 vatpic_intr_accepted(vmx->vm, vector);
1578 * After we accepted the current ExtINT the PIC may
1579 * have posted another one. If that is the case, set
1580 * the Interrupt Window Exiting execution control so
1581 * we can inject that one too.
1583 * Also, interrupt window exiting allows us to inject any
1584 * pending APIC vector that was preempted by the ExtINT
1585 * as soon as possible. This applies both for the software
1586 * emulated vlapic and the hardware assisted virtual APIC.
1588 vmx_set_int_window_exiting(vmx, vcpu);
1591 VCPU_CTR1(vmx->vm, vcpu, "Injecting hwintr at vector %d", vector);
1597 * Set the Interrupt Window Exiting execution control so we can inject
1598 * the interrupt as soon as blocking condition goes away.
1600 vmx_set_int_window_exiting(vmx, vcpu);
1604 * If the Virtual NMIs execution control is '1' then the logical processor
1605 * tracks virtual-NMI blocking in the Guest Interruptibility-state field of
1606 * the VMCS. An IRET instruction in VMX non-root operation will remove any
1607 * virtual-NMI blocking.
1609 * This unblocking occurs even if the IRET causes a fault. In this case the
1610 * hypervisor needs to restore virtual-NMI blocking before resuming the guest.
1613 vmx_restore_nmi_blocking(struct vmx *vmx, int vcpuid)
1617 VCPU_CTR0(vmx->vm, vcpuid, "Restore Virtual-NMI blocking");
1618 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1619 gi |= VMCS_INTERRUPTIBILITY_NMI_BLOCKING;
1620 vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1624 vmx_clear_nmi_blocking(struct vmx *vmx, int vcpuid)
1628 VCPU_CTR0(vmx->vm, vcpuid, "Clear Virtual-NMI blocking");
1629 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1630 gi &= ~VMCS_INTERRUPTIBILITY_NMI_BLOCKING;
1631 vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1635 vmx_assert_nmi_blocking(struct vmx *vmx, int vcpuid)
1639 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1640 KASSERT(gi & VMCS_INTERRUPTIBILITY_NMI_BLOCKING,
1641 ("NMI blocking is not in effect %#x", gi));
1645 vmx_emulate_xsetbv(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
1647 struct vmxctx *vmxctx;
1649 const struct xsave_limits *limits;
1651 vmxctx = &vmx->ctx[vcpu];
1652 limits = vmm_get_xsave_limits();
1655 * Note that the processor raises a GP# fault on its own if
1656 * xsetbv is executed for CPL != 0, so we do not have to
1657 * emulate that fault here.
1660 /* Only xcr0 is supported. */
1661 if (vmxctx->guest_rcx != 0) {
1662 vm_inject_gp(vmx->vm, vcpu);
1666 /* We only handle xcr0 if both the host and guest have XSAVE enabled. */
1667 if (!limits->xsave_enabled || !(vmcs_read(VMCS_GUEST_CR4) & CR4_XSAVE)) {
1668 vm_inject_ud(vmx->vm, vcpu);
1672 xcrval = vmxctx->guest_rdx << 32 | (vmxctx->guest_rax & 0xffffffff);
1673 if ((xcrval & ~limits->xcr0_allowed) != 0) {
1674 vm_inject_gp(vmx->vm, vcpu);
1678 if (!(xcrval & XFEATURE_ENABLED_X87)) {
1679 vm_inject_gp(vmx->vm, vcpu);
1683 /* AVX (YMM_Hi128) requires SSE. */
1684 if (xcrval & XFEATURE_ENABLED_AVX &&
1685 (xcrval & XFEATURE_AVX) != XFEATURE_AVX) {
1686 vm_inject_gp(vmx->vm, vcpu);
1691 * AVX512 requires base AVX (YMM_Hi128) as well as OpMask,
1692 * ZMM_Hi256, and Hi16_ZMM.
1694 if (xcrval & XFEATURE_AVX512 &&
1695 (xcrval & (XFEATURE_AVX512 | XFEATURE_AVX)) !=
1696 (XFEATURE_AVX512 | XFEATURE_AVX)) {
1697 vm_inject_gp(vmx->vm, vcpu);
1702 * Intel MPX requires both bound register state flags to be
1705 if (((xcrval & XFEATURE_ENABLED_BNDREGS) != 0) !=
1706 ((xcrval & XFEATURE_ENABLED_BNDCSR) != 0)) {
1707 vm_inject_gp(vmx->vm, vcpu);
1712 * This runs "inside" vmrun() with the guest's FPU state, so
1713 * modifying xcr0 directly modifies the guest's xcr0, not the
1716 load_xcr(0, xcrval);
1721 vmx_get_guest_reg(struct vmx *vmx, int vcpu, int ident)
1723 const struct vmxctx *vmxctx;
1725 vmxctx = &vmx->ctx[vcpu];
1729 return (vmxctx->guest_rax);
1731 return (vmxctx->guest_rcx);
1733 return (vmxctx->guest_rdx);
1735 return (vmxctx->guest_rbx);
1737 return (vmcs_read(VMCS_GUEST_RSP));
1739 return (vmxctx->guest_rbp);
1741 return (vmxctx->guest_rsi);
1743 return (vmxctx->guest_rdi);
1745 return (vmxctx->guest_r8);
1747 return (vmxctx->guest_r9);
1749 return (vmxctx->guest_r10);
1751 return (vmxctx->guest_r11);
1753 return (vmxctx->guest_r12);
1755 return (vmxctx->guest_r13);
1757 return (vmxctx->guest_r14);
1759 return (vmxctx->guest_r15);
1761 panic("invalid vmx register %d", ident);
1766 vmx_set_guest_reg(struct vmx *vmx, int vcpu, int ident, uint64_t regval)
1768 struct vmxctx *vmxctx;
1770 vmxctx = &vmx->ctx[vcpu];
1774 vmxctx->guest_rax = regval;
1777 vmxctx->guest_rcx = regval;
1780 vmxctx->guest_rdx = regval;
1783 vmxctx->guest_rbx = regval;
1786 vmcs_write(VMCS_GUEST_RSP, regval);
1789 vmxctx->guest_rbp = regval;
1792 vmxctx->guest_rsi = regval;
1795 vmxctx->guest_rdi = regval;
1798 vmxctx->guest_r8 = regval;
1801 vmxctx->guest_r9 = regval;
1804 vmxctx->guest_r10 = regval;
1807 vmxctx->guest_r11 = regval;
1810 vmxctx->guest_r12 = regval;
1813 vmxctx->guest_r13 = regval;
1816 vmxctx->guest_r14 = regval;
1819 vmxctx->guest_r15 = regval;
1822 panic("invalid vmx register %d", ident);
1827 vmx_emulate_cr0_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1829 uint64_t crval, regval;
1831 /* We only handle mov to %cr0 at this time */
1832 if ((exitqual & 0xf0) != 0x00)
1835 regval = vmx_get_guest_reg(vmx, vcpu, (exitqual >> 8) & 0xf);
1837 vmcs_write(VMCS_CR0_SHADOW, regval);
1839 crval = regval | cr0_ones_mask;
1840 crval &= ~cr0_zeros_mask;
1841 vmcs_write(VMCS_GUEST_CR0, crval);
1843 if (regval & CR0_PG) {
1844 uint64_t efer, entry_ctls;
1847 * If CR0.PG is 1 and EFER.LME is 1 then EFER.LMA and
1848 * the "IA-32e mode guest" bit in VM-entry control must be
1851 efer = vmcs_read(VMCS_GUEST_IA32_EFER);
1852 if (efer & EFER_LME) {
1854 vmcs_write(VMCS_GUEST_IA32_EFER, efer);
1855 entry_ctls = vmcs_read(VMCS_ENTRY_CTLS);
1856 entry_ctls |= VM_ENTRY_GUEST_LMA;
1857 vmcs_write(VMCS_ENTRY_CTLS, entry_ctls);
1865 vmx_emulate_cr4_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1867 uint64_t crval, regval;
1869 /* We only handle mov to %cr4 at this time */
1870 if ((exitqual & 0xf0) != 0x00)
1873 regval = vmx_get_guest_reg(vmx, vcpu, (exitqual >> 8) & 0xf);
1875 vmcs_write(VMCS_CR4_SHADOW, regval);
1877 crval = regval | cr4_ones_mask;
1878 crval &= ~cr4_zeros_mask;
1879 vmcs_write(VMCS_GUEST_CR4, crval);
1885 vmx_emulate_cr8_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1887 struct vlapic *vlapic;
1891 /* We only handle mov %cr8 to/from a register at this time. */
1892 if ((exitqual & 0xe0) != 0x00) {
1896 vlapic = vm_lapic(vmx->vm, vcpu);
1897 regnum = (exitqual >> 8) & 0xf;
1898 if (exitqual & 0x10) {
1899 cr8 = vlapic_get_cr8(vlapic);
1900 vmx_set_guest_reg(vmx, vcpu, regnum, cr8);
1902 cr8 = vmx_get_guest_reg(vmx, vcpu, regnum);
1903 vlapic_set_cr8(vlapic, cr8);
1910 * From section "Guest Register State" in the Intel SDM: CPL = SS.DPL
1917 ssar = vmcs_read(VMCS_GUEST_SS_ACCESS_RIGHTS);
1918 return ((ssar >> 5) & 0x3);
1921 static enum vm_cpu_mode
1926 if (vmcs_read(VMCS_GUEST_IA32_EFER) & EFER_LMA) {
1927 csar = vmcs_read(VMCS_GUEST_CS_ACCESS_RIGHTS);
1929 return (CPU_MODE_64BIT); /* CS.L = 1 */
1931 return (CPU_MODE_COMPATIBILITY);
1932 } else if (vmcs_read(VMCS_GUEST_CR0) & CR0_PE) {
1933 return (CPU_MODE_PROTECTED);
1935 return (CPU_MODE_REAL);
1939 static enum vm_paging_mode
1940 vmx_paging_mode(void)
1944 if (!(vmcs_read(VMCS_GUEST_CR0) & CR0_PG))
1945 return (PAGING_MODE_FLAT);
1946 cr4 = vmcs_read(VMCS_GUEST_CR4);
1947 if (!(cr4 & CR4_PAE))
1948 return (PAGING_MODE_32);
1949 if (vmcs_read(VMCS_GUEST_IA32_EFER) & EFER_LME) {
1950 if (!(cr4 & CR4_LA57))
1951 return (PAGING_MODE_64);
1952 return (PAGING_MODE_64_LA57);
1954 return (PAGING_MODE_PAE);
1958 inout_str_index(struct vmx *vmx, int vcpuid, int in)
1962 enum vm_reg_name reg;
1964 reg = in ? VM_REG_GUEST_RDI : VM_REG_GUEST_RSI;
1965 error = vmx_getreg(vmx, vcpuid, reg, &val);
1966 KASSERT(error == 0, ("%s: vmx_getreg error %d", __func__, error));
1971 inout_str_count(struct vmx *vmx, int vcpuid, int rep)
1977 error = vmx_getreg(vmx, vcpuid, VM_REG_GUEST_RCX, &val);
1978 KASSERT(!error, ("%s: vmx_getreg error %d", __func__, error));
1986 inout_str_addrsize(uint32_t inst_info)
1990 size = (inst_info >> 7) & 0x7;
1993 return (2); /* 16 bit */
1995 return (4); /* 32 bit */
1997 return (8); /* 64 bit */
1999 panic("%s: invalid size encoding %d", __func__, size);
2004 inout_str_seginfo(struct vmx *vmx, int vcpuid, uint32_t inst_info, int in,
2005 struct vm_inout_str *vis)
2010 vis->seg_name = VM_REG_GUEST_ES;
2012 s = (inst_info >> 15) & 0x7;
2013 vis->seg_name = vm_segment_name(s);
2016 error = vmx_getdesc(vmx, vcpuid, vis->seg_name, &vis->seg_desc);
2017 KASSERT(error == 0, ("%s: vmx_getdesc error %d", __func__, error));
2021 vmx_paging_info(struct vm_guest_paging *paging)
2023 paging->cr3 = vmcs_guest_cr3();
2024 paging->cpl = vmx_cpl();
2025 paging->cpu_mode = vmx_cpu_mode();
2026 paging->paging_mode = vmx_paging_mode();
2030 vmexit_inst_emul(struct vm_exit *vmexit, uint64_t gpa, uint64_t gla)
2032 struct vm_guest_paging *paging;
2035 paging = &vmexit->u.inst_emul.paging;
2037 vmexit->exitcode = VM_EXITCODE_INST_EMUL;
2038 vmexit->inst_length = 0;
2039 vmexit->u.inst_emul.gpa = gpa;
2040 vmexit->u.inst_emul.gla = gla;
2041 vmx_paging_info(paging);
2042 switch (paging->cpu_mode) {
2044 vmexit->u.inst_emul.cs_base = vmcs_read(VMCS_GUEST_CS_BASE);
2045 vmexit->u.inst_emul.cs_d = 0;
2047 case CPU_MODE_PROTECTED:
2048 case CPU_MODE_COMPATIBILITY:
2049 vmexit->u.inst_emul.cs_base = vmcs_read(VMCS_GUEST_CS_BASE);
2050 csar = vmcs_read(VMCS_GUEST_CS_ACCESS_RIGHTS);
2051 vmexit->u.inst_emul.cs_d = SEG_DESC_DEF32(csar);
2054 vmexit->u.inst_emul.cs_base = 0;
2055 vmexit->u.inst_emul.cs_d = 0;
2058 vie_init(&vmexit->u.inst_emul.vie, NULL, 0);
2062 ept_fault_type(uint64_t ept_qual)
2066 if (ept_qual & EPT_VIOLATION_DATA_WRITE)
2067 fault_type = VM_PROT_WRITE;
2068 else if (ept_qual & EPT_VIOLATION_INST_FETCH)
2069 fault_type = VM_PROT_EXECUTE;
2071 fault_type= VM_PROT_READ;
2073 return (fault_type);
2077 ept_emulation_fault(uint64_t ept_qual)
2081 /* EPT fault on an instruction fetch doesn't make sense here */
2082 if (ept_qual & EPT_VIOLATION_INST_FETCH)
2085 /* EPT fault must be a read fault or a write fault */
2086 read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0;
2087 write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0;
2088 if ((read | write) == 0)
2092 * The EPT violation must have been caused by accessing a
2093 * guest-physical address that is a translation of a guest-linear
2096 if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 ||
2097 (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) {
2105 apic_access_virtualization(struct vmx *vmx, int vcpuid)
2107 uint32_t proc_ctls2;
2109 proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
2110 return ((proc_ctls2 & PROCBASED2_VIRTUALIZE_APIC_ACCESSES) ? 1 : 0);
2114 x2apic_virtualization(struct vmx *vmx, int vcpuid)
2116 uint32_t proc_ctls2;
2118 proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
2119 return ((proc_ctls2 & PROCBASED2_VIRTUALIZE_X2APIC_MODE) ? 1 : 0);
2123 vmx_handle_apic_write(struct vmx *vmx, int vcpuid, struct vlapic *vlapic,
2126 int error, handled, offset;
2127 uint32_t *apic_regs, vector;
2131 offset = APIC_WRITE_OFFSET(qual);
2133 if (!apic_access_virtualization(vmx, vcpuid)) {
2135 * In general there should not be any APIC write VM-exits
2136 * unless APIC-access virtualization is enabled.
2138 * However self-IPI virtualization can legitimately trigger
2139 * an APIC-write VM-exit so treat it specially.
2141 if (x2apic_virtualization(vmx, vcpuid) &&
2142 offset == APIC_OFFSET_SELF_IPI) {
2143 apic_regs = (uint32_t *)(vlapic->apic_page);
2144 vector = apic_regs[APIC_OFFSET_SELF_IPI / 4];
2145 vlapic_self_ipi_handler(vlapic, vector);
2152 case APIC_OFFSET_ID:
2153 vlapic_id_write_handler(vlapic);
2155 case APIC_OFFSET_LDR:
2156 vlapic_ldr_write_handler(vlapic);
2158 case APIC_OFFSET_DFR:
2159 vlapic_dfr_write_handler(vlapic);
2161 case APIC_OFFSET_SVR:
2162 vlapic_svr_write_handler(vlapic);
2164 case APIC_OFFSET_ESR:
2165 vlapic_esr_write_handler(vlapic);
2167 case APIC_OFFSET_ICR_LOW:
2169 error = vlapic_icrlo_write_handler(vlapic, &retu);
2170 if (error != 0 || retu)
2171 handled = UNHANDLED;
2173 case APIC_OFFSET_CMCI_LVT:
2174 case APIC_OFFSET_TIMER_LVT ... APIC_OFFSET_ERROR_LVT:
2175 vlapic_lvt_write_handler(vlapic, offset);
2177 case APIC_OFFSET_TIMER_ICR:
2178 vlapic_icrtmr_write_handler(vlapic);
2180 case APIC_OFFSET_TIMER_DCR:
2181 vlapic_dcr_write_handler(vlapic);
2184 handled = UNHANDLED;
2191 apic_access_fault(struct vmx *vmx, int vcpuid, uint64_t gpa)
2194 if (apic_access_virtualization(vmx, vcpuid) &&
2195 (gpa >= DEFAULT_APIC_BASE && gpa < DEFAULT_APIC_BASE + PAGE_SIZE))
2202 vmx_handle_apic_access(struct vmx *vmx, int vcpuid, struct vm_exit *vmexit)
2205 int access_type, offset, allowed;
2207 if (!apic_access_virtualization(vmx, vcpuid))
2210 qual = vmexit->u.vmx.exit_qualification;
2211 access_type = APIC_ACCESS_TYPE(qual);
2212 offset = APIC_ACCESS_OFFSET(qual);
2215 if (access_type == 0) {
2217 * Read data access to the following registers is expected.
2220 case APIC_OFFSET_APR:
2221 case APIC_OFFSET_PPR:
2222 case APIC_OFFSET_RRR:
2223 case APIC_OFFSET_CMCI_LVT:
2224 case APIC_OFFSET_TIMER_CCR:
2230 } else if (access_type == 1) {
2232 * Write data access to the following registers is expected.
2235 case APIC_OFFSET_VER:
2236 case APIC_OFFSET_APR:
2237 case APIC_OFFSET_PPR:
2238 case APIC_OFFSET_RRR:
2239 case APIC_OFFSET_ISR0 ... APIC_OFFSET_ISR7:
2240 case APIC_OFFSET_TMR0 ... APIC_OFFSET_TMR7:
2241 case APIC_OFFSET_IRR0 ... APIC_OFFSET_IRR7:
2242 case APIC_OFFSET_CMCI_LVT:
2243 case APIC_OFFSET_TIMER_CCR:
2252 vmexit_inst_emul(vmexit, DEFAULT_APIC_BASE + offset,
2257 * Regardless of whether the APIC-access is allowed this handler
2258 * always returns UNHANDLED:
2259 * - if the access is allowed then it is handled by emulating the
2260 * instruction that caused the VM-exit (outside the critical section)
2261 * - if the access is not allowed then it will be converted to an
2262 * exitcode of VM_EXITCODE_VMX and will be dealt with in userland.
2267 static enum task_switch_reason
2268 vmx_task_switch_reason(uint64_t qual)
2272 reason = (qual >> 30) & 0x3;
2281 return (TSR_IDT_GATE);
2283 panic("%s: invalid reason %d", __func__, reason);
2288 emulate_wrmsr(struct vmx *vmx, int vcpuid, u_int num, uint64_t val, bool *retu)
2293 error = lapic_wrmsr(vmx->vm, vcpuid, num, val, retu);
2295 error = vmx_wrmsr(vmx, vcpuid, num, val, retu);
2301 emulate_rdmsr(struct vmx *vmx, int vcpuid, u_int num, bool *retu)
2303 struct vmxctx *vmxctx;
2309 error = lapic_rdmsr(vmx->vm, vcpuid, num, &result, retu);
2311 error = vmx_rdmsr(vmx, vcpuid, num, &result, retu);
2315 vmxctx = &vmx->ctx[vcpuid];
2316 error = vmxctx_setreg(vmxctx, VM_REG_GUEST_RAX, eax);
2317 KASSERT(error == 0, ("vmxctx_setreg(rax) error %d", error));
2320 error = vmxctx_setreg(vmxctx, VM_REG_GUEST_RDX, edx);
2321 KASSERT(error == 0, ("vmxctx_setreg(rdx) error %d", error));
2328 vmx_exit_process(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
2330 int error, errcode, errcode_valid, handled, in;
2331 struct vmxctx *vmxctx;
2332 struct vlapic *vlapic;
2333 struct vm_inout_str *vis;
2334 struct vm_task_switch *ts;
2335 uint32_t eax, ecx, edx, idtvec_info, idtvec_err, intr_info, inst_info;
2336 uint32_t intr_type, intr_vec, reason;
2337 uint64_t exitintinfo, qual, gpa;
2340 CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_VIRTUAL_NMI) != 0);
2341 CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_NMI_EXITING) != 0);
2343 handled = UNHANDLED;
2344 vmxctx = &vmx->ctx[vcpu];
2346 qual = vmexit->u.vmx.exit_qualification;
2347 reason = vmexit->u.vmx.exit_reason;
2348 vmexit->exitcode = VM_EXITCODE_BOGUS;
2350 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_COUNT, 1);
2351 SDT_PROBE3(vmm, vmx, exit, entry, vmx, vcpu, vmexit);
2354 * VM-entry failures during or after loading guest state.
2356 * These VM-exits are uncommon but must be handled specially
2357 * as most VM-exit fields are not populated as usual.
2359 if (__predict_false(reason == EXIT_REASON_MCE_DURING_ENTRY)) {
2360 VCPU_CTR0(vmx->vm, vcpu, "Handling MCE during VM-entry");
2361 __asm __volatile("int $18");
2366 * VM exits that can be triggered during event delivery need to
2367 * be handled specially by re-injecting the event if the IDT
2368 * vectoring information field's valid bit is set.
2370 * See "Information for VM Exits During Event Delivery" in Intel SDM
2373 idtvec_info = vmcs_idt_vectoring_info();
2374 if (idtvec_info & VMCS_IDT_VEC_VALID) {
2375 idtvec_info &= ~(1 << 12); /* clear undefined bit */
2376 exitintinfo = idtvec_info;
2377 if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) {
2378 idtvec_err = vmcs_idt_vectoring_err();
2379 exitintinfo |= (uint64_t)idtvec_err << 32;
2381 error = vm_exit_intinfo(vmx->vm, vcpu, exitintinfo);
2382 KASSERT(error == 0, ("%s: vm_set_intinfo error %d",
2386 * If 'virtual NMIs' are being used and the VM-exit
2387 * happened while injecting an NMI during the previous
2388 * VM-entry, then clear "blocking by NMI" in the
2389 * Guest Interruptibility-State so the NMI can be
2390 * reinjected on the subsequent VM-entry.
2392 * However, if the NMI was being delivered through a task
2393 * gate, then the new task must start execution with NMIs
2394 * blocked so don't clear NMI blocking in this case.
2396 intr_type = idtvec_info & VMCS_INTR_T_MASK;
2397 if (intr_type == VMCS_INTR_T_NMI) {
2398 if (reason != EXIT_REASON_TASK_SWITCH)
2399 vmx_clear_nmi_blocking(vmx, vcpu);
2401 vmx_assert_nmi_blocking(vmx, vcpu);
2405 * Update VM-entry instruction length if the event being
2406 * delivered was a software interrupt or software exception.
2408 if (intr_type == VMCS_INTR_T_SWINTR ||
2409 intr_type == VMCS_INTR_T_PRIV_SWEXCEPTION ||
2410 intr_type == VMCS_INTR_T_SWEXCEPTION) {
2411 vmcs_write(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length);
2416 case EXIT_REASON_TASK_SWITCH:
2417 ts = &vmexit->u.task_switch;
2418 ts->tsssel = qual & 0xffff;
2419 ts->reason = vmx_task_switch_reason(qual);
2421 ts->errcode_valid = 0;
2422 vmx_paging_info(&ts->paging);
2424 * If the task switch was due to a CALL, JMP, IRET, software
2425 * interrupt (INT n) or software exception (INT3, INTO),
2426 * then the saved %rip references the instruction that caused
2427 * the task switch. The instruction length field in the VMCS
2428 * is valid in this case.
2430 * In all other cases (e.g., NMI, hardware exception) the
2431 * saved %rip is one that would have been saved in the old TSS
2432 * had the task switch completed normally so the instruction
2433 * length field is not needed in this case and is explicitly
2436 if (ts->reason == TSR_IDT_GATE) {
2437 KASSERT(idtvec_info & VMCS_IDT_VEC_VALID,
2438 ("invalid idtvec_info %#x for IDT task switch",
2440 intr_type = idtvec_info & VMCS_INTR_T_MASK;
2441 if (intr_type != VMCS_INTR_T_SWINTR &&
2442 intr_type != VMCS_INTR_T_SWEXCEPTION &&
2443 intr_type != VMCS_INTR_T_PRIV_SWEXCEPTION) {
2444 /* Task switch triggered by external event */
2446 vmexit->inst_length = 0;
2447 if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) {
2448 ts->errcode_valid = 1;
2449 ts->errcode = vmcs_idt_vectoring_err();
2453 vmexit->exitcode = VM_EXITCODE_TASK_SWITCH;
2454 SDT_PROBE4(vmm, vmx, exit, taskswitch, vmx, vcpu, vmexit, ts);
2455 VCPU_CTR4(vmx->vm, vcpu, "task switch reason %d, tss 0x%04x, "
2456 "%s errcode 0x%016lx", ts->reason, ts->tsssel,
2457 ts->ext ? "external" : "internal",
2458 ((uint64_t)ts->errcode << 32) | ts->errcode_valid);
2460 case EXIT_REASON_CR_ACCESS:
2461 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CR_ACCESS, 1);
2462 SDT_PROBE4(vmm, vmx, exit, craccess, vmx, vcpu, vmexit, qual);
2463 switch (qual & 0xf) {
2465 handled = vmx_emulate_cr0_access(vmx, vcpu, qual);
2468 handled = vmx_emulate_cr4_access(vmx, vcpu, qual);
2471 handled = vmx_emulate_cr8_access(vmx, vcpu, qual);
2475 case EXIT_REASON_RDMSR:
2476 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_RDMSR, 1);
2478 ecx = vmxctx->guest_rcx;
2479 VCPU_CTR1(vmx->vm, vcpu, "rdmsr 0x%08x", ecx);
2480 SDT_PROBE4(vmm, vmx, exit, rdmsr, vmx, vcpu, vmexit, ecx);
2481 error = emulate_rdmsr(vmx, vcpu, ecx, &retu);
2483 vmexit->exitcode = VM_EXITCODE_RDMSR;
2484 vmexit->u.msr.code = ecx;
2488 /* Return to userspace with a valid exitcode */
2489 KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
2490 ("emulate_rdmsr retu with bogus exitcode"));
2493 case EXIT_REASON_WRMSR:
2494 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_WRMSR, 1);
2496 eax = vmxctx->guest_rax;
2497 ecx = vmxctx->guest_rcx;
2498 edx = vmxctx->guest_rdx;
2499 VCPU_CTR2(vmx->vm, vcpu, "wrmsr 0x%08x value 0x%016lx",
2500 ecx, (uint64_t)edx << 32 | eax);
2501 SDT_PROBE5(vmm, vmx, exit, wrmsr, vmx, vmexit, vcpu, ecx,
2502 (uint64_t)edx << 32 | eax);
2503 error = emulate_wrmsr(vmx, vcpu, ecx,
2504 (uint64_t)edx << 32 | eax, &retu);
2506 vmexit->exitcode = VM_EXITCODE_WRMSR;
2507 vmexit->u.msr.code = ecx;
2508 vmexit->u.msr.wval = (uint64_t)edx << 32 | eax;
2512 /* Return to userspace with a valid exitcode */
2513 KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
2514 ("emulate_wrmsr retu with bogus exitcode"));
2517 case EXIT_REASON_HLT:
2518 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_HLT, 1);
2519 SDT_PROBE3(vmm, vmx, exit, halt, vmx, vcpu, vmexit);
2520 vmexit->exitcode = VM_EXITCODE_HLT;
2521 vmexit->u.hlt.rflags = vmcs_read(VMCS_GUEST_RFLAGS);
2522 if (virtual_interrupt_delivery)
2523 vmexit->u.hlt.intr_status =
2524 vmcs_read(VMCS_GUEST_INTR_STATUS);
2526 vmexit->u.hlt.intr_status = 0;
2528 case EXIT_REASON_MTF:
2529 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_MTRAP, 1);
2530 SDT_PROBE3(vmm, vmx, exit, mtrap, vmx, vcpu, vmexit);
2531 vmexit->exitcode = VM_EXITCODE_MTRAP;
2532 vmexit->inst_length = 0;
2534 case EXIT_REASON_PAUSE:
2535 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_PAUSE, 1);
2536 SDT_PROBE3(vmm, vmx, exit, pause, vmx, vcpu, vmexit);
2537 vmexit->exitcode = VM_EXITCODE_PAUSE;
2539 case EXIT_REASON_INTR_WINDOW:
2540 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INTR_WINDOW, 1);
2541 SDT_PROBE3(vmm, vmx, exit, intrwindow, vmx, vcpu, vmexit);
2542 vmx_clear_int_window_exiting(vmx, vcpu);
2544 case EXIT_REASON_EXT_INTR:
2546 * External interrupts serve only to cause VM exits and allow
2547 * the host interrupt handler to run.
2549 * If this external interrupt triggers a virtual interrupt
2550 * to a VM, then that state will be recorded by the
2551 * host interrupt handler in the VM's softc. We will inject
2552 * this virtual interrupt during the subsequent VM enter.
2554 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
2555 SDT_PROBE4(vmm, vmx, exit, interrupt,
2556 vmx, vcpu, vmexit, intr_info);
2559 * XXX: Ignore this exit if VMCS_INTR_VALID is not set.
2560 * This appears to be a bug in VMware Fusion?
2562 if (!(intr_info & VMCS_INTR_VALID))
2564 KASSERT((intr_info & VMCS_INTR_VALID) != 0 &&
2565 (intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_HWINTR,
2566 ("VM exit interruption info invalid: %#x", intr_info));
2567 vmx_trigger_hostintr(intr_info & 0xff);
2570 * This is special. We want to treat this as an 'handled'
2571 * VM-exit but not increment the instruction pointer.
2573 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXTINT, 1);
2575 case EXIT_REASON_NMI_WINDOW:
2576 SDT_PROBE3(vmm, vmx, exit, nmiwindow, vmx, vcpu, vmexit);
2577 /* Exit to allow the pending virtual NMI to be injected */
2578 if (vm_nmi_pending(vmx->vm, vcpu))
2579 vmx_inject_nmi(vmx, vcpu);
2580 vmx_clear_nmi_window_exiting(vmx, vcpu);
2581 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NMI_WINDOW, 1);
2583 case EXIT_REASON_INOUT:
2584 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INOUT, 1);
2585 vmexit->exitcode = VM_EXITCODE_INOUT;
2586 vmexit->u.inout.bytes = (qual & 0x7) + 1;
2587 vmexit->u.inout.in = in = (qual & 0x8) ? 1 : 0;
2588 vmexit->u.inout.string = (qual & 0x10) ? 1 : 0;
2589 vmexit->u.inout.rep = (qual & 0x20) ? 1 : 0;
2590 vmexit->u.inout.port = (uint16_t)(qual >> 16);
2591 vmexit->u.inout.eax = (uint32_t)(vmxctx->guest_rax);
2592 if (vmexit->u.inout.string) {
2593 inst_info = vmcs_read(VMCS_EXIT_INSTRUCTION_INFO);
2594 vmexit->exitcode = VM_EXITCODE_INOUT_STR;
2595 vis = &vmexit->u.inout_str;
2596 vmx_paging_info(&vis->paging);
2597 vis->rflags = vmcs_read(VMCS_GUEST_RFLAGS);
2598 vis->cr0 = vmcs_read(VMCS_GUEST_CR0);
2599 vis->index = inout_str_index(vmx, vcpu, in);
2600 vis->count = inout_str_count(vmx, vcpu, vis->inout.rep);
2601 vis->addrsize = inout_str_addrsize(inst_info);
2602 inout_str_seginfo(vmx, vcpu, inst_info, in, vis);
2604 SDT_PROBE3(vmm, vmx, exit, inout, vmx, vcpu, vmexit);
2606 case EXIT_REASON_CPUID:
2607 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CPUID, 1);
2608 SDT_PROBE3(vmm, vmx, exit, cpuid, vmx, vcpu, vmexit);
2609 handled = vmx_handle_cpuid(vmx->vm, vcpu, vmxctx);
2611 case EXIT_REASON_EXCEPTION:
2612 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXCEPTION, 1);
2613 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
2614 KASSERT((intr_info & VMCS_INTR_VALID) != 0,
2615 ("VM exit interruption info invalid: %#x", intr_info));
2617 intr_vec = intr_info & 0xff;
2618 intr_type = intr_info & VMCS_INTR_T_MASK;
2621 * If Virtual NMIs control is 1 and the VM-exit is due to a
2622 * fault encountered during the execution of IRET then we must
2623 * restore the state of "virtual-NMI blocking" before resuming
2626 * See "Resuming Guest Software after Handling an Exception".
2627 * See "Information for VM Exits Due to Vectored Events".
2629 if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 &&
2630 (intr_vec != IDT_DF) &&
2631 (intr_info & EXIT_QUAL_NMIUDTI) != 0)
2632 vmx_restore_nmi_blocking(vmx, vcpu);
2635 * The NMI has already been handled in vmx_exit_handle_nmi().
2637 if (intr_type == VMCS_INTR_T_NMI)
2641 * Call the machine check handler by hand. Also don't reflect
2642 * the machine check back into the guest.
2644 if (intr_vec == IDT_MC) {
2645 VCPU_CTR0(vmx->vm, vcpu, "Vectoring to MCE handler");
2646 __asm __volatile("int $18");
2651 * If the hypervisor has requested user exits for
2652 * debug exceptions, bounce them out to userland.
2654 if (intr_type == VMCS_INTR_T_SWEXCEPTION && intr_vec == IDT_BP &&
2655 (vmx->cap[vcpu].set & (1 << VM_CAP_BPT_EXIT))) {
2656 vmexit->exitcode = VM_EXITCODE_BPT;
2657 vmexit->u.bpt.inst_length = vmexit->inst_length;
2658 vmexit->inst_length = 0;
2662 if (intr_vec == IDT_PF) {
2663 error = vmxctx_setreg(vmxctx, VM_REG_GUEST_CR2, qual);
2664 KASSERT(error == 0, ("%s: vmxctx_setreg(cr2) error %d",
2669 * Software exceptions exhibit trap-like behavior. This in
2670 * turn requires populating the VM-entry instruction length
2671 * so that the %rip in the trap frame is past the INT3/INTO
2674 if (intr_type == VMCS_INTR_T_SWEXCEPTION)
2675 vmcs_write(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length);
2677 /* Reflect all other exceptions back into the guest */
2678 errcode_valid = errcode = 0;
2679 if (intr_info & VMCS_INTR_DEL_ERRCODE) {
2681 errcode = vmcs_read(VMCS_EXIT_INTR_ERRCODE);
2683 VCPU_CTR2(vmx->vm, vcpu, "Reflecting exception %d/%#x into "
2684 "the guest", intr_vec, errcode);
2685 SDT_PROBE5(vmm, vmx, exit, exception,
2686 vmx, vcpu, vmexit, intr_vec, errcode);
2687 error = vm_inject_exception(vmx->vm, vcpu, intr_vec,
2688 errcode_valid, errcode, 0);
2689 KASSERT(error == 0, ("%s: vm_inject_exception error %d",
2693 case EXIT_REASON_EPT_FAULT:
2695 * If 'gpa' lies within the address space allocated to
2696 * memory then this must be a nested page fault otherwise
2697 * this must be an instruction that accesses MMIO space.
2700 if (vm_mem_allocated(vmx->vm, vcpu, gpa) ||
2701 apic_access_fault(vmx, vcpu, gpa)) {
2702 vmexit->exitcode = VM_EXITCODE_PAGING;
2703 vmexit->inst_length = 0;
2704 vmexit->u.paging.gpa = gpa;
2705 vmexit->u.paging.fault_type = ept_fault_type(qual);
2706 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NESTED_FAULT, 1);
2707 SDT_PROBE5(vmm, vmx, exit, nestedfault,
2708 vmx, vcpu, vmexit, gpa, qual);
2709 } else if (ept_emulation_fault(qual)) {
2710 vmexit_inst_emul(vmexit, gpa, vmcs_gla());
2711 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INST_EMUL, 1);
2712 SDT_PROBE4(vmm, vmx, exit, mmiofault,
2713 vmx, vcpu, vmexit, gpa);
2716 * If Virtual NMIs control is 1 and the VM-exit is due to an
2717 * EPT fault during the execution of IRET then we must restore
2718 * the state of "virtual-NMI blocking" before resuming.
2720 * See description of "NMI unblocking due to IRET" in
2721 * "Exit Qualification for EPT Violations".
2723 if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 &&
2724 (qual & EXIT_QUAL_NMIUDTI) != 0)
2725 vmx_restore_nmi_blocking(vmx, vcpu);
2727 case EXIT_REASON_VIRTUALIZED_EOI:
2728 vmexit->exitcode = VM_EXITCODE_IOAPIC_EOI;
2729 vmexit->u.ioapic_eoi.vector = qual & 0xFF;
2730 SDT_PROBE3(vmm, vmx, exit, eoi, vmx, vcpu, vmexit);
2731 vmexit->inst_length = 0; /* trap-like */
2733 case EXIT_REASON_APIC_ACCESS:
2734 SDT_PROBE3(vmm, vmx, exit, apicaccess, vmx, vcpu, vmexit);
2735 handled = vmx_handle_apic_access(vmx, vcpu, vmexit);
2737 case EXIT_REASON_APIC_WRITE:
2739 * APIC-write VM exit is trap-like so the %rip is already
2740 * pointing to the next instruction.
2742 vmexit->inst_length = 0;
2743 vlapic = vm_lapic(vmx->vm, vcpu);
2744 SDT_PROBE4(vmm, vmx, exit, apicwrite,
2745 vmx, vcpu, vmexit, vlapic);
2746 handled = vmx_handle_apic_write(vmx, vcpu, vlapic, qual);
2748 case EXIT_REASON_XSETBV:
2749 SDT_PROBE3(vmm, vmx, exit, xsetbv, vmx, vcpu, vmexit);
2750 handled = vmx_emulate_xsetbv(vmx, vcpu, vmexit);
2752 case EXIT_REASON_MONITOR:
2753 SDT_PROBE3(vmm, vmx, exit, monitor, vmx, vcpu, vmexit);
2754 vmexit->exitcode = VM_EXITCODE_MONITOR;
2756 case EXIT_REASON_MWAIT:
2757 SDT_PROBE3(vmm, vmx, exit, mwait, vmx, vcpu, vmexit);
2758 vmexit->exitcode = VM_EXITCODE_MWAIT;
2760 case EXIT_REASON_TPR:
2761 vlapic = vm_lapic(vmx->vm, vcpu);
2762 vlapic_sync_tpr(vlapic);
2763 vmexit->inst_length = 0;
2766 case EXIT_REASON_VMCALL:
2767 case EXIT_REASON_VMCLEAR:
2768 case EXIT_REASON_VMLAUNCH:
2769 case EXIT_REASON_VMPTRLD:
2770 case EXIT_REASON_VMPTRST:
2771 case EXIT_REASON_VMREAD:
2772 case EXIT_REASON_VMRESUME:
2773 case EXIT_REASON_VMWRITE:
2774 case EXIT_REASON_VMXOFF:
2775 case EXIT_REASON_VMXON:
2776 SDT_PROBE3(vmm, vmx, exit, vminsn, vmx, vcpu, vmexit);
2777 vmexit->exitcode = VM_EXITCODE_VMINSN;
2780 SDT_PROBE4(vmm, vmx, exit, unknown,
2781 vmx, vcpu, vmexit, reason);
2782 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_UNKNOWN, 1);
2788 * It is possible that control is returned to userland
2789 * even though we were able to handle the VM exit in the
2792 * In such a case we want to make sure that the userland
2793 * restarts guest execution at the instruction *after*
2794 * the one we just processed. Therefore we update the
2795 * guest rip in the VMCS and in 'vmexit'.
2797 vmexit->rip += vmexit->inst_length;
2798 vmexit->inst_length = 0;
2799 vmcs_write(VMCS_GUEST_RIP, vmexit->rip);
2801 if (vmexit->exitcode == VM_EXITCODE_BOGUS) {
2803 * If this VM exit was not claimed by anybody then
2804 * treat it as a generic VMX exit.
2806 vmexit->exitcode = VM_EXITCODE_VMX;
2807 vmexit->u.vmx.status = VM_SUCCESS;
2808 vmexit->u.vmx.inst_type = 0;
2809 vmexit->u.vmx.inst_error = 0;
2812 * The exitcode and collateral have been populated.
2813 * The VM exit will be processed further in userland.
2818 SDT_PROBE4(vmm, vmx, exit, return,
2819 vmx, vcpu, vmexit, handled);
2823 static __inline void
2824 vmx_exit_inst_error(struct vmxctx *vmxctx, int rc, struct vm_exit *vmexit)
2827 KASSERT(vmxctx->inst_fail_status != VM_SUCCESS,
2828 ("vmx_exit_inst_error: invalid inst_fail_status %d",
2829 vmxctx->inst_fail_status));
2831 vmexit->inst_length = 0;
2832 vmexit->exitcode = VM_EXITCODE_VMX;
2833 vmexit->u.vmx.status = vmxctx->inst_fail_status;
2834 vmexit->u.vmx.inst_error = vmcs_instruction_error();
2835 vmexit->u.vmx.exit_reason = ~0;
2836 vmexit->u.vmx.exit_qualification = ~0;
2839 case VMX_VMRESUME_ERROR:
2840 case VMX_VMLAUNCH_ERROR:
2841 vmexit->u.vmx.inst_type = rc;
2844 panic("vm_exit_inst_error: vmx_enter_guest returned %d", rc);
2849 * If the NMI-exiting VM execution control is set to '1' then an NMI in
2850 * non-root operation causes a VM-exit. NMI blocking is in effect so it is
2851 * sufficient to simply vector to the NMI handler via a software interrupt.
2852 * However, this must be done before maskable interrupts are enabled
2853 * otherwise the "iret" issued by an interrupt handler will incorrectly
2854 * clear NMI blocking.
2856 static __inline void
2857 vmx_exit_handle_nmi(struct vmx *vmx, int vcpuid, struct vm_exit *vmexit)
2861 KASSERT((read_rflags() & PSL_I) == 0, ("interrupts enabled"));
2863 if (vmexit->u.vmx.exit_reason != EXIT_REASON_EXCEPTION)
2866 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
2867 KASSERT((intr_info & VMCS_INTR_VALID) != 0,
2868 ("VM exit interruption info invalid: %#x", intr_info));
2870 if ((intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_NMI) {
2871 KASSERT((intr_info & 0xff) == IDT_NMI, ("VM exit due "
2872 "to NMI has invalid vector: %#x", intr_info));
2873 VCPU_CTR0(vmx->vm, vcpuid, "Vectoring to NMI handler");
2874 __asm __volatile("int $2");
2878 static __inline void
2879 vmx_dr_enter_guest(struct vmxctx *vmxctx)
2883 /* Save host control debug registers. */
2884 vmxctx->host_dr7 = rdr7();
2885 vmxctx->host_debugctl = rdmsr(MSR_DEBUGCTLMSR);
2888 * Disable debugging in DR7 and DEBUGCTL to avoid triggering
2889 * exceptions in the host based on the guest DRx values. The
2890 * guest DR7 and DEBUGCTL are saved/restored in the VMCS.
2893 wrmsr(MSR_DEBUGCTLMSR, 0);
2896 * Disable single stepping the kernel to avoid corrupting the
2897 * guest DR6. A debugger might still be able to corrupt the
2898 * guest DR6 by setting a breakpoint after this point and then
2901 rflags = read_rflags();
2902 vmxctx->host_tf = rflags & PSL_T;
2903 write_rflags(rflags & ~PSL_T);
2905 /* Save host debug registers. */
2906 vmxctx->host_dr0 = rdr0();
2907 vmxctx->host_dr1 = rdr1();
2908 vmxctx->host_dr2 = rdr2();
2909 vmxctx->host_dr3 = rdr3();
2910 vmxctx->host_dr6 = rdr6();
2912 /* Restore guest debug registers. */
2913 load_dr0(vmxctx->guest_dr0);
2914 load_dr1(vmxctx->guest_dr1);
2915 load_dr2(vmxctx->guest_dr2);
2916 load_dr3(vmxctx->guest_dr3);
2917 load_dr6(vmxctx->guest_dr6);
2920 static __inline void
2921 vmx_dr_leave_guest(struct vmxctx *vmxctx)
2924 /* Save guest debug registers. */
2925 vmxctx->guest_dr0 = rdr0();
2926 vmxctx->guest_dr1 = rdr1();
2927 vmxctx->guest_dr2 = rdr2();
2928 vmxctx->guest_dr3 = rdr3();
2929 vmxctx->guest_dr6 = rdr6();
2932 * Restore host debug registers. Restore DR7, DEBUGCTL, and
2935 load_dr0(vmxctx->host_dr0);
2936 load_dr1(vmxctx->host_dr1);
2937 load_dr2(vmxctx->host_dr2);
2938 load_dr3(vmxctx->host_dr3);
2939 load_dr6(vmxctx->host_dr6);
2940 wrmsr(MSR_DEBUGCTLMSR, vmxctx->host_debugctl);
2941 load_dr7(vmxctx->host_dr7);
2942 write_rflags(read_rflags() | vmxctx->host_tf);
2945 static __inline void
2946 vmx_pmap_activate(struct vmx *vmx, pmap_t pmap)
2953 CPU_SET_ATOMIC(cpu, &pmap->pm_active);
2954 smr_enter(pmap->pm_eptsmr);
2955 eptgen = atomic_load_long(&pmap->pm_eptgen);
2956 if (eptgen != vmx->eptgen[cpu]) {
2957 vmx->eptgen[cpu] = eptgen;
2958 invept(INVEPT_TYPE_SINGLE_CONTEXT,
2959 (struct invept_desc){ .eptp = vmx->eptp, ._res = 0 });
2963 static __inline void
2964 vmx_pmap_deactivate(struct vmx *vmx, pmap_t pmap)
2966 smr_exit(pmap->pm_eptsmr);
2967 CPU_CLR_ATOMIC(curcpu, &pmap->pm_active);
2971 vmx_run(void *arg, int vcpu, register_t rip, pmap_t pmap,
2972 struct vm_eventinfo *evinfo)
2974 int rc, handled, launched;
2977 struct vmxctx *vmxctx;
2979 struct vm_exit *vmexit;
2980 struct vlapic *vlapic;
2981 uint32_t exit_reason;
2982 struct region_descriptor gdtr, idtr;
2987 vmcs = &vmx->vmcs[vcpu];
2988 vmxctx = &vmx->ctx[vcpu];
2989 vlapic = vm_lapic(vm, vcpu);
2990 vmexit = vm_exitinfo(vm, vcpu);
2993 KASSERT(vmxctx->pmap == pmap,
2994 ("pmap %p different than ctx pmap %p", pmap, vmxctx->pmap));
2996 vmx_msr_guest_enter(vmx, vcpu);
3002 * We do this every time because we may setup the virtual machine
3003 * from a different process than the one that actually runs it.
3005 * If the life of a virtual machine was spent entirely in the context
3006 * of a single process we could do this once in vmx_init().
3008 vmcs_write(VMCS_HOST_CR3, rcr3());
3010 vmcs_write(VMCS_GUEST_RIP, rip);
3011 vmx_set_pcpu_defaults(vmx, vcpu, pmap);
3013 KASSERT(vmcs_guest_rip() == rip, ("%s: vmcs guest rip mismatch "
3014 "%#lx/%#lx", __func__, vmcs_guest_rip(), rip));
3016 handled = UNHANDLED;
3018 * Interrupts are disabled from this point on until the
3019 * guest starts executing. This is done for the following
3022 * If an AST is asserted on this thread after the check below,
3023 * then the IPI_AST notification will not be lost, because it
3024 * will cause a VM exit due to external interrupt as soon as
3025 * the guest state is loaded.
3027 * A posted interrupt after 'vmx_inject_interrupts()' will
3028 * not be "lost" because it will be held pending in the host
3029 * APIC because interrupts are disabled. The pending interrupt
3030 * will be recognized as soon as the guest state is loaded.
3032 * The same reasoning applies to the IPI generated by
3033 * pmap_invalidate_ept().
3036 vmx_inject_interrupts(vmx, vcpu, vlapic, rip);
3039 * Check for vcpu suspension after injecting events because
3040 * vmx_inject_interrupts() can suspend the vcpu due to a
3043 if (vcpu_suspended(evinfo)) {
3045 vm_exit_suspended(vmx->vm, vcpu, rip);
3049 if (vcpu_rendezvous_pending(evinfo)) {
3051 vm_exit_rendezvous(vmx->vm, vcpu, rip);
3055 if (vcpu_reqidle(evinfo)) {
3057 vm_exit_reqidle(vmx->vm, vcpu, rip);
3061 if (vcpu_should_yield(vm, vcpu)) {
3063 vm_exit_astpending(vmx->vm, vcpu, rip);
3064 vmx_astpending_trace(vmx, vcpu, rip);
3069 if (vcpu_debugged(vm, vcpu)) {
3071 vm_exit_debug(vmx->vm, vcpu, rip);
3076 * If TPR Shadowing is enabled, the TPR Threshold
3077 * must be updated right before entering the guest.
3079 if (tpr_shadowing && !virtual_interrupt_delivery) {
3080 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_USE_TPR_SHADOW) != 0) {
3081 vmcs_write(VMCS_TPR_THRESHOLD, vlapic_get_cr8(vlapic));
3086 * VM exits restore the base address but not the
3087 * limits of GDTR and IDTR. The VMCS only stores the
3088 * base address, so VM exits set the limits to 0xffff.
3089 * Save and restore the full GDTR and IDTR to restore
3092 * The VMCS does not save the LDTR at all, and VM
3093 * exits clear LDTR as if a NULL selector were loaded.
3094 * The userspace hypervisor probably doesn't use a
3095 * LDT, but save and restore it to be safe.
3102 * The TSC_AUX MSR must be saved/restored while interrupts
3103 * are disabled so that it is not possible for the guest
3104 * TSC_AUX MSR value to be overwritten by the resume
3105 * portion of the IPI_SUSPEND codepath. This is why the
3106 * transition of this MSR is handled separately from those
3107 * handled by vmx_msr_guest_{enter,exit}(), which are ok to
3108 * be transitioned with preemption disabled but interrupts
3111 * These vmx_msr_guest_{enter,exit}_tsc_aux() calls can be
3112 * anywhere in this loop so long as they happen with
3113 * interrupts disabled. This location is chosen for
3116 vmx_msr_guest_enter_tsc_aux(vmx, vcpu);
3118 vmx_dr_enter_guest(vmxctx);
3121 * Mark the EPT as active on this host CPU and invalidate
3122 * EPTP-tagged TLB entries if required.
3124 vmx_pmap_activate(vmx, pmap);
3126 vmx_run_trace(vmx, vcpu);
3127 rc = vmx_enter_guest(vmxctx, vmx, launched);
3129 vmx_pmap_deactivate(vmx, pmap);
3130 vmx_dr_leave_guest(vmxctx);
3131 vmx_msr_guest_exit_tsc_aux(vmx, vcpu);
3137 /* Collect some information for VM exit processing */
3138 vmexit->rip = rip = vmcs_guest_rip();
3139 vmexit->inst_length = vmexit_instruction_length();
3140 vmexit->u.vmx.exit_reason = exit_reason = vmcs_exit_reason();
3141 vmexit->u.vmx.exit_qualification = vmcs_exit_qualification();
3143 /* Update 'nextrip' */
3144 vmx->state[vcpu].nextrip = rip;
3146 if (rc == VMX_GUEST_VMEXIT) {
3147 vmx_exit_handle_nmi(vmx, vcpu, vmexit);
3149 handled = vmx_exit_process(vmx, vcpu, vmexit);
3152 vmx_exit_inst_error(vmxctx, rc, vmexit);
3155 vmx_exit_trace(vmx, vcpu, rip, exit_reason, handled);
3160 * If a VM exit has been handled then the exitcode must be BOGUS
3161 * If a VM exit is not handled then the exitcode must not be BOGUS
3163 if ((handled && vmexit->exitcode != VM_EXITCODE_BOGUS) ||
3164 (!handled && vmexit->exitcode == VM_EXITCODE_BOGUS)) {
3165 panic("Mismatch between handled (%d) and exitcode (%d)",
3166 handled, vmexit->exitcode);
3170 vmm_stat_incr(vm, vcpu, VMEXIT_USERSPACE, 1);
3172 VCPU_CTR1(vm, vcpu, "returning from vmx_run: exitcode %d",
3176 vmx_msr_guest_exit(vmx, vcpu);
3182 vmx_cleanup(void *arg)
3185 struct vmx *vmx = arg;
3188 if (apic_access_virtualization(vmx, 0))
3189 vm_unmap_mmio(vmx->vm, DEFAULT_APIC_BASE, PAGE_SIZE);
3191 maxcpus = vm_get_maxcpus(vmx->vm);
3192 for (i = 0; i < maxcpus; i++)
3193 vpid_free(vmx->state[i].vpid);
3201 vmxctx_regptr(struct vmxctx *vmxctx, int reg)
3205 case VM_REG_GUEST_RAX:
3206 return (&vmxctx->guest_rax);
3207 case VM_REG_GUEST_RBX:
3208 return (&vmxctx->guest_rbx);
3209 case VM_REG_GUEST_RCX:
3210 return (&vmxctx->guest_rcx);
3211 case VM_REG_GUEST_RDX:
3212 return (&vmxctx->guest_rdx);
3213 case VM_REG_GUEST_RSI:
3214 return (&vmxctx->guest_rsi);
3215 case VM_REG_GUEST_RDI:
3216 return (&vmxctx->guest_rdi);
3217 case VM_REG_GUEST_RBP:
3218 return (&vmxctx->guest_rbp);
3219 case VM_REG_GUEST_R8:
3220 return (&vmxctx->guest_r8);
3221 case VM_REG_GUEST_R9:
3222 return (&vmxctx->guest_r9);
3223 case VM_REG_GUEST_R10:
3224 return (&vmxctx->guest_r10);
3225 case VM_REG_GUEST_R11:
3226 return (&vmxctx->guest_r11);
3227 case VM_REG_GUEST_R12:
3228 return (&vmxctx->guest_r12);
3229 case VM_REG_GUEST_R13:
3230 return (&vmxctx->guest_r13);
3231 case VM_REG_GUEST_R14:
3232 return (&vmxctx->guest_r14);
3233 case VM_REG_GUEST_R15:
3234 return (&vmxctx->guest_r15);
3235 case VM_REG_GUEST_CR2:
3236 return (&vmxctx->guest_cr2);
3237 case VM_REG_GUEST_DR0:
3238 return (&vmxctx->guest_dr0);
3239 case VM_REG_GUEST_DR1:
3240 return (&vmxctx->guest_dr1);
3241 case VM_REG_GUEST_DR2:
3242 return (&vmxctx->guest_dr2);
3243 case VM_REG_GUEST_DR3:
3244 return (&vmxctx->guest_dr3);
3245 case VM_REG_GUEST_DR6:
3246 return (&vmxctx->guest_dr6);
3254 vmxctx_getreg(struct vmxctx *vmxctx, int reg, uint64_t *retval)
3258 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
3266 vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val)
3270 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
3278 vmx_get_intr_shadow(struct vmx *vmx, int vcpu, int running, uint64_t *retval)
3283 error = vmcs_getreg(&vmx->vmcs[vcpu], running,
3284 VMCS_IDENT(VMCS_GUEST_INTERRUPTIBILITY), &gi);
3285 *retval = (gi & HWINTR_BLOCKING) ? 1 : 0;
3290 vmx_modify_intr_shadow(struct vmx *vmx, int vcpu, int running, uint64_t val)
3297 * Forcing the vcpu into an interrupt shadow is not supported.
3304 vmcs = &vmx->vmcs[vcpu];
3305 ident = VMCS_IDENT(VMCS_GUEST_INTERRUPTIBILITY);
3306 error = vmcs_getreg(vmcs, running, ident, &gi);
3308 gi &= ~HWINTR_BLOCKING;
3309 error = vmcs_setreg(vmcs, running, ident, gi);
3312 VCPU_CTR2(vmx->vm, vcpu, "Setting intr_shadow to %#lx %s", val,
3313 error ? "failed" : "succeeded");
3318 vmx_shadow_reg(int reg)
3325 case VM_REG_GUEST_CR0:
3326 shreg = VMCS_CR0_SHADOW;
3328 case VM_REG_GUEST_CR4:
3329 shreg = VMCS_CR4_SHADOW;
3339 vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval)
3341 int running, hostcpu;
3342 struct vmx *vmx = arg;
3344 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
3345 if (running && hostcpu != curcpu)
3346 panic("vmx_getreg: %s%d is running", vm_name(vmx->vm), vcpu);
3348 if (reg == VM_REG_GUEST_INTR_SHADOW)
3349 return (vmx_get_intr_shadow(vmx, vcpu, running, retval));
3351 if (vmxctx_getreg(&vmx->ctx[vcpu], reg, retval) == 0)
3354 return (vmcs_getreg(&vmx->vmcs[vcpu], running, reg, retval));
3358 vmx_setreg(void *arg, int vcpu, int reg, uint64_t val)
3360 int error, hostcpu, running, shadow;
3363 struct vmx *vmx = arg;
3365 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
3366 if (running && hostcpu != curcpu)
3367 panic("vmx_setreg: %s%d is running", vm_name(vmx->vm), vcpu);
3369 if (reg == VM_REG_GUEST_INTR_SHADOW)
3370 return (vmx_modify_intr_shadow(vmx, vcpu, running, val));
3372 if (vmxctx_setreg(&vmx->ctx[vcpu], reg, val) == 0)
3375 /* Do not permit user write access to VMCS fields by offset. */
3379 error = vmcs_setreg(&vmx->vmcs[vcpu], running, reg, val);
3383 * If the "load EFER" VM-entry control is 1 then the
3384 * value of EFER.LMA must be identical to "IA-32e mode guest"
3385 * bit in the VM-entry control.
3387 if ((entry_ctls & VM_ENTRY_LOAD_EFER) != 0 &&
3388 (reg == VM_REG_GUEST_EFER)) {
3389 vmcs_getreg(&vmx->vmcs[vcpu], running,
3390 VMCS_IDENT(VMCS_ENTRY_CTLS), &ctls);
3392 ctls |= VM_ENTRY_GUEST_LMA;
3394 ctls &= ~VM_ENTRY_GUEST_LMA;
3395 vmcs_setreg(&vmx->vmcs[vcpu], running,
3396 VMCS_IDENT(VMCS_ENTRY_CTLS), ctls);
3399 shadow = vmx_shadow_reg(reg);
3402 * Store the unmodified value in the shadow
3404 error = vmcs_setreg(&vmx->vmcs[vcpu], running,
3405 VMCS_IDENT(shadow), val);
3408 if (reg == VM_REG_GUEST_CR3) {
3410 * Invalidate the guest vcpu's TLB mappings to emulate
3411 * the behavior of updating %cr3.
3413 * XXX the processor retains global mappings when %cr3
3414 * is updated but vmx_invvpid() does not.
3416 pmap = vmx->ctx[vcpu].pmap;
3417 vmx_invvpid(vmx, vcpu, pmap, running);
3425 vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
3427 int hostcpu, running;
3428 struct vmx *vmx = arg;
3430 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
3431 if (running && hostcpu != curcpu)
3432 panic("vmx_getdesc: %s%d is running", vm_name(vmx->vm), vcpu);
3434 return (vmcs_getdesc(&vmx->vmcs[vcpu], running, reg, desc));
3438 vmx_setdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
3440 int hostcpu, running;
3441 struct vmx *vmx = arg;
3443 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
3444 if (running && hostcpu != curcpu)
3445 panic("vmx_setdesc: %s%d is running", vm_name(vmx->vm), vcpu);
3447 return (vmcs_setdesc(&vmx->vmcs[vcpu], running, reg, desc));
3451 vmx_getcap(void *arg, int vcpu, int type, int *retval)
3453 struct vmx *vmx = arg;
3459 vcap = vmx->cap[vcpu].set;
3462 case VM_CAP_HALT_EXIT:
3466 case VM_CAP_PAUSE_EXIT:
3470 case VM_CAP_MTRAP_EXIT:
3471 if (cap_monitor_trap)
3482 case VM_CAP_UNRESTRICTED_GUEST:
3483 if (cap_unrestricted_guest)
3486 case VM_CAP_ENABLE_INVPCID:
3490 case VM_CAP_BPT_EXIT:
3498 *retval = (vcap & (1 << type)) ? 1 : 0;
3504 vmx_setcap(void *arg, int vcpu, int type, int val)
3506 struct vmx *vmx = arg;
3507 struct vmcs *vmcs = &vmx->vmcs[vcpu];
3519 case VM_CAP_HALT_EXIT:
3520 if (cap_halt_exit) {
3522 pptr = &vmx->cap[vcpu].proc_ctls;
3524 flag = PROCBASED_HLT_EXITING;
3525 reg = VMCS_PRI_PROC_BASED_CTLS;
3528 case VM_CAP_MTRAP_EXIT:
3529 if (cap_monitor_trap) {
3531 pptr = &vmx->cap[vcpu].proc_ctls;
3533 flag = PROCBASED_MTF;
3534 reg = VMCS_PRI_PROC_BASED_CTLS;
3537 case VM_CAP_PAUSE_EXIT:
3538 if (cap_pause_exit) {
3540 pptr = &vmx->cap[vcpu].proc_ctls;
3542 flag = PROCBASED_PAUSE_EXITING;
3543 reg = VMCS_PRI_PROC_BASED_CTLS;
3548 if (cap_rdpid || cap_rdtscp)
3550 * Choose not to support enabling/disabling
3551 * RDPID/RDTSCP via libvmmapi since, as per the
3552 * discussion in vmx_modinit(), RDPID/RDTSCP are
3553 * either always enabled or always disabled.
3557 case VM_CAP_UNRESTRICTED_GUEST:
3558 if (cap_unrestricted_guest) {
3560 pptr = &vmx->cap[vcpu].proc_ctls2;
3562 flag = PROCBASED2_UNRESTRICTED_GUEST;
3563 reg = VMCS_SEC_PROC_BASED_CTLS;
3566 case VM_CAP_ENABLE_INVPCID:
3569 pptr = &vmx->cap[vcpu].proc_ctls2;
3571 flag = PROCBASED2_ENABLE_INVPCID;
3572 reg = VMCS_SEC_PROC_BASED_CTLS;
3575 case VM_CAP_BPT_EXIT:
3578 /* Don't change the bitmap if we are tracing all exceptions. */
3579 if (vmx->cap[vcpu].exc_bitmap != 0xffffffff) {
3580 pptr = &vmx->cap[vcpu].exc_bitmap;
3582 flag = (1 << IDT_BP);
3583 reg = VMCS_EXCEPTION_BITMAP;
3600 error = vmwrite(reg, baseval);
3607 * Update optional stored flags, and record
3614 vmx->cap[vcpu].set |= (1 << type);
3616 vmx->cap[vcpu].set &= ~(1 << type);
3622 static struct vmspace *
3623 vmx_vmspace_alloc(vm_offset_t min, vm_offset_t max)
3625 return (ept_vmspace_alloc(min, max));
3629 vmx_vmspace_free(struct vmspace *vmspace)
3631 ept_vmspace_free(vmspace);
3635 struct vlapic vlapic;
3636 struct pir_desc *pir_desc;
3641 #define VPR_PRIO_BIT(vpr) (1 << ((vpr) >> 4))
3643 #define VMX_CTR_PIR(vm, vcpuid, pir_desc, notify, vector, level, msg) \
3645 VCPU_CTR2(vm, vcpuid, msg " assert %s-triggered vector %d", \
3646 level ? "level" : "edge", vector); \
3647 VCPU_CTR1(vm, vcpuid, msg " pir0 0x%016lx", pir_desc->pir[0]); \
3648 VCPU_CTR1(vm, vcpuid, msg " pir1 0x%016lx", pir_desc->pir[1]); \
3649 VCPU_CTR1(vm, vcpuid, msg " pir2 0x%016lx", pir_desc->pir[2]); \
3650 VCPU_CTR1(vm, vcpuid, msg " pir3 0x%016lx", pir_desc->pir[3]); \
3651 VCPU_CTR1(vm, vcpuid, msg " notify: %s", notify ? "yes" : "no");\
3655 * vlapic->ops handlers that utilize the APICv hardware assist described in
3656 * Chapter 29 of the Intel SDM.
3659 vmx_set_intr_ready(struct vlapic *vlapic, int vector, bool level)
3661 struct vlapic_vtx *vlapic_vtx;
3662 struct pir_desc *pir_desc;
3664 int idx, notify = 0;
3666 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3667 pir_desc = vlapic_vtx->pir_desc;
3670 * Keep track of interrupt requests in the PIR descriptor. This is
3671 * because the virtual APIC page pointed to by the VMCS cannot be
3672 * modified if the vcpu is running.
3675 mask = 1UL << (vector % 64);
3676 atomic_set_long(&pir_desc->pir[idx], mask);
3679 * A notification is required whenever the 'pending' bit makes a
3680 * transition from 0->1.
3682 * Even if the 'pending' bit is already asserted, notification about
3683 * the incoming interrupt may still be necessary. For example, if a
3684 * vCPU is HLTed with a high PPR, a low priority interrupt would cause
3685 * the 0->1 'pending' transition with a notification, but the vCPU
3686 * would ignore the interrupt for the time being. The same vCPU would
3687 * need to then be notified if a high-priority interrupt arrived which
3688 * satisfied the PPR.
3690 * The priorities of interrupts injected while 'pending' is asserted
3691 * are tracked in a custom bitfield 'pending_prio'. Should the
3692 * to-be-injected interrupt exceed the priorities already present, the
3693 * notification is sent. The priorities recorded in 'pending_prio' are
3694 * cleared whenever the 'pending' bit makes another 0->1 transition.
3696 if (atomic_cmpset_long(&pir_desc->pending, 0, 1) != 0) {
3698 vlapic_vtx->pending_prio = 0;
3700 const u_int old_prio = vlapic_vtx->pending_prio;
3701 const u_int prio_bit = VPR_PRIO_BIT(vector & APIC_TPR_INT);
3703 if ((old_prio & prio_bit) == 0 && prio_bit > old_prio) {
3704 atomic_set_int(&vlapic_vtx->pending_prio, prio_bit);
3709 VMX_CTR_PIR(vlapic->vm, vlapic->vcpuid, pir_desc, notify, vector,
3710 level, "vmx_set_intr_ready");
3715 vmx_pending_intr(struct vlapic *vlapic, int *vecptr)
3717 struct vlapic_vtx *vlapic_vtx;
3718 struct pir_desc *pir_desc;
3719 struct LAPIC *lapic;
3720 uint64_t pending, pirval;
3725 * This function is only expected to be called from the 'HLT' exit
3726 * handler which does not care about the vector that is pending.
3728 KASSERT(vecptr == NULL, ("vmx_pending_intr: vecptr must be NULL"));
3730 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3731 pir_desc = vlapic_vtx->pir_desc;
3733 pending = atomic_load_acq_long(&pir_desc->pending);
3736 * While a virtual interrupt may have already been
3737 * processed the actual delivery maybe pending the
3738 * interruptibility of the guest. Recognize a pending
3739 * interrupt by reevaluating virtual interrupts
3740 * following Section 29.2.1 in the Intel SDM Volume 3.
3742 struct vm_exit *vmexit;
3745 vmexit = vm_exitinfo(vlapic->vm, vlapic->vcpuid);
3746 KASSERT(vmexit->exitcode == VM_EXITCODE_HLT,
3747 ("vmx_pending_intr: exitcode not 'HLT'"));
3748 rvi = vmexit->u.hlt.intr_status & APIC_TPR_INT;
3749 lapic = vlapic->apic_page;
3750 ppr = lapic->ppr & APIC_TPR_INT;
3759 * If there is an interrupt pending then it will be recognized only
3760 * if its priority is greater than the processor priority.
3762 * Special case: if the processor priority is zero then any pending
3763 * interrupt will be recognized.
3765 lapic = vlapic->apic_page;
3766 ppr = lapic->ppr & APIC_TPR_INT;
3770 VCPU_CTR1(vlapic->vm, vlapic->vcpuid, "HLT with non-zero PPR %d",
3774 for (i = 3; i >= 0; i--) {
3775 pirval = pir_desc->pir[i];
3777 vpr = (i * 64 + flsl(pirval) - 1) & APIC_TPR_INT;
3783 * If the highest-priority pending interrupt falls short of the
3784 * processor priority of this vCPU, ensure that 'pending_prio' does not
3785 * have any stale bits which would preclude a higher-priority interrupt
3786 * from incurring a notification later.
3789 const u_int prio_bit = VPR_PRIO_BIT(vpr);
3790 const u_int old = vlapic_vtx->pending_prio;
3792 if (old > prio_bit && (old & prio_bit) == 0) {
3793 vlapic_vtx->pending_prio = prio_bit;
3801 vmx_intr_accepted(struct vlapic *vlapic, int vector)
3804 panic("vmx_intr_accepted: not expected to be called");
3808 vmx_set_tmr(struct vlapic *vlapic, int vector, bool level)
3810 struct vlapic_vtx *vlapic_vtx;
3815 KASSERT(vector >= 0 && vector <= 255, ("invalid vector %d", vector));
3816 KASSERT(!vcpu_is_running(vlapic->vm, vlapic->vcpuid, NULL),
3817 ("vmx_set_tmr: vcpu cannot be running"));
3819 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3820 vmx = vlapic_vtx->vmx;
3821 vmcs = &vmx->vmcs[vlapic->vcpuid];
3822 mask = 1UL << (vector % 64);
3825 val = vmcs_read(VMCS_EOI_EXIT(vector));
3830 vmcs_write(VMCS_EOI_EXIT(vector), val);
3835 vmx_enable_x2apic_mode_ts(struct vlapic *vlapic)
3842 vcpuid = vlapic->vcpuid;
3843 vmx = ((struct vlapic_vtx *)vlapic)->vmx;
3844 vmcs = &vmx->vmcs[vcpuid];
3846 proc_ctls = vmx->cap[vcpuid].proc_ctls;
3847 proc_ctls &= ~PROCBASED_USE_TPR_SHADOW;
3848 proc_ctls |= PROCBASED_CR8_LOAD_EXITING;
3849 proc_ctls |= PROCBASED_CR8_STORE_EXITING;
3850 vmx->cap[vcpuid].proc_ctls = proc_ctls;
3853 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, proc_ctls);
3858 vmx_enable_x2apic_mode_vid(struct vlapic *vlapic)
3862 uint32_t proc_ctls2;
3865 vcpuid = vlapic->vcpuid;
3866 vmx = ((struct vlapic_vtx *)vlapic)->vmx;
3867 vmcs = &vmx->vmcs[vcpuid];
3869 proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
3870 KASSERT((proc_ctls2 & PROCBASED2_VIRTUALIZE_APIC_ACCESSES) != 0,
3871 ("%s: invalid proc_ctls2 %#x", __func__, proc_ctls2));
3873 proc_ctls2 &= ~PROCBASED2_VIRTUALIZE_APIC_ACCESSES;
3874 proc_ctls2 |= PROCBASED2_VIRTUALIZE_X2APIC_MODE;
3875 vmx->cap[vcpuid].proc_ctls2 = proc_ctls2;
3878 vmcs_write(VMCS_SEC_PROC_BASED_CTLS, proc_ctls2);
3881 if (vlapic->vcpuid == 0) {
3883 * The nested page table mappings are shared by all vcpus
3884 * so unmap the APIC access page just once.
3886 error = vm_unmap_mmio(vmx->vm, DEFAULT_APIC_BASE, PAGE_SIZE);
3887 KASSERT(error == 0, ("%s: vm_unmap_mmio error %d",
3891 * The MSR bitmap is shared by all vcpus so modify it only
3892 * once in the context of vcpu 0.
3894 error = vmx_allow_x2apic_msrs(vmx);
3895 KASSERT(error == 0, ("%s: vmx_allow_x2apic_msrs error %d",
3901 vmx_post_intr(struct vlapic *vlapic, int hostcpu)
3904 ipi_cpu(hostcpu, pirvec);
3908 * Transfer the pending interrupts in the PIR descriptor to the IRR
3909 * in the virtual APIC page.
3912 vmx_inject_pir(struct vlapic *vlapic)
3914 struct vlapic_vtx *vlapic_vtx;
3915 struct pir_desc *pir_desc;
3916 struct LAPIC *lapic;
3917 uint64_t val, pirval;
3918 int rvi, pirbase = -1;
3919 uint16_t intr_status_old, intr_status_new;
3921 vlapic_vtx = (struct vlapic_vtx *)vlapic;
3922 pir_desc = vlapic_vtx->pir_desc;
3923 if (atomic_cmpset_long(&pir_desc->pending, 1, 0) == 0) {
3924 VCPU_CTR0(vlapic->vm, vlapic->vcpuid, "vmx_inject_pir: "
3925 "no posted interrupt pending");
3931 lapic = vlapic->apic_page;
3933 val = atomic_readandclear_long(&pir_desc->pir[0]);
3936 lapic->irr1 |= val >> 32;
3941 val = atomic_readandclear_long(&pir_desc->pir[1]);
3944 lapic->irr3 |= val >> 32;
3949 val = atomic_readandclear_long(&pir_desc->pir[2]);
3952 lapic->irr5 |= val >> 32;
3957 val = atomic_readandclear_long(&pir_desc->pir[3]);
3960 lapic->irr7 |= val >> 32;
3965 VLAPIC_CTR_IRR(vlapic, "vmx_inject_pir");
3968 * Update RVI so the processor can evaluate pending virtual
3969 * interrupts on VM-entry.
3971 * It is possible for pirval to be 0 here, even though the
3972 * pending bit has been set. The scenario is:
3973 * CPU-Y is sending a posted interrupt to CPU-X, which
3974 * is running a guest and processing posted interrupts in h/w.
3975 * CPU-X will eventually exit and the state seen in s/w is
3976 * the pending bit set, but no PIR bits set.
3979 * (vm running) (host running)
3980 * rx posted interrupt
3983 * READ/CLEAR PIR bits
3986 * pending bit set, PIR 0
3989 rvi = pirbase + flsl(pirval) - 1;
3990 intr_status_old = vmcs_read(VMCS_GUEST_INTR_STATUS);
3991 intr_status_new = (intr_status_old & 0xFF00) | rvi;
3992 if (intr_status_new > intr_status_old) {
3993 vmcs_write(VMCS_GUEST_INTR_STATUS, intr_status_new);
3994 VCPU_CTR2(vlapic->vm, vlapic->vcpuid, "vmx_inject_pir: "
3995 "guest_intr_status changed from 0x%04x to 0x%04x",
3996 intr_status_old, intr_status_new);
4001 static struct vlapic *
4002 vmx_vlapic_init(void *arg, int vcpuid)
4005 struct vlapic *vlapic;
4006 struct vlapic_vtx *vlapic_vtx;
4010 vlapic = malloc(sizeof(struct vlapic_vtx), M_VLAPIC, M_WAITOK | M_ZERO);
4011 vlapic->vm = vmx->vm;
4012 vlapic->vcpuid = vcpuid;
4013 vlapic->apic_page = (struct LAPIC *)&vmx->apic_page[vcpuid];
4015 vlapic_vtx = (struct vlapic_vtx *)vlapic;
4016 vlapic_vtx->pir_desc = &vmx->pir_desc[vcpuid];
4017 vlapic_vtx->vmx = vmx;
4019 if (tpr_shadowing) {
4020 vlapic->ops.enable_x2apic_mode = vmx_enable_x2apic_mode_ts;
4023 if (virtual_interrupt_delivery) {
4024 vlapic->ops.set_intr_ready = vmx_set_intr_ready;
4025 vlapic->ops.pending_intr = vmx_pending_intr;
4026 vlapic->ops.intr_accepted = vmx_intr_accepted;
4027 vlapic->ops.set_tmr = vmx_set_tmr;
4028 vlapic->ops.enable_x2apic_mode = vmx_enable_x2apic_mode_vid;
4031 if (posted_interrupts)
4032 vlapic->ops.post_intr = vmx_post_intr;
4034 vlapic_init(vlapic);
4040 vmx_vlapic_cleanup(void *arg, struct vlapic *vlapic)
4043 vlapic_cleanup(vlapic);
4044 free(vlapic, M_VLAPIC);
4047 #ifdef BHYVE_SNAPSHOT
4049 vmx_snapshot(void *arg, struct vm_snapshot_meta *meta)
4052 struct vmxctx *vmxctx;
4058 KASSERT(vmx != NULL, ("%s: arg was NULL", __func__));
4060 for (i = 0; i < VM_MAXCPU; i++) {
4061 SNAPSHOT_BUF_OR_LEAVE(vmx->guest_msrs[i],
4062 sizeof(vmx->guest_msrs[i]), meta, ret, done);
4064 vmxctx = &vmx->ctx[i];
4065 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_rdi, meta, ret, done);
4066 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_rsi, meta, ret, done);
4067 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_rdx, meta, ret, done);
4068 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_rcx, meta, ret, done);
4069 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_r8, meta, ret, done);
4070 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_r9, meta, ret, done);
4071 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_rax, meta, ret, done);
4072 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_rbx, meta, ret, done);
4073 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_rbp, meta, ret, done);
4074 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_r10, meta, ret, done);
4075 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_r11, meta, ret, done);
4076 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_r12, meta, ret, done);
4077 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_r13, meta, ret, done);
4078 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_r14, meta, ret, done);
4079 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_r15, meta, ret, done);
4080 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_cr2, meta, ret, done);
4081 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_dr0, meta, ret, done);
4082 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_dr1, meta, ret, done);
4083 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_dr2, meta, ret, done);
4084 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_dr3, meta, ret, done);
4085 SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_dr6, meta, ret, done);
4093 vmx_vmcx_snapshot(void *arg, struct vm_snapshot_meta *meta, int vcpu)
4097 int err, run, hostcpu;
4099 vmx = (struct vmx *)arg;
4102 KASSERT(arg != NULL, ("%s: arg was NULL", __func__));
4103 vmcs = &vmx->vmcs[vcpu];
4105 run = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
4106 if (run && hostcpu != curcpu) {
4107 printf("%s: %s%d is running", __func__, vm_name(vmx->vm), vcpu);
4111 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_CR0, meta);
4112 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_CR3, meta);
4113 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_CR4, meta);
4114 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_DR7, meta);
4115 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_RSP, meta);
4116 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_RIP, meta);
4117 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_RFLAGS, meta);
4119 /* Guest segments */
4120 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_ES, meta);
4121 err += vmcs_snapshot_desc(vmcs, run, VM_REG_GUEST_ES, meta);
4123 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_CS, meta);
4124 err += vmcs_snapshot_desc(vmcs, run, VM_REG_GUEST_CS, meta);
4126 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_SS, meta);
4127 err += vmcs_snapshot_desc(vmcs, run, VM_REG_GUEST_SS, meta);
4129 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_DS, meta);
4130 err += vmcs_snapshot_desc(vmcs, run, VM_REG_GUEST_DS, meta);
4132 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_FS, meta);
4133 err += vmcs_snapshot_desc(vmcs, run, VM_REG_GUEST_FS, meta);
4135 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_GS, meta);
4136 err += vmcs_snapshot_desc(vmcs, run, VM_REG_GUEST_GS, meta);
4138 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_TR, meta);
4139 err += vmcs_snapshot_desc(vmcs, run, VM_REG_GUEST_TR, meta);
4141 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_LDTR, meta);
4142 err += vmcs_snapshot_desc(vmcs, run, VM_REG_GUEST_LDTR, meta);
4144 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_EFER, meta);
4146 err += vmcs_snapshot_desc(vmcs, run, VM_REG_GUEST_IDTR, meta);
4147 err += vmcs_snapshot_desc(vmcs, run, VM_REG_GUEST_GDTR, meta);
4149 /* Guest page tables */
4150 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_PDPTE0, meta);
4151 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_PDPTE1, meta);
4152 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_PDPTE2, meta);
4153 err += vmcs_snapshot_reg(vmcs, run, VM_REG_GUEST_PDPTE3, meta);
4155 /* Other guest state */
4156 err += vmcs_snapshot_any(vmcs, run, VMCS_GUEST_IA32_SYSENTER_CS, meta);
4157 err += vmcs_snapshot_any(vmcs, run, VMCS_GUEST_IA32_SYSENTER_ESP, meta);
4158 err += vmcs_snapshot_any(vmcs, run, VMCS_GUEST_IA32_SYSENTER_EIP, meta);
4159 err += vmcs_snapshot_any(vmcs, run, VMCS_GUEST_INTERRUPTIBILITY, meta);
4160 err += vmcs_snapshot_any(vmcs, run, VMCS_GUEST_ACTIVITY, meta);
4161 err += vmcs_snapshot_any(vmcs, run, VMCS_ENTRY_CTLS, meta);
4162 err += vmcs_snapshot_any(vmcs, run, VMCS_EXIT_CTLS, meta);
4168 vmx_restore_tsc(void *arg, int vcpu, uint64_t offset)
4171 struct vmx *vmx = (struct vmx *)arg;
4172 int error, running, hostcpu;
4174 KASSERT(arg != NULL, ("%s: arg was NULL", __func__));
4175 vmcs = &vmx->vmcs[vcpu];
4177 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
4178 if (running && hostcpu != curcpu) {
4179 printf("%s: %s%d is running", __func__, vm_name(vmx->vm), vcpu);
4186 error = vmx_set_tsc_offset(vmx, vcpu, offset);
4194 const struct vmm_ops vmm_ops_intel = {
4195 .modinit = vmx_modinit,
4196 .modcleanup = vmx_modcleanup,
4197 .modresume = vmx_modresume,
4200 .cleanup = vmx_cleanup,
4201 .getreg = vmx_getreg,
4202 .setreg = vmx_setreg,
4203 .getdesc = vmx_getdesc,
4204 .setdesc = vmx_setdesc,
4205 .getcap = vmx_getcap,
4206 .setcap = vmx_setcap,
4207 .vmspace_alloc = vmx_vmspace_alloc,
4208 .vmspace_free = vmx_vmspace_free,
4209 .vlapic_init = vmx_vlapic_init,
4210 .vlapic_cleanup = vmx_vlapic_cleanup,
4211 #ifdef BHYVE_SNAPSHOT
4212 .snapshot = vmx_snapshot,
4213 .vmcx_snapshot = vmx_vmcx_snapshot,
4214 .restore_tsc = vmx_restore_tsc,