Blame SOURCES/kvm-exec-extract-address_space_translate_iommu-fix-page_.patch

7711c0
From 2a1d22e7758fe0ae11050d24f489ce2d76b6e5dc Mon Sep 17 00:00:00 2001
7711c0
From: John Snow <jsnow@redhat.com>
7711c0
Date: Fri, 25 Jan 2019 22:50:03 +0100
7711c0
Subject: [PATCH 03/23] exec: extract address_space_translate_iommu, fix
7711c0
 page_mask corner case
7711c0
7711c0
RH-Author: John Snow <jsnow@redhat.com>
7711c0
Message-id: <20190125225007.8197-4-jsnow@redhat.com>
7711c0
Patchwork-id: 84116
7711c0
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH v2 3/7] exec: extract address_space_translate_iommu, fix page_mask corner case
7711c0
Bugzilla: 1597482
7711c0
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
7711c0
RH-Acked-by: Peter Xu <peterx@redhat.com>
7711c0
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
7711c0
7711c0
From: Paolo Bonzini <pbonzini@redhat.com>
7711c0
7711c0
This will be used to process IOMMUs in a MemoryRegionCache.  This
7711c0
includes a small bugfix, in that the returned page_mask is now
7711c0
correctly -1 if the IOMMU memory region maps the entire address
7711c0
space directly.  Previously, address_space_get_iotlb_entry would
7711c0
return ~TARGET_PAGE_MASK.
7711c0
7711c0
Reviewed-by: Peter Xu <peterx@redhat.com>
7711c0
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
7711c0
(cherry picked from commit a411c84b561baa94b28165c52f21c33517ee8f59)
7711c0
Signed-off-by: John Snow <jsnow@redhat.com>
7711c0
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
7711c0
---
7711c0
 exec.c | 110 ++++++++++++++++++++++++++++++++++++++++++++---------------------
7711c0
 1 file changed, 75 insertions(+), 35 deletions(-)
7711c0
7711c0
diff --git a/exec.c b/exec.c
7711c0
index c6aeded..1bd0e6c 100644
7711c0
--- a/exec.c
7711c0
+++ b/exec.c
7711c0
@@ -445,6 +445,70 @@ address_space_translate_internal(AddressSpaceDispatch *d, hwaddr addr, hwaddr *x
7711c0
 }
7711c0
 
