Home | History | Annotate | Line # | Download | only in nvmm
nvmm.c revision 1.7
      1 /*	$NetBSD: nvmm.c,v 1.7 2019/02/13 16:03:16 maxv Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2018 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.7 2019/02/13 16:03:16 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 
     45 #include <uvm/uvm.h>
     46 #include <uvm/uvm_page.h>
     47 
     48 #include "ioconf.h"
     49 
     50 #include <dev/nvmm/nvmm.h>
     51 #include <dev/nvmm/nvmm_internal.h>
     52 #include <dev/nvmm/nvmm_ioctl.h>
     53 
     54 static struct nvmm_machine machines[NVMM_MAX_MACHINES];
     55 
     56 static const struct nvmm_impl *nvmm_impl_list[] = {
     57 	&nvmm_x86_svm,	/* x86 AMD SVM */
     58 	&nvmm_x86_vmx	/* x86 Intel VMX */
     59 };
     60 
     61 static const struct nvmm_impl *nvmm_impl = NULL;
     62 
     63 /* -------------------------------------------------------------------------- */
     64 
     65 static int
     66 nvmm_machine_alloc(struct nvmm_machine **ret)
     67 {
     68 	struct nvmm_machine *mach;
     69 	size_t i;
     70 
     71 	for (i = 0; i < NVMM_MAX_MACHINES; i++) {
     72 		mach = &machines[i];
     73 
     74 		rw_enter(&mach->lock, RW_WRITER);
     75 		if (mach->present) {
     76 			rw_exit(&mach->lock);
     77 			continue;
     78 		}
     79 
     80 		mach->present = true;
     81 		*ret = mach;
     82 		return 0;
     83 	}
     84 
     85 	return ENOBUFS;
     86 }
     87 
     88 static void
     89 nvmm_machine_free(struct nvmm_machine *mach)
     90 {
     91 	KASSERT(rw_write_held(&mach->lock));
     92 	KASSERT(mach->present);
     93 	mach->present = false;
     94 }
     95 
     96 static int
     97 nvmm_machine_get(nvmm_machid_t machid, struct nvmm_machine **ret, bool writer)
     98 {
     99 	struct nvmm_machine *mach;
    100 	krw_t op = writer ? RW_WRITER : RW_READER;
    101 
    102 	if (machid >= NVMM_MAX_MACHINES) {
    103 		return EINVAL;
    104 	}
    105 	mach = &machines[machid];
    106 
    107 	rw_enter(&mach->lock, op);
    108 	if (!mach->present) {
    109 		rw_exit(&mach->lock);
    110 		return ENOENT;
    111 	}
    112 	if (mach->procid != curproc->p_pid) {
    113 		rw_exit(&mach->lock);
    114 		return EPERM;
    115 	}
    116 	*ret = mach;
    117 
    118 	return 0;
    119 }
    120 
    121 static void
    122 nvmm_machine_put(struct nvmm_machine *mach)
    123 {
    124 	rw_exit(&mach->lock);
    125 }
    126 
    127 /* -------------------------------------------------------------------------- */
    128 
    129 static int
    130 nvmm_vcpu_alloc(struct nvmm_machine *mach, struct nvmm_cpu **ret)
    131 {
    132 	struct nvmm_cpu *vcpu;
    133 	size_t i;
    134 
    135 	for (i = 0; i < NVMM_MAX_VCPUS; i++) {
    136 		vcpu = &mach->cpus[i];
    137 
    138 		mutex_enter(&vcpu->lock);
    139 		if (vcpu->present) {
    140 			mutex_exit(&vcpu->lock);
    141 			continue;
    142 		}
    143 
    144 		vcpu->present = true;
    145 		vcpu->cpuid = i;
    146 		vcpu->state = kmem_zalloc(nvmm_impl->state_size, KM_SLEEP);
    147 		*ret = vcpu;
    148 		return 0;
    149 	}
    150 
    151 	return ENOBUFS;
    152 }
    153 
    154 static void
    155 nvmm_vcpu_free(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
    156 {
    157 	KASSERT(mutex_owned(&vcpu->lock));
    158 	vcpu->present = false;
    159 	kmem_free(vcpu->state, nvmm_impl->state_size);
    160 	vcpu->hcpu_last = -1;
    161 }
    162 
    163 int
    164 nvmm_vcpu_get(struct nvmm_machine *mach, nvmm_cpuid_t cpuid,
    165     struct nvmm_cpu **ret)
    166 {
    167 	struct nvmm_cpu *vcpu;
    168 
    169 	if (cpuid >= NVMM_MAX_VCPUS) {
    170 		return EINVAL;
    171 	}
    172 	vcpu = &mach->cpus[cpuid];
    173 
    174 	mutex_enter(&vcpu->lock);
    175 	if (!vcpu->present) {
    176 		mutex_exit(&vcpu->lock);
    177 		return ENOENT;
    178 	}
    179 	*ret = vcpu;
    180 
    181 	return 0;
    182 }
    183 
    184 void
    185 nvmm_vcpu_put(struct nvmm_cpu *vcpu)
    186 {
    187 	mutex_exit(&vcpu->lock);
    188 }
    189 
    190 /* -------------------------------------------------------------------------- */
    191 
    192 static void
    193 nvmm_kill_machines(pid_t pid)
    194 {
    195 	struct nvmm_machine *mach;
    196 	struct nvmm_cpu *vcpu;
    197 	size_t i, j;
    198 	int error;
    199 
    200 	for (i = 0; i < NVMM_MAX_MACHINES; i++) {
    201 		mach = &machines[i];
    202 
    203 		rw_enter(&mach->lock, RW_WRITER);
    204 		if (!mach->present || mach->procid != pid) {
    205 			rw_exit(&mach->lock);
    206 			continue;
    207 		}
    208 
    209 		/* Kill it. */
    210 		for (j = 0; j < NVMM_MAX_VCPUS; j++) {
    211 			error = nvmm_vcpu_get(mach, j, &vcpu);
    212 			if (error)
    213 				continue;
    214 			(*nvmm_impl->vcpu_destroy)(mach, vcpu);
    215 			nvmm_vcpu_free(mach, vcpu);
    216 			nvmm_vcpu_put(vcpu);
    217 		}
    218 		uvmspace_free(mach->vm);
    219 
    220 		/* Drop the kernel UOBJ refs. */
    221 		for (j = 0; j < NVMM_MAX_SEGS; j++) {
    222 			if (!mach->segs[j].present)
    223 				continue;
    224 			uao_detach(mach->segs[j].uobj);
    225 		}
    226 
    227 		nvmm_machine_free(mach);
    228 
    229 		rw_exit(&mach->lock);
    230 	}
    231 }
    232 
    233 /* -------------------------------------------------------------------------- */
    234 
    235 static int
    236 nvmm_capability(struct nvmm_ioc_capability *args)
    237 {
    238 	args->cap.version = NVMM_CAPABILITY_VERSION;
    239 	args->cap.state_size = nvmm_impl->state_size;
    240 	args->cap.max_machines = NVMM_MAX_MACHINES;
    241 	args->cap.max_vcpus = NVMM_MAX_VCPUS;
    242 	args->cap.max_ram = NVMM_MAX_RAM;
    243 
    244 	(*nvmm_impl->capability)(&args->cap);
    245 
    246 	return 0;
    247 }
    248 
    249 static int
    250 nvmm_machine_create(struct nvmm_ioc_machine_create *args)
    251 {
    252 	struct nvmm_machine *mach;
    253 	int error;
    254 
    255 	error = nvmm_machine_alloc(&mach);
    256 	if (error)
    257 		return error;
    258 
    259 	/* Curproc owns the machine. */
    260 	mach->procid = curproc->p_pid;
    261 
    262 	/* Zero out the segments. */
    263 	memset(&mach->segs, 0, sizeof(mach->segs));
    264 
    265 	/* Create the machine vmspace. */
    266 	mach->gpa_begin = 0;
    267 	mach->gpa_end = NVMM_MAX_RAM;
    268 	mach->vm = uvmspace_alloc(0, mach->gpa_end - mach->gpa_begin, false);
    269 
    270 	(*nvmm_impl->machine_create)(mach);
    271 
    272 	args->machid = mach->machid;
    273 	nvmm_machine_put(mach);
    274 
    275 	return 0;
    276 }
    277 
    278 static int
    279 nvmm_machine_destroy(struct nvmm_ioc_machine_destroy *args)
    280 {
    281 	struct nvmm_machine *mach;
    282 	struct nvmm_cpu *vcpu;
    283 	int error;
    284 	size_t i;
    285 
    286 	error = nvmm_machine_get(args->machid, &mach, true);
    287 	if (error)
    288 		return error;
    289 
    290 	for (i = 0; i < NVMM_MAX_VCPUS; i++) {
    291 		error = nvmm_vcpu_get(mach, i, &vcpu);
    292 		if (error)
    293 			continue;
    294 
    295 		(*nvmm_impl->vcpu_destroy)(mach, vcpu);
    296 		nvmm_vcpu_free(mach, vcpu);
    297 		nvmm_vcpu_put(vcpu);
    298 	}
    299 
    300 	(*nvmm_impl->machine_destroy)(mach);
    301 
    302 	/* Free the machine vmspace. */
    303 	uvmspace_free(mach->vm);
    304 
    305 	/* Drop the kernel UOBJ refs. */
    306 	for (i = 0; i < NVMM_MAX_SEGS; i++) {
    307 		if (!mach->segs[i].present)
    308 			continue;
    309 		uao_detach(mach->segs[i].uobj);
    310 	}
    311 
    312 	nvmm_machine_free(mach);
    313 	nvmm_machine_put(mach);
    314 
    315 	return 0;
    316 }
    317 
    318 static int
    319 nvmm_machine_configure(struct nvmm_ioc_machine_configure *args)
    320 {
    321 	struct nvmm_machine *mach;
    322 	size_t allocsz;
    323 	void *data;
    324 	int error;
    325 
    326 	if (__predict_false(args->op >= nvmm_impl->conf_max)) {
    327 		return EINVAL;
    328 	}
    329 
    330 	allocsz = nvmm_impl->conf_sizes[args->op];
    331 	data = kmem_alloc(allocsz, KM_SLEEP);
    332 
    333 	error = nvmm_machine_get(args->machid, &mach, true);
    334 	if (error) {
    335 		kmem_free(data, allocsz);
    336 		return error;
    337 	}
    338 
    339 	error = copyin(args->conf, data, allocsz);
    340 	if (error) {
    341 		goto out;
    342 	}
    343 
    344 	error = (*nvmm_impl->machine_configure)(mach, args->op, data);
    345 
    346 out:
    347 	nvmm_machine_put(mach);
    348 	kmem_free(data, allocsz);
    349 	return error;
    350 }
    351 
    352 static int
    353 nvmm_vcpu_create(struct nvmm_ioc_vcpu_create *args)
    354 {
    355 	struct nvmm_machine *mach;
    356 	struct nvmm_cpu *vcpu;
    357 	int error;
    358 
    359 	error = nvmm_machine_get(args->machid, &mach, false);
    360 	if (error)
    361 		return error;
    362 
    363 	error = nvmm_vcpu_alloc(mach, &vcpu);
    364 	if (error)
    365 		goto out;
    366 
    367 	error = (*nvmm_impl->vcpu_create)(mach, vcpu);
    368 	if (error) {
    369 		nvmm_vcpu_free(mach, vcpu);
    370 		nvmm_vcpu_put(vcpu);
    371 		goto out;
    372 	}
    373 
    374 	nvmm_vcpu_put(vcpu);
    375 
    376 out:
    377 	nvmm_machine_put(mach);
    378 	return error;
    379 }
    380 
    381 static int
    382 nvmm_vcpu_destroy(struct nvmm_ioc_vcpu_destroy *args)
    383 {
    384 	struct nvmm_machine *mach;
    385 	struct nvmm_cpu *vcpu;
    386 	int error;
    387 
    388 	error = nvmm_machine_get(args->machid, &mach, false);
    389 	if (error)
    390 		return error;
    391 
    392 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    393 	if (error)
    394 		goto out;
    395 
    396 	(*nvmm_impl->vcpu_destroy)(mach, vcpu);
    397 	nvmm_vcpu_free(mach, vcpu);
    398 	nvmm_vcpu_put(vcpu);
    399 
    400 out:
    401 	nvmm_machine_put(mach);
    402 	return error;
    403 }
    404 
    405 static int
    406 nvmm_vcpu_setstate(struct nvmm_ioc_vcpu_setstate *args)
    407 {
    408 	struct nvmm_machine *mach;
    409 	struct nvmm_cpu *vcpu;
    410 	int error;
    411 
    412 	error = nvmm_machine_get(args->machid, &mach, false);
    413 	if (error)
    414 		return error;
    415 
    416 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    417 	if (error)
    418 		goto out;
    419 
    420 	error = copyin(args->state, vcpu->state, nvmm_impl->state_size);
    421 	if (error) {
    422 		nvmm_vcpu_put(vcpu);
    423 		goto out;
    424 	}
    425 
    426 	(*nvmm_impl->vcpu_setstate)(vcpu, vcpu->state, args->flags);
    427 	nvmm_vcpu_put(vcpu);
    428 
    429 out:
    430 	nvmm_machine_put(mach);
    431 	return error;
    432 }
    433 
    434 static int
    435 nvmm_vcpu_getstate(struct nvmm_ioc_vcpu_getstate *args)
    436 {
    437 	struct nvmm_machine *mach;
    438 	struct nvmm_cpu *vcpu;
    439 	int error;
    440 
    441 	error = nvmm_machine_get(args->machid, &mach, false);
    442 	if (error)
    443 		return error;
    444 
    445 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    446 	if (error)
    447 		goto out;
    448 
    449 	(*nvmm_impl->vcpu_getstate)(vcpu, vcpu->state, args->flags);
    450 	nvmm_vcpu_put(vcpu);
    451 	error = copyout(vcpu->state, args->state, nvmm_impl->state_size);
    452 
    453 out:
    454 	nvmm_machine_put(mach);
    455 	return error;
    456 }
    457 
    458 static int
    459 nvmm_vcpu_inject(struct nvmm_ioc_vcpu_inject *args)
    460 {
    461 	struct nvmm_machine *mach;
    462 	struct nvmm_cpu *vcpu;
    463 	int error;
    464 
    465 	error = nvmm_machine_get(args->machid, &mach, false);
    466 	if (error)
    467 		return error;
    468 
    469 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    470 	if (error)
    471 		goto out;
    472 
    473 	error = (*nvmm_impl->vcpu_inject)(mach, vcpu, &args->event);
    474 	nvmm_vcpu_put(vcpu);
    475 
    476 out:
    477 	nvmm_machine_put(mach);
    478 	return error;
    479 }
    480 
    481 static int
    482 nvmm_vcpu_run(struct nvmm_ioc_vcpu_run *args)
    483 {
    484 	struct nvmm_machine *mach;
    485 	struct nvmm_cpu *vcpu;
    486 	int error;
    487 
    488 	error = nvmm_machine_get(args->machid, &mach, false);
    489 	if (error)
    490 		return error;
    491 
    492 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    493 	if (error)
    494 		goto out;
    495 
    496 	(*nvmm_impl->vcpu_run)(mach, vcpu, &args->exit);
    497 	nvmm_vcpu_put(vcpu);
    498 
    499 out:
    500 	nvmm_machine_put(mach);
    501 	return error;
    502 }
    503 
    504 /* -------------------------------------------------------------------------- */
    505 
    506 static struct uvm_object *
    507 nvmm_seg_getuobj(struct nvmm_machine *mach, uintptr_t hva, size_t size,
    508    size_t *off)
    509 {
    510 	struct nvmm_seg *seg;
    511 	size_t i;
    512 
    513 	for (i = 0; i < NVMM_MAX_SEGS; i++) {
    514 		seg = &mach->segs[i];
    515 		if (!seg->present) {
    516 			continue;
    517 		}
    518 		if (hva >= seg->hva && hva + size <= seg->hva + seg->size) {
    519 			*off = hva - seg->hva;
    520 			return seg->uobj;
    521 		}
    522 	}
    523 
    524 	return NULL;
    525 }
    526 
    527 static struct nvmm_seg *
    528 nvmm_seg_find(struct nvmm_machine *mach, uintptr_t hva, size_t size)
    529 {
    530 	struct nvmm_seg *seg;
    531 	size_t i;
    532 
    533 	for (i = 0; i < NVMM_MAX_SEGS; i++) {
    534 		seg = &mach->segs[i];
    535 		if (seg->present && seg->hva == hva && seg->size == size) {
    536 			return seg;
    537 		}
    538 	}
    539 
    540 	return NULL;
    541 }
    542 
    543 static int
    544 nvmm_seg_validate(struct nvmm_machine *mach, uintptr_t hva, size_t size)
    545 {
    546 	struct nvmm_seg *seg;
    547 	size_t i;
    548 
    549 	if ((hva % PAGE_SIZE) != 0 || (size % PAGE_SIZE) != 0) {
    550 		return EINVAL;
    551 	}
    552 	if (hva == 0) {
    553 		return EINVAL;
    554 	}
    555 
    556 	for (i = 0; i < NVMM_MAX_SEGS; i++) {
    557 		seg = &mach->segs[i];
    558 		if (!seg->present) {
    559 			continue;
    560 		}
    561 
    562 		if (hva >= seg->hva && hva + size <= seg->hva + seg->size) {
    563 			break;
    564 		}
    565 
    566 		if (hva >= seg->hva && hva < seg->hva + seg->size) {
    567 			return EEXIST;
    568 		}
    569 		if (hva + size > seg->hva &&
    570 		    hva + size <= seg->hva + seg->size) {
    571 			return EEXIST;
    572 		}
    573 		if (hva <= seg->hva && hva + size >= seg->hva + seg->size) {
    574 			return EEXIST;
    575 		}
    576 	}
    577 
    578 	return 0;
    579 }
    580 
    581 static struct nvmm_seg *
    582 nvmm_seg_alloc(struct nvmm_machine *mach)
    583 {
    584 	struct nvmm_seg *seg;
    585 	size_t i;
    586 
    587 	for (i = 0; i < NVMM_MAX_SEGS; i++) {
    588 		seg = &mach->segs[i];
    589 		if (!seg->present) {
    590 			seg->present = true;
    591 			return seg;
    592 		}
    593 	}
    594 
    595 	return NULL;
    596 }
    597 
    598 static void
    599 nvmm_seg_free(struct nvmm_seg *seg)
    600 {
    601 	struct vmspace *vmspace = curproc->p_vmspace;
    602 
    603 	uvm_unmap(&vmspace->vm_map, seg->hva, seg->hva + seg->size);
    604 	uao_detach(seg->uobj);
    605 
    606 	seg->uobj = NULL;
    607 	seg->present = false;
    608 }
    609 
    610 static int
    611 nvmm_hva_map(struct nvmm_ioc_hva_map *args)
    612 {
    613 	struct vmspace *vmspace = curproc->p_vmspace;
    614 	struct nvmm_machine *mach;
    615 	struct nvmm_seg *seg;
    616 	vaddr_t uva;
    617 	int error;
    618 
    619 	error = nvmm_machine_get(args->machid, &mach, true);
    620 	if (error)
    621 		return error;
    622 
    623 	error = nvmm_seg_validate(mach, args->hva, args->size);
    624 	if (error)
    625 		goto out;
    626 
    627 	seg = nvmm_seg_alloc(mach);
    628 	if (seg == NULL) {
    629 		error = ENOBUFS;
    630 		goto out;
    631 	}
    632 
    633 	seg->hva = args->hva;
    634 	seg->size = args->size;
    635 	seg->uobj = uao_create(seg->size, 0);
    636 	uva = seg->hva;
    637 
    638 	/* Take a reference for the user. */
    639 	uao_reference(seg->uobj);
    640 
    641 	/* Map the uobj into the user address space, as pageable. */
    642 	error = uvm_map(&vmspace->vm_map, &uva, seg->size, seg->uobj, 0, 0,
    643 	    UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_SHARE,
    644 	    UVM_ADV_RANDOM, UVM_FLAG_FIXED|UVM_FLAG_UNMAP));
    645 	if (error) {
    646 		uao_detach(seg->uobj);
    647 	}
    648 
    649 out:
    650 	nvmm_machine_put(mach);
    651 	return error;
    652 }
    653 
    654 static int
    655 nvmm_hva_unmap(struct nvmm_ioc_hva_unmap *args)
    656 {
    657 	struct nvmm_machine *mach;
    658 	struct nvmm_seg *seg;
    659 	int error;
    660 
    661 	error = nvmm_machine_get(args->machid, &mach, true);
    662 	if (error)
    663 		return error;
    664 
    665 	seg = nvmm_seg_find(mach, args->hva, args->size);
    666 	if (seg == NULL)
    667 		return ENOENT;
    668 
    669 	nvmm_seg_free(seg);
    670 
    671 	nvmm_machine_put(mach);
    672 	return 0;
    673 }
    674 
    675 /* -------------------------------------------------------------------------- */
    676 
    677 static int
    678 nvmm_gpa_map(struct nvmm_ioc_gpa_map *args)
    679 {
    680 	struct nvmm_machine *mach;
    681 	struct uvm_object *uobj;
    682 	gpaddr_t gpa;
    683 	size_t off;
    684 	int error;
    685 
    686 	error = nvmm_machine_get(args->machid, &mach, false);
    687 	if (error)
    688 		return error;
    689 
    690 	if ((args->gpa % PAGE_SIZE) != 0 || (args->size % PAGE_SIZE) != 0 ||
    691 	    (args->hva % PAGE_SIZE) != 0) {
    692 		error = EINVAL;
    693 		goto out;
    694 	}
    695 	if (args->hva == 0) {
    696 		error = EINVAL;
    697 		goto out;
    698 	}
    699 	if (args->gpa < mach->gpa_begin || args->gpa >= mach->gpa_end) {
    700 		error = EINVAL;
    701 		goto out;
    702 	}
    703 	if (args->gpa + args->size <= args->gpa) {
    704 		error = EINVAL;
    705 		goto out;
    706 	}
    707 	if (args->gpa + args->size > mach->gpa_end) {
    708 		error = EINVAL;
    709 		goto out;
    710 	}
    711 	gpa = args->gpa;
    712 
    713 	uobj = nvmm_seg_getuobj(mach, args->hva, args->size, &off);
    714 	if (uobj == NULL) {
    715 		error = EINVAL;
    716 		goto out;
    717 	}
    718 
    719 	/* Take a reference for the machine. */
    720 	uao_reference(uobj);
    721 
    722 	/* Map the uobj into the machine address space, as pageable. */
    723 	error = uvm_map(&mach->vm->vm_map, &gpa, args->size, uobj, off, 0,
    724 	    UVM_MAPFLAG(UVM_PROT_RWX, UVM_PROT_RWX, UVM_INH_NONE,
    725 	    UVM_ADV_RANDOM, UVM_FLAG_FIXED|UVM_FLAG_UNMAP));
    726 	if (error) {
    727 		uao_detach(uobj);
    728 		goto out;
    729 	}
    730 	if (gpa != args->gpa) {
    731 		uao_detach(uobj);
    732 		printf("[!] uvm_map problem\n");
    733 		error = EINVAL;
    734 		goto out;
    735 	}
    736 
    737 out:
    738 	nvmm_machine_put(mach);
    739 	return error;
    740 }
    741 
    742 static int
    743 nvmm_gpa_unmap(struct nvmm_ioc_gpa_unmap *args)
    744 {
    745 	struct nvmm_machine *mach;
    746 	gpaddr_t gpa;
    747 	int error;
    748 
    749 	error = nvmm_machine_get(args->machid, &mach, false);
    750 	if (error)
    751 		return error;
    752 
    753 	if ((args->gpa % PAGE_SIZE) != 0 || (args->size % PAGE_SIZE) != 0) {
    754 		error = EINVAL;
    755 		goto out;
    756 	}
    757 	if (args->gpa < mach->gpa_begin || args->gpa >= mach->gpa_end) {
    758 		error = EINVAL;
    759 		goto out;
    760 	}
    761 	if (args->gpa + args->size <= args->gpa) {
    762 		error = EINVAL;
    763 		goto out;
    764 	}
    765 	if (args->gpa + args->size >= mach->gpa_end) {
    766 		error = EINVAL;
    767 		goto out;
    768 	}
    769 	gpa = args->gpa;
    770 
    771 	/* Unmap the memory from the machine. */
    772 	uvm_unmap(&mach->vm->vm_map, gpa, gpa + args->size);
    773 
    774 out:
    775 	nvmm_machine_put(mach);
    776 	return error;
    777 }
    778 
    779 /* -------------------------------------------------------------------------- */
    780 
    781 static int
    782 nvmm_init(void)
    783 {
    784 	size_t i, n;
    785 
    786 	for (i = 0; i < __arraycount(nvmm_impl_list); i++) {
    787 		if (!(*nvmm_impl_list[i]->ident)()) {
    788 			continue;
    789 		}
    790 		nvmm_impl = nvmm_impl_list[i];
    791 		break;
    792 	}
    793 	if (nvmm_impl == NULL) {
    794 		printf("[!] No implementation found\n");
    795 		return ENOTSUP;
    796 	}
    797 
    798 	for (i = 0; i < NVMM_MAX_MACHINES; i++) {
    799 		machines[i].machid = i;
    800 		rw_init(&machines[i].lock);
    801 		for (n = 0; n < NVMM_MAX_VCPUS; n++) {
    802 			mutex_init(&machines[i].cpus[n].lock, MUTEX_DEFAULT,
    803 			    IPL_NONE);
    804 			machines[i].cpus[n].hcpu_last = -1;
    805 		}
    806 	}
    807 
    808 	(*nvmm_impl->init)();
    809 
    810 	return 0;
    811 }
    812 
    813 static void
    814 nvmm_fini(void)
    815 {
    816 	size_t i, n;
    817 
    818 	for (i = 0; i < NVMM_MAX_MACHINES; i++) {
    819 		rw_destroy(&machines[i].lock);
    820 		for (n = 0; n < NVMM_MAX_VCPUS; n++) {
    821 			mutex_destroy(&machines[i].cpus[n].lock);
    822 		}
    823 		/* TODO need to free stuff, etc */
    824 	}
    825 
    826 	(*nvmm_impl->fini)();
    827 }
    828 
    829 /* -------------------------------------------------------------------------- */
    830 
    831 static int
    832 nvmm_open(dev_t dev, int flags, int type, struct lwp *l)
    833 {
    834 	if (minor(dev) != 0) {
    835 		return EXDEV;
    836 	}
    837 
    838 	return 0;
    839 }
    840 
    841 static int
    842 nvmm_close(dev_t dev, int flags, int type, struct lwp *l)
    843 {
    844 	KASSERT(minor(dev) == 0);
    845 
    846 	nvmm_kill_machines(l->l_proc->p_pid);
    847 
    848 	return 0;
    849 }
    850 
    851 static int
    852 nvmm_ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    853 {
    854 	KASSERT(minor(dev) == 0);
    855 
    856 	switch (cmd) {
    857 	case NVMM_IOC_CAPABILITY:
    858 		return nvmm_capability(data);
    859 	case NVMM_IOC_MACHINE_CREATE:
    860 		return nvmm_machine_create(data);
    861 	case NVMM_IOC_MACHINE_DESTROY:
    862 		return nvmm_machine_destroy(data);
    863 	case NVMM_IOC_MACHINE_CONFIGURE:
    864 		return nvmm_machine_configure(data);
    865 	case NVMM_IOC_VCPU_CREATE:
    866 		return nvmm_vcpu_create(data);
    867 	case NVMM_IOC_VCPU_DESTROY:
    868 		return nvmm_vcpu_destroy(data);
    869 	case NVMM_IOC_VCPU_SETSTATE:
    870 		return nvmm_vcpu_setstate(data);
    871 	case NVMM_IOC_VCPU_GETSTATE:
    872 		return nvmm_vcpu_getstate(data);
    873 	case NVMM_IOC_VCPU_INJECT:
    874 		return nvmm_vcpu_inject(data);
    875 	case NVMM_IOC_VCPU_RUN:
    876 		return nvmm_vcpu_run(data);
    877 	case NVMM_IOC_GPA_MAP:
    878 		return nvmm_gpa_map(data);
    879 	case NVMM_IOC_GPA_UNMAP:
    880 		return nvmm_gpa_unmap(data);
    881 	case NVMM_IOC_HVA_MAP:
    882 		return nvmm_hva_map(data);
    883 	case NVMM_IOC_HVA_UNMAP:
    884 		return nvmm_hva_unmap(data);
    885 	default:
    886 		return EINVAL;
    887 	}
    888 }
    889 
    890 const struct cdevsw nvmm_cdevsw = {
    891 	.d_open = nvmm_open,
    892 	.d_close = nvmm_close,
    893 	.d_read = noread,
    894 	.d_write = nowrite,
    895 	.d_ioctl = nvmm_ioctl,
    896 	.d_stop = nostop,
    897 	.d_tty = notty,
    898 	.d_poll = nopoll,
    899 	.d_mmap = nommap,
    900 	.d_kqfilter = nokqfilter,
    901 	.d_discard = nodiscard,
    902 	.d_flag = D_OTHER | D_MPSAFE
    903 };
    904 
    905 void
    906 nvmmattach(int nunits)
    907 {
    908 	/* nothing */
    909 }
    910 
    911 MODULE(MODULE_CLASS_DRIVER, nvmm, NULL);
    912 
    913 static int
    914 nvmm_modcmd(modcmd_t cmd, void *arg)
    915 {
    916 	int error;
    917 
    918 	switch (cmd) {
    919 	case MODULE_CMD_INIT:
    920 		error = nvmm_init();
    921 		if (error)
    922 			return error;
    923 
    924 #if defined(_MODULE)
    925 		{
    926 			devmajor_t bmajor = NODEVMAJOR;
    927 			devmajor_t cmajor = 345;
    928 
    929 			/* mknod /dev/nvmm c 345 0 */
    930 			error = devsw_attach("nvmm", NULL, &bmajor,
    931 			    &nvmm_cdevsw, &cmajor);
    932 			if (error) {
    933 				nvmm_fini();
    934 				return error;
    935 			}
    936 		}
    937 #endif
    938 		return 0;
    939 
    940 	case MODULE_CMD_FINI:
    941 #if defined(_MODULE)
    942 		{
    943 			error = devsw_detach(NULL, &nvmm_cdevsw);
    944 			if (error) {
    945 				return error;
    946 			}
    947 		}
    948 #endif
    949 		nvmm_fini();
    950 		return 0;
    951 
    952 	default:
    953 		return ENOTTY;
    954 	}
    955 }
    956