vmx.c revision 284899
1/*-
2 * Copyright (c) 2011 NetApp, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: stable/10/sys/amd64/vmm/intel/vmx.c 284899 2015-06-28 01:21:55Z neel $
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: stable/10/sys/amd64/vmm/intel/vmx.c 284899 2015-06-28 01:21:55Z neel $");
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/smp.h>
35#include <sys/kernel.h>
36#include <sys/malloc.h>
37#include <sys/pcpu.h>
38#include <sys/proc.h>
39#include <sys/sysctl.h>
40
41#include <vm/vm.h>
42#include <vm/pmap.h>
43
44#include <machine/psl.h>
45#include <machine/cpufunc.h>
46#include <machine/md_var.h>
47#include <machine/segments.h>
48#include <machine/smp.h>
49#include <machine/specialreg.h>
50#include <machine/vmparam.h>
51
52#include <machine/vmm.h>
53#include <machine/vmm_dev.h>
54#include <machine/vmm_instruction_emul.h>
55#include "vmm_lapic.h"
56#include "vmm_host.h"
57#include "vmm_ioport.h"
58#include "vmm_ipi.h"
59#include "vmm_ktr.h"
60#include "vmm_stat.h"
61#include "vatpic.h"
62#include "vlapic.h"
63#include "vlapic_priv.h"
64
65#include "ept.h"
66#include "vmx_cpufunc.h"
67#include "vmx.h"
68#include "vmx_msr.h"
69#include "x86.h"
70#include "vmx_controls.h"
71
72#define	PINBASED_CTLS_ONE_SETTING					\
73	(PINBASED_EXTINT_EXITING	|				\
74	 PINBASED_NMI_EXITING		|				\
75	 PINBASED_VIRTUAL_NMI)
76#define	PINBASED_CTLS_ZERO_SETTING	0
77
78#define PROCBASED_CTLS_WINDOW_SETTING					\
79	(PROCBASED_INT_WINDOW_EXITING	|				\
80	 PROCBASED_NMI_WINDOW_EXITING)
81
82#define	PROCBASED_CTLS_ONE_SETTING 					\
83	(PROCBASED_SECONDARY_CONTROLS	|				\
84	 PROCBASED_MWAIT_EXITING	|				\
85	 PROCBASED_MONITOR_EXITING	|				\
86	 PROCBASED_IO_EXITING		|				\
87	 PROCBASED_MSR_BITMAPS		|				\
88	 PROCBASED_CTLS_WINDOW_SETTING	|				\
89	 PROCBASED_CR8_LOAD_EXITING	|				\
90	 PROCBASED_CR8_STORE_EXITING)
91#define	PROCBASED_CTLS_ZERO_SETTING	\
92	(PROCBASED_CR3_LOAD_EXITING |	\
93	PROCBASED_CR3_STORE_EXITING |	\
94	PROCBASED_IO_BITMAPS)
95
96#define	PROCBASED_CTLS2_ONE_SETTING	PROCBASED2_ENABLE_EPT
97#define	PROCBASED_CTLS2_ZERO_SETTING	0
98
99#define	VM_EXIT_CTLS_ONE_SETTING					\
100	(VM_EXIT_HOST_LMA			|			\
101	VM_EXIT_SAVE_EFER			|			\
102	VM_EXIT_LOAD_EFER			|			\
103	VM_EXIT_ACKNOWLEDGE_INTERRUPT)
104
105#define	VM_EXIT_CTLS_ZERO_SETTING	VM_EXIT_SAVE_DEBUG_CONTROLS
106
107#define	VM_ENTRY_CTLS_ONE_SETTING	(VM_ENTRY_LOAD_EFER)
108
109#define	VM_ENTRY_CTLS_ZERO_SETTING					\
110	(VM_ENTRY_LOAD_DEBUG_CONTROLS		|			\
111	VM_ENTRY_INTO_SMM			|			\
112	VM_ENTRY_DEACTIVATE_DUAL_MONITOR)
113
114#define	HANDLED		1
115#define	UNHANDLED	0
116
117static MALLOC_DEFINE(M_VMX, "vmx", "vmx");
118static MALLOC_DEFINE(M_VLAPIC, "vlapic", "vlapic");
119
120SYSCTL_DECL(_hw_vmm);
121SYSCTL_NODE(_hw_vmm, OID_AUTO, vmx, CTLFLAG_RW, NULL, NULL);
122
123int vmxon_enabled[MAXCPU];
124static char vmxon_region[MAXCPU][PAGE_SIZE] __aligned(PAGE_SIZE);
125
126static uint32_t pinbased_ctls, procbased_ctls, procbased_ctls2;
127static uint32_t exit_ctls, entry_ctls;
128
129static uint64_t cr0_ones_mask, cr0_zeros_mask;
130SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_ones_mask, CTLFLAG_RD,
131	     &cr0_ones_mask, 0, NULL);
132SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_zeros_mask, CTLFLAG_RD,
133	     &cr0_zeros_mask, 0, NULL);
134
135static uint64_t cr4_ones_mask, cr4_zeros_mask;
136SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_ones_mask, CTLFLAG_RD,
137	     &cr4_ones_mask, 0, NULL);
138SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_zeros_mask, CTLFLAG_RD,
139	     &cr4_zeros_mask, 0, NULL);
140
141static int vmx_initialized;
142SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, initialized, CTLFLAG_RD,
143	   &vmx_initialized, 0, "Intel VMX initialized");
144
145/*
146 * Optional capabilities
147 */
148static SYSCTL_NODE(_hw_vmm_vmx, OID_AUTO, cap, CTLFLAG_RW, NULL, NULL);
149
150static int cap_halt_exit;
151SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, halt_exit, CTLFLAG_RD, &cap_halt_exit, 0,
152    "HLT triggers a VM-exit");
153
154static int cap_pause_exit;
155SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, pause_exit, CTLFLAG_RD, &cap_pause_exit,
156    0, "PAUSE triggers a VM-exit");
157
158static int cap_unrestricted_guest;
159SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, unrestricted_guest, CTLFLAG_RD,
160    &cap_unrestricted_guest, 0, "Unrestricted guests");
161
162static int cap_monitor_trap;
163SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, monitor_trap, CTLFLAG_RD,
164    &cap_monitor_trap, 0, "Monitor trap flag");
165
166static int cap_invpcid;
167SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, invpcid, CTLFLAG_RD, &cap_invpcid,
168    0, "Guests are allowed to use INVPCID");
169
170static int virtual_interrupt_delivery;
171SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, virtual_interrupt_delivery, CTLFLAG_RD,
172    &virtual_interrupt_delivery, 0, "APICv virtual interrupt delivery support");
173
174static int posted_interrupts;
175SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, posted_interrupts, CTLFLAG_RD,
176    &posted_interrupts, 0, "APICv posted interrupt support");
177
178static int pirvec;
179SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, posted_interrupt_vector, CTLFLAG_RD,
180    &pirvec, 0, "APICv posted interrupt vector");
181
182static struct unrhdr *vpid_unr;
183static u_int vpid_alloc_failed;
184SYSCTL_UINT(_hw_vmm_vmx, OID_AUTO, vpid_alloc_failed, CTLFLAG_RD,
185	    &vpid_alloc_failed, 0, NULL);
186
187/*
188 * Use the last page below 4GB as the APIC access address. This address is
189 * occupied by the boot firmware so it is guaranteed that it will not conflict
190 * with a page in system memory.
191 */
192#define	APIC_ACCESS_ADDRESS	0xFFFFF000
193
194static int vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc);
195static int vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval);
196static int vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val);
197static void vmx_inject_pir(struct vlapic *vlapic);
198
199#ifdef KTR
200static const char *
201exit_reason_to_str(int reason)
202{
203	static char reasonbuf[32];
204
205	switch (reason) {
206	case EXIT_REASON_EXCEPTION:
207		return "exception";
208	case EXIT_REASON_EXT_INTR:
209		return "extint";
210	case EXIT_REASON_TRIPLE_FAULT:
211		return "triplefault";
212	case EXIT_REASON_INIT:
213		return "init";
214	case EXIT_REASON_SIPI:
215		return "sipi";
216	case EXIT_REASON_IO_SMI:
217		return "iosmi";
218	case EXIT_REASON_SMI:
219		return "smi";
220	case EXIT_REASON_INTR_WINDOW:
221		return "intrwindow";
222	case EXIT_REASON_NMI_WINDOW:
223		return "nmiwindow";
224	case EXIT_REASON_TASK_SWITCH:
225		return "taskswitch";
226	case EXIT_REASON_CPUID:
227		return "cpuid";
228	case EXIT_REASON_GETSEC:
229		return "getsec";
230	case EXIT_REASON_HLT:
231		return "hlt";
232	case EXIT_REASON_INVD:
233		return "invd";
234	case EXIT_REASON_INVLPG:
235		return "invlpg";
236	case EXIT_REASON_RDPMC:
237		return "rdpmc";
238	case EXIT_REASON_RDTSC:
239		return "rdtsc";
240	case EXIT_REASON_RSM:
241		return "rsm";
242	case EXIT_REASON_VMCALL:
243		return "vmcall";
244	case EXIT_REASON_VMCLEAR:
245		return "vmclear";
246	case EXIT_REASON_VMLAUNCH:
247		return "vmlaunch";
248	case EXIT_REASON_VMPTRLD:
249		return "vmptrld";
250	case EXIT_REASON_VMPTRST:
251		return "vmptrst";
252	case EXIT_REASON_VMREAD:
253		return "vmread";
254	case EXIT_REASON_VMRESUME:
255		return "vmresume";
256	case EXIT_REASON_VMWRITE:
257		return "vmwrite";
258	case EXIT_REASON_VMXOFF:
259		return "vmxoff";
260	case EXIT_REASON_VMXON:
261		return "vmxon";
262	case EXIT_REASON_CR_ACCESS:
263		return "craccess";
264	case EXIT_REASON_DR_ACCESS:
265		return "draccess";
266	case EXIT_REASON_INOUT:
267		return "inout";
268	case EXIT_REASON_RDMSR:
269		return "rdmsr";
270	case EXIT_REASON_WRMSR:
271		return "wrmsr";
272	case EXIT_REASON_INVAL_VMCS:
273		return "invalvmcs";
274	case EXIT_REASON_INVAL_MSR:
275		return "invalmsr";
276	case EXIT_REASON_MWAIT:
277		return "mwait";
278	case EXIT_REASON_MTF:
279		return "mtf";
280	case EXIT_REASON_MONITOR:
281		return "monitor";
282	case EXIT_REASON_PAUSE:
283		return "pause";
284	case EXIT_REASON_MCE_DURING_ENTRY:
285		return "mce-during-entry";
286	case EXIT_REASON_TPR:
287		return "tpr";
288	case EXIT_REASON_APIC_ACCESS:
289		return "apic-access";
290	case EXIT_REASON_GDTR_IDTR:
291		return "gdtridtr";
292	case EXIT_REASON_LDTR_TR:
293		return "ldtrtr";
294	case EXIT_REASON_EPT_FAULT:
295		return "eptfault";
296	case EXIT_REASON_EPT_MISCONFIG:
297		return "eptmisconfig";
298	case EXIT_REASON_INVEPT:
299		return "invept";
300	case EXIT_REASON_RDTSCP:
301		return "rdtscp";
302	case EXIT_REASON_VMX_PREEMPT:
303		return "vmxpreempt";
304	case EXIT_REASON_INVVPID:
305		return "invvpid";
306	case EXIT_REASON_WBINVD:
307		return "wbinvd";
308	case EXIT_REASON_XSETBV:
309		return "xsetbv";
310	case EXIT_REASON_APIC_WRITE:
311		return "apic-write";
312	default:
313		snprintf(reasonbuf, sizeof(reasonbuf), "%d", reason);
314		return (reasonbuf);
315	}
316}
317#endif	/* KTR */
318
319static int
320vmx_allow_x2apic_msrs(struct vmx *vmx)
321{
322	int i, error;
323
324	error = 0;
325
326	/*
327	 * Allow readonly access to the following x2APIC MSRs from the guest.
328	 */
329	error += guest_msr_ro(vmx, MSR_APIC_ID);
330	error += guest_msr_ro(vmx, MSR_APIC_VERSION);
331	error += guest_msr_ro(vmx, MSR_APIC_LDR);
332	error += guest_msr_ro(vmx, MSR_APIC_SVR);
333
334	for (i = 0; i < 8; i++)
335		error += guest_msr_ro(vmx, MSR_APIC_ISR0 + i);
336
337	for (i = 0; i < 8; i++)
338		error += guest_msr_ro(vmx, MSR_APIC_TMR0 + i);
339
340	for (i = 0; i < 8; i++)
341		error += guest_msr_ro(vmx, MSR_APIC_IRR0 + i);
342
343	error += guest_msr_ro(vmx, MSR_APIC_ESR);
344	error += guest_msr_ro(vmx, MSR_APIC_LVT_TIMER);
345	error += guest_msr_ro(vmx, MSR_APIC_LVT_THERMAL);
346	error += guest_msr_ro(vmx, MSR_APIC_LVT_PCINT);
347	error += guest_msr_ro(vmx, MSR_APIC_LVT_LINT0);
348	error += guest_msr_ro(vmx, MSR_APIC_LVT_LINT1);
349	error += guest_msr_ro(vmx, MSR_APIC_LVT_ERROR);
350	error += guest_msr_ro(vmx, MSR_APIC_ICR_TIMER);
351	error += guest_msr_ro(vmx, MSR_APIC_DCR_TIMER);
352	error += guest_msr_ro(vmx, MSR_APIC_ICR);
353
354	/*
355	 * Allow TPR, EOI and SELF_IPI MSRs to be read and written by the guest.
356	 *
357	 * These registers get special treatment described in the section
358	 * "Virtualizing MSR-Based APIC Accesses".
359	 */
360	error += guest_msr_rw(vmx, MSR_APIC_TPR);
361	error += guest_msr_rw(vmx, MSR_APIC_EOI);
362	error += guest_msr_rw(vmx, MSR_APIC_SELF_IPI);
363
364	return (error);
365}
366
367u_long
368vmx_fix_cr0(u_long cr0)
369{
370
371	return ((cr0 | cr0_ones_mask) & ~cr0_zeros_mask);
372}
373
374u_long
375vmx_fix_cr4(u_long cr4)
376{
377
378	return ((cr4 | cr4_ones_mask) & ~cr4_zeros_mask);
379}
380
381static void
382vpid_free(int vpid)
383{
384	if (vpid < 0 || vpid > 0xffff)
385		panic("vpid_free: invalid vpid %d", vpid);
386
387	/*
388	 * VPIDs [0,VM_MAXCPU] are special and are not allocated from
389	 * the unit number allocator.
390	 */
391
392	if (vpid > VM_MAXCPU)
393		free_unr(vpid_unr, vpid);
394}
395
396static void
397vpid_alloc(uint16_t *vpid, int num)
398{
399	int i, x;
400
401	if (num <= 0 || num > VM_MAXCPU)
402		panic("invalid number of vpids requested: %d", num);
403
404	/*
405	 * If the "enable vpid" execution control is not enabled then the
406	 * VPID is required to be 0 for all vcpus.
407	 */
408	if ((procbased_ctls2 & PROCBASED2_ENABLE_VPID) == 0) {
409		for (i = 0; i < num; i++)
410			vpid[i] = 0;
411		return;
412	}
413
414	/*
415	 * Allocate a unique VPID for each vcpu from the unit number allocator.
416	 */
417	for (i = 0; i < num; i++) {
418		x = alloc_unr(vpid_unr);
419		if (x == -1)
420			break;
421		else
422			vpid[i] = x;
423	}
424
425	if (i < num) {
426		atomic_add_int(&vpid_alloc_failed, 1);
427
428		/*
429		 * If the unit number allocator does not have enough unique
430		 * VPIDs then we need to allocate from the [1,VM_MAXCPU] range.
431		 *
432		 * These VPIDs are not be unique across VMs but this does not
433		 * affect correctness because the combined mappings are also
434		 * tagged with the EP4TA which is unique for each VM.
435		 *
436		 * It is still sub-optimal because the invvpid will invalidate
437		 * combined mappings for a particular VPID across all EP4TAs.
438		 */
439		while (i-- > 0)
440			vpid_free(vpid[i]);
441
442		for (i = 0; i < num; i++)
443			vpid[i] = i + 1;
444	}
445}
446
447static void
448vpid_init(void)
449{
450	/*
451	 * VPID 0 is required when the "enable VPID" execution control is
452	 * disabled.
453	 *
454	 * VPIDs [1,VM_MAXCPU] are used as the "overflow namespace" when the
455	 * unit number allocator does not have sufficient unique VPIDs to
456	 * satisfy the allocation.
457	 *
458	 * The remaining VPIDs are managed by the unit number allocator.
459	 */
460	vpid_unr = new_unrhdr(VM_MAXCPU + 1, 0xffff, NULL);
461}
462
463static void
464vmx_disable(void *arg __unused)
465{
466	struct invvpid_desc invvpid_desc = { 0 };
467	struct invept_desc invept_desc = { 0 };
468
469	if (vmxon_enabled[curcpu]) {
470		/*
471		 * See sections 25.3.3.3 and 25.3.3.4 in Intel Vol 3b.
472		 *
473		 * VMXON or VMXOFF are not required to invalidate any TLB
474		 * caching structures. This prevents potential retention of
475		 * cached information in the TLB between distinct VMX episodes.
476		 */
477		invvpid(INVVPID_TYPE_ALL_CONTEXTS, invvpid_desc);
478		invept(INVEPT_TYPE_ALL_CONTEXTS, invept_desc);
479		vmxoff();
480	}
481	load_cr4(rcr4() & ~CR4_VMXE);
482}
483
484static int
485vmx_cleanup(void)
486{
487
488	if (pirvec != 0)
489		vmm_ipi_free(pirvec);
490
491	if (vpid_unr != NULL) {
492		delete_unrhdr(vpid_unr);
493		vpid_unr = NULL;
494	}
495
496	smp_rendezvous(NULL, vmx_disable, NULL, NULL);
497
498	return (0);
499}
500
501static void
502vmx_enable(void *arg __unused)
503{
504	int error;
505	uint64_t feature_control;
506
507	feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
508	if ((feature_control & IA32_FEATURE_CONTROL_LOCK) == 0 ||
509	    (feature_control & IA32_FEATURE_CONTROL_VMX_EN) == 0) {
510		wrmsr(MSR_IA32_FEATURE_CONTROL,
511		    feature_control | IA32_FEATURE_CONTROL_VMX_EN |
512		    IA32_FEATURE_CONTROL_LOCK);
513	}
514
515	load_cr4(rcr4() | CR4_VMXE);
516
517	*(uint32_t *)vmxon_region[curcpu] = vmx_revision();
518	error = vmxon(vmxon_region[curcpu]);
519	if (error == 0)
520		vmxon_enabled[curcpu] = 1;
521}
522
523static void
524vmx_restore(void)
525{
526
527	if (vmxon_enabled[curcpu])
528		vmxon(vmxon_region[curcpu]);
529}
530
531static int
532vmx_init(int ipinum)
533{
534	int error, use_tpr_shadow;
535	uint64_t basic, fixed0, fixed1, feature_control;
536	uint32_t tmp, procbased2_vid_bits;
537
538	/* CPUID.1:ECX[bit 5] must be 1 for processor to support VMX */
539	if (!(cpu_feature2 & CPUID2_VMX)) {
540		printf("vmx_init: processor does not support VMX operation\n");
541		return (ENXIO);
542	}
543
544	/*
545	 * Verify that MSR_IA32_FEATURE_CONTROL lock and VMXON enable bits
546	 * are set (bits 0 and 2 respectively).
547	 */
548	feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
549	if ((feature_control & IA32_FEATURE_CONTROL_LOCK) == 1 &&
550	    (feature_control & IA32_FEATURE_CONTROL_VMX_EN) == 0) {
551		printf("vmx_init: VMX operation disabled by BIOS\n");
552		return (ENXIO);
553	}
554
555	/*
556	 * Verify capabilities MSR_VMX_BASIC:
557	 * - bit 54 indicates support for INS/OUTS decoding
558	 */
559	basic = rdmsr(MSR_VMX_BASIC);
560	if ((basic & (1UL << 54)) == 0) {
561		printf("vmx_init: processor does not support desired basic "
562		    "capabilities\n");
563		return (EINVAL);
564	}
565
566	/* Check support for primary processor-based VM-execution controls */
567	error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
568			       MSR_VMX_TRUE_PROCBASED_CTLS,
569			       PROCBASED_CTLS_ONE_SETTING,
570			       PROCBASED_CTLS_ZERO_SETTING, &procbased_ctls);
571	if (error) {
572		printf("vmx_init: processor does not support desired primary "
573		       "processor-based controls\n");
574		return (error);
575	}
576
577	/* Clear the processor-based ctl bits that are set on demand */
578	procbased_ctls &= ~PROCBASED_CTLS_WINDOW_SETTING;
579
580	/* Check support for secondary processor-based VM-execution controls */
581	error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
582			       MSR_VMX_PROCBASED_CTLS2,
583			       PROCBASED_CTLS2_ONE_SETTING,
584			       PROCBASED_CTLS2_ZERO_SETTING, &procbased_ctls2);
585	if (error) {
586		printf("vmx_init: processor does not support desired secondary "
587		       "processor-based controls\n");
588		return (error);
589	}
590
591	/* Check support for VPID */
592	error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2,
593			       PROCBASED2_ENABLE_VPID, 0, &tmp);
594	if (error == 0)
595		procbased_ctls2 |= PROCBASED2_ENABLE_VPID;
596
597	/* Check support for pin-based VM-execution controls */
598	error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS,
599			       MSR_VMX_TRUE_PINBASED_CTLS,
600			       PINBASED_CTLS_ONE_SETTING,
601			       PINBASED_CTLS_ZERO_SETTING, &pinbased_ctls);
602	if (error) {
603		printf("vmx_init: processor does not support desired "
604		       "pin-based controls\n");
605		return (error);
606	}
607
608	/* Check support for VM-exit controls */
609	error = vmx_set_ctlreg(MSR_VMX_EXIT_CTLS, MSR_VMX_TRUE_EXIT_CTLS,
610			       VM_EXIT_CTLS_ONE_SETTING,
611			       VM_EXIT_CTLS_ZERO_SETTING,
612			       &exit_ctls);
613	if (error) {
614		printf("vmx_init: processor does not support desired "
615		    "exit controls\n");
616		return (error);
617	}
618
619	/* Check support for VM-entry controls */
620	error = vmx_set_ctlreg(MSR_VMX_ENTRY_CTLS, MSR_VMX_TRUE_ENTRY_CTLS,
621	    VM_ENTRY_CTLS_ONE_SETTING, VM_ENTRY_CTLS_ZERO_SETTING,
622	    &entry_ctls);
623	if (error) {
624		printf("vmx_init: processor does not support desired "
625		    "entry controls\n");
626		return (error);
627	}
628
629	/*
630	 * Check support for optional features by testing them
631	 * as individual bits
632	 */
633	cap_halt_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
634					MSR_VMX_TRUE_PROCBASED_CTLS,
635					PROCBASED_HLT_EXITING, 0,
636					&tmp) == 0);
637
638	cap_monitor_trap = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
639					MSR_VMX_PROCBASED_CTLS,
640					PROCBASED_MTF, 0,
641					&tmp) == 0);
642
643	cap_pause_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
644					 MSR_VMX_TRUE_PROCBASED_CTLS,
645					 PROCBASED_PAUSE_EXITING, 0,
646					 &tmp) == 0);
647
648	cap_unrestricted_guest = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
649					MSR_VMX_PROCBASED_CTLS2,
650					PROCBASED2_UNRESTRICTED_GUEST, 0,
651				        &tmp) == 0);
652
653	cap_invpcid = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2,
654	    MSR_VMX_PROCBASED_CTLS2, PROCBASED2_ENABLE_INVPCID, 0,
655	    &tmp) == 0);
656
657	/*
658	 * Check support for virtual interrupt delivery.
659	 */
660	procbased2_vid_bits = (PROCBASED2_VIRTUALIZE_APIC_ACCESSES |
661	    PROCBASED2_VIRTUALIZE_X2APIC_MODE |
662	    PROCBASED2_APIC_REGISTER_VIRTUALIZATION |
663	    PROCBASED2_VIRTUAL_INTERRUPT_DELIVERY);
664
665	use_tpr_shadow = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS,
666	    MSR_VMX_TRUE_PROCBASED_CTLS, PROCBASED_USE_TPR_SHADOW, 0,
667	    &tmp) == 0);
668
669	error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2,
670	    procbased2_vid_bits, 0, &tmp);
671	if (error == 0 && use_tpr_shadow) {
672		virtual_interrupt_delivery = 1;
673		TUNABLE_INT_FETCH("hw.vmm.vmx.use_apic_vid",
674		    &virtual_interrupt_delivery);
675	}
676
677	if (virtual_interrupt_delivery) {
678		procbased_ctls |= PROCBASED_USE_TPR_SHADOW;
679		procbased_ctls2 |= procbased2_vid_bits;
680		procbased_ctls2 &= ~PROCBASED2_VIRTUALIZE_X2APIC_MODE;
681
682		/*
683		 * No need to emulate accesses to %CR8 if virtual
684		 * interrupt delivery is enabled.
685		 */
686		procbased_ctls &= ~PROCBASED_CR8_LOAD_EXITING;
687		procbased_ctls &= ~PROCBASED_CR8_STORE_EXITING;
688
689		/*
690		 * Check for Posted Interrupts only if Virtual Interrupt
691		 * Delivery is enabled.
692		 */
693		error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS,
694		    MSR_VMX_TRUE_PINBASED_CTLS, PINBASED_POSTED_INTERRUPT, 0,
695		    &tmp);
696		if (error == 0) {
697			pirvec = vmm_ipi_alloc();
698			if (pirvec == 0) {
699				if (bootverbose) {
700					printf("vmx_init: unable to allocate "
701					    "posted interrupt vector\n");
702				}
703			} else {
704				posted_interrupts = 1;
705				TUNABLE_INT_FETCH("hw.vmm.vmx.use_apic_pir",
706				    &posted_interrupts);
707			}
708		}
709	}
710
711	if (posted_interrupts)
712		    pinbased_ctls |= PINBASED_POSTED_INTERRUPT;
713
714	/* Initialize EPT */
715	error = ept_init(ipinum);
716	if (error) {
717		printf("vmx_init: ept initialization failed (%d)\n", error);
718		return (error);
719	}
720
721	/*
722	 * Stash the cr0 and cr4 bits that must be fixed to 0 or 1
723	 */
724	fixed0 = rdmsr(MSR_VMX_CR0_FIXED0);
725	fixed1 = rdmsr(MSR_VMX_CR0_FIXED1);
726	cr0_ones_mask = fixed0 & fixed1;
727	cr0_zeros_mask = ~fixed0 & ~fixed1;
728
729	/*
730	 * CR0_PE and CR0_PG can be set to zero in VMX non-root operation
731	 * if unrestricted guest execution is allowed.
732	 */
733	if (cap_unrestricted_guest)
734		cr0_ones_mask &= ~(CR0_PG | CR0_PE);
735
736	/*
737	 * Do not allow the guest to set CR0_NW or CR0_CD.
738	 */
739	cr0_zeros_mask |= (CR0_NW | CR0_CD);
740
741	fixed0 = rdmsr(MSR_VMX_CR4_FIXED0);
742	fixed1 = rdmsr(MSR_VMX_CR4_FIXED1);
743	cr4_ones_mask = fixed0 & fixed1;
744	cr4_zeros_mask = ~fixed0 & ~fixed1;
745
746	vpid_init();
747
748	vmx_msr_init();
749
750	/* enable VMX operation */
751	smp_rendezvous(NULL, vmx_enable, NULL, NULL);
752
753	vmx_initialized = 1;
754
755	return (0);
756}
757
758static void
759vmx_trigger_hostintr(int vector)
760{
761	uintptr_t func;
762	struct gate_descriptor *gd;
763
764	gd = &idt[vector];
765
766	KASSERT(vector >= 32 && vector <= 255, ("vmx_trigger_hostintr: "
767	    "invalid vector %d", vector));
768	KASSERT(gd->gd_p == 1, ("gate descriptor for vector %d not present",
769	    vector));
770	KASSERT(gd->gd_type == SDT_SYSIGT, ("gate descriptor for vector %d "
771	    "has invalid type %d", vector, gd->gd_type));
772	KASSERT(gd->gd_dpl == SEL_KPL, ("gate descriptor for vector %d "
773	    "has invalid dpl %d", vector, gd->gd_dpl));
774	KASSERT(gd->gd_selector == GSEL(GCODE_SEL, SEL_KPL), ("gate descriptor "
775	    "for vector %d has invalid selector %d", vector, gd->gd_selector));
776	KASSERT(gd->gd_ist == 0, ("gate descriptor for vector %d has invalid "
777	    "IST %d", vector, gd->gd_ist));
778
779	func = ((long)gd->gd_hioffset << 16 | gd->gd_looffset);
780	vmx_call_isr(func);
781}
782
783static int
784vmx_setup_cr_shadow(int which, struct vmcs *vmcs, uint32_t initial)
785{
786	int error, mask_ident, shadow_ident;
787	uint64_t mask_value;
788
789	if (which != 0 && which != 4)
790		panic("vmx_setup_cr_shadow: unknown cr%d", which);
791
792	if (which == 0) {
793		mask_ident = VMCS_CR0_MASK;
794		mask_value = cr0_ones_mask | cr0_zeros_mask;
795		shadow_ident = VMCS_CR0_SHADOW;
796	} else {
797		mask_ident = VMCS_CR4_MASK;
798		mask_value = cr4_ones_mask | cr4_zeros_mask;
799		shadow_ident = VMCS_CR4_SHADOW;
800	}
801
802	error = vmcs_setreg(vmcs, 0, VMCS_IDENT(mask_ident), mask_value);
803	if (error)
804		return (error);
805
806	error = vmcs_setreg(vmcs, 0, VMCS_IDENT(shadow_ident), initial);
807	if (error)
808		return (error);
809
810	return (0);
811}
812#define	vmx_setup_cr0_shadow(vmcs,init)	vmx_setup_cr_shadow(0, (vmcs), (init))
813#define	vmx_setup_cr4_shadow(vmcs,init)	vmx_setup_cr_shadow(4, (vmcs), (init))
814
815static void *
816vmx_vminit(struct vm *vm, pmap_t pmap)
817{
818	uint16_t vpid[VM_MAXCPU];
819	int i, error;
820	struct vmx *vmx;
821	struct vmcs *vmcs;
822	uint32_t exc_bitmap;
823
824	vmx = malloc(sizeof(struct vmx), M_VMX, M_WAITOK | M_ZERO);
825	if ((uintptr_t)vmx & PAGE_MASK) {
826		panic("malloc of struct vmx not aligned on %d byte boundary",
827		      PAGE_SIZE);
828	}
829	vmx->vm = vm;
830
831	vmx->eptp = eptp(vtophys((vm_offset_t)pmap->pm_pml4));
832
833	/*
834	 * Clean up EPTP-tagged guest physical and combined mappings
835	 *
836	 * VMX transitions are not required to invalidate any guest physical
837	 * mappings. So, it may be possible for stale guest physical mappings
838	 * to be present in the processor TLBs.
839	 *
840	 * Combined mappings for this EP4TA are also invalidated for all VPIDs.
841	 */
842	ept_invalidate_mappings(vmx->eptp);
843
844	msr_bitmap_initialize(vmx->msr_bitmap);
845
846	/*
847	 * It is safe to allow direct access to MSR_GSBASE and MSR_FSBASE.
848	 * The guest FSBASE and GSBASE are saved and restored during
849	 * vm-exit and vm-entry respectively. The host FSBASE and GSBASE are
850	 * always restored from the vmcs host state area on vm-exit.
851	 *
852	 * The SYSENTER_CS/ESP/EIP MSRs are identical to FS/GSBASE in
853	 * how they are saved/restored so can be directly accessed by the
854	 * guest.
855	 *
856	 * MSR_EFER is saved and restored in the guest VMCS area on a
857	 * VM exit and entry respectively. It is also restored from the
858	 * host VMCS area on a VM exit.
859	 *
860	 * The TSC MSR is exposed read-only. Writes are disallowed as that
861	 * will impact the host TSC.
862	 * XXX Writes would be implemented with a wrmsr trap, and
863	 * then modifying the TSC offset in the VMCS.
864	 */
865	if (guest_msr_rw(vmx, MSR_GSBASE) ||
866	    guest_msr_rw(vmx, MSR_FSBASE) ||
867	    guest_msr_rw(vmx, MSR_SYSENTER_CS_MSR) ||
868	    guest_msr_rw(vmx, MSR_SYSENTER_ESP_MSR) ||
869	    guest_msr_rw(vmx, MSR_SYSENTER_EIP_MSR) ||
870	    guest_msr_rw(vmx, MSR_EFER) ||
871	    guest_msr_ro(vmx, MSR_TSC))
872		panic("vmx_vminit: error setting guest msr access");
873
874	vpid_alloc(vpid, VM_MAXCPU);
875
876	if (virtual_interrupt_delivery) {
877		error = vm_map_mmio(vm, DEFAULT_APIC_BASE, PAGE_SIZE,
878		    APIC_ACCESS_ADDRESS);
879		/* XXX this should really return an error to the caller */
880		KASSERT(error == 0, ("vm_map_mmio(apicbase) error %d", error));
881	}
882
883	for (i = 0; i < VM_MAXCPU; i++) {
884		vmcs = &vmx->vmcs[i];
885		vmcs->identifier = vmx_revision();
886		error = vmclear(vmcs);
887		if (error != 0) {
888			panic("vmx_vminit: vmclear error %d on vcpu %d\n",
889			      error, i);
890		}
891
892		vmx_msr_guest_init(vmx, i);
893
894		error = vmcs_init(vmcs);
895		KASSERT(error == 0, ("vmcs_init error %d", error));
896
897		VMPTRLD(vmcs);
898		error = 0;
899		error += vmwrite(VMCS_HOST_RSP, (u_long)&vmx->ctx[i]);
900		error += vmwrite(VMCS_EPTP, vmx->eptp);
901		error += vmwrite(VMCS_PIN_BASED_CTLS, pinbased_ctls);
902		error += vmwrite(VMCS_PRI_PROC_BASED_CTLS, procbased_ctls);
903		error += vmwrite(VMCS_SEC_PROC_BASED_CTLS, procbased_ctls2);
904		error += vmwrite(VMCS_EXIT_CTLS, exit_ctls);
905		error += vmwrite(VMCS_ENTRY_CTLS, entry_ctls);
906		error += vmwrite(VMCS_MSR_BITMAP, vtophys(vmx->msr_bitmap));
907		error += vmwrite(VMCS_VPID, vpid[i]);
908
909		/* exception bitmap */
910		if (vcpu_trace_exceptions(vm, i))
911			exc_bitmap = 0xffffffff;
912		else
913			exc_bitmap = 1 << IDT_MC;
914		error += vmwrite(VMCS_EXCEPTION_BITMAP, exc_bitmap);
915
916		if (virtual_interrupt_delivery) {
917			error += vmwrite(VMCS_APIC_ACCESS, APIC_ACCESS_ADDRESS);
918			error += vmwrite(VMCS_VIRTUAL_APIC,
919			    vtophys(&vmx->apic_page[i]));
920			error += vmwrite(VMCS_EOI_EXIT0, 0);
921			error += vmwrite(VMCS_EOI_EXIT1, 0);
922			error += vmwrite(VMCS_EOI_EXIT2, 0);
923			error += vmwrite(VMCS_EOI_EXIT3, 0);
924		}
925		if (posted_interrupts) {
926			error += vmwrite(VMCS_PIR_VECTOR, pirvec);
927			error += vmwrite(VMCS_PIR_DESC,
928			    vtophys(&vmx->pir_desc[i]));
929		}
930		VMCLEAR(vmcs);
931		KASSERT(error == 0, ("vmx_vminit: error customizing the vmcs"));
932
933		vmx->cap[i].set = 0;
934		vmx->cap[i].proc_ctls = procbased_ctls;
935		vmx->cap[i].proc_ctls2 = procbased_ctls2;
936
937		vmx->state[i].nextrip = ~0;
938		vmx->state[i].lastcpu = NOCPU;
939		vmx->state[i].vpid = vpid[i];
940
941		/*
942		 * Set up the CR0/4 shadows, and init the read shadow
943		 * to the power-on register value from the Intel Sys Arch.
944		 *  CR0 - 0x60000010
945		 *  CR4 - 0
946		 */
947		error = vmx_setup_cr0_shadow(vmcs, 0x60000010);
948		if (error != 0)
949			panic("vmx_setup_cr0_shadow %d", error);
950
951		error = vmx_setup_cr4_shadow(vmcs, 0);
952		if (error != 0)
953			panic("vmx_setup_cr4_shadow %d", error);
954
955		vmx->ctx[i].pmap = pmap;
956	}
957
958	return (vmx);
959}
960
961static int
962vmx_handle_cpuid(struct vm *vm, int vcpu, struct vmxctx *vmxctx)
963{
964	int handled, func;
965
966	func = vmxctx->guest_rax;
967
968	handled = x86_emulate_cpuid(vm, vcpu,
969				    (uint32_t*)(&vmxctx->guest_rax),
970				    (uint32_t*)(&vmxctx->guest_rbx),
971				    (uint32_t*)(&vmxctx->guest_rcx),
972				    (uint32_t*)(&vmxctx->guest_rdx));
973	return (handled);
974}
975
976static __inline void
977vmx_run_trace(struct vmx *vmx, int vcpu)
978{
979#ifdef KTR
980	VCPU_CTR1(vmx->vm, vcpu, "Resume execution at %#lx", vmcs_guest_rip());
981#endif
982}
983
984static __inline void
985vmx_exit_trace(struct vmx *vmx, int vcpu, uint64_t rip, uint32_t exit_reason,
986	       int handled)
987{
988#ifdef KTR
989	VCPU_CTR3(vmx->vm, vcpu, "%s %s vmexit at 0x%0lx",
990		 handled ? "handled" : "unhandled",
991		 exit_reason_to_str(exit_reason), rip);
992#endif
993}
994
995static __inline void
996vmx_astpending_trace(struct vmx *vmx, int vcpu, uint64_t rip)
997{
998#ifdef KTR
999	VCPU_CTR1(vmx->vm, vcpu, "astpending vmexit at 0x%0lx", rip);
1000#endif
1001}
1002
1003static VMM_STAT_INTEL(VCPU_INVVPID_SAVED, "Number of vpid invalidations saved");
1004static VMM_STAT_INTEL(VCPU_INVVPID_DONE, "Number of vpid invalidations done");
1005
1006/*
1007 * Invalidate guest mappings identified by its vpid from the TLB.
1008 */
1009static __inline void
1010vmx_invvpid(struct vmx *vmx, int vcpu, pmap_t pmap, int running)
1011{
1012	struct vmxstate *vmxstate;
1013	struct invvpid_desc invvpid_desc;
1014
1015	vmxstate = &vmx->state[vcpu];
1016	if (vmxstate->vpid == 0)
1017		return;
1018
1019	if (!running) {
1020		/*
1021		 * Set the 'lastcpu' to an invalid host cpu.
1022		 *
1023		 * This will invalidate TLB entries tagged with the vcpu's
1024		 * vpid the next time it runs via vmx_set_pcpu_defaults().
1025		 */
1026		vmxstate->lastcpu = NOCPU;
1027		return;
1028	}
1029
1030	KASSERT(curthread->td_critnest > 0, ("%s: vcpu %d running outside "
1031	    "critical section", __func__, vcpu));
1032
1033	/*
1034	 * Invalidate all mappings tagged with 'vpid'
1035	 *
1036	 * We do this because this vcpu was executing on a different host
1037	 * cpu when it last ran. We do not track whether it invalidated
1038	 * mappings associated with its 'vpid' during that run. So we must
1039	 * assume that the mappings associated with 'vpid' on 'curcpu' are
1040	 * stale and invalidate them.
1041	 *
1042	 * Note that we incur this penalty only when the scheduler chooses to
1043	 * move the thread associated with this vcpu between host cpus.
1044	 *
1045	 * Note also that this will invalidate mappings tagged with 'vpid'
1046	 * for "all" EP4TAs.
1047	 */
1048	if (pmap->pm_eptgen == vmx->eptgen[curcpu]) {
1049		invvpid_desc._res1 = 0;
1050		invvpid_desc._res2 = 0;
1051		invvpid_desc.vpid = vmxstate->vpid;
1052		invvpid_desc.linear_addr = 0;
1053		invvpid(INVVPID_TYPE_SINGLE_CONTEXT, invvpid_desc);
1054		vmm_stat_incr(vmx->vm, vcpu, VCPU_INVVPID_DONE, 1);
1055	} else {
1056		/*
1057		 * The invvpid can be skipped if an invept is going to
1058		 * be performed before entering the guest. The invept
1059		 * will invalidate combined mappings tagged with
1060		 * 'vmx->eptp' for all vpids.
1061		 */
1062		vmm_stat_incr(vmx->vm, vcpu, VCPU_INVVPID_SAVED, 1);
1063	}
1064}
1065
1066static void
1067vmx_set_pcpu_defaults(struct vmx *vmx, int vcpu, pmap_t pmap)
1068{
1069	struct vmxstate *vmxstate;
1070
1071	vmxstate = &vmx->state[vcpu];
1072	if (vmxstate->lastcpu == curcpu)
1073		return;
1074
1075	vmxstate->lastcpu = curcpu;
1076
1077	vmm_stat_incr(vmx->vm, vcpu, VCPU_MIGRATIONS, 1);
1078
1079	vmcs_write(VMCS_HOST_TR_BASE, vmm_get_host_trbase());
1080	vmcs_write(VMCS_HOST_GDTR_BASE, vmm_get_host_gdtrbase());
1081	vmcs_write(VMCS_HOST_GS_BASE, vmm_get_host_gsbase());
1082	vmx_invvpid(vmx, vcpu, pmap, 1);
1083}
1084
1085/*
1086 * We depend on 'procbased_ctls' to have the Interrupt Window Exiting bit set.
1087 */
1088CTASSERT((PROCBASED_CTLS_ONE_SETTING & PROCBASED_INT_WINDOW_EXITING) != 0);
1089
1090static void __inline
1091vmx_set_int_window_exiting(struct vmx *vmx, int vcpu)
1092{
1093
1094	if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) == 0) {
1095		vmx->cap[vcpu].proc_ctls |= PROCBASED_INT_WINDOW_EXITING;
1096		vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1097		VCPU_CTR0(vmx->vm, vcpu, "Enabling interrupt window exiting");
1098	}
1099}
1100
1101static void __inline
1102vmx_clear_int_window_exiting(struct vmx *vmx, int vcpu)
1103{
1104
1105	KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0,
1106	    ("intr_window_exiting not set: %#x", vmx->cap[vcpu].proc_ctls));
1107	vmx->cap[vcpu].proc_ctls &= ~PROCBASED_INT_WINDOW_EXITING;
1108	vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1109	VCPU_CTR0(vmx->vm, vcpu, "Disabling interrupt window exiting");
1110}
1111
1112static void __inline
1113vmx_set_nmi_window_exiting(struct vmx *vmx, int vcpu)
1114{
1115
1116	if ((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) == 0) {
1117		vmx->cap[vcpu].proc_ctls |= PROCBASED_NMI_WINDOW_EXITING;
1118		vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1119		VCPU_CTR0(vmx->vm, vcpu, "Enabling NMI window exiting");
1120	}
1121}
1122
1123static void __inline
1124vmx_clear_nmi_window_exiting(struct vmx *vmx, int vcpu)
1125{
1126
1127	KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) != 0,
1128	    ("nmi_window_exiting not set %#x", vmx->cap[vcpu].proc_ctls));
1129	vmx->cap[vcpu].proc_ctls &= ~PROCBASED_NMI_WINDOW_EXITING;
1130	vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls);
1131	VCPU_CTR0(vmx->vm, vcpu, "Disabling NMI window exiting");
1132}
1133
1134#define	NMI_BLOCKING	(VMCS_INTERRUPTIBILITY_NMI_BLOCKING |		\
1135			 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)
1136#define	HWINTR_BLOCKING	(VMCS_INTERRUPTIBILITY_STI_BLOCKING |		\
1137			 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)
1138
1139static void
1140vmx_inject_nmi(struct vmx *vmx, int vcpu)
1141{
1142	uint32_t gi, info;
1143
1144	gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1145	KASSERT((gi & NMI_BLOCKING) == 0, ("vmx_inject_nmi: invalid guest "
1146	    "interruptibility-state %#x", gi));
1147
1148	info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1149	KASSERT((info & VMCS_INTR_VALID) == 0, ("vmx_inject_nmi: invalid "
1150	    "VM-entry interruption information %#x", info));
1151
1152	/*
1153	 * Inject the virtual NMI. The vector must be the NMI IDT entry
1154	 * or the VMCS entry check will fail.
1155	 */
1156	info = IDT_NMI | VMCS_INTR_T_NMI | VMCS_INTR_VALID;
1157	vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1158
1159	VCPU_CTR0(vmx->vm, vcpu, "Injecting vNMI");
1160
1161	/* Clear the request */
1162	vm_nmi_clear(vmx->vm, vcpu);
1163}
1164
1165static void
1166vmx_inject_interrupts(struct vmx *vmx, int vcpu, struct vlapic *vlapic,
1167    uint64_t guestrip)
1168{
1169	int vector, need_nmi_exiting, extint_pending;
1170	uint64_t rflags, entryinfo;
1171	uint32_t gi, info;
1172
1173	if (vmx->state[vcpu].nextrip != guestrip) {
1174		gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1175		if (gi & HWINTR_BLOCKING) {
1176			VCPU_CTR2(vmx->vm, vcpu, "Guest interrupt blocking "
1177			    "cleared due to rip change: %#lx/%#lx",
1178			    vmx->state[vcpu].nextrip, guestrip);
1179			gi &= ~HWINTR_BLOCKING;
1180			vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1181		}
1182	}
1183
1184	if (vm_entry_intinfo(vmx->vm, vcpu, &entryinfo)) {
1185		KASSERT((entryinfo & VMCS_INTR_VALID) != 0, ("%s: entry "
1186		    "intinfo is not valid: %#lx", __func__, entryinfo));
1187
1188		info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1189		KASSERT((info & VMCS_INTR_VALID) == 0, ("%s: cannot inject "
1190		     "pending exception: %#lx/%#x", __func__, entryinfo, info));
1191
1192		info = entryinfo;
1193		vector = info & 0xff;
1194		if (vector == IDT_BP || vector == IDT_OF) {
1195			/*
1196			 * VT-x requires #BP and #OF to be injected as software
1197			 * exceptions.
1198			 */
1199			info &= ~VMCS_INTR_T_MASK;
1200			info |= VMCS_INTR_T_SWEXCEPTION;
1201		}
1202
1203		if (info & VMCS_INTR_DEL_ERRCODE)
1204			vmcs_write(VMCS_ENTRY_EXCEPTION_ERROR, entryinfo >> 32);
1205
1206		vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1207	}
1208
1209	if (vm_nmi_pending(vmx->vm, vcpu)) {
1210		/*
1211		 * If there are no conditions blocking NMI injection then
1212		 * inject it directly here otherwise enable "NMI window
1213		 * exiting" to inject it as soon as we can.
1214		 *
1215		 * We also check for STI_BLOCKING because some implementations
1216		 * don't allow NMI injection in this case. If we are running
1217		 * on a processor that doesn't have this restriction it will
1218		 * immediately exit and the NMI will be injected in the
1219		 * "NMI window exiting" handler.
1220		 */
1221		need_nmi_exiting = 1;
1222		gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1223		if ((gi & (HWINTR_BLOCKING | NMI_BLOCKING)) == 0) {
1224			info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1225			if ((info & VMCS_INTR_VALID) == 0) {
1226				vmx_inject_nmi(vmx, vcpu);
1227				need_nmi_exiting = 0;
1228			} else {
1229				VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI "
1230				    "due to VM-entry intr info %#x", info);
1231			}
1232		} else {
1233			VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI due to "
1234			    "Guest Interruptibility-state %#x", gi);
1235		}
1236
1237		if (need_nmi_exiting)
1238			vmx_set_nmi_window_exiting(vmx, vcpu);
1239	}
1240
1241	extint_pending = vm_extint_pending(vmx->vm, vcpu);
1242
1243	if (!extint_pending && virtual_interrupt_delivery) {
1244		vmx_inject_pir(vlapic);
1245		return;
1246	}
1247
1248	/*
1249	 * If interrupt-window exiting is already in effect then don't bother
1250	 * checking for pending interrupts. This is just an optimization and
1251	 * not needed for correctness.
1252	 */
1253	if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0) {
1254		VCPU_CTR0(vmx->vm, vcpu, "Skip interrupt injection due to "
1255		    "pending int_window_exiting");
1256		return;
1257	}
1258
1259	if (!extint_pending) {
1260		/* Ask the local apic for a vector to inject */
1261		if (!vlapic_pending_intr(vlapic, &vector))
1262			return;
1263
1264		/*
1265		 * From the Intel SDM, Volume 3, Section "Maskable
1266		 * Hardware Interrupts":
1267		 * - maskable interrupt vectors [16,255] can be delivered
1268		 *   through the local APIC.
1269		*/
1270		KASSERT(vector >= 16 && vector <= 255,
1271		    ("invalid vector %d from local APIC", vector));
1272	} else {
1273		/* Ask the legacy pic for a vector to inject */
1274		vatpic_pending_intr(vmx->vm, &vector);
1275
1276		/*
1277		 * From the Intel SDM, Volume 3, Section "Maskable
1278		 * Hardware Interrupts":
1279		 * - maskable interrupt vectors [0,255] can be delivered
1280		 *   through the INTR pin.
1281		 */
1282		KASSERT(vector >= 0 && vector <= 255,
1283		    ("invalid vector %d from INTR", vector));
1284	}
1285
1286	/* Check RFLAGS.IF and the interruptibility state of the guest */
1287	rflags = vmcs_read(VMCS_GUEST_RFLAGS);
1288	if ((rflags & PSL_I) == 0) {
1289		VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1290		    "rflags %#lx", vector, rflags);
1291		goto cantinject;
1292	}
1293
1294	gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1295	if (gi & HWINTR_BLOCKING) {
1296		VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1297		    "Guest Interruptibility-state %#x", vector, gi);
1298		goto cantinject;
1299	}
1300
1301	info = vmcs_read(VMCS_ENTRY_INTR_INFO);
1302	if (info & VMCS_INTR_VALID) {
1303		/*
1304		 * This is expected and could happen for multiple reasons:
1305		 * - A vectoring VM-entry was aborted due to astpending
1306		 * - A VM-exit happened during event injection.
1307		 * - An exception was injected above.
1308		 * - An NMI was injected above or after "NMI window exiting"
1309		 */
1310		VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to "
1311		    "VM-entry intr info %#x", vector, info);
1312		goto cantinject;
1313	}
1314
1315	/* Inject the interrupt */
1316	info = VMCS_INTR_T_HWINTR | VMCS_INTR_VALID;
1317	info |= vector;
1318	vmcs_write(VMCS_ENTRY_INTR_INFO, info);
1319
1320	if (!extint_pending) {
1321		/* Update the Local APIC ISR */
1322		vlapic_intr_accepted(vlapic, vector);
1323	} else {
1324		vm_extint_clear(vmx->vm, vcpu);
1325		vatpic_intr_accepted(vmx->vm, vector);
1326
1327		/*
1328		 * After we accepted the current ExtINT the PIC may
1329		 * have posted another one.  If that is the case, set
1330		 * the Interrupt Window Exiting execution control so
1331		 * we can inject that one too.
1332		 *
1333		 * Also, interrupt window exiting allows us to inject any
1334		 * pending APIC vector that was preempted by the ExtINT
1335		 * as soon as possible. This applies both for the software
1336		 * emulated vlapic and the hardware assisted virtual APIC.
1337		 */
1338		vmx_set_int_window_exiting(vmx, vcpu);
1339	}
1340
1341	VCPU_CTR1(vmx->vm, vcpu, "Injecting hwintr at vector %d", vector);
1342
1343	return;
1344
1345cantinject:
1346	/*
1347	 * Set the Interrupt Window Exiting execution control so we can inject
1348	 * the interrupt as soon as blocking condition goes away.
1349	 */
1350	vmx_set_int_window_exiting(vmx, vcpu);
1351}
1352
1353/*
1354 * If the Virtual NMIs execution control is '1' then the logical processor
1355 * tracks virtual-NMI blocking in the Guest Interruptibility-state field of
1356 * the VMCS. An IRET instruction in VMX non-root operation will remove any
1357 * virtual-NMI blocking.
1358 *
1359 * This unblocking occurs even if the IRET causes a fault. In this case the
1360 * hypervisor needs to restore virtual-NMI blocking before resuming the guest.
1361 */
1362static void
1363vmx_restore_nmi_blocking(struct vmx *vmx, int vcpuid)
1364{
1365	uint32_t gi;
1366
1367	VCPU_CTR0(vmx->vm, vcpuid, "Restore Virtual-NMI blocking");
1368	gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1369	gi |= VMCS_INTERRUPTIBILITY_NMI_BLOCKING;
1370	vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1371}
1372
1373static void
1374vmx_clear_nmi_blocking(struct vmx *vmx, int vcpuid)
1375{
1376	uint32_t gi;
1377
1378	VCPU_CTR0(vmx->vm, vcpuid, "Clear Virtual-NMI blocking");
1379	gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1380	gi &= ~VMCS_INTERRUPTIBILITY_NMI_BLOCKING;
1381	vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi);
1382}
1383
1384static void
1385vmx_assert_nmi_blocking(struct vmx *vmx, int vcpuid)
1386{
1387	uint32_t gi;
1388
1389	gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
1390	KASSERT(gi & VMCS_INTERRUPTIBILITY_NMI_BLOCKING,
1391	    ("NMI blocking is not in effect %#x", gi));
1392}
1393
1394static int
1395vmx_emulate_xsetbv(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
1396{
1397	struct vmxctx *vmxctx;
1398	uint64_t xcrval;
1399	const struct xsave_limits *limits;
1400
1401	vmxctx = &vmx->ctx[vcpu];
1402	limits = vmm_get_xsave_limits();
1403
1404	/*
1405	 * Note that the processor raises a GP# fault on its own if
1406	 * xsetbv is executed for CPL != 0, so we do not have to
1407	 * emulate that fault here.
1408	 */
1409
1410	/* Only xcr0 is supported. */
1411	if (vmxctx->guest_rcx != 0) {
1412		vm_inject_gp(vmx->vm, vcpu);
1413		return (HANDLED);
1414	}
1415
1416	/* We only handle xcr0 if both the host and guest have XSAVE enabled. */
1417	if (!limits->xsave_enabled || !(vmcs_read(VMCS_GUEST_CR4) & CR4_XSAVE)) {
1418		vm_inject_ud(vmx->vm, vcpu);
1419		return (HANDLED);
1420	}
1421
1422	xcrval = vmxctx->guest_rdx << 32 | (vmxctx->guest_rax & 0xffffffff);
1423	if ((xcrval & ~limits->xcr0_allowed) != 0) {
1424		vm_inject_gp(vmx->vm, vcpu);
1425		return (HANDLED);
1426	}
1427
1428	if (!(xcrval & XFEATURE_ENABLED_X87)) {
1429		vm_inject_gp(vmx->vm, vcpu);
1430		return (HANDLED);
1431	}
1432
1433	/* AVX (YMM_Hi128) requires SSE. */
1434	if (xcrval & XFEATURE_ENABLED_AVX &&
1435	    (xcrval & XFEATURE_AVX) != XFEATURE_AVX) {
1436		vm_inject_gp(vmx->vm, vcpu);
1437		return (HANDLED);
1438	}
1439
1440	/*
1441	 * AVX512 requires base AVX (YMM_Hi128) as well as OpMask,
1442	 * ZMM_Hi256, and Hi16_ZMM.
1443	 */
1444	if (xcrval & XFEATURE_AVX512 &&
1445	    (xcrval & (XFEATURE_AVX512 | XFEATURE_AVX)) !=
1446	    (XFEATURE_AVX512 | XFEATURE_AVX)) {
1447		vm_inject_gp(vmx->vm, vcpu);
1448		return (HANDLED);
1449	}
1450
1451	/*
1452	 * Intel MPX requires both bound register state flags to be
1453	 * set.
1454	 */
1455	if (((xcrval & XFEATURE_ENABLED_BNDREGS) != 0) !=
1456	    ((xcrval & XFEATURE_ENABLED_BNDCSR) != 0)) {
1457		vm_inject_gp(vmx->vm, vcpu);
1458		return (HANDLED);
1459	}
1460
1461	/*
1462	 * This runs "inside" vmrun() with the guest's FPU state, so
1463	 * modifying xcr0 directly modifies the guest's xcr0, not the
1464	 * host's.
1465	 */
1466	load_xcr(0, xcrval);
1467	return (HANDLED);
1468}
1469
1470static uint64_t
1471vmx_get_guest_reg(struct vmx *vmx, int vcpu, int ident)
1472{
1473	const struct vmxctx *vmxctx;
1474
1475	vmxctx = &vmx->ctx[vcpu];
1476
1477	switch (ident) {
1478	case 0:
1479		return (vmxctx->guest_rax);
1480	case 1:
1481		return (vmxctx->guest_rcx);
1482	case 2:
1483		return (vmxctx->guest_rdx);
1484	case 3:
1485		return (vmxctx->guest_rbx);
1486	case 4:
1487		return (vmcs_read(VMCS_GUEST_RSP));
1488	case 5:
1489		return (vmxctx->guest_rbp);
1490	case 6:
1491		return (vmxctx->guest_rsi);
1492	case 7:
1493		return (vmxctx->guest_rdi);
1494	case 8:
1495		return (vmxctx->guest_r8);
1496	case 9:
1497		return (vmxctx->guest_r9);
1498	case 10:
1499		return (vmxctx->guest_r10);
1500	case 11:
1501		return (vmxctx->guest_r11);
1502	case 12:
1503		return (vmxctx->guest_r12);
1504	case 13:
1505		return (vmxctx->guest_r13);
1506	case 14:
1507		return (vmxctx->guest_r14);
1508	case 15:
1509		return (vmxctx->guest_r15);
1510	default:
1511		panic("invalid vmx register %d", ident);
1512	}
1513}
1514
1515static void
1516vmx_set_guest_reg(struct vmx *vmx, int vcpu, int ident, uint64_t regval)
1517{
1518	struct vmxctx *vmxctx;
1519
1520	vmxctx = &vmx->ctx[vcpu];
1521
1522	switch (ident) {
1523	case 0:
1524		vmxctx->guest_rax = regval;
1525		break;
1526	case 1:
1527		vmxctx->guest_rcx = regval;
1528		break;
1529	case 2:
1530		vmxctx->guest_rdx = regval;
1531		break;
1532	case 3:
1533		vmxctx->guest_rbx = regval;
1534		break;
1535	case 4:
1536		vmcs_write(VMCS_GUEST_RSP, regval);
1537		break;
1538	case 5:
1539		vmxctx->guest_rbp = regval;
1540		break;
1541	case 6:
1542		vmxctx->guest_rsi = regval;
1543		break;
1544	case 7:
1545		vmxctx->guest_rdi = regval;
1546		break;
1547	case 8:
1548		vmxctx->guest_r8 = regval;
1549		break;
1550	case 9:
1551		vmxctx->guest_r9 = regval;
1552		break;
1553	case 10:
1554		vmxctx->guest_r10 = regval;
1555		break;
1556	case 11:
1557		vmxctx->guest_r11 = regval;
1558		break;
1559	case 12:
1560		vmxctx->guest_r12 = regval;
1561		break;
1562	case 13:
1563		vmxctx->guest_r13 = regval;
1564		break;
1565	case 14:
1566		vmxctx->guest_r14 = regval;
1567		break;
1568	case 15:
1569		vmxctx->guest_r15 = regval;
1570		break;
1571	default:
1572		panic("invalid vmx register %d", ident);
1573	}
1574}
1575
1576static int
1577vmx_emulate_cr0_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1578{
1579	uint64_t crval, regval;
1580
1581	/* We only handle mov to %cr0 at this time */
1582	if ((exitqual & 0xf0) != 0x00)
1583		return (UNHANDLED);
1584
1585	regval = vmx_get_guest_reg(vmx, vcpu, (exitqual >> 8) & 0xf);
1586
1587	vmcs_write(VMCS_CR0_SHADOW, regval);
1588
1589	crval = regval | cr0_ones_mask;
1590	crval &= ~cr0_zeros_mask;
1591	vmcs_write(VMCS_GUEST_CR0, crval);
1592
1593	if (regval & CR0_PG) {
1594		uint64_t efer, entry_ctls;
1595
1596		/*
1597		 * If CR0.PG is 1 and EFER.LME is 1 then EFER.LMA and
1598		 * the "IA-32e mode guest" bit in VM-entry control must be
1599		 * equal.
1600		 */
1601		efer = vmcs_read(VMCS_GUEST_IA32_EFER);
1602		if (efer & EFER_LME) {
1603			efer |= EFER_LMA;
1604			vmcs_write(VMCS_GUEST_IA32_EFER, efer);
1605			entry_ctls = vmcs_read(VMCS_ENTRY_CTLS);
1606			entry_ctls |= VM_ENTRY_GUEST_LMA;
1607			vmcs_write(VMCS_ENTRY_CTLS, entry_ctls);
1608		}
1609	}
1610
1611	return (HANDLED);
1612}
1613
1614static int
1615vmx_emulate_cr4_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1616{
1617	uint64_t crval, regval;
1618
1619	/* We only handle mov to %cr4 at this time */
1620	if ((exitqual & 0xf0) != 0x00)
1621		return (UNHANDLED);
1622
1623	regval = vmx_get_guest_reg(vmx, vcpu, (exitqual >> 8) & 0xf);
1624
1625	vmcs_write(VMCS_CR4_SHADOW, regval);
1626
1627	crval = regval | cr4_ones_mask;
1628	crval &= ~cr4_zeros_mask;
1629	vmcs_write(VMCS_GUEST_CR4, crval);
1630
1631	return (HANDLED);
1632}
1633
1634static int
1635vmx_emulate_cr8_access(struct vmx *vmx, int vcpu, uint64_t exitqual)
1636{
1637	struct vlapic *vlapic;
1638	uint64_t cr8;
1639	int regnum;
1640
1641	/* We only handle mov %cr8 to/from a register at this time. */
1642	if ((exitqual & 0xe0) != 0x00) {
1643		return (UNHANDLED);
1644	}
1645
1646	vlapic = vm_lapic(vmx->vm, vcpu);
1647	regnum = (exitqual >> 8) & 0xf;
1648	if (exitqual & 0x10) {
1649		cr8 = vlapic_get_cr8(vlapic);
1650		vmx_set_guest_reg(vmx, vcpu, regnum, cr8);
1651	} else {
1652		cr8 = vmx_get_guest_reg(vmx, vcpu, regnum);
1653		vlapic_set_cr8(vlapic, cr8);
1654	}
1655
1656	return (HANDLED);
1657}
1658
1659/*
1660 * From section "Guest Register State" in the Intel SDM: CPL = SS.DPL
1661 */
1662static int
1663vmx_cpl(void)
1664{
1665	uint32_t ssar;
1666
1667	ssar = vmcs_read(VMCS_GUEST_SS_ACCESS_RIGHTS);
1668	return ((ssar >> 5) & 0x3);
1669}
1670
1671static enum vm_cpu_mode
1672vmx_cpu_mode(void)
1673{
1674	uint32_t csar;
1675
1676	if (vmcs_read(VMCS_GUEST_IA32_EFER) & EFER_LMA) {
1677		csar = vmcs_read(VMCS_GUEST_CS_ACCESS_RIGHTS);
1678		if (csar & 0x2000)
1679			return (CPU_MODE_64BIT);	/* CS.L = 1 */
1680		else
1681			return (CPU_MODE_COMPATIBILITY);
1682	} else if (vmcs_read(VMCS_GUEST_CR0) & CR0_PE) {
1683		return (CPU_MODE_PROTECTED);
1684	} else {
1685		return (CPU_MODE_REAL);
1686	}
1687}
1688
1689static enum vm_paging_mode
1690vmx_paging_mode(void)
1691{
1692
1693	if (!(vmcs_read(VMCS_GUEST_CR0) & CR0_PG))
1694		return (PAGING_MODE_FLAT);
1695	if (!(vmcs_read(VMCS_GUEST_CR4) & CR4_PAE))
1696		return (PAGING_MODE_32);
1697	if (vmcs_read(VMCS_GUEST_IA32_EFER) & EFER_LME)
1698		return (PAGING_MODE_64);
1699	else
1700		return (PAGING_MODE_PAE);
1701}
1702
1703static uint64_t
1704inout_str_index(struct vmx *vmx, int vcpuid, int in)
1705{
1706	uint64_t val;
1707	int error;
1708	enum vm_reg_name reg;
1709
1710	reg = in ? VM_REG_GUEST_RDI : VM_REG_GUEST_RSI;
1711	error = vmx_getreg(vmx, vcpuid, reg, &val);
1712	KASSERT(error == 0, ("%s: vmx_getreg error %d", __func__, error));
1713	return (val);
1714}
1715
1716static uint64_t
1717inout_str_count(struct vmx *vmx, int vcpuid, int rep)
1718{
1719	uint64_t val;
1720	int error;
1721
1722	if (rep) {
1723		error = vmx_getreg(vmx, vcpuid, VM_REG_GUEST_RCX, &val);
1724		KASSERT(!error, ("%s: vmx_getreg error %d", __func__, error));
1725	} else {
1726		val = 1;
1727	}
1728	return (val);
1729}
1730
1731static int
1732inout_str_addrsize(uint32_t inst_info)
1733{
1734	uint32_t size;
1735
1736	size = (inst_info >> 7) & 0x7;
1737	switch (size) {
1738	case 0:
1739		return (2);	/* 16 bit */
1740	case 1:
1741		return (4);	/* 32 bit */
1742	case 2:
1743		return (8);	/* 64 bit */
1744	default:
1745		panic("%s: invalid size encoding %d", __func__, size);
1746	}
1747}
1748
1749static void
1750inout_str_seginfo(struct vmx *vmx, int vcpuid, uint32_t inst_info, int in,
1751    struct vm_inout_str *vis)
1752{
1753	int error, s;
1754
1755	if (in) {
1756		vis->seg_name = VM_REG_GUEST_ES;
1757	} else {
1758		s = (inst_info >> 15) & 0x7;
1759		vis->seg_name = vm_segment_name(s);
1760	}
1761
1762	error = vmx_getdesc(vmx, vcpuid, vis->seg_name, &vis->seg_desc);
1763	KASSERT(error == 0, ("%s: vmx_getdesc error %d", __func__, error));
1764}
1765
1766static void
1767vmx_paging_info(struct vm_guest_paging *paging)
1768{
1769	paging->cr3 = vmcs_guest_cr3();
1770	paging->cpl = vmx_cpl();
1771	paging->cpu_mode = vmx_cpu_mode();
1772	paging->paging_mode = vmx_paging_mode();
1773}
1774
1775static void
1776vmexit_inst_emul(struct vm_exit *vmexit, uint64_t gpa, uint64_t gla)
1777{
1778	struct vm_guest_paging *paging;
1779	uint32_t csar;
1780
1781	paging = &vmexit->u.inst_emul.paging;
1782
1783	vmexit->exitcode = VM_EXITCODE_INST_EMUL;
1784	vmexit->u.inst_emul.gpa = gpa;
1785	vmexit->u.inst_emul.gla = gla;
1786	vmx_paging_info(paging);
1787	switch (paging->cpu_mode) {
1788	case CPU_MODE_REAL:
1789		vmexit->u.inst_emul.cs_base = vmcs_read(VMCS_GUEST_CS_BASE);
1790		vmexit->u.inst_emul.cs_d = 0;
1791		break;
1792	case CPU_MODE_PROTECTED:
1793	case CPU_MODE_COMPATIBILITY:
1794		vmexit->u.inst_emul.cs_base = vmcs_read(VMCS_GUEST_CS_BASE);
1795		csar = vmcs_read(VMCS_GUEST_CS_ACCESS_RIGHTS);
1796		vmexit->u.inst_emul.cs_d = SEG_DESC_DEF32(csar);
1797		break;
1798	default:
1799		vmexit->u.inst_emul.cs_base = 0;
1800		vmexit->u.inst_emul.cs_d = 0;
1801		break;
1802	}
1803	vie_init(&vmexit->u.inst_emul.vie, NULL, 0);
1804}
1805
1806static int
1807ept_fault_type(uint64_t ept_qual)
1808{
1809	int fault_type;
1810
1811	if (ept_qual & EPT_VIOLATION_DATA_WRITE)
1812		fault_type = VM_PROT_WRITE;
1813	else if (ept_qual & EPT_VIOLATION_INST_FETCH)
1814		fault_type = VM_PROT_EXECUTE;
1815	else
1816		fault_type= VM_PROT_READ;
1817
1818	return (fault_type);
1819}
1820
1821static boolean_t
1822ept_emulation_fault(uint64_t ept_qual)
1823{
1824	int read, write;
1825
1826	/* EPT fault on an instruction fetch doesn't make sense here */
1827	if (ept_qual & EPT_VIOLATION_INST_FETCH)
1828		return (FALSE);
1829
1830	/* EPT fault must be a read fault or a write fault */
1831	read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0;
1832	write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0;
1833	if ((read | write) == 0)
1834		return (FALSE);
1835
1836	/*
1837	 * The EPT violation must have been caused by accessing a
1838	 * guest-physical address that is a translation of a guest-linear
1839	 * address.
1840	 */
1841	if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 ||
1842	    (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) {
1843		return (FALSE);
1844	}
1845
1846	return (TRUE);
1847}
1848
1849static __inline int
1850apic_access_virtualization(struct vmx *vmx, int vcpuid)
1851{
1852	uint32_t proc_ctls2;
1853
1854	proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
1855	return ((proc_ctls2 & PROCBASED2_VIRTUALIZE_APIC_ACCESSES) ? 1 : 0);
1856}
1857
1858static __inline int
1859x2apic_virtualization(struct vmx *vmx, int vcpuid)
1860{
1861	uint32_t proc_ctls2;
1862
1863	proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
1864	return ((proc_ctls2 & PROCBASED2_VIRTUALIZE_X2APIC_MODE) ? 1 : 0);
1865}
1866
1867static int
1868vmx_handle_apic_write(struct vmx *vmx, int vcpuid, struct vlapic *vlapic,
1869    uint64_t qual)
1870{
1871	int error, handled, offset;
1872	uint32_t *apic_regs, vector;
1873	bool retu;
1874
1875	handled = HANDLED;
1876	offset = APIC_WRITE_OFFSET(qual);
1877
1878	if (!apic_access_virtualization(vmx, vcpuid)) {
1879		/*
1880		 * In general there should not be any APIC write VM-exits
1881		 * unless APIC-access virtualization is enabled.
1882		 *
1883		 * However self-IPI virtualization can legitimately trigger
1884		 * an APIC-write VM-exit so treat it specially.
1885		 */
1886		if (x2apic_virtualization(vmx, vcpuid) &&
1887		    offset == APIC_OFFSET_SELF_IPI) {
1888			apic_regs = (uint32_t *)(vlapic->apic_page);
1889			vector = apic_regs[APIC_OFFSET_SELF_IPI / 4];
1890			vlapic_self_ipi_handler(vlapic, vector);
1891			return (HANDLED);
1892		} else
1893			return (UNHANDLED);
1894	}
1895
1896	switch (offset) {
1897	case APIC_OFFSET_ID:
1898		vlapic_id_write_handler(vlapic);
1899		break;
1900	case APIC_OFFSET_LDR:
1901		vlapic_ldr_write_handler(vlapic);
1902		break;
1903	case APIC_OFFSET_DFR:
1904		vlapic_dfr_write_handler(vlapic);
1905		break;
1906	case APIC_OFFSET_SVR:
1907		vlapic_svr_write_handler(vlapic);
1908		break;
1909	case APIC_OFFSET_ESR:
1910		vlapic_esr_write_handler(vlapic);
1911		break;
1912	case APIC_OFFSET_ICR_LOW:
1913		retu = false;
1914		error = vlapic_icrlo_write_handler(vlapic, &retu);
1915		if (error != 0 || retu)
1916			handled = UNHANDLED;
1917		break;
1918	case APIC_OFFSET_CMCI_LVT:
1919	case APIC_OFFSET_TIMER_LVT ... APIC_OFFSET_ERROR_LVT:
1920		vlapic_lvt_write_handler(vlapic, offset);
1921		break;
1922	case APIC_OFFSET_TIMER_ICR:
1923		vlapic_icrtmr_write_handler(vlapic);
1924		break;
1925	case APIC_OFFSET_TIMER_DCR:
1926		vlapic_dcr_write_handler(vlapic);
1927		break;
1928	default:
1929		handled = UNHANDLED;
1930		break;
1931	}
1932	return (handled);
1933}
1934
1935static bool
1936apic_access_fault(struct vmx *vmx, int vcpuid, uint64_t gpa)
1937{
1938
1939	if (apic_access_virtualization(vmx, vcpuid) &&
1940	    (gpa >= DEFAULT_APIC_BASE && gpa < DEFAULT_APIC_BASE + PAGE_SIZE))
1941		return (true);
1942	else
1943		return (false);
1944}
1945
1946static int
1947vmx_handle_apic_access(struct vmx *vmx, int vcpuid, struct vm_exit *vmexit)
1948{
1949	uint64_t qual;
1950	int access_type, offset, allowed;
1951
1952	if (!apic_access_virtualization(vmx, vcpuid))
1953		return (UNHANDLED);
1954
1955	qual = vmexit->u.vmx.exit_qualification;
1956	access_type = APIC_ACCESS_TYPE(qual);
1957	offset = APIC_ACCESS_OFFSET(qual);
1958
1959	allowed = 0;
1960	if (access_type == 0) {
1961		/*
1962		 * Read data access to the following registers is expected.
1963		 */
1964		switch (offset) {
1965		case APIC_OFFSET_APR:
1966		case APIC_OFFSET_PPR:
1967		case APIC_OFFSET_RRR:
1968		case APIC_OFFSET_CMCI_LVT:
1969		case APIC_OFFSET_TIMER_CCR:
1970			allowed = 1;
1971			break;
1972		default:
1973			break;
1974		}
1975	} else if (access_type == 1) {
1976		/*
1977		 * Write data access to the following registers is expected.
1978		 */
1979		switch (offset) {
1980		case APIC_OFFSET_VER:
1981		case APIC_OFFSET_APR:
1982		case APIC_OFFSET_PPR:
1983		case APIC_OFFSET_RRR:
1984		case APIC_OFFSET_ISR0 ... APIC_OFFSET_ISR7:
1985		case APIC_OFFSET_TMR0 ... APIC_OFFSET_TMR7:
1986		case APIC_OFFSET_IRR0 ... APIC_OFFSET_IRR7:
1987		case APIC_OFFSET_CMCI_LVT:
1988		case APIC_OFFSET_TIMER_CCR:
1989			allowed = 1;
1990			break;
1991		default:
1992			break;
1993		}
1994	}
1995
1996	if (allowed) {
1997		vmexit_inst_emul(vmexit, DEFAULT_APIC_BASE + offset,
1998		    VIE_INVALID_GLA);
1999	}
2000
2001	/*
2002	 * Regardless of whether the APIC-access is allowed this handler
2003	 * always returns UNHANDLED:
2004	 * - if the access is allowed then it is handled by emulating the
2005	 *   instruction that caused the VM-exit (outside the critical section)
2006	 * - if the access is not allowed then it will be converted to an
2007	 *   exitcode of VM_EXITCODE_VMX and will be dealt with in userland.
2008	 */
2009	return (UNHANDLED);
2010}
2011
2012static enum task_switch_reason
2013vmx_task_switch_reason(uint64_t qual)
2014{
2015	int reason;
2016
2017	reason = (qual >> 30) & 0x3;
2018	switch (reason) {
2019	case 0:
2020		return (TSR_CALL);
2021	case 1:
2022		return (TSR_IRET);
2023	case 2:
2024		return (TSR_JMP);
2025	case 3:
2026		return (TSR_IDT_GATE);
2027	default:
2028		panic("%s: invalid reason %d", __func__, reason);
2029	}
2030}
2031
2032static int
2033emulate_wrmsr(struct vmx *vmx, int vcpuid, u_int num, uint64_t val, bool *retu)
2034{
2035	int error;
2036
2037	if (lapic_msr(num))
2038		error = lapic_wrmsr(vmx->vm, vcpuid, num, val, retu);
2039	else
2040		error = vmx_wrmsr(vmx, vcpuid, num, val, retu);
2041
2042	return (error);
2043}
2044
2045static int
2046emulate_rdmsr(struct vmx *vmx, int vcpuid, u_int num, bool *retu)
2047{
2048	struct vmxctx *vmxctx;
2049	uint64_t result;
2050	uint32_t eax, edx;
2051	int error;
2052
2053	if (lapic_msr(num))
2054		error = lapic_rdmsr(vmx->vm, vcpuid, num, &result, retu);
2055	else
2056		error = vmx_rdmsr(vmx, vcpuid, num, &result, retu);
2057
2058	if (error == 0) {
2059		eax = result;
2060		vmxctx = &vmx->ctx[vcpuid];
2061		error = vmxctx_setreg(vmxctx, VM_REG_GUEST_RAX, eax);
2062		KASSERT(error == 0, ("vmxctx_setreg(rax) error %d", error));
2063
2064		edx = result >> 32;
2065		error = vmxctx_setreg(vmxctx, VM_REG_GUEST_RDX, edx);
2066		KASSERT(error == 0, ("vmxctx_setreg(rdx) error %d", error));
2067	}
2068
2069	return (error);
2070}
2071
2072static int
2073vmx_exit_process(struct vmx *vmx, int vcpu, struct vm_exit *vmexit)
2074{
2075	int error, errcode, errcode_valid, handled, in;
2076	struct vmxctx *vmxctx;
2077	struct vlapic *vlapic;
2078	struct vm_inout_str *vis;
2079	struct vm_task_switch *ts;
2080	uint32_t eax, ecx, edx, idtvec_info, idtvec_err, intr_info, inst_info;
2081	uint32_t intr_type, intr_vec, reason;
2082	uint64_t exitintinfo, qual, gpa;
2083	bool retu;
2084
2085	CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_VIRTUAL_NMI) != 0);
2086	CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_NMI_EXITING) != 0);
2087
2088	handled = UNHANDLED;
2089	vmxctx = &vmx->ctx[vcpu];
2090
2091	qual = vmexit->u.vmx.exit_qualification;
2092	reason = vmexit->u.vmx.exit_reason;
2093	vmexit->exitcode = VM_EXITCODE_BOGUS;
2094
2095	vmm_stat_incr(vmx->vm, vcpu, VMEXIT_COUNT, 1);
2096
2097	/*
2098	 * VM-entry failures during or after loading guest state.
2099	 *
2100	 * These VM-exits are uncommon but must be handled specially
2101	 * as most VM-exit fields are not populated as usual.
2102	 */
2103	if (__predict_false(reason == EXIT_REASON_MCE_DURING_ENTRY)) {
2104		VCPU_CTR0(vmx->vm, vcpu, "Handling MCE during VM-entry");
2105		__asm __volatile("int $18");
2106		return (1);
2107	}
2108
2109	/*
2110	 * VM exits that can be triggered during event delivery need to
2111	 * be handled specially by re-injecting the event if the IDT
2112	 * vectoring information field's valid bit is set.
2113	 *
2114	 * See "Information for VM Exits During Event Delivery" in Intel SDM
2115	 * for details.
2116	 */
2117	idtvec_info = vmcs_idt_vectoring_info();
2118	if (idtvec_info & VMCS_IDT_VEC_VALID) {
2119		idtvec_info &= ~(1 << 12); /* clear undefined bit */
2120		exitintinfo = idtvec_info;
2121		if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) {
2122			idtvec_err = vmcs_idt_vectoring_err();
2123			exitintinfo |= (uint64_t)idtvec_err << 32;
2124		}
2125		error = vm_exit_intinfo(vmx->vm, vcpu, exitintinfo);
2126		KASSERT(error == 0, ("%s: vm_set_intinfo error %d",
2127		    __func__, error));
2128
2129		/*
2130		 * If 'virtual NMIs' are being used and the VM-exit
2131		 * happened while injecting an NMI during the previous
2132		 * VM-entry, then clear "blocking by NMI" in the
2133		 * Guest Interruptibility-State so the NMI can be
2134		 * reinjected on the subsequent VM-entry.
2135		 *
2136		 * However, if the NMI was being delivered through a task
2137		 * gate, then the new task must start execution with NMIs
2138		 * blocked so don't clear NMI blocking in this case.
2139		 */
2140		intr_type = idtvec_info & VMCS_INTR_T_MASK;
2141		if (intr_type == VMCS_INTR_T_NMI) {
2142			if (reason != EXIT_REASON_TASK_SWITCH)
2143				vmx_clear_nmi_blocking(vmx, vcpu);
2144			else
2145				vmx_assert_nmi_blocking(vmx, vcpu);
2146		}
2147
2148		/*
2149		 * Update VM-entry instruction length if the event being
2150		 * delivered was a software interrupt or software exception.
2151		 */
2152		if (intr_type == VMCS_INTR_T_SWINTR ||
2153		    intr_type == VMCS_INTR_T_PRIV_SWEXCEPTION ||
2154		    intr_type == VMCS_INTR_T_SWEXCEPTION) {
2155			vmcs_write(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length);
2156		}
2157	}
2158
2159	switch (reason) {
2160	case EXIT_REASON_TASK_SWITCH:
2161		ts = &vmexit->u.task_switch;
2162		ts->tsssel = qual & 0xffff;
2163		ts->reason = vmx_task_switch_reason(qual);
2164		ts->ext = 0;
2165		ts->errcode_valid = 0;
2166		vmx_paging_info(&ts->paging);
2167		/*
2168		 * If the task switch was due to a CALL, JMP, IRET, software
2169		 * interrupt (INT n) or software exception (INT3, INTO),
2170		 * then the saved %rip references the instruction that caused
2171		 * the task switch. The instruction length field in the VMCS
2172		 * is valid in this case.
2173		 *
2174		 * In all other cases (e.g., NMI, hardware exception) the
2175		 * saved %rip is one that would have been saved in the old TSS
2176		 * had the task switch completed normally so the instruction
2177		 * length field is not needed in this case and is explicitly
2178		 * set to 0.
2179		 */
2180		if (ts->reason == TSR_IDT_GATE) {
2181			KASSERT(idtvec_info & VMCS_IDT_VEC_VALID,
2182			    ("invalid idtvec_info %#x for IDT task switch",
2183			    idtvec_info));
2184			intr_type = idtvec_info & VMCS_INTR_T_MASK;
2185			if (intr_type != VMCS_INTR_T_SWINTR &&
2186			    intr_type != VMCS_INTR_T_SWEXCEPTION &&
2187			    intr_type != VMCS_INTR_T_PRIV_SWEXCEPTION) {
2188				/* Task switch triggered by external event */
2189				ts->ext = 1;
2190				vmexit->inst_length = 0;
2191				if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) {
2192					ts->errcode_valid = 1;
2193					ts->errcode = vmcs_idt_vectoring_err();
2194				}
2195			}
2196		}
2197		vmexit->exitcode = VM_EXITCODE_TASK_SWITCH;
2198		VCPU_CTR4(vmx->vm, vcpu, "task switch reason %d, tss 0x%04x, "
2199		    "%s errcode 0x%016lx", ts->reason, ts->tsssel,
2200		    ts->ext ? "external" : "internal",
2201		    ((uint64_t)ts->errcode << 32) | ts->errcode_valid);
2202		break;
2203	case EXIT_REASON_CR_ACCESS:
2204		vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CR_ACCESS, 1);
2205		switch (qual & 0xf) {
2206		case 0:
2207			handled = vmx_emulate_cr0_access(vmx, vcpu, qual);
2208			break;
2209		case 4:
2210			handled = vmx_emulate_cr4_access(vmx, vcpu, qual);
2211			break;
2212		case 8:
2213			handled = vmx_emulate_cr8_access(vmx, vcpu, qual);
2214			break;
2215		}
2216		break;
2217	case EXIT_REASON_RDMSR:
2218		vmm_stat_incr(vmx->vm, vcpu, VMEXIT_RDMSR, 1);
2219		retu = false;
2220		ecx = vmxctx->guest_rcx;
2221		VCPU_CTR1(vmx->vm, vcpu, "rdmsr 0x%08x", ecx);
2222		error = emulate_rdmsr(vmx, vcpu, ecx, &retu);
2223		if (error) {
2224			vmexit->exitcode = VM_EXITCODE_RDMSR;
2225			vmexit->u.msr.code = ecx;
2226		} else if (!retu) {
2227			handled = HANDLED;
2228		} else {
2229			/* Return to userspace with a valid exitcode */
2230			KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
2231			    ("emulate_rdmsr retu with bogus exitcode"));
2232		}
2233		break;
2234	case EXIT_REASON_WRMSR:
2235		vmm_stat_incr(vmx->vm, vcpu, VMEXIT_WRMSR, 1);
2236		retu = false;
2237		eax = vmxctx->guest_rax;
2238		ecx = vmxctx->guest_rcx;
2239		edx = vmxctx->guest_rdx;
2240		VCPU_CTR2(vmx->vm, vcpu, "wrmsr 0x%08x value 0x%016lx",
2241		    ecx, (uint64_t)edx << 32 | eax);
2242		error = emulate_wrmsr(vmx, vcpu, ecx,
2243		    (uint64_t)edx << 32 | eax, &retu);
2244		if (error) {
2245			vmexit->exitcode = VM_EXITCODE_WRMSR;
2246			vmexit->u.msr.code = ecx;
2247			vmexit->u.msr.wval = (uint64_t)edx << 32 | eax;
2248		} else if (!retu) {
2249			handled = HANDLED;
2250		} else {
2251			/* Return to userspace with a valid exitcode */
2252			KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
2253			    ("emulate_wrmsr retu with bogus exitcode"));
2254		}
2255		break;
2256	case EXIT_REASON_HLT:
2257		vmm_stat_incr(vmx->vm, vcpu, VMEXIT_HLT, 1);
2258		vmexit->exitcode = VM_EXITCODE_HLT;
2259		vmexit->u.hlt.rflags = vmcs_read(VMCS_GUEST_RFLAGS);
2260		break;
2261	case EXIT_REASON_MTF:
2262		vmm_stat_incr(vmx->vm, vcpu, VMEXIT_MTRAP, 1);
2263		vmexit->exitcode = VM_EXITCODE_MTRAP;
2264		vmexit->inst_length = 0;
2265		break;
2266	case EXIT_REASON_PAUSE:
2267		vmm_stat_incr(vmx->vm, vcpu, VMEXIT_PAUSE, 1);
2268		vmexit->exitcode = VM_EXITCODE_PAUSE;
2269		break;
2270	case EXIT_REASON_INTR_WINDOW:
2271		vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INTR_WINDOW, 1);
2272		vmx_clear_int_window_exiting(vmx, vcpu);
2273		return (1);
2274	case EXIT_REASON_EXT_INTR:
2275		/*
2276		 * External interrupts serve only to cause VM exits and allow
2277		 * the host interrupt handler to run.
2278		 *
2279		 * If this external interrupt triggers a virtual interrupt
2280		 * to a VM, then that state will be recorded by the
2281		 * host interrupt handler in the VM's softc. We will inject
2282		 * this virtual interrupt during the subsequent VM enter.
2283		 */
2284		intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
2285
2286		/*
2287		 * XXX: Ignore this exit if VMCS_INTR_VALID is not set.
2288		 * This appears to be a bug in VMware Fusion?
2289		 */
2290		if (!(intr_info & VMCS_INTR_VALID))
2291			return (1);
2292		KASSERT((intr_info & VMCS_INTR_VALID) != 0 &&
2293		    (intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_HWINTR,
2294		    ("VM exit interruption info invalid: %#x", intr_info));
2295		vmx_trigger_hostintr(intr_info & 0xff);
2296
2297		/*
2298		 * This is special. We want to treat this as an 'handled'
2299		 * VM-exit but not increment the instruction pointer.
2300		 */
2301		vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXTINT, 1);
2302		return (1);
2303	case EXIT_REASON_NMI_WINDOW:
2304		/* Exit to allow the pending virtual NMI to be injected */
2305		if (vm_nmi_pending(vmx->vm, vcpu))
2306			vmx_inject_nmi(vmx, vcpu);
2307		vmx_clear_nmi_window_exiting(vmx, vcpu);
2308		vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NMI_WINDOW, 1);
2309		return (1);
2310	case EXIT_REASON_INOUT:
2311		vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INOUT, 1);
2312		vmexit->exitcode = VM_EXITCODE_INOUT;
2313		vmexit->u.inout.bytes = (qual & 0x7) + 1;
2314		vmexit->u.inout.in = in = (qual & 0x8) ? 1 : 0;
2315		vmexit->u.inout.string = (qual & 0x10) ? 1 : 0;
2316		vmexit->u.inout.rep = (qual & 0x20) ? 1 : 0;
2317		vmexit->u.inout.port = (uint16_t)(qual >> 16);
2318		vmexit->u.inout.eax = (uint32_t)(vmxctx->guest_rax);
2319		if (vmexit->u.inout.string) {
2320			inst_info = vmcs_read(VMCS_EXIT_INSTRUCTION_INFO);
2321			vmexit->exitcode = VM_EXITCODE_INOUT_STR;
2322			vis = &vmexit->u.inout_str;
2323			vmx_paging_info(&vis->paging);
2324			vis->rflags = vmcs_read(VMCS_GUEST_RFLAGS);
2325			vis->cr0 = vmcs_read(VMCS_GUEST_CR0);
2326			vis->index = inout_str_index(vmx, vcpu, in);
2327			vis->count = inout_str_count(vmx, vcpu, vis->inout.rep);
2328			vis->addrsize = inout_str_addrsize(inst_info);
2329			inout_str_seginfo(vmx, vcpu, inst_info, in, vis);
2330		}
2331		break;
2332	case EXIT_REASON_CPUID:
2333		vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CPUID, 1);
2334		handled = vmx_handle_cpuid(vmx->vm, vcpu, vmxctx);
2335		break;
2336	case EXIT_REASON_EXCEPTION:
2337		vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXCEPTION, 1);
2338		intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
2339		KASSERT((intr_info & VMCS_INTR_VALID) != 0,
2340		    ("VM exit interruption info invalid: %#x", intr_info));
2341
2342		intr_vec = intr_info & 0xff;
2343		intr_type = intr_info & VMCS_INTR_T_MASK;
2344
2345		/*
2346		 * If Virtual NMIs control is 1 and the VM-exit is due to a
2347		 * fault encountered during the execution of IRET then we must
2348		 * restore the state of "virtual-NMI blocking" before resuming
2349		 * the guest.
2350		 *
2351		 * See "Resuming Guest Software after Handling an Exception".
2352		 * See "Information for VM Exits Due to Vectored Events".
2353		 */
2354		if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 &&
2355		    (intr_vec != IDT_DF) &&
2356		    (intr_info & EXIT_QUAL_NMIUDTI) != 0)
2357			vmx_restore_nmi_blocking(vmx, vcpu);
2358
2359		/*
2360		 * The NMI has already been handled in vmx_exit_handle_nmi().
2361		 */
2362		if (intr_type == VMCS_INTR_T_NMI)
2363			return (1);
2364
2365		/*
2366		 * Call the machine check handler by hand. Also don't reflect
2367		 * the machine check back into the guest.
2368		 */
2369		if (intr_vec == IDT_MC) {
2370			VCPU_CTR0(vmx->vm, vcpu, "Vectoring to MCE handler");
2371			__asm __volatile("int $18");
2372			return (1);
2373		}
2374
2375		if (intr_vec == IDT_PF) {
2376			error = vmxctx_setreg(vmxctx, VM_REG_GUEST_CR2, qual);
2377			KASSERT(error == 0, ("%s: vmxctx_setreg(cr2) error %d",
2378			    __func__, error));
2379		}
2380
2381		/*
2382		 * Software exceptions exhibit trap-like behavior. This in
2383		 * turn requires populating the VM-entry instruction length
2384		 * so that the %rip in the trap frame is past the INT3/INTO
2385		 * instruction.
2386		 */
2387		if (intr_type == VMCS_INTR_T_SWEXCEPTION)
2388			vmcs_write(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length);
2389
2390		/* Reflect all other exceptions back into the guest */
2391		errcode_valid = errcode = 0;
2392		if (intr_info & VMCS_INTR_DEL_ERRCODE) {
2393			errcode_valid = 1;
2394			errcode = vmcs_read(VMCS_EXIT_INTR_ERRCODE);
2395		}
2396		VCPU_CTR2(vmx->vm, vcpu, "Reflecting exception %d/%#x into "
2397		    "the guest", intr_vec, errcode);
2398		error = vm_inject_exception(vmx->vm, vcpu, intr_vec,
2399		    errcode_valid, errcode, 0);
2400		KASSERT(error == 0, ("%s: vm_inject_exception error %d",
2401		    __func__, error));
2402		return (1);
2403
2404	case EXIT_REASON_EPT_FAULT:
2405		/*
2406		 * If 'gpa' lies within the address space allocated to
2407		 * memory then this must be a nested page fault otherwise
2408		 * this must be an instruction that accesses MMIO space.
2409		 */
2410		gpa = vmcs_gpa();
2411		if (vm_mem_allocated(vmx->vm, gpa) ||
2412		    apic_access_fault(vmx, vcpu, gpa)) {
2413			vmexit->exitcode = VM_EXITCODE_PAGING;
2414			vmexit->inst_length = 0;
2415			vmexit->u.paging.gpa = gpa;
2416			vmexit->u.paging.fault_type = ept_fault_type(qual);
2417			vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NESTED_FAULT, 1);
2418		} else if (ept_emulation_fault(qual)) {
2419			vmexit_inst_emul(vmexit, gpa, vmcs_gla());
2420			vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INST_EMUL, 1);
2421		}
2422		/*
2423		 * If Virtual NMIs control is 1 and the VM-exit is due to an
2424		 * EPT fault during the execution of IRET then we must restore
2425		 * the state of "virtual-NMI blocking" before resuming.
2426		 *
2427		 * See description of "NMI unblocking due to IRET" in
2428		 * "Exit Qualification for EPT Violations".
2429		 */
2430		if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 &&
2431		    (qual & EXIT_QUAL_NMIUDTI) != 0)
2432			vmx_restore_nmi_blocking(vmx, vcpu);
2433		break;
2434	case EXIT_REASON_VIRTUALIZED_EOI:
2435		vmexit->exitcode = VM_EXITCODE_IOAPIC_EOI;
2436		vmexit->u.ioapic_eoi.vector = qual & 0xFF;
2437		vmexit->inst_length = 0;	/* trap-like */
2438		break;
2439	case EXIT_REASON_APIC_ACCESS:
2440		handled = vmx_handle_apic_access(vmx, vcpu, vmexit);
2441		break;
2442	case EXIT_REASON_APIC_WRITE:
2443		/*
2444		 * APIC-write VM exit is trap-like so the %rip is already
2445		 * pointing to the next instruction.
2446		 */
2447		vmexit->inst_length = 0;
2448		vlapic = vm_lapic(vmx->vm, vcpu);
2449		handled = vmx_handle_apic_write(vmx, vcpu, vlapic, qual);
2450		break;
2451	case EXIT_REASON_XSETBV:
2452		handled = vmx_emulate_xsetbv(vmx, vcpu, vmexit);
2453		break;
2454	case EXIT_REASON_MONITOR:
2455		vmexit->exitcode = VM_EXITCODE_MONITOR;
2456		break;
2457	case EXIT_REASON_MWAIT:
2458		vmexit->exitcode = VM_EXITCODE_MWAIT;
2459		break;
2460	default:
2461		vmm_stat_incr(vmx->vm, vcpu, VMEXIT_UNKNOWN, 1);
2462		break;
2463	}
2464
2465	if (handled) {
2466		/*
2467		 * It is possible that control is returned to userland
2468		 * even though we were able to handle the VM exit in the
2469		 * kernel.
2470		 *
2471		 * In such a case we want to make sure that the userland
2472		 * restarts guest execution at the instruction *after*
2473		 * the one we just processed. Therefore we update the
2474		 * guest rip in the VMCS and in 'vmexit'.
2475		 */
2476		vmexit->rip += vmexit->inst_length;
2477		vmexit->inst_length = 0;
2478		vmcs_write(VMCS_GUEST_RIP, vmexit->rip);
2479	} else {
2480		if (vmexit->exitcode == VM_EXITCODE_BOGUS) {
2481			/*
2482			 * If this VM exit was not claimed by anybody then
2483			 * treat it as a generic VMX exit.
2484			 */
2485			vmexit->exitcode = VM_EXITCODE_VMX;
2486			vmexit->u.vmx.status = VM_SUCCESS;
2487			vmexit->u.vmx.inst_type = 0;
2488			vmexit->u.vmx.inst_error = 0;
2489		} else {
2490			/*
2491			 * The exitcode and collateral have been populated.
2492			 * The VM exit will be processed further in userland.
2493			 */
2494		}
2495	}
2496	return (handled);
2497}
2498
2499static __inline void
2500vmx_exit_inst_error(struct vmxctx *vmxctx, int rc, struct vm_exit *vmexit)
2501{
2502
2503	KASSERT(vmxctx->inst_fail_status != VM_SUCCESS,
2504	    ("vmx_exit_inst_error: invalid inst_fail_status %d",
2505	    vmxctx->inst_fail_status));
2506
2507	vmexit->inst_length = 0;
2508	vmexit->exitcode = VM_EXITCODE_VMX;
2509	vmexit->u.vmx.status = vmxctx->inst_fail_status;
2510	vmexit->u.vmx.inst_error = vmcs_instruction_error();
2511	vmexit->u.vmx.exit_reason = ~0;
2512	vmexit->u.vmx.exit_qualification = ~0;
2513
2514	switch (rc) {
2515	case VMX_VMRESUME_ERROR:
2516	case VMX_VMLAUNCH_ERROR:
2517	case VMX_INVEPT_ERROR:
2518		vmexit->u.vmx.inst_type = rc;
2519		break;
2520	default:
2521		panic("vm_exit_inst_error: vmx_enter_guest returned %d", rc);
2522	}
2523}
2524
2525/*
2526 * If the NMI-exiting VM execution control is set to '1' then an NMI in
2527 * non-root operation causes a VM-exit. NMI blocking is in effect so it is
2528 * sufficient to simply vector to the NMI handler via a software interrupt.
2529 * However, this must be done before maskable interrupts are enabled
2530 * otherwise the "iret" issued by an interrupt handler will incorrectly
2531 * clear NMI blocking.
2532 */
2533static __inline void
2534vmx_exit_handle_nmi(struct vmx *vmx, int vcpuid, struct vm_exit *vmexit)
2535{
2536	uint32_t intr_info;
2537
2538	KASSERT((read_rflags() & PSL_I) == 0, ("interrupts enabled"));
2539
2540	if (vmexit->u.vmx.exit_reason != EXIT_REASON_EXCEPTION)
2541		return;
2542
2543	intr_info = vmcs_read(VMCS_EXIT_INTR_INFO);
2544	KASSERT((intr_info & VMCS_INTR_VALID) != 0,
2545	    ("VM exit interruption info invalid: %#x", intr_info));
2546
2547	if ((intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_NMI) {
2548		KASSERT((intr_info & 0xff) == IDT_NMI, ("VM exit due "
2549		    "to NMI has invalid vector: %#x", intr_info));
2550		VCPU_CTR0(vmx->vm, vcpuid, "Vectoring to NMI handler");
2551		__asm __volatile("int $2");
2552	}
2553}
2554
2555static int
2556vmx_run(void *arg, int vcpu, register_t rip, pmap_t pmap,
2557    void *rendezvous_cookie, void *suspend_cookie)
2558{
2559	int rc, handled, launched;
2560	struct vmx *vmx;
2561	struct vm *vm;
2562	struct vmxctx *vmxctx;
2563	struct vmcs *vmcs;
2564	struct vm_exit *vmexit;
2565	struct vlapic *vlapic;
2566	uint32_t exit_reason;
2567
2568	vmx = arg;
2569	vm = vmx->vm;
2570	vmcs = &vmx->vmcs[vcpu];
2571	vmxctx = &vmx->ctx[vcpu];
2572	vlapic = vm_lapic(vm, vcpu);
2573	vmexit = vm_exitinfo(vm, vcpu);
2574	launched = 0;
2575
2576	KASSERT(vmxctx->pmap == pmap,
2577	    ("pmap %p different than ctx pmap %p", pmap, vmxctx->pmap));
2578
2579	vmx_msr_guest_enter(vmx, vcpu);
2580
2581	VMPTRLD(vmcs);
2582
2583	/*
2584	 * XXX
2585	 * We do this every time because we may setup the virtual machine
2586	 * from a different process than the one that actually runs it.
2587	 *
2588	 * If the life of a virtual machine was spent entirely in the context
2589	 * of a single process we could do this once in vmx_vminit().
2590	 */
2591	vmcs_write(VMCS_HOST_CR3, rcr3());
2592
2593	vmcs_write(VMCS_GUEST_RIP, rip);
2594	vmx_set_pcpu_defaults(vmx, vcpu, pmap);
2595	do {
2596		KASSERT(vmcs_guest_rip() == rip, ("%s: vmcs guest rip mismatch "
2597		    "%#lx/%#lx", __func__, vmcs_guest_rip(), rip));
2598
2599		handled = UNHANDLED;
2600		/*
2601		 * Interrupts are disabled from this point on until the
2602		 * guest starts executing. This is done for the following
2603		 * reasons:
2604		 *
2605		 * If an AST is asserted on this thread after the check below,
2606		 * then the IPI_AST notification will not be lost, because it
2607		 * will cause a VM exit due to external interrupt as soon as
2608		 * the guest state is loaded.
2609		 *
2610		 * A posted interrupt after 'vmx_inject_interrupts()' will
2611		 * not be "lost" because it will be held pending in the host
2612		 * APIC because interrupts are disabled. The pending interrupt
2613		 * will be recognized as soon as the guest state is loaded.
2614		 *
2615		 * The same reasoning applies to the IPI generated by
2616		 * pmap_invalidate_ept().
2617		 */
2618		disable_intr();
2619		vmx_inject_interrupts(vmx, vcpu, vlapic, rip);
2620
2621		/*
2622		 * Check for vcpu suspension after injecting events because
2623		 * vmx_inject_interrupts() can suspend the vcpu due to a
2624		 * triple fault.
2625		 */
2626		if (vcpu_suspended(suspend_cookie)) {
2627			enable_intr();
2628			vm_exit_suspended(vmx->vm, vcpu, rip);
2629			break;
2630		}
2631
2632		if (vcpu_rendezvous_pending(rendezvous_cookie)) {
2633			enable_intr();
2634			vm_exit_rendezvous(vmx->vm, vcpu, rip);
2635			break;
2636		}
2637
2638		if (vcpu_should_yield(vm, vcpu)) {
2639			enable_intr();
2640			vm_exit_astpending(vmx->vm, vcpu, rip);
2641			vmx_astpending_trace(vmx, vcpu, rip);
2642			handled = HANDLED;
2643			break;
2644		}
2645
2646		vmx_run_trace(vmx, vcpu);
2647		rc = vmx_enter_guest(vmxctx, vmx, launched);
2648
2649		/* Collect some information for VM exit processing */
2650		vmexit->rip = rip = vmcs_guest_rip();
2651		vmexit->inst_length = vmexit_instruction_length();
2652		vmexit->u.vmx.exit_reason = exit_reason = vmcs_exit_reason();
2653		vmexit->u.vmx.exit_qualification = vmcs_exit_qualification();
2654
2655		/* Update 'nextrip' */
2656		vmx->state[vcpu].nextrip = rip;
2657
2658		if (rc == VMX_GUEST_VMEXIT) {
2659			vmx_exit_handle_nmi(vmx, vcpu, vmexit);
2660			enable_intr();
2661			handled = vmx_exit_process(vmx, vcpu, vmexit);
2662		} else {
2663			enable_intr();
2664			vmx_exit_inst_error(vmxctx, rc, vmexit);
2665		}
2666		launched = 1;
2667		vmx_exit_trace(vmx, vcpu, rip, exit_reason, handled);
2668		rip = vmexit->rip;
2669	} while (handled);
2670
2671	/*
2672	 * If a VM exit has been handled then the exitcode must be BOGUS
2673	 * If a VM exit is not handled then the exitcode must not be BOGUS
2674	 */
2675	if ((handled && vmexit->exitcode != VM_EXITCODE_BOGUS) ||
2676	    (!handled && vmexit->exitcode == VM_EXITCODE_BOGUS)) {
2677		panic("Mismatch between handled (%d) and exitcode (%d)",
2678		      handled, vmexit->exitcode);
2679	}
2680
2681	if (!handled)
2682		vmm_stat_incr(vm, vcpu, VMEXIT_USERSPACE, 1);
2683
2684	VCPU_CTR1(vm, vcpu, "returning from vmx_run: exitcode %d",
2685	    vmexit->exitcode);
2686
2687	VMCLEAR(vmcs);
2688	vmx_msr_guest_exit(vmx, vcpu);
2689
2690	return (0);
2691}
2692
2693static void
2694vmx_vmcleanup(void *arg)
2695{
2696	int i;
2697	struct vmx *vmx = arg;
2698
2699	if (apic_access_virtualization(vmx, 0))
2700		vm_unmap_mmio(vmx->vm, DEFAULT_APIC_BASE, PAGE_SIZE);
2701
2702	for (i = 0; i < VM_MAXCPU; i++)
2703		vpid_free(vmx->state[i].vpid);
2704
2705	free(vmx, M_VMX);
2706
2707	return;
2708}
2709
2710static register_t *
2711vmxctx_regptr(struct vmxctx *vmxctx, int reg)
2712{
2713
2714	switch (reg) {
2715	case VM_REG_GUEST_RAX:
2716		return (&vmxctx->guest_rax);
2717	case VM_REG_GUEST_RBX:
2718		return (&vmxctx->guest_rbx);
2719	case VM_REG_GUEST_RCX:
2720		return (&vmxctx->guest_rcx);
2721	case VM_REG_GUEST_RDX:
2722		return (&vmxctx->guest_rdx);
2723	case VM_REG_GUEST_RSI:
2724		return (&vmxctx->guest_rsi);
2725	case VM_REG_GUEST_RDI:
2726		return (&vmxctx->guest_rdi);
2727	case VM_REG_GUEST_RBP:
2728		return (&vmxctx->guest_rbp);
2729	case VM_REG_GUEST_R8:
2730		return (&vmxctx->guest_r8);
2731	case VM_REG_GUEST_R9:
2732		return (&vmxctx->guest_r9);
2733	case VM_REG_GUEST_R10:
2734		return (&vmxctx->guest_r10);
2735	case VM_REG_GUEST_R11:
2736		return (&vmxctx->guest_r11);
2737	case VM_REG_GUEST_R12:
2738		return (&vmxctx->guest_r12);
2739	case VM_REG_GUEST_R13:
2740		return (&vmxctx->guest_r13);
2741	case VM_REG_GUEST_R14:
2742		return (&vmxctx->guest_r14);
2743	case VM_REG_GUEST_R15:
2744		return (&vmxctx->guest_r15);
2745	case VM_REG_GUEST_CR2:
2746		return (&vmxctx->guest_cr2);
2747	default:
2748		break;
2749	}
2750	return (NULL);
2751}
2752
2753static int
2754vmxctx_getreg(struct vmxctx *vmxctx, int reg, uint64_t *retval)
2755{
2756	register_t *regp;
2757
2758	if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
2759		*retval = *regp;
2760		return (0);
2761	} else
2762		return (EINVAL);
2763}
2764
2765static int
2766vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val)
2767{
2768	register_t *regp;
2769
2770	if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) {
2771		*regp = val;
2772		return (0);
2773	} else
2774		return (EINVAL);
2775}
2776
2777static int
2778vmx_get_intr_shadow(struct vmx *vmx, int vcpu, int running, uint64_t *retval)
2779{
2780	uint64_t gi;
2781	int error;
2782
2783	error = vmcs_getreg(&vmx->vmcs[vcpu], running,
2784	    VMCS_IDENT(VMCS_GUEST_INTERRUPTIBILITY), &gi);
2785	*retval = (gi & HWINTR_BLOCKING) ? 1 : 0;
2786	return (error);
2787}
2788
2789static int
2790vmx_modify_intr_shadow(struct vmx *vmx, int vcpu, int running, uint64_t val)
2791{
2792	struct vmcs *vmcs;
2793	uint64_t gi;
2794	int error, ident;
2795
2796	/*
2797	 * Forcing the vcpu into an interrupt shadow is not supported.
2798	 */
2799	if (val) {
2800		error = EINVAL;
2801		goto done;
2802	}
2803
2804	vmcs = &vmx->vmcs[vcpu];
2805	ident = VMCS_IDENT(VMCS_GUEST_INTERRUPTIBILITY);
2806	error = vmcs_getreg(vmcs, running, ident, &gi);
2807	if (error == 0) {
2808		gi &= ~HWINTR_BLOCKING;
2809		error = vmcs_setreg(vmcs, running, ident, gi);
2810	}
2811done:
2812	VCPU_CTR2(vmx->vm, vcpu, "Setting intr_shadow to %#lx %s", val,
2813	    error ? "failed" : "succeeded");
2814	return (error);
2815}
2816
2817static int
2818vmx_shadow_reg(int reg)
2819{
2820	int shreg;
2821
2822	shreg = -1;
2823
2824	switch (reg) {
2825	case VM_REG_GUEST_CR0:
2826		shreg = VMCS_CR0_SHADOW;
2827                break;
2828        case VM_REG_GUEST_CR4:
2829		shreg = VMCS_CR4_SHADOW;
2830		break;
2831	default:
2832		break;
2833	}
2834
2835	return (shreg);
2836}
2837
2838static int
2839vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval)
2840{
2841	int running, hostcpu;
2842	struct vmx *vmx = arg;
2843
2844	running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
2845	if (running && hostcpu != curcpu)
2846		panic("vmx_getreg: %s%d is running", vm_name(vmx->vm), vcpu);
2847
2848	if (reg == VM_REG_GUEST_INTR_SHADOW)
2849		return (vmx_get_intr_shadow(vmx, vcpu, running, retval));
2850
2851	if (vmxctx_getreg(&vmx->ctx[vcpu], reg, retval) == 0)
2852		return (0);
2853
2854	return (vmcs_getreg(&vmx->vmcs[vcpu], running, reg, retval));
2855}
2856
2857static int
2858vmx_setreg(void *arg, int vcpu, int reg, uint64_t val)
2859{
2860	int error, hostcpu, running, shadow;
2861	uint64_t ctls;
2862	pmap_t pmap;
2863	struct vmx *vmx = arg;
2864
2865	running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
2866	if (running && hostcpu != curcpu)
2867		panic("vmx_setreg: %s%d is running", vm_name(vmx->vm), vcpu);
2868
2869	if (reg == VM_REG_GUEST_INTR_SHADOW)
2870		return (vmx_modify_intr_shadow(vmx, vcpu, running, val));
2871
2872	if (vmxctx_setreg(&vmx->ctx[vcpu], reg, val) == 0)
2873		return (0);
2874
2875	error = vmcs_setreg(&vmx->vmcs[vcpu], running, reg, val);
2876
2877	if (error == 0) {
2878		/*
2879		 * If the "load EFER" VM-entry control is 1 then the
2880		 * value of EFER.LMA must be identical to "IA-32e mode guest"
2881		 * bit in the VM-entry control.
2882		 */
2883		if ((entry_ctls & VM_ENTRY_LOAD_EFER) != 0 &&
2884		    (reg == VM_REG_GUEST_EFER)) {
2885			vmcs_getreg(&vmx->vmcs[vcpu], running,
2886				    VMCS_IDENT(VMCS_ENTRY_CTLS), &ctls);
2887			if (val & EFER_LMA)
2888				ctls |= VM_ENTRY_GUEST_LMA;
2889			else
2890				ctls &= ~VM_ENTRY_GUEST_LMA;
2891			vmcs_setreg(&vmx->vmcs[vcpu], running,
2892				    VMCS_IDENT(VMCS_ENTRY_CTLS), ctls);
2893		}
2894
2895		shadow = vmx_shadow_reg(reg);
2896		if (shadow > 0) {
2897			/*
2898			 * Store the unmodified value in the shadow
2899			 */
2900			error = vmcs_setreg(&vmx->vmcs[vcpu], running,
2901				    VMCS_IDENT(shadow), val);
2902		}
2903
2904		if (reg == VM_REG_GUEST_CR3) {
2905			/*
2906			 * Invalidate the guest vcpu's TLB mappings to emulate
2907			 * the behavior of updating %cr3.
2908			 *
2909			 * XXX the processor retains global mappings when %cr3
2910			 * is updated but vmx_invvpid() does not.
2911			 */
2912			pmap = vmx->ctx[vcpu].pmap;
2913			vmx_invvpid(vmx, vcpu, pmap, running);
2914		}
2915	}
2916
2917	return (error);
2918}
2919
2920static int
2921vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
2922{
2923	int hostcpu, running;
2924	struct vmx *vmx = arg;
2925
2926	running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
2927	if (running && hostcpu != curcpu)
2928		panic("vmx_getdesc: %s%d is running", vm_name(vmx->vm), vcpu);
2929
2930	return (vmcs_getdesc(&vmx->vmcs[vcpu], running, reg, desc));
2931}
2932
2933static int
2934vmx_setdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
2935{
2936	int hostcpu, running;
2937	struct vmx *vmx = arg;
2938
2939	running = vcpu_is_running(vmx->vm, vcpu, &hostcpu);
2940	if (running && hostcpu != curcpu)
2941		panic("vmx_setdesc: %s%d is running", vm_name(vmx->vm), vcpu);
2942
2943	return (vmcs_setdesc(&vmx->vmcs[vcpu], running, reg, desc));
2944}
2945
2946static int
2947vmx_getcap(void *arg, int vcpu, int type, int *retval)
2948{
2949	struct vmx *vmx = arg;
2950	int vcap;
2951	int ret;
2952
2953	ret = ENOENT;
2954
2955	vcap = vmx->cap[vcpu].set;
2956
2957	switch (type) {
2958	case VM_CAP_HALT_EXIT:
2959		if (cap_halt_exit)
2960			ret = 0;
2961		break;
2962	case VM_CAP_PAUSE_EXIT:
2963		if (cap_pause_exit)
2964			ret = 0;
2965		break;
2966	case VM_CAP_MTRAP_EXIT:
2967		if (cap_monitor_trap)
2968			ret = 0;
2969		break;
2970	case VM_CAP_UNRESTRICTED_GUEST:
2971		if (cap_unrestricted_guest)
2972			ret = 0;
2973		break;
2974	case VM_CAP_ENABLE_INVPCID:
2975		if (cap_invpcid)
2976			ret = 0;
2977		break;
2978	default:
2979		break;
2980	}
2981
2982	if (ret == 0)
2983		*retval = (vcap & (1 << type)) ? 1 : 0;
2984
2985	return (ret);
2986}
2987
2988static int
2989vmx_setcap(void *arg, int vcpu, int type, int val)
2990{
2991	struct vmx *vmx = arg;
2992	struct vmcs *vmcs = &vmx->vmcs[vcpu];
2993	uint32_t baseval;
2994	uint32_t *pptr;
2995	int error;
2996	int flag;
2997	int reg;
2998	int retval;
2999
3000	retval = ENOENT;
3001	pptr = NULL;
3002
3003	switch (type) {
3004	case VM_CAP_HALT_EXIT:
3005		if (cap_halt_exit) {
3006			retval = 0;
3007			pptr = &vmx->cap[vcpu].proc_ctls;
3008			baseval = *pptr;
3009			flag = PROCBASED_HLT_EXITING;
3010			reg = VMCS_PRI_PROC_BASED_CTLS;
3011		}
3012		break;
3013	case VM_CAP_MTRAP_EXIT:
3014		if (cap_monitor_trap) {
3015			retval = 0;
3016			pptr = &vmx->cap[vcpu].proc_ctls;
3017			baseval = *pptr;
3018			flag = PROCBASED_MTF;
3019			reg = VMCS_PRI_PROC_BASED_CTLS;
3020		}
3021		break;
3022	case VM_CAP_PAUSE_EXIT:
3023		if (cap_pause_exit) {
3024			retval = 0;
3025			pptr = &vmx->cap[vcpu].proc_ctls;
3026			baseval = *pptr;
3027			flag = PROCBASED_PAUSE_EXITING;
3028			reg = VMCS_PRI_PROC_BASED_CTLS;
3029		}
3030		break;
3031	case VM_CAP_UNRESTRICTED_GUEST:
3032		if (cap_unrestricted_guest) {
3033			retval = 0;
3034			pptr = &vmx->cap[vcpu].proc_ctls2;
3035			baseval = *pptr;
3036			flag = PROCBASED2_UNRESTRICTED_GUEST;
3037			reg = VMCS_SEC_PROC_BASED_CTLS;
3038		}
3039		break;
3040	case VM_CAP_ENABLE_INVPCID:
3041		if (cap_invpcid) {
3042			retval = 0;
3043			pptr = &vmx->cap[vcpu].proc_ctls2;
3044			baseval = *pptr;
3045			flag = PROCBASED2_ENABLE_INVPCID;
3046			reg = VMCS_SEC_PROC_BASED_CTLS;
3047		}
3048		break;
3049	default:
3050		break;
3051	}
3052
3053	if (retval == 0) {
3054		if (val) {
3055			baseval |= flag;
3056		} else {
3057			baseval &= ~flag;
3058		}
3059		VMPTRLD(vmcs);
3060		error = vmwrite(reg, baseval);
3061		VMCLEAR(vmcs);
3062
3063		if (error) {
3064			retval = error;
3065		} else {
3066			/*
3067			 * Update optional stored flags, and record
3068			 * setting
3069			 */
3070			if (pptr != NULL) {
3071				*pptr = baseval;
3072			}
3073
3074			if (val) {
3075				vmx->cap[vcpu].set |= (1 << type);
3076			} else {
3077				vmx->cap[vcpu].set &= ~(1 << type);
3078			}
3079		}
3080	}
3081
3082        return (retval);
3083}
3084
3085struct vlapic_vtx {
3086	struct vlapic	vlapic;
3087	struct pir_desc	*pir_desc;
3088	struct vmx	*vmx;
3089};
3090
3091#define	VMX_CTR_PIR(vm, vcpuid, pir_desc, notify, vector, level, msg)	\
3092do {									\
3093	VCPU_CTR2(vm, vcpuid, msg " assert %s-triggered vector %d",	\
3094	    level ? "level" : "edge", vector);				\
3095	VCPU_CTR1(vm, vcpuid, msg " pir0 0x%016lx", pir_desc->pir[0]);	\
3096	VCPU_CTR1(vm, vcpuid, msg " pir1 0x%016lx", pir_desc->pir[1]);	\
3097	VCPU_CTR1(vm, vcpuid, msg " pir2 0x%016lx", pir_desc->pir[2]);	\
3098	VCPU_CTR1(vm, vcpuid, msg " pir3 0x%016lx", pir_desc->pir[3]);	\
3099	VCPU_CTR1(vm, vcpuid, msg " notify: %s", notify ? "yes" : "no");\
3100} while (0)
3101
3102/*
3103 * vlapic->ops handlers that utilize the APICv hardware assist described in
3104 * Chapter 29 of the Intel SDM.
3105 */
3106static int
3107vmx_set_intr_ready(struct vlapic *vlapic, int vector, bool level)
3108{
3109	struct vlapic_vtx *vlapic_vtx;
3110	struct pir_desc *pir_desc;
3111	uint64_t mask;
3112	int idx, notify;
3113
3114	vlapic_vtx = (struct vlapic_vtx *)vlapic;
3115	pir_desc = vlapic_vtx->pir_desc;
3116
3117	/*
3118	 * Keep track of interrupt requests in the PIR descriptor. This is
3119	 * because the virtual APIC page pointed to by the VMCS cannot be
3120	 * modified if the vcpu is running.
3121	 */
3122	idx = vector / 64;
3123	mask = 1UL << (vector % 64);
3124	atomic_set_long(&pir_desc->pir[idx], mask);
3125	notify = atomic_cmpset_long(&pir_desc->pending, 0, 1);
3126
3127	VMX_CTR_PIR(vlapic->vm, vlapic->vcpuid, pir_desc, notify, vector,
3128	    level, "vmx_set_intr_ready");
3129	return (notify);
3130}
3131
3132static int
3133vmx_pending_intr(struct vlapic *vlapic, int *vecptr)
3134{
3135	struct vlapic_vtx *vlapic_vtx;
3136	struct pir_desc *pir_desc;
3137	struct LAPIC *lapic;
3138	uint64_t pending, pirval;
3139	uint32_t ppr, vpr;
3140	int i;
3141
3142	/*
3143	 * This function is only expected to be called from the 'HLT' exit
3144	 * handler which does not care about the vector that is pending.
3145	 */
3146	KASSERT(vecptr == NULL, ("vmx_pending_intr: vecptr must be NULL"));
3147
3148	vlapic_vtx = (struct vlapic_vtx *)vlapic;
3149	pir_desc = vlapic_vtx->pir_desc;
3150
3151	pending = atomic_load_acq_long(&pir_desc->pending);
3152	if (!pending)
3153		return (0);	/* common case */
3154
3155	/*
3156	 * If there is an interrupt pending then it will be recognized only
3157	 * if its priority is greater than the processor priority.
3158	 *
3159	 * Special case: if the processor priority is zero then any pending
3160	 * interrupt will be recognized.
3161	 */
3162	lapic = vlapic->apic_page;
3163	ppr = lapic->ppr & 0xf0;
3164	if (ppr == 0)
3165		return (1);
3166
3167	VCPU_CTR1(vlapic->vm, vlapic->vcpuid, "HLT with non-zero PPR %d",
3168	    lapic->ppr);
3169
3170	for (i = 3; i >= 0; i--) {
3171		pirval = pir_desc->pir[i];
3172		if (pirval != 0) {
3173			vpr = (i * 64 + flsl(pirval) - 1) & 0xf0;
3174			return (vpr > ppr);
3175		}
3176	}
3177	return (0);
3178}
3179
3180static void
3181vmx_intr_accepted(struct vlapic *vlapic, int vector)
3182{
3183
3184	panic("vmx_intr_accepted: not expected to be called");
3185}
3186
3187static void
3188vmx_set_tmr(struct vlapic *vlapic, int vector, bool level)
3189{
3190	struct vlapic_vtx *vlapic_vtx;
3191	struct vmx *vmx;
3192	struct vmcs *vmcs;
3193	uint64_t mask, val;
3194
3195	KASSERT(vector >= 0 && vector <= 255, ("invalid vector %d", vector));
3196	KASSERT(!vcpu_is_running(vlapic->vm, vlapic->vcpuid, NULL),
3197	    ("vmx_set_tmr: vcpu cannot be running"));
3198
3199	vlapic_vtx = (struct vlapic_vtx *)vlapic;
3200	vmx = vlapic_vtx->vmx;
3201	vmcs = &vmx->vmcs[vlapic->vcpuid];
3202	mask = 1UL << (vector % 64);
3203
3204	VMPTRLD(vmcs);
3205	val = vmcs_read(VMCS_EOI_EXIT(vector));
3206	if (level)
3207		val |= mask;
3208	else
3209		val &= ~mask;
3210	vmcs_write(VMCS_EOI_EXIT(vector), val);
3211	VMCLEAR(vmcs);
3212}
3213
3214static void
3215vmx_enable_x2apic_mode(struct vlapic *vlapic)
3216{
3217	struct vmx *vmx;
3218	struct vmcs *vmcs;
3219	uint32_t proc_ctls2;
3220	int vcpuid, error;
3221
3222	vcpuid = vlapic->vcpuid;
3223	vmx = ((struct vlapic_vtx *)vlapic)->vmx;
3224	vmcs = &vmx->vmcs[vcpuid];
3225
3226	proc_ctls2 = vmx->cap[vcpuid].proc_ctls2;
3227	KASSERT((proc_ctls2 & PROCBASED2_VIRTUALIZE_APIC_ACCESSES) != 0,
3228	    ("%s: invalid proc_ctls2 %#x", __func__, proc_ctls2));
3229
3230	proc_ctls2 &= ~PROCBASED2_VIRTUALIZE_APIC_ACCESSES;
3231	proc_ctls2 |= PROCBASED2_VIRTUALIZE_X2APIC_MODE;
3232	vmx->cap[vcpuid].proc_ctls2 = proc_ctls2;
3233
3234	VMPTRLD(vmcs);
3235	vmcs_write(VMCS_SEC_PROC_BASED_CTLS, proc_ctls2);
3236	VMCLEAR(vmcs);
3237
3238	if (vlapic->vcpuid == 0) {
3239		/*
3240		 * The nested page table mappings are shared by all vcpus
3241		 * so unmap the APIC access page just once.
3242		 */
3243		error = vm_unmap_mmio(vmx->vm, DEFAULT_APIC_BASE, PAGE_SIZE);
3244		KASSERT(error == 0, ("%s: vm_unmap_mmio error %d",
3245		    __func__, error));
3246
3247		/*
3248		 * The MSR bitmap is shared by all vcpus so modify it only
3249		 * once in the context of vcpu 0.
3250		 */
3251		error = vmx_allow_x2apic_msrs(vmx);
3252		KASSERT(error == 0, ("%s: vmx_allow_x2apic_msrs error %d",
3253		    __func__, error));
3254	}
3255}
3256
3257static void
3258vmx_post_intr(struct vlapic *vlapic, int hostcpu)
3259{
3260
3261	ipi_cpu(hostcpu, pirvec);
3262}
3263
3264/*
3265 * Transfer the pending interrupts in the PIR descriptor to the IRR
3266 * in the virtual APIC page.
3267 */
3268static void
3269vmx_inject_pir(struct vlapic *vlapic)
3270{
3271	struct vlapic_vtx *vlapic_vtx;
3272	struct pir_desc *pir_desc;
3273	struct LAPIC *lapic;
3274	uint64_t val, pirval;
3275	int rvi, pirbase = -1;
3276	uint16_t intr_status_old, intr_status_new;
3277
3278	vlapic_vtx = (struct vlapic_vtx *)vlapic;
3279	pir_desc = vlapic_vtx->pir_desc;
3280	if (atomic_cmpset_long(&pir_desc->pending, 1, 0) == 0) {
3281		VCPU_CTR0(vlapic->vm, vlapic->vcpuid, "vmx_inject_pir: "
3282		    "no posted interrupt pending");
3283		return;
3284	}
3285
3286	pirval = 0;
3287	pirbase = -1;
3288	lapic = vlapic->apic_page;
3289
3290	val = atomic_readandclear_long(&pir_desc->pir[0]);
3291	if (val != 0) {
3292		lapic->irr0 |= val;
3293		lapic->irr1 |= val >> 32;
3294		pirbase = 0;
3295		pirval = val;
3296	}
3297
3298	val = atomic_readandclear_long(&pir_desc->pir[1]);
3299	if (val != 0) {
3300		lapic->irr2 |= val;
3301		lapic->irr3 |= val >> 32;
3302		pirbase = 64;
3303		pirval = val;
3304	}
3305
3306	val = atomic_readandclear_long(&pir_desc->pir[2]);
3307	if (val != 0) {
3308		lapic->irr4 |= val;
3309		lapic->irr5 |= val >> 32;
3310		pirbase = 128;
3311		pirval = val;
3312	}
3313
3314	val = atomic_readandclear_long(&pir_desc->pir[3]);
3315	if (val != 0) {
3316		lapic->irr6 |= val;
3317		lapic->irr7 |= val >> 32;
3318		pirbase = 192;
3319		pirval = val;
3320	}
3321
3322	VLAPIC_CTR_IRR(vlapic, "vmx_inject_pir");
3323
3324	/*
3325	 * Update RVI so the processor can evaluate pending virtual
3326	 * interrupts on VM-entry.
3327	 *
3328	 * It is possible for pirval to be 0 here, even though the
3329	 * pending bit has been set. The scenario is:
3330	 * CPU-Y is sending a posted interrupt to CPU-X, which
3331	 * is running a guest and processing posted interrupts in h/w.
3332	 * CPU-X will eventually exit and the state seen in s/w is
3333	 * the pending bit set, but no PIR bits set.
3334	 *
3335	 *      CPU-X                      CPU-Y
3336	 *   (vm running)                (host running)
3337	 *   rx posted interrupt
3338	 *   CLEAR pending bit
3339	 *				 SET PIR bit
3340	 *   READ/CLEAR PIR bits
3341	 *				 SET pending bit
3342	 *   (vm exit)
3343	 *   pending bit set, PIR 0
3344	 */
3345	if (pirval != 0) {
3346		rvi = pirbase + flsl(pirval) - 1;
3347		intr_status_old = vmcs_read(VMCS_GUEST_INTR_STATUS);
3348		intr_status_new = (intr_status_old & 0xFF00) | rvi;
3349		if (intr_status_new > intr_status_old) {
3350			vmcs_write(VMCS_GUEST_INTR_STATUS, intr_status_new);
3351			VCPU_CTR2(vlapic->vm, vlapic->vcpuid, "vmx_inject_pir: "
3352			    "guest_intr_status changed from 0x%04x to 0x%04x",
3353			    intr_status_old, intr_status_new);
3354		}
3355	}
3356}
3357
3358static struct vlapic *
3359vmx_vlapic_init(void *arg, int vcpuid)
3360{
3361	struct vmx *vmx;
3362	struct vlapic *vlapic;
3363	struct vlapic_vtx *vlapic_vtx;
3364
3365	vmx = arg;
3366
3367	vlapic = malloc(sizeof(struct vlapic_vtx), M_VLAPIC, M_WAITOK | M_ZERO);
3368	vlapic->vm = vmx->vm;
3369	vlapic->vcpuid = vcpuid;
3370	vlapic->apic_page = (struct LAPIC *)&vmx->apic_page[vcpuid];
3371
3372	vlapic_vtx = (struct vlapic_vtx *)vlapic;
3373	vlapic_vtx->pir_desc = &vmx->pir_desc[vcpuid];
3374	vlapic_vtx->vmx = vmx;
3375
3376	if (virtual_interrupt_delivery) {
3377		vlapic->ops.set_intr_ready = vmx_set_intr_ready;
3378		vlapic->ops.pending_intr = vmx_pending_intr;
3379		vlapic->ops.intr_accepted = vmx_intr_accepted;
3380		vlapic->ops.set_tmr = vmx_set_tmr;
3381		vlapic->ops.enable_x2apic_mode = vmx_enable_x2apic_mode;
3382	}
3383
3384	if (posted_interrupts)
3385		vlapic->ops.post_intr = vmx_post_intr;
3386
3387	vlapic_init(vlapic);
3388
3389	return (vlapic);
3390}
3391
3392static void
3393vmx_vlapic_cleanup(void *arg, struct vlapic *vlapic)
3394{
3395
3396	vlapic_cleanup(vlapic);
3397	free(vlapic, M_VLAPIC);
3398}
3399
3400struct vmm_ops vmm_ops_intel = {
3401	vmx_init,
3402	vmx_cleanup,
3403	vmx_restore,
3404	vmx_vminit,
3405	vmx_run,
3406	vmx_vmcleanup,
3407	vmx_getreg,
3408	vmx_setreg,
3409	vmx_getdesc,
3410	vmx_setdesc,
3411	vmx_getcap,
3412	vmx_setcap,
3413	ept_vmspace_alloc,
3414	ept_vmspace_free,
3415	vmx_vlapic_init,
3416	vmx_vlapic_cleanup,
3417};
3418