svm.c revision 271912
1/*-
2 * Copyright (c) 2013, Anish Gupta (akgupt3@gmail.com)
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 unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: projects/bhyve_svm/sys/amd64/vmm/amd/svm.c 271912 2014-09-20 21:46:31Z neel $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/smp.h>
33#include <sys/kernel.h>
34#include <sys/malloc.h>
35#include <sys/pcpu.h>
36#include <sys/proc.h>
37#include <sys/sysctl.h>
38
39#include <vm/vm.h>
40#include <vm/pmap.h>
41
42#include <machine/cpufunc.h>
43#include <machine/psl.h>
44#include <machine/pmap.h>
45#include <machine/md_var.h>
46#include <machine/vmparam.h>
47#include <machine/specialreg.h>
48#include <machine/segments.h>
49#include <machine/smp.h>
50#include <machine/vmm.h>
51#include <machine/vmm_dev.h>
52#include <machine/vmm_instruction_emul.h>
53
54#include <x86/apicreg.h>
55
56#include "vmm_lapic.h"
57#include "vmm_stat.h"
58#include "vmm_ktr.h"
59#include "vmm_ioport.h"
60#include "vatpic.h"
61#include "vlapic.h"
62#include "vlapic_priv.h"
63
64#include "x86.h"
65#include "vmcb.h"
66#include "svm.h"
67#include "svm_softc.h"
68#include "svm_msr.h"
69#include "npt.h"
70
71SYSCTL_DECL(_hw_vmm);
72SYSCTL_NODE(_hw_vmm, OID_AUTO, svm, CTLFLAG_RW, NULL, NULL);
73
74/*
75 * SVM CPUID function 0x8000_000A, edx bit decoding.
76 */
77#define AMD_CPUID_SVM_NP		BIT(0)  /* Nested paging or RVI */
78#define AMD_CPUID_SVM_LBR		BIT(1)  /* Last branch virtualization */
79#define AMD_CPUID_SVM_SVML		BIT(2)  /* SVM lock */
80#define AMD_CPUID_SVM_NRIP_SAVE		BIT(3)  /* Next RIP is saved */
81#define AMD_CPUID_SVM_TSC_RATE		BIT(4)  /* TSC rate control. */
82#define AMD_CPUID_SVM_VMCB_CLEAN	BIT(5)  /* VMCB state caching */
83#define AMD_CPUID_SVM_FLUSH_BY_ASID	BIT(6)  /* Flush by ASID */
84#define AMD_CPUID_SVM_DECODE_ASSIST	BIT(7)  /* Decode assist */
85#define AMD_CPUID_SVM_PAUSE_INC		BIT(10) /* Pause intercept filter. */
86#define AMD_CPUID_SVM_PAUSE_FTH		BIT(12) /* Pause filter threshold */
87
88#define	VMCB_CACHE_DEFAULT	(VMCB_CACHE_ASID 	|	\
89				VMCB_CACHE_IOPM		|	\
90				VMCB_CACHE_I		|	\
91				VMCB_CACHE_TPR		|	\
92				VMCB_CACHE_NP)
93
94MALLOC_DEFINE(M_SVM, "svm", "svm");
95MALLOC_DEFINE(M_SVM_VLAPIC, "svm-vlapic", "svm-vlapic");
96
97/* Per-CPU context area. */
98extern struct pcpu __pcpu[];
99
100static int svm_getdesc(void *arg, int vcpu, int type, struct seg_desc *desc);
101
102static uint32_t svm_feature;	/* AMD SVM features. */
103SYSCTL_UINT(_hw_vmm_svm, OID_AUTO, features, CTLFLAG_RD, &svm_feature, 0,
104    "SVM features advertised by CPUID.8000000AH:EDX");
105
106static int disable_npf_assist;
107SYSCTL_INT(_hw_vmm_svm, OID_AUTO, disable_npf_assist, CTLFLAG_RWTUN,
108    &disable_npf_assist, 0, NULL);
109
110/* Maximum ASIDs supported by the processor */
111static uint32_t nasid;
112SYSCTL_UINT(_hw_vmm_svm, OID_AUTO, num_asids, CTLFLAG_RD, &nasid, 0,
113    "Number of ASIDs supported by this processor");
114
115/* Current ASID generation for each host cpu */
116static struct asid asid[MAXCPU];
117
118/*
119 * SVM host state saved area of size 4KB for each core.
120 */
121static uint8_t hsave[MAXCPU][PAGE_SIZE] __aligned(PAGE_SIZE);
122
123/*
124 * S/w saved host context.
125 */
126static struct svm_regctx host_ctx[MAXCPU];
127
128static VMM_STAT_AMD(VCPU_EXITINTINFO, "VM exits during event delivery");
129static VMM_STAT_AMD(VCPU_INTINFO_INJECTED, "Events pending at VM entry");
130static VMM_STAT_AMD(VMEXIT_VINTR, "VM exits due to interrupt window");
131
132/*
133 * Common function to enable or disabled SVM for a CPU.
134 */
135static int
136cpu_svm_enable_disable(boolean_t enable)
137{
138	uint64_t efer_msr;
139
140	efer_msr = rdmsr(MSR_EFER);
141
142	if (enable)
143		efer_msr |= EFER_SVM;
144	else
145		efer_msr &= ~EFER_SVM;
146
147	wrmsr(MSR_EFER, efer_msr);
148
149	return(0);
150}
151
152/*
153 * Disable SVM on a CPU.
154 */
155static void
156svm_disable(void *arg __unused)
157{
158
159	(void)cpu_svm_enable_disable(FALSE);
160}
161
162/*
163 * Disable SVM for all CPUs.
164 */
165static int
166svm_cleanup(void)
167{
168
169	smp_rendezvous(NULL, svm_disable, NULL, NULL);
170	return (0);
171}
172
173/*
174 * Check for required BHyVe SVM features in a CPU.
175 */
176static int
177svm_cpuid_features(void)
178{
179	u_int regs[4];
180
181	/* CPUID Fn8000_000A is for SVM */
182	do_cpuid(0x8000000A, regs);
183	svm_feature = regs[3];
184
185	printf("SVM rev: 0x%x NASID:0x%x\n", regs[0] & 0xFF, regs[1]);
186	nasid = regs[1];
187	KASSERT(nasid > 1, ("Insufficient ASIDs for guests: %#x", nasid));
188
189	printf("SVM Features:0x%b\n", svm_feature,
190		"\020"
191		"\001NP"		/* Nested paging */
192		"\002LbrVirt"		/* LBR virtualization */
193		"\003SVML"		/* SVM lock */
194		"\004NRIPS"		/* NRIP save */
195		"\005TscRateMsr"	/* MSR based TSC rate control */
196		"\006VmcbClean"		/* VMCB clean bits */
197		"\007FlushByAsid"	/* Flush by ASID */
198		"\010DecodeAssist"	/* Decode assist */
199		"\011<b20>"
200		"\012<b20>"
201		"\013PauseFilter"
202		"\014<b20>"
203		"\015PauseFilterThreshold"
204		"\016AVIC"
205		);
206
207	/* SVM Lock */
208	if (!(svm_feature & AMD_CPUID_SVM_SVML)) {
209		printf("SVM is disabled by BIOS, please enable in BIOS.\n");
210		return (ENXIO);
211	}
212
213	/*
214	 * bhyve need RVI to work.
215	 */
216	if (!(svm_feature & AMD_CPUID_SVM_NP)) {
217		printf("Missing Nested paging or RVI SVM support in processor.\n");
218		return (EIO);
219	}
220
221	if (svm_feature & AMD_CPUID_SVM_NRIP_SAVE)
222		return (0);
223
224	return (EIO);
225}
226
227static __inline int
228flush_by_asid(void)
229{
230
231	return (svm_feature & AMD_CPUID_SVM_FLUSH_BY_ASID);
232}
233
234static __inline int
235decode_assist(void)
236{
237
238	return (svm_feature & AMD_CPUID_SVM_DECODE_ASSIST);
239}
240
241/*
242 * Enable SVM for a CPU.
243 */
244static void
245svm_enable(void *arg __unused)
246{
247	uint64_t hsave_pa;
248
249	(void)cpu_svm_enable_disable(TRUE);
250
251	hsave_pa = vtophys(hsave[curcpu]);
252	wrmsr(MSR_VM_HSAVE_PA, hsave_pa);
253
254	if (rdmsr(MSR_VM_HSAVE_PA) != hsave_pa) {
255		panic("VM_HSAVE_PA is wrong on CPU%d\n", curcpu);
256	}
257}
258
259/*
260 * Check if a processor support SVM.
261 */
262static int
263is_svm_enabled(void)
264{
265	uint64_t msr;
266
267	 /* Section 15.4 Enabling SVM from APM2. */
268	if ((amd_feature2 & AMDID2_SVM) == 0) {
269		printf("SVM is not supported on this processor.\n");
270		return (ENXIO);
271	}
272
273	msr = rdmsr(MSR_VM_CR);
274	/* Make sure SVM is not disabled by BIOS. */
275	if ((msr & VM_CR_SVMDIS) == 0) {
276		return svm_cpuid_features();
277	}
278
279	printf("SVM disabled by Key, consult TPM/BIOS manual.\n");
280	return (ENXIO);
281}
282
283/*
284 * Enable SVM on CPU and initialize nested page table h/w.
285 */
286static int
287svm_init(int ipinum)
288{
289	int err, cpu;
290
291	err = is_svm_enabled();
292	if (err)
293		return (err);
294
295	for (cpu = 0; cpu < MAXCPU; cpu++) {
296		/*
297		 * Initialize the host ASIDs to their "highest" valid values.
298		 *
299		 * The next ASID allocation will rollover both 'gen' and 'num'
300		 * and start off the sequence at {1,1}.
301		 */
302		asid[cpu].gen = ~0UL;
303		asid[cpu].num = nasid - 1;
304	}
305
306	svm_msr_init();
307	svm_npt_init(ipinum);
308
309	/* Start SVM on all CPUs */
310	smp_rendezvous(NULL, svm_enable, NULL, NULL);
311
312	return (0);
313}
314
315static void
316svm_restore(void)
317{
318	svm_enable(NULL);
319}
320
321/*
322 * Get index and bit position for a MSR in MSR permission
323 * bitmap. Two bits are used for each MSR, lower bit is
324 * for read and higher bit is for write.
325 */
326static int
327svm_msr_index(uint64_t msr, int *index, int *bit)
328{
329	uint32_t base, off;
330
331/* Pentium compatible MSRs */
332#define MSR_PENTIUM_START 	0
333#define MSR_PENTIUM_END 	0x1FFF
334/* AMD 6th generation and Intel compatible MSRs */
335#define MSR_AMD6TH_START 	0xC0000000UL
336#define MSR_AMD6TH_END 		0xC0001FFFUL
337/* AMD 7th and 8th generation compatible MSRs */
338#define MSR_AMD7TH_START 	0xC0010000UL
339#define MSR_AMD7TH_END 		0xC0011FFFUL
340
341	*index = -1;
342	*bit = (msr % 4) * 2;
343	base = 0;
344
345	if (msr >= MSR_PENTIUM_START && msr <= MSR_PENTIUM_END) {
346		*index = msr / 4;
347		return (0);
348	}
349
350	base += (MSR_PENTIUM_END - MSR_PENTIUM_START + 1);
351	if (msr >= MSR_AMD6TH_START && msr <= MSR_AMD6TH_END) {
352		off = (msr - MSR_AMD6TH_START);
353		*index = (off + base) / 4;
354		return (0);
355	}
356
357	base += (MSR_AMD6TH_END - MSR_AMD6TH_START + 1);
358	if (msr >= MSR_AMD7TH_START && msr <= MSR_AMD7TH_END) {
359		off = (msr - MSR_AMD7TH_START);
360		*index = (off + base) / 4;
361		return (0);
362	}
363
364	return (EIO);
365}
366
367/*
368 * Give virtual cpu the complete access to MSR(read & write).
369 */
370static int
371svm_msr_perm(uint8_t *perm_bitmap, uint64_t msr, bool read, bool write)
372{
373	int index, bit, err;
374
375	err = svm_msr_index(msr, &index, &bit);
376	if (err) {
377		ERR("MSR 0x%lx is not writeable by guest.\n", msr);
378		return (err);
379	}
380
381	if (index < 0 || index > (SVM_MSR_BITMAP_SIZE)) {
382		ERR("MSR 0x%lx index out of range(%d).\n", msr, index);
383		return (EINVAL);
384	}
385	if (bit < 0 || bit > 8) {
386		ERR("MSR 0x%lx bit out of range(%d).\n", msr, bit);
387		return (EINVAL);
388	}
389
390	/* Disable intercept for read and write. */
391	if (read)
392		perm_bitmap[index] &= ~(1UL << bit);
393	if (write)
394		perm_bitmap[index] &= ~(2UL << bit);
395	CTR2(KTR_VMM, "Guest has control:0x%x on SVM:MSR(0x%lx).\n",
396		(perm_bitmap[index] >> bit) & 0x3, msr);
397
398	return (0);
399}
400
401static int
402svm_msr_rw_ok(uint8_t *perm_bitmap, uint64_t msr)
403{
404	return svm_msr_perm(perm_bitmap, msr, true, true);
405}
406
407static int
408svm_msr_rd_ok(uint8_t *perm_bitmap, uint64_t msr)
409{
410	return svm_msr_perm(perm_bitmap, msr, true, false);
411}
412
413static __inline void
414vcpu_set_dirty(struct svm_softc *sc, int vcpu, uint32_t dirtybits)
415{
416	struct svm_vcpu *vcpustate;
417
418	vcpustate = svm_get_vcpu(sc, vcpu);
419
420	vcpustate->dirty |= dirtybits;
421}
422
423static __inline int
424svm_get_intercept(struct svm_softc *sc, int vcpu, int idx, uint32_t bitmask)
425{
426	struct vmcb_ctrl *ctrl;
427
428	KASSERT(idx >=0 && idx < 5, ("invalid intercept index %d", idx));
429
430	ctrl = svm_get_vmcb_ctrl(sc, vcpu);
431	return (ctrl->intercept[idx] & bitmask ? 1 : 0);
432}
433
434static __inline void
435svm_set_intercept(struct svm_softc *sc, int vcpu, int idx, uint32_t bitmask,
436    int enabled)
437{
438	struct vmcb_ctrl *ctrl;
439	uint32_t oldval;
440
441	KASSERT(idx >=0 && idx < 5, ("invalid intercept index %d", idx));
442
443	ctrl = svm_get_vmcb_ctrl(sc, vcpu);
444	oldval = ctrl->intercept[idx];
445
446	if (enabled)
447		ctrl->intercept[idx] |= bitmask;
448	else
449		ctrl->intercept[idx] &= ~bitmask;
450
451	if (ctrl->intercept[idx] != oldval) {
452		vcpu_set_dirty(sc, vcpu, VMCB_CACHE_I);
453		VCPU_CTR3(sc->vm, vcpu, "intercept[%d] modified "
454		    "from %#x to %#x", idx, oldval, ctrl->intercept[idx]);
455	}
456}
457
458static __inline void
459svm_disable_intercept(struct svm_softc *sc, int vcpu, int off, uint32_t bitmask)
460{
461	svm_set_intercept(sc, vcpu, off, bitmask, 0);
462}
463
464static __inline void
465svm_enable_intercept(struct svm_softc *sc, int vcpu, int off, uint32_t bitmask)
466{
467	svm_set_intercept(sc, vcpu, off, bitmask, 1);
468}
469
470static void
471vmcb_init(struct svm_softc *sc, int vcpu, uint64_t iopm_base_pa,
472    uint64_t msrpm_base_pa, uint64_t np_pml4)
473{
474	struct vmcb_ctrl *ctrl;
475	struct vmcb_state *state;
476	uint32_t mask;
477	int n;
478
479	ctrl = svm_get_vmcb_ctrl(sc, vcpu);
480	state = svm_get_vmcb_state(sc, vcpu);
481
482	ctrl->iopm_base_pa = iopm_base_pa;
483	ctrl->msrpm_base_pa = msrpm_base_pa;
484
485	/* Enable nested paging */
486	ctrl->np_enable = 1;
487	ctrl->n_cr3 = np_pml4;
488
489	/*
490	 * Intercept accesses to the control registers that are not shadowed
491	 * in the VMCB - i.e. all except cr0, cr2, cr3, cr4 and cr8.
492	 */
493	for (n = 0; n < 16; n++) {
494		mask = (BIT(n) << 16) | BIT(n);
495		if (n == 0 || n == 2 || n == 3 || n == 4 || n == 8)
496			svm_disable_intercept(sc, vcpu, VMCB_CR_INTCPT, mask);
497		else
498			svm_enable_intercept(sc, vcpu, VMCB_CR_INTCPT, mask);
499	}
500
501	/* Intercept Machine Check exceptions. */
502	svm_enable_intercept(sc, vcpu, VMCB_EXC_INTCPT, BIT(IDT_MC));
503
504	/* Intercept various events (for e.g. I/O, MSR and CPUID accesses) */
505	svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_IO);
506	svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_MSR);
507	svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_CPUID);
508	svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_INTR);
509	svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_INIT);
510	svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_NMI);
511	svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_SMI);
512	svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_SHUTDOWN);
513	svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT,
514	    VMCB_INTCPT_FERR_FREEZE);
515
516	/*
517	 * From section "Canonicalization and Consistency Checks" in APMv2
518	 * the VMRUN intercept bit must be set to pass the consistency check.
519	 */
520	svm_enable_intercept(sc, vcpu, VMCB_CTRL2_INTCPT, VMCB_INTCPT_VMRUN);
521
522	/*
523	 * The ASID will be set to a non-zero value just before VMRUN.
524	 */
525	ctrl->asid = 0;
526
527	/*
528	 * Section 15.21.1, Interrupt Masking in EFLAGS
529	 * Section 15.21.2, Virtualizing APIC.TPR
530	 *
531	 * This must be set for %rflag and %cr8 isolation of guest and host.
532	 */
533	ctrl->v_intr_masking = 1;
534
535	/* Enable Last Branch Record aka LBR for debugging */
536	ctrl->lbr_virt_en = 1;
537	state->dbgctl = BIT(0);
538
539	/* EFER_SVM must always be set when the guest is executing */
540	state->efer = EFER_SVM;
541
542	/* Set up the PAT to power-on state */
543	state->g_pat = PAT_VALUE(0, PAT_WRITE_BACK)	|
544	    PAT_VALUE(1, PAT_WRITE_THROUGH)	|
545	    PAT_VALUE(2, PAT_UNCACHED)		|
546	    PAT_VALUE(3, PAT_UNCACHEABLE)	|
547	    PAT_VALUE(4, PAT_WRITE_BACK)	|
548	    PAT_VALUE(5, PAT_WRITE_THROUGH)	|
549	    PAT_VALUE(6, PAT_UNCACHED)		|
550	    PAT_VALUE(7, PAT_UNCACHEABLE);
551}
552
553/*
554 * Initialise a virtual machine.
555 */
556static void *
557svm_vminit(struct vm *vm, pmap_t pmap)
558{
559	struct svm_softc *svm_sc;
560	struct svm_vcpu *vcpu;
561	vm_paddr_t msrpm_pa, iopm_pa, pml4_pa;
562	int i;
563
564	svm_sc = (struct svm_softc *)malloc(sizeof (struct svm_softc),
565			M_SVM, M_WAITOK | M_ZERO);
566
567	svm_sc->vm = vm;
568	svm_sc->nptp = (vm_offset_t)vtophys(pmap->pm_pml4);
569
570	/*
571	 * Intercept MSR access to all MSRs except GSBASE, FSBASE,... etc.
572	 */
573	 memset(svm_sc->msr_bitmap, 0xFF, sizeof(svm_sc->msr_bitmap));
574
575	/*
576	 * Following MSR can be completely controlled by virtual machines
577	 * since access to following are translated to access to VMCB.
578	 */
579	svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_GSBASE);
580	svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_FSBASE);
581	svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_KGSBASE);
582
583	svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_STAR);
584	svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_LSTAR);
585	svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_CSTAR);
586	svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_SF_MASK);
587	svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_SYSENTER_CS_MSR);
588	svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_SYSENTER_ESP_MSR);
589	svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_SYSENTER_EIP_MSR);
590
591	/* For Nested Paging/RVI only. */
592	svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_PAT);
593
594	svm_msr_rd_ok(svm_sc->msr_bitmap, MSR_TSC);
595	svm_msr_rd_ok(svm_sc->msr_bitmap, MSR_EFER);
596
597	 /* Intercept access to all I/O ports. */
598	memset(svm_sc->iopm_bitmap, 0xFF, sizeof(svm_sc->iopm_bitmap));
599
600	/* Cache physical address for multiple vcpus. */
601	iopm_pa = vtophys(svm_sc->iopm_bitmap);
602	msrpm_pa = vtophys(svm_sc->msr_bitmap);
603	pml4_pa = svm_sc->nptp;
604
605	for (i = 0; i < VM_MAXCPU; i++) {
606		vcpu = svm_get_vcpu(svm_sc, i);
607		vcpu->lastcpu = NOCPU;
608		vcpu->vmcb_pa = vtophys(&vcpu->vmcb);
609		vmcb_init(svm_sc, i, iopm_pa, msrpm_pa, pml4_pa);
610		svm_msr_guest_init(svm_sc, i);
611	}
612	return (svm_sc);
613}
614
615static int
616svm_cpl(struct vmcb_state *state)
617{
618
619	/*
620	 * From APMv2:
621	 *   "Retrieve the CPL from the CPL field in the VMCB, not
622	 *    from any segment DPL"
623	 */
624	return (state->cpl);
625}
626
627static enum vm_cpu_mode
628svm_vcpu_mode(struct vmcb *vmcb)
629{
630	struct vmcb_segment *seg;
631	struct vmcb_state *state;
632
633	state = &vmcb->state;
634
635	if (state->efer & EFER_LMA) {
636		seg = vmcb_seg(vmcb, VM_REG_GUEST_CS);
637		/*
638		 * Section 4.8.1 for APM2, check if Code Segment has
639		 * Long attribute set in descriptor.
640		 */
641		if (seg->attrib & VMCB_CS_ATTRIB_L)
642			return (CPU_MODE_64BIT);
643		else
644			return (CPU_MODE_COMPATIBILITY);
645	} else  if (state->cr0 & CR0_PE) {
646		return (CPU_MODE_PROTECTED);
647	} else {
648		return (CPU_MODE_REAL);
649	}
650}
651
652static enum vm_paging_mode
653svm_paging_mode(uint64_t cr0, uint64_t cr4, uint64_t efer)
654{
655
656	if ((cr0 & CR0_PG) == 0)
657		return (PAGING_MODE_FLAT);
658	if ((cr4 & CR4_PAE) == 0)
659		return (PAGING_MODE_32);
660	if (efer & EFER_LME)
661		return (PAGING_MODE_64);
662	else
663		return (PAGING_MODE_PAE);
664}
665
666/*
667 * ins/outs utility routines
668 */
669static uint64_t
670svm_inout_str_index(struct svm_regctx *regs, int in)
671{
672	uint64_t val;
673
674	val = in ? regs->e.g.sctx_rdi : regs->e.g.sctx_rsi;
675
676	return (val);
677}
678
679static uint64_t
680svm_inout_str_count(struct svm_regctx *regs, int rep)
681{
682	uint64_t val;
683
684	val = rep ? regs->sctx_rcx : 1;
685
686	return (val);
687}
688
689static void
690svm_inout_str_seginfo(struct svm_softc *svm_sc, int vcpu, int64_t info1,
691    int in, struct vm_inout_str *vis)
692{
693	int error, s;
694
695	if (in) {
696		vis->seg_name = VM_REG_GUEST_ES;
697	} else {
698		/* The segment field has standard encoding */
699		s = (info1 >> 10) & 0x7;
700		vis->seg_name = vm_segment_name(s);
701	}
702
703	error = svm_getdesc(svm_sc, vcpu, vis->seg_name, &vis->seg_desc);
704	KASSERT(error == 0, ("%s: svm_getdesc error %d", __func__, error));
705}
706
707static int
708svm_inout_str_addrsize(uint64_t info1)
709{
710        uint32_t size;
711
712        size = (info1 >> 7) & 0x7;
713        switch (size) {
714        case 1:
715                return (2);     /* 16 bit */
716        case 2:
717                return (4);     /* 32 bit */
718        case 4:
719                return (8);     /* 64 bit */
720        default:
721                panic("%s: invalid size encoding %d", __func__, size);
722        }
723}
724
725static void
726svm_paging_info(struct vmcb *vmcb, struct vm_guest_paging *paging)
727{
728	struct vmcb_state *state;
729
730	state = &vmcb->state;
731	paging->cr3 = state->cr3;
732	paging->cpl = svm_cpl(state);
733	paging->cpu_mode = svm_vcpu_mode(vmcb);
734	paging->paging_mode = svm_paging_mode(state->cr0, state->cr4,
735	    state->efer);
736}
737
738#define	UNHANDLED 0
739
740/*
741 * Handle guest I/O intercept.
742 */
743static int
744svm_handle_io(struct svm_softc *svm_sc, int vcpu, struct vm_exit *vmexit)
745{
746	struct vmcb_ctrl *ctrl;
747	struct vmcb_state *state;
748	struct svm_regctx *regs;
749	struct vm_inout_str *vis;
750	uint64_t info1;
751	int inout_string;
752
753	state = svm_get_vmcb_state(svm_sc, vcpu);
754	ctrl  = svm_get_vmcb_ctrl(svm_sc, vcpu);
755	regs  = svm_get_guest_regctx(svm_sc, vcpu);
756
757	info1 = ctrl->exitinfo1;
758	inout_string = info1 & BIT(2) ? 1 : 0;
759
760	/*
761	 * The effective segment number in EXITINFO1[12:10] is populated
762	 * only if the processor has the DecodeAssist capability.
763	 *
764	 * XXX this is not specified explicitly in APMv2 but can be verified
765	 * empirically.
766	 */
767	if (inout_string && !decode_assist())
768		return (UNHANDLED);
769
770	vmexit->exitcode 	= VM_EXITCODE_INOUT;
771	vmexit->u.inout.in 	= (info1 & BIT(0)) ? 1 : 0;
772	vmexit->u.inout.string 	= inout_string;
773	vmexit->u.inout.rep 	= (info1 & BIT(3)) ? 1 : 0;
774	vmexit->u.inout.bytes 	= (info1 >> 4) & 0x7;
775	vmexit->u.inout.port 	= (uint16_t)(info1 >> 16);
776	vmexit->u.inout.eax 	= (uint32_t)(state->rax);
777
778	if (inout_string) {
779		vmexit->exitcode = VM_EXITCODE_INOUT_STR;
780		vis = &vmexit->u.inout_str;
781		svm_paging_info(svm_get_vmcb(svm_sc, vcpu), &vis->paging);
782		vis->rflags = state->rflags;
783		vis->cr0 = state->cr0;
784		vis->index = svm_inout_str_index(regs, vmexit->u.inout.in);
785		vis->count = svm_inout_str_count(regs, vmexit->u.inout.rep);
786		vis->addrsize = svm_inout_str_addrsize(info1);
787		svm_inout_str_seginfo(svm_sc, vcpu, info1,
788		    vmexit->u.inout.in, vis);
789	}
790
791	return (UNHANDLED);
792}
793
794static int
795svm_npf_paging(uint64_t exitinfo1)
796{
797
798	if (exitinfo1 & VMCB_NPF_INFO1_W)
799		return (VM_PROT_WRITE);
800
801	return (VM_PROT_READ);
802}
803
804static bool
805svm_npf_emul_fault(uint64_t exitinfo1)
806{
807
808	if (exitinfo1 & VMCB_NPF_INFO1_ID) {
809		return (false);
810	}
811
812	if (exitinfo1 & VMCB_NPF_INFO1_GPT) {
813		return (false);
814	}
815
816	if ((exitinfo1 & VMCB_NPF_INFO1_GPA) == 0) {
817		return (false);
818	}
819
820	return (true);
821}
822
823static void
824svm_handle_inst_emul(struct vmcb *vmcb, uint64_t gpa, struct vm_exit *vmexit)
825{
826	struct vm_guest_paging *paging;
827	struct vmcb_segment *seg;
828	struct vmcb_ctrl *ctrl;
829	char *inst_bytes;
830	int inst_len;
831
832	ctrl = &vmcb->ctrl;
833	paging = &vmexit->u.inst_emul.paging;
834
835	vmexit->exitcode = VM_EXITCODE_INST_EMUL;
836	vmexit->u.inst_emul.gpa = gpa;
837	vmexit->u.inst_emul.gla = VIE_INVALID_GLA;
838	svm_paging_info(vmcb, paging);
839
840	seg = vmcb_seg(vmcb, VM_REG_GUEST_CS);
841	switch(paging->cpu_mode) {
842	case CPU_MODE_PROTECTED:
843	case CPU_MODE_COMPATIBILITY:
844		/*
845		 * Section 4.8.1 of APM2, Default Operand Size or D bit.
846		 */
847		vmexit->u.inst_emul.cs_d = (seg->attrib & VMCB_CS_ATTRIB_D) ?
848		    1 : 0;
849		break;
850	default:
851		vmexit->u.inst_emul.cs_d = 0;
852		break;
853	}
854
855	/*
856	 * Copy the instruction bytes into 'vie' if available.
857	 */
858	if (decode_assist() && !disable_npf_assist) {
859		inst_len = ctrl->inst_len;
860		inst_bytes = ctrl->inst_bytes;
861	} else {
862		inst_len = 0;
863		inst_bytes = NULL;
864	}
865	vie_init(&vmexit->u.inst_emul.vie, inst_bytes, inst_len);
866}
867
868/*
869 * Intercept access to MSR_EFER to prevent the guest from clearing the
870 * SVM enable bit.
871 */
872static int
873svm_write_efer(struct svm_softc *sc, int vcpu, uint64_t val)
874{
875	struct vmcb_state *state;
876	uint64_t oldval;
877
878	state = svm_get_vmcb_state(sc, vcpu);
879
880	oldval = state->efer;
881	state->efer = val | EFER_SVM;
882	if (state->efer != oldval) {
883		VCPU_CTR2(sc->vm, vcpu, "Guest EFER changed from %#lx to %#lx",
884		    oldval, state->efer);
885		vcpu_set_dirty(sc, vcpu, VMCB_CACHE_CR);
886	}
887	return (0);
888}
889
890#ifdef KTR
891static const char *
892intrtype_to_str(int intr_type)
893{
894	switch (intr_type) {
895	case VMCB_EVENTINJ_TYPE_INTR:
896		return ("hwintr");
897	case VMCB_EVENTINJ_TYPE_NMI:
898		return ("nmi");
899	case VMCB_EVENTINJ_TYPE_INTn:
900		return ("swintr");
901	case VMCB_EVENTINJ_TYPE_EXCEPTION:
902		return ("exception");
903	default:
904		panic("%s: unknown intr_type %d", __func__, intr_type);
905	}
906}
907#endif
908
909/*
910 * Inject an event to vcpu as described in section 15.20, "Event injection".
911 */
912static void
913svm_eventinject(struct svm_softc *sc, int vcpu, int intr_type, int vector,
914		 uint32_t error, bool ec_valid)
915{
916	struct vmcb_ctrl *ctrl;
917
918	ctrl = svm_get_vmcb_ctrl(sc, vcpu);
919
920	KASSERT((ctrl->eventinj & VMCB_EVENTINJ_VALID) == 0,
921	    ("%s: event already pending %#lx", __func__, ctrl->eventinj));
922
923	KASSERT(vector >=0 && vector <= 255, ("%s: invalid vector %d",
924	    __func__, vector));
925
926	switch (intr_type) {
927	case VMCB_EVENTINJ_TYPE_INTR:
928	case VMCB_EVENTINJ_TYPE_NMI:
929	case VMCB_EVENTINJ_TYPE_INTn:
930		break;
931	case VMCB_EVENTINJ_TYPE_EXCEPTION:
932		if (vector >= 0 && vector <= 31 && vector != 2)
933			break;
934		/* FALLTHROUGH */
935	default:
936		panic("%s: invalid intr_type/vector: %d/%d", __func__,
937		    intr_type, vector);
938	}
939	ctrl->eventinj = vector | (intr_type << 8) | VMCB_EVENTINJ_VALID;
940	if (ec_valid) {
941		ctrl->eventinj |= VMCB_EVENTINJ_EC_VALID;
942		ctrl->eventinj |= (uint64_t)error << 32;
943		VCPU_CTR3(sc->vm, vcpu, "Injecting %s at vector %d errcode %#x",
944		    intrtype_to_str(intr_type), vector, error);
945	} else {
946		VCPU_CTR2(sc->vm, vcpu, "Injecting %s at vector %d",
947		    intrtype_to_str(intr_type), vector);
948	}
949}
950
951static void
952svm_update_virqinfo(struct svm_softc *sc, int vcpu)
953{
954	struct vm *vm;
955	struct vlapic *vlapic;
956	struct vmcb_ctrl *ctrl;
957	int pending;
958
959	vm = sc->vm;
960	vlapic = vm_lapic(vm, vcpu);
961	ctrl = svm_get_vmcb_ctrl(sc, vcpu);
962
963	/* Update %cr8 in the emulated vlapic */
964	vlapic_set_cr8(vlapic, ctrl->v_tpr);
965
966	/*
967	 * If V_IRQ indicates that the interrupt injection attempted on then
968	 * last VMRUN was successful then update the vlapic accordingly.
969	 */
970	if (ctrl->v_intr_vector != 0) {
971		pending = ctrl->v_irq;
972		KASSERT(ctrl->v_intr_vector >= 16, ("%s: invalid "
973		    "v_intr_vector %d", __func__, ctrl->v_intr_vector));
974		KASSERT(!ctrl->v_ign_tpr, ("%s: invalid v_ign_tpr", __func__));
975		VCPU_CTR2(vm, vcpu, "v_intr_vector %d %s", ctrl->v_intr_vector,
976		    pending ? "pending" : "accepted");
977		if (!pending)
978			vlapic_intr_accepted(vlapic, ctrl->v_intr_vector);
979	}
980}
981
982static void
983svm_save_intinfo(struct svm_softc *svm_sc, int vcpu)
984{
985	struct vmcb_ctrl *ctrl;
986	uint64_t intinfo;
987
988	ctrl  = svm_get_vmcb_ctrl(svm_sc, vcpu);
989	intinfo = ctrl->exitintinfo;
990	if (!VMCB_EXITINTINFO_VALID(intinfo))
991		return;
992
993	/*
994	 * From APMv2, Section "Intercepts during IDT interrupt delivery"
995	 *
996	 * If a #VMEXIT happened during event delivery then record the event
997	 * that was being delivered.
998	 */
999	VCPU_CTR2(svm_sc->vm, vcpu, "SVM:Pending INTINFO(0x%lx), vector=%d.\n",
1000		intinfo, VMCB_EXITINTINFO_VECTOR(intinfo));
1001	vmm_stat_incr(svm_sc->vm, vcpu, VCPU_EXITINTINFO, 1);
1002	vm_exit_intinfo(svm_sc->vm, vcpu, intinfo);
1003}
1004
1005static __inline int
1006vintr_intercept_enabled(struct svm_softc *sc, int vcpu)
1007{
1008
1009	return (svm_get_intercept(sc, vcpu, VMCB_CTRL1_INTCPT,
1010	    VMCB_INTCPT_VINTR));
1011}
1012
1013static __inline void
1014enable_intr_window_exiting(struct svm_softc *sc, int vcpu)
1015{
1016	struct vmcb_ctrl *ctrl;
1017
1018	ctrl = svm_get_vmcb_ctrl(sc, vcpu);
1019
1020	if (ctrl->v_irq && ctrl->v_intr_vector == 0) {
1021		KASSERT(ctrl->v_ign_tpr, ("%s: invalid v_ign_tpr", __func__));
1022		KASSERT(vintr_intercept_enabled(sc, vcpu),
1023		    ("%s: vintr intercept should be enabled", __func__));
1024		return;
1025	}
1026
1027	VCPU_CTR0(sc->vm, vcpu, "Enable intr window exiting");
1028	ctrl->v_irq = 1;
1029	ctrl->v_ign_tpr = 1;
1030	ctrl->v_intr_vector = 0;
1031	vcpu_set_dirty(sc, vcpu, VMCB_CACHE_TPR);
1032	svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_VINTR);
1033}
1034
1035static __inline void
1036disable_intr_window_exiting(struct svm_softc *sc, int vcpu)
1037{
1038	struct vmcb_ctrl *ctrl;
1039
1040	ctrl = svm_get_vmcb_ctrl(sc, vcpu);
1041
1042	if (!ctrl->v_irq && ctrl->v_intr_vector == 0) {
1043		KASSERT(!vintr_intercept_enabled(sc, vcpu),
1044		    ("%s: vintr intercept should be disabled", __func__));
1045		return;
1046	}
1047
1048#ifdef KTR
1049	if (ctrl->v_intr_vector == 0)
1050		VCPU_CTR0(sc->vm, vcpu, "Disable intr window exiting");
1051	else
1052		VCPU_CTR0(sc->vm, vcpu, "Clearing V_IRQ interrupt injection");
1053#endif
1054	ctrl->v_irq = 0;
1055	ctrl->v_intr_vector = 0;
1056	vcpu_set_dirty(sc, vcpu, VMCB_CACHE_TPR);
1057	svm_disable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_VINTR);
1058}
1059
1060static int
1061svm_modify_intr_shadow(struct svm_softc *sc, int vcpu, uint64_t val)
1062{
1063	struct vmcb_ctrl *ctrl;
1064	int oldval, newval;
1065
1066	ctrl = svm_get_vmcb_ctrl(sc, vcpu);
1067	oldval = ctrl->intr_shadow;
1068	newval = val ? 1 : 0;
1069	if (newval != oldval) {
1070		ctrl->intr_shadow = newval;
1071		VCPU_CTR1(sc->vm, vcpu, "Setting intr_shadow to %d", newval);
1072	}
1073	return (0);
1074}
1075
1076static int
1077svm_get_intr_shadow(struct svm_softc *sc, int vcpu, uint64_t *val)
1078{
1079	struct vmcb_ctrl *ctrl;
1080
1081	ctrl = svm_get_vmcb_ctrl(sc, vcpu);
1082	*val = ctrl->intr_shadow;
1083	return (0);
1084}
1085
1086/*
1087 * Once an NMI is injected it blocks delivery of further NMIs until the handler
1088 * executes an IRET. The IRET intercept is enabled when an NMI is injected to
1089 * to track when the vcpu is done handling the NMI.
1090 */
1091static int
1092nmi_blocked(struct svm_softc *sc, int vcpu)
1093{
1094	int blocked;
1095
1096	blocked = svm_get_intercept(sc, vcpu, VMCB_CTRL1_INTCPT,
1097	    VMCB_INTCPT_IRET);
1098	return (blocked);
1099}
1100
1101static void
1102enable_nmi_blocking(struct svm_softc *sc, int vcpu)
1103{
1104
1105	KASSERT(!nmi_blocked(sc, vcpu), ("vNMI already blocked"));
1106	VCPU_CTR0(sc->vm, vcpu, "vNMI blocking enabled");
1107	svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_IRET);
1108}
1109
1110static void
1111clear_nmi_blocking(struct svm_softc *sc, int vcpu)
1112{
1113	int error;
1114
1115	KASSERT(nmi_blocked(sc, vcpu), ("vNMI already unblocked"));
1116	VCPU_CTR0(sc->vm, vcpu, "vNMI blocking cleared");
1117	/*
1118	 * When the IRET intercept is cleared the vcpu will attempt to execute
1119	 * the "iret" when it runs next. However, it is possible to inject
1120	 * another NMI into the vcpu before the "iret" has actually executed.
1121	 *
1122	 * For e.g. if the "iret" encounters a #NPF when accessing the stack
1123	 * it will trap back into the hypervisor. If an NMI is pending for
1124	 * the vcpu it will be injected into the guest.
1125	 *
1126	 * XXX this needs to be fixed
1127	 */
1128	svm_disable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_IRET);
1129
1130	/*
1131	 * Set 'intr_shadow' to prevent an NMI from being injected on the
1132	 * immediate VMRUN.
1133	 */
1134	error = svm_modify_intr_shadow(sc, vcpu, 1);
1135	KASSERT(!error, ("%s: error %d setting intr_shadow", __func__, error));
1136}
1137
1138static int
1139emulate_wrmsr(struct svm_softc *sc, int vcpu, u_int num, uint64_t val,
1140    bool *retu)
1141{
1142	int error;
1143
1144	if (lapic_msr(num))
1145		error = lapic_wrmsr(sc->vm, vcpu, num, val, retu);
1146	else if (num == MSR_EFER)
1147		error = svm_write_efer(sc, vcpu, val);
1148	else
1149		error = svm_wrmsr(sc, vcpu, num, val, retu);
1150
1151	return (error);
1152}
1153
1154static int
1155emulate_rdmsr(struct svm_softc *sc, int vcpu, u_int num, bool *retu)
1156{
1157	struct vmcb_state *state;
1158	struct svm_regctx *ctx;
1159	uint64_t result;
1160	int error;
1161
1162	if (lapic_msr(num))
1163		error = lapic_rdmsr(sc->vm, vcpu, num, &result, retu);
1164	else
1165		error = svm_rdmsr(sc, vcpu, num, &result, retu);
1166
1167	if (error == 0) {
1168		state = svm_get_vmcb_state(sc, vcpu);
1169		ctx = svm_get_guest_regctx(sc, vcpu);
1170		state->rax = result & 0xffffffff;
1171		ctx->e.g.sctx_rdx = result >> 32;
1172	}
1173
1174	return (error);
1175}
1176
1177#ifdef KTR
1178static const char *
1179exit_reason_to_str(uint64_t reason)
1180{
1181	static char reasonbuf[32];
1182
1183	switch (reason) {
1184	case VMCB_EXIT_INVALID:
1185		return ("invalvmcb");
1186	case VMCB_EXIT_SHUTDOWN:
1187		return ("shutdown");
1188	case VMCB_EXIT_NPF:
1189		return ("nptfault");
1190	case VMCB_EXIT_PAUSE:
1191		return ("pause");
1192	case VMCB_EXIT_HLT:
1193		return ("hlt");
1194	case VMCB_EXIT_CPUID:
1195		return ("cpuid");
1196	case VMCB_EXIT_IO:
1197		return ("inout");
1198	case VMCB_EXIT_MC:
1199		return ("mchk");
1200	case VMCB_EXIT_INTR:
1201		return ("extintr");
1202	case VMCB_EXIT_NMI:
1203		return ("nmi");
1204	case VMCB_EXIT_VINTR:
1205		return ("vintr");
1206	case VMCB_EXIT_MSR:
1207		return ("msr");
1208	case VMCB_EXIT_IRET:
1209		return ("iret");
1210	default:
1211		snprintf(reasonbuf, sizeof(reasonbuf), "%#lx", reason);
1212		return (reasonbuf);
1213	}
1214}
1215#endif	/* KTR */
1216
1217/*
1218 * From section "State Saved on Exit" in APMv2: nRIP is saved for all #VMEXITs
1219 * that are due to instruction intercepts as well as MSR and IOIO intercepts
1220 * and exceptions caused by INT3, INTO and BOUND instructions.
1221 *
1222 * Return 1 if the nRIP is valid and 0 otherwise.
1223 */
1224static int
1225nrip_valid(uint64_t exitcode)
1226{
1227	switch (exitcode) {
1228	case 0x00 ... 0x0F:	/* read of CR0 through CR15 */
1229	case 0x10 ... 0x1F:	/* write of CR0 through CR15 */
1230	case 0x20 ... 0x2F:	/* read of DR0 through DR15 */
1231	case 0x30 ... 0x3F:	/* write of DR0 through DR15 */
1232	case 0x43:		/* INT3 */
1233	case 0x44:		/* INTO */
1234	case 0x45:		/* BOUND */
1235	case 0x65 ... 0x7C:	/* VMEXIT_CR0_SEL_WRITE ... VMEXIT_MSR */
1236	case 0x80 ... 0x8D:	/* VMEXIT_VMRUN ... VMEXIT_XSETBV */
1237		return (1);
1238	default:
1239		return (0);
1240	}
1241}
1242
1243/*
1244 * Collateral for a generic SVM VM-exit.
1245 */
1246static void
1247vm_exit_svm(struct vm_exit *vme, uint64_t code, uint64_t info1, uint64_t info2)
1248{
1249
1250	vme->exitcode = VM_EXITCODE_SVM;
1251	vme->u.svm.exitcode = code;
1252	vme->u.svm.exitinfo1 = info1;
1253	vme->u.svm.exitinfo2 = info2;
1254}
1255
1256static int
1257svm_vmexit(struct svm_softc *svm_sc, int vcpu, struct vm_exit *vmexit)
1258{
1259	struct vmcb *vmcb;
1260	struct vmcb_state *state;
1261	struct vmcb_ctrl *ctrl;
1262	struct svm_regctx *ctx;
1263	uint64_t code, info1, info2, val;
1264	uint32_t eax, ecx, edx;
1265	int handled;
1266	bool retu;
1267
1268	ctx = svm_get_guest_regctx(svm_sc, vcpu);
1269	vmcb = svm_get_vmcb(svm_sc, vcpu);
1270	state = &vmcb->state;
1271	ctrl = &vmcb->ctrl;
1272
1273	handled = 0;
1274	code = ctrl->exitcode;
1275	info1 = ctrl->exitinfo1;
1276	info2 = ctrl->exitinfo2;
1277
1278	vmexit->exitcode = VM_EXITCODE_BOGUS;
1279	vmexit->rip = state->rip;
1280	vmexit->inst_length = nrip_valid(code) ? ctrl->nrip - state->rip : 0;
1281
1282	vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_COUNT, 1);
1283
1284	/*
1285	 * #VMEXIT(INVALID) needs to be handled early because the VMCB is
1286	 * in an inconsistent state and can trigger assertions that would
1287	 * never happen otherwise.
1288	 */
1289	if (code == VMCB_EXIT_INVALID) {
1290		vm_exit_svm(vmexit, code, info1, info2);
1291		return (0);
1292	}
1293
1294	KASSERT((ctrl->eventinj & VMCB_EVENTINJ_VALID) == 0, ("%s: event "
1295	    "injection valid bit is set %#lx", __func__, ctrl->eventinj));
1296
1297	KASSERT(vmexit->inst_length >= 0 && vmexit->inst_length <= 15,
1298	    ("invalid inst_length %d: code (%#lx), info1 (%#lx), info2 (%#lx)",
1299	    vmexit->inst_length, code, info1, info2));
1300
1301	svm_update_virqinfo(svm_sc, vcpu);
1302	svm_save_intinfo(svm_sc, vcpu);
1303
1304	switch (code) {
1305	case VMCB_EXIT_IRET:
1306		/*
1307		 * Restart execution at "iret" but with the intercept cleared.
1308		 */
1309		vmexit->inst_length = 0;
1310		clear_nmi_blocking(svm_sc, vcpu);
1311		handled = 1;
1312		break;
1313	case VMCB_EXIT_VINTR:	/* interrupt window exiting */
1314		vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_VINTR, 1);
1315		handled = 1;
1316		break;
1317	case VMCB_EXIT_INTR:	/* external interrupt */
1318		vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_EXTINT, 1);
1319		handled = 1;
1320		break;
1321	case VMCB_EXIT_NMI:	/* external NMI */
1322		handled = 1;
1323		break;
1324	case VMCB_EXIT_MC:	/* machine check */
1325		vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_EXCEPTION, 1);
1326		break;
1327	case VMCB_EXIT_MSR:	/* MSR access. */
1328		eax = state->rax;
1329		ecx = ctx->sctx_rcx;
1330		edx = ctx->e.g.sctx_rdx;
1331		retu = false;
1332
1333		if (info1) {
1334			vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_WRMSR, 1);
1335			val = (uint64_t)edx << 32 | eax;
1336			VCPU_CTR2(svm_sc->vm, vcpu, "wrmsr %#x val %#lx",
1337			    ecx, val);
1338			if (emulate_wrmsr(svm_sc, vcpu, ecx, val, &retu)) {
1339				vmexit->exitcode = VM_EXITCODE_WRMSR;
1340				vmexit->u.msr.code = ecx;
1341				vmexit->u.msr.wval = val;
1342			} else if (!retu) {
1343				handled = 1;
1344			} else {
1345				KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
1346				    ("emulate_wrmsr retu with bogus exitcode"));
1347			}
1348		} else {
1349			VCPU_CTR1(svm_sc->vm, vcpu, "rdmsr %#x", ecx);
1350			vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_RDMSR, 1);
1351			if (emulate_rdmsr(svm_sc, vcpu, ecx, &retu)) {
1352				vmexit->exitcode = VM_EXITCODE_RDMSR;
1353				vmexit->u.msr.code = ecx;
1354			} else if (!retu) {
1355				handled = 1;
1356			} else {
1357				KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS,
1358				    ("emulate_rdmsr retu with bogus exitcode"));
1359			}
1360		}
1361		break;
1362	case VMCB_EXIT_IO:
1363		handled = svm_handle_io(svm_sc, vcpu, vmexit);
1364		vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_INOUT, 1);
1365		break;
1366	case VMCB_EXIT_CPUID:
1367		vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_CPUID, 1);
1368		handled = x86_emulate_cpuid(svm_sc->vm, vcpu,
1369		    (uint32_t *)&state->rax,
1370		    (uint32_t *)&ctx->sctx_rbx,
1371		    (uint32_t *)&ctx->sctx_rcx,
1372		    (uint32_t *)&ctx->e.g.sctx_rdx);
1373		break;
1374	case VMCB_EXIT_HLT:
1375		vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_HLT, 1);
1376		vmexit->exitcode = VM_EXITCODE_HLT;
1377		vmexit->u.hlt.rflags = state->rflags;
1378		break;
1379	case VMCB_EXIT_PAUSE:
1380		vmexit->exitcode = VM_EXITCODE_PAUSE;
1381		vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_PAUSE, 1);
1382		break;
1383	case VMCB_EXIT_NPF:
1384		/* EXITINFO2 contains the faulting guest physical address */
1385		if (info1 & VMCB_NPF_INFO1_RSV) {
1386			VCPU_CTR2(svm_sc->vm, vcpu, "nested page fault with "
1387			    "reserved bits set: info1(%#lx) info2(%#lx)",
1388			    info1, info2);
1389		} else if (vm_mem_allocated(svm_sc->vm, info2)) {
1390			vmexit->exitcode = VM_EXITCODE_PAGING;
1391			vmexit->u.paging.gpa = info2;
1392			vmexit->u.paging.fault_type = svm_npf_paging(info1);
1393			vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_NESTED_FAULT, 1);
1394			VCPU_CTR3(svm_sc->vm, vcpu, "nested page fault "
1395			    "on gpa %#lx/%#lx at rip %#lx",
1396			    info2, info1, state->rip);
1397		} else if (svm_npf_emul_fault(info1)) {
1398			svm_handle_inst_emul(vmcb, info2, vmexit);
1399			vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_INST_EMUL, 1);
1400			VCPU_CTR3(svm_sc->vm, vcpu, "inst_emul fault "
1401			    "for gpa %#lx/%#lx at rip %#lx",
1402			    info2, info1, state->rip);
1403		}
1404		break;
1405	default:
1406		vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_UNKNOWN, 1);
1407		break;
1408	}
1409
1410	VCPU_CTR4(svm_sc->vm, vcpu, "%s %s vmexit at %#lx/%d",
1411	    handled ? "handled" : "unhandled", exit_reason_to_str(code),
1412	    vmexit->rip, vmexit->inst_length);
1413
1414	if (handled) {
1415		vmexit->rip += vmexit->inst_length;
1416		vmexit->inst_length = 0;
1417		state->rip = vmexit->rip;
1418	} else {
1419		if (vmexit->exitcode == VM_EXITCODE_BOGUS) {
1420			/*
1421			 * If this VM exit was not claimed by anybody then
1422			 * treat it as a generic SVM exit.
1423			 */
1424			vm_exit_svm(vmexit, code, info1, info2);
1425		} else {
1426			/*
1427			 * The exitcode and collateral have been populated.
1428			 * The VM exit will be processed further in userland.
1429			 */
1430		}
1431	}
1432	return (handled);
1433}
1434
1435static void
1436svm_inj_intinfo(struct svm_softc *svm_sc, int vcpu)
1437{
1438	uint64_t intinfo;
1439
1440	if (!vm_entry_intinfo(svm_sc->vm, vcpu, &intinfo))
1441		return;
1442
1443	KASSERT(VMCB_EXITINTINFO_VALID(intinfo), ("%s: entry intinfo is not "
1444	    "valid: %#lx", __func__, intinfo));
1445
1446	svm_eventinject(svm_sc, vcpu, VMCB_EXITINTINFO_TYPE(intinfo),
1447		VMCB_EXITINTINFO_VECTOR(intinfo),
1448		VMCB_EXITINTINFO_EC(intinfo),
1449		VMCB_EXITINTINFO_EC_VALID(intinfo));
1450	vmm_stat_incr(svm_sc->vm, vcpu, VCPU_INTINFO_INJECTED, 1);
1451	VCPU_CTR1(svm_sc->vm, vcpu, "Injected entry intinfo: %#lx", intinfo);
1452}
1453
1454/*
1455 * Inject event to virtual cpu.
1456 */
1457static void
1458svm_inj_interrupts(struct svm_softc *sc, int vcpu, struct vlapic *vlapic)
1459{
1460	struct vmcb_ctrl *ctrl;
1461	struct vmcb_state *state;
1462	uint8_t v_tpr;
1463	int vector, need_intr_window, pending_apic_vector;
1464
1465	state = svm_get_vmcb_state(sc, vcpu);
1466	ctrl  = svm_get_vmcb_ctrl(sc, vcpu);
1467
1468	need_intr_window = 0;
1469	pending_apic_vector = 0;
1470
1471	/*
1472	 * Inject pending events or exceptions for this vcpu.
1473	 *
1474	 * An event might be pending because the previous #VMEXIT happened
1475	 * during event delivery (i.e. ctrl->exitintinfo).
1476	 *
1477	 * An event might also be pending because an exception was injected
1478	 * by the hypervisor (e.g. #PF during instruction emulation).
1479	 */
1480	svm_inj_intinfo(sc, vcpu);
1481
1482	/* NMI event has priority over interrupts. */
1483	if (vm_nmi_pending(sc->vm, vcpu)) {
1484		if (nmi_blocked(sc, vcpu)) {
1485			/*
1486			 * Can't inject another NMI if the guest has not
1487			 * yet executed an "iret" after the last NMI.
1488			 */
1489			VCPU_CTR0(sc->vm, vcpu, "Cannot inject NMI due "
1490			    "to NMI-blocking");
1491		} else if (ctrl->intr_shadow) {
1492			/*
1493			 * Can't inject an NMI if the vcpu is in an intr_shadow.
1494			 */
1495			VCPU_CTR0(sc->vm, vcpu, "Cannot inject NMI due to "
1496			    "interrupt shadow");
1497			need_intr_window = 1;
1498			goto done;
1499		} else if (ctrl->eventinj & VMCB_EVENTINJ_VALID) {
1500			/*
1501			 * If there is already an exception/interrupt pending
1502			 * then defer the NMI until after that.
1503			 */
1504			VCPU_CTR1(sc->vm, vcpu, "Cannot inject NMI due to "
1505			    "eventinj %#lx", ctrl->eventinj);
1506
1507			/*
1508			 * Use self-IPI to trigger a VM-exit as soon as
1509			 * possible after the event injection is completed.
1510			 *
1511			 * This works only if the external interrupt exiting
1512			 * is at a lower priority than the event injection.
1513			 *
1514			 * Although not explicitly specified in APMv2 the
1515			 * relative priorities were verified empirically.
1516			 */
1517			ipi_cpu(curcpu, IPI_AST);	/* XXX vmm_ipinum? */
1518		} else {
1519			vm_nmi_clear(sc->vm, vcpu);
1520
1521			/* Inject NMI, vector number is not used */
1522			svm_eventinject(sc, vcpu, VMCB_EVENTINJ_TYPE_NMI,
1523			    IDT_NMI, 0, false);
1524
1525			/* virtual NMI blocking is now in effect */
1526			enable_nmi_blocking(sc, vcpu);
1527
1528			VCPU_CTR0(sc->vm, vcpu, "Injecting vNMI");
1529		}
1530	}
1531
1532	if (!vm_extint_pending(sc->vm, vcpu)) {
1533		/*
1534		 * APIC interrupts are delivered using the V_IRQ offload.
1535		 *
1536		 * The primary benefit is that the hypervisor doesn't need to
1537		 * deal with the various conditions that inhibit interrupts.
1538		 * It also means that TPR changes via CR8 will be handled
1539		 * without any hypervisor involvement.
1540		 *
1541		 * Note that the APIC vector must remain pending in the vIRR
1542		 * until it is confirmed that it was delivered to the guest.
1543		 * This can be confirmed based on the value of V_IRQ at the
1544		 * next #VMEXIT (1 = pending, 0 = delivered).
1545		 *
1546		 * Also note that it is possible that another higher priority
1547		 * vector can become pending before this vector is delivered
1548		 * to the guest. This is alright because vcpu_notify_event()
1549		 * will send an IPI and force the vcpu to trap back into the
1550		 * hypervisor. The higher priority vector will be injected on
1551		 * the next VMRUN.
1552		 */
1553		if (vlapic_pending_intr(vlapic, &vector)) {
1554			KASSERT(vector >= 16 && vector <= 255,
1555			    ("invalid vector %d from local APIC", vector));
1556			pending_apic_vector = vector;
1557		}
1558		goto done;
1559	}
1560
1561	/* Ask the legacy pic for a vector to inject */
1562	vatpic_pending_intr(sc->vm, &vector);
1563	KASSERT(vector >= 0 && vector <= 255, ("invalid vector %d from INTR",
1564	    vector));
1565
1566	/*
1567	 * If the guest has disabled interrupts or is in an interrupt shadow
1568	 * then we cannot inject the pending interrupt.
1569	 */
1570	if ((state->rflags & PSL_I) == 0) {
1571		VCPU_CTR2(sc->vm, vcpu, "Cannot inject vector %d due to "
1572		    "rflags %#lx", vector, state->rflags);
1573		need_intr_window = 1;
1574		goto done;
1575	}
1576
1577	if (ctrl->intr_shadow) {
1578		VCPU_CTR1(sc->vm, vcpu, "Cannot inject vector %d due to "
1579		    "interrupt shadow", vector);
1580		need_intr_window = 1;
1581		goto done;
1582	}
1583
1584	if (ctrl->eventinj & VMCB_EVENTINJ_VALID) {
1585		VCPU_CTR2(sc->vm, vcpu, "Cannot inject vector %d due to "
1586		    "eventinj %#lx", vector, ctrl->eventinj);
1587		need_intr_window = 1;
1588		goto done;
1589	}
1590
1591	/*
1592	 * Legacy PIC interrupts are delivered via the event injection
1593	 * mechanism.
1594	 */
1595	svm_eventinject(sc, vcpu, VMCB_EVENTINJ_TYPE_INTR, vector, 0, false);
1596
1597	vm_extint_clear(sc->vm, vcpu);
1598	vatpic_intr_accepted(sc->vm, vector);
1599
1600	/*
1601	 * Force a VM-exit as soon as the vcpu is ready to accept another
1602	 * interrupt. This is done because the PIC might have another vector
1603	 * that it wants to inject. Also, if the APIC has a pending interrupt
1604	 * that was preempted by the ExtInt then it allows us to inject the
1605	 * APIC vector as soon as possible.
1606	 */
1607	need_intr_window = 1;
1608done:
1609	/*
1610	 * The guest can modify the TPR by writing to %CR8. In guest mode
1611	 * the processor reflects this write to V_TPR without hypervisor
1612	 * intervention.
1613	 *
1614	 * The guest can also modify the TPR by writing to it via the memory
1615	 * mapped APIC page. In this case, the write will be emulated by the
1616	 * hypervisor. For this reason V_TPR must be updated before every
1617	 * VMRUN.
1618	 */
1619	v_tpr = vlapic_get_cr8(vlapic);
1620	KASSERT(v_tpr >= 0 && v_tpr <= 15, ("invalid v_tpr %#x", v_tpr));
1621	if (ctrl->v_tpr != v_tpr) {
1622		VCPU_CTR2(sc->vm, vcpu, "VMCB V_TPR changed from %#x to %#x",
1623		    ctrl->v_tpr, v_tpr);
1624		ctrl->v_tpr = v_tpr;
1625		vcpu_set_dirty(sc, vcpu, VMCB_CACHE_TPR);
1626	}
1627
1628	if (pending_apic_vector) {
1629		/*
1630		 * If an APIC vector is being injected then interrupt window
1631		 * exiting is not possible on this VMRUN.
1632		 */
1633		KASSERT(!need_intr_window, ("intr_window exiting impossible"));
1634		VCPU_CTR1(sc->vm, vcpu, "Injecting vector %d using V_IRQ",
1635		    pending_apic_vector);
1636
1637		ctrl->v_irq = 1;
1638		ctrl->v_ign_tpr = 0;
1639		ctrl->v_intr_vector = pending_apic_vector;
1640		ctrl->v_intr_prio = pending_apic_vector >> 4;
1641		vcpu_set_dirty(sc, vcpu, VMCB_CACHE_TPR);
1642	} else if (need_intr_window) {
1643		/*
1644		 * We use V_IRQ in conjunction with the VINTR intercept to
1645		 * trap into the hypervisor as soon as a virtual interrupt
1646		 * can be delivered.
1647		 *
1648		 * Since injected events are not subject to intercept checks
1649		 * we need to ensure that the V_IRQ is not actually going to
1650		 * be delivered on VM entry. The KASSERT below enforces this.
1651		 */
1652		KASSERT((ctrl->eventinj & VMCB_EVENTINJ_VALID) != 0 ||
1653		    (state->rflags & PSL_I) == 0 || ctrl->intr_shadow,
1654		    ("Bogus intr_window_exiting: eventinj (%#lx), "
1655		    "intr_shadow (%u), rflags (%#lx)",
1656		    ctrl->eventinj, ctrl->intr_shadow, state->rflags));
1657		enable_intr_window_exiting(sc, vcpu);
1658	} else {
1659		disable_intr_window_exiting(sc, vcpu);
1660	}
1661}
1662
1663static __inline void
1664restore_host_tss(void)
1665{
1666	struct system_segment_descriptor *tss_sd;
1667
1668	/*
1669	 * The TSS descriptor was in use prior to launching the guest so it
1670	 * has been marked busy.
1671	 *
1672	 * 'ltr' requires the descriptor to be marked available so change the
1673	 * type to "64-bit available TSS".
1674	 */
1675	tss_sd = PCPU_GET(tss);
1676	tss_sd->sd_type = SDT_SYSTSS;
1677	ltr(GSEL(GPROC0_SEL, SEL_KPL));
1678}
1679
1680static void
1681check_asid(struct svm_softc *sc, int vcpuid, pmap_t pmap, u_int thiscpu)
1682{
1683	struct svm_vcpu *vcpustate;
1684	struct vmcb_ctrl *ctrl;
1685	long eptgen;
1686	bool alloc_asid;
1687
1688	KASSERT(CPU_ISSET(thiscpu, &pmap->pm_active), ("%s: nested pmap not "
1689	    "active on cpu %u", __func__, thiscpu));
1690
1691	vcpustate = svm_get_vcpu(sc, vcpuid);
1692	ctrl = svm_get_vmcb_ctrl(sc, vcpuid);
1693
1694	/*
1695	 * The TLB entries associated with the vcpu's ASID are not valid
1696	 * if either of the following conditions is true:
1697	 *
1698	 * 1. The vcpu's ASID generation is different than the host cpu's
1699	 *    ASID generation. This happens when the vcpu migrates to a new
1700	 *    host cpu. It can also happen when the number of vcpus executing
1701	 *    on a host cpu is greater than the number of ASIDs available.
1702	 *
1703	 * 2. The pmap generation number is different than the value cached in
1704	 *    the 'vcpustate'. This happens when the host invalidates pages
1705	 *    belonging to the guest.
1706	 *
1707	 *	asidgen		eptgen	      Action
1708	 *	mismatch	mismatch
1709	 *	   0		   0		(a)
1710	 *	   0		   1		(b1) or (b2)
1711	 *	   1		   0		(c)
1712	 *	   1		   1		(d)
1713	 *
1714	 * (a) There is no mismatch in eptgen or ASID generation and therefore
1715	 *     no further action is needed.
1716	 *
1717	 * (b1) If the cpu supports FlushByAsid then the vcpu's ASID is
1718	 *      retained and the TLB entries associated with this ASID
1719	 *      are flushed by VMRUN.
1720	 *
1721	 * (b2) If the cpu does not support FlushByAsid then a new ASID is
1722	 *      allocated.
1723	 *
1724	 * (c) A new ASID is allocated.
1725	 *
1726	 * (d) A new ASID is allocated.
1727	 */
1728
1729	alloc_asid = false;
1730	eptgen = pmap->pm_eptgen;
1731	ctrl->tlb_ctrl = VMCB_TLB_FLUSH_NOTHING;
1732
1733	if (vcpustate->asid.gen != asid[thiscpu].gen) {
1734		alloc_asid = true;	/* (c) and (d) */
1735	} else if (vcpustate->eptgen != eptgen) {
1736		if (flush_by_asid())
1737			ctrl->tlb_ctrl = VMCB_TLB_FLUSH_GUEST;	/* (b1) */
1738		else
1739			alloc_asid = true;			/* (b2) */
1740	} else {
1741		/*
1742		 * This is the common case (a).
1743		 */
1744		KASSERT(!alloc_asid, ("ASID allocation not necessary"));
1745		KASSERT(ctrl->tlb_ctrl == VMCB_TLB_FLUSH_NOTHING,
1746		    ("Invalid VMCB tlb_ctrl: %#x", ctrl->tlb_ctrl));
1747	}
1748
1749	if (alloc_asid) {
1750		if (++asid[thiscpu].num >= nasid) {
1751			asid[thiscpu].num = 1;
1752			if (++asid[thiscpu].gen == 0)
1753				asid[thiscpu].gen = 1;
1754			/*
1755			 * If this cpu does not support "flush-by-asid"
1756			 * then flush the entire TLB on a generation
1757			 * bump. Subsequent ASID allocation in this
1758			 * generation can be done without a TLB flush.
1759			 */
1760			if (!flush_by_asid())
1761				ctrl->tlb_ctrl = VMCB_TLB_FLUSH_ALL;
1762		}
1763		vcpustate->asid.gen = asid[thiscpu].gen;
1764		vcpustate->asid.num = asid[thiscpu].num;
1765
1766		ctrl->asid = vcpustate->asid.num;
1767		vcpu_set_dirty(sc, vcpuid, VMCB_CACHE_ASID);
1768		/*
1769		 * If this cpu supports "flush-by-asid" then the TLB
1770		 * was not flushed after the generation bump. The TLB
1771		 * is flushed selectively after every new ASID allocation.
1772		 */
1773		if (flush_by_asid())
1774			ctrl->tlb_ctrl = VMCB_TLB_FLUSH_GUEST;
1775	}
1776	vcpustate->eptgen = eptgen;
1777
1778	KASSERT(ctrl->asid != 0, ("Guest ASID must be non-zero"));
1779	KASSERT(ctrl->asid == vcpustate->asid.num,
1780	    ("ASID mismatch: %u/%u", ctrl->asid, vcpustate->asid.num));
1781}
1782
1783/*
1784 * Start vcpu with specified RIP.
1785 */
1786static int
1787svm_vmrun(void *arg, int vcpu, register_t rip, pmap_t pmap,
1788	void *rend_cookie, void *suspended_cookie)
1789{
1790	struct svm_regctx *hctx, *gctx;
1791	struct svm_softc *svm_sc;
1792	struct svm_vcpu *vcpustate;
1793	struct vmcb_state *state;
1794	struct vmcb_ctrl *ctrl;
1795	struct vm_exit *vmexit;
1796	struct vlapic *vlapic;
1797	struct vm *vm;
1798	uint64_t vmcb_pa;
1799	u_int thiscpu;
1800	int handled;
1801
1802	svm_sc = arg;
1803	vm = svm_sc->vm;
1804
1805	vcpustate = svm_get_vcpu(svm_sc, vcpu);
1806	state = svm_get_vmcb_state(svm_sc, vcpu);
1807	ctrl = svm_get_vmcb_ctrl(svm_sc, vcpu);
1808	vmexit = vm_exitinfo(vm, vcpu);
1809	vlapic = vm_lapic(vm, vcpu);
1810
1811	/*
1812	 * Stash 'curcpu' on the stack as 'thiscpu'.
1813	 *
1814	 * The per-cpu data area is not accessible until MSR_GSBASE is restored
1815	 * after the #VMEXIT. Since VMRUN is executed inside a critical section
1816	 * 'curcpu' and 'thiscpu' are guaranteed to identical.
1817	 */
1818	thiscpu = curcpu;
1819
1820	gctx = svm_get_guest_regctx(svm_sc, vcpu);
1821	hctx = &host_ctx[thiscpu];
1822	vmcb_pa = svm_sc->vcpu[vcpu].vmcb_pa;
1823
1824	if (vcpustate->lastcpu != thiscpu) {
1825		/*
1826		 * Force new ASID allocation by invalidating the generation.
1827		 */
1828		vcpustate->asid.gen = 0;
1829
1830		/*
1831		 * Invalidate the VMCB state cache by marking all fields dirty.
1832		 */
1833		vcpu_set_dirty(svm_sc, vcpu, 0xffffffff);
1834
1835		/*
1836		 * XXX
1837		 * Setting 'vcpustate->lastcpu' here is bit premature because
1838		 * we may return from this function without actually executing
1839		 * the VMRUN  instruction. This could happen if a rendezvous
1840		 * or an AST is pending on the first time through the loop.
1841		 *
1842		 * This works for now but any new side-effects of vcpu
1843		 * migration should take this case into account.
1844		 */
1845		vcpustate->lastcpu = thiscpu;
1846		vmm_stat_incr(vm, vcpu, VCPU_MIGRATIONS, 1);
1847	}
1848
1849	svm_msr_guest_enter(svm_sc, vcpu);
1850
1851	/* Update Guest RIP */
1852	state->rip = rip;
1853
1854	do {
1855		/*
1856		 * Disable global interrupts to guarantee atomicity during
1857		 * loading of guest state. This includes not only the state
1858		 * loaded by the "vmrun" instruction but also software state
1859		 * maintained by the hypervisor: suspended and rendezvous
1860		 * state, NPT generation number, vlapic interrupts etc.
1861		 */
1862		disable_gintr();
1863
1864		if (vcpu_suspended(suspended_cookie)) {
1865			enable_gintr();
1866			vm_exit_suspended(vm, vcpu, state->rip);
1867			break;
1868		}
1869
1870		if (vcpu_rendezvous_pending(rend_cookie)) {
1871			enable_gintr();
1872			vm_exit_rendezvous(vm, vcpu, state->rip);
1873			break;
1874		}
1875
1876		/* We are asked to give the cpu by scheduler. */
1877		if (curthread->td_flags & (TDF_ASTPENDING | TDF_NEEDRESCHED)) {
1878			enable_gintr();
1879			vm_exit_astpending(vm, vcpu, state->rip);
1880			break;
1881		}
1882
1883		svm_inj_interrupts(svm_sc, vcpu, vlapic);
1884
1885		/* Activate the nested pmap on 'thiscpu' */
1886		CPU_SET_ATOMIC_ACQ(thiscpu, &pmap->pm_active);
1887
1888		/*
1889		 * Check the pmap generation and the ASID generation to
1890		 * ensure that the vcpu does not use stale TLB mappings.
1891		 */
1892		check_asid(svm_sc, vcpu, pmap, thiscpu);
1893
1894		ctrl->vmcb_clean = VMCB_CACHE_DEFAULT & ~vcpustate->dirty;
1895		vcpustate->dirty = 0;
1896		VCPU_CTR1(vm, vcpu, "vmcb clean %#x", ctrl->vmcb_clean);
1897
1898		/* Launch Virtual Machine. */
1899		VCPU_CTR1(vm, vcpu, "Resume execution at %#lx", state->rip);
1900		svm_launch(vmcb_pa, gctx, hctx);
1901
1902		CPU_CLR_ATOMIC(thiscpu, &pmap->pm_active);
1903
1904		/*
1905		 * Restore MSR_GSBASE to point to the pcpu data area.
1906		 *
1907		 * Note that accesses done via PCPU_GET/PCPU_SET will work
1908		 * only after MSR_GSBASE is restored.
1909		 *
1910		 * Also note that we don't bother restoring MSR_KGSBASE
1911		 * since it is not used in the kernel and will be restored
1912		 * when the VMRUN ioctl returns to userspace.
1913		 */
1914		wrmsr(MSR_GSBASE, (uint64_t)&__pcpu[thiscpu]);
1915		KASSERT(curcpu == thiscpu, ("thiscpu/curcpu (%u/%u) mismatch",
1916		    thiscpu, curcpu));
1917
1918		/*
1919		 * The host GDTR and IDTR is saved by VMRUN and restored
1920		 * automatically on #VMEXIT. However, the host TSS needs
1921		 * to be restored explicitly.
1922		 */
1923		restore_host_tss();
1924
1925		/* #VMEXIT disables interrupts so re-enable them here. */
1926		enable_gintr();
1927
1928		/* Handle #VMEXIT and if required return to user space. */
1929		handled = svm_vmexit(svm_sc, vcpu, vmexit);
1930	} while (handled);
1931
1932	svm_msr_guest_exit(svm_sc, vcpu);
1933
1934	return (0);
1935}
1936
1937/*
1938 * Cleanup for virtual machine.
1939 */
1940static void
1941svm_vmcleanup(void *arg)
1942{
1943	struct svm_softc *svm_sc;
1944
1945	svm_sc = arg;
1946
1947	VCPU_CTR0(svm_sc->vm, 0, "SVM:cleanup\n");
1948
1949	free(svm_sc, M_SVM);
1950}
1951
1952/*
1953 * Return pointer to hypervisor saved register state.
1954 */
1955static register_t *
1956swctx_regptr(struct svm_regctx *regctx, int reg)
1957{
1958
1959	switch (reg) {
1960		case VM_REG_GUEST_RBX:
1961			return (&regctx->sctx_rbx);
1962		case VM_REG_GUEST_RCX:
1963			return (&regctx->sctx_rcx);
1964		case VM_REG_GUEST_RDX:
1965			return (&regctx->e.g.sctx_rdx);
1966		case VM_REG_GUEST_RDI:
1967			return (&regctx->e.g.sctx_rdi);
1968		case VM_REG_GUEST_RSI:
1969			return (&regctx->e.g.sctx_rsi);
1970		case VM_REG_GUEST_RBP:
1971			return (&regctx->sctx_rbp);
1972		case VM_REG_GUEST_R8:
1973			return (&regctx->sctx_r8);
1974		case VM_REG_GUEST_R9:
1975			return (&regctx->sctx_r9);
1976		case VM_REG_GUEST_R10:
1977			return (&regctx->sctx_r10);
1978		case VM_REG_GUEST_R11:
1979			return (&regctx->sctx_r11);
1980		case VM_REG_GUEST_R12:
1981			return (&regctx->sctx_r12);
1982		case VM_REG_GUEST_R13:
1983			return (&regctx->sctx_r13);
1984		case VM_REG_GUEST_R14:
1985			return (&regctx->sctx_r14);
1986		case VM_REG_GUEST_R15:
1987			return (&regctx->sctx_r15);
1988		default:
1989			ERR("Unknown register requested, reg=%d.\n", reg);
1990			break;
1991	}
1992
1993	return (NULL);
1994}
1995
1996/*
1997 * Interface to read guest registers.
1998 * This can be SVM h/w saved or hypervisor saved register.
1999 */
2000static int
2001svm_getreg(void *arg, int vcpu, int ident, uint64_t *val)
2002{
2003	struct svm_softc *svm_sc;
2004	struct vmcb *vmcb;
2005	register_t *reg;
2006
2007	svm_sc = arg;
2008	vmcb = svm_get_vmcb(svm_sc, vcpu);
2009
2010	if (ident == VM_REG_GUEST_INTR_SHADOW) {
2011		return (svm_get_intr_shadow(svm_sc, vcpu, val));
2012	}
2013
2014	if (vmcb_read(vmcb, ident, val) == 0) {
2015		return (0);
2016	}
2017
2018	reg = swctx_regptr(svm_get_guest_regctx(svm_sc, vcpu), ident);
2019
2020	if (reg != NULL) {
2021		*val = *reg;
2022		return (0);
2023	}
2024
2025 	ERR("SVM_ERR:reg type %x is not saved in VMCB.\n", ident);
2026	return (EINVAL);
2027}
2028
2029/*
2030 * Interface to write to guest registers.
2031 * This can be SVM h/w saved or hypervisor saved register.
2032 */
2033static int
2034svm_setreg(void *arg, int vcpu, int ident, uint64_t val)
2035{
2036	struct svm_softc *svm_sc;
2037	struct vmcb *vmcb;
2038	register_t *reg;
2039
2040	svm_sc = arg;
2041	vmcb = svm_get_vmcb(svm_sc, vcpu);
2042
2043	if (ident == VM_REG_GUEST_INTR_SHADOW) {
2044		return (svm_modify_intr_shadow(svm_sc, vcpu, val));
2045	}
2046
2047	if (vmcb_write(vmcb, ident, val) == 0) {
2048		return (0);
2049	}
2050
2051	reg = swctx_regptr(svm_get_guest_regctx(svm_sc, vcpu), ident);
2052
2053	if (reg != NULL) {
2054		*reg = val;
2055		return (0);
2056	}
2057
2058	/*
2059	 * XXX deal with CR3 and invalidate TLB entries tagged with the
2060	 * vcpu's ASID. This needs to be treated differently depending on
2061	 * whether 'running' is true/false.
2062	 */
2063
2064 	ERR("SVM_ERR:reg type %x is not saved in VMCB.\n", ident);
2065	return (EINVAL);
2066}
2067
2068
2069/*
2070 * Inteface to set various descriptors.
2071 */
2072static int
2073svm_setdesc(void *arg, int vcpu, int type, struct seg_desc *desc)
2074{
2075	struct svm_softc *svm_sc;
2076	struct vmcb *vmcb;
2077	struct vmcb_segment *seg;
2078	uint16_t attrib;
2079
2080	svm_sc = arg;
2081	vmcb = svm_get_vmcb(svm_sc, vcpu);
2082
2083	VCPU_CTR1(svm_sc->vm, vcpu, "SVM:set_desc: Type%d\n", type);
2084
2085	seg = vmcb_seg(vmcb, type);
2086	if (seg == NULL) {
2087		ERR("SVM_ERR:Unsupported segment type%d\n", type);
2088		return (EINVAL);
2089	}
2090
2091	/* Map seg_desc access to VMCB attribute format.*/
2092	attrib = ((desc->access & 0xF000) >> 4) | (desc->access & 0xFF);
2093	VCPU_CTR3(svm_sc->vm, vcpu, "SVM:[sel %d attribute 0x%x limit:0x%x]\n",
2094		type, desc->access, desc->limit);
2095	seg->attrib = attrib;
2096	seg->base = desc->base;
2097	seg->limit = desc->limit;
2098
2099	return (0);
2100}
2101
2102/*
2103 * Interface to get guest descriptor.
2104 */
2105static int
2106svm_getdesc(void *arg, int vcpu, int type, struct seg_desc *desc)
2107{
2108	struct svm_softc *svm_sc;
2109	struct vmcb_segment	*seg;
2110
2111	svm_sc = arg;
2112	VCPU_CTR1(svm_sc->vm, vcpu, "SVM:get_desc: Type%d\n", type);
2113
2114	seg = vmcb_seg(svm_get_vmcb(svm_sc, vcpu), type);
2115	if (!seg) {
2116		ERR("SVM_ERR:Unsupported segment type%d\n", type);
2117		return (EINVAL);
2118	}
2119
2120	/* Map seg_desc access to VMCB attribute format.*/
2121	desc->access = ((seg->attrib & 0xF00) << 4) | (seg->attrib & 0xFF);
2122	desc->base = seg->base;
2123	desc->limit = seg->limit;
2124
2125	/*
2126	 * VT-x uses bit 16 (Unusable) to indicate a segment that has been
2127	 * loaded with a NULL segment selector. The 'desc->access' field is
2128	 * interpreted in the VT-x format by the processor-independent code.
2129	 *
2130	 * SVM uses the 'P' bit to convey the same information so convert it
2131	 * into the VT-x format. For more details refer to section
2132	 * "Segment State in the VMCB" in APMv2.
2133	 */
2134	if (type == VM_REG_GUEST_CS && type == VM_REG_GUEST_TR)
2135		desc->access |= 0x80;		/* CS and TS always present */
2136
2137	if (!(desc->access & 0x80))
2138		desc->access |= 0x10000;	/* Unusable segment */
2139
2140	return (0);
2141}
2142
2143static int
2144svm_setcap(void *arg, int vcpu, int type, int val)
2145{
2146	struct svm_softc *sc;
2147	int error;
2148
2149	sc = arg;
2150	error = 0;
2151	switch (type) {
2152	case VM_CAP_HALT_EXIT:
2153		svm_set_intercept(sc, vcpu, VMCB_CTRL1_INTCPT,
2154		    VMCB_INTCPT_HLT, val);
2155		break;
2156	case VM_CAP_PAUSE_EXIT:
2157		svm_set_intercept(sc, vcpu, VMCB_CTRL1_INTCPT,
2158		    VMCB_INTCPT_PAUSE, val);
2159		break;
2160	case VM_CAP_UNRESTRICTED_GUEST:
2161		/* Unrestricted guest execution cannot be disabled in SVM */
2162		if (val == 0)
2163			error = EINVAL;
2164		break;
2165	default:
2166		error = ENOENT;
2167		break;
2168	}
2169	return (error);
2170}
2171
2172static int
2173svm_getcap(void *arg, int vcpu, int type, int *retval)
2174{
2175	struct svm_softc *sc;
2176	int error;
2177
2178	sc = arg;
2179	error = 0;
2180
2181	switch (type) {
2182	case VM_CAP_HALT_EXIT:
2183		*retval = svm_get_intercept(sc, vcpu, VMCB_CTRL1_INTCPT,
2184		    VMCB_INTCPT_HLT);
2185		break;
2186	case VM_CAP_PAUSE_EXIT:
2187		*retval = svm_get_intercept(sc, vcpu, VMCB_CTRL1_INTCPT,
2188		    VMCB_INTCPT_PAUSE);
2189		break;
2190	case VM_CAP_UNRESTRICTED_GUEST:
2191		*retval = 1;	/* unrestricted guest is always enabled */
2192		break;
2193	default:
2194		error = ENOENT;
2195		break;
2196	}
2197	return (error);
2198}
2199
2200static struct vlapic *
2201svm_vlapic_init(void *arg, int vcpuid)
2202{
2203	struct svm_softc *svm_sc;
2204	struct vlapic *vlapic;
2205
2206	svm_sc = arg;
2207	vlapic = malloc(sizeof(struct vlapic), M_SVM_VLAPIC, M_WAITOK | M_ZERO);
2208	vlapic->vm = svm_sc->vm;
2209	vlapic->vcpuid = vcpuid;
2210	vlapic->apic_page = (struct LAPIC *)&svm_sc->apic_page[vcpuid];
2211
2212	vlapic_init(vlapic);
2213
2214	return (vlapic);
2215}
2216
2217static void
2218svm_vlapic_cleanup(void *arg, struct vlapic *vlapic)
2219{
2220
2221        vlapic_cleanup(vlapic);
2222        free(vlapic, M_SVM_VLAPIC);
2223}
2224
2225struct vmm_ops vmm_ops_amd = {
2226	svm_init,
2227	svm_cleanup,
2228	svm_restore,
2229	svm_vminit,
2230	svm_vmrun,
2231	svm_vmcleanup,
2232	svm_getreg,
2233	svm_setreg,
2234	svm_getdesc,
2235	svm_setdesc,
2236	svm_getcap,
2237	svm_setcap,
2238	svm_npt_alloc,
2239	svm_npt_free,
2240	svm_vlapic_init,
2241	svm_vlapic_cleanup
2242};
2243