Skip to contentSkip to conversation

Defer the IOMMU translation in the CPU path and support access_type

PATCH In review Jim Shu 2 participants 8 messages May 6 – Oct 13 RISC-V

lore
Share & tools
lore

Jim Shu

author
#0 lore raw

v3 cover Defer the IOMMU translation in the CPU path and support access_type

Incoming security protection devices feature more complex IOMMUMemoryRegion implementation in the CPU path than ARM MPC device. For example, RISC-V wgChecker [1] may permit the access with only RO/WO permissions. Consequently, the IOMMUMemoryRegion could return different sections for read & write access.

To support such IOMMUMemoryRegion behavior in the CPU path, the design of IOMMU translation must be updated:

1. address_space_translate*() must now pass the access_type to IOMMUMemoryRegion.
2. Since IOMMU translation results are too complex to be fully stored in the CPU TLB. we will defer the translation until the actual access occurs. Also, TLB is allowed to store the untranslated IOMMU region.

To implement deferred IOMMU translation, this patchset introduces the following changes:

1. tlb_set_page_full() no longer translates the IOMMU region immediately. Instead, it stores the untranslated region directly in the TLB. A new slow-path flag, TLB_IOMMU, is introduced to force access into the slow path when a region has not yet been translated in the TLB entry.

2. When the CPU utilizes a TLB entry in the slow path, it should perform the lazy IOMMU translation of the access_type first. The resulting translated region and access type are stored in CPUTLBEntryFull. Since the slow path always performs lazy translation first, we can switch the CPUTLBEntryFull content to the correct access type before use.

3. To accelerate memory access in the fast path, lazy translation can update the addend of the CPUTLBEntry when translating the region to a host memory region. We restrict the IOMMU region to have a single non-zero 'addend' across all permissions. If a second 'addend' is present for a CPUTLBEntry, QEMU will trigger an assertion. This limitation is sufficient for security devices, as their "secondary" region is typically an IO region used to emulate device error handling when access is rejected.

4. To support non-slow TLB flags, lazy translation can update the TLB flags in the 'addr_idx' of the CPUTLBEntry. Lazy translation only updates the flags for the permissions specified in @prot. This ensures that each access_type of a translated region to maintains independent TLB flags. For example, TLB_DIRTY of memory region will not be "polluted" from other permission that translated to different region.

Both RISC-V wgChecker [1] and RISC-V IOPMP [2] devices require this feature.

[1] RISC-V WG:
https://patchew.org/QEMU/[email protected]/
[2] RISC-V IOPMP:
https://patchew.org/QEMU/[email protected]/

Changed since v2:
- Fix unused initialized memory error

Changed since v1:
- Remove the ping-pong TLB entry behavior. Instead, defer the IOMMU translation until actual access in the CPU path. Provide a IOMMU lazy translation function with the special handling of 'addend' and 'addr_idx' fields of CPUTLBEntry.
- Fix the checkpatch warning.

Jim Shu (5):
accel/tcg: Pass access_type as an argument of tlb_set_page*() accel/tcg: address_space_translate*() will pass the correct
iommu_flags
accel/tcg: Provide early AS translate function accel/tcg: Add IOMMU lazy translation function accel/tcg: Support IOMMU lazy translation in CPU TLB

 accel/tcg/cputlb.c                   | 247 +++++++++++++++++++++++++--
 include/accel/tcg/iommu.h            |  17 +-
 include/exec/cputlb.h                |  11 +-
 include/exec/tlb-flags.h             |   4 +-
 include/hw/core/cpu.h                |  15 ++
 system/physmem.c                     |  60 ++++++-
 target/alpha/helper.c                |   2 +-
 target/avr/helper.c                  |   3 +-
 target/i386/tcg/system/excp_helper.c |   3 +-
 target/loongarch/tcg/tlb_helper.c    |   2 +-
 target/m68k/helper.c                 |  10 +-
 target/microblaze/helper.c           |   8 +-
 target/mips/tcg/system/tlb_helper.c  |   4 +-
 target/or1k/mmu.c                    |   2 +-
 target/ppc/mmu_helper.c              |   2 +-
 target/riscv/cpu_helper.c            |   2 +-
 target/rx/cpu.c                      |   3 +-
 target/s390x/tcg/excp_helper.c       |   2 +-
 target/sh4/helper.c                  |   3 +-
 target/sparc/mmu_helper.c            |   6 +-
 target/tricore/helper.c              |   2 +-
 target/xtensa/helper.c               |   3 +-
 22 files changed, 354 insertions(+), 57 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 1/5 accel/tcg: Pass access_type as an argument of tlb_set_page*()

Pass the access_type so that we could know CPU will do the read or write access. Then, CPU can fill the CPUTLBEntry[Full] of the specific permission (@prot).

It is fine for address_space_translate*() to return different section of read and write access. tlb_set_page*() only sets 'CPUTLBEntry.addr_*' for specific @prot, so access from another @prot will only get TLB miss and start to overwrite 'CPUTLBEntry[Full]' with new @prot.

It is the preliminary patch of next commit to pass the iommu_flags to IOMMUMemoryRegion from access_type.

---
 accel/tcg/cputlb.c                   | 14 ++++++++------
 include/exec/cputlb.h                | 11 +++++++----
 target/alpha/helper.c                |  2 +-
 target/avr/helper.c                  |  3 ++-
 target/i386/tcg/system/excp_helper.c |  3 ++-
 target/loongarch/tcg/tlb_helper.c    |  2 +-
 target/m68k/helper.c                 | 10 +++++++---
 target/microblaze/helper.c           |  8 ++++----
 target/mips/tcg/system/tlb_helper.c  |  4 ++--
 target/or1k/mmu.c                    |  2 +-
 target/ppc/mmu_helper.c              |  2 +-
 target/riscv/cpu_helper.c            |  2 +-
 target/rx/cpu.c                      |  3 ++-
 target/s390x/tcg/excp_helper.c       |  2 +-
 target/sh4/helper.c                  |  3 ++-
 target/sparc/mmu_helper.c            |  6 +++---
 target/tricore/helper.c              |  2 +-
 target/xtensa/helper.c               |  3 ++-
 18 files changed, 48 insertions(+), 34 deletions(-)
