xref: /freebsd-src/sys/amd64/include/vmm.h (revision c945c9ddca8d6db162334127a43ef8a0be1d3db1)
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
147aede0d3bSMark Johnston #include <sys/kassert.h>
148aede0d3bSMark Johnston 
149422a8a4dSScott Long CTASSERT(VM_MAX_NAMELEN >= VM_MIN_NAMELEN);
150366f6083SPeter Grehan 
151366f6083SPeter Grehan struct vm;
152dc506506SNeel Natu struct vm_exception;
153366f6083SPeter Grehan struct seg_desc;
154366f6083SPeter Grehan struct vm_exit;
155366f6083SPeter Grehan struct vm_run;
15608e3ff32SNeel Natu struct vhpet;
157565bbb86SNeel Natu struct vioapic;
158366f6083SPeter Grehan struct vlapic;
159318224bbSNeel Natu struct vmspace;
160318224bbSNeel Natu struct vm_object;
161d665d229SNeel Natu struct vm_guest_paging;
162318224bbSNeel Natu struct pmap;
163483d953aSJohn Baldwin enum snapshot_req;
164366f6083SPeter Grehan 
165248e6799SNeel Natu struct vm_eventinfo {
166892feec2SCorvin Köhne 	cpuset_t *rptr;		/* rendezvous cookie */
167248e6799SNeel Natu 	int	*sptr;		/* suspend cookie */
168248e6799SNeel Natu 	int	*iptr;		/* reqidle cookie */
169248e6799SNeel Natu };
170248e6799SNeel Natu 
171add611fdSNeel Natu typedef int	(*vmm_init_func_t)(int ipinum);
172366f6083SPeter Grehan typedef int	(*vmm_cleanup_func_t)(void);
1730b32ef71SJoshua Rogers typedef void	(*vmm_suspend_func_t)(void);
17463e62d39SJohn Baldwin typedef void	(*vmm_resume_func_t)(void);
175318224bbSNeel Natu typedef void *	(*vmi_init_func_t)(struct vm *vm, struct pmap *pmap);
176869c8d19SJohn Baldwin typedef int	(*vmi_run_func_t)(void *vcpui, register_t rip,
177248e6799SNeel Natu 		    struct pmap *pmap, struct vm_eventinfo *info);
178366f6083SPeter Grehan typedef void	(*vmi_cleanup_func_t)(void *vmi);
179950af9ffSJohn Baldwin typedef void *	(*vmi_vcpu_init_func_t)(void *vmi, struct vcpu *vcpu,
180950af9ffSJohn Baldwin 		    int vcpu_id);
181869c8d19SJohn Baldwin typedef void	(*vmi_vcpu_cleanup_func_t)(void *vcpui);
182869c8d19SJohn Baldwin typedef int	(*vmi_get_register_t)(void *vcpui, int num, uint64_t *retval);
183869c8d19SJohn Baldwin typedef int	(*vmi_set_register_t)(void *vcpui, int num, uint64_t val);
184869c8d19SJohn Baldwin typedef int	(*vmi_get_desc_t)(void *vcpui, int num, struct seg_desc *desc);
185869c8d19SJohn Baldwin typedef int	(*vmi_set_desc_t)(void *vcpui, int num, struct seg_desc *desc);
186869c8d19SJohn Baldwin typedef int	(*vmi_get_cap_t)(void *vcpui, int num, int *retval);
187869c8d19SJohn Baldwin typedef int	(*vmi_set_cap_t)(void *vcpui, int num, int val);
188318224bbSNeel Natu typedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max);
189318224bbSNeel Natu typedef void	(*vmi_vmspace_free)(struct vmspace *vmspace);
190869c8d19SJohn Baldwin typedef struct vlapic * (*vmi_vlapic_init)(void *vcpui);
191869c8d19SJohn Baldwin typedef void	(*vmi_vlapic_cleanup)(struct vlapic *vlapic);
192869c8d19SJohn Baldwin typedef int	(*vmi_snapshot_vcpu_t)(void *vcpui, struct vm_snapshot_meta *meta);
193869c8d19SJohn Baldwin typedef int	(*vmi_restore_tsc_t)(void *vcpui, uint64_t now);
194366f6083SPeter Grehan 
195366f6083SPeter Grehan struct vmm_ops {
19615add60dSPeter Grehan 	vmm_init_func_t		modinit;	/* module wide initialization */
19715add60dSPeter Grehan 	vmm_cleanup_func_t	modcleanup;
1980b32ef71SJoshua Rogers 	vmm_resume_func_t	modsuspend;
19915add60dSPeter Grehan 	vmm_resume_func_t	modresume;
200366f6083SPeter Grehan 
20115add60dSPeter Grehan 	vmi_init_func_t		init;		/* vm-specific initialization */
20215add60dSPeter Grehan 	vmi_run_func_t		run;
20315add60dSPeter Grehan 	vmi_cleanup_func_t	cleanup;
2041aa51504SJohn Baldwin 	vmi_vcpu_init_func_t	vcpu_init;
2051aa51504SJohn Baldwin 	vmi_vcpu_cleanup_func_t	vcpu_cleanup;
20615add60dSPeter Grehan 	vmi_get_register_t	getreg;
20715add60dSPeter Grehan 	vmi_set_register_t	setreg;
20815add60dSPeter Grehan 	vmi_get_desc_t		getdesc;
20915add60dSPeter Grehan 	vmi_set_desc_t		setdesc;
21015add60dSPeter Grehan 	vmi_get_cap_t		getcap;
21115add60dSPeter Grehan 	vmi_set_cap_t		setcap;
212318224bbSNeel Natu 	vmi_vmspace_alloc	vmspace_alloc;
213318224bbSNeel Natu 	vmi_vmspace_free	vmspace_free;
214de5ea6b6SNeel Natu 	vmi_vlapic_init		vlapic_init;
215de5ea6b6SNeel Natu 	vmi_vlapic_cleanup	vlapic_cleanup;
216483d953aSJohn Baldwin 
217483d953aSJohn Baldwin 	/* checkpoint operations */
21839ec056eSJohn Baldwin 	vmi_snapshot_vcpu_t	vcpu_snapshot;
21915add60dSPeter Grehan 	vmi_restore_tsc_t	restore_tsc;
220366f6083SPeter Grehan };
221366f6083SPeter Grehan 
22215add60dSPeter Grehan extern const struct vmm_ops vmm_ops_intel;
22315add60dSPeter Grehan extern const struct vmm_ops vmm_ops_amd;
224366f6083SPeter Grehan 
225ee98f99dSJohn Baldwin extern u_int vm_maxcpu;			/* maximum virtual cpus */
226ee98f99dSJohn Baldwin 
227d5408b1dSNeel Natu int vm_create(const char *name, struct vm **retvm);
22898568a00SJohn Baldwin struct vcpu *vm_alloc_vcpu(struct vm *vm, int vcpuid);
22998568a00SJohn Baldwin void vm_disable_vcpu_creation(struct vm *vm);
23098568a00SJohn Baldwin void vm_slock_vcpus(struct vm *vm);
23198568a00SJohn Baldwin void vm_unlock_vcpus(struct vm *vm);
232366f6083SPeter Grehan void vm_destroy(struct vm *vm);
2335fcf252fSNeel Natu int vm_reinit(struct vm *vm);
234366f6083SPeter Grehan const char *vm_name(struct vm *vm);
235a488c9c9SRodney W. Grimes uint16_t vm_get_maxcpus(struct vm *vm);
23601d822d3SRodney W. Grimes void vm_get_topology(struct vm *vm, uint16_t *sockets, uint16_t *cores,
23701d822d3SRodney W. Grimes     uint16_t *threads, uint16_t *maxcpus);
23801d822d3SRodney W. Grimes int vm_set_topology(struct vm *vm, uint16_t sockets, uint16_t cores,
23901d822d3SRodney W. Grimes     uint16_t threads, uint16_t maxcpus);
2409b1aa8d6SNeel Natu 
2419b1aa8d6SNeel Natu /*
2429b1aa8d6SNeel Natu  * APIs that modify the guest memory map require all vcpus to be frozen.
2439b1aa8d6SNeel Natu  */
24467b69e76SJohn Baldwin void vm_slock_memsegs(struct vm *vm);
24567b69e76SJohn Baldwin void vm_xlock_memsegs(struct vm *vm);
24667b69e76SJohn Baldwin void vm_unlock_memsegs(struct vm *vm);
2479b1aa8d6SNeel Natu int vm_mmap_memseg(struct vm *vm, vm_paddr_t gpa, int segid, vm_ooffset_t off,
2489b1aa8d6SNeel Natu     size_t len, int prot, int flags);
249f8a6ec2dSD Scott Phillips int vm_munmap_memseg(struct vm *vm, vm_paddr_t gpa, size_t len);
2509b1aa8d6SNeel Natu int vm_alloc_memseg(struct vm *vm, int ident, size_t len, bool sysmem);
2519b1aa8d6SNeel Natu void vm_free_memseg(struct vm *vm, int ident);
252366f6083SPeter Grehan int vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
253366f6083SPeter Grehan int vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
2549b1aa8d6SNeel Natu int vm_assign_pptdev(struct vm *vm, int bus, int slot, int func);
2559b1aa8d6SNeel Natu int vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func);
2569b1aa8d6SNeel Natu 
2579b1aa8d6SNeel Natu /*
2589b1aa8d6SNeel Natu  * APIs that inspect the guest memory map require only a *single* vcpu to
2599b1aa8d6SNeel Natu  * be frozen. This acts like a read lock on the guest memory map since any
2609b1aa8d6SNeel Natu  * modification requires *all* vcpus to be frozen.
2619b1aa8d6SNeel Natu  */
2629b1aa8d6SNeel Natu int vm_mmap_getnext(struct vm *vm, vm_paddr_t *gpa, int *segid,
2639b1aa8d6SNeel Natu     vm_ooffset_t *segoff, size_t *len, int *prot, int *flags);
2649b1aa8d6SNeel Natu int vm_get_memseg(struct vm *vm, int ident, size_t *len, bool *sysmem,
2659b1aa8d6SNeel Natu     struct vm_object **objptr);
266147d12a7SAntoine Brodin vm_paddr_t vmm_sysmem_maxaddr(struct vm *vm);
267d3956e46SJohn Baldwin void *vm_gpa_hold(struct vcpu *vcpu, vm_paddr_t gpa, size_t len,
268d3956e46SJohn Baldwin     int prot, void **cookie);
269d3956e46SJohn Baldwin void *vm_gpa_hold_global(struct vm *vm, vm_paddr_t gpa, size_t len,
2709b1aa8d6SNeel Natu     int prot, void **cookie);
271318224bbSNeel Natu void vm_gpa_release(void *cookie);
27280cb5d84SJohn Baldwin bool vm_mem_allocated(struct vcpu *vcpu, vm_paddr_t gpa);
2739b1aa8d6SNeel Natu 
274d3956e46SJohn Baldwin int vm_get_register(struct vcpu *vcpu, int reg, uint64_t *retval);
275d3956e46SJohn Baldwin int vm_set_register(struct vcpu *vcpu, int reg, uint64_t val);
276d3956e46SJohn Baldwin int vm_get_seg_desc(struct vcpu *vcpu, int reg,
277366f6083SPeter Grehan 		    struct seg_desc *ret_desc);
2783f0f4b15SJohn Baldwin int vm_set_seg_desc(struct vcpu *vcpu, int reg,
279366f6083SPeter Grehan 		    struct seg_desc *desc);
280e17eca32SMark Johnston int vm_run(struct vcpu *vcpu);
281f0fdcfe2SNeel Natu int vm_suspend(struct vm *vm, enum vm_suspend_how how);
2823f0f4b15SJohn Baldwin int vm_inject_nmi(struct vcpu *vcpu);
28380cb5d84SJohn Baldwin int vm_nmi_pending(struct vcpu *vcpu);
28480cb5d84SJohn Baldwin void vm_nmi_clear(struct vcpu *vcpu);
2853f0f4b15SJohn Baldwin int vm_inject_extint(struct vcpu *vcpu);
28680cb5d84SJohn Baldwin int vm_extint_pending(struct vcpu *vcpu);
28780cb5d84SJohn Baldwin void vm_extint_clear(struct vcpu *vcpu);
288950af9ffSJohn Baldwin int vcpu_vcpuid(struct vcpu *vcpu);
289950af9ffSJohn Baldwin struct vm *vcpu_vm(struct vcpu *vcpu);
290950af9ffSJohn Baldwin struct vcpu *vm_vcpu(struct vm *vm, int cpu);
291d3956e46SJohn Baldwin struct vlapic *vm_lapic(struct vcpu *vcpu);
292565bbb86SNeel Natu struct vioapic *vm_ioapic(struct vm *vm);
29308e3ff32SNeel Natu struct vhpet *vm_hpet(struct vm *vm);
2943f0f4b15SJohn Baldwin int vm_get_capability(struct vcpu *vcpu, int type, int *val);
2953f0f4b15SJohn Baldwin int vm_set_capability(struct vcpu *vcpu, int type, int val);
2963f0f4b15SJohn Baldwin int vm_get_x2apic_state(struct vcpu *vcpu, enum x2apic_state *state);
2973f0f4b15SJohn Baldwin int vm_set_x2apic_state(struct vcpu *vcpu, enum x2apic_state state);
298565bbb86SNeel Natu int vm_apicid2vcpuid(struct vm *vm, int apicid);
2993f0f4b15SJohn Baldwin int vm_activate_cpu(struct vcpu *vcpu);
3003f0f4b15SJohn Baldwin int vm_suspend_cpu(struct vm *vm, struct vcpu *vcpu);
3013f0f4b15SJohn Baldwin int vm_resume_cpu(struct vm *vm, struct vcpu *vcpu);
302d3956e46SJohn Baldwin int vm_restart_instruction(struct vcpu *vcpu);
30380cb5d84SJohn Baldwin struct vm_exit *vm_exitinfo(struct vcpu *vcpu);
304e17eca32SMark Johnston cpuset_t *vm_exitinfo_cpuset(struct vcpu *vcpu);
30580cb5d84SJohn Baldwin void vm_exit_suspended(struct vcpu *vcpu, uint64_t rip);
30680cb5d84SJohn Baldwin void vm_exit_debug(struct vcpu *vcpu, uint64_t rip);
30780cb5d84SJohn Baldwin void vm_exit_rendezvous(struct vcpu *vcpu, uint64_t rip);
30880cb5d84SJohn Baldwin void vm_exit_astpending(struct vcpu *vcpu, uint64_t rip);
30980cb5d84SJohn Baldwin void vm_exit_reqidle(struct vcpu *vcpu, uint64_t rip);
310483d953aSJohn Baldwin int vm_snapshot_req(struct vm *vm, struct vm_snapshot_meta *meta);
311483d953aSJohn Baldwin int vm_restore_time(struct vm *vm);
312483d953aSJohn Baldwin 
3138325ce5cSNeel Natu #ifdef _SYS__CPUSET_H_
314366f6083SPeter Grehan /*
3155b8a8cd1SNeel Natu  * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'.
3165b8a8cd1SNeel Natu  * The rendezvous 'func(arg)' is not allowed to do anything that will
3175b8a8cd1SNeel Natu  * cause the thread to be put to sleep.
3185b8a8cd1SNeel Natu  *
3195b8a8cd1SNeel Natu  * The caller cannot hold any locks when initiating the rendezvous.
3205b8a8cd1SNeel Natu  *
3215b8a8cd1SNeel Natu  * The implementation of this API may cause vcpus other than those specified
3225b8a8cd1SNeel Natu  * by 'dest' to be stalled. The caller should not rely on any vcpus making
3235b8a8cd1SNeel Natu  * forward progress when the rendezvous is in progress.
3245b8a8cd1SNeel Natu  */
325d8be3d52SJohn Baldwin typedef void (*vm_rendezvous_func_t)(struct vcpu *vcpu, void *arg);
326d8be3d52SJohn Baldwin int vm_smp_rendezvous(struct vcpu *vcpu, cpuset_t dest,
3275b8a8cd1SNeel Natu     vm_rendezvous_func_t func, void *arg);
328c0f35dbfSJohn Baldwin 
3298325ce5cSNeel Natu cpuset_t vm_active_cpus(struct vm *vm);
330fc276d92SJohn Baldwin cpuset_t vm_debug_cpus(struct vm *vm);
3318325ce5cSNeel Natu cpuset_t vm_suspended_cpus(struct vm *vm);
332c0f35dbfSJohn Baldwin cpuset_t vm_start_cpus(struct vm *vm, const cpuset_t *tostart);
333c0f35dbfSJohn Baldwin void vm_await_start(struct vm *vm, const cpuset_t *waiting);
3348325ce5cSNeel Natu #endif	/* _SYS__CPUSET_H_ */
3355b8a8cd1SNeel Natu 
3365b8a8cd1SNeel Natu static __inline int
337892feec2SCorvin Köhne vcpu_rendezvous_pending(struct vcpu *vcpu, struct vm_eventinfo *info)
3385b8a8cd1SNeel Natu {
339892feec2SCorvin Köhne 	/*
340892feec2SCorvin Köhne 	 * This check isn't done with atomic operations or under a lock because
341892feec2SCorvin Köhne 	 * there's no need to. If the vcpuid bit is set, the vcpu is part of a
342892feec2SCorvin Köhne 	 * rendezvous and the bit won't be cleared until the vcpu enters the
343892feec2SCorvin Köhne 	 * rendezvous. On rendezvous exit, the cpuset is cleared and the vcpu
344892feec2SCorvin Köhne 	 * will see an empty cpuset. So, the races are harmless.
345892feec2SCorvin Köhne 	 */
346892feec2SCorvin Köhne 	return (CPU_ISSET(vcpu_vcpuid(vcpu), info->rptr));
3475b8a8cd1SNeel Natu }
3485b8a8cd1SNeel Natu 
349b15a09c0SNeel Natu static __inline int
350248e6799SNeel Natu vcpu_suspended(struct vm_eventinfo *info)
351b15a09c0SNeel Natu {
352b15a09c0SNeel Natu 
353248e6799SNeel Natu 	return (*info->sptr);
354248e6799SNeel Natu }
355248e6799SNeel Natu 
356248e6799SNeel Natu static __inline int
357248e6799SNeel Natu vcpu_reqidle(struct vm_eventinfo *info)
358248e6799SNeel Natu {
359248e6799SNeel Natu 
360248e6799SNeel Natu 	return (*info->iptr);
361b15a09c0SNeel Natu }
362b15a09c0SNeel Natu 
36380cb5d84SJohn Baldwin int vcpu_debugged(struct vcpu *vcpu);
364fc276d92SJohn Baldwin 
3655b8a8cd1SNeel Natu /*
366490d56c5SEd Maste  * Return true if device indicated by bus/slot/func is supposed to be a
367366f6083SPeter Grehan  * pci passthrough device.
368366f6083SPeter Grehan  *
369490d56c5SEd Maste  * Return false otherwise.
370366f6083SPeter Grehan  */
371490d56c5SEd Maste bool vmm_is_pptdev(int bus, int slot, int func);
372366f6083SPeter Grehan 
373366f6083SPeter Grehan void *vm_iommu_domain(struct vm *vm);
374366f6083SPeter Grehan 
37575dd3366SNeel Natu enum vcpu_state {
37675dd3366SNeel Natu 	VCPU_IDLE,
377318224bbSNeel Natu 	VCPU_FROZEN,
37875dd3366SNeel Natu 	VCPU_RUNNING,
379318224bbSNeel Natu 	VCPU_SLEEPING,
38075dd3366SNeel Natu };
381366f6083SPeter Grehan 
3823f0f4b15SJohn Baldwin int vcpu_set_state(struct vcpu *vcpu, enum vcpu_state state, bool from_idle);
383d3956e46SJohn Baldwin enum vcpu_state vcpu_get_state(struct vcpu *vcpu, int *hostcpu);
384366f6083SPeter Grehan 
385366f6083SPeter Grehan static int __inline
38680cb5d84SJohn Baldwin vcpu_is_running(struct vcpu *vcpu, int *hostcpu)
387366f6083SPeter Grehan {
38880cb5d84SJohn Baldwin 	return (vcpu_get_state(vcpu, hostcpu) == VCPU_RUNNING);
389366f6083SPeter Grehan }
390366f6083SPeter Grehan 
391f008d157SNeel Natu #ifdef _SYS_PROC_H_
392f008d157SNeel Natu static int __inline
39380cb5d84SJohn Baldwin vcpu_should_yield(struct vcpu *vcpu)
394f008d157SNeel Natu {
395c6d31b83SKonstantin Belousov 	struct thread *td;
396ede04033SNeel Natu 
397c6d31b83SKonstantin Belousov 	td = curthread;
398c6d31b83SKonstantin Belousov 	return (td->td_ast != 0 || td->td_owepreempt != 0);
399f008d157SNeel Natu }
400f008d157SNeel Natu #endif
401f008d157SNeel Natu 
4023dc3d32aSJohn Baldwin void *vcpu_stats(struct vcpu *vcpu);
4033f0f4b15SJohn Baldwin void vcpu_notify_event(struct vcpu *vcpu, bool lapic_intr);
404*c945c9ddSMark Johnston struct vmspace *vm_vmspace(struct vm *vm);
405762fd208STycho Nightingale struct vatpic *vm_atpic(struct vm *vm);
406e883c9bbSTycho Nightingale struct vatpit *vm_atpit(struct vm *vm);
407160ef77aSNeel Natu struct vpmtmr *vm_pmtmr(struct vm *vm);
4080dafa5cdSNeel Natu struct vrtc *vm_rtc(struct vm *vm);
409dc506506SNeel Natu 
410dc506506SNeel Natu /*
411c9c75df4SNeel Natu  * Inject exception 'vector' into the guest vcpu. This function returns 0 on
412dc506506SNeel Natu  * success and non-zero on failure.
413dc506506SNeel Natu  *
414dc506506SNeel Natu  * Wrapper functions like 'vm_inject_gp()' should be preferred to calling
415dc506506SNeel Natu  * this function directly because they enforce the trap-like or fault-like
416dc506506SNeel Natu  * behavior of an exception.
417dc506506SNeel Natu  *
418dc506506SNeel Natu  * This function should only be called in the context of the thread that is
419dc506506SNeel Natu  * executing this vcpu.
420dc506506SNeel Natu  */
421d3956e46SJohn Baldwin int vm_inject_exception(struct vcpu *vcpu, int vector, int err_valid,
422c9c75df4SNeel Natu     uint32_t errcode, int restart_instruction);
423dc506506SNeel Natu 
424dc506506SNeel Natu /*
425091d4532SNeel Natu  * This function is called after a VM-exit that occurred during exception or
426091d4532SNeel Natu  * interrupt delivery through the IDT. The format of 'intinfo' is described
427091d4532SNeel Natu  * in Figure 15-1, "EXITINTINFO for All Intercepts", APM, Vol 2.
428dc506506SNeel Natu  *
429091d4532SNeel Natu  * If a VM-exit handler completes the event delivery successfully then it
430091d4532SNeel Natu  * should call vm_exit_intinfo() to extinguish the pending event. For e.g.,
431091d4532SNeel Natu  * if the task switch emulation is triggered via a task gate then it should
432091d4532SNeel Natu  * call this function with 'intinfo=0' to indicate that the external event
433091d4532SNeel Natu  * is not pending anymore.
434091d4532SNeel Natu  *
435091d4532SNeel Natu  * Return value is 0 on success and non-zero on failure.
436dc506506SNeel Natu  */
43780cb5d84SJohn Baldwin int vm_exit_intinfo(struct vcpu *vcpu, uint64_t intinfo);
438091d4532SNeel Natu 
439091d4532SNeel Natu /*
440091d4532SNeel Natu  * This function is called before every VM-entry to retrieve a pending
441091d4532SNeel Natu  * event that should be injected into the guest. This function combines
442091d4532SNeel Natu  * nested events into a double or triple fault.
443091d4532SNeel Natu  *
444091d4532SNeel Natu  * Returns 0 if there are no events that need to be injected into the guest
445091d4532SNeel Natu  * and non-zero otherwise.
446091d4532SNeel Natu  */
44780cb5d84SJohn Baldwin int vm_entry_intinfo(struct vcpu *vcpu, uint64_t *info);
448091d4532SNeel Natu 
4493f0f4b15SJohn Baldwin int vm_get_intinfo(struct vcpu *vcpu, uint64_t *info1, uint64_t *info2);
450dc506506SNeel Natu 
451483d953aSJohn Baldwin /*
452483d953aSJohn Baldwin  * Function used to keep track of the guest's TSC offset. The
4531eedb4e5SElyes Haouas  * offset is used by the virtualization extensions to provide a consistent
454483d953aSJohn Baldwin  * value for the Time Stamp Counter to the guest.
455483d953aSJohn Baldwin  */
45680cb5d84SJohn Baldwin void vm_set_tsc_offset(struct vcpu *vcpu, uint64_t offset);
457483d953aSJohn Baldwin 
458d17b5104SNeel Natu enum vm_reg_name vm_segment_name(int seg_encoding);
459d17b5104SNeel Natu 
460d665d229SNeel Natu struct vm_copyinfo {
461d665d229SNeel Natu 	uint64_t	gpa;
462d665d229SNeel Natu 	size_t		len;
463d665d229SNeel Natu 	void		*hva;
464d665d229SNeel Natu 	void		*cookie;
465d665d229SNeel Natu };
466d665d229SNeel Natu 
467d665d229SNeel Natu /*
468d665d229SNeel Natu  * Set up 'copyinfo[]' to copy to/from guest linear address space starting
469d665d229SNeel Natu  * at 'gla' and 'len' bytes long. The 'prot' should be set to PROT_READ for
470d665d229SNeel Natu  * a copyin or PROT_WRITE for a copyout.
471d665d229SNeel Natu  *
472edafb5a3SPedro F. Giffuni  * retval	is_fault	Interpretation
4739c4d5478SNeel Natu  *   0		   0		Success
4749c4d5478SNeel Natu  *   0		   1		An exception was injected into the guest
4759c4d5478SNeel Natu  * EFAULT	  N/A		Unrecoverable error
476d665d229SNeel Natu  *
477d665d229SNeel Natu  * The 'copyinfo[]' can be passed to 'vm_copyin()' or 'vm_copyout()' only if
478d665d229SNeel Natu  * the return value is 0. The 'copyinfo[]' resources should be freed by calling
479d665d229SNeel Natu  * 'vm_copy_teardown()' after the copy is done.
480d665d229SNeel Natu  */
481d3956e46SJohn Baldwin int vm_copy_setup(struct vcpu *vcpu, struct vm_guest_paging *paging,
482d665d229SNeel Natu     uint64_t gla, size_t len, int prot, struct vm_copyinfo *copyinfo,
4839c4d5478SNeel Natu     int num_copyinfo, int *is_fault);
4842b4fe856SJohn Baldwin void vm_copy_teardown(struct vm_copyinfo *copyinfo, int num_copyinfo);
4852b4fe856SJohn Baldwin void vm_copyin(struct vm_copyinfo *copyinfo, void *kaddr, size_t len);
4862b4fe856SJohn Baldwin void vm_copyout(const void *kaddr, struct vm_copyinfo *copyinfo, size_t len);
487b0538143SNeel Natu 
48880cb5d84SJohn Baldwin int vcpu_trace_exceptions(struct vcpu *vcpu);
48980cb5d84SJohn Baldwin int vcpu_trap_wbinvd(struct vcpu *vcpu);
490366f6083SPeter Grehan #endif	/* KERNEL */
491366f6083SPeter Grehan 
492366f6083SPeter Grehan /*
493366f6083SPeter Grehan  * Identifiers for optional vmm capabilities
494366f6083SPeter Grehan  */
495366f6083SPeter Grehan enum vm_cap_type {
496366f6083SPeter Grehan 	VM_CAP_HALT_EXIT,
497366f6083SPeter Grehan 	VM_CAP_MTRAP_EXIT,
498366f6083SPeter Grehan 	VM_CAP_PAUSE_EXIT,
499366f6083SPeter Grehan 	VM_CAP_UNRESTRICTED_GUEST,
50049cc03daSNeel Natu 	VM_CAP_ENABLE_INVPCID,
501cbd03a9dSJohn Baldwin 	VM_CAP_BPT_EXIT,
502f5f5f1e7SPeter Grehan 	VM_CAP_RDPID,
503f5f5f1e7SPeter Grehan 	VM_CAP_RDTSCP,
5040bda8d3eSCorvin Köhne 	VM_CAP_IPI_EXIT,
505fefac543SBojan Novković 	VM_CAP_MASK_HWINTR,
506e3b4fe64SBojan Novković 	VM_CAP_RFLAGS_TF,
507366f6083SPeter Grehan 	VM_CAP_MAX
508366f6083SPeter Grehan };
509366f6083SPeter Grehan 
510b3e9732aSJohn Baldwin enum vm_intr_trigger {
511b3e9732aSJohn Baldwin 	EDGE_TRIGGER,
512b3e9732aSJohn Baldwin 	LEVEL_TRIGGER
513b3e9732aSJohn Baldwin };
514b3e9732aSJohn Baldwin 
515366f6083SPeter Grehan /*
516366f6083SPeter Grehan  * The 'access' field has the format specified in Table 21-2 of the Intel
517366f6083SPeter Grehan  * Architecture Manual vol 3b.
518366f6083SPeter Grehan  *
519366f6083SPeter Grehan  * XXX The contents of the 'access' field are architecturally defined except
520366f6083SPeter Grehan  * bit 16 - Segment Unusable.
521366f6083SPeter Grehan  */
522366f6083SPeter Grehan struct seg_desc {
523366f6083SPeter Grehan 	uint64_t	base;
524366f6083SPeter Grehan 	uint32_t	limit;
525366f6083SPeter Grehan 	uint32_t	access;
526366f6083SPeter Grehan };
527f7a9f178SNeel Natu #define	SEG_DESC_TYPE(access)		((access) & 0x001f)
5283d5444c8SNeel Natu #define	SEG_DESC_DPL(access)		(((access) >> 5) & 0x3)
529f7a9f178SNeel Natu #define	SEG_DESC_PRESENT(access)	(((access) & 0x0080) ? 1 : 0)
530f7a9f178SNeel Natu #define	SEG_DESC_DEF32(access)		(((access) & 0x4000) ? 1 : 0)
531f7a9f178SNeel Natu #define	SEG_DESC_GRANULARITY(access)	(((access) & 0x8000) ? 1 : 0)
532f7a9f178SNeel Natu #define	SEG_DESC_UNUSABLE(access)	(((access) & 0x10000) ? 1 : 0)
533366f6083SPeter Grehan 
534e813a873SNeel Natu enum vm_cpu_mode {
535b301b9e2SNeel Natu 	CPU_MODE_REAL,
536b301b9e2SNeel Natu 	CPU_MODE_PROTECTED,
537e813a873SNeel Natu 	CPU_MODE_COMPATIBILITY,		/* IA-32E mode (CS.L = 0) */
538e813a873SNeel Natu 	CPU_MODE_64BIT,			/* IA-32E mode (CS.L = 1) */
539e813a873SNeel Natu };
540e813a873SNeel Natu 
541e813a873SNeel Natu enum vm_paging_mode {
542e813a873SNeel Natu 	PAGING_MODE_FLAT,
543e813a873SNeel Natu 	PAGING_MODE_32,
544e813a873SNeel Natu 	PAGING_MODE_PAE,
545e813a873SNeel Natu 	PAGING_MODE_64,
546f3eb12e4SKonstantin Belousov 	PAGING_MODE_64_LA57,
547e813a873SNeel Natu };
548e813a873SNeel Natu 
549e813a873SNeel Natu struct vm_guest_paging {
550e813a873SNeel Natu 	uint64_t	cr3;
551e813a873SNeel Natu 	int		cpl;
552e813a873SNeel Natu 	enum vm_cpu_mode cpu_mode;
553e813a873SNeel Natu 	enum vm_paging_mode paging_mode;
554e813a873SNeel Natu };
555e813a873SNeel Natu 
556e813a873SNeel Natu /*
557e813a873SNeel Natu  * The data structures 'vie' and 'vie_op' are meant to be opaque to the
558e813a873SNeel Natu  * consumers of instruction decoding. The only reason why their contents
559e813a873SNeel Natu  * need to be exposed is because they are part of the 'vm_exit' structure.
560e813a873SNeel Natu  */
561e813a873SNeel Natu struct vie_op {
562e813a873SNeel Natu 	uint8_t		op_byte;	/* actual opcode byte */
563e813a873SNeel Natu 	uint8_t		op_type;	/* type of operation (e.g. MOV) */
564e813a873SNeel Natu 	uint16_t	op_flags;
565e813a873SNeel Natu };
566497cb925SConrad Meyer _Static_assert(sizeof(struct vie_op) == 4, "ABI");
567497cb925SConrad Meyer _Static_assert(_Alignof(struct vie_op) == 2, "ABI");
568e813a873SNeel Natu 
569e813a873SNeel Natu #define	VIE_INST_SIZE	15
570e813a873SNeel Natu struct vie {
571e813a873SNeel Natu 	uint8_t		inst[VIE_INST_SIZE];	/* instruction bytes */
572e813a873SNeel Natu 	uint8_t		num_valid;		/* size of the instruction */
5734daa95f8SConrad Meyer 
5744daa95f8SConrad Meyer /* The following fields are all zeroed upon restart. */
5754daa95f8SConrad Meyer #define	vie_startzero	num_processed
576e813a873SNeel Natu 	uint8_t		num_processed;
577e813a873SNeel Natu 
578f7a9f178SNeel Natu 	uint8_t		addrsize:4, opsize:4;	/* address and operand sizes */
579e813a873SNeel Natu 	uint8_t		rex_w:1,		/* REX prefix */
580e813a873SNeel Natu 			rex_r:1,
581e813a873SNeel Natu 			rex_x:1,
582e813a873SNeel Natu 			rex_b:1,
583f7a9f178SNeel Natu 			rex_present:1,
58475346353SNeel Natu 			repz_present:1,		/* REP/REPE/REPZ prefix */
58575346353SNeel Natu 			repnz_present:1,	/* REPNE/REPNZ prefix */
586f7a9f178SNeel Natu 			opsize_override:1,	/* Operand size override */
58775346353SNeel Natu 			addrsize_override:1,	/* Address size override */
58875346353SNeel Natu 			segment_override:1;	/* Segment override */
589e813a873SNeel Natu 
590e813a873SNeel Natu 	uint8_t		mod:2,			/* ModRM byte */
591e813a873SNeel Natu 			reg:4,
592e813a873SNeel Natu 			rm:4;
593e813a873SNeel Natu 
594e813a873SNeel Natu 	uint8_t		ss:2,			/* SIB byte */
595cfdea69dSConrad Meyer 			vex_present:1,		/* VEX prefixed */
596cfdea69dSConrad Meyer 			vex_l:1,		/* L bit */
597497cb925SConrad Meyer 			index:4,		/* SIB byte */
598497cb925SConrad Meyer 			base:4;			/* SIB byte */
599e813a873SNeel Natu 
600e813a873SNeel Natu 	uint8_t		disp_bytes;
601e813a873SNeel Natu 	uint8_t		imm_bytes;
602e813a873SNeel Natu 
603e813a873SNeel Natu 	uint8_t		scale;
604497cb925SConrad Meyer 
605cfdea69dSConrad Meyer 	uint8_t		vex_reg:4,		/* vvvv: first source register specifier */
606cfdea69dSConrad Meyer 			vex_pp:2,		/* pp */
607cfdea69dSConrad Meyer 			_sparebits:2;
608cfdea69dSConrad Meyer 
609cfdea69dSConrad Meyer 	uint8_t		_sparebytes[2];
610497cb925SConrad Meyer 
611e813a873SNeel Natu 	int		base_register;		/* VM_REG_GUEST_xyz */
612e813a873SNeel Natu 	int		index_register;		/* VM_REG_GUEST_xyz */
61375346353SNeel Natu 	int		segment_register;	/* VM_REG_GUEST_xyz */
614e813a873SNeel Natu 
615e813a873SNeel Natu 	int64_t		displacement;		/* optional addr displacement */
616e813a873SNeel Natu 	int64_t		immediate;		/* optional immediate operand */
617e813a873SNeel Natu 
618e813a873SNeel Natu 	uint8_t		decoded;	/* set to 1 if successfully decoded */
619e813a873SNeel Natu 
620497cb925SConrad Meyer 	uint8_t		_sparebyte;
621497cb925SConrad Meyer 
622e813a873SNeel Natu 	struct vie_op	op;			/* opcode description */
623e813a873SNeel Natu };
624497cb925SConrad Meyer _Static_assert(sizeof(struct vie) == 64, "ABI");
625497cb925SConrad Meyer _Static_assert(__offsetof(struct vie, disp_bytes) == 22, "ABI");
626497cb925SConrad Meyer _Static_assert(__offsetof(struct vie, scale) == 24, "ABI");
627497cb925SConrad Meyer _Static_assert(__offsetof(struct vie, base_register) == 28, "ABI");
628e813a873SNeel Natu 
629366f6083SPeter Grehan enum vm_exitcode {
630366f6083SPeter Grehan 	VM_EXITCODE_INOUT,
631366f6083SPeter Grehan 	VM_EXITCODE_VMX,
632366f6083SPeter Grehan 	VM_EXITCODE_BOGUS,
633366f6083SPeter Grehan 	VM_EXITCODE_RDMSR,
634366f6083SPeter Grehan 	VM_EXITCODE_WRMSR,
635366f6083SPeter Grehan 	VM_EXITCODE_HLT,
636366f6083SPeter Grehan 	VM_EXITCODE_MTRAP,
637366f6083SPeter Grehan 	VM_EXITCODE_PAUSE,
638cd942e0fSPeter Grehan 	VM_EXITCODE_PAGING,
639318224bbSNeel Natu 	VM_EXITCODE_INST_EMUL,
640edf89256SNeel Natu 	VM_EXITCODE_SPINUP_AP,
641c6a0cc2eSNeel Natu 	VM_EXITCODE_DEPRECATED1,	/* used to be SPINDOWN_CPU */
6425b8a8cd1SNeel Natu 	VM_EXITCODE_RENDEZVOUS,
64330b94db8SNeel Natu 	VM_EXITCODE_IOAPIC_EOI,
644b15a09c0SNeel Natu 	VM_EXITCODE_SUSPENDED,
645d17b5104SNeel Natu 	VM_EXITCODE_INOUT_STR,
6463d5444c8SNeel Natu 	VM_EXITCODE_TASK_SWITCH,
64765145c7fSNeel Natu 	VM_EXITCODE_MONITOR,
64865145c7fSNeel Natu 	VM_EXITCODE_MWAIT,
649bbadcde4SNeel Natu 	VM_EXITCODE_SVM,
650248e6799SNeel Natu 	VM_EXITCODE_REQIDLE,
651fc276d92SJohn Baldwin 	VM_EXITCODE_DEBUG,
65227d26457SAndrew Turner 	VM_EXITCODE_VMINSN,
653cbd03a9dSJohn Baldwin 	VM_EXITCODE_BPT,
6540bda8d3eSCorvin Köhne 	VM_EXITCODE_IPI,
655e3b4fe64SBojan Novković 	VM_EXITCODE_DB,
656cd942e0fSPeter Grehan 	VM_EXITCODE_MAX
657366f6083SPeter Grehan };
658366f6083SPeter Grehan 
659d17b5104SNeel Natu struct vm_inout {
660d17b5104SNeel Natu 	uint16_t	bytes:3;	/* 1 or 2 or 4 */
661d17b5104SNeel Natu 	uint16_t	in:1;
662d17b5104SNeel Natu 	uint16_t	string:1;
663d17b5104SNeel Natu 	uint16_t	rep:1;
664d17b5104SNeel Natu 	uint16_t	port;
665d17b5104SNeel Natu 	uint32_t	eax;		/* valid for out */
666d17b5104SNeel Natu };
667d17b5104SNeel Natu 
668d17b5104SNeel Natu struct vm_inout_str {
669d17b5104SNeel Natu 	struct vm_inout	inout;		/* must be the first element */
670e813a873SNeel Natu 	struct vm_guest_paging paging;
671d17b5104SNeel Natu 	uint64_t	rflags;
672d17b5104SNeel Natu 	uint64_t	cr0;
673d17b5104SNeel Natu 	uint64_t	index;
674d17b5104SNeel Natu 	uint64_t	count;		/* rep=1 (%rcx), rep=0 (1) */
675d17b5104SNeel Natu 	int		addrsize;
676d17b5104SNeel Natu 	enum vm_reg_name seg_name;
677d17b5104SNeel Natu 	struct seg_desc seg_desc;
678d17b5104SNeel Natu };
679d17b5104SNeel Natu 
6803d5444c8SNeel Natu enum task_switch_reason {
6813d5444c8SNeel Natu 	TSR_CALL,
6823d5444c8SNeel Natu 	TSR_IRET,
6833d5444c8SNeel Natu 	TSR_JMP,
6843d5444c8SNeel Natu 	TSR_IDT_GATE,	/* task gate in IDT */
6853d5444c8SNeel Natu };
6863d5444c8SNeel Natu 
6873d5444c8SNeel Natu struct vm_task_switch {
6883d5444c8SNeel Natu 	uint16_t	tsssel;		/* new TSS selector */
6893d5444c8SNeel Natu 	int		ext;		/* task switch due to external event */
6903d5444c8SNeel Natu 	uint32_t	errcode;
6913d5444c8SNeel Natu 	int		errcode_valid;	/* push 'errcode' on the new stack */
6923d5444c8SNeel Natu 	enum task_switch_reason reason;
6933d5444c8SNeel Natu 	struct vm_guest_paging paging;
6943d5444c8SNeel Natu };
6953d5444c8SNeel Natu 
696366f6083SPeter Grehan struct vm_exit {
697366f6083SPeter Grehan 	enum vm_exitcode	exitcode;
698366f6083SPeter Grehan 	int			inst_length;	/* 0 means unknown */
699366f6083SPeter Grehan 	uint64_t		rip;
700366f6083SPeter Grehan 	union {
701d17b5104SNeel Natu 		struct vm_inout	inout;
702d17b5104SNeel Natu 		struct vm_inout_str inout_str;
703cd942e0fSPeter Grehan 		struct {
70413ec9371SPeter Grehan 			uint64_t	gpa;
705318224bbSNeel Natu 			int		fault_type;
706cd942e0fSPeter Grehan 		} paging;
707318224bbSNeel Natu 		struct {
708318224bbSNeel Natu 			uint64_t	gpa;
709318224bbSNeel Natu 			uint64_t	gla;
710e4f605eeSTycho Nightingale 			uint64_t	cs_base;
711f7a9f178SNeel Natu 			int		cs_d;		/* CS.D */
712e813a873SNeel Natu 			struct vm_guest_paging paging;
713318224bbSNeel Natu 			struct vie	vie;
714318224bbSNeel Natu 		} inst_emul;
715366f6083SPeter Grehan 		/*
716366f6083SPeter Grehan 		 * VMX specific payload. Used when there is no "better"
717366f6083SPeter Grehan 		 * exitcode to represent the VM-exit.
718366f6083SPeter Grehan 		 */
719366f6083SPeter Grehan 		struct {
7200492757cSNeel Natu 			int		status;		/* vmx inst status */
7210492757cSNeel Natu 			/*
7220492757cSNeel Natu 			 * 'exit_reason' and 'exit_qualification' are valid
7230492757cSNeel Natu 			 * only if 'status' is zero.
7240492757cSNeel Natu 			 */
725366f6083SPeter Grehan 			uint32_t	exit_reason;
726366f6083SPeter Grehan 			uint64_t	exit_qualification;
7270492757cSNeel Natu 			/*
7280492757cSNeel Natu 			 * 'inst_error' and 'inst_type' are valid
7290492757cSNeel Natu 			 * only if 'status' is non-zero.
7300492757cSNeel Natu 			 */
7310492757cSNeel Natu 			int		inst_type;
7320492757cSNeel Natu 			int		inst_error;
733366f6083SPeter Grehan 		} vmx;
734bbadcde4SNeel Natu 		/*
735bbadcde4SNeel Natu 		 * SVM specific payload.
736bbadcde4SNeel Natu 		 */
737bbadcde4SNeel Natu 		struct {
738bbadcde4SNeel Natu 			uint64_t	exitcode;
739bbadcde4SNeel Natu 			uint64_t	exitinfo1;
740bbadcde4SNeel Natu 			uint64_t	exitinfo2;
741bbadcde4SNeel Natu 		} svm;
742366f6083SPeter Grehan 		struct {
743cbd03a9dSJohn Baldwin 			int		inst_length;
744cbd03a9dSJohn Baldwin 		} bpt;
745cbd03a9dSJohn Baldwin 		struct {
746e3b4fe64SBojan Novković 			int		trace_trap;
747e3b4fe64SBojan Novković 			int		pushf_intercept;
748e3b4fe64SBojan Novković 			int		tf_shadow_val;
749e3b4fe64SBojan Novković 			struct		vm_guest_paging paging;
750e3b4fe64SBojan Novković 		} dbg;
751e3b4fe64SBojan Novković 		struct {
752366f6083SPeter Grehan 			uint32_t	code;		/* ecx value */
753366f6083SPeter Grehan 			uint64_t	wval;
754366f6083SPeter Grehan 		} msr;
755edf89256SNeel Natu 		struct {
756edf89256SNeel Natu 			int		vcpu;
757edf89256SNeel Natu 			uint64_t	rip;
758edf89256SNeel Natu 		} spinup_ap;
7591c052192SNeel Natu 		struct {
7601c052192SNeel Natu 			uint64_t	rflags;
761490768e2STycho Nightingale 			uint64_t	intr_status;
7621c052192SNeel Natu 		} hlt;
76330b94db8SNeel Natu 		struct {
76430b94db8SNeel Natu 			int		vector;
76530b94db8SNeel Natu 		} ioapic_eoi;
766f0fdcfe2SNeel Natu 		struct {
767f0fdcfe2SNeel Natu 			enum vm_suspend_how how;
768f0fdcfe2SNeel Natu 		} suspended;
7690bda8d3eSCorvin Köhne 		struct {
770e17eca32SMark Johnston 			/*
771e17eca32SMark Johnston 			 * The destination vCPU mask is saved in vcpu->cpuset
772e17eca32SMark Johnston 			 * and is copied out to userspace separately to avoid
773e17eca32SMark Johnston 			 * ABI concerns.
774e17eca32SMark Johnston 			 */
7750bda8d3eSCorvin Köhne 			uint32_t mode;
7760bda8d3eSCorvin Köhne 			uint8_t vector;
7770bda8d3eSCorvin Köhne 		} ipi;
7783d5444c8SNeel Natu 		struct vm_task_switch task_switch;
779366f6083SPeter Grehan 	} u;
780366f6083SPeter Grehan };
781366f6083SPeter Grehan 
782d37f2adbSNeel Natu /* APIs to inject faults into the guest */
783d3956e46SJohn Baldwin void vm_inject_fault(struct vcpu *vcpu, int vector, int errcode_valid,
784d3956e46SJohn Baldwin     int errcode);
785d3956e46SJohn Baldwin 
786d3956e46SJohn Baldwin static __inline void
787d3956e46SJohn Baldwin vm_inject_ud(struct vcpu *vcpu)
788d3956e46SJohn Baldwin {
789d3956e46SJohn Baldwin 	vm_inject_fault(vcpu, IDT_UD, 0, 0);
790d3956e46SJohn Baldwin }
791d3956e46SJohn Baldwin 
792d3956e46SJohn Baldwin static __inline void
793d3956e46SJohn Baldwin vm_inject_gp(struct vcpu *vcpu)
794d3956e46SJohn Baldwin {
795d3956e46SJohn Baldwin 	vm_inject_fault(vcpu, IDT_GP, 1, 0);
796d3956e46SJohn Baldwin }
797d3956e46SJohn Baldwin 
798d3956e46SJohn Baldwin static __inline void
799d3956e46SJohn Baldwin vm_inject_ac(struct vcpu *vcpu, int errcode)
800d3956e46SJohn Baldwin {
801d3956e46SJohn Baldwin 	vm_inject_fault(vcpu, IDT_AC, 1, errcode);
802d3956e46SJohn Baldwin }
803d3956e46SJohn Baldwin 
804d3956e46SJohn Baldwin static __inline void
805d3956e46SJohn Baldwin vm_inject_ss(struct vcpu *vcpu, int errcode)
806d3956e46SJohn Baldwin {
807d3956e46SJohn Baldwin 	vm_inject_fault(vcpu, IDT_SS, 1, errcode);
808d3956e46SJohn Baldwin }
809d3956e46SJohn Baldwin 
810d3956e46SJohn Baldwin void vm_inject_pf(struct vcpu *vcpu, int error_code, uint64_t cr2);
811c9c75df4SNeel Natu 
812366f6083SPeter Grehan #endif	/* _VMM_H_ */
813