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