1 /* 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2015 Mihai Carabas <mihai.carabas@gmail.com> 5 * Copyright (c) 2024 Ruslan Bukin <br@bsdpad.com> 6 * 7 * This software was developed by the University of Cambridge Computer 8 * Laboratory (Department of Computer Science and Technology) under Innovate 9 * UK project 105694, "Digital Security by Design (DSbD) Technology Platform 10 * Prototype". 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef _VMM_DEV_H_ 35 #define _VMM_DEV_H_ 36 37 #include <machine/vmm.h> 38 39 struct vm_memmap { 40 vm_paddr_t gpa; 41 int segid; /* memory segment */ 42 vm_ooffset_t segoff; /* offset into memory segment */ 43 size_t len; /* mmap length */ 44 int prot; /* RWX */ 45 int flags; 46 }; 47 #define VM_MEMMAP_F_WIRED 0x01 48 49 struct vm_munmap { 50 vm_paddr_t gpa; 51 size_t len; 52 }; 53 54 #define VM_MEMSEG_NAME(m) ((m)->name[0] != '\0' ? (m)->name : NULL) 55 struct vm_memseg { 56 int segid; 57 size_t len; 58 char name[VM_MAX_SUFFIXLEN + 1]; 59 }; 60 61 struct vm_register { 62 int cpuid; 63 int regnum; /* enum vm_reg_name */ 64 uint64_t regval; 65 }; 66 67 struct vm_register_set { 68 int cpuid; 69 unsigned int count; 70 const int *regnums; /* enum vm_reg_name */ 71 uint64_t *regvals; 72 }; 73 74 struct vm_run { 75 int cpuid; 76 cpuset_t *cpuset; /* CPU set storage */ 77 size_t cpusetsize; 78 struct vm_exit *vm_exit; 79 }; 80 81 struct vm_exception { 82 int cpuid; 83 uint64_t scause; 84 }; 85 86 struct vm_msi { 87 uint64_t msg; 88 uint64_t addr; 89 int bus; 90 int slot; 91 int func; 92 }; 93 94 struct vm_capability { 95 int cpuid; 96 enum vm_cap_type captype; 97 int capval; 98 int allcpus; 99 }; 100 101 #define MAX_VM_STATS 64 102 struct vm_stats { 103 int cpuid; /* in */ 104 int index; /* in */ 105 int num_entries; /* out */ 106 struct timeval tv; 107 uint64_t statbuf[MAX_VM_STATS]; 108 }; 109 struct vm_stat_desc { 110 int index; /* in */ 111 char desc[128]; /* out */ 112 }; 113 114 struct vm_suspend { 115 enum vm_suspend_how how; 116 }; 117 118 struct vm_gla2gpa { 119 int vcpuid; /* inputs */ 120 int prot; /* PROT_READ or PROT_WRITE */ 121 uint64_t gla; 122 struct vm_guest_paging paging; 123 int fault; /* outputs */ 124 uint64_t gpa; 125 }; 126 127 struct vm_activate_cpu { 128 int vcpuid; 129 }; 130 131 struct vm_cpuset { 132 int which; 133 int cpusetsize; 134 cpuset_t *cpus; 135 }; 136 #define VM_ACTIVE_CPUS 0 137 #define VM_SUSPENDED_CPUS 1 138 #define VM_DEBUG_CPUS 2 139 140 struct vm_aplic_descr { 141 uint64_t mem_start; 142 uint64_t mem_size; 143 }; 144 145 struct vm_irq { 146 uint32_t irq; 147 }; 148 149 struct vm_cpu_topology { 150 uint16_t sockets; 151 uint16_t cores; 152 uint16_t threads; 153 uint16_t maxcpus; 154 }; 155 156 enum { 157 /* general routines */ 158 IOCNUM_ABIVERS = 0, 159 IOCNUM_RUN = 1, 160 IOCNUM_SET_CAPABILITY = 2, 161 IOCNUM_GET_CAPABILITY = 3, 162 IOCNUM_SUSPEND = 4, 163 IOCNUM_REINIT = 5, 164 165 /* memory apis */ 166 IOCNUM_GET_GPA_PMAP = 12, 167 IOCNUM_GLA2GPA_NOFAULT = 13, 168 IOCNUM_ALLOC_MEMSEG = 14, 169 IOCNUM_GET_MEMSEG = 15, 170 IOCNUM_MMAP_MEMSEG = 16, 171 IOCNUM_MMAP_GETNEXT = 17, 172 IOCNUM_MUNMAP_MEMSEG = 18, 173 174 /* register/state accessors */ 175 IOCNUM_SET_REGISTER = 20, 176 IOCNUM_GET_REGISTER = 21, 177 IOCNUM_SET_REGISTER_SET = 24, 178 IOCNUM_GET_REGISTER_SET = 25, 179 180 /* statistics */ 181 IOCNUM_VM_STATS = 50, 182 IOCNUM_VM_STAT_DESC = 51, 183 184 /* CPU Topology */ 185 IOCNUM_SET_TOPOLOGY = 63, 186 IOCNUM_GET_TOPOLOGY = 64, 187 188 /* interrupt injection */ 189 IOCNUM_ASSERT_IRQ = 80, 190 IOCNUM_DEASSERT_IRQ = 81, 191 IOCNUM_RAISE_MSI = 82, 192 IOCNUM_INJECT_EXCEPTION = 83, 193 194 /* vm_cpuset */ 195 IOCNUM_ACTIVATE_CPU = 90, 196 IOCNUM_GET_CPUSET = 91, 197 IOCNUM_SUSPEND_CPU = 92, 198 IOCNUM_RESUME_CPU = 93, 199 200 /* vm_attach_aplic */ 201 IOCNUM_ATTACH_APLIC = 110, 202 }; 203 204 #define VM_RUN \ 205 _IOWR('v', IOCNUM_RUN, struct vm_run) 206 #define VM_SUSPEND \ 207 _IOW('v', IOCNUM_SUSPEND, struct vm_suspend) 208 #define VM_REINIT \ 209 _IO('v', IOCNUM_REINIT) 210 #define VM_ALLOC_MEMSEG \ 211 _IOW('v', IOCNUM_ALLOC_MEMSEG, struct vm_memseg) 212 #define VM_GET_MEMSEG \ 213 _IOWR('v', IOCNUM_GET_MEMSEG, struct vm_memseg) 214 #define VM_MMAP_MEMSEG \ 215 _IOW('v', IOCNUM_MMAP_MEMSEG, struct vm_memmap) 216 #define VM_MMAP_GETNEXT \ 217 _IOWR('v', IOCNUM_MMAP_GETNEXT, struct vm_memmap) 218 #define VM_MUNMAP_MEMSEG \ 219 _IOW('v', IOCNUM_MUNMAP_MEMSEG, struct vm_munmap) 220 #define VM_SET_REGISTER \ 221 _IOW('v', IOCNUM_SET_REGISTER, struct vm_register) 222 #define VM_GET_REGISTER \ 223 _IOWR('v', IOCNUM_GET_REGISTER, struct vm_register) 224 #define VM_SET_REGISTER_SET \ 225 _IOW('v', IOCNUM_SET_REGISTER_SET, struct vm_register_set) 226 #define VM_GET_REGISTER_SET \ 227 _IOWR('v', IOCNUM_GET_REGISTER_SET, struct vm_register_set) 228 #define VM_SET_CAPABILITY \ 229 _IOW('v', IOCNUM_SET_CAPABILITY, struct vm_capability) 230 #define VM_GET_CAPABILITY \ 231 _IOWR('v', IOCNUM_GET_CAPABILITY, struct vm_capability) 232 #define VM_STATS \ 233 _IOWR('v', IOCNUM_VM_STATS, struct vm_stats) 234 #define VM_STAT_DESC \ 235 _IOWR('v', IOCNUM_VM_STAT_DESC, struct vm_stat_desc) 236 #define VM_ASSERT_IRQ \ 237 _IOW('v', IOCNUM_ASSERT_IRQ, struct vm_irq) 238 #define VM_DEASSERT_IRQ \ 239 _IOW('v', IOCNUM_DEASSERT_IRQ, struct vm_irq) 240 #define VM_RAISE_MSI \ 241 _IOW('v', IOCNUM_RAISE_MSI, struct vm_msi) 242 #define VM_INJECT_EXCEPTION \ 243 _IOW('v', IOCNUM_INJECT_EXCEPTION, struct vm_exception) 244 #define VM_SET_TOPOLOGY \ 245 _IOW('v', IOCNUM_SET_TOPOLOGY, struct vm_cpu_topology) 246 #define VM_GET_TOPOLOGY \ 247 _IOR('v', IOCNUM_GET_TOPOLOGY, struct vm_cpu_topology) 248 #define VM_GLA2GPA_NOFAULT \ 249 _IOWR('v', IOCNUM_GLA2GPA_NOFAULT, struct vm_gla2gpa) 250 #define VM_ACTIVATE_CPU \ 251 _IOW('v', IOCNUM_ACTIVATE_CPU, struct vm_activate_cpu) 252 #define VM_GET_CPUS \ 253 _IOW('v', IOCNUM_GET_CPUSET, struct vm_cpuset) 254 #define VM_SUSPEND_CPU \ 255 _IOW('v', IOCNUM_SUSPEND_CPU, struct vm_activate_cpu) 256 #define VM_RESUME_CPU \ 257 _IOW('v', IOCNUM_RESUME_CPU, struct vm_activate_cpu) 258 #define VM_ATTACH_APLIC \ 259 _IOW('v', IOCNUM_ATTACH_APLIC, struct vm_aplic_descr) 260 #endif 261