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
45struct vcpu;
46
47enum 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 */
59enum 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#define VM_MAX_NAMELEN 32
107#define VM_MAX_SUFFIXLEN 15
108
109#ifdef _KERNEL
110
111struct vm;
112struct vm_exception;
113struct vm_exit;
114struct vm_run;
115struct vm_object;
116struct vm_guest_paging;
117struct vm_aplic_descr;
118struct pmap;
119
120struct vm_eventinfo {
121 void *rptr; /* rendezvous cookie */
122 int *sptr; /* suspend cookie */
123 int *iptr; /* reqidle cookie */
124};
125
126int vm_create(const char *name, struct vm **retvm);
127struct vcpu *vm_alloc_vcpu(struct vm *vm, int vcpuid);
128void vm_disable_vcpu_creation(struct vm *vm);
129void vm_slock_vcpus(struct vm *vm);
130void vm_unlock_vcpus(struct vm *vm);
131void vm_destroy(struct vm *vm);
132int vm_reinit(struct vm *vm);
133const char *vm_name(struct vm *vm);
134
135uint16_t vm_get_maxcpus(struct vm *vm);
136void vm_get_topology(struct vm *vm, uint16_t *sockets, uint16_t *cores,
137 uint16_t *threads, uint16_t *maxcpus);
138int vm_set_topology(struct vm *vm, uint16_t sockets, uint16_t cores,
139 uint16_t threads, uint16_t maxcpus);
140int vm_get_register(struct vcpu *vcpu, int reg, uint64_t *retval);
141int vm_set_register(struct vcpu *vcpu, int reg, uint64_t val);
142int vm_run(struct vcpu *vcpu);
143int vm_suspend(struct vm *vm, enum vm_suspend_how how);
144void* vm_get_cookie(struct vm *vm);
145int vcpu_vcpuid(struct vcpu *vcpu);
146void *vcpu_get_cookie(struct vcpu *vcpu);
147struct vm *vcpu_vm(struct vcpu *vcpu);
148struct vcpu *vm_vcpu(struct vm *vm, int cpu);
149int vm_get_capability(struct vcpu *vcpu, int type, int *val);
150int vm_set_capability(struct vcpu *vcpu, int type, int val);
151int vm_activate_cpu(struct vcpu *vcpu);
152int vm_suspend_cpu(struct vm *vm, struct vcpu *vcpu);
153int vm_resume_cpu(struct vm *vm, struct vcpu *vcpu);
154int vm_inject_exception(struct vcpu *vcpu, uint64_t scause);
155int vm_attach_aplic(struct vm *vm, struct vm_aplic_descr *descr);
156int vm_assert_irq(struct vm *vm, uint32_t irq);
157int vm_deassert_irq(struct vm *vm, uint32_t irq);
158int vm_raise_msi(struct vm *vm, uint64_t msg, uint64_t addr, int bus, int slot,
159 int func);
160struct vm_exit *vm_exitinfo(struct vcpu *vcpu);
161void vm_exit_suspended(struct vcpu *vcpu, uint64_t pc);
162void vm_exit_debug(struct vcpu *vcpu, uint64_t pc);
163void vm_exit_rendezvous(struct vcpu *vcpu, uint64_t pc);
164void vm_exit_astpending(struct vcpu *vcpu, uint64_t pc);
165
166cpuset_t vm_active_cpus(struct vm *vm);
167cpuset_t vm_debug_cpus(struct vm *vm);
168cpuset_t vm_suspended_cpus(struct vm *vm);
169
170static __inline int
171vcpu_rendezvous_pending(struct vm_eventinfo *info)
172{
173
174 return (*((uintptr_t *)(info->rptr)) != 0);
175}
176
177static __inline int
178vcpu_suspended(struct vm_eventinfo *info)
179{
180
181 return (*info->sptr);
182}
183
184int vcpu_debugged(struct vcpu *vcpu);
185
186enum vcpu_state {
187 VCPU_IDLE,
188 VCPU_FROZEN,
189 VCPU_RUNNING,
190 VCPU_SLEEPING,
191};
192
193int vcpu_set_state(struct vcpu *vcpu, enum vcpu_state state, bool from_idle);
194enum vcpu_state vcpu_get_state(struct vcpu *vcpu, int *hostcpu);
195
196static int __inline
197vcpu_is_running(struct vcpu *vcpu, int *hostcpu)
198{
199 return (vcpu_get_state(vcpu, hostcpu) == VCPU_RUNNING);
200}
201
202#ifdef _SYS_PROC_H_
203static int __inline
204vcpu_should_yield(struct vcpu *vcpu)
205{
206 struct thread *td;
207
208 td = curthread;
209 return (td->td_ast != 0 || td->td_owepreempt != 0);
210}
211#endif
212
213void *vcpu_stats(struct vcpu *vcpu);
214void vcpu_notify_event(struct vcpu *vcpu);
215struct vmspace *vm_vmspace(struct vm *vm);
216struct vm_mem *vm_mem(struct vm *vm);
217
218enum vm_reg_name vm_segment_name(int seg_encoding);
219
220#endif /* _KERNEL */
221
222#define VM_DIR_READ 0
223#define VM_DIR_WRITE 1
224
225#define VM_GP_M_MASK 0x1f
226#define VM_GP_MMU_ENABLED (1 << 5)
227
228struct vm_guest_paging {
229 int flags;
230 int padding;
231};
232
233struct vie {
234 uint8_t access_size:4, sign_extend:1, dir:1, unused:2;
235 enum vm_reg_name reg;
236};
237
238struct vre {
239 uint32_t inst_syndrome;
240 uint8_t dir:1, unused:7;
241 enum vm_reg_name reg;
242};
243
244/*
245 * Identifiers for optional vmm capabilities
246 */
247enum vm_cap_type {
248 VM_CAP_UNRESTRICTED_GUEST,
249 VM_CAP_SSTC,
250 VM_CAP_MAX
251};
252
253enum vm_exitcode {
254 VM_EXITCODE_BOGUS,
255 VM_EXITCODE_ECALL,
256 VM_EXITCODE_HYP,
257 VM_EXITCODE_PAGING,
258 VM_EXITCODE_SUSPENDED,
259 VM_EXITCODE_DEBUG,
260 VM_EXITCODE_INST_EMUL,
261 VM_EXITCODE_WFI,
262 VM_EXITCODE_MAX
263};
264
265struct vm_exit {
266 uint64_t scause;
267 uint64_t sepc;
268 uint64_t stval;
269 uint64_t htval;
270 uint64_t htinst;
271 enum vm_exitcode exitcode;
272 int inst_length;
273 uint64_t pc;
274 union {
275 struct {
276 uint64_t gpa;
277 } paging;
278
279 struct {
280 uint64_t gpa;
281 struct vm_guest_paging paging;
282 struct vie vie;
283 } inst_emul;
284
285 struct {
286 uint64_t args[8];
287 } ecall;
288
289 struct {
290 enum vm_suspend_how how;
291 } suspended;
292
293 struct {
294 uint64_t scause;
295 } hyp;
296 } u;
297};
298
299#endif /* _VMM_H_ */