accel/tcg/cputlb.c +8 -6
index d6115bbb0a4..3bc951603dc 100644
@@ -1022,7 +1022,8 @@ static inline void tlb_set_compare(CPUTLBEntryFull *full, CPUTLBEntry *ent,
  * critical section.
  */
 void tlb_set_page_full(CPUState *cpu, int mmu_idx,
-                       vaddr addr, CPUTLBEntryFull *full)
+                       vaddr addr, MMUAccessType access_type,
+                       CPUTLBEntryFull *full)
 {
     CPUTLB *tlb = &cpu->neg.tlb;
     CPUTLBDesc *desc = &tlb->d[mmu_idx];
@@ -1185,7 +1186,8 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx,
 
 void tlb_set_page_with_attrs(CPUState *cpu, vaddr addr,
                              hwaddr paddr, MemTxAttrs attrs, int prot,
-                             int mmu_idx, vaddr size)
+                             MMUAccessType access_type, int mmu_idx,
+                             vaddr size)
 {
     CPUTLBEntryFull full = {
         .phys_addr = paddr,
@@ -1195,15 +1197,15 @@ void tlb_set_page_with_attrs(CPUState *cpu, vaddr addr,
     };
 
     assert(is_power_of_2(size));
-    tlb_set_page_full(cpu, mmu_idx, addr, &full);
+    tlb_set_page_full(cpu, mmu_idx, addr, access_type, &full);
 }
 
 void tlb_set_page(CPUState *cpu, vaddr addr,
-                  hwaddr paddr, int prot,
+                  hwaddr paddr, int prot, MMUAccessType access_type,
                   int mmu_idx, vaddr size)
 {
     tlb_set_page_with_attrs(cpu, addr, paddr, MEMTXATTRS_UNSPECIFIED,
-                            prot, mmu_idx, size);
+                            prot, access_type, mmu_idx, size);
 }
 
 /**
@@ -1245,7 +1247,7 @@ static bool tlb_fill_align(CPUState *cpu, vaddr addr, MMUAccessType type,
     if (ops->tlb_fill_align) {
         if (ops->tlb_fill_align(cpu, &full, addr, type, mmu_idx,
                                 memop, size, probe, ra)) {
-            tlb_set_page_full(cpu, mmu_idx, addr, &full);
+            tlb_set_page_full(cpu, mmu_idx, addr, type, &full);
             return true;
         }
     } else {
include/exec/cputlb.h +7 -4
index 3a9603a6965..47fa4302a9a 100644
@@ -41,6 +41,7 @@ void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t length);
  * @cpu: CPU context
  * @mmu_idx: mmu index of the tlb to modify
  * @addr: virtual address of the entry to add
+ * @access_type: access was read/write/execute
  * @full: the details of the tlb entry
  *
  * Add an entry to @cpu tlb index @mmu_idx.  All of the fields of
@@ -56,6 +57,7 @@ void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t length);
  * used by tlb_flush_page.
  */
 void tlb_set_page_full(CPUState *cpu, int mmu_idx, vaddr addr,
+                       MMUAccessType access_type,
                        CPUTLBEntryFull *full);
 
 /**
@@ -65,6 +67,7 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, vaddr addr,
  * @paddr: physical address of the page
  * @attrs: memory transaction attributes
  * @prot: access permissions (PAGE_READ/PAGE_WRITE/PAGE_EXEC bits)
+ * @access_type: access was read/write/execute
  * @mmu_idx: MMU index to insert TLB entry for
  * @size: size of the page in bytes
  *
@@ -81,9 +84,9 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, vaddr addr,
  * used by tlb_flush_page.
  */
 void tlb_set_page_with_attrs(CPUState *cpu, vaddr addr,
-                             hwaddr paddr, MemTxAttrs attrs,
-                             int prot, int mmu_idx, vaddr size);
-
+                             hwaddr paddr, MemTxAttrs attrs, int prot,
+                             MMUAccessType access_type, int mmu_idx,
+                             vaddr size);
 /**
  * tlb_set_page:
  *
@@ -92,7 +95,7 @@ void tlb_set_page_with_attrs(CPUState *cpu, vaddr addr,
  * as a convenience for CPUs which don't use memory transaction attributes.
  */
 void tlb_set_page(CPUState *cpu, vaddr addr,
-                  hwaddr paddr, int prot,
+                  hwaddr paddr, int prot, MMUAccessType access_type,
                   int mmu_idx, vaddr size);
 
 #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
target/alpha/helper.c +1 -1
index 179dc2dc7ae..7645ff27e20 100644
@@ -328,7 +328,7 @@ bool alpha_cpu_tlb_fill(CPUState *cs, vaddr addr, int size,
     }
 
     tlb_set_page(cs, addr & TARGET_PAGE_MASK, phys & TARGET_PAGE_MASK,
-                 prot, mmu_idx, TARGET_PAGE_SIZE);
+                 prot, access_type, mmu_idx, TARGET_PAGE_SIZE);
     return true;
 }
 
target/avr/helper.c +2 -1
index 365c8c60e19..4f536f08676 100644
@@ -143,7 +143,8 @@ bool avr_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
         prot = PAGE_READ | PAGE_WRITE;
     }
 
-    tlb_set_page(cs, address, paddr, prot, mmu_idx, TARGET_PAGE_SIZE);
+    tlb_set_page(cs, address, paddr, prot, access_type, mmu_idx,
+                 TARGET_PAGE_SIZE);
     return true;
 }
 
target/i386/tcg/system/excp_helper.c +2 -1
index d7ea77c8558..0fdae83f0a2 100644
@@ -628,7 +628,8 @@ bool x86_cpu_tlb_fill(CPUState *cs, vaddr addr, int size,
         tlb_set_page_with_attrs(cs, addr & TARGET_PAGE_MASK,
                                 out.paddr & TARGET_PAGE_MASK,
                                 cpu_get_mem_attrs(env),
-                                out.prot, mmu_idx, out.page_size);
+                                out.prot, access_type, mmu_idx,
+                                out.page_size);
         return true;
     }
 
target/loongarch/tcg/tlb_helper.c +1 -1
index c0fd8527fe9..f84a62e2861 100644
@@ -669,7 +669,7 @@ bool loongarch_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
         prot = context.prot;
         tlb_set_page(cs, address & TARGET_PAGE_MASK,
                      physical & TARGET_PAGE_MASK, prot,
-                     mmu_idx, TARGET_PAGE_SIZE);
+                     access_type, mmu_idx, TARGET_PAGE_SIZE);
         qemu_log_mask(CPU_LOG_MMU,
                       "%s address=%" VADDR_PRIx " physical " HWADDR_FMT_plx
                       " prot %d\n", __func__, address, physical, prot);
target/m68k/helper.c +7 -3
index 9bab1843892..61048892adf 100644
@@ -969,7 +969,7 @@ bool m68k_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
         tlb_set_page(cs, address & TARGET_PAGE_MASK,
                      address & TARGET_PAGE_MASK,
                      PAGE_READ | PAGE_WRITE | PAGE_EXEC,
-                     mmu_idx, TARGET_PAGE_SIZE);
+                     qemu_access_type, mmu_idx, TARGET_PAGE_SIZE);
         return true;
     }
 
@@ -989,7 +989,8 @@ bool m68k_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
                                address, access_type, &page_size);
     if (likely(ret == 0)) {
         tlb_set_page(cs, address & TARGET_PAGE_MASK,
-                     physical & TARGET_PAGE_MASK, prot, mmu_idx, page_size);
+                     physical & TARGET_PAGE_MASK, prot, qemu_access_type,
+                     mmu_idx, page_size);
         return true;
     }
 
