vmm_dev.c revision 268976
1/*- 2 * Copyright (c) 2011 NetApp, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: stable/10/sys/amd64/vmm/vmm_dev.c 268976 2014-07-22 04:39:16Z jhb $ 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: stable/10/sys/amd64/vmm/vmm_dev.c 268976 2014-07-22 04:39:16Z jhb $"); 31 32#include <sys/param.h> 33#include <sys/kernel.h> 34#include <sys/queue.h> 35#include <sys/lock.h> 36#include <sys/mutex.h> 37#include <sys/malloc.h> 38#include <sys/conf.h> 39#include <sys/sysctl.h> 40#include <sys/libkern.h> 41#include <sys/ioccom.h> 42#include <sys/mman.h> 43#include <sys/uio.h> 44 45#include <vm/vm.h> 46#include <vm/pmap.h> 47#include <vm/vm_map.h> 48 49#include <machine/vmparam.h> 50#include <machine/vmm.h> 51#include <machine/vmm_instruction_emul.h> 52#include <machine/vmm_dev.h> 53 54#include "vmm_lapic.h" 55#include "vmm_stat.h" 56#include "vmm_mem.h" 57#include "io/ppt.h" 58#include "io/vatpic.h" 59#include "io/vioapic.h" 60#include "io/vhpet.h" 61 62struct vmmdev_softc { 63 struct vm *vm; /* vm instance cookie */ 64 struct cdev *cdev; 65 SLIST_ENTRY(vmmdev_softc) link; 66 int flags; 67}; 68#define VSC_LINKED 0x01 69 70static SLIST_HEAD(, vmmdev_softc) head; 71 72static struct mtx vmmdev_mtx; 73 74static MALLOC_DEFINE(M_VMMDEV, "vmmdev", "vmmdev"); 75 76SYSCTL_DECL(_hw_vmm); 77 78static struct vmmdev_softc * 79vmmdev_lookup(const char *name) 80{ 81 struct vmmdev_softc *sc; 82 83#ifdef notyet /* XXX kernel is not compiled with invariants */ 84 mtx_assert(&vmmdev_mtx, MA_OWNED); 85#endif 86 87 SLIST_FOREACH(sc, &head, link) { 88 if (strcmp(name, vm_name(sc->vm)) == 0) 89 break; 90 } 91 92 return (sc); 93} 94 95static struct vmmdev_softc * 96vmmdev_lookup2(struct cdev *cdev) 97{ 98 99 return (cdev->si_drv1); 100} 101 102static int 103vmmdev_rw(struct cdev *cdev, struct uio *uio, int flags) 104{ 105 int error, off, c, prot; 106 vm_paddr_t gpa; 107 void *hpa, *cookie; 108 struct vmmdev_softc *sc; 109 110 static char zerobuf[PAGE_SIZE]; 111 112 error = 0; 113 sc = vmmdev_lookup2(cdev); 114 if (sc == NULL) 115 error = ENXIO; 116 117 prot = (uio->uio_rw == UIO_WRITE ? VM_PROT_WRITE : VM_PROT_READ); 118 while (uio->uio_resid > 0 && error == 0) { 119 gpa = uio->uio_offset; 120 off = gpa & PAGE_MASK; 121 c = min(uio->uio_resid, PAGE_SIZE - off); 122 123 /* 124 * The VM has a hole in its physical memory map. If we want to 125 * use 'dd' to inspect memory beyond the hole we need to 126 * provide bogus data for memory that lies in the hole. 127 * 128 * Since this device does not support lseek(2), dd(1) will 129 * read(2) blocks of data to simulate the lseek(2). 130 */ 131 hpa = vm_gpa_hold(sc->vm, gpa, c, prot, &cookie); 132 if (hpa == NULL) { 133 if (uio->uio_rw == UIO_READ) 134 error = uiomove(zerobuf, c, uio); 135 else 136 error = EFAULT; 137 } else { 138 error = uiomove(hpa, c, uio); 139 vm_gpa_release(cookie); 140 } 141 } 142 return (error); 143} 144 145static int 146vmmdev_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag, 147 struct thread *td) 148{ 149 int error, vcpu, state_changed; 150 struct vmmdev_softc *sc; 151 struct vm_memory_segment *seg; 152 struct vm_register *vmreg; 153 struct vm_seg_desc *vmsegdesc; 154 struct vm_run *vmrun; 155 struct vm_exception *vmexc; 156 struct vm_lapic_irq *vmirq; 157 struct vm_lapic_msi *vmmsi; 158 struct vm_ioapic_irq *ioapic_irq; 159 struct vm_isa_irq *isa_irq; 160 struct vm_isa_irq_trigger *isa_irq_trigger; 161 struct vm_capability *vmcap; 162 struct vm_pptdev *pptdev; 163 struct vm_pptdev_mmio *pptmmio; 164 struct vm_pptdev_msi *pptmsi; 165 struct vm_pptdev_msix *pptmsix; 166 struct vm_nmi *vmnmi; 167 struct vm_stats *vmstats; 168 struct vm_stat_desc *statdesc; 169 struct vm_x2apic *x2apic; 170 struct vm_gpa_pte *gpapte; 171 struct vm_suspend *vmsuspend; 172 struct vm_gla2gpa *gg; 173 174 sc = vmmdev_lookup2(cdev); 175 if (sc == NULL) 176 return (ENXIO); 177 178 error = 0; 179 vcpu = -1; 180 state_changed = 0; 181 182 /* 183 * Some VMM ioctls can operate only on vcpus that are not running. 184 */ 185 switch (cmd) { 186 case VM_RUN: 187 case VM_GET_REGISTER: 188 case VM_SET_REGISTER: 189 case VM_GET_SEGMENT_DESCRIPTOR: 190 case VM_SET_SEGMENT_DESCRIPTOR: 191 case VM_INJECT_EXCEPTION: 192 case VM_GET_CAPABILITY: 193 case VM_SET_CAPABILITY: 194 case VM_PPTDEV_MSI: 195 case VM_PPTDEV_MSIX: 196 case VM_SET_X2APIC_STATE: 197 case VM_GLA2GPA: 198 /* 199 * XXX fragile, handle with care 200 * Assumes that the first field of the ioctl data is the vcpu. 201 */ 202 vcpu = *(int *)data; 203 if (vcpu < 0 || vcpu >= VM_MAXCPU) { 204 error = EINVAL; 205 goto done; 206 } 207 208 error = vcpu_set_state(sc->vm, vcpu, VCPU_FROZEN, true); 209 if (error) 210 goto done; 211 212 state_changed = 1; 213 break; 214 215 case VM_MAP_PPTDEV_MMIO: 216 case VM_BIND_PPTDEV: 217 case VM_UNBIND_PPTDEV: 218 case VM_MAP_MEMORY: 219 /* 220 * ioctls that operate on the entire virtual machine must 221 * prevent all vcpus from running. 222 */ 223 error = 0; 224 for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++) { 225 error = vcpu_set_state(sc->vm, vcpu, VCPU_FROZEN, true); 226 if (error) 227 break; 228 } 229 230 if (error) { 231 while (--vcpu >= 0) 232 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false); 233 goto done; 234 } 235 236 state_changed = 2; 237 break; 238 239 default: 240 break; 241 } 242 243 switch(cmd) { 244 case VM_RUN: 245 vmrun = (struct vm_run *)data; 246 error = vm_run(sc->vm, vmrun); 247 break; 248 case VM_SUSPEND: 249 vmsuspend = (struct vm_suspend *)data; 250 error = vm_suspend(sc->vm, vmsuspend->how); 251 break; 252 case VM_STAT_DESC: { 253 statdesc = (struct vm_stat_desc *)data; 254 error = vmm_stat_desc_copy(statdesc->index, 255 statdesc->desc, sizeof(statdesc->desc)); 256 break; 257 } 258 case VM_STATS: { 259 CTASSERT(MAX_VM_STATS >= MAX_VMM_STAT_ELEMS); 260 vmstats = (struct vm_stats *)data; 261 getmicrotime(&vmstats->tv); 262 error = vmm_stat_copy(sc->vm, vmstats->cpuid, 263 &vmstats->num_entries, vmstats->statbuf); 264 break; 265 } 266 case VM_PPTDEV_MSI: 267 pptmsi = (struct vm_pptdev_msi *)data; 268 error = ppt_setup_msi(sc->vm, pptmsi->vcpu, 269 pptmsi->bus, pptmsi->slot, pptmsi->func, 270 pptmsi->addr, pptmsi->msg, 271 pptmsi->numvec); 272 break; 273 case VM_PPTDEV_MSIX: 274 pptmsix = (struct vm_pptdev_msix *)data; 275 error = ppt_setup_msix(sc->vm, pptmsix->vcpu, 276 pptmsix->bus, pptmsix->slot, 277 pptmsix->func, pptmsix->idx, 278 pptmsix->addr, pptmsix->msg, 279 pptmsix->vector_control); 280 break; 281 case VM_MAP_PPTDEV_MMIO: 282 pptmmio = (struct vm_pptdev_mmio *)data; 283 error = ppt_map_mmio(sc->vm, pptmmio->bus, pptmmio->slot, 284 pptmmio->func, pptmmio->gpa, pptmmio->len, 285 pptmmio->hpa); 286 break; 287 case VM_BIND_PPTDEV: 288 pptdev = (struct vm_pptdev *)data; 289 error = vm_assign_pptdev(sc->vm, pptdev->bus, pptdev->slot, 290 pptdev->func); 291 break; 292 case VM_UNBIND_PPTDEV: 293 pptdev = (struct vm_pptdev *)data; 294 error = vm_unassign_pptdev(sc->vm, pptdev->bus, pptdev->slot, 295 pptdev->func); 296 break; 297 case VM_INJECT_EXCEPTION: 298 vmexc = (struct vm_exception *)data; 299 error = vm_inject_exception(sc->vm, vmexc->cpuid, vmexc); 300 break; 301 case VM_INJECT_NMI: 302 vmnmi = (struct vm_nmi *)data; 303 error = vm_inject_nmi(sc->vm, vmnmi->cpuid); 304 break; 305 case VM_LAPIC_IRQ: 306 vmirq = (struct vm_lapic_irq *)data; 307 error = lapic_intr_edge(sc->vm, vmirq->cpuid, vmirq->vector); 308 break; 309 case VM_LAPIC_LOCAL_IRQ: 310 vmirq = (struct vm_lapic_irq *)data; 311 error = lapic_set_local_intr(sc->vm, vmirq->cpuid, 312 vmirq->vector); 313 break; 314 case VM_LAPIC_MSI: 315 vmmsi = (struct vm_lapic_msi *)data; 316 error = lapic_intr_msi(sc->vm, vmmsi->addr, vmmsi->msg); 317 break; 318 case VM_IOAPIC_ASSERT_IRQ: 319 ioapic_irq = (struct vm_ioapic_irq *)data; 320 error = vioapic_assert_irq(sc->vm, ioapic_irq->irq); 321 break; 322 case VM_IOAPIC_DEASSERT_IRQ: 323 ioapic_irq = (struct vm_ioapic_irq *)data; 324 error = vioapic_deassert_irq(sc->vm, ioapic_irq->irq); 325 break; 326 case VM_IOAPIC_PULSE_IRQ: 327 ioapic_irq = (struct vm_ioapic_irq *)data; 328 error = vioapic_pulse_irq(sc->vm, ioapic_irq->irq); 329 break; 330 case VM_IOAPIC_PINCOUNT: 331 *(int *)data = vioapic_pincount(sc->vm); 332 break; 333 case VM_ISA_ASSERT_IRQ: 334 isa_irq = (struct vm_isa_irq *)data; 335 error = vatpic_assert_irq(sc->vm, isa_irq->atpic_irq); 336 if (error == 0 && isa_irq->ioapic_irq != -1) 337 error = vioapic_assert_irq(sc->vm, 338 isa_irq->ioapic_irq); 339 break; 340 case VM_ISA_DEASSERT_IRQ: 341 isa_irq = (struct vm_isa_irq *)data; 342 error = vatpic_deassert_irq(sc->vm, isa_irq->atpic_irq); 343 if (error == 0 && isa_irq->ioapic_irq != -1) 344 error = vioapic_deassert_irq(sc->vm, 345 isa_irq->ioapic_irq); 346 break; 347 case VM_ISA_PULSE_IRQ: 348 isa_irq = (struct vm_isa_irq *)data; 349 error = vatpic_pulse_irq(sc->vm, isa_irq->atpic_irq); 350 if (error == 0 && isa_irq->ioapic_irq != -1) 351 error = vioapic_pulse_irq(sc->vm, isa_irq->ioapic_irq); 352 break; 353 case VM_ISA_SET_IRQ_TRIGGER: 354 isa_irq_trigger = (struct vm_isa_irq_trigger *)data; 355 error = vatpic_set_irq_trigger(sc->vm, 356 isa_irq_trigger->atpic_irq, isa_irq_trigger->trigger); 357 break; 358 case VM_MAP_MEMORY: 359 seg = (struct vm_memory_segment *)data; 360 error = vm_malloc(sc->vm, seg->gpa, seg->len); 361 break; 362 case VM_GET_MEMORY_SEG: 363 seg = (struct vm_memory_segment *)data; 364 seg->len = 0; 365 (void)vm_gpabase2memseg(sc->vm, seg->gpa, seg); 366 error = 0; 367 break; 368 case VM_GET_REGISTER: 369 vmreg = (struct vm_register *)data; 370 error = vm_get_register(sc->vm, vmreg->cpuid, vmreg->regnum, 371 &vmreg->regval); 372 break; 373 case VM_SET_REGISTER: 374 vmreg = (struct vm_register *)data; 375 error = vm_set_register(sc->vm, vmreg->cpuid, vmreg->regnum, 376 vmreg->regval); 377 break; 378 case VM_SET_SEGMENT_DESCRIPTOR: 379 vmsegdesc = (struct vm_seg_desc *)data; 380 error = vm_set_seg_desc(sc->vm, vmsegdesc->cpuid, 381 vmsegdesc->regnum, 382 &vmsegdesc->desc); 383 break; 384 case VM_GET_SEGMENT_DESCRIPTOR: 385 vmsegdesc = (struct vm_seg_desc *)data; 386 error = vm_get_seg_desc(sc->vm, vmsegdesc->cpuid, 387 vmsegdesc->regnum, 388 &vmsegdesc->desc); 389 break; 390 case VM_GET_CAPABILITY: 391 vmcap = (struct vm_capability *)data; 392 error = vm_get_capability(sc->vm, vmcap->cpuid, 393 vmcap->captype, 394 &vmcap->capval); 395 break; 396 case VM_SET_CAPABILITY: 397 vmcap = (struct vm_capability *)data; 398 error = vm_set_capability(sc->vm, vmcap->cpuid, 399 vmcap->captype, 400 vmcap->capval); 401 break; 402 case VM_SET_X2APIC_STATE: 403 x2apic = (struct vm_x2apic *)data; 404 error = vm_set_x2apic_state(sc->vm, 405 x2apic->cpuid, x2apic->state); 406 break; 407 case VM_GET_X2APIC_STATE: 408 x2apic = (struct vm_x2apic *)data; 409 error = vm_get_x2apic_state(sc->vm, 410 x2apic->cpuid, &x2apic->state); 411 break; 412 case VM_GET_GPA_PMAP: 413 gpapte = (struct vm_gpa_pte *)data; 414 pmap_get_mapping(vmspace_pmap(vm_get_vmspace(sc->vm)), 415 gpapte->gpa, gpapte->pte, &gpapte->ptenum); 416 error = 0; 417 break; 418 case VM_GET_HPET_CAPABILITIES: 419 error = vhpet_getcap((struct vm_hpet_cap *)data); 420 break; 421 case VM_GLA2GPA: { 422 CTASSERT(PROT_READ == VM_PROT_READ); 423 CTASSERT(PROT_WRITE == VM_PROT_WRITE); 424 CTASSERT(PROT_EXEC == VM_PROT_EXECUTE); 425 gg = (struct vm_gla2gpa *)data; 426 error = vmm_gla2gpa(sc->vm, gg->vcpuid, &gg->paging, gg->gla, 427 gg->prot, &gg->gpa); 428 KASSERT(error == 0 || error == 1 || error == -1, 429 ("%s: vmm_gla2gpa unknown error %d", __func__, error)); 430 if (error >= 0) { 431 /* 432 * error = 0: the translation was successful 433 * error = 1: a fault was injected into the guest 434 */ 435 gg->fault = error; 436 error = 0; 437 } else { 438 error = EFAULT; 439 } 440 break; 441 } 442 default: 443 error = ENOTTY; 444 break; 445 } 446 447 if (state_changed == 1) { 448 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false); 449 } else if (state_changed == 2) { 450 for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++) 451 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false); 452 } 453 454done: 455 /* Make sure that no handler returns a bogus value like ERESTART */ 456 KASSERT(error >= 0, ("vmmdev_ioctl: invalid error return %d", error)); 457 return (error); 458} 459 460static int 461vmmdev_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, 462 vm_size_t size, struct vm_object **object, int nprot) 463{ 464 int error; 465 struct vmmdev_softc *sc; 466 467 sc = vmmdev_lookup2(cdev); 468 if (sc != NULL && (nprot & PROT_EXEC) == 0) 469 error = vm_get_memobj(sc->vm, *offset, size, offset, object); 470 else 471 error = EINVAL; 472 473 return (error); 474} 475 476static void 477vmmdev_destroy(void *arg) 478{ 479 480 struct vmmdev_softc *sc = arg; 481 482 if (sc->cdev != NULL) 483 destroy_dev(sc->cdev); 484 485 if (sc->vm != NULL) 486 vm_destroy(sc->vm); 487 488 if ((sc->flags & VSC_LINKED) != 0) { 489 mtx_lock(&vmmdev_mtx); 490 SLIST_REMOVE(&head, sc, vmmdev_softc, link); 491 mtx_unlock(&vmmdev_mtx); 492 } 493 494 free(sc, M_VMMDEV); 495} 496 497static int 498sysctl_vmm_destroy(SYSCTL_HANDLER_ARGS) 499{ 500 int error; 501 char buf[VM_MAX_NAMELEN]; 502 struct vmmdev_softc *sc; 503 struct cdev *cdev; 504 505 strlcpy(buf, "beavis", sizeof(buf)); 506 error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 507 if (error != 0 || req->newptr == NULL) 508 return (error); 509 510 mtx_lock(&vmmdev_mtx); 511 sc = vmmdev_lookup(buf); 512 if (sc == NULL || sc->cdev == NULL) { 513 mtx_unlock(&vmmdev_mtx); 514 return (EINVAL); 515 } 516 517 /* 518 * The 'cdev' will be destroyed asynchronously when 'si_threadcount' 519 * goes down to 0 so we should not do it again in the callback. 520 */ 521 cdev = sc->cdev; 522 sc->cdev = NULL; 523 mtx_unlock(&vmmdev_mtx); 524 525 /* 526 * Schedule the 'cdev' to be destroyed: 527 * 528 * - any new operations on this 'cdev' will return an error (ENXIO). 529 * 530 * - when the 'si_threadcount' dwindles down to zero the 'cdev' will 531 * be destroyed and the callback will be invoked in a taskqueue 532 * context. 533 */ 534 destroy_dev_sched_cb(cdev, vmmdev_destroy, sc); 535 536 return (0); 537} 538SYSCTL_PROC(_hw_vmm, OID_AUTO, destroy, CTLTYPE_STRING | CTLFLAG_RW, 539 NULL, 0, sysctl_vmm_destroy, "A", NULL); 540 541static struct cdevsw vmmdevsw = { 542 .d_name = "vmmdev", 543 .d_version = D_VERSION, 544 .d_ioctl = vmmdev_ioctl, 545 .d_mmap_single = vmmdev_mmap_single, 546 .d_read = vmmdev_rw, 547 .d_write = vmmdev_rw, 548}; 549 550static int 551sysctl_vmm_create(SYSCTL_HANDLER_ARGS) 552{ 553 int error; 554 struct vm *vm; 555 struct cdev *cdev; 556 struct vmmdev_softc *sc, *sc2; 557 char buf[VM_MAX_NAMELEN]; 558 559 strlcpy(buf, "beavis", sizeof(buf)); 560 error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 561 if (error != 0 || req->newptr == NULL) 562 return (error); 563 564 mtx_lock(&vmmdev_mtx); 565 sc = vmmdev_lookup(buf); 566 mtx_unlock(&vmmdev_mtx); 567 if (sc != NULL) 568 return (EEXIST); 569 570 error = vm_create(buf, &vm); 571 if (error != 0) 572 return (error); 573 574 sc = malloc(sizeof(struct vmmdev_softc), M_VMMDEV, M_WAITOK | M_ZERO); 575 sc->vm = vm; 576 577 /* 578 * Lookup the name again just in case somebody sneaked in when we 579 * dropped the lock. 580 */ 581 mtx_lock(&vmmdev_mtx); 582 sc2 = vmmdev_lookup(buf); 583 if (sc2 == NULL) { 584 SLIST_INSERT_HEAD(&head, sc, link); 585 sc->flags |= VSC_LINKED; 586 } 587 mtx_unlock(&vmmdev_mtx); 588 589 if (sc2 != NULL) { 590 vmmdev_destroy(sc); 591 return (EEXIST); 592 } 593 594 error = make_dev_p(MAKEDEV_CHECKNAME, &cdev, &vmmdevsw, NULL, 595 UID_ROOT, GID_WHEEL, 0600, "vmm/%s", buf); 596 if (error != 0) { 597 vmmdev_destroy(sc); 598 return (error); 599 } 600 601 mtx_lock(&vmmdev_mtx); 602 sc->cdev = cdev; 603 sc->cdev->si_drv1 = sc; 604 mtx_unlock(&vmmdev_mtx); 605 606 return (0); 607} 608SYSCTL_PROC(_hw_vmm, OID_AUTO, create, CTLTYPE_STRING | CTLFLAG_RW, 609 NULL, 0, sysctl_vmm_create, "A", NULL); 610 611void 612vmmdev_init(void) 613{ 614 mtx_init(&vmmdev_mtx, "vmm device mutex", NULL, MTX_DEF); 615} 616 617int 618vmmdev_cleanup(void) 619{ 620 int error; 621 622 if (SLIST_EMPTY(&head)) 623 error = 0; 624 else 625 error = EBUSY; 626 627 return (error); 628} 629