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