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