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