vmm.h revision 348201
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 348201 2019-05-23 21:23:18Z rgrimes $
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);
187348201Srgrimesvoid vm_get_topology(struct vm *vm, uint16_t *sockets, uint16_t *cores,
188348201Srgrimes    uint16_t *threads, uint16_t *maxcpus);
189348201Srgrimesint vm_set_topology(struct vm *vm, uint16_t sockets, uint16_t cores,
190348201Srgrimes    uint16_t threads, uint16_t maxcpus);
191284539Sneel
192284539Sneel/*
193284539Sneel * APIs that modify the guest memory map require all vcpus to be frozen.
194284539Sneel */
195284539Sneelint vm_mmap_memseg(struct vm *vm, vm_paddr_t gpa, int segid, vm_ooffset_t off,
196284539Sneel    size_t len, int prot, int flags);
197284539Sneelint vm_alloc_memseg(struct vm *vm, int ident, size_t len, bool sysmem);
198284539Sneelvoid vm_free_memseg(struct vm *vm, int ident);
199221828Sgrehanint vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
200221828Sgrehanint vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
201284539Sneelint vm_assign_pptdev(struct vm *vm, int bus, int slot, int func);
202284539Sneelint vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func);
203284539Sneel
204284539Sneel/*
205284539Sneel * APIs that inspect the guest memory map require only a *single* vcpu to
206284539Sneel * be frozen. This acts like a read lock on the guest memory map since any
207284539Sneel * modification requires *all* vcpus to be frozen.
208284539Sneel */
209284539Sneelint vm_mmap_getnext(struct vm *vm, vm_paddr_t *gpa, int *segid,
210284539Sneel    vm_ooffset_t *segoff, size_t *len, int *prot, int *flags);
211284539Sneelint vm_get_memseg(struct vm *vm, int ident, size_t *len, bool *sysmem,
212284539Sneel    struct vm_object **objptr);
213347409Sjhbvm_paddr_t vmm_sysmem_maxaddr(struct vm *vm);
214284539Sneelvoid *vm_gpa_hold(struct vm *, int vcpuid, vm_paddr_t gpa, size_t len,
215284539Sneel    int prot, void **cookie);
216256072Sneelvoid vm_gpa_release(void *cookie);
217284539Sneelbool vm_mem_allocated(struct vm *vm, int vcpuid, vm_paddr_t gpa);
218284539Sneel
219221828Sgrehanint vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval);
220221828Sgrehanint vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val);
221221828Sgrehanint vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
222221828Sgrehan		    struct seg_desc *ret_desc);
223221828Sgrehanint vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
224221828Sgrehan		    struct seg_desc *desc);
225221828Sgrehanint vm_run(struct vm *vm, struct vm_run *vmrun);
226265062Sneelint vm_suspend(struct vm *vm, enum vm_suspend_how how);
227221828Sgrehanint vm_inject_nmi(struct vm *vm, int vcpu);
228241982Sneelint vm_nmi_pending(struct vm *vm, int vcpuid);
229241982Sneelvoid vm_nmi_clear(struct vm *vm, int vcpuid);
230263211Stychonint vm_inject_extint(struct vm *vm, int vcpu);
231263211Stychonint vm_extint_pending(struct vm *vm, int vcpuid);
232263211Stychonvoid vm_extint_clear(struct vm *vm, int vcpuid);
233221828Sgrehanstruct vlapic *vm_lapic(struct vm *vm, int cpu);
234258075Sneelstruct vioapic *vm_ioapic(struct vm *vm);
235258579Sneelstruct vhpet *vm_hpet(struct vm *vm);
236221828Sgrehanint vm_get_capability(struct vm *vm, int vcpu, int type, int *val);
237221828Sgrehanint vm_set_capability(struct vm *vm, int vcpu, int type, int val);
238240922Sneelint vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state);
239240922Sneelint vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state);
240258075Sneelint vm_apicid2vcpuid(struct vm *vm, int apicid);
241266933Sneelint vm_activate_cpu(struct vm *vm, int vcpu);
242240894Sneelstruct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid);
243265062Sneelvoid vm_exit_suspended(struct vm *vm, int vcpuid, uint64_t rip);
244267330Sneelvoid vm_exit_rendezvous(struct vm *vm, int vcpuid, uint64_t rip);
245267330Sneelvoid vm_exit_astpending(struct vm *vm, int vcpuid, uint64_t rip);
246283657Sneelvoid vm_exit_reqidle(struct vm *vm, int vcpuid, uint64_t rip);
247221828Sgrehan
248282287Sneel#ifdef _SYS__CPUSET_H_
249221828Sgrehan/*
250260619Sneel * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'.
251260619Sneel * The rendezvous 'func(arg)' is not allowed to do anything that will
252260619Sneel * cause the thread to be put to sleep.
253260619Sneel *
254260619Sneel * If the rendezvous is being initiated from a vcpu context then the
255260619Sneel * 'vcpuid' must refer to that vcpu, otherwise it should be set to -1.
256260619Sneel *
257260619Sneel * The caller cannot hold any locks when initiating the rendezvous.
258260619Sneel *
259260619Sneel * The implementation of this API may cause vcpus other than those specified
260260619Sneel * by 'dest' to be stalled. The caller should not rely on any vcpus making
261260619Sneel * forward progress when the rendezvous is in progress.
262260619Sneel */
263260619Sneeltypedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg);
264260619Sneelvoid vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest,
265260619Sneel    vm_rendezvous_func_t func, void *arg);
266282287Sneelcpuset_t vm_active_cpus(struct vm *vm);
267282287Sneelcpuset_t vm_suspended_cpus(struct vm *vm);
268282287Sneel#endif	/* _SYS__CPUSET_H_ */
269260619Sneel
270260619Sneelstatic __inline int
271283657Sneelvcpu_rendezvous_pending(struct vm_eventinfo *info)
272260619Sneel{
273260619Sneel
274283657Sneel	return (*((uintptr_t *)(info->rptr)) != 0);
275260619Sneel}
276260619Sneel
277263780Sneelstatic __inline int
278283657Sneelvcpu_suspended(struct vm_eventinfo *info)
279263780Sneel{
280263780Sneel
281283657Sneel	return (*info->sptr);
282263780Sneel}
283263780Sneel
284283657Sneelstatic __inline int
285283657Sneelvcpu_reqidle(struct vm_eventinfo *info)
286283657Sneel{
287283657Sneel
288283657Sneel	return (*info->iptr);
289283657Sneel}
290283657Sneel
291260619Sneel/*
292221828Sgrehan * Return 1 if device indicated by bus/slot/func is supposed to be a
293221828Sgrehan * pci passthrough device.
294221828Sgrehan *
295221828Sgrehan * Return 0 otherwise.
296221828Sgrehan */
297221828Sgrehanint vmm_is_pptdev(int bus, int slot, int func);
298221828Sgrehan
299221828Sgrehanvoid *vm_iommu_domain(struct vm *vm);
300221828Sgrehan
301241489Sneelenum vcpu_state {
302241489Sneel	VCPU_IDLE,
303256072Sneel	VCPU_FROZEN,
304241489Sneel	VCPU_RUNNING,
305256072Sneel	VCPU_SLEEPING,
306241489Sneel};
307221828Sgrehan
308259737Sneelint vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state,
309259737Sneel    bool from_idle);
310249879Sgrehanenum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu);
311221828Sgrehan
312221828Sgrehanstatic int __inline
313249879Sgrehanvcpu_is_running(struct vm *vm, int vcpu, int *hostcpu)
314221828Sgrehan{
315249879Sgrehan	return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING);
316221828Sgrehan}
317221828Sgrehan
318269109Sneel#ifdef _SYS_PROC_H_
319269109Sneelstatic int __inline
320269109Sneelvcpu_should_yield(struct vm *vm, int vcpu)
321269109Sneel{
322282571Sneel
323282571Sneel	if (curthread->td_flags & (TDF_ASTPENDING | TDF_NEEDRESCHED))
324282571Sneel		return (1);
325282571Sneel	else if (curthread->td_owepreempt)
326282571Sneel		return (1);
327282571Sneel	else
328282571Sneel		return (0);
329269109Sneel}
330269109Sneel#endif
331269109Sneel
332241489Sneelvoid *vcpu_stats(struct vm *vm, int vcpu);
333259863Sneelvoid vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr);
334256072Sneelstruct vmspace *vm_get_vmspace(struct vm *vm);
335263035Stychonstruct vatpic *vm_atpic(struct vm *vm);
336263744Stychonstruct vatpit *vm_atpit(struct vm *vm);
337273683Sneelstruct vpmtmr *vm_pmtmr(struct vm *vm);
338276428Sneelstruct vrtc *vm_rtc(struct vm *vm);
339262506Sneel
340262506Sneel/*
341277149Sneel * Inject exception 'vector' into the guest vcpu. This function returns 0 on
342262506Sneel * success and non-zero on failure.
343262506Sneel *
344262506Sneel * Wrapper functions like 'vm_inject_gp()' should be preferred to calling
345262506Sneel * this function directly because they enforce the trap-like or fault-like
346262506Sneel * behavior of an exception.
347262506Sneel *
348262506Sneel * This function should only be called in the context of the thread that is
349262506Sneel * executing this vcpu.
350262506Sneel */
351277149Sneelint vm_inject_exception(struct vm *vm, int vcpuid, int vector, int err_valid,
352277149Sneel    uint32_t errcode, int restart_instruction);
353262506Sneel
354262506Sneel/*
355268889Sneel * This function is called after a VM-exit that occurred during exception or
356268889Sneel * interrupt delivery through the IDT. The format of 'intinfo' is described
357268889Sneel * in Figure 15-1, "EXITINTINFO for All Intercepts", APM, Vol 2.
358262506Sneel *
359268889Sneel * If a VM-exit handler completes the event delivery successfully then it
360268889Sneel * should call vm_exit_intinfo() to extinguish the pending event. For e.g.,
361268889Sneel * if the task switch emulation is triggered via a task gate then it should
362268889Sneel * call this function with 'intinfo=0' to indicate that the external event
363268889Sneel * is not pending anymore.
364268889Sneel *
365268889Sneel * Return value is 0 on success and non-zero on failure.
366262506Sneel */
367268889Sneelint vm_exit_intinfo(struct vm *vm, int vcpuid, uint64_t intinfo);
368262506Sneel
369268889Sneel/*
370268889Sneel * This function is called before every VM-entry to retrieve a pending
371268889Sneel * event that should be injected into the guest. This function combines
372268889Sneel * nested events into a double or triple fault.
373268889Sneel *
374268889Sneel * Returns 0 if there are no events that need to be injected into the guest
375268889Sneel * and non-zero otherwise.
376268889Sneel */
377268889Sneelint vm_entry_intinfo(struct vm *vm, int vcpuid, uint64_t *info);
378268889Sneel
379268889Sneelint vm_get_intinfo(struct vm *vm, int vcpuid, uint64_t *info1, uint64_t *info2);
380268889Sneel
381266573Sneelenum vm_reg_name vm_segment_name(int seg_encoding);
382266573Sneel
383269008Sneelstruct vm_copyinfo {
384269008Sneel	uint64_t	gpa;
385269008Sneel	size_t		len;
386269008Sneel	void		*hva;
387269008Sneel	void		*cookie;
388269008Sneel};
389269008Sneel
390269008Sneel/*
391269008Sneel * Set up 'copyinfo[]' to copy to/from guest linear address space starting
392269008Sneel * at 'gla' and 'len' bytes long. The 'prot' should be set to PROT_READ for
393269008Sneel * a copyin or PROT_WRITE for a copyout.
394269008Sneel *
395299010Spfg * retval	is_fault	Interpretation
396282558Sneel *   0		   0		Success
397282558Sneel *   0		   1		An exception was injected into the guest
398282558Sneel * EFAULT	  N/A		Unrecoverable error
399269008Sneel *
400269008Sneel * The 'copyinfo[]' can be passed to 'vm_copyin()' or 'vm_copyout()' only if
401269008Sneel * the return value is 0. The 'copyinfo[]' resources should be freed by calling
402269008Sneel * 'vm_copy_teardown()' after the copy is done.
403269008Sneel */
404269008Sneelint vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging,
405269008Sneel    uint64_t gla, size_t len, int prot, struct vm_copyinfo *copyinfo,
406282558Sneel    int num_copyinfo, int *is_fault);
407269008Sneelvoid vm_copy_teardown(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo,
408269008Sneel    int num_copyinfo);
409269008Sneelvoid vm_copyin(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo,
410269008Sneel    void *kaddr, size_t len);
411269008Sneelvoid vm_copyout(struct vm *vm, int vcpuid, const void *kaddr,
412269008Sneel    struct vm_copyinfo *copyinfo, size_t len);
413276098Sneel
414276098Sneelint vcpu_trace_exceptions(struct vm *vm, int vcpuid);
415221828Sgrehan#endif	/* KERNEL */
416221828Sgrehan
417255438Sgrehan#define	VM_MAXCPU	16			/* maximum virtual cpus */
418221828Sgrehan
419221828Sgrehan/*
420221828Sgrehan * Identifiers for optional vmm capabilities
421221828Sgrehan */
422221828Sgrehanenum vm_cap_type {
423221828Sgrehan	VM_CAP_HALT_EXIT,
424221828Sgrehan	VM_CAP_MTRAP_EXIT,
425221828Sgrehan	VM_CAP_PAUSE_EXIT,
426221828Sgrehan	VM_CAP_UNRESTRICTED_GUEST,
427256645Sneel	VM_CAP_ENABLE_INVPCID,
428221828Sgrehan	VM_CAP_MAX
429221828Sgrehan};
430221828Sgrehan
431266125Sjhbenum vm_intr_trigger {
432266125Sjhb	EDGE_TRIGGER,
433266125Sjhb	LEVEL_TRIGGER
434266125Sjhb};
435266125Sjhb
436221828Sgrehan/*
437221828Sgrehan * The 'access' field has the format specified in Table 21-2 of the Intel
438221828Sgrehan * Architecture Manual vol 3b.
439221828Sgrehan *
440221828Sgrehan * XXX The contents of the 'access' field are architecturally defined except
441221828Sgrehan * bit 16 - Segment Unusable.
442221828Sgrehan */
443221828Sgrehanstruct seg_desc {
444221828Sgrehan	uint64_t	base;
445221828Sgrehan	uint32_t	limit;
446221828Sgrehan	uint32_t	access;
447221828Sgrehan};
448268701Sneel#define	SEG_DESC_TYPE(access)		((access) & 0x001f)
449268777Sneel#define	SEG_DESC_DPL(access)		(((access) >> 5) & 0x3)
450268701Sneel#define	SEG_DESC_PRESENT(access)	(((access) & 0x0080) ? 1 : 0)
451268701Sneel#define	SEG_DESC_DEF32(access)		(((access) & 0x4000) ? 1 : 0)
452268701Sneel#define	SEG_DESC_GRANULARITY(access)	(((access) & 0x8000) ? 1 : 0)
453268701Sneel#define	SEG_DESC_UNUSABLE(access)	(((access) & 0x10000) ? 1 : 0)
454221828Sgrehan
455266627Sneelenum vm_cpu_mode {
456268428Sneel	CPU_MODE_REAL,
457268428Sneel	CPU_MODE_PROTECTED,
458266627Sneel	CPU_MODE_COMPATIBILITY,		/* IA-32E mode (CS.L = 0) */
459266627Sneel	CPU_MODE_64BIT,			/* IA-32E mode (CS.L = 1) */
460266627Sneel};
461266627Sneel
462266627Sneelenum vm_paging_mode {
463266627Sneel	PAGING_MODE_FLAT,
464266627Sneel	PAGING_MODE_32,
465266627Sneel	PAGING_MODE_PAE,
466266627Sneel	PAGING_MODE_64,
467266627Sneel};
468266627Sneel
469266627Sneelstruct vm_guest_paging {
470266627Sneel	uint64_t	cr3;
471266627Sneel	int		cpl;
472266627Sneel	enum vm_cpu_mode cpu_mode;
473266627Sneel	enum vm_paging_mode paging_mode;
474266627Sneel};
475266627Sneel
476266627Sneel/*
477266627Sneel * The data structures 'vie' and 'vie_op' are meant to be opaque to the
478266627Sneel * consumers of instruction decoding. The only reason why their contents
479266627Sneel * need to be exposed is because they are part of the 'vm_exit' structure.
480266627Sneel */
481266627Sneelstruct vie_op {
482266627Sneel	uint8_t		op_byte;	/* actual opcode byte */
483266627Sneel	uint8_t		op_type;	/* type of operation (e.g. MOV) */
484266627Sneel	uint16_t	op_flags;
485266627Sneel};
486266627Sneel
487266627Sneel#define	VIE_INST_SIZE	15
488266627Sneelstruct vie {
489266627Sneel	uint8_t		inst[VIE_INST_SIZE];	/* instruction bytes */
490266627Sneel	uint8_t		num_valid;		/* size of the instruction */
491266627Sneel	uint8_t		num_processed;
492266627Sneel
493268701Sneel	uint8_t		addrsize:4, opsize:4;	/* address and operand sizes */
494266627Sneel	uint8_t		rex_w:1,		/* REX prefix */
495266627Sneel			rex_r:1,
496266627Sneel			rex_x:1,
497266627Sneel			rex_b:1,
498268701Sneel			rex_present:1,
499277360Sneel			repz_present:1,		/* REP/REPE/REPZ prefix */
500277360Sneel			repnz_present:1,	/* REPNE/REPNZ prefix */
501268701Sneel			opsize_override:1,	/* Operand size override */
502277360Sneel			addrsize_override:1,	/* Address size override */
503277360Sneel			segment_override:1;	/* Segment override */
504266627Sneel
505266627Sneel	uint8_t		mod:2,			/* ModRM byte */
506266627Sneel			reg:4,
507266627Sneel			rm:4;
508266627Sneel
509266627Sneel	uint8_t		ss:2,			/* SIB byte */
510266627Sneel			index:4,
511266627Sneel			base:4;
512266627Sneel
513266627Sneel	uint8_t		disp_bytes;
514266627Sneel	uint8_t		imm_bytes;
515266627Sneel
516266627Sneel	uint8_t		scale;
517266627Sneel	int		base_register;		/* VM_REG_GUEST_xyz */
518266627Sneel	int		index_register;		/* VM_REG_GUEST_xyz */
519277360Sneel	int		segment_register;	/* VM_REG_GUEST_xyz */
520266627Sneel
521266627Sneel	int64_t		displacement;		/* optional addr displacement */
522266627Sneel	int64_t		immediate;		/* optional immediate operand */
523266627Sneel
524266627Sneel	uint8_t		decoded;	/* set to 1 if successfully decoded */
525266627Sneel
526266627Sneel	struct vie_op	op;			/* opcode description */
527266627Sneel};
528266627Sneel
529221828Sgrehanenum vm_exitcode {
530221828Sgrehan	VM_EXITCODE_INOUT,
531221828Sgrehan	VM_EXITCODE_VMX,
532221828Sgrehan	VM_EXITCODE_BOGUS,
533221828Sgrehan	VM_EXITCODE_RDMSR,
534221828Sgrehan	VM_EXITCODE_WRMSR,
535221828Sgrehan	VM_EXITCODE_HLT,
536221828Sgrehan	VM_EXITCODE_MTRAP,
537221828Sgrehan	VM_EXITCODE_PAUSE,
538234761Sgrehan	VM_EXITCODE_PAGING,
539256072Sneel	VM_EXITCODE_INST_EMUL,
540240912Sneel	VM_EXITCODE_SPINUP_AP,
541265101Sneel	VM_EXITCODE_DEPRECATED1,	/* used to be SPINDOWN_CPU */
542260619Sneel	VM_EXITCODE_RENDEZVOUS,
543261170Sneel	VM_EXITCODE_IOAPIC_EOI,
544263780Sneel	VM_EXITCODE_SUSPENDED,
545266573Sneel	VM_EXITCODE_INOUT_STR,
546268777Sneel	VM_EXITCODE_TASK_SWITCH,
547272670Sneel	VM_EXITCODE_MONITOR,
548272670Sneel	VM_EXITCODE_MWAIT,
549273375Sneel	VM_EXITCODE_SVM,
550283657Sneel	VM_EXITCODE_REQIDLE,
551347435Sjhb	VM_EXITCODE_VMINSN,
552234761Sgrehan	VM_EXITCODE_MAX
553221828Sgrehan};
554221828Sgrehan
555266573Sneelstruct vm_inout {
556266573Sneel	uint16_t	bytes:3;	/* 1 or 2 or 4 */
557266573Sneel	uint16_t	in:1;
558266573Sneel	uint16_t	string:1;
559266573Sneel	uint16_t	rep:1;
560266573Sneel	uint16_t	port;
561266573Sneel	uint32_t	eax;		/* valid for out */
562266573Sneel};
563266573Sneel
564266573Sneelstruct vm_inout_str {
565266573Sneel	struct vm_inout	inout;		/* must be the first element */
566266627Sneel	struct vm_guest_paging paging;
567266573Sneel	uint64_t	rflags;
568266573Sneel	uint64_t	cr0;
569266573Sneel	uint64_t	index;
570266573Sneel	uint64_t	count;		/* rep=1 (%rcx), rep=0 (1) */
571266573Sneel	int		addrsize;
572266573Sneel	enum vm_reg_name seg_name;
573266573Sneel	struct seg_desc seg_desc;
574266573Sneel};
575266573Sneel
576268777Sneelenum task_switch_reason {
577268777Sneel	TSR_CALL,
578268777Sneel	TSR_IRET,
579268777Sneel	TSR_JMP,
580268777Sneel	TSR_IDT_GATE,	/* task gate in IDT */
581268777Sneel};
582268777Sneel
583268777Sneelstruct vm_task_switch {
584268777Sneel	uint16_t	tsssel;		/* new TSS selector */
585268777Sneel	int		ext;		/* task switch due to external event */
586268777Sneel	uint32_t	errcode;
587268777Sneel	int		errcode_valid;	/* push 'errcode' on the new stack */
588268777Sneel	enum task_switch_reason reason;
589268777Sneel	struct vm_guest_paging paging;
590268777Sneel};
591268777Sneel
592221828Sgrehanstruct vm_exit {
593221828Sgrehan	enum vm_exitcode	exitcode;
594221828Sgrehan	int			inst_length;	/* 0 means unknown */
595221828Sgrehan	uint64_t		rip;
596221828Sgrehan	union {
597266573Sneel		struct vm_inout	inout;
598266573Sneel		struct vm_inout_str inout_str;
599221828Sgrehan		struct {
600241497Sgrehan			uint64_t	gpa;
601256072Sneel			int		fault_type;
602256072Sneel		} paging;
603256072Sneel		struct {
604256072Sneel			uint64_t	gpa;
605256072Sneel			uint64_t	gla;
606280447Stychon			uint64_t	cs_base;
607268701Sneel			int		cs_d;		/* CS.D */
608266627Sneel			struct vm_guest_paging paging;
609243640Sneel			struct vie	vie;
610256072Sneel		} inst_emul;
611221828Sgrehan		/*
612221828Sgrehan		 * VMX specific payload. Used when there is no "better"
613221828Sgrehan		 * exitcode to represent the VM-exit.
614221828Sgrehan		 */
615221828Sgrehan		struct {
616260167Sneel			int		status;		/* vmx inst status */
617260167Sneel			/*
618260167Sneel			 * 'exit_reason' and 'exit_qualification' are valid
619260167Sneel			 * only if 'status' is zero.
620260167Sneel			 */
621221828Sgrehan			uint32_t	exit_reason;
622221828Sgrehan			uint64_t	exit_qualification;
623260167Sneel			/*
624260167Sneel			 * 'inst_error' and 'inst_type' are valid
625260167Sneel			 * only if 'status' is non-zero.
626260167Sneel			 */
627260167Sneel			int		inst_type;
628260167Sneel			int		inst_error;
629221828Sgrehan		} vmx;
630273375Sneel		/*
631273375Sneel		 * SVM specific payload.
632273375Sneel		 */
633221828Sgrehan		struct {
634273375Sneel			uint64_t	exitcode;
635273375Sneel			uint64_t	exitinfo1;
636273375Sneel			uint64_t	exitinfo2;
637273375Sneel		} svm;
638273375Sneel		struct {
639221828Sgrehan			uint32_t	code;		/* ecx value */
640221828Sgrehan			uint64_t	wval;
641221828Sgrehan		} msr;
642240912Sneel		struct {
643240912Sneel			int		vcpu;
644240912Sneel			uint64_t	rip;
645240912Sneel		} spinup_ap;
646259081Sneel		struct {
647259081Sneel			uint64_t	rflags;
648347074Sjhb			uint64_t	intr_status;
649259081Sneel		} hlt;
650261170Sneel		struct {
651261170Sneel			int		vector;
652261170Sneel		} ioapic_eoi;
653265062Sneel		struct {
654265062Sneel			enum vm_suspend_how how;
655265062Sneel		} suspended;
656268777Sneel		struct vm_task_switch task_switch;
657221828Sgrehan	} u;
658221828Sgrehan};
659221828Sgrehan
660269042Sneel/* APIs to inject faults into the guest */
661269042Sneelvoid vm_inject_fault(void *vm, int vcpuid, int vector, int errcode_valid,
662269042Sneel    int errcode);
663269042Sneel
664270438Sgrehanstatic __inline void
665269042Sneelvm_inject_ud(void *vm, int vcpuid)
666269042Sneel{
667269042Sneel	vm_inject_fault(vm, vcpuid, IDT_UD, 0, 0);
668269042Sneel}
669269042Sneel
670270438Sgrehanstatic __inline void
671269042Sneelvm_inject_gp(void *vm, int vcpuid)
672269042Sneel{
673269042Sneel	vm_inject_fault(vm, vcpuid, IDT_GP, 1, 0);
674269042Sneel}
675269042Sneel
676270438Sgrehanstatic __inline void
677269042Sneelvm_inject_ac(void *vm, int vcpuid, int errcode)
678269042Sneel{
679269042Sneel	vm_inject_fault(vm, vcpuid, IDT_AC, 1, errcode);
680269042Sneel}
681269042Sneel
682270438Sgrehanstatic __inline void
683269042Sneelvm_inject_ss(void *vm, int vcpuid, int errcode)
684269042Sneel{
685269042Sneel	vm_inject_fault(vm, vcpuid, IDT_SS, 1, errcode);
686269042Sneel}
687269042Sneel
688269042Sneelvoid vm_inject_pf(void *vm, int vcpuid, int error_code, uint64_t cr2);
689269042Sneel
690277149Sneelint vm_restart_instruction(void *vm, int vcpuid);
691277149Sneel
692221828Sgrehan#endif	/* _VMM_H_ */
693