xref: /openbmc/qemu/hw/vfio/listener.c (revision d9ce74873a6a5a7c504379857461e4ae64fcf0cd)
16b62a90cSCédric Le Goater /*
26b62a90cSCédric Le Goater  * generic functions used by VFIO devices
36b62a90cSCédric Le Goater  *
46b62a90cSCédric Le Goater  * Copyright Red Hat, Inc. 2012
56b62a90cSCédric Le Goater  *
66b62a90cSCédric Le Goater  * Authors:
76b62a90cSCédric Le Goater  *  Alex Williamson <alex.williamson@redhat.com>
86b62a90cSCédric Le Goater  *
96b62a90cSCédric Le Goater  * This work is licensed under the terms of the GNU GPL, version 2.  See
106b62a90cSCédric Le Goater  * the COPYING file in the top-level directory.
116b62a90cSCédric Le Goater  *
126b62a90cSCédric Le Goater  * Based on qemu-kvm device-assignment:
136b62a90cSCédric Le Goater  *  Adapted for KVM by Qumranet.
146b62a90cSCédric Le Goater  *  Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com)
156b62a90cSCédric Le Goater  *  Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com)
166b62a90cSCédric Le Goater  *  Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com)
176b62a90cSCédric Le Goater  *  Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com)
186b62a90cSCédric Le Goater  *  Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com)
196b62a90cSCédric Le Goater  */
206b62a90cSCédric Le Goater 
216b62a90cSCédric Le Goater #include "qemu/osdep.h"
226b62a90cSCédric Le Goater #include <sys/ioctl.h>
236b62a90cSCédric Le Goater #ifdef CONFIG_KVM
246b62a90cSCédric Le Goater #include <linux/kvm.h>
256b62a90cSCédric Le Goater #endif
266b62a90cSCédric Le Goater #include <linux/vfio.h>
276b62a90cSCédric Le Goater 
2811b8b9d5SCédric Le Goater #include "hw/vfio/vfio-device.h"
296b62a90cSCédric Le Goater #include "hw/vfio/pci.h"
306b62a90cSCédric Le Goater #include "system/address-spaces.h"
316b62a90cSCédric Le Goater #include "system/memory.h"
326b62a90cSCédric Le Goater #include "system/ram_addr.h"
336b62a90cSCédric Le Goater #include "hw/hw.h"
346b62a90cSCédric Le Goater #include "qemu/error-report.h"
356b62a90cSCédric Le Goater #include "qemu/main-loop.h"
366b62a90cSCédric Le Goater #include "qemu/range.h"
376b62a90cSCédric Le Goater #include "system/kvm.h"
386b62a90cSCédric Le Goater #include "system/reset.h"
396b62a90cSCédric Le Goater #include "system/runstate.h"
406b62a90cSCédric Le Goater #include "trace.h"
416b62a90cSCédric Le Goater #include "qapi/error.h"
426b62a90cSCédric Le Goater #include "migration/misc.h"
436b62a90cSCédric Le Goater #include "migration/qemu-file.h"
446b62a90cSCédric Le Goater #include "system/tcg.h"
456b62a90cSCédric Le Goater #include "system/tpm.h"
466b62a90cSCédric Le Goater #include "vfio-migration-internal.h"
476b62a90cSCédric Le Goater #include "vfio-helpers.h"
48a9183378SCédric Le Goater #include "vfio-listener.h"
496b62a90cSCédric Le Goater 
506b62a90cSCédric Le Goater /*
516b62a90cSCédric Le Goater  * Device state interfaces
526b62a90cSCédric Le Goater  */
536b62a90cSCédric Le Goater 
546b62a90cSCédric Le Goater 
vfio_log_sync_needed(const VFIOContainerBase * bcontainer)556b62a90cSCédric Le Goater static bool vfio_log_sync_needed(const VFIOContainerBase *bcontainer)
566b62a90cSCédric Le Goater {
576b62a90cSCédric Le Goater     VFIODevice *vbasedev;
586b62a90cSCédric Le Goater 
596b62a90cSCédric Le Goater     if (!vfio_container_dirty_tracking_is_started(bcontainer)) {
606b62a90cSCédric Le Goater         return false;
616b62a90cSCédric Le Goater     }
626b62a90cSCédric Le Goater 
636b62a90cSCédric Le Goater     QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
646b62a90cSCédric Le Goater         VFIOMigration *migration = vbasedev->migration;
656b62a90cSCédric Le Goater 
666b62a90cSCédric Le Goater         if (!migration) {
676b62a90cSCédric Le Goater             return false;
686b62a90cSCédric Le Goater         }
696b62a90cSCédric Le Goater 
706b62a90cSCédric Le Goater         if (vbasedev->pre_copy_dirty_page_tracking == ON_OFF_AUTO_OFF &&
716b62a90cSCédric Le Goater             (vfio_device_state_is_running(vbasedev) ||
726b62a90cSCédric Le Goater              vfio_device_state_is_precopy(vbasedev))) {
736b62a90cSCédric Le Goater             return false;
746b62a90cSCédric Le Goater         }
756b62a90cSCédric Le Goater     }
766b62a90cSCédric Le Goater     return true;
776b62a90cSCédric Le Goater }
786b62a90cSCédric Le Goater 
vfio_listener_skipped_section(MemoryRegionSection * section)796b62a90cSCédric Le Goater static bool vfio_listener_skipped_section(MemoryRegionSection *section)
806b62a90cSCédric Le Goater {
816b62a90cSCédric Le Goater     return (!memory_region_is_ram(section->mr) &&
826b62a90cSCédric Le Goater             !memory_region_is_iommu(section->mr)) ||
836b62a90cSCédric Le Goater            memory_region_is_protected(section->mr) ||
846b62a90cSCédric Le Goater            /*
856b62a90cSCédric Le Goater             * Sizing an enabled 64-bit BAR can cause spurious mappings to
866b62a90cSCédric Le Goater             * addresses in the upper part of the 64-bit address space.  These
876b62a90cSCédric Le Goater             * are never accessed by the CPU and beyond the address width of
886b62a90cSCédric Le Goater             * some IOMMU hardware.  TODO: VFIO should tell us the IOMMU width.
896b62a90cSCédric Le Goater             */
906b62a90cSCédric Le Goater            section->offset_within_address_space & (1ULL << 63);
916b62a90cSCédric Le Goater }
926b62a90cSCédric Le Goater 
93e3353d63SSteve Sistare /*
94e3353d63SSteve Sistare  * Called with rcu_read_lock held.
95e3353d63SSteve Sistare  * The returned MemoryRegion must not be accessed after calling rcu_read_unlock.
96e3353d63SSteve Sistare  */
vfio_translate_iotlb(IOMMUTLBEntry * iotlb,hwaddr * xlat_p,Error ** errp)97e3353d63SSteve Sistare static MemoryRegion *vfio_translate_iotlb(IOMMUTLBEntry *iotlb, hwaddr *xlat_p,
986b62a90cSCédric Le Goater                                           Error **errp)
996b62a90cSCédric Le Goater {
100e3353d63SSteve Sistare     MemoryRegion *mr;
1016b62a90cSCédric Le Goater 
102e3353d63SSteve Sistare     mr = memory_translate_iotlb(iotlb, xlat_p, errp);
103e3353d63SSteve Sistare     if (mr && memory_region_has_ram_discard_manager(mr)) {
1046b62a90cSCédric Le Goater         /*
1056b62a90cSCédric Le Goater          * Malicious VMs might trigger discarding of IOMMU-mapped memory. The
1066b62a90cSCédric Le Goater          * pages will remain pinned inside vfio until unmapped, resulting in a
1076b62a90cSCédric Le Goater          * higher memory consumption than expected. If memory would get
1086b62a90cSCédric Le Goater          * populated again later, there would be an inconsistency between pages
1096b62a90cSCédric Le Goater          * pinned by vfio and pages seen by QEMU. This is the case until
1106b62a90cSCédric Le Goater          * unmapped from the IOMMU (e.g., during device reset).
1116b62a90cSCédric Le Goater          *
1126b62a90cSCédric Le Goater          * With malicious guests, we really only care about pinning more memory
1136b62a90cSCédric Le Goater          * than expected. RLIMIT_MEMLOCK set for the user/process can never be
1146b62a90cSCédric Le Goater          * exceeded and can be used to mitigate this problem.
1156b62a90cSCédric Le Goater          */
1166b62a90cSCédric Le Goater         warn_report_once("Using vfio with vIOMMUs and coordinated discarding of"
1176b62a90cSCédric Le Goater                          " RAM (e.g., virtio-mem) works, however, malicious"
1186b62a90cSCédric Le Goater                          " guests can trigger pinning of more memory than"
1196b62a90cSCédric Le Goater                          " intended via an IOMMU. It's possible to mitigate "
1206b62a90cSCédric Le Goater                          " by setting/adjusting RLIMIT_MEMLOCK.");
1216b62a90cSCédric Le Goater     }
122e3353d63SSteve Sistare     return mr;
1236b62a90cSCédric Le Goater }
1246b62a90cSCédric Le Goater 
vfio_iommu_map_notify(IOMMUNotifier * n,IOMMUTLBEntry * iotlb)1256b62a90cSCédric Le Goater static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
1266b62a90cSCédric Le Goater {
1276b62a90cSCédric Le Goater     VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n);
1286b62a90cSCédric Le Goater     VFIOContainerBase *bcontainer = giommu->bcontainer;
1296b62a90cSCédric Le Goater     hwaddr iova = iotlb->iova + giommu->iommu_offset;
130e3353d63SSteve Sistare     MemoryRegion *mr;
131e3353d63SSteve Sistare     hwaddr xlat;
1326b62a90cSCédric Le Goater     void *vaddr;
1336b62a90cSCédric Le Goater     int ret;
1346b62a90cSCédric Le Goater     Error *local_err = NULL;
1356b62a90cSCédric Le Goater 
1366b62a90cSCédric Le Goater     trace_vfio_iommu_map_notify(iotlb->perm == IOMMU_NONE ? "UNMAP" : "MAP",
1376b62a90cSCédric Le Goater                                 iova, iova + iotlb->addr_mask);
1386b62a90cSCédric Le Goater 
1396b62a90cSCédric Le Goater     if (iotlb->target_as != &address_space_memory) {
1406b62a90cSCédric Le Goater         error_setg(&local_err,
1416b62a90cSCédric Le Goater                    "Wrong target AS \"%s\", only system memory is allowed",
1426b62a90cSCédric Le Goater                    iotlb->target_as->name ? iotlb->target_as->name : "none");
1436b62a90cSCédric Le Goater         if (migration_is_running()) {
1446b62a90cSCédric Le Goater             migration_file_set_error(-EINVAL, local_err);
1456b62a90cSCédric Le Goater         } else {
1466b62a90cSCédric Le Goater             error_report_err(local_err);
1476b62a90cSCédric Le Goater         }
1486b62a90cSCédric Le Goater         return;
1496b62a90cSCédric Le Goater     }
1506b62a90cSCédric Le Goater 
1516b62a90cSCédric Le Goater     rcu_read_lock();
1526b62a90cSCédric Le Goater 
1536b62a90cSCédric Le Goater     if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) {
1546b62a90cSCédric Le Goater         bool read_only;
1556b62a90cSCédric Le Goater 
156e3353d63SSteve Sistare         mr = vfio_translate_iotlb(iotlb, &xlat, &local_err);
157e3353d63SSteve Sistare         if (!mr) {
1586b62a90cSCédric Le Goater             error_report_err(local_err);
1596b62a90cSCédric Le Goater             goto out;
1606b62a90cSCédric Le Goater         }
161e3353d63SSteve Sistare         vaddr = memory_region_get_ram_ptr(mr) + xlat;
162e3353d63SSteve Sistare         read_only = !(iotlb->perm & IOMMU_WO) || mr->readonly;
163e3353d63SSteve Sistare 
1646b62a90cSCédric Le Goater         /*
1656b62a90cSCédric Le Goater          * vaddr is only valid until rcu_read_unlock(). But after
1666b62a90cSCédric Le Goater          * vfio_dma_map has set up the mapping the pages will be
1676b62a90cSCédric Le Goater          * pinned by the kernel. This makes sure that the RAM backend
1686b62a90cSCédric Le Goater          * of vaddr will always be there, even if the memory object is
1696b62a90cSCédric Le Goater          * destroyed and its backing memory munmap-ed.
1706b62a90cSCédric Le Goater          */
1716b62a90cSCédric Le Goater         ret = vfio_container_dma_map(bcontainer, iova,
1726b62a90cSCédric Le Goater                                      iotlb->addr_mask + 1, vaddr,
17344d0acf8SJohn Levon                                      read_only, mr);
1746b62a90cSCédric Le Goater         if (ret) {
1756b62a90cSCédric Le Goater             error_report("vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", "
1766b62a90cSCédric Le Goater                          "0x%"HWADDR_PRIx", %p) = %d (%s)",
1776b62a90cSCédric Le Goater                          bcontainer, iova,
1786b62a90cSCédric Le Goater                          iotlb->addr_mask + 1, vaddr, ret, strerror(-ret));
1796b62a90cSCédric Le Goater         }
1806b62a90cSCédric Le Goater     } else {
1816b62a90cSCédric Le Goater         ret = vfio_container_dma_unmap(bcontainer, iova,
1825a22b505SJohn Levon                                        iotlb->addr_mask + 1, iotlb, false);
1836b62a90cSCédric Le Goater         if (ret) {
1846b62a90cSCédric Le Goater             error_setg(&local_err,
1856b62a90cSCédric Le Goater                        "vfio_container_dma_unmap(%p, 0x%"HWADDR_PRIx", "
1866b62a90cSCédric Le Goater                        "0x%"HWADDR_PRIx") = %d (%s)",
1876b62a90cSCédric Le Goater                        bcontainer, iova,
1886b62a90cSCédric Le Goater                        iotlb->addr_mask + 1, ret, strerror(-ret));
1896b62a90cSCédric Le Goater             if (migration_is_running()) {
1906b62a90cSCédric Le Goater                 migration_file_set_error(ret, local_err);
1916b62a90cSCédric Le Goater             } else {
1926b62a90cSCédric Le Goater                 error_report_err(local_err);
1936b62a90cSCédric Le Goater             }
1946b62a90cSCédric Le Goater         }
1956b62a90cSCédric Le Goater     }
1966b62a90cSCédric Le Goater out:
1976b62a90cSCédric Le Goater     rcu_read_unlock();
1986b62a90cSCédric Le Goater }
1996b62a90cSCédric Le Goater 
vfio_ram_discard_notify_discard(RamDiscardListener * rdl,MemoryRegionSection * section)2006b62a90cSCédric Le Goater static void vfio_ram_discard_notify_discard(RamDiscardListener *rdl,
2016b62a90cSCédric Le Goater                                             MemoryRegionSection *section)
2026b62a90cSCédric Le Goater {
2036b62a90cSCédric Le Goater     VFIORamDiscardListener *vrdl = container_of(rdl, VFIORamDiscardListener,
2046b62a90cSCédric Le Goater                                                 listener);
2056b62a90cSCédric Le Goater     VFIOContainerBase *bcontainer = vrdl->bcontainer;
2066b62a90cSCédric Le Goater     const hwaddr size = int128_get64(section->size);
2076b62a90cSCédric Le Goater     const hwaddr iova = section->offset_within_address_space;
2086b62a90cSCédric Le Goater     int ret;
2096b62a90cSCédric Le Goater 
2106b62a90cSCédric Le Goater     /* Unmap with a single call. */
2115a22b505SJohn Levon     ret = vfio_container_dma_unmap(bcontainer, iova, size , NULL, false);
2126b62a90cSCédric Le Goater     if (ret) {
2136b62a90cSCédric Le Goater         error_report("%s: vfio_container_dma_unmap() failed: %s", __func__,
2146b62a90cSCédric Le Goater                      strerror(-ret));
2156b62a90cSCédric Le Goater     }
2166b62a90cSCédric Le Goater }
2176b62a90cSCédric Le Goater 
vfio_ram_discard_notify_populate(RamDiscardListener * rdl,MemoryRegionSection * section)2186b62a90cSCédric Le Goater static int vfio_ram_discard_notify_populate(RamDiscardListener *rdl,
2196b62a90cSCédric Le Goater                                             MemoryRegionSection *section)
2206b62a90cSCédric Le Goater {
2216b62a90cSCédric Le Goater     VFIORamDiscardListener *vrdl = container_of(rdl, VFIORamDiscardListener,
2226b62a90cSCédric Le Goater                                                 listener);
2236b62a90cSCédric Le Goater     VFIOContainerBase *bcontainer = vrdl->bcontainer;
2246b62a90cSCédric Le Goater     const hwaddr end = section->offset_within_region +
2256b62a90cSCédric Le Goater                        int128_get64(section->size);
2266b62a90cSCédric Le Goater     hwaddr start, next, iova;
2276b62a90cSCédric Le Goater     void *vaddr;
2286b62a90cSCédric Le Goater     int ret;
2296b62a90cSCédric Le Goater 
2306b62a90cSCédric Le Goater     /*
2316b62a90cSCédric Le Goater      * Map in (aligned within memory region) minimum granularity, so we can
2326b62a90cSCédric Le Goater      * unmap in minimum granularity later.
2336b62a90cSCédric Le Goater      */
2346b62a90cSCédric Le Goater     for (start = section->offset_within_region; start < end; start = next) {
2356b62a90cSCédric Le Goater         next = ROUND_UP(start + 1, vrdl->granularity);
2366b62a90cSCédric Le Goater         next = MIN(next, end);
2376b62a90cSCédric Le Goater 
2386b62a90cSCédric Le Goater         iova = start - section->offset_within_region +
2396b62a90cSCédric Le Goater                section->offset_within_address_space;
2406b62a90cSCédric Le Goater         vaddr = memory_region_get_ram_ptr(section->mr) + start;
2416b62a90cSCédric Le Goater 
2426b62a90cSCédric Le Goater         ret = vfio_container_dma_map(bcontainer, iova, next - start,
24344d0acf8SJohn Levon                                      vaddr, section->readonly, section->mr);
2446b62a90cSCédric Le Goater         if (ret) {
2456b62a90cSCédric Le Goater             /* Rollback */
2466b62a90cSCédric Le Goater             vfio_ram_discard_notify_discard(rdl, section);
2476b62a90cSCédric Le Goater             return ret;
2486b62a90cSCédric Le Goater         }
2496b62a90cSCédric Le Goater     }
2506b62a90cSCédric Le Goater     return 0;
2516b62a90cSCédric Le Goater }
2526b62a90cSCédric Le Goater 
vfio_ram_discard_register_listener(VFIOContainerBase * bcontainer,MemoryRegionSection * section)25374d37637SCédric Le Goater static void vfio_ram_discard_register_listener(VFIOContainerBase *bcontainer,
2546b62a90cSCédric Le Goater                                                MemoryRegionSection *section)
2556b62a90cSCédric Le Goater {
2566b62a90cSCédric Le Goater     RamDiscardManager *rdm = memory_region_get_ram_discard_manager(section->mr);
2576b62a90cSCédric Le Goater     int target_page_size = qemu_target_page_size();
2586b62a90cSCédric Le Goater     VFIORamDiscardListener *vrdl;
2596b62a90cSCédric Le Goater 
2606b62a90cSCédric Le Goater     /* Ignore some corner cases not relevant in practice. */
2616b62a90cSCédric Le Goater     g_assert(QEMU_IS_ALIGNED(section->offset_within_region, target_page_size));
2626b62a90cSCédric Le Goater     g_assert(QEMU_IS_ALIGNED(section->offset_within_address_space,
2636b62a90cSCédric Le Goater                              target_page_size));
2646b62a90cSCédric Le Goater     g_assert(QEMU_IS_ALIGNED(int128_get64(section->size), target_page_size));
2656b62a90cSCédric Le Goater 
2666b62a90cSCédric Le Goater     vrdl = g_new0(VFIORamDiscardListener, 1);
2676b62a90cSCédric Le Goater     vrdl->bcontainer = bcontainer;
2686b62a90cSCédric Le Goater     vrdl->mr = section->mr;
2696b62a90cSCédric Le Goater     vrdl->offset_within_address_space = section->offset_within_address_space;
2706b62a90cSCédric Le Goater     vrdl->size = int128_get64(section->size);
2716b62a90cSCédric Le Goater     vrdl->granularity = ram_discard_manager_get_min_granularity(rdm,
2726b62a90cSCédric Le Goater                                                                 section->mr);
2736b62a90cSCédric Le Goater 
2746b62a90cSCédric Le Goater     g_assert(vrdl->granularity && is_power_of_2(vrdl->granularity));
2756b62a90cSCédric Le Goater     g_assert(bcontainer->pgsizes &&
2766b62a90cSCédric Le Goater              vrdl->granularity >= 1ULL << ctz64(bcontainer->pgsizes));
2776b62a90cSCédric Le Goater 
2786b62a90cSCédric Le Goater     ram_discard_listener_init(&vrdl->listener,
2796b62a90cSCédric Le Goater                               vfio_ram_discard_notify_populate,
2806b62a90cSCédric Le Goater                               vfio_ram_discard_notify_discard, true);
2816b62a90cSCédric Le Goater     ram_discard_manager_register_listener(rdm, &vrdl->listener, section);
2826b62a90cSCédric Le Goater     QLIST_INSERT_HEAD(&bcontainer->vrdl_list, vrdl, next);
2836b62a90cSCédric Le Goater 
2846b62a90cSCédric Le Goater     /*
2856b62a90cSCédric Le Goater      * Sanity-check if we have a theoretically problematic setup where we could
2866b62a90cSCédric Le Goater      * exceed the maximum number of possible DMA mappings over time. We assume
2876b62a90cSCédric Le Goater      * that each mapped section in the same address space as a RamDiscardManager
2886b62a90cSCédric Le Goater      * section consumes exactly one DMA mapping, with the exception of
2896b62a90cSCédric Le Goater      * RamDiscardManager sections; i.e., we don't expect to have gIOMMU sections
2906b62a90cSCédric Le Goater      * in the same address space as RamDiscardManager sections.
2916b62a90cSCédric Le Goater      *
2926b62a90cSCédric Le Goater      * We assume that each section in the address space consumes one memslot.
2936b62a90cSCédric Le Goater      * We take the number of KVM memory slots as a best guess for the maximum
2946b62a90cSCédric Le Goater      * number of sections in the address space we could have over time,
2956b62a90cSCédric Le Goater      * also consuming DMA mappings.
2966b62a90cSCédric Le Goater      */
2976b62a90cSCédric Le Goater     if (bcontainer->dma_max_mappings) {
2986b62a90cSCédric Le Goater         unsigned int vrdl_count = 0, vrdl_mappings = 0, max_memslots = 512;
2996b62a90cSCédric Le Goater 
3006b62a90cSCédric Le Goater #ifdef CONFIG_KVM
3016b62a90cSCédric Le Goater         if (kvm_enabled()) {
3026b62a90cSCédric Le Goater             max_memslots = kvm_get_max_memslots();
3036b62a90cSCédric Le Goater         }
3046b62a90cSCédric Le Goater #endif
3056b62a90cSCédric Le Goater 
3066b62a90cSCédric Le Goater         QLIST_FOREACH(vrdl, &bcontainer->vrdl_list, next) {
3076b62a90cSCédric Le Goater             hwaddr start, end;
3086b62a90cSCédric Le Goater 
3096b62a90cSCédric Le Goater             start = QEMU_ALIGN_DOWN(vrdl->offset_within_address_space,
3106b62a90cSCédric Le Goater                                     vrdl->granularity);
3116b62a90cSCédric Le Goater             end = ROUND_UP(vrdl->offset_within_address_space + vrdl->size,
3126b62a90cSCédric Le Goater                            vrdl->granularity);
3136b62a90cSCédric Le Goater             vrdl_mappings += (end - start) / vrdl->granularity;
3146b62a90cSCédric Le Goater             vrdl_count++;
3156b62a90cSCédric Le Goater         }
3166b62a90cSCédric Le Goater 
3176b62a90cSCédric Le Goater         if (vrdl_mappings + max_memslots - vrdl_count >
3186b62a90cSCédric Le Goater             bcontainer->dma_max_mappings) {
3196b62a90cSCédric Le Goater             warn_report("%s: possibly running out of DMA mappings. E.g., try"
3206b62a90cSCédric Le Goater                         " increasing the 'block-size' of virtio-mem devies."
3216b62a90cSCédric Le Goater                         " Maximum possible DMA mappings: %d, Maximum possible"
3226b62a90cSCédric Le Goater                         " memslots: %d", __func__, bcontainer->dma_max_mappings,
3236b62a90cSCédric Le Goater                         max_memslots);
3246b62a90cSCédric Le Goater         }
3256b62a90cSCédric Le Goater     }
3266b62a90cSCédric Le Goater }
3276b62a90cSCédric Le Goater 
vfio_ram_discard_unregister_listener(VFIOContainerBase * bcontainer,MemoryRegionSection * section)32874d37637SCédric Le Goater static void vfio_ram_discard_unregister_listener(VFIOContainerBase *bcontainer,
3296b62a90cSCédric Le Goater                                                  MemoryRegionSection *section)
3306b62a90cSCédric Le Goater {
3316b62a90cSCédric Le Goater     RamDiscardManager *rdm = memory_region_get_ram_discard_manager(section->mr);
3326b62a90cSCédric Le Goater     VFIORamDiscardListener *vrdl = NULL;
3336b62a90cSCédric Le Goater 
3346b62a90cSCédric Le Goater     QLIST_FOREACH(vrdl, &bcontainer->vrdl_list, next) {
3356b62a90cSCédric Le Goater         if (vrdl->mr == section->mr &&
3366b62a90cSCédric Le Goater             vrdl->offset_within_address_space ==
3376b62a90cSCédric Le Goater             section->offset_within_address_space) {
3386b62a90cSCédric Le Goater             break;
3396b62a90cSCédric Le Goater         }
3406b62a90cSCédric Le Goater     }
3416b62a90cSCédric Le Goater 
3426b62a90cSCédric Le Goater     if (!vrdl) {
3436b62a90cSCédric Le Goater         hw_error("vfio: Trying to unregister missing RAM discard listener");
3446b62a90cSCédric Le Goater     }
3456b62a90cSCédric Le Goater 
3466b62a90cSCédric Le Goater     ram_discard_manager_unregister_listener(rdm, &vrdl->listener);
3476b62a90cSCédric Le Goater     QLIST_REMOVE(vrdl, next);
3486b62a90cSCédric Le Goater     g_free(vrdl);
3496b62a90cSCédric Le Goater }
3506b62a90cSCédric Le Goater 
vfio_known_safe_misalignment(MemoryRegionSection * section)3516b62a90cSCédric Le Goater static bool vfio_known_safe_misalignment(MemoryRegionSection *section)
3526b62a90cSCédric Le Goater {
3536b62a90cSCédric Le Goater     MemoryRegion *mr = section->mr;
3546b62a90cSCédric Le Goater 
3556b62a90cSCédric Le Goater     if (!TPM_IS_CRB(mr->owner)) {
3566b62a90cSCédric Le Goater         return false;
3576b62a90cSCédric Le Goater     }
3586b62a90cSCédric Le Goater 
3596b62a90cSCédric Le Goater     /* this is a known safe misaligned region, just trace for debug purpose */
3606b62a90cSCédric Le Goater     trace_vfio_known_safe_misalignment(memory_region_name(mr),
3616b62a90cSCédric Le Goater                                        section->offset_within_address_space,
3626b62a90cSCédric Le Goater                                        section->offset_within_region,
3636b62a90cSCédric Le Goater                                        qemu_real_host_page_size());
3646b62a90cSCédric Le Goater     return true;
3656b62a90cSCédric Le Goater }
3666b62a90cSCédric Le Goater 
vfio_listener_valid_section(MemoryRegionSection * section,const char * name)3676b62a90cSCédric Le Goater static bool vfio_listener_valid_section(MemoryRegionSection *section,
3686b62a90cSCédric Le Goater                                         const char *name)
3696b62a90cSCédric Le Goater {
3706b62a90cSCédric Le Goater     if (vfio_listener_skipped_section(section)) {
3716b62a90cSCédric Le Goater         trace_vfio_listener_region_skip(name,
3726b62a90cSCédric Le Goater                 section->offset_within_address_space,
3736b62a90cSCédric Le Goater                 section->offset_within_address_space +
3746b62a90cSCédric Le Goater                 int128_get64(int128_sub(section->size, int128_one())));
3756b62a90cSCédric Le Goater         return false;
3766b62a90cSCédric Le Goater     }
3776b62a90cSCédric Le Goater 
3786b62a90cSCédric Le Goater     if (unlikely((section->offset_within_address_space &
3796b62a90cSCédric Le Goater                   ~qemu_real_host_page_mask()) !=
3806b62a90cSCédric Le Goater                  (section->offset_within_region & ~qemu_real_host_page_mask()))) {
3816b62a90cSCédric Le Goater         if (!vfio_known_safe_misalignment(section)) {
3826b62a90cSCédric Le Goater             error_report("%s received unaligned region %s iova=0x%"PRIx64
3836b62a90cSCédric Le Goater                          " offset_within_region=0x%"PRIx64
3846b62a90cSCédric Le Goater                          " qemu_real_host_page_size=0x%"PRIxPTR,
3856b62a90cSCédric Le Goater                          __func__, memory_region_name(section->mr),
3866b62a90cSCédric Le Goater                          section->offset_within_address_space,
3876b62a90cSCédric Le Goater                          section->offset_within_region,
3886b62a90cSCédric Le Goater                          qemu_real_host_page_size());
3896b62a90cSCédric Le Goater         }
3906b62a90cSCédric Le Goater         return false;
3916b62a90cSCédric Le Goater     }
3926b62a90cSCédric Le Goater 
3936b62a90cSCédric Le Goater     return true;
3946b62a90cSCédric Le Goater }
3956b62a90cSCédric Le Goater 
vfio_get_section_iova_range(VFIOContainerBase * bcontainer,MemoryRegionSection * section,hwaddr * out_iova,hwaddr * out_end,Int128 * out_llend)3966b62a90cSCédric Le Goater static bool vfio_get_section_iova_range(VFIOContainerBase *bcontainer,
3976b62a90cSCédric Le Goater                                         MemoryRegionSection *section,
3986b62a90cSCédric Le Goater                                         hwaddr *out_iova, hwaddr *out_end,
3996b62a90cSCédric Le Goater                                         Int128 *out_llend)
4006b62a90cSCédric Le Goater {
4016b62a90cSCédric Le Goater     Int128 llend;
4026b62a90cSCédric Le Goater     hwaddr iova;
4036b62a90cSCédric Le Goater 
4046b62a90cSCédric Le Goater     iova = REAL_HOST_PAGE_ALIGN(section->offset_within_address_space);
4056b62a90cSCédric Le Goater     llend = int128_make64(section->offset_within_address_space);
4066b62a90cSCédric Le Goater     llend = int128_add(llend, section->size);
4076b62a90cSCédric Le Goater     llend = int128_and(llend, int128_exts64(qemu_real_host_page_mask()));
4086b62a90cSCédric Le Goater 
4096b62a90cSCédric Le Goater     if (int128_ge(int128_make64(iova), llend)) {
4106b62a90cSCédric Le Goater         return false;
4116b62a90cSCédric Le Goater     }
4126b62a90cSCédric Le Goater 
4136b62a90cSCédric Le Goater     *out_iova = iova;
4146b62a90cSCédric Le Goater     *out_end = int128_get64(int128_sub(llend, int128_one()));
4156b62a90cSCédric Le Goater     if (out_llend) {
4166b62a90cSCédric Le Goater         *out_llend = llend;
4176b62a90cSCédric Le Goater     }
4186b62a90cSCédric Le Goater     return true;
4196b62a90cSCédric Le Goater }
4206b62a90cSCédric Le Goater 
vfio_listener_begin(MemoryListener * listener)421d9b7d8b6SJohn Levon static void vfio_listener_begin(MemoryListener *listener)
422d9b7d8b6SJohn Levon {
423d9b7d8b6SJohn Levon     VFIOContainerBase *bcontainer = container_of(listener, VFIOContainerBase,
424d9b7d8b6SJohn Levon                                                  listener);
425d9b7d8b6SJohn Levon     void (*listener_begin)(VFIOContainerBase *bcontainer);
426d9b7d8b6SJohn Levon 
427d9b7d8b6SJohn Levon     listener_begin = VFIO_IOMMU_GET_CLASS(bcontainer)->listener_begin;
428d9b7d8b6SJohn Levon 
429d9b7d8b6SJohn Levon     if (listener_begin) {
430d9b7d8b6SJohn Levon         listener_begin(bcontainer);
431d9b7d8b6SJohn Levon     }
432d9b7d8b6SJohn Levon }
433d9b7d8b6SJohn Levon 
vfio_listener_commit(MemoryListener * listener)434d9b7d8b6SJohn Levon static void vfio_listener_commit(MemoryListener *listener)
435d9b7d8b6SJohn Levon {
436d9b7d8b6SJohn Levon     VFIOContainerBase *bcontainer = container_of(listener, VFIOContainerBase,
437d9b7d8b6SJohn Levon                                                  listener);
438d9b7d8b6SJohn Levon     void (*listener_commit)(VFIOContainerBase *bcontainer);
439d9b7d8b6SJohn Levon 
440144227dbSZhenzhong Duan     listener_commit = VFIO_IOMMU_GET_CLASS(bcontainer)->listener_commit;
441d9b7d8b6SJohn Levon 
442d9b7d8b6SJohn Levon     if (listener_commit) {
443d9b7d8b6SJohn Levon         listener_commit(bcontainer);
444d9b7d8b6SJohn Levon     }
445d9b7d8b6SJohn Levon }
446d9b7d8b6SJohn Levon 
vfio_device_error_append(VFIODevice * vbasedev,Error ** errp)4476b62a90cSCédric Le Goater static void vfio_device_error_append(VFIODevice *vbasedev, Error **errp)
4486b62a90cSCédric Le Goater {
4496b62a90cSCédric Le Goater     /*
4506b62a90cSCédric Le Goater      * MMIO region mapping failures are not fatal but in this case PCI
4516b62a90cSCédric Le Goater      * peer-to-peer transactions are broken.
4526b62a90cSCédric Le Goater      */
4536b62a90cSCédric Le Goater     if (vbasedev && vbasedev->type == VFIO_DEVICE_TYPE_PCI) {
4546b62a90cSCédric Le Goater         error_append_hint(errp, "%s: PCI peer-to-peer transactions "
4556b62a90cSCédric Le Goater                           "on BARs are not supported.\n", vbasedev->name);
4566b62a90cSCédric Le Goater     }
4576b62a90cSCédric Le Goater }
4586b62a90cSCédric Le Goater 
vfio_find_ram_discard_listener(VFIOContainerBase * bcontainer,MemoryRegionSection * section)4592372f8d9SSteve Sistare VFIORamDiscardListener *vfio_find_ram_discard_listener(
4602372f8d9SSteve Sistare     VFIOContainerBase *bcontainer, MemoryRegionSection *section)
4612372f8d9SSteve Sistare {
4622372f8d9SSteve Sistare     VFIORamDiscardListener *vrdl = NULL;
4632372f8d9SSteve Sistare 
4642372f8d9SSteve Sistare     QLIST_FOREACH(vrdl, &bcontainer->vrdl_list, next) {
4652372f8d9SSteve Sistare         if (vrdl->mr == section->mr &&
4662372f8d9SSteve Sistare             vrdl->offset_within_address_space ==
4672372f8d9SSteve Sistare             section->offset_within_address_space) {
4682372f8d9SSteve Sistare             break;
4692372f8d9SSteve Sistare         }
4702372f8d9SSteve Sistare     }
4712372f8d9SSteve Sistare 
4722372f8d9SSteve Sistare     if (!vrdl) {
4732372f8d9SSteve Sistare         hw_error("vfio: Trying to sync missing RAM discard listener");
4742372f8d9SSteve Sistare         /* does not return */
4752372f8d9SSteve Sistare     }
4762372f8d9SSteve Sistare     return vrdl;
4772372f8d9SSteve Sistare }
4782372f8d9SSteve Sistare 
vfio_listener_region_add(MemoryListener * listener,MemoryRegionSection * section)4796b62a90cSCédric Le Goater static void vfio_listener_region_add(MemoryListener *listener,
4806b62a90cSCédric Le Goater                                      MemoryRegionSection *section)
4816b62a90cSCédric Le Goater {
4826b62a90cSCédric Le Goater     VFIOContainerBase *bcontainer = container_of(listener, VFIOContainerBase,
4836b62a90cSCédric Le Goater                                                  listener);
484*eba1f657SSteve Sistare     vfio_container_region_add(bcontainer, section, false);
485*eba1f657SSteve Sistare }
486*eba1f657SSteve Sistare 
vfio_container_region_add(VFIOContainerBase * bcontainer,MemoryRegionSection * section,bool cpr_remap)487*eba1f657SSteve Sistare void vfio_container_region_add(VFIOContainerBase *bcontainer,
488*eba1f657SSteve Sistare                                MemoryRegionSection *section,
489*eba1f657SSteve Sistare                                bool cpr_remap)
490*eba1f657SSteve Sistare {
4916b62a90cSCédric Le Goater     hwaddr iova, end;
4926b62a90cSCédric Le Goater     Int128 llend, llsize;
4936b62a90cSCédric Le Goater     void *vaddr;
4946b62a90cSCédric Le Goater     int ret;
4956b62a90cSCédric Le Goater     Error *err = NULL;
4966b62a90cSCédric Le Goater 
4976b62a90cSCédric Le Goater     if (!vfio_listener_valid_section(section, "region_add")) {
4986b62a90cSCédric Le Goater         return;
4996b62a90cSCédric Le Goater     }
5006b62a90cSCédric Le Goater 
5016b62a90cSCédric Le Goater     if (!vfio_get_section_iova_range(bcontainer, section, &iova, &end,
5026b62a90cSCédric Le Goater                                      &llend)) {
5036b62a90cSCédric Le Goater         if (memory_region_is_ram_device(section->mr)) {
5046b62a90cSCédric Le Goater             trace_vfio_listener_region_add_no_dma_map(
5056b62a90cSCédric Le Goater                 memory_region_name(section->mr),
5066b62a90cSCédric Le Goater                 section->offset_within_address_space,
5076b62a90cSCédric Le Goater                 int128_getlo(section->size),
5086b62a90cSCédric Le Goater                 qemu_real_host_page_size());
5096b62a90cSCédric Le Goater         }
5106b62a90cSCédric Le Goater         return;
5116b62a90cSCédric Le Goater     }
5126b62a90cSCédric Le Goater 
5136b62a90cSCédric Le Goater     /* PPC64/pseries machine only */
5146b62a90cSCédric Le Goater     if (!vfio_container_add_section_window(bcontainer, section, &err)) {
5156b62a90cSCédric Le Goater         goto mmio_dma_error;
5166b62a90cSCédric Le Goater     }
5176b62a90cSCédric Le Goater 
5186b62a90cSCédric Le Goater     memory_region_ref(section->mr);
5196b62a90cSCédric Le Goater 
5206b62a90cSCédric Le Goater     if (memory_region_is_iommu(section->mr)) {
5216b62a90cSCédric Le Goater         VFIOGuestIOMMU *giommu;
5226b62a90cSCédric Le Goater         IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
5236b62a90cSCédric Le Goater         int iommu_idx;
5246b62a90cSCédric Le Goater 
5256b62a90cSCédric Le Goater         trace_vfio_listener_region_add_iommu(section->mr->name, iova, end);
526*eba1f657SSteve Sistare 
527*eba1f657SSteve Sistare         if (cpr_remap) {
528*eba1f657SSteve Sistare             vfio_cpr_giommu_remap(bcontainer, section);
529*eba1f657SSteve Sistare         }
530*eba1f657SSteve Sistare 
5316b62a90cSCédric Le Goater         /*
5326b62a90cSCédric Le Goater          * FIXME: For VFIO iommu types which have KVM acceleration to
5336b62a90cSCédric Le Goater          * avoid bouncing all map/unmaps through qemu this way, this
5346b62a90cSCédric Le Goater          * would be the right place to wire that up (tell the KVM
5356b62a90cSCédric Le Goater          * device emulation the VFIO iommu handles to use).
5366b62a90cSCédric Le Goater          */
5376b62a90cSCédric Le Goater         giommu = g_malloc0(sizeof(*giommu));
5386b62a90cSCédric Le Goater         giommu->iommu_mr = iommu_mr;
5396b62a90cSCédric Le Goater         giommu->iommu_offset = section->offset_within_address_space -
5406b62a90cSCédric Le Goater                                section->offset_within_region;
5416b62a90cSCédric Le Goater         giommu->bcontainer = bcontainer;
5426b62a90cSCédric Le Goater         llend = int128_add(int128_make64(section->offset_within_region),
5436b62a90cSCédric Le Goater                            section->size);
5446b62a90cSCédric Le Goater         llend = int128_sub(llend, int128_one());
5456b62a90cSCédric Le Goater         iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr,
5466b62a90cSCédric Le Goater                                                        MEMTXATTRS_UNSPECIFIED);
5476b62a90cSCédric Le Goater         iommu_notifier_init(&giommu->n, vfio_iommu_map_notify,
5486b62a90cSCédric Le Goater                             IOMMU_NOTIFIER_IOTLB_EVENTS,
5496b62a90cSCédric Le Goater                             section->offset_within_region,
5506b62a90cSCédric Le Goater                             int128_get64(llend),
5516b62a90cSCédric Le Goater                             iommu_idx);
5526b62a90cSCédric Le Goater 
5536b62a90cSCédric Le Goater         ret = memory_region_register_iommu_notifier(section->mr, &giommu->n,
5546b62a90cSCédric Le Goater                                                     &err);
5556b62a90cSCédric Le Goater         if (ret) {
5566b62a90cSCédric Le Goater             g_free(giommu);
5576b62a90cSCédric Le Goater             goto fail;
5586b62a90cSCédric Le Goater         }
5596b62a90cSCédric Le Goater         QLIST_INSERT_HEAD(&bcontainer->giommu_list, giommu, giommu_next);
5606b62a90cSCédric Le Goater         memory_region_iommu_replay(giommu->iommu_mr, &giommu->n);
5616b62a90cSCédric Le Goater 
5626b62a90cSCédric Le Goater         return;
5636b62a90cSCédric Le Goater     }
5646b62a90cSCédric Le Goater 
5656b62a90cSCédric Le Goater     /* Here we assume that memory_region_is_ram(section->mr)==true */
5666b62a90cSCédric Le Goater 
5676b62a90cSCédric Le Goater     /*
5686b62a90cSCédric Le Goater      * For RAM memory regions with a RamDiscardManager, we only want to map the
5696b62a90cSCédric Le Goater      * actually populated parts - and update the mapping whenever we're notified
5706b62a90cSCédric Le Goater      * about changes.
5716b62a90cSCédric Le Goater      */
5726b62a90cSCédric Le Goater     if (memory_region_has_ram_discard_manager(section->mr)) {
573*eba1f657SSteve Sistare         if (!cpr_remap) {
57474d37637SCédric Le Goater             vfio_ram_discard_register_listener(bcontainer, section);
575*eba1f657SSteve Sistare         } else if (!vfio_cpr_ram_discard_register_listener(bcontainer,
576*eba1f657SSteve Sistare                                                            section)) {
577*eba1f657SSteve Sistare             goto fail;
578*eba1f657SSteve Sistare         }
5796b62a90cSCédric Le Goater         return;
5806b62a90cSCédric Le Goater     }
5816b62a90cSCédric Le Goater 
5826b62a90cSCédric Le Goater     vaddr = memory_region_get_ram_ptr(section->mr) +
5836b62a90cSCédric Le Goater             section->offset_within_region +
5846b62a90cSCédric Le Goater             (iova - section->offset_within_address_space);
5856b62a90cSCédric Le Goater 
5866b62a90cSCédric Le Goater     trace_vfio_listener_region_add_ram(iova, end, vaddr);
5876b62a90cSCédric Le Goater 
5886b62a90cSCédric Le Goater     llsize = int128_sub(llend, int128_make64(iova));
5896b62a90cSCédric Le Goater 
5906b62a90cSCédric Le Goater     if (memory_region_is_ram_device(section->mr)) {
5916b62a90cSCédric Le Goater         hwaddr pgmask = (1ULL << ctz64(bcontainer->pgsizes)) - 1;
5926b62a90cSCédric Le Goater 
5936b62a90cSCédric Le Goater         if ((iova & pgmask) || (int128_get64(llsize) & pgmask)) {
5946b62a90cSCédric Le Goater             trace_vfio_listener_region_add_no_dma_map(
5956b62a90cSCédric Le Goater                 memory_region_name(section->mr),
5966b62a90cSCédric Le Goater                 section->offset_within_address_space,
5976b62a90cSCédric Le Goater                 int128_getlo(section->size),
5986b62a90cSCédric Le Goater                 pgmask + 1);
5996b62a90cSCédric Le Goater             return;
6006b62a90cSCédric Le Goater         }
6016b62a90cSCédric Le Goater     }
6026b62a90cSCédric Le Goater 
6036b62a90cSCédric Le Goater     ret = vfio_container_dma_map(bcontainer, iova, int128_get64(llsize),
60444d0acf8SJohn Levon                                  vaddr, section->readonly, section->mr);
6056b62a90cSCédric Le Goater     if (ret) {
6066b62a90cSCédric Le Goater         error_setg(&err, "vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", "
6076b62a90cSCédric Le Goater                    "0x%"HWADDR_PRIx", %p) = %d (%s)",
6086b62a90cSCédric Le Goater                    bcontainer, iova, int128_get64(llsize), vaddr, ret,
6096b62a90cSCédric Le Goater                    strerror(-ret));
6106b62a90cSCédric Le Goater     mmio_dma_error:
6116b62a90cSCédric Le Goater         if (memory_region_is_ram_device(section->mr)) {
6126b62a90cSCédric Le Goater             /* Allow unexpected mappings not to be fatal for RAM devices */
6136b62a90cSCédric Le Goater             VFIODevice *vbasedev =
6146b62a90cSCédric Le Goater                 vfio_get_vfio_device(memory_region_owner(section->mr));
6156b62a90cSCédric Le Goater             vfio_device_error_append(vbasedev, &err);
6166b62a90cSCédric Le Goater             warn_report_err_once(err);
6176b62a90cSCédric Le Goater             return;
6186b62a90cSCédric Le Goater         }
6196b62a90cSCédric Le Goater         goto fail;
6206b62a90cSCédric Le Goater     }
6216b62a90cSCédric Le Goater 
6226b62a90cSCédric Le Goater     return;
6236b62a90cSCédric Le Goater 
6246b62a90cSCédric Le Goater fail:
6256b62a90cSCédric Le Goater     if (!bcontainer->initialized) {
6266b62a90cSCédric Le Goater         /*
6276b62a90cSCédric Le Goater          * At machine init time or when the device is attached to the
6286b62a90cSCédric Le Goater          * VM, store the first error in the container so we can
6296b62a90cSCédric Le Goater          * gracefully fail the device realize routine.
6306b62a90cSCédric Le Goater          */
6316b62a90cSCédric Le Goater         if (!bcontainer->error) {
6326b62a90cSCédric Le Goater             error_propagate_prepend(&bcontainer->error, err,
6336b62a90cSCédric Le Goater                                     "Region %s: ",
6346b62a90cSCédric Le Goater                                     memory_region_name(section->mr));
6356b62a90cSCédric Le Goater         } else {
6366b62a90cSCédric Le Goater             error_free(err);
6376b62a90cSCédric Le Goater         }
6386b62a90cSCédric Le Goater     } else {
6396b62a90cSCédric Le Goater         /*
6406b62a90cSCédric Le Goater          * At runtime, there's not much we can do other than throw a
6416b62a90cSCédric Le Goater          * hardware error.
6426b62a90cSCédric Le Goater          */
6436b62a90cSCédric Le Goater         error_report_err(err);
6446b62a90cSCédric Le Goater         hw_error("vfio: DMA mapping failed, unable to continue");
6456b62a90cSCédric Le Goater     }
6466b62a90cSCédric Le Goater }
6476b62a90cSCédric Le Goater 
vfio_listener_region_del(MemoryListener * listener,MemoryRegionSection * section)6486b62a90cSCédric Le Goater static void vfio_listener_region_del(MemoryListener *listener,
6496b62a90cSCédric Le Goater                                      MemoryRegionSection *section)
6506b62a90cSCédric Le Goater {
6516b62a90cSCédric Le Goater     VFIOContainerBase *bcontainer = container_of(listener, VFIOContainerBase,
6526b62a90cSCédric Le Goater                                                  listener);
6536b62a90cSCédric Le Goater     hwaddr iova, end;
6546b62a90cSCédric Le Goater     Int128 llend, llsize;
6556b62a90cSCédric Le Goater     int ret;
6566b62a90cSCédric Le Goater     bool try_unmap = true;
6576b62a90cSCédric Le Goater 
6586b62a90cSCédric Le Goater     if (!vfio_listener_valid_section(section, "region_del")) {
6596b62a90cSCédric Le Goater         return;
6606b62a90cSCédric Le Goater     }
6616b62a90cSCédric Le Goater 
6626b62a90cSCédric Le Goater     if (memory_region_is_iommu(section->mr)) {
6636b62a90cSCédric Le Goater         VFIOGuestIOMMU *giommu;
6646b62a90cSCédric Le Goater 
6656b62a90cSCédric Le Goater         trace_vfio_listener_region_del_iommu(section->mr->name);
6666b62a90cSCédric Le Goater         QLIST_FOREACH(giommu, &bcontainer->giommu_list, giommu_next) {
6676b62a90cSCédric Le Goater             if (MEMORY_REGION(giommu->iommu_mr) == section->mr &&
6686b62a90cSCédric Le Goater                 giommu->n.start == section->offset_within_region) {
6696b62a90cSCédric Le Goater                 memory_region_unregister_iommu_notifier(section->mr,
6706b62a90cSCédric Le Goater                                                         &giommu->n);
6716b62a90cSCédric Le Goater                 QLIST_REMOVE(giommu, giommu_next);
6726b62a90cSCédric Le Goater                 g_free(giommu);
6736b62a90cSCédric Le Goater                 break;
6746b62a90cSCédric Le Goater             }
6756b62a90cSCédric Le Goater         }
6766b62a90cSCédric Le Goater 
6776b62a90cSCédric Le Goater         /*
6786b62a90cSCédric Le Goater          * FIXME: We assume the one big unmap below is adequate to
6796b62a90cSCédric Le Goater          * remove any individual page mappings in the IOMMU which
6806b62a90cSCédric Le Goater          * might have been copied into VFIO. This works for a page table
6816b62a90cSCédric Le Goater          * based IOMMU where a big unmap flattens a large range of IO-PTEs.
6826b62a90cSCédric Le Goater          * That may not be true for all IOMMU types.
6836b62a90cSCédric Le Goater          */
6846b62a90cSCédric Le Goater     }
6856b62a90cSCédric Le Goater 
6866b62a90cSCédric Le Goater     if (!vfio_get_section_iova_range(bcontainer, section, &iova, &end,
6876b62a90cSCédric Le Goater                                      &llend)) {
6886b62a90cSCédric Le Goater         return;
6896b62a90cSCédric Le Goater     }
6906b62a90cSCédric Le Goater 
6916b62a90cSCédric Le Goater     llsize = int128_sub(llend, int128_make64(iova));
6926b62a90cSCédric Le Goater 
6936b62a90cSCédric Le Goater     trace_vfio_listener_region_del(iova, end);
6946b62a90cSCédric Le Goater 
6956b62a90cSCédric Le Goater     if (memory_region_is_ram_device(section->mr)) {
6966b62a90cSCédric Le Goater         hwaddr pgmask;
6976b62a90cSCédric Le Goater 
6986b62a90cSCédric Le Goater         pgmask = (1ULL << ctz64(bcontainer->pgsizes)) - 1;
6996b62a90cSCédric Le Goater         try_unmap = !((iova & pgmask) || (int128_get64(llsize) & pgmask));
7006b62a90cSCédric Le Goater     } else if (memory_region_has_ram_discard_manager(section->mr)) {
70174d37637SCédric Le Goater         vfio_ram_discard_unregister_listener(bcontainer, section);
7026b62a90cSCédric Le Goater         /* Unregistering will trigger an unmap. */
7036b62a90cSCédric Le Goater         try_unmap = false;
7046b62a90cSCédric Le Goater     }
7056b62a90cSCédric Le Goater 
7066b62a90cSCédric Le Goater     if (try_unmap) {
7079458d9b4SJohn Levon         bool unmap_all = false;
7089458d9b4SJohn Levon 
7096b62a90cSCédric Le Goater         if (int128_eq(llsize, int128_2_64())) {
7109458d9b4SJohn Levon             unmap_all = true;
7119458d9b4SJohn Levon             llsize = int128_zero();
7126b62a90cSCédric Le Goater         }
7139458d9b4SJohn Levon         ret = vfio_container_dma_unmap(bcontainer, iova, int128_get64(llsize),
7149458d9b4SJohn Levon                                        NULL, unmap_all);
7156b62a90cSCédric Le Goater         if (ret) {
7166b62a90cSCédric Le Goater             error_report("vfio_container_dma_unmap(%p, 0x%"HWADDR_PRIx", "
7176b62a90cSCédric Le Goater                          "0x%"HWADDR_PRIx") = %d (%s)",
7186b62a90cSCédric Le Goater                          bcontainer, iova, int128_get64(llsize), ret,
7196b62a90cSCédric Le Goater                          strerror(-ret));
7206b62a90cSCédric Le Goater         }
7216b62a90cSCédric Le Goater     }
7226b62a90cSCédric Le Goater 
7236b62a90cSCédric Le Goater     memory_region_unref(section->mr);
7246b62a90cSCédric Le Goater 
7256b62a90cSCédric Le Goater     /* PPC64/pseries machine only */
7266b62a90cSCédric Le Goater     vfio_container_del_section_window(bcontainer, section);
7276b62a90cSCédric Le Goater }
7286b62a90cSCédric Le Goater 
7296b62a90cSCédric Le Goater typedef struct VFIODirtyRanges {
7306b62a90cSCédric Le Goater     hwaddr min32;
7316b62a90cSCédric Le Goater     hwaddr max32;
7326b62a90cSCédric Le Goater     hwaddr min64;
7336b62a90cSCédric Le Goater     hwaddr max64;
7346b62a90cSCédric Le Goater     hwaddr minpci64;
7356b62a90cSCédric Le Goater     hwaddr maxpci64;
7366b62a90cSCédric Le Goater } VFIODirtyRanges;
7376b62a90cSCédric Le Goater 
7386b62a90cSCédric Le Goater typedef struct VFIODirtyRangesListener {
7396b62a90cSCédric Le Goater     VFIOContainerBase *bcontainer;
7406b62a90cSCédric Le Goater     VFIODirtyRanges ranges;
7416b62a90cSCédric Le Goater     MemoryListener listener;
7426b62a90cSCédric Le Goater } VFIODirtyRangesListener;
7436b62a90cSCédric Le Goater 
vfio_section_is_vfio_pci(MemoryRegionSection * section,VFIOContainerBase * bcontainer)7446b62a90cSCédric Le Goater static bool vfio_section_is_vfio_pci(MemoryRegionSection *section,
7456b62a90cSCédric Le Goater                                      VFIOContainerBase *bcontainer)
7466b62a90cSCédric Le Goater {
7476b62a90cSCédric Le Goater     VFIOPCIDevice *pcidev;
7486b62a90cSCédric Le Goater     VFIODevice *vbasedev;
7496b62a90cSCédric Le Goater     Object *owner;
7506b62a90cSCédric Le Goater 
7516b62a90cSCédric Le Goater     owner = memory_region_owner(section->mr);
7526b62a90cSCédric Le Goater 
7536b62a90cSCédric Le Goater     QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
7546b62a90cSCédric Le Goater         if (vbasedev->type != VFIO_DEVICE_TYPE_PCI) {
7556b62a90cSCédric Le Goater             continue;
7566b62a90cSCédric Le Goater         }
7576b62a90cSCédric Le Goater         pcidev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
7586b62a90cSCédric Le Goater         if (OBJECT(pcidev) == owner) {
7596b62a90cSCédric Le Goater             return true;
7606b62a90cSCédric Le Goater         }
7616b62a90cSCédric Le Goater     }
7626b62a90cSCédric Le Goater 
7636b62a90cSCédric Le Goater     return false;
7646b62a90cSCédric Le Goater }
7656b62a90cSCédric Le Goater 
vfio_dirty_tracking_update_range(VFIODirtyRanges * range,hwaddr iova,hwaddr end,bool update_pci)7666b62a90cSCédric Le Goater static void vfio_dirty_tracking_update_range(VFIODirtyRanges *range,
7676b62a90cSCédric Le Goater                                              hwaddr iova, hwaddr end,
7686b62a90cSCédric Le Goater                                              bool update_pci)
7696b62a90cSCédric Le Goater {
7706b62a90cSCédric Le Goater     hwaddr *min, *max;
7716b62a90cSCédric Le Goater 
7726b62a90cSCédric Le Goater     /*
7736b62a90cSCédric Le Goater      * The address space passed to the dirty tracker is reduced to three ranges:
7746b62a90cSCédric Le Goater      * one for 32-bit DMA ranges, one for 64-bit DMA ranges and one for the
7756b62a90cSCédric Le Goater      * PCI 64-bit hole.
7766b62a90cSCédric Le Goater      *
7776b62a90cSCédric Le Goater      * The underlying reports of dirty will query a sub-interval of each of
7786b62a90cSCédric Le Goater      * these ranges.
7796b62a90cSCédric Le Goater      *
7806b62a90cSCédric Le Goater      * The purpose of the three range handling is to handle known cases of big
7816b62a90cSCédric Le Goater      * holes in the address space, like the x86 AMD 1T hole, and firmware (like
7826b62a90cSCédric Le Goater      * OVMF) which may relocate the pci-hole64 to the end of the address space.
7836b62a90cSCédric Le Goater      * The latter would otherwise generate large ranges for tracking, stressing
7846b62a90cSCédric Le Goater      * the limits of supported hardware. The pci-hole32 will always be below 4G
7856b62a90cSCédric Le Goater      * (overlapping or not) so it doesn't need special handling and is part of
7866b62a90cSCédric Le Goater      * the 32-bit range.
7876b62a90cSCédric Le Goater      *
7886b62a90cSCédric Le Goater      * The alternative would be an IOVATree but that has a much bigger runtime
7896b62a90cSCédric Le Goater      * overhead and unnecessary complexity.
7906b62a90cSCédric Le Goater      */
7916b62a90cSCédric Le Goater     if (update_pci && iova >= UINT32_MAX) {
7926b62a90cSCédric Le Goater         min = &range->minpci64;
7936b62a90cSCédric Le Goater         max = &range->maxpci64;
7946b62a90cSCédric Le Goater     } else {
7956b62a90cSCédric Le Goater         min = (end <= UINT32_MAX) ? &range->min32 : &range->min64;
7966b62a90cSCédric Le Goater         max = (end <= UINT32_MAX) ? &range->max32 : &range->max64;
7976b62a90cSCédric Le Goater     }
7986b62a90cSCédric Le Goater     if (*min > iova) {
7996b62a90cSCédric Le Goater         *min = iova;
8006b62a90cSCédric Le Goater     }
8016b62a90cSCédric Le Goater     if (*max < end) {
8026b62a90cSCédric Le Goater         *max = end;
8036b62a90cSCédric Le Goater     }
8046b62a90cSCédric Le Goater 
8056b62a90cSCédric Le Goater     trace_vfio_device_dirty_tracking_update(iova, end, *min, *max);
8066b62a90cSCédric Le Goater }
8076b62a90cSCédric Le Goater 
vfio_dirty_tracking_update(MemoryListener * listener,MemoryRegionSection * section)8086b62a90cSCédric Le Goater static void vfio_dirty_tracking_update(MemoryListener *listener,
8096b62a90cSCédric Le Goater                                        MemoryRegionSection *section)
8106b62a90cSCédric Le Goater {
8116b62a90cSCédric Le Goater     VFIODirtyRangesListener *dirty =
8126b62a90cSCédric Le Goater         container_of(listener, VFIODirtyRangesListener, listener);
8136b62a90cSCédric Le Goater     hwaddr iova, end;
8146b62a90cSCédric Le Goater 
8156b62a90cSCédric Le Goater     if (!vfio_listener_valid_section(section, "tracking_update") ||
8166b62a90cSCédric Le Goater         !vfio_get_section_iova_range(dirty->bcontainer, section,
8176b62a90cSCédric Le Goater                                      &iova, &end, NULL)) {
8186b62a90cSCédric Le Goater         return;
8196b62a90cSCédric Le Goater     }
8206b62a90cSCédric Le Goater 
8216b62a90cSCédric Le Goater     vfio_dirty_tracking_update_range(&dirty->ranges, iova, end,
8226b62a90cSCédric Le Goater                       vfio_section_is_vfio_pci(section, dirty->bcontainer));
8236b62a90cSCédric Le Goater }
8246b62a90cSCédric Le Goater 
8256b62a90cSCédric Le Goater static const MemoryListener vfio_dirty_tracking_listener = {
8266b62a90cSCédric Le Goater     .name = "vfio-tracking",
8276b62a90cSCédric Le Goater     .region_add = vfio_dirty_tracking_update,
8286b62a90cSCédric Le Goater };
8296b62a90cSCédric Le Goater 
vfio_dirty_tracking_init(VFIOContainerBase * bcontainer,VFIODirtyRanges * ranges)8306b62a90cSCédric Le Goater static void vfio_dirty_tracking_init(VFIOContainerBase *bcontainer,
8316b62a90cSCédric Le Goater                                      VFIODirtyRanges *ranges)
8326b62a90cSCédric Le Goater {
8336b62a90cSCédric Le Goater     VFIODirtyRangesListener dirty;
8346b62a90cSCédric Le Goater 
8356b62a90cSCédric Le Goater     memset(&dirty, 0, sizeof(dirty));
8366b62a90cSCédric Le Goater     dirty.ranges.min32 = UINT32_MAX;
8376b62a90cSCédric Le Goater     dirty.ranges.min64 = UINT64_MAX;
8386b62a90cSCédric Le Goater     dirty.ranges.minpci64 = UINT64_MAX;
8396b62a90cSCédric Le Goater     dirty.listener = vfio_dirty_tracking_listener;
8406b62a90cSCédric Le Goater     dirty.bcontainer = bcontainer;
8416b62a90cSCédric Le Goater 
8426b62a90cSCédric Le Goater     memory_listener_register(&dirty.listener,
8436b62a90cSCédric Le Goater                              bcontainer->space->as);
8446b62a90cSCédric Le Goater 
8456b62a90cSCédric Le Goater     *ranges = dirty.ranges;
8466b62a90cSCédric Le Goater 
8476b62a90cSCédric Le Goater     /*
8486b62a90cSCédric Le Goater      * The memory listener is synchronous, and used to calculate the range
8496b62a90cSCédric Le Goater      * to dirty tracking. Unregister it after we are done as we are not
8506b62a90cSCédric Le Goater      * interested in any follow-up updates.
8516b62a90cSCédric Le Goater      */
8526b62a90cSCédric Le Goater     memory_listener_unregister(&dirty.listener);
8536b62a90cSCédric Le Goater }
8546b62a90cSCédric Le Goater 
vfio_devices_dma_logging_stop(VFIOContainerBase * bcontainer)8556b62a90cSCédric Le Goater static void vfio_devices_dma_logging_stop(VFIOContainerBase *bcontainer)
8566b62a90cSCédric Le Goater {
8576b62a90cSCédric Le Goater     uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature),
8586b62a90cSCédric Le Goater                               sizeof(uint64_t))] = {};
8596b62a90cSCédric Le Goater     struct vfio_device_feature *feature = (struct vfio_device_feature *)buf;
8606b62a90cSCédric Le Goater     VFIODevice *vbasedev;
8616b62a90cSCédric Le Goater 
8626b62a90cSCédric Le Goater     feature->argsz = sizeof(buf);
8636b62a90cSCédric Le Goater     feature->flags = VFIO_DEVICE_FEATURE_SET |
8646b62a90cSCédric Le Goater                      VFIO_DEVICE_FEATURE_DMA_LOGGING_STOP;
8656b62a90cSCédric Le Goater 
8666b62a90cSCédric Le Goater     QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
86738bf025dSJohn Levon         int ret;
86838bf025dSJohn Levon 
8696b62a90cSCédric Le Goater         if (!vbasedev->dirty_tracking) {
8706b62a90cSCédric Le Goater             continue;
8716b62a90cSCédric Le Goater         }
8726b62a90cSCédric Le Goater 
87338bf025dSJohn Levon         ret = vbasedev->io_ops->device_feature(vbasedev, feature);
87438bf025dSJohn Levon 
87538bf025dSJohn Levon         if (ret != 0) {
8766b62a90cSCédric Le Goater             warn_report("%s: Failed to stop DMA logging, err %d (%s)",
87738bf025dSJohn Levon                         vbasedev->name, -ret, strerror(-ret));
8786b62a90cSCédric Le Goater         }
8796b62a90cSCédric Le Goater         vbasedev->dirty_tracking = false;
8806b62a90cSCédric Le Goater     }
8816b62a90cSCédric Le Goater }
8826b62a90cSCédric Le Goater 
8836b62a90cSCédric Le Goater static struct vfio_device_feature *
vfio_device_feature_dma_logging_start_create(VFIOContainerBase * bcontainer,VFIODirtyRanges * tracking)8846b62a90cSCédric Le Goater vfio_device_feature_dma_logging_start_create(VFIOContainerBase *bcontainer,
8856b62a90cSCédric Le Goater                                              VFIODirtyRanges *tracking)
8866b62a90cSCédric Le Goater {
8876b62a90cSCédric Le Goater     struct vfio_device_feature *feature;
8886b62a90cSCédric Le Goater     size_t feature_size;
8896b62a90cSCédric Le Goater     struct vfio_device_feature_dma_logging_control *control;
8906b62a90cSCédric Le Goater     struct vfio_device_feature_dma_logging_range *ranges;
8916b62a90cSCédric Le Goater 
8926b62a90cSCédric Le Goater     feature_size = sizeof(struct vfio_device_feature) +
8936b62a90cSCédric Le Goater                    sizeof(struct vfio_device_feature_dma_logging_control);
8946b62a90cSCédric Le Goater     feature = g_try_malloc0(feature_size);
8956b62a90cSCédric Le Goater     if (!feature) {
8966b62a90cSCédric Le Goater         errno = ENOMEM;
8976b62a90cSCédric Le Goater         return NULL;
8986b62a90cSCédric Le Goater     }
8996b62a90cSCédric Le Goater     feature->argsz = feature_size;
9006b62a90cSCédric Le Goater     feature->flags = VFIO_DEVICE_FEATURE_SET |
9016b62a90cSCédric Le Goater                      VFIO_DEVICE_FEATURE_DMA_LOGGING_START;
9026b62a90cSCédric Le Goater 
9036b62a90cSCédric Le Goater     control = (struct vfio_device_feature_dma_logging_control *)feature->data;
9046b62a90cSCédric Le Goater     control->page_size = qemu_real_host_page_size();
9056b62a90cSCédric Le Goater 
9066b62a90cSCédric Le Goater     /*
9076b62a90cSCédric Le Goater      * DMA logging uAPI guarantees to support at least a number of ranges that
9086b62a90cSCédric Le Goater      * fits into a single host kernel base page.
9096b62a90cSCédric Le Goater      */
9106b62a90cSCédric Le Goater     control->num_ranges = !!tracking->max32 + !!tracking->max64 +
9116b62a90cSCédric Le Goater         !!tracking->maxpci64;
9126b62a90cSCédric Le Goater     ranges = g_try_new0(struct vfio_device_feature_dma_logging_range,
9136b62a90cSCédric Le Goater                         control->num_ranges);
9146b62a90cSCédric Le Goater     if (!ranges) {
9156b62a90cSCédric Le Goater         g_free(feature);
9166b62a90cSCédric Le Goater         errno = ENOMEM;
9176b62a90cSCédric Le Goater 
9186b62a90cSCédric Le Goater         return NULL;
9196b62a90cSCédric Le Goater     }
9206b62a90cSCédric Le Goater 
9216b62a90cSCédric Le Goater     control->ranges = (uintptr_t)ranges;
9226b62a90cSCédric Le Goater     if (tracking->max32) {
9236b62a90cSCédric Le Goater         ranges->iova = tracking->min32;
9246b62a90cSCédric Le Goater         ranges->length = (tracking->max32 - tracking->min32) + 1;
9256b62a90cSCédric Le Goater         ranges++;
9266b62a90cSCédric Le Goater     }
9276b62a90cSCédric Le Goater     if (tracking->max64) {
9286b62a90cSCédric Le Goater         ranges->iova = tracking->min64;
9296b62a90cSCédric Le Goater         ranges->length = (tracking->max64 - tracking->min64) + 1;
9306b62a90cSCédric Le Goater         ranges++;
9316b62a90cSCédric Le Goater     }
9326b62a90cSCédric Le Goater     if (tracking->maxpci64) {
9336b62a90cSCédric Le Goater         ranges->iova = tracking->minpci64;
9346b62a90cSCédric Le Goater         ranges->length = (tracking->maxpci64 - tracking->minpci64) + 1;
9356b62a90cSCédric Le Goater     }
9366b62a90cSCédric Le Goater 
9376b62a90cSCédric Le Goater     trace_vfio_device_dirty_tracking_start(control->num_ranges,
9386b62a90cSCédric Le Goater                                            tracking->min32, tracking->max32,
9396b62a90cSCédric Le Goater                                            tracking->min64, tracking->max64,
9406b62a90cSCédric Le Goater                                            tracking->minpci64, tracking->maxpci64);
9416b62a90cSCédric Le Goater 
9426b62a90cSCédric Le Goater     return feature;
9436b62a90cSCédric Le Goater }
9446b62a90cSCédric Le Goater 
vfio_device_feature_dma_logging_start_destroy(struct vfio_device_feature * feature)9456b62a90cSCédric Le Goater static void vfio_device_feature_dma_logging_start_destroy(
9466b62a90cSCédric Le Goater     struct vfio_device_feature *feature)
9476b62a90cSCédric Le Goater {
9486b62a90cSCédric Le Goater     struct vfio_device_feature_dma_logging_control *control =
9496b62a90cSCédric Le Goater         (struct vfio_device_feature_dma_logging_control *)feature->data;
9506b62a90cSCédric Le Goater     struct vfio_device_feature_dma_logging_range *ranges =
9516b62a90cSCédric Le Goater         (struct vfio_device_feature_dma_logging_range *)(uintptr_t)control->ranges;
9526b62a90cSCédric Le Goater 
9536b62a90cSCédric Le Goater     g_free(ranges);
9546b62a90cSCédric Le Goater     g_free(feature);
9556b62a90cSCédric Le Goater }
9566b62a90cSCédric Le Goater 
vfio_devices_dma_logging_start(VFIOContainerBase * bcontainer,Error ** errp)9576b62a90cSCédric Le Goater static bool vfio_devices_dma_logging_start(VFIOContainerBase *bcontainer,
9586b62a90cSCédric Le Goater                                           Error **errp)
9596b62a90cSCédric Le Goater {
9606b62a90cSCédric Le Goater     struct vfio_device_feature *feature;
9616b62a90cSCédric Le Goater     VFIODirtyRanges ranges;
9626b62a90cSCédric Le Goater     VFIODevice *vbasedev;
9636b62a90cSCédric Le Goater     int ret = 0;
9646b62a90cSCédric Le Goater 
9656b62a90cSCédric Le Goater     vfio_dirty_tracking_init(bcontainer, &ranges);
9666b62a90cSCédric Le Goater     feature = vfio_device_feature_dma_logging_start_create(bcontainer,
9676b62a90cSCédric Le Goater                                                            &ranges);
9686b62a90cSCédric Le Goater     if (!feature) {
9696b62a90cSCédric Le Goater         error_setg_errno(errp, errno, "Failed to prepare DMA logging");
9706b62a90cSCédric Le Goater         return false;
9716b62a90cSCédric Le Goater     }
9726b62a90cSCédric Le Goater 
9736b62a90cSCédric Le Goater     QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
9746b62a90cSCédric Le Goater         if (vbasedev->dirty_tracking) {
9756b62a90cSCédric Le Goater             continue;
9766b62a90cSCédric Le Goater         }
9776b62a90cSCédric Le Goater 
97838bf025dSJohn Levon         ret = vbasedev->io_ops->device_feature(vbasedev, feature);
9796b62a90cSCédric Le Goater         if (ret) {
98038bf025dSJohn Levon             error_setg_errno(errp, -ret, "%s: Failed to start DMA logging",
9816b62a90cSCédric Le Goater                              vbasedev->name);
9826b62a90cSCédric Le Goater             goto out;
9836b62a90cSCédric Le Goater         }
9846b62a90cSCédric Le Goater         vbasedev->dirty_tracking = true;
9856b62a90cSCédric Le Goater     }
9866b62a90cSCédric Le Goater 
9876b62a90cSCédric Le Goater out:
9886b62a90cSCédric Le Goater     if (ret) {
9896b62a90cSCédric Le Goater         vfio_devices_dma_logging_stop(bcontainer);
9906b62a90cSCédric Le Goater     }
9916b62a90cSCédric Le Goater 
9926b62a90cSCédric Le Goater     vfio_device_feature_dma_logging_start_destroy(feature);
9936b62a90cSCédric Le Goater 
9946b62a90cSCédric Le Goater     return ret == 0;
9956b62a90cSCédric Le Goater }
9966b62a90cSCédric Le Goater 
vfio_listener_log_global_start(MemoryListener * listener,Error ** errp)9976b62a90cSCédric Le Goater static bool vfio_listener_log_global_start(MemoryListener *listener,
9986b62a90cSCédric Le Goater                                            Error **errp)
9996b62a90cSCédric Le Goater {
10006b62a90cSCédric Le Goater     ERRP_GUARD();
10016b62a90cSCédric Le Goater     VFIOContainerBase *bcontainer = container_of(listener, VFIOContainerBase,
10026b62a90cSCédric Le Goater                                                  listener);
10036b62a90cSCédric Le Goater     bool ret;
10046b62a90cSCédric Le Goater 
10056b62a90cSCédric Le Goater     if (vfio_container_devices_dirty_tracking_is_supported(bcontainer)) {
10066b62a90cSCédric Le Goater         ret = vfio_devices_dma_logging_start(bcontainer, errp);
10076b62a90cSCédric Le Goater     } else {
10086b62a90cSCédric Le Goater         ret = vfio_container_set_dirty_page_tracking(bcontainer, true, errp) == 0;
10096b62a90cSCédric Le Goater     }
10106b62a90cSCédric Le Goater 
10116b62a90cSCédric Le Goater     if (!ret) {
10126b62a90cSCédric Le Goater         error_prepend(errp, "vfio: Could not start dirty page tracking - ");
10136b62a90cSCédric Le Goater     }
10146b62a90cSCédric Le Goater     return ret;
10156b62a90cSCédric Le Goater }
10166b62a90cSCédric Le Goater 
vfio_listener_log_global_stop(MemoryListener * listener)10176b62a90cSCédric Le Goater static void vfio_listener_log_global_stop(MemoryListener *listener)
10186b62a90cSCédric Le Goater {
10196b62a90cSCédric Le Goater     VFIOContainerBase *bcontainer = container_of(listener, VFIOContainerBase,
10206b62a90cSCédric Le Goater                                                  listener);
10216b62a90cSCédric Le Goater     Error *local_err = NULL;
10226b62a90cSCédric Le Goater     int ret = 0;
10236b62a90cSCédric Le Goater 
10246b62a90cSCédric Le Goater     if (vfio_container_devices_dirty_tracking_is_supported(bcontainer)) {
10256b62a90cSCédric Le Goater         vfio_devices_dma_logging_stop(bcontainer);
10266b62a90cSCédric Le Goater     } else {
10276b62a90cSCédric Le Goater         ret = vfio_container_set_dirty_page_tracking(bcontainer, false,
10286b62a90cSCédric Le Goater                                                      &local_err);
10296b62a90cSCédric Le Goater     }
10306b62a90cSCédric Le Goater 
10316b62a90cSCédric Le Goater     if (ret) {
10326b62a90cSCédric Le Goater         error_prepend(&local_err,
10336b62a90cSCédric Le Goater                       "vfio: Could not stop dirty page tracking - ");
10346b62a90cSCédric Le Goater         if (migration_is_running()) {
10356b62a90cSCédric Le Goater             migration_file_set_error(ret, local_err);
10366b62a90cSCédric Le Goater         } else {
10376b62a90cSCédric Le Goater             error_report_err(local_err);
10386b62a90cSCédric Le Goater         }
10396b62a90cSCédric Le Goater     }
10406b62a90cSCédric Le Goater }
10416b62a90cSCédric Le Goater 
10426b62a90cSCédric Le Goater typedef struct {
10436b62a90cSCédric Le Goater     IOMMUNotifier n;
10446b62a90cSCédric Le Goater     VFIOGuestIOMMU *giommu;
10456b62a90cSCédric Le Goater } vfio_giommu_dirty_notifier;
10466b62a90cSCédric Le Goater 
vfio_iommu_map_dirty_notify(IOMMUNotifier * n,IOMMUTLBEntry * iotlb)10476b62a90cSCédric Le Goater static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
10486b62a90cSCédric Le Goater {
10496b62a90cSCédric Le Goater     vfio_giommu_dirty_notifier *gdn = container_of(n,
10506b62a90cSCédric Le Goater                                                 vfio_giommu_dirty_notifier, n);
10516b62a90cSCédric Le Goater     VFIOGuestIOMMU *giommu = gdn->giommu;
10526b62a90cSCédric Le Goater     VFIOContainerBase *bcontainer = giommu->bcontainer;
10536b62a90cSCédric Le Goater     hwaddr iova = iotlb->iova + giommu->iommu_offset;
10546b62a90cSCédric Le Goater     ram_addr_t translated_addr;
10556b62a90cSCédric Le Goater     Error *local_err = NULL;
10566b62a90cSCédric Le Goater     int ret = -EINVAL;
1057e3353d63SSteve Sistare     MemoryRegion *mr;
1058e3353d63SSteve Sistare     hwaddr xlat;
10596b62a90cSCédric Le Goater 
10606b62a90cSCédric Le Goater     trace_vfio_iommu_map_dirty_notify(iova, iova + iotlb->addr_mask);
10616b62a90cSCédric Le Goater 
10626b62a90cSCédric Le Goater     if (iotlb->target_as != &address_space_memory) {
10636b62a90cSCédric Le Goater         error_setg(&local_err,
10646b62a90cSCédric Le Goater                    "Wrong target AS \"%s\", only system memory is allowed",
10656b62a90cSCédric Le Goater                    iotlb->target_as->name ? iotlb->target_as->name : "none");
10666b62a90cSCédric Le Goater         goto out;
10676b62a90cSCédric Le Goater     }
10686b62a90cSCédric Le Goater 
10696b62a90cSCédric Le Goater     rcu_read_lock();
1070e3353d63SSteve Sistare     mr = vfio_translate_iotlb(iotlb, &xlat, &local_err);
1071e3353d63SSteve Sistare     if (!mr) {
10726b62a90cSCédric Le Goater         goto out_unlock;
10736b62a90cSCédric Le Goater     }
1074e3353d63SSteve Sistare     translated_addr = memory_region_get_ram_addr(mr) + xlat;
10756b62a90cSCédric Le Goater 
10766b62a90cSCédric Le Goater     ret = vfio_container_query_dirty_bitmap(bcontainer, iova, iotlb->addr_mask + 1,
10776b62a90cSCédric Le Goater                                 translated_addr, &local_err);
10786b62a90cSCédric Le Goater     if (ret) {
10796b62a90cSCédric Le Goater         error_prepend(&local_err,
10806b62a90cSCédric Le Goater                       "vfio_iommu_map_dirty_notify(%p, 0x%"HWADDR_PRIx", "
10816b62a90cSCédric Le Goater                       "0x%"HWADDR_PRIx") failed - ", bcontainer, iova,
10826b62a90cSCédric Le Goater                       iotlb->addr_mask + 1);
10836b62a90cSCédric Le Goater     }
10846b62a90cSCédric Le Goater 
10856b62a90cSCédric Le Goater out_unlock:
10866b62a90cSCédric Le Goater     rcu_read_unlock();
10876b62a90cSCédric Le Goater 
10886b62a90cSCédric Le Goater out:
10896b62a90cSCédric Le Goater     if (ret) {
10906b62a90cSCédric Le Goater         if (migration_is_running()) {
10916b62a90cSCédric Le Goater             migration_file_set_error(ret, local_err);
10926b62a90cSCédric Le Goater         } else {
10936b62a90cSCédric Le Goater             error_report_err(local_err);
10946b62a90cSCédric Le Goater         }
10956b62a90cSCédric Le Goater     }
10966b62a90cSCédric Le Goater }
10976b62a90cSCédric Le Goater 
vfio_ram_discard_query_dirty_bitmap(MemoryRegionSection * section,void * opaque)109874d37637SCédric Le Goater static int vfio_ram_discard_query_dirty_bitmap(MemoryRegionSection *section,
10996b62a90cSCédric Le Goater                                              void *opaque)
11006b62a90cSCédric Le Goater {
11016b62a90cSCédric Le Goater     const hwaddr size = int128_get64(section->size);
11026b62a90cSCédric Le Goater     const hwaddr iova = section->offset_within_address_space;
11036b62a90cSCédric Le Goater     const ram_addr_t ram_addr = memory_region_get_ram_addr(section->mr) +
11046b62a90cSCédric Le Goater                                 section->offset_within_region;
11056b62a90cSCédric Le Goater     VFIORamDiscardListener *vrdl = opaque;
11066b62a90cSCédric Le Goater     Error *local_err = NULL;
11076b62a90cSCédric Le Goater     int ret;
11086b62a90cSCédric Le Goater 
11096b62a90cSCédric Le Goater     /*
11106b62a90cSCédric Le Goater      * Sync the whole mapped region (spanning multiple individual mappings)
11116b62a90cSCédric Le Goater      * in one go.
11126b62a90cSCédric Le Goater      */
11136b62a90cSCédric Le Goater     ret = vfio_container_query_dirty_bitmap(vrdl->bcontainer, iova, size, ram_addr,
11146b62a90cSCédric Le Goater                                 &local_err);
11156b62a90cSCédric Le Goater     if (ret) {
11166b62a90cSCédric Le Goater         error_report_err(local_err);
11176b62a90cSCédric Le Goater     }
11186b62a90cSCédric Le Goater     return ret;
11196b62a90cSCédric Le Goater }
11206b62a90cSCédric Le Goater 
11216b62a90cSCédric Le Goater static int
vfio_sync_ram_discard_listener_dirty_bitmap(VFIOContainerBase * bcontainer,MemoryRegionSection * section)11226b62a90cSCédric Le Goater vfio_sync_ram_discard_listener_dirty_bitmap(VFIOContainerBase *bcontainer,
11236b62a90cSCédric Le Goater                                             MemoryRegionSection *section)
11246b62a90cSCédric Le Goater {
11256b62a90cSCédric Le Goater     RamDiscardManager *rdm = memory_region_get_ram_discard_manager(section->mr);
11262372f8d9SSteve Sistare     VFIORamDiscardListener *vrdl =
11272372f8d9SSteve Sistare         vfio_find_ram_discard_listener(bcontainer, section);
11286b62a90cSCédric Le Goater 
11296b62a90cSCédric Le Goater     /*
11306b62a90cSCédric Le Goater      * We only want/can synchronize the bitmap for actually mapped parts -
11316b62a90cSCédric Le Goater      * which correspond to populated parts. Replay all populated parts.
11326b62a90cSCédric Le Goater      */
11336b62a90cSCédric Le Goater     return ram_discard_manager_replay_populated(rdm, section,
113474d37637SCédric Le Goater                                                 vfio_ram_discard_query_dirty_bitmap,
11356b62a90cSCédric Le Goater                                                 &vrdl);
11366b62a90cSCédric Le Goater }
11376b62a90cSCédric Le Goater 
vfio_sync_iommu_dirty_bitmap(VFIOContainerBase * bcontainer,MemoryRegionSection * section)11386b62a90cSCédric Le Goater static int vfio_sync_iommu_dirty_bitmap(VFIOContainerBase *bcontainer,
11396b62a90cSCédric Le Goater                                         MemoryRegionSection *section)
11406b62a90cSCédric Le Goater {
11416b62a90cSCédric Le Goater     VFIOGuestIOMMU *giommu;
11426b62a90cSCédric Le Goater     bool found = false;
11436b62a90cSCédric Le Goater     Int128 llend;
11446b62a90cSCédric Le Goater     vfio_giommu_dirty_notifier gdn;
11456b62a90cSCédric Le Goater     int idx;
11466b62a90cSCédric Le Goater 
11476b62a90cSCédric Le Goater     QLIST_FOREACH(giommu, &bcontainer->giommu_list, giommu_next) {
11486b62a90cSCédric Le Goater         if (MEMORY_REGION(giommu->iommu_mr) == section->mr &&
11496b62a90cSCédric Le Goater             giommu->n.start == section->offset_within_region) {
11506b62a90cSCédric Le Goater             found = true;
11516b62a90cSCédric Le Goater             break;
11526b62a90cSCédric Le Goater         }
11536b62a90cSCédric Le Goater     }
11546b62a90cSCédric Le Goater 
11556b62a90cSCédric Le Goater     if (!found) {
11566b62a90cSCédric Le Goater         return 0;
11576b62a90cSCédric Le Goater     }
11586b62a90cSCédric Le Goater 
11596b62a90cSCédric Le Goater     gdn.giommu = giommu;
11606b62a90cSCédric Le Goater     idx = memory_region_iommu_attrs_to_index(giommu->iommu_mr,
11616b62a90cSCédric Le Goater                                              MEMTXATTRS_UNSPECIFIED);
11626b62a90cSCédric Le Goater 
11636b62a90cSCédric Le Goater     llend = int128_add(int128_make64(section->offset_within_region),
11646b62a90cSCédric Le Goater                        section->size);
11656b62a90cSCédric Le Goater     llend = int128_sub(llend, int128_one());
11666b62a90cSCédric Le Goater 
11676b62a90cSCédric Le Goater     iommu_notifier_init(&gdn.n, vfio_iommu_map_dirty_notify, IOMMU_NOTIFIER_MAP,
11686b62a90cSCédric Le Goater                         section->offset_within_region, int128_get64(llend),
11696b62a90cSCédric Le Goater                         idx);
11706b62a90cSCédric Le Goater     memory_region_iommu_replay(giommu->iommu_mr, &gdn.n);
11716b62a90cSCédric Le Goater 
11726b62a90cSCédric Le Goater     return 0;
11736b62a90cSCédric Le Goater }
11746b62a90cSCédric Le Goater 
vfio_sync_dirty_bitmap(VFIOContainerBase * bcontainer,MemoryRegionSection * section,Error ** errp)11756b62a90cSCédric Le Goater static int vfio_sync_dirty_bitmap(VFIOContainerBase *bcontainer,
11766b62a90cSCédric Le Goater                                   MemoryRegionSection *section, Error **errp)
11776b62a90cSCédric Le Goater {
11786b62a90cSCédric Le Goater     ram_addr_t ram_addr;
11796b62a90cSCédric Le Goater 
11806b62a90cSCédric Le Goater     if (memory_region_is_iommu(section->mr)) {
11816b62a90cSCédric Le Goater         return vfio_sync_iommu_dirty_bitmap(bcontainer, section);
11826b62a90cSCédric Le Goater     } else if (memory_region_has_ram_discard_manager(section->mr)) {
11836b62a90cSCédric Le Goater         int ret;
11846b62a90cSCédric Le Goater 
11856b62a90cSCédric Le Goater         ret = vfio_sync_ram_discard_listener_dirty_bitmap(bcontainer, section);
11866b62a90cSCédric Le Goater         if (ret) {
11876b62a90cSCédric Le Goater             error_setg(errp,
11886b62a90cSCédric Le Goater                        "Failed to sync dirty bitmap with RAM discard listener");
11896b62a90cSCédric Le Goater         }
11906b62a90cSCédric Le Goater         return ret;
11916b62a90cSCédric Le Goater     }
11926b62a90cSCédric Le Goater 
11936b62a90cSCédric Le Goater     ram_addr = memory_region_get_ram_addr(section->mr) +
11946b62a90cSCédric Le Goater                section->offset_within_region;
11956b62a90cSCédric Le Goater 
11966b62a90cSCédric Le Goater     return vfio_container_query_dirty_bitmap(bcontainer,
11976b62a90cSCédric Le Goater                    REAL_HOST_PAGE_ALIGN(section->offset_within_address_space),
11986b62a90cSCédric Le Goater                                  int128_get64(section->size), ram_addr, errp);
11996b62a90cSCédric Le Goater }
12006b62a90cSCédric Le Goater 
vfio_listener_log_sync(MemoryListener * listener,MemoryRegionSection * section)12016b62a90cSCédric Le Goater static void vfio_listener_log_sync(MemoryListener *listener,
12026b62a90cSCédric Le Goater         MemoryRegionSection *section)
12036b62a90cSCédric Le Goater {
12046b62a90cSCédric Le Goater     VFIOContainerBase *bcontainer = container_of(listener, VFIOContainerBase,
12056b62a90cSCédric Le Goater                                                  listener);
12066b62a90cSCédric Le Goater     int ret;
12076b62a90cSCédric Le Goater     Error *local_err = NULL;
12086b62a90cSCédric Le Goater 
12096b62a90cSCédric Le Goater     if (vfio_listener_skipped_section(section)) {
12106b62a90cSCédric Le Goater         return;
12116b62a90cSCédric Le Goater     }
12126b62a90cSCédric Le Goater 
12136b62a90cSCédric Le Goater     if (vfio_log_sync_needed(bcontainer)) {
12146b62a90cSCédric Le Goater         ret = vfio_sync_dirty_bitmap(bcontainer, section, &local_err);
12156b62a90cSCédric Le Goater         if (ret) {
12166b62a90cSCédric Le Goater             if (migration_is_running()) {
12176b62a90cSCédric Le Goater                 migration_file_set_error(ret, local_err);
12186b62a90cSCédric Le Goater             } else {
12196b62a90cSCédric Le Goater                 error_report_err(local_err);
12206b62a90cSCédric Le Goater             }
12216b62a90cSCédric Le Goater         }
12226b62a90cSCédric Le Goater     }
12236b62a90cSCédric Le Goater }
12246b62a90cSCédric Le Goater 
1225a9183378SCédric Le Goater static const MemoryListener vfio_memory_listener = {
12266b62a90cSCédric Le Goater     .name = "vfio",
1227d9b7d8b6SJohn Levon     .begin = vfio_listener_begin,
1228d9b7d8b6SJohn Levon     .commit = vfio_listener_commit,
12296b62a90cSCédric Le Goater     .region_add = vfio_listener_region_add,
12306b62a90cSCédric Le Goater     .region_del = vfio_listener_region_del,
12316b62a90cSCédric Le Goater     .log_global_start = vfio_listener_log_global_start,
12326b62a90cSCédric Le Goater     .log_global_stop = vfio_listener_log_global_stop,
12336b62a90cSCédric Le Goater     .log_sync = vfio_listener_log_sync,
12346b62a90cSCédric Le Goater };
1235a9183378SCédric Le Goater 
vfio_listener_register(VFIOContainerBase * bcontainer,Error ** errp)1236a9183378SCédric Le Goater bool vfio_listener_register(VFIOContainerBase *bcontainer, Error **errp)
1237a9183378SCédric Le Goater {
1238a9183378SCédric Le Goater     bcontainer->listener = vfio_memory_listener;
1239a9183378SCédric Le Goater     memory_listener_register(&bcontainer->listener, bcontainer->space->as);
1240a9183378SCédric Le Goater 
1241a9183378SCédric Le Goater     if (bcontainer->error) {
1242a9183378SCédric Le Goater         error_propagate_prepend(errp, bcontainer->error,
1243a9183378SCédric Le Goater                                 "memory listener initialization failed: ");
1244a9183378SCédric Le Goater         return false;
1245a9183378SCédric Le Goater     }
1246a9183378SCédric Le Goater 
1247a9183378SCédric Le Goater     return true;
1248a9183378SCédric Le Goater }
1249a9183378SCédric Le Goater 
vfio_listener_unregister(VFIOContainerBase * bcontainer)1250a9183378SCédric Le Goater void vfio_listener_unregister(VFIOContainerBase *bcontainer)
1251a9183378SCédric Le Goater {
1252a9183378SCédric Le Goater     memory_listener_unregister(&bcontainer->listener);
1253a9183378SCédric Le Goater }
1254