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/kernel.h>
36 #include <sys/queue.h>
38 #include <sys/mutex.h>
39 #include <sys/malloc.h>
41 #include <sys/sysctl.h>
42 #include <sys/libkern.h>
43 #include <sys/ioccom.h>
49 #include <vm/vm_map.h>
50 #include <vm/vm_object.h>
52 #include <machine/vmparam.h>
53 #include <machine/vmm.h>
54 #include <machine/vmm_instruction_emul.h>
55 #include <machine/vmm_dev.h>
57 #include "vmm_lapic.h"
61 #include "io/vatpic.h"
62 #include "io/vioapic.h"
70 struct vmmdev_softc *sc;
71 SLIST_ENTRY(devmem_softc) link;
75 struct vm *vm; /* vm instance cookie */
77 SLIST_ENTRY(vmmdev_softc) link;
78 SLIST_HEAD(, devmem_softc) devmem;
81 #define VSC_LINKED 0x01
83 static SLIST_HEAD(, vmmdev_softc) head;
85 static struct mtx vmmdev_mtx;
87 static MALLOC_DEFINE(M_VMMDEV, "vmmdev", "vmmdev");
91 static int devmem_create_cdev(const char *vmname, int id, char *devmem);
92 static void devmem_destroy(void *arg);
95 vcpu_lock_one(struct vmmdev_softc *sc, int vcpu)
99 if (vcpu < 0 || vcpu >= VM_MAXCPU)
102 error = vcpu_set_state(sc->vm, vcpu, VCPU_FROZEN, true);
107 vcpu_unlock_one(struct vmmdev_softc *sc, int vcpu)
109 enum vcpu_state state;
111 state = vcpu_get_state(sc->vm, vcpu, NULL);
112 if (state != VCPU_FROZEN) {
113 panic("vcpu %s(%d) has invalid state %d", vm_name(sc->vm),
117 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false);
121 vcpu_lock_all(struct vmmdev_softc *sc)
125 for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++) {
126 error = vcpu_lock_one(sc, vcpu);
133 vcpu_unlock_one(sc, vcpu);
140 vcpu_unlock_all(struct vmmdev_softc *sc)
144 for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++)
145 vcpu_unlock_one(sc, vcpu);
148 static struct vmmdev_softc *
149 vmmdev_lookup(const char *name)
151 struct vmmdev_softc *sc;
153 #ifdef notyet /* XXX kernel is not compiled with invariants */
154 mtx_assert(&vmmdev_mtx, MA_OWNED);
157 SLIST_FOREACH(sc, &head, link) {
158 if (strcmp(name, vm_name(sc->vm)) == 0)
165 static struct vmmdev_softc *
166 vmmdev_lookup2(struct cdev *cdev)
169 return (cdev->si_drv1);
173 vmmdev_rw(struct cdev *cdev, struct uio *uio, int flags)
175 int error, off, c, prot;
178 struct vmmdev_softc *sc;
180 sc = vmmdev_lookup2(cdev);
185 * Get a read lock on the guest memory map by freezing any vcpu.
187 error = vcpu_lock_one(sc, VM_MAXCPU - 1);
191 prot = (uio->uio_rw == UIO_WRITE ? VM_PROT_WRITE : VM_PROT_READ);
192 while (uio->uio_resid > 0 && error == 0) {
193 gpa = uio->uio_offset;
194 off = gpa & PAGE_MASK;
195 c = min(uio->uio_resid, PAGE_SIZE - off);
198 * The VM has a hole in its physical memory map. If we want to
199 * use 'dd' to inspect memory beyond the hole we need to
200 * provide bogus data for memory that lies in the hole.
202 * Since this device does not support lseek(2), dd(1) will
203 * read(2) blocks of data to simulate the lseek(2).
205 hpa = vm_gpa_hold(sc->vm, VM_MAXCPU - 1, gpa, c, prot, &cookie);
207 if (uio->uio_rw == UIO_READ)
208 error = uiomove(__DECONST(void *, zero_region),
213 error = uiomove(hpa, c, uio);
214 vm_gpa_release(cookie);
217 vcpu_unlock_one(sc, VM_MAXCPU - 1);
221 CTASSERT(sizeof(((struct vm_memseg *)0)->name) >= SPECNAMELEN + 1);
224 get_memseg(struct vmmdev_softc *sc, struct vm_memseg *mseg)
226 struct devmem_softc *dsc;
230 error = vm_get_memseg(sc->vm, mseg->segid, &mseg->len, &sysmem, NULL);
231 if (error || mseg->len == 0)
235 SLIST_FOREACH(dsc, &sc->devmem, link) {
236 if (dsc->segid == mseg->segid)
239 KASSERT(dsc != NULL, ("%s: devmem segment %d not found",
240 __func__, mseg->segid));
241 error = copystr(dsc->name, mseg->name, SPECNAMELEN + 1, NULL);
243 bzero(mseg->name, sizeof(mseg->name));
250 alloc_memseg(struct vmmdev_softc *sc, struct vm_memseg *mseg)
260 if (VM_MEMSEG_NAME(mseg)) {
262 name = malloc(SPECNAMELEN + 1, M_VMMDEV, M_WAITOK);
263 error = copystr(mseg->name, name, SPECNAMELEN + 1, 0);
268 error = vm_alloc_memseg(sc->vm, mseg->segid, mseg->len, sysmem);
272 if (VM_MEMSEG_NAME(mseg)) {
273 error = devmem_create_cdev(vm_name(sc->vm), mseg->segid, name);
275 vm_free_memseg(sc->vm, mseg->segid);
277 name = NULL; /* freed when 'cdev' is destroyed */
280 free(name, M_VMMDEV);
285 vm_get_register_set(struct vm *vm, int vcpu, unsigned int count, int *regnum,
291 for (i = 0; i < count; i++) {
292 error = vm_get_register(vm, vcpu, regnum[i], ®val[i]);
300 vm_set_register_set(struct vm *vm, int vcpu, unsigned int count, int *regnum,
306 for (i = 0; i < count; i++) {
307 error = vm_set_register(vm, vcpu, regnum[i], regval[i]);
315 vmmdev_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
318 int error, vcpu, state_changed, size;
320 struct vmmdev_softc *sc;
321 struct vm_register *vmreg;
322 struct vm_seg_desc *vmsegdesc;
323 struct vm_register_set *vmregset;
324 struct vm_run *vmrun;
325 struct vm_exception *vmexc;
326 struct vm_lapic_irq *vmirq;
327 struct vm_lapic_msi *vmmsi;
328 struct vm_ioapic_irq *ioapic_irq;
329 struct vm_isa_irq *isa_irq;
330 struct vm_isa_irq_trigger *isa_irq_trigger;
331 struct vm_capability *vmcap;
332 struct vm_pptdev *pptdev;
333 struct vm_pptdev_mmio *pptmmio;
334 struct vm_pptdev_msi *pptmsi;
335 struct vm_pptdev_msix *pptmsix;
336 struct vm_nmi *vmnmi;
337 struct vm_stats *vmstats;
338 struct vm_stat_desc *statdesc;
339 struct vm_x2apic *x2apic;
340 struct vm_gpa_pte *gpapte;
341 struct vm_suspend *vmsuspend;
342 struct vm_gla2gpa *gg;
343 struct vm_activate_cpu *vac;
344 struct vm_cpuset *vm_cpuset;
345 struct vm_intinfo *vmii;
346 struct vm_rtc_time *rtctime;
347 struct vm_rtc_data *rtcdata;
348 struct vm_memmap *mm;
352 sc = vmmdev_lookup2(cdev);
361 * Some VMM ioctls can operate only on vcpus that are not running.
365 case VM_GET_REGISTER:
366 case VM_SET_REGISTER:
367 case VM_GET_SEGMENT_DESCRIPTOR:
368 case VM_SET_SEGMENT_DESCRIPTOR:
369 case VM_GET_REGISTER_SET:
370 case VM_SET_REGISTER_SET:
371 case VM_INJECT_EXCEPTION:
372 case VM_GET_CAPABILITY:
373 case VM_SET_CAPABILITY:
376 case VM_SET_X2APIC_STATE:
378 case VM_GLA2GPA_NOFAULT:
379 case VM_ACTIVATE_CPU:
382 case VM_RESTART_INSTRUCTION:
384 * XXX fragile, handle with care
385 * Assumes that the first field of the ioctl data is the vcpu.
388 error = vcpu_lock_one(sc, vcpu);
394 case VM_MAP_PPTDEV_MMIO:
396 case VM_UNBIND_PPTDEV:
397 case VM_ALLOC_MEMSEG:
401 * ioctls that operate on the entire virtual machine must
402 * prevent all vcpus from running.
404 error = vcpu_lock_all(sc);
411 case VM_MMAP_GETNEXT:
413 * Lock a vcpu to make sure that the memory map cannot be
414 * modified while it is being inspected.
416 vcpu = VM_MAXCPU - 1;
417 error = vcpu_lock_one(sc, vcpu);
429 vmrun = (struct vm_run *)data;
430 error = vm_run(sc->vm, vmrun);
433 vmsuspend = (struct vm_suspend *)data;
434 error = vm_suspend(sc->vm, vmsuspend->how);
437 error = vm_reinit(sc->vm);
440 statdesc = (struct vm_stat_desc *)data;
441 error = vmm_stat_desc_copy(statdesc->index,
442 statdesc->desc, sizeof(statdesc->desc));
446 CTASSERT(MAX_VM_STATS >= MAX_VMM_STAT_ELEMS);
447 vmstats = (struct vm_stats *)data;
448 getmicrotime(&vmstats->tv);
449 error = vmm_stat_copy(sc->vm, vmstats->cpuid,
450 &vmstats->num_entries, vmstats->statbuf);
454 pptmsi = (struct vm_pptdev_msi *)data;
455 error = ppt_setup_msi(sc->vm, pptmsi->vcpu,
456 pptmsi->bus, pptmsi->slot, pptmsi->func,
457 pptmsi->addr, pptmsi->msg,
461 pptmsix = (struct vm_pptdev_msix *)data;
462 error = ppt_setup_msix(sc->vm, pptmsix->vcpu,
463 pptmsix->bus, pptmsix->slot,
464 pptmsix->func, pptmsix->idx,
465 pptmsix->addr, pptmsix->msg,
466 pptmsix->vector_control);
468 case VM_MAP_PPTDEV_MMIO:
469 pptmmio = (struct vm_pptdev_mmio *)data;
470 error = ppt_map_mmio(sc->vm, pptmmio->bus, pptmmio->slot,
471 pptmmio->func, pptmmio->gpa, pptmmio->len,
475 pptdev = (struct vm_pptdev *)data;
476 error = vm_assign_pptdev(sc->vm, pptdev->bus, pptdev->slot,
479 case VM_UNBIND_PPTDEV:
480 pptdev = (struct vm_pptdev *)data;
481 error = vm_unassign_pptdev(sc->vm, pptdev->bus, pptdev->slot,
484 case VM_INJECT_EXCEPTION:
485 vmexc = (struct vm_exception *)data;
486 error = vm_inject_exception(sc->vm, vmexc->cpuid,
487 vmexc->vector, vmexc->error_code_valid, vmexc->error_code,
488 vmexc->restart_instruction);
491 vmnmi = (struct vm_nmi *)data;
492 error = vm_inject_nmi(sc->vm, vmnmi->cpuid);
495 vmirq = (struct vm_lapic_irq *)data;
496 error = lapic_intr_edge(sc->vm, vmirq->cpuid, vmirq->vector);
498 case VM_LAPIC_LOCAL_IRQ:
499 vmirq = (struct vm_lapic_irq *)data;
500 error = lapic_set_local_intr(sc->vm, vmirq->cpuid,
504 vmmsi = (struct vm_lapic_msi *)data;
505 error = lapic_intr_msi(sc->vm, vmmsi->addr, vmmsi->msg);
507 case VM_IOAPIC_ASSERT_IRQ:
508 ioapic_irq = (struct vm_ioapic_irq *)data;
509 error = vioapic_assert_irq(sc->vm, ioapic_irq->irq);
511 case VM_IOAPIC_DEASSERT_IRQ:
512 ioapic_irq = (struct vm_ioapic_irq *)data;
513 error = vioapic_deassert_irq(sc->vm, ioapic_irq->irq);
515 case VM_IOAPIC_PULSE_IRQ:
516 ioapic_irq = (struct vm_ioapic_irq *)data;
517 error = vioapic_pulse_irq(sc->vm, ioapic_irq->irq);
519 case VM_IOAPIC_PINCOUNT:
520 *(int *)data = vioapic_pincount(sc->vm);
522 case VM_ISA_ASSERT_IRQ:
523 isa_irq = (struct vm_isa_irq *)data;
524 error = vatpic_assert_irq(sc->vm, isa_irq->atpic_irq);
525 if (error == 0 && isa_irq->ioapic_irq != -1)
526 error = vioapic_assert_irq(sc->vm,
527 isa_irq->ioapic_irq);
529 case VM_ISA_DEASSERT_IRQ:
530 isa_irq = (struct vm_isa_irq *)data;
531 error = vatpic_deassert_irq(sc->vm, isa_irq->atpic_irq);
532 if (error == 0 && isa_irq->ioapic_irq != -1)
533 error = vioapic_deassert_irq(sc->vm,
534 isa_irq->ioapic_irq);
536 case VM_ISA_PULSE_IRQ:
537 isa_irq = (struct vm_isa_irq *)data;
538 error = vatpic_pulse_irq(sc->vm, isa_irq->atpic_irq);
539 if (error == 0 && isa_irq->ioapic_irq != -1)
540 error = vioapic_pulse_irq(sc->vm, isa_irq->ioapic_irq);
542 case VM_ISA_SET_IRQ_TRIGGER:
543 isa_irq_trigger = (struct vm_isa_irq_trigger *)data;
544 error = vatpic_set_irq_trigger(sc->vm,
545 isa_irq_trigger->atpic_irq, isa_irq_trigger->trigger);
547 case VM_MMAP_GETNEXT:
548 mm = (struct vm_memmap *)data;
549 error = vm_mmap_getnext(sc->vm, &mm->gpa, &mm->segid,
550 &mm->segoff, &mm->len, &mm->prot, &mm->flags);
553 mm = (struct vm_memmap *)data;
554 error = vm_mmap_memseg(sc->vm, mm->gpa, mm->segid, mm->segoff,
555 mm->len, mm->prot, mm->flags);
557 case VM_ALLOC_MEMSEG:
558 error = alloc_memseg(sc, (struct vm_memseg *)data);
561 error = get_memseg(sc, (struct vm_memseg *)data);
563 case VM_GET_REGISTER:
564 vmreg = (struct vm_register *)data;
565 error = vm_get_register(sc->vm, vmreg->cpuid, vmreg->regnum,
568 case VM_SET_REGISTER:
569 vmreg = (struct vm_register *)data;
570 error = vm_set_register(sc->vm, vmreg->cpuid, vmreg->regnum,
573 case VM_SET_SEGMENT_DESCRIPTOR:
574 vmsegdesc = (struct vm_seg_desc *)data;
575 error = vm_set_seg_desc(sc->vm, vmsegdesc->cpuid,
579 case VM_GET_SEGMENT_DESCRIPTOR:
580 vmsegdesc = (struct vm_seg_desc *)data;
581 error = vm_get_seg_desc(sc->vm, vmsegdesc->cpuid,
585 case VM_GET_REGISTER_SET:
586 vmregset = (struct vm_register_set *)data;
587 if (vmregset->count > VM_REG_LAST) {
591 regvals = malloc(sizeof(regvals[0]) * vmregset->count, M_VMMDEV,
593 regnums = malloc(sizeof(regnums[0]) * vmregset->count, M_VMMDEV,
595 error = copyin(vmregset->regnums, regnums, sizeof(regnums[0]) *
598 error = vm_get_register_set(sc->vm, vmregset->cpuid,
599 vmregset->count, regnums, regvals);
601 error = copyout(regvals, vmregset->regvals,
602 sizeof(regvals[0]) * vmregset->count);
603 free(regvals, M_VMMDEV);
604 free(regnums, M_VMMDEV);
606 case VM_SET_REGISTER_SET:
607 vmregset = (struct vm_register_set *)data;
608 if (vmregset->count > VM_REG_LAST) {
612 regvals = malloc(sizeof(regvals[0]) * vmregset->count, M_VMMDEV,
614 regnums = malloc(sizeof(regnums[0]) * vmregset->count, M_VMMDEV,
616 error = copyin(vmregset->regnums, regnums, sizeof(regnums[0]) *
619 error = copyin(vmregset->regvals, regvals,
620 sizeof(regvals[0]) * vmregset->count);
622 error = vm_set_register_set(sc->vm, vmregset->cpuid,
623 vmregset->count, regnums, regvals);
624 free(regvals, M_VMMDEV);
625 free(regnums, M_VMMDEV);
627 case VM_GET_CAPABILITY:
628 vmcap = (struct vm_capability *)data;
629 error = vm_get_capability(sc->vm, vmcap->cpuid,
633 case VM_SET_CAPABILITY:
634 vmcap = (struct vm_capability *)data;
635 error = vm_set_capability(sc->vm, vmcap->cpuid,
639 case VM_SET_X2APIC_STATE:
640 x2apic = (struct vm_x2apic *)data;
641 error = vm_set_x2apic_state(sc->vm,
642 x2apic->cpuid, x2apic->state);
644 case VM_GET_X2APIC_STATE:
645 x2apic = (struct vm_x2apic *)data;
646 error = vm_get_x2apic_state(sc->vm,
647 x2apic->cpuid, &x2apic->state);
649 case VM_GET_GPA_PMAP:
650 gpapte = (struct vm_gpa_pte *)data;
651 pmap_get_mapping(vmspace_pmap(vm_get_vmspace(sc->vm)),
652 gpapte->gpa, gpapte->pte, &gpapte->ptenum);
655 case VM_GET_HPET_CAPABILITIES:
656 error = vhpet_getcap((struct vm_hpet_cap *)data);
659 CTASSERT(PROT_READ == VM_PROT_READ);
660 CTASSERT(PROT_WRITE == VM_PROT_WRITE);
661 CTASSERT(PROT_EXEC == VM_PROT_EXECUTE);
662 gg = (struct vm_gla2gpa *)data;
663 error = vm_gla2gpa(sc->vm, gg->vcpuid, &gg->paging, gg->gla,
664 gg->prot, &gg->gpa, &gg->fault);
665 KASSERT(error == 0 || error == EFAULT,
666 ("%s: vm_gla2gpa unknown error %d", __func__, error));
669 case VM_GLA2GPA_NOFAULT:
670 gg = (struct vm_gla2gpa *)data;
671 error = vm_gla2gpa_nofault(sc->vm, gg->vcpuid, &gg->paging,
672 gg->gla, gg->prot, &gg->gpa, &gg->fault);
673 KASSERT(error == 0 || error == EFAULT,
674 ("%s: vm_gla2gpa unknown error %d", __func__, error));
676 case VM_ACTIVATE_CPU:
677 vac = (struct vm_activate_cpu *)data;
678 error = vm_activate_cpu(sc->vm, vac->vcpuid);
682 vm_cpuset = (struct vm_cpuset *)data;
683 size = vm_cpuset->cpusetsize;
684 if (size < sizeof(cpuset_t) || size > CPU_MAXSIZE / NBBY) {
688 cpuset = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
689 if (vm_cpuset->which == VM_ACTIVE_CPUS)
690 *cpuset = vm_active_cpus(sc->vm);
691 else if (vm_cpuset->which == VM_SUSPENDED_CPUS)
692 *cpuset = vm_suspended_cpus(sc->vm);
696 error = copyout(cpuset, vm_cpuset->cpus, size);
697 free(cpuset, M_TEMP);
700 vmii = (struct vm_intinfo *)data;
701 error = vm_exit_intinfo(sc->vm, vmii->vcpuid, vmii->info1);
704 vmii = (struct vm_intinfo *)data;
705 error = vm_get_intinfo(sc->vm, vmii->vcpuid, &vmii->info1,
709 rtcdata = (struct vm_rtc_data *)data;
710 error = vrtc_nvram_write(sc->vm, rtcdata->offset,
714 rtcdata = (struct vm_rtc_data *)data;
715 error = vrtc_nvram_read(sc->vm, rtcdata->offset,
719 rtctime = (struct vm_rtc_time *)data;
720 error = vrtc_set_time(sc->vm, rtctime->secs);
724 rtctime = (struct vm_rtc_time *)data;
725 rtctime->secs = vrtc_get_time(sc->vm);
727 case VM_RESTART_INSTRUCTION:
728 error = vm_restart_instruction(sc->vm, vcpu);
735 if (state_changed == 1)
736 vcpu_unlock_one(sc, vcpu);
737 else if (state_changed == 2)
741 /* Make sure that no handler returns a bogus value like ERESTART */
742 KASSERT(error >= 0, ("vmmdev_ioctl: invalid error return %d", error));
747 vmmdev_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t mapsize,
748 struct vm_object **objp, int nprot)
750 struct vmmdev_softc *sc;
753 vm_ooffset_t segoff, first, last;
754 int error, found, segid;
758 last = first + mapsize;
759 if ((nprot & PROT_EXEC) || first < 0 || first >= last)
762 sc = vmmdev_lookup2(cdev);
764 /* virtual machine is in the process of being created */
769 * Get a read lock on the guest memory map by freezing any vcpu.
771 error = vcpu_lock_one(sc, VM_MAXCPU - 1);
778 error = vm_mmap_getnext(sc->vm, &gpa, &segid, &segoff, &len,
783 if (first >= gpa && last <= gpa + len)
790 error = vm_get_memseg(sc->vm, segid, &len, &sysmem, objp);
791 KASSERT(error == 0 && *objp != NULL,
792 ("%s: invalid memory segment %d", __func__, segid));
794 vm_object_reference(*objp);
795 *offset = segoff + (first - gpa);
800 vcpu_unlock_one(sc, VM_MAXCPU - 1);
805 vmmdev_destroy(void *arg)
807 struct vmmdev_softc *sc = arg;
808 struct devmem_softc *dsc;
811 error = vcpu_lock_all(sc);
812 KASSERT(error == 0, ("%s: error %d freezing vcpus", __func__, error));
814 while ((dsc = SLIST_FIRST(&sc->devmem)) != NULL) {
815 KASSERT(dsc->cdev == NULL, ("%s: devmem not free", __func__));
816 SLIST_REMOVE_HEAD(&sc->devmem, link);
817 free(dsc->name, M_VMMDEV);
821 if (sc->cdev != NULL)
822 destroy_dev(sc->cdev);
827 if ((sc->flags & VSC_LINKED) != 0) {
828 mtx_lock(&vmmdev_mtx);
829 SLIST_REMOVE(&head, sc, vmmdev_softc, link);
830 mtx_unlock(&vmmdev_mtx);
837 sysctl_vmm_destroy(SYSCTL_HANDLER_ARGS)
840 char buf[VM_MAX_NAMELEN];
841 struct devmem_softc *dsc;
842 struct vmmdev_softc *sc;
845 strlcpy(buf, "beavis", sizeof(buf));
846 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
847 if (error != 0 || req->newptr == NULL)
850 mtx_lock(&vmmdev_mtx);
851 sc = vmmdev_lookup(buf);
852 if (sc == NULL || sc->cdev == NULL) {
853 mtx_unlock(&vmmdev_mtx);
858 * The 'cdev' will be destroyed asynchronously when 'si_threadcount'
859 * goes down to 0 so we should not do it again in the callback.
861 * Setting 'sc->cdev' to NULL is also used to indicate that the VM
862 * is scheduled for destruction.
866 mtx_unlock(&vmmdev_mtx);
869 * Schedule all cdevs to be destroyed:
871 * - any new operations on the 'cdev' will return an error (ENXIO).
873 * - when the 'si_threadcount' dwindles down to zero the 'cdev' will
874 * be destroyed and the callback will be invoked in a taskqueue
877 * - the 'devmem' cdevs are destroyed before the virtual machine 'cdev'
879 SLIST_FOREACH(dsc, &sc->devmem, link) {
880 KASSERT(dsc->cdev != NULL, ("devmem cdev already destroyed"));
881 destroy_dev_sched_cb(dsc->cdev, devmem_destroy, dsc);
883 destroy_dev_sched_cb(cdev, vmmdev_destroy, sc);
886 SYSCTL_PROC(_hw_vmm, OID_AUTO, destroy, CTLTYPE_STRING | CTLFLAG_RW,
887 NULL, 0, sysctl_vmm_destroy, "A", NULL);
889 static struct cdevsw vmmdevsw = {
891 .d_version = D_VERSION,
892 .d_ioctl = vmmdev_ioctl,
893 .d_mmap_single = vmmdev_mmap_single,
895 .d_write = vmmdev_rw,
899 sysctl_vmm_create(SYSCTL_HANDLER_ARGS)
904 struct vmmdev_softc *sc, *sc2;
905 char buf[VM_MAX_NAMELEN];
907 strlcpy(buf, "beavis", sizeof(buf));
908 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
909 if (error != 0 || req->newptr == NULL)
912 mtx_lock(&vmmdev_mtx);
913 sc = vmmdev_lookup(buf);
914 mtx_unlock(&vmmdev_mtx);
918 error = vm_create(buf, &vm);
922 sc = malloc(sizeof(struct vmmdev_softc), M_VMMDEV, M_WAITOK | M_ZERO);
924 SLIST_INIT(&sc->devmem);
927 * Lookup the name again just in case somebody sneaked in when we
930 mtx_lock(&vmmdev_mtx);
931 sc2 = vmmdev_lookup(buf);
933 SLIST_INSERT_HEAD(&head, sc, link);
934 sc->flags |= VSC_LINKED;
936 mtx_unlock(&vmmdev_mtx);
943 error = make_dev_p(MAKEDEV_CHECKNAME, &cdev, &vmmdevsw, NULL,
944 UID_ROOT, GID_WHEEL, 0600, "vmm/%s", buf);
950 mtx_lock(&vmmdev_mtx);
952 sc->cdev->si_drv1 = sc;
953 mtx_unlock(&vmmdev_mtx);
957 SYSCTL_PROC(_hw_vmm, OID_AUTO, create, CTLTYPE_STRING | CTLFLAG_RW,
958 NULL, 0, sysctl_vmm_create, "A", NULL);
963 mtx_init(&vmmdev_mtx, "vmm device mutex", NULL, MTX_DEF);
971 if (SLIST_EMPTY(&head))
980 devmem_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t len,
981 struct vm_object **objp, int nprot)
983 struct devmem_softc *dsc;
984 vm_ooffset_t first, last;
991 /* 'cdev' has been created but is not ready for use */
996 last = *offset + len;
997 if ((nprot & PROT_EXEC) || first < 0 || first >= last)
1000 error = vcpu_lock_one(dsc->sc, VM_MAXCPU - 1);
1004 error = vm_get_memseg(dsc->sc->vm, dsc->segid, &seglen, &sysmem, objp);
1005 KASSERT(error == 0 && !sysmem && *objp != NULL,
1006 ("%s: invalid devmem segment %d", __func__, dsc->segid));
1008 vcpu_unlock_one(dsc->sc, VM_MAXCPU - 1);
1010 if (seglen >= last) {
1011 vm_object_reference(*objp);
1018 static struct cdevsw devmemsw = {
1020 .d_version = D_VERSION,
1021 .d_mmap_single = devmem_mmap_single,
1025 devmem_create_cdev(const char *vmname, int segid, char *devname)
1027 struct devmem_softc *dsc;
1028 struct vmmdev_softc *sc;
1032 error = make_dev_p(MAKEDEV_CHECKNAME, &cdev, &devmemsw, NULL,
1033 UID_ROOT, GID_WHEEL, 0600, "vmm.io/%s.%s", vmname, devname);
1037 dsc = malloc(sizeof(struct devmem_softc), M_VMMDEV, M_WAITOK | M_ZERO);
1039 mtx_lock(&vmmdev_mtx);
1040 sc = vmmdev_lookup(vmname);
1041 KASSERT(sc != NULL, ("%s: vm %s softc not found", __func__, vmname));
1042 if (sc->cdev == NULL) {
1043 /* virtual machine is being created or destroyed */
1044 mtx_unlock(&vmmdev_mtx);
1045 free(dsc, M_VMMDEV);
1046 destroy_dev_sched_cb(cdev, NULL, 0);
1051 dsc->name = devname;
1054 SLIST_INSERT_HEAD(&sc->devmem, dsc, link);
1055 mtx_unlock(&vmmdev_mtx);
1057 /* The 'cdev' is ready for use after 'si_drv1' is initialized */
1058 cdev->si_drv1 = dsc;
1063 devmem_destroy(void *arg)
1065 struct devmem_softc *dsc = arg;
1067 KASSERT(dsc->cdev, ("%s: devmem cdev already destroyed", __func__));