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>
49 #include <machine/vmparam.h>
50 #include <machine/vmm.h>
51 #include <machine/vmm_dev.h>
53 #include "vmm_lapic.h"
57 #include "io/vioapic.h"
61 struct vm *vm; /* vm instance cookie */
63 SLIST_ENTRY(vmmdev_softc) link;
66 #define VSC_LINKED 0x01
68 static SLIST_HEAD(, vmmdev_softc) head;
70 static struct mtx vmmdev_mtx;
72 static MALLOC_DEFINE(M_VMMDEV, "vmmdev", "vmmdev");
76 static struct vmmdev_softc *
77 vmmdev_lookup(const char *name)
79 struct vmmdev_softc *sc;
81 #ifdef notyet /* XXX kernel is not compiled with invariants */
82 mtx_assert(&vmmdev_mtx, MA_OWNED);
85 SLIST_FOREACH(sc, &head, link) {
86 if (strcmp(name, vm_name(sc->vm)) == 0)
93 static struct vmmdev_softc *
94 vmmdev_lookup2(struct cdev *cdev)
97 return (cdev->si_drv1);
101 vmmdev_rw(struct cdev *cdev, struct uio *uio, int flags)
103 int error, off, c, prot;
106 struct vmmdev_softc *sc;
108 static char zerobuf[PAGE_SIZE];
111 sc = vmmdev_lookup2(cdev);
115 prot = (uio->uio_rw == UIO_WRITE ? VM_PROT_WRITE : VM_PROT_READ);
116 while (uio->uio_resid > 0 && error == 0) {
117 gpa = uio->uio_offset;
118 off = gpa & PAGE_MASK;
119 c = min(uio->uio_resid, PAGE_SIZE - off);
122 * The VM has a hole in its physical memory map. If we want to
123 * use 'dd' to inspect memory beyond the hole we need to
124 * provide bogus data for memory that lies in the hole.
126 * Since this device does not support lseek(2), dd(1) will
127 * read(2) blocks of data to simulate the lseek(2).
129 hpa = vm_gpa_hold(sc->vm, gpa, c, prot, &cookie);
131 if (uio->uio_rw == UIO_READ)
132 error = uiomove(zerobuf, c, uio);
136 error = uiomove(hpa, c, uio);
137 vm_gpa_release(cookie);
144 vmmdev_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
147 int error, vcpu, state_changed;
148 struct vmmdev_softc *sc;
149 struct vm_memory_segment *seg;
150 struct vm_register *vmreg;
151 struct vm_seg_desc *vmsegdesc;
152 struct vm_run *vmrun;
153 struct vm_exception *vmexc;
154 struct vm_lapic_irq *vmirq;
155 struct vm_lapic_msi *vmmsi;
156 struct vm_ioapic_irq *ioapic_irq;
157 struct vm_capability *vmcap;
158 struct vm_pptdev *pptdev;
159 struct vm_pptdev_mmio *pptmmio;
160 struct vm_pptdev_msi *pptmsi;
161 struct vm_pptdev_msix *pptmsix;
162 struct vm_nmi *vmnmi;
163 struct vm_stats *vmstats;
164 struct vm_stat_desc *statdesc;
165 struct vm_x2apic *x2apic;
166 struct vm_gpa_pte *gpapte;
168 sc = vmmdev_lookup2(cdev);
176 * Some VMM ioctls can operate only on vcpus that are not running.
180 case VM_GET_REGISTER:
181 case VM_SET_REGISTER:
182 case VM_GET_SEGMENT_DESCRIPTOR:
183 case VM_SET_SEGMENT_DESCRIPTOR:
184 case VM_INJECT_EXCEPTION:
185 case VM_GET_CAPABILITY:
186 case VM_SET_CAPABILITY:
189 case VM_SET_X2APIC_STATE:
191 * XXX fragile, handle with care
192 * Assumes that the first field of the ioctl data is the vcpu.
195 if (vcpu < 0 || vcpu >= VM_MAXCPU) {
200 error = vcpu_set_state(sc->vm, vcpu, VCPU_FROZEN, true);
207 case VM_MAP_PPTDEV_MMIO:
209 case VM_UNBIND_PPTDEV:
212 * ioctls that operate on the entire virtual machine must
213 * prevent all vcpus from running.
216 for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++) {
217 error = vcpu_set_state(sc->vm, vcpu, VCPU_FROZEN, true);
224 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false);
237 vmrun = (struct vm_run *)data;
238 error = vm_run(sc->vm, vmrun);
241 statdesc = (struct vm_stat_desc *)data;
242 error = vmm_stat_desc_copy(statdesc->index,
243 statdesc->desc, sizeof(statdesc->desc));
247 CTASSERT(MAX_VM_STATS >= MAX_VMM_STAT_ELEMS);
248 vmstats = (struct vm_stats *)data;
249 getmicrotime(&vmstats->tv);
250 error = vmm_stat_copy(sc->vm, vmstats->cpuid,
251 &vmstats->num_entries, vmstats->statbuf);
255 pptmsi = (struct vm_pptdev_msi *)data;
256 error = ppt_setup_msi(sc->vm, pptmsi->vcpu,
257 pptmsi->bus, pptmsi->slot, pptmsi->func,
258 pptmsi->addr, pptmsi->msg,
262 pptmsix = (struct vm_pptdev_msix *)data;
263 error = ppt_setup_msix(sc->vm, pptmsix->vcpu,
264 pptmsix->bus, pptmsix->slot,
265 pptmsix->func, pptmsix->idx,
266 pptmsix->addr, pptmsix->msg,
267 pptmsix->vector_control);
269 case VM_MAP_PPTDEV_MMIO:
270 pptmmio = (struct vm_pptdev_mmio *)data;
271 error = ppt_map_mmio(sc->vm, pptmmio->bus, pptmmio->slot,
272 pptmmio->func, pptmmio->gpa, pptmmio->len,
276 pptdev = (struct vm_pptdev *)data;
277 error = vm_assign_pptdev(sc->vm, pptdev->bus, pptdev->slot,
280 case VM_UNBIND_PPTDEV:
281 pptdev = (struct vm_pptdev *)data;
282 error = vm_unassign_pptdev(sc->vm, pptdev->bus, pptdev->slot,
285 case VM_INJECT_EXCEPTION:
286 vmexc = (struct vm_exception *)data;
287 error = vm_inject_exception(sc->vm, vmexc->cpuid, vmexc);
290 vmnmi = (struct vm_nmi *)data;
291 error = vm_inject_nmi(sc->vm, vmnmi->cpuid);
294 vmirq = (struct vm_lapic_irq *)data;
295 error = lapic_intr_edge(sc->vm, vmirq->cpuid, vmirq->vector);
297 case VM_LAPIC_LOCAL_IRQ:
298 vmirq = (struct vm_lapic_irq *)data;
299 error = lapic_set_local_intr(sc->vm, vmirq->cpuid,
303 vmmsi = (struct vm_lapic_msi *)data;
304 error = lapic_intr_msi(sc->vm, vmmsi->addr, vmmsi->msg);
306 case VM_IOAPIC_ASSERT_IRQ:
307 ioapic_irq = (struct vm_ioapic_irq *)data;
308 error = vioapic_assert_irq(sc->vm, ioapic_irq->irq);
310 case VM_IOAPIC_DEASSERT_IRQ:
311 ioapic_irq = (struct vm_ioapic_irq *)data;
312 error = vioapic_deassert_irq(sc->vm, ioapic_irq->irq);
314 case VM_IOAPIC_PULSE_IRQ:
315 ioapic_irq = (struct vm_ioapic_irq *)data;
316 error = vioapic_pulse_irq(sc->vm, ioapic_irq->irq);
318 case VM_IOAPIC_PINCOUNT:
319 *(int *)data = vioapic_pincount(sc->vm);
322 seg = (struct vm_memory_segment *)data;
323 error = vm_malloc(sc->vm, seg->gpa, seg->len);
325 case VM_GET_MEMORY_SEG:
326 seg = (struct vm_memory_segment *)data;
328 (void)vm_gpabase2memseg(sc->vm, seg->gpa, seg);
331 case VM_GET_REGISTER:
332 vmreg = (struct vm_register *)data;
333 error = vm_get_register(sc->vm, vmreg->cpuid, vmreg->regnum,
336 case VM_SET_REGISTER:
337 vmreg = (struct vm_register *)data;
338 error = vm_set_register(sc->vm, vmreg->cpuid, vmreg->regnum,
341 case VM_SET_SEGMENT_DESCRIPTOR:
342 vmsegdesc = (struct vm_seg_desc *)data;
343 error = vm_set_seg_desc(sc->vm, vmsegdesc->cpuid,
347 case VM_GET_SEGMENT_DESCRIPTOR:
348 vmsegdesc = (struct vm_seg_desc *)data;
349 error = vm_get_seg_desc(sc->vm, vmsegdesc->cpuid,
353 case VM_GET_CAPABILITY:
354 vmcap = (struct vm_capability *)data;
355 error = vm_get_capability(sc->vm, vmcap->cpuid,
359 case VM_SET_CAPABILITY:
360 vmcap = (struct vm_capability *)data;
361 error = vm_set_capability(sc->vm, vmcap->cpuid,
365 case VM_SET_X2APIC_STATE:
366 x2apic = (struct vm_x2apic *)data;
367 error = vm_set_x2apic_state(sc->vm,
368 x2apic->cpuid, x2apic->state);
370 case VM_GET_X2APIC_STATE:
371 x2apic = (struct vm_x2apic *)data;
372 error = vm_get_x2apic_state(sc->vm,
373 x2apic->cpuid, &x2apic->state);
375 case VM_GET_GPA_PMAP:
376 gpapte = (struct vm_gpa_pte *)data;
377 pmap_get_mapping(vmspace_pmap(vm_get_vmspace(sc->vm)),
378 gpapte->gpa, gpapte->pte, &gpapte->ptenum);
381 case VM_GET_HPET_CAPABILITIES:
382 error = vhpet_getcap((struct vm_hpet_cap *)data);
389 if (state_changed == 1) {
390 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false);
391 } else if (state_changed == 2) {
392 for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++)
393 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false);
397 /* Make sure that no handler returns a bogus value like ERESTART */
398 KASSERT(error >= 0, ("vmmdev_ioctl: invalid error return %d", error));
403 vmmdev_mmap_single(struct cdev *cdev, vm_ooffset_t *offset,
404 vm_size_t size, struct vm_object **object, int nprot)
407 struct vmmdev_softc *sc;
409 sc = vmmdev_lookup2(cdev);
410 if (sc != NULL && (nprot & PROT_EXEC) == 0)
411 error = vm_get_memobj(sc->vm, *offset, size, offset, object);
419 vmmdev_destroy(void *arg)
422 struct vmmdev_softc *sc = arg;
424 if (sc->cdev != NULL)
425 destroy_dev(sc->cdev);
430 if ((sc->flags & VSC_LINKED) != 0) {
431 mtx_lock(&vmmdev_mtx);
432 SLIST_REMOVE(&head, sc, vmmdev_softc, link);
433 mtx_unlock(&vmmdev_mtx);
440 sysctl_vmm_destroy(SYSCTL_HANDLER_ARGS)
443 char buf[VM_MAX_NAMELEN];
444 struct vmmdev_softc *sc;
447 strlcpy(buf, "beavis", sizeof(buf));
448 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
449 if (error != 0 || req->newptr == NULL)
452 mtx_lock(&vmmdev_mtx);
453 sc = vmmdev_lookup(buf);
454 if (sc == NULL || sc->cdev == NULL) {
455 mtx_unlock(&vmmdev_mtx);
460 * The 'cdev' will be destroyed asynchronously when 'si_threadcount'
461 * goes down to 0 so we should not do it again in the callback.
465 mtx_unlock(&vmmdev_mtx);
468 * Schedule the 'cdev' to be destroyed:
470 * - any new operations on this 'cdev' will return an error (ENXIO).
472 * - when the 'si_threadcount' dwindles down to zero the 'cdev' will
473 * be destroyed and the callback will be invoked in a taskqueue
476 destroy_dev_sched_cb(cdev, vmmdev_destroy, sc);
480 SYSCTL_PROC(_hw_vmm, OID_AUTO, destroy, CTLTYPE_STRING | CTLFLAG_RW,
481 NULL, 0, sysctl_vmm_destroy, "A", NULL);
483 static struct cdevsw vmmdevsw = {
485 .d_version = D_VERSION,
486 .d_ioctl = vmmdev_ioctl,
487 .d_mmap_single = vmmdev_mmap_single,
489 .d_write = vmmdev_rw,
493 sysctl_vmm_create(SYSCTL_HANDLER_ARGS)
498 struct vmmdev_softc *sc, *sc2;
499 char buf[VM_MAX_NAMELEN];
501 strlcpy(buf, "beavis", sizeof(buf));
502 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
503 if (error != 0 || req->newptr == NULL)
506 mtx_lock(&vmmdev_mtx);
507 sc = vmmdev_lookup(buf);
508 mtx_unlock(&vmmdev_mtx);
512 error = vm_create(buf, &vm);
516 sc = malloc(sizeof(struct vmmdev_softc), M_VMMDEV, M_WAITOK | M_ZERO);
520 * Lookup the name again just in case somebody sneaked in when we
523 mtx_lock(&vmmdev_mtx);
524 sc2 = vmmdev_lookup(buf);
526 SLIST_INSERT_HEAD(&head, sc, link);
527 sc->flags |= VSC_LINKED;
529 mtx_unlock(&vmmdev_mtx);
536 error = make_dev_p(MAKEDEV_CHECKNAME, &cdev, &vmmdevsw, NULL,
537 UID_ROOT, GID_WHEEL, 0600, "vmm/%s", buf);
543 mtx_lock(&vmmdev_mtx);
545 sc->cdev->si_drv1 = sc;
546 mtx_unlock(&vmmdev_mtx);
550 SYSCTL_PROC(_hw_vmm, OID_AUTO, create, CTLTYPE_STRING | CTLFLAG_RW,
551 NULL, 0, sysctl_vmm_create, "A", NULL);
556 mtx_init(&vmmdev_mtx, "vmm device mutex", NULL, MTX_DEF);
564 if (SLIST_EMPTY(&head))