@@ -1461,6 +1462,7 @@ void HELPER(ptest)(CPUM68KState *env, uint32_t addr, uint32_t is_read)
     int prot;
     int ret;
     target_ulong page_size;
+    MMUAccessType qemu_access_type = MMU_DATA_LOAD;
 
     access_type = ACCESS_PTEST;
     if (env->dfc & 4) {
@@ -1468,9 +1470,11 @@ void HELPER(ptest)(CPUM68KState *env, uint32_t addr, uint32_t is_read)
     }
     if ((env->dfc & 3) == 2) {
         access_type |= ACCESS_CODE;
+        qemu_access_type = MMU_INST_FETCH;
     }
     if (!is_read) {
         access_type |= ACCESS_STORE;
+        qemu_access_type = MMU_DATA_STORE;
     }
 
     env->mmu.mmusr = 0;
@@ -1480,7 +1484,7 @@ void HELPER(ptest)(CPUM68KState *env, uint32_t addr, uint32_t is_read)
     if (ret == 0) {
         tlb_set_page(env_cpu(env), addr & TARGET_PAGE_MASK,
                      physical & TARGET_PAGE_MASK,
-                     prot, access_type & ACCESS_SUPER ?
+                     prot, qemu_access_type, access_type & ACCESS_SUPER ?
                      MMU_KERNEL_IDX : MMU_USER_IDX, page_size);
     }
 }
target/microblaze/helper.c +4 -4
index a1857b72172..2bdf8c3ea03 100644
@@ -101,8 +101,8 @@ bool mb_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
         /* MMU disabled or not available.  */
         address &= TARGET_PAGE_MASK;
         prot = PAGE_RWX;
-        tlb_set_page_with_attrs(cs, address, address, attrs, prot, mmu_idx,
-                                TARGET_PAGE_SIZE);
+        tlb_set_page_with_attrs(cs, address, address, attrs, prot, access_type,
+                                mmu_idx, TARGET_PAGE_SIZE);
         return true;
     }
 
@@ -113,8 +113,8 @@ bool mb_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
 
         qemu_log_mask(CPU_LOG_MMU, "MMU map mmu=%d v=%x p=%x prot=%x\n",
                       mmu_idx, vaddr, paddr, lu.prot);
-        tlb_set_page_with_attrs(cs, vaddr, paddr, attrs, lu.prot, mmu_idx,
-                                TARGET_PAGE_SIZE);
+        tlb_set_page_with_attrs(cs, vaddr, paddr, attrs, lu.prot, access_type,
+                                mmu_idx, TARGET_PAGE_SIZE);
         return true;
     }
 
target/mips/tcg/system/tlb_helper.c +2 -2
index 566924b079e..bf08ba29d02 100644
@@ -934,7 +934,7 @@ bool mips_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
     if (ret == TLBRET_MATCH) {
         tlb_set_page(cs, address & TARGET_PAGE_MASK,
                      physical & TARGET_PAGE_MASK, prot,
-                     mmu_idx, TARGET_PAGE_SIZE);
+                     access_type, mmu_idx, TARGET_PAGE_SIZE);
         return true;
     }
 #if !defined(TARGET_MIPS64)
@@ -952,7 +952,7 @@ bool mips_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
             if (ret == TLBRET_MATCH) {
                 tlb_set_page(cs, address & TARGET_PAGE_MASK,
                              physical & TARGET_PAGE_MASK, prot,
-                             mmu_idx, TARGET_PAGE_SIZE);
+                             access_type, mmu_idx, TARGET_PAGE_SIZE);
                 return true;
             }
         }
target/or1k/mmu.c +1 -1
index 315debaf3e5..c14f03081e1 100644
@@ -127,7 +127,7 @@ bool openrisc_cpu_tlb_fill(CPUState *cs, vaddr addr, int size,
     if (likely(excp == 0)) {
         tlb_set_page(cs, addr & TARGET_PAGE_MASK,
                      phys_addr & TARGET_PAGE_MASK, prot,
-                     mmu_idx, TARGET_PAGE_SIZE);
+                     access_type, mmu_idx, TARGET_PAGE_SIZE);
         return true;
     }
     if (probe) {
target/ppc/mmu_helper.c +1 -1
index ac607054027..8b55a9e4ddf 100644
@@ -1369,7 +1369,7 @@ bool ppc_cpu_tlb_fill(CPUState *cs, vaddr eaddr, int size,
     if (ppc_xlate(cpu, eaddr, access_type, &raddr,
                   &page_size, &prot, mmu_idx, !probe)) {
         tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
-                     prot, mmu_idx, 1UL << page_size);
+                     prot, access_type, mmu_idx, 1UL << page_size);
         return true;
     }
     if (probe) {
target/riscv/cpu_helper.c +1 -1
index 39c3486ae0f..6ff7b499711 100644
@@ -1927,7 +1927,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
 
     if (ret == TRANSLATE_SUCCESS) {
         tlb_set_page(cs, address & ~(tlb_size - 1), pa & ~(tlb_size - 1),
-                     prot, mmu_idx, tlb_size);
+                     prot, access_type, mmu_idx, tlb_size);
         return true;
     } else if (probe) {
         return false;
target/rx/cpu.c +2 -1
index b5284199e6d..6114e345e65 100644
@@ -194,7 +194,8 @@ static bool rx_cpu_tlb_fill(CPUState *cs, vaddr addr, int size,
     /* Linear mapping */
     address = physical = addr & TARGET_PAGE_MASK;
     prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
-    tlb_set_page(cs, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE);
+    tlb_set_page(cs, address, physical, prot, access_type,
+                 mmu_idx, TARGET_PAGE_SIZE);
     return true;
 }
 
target/s390x/tcg/excp_helper.c +1 -1
index 05e448d3f20..3363e2818d5 100644
@@ -184,7 +184,7 @@ bool s390_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
                       "%s: set tlb %" PRIx64 " -> %" PRIx64 " (%x)\n",
                       __func__, (uint64_t)vaddr, (uint64_t)raddr, prot);
         tlb_set_page(cs, address & TARGET_PAGE_MASK, raddr, prot,
-                     mmu_idx, TARGET_PAGE_SIZE);
+                     access_type, mmu_idx, TARGET_PAGE_SIZE);
         return true;
     }
     if (probe) {
target/sh4/helper.c +2 -1
index 5d6295618f5..2542d3d88f5 100644
@@ -812,7 +812,8 @@ bool superh_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
     if (ret == MMU_OK) {
         address &= TARGET_PAGE_MASK;
         physical &= TARGET_PAGE_MASK;
-        tlb_set_page(cs, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE);
+        tlb_set_page(cs, address, physical, prot, access_type, mmu_idx,
+                     TARGET_PAGE_SIZE);
         return true;
     }
     if (probe) {
target/sparc/mmu_helper.c +3 -3
index a6f76a1ab76..316f4182848 100644
@@ -236,7 +236,7 @@ bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
                       "Translate at %" VADDR_PRIx " -> "
                       HWADDR_FMT_plx ", vaddr " TARGET_FMT_lx "\n",
                       address, full.phys_addr, vaddr);
-        tlb_set_page_full(cs, mmu_idx, vaddr, &full);
+        tlb_set_page_full(cs, mmu_idx, vaddr, access_type, &full);
         return true;
     }
 
