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 "opt_bhyve_snapshot.h"
36 #include <sys/param.h>
37 #include <sys/kernel.h>
39 #include <sys/queue.h>
41 #include <sys/mutex.h>
42 #include <sys/malloc.h>
44 #include <sys/sysctl.h>
45 #include <sys/libkern.h>
46 #include <sys/ioccom.h>
53 #include <vm/vm_map.h>
54 #include <vm/vm_object.h>
56 #include <machine/vmparam.h>
57 #include <machine/vmm.h>
58 #include <machine/vmm_dev.h>
59 #include <machine/vmm_instruction_emul.h>
60 #include <machine/vmm_snapshot.h>
62 #include "vmm_lapic.h"
66 #include "io/vatpic.h"
67 #include "io/vioapic.h"
75 struct vmmdev_softc *sc;
76 SLIST_ENTRY(devmem_softc) link;
80 struct vm *vm; /* vm instance cookie */
82 SLIST_ENTRY(vmmdev_softc) link;
83 SLIST_HEAD(, devmem_softc) devmem;
86 #define VSC_LINKED 0x01
88 static SLIST_HEAD(, vmmdev_softc) head;
90 static unsigned pr_allow_flag;
91 static struct mtx vmmdev_mtx;
93 static MALLOC_DEFINE(M_VMMDEV, "vmmdev", "vmmdev");
97 static int vmm_priv_check(struct ucred *ucred);
98 static int devmem_create_cdev(const char *vmname, int id, char *devmem);
99 static void devmem_destroy(void *arg);
102 vmm_priv_check(struct ucred *ucred)
106 !(ucred->cr_prison->pr_allow & pr_allow_flag))
113 vcpu_lock_one(struct vmmdev_softc *sc, int vcpu)
117 if (vcpu < 0 || vcpu >= vm_get_maxcpus(sc->vm))
120 error = vcpu_set_state(sc->vm, vcpu, VCPU_FROZEN, true);
125 vcpu_unlock_one(struct vmmdev_softc *sc, int vcpu)
127 enum vcpu_state state;
129 state = vcpu_get_state(sc->vm, vcpu, NULL);
130 if (state != VCPU_FROZEN) {
131 panic("vcpu %s(%d) has invalid state %d", vm_name(sc->vm),
135 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false);
139 vcpu_lock_all(struct vmmdev_softc *sc)
144 maxcpus = vm_get_maxcpus(sc->vm);
145 for (vcpu = 0; vcpu < maxcpus; vcpu++) {
146 error = vcpu_lock_one(sc, vcpu);
153 vcpu_unlock_one(sc, vcpu);
160 vcpu_unlock_all(struct vmmdev_softc *sc)
165 maxcpus = vm_get_maxcpus(sc->vm);
166 for (vcpu = 0; vcpu < maxcpus; vcpu++)
167 vcpu_unlock_one(sc, vcpu);
170 static struct vmmdev_softc *
171 vmmdev_lookup(const char *name)
173 struct vmmdev_softc *sc;
175 #ifdef notyet /* XXX kernel is not compiled with invariants */
176 mtx_assert(&vmmdev_mtx, MA_OWNED);
179 SLIST_FOREACH(sc, &head, link) {
180 if (strcmp(name, vm_name(sc->vm)) == 0)
187 static struct vmmdev_softc *
188 vmmdev_lookup2(struct cdev *cdev)
191 return (cdev->si_drv1);
195 vmmdev_rw(struct cdev *cdev, struct uio *uio, int flags)
197 int error, off, c, prot;
198 vm_paddr_t gpa, maxaddr;
200 struct vmmdev_softc *sc;
203 error = vmm_priv_check(curthread->td_ucred);
207 sc = vmmdev_lookup2(cdev);
212 * Get a read lock on the guest memory map by freezing any vcpu.
214 lastcpu = vm_get_maxcpus(sc->vm) - 1;
215 error = vcpu_lock_one(sc, lastcpu);
219 prot = (uio->uio_rw == UIO_WRITE ? VM_PROT_WRITE : VM_PROT_READ);
220 maxaddr = vmm_sysmem_maxaddr(sc->vm);
221 while (uio->uio_resid > 0 && error == 0) {
222 gpa = uio->uio_offset;
223 off = gpa & PAGE_MASK;
224 c = min(uio->uio_resid, PAGE_SIZE - off);
227 * The VM has a hole in its physical memory map. If we want to
228 * use 'dd' to inspect memory beyond the hole we need to
229 * provide bogus data for memory that lies in the hole.
231 * Since this device does not support lseek(2), dd(1) will
232 * read(2) blocks of data to simulate the lseek(2).
234 hpa = vm_gpa_hold(sc->vm, lastcpu, gpa, c,
237 if (uio->uio_rw == UIO_READ && gpa < maxaddr)
238 error = uiomove(__DECONST(void *, zero_region),
243 error = uiomove(hpa, c, uio);
244 vm_gpa_release(cookie);
247 vcpu_unlock_one(sc, lastcpu);
251 CTASSERT(sizeof(((struct vm_memseg *)0)->name) >= VM_MAX_SUFFIXLEN + 1);
254 get_memseg(struct vmmdev_softc *sc, struct vm_memseg *mseg)
256 struct devmem_softc *dsc;
260 error = vm_get_memseg(sc->vm, mseg->segid, &mseg->len, &sysmem, NULL);
261 if (error || mseg->len == 0)
265 SLIST_FOREACH(dsc, &sc->devmem, link) {
266 if (dsc->segid == mseg->segid)
269 KASSERT(dsc != NULL, ("%s: devmem segment %d not found",
270 __func__, mseg->segid));
271 error = copystr(dsc->name, mseg->name, sizeof(mseg->name),
274 bzero(mseg->name, sizeof(mseg->name));
281 alloc_memseg(struct vmmdev_softc *sc, struct vm_memseg *mseg)
292 * The allocation is lengthened by 1 to hold a terminating NUL. It'll
293 * by stripped off when devfs processes the full string.
295 if (VM_MEMSEG_NAME(mseg)) {
297 name = malloc(sizeof(mseg->name), M_VMMDEV, M_WAITOK);
298 error = copystr(mseg->name, name, sizeof(mseg->name), NULL);
303 error = vm_alloc_memseg(sc->vm, mseg->segid, mseg->len, sysmem);
307 if (VM_MEMSEG_NAME(mseg)) {
308 error = devmem_create_cdev(vm_name(sc->vm), mseg->segid, name);
310 vm_free_memseg(sc->vm, mseg->segid);
312 name = NULL; /* freed when 'cdev' is destroyed */
315 free(name, M_VMMDEV);
320 vm_get_register_set(struct vm *vm, int vcpu, unsigned int count, int *regnum,
326 for (i = 0; i < count; i++) {
327 error = vm_get_register(vm, vcpu, regnum[i], ®val[i]);
335 vm_set_register_set(struct vm *vm, int vcpu, unsigned int count, int *regnum,
341 for (i = 0; i < count; i++) {
342 error = vm_set_register(vm, vcpu, regnum[i], regval[i]);
350 vmmdev_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
353 int error, vcpu, state_changed, size;
355 struct vmmdev_softc *sc;
356 struct vm_register *vmreg;
357 struct vm_seg_desc *vmsegdesc;
358 struct vm_register_set *vmregset;
359 struct vm_run *vmrun;
360 struct vm_exception *vmexc;
361 struct vm_lapic_irq *vmirq;
362 struct vm_lapic_msi *vmmsi;
363 struct vm_ioapic_irq *ioapic_irq;
364 struct vm_isa_irq *isa_irq;
365 struct vm_isa_irq_trigger *isa_irq_trigger;
366 struct vm_capability *vmcap;
367 struct vm_pptdev *pptdev;
368 struct vm_pptdev_mmio *pptmmio;
369 struct vm_pptdev_msi *pptmsi;
370 struct vm_pptdev_msix *pptmsix;
371 struct vm_nmi *vmnmi;
372 struct vm_stats *vmstats;
373 struct vm_stat_desc *statdesc;
374 struct vm_x2apic *x2apic;
375 struct vm_gpa_pte *gpapte;
376 struct vm_suspend *vmsuspend;
377 struct vm_gla2gpa *gg;
378 struct vm_activate_cpu *vac;
379 struct vm_cpuset *vm_cpuset;
380 struct vm_intinfo *vmii;
381 struct vm_rtc_time *rtctime;
382 struct vm_rtc_data *rtcdata;
383 struct vm_memmap *mm;
384 struct vm_cpu_topology *topology;
387 #ifdef BHYVE_SNAPSHOT
388 struct vm_snapshot_meta *snapshot_meta;
391 error = vmm_priv_check(curthread->td_ucred);
395 sc = vmmdev_lookup2(cdev);
403 * Some VMM ioctls can operate only on vcpus that are not running.
407 case VM_GET_REGISTER:
408 case VM_SET_REGISTER:
409 case VM_GET_SEGMENT_DESCRIPTOR:
410 case VM_SET_SEGMENT_DESCRIPTOR:
411 case VM_GET_REGISTER_SET:
412 case VM_SET_REGISTER_SET:
413 case VM_INJECT_EXCEPTION:
414 case VM_GET_CAPABILITY:
415 case VM_SET_CAPABILITY:
418 case VM_SET_X2APIC_STATE:
420 case VM_GLA2GPA_NOFAULT:
421 case VM_ACTIVATE_CPU:
424 case VM_RESTART_INSTRUCTION:
426 * XXX fragile, handle with care
427 * Assumes that the first field of the ioctl data is the vcpu.
430 error = vcpu_lock_one(sc, vcpu);
436 case VM_MAP_PPTDEV_MMIO:
438 case VM_UNBIND_PPTDEV:
439 case VM_ALLOC_MEMSEG:
443 * ioctls that operate on the entire virtual machine must
444 * prevent all vcpus from running.
446 error = vcpu_lock_all(sc);
453 case VM_MMAP_GETNEXT:
455 * Lock a vcpu to make sure that the memory map cannot be
456 * modified while it is being inspected.
458 vcpu = vm_get_maxcpus(sc->vm) - 1;
459 error = vcpu_lock_one(sc, vcpu);
471 vmrun = (struct vm_run *)data;
472 error = vm_run(sc->vm, vmrun);
475 vmsuspend = (struct vm_suspend *)data;
476 error = vm_suspend(sc->vm, vmsuspend->how);
479 error = vm_reinit(sc->vm);
482 statdesc = (struct vm_stat_desc *)data;
483 error = vmm_stat_desc_copy(statdesc->index,
484 statdesc->desc, sizeof(statdesc->desc));
488 CTASSERT(MAX_VM_STATS >= MAX_VMM_STAT_ELEMS);
489 vmstats = (struct vm_stats *)data;
490 getmicrotime(&vmstats->tv);
491 error = vmm_stat_copy(sc->vm, vmstats->cpuid,
492 &vmstats->num_entries, vmstats->statbuf);
496 pptmsi = (struct vm_pptdev_msi *)data;
497 error = ppt_setup_msi(sc->vm, pptmsi->vcpu,
498 pptmsi->bus, pptmsi->slot, pptmsi->func,
499 pptmsi->addr, pptmsi->msg,
503 pptmsix = (struct vm_pptdev_msix *)data;
504 error = ppt_setup_msix(sc->vm, pptmsix->vcpu,
505 pptmsix->bus, pptmsix->slot,
506 pptmsix->func, pptmsix->idx,
507 pptmsix->addr, pptmsix->msg,
508 pptmsix->vector_control);
510 case VM_MAP_PPTDEV_MMIO:
511 pptmmio = (struct vm_pptdev_mmio *)data;
512 error = ppt_map_mmio(sc->vm, pptmmio->bus, pptmmio->slot,
513 pptmmio->func, pptmmio->gpa, pptmmio->len,
517 pptdev = (struct vm_pptdev *)data;
518 error = vm_assign_pptdev(sc->vm, pptdev->bus, pptdev->slot,
521 case VM_UNBIND_PPTDEV:
522 pptdev = (struct vm_pptdev *)data;
523 error = vm_unassign_pptdev(sc->vm, pptdev->bus, pptdev->slot,
526 case VM_INJECT_EXCEPTION:
527 vmexc = (struct vm_exception *)data;
528 error = vm_inject_exception(sc->vm, vmexc->cpuid,
529 vmexc->vector, vmexc->error_code_valid, vmexc->error_code,
530 vmexc->restart_instruction);
533 vmnmi = (struct vm_nmi *)data;
534 error = vm_inject_nmi(sc->vm, vmnmi->cpuid);
537 vmirq = (struct vm_lapic_irq *)data;
538 error = lapic_intr_edge(sc->vm, vmirq->cpuid, vmirq->vector);
540 case VM_LAPIC_LOCAL_IRQ:
541 vmirq = (struct vm_lapic_irq *)data;
542 error = lapic_set_local_intr(sc->vm, vmirq->cpuid,
546 vmmsi = (struct vm_lapic_msi *)data;
547 error = lapic_intr_msi(sc->vm, vmmsi->addr, vmmsi->msg);
549 case VM_IOAPIC_ASSERT_IRQ:
550 ioapic_irq = (struct vm_ioapic_irq *)data;
551 error = vioapic_assert_irq(sc->vm, ioapic_irq->irq);
553 case VM_IOAPIC_DEASSERT_IRQ:
554 ioapic_irq = (struct vm_ioapic_irq *)data;
555 error = vioapic_deassert_irq(sc->vm, ioapic_irq->irq);
557 case VM_IOAPIC_PULSE_IRQ:
558 ioapic_irq = (struct vm_ioapic_irq *)data;
559 error = vioapic_pulse_irq(sc->vm, ioapic_irq->irq);
561 case VM_IOAPIC_PINCOUNT:
562 *(int *)data = vioapic_pincount(sc->vm);
564 case VM_ISA_ASSERT_IRQ:
565 isa_irq = (struct vm_isa_irq *)data;
566 error = vatpic_assert_irq(sc->vm, isa_irq->atpic_irq);
567 if (error == 0 && isa_irq->ioapic_irq != -1)
568 error = vioapic_assert_irq(sc->vm,
569 isa_irq->ioapic_irq);
571 case VM_ISA_DEASSERT_IRQ:
572 isa_irq = (struct vm_isa_irq *)data;
573 error = vatpic_deassert_irq(sc->vm, isa_irq->atpic_irq);
574 if (error == 0 && isa_irq->ioapic_irq != -1)
575 error = vioapic_deassert_irq(sc->vm,
576 isa_irq->ioapic_irq);
578 case VM_ISA_PULSE_IRQ:
579 isa_irq = (struct vm_isa_irq *)data;
580 error = vatpic_pulse_irq(sc->vm, isa_irq->atpic_irq);
581 if (error == 0 && isa_irq->ioapic_irq != -1)
582 error = vioapic_pulse_irq(sc->vm, isa_irq->ioapic_irq);
584 case VM_ISA_SET_IRQ_TRIGGER:
585 isa_irq_trigger = (struct vm_isa_irq_trigger *)data;
586 error = vatpic_set_irq_trigger(sc->vm,
587 isa_irq_trigger->atpic_irq, isa_irq_trigger->trigger);
589 case VM_MMAP_GETNEXT:
590 mm = (struct vm_memmap *)data;
591 error = vm_mmap_getnext(sc->vm, &mm->gpa, &mm->segid,
592 &mm->segoff, &mm->len, &mm->prot, &mm->flags);
595 mm = (struct vm_memmap *)data;
596 error = vm_mmap_memseg(sc->vm, mm->gpa, mm->segid, mm->segoff,
597 mm->len, mm->prot, mm->flags);
599 case VM_ALLOC_MEMSEG:
600 error = alloc_memseg(sc, (struct vm_memseg *)data);
603 error = get_memseg(sc, (struct vm_memseg *)data);
605 case VM_GET_REGISTER:
606 vmreg = (struct vm_register *)data;
607 error = vm_get_register(sc->vm, vmreg->cpuid, vmreg->regnum,
610 case VM_SET_REGISTER:
611 vmreg = (struct vm_register *)data;
612 error = vm_set_register(sc->vm, vmreg->cpuid, vmreg->regnum,
615 case VM_SET_SEGMENT_DESCRIPTOR:
616 vmsegdesc = (struct vm_seg_desc *)data;
617 error = vm_set_seg_desc(sc->vm, vmsegdesc->cpuid,
621 case VM_GET_SEGMENT_DESCRIPTOR:
622 vmsegdesc = (struct vm_seg_desc *)data;
623 error = vm_get_seg_desc(sc->vm, vmsegdesc->cpuid,
627 case VM_GET_REGISTER_SET:
628 vmregset = (struct vm_register_set *)data;
629 if (vmregset->count > VM_REG_LAST) {
633 regvals = malloc(sizeof(regvals[0]) * vmregset->count, M_VMMDEV,
635 regnums = malloc(sizeof(regnums[0]) * vmregset->count, M_VMMDEV,
637 error = copyin(vmregset->regnums, regnums, sizeof(regnums[0]) *
640 error = vm_get_register_set(sc->vm, vmregset->cpuid,
641 vmregset->count, regnums, regvals);
643 error = copyout(regvals, vmregset->regvals,
644 sizeof(regvals[0]) * vmregset->count);
645 free(regvals, M_VMMDEV);
646 free(regnums, M_VMMDEV);
648 case VM_SET_REGISTER_SET:
649 vmregset = (struct vm_register_set *)data;
650 if (vmregset->count > VM_REG_LAST) {
654 regvals = malloc(sizeof(regvals[0]) * vmregset->count, M_VMMDEV,
656 regnums = malloc(sizeof(regnums[0]) * vmregset->count, M_VMMDEV,
658 error = copyin(vmregset->regnums, regnums, sizeof(regnums[0]) *
661 error = copyin(vmregset->regvals, regvals,
662 sizeof(regvals[0]) * vmregset->count);
664 error = vm_set_register_set(sc->vm, vmregset->cpuid,
665 vmregset->count, regnums, regvals);
666 free(regvals, M_VMMDEV);
667 free(regnums, M_VMMDEV);
669 case VM_GET_CAPABILITY:
670 vmcap = (struct vm_capability *)data;
671 error = vm_get_capability(sc->vm, vmcap->cpuid,
675 case VM_SET_CAPABILITY:
676 vmcap = (struct vm_capability *)data;
677 error = vm_set_capability(sc->vm, vmcap->cpuid,
681 case VM_SET_X2APIC_STATE:
682 x2apic = (struct vm_x2apic *)data;
683 error = vm_set_x2apic_state(sc->vm,
684 x2apic->cpuid, x2apic->state);
686 case VM_GET_X2APIC_STATE:
687 x2apic = (struct vm_x2apic *)data;
688 error = vm_get_x2apic_state(sc->vm,
689 x2apic->cpuid, &x2apic->state);
691 case VM_GET_GPA_PMAP:
692 gpapte = (struct vm_gpa_pte *)data;
693 pmap_get_mapping(vmspace_pmap(vm_get_vmspace(sc->vm)),
694 gpapte->gpa, gpapte->pte, &gpapte->ptenum);
697 case VM_GET_HPET_CAPABILITIES:
698 error = vhpet_getcap((struct vm_hpet_cap *)data);
701 CTASSERT(PROT_READ == VM_PROT_READ);
702 CTASSERT(PROT_WRITE == VM_PROT_WRITE);
703 CTASSERT(PROT_EXEC == VM_PROT_EXECUTE);
704 gg = (struct vm_gla2gpa *)data;
705 error = vm_gla2gpa(sc->vm, gg->vcpuid, &gg->paging, gg->gla,
706 gg->prot, &gg->gpa, &gg->fault);
707 KASSERT(error == 0 || error == EFAULT,
708 ("%s: vm_gla2gpa unknown error %d", __func__, error));
711 case VM_GLA2GPA_NOFAULT:
712 gg = (struct vm_gla2gpa *)data;
713 error = vm_gla2gpa_nofault(sc->vm, gg->vcpuid, &gg->paging,
714 gg->gla, gg->prot, &gg->gpa, &gg->fault);
715 KASSERT(error == 0 || error == EFAULT,
716 ("%s: vm_gla2gpa unknown error %d", __func__, error));
718 case VM_ACTIVATE_CPU:
719 vac = (struct vm_activate_cpu *)data;
720 error = vm_activate_cpu(sc->vm, vac->vcpuid);
724 vm_cpuset = (struct vm_cpuset *)data;
725 size = vm_cpuset->cpusetsize;
726 if (size < sizeof(cpuset_t) || size > CPU_MAXSIZE / NBBY) {
730 cpuset = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
731 if (vm_cpuset->which == VM_ACTIVE_CPUS)
732 *cpuset = vm_active_cpus(sc->vm);
733 else if (vm_cpuset->which == VM_SUSPENDED_CPUS)
734 *cpuset = vm_suspended_cpus(sc->vm);
735 else if (vm_cpuset->which == VM_DEBUG_CPUS)
736 *cpuset = vm_debug_cpus(sc->vm);
740 error = copyout(cpuset, vm_cpuset->cpus, size);
741 free(cpuset, M_TEMP);
744 vac = (struct vm_activate_cpu *)data;
745 error = vm_suspend_cpu(sc->vm, vac->vcpuid);
748 vac = (struct vm_activate_cpu *)data;
749 error = vm_resume_cpu(sc->vm, vac->vcpuid);
752 vmii = (struct vm_intinfo *)data;
753 error = vm_exit_intinfo(sc->vm, vmii->vcpuid, vmii->info1);
756 vmii = (struct vm_intinfo *)data;
757 error = vm_get_intinfo(sc->vm, vmii->vcpuid, &vmii->info1,
761 rtcdata = (struct vm_rtc_data *)data;
762 error = vrtc_nvram_write(sc->vm, rtcdata->offset,
766 rtcdata = (struct vm_rtc_data *)data;
767 error = vrtc_nvram_read(sc->vm, rtcdata->offset,
771 rtctime = (struct vm_rtc_time *)data;
772 error = vrtc_set_time(sc->vm, rtctime->secs);
776 rtctime = (struct vm_rtc_time *)data;
777 rtctime->secs = vrtc_get_time(sc->vm);
779 case VM_RESTART_INSTRUCTION:
780 error = vm_restart_instruction(sc->vm, vcpu);
782 case VM_SET_TOPOLOGY:
783 topology = (struct vm_cpu_topology *)data;
784 error = vm_set_topology(sc->vm, topology->sockets,
785 topology->cores, topology->threads, topology->maxcpus);
787 case VM_GET_TOPOLOGY:
788 topology = (struct vm_cpu_topology *)data;
789 vm_get_topology(sc->vm, &topology->sockets, &topology->cores,
790 &topology->threads, &topology->maxcpus);
793 #ifdef BHYVE_SNAPSHOT
794 case VM_SNAPSHOT_REQ:
795 snapshot_meta = (struct vm_snapshot_meta *)data;
796 error = vm_snapshot_req(sc->vm, snapshot_meta);
798 case VM_RESTORE_TIME:
799 error = vm_restore_time(sc->vm);
807 if (state_changed == 1)
808 vcpu_unlock_one(sc, vcpu);
809 else if (state_changed == 2)
814 * Make sure that no handler returns a kernel-internal
815 * error value to userspace.
817 KASSERT(error == ERESTART || error >= 0,
818 ("vmmdev_ioctl: invalid error return %d", error));
823 vmmdev_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t mapsize,
824 struct vm_object **objp, int nprot)
826 struct vmmdev_softc *sc;
829 vm_ooffset_t segoff, first, last;
830 int error, found, segid;
834 error = vmm_priv_check(curthread->td_ucred);
839 last = first + mapsize;
840 if ((nprot & PROT_EXEC) || first < 0 || first >= last)
843 sc = vmmdev_lookup2(cdev);
845 /* virtual machine is in the process of being created */
850 * Get a read lock on the guest memory map by freezing any vcpu.
852 lastcpu = vm_get_maxcpus(sc->vm) - 1;
853 error = vcpu_lock_one(sc, lastcpu);
860 error = vm_mmap_getnext(sc->vm, &gpa, &segid, &segoff, &len,
865 if (first >= gpa && last <= gpa + len)
872 error = vm_get_memseg(sc->vm, segid, &len, &sysmem, objp);
873 KASSERT(error == 0 && *objp != NULL,
874 ("%s: invalid memory segment %d", __func__, segid));
876 vm_object_reference(*objp);
877 *offset = segoff + (first - gpa);
882 vcpu_unlock_one(sc, lastcpu);
887 vmmdev_destroy(void *arg)
889 struct vmmdev_softc *sc = arg;
890 struct devmem_softc *dsc;
893 error = vcpu_lock_all(sc);
894 KASSERT(error == 0, ("%s: error %d freezing vcpus", __func__, error));
896 while ((dsc = SLIST_FIRST(&sc->devmem)) != NULL) {
897 KASSERT(dsc->cdev == NULL, ("%s: devmem not free", __func__));
898 SLIST_REMOVE_HEAD(&sc->devmem, link);
899 free(dsc->name, M_VMMDEV);
903 if (sc->cdev != NULL)
904 destroy_dev(sc->cdev);
909 if ((sc->flags & VSC_LINKED) != 0) {
910 mtx_lock(&vmmdev_mtx);
911 SLIST_REMOVE(&head, sc, vmmdev_softc, link);
912 mtx_unlock(&vmmdev_mtx);
919 sysctl_vmm_destroy(SYSCTL_HANDLER_ARGS)
921 struct devmem_softc *dsc;
922 struct vmmdev_softc *sc;
927 error = vmm_priv_check(req->td->td_ucred);
931 buflen = VM_MAX_NAMELEN + 1;
932 buf = malloc(buflen, M_VMMDEV, M_WAITOK | M_ZERO);
933 strlcpy(buf, "beavis", buflen);
934 error = sysctl_handle_string(oidp, buf, buflen, req);
935 if (error != 0 || req->newptr == NULL)
938 mtx_lock(&vmmdev_mtx);
939 sc = vmmdev_lookup(buf);
940 if (sc == NULL || sc->cdev == NULL) {
941 mtx_unlock(&vmmdev_mtx);
947 * The 'cdev' will be destroyed asynchronously when 'si_threadcount'
948 * goes down to 0 so we should not do it again in the callback.
950 * Setting 'sc->cdev' to NULL is also used to indicate that the VM
951 * is scheduled for destruction.
955 mtx_unlock(&vmmdev_mtx);
958 * Schedule all cdevs to be destroyed:
960 * - any new operations on the 'cdev' will return an error (ENXIO).
962 * - when the 'si_threadcount' dwindles down to zero the 'cdev' will
963 * be destroyed and the callback will be invoked in a taskqueue
966 * - the 'devmem' cdevs are destroyed before the virtual machine 'cdev'
968 SLIST_FOREACH(dsc, &sc->devmem, link) {
969 KASSERT(dsc->cdev != NULL, ("devmem cdev already destroyed"));
970 destroy_dev_sched_cb(dsc->cdev, devmem_destroy, dsc);
972 destroy_dev_sched_cb(cdev, vmmdev_destroy, sc);
979 SYSCTL_PROC(_hw_vmm, OID_AUTO, destroy,
980 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
981 NULL, 0, sysctl_vmm_destroy, "A",
984 static struct cdevsw vmmdevsw = {
986 .d_version = D_VERSION,
987 .d_ioctl = vmmdev_ioctl,
988 .d_mmap_single = vmmdev_mmap_single,
990 .d_write = vmmdev_rw,
994 sysctl_vmm_create(SYSCTL_HANDLER_ARGS)
998 struct vmmdev_softc *sc, *sc2;
1002 error = vmm_priv_check(req->td->td_ucred);
1006 buflen = VM_MAX_NAMELEN + 1;
1007 buf = malloc(buflen, M_VMMDEV, M_WAITOK | M_ZERO);
1008 strlcpy(buf, "beavis", buflen);
1009 error = sysctl_handle_string(oidp, buf, buflen, req);
1010 if (error != 0 || req->newptr == NULL)
1013 mtx_lock(&vmmdev_mtx);
1014 sc = vmmdev_lookup(buf);
1015 mtx_unlock(&vmmdev_mtx);
1021 error = vm_create(buf, &vm);
1025 sc = malloc(sizeof(struct vmmdev_softc), M_VMMDEV, M_WAITOK | M_ZERO);
1027 SLIST_INIT(&sc->devmem);
1030 * Lookup the name again just in case somebody sneaked in when we
1033 mtx_lock(&vmmdev_mtx);
1034 sc2 = vmmdev_lookup(buf);
1036 SLIST_INSERT_HEAD(&head, sc, link);
1037 sc->flags |= VSC_LINKED;
1039 mtx_unlock(&vmmdev_mtx);
1047 error = make_dev_p(MAKEDEV_CHECKNAME, &cdev, &vmmdevsw, NULL,
1048 UID_ROOT, GID_WHEEL, 0600, "vmm/%s", buf);
1054 mtx_lock(&vmmdev_mtx);
1056 sc->cdev->si_drv1 = sc;
1057 mtx_unlock(&vmmdev_mtx);
1060 free(buf, M_VMMDEV);
1063 SYSCTL_PROC(_hw_vmm, OID_AUTO, create,
1064 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
1065 NULL, 0, sysctl_vmm_create, "A",
1071 mtx_init(&vmmdev_mtx, "vmm device mutex", NULL, MTX_DEF);
1072 pr_allow_flag = prison_add_allow(NULL, "vmm", NULL,
1073 "Allow use of vmm in a jail.");
1077 vmmdev_cleanup(void)
1081 if (SLIST_EMPTY(&head))
1090 devmem_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t len,
1091 struct vm_object **objp, int nprot)
1093 struct devmem_softc *dsc;
1094 vm_ooffset_t first, last;
1100 dsc = cdev->si_drv1;
1102 /* 'cdev' has been created but is not ready for use */
1107 last = *offset + len;
1108 if ((nprot & PROT_EXEC) || first < 0 || first >= last)
1111 lastcpu = vm_get_maxcpus(dsc->sc->vm) - 1;
1112 error = vcpu_lock_one(dsc->sc, lastcpu);
1116 error = vm_get_memseg(dsc->sc->vm, dsc->segid, &seglen, &sysmem, objp);
1117 KASSERT(error == 0 && !sysmem && *objp != NULL,
1118 ("%s: invalid devmem segment %d", __func__, dsc->segid));
1120 vcpu_unlock_one(dsc->sc, lastcpu);
1122 if (seglen >= last) {
1123 vm_object_reference(*objp);
1130 static struct cdevsw devmemsw = {
1132 .d_version = D_VERSION,
1133 .d_mmap_single = devmem_mmap_single,
1137 devmem_create_cdev(const char *vmname, int segid, char *devname)
1139 struct devmem_softc *dsc;
1140 struct vmmdev_softc *sc;
1144 error = make_dev_p(MAKEDEV_CHECKNAME, &cdev, &devmemsw, NULL,
1145 UID_ROOT, GID_WHEEL, 0600, "vmm.io/%s.%s", vmname, devname);
1149 dsc = malloc(sizeof(struct devmem_softc), M_VMMDEV, M_WAITOK | M_ZERO);
1151 mtx_lock(&vmmdev_mtx);
1152 sc = vmmdev_lookup(vmname);
1153 KASSERT(sc != NULL, ("%s: vm %s softc not found", __func__, vmname));
1154 if (sc->cdev == NULL) {
1155 /* virtual machine is being created or destroyed */
1156 mtx_unlock(&vmmdev_mtx);
1157 free(dsc, M_VMMDEV);
1158 destroy_dev_sched_cb(cdev, NULL, 0);
1163 dsc->name = devname;
1166 SLIST_INSERT_HEAD(&sc->devmem, dsc, link);
1167 mtx_unlock(&vmmdev_mtx);
1169 /* The 'cdev' is ready for use after 'si_drv1' is initialized */
1170 cdev->si_drv1 = dsc;
1175 devmem_destroy(void *arg)
1177 struct devmem_softc *dsc = arg;
1179 KASSERT(dsc->cdev, ("%s: devmem cdev already destroyed", __func__));