Skip to content

Defer the IOMMU translation in the CPU path and support access_type

Jim Shu qemu-devel qemu-riscv 3 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 Latest5 patches 2 repliesIn review
  2. v25 patches 12 repliesSuperseded
  3. v12 patches 8 repliesSuperseded

Changes in v3 from the cover letter

- Fix unused initialized memory error

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/5accel/tcg: Pass access_type as an argument of tlb_set_page*()accel/tcg/cputlb.c include/exec/cputlb.h target/alpha/helper.c +15 more+48 -340 replies
2/5accel/tcg: address_space_translate*() will pass the correct iommu_flagsaccel/tcg/cputlb.c include/accel/tcg/iommu.h system/physmem.c+18 -130 replies
3/5accel/tcg: Provide early AS translate functionaccel/tcg/cputlb.c include/accel/tcg/iommu.h system/physmem.c+55 -100 replies
4/5accel/tcg: Add IOMMU lazy translation functionaccel/tcg/cputlb.c include/hw/core/cpu.h+183 -00 replies
5/5accel/tcg: Support IOMMU lazy translation in CPU TLBaccel/tcg/cputlb.c include/exec/tlb-flags.h+60 -100 replies

Reviews trailers per patch

All 5 patches have a Reviewed-by or Acked-by.

PatchPierrick Bouvier
1/5A-b
2/5A-b
3/5A-b
4/5A-b
5/5A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v3 thread

Cover letter [PATCH v3 0/5] 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