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