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 #include <machine/vmm_instruction_emul.h> 111 112 #define VMM_VCPU_MD_FIELDS \ 113 struct vm_exit exitinfo; \ 114 uint64_t nextpc; /* (x) next instruction to execute */ \ 115 struct vfpstate *guestfpu /* (a,i) guest fpu state */ 116 117 #define VMM_VM_MD_FIELDS \ 118 struct vmm_mmio_region mmio_region[VM_MAX_MMIO_REGIONS]; \ 119 struct vmm_special_reg special_reg[VM_MAX_SPECIAL_REGS] 120 121 struct vmm_mmio_region { 122 uint64_t start; 123 uint64_t end; 124 mem_region_read_t read; 125 mem_region_write_t write; 126 }; 127 #define VM_MAX_MMIO_REGIONS 4 128 129 struct vmm_special_reg { 130 uint32_t esr_iss; 131 uint32_t esr_mask; 132 reg_read_t reg_read; 133 reg_write_t reg_write; 134 void *arg; 135 }; 136 #define VM_MAX_SPECIAL_REGS 16 137 138 #define DECLARE_VMMOPS_FUNC(ret_type, opname, args) \ 139 ret_type vmmops_##opname args 140 141 struct vm; 142 struct vm_eventinfo; 143 struct vm_exception; 144 struct vm_exit; 145 struct vm_run; 146 struct vm_object; 147 struct vm_guest_paging; 148 struct vm_vgic_descr; 149 struct pmap; 150 151 DECLARE_VMMOPS_FUNC(int, modinit, (int ipinum)); 152 DECLARE_VMMOPS_FUNC(int, modcleanup, (void)); 153 DECLARE_VMMOPS_FUNC(void *, init, (struct vm *vm, struct pmap *pmap)); 154 DECLARE_VMMOPS_FUNC(int, gla2gpa, (void *vcpui, struct vm_guest_paging *paging, 155 uint64_t gla, int prot, uint64_t *gpa, int *is_fault)); 156 DECLARE_VMMOPS_FUNC(int, run, (void *vcpui, register_t pc, struct pmap *pmap, 157 struct vm_eventinfo *info)); 158 DECLARE_VMMOPS_FUNC(void, cleanup, (void *vmi)); 159 DECLARE_VMMOPS_FUNC(void *, vcpu_init, (void *vmi, struct vcpu *vcpu, 160 int vcpu_id)); 161 DECLARE_VMMOPS_FUNC(void, vcpu_cleanup, (void *vcpui)); 162 DECLARE_VMMOPS_FUNC(int, exception, (void *vcpui, uint64_t esr, uint64_t far)); 163 DECLARE_VMMOPS_FUNC(int, getreg, (void *vcpui, int num, uint64_t *retval)); 164 DECLARE_VMMOPS_FUNC(int, setreg, (void *vcpui, int num, uint64_t val)); 165 DECLARE_VMMOPS_FUNC(int, getcap, (void *vcpui, int num, int *retval)); 166 DECLARE_VMMOPS_FUNC(int, setcap, (void *vcpui, int num, int val)); 167 DECLARE_VMMOPS_FUNC(struct vmspace *, vmspace_alloc, (vm_offset_t min, 168 vm_offset_t max)); 169 DECLARE_VMMOPS_FUNC(void, vmspace_free, (struct vmspace *vmspace)); 170 #ifdef notyet 171 #ifdef BHYVE_SNAPSHOT 172 DECLARE_VMMOPS_FUNC(int, snapshot, (void *vmi, struct vm_snapshot_meta *meta)); 173 DECLARE_VMMOPS_FUNC(int, vcpu_snapshot, (void *vcpui, 174 struct vm_snapshot_meta *meta)); 175 DECLARE_VMMOPS_FUNC(int, restore_tsc, (void *vcpui, uint64_t now)); 176 #endif 177 #endif 178 179 int vm_get_register(struct vcpu *vcpu, int reg, uint64_t *retval); 180 int vm_set_register(struct vcpu *vcpu, int reg, uint64_t val); 181 int vm_run(struct vcpu *vcpu); 182 void* vm_get_cookie(struct vm *vm); 183 void *vcpu_get_cookie(struct vcpu *vcpu); 184 int vm_get_capability(struct vcpu *vcpu, int type, int *val); 185 int vm_set_capability(struct vcpu *vcpu, int type, int val); 186 int vm_inject_exception(struct vcpu *vcpu, uint64_t esr, uint64_t far); 187 int vm_attach_vgic(struct vm *vm, struct vm_vgic_descr *descr); 188 int vm_assert_irq(struct vm *vm, uint32_t irq); 189 int vm_deassert_irq(struct vm *vm, uint32_t irq); 190 int vm_raise_msi(struct vm *vm, uint64_t msg, uint64_t addr, int bus, int slot, 191 int func); 192 struct vm_exit *vm_exitinfo(struct vcpu *vcpu); 193 void vm_exit_suspended(struct vcpu *vcpu, uint64_t pc); 194 void vm_exit_debug(struct vcpu *vcpu, uint64_t pc); 195 void vm_exit_astpending(struct vcpu *vcpu, uint64_t pc); 196 197 struct vm_copyinfo { 198 uint64_t gpa; 199 size_t len; 200 void *hva; 201 void *cookie; 202 }; 203 204 #endif /* _KERNEL */ 205 206 #define VM_DIR_READ 0 207 #define VM_DIR_WRITE 1 208 209 #define VM_GP_M_MASK 0x1f 210 #define VM_GP_MMU_ENABLED (1 << 5) 211 212 struct vm_guest_paging { 213 uint64_t ttbr0_addr; 214 uint64_t ttbr1_addr; 215 uint64_t tcr_el1; 216 uint64_t tcr2_el1; 217 int flags; 218 int padding; 219 }; 220 221 struct vie { 222 uint8_t access_size:4, sign_extend:1, dir:1, unused:2; 223 enum vm_reg_name reg; 224 }; 225 226 struct vre { 227 uint32_t inst_syndrome; 228 uint8_t dir:1, unused:7; 229 enum vm_reg_name reg; 230 }; 231 232 /* 233 * Identifiers for optional vmm capabilities 234 */ 235 enum vm_cap_type { 236 VM_CAP_HALT_EXIT, 237 VM_CAP_PAUSE_EXIT, 238 VM_CAP_UNRESTRICTED_GUEST, 239 VM_CAP_BRK_EXIT, 240 VM_CAP_SS_EXIT, 241 VM_CAP_MASK_HWINTR, 242 VM_CAP_MAX 243 }; 244 245 enum vm_exitcode { 246 VM_EXITCODE_BOGUS, 247 VM_EXITCODE_INST_EMUL, 248 VM_EXITCODE_REG_EMUL, 249 VM_EXITCODE_HVC, 250 VM_EXITCODE_SUSPENDED, 251 VM_EXITCODE_HYP, 252 VM_EXITCODE_WFI, 253 VM_EXITCODE_PAGING, 254 VM_EXITCODE_SMCCC, 255 VM_EXITCODE_DEBUG, 256 VM_EXITCODE_BRK, 257 VM_EXITCODE_SS, 258 VM_EXITCODE_MAX 259 }; 260 261 struct vm_exit { 262 enum vm_exitcode exitcode; 263 int inst_length; 264 uint64_t pc; 265 union { 266 /* 267 * ARM specific payload. 268 */ 269 struct { 270 uint32_t exception_nr; 271 uint32_t pad; 272 uint64_t esr_el2; /* Exception Syndrome Register */ 273 uint64_t far_el2; /* Fault Address Register */ 274 uint64_t hpfar_el2; /* Hypervisor IPA Fault Address Register */ 275 } hyp; 276 struct { 277 struct vre vre; 278 } reg_emul; 279 struct { 280 uint64_t gpa; 281 uint64_t esr; 282 } paging; 283 struct { 284 uint64_t gpa; 285 struct vm_guest_paging paging; 286 struct vie vie; 287 } inst_emul; 288 289 /* 290 * A SMCCC call, e.g. starting a core via PSCI. 291 * Further arguments can be read by asking the kernel for 292 * all register values. 293 */ 294 struct { 295 uint64_t func_id; 296 uint64_t args[7]; 297 } smccc_call; 298 299 struct { 300 enum vm_suspend_how how; 301 } suspended; 302 } u; 303 }; 304 305 #endif /* _VMM_H_ */ 306