1 /* 2 * vhost support 3 * 4 * Copyright Red Hat, Inc. 2010 5 * 6 * Authors: 7 * Michael S. Tsirkin <mst@redhat.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. See 10 * the COPYING file in the top-level directory. 11 * 12 * Contributions after 2012-01-13 are licensed under the terms of the 13 * GNU GPL, version 2 or (at your option) any later version. 14 */ 15 16 #include "qemu/osdep.h" 17 #include "qapi/error.h" 18 #include "hw/virtio/vhost.h" 19 #include "hw/hw.h" 20 #include "qemu/atomic.h" 21 #include "qemu/range.h" 22 #include "qemu/error-report.h" 23 #include "qemu/memfd.h" 24 #include <linux/vhost.h> 25 #include "exec/address-spaces.h" 26 #include "hw/virtio/virtio-bus.h" 27 #include "hw/virtio/virtio-access.h" 28 #include "migration/blocker.h" 29 #include "sysemu/dma.h" 30 #include "trace.h" 31 32 /* enabled until disconnected backend stabilizes */ 33 #define _VHOST_DEBUG 1 34 35 #ifdef _VHOST_DEBUG 36 #define VHOST_OPS_DEBUG(fmt, ...) \ 37 do { error_report(fmt ": %s (%d)", ## __VA_ARGS__, \ 38 strerror(errno), errno); } while (0) 39 #else 40 #define VHOST_OPS_DEBUG(fmt, ...) \ 41 do { } while (0) 42 #endif 43 44 static struct vhost_log *vhost_log; 45 static struct vhost_log *vhost_log_shm; 46 47 static unsigned int used_memslots; 48 static QLIST_HEAD(, vhost_dev) vhost_devices = 49 QLIST_HEAD_INITIALIZER(vhost_devices); 50 51 bool vhost_has_free_slot(void) 52 { 53 unsigned int slots_limit = ~0U; 54 struct vhost_dev *hdev; 55 56 QLIST_FOREACH(hdev, &vhost_devices, entry) { 57 unsigned int r = hdev->vhost_ops->vhost_backend_memslots_limit(hdev); 58 slots_limit = MIN(slots_limit, r); 59 } 60 return slots_limit > used_memslots; 61 } 62 63 static void vhost_dev_sync_region(struct vhost_dev *dev, 64 MemoryRegionSection *section, 65 uint64_t mfirst, uint64_t mlast, 66 uint64_t rfirst, uint64_t rlast) 67 { 68 vhost_log_chunk_t *log = dev->log->log; 69 70 uint64_t start = MAX(mfirst, rfirst); 71 uint64_t end = MIN(mlast, rlast); 72 vhost_log_chunk_t *from = log + start / VHOST_LOG_CHUNK; 73 vhost_log_chunk_t *to = log + end / VHOST_LOG_CHUNK + 1; 74 uint64_t addr = QEMU_ALIGN_DOWN(start, VHOST_LOG_CHUNK); 75 76 if (end < start) { 77 return; 78 } 79 assert(end / VHOST_LOG_CHUNK < dev->log_size); 80 assert(start / VHOST_LOG_CHUNK < dev->log_size); 81 82 for (;from < to; ++from) { 83 vhost_log_chunk_t log; 84 /* We first check with non-atomic: much cheaper, 85 * and we expect non-dirty to be the common case. */ 86 if (!*from) { 87 addr += VHOST_LOG_CHUNK; 88 continue; 89 } 90 /* Data must be read atomically. We don't really need barrier semantics 91 * but it's easier to use atomic_* than roll our own. */ 92 log = atomic_xchg(from, 0); 93 while (log) { 94 int bit = ctzl(log); 95 hwaddr page_addr; 96 hwaddr section_offset; 97 hwaddr mr_offset; 98 page_addr = addr + bit * VHOST_LOG_PAGE; 99 section_offset = page_addr - section->offset_within_address_space; 100 mr_offset = section_offset + section->offset_within_region; 101 memory_region_set_dirty(section->mr, mr_offset, VHOST_LOG_PAGE); 102 log &= ~(0x1ull << bit); 103 } 104 addr += VHOST_LOG_CHUNK; 105 } 106 } 107 108 static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, 109 MemoryRegionSection *section, 110 hwaddr first, 111 hwaddr last) 112 { 113 int i; 114 hwaddr start_addr; 115 hwaddr end_addr; 116 117 if (!dev->log_enabled || !dev->started) { 118 return 0; 119 } 120 start_addr = section->offset_within_address_space; 121 end_addr = range_get_last(start_addr, int128_get64(section->size)); 122 start_addr = MAX(first, start_addr); 123 end_addr = MIN(last, end_addr); 124 125 for (i = 0; i < dev->mem->nregions; ++i) { 126 struct vhost_memory_region *reg = dev->mem->regions + i; 127 vhost_dev_sync_region(dev, section, start_addr, end_addr, 128 reg->guest_phys_addr, 129 range_get_last(reg->guest_phys_addr, 130 reg->memory_size)); 131 } 132 for (i = 0; i < dev->nvqs; ++i) { 133 struct vhost_virtqueue *vq = dev->vqs + i; 134 vhost_dev_sync_region(dev, section, start_addr, end_addr, vq->used_phys, 135 range_get_last(vq->used_phys, vq->used_size)); 136 } 137 return 0; 138 } 139 140 static void vhost_log_sync(MemoryListener *listener, 141 MemoryRegionSection *section) 142 { 143 struct vhost_dev *dev = container_of(listener, struct vhost_dev, 144 memory_listener); 145 vhost_sync_dirty_bitmap(dev, section, 0x0, ~0x0ULL); 146 } 147 148 static void vhost_log_sync_range(struct vhost_dev *dev, 149 hwaddr first, hwaddr last) 150 { 151 int i; 152 /* FIXME: this is N^2 in number of sections */ 153 for (i = 0; i < dev->n_mem_sections; ++i) { 154 MemoryRegionSection *section = &dev->mem_sections[i]; 155 vhost_sync_dirty_bitmap(dev, section, first, last); 156 } 157 } 158 159 static uint64_t vhost_get_log_size(struct vhost_dev *dev) 160 { 161 uint64_t log_size = 0; 162 int i; 163 for (i = 0; i < dev->mem->nregions; ++i) { 164 struct vhost_memory_region *reg = dev->mem->regions + i; 165 uint64_t last = range_get_last(reg->guest_phys_addr, 166 reg->memory_size); 167 log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1); 168 } 169 for (i = 0; i < dev->nvqs; ++i) { 170 struct vhost_virtqueue *vq = dev->vqs + i; 171 uint64_t last = vq->used_phys + vq->used_size - 1; 172 log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1); 173 } 174 return log_size; 175 } 176 177 static struct vhost_log *vhost_log_alloc(uint64_t size, bool share) 178 { 179 Error *err = NULL; 180 struct vhost_log *log; 181 uint64_t logsize = size * sizeof(*(log->log)); 182 int fd = -1; 183 184 log = g_new0(struct vhost_log, 1); 185 if (share) { 186 log->log = qemu_memfd_alloc("vhost-log", logsize, 187 F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL, 188 &fd, &err); 189 if (err) { 190 error_report_err(err); 191 g_free(log); 192 return NULL; 193 } 194 memset(log->log, 0, logsize); 195 } else { 196 log->log = g_malloc0(logsize); 197 } 198 199 log->size = size; 200 log->refcnt = 1; 201 log->fd = fd; 202 203 return log; 204 } 205 206 static struct vhost_log *vhost_log_get(uint64_t size, bool share) 207 { 208 struct vhost_log *log = share ? vhost_log_shm : vhost_log; 209 210 if (!log || log->size != size) { 211 log = vhost_log_alloc(size, share); 212 if (share) { 213 vhost_log_shm = log; 214 } else { 215 vhost_log = log; 216 } 217 } else { 218 ++log->refcnt; 219 } 220 221 return log; 222 } 223 224 static void vhost_log_put(struct vhost_dev *dev, bool sync) 225 { 226 struct vhost_log *log = dev->log; 227 228 if (!log) { 229 return; 230 } 231 232 --log->refcnt; 233 if (log->refcnt == 0) { 234 /* Sync only the range covered by the old log */ 235 if (dev->log_size && sync) { 236 vhost_log_sync_range(dev, 0, dev->log_size * VHOST_LOG_CHUNK - 1); 237 } 238 239 if (vhost_log == log) { 240 g_free(log->log); 241 vhost_log = NULL; 242 } else if (vhost_log_shm == log) { 243 qemu_memfd_free(log->log, log->size * sizeof(*(log->log)), 244 log->fd); 245 vhost_log_shm = NULL; 246 } 247 248 g_free(log); 249 } 250 251 dev->log = NULL; 252 dev->log_size = 0; 253 } 254 255 static bool vhost_dev_log_is_shared(struct vhost_dev *dev) 256 { 257 return dev->vhost_ops->vhost_requires_shm_log && 258 dev->vhost_ops->vhost_requires_shm_log(dev); 259 } 260 261 static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) 262 { 263 struct vhost_log *log = vhost_log_get(size, vhost_dev_log_is_shared(dev)); 264 uint64_t log_base = (uintptr_t)log->log; 265 int r; 266 267 /* inform backend of log switching, this must be done before 268 releasing the current log, to ensure no logging is lost */ 269 r = dev->vhost_ops->vhost_set_log_base(dev, log_base, log); 270 if (r < 0) { 271 VHOST_OPS_DEBUG("vhost_set_log_base failed"); 272 } 273 274 vhost_log_put(dev, true); 275 dev->log = log; 276 dev->log_size = size; 277 } 278 279 static int vhost_dev_has_iommu(struct vhost_dev *dev) 280 { 281 VirtIODevice *vdev = dev->vdev; 282 283 return virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM); 284 } 285 286 static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr, 287 hwaddr *plen, int is_write) 288 { 289 if (!vhost_dev_has_iommu(dev)) { 290 return cpu_physical_memory_map(addr, plen, is_write); 291 } else { 292 return (void *)(uintptr_t)addr; 293 } 294 } 295 296 static void vhost_memory_unmap(struct vhost_dev *dev, void *buffer, 297 hwaddr len, int is_write, 298 hwaddr access_len) 299 { 300 if (!vhost_dev_has_iommu(dev)) { 301 cpu_physical_memory_unmap(buffer, len, is_write, access_len); 302 } 303 } 304 305 static int vhost_verify_ring_part_mapping(void *ring_hva, 306 uint64_t ring_gpa, 307 uint64_t ring_size, 308 void *reg_hva, 309 uint64_t reg_gpa, 310 uint64_t reg_size) 311 { 312 uint64_t hva_ring_offset; 313 uint64_t ring_last = range_get_last(ring_gpa, ring_size); 314 uint64_t reg_last = range_get_last(reg_gpa, reg_size); 315 316 if (ring_last < reg_gpa || ring_gpa > reg_last) { 317 return 0; 318 } 319 /* check that whole ring's is mapped */ 320 if (ring_last > reg_last) { 321 return -ENOMEM; 322 } 323 /* check that ring's MemoryRegion wasn't replaced */ 324 hva_ring_offset = ring_gpa - reg_gpa; 325 if (ring_hva != reg_hva + hva_ring_offset) { 326 return -EBUSY; 327 } 328 329 return 0; 330 } 331 332 static int vhost_verify_ring_mappings(struct vhost_dev *dev, 333 void *reg_hva, 334 uint64_t reg_gpa, 335 uint64_t reg_size) 336 { 337 int i, j; 338 int r = 0; 339 const char *part_name[] = { 340 "descriptor table", 341 "available ring", 342 "used ring" 343 }; 344 345 if (vhost_dev_has_iommu(dev)) { 346 return 0; 347 } 348 349 for (i = 0; i < dev->nvqs; ++i) { 350 struct vhost_virtqueue *vq = dev->vqs + i; 351 352 if (vq->desc_phys == 0) { 353 continue; 354 } 355 356 j = 0; 357 r = vhost_verify_ring_part_mapping( 358 vq->desc, vq->desc_phys, vq->desc_size, 359 reg_hva, reg_gpa, reg_size); 360 if (r) { 361 break; 362 } 363 364 j++; 365 r = vhost_verify_ring_part_mapping( 366 vq->avail, vq->avail_phys, vq->avail_size, 367 reg_hva, reg_gpa, reg_size); 368 if (r) { 369 break; 370 } 371 372 j++; 373 r = vhost_verify_ring_part_mapping( 374 vq->used, vq->used_phys, vq->used_size, 375 reg_hva, reg_gpa, reg_size); 376 if (r) { 377 break; 378 } 379 } 380 381 if (r == -ENOMEM) { 382 error_report("Unable to map %s for ring %d", part_name[j], i); 383 } else if (r == -EBUSY) { 384 error_report("%s relocated for ring %d", part_name[j], i); 385 } 386 return r; 387 } 388 389 static bool vhost_section(struct vhost_dev *dev, MemoryRegionSection *section) 390 { 391 bool result; 392 bool log_dirty = memory_region_get_dirty_log_mask(section->mr) & 393 ~(1 << DIRTY_MEMORY_MIGRATION); 394 result = memory_region_is_ram(section->mr) && 395 !memory_region_is_rom(section->mr); 396 397 /* Vhost doesn't handle any block which is doing dirty-tracking other 398 * than migration; this typically fires on VGA areas. 399 */ 400 result &= !log_dirty; 401 402 if (result && dev->vhost_ops->vhost_backend_mem_section_filter) { 403 result &= 404 dev->vhost_ops->vhost_backend_mem_section_filter(dev, section); 405 } 406 407 trace_vhost_section(section->mr->name, result); 408 return result; 409 } 410 411 static void vhost_begin(MemoryListener *listener) 412 { 413 struct vhost_dev *dev = container_of(listener, struct vhost_dev, 414 memory_listener); 415 dev->tmp_sections = NULL; 416 dev->n_tmp_sections = 0; 417 } 418 419 static void vhost_commit(MemoryListener *listener) 420 { 421 struct vhost_dev *dev = container_of(listener, struct vhost_dev, 422 memory_listener); 423 MemoryRegionSection *old_sections; 424 int n_old_sections; 425 uint64_t log_size; 426 size_t regions_size; 427 int r; 428 int i; 429 bool changed = false; 430 431 /* Note we can be called before the device is started, but then 432 * starting the device calls set_mem_table, so we need to have 433 * built the data structures. 434 */ 435 old_sections = dev->mem_sections; 436 n_old_sections = dev->n_mem_sections; 437 dev->mem_sections = dev->tmp_sections; 438 dev->n_mem_sections = dev->n_tmp_sections; 439 440 if (dev->n_mem_sections != n_old_sections) { 441 changed = true; 442 } else { 443 /* Same size, lets check the contents */ 444 changed = n_old_sections && memcmp(dev->mem_sections, old_sections, 445 n_old_sections * sizeof(old_sections[0])) != 0; 446 } 447 448 trace_vhost_commit(dev->started, changed); 449 if (!changed) { 450 goto out; 451 } 452 453 /* Rebuild the regions list from the new sections list */ 454 regions_size = offsetof(struct vhost_memory, regions) + 455 dev->n_mem_sections * sizeof dev->mem->regions[0]; 456 dev->mem = g_realloc(dev->mem, regions_size); 457 dev->mem->nregions = dev->n_mem_sections; 458 used_memslots = dev->mem->nregions; 459 for (i = 0; i < dev->n_mem_sections; i++) { 460 struct vhost_memory_region *cur_vmr = dev->mem->regions + i; 461 struct MemoryRegionSection *mrs = dev->mem_sections + i; 462 463 cur_vmr->guest_phys_addr = mrs->offset_within_address_space; 464 cur_vmr->memory_size = int128_get64(mrs->size); 465 cur_vmr->userspace_addr = 466 (uintptr_t)memory_region_get_ram_ptr(mrs->mr) + 467 mrs->offset_within_region; 468 cur_vmr->flags_padding = 0; 469 } 470 471 if (!dev->started) { 472 goto out; 473 } 474 475 for (i = 0; i < dev->mem->nregions; i++) { 476 if (vhost_verify_ring_mappings(dev, 477 (void *)(uintptr_t)dev->mem->regions[i].userspace_addr, 478 dev->mem->regions[i].guest_phys_addr, 479 dev->mem->regions[i].memory_size)) { 480 error_report("Verify ring failure on region %d", i); 481 abort(); 482 } 483 } 484 485 if (!dev->log_enabled) { 486 r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem); 487 if (r < 0) { 488 VHOST_OPS_DEBUG("vhost_set_mem_table failed"); 489 } 490 goto out; 491 } 492 log_size = vhost_get_log_size(dev); 493 /* We allocate an extra 4K bytes to log, 494 * to reduce the * number of reallocations. */ 495 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log) 496 /* To log more, must increase log size before table update. */ 497 if (dev->log_size < log_size) { 498 vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER); 499 } 500 r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem); 501 if (r < 0) { 502 VHOST_OPS_DEBUG("vhost_set_mem_table failed"); 503 } 504 /* To log less, can only decrease log size after table update. */ 505 if (dev->log_size > log_size + VHOST_LOG_BUFFER) { 506 vhost_dev_log_resize(dev, log_size); 507 } 508 509 out: 510 /* Deref the old list of sections, this must happen _after_ the 511 * vhost_set_mem_table to ensure the client isn't still using the 512 * section we're about to unref. 513 */ 514 while (n_old_sections--) { 515 memory_region_unref(old_sections[n_old_sections].mr); 516 } 517 g_free(old_sections); 518 return; 519 } 520 521 /* Adds the section data to the tmp_section structure. 522 * It relies on the listener calling us in memory address order 523 * and for each region (via the _add and _nop methods) to 524 * join neighbours. 525 */ 526 static void vhost_region_add_section(struct vhost_dev *dev, 527 MemoryRegionSection *section) 528 { 529 bool need_add = true; 530 uint64_t mrs_size = int128_get64(section->size); 531 uint64_t mrs_gpa = section->offset_within_address_space; 532 uintptr_t mrs_host = (uintptr_t)memory_region_get_ram_ptr(section->mr) + 533 section->offset_within_region; 534 RAMBlock *mrs_rb = section->mr->ram_block; 535 size_t mrs_page = qemu_ram_pagesize(mrs_rb); 536 537 trace_vhost_region_add_section(section->mr->name, mrs_gpa, mrs_size, 538 mrs_host); 539 540 /* Round the section to it's page size */ 541 /* First align the start down to a page boundary */ 542 uint64_t alignage = mrs_host & (mrs_page - 1); 543 if (alignage) { 544 mrs_host -= alignage; 545 mrs_size += alignage; 546 mrs_gpa -= alignage; 547 } 548 /* Now align the size up to a page boundary */ 549 alignage = mrs_size & (mrs_page - 1); 550 if (alignage) { 551 mrs_size += mrs_page - alignage; 552 } 553 trace_vhost_region_add_section_aligned(section->mr->name, mrs_gpa, mrs_size, 554 mrs_host); 555 556 if (dev->n_tmp_sections) { 557 /* Since we already have at least one section, lets see if 558 * this extends it; since we're scanning in order, we only 559 * have to look at the last one, and the FlatView that calls 560 * us shouldn't have overlaps. 561 */ 562 MemoryRegionSection *prev_sec = dev->tmp_sections + 563 (dev->n_tmp_sections - 1); 564 uint64_t prev_gpa_start = prev_sec->offset_within_address_space; 565 uint64_t prev_size = int128_get64(prev_sec->size); 566 uint64_t prev_gpa_end = range_get_last(prev_gpa_start, prev_size); 567 uint64_t prev_host_start = 568 (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr) + 569 prev_sec->offset_within_region; 570 uint64_t prev_host_end = range_get_last(prev_host_start, prev_size); 571 572 if (mrs_gpa <= (prev_gpa_end + 1)) { 573 /* OK, looks like overlapping/intersecting - it's possible that 574 * the rounding to page sizes has made them overlap, but they should 575 * match up in the same RAMBlock if they do. 576 */ 577 if (mrs_gpa < prev_gpa_start) { 578 error_report("%s:Section rounded to %"PRIx64 579 " prior to previous %"PRIx64, 580 __func__, mrs_gpa, prev_gpa_start); 581 /* A way to cleanly fail here would be better */ 582 return; 583 } 584 /* Offset from the start of the previous GPA to this GPA */ 585 size_t offset = mrs_gpa - prev_gpa_start; 586 587 if (prev_host_start + offset == mrs_host && 588 section->mr == prev_sec->mr && 589 (!dev->vhost_ops->vhost_backend_can_merge || 590 dev->vhost_ops->vhost_backend_can_merge(dev, 591 mrs_host, mrs_size, 592 prev_host_start, prev_size))) { 593 uint64_t max_end = MAX(prev_host_end, mrs_host + mrs_size); 594 need_add = false; 595 prev_sec->offset_within_address_space = 596 MIN(prev_gpa_start, mrs_gpa); 597 prev_sec->offset_within_region = 598 MIN(prev_host_start, mrs_host) - 599 (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr); 600 prev_sec->size = int128_make64(max_end - MIN(prev_host_start, 601 mrs_host)); 602 trace_vhost_region_add_section_merge(section->mr->name, 603 int128_get64(prev_sec->size), 604 prev_sec->offset_within_address_space, 605 prev_sec->offset_within_region); 606 } else { 607 /* adjoining regions are fine, but overlapping ones with 608 * different blocks/offsets shouldn't happen 609 */ 610 if (mrs_gpa != prev_gpa_end + 1) { 611 error_report("%s: Overlapping but not coherent sections " 612 "at %"PRIx64, 613 __func__, mrs_gpa); 614 return; 615 } 616 } 617 } 618 } 619 620 if (need_add) { 621 ++dev->n_tmp_sections; 622 dev->tmp_sections = g_renew(MemoryRegionSection, dev->tmp_sections, 623 dev->n_tmp_sections); 624 dev->tmp_sections[dev->n_tmp_sections - 1] = *section; 625 /* The flatview isn't stable and we don't use it, making it NULL 626 * means we can memcmp the list. 627 */ 628 dev->tmp_sections[dev->n_tmp_sections - 1].fv = NULL; 629 memory_region_ref(section->mr); 630 } 631 } 632 633 /* Used for both add and nop callbacks */ 634 static void vhost_region_addnop(MemoryListener *listener, 635 MemoryRegionSection *section) 636 { 637 struct vhost_dev *dev = container_of(listener, struct vhost_dev, 638 memory_listener); 639 640 if (!vhost_section(dev, section)) { 641 return; 642 } 643 vhost_region_add_section(dev, section); 644 } 645 646 static void vhost_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) 647 { 648 struct vhost_iommu *iommu = container_of(n, struct vhost_iommu, n); 649 struct vhost_dev *hdev = iommu->hdev; 650 hwaddr iova = iotlb->iova + iommu->iommu_offset; 651 652 if (vhost_backend_invalidate_device_iotlb(hdev, iova, 653 iotlb->addr_mask + 1)) { 654 error_report("Fail to invalidate device iotlb"); 655 } 656 } 657 658 static void vhost_iommu_region_add(MemoryListener *listener, 659 MemoryRegionSection *section) 660 { 661 struct vhost_dev *dev = container_of(listener, struct vhost_dev, 662 iommu_listener); 663 struct vhost_iommu *iommu; 664 Int128 end; 665 666 if (!memory_region_is_iommu(section->mr)) { 667 return; 668 } 669 670 iommu = g_malloc0(sizeof(*iommu)); 671 end = int128_add(int128_make64(section->offset_within_region), 672 section->size); 673 end = int128_sub(end, int128_one()); 674 iommu_notifier_init(&iommu->n, vhost_iommu_unmap_notify, 675 IOMMU_NOTIFIER_UNMAP, 676 section->offset_within_region, 677 int128_get64(end)); 678 iommu->mr = section->mr; 679 iommu->iommu_offset = section->offset_within_address_space - 680 section->offset_within_region; 681 iommu->hdev = dev; 682 memory_region_register_iommu_notifier(section->mr, &iommu->n); 683 QLIST_INSERT_HEAD(&dev->iommu_list, iommu, iommu_next); 684 /* TODO: can replay help performance here? */ 685 } 686 687 static void vhost_iommu_region_del(MemoryListener *listener, 688 MemoryRegionSection *section) 689 { 690 struct vhost_dev *dev = container_of(listener, struct vhost_dev, 691 iommu_listener); 692 struct vhost_iommu *iommu; 693 694 if (!memory_region_is_iommu(section->mr)) { 695 return; 696 } 697 698 QLIST_FOREACH(iommu, &dev->iommu_list, iommu_next) { 699 if (iommu->mr == section->mr && 700 iommu->n.start == section->offset_within_region) { 701 memory_region_unregister_iommu_notifier(iommu->mr, 702 &iommu->n); 703 QLIST_REMOVE(iommu, iommu_next); 704 g_free(iommu); 705 break; 706 } 707 } 708 } 709 710 static int vhost_virtqueue_set_addr(struct vhost_dev *dev, 711 struct vhost_virtqueue *vq, 712 unsigned idx, bool enable_log) 713 { 714 struct vhost_vring_addr addr = { 715 .index = idx, 716 .desc_user_addr = (uint64_t)(unsigned long)vq->desc, 717 .avail_user_addr = (uint64_t)(unsigned long)vq->avail, 718 .used_user_addr = (uint64_t)(unsigned long)vq->used, 719 .log_guest_addr = vq->used_phys, 720 .flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0, 721 }; 722 int r = dev->vhost_ops->vhost_set_vring_addr(dev, &addr); 723 if (r < 0) { 724 VHOST_OPS_DEBUG("vhost_set_vring_addr failed"); 725 return -errno; 726 } 727 return 0; 728 } 729 730 static int vhost_dev_set_features(struct vhost_dev *dev, 731 bool enable_log) 732 { 733 uint64_t features = dev->acked_features; 734 int r; 735 if (enable_log) { 736 features |= 0x1ULL << VHOST_F_LOG_ALL; 737 } 738 r = dev->vhost_ops->vhost_set_features(dev, features); 739 if (r < 0) { 740 VHOST_OPS_DEBUG("vhost_set_features failed"); 741 } 742 return r < 0 ? -errno : 0; 743 } 744 745 static int vhost_dev_set_log(struct vhost_dev *dev, bool enable_log) 746 { 747 int r, i, idx; 748 r = vhost_dev_set_features(dev, enable_log); 749 if (r < 0) { 750 goto err_features; 751 } 752 for (i = 0; i < dev->nvqs; ++i) { 753 idx = dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i); 754 r = vhost_virtqueue_set_addr(dev, dev->vqs + i, idx, 755 enable_log); 756 if (r < 0) { 757 goto err_vq; 758 } 759 } 760 return 0; 761 err_vq: 762 for (; i >= 0; --i) { 763 idx = dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i); 764 vhost_virtqueue_set_addr(dev, dev->vqs + i, idx, 765 dev->log_enabled); 766 } 767 vhost_dev_set_features(dev, dev->log_enabled); 768 err_features: 769 return r; 770 } 771 772 static int vhost_migration_log(MemoryListener *listener, int enable) 773 { 774 struct vhost_dev *dev = container_of(listener, struct vhost_dev, 775 memory_listener); 776 int r; 777 if (!!enable == dev->log_enabled) { 778 return 0; 779 } 780 if (!dev->started) { 781 dev->log_enabled = enable; 782 return 0; 783 } 784 if (!enable) { 785 r = vhost_dev_set_log(dev, false); 786 if (r < 0) { 787 return r; 788 } 789 vhost_log_put(dev, false); 790 } else { 791 vhost_dev_log_resize(dev, vhost_get_log_size(dev)); 792 r = vhost_dev_set_log(dev, true); 793 if (r < 0) { 794 return r; 795 } 796 } 797 dev->log_enabled = enable; 798 return 0; 799 } 800 801 static void vhost_log_global_start(MemoryListener *listener) 802 { 803 int r; 804 805 r = vhost_migration_log(listener, true); 806 if (r < 0) { 807 abort(); 808 } 809 } 810 811 static void vhost_log_global_stop(MemoryListener *listener) 812 { 813 int r; 814 815 r = vhost_migration_log(listener, false); 816 if (r < 0) { 817 abort(); 818 } 819 } 820 821 static void vhost_log_start(MemoryListener *listener, 822 MemoryRegionSection *section, 823 int old, int new) 824 { 825 /* FIXME: implement */ 826 } 827 828 static void vhost_log_stop(MemoryListener *listener, 829 MemoryRegionSection *section, 830 int old, int new) 831 { 832 /* FIXME: implement */ 833 } 834 835 /* The vhost driver natively knows how to handle the vrings of non 836 * cross-endian legacy devices and modern devices. Only legacy devices 837 * exposed to a bi-endian guest may require the vhost driver to use a 838 * specific endianness. 839 */ 840 static inline bool vhost_needs_vring_endian(VirtIODevice *vdev) 841 { 842 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { 843 return false; 844 } 845 #ifdef HOST_WORDS_BIGENDIAN 846 return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_LITTLE; 847 #else 848 return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG; 849 #endif 850 } 851 852 static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev *dev, 853 bool is_big_endian, 854 int vhost_vq_index) 855 { 856 struct vhost_vring_state s = { 857 .index = vhost_vq_index, 858 .num = is_big_endian 859 }; 860 861 if (!dev->vhost_ops->vhost_set_vring_endian(dev, &s)) { 862 return 0; 863 } 864 865 VHOST_OPS_DEBUG("vhost_set_vring_endian failed"); 866 if (errno == ENOTTY) { 867 error_report("vhost does not support cross-endian"); 868 return -ENOSYS; 869 } 870 871 return -errno; 872 } 873 874 static int vhost_memory_region_lookup(struct vhost_dev *hdev, 875 uint64_t gpa, uint64_t *uaddr, 876 uint64_t *len) 877 { 878 int i; 879 880 for (i = 0; i < hdev->mem->nregions; i++) { 881 struct vhost_memory_region *reg = hdev->mem->regions + i; 882 883 if (gpa >= reg->guest_phys_addr && 884 reg->guest_phys_addr + reg->memory_size > gpa) { 885 *uaddr = reg->userspace_addr + gpa - reg->guest_phys_addr; 886 *len = reg->guest_phys_addr + reg->memory_size - gpa; 887 return 0; 888 } 889 } 890 891 return -EFAULT; 892 } 893 894 int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write) 895 { 896 IOMMUTLBEntry iotlb; 897 uint64_t uaddr, len; 898 int ret = -EFAULT; 899 900 rcu_read_lock(); 901 902 trace_vhost_iotlb_miss(dev, 1); 903 904 iotlb = address_space_get_iotlb_entry(dev->vdev->dma_as, 905 iova, write, 906 MEMTXATTRS_UNSPECIFIED); 907 if (iotlb.target_as != NULL) { 908 ret = vhost_memory_region_lookup(dev, iotlb.translated_addr, 909 &uaddr, &len); 910 if (ret) { 911 trace_vhost_iotlb_miss(dev, 3); 912 error_report("Fail to lookup the translated address " 913 "%"PRIx64, iotlb.translated_addr); 914 goto out; 915 } 916 917 len = MIN(iotlb.addr_mask + 1, len); 918 iova = iova & ~iotlb.addr_mask; 919 920 ret = vhost_backend_update_device_iotlb(dev, iova, uaddr, 921 len, iotlb.perm); 922 if (ret) { 923 trace_vhost_iotlb_miss(dev, 4); 924 error_report("Fail to update device iotlb"); 925 goto out; 926 } 927 } 928 929 trace_vhost_iotlb_miss(dev, 2); 930 931 out: 932 rcu_read_unlock(); 933 934 return ret; 935 } 936 937 static int vhost_virtqueue_start(struct vhost_dev *dev, 938 struct VirtIODevice *vdev, 939 struct vhost_virtqueue *vq, 940 unsigned idx) 941 { 942 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 943 VirtioBusState *vbus = VIRTIO_BUS(qbus); 944 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); 945 hwaddr s, l, a; 946 int r; 947 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx); 948 struct vhost_vring_file file = { 949 .index = vhost_vq_index 950 }; 951 struct vhost_vring_state state = { 952 .index = vhost_vq_index 953 }; 954 struct VirtQueue *vvq = virtio_get_queue(vdev, idx); 955 956 a = virtio_queue_get_desc_addr(vdev, idx); 957 if (a == 0) { 958 /* Queue might not be ready for start */ 959 return 0; 960 } 961 962 vq->num = state.num = virtio_queue_get_num(vdev, idx); 963 r = dev->vhost_ops->vhost_set_vring_num(dev, &state); 964 if (r) { 965 VHOST_OPS_DEBUG("vhost_set_vring_num failed"); 966 return -errno; 967 } 968 969 state.num = virtio_queue_get_last_avail_idx(vdev, idx); 970 r = dev->vhost_ops->vhost_set_vring_base(dev, &state); 971 if (r) { 972 VHOST_OPS_DEBUG("vhost_set_vring_base failed"); 973 return -errno; 974 } 975 976 if (vhost_needs_vring_endian(vdev)) { 977 r = vhost_virtqueue_set_vring_endian_legacy(dev, 978 virtio_is_big_endian(vdev), 979 vhost_vq_index); 980 if (r) { 981 return -errno; 982 } 983 } 984 985 vq->desc_size = s = l = virtio_queue_get_desc_size(vdev, idx); 986 vq->desc_phys = a; 987 vq->desc = vhost_memory_map(dev, a, &l, 0); 988 if (!vq->desc || l != s) { 989 r = -ENOMEM; 990 goto fail_alloc_desc; 991 } 992 vq->avail_size = s = l = virtio_queue_get_avail_size(vdev, idx); 993 vq->avail_phys = a = virtio_queue_get_avail_addr(vdev, idx); 994 vq->avail = vhost_memory_map(dev, a, &l, 0); 995 if (!vq->avail || l != s) { 996 r = -ENOMEM; 997 goto fail_alloc_avail; 998 } 999 vq->used_size = s = l = virtio_queue_get_used_size(vdev, idx); 1000 vq->used_phys = a = virtio_queue_get_used_addr(vdev, idx); 1001 vq->used = vhost_memory_map(dev, a, &l, 1); 1002 if (!vq->used || l != s) { 1003 r = -ENOMEM; 1004 goto fail_alloc_used; 1005 } 1006 1007 r = vhost_virtqueue_set_addr(dev, vq, vhost_vq_index, dev->log_enabled); 1008 if (r < 0) { 1009 r = -errno; 1010 goto fail_alloc; 1011 } 1012 1013 file.fd = event_notifier_get_fd(virtio_queue_get_host_notifier(vvq)); 1014 r = dev->vhost_ops->vhost_set_vring_kick(dev, &file); 1015 if (r) { 1016 VHOST_OPS_DEBUG("vhost_set_vring_kick failed"); 1017 r = -errno; 1018 goto fail_kick; 1019 } 1020 1021 /* Clear and discard previous events if any. */ 1022 event_notifier_test_and_clear(&vq->masked_notifier); 1023 1024 /* Init vring in unmasked state, unless guest_notifier_mask 1025 * will do it later. 1026 */ 1027 if (!vdev->use_guest_notifier_mask) { 1028 /* TODO: check and handle errors. */ 1029 vhost_virtqueue_mask(dev, vdev, idx, false); 1030 } 1031 1032 if (k->query_guest_notifiers && 1033 k->query_guest_notifiers(qbus->parent) && 1034 virtio_queue_vector(vdev, idx) == VIRTIO_NO_VECTOR) { 1035 file.fd = -1; 1036 r = dev->vhost_ops->vhost_set_vring_call(dev, &file); 1037 if (r) { 1038 goto fail_vector; 1039 } 1040 } 1041 1042 return 0; 1043 1044 fail_vector: 1045 fail_kick: 1046 fail_alloc: 1047 vhost_memory_unmap(dev, vq->used, virtio_queue_get_used_size(vdev, idx), 1048 0, 0); 1049 fail_alloc_used: 1050 vhost_memory_unmap(dev, vq->avail, virtio_queue_get_avail_size(vdev, idx), 1051 0, 0); 1052 fail_alloc_avail: 1053 vhost_memory_unmap(dev, vq->desc, virtio_queue_get_desc_size(vdev, idx), 1054 0, 0); 1055 fail_alloc_desc: 1056 return r; 1057 } 1058 1059 static void vhost_virtqueue_stop(struct vhost_dev *dev, 1060 struct VirtIODevice *vdev, 1061 struct vhost_virtqueue *vq, 1062 unsigned idx) 1063 { 1064 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx); 1065 struct vhost_vring_state state = { 1066 .index = vhost_vq_index, 1067 }; 1068 int r; 1069 int a; 1070 1071 a = virtio_queue_get_desc_addr(vdev, idx); 1072 if (a == 0) { 1073 /* Don't stop the virtqueue which might have not been started */ 1074 return; 1075 } 1076 1077 r = dev->vhost_ops->vhost_get_vring_base(dev, &state); 1078 if (r < 0) { 1079 VHOST_OPS_DEBUG("vhost VQ %d ring restore failed: %d", idx, r); 1080 /* Connection to the backend is broken, so let's sync internal 1081 * last avail idx to the device used idx. 1082 */ 1083 virtio_queue_restore_last_avail_idx(vdev, idx); 1084 } else { 1085 virtio_queue_set_last_avail_idx(vdev, idx, state.num); 1086 } 1087 virtio_queue_invalidate_signalled_used(vdev, idx); 1088 virtio_queue_update_used_idx(vdev, idx); 1089 1090 /* In the cross-endian case, we need to reset the vring endianness to 1091 * native as legacy devices expect so by default. 1092 */ 1093 if (vhost_needs_vring_endian(vdev)) { 1094 vhost_virtqueue_set_vring_endian_legacy(dev, 1095 !virtio_is_big_endian(vdev), 1096 vhost_vq_index); 1097 } 1098 1099 vhost_memory_unmap(dev, vq->used, virtio_queue_get_used_size(vdev, idx), 1100 1, virtio_queue_get_used_size(vdev, idx)); 1101 vhost_memory_unmap(dev, vq->avail, virtio_queue_get_avail_size(vdev, idx), 1102 0, virtio_queue_get_avail_size(vdev, idx)); 1103 vhost_memory_unmap(dev, vq->desc, virtio_queue_get_desc_size(vdev, idx), 1104 0, virtio_queue_get_desc_size(vdev, idx)); 1105 } 1106 1107 static void vhost_eventfd_add(MemoryListener *listener, 1108 MemoryRegionSection *section, 1109 bool match_data, uint64_t data, EventNotifier *e) 1110 { 1111 } 1112 1113 static void vhost_eventfd_del(MemoryListener *listener, 1114 MemoryRegionSection *section, 1115 bool match_data, uint64_t data, EventNotifier *e) 1116 { 1117 } 1118 1119 static int vhost_virtqueue_set_busyloop_timeout(struct vhost_dev *dev, 1120 int n, uint32_t timeout) 1121 { 1122 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n); 1123 struct vhost_vring_state state = { 1124 .index = vhost_vq_index, 1125 .num = timeout, 1126 }; 1127 int r; 1128 1129 if (!dev->vhost_ops->vhost_set_vring_busyloop_timeout) { 1130 return -EINVAL; 1131 } 1132 1133 r = dev->vhost_ops->vhost_set_vring_busyloop_timeout(dev, &state); 1134 if (r) { 1135 VHOST_OPS_DEBUG("vhost_set_vring_busyloop_timeout failed"); 1136 return r; 1137 } 1138 1139 return 0; 1140 } 1141 1142 static int vhost_virtqueue_init(struct vhost_dev *dev, 1143 struct vhost_virtqueue *vq, int n) 1144 { 1145 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n); 1146 struct vhost_vring_file file = { 1147 .index = vhost_vq_index, 1148 }; 1149 int r = event_notifier_init(&vq->masked_notifier, 0); 1150 if (r < 0) { 1151 return r; 1152 } 1153 1154 file.fd = event_notifier_get_fd(&vq->masked_notifier); 1155 r = dev->vhost_ops->vhost_set_vring_call(dev, &file); 1156 if (r) { 1157 VHOST_OPS_DEBUG("vhost_set_vring_call failed"); 1158 r = -errno; 1159 goto fail_call; 1160 } 1161 1162 vq->dev = dev; 1163 1164 return 0; 1165 fail_call: 1166 event_notifier_cleanup(&vq->masked_notifier); 1167 return r; 1168 } 1169 1170 static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq) 1171 { 1172 event_notifier_cleanup(&vq->masked_notifier); 1173 } 1174 1175 int vhost_dev_init(struct vhost_dev *hdev, void *opaque, 1176 VhostBackendType backend_type, uint32_t busyloop_timeout) 1177 { 1178 uint64_t features; 1179 int i, r, n_initialized_vqs = 0; 1180 Error *local_err = NULL; 1181 1182 hdev->vdev = NULL; 1183 hdev->migration_blocker = NULL; 1184 1185 r = vhost_set_backend_type(hdev, backend_type); 1186 assert(r >= 0); 1187 1188 r = hdev->vhost_ops->vhost_backend_init(hdev, opaque); 1189 if (r < 0) { 1190 goto fail; 1191 } 1192 1193 r = hdev->vhost_ops->vhost_set_owner(hdev); 1194 if (r < 0) { 1195 VHOST_OPS_DEBUG("vhost_set_owner failed"); 1196 goto fail; 1197 } 1198 1199 r = hdev->vhost_ops->vhost_get_features(hdev, &features); 1200 if (r < 0) { 1201 VHOST_OPS_DEBUG("vhost_get_features failed"); 1202 goto fail; 1203 } 1204 1205 for (i = 0; i < hdev->nvqs; ++i, ++n_initialized_vqs) { 1206 r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i); 1207 if (r < 0) { 1208 goto fail; 1209 } 1210 } 1211 1212 if (busyloop_timeout) { 1213 for (i = 0; i < hdev->nvqs; ++i) { 1214 r = vhost_virtqueue_set_busyloop_timeout(hdev, hdev->vq_index + i, 1215 busyloop_timeout); 1216 if (r < 0) { 1217 goto fail_busyloop; 1218 } 1219 } 1220 } 1221 1222 hdev->features = features; 1223 1224 hdev->memory_listener = (MemoryListener) { 1225 .begin = vhost_begin, 1226 .commit = vhost_commit, 1227 .region_add = vhost_region_addnop, 1228 .region_nop = vhost_region_addnop, 1229 .log_start = vhost_log_start, 1230 .log_stop = vhost_log_stop, 1231 .log_sync = vhost_log_sync, 1232 .log_global_start = vhost_log_global_start, 1233 .log_global_stop = vhost_log_global_stop, 1234 .eventfd_add = vhost_eventfd_add, 1235 .eventfd_del = vhost_eventfd_del, 1236 .priority = 10 1237 }; 1238 1239 hdev->iommu_listener = (MemoryListener) { 1240 .region_add = vhost_iommu_region_add, 1241 .region_del = vhost_iommu_region_del, 1242 }; 1243 1244 if (hdev->migration_blocker == NULL) { 1245 if (!(hdev->features & (0x1ULL << VHOST_F_LOG_ALL))) { 1246 error_setg(&hdev->migration_blocker, 1247 "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature."); 1248 } else if (vhost_dev_log_is_shared(hdev) && !qemu_memfd_alloc_check()) { 1249 error_setg(&hdev->migration_blocker, 1250 "Migration disabled: failed to allocate shared memory"); 1251 } 1252 } 1253 1254 if (hdev->migration_blocker != NULL) { 1255 r = migrate_add_blocker(hdev->migration_blocker, &local_err); 1256 if (local_err) { 1257 error_report_err(local_err); 1258 error_free(hdev->migration_blocker); 1259 goto fail_busyloop; 1260 } 1261 } 1262 1263 hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions)); 1264 hdev->n_mem_sections = 0; 1265 hdev->mem_sections = NULL; 1266 hdev->log = NULL; 1267 hdev->log_size = 0; 1268 hdev->log_enabled = false; 1269 hdev->started = false; 1270 memory_listener_register(&hdev->memory_listener, &address_space_memory); 1271 QLIST_INSERT_HEAD(&vhost_devices, hdev, entry); 1272 1273 if (used_memslots > hdev->vhost_ops->vhost_backend_memslots_limit(hdev)) { 1274 error_report("vhost backend memory slots limit is less" 1275 " than current number of present memory slots"); 1276 r = -1; 1277 if (busyloop_timeout) { 1278 goto fail_busyloop; 1279 } else { 1280 goto fail; 1281 } 1282 } 1283 1284 return 0; 1285 1286 fail_busyloop: 1287 while (--i >= 0) { 1288 vhost_virtqueue_set_busyloop_timeout(hdev, hdev->vq_index + i, 0); 1289 } 1290 fail: 1291 hdev->nvqs = n_initialized_vqs; 1292 vhost_dev_cleanup(hdev); 1293 return r; 1294 } 1295 1296 void vhost_dev_cleanup(struct vhost_dev *hdev) 1297 { 1298 int i; 1299 1300 for (i = 0; i < hdev->nvqs; ++i) { 1301 vhost_virtqueue_cleanup(hdev->vqs + i); 1302 } 1303 if (hdev->mem) { 1304 /* those are only safe after successful init */ 1305 memory_listener_unregister(&hdev->memory_listener); 1306 QLIST_REMOVE(hdev, entry); 1307 } 1308 if (hdev->migration_blocker) { 1309 migrate_del_blocker(hdev->migration_blocker); 1310 error_free(hdev->migration_blocker); 1311 } 1312 g_free(hdev->mem); 1313 g_free(hdev->mem_sections); 1314 if (hdev->vhost_ops) { 1315 hdev->vhost_ops->vhost_backend_cleanup(hdev); 1316 } 1317 assert(!hdev->log); 1318 1319 memset(hdev, 0, sizeof(struct vhost_dev)); 1320 } 1321 1322 /* Stop processing guest IO notifications in qemu. 1323 * Start processing them in vhost in kernel. 1324 */ 1325 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev) 1326 { 1327 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 1328 int i, r, e; 1329 1330 /* We will pass the notifiers to the kernel, make sure that QEMU 1331 * doesn't interfere. 1332 */ 1333 r = virtio_device_grab_ioeventfd(vdev); 1334 if (r < 0) { 1335 error_report("binding does not support host notifiers"); 1336 goto fail; 1337 } 1338 1339 for (i = 0; i < hdev->nvqs; ++i) { 1340 r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i, 1341 true); 1342 if (r < 0) { 1343 error_report("vhost VQ %d notifier binding failed: %d", i, -r); 1344 goto fail_vq; 1345 } 1346 } 1347 1348 return 0; 1349 fail_vq: 1350 while (--i >= 0) { 1351 e = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i, 1352 false); 1353 if (e < 0) { 1354 error_report("vhost VQ %d notifier cleanup error: %d", i, -r); 1355 } 1356 assert (e >= 0); 1357 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i); 1358 } 1359 virtio_device_release_ioeventfd(vdev); 1360 fail: 1361 return r; 1362 } 1363 1364 /* Stop processing guest IO notifications in vhost. 1365 * Start processing them in qemu. 1366 * This might actually run the qemu handlers right away, 1367 * so virtio in qemu must be completely setup when this is called. 1368 */ 1369 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev) 1370 { 1371 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 1372 int i, r; 1373 1374 for (i = 0; i < hdev->nvqs; ++i) { 1375 r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i, 1376 false); 1377 if (r < 0) { 1378 error_report("vhost VQ %d notifier cleanup failed: %d", i, -r); 1379 } 1380 assert (r >= 0); 1381 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i); 1382 } 1383 virtio_device_release_ioeventfd(vdev); 1384 } 1385 1386 /* Test and clear event pending status. 1387 * Should be called after unmask to avoid losing events. 1388 */ 1389 bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n) 1390 { 1391 struct vhost_virtqueue *vq = hdev->vqs + n - hdev->vq_index; 1392 assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs); 1393 return event_notifier_test_and_clear(&vq->masked_notifier); 1394 } 1395 1396 /* Mask/unmask events from this vq. */ 1397 void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n, 1398 bool mask) 1399 { 1400 struct VirtQueue *vvq = virtio_get_queue(vdev, n); 1401 int r, index = n - hdev->vq_index; 1402 struct vhost_vring_file file; 1403 1404 /* should only be called after backend is connected */ 1405 assert(hdev->vhost_ops); 1406 1407 if (mask) { 1408 assert(vdev->use_guest_notifier_mask); 1409 file.fd = event_notifier_get_fd(&hdev->vqs[index].masked_notifier); 1410 } else { 1411 file.fd = event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq)); 1412 } 1413 1414 file.index = hdev->vhost_ops->vhost_get_vq_index(hdev, n); 1415 r = hdev->vhost_ops->vhost_set_vring_call(hdev, &file); 1416 if (r < 0) { 1417 VHOST_OPS_DEBUG("vhost_set_vring_call failed"); 1418 } 1419 } 1420 1421 uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits, 1422 uint64_t features) 1423 { 1424 const int *bit = feature_bits; 1425 while (*bit != VHOST_INVALID_FEATURE_BIT) { 1426 uint64_t bit_mask = (1ULL << *bit); 1427 if (!(hdev->features & bit_mask)) { 1428 features &= ~bit_mask; 1429 } 1430 bit++; 1431 } 1432 return features; 1433 } 1434 1435 void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits, 1436 uint64_t features) 1437 { 1438 const int *bit = feature_bits; 1439 while (*bit != VHOST_INVALID_FEATURE_BIT) { 1440 uint64_t bit_mask = (1ULL << *bit); 1441 if (features & bit_mask) { 1442 hdev->acked_features |= bit_mask; 1443 } 1444 bit++; 1445 } 1446 } 1447 1448 int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config, 1449 uint32_t config_len) 1450 { 1451 assert(hdev->vhost_ops); 1452 1453 if (hdev->vhost_ops->vhost_get_config) { 1454 return hdev->vhost_ops->vhost_get_config(hdev, config, config_len); 1455 } 1456 1457 return -1; 1458 } 1459 1460 int vhost_dev_set_config(struct vhost_dev *hdev, const uint8_t *data, 1461 uint32_t offset, uint32_t size, uint32_t flags) 1462 { 1463 assert(hdev->vhost_ops); 1464 1465 if (hdev->vhost_ops->vhost_set_config) { 1466 return hdev->vhost_ops->vhost_set_config(hdev, data, offset, 1467 size, flags); 1468 } 1469 1470 return -1; 1471 } 1472 1473 void vhost_dev_set_config_notifier(struct vhost_dev *hdev, 1474 const VhostDevConfigOps *ops) 1475 { 1476 hdev->config_ops = ops; 1477 } 1478 1479 /* Host notifiers must be enabled at this point. */ 1480 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) 1481 { 1482 int i, r; 1483 1484 /* should only be called after backend is connected */ 1485 assert(hdev->vhost_ops); 1486 1487 hdev->started = true; 1488 hdev->vdev = vdev; 1489 1490 r = vhost_dev_set_features(hdev, hdev->log_enabled); 1491 if (r < 0) { 1492 goto fail_features; 1493 } 1494 1495 if (vhost_dev_has_iommu(hdev)) { 1496 memory_listener_register(&hdev->iommu_listener, vdev->dma_as); 1497 } 1498 1499 r = hdev->vhost_ops->vhost_set_mem_table(hdev, hdev->mem); 1500 if (r < 0) { 1501 VHOST_OPS_DEBUG("vhost_set_mem_table failed"); 1502 r = -errno; 1503 goto fail_mem; 1504 } 1505 for (i = 0; i < hdev->nvqs; ++i) { 1506 r = vhost_virtqueue_start(hdev, 1507 vdev, 1508 hdev->vqs + i, 1509 hdev->vq_index + i); 1510 if (r < 0) { 1511 goto fail_vq; 1512 } 1513 } 1514 1515 if (hdev->log_enabled) { 1516 uint64_t log_base; 1517 1518 hdev->log_size = vhost_get_log_size(hdev); 1519 hdev->log = vhost_log_get(hdev->log_size, 1520 vhost_dev_log_is_shared(hdev)); 1521 log_base = (uintptr_t)hdev->log->log; 1522 r = hdev->vhost_ops->vhost_set_log_base(hdev, 1523 hdev->log_size ? log_base : 0, 1524 hdev->log); 1525 if (r < 0) { 1526 VHOST_OPS_DEBUG("vhost_set_log_base failed"); 1527 r = -errno; 1528 goto fail_log; 1529 } 1530 } 1531 1532 if (vhost_dev_has_iommu(hdev)) { 1533 hdev->vhost_ops->vhost_set_iotlb_callback(hdev, true); 1534 1535 /* Update used ring information for IOTLB to work correctly, 1536 * vhost-kernel code requires for this.*/ 1537 for (i = 0; i < hdev->nvqs; ++i) { 1538 struct vhost_virtqueue *vq = hdev->vqs + i; 1539 vhost_device_iotlb_miss(hdev, vq->used_phys, true); 1540 } 1541 } 1542 return 0; 1543 fail_log: 1544 vhost_log_put(hdev, false); 1545 fail_vq: 1546 while (--i >= 0) { 1547 vhost_virtqueue_stop(hdev, 1548 vdev, 1549 hdev->vqs + i, 1550 hdev->vq_index + i); 1551 } 1552 i = hdev->nvqs; 1553 1554 fail_mem: 1555 fail_features: 1556 1557 hdev->started = false; 1558 return r; 1559 } 1560 1561 /* Host notifiers must be enabled at this point. */ 1562 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev) 1563 { 1564 int i; 1565 1566 /* should only be called after backend is connected */ 1567 assert(hdev->vhost_ops); 1568 1569 for (i = 0; i < hdev->nvqs; ++i) { 1570 vhost_virtqueue_stop(hdev, 1571 vdev, 1572 hdev->vqs + i, 1573 hdev->vq_index + i); 1574 } 1575 1576 if (vhost_dev_has_iommu(hdev)) { 1577 hdev->vhost_ops->vhost_set_iotlb_callback(hdev, false); 1578 memory_listener_unregister(&hdev->iommu_listener); 1579 } 1580 vhost_log_put(hdev, true); 1581 hdev->started = false; 1582 hdev->vdev = NULL; 1583 } 1584 1585 int vhost_net_set_backend(struct vhost_dev *hdev, 1586 struct vhost_vring_file *file) 1587 { 1588 if (hdev->vhost_ops->vhost_net_set_backend) { 1589 return hdev->vhost_ops->vhost_net_set_backend(hdev, file); 1590 } 1591 1592 return -1; 1593 } 1594