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