2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/sysctl.h>
39 #include <sys/malloc.h>
42 #include <sys/mutex.h>
44 #include <sys/rwlock.h>
45 #include <sys/sched.h>
47 #include <sys/systm.h>
50 #include <vm/vm_object.h>
51 #include <vm/vm_page.h>
53 #include <vm/vm_map.h>
54 #include <vm/vm_extern.h>
55 #include <vm/vm_param.h>
57 #include <machine/cpu.h>
58 #include <machine/pcb.h>
59 #include <machine/smp.h>
60 #include <machine/md_var.h>
62 #include <x86/apicreg.h>
64 #include <machine/vmm.h>
65 #include <machine/vmm_dev.h>
66 #include <machine/vmm_instruction_emul.h>
68 #include "vmm_ioport.h"
81 #include "vmm_lapic.h"
90 * (a) allocated when vcpu is created
91 * (i) initialized when vcpu is created and when it is reinitialized
92 * (o) initialized the first time the vcpu is created
93 * (x) initialized before use
96 struct mtx mtx; /* (o) protects 'state' and 'hostcpu' */
97 enum vcpu_state state; /* (o) vcpu state */
98 int hostcpu; /* (o) vcpu's host cpu */
99 int reqidle; /* (i) request vcpu to idle */
100 struct vlapic *vlapic; /* (i) APIC device model */
101 enum x2apic_state x2apic_state; /* (i) APIC mode */
102 uint64_t exitintinfo; /* (i) events pending at VM exit */
103 int nmi_pending; /* (i) NMI pending */
104 int extint_pending; /* (i) INTR pending */
105 int exception_pending; /* (i) exception pending */
106 int exc_vector; /* (x) exception collateral */
107 int exc_errcode_valid;
108 uint32_t exc_errcode;
109 struct savefpu *guestfpu; /* (a,i) guest fpu state */
110 uint64_t guest_xcr0; /* (i) guest %xcr0 register */
111 void *stats; /* (a,i) statistics */
112 struct vm_exit exitinfo; /* (x) exit reason and collateral */
113 uint64_t nextrip; /* (x) next instruction to execute */
116 #define vcpu_lock_initialized(v) mtx_initialized(&((v)->mtx))
117 #define vcpu_lock_init(v) mtx_init(&((v)->mtx), "vcpu lock", 0, MTX_SPIN)
118 #define vcpu_lock(v) mtx_lock_spin(&((v)->mtx))
119 #define vcpu_unlock(v) mtx_unlock_spin(&((v)->mtx))
120 #define vcpu_assert_locked(v) mtx_assert(&((v)->mtx), MA_OWNED)
125 struct vm_object *object;
127 #define VM_MAX_MEMSEGS 3
137 #define VM_MAX_MEMMAPS 4
141 * (o) initialized the first time the VM is created
142 * (i) initialized when VM is created and when it is reinitialized
143 * (x) initialized before use
146 void *cookie; /* (i) cpu-specific data */
147 void *iommu; /* (x) iommu-specific data */
148 struct vhpet *vhpet; /* (i) virtual HPET */
149 struct vioapic *vioapic; /* (i) virtual ioapic */
150 struct vatpic *vatpic; /* (i) virtual atpic */
151 struct vatpit *vatpit; /* (i) virtual atpit */
152 struct vpmtmr *vpmtmr; /* (i) virtual ACPI PM timer */
153 struct vrtc *vrtc; /* (o) virtual RTC */
154 volatile cpuset_t active_cpus; /* (i) active vcpus */
155 volatile cpuset_t debug_cpus; /* (i) vcpus stopped for debug */
156 int suspend; /* (i) stop VM execution */
157 volatile cpuset_t suspended_cpus; /* (i) suspended vcpus */
158 volatile cpuset_t halted_cpus; /* (x) cpus in a hard halt */
159 cpuset_t rendezvous_req_cpus; /* (x) rendezvous requested */
160 cpuset_t rendezvous_done_cpus; /* (x) rendezvous finished */
161 void *rendezvous_arg; /* (x) rendezvous func/arg */
162 vm_rendezvous_func_t rendezvous_func;
163 struct mtx rendezvous_mtx; /* (o) rendezvous lock */
164 struct mem_map mem_maps[VM_MAX_MEMMAPS]; /* (i) guest address space */
165 struct mem_seg mem_segs[VM_MAX_MEMSEGS]; /* (o) guest memory regions */
166 struct vmspace *vmspace; /* (o) guest's address space */
167 char name[VM_MAX_NAMELEN]; /* (o) virtual machine name */
168 struct vcpu vcpu[VM_MAXCPU]; /* (i) guest vcpus */
169 /* The following describe the vm cpu topology */
170 uint16_t sockets; /* (o) num of sockets */
171 uint16_t cores; /* (o) num of cores/socket */
172 uint16_t threads; /* (o) num of threads/core */
173 uint16_t maxcpus; /* (o) max pluggable cpus */
176 static int vmm_initialized;
178 static struct vmm_ops *ops;
179 #define VMM_INIT(num) (ops != NULL ? (*ops->init)(num) : 0)
180 #define VMM_CLEANUP() (ops != NULL ? (*ops->cleanup)() : 0)
181 #define VMM_RESUME() (ops != NULL ? (*ops->resume)() : 0)
183 #define VMINIT(vm, pmap) (ops != NULL ? (*ops->vminit)(vm, pmap): NULL)
184 #define VMRUN(vmi, vcpu, rip, pmap, evinfo) \
185 (ops != NULL ? (*ops->vmrun)(vmi, vcpu, rip, pmap, evinfo) : ENXIO)
186 #define VMCLEANUP(vmi) (ops != NULL ? (*ops->vmcleanup)(vmi) : NULL)
187 #define VMSPACE_ALLOC(min, max) \
188 (ops != NULL ? (*ops->vmspace_alloc)(min, max) : NULL)
189 #define VMSPACE_FREE(vmspace) \
190 (ops != NULL ? (*ops->vmspace_free)(vmspace) : ENXIO)
191 #define VMGETREG(vmi, vcpu, num, retval) \
192 (ops != NULL ? (*ops->vmgetreg)(vmi, vcpu, num, retval) : ENXIO)
193 #define VMSETREG(vmi, vcpu, num, val) \
194 (ops != NULL ? (*ops->vmsetreg)(vmi, vcpu, num, val) : ENXIO)
195 #define VMGETDESC(vmi, vcpu, num, desc) \
196 (ops != NULL ? (*ops->vmgetdesc)(vmi, vcpu, num, desc) : ENXIO)
197 #define VMSETDESC(vmi, vcpu, num, desc) \
198 (ops != NULL ? (*ops->vmsetdesc)(vmi, vcpu, num, desc) : ENXIO)
199 #define VMGETCAP(vmi, vcpu, num, retval) \
200 (ops != NULL ? (*ops->vmgetcap)(vmi, vcpu, num, retval) : ENXIO)
201 #define VMSETCAP(vmi, vcpu, num, val) \
202 (ops != NULL ? (*ops->vmsetcap)(vmi, vcpu, num, val) : ENXIO)
203 #define VLAPIC_INIT(vmi, vcpu) \
204 (ops != NULL ? (*ops->vlapic_init)(vmi, vcpu) : NULL)
205 #define VLAPIC_CLEANUP(vmi, vlapic) \
206 (ops != NULL ? (*ops->vlapic_cleanup)(vmi, vlapic) : NULL)
208 #define fpu_start_emulating() load_cr0(rcr0() | CR0_TS)
209 #define fpu_stop_emulating() clts()
211 SDT_PROVIDER_DEFINE(vmm);
213 static MALLOC_DEFINE(M_VM, "vm", "vm");
216 static VMM_STAT(VCPU_TOTAL_RUNTIME, "vcpu total runtime");
218 SYSCTL_NODE(_hw, OID_AUTO, vmm, CTLFLAG_RW, NULL, NULL);
221 * Halt the guest if all vcpus are executing a HLT instruction with
222 * interrupts disabled.
224 static int halt_detection_enabled = 1;
225 SYSCTL_INT(_hw_vmm, OID_AUTO, halt_detection, CTLFLAG_RDTUN,
226 &halt_detection_enabled, 0,
227 "Halt VM if all vcpus execute HLT with interrupts disabled");
229 static int vmm_ipinum;
230 SYSCTL_INT(_hw_vmm, OID_AUTO, ipinum, CTLFLAG_RD, &vmm_ipinum, 0,
231 "IPI vector used for vcpu notifications");
233 static int trace_guest_exceptions;
234 SYSCTL_INT(_hw_vmm, OID_AUTO, trace_guest_exceptions, CTLFLAG_RDTUN,
235 &trace_guest_exceptions, 0,
236 "Trap into hypervisor on all guest exceptions and reflect them back");
238 static void vm_free_memmap(struct vm *vm, int ident);
239 static bool sysmem_mapping(struct vm *vm, struct mem_map *mm);
240 static void vcpu_notify_event_locked(struct vcpu *vcpu, bool lapic_intr);
244 vcpu_state2str(enum vcpu_state state)
263 vcpu_cleanup(struct vm *vm, int i, bool destroy)
265 struct vcpu *vcpu = &vm->vcpu[i];
267 VLAPIC_CLEANUP(vm->cookie, vcpu->vlapic);
269 vmm_stat_free(vcpu->stats);
270 fpu_save_area_free(vcpu->guestfpu);
275 vcpu_init(struct vm *vm, int vcpu_id, bool create)
279 KASSERT(vcpu_id >= 0 && vcpu_id < vm->maxcpus,
280 ("vcpu_init: invalid vcpu %d", vcpu_id));
282 vcpu = &vm->vcpu[vcpu_id];
285 KASSERT(!vcpu_lock_initialized(vcpu), ("vcpu %d already "
286 "initialized", vcpu_id));
287 vcpu_lock_init(vcpu);
288 vcpu->state = VCPU_IDLE;
289 vcpu->hostcpu = NOCPU;
290 vcpu->guestfpu = fpu_save_area_alloc();
291 vcpu->stats = vmm_stat_alloc();
294 vcpu->vlapic = VLAPIC_INIT(vm->cookie, vcpu_id);
295 vm_set_x2apic_state(vm, vcpu_id, X2APIC_DISABLED);
297 vcpu->exitintinfo = 0;
298 vcpu->nmi_pending = 0;
299 vcpu->extint_pending = 0;
300 vcpu->exception_pending = 0;
301 vcpu->guest_xcr0 = XFEATURE_ENABLED_X87;
302 fpu_save_area_reset(vcpu->guestfpu);
303 vmm_stat_init(vcpu->stats);
307 vcpu_trace_exceptions(struct vm *vm, int vcpuid)
310 return (trace_guest_exceptions);
314 vm_exitinfo(struct vm *vm, int cpuid)
318 if (cpuid < 0 || cpuid >= vm->maxcpus)
319 panic("vm_exitinfo: invalid cpuid %d", cpuid);
321 vcpu = &vm->vcpu[cpuid];
323 return (&vcpu->exitinfo);
337 vmm_host_state_init();
339 vmm_ipinum = lapic_ipi_alloc(pti ? &IDTVEC(justreturn1_pti) :
340 &IDTVEC(justreturn));
342 vmm_ipinum = IPI_AST;
344 error = vmm_mem_init();
349 ops = &vmm_ops_intel;
350 else if (vmm_is_amd())
355 vmm_resume_p = vmm_resume;
357 return (VMM_INIT(vmm_ipinum));
361 vmm_handler(module_t mod, int what, void *arg)
373 error = vmmdev_cleanup();
377 if (vmm_ipinum != IPI_AST)
378 lapic_ipi_free(vmm_ipinum);
379 error = VMM_CLEANUP();
381 * Something bad happened - prevent new
382 * VMs from being created
395 static moduledata_t vmm_kmod = {
402 * vmm initialization has the following dependencies:
404 * - VT-x initialization requires smp_rendezvous() and therefore must happen
405 * after SMP is fully functional (after SI_SUB_SMP).
407 DECLARE_MODULE(vmm, vmm_kmod, SI_SUB_SMP + 1, SI_ORDER_ANY);
408 MODULE_VERSION(vmm, 1);
411 vm_init(struct vm *vm, bool create)
415 vm->cookie = VMINIT(vm, vmspace_pmap(vm->vmspace));
417 vm->vioapic = vioapic_init(vm);
418 vm->vhpet = vhpet_init(vm);
419 vm->vatpic = vatpic_init(vm);
420 vm->vatpit = vatpit_init(vm);
421 vm->vpmtmr = vpmtmr_init(vm);
423 vm->vrtc = vrtc_init(vm);
425 CPU_ZERO(&vm->active_cpus);
426 CPU_ZERO(&vm->debug_cpus);
429 CPU_ZERO(&vm->suspended_cpus);
431 for (i = 0; i < vm->maxcpus; i++)
432 vcpu_init(vm, i, create);
436 * The default CPU topology is a single thread per package.
438 u_int cores_per_package = 1;
439 u_int threads_per_core = 1;
442 vm_create(const char *name, struct vm **retvm)
445 struct vmspace *vmspace;
448 * If vmm.ko could not be successfully initialized then don't attempt
449 * to create the virtual machine.
451 if (!vmm_initialized)
454 if (name == NULL || strlen(name) >= VM_MAX_NAMELEN)
457 vmspace = VMSPACE_ALLOC(0, VM_MAXUSER_ADDRESS);
461 vm = malloc(sizeof(struct vm), M_VM, M_WAITOK | M_ZERO);
462 strcpy(vm->name, name);
463 vm->vmspace = vmspace;
464 mtx_init(&vm->rendezvous_mtx, "vm rendezvous lock", 0, MTX_DEF);
467 vm->cores = cores_per_package; /* XXX backwards compatibility */
468 vm->threads = threads_per_core; /* XXX backwards compatibility */
469 vm->maxcpus = VM_MAXCPU; /* XXX temp to keep code working */
478 vm_get_topology(struct vm *vm, uint16_t *sockets, uint16_t *cores,
479 uint16_t *threads, uint16_t *maxcpus)
481 *sockets = vm->sockets;
483 *threads = vm->threads;
484 *maxcpus = vm->maxcpus;
488 vm_get_maxcpus(struct vm *vm)
490 return (vm->maxcpus);
494 vm_set_topology(struct vm *vm, uint16_t sockets, uint16_t cores,
495 uint16_t threads, uint16_t maxcpus)
498 return (EINVAL); /* XXX remove when supported */
499 if ((sockets * cores * threads) > vm->maxcpus)
501 /* XXX need to check sockets * cores * threads == vCPU, how? */
502 vm->sockets = sockets;
504 vm->threads = threads;
505 vm->maxcpus = VM_MAXCPU; /* XXX temp to keep code working */
510 vm_cleanup(struct vm *vm, bool destroy)
515 ppt_unassign_all(vm);
517 if (vm->iommu != NULL)
518 iommu_destroy_domain(vm->iommu);
521 vrtc_cleanup(vm->vrtc);
523 vrtc_reset(vm->vrtc);
524 vpmtmr_cleanup(vm->vpmtmr);
525 vatpit_cleanup(vm->vatpit);
526 vhpet_cleanup(vm->vhpet);
527 vatpic_cleanup(vm->vatpic);
528 vioapic_cleanup(vm->vioapic);
530 for (i = 0; i < vm->maxcpus; i++)
531 vcpu_cleanup(vm, i, destroy);
533 VMCLEANUP(vm->cookie);
536 * System memory is removed from the guest address space only when
537 * the VM is destroyed. This is because the mapping remains the same
540 * Device memory can be relocated by the guest (e.g. using PCI BARs)
541 * so those mappings are removed on a VM reset.
543 for (i = 0; i < VM_MAX_MEMMAPS; i++) {
544 mm = &vm->mem_maps[i];
545 if (destroy || !sysmem_mapping(vm, mm))
546 vm_free_memmap(vm, i);
550 for (i = 0; i < VM_MAX_MEMSEGS; i++)
551 vm_free_memseg(vm, i);
553 VMSPACE_FREE(vm->vmspace);
559 vm_destroy(struct vm *vm)
561 vm_cleanup(vm, true);
566 vm_reinit(struct vm *vm)
571 * A virtual machine can be reset only if all vcpus are suspended.
573 if (CPU_CMP(&vm->suspended_cpus, &vm->active_cpus) == 0) {
574 vm_cleanup(vm, false);
585 vm_name(struct vm *vm)
591 vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
595 if ((obj = vmm_mmio_alloc(vm->vmspace, gpa, len, hpa)) == NULL)
602 vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len)
605 vmm_mmio_free(vm->vmspace, gpa, len);
610 * Return 'true' if 'gpa' is allocated in the guest address space.
612 * This function is called in the context of a running vcpu which acts as
613 * an implicit lock on 'vm->mem_maps[]'.
616 vm_mem_allocated(struct vm *vm, int vcpuid, vm_paddr_t gpa)
623 state = vcpu_get_state(vm, vcpuid, &hostcpu);
624 KASSERT(state == VCPU_RUNNING && hostcpu == curcpu,
625 ("%s: invalid vcpu state %d/%d", __func__, state, hostcpu));
628 for (i = 0; i < VM_MAX_MEMMAPS; i++) {
629 mm = &vm->mem_maps[i];
630 if (mm->len != 0 && gpa >= mm->gpa && gpa < mm->gpa + mm->len)
631 return (true); /* 'gpa' is sysmem or devmem */
634 if (ppt_is_mmio(vm, gpa))
635 return (true); /* 'gpa' is pci passthru mmio */
641 vm_alloc_memseg(struct vm *vm, int ident, size_t len, bool sysmem)
646 if (ident < 0 || ident >= VM_MAX_MEMSEGS)
649 if (len == 0 || (len & PAGE_MASK))
652 seg = &vm->mem_segs[ident];
653 if (seg->object != NULL) {
654 if (seg->len == len && seg->sysmem == sysmem)
660 obj = vm_object_allocate(OBJT_DEFAULT, len >> PAGE_SHIFT);
666 seg->sysmem = sysmem;
671 vm_get_memseg(struct vm *vm, int ident, size_t *len, bool *sysmem,
676 if (ident < 0 || ident >= VM_MAX_MEMSEGS)
679 seg = &vm->mem_segs[ident];
683 *sysmem = seg->sysmem;
685 *objptr = seg->object;
690 vm_free_memseg(struct vm *vm, int ident)
694 KASSERT(ident >= 0 && ident < VM_MAX_MEMSEGS,
695 ("%s: invalid memseg ident %d", __func__, ident));
697 seg = &vm->mem_segs[ident];
698 if (seg->object != NULL) {
699 vm_object_deallocate(seg->object);
700 bzero(seg, sizeof(struct mem_seg));
705 vm_mmap_memseg(struct vm *vm, vm_paddr_t gpa, int segid, vm_ooffset_t first,
706 size_t len, int prot, int flags)
709 struct mem_map *m, *map;
713 if (prot == 0 || (prot & ~(VM_PROT_ALL)) != 0)
716 if (flags & ~VM_MEMMAP_F_WIRED)
719 if (segid < 0 || segid >= VM_MAX_MEMSEGS)
722 seg = &vm->mem_segs[segid];
723 if (seg->object == NULL)
727 if (first < 0 || first >= last || last > seg->len)
730 if ((gpa | first | last) & PAGE_MASK)
734 for (i = 0; i < VM_MAX_MEMMAPS; i++) {
735 m = &vm->mem_maps[i];
745 error = vm_map_find(&vm->vmspace->vm_map, seg->object, first, &gpa,
746 len, 0, VMFS_NO_SPACE, prot, prot, 0);
747 if (error != KERN_SUCCESS)
750 vm_object_reference(seg->object);
752 if (flags & VM_MEMMAP_F_WIRED) {
753 error = vm_map_wire(&vm->vmspace->vm_map, gpa, gpa + len,
754 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
755 if (error != KERN_SUCCESS) {
756 vm_map_remove(&vm->vmspace->vm_map, gpa, gpa + len);
757 return (error == KERN_RESOURCE_SHORTAGE ? ENOMEM :
772 vm_mmap_getnext(struct vm *vm, vm_paddr_t *gpa, int *segid,
773 vm_ooffset_t *segoff, size_t *len, int *prot, int *flags)
775 struct mem_map *mm, *mmnext;
779 for (i = 0; i < VM_MAX_MEMMAPS; i++) {
780 mm = &vm->mem_maps[i];
781 if (mm->len == 0 || mm->gpa < *gpa)
783 if (mmnext == NULL || mm->gpa < mmnext->gpa)
787 if (mmnext != NULL) {
790 *segid = mmnext->segid;
792 *segoff = mmnext->segoff;
796 *prot = mmnext->prot;
798 *flags = mmnext->flags;
806 vm_free_memmap(struct vm *vm, int ident)
811 mm = &vm->mem_maps[ident];
813 error = vm_map_remove(&vm->vmspace->vm_map, mm->gpa,
815 KASSERT(error == KERN_SUCCESS, ("%s: vm_map_remove error %d",
817 bzero(mm, sizeof(struct mem_map));
822 sysmem_mapping(struct vm *vm, struct mem_map *mm)
825 if (mm->len != 0 && vm->mem_segs[mm->segid].sysmem)
832 vmm_sysmem_maxaddr(struct vm *vm)
839 for (i = 0; i < VM_MAX_MEMMAPS; i++) {
840 mm = &vm->mem_maps[i];
841 if (sysmem_mapping(vm, mm)) {
842 if (maxaddr < mm->gpa + mm->len)
843 maxaddr = mm->gpa + mm->len;
850 vm_iommu_modify(struct vm *vm, bool map)
855 void *vp, *cookie, *host_domain;
858 host_domain = iommu_host_domain();
860 for (i = 0; i < VM_MAX_MEMMAPS; i++) {
861 mm = &vm->mem_maps[i];
862 if (!sysmem_mapping(vm, mm))
866 KASSERT((mm->flags & VM_MEMMAP_F_IOMMU) == 0,
867 ("iommu map found invalid memmap %#lx/%#lx/%#x",
868 mm->gpa, mm->len, mm->flags));
869 if ((mm->flags & VM_MEMMAP_F_WIRED) == 0)
871 mm->flags |= VM_MEMMAP_F_IOMMU;
873 if ((mm->flags & VM_MEMMAP_F_IOMMU) == 0)
875 mm->flags &= ~VM_MEMMAP_F_IOMMU;
876 KASSERT((mm->flags & VM_MEMMAP_F_WIRED) != 0,
877 ("iommu unmap found invalid memmap %#lx/%#lx/%#x",
878 mm->gpa, mm->len, mm->flags));
882 while (gpa < mm->gpa + mm->len) {
883 vp = vm_gpa_hold(vm, -1, gpa, PAGE_SIZE, VM_PROT_WRITE,
885 KASSERT(vp != NULL, ("vm(%s) could not map gpa %#lx",
888 vm_gpa_release(cookie);
890 hpa = DMAP_TO_PHYS((uintptr_t)vp);
892 iommu_create_mapping(vm->iommu, gpa, hpa, sz);
893 iommu_remove_mapping(host_domain, hpa, sz);
895 iommu_remove_mapping(vm->iommu, gpa, sz);
896 iommu_create_mapping(host_domain, hpa, hpa, sz);
904 * Invalidate the cached translations associated with the domain
905 * from which pages were removed.
908 iommu_invalidate_tlb(host_domain);
910 iommu_invalidate_tlb(vm->iommu);
913 #define vm_iommu_unmap(vm) vm_iommu_modify((vm), false)
914 #define vm_iommu_map(vm) vm_iommu_modify((vm), true)
917 vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func)
921 error = ppt_unassign_device(vm, bus, slot, func);
925 if (ppt_assigned_devices(vm) == 0)
932 vm_assign_pptdev(struct vm *vm, int bus, int slot, int func)
937 /* Set up the IOMMU to do the 'gpa' to 'hpa' translation */
938 if (ppt_assigned_devices(vm) == 0) {
939 KASSERT(vm->iommu == NULL,
940 ("vm_assign_pptdev: iommu must be NULL"));
941 maxaddr = vmm_sysmem_maxaddr(vm);
942 vm->iommu = iommu_create_domain(maxaddr);
943 if (vm->iommu == NULL)
948 error = ppt_assign_device(vm, bus, slot, func);
953 vm_gpa_hold(struct vm *vm, int vcpuid, vm_paddr_t gpa, size_t len, int reqprot,
956 int i, count, pageoff;
961 * All vcpus are frozen by ioctls that modify the memory map
962 * (e.g. VM_MMAP_MEMSEG). Therefore 'vm->memmap[]' stability is
963 * guaranteed if at least one vcpu is in the VCPU_FROZEN state.
966 KASSERT(vcpuid >= -1 && vcpuid < vm->maxcpus, ("%s: invalid vcpuid %d",
968 for (i = 0; i < vm->maxcpus; i++) {
969 if (vcpuid != -1 && vcpuid != i)
971 state = vcpu_get_state(vm, i, NULL);
972 KASSERT(state == VCPU_FROZEN, ("%s: invalid vcpu state %d",
976 pageoff = gpa & PAGE_MASK;
977 if (len > PAGE_SIZE - pageoff)
978 panic("vm_gpa_hold: invalid gpa/len: 0x%016lx/%lu", gpa, len);
981 for (i = 0; i < VM_MAX_MEMMAPS; i++) {
982 mm = &vm->mem_maps[i];
983 if (sysmem_mapping(vm, mm) && gpa >= mm->gpa &&
984 gpa < mm->gpa + mm->len) {
985 count = vm_fault_quick_hold_pages(&vm->vmspace->vm_map,
986 trunc_page(gpa), PAGE_SIZE, reqprot, &m, 1);
993 return ((void *)(PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)) + pageoff));
1001 vm_gpa_release(void *cookie)
1003 vm_page_t m = cookie;
1006 vm_page_unwire(m, PQ_ACTIVE);
1011 vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval)
1014 if (vcpu < 0 || vcpu >= vm->maxcpus)
1017 if (reg >= VM_REG_LAST)
1020 return (VMGETREG(vm->cookie, vcpu, reg, retval));
1024 vm_set_register(struct vm *vm, int vcpuid, int reg, uint64_t val)
1029 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
1032 if (reg >= VM_REG_LAST)
1035 error = VMSETREG(vm->cookie, vcpuid, reg, val);
1036 if (error || reg != VM_REG_GUEST_RIP)
1039 /* Set 'nextrip' to match the value of %rip */
1040 VCPU_CTR1(vm, vcpuid, "Setting nextrip to %#lx", val);
1041 vcpu = &vm->vcpu[vcpuid];
1042 vcpu->nextrip = val;
1047 is_descriptor_table(int reg)
1051 case VM_REG_GUEST_IDTR:
1052 case VM_REG_GUEST_GDTR:
1060 is_segment_register(int reg)
1064 case VM_REG_GUEST_ES:
1065 case VM_REG_GUEST_CS:
1066 case VM_REG_GUEST_SS:
1067 case VM_REG_GUEST_DS:
1068 case VM_REG_GUEST_FS:
1069 case VM_REG_GUEST_GS:
1070 case VM_REG_GUEST_TR:
1071 case VM_REG_GUEST_LDTR:
1079 vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
1080 struct seg_desc *desc)
1083 if (vcpu < 0 || vcpu >= vm->maxcpus)
1086 if (!is_segment_register(reg) && !is_descriptor_table(reg))
1089 return (VMGETDESC(vm->cookie, vcpu, reg, desc));
1093 vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
1094 struct seg_desc *desc)
1096 if (vcpu < 0 || vcpu >= vm->maxcpus)
1099 if (!is_segment_register(reg) && !is_descriptor_table(reg))
1102 return (VMSETDESC(vm->cookie, vcpu, reg, desc));
1106 restore_guest_fpustate(struct vcpu *vcpu)
1109 /* flush host state to the pcb */
1112 /* restore guest FPU state */
1113 fpu_stop_emulating();
1114 fpurestore(vcpu->guestfpu);
1116 /* restore guest XCR0 if XSAVE is enabled in the host */
1117 if (rcr4() & CR4_XSAVE)
1118 load_xcr(0, vcpu->guest_xcr0);
1121 * The FPU is now "dirty" with the guest's state so turn on emulation
1122 * to trap any access to the FPU by the host.
1124 fpu_start_emulating();
1128 save_guest_fpustate(struct vcpu *vcpu)
1131 if ((rcr0() & CR0_TS) == 0)
1132 panic("fpu emulation not enabled in host!");
1134 /* save guest XCR0 and restore host XCR0 */
1135 if (rcr4() & CR4_XSAVE) {
1136 vcpu->guest_xcr0 = rxcr(0);
1137 load_xcr(0, vmm_get_host_xcr0());
1140 /* save guest FPU state */
1141 fpu_stop_emulating();
1142 fpusave(vcpu->guestfpu);
1143 fpu_start_emulating();
1146 static VMM_STAT(VCPU_IDLE_TICKS, "number of ticks vcpu was idle");
1149 vcpu_set_state_locked(struct vm *vm, int vcpuid, enum vcpu_state newstate,
1155 vcpu = &vm->vcpu[vcpuid];
1156 vcpu_assert_locked(vcpu);
1159 * State transitions from the vmmdev_ioctl() must always begin from
1160 * the VCPU_IDLE state. This guarantees that there is only a single
1161 * ioctl() operating on a vcpu at any point.
1164 while (vcpu->state != VCPU_IDLE) {
1166 vcpu_notify_event_locked(vcpu, false);
1167 VCPU_CTR1(vm, vcpuid, "vcpu state change from %s to "
1168 "idle requested", vcpu_state2str(vcpu->state));
1169 msleep_spin(&vcpu->state, &vcpu->mtx, "vmstat", hz);
1172 KASSERT(vcpu->state != VCPU_IDLE, ("invalid transition from "
1173 "vcpu idle state"));
1176 if (vcpu->state == VCPU_RUNNING) {
1177 KASSERT(vcpu->hostcpu == curcpu, ("curcpu %d and hostcpu %d "
1178 "mismatch for running vcpu", curcpu, vcpu->hostcpu));
1180 KASSERT(vcpu->hostcpu == NOCPU, ("Invalid hostcpu %d for a "
1181 "vcpu that is not running", vcpu->hostcpu));
1185 * The following state transitions are allowed:
1186 * IDLE -> FROZEN -> IDLE
1187 * FROZEN -> RUNNING -> FROZEN
1188 * FROZEN -> SLEEPING -> FROZEN
1190 switch (vcpu->state) {
1194 error = (newstate != VCPU_FROZEN);
1197 error = (newstate == VCPU_FROZEN);
1207 VCPU_CTR2(vm, vcpuid, "vcpu state changed from %s to %s",
1208 vcpu_state2str(vcpu->state), vcpu_state2str(newstate));
1210 vcpu->state = newstate;
1211 if (newstate == VCPU_RUNNING)
1212 vcpu->hostcpu = curcpu;
1214 vcpu->hostcpu = NOCPU;
1216 if (newstate == VCPU_IDLE)
1217 wakeup(&vcpu->state);
1223 vcpu_require_state(struct vm *vm, int vcpuid, enum vcpu_state newstate)
1227 if ((error = vcpu_set_state(vm, vcpuid, newstate, false)) != 0)
1228 panic("Error %d setting state to %d\n", error, newstate);
1232 vcpu_require_state_locked(struct vm *vm, int vcpuid, enum vcpu_state newstate)
1236 if ((error = vcpu_set_state_locked(vm, vcpuid, newstate, false)) != 0)
1237 panic("Error %d setting state to %d", error, newstate);
1241 vm_set_rendezvous_func(struct vm *vm, vm_rendezvous_func_t func)
1244 KASSERT(mtx_owned(&vm->rendezvous_mtx), ("rendezvous_mtx not locked"));
1247 * Update 'rendezvous_func' and execute a write memory barrier to
1248 * ensure that it is visible across all host cpus. This is not needed
1249 * for correctness but it does ensure that all the vcpus will notice
1250 * that the rendezvous is requested immediately.
1252 vm->rendezvous_func = func;
1256 #define RENDEZVOUS_CTR0(vm, vcpuid, fmt) \
1259 VCPU_CTR0(vm, vcpuid, fmt); \
1265 vm_handle_rendezvous(struct vm *vm, int vcpuid)
1268 KASSERT(vcpuid == -1 || (vcpuid >= 0 && vcpuid < vm->maxcpus),
1269 ("vm_handle_rendezvous: invalid vcpuid %d", vcpuid));
1271 mtx_lock(&vm->rendezvous_mtx);
1272 while (vm->rendezvous_func != NULL) {
1273 /* 'rendezvous_req_cpus' must be a subset of 'active_cpus' */
1274 CPU_AND(&vm->rendezvous_req_cpus, &vm->active_cpus);
1277 CPU_ISSET(vcpuid, &vm->rendezvous_req_cpus) &&
1278 !CPU_ISSET(vcpuid, &vm->rendezvous_done_cpus)) {
1279 VCPU_CTR0(vm, vcpuid, "Calling rendezvous func");
1280 (*vm->rendezvous_func)(vm, vcpuid, vm->rendezvous_arg);
1281 CPU_SET(vcpuid, &vm->rendezvous_done_cpus);
1283 if (CPU_CMP(&vm->rendezvous_req_cpus,
1284 &vm->rendezvous_done_cpus) == 0) {
1285 VCPU_CTR0(vm, vcpuid, "Rendezvous completed");
1286 vm_set_rendezvous_func(vm, NULL);
1287 wakeup(&vm->rendezvous_func);
1290 RENDEZVOUS_CTR0(vm, vcpuid, "Wait for rendezvous completion");
1291 mtx_sleep(&vm->rendezvous_func, &vm->rendezvous_mtx, 0,
1294 mtx_unlock(&vm->rendezvous_mtx);
1298 * Emulate a guest 'hlt' by sleeping until the vcpu is ready to run.
1301 vm_handle_hlt(struct vm *vm, int vcpuid, bool intr_disabled, bool *retu)
1305 int t, vcpu_halted, vm_halted;
1307 KASSERT(!CPU_ISSET(vcpuid, &vm->halted_cpus), ("vcpu already halted"));
1309 vcpu = &vm->vcpu[vcpuid];
1316 * Do a final check for pending NMI or interrupts before
1317 * really putting this thread to sleep. Also check for
1318 * software events that would cause this vcpu to wakeup.
1320 * These interrupts/events could have happened after the
1321 * vcpu returned from VMRUN() and before it acquired the
1324 if (vm->rendezvous_func != NULL || vm->suspend || vcpu->reqidle)
1326 if (vm_nmi_pending(vm, vcpuid))
1328 if (!intr_disabled) {
1329 if (vm_extint_pending(vm, vcpuid) ||
1330 vlapic_pending_intr(vcpu->vlapic, NULL)) {
1335 /* Don't go to sleep if the vcpu thread needs to yield */
1336 if (vcpu_should_yield(vm, vcpuid))
1339 if (vcpu_debugged(vm, vcpuid))
1343 * Some Linux guests implement "halt" by having all vcpus
1344 * execute HLT with interrupts disabled. 'halted_cpus' keeps
1345 * track of the vcpus that have entered this state. When all
1346 * vcpus enter the halted state the virtual machine is halted.
1348 if (intr_disabled) {
1350 VCPU_CTR0(vm, vcpuid, "Halted");
1351 if (!vcpu_halted && halt_detection_enabled) {
1353 CPU_SET_ATOMIC(vcpuid, &vm->halted_cpus);
1355 if (CPU_CMP(&vm->halted_cpus, &vm->active_cpus) == 0) {
1364 vcpu_require_state_locked(vm, vcpuid, VCPU_SLEEPING);
1366 * XXX msleep_spin() cannot be interrupted by signals so
1367 * wake up periodically to check pending signals.
1369 msleep_spin(vcpu, &vcpu->mtx, wmesg, hz);
1370 vcpu_require_state_locked(vm, vcpuid, VCPU_FROZEN);
1371 vmm_stat_incr(vm, vcpuid, VCPU_IDLE_TICKS, ticks - t);
1375 CPU_CLR_ATOMIC(vcpuid, &vm->halted_cpus);
1380 vm_suspend(vm, VM_SUSPEND_HALT);
1386 vm_handle_paging(struct vm *vm, int vcpuid, bool *retu)
1391 struct vm_exit *vme;
1393 vcpu = &vm->vcpu[vcpuid];
1394 vme = &vcpu->exitinfo;
1396 KASSERT(vme->inst_length == 0, ("%s: invalid inst_length %d",
1397 __func__, vme->inst_length));
1399 ftype = vme->u.paging.fault_type;
1400 KASSERT(ftype == VM_PROT_READ ||
1401 ftype == VM_PROT_WRITE || ftype == VM_PROT_EXECUTE,
1402 ("vm_handle_paging: invalid fault_type %d", ftype));
1404 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) {
1405 rv = pmap_emulate_accessed_dirty(vmspace_pmap(vm->vmspace),
1406 vme->u.paging.gpa, ftype);
1408 VCPU_CTR2(vm, vcpuid, "%s bit emulation for gpa %#lx",
1409 ftype == VM_PROT_READ ? "accessed" : "dirty",
1415 map = &vm->vmspace->vm_map;
1416 rv = vm_fault(map, vme->u.paging.gpa, ftype, VM_FAULT_NORMAL);
1418 VCPU_CTR3(vm, vcpuid, "vm_handle_paging rv = %d, gpa = %#lx, "
1419 "ftype = %d", rv, vme->u.paging.gpa, ftype);
1421 if (rv != KERN_SUCCESS)
1428 vm_handle_inst_emul(struct vm *vm, int vcpuid, bool *retu)
1432 struct vm_exit *vme;
1433 uint64_t gla, gpa, cs_base;
1434 struct vm_guest_paging *paging;
1435 mem_region_read_t mread;
1436 mem_region_write_t mwrite;
1437 enum vm_cpu_mode cpu_mode;
1438 int cs_d, error, fault;
1440 vcpu = &vm->vcpu[vcpuid];
1441 vme = &vcpu->exitinfo;
1443 KASSERT(vme->inst_length == 0, ("%s: invalid inst_length %d",
1444 __func__, vme->inst_length));
1446 gla = vme->u.inst_emul.gla;
1447 gpa = vme->u.inst_emul.gpa;
1448 cs_base = vme->u.inst_emul.cs_base;
1449 cs_d = vme->u.inst_emul.cs_d;
1450 vie = &vme->u.inst_emul.vie;
1451 paging = &vme->u.inst_emul.paging;
1452 cpu_mode = paging->cpu_mode;
1454 VCPU_CTR1(vm, vcpuid, "inst_emul fault accessing gpa %#lx", gpa);
1456 /* Fetch, decode and emulate the faulting instruction */
1457 if (vie->num_valid == 0) {
1458 error = vmm_fetch_instruction(vm, vcpuid, paging, vme->rip +
1459 cs_base, VIE_INST_SIZE, vie, &fault);
1462 * The instruction bytes have already been copied into 'vie'
1469 if (vmm_decode_instruction(vm, vcpuid, gla, cpu_mode, cs_d, vie) != 0) {
1470 VCPU_CTR1(vm, vcpuid, "Error decoding instruction at %#lx",
1471 vme->rip + cs_base);
1472 *retu = true; /* dump instruction bytes in userspace */
1477 * Update 'nextrip' based on the length of the emulated instruction.
1479 vme->inst_length = vie->num_processed;
1480 vcpu->nextrip += vie->num_processed;
1481 VCPU_CTR1(vm, vcpuid, "nextrip updated to %#lx after instruction "
1482 "decoding", vcpu->nextrip);
1484 /* return to userland unless this is an in-kernel emulated device */
1485 if (gpa >= DEFAULT_APIC_BASE && gpa < DEFAULT_APIC_BASE + PAGE_SIZE) {
1486 mread = lapic_mmio_read;
1487 mwrite = lapic_mmio_write;
1488 } else if (gpa >= VIOAPIC_BASE && gpa < VIOAPIC_BASE + VIOAPIC_SIZE) {
1489 mread = vioapic_mmio_read;
1490 mwrite = vioapic_mmio_write;
1491 } else if (gpa >= VHPET_BASE && gpa < VHPET_BASE + VHPET_SIZE) {
1492 mread = vhpet_mmio_read;
1493 mwrite = vhpet_mmio_write;
1499 error = vmm_emulate_instruction(vm, vcpuid, gpa, vie, paging,
1500 mread, mwrite, retu);
1506 vm_handle_suspend(struct vm *vm, int vcpuid, bool *retu)
1512 vcpu = &vm->vcpu[vcpuid];
1514 CPU_SET_ATOMIC(vcpuid, &vm->suspended_cpus);
1517 * Wait until all 'active_cpus' have suspended themselves.
1519 * Since a VM may be suspended at any time including when one or
1520 * more vcpus are doing a rendezvous we need to call the rendezvous
1521 * handler while we are waiting to prevent a deadlock.
1525 if (CPU_CMP(&vm->suspended_cpus, &vm->active_cpus) == 0) {
1526 VCPU_CTR0(vm, vcpuid, "All vcpus suspended");
1530 if (vm->rendezvous_func == NULL) {
1531 VCPU_CTR0(vm, vcpuid, "Sleeping during suspend");
1532 vcpu_require_state_locked(vm, vcpuid, VCPU_SLEEPING);
1533 msleep_spin(vcpu, &vcpu->mtx, "vmsusp", hz);
1534 vcpu_require_state_locked(vm, vcpuid, VCPU_FROZEN);
1536 VCPU_CTR0(vm, vcpuid, "Rendezvous during suspend");
1538 vm_handle_rendezvous(vm, vcpuid);
1545 * Wakeup the other sleeping vcpus and return to userspace.
1547 for (i = 0; i < vm->maxcpus; i++) {
1548 if (CPU_ISSET(i, &vm->suspended_cpus)) {
1549 vcpu_notify_event(vm, i, false);
1558 vm_handle_reqidle(struct vm *vm, int vcpuid, bool *retu)
1560 struct vcpu *vcpu = &vm->vcpu[vcpuid];
1563 KASSERT(vcpu->reqidle, ("invalid vcpu reqidle %d", vcpu->reqidle));
1571 vm_suspend(struct vm *vm, enum vm_suspend_how how)
1575 if (how <= VM_SUSPEND_NONE || how >= VM_SUSPEND_LAST)
1578 if (atomic_cmpset_int(&vm->suspend, 0, how) == 0) {
1579 VM_CTR2(vm, "virtual machine already suspended %d/%d",
1584 VM_CTR1(vm, "virtual machine successfully suspended %d", how);
1587 * Notify all active vcpus that they are now suspended.
1589 for (i = 0; i < vm->maxcpus; i++) {
1590 if (CPU_ISSET(i, &vm->active_cpus))
1591 vcpu_notify_event(vm, i, false);
1598 vm_exit_suspended(struct vm *vm, int vcpuid, uint64_t rip)
1600 struct vm_exit *vmexit;
1602 KASSERT(vm->suspend > VM_SUSPEND_NONE && vm->suspend < VM_SUSPEND_LAST,
1603 ("vm_exit_suspended: invalid suspend type %d", vm->suspend));
1605 vmexit = vm_exitinfo(vm, vcpuid);
1607 vmexit->inst_length = 0;
1608 vmexit->exitcode = VM_EXITCODE_SUSPENDED;
1609 vmexit->u.suspended.how = vm->suspend;
1613 vm_exit_debug(struct vm *vm, int vcpuid, uint64_t rip)
1615 struct vm_exit *vmexit;
1617 vmexit = vm_exitinfo(vm, vcpuid);
1619 vmexit->inst_length = 0;
1620 vmexit->exitcode = VM_EXITCODE_DEBUG;
1624 vm_exit_rendezvous(struct vm *vm, int vcpuid, uint64_t rip)
1626 struct vm_exit *vmexit;
1628 KASSERT(vm->rendezvous_func != NULL, ("rendezvous not in progress"));
1630 vmexit = vm_exitinfo(vm, vcpuid);
1632 vmexit->inst_length = 0;
1633 vmexit->exitcode = VM_EXITCODE_RENDEZVOUS;
1634 vmm_stat_incr(vm, vcpuid, VMEXIT_RENDEZVOUS, 1);
1638 vm_exit_reqidle(struct vm *vm, int vcpuid, uint64_t rip)
1640 struct vm_exit *vmexit;
1642 vmexit = vm_exitinfo(vm, vcpuid);
1644 vmexit->inst_length = 0;
1645 vmexit->exitcode = VM_EXITCODE_REQIDLE;
1646 vmm_stat_incr(vm, vcpuid, VMEXIT_REQIDLE, 1);
1650 vm_exit_astpending(struct vm *vm, int vcpuid, uint64_t rip)
1652 struct vm_exit *vmexit;
1654 vmexit = vm_exitinfo(vm, vcpuid);
1656 vmexit->inst_length = 0;
1657 vmexit->exitcode = VM_EXITCODE_BOGUS;
1658 vmm_stat_incr(vm, vcpuid, VMEXIT_ASTPENDING, 1);
1662 vm_run(struct vm *vm, struct vm_run *vmrun)
1664 struct vm_eventinfo evinfo;
1669 struct vm_exit *vme;
1670 bool retu, intr_disabled;
1673 vcpuid = vmrun->cpuid;
1675 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
1678 if (!CPU_ISSET(vcpuid, &vm->active_cpus))
1681 if (CPU_ISSET(vcpuid, &vm->suspended_cpus))
1684 pmap = vmspace_pmap(vm->vmspace);
1685 vcpu = &vm->vcpu[vcpuid];
1686 vme = &vcpu->exitinfo;
1687 evinfo.rptr = &vm->rendezvous_func;
1688 evinfo.sptr = &vm->suspend;
1689 evinfo.iptr = &vcpu->reqidle;
1693 KASSERT(!CPU_ISSET(curcpu, &pmap->pm_active),
1694 ("vm_run: absurd pm_active"));
1698 pcb = PCPU_GET(curpcb);
1699 set_pcb_flags(pcb, PCB_FULL_IRET);
1701 restore_guest_fpustate(vcpu);
1703 vcpu_require_state(vm, vcpuid, VCPU_RUNNING);
1704 error = VMRUN(vm->cookie, vcpuid, vcpu->nextrip, pmap, &evinfo);
1705 vcpu_require_state(vm, vcpuid, VCPU_FROZEN);
1707 save_guest_fpustate(vcpu);
1709 vmm_stat_incr(vm, vcpuid, VCPU_TOTAL_RUNTIME, rdtsc() - tscval);
1715 vcpu->nextrip = vme->rip + vme->inst_length;
1716 switch (vme->exitcode) {
1717 case VM_EXITCODE_REQIDLE:
1718 error = vm_handle_reqidle(vm, vcpuid, &retu);
1720 case VM_EXITCODE_SUSPENDED:
1721 error = vm_handle_suspend(vm, vcpuid, &retu);
1723 case VM_EXITCODE_IOAPIC_EOI:
1724 vioapic_process_eoi(vm, vcpuid,
1725 vme->u.ioapic_eoi.vector);
1727 case VM_EXITCODE_RENDEZVOUS:
1728 vm_handle_rendezvous(vm, vcpuid);
1731 case VM_EXITCODE_HLT:
1732 intr_disabled = ((vme->u.hlt.rflags & PSL_I) == 0);
1733 error = vm_handle_hlt(vm, vcpuid, intr_disabled, &retu);
1735 case VM_EXITCODE_PAGING:
1736 error = vm_handle_paging(vm, vcpuid, &retu);
1738 case VM_EXITCODE_INST_EMUL:
1739 error = vm_handle_inst_emul(vm, vcpuid, &retu);
1741 case VM_EXITCODE_INOUT:
1742 case VM_EXITCODE_INOUT_STR:
1743 error = vm_handle_inout(vm, vcpuid, vme, &retu);
1745 case VM_EXITCODE_MONITOR:
1746 case VM_EXITCODE_MWAIT:
1747 case VM_EXITCODE_VMINSN:
1748 vm_inject_ud(vm, vcpuid);
1751 retu = true; /* handled in userland */
1756 if (error == 0 && retu == false)
1759 VCPU_CTR2(vm, vcpuid, "retu %d/%d", error, vme->exitcode);
1761 /* copy the exit information */
1762 bcopy(vme, &vmrun->vm_exit, sizeof(struct vm_exit));
1767 vm_restart_instruction(void *arg, int vcpuid)
1771 enum vcpu_state state;
1776 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
1779 vcpu = &vm->vcpu[vcpuid];
1780 state = vcpu_get_state(vm, vcpuid, NULL);
1781 if (state == VCPU_RUNNING) {
1783 * When a vcpu is "running" the next instruction is determined
1784 * by adding 'rip' and 'inst_length' in the vcpu's 'exitinfo'.
1785 * Thus setting 'inst_length' to zero will cause the current
1786 * instruction to be restarted.
1788 vcpu->exitinfo.inst_length = 0;
1789 VCPU_CTR1(vm, vcpuid, "restarting instruction at %#lx by "
1790 "setting inst_length to zero", vcpu->exitinfo.rip);
1791 } else if (state == VCPU_FROZEN) {
1793 * When a vcpu is "frozen" it is outside the critical section
1794 * around VMRUN() and 'nextrip' points to the next instruction.
1795 * Thus instruction restart is achieved by setting 'nextrip'
1796 * to the vcpu's %rip.
1798 error = vm_get_register(vm, vcpuid, VM_REG_GUEST_RIP, &rip);
1799 KASSERT(!error, ("%s: error %d getting rip", __func__, error));
1800 VCPU_CTR2(vm, vcpuid, "restarting instruction by updating "
1801 "nextrip from %#lx to %#lx", vcpu->nextrip, rip);
1802 vcpu->nextrip = rip;
1804 panic("%s: invalid state %d", __func__, state);
1810 vm_exit_intinfo(struct vm *vm, int vcpuid, uint64_t info)
1815 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
1818 vcpu = &vm->vcpu[vcpuid];
1820 if (info & VM_INTINFO_VALID) {
1821 type = info & VM_INTINFO_TYPE;
1822 vector = info & 0xff;
1823 if (type == VM_INTINFO_NMI && vector != IDT_NMI)
1825 if (type == VM_INTINFO_HWEXCEPTION && vector >= 32)
1827 if (info & VM_INTINFO_RSVD)
1832 VCPU_CTR2(vm, vcpuid, "%s: info1(%#lx)", __func__, info);
1833 vcpu->exitintinfo = info;
1843 #define IDT_VE 20 /* Virtualization Exception (Intel specific) */
1845 static enum exc_class
1846 exception_class(uint64_t info)
1850 KASSERT(info & VM_INTINFO_VALID, ("intinfo must be valid: %#lx", info));
1851 type = info & VM_INTINFO_TYPE;
1852 vector = info & 0xff;
1854 /* Table 6-4, "Interrupt and Exception Classes", Intel SDM, Vol 3 */
1856 case VM_INTINFO_HWINTR:
1857 case VM_INTINFO_SWINTR:
1858 case VM_INTINFO_NMI:
1859 return (EXC_BENIGN);
1862 * Hardware exception.
1864 * SVM and VT-x use identical type values to represent NMI,
1865 * hardware interrupt and software interrupt.
1867 * SVM uses type '3' for all exceptions. VT-x uses type '3'
1868 * for exceptions except #BP and #OF. #BP and #OF use a type
1869 * value of '5' or '6'. Therefore we don't check for explicit
1870 * values of 'type' to classify 'intinfo' into a hardware
1879 return (EXC_PAGEFAULT);
1885 return (EXC_CONTRIBUTORY);
1887 return (EXC_BENIGN);
1892 nested_fault(struct vm *vm, int vcpuid, uint64_t info1, uint64_t info2,
1895 enum exc_class exc1, exc2;
1898 KASSERT(info1 & VM_INTINFO_VALID, ("info1 %#lx is not valid", info1));
1899 KASSERT(info2 & VM_INTINFO_VALID, ("info2 %#lx is not valid", info2));
1902 * If an exception occurs while attempting to call the double-fault
1903 * handler the processor enters shutdown mode (aka triple fault).
1905 type1 = info1 & VM_INTINFO_TYPE;
1906 vector1 = info1 & 0xff;
1907 if (type1 == VM_INTINFO_HWEXCEPTION && vector1 == IDT_DF) {
1908 VCPU_CTR2(vm, vcpuid, "triple fault: info1(%#lx), info2(%#lx)",
1910 vm_suspend(vm, VM_SUSPEND_TRIPLEFAULT);
1916 * Table 6-5 "Conditions for Generating a Double Fault", Intel SDM, Vol3
1918 exc1 = exception_class(info1);
1919 exc2 = exception_class(info2);
1920 if ((exc1 == EXC_CONTRIBUTORY && exc2 == EXC_CONTRIBUTORY) ||
1921 (exc1 == EXC_PAGEFAULT && exc2 != EXC_BENIGN)) {
1922 /* Convert nested fault into a double fault. */
1924 *retinfo |= VM_INTINFO_VALID | VM_INTINFO_HWEXCEPTION;
1925 *retinfo |= VM_INTINFO_DEL_ERRCODE;
1927 /* Handle exceptions serially */
1934 vcpu_exception_intinfo(struct vcpu *vcpu)
1938 if (vcpu->exception_pending) {
1939 info = vcpu->exc_vector & 0xff;
1940 info |= VM_INTINFO_VALID | VM_INTINFO_HWEXCEPTION;
1941 if (vcpu->exc_errcode_valid) {
1942 info |= VM_INTINFO_DEL_ERRCODE;
1943 info |= (uint64_t)vcpu->exc_errcode << 32;
1950 vm_entry_intinfo(struct vm *vm, int vcpuid, uint64_t *retinfo)
1953 uint64_t info1, info2;
1956 KASSERT(vcpuid >= 0 &&
1957 vcpuid < vm->maxcpus, ("invalid vcpu %d", vcpuid));
1959 vcpu = &vm->vcpu[vcpuid];
1961 info1 = vcpu->exitintinfo;
1962 vcpu->exitintinfo = 0;
1965 if (vcpu->exception_pending) {
1966 info2 = vcpu_exception_intinfo(vcpu);
1967 vcpu->exception_pending = 0;
1968 VCPU_CTR2(vm, vcpuid, "Exception %d delivered: %#lx",
1969 vcpu->exc_vector, info2);
1972 if ((info1 & VM_INTINFO_VALID) && (info2 & VM_INTINFO_VALID)) {
1973 valid = nested_fault(vm, vcpuid, info1, info2, retinfo);
1974 } else if (info1 & VM_INTINFO_VALID) {
1977 } else if (info2 & VM_INTINFO_VALID) {
1985 VCPU_CTR4(vm, vcpuid, "%s: info1(%#lx), info2(%#lx), "
1986 "retinfo(%#lx)", __func__, info1, info2, *retinfo);
1993 vm_get_intinfo(struct vm *vm, int vcpuid, uint64_t *info1, uint64_t *info2)
1997 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
2000 vcpu = &vm->vcpu[vcpuid];
2001 *info1 = vcpu->exitintinfo;
2002 *info2 = vcpu_exception_intinfo(vcpu);
2007 vm_inject_exception(struct vm *vm, int vcpuid, int vector, int errcode_valid,
2008 uint32_t errcode, int restart_instruction)
2014 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
2017 if (vector < 0 || vector >= 32)
2021 * A double fault exception should never be injected directly into
2022 * the guest. It is a derived exception that results from specific
2023 * combinations of nested faults.
2025 if (vector == IDT_DF)
2028 vcpu = &vm->vcpu[vcpuid];
2030 if (vcpu->exception_pending) {
2031 VCPU_CTR2(vm, vcpuid, "Unable to inject exception %d due to "
2032 "pending exception %d", vector, vcpu->exc_vector);
2036 if (errcode_valid) {
2038 * Exceptions don't deliver an error code in real mode.
2040 error = vm_get_register(vm, vcpuid, VM_REG_GUEST_CR0, ®val);
2041 KASSERT(!error, ("%s: error %d getting CR0", __func__, error));
2042 if (!(regval & CR0_PE))
2047 * From section 26.6.1 "Interruptibility State" in Intel SDM:
2049 * Event blocking by "STI" or "MOV SS" is cleared after guest executes
2050 * one instruction or incurs an exception.
2052 error = vm_set_register(vm, vcpuid, VM_REG_GUEST_INTR_SHADOW, 0);
2053 KASSERT(error == 0, ("%s: error %d clearing interrupt shadow",
2056 if (restart_instruction)
2057 vm_restart_instruction(vm, vcpuid);
2059 vcpu->exception_pending = 1;
2060 vcpu->exc_vector = vector;
2061 vcpu->exc_errcode = errcode;
2062 vcpu->exc_errcode_valid = errcode_valid;
2063 VCPU_CTR1(vm, vcpuid, "Exception %d pending", vector);
2068 vm_inject_fault(void *vmarg, int vcpuid, int vector, int errcode_valid,
2072 int error, restart_instruction;
2075 restart_instruction = 1;
2077 error = vm_inject_exception(vm, vcpuid, vector, errcode_valid,
2078 errcode, restart_instruction);
2079 KASSERT(error == 0, ("vm_inject_exception error %d", error));
2083 vm_inject_pf(void *vmarg, int vcpuid, int error_code, uint64_t cr2)
2089 VCPU_CTR2(vm, vcpuid, "Injecting page fault: error_code %#x, cr2 %#lx",
2092 error = vm_set_register(vm, vcpuid, VM_REG_GUEST_CR2, cr2);
2093 KASSERT(error == 0, ("vm_set_register(cr2) error %d", error));
2095 vm_inject_fault(vm, vcpuid, IDT_PF, 1, error_code);
2098 static VMM_STAT(VCPU_NMI_COUNT, "number of NMIs delivered to vcpu");
2101 vm_inject_nmi(struct vm *vm, int vcpuid)
2105 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
2108 vcpu = &vm->vcpu[vcpuid];
2110 vcpu->nmi_pending = 1;
2111 vcpu_notify_event(vm, vcpuid, false);
2116 vm_nmi_pending(struct vm *vm, int vcpuid)
2120 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
2121 panic("vm_nmi_pending: invalid vcpuid %d", vcpuid);
2123 vcpu = &vm->vcpu[vcpuid];
2125 return (vcpu->nmi_pending);
2129 vm_nmi_clear(struct vm *vm, int vcpuid)
2133 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
2134 panic("vm_nmi_pending: invalid vcpuid %d", vcpuid);
2136 vcpu = &vm->vcpu[vcpuid];
2138 if (vcpu->nmi_pending == 0)
2139 panic("vm_nmi_clear: inconsistent nmi_pending state");
2141 vcpu->nmi_pending = 0;
2142 vmm_stat_incr(vm, vcpuid, VCPU_NMI_COUNT, 1);
2145 static VMM_STAT(VCPU_EXTINT_COUNT, "number of ExtINTs delivered to vcpu");
2148 vm_inject_extint(struct vm *vm, int vcpuid)
2152 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
2155 vcpu = &vm->vcpu[vcpuid];
2157 vcpu->extint_pending = 1;
2158 vcpu_notify_event(vm, vcpuid, false);
2163 vm_extint_pending(struct vm *vm, int vcpuid)
2167 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
2168 panic("vm_extint_pending: invalid vcpuid %d", vcpuid);
2170 vcpu = &vm->vcpu[vcpuid];
2172 return (vcpu->extint_pending);
2176 vm_extint_clear(struct vm *vm, int vcpuid)
2180 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
2181 panic("vm_extint_pending: invalid vcpuid %d", vcpuid);
2183 vcpu = &vm->vcpu[vcpuid];
2185 if (vcpu->extint_pending == 0)
2186 panic("vm_extint_clear: inconsistent extint_pending state");
2188 vcpu->extint_pending = 0;
2189 vmm_stat_incr(vm, vcpuid, VCPU_EXTINT_COUNT, 1);
2193 vm_get_capability(struct vm *vm, int vcpu, int type, int *retval)
2195 if (vcpu < 0 || vcpu >= vm->maxcpus)
2198 if (type < 0 || type >= VM_CAP_MAX)
2201 return (VMGETCAP(vm->cookie, vcpu, type, retval));
2205 vm_set_capability(struct vm *vm, int vcpu, int type, int val)
2207 if (vcpu < 0 || vcpu >= vm->maxcpus)
2210 if (type < 0 || type >= VM_CAP_MAX)
2213 return (VMSETCAP(vm->cookie, vcpu, type, val));
2217 vm_lapic(struct vm *vm, int cpu)
2219 return (vm->vcpu[cpu].vlapic);
2223 vm_ioapic(struct vm *vm)
2226 return (vm->vioapic);
2230 vm_hpet(struct vm *vm)
2237 vmm_is_pptdev(int bus, int slot, int func)
2240 char *val, *cp, *cp2;
2245 * The length of an environment variable is limited to 128 bytes which
2246 * puts an upper limit on the number of passthru devices that may be
2247 * specified using a single environment variable.
2249 * Work around this by scanning multiple environment variable
2250 * names instead of a single one - yuck!
2252 const char *names[] = { "pptdevs", "pptdevs2", "pptdevs3", NULL };
2254 /* set pptdevs="1/2/3 4/5/6 7/8/9 10/11/12" */
2256 for (i = 0; names[i] != NULL && !found; i++) {
2257 cp = val = kern_getenv(names[i]);
2258 while (cp != NULL && *cp != '\0') {
2259 if ((cp2 = strchr(cp, ' ')) != NULL)
2262 n = sscanf(cp, "%d/%d/%d", &b, &s, &f);
2263 if (n == 3 && bus == b && slot == s && func == f) {
2279 vm_iommu_domain(struct vm *vm)
2286 vcpu_set_state(struct vm *vm, int vcpuid, enum vcpu_state newstate,
2292 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
2293 panic("vm_set_run_state: invalid vcpuid %d", vcpuid);
2295 vcpu = &vm->vcpu[vcpuid];
2298 error = vcpu_set_state_locked(vm, vcpuid, newstate, from_idle);
2305 vcpu_get_state(struct vm *vm, int vcpuid, int *hostcpu)
2308 enum vcpu_state state;
2310 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
2311 panic("vm_get_run_state: invalid vcpuid %d", vcpuid);
2313 vcpu = &vm->vcpu[vcpuid];
2316 state = vcpu->state;
2317 if (hostcpu != NULL)
2318 *hostcpu = vcpu->hostcpu;
2325 vm_activate_cpu(struct vm *vm, int vcpuid)
2328 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
2331 if (CPU_ISSET(vcpuid, &vm->active_cpus))
2334 VCPU_CTR0(vm, vcpuid, "activated");
2335 CPU_SET_ATOMIC(vcpuid, &vm->active_cpus);
2340 vm_suspend_cpu(struct vm *vm, int vcpuid)
2344 if (vcpuid < -1 || vcpuid >= vm->maxcpus)
2348 vm->debug_cpus = vm->active_cpus;
2349 for (i = 0; i < vm->maxcpus; i++) {
2350 if (CPU_ISSET(i, &vm->active_cpus))
2351 vcpu_notify_event(vm, i, false);
2354 if (!CPU_ISSET(vcpuid, &vm->active_cpus))
2357 CPU_SET_ATOMIC(vcpuid, &vm->debug_cpus);
2358 vcpu_notify_event(vm, vcpuid, false);
2364 vm_resume_cpu(struct vm *vm, int vcpuid)
2367 if (vcpuid < -1 || vcpuid >= vm->maxcpus)
2371 CPU_ZERO(&vm->debug_cpus);
2373 if (!CPU_ISSET(vcpuid, &vm->debug_cpus))
2376 CPU_CLR_ATOMIC(vcpuid, &vm->debug_cpus);
2382 vcpu_debugged(struct vm *vm, int vcpuid)
2385 return (CPU_ISSET(vcpuid, &vm->debug_cpus));
2389 vm_active_cpus(struct vm *vm)
2392 return (vm->active_cpus);
2396 vm_debug_cpus(struct vm *vm)
2399 return (vm->debug_cpus);
2403 vm_suspended_cpus(struct vm *vm)
2406 return (vm->suspended_cpus);
2410 vcpu_stats(struct vm *vm, int vcpuid)
2413 return (vm->vcpu[vcpuid].stats);
2417 vm_get_x2apic_state(struct vm *vm, int vcpuid, enum x2apic_state *state)
2419 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
2422 *state = vm->vcpu[vcpuid].x2apic_state;
2428 vm_set_x2apic_state(struct vm *vm, int vcpuid, enum x2apic_state state)
2430 if (vcpuid < 0 || vcpuid >= vm->maxcpus)
2433 if (state >= X2APIC_STATE_LAST)
2436 vm->vcpu[vcpuid].x2apic_state = state;
2438 vlapic_set_x2apic_state(vm, vcpuid, state);
2444 * This function is called to ensure that a vcpu "sees" a pending event
2445 * as soon as possible:
2446 * - If the vcpu thread is sleeping then it is woken up.
2447 * - If the vcpu is running on a different host_cpu then an IPI will be directed
2448 * to the host_cpu to cause the vcpu to trap into the hypervisor.
2451 vcpu_notify_event_locked(struct vcpu *vcpu, bool lapic_intr)
2455 hostcpu = vcpu->hostcpu;
2456 if (vcpu->state == VCPU_RUNNING) {
2457 KASSERT(hostcpu != NOCPU, ("vcpu running on invalid hostcpu"));
2458 if (hostcpu != curcpu) {
2460 vlapic_post_intr(vcpu->vlapic, hostcpu,
2463 ipi_cpu(hostcpu, vmm_ipinum);
2467 * If the 'vcpu' is running on 'curcpu' then it must
2468 * be sending a notification to itself (e.g. SELF_IPI).
2469 * The pending event will be picked up when the vcpu
2470 * transitions back to guest context.
2474 KASSERT(hostcpu == NOCPU, ("vcpu state %d not consistent "
2475 "with hostcpu %d", vcpu->state, hostcpu));
2476 if (vcpu->state == VCPU_SLEEPING)
2482 vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr)
2484 struct vcpu *vcpu = &vm->vcpu[vcpuid];
2487 vcpu_notify_event_locked(vcpu, lapic_intr);
2492 vm_get_vmspace(struct vm *vm)
2495 return (vm->vmspace);
2499 vm_apicid2vcpuid(struct vm *vm, int apicid)
2502 * XXX apic id is assumed to be numerically identical to vcpu id
2508 vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest,
2509 vm_rendezvous_func_t func, void *arg)
2514 * Enforce that this function is called without any locks
2516 WITNESS_WARN(WARN_PANIC, NULL, "vm_smp_rendezvous");
2517 KASSERT(vcpuid == -1 || (vcpuid >= 0 && vcpuid < vm->maxcpus),
2518 ("vm_smp_rendezvous: invalid vcpuid %d", vcpuid));
2521 mtx_lock(&vm->rendezvous_mtx);
2522 if (vm->rendezvous_func != NULL) {
2524 * If a rendezvous is already in progress then we need to
2525 * call the rendezvous handler in case this 'vcpuid' is one
2526 * of the targets of the rendezvous.
2528 RENDEZVOUS_CTR0(vm, vcpuid, "Rendezvous already in progress");
2529 mtx_unlock(&vm->rendezvous_mtx);
2530 vm_handle_rendezvous(vm, vcpuid);
2533 KASSERT(vm->rendezvous_func == NULL, ("vm_smp_rendezvous: previous "
2534 "rendezvous is still in progress"));
2536 RENDEZVOUS_CTR0(vm, vcpuid, "Initiating rendezvous");
2537 vm->rendezvous_req_cpus = dest;
2538 CPU_ZERO(&vm->rendezvous_done_cpus);
2539 vm->rendezvous_arg = arg;
2540 vm_set_rendezvous_func(vm, func);
2541 mtx_unlock(&vm->rendezvous_mtx);
2544 * Wake up any sleeping vcpus and trigger a VM-exit in any running
2545 * vcpus so they handle the rendezvous as soon as possible.
2547 for (i = 0; i < vm->maxcpus; i++) {
2548 if (CPU_ISSET(i, &dest))
2549 vcpu_notify_event(vm, i, false);
2552 vm_handle_rendezvous(vm, vcpuid);
2556 vm_atpic(struct vm *vm)
2558 return (vm->vatpic);
2562 vm_atpit(struct vm *vm)
2564 return (vm->vatpit);
2568 vm_pmtmr(struct vm *vm)
2571 return (vm->vpmtmr);
2575 vm_rtc(struct vm *vm)
2582 vm_segment_name(int seg)
2584 static enum vm_reg_name seg_names[] = {
2593 KASSERT(seg >= 0 && seg < nitems(seg_names),
2594 ("%s: invalid segment encoding %d", __func__, seg));
2595 return (seg_names[seg]);
2599 vm_copy_teardown(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo,
2604 for (idx = 0; idx < num_copyinfo; idx++) {
2605 if (copyinfo[idx].cookie != NULL)
2606 vm_gpa_release(copyinfo[idx].cookie);
2608 bzero(copyinfo, num_copyinfo * sizeof(struct vm_copyinfo));
2612 vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging,
2613 uint64_t gla, size_t len, int prot, struct vm_copyinfo *copyinfo,
2614 int num_copyinfo, int *fault)
2616 int error, idx, nused;
2617 size_t n, off, remaining;
2621 bzero(copyinfo, sizeof(struct vm_copyinfo) * num_copyinfo);
2625 while (remaining > 0) {
2626 KASSERT(nused < num_copyinfo, ("insufficient vm_copyinfo"));
2627 error = vm_gla2gpa(vm, vcpuid, paging, gla, prot, &gpa, fault);
2628 if (error || *fault)
2630 off = gpa & PAGE_MASK;
2631 n = min(remaining, PAGE_SIZE - off);
2632 copyinfo[nused].gpa = gpa;
2633 copyinfo[nused].len = n;
2639 for (idx = 0; idx < nused; idx++) {
2640 hva = vm_gpa_hold(vm, vcpuid, copyinfo[idx].gpa,
2641 copyinfo[idx].len, prot, &cookie);
2644 copyinfo[idx].hva = hva;
2645 copyinfo[idx].cookie = cookie;
2649 vm_copy_teardown(vm, vcpuid, copyinfo, num_copyinfo);
2658 vm_copyin(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo, void *kaddr,
2667 bcopy(copyinfo[idx].hva, dst, copyinfo[idx].len);
2668 len -= copyinfo[idx].len;
2669 dst += copyinfo[idx].len;
2675 vm_copyout(struct vm *vm, int vcpuid, const void *kaddr,
2676 struct vm_copyinfo *copyinfo, size_t len)
2684 bcopy(src, copyinfo[idx].hva, copyinfo[idx].len);
2685 len -= copyinfo[idx].len;
2686 src += copyinfo[idx].len;
2692 * Return the amount of in-use and wired memory for the VM. Since
2693 * these are global stats, only return the values with for vCPU 0
2695 VMM_STAT_DECLARE(VMM_MEM_RESIDENT);
2696 VMM_STAT_DECLARE(VMM_MEM_WIRED);
2699 vm_get_rescnt(struct vm *vm, int vcpu, struct vmm_stat_type *stat)
2703 vmm_stat_set(vm, vcpu, VMM_MEM_RESIDENT,
2704 PAGE_SIZE * vmspace_resident_count(vm->vmspace));
2709 vm_get_wiredcnt(struct vm *vm, int vcpu, struct vmm_stat_type *stat)
2713 vmm_stat_set(vm, vcpu, VMM_MEM_WIRED,
2714 PAGE_SIZE * pmap_wired_count(vmspace_pmap(vm->vmspace)));
2718 VMM_STAT_FUNC(VMM_MEM_RESIDENT, "Resident memory", vm_get_rescnt);
2719 VMM_STAT_FUNC(VMM_MEM_WIRED, "Wired memory", vm_get_wiredcnt);