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(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 trace_vhost_section(section->mr->name, result); 403 return result; 404 } 405 406 static void vhost_begin(MemoryListener *listener) 407 { 408 struct vhost_dev *dev = container_of(listener, struct vhost_dev, 409 memory_listener); 410 dev->tmp_sections = NULL; 411 dev->n_tmp_sections = 0; 412 } 413 414 static void vhost_commit(MemoryListener *listener) 415 { 416 struct vhost_dev *dev = container_of(listener, struct vhost_dev, 417 memory_listener); 418 MemoryRegionSection *old_sections; 419 int n_old_sections; 420 uint64_t log_size; 421 size_t regions_size; 422 int r; 423 int i; 424 bool changed = false; 425 426 /* Note we can be called before the device is started, but then 427 * starting the device calls set_mem_table, so we need to have 428 * built the data structures. 429 */ 430 old_sections = dev->mem_sections; 431 n_old_sections = dev->n_mem_sections; 432 dev->mem_sections = dev->tmp_sections; 433 dev->n_mem_sections = dev->n_tmp_sections; 434 435 if (dev->n_mem_sections != n_old_sections) { 436 changed = true; 437 } else { 438 /* Same size, lets check the contents */ 439 changed = n_old_sections && memcmp(dev->mem_sections, old_sections, 440 n_old_sections * sizeof(old_sections[0])) != 0; 441 } 442 443 trace_vhost_commit(dev->started, changed); 444 if (!changed) { 445 goto out; 446 } 447 448 /* Rebuild the regions list from the new sections list */ 449 regions_size = offsetof(struct vhost_memory, regions) + 450 dev->n_mem_sections * sizeof dev->mem->regions[0]; 451 dev->mem = g_realloc(dev->mem, regions_size); 452 dev->mem->nregions = dev->n_mem_sections; 453 used_memslots = dev->mem->nregions; 454 for (i = 0; i < dev->n_mem_sections; i++) { 455 struct vhost_memory_region *cur_vmr = dev->mem->regions + i; 456 struct MemoryRegionSection *mrs = dev->mem_sections + i; 457 458 cur_vmr->guest_phys_addr = mrs->offset_within_address_space; 459 cur_vmr->memory_size = int128_get64(mrs->size); 460 cur_vmr->userspace_addr = 461 (uintptr_t)memory_region_get_ram_ptr(mrs->mr) + 462 mrs->offset_within_region; 463 cur_vmr->flags_padding = 0; 464 } 465 466 if (!dev->started) { 467 goto out; 468 } 469 470 for (i = 0; i < dev->mem->nregions; i++) { 471 if (vhost_verify_ring_mappings(dev, 472 (void *)(uintptr_t)dev->mem->regions[i].userspace_addr, 473 dev->mem->regions[i].guest_phys_addr, 474 dev->mem->regions[i].memory_size)) { 475 error_report("Verify ring failure on region %d", i); 476 abort(); 477 } 478 } 479 480 if (!dev->log_enabled) { 481 r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem); 482 if (r < 0) { 483 VHOST_OPS_DEBUG("vhost_set_mem_table failed"); 484 } 485 goto out; 486 } 487 log_size = vhost_get_log_size(dev); 488 /* We allocate an extra 4K bytes to log, 489 * to reduce the * number of reallocations. */ 490 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log) 491 /* To log more, must increase log size before table update. */ 492 if (dev->log_size < log_size) { 493 vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER); 494 } 495 r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem); 496 if (r < 0) { 497 VHOST_OPS_DEBUG("vhost_set_mem_table failed"); 498 } 499 /* To log less, can only decrease log size after table update. */ 500 if (dev->log_size > log_size + VHOST_LOG_BUFFER) { 501 vhost_dev_log_resize(dev, log_size); 502 } 503 504 out: 505 /* Deref the old list of sections, this must happen _after_ the 506 * vhost_set_mem_table to ensure the client isn't still using the 507 * section we're about to unref. 508 */ 509 while (n_old_sections--) { 510 memory_region_unref(old_sections[n_old_sections].mr); 511 } 512 g_free(old_sections); 513 return; 514 } 515 516 /* Adds the section data to the tmp_section structure. 517 * It relies on the listener calling us in memory address order 518 * and for each region (via the _add and _nop methods) to 519 * join neighbours. 520 */ 521 static void vhost_region_add_section(struct vhost_dev *dev, 522 MemoryRegionSection *section) 523 { 524 bool need_add = true; 525 uint64_t mrs_size = int128_get64(section->size); 526 uint64_t mrs_gpa = section->offset_within_address_space; 527 uintptr_t mrs_host = (uintptr_t)memory_region_get_ram_ptr(section->mr) + 528 section->offset_within_region; 529 RAMBlock *mrs_rb = section->mr->ram_block; 530 size_t mrs_page = qemu_ram_pagesize(mrs_rb); 531 532 trace_vhost_region_add_section(section->mr->name, mrs_gpa, mrs_size, 533 mrs_host); 534 535 /* Round the section to it's page size */ 536 /* First align the start down to a page boundary */ 537 uint64_t alignage = mrs_host & (mrs_page - 1); 538 if (alignage) { 539 mrs_host -= alignage; 540 mrs_size += alignage; 541 mrs_gpa -= alignage; 542 } 543 /* Now align the size up to a page boundary */ 544 alignage = mrs_size & (mrs_page - 1); 545 if (alignage) { 546 mrs_size += mrs_page - alignage; 547 } 548 trace_vhost_region_add_section_aligned(section->mr->name, mrs_gpa, mrs_size, 549 mrs_host); 550 551 if (dev->n_tmp_sections) { 552 /* Since we already have at least one section, lets see if 553 * this extends it; since we're scanning in order, we only 554 * have to look at the last one, and the FlatView that calls 555 * us shouldn't have overlaps. 556 */ 557 MemoryRegionSection *prev_sec = dev->tmp_sections + 558 (dev->n_tmp_sections - 1); 559 uint64_t prev_gpa_start = prev_sec->offset_within_address_space; 560 uint64_t prev_size = int128_get64(prev_sec->size); 561 uint64_t prev_gpa_end = range_get_last(prev_gpa_start, prev_size); 562 uint64_t prev_host_start = 563 (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr) + 564 prev_sec->offset_within_region; 565 uint64_t prev_host_end = range_get_last(prev_host_start, prev_size); 566 567 if (mrs_gpa <= (prev_gpa_end + 1)) { 568 /* OK, looks like overlapping/intersecting - it's possible that 569 * the rounding to page sizes has made them overlap, but they should 570 * match up in the same RAMBlock if they do. 571 */ 572 if (mrs_gpa < prev_gpa_start) { 573 error_report("%s:Section rounded to %"PRIx64 574 " prior to previous %"PRIx64, 575 __func__, mrs_gpa, prev_gpa_start); 576 /* A way to cleanly fail here would be better */ 577 return; 578 } 579 /* Offset from the start of the previous GPA to this GPA */ 580 size_t offset = mrs_gpa - prev_gpa_start; 581 582 if (prev_host_start + offset == mrs_host && 583 section->mr == prev_sec->mr && 584 (!dev->vhost_ops->vhost_backend_can_merge || 585 dev->vhost_ops->vhost_backend_can_merge(dev, 586 mrs_host, mrs_size, 587 prev_host_start, prev_size))) { 588 uint64_t max_end = MAX(prev_host_end, mrs_host + mrs_size); 589 need_add = false; 590 prev_sec->offset_within_address_space = 591 MIN(prev_gpa_start, mrs_gpa); 592 prev_sec->offset_within_region = 593 MIN(prev_host_start, mrs_host) - 594 (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr); 595 prev_sec->size = int128_make64(max_end - MIN(prev_host_start, 596 mrs_host)); 597 trace_vhost_region_add_section_merge(section->mr->name, 598 int128_get64(prev_sec->size), 599 prev_sec->offset_within_address_space, 600 prev_sec->offset_within_region); 601 } else { 602 /* adjoining regions are fine, but overlapping ones with 603 * different blocks/offsets shouldn't happen 604 */ 605 if (mrs_gpa != prev_gpa_end + 1) { 606 error_report("%s: Overlapping but not coherent sections " 607 "at %"PRIx64, 608 __func__, mrs_gpa); 609 return; 610 } 611 } 612 } 613 } 614 615 if (need_add) { 616 ++dev->n_tmp_sections; 617 dev->tmp_sections = g_renew(MemoryRegionSection, dev->tmp_sections, 618 dev->n_tmp_sections); 619 dev->tmp_sections[dev->n_tmp_sections - 1] = *section; 620 /* The flatview isn't stable and we don't use it, making it NULL 621 * means we can memcmp the list. 622 */ 623 dev->tmp_sections[dev->n_tmp_sections - 1].fv = NULL; 624 memory_region_ref(section->mr); 625 } 626 } 627 628 /* Used for both add and nop callbacks */ 629 static void vhost_region_addnop(MemoryListener *listener, 630 MemoryRegionSection *section) 631 { 632 struct vhost_dev *dev = container_of(listener, struct vhost_dev, 633 memory_listener); 634 635 if (!vhost_section(section)) { 636 return; 637 } 638 vhost_region_add_section(dev, section); 639 } 640 641 static void vhost_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) 642 { 643 struct vhost_iommu *iommu = container_of(n, struct vhost_iommu, n); 644 struct vhost_dev *hdev = iommu->hdev; 645 hwaddr iova = iotlb->iova + iommu->iommu_offset; 646 647 if (vhost_backend_invalidate_device_iotlb(hdev, iova, 648 iotlb->addr_mask + 1)) { 649 error_report("Fail to invalidate device iotlb"); 650 } 651 } 652 653 static void vhost_iommu_region_add(MemoryListener *listener, 654 MemoryRegionSection *section) 655 { 656 struct vhost_dev *dev = container_of(listener, struct vhost_dev, 657 iommu_listener); 658 struct vhost_iommu *iommu; 659 Int128 end; 660 661 if (!memory_region_is_iommu(section->mr)) { 662 return; 663 } 664 665 iommu = g_malloc0(sizeof(*iommu)); 666 end = int128_add(int128_make64(section->offset_within_region), 667 section->size); 668 end = int128_sub(end, int128_one()); 669 iommu_notifier_init(&iommu->n, vhost_iommu_unmap_notify, 670 IOMMU_NOTIFIER_UNMAP, 671 section->offset_within_region, 672 int128_get64(end)); 673 iommu->mr = section->mr; 674 iommu->iommu_offset = section->offset_within_address_space - 675 section->offset_within_region; 676 iommu->hdev = dev; 677 memory_region_register_iommu_notifier(section->mr, &iommu->n); 678 QLIST_INSERT_HEAD(&dev->iommu_list, iommu, iommu_next); 679 /* TODO: can replay help performance here? */ 680 } 681 682 static void vhost_iommu_region_del(MemoryListener *listener, 683 MemoryRegionSection *section) 684 { 685 struct vhost_dev *dev = container_of(listener, struct vhost_dev, 686 iommu_listener); 687 struct vhost_iommu *iommu; 688 689 if (!memory_region_is_iommu(section->mr)) { 690 return; 691 } 692 693 QLIST_FOREACH(iommu, &dev->iommu_list, iommu_next) { 694 if (iommu->mr == section->mr && 695 iommu->n.start == section->offset_within_region) { 696 memory_region_unregister_iommu_notifier(iommu->mr, 697 &iommu->n); 698 QLIST_REMOVE(iommu, iommu_next); 699 g_free(iommu); 700 break; 701 } 702 } 703 } 704 705 static int vhost_virtqueue_set_addr(struct vhost_dev *dev, 706 struct vhost_virtqueue *vq, 707 unsigned idx, bool enable_log) 708 { 709 struct vhost_vring_addr addr = { 710 .index = idx, 711 .desc_user_addr = (uint64_t)(unsigned long)vq->desc, 712 .avail_user_addr = (uint64_t)(unsigned long)vq->avail, 713 .used_user_addr = (uint64_t)(unsigned long)vq->used, 714 .log_guest_addr = vq->used_phys, 715 .flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0, 716 }; 717 int r = dev->vhost_ops->vhost_set_vring_addr(dev, &addr); 718 if (r < 0) { 719 VHOST_OPS_DEBUG("vhost_set_vring_addr failed"); 720 return -errno; 721 } 722 return 0; 723 } 724 725 static int vhost_dev_set_features(struct vhost_dev *dev, 726 bool enable_log) 727 { 728 uint64_t features = dev->acked_features; 729 int r; 730 if (enable_log) { 731 features |= 0x1ULL << VHOST_F_LOG_ALL; 732 } 733 r = dev->vhost_ops->vhost_set_features(dev, features); 734 if (r < 0) { 735 VHOST_OPS_DEBUG("vhost_set_features failed"); 736 } 737 return r < 0 ? -errno : 0; 738 } 739 740 static int vhost_dev_set_log(struct vhost_dev *dev, bool enable_log) 741 { 742 int r, i, idx; 743 r = vhost_dev_set_features(dev, enable_log); 744 if (r < 0) { 745 goto err_features; 746 } 747 for (i = 0; i < dev->nvqs; ++i) { 748 idx = dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i); 749 r = vhost_virtqueue_set_addr(dev, dev->vqs + i, idx, 750 enable_log); 751 if (r < 0) { 752 goto err_vq; 753 } 754 } 755 return 0; 756 err_vq: 757 for (; i >= 0; --i) { 758 idx = dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i); 759 vhost_virtqueue_set_addr(dev, dev->vqs + i, idx, 760 dev->log_enabled); 761 } 762 vhost_dev_set_features(dev, dev->log_enabled); 763 err_features: 764 return r; 765 } 766 767 static int vhost_migration_log(MemoryListener *listener, int enable) 768 { 769 struct vhost_dev *dev = container_of(listener, struct vhost_dev, 770 memory_listener); 771 int r; 772 if (!!enable == dev->log_enabled) { 773 return 0; 774 } 775 if (!dev->started) { 776 dev->log_enabled = enable; 777 return 0; 778 } 779 if (!enable) { 780 r = vhost_dev_set_log(dev, false); 781 if (r < 0) { 782 return r; 783 } 784 vhost_log_put(dev, false); 785 } else { 786 vhost_dev_log_resize(dev, vhost_get_log_size(dev)); 787 r = vhost_dev_set_log(dev, true); 788 if (r < 0) { 789 return r; 790 } 791 } 792 dev->log_enabled = enable; 793 return 0; 794 } 795 796 static void vhost_log_global_start(MemoryListener *listener) 797 { 798 int r; 799 800 r = vhost_migration_log(listener, true); 801 if (r < 0) { 802 abort(); 803 } 804 } 805 806 static void vhost_log_global_stop(MemoryListener *listener) 807 { 808 int r; 809 810 r = vhost_migration_log(listener, false); 811 if (r < 0) { 812 abort(); 813 } 814 } 815 816 static void vhost_log_start(MemoryListener *listener, 817 MemoryRegionSection *section, 818 int old, int new) 819 { 820 /* FIXME: implement */ 821 } 822 823 static void vhost_log_stop(MemoryListener *listener, 824 MemoryRegionSection *section, 825 int old, int new) 826 { 827 /* FIXME: implement */ 828 } 829 830 /* The vhost driver natively knows how to handle the vrings of non 831 * cross-endian legacy devices and modern devices. Only legacy devices 832 * exposed to a bi-endian guest may require the vhost driver to use a 833 * specific endianness. 834 */ 835 static inline bool vhost_needs_vring_endian(VirtIODevice *vdev) 836 { 837 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { 838 return false; 839 } 840 #ifdef HOST_WORDS_BIGENDIAN 841 return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_LITTLE; 842 #else 843 return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG; 844 #endif 845 } 846 847 static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev *dev, 848 bool is_big_endian, 849 int vhost_vq_index) 850 { 851 struct vhost_vring_state s = { 852 .index = vhost_vq_index, 853 .num = is_big_endian 854 }; 855 856 if (!dev->vhost_ops->vhost_set_vring_endian(dev, &s)) { 857 return 0; 858 } 859 860 VHOST_OPS_DEBUG("vhost_set_vring_endian failed"); 861 if (errno == ENOTTY) { 862 error_report("vhost does not support cross-endian"); 863 return -ENOSYS; 864 } 865 866 return -errno; 867 } 868 869 static int vhost_memory_region_lookup(struct vhost_dev *hdev, 870 uint64_t gpa, uint64_t *uaddr, 871 uint64_t *len) 872 { 873 int i; 874 875 for (i = 0; i < hdev->mem->nregions; i++) { 876 struct vhost_memory_region *reg = hdev->mem->regions + i; 877 878 if (gpa >= reg->guest_phys_addr && 879 reg->guest_phys_addr + reg->memory_size > gpa) { 880 *uaddr = reg->userspace_addr + gpa - reg->guest_phys_addr; 881 *len = reg->guest_phys_addr + reg->memory_size - gpa; 882 return 0; 883 } 884 } 885 886 return -EFAULT; 887 } 888 889 int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write) 890 { 891 IOMMUTLBEntry iotlb; 892 uint64_t uaddr, len; 893 int ret = -EFAULT; 894 895 rcu_read_lock(); 896 897 trace_vhost_iotlb_miss(dev, 1); 898 899 iotlb = address_space_get_iotlb_entry(dev->vdev->dma_as, 900 iova, write, 901 MEMTXATTRS_UNSPECIFIED); 902 if (iotlb.target_as != NULL) { 903 ret = vhost_memory_region_lookup(dev, iotlb.translated_addr, 904 &uaddr, &len); 905 if (ret) { 906 trace_vhost_iotlb_miss(dev, 3); 907 error_report("Fail to lookup the translated address " 908 "%"PRIx64, iotlb.translated_addr); 909 goto out; 910 } 911 912 len = MIN(iotlb.addr_mask + 1, len); 913 iova = iova & ~iotlb.addr_mask; 914 915 ret = vhost_backend_update_device_iotlb(dev, iova, uaddr, 916 len, iotlb.perm); 917 if (ret) { 918 trace_vhost_iotlb_miss(dev, 4); 919 error_report("Fail to update device iotlb"); 920 goto out; 921 } 922 } 923 924 trace_vhost_iotlb_miss(dev, 2); 925 926 out: 927 rcu_read_unlock(); 928 929 return ret; 930 } 931 932 static int vhost_virtqueue_start(struct vhost_dev *dev, 933 struct VirtIODevice *vdev, 934 struct vhost_virtqueue *vq, 935 unsigned idx) 936 { 937 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 938 VirtioBusState *vbus = VIRTIO_BUS(qbus); 939 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); 940 hwaddr s, l, a; 941 int r; 942 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx); 943 struct vhost_vring_file file = { 944 .index = vhost_vq_index 945 }; 946 struct vhost_vring_state state = { 947 .index = vhost_vq_index 948 }; 949 struct VirtQueue *vvq = virtio_get_queue(vdev, idx); 950 951 a = virtio_queue_get_desc_addr(vdev, idx); 952 if (a == 0) { 953 /* Queue might not be ready for start */ 954 return 0; 955 } 956 957 vq->num = state.num = virtio_queue_get_num(vdev, idx); 958 r = dev->vhost_ops->vhost_set_vring_num(dev, &state); 959 if (r) { 960 VHOST_OPS_DEBUG("vhost_set_vring_num failed"); 961 return -errno; 962 } 963 964 state.num = virtio_queue_get_last_avail_idx(vdev, idx); 965 r = dev->vhost_ops->vhost_set_vring_base(dev, &state); 966 if (r) { 967 VHOST_OPS_DEBUG("vhost_set_vring_base failed"); 968 return -errno; 969 } 970 971 if (vhost_needs_vring_endian(vdev)) { 972 r = vhost_virtqueue_set_vring_endian_legacy(dev, 973 virtio_is_big_endian(vdev), 974 vhost_vq_index); 975 if (r) { 976 return -errno; 977 } 978 } 979 980 vq->desc_size = s = l = virtio_queue_get_desc_size(vdev, idx); 981 vq->desc_phys = a; 982 vq->desc = vhost_memory_map(dev, a, &l, 0); 983 if (!vq->desc || l != s) { 984 r = -ENOMEM; 985 goto fail_alloc_desc; 986 } 987 vq->avail_size = s = l = virtio_queue_get_avail_size(vdev, idx); 988 vq->avail_phys = a = virtio_queue_get_avail_addr(vdev, idx); 989 vq->avail = vhost_memory_map(dev, a, &l, 0); 990 if (!vq->avail || l != s) { 991 r = -ENOMEM; 992 goto fail_alloc_avail; 993 } 994 vq->used_size = s = l = virtio_queue_get_used_size(vdev, idx); 995 vq->used_phys = a = virtio_queue_get_used_addr(vdev, idx); 996 vq->used = vhost_memory_map(dev, a, &l, 1); 997 if (!vq->used || l != s) { 998 r = -ENOMEM; 999 goto fail_alloc_used; 1000 } 1001 1002 r = vhost_virtqueue_set_addr(dev, vq, vhost_vq_index, dev->log_enabled); 1003 if (r < 0) { 1004 r = -errno; 1005 goto fail_alloc; 1006 } 1007 1008 file.fd = event_notifier_get_fd(virtio_queue_get_host_notifier(vvq)); 1009 r = dev->vhost_ops->vhost_set_vring_kick(dev, &file); 1010 if (r) { 1011 VHOST_OPS_DEBUG("vhost_set_vring_kick failed"); 1012 r = -errno; 1013 goto fail_kick; 1014 } 1015 1016 /* Clear and discard previous events if any. */ 1017 event_notifier_test_and_clear(&vq->masked_notifier); 1018 1019 /* Init vring in unmasked state, unless guest_notifier_mask 1020 * will do it later. 1021 */ 1022 if (!vdev->use_guest_notifier_mask) { 1023 /* TODO: check and handle errors. */ 1024 vhost_virtqueue_mask(dev, vdev, idx, false); 1025 } 1026 1027 if (k->query_guest_notifiers && 1028 k->query_guest_notifiers(qbus->parent) && 1029 virtio_queue_vector(vdev, idx) == VIRTIO_NO_VECTOR) { 1030 file.fd = -1; 1031 r = dev->vhost_ops->vhost_set_vring_call(dev, &file); 1032 if (r) { 1033 goto fail_vector; 1034 } 1035 } 1036 1037 return 0; 1038 1039 fail_vector: 1040 fail_kick: 1041 fail_alloc: 1042 vhost_memory_unmap(dev, vq->used, virtio_queue_get_used_size(vdev, idx), 1043 0, 0); 1044 fail_alloc_used: 1045 vhost_memory_unmap(dev, vq->avail, virtio_queue_get_avail_size(vdev, idx), 1046 0, 0); 1047 fail_alloc_avail: 1048 vhost_memory_unmap(dev, vq->desc, virtio_queue_get_desc_size(vdev, idx), 1049 0, 0); 1050 fail_alloc_desc: 1051 return r; 1052 } 1053 1054 static void vhost_virtqueue_stop(struct vhost_dev *dev, 1055 struct VirtIODevice *vdev, 1056 struct vhost_virtqueue *vq, 1057 unsigned idx) 1058 { 1059 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx); 1060 struct vhost_vring_state state = { 1061 .index = vhost_vq_index, 1062 }; 1063 int r; 1064 int a; 1065 1066 a = virtio_queue_get_desc_addr(vdev, idx); 1067 if (a == 0) { 1068 /* Don't stop the virtqueue which might have not been started */ 1069 return; 1070 } 1071 1072 r = dev->vhost_ops->vhost_get_vring_base(dev, &state); 1073 if (r < 0) { 1074 VHOST_OPS_DEBUG("vhost VQ %d ring restore failed: %d", idx, r); 1075 /* Connection to the backend is broken, so let's sync internal 1076 * last avail idx to the device used idx. 1077 */ 1078 virtio_queue_restore_last_avail_idx(vdev, idx); 1079 } else { 1080 virtio_queue_set_last_avail_idx(vdev, idx, state.num); 1081 } 1082 virtio_queue_invalidate_signalled_used(vdev, idx); 1083 virtio_queue_update_used_idx(vdev, idx); 1084 1085 /* In the cross-endian case, we need to reset the vring endianness to 1086 * native as legacy devices expect so by default. 1087 */ 1088 if (vhost_needs_vring_endian(vdev)) { 1089 vhost_virtqueue_set_vring_endian_legacy(dev, 1090 !virtio_is_big_endian(vdev), 1091 vhost_vq_index); 1092 } 1093 1094 vhost_memory_unmap(dev, vq->used, virtio_queue_get_used_size(vdev, idx), 1095 1, virtio_queue_get_used_size(vdev, idx)); 1096 vhost_memory_unmap(dev, vq->avail, virtio_queue_get_avail_size(vdev, idx), 1097 0, virtio_queue_get_avail_size(vdev, idx)); 1098 vhost_memory_unmap(dev, vq->desc, virtio_queue_get_desc_size(vdev, idx), 1099 0, virtio_queue_get_desc_size(vdev, idx)); 1100 } 1101 1102 static void vhost_eventfd_add(MemoryListener *listener, 1103 MemoryRegionSection *section, 1104 bool match_data, uint64_t data, EventNotifier *e) 1105 { 1106 } 1107 1108 static void vhost_eventfd_del(MemoryListener *listener, 1109 MemoryRegionSection *section, 1110 bool match_data, uint64_t data, EventNotifier *e) 1111 { 1112 } 1113 1114 static int vhost_virtqueue_set_busyloop_timeout(struct vhost_dev *dev, 1115 int n, uint32_t timeout) 1116 { 1117 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n); 1118 struct vhost_vring_state state = { 1119 .index = vhost_vq_index, 1120 .num = timeout, 1121 }; 1122 int r; 1123 1124 if (!dev->vhost_ops->vhost_set_vring_busyloop_timeout) { 1125 return -EINVAL; 1126 } 1127 1128 r = dev->vhost_ops->vhost_set_vring_busyloop_timeout(dev, &state); 1129 if (r) { 1130 VHOST_OPS_DEBUG("vhost_set_vring_busyloop_timeout failed"); 1131 return r; 1132 } 1133 1134 return 0; 1135 } 1136 1137 static int vhost_virtqueue_init(struct vhost_dev *dev, 1138 struct vhost_virtqueue *vq, int n) 1139 { 1140 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n); 1141 struct vhost_vring_file file = { 1142 .index = vhost_vq_index, 1143 }; 1144 int r = event_notifier_init(&vq->masked_notifier, 0); 1145 if (r < 0) { 1146 return r; 1147 } 1148 1149 file.fd = event_notifier_get_fd(&vq->masked_notifier); 1150 r = dev->vhost_ops->vhost_set_vring_call(dev, &file); 1151 if (r) { 1152 VHOST_OPS_DEBUG("vhost_set_vring_call failed"); 1153 r = -errno; 1154 goto fail_call; 1155 } 1156 1157 vq->dev = dev; 1158 1159 return 0; 1160 fail_call: 1161 event_notifier_cleanup(&vq->masked_notifier); 1162 return r; 1163 } 1164 1165 static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq) 1166 { 1167 event_notifier_cleanup(&vq->masked_notifier); 1168 } 1169 1170 int vhost_dev_init(struct vhost_dev *hdev, void *opaque, 1171 VhostBackendType backend_type, uint32_t busyloop_timeout) 1172 { 1173 uint64_t features; 1174 int i, r, n_initialized_vqs = 0; 1175 Error *local_err = NULL; 1176 1177 hdev->vdev = NULL; 1178 hdev->migration_blocker = NULL; 1179 1180 r = vhost_set_backend_type(hdev, backend_type); 1181 assert(r >= 0); 1182 1183 r = hdev->vhost_ops->vhost_backend_init(hdev, opaque); 1184 if (r < 0) { 1185 goto fail; 1186 } 1187 1188 r = hdev->vhost_ops->vhost_set_owner(hdev); 1189 if (r < 0) { 1190 VHOST_OPS_DEBUG("vhost_set_owner failed"); 1191 goto fail; 1192 } 1193 1194 r = hdev->vhost_ops->vhost_get_features(hdev, &features); 1195 if (r < 0) { 1196 VHOST_OPS_DEBUG("vhost_get_features failed"); 1197 goto fail; 1198 } 1199 1200 for (i = 0; i < hdev->nvqs; ++i, ++n_initialized_vqs) { 1201 r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i); 1202 if (r < 0) { 1203 goto fail; 1204 } 1205 } 1206 1207 if (busyloop_timeout) { 1208 for (i = 0; i < hdev->nvqs; ++i) { 1209 r = vhost_virtqueue_set_busyloop_timeout(hdev, hdev->vq_index + i, 1210 busyloop_timeout); 1211 if (r < 0) { 1212 goto fail_busyloop; 1213 } 1214 } 1215 } 1216 1217 hdev->features = features; 1218 1219 hdev->memory_listener = (MemoryListener) { 1220 .begin = vhost_begin, 1221 .commit = vhost_commit, 1222 .region_add = vhost_region_addnop, 1223 .region_nop = vhost_region_addnop, 1224 .log_start = vhost_log_start, 1225 .log_stop = vhost_log_stop, 1226 .log_sync = vhost_log_sync, 1227 .log_global_start = vhost_log_global_start, 1228 .log_global_stop = vhost_log_global_stop, 1229 .eventfd_add = vhost_eventfd_add, 1230 .eventfd_del = vhost_eventfd_del, 1231 .priority = 10 1232 }; 1233 1234 hdev->iommu_listener = (MemoryListener) { 1235 .region_add = vhost_iommu_region_add, 1236 .region_del = vhost_iommu_region_del, 1237 }; 1238 1239 if (hdev->migration_blocker == NULL) { 1240 if (!(hdev->features & (0x1ULL << VHOST_F_LOG_ALL))) { 1241 error_setg(&hdev->migration_blocker, 1242 "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature."); 1243 } else if (vhost_dev_log_is_shared(hdev) && !qemu_memfd_alloc_check()) { 1244 error_setg(&hdev->migration_blocker, 1245 "Migration disabled: failed to allocate shared memory"); 1246 } 1247 } 1248 1249 if (hdev->migration_blocker != NULL) { 1250 r = migrate_add_blocker(hdev->migration_blocker, &local_err); 1251 if (local_err) { 1252 error_report_err(local_err); 1253 error_free(hdev->migration_blocker); 1254 goto fail_busyloop; 1255 } 1256 } 1257 1258 hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions)); 1259 hdev->n_mem_sections = 0; 1260 hdev->mem_sections = NULL; 1261 hdev->log = NULL; 1262 hdev->log_size = 0; 1263 hdev->log_enabled = false; 1264 hdev->started = false; 1265 memory_listener_register(&hdev->memory_listener, &address_space_memory); 1266 QLIST_INSERT_HEAD(&vhost_devices, hdev, entry); 1267 1268 if (used_memslots > hdev->vhost_ops->vhost_backend_memslots_limit(hdev)) { 1269 error_report("vhost backend memory slots limit is less" 1270 " than current number of present memory slots"); 1271 r = -1; 1272 if (busyloop_timeout) { 1273 goto fail_busyloop; 1274 } else { 1275 goto fail; 1276 } 1277 } 1278 1279 return 0; 1280 1281 fail_busyloop: 1282 while (--i >= 0) { 1283 vhost_virtqueue_set_busyloop_timeout(hdev, hdev->vq_index + i, 0); 1284 } 1285 fail: 1286 hdev->nvqs = n_initialized_vqs; 1287 vhost_dev_cleanup(hdev); 1288 return r; 1289 } 1290 1291 void vhost_dev_cleanup(struct vhost_dev *hdev) 1292 { 1293 int i; 1294 1295 for (i = 0; i < hdev->nvqs; ++i) { 1296 vhost_virtqueue_cleanup(hdev->vqs + i); 1297 } 1298 if (hdev->mem) { 1299 /* those are only safe after successful init */ 1300 memory_listener_unregister(&hdev->memory_listener); 1301 QLIST_REMOVE(hdev, entry); 1302 } 1303 if (hdev->migration_blocker) { 1304 migrate_del_blocker(hdev->migration_blocker); 1305 error_free(hdev->migration_blocker); 1306 } 1307 g_free(hdev->mem); 1308 g_free(hdev->mem_sections); 1309 if (hdev->vhost_ops) { 1310 hdev->vhost_ops->vhost_backend_cleanup(hdev); 1311 } 1312 assert(!hdev->log); 1313 1314 memset(hdev, 0, sizeof(struct vhost_dev)); 1315 } 1316 1317 /* Stop processing guest IO notifications in qemu. 1318 * Start processing them in vhost in kernel. 1319 */ 1320 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev) 1321 { 1322 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 1323 int i, r, e; 1324 1325 /* We will pass the notifiers to the kernel, make sure that QEMU 1326 * doesn't interfere. 1327 */ 1328 r = virtio_device_grab_ioeventfd(vdev); 1329 if (r < 0) { 1330 error_report("binding does not support host notifiers"); 1331 goto fail; 1332 } 1333 1334 for (i = 0; i < hdev->nvqs; ++i) { 1335 r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i, 1336 true); 1337 if (r < 0) { 1338 error_report("vhost VQ %d notifier binding failed: %d", i, -r); 1339 goto fail_vq; 1340 } 1341 } 1342 1343 return 0; 1344 fail_vq: 1345 while (--i >= 0) { 1346 e = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i, 1347 false); 1348 if (e < 0) { 1349 error_report("vhost VQ %d notifier cleanup error: %d", i, -r); 1350 } 1351 assert (e >= 0); 1352 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i); 1353 } 1354 virtio_device_release_ioeventfd(vdev); 1355 fail: 1356 return r; 1357 } 1358 1359 /* Stop processing guest IO notifications in vhost. 1360 * Start processing them in qemu. 1361 * This might actually run the qemu handlers right away, 1362 * so virtio in qemu must be completely setup when this is called. 1363 */ 1364 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev) 1365 { 1366 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 1367 int i, r; 1368 1369 for (i = 0; i < hdev->nvqs; ++i) { 1370 r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i, 1371 false); 1372 if (r < 0) { 1373 error_report("vhost VQ %d notifier cleanup failed: %d", i, -r); 1374 } 1375 assert (r >= 0); 1376 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i); 1377 } 1378 virtio_device_release_ioeventfd(vdev); 1379 } 1380 1381 /* Test and clear event pending status. 1382 * Should be called after unmask to avoid losing events. 1383 */ 1384 bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n) 1385 { 1386 struct vhost_virtqueue *vq = hdev->vqs + n - hdev->vq_index; 1387 assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs); 1388 return event_notifier_test_and_clear(&vq->masked_notifier); 1389 } 1390 1391 /* Mask/unmask events from this vq. */ 1392 void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n, 1393 bool mask) 1394 { 1395 struct VirtQueue *vvq = virtio_get_queue(vdev, n); 1396 int r, index = n - hdev->vq_index; 1397 struct vhost_vring_file file; 1398 1399 /* should only be called after backend is connected */ 1400 assert(hdev->vhost_ops); 1401 1402 if (mask) { 1403 assert(vdev->use_guest_notifier_mask); 1404 file.fd = event_notifier_get_fd(&hdev->vqs[index].masked_notifier); 1405 } else { 1406 file.fd = event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq)); 1407 } 1408 1409 file.index = hdev->vhost_ops->vhost_get_vq_index(hdev, n); 1410 r = hdev->vhost_ops->vhost_set_vring_call(hdev, &file); 1411 if (r < 0) { 1412 VHOST_OPS_DEBUG("vhost_set_vring_call failed"); 1413 } 1414 } 1415 1416 uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits, 1417 uint64_t features) 1418 { 1419 const int *bit = feature_bits; 1420 while (*bit != VHOST_INVALID_FEATURE_BIT) { 1421 uint64_t bit_mask = (1ULL << *bit); 1422 if (!(hdev->features & bit_mask)) { 1423 features &= ~bit_mask; 1424 } 1425 bit++; 1426 } 1427 return features; 1428 } 1429 1430 void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits, 1431 uint64_t features) 1432 { 1433 const int *bit = feature_bits; 1434 while (*bit != VHOST_INVALID_FEATURE_BIT) { 1435 uint64_t bit_mask = (1ULL << *bit); 1436 if (features & bit_mask) { 1437 hdev->acked_features |= bit_mask; 1438 } 1439 bit++; 1440 } 1441 } 1442 1443 int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config, 1444 uint32_t config_len) 1445 { 1446 assert(hdev->vhost_ops); 1447 1448 if (hdev->vhost_ops->vhost_get_config) { 1449 return hdev->vhost_ops->vhost_get_config(hdev, config, config_len); 1450 } 1451 1452 return -1; 1453 } 1454 1455 int vhost_dev_set_config(struct vhost_dev *hdev, const uint8_t *data, 1456 uint32_t offset, uint32_t size, uint32_t flags) 1457 { 1458 assert(hdev->vhost_ops); 1459 1460 if (hdev->vhost_ops->vhost_set_config) { 1461 return hdev->vhost_ops->vhost_set_config(hdev, data, offset, 1462 size, flags); 1463 } 1464 1465 return -1; 1466 } 1467 1468 void vhost_dev_set_config_notifier(struct vhost_dev *hdev, 1469 const VhostDevConfigOps *ops) 1470 { 1471 hdev->config_ops = ops; 1472 } 1473 1474 /* Host notifiers must be enabled at this point. */ 1475 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) 1476 { 1477 int i, r; 1478 1479 /* should only be called after backend is connected */ 1480 assert(hdev->vhost_ops); 1481 1482 hdev->started = true; 1483 hdev->vdev = vdev; 1484 1485 r = vhost_dev_set_features(hdev, hdev->log_enabled); 1486 if (r < 0) { 1487 goto fail_features; 1488 } 1489 1490 if (vhost_dev_has_iommu(hdev)) { 1491 memory_listener_register(&hdev->iommu_listener, vdev->dma_as); 1492 } 1493 1494 r = hdev->vhost_ops->vhost_set_mem_table(hdev, hdev->mem); 1495 if (r < 0) { 1496 VHOST_OPS_DEBUG("vhost_set_mem_table failed"); 1497 r = -errno; 1498 goto fail_mem; 1499 } 1500 for (i = 0; i < hdev->nvqs; ++i) { 1501 r = vhost_virtqueue_start(hdev, 1502 vdev, 1503 hdev->vqs + i, 1504 hdev->vq_index + i); 1505 if (r < 0) { 1506 goto fail_vq; 1507 } 1508 } 1509 1510 if (hdev->log_enabled) { 1511 uint64_t log_base; 1512 1513 hdev->log_size = vhost_get_log_size(hdev); 1514 hdev->log = vhost_log_get(hdev->log_size, 1515 vhost_dev_log_is_shared(hdev)); 1516 log_base = (uintptr_t)hdev->log->log; 1517 r = hdev->vhost_ops->vhost_set_log_base(hdev, 1518 hdev->log_size ? log_base : 0, 1519 hdev->log); 1520 if (r < 0) { 1521 VHOST_OPS_DEBUG("vhost_set_log_base failed"); 1522 r = -errno; 1523 goto fail_log; 1524 } 1525 } 1526 1527 if (vhost_dev_has_iommu(hdev)) { 1528 hdev->vhost_ops->vhost_set_iotlb_callback(hdev, true); 1529 1530 /* Update used ring information for IOTLB to work correctly, 1531 * vhost-kernel code requires for this.*/ 1532 for (i = 0; i < hdev->nvqs; ++i) { 1533 struct vhost_virtqueue *vq = hdev->vqs + i; 1534 vhost_device_iotlb_miss(hdev, vq->used_phys, true); 1535 } 1536 } 1537 return 0; 1538 fail_log: 1539 vhost_log_put(hdev, false); 1540 fail_vq: 1541 while (--i >= 0) { 1542 vhost_virtqueue_stop(hdev, 1543 vdev, 1544 hdev->vqs + i, 1545 hdev->vq_index + i); 1546 } 1547 i = hdev->nvqs; 1548 1549 fail_mem: 1550 fail_features: 1551 1552 hdev->started = false; 1553 return r; 1554 } 1555 1556 /* Host notifiers must be enabled at this point. */ 1557 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev) 1558 { 1559 int i; 1560 1561 /* should only be called after backend is connected */ 1562 assert(hdev->vhost_ops); 1563 1564 for (i = 0; i < hdev->nvqs; ++i) { 1565 vhost_virtqueue_stop(hdev, 1566 vdev, 1567 hdev->vqs + i, 1568 hdev->vq_index + i); 1569 } 1570 1571 if (vhost_dev_has_iommu(hdev)) { 1572 hdev->vhost_ops->vhost_set_iotlb_callback(hdev, false); 1573 memory_listener_unregister(&hdev->iommu_listener); 1574 } 1575 vhost_log_put(hdev, true); 1576 hdev->started = false; 1577 hdev->vdev = NULL; 1578 } 1579 1580 int vhost_net_set_backend(struct vhost_dev *hdev, 1581 struct vhost_vring_file *file) 1582 { 1583 if (hdev->vhost_ops->vhost_net_set_backend) { 1584 return hdev->vhost_ops->vhost_net_set_backend(hdev, file); 1585 } 1586 1587 return -1; 1588 } 1589