xref: /freebsd/sys/amd64/include/vmm.h (revision 1eedb4e5)
1366f6083SPeter Grehan /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3c49761ddSPedro F. Giffuni  *
4366f6083SPeter Grehan  * Copyright (c) 2011 NetApp, Inc.
5366f6083SPeter Grehan  * All rights reserved.
6366f6083SPeter Grehan  *
7366f6083SPeter Grehan  * Redistribution and use in source and binary forms, with or without
8366f6083SPeter Grehan  * modification, are permitted provided that the following conditions
9366f6083SPeter Grehan  * are met:
10366f6083SPeter Grehan  * 1. Redistributions of source code must retain the above copyright
11366f6083SPeter Grehan  *    notice, this list of conditions and the following disclaimer.
12366f6083SPeter Grehan  * 2. Redistributions in binary form must reproduce the above copyright
13366f6083SPeter Grehan  *    notice, this list of conditions and the following disclaimer in the
14366f6083SPeter Grehan  *    documentation and/or other materials provided with the distribution.
15366f6083SPeter Grehan  *
16366f6083SPeter Grehan  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17366f6083SPeter Grehan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18366f6083SPeter Grehan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19366f6083SPeter Grehan  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20366f6083SPeter Grehan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21366f6083SPeter Grehan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22366f6083SPeter Grehan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23366f6083SPeter Grehan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24366f6083SPeter Grehan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25366f6083SPeter Grehan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26366f6083SPeter Grehan  * SUCH DAMAGE.
27366f6083SPeter Grehan  */
28366f6083SPeter Grehan 
29366f6083SPeter Grehan #ifndef _VMM_H_
30366f6083SPeter Grehan #define	_VMM_H_
31366f6083SPeter Grehan 
320bda8d3eSCorvin Köhne #include <sys/cpuset.h>
336ac73777STycho Nightingale #include <sys/sdt.h>
34d37f2adbSNeel Natu #include <x86/segments.h>
35d37f2adbSNeel Natu 
367d9ef309SJohn Baldwin struct vcpu;
37483d953aSJohn Baldwin struct vm_snapshot_meta;
38483d953aSJohn Baldwin 
396ac73777STycho Nightingale #ifdef _KERNEL
406ac73777STycho Nightingale SDT_PROVIDER_DECLARE(vmm);
416ac73777STycho Nightingale #endif
426ac73777STycho Nightingale 
43f0fdcfe2SNeel Natu enum vm_suspend_how {
44f0fdcfe2SNeel Natu 	VM_SUSPEND_NONE,
45f0fdcfe2SNeel Natu 	VM_SUSPEND_RESET,
46f0fdcfe2SNeel Natu 	VM_SUSPEND_POWEROFF,
47e50ce2aaSNeel Natu 	VM_SUSPEND_HALT,
48091d4532SNeel Natu 	VM_SUSPEND_TRIPLEFAULT,
49f0fdcfe2SNeel Natu 	VM_SUSPEND_LAST
50f0fdcfe2SNeel Natu };
51f0fdcfe2SNeel Natu 
525ebc578bSTycho Nightingale /*
535ebc578bSTycho Nightingale  * Identifiers for architecturally defined registers.
545ebc578bSTycho Nightingale  */
555ebc578bSTycho Nightingale enum vm_reg_name {
565ebc578bSTycho Nightingale 	VM_REG_GUEST_RAX,
575ebc578bSTycho Nightingale 	VM_REG_GUEST_RBX,
585ebc578bSTycho Nightingale 	VM_REG_GUEST_RCX,
595ebc578bSTycho Nightingale 	VM_REG_GUEST_RDX,
605ebc578bSTycho Nightingale 	VM_REG_GUEST_RSI,
615ebc578bSTycho Nightingale 	VM_REG_GUEST_RDI,
625ebc578bSTycho Nightingale 	VM_REG_GUEST_RBP,
635ebc578bSTycho Nightingale 	VM_REG_GUEST_R8,
645ebc578bSTycho Nightingale 	VM_REG_GUEST_R9,
655ebc578bSTycho Nightingale 	VM_REG_GUEST_R10,
665ebc578bSTycho Nightingale 	VM_REG_GUEST_R11,
675ebc578bSTycho Nightingale 	VM_REG_GUEST_R12,
685ebc578bSTycho Nightingale 	VM_REG_GUEST_R13,
695ebc578bSTycho Nightingale 	VM_REG_GUEST_R14,
705ebc578bSTycho Nightingale 	VM_REG_GUEST_R15,
715ebc578bSTycho Nightingale 	VM_REG_GUEST_CR0,
725ebc578bSTycho Nightingale 	VM_REG_GUEST_CR3,
735ebc578bSTycho Nightingale 	VM_REG_GUEST_CR4,
745ebc578bSTycho Nightingale 	VM_REG_GUEST_DR7,
755ebc578bSTycho Nightingale 	VM_REG_GUEST_RSP,
765ebc578bSTycho Nightingale 	VM_REG_GUEST_RIP,
775ebc578bSTycho Nightingale 	VM_REG_GUEST_RFLAGS,
785ebc578bSTycho Nightingale 	VM_REG_GUEST_ES,
795ebc578bSTycho Nightingale 	VM_REG_GUEST_CS,
805ebc578bSTycho Nightingale 	VM_REG_GUEST_SS,
815ebc578bSTycho Nightingale 	VM_REG_GUEST_DS,
825ebc578bSTycho Nightingale 	VM_REG_GUEST_FS,
835ebc578bSTycho Nightingale 	VM_REG_GUEST_GS,
845ebc578bSTycho Nightingale 	VM_REG_GUEST_LDTR,
855ebc578bSTycho Nightingale 	VM_REG_GUEST_TR,
865ebc578bSTycho Nightingale 	VM_REG_GUEST_IDTR,
875ebc578bSTycho Nightingale 	VM_REG_GUEST_GDTR,
885ebc578bSTycho Nightingale 	VM_REG_GUEST_EFER,
895ebc578bSTycho Nightingale 	VM_REG_GUEST_CR2,
903d5444c8SNeel Natu 	VM_REG_GUEST_PDPTE0,
913d5444c8SNeel Natu 	VM_REG_GUEST_PDPTE1,
923d5444c8SNeel Natu 	VM_REG_GUEST_PDPTE2,
933d5444c8SNeel Natu 	VM_REG_GUEST_PDPTE3,
94d1819632SNeel Natu 	VM_REG_GUEST_INTR_SHADOW,
9565eefbe4SJohn Baldwin 	VM_REG_GUEST_DR0,
9665eefbe4SJohn Baldwin 	VM_REG_GUEST_DR1,
9765eefbe4SJohn Baldwin 	VM_REG_GUEST_DR2,
9865eefbe4SJohn Baldwin 	VM_REG_GUEST_DR3,
9965eefbe4SJohn Baldwin 	VM_REG_GUEST_DR6,
100cbd03a9dSJohn Baldwin 	VM_REG_GUEST_ENTRY_INST_LENGTH,
101f493ea65SMark Johnston 	VM_REG_GUEST_FS_BASE,
102f493ea65SMark Johnston 	VM_REG_GUEST_GS_BASE,
103f493ea65SMark Johnston 	VM_REG_GUEST_KGS_BASE,
104f493ea65SMark Johnston 	VM_REG_GUEST_TPR,
1055ebc578bSTycho Nightingale 	VM_REG_LAST
1065ebc578bSTycho Nightingale };
1075ebc578bSTycho Nightingale 
1085ebc578bSTycho Nightingale enum x2apic_state {
1095ebc578bSTycho Nightingale 	X2APIC_DISABLED,
1105ebc578bSTycho Nightingale 	X2APIC_ENABLED,
1115ebc578bSTycho Nightingale 	X2APIC_STATE_LAST
1125ebc578bSTycho Nightingale };
1135ebc578bSTycho Nightingale 
114091d4532SNeel Natu #define	VM_INTINFO_VECTOR(info)	((info) & 0xff)
115091d4532SNeel Natu #define	VM_INTINFO_DEL_ERRCODE	0x800
116091d4532SNeel Natu #define	VM_INTINFO_RSVD		0x7ffff000
117091d4532SNeel Natu #define	VM_INTINFO_VALID	0x80000000
118091d4532SNeel Natu #define	VM_INTINFO_TYPE		0x700
119091d4532SNeel Natu #define	VM_INTINFO_HWINTR	(0 << 8)
120091d4532SNeel Natu #define	VM_INTINFO_NMI		(2 << 8)
121091d4532SNeel Natu #define	VM_INTINFO_HWEXCEPTION	(3 << 8)
122091d4532SNeel Natu #define	VM_INTINFO_SWINTR	(4 << 8)
123091d4532SNeel Natu 
124422a8a4dSScott Long /*
125422a8a4dSScott Long  * The VM name has to fit into the pathname length constraints of devfs,
126422a8a4dSScott Long  * governed primarily by SPECNAMELEN.  The length is the total number of
127422a8a4dSScott Long  * characters in the full path, relative to the mount point and not
128422a8a4dSScott Long  * including any leading '/' characters.
129422a8a4dSScott Long  * A prefix and a suffix are added to the name specified by the user.
130422a8a4dSScott Long  * The prefix is usually "vmm/" or "vmm.io/", but can be a few characters
131422a8a4dSScott Long  * longer for future use.
132422a8a4dSScott Long  * The suffix is a string that identifies a bootrom image or some similar
133422a8a4dSScott Long  * image that is attached to the VM. A separator character gets added to
134422a8a4dSScott Long  * the suffix automatically when generating the full path, so it must be
135422a8a4dSScott Long  * accounted for, reducing the effective length by 1.
136422a8a4dSScott Long  * The effective length of a VM name is 229 bytes for FreeBSD 13 and 37
137422a8a4dSScott Long  * bytes for FreeBSD 12.  A minimum length is set for safety and supports
138422a8a4dSScott Long  * a SPECNAMELEN as small as 32 on old systems.
139422a8a4dSScott Long  */
140422a8a4dSScott Long #define VM_MAX_PREFIXLEN 10
141422a8a4dSScott Long #define VM_MAX_SUFFIXLEN 15
142422a8a4dSScott Long #define VM_MIN_NAMELEN   6
143422a8a4dSScott Long #define VM_MAX_NAMELEN \
144422a8a4dSScott Long     (SPECNAMELEN - VM_MAX_PREFIXLEN - VM_MAX_SUFFIXLEN - 1)
145366f6083SPeter Grehan 
146422a8a4dSScott Long #ifdef _KERNEL
147422a8a4dSScott Long CTASSERT(VM_MAX_NAMELEN >= VM_MIN_NAMELEN);
148366f6083SPeter Grehan 
149366f6083SPeter Grehan struct vm;
150dc506506SNeel Natu struct vm_exception;
151366f6083SPeter Grehan struct seg_desc;
152366f6083SPeter Grehan struct vm_exit;
153366f6083SPeter Grehan struct vm_run;
15408e3ff32SNeel Natu struct vhpet;
155565bbb86SNeel Natu struct vioapic;
156366f6083SPeter Grehan struct vlapic;
157318224bbSNeel Natu struct vmspace;
158318224bbSNeel Natu struct vm_object;
159d665d229SNeel Natu struct vm_guest_paging;
160318224bbSNeel Natu struct pmap;
161483d953aSJohn Baldwin enum snapshot_req;
162366f6083SPeter Grehan 
163248e6799SNeel Natu struct vm_eventinfo {
164892feec2SCorvin Köhne 	cpuset_t *rptr;		/* rendezvous cookie */
165248e6799SNeel Natu 	int	*sptr;		/* suspend cookie */
166248e6799SNeel Natu 	int	*iptr;		/* reqidle cookie */
167248e6799SNeel Natu };
168248e6799SNeel Natu 
169add611fdSNeel Natu typedef int	(*vmm_init_func_t)(int ipinum);
170366f6083SPeter Grehan typedef int	(*vmm_cleanup_func_t)(void);
17163e62d39SJohn Baldwin typedef void	(*vmm_resume_func_t)(void);
172318224bbSNeel Natu typedef void *	(*vmi_init_func_t)(struct vm *vm, struct pmap *pmap);
173869c8d19SJohn Baldwin typedef int	(*vmi_run_func_t)(void *vcpui, register_t rip,
174248e6799SNeel Natu 		    struct pmap *pmap, struct vm_eventinfo *info);
175366f6083SPeter Grehan typedef void	(*vmi_cleanup_func_t)(void *vmi);
176950af9ffSJohn Baldwin typedef void *	(*vmi_vcpu_init_func_t)(void *vmi, struct vcpu *vcpu,
177950af9ffSJohn Baldwin 		    int vcpu_id);
178869c8d19SJohn Baldwin typedef void	(*vmi_vcpu_cleanup_func_t)(void *vcpui);
179869c8d19SJohn Baldwin typedef int	(*vmi_get_register_t)(void *vcpui, int num, uint64_t *retval);
180869c8d19SJohn Baldwin typedef int	(*vmi_set_register_t)(void *vcpui, int num, uint64_t val);
181869c8d19SJohn Baldwin typedef int	(*vmi_get_desc_t)(void *vcpui, int num, struct seg_desc *desc);
182869c8d19SJohn Baldwin typedef int	(*vmi_set_desc_t)(void *vcpui, int num, struct seg_desc *desc);
183869c8d19SJohn Baldwin typedef int	(*vmi_get_cap_t)(void *vcpui, int num, int *retval);
184869c8d19SJohn Baldwin typedef int	(*vmi_set_cap_t)(void *vcpui, int num, int val);
185318224bbSNeel Natu typedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max);
186318224bbSNeel Natu typedef void	(*vmi_vmspace_free)(struct vmspace *vmspace);
187869c8d19SJohn Baldwin typedef struct vlapic * (*vmi_vlapic_init)(void *vcpui);
188869c8d19SJohn Baldwin typedef void	(*vmi_vlapic_cleanup)(struct vlapic *vlapic);
189869c8d19SJohn Baldwin typedef int	(*vmi_snapshot_vcpu_t)(void *vcpui, struct vm_snapshot_meta *meta);
190869c8d19SJohn Baldwin typedef int	(*vmi_restore_tsc_t)(void *vcpui, uint64_t now);
191366f6083SPeter Grehan 
192366f6083SPeter Grehan struct vmm_ops {
19315add60dSPeter Grehan 	vmm_init_func_t		modinit;	/* module wide initialization */
19415add60dSPeter Grehan 	vmm_cleanup_func_t	modcleanup;
19515add60dSPeter Grehan 	vmm_resume_func_t	modresume;
196366f6083SPeter Grehan 
19715add60dSPeter Grehan 	vmi_init_func_t		init;		/* vm-specific initialization */
19815add60dSPeter Grehan 	vmi_run_func_t		run;
19915add60dSPeter Grehan 	vmi_cleanup_func_t	cleanup;
2001aa51504SJohn Baldwin 	vmi_vcpu_init_func_t	vcpu_init;
2011aa51504SJohn Baldwin 	vmi_vcpu_cleanup_func_t	vcpu_cleanup;
20215add60dSPeter Grehan 	vmi_get_register_t	getreg;
20315add60dSPeter Grehan 	vmi_set_register_t	setreg;
20415add60dSPeter Grehan 	vmi_get_desc_t		getdesc;
20515add60dSPeter Grehan 	vmi_set_desc_t		setdesc;
20615add60dSPeter Grehan 	vmi_get_cap_t		getcap;
20715add60dSPeter Grehan 	vmi_set_cap_t		setcap;
208318224bbSNeel Natu 	vmi_vmspace_alloc	vmspace_alloc;
209318224bbSNeel Natu 	vmi_vmspace_free	vmspace_free;
210de5ea6b6SNeel Natu 	vmi_vlapic_init		vlapic_init;
211de5ea6b6SNeel Natu 	vmi_vlapic_cleanup	vlapic_cleanup;
212483d953aSJohn Baldwin 
213483d953aSJohn Baldwin 	/* checkpoint operations */
21439ec056eSJohn Baldwin 	vmi_snapshot_vcpu_t	vcpu_snapshot;
21515add60dSPeter Grehan 	vmi_restore_tsc_t	restore_tsc;
216366f6083SPeter Grehan };
217366f6083SPeter Grehan 
21815add60dSPeter Grehan extern const struct vmm_ops vmm_ops_intel;
21915add60dSPeter Grehan extern const struct vmm_ops vmm_ops_amd;
220366f6083SPeter Grehan 
221ee98f99dSJohn Baldwin extern u_int vm_maxcpu;			/* maximum virtual cpus */
222ee98f99dSJohn Baldwin 
223d5408b1dSNeel Natu int vm_create(const char *name, struct vm **retvm);
22498568a00SJohn Baldwin struct vcpu *vm_alloc_vcpu(struct vm *vm, int vcpuid);
22598568a00SJohn Baldwin void vm_disable_vcpu_creation(struct vm *vm);
22698568a00SJohn Baldwin void vm_slock_vcpus(struct vm *vm);
22798568a00SJohn Baldwin void vm_unlock_vcpus(struct vm *vm);
228366f6083SPeter Grehan void vm_destroy(struct vm *vm);
2295fcf252fSNeel Natu int vm_reinit(struct vm *vm);
230366f6083SPeter Grehan const char *vm_name(struct vm *vm);
231a488c9c9SRodney W. Grimes uint16_t vm_get_maxcpus(struct vm *vm);
23201d822d3SRodney W. Grimes void vm_get_topology(struct vm *vm, uint16_t *sockets, uint16_t *cores,
23301d822d3SRodney W. Grimes     uint16_t *threads, uint16_t *maxcpus);
23401d822d3SRodney W. Grimes int vm_set_topology(struct vm *vm, uint16_t sockets, uint16_t cores,
23501d822d3SRodney W. Grimes     uint16_t threads, uint16_t maxcpus);
2369b1aa8d6SNeel Natu 
2379b1aa8d6SNeel Natu /*
2389b1aa8d6SNeel Natu  * APIs that modify the guest memory map require all vcpus to be frozen.
2399b1aa8d6SNeel Natu  */
24067b69e76SJohn Baldwin void vm_slock_memsegs(struct vm *vm);
24167b69e76SJohn Baldwin void vm_xlock_memsegs(struct vm *vm);
24267b69e76SJohn Baldwin void vm_unlock_memsegs(struct vm *vm);
2439b1aa8d6SNeel Natu int vm_mmap_memseg(struct vm *vm, vm_paddr_t gpa, int segid, vm_ooffset_t off,
2449b1aa8d6SNeel Natu     size_t len, int prot, int flags);
245f8a6ec2dSD Scott Phillips int vm_munmap_memseg(struct vm *vm, vm_paddr_t gpa, size_t len);
2469b1aa8d6SNeel Natu int vm_alloc_memseg(struct vm *vm, int ident, size_t len, bool sysmem);
2479b1aa8d6SNeel Natu void vm_free_memseg(struct vm *vm, int ident);
248366f6083SPeter Grehan int vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
249366f6083SPeter Grehan int vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
2509b1aa8d6SNeel Natu int vm_assign_pptdev(struct vm *vm, int bus, int slot, int func);
2519b1aa8d6SNeel Natu int vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func);
2529b1aa8d6SNeel Natu 
2539b1aa8d6SNeel Natu /*
2549b1aa8d6SNeel Natu  * APIs that inspect the guest memory map require only a *single* vcpu to
2559b1aa8d6SNeel Natu  * be frozen. This acts like a read lock on the guest memory map since any
2569b1aa8d6SNeel Natu  * modification requires *all* vcpus to be frozen.
2579b1aa8d6SNeel Natu  */
2589b1aa8d6SNeel Natu int vm_mmap_getnext(struct vm *vm, vm_paddr_t *gpa, int *segid,
2599b1aa8d6SNeel Natu     vm_ooffset_t *segoff, size_t *len, int *prot, int *flags);
2609b1aa8d6SNeel Natu int vm_get_memseg(struct vm *vm, int ident, size_t *len, bool *sysmem,
2619b1aa8d6SNeel Natu     struct vm_object **objptr);
262147d12a7SAntoine Brodin vm_paddr_t vmm_sysmem_maxaddr(struct vm *vm);
263d3956e46SJohn Baldwin void *vm_gpa_hold(struct vcpu *vcpu, vm_paddr_t gpa, size_t len,
264d3956e46SJohn Baldwin     int prot, void **cookie);
265d3956e46SJohn Baldwin void *vm_gpa_hold_global(struct vm *vm, vm_paddr_t gpa, size_t len,
2669b1aa8d6SNeel Natu     int prot, void **cookie);
267318224bbSNeel Natu void vm_gpa_release(void *cookie);
26880cb5d84SJohn Baldwin bool vm_mem_allocated(struct vcpu *vcpu, vm_paddr_t gpa);
2699b1aa8d6SNeel Natu 
270d3956e46SJohn Baldwin int vm_get_register(struct vcpu *vcpu, int reg, uint64_t *retval);
271d3956e46SJohn Baldwin int vm_set_register(struct vcpu *vcpu, int reg, uint64_t val);
272d3956e46SJohn Baldwin int vm_get_seg_desc(struct vcpu *vcpu, int reg,
273366f6083SPeter Grehan 		    struct seg_desc *ret_desc);
2743f0f4b15SJohn Baldwin int vm_set_seg_desc(struct vcpu *vcpu, int reg,
275366f6083SPeter Grehan 		    struct seg_desc *desc);
276e17eca32SMark Johnston int vm_run(struct vcpu *vcpu);
277f0fdcfe2SNeel Natu int vm_suspend(struct vm *vm, enum vm_suspend_how how);
2783f0f4b15SJohn Baldwin int vm_inject_nmi(struct vcpu *vcpu);
27980cb5d84SJohn Baldwin int vm_nmi_pending(struct vcpu *vcpu);
28080cb5d84SJohn Baldwin void vm_nmi_clear(struct vcpu *vcpu);
2813f0f4b15SJohn Baldwin int vm_inject_extint(struct vcpu *vcpu);
28280cb5d84SJohn Baldwin int vm_extint_pending(struct vcpu *vcpu);
28380cb5d84SJohn Baldwin void vm_extint_clear(struct vcpu *vcpu);
284950af9ffSJohn Baldwin int vcpu_vcpuid(struct vcpu *vcpu);
285950af9ffSJohn Baldwin struct vm *vcpu_vm(struct vcpu *vcpu);
286950af9ffSJohn Baldwin struct vcpu *vm_vcpu(struct vm *vm, int cpu);
287d3956e46SJohn Baldwin struct vlapic *vm_lapic(struct vcpu *vcpu);
288565bbb86SNeel Natu struct vioapic *vm_ioapic(struct vm *vm);
28908e3ff32SNeel Natu struct vhpet *vm_hpet(struct vm *vm);
2903f0f4b15SJohn Baldwin int vm_get_capability(struct vcpu *vcpu, int type, int *val);
2913f0f4b15SJohn Baldwin int vm_set_capability(struct vcpu *vcpu, int type, int val);
2923f0f4b15SJohn Baldwin int vm_get_x2apic_state(struct vcpu *vcpu, enum x2apic_state *state);
2933f0f4b15SJohn Baldwin int vm_set_x2apic_state(struct vcpu *vcpu, enum x2apic_state state);
294565bbb86SNeel Natu int vm_apicid2vcpuid(struct vm *vm, int apicid);
2953f0f4b15SJohn Baldwin int vm_activate_cpu(struct vcpu *vcpu);
2963f0f4b15SJohn Baldwin int vm_suspend_cpu(struct vm *vm, struct vcpu *vcpu);
2973f0f4b15SJohn Baldwin int vm_resume_cpu(struct vm *vm, struct vcpu *vcpu);
298d3956e46SJohn Baldwin int vm_restart_instruction(struct vcpu *vcpu);
29980cb5d84SJohn Baldwin struct vm_exit *vm_exitinfo(struct vcpu *vcpu);
300e17eca32SMark Johnston cpuset_t *vm_exitinfo_cpuset(struct vcpu *vcpu);
30180cb5d84SJohn Baldwin void vm_exit_suspended(struct vcpu *vcpu, uint64_t rip);
30280cb5d84SJohn Baldwin void vm_exit_debug(struct vcpu *vcpu, uint64_t rip);
30380cb5d84SJohn Baldwin void vm_exit_rendezvous(struct vcpu *vcpu, uint64_t rip);
30480cb5d84SJohn Baldwin void vm_exit_astpending(struct vcpu *vcpu, uint64_t rip);
30580cb5d84SJohn Baldwin void vm_exit_reqidle(struct vcpu *vcpu, uint64_t rip);
306483d953aSJohn Baldwin int vm_snapshot_req(struct vm *vm, struct vm_snapshot_meta *meta);
307483d953aSJohn Baldwin int vm_restore_time(struct vm *vm);
308483d953aSJohn Baldwin 
3098325ce5cSNeel Natu #ifdef _SYS__CPUSET_H_
310366f6083SPeter Grehan /*
3115b8a8cd1SNeel Natu  * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'.
3125b8a8cd1SNeel Natu  * The rendezvous 'func(arg)' is not allowed to do anything that will
3135b8a8cd1SNeel Natu  * cause the thread to be put to sleep.
3145b8a8cd1SNeel Natu  *
3155b8a8cd1SNeel Natu  * The caller cannot hold any locks when initiating the rendezvous.
3165b8a8cd1SNeel Natu  *
3175b8a8cd1SNeel Natu  * The implementation of this API may cause vcpus other than those specified
3185b8a8cd1SNeel Natu  * by 'dest' to be stalled. The caller should not rely on any vcpus making
3195b8a8cd1SNeel Natu  * forward progress when the rendezvous is in progress.
3205b8a8cd1SNeel Natu  */
321d8be3d52SJohn Baldwin typedef void (*vm_rendezvous_func_t)(struct vcpu *vcpu, void *arg);
322d8be3d52SJohn Baldwin int vm_smp_rendezvous(struct vcpu *vcpu, cpuset_t dest,
3235b8a8cd1SNeel Natu     vm_rendezvous_func_t func, void *arg);
324c0f35dbfSJohn Baldwin 
3258325ce5cSNeel Natu cpuset_t vm_active_cpus(struct vm *vm);
326fc276d92SJohn Baldwin cpuset_t vm_debug_cpus(struct vm *vm);
3278325ce5cSNeel Natu cpuset_t vm_suspended_cpus(struct vm *vm);
328c0f35dbfSJohn Baldwin cpuset_t vm_start_cpus(struct vm *vm, const cpuset_t *tostart);
329c0f35dbfSJohn Baldwin void vm_await_start(struct vm *vm, const cpuset_t *waiting);
3308325ce5cSNeel Natu #endif	/* _SYS__CPUSET_H_ */
3315b8a8cd1SNeel Natu 
3325b8a8cd1SNeel Natu static __inline int
vcpu_rendezvous_pending(struct vcpu * vcpu,struct vm_eventinfo * info)333892feec2SCorvin Köhne vcpu_rendezvous_pending(struct vcpu *vcpu, struct vm_eventinfo *info)
3345b8a8cd1SNeel Natu {
335892feec2SCorvin Köhne 	/*
336892feec2SCorvin Köhne 	 * This check isn't done with atomic operations or under a lock because
337892feec2SCorvin Köhne 	 * there's no need to. If the vcpuid bit is set, the vcpu is part of a
338892feec2SCorvin Köhne 	 * rendezvous and the bit won't be cleared until the vcpu enters the
339892feec2SCorvin Köhne 	 * rendezvous. On rendezvous exit, the cpuset is cleared and the vcpu
340892feec2SCorvin Köhne 	 * will see an empty cpuset. So, the races are harmless.
341892feec2SCorvin Köhne 	 */
342892feec2SCorvin Köhne 	return (CPU_ISSET(vcpu_vcpuid(vcpu), info->rptr));
3435b8a8cd1SNeel Natu }
3445b8a8cd1SNeel Natu 
345b15a09c0SNeel Natu static __inline int
vcpu_suspended(struct vm_eventinfo * info)346248e6799SNeel Natu vcpu_suspended(struct vm_eventinfo *info)
347b15a09c0SNeel Natu {
348b15a09c0SNeel Natu 
349248e6799SNeel Natu 	return (*info->sptr);
350248e6799SNeel Natu }
351248e6799SNeel Natu 
352248e6799SNeel Natu static __inline int
vcpu_reqidle(struct vm_eventinfo * info)353248e6799SNeel Natu vcpu_reqidle(struct vm_eventinfo *info)
354248e6799SNeel Natu {
355248e6799SNeel Natu 
356248e6799SNeel Natu 	return (*info->iptr);
357b15a09c0SNeel Natu }
358b15a09c0SNeel Natu 
35980cb5d84SJohn Baldwin int vcpu_debugged(struct vcpu *vcpu);
360fc276d92SJohn Baldwin 
3615b8a8cd1SNeel Natu /*
362490d56c5SEd Maste  * Return true if device indicated by bus/slot/func is supposed to be a
363366f6083SPeter Grehan  * pci passthrough device.
364366f6083SPeter Grehan  *
365490d56c5SEd Maste  * Return false otherwise.
366366f6083SPeter Grehan  */
367490d56c5SEd Maste bool vmm_is_pptdev(int bus, int slot, int func);
368366f6083SPeter Grehan 
369366f6083SPeter Grehan void *vm_iommu_domain(struct vm *vm);
370366f6083SPeter Grehan 
37175dd3366SNeel Natu enum vcpu_state {
37275dd3366SNeel Natu 	VCPU_IDLE,
373318224bbSNeel Natu 	VCPU_FROZEN,
37475dd3366SNeel Natu 	VCPU_RUNNING,
375318224bbSNeel Natu 	VCPU_SLEEPING,
37675dd3366SNeel Natu };
377366f6083SPeter Grehan 
3783f0f4b15SJohn Baldwin int vcpu_set_state(struct vcpu *vcpu, enum vcpu_state state, bool from_idle);
379d3956e46SJohn Baldwin enum vcpu_state vcpu_get_state(struct vcpu *vcpu, int *hostcpu);
380366f6083SPeter Grehan 
381366f6083SPeter Grehan static int __inline
vcpu_is_running(struct vcpu * vcpu,int * hostcpu)38280cb5d84SJohn Baldwin vcpu_is_running(struct vcpu *vcpu, int *hostcpu)
383366f6083SPeter Grehan {
38480cb5d84SJohn Baldwin 	return (vcpu_get_state(vcpu, hostcpu) == VCPU_RUNNING);
385366f6083SPeter Grehan }
386366f6083SPeter Grehan 
387f008d157SNeel Natu #ifdef _SYS_PROC_H_
388f008d157SNeel Natu static int __inline
vcpu_should_yield(struct vcpu * vcpu)38980cb5d84SJohn Baldwin vcpu_should_yield(struct vcpu *vcpu)
390f008d157SNeel Natu {
391c6d31b83SKonstantin Belousov 	struct thread *td;
392ede04033SNeel Natu 
393c6d31b83SKonstantin Belousov 	td = curthread;
394c6d31b83SKonstantin Belousov 	return (td->td_ast != 0 || td->td_owepreempt != 0);
395f008d157SNeel Natu }
396f008d157SNeel Natu #endif
397f008d157SNeel Natu 
3983dc3d32aSJohn Baldwin void *vcpu_stats(struct vcpu *vcpu);
3993f0f4b15SJohn Baldwin void vcpu_notify_event(struct vcpu *vcpu, bool lapic_intr);
400318224bbSNeel Natu struct vmspace *vm_get_vmspace(struct vm *vm);
401762fd208STycho Nightingale struct vatpic *vm_atpic(struct vm *vm);
402e883c9bbSTycho Nightingale struct vatpit *vm_atpit(struct vm *vm);
403160ef77aSNeel Natu struct vpmtmr *vm_pmtmr(struct vm *vm);
4040dafa5cdSNeel Natu struct vrtc *vm_rtc(struct vm *vm);
405dc506506SNeel Natu 
406dc506506SNeel Natu /*
407c9c75df4SNeel Natu  * Inject exception 'vector' into the guest vcpu. This function returns 0 on
408dc506506SNeel Natu  * success and non-zero on failure.
409dc506506SNeel Natu  *
410dc506506SNeel Natu  * Wrapper functions like 'vm_inject_gp()' should be preferred to calling
411dc506506SNeel Natu  * this function directly because they enforce the trap-like or fault-like
412dc506506SNeel Natu  * behavior of an exception.
413dc506506SNeel Natu  *
414dc506506SNeel Natu  * This function should only be called in the context of the thread that is
415dc506506SNeel Natu  * executing this vcpu.
416dc506506SNeel Natu  */
417d3956e46SJohn Baldwin int vm_inject_exception(struct vcpu *vcpu, int vector, int err_valid,
418c9c75df4SNeel Natu     uint32_t errcode, int restart_instruction);
419dc506506SNeel Natu 
420dc506506SNeel Natu /*
421091d4532SNeel Natu  * This function is called after a VM-exit that occurred during exception or
422091d4532SNeel Natu  * interrupt delivery through the IDT. The format of 'intinfo' is described
423091d4532SNeel Natu  * in Figure 15-1, "EXITINTINFO for All Intercepts", APM, Vol 2.
424dc506506SNeel Natu  *
425091d4532SNeel Natu  * If a VM-exit handler completes the event delivery successfully then it
426091d4532SNeel Natu  * should call vm_exit_intinfo() to extinguish the pending event. For e.g.,
427091d4532SNeel Natu  * if the task switch emulation is triggered via a task gate then it should
428091d4532SNeel Natu  * call this function with 'intinfo=0' to indicate that the external event
429091d4532SNeel Natu  * is not pending anymore.
430091d4532SNeel Natu  *
431091d4532SNeel Natu  * Return value is 0 on success and non-zero on failure.
432dc506506SNeel Natu  */
43380cb5d84SJohn Baldwin int vm_exit_intinfo(struct vcpu *vcpu, uint64_t intinfo);
434091d4532SNeel Natu 
435091d4532SNeel Natu /*
436091d4532SNeel Natu  * This function is called before every VM-entry to retrieve a pending
437091d4532SNeel Natu  * event that should be injected into the guest. This function combines
438091d4532SNeel Natu  * nested events into a double or triple fault.
439091d4532SNeel Natu  *
440091d4532SNeel Natu  * Returns 0 if there are no events that need to be injected into the guest
441091d4532SNeel Natu  * and non-zero otherwise.
442091d4532SNeel Natu  */
44380cb5d84SJohn Baldwin int vm_entry_intinfo(struct vcpu *vcpu, uint64_t *info);
444091d4532SNeel Natu 
4453f0f4b15SJohn Baldwin int vm_get_intinfo(struct vcpu *vcpu, uint64_t *info1, uint64_t *info2);
446dc506506SNeel Natu 
447483d953aSJohn Baldwin /*
448483d953aSJohn Baldwin  * Function used to keep track of the guest's TSC offset. The
449*1eedb4e5SElyes Haouas  * offset is used by the virtualization extensions to provide a consistent
450483d953aSJohn Baldwin  * value for the Time Stamp Counter to the guest.
451483d953aSJohn Baldwin  */
45280cb5d84SJohn Baldwin void vm_set_tsc_offset(struct vcpu *vcpu, uint64_t offset);
453483d953aSJohn Baldwin 
454d17b5104SNeel Natu enum vm_reg_name vm_segment_name(int seg_encoding);
455d17b5104SNeel Natu 
456d665d229SNeel Natu struct vm_copyinfo {
457d665d229SNeel Natu 	uint64_t	gpa;
458d665d229SNeel Natu 	size_t		len;
459d665d229SNeel Natu 	void		*hva;
460d665d229SNeel Natu 	void		*cookie;
461d665d229SNeel Natu };
462d665d229SNeel Natu 
463d665d229SNeel Natu /*
464d665d229SNeel Natu  * Set up 'copyinfo[]' to copy to/from guest linear address space starting
465d665d229SNeel Natu  * at 'gla' and 'len' bytes long. The 'prot' should be set to PROT_READ for
466d665d229SNeel Natu  * a copyin or PROT_WRITE for a copyout.
467d665d229SNeel Natu  *
468edafb5a3SPedro F. Giffuni  * retval	is_fault	Interpretation
4699c4d5478SNeel Natu  *   0		   0		Success
4709c4d5478SNeel Natu  *   0		   1		An exception was injected into the guest
4719c4d5478SNeel Natu  * EFAULT	  N/A		Unrecoverable error
472d665d229SNeel Natu  *
473d665d229SNeel Natu  * The 'copyinfo[]' can be passed to 'vm_copyin()' or 'vm_copyout()' only if
474d665d229SNeel Natu  * the return value is 0. The 'copyinfo[]' resources should be freed by calling
475d665d229SNeel Natu  * 'vm_copy_teardown()' after the copy is done.
476d665d229SNeel Natu  */
477d3956e46SJohn Baldwin int vm_copy_setup(struct vcpu *vcpu, struct vm_guest_paging *paging,
478d665d229SNeel Natu     uint64_t gla, size_t len, int prot, struct vm_copyinfo *copyinfo,
4799c4d5478SNeel Natu     int num_copyinfo, int *is_fault);
4802b4fe856SJohn Baldwin void vm_copy_teardown(struct vm_copyinfo *copyinfo, int num_copyinfo);
4812b4fe856SJohn Baldwin void vm_copyin(struct vm_copyinfo *copyinfo, void *kaddr, size_t len);
4822b4fe856SJohn Baldwin void vm_copyout(const void *kaddr, struct vm_copyinfo *copyinfo, size_t len);
483b0538143SNeel Natu 
48480cb5d84SJohn Baldwin int vcpu_trace_exceptions(struct vcpu *vcpu);
48580cb5d84SJohn Baldwin int vcpu_trap_wbinvd(struct vcpu *vcpu);
486366f6083SPeter Grehan #endif	/* KERNEL */
487366f6083SPeter Grehan 
488366f6083SPeter Grehan /*
489366f6083SPeter Grehan  * Identifiers for optional vmm capabilities
490366f6083SPeter Grehan  */
491366f6083SPeter Grehan enum vm_cap_type {
492366f6083SPeter Grehan 	VM_CAP_HALT_EXIT,
493366f6083SPeter Grehan 	VM_CAP_MTRAP_EXIT,
494366f6083SPeter Grehan 	VM_CAP_PAUSE_EXIT,
495366f6083SPeter Grehan 	VM_CAP_UNRESTRICTED_GUEST,
49649cc03daSNeel Natu 	VM_CAP_ENABLE_INVPCID,
497cbd03a9dSJohn Baldwin 	VM_CAP_BPT_EXIT,
498f5f5f1e7SPeter Grehan 	VM_CAP_RDPID,
499f5f5f1e7SPeter Grehan 	VM_CAP_RDTSCP,
5000bda8d3eSCorvin Köhne 	VM_CAP_IPI_EXIT,
501fefac543SBojan Novković 	VM_CAP_MASK_HWINTR,
502e3b4fe64SBojan Novković 	VM_CAP_RFLAGS_TF,
503366f6083SPeter Grehan 	VM_CAP_MAX
504366f6083SPeter Grehan };
505366f6083SPeter Grehan 
506b3e9732aSJohn Baldwin enum vm_intr_trigger {
507b3e9732aSJohn Baldwin 	EDGE_TRIGGER,
508b3e9732aSJohn Baldwin 	LEVEL_TRIGGER
509b3e9732aSJohn Baldwin };
510b3e9732aSJohn Baldwin 
511366f6083SPeter Grehan /*
512366f6083SPeter Grehan  * The 'access' field has the format specified in Table 21-2 of the Intel
513366f6083SPeter Grehan  * Architecture Manual vol 3b.
514366f6083SPeter Grehan  *
515366f6083SPeter Grehan  * XXX The contents of the 'access' field are architecturally defined except
516366f6083SPeter Grehan  * bit 16 - Segment Unusable.
517366f6083SPeter Grehan  */
518366f6083SPeter Grehan struct seg_desc {
519366f6083SPeter Grehan 	uint64_t	base;
520366f6083SPeter Grehan 	uint32_t	limit;
521366f6083SPeter Grehan 	uint32_t	access;
522366f6083SPeter Grehan };
523f7a9f178SNeel Natu #define	SEG_DESC_TYPE(access)		((access) & 0x001f)
5243d5444c8SNeel Natu #define	SEG_DESC_DPL(access)		(((access) >> 5) & 0x3)
525f7a9f178SNeel Natu #define	SEG_DESC_PRESENT(access)	(((access) & 0x0080) ? 1 : 0)
526f7a9f178SNeel Natu #define	SEG_DESC_DEF32(access)		(((access) & 0x4000) ? 1 : 0)
527f7a9f178SNeel Natu #define	SEG_DESC_GRANULARITY(access)	(((access) & 0x8000) ? 1 : 0)
528f7a9f178SNeel Natu #define	SEG_DESC_UNUSABLE(access)	(((access) & 0x10000) ? 1 : 0)
529366f6083SPeter Grehan 
530e813a873SNeel Natu enum vm_cpu_mode {
531b301b9e2SNeel Natu 	CPU_MODE_REAL,
532b301b9e2SNeel Natu 	CPU_MODE_PROTECTED,
533e813a873SNeel Natu 	CPU_MODE_COMPATIBILITY,		/* IA-32E mode (CS.L = 0) */
534e813a873SNeel Natu 	CPU_MODE_64BIT,			/* IA-32E mode (CS.L = 1) */
535e813a873SNeel Natu };
536e813a873SNeel Natu 
537e813a873SNeel Natu enum vm_paging_mode {
538e813a873SNeel Natu 	PAGING_MODE_FLAT,
539e813a873SNeel Natu 	PAGING_MODE_32,
540e813a873SNeel Natu 	PAGING_MODE_PAE,
541e813a873SNeel Natu 	PAGING_MODE_64,
542f3eb12e4SKonstantin Belousov 	PAGING_MODE_64_LA57,
543e813a873SNeel Natu };
544e813a873SNeel Natu 
545e813a873SNeel Natu struct vm_guest_paging {
546e813a873SNeel Natu 	uint64_t	cr3;
547e813a873SNeel Natu 	int		cpl;
548e813a873SNeel Natu 	enum vm_cpu_mode cpu_mode;
549e813a873SNeel Natu 	enum vm_paging_mode paging_mode;
550e813a873SNeel Natu };
551e813a873SNeel Natu 
552e813a873SNeel Natu /*
553e813a873SNeel Natu  * The data structures 'vie' and 'vie_op' are meant to be opaque to the
554e813a873SNeel Natu  * consumers of instruction decoding. The only reason why their contents
555e813a873SNeel Natu  * need to be exposed is because they are part of the 'vm_exit' structure.
556e813a873SNeel Natu  */
557e813a873SNeel Natu struct vie_op {
558e813a873SNeel Natu 	uint8_t		op_byte;	/* actual opcode byte */
559e813a873SNeel Natu 	uint8_t		op_type;	/* type of operation (e.g. MOV) */
560e813a873SNeel Natu 	uint16_t	op_flags;
561e813a873SNeel Natu };
562497cb925SConrad Meyer _Static_assert(sizeof(struct vie_op) == 4, "ABI");
563497cb925SConrad Meyer _Static_assert(_Alignof(struct vie_op) == 2, "ABI");
564e813a873SNeel Natu 
565e813a873SNeel Natu #define	VIE_INST_SIZE	15
566e813a873SNeel Natu struct vie {
567e813a873SNeel Natu 	uint8_t		inst[VIE_INST_SIZE];	/* instruction bytes */
568e813a873SNeel Natu 	uint8_t		num_valid;		/* size of the instruction */
5694daa95f8SConrad Meyer 
5704daa95f8SConrad Meyer /* The following fields are all zeroed upon restart. */
5714daa95f8SConrad Meyer #define	vie_startzero	num_processed
572e813a873SNeel Natu 	uint8_t		num_processed;
573e813a873SNeel Natu 
574f7a9f178SNeel Natu 	uint8_t		addrsize:4, opsize:4;	/* address and operand sizes */
575e813a873SNeel Natu 	uint8_t		rex_w:1,		/* REX prefix */
576e813a873SNeel Natu 			rex_r:1,
577e813a873SNeel Natu 			rex_x:1,
578e813a873SNeel Natu 			rex_b:1,
579f7a9f178SNeel Natu 			rex_present:1,
58075346353SNeel Natu 			repz_present:1,		/* REP/REPE/REPZ prefix */
58175346353SNeel Natu 			repnz_present:1,	/* REPNE/REPNZ prefix */
582f7a9f178SNeel Natu 			opsize_override:1,	/* Operand size override */
58375346353SNeel Natu 			addrsize_override:1,	/* Address size override */
58475346353SNeel Natu 			segment_override:1;	/* Segment override */
585e813a873SNeel Natu 
586e813a873SNeel Natu 	uint8_t		mod:2,			/* ModRM byte */
587e813a873SNeel Natu 			reg:4,
588e813a873SNeel Natu 			rm:4;
589e813a873SNeel Natu 
590e813a873SNeel Natu 	uint8_t		ss:2,			/* SIB byte */
591cfdea69dSConrad Meyer 			vex_present:1,		/* VEX prefixed */
592cfdea69dSConrad Meyer 			vex_l:1,		/* L bit */
593497cb925SConrad Meyer 			index:4,		/* SIB byte */
594497cb925SConrad Meyer 			base:4;			/* SIB byte */
595e813a873SNeel Natu 
596e813a873SNeel Natu 	uint8_t		disp_bytes;
597e813a873SNeel Natu 	uint8_t		imm_bytes;
598e813a873SNeel Natu 
599e813a873SNeel Natu 	uint8_t		scale;
600497cb925SConrad Meyer 
601cfdea69dSConrad Meyer 	uint8_t		vex_reg:4,		/* vvvv: first source register specifier */
602cfdea69dSConrad Meyer 			vex_pp:2,		/* pp */
603cfdea69dSConrad Meyer 			_sparebits:2;
604cfdea69dSConrad Meyer 
605cfdea69dSConrad Meyer 	uint8_t		_sparebytes[2];
606497cb925SConrad Meyer 
607e813a873SNeel Natu 	int		base_register;		/* VM_REG_GUEST_xyz */
608e813a873SNeel Natu 	int		index_register;		/* VM_REG_GUEST_xyz */
60975346353SNeel Natu 	int		segment_register;	/* VM_REG_GUEST_xyz */
610e813a873SNeel Natu 
611e813a873SNeel Natu 	int64_t		displacement;		/* optional addr displacement */
612e813a873SNeel Natu 	int64_t		immediate;		/* optional immediate operand */
613e813a873SNeel Natu 
614e813a873SNeel Natu 	uint8_t		decoded;	/* set to 1 if successfully decoded */
615e813a873SNeel Natu 
616497cb925SConrad Meyer 	uint8_t		_sparebyte;
617497cb925SConrad Meyer 
618e813a873SNeel Natu 	struct vie_op	op;			/* opcode description */
619e813a873SNeel Natu };
620497cb925SConrad Meyer _Static_assert(sizeof(struct vie) == 64, "ABI");
621497cb925SConrad Meyer _Static_assert(__offsetof(struct vie, disp_bytes) == 22, "ABI");
622497cb925SConrad Meyer _Static_assert(__offsetof(struct vie, scale) == 24, "ABI");
623497cb925SConrad Meyer _Static_assert(__offsetof(struct vie, base_register) == 28, "ABI");
624e813a873SNeel Natu 
625366f6083SPeter Grehan enum vm_exitcode {
626366f6083SPeter Grehan 	VM_EXITCODE_INOUT,
627366f6083SPeter Grehan 	VM_EXITCODE_VMX,
628366f6083SPeter Grehan 	VM_EXITCODE_BOGUS,
629366f6083SPeter Grehan 	VM_EXITCODE_RDMSR,
630366f6083SPeter Grehan 	VM_EXITCODE_WRMSR,
631366f6083SPeter Grehan 	VM_EXITCODE_HLT,
632366f6083SPeter Grehan 	VM_EXITCODE_MTRAP,
633366f6083SPeter Grehan 	VM_EXITCODE_PAUSE,
634cd942e0fSPeter Grehan 	VM_EXITCODE_PAGING,
635318224bbSNeel Natu 	VM_EXITCODE_INST_EMUL,
636edf89256SNeel Natu 	VM_EXITCODE_SPINUP_AP,
637c6a0cc2eSNeel Natu 	VM_EXITCODE_DEPRECATED1,	/* used to be SPINDOWN_CPU */
6385b8a8cd1SNeel Natu 	VM_EXITCODE_RENDEZVOUS,
63930b94db8SNeel Natu 	VM_EXITCODE_IOAPIC_EOI,
640b15a09c0SNeel Natu 	VM_EXITCODE_SUSPENDED,
641d17b5104SNeel Natu 	VM_EXITCODE_INOUT_STR,
6423d5444c8SNeel Natu 	VM_EXITCODE_TASK_SWITCH,
64365145c7fSNeel Natu 	VM_EXITCODE_MONITOR,
64465145c7fSNeel Natu 	VM_EXITCODE_MWAIT,
645bbadcde4SNeel Natu 	VM_EXITCODE_SVM,
646248e6799SNeel Natu 	VM_EXITCODE_REQIDLE,
647fc276d92SJohn Baldwin 	VM_EXITCODE_DEBUG,
64827d26457SAndrew Turner 	VM_EXITCODE_VMINSN,
649cbd03a9dSJohn Baldwin 	VM_EXITCODE_BPT,
6500bda8d3eSCorvin Köhne 	VM_EXITCODE_IPI,
651e3b4fe64SBojan Novković 	VM_EXITCODE_DB,
652cd942e0fSPeter Grehan 	VM_EXITCODE_MAX
653366f6083SPeter Grehan };
654366f6083SPeter Grehan 
655d17b5104SNeel Natu struct vm_inout {
656d17b5104SNeel Natu 	uint16_t	bytes:3;	/* 1 or 2 or 4 */
657d17b5104SNeel Natu 	uint16_t	in:1;
658d17b5104SNeel Natu 	uint16_t	string:1;
659d17b5104SNeel Natu 	uint16_t	rep:1;
660d17b5104SNeel Natu 	uint16_t	port;
661d17b5104SNeel Natu 	uint32_t	eax;		/* valid for out */
662d17b5104SNeel Natu };
663d17b5104SNeel Natu 
664d17b5104SNeel Natu struct vm_inout_str {
665d17b5104SNeel Natu 	struct vm_inout	inout;		/* must be the first element */
666e813a873SNeel Natu 	struct vm_guest_paging paging;
667d17b5104SNeel Natu 	uint64_t	rflags;
668d17b5104SNeel Natu 	uint64_t	cr0;
669d17b5104SNeel Natu 	uint64_t	index;
670d17b5104SNeel Natu 	uint64_t	count;		/* rep=1 (%rcx), rep=0 (1) */
671d17b5104SNeel Natu 	int		addrsize;
672d17b5104SNeel Natu 	enum vm_reg_name seg_name;
673d17b5104SNeel Natu 	struct seg_desc seg_desc;
674d17b5104SNeel Natu };
675d17b5104SNeel Natu 
6763d5444c8SNeel Natu enum task_switch_reason {
6773d5444c8SNeel Natu 	TSR_CALL,
6783d5444c8SNeel Natu 	TSR_IRET,
6793d5444c8SNeel Natu 	TSR_JMP,
6803d5444c8SNeel Natu 	TSR_IDT_GATE,	/* task gate in IDT */
6813d5444c8SNeel Natu };
6823d5444c8SNeel Natu 
6833d5444c8SNeel Natu struct vm_task_switch {
6843d5444c8SNeel Natu 	uint16_t	tsssel;		/* new TSS selector */
6853d5444c8SNeel Natu 	int		ext;		/* task switch due to external event */
6863d5444c8SNeel Natu 	uint32_t	errcode;
6873d5444c8SNeel Natu 	int		errcode_valid;	/* push 'errcode' on the new stack */
6883d5444c8SNeel Natu 	enum task_switch_reason reason;
6893d5444c8SNeel Natu 	struct vm_guest_paging paging;
6903d5444c8SNeel Natu };
6913d5444c8SNeel Natu 
692366f6083SPeter Grehan struct vm_exit {
693366f6083SPeter Grehan 	enum vm_exitcode	exitcode;
694366f6083SPeter Grehan 	int			inst_length;	/* 0 means unknown */
695366f6083SPeter Grehan 	uint64_t		rip;
696366f6083SPeter Grehan 	union {
697d17b5104SNeel Natu 		struct vm_inout	inout;
698d17b5104SNeel Natu 		struct vm_inout_str inout_str;
699cd942e0fSPeter Grehan 		struct {
70013ec9371SPeter Grehan 			uint64_t	gpa;
701318224bbSNeel Natu 			int		fault_type;
702cd942e0fSPeter Grehan 		} paging;
703318224bbSNeel Natu 		struct {
704318224bbSNeel Natu 			uint64_t	gpa;
705318224bbSNeel Natu 			uint64_t	gla;
706e4f605eeSTycho Nightingale 			uint64_t	cs_base;
707f7a9f178SNeel Natu 			int		cs_d;		/* CS.D */
708e813a873SNeel Natu 			struct vm_guest_paging paging;
709318224bbSNeel Natu 			struct vie	vie;
710318224bbSNeel Natu 		} inst_emul;
711366f6083SPeter Grehan 		/*
712366f6083SPeter Grehan 		 * VMX specific payload. Used when there is no "better"
713366f6083SPeter Grehan 		 * exitcode to represent the VM-exit.
714366f6083SPeter Grehan 		 */
715366f6083SPeter Grehan 		struct {
7160492757cSNeel Natu 			int		status;		/* vmx inst status */
7170492757cSNeel Natu 			/*
7180492757cSNeel Natu 			 * 'exit_reason' and 'exit_qualification' are valid
7190492757cSNeel Natu 			 * only if 'status' is zero.
7200492757cSNeel Natu 			 */
721366f6083SPeter Grehan 			uint32_t	exit_reason;
722366f6083SPeter Grehan 			uint64_t	exit_qualification;
7230492757cSNeel Natu 			/*
7240492757cSNeel Natu 			 * 'inst_error' and 'inst_type' are valid
7250492757cSNeel Natu 			 * only if 'status' is non-zero.
7260492757cSNeel Natu 			 */
7270492757cSNeel Natu 			int		inst_type;
7280492757cSNeel Natu 			int		inst_error;
729366f6083SPeter Grehan 		} vmx;
730bbadcde4SNeel Natu 		/*
731bbadcde4SNeel Natu 		 * SVM specific payload.
732bbadcde4SNeel Natu 		 */
733bbadcde4SNeel Natu 		struct {
734bbadcde4SNeel Natu 			uint64_t	exitcode;
735bbadcde4SNeel Natu 			uint64_t	exitinfo1;
736bbadcde4SNeel Natu 			uint64_t	exitinfo2;
737bbadcde4SNeel Natu 		} svm;
738366f6083SPeter Grehan 		struct {
739cbd03a9dSJohn Baldwin 			int		inst_length;
740cbd03a9dSJohn Baldwin 		} bpt;
741cbd03a9dSJohn Baldwin 		struct {
742e3b4fe64SBojan Novković 			int		trace_trap;
743e3b4fe64SBojan Novković 			int		pushf_intercept;
744e3b4fe64SBojan Novković 			int		tf_shadow_val;
745e3b4fe64SBojan Novković 			struct		vm_guest_paging paging;
746e3b4fe64SBojan Novković 		} dbg;
747e3b4fe64SBojan Novković 		struct {
748366f6083SPeter Grehan 			uint32_t	code;		/* ecx value */
749366f6083SPeter Grehan 			uint64_t	wval;
750366f6083SPeter Grehan 		} msr;
751edf89256SNeel Natu 		struct {
752edf89256SNeel Natu 			int		vcpu;
753edf89256SNeel Natu 			uint64_t	rip;
754edf89256SNeel Natu 		} spinup_ap;
7551c052192SNeel Natu 		struct {
7561c052192SNeel Natu 			uint64_t	rflags;
757490768e2STycho Nightingale 			uint64_t	intr_status;
7581c052192SNeel Natu 		} hlt;
75930b94db8SNeel Natu 		struct {
76030b94db8SNeel Natu 			int		vector;
76130b94db8SNeel Natu 		} ioapic_eoi;
762f0fdcfe2SNeel Natu 		struct {
763f0fdcfe2SNeel Natu 			enum vm_suspend_how how;
764f0fdcfe2SNeel Natu 		} suspended;
7650bda8d3eSCorvin Köhne 		struct {
766e17eca32SMark Johnston 			/*
767e17eca32SMark Johnston 			 * The destination vCPU mask is saved in vcpu->cpuset
768e17eca32SMark Johnston 			 * and is copied out to userspace separately to avoid
769e17eca32SMark Johnston 			 * ABI concerns.
770e17eca32SMark Johnston 			 */
7710bda8d3eSCorvin Köhne 			uint32_t mode;
7720bda8d3eSCorvin Köhne 			uint8_t vector;
7730bda8d3eSCorvin Köhne 		} ipi;
7743d5444c8SNeel Natu 		struct vm_task_switch task_switch;
775366f6083SPeter Grehan 	} u;
776366f6083SPeter Grehan };
777366f6083SPeter Grehan 
778d37f2adbSNeel Natu /* APIs to inject faults into the guest */
779d3956e46SJohn Baldwin void vm_inject_fault(struct vcpu *vcpu, int vector, int errcode_valid,
780d3956e46SJohn Baldwin     int errcode);
781d3956e46SJohn Baldwin 
782d3956e46SJohn Baldwin static __inline void
vm_inject_ud(struct vcpu * vcpu)783d3956e46SJohn Baldwin vm_inject_ud(struct vcpu *vcpu)
784d3956e46SJohn Baldwin {
785d3956e46SJohn Baldwin 	vm_inject_fault(vcpu, IDT_UD, 0, 0);
786d3956e46SJohn Baldwin }
787d3956e46SJohn Baldwin 
788d3956e46SJohn Baldwin static __inline void
vm_inject_gp(struct vcpu * vcpu)789d3956e46SJohn Baldwin vm_inject_gp(struct vcpu *vcpu)
790d3956e46SJohn Baldwin {
791d3956e46SJohn Baldwin 	vm_inject_fault(vcpu, IDT_GP, 1, 0);
792d3956e46SJohn Baldwin }
793d3956e46SJohn Baldwin 
794d3956e46SJohn Baldwin static __inline void
vm_inject_ac(struct vcpu * vcpu,int errcode)795d3956e46SJohn Baldwin vm_inject_ac(struct vcpu *vcpu, int errcode)
796d3956e46SJohn Baldwin {
797d3956e46SJohn Baldwin 	vm_inject_fault(vcpu, IDT_AC, 1, errcode);
798d3956e46SJohn Baldwin }
799d3956e46SJohn Baldwin 
800d3956e46SJohn Baldwin static __inline void
vm_inject_ss(struct vcpu * vcpu,int errcode)801d3956e46SJohn Baldwin vm_inject_ss(struct vcpu *vcpu, int errcode)
802d3956e46SJohn Baldwin {
803d3956e46SJohn Baldwin 	vm_inject_fault(vcpu, IDT_SS, 1, errcode);
804d3956e46SJohn Baldwin }
805d3956e46SJohn Baldwin 
806d3956e46SJohn Baldwin void vm_inject_pf(struct vcpu *vcpu, int error_code, uint64_t cr2);
807c9c75df4SNeel Natu 
808366f6083SPeter Grehan #endif	/* _VMM_H_ */
809