vmm.h revision 263211
1/*-
2 * Copyright (c) 2011 NetApp, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/amd64/include/vmm.h 263211 2014-03-15 23:09:34Z tychon $
27 */
28
29#ifndef _VMM_H_
30#define	_VMM_H_
31
32#ifdef _KERNEL
33
34#define	VM_MAX_NAMELEN	32
35
36struct vm;
37struct vm_exception;
38struct vm_memory_segment;
39struct seg_desc;
40struct vm_exit;
41struct vm_run;
42struct vhpet;
43struct vioapic;
44struct vlapic;
45struct vmspace;
46struct vm_object;
47struct pmap;
48
49enum x2apic_state;
50
51typedef int	(*vmm_init_func_t)(int ipinum);
52typedef int	(*vmm_cleanup_func_t)(void);
53typedef void	(*vmm_resume_func_t)(void);
54typedef void *	(*vmi_init_func_t)(struct vm *vm, struct pmap *pmap);
55typedef int	(*vmi_run_func_t)(void *vmi, int vcpu, register_t rip,
56				  struct pmap *pmap, void *rendezvous_cookie);
57typedef void	(*vmi_cleanup_func_t)(void *vmi);
58typedef int	(*vmi_get_register_t)(void *vmi, int vcpu, int num,
59				      uint64_t *retval);
60typedef int	(*vmi_set_register_t)(void *vmi, int vcpu, int num,
61				      uint64_t val);
62typedef int	(*vmi_get_desc_t)(void *vmi, int vcpu, int num,
63				  struct seg_desc *desc);
64typedef int	(*vmi_set_desc_t)(void *vmi, int vcpu, int num,
65				  struct seg_desc *desc);
66typedef int	(*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval);
67typedef int	(*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val);
68typedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max);
69typedef void	(*vmi_vmspace_free)(struct vmspace *vmspace);
70typedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu);
71typedef void	(*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic);
72
73struct vmm_ops {
74	vmm_init_func_t		init;		/* module wide initialization */
75	vmm_cleanup_func_t	cleanup;
76	vmm_resume_func_t	resume;
77
78	vmi_init_func_t		vminit;		/* vm-specific initialization */
79	vmi_run_func_t		vmrun;
80	vmi_cleanup_func_t	vmcleanup;
81	vmi_get_register_t	vmgetreg;
82	vmi_set_register_t	vmsetreg;
83	vmi_get_desc_t		vmgetdesc;
84	vmi_set_desc_t		vmsetdesc;
85	vmi_get_cap_t		vmgetcap;
86	vmi_set_cap_t		vmsetcap;
87	vmi_vmspace_alloc	vmspace_alloc;
88	vmi_vmspace_free	vmspace_free;
89	vmi_vlapic_init		vlapic_init;
90	vmi_vlapic_cleanup	vlapic_cleanup;
91};
92
93extern struct vmm_ops vmm_ops_intel;
94extern struct vmm_ops vmm_ops_amd;
95
96int vm_create(const char *name, struct vm **retvm);
97void vm_destroy(struct vm *vm);
98const char *vm_name(struct vm *vm);
99int vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len);
100int vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
101int vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
102void *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot,
103		  void **cookie);
104void vm_gpa_release(void *cookie);
105int vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase,
106	      struct vm_memory_segment *seg);
107int vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len,
108		  vm_offset_t *offset, struct vm_object **object);
109boolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa);
110int vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval);
111int vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val);
112int vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
113		    struct seg_desc *ret_desc);
114int vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
115		    struct seg_desc *desc);
116int vm_run(struct vm *vm, struct vm_run *vmrun);
117int vm_inject_nmi(struct vm *vm, int vcpu);
118int vm_nmi_pending(struct vm *vm, int vcpuid);
119void vm_nmi_clear(struct vm *vm, int vcpuid);
120int vm_inject_extint(struct vm *vm, int vcpu);
121int vm_extint_pending(struct vm *vm, int vcpuid);
122void vm_extint_clear(struct vm *vm, int vcpuid);
123uint64_t *vm_guest_msrs(struct vm *vm, int cpu);
124struct vlapic *vm_lapic(struct vm *vm, int cpu);
125struct vioapic *vm_ioapic(struct vm *vm);
126struct vhpet *vm_hpet(struct vm *vm);
127int vm_get_capability(struct vm *vm, int vcpu, int type, int *val);
128int vm_set_capability(struct vm *vm, int vcpu, int type, int val);
129int vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state);
130int vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state);
131int vm_apicid2vcpuid(struct vm *vm, int apicid);
132void vm_activate_cpu(struct vm *vm, int vcpu);
133cpuset_t vm_active_cpus(struct vm *vm);
134struct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid);
135
136/*
137 * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'.
138 * The rendezvous 'func(arg)' is not allowed to do anything that will
139 * cause the thread to be put to sleep.
140 *
141 * If the rendezvous is being initiated from a vcpu context then the
142 * 'vcpuid' must refer to that vcpu, otherwise it should be set to -1.
143 *
144 * The caller cannot hold any locks when initiating the rendezvous.
145 *
146 * The implementation of this API may cause vcpus other than those specified
147 * by 'dest' to be stalled. The caller should not rely on any vcpus making
148 * forward progress when the rendezvous is in progress.
149 */
150typedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg);
151void vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest,
152    vm_rendezvous_func_t func, void *arg);
153
154static __inline int
155vcpu_rendezvous_pending(void *rendezvous_cookie)
156{
157
158	return (*(uintptr_t *)rendezvous_cookie != 0);
159}
160
161/*
162 * Return 1 if device indicated by bus/slot/func is supposed to be a
163 * pci passthrough device.
164 *
165 * Return 0 otherwise.
166 */
167int vmm_is_pptdev(int bus, int slot, int func);
168
169void *vm_iommu_domain(struct vm *vm);
170
171enum vcpu_state {
172	VCPU_IDLE,
173	VCPU_FROZEN,
174	VCPU_RUNNING,
175	VCPU_SLEEPING,
176};
177
178int vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state,
179    bool from_idle);
180enum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu);
181
182static int __inline
183vcpu_is_running(struct vm *vm, int vcpu, int *hostcpu)
184{
185	return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING);
186}
187
188void *vcpu_stats(struct vm *vm, int vcpu);
189void vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr);
190struct vmspace *vm_get_vmspace(struct vm *vm);
191int vm_assign_pptdev(struct vm *vm, int bus, int slot, int func);
192int vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func);
193struct vatpic *vm_atpic(struct vm *vm);
194
195/*
196 * Inject exception 'vme' into the guest vcpu. This function returns 0 on
197 * success and non-zero on failure.
198 *
199 * Wrapper functions like 'vm_inject_gp()' should be preferred to calling
200 * this function directly because they enforce the trap-like or fault-like
201 * behavior of an exception.
202 *
203 * This function should only be called in the context of the thread that is
204 * executing this vcpu.
205 */
206int vm_inject_exception(struct vm *vm, int vcpuid, struct vm_exception *vme);
207
208/*
209 * Returns 0 if there is no exception pending for this vcpu. Returns 1 if an
210 * exception is pending and also updates 'vme'. The pending exception is
211 * cleared when this function returns.
212 *
213 * This function should only be called in the context of the thread that is
214 * executing this vcpu.
215 */
216int vm_exception_pending(struct vm *vm, int vcpuid, struct vm_exception *vme);
217
218void vm_inject_gp(struct vm *vm, int vcpuid); /* general protection fault */
219void vm_inject_ud(struct vm *vm, int vcpuid); /* undefined instruction fault */
220
221#endif	/* KERNEL */
222
223#include <machine/vmm_instruction_emul.h>
224
225#define	VM_MAXCPU	16			/* maximum virtual cpus */
226
227/*
228 * Identifiers for architecturally defined registers.
229 */
230enum vm_reg_name {
231	VM_REG_GUEST_RAX,
232	VM_REG_GUEST_RBX,
233	VM_REG_GUEST_RCX,
234	VM_REG_GUEST_RDX,
235	VM_REG_GUEST_RSI,
236	VM_REG_GUEST_RDI,
237	VM_REG_GUEST_RBP,
238	VM_REG_GUEST_R8,
239	VM_REG_GUEST_R9,
240	VM_REG_GUEST_R10,
241	VM_REG_GUEST_R11,
242	VM_REG_GUEST_R12,
243	VM_REG_GUEST_R13,
244	VM_REG_GUEST_R14,
245	VM_REG_GUEST_R15,
246	VM_REG_GUEST_CR0,
247	VM_REG_GUEST_CR3,
248	VM_REG_GUEST_CR4,
249	VM_REG_GUEST_DR7,
250	VM_REG_GUEST_RSP,
251	VM_REG_GUEST_RIP,
252	VM_REG_GUEST_RFLAGS,
253	VM_REG_GUEST_ES,
254	VM_REG_GUEST_CS,
255	VM_REG_GUEST_SS,
256	VM_REG_GUEST_DS,
257	VM_REG_GUEST_FS,
258	VM_REG_GUEST_GS,
259	VM_REG_GUEST_LDTR,
260	VM_REG_GUEST_TR,
261	VM_REG_GUEST_IDTR,
262	VM_REG_GUEST_GDTR,
263	VM_REG_GUEST_EFER,
264	VM_REG_LAST
265};
266
267/*
268 * Identifiers for optional vmm capabilities
269 */
270enum vm_cap_type {
271	VM_CAP_HALT_EXIT,
272	VM_CAP_MTRAP_EXIT,
273	VM_CAP_PAUSE_EXIT,
274	VM_CAP_UNRESTRICTED_GUEST,
275	VM_CAP_ENABLE_INVPCID,
276	VM_CAP_MAX
277};
278
279enum x2apic_state {
280	X2APIC_DISABLED,
281	X2APIC_ENABLED,
282	X2APIC_STATE_LAST
283};
284
285/*
286 * The 'access' field has the format specified in Table 21-2 of the Intel
287 * Architecture Manual vol 3b.
288 *
289 * XXX The contents of the 'access' field are architecturally defined except
290 * bit 16 - Segment Unusable.
291 */
292struct seg_desc {
293	uint64_t	base;
294	uint32_t	limit;
295	uint32_t	access;
296};
297
298enum vm_exitcode {
299	VM_EXITCODE_INOUT,
300	VM_EXITCODE_VMX,
301	VM_EXITCODE_BOGUS,
302	VM_EXITCODE_RDMSR,
303	VM_EXITCODE_WRMSR,
304	VM_EXITCODE_HLT,
305	VM_EXITCODE_MTRAP,
306	VM_EXITCODE_PAUSE,
307	VM_EXITCODE_PAGING,
308	VM_EXITCODE_INST_EMUL,
309	VM_EXITCODE_SPINUP_AP,
310	VM_EXITCODE_SPINDOWN_CPU,
311	VM_EXITCODE_RENDEZVOUS,
312	VM_EXITCODE_IOAPIC_EOI,
313	VM_EXITCODE_MAX
314};
315
316struct vm_exit {
317	enum vm_exitcode	exitcode;
318	int			inst_length;	/* 0 means unknown */
319	uint64_t		rip;
320	union {
321		struct {
322			uint16_t	bytes:3;	/* 1 or 2 or 4 */
323			uint16_t	in:1;		/* out is 0, in is 1 */
324			uint16_t	string:1;
325			uint16_t	rep:1;
326			uint16_t	port;
327			uint32_t	eax;		/* valid for out */
328		} inout;
329		struct {
330			uint64_t	gpa;
331			int		fault_type;
332		} paging;
333		struct {
334			uint64_t	gpa;
335			uint64_t	gla;
336			uint64_t	cr3;
337			enum vie_cpu_mode cpu_mode;
338			enum vie_paging_mode paging_mode;
339			struct vie	vie;
340		} inst_emul;
341		/*
342		 * VMX specific payload. Used when there is no "better"
343		 * exitcode to represent the VM-exit.
344		 */
345		struct {
346			int		status;		/* vmx inst status */
347			/*
348			 * 'exit_reason' and 'exit_qualification' are valid
349			 * only if 'status' is zero.
350			 */
351			uint32_t	exit_reason;
352			uint64_t	exit_qualification;
353			/*
354			 * 'inst_error' and 'inst_type' are valid
355			 * only if 'status' is non-zero.
356			 */
357			int		inst_type;
358			int		inst_error;
359		} vmx;
360		struct {
361			uint32_t	code;		/* ecx value */
362			uint64_t	wval;
363		} msr;
364		struct {
365			int		vcpu;
366			uint64_t	rip;
367		} spinup_ap;
368		struct {
369			uint64_t	rflags;
370		} hlt;
371		struct {
372			int		vector;
373		} ioapic_eoi;
374	} u;
375};
376
377#endif	/* _VMM_H_ */
378