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