1 /*- 2 * Copyright (c) 2011 NetApp, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/module.h> 36 #include <sys/sysctl.h> 37 #include <sys/malloc.h> 38 #include <sys/pcpu.h> 39 #include <sys/lock.h> 40 #include <sys/mutex.h> 41 #include <sys/proc.h> 42 #include <sys/rwlock.h> 43 #include <sys/sched.h> 44 #include <sys/smp.h> 45 #include <sys/systm.h> 46 47 #include <vm/vm.h> 48 #include <vm/vm_object.h> 49 #include <vm/vm_page.h> 50 #include <vm/pmap.h> 51 #include <vm/vm_map.h> 52 #include <vm/vm_extern.h> 53 #include <vm/vm_param.h> 54 55 #include <machine/cpu.h> 56 #include <machine/vm.h> 57 #include <machine/pcb.h> 58 #include <machine/smp.h> 59 #include <x86/psl.h> 60 #include <x86/apicreg.h> 61 #include <machine/vmparam.h> 62 63 #include <machine/vmm.h> 64 #include <machine/vmm_dev.h> 65 #include <machine/vmm_instruction_emul.h> 66 67 #include "vmm_ioport.h" 68 #include "vmm_ktr.h" 69 #include "vmm_host.h" 70 #include "vmm_mem.h" 71 #include "vmm_util.h" 72 #include "vatpic.h" 73 #include "vatpit.h" 74 #include "vhpet.h" 75 #include "vioapic.h" 76 #include "vlapic.h" 77 #include "vmm_msr.h" 78 #include "vmm_ipi.h" 79 #include "vmm_stat.h" 80 #include "vmm_lapic.h" 81 82 #include "io/ppt.h" 83 #include "io/iommu.h" 84 85 struct vlapic; 86 87 /* 88 * Initialization: 89 * (a) allocated when vcpu is created 90 * (i) initialized when vcpu is created and when it is reinitialized 91 * (o) initialized the first time the vcpu is created 92 * (x) initialized before use 93 */ 94 struct vcpu { 95 struct mtx mtx; /* (o) protects 'state' and 'hostcpu' */ 96 enum vcpu_state state; /* (o) vcpu state */ 97 int hostcpu; /* (o) vcpu's host cpu */ 98 struct vlapic *vlapic; /* (i) APIC device model */ 99 enum x2apic_state x2apic_state; /* (i) APIC mode */ 100 uint64_t exitintinfo; /* (i) events pending at VM exit */ 101 int nmi_pending; /* (i) NMI pending */ 102 int extint_pending; /* (i) INTR pending */ 103 struct vm_exception exception; /* (x) exception collateral */ 104 int exception_pending; /* (i) exception pending */ 105 struct savefpu *guestfpu; /* (a,i) guest fpu state */ 106 uint64_t guest_xcr0; /* (i) guest %xcr0 register */ 107 void *stats; /* (a,i) statistics */ 108 uint64_t guest_msrs[VMM_MSR_NUM]; /* (i) emulated MSRs */ 109 struct vm_exit exitinfo; /* (x) exit reason and collateral */ 110 }; 111 112 #define vcpu_lock_initialized(v) mtx_initialized(&((v)->mtx)) 113 #define vcpu_lock_init(v) mtx_init(&((v)->mtx), "vcpu lock", 0, MTX_SPIN) 114 #define vcpu_lock(v) mtx_lock_spin(&((v)->mtx)) 115 #define vcpu_unlock(v) mtx_unlock_spin(&((v)->mtx)) 116 #define vcpu_assert_locked(v) mtx_assert(&((v)->mtx), MA_OWNED) 117 118 struct mem_seg { 119 vm_paddr_t gpa; 120 size_t len; 121 boolean_t wired; 122 vm_object_t object; 123 }; 124 #define VM_MAX_MEMORY_SEGMENTS 2 125 126 /* 127 * Initialization: 128 * (o) initialized the first time the VM is created 129 * (i) initialized when VM is created and when it is reinitialized 130 * (x) initialized before use 131 */ 132 struct vm { 133 void *cookie; /* (i) cpu-specific data */ 134 void *iommu; /* (x) iommu-specific data */ 135 struct vhpet *vhpet; /* (i) virtual HPET */ 136 struct vioapic *vioapic; /* (i) virtual ioapic */ 137 struct vatpic *vatpic; /* (i) virtual atpic */ 138 struct vatpit *vatpit; /* (i) virtual atpit */ 139 volatile cpuset_t active_cpus; /* (i) active vcpus */ 140 int suspend; /* (i) stop VM execution */ 141 volatile cpuset_t suspended_cpus; /* (i) suspended vcpus */ 142 volatile cpuset_t halted_cpus; /* (x) cpus in a hard halt */ 143 cpuset_t rendezvous_req_cpus; /* (x) rendezvous requested */ 144 cpuset_t rendezvous_done_cpus; /* (x) rendezvous finished */ 145 void *rendezvous_arg; /* (x) rendezvous func/arg */ 146 vm_rendezvous_func_t rendezvous_func; 147 struct mtx rendezvous_mtx; /* (o) rendezvous lock */ 148 int num_mem_segs; /* (o) guest memory segments */ 149 struct mem_seg mem_segs[VM_MAX_MEMORY_SEGMENTS]; 150 struct vmspace *vmspace; /* (o) guest's address space */ 151 char name[VM_MAX_NAMELEN]; /* (o) virtual machine name */ 152 struct vcpu vcpu[VM_MAXCPU]; /* (i) guest vcpus */ 153 }; 154 155 static int vmm_initialized; 156 157 static struct vmm_ops *ops; 158 #define VMM_INIT(num) (ops != NULL ? (*ops->init)(num) : 0) 159 #define VMM_CLEANUP() (ops != NULL ? (*ops->cleanup)() : 0) 160 #define VMM_RESUME() (ops != NULL ? (*ops->resume)() : 0) 161 162 #define VMINIT(vm, pmap) (ops != NULL ? (*ops->vminit)(vm, pmap): NULL) 163 #define VMRUN(vmi, vcpu, rip, pmap, rptr, sptr) \ 164 (ops != NULL ? (*ops->vmrun)(vmi, vcpu, rip, pmap, rptr, sptr) : ENXIO) 165 #define VMCLEANUP(vmi) (ops != NULL ? (*ops->vmcleanup)(vmi) : NULL) 166 #define VMSPACE_ALLOC(min, max) \ 167 (ops != NULL ? (*ops->vmspace_alloc)(min, max) : NULL) 168 #define VMSPACE_FREE(vmspace) \ 169 (ops != NULL ? (*ops->vmspace_free)(vmspace) : ENXIO) 170 #define VMGETREG(vmi, vcpu, num, retval) \ 171 (ops != NULL ? (*ops->vmgetreg)(vmi, vcpu, num, retval) : ENXIO) 172 #define VMSETREG(vmi, vcpu, num, val) \ 173 (ops != NULL ? (*ops->vmsetreg)(vmi, vcpu, num, val) : ENXIO) 174 #define VMGETDESC(vmi, vcpu, num, desc) \ 175 (ops != NULL ? (*ops->vmgetdesc)(vmi, vcpu, num, desc) : ENXIO) 176 #define VMSETDESC(vmi, vcpu, num, desc) \ 177 (ops != NULL ? (*ops->vmsetdesc)(vmi, vcpu, num, desc) : ENXIO) 178 #define VMGETCAP(vmi, vcpu, num, retval) \ 179 (ops != NULL ? (*ops->vmgetcap)(vmi, vcpu, num, retval) : ENXIO) 180 #define VMSETCAP(vmi, vcpu, num, val) \ 181 (ops != NULL ? (*ops->vmsetcap)(vmi, vcpu, num, val) : ENXIO) 182 #define VLAPIC_INIT(vmi, vcpu) \ 183 (ops != NULL ? (*ops->vlapic_init)(vmi, vcpu) : NULL) 184 #define VLAPIC_CLEANUP(vmi, vlapic) \ 185 (ops != NULL ? (*ops->vlapic_cleanup)(vmi, vlapic) : NULL) 186 187 #define fpu_start_emulating() load_cr0(rcr0() | CR0_TS) 188 #define fpu_stop_emulating() clts() 189 190 static MALLOC_DEFINE(M_VM, "vm", "vm"); 191 CTASSERT(VMM_MSR_NUM <= 64); /* msr_mask can keep track of up to 64 msrs */ 192 193 /* statistics */ 194 static VMM_STAT(VCPU_TOTAL_RUNTIME, "vcpu total runtime"); 195 196 SYSCTL_NODE(_hw, OID_AUTO, vmm, CTLFLAG_RW, NULL, NULL); 197 198 /* 199 * Halt the guest if all vcpus are executing a HLT instruction with 200 * interrupts disabled. 201 */ 202 static int halt_detection_enabled = 1; 203 SYSCTL_INT(_hw_vmm, OID_AUTO, halt_detection, CTLFLAG_RDTUN, 204 &halt_detection_enabled, 0, 205 "Halt VM if all vcpus execute HLT with interrupts disabled"); 206 207 static int vmm_ipinum; 208 SYSCTL_INT(_hw_vmm, OID_AUTO, ipinum, CTLFLAG_RD, &vmm_ipinum, 0, 209 "IPI vector used for vcpu notifications"); 210 211 static void 212 vcpu_cleanup(struct vm *vm, int i, bool destroy) 213 { 214 struct vcpu *vcpu = &vm->vcpu[i]; 215 216 VLAPIC_CLEANUP(vm->cookie, vcpu->vlapic); 217 if (destroy) { 218 vmm_stat_free(vcpu->stats); 219 fpu_save_area_free(vcpu->guestfpu); 220 } 221 } 222 223 static void 224 vcpu_init(struct vm *vm, int vcpu_id, bool create) 225 { 226 struct vcpu *vcpu; 227 228 KASSERT(vcpu_id >= 0 && vcpu_id < VM_MAXCPU, 229 ("vcpu_init: invalid vcpu %d", vcpu_id)); 230 231 vcpu = &vm->vcpu[vcpu_id]; 232 233 if (create) { 234 KASSERT(!vcpu_lock_initialized(vcpu), ("vcpu %d already " 235 "initialized", vcpu_id)); 236 vcpu_lock_init(vcpu); 237 vcpu->state = VCPU_IDLE; 238 vcpu->hostcpu = NOCPU; 239 vcpu->guestfpu = fpu_save_area_alloc(); 240 vcpu->stats = vmm_stat_alloc(); 241 } 242 243 vcpu->vlapic = VLAPIC_INIT(vm->cookie, vcpu_id); 244 vm_set_x2apic_state(vm, vcpu_id, X2APIC_DISABLED); 245 vcpu->exitintinfo = 0; 246 vcpu->nmi_pending = 0; 247 vcpu->extint_pending = 0; 248 vcpu->exception_pending = 0; 249 vcpu->guest_xcr0 = XFEATURE_ENABLED_X87; 250 fpu_save_area_reset(vcpu->guestfpu); 251 vmm_stat_init(vcpu->stats); 252 guest_msrs_init(vm, vcpu_id); 253 } 254 255 struct vm_exit * 256 vm_exitinfo(struct vm *vm, int cpuid) 257 { 258 struct vcpu *vcpu; 259 260 if (cpuid < 0 || cpuid >= VM_MAXCPU) 261 panic("vm_exitinfo: invalid cpuid %d", cpuid); 262 263 vcpu = &vm->vcpu[cpuid]; 264 265 return (&vcpu->exitinfo); 266 } 267 268 static void 269 vmm_resume(void) 270 { 271 VMM_RESUME(); 272 } 273 274 static int 275 vmm_init(void) 276 { 277 int error; 278 279 vmm_host_state_init(); 280 281 vmm_ipinum = vmm_ipi_alloc(); 282 if (vmm_ipinum == 0) 283 vmm_ipinum = IPI_AST; 284 285 error = vmm_mem_init(); 286 if (error) 287 return (error); 288 289 if (vmm_is_intel()) 290 ops = &vmm_ops_intel; 291 else if (vmm_is_amd()) 292 ops = &vmm_ops_amd; 293 else 294 return (ENXIO); 295 296 vmm_msr_init(); 297 vmm_resume_p = vmm_resume; 298 299 return (VMM_INIT(vmm_ipinum)); 300 } 301 302 static int 303 vmm_handler(module_t mod, int what, void *arg) 304 { 305 int error; 306 307 switch (what) { 308 case MOD_LOAD: 309 vmmdev_init(); 310 if (ppt_avail_devices() > 0) 311 iommu_init(); 312 error = vmm_init(); 313 if (error == 0) 314 vmm_initialized = 1; 315 break; 316 case MOD_UNLOAD: 317 error = vmmdev_cleanup(); 318 if (error == 0) { 319 vmm_resume_p = NULL; 320 iommu_cleanup(); 321 if (vmm_ipinum != IPI_AST) 322 vmm_ipi_free(vmm_ipinum); 323 error = VMM_CLEANUP(); 324 /* 325 * Something bad happened - prevent new 326 * VMs from being created 327 */ 328 if (error) 329 vmm_initialized = 0; 330 } 331 break; 332 default: 333 error = 0; 334 break; 335 } 336 return (error); 337 } 338 339 static moduledata_t vmm_kmod = { 340 "vmm", 341 vmm_handler, 342 NULL 343 }; 344 345 /* 346 * vmm initialization has the following dependencies: 347 * 348 * - iommu initialization must happen after the pci passthru driver has had 349 * a chance to attach to any passthru devices (after SI_SUB_CONFIGURE). 350 * 351 * - VT-x initialization requires smp_rendezvous() and therefore must happen 352 * after SMP is fully functional (after SI_SUB_SMP). 353 */ 354 DECLARE_MODULE(vmm, vmm_kmod, SI_SUB_SMP + 1, SI_ORDER_ANY); 355 MODULE_VERSION(vmm, 1); 356 357 static void 358 vm_init(struct vm *vm, bool create) 359 { 360 int i; 361 362 vm->cookie = VMINIT(vm, vmspace_pmap(vm->vmspace)); 363 vm->iommu = NULL; 364 vm->vioapic = vioapic_init(vm); 365 vm->vhpet = vhpet_init(vm); 366 vm->vatpic = vatpic_init(vm); 367 vm->vatpit = vatpit_init(vm); 368 369 CPU_ZERO(&vm->active_cpus); 370 371 vm->suspend = 0; 372 CPU_ZERO(&vm->suspended_cpus); 373 374 for (i = 0; i < VM_MAXCPU; i++) 375 vcpu_init(vm, i, create); 376 } 377 378 int 379 vm_create(const char *name, struct vm **retvm) 380 { 381 struct vm *vm; 382 struct vmspace *vmspace; 383 384 /* 385 * If vmm.ko could not be successfully initialized then don't attempt 386 * to create the virtual machine. 387 */ 388 if (!vmm_initialized) 389 return (ENXIO); 390 391 if (name == NULL || strlen(name) >= VM_MAX_NAMELEN) 392 return (EINVAL); 393 394 vmspace = VMSPACE_ALLOC(VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS); 395 if (vmspace == NULL) 396 return (ENOMEM); 397 398 vm = malloc(sizeof(struct vm), M_VM, M_WAITOK | M_ZERO); 399 strcpy(vm->name, name); 400 vm->num_mem_segs = 0; 401 vm->vmspace = vmspace; 402 mtx_init(&vm->rendezvous_mtx, "vm rendezvous lock", 0, MTX_DEF); 403 404 vm_init(vm, true); 405 406 *retvm = vm; 407 return (0); 408 } 409 410 static void 411 vm_free_mem_seg(struct vm *vm, struct mem_seg *seg) 412 { 413 414 if (seg->object != NULL) 415 vmm_mem_free(vm->vmspace, seg->gpa, seg->len); 416 417 bzero(seg, sizeof(*seg)); 418 } 419 420 static void 421 vm_cleanup(struct vm *vm, bool destroy) 422 { 423 int i; 424 425 ppt_unassign_all(vm); 426 427 if (vm->iommu != NULL) 428 iommu_destroy_domain(vm->iommu); 429 430 vatpit_cleanup(vm->vatpit); 431 vhpet_cleanup(vm->vhpet); 432 vatpic_cleanup(vm->vatpic); 433 vioapic_cleanup(vm->vioapic); 434 435 for (i = 0; i < VM_MAXCPU; i++) 436 vcpu_cleanup(vm, i, destroy); 437 438 VMCLEANUP(vm->cookie); 439 440 if (destroy) { 441 for (i = 0; i < vm->num_mem_segs; i++) 442 vm_free_mem_seg(vm, &vm->mem_segs[i]); 443 444 vm->num_mem_segs = 0; 445 446 VMSPACE_FREE(vm->vmspace); 447 vm->vmspace = NULL; 448 } 449 } 450 451 void 452 vm_destroy(struct vm *vm) 453 { 454 vm_cleanup(vm, true); 455 free(vm, M_VM); 456 } 457 458 int 459 vm_reinit(struct vm *vm) 460 { 461 int error; 462 463 /* 464 * A virtual machine can be reset only if all vcpus are suspended. 465 */ 466 if (CPU_CMP(&vm->suspended_cpus, &vm->active_cpus) == 0) { 467 vm_cleanup(vm, false); 468 vm_init(vm, false); 469 error = 0; 470 } else { 471 error = EBUSY; 472 } 473 474 return (error); 475 } 476 477 const char * 478 vm_name(struct vm *vm) 479 { 480 return (vm->name); 481 } 482 483 int 484 vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa) 485 { 486 vm_object_t obj; 487 488 if ((obj = vmm_mmio_alloc(vm->vmspace, gpa, len, hpa)) == NULL) 489 return (ENOMEM); 490 else 491 return (0); 492 } 493 494 int 495 vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len) 496 { 497 498 vmm_mmio_free(vm->vmspace, gpa, len); 499 return (0); 500 } 501 502 boolean_t 503 vm_mem_allocated(struct vm *vm, vm_paddr_t gpa) 504 { 505 int i; 506 vm_paddr_t gpabase, gpalimit; 507 508 for (i = 0; i < vm->num_mem_segs; i++) { 509 gpabase = vm->mem_segs[i].gpa; 510 gpalimit = gpabase + vm->mem_segs[i].len; 511 if (gpa >= gpabase && gpa < gpalimit) 512 return (TRUE); /* 'gpa' is regular memory */ 513 } 514 515 if (ppt_is_mmio(vm, gpa)) 516 return (TRUE); /* 'gpa' is pci passthru mmio */ 517 518 return (FALSE); 519 } 520 521 int 522 vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len) 523 { 524 int available, allocated; 525 struct mem_seg *seg; 526 vm_object_t object; 527 vm_paddr_t g; 528 529 if ((gpa & PAGE_MASK) || (len & PAGE_MASK) || len == 0) 530 return (EINVAL); 531 532 available = allocated = 0; 533 g = gpa; 534 while (g < gpa + len) { 535 if (vm_mem_allocated(vm, g)) 536 allocated++; 537 else 538 available++; 539 540 g += PAGE_SIZE; 541 } 542 543 /* 544 * If there are some allocated and some available pages in the address 545 * range then it is an error. 546 */ 547 if (allocated && available) 548 return (EINVAL); 549 550 /* 551 * If the entire address range being requested has already been 552 * allocated then there isn't anything more to do. 553 */ 554 if (allocated && available == 0) 555 return (0); 556 557 if (vm->num_mem_segs >= VM_MAX_MEMORY_SEGMENTS) 558 return (E2BIG); 559 560 seg = &vm->mem_segs[vm->num_mem_segs]; 561 562 if ((object = vmm_mem_alloc(vm->vmspace, gpa, len)) == NULL) 563 return (ENOMEM); 564 565 seg->gpa = gpa; 566 seg->len = len; 567 seg->object = object; 568 seg->wired = FALSE; 569 570 vm->num_mem_segs++; 571 572 return (0); 573 } 574 575 static vm_paddr_t 576 vm_maxmem(struct vm *vm) 577 { 578 int i; 579 vm_paddr_t gpa, maxmem; 580 581 maxmem = 0; 582 for (i = 0; i < vm->num_mem_segs; i++) { 583 gpa = vm->mem_segs[i].gpa + vm->mem_segs[i].len; 584 if (gpa > maxmem) 585 maxmem = gpa; 586 } 587 return (maxmem); 588 } 589 590 static void 591 vm_gpa_unwire(struct vm *vm) 592 { 593 int i, rv; 594 struct mem_seg *seg; 595 596 for (i = 0; i < vm->num_mem_segs; i++) { 597 seg = &vm->mem_segs[i]; 598 if (!seg->wired) 599 continue; 600 601 rv = vm_map_unwire(&vm->vmspace->vm_map, 602 seg->gpa, seg->gpa + seg->len, 603 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES); 604 KASSERT(rv == KERN_SUCCESS, ("vm(%s) memory segment " 605 "%#lx/%ld could not be unwired: %d", 606 vm_name(vm), seg->gpa, seg->len, rv)); 607 608 seg->wired = FALSE; 609 } 610 } 611 612 static int 613 vm_gpa_wire(struct vm *vm) 614 { 615 int i, rv; 616 struct mem_seg *seg; 617 618 for (i = 0; i < vm->num_mem_segs; i++) { 619 seg = &vm->mem_segs[i]; 620 if (seg->wired) 621 continue; 622 623 /* XXX rlimits? */ 624 rv = vm_map_wire(&vm->vmspace->vm_map, 625 seg->gpa, seg->gpa + seg->len, 626 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES); 627 if (rv != KERN_SUCCESS) 628 break; 629 630 seg->wired = TRUE; 631 } 632 633 if (i < vm->num_mem_segs) { 634 /* 635 * Undo the wiring before returning an error. 636 */ 637 vm_gpa_unwire(vm); 638 return (EAGAIN); 639 } 640 641 return (0); 642 } 643 644 static void 645 vm_iommu_modify(struct vm *vm, boolean_t map) 646 { 647 int i, sz; 648 vm_paddr_t gpa, hpa; 649 struct mem_seg *seg; 650 void *vp, *cookie, *host_domain; 651 652 sz = PAGE_SIZE; 653 host_domain = iommu_host_domain(); 654 655 for (i = 0; i < vm->num_mem_segs; i++) { 656 seg = &vm->mem_segs[i]; 657 KASSERT(seg->wired, ("vm(%s) memory segment %#lx/%ld not wired", 658 vm_name(vm), seg->gpa, seg->len)); 659 660 gpa = seg->gpa; 661 while (gpa < seg->gpa + seg->len) { 662 vp = vm_gpa_hold(vm, gpa, PAGE_SIZE, VM_PROT_WRITE, 663 &cookie); 664 KASSERT(vp != NULL, ("vm(%s) could not map gpa %#lx", 665 vm_name(vm), gpa)); 666 667 vm_gpa_release(cookie); 668 669 hpa = DMAP_TO_PHYS((uintptr_t)vp); 670 if (map) { 671 iommu_create_mapping(vm->iommu, gpa, hpa, sz); 672 iommu_remove_mapping(host_domain, hpa, sz); 673 } else { 674 iommu_remove_mapping(vm->iommu, gpa, sz); 675 iommu_create_mapping(host_domain, hpa, hpa, sz); 676 } 677 678 gpa += PAGE_SIZE; 679 } 680 } 681 682 /* 683 * Invalidate the cached translations associated with the domain 684 * from which pages were removed. 685 */ 686 if (map) 687 iommu_invalidate_tlb(host_domain); 688 else 689 iommu_invalidate_tlb(vm->iommu); 690 } 691 692 #define vm_iommu_unmap(vm) vm_iommu_modify((vm), FALSE) 693 #define vm_iommu_map(vm) vm_iommu_modify((vm), TRUE) 694 695 int 696 vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func) 697 { 698 int error; 699 700 error = ppt_unassign_device(vm, bus, slot, func); 701 if (error) 702 return (error); 703 704 if (ppt_assigned_devices(vm) == 0) { 705 vm_iommu_unmap(vm); 706 vm_gpa_unwire(vm); 707 } 708 return (0); 709 } 710 711 int 712 vm_assign_pptdev(struct vm *vm, int bus, int slot, int func) 713 { 714 int error; 715 vm_paddr_t maxaddr; 716 717 /* 718 * Virtual machines with pci passthru devices get special treatment: 719 * - the guest physical memory is wired 720 * - the iommu is programmed to do the 'gpa' to 'hpa' translation 721 * 722 * We need to do this before the first pci passthru device is attached. 723 */ 724 if (ppt_assigned_devices(vm) == 0) { 725 KASSERT(vm->iommu == NULL, 726 ("vm_assign_pptdev: iommu must be NULL")); 727 maxaddr = vm_maxmem(vm); 728 vm->iommu = iommu_create_domain(maxaddr); 729 730 error = vm_gpa_wire(vm); 731 if (error) 732 return (error); 733 734 vm_iommu_map(vm); 735 } 736 737 error = ppt_assign_device(vm, bus, slot, func); 738 return (error); 739 } 740 741 void * 742 vm_gpa_hold(struct vm *vm, vm_paddr_t gpa, size_t len, int reqprot, 743 void **cookie) 744 { 745 int count, pageoff; 746 vm_page_t m; 747 748 pageoff = gpa & PAGE_MASK; 749 if (len > PAGE_SIZE - pageoff) 750 panic("vm_gpa_hold: invalid gpa/len: 0x%016lx/%lu", gpa, len); 751 752 count = vm_fault_quick_hold_pages(&vm->vmspace->vm_map, 753 trunc_page(gpa), PAGE_SIZE, reqprot, &m, 1); 754 755 if (count == 1) { 756 *cookie = m; 757 return ((void *)(PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)) + pageoff)); 758 } else { 759 *cookie = NULL; 760 return (NULL); 761 } 762 } 763 764 void 765 vm_gpa_release(void *cookie) 766 { 767 vm_page_t m = cookie; 768 769 vm_page_lock(m); 770 vm_page_unhold(m); 771 vm_page_unlock(m); 772 } 773 774 int 775 vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, 776 struct vm_memory_segment *seg) 777 { 778 int i; 779 780 for (i = 0; i < vm->num_mem_segs; i++) { 781 if (gpabase == vm->mem_segs[i].gpa) { 782 seg->gpa = vm->mem_segs[i].gpa; 783 seg->len = vm->mem_segs[i].len; 784 seg->wired = vm->mem_segs[i].wired; 785 return (0); 786 } 787 } 788 return (-1); 789 } 790 791 int 792 vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len, 793 vm_offset_t *offset, struct vm_object **object) 794 { 795 int i; 796 size_t seg_len; 797 vm_paddr_t seg_gpa; 798 vm_object_t seg_obj; 799 800 for (i = 0; i < vm->num_mem_segs; i++) { 801 if ((seg_obj = vm->mem_segs[i].object) == NULL) 802 continue; 803 804 seg_gpa = vm->mem_segs[i].gpa; 805 seg_len = vm->mem_segs[i].len; 806 807 if (gpa >= seg_gpa && gpa < seg_gpa + seg_len) { 808 *offset = gpa - seg_gpa; 809 *object = seg_obj; 810 vm_object_reference(seg_obj); 811 return (0); 812 } 813 } 814 815 return (EINVAL); 816 } 817 818 int 819 vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval) 820 { 821 822 if (vcpu < 0 || vcpu >= VM_MAXCPU) 823 return (EINVAL); 824 825 if (reg >= VM_REG_LAST) 826 return (EINVAL); 827 828 return (VMGETREG(vm->cookie, vcpu, reg, retval)); 829 } 830 831 int 832 vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val) 833 { 834 835 if (vcpu < 0 || vcpu >= VM_MAXCPU) 836 return (EINVAL); 837 838 if (reg >= VM_REG_LAST) 839 return (EINVAL); 840 841 return (VMSETREG(vm->cookie, vcpu, reg, val)); 842 } 843 844 static boolean_t 845 is_descriptor_table(int reg) 846 { 847 848 switch (reg) { 849 case VM_REG_GUEST_IDTR: 850 case VM_REG_GUEST_GDTR: 851 return (TRUE); 852 default: 853 return (FALSE); 854 } 855 } 856 857 static boolean_t 858 is_segment_register(int reg) 859 { 860 861 switch (reg) { 862 case VM_REG_GUEST_ES: 863 case VM_REG_GUEST_CS: 864 case VM_REG_GUEST_SS: 865 case VM_REG_GUEST_DS: 866 case VM_REG_GUEST_FS: 867 case VM_REG_GUEST_GS: 868 case VM_REG_GUEST_TR: 869 case VM_REG_GUEST_LDTR: 870 return (TRUE); 871 default: 872 return (FALSE); 873 } 874 } 875 876 int 877 vm_get_seg_desc(struct vm *vm, int vcpu, int reg, 878 struct seg_desc *desc) 879 { 880 881 if (vcpu < 0 || vcpu >= VM_MAXCPU) 882 return (EINVAL); 883 884 if (!is_segment_register(reg) && !is_descriptor_table(reg)) 885 return (EINVAL); 886 887 return (VMGETDESC(vm->cookie, vcpu, reg, desc)); 888 } 889 890 int 891 vm_set_seg_desc(struct vm *vm, int vcpu, int reg, 892 struct seg_desc *desc) 893 { 894 if (vcpu < 0 || vcpu >= VM_MAXCPU) 895 return (EINVAL); 896 897 if (!is_segment_register(reg) && !is_descriptor_table(reg)) 898 return (EINVAL); 899 900 return (VMSETDESC(vm->cookie, vcpu, reg, desc)); 901 } 902 903 static void 904 restore_guest_fpustate(struct vcpu *vcpu) 905 { 906 907 /* flush host state to the pcb */ 908 fpuexit(curthread); 909 910 /* restore guest FPU state */ 911 fpu_stop_emulating(); 912 fpurestore(vcpu->guestfpu); 913 914 /* restore guest XCR0 if XSAVE is enabled in the host */ 915 if (rcr4() & CR4_XSAVE) 916 load_xcr(0, vcpu->guest_xcr0); 917 918 /* 919 * The FPU is now "dirty" with the guest's state so turn on emulation 920 * to trap any access to the FPU by the host. 921 */ 922 fpu_start_emulating(); 923 } 924 925 static void 926 save_guest_fpustate(struct vcpu *vcpu) 927 { 928 929 if ((rcr0() & CR0_TS) == 0) 930 panic("fpu emulation not enabled in host!"); 931 932 /* save guest XCR0 and restore host XCR0 */ 933 if (rcr4() & CR4_XSAVE) { 934 vcpu->guest_xcr0 = rxcr(0); 935 load_xcr(0, vmm_get_host_xcr0()); 936 } 937 938 /* save guest FPU state */ 939 fpu_stop_emulating(); 940 fpusave(vcpu->guestfpu); 941 fpu_start_emulating(); 942 } 943 944 static VMM_STAT(VCPU_IDLE_TICKS, "number of ticks vcpu was idle"); 945 946 static int 947 vcpu_set_state_locked(struct vcpu *vcpu, enum vcpu_state newstate, 948 bool from_idle) 949 { 950 int error; 951 952 vcpu_assert_locked(vcpu); 953 954 /* 955 * State transitions from the vmmdev_ioctl() must always begin from 956 * the VCPU_IDLE state. This guarantees that there is only a single 957 * ioctl() operating on a vcpu at any point. 958 */ 959 if (from_idle) { 960 while (vcpu->state != VCPU_IDLE) 961 msleep_spin(&vcpu->state, &vcpu->mtx, "vmstat", hz); 962 } else { 963 KASSERT(vcpu->state != VCPU_IDLE, ("invalid transition from " 964 "vcpu idle state")); 965 } 966 967 if (vcpu->state == VCPU_RUNNING) { 968 KASSERT(vcpu->hostcpu == curcpu, ("curcpu %d and hostcpu %d " 969 "mismatch for running vcpu", curcpu, vcpu->hostcpu)); 970 } else { 971 KASSERT(vcpu->hostcpu == NOCPU, ("Invalid hostcpu %d for a " 972 "vcpu that is not running", vcpu->hostcpu)); 973 } 974 975 /* 976 * The following state transitions are allowed: 977 * IDLE -> FROZEN -> IDLE 978 * FROZEN -> RUNNING -> FROZEN 979 * FROZEN -> SLEEPING -> FROZEN 980 */ 981 switch (vcpu->state) { 982 case VCPU_IDLE: 983 case VCPU_RUNNING: 984 case VCPU_SLEEPING: 985 error = (newstate != VCPU_FROZEN); 986 break; 987 case VCPU_FROZEN: 988 error = (newstate == VCPU_FROZEN); 989 break; 990 default: 991 error = 1; 992 break; 993 } 994 995 if (error) 996 return (EBUSY); 997 998 vcpu->state = newstate; 999 if (newstate == VCPU_RUNNING) 1000 vcpu->hostcpu = curcpu; 1001 else 1002 vcpu->hostcpu = NOCPU; 1003 1004 if (newstate == VCPU_IDLE) 1005 wakeup(&vcpu->state); 1006 1007 return (0); 1008 } 1009 1010 static void 1011 vcpu_require_state(struct vm *vm, int vcpuid, enum vcpu_state newstate) 1012 { 1013 int error; 1014 1015 if ((error = vcpu_set_state(vm, vcpuid, newstate, false)) != 0) 1016 panic("Error %d setting state to %d\n", error, newstate); 1017 } 1018 1019 static void 1020 vcpu_require_state_locked(struct vcpu *vcpu, enum vcpu_state newstate) 1021 { 1022 int error; 1023 1024 if ((error = vcpu_set_state_locked(vcpu, newstate, false)) != 0) 1025 panic("Error %d setting state to %d", error, newstate); 1026 } 1027 1028 static void 1029 vm_set_rendezvous_func(struct vm *vm, vm_rendezvous_func_t func) 1030 { 1031 1032 KASSERT(mtx_owned(&vm->rendezvous_mtx), ("rendezvous_mtx not locked")); 1033 1034 /* 1035 * Update 'rendezvous_func' and execute a write memory barrier to 1036 * ensure that it is visible across all host cpus. This is not needed 1037 * for correctness but it does ensure that all the vcpus will notice 1038 * that the rendezvous is requested immediately. 1039 */ 1040 vm->rendezvous_func = func; 1041 wmb(); 1042 } 1043 1044 #define RENDEZVOUS_CTR0(vm, vcpuid, fmt) \ 1045 do { \ 1046 if (vcpuid >= 0) \ 1047 VCPU_CTR0(vm, vcpuid, fmt); \ 1048 else \ 1049 VM_CTR0(vm, fmt); \ 1050 } while (0) 1051 1052 static void 1053 vm_handle_rendezvous(struct vm *vm, int vcpuid) 1054 { 1055 1056 KASSERT(vcpuid == -1 || (vcpuid >= 0 && vcpuid < VM_MAXCPU), 1057 ("vm_handle_rendezvous: invalid vcpuid %d", vcpuid)); 1058 1059 mtx_lock(&vm->rendezvous_mtx); 1060 while (vm->rendezvous_func != NULL) { 1061 /* 'rendezvous_req_cpus' must be a subset of 'active_cpus' */ 1062 CPU_AND(&vm->rendezvous_req_cpus, &vm->active_cpus); 1063 1064 if (vcpuid != -1 && 1065 CPU_ISSET(vcpuid, &vm->rendezvous_req_cpus) && 1066 !CPU_ISSET(vcpuid, &vm->rendezvous_done_cpus)) { 1067 VCPU_CTR0(vm, vcpuid, "Calling rendezvous func"); 1068 (*vm->rendezvous_func)(vm, vcpuid, vm->rendezvous_arg); 1069 CPU_SET(vcpuid, &vm->rendezvous_done_cpus); 1070 } 1071 if (CPU_CMP(&vm->rendezvous_req_cpus, 1072 &vm->rendezvous_done_cpus) == 0) { 1073 VCPU_CTR0(vm, vcpuid, "Rendezvous completed"); 1074 vm_set_rendezvous_func(vm, NULL); 1075 wakeup(&vm->rendezvous_func); 1076 break; 1077 } 1078 RENDEZVOUS_CTR0(vm, vcpuid, "Wait for rendezvous completion"); 1079 mtx_sleep(&vm->rendezvous_func, &vm->rendezvous_mtx, 0, 1080 "vmrndv", 0); 1081 } 1082 mtx_unlock(&vm->rendezvous_mtx); 1083 } 1084 1085 /* 1086 * Emulate a guest 'hlt' by sleeping until the vcpu is ready to run. 1087 */ 1088 static int 1089 vm_handle_hlt(struct vm *vm, int vcpuid, bool intr_disabled, bool *retu) 1090 { 1091 struct vcpu *vcpu; 1092 const char *wmesg; 1093 int t, vcpu_halted, vm_halted; 1094 1095 KASSERT(!CPU_ISSET(vcpuid, &vm->halted_cpus), ("vcpu already halted")); 1096 1097 vcpu = &vm->vcpu[vcpuid]; 1098 vcpu_halted = 0; 1099 vm_halted = 0; 1100 1101 vcpu_lock(vcpu); 1102 while (1) { 1103 /* 1104 * Do a final check for pending NMI or interrupts before 1105 * really putting this thread to sleep. Also check for 1106 * software events that would cause this vcpu to wakeup. 1107 * 1108 * These interrupts/events could have happened after the 1109 * vcpu returned from VMRUN() and before it acquired the 1110 * vcpu lock above. 1111 */ 1112 if (vm->rendezvous_func != NULL || vm->suspend) 1113 break; 1114 if (vm_nmi_pending(vm, vcpuid)) 1115 break; 1116 if (!intr_disabled) { 1117 if (vm_extint_pending(vm, vcpuid) || 1118 vlapic_pending_intr(vcpu->vlapic, NULL)) { 1119 break; 1120 } 1121 } 1122 1123 /* Don't go to sleep if the vcpu thread needs to yield */ 1124 if (vcpu_should_yield(vm, vcpuid)) 1125 break; 1126 1127 /* 1128 * Some Linux guests implement "halt" by having all vcpus 1129 * execute HLT with interrupts disabled. 'halted_cpus' keeps 1130 * track of the vcpus that have entered this state. When all 1131 * vcpus enter the halted state the virtual machine is halted. 1132 */ 1133 if (intr_disabled) { 1134 wmesg = "vmhalt"; 1135 VCPU_CTR0(vm, vcpuid, "Halted"); 1136 if (!vcpu_halted && halt_detection_enabled) { 1137 vcpu_halted = 1; 1138 CPU_SET_ATOMIC(vcpuid, &vm->halted_cpus); 1139 } 1140 if (CPU_CMP(&vm->halted_cpus, &vm->active_cpus) == 0) { 1141 vm_halted = 1; 1142 break; 1143 } 1144 } else { 1145 wmesg = "vmidle"; 1146 } 1147 1148 t = ticks; 1149 vcpu_require_state_locked(vcpu, VCPU_SLEEPING); 1150 /* 1151 * XXX msleep_spin() cannot be interrupted by signals so 1152 * wake up periodically to check pending signals. 1153 */ 1154 msleep_spin(vcpu, &vcpu->mtx, wmesg, hz); 1155 vcpu_require_state_locked(vcpu, VCPU_FROZEN); 1156 vmm_stat_incr(vm, vcpuid, VCPU_IDLE_TICKS, ticks - t); 1157 } 1158 1159 if (vcpu_halted) 1160 CPU_CLR_ATOMIC(vcpuid, &vm->halted_cpus); 1161 1162 vcpu_unlock(vcpu); 1163 1164 if (vm_halted) 1165 vm_suspend(vm, VM_SUSPEND_HALT); 1166 1167 return (0); 1168 } 1169 1170 static int 1171 vm_handle_paging(struct vm *vm, int vcpuid, bool *retu) 1172 { 1173 int rv, ftype; 1174 struct vm_map *map; 1175 struct vcpu *vcpu; 1176 struct vm_exit *vme; 1177 1178 vcpu = &vm->vcpu[vcpuid]; 1179 vme = &vcpu->exitinfo; 1180 1181 ftype = vme->u.paging.fault_type; 1182 KASSERT(ftype == VM_PROT_READ || 1183 ftype == VM_PROT_WRITE || ftype == VM_PROT_EXECUTE, 1184 ("vm_handle_paging: invalid fault_type %d", ftype)); 1185 1186 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) { 1187 rv = pmap_emulate_accessed_dirty(vmspace_pmap(vm->vmspace), 1188 vme->u.paging.gpa, ftype); 1189 if (rv == 0) 1190 goto done; 1191 } 1192 1193 map = &vm->vmspace->vm_map; 1194 rv = vm_fault(map, vme->u.paging.gpa, ftype, VM_FAULT_NORMAL); 1195 1196 VCPU_CTR3(vm, vcpuid, "vm_handle_paging rv = %d, gpa = %#lx, " 1197 "ftype = %d", rv, vme->u.paging.gpa, ftype); 1198 1199 if (rv != KERN_SUCCESS) 1200 return (EFAULT); 1201 done: 1202 /* restart execution at the faulting instruction */ 1203 vme->inst_length = 0; 1204 1205 return (0); 1206 } 1207 1208 static int 1209 vm_handle_inst_emul(struct vm *vm, int vcpuid, bool *retu) 1210 { 1211 struct vie *vie; 1212 struct vcpu *vcpu; 1213 struct vm_exit *vme; 1214 uint64_t gla, gpa; 1215 struct vm_guest_paging *paging; 1216 mem_region_read_t mread; 1217 mem_region_write_t mwrite; 1218 enum vm_cpu_mode cpu_mode; 1219 int cs_d, error; 1220 1221 vcpu = &vm->vcpu[vcpuid]; 1222 vme = &vcpu->exitinfo; 1223 1224 gla = vme->u.inst_emul.gla; 1225 gpa = vme->u.inst_emul.gpa; 1226 cs_d = vme->u.inst_emul.cs_d; 1227 vie = &vme->u.inst_emul.vie; 1228 paging = &vme->u.inst_emul.paging; 1229 cpu_mode = paging->cpu_mode; 1230 1231 vie_init(vie); 1232 1233 /* Fetch, decode and emulate the faulting instruction */ 1234 error = vmm_fetch_instruction(vm, vcpuid, paging, vme->rip, 1235 vme->inst_length, vie); 1236 if (error == 1) 1237 return (0); /* Resume guest to handle page fault */ 1238 else if (error == -1) 1239 return (EFAULT); 1240 else if (error != 0) 1241 panic("%s: vmm_fetch_instruction error %d", __func__, error); 1242 1243 if (vmm_decode_instruction(vm, vcpuid, gla, cpu_mode, cs_d, vie) != 0) 1244 return (EFAULT); 1245 1246 /* return to userland unless this is an in-kernel emulated device */ 1247 if (gpa >= DEFAULT_APIC_BASE && gpa < DEFAULT_APIC_BASE + PAGE_SIZE) { 1248 mread = lapic_mmio_read; 1249 mwrite = lapic_mmio_write; 1250 } else if (gpa >= VIOAPIC_BASE && gpa < VIOAPIC_BASE + VIOAPIC_SIZE) { 1251 mread = vioapic_mmio_read; 1252 mwrite = vioapic_mmio_write; 1253 } else if (gpa >= VHPET_BASE && gpa < VHPET_BASE + VHPET_SIZE) { 1254 mread = vhpet_mmio_read; 1255 mwrite = vhpet_mmio_write; 1256 } else { 1257 *retu = true; 1258 return (0); 1259 } 1260 1261 error = vmm_emulate_instruction(vm, vcpuid, gpa, vie, paging, 1262 mread, mwrite, retu); 1263 1264 return (error); 1265 } 1266 1267 static int 1268 vm_handle_suspend(struct vm *vm, int vcpuid, bool *retu) 1269 { 1270 int i, done; 1271 struct vcpu *vcpu; 1272 1273 done = 0; 1274 vcpu = &vm->vcpu[vcpuid]; 1275 1276 CPU_SET_ATOMIC(vcpuid, &vm->suspended_cpus); 1277 1278 /* 1279 * Wait until all 'active_cpus' have suspended themselves. 1280 * 1281 * Since a VM may be suspended at any time including when one or 1282 * more vcpus are doing a rendezvous we need to call the rendezvous 1283 * handler while we are waiting to prevent a deadlock. 1284 */ 1285 vcpu_lock(vcpu); 1286 while (1) { 1287 if (CPU_CMP(&vm->suspended_cpus, &vm->active_cpus) == 0) { 1288 VCPU_CTR0(vm, vcpuid, "All vcpus suspended"); 1289 break; 1290 } 1291 1292 if (vm->rendezvous_func == NULL) { 1293 VCPU_CTR0(vm, vcpuid, "Sleeping during suspend"); 1294 vcpu_require_state_locked(vcpu, VCPU_SLEEPING); 1295 msleep_spin(vcpu, &vcpu->mtx, "vmsusp", hz); 1296 vcpu_require_state_locked(vcpu, VCPU_FROZEN); 1297 } else { 1298 VCPU_CTR0(vm, vcpuid, "Rendezvous during suspend"); 1299 vcpu_unlock(vcpu); 1300 vm_handle_rendezvous(vm, vcpuid); 1301 vcpu_lock(vcpu); 1302 } 1303 } 1304 vcpu_unlock(vcpu); 1305 1306 /* 1307 * Wakeup the other sleeping vcpus and return to userspace. 1308 */ 1309 for (i = 0; i < VM_MAXCPU; i++) { 1310 if (CPU_ISSET(i, &vm->suspended_cpus)) { 1311 vcpu_notify_event(vm, i, false); 1312 } 1313 } 1314 1315 *retu = true; 1316 return (0); 1317 } 1318 1319 int 1320 vm_suspend(struct vm *vm, enum vm_suspend_how how) 1321 { 1322 int i; 1323 1324 if (how <= VM_SUSPEND_NONE || how >= VM_SUSPEND_LAST) 1325 return (EINVAL); 1326 1327 if (atomic_cmpset_int(&vm->suspend, 0, how) == 0) { 1328 VM_CTR2(vm, "virtual machine already suspended %d/%d", 1329 vm->suspend, how); 1330 return (EALREADY); 1331 } 1332 1333 VM_CTR1(vm, "virtual machine successfully suspended %d", how); 1334 1335 /* 1336 * Notify all active vcpus that they are now suspended. 1337 */ 1338 for (i = 0; i < VM_MAXCPU; i++) { 1339 if (CPU_ISSET(i, &vm->active_cpus)) 1340 vcpu_notify_event(vm, i, false); 1341 } 1342 1343 return (0); 1344 } 1345 1346 void 1347 vm_exit_suspended(struct vm *vm, int vcpuid, uint64_t rip) 1348 { 1349 struct vm_exit *vmexit; 1350 1351 KASSERT(vm->suspend > VM_SUSPEND_NONE && vm->suspend < VM_SUSPEND_LAST, 1352 ("vm_exit_suspended: invalid suspend type %d", vm->suspend)); 1353 1354 vmexit = vm_exitinfo(vm, vcpuid); 1355 vmexit->rip = rip; 1356 vmexit->inst_length = 0; 1357 vmexit->exitcode = VM_EXITCODE_SUSPENDED; 1358 vmexit->u.suspended.how = vm->suspend; 1359 } 1360 1361 void 1362 vm_exit_rendezvous(struct vm *vm, int vcpuid, uint64_t rip) 1363 { 1364 struct vm_exit *vmexit; 1365 1366 KASSERT(vm->rendezvous_func != NULL, ("rendezvous not in progress")); 1367 1368 vmexit = vm_exitinfo(vm, vcpuid); 1369 vmexit->rip = rip; 1370 vmexit->inst_length = 0; 1371 vmexit->exitcode = VM_EXITCODE_RENDEZVOUS; 1372 vmm_stat_incr(vm, vcpuid, VMEXIT_RENDEZVOUS, 1); 1373 } 1374 1375 void 1376 vm_exit_astpending(struct vm *vm, int vcpuid, uint64_t rip) 1377 { 1378 struct vm_exit *vmexit; 1379 1380 vmexit = vm_exitinfo(vm, vcpuid); 1381 vmexit->rip = rip; 1382 vmexit->inst_length = 0; 1383 vmexit->exitcode = VM_EXITCODE_BOGUS; 1384 vmm_stat_incr(vm, vcpuid, VMEXIT_ASTPENDING, 1); 1385 } 1386 1387 int 1388 vm_run(struct vm *vm, struct vm_run *vmrun) 1389 { 1390 int error, vcpuid; 1391 struct vcpu *vcpu; 1392 struct pcb *pcb; 1393 uint64_t tscval, rip; 1394 struct vm_exit *vme; 1395 bool retu, intr_disabled; 1396 pmap_t pmap; 1397 void *rptr, *sptr; 1398 1399 vcpuid = vmrun->cpuid; 1400 1401 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1402 return (EINVAL); 1403 1404 if (!CPU_ISSET(vcpuid, &vm->active_cpus)) 1405 return (EINVAL); 1406 1407 if (CPU_ISSET(vcpuid, &vm->suspended_cpus)) 1408 return (EINVAL); 1409 1410 rptr = &vm->rendezvous_func; 1411 sptr = &vm->suspend; 1412 pmap = vmspace_pmap(vm->vmspace); 1413 vcpu = &vm->vcpu[vcpuid]; 1414 vme = &vcpu->exitinfo; 1415 rip = vmrun->rip; 1416 restart: 1417 critical_enter(); 1418 1419 KASSERT(!CPU_ISSET(curcpu, &pmap->pm_active), 1420 ("vm_run: absurd pm_active")); 1421 1422 tscval = rdtsc(); 1423 1424 pcb = PCPU_GET(curpcb); 1425 set_pcb_flags(pcb, PCB_FULL_IRET); 1426 1427 restore_guest_msrs(vm, vcpuid); 1428 restore_guest_fpustate(vcpu); 1429 1430 vcpu_require_state(vm, vcpuid, VCPU_RUNNING); 1431 error = VMRUN(vm->cookie, vcpuid, rip, pmap, rptr, sptr); 1432 vcpu_require_state(vm, vcpuid, VCPU_FROZEN); 1433 1434 save_guest_fpustate(vcpu); 1435 restore_host_msrs(vm, vcpuid); 1436 1437 vmm_stat_incr(vm, vcpuid, VCPU_TOTAL_RUNTIME, rdtsc() - tscval); 1438 1439 critical_exit(); 1440 1441 if (error == 0) { 1442 retu = false; 1443 switch (vme->exitcode) { 1444 case VM_EXITCODE_SUSPENDED: 1445 error = vm_handle_suspend(vm, vcpuid, &retu); 1446 break; 1447 case VM_EXITCODE_IOAPIC_EOI: 1448 vioapic_process_eoi(vm, vcpuid, 1449 vme->u.ioapic_eoi.vector); 1450 break; 1451 case VM_EXITCODE_RENDEZVOUS: 1452 vm_handle_rendezvous(vm, vcpuid); 1453 error = 0; 1454 break; 1455 case VM_EXITCODE_HLT: 1456 intr_disabled = ((vme->u.hlt.rflags & PSL_I) == 0); 1457 error = vm_handle_hlt(vm, vcpuid, intr_disabled, &retu); 1458 break; 1459 case VM_EXITCODE_PAGING: 1460 error = vm_handle_paging(vm, vcpuid, &retu); 1461 break; 1462 case VM_EXITCODE_INST_EMUL: 1463 error = vm_handle_inst_emul(vm, vcpuid, &retu); 1464 break; 1465 case VM_EXITCODE_INOUT: 1466 case VM_EXITCODE_INOUT_STR: 1467 error = vm_handle_inout(vm, vcpuid, vme, &retu); 1468 break; 1469 default: 1470 retu = true; /* handled in userland */ 1471 break; 1472 } 1473 } 1474 1475 if (error == 0 && retu == false) { 1476 rip = vme->rip + vme->inst_length; 1477 goto restart; 1478 } 1479 1480 /* copy the exit information */ 1481 bcopy(vme, &vmrun->vm_exit, sizeof(struct vm_exit)); 1482 return (error); 1483 } 1484 1485 int 1486 vm_exit_intinfo(struct vm *vm, int vcpuid, uint64_t info) 1487 { 1488 struct vcpu *vcpu; 1489 int type, vector; 1490 1491 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1492 return (EINVAL); 1493 1494 vcpu = &vm->vcpu[vcpuid]; 1495 1496 if (info & VM_INTINFO_VALID) { 1497 type = info & VM_INTINFO_TYPE; 1498 vector = info & 0xff; 1499 if (type == VM_INTINFO_NMI && vector != IDT_NMI) 1500 return (EINVAL); 1501 if (type == VM_INTINFO_HWEXCEPTION && vector >= 32) 1502 return (EINVAL); 1503 if (info & VM_INTINFO_RSVD) 1504 return (EINVAL); 1505 } else { 1506 info = 0; 1507 } 1508 VCPU_CTR2(vm, vcpuid, "%s: info1(%#lx)", __func__, info); 1509 vcpu->exitintinfo = info; 1510 return (0); 1511 } 1512 1513 enum exc_class { 1514 EXC_BENIGN, 1515 EXC_CONTRIBUTORY, 1516 EXC_PAGEFAULT 1517 }; 1518 1519 #define IDT_VE 20 /* Virtualization Exception (Intel specific) */ 1520 1521 static enum exc_class 1522 exception_class(uint64_t info) 1523 { 1524 int type, vector; 1525 1526 KASSERT(info & VM_INTINFO_VALID, ("intinfo must be valid: %#lx", info)); 1527 type = info & VM_INTINFO_TYPE; 1528 vector = info & 0xff; 1529 1530 /* Table 6-4, "Interrupt and Exception Classes", Intel SDM, Vol 3 */ 1531 switch (type) { 1532 case VM_INTINFO_HWINTR: 1533 case VM_INTINFO_SWINTR: 1534 case VM_INTINFO_NMI: 1535 return (EXC_BENIGN); 1536 default: 1537 /* 1538 * Hardware exception. 1539 * 1540 * SVM and VT-x use identical type values to represent NMI, 1541 * hardware interrupt and software interrupt. 1542 * 1543 * SVM uses type '3' for all exceptions. VT-x uses type '3' 1544 * for exceptions except #BP and #OF. #BP and #OF use a type 1545 * value of '5' or '6'. Therefore we don't check for explicit 1546 * values of 'type' to classify 'intinfo' into a hardware 1547 * exception. 1548 */ 1549 break; 1550 } 1551 1552 switch (vector) { 1553 case IDT_PF: 1554 case IDT_VE: 1555 return (EXC_PAGEFAULT); 1556 case IDT_DE: 1557 case IDT_TS: 1558 case IDT_NP: 1559 case IDT_SS: 1560 case IDT_GP: 1561 return (EXC_CONTRIBUTORY); 1562 default: 1563 return (EXC_BENIGN); 1564 } 1565 } 1566 1567 static int 1568 nested_fault(struct vm *vm, int vcpuid, uint64_t info1, uint64_t info2, 1569 uint64_t *retinfo) 1570 { 1571 enum exc_class exc1, exc2; 1572 int type1, vector1; 1573 1574 KASSERT(info1 & VM_INTINFO_VALID, ("info1 %#lx is not valid", info1)); 1575 KASSERT(info2 & VM_INTINFO_VALID, ("info2 %#lx is not valid", info2)); 1576 1577 /* 1578 * If an exception occurs while attempting to call the double-fault 1579 * handler the processor enters shutdown mode (aka triple fault). 1580 */ 1581 type1 = info1 & VM_INTINFO_TYPE; 1582 vector1 = info1 & 0xff; 1583 if (type1 == VM_INTINFO_HWEXCEPTION && vector1 == IDT_DF) { 1584 VCPU_CTR2(vm, vcpuid, "triple fault: info1(%#lx), info2(%#lx)", 1585 info1, info2); 1586 vm_suspend(vm, VM_SUSPEND_TRIPLEFAULT); 1587 *retinfo = 0; 1588 return (0); 1589 } 1590 1591 /* 1592 * Table 6-5 "Conditions for Generating a Double Fault", Intel SDM, Vol3 1593 */ 1594 exc1 = exception_class(info1); 1595 exc2 = exception_class(info2); 1596 if ((exc1 == EXC_CONTRIBUTORY && exc2 == EXC_CONTRIBUTORY) || 1597 (exc1 == EXC_PAGEFAULT && exc2 != EXC_BENIGN)) { 1598 /* Convert nested fault into a double fault. */ 1599 *retinfo = IDT_DF; 1600 *retinfo |= VM_INTINFO_VALID | VM_INTINFO_HWEXCEPTION; 1601 *retinfo |= VM_INTINFO_DEL_ERRCODE; 1602 } else { 1603 /* Handle exceptions serially */ 1604 *retinfo = info2; 1605 } 1606 return (1); 1607 } 1608 1609 static uint64_t 1610 vcpu_exception_intinfo(struct vcpu *vcpu) 1611 { 1612 uint64_t info = 0; 1613 1614 if (vcpu->exception_pending) { 1615 info = vcpu->exception.vector & 0xff; 1616 info |= VM_INTINFO_VALID | VM_INTINFO_HWEXCEPTION; 1617 if (vcpu->exception.error_code_valid) { 1618 info |= VM_INTINFO_DEL_ERRCODE; 1619 info |= (uint64_t)vcpu->exception.error_code << 32; 1620 } 1621 } 1622 return (info); 1623 } 1624 1625 int 1626 vm_entry_intinfo(struct vm *vm, int vcpuid, uint64_t *retinfo) 1627 { 1628 struct vcpu *vcpu; 1629 uint64_t info1, info2; 1630 int valid; 1631 1632 KASSERT(vcpuid >= 0 && vcpuid < VM_MAXCPU, ("invalid vcpu %d", vcpuid)); 1633 1634 vcpu = &vm->vcpu[vcpuid]; 1635 1636 info1 = vcpu->exitintinfo; 1637 vcpu->exitintinfo = 0; 1638 1639 info2 = 0; 1640 if (vcpu->exception_pending) { 1641 info2 = vcpu_exception_intinfo(vcpu); 1642 vcpu->exception_pending = 0; 1643 VCPU_CTR2(vm, vcpuid, "Exception %d delivered: %#lx", 1644 vcpu->exception.vector, info2); 1645 } 1646 1647 if ((info1 & VM_INTINFO_VALID) && (info2 & VM_INTINFO_VALID)) { 1648 valid = nested_fault(vm, vcpuid, info1, info2, retinfo); 1649 } else if (info1 & VM_INTINFO_VALID) { 1650 *retinfo = info1; 1651 valid = 1; 1652 } else if (info2 & VM_INTINFO_VALID) { 1653 *retinfo = info2; 1654 valid = 1; 1655 } else { 1656 valid = 0; 1657 } 1658 1659 if (valid) { 1660 VCPU_CTR4(vm, vcpuid, "%s: info1(%#lx), info2(%#lx), " 1661 "retinfo(%#lx)", __func__, info1, info2, *retinfo); 1662 } 1663 1664 return (valid); 1665 } 1666 1667 int 1668 vm_get_intinfo(struct vm *vm, int vcpuid, uint64_t *info1, uint64_t *info2) 1669 { 1670 struct vcpu *vcpu; 1671 1672 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1673 return (EINVAL); 1674 1675 vcpu = &vm->vcpu[vcpuid]; 1676 *info1 = vcpu->exitintinfo; 1677 *info2 = vcpu_exception_intinfo(vcpu); 1678 return (0); 1679 } 1680 1681 int 1682 vm_inject_exception(struct vm *vm, int vcpuid, struct vm_exception *exception) 1683 { 1684 struct vcpu *vcpu; 1685 1686 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1687 return (EINVAL); 1688 1689 if (exception->vector < 0 || exception->vector >= 32) 1690 return (EINVAL); 1691 1692 /* 1693 * A double fault exception should never be injected directly into 1694 * the guest. It is a derived exception that results from specific 1695 * combinations of nested faults. 1696 */ 1697 if (exception->vector == IDT_DF) 1698 return (EINVAL); 1699 1700 vcpu = &vm->vcpu[vcpuid]; 1701 1702 if (vcpu->exception_pending) { 1703 VCPU_CTR2(vm, vcpuid, "Unable to inject exception %d due to " 1704 "pending exception %d", exception->vector, 1705 vcpu->exception.vector); 1706 return (EBUSY); 1707 } 1708 1709 vcpu->exception_pending = 1; 1710 vcpu->exception = *exception; 1711 VCPU_CTR1(vm, vcpuid, "Exception %d pending", exception->vector); 1712 return (0); 1713 } 1714 1715 void 1716 vm_inject_fault(void *vmarg, int vcpuid, int vector, int errcode_valid, 1717 int errcode) 1718 { 1719 struct vm_exception exception; 1720 struct vm_exit *vmexit; 1721 struct vm *vm; 1722 int error; 1723 1724 vm = vmarg; 1725 1726 exception.vector = vector; 1727 exception.error_code = errcode; 1728 exception.error_code_valid = errcode_valid; 1729 error = vm_inject_exception(vm, vcpuid, &exception); 1730 KASSERT(error == 0, ("vm_inject_exception error %d", error)); 1731 1732 /* 1733 * A fault-like exception allows the instruction to be restarted 1734 * after the exception handler returns. 1735 * 1736 * By setting the inst_length to 0 we ensure that the instruction 1737 * pointer remains at the faulting instruction. 1738 */ 1739 vmexit = vm_exitinfo(vm, vcpuid); 1740 vmexit->inst_length = 0; 1741 } 1742 1743 void 1744 vm_inject_pf(void *vmarg, int vcpuid, int error_code, uint64_t cr2) 1745 { 1746 struct vm *vm; 1747 int error; 1748 1749 vm = vmarg; 1750 VCPU_CTR2(vm, vcpuid, "Injecting page fault: error_code %#x, cr2 %#lx", 1751 error_code, cr2); 1752 1753 error = vm_set_register(vm, vcpuid, VM_REG_GUEST_CR2, cr2); 1754 KASSERT(error == 0, ("vm_set_register(cr2) error %d", error)); 1755 1756 vm_inject_fault(vm, vcpuid, IDT_PF, 1, error_code); 1757 } 1758 1759 static VMM_STAT(VCPU_NMI_COUNT, "number of NMIs delivered to vcpu"); 1760 1761 int 1762 vm_inject_nmi(struct vm *vm, int vcpuid) 1763 { 1764 struct vcpu *vcpu; 1765 1766 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1767 return (EINVAL); 1768 1769 vcpu = &vm->vcpu[vcpuid]; 1770 1771 vcpu->nmi_pending = 1; 1772 vcpu_notify_event(vm, vcpuid, false); 1773 return (0); 1774 } 1775 1776 int 1777 vm_nmi_pending(struct vm *vm, int vcpuid) 1778 { 1779 struct vcpu *vcpu; 1780 1781 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1782 panic("vm_nmi_pending: invalid vcpuid %d", vcpuid); 1783 1784 vcpu = &vm->vcpu[vcpuid]; 1785 1786 return (vcpu->nmi_pending); 1787 } 1788 1789 void 1790 vm_nmi_clear(struct vm *vm, int vcpuid) 1791 { 1792 struct vcpu *vcpu; 1793 1794 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1795 panic("vm_nmi_pending: invalid vcpuid %d", vcpuid); 1796 1797 vcpu = &vm->vcpu[vcpuid]; 1798 1799 if (vcpu->nmi_pending == 0) 1800 panic("vm_nmi_clear: inconsistent nmi_pending state"); 1801 1802 vcpu->nmi_pending = 0; 1803 vmm_stat_incr(vm, vcpuid, VCPU_NMI_COUNT, 1); 1804 } 1805 1806 static VMM_STAT(VCPU_EXTINT_COUNT, "number of ExtINTs delivered to vcpu"); 1807 1808 int 1809 vm_inject_extint(struct vm *vm, int vcpuid) 1810 { 1811 struct vcpu *vcpu; 1812 1813 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1814 return (EINVAL); 1815 1816 vcpu = &vm->vcpu[vcpuid]; 1817 1818 vcpu->extint_pending = 1; 1819 vcpu_notify_event(vm, vcpuid, false); 1820 return (0); 1821 } 1822 1823 int 1824 vm_extint_pending(struct vm *vm, int vcpuid) 1825 { 1826 struct vcpu *vcpu; 1827 1828 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1829 panic("vm_extint_pending: invalid vcpuid %d", vcpuid); 1830 1831 vcpu = &vm->vcpu[vcpuid]; 1832 1833 return (vcpu->extint_pending); 1834 } 1835 1836 void 1837 vm_extint_clear(struct vm *vm, int vcpuid) 1838 { 1839 struct vcpu *vcpu; 1840 1841 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1842 panic("vm_extint_pending: invalid vcpuid %d", vcpuid); 1843 1844 vcpu = &vm->vcpu[vcpuid]; 1845 1846 if (vcpu->extint_pending == 0) 1847 panic("vm_extint_clear: inconsistent extint_pending state"); 1848 1849 vcpu->extint_pending = 0; 1850 vmm_stat_incr(vm, vcpuid, VCPU_EXTINT_COUNT, 1); 1851 } 1852 1853 int 1854 vm_get_capability(struct vm *vm, int vcpu, int type, int *retval) 1855 { 1856 if (vcpu < 0 || vcpu >= VM_MAXCPU) 1857 return (EINVAL); 1858 1859 if (type < 0 || type >= VM_CAP_MAX) 1860 return (EINVAL); 1861 1862 return (VMGETCAP(vm->cookie, vcpu, type, retval)); 1863 } 1864 1865 int 1866 vm_set_capability(struct vm *vm, int vcpu, int type, int val) 1867 { 1868 if (vcpu < 0 || vcpu >= VM_MAXCPU) 1869 return (EINVAL); 1870 1871 if (type < 0 || type >= VM_CAP_MAX) 1872 return (EINVAL); 1873 1874 return (VMSETCAP(vm->cookie, vcpu, type, val)); 1875 } 1876 1877 uint64_t * 1878 vm_guest_msrs(struct vm *vm, int cpu) 1879 { 1880 return (vm->vcpu[cpu].guest_msrs); 1881 } 1882 1883 struct vlapic * 1884 vm_lapic(struct vm *vm, int cpu) 1885 { 1886 return (vm->vcpu[cpu].vlapic); 1887 } 1888 1889 struct vioapic * 1890 vm_ioapic(struct vm *vm) 1891 { 1892 1893 return (vm->vioapic); 1894 } 1895 1896 struct vhpet * 1897 vm_hpet(struct vm *vm) 1898 { 1899 1900 return (vm->vhpet); 1901 } 1902 1903 boolean_t 1904 vmm_is_pptdev(int bus, int slot, int func) 1905 { 1906 int found, i, n; 1907 int b, s, f; 1908 char *val, *cp, *cp2; 1909 1910 /* 1911 * XXX 1912 * The length of an environment variable is limited to 128 bytes which 1913 * puts an upper limit on the number of passthru devices that may be 1914 * specified using a single environment variable. 1915 * 1916 * Work around this by scanning multiple environment variable 1917 * names instead of a single one - yuck! 1918 */ 1919 const char *names[] = { "pptdevs", "pptdevs2", "pptdevs3", NULL }; 1920 1921 /* set pptdevs="1/2/3 4/5/6 7/8/9 10/11/12" */ 1922 found = 0; 1923 for (i = 0; names[i] != NULL && !found; i++) { 1924 cp = val = getenv(names[i]); 1925 while (cp != NULL && *cp != '\0') { 1926 if ((cp2 = strchr(cp, ' ')) != NULL) 1927 *cp2 = '\0'; 1928 1929 n = sscanf(cp, "%d/%d/%d", &b, &s, &f); 1930 if (n == 3 && bus == b && slot == s && func == f) { 1931 found = 1; 1932 break; 1933 } 1934 1935 if (cp2 != NULL) 1936 *cp2++ = ' '; 1937 1938 cp = cp2; 1939 } 1940 freeenv(val); 1941 } 1942 return (found); 1943 } 1944 1945 void * 1946 vm_iommu_domain(struct vm *vm) 1947 { 1948 1949 return (vm->iommu); 1950 } 1951 1952 int 1953 vcpu_set_state(struct vm *vm, int vcpuid, enum vcpu_state newstate, 1954 bool from_idle) 1955 { 1956 int error; 1957 struct vcpu *vcpu; 1958 1959 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1960 panic("vm_set_run_state: invalid vcpuid %d", vcpuid); 1961 1962 vcpu = &vm->vcpu[vcpuid]; 1963 1964 vcpu_lock(vcpu); 1965 error = vcpu_set_state_locked(vcpu, newstate, from_idle); 1966 vcpu_unlock(vcpu); 1967 1968 return (error); 1969 } 1970 1971 enum vcpu_state 1972 vcpu_get_state(struct vm *vm, int vcpuid, int *hostcpu) 1973 { 1974 struct vcpu *vcpu; 1975 enum vcpu_state state; 1976 1977 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1978 panic("vm_get_run_state: invalid vcpuid %d", vcpuid); 1979 1980 vcpu = &vm->vcpu[vcpuid]; 1981 1982 vcpu_lock(vcpu); 1983 state = vcpu->state; 1984 if (hostcpu != NULL) 1985 *hostcpu = vcpu->hostcpu; 1986 vcpu_unlock(vcpu); 1987 1988 return (state); 1989 } 1990 1991 int 1992 vm_activate_cpu(struct vm *vm, int vcpuid) 1993 { 1994 1995 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1996 return (EINVAL); 1997 1998 if (CPU_ISSET(vcpuid, &vm->active_cpus)) 1999 return (EBUSY); 2000 2001 VCPU_CTR0(vm, vcpuid, "activated"); 2002 CPU_SET_ATOMIC(vcpuid, &vm->active_cpus); 2003 return (0); 2004 } 2005 2006 cpuset_t 2007 vm_active_cpus(struct vm *vm) 2008 { 2009 2010 return (vm->active_cpus); 2011 } 2012 2013 cpuset_t 2014 vm_suspended_cpus(struct vm *vm) 2015 { 2016 2017 return (vm->suspended_cpus); 2018 } 2019 2020 void * 2021 vcpu_stats(struct vm *vm, int vcpuid) 2022 { 2023 2024 return (vm->vcpu[vcpuid].stats); 2025 } 2026 2027 int 2028 vm_get_x2apic_state(struct vm *vm, int vcpuid, enum x2apic_state *state) 2029 { 2030 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 2031 return (EINVAL); 2032 2033 *state = vm->vcpu[vcpuid].x2apic_state; 2034 2035 return (0); 2036 } 2037 2038 int 2039 vm_set_x2apic_state(struct vm *vm, int vcpuid, enum x2apic_state state) 2040 { 2041 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 2042 return (EINVAL); 2043 2044 if (state >= X2APIC_STATE_LAST) 2045 return (EINVAL); 2046 2047 vm->vcpu[vcpuid].x2apic_state = state; 2048 2049 vlapic_set_x2apic_state(vm, vcpuid, state); 2050 2051 return (0); 2052 } 2053 2054 /* 2055 * This function is called to ensure that a vcpu "sees" a pending event 2056 * as soon as possible: 2057 * - If the vcpu thread is sleeping then it is woken up. 2058 * - If the vcpu is running on a different host_cpu then an IPI will be directed 2059 * to the host_cpu to cause the vcpu to trap into the hypervisor. 2060 */ 2061 void 2062 vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr) 2063 { 2064 int hostcpu; 2065 struct vcpu *vcpu; 2066 2067 vcpu = &vm->vcpu[vcpuid]; 2068 2069 vcpu_lock(vcpu); 2070 hostcpu = vcpu->hostcpu; 2071 if (vcpu->state == VCPU_RUNNING) { 2072 KASSERT(hostcpu != NOCPU, ("vcpu running on invalid hostcpu")); 2073 if (hostcpu != curcpu) { 2074 if (lapic_intr) { 2075 vlapic_post_intr(vcpu->vlapic, hostcpu, 2076 vmm_ipinum); 2077 } else { 2078 ipi_cpu(hostcpu, vmm_ipinum); 2079 } 2080 } else { 2081 /* 2082 * If the 'vcpu' is running on 'curcpu' then it must 2083 * be sending a notification to itself (e.g. SELF_IPI). 2084 * The pending event will be picked up when the vcpu 2085 * transitions back to guest context. 2086 */ 2087 } 2088 } else { 2089 KASSERT(hostcpu == NOCPU, ("vcpu state %d not consistent " 2090 "with hostcpu %d", vcpu->state, hostcpu)); 2091 if (vcpu->state == VCPU_SLEEPING) 2092 wakeup_one(vcpu); 2093 } 2094 vcpu_unlock(vcpu); 2095 } 2096 2097 struct vmspace * 2098 vm_get_vmspace(struct vm *vm) 2099 { 2100 2101 return (vm->vmspace); 2102 } 2103 2104 int 2105 vm_apicid2vcpuid(struct vm *vm, int apicid) 2106 { 2107 /* 2108 * XXX apic id is assumed to be numerically identical to vcpu id 2109 */ 2110 return (apicid); 2111 } 2112 2113 void 2114 vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest, 2115 vm_rendezvous_func_t func, void *arg) 2116 { 2117 int i; 2118 2119 /* 2120 * Enforce that this function is called without any locks 2121 */ 2122 WITNESS_WARN(WARN_PANIC, NULL, "vm_smp_rendezvous"); 2123 KASSERT(vcpuid == -1 || (vcpuid >= 0 && vcpuid < VM_MAXCPU), 2124 ("vm_smp_rendezvous: invalid vcpuid %d", vcpuid)); 2125 2126 restart: 2127 mtx_lock(&vm->rendezvous_mtx); 2128 if (vm->rendezvous_func != NULL) { 2129 /* 2130 * If a rendezvous is already in progress then we need to 2131 * call the rendezvous handler in case this 'vcpuid' is one 2132 * of the targets of the rendezvous. 2133 */ 2134 RENDEZVOUS_CTR0(vm, vcpuid, "Rendezvous already in progress"); 2135 mtx_unlock(&vm->rendezvous_mtx); 2136 vm_handle_rendezvous(vm, vcpuid); 2137 goto restart; 2138 } 2139 KASSERT(vm->rendezvous_func == NULL, ("vm_smp_rendezvous: previous " 2140 "rendezvous is still in progress")); 2141 2142 RENDEZVOUS_CTR0(vm, vcpuid, "Initiating rendezvous"); 2143 vm->rendezvous_req_cpus = dest; 2144 CPU_ZERO(&vm->rendezvous_done_cpus); 2145 vm->rendezvous_arg = arg; 2146 vm_set_rendezvous_func(vm, func); 2147 mtx_unlock(&vm->rendezvous_mtx); 2148 2149 /* 2150 * Wake up any sleeping vcpus and trigger a VM-exit in any running 2151 * vcpus so they handle the rendezvous as soon as possible. 2152 */ 2153 for (i = 0; i < VM_MAXCPU; i++) { 2154 if (CPU_ISSET(i, &dest)) 2155 vcpu_notify_event(vm, i, false); 2156 } 2157 2158 vm_handle_rendezvous(vm, vcpuid); 2159 } 2160 2161 struct vatpic * 2162 vm_atpic(struct vm *vm) 2163 { 2164 return (vm->vatpic); 2165 } 2166 2167 struct vatpit * 2168 vm_atpit(struct vm *vm) 2169 { 2170 return (vm->vatpit); 2171 } 2172 2173 enum vm_reg_name 2174 vm_segment_name(int seg) 2175 { 2176 static enum vm_reg_name seg_names[] = { 2177 VM_REG_GUEST_ES, 2178 VM_REG_GUEST_CS, 2179 VM_REG_GUEST_SS, 2180 VM_REG_GUEST_DS, 2181 VM_REG_GUEST_FS, 2182 VM_REG_GUEST_GS 2183 }; 2184 2185 KASSERT(seg >= 0 && seg < nitems(seg_names), 2186 ("%s: invalid segment encoding %d", __func__, seg)); 2187 return (seg_names[seg]); 2188 } 2189 2190 void 2191 vm_copy_teardown(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo, 2192 int num_copyinfo) 2193 { 2194 int idx; 2195 2196 for (idx = 0; idx < num_copyinfo; idx++) { 2197 if (copyinfo[idx].cookie != NULL) 2198 vm_gpa_release(copyinfo[idx].cookie); 2199 } 2200 bzero(copyinfo, num_copyinfo * sizeof(struct vm_copyinfo)); 2201 } 2202 2203 int 2204 vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging, 2205 uint64_t gla, size_t len, int prot, struct vm_copyinfo *copyinfo, 2206 int num_copyinfo) 2207 { 2208 int error, idx, nused; 2209 size_t n, off, remaining; 2210 void *hva, *cookie; 2211 uint64_t gpa; 2212 2213 bzero(copyinfo, sizeof(struct vm_copyinfo) * num_copyinfo); 2214 2215 nused = 0; 2216 remaining = len; 2217 while (remaining > 0) { 2218 KASSERT(nused < num_copyinfo, ("insufficient vm_copyinfo")); 2219 error = vmm_gla2gpa(vm, vcpuid, paging, gla, prot, &gpa); 2220 if (error) 2221 return (error); 2222 off = gpa & PAGE_MASK; 2223 n = min(remaining, PAGE_SIZE - off); 2224 copyinfo[nused].gpa = gpa; 2225 copyinfo[nused].len = n; 2226 remaining -= n; 2227 gla += n; 2228 nused++; 2229 } 2230 2231 for (idx = 0; idx < nused; idx++) { 2232 hva = vm_gpa_hold(vm, copyinfo[idx].gpa, copyinfo[idx].len, 2233 prot, &cookie); 2234 if (hva == NULL) 2235 break; 2236 copyinfo[idx].hva = hva; 2237 copyinfo[idx].cookie = cookie; 2238 } 2239 2240 if (idx != nused) { 2241 vm_copy_teardown(vm, vcpuid, copyinfo, num_copyinfo); 2242 return (-1); 2243 } else { 2244 return (0); 2245 } 2246 } 2247 2248 void 2249 vm_copyin(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo, void *kaddr, 2250 size_t len) 2251 { 2252 char *dst; 2253 int idx; 2254 2255 dst = kaddr; 2256 idx = 0; 2257 while (len > 0) { 2258 bcopy(copyinfo[idx].hva, dst, copyinfo[idx].len); 2259 len -= copyinfo[idx].len; 2260 dst += copyinfo[idx].len; 2261 idx++; 2262 } 2263 } 2264 2265 void 2266 vm_copyout(struct vm *vm, int vcpuid, const void *kaddr, 2267 struct vm_copyinfo *copyinfo, size_t len) 2268 { 2269 const char *src; 2270 int idx; 2271 2272 src = kaddr; 2273 idx = 0; 2274 while (len > 0) { 2275 bcopy(src, copyinfo[idx].hva, copyinfo[idx].len); 2276 len -= copyinfo[idx].len; 2277 src += copyinfo[idx].len; 2278 idx++; 2279 } 2280 } 2281 2282 /* 2283 * Return the amount of in-use and wired memory for the VM. Since 2284 * these are global stats, only return the values with for vCPU 0 2285 */ 2286 VMM_STAT_DECLARE(VMM_MEM_RESIDENT); 2287 VMM_STAT_DECLARE(VMM_MEM_WIRED); 2288 2289 static void 2290 vm_get_rescnt(struct vm *vm, int vcpu, struct vmm_stat_type *stat) 2291 { 2292 2293 if (vcpu == 0) { 2294 vmm_stat_set(vm, vcpu, VMM_MEM_RESIDENT, 2295 PAGE_SIZE * vmspace_resident_count(vm->vmspace)); 2296 } 2297 } 2298 2299 static void 2300 vm_get_wiredcnt(struct vm *vm, int vcpu, struct vmm_stat_type *stat) 2301 { 2302 2303 if (vcpu == 0) { 2304 vmm_stat_set(vm, vcpu, VMM_MEM_WIRED, 2305 PAGE_SIZE * pmap_wired_count(vmspace_pmap(vm->vmspace))); 2306 } 2307 } 2308 2309 VMM_STAT_FUNC(VMM_MEM_RESIDENT, "Resident memory", vm_get_rescnt); 2310 VMM_STAT_FUNC(VMM_MEM_WIRED, "Wired memory", vm_get_wiredcnt); 2311