2 * Copyright (C) 2015 Mihai Carabas <mihai.carabas@gmail.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/param.h>
31 #include <sys/cpuset.h>
49 * Identifiers for architecturally defined registers.
87 VM_REG_GUEST_SCTLR_EL1
,
88 VM_REG_GUEST_TTBR0_EL1
,
89 VM_REG_GUEST_TTBR1_EL1
,
91 VM_REG_GUEST_TCR2_EL1
,
95 #define VM_INTINFO_VECTOR(info) ((info) & 0xff)
96 #define VM_INTINFO_DEL_ERRCODE 0x800
97 #define VM_INTINFO_RSVD 0x7ffff000
98 #define VM_INTINFO_VALID 0x80000000
99 #define VM_INTINFO_TYPE 0x700
100 #define VM_INTINFO_HWINTR (0 << 8)
101 #define VM_INTINFO_NMI (2 << 8)
102 #define VM_INTINFO_HWEXCEPTION (3 << 8)
103 #define VM_INTINFO_SWINTR (4 << 8)
105 #define VM_GUEST_BASE_IPA 0x80000000UL /* Guest kernel start ipa */
108 * The VM name has to fit into the pathname length constraints of devfs,
109 * governed primarily by SPECNAMELEN. The length is the total number of
110 * characters in the full path, relative to the mount point and not
111 * including any leading '/' characters.
112 * A prefix and a suffix are added to the name specified by the user.
113 * The prefix is usually "vmm/" or "vmm.io/", but can be a few characters
114 * longer for future use.
115 * The suffix is a string that identifies a bootrom image or some similar
116 * image that is attached to the VM. A separator character gets added to
117 * the suffix automatically when generating the full path, so it must be
118 * accounted for, reducing the effective length by 1.
119 * The effective length of a VM name is 229 bytes for FreeBSD 13 and 37
120 * bytes for FreeBSD 12. A minimum length is set for safety and supports
121 * a SPECNAMELEN as small as 32 on old systems.
123 #define VM_MAX_PREFIXLEN 10
124 #define VM_MAX_SUFFIXLEN 15
125 #define VM_MAX_NAMELEN \
126 (SPECNAMELEN - VM_MAX_PREFIXLEN - VM_MAX_SUFFIXLEN - 1)
134 struct vm_guest_paging
;
135 struct vm_vgic_descr
;
138 struct vm_eventinfo
{
139 void *rptr
; /* rendezvous cookie */
140 int *sptr
; /* suspend cookie */
141 int *iptr
; /* reqidle cookie */
144 int vm_create(const char *name
, struct vm
**retvm
);
145 struct vcpu
*vm_alloc_vcpu(struct vm
*vm
, int vcpuid
);
146 void vm_disable_vcpu_creation(struct vm
*vm
);
147 void vm_slock_vcpus(struct vm
*vm
);
148 void vm_unlock_vcpus(struct vm
*vm
);
149 void vm_destroy(struct vm
*vm
);
150 int vm_reinit(struct vm
*vm
);
151 const char *vm_name(struct vm
*vm
);
154 * APIs that modify the guest memory map require all vcpus to be frozen.
156 void vm_slock_memsegs(struct vm
*vm
);
157 void vm_xlock_memsegs(struct vm
*vm
);
158 void vm_unlock_memsegs(struct vm
*vm
);
159 int vm_mmap_memseg(struct vm
*vm
, vm_paddr_t gpa
, int segid
, vm_ooffset_t off
,
160 size_t len
, int prot
, int flags
);
161 int vm_munmap_memseg(struct vm
*vm
, vm_paddr_t gpa
, size_t len
);
162 int vm_alloc_memseg(struct vm
*vm
, int ident
, size_t len
, bool sysmem
);
163 void vm_free_memseg(struct vm
*vm
, int ident
);
166 * APIs that inspect the guest memory map require only a *single* vcpu to
167 * be frozen. This acts like a read lock on the guest memory map since any
168 * modification requires *all* vcpus to be frozen.
170 int vm_mmap_getnext(struct vm
*vm
, vm_paddr_t
*gpa
, int *segid
,
171 vm_ooffset_t
*segoff
, size_t *len
, int *prot
, int *flags
);
172 int vm_get_memseg(struct vm
*vm
, int ident
, size_t *len
, bool *sysmem
,
173 struct vm_object
**objptr
);
174 vm_paddr_t
vmm_sysmem_maxaddr(struct vm
*vm
);
175 void *vm_gpa_hold(struct vcpu
*vcpu
, vm_paddr_t gpa
, size_t len
,
176 int prot
, void **cookie
);
177 void *vm_gpa_hold_global(struct vm
*vm
, vm_paddr_t gpa
, size_t len
,
178 int prot
, void **cookie
);
179 void vm_gpa_release(void *cookie
);
180 bool vm_mem_allocated(struct vcpu
*vcpu
, vm_paddr_t gpa
);
182 int vm_gla2gpa_nofault(struct vcpu
*vcpu
, struct vm_guest_paging
*paging
,
183 uint64_t gla
, int prot
, uint64_t *gpa
, int *is_fault
);
185 uint16_t vm_get_maxcpus(struct vm
*vm
);
186 void vm_get_topology(struct vm
*vm
, uint16_t *sockets
, uint16_t *cores
,
187 uint16_t *threads
, uint16_t *maxcpus
);
188 int vm_set_topology(struct vm
*vm
, uint16_t sockets
, uint16_t cores
,
189 uint16_t threads
, uint16_t maxcpus
);
190 int vm_get_register(struct vcpu
*vcpu
, int reg
, uint64_t *retval
);
191 int vm_set_register(struct vcpu
*vcpu
, int reg
, uint64_t val
);
192 int vm_run(struct vcpu
*vcpu
);
193 int vm_suspend(struct vm
*vm
, enum vm_suspend_how how
);
194 void* vm_get_cookie(struct vm
*vm
);
195 int vcpu_vcpuid(struct vcpu
*vcpu
);
196 void *vcpu_get_cookie(struct vcpu
*vcpu
);
197 struct vm
*vcpu_vm(struct vcpu
*vcpu
);
198 struct vcpu
*vm_vcpu(struct vm
*vm
, int cpu
);
199 int vm_get_capability(struct vcpu
*vcpu
, int type
, int *val
);
200 int vm_set_capability(struct vcpu
*vcpu
, int type
, int val
);
201 int vm_activate_cpu(struct vcpu
*vcpu
);
202 int vm_suspend_cpu(struct vm
*vm
, struct vcpu
*vcpu
);
203 int vm_resume_cpu(struct vm
*vm
, struct vcpu
*vcpu
);
204 int vm_inject_exception(struct vcpu
*vcpu
, uint64_t esr
, uint64_t far
);
205 int vm_attach_vgic(struct vm
*vm
, struct vm_vgic_descr
*descr
);
206 int vm_assert_irq(struct vm
*vm
, uint32_t irq
);
207 int vm_deassert_irq(struct vm
*vm
, uint32_t irq
);
208 int vm_raise_msi(struct vm
*vm
, uint64_t msg
, uint64_t addr
, int bus
, int slot
,
210 struct vm_exit
*vm_exitinfo(struct vcpu
*vcpu
);
211 void vm_exit_suspended(struct vcpu
*vcpu
, uint64_t pc
);
212 void vm_exit_debug(struct vcpu
*vcpu
, uint64_t pc
);
213 void vm_exit_rendezvous(struct vcpu
*vcpu
, uint64_t pc
);
214 void vm_exit_astpending(struct vcpu
*vcpu
, uint64_t pc
);
216 cpuset_t
vm_active_cpus(struct vm
*vm
);
217 cpuset_t
vm_debug_cpus(struct vm
*vm
);
218 cpuset_t
vm_suspended_cpus(struct vm
*vm
);
221 vcpu_rendezvous_pending(struct vm_eventinfo
*info
)
224 return (*((uintptr_t *)(info
->rptr
)) != 0);
228 vcpu_suspended(struct vm_eventinfo
*info
)
231 return (*info
->sptr
);
234 int vcpu_debugged(struct vcpu
*vcpu
);
243 int vcpu_set_state(struct vcpu
*vcpu
, enum vcpu_state state
, bool from_idle
);
244 enum vcpu_state
vcpu_get_state(struct vcpu
*vcpu
, int *hostcpu
);
247 vcpu_is_running(struct vcpu
*vcpu
, int *hostcpu
)
249 return (vcpu_get_state(vcpu
, hostcpu
) == VCPU_RUNNING
);
254 vcpu_should_yield(struct vcpu
*vcpu
)
259 return (td
->td_ast
!= 0 || td
->td_owepreempt
!= 0);
263 void *vcpu_stats(struct vcpu
*vcpu
);
264 void vcpu_notify_event(struct vcpu
*vcpu
);
266 enum vm_reg_name
vm_segment_name(int seg_encoding
);
277 #define VM_DIR_READ 0
278 #define VM_DIR_WRITE 1
280 #define VM_GP_M_MASK 0x1f
281 #define VM_GP_MMU_ENABLED (1 << 5)
283 struct vm_guest_paging
{
293 uint8_t access_size
:4, sign_extend
:1, dir
:1, unused
:2;
294 enum vm_reg_name reg
;
298 uint32_t inst_syndrome
;
299 uint8_t dir
:1, unused
:7;
300 enum vm_reg_name reg
;
304 * Identifiers for optional vmm capabilities
309 VM_CAP_UNRESTRICTED_GUEST
,
318 VM_EXITCODE_INST_EMUL
,
319 VM_EXITCODE_REG_EMUL
,
321 VM_EXITCODE_SUSPENDED
,
333 enum vm_exitcode exitcode
;
338 * ARM specific payload.
341 uint32_t exception_nr
;
343 uint64_t esr_el2
; /* Exception Syndrome Register */
344 uint64_t far_el2
; /* Fault Address Register */
345 uint64_t hpfar_el2
; /* Hypervisor IPA Fault Address Register */
356 struct vm_guest_paging paging
;
361 * A SMCCC call, e.g. starting a core via PSCI.
362 * Further arguments can be read by asking the kernel for
363 * all register values.
371 enum vm_suspend_how how
;