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