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