Lines Matching +full:timer +full:- +full:cannot +full:- +full:wake +full:- +full:cpu
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
61 #include <machine/cpu.h>
106 int hostcpu; /* (o) vcpu's host cpu */
109 void *cookie; /* (i) cpu-specific data */
128 #define vcpu_lock_init(v) mtx_init(&((v)->mtx), "vcpu lock", 0, MTX_SPIN)
129 #define vcpu_lock_destroy(v) mtx_destroy(&((v)->mtx))
130 #define vcpu_lock(v) mtx_lock_spin(&((v)->mtx))
131 #define vcpu_unlock(v) mtx_unlock_spin(&((v)->mtx))
132 #define vcpu_assert_locked(v) mtx_assert(&((v)->mtx), MA_OWNED)
163 void *cookie; /* (i) cpu-specific data */
164 void *iommu; /* (x) iommu-specific data */
169 struct vpmtmr *vpmtmr; /* (i) virtual ACPI PM timer */
188 /* The following describe the vm cpu topology */
198 VCPU_CTR0((vcpu)->vm, (vcpu)->vcpuid, format)
201 VCPU_CTR1((vcpu)->vm, (vcpu)->vcpuid, format, p1)
204 VCPU_CTR2((vcpu)->vm, (vcpu)->vcpuid, format, p1, p2)
207 VCPU_CTR3((vcpu)->vm, (vcpu)->vcpuid, format, p1, p2, p3)
210 VCPU_CTR4((vcpu)->vm, (vcpu)->vcpuid, format, p1, p2, p3, p4)
291 "WBINVD triggers a VM-exit");
325 * Upper limit on vm_maxcpu. Limited by use of uint16_t types for CPU
326 * counts as well as range of vpid values for VT-x and by the capacity
328 * vmx.c requires 'vm_maxcpu + 1 <= 0xffff', hence the '- 1' below.
330 #define VM_MAXCPU MIN(0xffff - 1, CPU_SETSIZE)
355 vmmops_vlapic_cleanup(vcpu->vlapic); in vcpu_cleanup()
356 vmmops_vcpu_cleanup(vcpu->cookie); in vcpu_cleanup()
357 vcpu->cookie = NULL; in vcpu_cleanup()
359 vmm_stat_free(vcpu->stats); in vcpu_cleanup()
360 fpu_save_area_free(vcpu->guestfpu); in vcpu_cleanup()
371 KASSERT(vcpu_id >= 0 && vcpu_id < vm->maxcpus, in vcpu_alloc()
376 vcpu->state = VCPU_IDLE; in vcpu_alloc()
377 vcpu->hostcpu = NOCPU; in vcpu_alloc()
378 vcpu->vcpuid = vcpu_id; in vcpu_alloc()
379 vcpu->vm = vm; in vcpu_alloc()
380 vcpu->guestfpu = fpu_save_area_alloc(); in vcpu_alloc()
381 vcpu->stats = vmm_stat_alloc(); in vcpu_alloc()
382 vcpu->tsc_offset = 0; in vcpu_alloc()
389 vcpu->cookie = vmmops_vcpu_init(vcpu->vm->cookie, vcpu, vcpu->vcpuid); in vcpu_init()
390 vcpu->vlapic = vmmops_vlapic_init(vcpu->cookie); in vcpu_init()
392 vcpu->reqidle = 0; in vcpu_init()
393 vcpu->exitintinfo = 0; in vcpu_init()
394 vcpu->nmi_pending = 0; in vcpu_init()
395 vcpu->extint_pending = 0; in vcpu_init()
396 vcpu->exception_pending = 0; in vcpu_init()
397 vcpu->guest_xcr0 = XFEATURE_ENABLED_X87; in vcpu_init()
398 fpu_save_area_reset(vcpu->guestfpu); in vcpu_init()
399 vmm_stat_init(vcpu->stats); in vcpu_init()
418 return (&vcpu->exitinfo); in vm_exitinfo()
424 return (&vcpu->exitinfo_cpuset); in vm_exitinfo_cpuset()
487 * Something bad happened - prevent new in vmm_handler()
513 * - VT-x initialization requires smp_rendezvous() and therefore must happen
515 * - vmm device initialization requires an initialized devfs.
523 vm->cookie = vmmops_init(vm, vmspace_pmap(vm->vmspace)); in vm_init()
524 vm->iommu = NULL; in vm_init()
525 vm->vioapic = vioapic_init(vm); in vm_init()
526 vm->vhpet = vhpet_init(vm); in vm_init()
527 vm->vatpic = vatpic_init(vm); in vm_init()
528 vm->vatpit = vatpit_init(vm); in vm_init()
529 vm->vpmtmr = vpmtmr_init(vm); in vm_init()
531 vm->vrtc = vrtc_init(vm); in vm_init()
533 CPU_ZERO(&vm->active_cpus); in vm_init()
534 CPU_ZERO(&vm->debug_cpus); in vm_init()
535 CPU_ZERO(&vm->startup_cpus); in vm_init()
537 vm->suspend = 0; in vm_init()
538 CPU_ZERO(&vm->suspended_cpus); in vm_init()
541 for (int i = 0; i < vm->maxcpus; i++) { in vm_init()
542 if (vm->vcpu[i] != NULL) in vm_init()
543 vcpu_init(vm->vcpu[i]); in vm_init()
551 sx_xlock(&vm->vcpus_init_lock); in vm_disable_vcpu_creation()
552 vm->dying = true; in vm_disable_vcpu_creation()
553 sx_xunlock(&vm->vcpus_init_lock); in vm_disable_vcpu_creation()
565 atomic_load_acq_ptr((uintptr_t *)&vm->vcpu[vcpuid]); in vm_alloc_vcpu()
569 sx_xlock(&vm->vcpus_init_lock); in vm_alloc_vcpu()
570 vcpu = vm->vcpu[vcpuid]; in vm_alloc_vcpu()
571 if (vcpu == NULL && !vm->dying) { in vm_alloc_vcpu()
579 atomic_store_rel_ptr((uintptr_t *)&vm->vcpu[vcpuid], in vm_alloc_vcpu()
582 sx_xunlock(&vm->vcpus_init_lock); in vm_alloc_vcpu()
589 sx_slock(&vm->vcpus_init_lock); in vm_slock_vcpus()
595 sx_unlock(&vm->vcpus_init_lock); in vm_unlock_vcpus()
599 * The default CPU topology is a single thread per package.
626 strcpy(vm->name, name); in vm_create()
627 vm->vmspace = vmspace; in vm_create()
628 mtx_init(&vm->rendezvous_mtx, "vm rendezvous lock", 0, MTX_DEF); in vm_create()
629 sx_init(&vm->mem_segs_lock, "vm mem_segs"); in vm_create()
630 sx_init(&vm->vcpus_init_lock, "vm vcpus"); in vm_create()
631 vm->vcpu = malloc(sizeof(*vm->vcpu) * vm_maxcpu, M_VM, M_WAITOK | in vm_create()
634 vm->sockets = 1; in vm_create()
635 vm->cores = cores_per_package; /* XXX backwards compatibility */ in vm_create()
636 vm->threads = threads_per_core; /* XXX backwards compatibility */ in vm_create()
637 vm->maxcpus = vm_maxcpu; in vm_create()
649 *sockets = vm->sockets; in vm_get_topology()
650 *cores = vm->cores; in vm_get_topology()
651 *threads = vm->threads; in vm_get_topology()
652 *maxcpus = vm->maxcpus; in vm_get_topology()
658 return (vm->maxcpus); in vm_get_maxcpus()
666 if ((sockets * cores * threads) > vm->maxcpus) in vm_set_topology()
668 vm->sockets = sockets; in vm_set_topology()
669 vm->cores = cores; in vm_set_topology()
670 vm->threads = threads; in vm_set_topology()
685 if (vm->iommu != NULL) in vm_cleanup()
686 iommu_destroy_domain(vm->iommu); in vm_cleanup()
689 vrtc_cleanup(vm->vrtc); in vm_cleanup()
691 vrtc_reset(vm->vrtc); in vm_cleanup()
692 vpmtmr_cleanup(vm->vpmtmr); in vm_cleanup()
693 vatpit_cleanup(vm->vatpit); in vm_cleanup()
694 vhpet_cleanup(vm->vhpet); in vm_cleanup()
695 vatpic_cleanup(vm->vatpic); in vm_cleanup()
696 vioapic_cleanup(vm->vioapic); in vm_cleanup()
698 for (i = 0; i < vm->maxcpus; i++) { in vm_cleanup()
699 if (vm->vcpu[i] != NULL) in vm_cleanup()
700 vcpu_cleanup(vm->vcpu[i], destroy); in vm_cleanup()
703 vmmops_cleanup(vm->cookie); in vm_cleanup()
714 mm = &vm->mem_maps[i]; in vm_cleanup()
724 vmmops_vmspace_free(vm->vmspace); in vm_cleanup()
725 vm->vmspace = NULL; in vm_cleanup()
727 free(vm->vcpu, M_VM); in vm_cleanup()
728 sx_destroy(&vm->vcpus_init_lock); in vm_cleanup()
729 sx_destroy(&vm->mem_segs_lock); in vm_cleanup()
730 mtx_destroy(&vm->rendezvous_mtx); in vm_cleanup()
749 if (CPU_CMP(&vm->suspended_cpus, &vm->active_cpus) == 0) { in vm_reinit()
763 return (vm->name); in vm_name()
769 sx_slock(&vm->mem_segs_lock); in vm_slock_memsegs()
775 sx_xlock(&vm->mem_segs_lock); in vm_xlock_memsegs()
781 sx_unlock(&vm->mem_segs_lock); in vm_unlock_memsegs()
789 if ((obj = vmm_mmio_alloc(vm->vmspace, gpa, len, hpa)) == NULL) in vm_map_mmio()
799 vmm_mmio_free(vm->vmspace, gpa, len); in vm_unmap_mmio()
807 * an implicit lock on 'vm->mem_maps[]'.
812 struct vm *vm = vcpu->vm; in vm_mem_allocated()
824 mm = &vm->mem_maps[i]; in vm_mem_allocated()
825 if (mm->len != 0 && gpa >= mm->gpa && gpa < mm->gpa + mm->len) in vm_mem_allocated()
841 sx_assert(&vm->mem_segs_lock, SX_XLOCKED); in vm_alloc_memseg()
849 seg = &vm->mem_segs[ident]; in vm_alloc_memseg()
850 if (seg->object != NULL) { in vm_alloc_memseg()
851 if (seg->len == len && seg->sysmem == sysmem) in vm_alloc_memseg()
861 seg->len = len; in vm_alloc_memseg()
862 seg->object = obj; in vm_alloc_memseg()
863 seg->sysmem = sysmem; in vm_alloc_memseg()
873 sx_assert(&vm->mem_segs_lock, SX_LOCKED); in vm_get_memseg()
878 seg = &vm->mem_segs[ident]; in vm_get_memseg()
880 *len = seg->len; in vm_get_memseg()
882 *sysmem = seg->sysmem; in vm_get_memseg()
884 *objptr = seg->object; in vm_get_memseg()
896 seg = &vm->mem_segs[ident]; in vm_free_memseg()
897 if (seg->object != NULL) { in vm_free_memseg()
898 vm_object_deallocate(seg->object); in vm_free_memseg()
921 seg = &vm->mem_segs[segid]; in vm_mmap_memseg()
922 if (seg->object == NULL) in vm_mmap_memseg()
926 if (first < 0 || first >= last || last > seg->len) in vm_mmap_memseg()
934 m = &vm->mem_maps[i]; in vm_mmap_memseg()
935 if (m->len == 0) { in vm_mmap_memseg()
944 error = vm_map_find(&vm->vmspace->vm_map, seg->object, first, &gpa, in vm_mmap_memseg()
949 vm_object_reference(seg->object); in vm_mmap_memseg()
952 error = vm_map_wire(&vm->vmspace->vm_map, gpa, gpa + len, in vm_mmap_memseg()
955 vm_map_remove(&vm->vmspace->vm_map, gpa, gpa + len); in vm_mmap_memseg()
961 map->gpa = gpa; in vm_mmap_memseg()
962 map->len = len; in vm_mmap_memseg()
963 map->segoff = first; in vm_mmap_memseg()
964 map->segid = segid; in vm_mmap_memseg()
965 map->prot = prot; in vm_mmap_memseg()
966 map->flags = flags; in vm_mmap_memseg()
977 m = &vm->mem_maps[i]; in vm_munmap_memseg()
978 if (m->gpa == gpa && m->len == len && in vm_munmap_memseg()
979 (m->flags & VM_MEMMAP_F_IOMMU) == 0) { in vm_munmap_memseg()
997 mm = &vm->mem_maps[i]; in vm_mmap_getnext()
998 if (mm->len == 0 || mm->gpa < *gpa) in vm_mmap_getnext()
1000 if (mmnext == NULL || mm->gpa < mmnext->gpa) in vm_mmap_getnext()
1005 *gpa = mmnext->gpa; in vm_mmap_getnext()
1007 *segid = mmnext->segid; in vm_mmap_getnext()
1009 *segoff = mmnext->segoff; in vm_mmap_getnext()
1011 *len = mmnext->len; in vm_mmap_getnext()
1013 *prot = mmnext->prot; in vm_mmap_getnext()
1015 *flags = mmnext->flags; in vm_mmap_getnext()
1028 mm = &vm->mem_maps[ident]; in vm_free_memmap()
1029 if (mm->len) { in vm_free_memmap()
1030 error = vm_map_remove(&vm->vmspace->vm_map, mm->gpa, in vm_free_memmap()
1031 mm->gpa + mm->len); in vm_free_memmap()
1042 if (mm->len != 0 && vm->mem_segs[mm->segid].sysmem) in sysmem_mapping()
1057 mm = &vm->mem_maps[i]; in vmm_sysmem_maxaddr()
1059 if (maxaddr < mm->gpa + mm->len) in vmm_sysmem_maxaddr()
1060 maxaddr = mm->gpa + mm->len; in vmm_sysmem_maxaddr()
1073 sx_assert(&vm->mem_segs_lock, SX_LOCKED); in vm_iommu_map()
1076 mm = &vm->mem_maps[i]; in vm_iommu_map()
1080 KASSERT((mm->flags & VM_MEMMAP_F_IOMMU) == 0, in vm_iommu_map()
1082 mm->gpa, mm->len, mm->flags)); in vm_iommu_map()
1083 if ((mm->flags & VM_MEMMAP_F_WIRED) == 0) in vm_iommu_map()
1085 mm->flags |= VM_MEMMAP_F_IOMMU; in vm_iommu_map()
1087 for (gpa = mm->gpa; gpa < mm->gpa + mm->len; gpa += PAGE_SIZE) { in vm_iommu_map()
1088 hpa = pmap_extract(vmspace_pmap(vm->vmspace), gpa); in vm_iommu_map()
1093 * Because we are in pass-through mode, the in vm_iommu_map()
1106 iommu_create_mapping(vm->iommu, gpa, hpa, PAGE_SIZE); in vm_iommu_map()
1120 sx_assert(&vm->mem_segs_lock, SX_LOCKED); in vm_iommu_unmap()
1123 mm = &vm->mem_maps[i]; in vm_iommu_unmap()
1127 if ((mm->flags & VM_MEMMAP_F_IOMMU) == 0) in vm_iommu_unmap()
1129 mm->flags &= ~VM_MEMMAP_F_IOMMU; in vm_iommu_unmap()
1130 KASSERT((mm->flags & VM_MEMMAP_F_WIRED) != 0, in vm_iommu_unmap()
1132 mm->gpa, mm->len, mm->flags)); in vm_iommu_unmap()
1134 for (gpa = mm->gpa; gpa < mm->gpa + mm->len; gpa += PAGE_SIZE) { in vm_iommu_unmap()
1136 vmspace_pmap(vm->vmspace), gpa))), in vm_iommu_unmap()
1139 iommu_remove_mapping(vm->iommu, gpa, PAGE_SIZE); in vm_iommu_unmap()
1147 iommu_invalidate_tlb(vm->iommu); in vm_iommu_unmap()
1173 KASSERT(vm->iommu == NULL, in vm_assign_pptdev()
1176 vm->iommu = iommu_create_domain(maxaddr); in vm_assign_pptdev()
1177 if (vm->iommu == NULL) in vm_assign_pptdev()
1195 if (len > PAGE_SIZE - pageoff) in _vm_gpa_hold()
1200 mm = &vm->mem_maps[i]; in _vm_gpa_hold()
1201 if (gpa >= mm->gpa && gpa < mm->gpa + mm->len) { in _vm_gpa_hold()
1202 count = vm_fault_quick_hold_pages(&vm->vmspace->vm_map, in _vm_gpa_hold()
1230 return (_vm_gpa_hold(vcpu->vm, gpa, len, reqprot, cookie)); in vm_gpa_hold()
1237 sx_assert(&vm->mem_segs_lock, SX_LOCKED); in vm_gpa_hold_global()
1256 return (vmmops_getreg(vcpu->cookie, reg, retval)); in vm_get_register()
1267 error = vmmops_setreg(vcpu->cookie, reg, val); in vm_set_register()
1273 vcpu->nextrip = val; in vm_set_register()
1316 return (vmmops_getdesc(vcpu->cookie, reg, desc)); in vm_get_seg_desc()
1326 return (vmmops_setdesc(vcpu->cookie, reg, desc)); in vm_set_seg_desc()
1338 fpurestore(vcpu->guestfpu); in restore_guest_fpustate()
1342 load_xcr(0, vcpu->guest_xcr0); in restore_guest_fpustate()
1360 vcpu->guest_xcr0 = rxcr(0); in save_guest_fpustate()
1366 fpusave(vcpu->guestfpu); in save_guest_fpustate()
1386 while (vcpu->state != VCPU_IDLE) { in vcpu_set_state_locked()
1387 vcpu->reqidle = 1; in vcpu_set_state_locked()
1390 "idle requested", vcpu_state2str(vcpu->state)); in vcpu_set_state_locked()
1391 msleep_spin(&vcpu->state, &vcpu->mtx, "vmstat", hz); in vcpu_set_state_locked()
1394 KASSERT(vcpu->state != VCPU_IDLE, ("invalid transition from " in vcpu_set_state_locked()
1398 if (vcpu->state == VCPU_RUNNING) { in vcpu_set_state_locked()
1399 KASSERT(vcpu->hostcpu == curcpu, ("curcpu %d and hostcpu %d " in vcpu_set_state_locked()
1400 "mismatch for running vcpu", curcpu, vcpu->hostcpu)); in vcpu_set_state_locked()
1402 KASSERT(vcpu->hostcpu == NOCPU, ("Invalid hostcpu %d for a " in vcpu_set_state_locked()
1403 "vcpu that is not running", vcpu->hostcpu)); in vcpu_set_state_locked()
1408 * IDLE -> FROZEN -> IDLE in vcpu_set_state_locked()
1409 * FROZEN -> RUNNING -> FROZEN in vcpu_set_state_locked()
1410 * FROZEN -> SLEEPING -> FROZEN in vcpu_set_state_locked()
1412 switch (vcpu->state) { in vcpu_set_state_locked()
1430 vcpu_state2str(vcpu->state), vcpu_state2str(newstate)); in vcpu_set_state_locked()
1432 vcpu->state = newstate; in vcpu_set_state_locked()
1434 vcpu->hostcpu = curcpu; in vcpu_set_state_locked()
1436 vcpu->hostcpu = NOCPU; in vcpu_set_state_locked()
1439 wakeup(&vcpu->state); in vcpu_set_state_locked()
1465 struct vm *vm = vcpu->vm; in vm_handle_rendezvous()
1470 vcpuid = vcpu->vcpuid; in vm_handle_rendezvous()
1472 mtx_lock(&vm->rendezvous_mtx); in vm_handle_rendezvous()
1473 while (vm->rendezvous_func != NULL) { in vm_handle_rendezvous()
1475 CPU_AND(&vm->rendezvous_req_cpus, &vm->rendezvous_req_cpus, &vm->active_cpus); in vm_handle_rendezvous()
1477 if (CPU_ISSET(vcpuid, &vm->rendezvous_req_cpus) && in vm_handle_rendezvous()
1478 !CPU_ISSET(vcpuid, &vm->rendezvous_done_cpus)) { in vm_handle_rendezvous()
1480 (*vm->rendezvous_func)(vcpu, vm->rendezvous_arg); in vm_handle_rendezvous()
1481 CPU_SET(vcpuid, &vm->rendezvous_done_cpus); in vm_handle_rendezvous()
1483 if (CPU_CMP(&vm->rendezvous_req_cpus, in vm_handle_rendezvous()
1484 &vm->rendezvous_done_cpus) == 0) { in vm_handle_rendezvous()
1486 CPU_ZERO(&vm->rendezvous_req_cpus); in vm_handle_rendezvous()
1487 vm->rendezvous_func = NULL; in vm_handle_rendezvous()
1488 wakeup(&vm->rendezvous_func); in vm_handle_rendezvous()
1492 mtx_sleep(&vm->rendezvous_func, &vm->rendezvous_mtx, 0, in vm_handle_rendezvous()
1495 mtx_unlock(&vm->rendezvous_mtx); in vm_handle_rendezvous()
1499 mtx_lock(&vm->rendezvous_mtx); in vm_handle_rendezvous()
1502 mtx_unlock(&vm->rendezvous_mtx); in vm_handle_rendezvous()
1512 struct vm *vm = vcpu->vm; in vm_handle_hlt()
1517 vcpuid = vcpu->vcpuid; in vm_handle_hlt()
1523 KASSERT(!CPU_ISSET(vcpuid, &vm->halted_cpus), ("vcpu already halted")); in vm_handle_hlt()
1536 if (vm->rendezvous_func != NULL || vm->suspend || vcpu->reqidle) in vm_handle_hlt()
1542 vlapic_pending_intr(vcpu->vlapic, NULL)) { in vm_handle_hlt()
1565 CPU_SET_ATOMIC(vcpuid, &vm->halted_cpus); in vm_handle_hlt()
1567 if (CPU_CMP(&vm->halted_cpus, &vm->active_cpus) == 0) { in vm_handle_hlt()
1578 * XXX msleep_spin() cannot be interrupted by signals so in vm_handle_hlt()
1579 * wake up periodically to check pending signals. in vm_handle_hlt()
1581 msleep_spin(vcpu, &vcpu->mtx, wmesg, hz); in vm_handle_hlt()
1583 vmm_stat_incr(vcpu, VCPU_IDLE_TICKS, ticks - t); in vm_handle_hlt()
1590 &vm->halted_cpus); in vm_handle_hlt()
1599 CPU_CLR_ATOMIC(vcpuid, &vm->halted_cpus); in vm_handle_hlt()
1612 struct vm *vm = vcpu->vm; in vm_handle_paging()
1617 vme = &vcpu->exitinfo; in vm_handle_paging()
1619 KASSERT(vme->inst_length == 0, ("%s: invalid inst_length %d", in vm_handle_paging()
1620 __func__, vme->inst_length)); in vm_handle_paging()
1622 ftype = vme->u.paging.fault_type; in vm_handle_paging()
1628 rv = pmap_emulate_accessed_dirty(vmspace_pmap(vm->vmspace), in vm_handle_paging()
1629 vme->u.paging.gpa, ftype); in vm_handle_paging()
1633 vme->u.paging.gpa); in vm_handle_paging()
1638 map = &vm->vmspace->vm_map; in vm_handle_paging()
1639 rv = vm_fault(map, vme->u.paging.gpa, ftype, VM_FAULT_NORMAL, NULL); in vm_handle_paging()
1642 "ftype = %d", rv, vme->u.paging.gpa, ftype); in vm_handle_paging()
1662 vme = &vcpu->exitinfo; in vm_handle_inst_emul()
1664 KASSERT(vme->inst_length == 0, ("%s: invalid inst_length %d", in vm_handle_inst_emul()
1665 __func__, vme->inst_length)); in vm_handle_inst_emul()
1667 gla = vme->u.inst_emul.gla; in vm_handle_inst_emul()
1668 gpa = vme->u.inst_emul.gpa; in vm_handle_inst_emul()
1669 cs_base = vme->u.inst_emul.cs_base; in vm_handle_inst_emul()
1670 cs_d = vme->u.inst_emul.cs_d; in vm_handle_inst_emul()
1671 vie = &vme->u.inst_emul.vie; in vm_handle_inst_emul()
1672 paging = &vme->u.inst_emul.paging; in vm_handle_inst_emul()
1673 cpu_mode = paging->cpu_mode; in vm_handle_inst_emul()
1678 if (vie->num_valid == 0) { in vm_handle_inst_emul()
1679 error = vmm_fetch_instruction(vcpu, paging, vme->rip + cs_base, in vm_handle_inst_emul()
1692 vme->rip + cs_base); in vm_handle_inst_emul()
1700 vme->inst_length = vie->num_processed; in vm_handle_inst_emul()
1701 vcpu->nextrip += vie->num_processed; in vm_handle_inst_emul()
1703 vcpu->nextrip); in vm_handle_inst_emul()
1705 /* return to userland unless this is an in-kernel emulated device */ in vm_handle_inst_emul()
1729 struct vm *vm = vcpu->vm; in vm_handle_suspend()
1736 CPU_SET_ATOMIC(vcpu->vcpuid, &vm->suspended_cpus); in vm_handle_suspend()
1747 if (CPU_CMP(&vm->suspended_cpus, &vm->active_cpus) == 0) { in vm_handle_suspend()
1752 if (vm->rendezvous_func == NULL) { in vm_handle_suspend()
1755 msleep_spin(vcpu, &vcpu->mtx, "vmsusp", hz); in vm_handle_suspend()
1774 for (i = 0; i < vm->maxcpus; i++) { in vm_handle_suspend()
1775 if (CPU_ISSET(i, &vm->suspended_cpus)) { in vm_handle_suspend()
1788 KASSERT(vcpu->reqidle, ("invalid vcpu reqidle %d", vcpu->reqidle)); in vm_handle_reqidle()
1789 vcpu->reqidle = 0; in vm_handle_reqidle()
1804 if (!vme->u.dbg.pushf_intercept || vme->u.dbg.tf_shadow_val != 0) { in vm_handle_db()
1809 error = vm_copy_setup(vcpu, &vme->u.dbg.paging, rsp, sizeof(uint64_t), in vm_handle_db()
1837 if (atomic_cmpset_int(&vm->suspend, 0, how) == 0) { in vm_suspend()
1839 vm->suspend, how); in vm_suspend()
1848 for (i = 0; i < vm->maxcpus; i++) { in vm_suspend()
1849 if (CPU_ISSET(i, &vm->active_cpus)) in vm_suspend()
1859 struct vm *vm = vcpu->vm; in vm_exit_suspended()
1862 KASSERT(vm->suspend > VM_SUSPEND_NONE && vm->suspend < VM_SUSPEND_LAST, in vm_exit_suspended()
1863 ("vm_exit_suspended: invalid suspend type %d", vm->suspend)); in vm_exit_suspended()
1866 vmexit->rip = rip; in vm_exit_suspended()
1867 vmexit->inst_length = 0; in vm_exit_suspended()
1868 vmexit->exitcode = VM_EXITCODE_SUSPENDED; in vm_exit_suspended()
1869 vmexit->u.suspended.how = vm->suspend; in vm_exit_suspended()
1878 vmexit->rip = rip; in vm_exit_debug()
1879 vmexit->inst_length = 0; in vm_exit_debug()
1880 vmexit->exitcode = VM_EXITCODE_DEBUG; in vm_exit_debug()
1889 vmexit->rip = rip; in vm_exit_rendezvous()
1890 vmexit->inst_length = 0; in vm_exit_rendezvous()
1891 vmexit->exitcode = VM_EXITCODE_RENDEZVOUS; in vm_exit_rendezvous()
1901 vmexit->rip = rip; in vm_exit_reqidle()
1902 vmexit->inst_length = 0; in vm_exit_reqidle()
1903 vmexit->exitcode = VM_EXITCODE_REQIDLE; in vm_exit_reqidle()
1913 vmexit->rip = rip; in vm_exit_astpending()
1914 vmexit->inst_length = 0; in vm_exit_astpending()
1915 vmexit->exitcode = VM_EXITCODE_BOGUS; in vm_exit_astpending()
1922 struct vm *vm = vcpu->vm; in vm_run()
1931 vcpuid = vcpu->vcpuid; in vm_run()
1933 if (!CPU_ISSET(vcpuid, &vm->active_cpus)) in vm_run()
1936 if (CPU_ISSET(vcpuid, &vm->suspended_cpus)) in vm_run()
1939 pmap = vmspace_pmap(vm->vmspace); in vm_run()
1940 vme = &vcpu->exitinfo; in vm_run()
1941 evinfo.rptr = &vm->rendezvous_req_cpus; in vm_run()
1942 evinfo.sptr = &vm->suspend; in vm_run()
1943 evinfo.iptr = &vcpu->reqidle; in vm_run()
1947 KASSERT(!CPU_ISSET(curcpu, &pmap->pm_active), in vm_run()
1958 error = vmmops_run(vcpu->cookie, vcpu->nextrip, pmap, &evinfo); in vm_run()
1963 vmm_stat_incr(vcpu, VCPU_TOTAL_RUNTIME, rdtsc() - tscval); in vm_run()
1969 vcpu->nextrip = vme->rip + vme->inst_length; in vm_run()
1970 switch (vme->exitcode) { in vm_run()
1978 vioapic_process_eoi(vm, vme->u.ioapic_eoi.vector); in vm_run()
1984 intr_disabled = ((vme->u.hlt.rflags & PSL_I) == 0); in vm_run()
2015 if (error == 0 && vme->exitcode == VM_EXITCODE_IPI) in vm_run()
2022 VMM_CTR2(vcpu, "retu %d/%d", error, vme->exitcode); in vm_run()
2042 vcpu->exitinfo.inst_length = 0; in vm_restart_instruction()
2044 "setting inst_length to zero", vcpu->exitinfo.rip); in vm_restart_instruction()
2055 "nextrip from %#lx to %#lx", vcpu->nextrip, rip); in vm_restart_instruction()
2056 vcpu->nextrip = rip; in vm_restart_instruction()
2081 vcpu->exitintinfo = info; in vm_exit_intinfo()
2102 /* Table 6-4, "Interrupt and Exception Classes", Intel SDM, Vol 3 */ in exception_class()
2112 * SVM and VT-x use identical type values to represent NMI, in exception_class()
2115 * SVM uses type '3' for all exceptions. VT-x uses type '3' in exception_class()
2150 * If an exception occurs while attempting to call the double-fault in nested_fault()
2158 vm_suspend(vcpu->vm, VM_SUSPEND_TRIPLEFAULT); in nested_fault()
2164 * Table 6-5 "Conditions for Generating a Double Fault", Intel SDM, Vol3 in nested_fault()
2186 if (vcpu->exception_pending) { in vcpu_exception_intinfo()
2187 info = vcpu->exc_vector & 0xff; in vcpu_exception_intinfo()
2189 if (vcpu->exc_errcode_valid) { in vcpu_exception_intinfo()
2191 info |= (uint64_t)vcpu->exc_errcode << 32; in vcpu_exception_intinfo()
2203 info1 = vcpu->exitintinfo; in vm_entry_intinfo()
2204 vcpu->exitintinfo = 0; in vm_entry_intinfo()
2207 if (vcpu->exception_pending) { in vm_entry_intinfo()
2209 vcpu->exception_pending = 0; in vm_entry_intinfo()
2211 vcpu->exc_vector, info2); in vm_entry_intinfo()
2237 *info1 = vcpu->exitintinfo; in vm_get_intinfo()
2260 if (vcpu->exception_pending) { in vm_inject_exception()
2262 "pending exception %d", vector, vcpu->exc_vector); in vm_inject_exception()
2289 vcpu->exception_pending = 1; in vm_inject_exception()
2290 vcpu->exc_vector = vector; in vm_inject_exception()
2291 vcpu->exc_errcode = errcode; in vm_inject_exception()
2292 vcpu->exc_errcode_valid = errcode_valid; in vm_inject_exception()
2329 vcpu->nmi_pending = 1; in vm_inject_nmi()
2337 return (vcpu->nmi_pending); in vm_nmi_pending()
2343 if (vcpu->nmi_pending == 0) in vm_nmi_clear()
2346 vcpu->nmi_pending = 0; in vm_nmi_clear()
2356 vcpu->extint_pending = 1; in vm_inject_extint()
2364 return (vcpu->extint_pending); in vm_extint_pending()
2370 if (vcpu->extint_pending == 0) in vm_extint_clear()
2373 vcpu->extint_pending = 0; in vm_extint_clear()
2383 return (vmmops_getcap(vcpu->cookie, type, retval)); in vm_get_capability()
2392 return (vmmops_setcap(vcpu->cookie, type, val)); in vm_set_capability()
2398 return (vcpu->vm); in vcpu_vm()
2404 return (vcpu->vcpuid); in vcpu_vcpuid()
2410 return (vm->vcpu[vcpuid]); in vm_vcpu()
2416 return (vcpu->vlapic); in vm_lapic()
2423 return (vm->vioapic); in vm_ioapic()
2430 return (vm->vhpet); in vm_hpet()
2447 * names instead of a single one - yuck! in vmm_is_pptdev()
2479 return (vm->iommu); in vm_iommu_domain()
2500 state = vcpu->state; in vcpu_get_state()
2502 *hostcpu = vcpu->hostcpu; in vcpu_get_state()
2511 struct vm *vm = vcpu->vm; in vm_activate_cpu()
2513 if (CPU_ISSET(vcpu->vcpuid, &vm->active_cpus)) in vm_activate_cpu()
2517 CPU_SET_ATOMIC(vcpu->vcpuid, &vm->active_cpus); in vm_activate_cpu()
2525 vm->debug_cpus = vm->active_cpus; in vm_suspend_cpu()
2526 for (int i = 0; i < vm->maxcpus; i++) { in vm_suspend_cpu()
2527 if (CPU_ISSET(i, &vm->active_cpus)) in vm_suspend_cpu()
2531 if (!CPU_ISSET(vcpu->vcpuid, &vm->active_cpus)) in vm_suspend_cpu()
2534 CPU_SET_ATOMIC(vcpu->vcpuid, &vm->debug_cpus); in vm_suspend_cpu()
2545 CPU_ZERO(&vm->debug_cpus); in vm_resume_cpu()
2547 if (!CPU_ISSET(vcpu->vcpuid, &vm->debug_cpus)) in vm_resume_cpu()
2550 CPU_CLR_ATOMIC(vcpu->vcpuid, &vm->debug_cpus); in vm_resume_cpu()
2559 return (CPU_ISSET(vcpu->vcpuid, &vcpu->vm->debug_cpus)); in vcpu_debugged()
2566 return (vm->active_cpus); in vm_active_cpus()
2573 return (vm->debug_cpus); in vm_debug_cpus()
2580 return (vm->suspended_cpus); in vm_suspended_cpus()
2592 mtx_lock(&vm->rendezvous_mtx); in vm_start_cpus()
2593 CPU_AND(&set, &vm->startup_cpus, tostart); in vm_start_cpus()
2594 CPU_ANDNOT(&vm->startup_cpus, &vm->startup_cpus, &set); in vm_start_cpus()
2595 mtx_unlock(&vm->rendezvous_mtx); in vm_start_cpus()
2602 mtx_lock(&vm->rendezvous_mtx); in vm_await_start()
2603 CPU_OR(&vm->startup_cpus, &vm->startup_cpus, waiting); in vm_await_start()
2604 mtx_unlock(&vm->rendezvous_mtx); in vm_await_start()
2611 return (vcpu->stats); in vcpu_stats()
2617 *state = vcpu->x2apic_state; in vm_get_x2apic_state()
2628 vcpu->x2apic_state = state; in vm_set_x2apic_state()
2638 * - If the vcpu thread is sleeping then it is woken up.
2639 * - If the vcpu is running on a different host_cpu then an IPI will be directed
2647 hostcpu = vcpu->hostcpu; in vcpu_notify_event_locked()
2648 if (vcpu->state == VCPU_RUNNING) { in vcpu_notify_event_locked()
2652 vlapic_post_intr(vcpu->vlapic, hostcpu, in vcpu_notify_event_locked()
2667 "with hostcpu %d", vcpu->state, hostcpu)); in vcpu_notify_event_locked()
2668 if (vcpu->state == VCPU_SLEEPING) in vcpu_notify_event_locked()
2684 return (vm->vmspace); in vm_vmspace()
2700 struct vm *vm = vcpu->vm; in vm_smp_rendezvous()
2709 mtx_lock(&vm->rendezvous_mtx); in vm_smp_rendezvous()
2710 if (vm->rendezvous_func != NULL) { in vm_smp_rendezvous()
2717 mtx_unlock(&vm->rendezvous_mtx); in vm_smp_rendezvous()
2723 KASSERT(vm->rendezvous_func == NULL, ("vm_smp_rendezvous: previous " in vm_smp_rendezvous()
2727 vm->rendezvous_req_cpus = dest; in vm_smp_rendezvous()
2728 CPU_ZERO(&vm->rendezvous_done_cpus); in vm_smp_rendezvous()
2729 vm->rendezvous_arg = arg; in vm_smp_rendezvous()
2730 vm->rendezvous_func = func; in vm_smp_rendezvous()
2731 mtx_unlock(&vm->rendezvous_mtx); in vm_smp_rendezvous()
2734 * Wake up any sleeping vcpus and trigger a VM-exit in any running in vm_smp_rendezvous()
2737 for (i = 0; i < vm->maxcpus; i++) { in vm_smp_rendezvous()
2748 return (vm->vatpic); in vm_atpic()
2754 return (vm->vatpit); in vm_atpit()
2761 return (vm->vpmtmr); in vm_pmtmr()
2768 return (vm->vrtc); in vm_rtc()
2821 n = min(remaining, PAGE_SIZE - off); in vm_copy_setup()
2824 remaining -= n; in vm_copy_setup()
2857 len -= copyinfo[idx].len; in vm_copyin()
2873 len -= copyinfo[idx].len; in vm_copyout()
2880 * Return the amount of in-use and wired memory for the VM. Since
2890 if (vcpu->vcpuid == 0) { in vm_get_rescnt()
2892 vmspace_resident_count(vcpu->vm->vmspace)); in vm_get_rescnt()
2900 if (vcpu->vcpuid == 0) { in vm_get_wiredcnt()
2902 pmap_wired_count(vmspace_pmap(vcpu->vm->vmspace))); in vm_get_wiredcnt()
2921 vcpu = vm->vcpu[i]; in vm_snapshot_vcpus()
2925 SNAPSHOT_VAR_OR_LEAVE(vcpu->x2apic_state, meta, ret, done); in vm_snapshot_vcpus()
2926 SNAPSHOT_VAR_OR_LEAVE(vcpu->exitintinfo, meta, ret, done); in vm_snapshot_vcpus()
2927 SNAPSHOT_VAR_OR_LEAVE(vcpu->exc_vector, meta, ret, done); in vm_snapshot_vcpus()
2928 SNAPSHOT_VAR_OR_LEAVE(vcpu->exc_errcode_valid, meta, ret, done); in vm_snapshot_vcpus()
2929 SNAPSHOT_VAR_OR_LEAVE(vcpu->exc_errcode, meta, ret, done); in vm_snapshot_vcpus()
2930 SNAPSHOT_VAR_OR_LEAVE(vcpu->guest_xcr0, meta, ret, done); in vm_snapshot_vcpus()
2931 SNAPSHOT_VAR_OR_LEAVE(vcpu->exitinfo, meta, ret, done); in vm_snapshot_vcpus()
2932 SNAPSHOT_VAR_OR_LEAVE(vcpu->nextrip, meta, ret, done); in vm_snapshot_vcpus()
2940 tsc = now + vcpu->tsc_offset; in vm_snapshot_vcpus()
2942 if (meta->op == VM_SNAPSHOT_RESTORE) in vm_snapshot_vcpus()
2943 vcpu->tsc_offset = tsc; in vm_snapshot_vcpus()
2959 SNAPSHOT_VAR_OR_LEAVE(vm->startup_cpus, meta, ret, done); in vm_snapshot_vm()
2975 vcpu = vm->vcpu[i]; in vm_snapshot_vcpu()
2979 error = vmmops_vcpu_snapshot(vcpu->cookie, meta); in vm_snapshot_vcpu()
2992 * Save kernel-side structures to user-space for snapshotting.
2999 switch (meta->dev_req) { in vm_snapshot_req()
3029 __func__, meta->dev_req); in vm_snapshot_req()
3038 vcpu->tsc_offset = offset; in vm_set_tsc_offset()
3057 vcpu = vm->vcpu[i]; in vm_restore_time()
3061 error = vmmops_restore_tsc(vcpu->cookie, in vm_restore_time()
3062 vcpu->tsc_offset - now); in vm_restore_time()