vmm_dev.c revision 257297
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: head/sys/amd64/vmm/vmm_dev.c 257297 2013-10-29 02:25:18Z neel $ 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: head/sys/amd64/vmm/vmm_dev.c 257297 2013-10-29 02:25:18Z neel $"); 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 51#include <machine/vmm.h> 52#include "vmm_lapic.h" 53#include "vmm_stat.h" 54#include "vmm_mem.h" 55#include "io/ppt.h" 56#include <machine/vmm_dev.h> 57 58struct vmmdev_softc { 59 struct vm *vm; /* vm instance cookie */ 60 struct cdev *cdev; 61 SLIST_ENTRY(vmmdev_softc) link; 62 int flags; 63}; 64#define VSC_LINKED 0x01 65 66static SLIST_HEAD(, vmmdev_softc) head; 67 68static struct mtx vmmdev_mtx; 69 70static MALLOC_DEFINE(M_VMMDEV, "vmmdev", "vmmdev"); 71 72SYSCTL_DECL(_hw_vmm); 73 74static struct vmmdev_softc * 75vmmdev_lookup(const char *name) 76{ 77 struct vmmdev_softc *sc; 78 79#ifdef notyet /* XXX kernel is not compiled with invariants */ 80 mtx_assert(&vmmdev_mtx, MA_OWNED); 81#endif 82 83 SLIST_FOREACH(sc, &head, link) { 84 if (strcmp(name, vm_name(sc->vm)) == 0) 85 break; 86 } 87 88 return (sc); 89} 90 91static struct vmmdev_softc * 92vmmdev_lookup2(struct cdev *cdev) 93{ 94 95 return (cdev->si_drv1); 96} 97 98static int 99vmmdev_rw(struct cdev *cdev, struct uio *uio, int flags) 100{ 101 int error, off, c, prot; 102 vm_paddr_t gpa; 103 void *hpa, *cookie; 104 struct vmmdev_softc *sc; 105 106 static char zerobuf[PAGE_SIZE]; 107 108 error = 0; 109 sc = vmmdev_lookup2(cdev); 110 if (sc == NULL) 111 error = ENXIO; 112 113 prot = (uio->uio_rw == UIO_WRITE ? VM_PROT_WRITE : VM_PROT_READ); 114 while (uio->uio_resid > 0 && error == 0) { 115 gpa = uio->uio_offset; 116 off = gpa & PAGE_MASK; 117 c = min(uio->uio_resid, PAGE_SIZE - off); 118 119 /* 120 * The VM has a hole in its physical memory map. If we want to 121 * use 'dd' to inspect memory beyond the hole we need to 122 * provide bogus data for memory that lies in the hole. 123 * 124 * Since this device does not support lseek(2), dd(1) will 125 * read(2) blocks of data to simulate the lseek(2). 126 */ 127 hpa = vm_gpa_hold(sc->vm, gpa, c, prot, &cookie); 128 if (hpa == NULL) { 129 if (uio->uio_rw == UIO_READ) 130 error = uiomove(zerobuf, c, uio); 131 else 132 error = EFAULT; 133 } else { 134 error = uiomove(hpa, c, uio); 135 vm_gpa_release(cookie); 136 } 137 } 138 return (error); 139} 140 141static int 142vmmdev_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag, 143 struct thread *td) 144{ 145 int error, vcpu, state_changed; 146 struct vmmdev_softc *sc; 147 struct vm_memory_segment *seg; 148 struct vm_register *vmreg; 149 struct vm_seg_desc* vmsegdesc; 150 struct vm_run *vmrun; 151 struct vm_event *vmevent; 152 struct vm_lapic_irq *vmirq; 153 struct vm_capability *vmcap; 154 struct vm_pptdev *pptdev; 155 struct vm_pptdev_mmio *pptmmio; 156 struct vm_pptdev_msi *pptmsi; 157 struct vm_pptdev_msix *pptmsix; 158 struct vm_nmi *vmnmi; 159 struct vm_stats *vmstats; 160 struct vm_stat_desc *statdesc; 161 struct vm_x2apic *x2apic; 162 struct vm_gpa_pte *gpapte; 163 164 sc = vmmdev_lookup2(cdev); 165 if (sc == NULL) 166 return (ENXIO); 167 168 vcpu = -1; 169 state_changed = 0; 170 171 /* 172 * Some VMM ioctls can operate only on vcpus that are not running. 173 */ 174 switch (cmd) { 175 case VM_RUN: 176 case VM_GET_REGISTER: 177 case VM_SET_REGISTER: 178 case VM_GET_SEGMENT_DESCRIPTOR: 179 case VM_SET_SEGMENT_DESCRIPTOR: 180 case VM_INJECT_EVENT: 181 case VM_GET_CAPABILITY: 182 case VM_SET_CAPABILITY: 183 case VM_PPTDEV_MSI: 184 case VM_PPTDEV_MSIX: 185 case VM_SET_X2APIC_STATE: 186 /* 187 * XXX fragile, handle with care 188 * Assumes that the first field of the ioctl data is the vcpu. 189 */ 190 vcpu = *(int *)data; 191 if (vcpu < 0 || vcpu >= VM_MAXCPU) { 192 error = EINVAL; 193 goto done; 194 } 195 196 error = vcpu_set_state(sc->vm, vcpu, VCPU_FROZEN); 197 if (error) 198 goto done; 199 200 state_changed = 1; 201 break; 202 203 case VM_MAP_PPTDEV_MMIO: 204 case VM_BIND_PPTDEV: 205 case VM_UNBIND_PPTDEV: 206 case VM_MAP_MEMORY: 207 /* 208 * ioctls that operate on the entire virtual machine must 209 * prevent all vcpus from running. 210 */ 211 error = 0; 212 for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++) { 213 error = vcpu_set_state(sc->vm, vcpu, VCPU_FROZEN); 214 if (error) 215 break; 216 } 217 218 if (error) { 219 while (--vcpu >= 0) 220 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE); 221 goto done; 222 } 223 224 state_changed = 2; 225 break; 226 227 default: 228 break; 229 } 230 231 switch(cmd) { 232 case VM_RUN: 233 vmrun = (struct vm_run *)data; 234 error = vm_run(sc->vm, vmrun); 235 break; 236 case VM_STAT_DESC: { 237 statdesc = (struct vm_stat_desc *)data; 238 error = vmm_stat_desc_copy(statdesc->index, 239 statdesc->desc, sizeof(statdesc->desc)); 240 break; 241 } 242 case VM_STATS: { 243 CTASSERT(MAX_VM_STATS >= MAX_VMM_STAT_ELEMS); 244 vmstats = (struct vm_stats *)data; 245 getmicrotime(&vmstats->tv); 246 error = vmm_stat_copy(sc->vm, vmstats->cpuid, 247 &vmstats->num_entries, vmstats->statbuf); 248 break; 249 } 250 case VM_PPTDEV_MSI: 251 pptmsi = (struct vm_pptdev_msi *)data; 252 error = ppt_setup_msi(sc->vm, pptmsi->vcpu, 253 pptmsi->bus, pptmsi->slot, pptmsi->func, 254 pptmsi->destcpu, pptmsi->vector, 255 pptmsi->numvec); 256 break; 257 case VM_PPTDEV_MSIX: 258 pptmsix = (struct vm_pptdev_msix *)data; 259 error = ppt_setup_msix(sc->vm, pptmsix->vcpu, 260 pptmsix->bus, pptmsix->slot, 261 pptmsix->func, pptmsix->idx, 262 pptmsix->msg, pptmsix->vector_control, 263 pptmsix->addr); 264 break; 265 case VM_MAP_PPTDEV_MMIO: 266 pptmmio = (struct vm_pptdev_mmio *)data; 267 error = ppt_map_mmio(sc->vm, pptmmio->bus, pptmmio->slot, 268 pptmmio->func, pptmmio->gpa, pptmmio->len, 269 pptmmio->hpa); 270 break; 271 case VM_BIND_PPTDEV: 272 pptdev = (struct vm_pptdev *)data; 273 error = vm_assign_pptdev(sc->vm, pptdev->bus, pptdev->slot, 274 pptdev->func); 275 break; 276 case VM_UNBIND_PPTDEV: 277 pptdev = (struct vm_pptdev *)data; 278 error = vm_unassign_pptdev(sc->vm, pptdev->bus, pptdev->slot, 279 pptdev->func); 280 break; 281 case VM_INJECT_EVENT: 282 vmevent = (struct vm_event *)data; 283 error = vm_inject_event(sc->vm, vmevent->cpuid, vmevent->type, 284 vmevent->vector, 285 vmevent->error_code, 286 vmevent->error_code_valid); 287 break; 288 case VM_INJECT_NMI: 289 vmnmi = (struct vm_nmi *)data; 290 error = vm_inject_nmi(sc->vm, vmnmi->cpuid); 291 break; 292 case VM_LAPIC_IRQ: 293 vmirq = (struct vm_lapic_irq *)data; 294 error = lapic_set_intr(sc->vm, vmirq->cpuid, vmirq->vector); 295 break; 296 case VM_MAP_MEMORY: 297 seg = (struct vm_memory_segment *)data; 298 error = vm_malloc(sc->vm, seg->gpa, seg->len); 299 break; 300 case VM_GET_MEMORY_SEG: 301 seg = (struct vm_memory_segment *)data; 302 seg->len = 0; 303 (void)vm_gpabase2memseg(sc->vm, seg->gpa, seg); 304 error = 0; 305 break; 306 case VM_GET_REGISTER: 307 vmreg = (struct vm_register *)data; 308 error = vm_get_register(sc->vm, vmreg->cpuid, vmreg->regnum, 309 &vmreg->regval); 310 break; 311 case VM_SET_REGISTER: 312 vmreg = (struct vm_register *)data; 313 error = vm_set_register(sc->vm, vmreg->cpuid, vmreg->regnum, 314 vmreg->regval); 315 break; 316 case VM_SET_SEGMENT_DESCRIPTOR: 317 vmsegdesc = (struct vm_seg_desc *)data; 318 error = vm_set_seg_desc(sc->vm, vmsegdesc->cpuid, 319 vmsegdesc->regnum, 320 &vmsegdesc->desc); 321 break; 322 case VM_GET_SEGMENT_DESCRIPTOR: 323 vmsegdesc = (struct vm_seg_desc *)data; 324 error = vm_get_seg_desc(sc->vm, vmsegdesc->cpuid, 325 vmsegdesc->regnum, 326 &vmsegdesc->desc); 327 break; 328 case VM_GET_CAPABILITY: 329 vmcap = (struct vm_capability *)data; 330 error = vm_get_capability(sc->vm, vmcap->cpuid, 331 vmcap->captype, 332 &vmcap->capval); 333 break; 334 case VM_SET_CAPABILITY: 335 vmcap = (struct vm_capability *)data; 336 error = vm_set_capability(sc->vm, vmcap->cpuid, 337 vmcap->captype, 338 vmcap->capval); 339 break; 340 case VM_SET_X2APIC_STATE: 341 x2apic = (struct vm_x2apic *)data; 342 error = vm_set_x2apic_state(sc->vm, 343 x2apic->cpuid, x2apic->state); 344 break; 345 case VM_GET_X2APIC_STATE: 346 x2apic = (struct vm_x2apic *)data; 347 error = vm_get_x2apic_state(sc->vm, 348 x2apic->cpuid, &x2apic->state); 349 break; 350 case VM_GET_GPA_PMAP: 351 gpapte = (struct vm_gpa_pte *)data; 352 pmap_get_mapping(vmspace_pmap(vm_get_vmspace(sc->vm)), 353 gpapte->gpa, gpapte->pte, &gpapte->ptenum); 354 error = 0; 355 break; 356 default: 357 error = ENOTTY; 358 break; 359 } 360 361 if (state_changed == 1) { 362 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE); 363 } else if (state_changed == 2) { 364 for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++) 365 vcpu_set_state(sc->vm, vcpu, VCPU_IDLE); 366 } 367 368done: 369 /* Make sure that no handler returns a bogus value like ERESTART */ 370 KASSERT(error >= 0, ("vmmdev_ioctl: invalid error return %d", error)); 371 return (error); 372} 373 374static int 375vmmdev_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, 376 vm_size_t size, struct vm_object **object, int nprot) 377{ 378 int error; 379 struct vmmdev_softc *sc; 380 381 sc = vmmdev_lookup2(cdev); 382 if (sc != NULL && (nprot & PROT_EXEC) == 0) 383 error = vm_get_memobj(sc->vm, *offset, size, offset, object); 384 else 385 error = EINVAL; 386 387 return (error); 388} 389 390static void 391vmmdev_destroy(void *arg) 392{ 393 394 struct vmmdev_softc *sc = arg; 395 396 if (sc->cdev != NULL) 397 destroy_dev(sc->cdev); 398 399 if (sc->vm != NULL) 400 vm_destroy(sc->vm); 401 402 if ((sc->flags & VSC_LINKED) != 0) { 403 mtx_lock(&vmmdev_mtx); 404 SLIST_REMOVE(&head, sc, vmmdev_softc, link); 405 mtx_unlock(&vmmdev_mtx); 406 } 407 408 free(sc, M_VMMDEV); 409} 410 411static int 412sysctl_vmm_destroy(SYSCTL_HANDLER_ARGS) 413{ 414 int error; 415 char buf[VM_MAX_NAMELEN]; 416 struct vmmdev_softc *sc; 417 struct cdev *cdev; 418 419 strlcpy(buf, "beavis", sizeof(buf)); 420 error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 421 if (error != 0 || req->newptr == NULL) 422 return (error); 423 424 mtx_lock(&vmmdev_mtx); 425 sc = vmmdev_lookup(buf); 426 if (sc == NULL || sc->cdev == NULL) { 427 mtx_unlock(&vmmdev_mtx); 428 return (EINVAL); 429 } 430 431 /* 432 * The 'cdev' will be destroyed asynchronously when 'si_threadcount' 433 * goes down to 0 so we should not do it again in the callback. 434 */ 435 cdev = sc->cdev; 436 sc->cdev = NULL; 437 mtx_unlock(&vmmdev_mtx); 438 439 /* 440 * Schedule the 'cdev' to be destroyed: 441 * 442 * - any new operations on this 'cdev' will return an error (ENXIO). 443 * 444 * - when the 'si_threadcount' dwindles down to zero the 'cdev' will 445 * be destroyed and the callback will be invoked in a taskqueue 446 * context. 447 */ 448 destroy_dev_sched_cb(cdev, vmmdev_destroy, sc); 449 450 return (0); 451} 452SYSCTL_PROC(_hw_vmm, OID_AUTO, destroy, CTLTYPE_STRING | CTLFLAG_RW, 453 NULL, 0, sysctl_vmm_destroy, "A", NULL); 454 455static struct cdevsw vmmdevsw = { 456 .d_name = "vmmdev", 457 .d_version = D_VERSION, 458 .d_ioctl = vmmdev_ioctl, 459 .d_mmap_single = vmmdev_mmap_single, 460 .d_read = vmmdev_rw, 461 .d_write = vmmdev_rw, 462}; 463 464static int 465sysctl_vmm_create(SYSCTL_HANDLER_ARGS) 466{ 467 int error; 468 struct vm *vm; 469 struct cdev *cdev; 470 struct vmmdev_softc *sc, *sc2; 471 char buf[VM_MAX_NAMELEN]; 472 473 strlcpy(buf, "beavis", sizeof(buf)); 474 error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 475 if (error != 0 || req->newptr == NULL) 476 return (error); 477 478 mtx_lock(&vmmdev_mtx); 479 sc = vmmdev_lookup(buf); 480 mtx_unlock(&vmmdev_mtx); 481 if (sc != NULL) 482 return (EEXIST); 483 484 error = vm_create(buf, &vm); 485 if (error != 0) 486 return (error); 487 488 sc = malloc(sizeof(struct vmmdev_softc), M_VMMDEV, M_WAITOK | M_ZERO); 489 sc->vm = vm; 490 491 /* 492 * Lookup the name again just in case somebody sneaked in when we 493 * dropped the lock. 494 */ 495 mtx_lock(&vmmdev_mtx); 496 sc2 = vmmdev_lookup(buf); 497 if (sc2 == NULL) { 498 SLIST_INSERT_HEAD(&head, sc, link); 499 sc->flags |= VSC_LINKED; 500 } 501 mtx_unlock(&vmmdev_mtx); 502 503 if (sc2 != NULL) { 504 vmmdev_destroy(sc); 505 return (EEXIST); 506 } 507 508 error = make_dev_p(MAKEDEV_CHECKNAME, &cdev, &vmmdevsw, NULL, 509 UID_ROOT, GID_WHEEL, 0600, "vmm/%s", buf); 510 if (error != 0) { 511 vmmdev_destroy(sc); 512 return (error); 513 } 514 515 mtx_lock(&vmmdev_mtx); 516 sc->cdev = cdev; 517 sc->cdev->si_drv1 = sc; 518 mtx_unlock(&vmmdev_mtx); 519 520 return (0); 521} 522SYSCTL_PROC(_hw_vmm, OID_AUTO, create, CTLTYPE_STRING | CTLFLAG_RW, 523 NULL, 0, sysctl_vmm_create, "A", NULL); 524 525void 526vmmdev_init(void) 527{ 528 mtx_init(&vmmdev_mtx, "vmm device mutex", NULL, MTX_DEF); 529} 530 531int 532vmmdev_cleanup(void) 533{ 534 int error; 535 536 if (SLIST_EMPTY(&head)) 537 error = 0; 538 else 539 error = EBUSY; 540 541 return (error); 542} 543