@@ -252,7 +252,7 @@ bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
            neverland. Fake/overridden mappings will be flushed when
            switching to normal mode. */
         full.prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
-        tlb_set_page_full(cs, mmu_idx, vaddr, &full);
+        tlb_set_page_full(cs, mmu_idx, vaddr, access_type, &full);
         return true;
     } else {
         if (access_type == MMU_INST_FETCH) {
@@ -777,7 +777,7 @@ bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
         trace_mmu_helper_mmu_fault(address, full.phys_addr, mmu_idx, env->tl,
                                    env->dmmu.mmu_primary_context,
                                    env->dmmu.mmu_secondary_context);
-        tlb_set_page_full(cs, mmu_idx, address, &full);
+        tlb_set_page_full(cs, mmu_idx, address, access_type, &full);
         return true;
     }
     if (probe) {
target/tricore/helper.c +1 -1
index 7ee8c7fd699..a7173dc73f0 100644
@@ -86,7 +86,7 @@ bool tricore_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
     if (ret == TLBRET_MATCH) {
         tlb_set_page(cs, address & TARGET_PAGE_MASK,
                      physical & TARGET_PAGE_MASK, prot | PAGE_EXEC,
-                     mmu_idx, TARGET_PAGE_SIZE);
+                     rw, mmu_idx, TARGET_PAGE_SIZE);
         return true;
     } else {
         assert(ret < 0);
target/xtensa/helper.c +2 -1
index 2d93b45036d..2cd51ba0cb7 100644
@@ -282,7 +282,8 @@ bool xtensa_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
         tlb_set_page(cs,
                      address & TARGET_PAGE_MASK,
                      paddr & TARGET_PAGE_MASK,
-                     access, mmu_idx, page_size);
+                     access, access_type, mmu_idx,
+                     page_size);
         return true;
     } else if (probe) {
         return false;
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v3 2/5 accel/tcg: address_space_translate*() will pass the correct iommu_flags

In reply to Jim Shu

Instead of IOMMU_NONE, address_space_translate_for_iotlb() now can pass the correct iommu_flags to the IOMMU translate function from the access_type.

Since RISC-V wgChecker [1] could permit access in RO or WO permission only, the IOMMUMemoryRegion could return different section for read and write access. To support this kind of IOMMUMemoryRegion in the path of CPU access, we should pass correct iommu_flags here.

[1] RISC-V WG:
https://patchew.org/QEMU/[email protected]/

---
 accel/tcg/cputlb.c        |  3 ++-
 include/accel/tcg/iommu.h | 12 +++++-------
 system/physmem.c          | 16 +++++++++++-----
 3 files changed, 18 insertions(+), 13 deletions(-)
accel/tcg/cputlb.c +2 -1
index 3bc951603dc..4ca4152579b 100644
@@ -1050,7 +1050,8 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx,
     prot = full->prot;
     asidx = cpu_asidx_from_attrs(cpu, full->attrs);
     section = address_space_translate_for_iotlb(cpu, asidx, paddr_page,
-                                                &xlat, &sz, full->attrs, &prot);
+                                                &xlat, &sz, full->attrs, &prot,
+                                                access_type);
     assert(sz >= TARGET_PAGE_SIZE);
 
     tlb_debug("vaddr=%016" VADDR_PRIx " paddr=0x" HWADDR_FMT_plx
include/accel/tcg/iommu.h +5 -7
index 547f8ea0ef0..30655aab4ba 100644
@@ -14,13 +14,11 @@
 #include "exec/hwaddr.h"
 #include "exec/memattrs.h"
 
-MemoryRegionSection *address_space_translate_for_iotlb(CPUState *cpu,
-                                                       int asidx,
-                                                       hwaddr addr,
-                                                       hwaddr *xlat,
-                                                       hwaddr *plen,
-                                                       MemTxAttrs attrs,
-                                                       int *prot);
+MemoryRegionSection *
+address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr,
+                                  hwaddr *xlat, hwaddr *plen,
+                                  MemTxAttrs attrs, int *prot,
+                                  MMUAccessType access_type);
 
 #endif
 
system/physmem.c +11 -5
index c58d940e807..564d2c6a648 100644
@@ -685,12 +685,14 @@ void tcg_iommu_init_notifier_list(CPUState *cpu)
 MemoryRegionSection *
 address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr orig_addr,
                                   hwaddr *xlat, hwaddr *plen,
