Home | History | Annotate | Line # | Download | only in nvmm
nvmm.c revision 1.21
      1 /*	$NetBSD: nvmm.c,v 1.21 2019/05/11 07:31:56 maxv Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2018-2019 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Maxime Villard.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     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.
     18  *
     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.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.21 2019/05/11 07:31:56 maxv Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 
     39 #include <sys/cpu.h>
     40 #include <sys/conf.h>
     41 #include <sys/kmem.h>
     42 #include <sys/module.h>
     43 #include <sys/proc.h>
     44 #include <sys/mman.h>
     45 #include <sys/file.h>
     46 #include <sys/filedesc.h>
     47 #include <sys/kauth.h>
     48 
     49 #include <uvm/uvm.h>
     50 #include <uvm/uvm_page.h>
     51 
     52 #include "ioconf.h"
     53 
     54 #include <dev/nvmm/nvmm.h>
     55 #include <dev/nvmm/nvmm_internal.h>
     56 #include <dev/nvmm/nvmm_ioctl.h>
     57 
     58 static struct nvmm_machine machines[NVMM_MAX_MACHINES];
     59 static volatile unsigned int nmachines __cacheline_aligned;
     60 
     61 static const struct nvmm_impl *nvmm_impl_list[] = {
     62 	&nvmm_x86_svm,	/* x86 AMD SVM */
     63 	&nvmm_x86_vmx	/* x86 Intel VMX */
     64 };
     65 
     66 static const struct nvmm_impl *nvmm_impl = NULL;
     67 
     68 static struct nvmm_owner root_owner;
     69 
     70 /* -------------------------------------------------------------------------- */
     71 
     72 static int
     73 nvmm_machine_alloc(struct nvmm_machine **ret)
     74 {
     75 	struct nvmm_machine *mach;
     76 	size_t i;
     77 
     78 	for (i = 0; i < NVMM_MAX_MACHINES; i++) {
     79 		mach = &machines[i];
     80 
     81 		rw_enter(&mach->lock, RW_WRITER);
     82 		if (mach->present) {
     83 			rw_exit(&mach->lock);
     84 			continue;
     85 		}
     86 
     87 		mach->present = true;
     88 		mach->time = time_second;
     89 		*ret = mach;
     90 		atomic_inc_uint(&nmachines);
     91 		return 0;
     92 	}
     93 
     94 	return ENOBUFS;
     95 }
     96 
     97 static void
     98 nvmm_machine_free(struct nvmm_machine *mach)
     99 {
    100 	KASSERT(rw_write_held(&mach->lock));
    101 	KASSERT(mach->present);
    102 	mach->present = false;
    103 	atomic_dec_uint(&nmachines);
    104 }
    105 
    106 static int
    107 nvmm_machine_get(struct nvmm_owner *owner, nvmm_machid_t machid,
    108     struct nvmm_machine **ret, bool writer)
    109 {
    110 	struct nvmm_machine *mach;
    111 	krw_t op = writer ? RW_WRITER : RW_READER;
    112 
    113 	if (machid >= NVMM_MAX_MACHINES) {
    114 		return EINVAL;
    115 	}
    116 	mach = &machines[machid];
    117 
    118 	rw_enter(&mach->lock, op);
    119 	if (!mach->present) {
    120 		rw_exit(&mach->lock);
    121 		return ENOENT;
    122 	}
    123 	if (owner != &root_owner && mach->owner != owner) {
    124 		rw_exit(&mach->lock);
    125 		return EPERM;
    126 	}
    127 	*ret = mach;
    128 
    129 	return 0;
    130 }
    131 
    132 static void
    133 nvmm_machine_put(struct nvmm_machine *mach)
    134 {
    135 	rw_exit(&mach->lock);
    136 }
    137 
    138 /* -------------------------------------------------------------------------- */
    139 
    140 static int
    141 nvmm_vcpu_alloc(struct nvmm_machine *mach, nvmm_cpuid_t cpuid,
    142     struct nvmm_cpu **ret)
    143 {
    144 	struct nvmm_cpu *vcpu;
    145 
    146 	if (cpuid >= NVMM_MAX_VCPUS) {
    147 		return EINVAL;
    148 	}
    149 	vcpu = &mach->cpus[cpuid];
    150 
    151 	mutex_enter(&vcpu->lock);
    152 	if (vcpu->present) {
    153 		mutex_exit(&vcpu->lock);
    154 		return EBUSY;
    155 	}
    156 
    157 	vcpu->present = true;
    158 	vcpu->comm = NULL;
    159 	vcpu->hcpu_last = -1;
    160 	*ret = vcpu;
    161 	return 0;
    162 }
    163 
    164 static void
    165 nvmm_vcpu_free(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
    166 {
    167 	KASSERT(mutex_owned(&vcpu->lock));
    168 	vcpu->present = false;
    169 	if (vcpu->comm != NULL) {
    170 		uvm_deallocate(kernel_map, (vaddr_t)vcpu->comm, PAGE_SIZE);
    171 	}
    172 }
    173 
    174 int
    175 nvmm_vcpu_get(struct nvmm_machine *mach, nvmm_cpuid_t cpuid,
    176     struct nvmm_cpu **ret)
    177 {
    178 	struct nvmm_cpu *vcpu;
    179 
    180 	if (cpuid >= NVMM_MAX_VCPUS) {
    181 		return EINVAL;
    182 	}
    183 	vcpu = &mach->cpus[cpuid];
    184 
    185 	mutex_enter(&vcpu->lock);
    186 	if (!vcpu->present) {
    187 		mutex_exit(&vcpu->lock);
    188 		return ENOENT;
    189 	}
    190 	*ret = vcpu;
    191 
    192 	return 0;
    193 }
    194 
    195 void
    196 nvmm_vcpu_put(struct nvmm_cpu *vcpu)
    197 {
    198 	mutex_exit(&vcpu->lock);
    199 }
    200 
    201 /* -------------------------------------------------------------------------- */
    202 
    203 static void
    204 nvmm_kill_machines(struct nvmm_owner *owner)
    205 {
    206 	struct nvmm_machine *mach;
    207 	struct nvmm_cpu *vcpu;
    208 	size_t i, j;
    209 	int error;
    210 
    211 	for (i = 0; i < NVMM_MAX_MACHINES; i++) {
    212 		mach = &machines[i];
    213 
    214 		rw_enter(&mach->lock, RW_WRITER);
    215 		if (!mach->present || mach->owner != owner) {
    216 			rw_exit(&mach->lock);
    217 			continue;
    218 		}
    219 
    220 		/* Kill it. */
    221 		for (j = 0; j < NVMM_MAX_VCPUS; j++) {
    222 			error = nvmm_vcpu_get(mach, j, &vcpu);
    223 			if (error)
    224 				continue;
    225 			(*nvmm_impl->vcpu_destroy)(mach, vcpu);
    226 			nvmm_vcpu_free(mach, vcpu);
    227 			nvmm_vcpu_put(vcpu);
    228 		}
    229 		(*nvmm_impl->machine_destroy)(mach);
    230 		uvmspace_free(mach->vm);
    231 
    232 		/* Drop the kernel UOBJ refs. */
    233 		for (j = 0; j < NVMM_MAX_HMAPPINGS; j++) {
    234 			if (!mach->hmap[j].present)
    235 				continue;
    236 			uao_detach(mach->hmap[j].uobj);
    237 		}
    238 
    239 		nvmm_machine_free(mach);
    240 
    241 		rw_exit(&mach->lock);
    242 	}
    243 }
    244 
    245 /* -------------------------------------------------------------------------- */
    246 
    247 static int
    248 nvmm_capability(struct nvmm_owner *owner, struct nvmm_ioc_capability *args)
    249 {
    250 	args->cap.version = NVMM_CAPABILITY_VERSION;
    251 	args->cap.state_size = nvmm_impl->state_size;
    252 	args->cap.max_machines = NVMM_MAX_MACHINES;
    253 	args->cap.max_vcpus = NVMM_MAX_VCPUS;
    254 	args->cap.max_ram = NVMM_MAX_RAM;
    255 
    256 	(*nvmm_impl->capability)(&args->cap);
    257 
    258 	return 0;
    259 }
    260 
    261 static int
    262 nvmm_machine_create(struct nvmm_owner *owner,
    263     struct nvmm_ioc_machine_create *args)
    264 {
    265 	struct nvmm_machine *mach;
    266 	int error;
    267 
    268 	error = nvmm_machine_alloc(&mach);
    269 	if (error)
    270 		return error;
    271 
    272 	/* Curproc owns the machine. */
    273 	mach->owner = owner;
    274 
    275 	/* Zero out the host mappings. */
    276 	memset(&mach->hmap, 0, sizeof(mach->hmap));
    277 
    278 	/* Create the machine vmspace. */
    279 	mach->gpa_begin = 0;
    280 	mach->gpa_end = NVMM_MAX_RAM;
    281 	mach->vm = uvmspace_alloc(0, mach->gpa_end - mach->gpa_begin, false);
    282 
    283 	/* Create the comm uobj. */
    284 	mach->commuobj = uao_create(NVMM_MAX_VCPUS * PAGE_SIZE, 0);
    285 
    286 	(*nvmm_impl->machine_create)(mach);
    287 
    288 	args->machid = mach->machid;
    289 	nvmm_machine_put(mach);
    290 
    291 	return 0;
    292 }
    293 
    294 static int
    295 nvmm_machine_destroy(struct nvmm_owner *owner,
    296     struct nvmm_ioc_machine_destroy *args)
    297 {
    298 	struct nvmm_machine *mach;
    299 	struct nvmm_cpu *vcpu;
    300 	int error;
    301 	size_t i;
    302 
    303 	error = nvmm_machine_get(owner, args->machid, &mach, true);
    304 	if (error)
    305 		return error;
    306 
    307 	for (i = 0; i < NVMM_MAX_VCPUS; i++) {
    308 		error = nvmm_vcpu_get(mach, i, &vcpu);
    309 		if (error)
    310 			continue;
    311 
    312 		(*nvmm_impl->vcpu_destroy)(mach, vcpu);
    313 		nvmm_vcpu_free(mach, vcpu);
    314 		nvmm_vcpu_put(vcpu);
    315 	}
    316 
    317 	(*nvmm_impl->machine_destroy)(mach);
    318 
    319 	/* Free the machine vmspace. */
    320 	uvmspace_free(mach->vm);
    321 
    322 	/* Drop the kernel UOBJ refs. */
    323 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
    324 		if (!mach->hmap[i].present)
    325 			continue;
    326 		uao_detach(mach->hmap[i].uobj);
    327 	}
    328 
    329 	nvmm_machine_free(mach);
    330 	nvmm_machine_put(mach);
    331 
    332 	return 0;
    333 }
    334 
    335 static int
    336 nvmm_machine_configure(struct nvmm_owner *owner,
    337     struct nvmm_ioc_machine_configure *args)
    338 {
    339 	struct nvmm_machine *mach;
    340 	size_t allocsz;
    341 	uint64_t op;
    342 	void *data;
    343 	int error;
    344 
    345 	op = NVMM_MACH_CONF_MD(args->op);
    346 	if (__predict_false(op >= nvmm_impl->conf_max)) {
    347 		return EINVAL;
    348 	}
    349 
    350 	allocsz = nvmm_impl->conf_sizes[op];
    351 	data = kmem_alloc(allocsz, KM_SLEEP);
    352 
    353 	error = nvmm_machine_get(owner, args->machid, &mach, true);
    354 	if (error) {
    355 		kmem_free(data, allocsz);
    356 		return error;
    357 	}
    358 
    359 	error = copyin(args->conf, data, allocsz);
    360 	if (error) {
    361 		goto out;
    362 	}
    363 
    364 	error = (*nvmm_impl->machine_configure)(mach, op, data);
    365 
    366 out:
    367 	nvmm_machine_put(mach);
    368 	kmem_free(data, allocsz);
    369 	return error;
    370 }
    371 
    372 static int
    373 nvmm_vcpu_create(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_create *args)
    374 {
    375 	struct nvmm_machine *mach;
    376 	struct nvmm_cpu *vcpu;
    377 	int error;
    378 
    379 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    380 	if (error)
    381 		return error;
    382 
    383 	error = nvmm_vcpu_alloc(mach, args->cpuid, &vcpu);
    384 	if (error)
    385 		goto out;
    386 
    387 	/* Allocate the comm page. */
    388 	uao_reference(mach->commuobj);
    389 	error = uvm_map(kernel_map, (vaddr_t *)&vcpu->comm, PAGE_SIZE,
    390 	    mach->commuobj, args->cpuid * PAGE_SIZE, 0, UVM_MAPFLAG(UVM_PROT_RW,
    391 	    UVM_PROT_RW, UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
    392 	if (error) {
    393 		uao_detach(mach->commuobj);
    394 		nvmm_vcpu_free(mach, vcpu);
    395 		nvmm_vcpu_put(vcpu);
    396 		goto out;
    397 	}
    398 	error = uvm_map_pageable(kernel_map, (vaddr_t)vcpu->comm,
    399 	    (vaddr_t)vcpu->comm + PAGE_SIZE, false, 0);
    400 	if (error) {
    401 		nvmm_vcpu_free(mach, vcpu);
    402 		nvmm_vcpu_put(vcpu);
    403 		goto out;
    404 	}
    405 	memset(vcpu->comm, 0, PAGE_SIZE);
    406 
    407 	error = (*nvmm_impl->vcpu_create)(mach, vcpu);
    408 	if (error) {
    409 		nvmm_vcpu_free(mach, vcpu);
    410 		nvmm_vcpu_put(vcpu);
    411 		goto out;
    412 	}
    413 
    414 	nvmm_vcpu_put(vcpu);
    415 
    416 out:
    417 	nvmm_machine_put(mach);
    418 	return error;
    419 }
    420 
    421 static int
    422 nvmm_vcpu_destroy(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_destroy *args)
    423 {
    424 	struct nvmm_machine *mach;
    425 	struct nvmm_cpu *vcpu;
    426 	int error;
    427 
    428 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    429 	if (error)
    430 		return error;
    431 
    432 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    433 	if (error)
    434 		goto out;
    435 
    436 	(*nvmm_impl->vcpu_destroy)(mach, vcpu);
    437 	nvmm_vcpu_free(mach, vcpu);
    438 	nvmm_vcpu_put(vcpu);
    439 
    440 out:
    441 	nvmm_machine_put(mach);
    442 	return error;
    443 }
    444 
    445 static int
    446 nvmm_vcpu_setstate(struct nvmm_owner *owner,
    447     struct nvmm_ioc_vcpu_setstate *args)
    448 {
    449 	struct nvmm_machine *mach;
    450 	struct nvmm_cpu *vcpu;
    451 	int error;
    452 
    453 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    454 	if (error)
    455 		return error;
    456 
    457 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    458 	if (error)
    459 		goto out;
    460 
    461 	(*nvmm_impl->vcpu_setstate)(vcpu);
    462 	nvmm_vcpu_put(vcpu);
    463 
    464 out:
    465 	nvmm_machine_put(mach);
    466 	return error;
    467 }
    468 
    469 static int
    470 nvmm_vcpu_getstate(struct nvmm_owner *owner,
    471     struct nvmm_ioc_vcpu_getstate *args)
    472 {
    473 	struct nvmm_machine *mach;
    474 	struct nvmm_cpu *vcpu;
    475 	int error;
    476 
    477 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    478 	if (error)
    479 		return error;
    480 
    481 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    482 	if (error)
    483 		goto out;
    484 
    485 	(*nvmm_impl->vcpu_getstate)(vcpu);
    486 	nvmm_vcpu_put(vcpu);
    487 
    488 out:
    489 	nvmm_machine_put(mach);
    490 	return error;
    491 }
    492 
    493 static int
    494 nvmm_vcpu_inject(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_inject *args)
    495 {
    496 	struct nvmm_machine *mach;
    497 	struct nvmm_cpu *vcpu;
    498 	int error;
    499 
    500 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    501 	if (error)
    502 		return error;
    503 
    504 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    505 	if (error)
    506 		goto out;
    507 
    508 	error = (*nvmm_impl->vcpu_inject)(vcpu);
    509 	nvmm_vcpu_put(vcpu);
    510 
    511 out:
    512 	nvmm_machine_put(mach);
    513 	return error;
    514 }
    515 
    516 static void
    517 nvmm_do_vcpu_run(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
    518     struct nvmm_exit *exit)
    519 {
    520 	struct vmspace *vm = mach->vm;
    521 
    522 	while (1) {
    523 		(*nvmm_impl->vcpu_run)(mach, vcpu, exit);
    524 
    525 		if (__predict_true(exit->reason != NVMM_EXIT_MEMORY)) {
    526 			break;
    527 		}
    528 		if (exit->u.mem.gpa >= mach->gpa_end) {
    529 			break;
    530 		}
    531 		if (uvm_fault(&vm->vm_map, exit->u.mem.gpa, exit->u.mem.prot)) {
    532 			break;
    533 		}
    534 	}
    535 }
    536 
    537 static int
    538 nvmm_vcpu_run(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_run *args)
    539 {
    540 	struct nvmm_machine *mach;
    541 	struct nvmm_cpu *vcpu;
    542 	int error;
    543 
    544 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    545 	if (error)
    546 		return error;
    547 
    548 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    549 	if (error)
    550 		goto out;
    551 
    552 	nvmm_do_vcpu_run(mach, vcpu, &args->exit);
    553 	nvmm_vcpu_put(vcpu);
    554 
    555 out:
    556 	nvmm_machine_put(mach);
    557 	return error;
    558 }
    559 
    560 /* -------------------------------------------------------------------------- */
    561 
    562 static struct uvm_object *
    563 nvmm_hmapping_getuobj(struct nvmm_machine *mach, uintptr_t hva, size_t size,
    564    size_t *off)
    565 {
    566 	struct nvmm_hmapping *hmapping;
    567 	size_t i;
    568 
    569 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
    570 		hmapping = &mach->hmap[i];
    571 		if (!hmapping->present) {
    572 			continue;
    573 		}
    574 		if (hva >= hmapping->hva &&
    575 		    hva + size <= hmapping->hva + hmapping->size) {
    576 			*off = hva - hmapping->hva;
    577 			return hmapping->uobj;
    578 		}
    579 	}
    580 
    581 	return NULL;
    582 }
    583 
    584 static int
    585 nvmm_hmapping_validate(struct nvmm_machine *mach, uintptr_t hva, size_t size)
    586 {
    587 	struct nvmm_hmapping *hmapping;
    588 	size_t i;
    589 
    590 	if ((hva % PAGE_SIZE) != 0 || (size % PAGE_SIZE) != 0) {
    591 		return EINVAL;
    592 	}
    593 	if (hva == 0) {
    594 		return EINVAL;
    595 	}
    596 
    597 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
    598 		hmapping = &mach->hmap[i];
    599 		if (!hmapping->present) {
    600 			continue;
    601 		}
    602 
    603 		if (hva >= hmapping->hva &&
    604 		    hva + size <= hmapping->hva + hmapping->size) {
    605 			break;
    606 		}
    607 
    608 		if (hva >= hmapping->hva &&
    609 		    hva < hmapping->hva + hmapping->size) {
    610 			return EEXIST;
    611 		}
    612 		if (hva + size > hmapping->hva &&
    613 		    hva + size <= hmapping->hva + hmapping->size) {
    614 			return EEXIST;
    615 		}
    616 		if (hva <= hmapping->hva &&
    617 		    hva + size >= hmapping->hva + hmapping->size) {
    618 			return EEXIST;
    619 		}
    620 	}
    621 
    622 	return 0;
    623 }
    624 
    625 static struct nvmm_hmapping *
    626 nvmm_hmapping_alloc(struct nvmm_machine *mach)
    627 {
    628 	struct nvmm_hmapping *hmapping;
    629 	size_t i;
    630 
    631 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
    632 		hmapping = &mach->hmap[i];
    633 		if (!hmapping->present) {
    634 			hmapping->present = true;
    635 			return hmapping;
    636 		}
    637 	}
    638 
    639 	return NULL;
    640 }
    641 
    642 static int
    643 nvmm_hmapping_free(struct nvmm_machine *mach, uintptr_t hva, size_t size)
    644 {
    645 	struct vmspace *vmspace = curproc->p_vmspace;
    646 	struct nvmm_hmapping *hmapping;
    647 	size_t i;
    648 
    649 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
    650 		hmapping = &mach->hmap[i];
    651 		if (!hmapping->present || hmapping->hva != hva ||
    652 		    hmapping->size != size) {
    653 			continue;
    654 		}
    655 
    656 		uvm_unmap(&vmspace->vm_map, hmapping->hva,
    657 		    hmapping->hva + hmapping->size);
    658 		uao_detach(hmapping->uobj);
    659 
    660 		hmapping->uobj = NULL;
    661 		hmapping->present = false;
    662 
    663 		return 0;
    664 	}
    665 
    666 	return ENOENT;
    667 }
    668 
    669 static int
    670 nvmm_hva_map(struct nvmm_owner *owner, struct nvmm_ioc_hva_map *args)
    671 {
    672 	struct vmspace *vmspace = curproc->p_vmspace;
    673 	struct nvmm_machine *mach;
    674 	struct nvmm_hmapping *hmapping;
    675 	vaddr_t uva;
    676 	int error;
    677 
    678 	error = nvmm_machine_get(owner, args->machid, &mach, true);
    679 	if (error)
    680 		return error;
    681 
    682 	error = nvmm_hmapping_validate(mach, args->hva, args->size);
    683 	if (error)
    684 		goto out;
    685 
    686 	hmapping = nvmm_hmapping_alloc(mach);
    687 	if (hmapping == NULL) {
    688 		error = ENOBUFS;
    689 		goto out;
    690 	}
    691 
    692 	hmapping->hva = args->hva;
    693 	hmapping->size = args->size;
    694 	hmapping->uobj = uao_create(hmapping->size, 0);
    695 	uva = hmapping->hva;
    696 
    697 	/* Take a reference for the user. */
    698 	uao_reference(hmapping->uobj);
    699 
    700 	/* Map the uobj into the user address space, as pageable. */
    701 	error = uvm_map(&vmspace->vm_map, &uva, hmapping->size, hmapping->uobj,
    702 	    0, 0, UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_SHARE,
    703 	    UVM_ADV_RANDOM, UVM_FLAG_FIXED|UVM_FLAG_UNMAP));
    704 	if (error) {
    705 		uao_detach(hmapping->uobj);
    706 	}
    707 
    708 out:
    709 	nvmm_machine_put(mach);
    710 	return error;
    711 }
    712 
    713 static int
    714 nvmm_hva_unmap(struct nvmm_owner *owner, struct nvmm_ioc_hva_unmap *args)
    715 {
    716 	struct nvmm_machine *mach;
    717 	int error;
    718 
    719 	error = nvmm_machine_get(owner, args->machid, &mach, true);
    720 	if (error)
    721 		return error;
    722 
    723 	error = nvmm_hmapping_free(mach, args->hva, args->size);
    724 
    725 	nvmm_machine_put(mach);
    726 	return error;
    727 }
    728 
    729 /* -------------------------------------------------------------------------- */
    730 
    731 static int
    732 nvmm_gpa_map(struct nvmm_owner *owner, struct nvmm_ioc_gpa_map *args)
    733 {
    734 	struct nvmm_machine *mach;
    735 	struct uvm_object *uobj;
    736 	gpaddr_t gpa;
    737 	size_t off;
    738 	int error;
    739 
    740 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    741 	if (error)
    742 		return error;
    743 
    744 	if ((args->prot & ~(PROT_READ|PROT_WRITE|PROT_EXEC)) != 0) {
    745 		error = EINVAL;
    746 		goto out;
    747 	}
    748 
    749 	if ((args->gpa % PAGE_SIZE) != 0 || (args->size % PAGE_SIZE) != 0 ||
    750 	    (args->hva % PAGE_SIZE) != 0) {
    751 		error = EINVAL;
    752 		goto out;
    753 	}
    754 	if (args->hva == 0) {
    755 		error = EINVAL;
    756 		goto out;
    757 	}
    758 	if (args->gpa < mach->gpa_begin || args->gpa >= mach->gpa_end) {
    759 		error = EINVAL;
    760 		goto out;
    761 	}
    762 	if (args->gpa + args->size <= args->gpa) {
    763 		error = EINVAL;
    764 		goto out;
    765 	}
    766 	if (args->gpa + args->size > mach->gpa_end) {
    767 		error = EINVAL;
    768 		goto out;
    769 	}
    770 	gpa = args->gpa;
    771 
    772 	uobj = nvmm_hmapping_getuobj(mach, args->hva, args->size, &off);
    773 	if (uobj == NULL) {
    774 		error = EINVAL;
    775 		goto out;
    776 	}
    777 
    778 	/* Take a reference for the machine. */
    779 	uao_reference(uobj);
    780 
    781 	/* Map the uobj into the machine address space, as pageable. */
    782 	error = uvm_map(&mach->vm->vm_map, &gpa, args->size, uobj, off, 0,
    783 	    UVM_MAPFLAG(args->prot, UVM_PROT_RWX, UVM_INH_NONE,
    784 	    UVM_ADV_RANDOM, UVM_FLAG_FIXED|UVM_FLAG_UNMAP));
    785 	if (error) {
    786 		uao_detach(uobj);
    787 		goto out;
    788 	}
    789 	if (gpa != args->gpa) {
    790 		uao_detach(uobj);
    791 		printf("[!] uvm_map problem\n");
    792 		error = EINVAL;
    793 		goto out;
    794 	}
    795 
    796 out:
    797 	nvmm_machine_put(mach);
    798 	return error;
    799 }
    800 
    801 static int
    802 nvmm_gpa_unmap(struct nvmm_owner *owner, struct nvmm_ioc_gpa_unmap *args)
    803 {
    804 	struct nvmm_machine *mach;
    805 	gpaddr_t gpa;
    806 	int error;
    807 
    808 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    809 	if (error)
    810 		return error;
    811 
    812 	if ((args->gpa % PAGE_SIZE) != 0 || (args->size % PAGE_SIZE) != 0) {
    813 		error = EINVAL;
    814 		goto out;
    815 	}
    816 	if (args->gpa < mach->gpa_begin || args->gpa >= mach->gpa_end) {
    817 		error = EINVAL;
    818 		goto out;
    819 	}
    820 	if (args->gpa + args->size <= args->gpa) {
    821 		error = EINVAL;
    822 		goto out;
    823 	}
    824 	if (args->gpa + args->size >= mach->gpa_end) {
    825 		error = EINVAL;
    826 		goto out;
    827 	}
    828 	gpa = args->gpa;
    829 
    830 	/* Unmap the memory from the machine. */
    831 	uvm_unmap(&mach->vm->vm_map, gpa, gpa + args->size);
    832 
    833 out:
    834 	nvmm_machine_put(mach);
    835 	return error;
    836 }
    837 
    838 /* -------------------------------------------------------------------------- */
    839 
    840 static int
    841 nvmm_ctl_mach_info(struct nvmm_ioc_ctl *args)
    842 {
    843 	struct nvmm_ctl_mach_info ctl;
    844 	struct nvmm_machine *mach;
    845 	struct nvmm_cpu *vcpu;
    846 	int error;
    847 	size_t i;
    848 
    849 	if (args->size != sizeof(ctl))
    850 		return EINVAL;
    851 	error = copyin(args->data, &ctl, sizeof(ctl));
    852 	if (error)
    853 		return error;
    854 
    855 	error = nvmm_machine_get(&root_owner, ctl.machid, &mach, true);
    856 	if (error)
    857 		return error;
    858 
    859 	ctl.nvcpus = 0;
    860 	for (i = 0; i < NVMM_MAX_VCPUS; i++) {
    861 		error = nvmm_vcpu_get(mach, i, &vcpu);
    862 		if (error)
    863 			continue;
    864 		ctl.nvcpus++;
    865 		nvmm_vcpu_put(vcpu);
    866 	}
    867 	ctl.pid = mach->owner->pid;
    868 	ctl.time = mach->time;
    869 
    870 	nvmm_machine_put(mach);
    871 
    872 	error = copyout(&ctl, args->data, sizeof(ctl));
    873 	if (error)
    874 		return error;
    875 
    876 	return 0;
    877 }
    878 
    879 static int
    880 nvmm_ctl(struct nvmm_owner *owner, struct nvmm_ioc_ctl *args)
    881 {
    882 	int error;
    883 
    884 	error = kauth_authorize_device(curlwp->l_cred, KAUTH_DEVICE_NVMM_CTL,
    885 	    NULL, NULL, NULL, NULL);
    886 	if (error)
    887 		return error;
    888 
    889 	switch (args->op) {
    890 	case NVMM_CTL_MACH_INFO:
    891 		return nvmm_ctl_mach_info(args);
    892 	default:
    893 		return EINVAL;
    894 	}
    895 }
    896 
    897 /* -------------------------------------------------------------------------- */
    898 
    899 static int
    900 nvmm_init(void)
    901 {
    902 	size_t i, n;
    903 
    904 	for (i = 0; i < __arraycount(nvmm_impl_list); i++) {
    905 		if (!(*nvmm_impl_list[i]->ident)()) {
    906 			continue;
    907 		}
    908 		nvmm_impl = nvmm_impl_list[i];
    909 		break;
    910 	}
    911 	if (nvmm_impl == NULL) {
    912 		printf("[!] No implementation found\n");
    913 		return ENOTSUP;
    914 	}
    915 
    916 	for (i = 0; i < NVMM_MAX_MACHINES; i++) {
    917 		machines[i].machid = i;
    918 		rw_init(&machines[i].lock);
    919 		for (n = 0; n < NVMM_MAX_VCPUS; n++) {
    920 			machines[i].cpus[n].present = false;
    921 			machines[i].cpus[n].cpuid = n;
    922 			mutex_init(&machines[i].cpus[n].lock, MUTEX_DEFAULT,
    923 			    IPL_NONE);
    924 		}
    925 	}
    926 
    927 	(*nvmm_impl->init)();
    928 
    929 	return 0;
    930 }
    931 
    932 static void
    933 nvmm_fini(void)
    934 {
    935 	size_t i, n;
    936 
    937 	for (i = 0; i < NVMM_MAX_MACHINES; i++) {
    938 		rw_destroy(&machines[i].lock);
    939 		for (n = 0; n < NVMM_MAX_VCPUS; n++) {
    940 			mutex_destroy(&machines[i].cpus[n].lock);
    941 		}
    942 	}
    943 
    944 	(*nvmm_impl->fini)();
    945 }
    946 
    947 /* -------------------------------------------------------------------------- */
    948 
    949 static dev_type_open(nvmm_open);
    950 
    951 const struct cdevsw nvmm_cdevsw = {
    952 	.d_open = nvmm_open,
    953 	.d_close = noclose,
    954 	.d_read = noread,
    955 	.d_write = nowrite,
    956 	.d_ioctl = noioctl,
    957 	.d_stop = nostop,
    958 	.d_tty = notty,
    959 	.d_poll = nopoll,
    960 	.d_mmap = nommap,
    961 	.d_kqfilter = nokqfilter,
    962 	.d_discard = nodiscard,
    963 	.d_flag = D_OTHER | D_MPSAFE
    964 };
    965 
    966 static int nvmm_ioctl(file_t *, u_long, void *);
    967 static int nvmm_close(file_t *);
    968 static int nvmm_mmap(file_t *, off_t *, size_t, int, int *, int *,
    969     struct uvm_object **, int *);
    970 
    971 const struct fileops nvmm_fileops = {
    972 	.fo_read = fbadop_read,
    973 	.fo_write = fbadop_write,
    974 	.fo_ioctl = nvmm_ioctl,
    975 	.fo_fcntl = fnullop_fcntl,
    976 	.fo_poll = fnullop_poll,
    977 	.fo_stat = fbadop_stat,
    978 	.fo_close = nvmm_close,
    979 	.fo_kqfilter = fnullop_kqfilter,
    980 	.fo_restart = fnullop_restart,
    981 	.fo_mmap = nvmm_mmap,
    982 };
    983 
    984 static int
    985 nvmm_open(dev_t dev, int flags, int type, struct lwp *l)
    986 {
    987 	struct nvmm_owner *owner;
    988 	struct file *fp;
    989 	int error, fd;
    990 
    991 	if (minor(dev) != 0)
    992 		return EXDEV;
    993 	error = fd_allocfile(&fp, &fd);
    994 	if (error)
    995 		return error;
    996 
    997 	owner = kmem_alloc(sizeof(*owner), KM_SLEEP);
    998 	owner->pid = l->l_proc->p_pid;
    999 
   1000 	return fd_clone(fp, fd, flags, &nvmm_fileops, owner);
   1001 }
   1002 
   1003 static int
   1004 nvmm_close(file_t *fp)
   1005 {
   1006 	struct nvmm_owner *owner = fp->f_data;
   1007 
   1008 	KASSERT(owner != NULL);
   1009 	nvmm_kill_machines(owner);
   1010 	kmem_free(owner, sizeof(*owner));
   1011 	fp->f_data = NULL;
   1012 
   1013    	return 0;
   1014 }
   1015 
   1016 static int
   1017 nvmm_mmap(file_t *fp, off_t *offp, size_t size, int prot, int *flagsp,
   1018     int *advicep, struct uvm_object **uobjp, int *maxprotp)
   1019 {
   1020 	struct nvmm_owner *owner = fp->f_data;
   1021 	struct nvmm_machine *mach;
   1022 	nvmm_machid_t machid;
   1023 	nvmm_cpuid_t cpuid;
   1024 	int error;
   1025 
   1026 	if (prot & PROT_EXEC)
   1027 		return EACCES;
   1028 	if (size != PAGE_SIZE)
   1029 		return EINVAL;
   1030 
   1031 	cpuid = NVMM_COMM_CPUID(*offp);
   1032 	if (__predict_false(cpuid >= NVMM_MAX_VCPUS))
   1033 		return EINVAL;
   1034 
   1035 	machid = NVMM_COMM_MACHID(*offp);
   1036 	error = nvmm_machine_get(owner, machid, &mach, false);
   1037 	if (error)
   1038 		return error;
   1039 
   1040 	uao_reference(mach->commuobj);
   1041 	*uobjp = mach->commuobj;
   1042 	*offp = cpuid * PAGE_SIZE;
   1043 	*maxprotp = prot;
   1044 	*advicep = UVM_ADV_RANDOM;
   1045 
   1046 	nvmm_machine_put(mach);
   1047 	return 0;
   1048 }
   1049 
   1050 static int
   1051 nvmm_ioctl(file_t *fp, u_long cmd, void *data)
   1052 {
   1053 	struct nvmm_owner *owner = fp->f_data;
   1054 
   1055 	KASSERT(owner != NULL);
   1056 
   1057 	switch (cmd) {
   1058 	case NVMM_IOC_CAPABILITY:
   1059 		return nvmm_capability(owner, data);
   1060 	case NVMM_IOC_MACHINE_CREATE:
   1061 		return nvmm_machine_create(owner, data);
   1062 	case NVMM_IOC_MACHINE_DESTROY:
   1063 		return nvmm_machine_destroy(owner, data);
   1064 	case NVMM_IOC_MACHINE_CONFIGURE:
   1065 		return nvmm_machine_configure(owner, data);
   1066 	case NVMM_IOC_VCPU_CREATE:
   1067 		return nvmm_vcpu_create(owner, data);
   1068 	case NVMM_IOC_VCPU_DESTROY:
   1069 		return nvmm_vcpu_destroy(owner, data);
   1070 	case NVMM_IOC_VCPU_SETSTATE:
   1071 		return nvmm_vcpu_setstate(owner, data);
   1072 	case NVMM_IOC_VCPU_GETSTATE:
   1073 		return nvmm_vcpu_getstate(owner, data);
   1074 	case NVMM_IOC_VCPU_INJECT:
   1075 		return nvmm_vcpu_inject(owner, data);
   1076 	case NVMM_IOC_VCPU_RUN:
   1077 		return nvmm_vcpu_run(owner, data);
   1078 	case NVMM_IOC_GPA_MAP:
   1079 		return nvmm_gpa_map(owner, data);
   1080 	case NVMM_IOC_GPA_UNMAP:
   1081 		return nvmm_gpa_unmap(owner, data);
   1082 	case NVMM_IOC_HVA_MAP:
   1083 		return nvmm_hva_map(owner, data);
   1084 	case NVMM_IOC_HVA_UNMAP:
   1085 		return nvmm_hva_unmap(owner, data);
   1086 	case NVMM_IOC_CTL:
   1087 		return nvmm_ctl(owner, data);
   1088 	default:
   1089 		return EINVAL;
   1090 	}
   1091 }
   1092 
   1093 /* -------------------------------------------------------------------------- */
   1094 
   1095 void
   1096 nvmmattach(int nunits)
   1097 {
   1098 	/* nothing */
   1099 }
   1100 
   1101 MODULE(MODULE_CLASS_MISC, nvmm, NULL);
   1102 
   1103 static int
   1104 nvmm_modcmd(modcmd_t cmd, void *arg)
   1105 {
   1106 	int error;
   1107 
   1108 	switch (cmd) {
   1109 	case MODULE_CMD_INIT:
   1110 		error = nvmm_init();
   1111 		if (error)
   1112 			return error;
   1113 
   1114 #if defined(_MODULE)
   1115 		{
   1116 			devmajor_t bmajor = NODEVMAJOR;
   1117 			devmajor_t cmajor = 345;
   1118 
   1119 			/* mknod /dev/nvmm c 345 0 */
   1120 			error = devsw_attach("nvmm", NULL, &bmajor,
   1121 			    &nvmm_cdevsw, &cmajor);
   1122 			if (error) {
   1123 				nvmm_fini();
   1124 				return error;
   1125 			}
   1126 		}
   1127 #endif
   1128 		return 0;
   1129 
   1130 	case MODULE_CMD_FINI:
   1131 		if (nmachines > 0) {
   1132 			return EBUSY;
   1133 		}
   1134 #if defined(_MODULE)
   1135 		{
   1136 			error = devsw_detach(NULL, &nvmm_cdevsw);
   1137 			if (error) {
   1138 				return error;
   1139 			}
   1140 		}
   1141 #endif
   1142 		nvmm_fini();
   1143 		return 0;
   1144 
   1145 	case MODULE_CMD_AUTOUNLOAD:
   1146 		return EBUSY;
   1147 
   1148 	default:
   1149 		return ENOTTY;
   1150 	}
   1151 }
   1152