xref: /freebsd/sys/arm64/include/vmm.h (revision 059b0b7046639121f3dca48f5de051e019f9d57c)
1 /*
2  * Copyright (C) 2015 Mihai Carabas <mihai.carabas@gmail.com>
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 AUTHOR AND CONTRIBUTORS ``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 AUTHOR 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 
27 #ifndef _VMM_H_
28 #define	_VMM_H_
29 
30 #include <sys/param.h>
31 #include <sys/cpuset.h>
32 #include <vm/vm.h>
33 #include <vm/pmap.h>
34 
35 #include "pte.h"
36 #include "pmap.h"
37 
38 struct vcpu;
39 
40 enum vm_suspend_how {
41 	VM_SUSPEND_NONE,
42 	VM_SUSPEND_RESET,
43 	VM_SUSPEND_POWEROFF,
44 	VM_SUSPEND_HALT,
45 	VM_SUSPEND_DESTROY,
46 	VM_SUSPEND_LAST
47 };
48 
49 /*
50  * Identifiers for architecturally defined registers.
51  */
52 enum vm_reg_name {
53 	VM_REG_GUEST_X0 = 0,
54 	VM_REG_GUEST_X1,
55 	VM_REG_GUEST_X2,
56 	VM_REG_GUEST_X3,
57 	VM_REG_GUEST_X4,
58 	VM_REG_GUEST_X5,
59 	VM_REG_GUEST_X6,
60 	VM_REG_GUEST_X7,
61 	VM_REG_GUEST_X8,
62 	VM_REG_GUEST_X9,
63 	VM_REG_GUEST_X10,
64 	VM_REG_GUEST_X11,
65 	VM_REG_GUEST_X12,
66 	VM_REG_GUEST_X13,
67 	VM_REG_GUEST_X14,
68 	VM_REG_GUEST_X15,
69 	VM_REG_GUEST_X16,
70 	VM_REG_GUEST_X17,
71 	VM_REG_GUEST_X18,
72 	VM_REG_GUEST_X19,
73 	VM_REG_GUEST_X20,
74 	VM_REG_GUEST_X21,
75 	VM_REG_GUEST_X22,
76 	VM_REG_GUEST_X23,
77 	VM_REG_GUEST_X24,
78 	VM_REG_GUEST_X25,
79 	VM_REG_GUEST_X26,
80 	VM_REG_GUEST_X27,
81 	VM_REG_GUEST_X28,
82 	VM_REG_GUEST_X29,
83 	VM_REG_GUEST_LR,
84 	VM_REG_GUEST_SP,
85 	VM_REG_GUEST_PC,
86 	VM_REG_GUEST_CPSR,
87 
88 	VM_REG_GUEST_SCTLR_EL1,
89 	VM_REG_GUEST_TTBR0_EL1,
90 	VM_REG_GUEST_TTBR1_EL1,
91 	VM_REG_GUEST_TCR_EL1,
92 	VM_REG_GUEST_TCR2_EL1,
93 	VM_REG_GUEST_MPIDR_EL1,
94 	VM_REG_LAST
95 };
96 
97 #define	VM_INTINFO_VECTOR(info)	((info) & 0xff)
98 #define	VM_INTINFO_DEL_ERRCODE	0x800
99 #define	VM_INTINFO_RSVD		0x7ffff000
100 #define	VM_INTINFO_VALID	0x80000000
101 #define	VM_INTINFO_TYPE		0x700
102 #define	VM_INTINFO_HWINTR	(0 << 8)
103 #define	VM_INTINFO_NMI		(2 << 8)
104 #define	VM_INTINFO_HWEXCEPTION	(3 << 8)
105 #define	VM_INTINFO_SWINTR	(4 << 8)
106 
107 #define VM_GUEST_BASE_IPA	0x80000000UL	/* Guest kernel start ipa */
108 
109 /*
110  * The VM name has to fit into the pathname length constraints of devfs,
111  * governed primarily by SPECNAMELEN.  The length is the total number of
112  * characters in the full path, relative to the mount point and not
113  * including any leading '/' characters.
114  * A prefix and a suffix are added to the name specified by the user.
115  * The prefix is usually "vmm/" or "vmm.io/", but can be a few characters
116  * longer for future use.
117  * The suffix is a string that identifies a bootrom image or some similar
118  * image that is attached to the VM. A separator character gets added to
119  * the suffix automatically when generating the full path, so it must be
120  * accounted for, reducing the effective length by 1.
121  * The effective length of a VM name is 229 bytes for FreeBSD 13 and 37
122  * bytes for FreeBSD 12.  A minimum length is set for safety and supports
123  * a SPECNAMELEN as small as 32 on old systems.
124  */
125 #define VM_MAX_PREFIXLEN 10
126 #define VM_MAX_SUFFIXLEN 15
127 #define VM_MAX_NAMELEN \
128     (SPECNAMELEN - VM_MAX_PREFIXLEN - VM_MAX_SUFFIXLEN - 1)
129 
130 #ifdef _KERNEL
131 struct vm;
132 struct vm_exception;
133 struct vm_exit;
134 struct vm_run;
135 struct vm_object;
136 struct vm_guest_paging;
137 struct vm_vgic_descr;
138 struct pmap;
139 
140 struct vm_eventinfo {
141 	void	*rptr;		/* rendezvous cookie */
142 	int	*sptr;		/* suspend cookie */
143 	int	*iptr;		/* reqidle cookie */
144 };
145 
146 #define	DECLARE_VMMOPS_FUNC(ret_type, opname, args)			\
147 	ret_type vmmops_##opname args
148 
149 DECLARE_VMMOPS_FUNC(int, modinit, (int ipinum));
150 DECLARE_VMMOPS_FUNC(int, modcleanup, (void));
151 DECLARE_VMMOPS_FUNC(void *, init, (struct vm *vm, struct pmap *pmap));
152 DECLARE_VMMOPS_FUNC(int, gla2gpa, (void *vcpui, struct vm_guest_paging *paging,
153     uint64_t gla, int prot, uint64_t *gpa, int *is_fault));
154 DECLARE_VMMOPS_FUNC(int, run, (void *vcpui, register_t pc, struct pmap *pmap,
155     struct vm_eventinfo *info));
156 DECLARE_VMMOPS_FUNC(void, cleanup, (void *vmi));
157 DECLARE_VMMOPS_FUNC(void *, vcpu_init, (void *vmi, struct vcpu *vcpu,
158     int vcpu_id));
159 DECLARE_VMMOPS_FUNC(void, vcpu_cleanup, (void *vcpui));
160 DECLARE_VMMOPS_FUNC(int, exception, (void *vcpui, uint64_t esr, uint64_t far));
161 DECLARE_VMMOPS_FUNC(int, getreg, (void *vcpui, int num, uint64_t *retval));
162 DECLARE_VMMOPS_FUNC(int, setreg, (void *vcpui, int num, uint64_t val));
163 DECLARE_VMMOPS_FUNC(int, getcap, (void *vcpui, int num, int *retval));
164 DECLARE_VMMOPS_FUNC(int, setcap, (void *vcpui, int num, int val));
165 DECLARE_VMMOPS_FUNC(struct vmspace *, vmspace_alloc, (vm_offset_t min,
166     vm_offset_t max));
167 DECLARE_VMMOPS_FUNC(void, vmspace_free, (struct vmspace *vmspace));
168 #ifdef notyet
169 #ifdef BHYVE_SNAPSHOT
170 DECLARE_VMMOPS_FUNC(int, snapshot, (void *vmi, struct vm_snapshot_meta *meta));
171 DECLARE_VMMOPS_FUNC(int, vcpu_snapshot, (void *vcpui,
172     struct vm_snapshot_meta *meta));
173 DECLARE_VMMOPS_FUNC(int, restore_tsc, (void *vcpui, uint64_t now));
174 #endif
175 #endif
176 
177 int vm_create(const char *name, struct vm **retvm);
178 struct vcpu *vm_alloc_vcpu(struct vm *vm, int vcpuid);
179 void vm_disable_vcpu_creation(struct vm *vm);
180 void vm_slock_vcpus(struct vm *vm);
181 void vm_unlock_vcpus(struct vm *vm);
182 void vm_destroy(struct vm *vm);
183 int vm_reinit(struct vm *vm);
184 const char *vm_name(struct vm *vm);
185 
186 uint16_t vm_get_maxcpus(struct vm *vm);
187 void vm_get_topology(struct vm *vm, uint16_t *sockets, uint16_t *cores,
188     uint16_t *threads, uint16_t *maxcpus);
189 int vm_set_topology(struct vm *vm, uint16_t sockets, uint16_t cores,
190     uint16_t threads, uint16_t maxcpus);
191 int vm_get_register(struct vcpu *vcpu, int reg, uint64_t *retval);
192 int vm_set_register(struct vcpu *vcpu, int reg, uint64_t val);
193 int vm_run(struct vcpu *vcpu);
194 int vm_suspend(struct vm *vm, enum vm_suspend_how how);
195 void* vm_get_cookie(struct vm *vm);
196 int vcpu_vcpuid(struct vcpu *vcpu);
197 void *vcpu_get_cookie(struct vcpu *vcpu);
198 struct vm *vcpu_vm(struct vcpu *vcpu);
199 struct vcpu *vm_vcpu(struct vm *vm, int cpu);
200 int vm_get_capability(struct vcpu *vcpu, int type, int *val);
201 int vm_set_capability(struct vcpu *vcpu, int type, int val);
202 int vm_activate_cpu(struct vcpu *vcpu);
203 int vm_suspend_cpu(struct vm *vm, struct vcpu *vcpu);
204 int vm_resume_cpu(struct vm *vm, struct vcpu *vcpu);
205 int vm_inject_exception(struct vcpu *vcpu, uint64_t esr, uint64_t far);
206 int vm_attach_vgic(struct vm *vm, struct vm_vgic_descr *descr);
207 int vm_assert_irq(struct vm *vm, uint32_t irq);
208 int vm_deassert_irq(struct vm *vm, uint32_t irq);
209 int vm_raise_msi(struct vm *vm, uint64_t msg, uint64_t addr, int bus, int slot,
210     int func);
211 struct vm_exit *vm_exitinfo(struct vcpu *vcpu);
212 void vm_exit_suspended(struct vcpu *vcpu, uint64_t pc);
213 void vm_exit_debug(struct vcpu *vcpu, uint64_t pc);
214 void vm_exit_rendezvous(struct vcpu *vcpu, uint64_t pc);
215 void vm_exit_astpending(struct vcpu *vcpu, uint64_t pc);
216 
217 cpuset_t vm_active_cpus(struct vm *vm);
218 cpuset_t vm_debug_cpus(struct vm *vm);
219 cpuset_t vm_suspended_cpus(struct vm *vm);
220 
221 static __inline int
vcpu_rendezvous_pending(struct vm_eventinfo * info)222 vcpu_rendezvous_pending(struct vm_eventinfo *info)
223 {
224 
225 	return (*((uintptr_t *)(info->rptr)) != 0);
226 }
227 
228 static __inline int
vcpu_suspended(struct vm_eventinfo * info)229 vcpu_suspended(struct vm_eventinfo *info)
230 {
231 
232 	return (*info->sptr);
233 }
234 
235 int vcpu_debugged(struct vcpu *vcpu);
236 
237 enum vcpu_state {
238 	VCPU_IDLE,
239 	VCPU_FROZEN,
240 	VCPU_RUNNING,
241 	VCPU_SLEEPING,
242 };
243 
244 int vcpu_set_state(struct vcpu *vcpu, enum vcpu_state state, bool from_idle);
245 enum vcpu_state vcpu_get_state(struct vcpu *vcpu, int *hostcpu);
246 
247 static int __inline
vcpu_is_running(struct vcpu * vcpu,int * hostcpu)248 vcpu_is_running(struct vcpu *vcpu, int *hostcpu)
249 {
250 	return (vcpu_get_state(vcpu, hostcpu) == VCPU_RUNNING);
251 }
252 
253 #ifdef _SYS_PROC_H_
254 static int __inline
vcpu_should_yield(struct vcpu * vcpu)255 vcpu_should_yield(struct vcpu *vcpu)
256 {
257 	struct thread *td;
258 
259 	td = curthread;
260 	return (td->td_ast != 0 || td->td_owepreempt != 0);
261 }
262 #endif
263 
264 void *vcpu_stats(struct vcpu *vcpu);
265 void vcpu_notify_event(struct vcpu *vcpu);
266 struct vm_mem *vm_mem(struct vm *vm);
267 
268 enum vm_reg_name vm_segment_name(int seg_encoding);
269 
270 struct vm_copyinfo {
271 	uint64_t	gpa;
272 	size_t		len;
273 	void		*hva;
274 	void		*cookie;
275 };
276 
277 #endif	/* _KERNEL */
278 
279 #define	VM_DIR_READ	0
280 #define	VM_DIR_WRITE	1
281 
282 #define	VM_GP_M_MASK		0x1f
283 #define	VM_GP_MMU_ENABLED	(1 << 5)
284 
285 struct vm_guest_paging {
286 	uint64_t	ttbr0_addr;
287 	uint64_t	ttbr1_addr;
288 	uint64_t	tcr_el1;
289 	uint64_t	tcr2_el1;
290 	int		flags;
291 	int		padding;
292 };
293 
294 struct vie {
295 	uint8_t access_size:4, sign_extend:1, dir:1, unused:2;
296 	enum vm_reg_name reg;
297 };
298 
299 struct vre {
300 	uint32_t inst_syndrome;
301 	uint8_t dir:1, unused:7;
302 	enum vm_reg_name reg;
303 };
304 
305 /*
306  * Identifiers for optional vmm capabilities
307  */
308 enum vm_cap_type {
309 	VM_CAP_HALT_EXIT,
310 	VM_CAP_PAUSE_EXIT,
311 	VM_CAP_UNRESTRICTED_GUEST,
312 	VM_CAP_BRK_EXIT,
313 	VM_CAP_SS_EXIT,
314 	VM_CAP_MASK_HWINTR,
315 	VM_CAP_MAX
316 };
317 
318 enum vm_exitcode {
319 	VM_EXITCODE_BOGUS,
320 	VM_EXITCODE_INST_EMUL,
321 	VM_EXITCODE_REG_EMUL,
322 	VM_EXITCODE_HVC,
323 	VM_EXITCODE_SUSPENDED,
324 	VM_EXITCODE_HYP,
325 	VM_EXITCODE_WFI,
326 	VM_EXITCODE_PAGING,
327 	VM_EXITCODE_SMCCC,
328 	VM_EXITCODE_DEBUG,
329 	VM_EXITCODE_BRK,
330 	VM_EXITCODE_SS,
331 	VM_EXITCODE_MAX
332 };
333 
334 struct vm_exit {
335 	enum vm_exitcode	exitcode;
336 	int			inst_length;
337 	uint64_t		pc;
338 	union {
339 		/*
340 		 * ARM specific payload.
341 		 */
342 		struct {
343 			uint32_t	exception_nr;
344 			uint32_t	pad;
345 			uint64_t	esr_el2;	/* Exception Syndrome Register */
346 			uint64_t	far_el2;	/* Fault Address Register */
347 			uint64_t	hpfar_el2;	/* Hypervisor IPA Fault Address Register */
348 		} hyp;
349 		struct {
350 			struct vre 	vre;
351 		} reg_emul;
352 		struct {
353 			uint64_t	gpa;
354 			uint64_t	esr;
355 		} paging;
356 		struct {
357 			uint64_t	gpa;
358 			struct vm_guest_paging paging;
359 			struct vie	vie;
360 		} inst_emul;
361 
362 		/*
363 		 * A SMCCC call, e.g. starting a core via PSCI.
364 		 * Further arguments can be read by asking the kernel for
365 		 * all register values.
366 		 */
367 		struct {
368 			uint64_t	func_id;
369 			uint64_t	args[7];
370 		} smccc_call;
371 
372 		struct {
373 			enum vm_suspend_how how;
374 		} suspended;
375 	} u;
376 };
377 
378 #endif	/* _VMM_H_ */
379