xref: /freebsd-13-stable/lib/libvmmapi/vmmapi.c (revision 3d497e17ebd33fe0f58d773e35ab994d750258d6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/capsicum.h>
32 #include <sys/sysctl.h>
33 #include <sys/ioctl.h>
34 #include <sys/linker.h>
35 #include <sys/mman.h>
36 #include <sys/module.h>
37 #include <sys/_iovec.h>
38 #include <sys/cpuset.h>
39 
40 #include <capsicum_helpers.h>
41 #include <errno.h>
42 #include <stdbool.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <assert.h>
46 #include <string.h>
47 #include <fcntl.h>
48 #include <unistd.h>
49 
50 #include <libutil.h>
51 
52 #include <vm/vm.h>
53 #include <machine/vmm.h>
54 #include <machine/vmm_dev.h>
55 #include <machine/vmm_snapshot.h>
56 
57 #include "vmmapi.h"
58 
59 #define	MB	(1024 * 1024UL)
60 #define	GB	(1024 * 1024 * 1024UL)
61 
62 /*
63  * Size of the guard region before and after the virtual address space
64  * mapping the guest physical memory. This must be a multiple of the
65  * superpage size for performance reasons.
66  */
67 #define	VM_MMAP_GUARD_SIZE	(4 * MB)
68 
69 #define	PROT_RW		(PROT_READ | PROT_WRITE)
70 #define	PROT_ALL	(PROT_READ | PROT_WRITE | PROT_EXEC)
71 
72 struct vmctx {
73 	int	fd;
74 	uint32_t lowmem_limit;
75 	int	memflags;
76 	size_t	lowmem;
77 	size_t	highmem;
78 	char	*baseaddr;
79 	char	*name;
80 };
81 
82 #define	CREATE(x)  sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x)))
83 #define	DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x)))
84 
85 static int
vm_device_open(const char * name)86 vm_device_open(const char *name)
87 {
88 	int fd, len;
89 	char *vmfile;
90 
91 	len = strlen("/dev/vmm/") + strlen(name) + 1;
92 	vmfile = malloc(len);
93 	assert(vmfile != NULL);
94 	snprintf(vmfile, len, "/dev/vmm/%s", name);
95 
96 	/* Open the device file */
97 	fd = open(vmfile, O_RDWR, 0);
98 
99 	free(vmfile);
100 	return (fd);
101 }
102 
103 int
vm_create(const char * name)104 vm_create(const char *name)
105 {
106 	/* Try to load vmm(4) module before creating a guest. */
107 	if (modfind("vmm") < 0)
108 		kldload("vmm");
109 	return (CREATE(name));
110 }
111 
112 struct vmctx *
vm_open(const char * name)113 vm_open(const char *name)
114 {
115 	struct vmctx *vm;
116 	int saved_errno;
117 
118 	vm = malloc(sizeof(struct vmctx) + strlen(name) + 1);
119 	assert(vm != NULL);
120 
121 	vm->fd = -1;
122 	vm->memflags = 0;
123 	vm->lowmem_limit = 3 * GB;
124 	vm->name = (char *)(vm + 1);
125 	strcpy(vm->name, name);
126 
127 	if ((vm->fd = vm_device_open(vm->name)) < 0)
128 		goto err;
129 
130 	return (vm);
131 err:
132 	saved_errno = errno;
133 	free(vm);
134 	errno = saved_errno;
135 	return (NULL);
136 }
137 
138 void
vm_close(struct vmctx * vm)139 vm_close(struct vmctx *vm)
140 {
141 	assert(vm != NULL);
142 
143 	close(vm->fd);
144 	free(vm);
145 }
146 
147 void
vm_destroy(struct vmctx * vm)148 vm_destroy(struct vmctx *vm)
149 {
150 	assert(vm != NULL);
151 
152 	if (vm->fd >= 0)
153 		close(vm->fd);
154 	DESTROY(vm->name);
155 
156 	free(vm);
157 }
158 
159 int
vm_parse_memsize(const char * opt,size_t * ret_memsize)160 vm_parse_memsize(const char *opt, size_t *ret_memsize)
161 {
162 	char *endptr;
163 	size_t optval;
164 	int error;
165 
166 	optval = strtoul(opt, &endptr, 0);
167 	if (*opt != '\0' && *endptr == '\0') {
168 		/*
169 		 * For the sake of backward compatibility if the memory size
170 		 * specified on the command line is less than a megabyte then
171 		 * it is interpreted as being in units of MB.
172 		 */
173 		if (optval < MB)
174 			optval *= MB;
175 		*ret_memsize = optval;
176 		error = 0;
177 	} else
178 		error = expand_number(opt, ret_memsize);
179 
180 	return (error);
181 }
182 
183 uint32_t
vm_get_lowmem_limit(struct vmctx * ctx)184 vm_get_lowmem_limit(struct vmctx *ctx)
185 {
186 
187 	return (ctx->lowmem_limit);
188 }
189 
190 void
vm_set_lowmem_limit(struct vmctx * ctx,uint32_t limit)191 vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit)
192 {
193 
194 	ctx->lowmem_limit = limit;
195 }
196 
197 void
vm_set_memflags(struct vmctx * ctx,int flags)198 vm_set_memflags(struct vmctx *ctx, int flags)
199 {
200 
201 	ctx->memflags = flags;
202 }
203 
204 int
vm_get_memflags(struct vmctx * ctx)205 vm_get_memflags(struct vmctx *ctx)
206 {
207 
208 	return (ctx->memflags);
209 }
210 
211 /*
212  * Map segment 'segid' starting at 'off' into guest address range [gpa,gpa+len).
213  */
214 int
vm_mmap_memseg(struct vmctx * ctx,vm_paddr_t gpa,int segid,vm_ooffset_t off,size_t len,int prot)215 vm_mmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, int segid, vm_ooffset_t off,
216     size_t len, int prot)
217 {
218 	struct vm_memmap memmap;
219 	int error, flags;
220 
221 	memmap.gpa = gpa;
222 	memmap.segid = segid;
223 	memmap.segoff = off;
224 	memmap.len = len;
225 	memmap.prot = prot;
226 	memmap.flags = 0;
227 
228 	if (ctx->memflags & VM_MEM_F_WIRED)
229 		memmap.flags |= VM_MEMMAP_F_WIRED;
230 
231 	/*
232 	 * If this mapping already exists then don't create it again. This
233 	 * is the common case for SYSMEM mappings created by bhyveload(8).
234 	 */
235 	error = vm_mmap_getnext(ctx, &gpa, &segid, &off, &len, &prot, &flags);
236 	if (error == 0 && gpa == memmap.gpa) {
237 		if (segid != memmap.segid || off != memmap.segoff ||
238 		    prot != memmap.prot || flags != memmap.flags) {
239 			errno = EEXIST;
240 			return (-1);
241 		} else {
242 			return (0);
243 		}
244 	}
245 
246 	error = ioctl(ctx->fd, VM_MMAP_MEMSEG, &memmap);
247 	return (error);
248 }
249 
250 int
vm_get_guestmem_from_ctx(struct vmctx * ctx,char ** guest_baseaddr,size_t * lowmem_size,size_t * highmem_size)251 vm_get_guestmem_from_ctx(struct vmctx *ctx, char **guest_baseaddr,
252     size_t *lowmem_size, size_t *highmem_size)
253 {
254 
255 	*guest_baseaddr = ctx->baseaddr;
256 	*lowmem_size = ctx->lowmem;
257 	*highmem_size = ctx->highmem;
258 	return (0);
259 }
260 
261 int
vm_munmap_memseg(struct vmctx * ctx,vm_paddr_t gpa,size_t len)262 vm_munmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, size_t len)
263 {
264 	struct vm_munmap munmap;
265 	int error;
266 
267 	munmap.gpa = gpa;
268 	munmap.len = len;
269 
270 	error = ioctl(ctx->fd, VM_MUNMAP_MEMSEG, &munmap);
271 	return (error);
272 }
273 
274 int
vm_mmap_getnext(struct vmctx * ctx,vm_paddr_t * gpa,int * segid,vm_ooffset_t * segoff,size_t * len,int * prot,int * flags)275 vm_mmap_getnext(struct vmctx *ctx, vm_paddr_t *gpa, int *segid,
276     vm_ooffset_t *segoff, size_t *len, int *prot, int *flags)
277 {
278 	struct vm_memmap memmap;
279 	int error;
280 
281 	bzero(&memmap, sizeof(struct vm_memmap));
282 	memmap.gpa = *gpa;
283 	error = ioctl(ctx->fd, VM_MMAP_GETNEXT, &memmap);
284 	if (error == 0) {
285 		*gpa = memmap.gpa;
286 		*segid = memmap.segid;
287 		*segoff = memmap.segoff;
288 		*len = memmap.len;
289 		*prot = memmap.prot;
290 		*flags = memmap.flags;
291 	}
292 	return (error);
293 }
294 
295 /*
296  * Return 0 if the segments are identical and non-zero otherwise.
297  *
298  * This is slightly complicated by the fact that only device memory segments
299  * are named.
300  */
301 static int
cmpseg(size_t len,const char * str,size_t len2,const char * str2)302 cmpseg(size_t len, const char *str, size_t len2, const char *str2)
303 {
304 
305 	if (len == len2) {
306 		if ((!str && !str2) || (str && str2 && !strcmp(str, str2)))
307 			return (0);
308 	}
309 	return (-1);
310 }
311 
312 static int
vm_alloc_memseg(struct vmctx * ctx,int segid,size_t len,const char * name)313 vm_alloc_memseg(struct vmctx *ctx, int segid, size_t len, const char *name)
314 {
315 	struct vm_memseg memseg;
316 	size_t n;
317 	int error;
318 
319 	/*
320 	 * If the memory segment has already been created then just return.
321 	 * This is the usual case for the SYSMEM segment created by userspace
322 	 * loaders like bhyveload(8).
323 	 */
324 	error = vm_get_memseg(ctx, segid, &memseg.len, memseg.name,
325 	    sizeof(memseg.name));
326 	if (error)
327 		return (error);
328 
329 	if (memseg.len != 0) {
330 		if (cmpseg(len, name, memseg.len, VM_MEMSEG_NAME(&memseg))) {
331 			errno = EINVAL;
332 			return (-1);
333 		} else {
334 			return (0);
335 		}
336 	}
337 
338 	bzero(&memseg, sizeof(struct vm_memseg));
339 	memseg.segid = segid;
340 	memseg.len = len;
341 	if (name != NULL) {
342 		n = strlcpy(memseg.name, name, sizeof(memseg.name));
343 		if (n >= sizeof(memseg.name)) {
344 			errno = ENAMETOOLONG;
345 			return (-1);
346 		}
347 	}
348 
349 	error = ioctl(ctx->fd, VM_ALLOC_MEMSEG, &memseg);
350 	return (error);
351 }
352 
353 int
vm_get_memseg(struct vmctx * ctx,int segid,size_t * lenp,char * namebuf,size_t bufsize)354 vm_get_memseg(struct vmctx *ctx, int segid, size_t *lenp, char *namebuf,
355     size_t bufsize)
356 {
357 	struct vm_memseg memseg;
358 	size_t n;
359 	int error;
360 
361 	memseg.segid = segid;
362 	error = ioctl(ctx->fd, VM_GET_MEMSEG, &memseg);
363 	if (error == 0) {
364 		*lenp = memseg.len;
365 		n = strlcpy(namebuf, memseg.name, bufsize);
366 		if (n >= bufsize) {
367 			errno = ENAMETOOLONG;
368 			error = -1;
369 		}
370 	}
371 	return (error);
372 }
373 
374 static int
setup_memory_segment(struct vmctx * ctx,vm_paddr_t gpa,size_t len,char * base)375 setup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char *base)
376 {
377 	char *ptr;
378 	int error, flags;
379 
380 	/* Map 'len' bytes starting at 'gpa' in the guest address space */
381 	error = vm_mmap_memseg(ctx, gpa, VM_SYSMEM, gpa, len, PROT_ALL);
382 	if (error)
383 		return (error);
384 
385 	flags = MAP_SHARED | MAP_FIXED;
386 	if ((ctx->memflags & VM_MEM_F_INCORE) == 0)
387 		flags |= MAP_NOCORE;
388 
389 	/* mmap into the process address space on the host */
390 	ptr = mmap(base + gpa, len, PROT_RW, flags, ctx->fd, gpa);
391 	if (ptr == MAP_FAILED)
392 		return (-1);
393 
394 	return (0);
395 }
396 
397 int
vm_setup_memory(struct vmctx * ctx,size_t memsize,enum vm_mmap_style vms)398 vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms)
399 {
400 	size_t objsize, len;
401 	vm_paddr_t gpa;
402 	char *baseaddr, *ptr;
403 	int error;
404 
405 	assert(vms == VM_MMAP_ALL);
406 
407 	/*
408 	 * If 'memsize' cannot fit entirely in the 'lowmem' segment then
409 	 * create another 'highmem' segment above 4GB for the remainder.
410 	 */
411 	if (memsize > ctx->lowmem_limit) {
412 		ctx->lowmem = ctx->lowmem_limit;
413 		ctx->highmem = memsize - ctx->lowmem_limit;
414 		objsize = 4*GB + ctx->highmem;
415 	} else {
416 		ctx->lowmem = memsize;
417 		ctx->highmem = 0;
418 		objsize = ctx->lowmem;
419 	}
420 
421 	error = vm_alloc_memseg(ctx, VM_SYSMEM, objsize, NULL);
422 	if (error)
423 		return (error);
424 
425 	/*
426 	 * Stake out a contiguous region covering the guest physical memory
427 	 * and the adjoining guard regions.
428 	 */
429 	len = VM_MMAP_GUARD_SIZE + objsize + VM_MMAP_GUARD_SIZE;
430 	ptr = mmap(NULL, len, PROT_NONE, MAP_GUARD | MAP_ALIGNED_SUPER, -1, 0);
431 	if (ptr == MAP_FAILED)
432 		return (-1);
433 
434 	baseaddr = ptr + VM_MMAP_GUARD_SIZE;
435 	if (ctx->highmem > 0) {
436 		gpa = 4*GB;
437 		len = ctx->highmem;
438 		error = setup_memory_segment(ctx, gpa, len, baseaddr);
439 		if (error)
440 			return (error);
441 	}
442 
443 	if (ctx->lowmem > 0) {
444 		gpa = 0;
445 		len = ctx->lowmem;
446 		error = setup_memory_segment(ctx, gpa, len, baseaddr);
447 		if (error)
448 			return (error);
449 	}
450 
451 	ctx->baseaddr = baseaddr;
452 
453 	return (0);
454 }
455 
456 /*
457  * Returns a non-NULL pointer if [gaddr, gaddr+len) is entirely contained in
458  * the lowmem or highmem regions.
459  *
460  * In particular return NULL if [gaddr, gaddr+len) falls in guest MMIO region.
461  * The instruction emulation code depends on this behavior.
462  */
463 void *
vm_map_gpa(struct vmctx * ctx,vm_paddr_t gaddr,size_t len)464 vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len)
465 {
466 
467 	if (ctx->lowmem > 0) {
468 		if (gaddr < ctx->lowmem && len <= ctx->lowmem &&
469 		    gaddr + len <= ctx->lowmem)
470 			return (ctx->baseaddr + gaddr);
471 	}
472 
473 	if (ctx->highmem > 0) {
474                 if (gaddr >= 4*GB) {
475 			if (gaddr < 4*GB + ctx->highmem &&
476 			    len <= ctx->highmem &&
477 			    gaddr + len <= 4*GB + ctx->highmem)
478 				return (ctx->baseaddr + gaddr);
479 		}
480 	}
481 
482 	return (NULL);
483 }
484 
485 vm_paddr_t
vm_rev_map_gpa(struct vmctx * ctx,void * addr)486 vm_rev_map_gpa(struct vmctx *ctx, void *addr)
487 {
488 	vm_paddr_t offaddr;
489 
490 	offaddr = (char *)addr - ctx->baseaddr;
491 
492 	if (ctx->lowmem > 0)
493 		if (offaddr <= ctx->lowmem)
494 			return (offaddr);
495 
496 	if (ctx->highmem > 0)
497 		if (offaddr >= 4*GB && offaddr < 4*GB + ctx->highmem)
498 			return (offaddr);
499 
500 	return ((vm_paddr_t)-1);
501 }
502 
503 const char *
vm_get_name(struct vmctx * ctx)504 vm_get_name(struct vmctx *ctx)
505 {
506 
507 	return (ctx->name);
508 }
509 
510 size_t
vm_get_lowmem_size(struct vmctx * ctx)511 vm_get_lowmem_size(struct vmctx *ctx)
512 {
513 
514 	return (ctx->lowmem);
515 }
516 
517 size_t
vm_get_highmem_size(struct vmctx * ctx)518 vm_get_highmem_size(struct vmctx *ctx)
519 {
520 
521 	return (ctx->highmem);
522 }
523 
524 void *
vm_create_devmem(struct vmctx * ctx,int segid,const char * name,size_t len)525 vm_create_devmem(struct vmctx *ctx, int segid, const char *name, size_t len)
526 {
527 	char pathname[MAXPATHLEN];
528 	size_t len2;
529 	char *base, *ptr;
530 	int fd, error, flags;
531 
532 	fd = -1;
533 	ptr = MAP_FAILED;
534 	if (name == NULL || strlen(name) == 0) {
535 		errno = EINVAL;
536 		goto done;
537 	}
538 
539 	error = vm_alloc_memseg(ctx, segid, len, name);
540 	if (error)
541 		goto done;
542 
543 	strlcpy(pathname, "/dev/vmm.io/", sizeof(pathname));
544 	strlcat(pathname, ctx->name, sizeof(pathname));
545 	strlcat(pathname, ".", sizeof(pathname));
546 	strlcat(pathname, name, sizeof(pathname));
547 
548 	fd = open(pathname, O_RDWR);
549 	if (fd < 0)
550 		goto done;
551 
552 	/*
553 	 * Stake out a contiguous region covering the device memory and the
554 	 * adjoining guard regions.
555 	 */
556 	len2 = VM_MMAP_GUARD_SIZE + len + VM_MMAP_GUARD_SIZE;
557 	base = mmap(NULL, len2, PROT_NONE, MAP_GUARD | MAP_ALIGNED_SUPER, -1,
558 	    0);
559 	if (base == MAP_FAILED)
560 		goto done;
561 
562 	flags = MAP_SHARED | MAP_FIXED;
563 	if ((ctx->memflags & VM_MEM_F_INCORE) == 0)
564 		flags |= MAP_NOCORE;
565 
566 	/* mmap the devmem region in the host address space */
567 	ptr = mmap(base + VM_MMAP_GUARD_SIZE, len, PROT_RW, flags, fd, 0);
568 done:
569 	if (fd >= 0)
570 		close(fd);
571 	return (ptr);
572 }
573 
574 int
vm_set_desc(struct vmctx * ctx,int vcpu,int reg,uint64_t base,uint32_t limit,uint32_t access)575 vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
576 	    uint64_t base, uint32_t limit, uint32_t access)
577 {
578 	int error;
579 	struct vm_seg_desc vmsegdesc;
580 
581 	bzero(&vmsegdesc, sizeof(vmsegdesc));
582 	vmsegdesc.cpuid = vcpu;
583 	vmsegdesc.regnum = reg;
584 	vmsegdesc.desc.base = base;
585 	vmsegdesc.desc.limit = limit;
586 	vmsegdesc.desc.access = access;
587 
588 	error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
589 	return (error);
590 }
591 
592 int
vm_get_desc(struct vmctx * ctx,int vcpu,int reg,uint64_t * base,uint32_t * limit,uint32_t * access)593 vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
594 	    uint64_t *base, uint32_t *limit, uint32_t *access)
595 {
596 	int error;
597 	struct vm_seg_desc vmsegdesc;
598 
599 	bzero(&vmsegdesc, sizeof(vmsegdesc));
600 	vmsegdesc.cpuid = vcpu;
601 	vmsegdesc.regnum = reg;
602 
603 	error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
604 	if (error == 0) {
605 		*base = vmsegdesc.desc.base;
606 		*limit = vmsegdesc.desc.limit;
607 		*access = vmsegdesc.desc.access;
608 	}
609 	return (error);
610 }
611 
612 int
vm_get_seg_desc(struct vmctx * ctx,int vcpu,int reg,struct seg_desc * seg_desc)613 vm_get_seg_desc(struct vmctx *ctx, int vcpu, int reg, struct seg_desc *seg_desc)
614 {
615 	int error;
616 
617 	error = vm_get_desc(ctx, vcpu, reg, &seg_desc->base, &seg_desc->limit,
618 	    &seg_desc->access);
619 	return (error);
620 }
621 
622 int
vm_set_register(struct vmctx * ctx,int vcpu,int reg,uint64_t val)623 vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
624 {
625 	int error;
626 	struct vm_register vmreg;
627 
628 	bzero(&vmreg, sizeof(vmreg));
629 	vmreg.cpuid = vcpu;
630 	vmreg.regnum = reg;
631 	vmreg.regval = val;
632 
633 	error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg);
634 	return (error);
635 }
636 
637 int
vm_get_register(struct vmctx * ctx,int vcpu,int reg,uint64_t * ret_val)638 vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val)
639 {
640 	int error;
641 	struct vm_register vmreg;
642 
643 	bzero(&vmreg, sizeof(vmreg));
644 	vmreg.cpuid = vcpu;
645 	vmreg.regnum = reg;
646 
647 	error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg);
648 	*ret_val = vmreg.regval;
649 	return (error);
650 }
651 
652 int
vm_set_register_set(struct vmctx * ctx,int vcpu,unsigned int count,const int * regnums,uint64_t * regvals)653 vm_set_register_set(struct vmctx *ctx, int vcpu, unsigned int count,
654     const int *regnums, uint64_t *regvals)
655 {
656 	int error;
657 	struct vm_register_set vmregset;
658 
659 	bzero(&vmregset, sizeof(vmregset));
660 	vmregset.cpuid = vcpu;
661 	vmregset.count = count;
662 	vmregset.regnums = regnums;
663 	vmregset.regvals = regvals;
664 
665 	error = ioctl(ctx->fd, VM_SET_REGISTER_SET, &vmregset);
666 	return (error);
667 }
668 
669 int
vm_get_register_set(struct vmctx * ctx,int vcpu,unsigned int count,const int * regnums,uint64_t * regvals)670 vm_get_register_set(struct vmctx *ctx, int vcpu, unsigned int count,
671     const int *regnums, uint64_t *regvals)
672 {
673 	int error;
674 	struct vm_register_set vmregset;
675 
676 	bzero(&vmregset, sizeof(vmregset));
677 	vmregset.cpuid = vcpu;
678 	vmregset.count = count;
679 	vmregset.regnums = regnums;
680 	vmregset.regvals = regvals;
681 
682 	error = ioctl(ctx->fd, VM_GET_REGISTER_SET, &vmregset);
683 	return (error);
684 }
685 
686 int
vm_run(struct vmctx * ctx,int vcpu,struct vm_exit * vmexit)687 vm_run(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit)
688 {
689 	int error;
690 	struct vm_run vmrun;
691 
692 	bzero(&vmrun, sizeof(vmrun));
693 	vmrun.cpuid = vcpu;
694 
695 	error = ioctl(ctx->fd, VM_RUN, &vmrun);
696 	bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit));
697 	return (error);
698 }
699 
700 int
vm_suspend(struct vmctx * ctx,enum vm_suspend_how how)701 vm_suspend(struct vmctx *ctx, enum vm_suspend_how how)
702 {
703 	struct vm_suspend vmsuspend;
704 
705 	bzero(&vmsuspend, sizeof(vmsuspend));
706 	vmsuspend.how = how;
707 	return (ioctl(ctx->fd, VM_SUSPEND, &vmsuspend));
708 }
709 
710 int
vm_reinit(struct vmctx * ctx)711 vm_reinit(struct vmctx *ctx)
712 {
713 
714 	return (ioctl(ctx->fd, VM_REINIT, 0));
715 }
716 
717 int
vm_inject_exception(struct vmctx * ctx,int vcpu,int vector,int errcode_valid,uint32_t errcode,int restart_instruction)718 vm_inject_exception(struct vmctx *ctx, int vcpu, int vector, int errcode_valid,
719     uint32_t errcode, int restart_instruction)
720 {
721 	struct vm_exception exc;
722 
723 	exc.cpuid = vcpu;
724 	exc.vector = vector;
725 	exc.error_code = errcode;
726 	exc.error_code_valid = errcode_valid;
727 	exc.restart_instruction = restart_instruction;
728 
729 	return (ioctl(ctx->fd, VM_INJECT_EXCEPTION, &exc));
730 }
731 
732 int
vm_apicid2vcpu(struct vmctx * ctx __unused,int apicid)733 vm_apicid2vcpu(struct vmctx *ctx __unused, int apicid)
734 {
735 	/*
736 	 * The apic id associated with the 'vcpu' has the same numerical value
737 	 * as the 'vcpu' itself.
738 	 */
739 	return (apicid);
740 }
741 
742 int
vm_lapic_irq(struct vmctx * ctx,int vcpu,int vector)743 vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
744 {
745 	struct vm_lapic_irq vmirq;
746 
747 	bzero(&vmirq, sizeof(vmirq));
748 	vmirq.cpuid = vcpu;
749 	vmirq.vector = vector;
750 
751 	return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
752 }
753 
754 int
vm_lapic_local_irq(struct vmctx * ctx,int vcpu,int vector)755 vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector)
756 {
757 	struct vm_lapic_irq vmirq;
758 
759 	bzero(&vmirq, sizeof(vmirq));
760 	vmirq.cpuid = vcpu;
761 	vmirq.vector = vector;
762 
763 	return (ioctl(ctx->fd, VM_LAPIC_LOCAL_IRQ, &vmirq));
764 }
765 
766 int
vm_lapic_msi(struct vmctx * ctx,uint64_t addr,uint64_t msg)767 vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg)
768 {
769 	struct vm_lapic_msi vmmsi;
770 
771 	bzero(&vmmsi, sizeof(vmmsi));
772 	vmmsi.addr = addr;
773 	vmmsi.msg = msg;
774 
775 	return (ioctl(ctx->fd, VM_LAPIC_MSI, &vmmsi));
776 }
777 
778 int
vm_ioapic_assert_irq(struct vmctx * ctx,int irq)779 vm_ioapic_assert_irq(struct vmctx *ctx, int irq)
780 {
781 	struct vm_ioapic_irq ioapic_irq;
782 
783 	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
784 	ioapic_irq.irq = irq;
785 
786 	return (ioctl(ctx->fd, VM_IOAPIC_ASSERT_IRQ, &ioapic_irq));
787 }
788 
789 int
vm_ioapic_deassert_irq(struct vmctx * ctx,int irq)790 vm_ioapic_deassert_irq(struct vmctx *ctx, int irq)
791 {
792 	struct vm_ioapic_irq ioapic_irq;
793 
794 	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
795 	ioapic_irq.irq = irq;
796 
797 	return (ioctl(ctx->fd, VM_IOAPIC_DEASSERT_IRQ, &ioapic_irq));
798 }
799 
800 int
vm_ioapic_pulse_irq(struct vmctx * ctx,int irq)801 vm_ioapic_pulse_irq(struct vmctx *ctx, int irq)
802 {
803 	struct vm_ioapic_irq ioapic_irq;
804 
805 	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
806 	ioapic_irq.irq = irq;
807 
808 	return (ioctl(ctx->fd, VM_IOAPIC_PULSE_IRQ, &ioapic_irq));
809 }
810 
811 int
vm_ioapic_pincount(struct vmctx * ctx,int * pincount)812 vm_ioapic_pincount(struct vmctx *ctx, int *pincount)
813 {
814 
815 	return (ioctl(ctx->fd, VM_IOAPIC_PINCOUNT, pincount));
816 }
817 
818 int
vm_readwrite_kernemu_device(struct vmctx * ctx,int vcpu,vm_paddr_t gpa,bool write,int size,uint64_t * value)819 vm_readwrite_kernemu_device(struct vmctx *ctx, int vcpu, vm_paddr_t gpa,
820     bool write, int size, uint64_t *value)
821 {
822 	struct vm_readwrite_kernemu_device irp = {
823 		.vcpuid = vcpu,
824 		.access_width = fls(size) - 1,
825 		.gpa = gpa,
826 		.value = write ? *value : ~0ul,
827 	};
828 	long cmd = (write ? VM_SET_KERNEMU_DEV : VM_GET_KERNEMU_DEV);
829 	int rc;
830 
831 	rc = ioctl(ctx->fd, cmd, &irp);
832 	if (rc == 0 && !write)
833 		*value = irp.value;
834 	return (rc);
835 }
836 
837 int
vm_isa_assert_irq(struct vmctx * ctx,int atpic_irq,int ioapic_irq)838 vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
839 {
840 	struct vm_isa_irq isa_irq;
841 
842 	bzero(&isa_irq, sizeof(struct vm_isa_irq));
843 	isa_irq.atpic_irq = atpic_irq;
844 	isa_irq.ioapic_irq = ioapic_irq;
845 
846 	return (ioctl(ctx->fd, VM_ISA_ASSERT_IRQ, &isa_irq));
847 }
848 
849 int
vm_isa_deassert_irq(struct vmctx * ctx,int atpic_irq,int ioapic_irq)850 vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
851 {
852 	struct vm_isa_irq isa_irq;
853 
854 	bzero(&isa_irq, sizeof(struct vm_isa_irq));
855 	isa_irq.atpic_irq = atpic_irq;
856 	isa_irq.ioapic_irq = ioapic_irq;
857 
858 	return (ioctl(ctx->fd, VM_ISA_DEASSERT_IRQ, &isa_irq));
859 }
860 
861 int
vm_isa_pulse_irq(struct vmctx * ctx,int atpic_irq,int ioapic_irq)862 vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
863 {
864 	struct vm_isa_irq isa_irq;
865 
866 	bzero(&isa_irq, sizeof(struct vm_isa_irq));
867 	isa_irq.atpic_irq = atpic_irq;
868 	isa_irq.ioapic_irq = ioapic_irq;
869 
870 	return (ioctl(ctx->fd, VM_ISA_PULSE_IRQ, &isa_irq));
871 }
872 
873 int
vm_isa_set_irq_trigger(struct vmctx * ctx,int atpic_irq,enum vm_intr_trigger trigger)874 vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
875     enum vm_intr_trigger trigger)
876 {
877 	struct vm_isa_irq_trigger isa_irq_trigger;
878 
879 	bzero(&isa_irq_trigger, sizeof(struct vm_isa_irq_trigger));
880 	isa_irq_trigger.atpic_irq = atpic_irq;
881 	isa_irq_trigger.trigger = trigger;
882 
883 	return (ioctl(ctx->fd, VM_ISA_SET_IRQ_TRIGGER, &isa_irq_trigger));
884 }
885 
886 int
vm_inject_nmi(struct vmctx * ctx,int vcpu)887 vm_inject_nmi(struct vmctx *ctx, int vcpu)
888 {
889 	struct vm_nmi vmnmi;
890 
891 	bzero(&vmnmi, sizeof(vmnmi));
892 	vmnmi.cpuid = vcpu;
893 
894 	return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
895 }
896 
897 static const char *capstrmap[] = {
898 	[VM_CAP_HALT_EXIT]  = "hlt_exit",
899 	[VM_CAP_MTRAP_EXIT] = "mtrap_exit",
900 	[VM_CAP_PAUSE_EXIT] = "pause_exit",
901 	[VM_CAP_UNRESTRICTED_GUEST] = "unrestricted_guest",
902 	[VM_CAP_ENABLE_INVPCID] = "enable_invpcid",
903 	[VM_CAP_BPT_EXIT] = "bpt_exit",
904 };
905 
906 int
vm_capability_name2type(const char * capname)907 vm_capability_name2type(const char *capname)
908 {
909 	int i;
910 
911 	for (i = 0; i < (int)nitems(capstrmap); i++) {
912 		if (strcmp(capstrmap[i], capname) == 0)
913 			return (i);
914 	}
915 
916 	return (-1);
917 }
918 
919 const char *
vm_capability_type2name(int type)920 vm_capability_type2name(int type)
921 {
922 	if (type >= 0 && type < (int)nitems(capstrmap))
923 		return (capstrmap[type]);
924 
925 	return (NULL);
926 }
927 
928 int
vm_get_capability(struct vmctx * ctx,int vcpu,enum vm_cap_type cap,int * retval)929 vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
930 		  int *retval)
931 {
932 	int error;
933 	struct vm_capability vmcap;
934 
935 	bzero(&vmcap, sizeof(vmcap));
936 	vmcap.cpuid = vcpu;
937 	vmcap.captype = cap;
938 
939 	error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
940 	*retval = vmcap.capval;
941 	return (error);
942 }
943 
944 int
vm_set_capability(struct vmctx * ctx,int vcpu,enum vm_cap_type cap,int val)945 vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
946 {
947 	struct vm_capability vmcap;
948 
949 	bzero(&vmcap, sizeof(vmcap));
950 	vmcap.cpuid = vcpu;
951 	vmcap.captype = cap;
952 	vmcap.capval = val;
953 
954 	return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
955 }
956 
957 int
vm_assign_pptdev(struct vmctx * ctx,int bus,int slot,int func)958 vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
959 {
960 	struct vm_pptdev pptdev;
961 
962 	bzero(&pptdev, sizeof(pptdev));
963 	pptdev.bus = bus;
964 	pptdev.slot = slot;
965 	pptdev.func = func;
966 
967 	return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
968 }
969 
970 int
vm_unassign_pptdev(struct vmctx * ctx,int bus,int slot,int func)971 vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
972 {
973 	struct vm_pptdev pptdev;
974 
975 	bzero(&pptdev, sizeof(pptdev));
976 	pptdev.bus = bus;
977 	pptdev.slot = slot;
978 	pptdev.func = func;
979 
980 	return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
981 }
982 
983 int
vm_map_pptdev_mmio(struct vmctx * ctx,int bus,int slot,int func,vm_paddr_t gpa,size_t len,vm_paddr_t hpa)984 vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
985 		   vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
986 {
987 	struct vm_pptdev_mmio pptmmio;
988 
989 	bzero(&pptmmio, sizeof(pptmmio));
990 	pptmmio.bus = bus;
991 	pptmmio.slot = slot;
992 	pptmmio.func = func;
993 	pptmmio.gpa = gpa;
994 	pptmmio.len = len;
995 	pptmmio.hpa = hpa;
996 
997 	return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
998 }
999 
1000 int
vm_unmap_pptdev_mmio(struct vmctx * ctx,int bus,int slot,int func,vm_paddr_t gpa,size_t len)1001 vm_unmap_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
1002 		     vm_paddr_t gpa, size_t len)
1003 {
1004 	struct vm_pptdev_mmio pptmmio;
1005 
1006 	bzero(&pptmmio, sizeof(pptmmio));
1007 	pptmmio.bus = bus;
1008 	pptmmio.slot = slot;
1009 	pptmmio.func = func;
1010 	pptmmio.gpa = gpa;
1011 	pptmmio.len = len;
1012 
1013 	return (ioctl(ctx->fd, VM_UNMAP_PPTDEV_MMIO, &pptmmio));
1014 }
1015 
1016 int
vm_setup_pptdev_msi(struct vmctx * ctx,int vcpu,int bus,int slot,int func,uint64_t addr,uint64_t msg,int numvec)1017 vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
1018     uint64_t addr, uint64_t msg, int numvec)
1019 {
1020 	struct vm_pptdev_msi pptmsi;
1021 
1022 	bzero(&pptmsi, sizeof(pptmsi));
1023 	pptmsi.vcpu = vcpu;
1024 	pptmsi.bus = bus;
1025 	pptmsi.slot = slot;
1026 	pptmsi.func = func;
1027 	pptmsi.msg = msg;
1028 	pptmsi.addr = addr;
1029 	pptmsi.numvec = numvec;
1030 
1031 	return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
1032 }
1033 
1034 int
vm_setup_pptdev_msix(struct vmctx * ctx,int vcpu,int bus,int slot,int func,int idx,uint64_t addr,uint64_t msg,uint32_t vector_control)1035 vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
1036     int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
1037 {
1038 	struct vm_pptdev_msix pptmsix;
1039 
1040 	bzero(&pptmsix, sizeof(pptmsix));
1041 	pptmsix.vcpu = vcpu;
1042 	pptmsix.bus = bus;
1043 	pptmsix.slot = slot;
1044 	pptmsix.func = func;
1045 	pptmsix.idx = idx;
1046 	pptmsix.msg = msg;
1047 	pptmsix.addr = addr;
1048 	pptmsix.vector_control = vector_control;
1049 
1050 	return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
1051 }
1052 
1053 int
vm_disable_pptdev_msix(struct vmctx * ctx,int bus,int slot,int func)1054 vm_disable_pptdev_msix(struct vmctx *ctx, int bus, int slot, int func)
1055 {
1056 	struct vm_pptdev ppt;
1057 
1058 	bzero(&ppt, sizeof(ppt));
1059 	ppt.bus = bus;
1060 	ppt.slot = slot;
1061 	ppt.func = func;
1062 
1063 	return ioctl(ctx->fd, VM_PPTDEV_DISABLE_MSIX, &ppt);
1064 }
1065 
1066 uint64_t *
vm_get_stats(struct vmctx * ctx,int vcpu,struct timeval * ret_tv,int * ret_entries)1067 vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
1068 	     int *ret_entries)
1069 {
1070 	static _Thread_local uint64_t *stats_buf;
1071 	static _Thread_local u_int stats_count;
1072 	uint64_t *new_stats;
1073 	struct vm_stats vmstats;
1074 	u_int count, index;
1075 	bool have_stats;
1076 
1077 	have_stats = false;
1078 	vmstats.cpuid = vcpu;
1079 	count = 0;
1080 	for (index = 0;; index += nitems(vmstats.statbuf)) {
1081 		vmstats.index = index;
1082 		if (ioctl(ctx->fd, VM_STATS, &vmstats) != 0)
1083 			break;
1084 		if (stats_count < index + vmstats.num_entries) {
1085 			new_stats = realloc(stats_buf,
1086 			    (index + vmstats.num_entries) * sizeof(uint64_t));
1087 			if (new_stats == NULL) {
1088 				errno = ENOMEM;
1089 				return (NULL);
1090 			}
1091 			stats_count = index + vmstats.num_entries;
1092 			stats_buf = new_stats;
1093 		}
1094 		memcpy(stats_buf + index, vmstats.statbuf,
1095 		    vmstats.num_entries * sizeof(uint64_t));
1096 		count += vmstats.num_entries;
1097 		have_stats = true;
1098 
1099 		if (vmstats.num_entries != nitems(vmstats.statbuf))
1100 			break;
1101 	}
1102 	if (have_stats) {
1103 		if (ret_entries)
1104 			*ret_entries = count;
1105 		if (ret_tv)
1106 			*ret_tv = vmstats.tv;
1107 		return (stats_buf);
1108 	} else
1109 		return (NULL);
1110 }
1111 
1112 const char *
vm_get_stat_desc(struct vmctx * ctx,int index)1113 vm_get_stat_desc(struct vmctx *ctx, int index)
1114 {
1115 	static struct vm_stat_desc statdesc;
1116 
1117 	statdesc.index = index;
1118 	if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
1119 		return (statdesc.desc);
1120 	else
1121 		return (NULL);
1122 }
1123 
1124 int
vm_get_x2apic_state(struct vmctx * ctx,int vcpu,enum x2apic_state * state)1125 vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state)
1126 {
1127 	int error;
1128 	struct vm_x2apic x2apic;
1129 
1130 	bzero(&x2apic, sizeof(x2apic));
1131 	x2apic.cpuid = vcpu;
1132 
1133 	error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic);
1134 	*state = x2apic.state;
1135 	return (error);
1136 }
1137 
1138 int
vm_set_x2apic_state(struct vmctx * ctx,int vcpu,enum x2apic_state state)1139 vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state)
1140 {
1141 	int error;
1142 	struct vm_x2apic x2apic;
1143 
1144 	bzero(&x2apic, sizeof(x2apic));
1145 	x2apic.cpuid = vcpu;
1146 	x2apic.state = state;
1147 
1148 	error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic);
1149 
1150 	return (error);
1151 }
1152 
1153 /*
1154  * From Intel Vol 3a:
1155  * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
1156  */
1157 int
vcpu_reset(struct vmctx * vmctx,int vcpu)1158 vcpu_reset(struct vmctx *vmctx, int vcpu)
1159 {
1160 	int error;
1161 	uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
1162 	uint32_t desc_access, desc_limit;
1163 	uint16_t sel;
1164 
1165 	zero = 0;
1166 
1167 	rflags = 0x2;
1168 	error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
1169 	if (error)
1170 		goto done;
1171 
1172 	rip = 0xfff0;
1173 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
1174 		goto done;
1175 
1176 	/*
1177 	 * According to Intels Software Developer Manual CR0 should be
1178 	 * initialized with CR0_ET | CR0_NW | CR0_CD but that crashes some
1179 	 * guests like Windows.
1180 	 */
1181 	cr0 = CR0_NE;
1182 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
1183 		goto done;
1184 
1185 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR2, zero)) != 0)
1186 		goto done;
1187 
1188 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
1189 		goto done;
1190 
1191 	cr4 = 0;
1192 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
1193 		goto done;
1194 
1195 	/*
1196 	 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
1197 	 */
1198 	desc_base = 0xffff0000;
1199 	desc_limit = 0xffff;
1200 	desc_access = 0x0093;
1201 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
1202 			    desc_base, desc_limit, desc_access);
1203 	if (error)
1204 		goto done;
1205 
1206 	sel = 0xf000;
1207 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
1208 		goto done;
1209 
1210 	/*
1211 	 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
1212 	 */
1213 	desc_base = 0;
1214 	desc_limit = 0xffff;
1215 	desc_access = 0x0093;
1216 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
1217 			    desc_base, desc_limit, desc_access);
1218 	if (error)
1219 		goto done;
1220 
1221 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
1222 			    desc_base, desc_limit, desc_access);
1223 	if (error)
1224 		goto done;
1225 
1226 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
1227 			    desc_base, desc_limit, desc_access);
1228 	if (error)
1229 		goto done;
1230 
1231 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
1232 			    desc_base, desc_limit, desc_access);
1233 	if (error)
1234 		goto done;
1235 
1236 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
1237 			    desc_base, desc_limit, desc_access);
1238 	if (error)
1239 		goto done;
1240 
1241 	sel = 0;
1242 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
1243 		goto done;
1244 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
1245 		goto done;
1246 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
1247 		goto done;
1248 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
1249 		goto done;
1250 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
1251 		goto done;
1252 
1253 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_EFER, zero)) != 0)
1254 		goto done;
1255 
1256 	/* General purpose registers */
1257 	rdx = 0xf00;
1258 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
1259 		goto done;
1260 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
1261 		goto done;
1262 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
1263 		goto done;
1264 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
1265 		goto done;
1266 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
1267 		goto done;
1268 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
1269 		goto done;
1270 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
1271 		goto done;
1272 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
1273 		goto done;
1274 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_R8, zero)) != 0)
1275 		goto done;
1276 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_R9, zero)) != 0)
1277 		goto done;
1278 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_R10, zero)) != 0)
1279 		goto done;
1280 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_R11, zero)) != 0)
1281 		goto done;
1282 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_R12, zero)) != 0)
1283 		goto done;
1284 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_R13, zero)) != 0)
1285 		goto done;
1286 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_R14, zero)) != 0)
1287 		goto done;
1288 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_R15, zero)) != 0)
1289 		goto done;
1290 
1291 	/* GDTR, IDTR */
1292 	desc_base = 0;
1293 	desc_limit = 0xffff;
1294 	desc_access = 0;
1295 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
1296 			    desc_base, desc_limit, desc_access);
1297 	if (error != 0)
1298 		goto done;
1299 
1300 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
1301 			    desc_base, desc_limit, desc_access);
1302 	if (error != 0)
1303 		goto done;
1304 
1305 	/* TR */
1306 	desc_base = 0;
1307 	desc_limit = 0xffff;
1308 	desc_access = 0x0000008b;
1309 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
1310 	if (error)
1311 		goto done;
1312 
1313 	sel = 0;
1314 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
1315 		goto done;
1316 
1317 	/* LDTR */
1318 	desc_base = 0;
1319 	desc_limit = 0xffff;
1320 	desc_access = 0x00000082;
1321 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
1322 			    desc_limit, desc_access);
1323 	if (error)
1324 		goto done;
1325 
1326 	sel = 0;
1327 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
1328 		goto done;
1329 
1330 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DR6,
1331 		 0xffff0ff0)) != 0)
1332 		goto done;
1333 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DR7, 0x400)) !=
1334 	    0)
1335 		goto done;
1336 
1337 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_INTR_SHADOW,
1338 		 zero)) != 0)
1339 		goto done;
1340 
1341 	error = 0;
1342 done:
1343 	return (error);
1344 }
1345 
1346 int
vm_get_gpa_pmap(struct vmctx * ctx,uint64_t gpa,uint64_t * pte,int * num)1347 vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num)
1348 {
1349 	int error, i;
1350 	struct vm_gpa_pte gpapte;
1351 
1352 	bzero(&gpapte, sizeof(gpapte));
1353 	gpapte.gpa = gpa;
1354 
1355 	error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte);
1356 
1357 	if (error == 0) {
1358 		*num = gpapte.ptenum;
1359 		for (i = 0; i < gpapte.ptenum; i++)
1360 			pte[i] = gpapte.pte[i];
1361 	}
1362 
1363 	return (error);
1364 }
1365 
1366 int
vm_get_hpet_capabilities(struct vmctx * ctx,uint32_t * capabilities)1367 vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities)
1368 {
1369 	int error;
1370 	struct vm_hpet_cap cap;
1371 
1372 	bzero(&cap, sizeof(struct vm_hpet_cap));
1373 	error = ioctl(ctx->fd, VM_GET_HPET_CAPABILITIES, &cap);
1374 	if (capabilities != NULL)
1375 		*capabilities = cap.capabilities;
1376 	return (error);
1377 }
1378 
1379 int
vm_gla2gpa(struct vmctx * ctx,int vcpu,struct vm_guest_paging * paging,uint64_t gla,int prot,uint64_t * gpa,int * fault)1380 vm_gla2gpa(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
1381     uint64_t gla, int prot, uint64_t *gpa, int *fault)
1382 {
1383 	struct vm_gla2gpa gg;
1384 	int error;
1385 
1386 	bzero(&gg, sizeof(struct vm_gla2gpa));
1387 	gg.vcpuid = vcpu;
1388 	gg.prot = prot;
1389 	gg.gla = gla;
1390 	gg.paging = *paging;
1391 
1392 	error = ioctl(ctx->fd, VM_GLA2GPA, &gg);
1393 	if (error == 0) {
1394 		*fault = gg.fault;
1395 		*gpa = gg.gpa;
1396 	}
1397 	return (error);
1398 }
1399 
1400 int
vm_gla2gpa_nofault(struct vmctx * ctx,int vcpu,struct vm_guest_paging * paging,uint64_t gla,int prot,uint64_t * gpa,int * fault)1401 vm_gla2gpa_nofault(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
1402     uint64_t gla, int prot, uint64_t *gpa, int *fault)
1403 {
1404 	struct vm_gla2gpa gg;
1405 	int error;
1406 
1407 	bzero(&gg, sizeof(struct vm_gla2gpa));
1408 	gg.vcpuid = vcpu;
1409 	gg.prot = prot;
1410 	gg.gla = gla;
1411 	gg.paging = *paging;
1412 
1413 	error = ioctl(ctx->fd, VM_GLA2GPA_NOFAULT, &gg);
1414 	if (error == 0) {
1415 		*fault = gg.fault;
1416 		*gpa = gg.gpa;
1417 	}
1418 	return (error);
1419 }
1420 
1421 #ifndef min
1422 #define	min(a,b)	(((a) < (b)) ? (a) : (b))
1423 #endif
1424 
1425 int
vm_copy_setup(struct vmctx * ctx,int vcpu,struct vm_guest_paging * paging,uint64_t gla,size_t len,int prot,struct iovec * iov,int iovcnt,int * fault)1426 vm_copy_setup(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
1427     uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt,
1428     int *fault)
1429 {
1430 	void *va;
1431 	uint64_t gpa, off;
1432 	int error, i, n;
1433 
1434 	for (i = 0; i < iovcnt; i++) {
1435 		iov[i].iov_base = 0;
1436 		iov[i].iov_len = 0;
1437 	}
1438 
1439 	while (len) {
1440 		assert(iovcnt > 0);
1441 		error = vm_gla2gpa(ctx, vcpu, paging, gla, prot, &gpa, fault);
1442 		if (error || *fault)
1443 			return (error);
1444 
1445 		off = gpa & PAGE_MASK;
1446 		n = MIN(len, PAGE_SIZE - off);
1447 
1448 		va = vm_map_gpa(ctx, gpa, n);
1449 		if (va == NULL)
1450 			return (EFAULT);
1451 
1452 		iov->iov_base = va;
1453 		iov->iov_len = n;
1454 		iov++;
1455 		iovcnt--;
1456 
1457 		gla += n;
1458 		len -= n;
1459 	}
1460 	return (0);
1461 }
1462 
1463 void
vm_copy_teardown(struct iovec * iov __unused,int iovcnt __unused)1464 vm_copy_teardown(struct iovec *iov __unused, int iovcnt __unused)
1465 {
1466 	/*
1467 	 * Intentionally empty.  This is used by the instruction
1468 	 * emulation code shared with the kernel.  The in-kernel
1469 	 * version of this is non-empty.
1470 	 */
1471 }
1472 
1473 void
vm_copyin(struct iovec * iov,void * vp,size_t len)1474 vm_copyin(struct iovec *iov, void *vp, size_t len)
1475 {
1476 	const char *src;
1477 	char *dst;
1478 	size_t n;
1479 
1480 	dst = vp;
1481 	while (len) {
1482 		assert(iov->iov_len);
1483 		n = min(len, iov->iov_len);
1484 		src = iov->iov_base;
1485 		bcopy(src, dst, n);
1486 
1487 		iov++;
1488 		dst += n;
1489 		len -= n;
1490 	}
1491 }
1492 
1493 void
vm_copyout(const void * vp,struct iovec * iov,size_t len)1494 vm_copyout(const void *vp, struct iovec *iov, size_t len)
1495 {
1496 	const char *src;
1497 	char *dst;
1498 	size_t n;
1499 
1500 	src = vp;
1501 	while (len) {
1502 		assert(iov->iov_len);
1503 		n = min(len, iov->iov_len);
1504 		dst = iov->iov_base;
1505 		bcopy(src, dst, n);
1506 
1507 		iov++;
1508 		src += n;
1509 		len -= n;
1510 	}
1511 }
1512 
1513 static int
vm_get_cpus(struct vmctx * ctx,int which,cpuset_t * cpus)1514 vm_get_cpus(struct vmctx *ctx, int which, cpuset_t *cpus)
1515 {
1516 	struct vm_cpuset vm_cpuset;
1517 	int error;
1518 
1519 	bzero(&vm_cpuset, sizeof(struct vm_cpuset));
1520 	vm_cpuset.which = which;
1521 	vm_cpuset.cpusetsize = sizeof(cpuset_t);
1522 	vm_cpuset.cpus = cpus;
1523 
1524 	error = ioctl(ctx->fd, VM_GET_CPUS, &vm_cpuset);
1525 	return (error);
1526 }
1527 
1528 int
vm_active_cpus(struct vmctx * ctx,cpuset_t * cpus)1529 vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus)
1530 {
1531 
1532 	return (vm_get_cpus(ctx, VM_ACTIVE_CPUS, cpus));
1533 }
1534 
1535 int
vm_suspended_cpus(struct vmctx * ctx,cpuset_t * cpus)1536 vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus)
1537 {
1538 
1539 	return (vm_get_cpus(ctx, VM_SUSPENDED_CPUS, cpus));
1540 }
1541 
1542 int
vm_debug_cpus(struct vmctx * ctx,cpuset_t * cpus)1543 vm_debug_cpus(struct vmctx *ctx, cpuset_t *cpus)
1544 {
1545 
1546 	return (vm_get_cpus(ctx, VM_DEBUG_CPUS, cpus));
1547 }
1548 
1549 int
vm_activate_cpu(struct vmctx * ctx,int vcpu)1550 vm_activate_cpu(struct vmctx *ctx, int vcpu)
1551 {
1552 	struct vm_activate_cpu ac;
1553 	int error;
1554 
1555 	bzero(&ac, sizeof(struct vm_activate_cpu));
1556 	ac.vcpuid = vcpu;
1557 	error = ioctl(ctx->fd, VM_ACTIVATE_CPU, &ac);
1558 	return (error);
1559 }
1560 
1561 int
vm_suspend_cpu(struct vmctx * ctx,int vcpu)1562 vm_suspend_cpu(struct vmctx *ctx, int vcpu)
1563 {
1564 	struct vm_activate_cpu ac;
1565 	int error;
1566 
1567 	bzero(&ac, sizeof(struct vm_activate_cpu));
1568 	ac.vcpuid = vcpu;
1569 	error = ioctl(ctx->fd, VM_SUSPEND_CPU, &ac);
1570 	return (error);
1571 }
1572 
1573 int
vm_resume_cpu(struct vmctx * ctx,int vcpu)1574 vm_resume_cpu(struct vmctx *ctx, int vcpu)
1575 {
1576 	struct vm_activate_cpu ac;
1577 	int error;
1578 
1579 	bzero(&ac, sizeof(struct vm_activate_cpu));
1580 	ac.vcpuid = vcpu;
1581 	error = ioctl(ctx->fd, VM_RESUME_CPU, &ac);
1582 	return (error);
1583 }
1584 
1585 int
vm_get_intinfo(struct vmctx * ctx,int vcpu,uint64_t * info1,uint64_t * info2)1586 vm_get_intinfo(struct vmctx *ctx, int vcpu, uint64_t *info1, uint64_t *info2)
1587 {
1588 	struct vm_intinfo vmii;
1589 	int error;
1590 
1591 	bzero(&vmii, sizeof(struct vm_intinfo));
1592 	vmii.vcpuid = vcpu;
1593 	error = ioctl(ctx->fd, VM_GET_INTINFO, &vmii);
1594 	if (error == 0) {
1595 		*info1 = vmii.info1;
1596 		*info2 = vmii.info2;
1597 	}
1598 	return (error);
1599 }
1600 
1601 int
vm_set_intinfo(struct vmctx * ctx,int vcpu,uint64_t info1)1602 vm_set_intinfo(struct vmctx *ctx, int vcpu, uint64_t info1)
1603 {
1604 	struct vm_intinfo vmii;
1605 	int error;
1606 
1607 	bzero(&vmii, sizeof(struct vm_intinfo));
1608 	vmii.vcpuid = vcpu;
1609 	vmii.info1 = info1;
1610 	error = ioctl(ctx->fd, VM_SET_INTINFO, &vmii);
1611 	return (error);
1612 }
1613 
1614 int
vm_rtc_write(struct vmctx * ctx,int offset,uint8_t value)1615 vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value)
1616 {
1617 	struct vm_rtc_data rtcdata;
1618 	int error;
1619 
1620 	bzero(&rtcdata, sizeof(struct vm_rtc_data));
1621 	rtcdata.offset = offset;
1622 	rtcdata.value = value;
1623 	error = ioctl(ctx->fd, VM_RTC_WRITE, &rtcdata);
1624 	return (error);
1625 }
1626 
1627 int
vm_rtc_read(struct vmctx * ctx,int offset,uint8_t * retval)1628 vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval)
1629 {
1630 	struct vm_rtc_data rtcdata;
1631 	int error;
1632 
1633 	bzero(&rtcdata, sizeof(struct vm_rtc_data));
1634 	rtcdata.offset = offset;
1635 	error = ioctl(ctx->fd, VM_RTC_READ, &rtcdata);
1636 	if (error == 0)
1637 		*retval = rtcdata.value;
1638 	return (error);
1639 }
1640 
1641 int
vm_rtc_settime(struct vmctx * ctx,time_t secs)1642 vm_rtc_settime(struct vmctx *ctx, time_t secs)
1643 {
1644 	struct vm_rtc_time rtctime;
1645 	int error;
1646 
1647 	bzero(&rtctime, sizeof(struct vm_rtc_time));
1648 	rtctime.secs = secs;
1649 	error = ioctl(ctx->fd, VM_RTC_SETTIME, &rtctime);
1650 	return (error);
1651 }
1652 
1653 int
vm_rtc_gettime(struct vmctx * ctx,time_t * secs)1654 vm_rtc_gettime(struct vmctx *ctx, time_t *secs)
1655 {
1656 	struct vm_rtc_time rtctime;
1657 	int error;
1658 
1659 	bzero(&rtctime, sizeof(struct vm_rtc_time));
1660 	error = ioctl(ctx->fd, VM_RTC_GETTIME, &rtctime);
1661 	if (error == 0)
1662 		*secs = rtctime.secs;
1663 	return (error);
1664 }
1665 
1666 int
vm_restart_instruction(struct vmctx * ctx,int vcpu)1667 vm_restart_instruction(struct vmctx *ctx, int vcpu)
1668 {
1669 
1670 	return (ioctl(ctx->fd, VM_RESTART_INSTRUCTION, &vcpu));
1671 }
1672 
1673 int
vm_snapshot_req(struct vm_snapshot_meta * meta)1674 vm_snapshot_req(struct vm_snapshot_meta *meta)
1675 {
1676 
1677 	if (ioctl(meta->ctx->fd, VM_SNAPSHOT_REQ, meta) == -1) {
1678 #ifdef SNAPSHOT_DEBUG
1679 		fprintf(stderr, "%s: snapshot failed for %s: %d\r\n",
1680 		    __func__, meta->dev_name, errno);
1681 #endif
1682 		return (-1);
1683 	}
1684 	return (0);
1685 }
1686 
1687 int
vm_restore_time(struct vmctx * ctx)1688 vm_restore_time(struct vmctx *ctx)
1689 {
1690 	int dummy;
1691 
1692 	dummy = 0;
1693 	return (ioctl(ctx->fd, VM_RESTORE_TIME, &dummy));
1694 }
1695 
1696 int
vm_set_topology(struct vmctx * ctx,uint16_t sockets,uint16_t cores,uint16_t threads,uint16_t maxcpus)1697 vm_set_topology(struct vmctx *ctx,
1698     uint16_t sockets, uint16_t cores, uint16_t threads, uint16_t maxcpus)
1699 {
1700 	struct vm_cpu_topology topology;
1701 
1702 	bzero(&topology, sizeof (struct vm_cpu_topology));
1703 	topology.sockets = sockets;
1704 	topology.cores = cores;
1705 	topology.threads = threads;
1706 	topology.maxcpus = maxcpus;
1707 	return (ioctl(ctx->fd, VM_SET_TOPOLOGY, &topology));
1708 }
1709 
1710 int
vm_get_topology(struct vmctx * ctx,uint16_t * sockets,uint16_t * cores,uint16_t * threads,uint16_t * maxcpus)1711 vm_get_topology(struct vmctx *ctx,
1712     uint16_t *sockets, uint16_t *cores, uint16_t *threads, uint16_t *maxcpus)
1713 {
1714 	struct vm_cpu_topology topology;
1715 	int error;
1716 
1717 	bzero(&topology, sizeof (struct vm_cpu_topology));
1718 	error = ioctl(ctx->fd, VM_GET_TOPOLOGY, &topology);
1719 	if (error == 0) {
1720 		*sockets = topology.sockets;
1721 		*cores = topology.cores;
1722 		*threads = topology.threads;
1723 		*maxcpus = topology.maxcpus;
1724 	}
1725 	return (error);
1726 }
1727 
1728 /* Keep in sync with machine/vmm_dev.h. */
1729 static const cap_ioctl_t vm_ioctl_cmds[] = { VM_RUN, VM_SUSPEND, VM_REINIT,
1730     VM_ALLOC_MEMSEG, VM_GET_MEMSEG, VM_MMAP_MEMSEG, VM_MMAP_MEMSEG,
1731     VM_MMAP_GETNEXT, VM_MUNMAP_MEMSEG, VM_SET_REGISTER, VM_GET_REGISTER,
1732     VM_SET_SEGMENT_DESCRIPTOR, VM_GET_SEGMENT_DESCRIPTOR,
1733     VM_SET_REGISTER_SET, VM_GET_REGISTER_SET,
1734     VM_SET_KERNEMU_DEV, VM_GET_KERNEMU_DEV,
1735     VM_INJECT_EXCEPTION, VM_LAPIC_IRQ, VM_LAPIC_LOCAL_IRQ,
1736     VM_LAPIC_MSI, VM_IOAPIC_ASSERT_IRQ, VM_IOAPIC_DEASSERT_IRQ,
1737     VM_IOAPIC_PULSE_IRQ, VM_IOAPIC_PINCOUNT, VM_ISA_ASSERT_IRQ,
1738     VM_ISA_DEASSERT_IRQ, VM_ISA_PULSE_IRQ, VM_ISA_SET_IRQ_TRIGGER,
1739     VM_SET_CAPABILITY, VM_GET_CAPABILITY, VM_BIND_PPTDEV,
1740     VM_UNBIND_PPTDEV, VM_MAP_PPTDEV_MMIO, VM_PPTDEV_MSI,
1741     VM_PPTDEV_MSIX, VM_UNMAP_PPTDEV_MMIO, VM_PPTDEV_DISABLE_MSIX,
1742     VM_INJECT_NMI, VM_STATS, VM_STAT_DESC,
1743     VM_SET_X2APIC_STATE, VM_GET_X2APIC_STATE,
1744     VM_GET_HPET_CAPABILITIES, VM_GET_GPA_PMAP, VM_GLA2GPA,
1745     VM_GLA2GPA_NOFAULT,
1746     VM_ACTIVATE_CPU, VM_GET_CPUS, VM_SUSPEND_CPU, VM_RESUME_CPU,
1747     VM_SET_INTINFO, VM_GET_INTINFO,
1748     VM_RTC_WRITE, VM_RTC_READ, VM_RTC_SETTIME, VM_RTC_GETTIME,
1749     VM_RESTART_INSTRUCTION, VM_SET_TOPOLOGY, VM_GET_TOPOLOGY,
1750     VM_SNAPSHOT_REQ, VM_RESTORE_TIME
1751 };
1752 
1753 int
vm_limit_rights(struct vmctx * ctx)1754 vm_limit_rights(struct vmctx *ctx)
1755 {
1756 	cap_rights_t rights;
1757 	size_t ncmds;
1758 
1759 	cap_rights_init(&rights, CAP_IOCTL, CAP_MMAP_RW);
1760 	if (caph_rights_limit(ctx->fd, &rights) != 0)
1761 		return (-1);
1762 	ncmds = nitems(vm_ioctl_cmds);
1763 	if (caph_ioctls_limit(ctx->fd, vm_ioctl_cmds, ncmds) != 0)
1764 		return (-1);
1765 	return (0);
1766 }
1767 
1768 /*
1769  * Avoid using in new code.  Operations on the fd should be wrapped here so that
1770  * capability rights can be kept in sync.
1771  */
1772 int
vm_get_device_fd(struct vmctx * ctx)1773 vm_get_device_fd(struct vmctx *ctx)
1774 {
1775 
1776 	return (ctx->fd);
1777 }
1778 
1779 /* Legacy interface, do not use. */
1780 const cap_ioctl_t *
vm_get_ioctls(size_t * len)1781 vm_get_ioctls(size_t *len)
1782 {
1783 	cap_ioctl_t *cmds;
1784 
1785 	if (len == NULL) {
1786 		cmds = malloc(sizeof(vm_ioctl_cmds));
1787 		if (cmds == NULL)
1788 			return (NULL);
1789 		bcopy(vm_ioctl_cmds, cmds, sizeof(vm_ioctl_cmds));
1790 		return (cmds);
1791 	}
1792 
1793 	*len = nitems(vm_ioctl_cmds);
1794 	return (NULL);
1795 }
1796