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