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