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