vmm.h revision 262350
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: stable/10/sys/amd64/include/vmm.h 262350 2014-02-23 00:46:05Z jhb $
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_memory_segment;
38struct seg_desc;
39struct vm_exit;
40struct vm_run;
41struct vhpet;
42struct vioapic;
43struct vlapic;
44struct vmspace;
45struct vm_object;
46struct pmap;
47
48enum x2apic_state;
49
50typedef int	(*vmm_init_func_t)(void);
51typedef int	(*vmm_cleanup_func_t)(void);
52typedef void	(*vmm_resume_func_t)(void);
53typedef void *	(*vmi_init_func_t)(struct vm *vm, struct pmap *pmap);
54typedef int	(*vmi_run_func_t)(void *vmi, int vcpu, register_t rip,
55				  struct pmap *pmap);
56typedef void	(*vmi_cleanup_func_t)(void *vmi);
57typedef int	(*vmi_get_register_t)(void *vmi, int vcpu, int num,
58				      uint64_t *retval);
59typedef int	(*vmi_set_register_t)(void *vmi, int vcpu, int num,
60				      uint64_t val);
61typedef int	(*vmi_get_desc_t)(void *vmi, int vcpu, int num,
62				  struct seg_desc *desc);
63typedef int	(*vmi_set_desc_t)(void *vmi, int vcpu, int num,
64				  struct seg_desc *desc);
65typedef int	(*vmi_inject_event_t)(void *vmi, int vcpu,
66				      int type, int vector,
67				      uint32_t code, int code_valid);
68typedef int	(*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval);
69typedef int	(*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val);
70typedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max);
71typedef void	(*vmi_vmspace_free)(struct vmspace *vmspace);
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_inject_event_t	vminject;
86	vmi_get_cap_t		vmgetcap;
87	vmi_set_cap_t		vmsetcap;
88	vmi_vmspace_alloc	vmspace_alloc;
89	vmi_vmspace_free	vmspace_free;
90};
91
92extern struct vmm_ops vmm_ops_intel;
93extern struct vmm_ops vmm_ops_amd;
94
95int vm_create(const char *name, struct vm **retvm);
96void vm_destroy(struct vm *vm);
97const char *vm_name(struct vm *vm);
98int vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len);
99int vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
100int vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
101void *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot,
102		  void **cookie);
103void vm_gpa_release(void *cookie);
104int vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase,
105	      struct vm_memory_segment *seg);
106int vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len,
107		  vm_offset_t *offset, struct vm_object **object);
108boolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa);
109int vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval);
110int vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val);
111int vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
112		    struct seg_desc *ret_desc);
113int vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
114		    struct seg_desc *desc);
115int vm_run(struct vm *vm, struct vm_run *vmrun);
116int vm_inject_event(struct vm *vm, int vcpu, int type,
117		    int vector, uint32_t error_code, int error_code_valid);
118int vm_inject_nmi(struct vm *vm, int vcpu);
119int vm_nmi_pending(struct vm *vm, int vcpuid);
120void vm_nmi_clear(struct vm *vm, int vcpuid);
121uint64_t *vm_guest_msrs(struct vm *vm, int cpu);
122struct vlapic *vm_lapic(struct vm *vm, int cpu);
123struct vioapic *vm_ioapic(struct vm *vm);
124struct vhpet *vm_hpet(struct vm *vm);
125int vm_get_capability(struct vm *vm, int vcpu, int type, int *val);
126int vm_set_capability(struct vm *vm, int vcpu, int type, int val);
127int vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state);
128int vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state);
129int vm_apicid2vcpuid(struct vm *vm, int apicid);
130void vm_activate_cpu(struct vm *vm, int vcpu);
131cpuset_t vm_active_cpus(struct vm *vm);
132struct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid);
133
134/*
135 * Return 1 if device indicated by bus/slot/func is supposed to be a
136 * pci passthrough device.
137 *
138 * Return 0 otherwise.
139 */
140int vmm_is_pptdev(int bus, int slot, int func);
141
142void *vm_iommu_domain(struct vm *vm);
143
144enum vcpu_state {
145	VCPU_IDLE,
146	VCPU_FROZEN,
147	VCPU_RUNNING,
148	VCPU_SLEEPING,
149};
150
151int vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state);
152enum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu);
153
154static int __inline
155vcpu_is_running(struct vm *vm, int vcpu, int *hostcpu)
156{
157	return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING);
158}
159
160void *vcpu_stats(struct vm *vm, int vcpu);
161void vcpu_notify_event(struct vm *vm, int vcpuid);
162struct vmspace *vm_get_vmspace(struct vm *vm);
163int vm_assign_pptdev(struct vm *vm, int bus, int slot, int func);
164int vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func);
165#endif	/* KERNEL */
166
167#include <machine/vmm_instruction_emul.h>
168
169#define	VM_MAXCPU	16			/* maximum virtual cpus */
170
171/*
172 * Identifiers for events that can be injected into the VM
173 */
174enum vm_event_type {
175	VM_EVENT_NONE,
176	VM_HW_INTR,
177	VM_NMI,
178	VM_HW_EXCEPTION,
179	VM_SW_INTR,
180	VM_PRIV_SW_EXCEPTION,
181	VM_SW_EXCEPTION,
182	VM_EVENT_MAX
183};
184
185/*
186 * Identifiers for architecturally defined registers.
187 */
188enum vm_reg_name {
189	VM_REG_GUEST_RAX,
190	VM_REG_GUEST_RBX,
191	VM_REG_GUEST_RCX,
192	VM_REG_GUEST_RDX,
193	VM_REG_GUEST_RSI,
194	VM_REG_GUEST_RDI,
195	VM_REG_GUEST_RBP,
196	VM_REG_GUEST_R8,
197	VM_REG_GUEST_R9,
198	VM_REG_GUEST_R10,
199	VM_REG_GUEST_R11,
200	VM_REG_GUEST_R12,
201	VM_REG_GUEST_R13,
202	VM_REG_GUEST_R14,
203	VM_REG_GUEST_R15,
204	VM_REG_GUEST_CR0,
205	VM_REG_GUEST_CR3,
206	VM_REG_GUEST_CR4,
207	VM_REG_GUEST_DR7,
208	VM_REG_GUEST_RSP,
209	VM_REG_GUEST_RIP,
210	VM_REG_GUEST_RFLAGS,
211	VM_REG_GUEST_ES,
212	VM_REG_GUEST_CS,
213	VM_REG_GUEST_SS,
214	VM_REG_GUEST_DS,
215	VM_REG_GUEST_FS,
216	VM_REG_GUEST_GS,
217	VM_REG_GUEST_LDTR,
218	VM_REG_GUEST_TR,
219	VM_REG_GUEST_IDTR,
220	VM_REG_GUEST_GDTR,
221	VM_REG_GUEST_EFER,
222	VM_REG_LAST
223};
224
225/*
226 * Identifiers for optional vmm capabilities
227 */
228enum vm_cap_type {
229	VM_CAP_HALT_EXIT,
230	VM_CAP_MTRAP_EXIT,
231	VM_CAP_PAUSE_EXIT,
232	VM_CAP_UNRESTRICTED_GUEST,
233	VM_CAP_ENABLE_INVPCID,
234	VM_CAP_MAX
235};
236
237enum x2apic_state {
238	X2APIC_ENABLED,
239	X2APIC_AVAILABLE,
240	X2APIC_DISABLED,
241	X2APIC_STATE_LAST
242};
243
244/*
245 * The 'access' field has the format specified in Table 21-2 of the Intel
246 * Architecture Manual vol 3b.
247 *
248 * XXX The contents of the 'access' field are architecturally defined except
249 * bit 16 - Segment Unusable.
250 */
251struct seg_desc {
252	uint64_t	base;
253	uint32_t	limit;
254	uint32_t	access;
255};
256
257enum vm_exitcode {
258	VM_EXITCODE_INOUT,
259	VM_EXITCODE_VMX,
260	VM_EXITCODE_BOGUS,
261	VM_EXITCODE_RDMSR,
262	VM_EXITCODE_WRMSR,
263	VM_EXITCODE_HLT,
264	VM_EXITCODE_MTRAP,
265	VM_EXITCODE_PAUSE,
266	VM_EXITCODE_PAGING,
267	VM_EXITCODE_INST_EMUL,
268	VM_EXITCODE_SPINUP_AP,
269	VM_EXITCODE_SPINDOWN_CPU,
270	VM_EXITCODE_MAX
271};
272
273struct vm_exit {
274	enum vm_exitcode	exitcode;
275	int			inst_length;	/* 0 means unknown */
276	uint64_t		rip;
277	union {
278		struct {
279			uint16_t	bytes:3;	/* 1 or 2 or 4 */
280			uint16_t	in:1;		/* out is 0, in is 1 */
281			uint16_t	string:1;
282			uint16_t	rep:1;
283			uint16_t	port;
284			uint32_t	eax;		/* valid for out */
285		} inout;
286		struct {
287			uint64_t	gpa;
288			int		fault_type;
289			int		protection;
290		} paging;
291		struct {
292			uint64_t	gpa;
293			uint64_t	gla;
294			uint64_t	cr3;
295			struct vie	vie;
296		} inst_emul;
297		/*
298		 * VMX specific payload. Used when there is no "better"
299		 * exitcode to represent the VM-exit.
300		 */
301		struct {
302			int		error;		/* vmx inst error */
303			uint32_t	exit_reason;
304			uint64_t	exit_qualification;
305		} vmx;
306		struct {
307			uint32_t	code;		/* ecx value */
308			uint64_t	wval;
309		} msr;
310		struct {
311			int		vcpu;
312			uint64_t	rip;
313		} spinup_ap;
314		struct {
315			uint64_t	rflags;
316		} hlt;
317	} u;
318};
319
320#endif	/* _VMM_H_ */
321