2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/queue.h>
36 #include <sys/mutex.h>
37 #include <sys/malloc.h>
39 #include <sys/sysctl.h>
40 #include <sys/libkern.h>
41 #include <sys/ioccom.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_object.h>
50 #include <machine/vmparam.h>
51 #include <machine/vmm.h>
52 #include <machine/vmm_instruction_emul.h>
53 #include <machine/vmm_dev.h>
55 #include "vmm_lapic.h"
59 #include "io/vatpic.h"
60 #include "io/vioapic.h"
68 struct vmmdev_softc *sc;
69 SLIST_ENTRY(devmem_softc) link;
73 struct vm *vm; /* vm instance cookie */
75 SLIST_ENTRY(vmmdev_softc) link;
76 SLIST_HEAD(, devmem_softc) devmem;
79 #define VSC_LINKED 0x01
81 static SLIST_HEAD(, vmmdev_softc) head;
83 static struct mtx vmmdev_mtx;
85 static MALLOC_DEFINE(M_VMMDEV, "vmmdev", "vmmdev");
89 static int devmem_create_cdev(const char *vmname, int id, char *devmem);
90 static void devmem_destroy(void *arg);
93 vcpu_lock_one(struct vmmdev_softc *sc, int vcpu)
97 if (vcpu < 0 || vcpu >= VM_MAXCPU)
100 error = vcpu_set_state(sc->vm, vcpu, VCPU_FROZEN, true);
105 vcpu_unlock_one(struct vmmdev_softc *sc, int vcpu)
107 enum vcpu_state state;
109 state = vcpu_get_state(sc->vm, vcpu, NULL);
110 if (state != VCPU_FROZEN) {
111 panic("vcpu %s(%d) has invalid state %d", vm_name(sc->vm),
115 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false);
119 vcpu_lock_all(struct vmmdev_softc *sc)
123 for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++) {
124 error = vcpu_lock_one(sc, vcpu);
131 vcpu_unlock_one(sc, vcpu);
138 vcpu_unlock_all(struct vmmdev_softc *sc)
142 for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++)
143 vcpu_unlock_one(sc, vcpu);
146 static struct vmmdev_softc *
147 vmmdev_lookup(const char *name)
149 struct vmmdev_softc *sc;
151 #ifdef notyet /* XXX kernel is not compiled with invariants */
152 mtx_assert(&vmmdev_mtx, MA_OWNED);
155 SLIST_FOREACH(sc, &head, link) {
156 if (strcmp(name, vm_name(sc->vm)) == 0)
163 static struct vmmdev_softc *
164 vmmdev_lookup2(struct cdev *cdev)
167 return (cdev->si_drv1);
171 vmmdev_rw(struct cdev *cdev, struct uio *uio, int flags)
173 int error, off, c, prot;
176 struct vmmdev_softc *sc;
178 sc = vmmdev_lookup2(cdev);
183 * Get a read lock on the guest memory map by freezing any vcpu.
185 error = vcpu_lock_one(sc, VM_MAXCPU - 1);
189 prot = (uio->uio_rw == UIO_WRITE ? VM_PROT_WRITE : VM_PROT_READ);
190 while (uio->uio_resid > 0 && error == 0) {
191 gpa = uio->uio_offset;
192 off = gpa & PAGE_MASK;
193 c = min(uio->uio_resid, PAGE_SIZE - off);
196 * The VM has a hole in its physical memory map. If we want to
197 * use 'dd' to inspect memory beyond the hole we need to
198 * provide bogus data for memory that lies in the hole.
200 * Since this device does not support lseek(2), dd(1) will
201 * read(2) blocks of data to simulate the lseek(2).
203 hpa = vm_gpa_hold(sc->vm, VM_MAXCPU - 1, gpa, c, prot, &cookie);
205 if (uio->uio_rw == UIO_READ)
206 error = uiomove(__DECONST(void *, zero_region),
211 error = uiomove(hpa, c, uio);
212 vm_gpa_release(cookie);
215 vcpu_unlock_one(sc, VM_MAXCPU - 1);
219 CTASSERT(sizeof(((struct vm_memseg *)0)->name) >= SPECNAMELEN + 1);
222 get_memseg(struct vmmdev_softc *sc, struct vm_memseg *mseg)
224 struct devmem_softc *dsc;
228 error = vm_get_memseg(sc->vm, mseg->segid, &mseg->len, &sysmem, NULL);
229 if (error || mseg->len == 0)
233 SLIST_FOREACH(dsc, &sc->devmem, link) {
234 if (dsc->segid == mseg->segid)
237 KASSERT(dsc != NULL, ("%s: devmem segment %d not found",
238 __func__, mseg->segid));
239 error = copystr(dsc->name, mseg->name, SPECNAMELEN + 1, NULL);
241 bzero(mseg->name, sizeof(mseg->name));
248 alloc_memseg(struct vmmdev_softc *sc, struct vm_memseg *mseg)
258 if (VM_MEMSEG_NAME(mseg)) {
260 name = malloc(SPECNAMELEN + 1, M_VMMDEV, M_WAITOK);
261 error = copystr(VM_MEMSEG_NAME(mseg), name, SPECNAMELEN + 1, 0);
266 error = vm_alloc_memseg(sc->vm, mseg->segid, mseg->len, sysmem);
270 if (VM_MEMSEG_NAME(mseg)) {
271 error = devmem_create_cdev(vm_name(sc->vm), mseg->segid, name);
273 vm_free_memseg(sc->vm, mseg->segid);
275 name = NULL; /* freed when 'cdev' is destroyed */
278 free(name, M_VMMDEV);
283 vmmdev_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
286 int error, vcpu, state_changed, size;
288 struct vmmdev_softc *sc;
289 struct vm_register *vmreg;
290 struct vm_seg_desc *vmsegdesc;
291 struct vm_run *vmrun;
292 struct vm_exception *vmexc;
293 struct vm_lapic_irq *vmirq;
294 struct vm_lapic_msi *vmmsi;
295 struct vm_ioapic_irq *ioapic_irq;
296 struct vm_isa_irq *isa_irq;
297 struct vm_isa_irq_trigger *isa_irq_trigger;
298 struct vm_capability *vmcap;
299 struct vm_pptdev *pptdev;
300 struct vm_pptdev_mmio *pptmmio;
301 struct vm_pptdev_msi *pptmsi;
302 struct vm_pptdev_msix *pptmsix;
303 struct vm_nmi *vmnmi;
304 struct vm_stats *vmstats;
305 struct vm_stat_desc *statdesc;
306 struct vm_x2apic *x2apic;
307 struct vm_gpa_pte *gpapte;
308 struct vm_suspend *vmsuspend;
309 struct vm_gla2gpa *gg;
310 struct vm_activate_cpu *vac;
311 struct vm_cpuset *vm_cpuset;
312 struct vm_intinfo *vmii;
313 struct vm_rtc_time *rtctime;
314 struct vm_rtc_data *rtcdata;
315 struct vm_memmap *mm;
317 sc = vmmdev_lookup2(cdev);
326 * Some VMM ioctls can operate only on vcpus that are not running.
330 case VM_GET_REGISTER:
331 case VM_SET_REGISTER:
332 case VM_GET_SEGMENT_DESCRIPTOR:
333 case VM_SET_SEGMENT_DESCRIPTOR:
334 case VM_INJECT_EXCEPTION:
335 case VM_GET_CAPABILITY:
336 case VM_SET_CAPABILITY:
339 case VM_SET_X2APIC_STATE:
341 case VM_ACTIVATE_CPU:
344 case VM_RESTART_INSTRUCTION:
346 * XXX fragile, handle with care
347 * Assumes that the first field of the ioctl data is the vcpu.
350 error = vcpu_lock_one(sc, vcpu);
356 case VM_MAP_PPTDEV_MMIO:
358 case VM_UNBIND_PPTDEV:
359 case VM_ALLOC_MEMSEG:
363 * ioctls that operate on the entire virtual machine must
364 * prevent all vcpus from running.
366 error = vcpu_lock_all(sc);
373 case VM_MMAP_GETNEXT:
375 * Lock a vcpu to make sure that the memory map cannot be
376 * modified while it is being inspected.
378 vcpu = VM_MAXCPU - 1;
379 error = vcpu_lock_one(sc, vcpu);
391 vmrun = (struct vm_run *)data;
392 error = vm_run(sc->vm, vmrun);
395 vmsuspend = (struct vm_suspend *)data;
396 error = vm_suspend(sc->vm, vmsuspend->how);
399 error = vm_reinit(sc->vm);
402 statdesc = (struct vm_stat_desc *)data;
403 error = vmm_stat_desc_copy(statdesc->index,
404 statdesc->desc, sizeof(statdesc->desc));
408 CTASSERT(MAX_VM_STATS >= MAX_VMM_STAT_ELEMS);
409 vmstats = (struct vm_stats *)data;
410 getmicrotime(&vmstats->tv);
411 error = vmm_stat_copy(sc->vm, vmstats->cpuid,
412 &vmstats->num_entries, vmstats->statbuf);
416 pptmsi = (struct vm_pptdev_msi *)data;
417 error = ppt_setup_msi(sc->vm, pptmsi->vcpu,
418 pptmsi->bus, pptmsi->slot, pptmsi->func,
419 pptmsi->addr, pptmsi->msg,
423 pptmsix = (struct vm_pptdev_msix *)data;
424 error = ppt_setup_msix(sc->vm, pptmsix->vcpu,
425 pptmsix->bus, pptmsix->slot,
426 pptmsix->func, pptmsix->idx,
427 pptmsix->addr, pptmsix->msg,
428 pptmsix->vector_control);
430 case VM_MAP_PPTDEV_MMIO:
431 pptmmio = (struct vm_pptdev_mmio *)data;
432 error = ppt_map_mmio(sc->vm, pptmmio->bus, pptmmio->slot,
433 pptmmio->func, pptmmio->gpa, pptmmio->len,
437 pptdev = (struct vm_pptdev *)data;
438 error = vm_assign_pptdev(sc->vm, pptdev->bus, pptdev->slot,
441 case VM_UNBIND_PPTDEV:
442 pptdev = (struct vm_pptdev *)data;
443 error = vm_unassign_pptdev(sc->vm, pptdev->bus, pptdev->slot,
446 case VM_INJECT_EXCEPTION:
447 vmexc = (struct vm_exception *)data;
448 error = vm_inject_exception(sc->vm, vmexc->cpuid,
449 vmexc->vector, vmexc->error_code_valid, vmexc->error_code,
450 vmexc->restart_instruction);
453 vmnmi = (struct vm_nmi *)data;
454 error = vm_inject_nmi(sc->vm, vmnmi->cpuid);
457 vmirq = (struct vm_lapic_irq *)data;
458 error = lapic_intr_edge(sc->vm, vmirq->cpuid, vmirq->vector);
460 case VM_LAPIC_LOCAL_IRQ:
461 vmirq = (struct vm_lapic_irq *)data;
462 error = lapic_set_local_intr(sc->vm, vmirq->cpuid,
466 vmmsi = (struct vm_lapic_msi *)data;
467 error = lapic_intr_msi(sc->vm, vmmsi->addr, vmmsi->msg);
469 case VM_IOAPIC_ASSERT_IRQ:
470 ioapic_irq = (struct vm_ioapic_irq *)data;
471 error = vioapic_assert_irq(sc->vm, ioapic_irq->irq);
473 case VM_IOAPIC_DEASSERT_IRQ:
474 ioapic_irq = (struct vm_ioapic_irq *)data;
475 error = vioapic_deassert_irq(sc->vm, ioapic_irq->irq);
477 case VM_IOAPIC_PULSE_IRQ:
478 ioapic_irq = (struct vm_ioapic_irq *)data;
479 error = vioapic_pulse_irq(sc->vm, ioapic_irq->irq);
481 case VM_IOAPIC_PINCOUNT:
482 *(int *)data = vioapic_pincount(sc->vm);
484 case VM_ISA_ASSERT_IRQ:
485 isa_irq = (struct vm_isa_irq *)data;
486 error = vatpic_assert_irq(sc->vm, isa_irq->atpic_irq);
487 if (error == 0 && isa_irq->ioapic_irq != -1)
488 error = vioapic_assert_irq(sc->vm,
489 isa_irq->ioapic_irq);
491 case VM_ISA_DEASSERT_IRQ:
492 isa_irq = (struct vm_isa_irq *)data;
493 error = vatpic_deassert_irq(sc->vm, isa_irq->atpic_irq);
494 if (error == 0 && isa_irq->ioapic_irq != -1)
495 error = vioapic_deassert_irq(sc->vm,
496 isa_irq->ioapic_irq);
498 case VM_ISA_PULSE_IRQ:
499 isa_irq = (struct vm_isa_irq *)data;
500 error = vatpic_pulse_irq(sc->vm, isa_irq->atpic_irq);
501 if (error == 0 && isa_irq->ioapic_irq != -1)
502 error = vioapic_pulse_irq(sc->vm, isa_irq->ioapic_irq);
504 case VM_ISA_SET_IRQ_TRIGGER:
505 isa_irq_trigger = (struct vm_isa_irq_trigger *)data;
506 error = vatpic_set_irq_trigger(sc->vm,
507 isa_irq_trigger->atpic_irq, isa_irq_trigger->trigger);
509 case VM_MMAP_GETNEXT:
510 mm = (struct vm_memmap *)data;
511 error = vm_mmap_getnext(sc->vm, &mm->gpa, &mm->segid,
512 &mm->segoff, &mm->len, &mm->prot, &mm->flags);
515 mm = (struct vm_memmap *)data;
516 error = vm_mmap_memseg(sc->vm, mm->gpa, mm->segid, mm->segoff,
517 mm->len, mm->prot, mm->flags);
519 case VM_ALLOC_MEMSEG:
520 error = alloc_memseg(sc, (struct vm_memseg *)data);
523 error = get_memseg(sc, (struct vm_memseg *)data);
525 case VM_GET_REGISTER:
526 vmreg = (struct vm_register *)data;
527 error = vm_get_register(sc->vm, vmreg->cpuid, vmreg->regnum,
530 case VM_SET_REGISTER:
531 vmreg = (struct vm_register *)data;
532 error = vm_set_register(sc->vm, vmreg->cpuid, vmreg->regnum,
535 case VM_SET_SEGMENT_DESCRIPTOR:
536 vmsegdesc = (struct vm_seg_desc *)data;
537 error = vm_set_seg_desc(sc->vm, vmsegdesc->cpuid,
541 case VM_GET_SEGMENT_DESCRIPTOR:
542 vmsegdesc = (struct vm_seg_desc *)data;
543 error = vm_get_seg_desc(sc->vm, vmsegdesc->cpuid,
547 case VM_GET_CAPABILITY:
548 vmcap = (struct vm_capability *)data;
549 error = vm_get_capability(sc->vm, vmcap->cpuid,
553 case VM_SET_CAPABILITY:
554 vmcap = (struct vm_capability *)data;
555 error = vm_set_capability(sc->vm, vmcap->cpuid,
559 case VM_SET_X2APIC_STATE:
560 x2apic = (struct vm_x2apic *)data;
561 error = vm_set_x2apic_state(sc->vm,
562 x2apic->cpuid, x2apic->state);
564 case VM_GET_X2APIC_STATE:
565 x2apic = (struct vm_x2apic *)data;
566 error = vm_get_x2apic_state(sc->vm,
567 x2apic->cpuid, &x2apic->state);
569 case VM_GET_GPA_PMAP:
570 gpapte = (struct vm_gpa_pte *)data;
571 pmap_get_mapping(vmspace_pmap(vm_get_vmspace(sc->vm)),
572 gpapte->gpa, gpapte->pte, &gpapte->ptenum);
575 case VM_GET_HPET_CAPABILITIES:
576 error = vhpet_getcap((struct vm_hpet_cap *)data);
579 CTASSERT(PROT_READ == VM_PROT_READ);
580 CTASSERT(PROT_WRITE == VM_PROT_WRITE);
581 CTASSERT(PROT_EXEC == VM_PROT_EXECUTE);
582 gg = (struct vm_gla2gpa *)data;
583 error = vm_gla2gpa(sc->vm, gg->vcpuid, &gg->paging, gg->gla,
584 gg->prot, &gg->gpa, &gg->fault);
585 KASSERT(error == 0 || error == EFAULT,
586 ("%s: vm_gla2gpa unknown error %d", __func__, error));
589 case VM_ACTIVATE_CPU:
590 vac = (struct vm_activate_cpu *)data;
591 error = vm_activate_cpu(sc->vm, vac->vcpuid);
595 vm_cpuset = (struct vm_cpuset *)data;
596 size = vm_cpuset->cpusetsize;
597 if (size < sizeof(cpuset_t) || size > CPU_MAXSIZE / NBBY) {
601 cpuset = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
602 if (vm_cpuset->which == VM_ACTIVE_CPUS)
603 *cpuset = vm_active_cpus(sc->vm);
604 else if (vm_cpuset->which == VM_SUSPENDED_CPUS)
605 *cpuset = vm_suspended_cpus(sc->vm);
609 error = copyout(cpuset, vm_cpuset->cpus, size);
610 free(cpuset, M_TEMP);
613 vmii = (struct vm_intinfo *)data;
614 error = vm_exit_intinfo(sc->vm, vmii->vcpuid, vmii->info1);
617 vmii = (struct vm_intinfo *)data;
618 error = vm_get_intinfo(sc->vm, vmii->vcpuid, &vmii->info1,
622 rtcdata = (struct vm_rtc_data *)data;
623 error = vrtc_nvram_write(sc->vm, rtcdata->offset,
627 rtcdata = (struct vm_rtc_data *)data;
628 error = vrtc_nvram_read(sc->vm, rtcdata->offset,
632 rtctime = (struct vm_rtc_time *)data;
633 error = vrtc_set_time(sc->vm, rtctime->secs);
637 rtctime = (struct vm_rtc_time *)data;
638 rtctime->secs = vrtc_get_time(sc->vm);
640 case VM_RESTART_INSTRUCTION:
641 error = vm_restart_instruction(sc->vm, vcpu);
648 if (state_changed == 1)
649 vcpu_unlock_one(sc, vcpu);
650 else if (state_changed == 2)
654 /* Make sure that no handler returns a bogus value like ERESTART */
655 KASSERT(error >= 0, ("vmmdev_ioctl: invalid error return %d", error));
660 vmmdev_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t mapsize,
661 struct vm_object **objp, int nprot)
663 struct vmmdev_softc *sc;
666 vm_ooffset_t segoff, first, last;
667 int error, found, segid;
671 last = first + mapsize;
672 if ((nprot & PROT_EXEC) || first < 0 || first >= last)
675 sc = vmmdev_lookup2(cdev);
677 /* virtual machine is in the process of being created */
682 * Get a read lock on the guest memory map by freezing any vcpu.
684 error = vcpu_lock_one(sc, VM_MAXCPU - 1);
691 error = vm_mmap_getnext(sc->vm, &gpa, &segid, &segoff, &len,
696 if (first >= gpa && last <= gpa + len)
703 error = vm_get_memseg(sc->vm, segid, &len, &sysmem, objp);
704 KASSERT(error == 0 && *objp != NULL,
705 ("%s: invalid memory segment %d", __func__, segid));
707 vm_object_reference(*objp);
708 *offset = segoff + (first - gpa);
713 vcpu_unlock_one(sc, VM_MAXCPU - 1);
718 vmmdev_destroy(void *arg)
720 struct vmmdev_softc *sc = arg;
721 struct devmem_softc *dsc;
724 error = vcpu_lock_all(sc);
725 KASSERT(error == 0, ("%s: error %d freezing vcpus", __func__, error));
727 while ((dsc = SLIST_FIRST(&sc->devmem)) != NULL) {
728 KASSERT(dsc->cdev == NULL, ("%s: devmem not free", __func__));
729 SLIST_REMOVE_HEAD(&sc->devmem, link);
730 free(dsc->name, M_VMMDEV);
734 if (sc->cdev != NULL)
735 destroy_dev(sc->cdev);
740 if ((sc->flags & VSC_LINKED) != 0) {
741 mtx_lock(&vmmdev_mtx);
742 SLIST_REMOVE(&head, sc, vmmdev_softc, link);
743 mtx_unlock(&vmmdev_mtx);
750 sysctl_vmm_destroy(SYSCTL_HANDLER_ARGS)
753 char buf[VM_MAX_NAMELEN];
754 struct devmem_softc *dsc;
755 struct vmmdev_softc *sc;
758 strlcpy(buf, "beavis", sizeof(buf));
759 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
760 if (error != 0 || req->newptr == NULL)
763 mtx_lock(&vmmdev_mtx);
764 sc = vmmdev_lookup(buf);
765 if (sc == NULL || sc->cdev == NULL) {
766 mtx_unlock(&vmmdev_mtx);
771 * The 'cdev' will be destroyed asynchronously when 'si_threadcount'
772 * goes down to 0 so we should not do it again in the callback.
774 * Setting 'sc->cdev' to NULL is also used to indicate that the VM
775 * is scheduled for destruction.
779 mtx_unlock(&vmmdev_mtx);
782 * Schedule all cdevs to be destroyed:
784 * - any new operations on the 'cdev' will return an error (ENXIO).
786 * - when the 'si_threadcount' dwindles down to zero the 'cdev' will
787 * be destroyed and the callback will be invoked in a taskqueue
790 * - the 'devmem' cdevs are destroyed before the virtual machine 'cdev'
792 SLIST_FOREACH(dsc, &sc->devmem, link) {
793 KASSERT(dsc->cdev != NULL, ("devmem cdev already destroyed"));
794 destroy_dev_sched_cb(dsc->cdev, devmem_destroy, dsc);
796 destroy_dev_sched_cb(cdev, vmmdev_destroy, sc);
799 SYSCTL_PROC(_hw_vmm, OID_AUTO, destroy, CTLTYPE_STRING | CTLFLAG_RW,
800 NULL, 0, sysctl_vmm_destroy, "A", NULL);
802 static struct cdevsw vmmdevsw = {
804 .d_version = D_VERSION,
805 .d_ioctl = vmmdev_ioctl,
806 .d_mmap_single = vmmdev_mmap_single,
808 .d_write = vmmdev_rw,
812 sysctl_vmm_create(SYSCTL_HANDLER_ARGS)
817 struct vmmdev_softc *sc, *sc2;
818 char buf[VM_MAX_NAMELEN];
820 strlcpy(buf, "beavis", sizeof(buf));
821 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
822 if (error != 0 || req->newptr == NULL)
825 mtx_lock(&vmmdev_mtx);
826 sc = vmmdev_lookup(buf);
827 mtx_unlock(&vmmdev_mtx);
831 error = vm_create(buf, &vm);
835 sc = malloc(sizeof(struct vmmdev_softc), M_VMMDEV, M_WAITOK | M_ZERO);
837 SLIST_INIT(&sc->devmem);
840 * Lookup the name again just in case somebody sneaked in when we
843 mtx_lock(&vmmdev_mtx);
844 sc2 = vmmdev_lookup(buf);
846 SLIST_INSERT_HEAD(&head, sc, link);
847 sc->flags |= VSC_LINKED;
849 mtx_unlock(&vmmdev_mtx);
856 error = make_dev_p(MAKEDEV_CHECKNAME, &cdev, &vmmdevsw, NULL,
857 UID_ROOT, GID_WHEEL, 0600, "vmm/%s", buf);
863 mtx_lock(&vmmdev_mtx);
865 sc->cdev->si_drv1 = sc;
866 mtx_unlock(&vmmdev_mtx);
870 SYSCTL_PROC(_hw_vmm, OID_AUTO, create, CTLTYPE_STRING | CTLFLAG_RW,
871 NULL, 0, sysctl_vmm_create, "A", NULL);
876 mtx_init(&vmmdev_mtx, "vmm device mutex", NULL, MTX_DEF);
884 if (SLIST_EMPTY(&head))
893 devmem_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t len,
894 struct vm_object **objp, int nprot)
896 struct devmem_softc *dsc;
897 vm_ooffset_t first, last;
904 /* 'cdev' has been created but is not ready for use */
909 last = *offset + len;
910 if ((nprot & PROT_EXEC) || first < 0 || first >= last)
913 error = vcpu_lock_one(dsc->sc, VM_MAXCPU - 1);
917 error = vm_get_memseg(dsc->sc->vm, dsc->segid, &seglen, &sysmem, objp);
918 KASSERT(error == 0 && !sysmem && *objp != NULL,
919 ("%s: invalid devmem segment %d", __func__, dsc->segid));
921 vcpu_unlock_one(dsc->sc, VM_MAXCPU - 1);
923 if (seglen >= last) {
924 vm_object_reference(*objp);
931 static struct cdevsw devmemsw = {
933 .d_version = D_VERSION,
934 .d_mmap_single = devmem_mmap_single,
938 devmem_create_cdev(const char *vmname, int segid, char *devname)
940 struct devmem_softc *dsc;
941 struct vmmdev_softc *sc;
945 error = make_dev_p(MAKEDEV_CHECKNAME, &cdev, &devmemsw, NULL,
946 UID_ROOT, GID_WHEEL, 0600, "vmm.io/%s.%s", vmname, devname);
950 dsc = malloc(sizeof(struct devmem_softc), M_VMMDEV, M_WAITOK | M_ZERO);
952 mtx_lock(&vmmdev_mtx);
953 sc = vmmdev_lookup(vmname);
954 KASSERT(sc != NULL, ("%s: vm %s softc not found", __func__, vmname));
955 if (sc->cdev == NULL) {
956 /* virtual machine is being created or destroyed */
957 mtx_unlock(&vmmdev_mtx);
959 destroy_dev_sched_cb(cdev, NULL, 0);
967 SLIST_INSERT_HEAD(&sc->devmem, dsc, link);
968 mtx_unlock(&vmmdev_mtx);
970 /* The 'cdev' is ready for use after 'si_drv1' is initialized */
976 devmem_destroy(void *arg)
978 struct devmem_softc *dsc = arg;
980 KASSERT(dsc->cdev, ("%s: devmem cdev already destroyed", __func__));