Home | History | Annotate | Line # | Download | only in nvmm
nvmm.c revision 1.48
      1 /*	$NetBSD: nvmm.c,v 1.48 2026/02/08 10:03:52 nia Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net
      5  * All rights reserved.
      6  *
      7  * This code is part of the NVMM hypervisor.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     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.
     17  *
     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
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.48 2026/02/08 10:03:52 nia Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/kernel.h>
     37 
     38 #include <sys/atomic.h>
     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/device.h>
     48 
     49 #include <uvm/uvm_aobj.h>
     50 #include <uvm/uvm_extern.h>
     51 #include <uvm/uvm_page.h>
     52 
     53 #include "ioconf.h"
     54 
     55 #include <dev/nvmm/nvmm.h>
     56 #include <dev/nvmm/nvmm_internal.h>
     57 #include <dev/nvmm/nvmm_ioctl.h>
     58 
     59 static struct nvmm_machine machines[NVMM_MAX_MACHINES];
     60 static volatile unsigned int nmachines __cacheline_aligned;
     61 
     62 static struct {
     63 	kmutex_t	lock;
     64 	kcondvar_t	suspendcv;
     65 	kcondvar_t	resumecv;
     66 	unsigned	users;
     67 } suspension;
     68 
     69 volatile bool nvmm_suspending;
     70 
     71 static const struct nvmm_impl *nvmm_impl_list[] = {
     72 #if defined(__x86_64__)
     73 	&nvmm_x86_svm,	/* x86 AMD SVM */
     74 	&nvmm_x86_vmx	/* x86 Intel VMX */
     75 #endif
     76 };
     77 
     78 static const struct nvmm_impl *nvmm_impl __read_mostly = NULL;
     79 
     80 static struct nvmm_owner root_owner;
     81 
     82 /* -------------------------------------------------------------------------- */
     83 
     84 static int
     85 nvmm_enter_sig(void)
     86 {
     87 	int error;
     88 
     89 	mutex_enter(&suspension.lock);
     90 	while (nvmm_suspending) {
     91 		error = cv_wait_sig(&suspension.resumecv, &suspension.lock);
     92 		if (error)
     93 			goto out;
     94 	}
     95 	KASSERT(suspension.users < UINT_MAX);
     96 	suspension.users++;
     97 	error = 0;
     98 out:	mutex_exit(&suspension.lock);
     99 
    100 	return 0;
    101 }
    102 
    103 static void
    104 nvmm_enter(void)
    105 {
    106 
    107 	mutex_enter(&suspension.lock);
    108 	while (nvmm_suspending)
    109 		cv_wait(&suspension.resumecv, &suspension.lock);
    110 	KASSERT(suspension.users < UINT_MAX);
    111 	suspension.users++;
    112 	mutex_exit(&suspension.lock);
    113 }
    114 
    115 static void
    116 nvmm_exit(void)
    117 {
    118 
    119 	mutex_enter(&suspension.lock);
    120 	KASSERT(suspension.users > 0);
    121 	if (--suspension.users == 0)
    122 		cv_signal(&suspension.suspendcv);
    123 	mutex_exit(&suspension.lock);
    124 }
    125 
    126 /* -------------------------------------------------------------------------- */
    127 
    128 static int
    129 nvmm_machine_alloc(struct nvmm_machine **ret)
    130 {
    131 	struct nvmm_machine *mach;
    132 	size_t i;
    133 
    134 	for (i = 0; i < NVMM_MAX_MACHINES; i++) {
    135 		mach = &machines[i];
    136 
    137 		rw_enter(&mach->lock, RW_WRITER);
    138 		if (mach->present) {
    139 			rw_exit(&mach->lock);
    140 			continue;
    141 		}
    142 
    143 		mach->present = true;
    144 		mach->time = time_second;
    145 		*ret = mach;
    146 		atomic_inc_uint(&nmachines);
    147 		return 0;
    148 	}
    149 
    150 	return ENOBUFS;
    151 }
    152 
    153 static void
    154 nvmm_machine_free(struct nvmm_machine *mach)
    155 {
    156 	KASSERT(rw_write_held(&mach->lock));
    157 	KASSERT(mach->present);
    158 	mach->present = false;
    159 	atomic_dec_uint(&nmachines);
    160 }
    161 
    162 static int
    163 nvmm_machine_get(struct nvmm_owner *owner, nvmm_machid_t machid,
    164     struct nvmm_machine **ret, bool writer)
    165 {
    166 	struct nvmm_machine *mach;
    167 	krw_t op = writer ? RW_WRITER : RW_READER;
    168 
    169 	if (__predict_false(machid >= NVMM_MAX_MACHINES)) {
    170 		return EINVAL;
    171 	}
    172 	mach = &machines[machid];
    173 
    174 	rw_enter(&mach->lock, op);
    175 	if (__predict_false(!mach->present)) {
    176 		rw_exit(&mach->lock);
    177 		return ENOENT;
    178 	}
    179 	if (__predict_false(mach->owner != owner && owner != &root_owner)) {
    180 		rw_exit(&mach->lock);
    181 		return EPERM;
    182 	}
    183 	*ret = mach;
    184 
    185 	return 0;
    186 }
    187 
    188 static void
    189 nvmm_machine_put(struct nvmm_machine *mach)
    190 {
    191 	rw_exit(&mach->lock);
    192 }
    193 
    194 /* -------------------------------------------------------------------------- */
    195 
    196 static int
    197 nvmm_vcpu_alloc(struct nvmm_machine *mach, nvmm_cpuid_t cpuid,
    198     struct nvmm_cpu **ret)
    199 {
    200 	struct nvmm_cpu *vcpu;
    201 
    202 	if (cpuid >= NVMM_MAX_VCPUS) {
    203 		return EINVAL;
    204 	}
    205 	vcpu = &mach->cpus[cpuid];
    206 
    207 	mutex_enter(&vcpu->lock);
    208 	if (vcpu->present) {
    209 		mutex_exit(&vcpu->lock);
    210 		return EBUSY;
    211 	}
    212 
    213 	vcpu->present = true;
    214 	vcpu->comm = NULL;
    215 	vcpu->hcpu_last = -1;
    216 	*ret = vcpu;
    217 	return 0;
    218 }
    219 
    220 static void
    221 nvmm_vcpu_free(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
    222 {
    223 	KASSERT(mutex_owned(&vcpu->lock));
    224 	vcpu->present = false;
    225 	if (vcpu->comm != NULL) {
    226 		uvm_deallocate(kernel_map, (vaddr_t)vcpu->comm, PAGE_SIZE);
    227 	}
    228 }
    229 
    230 static int
    231 nvmm_vcpu_get(struct nvmm_machine *mach, nvmm_cpuid_t cpuid,
    232     struct nvmm_cpu **ret)
    233 {
    234 	struct nvmm_cpu *vcpu;
    235 
    236 	if (__predict_false(cpuid >= NVMM_MAX_VCPUS)) {
    237 		return EINVAL;
    238 	}
    239 	vcpu = &mach->cpus[cpuid];
    240 
    241 	mutex_enter(&vcpu->lock);
    242 	if (__predict_false(!vcpu->present)) {
    243 		mutex_exit(&vcpu->lock);
    244 		return ENOENT;
    245 	}
    246 	*ret = vcpu;
    247 
    248 	return 0;
    249 }
    250 
    251 static void
    252 nvmm_vcpu_put(struct nvmm_cpu *vcpu)
    253 {
    254 	mutex_exit(&vcpu->lock);
    255 }
    256 
    257 /* -------------------------------------------------------------------------- */
    258 
    259 static void
    260 nvmm_kill_machines(struct nvmm_owner *owner)
    261 {
    262 	struct nvmm_machine *mach;
    263 	struct nvmm_cpu *vcpu;
    264 	size_t i, j;
    265 	int error;
    266 
    267 	for (i = 0; i < NVMM_MAX_MACHINES; i++) {
    268 		mach = &machines[i];
    269 
    270 		rw_enter(&mach->lock, RW_WRITER);
    271 		if (!mach->present || mach->owner != owner) {
    272 			rw_exit(&mach->lock);
    273 			continue;
    274 		}
    275 
    276 		/* Kill it. */
    277 		for (j = 0; j < NVMM_MAX_VCPUS; j++) {
    278 			error = nvmm_vcpu_get(mach, j, &vcpu);
    279 			if (error)
    280 				continue;
    281 			(*nvmm_impl->vcpu_destroy)(mach, vcpu);
    282 			nvmm_vcpu_free(mach, vcpu);
    283 			nvmm_vcpu_put(vcpu);
    284 			atomic_dec_uint(&mach->ncpus);
    285 		}
    286 		(*nvmm_impl->machine_destroy)(mach);
    287 		uvmspace_free(mach->vm);
    288 
    289 		/* Drop the kernel UOBJ refs. */
    290 		for (j = 0; j < NVMM_MAX_HMAPPINGS; j++) {
    291 			if (!mach->hmap[j].present)
    292 				continue;
    293 			uao_detach(mach->hmap[j].uobj);
    294 		}
    295 
    296 		nvmm_machine_free(mach);
    297 
    298 		rw_exit(&mach->lock);
    299 	}
    300 }
    301 
    302 /* -------------------------------------------------------------------------- */
    303 
    304 static int
    305 nvmm_capability(struct nvmm_owner *owner, struct nvmm_ioc_capability *args)
    306 {
    307 	args->cap.version = NVMM_KERN_VERSION;
    308 	args->cap.state_size = nvmm_impl->state_size;
    309 	args->cap.max_machines = NVMM_MAX_MACHINES;
    310 	args->cap.max_vcpus = NVMM_MAX_VCPUS;
    311 	args->cap.max_ram = NVMM_MAX_RAM;
    312 
    313 	(*nvmm_impl->capability)(&args->cap);
    314 
    315 	return 0;
    316 }
    317 
    318 static int
    319 nvmm_machine_create(struct nvmm_owner *owner,
    320     struct nvmm_ioc_machine_create *args)
    321 {
    322 	struct nvmm_machine *mach;
    323 	int error;
    324 
    325 	error = nvmm_machine_alloc(&mach);
    326 	if (error)
    327 		return error;
    328 
    329 	/* Curproc owns the machine. */
    330 	mach->owner = owner;
    331 
    332 	/* Zero out the host mappings. */
    333 	memset(&mach->hmap, 0, sizeof(mach->hmap));
    334 
    335 	/* Create the machine vmspace. */
    336 	mach->gpa_begin = 0;
    337 	mach->gpa_end = NVMM_MAX_RAM;
    338 	mach->vm = uvmspace_alloc(0, mach->gpa_end - mach->gpa_begin, false);
    339 
    340 	/* Create the comm uobj. */
    341 	mach->commuobj = uao_create(NVMM_MAX_VCPUS * PAGE_SIZE, 0);
    342 
    343 	(*nvmm_impl->machine_create)(mach);
    344 
    345 	args->machid = mach->machid;
    346 	nvmm_machine_put(mach);
    347 
    348 	return 0;
    349 }
    350 
    351 static int
    352 nvmm_machine_destroy(struct nvmm_owner *owner,
    353     struct nvmm_ioc_machine_destroy *args)
    354 {
    355 	struct nvmm_machine *mach;
    356 	struct nvmm_cpu *vcpu;
    357 	int error;
    358 	size_t i;
    359 
    360 	error = nvmm_machine_get(owner, args->machid, &mach, true);
    361 	if (error)
    362 		return error;
    363 
    364 	for (i = 0; i < NVMM_MAX_VCPUS; i++) {
    365 		error = nvmm_vcpu_get(mach, i, &vcpu);
    366 		if (error)
    367 			continue;
    368 
    369 		(*nvmm_impl->vcpu_destroy)(mach, vcpu);
    370 		nvmm_vcpu_free(mach, vcpu);
    371 		nvmm_vcpu_put(vcpu);
    372 		atomic_dec_uint(&mach->ncpus);
    373 	}
    374 
    375 	(*nvmm_impl->machine_destroy)(mach);
    376 
    377 	/* Free the machine vmspace. */
    378 	uvmspace_free(mach->vm);
    379 
    380 	/* Drop the kernel UOBJ refs. */
    381 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
    382 		if (!mach->hmap[i].present)
    383 			continue;
    384 		uao_detach(mach->hmap[i].uobj);
    385 	}
    386 
    387 	nvmm_machine_free(mach);
    388 	nvmm_machine_put(mach);
    389 
    390 	return 0;
    391 }
    392 
    393 static int
    394 nvmm_machine_configure(struct nvmm_owner *owner,
    395     struct nvmm_ioc_machine_configure *args)
    396 {
    397 	struct nvmm_machine *mach;
    398 	size_t allocsz;
    399 	uint64_t op;
    400 	void *data;
    401 	int error;
    402 
    403 	op = NVMM_MACH_CONF_MD(args->op);
    404 	if (__predict_false(op >= nvmm_impl->mach_conf_max)) {
    405 		return EINVAL;
    406 	}
    407 
    408 	allocsz = nvmm_impl->mach_conf_sizes[op];
    409 	data = kmem_alloc(allocsz, KM_SLEEP);
    410 
    411 	error = nvmm_machine_get(owner, args->machid, &mach, true);
    412 	if (error) {
    413 		kmem_free(data, allocsz);
    414 		return error;
    415 	}
    416 
    417 	error = copyin(args->conf, data, allocsz);
    418 	if (error) {
    419 		goto out;
    420 	}
    421 
    422 	error = (*nvmm_impl->machine_configure)(mach, op, data);
    423 
    424 out:
    425 	nvmm_machine_put(mach);
    426 	kmem_free(data, allocsz);
    427 	return error;
    428 }
    429 
    430 static int
    431 nvmm_vcpu_create(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_create *args)
    432 {
    433 	struct nvmm_machine *mach;
    434 	struct nvmm_cpu *vcpu;
    435 	int error;
    436 
    437 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    438 	if (error)
    439 		return error;
    440 
    441 	error = nvmm_vcpu_alloc(mach, args->cpuid, &vcpu);
    442 	if (error)
    443 		goto out;
    444 
    445 	/* Allocate the comm page. */
    446 	uao_reference(mach->commuobj);
    447 	error = uvm_map(kernel_map, (vaddr_t *)&vcpu->comm, PAGE_SIZE,
    448 	    mach->commuobj, args->cpuid * PAGE_SIZE, 0, UVM_MAPFLAG(UVM_PROT_RW,
    449 	    UVM_PROT_RW, UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
    450 	if (error) {
    451 		uao_detach(mach->commuobj);
    452 		nvmm_vcpu_free(mach, vcpu);
    453 		nvmm_vcpu_put(vcpu);
    454 		goto out;
    455 	}
    456 	error = uvm_map_pageable(kernel_map, (vaddr_t)vcpu->comm,
    457 	    (vaddr_t)vcpu->comm + PAGE_SIZE, false, 0);
    458 	if (error) {
    459 		nvmm_vcpu_free(mach, vcpu);
    460 		nvmm_vcpu_put(vcpu);
    461 		goto out;
    462 	}
    463 	memset(vcpu->comm, 0, PAGE_SIZE);
    464 
    465 	error = (*nvmm_impl->vcpu_create)(mach, vcpu);
    466 	if (error) {
    467 		nvmm_vcpu_free(mach, vcpu);
    468 		nvmm_vcpu_put(vcpu);
    469 		goto out;
    470 	}
    471 
    472 	nvmm_vcpu_put(vcpu);
    473 	atomic_inc_uint(&mach->ncpus);
    474 
    475 out:
    476 	nvmm_machine_put(mach);
    477 	return error;
    478 }
    479 
    480 static int
    481 nvmm_vcpu_destroy(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_destroy *args)
    482 {
    483 	struct nvmm_machine *mach;
    484 	struct nvmm_cpu *vcpu;
    485 	int error;
    486 
    487 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    488 	if (error)
    489 		return error;
    490 
    491 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    492 	if (error)
    493 		goto out;
    494 
    495 	(*nvmm_impl->vcpu_destroy)(mach, vcpu);
    496 	nvmm_vcpu_free(mach, vcpu);
    497 	nvmm_vcpu_put(vcpu);
    498 	atomic_dec_uint(&mach->ncpus);
    499 
    500 out:
    501 	nvmm_machine_put(mach);
    502 	return error;
    503 }
    504 
    505 static int
    506 nvmm_vcpu_configure(struct nvmm_owner *owner,
    507     struct nvmm_ioc_vcpu_configure *args)
    508 {
    509 	struct nvmm_machine *mach;
    510 	struct nvmm_cpu *vcpu;
    511 	size_t allocsz;
    512 	uint64_t op;
    513 	void *data;
    514 	int error;
    515 
    516 	op = NVMM_VCPU_CONF_MD(args->op);
    517 	if (__predict_false(op >= nvmm_impl->vcpu_conf_max))
    518 		return EINVAL;
    519 
    520 	allocsz = nvmm_impl->vcpu_conf_sizes[op];
    521 	data = kmem_alloc(allocsz, KM_SLEEP);
    522 
    523 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    524 	if (error) {
    525 		kmem_free(data, allocsz);
    526 		return error;
    527 	}
    528 
    529 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    530 	if (error) {
    531 		nvmm_machine_put(mach);
    532 		kmem_free(data, allocsz);
    533 		return error;
    534 	}
    535 
    536 	error = copyin(args->conf, data, allocsz);
    537 	if (error) {
    538 		goto out;
    539 	}
    540 
    541 	error = (*nvmm_impl->vcpu_configure)(vcpu, op, data);
    542 
    543 out:
    544 	nvmm_vcpu_put(vcpu);
    545 	nvmm_machine_put(mach);
    546 	kmem_free(data, allocsz);
    547 	return error;
    548 }
    549 
    550 static int
    551 nvmm_vcpu_setstate(struct nvmm_owner *owner,
    552     struct nvmm_ioc_vcpu_setstate *args)
    553 {
    554 	struct nvmm_machine *mach;
    555 	struct nvmm_cpu *vcpu;
    556 	int error;
    557 
    558 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    559 	if (error)
    560 		return error;
    561 
    562 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    563 	if (error)
    564 		goto out;
    565 
    566 	(*nvmm_impl->vcpu_setstate)(vcpu);
    567 	nvmm_vcpu_put(vcpu);
    568 
    569 out:
    570 	nvmm_machine_put(mach);
    571 	return error;
    572 }
    573 
    574 static int
    575 nvmm_vcpu_getstate(struct nvmm_owner *owner,
    576     struct nvmm_ioc_vcpu_getstate *args)
    577 {
    578 	struct nvmm_machine *mach;
    579 	struct nvmm_cpu *vcpu;
    580 	int error;
    581 
    582 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    583 	if (error)
    584 		return error;
    585 
    586 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    587 	if (error)
    588 		goto out;
    589 
    590 	(*nvmm_impl->vcpu_getstate)(vcpu);
    591 	nvmm_vcpu_put(vcpu);
    592 
    593 out:
    594 	nvmm_machine_put(mach);
    595 	return error;
    596 }
    597 
    598 static int
    599 nvmm_vcpu_inject(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_inject *args)
    600 {
    601 	struct nvmm_machine *mach;
    602 	struct nvmm_cpu *vcpu;
    603 	int error;
    604 
    605 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    606 	if (error)
    607 		return error;
    608 
    609 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    610 	if (error)
    611 		goto out;
    612 
    613 	error = (*nvmm_impl->vcpu_inject)(vcpu);
    614 	nvmm_vcpu_put(vcpu);
    615 
    616 out:
    617 	nvmm_machine_put(mach);
    618 	return error;
    619 }
    620 
    621 static int
    622 nvmm_do_vcpu_run(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
    623     struct nvmm_vcpu_exit *exit)
    624 {
    625 	struct vmspace *vm = mach->vm;
    626 	int ret;
    627 
    628 	while (1) {
    629 		/* Got a signal? Or pending resched? Leave. */
    630 		if (__predict_false(nvmm_return_needed(vcpu, exit))) {
    631 			return 0;
    632 		}
    633 
    634 		/* Run the VCPU. */
    635 		ret = (*nvmm_impl->vcpu_run)(mach, vcpu, exit);
    636 		if (__predict_false(ret != 0)) {
    637 			return ret;
    638 		}
    639 
    640 		/* Process nested page faults. */
    641 		if (__predict_true(exit->reason != NVMM_VCPU_EXIT_MEMORY)) {
    642 			break;
    643 		}
    644 		if (exit->u.mem.gpa >= mach->gpa_end) {
    645 			break;
    646 		}
    647 		if (uvm_fault(&vm->vm_map, exit->u.mem.gpa, exit->u.mem.prot)) {
    648 			break;
    649 		}
    650 	}
    651 
    652 	return 0;
    653 }
    654 
    655 static int
    656 nvmm_vcpu_run(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_run *args)
    657 {
    658 	struct nvmm_machine *mach;
    659 	struct nvmm_cpu *vcpu = NULL;
    660 	int error;
    661 
    662 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    663 	if (error)
    664 		return error;
    665 
    666 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    667 	if (error)
    668 		goto out;
    669 
    670 	error = nvmm_do_vcpu_run(mach, vcpu, &args->exit);
    671 	nvmm_vcpu_put(vcpu);
    672 
    673 out:
    674 	nvmm_machine_put(mach);
    675 	if (vcpu)
    676 		vcpu->comm->stop = 0;
    677 	return error;
    678 }
    679 
    680 /* -------------------------------------------------------------------------- */
    681 
    682 static struct uvm_object *
    683 nvmm_hmapping_getuobj(struct nvmm_machine *mach, uintptr_t hva, size_t size,
    684    size_t *off)
    685 {
    686 	struct nvmm_hmapping *hmapping;
    687 	size_t i;
    688 
    689 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
    690 		hmapping = &mach->hmap[i];
    691 		if (!hmapping->present) {
    692 			continue;
    693 		}
    694 		if (hva >= hmapping->hva &&
    695 		    hva + size <= hmapping->hva + hmapping->size) {
    696 			*off = hva - hmapping->hva;
    697 			return hmapping->uobj;
    698 		}
    699 	}
    700 
    701 	return NULL;
    702 }
    703 
    704 static int
    705 nvmm_hmapping_validate(struct nvmm_machine *mach, uintptr_t hva, size_t size)
    706 {
    707 	struct nvmm_hmapping *hmapping;
    708 	size_t i;
    709 	uintptr_t hva_end;
    710 	uintptr_t hmap_end;
    711 
    712 	if ((hva % PAGE_SIZE) != 0 || (size % PAGE_SIZE) != 0) {
    713 		return EINVAL;
    714 	}
    715 	if (hva == 0) {
    716 		return EINVAL;
    717 	}
    718 
    719 	/*
    720 	 * Overflow tests MUST be done very carefully to avoid compiler
    721 	 * optimizations from effectively deleting the test.
    722 	 */
    723 	hva_end = hva + size;
    724 	if (hva_end <= hva)
    725 		return EINVAL;
    726 
    727 	/*
    728 	 * Overlap tests
    729 	 */
    730 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
    731 		hmapping = &mach->hmap[i];
    732 
    733 		if (!hmapping->present) {
    734 			continue;
    735 		}
    736 		hmap_end = hmapping->hva + hmapping->size;
    737 
    738 		if (hva >= hmapping->hva && hva_end <= hmap_end)
    739 			break;
    740 		if (hva >= hmapping->hva && hva < hmap_end)
    741 			return EEXIST;
    742 		if (hva_end > hmapping->hva && hva_end <= hmap_end)
    743 			return EEXIST;
    744 		if (hva <= hmapping->hva && hva_end >= hmap_end)
    745 			return EEXIST;
    746 	}
    747 
    748 	return 0;
    749 }
    750 
    751 static struct nvmm_hmapping *
    752 nvmm_hmapping_alloc(struct nvmm_machine *mach)
    753 {
    754 	struct nvmm_hmapping *hmapping;
    755 	size_t i;
    756 
    757 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
    758 		hmapping = &mach->hmap[i];
    759 		if (!hmapping->present) {
    760 			hmapping->present = true;
    761 			return hmapping;
    762 		}
    763 	}
    764 
    765 	return NULL;
    766 }
    767 
    768 static int
    769 nvmm_hmapping_free(struct nvmm_machine *mach, uintptr_t hva, size_t size)
    770 {
    771 	struct vmspace *vmspace = curproc->p_vmspace;
    772 	struct nvmm_hmapping *hmapping;
    773 	size_t i;
    774 
    775 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
    776 		hmapping = &mach->hmap[i];
    777 		if (!hmapping->present || hmapping->hva != hva ||
    778 		    hmapping->size != size) {
    779 			continue;
    780 		}
    781 
    782 		uvm_unmap(&vmspace->vm_map, hmapping->hva,
    783 		    hmapping->hva + hmapping->size);
    784 		uao_detach(hmapping->uobj);
    785 
    786 		hmapping->uobj = NULL;
    787 		hmapping->present = false;
    788 
    789 		return 0;
    790 	}
    791 
    792 	return ENOENT;
    793 }
    794 
    795 static int
    796 nvmm_hva_map(struct nvmm_owner *owner, struct nvmm_ioc_hva_map *args)
    797 {
    798 	struct vmspace *vmspace = curproc->p_vmspace;
    799 	struct nvmm_machine *mach;
    800 	struct nvmm_hmapping *hmapping;
    801 	vaddr_t uva;
    802 	int error;
    803 
    804 	error = nvmm_machine_get(owner, args->machid, &mach, true);
    805 	if (error)
    806 		return error;
    807 
    808 	error = nvmm_hmapping_validate(mach, args->hva, args->size);
    809 	if (error)
    810 		goto out;
    811 
    812 	hmapping = nvmm_hmapping_alloc(mach);
    813 	if (hmapping == NULL) {
    814 		error = ENOBUFS;
    815 		goto out;
    816 	}
    817 
    818 	hmapping->hva = args->hva;
    819 	hmapping->size = args->size;
    820 	hmapping->uobj = uao_create(hmapping->size, 0);
    821 	uva = hmapping->hva;
    822 
    823 	/* Take a reference for the user. */
    824 	uao_reference(hmapping->uobj);
    825 
    826 	/* Map the uobj into the user address space, as pageable. */
    827 	error = uvm_map(&vmspace->vm_map, &uva, hmapping->size, hmapping->uobj,
    828 	    0, 0, UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_SHARE,
    829 	    UVM_ADV_RANDOM, UVM_FLAG_FIXED|UVM_FLAG_UNMAP));
    830 	if (error) {
    831 		uao_detach(hmapping->uobj);
    832 	}
    833 
    834 out:
    835 	nvmm_machine_put(mach);
    836 	return error;
    837 }
    838 
    839 static int
    840 nvmm_hva_unmap(struct nvmm_owner *owner, struct nvmm_ioc_hva_unmap *args)
    841 {
    842 	struct nvmm_machine *mach;
    843 	int error;
    844 
    845 	error = nvmm_machine_get(owner, args->machid, &mach, true);
    846 	if (error)
    847 		return error;
    848 
    849 	error = nvmm_hmapping_free(mach, args->hva, args->size);
    850 
    851 	nvmm_machine_put(mach);
    852 	return error;
    853 }
    854 
    855 /* -------------------------------------------------------------------------- */
    856 
    857 static int
    858 nvmm_gpa_map(struct nvmm_owner *owner, struct nvmm_ioc_gpa_map *args)
    859 {
    860 	struct nvmm_machine *mach;
    861 	struct uvm_object *uobj;
    862 	gpaddr_t gpa;
    863 	gpaddr_t gpa_end;
    864 	size_t off;
    865 	int error;
    866 
    867 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    868 	if (error)
    869 		return error;
    870 
    871 	if ((args->prot & ~(PROT_READ|PROT_WRITE|PROT_EXEC)) != 0) {
    872 		error = EINVAL;
    873 		goto out;
    874 	}
    875 
    876 	/*
    877 	 * Overflow tests MUST be done very carefully to avoid compiler
    878 	 * optimizations from effectively deleting the test.
    879 	 */
    880 	gpa = args->gpa;
    881 	gpa_end = gpa + args->size;
    882 	if (gpa_end <= gpa) {
    883 		error = EINVAL;
    884 		goto out;
    885 	}
    886 
    887 	if ((gpa % PAGE_SIZE) != 0 || (args->size % PAGE_SIZE) != 0 ||
    888 	    (args->hva % PAGE_SIZE) != 0) {
    889 		error = EINVAL;
    890 		goto out;
    891 	}
    892 	if (args->hva == 0) {
    893 		error = EINVAL;
    894 		goto out;
    895 	}
    896 
    897 	if (gpa < mach->gpa_begin || gpa >= mach->gpa_end) {
    898 		error = EINVAL;
    899 		goto out;
    900 	}
    901 	if (gpa_end  > mach->gpa_end) {
    902 		error = EINVAL;
    903 		goto out;
    904 	}
    905 
    906 	uobj = nvmm_hmapping_getuobj(mach, args->hva, args->size, &off);
    907 	if (uobj == NULL) {
    908 		error = EINVAL;
    909 		goto out;
    910 	}
    911 
    912 	/* Take a reference for the machine. */
    913 	uao_reference(uobj);
    914 
    915 	/* Map the uobj into the machine address space, as pageable. */
    916 	error = uvm_map(&mach->vm->vm_map, &gpa, args->size, uobj, off, 0,
    917 	    UVM_MAPFLAG(args->prot, UVM_PROT_RWX, UVM_INH_NONE,
    918 	    UVM_ADV_RANDOM, UVM_FLAG_FIXED|UVM_FLAG_UNMAP));
    919 	if (error) {
    920 		uao_detach(uobj);
    921 		goto out;
    922 	}
    923 	if (gpa != args->gpa) {
    924 		uao_detach(uobj);
    925 		printf("[!] uvm_map problem\n");
    926 		error = EINVAL;
    927 		goto out;
    928 	}
    929 
    930 out:
    931 	nvmm_machine_put(mach);
    932 	return error;
    933 }
    934 
    935 static int
    936 nvmm_gpa_unmap(struct nvmm_owner *owner, struct nvmm_ioc_gpa_unmap *args)
    937 {
    938 	struct nvmm_machine *mach;
    939 	gpaddr_t gpa;
    940 	gpaddr_t gpa_end;
    941 	int error;
    942 
    943 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    944 	if (error)
    945 		return error;
    946 
    947 	/*
    948 	 * Overflow tests MUST be done very carefully to avoid compiler
    949 	 * optimizations from effectively deleting the test.
    950 	 */
    951 	gpa = args->gpa;
    952 	gpa_end = gpa + args->size;
    953 	if (gpa_end <= gpa) {
    954 		error = EINVAL;
    955 		goto out;
    956 	}
    957 
    958 	if ((gpa % PAGE_SIZE) != 0 || (args->size % PAGE_SIZE) != 0) {
    959 		error = EINVAL;
    960 		goto out;
    961 	}
    962 	if (gpa < mach->gpa_begin || gpa >= mach->gpa_end) {
    963 		error = EINVAL;
    964 		goto out;
    965 	}
    966 	if (gpa_end >= mach->gpa_end) {
    967 		error = EINVAL;
    968 		goto out;
    969 	}
    970 
    971 	/* Unmap the memory from the machine. */
    972 	uvm_unmap(&mach->vm->vm_map, gpa, gpa + args->size);
    973 
    974 out:
    975 	nvmm_machine_put(mach);
    976 	return error;
    977 }
    978 
    979 /* -------------------------------------------------------------------------- */
    980 
    981 static int
    982 nvmm_ctl_mach_info(struct nvmm_owner *owner, struct nvmm_ioc_ctl *args)
    983 {
    984 	struct nvmm_ctl_mach_info ctl;
    985 	struct nvmm_machine *mach;
    986 	int error;
    987 	size_t i;
    988 
    989 	if (args->size != sizeof(ctl))
    990 		return EINVAL;
    991 	error = copyin(args->data, &ctl, sizeof(ctl));
    992 	if (error)
    993 		return error;
    994 
    995 	error = nvmm_machine_get(owner, ctl.machid, &mach, true);
    996 	if (error)
    997 		return error;
    998 
    999 	ctl.nvcpus = mach->ncpus;
   1000 
   1001 	ctl.nram = 0;
   1002 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
   1003 		if (!mach->hmap[i].present)
   1004 			continue;
   1005 		ctl.nram += mach->hmap[i].size;
   1006 	}
   1007 
   1008 	ctl.pid = mach->owner->pid;
   1009 	ctl.time = mach->time;
   1010 
   1011 	nvmm_machine_put(mach);
   1012 
   1013 	error = copyout(&ctl, args->data, sizeof(ctl));
   1014 	if (error)
   1015 		return error;
   1016 
   1017 	return 0;
   1018 }
   1019 
   1020 static int
   1021 nvmm_ctl(struct nvmm_owner *owner, struct nvmm_ioc_ctl *args)
   1022 {
   1023 	switch (args->op) {
   1024 	case NVMM_CTL_MACH_INFO:
   1025 		return nvmm_ctl_mach_info(owner, args);
   1026 	default:
   1027 		return EINVAL;
   1028 	}
   1029 }
   1030 
   1031 /* -------------------------------------------------------------------------- */
   1032 
   1033 static const struct nvmm_impl *
   1034 nvmm_ident(void)
   1035 {
   1036 	size_t i;
   1037 
   1038 	for (i = 0; i < __arraycount(nvmm_impl_list); i++) {
   1039 		if ((*nvmm_impl_list[i]->ident)())
   1040 			return nvmm_impl_list[i];
   1041 	}
   1042 
   1043 	return NULL;
   1044 }
   1045 
   1046 static int
   1047 nvmm_init(void)
   1048 {
   1049 	size_t i, n;
   1050 
   1051 	nvmm_impl = nvmm_ident();
   1052 	if (nvmm_impl == NULL)
   1053 		return ENOTSUP;
   1054 
   1055 	for (i = 0; i < NVMM_MAX_MACHINES; i++) {
   1056 		machines[i].machid = i;
   1057 		rw_init(&machines[i].lock);
   1058 		for (n = 0; n < NVMM_MAX_VCPUS; n++) {
   1059 			machines[i].cpus[n].present = false;
   1060 			machines[i].cpus[n].cpuid = n;
   1061 			mutex_init(&machines[i].cpus[n].lock, MUTEX_DEFAULT,
   1062 			    IPL_NONE);
   1063 		}
   1064 	}
   1065 
   1066 	mutex_init(&suspension.lock, MUTEX_DEFAULT, IPL_NONE);
   1067 	cv_init(&suspension.suspendcv, "nvmmsus");
   1068 	cv_init(&suspension.resumecv, "nvmmres");
   1069 	suspension.users = 0;
   1070 
   1071 	(*nvmm_impl->init)();
   1072 
   1073 	return 0;
   1074 }
   1075 
   1076 static void
   1077 nvmm_fini(void)
   1078 {
   1079 	size_t i, n;
   1080 
   1081 	for (i = 0; i < NVMM_MAX_MACHINES; i++) {
   1082 		rw_destroy(&machines[i].lock);
   1083 		for (n = 0; n < NVMM_MAX_VCPUS; n++) {
   1084 			mutex_destroy(&machines[i].cpus[n].lock);
   1085 		}
   1086 	}
   1087 
   1088 	(*nvmm_impl->fini)();
   1089 	nvmm_impl = NULL;
   1090 }
   1091 
   1092 /* -------------------------------------------------------------------------- */
   1093 
   1094 static dev_type_open(nvmm_open);
   1095 
   1096 const struct cdevsw nvmm_cdevsw = {
   1097 	.d_open = nvmm_open,
   1098 	.d_close = noclose,
   1099 	.d_read = noread,
   1100 	.d_write = nowrite,
   1101 	.d_ioctl = noioctl,
   1102 	.d_stop = nostop,
   1103 	.d_tty = notty,
   1104 	.d_poll = nopoll,
   1105 	.d_mmap = nommap,
   1106 	.d_kqfilter = nokqfilter,
   1107 	.d_discard = nodiscard,
   1108 	.d_flag = D_OTHER | D_MPSAFE
   1109 };
   1110 
   1111 static int nvmm_ioctl(file_t *, u_long, void *);
   1112 static int nvmm_close(file_t *);
   1113 static int nvmm_mmap(file_t *, off_t *, size_t, int, int *, int *,
   1114     struct uvm_object **, int *);
   1115 
   1116 static const struct fileops nvmm_fileops = {
   1117 	.fo_read = fbadop_read,
   1118 	.fo_write = fbadop_write,
   1119 	.fo_ioctl = nvmm_ioctl,
   1120 	.fo_fcntl = fnullop_fcntl,
   1121 	.fo_poll = fnullop_poll,
   1122 	.fo_stat = fbadop_stat,
   1123 	.fo_close = nvmm_close,
   1124 	.fo_kqfilter = fnullop_kqfilter,
   1125 	.fo_restart = fnullop_restart,
   1126 	.fo_mmap = nvmm_mmap,
   1127 };
   1128 
   1129 static int
   1130 nvmm_open(dev_t dev, int flags, int type, struct lwp *l)
   1131 {
   1132 	struct nvmm_owner *owner;
   1133 	struct file *fp;
   1134 	int error, fd;
   1135 
   1136 	if (__predict_false(nvmm_impl == NULL))
   1137 		return ENXIO;
   1138 	if (minor(dev) != 0)
   1139 		return EXDEV;
   1140 	if (!(flags & O_CLOEXEC))
   1141 		return EINVAL;
   1142 	error = fd_allocfile(&fp, &fd);
   1143 	if (error)
   1144 		return error;
   1145 
   1146 	if (OFLAGS(flags) & O_WRONLY) {
   1147 		owner = &root_owner;
   1148 	} else {
   1149 		owner = kmem_alloc(sizeof(*owner), KM_SLEEP);
   1150 		owner->pid = l->l_proc->p_pid;
   1151 	}
   1152 
   1153 	return fd_clone(fp, fd, flags, &nvmm_fileops, owner);
   1154 }
   1155 
   1156 static int
   1157 nvmm_close(file_t *fp)
   1158 {
   1159 	struct nvmm_owner *owner = fp->f_data;
   1160 
   1161 	KASSERT(owner != NULL);
   1162 
   1163 	nvmm_enter();
   1164 	nvmm_kill_machines(owner);
   1165 	nvmm_exit();
   1166 
   1167 	if (owner != &root_owner) {
   1168 		kmem_free(owner, sizeof(*owner));
   1169 	}
   1170 	fp->f_data = NULL;
   1171 
   1172 	return 0;
   1173 }
   1174 
   1175 static int
   1176 nvmm_mmap(file_t *fp, off_t *offp, size_t size, int prot, int *flagsp,
   1177     int *advicep, struct uvm_object **uobjp, int *maxprotp)
   1178 {
   1179 	struct nvmm_owner *owner = fp->f_data;
   1180 	struct nvmm_machine *mach;
   1181 	nvmm_machid_t machid;
   1182 	nvmm_cpuid_t cpuid;
   1183 	int error;
   1184 
   1185 	KASSERT(size > 0);
   1186 
   1187 	if (prot & PROT_EXEC)
   1188 		return EACCES;
   1189 	if (size != PAGE_SIZE)
   1190 		return EINVAL;
   1191 
   1192 	cpuid = NVMM_COMM_CPUID(*offp);
   1193 	if (__predict_false(cpuid >= NVMM_MAX_VCPUS))
   1194 		return EINVAL;
   1195 
   1196 	machid = NVMM_COMM_MACHID(*offp);
   1197 	error = nvmm_machine_get(owner, machid, &mach, false);
   1198 	if (error)
   1199 		return error;
   1200 
   1201 	uao_reference(mach->commuobj);
   1202 	*uobjp = mach->commuobj;
   1203 	*offp = cpuid * PAGE_SIZE;
   1204 	*maxprotp = prot;
   1205 	*advicep = UVM_ADV_RANDOM;
   1206 
   1207 	nvmm_machine_put(mach);
   1208 	return 0;
   1209 }
   1210 
   1211 static int
   1212 nvmm_ioctl_internal(file_t *fp, u_long cmd, void *data)
   1213 {
   1214 	struct nvmm_owner *owner = fp->f_data;
   1215 
   1216 	KASSERT(owner != NULL);
   1217 
   1218 	switch (cmd) {
   1219 	case NVMM_IOC_CAPABILITY:
   1220 		return nvmm_capability(owner, data);
   1221 	case NVMM_IOC_MACHINE_CREATE:
   1222 		return nvmm_machine_create(owner, data);
   1223 	case NVMM_IOC_MACHINE_DESTROY:
   1224 		return nvmm_machine_destroy(owner, data);
   1225 	case NVMM_IOC_MACHINE_CONFIGURE:
   1226 		return nvmm_machine_configure(owner, data);
   1227 	case NVMM_IOC_VCPU_CREATE:
   1228 		return nvmm_vcpu_create(owner, data);
   1229 	case NVMM_IOC_VCPU_DESTROY:
   1230 		return nvmm_vcpu_destroy(owner, data);
   1231 	case NVMM_IOC_VCPU_CONFIGURE:
   1232 		return nvmm_vcpu_configure(owner, data);
   1233 	case NVMM_IOC_VCPU_SETSTATE:
   1234 		return nvmm_vcpu_setstate(owner, data);
   1235 	case NVMM_IOC_VCPU_GETSTATE:
   1236 		return nvmm_vcpu_getstate(owner, data);
   1237 	case NVMM_IOC_VCPU_INJECT:
   1238 		return nvmm_vcpu_inject(owner, data);
   1239 	case NVMM_IOC_VCPU_RUN:
   1240 		return nvmm_vcpu_run(owner, data);
   1241 	case NVMM_IOC_GPA_MAP:
   1242 		return nvmm_gpa_map(owner, data);
   1243 	case NVMM_IOC_GPA_UNMAP:
   1244 		return nvmm_gpa_unmap(owner, data);
   1245 	case NVMM_IOC_HVA_MAP:
   1246 		return nvmm_hva_map(owner, data);
   1247 	case NVMM_IOC_HVA_UNMAP:
   1248 		return nvmm_hva_unmap(owner, data);
   1249 	case NVMM_IOC_CTL:
   1250 		return nvmm_ctl(owner, data);
   1251 	default:
   1252 		return EINVAL;
   1253 	}
   1254 }
   1255 
   1256 static int
   1257 nvmm_ioctl(struct file *fp, u_long cmd, void *data)
   1258 {
   1259 	int error;
   1260 
   1261 	error = nvmm_enter_sig();
   1262 	if (error)
   1263 		return error;
   1264 	error = nvmm_ioctl_internal(fp, cmd, data);
   1265 	nvmm_exit();
   1266 
   1267 	return error;
   1268 }
   1269 
   1270 /* -------------------------------------------------------------------------- */
   1271 
   1272 static int nvmm_match(device_t, cfdata_t, void *);
   1273 static void nvmm_attach(device_t, device_t, void *);
   1274 static int nvmm_detach(device_t, int);
   1275 static bool nvmm_suspend(device_t, const pmf_qual_t *);
   1276 static bool nvmm_resume(device_t, const pmf_qual_t *);
   1277 
   1278 extern struct cfdriver nvmm_cd;
   1279 
   1280 CFATTACH_DECL_NEW(nvmm, 0, nvmm_match, nvmm_attach, nvmm_detach, NULL);
   1281 
   1282 static struct cfdata nvmm_cfdata[] = {
   1283 	{
   1284 		.cf_name = "nvmm",
   1285 		.cf_atname = "nvmm",
   1286 		.cf_unit = 0,
   1287 		.cf_fstate = FSTATE_STAR,
   1288 		.cf_loc = NULL,
   1289 		.cf_flags = 0,
   1290 		.cf_pspec = NULL,
   1291 	},
   1292 	{ NULL, NULL, 0, FSTATE_NOTFOUND, NULL, 0, NULL }
   1293 };
   1294 
   1295 static int
   1296 nvmm_match(device_t self, cfdata_t cfdata, void *arg)
   1297 {
   1298 	return 1;
   1299 }
   1300 
   1301 static void
   1302 nvmm_attach(device_t parent, device_t self, void *aux)
   1303 {
   1304 	int error;
   1305 
   1306 	error = nvmm_init();
   1307 	if (error)
   1308 		panic("%s: impossible", __func__);
   1309 	aprint_normal_dev(self, "attached, using backend %s\n",
   1310 	    nvmm_impl->name);
   1311 	if (nvmm_impl->suspend != NULL && nvmm_impl->resume != NULL)
   1312 		pmf_device_register(self, nvmm_suspend, nvmm_resume);
   1313 }
   1314 
   1315 static int
   1316 nvmm_detach(device_t self, int flags)
   1317 {
   1318 	if (atomic_load_relaxed(&nmachines) > 0)
   1319 		return EBUSY;
   1320 	pmf_device_deregister(self);
   1321 	nvmm_fini();
   1322 	return 0;
   1323 }
   1324 
   1325 static void
   1326 nvmm_suspend_vcpu(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
   1327 {
   1328 
   1329 	mutex_enter(&vcpu->lock);
   1330 	if (vcpu->present && nvmm_impl->vcpu_suspend)
   1331 		(*nvmm_impl->vcpu_suspend)(mach, vcpu);
   1332 	mutex_exit(&vcpu->lock);
   1333 }
   1334 
   1335 static void
   1336 nvmm_resume_vcpu(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
   1337 {
   1338 
   1339 	mutex_enter(&vcpu->lock);
   1340 	if (vcpu->present && nvmm_impl->vcpu_resume)
   1341 		(*nvmm_impl->vcpu_resume)(mach, vcpu);
   1342 	mutex_exit(&vcpu->lock);
   1343 }
   1344 
   1345 static void
   1346 nvmm_suspend_machine(struct nvmm_machine *mach)
   1347 {
   1348 
   1349 	rw_enter(&mach->lock, RW_WRITER);
   1350 	if (mach->present) {
   1351 		if (nvmm_impl->vcpu_suspend) {
   1352 			size_t cpuid;
   1353 
   1354 			for (cpuid = 0; cpuid < NVMM_MAX_VCPUS; cpuid++)
   1355 				nvmm_suspend_vcpu(mach, &mach->cpus[cpuid]);
   1356 		}
   1357 		if (nvmm_impl->machine_suspend)
   1358 			(*nvmm_impl->machine_suspend)(mach);
   1359 	}
   1360 	rw_exit(&mach->lock);
   1361 }
   1362 
   1363 static void
   1364 nvmm_resume_machine(struct nvmm_machine *mach)
   1365 {
   1366 
   1367 	rw_enter(&mach->lock, RW_WRITER);
   1368 	if (mach->present) {
   1369 		if (nvmm_impl->vcpu_resume) {
   1370 			size_t cpuid;
   1371 
   1372 			for (cpuid = 0; cpuid < NVMM_MAX_VCPUS; cpuid++)
   1373 				nvmm_resume_vcpu(mach, &mach->cpus[cpuid]);
   1374 		}
   1375 		if (nvmm_impl->machine_resume)
   1376 			(*nvmm_impl->machine_resume)(mach);
   1377 	}
   1378 	rw_exit(&mach->lock);
   1379 }
   1380 
   1381 static bool
   1382 nvmm_suspend(device_t self, const pmf_qual_t *qual)
   1383 {
   1384 	size_t i;
   1385 
   1386 	/*
   1387 	 * Prevent new users (via ioctl) from starting.
   1388 	 */
   1389 	mutex_enter(&suspension.lock);
   1390 	KASSERT(!nvmm_suspending);
   1391 	atomic_store_relaxed(&nvmm_suspending, true);
   1392 	mutex_exit(&suspension.lock);
   1393 
   1394 	/*
   1395 	 * Interrupt any running VMs so they will break out of run
   1396 	 * loops or anything else and not start up again until we've
   1397 	 * resumed.
   1398 	 */
   1399 	if (nvmm_impl->suspend_interrupt)
   1400 		(*nvmm_impl->suspend_interrupt)();
   1401 
   1402 	/*
   1403 	 * Wait for any running VMs or other ioctls to finish running
   1404 	 * or handling any other ioctls.
   1405 	 */
   1406 	mutex_enter(&suspension.lock);
   1407 	while (suspension.users)
   1408 		cv_wait(&suspension.suspendcv, &suspension.lock);
   1409 	mutex_exit(&suspension.lock);
   1410 
   1411 	/*
   1412 	 * Suspend all the machines.
   1413 	 */
   1414 	if (nvmm_impl->machine_suspend || nvmm_impl->vcpu_suspend) {
   1415 		for (i = 0; i < NVMM_MAX_MACHINES; i++)
   1416 			nvmm_suspend_machine(&machines[i]);
   1417 	}
   1418 
   1419 	/*
   1420 	 * Take any systemwide suspend action.
   1421 	 */
   1422 	if (nvmm_impl->suspend)
   1423 		(*nvmm_impl->suspend)();
   1424 
   1425 	return true;
   1426 }
   1427 
   1428 static bool
   1429 nvmm_resume(device_t self, const pmf_qual_t *qual)
   1430 {
   1431 	size_t i;
   1432 
   1433 	KASSERT(atomic_load_relaxed(&nvmm_suspending));
   1434 	KASSERT(suspension.users == 0);
   1435 
   1436 	/*
   1437 	 * Take any systemwide resume action.
   1438 	 */
   1439 	if (nvmm_impl->resume)
   1440 		(*nvmm_impl->resume)();
   1441 
   1442 	/*
   1443 	 * Resume all the machines.
   1444 	 */
   1445 	if (nvmm_impl->machine_resume || nvmm_impl->vcpu_resume) {
   1446 		for (i = 0; i < NVMM_MAX_MACHINES; i++)
   1447 			nvmm_resume_machine(&machines[i]);
   1448 	}
   1449 
   1450 	/*
   1451 	 * Allow new users (via ioctl) to start again.
   1452 	 */
   1453 	mutex_enter(&suspension.lock);
   1454 	atomic_store_relaxed(&nvmm_suspending, false);
   1455 	cv_broadcast(&suspension.resumecv);
   1456 	mutex_exit(&suspension.lock);
   1457 
   1458 	return true;
   1459 }
   1460 
   1461 void
   1462 nvmmattach(int nunits)
   1463 {
   1464 	/* nothing */
   1465 }
   1466 
   1467 MODULE(MODULE_CLASS_MISC, nvmm, NULL);
   1468 
   1469 #if defined(_MODULE)
   1470 CFDRIVER_DECL(nvmm, DV_VIRTUAL, NULL);
   1471 #endif
   1472 
   1473 static int
   1474 nvmm_modcmd(modcmd_t cmd, void *arg)
   1475 {
   1476 #if defined(_MODULE)
   1477 	devmajor_t bmajor = NODEVMAJOR;
   1478 	devmajor_t cmajor = 345;
   1479 #endif
   1480 	int error;
   1481 
   1482 	switch (cmd) {
   1483 	case MODULE_CMD_INIT:
   1484 		if (nvmm_ident() == NULL) {
   1485 			aprint_error("%s: cpu not supported\n",
   1486 			    nvmm_cd.cd_name);
   1487 			return ENOTSUP;
   1488 		}
   1489 #if defined(_MODULE)
   1490 		error = config_cfdriver_attach(&nvmm_cd);
   1491 		if (error)
   1492 			return error;
   1493 #endif
   1494 		error = config_cfattach_attach(nvmm_cd.cd_name, &nvmm_ca);
   1495 		if (error) {
   1496 #if defined(_MODULE)
   1497 			config_cfdriver_detach(&nvmm_cd);
   1498 #endif
   1499 			aprint_error("%s: config_cfattach_attach failed\n",
   1500 			    nvmm_cd.cd_name);
   1501 			return error;
   1502 		}
   1503 
   1504 		error = config_cfdata_attach(nvmm_cfdata, 1);
   1505 		if (error) {
   1506 			config_cfattach_detach(nvmm_cd.cd_name, &nvmm_ca);
   1507 #if defined(_MODULE)
   1508 			config_cfdriver_detach(&nvmm_cd);
   1509 #endif
   1510 			aprint_error("%s: unable to register cfdata\n",
   1511 			    nvmm_cd.cd_name);
   1512 			return error;
   1513 		}
   1514 
   1515 		if (config_attach_pseudo(nvmm_cfdata) == NULL) {
   1516 			aprint_error("%s: config_attach_pseudo failed\n",
   1517 			    nvmm_cd.cd_name);
   1518 			config_cfattach_detach(nvmm_cd.cd_name, &nvmm_ca);
   1519 #if defined(_MODULE)
   1520 			config_cfdriver_detach(&nvmm_cd);
   1521 #endif
   1522 			return ENXIO;
   1523 		}
   1524 
   1525 #if defined(_MODULE)
   1526 		/* mknod /dev/nvmm c 345 0 */
   1527 		error = devsw_attach(nvmm_cd.cd_name, NULL, &bmajor,
   1528 			&nvmm_cdevsw, &cmajor);
   1529 		if (error) {
   1530 			aprint_error("%s: unable to register devsw, err %d\n",
   1531 			    nvmm_cd.cd_name, error);
   1532 			config_cfattach_detach(nvmm_cd.cd_name, &nvmm_ca);
   1533 			config_cfdriver_detach(&nvmm_cd);
   1534 			return error;
   1535 		}
   1536 #endif
   1537 		return 0;
   1538 	case MODULE_CMD_FINI:
   1539 		error = config_cfdata_detach(nvmm_cfdata);
   1540 		if (error)
   1541 			return error;
   1542 		error = config_cfattach_detach(nvmm_cd.cd_name, &nvmm_ca);
   1543 		if (error)
   1544 			return error;
   1545 #if defined(_MODULE)
   1546 		config_cfdriver_detach(&nvmm_cd);
   1547 		devsw_detach(NULL, &nvmm_cdevsw);
   1548 #endif
   1549 		return 0;
   1550 	case MODULE_CMD_AUTOUNLOAD:
   1551 		return EBUSY;
   1552 	default:
   1553 		return ENOTTY;
   1554 	}
   1555 }
   1556