Skip to contentSkip to conversation

hw/riscv: fix IOTINVAL.GVMA for nested translation

PATCH New Dmitry Kokorev via qemu development 1 participant 1 message Nov 29 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/riscv: fix IOTINVAL.GVMA for nested translation

The IOTINVAL.GVMA command with GV=1 and AV=1 expects a Guest Physical Address to invalidate cache entries. But the IOATC cache only stores IOVA and the final physical address, not the GPA in between. So when QEMU tries to match the GPA from the command, it compares it against IOVA in the cache, which never matches. The cache entry stays valid, and DMA keeps using the old physical address even after the guest updates its G-stage page tables. This is issue #3574.

The fix adds a GPA field to the cache entry. For nested translation, the GPA is saved when switching from S-stage to G-stage. For G-stage only translation, the GPA is just the IOVA since they are the same. Now IOTINVAL.GVMA with GV=1 and AV=1 matches entries by GSCID and GPA instead of IOVA. The invalidation applies to both VG and VN tags, so both G-stage only and nested entries are cleared. IOTINVAL.VMA still uses IOVA as before.

A new test gvma-nested sets up nested translation, runs DMA to fill the cache, changes the G-stage page table so the same GPA points to a new physical address, sends IOTINVAL.GVMA with GV=1 and AV=1, then runs DMA again and checks that the new physical address is used.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3574

---
 hw/riscv/riscv-iommu.c         |  88 +++++++++++++-----------
 tests/qtest/iommu-riscv-test.c | 118 +++++++++++++++++++++++++++++++++
 2 files changed, 168 insertions(+), 38 deletions(-)
hw/riscv/riscv-iommu.c +50 -38
index 0e6970773a..74c5234bff 100644
@@ -56,19 +56,20 @@ struct RISCVIOMMUSpace {
 };
 
 typedef enum RISCVIOMMUTransTag {
-    RISCV_IOMMU_TRANS_TAG_BY,  /* Bypass */
-    RISCV_IOMMU_TRANS_TAG_SS,  /* Single Stage */
-    RISCV_IOMMU_TRANS_TAG_VG,  /* G-stage only */
-    RISCV_IOMMU_TRANS_TAG_VN,  /* Nested translation */
+    RISCV_IOMMU_TRANS_TAG_BY = 0,  /* Bypass */
+    RISCV_IOMMU_TRANS_TAG_SS = 1,  /* Single Stage */
+    RISCV_IOMMU_TRANS_TAG_VG = 2,  /* G-stage only */
+    RISCV_IOMMU_TRANS_TAG_VN = 3,  /* Nested translation */
 } RISCVIOMMUTransTag;
 
 /* Address translation cache entry */
 struct RISCVIOMMUEntry {
-    RISCVIOMMUTransTag tag;     /* Translation Tag */
-    uint64_t iova:44;           /* IOVA Page Number */
-    uint64_t pscid:20;          /* Process Soft-Context identifier */
+    RISCVIOMMUTransTag tag:2;   /* Translation Tag */
     uint64_t phys:44;           /* Physical Page Number */
     uint64_t gscid:16;          /* Guest Soft-Context identifier */
+    uint64_t iova:44;           /* IOVA Page Number */
+    uint64_t pscid:20;          /* Process Soft-Context identifier */
+    uint64_t gpa:44;            /* Guest Physical Page Number */
     uint64_t perm:2;            /* IOMMU_RW flags */
 };
 
@@ -279,7 +280,7 @@ static hwaddr riscv_iommu_napot_page_mask(hwaddr ppn, hwaddr addr, hwaddr *out)
  * @return   : success or fault cause code.
  */
 static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
