vmm.h revision 347074
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 347074 2019-05-04 00:59:11Z jhb $
27221828Sgrehan */
28221828Sgrehan
29221828Sgrehan#ifndef _VMM_H_
30221828Sgrehan#define	_VMM_H_
31221828Sgrehan
32269042Sneel#include <x86/segments.h>
33269042Sneel
34265062Sneelenum vm_suspend_how {
35265062Sneel	VM_SUSPEND_NONE,
36265062Sneel	VM_SUSPEND_RESET,
37265062Sneel	VM_SUSPEND_POWEROFF,
38265203Sneel	VM_SUSPEND_HALT,
39268889Sneel	VM_SUSPEND_TRIPLEFAULT,
40265062Sneel	VM_SUSPEND_LAST
41265062Sneel};
42265062Sneel
43267338Stychon/*
44267338Stychon * Identifiers for architecturally defined registers.
45267338Stychon */
46267338Stychonenum vm_reg_name {
47267338Stychon	VM_REG_GUEST_RAX,
48267338Stychon	VM_REG_GUEST_RBX,
49267338Stychon	VM_REG_GUEST_RCX,
50267338Stychon	VM_REG_GUEST_RDX,
51267338Stychon	VM_REG_GUEST_RSI,
52267338Stychon	VM_REG_GUEST_RDI,
53267338Stychon	VM_REG_GUEST_RBP,
54267338Stychon	VM_REG_GUEST_R8,
55267338Stychon	VM_REG_GUEST_R9,
56267338Stychon	VM_REG_GUEST_R10,
57267338Stychon	VM_REG_GUEST_R11,
58267338Stychon	VM_REG_GUEST_R12,
59267338Stychon	VM_REG_GUEST_R13,
60267338Stychon	VM_REG_GUEST_R14,
61267338Stychon	VM_REG_GUEST_R15,
62267338Stychon	VM_REG_GUEST_CR0,
63267338Stychon	VM_REG_GUEST_CR3,
64267338Stychon	VM_REG_GUEST_CR4,
65267338Stychon	VM_REG_GUEST_DR7,
66267338Stychon	VM_REG_GUEST_RSP,
67267338Stychon	VM_REG_GUEST_RIP,
68267338Stychon	VM_REG_GUEST_RFLAGS,
69267338Stychon	VM_REG_GUEST_ES,
70267338Stychon	VM_REG_GUEST_CS,
71267338Stychon	VM_REG_GUEST_SS,
72267338Stychon	VM_REG_GUEST_DS,
73267338Stychon	VM_REG_GUEST_FS,
74267338Stychon	VM_REG_GUEST_GS,
75267338Stychon	VM_REG_GUEST_LDTR,
76267338Stychon	VM_REG_GUEST_TR,
77267338Stychon	VM_REG_GUEST_IDTR,
78267338Stychon	VM_REG_GUEST_GDTR,
79267338Stychon	VM_REG_GUEST_EFER,
80267338Stychon	VM_REG_GUEST_CR2,
81268777Sneel	VM_REG_GUEST_PDPTE0,
82268777Sneel	VM_REG_GUEST_PDPTE1,
83268777Sneel	VM_REG_GUEST_PDPTE2,
84268777Sneel	VM_REG_GUEST_PDPTE3,
85271451Sneel	VM_REG_GUEST_INTR_SHADOW,
86330623Sjhb	VM_REG_GUEST_DR0,
87330623Sjhb	VM_REG_GUEST_DR1,
88330623Sjhb	VM_REG_GUEST_DR2,
89330623Sjhb	VM_REG_GUEST_DR3,
90330623Sjhb	VM_REG_GUEST_DR6,
91267338Stychon	VM_REG_LAST
92267338Stychon};
93267338Stychon
94267338Stychonenum x2apic_state {
95267338Stychon	X2APIC_DISABLED,
96267338Stychon	X2APIC_ENABLED,
97267338Stychon	X2APIC_STATE_LAST
98267338Stychon};
99267338Stychon
100268889Sneel#define	VM_INTINFO_VECTOR(info)	((info) & 0xff)
101268889Sneel#define	VM_INTINFO_DEL_ERRCODE	0x800
102268889Sneel#define	VM_INTINFO_RSVD		0x7ffff000
103268889Sneel#define	VM_INTINFO_VALID	0x80000000
104268889Sneel#define	VM_INTINFO_TYPE		0x700
105268889Sneel#define	VM_INTINFO_HWINTR	(0 << 8)
106268889Sneel#define	VM_INTINFO_NMI		(2 << 8)
107268889Sneel#define	VM_INTINFO_HWEXCEPTION	(3 << 8)
108268889Sneel#define	VM_INTINFO_SWINTR	(4 << 8)
109268889Sneel
110221828Sgrehan#ifdef _KERNEL
111221828Sgrehan
112221828Sgrehan#define	VM_MAX_NAMELEN	32
113221828Sgrehan
114221828Sgrehanstruct vm;
115262506Sneelstruct vm_exception;
116221828Sgrehanstruct seg_desc;
117221828Sgrehanstruct vm_exit;
118221828Sgrehanstruct vm_run;
119258579Sneelstruct vhpet;
120258075Sneelstruct vioapic;
121221828Sgrehanstruct vlapic;
122256072Sneelstruct vmspace;
123256072Sneelstruct vm_object;
124269008Sneelstruct vm_guest_paging;
125256072Sneelstruct pmap;
126221828Sgrehan
127283657Sneelstruct vm_eventinfo {
128283657Sneel	void	*rptr;		/* rendezvous cookie */
129283657Sneel	int	*sptr;		/* suspend cookie */
130283657Sneel	int	*iptr;		/* reqidle cookie */
131283657Sneel};
132283657Sneel
133260466Sneeltypedef int	(*vmm_init_func_t)(int ipinum);
134221828Sgrehantypedef int	(*vmm_cleanup_func_t)(void);
135259782Sjhbtypedef void	(*vmm_resume_func_t)(void);
136256072Sneeltypedef void *	(*vmi_init_func_t)(struct vm *vm, struct pmap *pmap);
137256072Sneeltypedef int	(*vmi_run_func_t)(void *vmi, int vcpu, register_t rip,
138283657Sneel		    struct pmap *pmap, struct vm_eventinfo *info);
139221828Sgrehantypedef void	(*vmi_cleanup_func_t)(void *vmi);
140221828Sgrehantypedef int	(*vmi_get_register_t)(void *vmi, int vcpu, int num,
141221828Sgrehan				      uint64_t *retval);
142221828Sgrehantypedef int	(*vmi_set_register_t)(void *vmi, int vcpu, int num,
143221828Sgrehan				      uint64_t val);
144221828Sgrehantypedef int	(*vmi_get_desc_t)(void *vmi, int vcpu, int num,
145221828Sgrehan				  struct seg_desc *desc);
146221828Sgrehantypedef int	(*vmi_set_desc_t)(void *vmi, int vcpu, int num,
147221828Sgrehan				  struct seg_desc *desc);
148221828Sgrehantypedef int	(*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval);
149221828Sgrehantypedef int	(*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val);
150256072Sneeltypedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max);
151256072Sneeltypedef void	(*vmi_vmspace_free)(struct vmspace *vmspace);
152259863Sneeltypedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu);
153259863Sneeltypedef void	(*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic);
154221828Sgrehan
155221828Sgrehanstruct vmm_ops {
156221828Sgrehan	vmm_init_func_t		init;		/* module wide initialization */
157221828Sgrehan	vmm_cleanup_func_t	cleanup;
158259782Sjhb	vmm_resume_func_t	resume;
159221828Sgrehan
160221828Sgrehan	vmi_init_func_t		vminit;		/* vm-specific initialization */
161221828Sgrehan	vmi_run_func_t		vmrun;
162221828Sgrehan	vmi_cleanup_func_t	vmcleanup;
163221828Sgrehan	vmi_get_register_t	vmgetreg;
164221828Sgrehan	vmi_set_register_t	vmsetreg;
165221828Sgrehan	vmi_get_desc_t		vmgetdesc;
166221828Sgrehan	vmi_set_desc_t		vmsetdesc;
167221828Sgrehan	vmi_get_cap_t		vmgetcap;
168221828Sgrehan	vmi_set_cap_t		vmsetcap;
169256072Sneel	vmi_vmspace_alloc	vmspace_alloc;
170256072Sneel	vmi_vmspace_free	vmspace_free;
171259863Sneel	vmi_vlapic_init		vlapic_init;
172259863Sneel	vmi_vlapic_cleanup	vlapic_cleanup;
173221828Sgrehan};
174221828Sgrehan
175221828Sgrehanextern struct vmm_ops vmm_ops_intel;
176221828Sgrehanextern struct vmm_ops vmm_ops_amd;
177221828Sgrehan
178249396Sneelint vm_create(const char *name, struct vm **retvm);
179221828Sgrehanvoid vm_destroy(struct vm *vm);
180267216Sneelint vm_reinit(struct vm *vm);
181221828Sgrehanconst char *vm_name(struct vm *vm);
182284539Sneel
183284539Sneel/*
184284539Sneel * APIs that modify the guest memory map require all vcpus to be frozen.
185284539Sneel */
186284539Sneelint vm_mmap_memseg(struct vm *vm, vm_paddr_t gpa, int segid, vm_ooffset_t off,
187284539Sneel    size_t len, int prot, int flags);
188284539Sneelint vm_alloc_memseg(struct vm *vm, int ident, size_t len, bool sysmem);
189284539Sneelvoid vm_free_memseg(struct vm *vm, int ident);
190221828Sgrehanint vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
191221828Sgrehanint vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
192284539Sneelint vm_assign_pptdev(struct vm *vm, int bus, int slot, int func);
193284539Sneelint vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func);
194284539Sneel
195284539Sneel/*
196284539Sneel * APIs that inspect the guest memory map require only a *single* vcpu to
197284539Sneel * be frozen. This acts like a read lock on the guest memory map since any
198284539Sneel * modification requires *all* vcpus to be frozen.
199284539Sneel */
200284539Sneelint vm_mmap_getnext(struct vm *vm, vm_paddr_t *gpa, int *segid,
201284539Sneel    vm_ooffset_t *segoff, size_t *len, int *prot, int *flags);
202284539Sneelint vm_get_memseg(struct vm *vm, int ident, size_t *len, bool *sysmem,
203284539Sneel    struct vm_object **objptr);
204284539Sneelvoid *vm_gpa_hold(struct vm *, int vcpuid, vm_paddr_t gpa, size_t len,
205284539Sneel    int prot, void **cookie);
206256072Sneelvoid vm_gpa_release(void *cookie);
207284539Sneelbool vm_mem_allocated(struct vm *vm, int vcpuid, vm_paddr_t gpa);
208284539Sneel
209221828Sgrehanint vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval);
210221828Sgrehanint vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val);
211221828Sgrehanint vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
212221828Sgrehan		    struct seg_desc *ret_desc);
213221828Sgrehanint vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
214221828Sgrehan		    struct seg_desc *desc);
215221828Sgrehanint vm_run(struct vm *vm, struct vm_run *vmrun);
216265062Sneelint vm_suspend(struct vm *vm, enum vm_suspend_how how);
217221828Sgrehanint vm_inject_nmi(struct vm *vm, int vcpu);
218241982Sneelint vm_nmi_pending(struct vm *vm, int vcpuid);
219241982Sneelvoid vm_nmi_clear(struct vm *vm, int vcpuid);
220263211Stychonint vm_inject_extint(struct vm *vm, int vcpu);
221263211Stychonint vm_extint_pending(struct vm *vm, int vcpuid);
222263211Stychonvoid vm_extint_clear(struct vm *vm, int vcpuid);
223221828Sgrehanstruct vlapic *vm_lapic(struct vm *vm, int cpu);
224258075Sneelstruct vioapic *vm_ioapic(struct vm *vm);
225258579Sneelstruct vhpet *vm_hpet(struct vm *vm);
226221828Sgrehanint vm_get_capability(struct vm *vm, int vcpu, int type, int *val);
227221828Sgrehanint vm_set_capability(struct vm *vm, int vcpu, int type, int val);
228240922Sneelint vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state);
229240922Sneelint vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state);
230258075Sneelint vm_apicid2vcpuid(struct vm *vm, int apicid);
231266933Sneelint vm_activate_cpu(struct vm *vm, int vcpu);
232240894Sneelstruct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid);
233265062Sneelvoid vm_exit_suspended(struct vm *vm, int vcpuid, uint64_t rip);
234267330Sneelvoid vm_exit_rendezvous(struct vm *vm, int vcpuid, uint64_t rip);
235267330Sneelvoid vm_exit_astpending(struct vm *vm, int vcpuid, uint64_t rip);
236283657Sneelvoid vm_exit_reqidle(struct vm *vm, int vcpuid, uint64_t rip);
237221828Sgrehan
238282287Sneel#ifdef _SYS__CPUSET_H_
239221828Sgrehan/*
240260619Sneel * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'.
241260619Sneel * The rendezvous 'func(arg)' is not allowed to do anything that will
242260619Sneel * cause the thread to be put to sleep.
243260619Sneel *
244260619Sneel * If the rendezvous is being initiated from a vcpu context then the
245260619Sneel * 'vcpuid' must refer to that vcpu, otherwise it should be set to -1.
246260619Sneel *
247260619Sneel * The caller cannot hold any locks when initiating the rendezvous.
248260619Sneel *
249260619Sneel * The implementation of this API may cause vcpus other than those specified
250260619Sneel * by 'dest' to be stalled. The caller should not rely on any vcpus making
251260619Sneel * forward progress when the rendezvous is in progress.
252260619Sneel */
253260619Sneeltypedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg);
254260619Sneelvoid vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest,
255260619Sneel    vm_rendezvous_func_t func, void *arg);
256282287Sneelcpuset_t vm_active_cpus(struct vm *vm);
257282287Sneelcpuset_t vm_suspended_cpus(struct vm *vm);
258282287Sneel#endif	/* _SYS__CPUSET_H_ */
259260619Sneel
260260619Sneelstatic __inline int
261283657Sneelvcpu_rendezvous_pending(struct vm_eventinfo *info)
262260619Sneel{
263260619Sneel
264283657Sneel	return (*((uintptr_t *)(info->rptr)) != 0);
265260619Sneel}
266260619Sneel
267263780Sneelstatic __inline int
268283657Sneelvcpu_suspended(struct vm_eventinfo *info)
269263780Sneel{
270263780Sneel
271283657Sneel	return (*info->sptr);
272263780Sneel}
273263780Sneel
274283657Sneelstatic __inline int
275283657Sneelvcpu_reqidle(struct vm_eventinfo *info)
276283657Sneel{
277283657Sneel
278283657Sneel	return (*info->iptr);
279283657Sneel}
280283657Sneel
281260619Sneel/*
282221828Sgrehan * Return 1 if device indicated by bus/slot/func is supposed to be a
283221828Sgrehan * pci passthrough device.
284221828Sgrehan *
285221828Sgrehan * Return 0 otherwise.
286221828Sgrehan */
287221828Sgrehanint vmm_is_pptdev(int bus, int slot, int func);
288221828Sgrehan
289221828Sgrehanvoid *vm_iommu_domain(struct vm *vm);
290221828Sgrehan
291241489Sneelenum vcpu_state {
292241489Sneel	VCPU_IDLE,
293256072Sneel	VCPU_FROZEN,
294241489Sneel	VCPU_RUNNING,
295256072Sneel	VCPU_SLEEPING,
296241489Sneel};
297221828Sgrehan
298259737Sneelint vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state,
299259737Sneel    bool from_idle);
300249879Sgrehanenum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu);
301221828Sgrehan
302221828Sgrehanstatic int __inline
303249879Sgrehanvcpu_is_running(struct vm *vm, int vcpu, int *hostcpu)
304221828Sgrehan{
305249879Sgrehan	return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING);
306221828Sgrehan}
307221828Sgrehan
308269109Sneel#ifdef _SYS_PROC_H_
309269109Sneelstatic int __inline
310269109Sneelvcpu_should_yield(struct vm *vm, int vcpu)
311269109Sneel{
312282571Sneel
313282571Sneel	if (curthread->td_flags & (TDF_ASTPENDING | TDF_NEEDRESCHED))
314282571Sneel		return (1);
315282571Sneel	else if (curthread->td_owepreempt)
316282571Sneel		return (1);
317282571Sneel	else
318282571Sneel		return (0);
319269109Sneel}
320269109Sneel#endif
321269109Sneel
322241489Sneelvoid *vcpu_stats(struct vm *vm, int vcpu);
323259863Sneelvoid vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr);
324256072Sneelstruct vmspace *vm_get_vmspace(struct vm *vm);
325263035Stychonstruct vatpic *vm_atpic(struct vm *vm);
326263744Stychonstruct vatpit *vm_atpit(struct vm *vm);
327273683Sneelstruct vpmtmr *vm_pmtmr(struct vm *vm);
328276428Sneelstruct vrtc *vm_rtc(struct vm *vm);
329262506Sneel
330262506Sneel/*
331277149Sneel * Inject exception 'vector' into the guest vcpu. This function returns 0 on
332262506Sneel * success and non-zero on failure.
333262506Sneel *
334262506Sneel * Wrapper functions like 'vm_inject_gp()' should be preferred to calling
335262506Sneel * this function directly because they enforce the trap-like or fault-like
336262506Sneel * behavior of an exception.
337262506Sneel *
338262506Sneel * This function should only be called in the context of the thread that is
339262506Sneel * executing this vcpu.
340262506Sneel */
341277149Sneelint vm_inject_exception(struct vm *vm, int vcpuid, int vector, int err_valid,
342277149Sneel    uint32_t errcode, int restart_instruction);
343262506Sneel
344262506Sneel/*
345268889Sneel * This function is called after a VM-exit that occurred during exception or
346268889Sneel * interrupt delivery through the IDT. The format of 'intinfo' is described
347268889Sneel * in Figure 15-1, "EXITINTINFO for All Intercepts", APM, Vol 2.
348262506Sneel *
349268889Sneel * If a VM-exit handler completes the event delivery successfully then it
350268889Sneel * should call vm_exit_intinfo() to extinguish the pending event. For e.g.,
351268889Sneel * if the task switch emulation is triggered via a task gate then it should
352268889Sneel * call this function with 'intinfo=0' to indicate that the external event
353268889Sneel * is not pending anymore.
354268889Sneel *
355268889Sneel * Return value is 0 on success and non-zero on failure.
356262506Sneel */
357268889Sneelint vm_exit_intinfo(struct vm *vm, int vcpuid, uint64_t intinfo);
358262506Sneel
359268889Sneel/*
360268889Sneel * This function is called before every VM-entry to retrieve a pending
361268889Sneel * event that should be injected into the guest. This function combines
362268889Sneel * nested events into a double or triple fault.
363268889Sneel *
364268889Sneel * Returns 0 if there are no events that need to be injected into the guest
365268889Sneel * and non-zero otherwise.
366268889Sneel */
367268889Sneelint vm_entry_intinfo(struct vm *vm, int vcpuid, uint64_t *info);
368268889Sneel
369268889Sneelint vm_get_intinfo(struct vm *vm, int vcpuid, uint64_t *info1, uint64_t *info2);
370268889Sneel
371266573Sneelenum vm_reg_name vm_segment_name(int seg_encoding);
372266573Sneel
373269008Sneelstruct vm_copyinfo {
374269008Sneel	uint64_t	gpa;
375269008Sneel	size_t		len;
376269008Sneel	void		*hva;
377269008Sneel	void		*cookie;
378269008Sneel};
379269008Sneel
380269008Sneel/*
381269008Sneel * Set up 'copyinfo[]' to copy to/from guest linear address space starting
382269008Sneel * at 'gla' and 'len' bytes long. The 'prot' should be set to PROT_READ for
383269008Sneel * a copyin or PROT_WRITE for a copyout.
384269008Sneel *
385299010Spfg * retval	is_fault	Interpretation
386282558Sneel *   0		   0		Success
387282558Sneel *   0		   1		An exception was injected into the guest
388282558Sneel * EFAULT	  N/A		Unrecoverable error
389269008Sneel *
390269008Sneel * The 'copyinfo[]' can be passed to 'vm_copyin()' or 'vm_copyout()' only if
391269008Sneel * the return value is 0. The 'copyinfo[]' resources should be freed by calling
392269008Sneel * 'vm_copy_teardown()' after the copy is done.
393269008Sneel */
394269008Sneelint vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging,
395269008Sneel    uint64_t gla, size_t len, int prot, struct vm_copyinfo *copyinfo,
396282558Sneel    int num_copyinfo, int *is_fault);
397269008Sneelvoid vm_copy_teardown(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo,
398269008Sneel    int num_copyinfo);
399269008Sneelvoid vm_copyin(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo,
400269008Sneel    void *kaddr, size_t len);
401269008Sneelvoid vm_copyout(struct vm *vm, int vcpuid, const void *kaddr,
402269008Sneel    struct vm_copyinfo *copyinfo, size_t len);
403276098Sneel
404276098Sneelint vcpu_trace_exceptions(struct vm *vm, int vcpuid);
405221828Sgrehan#endif	/* KERNEL */
406221828Sgrehan
407255438Sgrehan#define	VM_MAXCPU	16			/* maximum virtual cpus */
408221828Sgrehan
409221828Sgrehan/*
410221828Sgrehan * Identifiers for optional vmm capabilities
411221828Sgrehan */
412221828Sgrehanenum vm_cap_type {
413221828Sgrehan	VM_CAP_HALT_EXIT,
414221828Sgrehan	VM_CAP_MTRAP_EXIT,
415221828Sgrehan	VM_CAP_PAUSE_EXIT,
416221828Sgrehan	VM_CAP_UNRESTRICTED_GUEST,
417256645Sneel	VM_CAP_ENABLE_INVPCID,
418221828Sgrehan	VM_CAP_MAX
419221828Sgrehan};
420221828Sgrehan
421266125Sjhbenum vm_intr_trigger {
422266125Sjhb	EDGE_TRIGGER,
423266125Sjhb	LEVEL_TRIGGER
424266125Sjhb};
425266125Sjhb
426221828Sgrehan/*
427221828Sgrehan * The 'access' field has the format specified in Table 21-2 of the Intel
428221828Sgrehan * Architecture Manual vol 3b.
429221828Sgrehan *
430221828Sgrehan * XXX The contents of the 'access' field are architecturally defined except
431221828Sgrehan * bit 16 - Segment Unusable.
432221828Sgrehan */
433221828Sgrehanstruct seg_desc {
434221828Sgrehan	uint64_t	base;
435221828Sgrehan	uint32_t	limit;
436221828Sgrehan	uint32_t	access;
437221828Sgrehan};
438268701Sneel#define	SEG_DESC_TYPE(access)		((access) & 0x001f)
439268777Sneel#define	SEG_DESC_DPL(access)		(((access) >> 5) & 0x3)
440268701Sneel#define	SEG_DESC_PRESENT(access)	(((access) & 0x0080) ? 1 : 0)
441268701Sneel#define	SEG_DESC_DEF32(access)		(((access) & 0x4000) ? 1 : 0)
442268701Sneel#define	SEG_DESC_GRANULARITY(access)	(((access) & 0x8000) ? 1 : 0)
443268701Sneel#define	SEG_DESC_UNUSABLE(access)	(((access) & 0x10000) ? 1 : 0)
444221828Sgrehan
445266627Sneelenum vm_cpu_mode {
446268428Sneel	CPU_MODE_REAL,
447268428Sneel	CPU_MODE_PROTECTED,
448266627Sneel	CPU_MODE_COMPATIBILITY,		/* IA-32E mode (CS.L = 0) */
449266627Sneel	CPU_MODE_64BIT,			/* IA-32E mode (CS.L = 1) */
450266627Sneel};
451266627Sneel
452266627Sneelenum vm_paging_mode {
453266627Sneel	PAGING_MODE_FLAT,
454266627Sneel	PAGING_MODE_32,
455266627Sneel	PAGING_MODE_PAE,
456266627Sneel	PAGING_MODE_64,
457266627Sneel};
458266627Sneel
459266627Sneelstruct vm_guest_paging {
460266627Sneel	uint64_t	cr3;
461266627Sneel	int		cpl;
462266627Sneel	enum vm_cpu_mode cpu_mode;
463266627Sneel	enum vm_paging_mode paging_mode;
464266627Sneel};
465266627Sneel
466266627Sneel/*
467266627Sneel * The data structures 'vie' and 'vie_op' are meant to be opaque to the
468266627Sneel * consumers of instruction decoding. The only reason why their contents
469266627Sneel * need to be exposed is because they are part of the 'vm_exit' structure.
470266627Sneel */
471266627Sneelstruct vie_op {
472266627Sneel	uint8_t		op_byte;	/* actual opcode byte */
473266627Sneel	uint8_t		op_type;	/* type of operation (e.g. MOV) */
474266627Sneel	uint16_t	op_flags;
475266627Sneel};
476266627Sneel
477266627Sneel#define	VIE_INST_SIZE	15
478266627Sneelstruct vie {
479266627Sneel	uint8_t		inst[VIE_INST_SIZE];	/* instruction bytes */
480266627Sneel	uint8_t		num_valid;		/* size of the instruction */
481266627Sneel	uint8_t		num_processed;
482266627Sneel
483268701Sneel	uint8_t		addrsize:4, opsize:4;	/* address and operand sizes */
484266627Sneel	uint8_t		rex_w:1,		/* REX prefix */
485266627Sneel			rex_r:1,
486266627Sneel			rex_x:1,
487266627Sneel			rex_b:1,
488268701Sneel			rex_present:1,
489277360Sneel			repz_present:1,		/* REP/REPE/REPZ prefix */
490277360Sneel			repnz_present:1,	/* REPNE/REPNZ prefix */
491268701Sneel			opsize_override:1,	/* Operand size override */
492277360Sneel			addrsize_override:1,	/* Address size override */
493277360Sneel			segment_override:1;	/* Segment override */
494266627Sneel
495266627Sneel	uint8_t		mod:2,			/* ModRM byte */
496266627Sneel			reg:4,
497266627Sneel			rm:4;
498266627Sneel
499266627Sneel	uint8_t		ss:2,			/* SIB byte */
500266627Sneel			index:4,
501266627Sneel			base:4;
502266627Sneel
503266627Sneel	uint8_t		disp_bytes;
504266627Sneel	uint8_t		imm_bytes;
505266627Sneel
506266627Sneel	uint8_t		scale;
507266627Sneel	int		base_register;		/* VM_REG_GUEST_xyz */
508266627Sneel	int		index_register;		/* VM_REG_GUEST_xyz */
509277360Sneel	int		segment_register;	/* VM_REG_GUEST_xyz */
510266627Sneel
511266627Sneel	int64_t		displacement;		/* optional addr displacement */
512266627Sneel	int64_t		immediate;		/* optional immediate operand */
513266627Sneel
514266627Sneel	uint8_t		decoded;	/* set to 1 if successfully decoded */
515266627Sneel
516266627Sneel	struct vie_op	op;			/* opcode description */
517266627Sneel};
518266627Sneel
519221828Sgrehanenum vm_exitcode {
520221828Sgrehan	VM_EXITCODE_INOUT,
521221828Sgrehan	VM_EXITCODE_VMX,
522221828Sgrehan	VM_EXITCODE_BOGUS,
523221828Sgrehan	VM_EXITCODE_RDMSR,
524221828Sgrehan	VM_EXITCODE_WRMSR,
525221828Sgrehan	VM_EXITCODE_HLT,
526221828Sgrehan	VM_EXITCODE_MTRAP,
527221828Sgrehan	VM_EXITCODE_PAUSE,
528234761Sgrehan	VM_EXITCODE_PAGING,
529256072Sneel	VM_EXITCODE_INST_EMUL,
530240912Sneel	VM_EXITCODE_SPINUP_AP,
531265101Sneel	VM_EXITCODE_DEPRECATED1,	/* used to be SPINDOWN_CPU */
532260619Sneel	VM_EXITCODE_RENDEZVOUS,
533261170Sneel	VM_EXITCODE_IOAPIC_EOI,
534263780Sneel	VM_EXITCODE_SUSPENDED,
535266573Sneel	VM_EXITCODE_INOUT_STR,
536268777Sneel	VM_EXITCODE_TASK_SWITCH,
537272670Sneel	VM_EXITCODE_MONITOR,
538272670Sneel	VM_EXITCODE_MWAIT,
539273375Sneel	VM_EXITCODE_SVM,
540283657Sneel	VM_EXITCODE_REQIDLE,
541234761Sgrehan	VM_EXITCODE_MAX
542221828Sgrehan};
543221828Sgrehan
544266573Sneelstruct vm_inout {
545266573Sneel	uint16_t	bytes:3;	/* 1 or 2 or 4 */
546266573Sneel	uint16_t	in:1;
547266573Sneel	uint16_t	string:1;
548266573Sneel	uint16_t	rep:1;
549266573Sneel	uint16_t	port;
550266573Sneel	uint32_t	eax;		/* valid for out */
551266573Sneel};
552266573Sneel
553266573Sneelstruct vm_inout_str {
554266573Sneel	struct vm_inout	inout;		/* must be the first element */
555266627Sneel	struct vm_guest_paging paging;
556266573Sneel	uint64_t	rflags;
557266573Sneel	uint64_t	cr0;
558266573Sneel	uint64_t	index;
559266573Sneel	uint64_t	count;		/* rep=1 (%rcx), rep=0 (1) */
560266573Sneel	int		addrsize;
561266573Sneel	enum vm_reg_name seg_name;
562266573Sneel	struct seg_desc seg_desc;
563266573Sneel};
564266573Sneel
565268777Sneelenum task_switch_reason {
566268777Sneel	TSR_CALL,
567268777Sneel	TSR_IRET,
568268777Sneel	TSR_JMP,
569268777Sneel	TSR_IDT_GATE,	/* task gate in IDT */
570268777Sneel};
571268777Sneel
572268777Sneelstruct vm_task_switch {
573268777Sneel	uint16_t	tsssel;		/* new TSS selector */
574268777Sneel	int		ext;		/* task switch due to external event */
575268777Sneel	uint32_t	errcode;
576268777Sneel	int		errcode_valid;	/* push 'errcode' on the new stack */
577268777Sneel	enum task_switch_reason reason;
578268777Sneel	struct vm_guest_paging paging;
579268777Sneel};
580268777Sneel
581221828Sgrehanstruct vm_exit {
582221828Sgrehan	enum vm_exitcode	exitcode;
583221828Sgrehan	int			inst_length;	/* 0 means unknown */
584221828Sgrehan	uint64_t		rip;
585221828Sgrehan	union {
586266573Sneel		struct vm_inout	inout;
587266573Sneel		struct vm_inout_str inout_str;
588221828Sgrehan		struct {
589241497Sgrehan			uint64_t	gpa;
590256072Sneel			int		fault_type;
591256072Sneel		} paging;
592256072Sneel		struct {
593256072Sneel			uint64_t	gpa;
594256072Sneel			uint64_t	gla;
595280447Stychon			uint64_t	cs_base;
596268701Sneel			int		cs_d;		/* CS.D */
597266627Sneel			struct vm_guest_paging paging;
598243640Sneel			struct vie	vie;
599256072Sneel		} inst_emul;
600221828Sgrehan		/*
601221828Sgrehan		 * VMX specific payload. Used when there is no "better"
602221828Sgrehan		 * exitcode to represent the VM-exit.
603221828Sgrehan		 */
604221828Sgrehan		struct {
605260167Sneel			int		status;		/* vmx inst status */
606260167Sneel			/*
607260167Sneel			 * 'exit_reason' and 'exit_qualification' are valid
608260167Sneel			 * only if 'status' is zero.
609260167Sneel			 */
610221828Sgrehan			uint32_t	exit_reason;
611221828Sgrehan			uint64_t	exit_qualification;
612260167Sneel			/*
613260167Sneel			 * 'inst_error' and 'inst_type' are valid
614260167Sneel			 * only if 'status' is non-zero.
615260167Sneel			 */
616260167Sneel			int		inst_type;
617260167Sneel			int		inst_error;
618221828Sgrehan		} vmx;
619273375Sneel		/*
620273375Sneel		 * SVM specific payload.
621273375Sneel		 */
622221828Sgrehan		struct {
623273375Sneel			uint64_t	exitcode;
624273375Sneel			uint64_t	exitinfo1;
625273375Sneel			uint64_t	exitinfo2;
626273375Sneel		} svm;
627273375Sneel		struct {
628221828Sgrehan			uint32_t	code;		/* ecx value */
629221828Sgrehan			uint64_t	wval;
630221828Sgrehan		} msr;
631240912Sneel		struct {
632240912Sneel			int		vcpu;
633240912Sneel			uint64_t	rip;
634240912Sneel		} spinup_ap;
635259081Sneel		struct {
636259081Sneel			uint64_t	rflags;
637347074Sjhb			uint64_t	intr_status;
638259081Sneel		} hlt;
639261170Sneel		struct {
640261170Sneel			int		vector;
641261170Sneel		} ioapic_eoi;
642265062Sneel		struct {
643265062Sneel			enum vm_suspend_how how;
644265062Sneel		} suspended;
645268777Sneel		struct vm_task_switch task_switch;
646221828Sgrehan	} u;
647221828Sgrehan};
648221828Sgrehan
649269042Sneel/* APIs to inject faults into the guest */
650269042Sneelvoid vm_inject_fault(void *vm, int vcpuid, int vector, int errcode_valid,
651269042Sneel    int errcode);
652269042Sneel
653270438Sgrehanstatic __inline void
654269042Sneelvm_inject_ud(void *vm, int vcpuid)
655269042Sneel{
656269042Sneel	vm_inject_fault(vm, vcpuid, IDT_UD, 0, 0);
657269042Sneel}
658269042Sneel
659270438Sgrehanstatic __inline void
660269042Sneelvm_inject_gp(void *vm, int vcpuid)
661269042Sneel{
662269042Sneel	vm_inject_fault(vm, vcpuid, IDT_GP, 1, 0);
663269042Sneel}
664269042Sneel
665270438Sgrehanstatic __inline void
666269042Sneelvm_inject_ac(void *vm, int vcpuid, int errcode)
667269042Sneel{
668269042Sneel	vm_inject_fault(vm, vcpuid, IDT_AC, 1, errcode);
669269042Sneel}
670269042Sneel
671270438Sgrehanstatic __inline void
672269042Sneelvm_inject_ss(void *vm, int vcpuid, int errcode)
673269042Sneel{
674269042Sneel	vm_inject_fault(vm, vcpuid, IDT_SS, 1, errcode);
675269042Sneel}
676269042Sneel
677269042Sneelvoid vm_inject_pf(void *vm, int vcpuid, int error_code, uint64_t cr2);
678269042Sneel
679277149Sneelint vm_restart_instruction(void *vm, int vcpuid);
680277149Sneel
681221828Sgrehan#endif	/* _VMM_H_ */
682