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