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 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/sysctl.h> 36 #include <sys/ioctl.h> 37 #include <sys/linker.h> 38 #include <sys/mman.h> 39 #include <sys/module.h> 40 #include <sys/_iovec.h> 41 #include <sys/cpuset.h> 42 43 #include <x86/segments.h> 44 #include <machine/specialreg.h> 45 46 #include <errno.h> 47 #include <stdbool.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <assert.h> 51 #include <string.h> 52 #include <fcntl.h> 53 #include <unistd.h> 54 55 #include <libutil.h> 56 57 #include <vm/vm.h> 58 #include <machine/vmm.h> 59 #include <machine/vmm_dev.h> 60 #include <machine/vmm_snapshot.h> 61 62 #include "vmmapi.h" 63 64 #define MB (1024 * 1024UL) 65 #define GB (1024 * 1024 * 1024UL) 66 67 /* 68 * Size of the guard region before and after the virtual address space 69 * mapping the guest physical memory. This must be a multiple of the 70 * superpage size for performance reasons. 71 */ 72 #define VM_MMAP_GUARD_SIZE (4 * MB) 73 74 #define PROT_RW (PROT_READ | PROT_WRITE) 75 #define PROT_ALL (PROT_READ | PROT_WRITE | PROT_EXEC) 76 77 struct vmctx { 78 int fd; 79 uint32_t lowmem_limit; 80 int memflags; 81 size_t lowmem; 82 size_t highmem; 83 char *baseaddr; 84 char *name; 85 }; 86 87 #define CREATE(x) sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x))) 88 #define DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x))) 89 90 static int 91 vm_device_open(const char *name) 92 { 93 int fd, len; 94 char *vmfile; 95 96 len = strlen("/dev/vmm/") + strlen(name) + 1; 97 vmfile = malloc(len); 98 assert(vmfile != NULL); 99 snprintf(vmfile, len, "/dev/vmm/%s", name); 100 101 /* Open the device file */ 102 fd = open(vmfile, O_RDWR, 0); 103 104 free(vmfile); 105 return (fd); 106 } 107 108 int 109 vm_create(const char *name) 110 { 111 /* Try to load vmm(4) module before creating a guest. */ 112 if (modfind("vmm") < 0) 113 kldload("vmm"); 114 return (CREATE((char *)name)); 115 } 116 117 struct vmctx * 118 vm_open(const char *name) 119 { 120 struct vmctx *vm; 121 122 vm = malloc(sizeof(struct vmctx) + strlen(name) + 1); 123 assert(vm != NULL); 124 125 vm->fd = -1; 126 vm->memflags = 0; 127 vm->lowmem_limit = 3 * GB; 128 vm->name = (char *)(vm + 1); 129 strcpy(vm->name, name); 130 131 if ((vm->fd = vm_device_open(vm->name)) < 0) 132 goto err; 133 134 return (vm); 135 err: 136 vm_destroy(vm); 137 return (NULL); 138 } 139 140 void 141 vm_destroy(struct vmctx *vm) 142 { 143 assert(vm != NULL); 144 145 if (vm->fd >= 0) 146 close(vm->fd); 147 DESTROY(vm->name); 148 149 free(vm); 150 } 151 152 int 153 vm_parse_memsize(const char *optarg, size_t *ret_memsize) 154 { 155 char *endptr; 156 size_t optval; 157 int error; 158 159 optval = strtoul(optarg, &endptr, 0); 160 if (*optarg != '\0' && *endptr == '\0') { 161 /* 162 * For the sake of backward compatibility if the memory size 163 * specified on the command line is less than a megabyte then 164 * it is interpreted as being in units of MB. 165 */ 166 if (optval < MB) 167 optval *= MB; 168 *ret_memsize = optval; 169 error = 0; 170 } else 171 error = expand_number(optarg, ret_memsize); 172 173 return (error); 174 } 175 176 uint32_t 177 vm_get_lowmem_limit(struct vmctx *ctx) 178 { 179 180 return (ctx->lowmem_limit); 181 } 182 183 void 184 vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit) 185 { 186 187 ctx->lowmem_limit = limit; 188 } 189 190 void 191 vm_set_memflags(struct vmctx *ctx, int flags) 192 { 193 194 ctx->memflags = flags; 195 } 196 197 int 198 vm_get_memflags(struct vmctx *ctx) 199 { 200 201 return (ctx->memflags); 202 } 203 204 /* 205 * Map segment 'segid' starting at 'off' into guest address range [gpa,gpa+len). 206 */ 207 int 208 vm_mmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, int segid, vm_ooffset_t off, 209 size_t len, int prot) 210 { 211 struct vm_memmap memmap; 212 int error, flags; 213 214 memmap.gpa = gpa; 215 memmap.segid = segid; 216 memmap.segoff = off; 217 memmap.len = len; 218 memmap.prot = prot; 219 memmap.flags = 0; 220 221 if (ctx->memflags & VM_MEM_F_WIRED) 222 memmap.flags |= VM_MEMMAP_F_WIRED; 223 224 /* 225 * If this mapping already exists then don't create it again. This 226 * is the common case for SYSMEM mappings created by bhyveload(8). 227 */ 228 error = vm_mmap_getnext(ctx, &gpa, &segid, &off, &len, &prot, &flags); 229 if (error == 0 && gpa == memmap.gpa) { 230 if (segid != memmap.segid || off != memmap.segoff || 231 prot != memmap.prot || flags != memmap.flags) { 232 errno = EEXIST; 233 return (-1); 234 } else { 235 return (0); 236 } 237 } 238 239 error = ioctl(ctx->fd, VM_MMAP_MEMSEG, &memmap); 240 return (error); 241 } 242 243 int 244 vm_get_guestmem_from_ctx(struct vmctx *ctx, char **guest_baseaddr, 245 size_t *lowmem_size, size_t *highmem_size) 246 { 247 248 *guest_baseaddr = ctx->baseaddr; 249 *lowmem_size = ctx->lowmem; 250 *highmem_size = ctx->highmem; 251 return (0); 252 } 253 254 int 255 vm_mmap_getnext(struct vmctx *ctx, vm_paddr_t *gpa, int *segid, 256 vm_ooffset_t *segoff, size_t *len, int *prot, int *flags) 257 { 258 struct vm_memmap memmap; 259 int error; 260 261 bzero(&memmap, sizeof(struct vm_memmap)); 262 memmap.gpa = *gpa; 263 error = ioctl(ctx->fd, VM_MMAP_GETNEXT, &memmap); 264 if (error == 0) { 265 *gpa = memmap.gpa; 266 *segid = memmap.segid; 267 *segoff = memmap.segoff; 268 *len = memmap.len; 269 *prot = memmap.prot; 270 *flags = memmap.flags; 271 } 272 return (error); 273 } 274 275 /* 276 * Return 0 if the segments are identical and non-zero otherwise. 277 * 278 * This is slightly complicated by the fact that only device memory segments 279 * are named. 280 */ 281 static int 282 cmpseg(size_t len, const char *str, size_t len2, const char *str2) 283 { 284 285 if (len == len2) { 286 if ((!str && !str2) || (str && str2 && !strcmp(str, str2))) 287 return (0); 288 } 289 return (-1); 290 } 291 292 static int 293 vm_alloc_memseg(struct vmctx *ctx, int segid, size_t len, const char *name) 294 { 295 struct vm_memseg memseg; 296 size_t n; 297 int error; 298 299 /* 300 * If the memory segment has already been created then just return. 301 * This is the usual case for the SYSMEM segment created by userspace 302 * loaders like bhyveload(8). 303 */ 304 error = vm_get_memseg(ctx, segid, &memseg.len, memseg.name, 305 sizeof(memseg.name)); 306 if (error) 307 return (error); 308 309 if (memseg.len != 0) { 310 if (cmpseg(len, name, memseg.len, VM_MEMSEG_NAME(&memseg))) { 311 errno = EINVAL; 312 return (-1); 313 } else { 314 return (0); 315 } 316 } 317 318 bzero(&memseg, sizeof(struct vm_memseg)); 319 memseg.segid = segid; 320 memseg.len = len; 321 if (name != NULL) { 322 n = strlcpy(memseg.name, name, sizeof(memseg.name)); 323 if (n >= sizeof(memseg.name)) { 324 errno = ENAMETOOLONG; 325 return (-1); 326 } 327 } 328 329 error = ioctl(ctx->fd, VM_ALLOC_MEMSEG, &memseg); 330 return (error); 331 } 332 333 int 334 vm_get_memseg(struct vmctx *ctx, int segid, size_t *lenp, char *namebuf, 335 size_t bufsize) 336 { 337 struct vm_memseg memseg; 338 size_t n; 339 int error; 340 341 memseg.segid = segid; 342 error = ioctl(ctx->fd, VM_GET_MEMSEG, &memseg); 343 if (error == 0) { 344 *lenp = memseg.len; 345 n = strlcpy(namebuf, memseg.name, bufsize); 346 if (n >= bufsize) { 347 errno = ENAMETOOLONG; 348 error = -1; 349 } 350 } 351 return (error); 352 } 353 354 static int 355 setup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char *base) 356 { 357 char *ptr; 358 int error, flags; 359 360 /* Map 'len' bytes starting at 'gpa' in the guest address space */ 361 error = vm_mmap_memseg(ctx, gpa, VM_SYSMEM, gpa, len, PROT_ALL); 362 if (error) 363 return (error); 364 365 flags = MAP_SHARED | MAP_FIXED; 366 if ((ctx->memflags & VM_MEM_F_INCORE) == 0) 367 flags |= MAP_NOCORE; 368 369 /* mmap into the process address space on the host */ 370 ptr = mmap(base + gpa, len, PROT_RW, flags, ctx->fd, gpa); 371 if (ptr == MAP_FAILED) 372 return (-1); 373 374 return (0); 375 } 376 377 int 378 vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms) 379 { 380 size_t objsize, len; 381 vm_paddr_t gpa; 382 char *baseaddr, *ptr; 383 int error; 384 385 assert(vms == VM_MMAP_ALL); 386 387 /* 388 * If 'memsize' cannot fit entirely in the 'lowmem' segment then 389 * create another 'highmem' segment above 4GB for the remainder. 390 */ 391 if (memsize > ctx->lowmem_limit) { 392 ctx->lowmem = ctx->lowmem_limit; 393 ctx->highmem = memsize - ctx->lowmem_limit; 394 objsize = 4*GB + ctx->highmem; 395 } else { 396 ctx->lowmem = memsize; 397 ctx->highmem = 0; 398 objsize = ctx->lowmem; 399 } 400 401 error = vm_alloc_memseg(ctx, VM_SYSMEM, objsize, NULL); 402 if (error) 403 return (error); 404 405 /* 406 * Stake out a contiguous region covering the guest physical memory 407 * and the adjoining guard regions. 408 */ 409 len = VM_MMAP_GUARD_SIZE + objsize + VM_MMAP_GUARD_SIZE; 410 ptr = mmap(NULL, len, PROT_NONE, MAP_GUARD | MAP_ALIGNED_SUPER, -1, 0); 411 if (ptr == MAP_FAILED) 412 return (-1); 413 414 baseaddr = ptr + VM_MMAP_GUARD_SIZE; 415 if (ctx->highmem > 0) { 416 gpa = 4*GB; 417 len = ctx->highmem; 418 error = setup_memory_segment(ctx, gpa, len, baseaddr); 419 if (error) 420 return (error); 421 } 422 423 if (ctx->lowmem > 0) { 424 gpa = 0; 425 len = ctx->lowmem; 426 error = setup_memory_segment(ctx, gpa, len, baseaddr); 427 if (error) 428 return (error); 429 } 430 431 ctx->baseaddr = baseaddr; 432 433 return (0); 434 } 435 436 /* 437 * Returns a non-NULL pointer if [gaddr, gaddr+len) is entirely contained in 438 * the lowmem or highmem regions. 439 * 440 * In particular return NULL if [gaddr, gaddr+len) falls in guest MMIO region. 441 * The instruction emulation code depends on this behavior. 442 */ 443 void * 444 vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len) 445 { 446 447 if (ctx->lowmem > 0) { 448 if (gaddr < ctx->lowmem && len <= ctx->lowmem && 449 gaddr + len <= ctx->lowmem) 450 return (ctx->baseaddr + gaddr); 451 } 452 453 if (ctx->highmem > 0) { 454 if (gaddr >= 4*GB) { 455 if (gaddr < 4*GB + ctx->highmem && 456 len <= ctx->highmem && 457 gaddr + len <= 4*GB + ctx->highmem) 458 return (ctx->baseaddr + gaddr); 459 } 460 } 461 462 return (NULL); 463 } 464 465 vm_paddr_t 466 vm_rev_map_gpa(struct vmctx *ctx, void *addr) 467 { 468 vm_paddr_t offaddr; 469 470 offaddr = (char *)addr - ctx->baseaddr; 471 472 if (ctx->lowmem > 0) 473 if (offaddr >= 0 && offaddr <= ctx->lowmem) 474 return (offaddr); 475 476 if (ctx->highmem > 0) 477 if (offaddr >= 4*GB && offaddr < 4*GB + ctx->highmem) 478 return (offaddr); 479 480 return ((vm_paddr_t)-1); 481 } 482 483 /* TODO: maximum size for vmname */ 484 int 485 vm_get_name(struct vmctx *ctx, char *buf, size_t max_len) 486 { 487 488 if (strlcpy(buf, ctx->name, max_len) >= max_len) 489 return (EINVAL); 490 return (0); 491 } 492 493 size_t 494 vm_get_lowmem_size(struct vmctx *ctx) 495 { 496 497 return (ctx->lowmem); 498 } 499 500 size_t 501 vm_get_highmem_size(struct vmctx *ctx) 502 { 503 504 return (ctx->highmem); 505 } 506 507 void * 508 vm_create_devmem(struct vmctx *ctx, int segid, const char *name, size_t len) 509 { 510 char pathname[MAXPATHLEN]; 511 size_t len2; 512 char *base, *ptr; 513 int fd, error, flags; 514 515 fd = -1; 516 ptr = MAP_FAILED; 517 if (name == NULL || strlen(name) == 0) { 518 errno = EINVAL; 519 goto done; 520 } 521 522 error = vm_alloc_memseg(ctx, segid, len, name); 523 if (error) 524 goto done; 525 526 strlcpy(pathname, "/dev/vmm.io/", sizeof(pathname)); 527 strlcat(pathname, ctx->name, sizeof(pathname)); 528 strlcat(pathname, ".", sizeof(pathname)); 529 strlcat(pathname, name, sizeof(pathname)); 530 531 fd = open(pathname, O_RDWR); 532 if (fd < 0) 533 goto done; 534 535 /* 536 * Stake out a contiguous region covering the device memory and the 537 * adjoining guard regions. 538 */ 539 len2 = VM_MMAP_GUARD_SIZE + len + VM_MMAP_GUARD_SIZE; 540 base = mmap(NULL, len2, PROT_NONE, MAP_GUARD | MAP_ALIGNED_SUPER, -1, 541 0); 542 if (base == MAP_FAILED) 543 goto done; 544 545 flags = MAP_SHARED | MAP_FIXED; 546 if ((ctx->memflags & VM_MEM_F_INCORE) == 0) 547 flags |= MAP_NOCORE; 548 549 /* mmap the devmem region in the host address space */ 550 ptr = mmap(base + VM_MMAP_GUARD_SIZE, len, PROT_RW, flags, fd, 0); 551 done: 552 if (fd >= 0) 553 close(fd); 554 return (ptr); 555 } 556 557 int 558 vm_set_desc(struct vmctx *ctx, int vcpu, int reg, 559 uint64_t base, uint32_t limit, uint32_t access) 560 { 561 int error; 562 struct vm_seg_desc vmsegdesc; 563 564 bzero(&vmsegdesc, sizeof(vmsegdesc)); 565 vmsegdesc.cpuid = vcpu; 566 vmsegdesc.regnum = reg; 567 vmsegdesc.desc.base = base; 568 vmsegdesc.desc.limit = limit; 569 vmsegdesc.desc.access = access; 570 571 error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc); 572 return (error); 573 } 574 575 int 576 vm_get_desc(struct vmctx *ctx, int vcpu, int reg, 577 uint64_t *base, uint32_t *limit, uint32_t *access) 578 { 579 int error; 580 struct vm_seg_desc vmsegdesc; 581 582 bzero(&vmsegdesc, sizeof(vmsegdesc)); 583 vmsegdesc.cpuid = vcpu; 584 vmsegdesc.regnum = reg; 585 586 error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc); 587 if (error == 0) { 588 *base = vmsegdesc.desc.base; 589 *limit = vmsegdesc.desc.limit; 590 *access = vmsegdesc.desc.access; 591 } 592 return (error); 593 } 594 595 int 596 vm_get_seg_desc(struct vmctx *ctx, int vcpu, int reg, struct seg_desc *seg_desc) 597 { 598 int error; 599 600 error = vm_get_desc(ctx, vcpu, reg, &seg_desc->base, &seg_desc->limit, 601 &seg_desc->access); 602 return (error); 603 } 604 605 int 606 vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val) 607 { 608 int error; 609 struct vm_register vmreg; 610 611 bzero(&vmreg, sizeof(vmreg)); 612 vmreg.cpuid = vcpu; 613 vmreg.regnum = reg; 614 vmreg.regval = val; 615 616 error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg); 617 return (error); 618 } 619 620 int 621 vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val) 622 { 623 int error; 624 struct vm_register vmreg; 625 626 bzero(&vmreg, sizeof(vmreg)); 627 vmreg.cpuid = vcpu; 628 vmreg.regnum = reg; 629 630 error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg); 631 *ret_val = vmreg.regval; 632 return (error); 633 } 634 635 int 636 vm_set_register_set(struct vmctx *ctx, int vcpu, unsigned int count, 637 const int *regnums, uint64_t *regvals) 638 { 639 int error; 640 struct vm_register_set vmregset; 641 642 bzero(&vmregset, sizeof(vmregset)); 643 vmregset.cpuid = vcpu; 644 vmregset.count = count; 645 vmregset.regnums = regnums; 646 vmregset.regvals = regvals; 647 648 error = ioctl(ctx->fd, VM_SET_REGISTER_SET, &vmregset); 649 return (error); 650 } 651 652 int 653 vm_get_register_set(struct vmctx *ctx, int vcpu, unsigned int count, 654 const int *regnums, uint64_t *regvals) 655 { 656 int error; 657 struct vm_register_set vmregset; 658 659 bzero(&vmregset, sizeof(vmregset)); 660 vmregset.cpuid = vcpu; 661 vmregset.count = count; 662 vmregset.regnums = regnums; 663 vmregset.regvals = regvals; 664 665 error = ioctl(ctx->fd, VM_GET_REGISTER_SET, &vmregset); 666 return (error); 667 } 668 669 int 670 vm_run(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit) 671 { 672 int error; 673 struct vm_run vmrun; 674 675 bzero(&vmrun, sizeof(vmrun)); 676 vmrun.cpuid = vcpu; 677 678 error = ioctl(ctx->fd, VM_RUN, &vmrun); 679 bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit)); 680 return (error); 681 } 682 683 int 684 vm_suspend(struct vmctx *ctx, enum vm_suspend_how how) 685 { 686 struct vm_suspend vmsuspend; 687 688 bzero(&vmsuspend, sizeof(vmsuspend)); 689 vmsuspend.how = how; 690 return (ioctl(ctx->fd, VM_SUSPEND, &vmsuspend)); 691 } 692 693 int 694 vm_reinit(struct vmctx *ctx) 695 { 696 697 return (ioctl(ctx->fd, VM_REINIT, 0)); 698 } 699 700 int 701 vm_inject_exception(struct vmctx *ctx, int vcpu, int vector, int errcode_valid, 702 uint32_t errcode, int restart_instruction) 703 { 704 struct vm_exception exc; 705 706 exc.cpuid = vcpu; 707 exc.vector = vector; 708 exc.error_code = errcode; 709 exc.error_code_valid = errcode_valid; 710 exc.restart_instruction = restart_instruction; 711 712 return (ioctl(ctx->fd, VM_INJECT_EXCEPTION, &exc)); 713 } 714 715 int 716 vm_apicid2vcpu(struct vmctx *ctx, int apicid) 717 { 718 /* 719 * The apic id associated with the 'vcpu' has the same numerical value 720 * as the 'vcpu' itself. 721 */ 722 return (apicid); 723 } 724 725 int 726 vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector) 727 { 728 struct vm_lapic_irq vmirq; 729 730 bzero(&vmirq, sizeof(vmirq)); 731 vmirq.cpuid = vcpu; 732 vmirq.vector = vector; 733 734 return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq)); 735 } 736 737 int 738 vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector) 739 { 740 struct vm_lapic_irq vmirq; 741 742 bzero(&vmirq, sizeof(vmirq)); 743 vmirq.cpuid = vcpu; 744 vmirq.vector = vector; 745 746 return (ioctl(ctx->fd, VM_LAPIC_LOCAL_IRQ, &vmirq)); 747 } 748 749 int 750 vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg) 751 { 752 struct vm_lapic_msi vmmsi; 753 754 bzero(&vmmsi, sizeof(vmmsi)); 755 vmmsi.addr = addr; 756 vmmsi.msg = msg; 757 758 return (ioctl(ctx->fd, VM_LAPIC_MSI, &vmmsi)); 759 } 760 761 int 762 vm_ioapic_assert_irq(struct vmctx *ctx, int irq) 763 { 764 struct vm_ioapic_irq ioapic_irq; 765 766 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq)); 767 ioapic_irq.irq = irq; 768 769 return (ioctl(ctx->fd, VM_IOAPIC_ASSERT_IRQ, &ioapic_irq)); 770 } 771 772 int 773 vm_ioapic_deassert_irq(struct vmctx *ctx, int irq) 774 { 775 struct vm_ioapic_irq ioapic_irq; 776 777 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq)); 778 ioapic_irq.irq = irq; 779 780 return (ioctl(ctx->fd, VM_IOAPIC_DEASSERT_IRQ, &ioapic_irq)); 781 } 782 783 int 784 vm_ioapic_pulse_irq(struct vmctx *ctx, int irq) 785 { 786 struct vm_ioapic_irq ioapic_irq; 787 788 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq)); 789 ioapic_irq.irq = irq; 790 791 return (ioctl(ctx->fd, VM_IOAPIC_PULSE_IRQ, &ioapic_irq)); 792 } 793 794 int 795 vm_ioapic_pincount(struct vmctx *ctx, int *pincount) 796 { 797 798 return (ioctl(ctx->fd, VM_IOAPIC_PINCOUNT, pincount)); 799 } 800 801 int 802 vm_readwrite_kernemu_device(struct vmctx *ctx, int vcpu, vm_paddr_t gpa, 803 bool write, int size, uint64_t *value) 804 { 805 struct vm_readwrite_kernemu_device irp = { 806 .vcpuid = vcpu, 807 .access_width = fls(size) - 1, 808 .gpa = gpa, 809 .value = write ? *value : ~0ul, 810 }; 811 long cmd = (write ? VM_SET_KERNEMU_DEV : VM_GET_KERNEMU_DEV); 812 int rc; 813 814 rc = ioctl(ctx->fd, cmd, &irp); 815 if (rc == 0 && !write) 816 *value = irp.value; 817 return (rc); 818 } 819 820 int 821 vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq) 822 { 823 struct vm_isa_irq isa_irq; 824 825 bzero(&isa_irq, sizeof(struct vm_isa_irq)); 826 isa_irq.atpic_irq = atpic_irq; 827 isa_irq.ioapic_irq = ioapic_irq; 828 829 return (ioctl(ctx->fd, VM_ISA_ASSERT_IRQ, &isa_irq)); 830 } 831 832 int 833 vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq) 834 { 835 struct vm_isa_irq isa_irq; 836 837 bzero(&isa_irq, sizeof(struct vm_isa_irq)); 838 isa_irq.atpic_irq = atpic_irq; 839 isa_irq.ioapic_irq = ioapic_irq; 840 841 return (ioctl(ctx->fd, VM_ISA_DEASSERT_IRQ, &isa_irq)); 842 } 843 844 int 845 vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq) 846 { 847 struct vm_isa_irq isa_irq; 848 849 bzero(&isa_irq, sizeof(struct vm_isa_irq)); 850 isa_irq.atpic_irq = atpic_irq; 851 isa_irq.ioapic_irq = ioapic_irq; 852 853 return (ioctl(ctx->fd, VM_ISA_PULSE_IRQ, &isa_irq)); 854 } 855 856 int 857 vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq, 858 enum vm_intr_trigger trigger) 859 { 860 struct vm_isa_irq_trigger isa_irq_trigger; 861 862 bzero(&isa_irq_trigger, sizeof(struct vm_isa_irq_trigger)); 863 isa_irq_trigger.atpic_irq = atpic_irq; 864 isa_irq_trigger.trigger = trigger; 865 866 return (ioctl(ctx->fd, VM_ISA_SET_IRQ_TRIGGER, &isa_irq_trigger)); 867 } 868 869 int 870 vm_inject_nmi(struct vmctx *ctx, int vcpu) 871 { 872 struct vm_nmi vmnmi; 873 874 bzero(&vmnmi, sizeof(vmnmi)); 875 vmnmi.cpuid = vcpu; 876 877 return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi)); 878 } 879 880 static const char *capstrmap[] = { 881 [VM_CAP_HALT_EXIT] = "hlt_exit", 882 [VM_CAP_MTRAP_EXIT] = "mtrap_exit", 883 [VM_CAP_PAUSE_EXIT] = "pause_exit", 884 [VM_CAP_UNRESTRICTED_GUEST] = "unrestricted_guest", 885 [VM_CAP_ENABLE_INVPCID] = "enable_invpcid", 886 [VM_CAP_BPT_EXIT] = "bpt_exit", 887 }; 888 889 int 890 vm_capability_name2type(const char *capname) 891 { 892 int i; 893 894 for (i = 0; i < nitems(capstrmap); i++) { 895 if (strcmp(capstrmap[i], capname) == 0) 896 return (i); 897 } 898 899 return (-1); 900 } 901 902 const char * 903 vm_capability_type2name(int type) 904 { 905 if (type >= 0 && type < nitems(capstrmap)) 906 return (capstrmap[type]); 907 908 return (NULL); 909 } 910 911 int 912 vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, 913 int *retval) 914 { 915 int error; 916 struct vm_capability vmcap; 917 918 bzero(&vmcap, sizeof(vmcap)); 919 vmcap.cpuid = vcpu; 920 vmcap.captype = cap; 921 922 error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap); 923 *retval = vmcap.capval; 924 return (error); 925 } 926 927 int 928 vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val) 929 { 930 struct vm_capability vmcap; 931 932 bzero(&vmcap, sizeof(vmcap)); 933 vmcap.cpuid = vcpu; 934 vmcap.captype = cap; 935 vmcap.capval = val; 936 937 return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap)); 938 } 939 940 int 941 vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func) 942 { 943 struct vm_pptdev pptdev; 944 945 bzero(&pptdev, sizeof(pptdev)); 946 pptdev.bus = bus; 947 pptdev.slot = slot; 948 pptdev.func = func; 949 950 return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev)); 951 } 952 953 int 954 vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func) 955 { 956 struct vm_pptdev pptdev; 957 958 bzero(&pptdev, sizeof(pptdev)); 959 pptdev.bus = bus; 960 pptdev.slot = slot; 961 pptdev.func = func; 962 963 return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev)); 964 } 965 966 int 967 vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func, 968 vm_paddr_t gpa, size_t len, vm_paddr_t hpa) 969 { 970 struct vm_pptdev_mmio pptmmio; 971 972 bzero(&pptmmio, sizeof(pptmmio)); 973 pptmmio.bus = bus; 974 pptmmio.slot = slot; 975 pptmmio.func = func; 976 pptmmio.gpa = gpa; 977 pptmmio.len = len; 978 pptmmio.hpa = hpa; 979 980 return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio)); 981 } 982 983 int 984 vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func, 985 uint64_t addr, uint64_t msg, int numvec) 986 { 987 struct vm_pptdev_msi pptmsi; 988 989 bzero(&pptmsi, sizeof(pptmsi)); 990 pptmsi.vcpu = vcpu; 991 pptmsi.bus = bus; 992 pptmsi.slot = slot; 993 pptmsi.func = func; 994 pptmsi.msg = msg; 995 pptmsi.addr = addr; 996 pptmsi.numvec = numvec; 997 998 return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi)); 999 } 1000 1001 int 1002 vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func, 1003 int idx, uint64_t addr, uint64_t msg, uint32_t vector_control) 1004 { 1005 struct vm_pptdev_msix pptmsix; 1006 1007 bzero(&pptmsix, sizeof(pptmsix)); 1008 pptmsix.vcpu = vcpu; 1009 pptmsix.bus = bus; 1010 pptmsix.slot = slot; 1011 pptmsix.func = func; 1012 pptmsix.idx = idx; 1013 pptmsix.msg = msg; 1014 pptmsix.addr = addr; 1015 pptmsix.vector_control = vector_control; 1016 1017 return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix); 1018 } 1019 1020 int 1021 vm_disable_pptdev_msix(struct vmctx *ctx, int bus, int slot, int func) 1022 { 1023 struct vm_pptdev ppt; 1024 1025 bzero(&ppt, sizeof(ppt)); 1026 ppt.bus = bus; 1027 ppt.slot = slot; 1028 ppt.func = func; 1029 1030 return ioctl(ctx->fd, VM_PPTDEV_DISABLE_MSIX, &ppt); 1031 } 1032 1033 uint64_t * 1034 vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv, 1035 int *ret_entries) 1036 { 1037 int error; 1038 1039 static struct vm_stats vmstats; 1040 1041 vmstats.cpuid = vcpu; 1042 1043 error = ioctl(ctx->fd, VM_STATS, &vmstats); 1044 if (error == 0) { 1045 if (ret_entries) 1046 *ret_entries = vmstats.num_entries; 1047 if (ret_tv) 1048 *ret_tv = vmstats.tv; 1049 return (vmstats.statbuf); 1050 } else 1051 return (NULL); 1052 } 1053 1054 const char * 1055 vm_get_stat_desc(struct vmctx *ctx, int index) 1056 { 1057 static struct vm_stat_desc statdesc; 1058 1059 statdesc.index = index; 1060 if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0) 1061 return (statdesc.desc); 1062 else 1063 return (NULL); 1064 } 1065 1066 int 1067 vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state) 1068 { 1069 int error; 1070 struct vm_x2apic x2apic; 1071 1072 bzero(&x2apic, sizeof(x2apic)); 1073 x2apic.cpuid = vcpu; 1074 1075 error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic); 1076 *state = x2apic.state; 1077 return (error); 1078 } 1079 1080 int 1081 vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state) 1082 { 1083 int error; 1084 struct vm_x2apic x2apic; 1085 1086 bzero(&x2apic, sizeof(x2apic)); 1087 x2apic.cpuid = vcpu; 1088 x2apic.state = state; 1089 1090 error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic); 1091 1092 return (error); 1093 } 1094 1095 /* 1096 * From Intel Vol 3a: 1097 * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT 1098 */ 1099 int 1100 vcpu_reset(struct vmctx *vmctx, int vcpu) 1101 { 1102 int error; 1103 uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx; 1104 uint32_t desc_access, desc_limit; 1105 uint16_t sel; 1106 1107 zero = 0; 1108 1109 rflags = 0x2; 1110 error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags); 1111 if (error) 1112 goto done; 1113 1114 rip = 0xfff0; 1115 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0) 1116 goto done; 1117 1118 cr0 = CR0_NE; 1119 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0) 1120 goto done; 1121 1122 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0) 1123 goto done; 1124 1125 cr4 = 0; 1126 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0) 1127 goto done; 1128 1129 /* 1130 * CS: present, r/w, accessed, 16-bit, byte granularity, usable 1131 */ 1132 desc_base = 0xffff0000; 1133 desc_limit = 0xffff; 1134 desc_access = 0x0093; 1135 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS, 1136 desc_base, desc_limit, desc_access); 1137 if (error) 1138 goto done; 1139 1140 sel = 0xf000; 1141 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0) 1142 goto done; 1143 1144 /* 1145 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity 1146 */ 1147 desc_base = 0; 1148 desc_limit = 0xffff; 1149 desc_access = 0x0093; 1150 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS, 1151 desc_base, desc_limit, desc_access); 1152 if (error) 1153 goto done; 1154 1155 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS, 1156 desc_base, desc_limit, desc_access); 1157 if (error) 1158 goto done; 1159 1160 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES, 1161 desc_base, desc_limit, desc_access); 1162 if (error) 1163 goto done; 1164 1165 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS, 1166 desc_base, desc_limit, desc_access); 1167 if (error) 1168 goto done; 1169 1170 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS, 1171 desc_base, desc_limit, desc_access); 1172 if (error) 1173 goto done; 1174 1175 sel = 0; 1176 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0) 1177 goto done; 1178 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0) 1179 goto done; 1180 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0) 1181 goto done; 1182 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0) 1183 goto done; 1184 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0) 1185 goto done; 1186 1187 /* General purpose registers */ 1188 rdx = 0xf00; 1189 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0) 1190 goto done; 1191 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0) 1192 goto done; 1193 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0) 1194 goto done; 1195 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0) 1196 goto done; 1197 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0) 1198 goto done; 1199 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0) 1200 goto done; 1201 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0) 1202 goto done; 1203 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0) 1204 goto done; 1205 1206 /* GDTR, IDTR */ 1207 desc_base = 0; 1208 desc_limit = 0xffff; 1209 desc_access = 0; 1210 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR, 1211 desc_base, desc_limit, desc_access); 1212 if (error != 0) 1213 goto done; 1214 1215 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR, 1216 desc_base, desc_limit, desc_access); 1217 if (error != 0) 1218 goto done; 1219 1220 /* TR */ 1221 desc_base = 0; 1222 desc_limit = 0xffff; 1223 desc_access = 0x0000008b; 1224 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access); 1225 if (error) 1226 goto done; 1227 1228 sel = 0; 1229 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0) 1230 goto done; 1231 1232 /* LDTR */ 1233 desc_base = 0; 1234 desc_limit = 0xffff; 1235 desc_access = 0x00000082; 1236 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base, 1237 desc_limit, desc_access); 1238 if (error) 1239 goto done; 1240 1241 sel = 0; 1242 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0) 1243 goto done; 1244 1245 /* XXX cr2, debug registers */ 1246 1247 error = 0; 1248 done: 1249 return (error); 1250 } 1251 1252 int 1253 vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num) 1254 { 1255 int error, i; 1256 struct vm_gpa_pte gpapte; 1257 1258 bzero(&gpapte, sizeof(gpapte)); 1259 gpapte.gpa = gpa; 1260 1261 error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte); 1262 1263 if (error == 0) { 1264 *num = gpapte.ptenum; 1265 for (i = 0; i < gpapte.ptenum; i++) 1266 pte[i] = gpapte.pte[i]; 1267 } 1268 1269 return (error); 1270 } 1271 1272 int 1273 vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities) 1274 { 1275 int error; 1276 struct vm_hpet_cap cap; 1277 1278 bzero(&cap, sizeof(struct vm_hpet_cap)); 1279 error = ioctl(ctx->fd, VM_GET_HPET_CAPABILITIES, &cap); 1280 if (capabilities != NULL) 1281 *capabilities = cap.capabilities; 1282 return (error); 1283 } 1284 1285 int 1286 vm_gla2gpa(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging, 1287 uint64_t gla, int prot, uint64_t *gpa, int *fault) 1288 { 1289 struct vm_gla2gpa gg; 1290 int error; 1291 1292 bzero(&gg, sizeof(struct vm_gla2gpa)); 1293 gg.vcpuid = vcpu; 1294 gg.prot = prot; 1295 gg.gla = gla; 1296 gg.paging = *paging; 1297 1298 error = ioctl(ctx->fd, VM_GLA2GPA, &gg); 1299 if (error == 0) { 1300 *fault = gg.fault; 1301 *gpa = gg.gpa; 1302 } 1303 return (error); 1304 } 1305 1306 int 1307 vm_gla2gpa_nofault(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging, 1308 uint64_t gla, int prot, uint64_t *gpa, int *fault) 1309 { 1310 struct vm_gla2gpa gg; 1311 int error; 1312 1313 bzero(&gg, sizeof(struct vm_gla2gpa)); 1314 gg.vcpuid = vcpu; 1315 gg.prot = prot; 1316 gg.gla = gla; 1317 gg.paging = *paging; 1318 1319 error = ioctl(ctx->fd, VM_GLA2GPA_NOFAULT, &gg); 1320 if (error == 0) { 1321 *fault = gg.fault; 1322 *gpa = gg.gpa; 1323 } 1324 return (error); 1325 } 1326 1327 #ifndef min 1328 #define min(a,b) (((a) < (b)) ? (a) : (b)) 1329 #endif 1330 1331 int 1332 vm_copy_setup(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging, 1333 uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt, 1334 int *fault) 1335 { 1336 void *va; 1337 uint64_t gpa; 1338 int error, i, n, off; 1339 1340 for (i = 0; i < iovcnt; i++) { 1341 iov[i].iov_base = 0; 1342 iov[i].iov_len = 0; 1343 } 1344 1345 while (len) { 1346 assert(iovcnt > 0); 1347 error = vm_gla2gpa(ctx, vcpu, paging, gla, prot, &gpa, fault); 1348 if (error || *fault) 1349 return (error); 1350 1351 off = gpa & PAGE_MASK; 1352 n = min(len, PAGE_SIZE - off); 1353 1354 va = vm_map_gpa(ctx, gpa, n); 1355 if (va == NULL) 1356 return (EFAULT); 1357 1358 iov->iov_base = va; 1359 iov->iov_len = n; 1360 iov++; 1361 iovcnt--; 1362 1363 gla += n; 1364 len -= n; 1365 } 1366 return (0); 1367 } 1368 1369 void 1370 vm_copy_teardown(struct vmctx *ctx, int vcpu, struct iovec *iov, int iovcnt) 1371 { 1372 1373 return; 1374 } 1375 1376 void 1377 vm_copyin(struct vmctx *ctx, int vcpu, struct iovec *iov, void *vp, size_t len) 1378 { 1379 const char *src; 1380 char *dst; 1381 size_t n; 1382 1383 dst = vp; 1384 while (len) { 1385 assert(iov->iov_len); 1386 n = min(len, iov->iov_len); 1387 src = iov->iov_base; 1388 bcopy(src, dst, n); 1389 1390 iov++; 1391 dst += n; 1392 len -= n; 1393 } 1394 } 1395 1396 void 1397 vm_copyout(struct vmctx *ctx, int vcpu, const void *vp, struct iovec *iov, 1398 size_t len) 1399 { 1400 const char *src; 1401 char *dst; 1402 size_t n; 1403 1404 src = vp; 1405 while (len) { 1406 assert(iov->iov_len); 1407 n = min(len, iov->iov_len); 1408 dst = iov->iov_base; 1409 bcopy(src, dst, n); 1410 1411 iov++; 1412 src += n; 1413 len -= n; 1414 } 1415 } 1416 1417 static int 1418 vm_get_cpus(struct vmctx *ctx, int which, cpuset_t *cpus) 1419 { 1420 struct vm_cpuset vm_cpuset; 1421 int error; 1422 1423 bzero(&vm_cpuset, sizeof(struct vm_cpuset)); 1424 vm_cpuset.which = which; 1425 vm_cpuset.cpusetsize = sizeof(cpuset_t); 1426 vm_cpuset.cpus = cpus; 1427 1428 error = ioctl(ctx->fd, VM_GET_CPUS, &vm_cpuset); 1429 return (error); 1430 } 1431 1432 int 1433 vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus) 1434 { 1435 1436 return (vm_get_cpus(ctx, VM_ACTIVE_CPUS, cpus)); 1437 } 1438 1439 int 1440 vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus) 1441 { 1442 1443 return (vm_get_cpus(ctx, VM_SUSPENDED_CPUS, cpus)); 1444 } 1445 1446 int 1447 vm_debug_cpus(struct vmctx *ctx, cpuset_t *cpus) 1448 { 1449 1450 return (vm_get_cpus(ctx, VM_DEBUG_CPUS, cpus)); 1451 } 1452 1453 int 1454 vm_activate_cpu(struct vmctx *ctx, int vcpu) 1455 { 1456 struct vm_activate_cpu ac; 1457 int error; 1458 1459 bzero(&ac, sizeof(struct vm_activate_cpu)); 1460 ac.vcpuid = vcpu; 1461 error = ioctl(ctx->fd, VM_ACTIVATE_CPU, &ac); 1462 return (error); 1463 } 1464 1465 int 1466 vm_suspend_cpu(struct vmctx *ctx, int vcpu) 1467 { 1468 struct vm_activate_cpu ac; 1469 int error; 1470 1471 bzero(&ac, sizeof(struct vm_activate_cpu)); 1472 ac.vcpuid = vcpu; 1473 error = ioctl(ctx->fd, VM_SUSPEND_CPU, &ac); 1474 return (error); 1475 } 1476 1477 int 1478 vm_resume_cpu(struct vmctx *ctx, int vcpu) 1479 { 1480 struct vm_activate_cpu ac; 1481 int error; 1482 1483 bzero(&ac, sizeof(struct vm_activate_cpu)); 1484 ac.vcpuid = vcpu; 1485 error = ioctl(ctx->fd, VM_RESUME_CPU, &ac); 1486 return (error); 1487 } 1488 1489 int 1490 vm_get_intinfo(struct vmctx *ctx, int vcpu, uint64_t *info1, uint64_t *info2) 1491 { 1492 struct vm_intinfo vmii; 1493 int error; 1494 1495 bzero(&vmii, sizeof(struct vm_intinfo)); 1496 vmii.vcpuid = vcpu; 1497 error = ioctl(ctx->fd, VM_GET_INTINFO, &vmii); 1498 if (error == 0) { 1499 *info1 = vmii.info1; 1500 *info2 = vmii.info2; 1501 } 1502 return (error); 1503 } 1504 1505 int 1506 vm_set_intinfo(struct vmctx *ctx, int vcpu, uint64_t info1) 1507 { 1508 struct vm_intinfo vmii; 1509 int error; 1510 1511 bzero(&vmii, sizeof(struct vm_intinfo)); 1512 vmii.vcpuid = vcpu; 1513 vmii.info1 = info1; 1514 error = ioctl(ctx->fd, VM_SET_INTINFO, &vmii); 1515 return (error); 1516 } 1517 1518 int 1519 vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value) 1520 { 1521 struct vm_rtc_data rtcdata; 1522 int error; 1523 1524 bzero(&rtcdata, sizeof(struct vm_rtc_data)); 1525 rtcdata.offset = offset; 1526 rtcdata.value = value; 1527 error = ioctl(ctx->fd, VM_RTC_WRITE, &rtcdata); 1528 return (error); 1529 } 1530 1531 int 1532 vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval) 1533 { 1534 struct vm_rtc_data rtcdata; 1535 int error; 1536 1537 bzero(&rtcdata, sizeof(struct vm_rtc_data)); 1538 rtcdata.offset = offset; 1539 error = ioctl(ctx->fd, VM_RTC_READ, &rtcdata); 1540 if (error == 0) 1541 *retval = rtcdata.value; 1542 return (error); 1543 } 1544 1545 int 1546 vm_rtc_settime(struct vmctx *ctx, time_t secs) 1547 { 1548 struct vm_rtc_time rtctime; 1549 int error; 1550 1551 bzero(&rtctime, sizeof(struct vm_rtc_time)); 1552 rtctime.secs = secs; 1553 error = ioctl(ctx->fd, VM_RTC_SETTIME, &rtctime); 1554 return (error); 1555 } 1556 1557 int 1558 vm_rtc_gettime(struct vmctx *ctx, time_t *secs) 1559 { 1560 struct vm_rtc_time rtctime; 1561 int error; 1562 1563 bzero(&rtctime, sizeof(struct vm_rtc_time)); 1564 error = ioctl(ctx->fd, VM_RTC_GETTIME, &rtctime); 1565 if (error == 0) 1566 *secs = rtctime.secs; 1567 return (error); 1568 } 1569 1570 int 1571 vm_restart_instruction(void *arg, int vcpu) 1572 { 1573 struct vmctx *ctx = arg; 1574 1575 return (ioctl(ctx->fd, VM_RESTART_INSTRUCTION, &vcpu)); 1576 } 1577 1578 int 1579 vm_snapshot_req(struct vm_snapshot_meta *meta) 1580 { 1581 1582 if (ioctl(meta->ctx->fd, VM_SNAPSHOT_REQ, meta) == -1) { 1583 #ifdef SNAPSHOT_DEBUG 1584 fprintf(stderr, "%s: snapshot failed for %s: %d\r\n", 1585 __func__, meta->dev_name, errno); 1586 #endif 1587 return (-1); 1588 } 1589 return (0); 1590 } 1591 1592 int 1593 vm_restore_time(struct vmctx *ctx) 1594 { 1595 int dummy; 1596 1597 dummy = 0; 1598 return (ioctl(ctx->fd, VM_RESTORE_TIME, &dummy)); 1599 } 1600 1601 int 1602 vm_set_topology(struct vmctx *ctx, 1603 uint16_t sockets, uint16_t cores, uint16_t threads, uint16_t maxcpus) 1604 { 1605 struct vm_cpu_topology topology; 1606 1607 bzero(&topology, sizeof (struct vm_cpu_topology)); 1608 topology.sockets = sockets; 1609 topology.cores = cores; 1610 topology.threads = threads; 1611 topology.maxcpus = maxcpus; 1612 return (ioctl(ctx->fd, VM_SET_TOPOLOGY, &topology)); 1613 } 1614 1615 int 1616 vm_get_topology(struct vmctx *ctx, 1617 uint16_t *sockets, uint16_t *cores, uint16_t *threads, uint16_t *maxcpus) 1618 { 1619 struct vm_cpu_topology topology; 1620 int error; 1621 1622 bzero(&topology, sizeof (struct vm_cpu_topology)); 1623 error = ioctl(ctx->fd, VM_GET_TOPOLOGY, &topology); 1624 if (error == 0) { 1625 *sockets = topology.sockets; 1626 *cores = topology.cores; 1627 *threads = topology.threads; 1628 *maxcpus = topology.maxcpus; 1629 } 1630 return (error); 1631 } 1632 1633 int 1634 vm_get_device_fd(struct vmctx *ctx) 1635 { 1636 1637 return (ctx->fd); 1638 } 1639 1640 const cap_ioctl_t * 1641 vm_get_ioctls(size_t *len) 1642 { 1643 cap_ioctl_t *cmds; 1644 /* keep in sync with machine/vmm_dev.h */ 1645 static const cap_ioctl_t vm_ioctl_cmds[] = { VM_RUN, VM_SUSPEND, VM_REINIT, 1646 VM_ALLOC_MEMSEG, VM_GET_MEMSEG, VM_MMAP_MEMSEG, VM_MMAP_MEMSEG, 1647 VM_MMAP_GETNEXT, VM_SET_REGISTER, VM_GET_REGISTER, 1648 VM_SET_SEGMENT_DESCRIPTOR, VM_GET_SEGMENT_DESCRIPTOR, 1649 VM_SET_REGISTER_SET, VM_GET_REGISTER_SET, 1650 VM_SET_KERNEMU_DEV, VM_GET_KERNEMU_DEV, 1651 VM_INJECT_EXCEPTION, VM_LAPIC_IRQ, VM_LAPIC_LOCAL_IRQ, 1652 VM_LAPIC_MSI, VM_IOAPIC_ASSERT_IRQ, VM_IOAPIC_DEASSERT_IRQ, 1653 VM_IOAPIC_PULSE_IRQ, VM_IOAPIC_PINCOUNT, VM_ISA_ASSERT_IRQ, 1654 VM_ISA_DEASSERT_IRQ, VM_ISA_PULSE_IRQ, VM_ISA_SET_IRQ_TRIGGER, 1655 VM_SET_CAPABILITY, VM_GET_CAPABILITY, VM_BIND_PPTDEV, 1656 VM_UNBIND_PPTDEV, VM_MAP_PPTDEV_MMIO, VM_PPTDEV_MSI, 1657 VM_PPTDEV_MSIX, VM_PPTDEV_DISABLE_MSIX, 1658 VM_INJECT_NMI, VM_STATS, VM_STAT_DESC, 1659 VM_SET_X2APIC_STATE, VM_GET_X2APIC_STATE, 1660 VM_GET_HPET_CAPABILITIES, VM_GET_GPA_PMAP, VM_GLA2GPA, 1661 VM_GLA2GPA_NOFAULT, 1662 VM_ACTIVATE_CPU, VM_GET_CPUS, VM_SUSPEND_CPU, VM_RESUME_CPU, 1663 VM_SET_INTINFO, VM_GET_INTINFO, 1664 VM_RTC_WRITE, VM_RTC_READ, VM_RTC_SETTIME, VM_RTC_GETTIME, 1665 VM_RESTART_INSTRUCTION, VM_SET_TOPOLOGY, VM_GET_TOPOLOGY }; 1666 1667 if (len == NULL) { 1668 cmds = malloc(sizeof(vm_ioctl_cmds)); 1669 if (cmds == NULL) 1670 return (NULL); 1671 bcopy(vm_ioctl_cmds, cmds, sizeof(vm_ioctl_cmds)); 1672 return (cmds); 1673 } 1674 1675 *len = nitems(vm_ioctl_cmds); 1676 return (NULL); 1677 } 1678