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/types.h>
33 #include <sys/sysctl.h>
34 #include <sys/ioctl.h>
37 #include <machine/specialreg.h>
48 #include <machine/vmm.h>
49 #include <machine/vmm_dev.h>
53 #define MB (1024 * 1024UL)
54 #define GB (1024 * 1024 * 1024UL)
58 uint32_t lowmem_limit;
59 enum vm_mmap_style vms;
67 #define CREATE(x) sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x)))
68 #define DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x)))
71 vm_device_open(const char *name)
76 len = strlen("/dev/vmm/") + strlen(name) + 1;
78 assert(vmfile != NULL);
79 snprintf(vmfile, len, "/dev/vmm/%s", name);
81 /* Open the device file */
82 fd = open(vmfile, O_RDWR, 0);
89 vm_create(const char *name)
92 return (CREATE((char *)name));
96 vm_open(const char *name)
100 vm = malloc(sizeof(struct vmctx) + strlen(name) + 1);
104 vm->lowmem_limit = 3 * GB;
105 vm->name = (char *)(vm + 1);
106 strcpy(vm->name, name);
108 if ((vm->fd = vm_device_open(vm->name)) < 0)
118 vm_destroy(struct vmctx *vm)
130 vm_parse_memsize(const char *optarg, size_t *ret_memsize)
136 optval = strtoul(optarg, &endptr, 0);
137 if (*optarg != '\0' && *endptr == '\0') {
139 * For the sake of backward compatibility if the memory size
140 * specified on the command line is less than a megabyte then
141 * it is interpreted as being in units of MB.
145 *ret_memsize = optval;
148 error = expand_number(optarg, ret_memsize);
154 vm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, size_t *ret_len,
158 struct vm_memory_segment seg;
160 bzero(&seg, sizeof(seg));
162 error = ioctl(ctx->fd, VM_GET_MEMORY_SEG, &seg);
170 vm_get_lowmem_limit(struct vmctx *ctx)
173 return (ctx->lowmem_limit);
177 vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit)
180 ctx->lowmem_limit = limit;
184 setup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **addr)
187 struct vm_memory_segment seg;
190 * Create and optionally map 'len' bytes of memory at guest
191 * physical address 'gpa'
193 bzero(&seg, sizeof(seg));
196 error = ioctl(ctx->fd, VM_MAP_MEMORY, &seg);
197 if (error == 0 && addr != NULL) {
198 *addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
205 vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms)
210 /* XXX VM_MMAP_SPARSE not implemented yet */
211 assert(vms == VM_MMAP_NONE || vms == VM_MMAP_ALL);
215 * If 'memsize' cannot fit entirely in the 'lowmem' segment then
216 * create another 'highmem' segment above 4GB for the remainder.
218 if (memsize > ctx->lowmem_limit) {
219 ctx->lowmem = ctx->lowmem_limit;
220 ctx->highmem = memsize - ctx->lowmem;
222 ctx->lowmem = memsize;
226 if (ctx->lowmem > 0) {
227 addr = (vms == VM_MMAP_ALL) ? &ctx->lowmem_addr : NULL;
228 error = setup_memory_segment(ctx, 0, ctx->lowmem, addr);
233 if (ctx->highmem > 0) {
234 addr = (vms == VM_MMAP_ALL) ? &ctx->highmem_addr : NULL;
235 error = setup_memory_segment(ctx, 4*GB, ctx->highmem, addr);
244 vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len)
247 /* XXX VM_MMAP_SPARSE not implemented yet */
248 assert(ctx->vms == VM_MMAP_ALL);
250 if (gaddr < ctx->lowmem && gaddr + len <= ctx->lowmem)
251 return ((void *)(ctx->lowmem_addr + gaddr));
255 if (gaddr < ctx->highmem && gaddr + len <= ctx->highmem)
256 return ((void *)(ctx->highmem_addr + gaddr));
263 vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
264 uint64_t base, uint32_t limit, uint32_t access)
267 struct vm_seg_desc vmsegdesc;
269 bzero(&vmsegdesc, sizeof(vmsegdesc));
270 vmsegdesc.cpuid = vcpu;
271 vmsegdesc.regnum = reg;
272 vmsegdesc.desc.base = base;
273 vmsegdesc.desc.limit = limit;
274 vmsegdesc.desc.access = access;
276 error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
281 vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
282 uint64_t *base, uint32_t *limit, uint32_t *access)
285 struct vm_seg_desc vmsegdesc;
287 bzero(&vmsegdesc, sizeof(vmsegdesc));
288 vmsegdesc.cpuid = vcpu;
289 vmsegdesc.regnum = reg;
291 error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
293 *base = vmsegdesc.desc.base;
294 *limit = vmsegdesc.desc.limit;
295 *access = vmsegdesc.desc.access;
301 vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
304 struct vm_register vmreg;
306 bzero(&vmreg, sizeof(vmreg));
311 error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg);
316 vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val)
319 struct vm_register vmreg;
321 bzero(&vmreg, sizeof(vmreg));
325 error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg);
326 *ret_val = vmreg.regval;
331 vm_run(struct vmctx *ctx, int vcpu, uint64_t rip, struct vm_exit *vmexit)
336 bzero(&vmrun, sizeof(vmrun));
340 error = ioctl(ctx->fd, VM_RUN, &vmrun);
341 bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit));
346 vm_inject_event_real(struct vmctx *ctx, int vcpu, enum vm_event_type type,
347 int vector, int error_code, int error_code_valid)
351 bzero(&ev, sizeof(ev));
355 ev.error_code = error_code;
356 ev.error_code_valid = error_code_valid;
358 return (ioctl(ctx->fd, VM_INJECT_EVENT, &ev));
362 vm_inject_event(struct vmctx *ctx, int vcpu, enum vm_event_type type,
366 return (vm_inject_event_real(ctx, vcpu, type, vector, 0, 0));
370 vm_inject_event2(struct vmctx *ctx, int vcpu, enum vm_event_type type,
371 int vector, int error_code)
374 return (vm_inject_event_real(ctx, vcpu, type, vector, error_code, 1));
378 vm_apicid2vcpu(struct vmctx *ctx, int apicid)
381 * The apic id associated with the 'vcpu' has the same numerical value
382 * as the 'vcpu' itself.
388 vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
390 struct vm_lapic_irq vmirq;
392 bzero(&vmirq, sizeof(vmirq));
394 vmirq.vector = vector;
396 return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
400 vm_inject_nmi(struct vmctx *ctx, int vcpu)
404 bzero(&vmnmi, sizeof(vmnmi));
407 return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
414 { "hlt_exit", VM_CAP_HALT_EXIT },
415 { "mtrap_exit", VM_CAP_MTRAP_EXIT },
416 { "pause_exit", VM_CAP_PAUSE_EXIT },
417 { "unrestricted_guest", VM_CAP_UNRESTRICTED_GUEST },
418 { "enable_invpcid", VM_CAP_ENABLE_INVPCID },
423 vm_capability_name2type(const char *capname)
427 for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) {
428 if (strcmp(capstrmap[i].name, capname) == 0)
429 return (capstrmap[i].type);
436 vm_capability_type2name(int type)
440 for (i = 0; capstrmap[i].name != NULL; i++) {
441 if (capstrmap[i].type == type)
442 return (capstrmap[i].name);
449 vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
453 struct vm_capability vmcap;
455 bzero(&vmcap, sizeof(vmcap));
459 error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
460 *retval = vmcap.capval;
465 vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
467 struct vm_capability vmcap;
469 bzero(&vmcap, sizeof(vmcap));
474 return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
478 vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
480 struct vm_pptdev pptdev;
482 bzero(&pptdev, sizeof(pptdev));
487 return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
491 vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
493 struct vm_pptdev pptdev;
495 bzero(&pptdev, sizeof(pptdev));
500 return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
504 vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
505 vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
507 struct vm_pptdev_mmio pptmmio;
509 bzero(&pptmmio, sizeof(pptmmio));
517 return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
521 vm_setup_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
522 int destcpu, int vector, int numvec)
524 struct vm_pptdev_msi pptmsi;
526 bzero(&pptmsi, sizeof(pptmsi));
531 pptmsi.destcpu = destcpu;
532 pptmsi.vector = vector;
533 pptmsi.numvec = numvec;
535 return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
539 vm_setup_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
540 int idx, uint32_t msg, uint32_t vector_control, uint64_t addr)
542 struct vm_pptdev_msix pptmsix;
544 bzero(&pptmsix, sizeof(pptmsix));
552 pptmsix.vector_control = vector_control;
554 return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
558 vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
563 static struct vm_stats vmstats;
565 vmstats.cpuid = vcpu;
567 error = ioctl(ctx->fd, VM_STATS, &vmstats);
570 *ret_entries = vmstats.num_entries;
572 *ret_tv = vmstats.tv;
573 return (vmstats.statbuf);
579 vm_get_stat_desc(struct vmctx *ctx, int index)
581 static struct vm_stat_desc statdesc;
583 statdesc.index = index;
584 if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
585 return (statdesc.desc);
591 vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state)
594 struct vm_x2apic x2apic;
596 bzero(&x2apic, sizeof(x2apic));
599 error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic);
600 *state = x2apic.state;
605 vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state)
608 struct vm_x2apic x2apic;
610 bzero(&x2apic, sizeof(x2apic));
612 x2apic.state = state;
614 error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic);
621 * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
624 vcpu_reset(struct vmctx *vmctx, int vcpu)
627 uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
628 uint32_t desc_access, desc_limit;
634 error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
639 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
643 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
646 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
650 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
654 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
656 desc_base = 0xffff0000;
658 desc_access = 0x0093;
659 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
660 desc_base, desc_limit, desc_access);
665 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
669 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
673 desc_access = 0x0093;
674 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
675 desc_base, desc_limit, desc_access);
679 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
680 desc_base, desc_limit, desc_access);
684 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
685 desc_base, desc_limit, desc_access);
689 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
690 desc_base, desc_limit, desc_access);
694 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
695 desc_base, desc_limit, desc_access);
700 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
702 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
704 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
706 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
708 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
711 /* General purpose registers */
713 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
715 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
717 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
719 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
721 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
723 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
725 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
727 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
734 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
735 desc_base, desc_limit, desc_access);
739 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
740 desc_base, desc_limit, desc_access);
747 desc_access = 0x0000008b;
748 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
753 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
759 desc_access = 0x00000082;
760 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
761 desc_limit, desc_access);
766 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
769 /* XXX cr2, debug registers */
777 vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num)
780 struct vm_gpa_pte gpapte;
782 bzero(&gpapte, sizeof(gpapte));
785 error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte);
788 *num = gpapte.ptenum;
789 for (i = 0; i < gpapte.ptenum; i++)
790 pte[i] = gpapte.pte[i];