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);
280 * Set PMAP_MULTI on the backing pmap for the machine. Only
281 * pmap changes to the backing pmap for the machine affect the
282 * guest. Changes to the host's pmap do not affect the guest's
285 pmap_maybethreaded(&mach->vm->vm_pmap);
288 /* Create the comm uobj. */
289 mach->commuobj = uao_create(NVMM_MAX_VCPUS * PAGE_SIZE, 0);
291 (*nvmm_impl->machine_create)(mach);
293 args->machid = mach->machid;
294 nvmm_machine_put(mach);
300 nvmm_machine_destroy(struct nvmm_owner *owner,
301 struct nvmm_ioc_machine_destroy *args)
303 struct nvmm_machine *mach;
304 struct nvmm_cpu *vcpu;
308 error = nvmm_machine_get(owner, args->machid, &mach, true);
312 for (i = 0; i < NVMM_MAX_VCPUS; i++) {
313 error = nvmm_vcpu_get(mach, i, &vcpu);
317 (*nvmm_impl->vcpu_destroy)(mach, vcpu);
318 nvmm_vcpu_free(mach, vcpu);
320 atomic_dec_uint(&mach->ncpus);
323 (*nvmm_impl->machine_destroy)(mach);
325 /* Free the machine vmspace. */
326 uvmspace_free(mach->vm);
328 /* Drop the kernel UOBJ refs. */
329 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
330 if (!mach->hmap[i].present)
332 uao_detach(mach->hmap[i].uobj);
335 nvmm_machine_free(mach);
336 nvmm_machine_put(mach);
342 nvmm_machine_configure(struct nvmm_owner *owner,
343 struct nvmm_ioc_machine_configure *args)
345 struct nvmm_machine *mach;
351 op = NVMM_MACH_CONF_MD(args->op);
352 if (__predict_false(op >= nvmm_impl->mach_conf_max)) {
356 allocsz = nvmm_impl->mach_conf_sizes[op];
357 data = kmem_alloc(allocsz, KM_SLEEP);
359 error = nvmm_machine_get(owner, args->machid, &mach, true);
361 kmem_free(data, allocsz);
365 error = copyin(args->conf, data, allocsz);
370 error = (*nvmm_impl->machine_configure)(mach, op, data);
373 nvmm_machine_put(mach);
374 kmem_free(data, allocsz);
379 nvmm_vcpu_create(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_create *args)
381 struct nvmm_machine *mach;
382 struct nvmm_cpu *vcpu;
385 error = nvmm_machine_get(owner, args->machid, &mach, false);
389 error = nvmm_vcpu_alloc(mach, args->cpuid, &vcpu);
393 /* Allocate the comm page. */
394 uao_reference(mach->commuobj);
395 error = uvm_map(kernel_map, (vaddr_t *)&vcpu->comm, PAGE_SIZE,
396 mach->commuobj, args->cpuid * PAGE_SIZE, 0, UVM_MAPFLAG(UVM_PROT_RW,
397 UVM_PROT_RW, UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
399 uao_detach(mach->commuobj);
400 nvmm_vcpu_free(mach, vcpu);
404 error = uvm_map_pageable(kernel_map, (vaddr_t)vcpu->comm,
405 (vaddr_t)vcpu->comm + PAGE_SIZE, false, 0);
407 nvmm_vcpu_free(mach, vcpu);
411 memset(vcpu->comm, 0, PAGE_SIZE);
413 error = (*nvmm_impl->vcpu_create)(mach, vcpu);
415 nvmm_vcpu_free(mach, vcpu);
421 atomic_inc_uint(&mach->ncpus);
424 nvmm_machine_put(mach);
429 nvmm_vcpu_destroy(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_destroy *args)
431 struct nvmm_machine *mach;
432 struct nvmm_cpu *vcpu;
435 error = nvmm_machine_get(owner, args->machid, &mach, false);
439 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
443 (*nvmm_impl->vcpu_destroy)(mach, vcpu);
444 nvmm_vcpu_free(mach, vcpu);
446 atomic_dec_uint(&mach->ncpus);
449 nvmm_machine_put(mach);
454 nvmm_vcpu_configure(struct nvmm_owner *owner,
455 struct nvmm_ioc_vcpu_configure *args)
457 struct nvmm_machine *mach;
458 struct nvmm_cpu *vcpu;
464 op = NVMM_VCPU_CONF_MD(args->op);
465 if (__predict_false(op >= nvmm_impl->vcpu_conf_max))
468 allocsz = nvmm_impl->vcpu_conf_sizes[op];
469 data = kmem_alloc(allocsz, KM_SLEEP);
471 error = nvmm_machine_get(owner, args->machid, &mach, false);
473 kmem_free(data, allocsz);
477 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
479 nvmm_machine_put(mach);
480 kmem_free(data, allocsz);
484 error = copyin(args->conf, data, allocsz);
489 error = (*nvmm_impl->vcpu_configure)(vcpu, op, data);
493 nvmm_machine_put(mach);
494 kmem_free(data, allocsz);
499 nvmm_vcpu_setstate(struct nvmm_owner *owner,
500 struct nvmm_ioc_vcpu_setstate *args)
502 struct nvmm_machine *mach;
503 struct nvmm_cpu *vcpu;
506 error = nvmm_machine_get(owner, args->machid, &mach, false);
510 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
514 (*nvmm_impl->vcpu_setstate)(vcpu);
518 nvmm_machine_put(mach);
523 nvmm_vcpu_getstate(struct nvmm_owner *owner,
524 struct nvmm_ioc_vcpu_getstate *args)
526 struct nvmm_machine *mach;
527 struct nvmm_cpu *vcpu;
530 error = nvmm_machine_get(owner, args->machid, &mach, false);
534 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
538 (*nvmm_impl->vcpu_getstate)(vcpu);
542 nvmm_machine_put(mach);
547 nvmm_vcpu_inject(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_inject *args)
549 struct nvmm_machine *mach;
550 struct nvmm_cpu *vcpu;
553 error = nvmm_machine_get(owner, args->machid, &mach, false);
557 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
561 error = (*nvmm_impl->vcpu_inject)(vcpu);
565 nvmm_machine_put(mach);
570 nvmm_do_vcpu_run(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
571 struct nvmm_vcpu_exit *exit)
573 struct vmspace *vm = mach->vm;
577 /* Got a signal? Or pending resched? Leave. */
578 if (__predict_false(nvmm_return_needed())) {
579 exit->reason = NVMM_VCPU_EXIT_NONE;
584 ret = (*nvmm_impl->vcpu_run)(mach, vcpu, exit);
585 if (__predict_false(ret != 0)) {
589 /* Process nested page faults. */
590 if (__predict_true(exit->reason != NVMM_VCPU_EXIT_MEMORY)) {
593 if (exit->u.mem.gpa >= mach->gpa_end) {
596 if (uvm_fault(&vm->vm_map, exit->u.mem.gpa, exit->u.mem.prot)) {
605 nvmm_vcpu_run(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_run *args)
607 struct nvmm_machine *mach;
608 struct nvmm_cpu *vcpu;
611 error = nvmm_machine_get(owner, args->machid, &mach, false);
615 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
619 error = nvmm_do_vcpu_run(mach, vcpu, &args->exit);
623 nvmm_machine_put(mach);
627 /* -------------------------------------------------------------------------- */
629 static struct uvm_object *
630 nvmm_hmapping_getuobj(struct nvmm_machine *mach, uintptr_t hva, size_t size,
633 struct nvmm_hmapping *hmapping;
636 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
637 hmapping = &mach->hmap[i];
638 if (!hmapping->present) {
641 if (hva >= hmapping->hva &&
642 hva + size <= hmapping->hva + hmapping->size) {
643 *off = hva - hmapping->hva;
644 return hmapping->uobj;
652 nvmm_hmapping_validate(struct nvmm_machine *mach, uintptr_t hva, size_t size)
654 struct nvmm_hmapping *hmapping;
657 if ((hva % PAGE_SIZE) != 0 || (size % PAGE_SIZE) != 0) {
664 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
665 hmapping = &mach->hmap[i];
666 if (!hmapping->present) {
670 if (hva >= hmapping->hva &&
671 hva + size <= hmapping->hva + hmapping->size) {
675 if (hva >= hmapping->hva &&
676 hva < hmapping->hva + hmapping->size) {
679 if (hva + size > hmapping->hva &&
680 hva + size <= hmapping->hva + hmapping->size) {
683 if (hva <= hmapping->hva &&
684 hva + size >= hmapping->hva + hmapping->size) {
692 static struct nvmm_hmapping *
693 nvmm_hmapping_alloc(struct nvmm_machine *mach)
695 struct nvmm_hmapping *hmapping;
698 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
699 hmapping = &mach->hmap[i];
700 if (!hmapping->present) {
701 hmapping->present = true;
710 nvmm_hmapping_free(struct nvmm_machine *mach, uintptr_t hva, size_t size)
712 struct vmspace *vmspace = curproc->p_vmspace;
713 struct nvmm_hmapping *hmapping;
716 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
717 hmapping = &mach->hmap[i];
718 if (!hmapping->present || hmapping->hva != hva ||
719 hmapping->size != size) {
723 uvm_unmap(&vmspace->vm_map, hmapping->hva,
724 hmapping->hva + hmapping->size);
725 uao_detach(hmapping->uobj);
727 hmapping->uobj = NULL;
728 hmapping->present = false;
737 nvmm_hva_map(struct nvmm_owner *owner, struct nvmm_ioc_hva_map *args)
739 struct vmspace *vmspace = curproc->p_vmspace;
740 struct nvmm_machine *mach;
741 struct nvmm_hmapping *hmapping;
745 error = nvmm_machine_get(owner, args->machid, &mach, true);
749 error = nvmm_hmapping_validate(mach, args->hva, args->size);
753 hmapping = nvmm_hmapping_alloc(mach);
754 if (hmapping == NULL) {
759 hmapping->hva = args->hva;
760 hmapping->size = args->size;
761 hmapping->uobj = uao_create(hmapping->size, 0);
764 /* Take a reference for the user. */
765 uao_reference(hmapping->uobj);
767 /* Map the uobj into the user address space, as pageable. */
768 error = uvm_map(&vmspace->vm_map, &uva, hmapping->size, hmapping->uobj,
769 0, 0, UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_SHARE,
770 UVM_ADV_RANDOM, UVM_FLAG_FIXED|UVM_FLAG_UNMAP));
772 uao_detach(hmapping->uobj);
776 nvmm_machine_put(mach);
781 nvmm_hva_unmap(struct nvmm_owner *owner, struct nvmm_ioc_hva_unmap *args)
783 struct nvmm_machine *mach;
786 error = nvmm_machine_get(owner, args->machid, &mach, true);
790 error = nvmm_hmapping_free(mach, args->hva, args->size);
792 nvmm_machine_put(mach);
796 /* -------------------------------------------------------------------------- */
799 nvmm_gpa_map(struct nvmm_owner *owner, struct nvmm_ioc_gpa_map *args)
801 struct nvmm_machine *mach;
802 struct uvm_object *uobj;
807 error = nvmm_machine_get(owner, args->machid, &mach, false);
811 if ((args->prot & ~(PROT_READ|PROT_WRITE|PROT_EXEC)) != 0) {
816 if ((args->gpa % PAGE_SIZE) != 0 || (args->size % PAGE_SIZE) != 0 ||
817 (args->hva % PAGE_SIZE) != 0) {
821 if (args->hva == 0) {
825 if (args->gpa < mach->gpa_begin || args->gpa >= mach->gpa_end) {
829 if (args->gpa + args->size <= args->gpa) {
833 if (args->gpa + args->size > mach->gpa_end) {
839 uobj = nvmm_hmapping_getuobj(mach, args->hva, args->size, &off);
845 /* Take a reference for the machine. */
848 /* Map the uobj into the machine address space, as pageable. */
849 error = uvm_map(&mach->vm->vm_map, &gpa, args->size, uobj, off, 0,
850 UVM_MAPFLAG(args->prot, UVM_PROT_RWX, UVM_INH_NONE,
851 UVM_ADV_RANDOM, UVM_FLAG_FIXED|UVM_FLAG_UNMAP));
856 if (gpa != args->gpa) {
858 printf("[!] uvm_map problem\n");
864 nvmm_machine_put(mach);
869 nvmm_gpa_unmap(struct nvmm_owner *owner, struct nvmm_ioc_gpa_unmap *args)
871 struct nvmm_machine *mach;
875 error = nvmm_machine_get(owner, args->machid, &mach, false);
879 if ((args->gpa % PAGE_SIZE) != 0 || (args->size % PAGE_SIZE) != 0) {
883 if (args->gpa < mach->gpa_begin || args->gpa >= mach->gpa_end) {
887 if (args->gpa + args->size <= args->gpa) {
891 if (args->gpa + args->size >= mach->gpa_end) {
897 /* Unmap the memory from the machine. */
898 uvm_unmap(&mach->vm->vm_map, gpa, gpa + args->size);
901 nvmm_machine_put(mach);
905 /* -------------------------------------------------------------------------- */
908 nvmm_ctl_mach_info(struct nvmm_owner *owner, struct nvmm_ioc_ctl *args)
910 struct nvmm_ctl_mach_info ctl;
911 struct nvmm_machine *mach;
915 if (args->size != sizeof(ctl))
917 error = copyin(args->data, &ctl, sizeof(ctl));
921 error = nvmm_machine_get(owner, ctl.machid, &mach, true);
925 ctl.nvcpus = mach->ncpus;
928 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
929 if (!mach->hmap[i].present)
931 ctl.nram += mach->hmap[i].size;
934 ctl.pid = mach->owner->pid;
935 ctl.time = mach->time;
937 nvmm_machine_put(mach);
939 error = copyout(&ctl, args->data, sizeof(ctl));
947 nvmm_ctl(struct nvmm_owner *owner, struct nvmm_ioc_ctl *args)
950 case NVMM_CTL_MACH_INFO:
951 return nvmm_ctl_mach_info(owner, args);
957 /* -------------------------------------------------------------------------- */
959 static const struct nvmm_impl *
964 for (i = 0; i < __arraycount(nvmm_impl_list); i++) {
965 if ((*nvmm_impl_list[i]->ident)())
966 return nvmm_impl_list[i];
977 nvmm_impl = nvmm_ident();
978 if (nvmm_impl == NULL)
981 for (i = 0; i < NVMM_MAX_MACHINES; i++) {
982 machines[i].machid = i;
983 rw_init(&machines[i].lock);
984 for (n = 0; n < NVMM_MAX_VCPUS; n++) {
985 machines[i].cpus[n].present = false;
986 machines[i].cpus[n].cpuid = n;
987 mutex_init(&machines[i].cpus[n].lock, MUTEX_DEFAULT,
992 (*nvmm_impl->init)();
1002 for (i = 0; i < NVMM_MAX_MACHINES; i++) {
1003 rw_destroy(&machines[i].lock);
1004 for (n = 0; n < NVMM_MAX_VCPUS; n++) {
1005 mutex_destroy(&machines[i].cpus[n].lock);
1009 (*nvmm_impl->fini)();
1013 /* -------------------------------------------------------------------------- */
1015 static d_open_t nvmm_open;
1016 static d_ioctl_t nvmm_ioctl;
1017 static d_mmap_single_t nvmm_mmap_single;
1018 static d_priv_dtor_t nvmm_dtor;
1020 static struct dev_ops nvmm_ops = {
1021 { "nvmm", 0, D_MPSAFE },
1022 .d_open = nvmm_open,
1023 .d_ioctl = nvmm_ioctl,
1024 .d_mmap_single = nvmm_mmap_single,
1028 nvmm_open(struct dev_open_args *ap)
1030 int flags = ap->a_oflags;
1031 struct nvmm_owner *owner;
1035 if (__predict_false(nvmm_impl == NULL))
1037 if (!(flags & O_CLOEXEC))
1040 if (priv_check_cred(ap->a_cred, PRIV_ROOT, 0) == 0) {
1041 owner = &root_owner;
1043 owner = kmem_alloc(sizeof(*owner), KM_SLEEP);
1044 owner->pid = curthread->td_proc->p_pid;
1047 fp = ap->a_fpp ? *ap->a_fpp : NULL;
1048 error = devfs_set_cdevpriv(fp, owner, nvmm_dtor);
1058 nvmm_dtor(void *arg)
1060 struct nvmm_owner *owner = arg;
1062 KASSERT(owner != NULL);
1063 nvmm_kill_machines(owner);
1064 if (owner != &root_owner) {
1065 kmem_free(owner, sizeof(*owner));
1070 nvmm_mmap_single(struct dev_mmap_single_args *ap)
1072 vm_ooffset_t *offp = ap->a_offset;
1073 size_t size = ap->a_size;
1074 int prot = ap->a_nprot;
1075 struct vm_object **uobjp = ap->a_object;
1076 struct file *fp = ap->a_fp;
1077 struct nvmm_owner *owner = NULL;
1078 struct nvmm_machine *mach;
1079 nvmm_machid_t machid;
1083 devfs_get_cdevpriv(fp, (void **)&owner);
1084 KASSERT(owner != NULL);
1086 if (prot & PROT_EXEC)
1088 if (size != PAGE_SIZE)
1091 cpuid = NVMM_COMM_CPUID(*offp);
1092 if (__predict_false(cpuid >= NVMM_MAX_VCPUS))
1095 machid = NVMM_COMM_MACHID(*offp);
1096 error = nvmm_machine_get(owner, machid, &mach, false);
1100 uao_reference(mach->commuobj);
1101 *uobjp = mach->commuobj;
1102 *offp = cpuid * PAGE_SIZE;
1104 nvmm_machine_put(mach);
1109 nvmm_ioctl(struct dev_ioctl_args *ap)
1111 unsigned long cmd = ap->a_cmd;
1112 void *data = ap->a_data;
1113 struct file *fp = ap->a_fp;
1114 struct nvmm_owner *owner = NULL;
1116 devfs_get_cdevpriv(fp, (void **)&owner);
1117 KASSERT(owner != NULL);
1120 case NVMM_IOC_CAPABILITY:
1121 return nvmm_capability(owner, data);
1122 case NVMM_IOC_MACHINE_CREATE:
1123 return nvmm_machine_create(owner, data);
1124 case NVMM_IOC_MACHINE_DESTROY:
1125 return nvmm_machine_destroy(owner, data);
1126 case NVMM_IOC_MACHINE_CONFIGURE:
1127 return nvmm_machine_configure(owner, data);
1128 case NVMM_IOC_VCPU_CREATE:
1129 return nvmm_vcpu_create(owner, data);
1130 case NVMM_IOC_VCPU_DESTROY:
1131 return nvmm_vcpu_destroy(owner, data);
1132 case NVMM_IOC_VCPU_CONFIGURE:
1133 return nvmm_vcpu_configure(owner, data);
1134 case NVMM_IOC_VCPU_SETSTATE:
1135 return nvmm_vcpu_setstate(owner, data);
1136 case NVMM_IOC_VCPU_GETSTATE:
1137 return nvmm_vcpu_getstate(owner, data);
1138 case NVMM_IOC_VCPU_INJECT:
1139 return nvmm_vcpu_inject(owner, data);
1140 case NVMM_IOC_VCPU_RUN:
1141 return nvmm_vcpu_run(owner, data);
1142 case NVMM_IOC_GPA_MAP:
1143 return nvmm_gpa_map(owner, data);
1144 case NVMM_IOC_GPA_UNMAP:
1145 return nvmm_gpa_unmap(owner, data);
1146 case NVMM_IOC_HVA_MAP:
1147 return nvmm_hva_map(owner, data);
1148 case NVMM_IOC_HVA_UNMAP:
1149 return nvmm_hva_unmap(owner, data);
1151 return nvmm_ctl(owner, data);
1157 /* -------------------------------------------------------------------------- */
1164 error = nvmm_init();
1166 panic("%s: impossible", __func__);
1167 printf("nvmm: attached, using backend %s\n", nvmm_impl->name);
1175 if (atomic_load_acq_int(&nmachines) > 0)
1183 nvmm_modevent(module_t mod __unused, int type, void *data __unused)
1185 static cdev_t dev = NULL;
1190 if (nvmm_ident() == NULL) {
1191 printf("nvmm: cpu not supported\n");
1194 error = nvmm_attach();
1198 dev = make_dev(&nvmm_ops, 0, UID_ROOT, GID_NVMM, 0660, "nvmm");
1200 printf("nvmm: unable to create device\n");
1208 error = nvmm_detach();
1225 static moduledata_t nvmm_moddata = {
1227 .evhand = nvmm_modevent,
1231 DECLARE_MODULE(nvmm, nvmm_moddata, SI_SUB_PSEUDO, SI_ORDER_ANY);
1232 MODULE_VERSION(nvmm, NVMM_KERN_VERSION);