-    IOMMUTLBEntry *iotlb)
+    IOMMUTLBEntry *iotlb, hwaddr *gpa)
 {
     IOMMUAccessFlags trans_perm = IOMMU_NONE;
     dma_addr_t addr, base;
@@ -563,6 +564,7 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
 
             /* Continue with G-Stage translation? */
             if (!pass && en_g) {
+                *gpa = PPN_DOWN(base);
                 pass = G_STAGE;
                 addr = base;
                 base = gatp;
@@ -1646,7 +1648,6 @@ static gboolean riscv_iommu_iot_inval_gscid_pscid(gpointer key,
 }
 
 /* GV: 1 AV: 1 PSCV: 0 GVMA: 0 */
-/* GV: 1 AV: 1 GVMA: 1 */
 static gboolean riscv_iommu_iot_inval_gscid_iova(gpointer key,
                                                  gpointer value,
                                                  gpointer data)
@@ -1658,6 +1659,18 @@ static gboolean riscv_iommu_iot_inval_gscid_iova(gpointer key,
            iot->iova == arg->iova;
 }
 
+/* GV: 1 AV: 1 GVMA: 1 */
+static gboolean riscv_iommu_iot_inval_gscid_gpa(gpointer key,
+                                                gpointer value,
+                                                gpointer data)
+{
+    RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;
+    RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data;
+    return iot->tag == arg->tag &&
+           iot->gscid == arg->gscid &&
+           iot->gpa == arg->gpa;
+}
+
 /* GV: 1 AV: 1 PSCV: 1 GVMA: 0 */
 static gboolean riscv_iommu_iot_inval_gscid_pscid_iova(gpointer key,
                                                        gpointer value,
@@ -1702,17 +1715,10 @@ static void riscv_iommu_iot_update(RISCVIOMMUState *s, RISCVIOMMUEntry *iot)
 }
 
 static void riscv_iommu_iot_inval(RISCVIOMMUState *s, GHRFunc func,
-    uint32_t gscid, uint32_t pscid, hwaddr iova, RISCVIOMMUTransTag transtag)
+                                  RISCVIOMMUEntry *key)
 {
-    RISCVIOMMUEntry key = {
-        .tag = transtag,
-        .gscid = gscid,
-        .pscid = pscid,
-        .iova  = PPN_DOWN(iova),
-    };
-
     qemu_mutex_lock(&s->iot_lock);
-    g_hash_table_foreach_remove(s->iot_cache, func, &key);
+    g_hash_table_foreach_remove(s->iot_cache, func, key);
     qemu_mutex_unlock(&s->iot_lock);
 }
 
@@ -1739,6 +1745,7 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
     bool enable_pid;
     bool enable_pri;
     hwaddr phys = 0;
+    hwaddr gpa = PPN_DOWN(iotlb->iova);
     int fault;
 
     riscv_iommu_hpm_incr_ctr(s, ctx, RISCV_IOMMU_HPMEVENT_URQ);
@@ -1776,7 +1783,7 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
     riscv_iommu_hpm_incr_ctr(s, ctx, RISCV_IOMMU_HPMEVENT_TLB_MISS);
 
     /* Translate using device directory / page table information. */
-    fault = riscv_iommu_spa_fetch(s, ctx, iotlb);
+    fault = riscv_iommu_spa_fetch(s, ctx, iotlb, &gpa);
 
     if (!fault && iotlb->target_as == &s->trap_as) {
         /* Do not cache trapped MSI translations */
@@ -1797,6 +1804,7 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
         iot->pscid = get_field(ctx->ta, RISCV_IOMMU_DC_TA_PSCID);
         iot->perm = iotlb->perm;
         iot->tag = transtag;
+        iot->gpa = gpa;
         qemu_mutex_lock(&s->iot_lock);
         riscv_iommu_iot_update(s, iot);
         qemu_mutex_unlock(&s->iot_lock);
@@ -2014,11 +2022,14 @@ static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
             bool gv = !!(cmd.dword0 & RISCV_IOMMU_CMD_IOTINVAL_GV);
             bool av = !!(cmd.dword0 & RISCV_IOMMU_CMD_IOTINVAL_AV);
             bool pscv = !!(cmd.dword0 & RISCV_IOMMU_CMD_IOTINVAL_PSCV);
-            uint32_t gscid = get_field(cmd.dword0,
-                                       RISCV_IOMMU_CMD_IOTINVAL_GSCID);
-            uint32_t pscid = get_field(cmd.dword0,
-                                       RISCV_IOMMU_CMD_IOTINVAL_PSCID);
-            hwaddr iova = (cmd.dword1 << 2) & TARGET_PAGE_MASK;
+            RISCVIOMMUEntry key = {
+                .tag = RISCV_IOMMU_TRANS_TAG_VG,
+                .gscid = get_field(cmd.dword0,
+                                   RISCV_IOMMU_CMD_IOTINVAL_GSCID),
+                .pscid = get_field(cmd.dword0,
+                                   RISCV_IOMMU_CMD_IOTINVAL_PSCID),
+                .gpa   = PPN_DOWN((cmd.dword1 << 2) & TARGET_PAGE_MASK),
+            };
 
             if (pscv) {
                 /* illegal command arguments IOTINVAL.GVMA & PSCV == 1 */
@@ -2028,15 +2039,15 @@ static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
             iot_func = riscv_iommu_iot_inval_all;
 
             if (gv) {
-                iot_func = (av) ? riscv_iommu_iot_inval_gscid_iova :
+                iot_func = (av) ? riscv_iommu_iot_inval_gscid_gpa :
                               riscv_iommu_iot_inval_gscid;
             }
 
-            riscv_iommu_iot_inval(
-                s, iot_func, gscid, pscid, iova, RISCV_IOMMU_TRANS_TAG_VG);
+            riscv_iommu_iot_inval(s, iot_func, &key);
+
+            key.tag = RISCV_IOMMU_TRANS_TAG_VN;
+            riscv_iommu_iot_inval(s, iot_func, &key);
 
-            riscv_iommu_iot_inval(
-                s, iot_func, gscid, pscid, iova, RISCV_IOMMU_TRANS_TAG_VN);
             break;
         }
 
@@ -2046,15 +2057,16 @@ static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
             bool gv = !!(cmd.dword0 & RISCV_IOMMU_CMD_IOTINVAL_GV);
             bool av = !!(cmd.dword0 & RISCV_IOMMU_CMD_IOTINVAL_AV);
             bool pscv = !!(cmd.dword0 & RISCV_IOMMU_CMD_IOTINVAL_PSCV);
-            uint32_t gscid = get_field(cmd.dword0,
-                                       RISCV_IOMMU_CMD_IOTINVAL_GSCID);
-            uint32_t pscid = get_field(cmd.dword0,
-                                       RISCV_IOMMU_CMD_IOTINVAL_PSCID);
-            hwaddr iova = (cmd.dword1 << 2) & TARGET_PAGE_MASK;
-            RISCVIOMMUTransTag transtag;
+            RISCVIOMMUEntry key = {
+                .gscid = get_field(cmd.dword0,
+                                   RISCV_IOMMU_CMD_IOTINVAL_GSCID),
+                .iova = PPN_DOWN((cmd.dword1 << 2) & TARGET_PAGE_MASK),
+                .pscid = get_field(cmd.dword0,
+                                   RISCV_IOMMU_CMD_IOTINVAL_PSCID),
+            };
 
             if (gv) {
-                transtag = RISCV_IOMMU_TRANS_TAG_VN;
+                key.tag = RISCV_IOMMU_TRANS_TAG_VN;
                 if (pscv) {
                     iot_func = (av) ? riscv_iommu_iot_inval_gscid_pscid_iova :
                                   riscv_iommu_iot_inval_gscid_pscid;
@@ -2063,7 +2075,7 @@ static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
                                   riscv_iommu_iot_inval_gscid;
                 }
             } else {
-                transtag = RISCV_IOMMU_TRANS_TAG_SS;
+                key.tag = RISCV_IOMMU_TRANS_TAG_SS;
                 if (pscv) {
                     iot_func = (av) ? riscv_iommu_iot_inval_pscid_iova :
                                   riscv_iommu_iot_inval_pscid;
@@ -2073,7 +2085,7 @@ static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
                 }
             }
 
-            riscv_iommu_iot_inval(s, iot_func, gscid, pscid, iova, transtag);
+            riscv_iommu_iot_inval(s, iot_func, &key);
             break;
         }
 
tests/qtest/iommu-riscv-test.c +118 -0

136 lines not on this page · raw message

Signature · 1 line
2.56.1