1 /* $NetBSD: nvmm.c,v 1.43 2021/04/12 09:22:58 mrg Exp $ */
4 * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net
7 * This code is part of the NVMM hypervisor.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/devfs.h>
36 #include <sys/device.h>
37 #include <sys/fcntl.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
41 #include <sys/thread.h>
43 #include <dev/virtual/nvmm/nvmm_compat.h>
44 #include <dev/virtual/nvmm/nvmm.h>
45 #include <dev/virtual/nvmm/nvmm_internal.h>
46 #include <dev/virtual/nvmm/nvmm_ioctl.h>
48 MALLOC_DEFINE(M_NVMM, "nvmm", "NVMM data");
50 static struct nvmm_machine machines[NVMM_MAX_MACHINES];
51 static volatile unsigned int nmachines __cacheline_aligned;
53 static const struct nvmm_impl *nvmm_impl_list[] = {
54 #if defined(__x86_64__)
55 &nvmm_x86_svm, /* x86 AMD SVM */
56 &nvmm_x86_vmx /* x86 Intel VMX */
60 static const struct nvmm_impl *nvmm_impl __read_mostly = NULL;
62 static struct nvmm_owner root_owner;
64 /* -------------------------------------------------------------------------- */
67 nvmm_machine_alloc(struct nvmm_machine **ret)
69 struct nvmm_machine *mach;
72 for (i = 0; i < NVMM_MAX_MACHINES; i++) {
75 rw_enter(&mach->lock, RW_WRITER);
82 mach->time = time_second;
84 atomic_inc_uint(&nmachines);
92 nvmm_machine_free(struct nvmm_machine *mach)
94 KASSERT(rw_write_held(&mach->lock));
95 KASSERT(mach->present);
96 mach->present = false;
97 atomic_dec_uint(&nmachines);
101 nvmm_machine_get(struct nvmm_owner *owner, nvmm_machid_t machid,
102 struct nvmm_machine **ret, bool writer)
104 struct nvmm_machine *mach;
105 krw_t op = writer ? RW_WRITER : RW_READER;
107 if (__predict_false(machid >= NVMM_MAX_MACHINES)) {
110 mach = &machines[machid];
112 rw_enter(&mach->lock, op);
113 if (__predict_false(!mach->present)) {
114 rw_exit(&mach->lock);
117 if (__predict_false(mach->owner != owner && owner != &root_owner)) {
118 rw_exit(&mach->lock);
127 nvmm_machine_put(struct nvmm_machine *mach)
129 rw_exit(&mach->lock);
132 /* -------------------------------------------------------------------------- */
135 nvmm_vcpu_alloc(struct nvmm_machine *mach, nvmm_cpuid_t cpuid,
136 struct nvmm_cpu **ret)
138 struct nvmm_cpu *vcpu;
140 if (cpuid >= NVMM_MAX_VCPUS) {
143 vcpu = &mach->cpus[cpuid];
145 mutex_enter(&vcpu->lock);
147 mutex_exit(&vcpu->lock);
151 vcpu->present = true;
153 vcpu->hcpu_last = -1;
159 nvmm_vcpu_free(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
161 KASSERT(mutex_owned(&vcpu->lock));
162 vcpu->present = false;
163 if (vcpu->comm != NULL) {
164 uvm_deallocate(kernel_map, (vaddr_t)vcpu->comm, PAGE_SIZE);
169 nvmm_vcpu_get(struct nvmm_machine *mach, nvmm_cpuid_t cpuid,
170 struct nvmm_cpu **ret)
172 struct nvmm_cpu *vcpu;
174 if (__predict_false(cpuid >= NVMM_MAX_VCPUS)) {
177 vcpu = &mach->cpus[cpuid];
179 mutex_enter(&vcpu->lock);
180 if (__predict_false(!vcpu->present)) {
181 mutex_exit(&vcpu->lock);
190 nvmm_vcpu_put(struct nvmm_cpu *vcpu)
192 mutex_exit(&vcpu->lock);
195 /* -------------------------------------------------------------------------- */
198 nvmm_kill_machines(struct nvmm_owner *owner)
200 struct nvmm_machine *mach;
201 struct nvmm_cpu *vcpu;
205 for (i = 0; i < NVMM_MAX_MACHINES; i++) {
208 rw_enter(&mach->lock, RW_WRITER);
209 if (!mach->present || mach->owner != owner) {
210 rw_exit(&mach->lock);
215 for (j = 0; j < NVMM_MAX_VCPUS; j++) {
216 error = nvmm_vcpu_get(mach, j, &vcpu);
219 (*nvmm_impl->vcpu_destroy)(mach, vcpu);
220 nvmm_vcpu_free(mach, vcpu);
222 atomic_dec_uint(&mach->ncpus);
224 (*nvmm_impl->machine_destroy)(mach);
225 uvmspace_free(mach->vm);
227 /* Drop the kernel UOBJ refs. */
228 for (j = 0; j < NVMM_MAX_HMAPPINGS; j++) {
229 if (!mach->hmap[j].present)
231 uao_detach(mach->hmap[j].uobj);
234 nvmm_machine_free(mach);
236 rw_exit(&mach->lock);
240 /* -------------------------------------------------------------------------- */
243 nvmm_capability(struct nvmm_owner *owner, struct nvmm_ioc_capability *args)
245 args->cap.version = NVMM_KERN_VERSION;
246 args->cap.state_size = nvmm_impl->state_size;
247 args->cap.max_machines = NVMM_MAX_MACHINES;
248 args->cap.max_vcpus = NVMM_MAX_VCPUS;
249 args->cap.max_ram = NVMM_MAX_RAM;
251 (*nvmm_impl->capability)(&args->cap);
257 nvmm_machine_create(struct nvmm_owner *owner,
258 struct nvmm_ioc_machine_create *args)
260 struct nvmm_machine *mach;
263 error = nvmm_machine_alloc(&mach);
267 /* Curproc owns the machine. */
270 /* Zero out the host mappings. */
271 memset(&mach->hmap, 0, sizeof(mach->hmap));
273 /* Create the machine vmspace. */
275 mach->gpa_end = NVMM_MAX_RAM;
276 mach->vm = uvmspace_alloc(0, mach->gpa_end - mach->gpa_begin, false);
278 /* Create the comm uobj. */
279 mach->commuobj = uao_create(NVMM_MAX_VCPUS * PAGE_SIZE, 0);
281 (*nvmm_impl->machine_create)(mach);
283 args->machid = mach->machid;
284 nvmm_machine_put(mach);
290 nvmm_machine_destroy(struct nvmm_owner *owner,
291 struct nvmm_ioc_machine_destroy *args)
293 struct nvmm_machine *mach;
294 struct nvmm_cpu *vcpu;
298 error = nvmm_machine_get(owner, args->machid, &mach, true);
302 for (i = 0; i < NVMM_MAX_VCPUS; i++) {
303 error = nvmm_vcpu_get(mach, i, &vcpu);
307 (*nvmm_impl->vcpu_destroy)(mach, vcpu);
308 nvmm_vcpu_free(mach, vcpu);
310 atomic_dec_uint(&mach->ncpus);
313 (*nvmm_impl->machine_destroy)(mach);
315 /* Free the machine vmspace. */
316 uvmspace_free(mach->vm);
318 /* Drop the kernel UOBJ refs. */
319 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
320 if (!mach->hmap[i].present)
322 uao_detach(mach->hmap[i].uobj);
325 nvmm_machine_free(mach);
326 nvmm_machine_put(mach);
332 nvmm_machine_configure(struct nvmm_owner *owner,
333 struct nvmm_ioc_machine_configure *args)
335 struct nvmm_machine *mach;
341 op = NVMM_MACH_CONF_MD(args->op);
342 if (__predict_false(op >= nvmm_impl->mach_conf_max)) {
346 allocsz = nvmm_impl->mach_conf_sizes[op];
347 data = kmem_alloc(allocsz, KM_SLEEP);
349 error = nvmm_machine_get(owner, args->machid, &mach, true);
351 kmem_free(data, allocsz);
355 error = copyin(args->conf, data, allocsz);
360 error = (*nvmm_impl->machine_configure)(mach, op, data);
363 nvmm_machine_put(mach);
364 kmem_free(data, allocsz);
369 nvmm_vcpu_create(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_create *args)
371 struct nvmm_machine *mach;
372 struct nvmm_cpu *vcpu;
375 error = nvmm_machine_get(owner, args->machid, &mach, false);
379 error = nvmm_vcpu_alloc(mach, args->cpuid, &vcpu);
383 /* Allocate the comm page. */
384 uao_reference(mach->commuobj);
385 error = uvm_map(kernel_map, (vaddr_t *)&vcpu->comm, PAGE_SIZE,
386 mach->commuobj, args->cpuid * PAGE_SIZE, 0, UVM_MAPFLAG(UVM_PROT_RW,
387 UVM_PROT_RW, UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
389 uao_detach(mach->commuobj);
390 nvmm_vcpu_free(mach, vcpu);
394 error = uvm_map_pageable(kernel_map, (vaddr_t)vcpu->comm,
395 (vaddr_t)vcpu->comm + PAGE_SIZE, false, 0);
397 nvmm_vcpu_free(mach, vcpu);
401 memset(vcpu->comm, 0, PAGE_SIZE);
403 error = (*nvmm_impl->vcpu_create)(mach, vcpu);
405 nvmm_vcpu_free(mach, vcpu);
411 atomic_inc_uint(&mach->ncpus);
414 nvmm_machine_put(mach);
419 nvmm_vcpu_destroy(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_destroy *args)
421 struct nvmm_machine *mach;
422 struct nvmm_cpu *vcpu;
425 error = nvmm_machine_get(owner, args->machid, &mach, false);
429 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
433 (*nvmm_impl->vcpu_destroy)(mach, vcpu);
434 nvmm_vcpu_free(mach, vcpu);
436 atomic_dec_uint(&mach->ncpus);
439 nvmm_machine_put(mach);
444 nvmm_vcpu_configure(struct nvmm_owner *owner,
445 struct nvmm_ioc_vcpu_configure *args)
447 struct nvmm_machine *mach;
448 struct nvmm_cpu *vcpu;
454 op = NVMM_VCPU_CONF_MD(args->op);
455 if (__predict_false(op >= nvmm_impl->vcpu_conf_max))
458 allocsz = nvmm_impl->vcpu_conf_sizes[op];
459 data = kmem_alloc(allocsz, KM_SLEEP);
461 error = nvmm_machine_get(owner, args->machid, &mach, false);
463 kmem_free(data, allocsz);
467 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
469 nvmm_machine_put(mach);
470 kmem_free(data, allocsz);
474 error = copyin(args->conf, data, allocsz);
479 error = (*nvmm_impl->vcpu_configure)(vcpu, op, data);
483 nvmm_machine_put(mach);
484 kmem_free(data, allocsz);
489 nvmm_vcpu_setstate(struct nvmm_owner *owner,
490 struct nvmm_ioc_vcpu_setstate *args)
492 struct nvmm_machine *mach;
493 struct nvmm_cpu *vcpu;
496 error = nvmm_machine_get(owner, args->machid, &mach, false);
500 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
504 (*nvmm_impl->vcpu_setstate)(vcpu);
508 nvmm_machine_put(mach);
513 nvmm_vcpu_getstate(struct nvmm_owner *owner,
514 struct nvmm_ioc_vcpu_getstate *args)
516 struct nvmm_machine *mach;
517 struct nvmm_cpu *vcpu;
520 error = nvmm_machine_get(owner, args->machid, &mach, false);
524 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
528 (*nvmm_impl->vcpu_getstate)(vcpu);
532 nvmm_machine_put(mach);
537 nvmm_vcpu_inject(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_inject *args)
539 struct nvmm_machine *mach;
540 struct nvmm_cpu *vcpu;
543 error = nvmm_machine_get(owner, args->machid, &mach, false);
547 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
551 error = (*nvmm_impl->vcpu_inject)(vcpu);
555 nvmm_machine_put(mach);
560 nvmm_do_vcpu_run(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
561 struct nvmm_vcpu_exit *exit)
563 struct vmspace *vm = mach->vm;
567 /* Got a signal? Or pending resched? Leave. */
568 if (__predict_false(nvmm_return_needed())) {
569 exit->reason = NVMM_VCPU_EXIT_NONE;
574 ret = (*nvmm_impl->vcpu_run)(mach, vcpu, exit);
575 if (__predict_false(ret != 0)) {
579 /* Process nested page faults. */
580 if (__predict_true(exit->reason != NVMM_VCPU_EXIT_MEMORY)) {
583 if (exit->u.mem.gpa >= mach->gpa_end) {
586 if (uvm_fault(&vm->vm_map, exit->u.mem.gpa, exit->u.mem.prot)) {
595 nvmm_vcpu_run(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_run *args)
597 struct nvmm_machine *mach;
598 struct nvmm_cpu *vcpu;
601 error = nvmm_machine_get(owner, args->machid, &mach, false);
605 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
609 error = nvmm_do_vcpu_run(mach, vcpu, &args->exit);
613 nvmm_machine_put(mach);
617 /* -------------------------------------------------------------------------- */
619 static struct uvm_object *
620 nvmm_hmapping_getuobj(struct nvmm_machine *mach, uintptr_t hva, size_t size,
623 struct nvmm_hmapping *hmapping;
626 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
627 hmapping = &mach->hmap[i];
628 if (!hmapping->present) {
631 if (hva >= hmapping->hva &&
632 hva + size <= hmapping->hva + hmapping->size) {
633 *off = hva - hmapping->hva;
634 return hmapping->uobj;
642 nvmm_hmapping_validate(struct nvmm_machine *mach, uintptr_t hva, size_t size)
644 struct nvmm_hmapping *hmapping;
647 if ((hva % PAGE_SIZE) != 0 || (size % PAGE_SIZE) != 0) {
654 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
655 hmapping = &mach->hmap[i];
656 if (!hmapping->present) {
660 if (hva >= hmapping->hva &&
661 hva + size <= hmapping->hva + hmapping->size) {
665 if (hva >= hmapping->hva &&
666 hva < hmapping->hva + hmapping->size) {
669 if (hva + size > hmapping->hva &&
670 hva + size <= hmapping->hva + hmapping->size) {
673 if (hva <= hmapping->hva &&
674 hva + size >= hmapping->hva + hmapping->size) {
682 static struct nvmm_hmapping *
683 nvmm_hmapping_alloc(struct nvmm_machine *mach)
685 struct nvmm_hmapping *hmapping;
688 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
689 hmapping = &mach->hmap[i];
690 if (!hmapping->present) {
691 hmapping->present = true;
700 nvmm_hmapping_free(struct nvmm_machine *mach, uintptr_t hva, size_t size)
702 struct vmspace *vmspace = curproc->p_vmspace;
703 struct nvmm_hmapping *hmapping;
706 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
707 hmapping = &mach->hmap[i];
708 if (!hmapping->present || hmapping->hva != hva ||
709 hmapping->size != size) {
713 uvm_unmap(&vmspace->vm_map, hmapping->hva,
714 hmapping->hva + hmapping->size);
715 uao_detach(hmapping->uobj);
717 hmapping->uobj = NULL;
718 hmapping->present = false;
727 nvmm_hva_map(struct nvmm_owner *owner, struct nvmm_ioc_hva_map *args)
729 struct vmspace *vmspace = curproc->p_vmspace;
730 struct nvmm_machine *mach;
731 struct nvmm_hmapping *hmapping;
735 error = nvmm_machine_get(owner, args->machid, &mach, true);
739 error = nvmm_hmapping_validate(mach, args->hva, args->size);
743 hmapping = nvmm_hmapping_alloc(mach);
744 if (hmapping == NULL) {
749 hmapping->hva = args->hva;
750 hmapping->size = args->size;
751 hmapping->uobj = uao_create(hmapping->size, 0);
754 /* Take a reference for the user. */
755 uao_reference(hmapping->uobj);
757 /* Map the uobj into the user address space, as pageable. */
758 error = uvm_map(&vmspace->vm_map, &uva, hmapping->size, hmapping->uobj,
759 0, 0, UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_SHARE,
760 UVM_ADV_RANDOM, UVM_FLAG_FIXED|UVM_FLAG_UNMAP));
762 uao_detach(hmapping->uobj);
766 nvmm_machine_put(mach);
771 nvmm_hva_unmap(struct nvmm_owner *owner, struct nvmm_ioc_hva_unmap *args)
773 struct nvmm_machine *mach;
776 error = nvmm_machine_get(owner, args->machid, &mach, true);
780 error = nvmm_hmapping_free(mach, args->hva, args->size);
782 nvmm_machine_put(mach);
786 /* -------------------------------------------------------------------------- */
789 nvmm_gpa_map(struct nvmm_owner *owner, struct nvmm_ioc_gpa_map *args)
791 struct nvmm_machine *mach;
792 struct uvm_object *uobj;
797 error = nvmm_machine_get(owner, args->machid, &mach, false);
801 if ((args->prot & ~(PROT_READ|PROT_WRITE|PROT_EXEC)) != 0) {
806 if ((args->gpa % PAGE_SIZE) != 0 || (args->size % PAGE_SIZE) != 0 ||
807 (args->hva % PAGE_SIZE) != 0) {
811 if (args->hva == 0) {
815 if (args->gpa < mach->gpa_begin || args->gpa >= mach->gpa_end) {
819 if (args->gpa + args->size <= args->gpa) {
823 if (args->gpa + args->size > mach->gpa_end) {
829 uobj = nvmm_hmapping_getuobj(mach, args->hva, args->size, &off);
835 /* Take a reference for the machine. */
838 /* Map the uobj into the machine address space, as pageable. */
839 error = uvm_map(&mach->vm->vm_map, &gpa, args->size, uobj, off, 0,
840 UVM_MAPFLAG(args->prot, UVM_PROT_RWX, UVM_INH_NONE,
841 UVM_ADV_RANDOM, UVM_FLAG_FIXED|UVM_FLAG_UNMAP));
846 if (gpa != args->gpa) {
848 printf("[!] uvm_map problem\n");
854 nvmm_machine_put(mach);
859 nvmm_gpa_unmap(struct nvmm_owner *owner, struct nvmm_ioc_gpa_unmap *args)
861 struct nvmm_machine *mach;
865 error = nvmm_machine_get(owner, args->machid, &mach, false);
869 if ((args->gpa % PAGE_SIZE) != 0 || (args->size % PAGE_SIZE) != 0) {
873 if (args->gpa < mach->gpa_begin || args->gpa >= mach->gpa_end) {
877 if (args->gpa + args->size <= args->gpa) {
881 if (args->gpa + args->size >= mach->gpa_end) {
887 /* Unmap the memory from the machine. */
888 uvm_unmap(&mach->vm->vm_map, gpa, gpa + args->size);
891 nvmm_machine_put(mach);
895 /* -------------------------------------------------------------------------- */
898 nvmm_ctl_mach_info(struct nvmm_owner *owner, struct nvmm_ioc_ctl *args)
900 struct nvmm_ctl_mach_info ctl;
901 struct nvmm_machine *mach;
905 if (args->size != sizeof(ctl))
907 error = copyin(args->data, &ctl, sizeof(ctl));
911 error = nvmm_machine_get(owner, ctl.machid, &mach, true);
915 ctl.nvcpus = mach->ncpus;
918 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
919 if (!mach->hmap[i].present)
921 ctl.nram += mach->hmap[i].size;
924 ctl.pid = mach->owner->pid;
925 ctl.time = mach->time;
927 nvmm_machine_put(mach);
929 error = copyout(&ctl, args->data, sizeof(ctl));
937 nvmm_ctl(struct nvmm_owner *owner, struct nvmm_ioc_ctl *args)
940 case NVMM_CTL_MACH_INFO:
941 return nvmm_ctl_mach_info(owner, args);
947 /* -------------------------------------------------------------------------- */
949 static const struct nvmm_impl *
954 for (i = 0; i < __arraycount(nvmm_impl_list); i++) {
955 if ((*nvmm_impl_list[i]->ident)())
956 return nvmm_impl_list[i];
967 nvmm_impl = nvmm_ident();
968 if (nvmm_impl == NULL)
971 for (i = 0; i < NVMM_MAX_MACHINES; i++) {
972 machines[i].machid = i;
973 rw_init(&machines[i].lock);
974 for (n = 0; n < NVMM_MAX_VCPUS; n++) {
975 machines[i].cpus[n].present = false;
976 machines[i].cpus[n].cpuid = n;
977 mutex_init(&machines[i].cpus[n].lock, MUTEX_DEFAULT,
982 (*nvmm_impl->init)();
992 for (i = 0; i < NVMM_MAX_MACHINES; i++) {
993 rw_destroy(&machines[i].lock);
994 for (n = 0; n < NVMM_MAX_VCPUS; n++) {
995 mutex_destroy(&machines[i].cpus[n].lock);
999 (*nvmm_impl->fini)();
1003 /* -------------------------------------------------------------------------- */
1005 static d_open_t nvmm_open;
1006 static d_ioctl_t nvmm_ioctl;
1007 static d_mmap_single_t nvmm_mmap_single;
1008 static d_priv_dtor_t nvmm_dtor;
1010 static struct dev_ops nvmm_ops = {
1011 { "nvmm", 0, D_MPSAFE },
1012 .d_open = nvmm_open,
1013 .d_ioctl = nvmm_ioctl,
1014 .d_mmap_single = nvmm_mmap_single,
1018 nvmm_open(struct dev_open_args *ap)
1020 int flags = ap->a_oflags;
1021 struct nvmm_owner *owner;
1025 if (__predict_false(nvmm_impl == NULL))
1027 if (!(flags & O_CLOEXEC))
1030 if (priv_check_cred(ap->a_cred, PRIV_ROOT, 0) == 0) {
1031 owner = &root_owner;
1033 owner = kmem_alloc(sizeof(*owner), KM_SLEEP);
1034 owner->pid = curthread->td_proc->p_pid;
1037 fp = ap->a_fpp ? *ap->a_fpp : NULL;
1038 error = devfs_set_cdevpriv(fp, owner, nvmm_dtor);
1048 nvmm_dtor(void *arg)
1050 struct nvmm_owner *owner = arg;
1052 KASSERT(owner != NULL);
1053 nvmm_kill_machines(owner);
1054 if (owner != &root_owner) {
1055 kmem_free(owner, sizeof(*owner));
1060 nvmm_mmap_single(struct dev_mmap_single_args *ap)
1062 vm_ooffset_t *offp = ap->a_offset;
1063 size_t size = ap->a_size;
1064 int prot = ap->a_nprot;
1065 struct vm_object **uobjp = ap->a_object;
1066 struct file *fp = ap->a_fp;
1067 struct nvmm_owner *owner = NULL;
1068 struct nvmm_machine *mach;
1069 nvmm_machid_t machid;
1073 devfs_get_cdevpriv(fp, (void **)&owner);
1074 KASSERT(owner != NULL);
1076 if (prot & PROT_EXEC)
1078 if (size != PAGE_SIZE)
1081 cpuid = NVMM_COMM_CPUID(*offp);
1082 if (__predict_false(cpuid >= NVMM_MAX_VCPUS))
1085 machid = NVMM_COMM_MACHID(*offp);
1086 error = nvmm_machine_get(owner, machid, &mach, false);
1090 uao_reference(mach->commuobj);
1091 *uobjp = mach->commuobj;
1092 *offp = cpuid * PAGE_SIZE;
1094 nvmm_machine_put(mach);
1099 nvmm_ioctl(struct dev_ioctl_args *ap)
1101 unsigned long cmd = ap->a_cmd;
1102 void *data = ap->a_data;
1103 struct file *fp = ap->a_fp;
1104 struct nvmm_owner *owner = NULL;
1106 devfs_get_cdevpriv(fp, (void **)&owner);
1107 KASSERT(owner != NULL);
1110 case NVMM_IOC_CAPABILITY:
1111 return nvmm_capability(owner, data);
1112 case NVMM_IOC_MACHINE_CREATE:
1113 return nvmm_machine_create(owner, data);
1114 case NVMM_IOC_MACHINE_DESTROY:
1115 return nvmm_machine_destroy(owner, data);
1116 case NVMM_IOC_MACHINE_CONFIGURE:
1117 return nvmm_machine_configure(owner, data);
1118 case NVMM_IOC_VCPU_CREATE:
1119 return nvmm_vcpu_create(owner, data);
1120 case NVMM_IOC_VCPU_DESTROY:
1121 return nvmm_vcpu_destroy(owner, data);
1122 case NVMM_IOC_VCPU_CONFIGURE:
1123 return nvmm_vcpu_configure(owner, data);
1124 case NVMM_IOC_VCPU_SETSTATE:
1125 return nvmm_vcpu_setstate(owner, data);
1126 case NVMM_IOC_VCPU_GETSTATE:
1127 return nvmm_vcpu_getstate(owner, data);
1128 case NVMM_IOC_VCPU_INJECT:
1129 return nvmm_vcpu_inject(owner, data);
1130 case NVMM_IOC_VCPU_RUN:
1131 return nvmm_vcpu_run(owner, data);
1132 case NVMM_IOC_GPA_MAP:
1133 return nvmm_gpa_map(owner, data);
1134 case NVMM_IOC_GPA_UNMAP:
1135 return nvmm_gpa_unmap(owner, data);
1136 case NVMM_IOC_HVA_MAP:
1137 return nvmm_hva_map(owner, data);
1138 case NVMM_IOC_HVA_UNMAP:
1139 return nvmm_hva_unmap(owner, data);
1141 return nvmm_ctl(owner, data);
1147 /* -------------------------------------------------------------------------- */
1154 error = nvmm_init();
1156 panic("%s: impossible", __func__);
1157 printf("nvmm: attached, using backend %s\n", nvmm_impl->name);
1165 if (atomic_load_acq_int(&nmachines) > 0)
1173 nvmm_modevent(module_t mod __unused, int type, void *data __unused)
1175 static cdev_t dev = NULL;
1180 if (nvmm_ident() == NULL) {
1181 printf("nvmm: cpu not supported\n");
1184 error = nvmm_attach();
1188 dev = make_dev(&nvmm_ops, 0, UID_ROOT, GID_NVMM, 0660, "nvmm");
1190 printf("nvmm: unable to create device\n");
1198 error = nvmm_detach();
1215 static moduledata_t nvmm_moddata = {
1217 .evhand = nvmm_modevent,
1221 DECLARE_MODULE(nvmm, nvmm_moddata, SI_SUB_PSEUDO, SI_ORDER_ANY);
1222 MODULE_VERSION(nvmm, NVMM_KERN_VERSION);