1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2011 NetApp, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 /* 31 * This file and its contents are supplied under the terms of the 32 * Common Development and Distribution License ("CDDL"), version 1.0. 33 * You may only use this file in accordance with the terms of version 34 * 1.0 of the CDDL. 35 * 36 * A full copy of the text of the CDDL should have accompanied this 37 * source. A copy of the CDDL is also available via the Internet at 38 * http://www.illumos.org/license/CDDL. 39 * 40 * Copyright 2015 Pluribus Networks Inc. 41 * Copyright 2019 Joyent, Inc. 42 * Copyright 2021 Oxide Computer Company 43 */ 44 45 #ifndef _VMM_DEV_H_ 46 #define _VMM_DEV_H_ 47 48 #include <machine/vmm.h> 49 50 #include <sys/param.h> 51 #include <sys/cpuset.h> 52 53 struct vm_create_req { 54 char name[VM_MAX_NAMELEN]; 55 uint64_t flags; 56 }; 57 58 59 struct vm_destroy_req { 60 char name[VM_MAX_NAMELEN]; 61 }; 62 63 struct vm_memmap { 64 vm_paddr_t gpa; 65 int segid; /* memory segment */ 66 vm_ooffset_t segoff; /* offset into memory segment */ 67 size_t len; /* mmap length */ 68 int prot; /* RWX */ 69 int flags; 70 }; 71 #define VM_MEMMAP_F_WIRED 0x01 72 #define VM_MEMMAP_F_IOMMU 0x02 73 74 struct vm_munmap { 75 vm_paddr_t gpa; 76 size_t len; 77 }; 78 79 #define VM_MEMSEG_NAME(m) ((m)->name[0] != '\0' ? (m)->name : NULL) 80 struct vm_memseg { 81 int segid; 82 size_t len; 83 char name[VM_MAX_SEG_NAMELEN]; 84 }; 85 86 struct vm_register { 87 int cpuid; 88 int regnum; /* enum vm_reg_name */ 89 uint64_t regval; 90 }; 91 92 struct vm_seg_desc { /* data or code segment */ 93 int cpuid; 94 int regnum; /* enum vm_reg_name */ 95 struct seg_desc desc; 96 }; 97 98 struct vm_register_set { 99 int cpuid; 100 unsigned int count; 101 const int *regnums; /* enum vm_reg_name */ 102 uint64_t *regvals; 103 }; 104 105 struct vm_exception { 106 int cpuid; 107 int vector; 108 uint32_t error_code; 109 int error_code_valid; 110 int restart_instruction; 111 }; 112 113 struct vm_lapic_msi { 114 uint64_t msg; 115 uint64_t addr; 116 }; 117 118 struct vm_lapic_irq { 119 int cpuid; 120 int vector; 121 }; 122 123 struct vm_ioapic_irq { 124 int irq; 125 }; 126 127 struct vm_isa_irq { 128 int atpic_irq; 129 int ioapic_irq; 130 }; 131 132 struct vm_isa_irq_trigger { 133 int atpic_irq; 134 enum vm_intr_trigger trigger; 135 }; 136 137 struct vm_capability { 138 int cpuid; 139 enum vm_cap_type captype; 140 int capval; 141 int allcpus; 142 }; 143 144 struct vm_pptdev { 145 int pptfd; 146 }; 147 148 struct vm_pptdev_mmio { 149 int pptfd; 150 vm_paddr_t gpa; 151 vm_paddr_t hpa; 152 size_t len; 153 }; 154 155 struct vm_pptdev_msi { 156 int vcpu; 157 int pptfd; 158 int numvec; /* 0 means disabled */ 159 uint64_t msg; 160 uint64_t addr; 161 }; 162 163 struct vm_pptdev_msix { 164 int vcpu; 165 int pptfd; 166 int idx; 167 uint64_t msg; 168 uint32_t vector_control; 169 uint64_t addr; 170 }; 171 172 struct vm_pptdev_limits { 173 int pptfd; 174 int msi_limit; 175 int msix_limit; 176 }; 177 178 struct vm_nmi { 179 int cpuid; 180 }; 181 182 #define MAX_VM_STATS (64 + VM_MAXCPU) 183 184 struct vm_stats { 185 int cpuid; /* in */ 186 int num_entries; /* out */ 187 struct timeval tv; 188 uint64_t statbuf[MAX_VM_STATS]; 189 }; 190 191 struct vm_stat_desc { 192 int index; /* in */ 193 char desc[128]; /* out */ 194 }; 195 196 struct vm_x2apic { 197 int cpuid; 198 enum x2apic_state state; 199 }; 200 201 struct vm_gpa_pte { 202 uint64_t gpa; /* in */ 203 uint64_t pte[4]; /* out */ 204 int ptenum; 205 }; 206 207 struct vm_hpet_cap { 208 uint32_t capabilities; /* lower 32 bits of HPET capabilities */ 209 }; 210 211 struct vm_suspend { 212 enum vm_suspend_how how; 213 }; 214 215 #define VM_REINIT_F_FORCE_SUSPEND (1 << 0) 216 217 struct vm_reinit { 218 uint64_t flags; 219 }; 220 221 struct vm_gla2gpa { 222 int vcpuid; /* inputs */ 223 int prot; /* PROT_READ or PROT_WRITE */ 224 uint64_t gla; 225 struct vm_guest_paging paging; 226 int fault; /* outputs */ 227 uint64_t gpa; 228 }; 229 230 struct vm_activate_cpu { 231 int vcpuid; 232 }; 233 234 struct vm_cpuset { 235 int which; 236 int cpusetsize; 237 #ifndef _KERNEL 238 cpuset_t *cpus; 239 #else 240 void *cpus; 241 #endif 242 }; 243 #define VM_ACTIVE_CPUS 0 244 #define VM_SUSPENDED_CPUS 1 245 #define VM_DEBUG_CPUS 2 246 247 struct vm_intinfo { 248 int vcpuid; 249 uint64_t info1; 250 uint64_t info2; 251 }; 252 253 struct vm_rtc_time { 254 time_t secs; 255 }; 256 257 struct vm_rtc_data { 258 int offset; 259 uint8_t value; 260 }; 261 262 struct vm_devmem_offset { 263 int segid; 264 off_t offset; 265 }; 266 267 struct vm_cpu_topology { 268 uint16_t sockets; 269 uint16_t cores; 270 uint16_t threads; 271 uint16_t maxcpus; 272 }; 273 274 struct vm_readwrite_kernemu_device { 275 int vcpuid; 276 unsigned access_width : 3; 277 unsigned _unused : 29; 278 uint64_t gpa; 279 uint64_t value; 280 }; 281 _Static_assert(sizeof(struct vm_readwrite_kernemu_device) == 24, "ABI"); 282 283 enum vcpu_reset_kind { 284 VRK_RESET = 0, 285 /* 286 * The reset performed by an INIT IPI clears much of the CPU state, but 287 * some portions are left untouched, unlike VRK_RESET, which represents 288 * a "full" reset as if the system was freshly powered on. 289 */ 290 VRK_INIT = 1, 291 }; 292 293 struct vm_vcpu_reset { 294 int vcpuid; 295 uint32_t kind; /* contains: enum vcpu_reset_kind */ 296 }; 297 298 struct vm_run_state { 299 int vcpuid; 300 uint32_t state; /* of enum cpu_init_status type */ 301 uint8_t sipi_vector; /* vector of SIPI, if any */ 302 uint8_t _pad[3]; 303 }; 304 305 /* Transfer data for VM_GET_FPU and VM_SET_FPU */ 306 struct vm_fpu_state { 307 int vcpuid; 308 void *buf; 309 size_t len; 310 }; 311 312 struct vm_fpu_desc_entry { 313 uint64_t vfde_feature; 314 uint32_t vfde_size; 315 uint32_t vfde_off; 316 }; 317 318 struct vm_fpu_desc { 319 struct vm_fpu_desc_entry *vfd_entry_data; 320 size_t vfd_req_size; 321 uint32_t vfd_num_entries; 322 }; 323 324 struct vmm_resv_query { 325 size_t vrq_free_sz; 326 size_t vrq_alloc_sz; 327 size_t vrq_alloc_transient_sz; 328 size_t vrq_limit; 329 }; 330 331 /* 332 * struct vmm_dirty_tracker is used for tracking dirty guest pages during 333 * e.g. live migration. 334 * 335 * - The `vdt_start_gpa` field specifies the offset from the beginning of 336 * guest physical memory to track; 337 * - `vdt_pfns` points to a bit vector indexed by guest PFN relative to the 338 * given start address. Each bit indicates whether the given guest page 339 * is dirty or not. 340 * - `vdt_pfns_len` specifies the length of the of the guest physical memory 341 * region in bytes. It also de facto bounds the range of guest addresses 342 * we will examine on any one `VM_TRACK_DIRTY_PAGES` ioctl(). If the 343 * range of the bit vector spans an unallocated region (or extends beyond 344 * the end of the guest physical address space) the corresponding bits in 345 * `vdt_pfns` will be zeroed. 346 */ 347 struct vmm_dirty_tracker { 348 uint64_t vdt_start_gpa; 349 size_t vdt_len; /* length of region */ 350 void *vdt_pfns; /* bit vector of dirty bits */ 351 }; 352 353 /* 354 * VMM Interface Version 355 * 356 * Despite the fact that the kernel interface to bhyve is explicitly considered 357 * Private, there are out-of-gate consumers which utilize it. While they assume 358 * the risk of any breakage incurred by changes to bhyve, we can at least try to 359 * make it easier to detect changes by exposing a "version" of the interface. 360 * It can also be used by the in-gate userland to detect if packaging updates 361 * somehow result in the userland and kernel falling out of sync. 362 * 363 * There are no established criteria for the magnitude of change which requires 364 * this version to be incremented, and maintenance of it is considered a 365 * best-effort activity. Nothing is to be inferred about the magnitude of a 366 * change when the version is modified. It follows no rules like semver. 367 */ 368 #define VMM_CURRENT_INTERFACE_VERSION 1 369 370 371 #define VMMCTL_IOC_BASE (('V' << 16) | ('M' << 8)) 372 #define VMM_IOC_BASE (('v' << 16) | ('m' << 8)) 373 #define VMM_LOCK_IOC_BASE (('v' << 16) | ('l' << 8)) 374 #define VMM_CPU_IOC_BASE (('v' << 16) | ('p' << 8)) 375 376 /* Operations performed on the vmmctl device */ 377 #define VMM_CREATE_VM (VMMCTL_IOC_BASE | 0x01) 378 #define VMM_DESTROY_VM (VMMCTL_IOC_BASE | 0x02) 379 #define VMM_VM_SUPPORTED (VMMCTL_IOC_BASE | 0x03) 380 #define VMM_INTERFACE_VERSION (VMMCTL_IOC_BASE | 0x04) 381 382 #define VMM_RESV_QUERY (VMMCTL_IOC_BASE | 0x10) 383 #define VMM_RESV_ADD (VMMCTL_IOC_BASE | 0x11) 384 #define VMM_RESV_REMOVE (VMMCTL_IOC_BASE | 0x12) 385 386 /* Operations performed in the context of a given vCPU */ 387 #define VM_RUN (VMM_CPU_IOC_BASE | 0x01) 388 #define VM_SET_REGISTER (VMM_CPU_IOC_BASE | 0x02) 389 #define VM_GET_REGISTER (VMM_CPU_IOC_BASE | 0x03) 390 #define VM_SET_SEGMENT_DESCRIPTOR (VMM_CPU_IOC_BASE | 0x04) 391 #define VM_GET_SEGMENT_DESCRIPTOR (VMM_CPU_IOC_BASE | 0x05) 392 #define VM_SET_REGISTER_SET (VMM_CPU_IOC_BASE | 0x06) 393 #define VM_GET_REGISTER_SET (VMM_CPU_IOC_BASE | 0x07) 394 #define VM_INJECT_EXCEPTION (VMM_CPU_IOC_BASE | 0x08) 395 #define VM_SET_CAPABILITY (VMM_CPU_IOC_BASE | 0x09) 396 #define VM_GET_CAPABILITY (VMM_CPU_IOC_BASE | 0x0a) 397 #define VM_PPTDEV_MSI (VMM_CPU_IOC_BASE | 0x0b) 398 #define VM_PPTDEV_MSIX (VMM_CPU_IOC_BASE | 0x0c) 399 #define VM_SET_X2APIC_STATE (VMM_CPU_IOC_BASE | 0x0d) 400 #define VM_GLA2GPA (VMM_CPU_IOC_BASE | 0x0e) 401 #define VM_GLA2GPA_NOFAULT (VMM_CPU_IOC_BASE | 0x0f) 402 #define VM_ACTIVATE_CPU (VMM_CPU_IOC_BASE | 0x10) 403 #define VM_SET_INTINFO (VMM_CPU_IOC_BASE | 0x11) 404 #define VM_GET_INTINFO (VMM_CPU_IOC_BASE | 0x12) 405 #define VM_RESTART_INSTRUCTION (VMM_CPU_IOC_BASE | 0x13) 406 #define VM_SET_KERNEMU_DEV (VMM_CPU_IOC_BASE | 0x14) 407 #define VM_GET_KERNEMU_DEV (VMM_CPU_IOC_BASE | 0x15) 408 #define VM_RESET_CPU (VMM_CPU_IOC_BASE | 0x16) 409 #define VM_GET_RUN_STATE (VMM_CPU_IOC_BASE | 0x17) 410 #define VM_SET_RUN_STATE (VMM_CPU_IOC_BASE | 0x18) 411 #define VM_GET_FPU (VMM_CPU_IOC_BASE | 0x19) 412 #define VM_SET_FPU (VMM_CPU_IOC_BASE | 0x1a) 413 414 /* Operations requiring write-locking the VM */ 415 #define VM_REINIT (VMM_LOCK_IOC_BASE | 0x01) 416 #define VM_BIND_PPTDEV (VMM_LOCK_IOC_BASE | 0x02) 417 #define VM_UNBIND_PPTDEV (VMM_LOCK_IOC_BASE | 0x03) 418 #define VM_MAP_PPTDEV_MMIO (VMM_LOCK_IOC_BASE | 0x04) 419 #define VM_ALLOC_MEMSEG (VMM_LOCK_IOC_BASE | 0x05) 420 #define VM_MMAP_MEMSEG (VMM_LOCK_IOC_BASE | 0x06) 421 #define VM_PMTMR_LOCATE (VMM_LOCK_IOC_BASE | 0x07) 422 #define VM_MUNMAP_MEMSEG (VMM_LOCK_IOC_BASE | 0x08) 423 #define VM_UNMAP_PPTDEV_MMIO (VMM_LOCK_IOC_BASE | 0x09) 424 425 #define VM_WRLOCK_CYCLE (VMM_LOCK_IOC_BASE | 0xff) 426 427 /* All other ioctls */ 428 #define VM_GET_GPA_PMAP (VMM_IOC_BASE | 0x01) 429 #define VM_GET_MEMSEG (VMM_IOC_BASE | 0x02) 430 #define VM_MMAP_GETNEXT (VMM_IOC_BASE | 0x03) 431 432 #define VM_LAPIC_IRQ (VMM_IOC_BASE | 0x04) 433 #define VM_LAPIC_LOCAL_IRQ (VMM_IOC_BASE | 0x05) 434 #define VM_LAPIC_MSI (VMM_IOC_BASE | 0x06) 435 436 #define VM_IOAPIC_ASSERT_IRQ (VMM_IOC_BASE | 0x07) 437 #define VM_IOAPIC_DEASSERT_IRQ (VMM_IOC_BASE | 0x08) 438 #define VM_IOAPIC_PULSE_IRQ (VMM_IOC_BASE | 0x09) 439 440 #define VM_ISA_ASSERT_IRQ (VMM_IOC_BASE | 0x0a) 441 #define VM_ISA_DEASSERT_IRQ (VMM_IOC_BASE | 0x0b) 442 #define VM_ISA_PULSE_IRQ (VMM_IOC_BASE | 0x0c) 443 #define VM_ISA_SET_IRQ_TRIGGER (VMM_IOC_BASE | 0x0d) 444 445 #define VM_RTC_WRITE (VMM_IOC_BASE | 0x0e) 446 #define VM_RTC_READ (VMM_IOC_BASE | 0x0f) 447 #define VM_RTC_SETTIME (VMM_IOC_BASE | 0x10) 448 #define VM_RTC_GETTIME (VMM_IOC_BASE | 0x11) 449 450 #define VM_SUSPEND (VMM_IOC_BASE | 0x12) 451 452 #define VM_IOAPIC_PINCOUNT (VMM_IOC_BASE | 0x13) 453 #define VM_GET_PPTDEV_LIMITS (VMM_IOC_BASE | 0x14) 454 #define VM_GET_HPET_CAPABILITIES (VMM_IOC_BASE | 0x15) 455 456 #define VM_STATS_IOC (VMM_IOC_BASE | 0x16) 457 #define VM_STAT_DESC (VMM_IOC_BASE | 0x17) 458 459 #define VM_INJECT_NMI (VMM_IOC_BASE | 0x18) 460 #define VM_GET_X2APIC_STATE (VMM_IOC_BASE | 0x19) 461 #define VM_SET_TOPOLOGY (VMM_IOC_BASE | 0x1a) 462 #define VM_GET_TOPOLOGY (VMM_IOC_BASE | 0x1b) 463 #define VM_GET_CPUS (VMM_IOC_BASE | 0x1c) 464 #define VM_SUSPEND_CPU (VMM_IOC_BASE | 0x1d) 465 #define VM_RESUME_CPU (VMM_IOC_BASE | 0x1e) 466 467 #define VM_PPTDEV_DISABLE_MSIX (VMM_IOC_BASE | 0x1f) 468 469 /* Note: forces a barrier on a flush operation before returning. */ 470 #define VM_TRACK_DIRTY_PAGES (VMM_IOC_BASE | 0x20) 471 #define VM_DESC_FPU_AREA (VMM_IOC_BASE | 0x21) 472 473 #define VM_DEVMEM_GETOFFSET (VMM_IOC_BASE | 0xff) 474 475 #define VMM_CTL_DEV "/dev/vmmctl" 476 477 #endif 478