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