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>
41 #include <machine/param.h>
53 #include <machine/vmm.h>
54 #include <machine/vmm_dev.h>
58 #define MB (1024 * 1024UL)
59 #define GB (1024 * 1024 * 1024UL)
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.
66 #define VM_MMAP_GUARD_SIZE (4 * MB)
68 #define PROT_RW (PROT_READ | PROT_WRITE)
69 #define PROT_ALL (PROT_READ | PROT_WRITE | PROT_EXEC)
73 uint32_t lowmem_limit;
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)))
85 vm_device_open(const char *name)
90 len = strlen("/dev/vmm/") + strlen(name) + 1;
92 assert(vmfile != NULL);
93 snprintf(vmfile, len, "/dev/vmm/%s", name);
95 /* Open the device file */
96 fd = open(vmfile, O_RDWR, 0);
103 vm_create(const char *name)
106 return (CREATE((char *)name));
110 vm_open(const char *name)
114 vm = malloc(sizeof(struct vmctx) + strlen(name) + 1);
119 vm->lowmem_limit = 3 * GB;
120 vm->name = (char *)(vm + 1);
121 strcpy(vm->name, name);
123 if ((vm->fd = vm_device_open(vm->name)) < 0)
133 vm_destroy(struct vmctx *vm)
145 vm_parse_memsize(const char *optarg, size_t *ret_memsize)
151 optval = strtoul(optarg, &endptr, 0);
152 if (*optarg != '\0' && *endptr == '\0') {
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.
160 *ret_memsize = optval;
163 error = expand_number(optarg, ret_memsize);
169 vm_get_lowmem_limit(struct vmctx *ctx)
172 return (ctx->lowmem_limit);
176 vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit)
179 ctx->lowmem_limit = limit;
183 vm_set_memflags(struct vmctx *ctx, int flags)
186 ctx->memflags = flags;
190 vm_get_memflags(struct vmctx *ctx)
193 return (ctx->memflags);
197 * Map segment 'segid' starting at 'off' into guest address range [gpa,gpa+len).
200 vm_mmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, int segid, vm_ooffset_t off,
201 size_t len, int prot)
203 struct vm_memmap memmap;
207 memmap.segid = segid;
213 if (ctx->memflags & VM_MEM_F_WIRED)
214 memmap.flags |= VM_MEMMAP_F_WIRED;
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).
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) {
231 error = ioctl(ctx->fd, VM_MMAP_MEMSEG, &memmap);
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)
239 struct vm_memmap memmap;
242 bzero(&memmap, sizeof(struct vm_memmap));
244 error = ioctl(ctx->fd, VM_MMAP_GETNEXT, &memmap);
247 *segid = memmap.segid;
248 *segoff = memmap.segoff;
251 *flags = memmap.flags;
257 * Return 0 if the segments are identical and non-zero otherwise.
259 * This is slightly complicated by the fact that only device memory segments
263 cmpseg(size_t len, const char *str, size_t len2, const char *str2)
267 if ((!str && !str2) || (str && str2 && !strcmp(str, str2)))
274 vm_alloc_memseg(struct vmctx *ctx, int segid, size_t len, const char *name)
276 struct vm_memseg memseg;
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).
285 error = vm_get_memseg(ctx, segid, &memseg.len, memseg.name,
286 sizeof(memseg.name));
290 if (memseg.len != 0) {
291 if (cmpseg(len, name, memseg.len, VM_MEMSEG_NAME(&memseg))) {
299 bzero(&memseg, sizeof(struct vm_memseg));
300 memseg.segid = segid;
303 n = strlcpy(memseg.name, name, sizeof(memseg.name));
304 if (n >= sizeof(memseg.name)) {
305 errno = ENAMETOOLONG;
310 error = ioctl(ctx->fd, VM_ALLOC_MEMSEG, &memseg);
315 vm_get_memseg(struct vmctx *ctx, int segid, size_t *lenp, char *namebuf,
318 struct vm_memseg memseg;
322 memseg.segid = segid;
323 error = ioctl(ctx->fd, VM_GET_MEMSEG, &memseg);
326 n = strlcpy(namebuf, memseg.name, bufsize);
328 errno = ENAMETOOLONG;
336 setup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char *base)
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);
346 flags = MAP_SHARED | MAP_FIXED;
347 if ((ctx->memflags & VM_MEM_F_INCORE) == 0)
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)
359 vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms)
363 char *baseaddr, *ptr;
366 assert(vms == VM_MMAP_ALL);
369 * If 'memsize' cannot fit entirely in the 'lowmem' segment then
370 * create another 'highmem' segment above 4GB for the remainder.
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;
377 ctx->lowmem = memsize;
379 objsize = ctx->lowmem;
382 error = vm_alloc_memseg(ctx, VM_SYSMEM, objsize, NULL);
387 * Stake out a contiguous region covering the guest physical memory
388 * and the adjoining guard regions.
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)
396 baseaddr = ptr + VM_MMAP_GUARD_SIZE;
397 if (ctx->highmem > 0) {
400 error = setup_memory_segment(ctx, gpa, len, baseaddr);
405 if (ctx->lowmem > 0) {
408 error = setup_memory_segment(ctx, gpa, len, baseaddr);
413 ctx->baseaddr = baseaddr;
419 * Returns a non-NULL pointer if [gaddr, gaddr+len) is entirely contained in
420 * the lowmem or highmem regions.
422 * In particular return NULL if [gaddr, gaddr+len) falls in guest MMIO region.
423 * The instruction emulation code depends on this behavior.
426 vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len)
429 if (ctx->lowmem > 0) {
430 if (gaddr < ctx->lowmem && gaddr + len <= ctx->lowmem)
431 return (ctx->baseaddr + gaddr);
434 if (ctx->highmem > 0) {
435 if (gaddr >= 4*GB && gaddr + len <= 4*GB + ctx->highmem)
436 return (ctx->baseaddr + gaddr);
443 vm_get_lowmem_size(struct vmctx *ctx)
446 return (ctx->lowmem);
450 vm_get_highmem_size(struct vmctx *ctx)
453 return (ctx->highmem);
457 vm_create_devmem(struct vmctx *ctx, int segid, const char *name, size_t len)
459 char pathname[MAXPATHLEN];
462 int fd, error, flags;
466 if (name == NULL || strlen(name) == 0) {
471 error = vm_alloc_memseg(ctx, segid, len, name);
475 strlcpy(pathname, "/dev/vmm.io/", sizeof(pathname));
476 strlcat(pathname, ctx->name, sizeof(pathname));
477 strlcat(pathname, ".", sizeof(pathname));
478 strlcat(pathname, name, sizeof(pathname));
480 fd = open(pathname, O_RDWR);
485 * Stake out a contiguous region covering the device memory and the
486 * adjoining guard regions.
488 len2 = VM_MMAP_GUARD_SIZE + len + VM_MMAP_GUARD_SIZE;
489 flags = MAP_PRIVATE | MAP_ANON | MAP_NOCORE | MAP_ALIGNED_SUPER;
490 base = mmap(NULL, len2, PROT_NONE, flags, -1, 0);
491 if (base == MAP_FAILED)
494 flags = MAP_SHARED | MAP_FIXED;
495 if ((ctx->memflags & VM_MEM_F_INCORE) == 0)
498 /* mmap the devmem region in the host address space */
499 ptr = mmap(base + VM_MMAP_GUARD_SIZE, len, PROT_RW, flags, fd, 0);
507 vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
508 uint64_t base, uint32_t limit, uint32_t access)
511 struct vm_seg_desc vmsegdesc;
513 bzero(&vmsegdesc, sizeof(vmsegdesc));
514 vmsegdesc.cpuid = vcpu;
515 vmsegdesc.regnum = reg;
516 vmsegdesc.desc.base = base;
517 vmsegdesc.desc.limit = limit;
518 vmsegdesc.desc.access = access;
520 error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
525 vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
526 uint64_t *base, uint32_t *limit, uint32_t *access)
529 struct vm_seg_desc vmsegdesc;
531 bzero(&vmsegdesc, sizeof(vmsegdesc));
532 vmsegdesc.cpuid = vcpu;
533 vmsegdesc.regnum = reg;
535 error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
537 *base = vmsegdesc.desc.base;
538 *limit = vmsegdesc.desc.limit;
539 *access = vmsegdesc.desc.access;
545 vm_get_seg_desc(struct vmctx *ctx, int vcpu, int reg, struct seg_desc *seg_desc)
549 error = vm_get_desc(ctx, vcpu, reg, &seg_desc->base, &seg_desc->limit,
555 vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
558 struct vm_register vmreg;
560 bzero(&vmreg, sizeof(vmreg));
565 error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg);
570 vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val)
573 struct vm_register vmreg;
575 bzero(&vmreg, sizeof(vmreg));
579 error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg);
580 *ret_val = vmreg.regval;
585 vm_run(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit)
590 bzero(&vmrun, sizeof(vmrun));
593 error = ioctl(ctx->fd, VM_RUN, &vmrun);
594 bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit));
599 vm_suspend(struct vmctx *ctx, enum vm_suspend_how how)
601 struct vm_suspend vmsuspend;
603 bzero(&vmsuspend, sizeof(vmsuspend));
605 return (ioctl(ctx->fd, VM_SUSPEND, &vmsuspend));
609 vm_reinit(struct vmctx *ctx)
612 return (ioctl(ctx->fd, VM_REINIT, 0));
616 vm_inject_exception(struct vmctx *ctx, int vcpu, int vector, int errcode_valid,
617 uint32_t errcode, int restart_instruction)
619 struct vm_exception exc;
623 exc.error_code = errcode;
624 exc.error_code_valid = errcode_valid;
625 exc.restart_instruction = restart_instruction;
627 return (ioctl(ctx->fd, VM_INJECT_EXCEPTION, &exc));
631 vm_apicid2vcpu(struct vmctx *ctx, int apicid)
634 * The apic id associated with the 'vcpu' has the same numerical value
635 * as the 'vcpu' itself.
641 vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
643 struct vm_lapic_irq vmirq;
645 bzero(&vmirq, sizeof(vmirq));
647 vmirq.vector = vector;
649 return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
653 vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector)
655 struct vm_lapic_irq vmirq;
657 bzero(&vmirq, sizeof(vmirq));
659 vmirq.vector = vector;
661 return (ioctl(ctx->fd, VM_LAPIC_LOCAL_IRQ, &vmirq));
665 vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg)
667 struct vm_lapic_msi vmmsi;
669 bzero(&vmmsi, sizeof(vmmsi));
673 return (ioctl(ctx->fd, VM_LAPIC_MSI, &vmmsi));
677 vm_ioapic_assert_irq(struct vmctx *ctx, int irq)
679 struct vm_ioapic_irq ioapic_irq;
681 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
682 ioapic_irq.irq = irq;
684 return (ioctl(ctx->fd, VM_IOAPIC_ASSERT_IRQ, &ioapic_irq));
688 vm_ioapic_deassert_irq(struct vmctx *ctx, int irq)
690 struct vm_ioapic_irq ioapic_irq;
692 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
693 ioapic_irq.irq = irq;
695 return (ioctl(ctx->fd, VM_IOAPIC_DEASSERT_IRQ, &ioapic_irq));
699 vm_ioapic_pulse_irq(struct vmctx *ctx, int irq)
701 struct vm_ioapic_irq ioapic_irq;
703 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
704 ioapic_irq.irq = irq;
706 return (ioctl(ctx->fd, VM_IOAPIC_PULSE_IRQ, &ioapic_irq));
710 vm_ioapic_pincount(struct vmctx *ctx, int *pincount)
713 return (ioctl(ctx->fd, VM_IOAPIC_PINCOUNT, pincount));
717 vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
719 struct vm_isa_irq isa_irq;
721 bzero(&isa_irq, sizeof(struct vm_isa_irq));
722 isa_irq.atpic_irq = atpic_irq;
723 isa_irq.ioapic_irq = ioapic_irq;
725 return (ioctl(ctx->fd, VM_ISA_ASSERT_IRQ, &isa_irq));
729 vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
731 struct vm_isa_irq isa_irq;
733 bzero(&isa_irq, sizeof(struct vm_isa_irq));
734 isa_irq.atpic_irq = atpic_irq;
735 isa_irq.ioapic_irq = ioapic_irq;
737 return (ioctl(ctx->fd, VM_ISA_DEASSERT_IRQ, &isa_irq));
741 vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
743 struct vm_isa_irq isa_irq;
745 bzero(&isa_irq, sizeof(struct vm_isa_irq));
746 isa_irq.atpic_irq = atpic_irq;
747 isa_irq.ioapic_irq = ioapic_irq;
749 return (ioctl(ctx->fd, VM_ISA_PULSE_IRQ, &isa_irq));
753 vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
754 enum vm_intr_trigger trigger)
756 struct vm_isa_irq_trigger isa_irq_trigger;
758 bzero(&isa_irq_trigger, sizeof(struct vm_isa_irq_trigger));
759 isa_irq_trigger.atpic_irq = atpic_irq;
760 isa_irq_trigger.trigger = trigger;
762 return (ioctl(ctx->fd, VM_ISA_SET_IRQ_TRIGGER, &isa_irq_trigger));
766 vm_inject_nmi(struct vmctx *ctx, int vcpu)
770 bzero(&vmnmi, sizeof(vmnmi));
773 return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
780 { "hlt_exit", VM_CAP_HALT_EXIT },
781 { "mtrap_exit", VM_CAP_MTRAP_EXIT },
782 { "pause_exit", VM_CAP_PAUSE_EXIT },
783 { "unrestricted_guest", VM_CAP_UNRESTRICTED_GUEST },
784 { "enable_invpcid", VM_CAP_ENABLE_INVPCID },
789 vm_capability_name2type(const char *capname)
793 for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) {
794 if (strcmp(capstrmap[i].name, capname) == 0)
795 return (capstrmap[i].type);
802 vm_capability_type2name(int type)
806 for (i = 0; capstrmap[i].name != NULL; i++) {
807 if (capstrmap[i].type == type)
808 return (capstrmap[i].name);
815 vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
819 struct vm_capability vmcap;
821 bzero(&vmcap, sizeof(vmcap));
825 error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
826 *retval = vmcap.capval;
831 vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
833 struct vm_capability vmcap;
835 bzero(&vmcap, sizeof(vmcap));
840 return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
844 vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
846 struct vm_pptdev pptdev;
848 bzero(&pptdev, sizeof(pptdev));
853 return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
857 vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
859 struct vm_pptdev pptdev;
861 bzero(&pptdev, sizeof(pptdev));
866 return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
870 vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
871 vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
873 struct vm_pptdev_mmio pptmmio;
875 bzero(&pptmmio, sizeof(pptmmio));
883 return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
887 vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
888 uint64_t addr, uint64_t msg, int numvec)
890 struct vm_pptdev_msi pptmsi;
892 bzero(&pptmsi, sizeof(pptmsi));
899 pptmsi.numvec = numvec;
901 return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
905 vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
906 int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
908 struct vm_pptdev_msix pptmsix;
910 bzero(&pptmsix, sizeof(pptmsix));
918 pptmsix.vector_control = vector_control;
920 return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
924 vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
929 static struct vm_stats vmstats;
931 vmstats.cpuid = vcpu;
933 error = ioctl(ctx->fd, VM_STATS, &vmstats);
936 *ret_entries = vmstats.num_entries;
938 *ret_tv = vmstats.tv;
939 return (vmstats.statbuf);
945 vm_get_stat_desc(struct vmctx *ctx, int index)
947 static struct vm_stat_desc statdesc;
949 statdesc.index = index;
950 if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
951 return (statdesc.desc);
957 vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state)
960 struct vm_x2apic x2apic;
962 bzero(&x2apic, sizeof(x2apic));
965 error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic);
966 *state = x2apic.state;
971 vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state)
974 struct vm_x2apic x2apic;
976 bzero(&x2apic, sizeof(x2apic));
978 x2apic.state = state;
980 error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic);
987 * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
990 vcpu_reset(struct vmctx *vmctx, int vcpu)
993 uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
994 uint32_t desc_access, desc_limit;
1000 error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
1005 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
1009 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
1012 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
1016 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
1020 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
1022 desc_base = 0xffff0000;
1023 desc_limit = 0xffff;
1024 desc_access = 0x0093;
1025 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
1026 desc_base, desc_limit, desc_access);
1031 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
1035 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
1038 desc_limit = 0xffff;
1039 desc_access = 0x0093;
1040 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
1041 desc_base, desc_limit, desc_access);
1045 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
1046 desc_base, desc_limit, desc_access);
1050 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
1051 desc_base, desc_limit, desc_access);
1055 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
1056 desc_base, desc_limit, desc_access);
1060 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
1061 desc_base, desc_limit, desc_access);
1066 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
1068 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
1070 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
1072 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
1074 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
1077 /* General purpose registers */
1079 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
1081 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
1083 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
1085 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
1087 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
1089 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
1091 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
1093 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
1098 desc_limit = 0xffff;
1100 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
1101 desc_base, desc_limit, desc_access);
1105 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
1106 desc_base, desc_limit, desc_access);
1112 desc_limit = 0xffff;
1113 desc_access = 0x0000008b;
1114 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
1119 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
1124 desc_limit = 0xffff;
1125 desc_access = 0x00000082;
1126 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
1127 desc_limit, desc_access);
1132 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
1135 /* XXX cr2, debug registers */
1143 vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num)
1146 struct vm_gpa_pte gpapte;
1148 bzero(&gpapte, sizeof(gpapte));
1151 error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte);
1154 *num = gpapte.ptenum;
1155 for (i = 0; i < gpapte.ptenum; i++)
1156 pte[i] = gpapte.pte[i];
1163 vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities)
1166 struct vm_hpet_cap cap;
1168 bzero(&cap, sizeof(struct vm_hpet_cap));
1169 error = ioctl(ctx->fd, VM_GET_HPET_CAPABILITIES, &cap);
1170 if (capabilities != NULL)
1171 *capabilities = cap.capabilities;
1176 vm_gla2gpa(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
1177 uint64_t gla, int prot, uint64_t *gpa, int *fault)
1179 struct vm_gla2gpa gg;
1182 bzero(&gg, sizeof(struct vm_gla2gpa));
1186 gg.paging = *paging;
1188 error = ioctl(ctx->fd, VM_GLA2GPA, &gg);
1197 #define min(a,b) (((a) < (b)) ? (a) : (b))
1201 vm_copy_setup(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
1202 uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt,
1207 int error, i, n, off;
1209 for (i = 0; i < iovcnt; i++) {
1210 iov[i].iov_base = 0;
1216 error = vm_gla2gpa(ctx, vcpu, paging, gla, prot, &gpa, fault);
1217 if (error || *fault)
1220 off = gpa & PAGE_MASK;
1221 n = min(len, PAGE_SIZE - off);
1223 va = vm_map_gpa(ctx, gpa, n);
1239 vm_copy_teardown(struct vmctx *ctx, int vcpu, struct iovec *iov, int iovcnt)
1246 vm_copyin(struct vmctx *ctx, int vcpu, struct iovec *iov, void *vp, size_t len)
1254 assert(iov->iov_len);
1255 n = min(len, iov->iov_len);
1256 src = iov->iov_base;
1266 vm_copyout(struct vmctx *ctx, int vcpu, const void *vp, struct iovec *iov,
1275 assert(iov->iov_len);
1276 n = min(len, iov->iov_len);
1277 dst = iov->iov_base;
1287 vm_get_cpus(struct vmctx *ctx, int which, cpuset_t *cpus)
1289 struct vm_cpuset vm_cpuset;
1292 bzero(&vm_cpuset, sizeof(struct vm_cpuset));
1293 vm_cpuset.which = which;
1294 vm_cpuset.cpusetsize = sizeof(cpuset_t);
1295 vm_cpuset.cpus = cpus;
1297 error = ioctl(ctx->fd, VM_GET_CPUS, &vm_cpuset);
1302 vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus)
1305 return (vm_get_cpus(ctx, VM_ACTIVE_CPUS, cpus));
1309 vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus)
1312 return (vm_get_cpus(ctx, VM_SUSPENDED_CPUS, cpus));
1316 vm_activate_cpu(struct vmctx *ctx, int vcpu)
1318 struct vm_activate_cpu ac;
1321 bzero(&ac, sizeof(struct vm_activate_cpu));
1323 error = ioctl(ctx->fd, VM_ACTIVATE_CPU, &ac);
1328 vm_get_intinfo(struct vmctx *ctx, int vcpu, uint64_t *info1, uint64_t *info2)
1330 struct vm_intinfo vmii;
1333 bzero(&vmii, sizeof(struct vm_intinfo));
1335 error = ioctl(ctx->fd, VM_GET_INTINFO, &vmii);
1337 *info1 = vmii.info1;
1338 *info2 = vmii.info2;
1344 vm_set_intinfo(struct vmctx *ctx, int vcpu, uint64_t info1)
1346 struct vm_intinfo vmii;
1349 bzero(&vmii, sizeof(struct vm_intinfo));
1352 error = ioctl(ctx->fd, VM_SET_INTINFO, &vmii);
1357 vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value)
1359 struct vm_rtc_data rtcdata;
1362 bzero(&rtcdata, sizeof(struct vm_rtc_data));
1363 rtcdata.offset = offset;
1364 rtcdata.value = value;
1365 error = ioctl(ctx->fd, VM_RTC_WRITE, &rtcdata);
1370 vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval)
1372 struct vm_rtc_data rtcdata;
1375 bzero(&rtcdata, sizeof(struct vm_rtc_data));
1376 rtcdata.offset = offset;
1377 error = ioctl(ctx->fd, VM_RTC_READ, &rtcdata);
1379 *retval = rtcdata.value;
1384 vm_rtc_settime(struct vmctx *ctx, time_t secs)
1386 struct vm_rtc_time rtctime;
1389 bzero(&rtctime, sizeof(struct vm_rtc_time));
1390 rtctime.secs = secs;
1391 error = ioctl(ctx->fd, VM_RTC_SETTIME, &rtctime);
1396 vm_rtc_gettime(struct vmctx *ctx, time_t *secs)
1398 struct vm_rtc_time rtctime;
1401 bzero(&rtctime, sizeof(struct vm_rtc_time));
1402 error = ioctl(ctx->fd, VM_RTC_GETTIME, &rtctime);
1404 *secs = rtctime.secs;
1409 vm_restart_instruction(void *arg, int vcpu)
1411 struct vmctx *ctx = arg;
1413 return (ioctl(ctx->fd, VM_RESTART_INSTRUCTION, &vcpu));