xref: /freebsd/sys/riscv/include/vmm.h (revision 7377c87e467343e71b3e803708b98e04ea8e84bd)
1 /*
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2015 Mihai Carabas <mihai.carabas@gmail.com>
5  * Copyright (c) 2024 Ruslan Bukin <br@bsdpad.com>
6  *
7  * This software was developed by the University of Cambridge Computer
8  * Laboratory (Department of Computer Science and Technology) under Innovate
9  * UK project 105694, "Digital Security by Design (DSbD) Technology Platform
10  * Prototype".
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef _VMM_H_
35 #define	_VMM_H_
36 
37 #include <sys/param.h>
38 #include <sys/cpuset.h>
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41 
42 #include "pte.h"
43 #include "pmap.h"
44 
45 struct vcpu;
46 
47 enum vm_suspend_how {
48 	VM_SUSPEND_NONE,
49 	VM_SUSPEND_RESET,
50 	VM_SUSPEND_POWEROFF,
51 	VM_SUSPEND_HALT,
52 	VM_SUSPEND_DESTROY,
53 	VM_SUSPEND_LAST
54 };
55 
56 /*
57  * Identifiers for architecturally defined registers.
58  */
59 enum vm_reg_name {
60 	VM_REG_GUEST_ZERO = 0,
61 	VM_REG_GUEST_RA,
62 	VM_REG_GUEST_SP,
63 	VM_REG_GUEST_GP,
64 	VM_REG_GUEST_TP,
65 	VM_REG_GUEST_T0,
66 	VM_REG_GUEST_T1,
67 	VM_REG_GUEST_T2,
68 	VM_REG_GUEST_S0,
69 	VM_REG_GUEST_S1,
70 	VM_REG_GUEST_A0,
71 	VM_REG_GUEST_A1,
72 	VM_REG_GUEST_A2,
73 	VM_REG_GUEST_A3,
74 	VM_REG_GUEST_A4,
75 	VM_REG_GUEST_A5,
76 	VM_REG_GUEST_A6,
77 	VM_REG_GUEST_A7,
78 	VM_REG_GUEST_S2,
79 	VM_REG_GUEST_S3,
80 	VM_REG_GUEST_S4,
81 	VM_REG_GUEST_S5,
82 	VM_REG_GUEST_S6,
83 	VM_REG_GUEST_S7,
84 	VM_REG_GUEST_S8,
85 	VM_REG_GUEST_S9,
86 	VM_REG_GUEST_S10,
87 	VM_REG_GUEST_S11,
88 	VM_REG_GUEST_T3,
89 	VM_REG_GUEST_T4,
90 	VM_REG_GUEST_T5,
91 	VM_REG_GUEST_T6,
92 	VM_REG_GUEST_SEPC,
93 	VM_REG_LAST
94 };
95 
96 #define	VM_INTINFO_VECTOR(info)	((info) & 0xff)
97 #define	VM_INTINFO_DEL_ERRCODE	0x800
98 #define	VM_INTINFO_RSVD		0x7ffff000
99 #define	VM_INTINFO_VALID	0x80000000
100 #define	VM_INTINFO_TYPE		0x700
101 #define	VM_INTINFO_HWINTR	(0 << 8)
102 #define	VM_INTINFO_NMI		(2 << 8)
103 #define	VM_INTINFO_HWEXCEPTION	(3 << 8)
104 #define	VM_INTINFO_SWINTR	(4 << 8)
105 
106 #ifdef _KERNEL
107 
108 struct vm;
109 struct vm_exception;
110 struct vm_exit;
111 struct vm_run;
112 struct vm_object;
113 struct vm_guest_paging;
114 struct vm_aplic_descr;
115 struct pmap;
116 
117 struct vm_eventinfo {
118 	void	*rptr;		/* rendezvous cookie */
119 	int	*sptr;		/* suspend cookie */
120 	int	*iptr;		/* reqidle cookie */
121 };
122 
123 #define	DECLARE_VMMOPS_FUNC(ret_type, opname, args)		\
124 	ret_type vmmops_##opname args
125 
126 DECLARE_VMMOPS_FUNC(int, modinit, (void));
127 DECLARE_VMMOPS_FUNC(int, modcleanup, (void));
128 DECLARE_VMMOPS_FUNC(void *, init, (struct vm *vm, struct pmap *pmap));
129 DECLARE_VMMOPS_FUNC(int, gla2gpa, (void *vcpui, struct vm_guest_paging *paging,
130     uint64_t gla, int prot, uint64_t *gpa, int *is_fault));
131 DECLARE_VMMOPS_FUNC(int, run, (void *vcpui, register_t pc, struct pmap *pmap,
132     struct vm_eventinfo *info));
133 DECLARE_VMMOPS_FUNC(void, cleanup, (void *vmi));
134 DECLARE_VMMOPS_FUNC(void *, vcpu_init, (void *vmi, struct vcpu *vcpu,
135     int vcpu_id));
136 DECLARE_VMMOPS_FUNC(void, vcpu_cleanup, (void *vcpui));
137 DECLARE_VMMOPS_FUNC(int, exception, (void *vcpui, uint64_t scause));
138 DECLARE_VMMOPS_FUNC(int, getreg, (void *vcpui, int num, uint64_t *retval));
139 DECLARE_VMMOPS_FUNC(int, setreg, (void *vcpui, int num, uint64_t val));
140 DECLARE_VMMOPS_FUNC(int, getcap, (void *vcpui, int num, int *retval));
141 DECLARE_VMMOPS_FUNC(int, setcap, (void *vcpui, int num, int val));
142 DECLARE_VMMOPS_FUNC(struct vmspace *, vmspace_alloc, (vm_offset_t min,
143     vm_offset_t max));
144 DECLARE_VMMOPS_FUNC(void, vmspace_free, (struct vmspace *vmspace));
145 
146 int vm_create(const char *name, struct vm **retvm);
147 struct vcpu *vm_alloc_vcpu(struct vm *vm, int vcpuid);
148 void vm_disable_vcpu_creation(struct vm *vm);
149 void vm_lock_vcpus(struct vm *vm);
150 void vm_unlock_vcpus(struct vm *vm);
151 void vm_destroy(struct vm *vm);
152 int vm_reinit(struct vm *vm);
153 const char *vm_name(struct vm *vm);
154 
155 uint16_t vm_get_maxcpus(struct vm *vm);
156 void vm_get_topology(struct vm *vm, uint16_t *sockets, uint16_t *cores,
157     uint16_t *threads, uint16_t *maxcpus);
158 int vm_set_topology(struct vm *vm, uint16_t sockets, uint16_t cores,
159     uint16_t threads, uint16_t maxcpus);
160 int vm_get_register(struct vcpu *vcpu, int reg, uint64_t *retval);
161 int vm_set_register(struct vcpu *vcpu, int reg, uint64_t val);
162 int vm_run(struct vcpu *vcpu);
163 int vm_suspend(struct vm *vm, enum vm_suspend_how how);
164 void* vm_get_cookie(struct vm *vm);
165 int vcpu_vcpuid(struct vcpu *vcpu);
166 void *vcpu_get_cookie(struct vcpu *vcpu);
167 struct vm *vcpu_vm(struct vcpu *vcpu);
168 struct vcpu *vm_vcpu(struct vm *vm, int cpu);
169 int vm_get_capability(struct vcpu *vcpu, int type, int *val);
170 int vm_set_capability(struct vcpu *vcpu, int type, int val);
171 int vm_activate_cpu(struct vcpu *vcpu);
172 int vm_suspend_cpu(struct vm *vm, struct vcpu *vcpu);
173 int vm_resume_cpu(struct vm *vm, struct vcpu *vcpu);
174 int vm_inject_exception(struct vcpu *vcpu, uint64_t scause);
175 int vm_attach_aplic(struct vm *vm, struct vm_aplic_descr *descr);
176 int vm_assert_irq(struct vm *vm, uint32_t irq);
177 int vm_deassert_irq(struct vm *vm, uint32_t irq);
178 int vm_raise_msi(struct vm *vm, uint64_t msg, uint64_t addr, int bus, int slot,
179     int func);
180 struct vm_exit *vm_exitinfo(struct vcpu *vcpu);
181 void vm_exit_suspended(struct vcpu *vcpu, uint64_t pc);
182 void vm_exit_debug(struct vcpu *vcpu, uint64_t pc);
183 void vm_exit_rendezvous(struct vcpu *vcpu, uint64_t pc);
184 void vm_exit_astpending(struct vcpu *vcpu, uint64_t pc);
185 
186 cpuset_t vm_active_cpus(struct vm *vm);
187 cpuset_t vm_debug_cpus(struct vm *vm);
188 cpuset_t vm_suspended_cpus(struct vm *vm);
189 
190 static __inline int
vcpu_rendezvous_pending(struct vm_eventinfo * info)191 vcpu_rendezvous_pending(struct vm_eventinfo *info)
192 {
193 
194 	return (*((uintptr_t *)(info->rptr)) != 0);
195 }
196 
197 static __inline int
vcpu_suspended(struct vm_eventinfo * info)198 vcpu_suspended(struct vm_eventinfo *info)
199 {
200 
201 	return (*info->sptr);
202 }
203 
204 int vcpu_debugged(struct vcpu *vcpu);
205 
206 enum vcpu_state {
207 	VCPU_IDLE,
208 	VCPU_FROZEN,
209 	VCPU_RUNNING,
210 	VCPU_SLEEPING,
211 };
212 
213 int vcpu_set_state(struct vcpu *vcpu, enum vcpu_state state, bool from_idle);
214 enum vcpu_state vcpu_get_state(struct vcpu *vcpu, int *hostcpu);
215 
216 static int __inline
vcpu_is_running(struct vcpu * vcpu,int * hostcpu)217 vcpu_is_running(struct vcpu *vcpu, int *hostcpu)
218 {
219 	return (vcpu_get_state(vcpu, hostcpu) == VCPU_RUNNING);
220 }
221 
222 #ifdef _SYS_PROC_H_
223 static int __inline
vcpu_should_yield(struct vcpu * vcpu)224 vcpu_should_yield(struct vcpu *vcpu)
225 {
226 	struct thread *td;
227 
228 	td = curthread;
229 	return (td->td_ast != 0 || td->td_owepreempt != 0);
230 }
231 #endif
232 
233 void *vcpu_stats(struct vcpu *vcpu);
234 void vcpu_notify_event(struct vcpu *vcpu);
235 struct vm_mem *vm_mem(struct vm *vm);
236 
237 enum vm_reg_name vm_segment_name(int seg_encoding);
238 
239 #endif	/* _KERNEL */
240 
241 #define	VM_DIR_READ	0
242 #define	VM_DIR_WRITE	1
243 
244 #define	VM_GP_M_MASK		0x1f
245 #define	VM_GP_MMU_ENABLED	(1 << 5)
246 
247 struct vm_guest_paging {
248 	int		flags;
249 	int		padding;
250 };
251 
252 struct vie {
253 	uint8_t access_size:4, sign_extend:1, dir:1, unused:2;
254 	enum vm_reg_name reg;
255 };
256 
257 struct vre {
258 	uint32_t inst_syndrome;
259 	uint8_t dir:1, unused:7;
260 	enum vm_reg_name reg;
261 };
262 
263 /*
264  * Identifiers for optional vmm capabilities
265  */
266 enum vm_cap_type {
267 	VM_CAP_UNRESTRICTED_GUEST,
268 	VM_CAP_SSTC,
269 	VM_CAP_MAX
270 };
271 
272 enum vm_exitcode {
273 	VM_EXITCODE_BOGUS,
274 	VM_EXITCODE_ECALL,
275 	VM_EXITCODE_HYP,
276 	VM_EXITCODE_PAGING,
277 	VM_EXITCODE_SUSPENDED,
278 	VM_EXITCODE_DEBUG,
279 	VM_EXITCODE_INST_EMUL,
280 	VM_EXITCODE_WFI,
281 	VM_EXITCODE_MAX
282 };
283 
284 struct vm_exit {
285 	uint64_t scause;
286 	uint64_t sepc;
287 	uint64_t stval;
288 	uint64_t htval;
289 	uint64_t htinst;
290 	enum vm_exitcode exitcode;
291 	int inst_length;
292 	uint64_t pc;
293 	union {
294 		struct {
295 			uint64_t gpa;
296 		} paging;
297 
298 		struct {
299 			uint64_t gpa;
300 			struct vm_guest_paging paging;
301 			struct vie vie;
302 		} inst_emul;
303 
304 		struct {
305 			uint64_t args[8];
306 		} ecall;
307 
308 		struct {
309 			enum vm_suspend_how how;
310 		} suspended;
311 
312 		struct {
313 			uint64_t scause;
314 		} hyp;
315 	} u;
316 };
317 
318 #endif	/* _VMM_H_ */
319