1 /*-
2 * Copyright (c) 2011 NetApp, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: stable/10/lib/libvmmapi/vmmapi.c 309648 2016-12-06 18:55:01Z glebius $
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: stable/10/lib/libvmmapi/vmmapi.c 309648 2016-12-06 18:55:01Z glebius $");
31
32 #include <sys/param.h>
33 #include <sys/sysctl.h>
34 #include <sys/ioctl.h>
35 #include <sys/mman.h>
36 #include <sys/_iovec.h>
37 #include <sys/cpuset.h>
38
39 #include <x86/segments.h>
40 #include <machine/specialreg.h>
41 #include <machine/param.h>
42
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <assert.h>
47 #include <string.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50
51 #include <libutil.h>
52
53 #include <machine/vmm.h>
54 #include <machine/vmm_dev.h>
55
56 #include "vmmapi.h"
57
58 #define MB (1024 * 1024UL)
59 #define GB (1024 * 1024 * 1024UL)
60
61 /*
62 * Size of the guard region before and after the virtual address space
63 * mapping the guest physical memory. This must be a multiple of the
64 * superpage size for performance reasons.
65 */
66 #define VM_MMAP_GUARD_SIZE (4 * MB)
67
68 #define PROT_RW (PROT_READ | PROT_WRITE)
69 #define PROT_ALL (PROT_READ | PROT_WRITE | PROT_EXEC)
70
71 struct vmctx {
72 int fd;
73 uint32_t lowmem_limit;
74 int memflags;
75 size_t lowmem;
76 size_t highmem;
77 char *baseaddr;
78 char *name;
79 };
80
81 #define CREATE(x) sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x)))
82 #define DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x)))
83
84 static int
vm_device_open(const char * name)85 vm_device_open(const char *name)
86 {
87 int fd, len;
88 char *vmfile;
89
90 len = strlen("/dev/vmm/") + strlen(name) + 1;
91 vmfile = malloc(len);
92 assert(vmfile != NULL);
93 snprintf(vmfile, len, "/dev/vmm/%s", name);
94
95 /* Open the device file */
96 fd = open(vmfile, O_RDWR, 0);
97
98 free(vmfile);
99 return (fd);
100 }
101
102 int
vm_create(const char * name)103 vm_create(const char *name)
104 {
105
106 return (CREATE((char *)name));
107 }
108
109 struct vmctx *
vm_open(const char * name)110 vm_open(const char *name)
111 {
112 struct vmctx *vm;
113
114 vm = malloc(sizeof(struct vmctx) + strlen(name) + 1);
115 assert(vm != NULL);
116
117 vm->fd = -1;
118 vm->memflags = 0;
119 vm->lowmem_limit = 3 * GB;
120 vm->name = (char *)(vm + 1);
121 strcpy(vm->name, name);
122
123 if ((vm->fd = vm_device_open(vm->name)) < 0)
124 goto err;
125
126 return (vm);
127 err:
128 vm_destroy(vm);
129 return (NULL);
130 }
131
132 void
vm_destroy(struct vmctx * vm)133 vm_destroy(struct vmctx *vm)
134 {
135 assert(vm != NULL);
136
137 if (vm->fd >= 0)
138 close(vm->fd);
139 DESTROY(vm->name);
140
141 free(vm);
142 }
143
144 int
vm_parse_memsize(const char * optarg,size_t * ret_memsize)145 vm_parse_memsize(const char *optarg, size_t *ret_memsize)
146 {
147 char *endptr;
148 size_t optval;
149 int error;
150
151 optval = strtoul(optarg, &endptr, 0);
152 if (*optarg != '\0' && *endptr == '\0') {
153 /*
154 * For the sake of backward compatibility if the memory size
155 * specified on the command line is less than a megabyte then
156 * it is interpreted as being in units of MB.
157 */
158 if (optval < MB)
159 optval *= MB;
160 *ret_memsize = optval;
161 error = 0;
162 } else
163 error = expand_number(optarg, ret_memsize);
164
165 return (error);
166 }
167
168 uint32_t
vm_get_lowmem_limit(struct vmctx * ctx)169 vm_get_lowmem_limit(struct vmctx *ctx)
170 {
171
172 return (ctx->lowmem_limit);
173 }
174
175 void
vm_set_lowmem_limit(struct vmctx * ctx,uint32_t limit)176 vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit)
177 {
178
179 ctx->lowmem_limit = limit;
180 }
181
182 void
vm_set_memflags(struct vmctx * ctx,int flags)183 vm_set_memflags(struct vmctx *ctx, int flags)
184 {
185
186 ctx->memflags = flags;
187 }
188
189 int
vm_get_memflags(struct vmctx * ctx)190 vm_get_memflags(struct vmctx *ctx)
191 {
192
193 return (ctx->memflags);
194 }
195
196 /*
197 * Map segment 'segid' starting at 'off' into guest address range [gpa,gpa+len).
198 */
199 int
vm_mmap_memseg(struct vmctx * ctx,vm_paddr_t gpa,int segid,vm_ooffset_t off,size_t len,int prot)200 vm_mmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, int segid, vm_ooffset_t off,
201 size_t len, int prot)
202 {
203 struct vm_memmap memmap;
204 int error, flags;
205
206 memmap.gpa = gpa;
207 memmap.segid = segid;
208 memmap.segoff = off;
209 memmap.len = len;
210 memmap.prot = prot;
211 memmap.flags = 0;
212
213 if (ctx->memflags & VM_MEM_F_WIRED)
214 memmap.flags |= VM_MEMMAP_F_WIRED;
215
216 /*
217 * If this mapping already exists then don't create it again. This
218 * is the common case for SYSMEM mappings created by bhyveload(8).
219 */
220 error = vm_mmap_getnext(ctx, &gpa, &segid, &off, &len, &prot, &flags);
221 if (error == 0 && gpa == memmap.gpa) {
222 if (segid != memmap.segid || off != memmap.segoff ||
223 prot != memmap.prot || flags != memmap.flags) {
224 errno = EEXIST;
225 return (-1);
226 } else {
227 return (0);
228 }
229 }
230
231 error = ioctl(ctx->fd, VM_MMAP_MEMSEG, &memmap);
232 return (error);
233 }
234
235 int
vm_mmap_getnext(struct vmctx * ctx,vm_paddr_t * gpa,int * segid,vm_ooffset_t * segoff,size_t * len,int * prot,int * flags)236 vm_mmap_getnext(struct vmctx *ctx, vm_paddr_t *gpa, int *segid,
237 vm_ooffset_t *segoff, size_t *len, int *prot, int *flags)
238 {
239 struct vm_memmap memmap;
240 int error;
241
242 bzero(&memmap, sizeof(struct vm_memmap));
243 memmap.gpa = *gpa;
244 error = ioctl(ctx->fd, VM_MMAP_GETNEXT, &memmap);
245 if (error == 0) {
246 *gpa = memmap.gpa;
247 *segid = memmap.segid;
248 *segoff = memmap.segoff;
249 *len = memmap.len;
250 *prot = memmap.prot;
251 *flags = memmap.flags;
252 }
253 return (error);
254 }
255
256 /*
257 * Return 0 if the segments are identical and non-zero otherwise.
258 *
259 * This is slightly complicated by the fact that only device memory segments
260 * are named.
261 */
262 static int
cmpseg(size_t len,const char * str,size_t len2,const char * str2)263 cmpseg(size_t len, const char *str, size_t len2, const char *str2)
264 {
265
266 if (len == len2) {
267 if ((!str && !str2) || (str && str2 && !strcmp(str, str2)))
268 return (0);
269 }
270 return (-1);
271 }
272
273 static int
vm_alloc_memseg(struct vmctx * ctx,int segid,size_t len,const char * name)274 vm_alloc_memseg(struct vmctx *ctx, int segid, size_t len, const char *name)
275 {
276 struct vm_memseg memseg;
277 size_t n;
278 int error;
279
280 /*
281 * If the memory segment has already been created then just return.
282 * This is the usual case for the SYSMEM segment created by userspace
283 * loaders like bhyveload(8).
284 */
285 error = vm_get_memseg(ctx, segid, &memseg.len, memseg.name,
286 sizeof(memseg.name));
287 if (error)
288 return (error);
289
290 if (memseg.len != 0) {
291 if (cmpseg(len, name, memseg.len, VM_MEMSEG_NAME(&memseg))) {
292 errno = EINVAL;
293 return (-1);
294 } else {
295 return (0);
296 }
297 }
298
299 bzero(&memseg, sizeof(struct vm_memseg));
300 memseg.segid = segid;
301 memseg.len = len;
302 if (name != NULL) {
303 n = strlcpy(memseg.name, name, sizeof(memseg.name));
304 if (n >= sizeof(memseg.name)) {
305 errno = ENAMETOOLONG;
306 return (-1);
307 }
308 }
309
310 error = ioctl(ctx->fd, VM_ALLOC_MEMSEG, &memseg);
311 return (error);
312 }
313
314 int
vm_get_memseg(struct vmctx * ctx,int segid,size_t * lenp,char * namebuf,size_t bufsize)315 vm_get_memseg(struct vmctx *ctx, int segid, size_t *lenp, char *namebuf,
316 size_t bufsize)
317 {
318 struct vm_memseg memseg;
319 size_t n;
320 int error;
321
322 memseg.segid = segid;
323 error = ioctl(ctx->fd, VM_GET_MEMSEG, &memseg);
324 if (error == 0) {
325 *lenp = memseg.len;
326 n = strlcpy(namebuf, memseg.name, bufsize);
327 if (n >= bufsize) {
328 errno = ENAMETOOLONG;
329 error = -1;
330 }
331 }
332 return (error);
333 }
334
335 static int
setup_memory_segment(struct vmctx * ctx,vm_paddr_t gpa,size_t len,char * base)336 setup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char *base)
337 {
338 char *ptr;
339 int error, flags;
340
341 /* Map 'len' bytes starting at 'gpa' in the guest address space */
342 error = vm_mmap_memseg(ctx, gpa, VM_SYSMEM, gpa, len, PROT_ALL);
343 if (error)
344 return (error);
345
346 flags = MAP_SHARED | MAP_FIXED;
347 if ((ctx->memflags & VM_MEM_F_INCORE) == 0)
348 flags |= MAP_NOCORE;
349
350 /* mmap into the process address space on the host */
351 ptr = mmap(base + gpa, len, PROT_RW, flags, ctx->fd, gpa);
352 if (ptr == MAP_FAILED)
353 return (-1);
354
355 return (0);
356 }
357
358 int
vm_setup_memory(struct vmctx * ctx,size_t memsize,enum vm_mmap_style vms)359 vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms)
360 {
361 size_t objsize, len;
362 vm_paddr_t gpa;
363 char *baseaddr, *ptr;
364 int error, flags;
365
366 assert(vms == VM_MMAP_ALL);
367
368 /*
369 * If 'memsize' cannot fit entirely in the 'lowmem' segment then
370 * create another 'highmem' segment above 4GB for the remainder.
371 */
372 if (memsize > ctx->lowmem_limit) {
373 ctx->lowmem = ctx->lowmem_limit;
374 ctx->highmem = memsize - ctx->lowmem_limit;
375 objsize = 4*GB + ctx->highmem;
376 } else {
377 ctx->lowmem = memsize;
378 ctx->highmem = 0;
379 objsize = ctx->lowmem;
380 }
381
382 error = vm_alloc_memseg(ctx, VM_SYSMEM, objsize, NULL);
383 if (error)
384 return (error);
385
386 /*
387 * Stake out a contiguous region covering the guest physical memory
388 * and the adjoining guard regions.
389 */
390 len = VM_MMAP_GUARD_SIZE + objsize + VM_MMAP_GUARD_SIZE;
391 flags = MAP_PRIVATE | MAP_ANON | MAP_NOCORE | MAP_ALIGNED_SUPER;
392 ptr = mmap(NULL, len, PROT_NONE, flags, -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 *
vm_map_gpa(struct vmctx * ctx,vm_paddr_t gaddr,size_t len)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
vm_get_lowmem_size(struct vmctx * ctx)448 vm_get_lowmem_size(struct vmctx *ctx)
449 {
450
451 return (ctx->lowmem);
452 }
453
454 size_t
vm_get_highmem_size(struct vmctx * ctx)455 vm_get_highmem_size(struct vmctx *ctx)
456 {
457
458 return (ctx->highmem);
459 }
460
461 void *
vm_create_devmem(struct vmctx * ctx,int segid,const char * name,size_t len)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 flags = MAP_PRIVATE | MAP_ANON | MAP_NOCORE | MAP_ALIGNED_SUPER;
495 base = mmap(NULL, len2, PROT_NONE, flags, -1, 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
vm_set_desc(struct vmctx * ctx,int vcpu,int reg,uint64_t base,uint32_t limit,uint32_t access)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
vm_get_desc(struct vmctx * ctx,int vcpu,int reg,uint64_t * base,uint32_t * limit,uint32_t * access)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
vm_get_seg_desc(struct vmctx * ctx,int vcpu,int reg,struct seg_desc * seg_desc)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
vm_set_register(struct vmctx * ctx,int vcpu,int reg,uint64_t val)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
vm_get_register(struct vmctx * ctx,int vcpu,int reg,uint64_t * ret_val)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
vm_run(struct vmctx * ctx,int vcpu,struct vm_exit * vmexit)590 vm_run(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit)
591 {
592 int error;
593 struct vm_run vmrun;
594
595 bzero(&vmrun, sizeof(vmrun));
596 vmrun.cpuid = vcpu;
597
598 error = ioctl(ctx->fd, VM_RUN, &vmrun);
599 bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit));
600 return (error);
601 }
602
603 int
vm_suspend(struct vmctx * ctx,enum vm_suspend_how how)604 vm_suspend(struct vmctx *ctx, enum vm_suspend_how how)
605 {
606 struct vm_suspend vmsuspend;
607
608 bzero(&vmsuspend, sizeof(vmsuspend));
609 vmsuspend.how = how;
610 return (ioctl(ctx->fd, VM_SUSPEND, &vmsuspend));
611 }
612
613 int
vm_reinit(struct vmctx * ctx)614 vm_reinit(struct vmctx *ctx)
615 {
616
617 return (ioctl(ctx->fd, VM_REINIT, 0));
618 }
619
620 int
vm_inject_exception(struct vmctx * ctx,int vcpu,int vector,int errcode_valid,uint32_t errcode,int restart_instruction)621 vm_inject_exception(struct vmctx *ctx, int vcpu, int vector, int errcode_valid,
622 uint32_t errcode, int restart_instruction)
623 {
624 struct vm_exception exc;
625
626 exc.cpuid = vcpu;
627 exc.vector = vector;
628 exc.error_code = errcode;
629 exc.error_code_valid = errcode_valid;
630 exc.restart_instruction = restart_instruction;
631
632 return (ioctl(ctx->fd, VM_INJECT_EXCEPTION, &exc));
633 }
634
635 int
vm_apicid2vcpu(struct vmctx * ctx,int apicid)636 vm_apicid2vcpu(struct vmctx *ctx, int apicid)
637 {
638 /*
639 * The apic id associated with the 'vcpu' has the same numerical value
640 * as the 'vcpu' itself.
641 */
642 return (apicid);
643 }
644
645 int
vm_lapic_irq(struct vmctx * ctx,int vcpu,int vector)646 vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
647 {
648 struct vm_lapic_irq vmirq;
649
650 bzero(&vmirq, sizeof(vmirq));
651 vmirq.cpuid = vcpu;
652 vmirq.vector = vector;
653
654 return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
655 }
656
657 int
vm_lapic_local_irq(struct vmctx * ctx,int vcpu,int vector)658 vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector)
659 {
660 struct vm_lapic_irq vmirq;
661
662 bzero(&vmirq, sizeof(vmirq));
663 vmirq.cpuid = vcpu;
664 vmirq.vector = vector;
665
666 return (ioctl(ctx->fd, VM_LAPIC_LOCAL_IRQ, &vmirq));
667 }
668
669 int
vm_lapic_msi(struct vmctx * ctx,uint64_t addr,uint64_t msg)670 vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg)
671 {
672 struct vm_lapic_msi vmmsi;
673
674 bzero(&vmmsi, sizeof(vmmsi));
675 vmmsi.addr = addr;
676 vmmsi.msg = msg;
677
678 return (ioctl(ctx->fd, VM_LAPIC_MSI, &vmmsi));
679 }
680
681 int
vm_ioapic_assert_irq(struct vmctx * ctx,int irq)682 vm_ioapic_assert_irq(struct vmctx *ctx, int irq)
683 {
684 struct vm_ioapic_irq ioapic_irq;
685
686 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
687 ioapic_irq.irq = irq;
688
689 return (ioctl(ctx->fd, VM_IOAPIC_ASSERT_IRQ, &ioapic_irq));
690 }
691
692 int
vm_ioapic_deassert_irq(struct vmctx * ctx,int irq)693 vm_ioapic_deassert_irq(struct vmctx *ctx, int irq)
694 {
695 struct vm_ioapic_irq ioapic_irq;
696
697 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
698 ioapic_irq.irq = irq;
699
700 return (ioctl(ctx->fd, VM_IOAPIC_DEASSERT_IRQ, &ioapic_irq));
701 }
702
703 int
vm_ioapic_pulse_irq(struct vmctx * ctx,int irq)704 vm_ioapic_pulse_irq(struct vmctx *ctx, int irq)
705 {
706 struct vm_ioapic_irq ioapic_irq;
707
708 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
709 ioapic_irq.irq = irq;
710
711 return (ioctl(ctx->fd, VM_IOAPIC_PULSE_IRQ, &ioapic_irq));
712 }
713
714 int
vm_ioapic_pincount(struct vmctx * ctx,int * pincount)715 vm_ioapic_pincount(struct vmctx *ctx, int *pincount)
716 {
717
718 return (ioctl(ctx->fd, VM_IOAPIC_PINCOUNT, pincount));
719 }
720
721 int
vm_isa_assert_irq(struct vmctx * ctx,int atpic_irq,int ioapic_irq)722 vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
723 {
724 struct vm_isa_irq isa_irq;
725
726 bzero(&isa_irq, sizeof(struct vm_isa_irq));
727 isa_irq.atpic_irq = atpic_irq;
728 isa_irq.ioapic_irq = ioapic_irq;
729
730 return (ioctl(ctx->fd, VM_ISA_ASSERT_IRQ, &isa_irq));
731 }
732
733 int
vm_isa_deassert_irq(struct vmctx * ctx,int atpic_irq,int ioapic_irq)734 vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
735 {
736 struct vm_isa_irq isa_irq;
737
738 bzero(&isa_irq, sizeof(struct vm_isa_irq));
739 isa_irq.atpic_irq = atpic_irq;
740 isa_irq.ioapic_irq = ioapic_irq;
741
742 return (ioctl(ctx->fd, VM_ISA_DEASSERT_IRQ, &isa_irq));
743 }
744
745 int
vm_isa_pulse_irq(struct vmctx * ctx,int atpic_irq,int ioapic_irq)746 vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
747 {
748 struct vm_isa_irq isa_irq;
749
750 bzero(&isa_irq, sizeof(struct vm_isa_irq));
751 isa_irq.atpic_irq = atpic_irq;
752 isa_irq.ioapic_irq = ioapic_irq;
753
754 return (ioctl(ctx->fd, VM_ISA_PULSE_IRQ, &isa_irq));
755 }
756
757 int
vm_isa_set_irq_trigger(struct vmctx * ctx,int atpic_irq,enum vm_intr_trigger trigger)758 vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
759 enum vm_intr_trigger trigger)
760 {
761 struct vm_isa_irq_trigger isa_irq_trigger;
762
763 bzero(&isa_irq_trigger, sizeof(struct vm_isa_irq_trigger));
764 isa_irq_trigger.atpic_irq = atpic_irq;
765 isa_irq_trigger.trigger = trigger;
766
767 return (ioctl(ctx->fd, VM_ISA_SET_IRQ_TRIGGER, &isa_irq_trigger));
768 }
769
770 int
vm_inject_nmi(struct vmctx * ctx,int vcpu)771 vm_inject_nmi(struct vmctx *ctx, int vcpu)
772 {
773 struct vm_nmi vmnmi;
774
775 bzero(&vmnmi, sizeof(vmnmi));
776 vmnmi.cpuid = vcpu;
777
778 return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
779 }
780
781 static struct {
782 const char *name;
783 int type;
784 } capstrmap[] = {
785 { "hlt_exit", VM_CAP_HALT_EXIT },
786 { "mtrap_exit", VM_CAP_MTRAP_EXIT },
787 { "pause_exit", VM_CAP_PAUSE_EXIT },
788 { "unrestricted_guest", VM_CAP_UNRESTRICTED_GUEST },
789 { "enable_invpcid", VM_CAP_ENABLE_INVPCID },
790 { 0 }
791 };
792
793 int
vm_capability_name2type(const char * capname)794 vm_capability_name2type(const char *capname)
795 {
796 int i;
797
798 for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) {
799 if (strcmp(capstrmap[i].name, capname) == 0)
800 return (capstrmap[i].type);
801 }
802
803 return (-1);
804 }
805
806 const char *
vm_capability_type2name(int type)807 vm_capability_type2name(int type)
808 {
809 int i;
810
811 for (i = 0; capstrmap[i].name != NULL; i++) {
812 if (capstrmap[i].type == type)
813 return (capstrmap[i].name);
814 }
815
816 return (NULL);
817 }
818
819 int
vm_get_capability(struct vmctx * ctx,int vcpu,enum vm_cap_type cap,int * retval)820 vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
821 int *retval)
822 {
823 int error;
824 struct vm_capability vmcap;
825
826 bzero(&vmcap, sizeof(vmcap));
827 vmcap.cpuid = vcpu;
828 vmcap.captype = cap;
829
830 error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
831 *retval = vmcap.capval;
832 return (error);
833 }
834
835 int
vm_set_capability(struct vmctx * ctx,int vcpu,enum vm_cap_type cap,int val)836 vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
837 {
838 struct vm_capability vmcap;
839
840 bzero(&vmcap, sizeof(vmcap));
841 vmcap.cpuid = vcpu;
842 vmcap.captype = cap;
843 vmcap.capval = val;
844
845 return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
846 }
847
848 int
vm_assign_pptdev(struct vmctx * ctx,int bus,int slot,int func)849 vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
850 {
851 struct vm_pptdev pptdev;
852
853 bzero(&pptdev, sizeof(pptdev));
854 pptdev.bus = bus;
855 pptdev.slot = slot;
856 pptdev.func = func;
857
858 return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
859 }
860
861 int
vm_unassign_pptdev(struct vmctx * ctx,int bus,int slot,int func)862 vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
863 {
864 struct vm_pptdev pptdev;
865
866 bzero(&pptdev, sizeof(pptdev));
867 pptdev.bus = bus;
868 pptdev.slot = slot;
869 pptdev.func = func;
870
871 return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
872 }
873
874 int
vm_map_pptdev_mmio(struct vmctx * ctx,int bus,int slot,int func,vm_paddr_t gpa,size_t len,vm_paddr_t hpa)875 vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
876 vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
877 {
878 struct vm_pptdev_mmio pptmmio;
879
880 bzero(&pptmmio, sizeof(pptmmio));
881 pptmmio.bus = bus;
882 pptmmio.slot = slot;
883 pptmmio.func = func;
884 pptmmio.gpa = gpa;
885 pptmmio.len = len;
886 pptmmio.hpa = hpa;
887
888 return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
889 }
890
891 int
vm_setup_pptdev_msi(struct vmctx * ctx,int vcpu,int bus,int slot,int func,uint64_t addr,uint64_t msg,int numvec)892 vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
893 uint64_t addr, uint64_t msg, int numvec)
894 {
895 struct vm_pptdev_msi pptmsi;
896
897 bzero(&pptmsi, sizeof(pptmsi));
898 pptmsi.vcpu = vcpu;
899 pptmsi.bus = bus;
900 pptmsi.slot = slot;
901 pptmsi.func = func;
902 pptmsi.msg = msg;
903 pptmsi.addr = addr;
904 pptmsi.numvec = numvec;
905
906 return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
907 }
908
909 int
vm_setup_pptdev_msix(struct vmctx * ctx,int vcpu,int bus,int slot,int func,int idx,uint64_t addr,uint64_t msg,uint32_t vector_control)910 vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
911 int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
912 {
913 struct vm_pptdev_msix pptmsix;
914
915 bzero(&pptmsix, sizeof(pptmsix));
916 pptmsix.vcpu = vcpu;
917 pptmsix.bus = bus;
918 pptmsix.slot = slot;
919 pptmsix.func = func;
920 pptmsix.idx = idx;
921 pptmsix.msg = msg;
922 pptmsix.addr = addr;
923 pptmsix.vector_control = vector_control;
924
925 return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
926 }
927
928 uint64_t *
vm_get_stats(struct vmctx * ctx,int vcpu,struct timeval * ret_tv,int * ret_entries)929 vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
930 int *ret_entries)
931 {
932 int error;
933
934 static struct vm_stats vmstats;
935
936 vmstats.cpuid = vcpu;
937
938 error = ioctl(ctx->fd, VM_STATS, &vmstats);
939 if (error == 0) {
940 if (ret_entries)
941 *ret_entries = vmstats.num_entries;
942 if (ret_tv)
943 *ret_tv = vmstats.tv;
944 return (vmstats.statbuf);
945 } else
946 return (NULL);
947 }
948
949 const char *
vm_get_stat_desc(struct vmctx * ctx,int index)950 vm_get_stat_desc(struct vmctx *ctx, int index)
951 {
952 static struct vm_stat_desc statdesc;
953
954 statdesc.index = index;
955 if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
956 return (statdesc.desc);
957 else
958 return (NULL);
959 }
960
961 int
vm_get_x2apic_state(struct vmctx * ctx,int vcpu,enum x2apic_state * state)962 vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state)
963 {
964 int error;
965 struct vm_x2apic x2apic;
966
967 bzero(&x2apic, sizeof(x2apic));
968 x2apic.cpuid = vcpu;
969
970 error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic);
971 *state = x2apic.state;
972 return (error);
973 }
974
975 int
vm_set_x2apic_state(struct vmctx * ctx,int vcpu,enum x2apic_state state)976 vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state)
977 {
978 int error;
979 struct vm_x2apic x2apic;
980
981 bzero(&x2apic, sizeof(x2apic));
982 x2apic.cpuid = vcpu;
983 x2apic.state = state;
984
985 error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic);
986
987 return (error);
988 }
989
990 /*
991 * From Intel Vol 3a:
992 * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
993 */
994 int
vcpu_reset(struct vmctx * vmctx,int vcpu)995 vcpu_reset(struct vmctx *vmctx, int vcpu)
996 {
997 int error;
998 uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
999 uint32_t desc_access, desc_limit;
1000 uint16_t sel;
1001
1002 zero = 0;
1003
1004 rflags = 0x2;
1005 error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
1006 if (error)
1007 goto done;
1008
1009 rip = 0xfff0;
1010 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
1011 goto done;
1012
1013 cr0 = CR0_NE;
1014 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
1015 goto done;
1016
1017 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
1018 goto done;
1019
1020 cr4 = 0;
1021 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
1022 goto done;
1023
1024 /*
1025 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
1026 */
1027 desc_base = 0xffff0000;
1028 desc_limit = 0xffff;
1029 desc_access = 0x0093;
1030 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
1031 desc_base, desc_limit, desc_access);
1032 if (error)
1033 goto done;
1034
1035 sel = 0xf000;
1036 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
1037 goto done;
1038
1039 /*
1040 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
1041 */
1042 desc_base = 0;
1043 desc_limit = 0xffff;
1044 desc_access = 0x0093;
1045 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
1046 desc_base, desc_limit, desc_access);
1047 if (error)
1048 goto done;
1049
1050 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
1051 desc_base, desc_limit, desc_access);
1052 if (error)
1053 goto done;
1054
1055 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
1056 desc_base, desc_limit, desc_access);
1057 if (error)
1058 goto done;
1059
1060 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
1061 desc_base, desc_limit, desc_access);
1062 if (error)
1063 goto done;
1064
1065 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
1066 desc_base, desc_limit, desc_access);
1067 if (error)
1068 goto done;
1069
1070 sel = 0;
1071 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
1072 goto done;
1073 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
1074 goto done;
1075 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
1076 goto done;
1077 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
1078 goto done;
1079 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
1080 goto done;
1081
1082 /* General purpose registers */
1083 rdx = 0xf00;
1084 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
1085 goto done;
1086 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
1087 goto done;
1088 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
1089 goto done;
1090 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
1091 goto done;
1092 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
1093 goto done;
1094 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
1095 goto done;
1096 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
1097 goto done;
1098 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
1099 goto done;
1100
1101 /* GDTR, IDTR */
1102 desc_base = 0;
1103 desc_limit = 0xffff;
1104 desc_access = 0;
1105 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
1106 desc_base, desc_limit, desc_access);
1107 if (error != 0)
1108 goto done;
1109
1110 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
1111 desc_base, desc_limit, desc_access);
1112 if (error != 0)
1113 goto done;
1114
1115 /* TR */
1116 desc_base = 0;
1117 desc_limit = 0xffff;
1118 desc_access = 0x0000008b;
1119 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
1120 if (error)
1121 goto done;
1122
1123 sel = 0;
1124 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
1125 goto done;
1126
1127 /* LDTR */
1128 desc_base = 0;
1129 desc_limit = 0xffff;
1130 desc_access = 0x00000082;
1131 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
1132 desc_limit, desc_access);
1133 if (error)
1134 goto done;
1135
1136 sel = 0;
1137 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
1138 goto done;
1139
1140 /* XXX cr2, debug registers */
1141
1142 error = 0;
1143 done:
1144 return (error);
1145 }
1146
1147 int
vm_get_gpa_pmap(struct vmctx * ctx,uint64_t gpa,uint64_t * pte,int * num)1148 vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num)
1149 {
1150 int error, i;
1151 struct vm_gpa_pte gpapte;
1152
1153 bzero(&gpapte, sizeof(gpapte));
1154 gpapte.gpa = gpa;
1155
1156 error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte);
1157
1158 if (error == 0) {
1159 *num = gpapte.ptenum;
1160 for (i = 0; i < gpapte.ptenum; i++)
1161 pte[i] = gpapte.pte[i];
1162 }
1163
1164 return (error);
1165 }
1166
1167 int
vm_get_hpet_capabilities(struct vmctx * ctx,uint32_t * capabilities)1168 vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities)
1169 {
1170 int error;
1171 struct vm_hpet_cap cap;
1172
1173 bzero(&cap, sizeof(struct vm_hpet_cap));
1174 error = ioctl(ctx->fd, VM_GET_HPET_CAPABILITIES, &cap);
1175 if (capabilities != NULL)
1176 *capabilities = cap.capabilities;
1177 return (error);
1178 }
1179
1180 int
vm_gla2gpa(struct vmctx * ctx,int vcpu,struct vm_guest_paging * paging,uint64_t gla,int prot,uint64_t * gpa,int * fault)1181 vm_gla2gpa(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
1182 uint64_t gla, int prot, uint64_t *gpa, int *fault)
1183 {
1184 struct vm_gla2gpa gg;
1185 int error;
1186
1187 bzero(&gg, sizeof(struct vm_gla2gpa));
1188 gg.vcpuid = vcpu;
1189 gg.prot = prot;
1190 gg.gla = gla;
1191 gg.paging = *paging;
1192
1193 error = ioctl(ctx->fd, VM_GLA2GPA, &gg);
1194 if (error == 0) {
1195 *fault = gg.fault;
1196 *gpa = gg.gpa;
1197 }
1198 return (error);
1199 }
1200
1201 #ifndef min
1202 #define min(a,b) (((a) < (b)) ? (a) : (b))
1203 #endif
1204
1205 int
vm_copy_setup(struct vmctx * ctx,int vcpu,struct vm_guest_paging * paging,uint64_t gla,size_t len,int prot,struct iovec * iov,int iovcnt,int * fault)1206 vm_copy_setup(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
1207 uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt,
1208 int *fault)
1209 {
1210 void *va;
1211 uint64_t gpa;
1212 int error, i, n, off;
1213
1214 for (i = 0; i < iovcnt; i++) {
1215 iov[i].iov_base = 0;
1216 iov[i].iov_len = 0;
1217 }
1218
1219 while (len) {
1220 assert(iovcnt > 0);
1221 error = vm_gla2gpa(ctx, vcpu, paging, gla, prot, &gpa, fault);
1222 if (error || *fault)
1223 return (error);
1224
1225 off = gpa & PAGE_MASK;
1226 n = min(len, PAGE_SIZE - off);
1227
1228 va = vm_map_gpa(ctx, gpa, n);
1229 if (va == NULL)
1230 return (EFAULT);
1231
1232 iov->iov_base = va;
1233 iov->iov_len = n;
1234 iov++;
1235 iovcnt--;
1236
1237 gla += n;
1238 len -= n;
1239 }
1240 return (0);
1241 }
1242
1243 void
vm_copy_teardown(struct vmctx * ctx,int vcpu,struct iovec * iov,int iovcnt)1244 vm_copy_teardown(struct vmctx *ctx, int vcpu, struct iovec *iov, int iovcnt)
1245 {
1246
1247 return;
1248 }
1249
1250 void
vm_copyin(struct vmctx * ctx,int vcpu,struct iovec * iov,void * vp,size_t len)1251 vm_copyin(struct vmctx *ctx, int vcpu, struct iovec *iov, void *vp, size_t len)
1252 {
1253 const char *src;
1254 char *dst;
1255 size_t n;
1256
1257 dst = vp;
1258 while (len) {
1259 assert(iov->iov_len);
1260 n = min(len, iov->iov_len);
1261 src = iov->iov_base;
1262 bcopy(src, dst, n);
1263
1264 iov++;
1265 dst += n;
1266 len -= n;
1267 }
1268 }
1269
1270 void
vm_copyout(struct vmctx * ctx,int vcpu,const void * vp,struct iovec * iov,size_t len)1271 vm_copyout(struct vmctx *ctx, int vcpu, const void *vp, struct iovec *iov,
1272 size_t len)
1273 {
1274 const char *src;
1275 char *dst;
1276 size_t n;
1277
1278 src = vp;
1279 while (len) {
1280 assert(iov->iov_len);
1281 n = min(len, iov->iov_len);
1282 dst = iov->iov_base;
1283 bcopy(src, dst, n);
1284
1285 iov++;
1286 src += n;
1287 len -= n;
1288 }
1289 }
1290
1291 static int
vm_get_cpus(struct vmctx * ctx,int which,cpuset_t * cpus)1292 vm_get_cpus(struct vmctx *ctx, int which, cpuset_t *cpus)
1293 {
1294 struct vm_cpuset vm_cpuset;
1295 int error;
1296
1297 bzero(&vm_cpuset, sizeof(struct vm_cpuset));
1298 vm_cpuset.which = which;
1299 vm_cpuset.cpusetsize = sizeof(cpuset_t);
1300 vm_cpuset.cpus = cpus;
1301
1302 error = ioctl(ctx->fd, VM_GET_CPUS, &vm_cpuset);
1303 return (error);
1304 }
1305
1306 int
vm_active_cpus(struct vmctx * ctx,cpuset_t * cpus)1307 vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus)
1308 {
1309
1310 return (vm_get_cpus(ctx, VM_ACTIVE_CPUS, cpus));
1311 }
1312
1313 int
vm_suspended_cpus(struct vmctx * ctx,cpuset_t * cpus)1314 vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus)
1315 {
1316
1317 return (vm_get_cpus(ctx, VM_SUSPENDED_CPUS, cpus));
1318 }
1319
1320 int
vm_activate_cpu(struct vmctx * ctx,int vcpu)1321 vm_activate_cpu(struct vmctx *ctx, int vcpu)
1322 {
1323 struct vm_activate_cpu ac;
1324 int error;
1325
1326 bzero(&ac, sizeof(struct vm_activate_cpu));
1327 ac.vcpuid = vcpu;
1328 error = ioctl(ctx->fd, VM_ACTIVATE_CPU, &ac);
1329 return (error);
1330 }
1331
1332 int
vm_get_intinfo(struct vmctx * ctx,int vcpu,uint64_t * info1,uint64_t * info2)1333 vm_get_intinfo(struct vmctx *ctx, int vcpu, uint64_t *info1, uint64_t *info2)
1334 {
1335 struct vm_intinfo vmii;
1336 int error;
1337
1338 bzero(&vmii, sizeof(struct vm_intinfo));
1339 vmii.vcpuid = vcpu;
1340 error = ioctl(ctx->fd, VM_GET_INTINFO, &vmii);
1341 if (error == 0) {
1342 *info1 = vmii.info1;
1343 *info2 = vmii.info2;
1344 }
1345 return (error);
1346 }
1347
1348 int
vm_set_intinfo(struct vmctx * ctx,int vcpu,uint64_t info1)1349 vm_set_intinfo(struct vmctx *ctx, int vcpu, uint64_t info1)
1350 {
1351 struct vm_intinfo vmii;
1352 int error;
1353
1354 bzero(&vmii, sizeof(struct vm_intinfo));
1355 vmii.vcpuid = vcpu;
1356 vmii.info1 = info1;
1357 error = ioctl(ctx->fd, VM_SET_INTINFO, &vmii);
1358 return (error);
1359 }
1360
1361 int
vm_rtc_write(struct vmctx * ctx,int offset,uint8_t value)1362 vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value)
1363 {
1364 struct vm_rtc_data rtcdata;
1365 int error;
1366
1367 bzero(&rtcdata, sizeof(struct vm_rtc_data));
1368 rtcdata.offset = offset;
1369 rtcdata.value = value;
1370 error = ioctl(ctx->fd, VM_RTC_WRITE, &rtcdata);
1371 return (error);
1372 }
1373
1374 int
vm_rtc_read(struct vmctx * ctx,int offset,uint8_t * retval)1375 vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval)
1376 {
1377 struct vm_rtc_data rtcdata;
1378 int error;
1379
1380 bzero(&rtcdata, sizeof(struct vm_rtc_data));
1381 rtcdata.offset = offset;
1382 error = ioctl(ctx->fd, VM_RTC_READ, &rtcdata);
1383 if (error == 0)
1384 *retval = rtcdata.value;
1385 return (error);
1386 }
1387
1388 int
vm_rtc_settime(struct vmctx * ctx,time_t secs)1389 vm_rtc_settime(struct vmctx *ctx, time_t secs)
1390 {
1391 struct vm_rtc_time rtctime;
1392 int error;
1393
1394 bzero(&rtctime, sizeof(struct vm_rtc_time));
1395 rtctime.secs = secs;
1396 error = ioctl(ctx->fd, VM_RTC_SETTIME, &rtctime);
1397 return (error);
1398 }
1399
1400 int
vm_rtc_gettime(struct vmctx * ctx,time_t * secs)1401 vm_rtc_gettime(struct vmctx *ctx, time_t *secs)
1402 {
1403 struct vm_rtc_time rtctime;
1404 int error;
1405
1406 bzero(&rtctime, sizeof(struct vm_rtc_time));
1407 error = ioctl(ctx->fd, VM_RTC_GETTIME, &rtctime);
1408 if (error == 0)
1409 *secs = rtctime.secs;
1410 return (error);
1411 }
1412
1413 int
vm_restart_instruction(void * arg,int vcpu)1414 vm_restart_instruction(void *arg, int vcpu)
1415 {
1416 struct vmctx *ctx = arg;
1417
1418 return (ioctl(ctx->fd, VM_RESTART_INSTRUCTION, &vcpu));
1419 }
1420