Home | History | Annotate | Line # | Download | only in nvmm
nvmm.c revision 1.37
      1  1.37  maxv /*	$NetBSD: nvmm.c,v 1.37 2020/08/29 07:14:17 maxv Exp $	*/
      2   1.1  maxv 
      3   1.1  maxv /*
      4  1.32  maxv  * Copyright (c) 2018-2020 The NetBSD Foundation, Inc.
      5   1.1  maxv  * All rights reserved.
      6   1.1  maxv  *
      7   1.1  maxv  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  maxv  * by Maxime Villard.
      9   1.1  maxv  *
     10   1.1  maxv  * Redistribution and use in source and binary forms, with or without
     11   1.1  maxv  * modification, are permitted provided that the following conditions
     12   1.1  maxv  * are met:
     13   1.1  maxv  * 1. Redistributions of source code must retain the above copyright
     14   1.1  maxv  *    notice, this list of conditions and the following disclaimer.
     15   1.1  maxv  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  maxv  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  maxv  *    documentation and/or other materials provided with the distribution.
     18   1.1  maxv  *
     19   1.1  maxv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  maxv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  maxv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  maxv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  maxv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  maxv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  maxv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  maxv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  maxv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  maxv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  maxv  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  maxv  */
     31   1.1  maxv 
     32   1.1  maxv #include <sys/cdefs.h>
     33  1.37  maxv __KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.37 2020/08/29 07:14:17 maxv Exp $");
     34   1.1  maxv 
     35   1.1  maxv #include <sys/param.h>
     36   1.1  maxv #include <sys/systm.h>
     37   1.1  maxv #include <sys/kernel.h>
     38   1.1  maxv 
     39  1.37  maxv #include <sys/atomic.h>
     40   1.1  maxv #include <sys/cpu.h>
     41   1.1  maxv #include <sys/conf.h>
     42   1.1  maxv #include <sys/kmem.h>
     43   1.1  maxv #include <sys/module.h>
     44   1.1  maxv #include <sys/proc.h>
     45  1.11  maxv #include <sys/mman.h>
     46  1.14  maxv #include <sys/file.h>
     47  1.14  maxv #include <sys/filedesc.h>
     48  1.31  maxv #include <sys/device.h>
     49   1.1  maxv 
     50   1.1  maxv #include <uvm/uvm.h>
     51   1.1  maxv #include <uvm/uvm_page.h>
     52   1.1  maxv 
     53   1.1  maxv #include "ioconf.h"
     54   1.1  maxv 
     55   1.1  maxv #include <dev/nvmm/nvmm.h>
     56   1.1  maxv #include <dev/nvmm/nvmm_internal.h>
     57   1.1  maxv #include <dev/nvmm/nvmm_ioctl.h>
     58   1.1  maxv 
     59   1.1  maxv static struct nvmm_machine machines[NVMM_MAX_MACHINES];
     60  1.13  maxv static volatile unsigned int nmachines __cacheline_aligned;
     61   1.1  maxv 
     62   1.1  maxv static const struct nvmm_impl *nvmm_impl_list[] = {
     63  1.33  maxv #if defined(__x86_64__)
     64   1.7  maxv 	&nvmm_x86_svm,	/* x86 AMD SVM */
     65   1.7  maxv 	&nvmm_x86_vmx	/* x86 Intel VMX */
     66  1.33  maxv #endif
     67   1.1  maxv };
     68   1.1  maxv 
     69   1.1  maxv static const struct nvmm_impl *nvmm_impl = NULL;
     70   1.1  maxv 
     71  1.17  maxv static struct nvmm_owner root_owner;
     72  1.17  maxv 
     73   1.1  maxv /* -------------------------------------------------------------------------- */
     74   1.1  maxv 
     75   1.1  maxv static int
     76   1.1  maxv nvmm_machine_alloc(struct nvmm_machine **ret)
     77   1.1  maxv {
     78   1.1  maxv 	struct nvmm_machine *mach;
     79   1.1  maxv 	size_t i;
     80   1.1  maxv 
     81   1.1  maxv 	for (i = 0; i < NVMM_MAX_MACHINES; i++) {
     82   1.1  maxv 		mach = &machines[i];
     83   1.1  maxv 
     84   1.1  maxv 		rw_enter(&mach->lock, RW_WRITER);
     85   1.1  maxv 		if (mach->present) {
     86   1.1  maxv 			rw_exit(&mach->lock);
     87   1.1  maxv 			continue;
     88   1.1  maxv 		}
     89   1.1  maxv 
     90   1.1  maxv 		mach->present = true;
     91  1.17  maxv 		mach->time = time_second;
     92   1.1  maxv 		*ret = mach;
     93  1.13  maxv 		atomic_inc_uint(&nmachines);
     94   1.1  maxv 		return 0;
     95   1.1  maxv 	}
     96   1.1  maxv 
     97   1.1  maxv 	return ENOBUFS;
     98   1.1  maxv }
     99   1.1  maxv 
    100   1.1  maxv static void
    101   1.1  maxv nvmm_machine_free(struct nvmm_machine *mach)
    102   1.1  maxv {
    103   1.1  maxv 	KASSERT(rw_write_held(&mach->lock));
    104   1.1  maxv 	KASSERT(mach->present);
    105   1.1  maxv 	mach->present = false;
    106  1.13  maxv 	atomic_dec_uint(&nmachines);
    107   1.1  maxv }
    108   1.1  maxv 
    109   1.1  maxv static int
    110  1.14  maxv nvmm_machine_get(struct nvmm_owner *owner, nvmm_machid_t machid,
    111  1.14  maxv     struct nvmm_machine **ret, bool writer)
    112   1.1  maxv {
    113   1.1  maxv 	struct nvmm_machine *mach;
    114   1.1  maxv 	krw_t op = writer ? RW_WRITER : RW_READER;
    115   1.1  maxv 
    116  1.36  maxv 	if (__predict_false(machid >= NVMM_MAX_MACHINES)) {
    117   1.1  maxv 		return EINVAL;
    118   1.1  maxv 	}
    119   1.1  maxv 	mach = &machines[machid];
    120   1.1  maxv 
    121   1.1  maxv 	rw_enter(&mach->lock, op);
    122  1.36  maxv 	if (__predict_false(!mach->present)) {
    123   1.1  maxv 		rw_exit(&mach->lock);
    124   1.1  maxv 		return ENOENT;
    125   1.1  maxv 	}
    126  1.36  maxv 	if (__predict_false(mach->owner != owner && owner != &root_owner)) {
    127   1.1  maxv 		rw_exit(&mach->lock);
    128   1.1  maxv 		return EPERM;
    129   1.1  maxv 	}
    130   1.1  maxv 	*ret = mach;
    131   1.1  maxv 
    132   1.1  maxv 	return 0;
    133   1.1  maxv }
    134   1.1  maxv 
    135   1.1  maxv static void
    136   1.1  maxv nvmm_machine_put(struct nvmm_machine *mach)
    137   1.1  maxv {
    138   1.1  maxv 	rw_exit(&mach->lock);
    139   1.1  maxv }
    140   1.1  maxv 
    141   1.1  maxv /* -------------------------------------------------------------------------- */
    142   1.1  maxv 
    143   1.1  maxv static int
    144  1.18  maxv nvmm_vcpu_alloc(struct nvmm_machine *mach, nvmm_cpuid_t cpuid,
    145  1.18  maxv     struct nvmm_cpu **ret)
    146   1.1  maxv {
    147   1.1  maxv 	struct nvmm_cpu *vcpu;
    148   1.1  maxv 
    149  1.18  maxv 	if (cpuid >= NVMM_MAX_VCPUS) {
    150  1.18  maxv 		return EINVAL;
    151  1.18  maxv 	}
    152  1.18  maxv 	vcpu = &mach->cpus[cpuid];
    153   1.1  maxv 
    154  1.18  maxv 	mutex_enter(&vcpu->lock);
    155  1.18  maxv 	if (vcpu->present) {
    156  1.18  maxv 		mutex_exit(&vcpu->lock);
    157  1.18  maxv 		return EBUSY;
    158   1.1  maxv 	}
    159   1.1  maxv 
    160  1.18  maxv 	vcpu->present = true;
    161  1.19  maxv 	vcpu->comm = NULL;
    162  1.18  maxv 	vcpu->hcpu_last = -1;
    163  1.18  maxv 	*ret = vcpu;
    164  1.18  maxv 	return 0;
    165   1.1  maxv }
    166   1.1  maxv 
    167   1.1  maxv static void
    168   1.1  maxv nvmm_vcpu_free(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
    169   1.1  maxv {
    170   1.1  maxv 	KASSERT(mutex_owned(&vcpu->lock));
    171   1.1  maxv 	vcpu->present = false;
    172  1.19  maxv 	if (vcpu->comm != NULL) {
    173  1.19  maxv 		uvm_deallocate(kernel_map, (vaddr_t)vcpu->comm, PAGE_SIZE);
    174  1.19  maxv 	}
    175   1.1  maxv }
    176   1.1  maxv 
    177  1.22  maxv static int
    178   1.1  maxv nvmm_vcpu_get(struct nvmm_machine *mach, nvmm_cpuid_t cpuid,
    179   1.1  maxv     struct nvmm_cpu **ret)
    180   1.1  maxv {
    181   1.1  maxv 	struct nvmm_cpu *vcpu;
    182   1.1  maxv 
    183  1.36  maxv 	if (__predict_false(cpuid >= NVMM_MAX_VCPUS)) {
    184   1.1  maxv 		return EINVAL;
    185   1.1  maxv 	}
    186   1.1  maxv 	vcpu = &mach->cpus[cpuid];
    187   1.1  maxv 
    188   1.1  maxv 	mutex_enter(&vcpu->lock);
    189  1.36  maxv 	if (__predict_false(!vcpu->present)) {
    190   1.1  maxv 		mutex_exit(&vcpu->lock);
    191   1.1  maxv 		return ENOENT;
    192   1.1  maxv 	}
    193   1.1  maxv 	*ret = vcpu;
    194   1.1  maxv 
    195   1.1  maxv 	return 0;
    196   1.1  maxv }
    197   1.1  maxv 
    198  1.22  maxv static void
    199   1.1  maxv nvmm_vcpu_put(struct nvmm_cpu *vcpu)
    200   1.1  maxv {
    201   1.1  maxv 	mutex_exit(&vcpu->lock);
    202   1.1  maxv }
    203   1.1  maxv 
    204   1.1  maxv /* -------------------------------------------------------------------------- */
    205   1.1  maxv 
    206   1.1  maxv static void
    207  1.14  maxv nvmm_kill_machines(struct nvmm_owner *owner)
    208   1.1  maxv {
    209   1.1  maxv 	struct nvmm_machine *mach;
    210   1.1  maxv 	struct nvmm_cpu *vcpu;
    211   1.1  maxv 	size_t i, j;
    212   1.1  maxv 	int error;
    213   1.1  maxv 
    214   1.1  maxv 	for (i = 0; i < NVMM_MAX_MACHINES; i++) {
    215   1.1  maxv 		mach = &machines[i];
    216   1.1  maxv 
    217   1.1  maxv 		rw_enter(&mach->lock, RW_WRITER);
    218  1.14  maxv 		if (!mach->present || mach->owner != owner) {
    219   1.1  maxv 			rw_exit(&mach->lock);
    220   1.1  maxv 			continue;
    221   1.1  maxv 		}
    222   1.1  maxv 
    223   1.1  maxv 		/* Kill it. */
    224   1.1  maxv 		for (j = 0; j < NVMM_MAX_VCPUS; j++) {
    225   1.1  maxv 			error = nvmm_vcpu_get(mach, j, &vcpu);
    226   1.1  maxv 			if (error)
    227   1.1  maxv 				continue;
    228   1.1  maxv 			(*nvmm_impl->vcpu_destroy)(mach, vcpu);
    229   1.1  maxv 			nvmm_vcpu_free(mach, vcpu);
    230   1.1  maxv 			nvmm_vcpu_put(vcpu);
    231  1.36  maxv 			atomic_dec_uint(&mach->ncpus);
    232   1.1  maxv 		}
    233  1.15  maxv 		(*nvmm_impl->machine_destroy)(mach);
    234   1.1  maxv 		uvmspace_free(mach->vm);
    235   1.4  maxv 
    236   1.4  maxv 		/* Drop the kernel UOBJ refs. */
    237   1.9  maxv 		for (j = 0; j < NVMM_MAX_HMAPPINGS; j++) {
    238   1.9  maxv 			if (!mach->hmap[j].present)
    239   1.4  maxv 				continue;
    240   1.9  maxv 			uao_detach(mach->hmap[j].uobj);
    241   1.4  maxv 		}
    242   1.4  maxv 
    243   1.1  maxv 		nvmm_machine_free(mach);
    244   1.1  maxv 
    245   1.1  maxv 		rw_exit(&mach->lock);
    246   1.1  maxv 	}
    247   1.1  maxv }
    248   1.1  maxv 
    249   1.1  maxv /* -------------------------------------------------------------------------- */
    250   1.1  maxv 
    251   1.1  maxv static int
    252  1.14  maxv nvmm_capability(struct nvmm_owner *owner, struct nvmm_ioc_capability *args)
    253   1.1  maxv {
    254  1.23  maxv 	args->cap.version = NVMM_KERN_VERSION;
    255   1.1  maxv 	args->cap.state_size = nvmm_impl->state_size;
    256   1.1  maxv 	args->cap.max_machines = NVMM_MAX_MACHINES;
    257   1.1  maxv 	args->cap.max_vcpus = NVMM_MAX_VCPUS;
    258   1.1  maxv 	args->cap.max_ram = NVMM_MAX_RAM;
    259   1.1  maxv 
    260   1.1  maxv 	(*nvmm_impl->capability)(&args->cap);
    261   1.1  maxv 
    262   1.1  maxv 	return 0;
    263   1.1  maxv }
    264   1.1  maxv 
    265   1.1  maxv static int
    266  1.14  maxv nvmm_machine_create(struct nvmm_owner *owner,
    267  1.14  maxv     struct nvmm_ioc_machine_create *args)
    268   1.1  maxv {
    269   1.1  maxv 	struct nvmm_machine *mach;
    270   1.1  maxv 	int error;
    271   1.1  maxv 
    272   1.1  maxv 	error = nvmm_machine_alloc(&mach);
    273   1.1  maxv 	if (error)
    274   1.1  maxv 		return error;
    275   1.1  maxv 
    276   1.1  maxv 	/* Curproc owns the machine. */
    277  1.14  maxv 	mach->owner = owner;
    278   1.1  maxv 
    279   1.9  maxv 	/* Zero out the host mappings. */
    280   1.9  maxv 	memset(&mach->hmap, 0, sizeof(mach->hmap));
    281   1.4  maxv 
    282   1.1  maxv 	/* Create the machine vmspace. */
    283   1.1  maxv 	mach->gpa_begin = 0;
    284   1.1  maxv 	mach->gpa_end = NVMM_MAX_RAM;
    285   1.1  maxv 	mach->vm = uvmspace_alloc(0, mach->gpa_end - mach->gpa_begin, false);
    286   1.1  maxv 
    287  1.19  maxv 	/* Create the comm uobj. */
    288  1.19  maxv 	mach->commuobj = uao_create(NVMM_MAX_VCPUS * PAGE_SIZE, 0);
    289  1.19  maxv 
    290   1.1  maxv 	(*nvmm_impl->machine_create)(mach);
    291   1.1  maxv 
    292   1.1  maxv 	args->machid = mach->machid;
    293   1.1  maxv 	nvmm_machine_put(mach);
    294   1.1  maxv 
    295   1.1  maxv 	return 0;
    296   1.1  maxv }
    297   1.1  maxv 
    298   1.1  maxv static int
    299  1.14  maxv nvmm_machine_destroy(struct nvmm_owner *owner,
    300  1.14  maxv     struct nvmm_ioc_machine_destroy *args)
    301   1.1  maxv {
    302   1.1  maxv 	struct nvmm_machine *mach;
    303   1.1  maxv 	struct nvmm_cpu *vcpu;
    304   1.1  maxv 	int error;
    305   1.1  maxv 	size_t i;
    306   1.1  maxv 
    307  1.14  maxv 	error = nvmm_machine_get(owner, args->machid, &mach, true);
    308   1.1  maxv 	if (error)
    309   1.1  maxv 		return error;
    310   1.1  maxv 
    311   1.1  maxv 	for (i = 0; i < NVMM_MAX_VCPUS; i++) {
    312   1.1  maxv 		error = nvmm_vcpu_get(mach, i, &vcpu);
    313   1.1  maxv 		if (error)
    314   1.1  maxv 			continue;
    315   1.1  maxv 
    316   1.1  maxv 		(*nvmm_impl->vcpu_destroy)(mach, vcpu);
    317   1.1  maxv 		nvmm_vcpu_free(mach, vcpu);
    318   1.1  maxv 		nvmm_vcpu_put(vcpu);
    319  1.36  maxv 		atomic_dec_uint(&mach->ncpus);
    320   1.1  maxv 	}
    321   1.1  maxv 
    322   1.1  maxv 	(*nvmm_impl->machine_destroy)(mach);
    323   1.1  maxv 
    324   1.1  maxv 	/* Free the machine vmspace. */
    325   1.1  maxv 	uvmspace_free(mach->vm);
    326   1.4  maxv 
    327   1.4  maxv 	/* Drop the kernel UOBJ refs. */
    328   1.9  maxv 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
    329   1.9  maxv 		if (!mach->hmap[i].present)
    330   1.4  maxv 			continue;
    331   1.9  maxv 		uao_detach(mach->hmap[i].uobj);
    332   1.4  maxv 	}
    333   1.1  maxv 
    334   1.1  maxv 	nvmm_machine_free(mach);
    335   1.1  maxv 	nvmm_machine_put(mach);
    336   1.1  maxv 
    337   1.1  maxv 	return 0;
    338   1.1  maxv }
    339   1.1  maxv 
    340   1.1  maxv static int
    341  1.14  maxv nvmm_machine_configure(struct nvmm_owner *owner,
    342  1.14  maxv     struct nvmm_ioc_machine_configure *args)
    343   1.1  maxv {
    344   1.1  maxv 	struct nvmm_machine *mach;
    345   1.1  maxv 	size_t allocsz;
    346  1.21  maxv 	uint64_t op;
    347   1.1  maxv 	void *data;
    348   1.1  maxv 	int error;
    349   1.1  maxv 
    350  1.21  maxv 	op = NVMM_MACH_CONF_MD(args->op);
    351  1.23  maxv 	if (__predict_false(op >= nvmm_impl->mach_conf_max)) {
    352   1.1  maxv 		return EINVAL;
    353   1.1  maxv 	}
    354   1.1  maxv 
    355  1.23  maxv 	allocsz = nvmm_impl->mach_conf_sizes[op];
    356   1.1  maxv 	data = kmem_alloc(allocsz, KM_SLEEP);
    357   1.1  maxv 
    358  1.14  maxv 	error = nvmm_machine_get(owner, args->machid, &mach, true);
    359   1.1  maxv 	if (error) {
    360   1.1  maxv 		kmem_free(data, allocsz);
    361   1.1  maxv 		return error;
    362   1.1  maxv 	}
    363   1.1  maxv 
    364   1.1  maxv 	error = copyin(args->conf, data, allocsz);
    365   1.1  maxv 	if (error) {
    366   1.1  maxv 		goto out;
    367   1.1  maxv 	}
    368   1.1  maxv 
    369  1.21  maxv 	error = (*nvmm_impl->machine_configure)(mach, op, data);
    370   1.1  maxv 
    371   1.1  maxv out:
    372   1.1  maxv 	nvmm_machine_put(mach);
    373   1.1  maxv 	kmem_free(data, allocsz);
    374   1.1  maxv 	return error;
    375   1.1  maxv }
    376   1.1  maxv 
    377   1.1  maxv static int
    378  1.14  maxv nvmm_vcpu_create(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_create *args)
    379   1.1  maxv {
    380   1.1  maxv 	struct nvmm_machine *mach;
    381   1.1  maxv 	struct nvmm_cpu *vcpu;
    382   1.1  maxv 	int error;
    383   1.1  maxv 
    384  1.14  maxv 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    385   1.1  maxv 	if (error)
    386   1.1  maxv 		return error;
    387   1.1  maxv 
    388  1.18  maxv 	error = nvmm_vcpu_alloc(mach, args->cpuid, &vcpu);
    389   1.1  maxv 	if (error)
    390   1.1  maxv 		goto out;
    391   1.1  maxv 
    392  1.19  maxv 	/* Allocate the comm page. */
    393  1.19  maxv 	uao_reference(mach->commuobj);
    394  1.19  maxv 	error = uvm_map(kernel_map, (vaddr_t *)&vcpu->comm, PAGE_SIZE,
    395  1.19  maxv 	    mach->commuobj, args->cpuid * PAGE_SIZE, 0, UVM_MAPFLAG(UVM_PROT_RW,
    396  1.19  maxv 	    UVM_PROT_RW, UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
    397  1.19  maxv 	if (error) {
    398  1.19  maxv 		uao_detach(mach->commuobj);
    399  1.19  maxv 		nvmm_vcpu_free(mach, vcpu);
    400  1.19  maxv 		nvmm_vcpu_put(vcpu);
    401  1.19  maxv 		goto out;
    402  1.19  maxv 	}
    403  1.19  maxv 	error = uvm_map_pageable(kernel_map, (vaddr_t)vcpu->comm,
    404  1.19  maxv 	    (vaddr_t)vcpu->comm + PAGE_SIZE, false, 0);
    405  1.19  maxv 	if (error) {
    406  1.19  maxv 		nvmm_vcpu_free(mach, vcpu);
    407  1.19  maxv 		nvmm_vcpu_put(vcpu);
    408  1.19  maxv 		goto out;
    409  1.19  maxv 	}
    410  1.19  maxv 	memset(vcpu->comm, 0, PAGE_SIZE);
    411  1.19  maxv 
    412   1.1  maxv 	error = (*nvmm_impl->vcpu_create)(mach, vcpu);
    413   1.1  maxv 	if (error) {
    414   1.1  maxv 		nvmm_vcpu_free(mach, vcpu);
    415   1.1  maxv 		nvmm_vcpu_put(vcpu);
    416   1.1  maxv 		goto out;
    417   1.1  maxv 	}
    418   1.1  maxv 
    419   1.1  maxv 	nvmm_vcpu_put(vcpu);
    420  1.28  maxv 	atomic_inc_uint(&mach->ncpus);
    421  1.28  maxv 
    422   1.1  maxv out:
    423   1.1  maxv 	nvmm_machine_put(mach);
    424   1.1  maxv 	return error;
    425   1.1  maxv }
    426   1.1  maxv 
    427   1.1  maxv static int
    428  1.14  maxv nvmm_vcpu_destroy(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_destroy *args)
    429   1.1  maxv {
    430   1.1  maxv 	struct nvmm_machine *mach;
    431   1.1  maxv 	struct nvmm_cpu *vcpu;
    432   1.1  maxv 	int error;
    433   1.1  maxv 
    434  1.14  maxv 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    435   1.1  maxv 	if (error)
    436   1.1  maxv 		return error;
    437   1.1  maxv 
    438   1.1  maxv 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    439   1.1  maxv 	if (error)
    440   1.1  maxv 		goto out;
    441   1.1  maxv 
    442   1.1  maxv 	(*nvmm_impl->vcpu_destroy)(mach, vcpu);
    443   1.1  maxv 	nvmm_vcpu_free(mach, vcpu);
    444   1.1  maxv 	nvmm_vcpu_put(vcpu);
    445  1.28  maxv 	atomic_dec_uint(&mach->ncpus);
    446  1.28  maxv 
    447   1.1  maxv out:
    448   1.1  maxv 	nvmm_machine_put(mach);
    449   1.1  maxv 	return error;
    450   1.1  maxv }
    451   1.1  maxv 
    452   1.1  maxv static int
    453  1.23  maxv nvmm_vcpu_configure(struct nvmm_owner *owner,
    454  1.23  maxv     struct nvmm_ioc_vcpu_configure *args)
    455  1.23  maxv {
    456  1.23  maxv 	struct nvmm_machine *mach;
    457  1.23  maxv 	struct nvmm_cpu *vcpu;
    458  1.23  maxv 	size_t allocsz;
    459  1.23  maxv 	uint64_t op;
    460  1.23  maxv 	void *data;
    461  1.23  maxv 	int error;
    462  1.23  maxv 
    463  1.23  maxv 	op = NVMM_VCPU_CONF_MD(args->op);
    464  1.23  maxv 	if (__predict_false(op >= nvmm_impl->vcpu_conf_max))
    465  1.23  maxv 		return EINVAL;
    466  1.23  maxv 
    467  1.23  maxv 	allocsz = nvmm_impl->vcpu_conf_sizes[op];
    468  1.23  maxv 	data = kmem_alloc(allocsz, KM_SLEEP);
    469  1.23  maxv 
    470  1.23  maxv 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    471  1.23  maxv 	if (error) {
    472  1.23  maxv 		kmem_free(data, allocsz);
    473  1.23  maxv 		return error;
    474  1.23  maxv 	}
    475  1.23  maxv 
    476  1.23  maxv 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    477  1.23  maxv 	if (error) {
    478  1.23  maxv 		nvmm_machine_put(mach);
    479  1.23  maxv 		kmem_free(data, allocsz);
    480  1.23  maxv 		return error;
    481  1.23  maxv 	}
    482  1.23  maxv 
    483  1.23  maxv 	error = copyin(args->conf, data, allocsz);
    484  1.23  maxv 	if (error) {
    485  1.23  maxv 		goto out;
    486  1.23  maxv 	}
    487  1.23  maxv 
    488  1.23  maxv 	error = (*nvmm_impl->vcpu_configure)(vcpu, op, data);
    489  1.23  maxv 
    490  1.23  maxv out:
    491  1.23  maxv 	nvmm_vcpu_put(vcpu);
    492  1.23  maxv 	nvmm_machine_put(mach);
    493  1.23  maxv 	kmem_free(data, allocsz);
    494  1.23  maxv 	return error;
    495  1.23  maxv }
    496  1.23  maxv 
    497  1.23  maxv static int
    498  1.14  maxv nvmm_vcpu_setstate(struct nvmm_owner *owner,
    499  1.14  maxv     struct nvmm_ioc_vcpu_setstate *args)
    500   1.1  maxv {
    501   1.1  maxv 	struct nvmm_machine *mach;
    502   1.1  maxv 	struct nvmm_cpu *vcpu;
    503   1.1  maxv 	int error;
    504   1.1  maxv 
    505  1.14  maxv 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    506   1.6  maxv 	if (error)
    507   1.1  maxv 		return error;
    508   1.1  maxv 
    509   1.1  maxv 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    510   1.1  maxv 	if (error)
    511   1.1  maxv 		goto out;
    512   1.1  maxv 
    513  1.19  maxv 	(*nvmm_impl->vcpu_setstate)(vcpu);
    514   1.1  maxv 	nvmm_vcpu_put(vcpu);
    515   1.1  maxv 
    516   1.1  maxv out:
    517   1.1  maxv 	nvmm_machine_put(mach);
    518   1.1  maxv 	return error;
    519   1.1  maxv }
    520   1.1  maxv 
    521   1.1  maxv static int
    522  1.14  maxv nvmm_vcpu_getstate(struct nvmm_owner *owner,
    523  1.14  maxv     struct nvmm_ioc_vcpu_getstate *args)
    524   1.1  maxv {
    525   1.1  maxv 	struct nvmm_machine *mach;
    526   1.1  maxv 	struct nvmm_cpu *vcpu;
    527   1.1  maxv 	int error;
    528   1.1  maxv 
    529  1.14  maxv 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    530   1.6  maxv 	if (error)
    531   1.1  maxv 		return error;
    532   1.1  maxv 
    533   1.1  maxv 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    534   1.1  maxv 	if (error)
    535   1.1  maxv 		goto out;
    536   1.1  maxv 
    537  1.19  maxv 	(*nvmm_impl->vcpu_getstate)(vcpu);
    538   1.1  maxv 	nvmm_vcpu_put(vcpu);
    539   1.1  maxv 
    540   1.1  maxv out:
    541   1.1  maxv 	nvmm_machine_put(mach);
    542   1.1  maxv 	return error;
    543   1.1  maxv }
    544   1.1  maxv 
    545   1.1  maxv static int
    546  1.14  maxv nvmm_vcpu_inject(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_inject *args)
    547   1.1  maxv {
    548   1.1  maxv 	struct nvmm_machine *mach;
    549   1.1  maxv 	struct nvmm_cpu *vcpu;
    550   1.1  maxv 	int error;
    551   1.1  maxv 
    552  1.14  maxv 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    553   1.1  maxv 	if (error)
    554   1.1  maxv 		return error;
    555   1.1  maxv 
    556   1.1  maxv 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    557   1.1  maxv 	if (error)
    558   1.1  maxv 		goto out;
    559   1.1  maxv 
    560  1.20  maxv 	error = (*nvmm_impl->vcpu_inject)(vcpu);
    561   1.1  maxv 	nvmm_vcpu_put(vcpu);
    562   1.1  maxv 
    563   1.1  maxv out:
    564   1.1  maxv 	nvmm_machine_put(mach);
    565   1.1  maxv 	return error;
    566   1.1  maxv }
    567   1.1  maxv 
    568  1.22  maxv static int
    569   1.8  maxv nvmm_do_vcpu_run(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
    570  1.23  maxv     struct nvmm_vcpu_exit *exit)
    571   1.8  maxv {
    572   1.8  maxv 	struct vmspace *vm = mach->vm;
    573  1.22  maxv 	int ret;
    574   1.8  maxv 
    575   1.8  maxv 	while (1) {
    576  1.30  maxv 		/* Got a signal? Or pending resched? Leave. */
    577  1.30  maxv 		if (__predict_false(nvmm_return_needed())) {
    578  1.30  maxv 			exit->reason = NVMM_VCPU_EXIT_NONE;
    579  1.30  maxv 			return 0;
    580  1.30  maxv 		}
    581  1.30  maxv 
    582  1.30  maxv 		/* Run the VCPU. */
    583  1.22  maxv 		ret = (*nvmm_impl->vcpu_run)(mach, vcpu, exit);
    584  1.22  maxv 		if (__predict_false(ret != 0)) {
    585  1.22  maxv 			return ret;
    586  1.22  maxv 		}
    587   1.8  maxv 
    588  1.30  maxv 		/* Process nested page faults. */
    589  1.23  maxv 		if (__predict_true(exit->reason != NVMM_VCPU_EXIT_MEMORY)) {
    590   1.8  maxv 			break;
    591   1.8  maxv 		}
    592  1.10  maxv 		if (exit->u.mem.gpa >= mach->gpa_end) {
    593  1.10  maxv 			break;
    594  1.10  maxv 		}
    595  1.11  maxv 		if (uvm_fault(&vm->vm_map, exit->u.mem.gpa, exit->u.mem.prot)) {
    596   1.8  maxv 			break;
    597   1.8  maxv 		}
    598   1.8  maxv 	}
    599  1.22  maxv 
    600  1.22  maxv 	return 0;
    601   1.8  maxv }
    602   1.8  maxv 
    603   1.1  maxv static int
    604  1.14  maxv nvmm_vcpu_run(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_run *args)
    605   1.1  maxv {
    606   1.1  maxv 	struct nvmm_machine *mach;
    607   1.1  maxv 	struct nvmm_cpu *vcpu;
    608   1.1  maxv 	int error;
    609   1.1  maxv 
    610  1.14  maxv 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    611   1.1  maxv 	if (error)
    612   1.1  maxv 		return error;
    613   1.1  maxv 
    614   1.1  maxv 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
    615   1.1  maxv 	if (error)
    616   1.1  maxv 		goto out;
    617   1.1  maxv 
    618  1.22  maxv 	error = nvmm_do_vcpu_run(mach, vcpu, &args->exit);
    619   1.1  maxv 	nvmm_vcpu_put(vcpu);
    620   1.1  maxv 
    621   1.1  maxv out:
    622   1.1  maxv 	nvmm_machine_put(mach);
    623   1.1  maxv 	return error;
    624   1.1  maxv }
    625   1.1  maxv 
    626   1.1  maxv /* -------------------------------------------------------------------------- */
    627   1.1  maxv 
    628   1.4  maxv static struct uvm_object *
    629   1.9  maxv nvmm_hmapping_getuobj(struct nvmm_machine *mach, uintptr_t hva, size_t size,
    630   1.4  maxv    size_t *off)
    631   1.4  maxv {
    632   1.9  maxv 	struct nvmm_hmapping *hmapping;
    633   1.4  maxv 	size_t i;
    634   1.4  maxv 
    635   1.9  maxv 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
    636   1.9  maxv 		hmapping = &mach->hmap[i];
    637   1.9  maxv 		if (!hmapping->present) {
    638   1.4  maxv 			continue;
    639   1.4  maxv 		}
    640   1.9  maxv 		if (hva >= hmapping->hva &&
    641   1.9  maxv 		    hva + size <= hmapping->hva + hmapping->size) {
    642   1.9  maxv 			*off = hva - hmapping->hva;
    643   1.9  maxv 			return hmapping->uobj;
    644   1.4  maxv 		}
    645   1.4  maxv 	}
    646   1.4  maxv 
    647   1.4  maxv 	return NULL;
    648   1.4  maxv }
    649   1.4  maxv 
    650   1.4  maxv static int
    651   1.9  maxv nvmm_hmapping_validate(struct nvmm_machine *mach, uintptr_t hva, size_t size)
    652   1.4  maxv {
    653   1.9  maxv 	struct nvmm_hmapping *hmapping;
    654   1.4  maxv 	size_t i;
    655   1.4  maxv 
    656   1.4  maxv 	if ((hva % PAGE_SIZE) != 0 || (size % PAGE_SIZE) != 0) {
    657   1.4  maxv 		return EINVAL;
    658   1.4  maxv 	}
    659   1.4  maxv 	if (hva == 0) {
    660   1.4  maxv 		return EINVAL;
    661   1.4  maxv 	}
    662   1.4  maxv 
    663   1.9  maxv 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
    664   1.9  maxv 		hmapping = &mach->hmap[i];
    665   1.9  maxv 		if (!hmapping->present) {
    666   1.4  maxv 			continue;
    667   1.4  maxv 		}
    668   1.4  maxv 
    669   1.9  maxv 		if (hva >= hmapping->hva &&
    670   1.9  maxv 		    hva + size <= hmapping->hva + hmapping->size) {
    671   1.4  maxv 			break;
    672   1.4  maxv 		}
    673   1.4  maxv 
    674   1.9  maxv 		if (hva >= hmapping->hva &&
    675   1.9  maxv 		    hva < hmapping->hva + hmapping->size) {
    676   1.4  maxv 			return EEXIST;
    677   1.4  maxv 		}
    678   1.9  maxv 		if (hva + size > hmapping->hva &&
    679   1.9  maxv 		    hva + size <= hmapping->hva + hmapping->size) {
    680   1.4  maxv 			return EEXIST;
    681   1.4  maxv 		}
    682   1.9  maxv 		if (hva <= hmapping->hva &&
    683   1.9  maxv 		    hva + size >= hmapping->hva + hmapping->size) {
    684   1.4  maxv 			return EEXIST;
    685   1.4  maxv 		}
    686   1.4  maxv 	}
    687   1.4  maxv 
    688   1.4  maxv 	return 0;
    689   1.4  maxv }
    690   1.4  maxv 
    691   1.9  maxv static struct nvmm_hmapping *
    692   1.9  maxv nvmm_hmapping_alloc(struct nvmm_machine *mach)
    693   1.4  maxv {
    694   1.9  maxv 	struct nvmm_hmapping *hmapping;
    695   1.4  maxv 	size_t i;
    696   1.4  maxv 
    697   1.9  maxv 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
    698   1.9  maxv 		hmapping = &mach->hmap[i];
    699   1.9  maxv 		if (!hmapping->present) {
    700   1.9  maxv 			hmapping->present = true;
    701   1.9  maxv 			return hmapping;
    702   1.4  maxv 		}
    703   1.4  maxv 	}
    704   1.4  maxv 
    705   1.4  maxv 	return NULL;
    706   1.4  maxv }
    707   1.4  maxv 
    708   1.9  maxv static int
    709   1.9  maxv nvmm_hmapping_free(struct nvmm_machine *mach, uintptr_t hva, size_t size)
    710   1.4  maxv {
    711   1.4  maxv 	struct vmspace *vmspace = curproc->p_vmspace;
    712   1.9  maxv 	struct nvmm_hmapping *hmapping;
    713   1.9  maxv 	size_t i;
    714   1.4  maxv 
    715   1.9  maxv 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
    716   1.9  maxv 		hmapping = &mach->hmap[i];
    717   1.9  maxv 		if (!hmapping->present || hmapping->hva != hva ||
    718   1.9  maxv 		    hmapping->size != size) {
    719   1.9  maxv 			continue;
    720   1.9  maxv 		}
    721   1.9  maxv 
    722   1.9  maxv 		uvm_unmap(&vmspace->vm_map, hmapping->hva,
    723   1.9  maxv 		    hmapping->hva + hmapping->size);
    724   1.9  maxv 		uao_detach(hmapping->uobj);
    725   1.4  maxv 
    726   1.9  maxv 		hmapping->uobj = NULL;
    727   1.9  maxv 		hmapping->present = false;
    728   1.9  maxv 
    729   1.9  maxv 		return 0;
    730   1.9  maxv 	}
    731   1.9  maxv 
    732   1.9  maxv 	return ENOENT;
    733   1.4  maxv }
    734   1.4  maxv 
    735   1.4  maxv static int
    736  1.14  maxv nvmm_hva_map(struct nvmm_owner *owner, struct nvmm_ioc_hva_map *args)
    737   1.4  maxv {
    738   1.4  maxv 	struct vmspace *vmspace = curproc->p_vmspace;
    739   1.4  maxv 	struct nvmm_machine *mach;
    740   1.9  maxv 	struct nvmm_hmapping *hmapping;
    741   1.4  maxv 	vaddr_t uva;
    742   1.4  maxv 	int error;
    743   1.4  maxv 
    744  1.14  maxv 	error = nvmm_machine_get(owner, args->machid, &mach, true);
    745   1.4  maxv 	if (error)
    746   1.4  maxv 		return error;
    747   1.4  maxv 
    748   1.9  maxv 	error = nvmm_hmapping_validate(mach, args->hva, args->size);
    749   1.4  maxv 	if (error)
    750   1.4  maxv 		goto out;
    751   1.4  maxv 
    752   1.9  maxv 	hmapping = nvmm_hmapping_alloc(mach);
    753   1.9  maxv 	if (hmapping == NULL) {
    754   1.4  maxv 		error = ENOBUFS;
    755   1.4  maxv 		goto out;
    756   1.4  maxv 	}
    757   1.4  maxv 
    758   1.9  maxv 	hmapping->hva = args->hva;
    759   1.9  maxv 	hmapping->size = args->size;
    760   1.9  maxv 	hmapping->uobj = uao_create(hmapping->size, 0);
    761   1.9  maxv 	uva = hmapping->hva;
    762   1.4  maxv 
    763   1.4  maxv 	/* Take a reference for the user. */
    764   1.9  maxv 	uao_reference(hmapping->uobj);
    765   1.4  maxv 
    766   1.4  maxv 	/* Map the uobj into the user address space, as pageable. */
    767   1.9  maxv 	error = uvm_map(&vmspace->vm_map, &uva, hmapping->size, hmapping->uobj,
    768   1.9  maxv 	    0, 0, UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_SHARE,
    769   1.4  maxv 	    UVM_ADV_RANDOM, UVM_FLAG_FIXED|UVM_FLAG_UNMAP));
    770   1.4  maxv 	if (error) {
    771   1.9  maxv 		uao_detach(hmapping->uobj);
    772   1.4  maxv 	}
    773   1.4  maxv 
    774   1.4  maxv out:
    775   1.4  maxv 	nvmm_machine_put(mach);
    776   1.4  maxv 	return error;
    777   1.4  maxv }
    778   1.4  maxv 
    779   1.4  maxv static int
    780  1.14  maxv nvmm_hva_unmap(struct nvmm_owner *owner, struct nvmm_ioc_hva_unmap *args)
    781   1.4  maxv {
    782   1.4  maxv 	struct nvmm_machine *mach;
    783   1.4  maxv 	int error;
    784   1.4  maxv 
    785  1.14  maxv 	error = nvmm_machine_get(owner, args->machid, &mach, true);
    786   1.4  maxv 	if (error)
    787   1.4  maxv 		return error;
    788   1.4  maxv 
    789   1.9  maxv 	error = nvmm_hmapping_free(mach, args->hva, args->size);
    790   1.4  maxv 
    791   1.4  maxv 	nvmm_machine_put(mach);
    792   1.9  maxv 	return error;
    793   1.4  maxv }
    794   1.4  maxv 
    795   1.4  maxv /* -------------------------------------------------------------------------- */
    796   1.4  maxv 
    797   1.1  maxv static int
    798  1.14  maxv nvmm_gpa_map(struct nvmm_owner *owner, struct nvmm_ioc_gpa_map *args)
    799   1.1  maxv {
    800   1.1  maxv 	struct nvmm_machine *mach;
    801   1.4  maxv 	struct uvm_object *uobj;
    802   1.1  maxv 	gpaddr_t gpa;
    803   1.4  maxv 	size_t off;
    804   1.1  maxv 	int error;
    805   1.1  maxv 
    806  1.14  maxv 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    807   1.1  maxv 	if (error)
    808   1.1  maxv 		return error;
    809   1.1  maxv 
    810  1.11  maxv 	if ((args->prot & ~(PROT_READ|PROT_WRITE|PROT_EXEC)) != 0) {
    811  1.11  maxv 		error = EINVAL;
    812  1.11  maxv 		goto out;
    813  1.11  maxv 	}
    814  1.11  maxv 
    815   1.1  maxv 	if ((args->gpa % PAGE_SIZE) != 0 || (args->size % PAGE_SIZE) != 0 ||
    816   1.1  maxv 	    (args->hva % PAGE_SIZE) != 0) {
    817   1.1  maxv 		error = EINVAL;
    818   1.1  maxv 		goto out;
    819   1.1  maxv 	}
    820   1.1  maxv 	if (args->hva == 0) {
    821   1.1  maxv 		error = EINVAL;
    822   1.1  maxv 		goto out;
    823   1.1  maxv 	}
    824   1.1  maxv 	if (args->gpa < mach->gpa_begin || args->gpa >= mach->gpa_end) {
    825   1.1  maxv 		error = EINVAL;
    826   1.1  maxv 		goto out;
    827   1.1  maxv 	}
    828   1.1  maxv 	if (args->gpa + args->size <= args->gpa) {
    829   1.1  maxv 		error = EINVAL;
    830   1.1  maxv 		goto out;
    831   1.1  maxv 	}
    832   1.3  maxv 	if (args->gpa + args->size > mach->gpa_end) {
    833   1.1  maxv 		error = EINVAL;
    834   1.1  maxv 		goto out;
    835   1.1  maxv 	}
    836   1.1  maxv 	gpa = args->gpa;
    837   1.1  maxv 
    838   1.9  maxv 	uobj = nvmm_hmapping_getuobj(mach, args->hva, args->size, &off);
    839   1.4  maxv 	if (uobj == NULL) {
    840   1.4  maxv 		error = EINVAL;
    841   1.4  maxv 		goto out;
    842   1.4  maxv 	}
    843   1.4  maxv 
    844   1.4  maxv 	/* Take a reference for the machine. */
    845   1.4  maxv 	uao_reference(uobj);
    846   1.1  maxv 
    847   1.1  maxv 	/* Map the uobj into the machine address space, as pageable. */
    848   1.4  maxv 	error = uvm_map(&mach->vm->vm_map, &gpa, args->size, uobj, off, 0,
    849  1.11  maxv 	    UVM_MAPFLAG(args->prot, UVM_PROT_RWX, UVM_INH_NONE,
    850   1.4  maxv 	    UVM_ADV_RANDOM, UVM_FLAG_FIXED|UVM_FLAG_UNMAP));
    851   1.1  maxv 	if (error) {
    852   1.4  maxv 		uao_detach(uobj);
    853   1.1  maxv 		goto out;
    854   1.1  maxv 	}
    855   1.1  maxv 	if (gpa != args->gpa) {
    856   1.4  maxv 		uao_detach(uobj);
    857   1.1  maxv 		printf("[!] uvm_map problem\n");
    858   1.1  maxv 		error = EINVAL;
    859   1.1  maxv 		goto out;
    860   1.1  maxv 	}
    861   1.1  maxv 
    862   1.1  maxv out:
    863   1.1  maxv 	nvmm_machine_put(mach);
    864   1.1  maxv 	return error;
    865   1.1  maxv }
    866   1.1  maxv 
    867   1.1  maxv static int
    868  1.14  maxv nvmm_gpa_unmap(struct nvmm_owner *owner, struct nvmm_ioc_gpa_unmap *args)
    869   1.1  maxv {
    870   1.1  maxv 	struct nvmm_machine *mach;
    871   1.1  maxv 	gpaddr_t gpa;
    872   1.1  maxv 	int error;
    873   1.1  maxv 
    874  1.14  maxv 	error = nvmm_machine_get(owner, args->machid, &mach, false);
    875   1.1  maxv 	if (error)
    876   1.1  maxv 		return error;
    877   1.1  maxv 
    878   1.1  maxv 	if ((args->gpa % PAGE_SIZE) != 0 || (args->size % PAGE_SIZE) != 0) {
    879   1.1  maxv 		error = EINVAL;
    880   1.1  maxv 		goto out;
    881   1.1  maxv 	}
    882   1.1  maxv 	if (args->gpa < mach->gpa_begin || args->gpa >= mach->gpa_end) {
    883   1.1  maxv 		error = EINVAL;
    884   1.1  maxv 		goto out;
    885   1.1  maxv 	}
    886   1.1  maxv 	if (args->gpa + args->size <= args->gpa) {
    887   1.1  maxv 		error = EINVAL;
    888   1.1  maxv 		goto out;
    889   1.1  maxv 	}
    890   1.1  maxv 	if (args->gpa + args->size >= mach->gpa_end) {
    891   1.1  maxv 		error = EINVAL;
    892   1.1  maxv 		goto out;
    893   1.1  maxv 	}
    894   1.1  maxv 	gpa = args->gpa;
    895   1.1  maxv 
    896   1.1  maxv 	/* Unmap the memory from the machine. */
    897   1.1  maxv 	uvm_unmap(&mach->vm->vm_map, gpa, gpa + args->size);
    898   1.1  maxv 
    899   1.1  maxv out:
    900   1.1  maxv 	nvmm_machine_put(mach);
    901   1.1  maxv 	return error;
    902   1.1  maxv }
    903   1.1  maxv 
    904   1.1  maxv /* -------------------------------------------------------------------------- */
    905   1.1  maxv 
    906   1.1  maxv static int
    907  1.24  maxv nvmm_ctl_mach_info(struct nvmm_owner *owner, struct nvmm_ioc_ctl *args)
    908  1.17  maxv {
    909  1.17  maxv 	struct nvmm_ctl_mach_info ctl;
    910  1.17  maxv 	struct nvmm_machine *mach;
    911  1.17  maxv 	int error;
    912  1.17  maxv 	size_t i;
    913  1.17  maxv 
    914  1.17  maxv 	if (args->size != sizeof(ctl))
    915  1.17  maxv 		return EINVAL;
    916  1.17  maxv 	error = copyin(args->data, &ctl, sizeof(ctl));
    917  1.17  maxv 	if (error)
    918  1.17  maxv 		return error;
    919  1.17  maxv 
    920  1.24  maxv 	error = nvmm_machine_get(owner, ctl.machid, &mach, true);
    921  1.17  maxv 	if (error)
    922  1.17  maxv 		return error;
    923  1.17  maxv 
    924  1.36  maxv 	ctl.nvcpus = mach->ncpus;
    925  1.25  maxv 
    926  1.25  maxv 	ctl.nram = 0;
    927  1.25  maxv 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
    928  1.25  maxv 		if (!mach->hmap[i].present)
    929  1.25  maxv 			continue;
    930  1.25  maxv 		ctl.nram += mach->hmap[i].size;
    931  1.25  maxv 	}
    932  1.25  maxv 
    933  1.17  maxv 	ctl.pid = mach->owner->pid;
    934  1.17  maxv 	ctl.time = mach->time;
    935  1.17  maxv 
    936  1.17  maxv 	nvmm_machine_put(mach);
    937  1.17  maxv 
    938  1.17  maxv 	error = copyout(&ctl, args->data, sizeof(ctl));
    939  1.17  maxv 	if (error)
    940  1.17  maxv 		return error;
    941  1.17  maxv 
    942  1.17  maxv 	return 0;
    943  1.17  maxv }
    944  1.17  maxv 
    945  1.17  maxv static int
    946  1.17  maxv nvmm_ctl(struct nvmm_owner *owner, struct nvmm_ioc_ctl *args)
    947  1.17  maxv {
    948  1.17  maxv 	switch (args->op) {
    949  1.17  maxv 	case NVMM_CTL_MACH_INFO:
    950  1.24  maxv 		return nvmm_ctl_mach_info(owner, args);
    951  1.17  maxv 	default:
    952  1.17  maxv 		return EINVAL;
    953  1.17  maxv 	}
    954  1.17  maxv }
    955  1.17  maxv 
    956  1.17  maxv /* -------------------------------------------------------------------------- */
    957  1.17  maxv 
    958  1.31  maxv static const struct nvmm_impl *
    959  1.31  maxv nvmm_ident(void)
    960  1.31  maxv {
    961  1.31  maxv 	size_t i;
    962  1.31  maxv 
    963  1.31  maxv 	for (i = 0; i < __arraycount(nvmm_impl_list); i++) {
    964  1.31  maxv 		if ((*nvmm_impl_list[i]->ident)())
    965  1.31  maxv 			return nvmm_impl_list[i];
    966  1.31  maxv 	}
    967  1.31  maxv 
    968  1.31  maxv 	return NULL;
    969  1.31  maxv }
    970  1.31  maxv 
    971  1.17  maxv static int
    972   1.1  maxv nvmm_init(void)
    973   1.1  maxv {
    974   1.1  maxv 	size_t i, n;
    975   1.1  maxv 
    976  1.31  maxv 	nvmm_impl = nvmm_ident();
    977  1.31  maxv 	if (nvmm_impl == NULL)
    978   1.1  maxv 		return ENOTSUP;
    979   1.1  maxv 
    980   1.1  maxv 	for (i = 0; i < NVMM_MAX_MACHINES; i++) {
    981   1.1  maxv 		machines[i].machid = i;
    982   1.1  maxv 		rw_init(&machines[i].lock);
    983   1.1  maxv 		for (n = 0; n < NVMM_MAX_VCPUS; n++) {
    984  1.18  maxv 			machines[i].cpus[n].present = false;
    985  1.18  maxv 			machines[i].cpus[n].cpuid = n;
    986   1.1  maxv 			mutex_init(&machines[i].cpus[n].lock, MUTEX_DEFAULT,
    987   1.1  maxv 			    IPL_NONE);
    988   1.1  maxv 		}
    989   1.1  maxv 	}
    990   1.1  maxv 
    991   1.1  maxv 	(*nvmm_impl->init)();
    992   1.1  maxv 
    993   1.1  maxv 	return 0;
    994   1.1  maxv }
    995   1.1  maxv 
    996   1.1  maxv static void
    997   1.1  maxv nvmm_fini(void)
    998   1.1  maxv {
    999   1.1  maxv 	size_t i, n;
   1000   1.1  maxv 
   1001   1.1  maxv 	for (i = 0; i < NVMM_MAX_MACHINES; i++) {
   1002   1.1  maxv 		rw_destroy(&machines[i].lock);
   1003   1.1  maxv 		for (n = 0; n < NVMM_MAX_VCPUS; n++) {
   1004   1.1  maxv 			mutex_destroy(&machines[i].cpus[n].lock);
   1005   1.1  maxv 		}
   1006   1.1  maxv 	}
   1007   1.1  maxv 
   1008   1.1  maxv 	(*nvmm_impl->fini)();
   1009  1.29  maxv 	nvmm_impl = NULL;
   1010   1.1  maxv }
   1011   1.1  maxv 
   1012   1.1  maxv /* -------------------------------------------------------------------------- */
   1013   1.1  maxv 
   1014  1.14  maxv static dev_type_open(nvmm_open);
   1015  1.14  maxv 
   1016  1.14  maxv const struct cdevsw nvmm_cdevsw = {
   1017  1.14  maxv 	.d_open = nvmm_open,
   1018  1.14  maxv 	.d_close = noclose,
   1019  1.14  maxv 	.d_read = noread,
   1020  1.14  maxv 	.d_write = nowrite,
   1021  1.14  maxv 	.d_ioctl = noioctl,
   1022  1.14  maxv 	.d_stop = nostop,
   1023  1.14  maxv 	.d_tty = notty,
   1024  1.14  maxv 	.d_poll = nopoll,
   1025  1.14  maxv 	.d_mmap = nommap,
   1026  1.14  maxv 	.d_kqfilter = nokqfilter,
   1027  1.14  maxv 	.d_discard = nodiscard,
   1028  1.14  maxv 	.d_flag = D_OTHER | D_MPSAFE
   1029  1.14  maxv };
   1030  1.14  maxv 
   1031  1.14  maxv static int nvmm_ioctl(file_t *, u_long, void *);
   1032  1.14  maxv static int nvmm_close(file_t *);
   1033  1.19  maxv static int nvmm_mmap(file_t *, off_t *, size_t, int, int *, int *,
   1034  1.19  maxv     struct uvm_object **, int *);
   1035  1.14  maxv 
   1036  1.34  maxv static const struct fileops nvmm_fileops = {
   1037  1.14  maxv 	.fo_read = fbadop_read,
   1038  1.14  maxv 	.fo_write = fbadop_write,
   1039  1.14  maxv 	.fo_ioctl = nvmm_ioctl,
   1040  1.14  maxv 	.fo_fcntl = fnullop_fcntl,
   1041  1.14  maxv 	.fo_poll = fnullop_poll,
   1042  1.14  maxv 	.fo_stat = fbadop_stat,
   1043  1.14  maxv 	.fo_close = nvmm_close,
   1044  1.14  maxv 	.fo_kqfilter = fnullop_kqfilter,
   1045  1.14  maxv 	.fo_restart = fnullop_restart,
   1046  1.19  maxv 	.fo_mmap = nvmm_mmap,
   1047  1.14  maxv };
   1048  1.14  maxv 
   1049   1.1  maxv static int
   1050   1.1  maxv nvmm_open(dev_t dev, int flags, int type, struct lwp *l)
   1051   1.1  maxv {
   1052  1.14  maxv 	struct nvmm_owner *owner;
   1053  1.14  maxv 	struct file *fp;
   1054  1.14  maxv 	int error, fd;
   1055  1.14  maxv 
   1056  1.26  maxv 	if (__predict_false(nvmm_impl == NULL))
   1057  1.26  maxv 		return ENXIO;
   1058  1.14  maxv 	if (minor(dev) != 0)
   1059   1.1  maxv 		return EXDEV;
   1060  1.23  maxv 	if (!(flags & O_CLOEXEC))
   1061  1.23  maxv 		return EINVAL;
   1062  1.14  maxv 	error = fd_allocfile(&fp, &fd);
   1063  1.14  maxv 	if (error)
   1064  1.14  maxv 		return error;
   1065  1.14  maxv 
   1066  1.24  maxv 	if (OFLAGS(flags) & O_WRONLY) {
   1067  1.24  maxv 		owner = &root_owner;
   1068  1.24  maxv 	} else {
   1069  1.24  maxv 		owner = kmem_alloc(sizeof(*owner), KM_SLEEP);
   1070  1.24  maxv 		owner->pid = l->l_proc->p_pid;
   1071  1.24  maxv 	}
   1072   1.1  maxv 
   1073  1.14  maxv 	return fd_clone(fp, fd, flags, &nvmm_fileops, owner);
   1074   1.1  maxv }
   1075   1.1  maxv 
   1076   1.1  maxv static int
   1077  1.14  maxv nvmm_close(file_t *fp)
   1078   1.1  maxv {
   1079  1.14  maxv 	struct nvmm_owner *owner = fp->f_data;
   1080   1.1  maxv 
   1081  1.14  maxv 	KASSERT(owner != NULL);
   1082  1.14  maxv 	nvmm_kill_machines(owner);
   1083  1.24  maxv 	if (owner != &root_owner) {
   1084  1.24  maxv 		kmem_free(owner, sizeof(*owner));
   1085  1.24  maxv 	}
   1086  1.14  maxv 	fp->f_data = NULL;
   1087   1.1  maxv 
   1088  1.14  maxv    	return 0;
   1089   1.1  maxv }
   1090   1.1  maxv 
   1091   1.1  maxv static int
   1092  1.19  maxv nvmm_mmap(file_t *fp, off_t *offp, size_t size, int prot, int *flagsp,
   1093  1.19  maxv     int *advicep, struct uvm_object **uobjp, int *maxprotp)
   1094  1.19  maxv {
   1095  1.19  maxv 	struct nvmm_owner *owner = fp->f_data;
   1096  1.19  maxv 	struct nvmm_machine *mach;
   1097  1.19  maxv 	nvmm_machid_t machid;
   1098  1.19  maxv 	nvmm_cpuid_t cpuid;
   1099  1.19  maxv 	int error;
   1100  1.19  maxv 
   1101  1.19  maxv 	if (prot & PROT_EXEC)
   1102  1.19  maxv 		return EACCES;
   1103  1.19  maxv 	if (size != PAGE_SIZE)
   1104  1.19  maxv 		return EINVAL;
   1105  1.19  maxv 
   1106  1.19  maxv 	cpuid = NVMM_COMM_CPUID(*offp);
   1107  1.19  maxv 	if (__predict_false(cpuid >= NVMM_MAX_VCPUS))
   1108  1.19  maxv 		return EINVAL;
   1109  1.19  maxv 
   1110  1.19  maxv 	machid = NVMM_COMM_MACHID(*offp);
   1111  1.19  maxv 	error = nvmm_machine_get(owner, machid, &mach, false);
   1112  1.19  maxv 	if (error)
   1113  1.19  maxv 		return error;
   1114  1.19  maxv 
   1115  1.19  maxv 	uao_reference(mach->commuobj);
   1116  1.19  maxv 	*uobjp = mach->commuobj;
   1117  1.19  maxv 	*offp = cpuid * PAGE_SIZE;
   1118  1.19  maxv 	*maxprotp = prot;
   1119  1.19  maxv 	*advicep = UVM_ADV_RANDOM;
   1120  1.19  maxv 
   1121  1.19  maxv 	nvmm_machine_put(mach);
   1122  1.19  maxv 	return 0;
   1123  1.19  maxv }
   1124  1.19  maxv 
   1125  1.19  maxv static int
   1126  1.14  maxv nvmm_ioctl(file_t *fp, u_long cmd, void *data)
   1127   1.1  maxv {
   1128  1.14  maxv 	struct nvmm_owner *owner = fp->f_data;
   1129  1.14  maxv 
   1130  1.14  maxv 	KASSERT(owner != NULL);
   1131   1.1  maxv 
   1132   1.1  maxv 	switch (cmd) {
   1133   1.1  maxv 	case NVMM_IOC_CAPABILITY:
   1134  1.14  maxv 		return nvmm_capability(owner, data);
   1135   1.1  maxv 	case NVMM_IOC_MACHINE_CREATE:
   1136  1.14  maxv 		return nvmm_machine_create(owner, data);
   1137   1.1  maxv 	case NVMM_IOC_MACHINE_DESTROY:
   1138  1.14  maxv 		return nvmm_machine_destroy(owner, data);
   1139   1.1  maxv 	case NVMM_IOC_MACHINE_CONFIGURE:
   1140  1.14  maxv 		return nvmm_machine_configure(owner, data);
   1141   1.1  maxv 	case NVMM_IOC_VCPU_CREATE:
   1142  1.14  maxv 		return nvmm_vcpu_create(owner, data);
   1143   1.1  maxv 	case NVMM_IOC_VCPU_DESTROY:
   1144  1.14  maxv 		return nvmm_vcpu_destroy(owner, data);
   1145  1.23  maxv 	case NVMM_IOC_VCPU_CONFIGURE:
   1146  1.23  maxv 		return nvmm_vcpu_configure(owner, data);
   1147   1.1  maxv 	case NVMM_IOC_VCPU_SETSTATE:
   1148  1.14  maxv 		return nvmm_vcpu_setstate(owner, data);
   1149   1.1  maxv 	case NVMM_IOC_VCPU_GETSTATE:
   1150  1.14  maxv 		return nvmm_vcpu_getstate(owner, data);
   1151   1.1  maxv 	case NVMM_IOC_VCPU_INJECT:
   1152  1.14  maxv 		return nvmm_vcpu_inject(owner, data);
   1153   1.1  maxv 	case NVMM_IOC_VCPU_RUN:
   1154  1.14  maxv 		return nvmm_vcpu_run(owner, data);
   1155   1.1  maxv 	case NVMM_IOC_GPA_MAP:
   1156  1.14  maxv 		return nvmm_gpa_map(owner, data);
   1157   1.1  maxv 	case NVMM_IOC_GPA_UNMAP:
   1158  1.14  maxv 		return nvmm_gpa_unmap(owner, data);
   1159   1.4  maxv 	case NVMM_IOC_HVA_MAP:
   1160  1.14  maxv 		return nvmm_hva_map(owner, data);
   1161   1.4  maxv 	case NVMM_IOC_HVA_UNMAP:
   1162  1.14  maxv 		return nvmm_hva_unmap(owner, data);
   1163  1.17  maxv 	case NVMM_IOC_CTL:
   1164  1.17  maxv 		return nvmm_ctl(owner, data);
   1165   1.1  maxv 	default:
   1166   1.1  maxv 		return EINVAL;
   1167   1.1  maxv 	}
   1168   1.1  maxv }
   1169   1.1  maxv 
   1170  1.14  maxv /* -------------------------------------------------------------------------- */
   1171   1.1  maxv 
   1172  1.31  maxv static int nvmm_match(device_t, cfdata_t, void *);
   1173  1.31  maxv static void nvmm_attach(device_t, device_t, void *);
   1174  1.31  maxv static int nvmm_detach(device_t, int);
   1175  1.31  maxv 
   1176  1.31  maxv extern struct cfdriver nvmm_cd;
   1177  1.31  maxv 
   1178  1.31  maxv CFATTACH_DECL_NEW(nvmm, 0, nvmm_match, nvmm_attach, nvmm_detach, NULL);
   1179  1.31  maxv 
   1180  1.31  maxv static struct cfdata nvmm_cfdata[] = {
   1181  1.31  maxv 	{
   1182  1.31  maxv 		.cf_name = "nvmm",
   1183  1.31  maxv 		.cf_atname = "nvmm",
   1184  1.31  maxv 		.cf_unit = 0,
   1185  1.31  maxv 		.cf_fstate = FSTATE_STAR,
   1186  1.31  maxv 		.cf_loc = NULL,
   1187  1.31  maxv 		.cf_flags = 0,
   1188  1.31  maxv 		.cf_pspec = NULL,
   1189  1.31  maxv 	},
   1190  1.31  maxv 	{ NULL, NULL, 0, FSTATE_NOTFOUND, NULL, 0, NULL }
   1191  1.31  maxv };
   1192  1.31  maxv 
   1193  1.31  maxv static int
   1194  1.31  maxv nvmm_match(device_t self, cfdata_t cfdata, void *arg)
   1195  1.31  maxv {
   1196  1.31  maxv 	return 1;
   1197  1.31  maxv }
   1198  1.31  maxv 
   1199  1.31  maxv static void
   1200  1.31  maxv nvmm_attach(device_t parent, device_t self, void *aux)
   1201  1.31  maxv {
   1202  1.31  maxv 	int error;
   1203  1.31  maxv 
   1204  1.31  maxv 	error = nvmm_init();
   1205  1.31  maxv 	if (error)
   1206  1.31  maxv 		panic("%s: impossible", __func__);
   1207  1.32  maxv 	aprint_normal_dev(self, "attached, using backend %s\n",
   1208  1.32  maxv 	    nvmm_impl->name);
   1209  1.31  maxv }
   1210  1.31  maxv 
   1211  1.31  maxv static int
   1212  1.31  maxv nvmm_detach(device_t self, int flags)
   1213  1.31  maxv {
   1214  1.35  maxv 	if (atomic_load_relaxed(&nmachines) > 0)
   1215  1.31  maxv 		return EBUSY;
   1216  1.31  maxv 	nvmm_fini();
   1217  1.31  maxv 	return 0;
   1218  1.31  maxv }
   1219  1.31  maxv 
   1220   1.1  maxv void
   1221   1.1  maxv nvmmattach(int nunits)
   1222   1.1  maxv {
   1223   1.1  maxv 	/* nothing */
   1224   1.1  maxv }
   1225   1.1  maxv 
   1226  1.16  maxv MODULE(MODULE_CLASS_MISC, nvmm, NULL);
   1227   1.1  maxv 
   1228  1.31  maxv #if defined(_MODULE)
   1229  1.31  maxv CFDRIVER_DECL(nvmm, DV_VIRTUAL, NULL);
   1230  1.31  maxv #endif
   1231  1.31  maxv 
   1232   1.1  maxv static int
   1233   1.1  maxv nvmm_modcmd(modcmd_t cmd, void *arg)
   1234   1.1  maxv {
   1235  1.31  maxv #if defined(_MODULE)
   1236  1.31  maxv 	devmajor_t bmajor = NODEVMAJOR;
   1237  1.31  maxv 	devmajor_t cmajor = 345;
   1238  1.31  maxv #endif
   1239   1.1  maxv 	int error;
   1240   1.1  maxv 
   1241   1.1  maxv 	switch (cmd) {
   1242   1.1  maxv 	case MODULE_CMD_INIT:
   1243  1.31  maxv 		if (nvmm_ident() == NULL) {
   1244  1.31  maxv 			aprint_error("%s: cpu not supported\n",
   1245  1.31  maxv 			    nvmm_cd.cd_name);
   1246  1.31  maxv 			return ENOTSUP;
   1247  1.31  maxv 		}
   1248  1.31  maxv #if defined(_MODULE)
   1249  1.31  maxv 		error = config_cfdriver_attach(&nvmm_cd);
   1250   1.1  maxv 		if (error)
   1251   1.1  maxv 			return error;
   1252  1.31  maxv #endif
   1253  1.31  maxv 		error = config_cfattach_attach(nvmm_cd.cd_name, &nvmm_ca);
   1254  1.31  maxv 		if (error) {
   1255  1.31  maxv 			config_cfdriver_detach(&nvmm_cd);
   1256  1.31  maxv 			aprint_error("%s: config_cfattach_attach failed\n",
   1257  1.31  maxv 			    nvmm_cd.cd_name);
   1258  1.31  maxv 			return error;
   1259  1.31  maxv 		}
   1260  1.31  maxv 
   1261  1.31  maxv 		error = config_cfdata_attach(nvmm_cfdata, 1);
   1262  1.31  maxv 		if (error) {
   1263  1.31  maxv 			config_cfattach_detach(nvmm_cd.cd_name, &nvmm_ca);
   1264  1.31  maxv 			config_cfdriver_detach(&nvmm_cd);
   1265  1.31  maxv 			aprint_error("%s: unable to register cfdata\n",
   1266  1.31  maxv 			    nvmm_cd.cd_name);
   1267  1.31  maxv 			return error;
   1268  1.31  maxv 		}
   1269  1.31  maxv 
   1270  1.31  maxv 		if (config_attach_pseudo(nvmm_cfdata) == NULL) {
   1271  1.31  maxv 			aprint_error("%s: config_attach_pseudo failed\n",
   1272  1.31  maxv 			    nvmm_cd.cd_name);
   1273  1.31  maxv 			config_cfattach_detach(nvmm_cd.cd_name, &nvmm_ca);
   1274  1.31  maxv 			config_cfdriver_detach(&nvmm_cd);
   1275  1.31  maxv 			return ENXIO;
   1276  1.31  maxv 		}
   1277   1.1  maxv 
   1278   1.1  maxv #if defined(_MODULE)
   1279  1.31  maxv 		/* mknod /dev/nvmm c 345 0 */
   1280  1.31  maxv 		error = devsw_attach(nvmm_cd.cd_name, NULL, &bmajor,
   1281  1.31  maxv 			&nvmm_cdevsw, &cmajor);
   1282  1.31  maxv 		if (error) {
   1283  1.31  maxv 			aprint_error("%s: unable to register devsw\n",
   1284  1.31  maxv 			    nvmm_cd.cd_name);
   1285  1.31  maxv 			config_cfattach_detach(nvmm_cd.cd_name, &nvmm_ca);
   1286  1.31  maxv 			config_cfdriver_detach(&nvmm_cd);
   1287  1.31  maxv 			return error;
   1288   1.1  maxv 		}
   1289   1.1  maxv #endif
   1290   1.1  maxv 		return 0;
   1291   1.1  maxv 	case MODULE_CMD_FINI:
   1292  1.31  maxv 		error = config_cfdata_detach(nvmm_cfdata);
   1293  1.31  maxv 		if (error)
   1294  1.31  maxv 			return error;
   1295  1.31  maxv 		error = config_cfattach_detach(nvmm_cd.cd_name, &nvmm_ca);
   1296  1.31  maxv 		if (error)
   1297  1.31  maxv 			return error;
   1298   1.1  maxv #if defined(_MODULE)
   1299  1.31  maxv 		config_cfdriver_detach(&nvmm_cd);
   1300  1.31  maxv 		devsw_detach(NULL, &nvmm_cdevsw);
   1301   1.1  maxv #endif
   1302   1.1  maxv 		return 0;
   1303  1.13  maxv 	case MODULE_CMD_AUTOUNLOAD:
   1304  1.13  maxv 		return EBUSY;
   1305   1.1  maxv 	default:
   1306   1.1  maxv 		return ENOTTY;
   1307   1.1  maxv 	}
   1308   1.1  maxv }
   1309