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_instruction_emul.h>
52 #include <machine/vmm_dev.h>
54 #include "vmm_lapic.h"
58 #include "io/vatpic.h"
59 #include "io/vioapic.h"
63 struct vm *vm; /* vm instance cookie */
65 SLIST_ENTRY(vmmdev_softc) link;
68 #define VSC_LINKED 0x01
70 static SLIST_HEAD(, vmmdev_softc) head;
72 static struct mtx vmmdev_mtx;
74 static MALLOC_DEFINE(M_VMMDEV, "vmmdev", "vmmdev");
78 static struct vmmdev_softc *
79 vmmdev_lookup(const char *name)
81 struct vmmdev_softc *sc;
83 #ifdef notyet /* XXX kernel is not compiled with invariants */
84 mtx_assert(&vmmdev_mtx, MA_OWNED);
87 SLIST_FOREACH(sc, &head, link) {
88 if (strcmp(name, vm_name(sc->vm)) == 0)
95 static struct vmmdev_softc *
96 vmmdev_lookup2(struct cdev *cdev)
99 return (cdev->si_drv1);
103 vmmdev_rw(struct cdev *cdev, struct uio *uio, int flags)
105 int error, off, c, prot;
108 struct vmmdev_softc *sc;
110 static char zerobuf[PAGE_SIZE];
113 sc = vmmdev_lookup2(cdev);
117 prot = (uio->uio_rw == UIO_WRITE ? VM_PROT_WRITE : VM_PROT_READ);
118 while (uio->uio_resid > 0 && error == 0) {
119 gpa = uio->uio_offset;
120 off = gpa & PAGE_MASK;
121 c = min(uio->uio_resid, PAGE_SIZE - off);
124 * The VM has a hole in its physical memory map. If we want to
125 * use 'dd' to inspect memory beyond the hole we need to
126 * provide bogus data for memory that lies in the hole.
128 * Since this device does not support lseek(2), dd(1) will
129 * read(2) blocks of data to simulate the lseek(2).
131 hpa = vm_gpa_hold(sc->vm, gpa, c, prot, &cookie);
133 if (uio->uio_rw == UIO_READ)
134 error = uiomove(zerobuf, c, uio);
138 error = uiomove(hpa, c, uio);
139 vm_gpa_release(cookie);
146 vmmdev_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
149 int error, vcpu, state_changed, size;
151 struct vmmdev_softc *sc;
152 struct vm_memory_segment *seg;
153 struct vm_register *vmreg;
154 struct vm_seg_desc *vmsegdesc;
155 struct vm_run *vmrun;
156 struct vm_exception *vmexc;
157 struct vm_lapic_irq *vmirq;
158 struct vm_lapic_msi *vmmsi;
159 struct vm_ioapic_irq *ioapic_irq;
160 struct vm_isa_irq *isa_irq;
161 struct vm_isa_irq_trigger *isa_irq_trigger;
162 struct vm_capability *vmcap;
163 struct vm_pptdev *pptdev;
164 struct vm_pptdev_mmio *pptmmio;
165 struct vm_pptdev_msi *pptmsi;
166 struct vm_pptdev_msix *pptmsix;
167 struct vm_nmi *vmnmi;
168 struct vm_stats *vmstats;
169 struct vm_stat_desc *statdesc;
170 struct vm_x2apic *x2apic;
171 struct vm_gpa_pte *gpapte;
172 struct vm_suspend *vmsuspend;
173 struct vm_gla2gpa *gg;
174 struct vm_activate_cpu *vac;
175 struct vm_cpuset *vm_cpuset;
177 sc = vmmdev_lookup2(cdev);
186 * Some VMM ioctls can operate only on vcpus that are not running.
190 case VM_GET_REGISTER:
191 case VM_SET_REGISTER:
192 case VM_GET_SEGMENT_DESCRIPTOR:
193 case VM_SET_SEGMENT_DESCRIPTOR:
194 case VM_INJECT_EXCEPTION:
195 case VM_GET_CAPABILITY:
196 case VM_SET_CAPABILITY:
199 case VM_SET_X2APIC_STATE:
201 case VM_ACTIVATE_CPU:
203 * XXX fragile, handle with care
204 * Assumes that the first field of the ioctl data is the vcpu.
207 if (vcpu < 0 || vcpu >= VM_MAXCPU) {
212 error = vcpu_set_state(sc->vm, vcpu, VCPU_FROZEN, true);
219 case VM_MAP_PPTDEV_MMIO:
221 case VM_UNBIND_PPTDEV:
224 * ioctls that operate on the entire virtual machine must
225 * prevent all vcpus from running.
228 for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++) {
229 error = vcpu_set_state(sc->vm, vcpu, VCPU_FROZEN, true);
236 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false);
249 vmrun = (struct vm_run *)data;
250 error = vm_run(sc->vm, vmrun);
253 vmsuspend = (struct vm_suspend *)data;
254 error = vm_suspend(sc->vm, vmsuspend->how);
257 statdesc = (struct vm_stat_desc *)data;
258 error = vmm_stat_desc_copy(statdesc->index,
259 statdesc->desc, sizeof(statdesc->desc));
263 CTASSERT(MAX_VM_STATS >= MAX_VMM_STAT_ELEMS);
264 vmstats = (struct vm_stats *)data;
265 getmicrotime(&vmstats->tv);
266 error = vmm_stat_copy(sc->vm, vmstats->cpuid,
267 &vmstats->num_entries, vmstats->statbuf);
271 pptmsi = (struct vm_pptdev_msi *)data;
272 error = ppt_setup_msi(sc->vm, pptmsi->vcpu,
273 pptmsi->bus, pptmsi->slot, pptmsi->func,
274 pptmsi->addr, pptmsi->msg,
278 pptmsix = (struct vm_pptdev_msix *)data;
279 error = ppt_setup_msix(sc->vm, pptmsix->vcpu,
280 pptmsix->bus, pptmsix->slot,
281 pptmsix->func, pptmsix->idx,
282 pptmsix->addr, pptmsix->msg,
283 pptmsix->vector_control);
285 case VM_MAP_PPTDEV_MMIO:
286 pptmmio = (struct vm_pptdev_mmio *)data;
287 error = ppt_map_mmio(sc->vm, pptmmio->bus, pptmmio->slot,
288 pptmmio->func, pptmmio->gpa, pptmmio->len,
292 pptdev = (struct vm_pptdev *)data;
293 error = vm_assign_pptdev(sc->vm, pptdev->bus, pptdev->slot,
296 case VM_UNBIND_PPTDEV:
297 pptdev = (struct vm_pptdev *)data;
298 error = vm_unassign_pptdev(sc->vm, pptdev->bus, pptdev->slot,
301 case VM_INJECT_EXCEPTION:
302 vmexc = (struct vm_exception *)data;
303 error = vm_inject_exception(sc->vm, vmexc->cpuid, vmexc);
306 vmnmi = (struct vm_nmi *)data;
307 error = vm_inject_nmi(sc->vm, vmnmi->cpuid);
310 vmirq = (struct vm_lapic_irq *)data;
311 error = lapic_intr_edge(sc->vm, vmirq->cpuid, vmirq->vector);
313 case VM_LAPIC_LOCAL_IRQ:
314 vmirq = (struct vm_lapic_irq *)data;
315 error = lapic_set_local_intr(sc->vm, vmirq->cpuid,
319 vmmsi = (struct vm_lapic_msi *)data;
320 error = lapic_intr_msi(sc->vm, vmmsi->addr, vmmsi->msg);
322 case VM_IOAPIC_ASSERT_IRQ:
323 ioapic_irq = (struct vm_ioapic_irq *)data;
324 error = vioapic_assert_irq(sc->vm, ioapic_irq->irq);
326 case VM_IOAPIC_DEASSERT_IRQ:
327 ioapic_irq = (struct vm_ioapic_irq *)data;
328 error = vioapic_deassert_irq(sc->vm, ioapic_irq->irq);
330 case VM_IOAPIC_PULSE_IRQ:
331 ioapic_irq = (struct vm_ioapic_irq *)data;
332 error = vioapic_pulse_irq(sc->vm, ioapic_irq->irq);
334 case VM_IOAPIC_PINCOUNT:
335 *(int *)data = vioapic_pincount(sc->vm);
337 case VM_ISA_ASSERT_IRQ:
338 isa_irq = (struct vm_isa_irq *)data;
339 error = vatpic_assert_irq(sc->vm, isa_irq->atpic_irq);
340 if (error == 0 && isa_irq->ioapic_irq != -1)
341 error = vioapic_assert_irq(sc->vm,
342 isa_irq->ioapic_irq);
344 case VM_ISA_DEASSERT_IRQ:
345 isa_irq = (struct vm_isa_irq *)data;
346 error = vatpic_deassert_irq(sc->vm, isa_irq->atpic_irq);
347 if (error == 0 && isa_irq->ioapic_irq != -1)
348 error = vioapic_deassert_irq(sc->vm,
349 isa_irq->ioapic_irq);
351 case VM_ISA_PULSE_IRQ:
352 isa_irq = (struct vm_isa_irq *)data;
353 error = vatpic_pulse_irq(sc->vm, isa_irq->atpic_irq);
354 if (error == 0 && isa_irq->ioapic_irq != -1)
355 error = vioapic_pulse_irq(sc->vm, isa_irq->ioapic_irq);
357 case VM_ISA_SET_IRQ_TRIGGER:
358 isa_irq_trigger = (struct vm_isa_irq_trigger *)data;
359 error = vatpic_set_irq_trigger(sc->vm,
360 isa_irq_trigger->atpic_irq, isa_irq_trigger->trigger);
363 seg = (struct vm_memory_segment *)data;
364 error = vm_malloc(sc->vm, seg->gpa, seg->len);
366 case VM_GET_MEMORY_SEG:
367 seg = (struct vm_memory_segment *)data;
369 (void)vm_gpabase2memseg(sc->vm, seg->gpa, seg);
372 case VM_GET_REGISTER:
373 vmreg = (struct vm_register *)data;
374 error = vm_get_register(sc->vm, vmreg->cpuid, vmreg->regnum,
377 case VM_SET_REGISTER:
378 vmreg = (struct vm_register *)data;
379 error = vm_set_register(sc->vm, vmreg->cpuid, vmreg->regnum,
382 case VM_SET_SEGMENT_DESCRIPTOR:
383 vmsegdesc = (struct vm_seg_desc *)data;
384 error = vm_set_seg_desc(sc->vm, vmsegdesc->cpuid,
388 case VM_GET_SEGMENT_DESCRIPTOR:
389 vmsegdesc = (struct vm_seg_desc *)data;
390 error = vm_get_seg_desc(sc->vm, vmsegdesc->cpuid,
394 case VM_GET_CAPABILITY:
395 vmcap = (struct vm_capability *)data;
396 error = vm_get_capability(sc->vm, vmcap->cpuid,
400 case VM_SET_CAPABILITY:
401 vmcap = (struct vm_capability *)data;
402 error = vm_set_capability(sc->vm, vmcap->cpuid,
406 case VM_SET_X2APIC_STATE:
407 x2apic = (struct vm_x2apic *)data;
408 error = vm_set_x2apic_state(sc->vm,
409 x2apic->cpuid, x2apic->state);
411 case VM_GET_X2APIC_STATE:
412 x2apic = (struct vm_x2apic *)data;
413 error = vm_get_x2apic_state(sc->vm,
414 x2apic->cpuid, &x2apic->state);
416 case VM_GET_GPA_PMAP:
417 gpapte = (struct vm_gpa_pte *)data;
418 pmap_get_mapping(vmspace_pmap(vm_get_vmspace(sc->vm)),
419 gpapte->gpa, gpapte->pte, &gpapte->ptenum);
422 case VM_GET_HPET_CAPABILITIES:
423 error = vhpet_getcap((struct vm_hpet_cap *)data);
426 CTASSERT(PROT_READ == VM_PROT_READ);
427 CTASSERT(PROT_WRITE == VM_PROT_WRITE);
428 CTASSERT(PROT_EXEC == VM_PROT_EXECUTE);
429 gg = (struct vm_gla2gpa *)data;
430 error = vmm_gla2gpa(sc->vm, gg->vcpuid, &gg->paging, gg->gla,
432 KASSERT(error == 0 || error == 1 || error == -1,
433 ("%s: vmm_gla2gpa unknown error %d", __func__, error));
436 * error = 0: the translation was successful
437 * error = 1: a fault was injected into the guest
446 case VM_ACTIVATE_CPU:
447 vac = (struct vm_activate_cpu *)data;
448 error = vm_activate_cpu(sc->vm, vac->vcpuid);
452 vm_cpuset = (struct vm_cpuset *)data;
453 size = vm_cpuset->cpusetsize;
454 if (size < sizeof(cpuset_t) || size > CPU_MAXSIZE / NBBY) {
458 cpuset = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
459 if (vm_cpuset->which == VM_ACTIVE_CPUS)
460 *cpuset = vm_active_cpus(sc->vm);
461 else if (vm_cpuset->which == VM_SUSPENDED_CPUS)
462 *cpuset = vm_suspended_cpus(sc->vm);
466 error = copyout(cpuset, vm_cpuset->cpus, size);
467 free(cpuset, M_TEMP);
474 if (state_changed == 1) {
475 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false);
476 } else if (state_changed == 2) {
477 for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++)
478 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false);
482 /* Make sure that no handler returns a bogus value like ERESTART */
483 KASSERT(error >= 0, ("vmmdev_ioctl: invalid error return %d", error));
488 vmmdev_mmap_single(struct cdev *cdev, vm_ooffset_t *offset,
489 vm_size_t size, struct vm_object **object, int nprot)
492 struct vmmdev_softc *sc;
494 sc = vmmdev_lookup2(cdev);
495 if (sc != NULL && (nprot & PROT_EXEC) == 0)
496 error = vm_get_memobj(sc->vm, *offset, size, offset, object);
504 vmmdev_destroy(void *arg)
507 struct vmmdev_softc *sc = arg;
509 if (sc->cdev != NULL)
510 destroy_dev(sc->cdev);
515 if ((sc->flags & VSC_LINKED) != 0) {
516 mtx_lock(&vmmdev_mtx);
517 SLIST_REMOVE(&head, sc, vmmdev_softc, link);
518 mtx_unlock(&vmmdev_mtx);
525 sysctl_vmm_destroy(SYSCTL_HANDLER_ARGS)
528 char buf[VM_MAX_NAMELEN];
529 struct vmmdev_softc *sc;
532 strlcpy(buf, "beavis", sizeof(buf));
533 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
534 if (error != 0 || req->newptr == NULL)
537 mtx_lock(&vmmdev_mtx);
538 sc = vmmdev_lookup(buf);
539 if (sc == NULL || sc->cdev == NULL) {
540 mtx_unlock(&vmmdev_mtx);
545 * The 'cdev' will be destroyed asynchronously when 'si_threadcount'
546 * goes down to 0 so we should not do it again in the callback.
550 mtx_unlock(&vmmdev_mtx);
553 * Schedule the 'cdev' to be destroyed:
555 * - any new operations on this 'cdev' will return an error (ENXIO).
557 * - when the 'si_threadcount' dwindles down to zero the 'cdev' will
558 * be destroyed and the callback will be invoked in a taskqueue
561 destroy_dev_sched_cb(cdev, vmmdev_destroy, sc);
565 SYSCTL_PROC(_hw_vmm, OID_AUTO, destroy, CTLTYPE_STRING | CTLFLAG_RW,
566 NULL, 0, sysctl_vmm_destroy, "A", NULL);
568 static struct cdevsw vmmdevsw = {
570 .d_version = D_VERSION,
571 .d_ioctl = vmmdev_ioctl,
572 .d_mmap_single = vmmdev_mmap_single,
574 .d_write = vmmdev_rw,
578 sysctl_vmm_create(SYSCTL_HANDLER_ARGS)
583 struct vmmdev_softc *sc, *sc2;
584 char buf[VM_MAX_NAMELEN];
586 strlcpy(buf, "beavis", sizeof(buf));
587 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
588 if (error != 0 || req->newptr == NULL)
591 mtx_lock(&vmmdev_mtx);
592 sc = vmmdev_lookup(buf);
593 mtx_unlock(&vmmdev_mtx);
597 error = vm_create(buf, &vm);
601 sc = malloc(sizeof(struct vmmdev_softc), M_VMMDEV, M_WAITOK | M_ZERO);
605 * Lookup the name again just in case somebody sneaked in when we
608 mtx_lock(&vmmdev_mtx);
609 sc2 = vmmdev_lookup(buf);
611 SLIST_INSERT_HEAD(&head, sc, link);
612 sc->flags |= VSC_LINKED;
614 mtx_unlock(&vmmdev_mtx);
621 error = make_dev_p(MAKEDEV_CHECKNAME, &cdev, &vmmdevsw, NULL,
622 UID_ROOT, GID_WHEEL, 0600, "vmm/%s", buf);
628 mtx_lock(&vmmdev_mtx);
630 sc->cdev->si_drv1 = sc;
631 mtx_unlock(&vmmdev_mtx);
635 SYSCTL_PROC(_hw_vmm, OID_AUTO, create, CTLTYPE_STRING | CTLFLAG_RW,
636 NULL, 0, sysctl_vmm_create, "A", NULL);
641 mtx_init(&vmmdev_mtx, "vmm device mutex", NULL, MTX_DEF);
649 if (SLIST_EMPTY(&head))