]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/vmm/vmm_dev.c
Reintegrate head revisions r273096-r277147
[FreeBSD/FreeBSD.git] / sys / amd64 / vmm / vmm_dev.c
1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/queue.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/malloc.h>
38 #include <sys/conf.h>
39 #include <sys/sysctl.h>
40 #include <sys/libkern.h>
41 #include <sys/ioccom.h>
42 #include <sys/mman.h>
43 #include <sys/uio.h>
44
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47 #include <vm/vm_map.h>
48
49 #include <machine/vmparam.h>
50 #include <machine/vmm.h>
51 #include <machine/vmm_instruction_emul.h>
52 #include <machine/vmm_dev.h>
53
54 #include "vmm_lapic.h"
55 #include "vmm_stat.h"
56 #include "vmm_mem.h"
57 #include "io/ppt.h"
58 #include "io/vatpic.h"
59 #include "io/vioapic.h"
60 #include "io/vhpet.h"
61 #include "io/vrtc.h"
62
63 struct vmmdev_softc {
64         struct vm       *vm;            /* vm instance cookie */
65         struct cdev     *cdev;
66         SLIST_ENTRY(vmmdev_softc) link;
67         int             flags;
68 };
69 #define VSC_LINKED              0x01
70
71 static SLIST_HEAD(, vmmdev_softc) head;
72
73 static struct mtx vmmdev_mtx;
74
75 static MALLOC_DEFINE(M_VMMDEV, "vmmdev", "vmmdev");
76
77 SYSCTL_DECL(_hw_vmm);
78
79 static struct vmmdev_softc *
80 vmmdev_lookup(const char *name)
81 {
82         struct vmmdev_softc *sc;
83
84 #ifdef notyet   /* XXX kernel is not compiled with invariants */
85         mtx_assert(&vmmdev_mtx, MA_OWNED);
86 #endif
87
88         SLIST_FOREACH(sc, &head, link) {
89                 if (strcmp(name, vm_name(sc->vm)) == 0)
90                         break;
91         }
92
93         return (sc);
94 }
95
96 static struct vmmdev_softc *
97 vmmdev_lookup2(struct cdev *cdev)
98 {
99
100         return (cdev->si_drv1);
101 }
102
103 static int
104 vmmdev_rw(struct cdev *cdev, struct uio *uio, int flags)
105 {
106         int error, off, c, prot;
107         vm_paddr_t gpa;
108         void *hpa, *cookie;
109         struct vmmdev_softc *sc;
110
111         static char zerobuf[PAGE_SIZE];
112
113         error = 0;
114         sc = vmmdev_lookup2(cdev);
115         if (sc == NULL)
116                 error = ENXIO;
117
118         prot = (uio->uio_rw == UIO_WRITE ? VM_PROT_WRITE : VM_PROT_READ);
119         while (uio->uio_resid > 0 && error == 0) {
120                 gpa = uio->uio_offset;
121                 off = gpa & PAGE_MASK;
122                 c = min(uio->uio_resid, PAGE_SIZE - off);
123
124                 /*
125                  * The VM has a hole in its physical memory map. If we want to
126                  * use 'dd' to inspect memory beyond the hole we need to
127                  * provide bogus data for memory that lies in the hole.
128                  *
129                  * Since this device does not support lseek(2), dd(1) will
130                  * read(2) blocks of data to simulate the lseek(2).
131                  */
132                 hpa = vm_gpa_hold(sc->vm, gpa, c, prot, &cookie);
133                 if (hpa == NULL) {
134                         if (uio->uio_rw == UIO_READ)
135                                 error = uiomove(zerobuf, c, uio);
136                         else
137                                 error = EFAULT;
138                 } else {
139                         error = uiomove(hpa, c, uio);
140                         vm_gpa_release(cookie);
141                 }
142         }
143         return (error);
144 }
145
146 static int
147 vmmdev_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
148              struct thread *td)
149 {
150         int error, vcpu, state_changed, size;
151         cpuset_t *cpuset;
152         struct vmmdev_softc *sc;
153         struct vm_memory_segment *seg;
154         struct vm_register *vmreg;
155         struct vm_seg_desc *vmsegdesc;
156         struct vm_run *vmrun;
157         struct vm_exception *vmexc;
158         struct vm_lapic_irq *vmirq;
159         struct vm_lapic_msi *vmmsi;
160         struct vm_ioapic_irq *ioapic_irq;
161         struct vm_isa_irq *isa_irq;
162         struct vm_isa_irq_trigger *isa_irq_trigger;
163         struct vm_capability *vmcap;
164         struct vm_pptdev *pptdev;
165         struct vm_pptdev_mmio *pptmmio;
166         struct vm_pptdev_msi *pptmsi;
167         struct vm_pptdev_msix *pptmsix;
168         struct vm_nmi *vmnmi;
169         struct vm_stats *vmstats;
170         struct vm_stat_desc *statdesc;
171         struct vm_x2apic *x2apic;
172         struct vm_gpa_pte *gpapte;
173         struct vm_suspend *vmsuspend;
174         struct vm_gla2gpa *gg;
175         struct vm_activate_cpu *vac;
176         struct vm_cpuset *vm_cpuset;
177         struct vm_intinfo *vmii;
178         struct vm_rtc_time *rtctime;
179         struct vm_rtc_data *rtcdata;
180
181         sc = vmmdev_lookup2(cdev);
182         if (sc == NULL)
183                 return (ENXIO);
184
185         error = 0;
186         vcpu = -1;
187         state_changed = 0;
188
189         /*
190          * Some VMM ioctls can operate only on vcpus that are not running.
191          */
192         switch (cmd) {
193         case VM_RUN:
194         case VM_GET_REGISTER:
195         case VM_SET_REGISTER:
196         case VM_GET_SEGMENT_DESCRIPTOR:
197         case VM_SET_SEGMENT_DESCRIPTOR:
198         case VM_INJECT_EXCEPTION:
199         case VM_GET_CAPABILITY:
200         case VM_SET_CAPABILITY:
201         case VM_PPTDEV_MSI:
202         case VM_PPTDEV_MSIX:
203         case VM_SET_X2APIC_STATE:
204         case VM_GLA2GPA:
205         case VM_ACTIVATE_CPU:
206         case VM_SET_INTINFO:
207         case VM_GET_INTINFO:
208                 /*
209                  * XXX fragile, handle with care
210                  * Assumes that the first field of the ioctl data is the vcpu.
211                  */
212                 vcpu = *(int *)data;
213                 if (vcpu < 0 || vcpu >= VM_MAXCPU) {
214                         error = EINVAL;
215                         goto done;
216                 }
217
218                 error = vcpu_set_state(sc->vm, vcpu, VCPU_FROZEN, true);
219                 if (error)
220                         goto done;
221
222                 state_changed = 1;
223                 break;
224
225         case VM_MAP_PPTDEV_MMIO:
226         case VM_BIND_PPTDEV:
227         case VM_UNBIND_PPTDEV:
228         case VM_MAP_MEMORY:
229         case VM_REINIT:
230                 /*
231                  * ioctls that operate on the entire virtual machine must
232                  * prevent all vcpus from running.
233                  */
234                 error = 0;
235                 for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++) {
236                         error = vcpu_set_state(sc->vm, vcpu, VCPU_FROZEN, true);
237                         if (error)
238                                 break;
239                 }
240
241                 if (error) {
242                         while (--vcpu >= 0)
243                                 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false);
244                         goto done;
245                 }
246
247                 state_changed = 2;
248                 break;
249
250         default:
251                 break;
252         }
253
254         switch(cmd) {
255         case VM_RUN:
256                 vmrun = (struct vm_run *)data;
257                 error = vm_run(sc->vm, vmrun);
258                 break;
259         case VM_SUSPEND:
260                 vmsuspend = (struct vm_suspend *)data;
261                 error = vm_suspend(sc->vm, vmsuspend->how);
262                 break;
263         case VM_REINIT:
264                 error = vm_reinit(sc->vm);
265                 break;
266         case VM_STAT_DESC: {
267                 statdesc = (struct vm_stat_desc *)data;
268                 error = vmm_stat_desc_copy(statdesc->index,
269                                         statdesc->desc, sizeof(statdesc->desc));
270                 break;
271         }
272         case VM_STATS: {
273                 CTASSERT(MAX_VM_STATS >= MAX_VMM_STAT_ELEMS);
274                 vmstats = (struct vm_stats *)data;
275                 getmicrotime(&vmstats->tv);
276                 error = vmm_stat_copy(sc->vm, vmstats->cpuid,
277                                       &vmstats->num_entries, vmstats->statbuf);
278                 break;
279         }
280         case VM_PPTDEV_MSI:
281                 pptmsi = (struct vm_pptdev_msi *)data;
282                 error = ppt_setup_msi(sc->vm, pptmsi->vcpu,
283                                       pptmsi->bus, pptmsi->slot, pptmsi->func,
284                                       pptmsi->addr, pptmsi->msg,
285                                       pptmsi->numvec);
286                 break;
287         case VM_PPTDEV_MSIX:
288                 pptmsix = (struct vm_pptdev_msix *)data;
289                 error = ppt_setup_msix(sc->vm, pptmsix->vcpu,
290                                        pptmsix->bus, pptmsix->slot, 
291                                        pptmsix->func, pptmsix->idx,
292                                        pptmsix->addr, pptmsix->msg,
293                                        pptmsix->vector_control);
294                 break;
295         case VM_MAP_PPTDEV_MMIO:
296                 pptmmio = (struct vm_pptdev_mmio *)data;
297                 error = ppt_map_mmio(sc->vm, pptmmio->bus, pptmmio->slot,
298                                      pptmmio->func, pptmmio->gpa, pptmmio->len,
299                                      pptmmio->hpa);
300                 break;
301         case VM_BIND_PPTDEV:
302                 pptdev = (struct vm_pptdev *)data;
303                 error = vm_assign_pptdev(sc->vm, pptdev->bus, pptdev->slot,
304                                          pptdev->func);
305                 break;
306         case VM_UNBIND_PPTDEV:
307                 pptdev = (struct vm_pptdev *)data;
308                 error = vm_unassign_pptdev(sc->vm, pptdev->bus, pptdev->slot,
309                                            pptdev->func);
310                 break;
311         case VM_INJECT_EXCEPTION:
312                 vmexc = (struct vm_exception *)data;
313                 error = vm_inject_exception(sc->vm, vmexc->cpuid, vmexc);
314                 break;
315         case VM_INJECT_NMI:
316                 vmnmi = (struct vm_nmi *)data;
317                 error = vm_inject_nmi(sc->vm, vmnmi->cpuid);
318                 break;
319         case VM_LAPIC_IRQ:
320                 vmirq = (struct vm_lapic_irq *)data;
321                 error = lapic_intr_edge(sc->vm, vmirq->cpuid, vmirq->vector);
322                 break;
323         case VM_LAPIC_LOCAL_IRQ:
324                 vmirq = (struct vm_lapic_irq *)data;
325                 error = lapic_set_local_intr(sc->vm, vmirq->cpuid,
326                     vmirq->vector);
327                 break;
328         case VM_LAPIC_MSI:
329                 vmmsi = (struct vm_lapic_msi *)data;
330                 error = lapic_intr_msi(sc->vm, vmmsi->addr, vmmsi->msg);
331                 break;
332         case VM_IOAPIC_ASSERT_IRQ:
333                 ioapic_irq = (struct vm_ioapic_irq *)data;
334                 error = vioapic_assert_irq(sc->vm, ioapic_irq->irq);
335                 break;
336         case VM_IOAPIC_DEASSERT_IRQ:
337                 ioapic_irq = (struct vm_ioapic_irq *)data;
338                 error = vioapic_deassert_irq(sc->vm, ioapic_irq->irq);
339                 break;
340         case VM_IOAPIC_PULSE_IRQ:
341                 ioapic_irq = (struct vm_ioapic_irq *)data;
342                 error = vioapic_pulse_irq(sc->vm, ioapic_irq->irq);
343                 break;
344         case VM_IOAPIC_PINCOUNT:
345                 *(int *)data = vioapic_pincount(sc->vm);
346                 break;
347         case VM_ISA_ASSERT_IRQ:
348                 isa_irq = (struct vm_isa_irq *)data;
349                 error = vatpic_assert_irq(sc->vm, isa_irq->atpic_irq);
350                 if (error == 0 && isa_irq->ioapic_irq != -1)
351                         error = vioapic_assert_irq(sc->vm,
352                             isa_irq->ioapic_irq);
353                 break;
354         case VM_ISA_DEASSERT_IRQ:
355                 isa_irq = (struct vm_isa_irq *)data;
356                 error = vatpic_deassert_irq(sc->vm, isa_irq->atpic_irq);
357                 if (error == 0 && isa_irq->ioapic_irq != -1)
358                         error = vioapic_deassert_irq(sc->vm,
359                             isa_irq->ioapic_irq);
360                 break;
361         case VM_ISA_PULSE_IRQ:
362                 isa_irq = (struct vm_isa_irq *)data;
363                 error = vatpic_pulse_irq(sc->vm, isa_irq->atpic_irq);
364                 if (error == 0 && isa_irq->ioapic_irq != -1)
365                         error = vioapic_pulse_irq(sc->vm, isa_irq->ioapic_irq);
366                 break;
367         case VM_ISA_SET_IRQ_TRIGGER:
368                 isa_irq_trigger = (struct vm_isa_irq_trigger *)data;
369                 error = vatpic_set_irq_trigger(sc->vm,
370                     isa_irq_trigger->atpic_irq, isa_irq_trigger->trigger);
371                 break;
372         case VM_MAP_MEMORY:
373                 seg = (struct vm_memory_segment *)data;
374                 error = vm_malloc(sc->vm, seg->gpa, seg->len);
375                 break;
376         case VM_GET_MEMORY_SEG:
377                 seg = (struct vm_memory_segment *)data;
378                 seg->len = 0;
379                 (void)vm_gpabase2memseg(sc->vm, seg->gpa, seg);
380                 error = 0;
381                 break;
382         case VM_GET_REGISTER:
383                 vmreg = (struct vm_register *)data;
384                 error = vm_get_register(sc->vm, vmreg->cpuid, vmreg->regnum,
385                                         &vmreg->regval);
386                 break;
387         case VM_SET_REGISTER:
388                 vmreg = (struct vm_register *)data;
389                 error = vm_set_register(sc->vm, vmreg->cpuid, vmreg->regnum,
390                                         vmreg->regval);
391                 break;
392         case VM_SET_SEGMENT_DESCRIPTOR:
393                 vmsegdesc = (struct vm_seg_desc *)data;
394                 error = vm_set_seg_desc(sc->vm, vmsegdesc->cpuid,
395                                         vmsegdesc->regnum,
396                                         &vmsegdesc->desc);
397                 break;
398         case VM_GET_SEGMENT_DESCRIPTOR:
399                 vmsegdesc = (struct vm_seg_desc *)data;
400                 error = vm_get_seg_desc(sc->vm, vmsegdesc->cpuid,
401                                         vmsegdesc->regnum,
402                                         &vmsegdesc->desc);
403                 break;
404         case VM_GET_CAPABILITY:
405                 vmcap = (struct vm_capability *)data;
406                 error = vm_get_capability(sc->vm, vmcap->cpuid,
407                                           vmcap->captype,
408                                           &vmcap->capval);
409                 break;
410         case VM_SET_CAPABILITY:
411                 vmcap = (struct vm_capability *)data;
412                 error = vm_set_capability(sc->vm, vmcap->cpuid,
413                                           vmcap->captype,
414                                           vmcap->capval);
415                 break;
416         case VM_SET_X2APIC_STATE:
417                 x2apic = (struct vm_x2apic *)data;
418                 error = vm_set_x2apic_state(sc->vm,
419                                             x2apic->cpuid, x2apic->state);
420                 break;
421         case VM_GET_X2APIC_STATE:
422                 x2apic = (struct vm_x2apic *)data;
423                 error = vm_get_x2apic_state(sc->vm,
424                                             x2apic->cpuid, &x2apic->state);
425                 break;
426         case VM_GET_GPA_PMAP:
427                 gpapte = (struct vm_gpa_pte *)data;
428                 pmap_get_mapping(vmspace_pmap(vm_get_vmspace(sc->vm)),
429                                  gpapte->gpa, gpapte->pte, &gpapte->ptenum);
430                 error = 0;
431                 break;
432         case VM_GET_HPET_CAPABILITIES:
433                 error = vhpet_getcap((struct vm_hpet_cap *)data);
434                 break;
435         case VM_GLA2GPA: {
436                 CTASSERT(PROT_READ == VM_PROT_READ);
437                 CTASSERT(PROT_WRITE == VM_PROT_WRITE);
438                 CTASSERT(PROT_EXEC == VM_PROT_EXECUTE);
439                 gg = (struct vm_gla2gpa *)data;
440                 error = vmm_gla2gpa(sc->vm, gg->vcpuid, &gg->paging, gg->gla,
441                     gg->prot, &gg->gpa);
442                 KASSERT(error == 0 || error == 1 || error == -1,
443                     ("%s: vmm_gla2gpa unknown error %d", __func__, error));
444                 if (error >= 0) {
445                         /*
446                          * error = 0: the translation was successful
447                          * error = 1: a fault was injected into the guest
448                          */
449                         gg->fault = error;
450                         error = 0;
451                 } else {
452                         error = EFAULT;
453                 }
454                 break;
455         }
456         case VM_ACTIVATE_CPU:
457                 vac = (struct vm_activate_cpu *)data;
458                 error = vm_activate_cpu(sc->vm, vac->vcpuid);
459                 break;
460         case VM_GET_CPUS:
461                 error = 0;
462                 vm_cpuset = (struct vm_cpuset *)data;
463                 size = vm_cpuset->cpusetsize;
464                 if (size < sizeof(cpuset_t) || size > CPU_MAXSIZE / NBBY) {
465                         error = ERANGE;
466                         break;
467                 }
468                 cpuset = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
469                 if (vm_cpuset->which == VM_ACTIVE_CPUS)
470                         *cpuset = vm_active_cpus(sc->vm);
471                 else if (vm_cpuset->which == VM_SUSPENDED_CPUS)
472                         *cpuset = vm_suspended_cpus(sc->vm);
473                 else
474                         error = EINVAL;
475                 if (error == 0)
476                         error = copyout(cpuset, vm_cpuset->cpus, size);
477                 free(cpuset, M_TEMP);
478                 break;
479         case VM_SET_INTINFO:
480                 vmii = (struct vm_intinfo *)data;
481                 error = vm_exit_intinfo(sc->vm, vmii->vcpuid, vmii->info1);
482                 break;
483         case VM_GET_INTINFO:
484                 vmii = (struct vm_intinfo *)data;
485                 error = vm_get_intinfo(sc->vm, vmii->vcpuid, &vmii->info1,
486                     &vmii->info2);
487                 break;
488         case VM_RTC_WRITE:
489                 rtcdata = (struct vm_rtc_data *)data;
490                 error = vrtc_nvram_write(sc->vm, rtcdata->offset,
491                     rtcdata->value);
492                 break;
493         case VM_RTC_READ:
494                 rtcdata = (struct vm_rtc_data *)data;
495                 error = vrtc_nvram_read(sc->vm, rtcdata->offset,
496                     &rtcdata->value);
497                 break;
498         case VM_RTC_SETTIME:
499                 rtctime = (struct vm_rtc_time *)data;
500                 error = vrtc_set_time(sc->vm, rtctime->secs);
501                 break;
502         case VM_RTC_GETTIME:
503                 error = 0;
504                 rtctime = (struct vm_rtc_time *)data;
505                 rtctime->secs = vrtc_get_time(sc->vm);
506                 break;
507         default:
508                 error = ENOTTY;
509                 break;
510         }
511
512         if (state_changed == 1) {
513                 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false);
514         } else if (state_changed == 2) {
515                 for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++)
516                         vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false);
517         }
518
519 done:
520         /* Make sure that no handler returns a bogus value like ERESTART */
521         KASSERT(error >= 0, ("vmmdev_ioctl: invalid error return %d", error));
522         return (error);
523 }
524
525 static int
526 vmmdev_mmap_single(struct cdev *cdev, vm_ooffset_t *offset,
527                    vm_size_t size, struct vm_object **object, int nprot)
528 {
529         int error;
530         struct vmmdev_softc *sc;
531
532         sc = vmmdev_lookup2(cdev);
533         if (sc != NULL && (nprot & PROT_EXEC) == 0)
534                 error = vm_get_memobj(sc->vm, *offset, size, offset, object);
535         else
536                 error = EINVAL;
537
538         return (error);
539 }
540
541 static void
542 vmmdev_destroy(void *arg)
543 {
544
545         struct vmmdev_softc *sc = arg;
546
547         if (sc->cdev != NULL)
548                 destroy_dev(sc->cdev);
549
550         if (sc->vm != NULL)
551                 vm_destroy(sc->vm);
552
553         if ((sc->flags & VSC_LINKED) != 0) {
554                 mtx_lock(&vmmdev_mtx);
555                 SLIST_REMOVE(&head, sc, vmmdev_softc, link);
556                 mtx_unlock(&vmmdev_mtx);
557         }
558
559         free(sc, M_VMMDEV);
560 }
561
562 static int
563 sysctl_vmm_destroy(SYSCTL_HANDLER_ARGS)
564 {
565         int error;
566         char buf[VM_MAX_NAMELEN];
567         struct vmmdev_softc *sc;
568         struct cdev *cdev;
569
570         strlcpy(buf, "beavis", sizeof(buf));
571         error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
572         if (error != 0 || req->newptr == NULL)
573                 return (error);
574
575         mtx_lock(&vmmdev_mtx);
576         sc = vmmdev_lookup(buf);
577         if (sc == NULL || sc->cdev == NULL) {
578                 mtx_unlock(&vmmdev_mtx);
579                 return (EINVAL);
580         }
581
582         /*
583          * The 'cdev' will be destroyed asynchronously when 'si_threadcount'
584          * goes down to 0 so we should not do it again in the callback.
585          */
586         cdev = sc->cdev;
587         sc->cdev = NULL;                
588         mtx_unlock(&vmmdev_mtx);
589
590         /*
591          * Schedule the 'cdev' to be destroyed:
592          *
593          * - any new operations on this 'cdev' will return an error (ENXIO).
594          *
595          * - when the 'si_threadcount' dwindles down to zero the 'cdev' will
596          *   be destroyed and the callback will be invoked in a taskqueue
597          *   context.
598          */
599         destroy_dev_sched_cb(cdev, vmmdev_destroy, sc);
600
601         return (0);
602 }
603 SYSCTL_PROC(_hw_vmm, OID_AUTO, destroy, CTLTYPE_STRING | CTLFLAG_RW,
604             NULL, 0, sysctl_vmm_destroy, "A", NULL);
605
606 static struct cdevsw vmmdevsw = {
607         .d_name         = "vmmdev",
608         .d_version      = D_VERSION,
609         .d_ioctl        = vmmdev_ioctl,
610         .d_mmap_single  = vmmdev_mmap_single,
611         .d_read         = vmmdev_rw,
612         .d_write        = vmmdev_rw,
613 };
614
615 static int
616 sysctl_vmm_create(SYSCTL_HANDLER_ARGS)
617 {
618         int error;
619         struct vm *vm;
620         struct cdev *cdev;
621         struct vmmdev_softc *sc, *sc2;
622         char buf[VM_MAX_NAMELEN];
623
624         strlcpy(buf, "beavis", sizeof(buf));
625         error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
626         if (error != 0 || req->newptr == NULL)
627                 return (error);
628
629         mtx_lock(&vmmdev_mtx);
630         sc = vmmdev_lookup(buf);
631         mtx_unlock(&vmmdev_mtx);
632         if (sc != NULL)
633                 return (EEXIST);
634
635         error = vm_create(buf, &vm);
636         if (error != 0)
637                 return (error);
638
639         sc = malloc(sizeof(struct vmmdev_softc), M_VMMDEV, M_WAITOK | M_ZERO);
640         sc->vm = vm;
641
642         /*
643          * Lookup the name again just in case somebody sneaked in when we
644          * dropped the lock.
645          */
646         mtx_lock(&vmmdev_mtx);
647         sc2 = vmmdev_lookup(buf);
648         if (sc2 == NULL) {
649                 SLIST_INSERT_HEAD(&head, sc, link);
650                 sc->flags |= VSC_LINKED;
651         }
652         mtx_unlock(&vmmdev_mtx);
653
654         if (sc2 != NULL) {
655                 vmmdev_destroy(sc);
656                 return (EEXIST);
657         }
658
659         error = make_dev_p(MAKEDEV_CHECKNAME, &cdev, &vmmdevsw, NULL,
660                            UID_ROOT, GID_WHEEL, 0600, "vmm/%s", buf);
661         if (error != 0) {
662                 vmmdev_destroy(sc);
663                 return (error);
664         }
665
666         mtx_lock(&vmmdev_mtx);
667         sc->cdev = cdev;
668         sc->cdev->si_drv1 = sc;
669         mtx_unlock(&vmmdev_mtx);
670
671         return (0);
672 }
673 SYSCTL_PROC(_hw_vmm, OID_AUTO, create, CTLTYPE_STRING | CTLFLAG_RW,
674             NULL, 0, sysctl_vmm_create, "A", NULL);
675
676 void
677 vmmdev_init(void)
678 {
679         mtx_init(&vmmdev_mtx, "vmm device mutex", NULL, MTX_DEF);
680 }
681
682 int
683 vmmdev_cleanup(void)
684 {
685         int error;
686
687         if (SLIST_EMPTY(&head))
688                 error = 0;
689         else
690                 error = EBUSY;
691
692         return (error);
693 }