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