2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/sysctl.h>
36 #include <sys/ioctl.h>
37 #include <sys/linker.h>
39 #include <sys/module.h>
40 #include <sys/_iovec.h>
41 #include <sys/cpuset.h>
43 #include <x86/segments.h>
44 #include <machine/specialreg.h>
58 #include <machine/vmm.h>
59 #include <machine/vmm_dev.h>
60 #include <machine/vmm_snapshot.h>
64 #define MB (1024 * 1024UL)
65 #define GB (1024 * 1024 * 1024UL)
68 * Size of the guard region before and after the virtual address space
69 * mapping the guest physical memory. This must be a multiple of the
70 * superpage size for performance reasons.
72 #define VM_MMAP_GUARD_SIZE (4 * MB)
74 #define PROT_RW (PROT_READ | PROT_WRITE)
75 #define PROT_ALL (PROT_READ | PROT_WRITE | PROT_EXEC)
79 uint32_t lowmem_limit;
87 #define CREATE(x) sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x)))
88 #define DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x)))
91 vm_device_open(const char *name)
96 len = strlen("/dev/vmm/") + strlen(name) + 1;
98 assert(vmfile != NULL);
99 snprintf(vmfile, len, "/dev/vmm/%s", name);
101 /* Open the device file */
102 fd = open(vmfile, O_RDWR, 0);
109 vm_create(const char *name)
111 /* Try to load vmm(4) module before creating a guest. */
112 if (modfind("vmm") < 0)
114 return (CREATE((char *)name));
118 vm_open(const char *name)
122 vm = malloc(sizeof(struct vmctx) + strlen(name) + 1);
127 vm->lowmem_limit = 3 * GB;
128 vm->name = (char *)(vm + 1);
129 strcpy(vm->name, name);
131 if ((vm->fd = vm_device_open(vm->name)) < 0)
141 vm_destroy(struct vmctx *vm)
153 vm_parse_memsize(const char *optarg, size_t *ret_memsize)
159 optval = strtoul(optarg, &endptr, 0);
160 if (*optarg != '\0' && *endptr == '\0') {
162 * For the sake of backward compatibility if the memory size
163 * specified on the command line is less than a megabyte then
164 * it is interpreted as being in units of MB.
168 *ret_memsize = optval;
171 error = expand_number(optarg, ret_memsize);
177 vm_get_lowmem_limit(struct vmctx *ctx)
180 return (ctx->lowmem_limit);
184 vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit)
187 ctx->lowmem_limit = limit;
191 vm_set_memflags(struct vmctx *ctx, int flags)
194 ctx->memflags = flags;
198 vm_get_memflags(struct vmctx *ctx)
201 return (ctx->memflags);
205 * Map segment 'segid' starting at 'off' into guest address range [gpa,gpa+len).
208 vm_mmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, int segid, vm_ooffset_t off,
209 size_t len, int prot)
211 struct vm_memmap memmap;
215 memmap.segid = segid;
221 if (ctx->memflags & VM_MEM_F_WIRED)
222 memmap.flags |= VM_MEMMAP_F_WIRED;
225 * If this mapping already exists then don't create it again. This
226 * is the common case for SYSMEM mappings created by bhyveload(8).
228 error = vm_mmap_getnext(ctx, &gpa, &segid, &off, &len, &prot, &flags);
229 if (error == 0 && gpa == memmap.gpa) {
230 if (segid != memmap.segid || off != memmap.segoff ||
231 prot != memmap.prot || flags != memmap.flags) {
239 error = ioctl(ctx->fd, VM_MMAP_MEMSEG, &memmap);
244 vm_get_guestmem_from_ctx(struct vmctx *ctx, char **guest_baseaddr,
245 size_t *lowmem_size, size_t *highmem_size)
248 *guest_baseaddr = ctx->baseaddr;
249 *lowmem_size = ctx->lowmem;
250 *highmem_size = ctx->highmem;
255 vm_mmap_getnext(struct vmctx *ctx, vm_paddr_t *gpa, int *segid,
256 vm_ooffset_t *segoff, size_t *len, int *prot, int *flags)
258 struct vm_memmap memmap;
261 bzero(&memmap, sizeof(struct vm_memmap));
263 error = ioctl(ctx->fd, VM_MMAP_GETNEXT, &memmap);
266 *segid = memmap.segid;
267 *segoff = memmap.segoff;
270 *flags = memmap.flags;
276 * Return 0 if the segments are identical and non-zero otherwise.
278 * This is slightly complicated by the fact that only device memory segments
282 cmpseg(size_t len, const char *str, size_t len2, const char *str2)
286 if ((!str && !str2) || (str && str2 && !strcmp(str, str2)))
293 vm_alloc_memseg(struct vmctx *ctx, int segid, size_t len, const char *name)
295 struct vm_memseg memseg;
300 * If the memory segment has already been created then just return.
301 * This is the usual case for the SYSMEM segment created by userspace
302 * loaders like bhyveload(8).
304 error = vm_get_memseg(ctx, segid, &memseg.len, memseg.name,
305 sizeof(memseg.name));
309 if (memseg.len != 0) {
310 if (cmpseg(len, name, memseg.len, VM_MEMSEG_NAME(&memseg))) {
318 bzero(&memseg, sizeof(struct vm_memseg));
319 memseg.segid = segid;
322 n = strlcpy(memseg.name, name, sizeof(memseg.name));
323 if (n >= sizeof(memseg.name)) {
324 errno = ENAMETOOLONG;
329 error = ioctl(ctx->fd, VM_ALLOC_MEMSEG, &memseg);
334 vm_get_memseg(struct vmctx *ctx, int segid, size_t *lenp, char *namebuf,
337 struct vm_memseg memseg;
341 memseg.segid = segid;
342 error = ioctl(ctx->fd, VM_GET_MEMSEG, &memseg);
345 n = strlcpy(namebuf, memseg.name, bufsize);
347 errno = ENAMETOOLONG;
355 setup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char *base)
360 /* Map 'len' bytes starting at 'gpa' in the guest address space */
361 error = vm_mmap_memseg(ctx, gpa, VM_SYSMEM, gpa, len, PROT_ALL);
365 flags = MAP_SHARED | MAP_FIXED;
366 if ((ctx->memflags & VM_MEM_F_INCORE) == 0)
369 /* mmap into the process address space on the host */
370 ptr = mmap(base + gpa, len, PROT_RW, flags, ctx->fd, gpa);
371 if (ptr == MAP_FAILED)
378 vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms)
382 char *baseaddr, *ptr;
385 assert(vms == VM_MMAP_ALL);
388 * If 'memsize' cannot fit entirely in the 'lowmem' segment then
389 * create another 'highmem' segment above 4GB for the remainder.
391 if (memsize > ctx->lowmem_limit) {
392 ctx->lowmem = ctx->lowmem_limit;
393 ctx->highmem = memsize - ctx->lowmem_limit;
394 objsize = 4*GB + ctx->highmem;
396 ctx->lowmem = memsize;
398 objsize = ctx->lowmem;
401 error = vm_alloc_memseg(ctx, VM_SYSMEM, objsize, NULL);
406 * Stake out a contiguous region covering the guest physical memory
407 * and the adjoining guard regions.
409 len = VM_MMAP_GUARD_SIZE + objsize + VM_MMAP_GUARD_SIZE;
410 ptr = mmap(NULL, len, PROT_NONE, MAP_GUARD | MAP_ALIGNED_SUPER, -1, 0);
411 if (ptr == MAP_FAILED)
414 baseaddr = ptr + VM_MMAP_GUARD_SIZE;
415 if (ctx->highmem > 0) {
418 error = setup_memory_segment(ctx, gpa, len, baseaddr);
423 if (ctx->lowmem > 0) {
426 error = setup_memory_segment(ctx, gpa, len, baseaddr);
431 ctx->baseaddr = baseaddr;
437 * Returns a non-NULL pointer if [gaddr, gaddr+len) is entirely contained in
438 * the lowmem or highmem regions.
440 * In particular return NULL if [gaddr, gaddr+len) falls in guest MMIO region.
441 * The instruction emulation code depends on this behavior.
444 vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len)
447 if (ctx->lowmem > 0) {
448 if (gaddr < ctx->lowmem && len <= ctx->lowmem &&
449 gaddr + len <= ctx->lowmem)
450 return (ctx->baseaddr + gaddr);
453 if (ctx->highmem > 0) {
455 if (gaddr < 4*GB + ctx->highmem &&
456 len <= ctx->highmem &&
457 gaddr + len <= 4*GB + ctx->highmem)
458 return (ctx->baseaddr + gaddr);
466 vm_rev_map_gpa(struct vmctx *ctx, void *addr)
470 offaddr = (char *)addr - ctx->baseaddr;
473 if (offaddr >= 0 && offaddr <= ctx->lowmem)
476 if (ctx->highmem > 0)
477 if (offaddr >= 4*GB && offaddr < 4*GB + ctx->highmem)
480 return ((vm_paddr_t)-1);
483 /* TODO: maximum size for vmname */
485 vm_get_name(struct vmctx *ctx, char *buf, size_t max_len)
488 if (strlcpy(buf, ctx->name, max_len) >= max_len)
494 vm_get_lowmem_size(struct vmctx *ctx)
497 return (ctx->lowmem);
501 vm_get_highmem_size(struct vmctx *ctx)
504 return (ctx->highmem);
508 vm_create_devmem(struct vmctx *ctx, int segid, const char *name, size_t len)
510 char pathname[MAXPATHLEN];
513 int fd, error, flags;
517 if (name == NULL || strlen(name) == 0) {
522 error = vm_alloc_memseg(ctx, segid, len, name);
526 strlcpy(pathname, "/dev/vmm.io/", sizeof(pathname));
527 strlcat(pathname, ctx->name, sizeof(pathname));
528 strlcat(pathname, ".", sizeof(pathname));
529 strlcat(pathname, name, sizeof(pathname));
531 fd = open(pathname, O_RDWR);
536 * Stake out a contiguous region covering the device memory and the
537 * adjoining guard regions.
539 len2 = VM_MMAP_GUARD_SIZE + len + VM_MMAP_GUARD_SIZE;
540 base = mmap(NULL, len2, PROT_NONE, MAP_GUARD | MAP_ALIGNED_SUPER, -1,
542 if (base == MAP_FAILED)
545 flags = MAP_SHARED | MAP_FIXED;
546 if ((ctx->memflags & VM_MEM_F_INCORE) == 0)
549 /* mmap the devmem region in the host address space */
550 ptr = mmap(base + VM_MMAP_GUARD_SIZE, len, PROT_RW, flags, fd, 0);
558 vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
559 uint64_t base, uint32_t limit, uint32_t access)
562 struct vm_seg_desc vmsegdesc;
564 bzero(&vmsegdesc, sizeof(vmsegdesc));
565 vmsegdesc.cpuid = vcpu;
566 vmsegdesc.regnum = reg;
567 vmsegdesc.desc.base = base;
568 vmsegdesc.desc.limit = limit;
569 vmsegdesc.desc.access = access;
571 error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
576 vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
577 uint64_t *base, uint32_t *limit, uint32_t *access)
580 struct vm_seg_desc vmsegdesc;
582 bzero(&vmsegdesc, sizeof(vmsegdesc));
583 vmsegdesc.cpuid = vcpu;
584 vmsegdesc.regnum = reg;
586 error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
588 *base = vmsegdesc.desc.base;
589 *limit = vmsegdesc.desc.limit;
590 *access = vmsegdesc.desc.access;
596 vm_get_seg_desc(struct vmctx *ctx, int vcpu, int reg, struct seg_desc *seg_desc)
600 error = vm_get_desc(ctx, vcpu, reg, &seg_desc->base, &seg_desc->limit,
606 vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
609 struct vm_register vmreg;
611 bzero(&vmreg, sizeof(vmreg));
616 error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg);
621 vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val)
624 struct vm_register vmreg;
626 bzero(&vmreg, sizeof(vmreg));
630 error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg);
631 *ret_val = vmreg.regval;
636 vm_set_register_set(struct vmctx *ctx, int vcpu, unsigned int count,
637 const int *regnums, uint64_t *regvals)
640 struct vm_register_set vmregset;
642 bzero(&vmregset, sizeof(vmregset));
643 vmregset.cpuid = vcpu;
644 vmregset.count = count;
645 vmregset.regnums = regnums;
646 vmregset.regvals = regvals;
648 error = ioctl(ctx->fd, VM_SET_REGISTER_SET, &vmregset);
653 vm_get_register_set(struct vmctx *ctx, int vcpu, unsigned int count,
654 const int *regnums, uint64_t *regvals)
657 struct vm_register_set vmregset;
659 bzero(&vmregset, sizeof(vmregset));
660 vmregset.cpuid = vcpu;
661 vmregset.count = count;
662 vmregset.regnums = regnums;
663 vmregset.regvals = regvals;
665 error = ioctl(ctx->fd, VM_GET_REGISTER_SET, &vmregset);
670 vm_run(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit)
675 bzero(&vmrun, sizeof(vmrun));
678 error = ioctl(ctx->fd, VM_RUN, &vmrun);
679 bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit));
684 vm_suspend(struct vmctx *ctx, enum vm_suspend_how how)
686 struct vm_suspend vmsuspend;
688 bzero(&vmsuspend, sizeof(vmsuspend));
690 return (ioctl(ctx->fd, VM_SUSPEND, &vmsuspend));
694 vm_reinit(struct vmctx *ctx)
697 return (ioctl(ctx->fd, VM_REINIT, 0));
701 vm_inject_exception(struct vmctx *ctx, int vcpu, int vector, int errcode_valid,
702 uint32_t errcode, int restart_instruction)
704 struct vm_exception exc;
708 exc.error_code = errcode;
709 exc.error_code_valid = errcode_valid;
710 exc.restart_instruction = restart_instruction;
712 return (ioctl(ctx->fd, VM_INJECT_EXCEPTION, &exc));
716 vm_apicid2vcpu(struct vmctx *ctx, int apicid)
719 * The apic id associated with the 'vcpu' has the same numerical value
720 * as the 'vcpu' itself.
726 vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
728 struct vm_lapic_irq vmirq;
730 bzero(&vmirq, sizeof(vmirq));
732 vmirq.vector = vector;
734 return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
738 vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector)
740 struct vm_lapic_irq vmirq;
742 bzero(&vmirq, sizeof(vmirq));
744 vmirq.vector = vector;
746 return (ioctl(ctx->fd, VM_LAPIC_LOCAL_IRQ, &vmirq));
750 vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg)
752 struct vm_lapic_msi vmmsi;
754 bzero(&vmmsi, sizeof(vmmsi));
758 return (ioctl(ctx->fd, VM_LAPIC_MSI, &vmmsi));
762 vm_ioapic_assert_irq(struct vmctx *ctx, int irq)
764 struct vm_ioapic_irq ioapic_irq;
766 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
767 ioapic_irq.irq = irq;
769 return (ioctl(ctx->fd, VM_IOAPIC_ASSERT_IRQ, &ioapic_irq));
773 vm_ioapic_deassert_irq(struct vmctx *ctx, int irq)
775 struct vm_ioapic_irq ioapic_irq;
777 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
778 ioapic_irq.irq = irq;
780 return (ioctl(ctx->fd, VM_IOAPIC_DEASSERT_IRQ, &ioapic_irq));
784 vm_ioapic_pulse_irq(struct vmctx *ctx, int irq)
786 struct vm_ioapic_irq ioapic_irq;
788 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
789 ioapic_irq.irq = irq;
791 return (ioctl(ctx->fd, VM_IOAPIC_PULSE_IRQ, &ioapic_irq));
795 vm_ioapic_pincount(struct vmctx *ctx, int *pincount)
798 return (ioctl(ctx->fd, VM_IOAPIC_PINCOUNT, pincount));
802 vm_readwrite_kernemu_device(struct vmctx *ctx, int vcpu, vm_paddr_t gpa,
803 bool write, int size, uint64_t *value)
805 struct vm_readwrite_kernemu_device irp = {
807 .access_width = fls(size) - 1,
809 .value = write ? *value : ~0ul,
811 long cmd = (write ? VM_SET_KERNEMU_DEV : VM_GET_KERNEMU_DEV);
814 rc = ioctl(ctx->fd, cmd, &irp);
815 if (rc == 0 && !write)
821 vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
823 struct vm_isa_irq isa_irq;
825 bzero(&isa_irq, sizeof(struct vm_isa_irq));
826 isa_irq.atpic_irq = atpic_irq;
827 isa_irq.ioapic_irq = ioapic_irq;
829 return (ioctl(ctx->fd, VM_ISA_ASSERT_IRQ, &isa_irq));
833 vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
835 struct vm_isa_irq isa_irq;
837 bzero(&isa_irq, sizeof(struct vm_isa_irq));
838 isa_irq.atpic_irq = atpic_irq;
839 isa_irq.ioapic_irq = ioapic_irq;
841 return (ioctl(ctx->fd, VM_ISA_DEASSERT_IRQ, &isa_irq));
845 vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
847 struct vm_isa_irq isa_irq;
849 bzero(&isa_irq, sizeof(struct vm_isa_irq));
850 isa_irq.atpic_irq = atpic_irq;
851 isa_irq.ioapic_irq = ioapic_irq;
853 return (ioctl(ctx->fd, VM_ISA_PULSE_IRQ, &isa_irq));
857 vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
858 enum vm_intr_trigger trigger)
860 struct vm_isa_irq_trigger isa_irq_trigger;
862 bzero(&isa_irq_trigger, sizeof(struct vm_isa_irq_trigger));
863 isa_irq_trigger.atpic_irq = atpic_irq;
864 isa_irq_trigger.trigger = trigger;
866 return (ioctl(ctx->fd, VM_ISA_SET_IRQ_TRIGGER, &isa_irq_trigger));
870 vm_inject_nmi(struct vmctx *ctx, int vcpu)
874 bzero(&vmnmi, sizeof(vmnmi));
877 return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
880 static const char *capstrmap[] = {
881 [VM_CAP_HALT_EXIT] = "hlt_exit",
882 [VM_CAP_MTRAP_EXIT] = "mtrap_exit",
883 [VM_CAP_PAUSE_EXIT] = "pause_exit",
884 [VM_CAP_UNRESTRICTED_GUEST] = "unrestricted_guest",
885 [VM_CAP_ENABLE_INVPCID] = "enable_invpcid",
886 [VM_CAP_BPT_EXIT] = "bpt_exit",
890 vm_capability_name2type(const char *capname)
894 for (i = 0; i < nitems(capstrmap); i++) {
895 if (strcmp(capstrmap[i], capname) == 0)
903 vm_capability_type2name(int type)
905 if (type >= 0 && type < nitems(capstrmap))
906 return (capstrmap[type]);
912 vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
916 struct vm_capability vmcap;
918 bzero(&vmcap, sizeof(vmcap));
922 error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
923 *retval = vmcap.capval;
928 vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
930 struct vm_capability vmcap;
932 bzero(&vmcap, sizeof(vmcap));
937 return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
941 vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
943 struct vm_pptdev pptdev;
945 bzero(&pptdev, sizeof(pptdev));
950 return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
954 vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
956 struct vm_pptdev pptdev;
958 bzero(&pptdev, sizeof(pptdev));
963 return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
967 vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
968 vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
970 struct vm_pptdev_mmio pptmmio;
972 bzero(&pptmmio, sizeof(pptmmio));
980 return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
984 vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
985 uint64_t addr, uint64_t msg, int numvec)
987 struct vm_pptdev_msi pptmsi;
989 bzero(&pptmsi, sizeof(pptmsi));
996 pptmsi.numvec = numvec;
998 return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
1002 vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
1003 int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
1005 struct vm_pptdev_msix pptmsix;
1007 bzero(&pptmsix, sizeof(pptmsix));
1008 pptmsix.vcpu = vcpu;
1010 pptmsix.slot = slot;
1011 pptmsix.func = func;
1014 pptmsix.addr = addr;
1015 pptmsix.vector_control = vector_control;
1017 return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
1021 vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
1026 static struct vm_stats vmstats;
1028 vmstats.cpuid = vcpu;
1030 error = ioctl(ctx->fd, VM_STATS, &vmstats);
1033 *ret_entries = vmstats.num_entries;
1035 *ret_tv = vmstats.tv;
1036 return (vmstats.statbuf);
1042 vm_get_stat_desc(struct vmctx *ctx, int index)
1044 static struct vm_stat_desc statdesc;
1046 statdesc.index = index;
1047 if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
1048 return (statdesc.desc);
1054 vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state)
1057 struct vm_x2apic x2apic;
1059 bzero(&x2apic, sizeof(x2apic));
1060 x2apic.cpuid = vcpu;
1062 error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic);
1063 *state = x2apic.state;
1068 vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state)
1071 struct vm_x2apic x2apic;
1073 bzero(&x2apic, sizeof(x2apic));
1074 x2apic.cpuid = vcpu;
1075 x2apic.state = state;
1077 error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic);
1083 * From Intel Vol 3a:
1084 * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
1087 vcpu_reset(struct vmctx *vmctx, int vcpu)
1090 uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
1091 uint32_t desc_access, desc_limit;
1097 error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
1102 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
1106 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
1109 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
1113 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
1117 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
1119 desc_base = 0xffff0000;
1120 desc_limit = 0xffff;
1121 desc_access = 0x0093;
1122 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
1123 desc_base, desc_limit, desc_access);
1128 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
1132 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
1135 desc_limit = 0xffff;
1136 desc_access = 0x0093;
1137 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
1138 desc_base, desc_limit, desc_access);
1142 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
1143 desc_base, desc_limit, desc_access);
1147 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
1148 desc_base, desc_limit, desc_access);
1152 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
1153 desc_base, desc_limit, desc_access);
1157 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
1158 desc_base, desc_limit, desc_access);
1163 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
1165 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
1167 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
1169 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
1171 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
1174 /* General purpose registers */
1176 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
1178 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
1180 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
1182 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
1184 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
1186 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
1188 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
1190 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
1195 desc_limit = 0xffff;
1197 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
1198 desc_base, desc_limit, desc_access);
1202 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
1203 desc_base, desc_limit, desc_access);
1209 desc_limit = 0xffff;
1210 desc_access = 0x0000008b;
1211 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
1216 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
1221 desc_limit = 0xffff;
1222 desc_access = 0x00000082;
1223 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
1224 desc_limit, desc_access);
1229 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
1232 /* XXX cr2, debug registers */
1240 vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num)
1243 struct vm_gpa_pte gpapte;
1245 bzero(&gpapte, sizeof(gpapte));
1248 error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte);
1251 *num = gpapte.ptenum;
1252 for (i = 0; i < gpapte.ptenum; i++)
1253 pte[i] = gpapte.pte[i];
1260 vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities)
1263 struct vm_hpet_cap cap;
1265 bzero(&cap, sizeof(struct vm_hpet_cap));
1266 error = ioctl(ctx->fd, VM_GET_HPET_CAPABILITIES, &cap);
1267 if (capabilities != NULL)
1268 *capabilities = cap.capabilities;
1273 vm_gla2gpa(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
1274 uint64_t gla, int prot, uint64_t *gpa, int *fault)
1276 struct vm_gla2gpa gg;
1279 bzero(&gg, sizeof(struct vm_gla2gpa));
1283 gg.paging = *paging;
1285 error = ioctl(ctx->fd, VM_GLA2GPA, &gg);
1294 vm_gla2gpa_nofault(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
1295 uint64_t gla, int prot, uint64_t *gpa, int *fault)
1297 struct vm_gla2gpa gg;
1300 bzero(&gg, sizeof(struct vm_gla2gpa));
1304 gg.paging = *paging;
1306 error = ioctl(ctx->fd, VM_GLA2GPA_NOFAULT, &gg);
1315 #define min(a,b) (((a) < (b)) ? (a) : (b))
1319 vm_copy_setup(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
1320 uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt,
1325 int error, i, n, off;
1327 for (i = 0; i < iovcnt; i++) {
1328 iov[i].iov_base = 0;
1334 error = vm_gla2gpa(ctx, vcpu, paging, gla, prot, &gpa, fault);
1335 if (error || *fault)
1338 off = gpa & PAGE_MASK;
1339 n = min(len, PAGE_SIZE - off);
1341 va = vm_map_gpa(ctx, gpa, n);
1357 vm_copy_teardown(struct vmctx *ctx, int vcpu, struct iovec *iov, int iovcnt)
1364 vm_copyin(struct vmctx *ctx, int vcpu, struct iovec *iov, void *vp, size_t len)
1372 assert(iov->iov_len);
1373 n = min(len, iov->iov_len);
1374 src = iov->iov_base;
1384 vm_copyout(struct vmctx *ctx, int vcpu, const void *vp, struct iovec *iov,
1393 assert(iov->iov_len);
1394 n = min(len, iov->iov_len);
1395 dst = iov->iov_base;
1405 vm_get_cpus(struct vmctx *ctx, int which, cpuset_t *cpus)
1407 struct vm_cpuset vm_cpuset;
1410 bzero(&vm_cpuset, sizeof(struct vm_cpuset));
1411 vm_cpuset.which = which;
1412 vm_cpuset.cpusetsize = sizeof(cpuset_t);
1413 vm_cpuset.cpus = cpus;
1415 error = ioctl(ctx->fd, VM_GET_CPUS, &vm_cpuset);
1420 vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus)
1423 return (vm_get_cpus(ctx, VM_ACTIVE_CPUS, cpus));
1427 vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus)
1430 return (vm_get_cpus(ctx, VM_SUSPENDED_CPUS, cpus));
1434 vm_debug_cpus(struct vmctx *ctx, cpuset_t *cpus)
1437 return (vm_get_cpus(ctx, VM_DEBUG_CPUS, cpus));
1441 vm_activate_cpu(struct vmctx *ctx, int vcpu)
1443 struct vm_activate_cpu ac;
1446 bzero(&ac, sizeof(struct vm_activate_cpu));
1448 error = ioctl(ctx->fd, VM_ACTIVATE_CPU, &ac);
1453 vm_suspend_cpu(struct vmctx *ctx, int vcpu)
1455 struct vm_activate_cpu ac;
1458 bzero(&ac, sizeof(struct vm_activate_cpu));
1460 error = ioctl(ctx->fd, VM_SUSPEND_CPU, &ac);
1465 vm_resume_cpu(struct vmctx *ctx, int vcpu)
1467 struct vm_activate_cpu ac;
1470 bzero(&ac, sizeof(struct vm_activate_cpu));
1472 error = ioctl(ctx->fd, VM_RESUME_CPU, &ac);
1477 vm_get_intinfo(struct vmctx *ctx, int vcpu, uint64_t *info1, uint64_t *info2)
1479 struct vm_intinfo vmii;
1482 bzero(&vmii, sizeof(struct vm_intinfo));
1484 error = ioctl(ctx->fd, VM_GET_INTINFO, &vmii);
1486 *info1 = vmii.info1;
1487 *info2 = vmii.info2;
1493 vm_set_intinfo(struct vmctx *ctx, int vcpu, uint64_t info1)
1495 struct vm_intinfo vmii;
1498 bzero(&vmii, sizeof(struct vm_intinfo));
1501 error = ioctl(ctx->fd, VM_SET_INTINFO, &vmii);
1506 vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value)
1508 struct vm_rtc_data rtcdata;
1511 bzero(&rtcdata, sizeof(struct vm_rtc_data));
1512 rtcdata.offset = offset;
1513 rtcdata.value = value;
1514 error = ioctl(ctx->fd, VM_RTC_WRITE, &rtcdata);
1519 vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval)
1521 struct vm_rtc_data rtcdata;
1524 bzero(&rtcdata, sizeof(struct vm_rtc_data));
1525 rtcdata.offset = offset;
1526 error = ioctl(ctx->fd, VM_RTC_READ, &rtcdata);
1528 *retval = rtcdata.value;
1533 vm_rtc_settime(struct vmctx *ctx, time_t secs)
1535 struct vm_rtc_time rtctime;
1538 bzero(&rtctime, sizeof(struct vm_rtc_time));
1539 rtctime.secs = secs;
1540 error = ioctl(ctx->fd, VM_RTC_SETTIME, &rtctime);
1545 vm_rtc_gettime(struct vmctx *ctx, time_t *secs)
1547 struct vm_rtc_time rtctime;
1550 bzero(&rtctime, sizeof(struct vm_rtc_time));
1551 error = ioctl(ctx->fd, VM_RTC_GETTIME, &rtctime);
1553 *secs = rtctime.secs;
1558 vm_restart_instruction(void *arg, int vcpu)
1560 struct vmctx *ctx = arg;
1562 return (ioctl(ctx->fd, VM_RESTART_INSTRUCTION, &vcpu));
1566 vm_snapshot_req(struct vm_snapshot_meta *meta)
1569 if (ioctl(meta->ctx->fd, VM_SNAPSHOT_REQ, meta) == -1) {
1570 #ifdef SNAPSHOT_DEBUG
1571 fprintf(stderr, "%s: snapshot failed for %s: %d\r\n",
1572 __func__, meta->dev_name, errno);
1580 vm_restore_time(struct vmctx *ctx)
1585 return (ioctl(ctx->fd, VM_RESTORE_TIME, &dummy));
1589 vm_set_topology(struct vmctx *ctx,
1590 uint16_t sockets, uint16_t cores, uint16_t threads, uint16_t maxcpus)
1592 struct vm_cpu_topology topology;
1594 bzero(&topology, sizeof (struct vm_cpu_topology));
1595 topology.sockets = sockets;
1596 topology.cores = cores;
1597 topology.threads = threads;
1598 topology.maxcpus = maxcpus;
1599 return (ioctl(ctx->fd, VM_SET_TOPOLOGY, &topology));
1603 vm_get_topology(struct vmctx *ctx,
1604 uint16_t *sockets, uint16_t *cores, uint16_t *threads, uint16_t *maxcpus)
1606 struct vm_cpu_topology topology;
1609 bzero(&topology, sizeof (struct vm_cpu_topology));
1610 error = ioctl(ctx->fd, VM_GET_TOPOLOGY, &topology);
1612 *sockets = topology.sockets;
1613 *cores = topology.cores;
1614 *threads = topology.threads;
1615 *maxcpus = topology.maxcpus;
1621 vm_get_device_fd(struct vmctx *ctx)
1628 vm_get_ioctls(size_t *len)
1631 /* keep in sync with machine/vmm_dev.h */
1632 static const cap_ioctl_t vm_ioctl_cmds[] = { VM_RUN, VM_SUSPEND, VM_REINIT,
1633 VM_ALLOC_MEMSEG, VM_GET_MEMSEG, VM_MMAP_MEMSEG, VM_MMAP_MEMSEG,
1634 VM_MMAP_GETNEXT, VM_SET_REGISTER, VM_GET_REGISTER,
1635 VM_SET_SEGMENT_DESCRIPTOR, VM_GET_SEGMENT_DESCRIPTOR,
1636 VM_SET_REGISTER_SET, VM_GET_REGISTER_SET,
1637 VM_SET_KERNEMU_DEV, VM_GET_KERNEMU_DEV,
1638 VM_INJECT_EXCEPTION, VM_LAPIC_IRQ, VM_LAPIC_LOCAL_IRQ,
1639 VM_LAPIC_MSI, VM_IOAPIC_ASSERT_IRQ, VM_IOAPIC_DEASSERT_IRQ,
1640 VM_IOAPIC_PULSE_IRQ, VM_IOAPIC_PINCOUNT, VM_ISA_ASSERT_IRQ,
1641 VM_ISA_DEASSERT_IRQ, VM_ISA_PULSE_IRQ, VM_ISA_SET_IRQ_TRIGGER,
1642 VM_SET_CAPABILITY, VM_GET_CAPABILITY, VM_BIND_PPTDEV,
1643 VM_UNBIND_PPTDEV, VM_MAP_PPTDEV_MMIO, VM_PPTDEV_MSI,
1644 VM_PPTDEV_MSIX, VM_INJECT_NMI, VM_STATS, VM_STAT_DESC,
1645 VM_SET_X2APIC_STATE, VM_GET_X2APIC_STATE,
1646 VM_GET_HPET_CAPABILITIES, VM_GET_GPA_PMAP, VM_GLA2GPA,
1648 VM_ACTIVATE_CPU, VM_GET_CPUS, VM_SUSPEND_CPU, VM_RESUME_CPU,
1649 VM_SET_INTINFO, VM_GET_INTINFO,
1650 VM_RTC_WRITE, VM_RTC_READ, VM_RTC_SETTIME, VM_RTC_GETTIME,
1651 VM_RESTART_INSTRUCTION, VM_SET_TOPOLOGY, VM_GET_TOPOLOGY };
1654 cmds = malloc(sizeof(vm_ioctl_cmds));
1657 bcopy(vm_ioctl_cmds, cmds, sizeof(vm_ioctl_cmds));
1661 *len = nitems(vm_ioctl_cmds);