-                                  MemTxAttrs attrs, int *prot)
+                                  MemTxAttrs attrs, int *prot,
+                                  MMUAccessType access_type)
 {
     MemoryRegionSection *section;
     IOMMUMemoryRegion *iommu_mr;
     IOMMUMemoryRegionClass *imrc;
     IOMMUTLBEntry iotlb;
+    IOMMUAccessFlags iommu_flags;
     int iommu_idx;
     hwaddr addr = orig_addr;
     AddressSpaceDispatch *d = address_space_to_dispatch(cpu->cpu_ases[asidx].as);
@@ -707,10 +709,14 @@ address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr orig_addr,
 
         iommu_idx = imrc->attrs_to_index(iommu_mr, attrs);
         tcg_register_iommu_notifier(cpu, iommu_mr, iommu_idx);
-        /* We need all the permissions, so pass IOMMU_NONE so the IOMMU
-         * doesn't short-cut its translation table walk.
-         */
-        iotlb = imrc->translate(iommu_mr, addr, IOMMU_NONE, iommu_idx);
+
+        if (access_type == MMU_DATA_STORE) {
+            iommu_flags = IOMMU_WO;
+        } else {
+            iommu_flags = IOMMU_RO;
+        }
+
+        iotlb = imrc->translate(iommu_mr, addr, iommu_flags, iommu_idx);
         addr = ((iotlb.translated_addr & ~iotlb.addr_mask)
                 | (addr & iotlb.addr_mask));
         /* Update the caller's prot bits to remove permissions the IOMMU
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v3 3/5 accel/tcg: Provide early AS translate function

In reply to Jim Shu

New early AS translate function will skip IOMMU translation. It will return IOMMU region if finding it. Original function is renamed to the late translate function.

It is preparation commit of IOMMU lazy translation.

---
 accel/tcg/cputlb.c        |  6 ++---
 include/accel/tcg/iommu.h | 13 +++++++----
 system/physmem.c          | 46 ++++++++++++++++++++++++++++++++++++---
 3 files changed, 55 insertions(+), 10 deletions(-)
accel/tcg/cputlb.c +3 -3
index 4ca4152579b..f0c049e1551 100644
@@ -1049,9 +1049,9 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx,
 
     prot = full->prot;
     asidx = cpu_asidx_from_attrs(cpu, full->attrs);
-    section = address_space_translate_for_iotlb(cpu, asidx, paddr_page,
-                                                &xlat, &sz, full->attrs, &prot,
-                                                access_type);
+    section = address_space_translate_for_iotlb_late(cpu, asidx, paddr_page,
+                                                     &xlat, &sz, full->attrs,
+                                                     &prot);
     assert(sz >= TARGET_PAGE_SIZE);
 
     tlb_debug("vaddr=%016" VADDR_PRIx " paddr=0x" HWADDR_FMT_plx
include/accel/tcg/iommu.h +9 -4
index 30655aab4ba..11e3d63d798 100644
@@ -15,10 +15,15 @@
 #include "exec/memattrs.h"
 
 MemoryRegionSection *
-address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr,
-                                  hwaddr *xlat, hwaddr *plen,
-                                  MemTxAttrs attrs, int *prot,
-                                  MMUAccessType access_type);
+address_space_translate_for_iotlb_early(CPUState *cpu, int asidx, hwaddr addr,
+                                        hwaddr *xlat, hwaddr *plen,
+                                        MemTxAttrs attrs, int *prot);
+
+MemoryRegionSection *
+address_space_translate_for_iotlb_late(CPUState *cpu, int asidx, hwaddr addr,
+                                       hwaddr *xlat, hwaddr *plen,
+                                       MemTxAttrs attrs, int *prot,
+                                       MMUAccessType access_type);
 
 #endif
 
system/physmem.c +43 -3

73 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#4 lore raw

PATCH v3 4/5 accel/tcg: Add IOMMU lazy translation function

In reply to Jim Shu

The lazy translation will translate IOMMU regions of the specific access_type and store the result into the CPUTLBEntryFull.

For CPUTLBEntry, lazy translation may update 'addend' and 'addr_idx' array. We restrict IOMMU region to have a single non-zero 'addend' across all permissions. Also, lazy translation only updates the 'addr_idx' for the permissions specified in @prot.

---
 accel/tcg/cputlb.c    | 168 ++++++++++++++++++++++++++++++++++++++++++
 include/hw/core/cpu.h |  15 ++++
 2 files changed, 183 insertions(+)
accel/tcg/cputlb.c +168 -0
index f0c049e1551..5735f632896 100644
@@ -1272,6 +1272,174 @@ static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr,
                                           mmu_idx, retaddr);
 }
 
+/*
+ * Perform lazy IOMMU translation for a CPUTLBEntry/CPUTLBEntryFull.
+ * This is called when CPU utilize the TLB entry in the slow path.
+ * Updates both entry and full entry to IOMMU translated data for the
+ * specific access type.
+ */
+static void
+tlb_translate_iommu(CPUState *cpu, int mmu_idx,
+                    vaddr addr, MMUAccessType access_type,
+                    CPUTLBEntryFull *full)
+{
+    CPUTLB *tlb = &cpu->neg.tlb;
+    MemoryRegionSection *section;
+    unsigned int read_flags, write_flags;
+    uintptr_t addend;
+    CPUTLBEntry *te;
+    hwaddr iotlb, xlat, sz, paddr_page;
+    vaddr addr_page;
+    int asidx, wp_flags, prot;
+    bool is_ram, is_romd;
+
+    if (!full->is_iommu || (full->iommu_last_at == access_type)) {
+        return;
+    }
+
+    assert_cpu_is_self(cpu);
+
+    if (full->lg_page_size <= TARGET_PAGE_BITS) {
+        sz = TARGET_PAGE_SIZE;
+    } else {
+        sz = (hwaddr)1 << full->lg_page_size;
+        tlb_add_large_page(cpu, mmu_idx, addr, sz);
+    }
+    addr_page = addr & TARGET_PAGE_MASK;
+    paddr_page = full->phys_addr & TARGET_PAGE_MASK;
+
+    prot = full->prot;
+    asidx = cpu_asidx_from_attrs(cpu, full->attrs);
+
+    section = address_space_translate_for_iotlb_late(cpu, asidx, paddr_page,
+                                                     &xlat, &sz, full->attrs,
+                                                     &prot, access_type);
+
+    assert(sz >= TARGET_PAGE_SIZE);
+
+    tlb_debug("vaddr=%016" VADDR_PRIx " paddr=0x" HWADDR_FMT_plx
+              " prot=%x idx=%d\n",
+              addr, full->phys_addr, prot, mmu_idx);
+
+    is_ram = memory_region_is_ram(section->mr);
+    is_romd = memory_region_is_romd(section->mr);
+
+    read_flags = full->tlb_fill_flags;
+    if (full->lg_page_size < TARGET_PAGE_BITS) {
+        /* Repeat the MMU check and TLB fill on every access.  */
+        read_flags |= TLB_INVALID_MASK;
+    }
+
+    if (is_ram || is_romd) {
+        /* RAM and ROMD both have associated host memory. */
+        addend = (uintptr_t)memory_region_get_ram_ptr(section->mr) + xlat;
+    } else {
+        /* I/O and IOMMU does not; force the host address to NULL. */
+        addend = 0;
+    }
+
+    write_flags = read_flags;
+
+    if (is_ram) {
+        iotlb = memory_region_get_ram_addr(section->mr) + xlat;
+        assert(!(iotlb & ~TARGET_PAGE_MASK));
+        /*
+         * Computing is_clean is expensive; avoid all that unless
+         * the page is actually writable.
+         */
+        if (prot & PAGE_WRITE) {
+            if (section->readonly) {
+                write_flags |= TLB_DISCARD_WRITE;
+            } else if (physical_memory_is_clean(iotlb)) {
+                write_flags |= TLB_NOTDIRTY;
+            }
+        }
+    } else {
+        /* I/O or ROMD */
+        iotlb = xlat;
+        /*
+         * Writes to romd devices must go through MMIO to enable write.
+         * Reads to romd devices go through the ram_ptr found above,
+         * but of course reads to I/O must go through MMIO.
+         */
+        write_flags |= TLB_MMIO;
+        if (!is_romd) {
+            read_flags = write_flags;
+        }
+    }
+
+    wp_flags = cpu_watchpoint_address_matches(cpu, addr_page,
+                                              TARGET_PAGE_SIZE);
+
+    /* Update the CPUTLBEntryFull for this access type. */
+    full->iommu_last_at = access_type;
+    full->xlat_offset = iotlb - addr_page;
+    full->section = section;
+    full->phys_addr = paddr_page;
+
+    /* Update the CPUTLBEntry: addend and addr_idx */
+    tlb = &cpu->neg.tlb;
+    te = tlb_entry(cpu, mmu_idx, addr_page);
+
+    qemu_spin_lock(&tlb->c.lock);
+
+    /*
+     * If IOMMU region is translated to the memories (has associated
+     * host memory), it will update the 'addend' to access memories in the
+     * fast path. Otherwise, IO region do not update the 'addend' because
+     * it might be already used by memory region from the other permissions.
+     * It is fine since IO region do not use addend.
+     */
+    if (is_ram || is_romd) {
+        if (te->addend + addr_page) {
+            /* addend of untranslated IOMMU region is 0 - addr_page. */
+
+            /*
+             * CPUTLBEntry only has 1 addend across all permissions.
+             * We don't support the IOMMUMemoryRegion to be translated to
+             * 2 different host memories from the different permissions.
+             * QEMU will trigger an assertion for such case.
+             */
+            g_assert(addend == te->addend + addr_page);
+        } else {
+            te->addend = addend - addr_page;
+        }
+    }
+
+    /*
+     * In the IOMMU lazy translation, we only update TLB flags for the
+     * permissions specified in @prot. For other permissions, we still
+     * keep the original TLB flags (e.g. TLB_IOMMU if not translated).
+     */
+    if (prot & PAGE_EXEC) {
+        tlb_set_compare(full, te, addr_page, read_flags,
+                        MMU_INST_FETCH, prot & PAGE_EXEC);
+    }
+
+    if (wp_flags & BP_MEM_READ) {
+        read_flags |= TLB_WATCHPOINT;
+    }
+    if (prot & PAGE_READ) {
+        tlb_set_compare(full, te, addr_page, read_flags,
+                        MMU_DATA_LOAD, prot & PAGE_READ);
+    }
+
+    if (prot & PAGE_WRITE_INV) {
+        write_flags |= TLB_INVALID_MASK;
+    }
+    if (wp_flags & BP_MEM_WRITE) {
+        write_flags |= TLB_WATCHPOINT;
+    }
+    if (prot & PAGE_WRITE) {
+        tlb_set_compare(full, te, addr_page, write_flags,
+                        MMU_DATA_STORE, prot & PAGE_WRITE);
+    }
+
+    qemu_spin_unlock(&tlb->c.lock);
+
+    return;
+}
+
 static MemoryRegionSection *
 io_prepare(hwaddr *out_offset, CPUState *cpu, CPUTLBEntryFull *full,
            vaddr addr, uintptr_t retaddr)
include/hw/core/cpu.h +15 -0
index 207a7a1becb..cfbce50d1c5 100644
@@ -254,6 +254,21 @@ struct CPUTLBEntryFull {
      */
     uint8_t slow_flags[MMU_ACCESS_COUNT];
 
+    /*
+     * @is_iommu indicates if the MemoryRegion is an IOMMU.
+     * When true, IOMMU translation is deferred until the entry is used.
+     */
+    bool is_iommu;
+
+    /*
+     * @iommu_last_at contains the access_type of last IOMMU translation.
+     * It means that this entry currently stores the translated data of
+     * IOMMU region with this access_type.
+     * When it is MMU_ACCESS_COUNT, the entry stores untranslated data of
+     * IOMMU region.
+     */
+    MMUAccessType iommu_last_at;
+
     /*
      * Allow target-specific additions to this structure.
      * This may be used to cache items from the guest cpu
Signature · 1 line
2.43.0
author
#5 lore raw

PATCH v3 5/5 accel/tcg: Support IOMMU lazy translation in CPU TLB

In reply to Jim Shu

tlb_set_page_full() no longer translates IOMMU region, but only stores untranslated IOMMU region in the CPUTLBEntryFull. Then, when CPU uses CPUTLBEntryFull in the slow path. it should perform the lazy translation via tlb_translate_iommu().

The untranslated IOMMU region in TLB always requires to run in the slow path to trigger the lazy translation, so the IOMMU region will contain a new TLB flag, TLB_IOMMU, to force the slow path. Lazy translation will overwrite the TLB flags to the flags of the translated region. The host memory region from IOMMU region can still run in the fast path to accelerate the performance.

---
 accel/tcg/cputlb.c       | 66 ++++++++++++++++++++++++++++++++++------
 include/exec/tlb-flags.h |  4 ++-
 2 files changed, 60 insertions(+), 10 deletions(-)
accel/tcg/cputlb.c +57 -9
index 5735f632896..7a44ae8238b 100644
@@ -1034,7 +1034,7 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx,
     hwaddr iotlb, xlat, sz, paddr_page;
     vaddr addr_page;
     int asidx, wp_flags, prot;
-    bool is_ram, is_romd;
+    bool is_ram, is_romd, is_iommu;
 
     assert_cpu_is_self(cpu);
 
@@ -1049,29 +1049,38 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx,
 
     prot = full->prot;
     asidx = cpu_asidx_from_attrs(cpu, full->attrs);
-    section = address_space_translate_for_iotlb_late(cpu, asidx, paddr_page,
-                                                     &xlat, &sz, full->attrs,
-                                                     &prot);
+
+    /*
+     * Use the early translation to check if it is an IOMMU region.
+     * This function stops at IOMMU regions without translating through them.
+     */
+    section = address_space_translate_for_iotlb_early(cpu, asidx, paddr_page,
+                                                      &xlat, &sz, full->attrs,
+                                                      &prot);
     assert(sz >= TARGET_PAGE_SIZE);
 
     tlb_debug("vaddr=%016" VADDR_PRIx " paddr=0x" HWADDR_FMT_plx
               " prot=%x idx=%d\n",
               addr, full->phys_addr, prot, mmu_idx);
 
+    is_iommu = memory_region_is_iommu(section->mr);
+    is_ram = memory_region_is_ram(section->mr);
+    is_romd = memory_region_is_romd(section->mr);
+
+    full->is_iommu = is_iommu;
+    full->iommu_last_at = MMU_ACCESS_COUNT;
+
     read_flags = full->tlb_fill_flags;
     if (full->lg_page_size < TARGET_PAGE_BITS) {
         /* Repeat the MMU check and TLB fill on every access.  */
         read_flags |= TLB_INVALID_MASK;
     }
 
-    is_ram = memory_region_is_ram(section->mr);
-    is_romd = memory_region_is_romd(section->mr);
-
     if (is_ram || is_romd) {
         /* RAM and ROMD both have associated host memory. */
         addend = (uintptr_t)memory_region_get_ram_ptr(section->mr) + xlat;
     } else {
-        /* I/O does not; force the host address to NULL. */
+        /* I/O and IOMMU does not; force the host address to NULL. */
         addend = 0;
     }
 
@@ -1090,6 +1099,15 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx,
                 write_flags |= TLB_NOTDIRTY;
             }
         }