7711c0
 /**
7711c0
+ * address_space_translate_iommu - translate an address through an IOMMU
7711c0
+ * memory region and then through the target address space.
7711c0
+ *
7711c0
+ * @iommu_mr: the IOMMU memory region that we start the translation from
7711c0
+ * @addr: the address to be translated through the MMU
7711c0
+ * @xlat: the translated address offset within the destination memory region.
7711c0
+ *        It cannot be %NULL.
7711c0
+ * @plen_out: valid read/write length of the translated address. It
7711c0
+ *            cannot be %NULL.
7711c0
+ * @page_mask_out: page mask for the translated address. This
7711c0
+ *            should only be meaningful for IOMMU translated
7711c0
+ *            addresses, since there may be huge pages that this bit
7711c0
+ *            would tell. It can be %NULL if we don't care about it.
7711c0
+ * @is_write: whether the translation operation is for write
7711c0
+ * @is_mmio: whether this can be MMIO, set true if it can
7711c0
+ * @target_as: the address space targeted by the IOMMU
7711c0
+ *
7711c0
+ * This function is called from RCU critical section.  It is the common
7711c0
+ * part of flatview_do_translate and address_space_translate_cached.
7711c0
+ */
7711c0
+static MemoryRegionSection address_space_translate_iommu(IOMMUMemoryRegion *iommu_mr,
7711c0
+                                                         hwaddr *xlat,
7711c0
+                                                         hwaddr *plen_out,
7711c0
+                                                         hwaddr *page_mask_out,
7711c0
+                                                         bool is_write,
7711c0
+                                                         bool is_mmio,
7711c0
+                                                         AddressSpace **target_as)
7711c0
+{
7711c0
+    MemoryRegionSection *section;
7711c0
+    hwaddr page_mask = (hwaddr)-1;
7711c0
+
7711c0
+    do {
7711c0
+        hwaddr addr = *xlat;
7711c0
+        IOMMUMemoryRegionClass *imrc = memory_region_get_iommu_class_nocheck(iommu_mr);
7711c0
+        IOMMUTLBEntry iotlb = imrc->translate(iommu_mr, addr, is_write ?
7711c0
+                                              IOMMU_WO : IOMMU_RO);
7711c0
+
7711c0
+        if (!(iotlb.perm & (1 << is_write))) {
7711c0
+            goto unassigned;
7711c0
+        }
7711c0
+
7711c0
+        addr = ((iotlb.translated_addr & ~iotlb.addr_mask)
7711c0
+                | (addr & iotlb.addr_mask));
7711c0
+        page_mask &= iotlb.addr_mask;
7711c0
+        *plen_out = MIN(*plen_out, (addr | iotlb.addr_mask) - addr + 1);
7711c0
+        *target_as = iotlb.target_as;
7711c0
+
7711c0
+        section = address_space_translate_internal(
7711c0
+                address_space_to_dispatch(iotlb.target_as), addr, xlat,
7711c0
+                plen_out, is_mmio);
7711c0
+
7711c0
+        iommu_mr = memory_region_get_iommu(section->mr);
7711c0
+    } while (unlikely(iommu_mr));
7711c0
+
7711c0
+    if (page_mask_out) {
7711c0
+        *page_mask_out = page_mask;
7711c0
+    }
7711c0
+    return *section;
7711c0
+
7711c0
+unassigned:
7711c0
+    return (MemoryRegionSection) { .mr = &io_mem_unassigned };
7711c0
+}
7711c0
+
7711c0
+/**
7711c0
  * flatview_do_translate - translate an address in FlatView
7711c0
  *
7711c0
  * @fv: the flat view that we want to translate on
7711c0
@@ -472,55 +536,31 @@ static MemoryRegionSection flatview_do_translate(FlatView *fv,
7711c0
                                                  bool is_mmio,
7711c0
                                                  AddressSpace **target_as)
7711c0
 {
7711c0
-    IOMMUTLBEntry iotlb;
7711c0
     MemoryRegionSection *section;
7711c0
     IOMMUMemoryRegion *iommu_mr;
7711c0
-    IOMMUMemoryRegionClass *imrc;
7711c0
-    hwaddr page_mask = (hwaddr)(-1);
7711c0
     hwaddr plen = (hwaddr)(-1);
7711c0
 
7711c0
     if (!plen_out) {
7711c0
         plen_out = &ple;;
7711c0
     }
7711c0
 
7711c0
-    for (;;) {
7711c0
-        section = address_space_translate_internal(
7711c0
-                flatview_to_dispatch(fv), addr, xlat,
7711c0
-                plen_out, is_mmio);
7711c0
-
7711c0
-        iommu_mr = memory_region_get_iommu(section->mr);
7711c0
-        if (!iommu_mr) {
7711c0
-            break;
7711c0
-        }
7711c0
-        imrc = memory_region_get_iommu_class_nocheck(iommu_mr);
7711c0
-
7711c0
-        addr = *xlat;
7711c0
-        iotlb = imrc->translate(iommu_mr, addr, is_write ?
7711c0
-                                IOMMU_WO : IOMMU_RO);
7711c0
-        if (!(iotlb.perm & (1 << is_write))) {
7711c0
-            goto translate_fail;
7711c0
-        }
7711c0
+    section = address_space_translate_internal(
7711c0
+            flatview_to_dispatch(fv), addr, xlat,
7711c0
+            plen_out, is_mmio);
7711c0
 
7711c0
-        addr = ((iotlb.translated_addr & ~iotlb.addr_mask)
7711c0
-                | (addr & iotlb.addr_mask));
7711c0
-        page_mask &= iotlb.addr_mask;
7711c0
-        *plen_out = MIN(*plen_out, (addr | iotlb.addr_mask) - addr + 1);
7711c0
-        fv = address_space_to_flatview(iotlb.target_as);
7711c0
-        *target_as = iotlb.target_as;
7711c0
+    iommu_mr = memory_region_get_iommu(section->mr);
7711c0
+    if (unlikely(iommu_mr)) {
7711c0
+        return address_space_translate_iommu(iommu_mr, xlat,
7711c0
+                                             plen_out, page_mask_out,
7711c0
+                                             is_write, is_mmio,
7711c0
+                                             target_as);
7711c0
     }
7711c0
-
7711c0
     if (page_mask_out) {
7711c0
-        if (page_mask == (hwaddr)(-1)) {
7711c0
-            /* Not behind an IOMMU, use default page size. */
7711c0
-            page_mask = ~TARGET_PAGE_MASK;
7711c0
-        }
7711c0
-        *page_mask_out = page_mask;
7711c0
+        /* Not behind an IOMMU, use default page size. */
7711c0
+        *page_mask_out = ~TARGET_PAGE_MASK;
7711c0
     }
7711c0
 
7711c0
     return *section;
7711c0
-
7711c0
-translate_fail:
7711c0
-    return (MemoryRegionSection) { .mr = &io_mem_unassigned };
7711c0
 }
7711c0
 
7711c0
 /* Called from RCU critical section */
7711c0
-- 
7711c0
1.8.3.1
7711c0