2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/sysctl.h>
34 #include <sys/ioctl.h>
36 #include <sys/_iovec.h>
37 #include <sys/cpuset.h>
39 #include <x86/segments.h>
40 #include <machine/specialreg.h>
52 #include <machine/vmm.h>
53 #include <machine/vmm_dev.h>
57 #define MB (1024 * 1024UL)
58 #define GB (1024 * 1024 * 1024UL)
61 * Size of the guard region before and after the virtual address space
62 * mapping the guest physical memory. This must be a multiple of the
63 * superpage size for performance reasons.
65 #define VM_MMAP_GUARD_SIZE (4 * MB)
67 #define PROT_RW (PROT_READ | PROT_WRITE)
68 #define PROT_ALL (PROT_READ | PROT_WRITE | PROT_EXEC)
72 uint32_t lowmem_limit;
80 #define CREATE(x) sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x)))
81 #define DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x)))
84 vm_device_open(const char *name)
89 len = strlen("/dev/vmm/") + strlen(name) + 1;
91 assert(vmfile != NULL);
92 snprintf(vmfile, len, "/dev/vmm/%s", name);
94 /* Open the device file */
95 fd = open(vmfile, O_RDWR, 0);
102 vm_create(const char *name)
105 return (CREATE((char *)name));
109 vm_open(const char *name)
113 vm = malloc(sizeof(struct vmctx) + strlen(name) + 1);
118 vm->lowmem_limit = 3 * GB;
119 vm->name = (char *)(vm + 1);
120 strcpy(vm->name, name);
122 if ((vm->fd = vm_device_open(vm->name)) < 0)
132 vm_destroy(struct vmctx *vm)
144 vm_parse_memsize(const char *optarg, size_t *ret_memsize)
150 optval = strtoul(optarg, &endptr, 0);
151 if (*optarg != '\0' && *endptr == '\0') {
153 * For the sake of backward compatibility if the memory size
154 * specified on the command line is less than a megabyte then
155 * it is interpreted as being in units of MB.
159 *ret_memsize = optval;
162 error = expand_number(optarg, ret_memsize);
168 vm_get_lowmem_limit(struct vmctx *ctx)
171 return (ctx->lowmem_limit);
175 vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit)
178 ctx->lowmem_limit = limit;
182 vm_set_memflags(struct vmctx *ctx, int flags)
185 ctx->memflags = flags;
189 vm_get_memflags(struct vmctx *ctx)
192 return (ctx->memflags);
196 * Map segment 'segid' starting at 'off' into guest address range [gpa,gpa+len).
199 vm_mmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, int segid, vm_ooffset_t off,
200 size_t len, int prot)
202 struct vm_memmap memmap;
206 memmap.segid = segid;
212 if (ctx->memflags & VM_MEM_F_WIRED)
213 memmap.flags |= VM_MEMMAP_F_WIRED;
216 * If this mapping already exists then don't create it again. This
217 * is the common case for SYSMEM mappings created by bhyveload(8).
219 error = vm_mmap_getnext(ctx, &gpa, &segid, &off, &len, &prot, &flags);
220 if (error == 0 && gpa == memmap.gpa) {
221 if (segid != memmap.segid || off != memmap.segoff ||
222 prot != memmap.prot || flags != memmap.flags) {
230 error = ioctl(ctx->fd, VM_MMAP_MEMSEG, &memmap);
235 vm_mmap_getnext(struct vmctx *ctx, vm_paddr_t *gpa, int *segid,
236 vm_ooffset_t *segoff, size_t *len, int *prot, int *flags)
238 struct vm_memmap memmap;
241 bzero(&memmap, sizeof(struct vm_memmap));
243 error = ioctl(ctx->fd, VM_MMAP_GETNEXT, &memmap);
246 *segid = memmap.segid;
247 *segoff = memmap.segoff;
250 *flags = memmap.flags;
256 * Return 0 if the segments are identical and non-zero otherwise.
258 * This is slightly complicated by the fact that only device memory segments
262 cmpseg(size_t len, const char *str, size_t len2, const char *str2)
266 if ((!str && !str2) || (str && str2 && !strcmp(str, str2)))
273 vm_alloc_memseg(struct vmctx *ctx, int segid, size_t len, const char *name)
275 struct vm_memseg memseg;
280 * If the memory segment has already been created then just return.
281 * This is the usual case for the SYSMEM segment created by userspace
282 * loaders like bhyveload(8).
284 error = vm_get_memseg(ctx, segid, &memseg.len, memseg.name,
285 sizeof(memseg.name));
289 if (memseg.len != 0) {
290 if (cmpseg(len, name, memseg.len, VM_MEMSEG_NAME(&memseg))) {
298 bzero(&memseg, sizeof(struct vm_memseg));
299 memseg.segid = segid;
302 n = strlcpy(memseg.name, name, sizeof(memseg.name));
303 if (n >= sizeof(memseg.name)) {
304 errno = ENAMETOOLONG;
309 error = ioctl(ctx->fd, VM_ALLOC_MEMSEG, &memseg);
314 vm_get_memseg(struct vmctx *ctx, int segid, size_t *lenp, char *namebuf,
317 struct vm_memseg memseg;
321 memseg.segid = segid;
322 error = ioctl(ctx->fd, VM_GET_MEMSEG, &memseg);
325 n = strlcpy(namebuf, memseg.name, bufsize);
327 errno = ENAMETOOLONG;
335 setup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char *base)
340 /* Map 'len' bytes starting at 'gpa' in the guest address space */
341 error = vm_mmap_memseg(ctx, gpa, VM_SYSMEM, gpa, len, PROT_ALL);
345 flags = MAP_SHARED | MAP_FIXED;
346 if ((ctx->memflags & VM_MEM_F_INCORE) == 0)
349 /* mmap into the process address space on the host */
350 ptr = mmap(base + gpa, len, PROT_RW, flags, ctx->fd, gpa);
351 if (ptr == MAP_FAILED)
358 vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms)
362 char *baseaddr, *ptr;
365 assert(vms == VM_MMAP_ALL);
368 * If 'memsize' cannot fit entirely in the 'lowmem' segment then
369 * create another 'highmem' segment above 4GB for the remainder.
371 if (memsize > ctx->lowmem_limit) {
372 ctx->lowmem = ctx->lowmem_limit;
373 ctx->highmem = memsize - ctx->lowmem_limit;
374 objsize = 4*GB + ctx->highmem;
376 ctx->lowmem = memsize;
378 objsize = ctx->lowmem;
381 error = vm_alloc_memseg(ctx, VM_SYSMEM, objsize, NULL);
386 * Stake out a contiguous region covering the guest physical memory
387 * and the adjoining guard regions.
389 len = VM_MMAP_GUARD_SIZE + objsize + VM_MMAP_GUARD_SIZE;
390 ptr = mmap(NULL, len, PROT_NONE, MAP_GUARD | MAP_ALIGNED_SUPER, -1, 0);
391 if (ptr == MAP_FAILED)
394 baseaddr = ptr + VM_MMAP_GUARD_SIZE;
395 if (ctx->highmem > 0) {
398 error = setup_memory_segment(ctx, gpa, len, baseaddr);
403 if (ctx->lowmem > 0) {
406 error = setup_memory_segment(ctx, gpa, len, baseaddr);
411 ctx->baseaddr = baseaddr;
417 * Returns a non-NULL pointer if [gaddr, gaddr+len) is entirely contained in
418 * the lowmem or highmem regions.
420 * In particular return NULL if [gaddr, gaddr+len) falls in guest MMIO region.
421 * The instruction emulation code depends on this behavior.
424 vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len)
427 if (ctx->lowmem > 0) {
428 if (gaddr < ctx->lowmem && len <= ctx->lowmem &&
429 gaddr + len <= ctx->lowmem)
430 return (ctx->baseaddr + gaddr);
433 if (ctx->highmem > 0) {
435 if (gaddr < 4*GB + ctx->highmem &&
436 len <= ctx->highmem &&
437 gaddr + len <= 4*GB + ctx->highmem)
438 return (ctx->baseaddr + gaddr);
446 vm_get_lowmem_size(struct vmctx *ctx)
449 return (ctx->lowmem);
453 vm_get_highmem_size(struct vmctx *ctx)
456 return (ctx->highmem);
460 vm_create_devmem(struct vmctx *ctx, int segid, const char *name, size_t len)
462 char pathname[MAXPATHLEN];
465 int fd, error, flags;
469 if (name == NULL || strlen(name) == 0) {
474 error = vm_alloc_memseg(ctx, segid, len, name);
478 strlcpy(pathname, "/dev/vmm.io/", sizeof(pathname));
479 strlcat(pathname, ctx->name, sizeof(pathname));
480 strlcat(pathname, ".", sizeof(pathname));
481 strlcat(pathname, name, sizeof(pathname));
483 fd = open(pathname, O_RDWR);
488 * Stake out a contiguous region covering the device memory and the
489 * adjoining guard regions.
491 len2 = VM_MMAP_GUARD_SIZE + len + VM_MMAP_GUARD_SIZE;
492 base = mmap(NULL, len2, PROT_NONE, MAP_GUARD | MAP_ALIGNED_SUPER, -1,
494 if (base == MAP_FAILED)
497 flags = MAP_SHARED | MAP_FIXED;
498 if ((ctx->memflags & VM_MEM_F_INCORE) == 0)
501 /* mmap the devmem region in the host address space */
502 ptr = mmap(base + VM_MMAP_GUARD_SIZE, len, PROT_RW, flags, fd, 0);
510 vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
511 uint64_t base, uint32_t limit, uint32_t access)
514 struct vm_seg_desc vmsegdesc;
516 bzero(&vmsegdesc, sizeof(vmsegdesc));
517 vmsegdesc.cpuid = vcpu;
518 vmsegdesc.regnum = reg;
519 vmsegdesc.desc.base = base;
520 vmsegdesc.desc.limit = limit;
521 vmsegdesc.desc.access = access;
523 error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
528 vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
529 uint64_t *base, uint32_t *limit, uint32_t *access)
532 struct vm_seg_desc vmsegdesc;
534 bzero(&vmsegdesc, sizeof(vmsegdesc));
535 vmsegdesc.cpuid = vcpu;
536 vmsegdesc.regnum = reg;
538 error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
540 *base = vmsegdesc.desc.base;
541 *limit = vmsegdesc.desc.limit;
542 *access = vmsegdesc.desc.access;
548 vm_get_seg_desc(struct vmctx *ctx, int vcpu, int reg, struct seg_desc *seg_desc)
552 error = vm_get_desc(ctx, vcpu, reg, &seg_desc->base, &seg_desc->limit,
558 vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
561 struct vm_register vmreg;
563 bzero(&vmreg, sizeof(vmreg));
568 error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg);
573 vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val)
576 struct vm_register vmreg;
578 bzero(&vmreg, sizeof(vmreg));
582 error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg);
583 *ret_val = vmreg.regval;
588 vm_run(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit)
593 bzero(&vmrun, sizeof(vmrun));
596 error = ioctl(ctx->fd, VM_RUN, &vmrun);
597 bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit));
602 vm_suspend(struct vmctx *ctx, enum vm_suspend_how how)
604 struct vm_suspend vmsuspend;
606 bzero(&vmsuspend, sizeof(vmsuspend));
608 return (ioctl(ctx->fd, VM_SUSPEND, &vmsuspend));
612 vm_reinit(struct vmctx *ctx)
615 return (ioctl(ctx->fd, VM_REINIT, 0));
619 vm_inject_exception(struct vmctx *ctx, int vcpu, int vector, int errcode_valid,
620 uint32_t errcode, int restart_instruction)
622 struct vm_exception exc;
626 exc.error_code = errcode;
627 exc.error_code_valid = errcode_valid;
628 exc.restart_instruction = restart_instruction;
630 return (ioctl(ctx->fd, VM_INJECT_EXCEPTION, &exc));
634 vm_apicid2vcpu(struct vmctx *ctx, int apicid)
637 * The apic id associated with the 'vcpu' has the same numerical value
638 * as the 'vcpu' itself.
644 vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
646 struct vm_lapic_irq vmirq;
648 bzero(&vmirq, sizeof(vmirq));
650 vmirq.vector = vector;
652 return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
656 vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector)
658 struct vm_lapic_irq vmirq;
660 bzero(&vmirq, sizeof(vmirq));
662 vmirq.vector = vector;
664 return (ioctl(ctx->fd, VM_LAPIC_LOCAL_IRQ, &vmirq));
668 vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg)
670 struct vm_lapic_msi vmmsi;
672 bzero(&vmmsi, sizeof(vmmsi));
676 return (ioctl(ctx->fd, VM_LAPIC_MSI, &vmmsi));
680 vm_ioapic_assert_irq(struct vmctx *ctx, int irq)
682 struct vm_ioapic_irq ioapic_irq;
684 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
685 ioapic_irq.irq = irq;
687 return (ioctl(ctx->fd, VM_IOAPIC_ASSERT_IRQ, &ioapic_irq));
691 vm_ioapic_deassert_irq(struct vmctx *ctx, int irq)
693 struct vm_ioapic_irq ioapic_irq;
695 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
696 ioapic_irq.irq = irq;
698 return (ioctl(ctx->fd, VM_IOAPIC_DEASSERT_IRQ, &ioapic_irq));
702 vm_ioapic_pulse_irq(struct vmctx *ctx, int irq)
704 struct vm_ioapic_irq ioapic_irq;
706 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
707 ioapic_irq.irq = irq;
709 return (ioctl(ctx->fd, VM_IOAPIC_PULSE_IRQ, &ioapic_irq));
713 vm_ioapic_pincount(struct vmctx *ctx, int *pincount)
716 return (ioctl(ctx->fd, VM_IOAPIC_PINCOUNT, pincount));
720 vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
722 struct vm_isa_irq isa_irq;
724 bzero(&isa_irq, sizeof(struct vm_isa_irq));
725 isa_irq.atpic_irq = atpic_irq;
726 isa_irq.ioapic_irq = ioapic_irq;
728 return (ioctl(ctx->fd, VM_ISA_ASSERT_IRQ, &isa_irq));
732 vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
734 struct vm_isa_irq isa_irq;
736 bzero(&isa_irq, sizeof(struct vm_isa_irq));
737 isa_irq.atpic_irq = atpic_irq;
738 isa_irq.ioapic_irq = ioapic_irq;
740 return (ioctl(ctx->fd, VM_ISA_DEASSERT_IRQ, &isa_irq));
744 vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
746 struct vm_isa_irq isa_irq;
748 bzero(&isa_irq, sizeof(struct vm_isa_irq));
749 isa_irq.atpic_irq = atpic_irq;
750 isa_irq.ioapic_irq = ioapic_irq;
752 return (ioctl(ctx->fd, VM_ISA_PULSE_IRQ, &isa_irq));
756 vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
757 enum vm_intr_trigger trigger)
759 struct vm_isa_irq_trigger isa_irq_trigger;
761 bzero(&isa_irq_trigger, sizeof(struct vm_isa_irq_trigger));
762 isa_irq_trigger.atpic_irq = atpic_irq;
763 isa_irq_trigger.trigger = trigger;
765 return (ioctl(ctx->fd, VM_ISA_SET_IRQ_TRIGGER, &isa_irq_trigger));
769 vm_inject_nmi(struct vmctx *ctx, int vcpu)
773 bzero(&vmnmi, sizeof(vmnmi));
776 return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
783 { "hlt_exit", VM_CAP_HALT_EXIT },
784 { "mtrap_exit", VM_CAP_MTRAP_EXIT },
785 { "pause_exit", VM_CAP_PAUSE_EXIT },
786 { "unrestricted_guest", VM_CAP_UNRESTRICTED_GUEST },
787 { "enable_invpcid", VM_CAP_ENABLE_INVPCID },
792 vm_capability_name2type(const char *capname)
796 for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) {
797 if (strcmp(capstrmap[i].name, capname) == 0)
798 return (capstrmap[i].type);
805 vm_capability_type2name(int type)
809 for (i = 0; capstrmap[i].name != NULL; i++) {
810 if (capstrmap[i].type == type)
811 return (capstrmap[i].name);
818 vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
822 struct vm_capability vmcap;
824 bzero(&vmcap, sizeof(vmcap));
828 error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
829 *retval = vmcap.capval;
834 vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
836 struct vm_capability vmcap;
838 bzero(&vmcap, sizeof(vmcap));
843 return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
847 vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
849 struct vm_pptdev pptdev;
851 bzero(&pptdev, sizeof(pptdev));
856 return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
860 vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
862 struct vm_pptdev pptdev;
864 bzero(&pptdev, sizeof(pptdev));
869 return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
873 vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
874 vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
876 struct vm_pptdev_mmio pptmmio;
878 bzero(&pptmmio, sizeof(pptmmio));
886 return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
890 vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
891 uint64_t addr, uint64_t msg, int numvec)
893 struct vm_pptdev_msi pptmsi;
895 bzero(&pptmsi, sizeof(pptmsi));
902 pptmsi.numvec = numvec;
904 return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
908 vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
909 int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
911 struct vm_pptdev_msix pptmsix;
913 bzero(&pptmsix, sizeof(pptmsix));
921 pptmsix.vector_control = vector_control;
923 return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
927 vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
932 static struct vm_stats vmstats;
934 vmstats.cpuid = vcpu;
936 error = ioctl(ctx->fd, VM_STATS, &vmstats);
939 *ret_entries = vmstats.num_entries;
941 *ret_tv = vmstats.tv;
942 return (vmstats.statbuf);
948 vm_get_stat_desc(struct vmctx *ctx, int index)
950 static struct vm_stat_desc statdesc;
952 statdesc.index = index;
953 if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
954 return (statdesc.desc);
960 vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state)
963 struct vm_x2apic x2apic;
965 bzero(&x2apic, sizeof(x2apic));
968 error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic);
969 *state = x2apic.state;
974 vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state)
977 struct vm_x2apic x2apic;
979 bzero(&x2apic, sizeof(x2apic));
981 x2apic.state = state;
983 error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic);
990 * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
993 vcpu_reset(struct vmctx *vmctx, int vcpu)
996 uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
997 uint32_t desc_access, desc_limit;
1003 error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
1008 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
1012 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
1015 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
1019 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
1023 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
1025 desc_base = 0xffff0000;
1026 desc_limit = 0xffff;
1027 desc_access = 0x0093;
1028 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
1029 desc_base, desc_limit, desc_access);
1034 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
1038 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
1041 desc_limit = 0xffff;
1042 desc_access = 0x0093;
1043 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
1044 desc_base, desc_limit, desc_access);
1048 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
1049 desc_base, desc_limit, desc_access);
1053 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
1054 desc_base, desc_limit, desc_access);
1058 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
1059 desc_base, desc_limit, desc_access);
1063 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
1064 desc_base, desc_limit, desc_access);
1069 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
1071 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
1073 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
1075 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
1077 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
1080 /* General purpose registers */
1082 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
1084 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
1086 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
1088 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
1090 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
1092 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
1094 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
1096 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
1101 desc_limit = 0xffff;
1103 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
1104 desc_base, desc_limit, desc_access);
1108 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
1109 desc_base, desc_limit, desc_access);
1115 desc_limit = 0xffff;
1116 desc_access = 0x0000008b;
1117 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
1122 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
1127 desc_limit = 0xffff;
1128 desc_access = 0x00000082;
1129 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
1130 desc_limit, desc_access);
1135 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
1138 /* XXX cr2, debug registers */
1146 vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num)
1149 struct vm_gpa_pte gpapte;
1151 bzero(&gpapte, sizeof(gpapte));
1154 error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte);
1157 *num = gpapte.ptenum;
1158 for (i = 0; i < gpapte.ptenum; i++)
1159 pte[i] = gpapte.pte[i];
1166 vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities)
1169 struct vm_hpet_cap cap;
1171 bzero(&cap, sizeof(struct vm_hpet_cap));
1172 error = ioctl(ctx->fd, VM_GET_HPET_CAPABILITIES, &cap);
1173 if (capabilities != NULL)
1174 *capabilities = cap.capabilities;
1179 vm_gla2gpa(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
1180 uint64_t gla, int prot, uint64_t *gpa, int *fault)
1182 struct vm_gla2gpa gg;
1185 bzero(&gg, sizeof(struct vm_gla2gpa));
1189 gg.paging = *paging;
1191 error = ioctl(ctx->fd, VM_GLA2GPA, &gg);
1200 #define min(a,b) (((a) < (b)) ? (a) : (b))
1204 vm_copy_setup(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
1205 uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt,
1210 int error, i, n, off;
1212 for (i = 0; i < iovcnt; i++) {
1213 iov[i].iov_base = 0;
1219 error = vm_gla2gpa(ctx, vcpu, paging, gla, prot, &gpa, fault);
1220 if (error || *fault)
1223 off = gpa & PAGE_MASK;
1224 n = min(len, PAGE_SIZE - off);
1226 va = vm_map_gpa(ctx, gpa, n);
1242 vm_copy_teardown(struct vmctx *ctx, int vcpu, struct iovec *iov, int iovcnt)
1249 vm_copyin(struct vmctx *ctx, int vcpu, struct iovec *iov, void *vp, size_t len)
1257 assert(iov->iov_len);
1258 n = min(len, iov->iov_len);
1259 src = iov->iov_base;
1269 vm_copyout(struct vmctx *ctx, int vcpu, const void *vp, struct iovec *iov,
1278 assert(iov->iov_len);
1279 n = min(len, iov->iov_len);
1280 dst = iov->iov_base;
1290 vm_get_cpus(struct vmctx *ctx, int which, cpuset_t *cpus)
1292 struct vm_cpuset vm_cpuset;
1295 bzero(&vm_cpuset, sizeof(struct vm_cpuset));
1296 vm_cpuset.which = which;
1297 vm_cpuset.cpusetsize = sizeof(cpuset_t);
1298 vm_cpuset.cpus = cpus;
1300 error = ioctl(ctx->fd, VM_GET_CPUS, &vm_cpuset);
1305 vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus)
1308 return (vm_get_cpus(ctx, VM_ACTIVE_CPUS, cpus));
1312 vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus)
1315 return (vm_get_cpus(ctx, VM_SUSPENDED_CPUS, cpus));
1319 vm_activate_cpu(struct vmctx *ctx, int vcpu)
1321 struct vm_activate_cpu ac;
1324 bzero(&ac, sizeof(struct vm_activate_cpu));
1326 error = ioctl(ctx->fd, VM_ACTIVATE_CPU, &ac);
1331 vm_get_intinfo(struct vmctx *ctx, int vcpu, uint64_t *info1, uint64_t *info2)
1333 struct vm_intinfo vmii;
1336 bzero(&vmii, sizeof(struct vm_intinfo));
1338 error = ioctl(ctx->fd, VM_GET_INTINFO, &vmii);
1340 *info1 = vmii.info1;
1341 *info2 = vmii.info2;
1347 vm_set_intinfo(struct vmctx *ctx, int vcpu, uint64_t info1)
1349 struct vm_intinfo vmii;
1352 bzero(&vmii, sizeof(struct vm_intinfo));
1355 error = ioctl(ctx->fd, VM_SET_INTINFO, &vmii);
1360 vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value)
1362 struct vm_rtc_data rtcdata;
1365 bzero(&rtcdata, sizeof(struct vm_rtc_data));
1366 rtcdata.offset = offset;
1367 rtcdata.value = value;
1368 error = ioctl(ctx->fd, VM_RTC_WRITE, &rtcdata);
1373 vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval)
1375 struct vm_rtc_data rtcdata;
1378 bzero(&rtcdata, sizeof(struct vm_rtc_data));
1379 rtcdata.offset = offset;
1380 error = ioctl(ctx->fd, VM_RTC_READ, &rtcdata);
1382 *retval = rtcdata.value;
1387 vm_rtc_settime(struct vmctx *ctx, time_t secs)
1389 struct vm_rtc_time rtctime;
1392 bzero(&rtctime, sizeof(struct vm_rtc_time));
1393 rtctime.secs = secs;
1394 error = ioctl(ctx->fd, VM_RTC_SETTIME, &rtctime);
1399 vm_rtc_gettime(struct vmctx *ctx, time_t *secs)
1401 struct vm_rtc_time rtctime;
1404 bzero(&rtctime, sizeof(struct vm_rtc_time));
1405 error = ioctl(ctx->fd, VM_RTC_GETTIME, &rtctime);
1407 *secs = rtctime.secs;
1412 vm_restart_instruction(void *arg, int vcpu)
1414 struct vmctx *ctx = arg;
1416 return (ioctl(ctx->fd, VM_RESTART_INSTRUCTION, &vcpu));
1420 vm_set_topology(struct vmctx *ctx,
1421 uint16_t sockets, uint16_t cores, uint16_t threads, uint16_t maxcpus)
1423 struct vm_cpu_topology topology;
1425 bzero(&topology, sizeof (struct vm_cpu_topology));
1426 topology.sockets = sockets;
1427 topology.cores = cores;
1428 topology.threads = threads;
1429 topology.maxcpus = maxcpus;
1430 return (ioctl(ctx->fd, VM_SET_TOPOLOGY, &topology));
1434 vm_get_topology(struct vmctx *ctx,
1435 uint16_t *sockets, uint16_t *cores, uint16_t *threads, uint16_t *maxcpus)
1437 struct vm_cpu_topology topology;
1440 bzero(&topology, sizeof (struct vm_cpu_topology));
1441 error = ioctl(ctx->fd, VM_GET_TOPOLOGY, &topology);
1443 *sockets = topology.sockets;
1444 *cores = topology.cores;
1445 *threads = topology.threads;
1446 *maxcpus = topology.maxcpus;
1452 vm_get_device_fd(struct vmctx *ctx)
1459 vm_get_ioctls(size_t *len)
1462 /* keep in sync with machine/vmm_dev.h */
1463 static const cap_ioctl_t vm_ioctl_cmds[] = { VM_RUN, VM_SUSPEND, VM_REINIT,
1464 VM_ALLOC_MEMSEG, VM_GET_MEMSEG, VM_MMAP_MEMSEG, VM_MMAP_MEMSEG,
1465 VM_MMAP_GETNEXT, VM_SET_REGISTER, VM_GET_REGISTER,
1466 VM_SET_SEGMENT_DESCRIPTOR, VM_GET_SEGMENT_DESCRIPTOR,
1467 VM_INJECT_EXCEPTION, VM_LAPIC_IRQ, VM_LAPIC_LOCAL_IRQ,
1468 VM_LAPIC_MSI, VM_IOAPIC_ASSERT_IRQ, VM_IOAPIC_DEASSERT_IRQ,
1469 VM_IOAPIC_PULSE_IRQ, VM_IOAPIC_PINCOUNT, VM_ISA_ASSERT_IRQ,
1470 VM_ISA_DEASSERT_IRQ, VM_ISA_PULSE_IRQ, VM_ISA_SET_IRQ_TRIGGER,
1471 VM_SET_CAPABILITY, VM_GET_CAPABILITY, VM_BIND_PPTDEV,
1472 VM_UNBIND_PPTDEV, VM_MAP_PPTDEV_MMIO, VM_PPTDEV_MSI,
1473 VM_PPTDEV_MSIX, VM_INJECT_NMI, VM_STATS, VM_STAT_DESC,
1474 VM_SET_X2APIC_STATE, VM_GET_X2APIC_STATE,
1475 VM_GET_HPET_CAPABILITIES, VM_GET_GPA_PMAP, VM_GLA2GPA,
1476 VM_ACTIVATE_CPU, VM_GET_CPUS, VM_SET_INTINFO, VM_GET_INTINFO,
1477 VM_RTC_WRITE, VM_RTC_READ, VM_RTC_SETTIME, VM_RTC_GETTIME,
1478 VM_RESTART_INSTRUCTION, VM_SET_TOPOLOGY, VM_GET_TOPOLOGY };
1481 cmds = malloc(sizeof(vm_ioctl_cmds));
1484 bcopy(vm_ioctl_cmds, cmds, sizeof(vm_ioctl_cmds));
1488 *len = nitems(vm_ioctl_cmds);