nvmm.c revision 1.19 1 1.19 maxv /* $NetBSD: nvmm.c,v 1.19 2019/04/28 14:22:13 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.19 maxv __KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.19 2019/04/28 14:22:13 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.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.1 maxv 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.1 maxv 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.1 maxv args->cap.version = NVMM_CAPABILITY_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.1 maxv void *data;
342 1.1 maxv int error;
343 1.1 maxv
344 1.1 maxv if (__predict_false(args->op >= nvmm_impl->conf_max)) {
345 1.1 maxv return EINVAL;
346 1.1 maxv }
347 1.1 maxv
348 1.1 maxv allocsz = nvmm_impl->conf_sizes[args->op];
349 1.1 maxv data = kmem_alloc(allocsz, KM_SLEEP);
350 1.1 maxv
351 1.14 maxv error = nvmm_machine_get(owner, args->machid, &mach, true);
352 1.1 maxv if (error) {
353 1.1 maxv kmem_free(data, allocsz);
354 1.1 maxv return error;
355 1.1 maxv }
356 1.1 maxv
357 1.1 maxv error = copyin(args->conf, data, allocsz);
358 1.1 maxv if (error) {
359 1.1 maxv goto out;
360 1.1 maxv }
361 1.1 maxv
362 1.1 maxv error = (*nvmm_impl->machine_configure)(mach, args->op, data);
363 1.1 maxv
364 1.1 maxv out:
365 1.1 maxv nvmm_machine_put(mach);
366 1.1 maxv kmem_free(data, allocsz);
367 1.1 maxv return error;
368 1.1 maxv }
369 1.1 maxv
370 1.1 maxv static int
371 1.14 maxv nvmm_vcpu_create(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_create *args)
372 1.1 maxv {
373 1.1 maxv struct nvmm_machine *mach;
374 1.1 maxv struct nvmm_cpu *vcpu;
375 1.1 maxv int error;
376 1.1 maxv
377 1.14 maxv error = nvmm_machine_get(owner, args->machid, &mach, false);
378 1.1 maxv if (error)
379 1.1 maxv return error;
380 1.1 maxv
381 1.18 maxv error = nvmm_vcpu_alloc(mach, args->cpuid, &vcpu);
382 1.1 maxv if (error)
383 1.1 maxv goto out;
384 1.1 maxv
385 1.19 maxv /* Allocate the comm page. */
386 1.19 maxv uao_reference(mach->commuobj);
387 1.19 maxv error = uvm_map(kernel_map, (vaddr_t *)&vcpu->comm, PAGE_SIZE,
388 1.19 maxv mach->commuobj, args->cpuid * PAGE_SIZE, 0, UVM_MAPFLAG(UVM_PROT_RW,
389 1.19 maxv UVM_PROT_RW, UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
390 1.19 maxv if (error) {
391 1.19 maxv uao_detach(mach->commuobj);
392 1.19 maxv nvmm_vcpu_free(mach, vcpu);
393 1.19 maxv nvmm_vcpu_put(vcpu);
394 1.19 maxv goto out;
395 1.19 maxv }
396 1.19 maxv error = uvm_map_pageable(kernel_map, (vaddr_t)vcpu->comm,
397 1.19 maxv (vaddr_t)vcpu->comm + PAGE_SIZE, false, 0);
398 1.19 maxv if (error) {
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 memset(vcpu->comm, 0, PAGE_SIZE);
404 1.19 maxv
405 1.1 maxv error = (*nvmm_impl->vcpu_create)(mach, vcpu);
406 1.1 maxv if (error) {
407 1.1 maxv nvmm_vcpu_free(mach, vcpu);
408 1.1 maxv nvmm_vcpu_put(vcpu);
409 1.1 maxv goto out;
410 1.1 maxv }
411 1.1 maxv
412 1.1 maxv nvmm_vcpu_put(vcpu);
413 1.1 maxv
414 1.1 maxv out:
415 1.1 maxv nvmm_machine_put(mach);
416 1.1 maxv return error;
417 1.1 maxv }
418 1.1 maxv
419 1.1 maxv static int
420 1.14 maxv nvmm_vcpu_destroy(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_destroy *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.1 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.1 maxv (*nvmm_impl->vcpu_destroy)(mach, vcpu);
435 1.1 maxv nvmm_vcpu_free(mach, vcpu);
436 1.1 maxv nvmm_vcpu_put(vcpu);
437 1.1 maxv
438 1.1 maxv out:
439 1.1 maxv nvmm_machine_put(mach);
440 1.1 maxv return error;
441 1.1 maxv }
442 1.1 maxv
443 1.1 maxv static int
444 1.14 maxv nvmm_vcpu_setstate(struct nvmm_owner *owner,
445 1.14 maxv struct nvmm_ioc_vcpu_setstate *args)
446 1.1 maxv {
447 1.1 maxv struct nvmm_machine *mach;
448 1.1 maxv struct nvmm_cpu *vcpu;
449 1.1 maxv int error;
450 1.1 maxv
451 1.14 maxv error = nvmm_machine_get(owner, args->machid, &mach, false);
452 1.6 maxv if (error)
453 1.1 maxv return error;
454 1.1 maxv
455 1.1 maxv error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
456 1.1 maxv if (error)
457 1.1 maxv goto out;
458 1.1 maxv
459 1.19 maxv (*nvmm_impl->vcpu_setstate)(vcpu);
460 1.1 maxv nvmm_vcpu_put(vcpu);
461 1.1 maxv
462 1.1 maxv out:
463 1.1 maxv nvmm_machine_put(mach);
464 1.1 maxv return error;
465 1.1 maxv }
466 1.1 maxv
467 1.1 maxv static int
468 1.14 maxv nvmm_vcpu_getstate(struct nvmm_owner *owner,
469 1.14 maxv struct nvmm_ioc_vcpu_getstate *args)
470 1.1 maxv {
471 1.1 maxv struct nvmm_machine *mach;
472 1.1 maxv struct nvmm_cpu *vcpu;
473 1.1 maxv int error;
474 1.1 maxv
475 1.14 maxv error = nvmm_machine_get(owner, args->machid, &mach, false);
476 1.6 maxv if (error)
477 1.1 maxv return error;
478 1.1 maxv
479 1.1 maxv error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
480 1.1 maxv if (error)
481 1.1 maxv goto out;
482 1.1 maxv
483 1.19 maxv (*nvmm_impl->vcpu_getstate)(vcpu);
484 1.1 maxv nvmm_vcpu_put(vcpu);
485 1.1 maxv
486 1.1 maxv out:
487 1.1 maxv nvmm_machine_put(mach);
488 1.1 maxv return error;
489 1.1 maxv }
490 1.1 maxv
491 1.1 maxv static int
492 1.14 maxv nvmm_vcpu_inject(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_inject *args)
493 1.1 maxv {
494 1.1 maxv struct nvmm_machine *mach;
495 1.1 maxv struct nvmm_cpu *vcpu;
496 1.1 maxv int error;
497 1.1 maxv
498 1.14 maxv error = nvmm_machine_get(owner, args->machid, &mach, false);
499 1.1 maxv if (error)
500 1.1 maxv return error;
501 1.1 maxv
502 1.1 maxv error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
503 1.1 maxv if (error)
504 1.1 maxv goto out;
505 1.1 maxv
506 1.1 maxv error = (*nvmm_impl->vcpu_inject)(mach, vcpu, &args->event);
507 1.1 maxv nvmm_vcpu_put(vcpu);
508 1.1 maxv
509 1.1 maxv out:
510 1.1 maxv nvmm_machine_put(mach);
511 1.1 maxv return error;
512 1.1 maxv }
513 1.1 maxv
514 1.8 maxv static void
515 1.8 maxv nvmm_do_vcpu_run(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
516 1.8 maxv struct nvmm_exit *exit)
517 1.8 maxv {
518 1.8 maxv struct vmspace *vm = mach->vm;
519 1.8 maxv
520 1.8 maxv while (1) {
521 1.8 maxv (*nvmm_impl->vcpu_run)(mach, vcpu, exit);
522 1.8 maxv
523 1.8 maxv if (__predict_true(exit->reason != NVMM_EXIT_MEMORY)) {
524 1.8 maxv break;
525 1.8 maxv }
526 1.10 maxv if (exit->u.mem.gpa >= mach->gpa_end) {
527 1.10 maxv break;
528 1.10 maxv }
529 1.11 maxv if (uvm_fault(&vm->vm_map, exit->u.mem.gpa, exit->u.mem.prot)) {
530 1.8 maxv break;
531 1.8 maxv }
532 1.8 maxv }
533 1.8 maxv }
534 1.8 maxv
535 1.1 maxv static int
536 1.14 maxv nvmm_vcpu_run(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_run *args)
537 1.1 maxv {
538 1.1 maxv struct nvmm_machine *mach;
539 1.1 maxv struct nvmm_cpu *vcpu;
540 1.1 maxv int error;
541 1.1 maxv
542 1.14 maxv error = nvmm_machine_get(owner, args->machid, &mach, false);
543 1.1 maxv if (error)
544 1.1 maxv return error;
545 1.1 maxv
546 1.1 maxv error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
547 1.1 maxv if (error)
548 1.1 maxv goto out;
549 1.1 maxv
550 1.8 maxv nvmm_do_vcpu_run(mach, vcpu, &args->exit);
551 1.1 maxv nvmm_vcpu_put(vcpu);
552 1.1 maxv
553 1.1 maxv out:
554 1.1 maxv nvmm_machine_put(mach);
555 1.1 maxv return error;
556 1.1 maxv }
557 1.1 maxv
558 1.1 maxv /* -------------------------------------------------------------------------- */
559 1.1 maxv
560 1.4 maxv static struct uvm_object *
561 1.9 maxv nvmm_hmapping_getuobj(struct nvmm_machine *mach, uintptr_t hva, size_t size,
562 1.4 maxv size_t *off)
563 1.4 maxv {
564 1.9 maxv struct nvmm_hmapping *hmapping;
565 1.4 maxv size_t i;
566 1.4 maxv
567 1.9 maxv for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
568 1.9 maxv hmapping = &mach->hmap[i];
569 1.9 maxv if (!hmapping->present) {
570 1.4 maxv continue;
571 1.4 maxv }
572 1.9 maxv if (hva >= hmapping->hva &&
573 1.9 maxv hva + size <= hmapping->hva + hmapping->size) {
574 1.9 maxv *off = hva - hmapping->hva;
575 1.9 maxv return hmapping->uobj;
576 1.4 maxv }
577 1.4 maxv }
578 1.4 maxv
579 1.4 maxv return NULL;
580 1.4 maxv }
581 1.4 maxv
582 1.4 maxv static int
583 1.9 maxv nvmm_hmapping_validate(struct nvmm_machine *mach, uintptr_t hva, size_t size)
584 1.4 maxv {
585 1.9 maxv struct nvmm_hmapping *hmapping;
586 1.4 maxv size_t i;
587 1.4 maxv
588 1.4 maxv if ((hva % PAGE_SIZE) != 0 || (size % PAGE_SIZE) != 0) {
589 1.4 maxv return EINVAL;
590 1.4 maxv }
591 1.4 maxv if (hva == 0) {
592 1.4 maxv return EINVAL;
593 1.4 maxv }
594 1.4 maxv
595 1.9 maxv for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
596 1.9 maxv hmapping = &mach->hmap[i];
597 1.9 maxv if (!hmapping->present) {
598 1.4 maxv continue;
599 1.4 maxv }
600 1.4 maxv
601 1.9 maxv if (hva >= hmapping->hva &&
602 1.9 maxv hva + size <= hmapping->hva + hmapping->size) {
603 1.4 maxv break;
604 1.4 maxv }
605 1.4 maxv
606 1.9 maxv if (hva >= hmapping->hva &&
607 1.9 maxv hva < hmapping->hva + hmapping->size) {
608 1.4 maxv return EEXIST;
609 1.4 maxv }
610 1.9 maxv if (hva + size > hmapping->hva &&
611 1.9 maxv hva + size <= hmapping->hva + hmapping->size) {
612 1.4 maxv return EEXIST;
613 1.4 maxv }
614 1.9 maxv if (hva <= hmapping->hva &&
615 1.9 maxv hva + size >= hmapping->hva + hmapping->size) {
616 1.4 maxv return EEXIST;
617 1.4 maxv }
618 1.4 maxv }
619 1.4 maxv
620 1.4 maxv return 0;
621 1.4 maxv }
622 1.4 maxv
623 1.9 maxv static struct nvmm_hmapping *
624 1.9 maxv nvmm_hmapping_alloc(struct nvmm_machine *mach)
625 1.4 maxv {
626 1.9 maxv struct nvmm_hmapping *hmapping;
627 1.4 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) {
632 1.9 maxv hmapping->present = true;
633 1.9 maxv return hmapping;
634 1.4 maxv }
635 1.4 maxv }
636 1.4 maxv
637 1.4 maxv return NULL;
638 1.4 maxv }
639 1.4 maxv
640 1.9 maxv static int
641 1.9 maxv nvmm_hmapping_free(struct nvmm_machine *mach, uintptr_t hva, size_t size)
642 1.4 maxv {
643 1.4 maxv struct vmspace *vmspace = curproc->p_vmspace;
644 1.9 maxv struct nvmm_hmapping *hmapping;
645 1.9 maxv size_t i;
646 1.4 maxv
647 1.9 maxv for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {
648 1.9 maxv hmapping = &mach->hmap[i];
649 1.9 maxv if (!hmapping->present || hmapping->hva != hva ||
650 1.9 maxv hmapping->size != size) {
651 1.9 maxv continue;
652 1.9 maxv }
653 1.9 maxv
654 1.9 maxv uvm_unmap(&vmspace->vm_map, hmapping->hva,
655 1.9 maxv hmapping->hva + hmapping->size);
656 1.9 maxv uao_detach(hmapping->uobj);
657 1.4 maxv
658 1.9 maxv hmapping->uobj = NULL;
659 1.9 maxv hmapping->present = false;
660 1.9 maxv
661 1.9 maxv return 0;
662 1.9 maxv }
663 1.9 maxv
664 1.9 maxv return ENOENT;
665 1.4 maxv }
666 1.4 maxv
667 1.4 maxv static int
668 1.14 maxv nvmm_hva_map(struct nvmm_owner *owner, struct nvmm_ioc_hva_map *args)
669 1.4 maxv {
670 1.4 maxv struct vmspace *vmspace = curproc->p_vmspace;
671 1.4 maxv struct nvmm_machine *mach;
672 1.9 maxv struct nvmm_hmapping *hmapping;
673 1.4 maxv vaddr_t uva;
674 1.4 maxv int error;
675 1.4 maxv
676 1.14 maxv error = nvmm_machine_get(owner, args->machid, &mach, true);
677 1.4 maxv if (error)
678 1.4 maxv return error;
679 1.4 maxv
680 1.9 maxv error = nvmm_hmapping_validate(mach, args->hva, args->size);
681 1.4 maxv if (error)
682 1.4 maxv goto out;
683 1.4 maxv
684 1.9 maxv hmapping = nvmm_hmapping_alloc(mach);
685 1.9 maxv if (hmapping == NULL) {
686 1.4 maxv error = ENOBUFS;
687 1.4 maxv goto out;
688 1.4 maxv }
689 1.4 maxv
690 1.9 maxv hmapping->hva = args->hva;
691 1.9 maxv hmapping->size = args->size;
692 1.9 maxv hmapping->uobj = uao_create(hmapping->size, 0);
693 1.9 maxv uva = hmapping->hva;
694 1.4 maxv
695 1.4 maxv /* Take a reference for the user. */
696 1.9 maxv uao_reference(hmapping->uobj);
697 1.4 maxv
698 1.4 maxv /* Map the uobj into the user address space, as pageable. */
699 1.9 maxv error = uvm_map(&vmspace->vm_map, &uva, hmapping->size, hmapping->uobj,
700 1.9 maxv 0, 0, UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_SHARE,
701 1.4 maxv UVM_ADV_RANDOM, UVM_FLAG_FIXED|UVM_FLAG_UNMAP));
702 1.4 maxv if (error) {
703 1.9 maxv uao_detach(hmapping->uobj);
704 1.4 maxv }
705 1.4 maxv
706 1.4 maxv out:
707 1.4 maxv nvmm_machine_put(mach);
708 1.4 maxv return error;
709 1.4 maxv }
710 1.4 maxv
711 1.4 maxv static int
712 1.14 maxv nvmm_hva_unmap(struct nvmm_owner *owner, struct nvmm_ioc_hva_unmap *args)
713 1.4 maxv {
714 1.4 maxv struct nvmm_machine *mach;
715 1.4 maxv int error;
716 1.4 maxv
717 1.14 maxv error = nvmm_machine_get(owner, args->machid, &mach, true);
718 1.4 maxv if (error)
719 1.4 maxv return error;
720 1.4 maxv
721 1.9 maxv error = nvmm_hmapping_free(mach, args->hva, args->size);
722 1.4 maxv
723 1.4 maxv nvmm_machine_put(mach);
724 1.9 maxv return error;
725 1.4 maxv }
726 1.4 maxv
727 1.4 maxv /* -------------------------------------------------------------------------- */
728 1.4 maxv
729 1.1 maxv static int
730 1.14 maxv nvmm_gpa_map(struct nvmm_owner *owner, struct nvmm_ioc_gpa_map *args)
731 1.1 maxv {
732 1.1 maxv struct nvmm_machine *mach;
733 1.4 maxv struct uvm_object *uobj;
734 1.1 maxv gpaddr_t gpa;
735 1.4 maxv size_t off;
736 1.1 maxv int error;
737 1.1 maxv
738 1.14 maxv error = nvmm_machine_get(owner, args->machid, &mach, false);
739 1.1 maxv if (error)
740 1.1 maxv return error;
741 1.1 maxv
742 1.11 maxv if ((args->prot & ~(PROT_READ|PROT_WRITE|PROT_EXEC)) != 0) {
743 1.11 maxv error = EINVAL;
744 1.11 maxv goto out;
745 1.11 maxv }
746 1.11 maxv
747 1.1 maxv if ((args->gpa % PAGE_SIZE) != 0 || (args->size % PAGE_SIZE) != 0 ||
748 1.1 maxv (args->hva % PAGE_SIZE) != 0) {
749 1.1 maxv error = EINVAL;
750 1.1 maxv goto out;
751 1.1 maxv }
752 1.1 maxv if (args->hva == 0) {
753 1.1 maxv error = EINVAL;
754 1.1 maxv goto out;
755 1.1 maxv }
756 1.1 maxv if (args->gpa < mach->gpa_begin || args->gpa >= mach->gpa_end) {
757 1.1 maxv error = EINVAL;
758 1.1 maxv goto out;
759 1.1 maxv }
760 1.1 maxv if (args->gpa + args->size <= args->gpa) {
761 1.1 maxv error = EINVAL;
762 1.1 maxv goto out;
763 1.1 maxv }
764 1.3 maxv if (args->gpa + args->size > mach->gpa_end) {
765 1.1 maxv error = EINVAL;
766 1.1 maxv goto out;
767 1.1 maxv }
768 1.1 maxv gpa = args->gpa;
769 1.1 maxv
770 1.9 maxv uobj = nvmm_hmapping_getuobj(mach, args->hva, args->size, &off);
771 1.4 maxv if (uobj == NULL) {
772 1.4 maxv error = EINVAL;
773 1.4 maxv goto out;
774 1.4 maxv }
775 1.4 maxv
776 1.4 maxv /* Take a reference for the machine. */
777 1.4 maxv uao_reference(uobj);
778 1.1 maxv
779 1.1 maxv /* Map the uobj into the machine address space, as pageable. */
780 1.4 maxv error = uvm_map(&mach->vm->vm_map, &gpa, args->size, uobj, off, 0,
781 1.11 maxv UVM_MAPFLAG(args->prot, UVM_PROT_RWX, UVM_INH_NONE,
782 1.4 maxv UVM_ADV_RANDOM, UVM_FLAG_FIXED|UVM_FLAG_UNMAP));
783 1.1 maxv if (error) {
784 1.4 maxv uao_detach(uobj);
785 1.1 maxv goto out;
786 1.1 maxv }
787 1.1 maxv if (gpa != args->gpa) {
788 1.4 maxv uao_detach(uobj);
789 1.1 maxv printf("[!] uvm_map problem\n");
790 1.1 maxv error = EINVAL;
791 1.1 maxv goto out;
792 1.1 maxv }
793 1.1 maxv
794 1.1 maxv out:
795 1.1 maxv nvmm_machine_put(mach);
796 1.1 maxv return error;
797 1.1 maxv }
798 1.1 maxv
799 1.1 maxv static int
800 1.14 maxv nvmm_gpa_unmap(struct nvmm_owner *owner, struct nvmm_ioc_gpa_unmap *args)
801 1.1 maxv {
802 1.1 maxv struct nvmm_machine *mach;
803 1.1 maxv gpaddr_t gpa;
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.1 maxv if ((args->gpa % PAGE_SIZE) != 0 || (args->size % PAGE_SIZE) != 0) {
811 1.1 maxv error = EINVAL;
812 1.1 maxv goto out;
813 1.1 maxv }
814 1.1 maxv if (args->gpa < mach->gpa_begin || args->gpa >= mach->gpa_end) {
815 1.1 maxv error = EINVAL;
816 1.1 maxv goto out;
817 1.1 maxv }
818 1.1 maxv if (args->gpa + args->size <= args->gpa) {
819 1.1 maxv error = EINVAL;
820 1.1 maxv goto out;
821 1.1 maxv }
822 1.1 maxv if (args->gpa + args->size >= mach->gpa_end) {
823 1.1 maxv error = EINVAL;
824 1.1 maxv goto out;
825 1.1 maxv }
826 1.1 maxv gpa = args->gpa;
827 1.1 maxv
828 1.1 maxv /* Unmap the memory from the machine. */
829 1.1 maxv uvm_unmap(&mach->vm->vm_map, gpa, gpa + args->size);
830 1.1 maxv
831 1.1 maxv out:
832 1.1 maxv nvmm_machine_put(mach);
833 1.1 maxv return error;
834 1.1 maxv }
835 1.1 maxv
836 1.1 maxv /* -------------------------------------------------------------------------- */
837 1.1 maxv
838 1.1 maxv static int
839 1.17 maxv nvmm_ctl_mach_info(struct nvmm_ioc_ctl *args)
840 1.17 maxv {
841 1.17 maxv struct nvmm_ctl_mach_info ctl;
842 1.17 maxv struct nvmm_machine *mach;
843 1.17 maxv struct nvmm_cpu *vcpu;
844 1.17 maxv int error;
845 1.17 maxv size_t i;
846 1.17 maxv
847 1.17 maxv if (args->size != sizeof(ctl))
848 1.17 maxv return EINVAL;
849 1.17 maxv error = copyin(args->data, &ctl, sizeof(ctl));
850 1.17 maxv if (error)
851 1.17 maxv return error;
852 1.17 maxv
853 1.17 maxv error = nvmm_machine_get(&root_owner, ctl.machid, &mach, true);
854 1.17 maxv if (error)
855 1.17 maxv return error;
856 1.17 maxv
857 1.17 maxv ctl.nvcpus = 0;
858 1.17 maxv for (i = 0; i < NVMM_MAX_VCPUS; i++) {
859 1.17 maxv error = nvmm_vcpu_get(mach, i, &vcpu);
860 1.17 maxv if (error)
861 1.17 maxv continue;
862 1.17 maxv ctl.nvcpus++;
863 1.17 maxv nvmm_vcpu_put(vcpu);
864 1.17 maxv }
865 1.17 maxv ctl.pid = mach->owner->pid;
866 1.17 maxv ctl.time = mach->time;
867 1.17 maxv
868 1.17 maxv nvmm_machine_put(mach);
869 1.17 maxv
870 1.17 maxv error = copyout(&ctl, args->data, sizeof(ctl));
871 1.17 maxv if (error)
872 1.17 maxv return error;
873 1.17 maxv
874 1.17 maxv return 0;
875 1.17 maxv }
876 1.17 maxv
877 1.17 maxv static int
878 1.17 maxv nvmm_ctl(struct nvmm_owner *owner, struct nvmm_ioc_ctl *args)
879 1.17 maxv {
880 1.17 maxv int error;
881 1.17 maxv
882 1.17 maxv error = kauth_authorize_device(curlwp->l_cred, KAUTH_DEVICE_NVMM_CTL,
883 1.17 maxv NULL, NULL, NULL, NULL);
884 1.17 maxv if (error)
885 1.17 maxv return error;
886 1.17 maxv
887 1.17 maxv switch (args->op) {
888 1.17 maxv case NVMM_CTL_MACH_INFO:
889 1.17 maxv return nvmm_ctl_mach_info(args);
890 1.17 maxv default:
891 1.17 maxv return EINVAL;
892 1.17 maxv }
893 1.17 maxv }
894 1.17 maxv
895 1.17 maxv /* -------------------------------------------------------------------------- */
896 1.17 maxv
897 1.17 maxv static int
898 1.1 maxv nvmm_init(void)
899 1.1 maxv {
900 1.1 maxv size_t i, n;
901 1.1 maxv
902 1.1 maxv for (i = 0; i < __arraycount(nvmm_impl_list); i++) {
903 1.1 maxv if (!(*nvmm_impl_list[i]->ident)()) {
904 1.1 maxv continue;
905 1.1 maxv }
906 1.1 maxv nvmm_impl = nvmm_impl_list[i];
907 1.1 maxv break;
908 1.1 maxv }
909 1.1 maxv if (nvmm_impl == NULL) {
910 1.1 maxv printf("[!] No implementation found\n");
911 1.1 maxv return ENOTSUP;
912 1.1 maxv }
913 1.1 maxv
914 1.1 maxv for (i = 0; i < NVMM_MAX_MACHINES; i++) {
915 1.1 maxv machines[i].machid = i;
916 1.1 maxv rw_init(&machines[i].lock);
917 1.1 maxv for (n = 0; n < NVMM_MAX_VCPUS; n++) {
918 1.18 maxv machines[i].cpus[n].present = false;
919 1.18 maxv machines[i].cpus[n].cpuid = n;
920 1.1 maxv mutex_init(&machines[i].cpus[n].lock, MUTEX_DEFAULT,
921 1.1 maxv IPL_NONE);
922 1.1 maxv }
923 1.1 maxv }
924 1.1 maxv
925 1.1 maxv (*nvmm_impl->init)();
926 1.1 maxv
927 1.1 maxv return 0;
928 1.1 maxv }
929 1.1 maxv
930 1.1 maxv static void
931 1.1 maxv nvmm_fini(void)
932 1.1 maxv {
933 1.1 maxv size_t i, n;
934 1.1 maxv
935 1.1 maxv for (i = 0; i < NVMM_MAX_MACHINES; i++) {
936 1.1 maxv rw_destroy(&machines[i].lock);
937 1.1 maxv for (n = 0; n < NVMM_MAX_VCPUS; n++) {
938 1.1 maxv mutex_destroy(&machines[i].cpus[n].lock);
939 1.1 maxv }
940 1.1 maxv }
941 1.1 maxv
942 1.1 maxv (*nvmm_impl->fini)();
943 1.1 maxv }
944 1.1 maxv
945 1.1 maxv /* -------------------------------------------------------------------------- */
946 1.1 maxv
947 1.14 maxv static dev_type_open(nvmm_open);
948 1.14 maxv
949 1.14 maxv const struct cdevsw nvmm_cdevsw = {
950 1.14 maxv .d_open = nvmm_open,
951 1.14 maxv .d_close = noclose,
952 1.14 maxv .d_read = noread,
953 1.14 maxv .d_write = nowrite,
954 1.14 maxv .d_ioctl = noioctl,
955 1.14 maxv .d_stop = nostop,
956 1.14 maxv .d_tty = notty,
957 1.14 maxv .d_poll = nopoll,
958 1.14 maxv .d_mmap = nommap,
959 1.14 maxv .d_kqfilter = nokqfilter,
960 1.14 maxv .d_discard = nodiscard,
961 1.14 maxv .d_flag = D_OTHER | D_MPSAFE
962 1.14 maxv };
963 1.14 maxv
964 1.14 maxv static int nvmm_ioctl(file_t *, u_long, void *);
965 1.14 maxv static int nvmm_close(file_t *);
966 1.19 maxv static int nvmm_mmap(file_t *, off_t *, size_t, int, int *, int *,
967 1.19 maxv struct uvm_object **, int *);
968 1.14 maxv
969 1.14 maxv const struct fileops nvmm_fileops = {
970 1.14 maxv .fo_read = fbadop_read,
971 1.14 maxv .fo_write = fbadop_write,
972 1.14 maxv .fo_ioctl = nvmm_ioctl,
973 1.14 maxv .fo_fcntl = fnullop_fcntl,
974 1.14 maxv .fo_poll = fnullop_poll,
975 1.14 maxv .fo_stat = fbadop_stat,
976 1.14 maxv .fo_close = nvmm_close,
977 1.14 maxv .fo_kqfilter = fnullop_kqfilter,
978 1.14 maxv .fo_restart = fnullop_restart,
979 1.19 maxv .fo_mmap = nvmm_mmap,
980 1.14 maxv };
981 1.14 maxv
982 1.1 maxv static int
983 1.1 maxv nvmm_open(dev_t dev, int flags, int type, struct lwp *l)
984 1.1 maxv {
985 1.14 maxv struct nvmm_owner *owner;
986 1.14 maxv struct file *fp;
987 1.14 maxv int error, fd;
988 1.14 maxv
989 1.14 maxv if (minor(dev) != 0)
990 1.1 maxv return EXDEV;
991 1.14 maxv error = fd_allocfile(&fp, &fd);
992 1.14 maxv if (error)
993 1.14 maxv return error;
994 1.14 maxv
995 1.14 maxv owner = kmem_alloc(sizeof(*owner), KM_SLEEP);
996 1.14 maxv owner->pid = l->l_proc->p_pid;
997 1.1 maxv
998 1.14 maxv return fd_clone(fp, fd, flags, &nvmm_fileops, owner);
999 1.1 maxv }
1000 1.1 maxv
1001 1.1 maxv static int
1002 1.14 maxv nvmm_close(file_t *fp)
1003 1.1 maxv {
1004 1.14 maxv struct nvmm_owner *owner = fp->f_data;
1005 1.1 maxv
1006 1.14 maxv KASSERT(owner != NULL);
1007 1.14 maxv nvmm_kill_machines(owner);
1008 1.14 maxv kmem_free(owner, sizeof(*owner));
1009 1.14 maxv fp->f_data = NULL;
1010 1.1 maxv
1011 1.14 maxv return 0;
1012 1.1 maxv }
1013 1.1 maxv
1014 1.1 maxv static int
1015 1.19 maxv nvmm_mmap(file_t *fp, off_t *offp, size_t size, int prot, int *flagsp,
1016 1.19 maxv int *advicep, struct uvm_object **uobjp, int *maxprotp)
1017 1.19 maxv {
1018 1.19 maxv struct nvmm_owner *owner = fp->f_data;
1019 1.19 maxv struct nvmm_machine *mach;
1020 1.19 maxv nvmm_machid_t machid;
1021 1.19 maxv nvmm_cpuid_t cpuid;
1022 1.19 maxv int error;
1023 1.19 maxv
1024 1.19 maxv if (prot & PROT_EXEC)
1025 1.19 maxv return EACCES;
1026 1.19 maxv if (size != PAGE_SIZE)
1027 1.19 maxv return EINVAL;
1028 1.19 maxv
1029 1.19 maxv cpuid = NVMM_COMM_CPUID(*offp);
1030 1.19 maxv if (__predict_false(cpuid >= NVMM_MAX_VCPUS))
1031 1.19 maxv return EINVAL;
1032 1.19 maxv
1033 1.19 maxv machid = NVMM_COMM_MACHID(*offp);
1034 1.19 maxv error = nvmm_machine_get(owner, machid, &mach, false);
1035 1.19 maxv if (error)
1036 1.19 maxv return error;
1037 1.19 maxv
1038 1.19 maxv uao_reference(mach->commuobj);
1039 1.19 maxv *uobjp = mach->commuobj;
1040 1.19 maxv *offp = cpuid * PAGE_SIZE;
1041 1.19 maxv *maxprotp = prot;
1042 1.19 maxv *advicep = UVM_ADV_RANDOM;
1043 1.19 maxv
1044 1.19 maxv nvmm_machine_put(mach);
1045 1.19 maxv return 0;
1046 1.19 maxv }
1047 1.19 maxv
1048 1.19 maxv static int
1049 1.14 maxv nvmm_ioctl(file_t *fp, u_long cmd, void *data)
1050 1.1 maxv {
1051 1.14 maxv struct nvmm_owner *owner = fp->f_data;
1052 1.14 maxv
1053 1.14 maxv KASSERT(owner != NULL);
1054 1.1 maxv
1055 1.1 maxv switch (cmd) {
1056 1.1 maxv case NVMM_IOC_CAPABILITY:
1057 1.14 maxv return nvmm_capability(owner, data);
1058 1.1 maxv case NVMM_IOC_MACHINE_CREATE:
1059 1.14 maxv return nvmm_machine_create(owner, data);
1060 1.1 maxv case NVMM_IOC_MACHINE_DESTROY:
1061 1.14 maxv return nvmm_machine_destroy(owner, data);
1062 1.1 maxv case NVMM_IOC_MACHINE_CONFIGURE:
1063 1.14 maxv return nvmm_machine_configure(owner, data);
1064 1.1 maxv case NVMM_IOC_VCPU_CREATE:
1065 1.14 maxv return nvmm_vcpu_create(owner, data);
1066 1.1 maxv case NVMM_IOC_VCPU_DESTROY:
1067 1.14 maxv return nvmm_vcpu_destroy(owner, data);
1068 1.1 maxv case NVMM_IOC_VCPU_SETSTATE:
1069 1.14 maxv return nvmm_vcpu_setstate(owner, data);
1070 1.1 maxv case NVMM_IOC_VCPU_GETSTATE:
1071 1.14 maxv return nvmm_vcpu_getstate(owner, data);
1072 1.1 maxv case NVMM_IOC_VCPU_INJECT:
1073 1.14 maxv return nvmm_vcpu_inject(owner, data);
1074 1.1 maxv case NVMM_IOC_VCPU_RUN:
1075 1.14 maxv return nvmm_vcpu_run(owner, data);
1076 1.1 maxv case NVMM_IOC_GPA_MAP:
1077 1.14 maxv return nvmm_gpa_map(owner, data);
1078 1.1 maxv case NVMM_IOC_GPA_UNMAP:
1079 1.14 maxv return nvmm_gpa_unmap(owner, data);
1080 1.4 maxv case NVMM_IOC_HVA_MAP:
1081 1.14 maxv return nvmm_hva_map(owner, data);
1082 1.4 maxv case NVMM_IOC_HVA_UNMAP:
1083 1.14 maxv return nvmm_hva_unmap(owner, data);
1084 1.17 maxv case NVMM_IOC_CTL:
1085 1.17 maxv return nvmm_ctl(owner, data);
1086 1.1 maxv default:
1087 1.1 maxv return EINVAL;
1088 1.1 maxv }
1089 1.1 maxv }
1090 1.1 maxv
1091 1.14 maxv /* -------------------------------------------------------------------------- */
1092 1.1 maxv
1093 1.1 maxv void
1094 1.1 maxv nvmmattach(int nunits)
1095 1.1 maxv {
1096 1.1 maxv /* nothing */
1097 1.1 maxv }
1098 1.1 maxv
1099 1.16 maxv MODULE(MODULE_CLASS_MISC, nvmm, NULL);
1100 1.1 maxv
1101 1.1 maxv static int
1102 1.1 maxv nvmm_modcmd(modcmd_t cmd, void *arg)
1103 1.1 maxv {
1104 1.1 maxv int error;
1105 1.1 maxv
1106 1.1 maxv switch (cmd) {
1107 1.1 maxv case MODULE_CMD_INIT:
1108 1.1 maxv error = nvmm_init();
1109 1.1 maxv if (error)
1110 1.1 maxv return error;
1111 1.1 maxv
1112 1.1 maxv #if defined(_MODULE)
1113 1.1 maxv {
1114 1.1 maxv devmajor_t bmajor = NODEVMAJOR;
1115 1.1 maxv devmajor_t cmajor = 345;
1116 1.1 maxv
1117 1.1 maxv /* mknod /dev/nvmm c 345 0 */
1118 1.1 maxv error = devsw_attach("nvmm", NULL, &bmajor,
1119 1.1 maxv &nvmm_cdevsw, &cmajor);
1120 1.1 maxv if (error) {
1121 1.1 maxv nvmm_fini();
1122 1.1 maxv return error;
1123 1.1 maxv }
1124 1.1 maxv }
1125 1.1 maxv #endif
1126 1.1 maxv return 0;
1127 1.1 maxv
1128 1.1 maxv case MODULE_CMD_FINI:
1129 1.13 maxv if (nmachines > 0) {
1130 1.13 maxv return EBUSY;
1131 1.13 maxv }
1132 1.1 maxv #if defined(_MODULE)
1133 1.1 maxv {
1134 1.1 maxv error = devsw_detach(NULL, &nvmm_cdevsw);
1135 1.1 maxv if (error) {
1136 1.1 maxv return error;
1137 1.1 maxv }
1138 1.1 maxv }
1139 1.1 maxv #endif
1140 1.1 maxv nvmm_fini();
1141 1.1 maxv return 0;
1142 1.1 maxv
1143 1.13 maxv case MODULE_CMD_AUTOUNLOAD:
1144 1.13 maxv return EBUSY;
1145 1.13 maxv
1146 1.1 maxv default:
1147 1.1 maxv return ENOTTY;
1148 1.1 maxv }
1149 1.1 maxv }
1150