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>
46 #include <machine/vmm.h>
47 #include <machine/vmm_dev.h>
51 #define GB (1024 * 1024 * 1024UL)
55 uint32_t lowmem_limit;
56 enum vm_mmap_style vms;
64 #define CREATE(x) sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x)))
65 #define DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x)))
68 vm_device_open(const char *name)
73 len = strlen("/dev/vmm/") + strlen(name) + 1;
75 assert(vmfile != NULL);
76 snprintf(vmfile, len, "/dev/vmm/%s", name);
78 /* Open the device file */
79 fd = open(vmfile, O_RDWR, 0);
86 vm_create(const char *name)
89 return (CREATE((char *)name));
93 vm_open(const char *name)
97 vm = malloc(sizeof(struct vmctx) + strlen(name) + 1);
101 vm->lowmem_limit = 3 * GB;
102 vm->name = (char *)(vm + 1);
103 strcpy(vm->name, name);
105 if ((vm->fd = vm_device_open(vm->name)) < 0)
115 vm_destroy(struct vmctx *vm)
127 vm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, size_t *ret_len)
130 struct vm_memory_segment seg;
132 bzero(&seg, sizeof(seg));
134 error = ioctl(ctx->fd, VM_GET_MEMORY_SEG, &seg);
140 vm_get_lowmem_limit(struct vmctx *ctx)
143 return (ctx->lowmem_limit);
147 vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit)
150 ctx->lowmem_limit = limit;
154 setup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **addr)
157 struct vm_memory_segment seg;
160 * Create and optionally map 'len' bytes of memory at guest
161 * physical address 'gpa'
163 bzero(&seg, sizeof(seg));
166 error = ioctl(ctx->fd, VM_MAP_MEMORY, &seg);
167 if (error == 0 && addr != NULL) {
168 *addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
175 vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms)
180 /* XXX VM_MMAP_SPARSE not implemented yet */
181 assert(vms == VM_MMAP_NONE || vms == VM_MMAP_ALL);
185 * If 'memsize' cannot fit entirely in the 'lowmem' segment then
186 * create another 'highmem' segment above 4GB for the remainder.
188 if (memsize > ctx->lowmem_limit) {
189 ctx->lowmem = ctx->lowmem_limit;
190 ctx->highmem = memsize - ctx->lowmem;
192 ctx->lowmem = memsize;
196 if (ctx->lowmem > 0) {
197 addr = (vms == VM_MMAP_ALL) ? &ctx->lowmem_addr : NULL;
198 error = setup_memory_segment(ctx, 0, ctx->lowmem, addr);
203 if (ctx->highmem > 0) {
204 addr = (vms == VM_MMAP_ALL) ? &ctx->highmem_addr : NULL;
205 error = setup_memory_segment(ctx, 4*GB, ctx->highmem, addr);
214 vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len)
217 /* XXX VM_MMAP_SPARSE not implemented yet */
218 assert(ctx->vms == VM_MMAP_ALL);
220 if (gaddr < ctx->lowmem && gaddr + len <= ctx->lowmem)
221 return ((void *)(ctx->lowmem_addr + gaddr));
225 if (gaddr < ctx->highmem && gaddr + len <= ctx->highmem)
226 return ((void *)(ctx->highmem_addr + gaddr));
233 vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
234 uint64_t base, uint32_t limit, uint32_t access)
237 struct vm_seg_desc vmsegdesc;
239 bzero(&vmsegdesc, sizeof(vmsegdesc));
240 vmsegdesc.cpuid = vcpu;
241 vmsegdesc.regnum = reg;
242 vmsegdesc.desc.base = base;
243 vmsegdesc.desc.limit = limit;
244 vmsegdesc.desc.access = access;
246 error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
251 vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
252 uint64_t *base, uint32_t *limit, uint32_t *access)
255 struct vm_seg_desc vmsegdesc;
257 bzero(&vmsegdesc, sizeof(vmsegdesc));
258 vmsegdesc.cpuid = vcpu;
259 vmsegdesc.regnum = reg;
261 error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
263 *base = vmsegdesc.desc.base;
264 *limit = vmsegdesc.desc.limit;
265 *access = vmsegdesc.desc.access;
271 vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
274 struct vm_register vmreg;
276 bzero(&vmreg, sizeof(vmreg));
281 error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg);
286 vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val)
289 struct vm_register vmreg;
291 bzero(&vmreg, sizeof(vmreg));
295 error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg);
296 *ret_val = vmreg.regval;
301 vm_run(struct vmctx *ctx, int vcpu, uint64_t rip, struct vm_exit *vmexit)
306 bzero(&vmrun, sizeof(vmrun));
310 error = ioctl(ctx->fd, VM_RUN, &vmrun);
311 bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit));
316 vm_inject_event_real(struct vmctx *ctx, int vcpu, enum vm_event_type type,
317 int vector, int error_code, int error_code_valid)
321 bzero(&ev, sizeof(ev));
325 ev.error_code = error_code;
326 ev.error_code_valid = error_code_valid;
328 return (ioctl(ctx->fd, VM_INJECT_EVENT, &ev));
332 vm_inject_event(struct vmctx *ctx, int vcpu, enum vm_event_type type,
336 return (vm_inject_event_real(ctx, vcpu, type, vector, 0, 0));
340 vm_inject_event2(struct vmctx *ctx, int vcpu, enum vm_event_type type,
341 int vector, int error_code)
344 return (vm_inject_event_real(ctx, vcpu, type, vector, error_code, 1));
348 vm_apicid2vcpu(struct vmctx *ctx, int apicid)
351 * The apic id associated with the 'vcpu' has the same numerical value
352 * as the 'vcpu' itself.
358 vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
360 struct vm_lapic_irq vmirq;
362 bzero(&vmirq, sizeof(vmirq));
364 vmirq.vector = vector;
366 return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
370 vm_inject_nmi(struct vmctx *ctx, int vcpu)
374 bzero(&vmnmi, sizeof(vmnmi));
377 return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
384 { "hlt_exit", VM_CAP_HALT_EXIT },
385 { "mtrap_exit", VM_CAP_MTRAP_EXIT },
386 { "pause_exit", VM_CAP_PAUSE_EXIT },
387 { "unrestricted_guest", VM_CAP_UNRESTRICTED_GUEST },
392 vm_capability_name2type(const char *capname)
396 for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) {
397 if (strcmp(capstrmap[i].name, capname) == 0)
398 return (capstrmap[i].type);
405 vm_capability_type2name(int type)
409 for (i = 0; capstrmap[i].name != NULL; i++) {
410 if (capstrmap[i].type == type)
411 return (capstrmap[i].name);
418 vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
422 struct vm_capability vmcap;
424 bzero(&vmcap, sizeof(vmcap));
428 error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
429 *retval = vmcap.capval;
434 vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
436 struct vm_capability vmcap;
438 bzero(&vmcap, sizeof(vmcap));
443 return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
447 vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
449 struct vm_pptdev pptdev;
451 bzero(&pptdev, sizeof(pptdev));
456 return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
460 vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
462 struct vm_pptdev pptdev;
464 bzero(&pptdev, sizeof(pptdev));
469 return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
473 vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
474 vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
476 struct vm_pptdev_mmio pptmmio;
478 bzero(&pptmmio, sizeof(pptmmio));
486 return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
490 vm_setup_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
491 int destcpu, int vector, int numvec)
493 struct vm_pptdev_msi pptmsi;
495 bzero(&pptmsi, sizeof(pptmsi));
500 pptmsi.destcpu = destcpu;
501 pptmsi.vector = vector;
502 pptmsi.numvec = numvec;
504 return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
508 vm_setup_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
509 int idx, uint32_t msg, uint32_t vector_control, uint64_t addr)
511 struct vm_pptdev_msix pptmsix;
513 bzero(&pptmsix, sizeof(pptmsix));
521 pptmsix.vector_control = vector_control;
523 return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
527 vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
532 static struct vm_stats vmstats;
534 vmstats.cpuid = vcpu;
536 error = ioctl(ctx->fd, VM_STATS, &vmstats);
539 *ret_entries = vmstats.num_entries;
541 *ret_tv = vmstats.tv;
542 return (vmstats.statbuf);
548 vm_get_stat_desc(struct vmctx *ctx, int index)
550 static struct vm_stat_desc statdesc;
552 statdesc.index = index;
553 if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
554 return (statdesc.desc);
560 vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state)
563 struct vm_x2apic x2apic;
565 bzero(&x2apic, sizeof(x2apic));
568 error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic);
569 *state = x2apic.state;
574 vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state)
577 struct vm_x2apic x2apic;
579 bzero(&x2apic, sizeof(x2apic));
581 x2apic.state = state;
583 error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic);
590 * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
593 vcpu_reset(struct vmctx *vmctx, int vcpu)
596 uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
597 uint32_t desc_access, desc_limit;
603 error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
608 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
612 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
615 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
619 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
623 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
625 desc_base = 0xffff0000;
627 desc_access = 0x0093;
628 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
629 desc_base, desc_limit, desc_access);
634 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
638 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
642 desc_access = 0x0093;
643 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
644 desc_base, desc_limit, desc_access);
648 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
649 desc_base, desc_limit, desc_access);
653 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
654 desc_base, desc_limit, desc_access);
658 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
659 desc_base, desc_limit, desc_access);
663 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
664 desc_base, desc_limit, desc_access);
669 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
671 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
673 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
675 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
677 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
680 /* General purpose registers */
682 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
684 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
686 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
688 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
690 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
692 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
694 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
696 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
703 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
704 desc_base, desc_limit, desc_access);
708 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
709 desc_base, desc_limit, desc_access);
716 desc_access = 0x0000008b;
717 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
722 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
728 desc_access = 0x00000082;
729 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
730 desc_limit, desc_access);
735 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
738 /* XXX cr2, debug registers */