vmm.c revision 242122
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$
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/kernel.h>
35#include <sys/module.h>
36#include <sys/sysctl.h>
37#include <sys/malloc.h>
38#include <sys/pcpu.h>
39#include <sys/lock.h>
40#include <sys/mutex.h>
41#include <sys/proc.h>
42#include <sys/sched.h>
43#include <sys/smp.h>
44#include <sys/systm.h>
45
46#include <vm/vm.h>
47
48#include <machine/vm.h>
49#include <machine/pcb.h>
50#include <machine/smp.h>
51#include <x86/apicreg.h>
52
53#include <machine/vmm.h>
54#include "vmm_mem.h"
55#include "vmm_util.h"
56#include <machine/vmm_dev.h>
57#include "vlapic.h"
58#include "vmm_msr.h"
59#include "vmm_ipi.h"
60#include "vmm_stat.h"
61#include "vmm_lapic.h"
62
63#include "io/ppt.h"
64#include "io/iommu.h"
65
66struct vlapic;
67
68struct vcpu {
69	int		flags;
70	enum vcpu_state	state;
71	struct mtx	mtx;
72	int		pincpu;		/* host cpuid this vcpu is bound to */
73	int		hostcpu;	/* host cpuid this vcpu last ran on */
74	uint64_t	guest_msrs[VMM_MSR_NUM];
75	struct vlapic	*vlapic;
76	int		 vcpuid;
77	struct savefpu	*guestfpu;	/* guest fpu state */
78	void		*stats;
79	struct vm_exit	exitinfo;
80	enum x2apic_state x2apic_state;
81	int		nmi_pending;
82};
83#define	VCPU_F_PINNED	0x0001
84
85#define	VCPU_PINCPU(vm, vcpuid)	\
86    ((vm->vcpu[vcpuid].flags & VCPU_F_PINNED) ? vm->vcpu[vcpuid].pincpu : -1)
87
88#define	VCPU_UNPIN(vm, vcpuid)	(vm->vcpu[vcpuid].flags &= ~VCPU_F_PINNED)
89
90#define	VCPU_PIN(vm, vcpuid, host_cpuid)				\
91do {									\
92	vm->vcpu[vcpuid].flags |= VCPU_F_PINNED;			\
93	vm->vcpu[vcpuid].pincpu = host_cpuid;				\
94} while(0)
95
96#define	vcpu_lock_init(v)	mtx_init(&((v)->mtx), "vcpu lock", 0, MTX_SPIN)
97#define	vcpu_lock(v)		mtx_lock_spin(&((v)->mtx))
98#define	vcpu_unlock(v)		mtx_unlock_spin(&((v)->mtx))
99
100#define	VM_MAX_MEMORY_SEGMENTS	2
101
102struct vm {
103	void		*cookie;	/* processor-specific data */
104	void		*iommu;		/* iommu-specific data */
105	struct vcpu	vcpu[VM_MAXCPU];
106	int		num_mem_segs;
107	struct vm_memory_segment mem_segs[VM_MAX_MEMORY_SEGMENTS];
108	char		name[VM_MAX_NAMELEN];
109
110	/*
111	 * Set of active vcpus.
112	 * An active vcpu is one that has been started implicitly (BSP) or
113	 * explicitly (AP) by sending it a startup ipi.
114	 */
115	cpuset_t	active_cpus;
116};
117
118static struct vmm_ops *ops;
119#define	VMM_INIT()	(ops != NULL ? (*ops->init)() : 0)
120#define	VMM_CLEANUP()	(ops != NULL ? (*ops->cleanup)() : 0)
121
122#define	VMINIT(vm)	(ops != NULL ? (*ops->vminit)(vm): NULL)
123#define	VMRUN(vmi, vcpu, rip) \
124	(ops != NULL ? (*ops->vmrun)(vmi, vcpu, rip) : ENXIO)
125#define	VMCLEANUP(vmi)	(ops != NULL ? (*ops->vmcleanup)(vmi) : NULL)
126#define	VMMMAP_SET(vmi, gpa, hpa, len, attr, prot, spm)			\
127    	(ops != NULL ? 							\
128    	(*ops->vmmmap_set)(vmi, gpa, hpa, len, attr, prot, spm) :	\
129	ENXIO)
130#define	VMMMAP_GET(vmi, gpa) \
131	(ops != NULL ? (*ops->vmmmap_get)(vmi, gpa) : ENXIO)
132#define	VMGETREG(vmi, vcpu, num, retval)		\
133	(ops != NULL ? (*ops->vmgetreg)(vmi, vcpu, num, retval) : ENXIO)
134#define	VMSETREG(vmi, vcpu, num, val)		\
135	(ops != NULL ? (*ops->vmsetreg)(vmi, vcpu, num, val) : ENXIO)
136#define	VMGETDESC(vmi, vcpu, num, desc)		\
137	(ops != NULL ? (*ops->vmgetdesc)(vmi, vcpu, num, desc) : ENXIO)
138#define	VMSETDESC(vmi, vcpu, num, desc)		\
139	(ops != NULL ? (*ops->vmsetdesc)(vmi, vcpu, num, desc) : ENXIO)
140#define	VMINJECT(vmi, vcpu, type, vec, ec, ecv)	\
141	(ops != NULL ? (*ops->vminject)(vmi, vcpu, type, vec, ec, ecv) : ENXIO)
142#define	VMGETCAP(vmi, vcpu, num, retval)	\
143	(ops != NULL ? (*ops->vmgetcap)(vmi, vcpu, num, retval) : ENXIO)
144#define	VMSETCAP(vmi, vcpu, num, val)		\
145	(ops != NULL ? (*ops->vmsetcap)(vmi, vcpu, num, val) : ENXIO)
146
147#define	fpu_start_emulating()	start_emulating()
148#define	fpu_stop_emulating()	stop_emulating()
149
150static MALLOC_DEFINE(M_VM, "vm", "vm");
151CTASSERT(VMM_MSR_NUM <= 64);	/* msr_mask can keep track of up to 64 msrs */
152
153/* statistics */
154static VMM_STAT_DEFINE(VCPU_TOTAL_RUNTIME, "vcpu total runtime");
155
156static void
157vcpu_cleanup(struct vcpu *vcpu)
158{
159	vlapic_cleanup(vcpu->vlapic);
160	vmm_stat_free(vcpu->stats);
161	fpu_save_area_free(vcpu->guestfpu);
162}
163
164static void
165vcpu_init(struct vm *vm, uint32_t vcpu_id)
166{
167	struct vcpu *vcpu;
168
169	vcpu = &vm->vcpu[vcpu_id];
170
171	vcpu_lock_init(vcpu);
172	vcpu->hostcpu = NOCPU;
173	vcpu->vcpuid = vcpu_id;
174	vcpu->vlapic = vlapic_init(vm, vcpu_id);
175	vm_set_x2apic_state(vm, vcpu_id, X2APIC_ENABLED);
176	vcpu->guestfpu = fpu_save_area_alloc();
177	fpu_save_area_reset(vcpu->guestfpu);
178	vcpu->stats = vmm_stat_alloc();
179}
180
181struct vm_exit *
182vm_exitinfo(struct vm *vm, int cpuid)
183{
184	struct vcpu *vcpu;
185
186	if (cpuid < 0 || cpuid >= VM_MAXCPU)
187		panic("vm_exitinfo: invalid cpuid %d", cpuid);
188
189	vcpu = &vm->vcpu[cpuid];
190
191	return (&vcpu->exitinfo);
192}
193
194static int
195vmm_init(void)
196{
197	int error;
198
199	vmm_ipi_init();
200
201	error = vmm_mem_init();
202	if (error)
203		return (error);
204
205	if (vmm_is_intel())
206		ops = &vmm_ops_intel;
207	else if (vmm_is_amd())
208		ops = &vmm_ops_amd;
209	else
210		return (ENXIO);
211
212	vmm_msr_init();
213
214	return (VMM_INIT());
215}
216
217static int
218vmm_handler(module_t mod, int what, void *arg)
219{
220	int error;
221
222	switch (what) {
223	case MOD_LOAD:
224		vmmdev_init();
225		iommu_init();
226		error = vmm_init();
227		break;
228	case MOD_UNLOAD:
229		error = vmmdev_cleanup();
230		if (error == 0) {
231			iommu_cleanup();
232			vmm_ipi_cleanup();
233			error = VMM_CLEANUP();
234		}
235		break;
236	default:
237		error = 0;
238		break;
239	}
240	return (error);
241}
242
243static moduledata_t vmm_kmod = {
244	"vmm",
245	vmm_handler,
246	NULL
247};
248
249/*
250 * Execute the module load handler after the pci passthru driver has had
251 * a chance to claim devices. We need this information at the time we do
252 * iommu initialization.
253 */
254DECLARE_MODULE(vmm, vmm_kmod, SI_SUB_CONFIGURE + 1, SI_ORDER_ANY);
255MODULE_VERSION(vmm, 1);
256
257SYSCTL_NODE(_hw, OID_AUTO, vmm, CTLFLAG_RW, NULL, NULL);
258
259struct vm *
260vm_create(const char *name)
261{
262	int i;
263	struct vm *vm;
264	vm_paddr_t maxaddr;
265
266	const int BSP = 0;
267
268	if (name == NULL || strlen(name) >= VM_MAX_NAMELEN)
269		return (NULL);
270
271	vm = malloc(sizeof(struct vm), M_VM, M_WAITOK | M_ZERO);
272	strcpy(vm->name, name);
273	vm->cookie = VMINIT(vm);
274
275	for (i = 0; i < VM_MAXCPU; i++) {
276		vcpu_init(vm, i);
277		guest_msrs_init(vm, i);
278	}
279
280	maxaddr = vmm_mem_maxaddr();
281	vm->iommu = iommu_create_domain(maxaddr);
282	vm_activate_cpu(vm, BSP);
283
284	return (vm);
285}
286
287static void
288vm_free_mem_seg(struct vm *vm, struct vm_memory_segment *seg)
289{
290	size_t len;
291	vm_paddr_t hpa;
292	void *host_domain;
293
294	host_domain = iommu_host_domain();
295
296	len = 0;
297	while (len < seg->len) {
298		hpa = vm_gpa2hpa(vm, seg->gpa + len, PAGE_SIZE);
299		if (hpa == (vm_paddr_t)-1) {
300			panic("vm_free_mem_segs: cannot free hpa "
301			      "associated with gpa 0x%016lx", seg->gpa + len);
302		}
303
304		/*
305		 * Remove the 'gpa' to 'hpa' mapping in VMs domain.
306		 * And resurrect the 1:1 mapping for 'hpa' in 'host_domain'.
307		 */
308		iommu_remove_mapping(vm->iommu, seg->gpa + len, PAGE_SIZE);
309		iommu_create_mapping(host_domain, hpa, hpa, PAGE_SIZE);
310
311		vmm_mem_free(hpa, PAGE_SIZE);
312
313		len += PAGE_SIZE;
314	}
315
316	/*
317	 * Invalidate cached translations associated with 'vm->iommu' since
318	 * we have now moved some pages from it.
319	 */
320	iommu_invalidate_tlb(vm->iommu);
321
322	bzero(seg, sizeof(struct vm_memory_segment));
323}
324
325void
326vm_destroy(struct vm *vm)
327{
328	int i;
329
330	ppt_unassign_all(vm);
331
332	for (i = 0; i < vm->num_mem_segs; i++)
333		vm_free_mem_seg(vm, &vm->mem_segs[i]);
334
335	vm->num_mem_segs = 0;
336
337	for (i = 0; i < VM_MAXCPU; i++)
338		vcpu_cleanup(&vm->vcpu[i]);
339
340	iommu_destroy_domain(vm->iommu);
341
342	VMCLEANUP(vm->cookie);
343
344	free(vm, M_VM);
345}
346
347const char *
348vm_name(struct vm *vm)
349{
350	return (vm->name);
351}
352
353int
354vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
355{
356	const boolean_t spok = TRUE;	/* superpage mappings are ok */
357
358	return (VMMMAP_SET(vm->cookie, gpa, hpa, len, VM_MEMATTR_UNCACHEABLE,
359			   VM_PROT_RW, spok));
360}
361
362int
363vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len)
364{
365	const boolean_t spok = TRUE;	/* superpage mappings are ok */
366
367	return (VMMMAP_SET(vm->cookie, gpa, 0, len, 0,
368			   VM_PROT_NONE, spok));
369}
370
371/*
372 * Returns TRUE if 'gpa' is available for allocation and FALSE otherwise
373 */
374static boolean_t
375vm_gpa_available(struct vm *vm, vm_paddr_t gpa)
376{
377	int i;
378	vm_paddr_t gpabase, gpalimit;
379
380	if (gpa & PAGE_MASK)
381		panic("vm_gpa_available: gpa (0x%016lx) not page aligned", gpa);
382
383	for (i = 0; i < vm->num_mem_segs; i++) {
384		gpabase = vm->mem_segs[i].gpa;
385		gpalimit = gpabase + vm->mem_segs[i].len;
386		if (gpa >= gpabase && gpa < gpalimit)
387			return (FALSE);
388	}
389
390	return (TRUE);
391}
392
393int
394vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len)
395{
396	int error, available, allocated;
397	struct vm_memory_segment *seg;
398	vm_paddr_t g, hpa;
399	void *host_domain;
400
401	const boolean_t spok = TRUE;	/* superpage mappings are ok */
402
403	if ((gpa & PAGE_MASK) || (len & PAGE_MASK) || len == 0)
404		return (EINVAL);
405
406	available = allocated = 0;
407	g = gpa;
408	while (g < gpa + len) {
409		if (vm_gpa_available(vm, g))
410			available++;
411		else
412			allocated++;
413
414		g += PAGE_SIZE;
415	}
416
417	/*
418	 * If there are some allocated and some available pages in the address
419	 * range then it is an error.
420	 */
421	if (allocated && available)
422		return (EINVAL);
423
424	/*
425	 * If the entire address range being requested has already been
426	 * allocated then there isn't anything more to do.
427	 */
428	if (allocated && available == 0)
429		return (0);
430
431	if (vm->num_mem_segs >= VM_MAX_MEMORY_SEGMENTS)
432		return (E2BIG);
433
434	host_domain = iommu_host_domain();
435
436	seg = &vm->mem_segs[vm->num_mem_segs];
437
438	error = 0;
439	seg->gpa = gpa;
440	seg->len = 0;
441	while (seg->len < len) {
442		hpa = vmm_mem_alloc(PAGE_SIZE);
443		if (hpa == 0) {
444			error = ENOMEM;
445			break;
446		}
447
448		error = VMMMAP_SET(vm->cookie, gpa + seg->len, hpa, PAGE_SIZE,
449				   VM_MEMATTR_WRITE_BACK, VM_PROT_ALL, spok);
450		if (error)
451			break;
452
453		/*
454		 * Remove the 1:1 mapping for 'hpa' from the 'host_domain'.
455		 * Add mapping for 'gpa + seg->len' to 'hpa' in the VMs domain.
456		 */
457		iommu_remove_mapping(host_domain, hpa, PAGE_SIZE);
458		iommu_create_mapping(vm->iommu, gpa + seg->len, hpa, PAGE_SIZE);
459
460		seg->len += PAGE_SIZE;
461	}
462
463	if (error) {
464		vm_free_mem_seg(vm, seg);
465		return (error);
466	}
467
468	/*
469	 * Invalidate cached translations associated with 'host_domain' since
470	 * we have now moved some pages from it.
471	 */
472	iommu_invalidate_tlb(host_domain);
473
474	vm->num_mem_segs++;
475
476	return (0);
477}
478
479vm_paddr_t
480vm_gpa2hpa(struct vm *vm, vm_paddr_t gpa, size_t len)
481{
482	vm_paddr_t nextpage;
483
484	nextpage = rounddown(gpa + PAGE_SIZE, PAGE_SIZE);
485	if (len > nextpage - gpa)
486		panic("vm_gpa2hpa: invalid gpa/len: 0x%016lx/%lu", gpa, len);
487
488	return (VMMMAP_GET(vm->cookie, gpa));
489}
490
491int
492vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase,
493		  struct vm_memory_segment *seg)
494{
495	int i;
496
497	for (i = 0; i < vm->num_mem_segs; i++) {
498		if (gpabase == vm->mem_segs[i].gpa) {
499			*seg = vm->mem_segs[i];
500			return (0);
501		}
502	}
503	return (-1);
504}
505
506int
507vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval)
508{
509
510	if (vcpu < 0 || vcpu >= VM_MAXCPU)
511		return (EINVAL);
512
513	if (reg >= VM_REG_LAST)
514		return (EINVAL);
515
516	return (VMGETREG(vm->cookie, vcpu, reg, retval));
517}
518
519int
520vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val)
521{
522
523	if (vcpu < 0 || vcpu >= VM_MAXCPU)
524		return (EINVAL);
525
526	if (reg >= VM_REG_LAST)
527		return (EINVAL);
528
529	return (VMSETREG(vm->cookie, vcpu, reg, val));
530}
531
532static boolean_t
533is_descriptor_table(int reg)
534{
535
536	switch (reg) {
537	case VM_REG_GUEST_IDTR:
538	case VM_REG_GUEST_GDTR:
539		return (TRUE);
540	default:
541		return (FALSE);
542	}
543}
544
545static boolean_t
546is_segment_register(int reg)
547{
548
549	switch (reg) {
550	case VM_REG_GUEST_ES:
551	case VM_REG_GUEST_CS:
552	case VM_REG_GUEST_SS:
553	case VM_REG_GUEST_DS:
554	case VM_REG_GUEST_FS:
555	case VM_REG_GUEST_GS:
556	case VM_REG_GUEST_TR:
557	case VM_REG_GUEST_LDTR:
558		return (TRUE);
559	default:
560		return (FALSE);
561	}
562}
563
564int
565vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
566		struct seg_desc *desc)
567{
568
569	if (vcpu < 0 || vcpu >= VM_MAXCPU)
570		return (EINVAL);
571
572	if (!is_segment_register(reg) && !is_descriptor_table(reg))
573		return (EINVAL);
574
575	return (VMGETDESC(vm->cookie, vcpu, reg, desc));
576}
577
578int
579vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
580		struct seg_desc *desc)
581{
582	if (vcpu < 0 || vcpu >= VM_MAXCPU)
583		return (EINVAL);
584
585	if (!is_segment_register(reg) && !is_descriptor_table(reg))
586		return (EINVAL);
587
588	return (VMSETDESC(vm->cookie, vcpu, reg, desc));
589}
590
591int
592vm_get_pinning(struct vm *vm, int vcpuid, int *cpuid)
593{
594
595	if (vcpuid < 0 || vcpuid >= VM_MAXCPU)
596		return (EINVAL);
597
598	*cpuid = VCPU_PINCPU(vm, vcpuid);
599
600	return (0);
601}
602
603int
604vm_set_pinning(struct vm *vm, int vcpuid, int host_cpuid)
605{
606	struct thread *td;
607
608	if (vcpuid < 0 || vcpuid >= VM_MAXCPU)
609		return (EINVAL);
610
611	td = curthread;		/* XXXSMP only safe when muxing vcpus */
612
613	/* unpin */
614	if (host_cpuid < 0) {
615		VCPU_UNPIN(vm, vcpuid);
616		thread_lock(td);
617		sched_unbind(td);
618		thread_unlock(td);
619		return (0);
620	}
621
622	if (CPU_ABSENT(host_cpuid))
623		return (EINVAL);
624
625	/*
626	 * XXX we should check that 'host_cpuid' has not already been pinned
627	 * by another vm.
628	 */
629	thread_lock(td);
630	sched_bind(td, host_cpuid);
631	thread_unlock(td);
632	VCPU_PIN(vm, vcpuid, host_cpuid);
633
634	return (0);
635}
636
637static void
638restore_guest_fpustate(struct vcpu *vcpu)
639{
640
641	/* flush host state to the pcb */
642	fpuexit(curthread);
643
644	/* restore guest FPU state */
645	fpu_stop_emulating();
646	fpurestore(vcpu->guestfpu);
647
648	/*
649	 * The FPU is now "dirty" with the guest's state so turn on emulation
650	 * to trap any access to the FPU by the host.
651	 */
652	fpu_start_emulating();
653}
654
655static void
656save_guest_fpustate(struct vcpu *vcpu)
657{
658
659	if ((rcr0() & CR0_TS) == 0)
660		panic("fpu emulation not enabled in host!");
661
662	/* save guest FPU state */
663	fpu_stop_emulating();
664	fpusave(vcpu->guestfpu);
665	fpu_start_emulating();
666}
667
668static VMM_STAT_DEFINE(VCPU_IDLE_TICKS, "number of ticks vcpu was idle");
669
670int
671vm_run(struct vm *vm, struct vm_run *vmrun)
672{
673	int error, vcpuid, sleepticks, t;
674	struct vcpu *vcpu;
675	struct pcb *pcb;
676	uint64_t tscval, rip;
677	struct vm_exit *vme;
678
679	vcpuid = vmrun->cpuid;
680
681	if (vcpuid < 0 || vcpuid >= VM_MAXCPU)
682		return (EINVAL);
683
684	vcpu = &vm->vcpu[vcpuid];
685	vme = &vmrun->vm_exit;
686	rip = vmrun->rip;
687restart:
688	critical_enter();
689
690	tscval = rdtsc();
691
692	pcb = PCPU_GET(curpcb);
693	set_pcb_flags(pcb, PCB_FULL_IRET);
694
695	restore_guest_msrs(vm, vcpuid);
696	restore_guest_fpustate(vcpu);
697
698	vcpu->hostcpu = curcpu;
699	error = VMRUN(vm->cookie, vcpuid, rip);
700	vcpu->hostcpu = NOCPU;
701
702	save_guest_fpustate(vcpu);
703	restore_host_msrs(vm, vcpuid);
704
705	vmm_stat_incr(vm, vcpuid, VCPU_TOTAL_RUNTIME, rdtsc() - tscval);
706
707	/* copy the exit information */
708	bcopy(&vcpu->exitinfo, vme, sizeof(struct vm_exit));
709
710	critical_exit();
711
712	/*
713	 * Oblige the guest's desire to 'hlt' by sleeping until the vcpu
714	 * is ready to run.
715	 */
716	if (error == 0 && vme->exitcode == VM_EXITCODE_HLT) {
717		vcpu_lock(vcpu);
718
719		/*
720		 * Figure out the number of host ticks until the next apic
721		 * timer interrupt in the guest.
722		 */
723		sleepticks = lapic_timer_tick(vm, vcpuid);
724
725		/*
726		 * If the guest local apic timer is disabled then sleep for
727		 * a long time but not forever.
728		 */
729		if (sleepticks < 0)
730			sleepticks = hz;
731
732		/*
733		 * Do a final check for pending NMI or interrupts before
734		 * really putting this thread to sleep.
735		 *
736		 * These interrupts could have happened any time after we
737		 * returned from VMRUN() and before we grabbed the vcpu lock.
738		 */
739		if (!vm_nmi_pending(vm, vcpuid) &&
740		    lapic_pending_intr(vm, vcpuid) < 0) {
741			if (sleepticks <= 0)
742				panic("invalid sleepticks %d", sleepticks);
743			t = ticks;
744			msleep_spin(vcpu, &vcpu->mtx, "vmidle", sleepticks);
745			vmm_stat_incr(vm, vcpuid, VCPU_IDLE_TICKS, ticks - t);
746		}
747
748		vcpu_unlock(vcpu);
749
750		rip = vme->rip + vme->inst_length;
751		goto restart;
752	}
753
754	return (error);
755}
756
757int
758vm_inject_event(struct vm *vm, int vcpuid, int type,
759		int vector, uint32_t code, int code_valid)
760{
761	if (vcpuid < 0 || vcpuid >= VM_MAXCPU)
762		return (EINVAL);
763
764	if ((type > VM_EVENT_NONE && type < VM_EVENT_MAX) == 0)
765		return (EINVAL);
766
767	if (vector < 0 || vector > 255)
768		return (EINVAL);
769
770	return (VMINJECT(vm->cookie, vcpuid, type, vector, code, code_valid));
771}
772
773static VMM_STAT_DEFINE(VCPU_NMI_COUNT, "number of NMIs delivered to vcpu");
774
775int
776vm_inject_nmi(struct vm *vm, int vcpuid)
777{
778	struct vcpu *vcpu;
779
780	if (vcpuid < 0 || vcpuid >= VM_MAXCPU)
781		return (EINVAL);
782
783	vcpu = &vm->vcpu[vcpuid];
784
785	vcpu->nmi_pending = 1;
786	vm_interrupt_hostcpu(vm, vcpuid);
787	return (0);
788}
789
790int
791vm_nmi_pending(struct vm *vm, int vcpuid)
792{
793	struct vcpu *vcpu;
794
795	if (vcpuid < 0 || vcpuid >= VM_MAXCPU)
796		panic("vm_nmi_pending: invalid vcpuid %d", vcpuid);
797
798	vcpu = &vm->vcpu[vcpuid];
799
800	return (vcpu->nmi_pending);
801}
802
803void
804vm_nmi_clear(struct vm *vm, int vcpuid)
805{
806	struct vcpu *vcpu;
807
808	if (vcpuid < 0 || vcpuid >= VM_MAXCPU)
809		panic("vm_nmi_pending: invalid vcpuid %d", vcpuid);
810
811	vcpu = &vm->vcpu[vcpuid];
812
813	if (vcpu->nmi_pending == 0)
814		panic("vm_nmi_clear: inconsistent nmi_pending state");
815
816	vcpu->nmi_pending = 0;
817	vmm_stat_incr(vm, vcpuid, VCPU_NMI_COUNT, 1);
818}
819
820int
821vm_get_capability(struct vm *vm, int vcpu, int type, int *retval)
822{
823	if (vcpu < 0 || vcpu >= VM_MAXCPU)
824		return (EINVAL);
825
826	if (type < 0 || type >= VM_CAP_MAX)
827		return (EINVAL);
828
829	return (VMGETCAP(vm->cookie, vcpu, type, retval));
830}
831
832int
833vm_set_capability(struct vm *vm, int vcpu, int type, int val)
834{
835	if (vcpu < 0 || vcpu >= VM_MAXCPU)
836		return (EINVAL);
837
838	if (type < 0 || type >= VM_CAP_MAX)
839		return (EINVAL);
840
841	return (VMSETCAP(vm->cookie, vcpu, type, val));
842}
843
844uint64_t *
845vm_guest_msrs(struct vm *vm, int cpu)
846{
847	return (vm->vcpu[cpu].guest_msrs);
848}
849
850struct vlapic *
851vm_lapic(struct vm *vm, int cpu)
852{
853	return (vm->vcpu[cpu].vlapic);
854}
855
856boolean_t
857vmm_is_pptdev(int bus, int slot, int func)
858{
859	int found, b, s, f, n;
860	char *val, *cp, *cp2;
861
862	/*
863	 * setenv pptdevs "1/2/3 4/5/6 7/8/9 10/11/12"
864	 */
865	found = 0;
866	cp = val = getenv("pptdevs");
867	while (cp != NULL && *cp != '\0') {
868		if ((cp2 = strchr(cp, ' ')) != NULL)
869			*cp2 = '\0';
870
871		n = sscanf(cp, "%d/%d/%d", &b, &s, &f);
872		if (n == 3 && bus == b && slot == s && func == f) {
873			found = 1;
874			break;
875		}
876
877		if (cp2 != NULL)
878			*cp2++ = ' ';
879
880		cp = cp2;
881	}
882	freeenv(val);
883	return (found);
884}
885
886void *
887vm_iommu_domain(struct vm *vm)
888{
889
890	return (vm->iommu);
891}
892
893int
894vcpu_set_state(struct vm *vm, int vcpuid, enum vcpu_state state)
895{
896	int error;
897	struct vcpu *vcpu;
898
899	if (vcpuid < 0 || vcpuid >= VM_MAXCPU)
900		panic("vm_set_run_state: invalid vcpuid %d", vcpuid);
901
902	vcpu = &vm->vcpu[vcpuid];
903
904	vcpu_lock(vcpu);
905
906	/*
907	 * The following state transitions are allowed:
908	 * IDLE -> RUNNING -> IDLE
909	 * IDLE -> CANNOT_RUN -> IDLE
910	 */
911	if ((vcpu->state == VCPU_IDLE && state != VCPU_IDLE) ||
912	    (vcpu->state != VCPU_IDLE && state == VCPU_IDLE)) {
913		error = 0;
914		vcpu->state = state;
915	} else {
916		error = EBUSY;
917	}
918
919	vcpu_unlock(vcpu);
920
921	return (error);
922}
923
924enum vcpu_state
925vcpu_get_state(struct vm *vm, int vcpuid)
926{
927	struct vcpu *vcpu;
928	enum vcpu_state state;
929
930	if (vcpuid < 0 || vcpuid >= VM_MAXCPU)
931		panic("vm_get_run_state: invalid vcpuid %d", vcpuid);
932
933	vcpu = &vm->vcpu[vcpuid];
934
935	vcpu_lock(vcpu);
936	state = vcpu->state;
937	vcpu_unlock(vcpu);
938
939	return (state);
940}
941
942void
943vm_activate_cpu(struct vm *vm, int vcpuid)
944{
945
946	if (vcpuid >= 0 && vcpuid < VM_MAXCPU)
947		CPU_SET(vcpuid, &vm->active_cpus);
948}
949
950cpuset_t
951vm_active_cpus(struct vm *vm)
952{
953
954	return (vm->active_cpus);
955}
956
957void *
958vcpu_stats(struct vm *vm, int vcpuid)
959{
960
961	return (vm->vcpu[vcpuid].stats);
962}
963
964int
965vm_get_x2apic_state(struct vm *vm, int vcpuid, enum x2apic_state *state)
966{
967	if (vcpuid < 0 || vcpuid >= VM_MAXCPU)
968		return (EINVAL);
969
970	*state = vm->vcpu[vcpuid].x2apic_state;
971
972	return (0);
973}
974
975int
976vm_set_x2apic_state(struct vm *vm, int vcpuid, enum x2apic_state state)
977{
978	if (vcpuid < 0 || vcpuid >= VM_MAXCPU)
979		return (EINVAL);
980
981	if (state < 0 || state >= X2APIC_STATE_LAST)
982		return (EINVAL);
983
984	vm->vcpu[vcpuid].x2apic_state = state;
985
986	vlapic_set_x2apic_state(vm, vcpuid, state);
987
988	return (0);
989}
990
991void
992vm_interrupt_hostcpu(struct vm *vm, int vcpuid)
993{
994	int hostcpu;
995	struct vcpu *vcpu;
996
997	vcpu = &vm->vcpu[vcpuid];
998
999	vcpu_lock(vcpu);
1000	hostcpu = vcpu->hostcpu;
1001	if (hostcpu == NOCPU) {
1002		/*
1003		 * If the vcpu is 'RUNNING' but without a valid 'hostcpu' then
1004		 * the host thread must be sleeping waiting for an event to
1005		 * kick the vcpu out of 'hlt'.
1006		 *
1007		 * XXX this is racy because the condition exists right before
1008		 * and after calling VMRUN() in vm_run(). The wakeup() is
1009		 * benign in this case.
1010		 */
1011		if (vcpu->state == VCPU_RUNNING)
1012			wakeup_one(vcpu);
1013	} else {
1014		if (vcpu->state != VCPU_RUNNING)
1015			panic("invalid vcpu state %d", vcpu->state);
1016		if (hostcpu != curcpu)
1017			ipi_cpu(hostcpu, vmm_ipinum);
1018	}
1019	vcpu_unlock(vcpu);
1020}
1021