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 66 #include "vmm_ktr.h" 67 #include "vmm_host.h" 68 #include "vmm_mem.h" 69 #include "vmm_util.h" 70 #include "vhpet.h" 71 #include "vioapic.h" 72 #include "vlapic.h" 73 #include "vmm_msr.h" 74 #include "vmm_ipi.h" 75 #include "vmm_stat.h" 76 #include "vmm_lapic.h" 77 78 #include "io/ppt.h" 79 #include "io/iommu.h" 80 81 struct vlapic; 82 83 struct vcpu { 84 int flags; 85 enum vcpu_state state; 86 struct mtx mtx; 87 int hostcpu; /* host cpuid this vcpu last ran on */ 88 uint64_t guest_msrs[VMM_MSR_NUM]; 89 struct vlapic *vlapic; 90 int vcpuid; 91 struct savefpu *guestfpu; /* guest fpu state */ 92 void *stats; 93 struct vm_exit exitinfo; 94 enum x2apic_state x2apic_state; 95 int nmi_pending; 96 }; 97 98 #define vcpu_lock_init(v) mtx_init(&((v)->mtx), "vcpu lock", 0, MTX_SPIN) 99 #define vcpu_lock(v) mtx_lock_spin(&((v)->mtx)) 100 #define vcpu_unlock(v) mtx_unlock_spin(&((v)->mtx)) 101 #define vcpu_assert_locked(v) mtx_assert(&((v)->mtx), MA_OWNED) 102 103 struct mem_seg { 104 vm_paddr_t gpa; 105 size_t len; 106 boolean_t wired; 107 vm_object_t object; 108 }; 109 #define VM_MAX_MEMORY_SEGMENTS 2 110 111 struct vm { 112 void *cookie; /* processor-specific data */ 113 void *iommu; /* iommu-specific data */ 114 struct vhpet *vhpet; /* virtual HPET */ 115 struct vioapic *vioapic; /* virtual ioapic */ 116 struct vmspace *vmspace; /* guest's address space */ 117 struct vcpu vcpu[VM_MAXCPU]; 118 int num_mem_segs; 119 struct mem_seg mem_segs[VM_MAX_MEMORY_SEGMENTS]; 120 char name[VM_MAX_NAMELEN]; 121 122 /* 123 * Set of active vcpus. 124 * An active vcpu is one that has been started implicitly (BSP) or 125 * explicitly (AP) by sending it a startup ipi. 126 */ 127 cpuset_t active_cpus; 128 129 struct mtx rendezvous_mtx; 130 cpuset_t rendezvous_req_cpus; 131 cpuset_t rendezvous_done_cpus; 132 void *rendezvous_arg; 133 vm_rendezvous_func_t rendezvous_func; 134 }; 135 136 static int vmm_initialized; 137 138 static struct vmm_ops *ops; 139 #define VMM_INIT(num) (ops != NULL ? (*ops->init)(num) : 0) 140 #define VMM_CLEANUP() (ops != NULL ? (*ops->cleanup)() : 0) 141 #define VMM_RESUME() (ops != NULL ? (*ops->resume)() : 0) 142 143 #define VMINIT(vm, pmap) (ops != NULL ? (*ops->vminit)(vm, pmap): NULL) 144 #define VMRUN(vmi, vcpu, rip, pmap, rptr) \ 145 (ops != NULL ? (*ops->vmrun)(vmi, vcpu, rip, pmap, rptr) : ENXIO) 146 #define VMCLEANUP(vmi) (ops != NULL ? (*ops->vmcleanup)(vmi) : NULL) 147 #define VMSPACE_ALLOC(min, max) \ 148 (ops != NULL ? (*ops->vmspace_alloc)(min, max) : NULL) 149 #define VMSPACE_FREE(vmspace) \ 150 (ops != NULL ? (*ops->vmspace_free)(vmspace) : ENXIO) 151 #define VMGETREG(vmi, vcpu, num, retval) \ 152 (ops != NULL ? (*ops->vmgetreg)(vmi, vcpu, num, retval) : ENXIO) 153 #define VMSETREG(vmi, vcpu, num, val) \ 154 (ops != NULL ? (*ops->vmsetreg)(vmi, vcpu, num, val) : ENXIO) 155 #define VMGETDESC(vmi, vcpu, num, desc) \ 156 (ops != NULL ? (*ops->vmgetdesc)(vmi, vcpu, num, desc) : ENXIO) 157 #define VMSETDESC(vmi, vcpu, num, desc) \ 158 (ops != NULL ? (*ops->vmsetdesc)(vmi, vcpu, num, desc) : ENXIO) 159 #define VMINJECT(vmi, vcpu, type, vec, ec, ecv) \ 160 (ops != NULL ? (*ops->vminject)(vmi, vcpu, type, vec, ec, ecv) : ENXIO) 161 #define VMGETCAP(vmi, vcpu, num, retval) \ 162 (ops != NULL ? (*ops->vmgetcap)(vmi, vcpu, num, retval) : ENXIO) 163 #define VMSETCAP(vmi, vcpu, num, val) \ 164 (ops != NULL ? (*ops->vmsetcap)(vmi, vcpu, num, val) : ENXIO) 165 #define VLAPIC_INIT(vmi, vcpu) \ 166 (ops != NULL ? (*ops->vlapic_init)(vmi, vcpu) : NULL) 167 #define VLAPIC_CLEANUP(vmi, vlapic) \ 168 (ops != NULL ? (*ops->vlapic_cleanup)(vmi, vlapic) : NULL) 169 170 #define fpu_start_emulating() load_cr0(rcr0() | CR0_TS) 171 #define fpu_stop_emulating() clts() 172 173 static MALLOC_DEFINE(M_VM, "vm", "vm"); 174 CTASSERT(VMM_MSR_NUM <= 64); /* msr_mask can keep track of up to 64 msrs */ 175 176 /* statistics */ 177 static VMM_STAT(VCPU_TOTAL_RUNTIME, "vcpu total runtime"); 178 179 SYSCTL_NODE(_hw, OID_AUTO, vmm, CTLFLAG_RW, NULL, NULL); 180 181 static int vmm_ipinum; 182 SYSCTL_INT(_hw_vmm, OID_AUTO, ipinum, CTLFLAG_RD, &vmm_ipinum, 0, 183 "IPI vector used for vcpu notifications"); 184 185 static void vm_deactivate_cpu(struct vm *vm, int vcpuid); 186 187 static void 188 vcpu_cleanup(struct vm *vm, int i) 189 { 190 struct vcpu *vcpu = &vm->vcpu[i]; 191 192 VLAPIC_CLEANUP(vm->cookie, vcpu->vlapic); 193 vmm_stat_free(vcpu->stats); 194 fpu_save_area_free(vcpu->guestfpu); 195 } 196 197 static void 198 vcpu_init(struct vm *vm, uint32_t vcpu_id) 199 { 200 struct vcpu *vcpu; 201 202 vcpu = &vm->vcpu[vcpu_id]; 203 204 vcpu_lock_init(vcpu); 205 vcpu->hostcpu = NOCPU; 206 vcpu->vcpuid = vcpu_id; 207 vcpu->vlapic = VLAPIC_INIT(vm->cookie, vcpu_id); 208 vm_set_x2apic_state(vm, vcpu_id, X2APIC_ENABLED); 209 vcpu->guestfpu = fpu_save_area_alloc(); 210 fpu_save_area_reset(vcpu->guestfpu); 211 vcpu->stats = vmm_stat_alloc(); 212 } 213 214 struct vm_exit * 215 vm_exitinfo(struct vm *vm, int cpuid) 216 { 217 struct vcpu *vcpu; 218 219 if (cpuid < 0 || cpuid >= VM_MAXCPU) 220 panic("vm_exitinfo: invalid cpuid %d", cpuid); 221 222 vcpu = &vm->vcpu[cpuid]; 223 224 return (&vcpu->exitinfo); 225 } 226 227 static void 228 vmm_resume(void) 229 { 230 VMM_RESUME(); 231 } 232 233 static int 234 vmm_init(void) 235 { 236 int error; 237 238 vmm_host_state_init(); 239 240 vmm_ipinum = vmm_ipi_alloc(); 241 if (vmm_ipinum == 0) 242 vmm_ipinum = IPI_AST; 243 244 error = vmm_mem_init(); 245 if (error) 246 return (error); 247 248 if (vmm_is_intel()) 249 ops = &vmm_ops_intel; 250 else if (vmm_is_amd()) 251 ops = &vmm_ops_amd; 252 else 253 return (ENXIO); 254 255 vmm_msr_init(); 256 vmm_resume_p = vmm_resume; 257 258 return (VMM_INIT(vmm_ipinum)); 259 } 260 261 static int 262 vmm_handler(module_t mod, int what, void *arg) 263 { 264 int error; 265 266 switch (what) { 267 case MOD_LOAD: 268 vmmdev_init(); 269 if (ppt_avail_devices() > 0) 270 iommu_init(); 271 error = vmm_init(); 272 if (error == 0) 273 vmm_initialized = 1; 274 break; 275 case MOD_UNLOAD: 276 error = vmmdev_cleanup(); 277 if (error == 0) { 278 vmm_resume_p = NULL; 279 iommu_cleanup(); 280 if (vmm_ipinum != IPI_AST) 281 vmm_ipi_free(vmm_ipinum); 282 error = VMM_CLEANUP(); 283 /* 284 * Something bad happened - prevent new 285 * VMs from being created 286 */ 287 if (error) 288 vmm_initialized = 0; 289 } 290 break; 291 default: 292 error = 0; 293 break; 294 } 295 return (error); 296 } 297 298 static moduledata_t vmm_kmod = { 299 "vmm", 300 vmm_handler, 301 NULL 302 }; 303 304 /* 305 * vmm initialization has the following dependencies: 306 * 307 * - iommu initialization must happen after the pci passthru driver has had 308 * a chance to attach to any passthru devices (after SI_SUB_CONFIGURE). 309 * 310 * - VT-x initialization requires smp_rendezvous() and therefore must happen 311 * after SMP is fully functional (after SI_SUB_SMP). 312 */ 313 DECLARE_MODULE(vmm, vmm_kmod, SI_SUB_SMP + 1, SI_ORDER_ANY); 314 MODULE_VERSION(vmm, 1); 315 316 int 317 vm_create(const char *name, struct vm **retvm) 318 { 319 int i; 320 struct vm *vm; 321 struct vmspace *vmspace; 322 323 const int BSP = 0; 324 325 /* 326 * If vmm.ko could not be successfully initialized then don't attempt 327 * to create the virtual machine. 328 */ 329 if (!vmm_initialized) 330 return (ENXIO); 331 332 if (name == NULL || strlen(name) >= VM_MAX_NAMELEN) 333 return (EINVAL); 334 335 vmspace = VMSPACE_ALLOC(VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS); 336 if (vmspace == NULL) 337 return (ENOMEM); 338 339 vm = malloc(sizeof(struct vm), M_VM, M_WAITOK | M_ZERO); 340 strcpy(vm->name, name); 341 vm->vmspace = vmspace; 342 mtx_init(&vm->rendezvous_mtx, "vm rendezvous lock", 0, MTX_DEF); 343 vm->cookie = VMINIT(vm, vmspace_pmap(vmspace)); 344 vm->vioapic = vioapic_init(vm); 345 vm->vhpet = vhpet_init(vm); 346 347 for (i = 0; i < VM_MAXCPU; i++) { 348 vcpu_init(vm, i); 349 guest_msrs_init(vm, i); 350 } 351 352 vm_activate_cpu(vm, BSP); 353 354 *retvm = vm; 355 return (0); 356 } 357 358 static void 359 vm_free_mem_seg(struct vm *vm, struct mem_seg *seg) 360 { 361 362 if (seg->object != NULL) 363 vmm_mem_free(vm->vmspace, seg->gpa, seg->len); 364 365 bzero(seg, sizeof(*seg)); 366 } 367 368 void 369 vm_destroy(struct vm *vm) 370 { 371 int i; 372 373 ppt_unassign_all(vm); 374 375 if (vm->iommu != NULL) 376 iommu_destroy_domain(vm->iommu); 377 378 vhpet_cleanup(vm->vhpet); 379 vioapic_cleanup(vm->vioapic); 380 381 for (i = 0; i < vm->num_mem_segs; i++) 382 vm_free_mem_seg(vm, &vm->mem_segs[i]); 383 384 vm->num_mem_segs = 0; 385 386 for (i = 0; i < VM_MAXCPU; i++) 387 vcpu_cleanup(vm, i); 388 389 VMSPACE_FREE(vm->vmspace); 390 391 VMCLEANUP(vm->cookie); 392 393 free(vm, M_VM); 394 } 395 396 const char * 397 vm_name(struct vm *vm) 398 { 399 return (vm->name); 400 } 401 402 int 403 vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa) 404 { 405 vm_object_t obj; 406 407 if ((obj = vmm_mmio_alloc(vm->vmspace, gpa, len, hpa)) == NULL) 408 return (ENOMEM); 409 else 410 return (0); 411 } 412 413 int 414 vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len) 415 { 416 417 vmm_mmio_free(vm->vmspace, gpa, len); 418 return (0); 419 } 420 421 boolean_t 422 vm_mem_allocated(struct vm *vm, vm_paddr_t gpa) 423 { 424 int i; 425 vm_paddr_t gpabase, gpalimit; 426 427 for (i = 0; i < vm->num_mem_segs; i++) { 428 gpabase = vm->mem_segs[i].gpa; 429 gpalimit = gpabase + vm->mem_segs[i].len; 430 if (gpa >= gpabase && gpa < gpalimit) 431 return (TRUE); /* 'gpa' is regular memory */ 432 } 433 434 if (ppt_is_mmio(vm, gpa)) 435 return (TRUE); /* 'gpa' is pci passthru mmio */ 436 437 return (FALSE); 438 } 439 440 int 441 vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len) 442 { 443 int available, allocated; 444 struct mem_seg *seg; 445 vm_object_t object; 446 vm_paddr_t g; 447 448 if ((gpa & PAGE_MASK) || (len & PAGE_MASK) || len == 0) 449 return (EINVAL); 450 451 available = allocated = 0; 452 g = gpa; 453 while (g < gpa + len) { 454 if (vm_mem_allocated(vm, g)) 455 allocated++; 456 else 457 available++; 458 459 g += PAGE_SIZE; 460 } 461 462 /* 463 * If there are some allocated and some available pages in the address 464 * range then it is an error. 465 */ 466 if (allocated && available) 467 return (EINVAL); 468 469 /* 470 * If the entire address range being requested has already been 471 * allocated then there isn't anything more to do. 472 */ 473 if (allocated && available == 0) 474 return (0); 475 476 if (vm->num_mem_segs >= VM_MAX_MEMORY_SEGMENTS) 477 return (E2BIG); 478 479 seg = &vm->mem_segs[vm->num_mem_segs]; 480 481 if ((object = vmm_mem_alloc(vm->vmspace, gpa, len)) == NULL) 482 return (ENOMEM); 483 484 seg->gpa = gpa; 485 seg->len = len; 486 seg->object = object; 487 seg->wired = FALSE; 488 489 vm->num_mem_segs++; 490 491 return (0); 492 } 493 494 static void 495 vm_gpa_unwire(struct vm *vm) 496 { 497 int i, rv; 498 struct mem_seg *seg; 499 500 for (i = 0; i < vm->num_mem_segs; i++) { 501 seg = &vm->mem_segs[i]; 502 if (!seg->wired) 503 continue; 504 505 rv = vm_map_unwire(&vm->vmspace->vm_map, 506 seg->gpa, seg->gpa + seg->len, 507 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES); 508 KASSERT(rv == KERN_SUCCESS, ("vm(%s) memory segment " 509 "%#lx/%ld could not be unwired: %d", 510 vm_name(vm), seg->gpa, seg->len, rv)); 511 512 seg->wired = FALSE; 513 } 514 } 515 516 static int 517 vm_gpa_wire(struct vm *vm) 518 { 519 int i, rv; 520 struct mem_seg *seg; 521 522 for (i = 0; i < vm->num_mem_segs; i++) { 523 seg = &vm->mem_segs[i]; 524 if (seg->wired) 525 continue; 526 527 /* XXX rlimits? */ 528 rv = vm_map_wire(&vm->vmspace->vm_map, 529 seg->gpa, seg->gpa + seg->len, 530 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES); 531 if (rv != KERN_SUCCESS) 532 break; 533 534 seg->wired = TRUE; 535 } 536 537 if (i < vm->num_mem_segs) { 538 /* 539 * Undo the wiring before returning an error. 540 */ 541 vm_gpa_unwire(vm); 542 return (EAGAIN); 543 } 544 545 return (0); 546 } 547 548 static void 549 vm_iommu_modify(struct vm *vm, boolean_t map) 550 { 551 int i, sz; 552 vm_paddr_t gpa, hpa; 553 struct mem_seg *seg; 554 void *vp, *cookie, *host_domain; 555 556 sz = PAGE_SIZE; 557 host_domain = iommu_host_domain(); 558 559 for (i = 0; i < vm->num_mem_segs; i++) { 560 seg = &vm->mem_segs[i]; 561 KASSERT(seg->wired, ("vm(%s) memory segment %#lx/%ld not wired", 562 vm_name(vm), seg->gpa, seg->len)); 563 564 gpa = seg->gpa; 565 while (gpa < seg->gpa + seg->len) { 566 vp = vm_gpa_hold(vm, gpa, PAGE_SIZE, VM_PROT_WRITE, 567 &cookie); 568 KASSERT(vp != NULL, ("vm(%s) could not map gpa %#lx", 569 vm_name(vm), gpa)); 570 571 vm_gpa_release(cookie); 572 573 hpa = DMAP_TO_PHYS((uintptr_t)vp); 574 if (map) { 575 iommu_create_mapping(vm->iommu, gpa, hpa, sz); 576 iommu_remove_mapping(host_domain, hpa, sz); 577 } else { 578 iommu_remove_mapping(vm->iommu, gpa, sz); 579 iommu_create_mapping(host_domain, hpa, hpa, sz); 580 } 581 582 gpa += PAGE_SIZE; 583 } 584 } 585 586 /* 587 * Invalidate the cached translations associated with the domain 588 * from which pages were removed. 589 */ 590 if (map) 591 iommu_invalidate_tlb(host_domain); 592 else 593 iommu_invalidate_tlb(vm->iommu); 594 } 595 596 #define vm_iommu_unmap(vm) vm_iommu_modify((vm), FALSE) 597 #define vm_iommu_map(vm) vm_iommu_modify((vm), TRUE) 598 599 int 600 vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func) 601 { 602 int error; 603 604 error = ppt_unassign_device(vm, bus, slot, func); 605 if (error) 606 return (error); 607 608 if (ppt_assigned_devices(vm) == 0) { 609 vm_iommu_unmap(vm); 610 vm_gpa_unwire(vm); 611 } 612 return (0); 613 } 614 615 int 616 vm_assign_pptdev(struct vm *vm, int bus, int slot, int func) 617 { 618 int error; 619 vm_paddr_t maxaddr; 620 621 /* 622 * Virtual machines with pci passthru devices get special treatment: 623 * - the guest physical memory is wired 624 * - the iommu is programmed to do the 'gpa' to 'hpa' translation 625 * 626 * We need to do this before the first pci passthru device is attached. 627 */ 628 if (ppt_assigned_devices(vm) == 0) { 629 KASSERT(vm->iommu == NULL, 630 ("vm_assign_pptdev: iommu must be NULL")); 631 maxaddr = vmm_mem_maxaddr(); 632 vm->iommu = iommu_create_domain(maxaddr); 633 634 error = vm_gpa_wire(vm); 635 if (error) 636 return (error); 637 638 vm_iommu_map(vm); 639 } 640 641 error = ppt_assign_device(vm, bus, slot, func); 642 return (error); 643 } 644 645 void * 646 vm_gpa_hold(struct vm *vm, vm_paddr_t gpa, size_t len, int reqprot, 647 void **cookie) 648 { 649 int count, pageoff; 650 vm_page_t m; 651 652 pageoff = gpa & PAGE_MASK; 653 if (len > PAGE_SIZE - pageoff) 654 panic("vm_gpa_hold: invalid gpa/len: 0x%016lx/%lu", gpa, len); 655 656 count = vm_fault_quick_hold_pages(&vm->vmspace->vm_map, 657 trunc_page(gpa), PAGE_SIZE, reqprot, &m, 1); 658 659 if (count == 1) { 660 *cookie = m; 661 return ((void *)(PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)) + pageoff)); 662 } else { 663 *cookie = NULL; 664 return (NULL); 665 } 666 } 667 668 void 669 vm_gpa_release(void *cookie) 670 { 671 vm_page_t m = cookie; 672 673 vm_page_lock(m); 674 vm_page_unhold(m); 675 vm_page_unlock(m); 676 } 677 678 int 679 vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, 680 struct vm_memory_segment *seg) 681 { 682 int i; 683 684 for (i = 0; i < vm->num_mem_segs; i++) { 685 if (gpabase == vm->mem_segs[i].gpa) { 686 seg->gpa = vm->mem_segs[i].gpa; 687 seg->len = vm->mem_segs[i].len; 688 seg->wired = vm->mem_segs[i].wired; 689 return (0); 690 } 691 } 692 return (-1); 693 } 694 695 int 696 vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len, 697 vm_offset_t *offset, struct vm_object **object) 698 { 699 int i; 700 size_t seg_len; 701 vm_paddr_t seg_gpa; 702 vm_object_t seg_obj; 703 704 for (i = 0; i < vm->num_mem_segs; i++) { 705 if ((seg_obj = vm->mem_segs[i].object) == NULL) 706 continue; 707 708 seg_gpa = vm->mem_segs[i].gpa; 709 seg_len = vm->mem_segs[i].len; 710 711 if (gpa >= seg_gpa && gpa < seg_gpa + seg_len) { 712 *offset = gpa - seg_gpa; 713 *object = seg_obj; 714 vm_object_reference(seg_obj); 715 return (0); 716 } 717 } 718 719 return (EINVAL); 720 } 721 722 int 723 vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval) 724 { 725 726 if (vcpu < 0 || vcpu >= VM_MAXCPU) 727 return (EINVAL); 728 729 if (reg >= VM_REG_LAST) 730 return (EINVAL); 731 732 return (VMGETREG(vm->cookie, vcpu, reg, retval)); 733 } 734 735 int 736 vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val) 737 { 738 739 if (vcpu < 0 || vcpu >= VM_MAXCPU) 740 return (EINVAL); 741 742 if (reg >= VM_REG_LAST) 743 return (EINVAL); 744 745 return (VMSETREG(vm->cookie, vcpu, reg, val)); 746 } 747 748 static boolean_t 749 is_descriptor_table(int reg) 750 { 751 752 switch (reg) { 753 case VM_REG_GUEST_IDTR: 754 case VM_REG_GUEST_GDTR: 755 return (TRUE); 756 default: 757 return (FALSE); 758 } 759 } 760 761 static boolean_t 762 is_segment_register(int reg) 763 { 764 765 switch (reg) { 766 case VM_REG_GUEST_ES: 767 case VM_REG_GUEST_CS: 768 case VM_REG_GUEST_SS: 769 case VM_REG_GUEST_DS: 770 case VM_REG_GUEST_FS: 771 case VM_REG_GUEST_GS: 772 case VM_REG_GUEST_TR: 773 case VM_REG_GUEST_LDTR: 774 return (TRUE); 775 default: 776 return (FALSE); 777 } 778 } 779 780 int 781 vm_get_seg_desc(struct vm *vm, int vcpu, int reg, 782 struct seg_desc *desc) 783 { 784 785 if (vcpu < 0 || vcpu >= VM_MAXCPU) 786 return (EINVAL); 787 788 if (!is_segment_register(reg) && !is_descriptor_table(reg)) 789 return (EINVAL); 790 791 return (VMGETDESC(vm->cookie, vcpu, reg, desc)); 792 } 793 794 int 795 vm_set_seg_desc(struct vm *vm, int vcpu, int reg, 796 struct seg_desc *desc) 797 { 798 if (vcpu < 0 || vcpu >= VM_MAXCPU) 799 return (EINVAL); 800 801 if (!is_segment_register(reg) && !is_descriptor_table(reg)) 802 return (EINVAL); 803 804 return (VMSETDESC(vm->cookie, vcpu, reg, desc)); 805 } 806 807 static void 808 restore_guest_fpustate(struct vcpu *vcpu) 809 { 810 811 /* flush host state to the pcb */ 812 fpuexit(curthread); 813 814 /* restore guest FPU state */ 815 fpu_stop_emulating(); 816 fpurestore(vcpu->guestfpu); 817 818 /* 819 * The FPU is now "dirty" with the guest's state so turn on emulation 820 * to trap any access to the FPU by the host. 821 */ 822 fpu_start_emulating(); 823 } 824 825 static void 826 save_guest_fpustate(struct vcpu *vcpu) 827 { 828 829 if ((rcr0() & CR0_TS) == 0) 830 panic("fpu emulation not enabled in host!"); 831 832 /* save guest FPU state */ 833 fpu_stop_emulating(); 834 fpusave(vcpu->guestfpu); 835 fpu_start_emulating(); 836 } 837 838 static VMM_STAT(VCPU_IDLE_TICKS, "number of ticks vcpu was idle"); 839 840 static int 841 vcpu_set_state_locked(struct vcpu *vcpu, enum vcpu_state newstate, 842 bool from_idle) 843 { 844 int error; 845 846 vcpu_assert_locked(vcpu); 847 848 /* 849 * State transitions from the vmmdev_ioctl() must always begin from 850 * the VCPU_IDLE state. This guarantees that there is only a single 851 * ioctl() operating on a vcpu at any point. 852 */ 853 if (from_idle) { 854 while (vcpu->state != VCPU_IDLE) 855 msleep_spin(&vcpu->state, &vcpu->mtx, "vmstat", hz); 856 } else { 857 KASSERT(vcpu->state != VCPU_IDLE, ("invalid transition from " 858 "vcpu idle state")); 859 } 860 861 /* 862 * The following state transitions are allowed: 863 * IDLE -> FROZEN -> IDLE 864 * FROZEN -> RUNNING -> FROZEN 865 * FROZEN -> SLEEPING -> FROZEN 866 */ 867 switch (vcpu->state) { 868 case VCPU_IDLE: 869 case VCPU_RUNNING: 870 case VCPU_SLEEPING: 871 error = (newstate != VCPU_FROZEN); 872 break; 873 case VCPU_FROZEN: 874 error = (newstate == VCPU_FROZEN); 875 break; 876 default: 877 error = 1; 878 break; 879 } 880 881 if (error) 882 return (EBUSY); 883 884 vcpu->state = newstate; 885 if (newstate == VCPU_IDLE) 886 wakeup(&vcpu->state); 887 888 return (0); 889 } 890 891 static void 892 vcpu_require_state(struct vm *vm, int vcpuid, enum vcpu_state newstate) 893 { 894 int error; 895 896 if ((error = vcpu_set_state(vm, vcpuid, newstate, false)) != 0) 897 panic("Error %d setting state to %d\n", error, newstate); 898 } 899 900 static void 901 vcpu_require_state_locked(struct vcpu *vcpu, enum vcpu_state newstate) 902 { 903 int error; 904 905 if ((error = vcpu_set_state_locked(vcpu, newstate, false)) != 0) 906 panic("Error %d setting state to %d", error, newstate); 907 } 908 909 static void 910 vm_set_rendezvous_func(struct vm *vm, vm_rendezvous_func_t func) 911 { 912 913 KASSERT(mtx_owned(&vm->rendezvous_mtx), ("rendezvous_mtx not locked")); 914 915 /* 916 * Update 'rendezvous_func' and execute a write memory barrier to 917 * ensure that it is visible across all host cpus. This is not needed 918 * for correctness but it does ensure that all the vcpus will notice 919 * that the rendezvous is requested immediately. 920 */ 921 vm->rendezvous_func = func; 922 wmb(); 923 } 924 925 #define RENDEZVOUS_CTR0(vm, vcpuid, fmt) \ 926 do { \ 927 if (vcpuid >= 0) \ 928 VCPU_CTR0(vm, vcpuid, fmt); \ 929 else \ 930 VM_CTR0(vm, fmt); \ 931 } while (0) 932 933 static void 934 vm_handle_rendezvous(struct vm *vm, int vcpuid) 935 { 936 937 KASSERT(vcpuid == -1 || (vcpuid >= 0 && vcpuid < VM_MAXCPU), 938 ("vm_handle_rendezvous: invalid vcpuid %d", vcpuid)); 939 940 mtx_lock(&vm->rendezvous_mtx); 941 while (vm->rendezvous_func != NULL) { 942 if (vcpuid != -1 && 943 CPU_ISSET(vcpuid, &vm->rendezvous_req_cpus)) { 944 VCPU_CTR0(vm, vcpuid, "Calling rendezvous func"); 945 (*vm->rendezvous_func)(vm, vcpuid, vm->rendezvous_arg); 946 CPU_SET(vcpuid, &vm->rendezvous_done_cpus); 947 } 948 if (CPU_CMP(&vm->rendezvous_req_cpus, 949 &vm->rendezvous_done_cpus) == 0) { 950 VCPU_CTR0(vm, vcpuid, "Rendezvous completed"); 951 vm_set_rendezvous_func(vm, NULL); 952 wakeup(&vm->rendezvous_func); 953 break; 954 } 955 RENDEZVOUS_CTR0(vm, vcpuid, "Wait for rendezvous completion"); 956 mtx_sleep(&vm->rendezvous_func, &vm->rendezvous_mtx, 0, 957 "vmrndv", 0); 958 } 959 mtx_unlock(&vm->rendezvous_mtx); 960 } 961 962 /* 963 * Emulate a guest 'hlt' by sleeping until the vcpu is ready to run. 964 */ 965 static int 966 vm_handle_hlt(struct vm *vm, int vcpuid, bool intr_disabled, bool *retu) 967 { 968 struct vm_exit *vmexit; 969 struct vcpu *vcpu; 970 int t, timo; 971 972 vcpu = &vm->vcpu[vcpuid]; 973 974 vcpu_lock(vcpu); 975 976 /* 977 * Do a final check for pending NMI or interrupts before 978 * really putting this thread to sleep. 979 * 980 * These interrupts could have happened any time after we 981 * returned from VMRUN() and before we grabbed the vcpu lock. 982 */ 983 if (!vm_nmi_pending(vm, vcpuid) && 984 (intr_disabled || !vlapic_pending_intr(vcpu->vlapic, NULL))) { 985 t = ticks; 986 vcpu_require_state_locked(vcpu, VCPU_SLEEPING); 987 if (vlapic_enabled(vcpu->vlapic)) { 988 /* 989 * XXX msleep_spin() is not interruptible so use the 990 * 'timo' to put an upper bound on the sleep time. 991 */ 992 timo = hz; 993 msleep_spin(vcpu, &vcpu->mtx, "vmidle", timo); 994 } else { 995 /* 996 * Spindown the vcpu if the apic is disabled and it 997 * had entered the halted state. 998 */ 999 *retu = true; 1000 vmexit = vm_exitinfo(vm, vcpuid); 1001 vmexit->exitcode = VM_EXITCODE_SPINDOWN_CPU; 1002 vm_deactivate_cpu(vm, vcpuid); 1003 VCPU_CTR0(vm, vcpuid, "spinning down cpu"); 1004 } 1005 vcpu_require_state_locked(vcpu, VCPU_FROZEN); 1006 vmm_stat_incr(vm, vcpuid, VCPU_IDLE_TICKS, ticks - t); 1007 } 1008 vcpu_unlock(vcpu); 1009 1010 return (0); 1011 } 1012 1013 static int 1014 vm_handle_paging(struct vm *vm, int vcpuid, bool *retu) 1015 { 1016 int rv, ftype; 1017 struct vm_map *map; 1018 struct vcpu *vcpu; 1019 struct vm_exit *vme; 1020 1021 vcpu = &vm->vcpu[vcpuid]; 1022 vme = &vcpu->exitinfo; 1023 1024 ftype = vme->u.paging.fault_type; 1025 KASSERT(ftype == VM_PROT_READ || 1026 ftype == VM_PROT_WRITE || ftype == VM_PROT_EXECUTE, 1027 ("vm_handle_paging: invalid fault_type %d", ftype)); 1028 1029 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) { 1030 rv = pmap_emulate_accessed_dirty(vmspace_pmap(vm->vmspace), 1031 vme->u.paging.gpa, ftype); 1032 if (rv == 0) 1033 goto done; 1034 } 1035 1036 map = &vm->vmspace->vm_map; 1037 rv = vm_fault(map, vme->u.paging.gpa, ftype, VM_FAULT_NORMAL); 1038 1039 VCPU_CTR3(vm, vcpuid, "vm_handle_paging rv = %d, gpa = %#lx, " 1040 "ftype = %d", rv, vme->u.paging.gpa, ftype); 1041 1042 if (rv != KERN_SUCCESS) 1043 return (EFAULT); 1044 done: 1045 /* restart execution at the faulting instruction */ 1046 vme->inst_length = 0; 1047 1048 return (0); 1049 } 1050 1051 static int 1052 vm_handle_inst_emul(struct vm *vm, int vcpuid, bool *retu) 1053 { 1054 struct vie *vie; 1055 struct vcpu *vcpu; 1056 struct vm_exit *vme; 1057 int error, inst_length; 1058 uint64_t rip, gla, gpa, cr3; 1059 mem_region_read_t mread; 1060 mem_region_write_t mwrite; 1061 1062 vcpu = &vm->vcpu[vcpuid]; 1063 vme = &vcpu->exitinfo; 1064 1065 rip = vme->rip; 1066 inst_length = vme->inst_length; 1067 1068 gla = vme->u.inst_emul.gla; 1069 gpa = vme->u.inst_emul.gpa; 1070 cr3 = vme->u.inst_emul.cr3; 1071 vie = &vme->u.inst_emul.vie; 1072 1073 vie_init(vie); 1074 1075 /* Fetch, decode and emulate the faulting instruction */ 1076 if (vmm_fetch_instruction(vm, vcpuid, rip, inst_length, cr3, vie) != 0) 1077 return (EFAULT); 1078 1079 if (vmm_decode_instruction(vm, vcpuid, gla, vie) != 0) 1080 return (EFAULT); 1081 1082 /* return to userland unless this is an in-kernel emulated device */ 1083 if (gpa >= DEFAULT_APIC_BASE && gpa < DEFAULT_APIC_BASE + PAGE_SIZE) { 1084 mread = lapic_mmio_read; 1085 mwrite = lapic_mmio_write; 1086 } else if (gpa >= VIOAPIC_BASE && gpa < VIOAPIC_BASE + VIOAPIC_SIZE) { 1087 mread = vioapic_mmio_read; 1088 mwrite = vioapic_mmio_write; 1089 } else if (gpa >= VHPET_BASE && gpa < VHPET_BASE + VHPET_SIZE) { 1090 mread = vhpet_mmio_read; 1091 mwrite = vhpet_mmio_write; 1092 } else { 1093 *retu = true; 1094 return (0); 1095 } 1096 1097 error = vmm_emulate_instruction(vm, vcpuid, gpa, vie, mread, mwrite, 1098 retu); 1099 1100 return (error); 1101 } 1102 1103 int 1104 vm_run(struct vm *vm, struct vm_run *vmrun) 1105 { 1106 int error, vcpuid; 1107 struct vcpu *vcpu; 1108 struct pcb *pcb; 1109 uint64_t tscval, rip; 1110 struct vm_exit *vme; 1111 bool retu, intr_disabled; 1112 pmap_t pmap; 1113 1114 vcpuid = vmrun->cpuid; 1115 1116 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1117 return (EINVAL); 1118 1119 pmap = vmspace_pmap(vm->vmspace); 1120 vcpu = &vm->vcpu[vcpuid]; 1121 vme = &vcpu->exitinfo; 1122 rip = vmrun->rip; 1123 restart: 1124 critical_enter(); 1125 1126 KASSERT(!CPU_ISSET(curcpu, &pmap->pm_active), 1127 ("vm_run: absurd pm_active")); 1128 1129 tscval = rdtsc(); 1130 1131 pcb = PCPU_GET(curpcb); 1132 set_pcb_flags(pcb, PCB_FULL_IRET); 1133 1134 restore_guest_msrs(vm, vcpuid); 1135 restore_guest_fpustate(vcpu); 1136 1137 vcpu_require_state(vm, vcpuid, VCPU_RUNNING); 1138 vcpu->hostcpu = curcpu; 1139 error = VMRUN(vm->cookie, vcpuid, rip, pmap, &vm->rendezvous_func); 1140 vcpu->hostcpu = NOCPU; 1141 vcpu_require_state(vm, vcpuid, VCPU_FROZEN); 1142 1143 save_guest_fpustate(vcpu); 1144 restore_host_msrs(vm, vcpuid); 1145 1146 vmm_stat_incr(vm, vcpuid, VCPU_TOTAL_RUNTIME, rdtsc() - tscval); 1147 1148 critical_exit(); 1149 1150 if (error == 0) { 1151 retu = false; 1152 switch (vme->exitcode) { 1153 case VM_EXITCODE_IOAPIC_EOI: 1154 vioapic_process_eoi(vm, vcpuid, 1155 vme->u.ioapic_eoi.vector); 1156 break; 1157 case VM_EXITCODE_RENDEZVOUS: 1158 vm_handle_rendezvous(vm, vcpuid); 1159 error = 0; 1160 break; 1161 case VM_EXITCODE_HLT: 1162 intr_disabled = ((vme->u.hlt.rflags & PSL_I) == 0); 1163 error = vm_handle_hlt(vm, vcpuid, intr_disabled, &retu); 1164 break; 1165 case VM_EXITCODE_PAGING: 1166 error = vm_handle_paging(vm, vcpuid, &retu); 1167 break; 1168 case VM_EXITCODE_INST_EMUL: 1169 error = vm_handle_inst_emul(vm, vcpuid, &retu); 1170 break; 1171 default: 1172 retu = true; /* handled in userland */ 1173 break; 1174 } 1175 } 1176 1177 if (error == 0 && retu == false) { 1178 rip = vme->rip + vme->inst_length; 1179 goto restart; 1180 } 1181 1182 /* copy the exit information */ 1183 bcopy(vme, &vmrun->vm_exit, sizeof(struct vm_exit)); 1184 return (error); 1185 } 1186 1187 int 1188 vm_inject_event(struct vm *vm, int vcpuid, int type, 1189 int vector, uint32_t code, int code_valid) 1190 { 1191 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1192 return (EINVAL); 1193 1194 if ((type > VM_EVENT_NONE && type < VM_EVENT_MAX) == 0) 1195 return (EINVAL); 1196 1197 if (vector < 0 || vector > 255) 1198 return (EINVAL); 1199 1200 return (VMINJECT(vm->cookie, vcpuid, type, vector, code, code_valid)); 1201 } 1202 1203 static VMM_STAT(VCPU_NMI_COUNT, "number of NMIs delivered to vcpu"); 1204 1205 int 1206 vm_inject_nmi(struct vm *vm, int vcpuid) 1207 { 1208 struct vcpu *vcpu; 1209 1210 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1211 return (EINVAL); 1212 1213 vcpu = &vm->vcpu[vcpuid]; 1214 1215 vcpu->nmi_pending = 1; 1216 vcpu_notify_event(vm, vcpuid, false); 1217 return (0); 1218 } 1219 1220 int 1221 vm_nmi_pending(struct vm *vm, int vcpuid) 1222 { 1223 struct vcpu *vcpu; 1224 1225 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1226 panic("vm_nmi_pending: invalid vcpuid %d", vcpuid); 1227 1228 vcpu = &vm->vcpu[vcpuid]; 1229 1230 return (vcpu->nmi_pending); 1231 } 1232 1233 void 1234 vm_nmi_clear(struct vm *vm, int vcpuid) 1235 { 1236 struct vcpu *vcpu; 1237 1238 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1239 panic("vm_nmi_pending: invalid vcpuid %d", vcpuid); 1240 1241 vcpu = &vm->vcpu[vcpuid]; 1242 1243 if (vcpu->nmi_pending == 0) 1244 panic("vm_nmi_clear: inconsistent nmi_pending state"); 1245 1246 vcpu->nmi_pending = 0; 1247 vmm_stat_incr(vm, vcpuid, VCPU_NMI_COUNT, 1); 1248 } 1249 1250 int 1251 vm_get_capability(struct vm *vm, int vcpu, int type, int *retval) 1252 { 1253 if (vcpu < 0 || vcpu >= VM_MAXCPU) 1254 return (EINVAL); 1255 1256 if (type < 0 || type >= VM_CAP_MAX) 1257 return (EINVAL); 1258 1259 return (VMGETCAP(vm->cookie, vcpu, type, retval)); 1260 } 1261 1262 int 1263 vm_set_capability(struct vm *vm, int vcpu, int type, int val) 1264 { 1265 if (vcpu < 0 || vcpu >= VM_MAXCPU) 1266 return (EINVAL); 1267 1268 if (type < 0 || type >= VM_CAP_MAX) 1269 return (EINVAL); 1270 1271 return (VMSETCAP(vm->cookie, vcpu, type, val)); 1272 } 1273 1274 uint64_t * 1275 vm_guest_msrs(struct vm *vm, int cpu) 1276 { 1277 return (vm->vcpu[cpu].guest_msrs); 1278 } 1279 1280 struct vlapic * 1281 vm_lapic(struct vm *vm, int cpu) 1282 { 1283 return (vm->vcpu[cpu].vlapic); 1284 } 1285 1286 struct vioapic * 1287 vm_ioapic(struct vm *vm) 1288 { 1289 1290 return (vm->vioapic); 1291 } 1292 1293 struct vhpet * 1294 vm_hpet(struct vm *vm) 1295 { 1296 1297 return (vm->vhpet); 1298 } 1299 1300 boolean_t 1301 vmm_is_pptdev(int bus, int slot, int func) 1302 { 1303 int found, i, n; 1304 int b, s, f; 1305 char *val, *cp, *cp2; 1306 1307 /* 1308 * XXX 1309 * The length of an environment variable is limited to 128 bytes which 1310 * puts an upper limit on the number of passthru devices that may be 1311 * specified using a single environment variable. 1312 * 1313 * Work around this by scanning multiple environment variable 1314 * names instead of a single one - yuck! 1315 */ 1316 const char *names[] = { "pptdevs", "pptdevs2", "pptdevs3", NULL }; 1317 1318 /* set pptdevs="1/2/3 4/5/6 7/8/9 10/11/12" */ 1319 found = 0; 1320 for (i = 0; names[i] != NULL && !found; i++) { 1321 cp = val = getenv(names[i]); 1322 while (cp != NULL && *cp != '\0') { 1323 if ((cp2 = strchr(cp, ' ')) != NULL) 1324 *cp2 = '\0'; 1325 1326 n = sscanf(cp, "%d/%d/%d", &b, &s, &f); 1327 if (n == 3 && bus == b && slot == s && func == f) { 1328 found = 1; 1329 break; 1330 } 1331 1332 if (cp2 != NULL) 1333 *cp2++ = ' '; 1334 1335 cp = cp2; 1336 } 1337 freeenv(val); 1338 } 1339 return (found); 1340 } 1341 1342 void * 1343 vm_iommu_domain(struct vm *vm) 1344 { 1345 1346 return (vm->iommu); 1347 } 1348 1349 int 1350 vcpu_set_state(struct vm *vm, int vcpuid, enum vcpu_state newstate, 1351 bool from_idle) 1352 { 1353 int error; 1354 struct vcpu *vcpu; 1355 1356 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1357 panic("vm_set_run_state: invalid vcpuid %d", vcpuid); 1358 1359 vcpu = &vm->vcpu[vcpuid]; 1360 1361 vcpu_lock(vcpu); 1362 error = vcpu_set_state_locked(vcpu, newstate, from_idle); 1363 vcpu_unlock(vcpu); 1364 1365 return (error); 1366 } 1367 1368 enum vcpu_state 1369 vcpu_get_state(struct vm *vm, int vcpuid, int *hostcpu) 1370 { 1371 struct vcpu *vcpu; 1372 enum vcpu_state state; 1373 1374 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1375 panic("vm_get_run_state: invalid vcpuid %d", vcpuid); 1376 1377 vcpu = &vm->vcpu[vcpuid]; 1378 1379 vcpu_lock(vcpu); 1380 state = vcpu->state; 1381 if (hostcpu != NULL) 1382 *hostcpu = vcpu->hostcpu; 1383 vcpu_unlock(vcpu); 1384 1385 return (state); 1386 } 1387 1388 void 1389 vm_activate_cpu(struct vm *vm, int vcpuid) 1390 { 1391 1392 if (vcpuid >= 0 && vcpuid < VM_MAXCPU) 1393 CPU_SET(vcpuid, &vm->active_cpus); 1394 } 1395 1396 static void 1397 vm_deactivate_cpu(struct vm *vm, int vcpuid) 1398 { 1399 1400 if (vcpuid >= 0 && vcpuid < VM_MAXCPU) 1401 CPU_CLR(vcpuid, &vm->active_cpus); 1402 } 1403 1404 cpuset_t 1405 vm_active_cpus(struct vm *vm) 1406 { 1407 1408 return (vm->active_cpus); 1409 } 1410 1411 void * 1412 vcpu_stats(struct vm *vm, int vcpuid) 1413 { 1414 1415 return (vm->vcpu[vcpuid].stats); 1416 } 1417 1418 int 1419 vm_get_x2apic_state(struct vm *vm, int vcpuid, enum x2apic_state *state) 1420 { 1421 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1422 return (EINVAL); 1423 1424 *state = vm->vcpu[vcpuid].x2apic_state; 1425 1426 return (0); 1427 } 1428 1429 int 1430 vm_set_x2apic_state(struct vm *vm, int vcpuid, enum x2apic_state state) 1431 { 1432 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 1433 return (EINVAL); 1434 1435 if (state >= X2APIC_STATE_LAST) 1436 return (EINVAL); 1437 1438 vm->vcpu[vcpuid].x2apic_state = state; 1439 1440 vlapic_set_x2apic_state(vm, vcpuid, state); 1441 1442 return (0); 1443 } 1444 1445 /* 1446 * This function is called to ensure that a vcpu "sees" a pending event 1447 * as soon as possible: 1448 * - If the vcpu thread is sleeping then it is woken up. 1449 * - If the vcpu is running on a different host_cpu then an IPI will be directed 1450 * to the host_cpu to cause the vcpu to trap into the hypervisor. 1451 */ 1452 void 1453 vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr) 1454 { 1455 int hostcpu; 1456 struct vcpu *vcpu; 1457 1458 vcpu = &vm->vcpu[vcpuid]; 1459 1460 vcpu_lock(vcpu); 1461 hostcpu = vcpu->hostcpu; 1462 if (hostcpu == NOCPU) { 1463 if (vcpu->state == VCPU_SLEEPING) 1464 wakeup_one(vcpu); 1465 } else { 1466 if (vcpu->state != VCPU_RUNNING) 1467 panic("invalid vcpu state %d", vcpu->state); 1468 if (hostcpu != curcpu) { 1469 if (lapic_intr) 1470 vlapic_post_intr(vcpu->vlapic, hostcpu, 1471 vmm_ipinum); 1472 else 1473 ipi_cpu(hostcpu, vmm_ipinum); 1474 } 1475 } 1476 vcpu_unlock(vcpu); 1477 } 1478 1479 struct vmspace * 1480 vm_get_vmspace(struct vm *vm) 1481 { 1482 1483 return (vm->vmspace); 1484 } 1485 1486 int 1487 vm_apicid2vcpuid(struct vm *vm, int apicid) 1488 { 1489 /* 1490 * XXX apic id is assumed to be numerically identical to vcpu id 1491 */ 1492 return (apicid); 1493 } 1494 1495 void 1496 vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest, 1497 vm_rendezvous_func_t func, void *arg) 1498 { 1499 /* 1500 * Enforce that this function is called without any locks 1501 */ 1502 WITNESS_WARN(WARN_PANIC, NULL, "vm_smp_rendezvous"); 1503 KASSERT(vcpuid == -1 || (vcpuid >= 0 && vcpuid < VM_MAXCPU), 1504 ("vm_smp_rendezvous: invalid vcpuid %d", vcpuid)); 1505 1506 restart: 1507 mtx_lock(&vm->rendezvous_mtx); 1508 if (vm->rendezvous_func != NULL) { 1509 /* 1510 * If a rendezvous is already in progress then we need to 1511 * call the rendezvous handler in case this 'vcpuid' is one 1512 * of the targets of the rendezvous. 1513 */ 1514 RENDEZVOUS_CTR0(vm, vcpuid, "Rendezvous already in progress"); 1515 mtx_unlock(&vm->rendezvous_mtx); 1516 vm_handle_rendezvous(vm, vcpuid); 1517 goto restart; 1518 } 1519 KASSERT(vm->rendezvous_func == NULL, ("vm_smp_rendezvous: previous " 1520 "rendezvous is still in progress")); 1521 1522 RENDEZVOUS_CTR0(vm, vcpuid, "Initiating rendezvous"); 1523 vm->rendezvous_req_cpus = dest; 1524 CPU_ZERO(&vm->rendezvous_done_cpus); 1525 vm->rendezvous_arg = arg; 1526 vm_set_rendezvous_func(vm, func); 1527 mtx_unlock(&vm->rendezvous_mtx); 1528 1529 vm_handle_rendezvous(vm, vcpuid); 1530 } 1531