vmmapi.c revision 256072
1221828Sgrehan/*- 2221828Sgrehan * Copyright (c) 2011 NetApp, Inc. 3221828Sgrehan * All rights reserved. 4221828Sgrehan * 5221828Sgrehan * Redistribution and use in source and binary forms, with or without 6221828Sgrehan * modification, are permitted provided that the following conditions 7221828Sgrehan * are met: 8221828Sgrehan * 1. Redistributions of source code must retain the above copyright 9221828Sgrehan * notice, this list of conditions and the following disclaimer. 10221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright 11221828Sgrehan * notice, this list of conditions and the following disclaimer in the 12221828Sgrehan * documentation and/or other materials provided with the distribution. 13221828Sgrehan * 14221828Sgrehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 15221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17221828Sgrehan * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 18221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20221828Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24221828Sgrehan * SUCH DAMAGE. 25221828Sgrehan * 26221828Sgrehan * $FreeBSD: head/lib/libvmmapi/vmmapi.c 256072 2013-10-05 21:22:35Z neel $ 27221828Sgrehan */ 28221828Sgrehan 29221828Sgrehan#include <sys/cdefs.h> 30221828Sgrehan__FBSDID("$FreeBSD: head/lib/libvmmapi/vmmapi.c 256072 2013-10-05 21:22:35Z neel $"); 31221828Sgrehan 32221828Sgrehan#include <sys/types.h> 33221828Sgrehan#include <sys/sysctl.h> 34221828Sgrehan#include <sys/ioctl.h> 35221828Sgrehan#include <sys/mman.h> 36221828Sgrehan 37221828Sgrehan#include <machine/specialreg.h> 38221828Sgrehan 39221828Sgrehan#include <stdio.h> 40221828Sgrehan#include <stdlib.h> 41221828Sgrehan#include <assert.h> 42221828Sgrehan#include <string.h> 43221828Sgrehan#include <fcntl.h> 44221828Sgrehan#include <unistd.h> 45221828Sgrehan 46221828Sgrehan#include <machine/vmm.h> 47221828Sgrehan#include <machine/vmm_dev.h> 48221828Sgrehan 49221828Sgrehan#include "vmmapi.h" 50221828Sgrehan 51248477Sneel#define GB (1024 * 1024 * 1024UL) 52248477Sneel 53221828Sgrehanstruct vmctx { 54221828Sgrehan int fd; 55248477Sneel uint32_t lowmem_limit; 56248477Sneel enum vm_mmap_style vms; 57248477Sneel size_t lowmem; 58248477Sneel char *lowmem_addr; 59248477Sneel size_t highmem; 60248477Sneel char *highmem_addr; 61221828Sgrehan char *name; 62221828Sgrehan}; 63221828Sgrehan 64221828Sgrehan#define CREATE(x) sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x))) 65221828Sgrehan#define DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x))) 66221828Sgrehan 67221828Sgrehanstatic int 68221828Sgrehanvm_device_open(const char *name) 69221828Sgrehan{ 70221828Sgrehan int fd, len; 71221828Sgrehan char *vmfile; 72221828Sgrehan 73221828Sgrehan len = strlen("/dev/vmm/") + strlen(name) + 1; 74221828Sgrehan vmfile = malloc(len); 75221828Sgrehan assert(vmfile != NULL); 76221828Sgrehan snprintf(vmfile, len, "/dev/vmm/%s", name); 77221828Sgrehan 78221828Sgrehan /* Open the device file */ 79221828Sgrehan fd = open(vmfile, O_RDWR, 0); 80221828Sgrehan 81221828Sgrehan free(vmfile); 82221828Sgrehan return (fd); 83221828Sgrehan} 84221828Sgrehan 85221828Sgrehanint 86221828Sgrehanvm_create(const char *name) 87221828Sgrehan{ 88221828Sgrehan 89221828Sgrehan return (CREATE((char *)name)); 90221828Sgrehan} 91221828Sgrehan 92221828Sgrehanstruct vmctx * 93221828Sgrehanvm_open(const char *name) 94221828Sgrehan{ 95221828Sgrehan struct vmctx *vm; 96221828Sgrehan 97221828Sgrehan vm = malloc(sizeof(struct vmctx) + strlen(name) + 1); 98221828Sgrehan assert(vm != NULL); 99221828Sgrehan 100221828Sgrehan vm->fd = -1; 101248477Sneel vm->lowmem_limit = 3 * GB; 102221828Sgrehan vm->name = (char *)(vm + 1); 103221828Sgrehan strcpy(vm->name, name); 104221828Sgrehan 105221828Sgrehan if ((vm->fd = vm_device_open(vm->name)) < 0) 106221828Sgrehan goto err; 107221828Sgrehan 108221828Sgrehan return (vm); 109221828Sgrehanerr: 110221828Sgrehan vm_destroy(vm); 111221828Sgrehan return (NULL); 112221828Sgrehan} 113221828Sgrehan 114221828Sgrehanvoid 115221828Sgrehanvm_destroy(struct vmctx *vm) 116221828Sgrehan{ 117221828Sgrehan assert(vm != NULL); 118221828Sgrehan 119221828Sgrehan if (vm->fd >= 0) 120221828Sgrehan close(vm->fd); 121241178Sneel DESTROY(vm->name); 122241178Sneel 123221828Sgrehan free(vm); 124221828Sgrehan} 125221828Sgrehan 126221828Sgrehanint 127256072Sneelvm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, size_t *ret_len, 128256072Sneel int *wired) 129221828Sgrehan{ 130221828Sgrehan int error; 131221828Sgrehan struct vm_memory_segment seg; 132221828Sgrehan 133221828Sgrehan bzero(&seg, sizeof(seg)); 134221828Sgrehan seg.gpa = gpa; 135221828Sgrehan error = ioctl(ctx->fd, VM_GET_MEMORY_SEG, &seg); 136221828Sgrehan *ret_len = seg.len; 137256072Sneel if (wired != NULL) 138256072Sneel *wired = seg.wired; 139221828Sgrehan return (error); 140221828Sgrehan} 141221828Sgrehan 142248477Sneeluint32_t 143248477Sneelvm_get_lowmem_limit(struct vmctx *ctx) 144221828Sgrehan{ 145248477Sneel 146248477Sneel return (ctx->lowmem_limit); 147248477Sneel} 148248477Sneel 149248477Sneelvoid 150248477Sneelvm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit) 151248477Sneel{ 152248477Sneel 153248477Sneel ctx->lowmem_limit = limit; 154248477Sneel} 155248477Sneel 156248477Sneelstatic int 157248477Sneelsetup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **addr) 158248477Sneel{ 159221828Sgrehan int error; 160221828Sgrehan struct vm_memory_segment seg; 161221828Sgrehan 162221828Sgrehan /* 163221828Sgrehan * Create and optionally map 'len' bytes of memory at guest 164221828Sgrehan * physical address 'gpa' 165221828Sgrehan */ 166221828Sgrehan bzero(&seg, sizeof(seg)); 167221828Sgrehan seg.gpa = gpa; 168221828Sgrehan seg.len = len; 169221828Sgrehan error = ioctl(ctx->fd, VM_MAP_MEMORY, &seg); 170248477Sneel if (error == 0 && addr != NULL) { 171248477Sneel *addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, 172221828Sgrehan ctx->fd, gpa); 173221828Sgrehan } 174221828Sgrehan return (error); 175221828Sgrehan} 176221828Sgrehan 177248477Sneelint 178248477Sneelvm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms) 179221828Sgrehan{ 180248477Sneel char **addr; 181248477Sneel int error; 182221828Sgrehan 183248477Sneel /* XXX VM_MMAP_SPARSE not implemented yet */ 184248477Sneel assert(vms == VM_MMAP_NONE || vms == VM_MMAP_ALL); 185248477Sneel ctx->vms = vms; 186248477Sneel 187248477Sneel /* 188248477Sneel * If 'memsize' cannot fit entirely in the 'lowmem' segment then 189248477Sneel * create another 'highmem' segment above 4GB for the remainder. 190248477Sneel */ 191248477Sneel if (memsize > ctx->lowmem_limit) { 192248477Sneel ctx->lowmem = ctx->lowmem_limit; 193248477Sneel ctx->highmem = memsize - ctx->lowmem; 194248477Sneel } else { 195248477Sneel ctx->lowmem = memsize; 196248477Sneel ctx->highmem = 0; 197248477Sneel } 198248477Sneel 199248477Sneel if (ctx->lowmem > 0) { 200248477Sneel addr = (vms == VM_MMAP_ALL) ? &ctx->lowmem_addr : NULL; 201248477Sneel error = setup_memory_segment(ctx, 0, ctx->lowmem, addr); 202248477Sneel if (error) 203248477Sneel return (error); 204248477Sneel } 205248477Sneel 206248477Sneel if (ctx->highmem > 0) { 207248477Sneel addr = (vms == VM_MMAP_ALL) ? &ctx->highmem_addr : NULL; 208248477Sneel error = setup_memory_segment(ctx, 4*GB, ctx->highmem, addr); 209248477Sneel if (error) 210248477Sneel return (error); 211248477Sneel } 212248477Sneel 213248477Sneel return (0); 214221828Sgrehan} 215221828Sgrehan 216248477Sneelvoid * 217248477Sneelvm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len) 218248477Sneel{ 219248477Sneel 220248477Sneel /* XXX VM_MMAP_SPARSE not implemented yet */ 221248477Sneel assert(ctx->vms == VM_MMAP_ALL); 222248477Sneel 223248477Sneel if (gaddr < ctx->lowmem && gaddr + len <= ctx->lowmem) 224248477Sneel return ((void *)(ctx->lowmem_addr + gaddr)); 225248477Sneel 226248477Sneel if (gaddr >= 4*GB) { 227248477Sneel gaddr -= 4*GB; 228248477Sneel if (gaddr < ctx->highmem && gaddr + len <= ctx->highmem) 229248477Sneel return ((void *)(ctx->highmem_addr + gaddr)); 230248477Sneel } 231248477Sneel 232248477Sneel return (NULL); 233248477Sneel} 234248477Sneel 235221828Sgrehanint 236221828Sgrehanvm_set_desc(struct vmctx *ctx, int vcpu, int reg, 237221828Sgrehan uint64_t base, uint32_t limit, uint32_t access) 238221828Sgrehan{ 239221828Sgrehan int error; 240221828Sgrehan struct vm_seg_desc vmsegdesc; 241221828Sgrehan 242221828Sgrehan bzero(&vmsegdesc, sizeof(vmsegdesc)); 243221828Sgrehan vmsegdesc.cpuid = vcpu; 244221828Sgrehan vmsegdesc.regnum = reg; 245221828Sgrehan vmsegdesc.desc.base = base; 246221828Sgrehan vmsegdesc.desc.limit = limit; 247221828Sgrehan vmsegdesc.desc.access = access; 248221828Sgrehan 249221828Sgrehan error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc); 250221828Sgrehan return (error); 251221828Sgrehan} 252221828Sgrehan 253221828Sgrehanint 254221828Sgrehanvm_get_desc(struct vmctx *ctx, int vcpu, int reg, 255221828Sgrehan uint64_t *base, uint32_t *limit, uint32_t *access) 256221828Sgrehan{ 257221828Sgrehan int error; 258221828Sgrehan struct vm_seg_desc vmsegdesc; 259221828Sgrehan 260221828Sgrehan bzero(&vmsegdesc, sizeof(vmsegdesc)); 261221828Sgrehan vmsegdesc.cpuid = vcpu; 262221828Sgrehan vmsegdesc.regnum = reg; 263221828Sgrehan 264221828Sgrehan error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc); 265221828Sgrehan if (error == 0) { 266221828Sgrehan *base = vmsegdesc.desc.base; 267221828Sgrehan *limit = vmsegdesc.desc.limit; 268221828Sgrehan *access = vmsegdesc.desc.access; 269221828Sgrehan } 270221828Sgrehan return (error); 271221828Sgrehan} 272221828Sgrehan 273221828Sgrehanint 274221828Sgrehanvm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val) 275221828Sgrehan{ 276221828Sgrehan int error; 277221828Sgrehan struct vm_register vmreg; 278221828Sgrehan 279221828Sgrehan bzero(&vmreg, sizeof(vmreg)); 280221828Sgrehan vmreg.cpuid = vcpu; 281221828Sgrehan vmreg.regnum = reg; 282221828Sgrehan vmreg.regval = val; 283221828Sgrehan 284221828Sgrehan error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg); 285221828Sgrehan return (error); 286221828Sgrehan} 287221828Sgrehan 288221828Sgrehanint 289221828Sgrehanvm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val) 290221828Sgrehan{ 291221828Sgrehan int error; 292221828Sgrehan struct vm_register vmreg; 293221828Sgrehan 294221828Sgrehan bzero(&vmreg, sizeof(vmreg)); 295221828Sgrehan vmreg.cpuid = vcpu; 296221828Sgrehan vmreg.regnum = reg; 297221828Sgrehan 298221828Sgrehan error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg); 299221828Sgrehan *ret_val = vmreg.regval; 300221828Sgrehan return (error); 301221828Sgrehan} 302221828Sgrehan 303221828Sgrehanint 304221828Sgrehanvm_run(struct vmctx *ctx, int vcpu, uint64_t rip, struct vm_exit *vmexit) 305221828Sgrehan{ 306221828Sgrehan int error; 307221828Sgrehan struct vm_run vmrun; 308221828Sgrehan 309221828Sgrehan bzero(&vmrun, sizeof(vmrun)); 310221828Sgrehan vmrun.cpuid = vcpu; 311221828Sgrehan vmrun.rip = rip; 312221828Sgrehan 313221828Sgrehan error = ioctl(ctx->fd, VM_RUN, &vmrun); 314221828Sgrehan bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit)); 315221828Sgrehan return (error); 316221828Sgrehan} 317221828Sgrehan 318221828Sgrehanstatic int 319221828Sgrehanvm_inject_event_real(struct vmctx *ctx, int vcpu, enum vm_event_type type, 320221828Sgrehan int vector, int error_code, int error_code_valid) 321221828Sgrehan{ 322221828Sgrehan struct vm_event ev; 323221828Sgrehan 324221828Sgrehan bzero(&ev, sizeof(ev)); 325221828Sgrehan ev.cpuid = vcpu; 326221828Sgrehan ev.type = type; 327221828Sgrehan ev.vector = vector; 328221828Sgrehan ev.error_code = error_code; 329221828Sgrehan ev.error_code_valid = error_code_valid; 330221828Sgrehan 331221828Sgrehan return (ioctl(ctx->fd, VM_INJECT_EVENT, &ev)); 332221828Sgrehan} 333221828Sgrehan 334221828Sgrehanint 335221828Sgrehanvm_inject_event(struct vmctx *ctx, int vcpu, enum vm_event_type type, 336221828Sgrehan int vector) 337221828Sgrehan{ 338221828Sgrehan 339221828Sgrehan return (vm_inject_event_real(ctx, vcpu, type, vector, 0, 0)); 340221828Sgrehan} 341221828Sgrehan 342221828Sgrehanint 343221828Sgrehanvm_inject_event2(struct vmctx *ctx, int vcpu, enum vm_event_type type, 344221828Sgrehan int vector, int error_code) 345221828Sgrehan{ 346221828Sgrehan 347221828Sgrehan return (vm_inject_event_real(ctx, vcpu, type, vector, error_code, 1)); 348221828Sgrehan} 349221828Sgrehan 350221828Sgrehanint 351239026Sneelvm_apicid2vcpu(struct vmctx *ctx, int apicid) 352239026Sneel{ 353239026Sneel /* 354239026Sneel * The apic id associated with the 'vcpu' has the same numerical value 355239026Sneel * as the 'vcpu' itself. 356239026Sneel */ 357239026Sneel return (apicid); 358239026Sneel} 359239026Sneel 360239026Sneelint 361221828Sgrehanvm_lapic_irq(struct vmctx *ctx, int vcpu, int vector) 362221828Sgrehan{ 363221828Sgrehan struct vm_lapic_irq vmirq; 364221828Sgrehan 365221828Sgrehan bzero(&vmirq, sizeof(vmirq)); 366221828Sgrehan vmirq.cpuid = vcpu; 367221828Sgrehan vmirq.vector = vector; 368221828Sgrehan 369221828Sgrehan return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq)); 370221828Sgrehan} 371221828Sgrehan 372221828Sgrehanint 373221828Sgrehanvm_inject_nmi(struct vmctx *ctx, int vcpu) 374221828Sgrehan{ 375221828Sgrehan struct vm_nmi vmnmi; 376221828Sgrehan 377221828Sgrehan bzero(&vmnmi, sizeof(vmnmi)); 378221828Sgrehan vmnmi.cpuid = vcpu; 379221828Sgrehan 380221828Sgrehan return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi)); 381221828Sgrehan} 382221828Sgrehan 383241486Sneelstatic struct { 384241486Sneel const char *name; 385241486Sneel int type; 386241486Sneel} capstrmap[] = { 387241486Sneel { "hlt_exit", VM_CAP_HALT_EXIT }, 388241486Sneel { "mtrap_exit", VM_CAP_MTRAP_EXIT }, 389241486Sneel { "pause_exit", VM_CAP_PAUSE_EXIT }, 390241486Sneel { "unrestricted_guest", VM_CAP_UNRESTRICTED_GUEST }, 391241486Sneel { 0 } 392241486Sneel}; 393241486Sneel 394221828Sgrehanint 395221828Sgrehanvm_capability_name2type(const char *capname) 396221828Sgrehan{ 397221828Sgrehan int i; 398221828Sgrehan 399221828Sgrehan for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) { 400221828Sgrehan if (strcmp(capstrmap[i].name, capname) == 0) 401221828Sgrehan return (capstrmap[i].type); 402221828Sgrehan } 403221828Sgrehan 404221828Sgrehan return (-1); 405221828Sgrehan} 406221828Sgrehan 407241486Sneelconst char * 408241486Sneelvm_capability_type2name(int type) 409241486Sneel{ 410241486Sneel int i; 411241486Sneel 412241486Sneel for (i = 0; capstrmap[i].name != NULL; i++) { 413241486Sneel if (capstrmap[i].type == type) 414241486Sneel return (capstrmap[i].name); 415241486Sneel } 416241486Sneel 417241486Sneel return (NULL); 418241486Sneel} 419241486Sneel 420221828Sgrehanint 421221828Sgrehanvm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, 422221828Sgrehan int *retval) 423221828Sgrehan{ 424221828Sgrehan int error; 425221828Sgrehan struct vm_capability vmcap; 426221828Sgrehan 427221828Sgrehan bzero(&vmcap, sizeof(vmcap)); 428221828Sgrehan vmcap.cpuid = vcpu; 429221828Sgrehan vmcap.captype = cap; 430221828Sgrehan 431221828Sgrehan error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap); 432221828Sgrehan *retval = vmcap.capval; 433221828Sgrehan return (error); 434221828Sgrehan} 435221828Sgrehan 436221828Sgrehanint 437221828Sgrehanvm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val) 438221828Sgrehan{ 439221828Sgrehan struct vm_capability vmcap; 440221828Sgrehan 441221828Sgrehan bzero(&vmcap, sizeof(vmcap)); 442221828Sgrehan vmcap.cpuid = vcpu; 443221828Sgrehan vmcap.captype = cap; 444221828Sgrehan vmcap.capval = val; 445221828Sgrehan 446221828Sgrehan return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap)); 447221828Sgrehan} 448221828Sgrehan 449221828Sgrehanint 450221828Sgrehanvm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func) 451221828Sgrehan{ 452221828Sgrehan struct vm_pptdev pptdev; 453221828Sgrehan 454221828Sgrehan bzero(&pptdev, sizeof(pptdev)); 455221828Sgrehan pptdev.bus = bus; 456221828Sgrehan pptdev.slot = slot; 457221828Sgrehan pptdev.func = func; 458221828Sgrehan 459221828Sgrehan return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev)); 460221828Sgrehan} 461221828Sgrehan 462221828Sgrehanint 463221828Sgrehanvm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func) 464221828Sgrehan{ 465221828Sgrehan struct vm_pptdev pptdev; 466221828Sgrehan 467221828Sgrehan bzero(&pptdev, sizeof(pptdev)); 468221828Sgrehan pptdev.bus = bus; 469221828Sgrehan pptdev.slot = slot; 470221828Sgrehan pptdev.func = func; 471221828Sgrehan 472221828Sgrehan return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev)); 473221828Sgrehan} 474221828Sgrehan 475221828Sgrehanint 476221828Sgrehanvm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func, 477221828Sgrehan vm_paddr_t gpa, size_t len, vm_paddr_t hpa) 478221828Sgrehan{ 479221828Sgrehan struct vm_pptdev_mmio pptmmio; 480221828Sgrehan 481221828Sgrehan bzero(&pptmmio, sizeof(pptmmio)); 482221828Sgrehan pptmmio.bus = bus; 483221828Sgrehan pptmmio.slot = slot; 484221828Sgrehan pptmmio.func = func; 485221828Sgrehan pptmmio.gpa = gpa; 486221828Sgrehan pptmmio.len = len; 487221828Sgrehan pptmmio.hpa = hpa; 488221828Sgrehan 489221828Sgrehan return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio)); 490221828Sgrehan} 491221828Sgrehan 492221828Sgrehanint 493221828Sgrehanvm_setup_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func, 494221828Sgrehan int destcpu, int vector, int numvec) 495221828Sgrehan{ 496221828Sgrehan struct vm_pptdev_msi pptmsi; 497221828Sgrehan 498221828Sgrehan bzero(&pptmsi, sizeof(pptmsi)); 499221828Sgrehan pptmsi.vcpu = vcpu; 500221828Sgrehan pptmsi.bus = bus; 501221828Sgrehan pptmsi.slot = slot; 502221828Sgrehan pptmsi.func = func; 503221828Sgrehan pptmsi.destcpu = destcpu; 504221828Sgrehan pptmsi.vector = vector; 505221828Sgrehan pptmsi.numvec = numvec; 506221828Sgrehan 507221828Sgrehan return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi)); 508221828Sgrehan} 509221828Sgrehan 510234761Sgrehanint 511234761Sgrehanvm_setup_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func, 512234761Sgrehan int idx, uint32_t msg, uint32_t vector_control, uint64_t addr) 513234761Sgrehan{ 514234761Sgrehan struct vm_pptdev_msix pptmsix; 515234761Sgrehan 516234761Sgrehan bzero(&pptmsix, sizeof(pptmsix)); 517234761Sgrehan pptmsix.vcpu = vcpu; 518234761Sgrehan pptmsix.bus = bus; 519234761Sgrehan pptmsix.slot = slot; 520234761Sgrehan pptmsix.func = func; 521234761Sgrehan pptmsix.idx = idx; 522234761Sgrehan pptmsix.msg = msg; 523234761Sgrehan pptmsix.addr = addr; 524234761Sgrehan pptmsix.vector_control = vector_control; 525234761Sgrehan 526234761Sgrehan return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix); 527234761Sgrehan} 528234761Sgrehan 529221828Sgrehanuint64_t * 530221828Sgrehanvm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv, 531221828Sgrehan int *ret_entries) 532221828Sgrehan{ 533221828Sgrehan int error; 534221828Sgrehan 535221828Sgrehan static struct vm_stats vmstats; 536221828Sgrehan 537221828Sgrehan vmstats.cpuid = vcpu; 538221828Sgrehan 539221828Sgrehan error = ioctl(ctx->fd, VM_STATS, &vmstats); 540221828Sgrehan if (error == 0) { 541221828Sgrehan if (ret_entries) 542221828Sgrehan *ret_entries = vmstats.num_entries; 543221828Sgrehan if (ret_tv) 544221828Sgrehan *ret_tv = vmstats.tv; 545221828Sgrehan return (vmstats.statbuf); 546221828Sgrehan } else 547221828Sgrehan return (NULL); 548221828Sgrehan} 549221828Sgrehan 550221828Sgrehanconst char * 551221828Sgrehanvm_get_stat_desc(struct vmctx *ctx, int index) 552221828Sgrehan{ 553221828Sgrehan static struct vm_stat_desc statdesc; 554221828Sgrehan 555221828Sgrehan statdesc.index = index; 556221828Sgrehan if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0) 557221828Sgrehan return (statdesc.desc); 558221828Sgrehan else 559221828Sgrehan return (NULL); 560221828Sgrehan} 561221828Sgrehan 562240922Sneelint 563240922Sneelvm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state) 564240922Sneel{ 565240922Sneel int error; 566240922Sneel struct vm_x2apic x2apic; 567240922Sneel 568240922Sneel bzero(&x2apic, sizeof(x2apic)); 569240922Sneel x2apic.cpuid = vcpu; 570240922Sneel 571240922Sneel error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic); 572240922Sneel *state = x2apic.state; 573240922Sneel return (error); 574240922Sneel} 575240922Sneel 576240922Sneelint 577240922Sneelvm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state) 578240922Sneel{ 579240922Sneel int error; 580240922Sneel struct vm_x2apic x2apic; 581240922Sneel 582240922Sneel bzero(&x2apic, sizeof(x2apic)); 583240922Sneel x2apic.cpuid = vcpu; 584240922Sneel x2apic.state = state; 585240922Sneel 586240922Sneel error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic); 587240922Sneel 588240922Sneel return (error); 589240922Sneel} 590240922Sneel 591221828Sgrehan/* 592221828Sgrehan * From Intel Vol 3a: 593221828Sgrehan * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT 594221828Sgrehan */ 595221828Sgrehanint 596221828Sgrehanvcpu_reset(struct vmctx *vmctx, int vcpu) 597221828Sgrehan{ 598221828Sgrehan int error; 599221828Sgrehan uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx; 600221828Sgrehan uint32_t desc_access, desc_limit; 601221828Sgrehan uint16_t sel; 602221828Sgrehan 603221828Sgrehan zero = 0; 604221828Sgrehan 605221828Sgrehan rflags = 0x2; 606221828Sgrehan error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags); 607221828Sgrehan if (error) 608221828Sgrehan goto done; 609221828Sgrehan 610221828Sgrehan rip = 0xfff0; 611221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0) 612221828Sgrehan goto done; 613221828Sgrehan 614221828Sgrehan cr0 = CR0_NE; 615221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0) 616221828Sgrehan goto done; 617221828Sgrehan 618221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0) 619221828Sgrehan goto done; 620221828Sgrehan 621239025Sneel cr4 = 0; 622221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0) 623221828Sgrehan goto done; 624221828Sgrehan 625221828Sgrehan /* 626221828Sgrehan * CS: present, r/w, accessed, 16-bit, byte granularity, usable 627221828Sgrehan */ 628221828Sgrehan desc_base = 0xffff0000; 629221828Sgrehan desc_limit = 0xffff; 630221828Sgrehan desc_access = 0x0093; 631221828Sgrehan error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS, 632221828Sgrehan desc_base, desc_limit, desc_access); 633221828Sgrehan if (error) 634221828Sgrehan goto done; 635221828Sgrehan 636221828Sgrehan sel = 0xf000; 637221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0) 638221828Sgrehan goto done; 639221828Sgrehan 640221828Sgrehan /* 641221828Sgrehan * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity 642221828Sgrehan */ 643221828Sgrehan desc_base = 0; 644221828Sgrehan desc_limit = 0xffff; 645221828Sgrehan desc_access = 0x0093; 646221828Sgrehan error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS, 647221828Sgrehan desc_base, desc_limit, desc_access); 648221828Sgrehan if (error) 649221828Sgrehan goto done; 650221828Sgrehan 651221828Sgrehan error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS, 652221828Sgrehan desc_base, desc_limit, desc_access); 653221828Sgrehan if (error) 654221828Sgrehan goto done; 655221828Sgrehan 656221828Sgrehan error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES, 657221828Sgrehan desc_base, desc_limit, desc_access); 658221828Sgrehan if (error) 659221828Sgrehan goto done; 660221828Sgrehan 661221828Sgrehan error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS, 662221828Sgrehan desc_base, desc_limit, desc_access); 663221828Sgrehan if (error) 664221828Sgrehan goto done; 665221828Sgrehan 666221828Sgrehan error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS, 667221828Sgrehan desc_base, desc_limit, desc_access); 668221828Sgrehan if (error) 669221828Sgrehan goto done; 670221828Sgrehan 671221828Sgrehan sel = 0; 672221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0) 673221828Sgrehan goto done; 674221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0) 675221828Sgrehan goto done; 676221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0) 677221828Sgrehan goto done; 678221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0) 679221828Sgrehan goto done; 680221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0) 681221828Sgrehan goto done; 682221828Sgrehan 683221828Sgrehan /* General purpose registers */ 684221828Sgrehan rdx = 0xf00; 685221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0) 686221828Sgrehan goto done; 687221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0) 688221828Sgrehan goto done; 689221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0) 690221828Sgrehan goto done; 691221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0) 692221828Sgrehan goto done; 693221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0) 694221828Sgrehan goto done; 695221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0) 696221828Sgrehan goto done; 697221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0) 698221828Sgrehan goto done; 699221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0) 700221828Sgrehan goto done; 701221828Sgrehan 702221828Sgrehan /* GDTR, IDTR */ 703221828Sgrehan desc_base = 0; 704221828Sgrehan desc_limit = 0xffff; 705221828Sgrehan desc_access = 0; 706221828Sgrehan error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR, 707221828Sgrehan desc_base, desc_limit, desc_access); 708221828Sgrehan if (error != 0) 709221828Sgrehan goto done; 710221828Sgrehan 711221828Sgrehan error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR, 712221828Sgrehan desc_base, desc_limit, desc_access); 713221828Sgrehan if (error != 0) 714221828Sgrehan goto done; 715221828Sgrehan 716221828Sgrehan /* TR */ 717221828Sgrehan desc_base = 0; 718221828Sgrehan desc_limit = 0xffff; 719221828Sgrehan desc_access = 0x0000008b; 720221828Sgrehan error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access); 721221828Sgrehan if (error) 722221828Sgrehan goto done; 723221828Sgrehan 724221828Sgrehan sel = 0; 725221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0) 726221828Sgrehan goto done; 727221828Sgrehan 728221828Sgrehan /* LDTR */ 729221828Sgrehan desc_base = 0; 730221828Sgrehan desc_limit = 0xffff; 731221828Sgrehan desc_access = 0x00000082; 732221828Sgrehan error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base, 733221828Sgrehan desc_limit, desc_access); 734221828Sgrehan if (error) 735221828Sgrehan goto done; 736221828Sgrehan 737221828Sgrehan sel = 0; 738221828Sgrehan if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0) 739221828Sgrehan goto done; 740221828Sgrehan 741221828Sgrehan /* XXX cr2, debug registers */ 742221828Sgrehan 743221828Sgrehan error = 0; 744221828Sgrehandone: 745221828Sgrehan return (error); 746221828Sgrehan} 747256072Sneel 748256072Sneelint 749256072Sneelvm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num) 750256072Sneel{ 751256072Sneel int error, i; 752256072Sneel struct vm_gpa_pte gpapte; 753256072Sneel 754256072Sneel bzero(&gpapte, sizeof(gpapte)); 755256072Sneel gpapte.gpa = gpa; 756256072Sneel 757256072Sneel error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte); 758256072Sneel 759256072Sneel if (error == 0) { 760256072Sneel *num = gpapte.ptenum; 761256072Sneel for (i = 0; i < gpapte.ptenum; i++) 762256072Sneel pte[i] = gpapte.pte[i]; 763256072Sneel } 764256072Sneel 765256072Sneel return (error); 766256072Sneel} 767