1 /* $NetBSD: nvmm.c,v 1.22.2.7 2020/08/29 17:00:28 martin Exp $ */
4 * Copyright (c) 2018-2020 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.22.2.7 2020/08/29 17:00:28 martin Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/atomic.h>
43 #include <sys/module.h>
47 #include <sys/filedesc.h>
48 #include <sys/device.h>
51 #include <uvm/uvm_page.h>
55 #include <dev/nvmm/nvmm.h>
56 #include <dev/nvmm/nvmm_internal.h>
57 #include <dev/nvmm/nvmm_ioctl.h>
59 MALLOC_DEFINE(M_NVMM, "nvmm", "NVMM data");
61 static struct nvmm_machine machines[NVMM_MAX_MACHINES];
62 static volatile unsigned int nmachines __cacheline_aligned;
64 static const struct nvmm_impl *nvmm_impl_list[] = {
65 #if defined(__x86_64__)
66 &nvmm_x86_svm, /* x86 AMD SVM */
67 &nvmm_x86_vmx /* x86 Intel VMX */
71 static const struct nvmm_impl *nvmm_impl = NULL;
73 static struct nvmm_owner root_owner;
75 /* -------------------------------------------------------------------------- */
78 nvmm_machine_alloc(struct nvmm_machine **ret)
80 struct nvmm_machine *mach;
83 for (i = 0; i < NVMM_MAX_MACHINES; i++) {
86 rw_enter(&mach->lock, RW_WRITER);
93 mach->time = time_second;
95 atomic_inc_uint(&nmachines);
103 nvmm_machine_free(struct nvmm_machine *mach)
105 KASSERT(rw_write_held(&mach->lock));
106 KASSERT(mach->present);
107 mach->present = false;
108 atomic_dec_uint(&nmachines);
112 nvmm_machine_get(struct nvmm_owner *owner, nvmm_machid_t machid,
113 struct nvmm_machine **ret, bool writer)
115 struct nvmm_machine *mach;
116 krw_t op = writer ? RW_WRITER : RW_READER;
118 if (__predict_false(machid >= NVMM_MAX_MACHINES)) {
121 mach = &machines[machid];
123 rw_enter(&mach->lock, op);
124 if (__predict_false(!mach->present)) {
125 rw_exit(&mach->lock);
128 if (__predict_false(mach->owner != owner && owner != &root_owner)) {
129 rw_exit(&mach->lock);
138 nvmm_machine_put(struct nvmm_machine *mach)
140 rw_exit(&mach->lock);
143 /* -------------------------------------------------------------------------- */
146 nvmm_vcpu_alloc(struct nvmm_machine *mach, nvmm_cpuid_t cpuid,
147 struct nvmm_cpu **ret)
149 struct nvmm_cpu *vcpu;
151 if (cpuid >= NVMM_MAX_VCPUS) {
154 vcpu = &mach->cpus[cpuid];
156 mutex_enter(&vcpu->lock);
158 mutex_exit(&vcpu->lock);
162 vcpu->present = true;
164 vcpu->hcpu_last = -1;
170 nvmm_vcpu_free(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
172 KASSERT(mutex_owned(&vcpu->lock));
173 vcpu->present = false;
174 if (vcpu->comm != NULL) {
175 uvm_deallocate(kernel_map, (vaddr_t)vcpu->comm, PAGE_SIZE);
180 nvmm_vcpu_get(struct nvmm_machine *mach, nvmm_cpuid_t cpuid,
181 struct nvmm_cpu **ret)
183 struct nvmm_cpu *vcpu;
185 if (__predict_false(cpuid >= NVMM_MAX_VCPUS)) {
188 vcpu = &mach->cpus[cpuid];
190 mutex_enter(&vcpu->lock);
191 if (__predict_false(!vcpu->present)) {
192 mutex_exit(&vcpu->lock);
201 nvmm_vcpu_put(struct nvmm_cpu *vcpu)
203 mutex_exit(&vcpu->lock);
206 /* -------------------------------------------------------------------------- */
209 nvmm_kill_machines(struct nvmm_owner *owner)
211 struct nvmm_machine *mach;
212 struct nvmm_cpu *vcpu;
216 for (i = 0; i < NVMM_MAX_MACHINES; i++) {
219 rw_enter(&mach->lock, RW_WRITER);
220 if (!mach->present || mach->owner != owner) {
221 rw_exit(&mach->lock);
226 for (j = 0; j < NVMM_MAX_VCPUS; j++) {
227 error = nvmm_vcpu_get(mach, j, &vcpu);
230 (*nvmm_impl->vcpu_destroy)(mach, vcpu);
231 nvmm_vcpu_free(mach, vcpu);
233 atomic_dec_uint(&mach->ncpus);
235 (*nvmm_impl->machine_destroy)(mach);
236 uvmspace_free(mach->vm);
238 /* Drop the kernel UOBJ refs. */
239 for (j = 0; j < NVMM_MAX_HMAPPINGS; j++) {
240 if (!mach->hmap[j].present)
242 uao_detach(mach->hmap[j].uobj);
245 nvmm_machine_free(mach);
247 rw_exit(&mach->lock);
251 /* -------------------------------------------------------------------------- */
254 nvmm_capability(struct nvmm_owner *owner, struct nvmm_ioc_capability *args)
256 args->cap.version = NVMM_KERN_VERSION;
257 args->cap.state_size = nvmm_impl->state_size;
258 args->cap.max_machines = NVMM_MAX_MACHINES;
259 args->cap.max_vcpus = NVMM_MAX_VCPUS;
260 args->cap.max_ram = NVMM_MAX_RAM;
262 (*nvmm_impl->capability)(&args->cap);
268 nvmm_machine_create(struct nvmm_owner *owner,
269 struct nvmm_ioc_machine_create *args)
271 struct nvmm_machine *mach;
274 error = nvmm_machine_alloc(&mach);
278 /* Curproc owns the machine. */
281 /* Zero out the host mappings. */
282 memset(&mach->hmap, 0, sizeof(mach->hmap));
284 /* Create the machine vmspace. */
286 mach->gpa_end = NVMM_MAX_RAM;
287 mach->vm = uvmspace_alloc(0, mach->gpa_end - mach->gpa_begin, false);
289 /* Create the comm uobj. */
290 mach->commuobj = uao_create(NVMM_MAX_VCPUS * PAGE_SIZE, 0);
292 (*nvmm_impl->machine_create)(mach);
294 args->machid = mach->machid;
295 nvmm_machine_put(mach);
301 nvmm_machine_destroy(struct nvmm_owner *owner,
302 struct nvmm_ioc_machine_destroy *args)
304 struct nvmm_machine *mach;
305 struct nvmm_cpu *vcpu;
309 error = nvmm_machine_get(owner, args->machid, &mach, true);
313 for (i = 0; i < NVMM_MAX_VCPUS; i++) {
314 error = nvmm_vcpu_get(mach, i, &vcpu);
318 (*nvmm_impl->vcpu_destroy)(mach, vcpu);
319 nvmm_vcpu_free(mach, vcpu);
321 atomic_dec_uint(&mach->ncpus);
324 (*nvmm_impl->machine_destroy)(mach);
326 /* Free the machine vmspace. */
327 uvmspace_free(mach->vm);
329 /* Drop the kernel UOBJ refs. */
330 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
331 if (!mach->hmap[i].present)
333 uao_detach(mach->hmap[i].uobj);
336 nvmm_machine_free(mach);
337 nvmm_machine_put(mach);
343 nvmm_machine_configure(struct nvmm_owner *owner,
344 struct nvmm_ioc_machine_configure *args)
346 struct nvmm_machine *mach;
352 op = NVMM_MACH_CONF_MD(args->op);
353 if (__predict_false(op >= nvmm_impl->mach_conf_max)) {
357 allocsz = nvmm_impl->mach_conf_sizes[op];
358 data = kmem_alloc(allocsz, KM_SLEEP);
360 error = nvmm_machine_get(owner, args->machid, &mach, true);
362 kmem_free(data, allocsz);
366 error = copyin(args->conf, data, allocsz);
371 error = (*nvmm_impl->machine_configure)(mach, op, data);
374 nvmm_machine_put(mach);
375 kmem_free(data, allocsz);
380 nvmm_vcpu_create(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_create *args)
382 struct nvmm_machine *mach;
383 struct nvmm_cpu *vcpu;
386 error = nvmm_machine_get(owner, args->machid, &mach, false);
390 error = nvmm_vcpu_alloc(mach, args->cpuid, &vcpu);
394 /* Allocate the comm page. */
395 uao_reference(mach->commuobj);
396 error = uvm_map(kernel_map, (vaddr_t *)&vcpu->comm, PAGE_SIZE,
397 mach->commuobj, args->cpuid * PAGE_SIZE, 0, UVM_MAPFLAG(UVM_PROT_RW,
398 UVM_PROT_RW, UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
400 uao_detach(mach->commuobj);
401 nvmm_vcpu_free(mach, vcpu);
405 error = uvm_map_pageable(kernel_map, (vaddr_t)vcpu->comm,
406 (vaddr_t)vcpu->comm + PAGE_SIZE, false, 0);
408 nvmm_vcpu_free(mach, vcpu);
412 memset(vcpu->comm, 0, PAGE_SIZE);
414 error = (*nvmm_impl->vcpu_create)(mach, vcpu);
416 nvmm_vcpu_free(mach, vcpu);
422 atomic_inc_uint(&mach->ncpus);
425 nvmm_machine_put(mach);
430 nvmm_vcpu_destroy(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_destroy *args)
432 struct nvmm_machine *mach;
433 struct nvmm_cpu *vcpu;
436 error = nvmm_machine_get(owner, args->machid, &mach, false);
440 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
444 (*nvmm_impl->vcpu_destroy)(mach, vcpu);
445 nvmm_vcpu_free(mach, vcpu);
447 atomic_dec_uint(&mach->ncpus);
450 nvmm_machine_put(mach);
455 nvmm_vcpu_configure(struct nvmm_owner *owner,
456 struct nvmm_ioc_vcpu_configure *args)
458 struct nvmm_machine *mach;
459 struct nvmm_cpu *vcpu;
465 op = NVMM_VCPU_CONF_MD(args->op);
466 if (__predict_false(op >= nvmm_impl->vcpu_conf_max))
469 allocsz = nvmm_impl->vcpu_conf_sizes[op];
470 data = kmem_alloc(allocsz, KM_SLEEP);
472 error = nvmm_machine_get(owner, args->machid, &mach, false);
474 kmem_free(data, allocsz);
478 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
480 nvmm_machine_put(mach);
481 kmem_free(data, allocsz);
485 error = copyin(args->conf, data, allocsz);
490 error = (*nvmm_impl->vcpu_configure)(vcpu, op, data);
494 nvmm_machine_put(mach);
495 kmem_free(data, allocsz);
500 nvmm_vcpu_setstate(struct nvmm_owner *owner,
501 struct nvmm_ioc_vcpu_setstate *args)
503 struct nvmm_machine *mach;
504 struct nvmm_cpu *vcpu;
507 error = nvmm_machine_get(owner, args->machid, &mach, false);
511 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
515 (*nvmm_impl->vcpu_setstate)(vcpu);
519 nvmm_machine_put(mach);
524 nvmm_vcpu_getstate(struct nvmm_owner *owner,
525 struct nvmm_ioc_vcpu_getstate *args)
527 struct nvmm_machine *mach;
528 struct nvmm_cpu *vcpu;
531 error = nvmm_machine_get(owner, args->machid, &mach, false);
535 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
539 (*nvmm_impl->vcpu_getstate)(vcpu);
543 nvmm_machine_put(mach);
548 nvmm_vcpu_inject(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_inject *args)
550 struct nvmm_machine *mach;
551 struct nvmm_cpu *vcpu;
554 error = nvmm_machine_get(owner, args->machid, &mach, false);
558 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
562 error = (*nvmm_impl->vcpu_inject)(vcpu);
566 nvmm_machine_put(mach);
571 nvmm_do_vcpu_run(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
572 struct nvmm_vcpu_exit *exit)
574 struct vmspace *vm = mach->vm;
578 /* Got a signal? Or pending resched? Leave. */
579 if (__predict_false(nvmm_return_needed())) {
580 exit->reason = NVMM_VCPU_EXIT_NONE;
585 ret = (*nvmm_impl->vcpu_run)(mach, vcpu, exit);
586 if (__predict_false(ret != 0)) {
590 /* Process nested page faults. */
591 if (__predict_true(exit->reason != NVMM_VCPU_EXIT_MEMORY)) {
594 if (exit->u.mem.gpa >= mach->gpa_end) {
597 if (uvm_fault(&vm->vm_map, exit->u.mem.gpa, exit->u.mem.prot)) {
606 nvmm_vcpu_run(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_run *args)
608 struct nvmm_machine *mach;
609 struct nvmm_cpu *vcpu;
612 error = nvmm_machine_get(owner, args->machid, &mach, false);
616 error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
620 error = nvmm_do_vcpu_run(mach, vcpu, &args->exit);
624 nvmm_machine_put(mach);
628 /* -------------------------------------------------------------------------- */
630 static struct uvm_object *
631 nvmm_hmapping_getuobj(struct nvmm_machine *mach, uintptr_t hva, size_t size,
634 struct nvmm_hmapping *hmapping;
637 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
638 hmapping = &mach->hmap[i];
639 if (!hmapping->present) {
642 if (hva >= hmapping->hva &&
643 hva + size <= hmapping->hva + hmapping->size) {
644 *off = hva - hmapping->hva;
645 return hmapping->uobj;
653 nvmm_hmapping_validate(struct nvmm_machine *mach, uintptr_t hva, size_t size)
655 struct nvmm_hmapping *hmapping;
658 if ((hva % PAGE_SIZE) != 0 || (size % PAGE_SIZE) != 0) {
665 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
666 hmapping = &mach->hmap[i];
667 if (!hmapping->present) {
671 if (hva >= hmapping->hva &&
672 hva + size <= hmapping->hva + hmapping->size) {
676 if (hva >= hmapping->hva &&
677 hva < hmapping->hva + hmapping->size) {
680 if (hva + size > hmapping->hva &&
681 hva + size <= hmapping->hva + hmapping->size) {
684 if (hva <= hmapping->hva &&
685 hva + size >= hmapping->hva + hmapping->size) {
693 static struct nvmm_hmapping *
694 nvmm_hmapping_alloc(struct nvmm_machine *mach)
696 struct nvmm_hmapping *hmapping;
699 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
700 hmapping = &mach->hmap[i];
701 if (!hmapping->present) {
702 hmapping->present = true;
711 nvmm_hmapping_free(struct nvmm_machine *mach, uintptr_t hva, size_t size)
713 struct vmspace *vmspace = curproc->p_vmspace;
714 struct nvmm_hmapping *hmapping;
717 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
718 hmapping = &mach->hmap[i];
719 if (!hmapping->present || hmapping->hva != hva ||
720 hmapping->size != size) {
724 uvm_unmap(&vmspace->vm_map, hmapping->hva,
725 hmapping->hva + hmapping->size);
726 uao_detach(hmapping->uobj);
728 hmapping->uobj = NULL;
729 hmapping->present = false;
738 nvmm_hva_map(struct nvmm_owner *owner, struct nvmm_ioc_hva_map *args)
740 struct vmspace *vmspace = curproc->p_vmspace;
741 struct nvmm_machine *mach;
742 struct nvmm_hmapping *hmapping;
746 error = nvmm_machine_get(owner, args->machid, &mach, true);
750 error = nvmm_hmapping_validate(mach, args->hva, args->size);
754 hmapping = nvmm_hmapping_alloc(mach);
755 if (hmapping == NULL) {
760 hmapping->hva = args->hva;
761 hmapping->size = args->size;
762 hmapping->uobj = uao_create(hmapping->size, 0);
765 /* Take a reference for the user. */
766 uao_reference(hmapping->uobj);
768 /* Map the uobj into the user address space, as pageable. */
769 error = uvm_map(&vmspace->vm_map, &uva, hmapping->size, hmapping->uobj,
770 0, 0, UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_SHARE,
771 UVM_ADV_RANDOM, UVM_FLAG_FIXED|UVM_FLAG_UNMAP));
773 uao_detach(hmapping->uobj);
777 nvmm_machine_put(mach);
782 nvmm_hva_unmap(struct nvmm_owner *owner, struct nvmm_ioc_hva_unmap *args)
784 struct nvmm_machine *mach;
787 error = nvmm_machine_get(owner, args->machid, &mach, true);
791 error = nvmm_hmapping_free(mach, args->hva, args->size);
793 nvmm_machine_put(mach);
797 /* -------------------------------------------------------------------------- */
800 nvmm_gpa_map(struct nvmm_owner *owner, struct nvmm_ioc_gpa_map *args)
802 struct nvmm_machine *mach;
803 struct uvm_object *uobj;
808 error = nvmm_machine_get(owner, args->machid, &mach, false);
812 if ((args->prot & ~(PROT_READ|PROT_WRITE|PROT_EXEC)) != 0) {
817 if ((args->gpa % PAGE_SIZE) != 0 || (args->size % PAGE_SIZE) != 0 ||
818 (args->hva % PAGE_SIZE) != 0) {
822 if (args->hva == 0) {
826 if (args->gpa < mach->gpa_begin || args->gpa >= mach->gpa_end) {
830 if (args->gpa + args->size <= args->gpa) {
834 if (args->gpa + args->size > mach->gpa_end) {
840 uobj = nvmm_hmapping_getuobj(mach, args->hva, args->size, &off);
846 /* Take a reference for the machine. */
849 /* Map the uobj into the machine address space, as pageable. */
850 error = uvm_map(&mach->vm->vm_map, &gpa, args->size, uobj, off, 0,
851 UVM_MAPFLAG(args->prot, UVM_PROT_RWX, UVM_INH_NONE,
852 UVM_ADV_RANDOM, UVM_FLAG_FIXED|UVM_FLAG_UNMAP));
857 if (gpa != args->gpa) {
859 printf("[!] uvm_map problem\n");
865 nvmm_machine_put(mach);
870 nvmm_gpa_unmap(struct nvmm_owner *owner, struct nvmm_ioc_gpa_unmap *args)
872 struct nvmm_machine *mach;
876 error = nvmm_machine_get(owner, args->machid, &mach, false);
880 if ((args->gpa % PAGE_SIZE) != 0 || (args->size % PAGE_SIZE) != 0) {
884 if (args->gpa < mach->gpa_begin || args->gpa >= mach->gpa_end) {
888 if (args->gpa + args->size <= args->gpa) {
892 if (args->gpa + args->size >= mach->gpa_end) {
898 /* Unmap the memory from the machine. */
899 uvm_unmap(&mach->vm->vm_map, gpa, gpa + args->size);
902 nvmm_machine_put(mach);
906 /* -------------------------------------------------------------------------- */
909 nvmm_ctl_mach_info(struct nvmm_owner *owner, struct nvmm_ioc_ctl *args)
911 struct nvmm_ctl_mach_info ctl;
912 struct nvmm_machine *mach;
916 if (args->size != sizeof(ctl))
918 error = copyin(args->data, &ctl, sizeof(ctl));
922 error = nvmm_machine_get(owner, ctl.machid, &mach, true);
926 ctl.nvcpus = mach->ncpus;
929 for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
930 if (!mach->hmap[i].present)
932 ctl.nram += mach->hmap[i].size;
935 ctl.pid = mach->owner->pid;
936 ctl.time = mach->time;
938 nvmm_machine_put(mach);
940 error = copyout(&ctl, args->data, sizeof(ctl));
948 nvmm_ctl(struct nvmm_owner *owner, struct nvmm_ioc_ctl *args)
951 case NVMM_CTL_MACH_INFO:
952 return nvmm_ctl_mach_info(owner, args);
958 /* -------------------------------------------------------------------------- */
960 static const struct nvmm_impl *
965 for (i = 0; i < __arraycount(nvmm_impl_list); i++) {
966 if ((*nvmm_impl_list[i]->ident)())
967 return nvmm_impl_list[i];
978 nvmm_impl = nvmm_ident();
979 if (nvmm_impl == NULL)
982 for (i = 0; i < NVMM_MAX_MACHINES; i++) {
983 machines[i].machid = i;
984 rw_init(&machines[i].lock);
985 for (n = 0; n < NVMM_MAX_VCPUS; n++) {
986 machines[i].cpus[n].present = false;
987 machines[i].cpus[n].cpuid = n;
988 mutex_init(&machines[i].cpus[n].lock, MUTEX_DEFAULT,
993 (*nvmm_impl->init)();
1003 for (i = 0; i < NVMM_MAX_MACHINES; i++) {
1004 rw_destroy(&machines[i].lock);
1005 for (n = 0; n < NVMM_MAX_VCPUS; n++) {
1006 mutex_destroy(&machines[i].cpus[n].lock);
1010 (*nvmm_impl->fini)();
1014 /* -------------------------------------------------------------------------- */
1016 static dev_type_open(nvmm_open);
1018 const struct cdevsw nvmm_cdevsw = {
1019 .d_open = nvmm_open,
1028 .d_kqfilter = nokqfilter,
1029 .d_discard = nodiscard,
1030 .d_flag = D_OTHER | D_MPSAFE
1033 static int nvmm_ioctl(file_t *, u_long, void *);
1034 static int nvmm_close(file_t *);
1035 static int nvmm_mmap(file_t *, off_t *, size_t, int, int *, int *,
1036 struct uvm_object **, int *);
1038 static const struct fileops nvmm_fileops = {
1039 .fo_read = fbadop_read,
1040 .fo_write = fbadop_write,
1041 .fo_ioctl = nvmm_ioctl,
1042 .fo_fcntl = fnullop_fcntl,
1043 .fo_poll = fnullop_poll,
1044 .fo_stat = fbadop_stat,
1045 .fo_close = nvmm_close,
1046 .fo_kqfilter = fnullop_kqfilter,
1047 .fo_restart = fnullop_restart,
1048 .fo_mmap = nvmm_mmap,
1052 nvmm_open(dev_t dev, int flags, int type, struct lwp *l)
1054 struct nvmm_owner *owner;
1058 if (__predict_false(nvmm_impl == NULL))
1060 if (minor(dev) != 0)
1062 if (!(flags & O_CLOEXEC))
1064 error = fd_allocfile(&fp, &fd);
1068 if (OFLAGS(flags) & O_WRONLY) {
1069 owner = &root_owner;
1071 owner = kmem_alloc(sizeof(*owner), KM_SLEEP);
1072 owner->pid = l->l_proc->p_pid;
1075 return fd_clone(fp, fd, flags, &nvmm_fileops, owner);
1079 nvmm_close(file_t *fp)
1081 struct nvmm_owner *owner = fp->f_data;
1083 KASSERT(owner != NULL);
1084 nvmm_kill_machines(owner);
1085 if (owner != &root_owner) {
1086 kmem_free(owner, sizeof(*owner));
1094 nvmm_mmap(file_t *fp, off_t *offp, size_t size, int prot, int *flagsp,
1095 int *advicep, struct uvm_object **uobjp, int *maxprotp)
1097 struct nvmm_owner *owner = fp->f_data;
1098 struct nvmm_machine *mach;
1099 nvmm_machid_t machid;
1103 if (prot & PROT_EXEC)
1105 if (size != PAGE_SIZE)
1108 cpuid = NVMM_COMM_CPUID(*offp);
1109 if (__predict_false(cpuid >= NVMM_MAX_VCPUS))
1112 machid = NVMM_COMM_MACHID(*offp);
1113 error = nvmm_machine_get(owner, machid, &mach, false);
1117 uao_reference(mach->commuobj);
1118 *uobjp = mach->commuobj;
1119 *offp = cpuid * PAGE_SIZE;
1121 *advicep = UVM_ADV_RANDOM;
1123 nvmm_machine_put(mach);
1128 nvmm_ioctl(file_t *fp, u_long cmd, void *data)
1130 struct nvmm_owner *owner = fp->f_data;
1132 KASSERT(owner != NULL);
1135 case NVMM_IOC_CAPABILITY:
1136 return nvmm_capability(owner, data);
1137 case NVMM_IOC_MACHINE_CREATE:
1138 return nvmm_machine_create(owner, data);
1139 case NVMM_IOC_MACHINE_DESTROY:
1140 return nvmm_machine_destroy(owner, data);
1141 case NVMM_IOC_MACHINE_CONFIGURE:
1142 return nvmm_machine_configure(owner, data);
1143 case NVMM_IOC_VCPU_CREATE:
1144 return nvmm_vcpu_create(owner, data);
1145 case NVMM_IOC_VCPU_DESTROY:
1146 return nvmm_vcpu_destroy(owner, data);
1147 case NVMM_IOC_VCPU_CONFIGURE:
1148 return nvmm_vcpu_configure(owner, data);
1149 case NVMM_IOC_VCPU_SETSTATE:
1150 return nvmm_vcpu_setstate(owner, data);
1151 case NVMM_IOC_VCPU_GETSTATE:
1152 return nvmm_vcpu_getstate(owner, data);
1153 case NVMM_IOC_VCPU_INJECT:
1154 return nvmm_vcpu_inject(owner, data);
1155 case NVMM_IOC_VCPU_RUN:
1156 return nvmm_vcpu_run(owner, data);
1157 case NVMM_IOC_GPA_MAP:
1158 return nvmm_gpa_map(owner, data);
1159 case NVMM_IOC_GPA_UNMAP:
1160 return nvmm_gpa_unmap(owner, data);
1161 case NVMM_IOC_HVA_MAP:
1162 return nvmm_hva_map(owner, data);
1163 case NVMM_IOC_HVA_UNMAP:
1164 return nvmm_hva_unmap(owner, data);
1166 return nvmm_ctl(owner, data);
1172 /* -------------------------------------------------------------------------- */
1174 static int nvmm_match(device_t, cfdata_t, void *);
1175 static void nvmm_attach(device_t, device_t, void *);
1176 static int nvmm_detach(device_t, int);
1178 extern struct cfdriver nvmm_cd;
1180 CFATTACH_DECL_NEW(nvmm, 0, nvmm_match, nvmm_attach, nvmm_detach, NULL);
1182 static struct cfdata nvmm_cfdata[] = {
1185 .cf_atname = "nvmm",
1187 .cf_fstate = FSTATE_STAR,
1192 { NULL, NULL, 0, FSTATE_NOTFOUND, NULL, 0, NULL }
1196 nvmm_match(device_t self, cfdata_t cfdata, void *arg)
1202 nvmm_attach(device_t parent, device_t self, void *aux)
1206 error = nvmm_init();
1208 panic("%s: impossible", __func__);
1209 aprint_normal_dev(self, "attached, using backend %s\n",
1214 nvmm_detach(device_t self, int flags)
1216 if (atomic_load_acq_int(&nmachines) > 0)
1223 nvmmattach(int nunits)
1228 MODULE(MODULE_CLASS_MISC, nvmm, NULL);
1230 #if defined(_MODULE)
1231 CFDRIVER_DECL(nvmm, DV_VIRTUAL, NULL);
1235 nvmm_modcmd(modcmd_t cmd, void *arg)
1237 #if defined(_MODULE)
1238 devmajor_t bmajor = NODEVMAJOR;
1239 devmajor_t cmajor = 345;
1244 case MODULE_CMD_INIT:
1245 if (nvmm_ident() == NULL) {
1246 aprint_error("%s: cpu not supported\n",
1250 #if defined(_MODULE)
1251 error = config_cfdriver_attach(&nvmm_cd);
1255 error = config_cfattach_attach(nvmm_cd.cd_name, &nvmm_ca);
1257 config_cfdriver_detach(&nvmm_cd);
1258 aprint_error("%s: config_cfattach_attach failed\n",
1263 error = config_cfdata_attach(nvmm_cfdata, 1);
1265 config_cfattach_detach(nvmm_cd.cd_name, &nvmm_ca);
1266 config_cfdriver_detach(&nvmm_cd);
1267 aprint_error("%s: unable to register cfdata\n",
1272 if (config_attach_pseudo(nvmm_cfdata) == NULL) {
1273 aprint_error("%s: config_attach_pseudo failed\n",
1275 config_cfattach_detach(nvmm_cd.cd_name, &nvmm_ca);
1276 config_cfdriver_detach(&nvmm_cd);
1280 #if defined(_MODULE)
1281 /* mknod /dev/nvmm c 345 0 */
1282 error = devsw_attach(nvmm_cd.cd_name, NULL, &bmajor,
1283 &nvmm_cdevsw, &cmajor);
1285 aprint_error("%s: unable to register devsw\n",
1287 config_cfattach_detach(nvmm_cd.cd_name, &nvmm_ca);
1288 config_cfdriver_detach(&nvmm_cd);
1293 case MODULE_CMD_FINI:
1294 error = config_cfdata_detach(nvmm_cfdata);
1297 error = config_cfattach_detach(nvmm_cd.cd_name, &nvmm_ca);
1300 #if defined(_MODULE)
1301 config_cfdriver_detach(&nvmm_cd);
1302 devsw_detach(NULL, &nvmm_cdevsw);
1305 case MODULE_CMD_AUTOUNLOAD: