vmm.h revision 347435
1221828Sgrehan/*-
2221828Sgrehan * Copyright (c) 2011 NetApp, Inc.
3221828Sgrehan * All rights reserved.
4221828Sgrehan *
5221828Sgrehan * Redistribution and use in source and binary forms, with or without
6221828Sgrehan * modification, are permitted provided that the following conditions
7221828Sgrehan * are met:
8221828Sgrehan * 1. Redistributions of source code must retain the above copyright
9221828Sgrehan *    notice, this list of conditions and the following disclaimer.
10221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
11221828Sgrehan *    notice, this list of conditions and the following disclaimer in the
12221828Sgrehan *    documentation and/or other materials provided with the distribution.
13221828Sgrehan *
14221828Sgrehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17221828Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20221828Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24221828Sgrehan * SUCH DAMAGE.
25221828Sgrehan *
26245678Sneel * $FreeBSD: stable/11/sys/amd64/include/vmm.h 347435 2019-05-10 16:32:44Z jhb $
27221828Sgrehan */
28221828Sgrehan
29221828Sgrehan#ifndef _VMM_H_
30221828Sgrehan#define	_VMM_H_
31221828Sgrehan
32347416Sjhb#include <sys/sdt.h>
33269042Sneel#include <x86/segments.h>
34269042Sneel
35347416Sjhb#ifdef _KERNEL
36347416SjhbSDT_PROVIDER_DECLARE(vmm);
37347416Sjhb#endif
38347416Sjhb
39265062Sneelenum vm_suspend_how {
40265062Sneel	VM_SUSPEND_NONE,
41265062Sneel	VM_SUSPEND_RESET,
42265062Sneel	VM_SUSPEND_POWEROFF,
43265203Sneel	VM_SUSPEND_HALT,
44268889Sneel	VM_SUSPEND_TRIPLEFAULT,
45265062Sneel	VM_SUSPEND_LAST
46265062Sneel};
47265062Sneel
48267338Stychon/*
49267338Stychon * Identifiers for architecturally defined registers.
50267338Stychon */
51267338Stychonenum vm_reg_name {
52267338Stychon	VM_REG_GUEST_RAX,
53267338Stychon	VM_REG_GUEST_RBX,
54267338Stychon	VM_REG_GUEST_RCX,
55267338Stychon	VM_REG_GUEST_RDX,
56267338Stychon	VM_REG_GUEST_RSI,
57267338Stychon	VM_REG_GUEST_RDI,
58267338Stychon	VM_REG_GUEST_RBP,
59267338Stychon	VM_REG_GUEST_R8,
60267338Stychon	VM_REG_GUEST_R9,
61267338Stychon	VM_REG_GUEST_R10,
62267338Stychon	VM_REG_GUEST_R11,
63267338Stychon	VM_REG_GUEST_R12,
64267338Stychon	VM_REG_GUEST_R13,
65267338Stychon	VM_REG_GUEST_R14,
66267338Stychon	VM_REG_GUEST_R15,
67267338Stychon	VM_REG_GUEST_CR0,
68267338Stychon	VM_REG_GUEST_CR3,
69267338Stychon	VM_REG_GUEST_CR4,
70267338Stychon	VM_REG_GUEST_DR7,
71267338Stychon	VM_REG_GUEST_RSP,
72267338Stychon	VM_REG_GUEST_RIP,
73267338Stychon	VM_REG_GUEST_RFLAGS,
74267338Stychon	VM_REG_GUEST_ES,
75267338Stychon	VM_REG_GUEST_CS,
76267338Stychon	VM_REG_GUEST_SS,
77267338Stychon	VM_REG_GUEST_DS,
78267338Stychon	VM_REG_GUEST_FS,
79267338Stychon	VM_REG_GUEST_GS,
80267338Stychon	VM_REG_GUEST_LDTR,
81267338Stychon	VM_REG_GUEST_TR,
82267338Stychon	VM_REG_GUEST_IDTR,
83267338Stychon	VM_REG_GUEST_GDTR,
84267338Stychon	VM_REG_GUEST_EFER,
85267338Stychon	VM_REG_GUEST_CR2,
86268777Sneel	VM_REG_GUEST_PDPTE0,
87268777Sneel	VM_REG_GUEST_PDPTE1,
88268777Sneel	VM_REG_GUEST_PDPTE2,
89268777Sneel	VM_REG_GUEST_PDPTE3,
90271451Sneel	VM_REG_GUEST_INTR_SHADOW,
91330623Sjhb	VM_REG_GUEST_DR0,
92330623Sjhb	VM_REG_GUEST_DR1,
93330623Sjhb	VM_REG_GUEST_DR2,
94330623Sjhb	VM_REG_GUEST_DR3,
95330623Sjhb	VM_REG_GUEST_DR6,
96267338Stychon	VM_REG_LAST
97267338Stychon};
98267338Stychon
99267338Stychonenum x2apic_state {
100267338Stychon	X2APIC_DISABLED,
101267338Stychon	X2APIC_ENABLED,
102267338Stychon	X2APIC_STATE_LAST
103267338Stychon};
104267338Stychon
105268889Sneel#define	VM_INTINFO_VECTOR(info)	((info) & 0xff)
106268889Sneel#define	VM_INTINFO_DEL_ERRCODE	0x800
107268889Sneel#define	VM_INTINFO_RSVD		0x7ffff000
108268889Sneel#define	VM_INTINFO_VALID	0x80000000
109268889Sneel#define	VM_INTINFO_TYPE		0x700
110268889Sneel#define	VM_INTINFO_HWINTR	(0 << 8)
111268889Sneel#define	VM_INTINFO_NMI		(2 << 8)
112268889Sneel#define	VM_INTINFO_HWEXCEPTION	(3 << 8)
113268889Sneel#define	VM_INTINFO_SWINTR	(4 << 8)
114268889Sneel
115221828Sgrehan#ifdef _KERNEL
116221828Sgrehan
117221828Sgrehan#define	VM_MAX_NAMELEN	32
118221828Sgrehan
119221828Sgrehanstruct vm;
120262506Sneelstruct vm_exception;
121221828Sgrehanstruct seg_desc;
122221828Sgrehanstruct vm_exit;
123221828Sgrehanstruct vm_run;
124258579Sneelstruct vhpet;
125258075Sneelstruct vioapic;
126221828Sgrehanstruct vlapic;
127256072Sneelstruct vmspace;
128256072Sneelstruct vm_object;
129269008Sneelstruct vm_guest_paging;
130256072Sneelstruct pmap;
131221828Sgrehan
132283657Sneelstruct vm_eventinfo {
133283657Sneel	void	*rptr;		/* rendezvous cookie */
134283657Sneel	int	*sptr;		/* suspend cookie */
135283657Sneel	int	*iptr;		/* reqidle cookie */
136283657Sneel};
137283657Sneel
138260466Sneeltypedef int	(*vmm_init_func_t)(int ipinum);
139221828Sgrehantypedef int	(*vmm_cleanup_func_t)(void);
140259782Sjhbtypedef void	(*vmm_resume_func_t)(void);
141256072Sneeltypedef void *	(*vmi_init_func_t)(struct vm *vm, struct pmap *pmap);
142256072Sneeltypedef int	(*vmi_run_func_t)(void *vmi, int vcpu, register_t rip,
143283657Sneel		    struct pmap *pmap, struct vm_eventinfo *info);
144221828Sgrehantypedef void	(*vmi_cleanup_func_t)(void *vmi);
145221828Sgrehantypedef int	(*vmi_get_register_t)(void *vmi, int vcpu, int num,
146221828Sgrehan				      uint64_t *retval);
147221828Sgrehantypedef int	(*vmi_set_register_t)(void *vmi, int vcpu, int num,
148221828Sgrehan				      uint64_t val);
149221828Sgrehantypedef int	(*vmi_get_desc_t)(void *vmi, int vcpu, int num,
150221828Sgrehan				  struct seg_desc *desc);
151221828Sgrehantypedef int	(*vmi_set_desc_t)(void *vmi, int vcpu, int num,
152221828Sgrehan				  struct seg_desc *desc);
153221828Sgrehantypedef int	(*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval);
154221828Sgrehantypedef int	(*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val);
155256072Sneeltypedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max);
156256072Sneeltypedef void	(*vmi_vmspace_free)(struct vmspace *vmspace);
157259863Sneeltypedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu);
158259863Sneeltypedef void	(*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic);
159221828Sgrehan
160221828Sgrehanstruct vmm_ops {
161221828Sgrehan	vmm_init_func_t		init;		/* module wide initialization */
162221828Sgrehan	vmm_cleanup_func_t	cleanup;
163259782Sjhb	vmm_resume_func_t	resume;
164221828Sgrehan
165221828Sgrehan	vmi_init_func_t		vminit;		/* vm-specific initialization */
166221828Sgrehan	vmi_run_func_t		vmrun;
167221828Sgrehan	vmi_cleanup_func_t	vmcleanup;
168221828Sgrehan	vmi_get_register_t	vmgetreg;
169221828Sgrehan	vmi_set_register_t	vmsetreg;
170221828Sgrehan	vmi_get_desc_t		vmgetdesc;
171221828Sgrehan	vmi_set_desc_t		vmsetdesc;
172221828Sgrehan	vmi_get_cap_t		vmgetcap;
173221828Sgrehan	vmi_set_cap_t		vmsetcap;
174256072Sneel	vmi_vmspace_alloc	vmspace_alloc;
175256072Sneel	vmi_vmspace_free	vmspace_free;
176259863Sneel	vmi_vlapic_init		vlapic_init;
177259863Sneel	vmi_vlapic_cleanup	vlapic_cleanup;
178221828Sgrehan};
179221828Sgrehan
180221828Sgrehanextern struct vmm_ops vmm_ops_intel;
181221828Sgrehanextern struct vmm_ops vmm_ops_amd;
182221828Sgrehan
183249396Sneelint vm_create(const char *name, struct vm **retvm);
184221828Sgrehanvoid vm_destroy(struct vm *vm);
185267216Sneelint vm_reinit(struct vm *vm);
186221828Sgrehanconst char *vm_name(struct vm *vm);
187284539Sneel
188284539Sneel/*
189284539Sneel * APIs that modify the guest memory map require all vcpus to be frozen.
190284539Sneel */
191284539Sneelint vm_mmap_memseg(struct vm *vm, vm_paddr_t gpa, int segid, vm_ooffset_t off,
192284539Sneel    size_t len, int prot, int flags);
193284539Sneelint vm_alloc_memseg(struct vm *vm, int ident, size_t len, bool sysmem);
194284539Sneelvoid vm_free_memseg(struct vm *vm, int ident);
195221828Sgrehanint vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
196221828Sgrehanint vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
197284539Sneelint vm_assign_pptdev(struct vm *vm, int bus, int slot, int func);
198284539Sneelint vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func);
199284539Sneel
200284539Sneel/*
201284539Sneel * APIs that inspect the guest memory map require only a *single* vcpu to
202284539Sneel * be frozen. This acts like a read lock on the guest memory map since any
203284539Sneel * modification requires *all* vcpus to be frozen.
204284539Sneel */
205284539Sneelint vm_mmap_getnext(struct vm *vm, vm_paddr_t *gpa, int *segid,
206284539Sneel    vm_ooffset_t *segoff, size_t *len, int *prot, int *flags);
207284539Sneelint vm_get_memseg(struct vm *vm, int ident, size_t *len, bool *sysmem,
208284539Sneel    struct vm_object **objptr);
209347409Sjhbvm_paddr_t vmm_sysmem_maxaddr(struct vm *vm);
210284539Sneelvoid *vm_gpa_hold(struct vm *, int vcpuid, vm_paddr_t gpa, size_t len,
211284539Sneel    int prot, void **cookie);
212256072Sneelvoid vm_gpa_release(void *cookie);
213284539Sneelbool vm_mem_allocated(struct vm *vm, int vcpuid, vm_paddr_t gpa);
214284539Sneel
215221828Sgrehanint vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval);
216221828Sgrehanint vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val);
217221828Sgrehanint vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
218221828Sgrehan		    struct seg_desc *ret_desc);
219221828Sgrehanint vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
220221828Sgrehan		    struct seg_desc *desc);
221221828Sgrehanint vm_run(struct vm *vm, struct vm_run *vmrun);
222265062Sneelint vm_suspend(struct vm *vm, enum vm_suspend_how how);
223221828Sgrehanint vm_inject_nmi(struct vm *vm, int vcpu);
224241982Sneelint vm_nmi_pending(struct vm *vm, int vcpuid);
225241982Sneelvoid vm_nmi_clear(struct vm *vm, int vcpuid);
226263211Stychonint vm_inject_extint(struct vm *vm, int vcpu);
227263211Stychonint vm_extint_pending(struct vm *vm, int vcpuid);
228263211Stychonvoid vm_extint_clear(struct vm *vm, int vcpuid);
229221828Sgrehanstruct vlapic *vm_lapic(struct vm *vm, int cpu);
230258075Sneelstruct vioapic *vm_ioapic(struct vm *vm);
231258579Sneelstruct vhpet *vm_hpet(struct vm *vm);
232221828Sgrehanint vm_get_capability(struct vm *vm, int vcpu, int type, int *val);
233221828Sgrehanint vm_set_capability(struct vm *vm, int vcpu, int type, int val);
234240922Sneelint vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state);
235240922Sneelint vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state);
236258075Sneelint vm_apicid2vcpuid(struct vm *vm, int apicid);
237266933Sneelint vm_activate_cpu(struct vm *vm, int vcpu);
238240894Sneelstruct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid);
239265062Sneelvoid vm_exit_suspended(struct vm *vm, int vcpuid, uint64_t rip);
240267330Sneelvoid vm_exit_rendezvous(struct vm *vm, int vcpuid, uint64_t rip);
241267330Sneelvoid vm_exit_astpending(struct vm *vm, int vcpuid, uint64_t rip);
242283657Sneelvoid vm_exit_reqidle(struct vm *vm, int vcpuid, uint64_t rip);
243221828Sgrehan
244282287Sneel#ifdef _SYS__CPUSET_H_
245221828Sgrehan/*
246260619Sneel * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'.
247260619Sneel * The rendezvous 'func(arg)' is not allowed to do anything that will
248260619Sneel * cause the thread to be put to sleep.
249260619Sneel *
250260619Sneel * If the rendezvous is being initiated from a vcpu context then the
251260619Sneel * 'vcpuid' must refer to that vcpu, otherwise it should be set to -1.
252260619Sneel *
253260619Sneel * The caller cannot hold any locks when initiating the rendezvous.
254260619Sneel *
255260619Sneel * The implementation of this API may cause vcpus other than those specified
256260619Sneel * by 'dest' to be stalled. The caller should not rely on any vcpus making
257260619Sneel * forward progress when the rendezvous is in progress.
258260619Sneel */
259260619Sneeltypedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg);
260260619Sneelvoid vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest,
261260619Sneel    vm_rendezvous_func_t func, void *arg);
262282287Sneelcpuset_t vm_active_cpus(struct vm *vm);
263282287Sneelcpuset_t vm_suspended_cpus(struct vm *vm);
264282287Sneel#endif	/* _SYS__CPUSET_H_ */
265260619Sneel
266260619Sneelstatic __inline int
267283657Sneelvcpu_rendezvous_pending(struct vm_eventinfo *info)
268260619Sneel{
269260619Sneel
270283657Sneel	return (*((uintptr_t *)(info->rptr)) != 0);
271260619Sneel}
272260619Sneel
273263780Sneelstatic __inline int
274283657Sneelvcpu_suspended(struct vm_eventinfo *info)
275263780Sneel{
276263780Sneel
277283657Sneel	return (*info->sptr);
278263780Sneel}
279263780Sneel
280283657Sneelstatic __inline int
281283657Sneelvcpu_reqidle(struct vm_eventinfo *info)
282283657Sneel{
283283657Sneel
284283657Sneel	return (*info->iptr);
285283657Sneel}
286283657Sneel
287260619Sneel/*
288221828Sgrehan * Return 1 if device indicated by bus/slot/func is supposed to be a
289221828Sgrehan * pci passthrough device.
290221828Sgrehan *
291221828Sgrehan * Return 0 otherwise.
292221828Sgrehan */
293221828Sgrehanint vmm_is_pptdev(int bus, int slot, int func);
294221828Sgrehan
295221828Sgrehanvoid *vm_iommu_domain(struct vm *vm);
296221828Sgrehan
297241489Sneelenum vcpu_state {
298241489Sneel	VCPU_IDLE,
299256072Sneel	VCPU_FROZEN,
300241489Sneel	VCPU_RUNNING,
301256072Sneel	VCPU_SLEEPING,
302241489Sneel};
303221828Sgrehan
304259737Sneelint vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state,
305259737Sneel    bool from_idle);
306249879Sgrehanenum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu);
307221828Sgrehan
308221828Sgrehanstatic int __inline
309249879Sgrehanvcpu_is_running(struct vm *vm, int vcpu, int *hostcpu)
310221828Sgrehan{
311249879Sgrehan	return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING);
312221828Sgrehan}
313221828Sgrehan
314269109Sneel#ifdef _SYS_PROC_H_
315269109Sneelstatic int __inline
316269109Sneelvcpu_should_yield(struct vm *vm, int vcpu)
317269109Sneel{
318282571Sneel
319282571Sneel	if (curthread->td_flags & (TDF_ASTPENDING | TDF_NEEDRESCHED))
320282571Sneel		return (1);
321282571Sneel	else if (curthread->td_owepreempt)
322282571Sneel		return (1);
323282571Sneel	else
324282571Sneel		return (0);
325269109Sneel}
326269109Sneel#endif
327269109Sneel
328241489Sneelvoid *vcpu_stats(struct vm *vm, int vcpu);
329259863Sneelvoid vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr);
330256072Sneelstruct vmspace *vm_get_vmspace(struct vm *vm);
331263035Stychonstruct vatpic *vm_atpic(struct vm *vm);
332263744Stychonstruct vatpit *vm_atpit(struct vm *vm);
333273683Sneelstruct vpmtmr *vm_pmtmr(struct vm *vm);
334276428Sneelstruct vrtc *vm_rtc(struct vm *vm);
335262506Sneel
336262506Sneel/*
337277149Sneel * Inject exception 'vector' into the guest vcpu. This function returns 0 on
338262506Sneel * success and non-zero on failure.
339262506Sneel *
340262506Sneel * Wrapper functions like 'vm_inject_gp()' should be preferred to calling
341262506Sneel * this function directly because they enforce the trap-like or fault-like
342262506Sneel * behavior of an exception.
343262506Sneel *
344262506Sneel * This function should only be called in the context of the thread that is
345262506Sneel * executing this vcpu.
346262506Sneel */
347277149Sneelint vm_inject_exception(struct vm *vm, int vcpuid, int vector, int err_valid,
348277149Sneel    uint32_t errcode, int restart_instruction);
349262506Sneel
350262506Sneel/*
351268889Sneel * This function is called after a VM-exit that occurred during exception or
352268889Sneel * interrupt delivery through the IDT. The format of 'intinfo' is described
353268889Sneel * in Figure 15-1, "EXITINTINFO for All Intercepts", APM, Vol 2.
354262506Sneel *
355268889Sneel * If a VM-exit handler completes the event delivery successfully then it
356268889Sneel * should call vm_exit_intinfo() to extinguish the pending event. For e.g.,
357268889Sneel * if the task switch emulation is triggered via a task gate then it should
358268889Sneel * call this function with 'intinfo=0' to indicate that the external event
359268889Sneel * is not pending anymore.
360268889Sneel *
361268889Sneel * Return value is 0 on success and non-zero on failure.
362262506Sneel */
363268889Sneelint vm_exit_intinfo(struct vm *vm, int vcpuid, uint64_t intinfo);
364262506Sneel
365268889Sneel/*
366268889Sneel * This function is called before every VM-entry to retrieve a pending
367268889Sneel * event that should be injected into the guest. This function combines
368268889Sneel * nested events into a double or triple fault.
369268889Sneel *
370268889Sneel * Returns 0 if there are no events that need to be injected into the guest
371268889Sneel * and non-zero otherwise.
372268889Sneel */
373268889Sneelint vm_entry_intinfo(struct vm *vm, int vcpuid, uint64_t *info);
374268889Sneel
375268889Sneelint vm_get_intinfo(struct vm *vm, int vcpuid, uint64_t *info1, uint64_t *info2);
376268889Sneel
377266573Sneelenum vm_reg_name vm_segment_name(int seg_encoding);
378266573Sneel
379269008Sneelstruct vm_copyinfo {
380269008Sneel	uint64_t	gpa;
381269008Sneel	size_t		len;
382269008Sneel	void		*hva;
383269008Sneel	void		*cookie;
384269008Sneel};
385269008Sneel
386269008Sneel/*
387269008Sneel * Set up 'copyinfo[]' to copy to/from guest linear address space starting
388269008Sneel * at 'gla' and 'len' bytes long. The 'prot' should be set to PROT_READ for
389269008Sneel * a copyin or PROT_WRITE for a copyout.
390269008Sneel *
391299010Spfg * retval	is_fault	Interpretation
392282558Sneel *   0		   0		Success
393282558Sneel *   0		   1		An exception was injected into the guest
394282558Sneel * EFAULT	  N/A		Unrecoverable error
395269008Sneel *
396269008Sneel * The 'copyinfo[]' can be passed to 'vm_copyin()' or 'vm_copyout()' only if
397269008Sneel * the return value is 0. The 'copyinfo[]' resources should be freed by calling
398269008Sneel * 'vm_copy_teardown()' after the copy is done.
399269008Sneel */
400269008Sneelint vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging,
401269008Sneel    uint64_t gla, size_t len, int prot, struct vm_copyinfo *copyinfo,
402282558Sneel    int num_copyinfo, int *is_fault);
403269008Sneelvoid vm_copy_teardown(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo,
404269008Sneel    int num_copyinfo);
405269008Sneelvoid vm_copyin(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo,
406269008Sneel    void *kaddr, size_t len);
407269008Sneelvoid vm_copyout(struct vm *vm, int vcpuid, const void *kaddr,
408269008Sneel    struct vm_copyinfo *copyinfo, size_t len);
409276098Sneel
410276098Sneelint vcpu_trace_exceptions(struct vm *vm, int vcpuid);
411221828Sgrehan#endif	/* KERNEL */
412221828Sgrehan
413255438Sgrehan#define	VM_MAXCPU	16			/* maximum virtual cpus */
414221828Sgrehan
415221828Sgrehan/*
416221828Sgrehan * Identifiers for optional vmm capabilities
417221828Sgrehan */
418221828Sgrehanenum vm_cap_type {
419221828Sgrehan	VM_CAP_HALT_EXIT,
420221828Sgrehan	VM_CAP_MTRAP_EXIT,
421221828Sgrehan	VM_CAP_PAUSE_EXIT,
422221828Sgrehan	VM_CAP_UNRESTRICTED_GUEST,
423256645Sneel	VM_CAP_ENABLE_INVPCID,
424221828Sgrehan	VM_CAP_MAX
425221828Sgrehan};
426221828Sgrehan
427266125Sjhbenum vm_intr_trigger {
428266125Sjhb	EDGE_TRIGGER,
429266125Sjhb	LEVEL_TRIGGER
430266125Sjhb};
431266125Sjhb
432221828Sgrehan/*
433221828Sgrehan * The 'access' field has the format specified in Table 21-2 of the Intel
434221828Sgrehan * Architecture Manual vol 3b.
435221828Sgrehan *
436221828Sgrehan * XXX The contents of the 'access' field are architecturally defined except
437221828Sgrehan * bit 16 - Segment Unusable.
438221828Sgrehan */
439221828Sgrehanstruct seg_desc {
440221828Sgrehan	uint64_t	base;
441221828Sgrehan	uint32_t	limit;
442221828Sgrehan	uint32_t	access;
443221828Sgrehan};
444268701Sneel#define	SEG_DESC_TYPE(access)		((access) & 0x001f)
445268777Sneel#define	SEG_DESC_DPL(access)		(((access) >> 5) & 0x3)
446268701Sneel#define	SEG_DESC_PRESENT(access)	(((access) & 0x0080) ? 1 : 0)
447268701Sneel#define	SEG_DESC_DEF32(access)		(((access) & 0x4000) ? 1 : 0)
448268701Sneel#define	SEG_DESC_GRANULARITY(access)	(((access) & 0x8000) ? 1 : 0)
449268701Sneel#define	SEG_DESC_UNUSABLE(access)	(((access) & 0x10000) ? 1 : 0)
450221828Sgrehan
451266627Sneelenum vm_cpu_mode {
452268428Sneel	CPU_MODE_REAL,
453268428Sneel	CPU_MODE_PROTECTED,
454266627Sneel	CPU_MODE_COMPATIBILITY,		/* IA-32E mode (CS.L = 0) */
455266627Sneel	CPU_MODE_64BIT,			/* IA-32E mode (CS.L = 1) */
456266627Sneel};
457266627Sneel
458266627Sneelenum vm_paging_mode {
459266627Sneel	PAGING_MODE_FLAT,
460266627Sneel	PAGING_MODE_32,
461266627Sneel	PAGING_MODE_PAE,
462266627Sneel	PAGING_MODE_64,
463266627Sneel};
464266627Sneel
465266627Sneelstruct vm_guest_paging {
466266627Sneel	uint64_t	cr3;
467266627Sneel	int		cpl;
468266627Sneel	enum vm_cpu_mode cpu_mode;
469266627Sneel	enum vm_paging_mode paging_mode;
470266627Sneel};
471266627Sneel
472266627Sneel/*
473266627Sneel * The data structures 'vie' and 'vie_op' are meant to be opaque to the
474266627Sneel * consumers of instruction decoding. The only reason why their contents
475266627Sneel * need to be exposed is because they are part of the 'vm_exit' structure.
476266627Sneel */
477266627Sneelstruct vie_op {
478266627Sneel	uint8_t		op_byte;	/* actual opcode byte */
479266627Sneel	uint8_t		op_type;	/* type of operation (e.g. MOV) */
480266627Sneel	uint16_t	op_flags;
481266627Sneel};
482266627Sneel
483266627Sneel#define	VIE_INST_SIZE	15
484266627Sneelstruct vie {
485266627Sneel	uint8_t		inst[VIE_INST_SIZE];	/* instruction bytes */
486266627Sneel	uint8_t		num_valid;		/* size of the instruction */
487266627Sneel	uint8_t		num_processed;
488266627Sneel
489268701Sneel	uint8_t		addrsize:4, opsize:4;	/* address and operand sizes */
490266627Sneel	uint8_t		rex_w:1,		/* REX prefix */
491266627Sneel			rex_r:1,
492266627Sneel			rex_x:1,
493266627Sneel			rex_b:1,
494268701Sneel			rex_present:1,
495277360Sneel			repz_present:1,		/* REP/REPE/REPZ prefix */
496277360Sneel			repnz_present:1,	/* REPNE/REPNZ prefix */
497268701Sneel			opsize_override:1,	/* Operand size override */
498277360Sneel			addrsize_override:1,	/* Address size override */
499277360Sneel			segment_override:1;	/* Segment override */
500266627Sneel
501266627Sneel	uint8_t		mod:2,			/* ModRM byte */
502266627Sneel			reg:4,
503266627Sneel			rm:4;
504266627Sneel
505266627Sneel	uint8_t		ss:2,			/* SIB byte */
506266627Sneel			index:4,
507266627Sneel			base:4;
508266627Sneel
509266627Sneel	uint8_t		disp_bytes;
510266627Sneel	uint8_t		imm_bytes;
511266627Sneel
512266627Sneel	uint8_t		scale;
513266627Sneel	int		base_register;		/* VM_REG_GUEST_xyz */
514266627Sneel	int		index_register;		/* VM_REG_GUEST_xyz */
515277360Sneel	int		segment_register;	/* VM_REG_GUEST_xyz */
516266627Sneel
517266627Sneel	int64_t		displacement;		/* optional addr displacement */
518266627Sneel	int64_t		immediate;		/* optional immediate operand */
519266627Sneel
520266627Sneel	uint8_t		decoded;	/* set to 1 if successfully decoded */
521266627Sneel
522266627Sneel	struct vie_op	op;			/* opcode description */
523266627Sneel};
524266627Sneel
525221828Sgrehanenum vm_exitcode {
526221828Sgrehan	VM_EXITCODE_INOUT,
527221828Sgrehan	VM_EXITCODE_VMX,
528221828Sgrehan	VM_EXITCODE_BOGUS,
529221828Sgrehan	VM_EXITCODE_RDMSR,
530221828Sgrehan	VM_EXITCODE_WRMSR,
531221828Sgrehan	VM_EXITCODE_HLT,
532221828Sgrehan	VM_EXITCODE_MTRAP,
533221828Sgrehan	VM_EXITCODE_PAUSE,
534234761Sgrehan	VM_EXITCODE_PAGING,
535256072Sneel	VM_EXITCODE_INST_EMUL,
536240912Sneel	VM_EXITCODE_SPINUP_AP,
537265101Sneel	VM_EXITCODE_DEPRECATED1,	/* used to be SPINDOWN_CPU */
538260619Sneel	VM_EXITCODE_RENDEZVOUS,
539261170Sneel	VM_EXITCODE_IOAPIC_EOI,
540263780Sneel	VM_EXITCODE_SUSPENDED,
541266573Sneel	VM_EXITCODE_INOUT_STR,
542268777Sneel	VM_EXITCODE_TASK_SWITCH,
543272670Sneel	VM_EXITCODE_MONITOR,
544272670Sneel	VM_EXITCODE_MWAIT,
545273375Sneel	VM_EXITCODE_SVM,
546283657Sneel	VM_EXITCODE_REQIDLE,
547347435Sjhb	VM_EXITCODE_VMINSN,
548234761Sgrehan	VM_EXITCODE_MAX
549221828Sgrehan};
550221828Sgrehan
551266573Sneelstruct vm_inout {
552266573Sneel	uint16_t	bytes:3;	/* 1 or 2 or 4 */
553266573Sneel	uint16_t	in:1;
554266573Sneel	uint16_t	string:1;
555266573Sneel	uint16_t	rep:1;
556266573Sneel	uint16_t	port;
557266573Sneel	uint32_t	eax;		/* valid for out */
558266573Sneel};
559266573Sneel
560266573Sneelstruct vm_inout_str {
561266573Sneel	struct vm_inout	inout;		/* must be the first element */
562266627Sneel	struct vm_guest_paging paging;
563266573Sneel	uint64_t	rflags;
564266573Sneel	uint64_t	cr0;
565266573Sneel	uint64_t	index;
566266573Sneel	uint64_t	count;		/* rep=1 (%rcx), rep=0 (1) */
567266573Sneel	int		addrsize;
568266573Sneel	enum vm_reg_name seg_name;
569266573Sneel	struct seg_desc seg_desc;
570266573Sneel};
571266573Sneel
572268777Sneelenum task_switch_reason {
573268777Sneel	TSR_CALL,
574268777Sneel	TSR_IRET,
575268777Sneel	TSR_JMP,
576268777Sneel	TSR_IDT_GATE,	/* task gate in IDT */
577268777Sneel};
578268777Sneel
579268777Sneelstruct vm_task_switch {
580268777Sneel	uint16_t	tsssel;		/* new TSS selector */
581268777Sneel	int		ext;		/* task switch due to external event */
582268777Sneel	uint32_t	errcode;
583268777Sneel	int		errcode_valid;	/* push 'errcode' on the new stack */
584268777Sneel	enum task_switch_reason reason;
585268777Sneel	struct vm_guest_paging paging;
586268777Sneel};
587268777Sneel
588221828Sgrehanstruct vm_exit {
589221828Sgrehan	enum vm_exitcode	exitcode;
590221828Sgrehan	int			inst_length;	/* 0 means unknown */
591221828Sgrehan	uint64_t		rip;
592221828Sgrehan	union {
593266573Sneel		struct vm_inout	inout;
594266573Sneel		struct vm_inout_str inout_str;
595221828Sgrehan		struct {
596241497Sgrehan			uint64_t	gpa;
597256072Sneel			int		fault_type;
598256072Sneel		} paging;
599256072Sneel		struct {
600256072Sneel			uint64_t	gpa;
601256072Sneel			uint64_t	gla;
602280447Stychon			uint64_t	cs_base;
603268701Sneel			int		cs_d;		/* CS.D */
604266627Sneel			struct vm_guest_paging paging;
605243640Sneel			struct vie	vie;
606256072Sneel		} inst_emul;
607221828Sgrehan		/*
608221828Sgrehan		 * VMX specific payload. Used when there is no "better"
609221828Sgrehan		 * exitcode to represent the VM-exit.
610221828Sgrehan		 */
611221828Sgrehan		struct {
612260167Sneel			int		status;		/* vmx inst status */
613260167Sneel			/*
614260167Sneel			 * 'exit_reason' and 'exit_qualification' are valid
615260167Sneel			 * only if 'status' is zero.
616260167Sneel			 */
617221828Sgrehan			uint32_t	exit_reason;
618221828Sgrehan			uint64_t	exit_qualification;
619260167Sneel			/*
620260167Sneel			 * 'inst_error' and 'inst_type' are valid
621260167Sneel			 * only if 'status' is non-zero.
622260167Sneel			 */
623260167Sneel			int		inst_type;
624260167Sneel			int		inst_error;
625221828Sgrehan		} vmx;
626273375Sneel		/*
627273375Sneel		 * SVM specific payload.
628273375Sneel		 */
629221828Sgrehan		struct {
630273375Sneel			uint64_t	exitcode;
631273375Sneel			uint64_t	exitinfo1;
632273375Sneel			uint64_t	exitinfo2;
633273375Sneel		} svm;
634273375Sneel		struct {
635221828Sgrehan			uint32_t	code;		/* ecx value */
636221828Sgrehan			uint64_t	wval;
637221828Sgrehan		} msr;
638240912Sneel		struct {
639240912Sneel			int		vcpu;
640240912Sneel			uint64_t	rip;
641240912Sneel		} spinup_ap;
642259081Sneel		struct {
643259081Sneel			uint64_t	rflags;
644347074Sjhb			uint64_t	intr_status;
645259081Sneel		} hlt;
646261170Sneel		struct {
647261170Sneel			int		vector;
648261170Sneel		} ioapic_eoi;
649265062Sneel		struct {
650265062Sneel			enum vm_suspend_how how;
651265062Sneel		} suspended;
652268777Sneel		struct vm_task_switch task_switch;
653221828Sgrehan	} u;
654221828Sgrehan};
655221828Sgrehan
656269042Sneel/* APIs to inject faults into the guest */
657269042Sneelvoid vm_inject_fault(void *vm, int vcpuid, int vector, int errcode_valid,
658269042Sneel    int errcode);
659269042Sneel
660270438Sgrehanstatic __inline void
661269042Sneelvm_inject_ud(void *vm, int vcpuid)
662269042Sneel{
663269042Sneel	vm_inject_fault(vm, vcpuid, IDT_UD, 0, 0);
664269042Sneel}
665269042Sneel
666270438Sgrehanstatic __inline void
667269042Sneelvm_inject_gp(void *vm, int vcpuid)
668269042Sneel{
669269042Sneel	vm_inject_fault(vm, vcpuid, IDT_GP, 1, 0);
670269042Sneel}
671269042Sneel
672270438Sgrehanstatic __inline void
673269042Sneelvm_inject_ac(void *vm, int vcpuid, int errcode)
674269042Sneel{
675269042Sneel	vm_inject_fault(vm, vcpuid, IDT_AC, 1, errcode);
676269042Sneel}
677269042Sneel
678270438Sgrehanstatic __inline void
679269042Sneelvm_inject_ss(void *vm, int vcpuid, int errcode)
680269042Sneel{
681269042Sneel	vm_inject_fault(vm, vcpuid, IDT_SS, 1, errcode);
682269042Sneel}
683269042Sneel
684269042Sneelvoid vm_inject_pf(void *vm, int vcpuid, int error_code, uint64_t cr2);
685269042Sneel
686277149Sneelint vm_restart_instruction(void *vm, int vcpuid);
687277149Sneel
688221828Sgrehan#endif	/* _VMM_H_ */
689