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