+    } else if (is_iommu) {
+        /* IOMMU */
+        iotlb = xlat;
+        /*
+         * If IOMMU region is not translated, any access will go to
+         * the slow path and do the lazy IOMMU translation.
+         */
+        read_flags |= TLB_IOMMU;
+        write_flags |= TLB_IOMMU;
     } else {
         /* I/O or ROMD */
         iotlb = xlat;
@@ -1568,6 +1586,11 @@ static int probe_access_internal(CPUState *cpu, vaddr addr,
     flags &= tlb_addr;
 
     *pfull = full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
+
+    if (full->is_iommu) {
+        tlb_translate_iommu(cpu, mmu_idx, addr, access_type, full);
+    }
+
     flags |= full->slow_flags[access_type];
 
     /* Fold all "mmio-like" bits into TLB_MMIO.  This is not RAM.  */
@@ -1760,6 +1783,11 @@ bool tlb_plugin_lookup(CPUState *cpu, vaddr addr, int mmu_idx,
     }
 
     full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
+
+    if (full->is_iommu) {
+        tlb_translate_iommu(cpu, mmu_idx, addr, access_type, full);
+    }
+
     data->phys_addr = full->phys_addr | (addr & ~TARGET_PAGE_MASK);
 
     /* We must have an iotlb entry for MMIO */
@@ -1833,6 +1861,11 @@ static bool mmu_lookup1(CPUState *cpu, MMULookupPageData *data, MemOp memop,
     }
 
     full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
+
+    if (full->is_iommu) {
+        tlb_translate_iommu(cpu, mmu_idx, addr, access_type, full);
+    }
+
     flags = tlb_addr & (TLB_FLAGS_MASK & ~TLB_FORCE_SLOW);
     flags |= full->slow_flags[access_type];
 
@@ -1944,6 +1977,11 @@ static bool mmu_lookup(CPUState *cpu, vaddr addr, MemOpIdx oi,
         if (mmu_lookup1(cpu, &l->page[1], 0, l->mmu_idx, type, ra)) {
             uintptr_t index = tlb_index(cpu, l->mmu_idx, addr);
             l->page[0].full = &cpu->neg.tlb.d[l->mmu_idx].fulltlb[index];
+
+            if (l->page[0].full->is_iommu) {
+                tlb_translate_iommu(cpu, l->mmu_idx, addr, type,
+                                    l->page[0].full);
+            }
         }
 
         flags = l->page[0].flags | l->page[1].flags;
@@ -2001,6 +2039,17 @@ static void *atomic_mmu_lookup(CPUState *cpu, vaddr addr, MemOpIdx oi,
         tlb_addr = tlb_addr_write(tlbe) & ~TLB_INVALID_MASK;
     }
 
+    full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
+
+    if (full->is_iommu) {
+        /*
+         * Do IOMMU lazy translation before accessing addr_idx and TLB flags.
+         * Generate TLB flags for both read and write.
+         */
+        tlb_translate_iommu(cpu, mmu_idx, addr, MMU_DATA_LOAD, full);
+        tlb_translate_iommu(cpu, mmu_idx, addr, MMU_DATA_STORE, full);
+    }
+
     /*
      * Let the guest notice RMW on a write-only page.
      * We have just verified that the page is writable.
@@ -2035,7 +2084,6 @@ static void *atomic_mmu_lookup(CPUState *cpu, vaddr addr, MemOpIdx oi,
     }
 
     /* Finish collecting tlb flags for both read and write. */
-    full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
     tlb_addr |= tlbe->addr_read;
     tlb_addr &= TLB_FLAGS_MASK & ~TLB_FORCE_SLOW;
     tlb_addr |= full->slow_flags[MMU_DATA_STORE];
include/exec/tlb-flags.h +3 -1
index 357e79095c9..d78f1c3dc64 100644
@@ -52,10 +52,12 @@
 #define TLB_DISCARD_WRITE    (1 << 3)
 /* Set if TLB entry is an IO callback.  */
 #define TLB_MMIO             (1 << 4)
+/* Set if TLB entry is a IOMMU region which requires lazy translation. */
+#define TLB_IOMMU            (1 << 5)
 
 #define TLB_SLOW_FLAGS_MASK \
     (TLB_BSWAP | TLB_WATCHPOINT | TLB_CHECK_ALIGNED | \
-     TLB_DISCARD_WRITE | TLB_MMIO)
+     TLB_DISCARD_WRITE | TLB_MMIO | TLB_IOMMU)
 
 /*
  * Flags stored in CPUTLBEntry.addr_idx[x].
Signature · 1 line
2.43.0
author
#6 lore raw
In reply to Jim Shu

Hi all,

Gentle ping on this patch.
Any suggestion is welcome.

Thanks,
Jim Shu

Quoted 102 lines · Jim Shu

Incoming security protection devices feature more complex IOMMUMemoryRegion implementation in the CPU path than ARM MPC device. For example, RISC-V wgChecker [1] may permit the access with only RO/WO permissions. Consequently, the IOMMUMemoryRegion could return different sections for read & write access.

To support such IOMMUMemoryRegion behavior in the CPU path, the design of IOMMU translation must be updated:

1. address_space_translate*() must now pass the access_type to IOMMUMemoryRegion.
2. Since IOMMU translation results are too complex to be fully stored in the CPU TLB. we will defer the translation until the actual access occurs. Also, TLB is allowed to store the untranslated IOMMU region.

To implement deferred IOMMU translation, this patchset introduces the following changes:

1. tlb_set_page_full() no longer translates the IOMMU region immediately. Instead, it stores the untranslated region directly in the TLB. A new slow-path flag, TLB_IOMMU, is introduced to force access into the slow path when a region has not yet been translated in the TLB entry.

2. When the CPU utilizes a TLB entry in the slow path, it should perform the lazy IOMMU translation of the access_type first. The resulting translated region and access type are stored in CPUTLBEntryFull. Since the slow path always performs lazy translation first, we can switch the CPUTLBEntryFull content to the correct access type before use.

3. To accelerate memory access in the fast path, lazy translation can update the addend of the CPUTLBEntry when translating the region to a host memory region. We restrict the IOMMU region to have a single non-zero 'addend' across all permissions. If a second 'addend' is present for a CPUTLBEntry, QEMU will trigger an assertion. This limitation is sufficient for security devices, as their "secondary" region is typically an IO region used to emulate device error handling when access is rejected.

4. To support non-slow TLB flags, lazy translation can update the TLB flags in the 'addr_idx' of the CPUTLBEntry. Lazy translation only updates the flags for the permissions specified in @prot. This ensures that each access_type of a translated region to maintains independent TLB flags. For example, TLB_DIRTY of memory region will not be "polluted" from other permission that translated to different region.

Both RISC-V wgChecker [1] and RISC-V IOPMP [2] devices require this feature.

[1] RISC-V WG:
https://patchew.org/QEMU/[email protected]/
[2] RISC-V IOPMP:
https://patchew.org/QEMU/[email protected]/

Changed since v2:
- Fix unused initialized memory error

Changed since v1:
- Remove the ping-pong TLB entry behavior. Instead, defer the IOMMU translation until actual access in the CPU path. Provide a IOMMU lazy translation function with the special handling of 'addend' and 'addr_idx' fields of CPUTLBEntry.
- Fix the checkpatch warning.

Jim Shu (5):
accel/tcg: Pass access_type as an argument of tlb_set_page*() accel/tcg: address_space_translate*() will pass the correct
iommu_flags
accel/tcg: Provide early AS translate function accel/tcg: Add IOMMU lazy translation function accel/tcg: Support IOMMU lazy translation in CPU TLB

 accel/tcg/cputlb.c                   | 247 +++++++++++++++++++++++++--
 include/accel/tcg/iommu.h            |  17 +-
 include/exec/cputlb.h                |  11 +-
 include/exec/tlb-flags.h             |   4 +-
 include/hw/core/cpu.h                |  15 ++
 system/physmem.c                     |  60 ++++++-
 target/alpha/helper.c                |   2 +-
 target/avr/helper.c                  |   3 +-
 target/i386/tcg/system/excp_helper.c |   3 +-
 target/loongarch/tcg/tlb_helper.c    |   2 +-
 target/m68k/helper.c                 |  10 +-
 target/microblaze/helper.c           |   8 +-
 target/mips/tcg/system/tlb_helper.c  |   4 +-
 target/or1k/mmu.c                    |   2 +-
 target/ppc/mmu_helper.c              |   2 +-
 target/riscv/cpu_helper.c            |   2 +-
 target/rx/cpu.c                      |   3 +-
 target/s390x/tcg/excp_helper.c       |   2 +-
 target/sh4/helper.c                  |   3 +-
 target/sparc/mmu_helper.c            |   6 +-
 target/tricore/helper.c              |   2 +-
 target/xtensa/helper.c               |   3 +-
 22 files changed, 354 insertions(+), 57 deletions(-)

--
2.43.0

#7 lore raw
In reply to Jim Shu

Hi everyone,

Seems like this series is fully acked, so ... can someone queue this up? This work is a prerequisite for a qemu-riscv feature (WorldGuard support) that we want to get merged. We have some reviews on the RISC-V bits already but without these accel/tcg bits merged we can't do much progress since any change here will affect the RV parts too.

Thanks,
Daniel

Quoted 98 lines · Jim Shu

Incoming security protection devices feature more complex IOMMUMemoryRegion implementation in the CPU path than ARM MPC device. For example, RISC-V wgChecker [1] may permit the access with only RO/WO permissions. Consequently, the IOMMUMemoryRegion could return different sections for read & write access.

To support such IOMMUMemoryRegion behavior in the CPU path, the design of IOMMU translation must be updated:

1. address_space_translate*() must now pass the access_type to IOMMUMemoryRegion.
2. Since IOMMU translation results are too complex to be fully stored in the CPU TLB. we will defer the translation until the actual access occurs. Also, TLB is allowed to store the untranslated IOMMU region.

To implement deferred IOMMU translation, this patchset introduces the following changes:

1. tlb_set_page_full() no longer translates the IOMMU region immediately. Instead, it stores the untranslated region directly in the TLB. A new slow-path flag, TLB_IOMMU, is introduced to force access into the slow path when a region has not yet been translated in the TLB entry.

2. When the CPU utilizes a TLB entry in the slow path, it should perform the lazy IOMMU translation of the access_type first. The resulting translated region and access type are stored in CPUTLBEntryFull. Since the slow path always performs lazy translation first, we can switch the CPUTLBEntryFull content to the correct access type before use.

3. To accelerate memory access in the fast path, lazy translation can update the addend of the CPUTLBEntry when translating the region to a host memory region. We restrict the IOMMU region to have a single non-zero 'addend' across all permissions. If a second 'addend' is present for a CPUTLBEntry, QEMU will trigger an assertion. This limitation is sufficient for security devices, as their "secondary" region is typically an IO region used to emulate device error handling when access is rejected.

4. To support non-slow TLB flags, lazy translation can update the TLB flags in the 'addr_idx' of the CPUTLBEntry. Lazy translation only updates the flags for the permissions specified in @prot. This ensures that each access_type of a translated region to maintains independent TLB flags. For example, TLB_DIRTY of memory region will not be "polluted" from other permission that translated to different region.

Both RISC-V wgChecker [1] and RISC-V IOPMP [2] devices require this feature.

[1] RISC-V WG:
https://patchew.org/QEMU/[email protected]/
[2] RISC-V IOPMP:
https://patchew.org/QEMU/[email protected]/

Changed since v2:
- Fix unused initialized memory error

Changed since v1:
- Remove the ping-pong TLB entry behavior. Instead, defer the IOMMU translation until actual access in the CPU path. Provide a IOMMU lazy translation function with the special handling of 'addend' and 'addr_idx' fields of CPUTLBEntry.
- Fix the checkpatch warning.

Jim Shu (5):
accel/tcg: Pass access_type as an argument of tlb_set_page*() accel/tcg: address_space_translate*() will pass the correct
iommu_flags
accel/tcg: Provide early AS translate function accel/tcg: Add IOMMU lazy translation function accel/tcg: Support IOMMU lazy translation in CPU TLB

  accel/tcg/cputlb.c                   | 247 +++++++++++++++++++++++++--
  include/accel/tcg/iommu.h            |  17 +-
  include/exec/cputlb.h                |  11 +-
  include/exec/tlb-flags.h             |   4 +-
  include/hw/core/cpu.h                |  15 ++
  system/physmem.c                     |  60 ++++++-
  target/alpha/helper.c                |   2 +-
  target/avr/helper.c                  |   3 +-
  target/i386/tcg/system/excp_helper.c |   3 +-
  target/loongarch/tcg/tlb_helper.c    |   2 +-
  target/m68k/helper.c                 |  10 +-
  target/microblaze/helper.c           |   8 +-
  target/mips/tcg/system/tlb_helper.c  |   4 +-
  target/or1k/mmu.c                    |   2 +-
  target/ppc/mmu_helper.c              |   2 +-
  target/riscv/cpu_helper.c            |   2 +-
  target/rx/cpu.c                      |   3 +-
  target/s390x/tcg/excp_helper.c       |   2 +-
  target/sh4/helper.c                  |   3 +-
  target/sparc/mmu_helper.c            |   6 +-
  target/tricore/helper.c              |   2 +-
  target/xtensa/helper.c               |   3 +-
  22 files changed, 354 insertions(+), 57 deletions(-)