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>
56 #define CREATE(x) sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x)))
57 #define DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x)))
60 vm_device_open(const char *name)
65 len = strlen("/dev/vmm/") + strlen(name) + 1;
67 assert(vmfile != NULL);
68 snprintf(vmfile, len, "/dev/vmm/%s", name);
70 /* Open the device file */
71 fd = open(vmfile, O_RDWR, 0);
78 vm_create(const char *name)
81 return (CREATE((char *)name));
85 vm_open(const char *name)
89 vm = malloc(sizeof(struct vmctx) + strlen(name) + 1);
93 vm->name = (char *)(vm + 1);
94 strcpy(vm->name, name);
96 if ((vm->fd = vm_device_open(vm->name)) < 0)
106 vm_destroy(struct vmctx *vm)
118 vmm_get_mem_total(void)
120 size_t mem_total = 0;
121 size_t oldlen = sizeof(mem_total);
123 error = sysctlbyname("hw.vmm.mem_total", &mem_total, &oldlen, NULL, 0);
130 vmm_get_mem_free(void)
133 size_t oldlen = sizeof(mem_free);
135 error = sysctlbyname("hw.vmm.mem_free", &mem_free, &oldlen, NULL, 0);
142 vm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, size_t *ret_len)
145 struct vm_memory_segment seg;
147 bzero(&seg, sizeof(seg));
149 error = ioctl(ctx->fd, VM_GET_MEMORY_SEG, &seg);
155 vm_setup_memory(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **mapaddr)
158 struct vm_memory_segment seg;
161 * Create and optionally map 'len' bytes of memory at guest
162 * physical address 'gpa'
164 bzero(&seg, sizeof(seg));
167 error = ioctl(ctx->fd, VM_MAP_MEMORY, &seg);
168 if (error == 0 && mapaddr != NULL) {
169 *mapaddr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
176 vm_map_memory(struct vmctx *ctx, vm_paddr_t gpa, size_t len)
179 /* Map 'len' bytes of memory at guest physical address 'gpa' */
180 return ((char *)mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
185 vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
186 uint64_t base, uint32_t limit, uint32_t access)
189 struct vm_seg_desc vmsegdesc;
191 bzero(&vmsegdesc, sizeof(vmsegdesc));
192 vmsegdesc.cpuid = vcpu;
193 vmsegdesc.regnum = reg;
194 vmsegdesc.desc.base = base;
195 vmsegdesc.desc.limit = limit;
196 vmsegdesc.desc.access = access;
198 error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
203 vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
204 uint64_t *base, uint32_t *limit, uint32_t *access)
207 struct vm_seg_desc vmsegdesc;
209 bzero(&vmsegdesc, sizeof(vmsegdesc));
210 vmsegdesc.cpuid = vcpu;
211 vmsegdesc.regnum = reg;
213 error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
215 *base = vmsegdesc.desc.base;
216 *limit = vmsegdesc.desc.limit;
217 *access = vmsegdesc.desc.access;
223 vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
226 struct vm_register vmreg;
228 bzero(&vmreg, sizeof(vmreg));
233 error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg);
238 vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val)
241 struct vm_register vmreg;
243 bzero(&vmreg, sizeof(vmreg));
247 error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg);
248 *ret_val = vmreg.regval;
253 vm_run(struct vmctx *ctx, int vcpu, uint64_t rip, struct vm_exit *vmexit)
258 bzero(&vmrun, sizeof(vmrun));
262 error = ioctl(ctx->fd, VM_RUN, &vmrun);
263 bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit));
268 vm_inject_event_real(struct vmctx *ctx, int vcpu, enum vm_event_type type,
269 int vector, int error_code, int error_code_valid)
273 bzero(&ev, sizeof(ev));
277 ev.error_code = error_code;
278 ev.error_code_valid = error_code_valid;
280 return (ioctl(ctx->fd, VM_INJECT_EVENT, &ev));
284 vm_inject_event(struct vmctx *ctx, int vcpu, enum vm_event_type type,
288 return (vm_inject_event_real(ctx, vcpu, type, vector, 0, 0));
292 vm_inject_event2(struct vmctx *ctx, int vcpu, enum vm_event_type type,
293 int vector, int error_code)
296 return (vm_inject_event_real(ctx, vcpu, type, vector, error_code, 1));
300 vm_apicid2vcpu(struct vmctx *ctx, int apicid)
303 * The apic id associated with the 'vcpu' has the same numerical value
304 * as the 'vcpu' itself.
310 vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
312 struct vm_lapic_irq vmirq;
314 bzero(&vmirq, sizeof(vmirq));
316 vmirq.vector = vector;
318 return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
322 vm_inject_nmi(struct vmctx *ctx, int vcpu)
326 bzero(&vmnmi, sizeof(vmnmi));
329 return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
336 { "hlt_exit", VM_CAP_HALT_EXIT },
337 { "mtrap_exit", VM_CAP_MTRAP_EXIT },
338 { "pause_exit", VM_CAP_PAUSE_EXIT },
339 { "unrestricted_guest", VM_CAP_UNRESTRICTED_GUEST },
344 vm_capability_name2type(const char *capname)
348 for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) {
349 if (strcmp(capstrmap[i].name, capname) == 0)
350 return (capstrmap[i].type);
357 vm_capability_type2name(int type)
361 for (i = 0; capstrmap[i].name != NULL; i++) {
362 if (capstrmap[i].type == type)
363 return (capstrmap[i].name);
370 vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
374 struct vm_capability vmcap;
376 bzero(&vmcap, sizeof(vmcap));
380 error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
381 *retval = vmcap.capval;
386 vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
388 struct vm_capability vmcap;
390 bzero(&vmcap, sizeof(vmcap));
395 return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
399 vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
401 struct vm_pptdev pptdev;
403 bzero(&pptdev, sizeof(pptdev));
408 return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
412 vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
414 struct vm_pptdev pptdev;
416 bzero(&pptdev, sizeof(pptdev));
421 return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
425 vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
426 vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
428 struct vm_pptdev_mmio pptmmio;
430 bzero(&pptmmio, sizeof(pptmmio));
438 return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
442 vm_setup_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
443 int destcpu, int vector, int numvec)
445 struct vm_pptdev_msi pptmsi;
447 bzero(&pptmsi, sizeof(pptmsi));
452 pptmsi.destcpu = destcpu;
453 pptmsi.vector = vector;
454 pptmsi.numvec = numvec;
456 return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
460 vm_setup_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
461 int idx, uint32_t msg, uint32_t vector_control, uint64_t addr)
463 struct vm_pptdev_msix pptmsix;
465 bzero(&pptmsix, sizeof(pptmsix));
473 pptmsix.vector_control = vector_control;
475 return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
479 vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
484 static struct vm_stats vmstats;
486 vmstats.cpuid = vcpu;
488 error = ioctl(ctx->fd, VM_STATS, &vmstats);
491 *ret_entries = vmstats.num_entries;
493 *ret_tv = vmstats.tv;
494 return (vmstats.statbuf);
500 vm_get_stat_desc(struct vmctx *ctx, int index)
502 static struct vm_stat_desc statdesc;
504 statdesc.index = index;
505 if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
506 return (statdesc.desc);
512 vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state)
515 struct vm_x2apic x2apic;
517 bzero(&x2apic, sizeof(x2apic));
520 error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic);
521 *state = x2apic.state;
526 vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state)
529 struct vm_x2apic x2apic;
531 bzero(&x2apic, sizeof(x2apic));
533 x2apic.state = state;
535 error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic);
542 * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
545 vcpu_reset(struct vmctx *vmctx, int vcpu)
548 uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
549 uint32_t desc_access, desc_limit;
555 error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
560 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
564 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
567 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
571 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
575 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
577 desc_base = 0xffff0000;
579 desc_access = 0x0093;
580 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
581 desc_base, desc_limit, desc_access);
586 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
590 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
594 desc_access = 0x0093;
595 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
596 desc_base, desc_limit, desc_access);
600 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
601 desc_base, desc_limit, desc_access);
605 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
606 desc_base, desc_limit, desc_access);
610 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
611 desc_base, desc_limit, desc_access);
615 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
616 desc_base, desc_limit, desc_access);
621 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
623 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
625 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
627 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
629 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
632 /* General purpose registers */
634 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
636 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
638 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
640 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
642 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
644 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
646 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
648 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
655 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
656 desc_base, desc_limit, desc_access);
660 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
661 desc_base, desc_limit, desc_access);
668 desc_access = 0x0000008b;
669 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
674 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
680 desc_access = 0x00000082;
681 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
682 desc_limit, desc_access);
687 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
690 /* XXX cr2, debug registers */