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 #ifdef _KERNEL
110 struct vm;
111 struct vm_exception;
112 struct vm_exit;
113 struct vm_run;
114 struct vm_object;
115 struct vm_guest_paging;
116 struct vm_vgic_descr;
117 struct pmap;
118
119 struct vm_eventinfo {
120 void *rptr; /* rendezvous cookie */
121 int *sptr; /* suspend cookie */
122 int *iptr; /* reqidle cookie */
123 };
124
125 #define DECLARE_VMMOPS_FUNC(ret_type, opname, args) \
126 ret_type vmmops_##opname args
127
128 DECLARE_VMMOPS_FUNC(int, modinit, (int ipinum));
129 DECLARE_VMMOPS_FUNC(int, modcleanup, (void));
130 DECLARE_VMMOPS_FUNC(void *, init, (struct vm *vm, struct pmap *pmap));
131 DECLARE_VMMOPS_FUNC(int, gla2gpa, (void *vcpui, struct vm_guest_paging *paging,
132 uint64_t gla, int prot, uint64_t *gpa, int *is_fault));
133 DECLARE_VMMOPS_FUNC(int, run, (void *vcpui, register_t pc, struct pmap *pmap,
134 struct vm_eventinfo *info));
135 DECLARE_VMMOPS_FUNC(void, cleanup, (void *vmi));
136 DECLARE_VMMOPS_FUNC(void *, vcpu_init, (void *vmi, struct vcpu *vcpu,
137 int vcpu_id));
138 DECLARE_VMMOPS_FUNC(void, vcpu_cleanup, (void *vcpui));
139 DECLARE_VMMOPS_FUNC(int, exception, (void *vcpui, uint64_t esr, uint64_t far));
140 DECLARE_VMMOPS_FUNC(int, getreg, (void *vcpui, int num, uint64_t *retval));
141 DECLARE_VMMOPS_FUNC(int, setreg, (void *vcpui, int num, uint64_t val));
142 DECLARE_VMMOPS_FUNC(int, getcap, (void *vcpui, int num, int *retval));
143 DECLARE_VMMOPS_FUNC(int, setcap, (void *vcpui, int num, int val));
144 DECLARE_VMMOPS_FUNC(struct vmspace *, vmspace_alloc, (vm_offset_t min,
145 vm_offset_t max));
146 DECLARE_VMMOPS_FUNC(void, vmspace_free, (struct vmspace *vmspace));
147 #ifdef notyet
148 #ifdef BHYVE_SNAPSHOT
149 DECLARE_VMMOPS_FUNC(int, snapshot, (void *vmi, struct vm_snapshot_meta *meta));
150 DECLARE_VMMOPS_FUNC(int, vcpu_snapshot, (void *vcpui,
151 struct vm_snapshot_meta *meta));
152 DECLARE_VMMOPS_FUNC(int, restore_tsc, (void *vcpui, uint64_t now));
153 #endif
154 #endif
155
156 int vm_create(const char *name, struct vm **retvm);
157 struct vcpu *vm_alloc_vcpu(struct vm *vm, int vcpuid);
158 void vm_disable_vcpu_creation(struct vm *vm);
159 void vm_lock_vcpus(struct vm *vm);
160 void vm_unlock_vcpus(struct vm *vm);
161 void vm_destroy(struct vm *vm);
162 int vm_reinit(struct vm *vm);
163 const char *vm_name(struct vm *vm);
164
165 uint16_t vm_get_maxcpus(struct vm *vm);
166 void vm_get_topology(struct vm *vm, uint16_t *sockets, uint16_t *cores,
167 uint16_t *threads, uint16_t *maxcpus);
168 int vm_set_topology(struct vm *vm, uint16_t sockets, uint16_t cores,
169 uint16_t threads, uint16_t maxcpus);
170 int vm_get_register(struct vcpu *vcpu, int reg, uint64_t *retval);
171 int vm_set_register(struct vcpu *vcpu, int reg, uint64_t val);
172 int vm_run(struct vcpu *vcpu);
173 int vm_suspend(struct vm *vm, enum vm_suspend_how how);
174 void* vm_get_cookie(struct vm *vm);
175 int vcpu_vcpuid(struct vcpu *vcpu);
176 void *vcpu_get_cookie(struct vcpu *vcpu);
177 struct vm *vcpu_vm(struct vcpu *vcpu);
178 struct vcpu *vm_vcpu(struct vm *vm, int cpu);
179 int vm_get_capability(struct vcpu *vcpu, int type, int *val);
180 int vm_set_capability(struct vcpu *vcpu, int type, int val);
181 int vm_activate_cpu(struct vcpu *vcpu);
182 int vm_suspend_cpu(struct vm *vm, struct vcpu *vcpu);
183 int vm_resume_cpu(struct vm *vm, struct vcpu *vcpu);
184 int vm_inject_exception(struct vcpu *vcpu, uint64_t esr, uint64_t far);
185 int vm_attach_vgic(struct vm *vm, struct vm_vgic_descr *descr);
186 int vm_assert_irq(struct vm *vm, uint32_t irq);
187 int vm_deassert_irq(struct vm *vm, uint32_t irq);
188 int vm_raise_msi(struct vm *vm, uint64_t msg, uint64_t addr, int bus, int slot,
189 int func);
190 struct vm_exit *vm_exitinfo(struct vcpu *vcpu);
191 void vm_exit_suspended(struct vcpu *vcpu, uint64_t pc);
192 void vm_exit_debug(struct vcpu *vcpu, uint64_t pc);
193 void vm_exit_rendezvous(struct vcpu *vcpu, uint64_t pc);
194 void vm_exit_astpending(struct vcpu *vcpu, uint64_t pc);
195
196 cpuset_t vm_active_cpus(struct vm *vm);
197 cpuset_t vm_debug_cpus(struct vm *vm);
198 cpuset_t vm_suspended_cpus(struct vm *vm);
199
200 static __inline int
vcpu_rendezvous_pending(struct vm_eventinfo * info)201 vcpu_rendezvous_pending(struct vm_eventinfo *info)
202 {
203
204 return (*((uintptr_t *)(info->rptr)) != 0);
205 }
206
207 static __inline int
vcpu_suspended(struct vm_eventinfo * info)208 vcpu_suspended(struct vm_eventinfo *info)
209 {
210
211 return (*info->sptr);
212 }
213
214 int vcpu_debugged(struct vcpu *vcpu);
215
216 enum vcpu_state {
217 VCPU_IDLE,
218 VCPU_FROZEN,
219 VCPU_RUNNING,
220 VCPU_SLEEPING,
221 };
222
223 int vcpu_set_state(struct vcpu *vcpu, enum vcpu_state state, bool from_idle);
224 enum vcpu_state vcpu_get_state(struct vcpu *vcpu, int *hostcpu);
225
226 static int __inline
vcpu_is_running(struct vcpu * vcpu,int * hostcpu)227 vcpu_is_running(struct vcpu *vcpu, int *hostcpu)
228 {
229 return (vcpu_get_state(vcpu, hostcpu) == VCPU_RUNNING);
230 }
231
232 #ifdef _SYS_PROC_H_
233 static int __inline
vcpu_should_yield(struct vcpu * vcpu)234 vcpu_should_yield(struct vcpu *vcpu)
235 {
236 struct thread *td;
237
238 td = curthread;
239 return (td->td_ast != 0 || td->td_owepreempt != 0);
240 }
241 #endif
242
243 void *vcpu_stats(struct vcpu *vcpu);
244 void vcpu_notify_event(struct vcpu *vcpu);
245 struct vm_mem *vm_mem(struct vm *vm);
246
247 enum vm_reg_name vm_segment_name(int seg_encoding);
248
249 struct vm_copyinfo {
250 uint64_t gpa;
251 size_t len;
252 void *hva;
253 void *cookie;
254 };
255
256 #endif /* _KERNEL */
257
258 #define VM_DIR_READ 0
259 #define VM_DIR_WRITE 1
260
261 #define VM_GP_M_MASK 0x1f
262 #define VM_GP_MMU_ENABLED (1 << 5)
263
264 struct vm_guest_paging {
265 uint64_t ttbr0_addr;
266 uint64_t ttbr1_addr;
267 uint64_t tcr_el1;
268 uint64_t tcr2_el1;
269 int flags;
270 int padding;
271 };
272
273 struct vie {
274 uint8_t access_size:4, sign_extend:1, dir:1, unused:2;
275 enum vm_reg_name reg;
276 };
277
278 struct vre {
279 uint32_t inst_syndrome;
280 uint8_t dir:1, unused:7;
281 enum vm_reg_name reg;
282 };
283
284 /*
285 * Identifiers for optional vmm capabilities
286 */
287 enum vm_cap_type {
288 VM_CAP_HALT_EXIT,
289 VM_CAP_PAUSE_EXIT,
290 VM_CAP_UNRESTRICTED_GUEST,
291 VM_CAP_BRK_EXIT,
292 VM_CAP_SS_EXIT,
293 VM_CAP_MASK_HWINTR,
294 VM_CAP_MAX
295 };
296
297 enum vm_exitcode {
298 VM_EXITCODE_BOGUS,
299 VM_EXITCODE_INST_EMUL,
300 VM_EXITCODE_REG_EMUL,
301 VM_EXITCODE_HVC,
302 VM_EXITCODE_SUSPENDED,
303 VM_EXITCODE_HYP,
304 VM_EXITCODE_WFI,
305 VM_EXITCODE_PAGING,
306 VM_EXITCODE_SMCCC,
307 VM_EXITCODE_DEBUG,
308 VM_EXITCODE_BRK,
309 VM_EXITCODE_SS,
310 VM_EXITCODE_MAX
311 };
312
313 struct vm_exit {
314 enum vm_exitcode exitcode;
315 int inst_length;
316 uint64_t pc;
317 union {
318 /*
319 * ARM specific payload.
320 */
321 struct {
322 uint32_t exception_nr;
323 uint32_t pad;
324 uint64_t esr_el2; /* Exception Syndrome Register */
325 uint64_t far_el2; /* Fault Address Register */
326 uint64_t hpfar_el2; /* Hypervisor IPA Fault Address Register */
327 } hyp;
328 struct {
329 struct vre vre;
330 } reg_emul;
331 struct {
332 uint64_t gpa;
333 uint64_t esr;
334 } paging;
335 struct {
336 uint64_t gpa;
337 struct vm_guest_paging paging;
338 struct vie vie;
339 } inst_emul;
340
341 /*
342 * A SMCCC call, e.g. starting a core via PSCI.
343 * Further arguments can be read by asking the kernel for
344 * all register values.
345 */
346 struct {
347 uint64_t func_id;
348 uint64_t args[7];
349 } smccc_call;
350
351 struct {
352 enum vm_suspend_how how;
353 } suspended;
354 } u;
355 };
356
357 #endif /* _VMM_H_ */
358