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 vmmdev_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
288 int error, vcpu, state_changed, size;
290 struct vmmdev_softc *sc;
291 struct vm_register *vmreg;
292 struct vm_seg_desc *vmsegdesc;
293 struct vm_run *vmrun;
294 struct vm_exception *vmexc;
295 struct vm_lapic_irq *vmirq;
296 struct vm_lapic_msi *vmmsi;
297 struct vm_ioapic_irq *ioapic_irq;
298 struct vm_isa_irq *isa_irq;
299 struct vm_isa_irq_trigger *isa_irq_trigger;
300 struct vm_capability *vmcap;
301 struct vm_pptdev *pptdev;
302 struct vm_pptdev_mmio *pptmmio;
303 struct vm_pptdev_msi *pptmsi;
304 struct vm_pptdev_msix *pptmsix;
305 struct vm_nmi *vmnmi;
306 struct vm_stats *vmstats;
307 struct vm_stat_desc *statdesc;
308 struct vm_x2apic *x2apic;
309 struct vm_gpa_pte *gpapte;
310 struct vm_suspend *vmsuspend;
311 struct vm_gla2gpa *gg;
312 struct vm_activate_cpu *vac;
313 struct vm_cpuset *vm_cpuset;
314 struct vm_intinfo *vmii;
315 struct vm_rtc_time *rtctime;
316 struct vm_rtc_data *rtcdata;
317 struct vm_memmap *mm;
319 sc = vmmdev_lookup2(cdev);
328 * Some VMM ioctls can operate only on vcpus that are not running.
332 case VM_GET_REGISTER:
333 case VM_SET_REGISTER:
334 case VM_GET_SEGMENT_DESCRIPTOR:
335 case VM_SET_SEGMENT_DESCRIPTOR:
336 case VM_INJECT_EXCEPTION:
337 case VM_GET_CAPABILITY:
338 case VM_SET_CAPABILITY:
341 case VM_SET_X2APIC_STATE:
343 case VM_ACTIVATE_CPU:
346 case VM_RESTART_INSTRUCTION:
348 * XXX fragile, handle with care
349 * Assumes that the first field of the ioctl data is the vcpu.
352 error = vcpu_lock_one(sc, vcpu);
358 case VM_MAP_PPTDEV_MMIO:
360 case VM_UNBIND_PPTDEV:
361 case VM_ALLOC_MEMSEG:
365 * ioctls that operate on the entire virtual machine must
366 * prevent all vcpus from running.
368 error = vcpu_lock_all(sc);
375 case VM_MMAP_GETNEXT:
377 * Lock a vcpu to make sure that the memory map cannot be
378 * modified while it is being inspected.
380 vcpu = VM_MAXCPU - 1;
381 error = vcpu_lock_one(sc, vcpu);
393 vmrun = (struct vm_run *)data;
394 error = vm_run(sc->vm, vmrun);
397 vmsuspend = (struct vm_suspend *)data;
398 error = vm_suspend(sc->vm, vmsuspend->how);
401 error = vm_reinit(sc->vm);
404 statdesc = (struct vm_stat_desc *)data;
405 error = vmm_stat_desc_copy(statdesc->index,
406 statdesc->desc, sizeof(statdesc->desc));
410 CTASSERT(MAX_VM_STATS >= MAX_VMM_STAT_ELEMS);
411 vmstats = (struct vm_stats *)data;
412 getmicrotime(&vmstats->tv);
413 error = vmm_stat_copy(sc->vm, vmstats->cpuid,
414 &vmstats->num_entries, vmstats->statbuf);
418 pptmsi = (struct vm_pptdev_msi *)data;
419 error = ppt_setup_msi(sc->vm, pptmsi->vcpu,
420 pptmsi->bus, pptmsi->slot, pptmsi->func,
421 pptmsi->addr, pptmsi->msg,
425 pptmsix = (struct vm_pptdev_msix *)data;
426 error = ppt_setup_msix(sc->vm, pptmsix->vcpu,
427 pptmsix->bus, pptmsix->slot,
428 pptmsix->func, pptmsix->idx,
429 pptmsix->addr, pptmsix->msg,
430 pptmsix->vector_control);
432 case VM_MAP_PPTDEV_MMIO:
433 pptmmio = (struct vm_pptdev_mmio *)data;
434 error = ppt_map_mmio(sc->vm, pptmmio->bus, pptmmio->slot,
435 pptmmio->func, pptmmio->gpa, pptmmio->len,
439 pptdev = (struct vm_pptdev *)data;
440 error = vm_assign_pptdev(sc->vm, pptdev->bus, pptdev->slot,
443 case VM_UNBIND_PPTDEV:
444 pptdev = (struct vm_pptdev *)data;
445 error = vm_unassign_pptdev(sc->vm, pptdev->bus, pptdev->slot,
448 case VM_INJECT_EXCEPTION:
449 vmexc = (struct vm_exception *)data;
450 error = vm_inject_exception(sc->vm, vmexc->cpuid,
451 vmexc->vector, vmexc->error_code_valid, vmexc->error_code,
452 vmexc->restart_instruction);
455 vmnmi = (struct vm_nmi *)data;
456 error = vm_inject_nmi(sc->vm, vmnmi->cpuid);
459 vmirq = (struct vm_lapic_irq *)data;
460 error = lapic_intr_edge(sc->vm, vmirq->cpuid, vmirq->vector);
462 case VM_LAPIC_LOCAL_IRQ:
463 vmirq = (struct vm_lapic_irq *)data;
464 error = lapic_set_local_intr(sc->vm, vmirq->cpuid,
468 vmmsi = (struct vm_lapic_msi *)data;
469 error = lapic_intr_msi(sc->vm, vmmsi->addr, vmmsi->msg);
471 case VM_IOAPIC_ASSERT_IRQ:
472 ioapic_irq = (struct vm_ioapic_irq *)data;
473 error = vioapic_assert_irq(sc->vm, ioapic_irq->irq);
475 case VM_IOAPIC_DEASSERT_IRQ:
476 ioapic_irq = (struct vm_ioapic_irq *)data;
477 error = vioapic_deassert_irq(sc->vm, ioapic_irq->irq);
479 case VM_IOAPIC_PULSE_IRQ:
480 ioapic_irq = (struct vm_ioapic_irq *)data;
481 error = vioapic_pulse_irq(sc->vm, ioapic_irq->irq);
483 case VM_IOAPIC_PINCOUNT:
484 *(int *)data = vioapic_pincount(sc->vm);
486 case VM_ISA_ASSERT_IRQ:
487 isa_irq = (struct vm_isa_irq *)data;
488 error = vatpic_assert_irq(sc->vm, isa_irq->atpic_irq);
489 if (error == 0 && isa_irq->ioapic_irq != -1)
490 error = vioapic_assert_irq(sc->vm,
491 isa_irq->ioapic_irq);
493 case VM_ISA_DEASSERT_IRQ:
494 isa_irq = (struct vm_isa_irq *)data;
495 error = vatpic_deassert_irq(sc->vm, isa_irq->atpic_irq);
496 if (error == 0 && isa_irq->ioapic_irq != -1)
497 error = vioapic_deassert_irq(sc->vm,
498 isa_irq->ioapic_irq);
500 case VM_ISA_PULSE_IRQ:
501 isa_irq = (struct vm_isa_irq *)data;
502 error = vatpic_pulse_irq(sc->vm, isa_irq->atpic_irq);
503 if (error == 0 && isa_irq->ioapic_irq != -1)
504 error = vioapic_pulse_irq(sc->vm, isa_irq->ioapic_irq);
506 case VM_ISA_SET_IRQ_TRIGGER:
507 isa_irq_trigger = (struct vm_isa_irq_trigger *)data;
508 error = vatpic_set_irq_trigger(sc->vm,
509 isa_irq_trigger->atpic_irq, isa_irq_trigger->trigger);
511 case VM_MMAP_GETNEXT:
512 mm = (struct vm_memmap *)data;
513 error = vm_mmap_getnext(sc->vm, &mm->gpa, &mm->segid,
514 &mm->segoff, &mm->len, &mm->prot, &mm->flags);
517 mm = (struct vm_memmap *)data;
518 error = vm_mmap_memseg(sc->vm, mm->gpa, mm->segid, mm->segoff,
519 mm->len, mm->prot, mm->flags);
521 case VM_ALLOC_MEMSEG:
522 error = alloc_memseg(sc, (struct vm_memseg *)data);
525 error = get_memseg(sc, (struct vm_memseg *)data);
527 case VM_GET_REGISTER:
528 vmreg = (struct vm_register *)data;
529 error = vm_get_register(sc->vm, vmreg->cpuid, vmreg->regnum,
532 case VM_SET_REGISTER:
533 vmreg = (struct vm_register *)data;
534 error = vm_set_register(sc->vm, vmreg->cpuid, vmreg->regnum,
537 case VM_SET_SEGMENT_DESCRIPTOR:
538 vmsegdesc = (struct vm_seg_desc *)data;
539 error = vm_set_seg_desc(sc->vm, vmsegdesc->cpuid,
543 case VM_GET_SEGMENT_DESCRIPTOR:
544 vmsegdesc = (struct vm_seg_desc *)data;
545 error = vm_get_seg_desc(sc->vm, vmsegdesc->cpuid,
549 case VM_GET_CAPABILITY:
550 vmcap = (struct vm_capability *)data;
551 error = vm_get_capability(sc->vm, vmcap->cpuid,
555 case VM_SET_CAPABILITY:
556 vmcap = (struct vm_capability *)data;
557 error = vm_set_capability(sc->vm, vmcap->cpuid,
561 case VM_SET_X2APIC_STATE:
562 x2apic = (struct vm_x2apic *)data;
563 error = vm_set_x2apic_state(sc->vm,
564 x2apic->cpuid, x2apic->state);
566 case VM_GET_X2APIC_STATE:
567 x2apic = (struct vm_x2apic *)data;
568 error = vm_get_x2apic_state(sc->vm,
569 x2apic->cpuid, &x2apic->state);
571 case VM_GET_GPA_PMAP:
572 gpapte = (struct vm_gpa_pte *)data;
573 pmap_get_mapping(vmspace_pmap(vm_get_vmspace(sc->vm)),
574 gpapte->gpa, gpapte->pte, &gpapte->ptenum);
577 case VM_GET_HPET_CAPABILITIES:
578 error = vhpet_getcap((struct vm_hpet_cap *)data);
581 CTASSERT(PROT_READ == VM_PROT_READ);
582 CTASSERT(PROT_WRITE == VM_PROT_WRITE);
583 CTASSERT(PROT_EXEC == VM_PROT_EXECUTE);
584 gg = (struct vm_gla2gpa *)data;
585 error = vm_gla2gpa(sc->vm, gg->vcpuid, &gg->paging, gg->gla,
586 gg->prot, &gg->gpa, &gg->fault);
587 KASSERT(error == 0 || error == EFAULT,
588 ("%s: vm_gla2gpa unknown error %d", __func__, error));
591 case VM_ACTIVATE_CPU:
592 vac = (struct vm_activate_cpu *)data;
593 error = vm_activate_cpu(sc->vm, vac->vcpuid);
597 vm_cpuset = (struct vm_cpuset *)data;
598 size = vm_cpuset->cpusetsize;
599 if (size < sizeof(cpuset_t) || size > CPU_MAXSIZE / NBBY) {
603 cpuset = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
604 if (vm_cpuset->which == VM_ACTIVE_CPUS)
605 *cpuset = vm_active_cpus(sc->vm);
606 else if (vm_cpuset->which == VM_SUSPENDED_CPUS)
607 *cpuset = vm_suspended_cpus(sc->vm);
611 error = copyout(cpuset, vm_cpuset->cpus, size);
612 free(cpuset, M_TEMP);
615 vmii = (struct vm_intinfo *)data;
616 error = vm_exit_intinfo(sc->vm, vmii->vcpuid, vmii->info1);
619 vmii = (struct vm_intinfo *)data;
620 error = vm_get_intinfo(sc->vm, vmii->vcpuid, &vmii->info1,
624 rtcdata = (struct vm_rtc_data *)data;
625 error = vrtc_nvram_write(sc->vm, rtcdata->offset,
629 rtcdata = (struct vm_rtc_data *)data;
630 error = vrtc_nvram_read(sc->vm, rtcdata->offset,
634 rtctime = (struct vm_rtc_time *)data;
635 error = vrtc_set_time(sc->vm, rtctime->secs);
639 rtctime = (struct vm_rtc_time *)data;
640 rtctime->secs = vrtc_get_time(sc->vm);
642 case VM_RESTART_INSTRUCTION:
643 error = vm_restart_instruction(sc->vm, vcpu);
650 if (state_changed == 1)
651 vcpu_unlock_one(sc, vcpu);
652 else if (state_changed == 2)
656 /* Make sure that no handler returns a bogus value like ERESTART */
657 KASSERT(error >= 0, ("vmmdev_ioctl: invalid error return %d", error));
662 vmmdev_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t mapsize,
663 struct vm_object **objp, int nprot)
665 struct vmmdev_softc *sc;
668 vm_ooffset_t segoff, first, last;
669 int error, found, segid;
673 last = first + mapsize;
674 if ((nprot & PROT_EXEC) || first < 0 || first >= last)
677 sc = vmmdev_lookup2(cdev);
679 /* virtual machine is in the process of being created */
684 * Get a read lock on the guest memory map by freezing any vcpu.
686 error = vcpu_lock_one(sc, VM_MAXCPU - 1);
693 error = vm_mmap_getnext(sc->vm, &gpa, &segid, &segoff, &len,
698 if (first >= gpa && last <= gpa + len)
705 error = vm_get_memseg(sc->vm, segid, &len, &sysmem, objp);
706 KASSERT(error == 0 && *objp != NULL,
707 ("%s: invalid memory segment %d", __func__, segid));
709 vm_object_reference(*objp);
710 *offset = segoff + (first - gpa);
715 vcpu_unlock_one(sc, VM_MAXCPU - 1);
720 vmmdev_destroy(void *arg)
722 struct vmmdev_softc *sc = arg;
723 struct devmem_softc *dsc;
726 error = vcpu_lock_all(sc);
727 KASSERT(error == 0, ("%s: error %d freezing vcpus", __func__, error));
729 while ((dsc = SLIST_FIRST(&sc->devmem)) != NULL) {
730 KASSERT(dsc->cdev == NULL, ("%s: devmem not free", __func__));
731 SLIST_REMOVE_HEAD(&sc->devmem, link);
732 free(dsc->name, M_VMMDEV);
736 if (sc->cdev != NULL)
737 destroy_dev(sc->cdev);
742 if ((sc->flags & VSC_LINKED) != 0) {
743 mtx_lock(&vmmdev_mtx);
744 SLIST_REMOVE(&head, sc, vmmdev_softc, link);
745 mtx_unlock(&vmmdev_mtx);
752 sysctl_vmm_destroy(SYSCTL_HANDLER_ARGS)
755 char buf[VM_MAX_NAMELEN];
756 struct devmem_softc *dsc;
757 struct vmmdev_softc *sc;
760 strlcpy(buf, "beavis", sizeof(buf));
761 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
762 if (error != 0 || req->newptr == NULL)
765 mtx_lock(&vmmdev_mtx);
766 sc = vmmdev_lookup(buf);
767 if (sc == NULL || sc->cdev == NULL) {
768 mtx_unlock(&vmmdev_mtx);
773 * The 'cdev' will be destroyed asynchronously when 'si_threadcount'
774 * goes down to 0 so we should not do it again in the callback.
776 * Setting 'sc->cdev' to NULL is also used to indicate that the VM
777 * is scheduled for destruction.
781 mtx_unlock(&vmmdev_mtx);
784 * Schedule all cdevs to be destroyed:
786 * - any new operations on the 'cdev' will return an error (ENXIO).
788 * - when the 'si_threadcount' dwindles down to zero the 'cdev' will
789 * be destroyed and the callback will be invoked in a taskqueue
792 * - the 'devmem' cdevs are destroyed before the virtual machine 'cdev'
794 SLIST_FOREACH(dsc, &sc->devmem, link) {
795 KASSERT(dsc->cdev != NULL, ("devmem cdev already destroyed"));
796 destroy_dev_sched_cb(dsc->cdev, devmem_destroy, dsc);
798 destroy_dev_sched_cb(cdev, vmmdev_destroy, sc);
801 SYSCTL_PROC(_hw_vmm, OID_AUTO, destroy, CTLTYPE_STRING | CTLFLAG_RW,
802 NULL, 0, sysctl_vmm_destroy, "A", NULL);
804 static struct cdevsw vmmdevsw = {
806 .d_version = D_VERSION,
807 .d_ioctl = vmmdev_ioctl,
808 .d_mmap_single = vmmdev_mmap_single,
810 .d_write = vmmdev_rw,
814 sysctl_vmm_create(SYSCTL_HANDLER_ARGS)
819 struct vmmdev_softc *sc, *sc2;
820 char buf[VM_MAX_NAMELEN];
822 strlcpy(buf, "beavis", sizeof(buf));
823 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
824 if (error != 0 || req->newptr == NULL)
827 mtx_lock(&vmmdev_mtx);
828 sc = vmmdev_lookup(buf);
829 mtx_unlock(&vmmdev_mtx);
833 error = vm_create(buf, &vm);
837 sc = malloc(sizeof(struct vmmdev_softc), M_VMMDEV, M_WAITOK | M_ZERO);
839 SLIST_INIT(&sc->devmem);
842 * Lookup the name again just in case somebody sneaked in when we
845 mtx_lock(&vmmdev_mtx);
846 sc2 = vmmdev_lookup(buf);
848 SLIST_INSERT_HEAD(&head, sc, link);
849 sc->flags |= VSC_LINKED;
851 mtx_unlock(&vmmdev_mtx);
858 error = make_dev_p(MAKEDEV_CHECKNAME, &cdev, &vmmdevsw, NULL,
859 UID_ROOT, GID_WHEEL, 0600, "vmm/%s", buf);
865 mtx_lock(&vmmdev_mtx);
867 sc->cdev->si_drv1 = sc;
868 mtx_unlock(&vmmdev_mtx);
872 SYSCTL_PROC(_hw_vmm, OID_AUTO, create, CTLTYPE_STRING | CTLFLAG_RW,
873 NULL, 0, sysctl_vmm_create, "A", NULL);
878 mtx_init(&vmmdev_mtx, "vmm device mutex", NULL, MTX_DEF);
886 if (SLIST_EMPTY(&head))
895 devmem_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t len,
896 struct vm_object **objp, int nprot)
898 struct devmem_softc *dsc;
899 vm_ooffset_t first, last;
906 /* 'cdev' has been created but is not ready for use */
911 last = *offset + len;
912 if ((nprot & PROT_EXEC) || first < 0 || first >= last)
915 error = vcpu_lock_one(dsc->sc, VM_MAXCPU - 1);
919 error = vm_get_memseg(dsc->sc->vm, dsc->segid, &seglen, &sysmem, objp);
920 KASSERT(error == 0 && !sysmem && *objp != NULL,
921 ("%s: invalid devmem segment %d", __func__, dsc->segid));
923 vcpu_unlock_one(dsc->sc, VM_MAXCPU - 1);
925 if (seglen >= last) {
926 vm_object_reference(*objp);
933 static struct cdevsw devmemsw = {
935 .d_version = D_VERSION,
936 .d_mmap_single = devmem_mmap_single,
940 devmem_create_cdev(const char *vmname, int segid, char *devname)
942 struct devmem_softc *dsc;
943 struct vmmdev_softc *sc;
947 error = make_dev_p(MAKEDEV_CHECKNAME, &cdev, &devmemsw, NULL,
948 UID_ROOT, GID_WHEEL, 0600, "vmm.io/%s.%s", vmname, devname);
952 dsc = malloc(sizeof(struct devmem_softc), M_VMMDEV, M_WAITOK | M_ZERO);
954 mtx_lock(&vmmdev_mtx);
955 sc = vmmdev_lookup(vmname);
956 KASSERT(sc != NULL, ("%s: vm %s softc not found", __func__, vmname));
957 if (sc->cdev == NULL) {
958 /* virtual machine is being created or destroyed */
959 mtx_unlock(&vmmdev_mtx);
961 destroy_dev_sched_cb(cdev, NULL, 0);
969 SLIST_INSERT_HEAD(&sc->devmem, dsc, link);
970 mtx_unlock(&vmmdev_mtx);
972 /* The 'cdev' is ready for use after 'si_drv1' is initialized */
978 devmem_destroy(void *arg)
980 struct devmem_softc *dsc = arg;
982 KASSERT(dsc->cdev, ("%s: devmem cdev already destroyed", __func__));