hw/riscv/riscv-iommu: Correct MSI fault records and test trap handling
Subsystems (2)
cover hw/riscv/riscv-iommu: Correct MSI fault records and test trap handling
This series corrects MSI fault records in the RISC-V IOMMU and adds regression coverage for the trap callback.
Patch 1 preserves the original IOVA through MSI interception, recovers the GPA for MSI lookup, and keeps internal recovery walks out of the modeled HPM events. It includes four firmware-independent MSI qtests.
Patch 2 separately aligns MSI fault PV reporting with QEMU's existing PDTV policy. Its PD8/DPE regression selects default process zero and checks PV, PID, and the original IOVA. Complete per-request PID-validity metadata handling is outside this change.
Patch 3 shares the private callback with nine deterministic unit tests for recovery faults and ordinary-DMA fallback, without adding a public API or runtime test hooks. The context stub uses a distinct release reference so cleanup checks cannot confuse it with the context.
Patch 1 can be applied on its own. Patch 2 builds on patch 1; neither requires patch 3. The refactoring and unit tests in patch 3 can be reviewed separately if the private implementation fragment or test structure needs further discussion.
The series is based on QEMU master at e9fa1f66f0d4. Validation covers RISC-V IOMMU qtests, MSI trap unit tests, the MSI regression probe, and an x86_64-softmmu-only unit-test build with -Werror.
The initial local series was also built and tested after each individual patch.
Issue: https://gitlab.com/qemu-project/qemu/-/work_items/3562
Zephyr Li (3):
hw/riscv/riscv-iommu: Preserve IOVA in MSI fault records hw/riscv/riscv-iommu: Fix MSI fault PV reporting for process ID zero hw/riscv/riscv-iommu: Add unit tests for MSI trap handling
MAINTAINERS | 1 + hw/riscv/riscv-iommu-trap.c.inc | 69 ++++++++ hw/riscv/riscv-iommu.c | 61 +++---- tests/qtest/iommu-riscv-test.c | 181 ++++++++++++++++++-- tests/unit/meson.build | 1 + tests/unit/test-riscv-iommu-trap.c | 266 +++++++++++++++++++++++++++++ 6 files changed, 525 insertions(+), 54 deletions(-) create mode 100644 hw/riscv/riscv-iommu-trap.c.inc create mode 100644 tests/unit/test-riscv-iommu-trap.c
Signature · 1 line
2.43.0
PATCH 1/3 hw/riscv/riscv-iommu: Preserve IOVA in MSI fault records
MSI translation errors currently report zero for both fault address fields. The MSI write handler receives the GPA used to select the MSI PTE, but an inbound transaction's iotval must identify its original IOVA. These addresses need not match when first-stage translation is enabled.
Keep trapped translations in IOVA space when returning them to the memory subsystem. In the trap callback, walk the same device/process context again to recover the GPA and pass both addresses to the MSI write handler. This retains the per-transaction IOVA without shared last-address state and leaves internal translation/debug results intact.
Report a rewalk fault with the original IOVA. If a changed mapping no longer selects MSI translation, complete the write as ordinary DMA instead. Preserve the request's process context and little-endian data in these paths. MSI fault reports continue to leave iotval2 unchanged.
Exclude the internal GPA-recovery replay from the modeled S/G-stage HPM walk events, so this fault-reporting change preserves the existing counter behavior. Ordinary translation walks retain their existing accounting.
Add firmware-independent qtests using device-generated MSIs and nonidentity first-stage mappings. Check fault addresses, Bare first-stage operation, successful MSI writes, and the modeled HPM counter deltas.
This still adds a page-table walk to intercepted MSI writes when first-stage translation is enabled; the Bare first-stage path uses its existing early MSI check.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3562
--- hw/riscv/riscv-iommu.c | 60 ++++++++++--- tests/qtest/iommu-riscv-test.c | 153 ++++++++++++++++++++++++++++++--- 2 files changed, 189 insertions(+), 24 deletions(-)
hw/riscv/riscv-iommu.c +49 -11
@@ -276,10 +276,11 @@ static hwaddr riscv_iommu_napot_page_mask(hwaddr ppn, hwaddr addr, hwaddr *out)* @s : IOMMU Device State * @ctx : Translation context for device id and process address space id. * @iotlb : translation data: physical address and access mode. +* @update_hpm : account for modeled page-table walk events. * @return : success or fault cause code. */ static int riscv_iommu_spa_fetch (RISCVIOMMUState *s,RISCVIOMMUContext *ctx, -IOMMUTLBEntry *iotlb) +IOMMUTLBEntry *iotlb,bool update_hpm) {IOMMUAccessFlags trans_perm =IOMMU_NONE ;dma_addr_t addr, base;
@@ -456,10 +457,14 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx, } -if (pass ==S_STAGE ) { -riscv_iommu_hpm_incr_ctr (s, ctx,RISCV_IOMMU_HPMEVENT_S_VS_WALKS ); - }else { -riscv_iommu_hpm_incr_ctr (s, ctx,RISCV_IOMMU_HPMEVENT_G_WALKS ); +if (update_hpm) { +if (pass ==S_STAGE ) { +riscv_iommu_hpm_incr_ctr (s, ctx, +RISCV_IOMMU_HPMEVENT_S_VS_WALKS ); + }else { +riscv_iommu_hpm_incr_ctr (s, ctx, +RISCV_IOMMU_HPMEVENT_G_WALKS ); + } }/* Read page table entry */
@@ -641,9 +646,9 @@ static void riscv_iommu_report_fault(RISCVIOMMUState *s,riscv_iommu_fault (s, &ev); } -/* Redirect MSI write for given GPA. */ +/* Redirect an MSI write, retaining the original IOVA for fault reporting. */ static MemTxResult riscv_iommu_msi_write (RISCVIOMMUState *s, -RISCVIOMMUContext *ctx,uint64_t gpa,uint64_t data, +RISCVIOMMUContext *ctx,uint64_t iova,uint64_t gpa,uint64_t data,unsigned size,MemTxAttrs attrs) {MemTxResult res;
@@ -826,7 +831,7 @@ static MemTxResult riscv_iommu_msi_write(RISCVIOMMUState *s, err:riscv_iommu_report_fault (s, ctx, fault_type, cause, - !!ctx->process_id,0 ,0 ); + !!ctx->process_id, iova,0 );return res; }
@@ -1776,7 +1781,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, true);if (!fault && iotlb->target_as == &s->trap_as) {/* Do not cache trapped MSI translations */
@@ -2609,6 +2614,13 @@ static MemTxResult riscv_iommu_trap_write(void *opaque, hwaddr addr,MemTxResult res;void *ref;uint32_t devid = attrs.requester_id; +IOMMUTLBEntry iotlb = { + .iova = addr, + .target_as = s->target_as, + .addr_mask = ~0ULL , + .perm =IOMMU_WO , + }; +int cause;if (attrs.unspecified) {return MEMTX_ACCESS_ERROR ;
@@ -2617,11 +2629,32 @@ static MemTxResult riscv_iommu_trap_write(void *opaque, hwaddr addr,/* FIXME: PCIe bus remapping for attached endpoints. */ devid |= s->bus <<8 ; - ctx =riscv_iommu_ctx (s, devid,0 ,IOMMU_RW , addr, &ref); + ctx =riscv_iommu_ctx (s, devid, attrs.pid,IOMMU_WO , addr, &ref);if (ctx ==NULL ) { res =MEMTX_ACCESS_ERROR ; }else { - res =riscv_iommu_msi_write (s, ctx, addr, data, size, attrs); +/* +* The trap address is the original IOVA. Resolve the MSI GPA +* without consulting or updating the translation cache. +* Exclude this internal replay from the modeled HPM events. +*/ + cause =riscv_iommu_spa_fetch (s, ctx, &iotlb, false); +if (cause) { +riscv_iommu_report_fault (s, ctx, +RISCV_IOMMU_FQ_TTYPE_UADDR_WR , cause, + !!(ctx->tc &RISCV_IOMMU_DC_TC_PDTV ), + addr, iotlb.translated_addr); + res =MEMTX_ACCESS_ERROR ; + }else if (iotlb.target_as == &s->trap_as) { + res =riscv_iommu_msi_write (s, ctx, addr, + iotlb.translated_addr, data, + size, attrs); + }else { +/* The mapping may have changed since the original walk. */ + data =cpu_to_le64 (data); + res =dma_memory_write (iotlb.target_as, iotlb.translated_addr, + &data, size, attrs); + } }riscv_iommu_ctx_put (s, ref);return res;
@@ -2943,6 +2976,11 @@ static IOMMUTLBEntry riscv_iommu_memory_region_translate(IOMMU_FLAG_STR [flag &IOMMU_RW ], iotlb.iova, iotlb.translated_addr); +if (iotlb.target_as == &as->iommu->trap_as) { +/* Preserve the transaction IOVA through the MSI write callback. */ + iotlb.translated_addr = iotlb.iova; + } +riscv_iommu_ctx_put (as->iommu, ref);return iotlb;
tests/qtest/iommu-riscv-test.c +140 -13
217 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH 1/3] hw/riscv/riscv-iommu: Preserve IOVA in MSI fault records
Quoted 27 lines · Zephyr Li
MSI translation errors currently report zero for both fault address fields. The MSI write handler receives the GPA used to select the MSI PTE, but an inbound transaction's iotval must identify its original IOVA. These addresses need not match when first-stage translation is enabled.
Keep trapped translations in IOVA space when returning them to the memory subsystem. In the trap callback, walk the same device/process context again to recover the GPA and pass both addresses to the MSI write handler. This retains the per-transaction IOVA without shared last-address state and leaves internal translation/debug results intact.
Report a rewalk fault with the original IOVA. If a changed mapping no longer selects MSI translation, complete the write as ordinary DMA instead. Preserve the request's process context and little-endian data in these paths. MSI fault reports continue to leave iotval2 unchanged.
Exclude the internal GPA-recovery replay from the modeled S/G-stage HPM walk events, so this fault-reporting change preserves the existing counter behavior. Ordinary translation walks retain their existing accounting.
Add firmware-independent qtests using device-generated MSIs and nonidentity first-stage mappings. Check fault addresses, Bare first-stage operation, successful MSI writes, and the modeled HPM counter deltas.
Quoted 8 lines
This still adds a page-table walk to intercepted MSI writes when first-stage translation is enabled; the Bare first-stage path uses its existing early MSI check.
Fixes 0c54acb8243d ("hw/riscv: add RISC-V IOMMU base emulation")Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3562
Signed-off-by Zephyr Li <[email protected]>---
Quoted 360 lines
hw/riscv/riscv-iommu.c | 60 ++++++++++--- tests/qtest/iommu-riscv-test.c | 153 ++++++++++++++++++++++++++++++--- 2 files changed, 189 insertions(+), 24 deletions(-)hw/riscv/riscv-iommu.c +48
-11@@ -276,10 +276,11 @@ static hwaddr riscv_iommu_napot_page_mask(hwaddr ppn, hwaddr addr, hwaddr *out)* @s : IOMMU Device State * @ctx : Translation context for device id and process address space id. * @iotlb : translation data: physical address and access mode. +* @update_hpm : account for modeled page-table walk events. * @return : success or fault cause code. */ static int riscv_iommu_spa_fetch (RISCVIOMMUState *s,RISCVIOMMUContext *ctx, -IOMMUTLBEntry *iotlb) +IOMMUTLBEntry *iotlb,bool update_hpm) {IOMMUAccessFlags trans_perm =IOMMU_NONE ;dma_addr_t addr, base;@@ -456,10 +457,14 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx, } -if (pass ==S_STAGE ) { -riscv_iommu_hpm_incr_ctr (s, ctx,RISCV_IOMMU_HPMEVENT_S_VS_WALKS ); - }else { -riscv_iommu_hpm_incr_ctr (s, ctx,RISCV_IOMMU_HPMEVENT_G_WALKS ); +if (update_hpm) { +if (pass ==S_STAGE ) { +riscv_iommu_hpm_incr_ctr (s, ctx, +RISCV_IOMMU_HPMEVENT_S_VS_WALKS ); + }else { +riscv_iommu_hpm_incr_ctr (s, ctx, +RISCV_IOMMU_HPMEVENT_G_WALKS ); + } }/* Read page table entry */ @@ -641,9 +646,9 @@ static void riscv_iommu_report_fault(RISCVIOMMUState *s,riscv_iommu_fault (s, &ev); } -/* Redirect MSI write for given GPA. */ +/* Redirect an MSI write, retaining the original IOVA for fault reporting. */ static MemTxResult riscv_iommu_msi_write (RISCVIOMMUState *s, -RISCVIOMMUContext *ctx,uint64_t gpa,uint64_t data, +RISCVIOMMUContext *ctx,uint64_t iova,uint64_t gpa,uint64_t data,unsigned size,MemTxAttrs attrs) {MemTxResult res;@@ -826,7 +831,7 @@ static MemTxResult riscv_iommu_msi_write(RISCVIOMMUState *s, err:riscv_iommu_report_fault (s, ctx, fault_type, cause, - !!ctx->process_id,0 ,0 ); + !!ctx->process_id, iova,0 );return res; }@@ -1776,7 +1781,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, true);if (!fault && iotlb->target_as == &s->trap_as) {/* Do not cache trapped MSI translations */ @@ -2609,6 +2614,13 @@ static MemTxResult riscv_iommu_trap_write(void *opaque, hwaddr addr,MemTxResult res;void *ref;uint32_t devid = attrs.requester_id; +IOMMUTLBEntry iotlb = { + .iova = addr, + .target_as = s->target_as, + .addr_mask = ~0ULL , + .perm =IOMMU_WO , + };> + int cause;if (attrs.unspecified) {return MEMTX_ACCESS_ERROR ;@@ -2617,11 +2629,32 @@ static MemTxResult riscv_iommu_trap_write(void *opaque, hwaddr addr,/* FIXME: PCIe bus remapping for attached endpoints. */ devid |= s->bus <<8 ; - ctx =riscv_iommu_ctx (s, devid,0 ,IOMMU_RW , addr, &ref); + ctx =riscv_iommu_ctx (s, devid, attrs.pid,IOMMU_WO , addr, &ref);if (ctx ==NULL ) { res =MEMTX_ACCESS_ERROR ; }else { - res =riscv_iommu_msi_write (s, ctx, addr, data, size, attrs); +/* +* The trap address is the original IOVA. Resolve the MSI GPA +* without consulting or updating the translation cache. +* Exclude this internal replay from the modeled HPM events. +*/ + cause =riscv_iommu_spa_fetch (s, ctx, &iotlb, false); +if (cause) { +riscv_iommu_report_fault (s, ctx, +RISCV_IOMMU_FQ_TTYPE_UADDR_WR , cause, + !!(ctx->tc &RISCV_IOMMU_DC_TC_PDTV ), + addr, iotlb.translated_addr); + res =MEMTX_ACCESS_ERROR ; + }else if (iotlb.target_as == &s->trap_as) { + res =riscv_iommu_msi_write (s, ctx, addr, + iotlb.translated_addr, data, + size, attrs); + }else { +/* The mapping may have changed since the original walk. */ + data =cpu_to_le64 (data); + res =dma_memory_write (iotlb.target_as, iotlb.translated_addr, + &data, size, attrs); + } }riscv_iommu_ctx_put (s, ref);return res;@@ -2943,6 +2976,11 @@ static IOMMUTLBEntry riscv_iommu_memory_region_translate(IOMMU_FLAG_STR [flag &IOMMU_RW ], iotlb.iova, iotlb.translated_addr); +if (iotlb.target_as == &as->iommu->trap_as) { +/* Preserve the transaction IOVA through the MSI write callback. */ + iotlb.translated_addr = iotlb.iova; + } +riscv_iommu_ctx_put (as->iommu, ref);return iotlb;tests/qtest/iommu-riscv-test.c +140
-13@@ -25,6 +25,12 @@#define DBG_TRANSLATED_PA 0x90000000ull#define DBG_NESTED_GPA 0x200000ull +#define MSI_TABLE_PA 0x80030000ull +#define MSI_TARGET_PA 0x80040000ull +#define MSI_DATA 0x37 +#define EDU_DEVICE_ID 0x11e8 +#define EDU_IRQ_RAISE 0x60 +/* RISC-V virt machine PCI configuration */ #define RISCV_GPEX_PIO_BASE 0x3000000#define RISCV_BUS_PIO_LIMIT 0x10000@@ -50,6 +56,21 @@ typedef struct RiscvIommuDbgTest {uint64_t expected_response; } RiscvIommuDbgTest; +typedef struct RiscvIommuMsiTest { +const char *name; +QRIOMMUTransMode mode; +bool valid_pte; +uint64_t s_walks; +uint64_t g_walks; +}RiscvIommuMsiTest ; + +static const RiscvIommuMsiTest msi_tests[] = { + {"first-stage-bare-fault" ,QRIOMMU_TM_G_STAGE_ONLY , false,0 ,0 }, + {"nested-fault" ,QRIOMMU_TM_NESTED , false,3 ,9 }, + {"first-stage-bare-basic" ,QRIOMMU_TM_G_STAGE_ONLY , true,0 ,0 }, + {"nested-basic" ,QRIOMMU_TM_NESTED , true,3 ,9 }, +}; +static const RiscvIommuDbgTest debug_tests[] = { { .name ="page-4k" ,@@ -157,15 +178,16 @@ static QPCIDevice *find_riscv_iommu_pci(QGenericPCIBus *gbus,return iommu_dev; } -static QPCIDevice *find_iommu_testdev (QGenericPCIBus *gbus,QPCIBar *bar) +static QPCIDevice *find_testdev (QGenericPCIBus *gbus,QPCIBar *bar,bool msi) {QPCIDevice *dev =NULL ; +uint16_t vendor = msi ?PCI_VENDOR_ID_QEMU :IOMMU_TESTDEV_VENDOR_ID ; +uint16_t device = msi ?EDU_DEVICE_ID :IOMMU_TESTDEV_DEVICE_ID ; -g_test_message ("Searching for iommu-testdev (vendor=0x%04x, device=0x%04x)" , -IOMMU_TESTDEV_VENDOR_ID ,IOMMU_TESTDEV_DEVICE_ID ); +g_test_message ("Searching for %s (vendor=0x%04x, device=0x%04x)" , + msi ?"edu" :"iommu-testdev" , vendor, device); -qpci_device_foreach (&gbus->bus,IOMMU_TESTDEV_VENDOR_ID , -IOMMU_TESTDEV_DEVICE_ID , save_fn, &dev); +qpci_device_foreach (&gbus->bus, vendor, device, save_fn, &dev);g_assert (dev);qpci_device_enable (dev);@@ -175,17 +197,21 @@ static QPCIDevice *find_iommu_testdev(QGenericPCIBus *gbus, QPCIBar *bar)return dev; } -static bool riscv_iommu_test_setup (RiscvIommuTestState *state) +static bool riscv_iommu_test_setup (RiscvIommuTestState *state,bool msi) {if (!qtest_has_machine ("virt" )) {g_test_skip ("virt machine not available" );return false; } +if (msi && !qtest_has_device ("edu" )) { +g_test_skip ("edu device not available" ); +return false; + } - state->qts =qtest_init ("-machine virt,acpi=off " -"-cpu max -smp 1 -m 512 -net none " -"-device riscv-iommu-pci " -"-device iommu-testdev" ); + state->qts =qtest_initf ("-machine virt,acpi=off " +"-cpu max -smp 1 -m 512 -bios none -net none " +"-device riscv-iommu-pci " +"-device %s" , msi ?"edu" :"iommu-testdev" );qpci_init_generic (&state->gbus, state->qts,NULL , false);riscv_config_qpci_bus (&state->gbus);@@ -193,7 +219,7 @@ static bool riscv_iommu_test_setup(RiscvIommuTestState *state) state->iommu_dev =find_riscv_iommu_pci (&state->gbus, &state->iommu_base);g_assert (state->iommu_dev); - state->testdev =find_iommu_testdev (&state->gbus, &state->testdev_bar); + state->testdev =find_testdev (&state->gbus, &state->testdev_bar, msi);g_assert (state->testdev);return true;@@ -267,7 +293,7 @@ static void run_riscv_iommu_translation(const QRIOMMUTestConfig *cfg) {RiscvIommuTestState state = {0 }; -if (!riscv_iommu_test_setup (&state)) { +if (!riscv_iommu_test_setup (&state, false)) {return ; }@@ -328,6 +354,101 @@ static void test_riscv_iommu_nested(void)run_riscv_iommu_translation (&cfg); } +static void test_riscv_iommu_msi (gconstpointer data) +{ +static const unsigned events[] = { +RISCV_IOMMU_HPMEVENT_URQ , +RISCV_IOMMU_HPMEVENT_TLB_MISS , +RISCV_IOMMU_HPMEVENT_S_VS_WALKS , +RISCV_IOMMU_HPMEVENT_G_WALKS , + }; +const RiscvIommuMsiTest *cfg = data; +const uint64_t expected[] = {1 ,1 , cfg->s_walks, cfg->g_walks }; +RiscvIommuTestState state = {0 }; +uint64_t iova = (QRIOMMU_IOVA & ~0xfffull ) + + (cfg->valid_pte ?0 :0x40 ); +uint64_t gpa = iova; +uint64_t dc_addr, pte =0 , hdr, cap; +uint64_t fq_addr =QRIOMMU_SPACE_OFFS +QRIOMMU_FQ_BASE_ADDR ; +uint8_t msi_cap; +uint16_t flags; + +if (!riscv_iommu_test_setup (&state, true)) { +return ; + } + + cap =riscv_iommu_check (state.qts, state.iommu_base, cfg->mode); + g_assert_cmphex(cap & RISCV_IOMMU_CAP_MSI_FLAT, !=, 0); + g_assert_cmphex(cap & RISCV_IOMMU_CAP_HPM, !=, 0); +qriommu_build_translation (state.qts, cfg->mode, state.testdev->devfn); +if (cfg->mode ==QRIOMMU_TM_NESTED ) { + gpa =QRIOMMU_L2_PTE_VAL + (iova &0xfff ); + g_assert_cmphex(iova, !=, gpa); + } + +/* Match one GPA page; interrupt-file number zero selects the first PTE. */ + dc_addr =QRIOMMU_SPACE_OFFS +QRIOMMU_DC_BASE + + state.testdev->devfn *sizeof (struct riscv_iommu_dc ); +qtest_writeq (state.qts, dc_addr +32 , + (MSI_TABLE_PA >>12 ) | (1ull <<60 )); +qtest_writeq (state.qts, dc_addr +40 ,0 ); +qtest_writeq (state.qts, dc_addr +48 , gpa >>12 ); +qtest_memset (state.qts,MSI_TABLE_PA ,0 ,0x1000 ); +qtest_writel (state.qts,MSI_TARGET_PA ,0 ); +if (cfg->valid_pte) { + pte = ((MSI_TARGET_PA >>12 ) <<10 ) | + (RISCV_IOMMU_MSI_PTE_M_BASIC <<1 ) |RISCV_IOMMU_MSI_PTE_V ; + } +qtest_writeq (state.qts,MSI_TABLE_PA , pte); +qriommu_program_regs (state.qts, state.iommu_base); + +for (unsigned i =0 ; i <G_N_ELEMENTS (events); i++) { +qtest_writeq (state.qts, state.iommu_base +RISCV_IOMMU_REG_IOHPMEVT (i), + events[i]); +qtest_writeq (state.qts, state.iommu_base +RISCV_IOMMU_REG_IOHPMCTR (i), +0 ); +g_assert_cmpuint (qtest_readq (state.qts, + state.iommu_base + RISCV_IOMMU_REG_IOHPMCTR(i)), ==, 0); + } +qtest_writel (state.qts, state.iommu_base +RISCV_IOMMU_REG_IOCOUNTINH , +RISCV_IOMMU_IOCOUNTINH_CY ); + +/* EDU sends a real PCI MSI, including the transaction requester ID. */ + msi_cap =qpci_find_capability (state.testdev,PCI_CAP_ID_MSI ,0 ); + g_assert_cmpuint(msi_cap, !=, 0); + flags =qpci_config_readw (state.testdev, msi_cap +PCI_MSI_FLAGS ); + g_assert_cmpuint(flags & PCI_MSI_FLAGS_64BIT, !=, 0); +qpci_config_writel (state.testdev, msi_cap +PCI_MSI_ADDRESS_LO , iova); +qpci_config_writel (state.testdev, msi_cap +PCI_MSI_ADDRESS_HI , iova >>32 ); +qpci_config_writew (state.testdev, msi_cap +PCI_MSI_DATA_64 ,MSI_DATA ); +qpci_config_writew (state.testdev, msi_cap +PCI_MSI_FLAGS , + flags |PCI_MSI_FLAGS_ENABLE ); +qpci_io_writel (state.testdev, state.testdev_bar,EDU_IRQ_RAISE ,1 ); + +g_assert_cmpuint (qtest_readl (state.qts, + state.iommu_base + RISCV_IOMMU_REG_FQT), ==, !cfg->valid_pte); +if (cfg->valid_pte) { + g_assert_cmphex(qtest_readl(state.qts, MSI_TARGET_PA), ==, MSI_DATA); + }else { + hdr =qtest_readq (state.qts, fq_addr); + g_assert_cmpuint(hdr & RISCV_IOMMU_FQ_HDR_CAUSE, ==, +RISCV_IOMMU_FQ_CAUSE_MSI_INVALID ); + g_assert_cmpuint((hdr & RISCV_IOMMU_FQ_HDR_TTYPE) >> 34, ==, +RISCV_IOMMU_FQ_TTYPE_UADDR_WR ); + g_assert_cmpuint((hdr & RISCV_IOMMU_FQ_HDR_DID) >> 40, ==, + state.testdev->devfn); + g_assert_cmphex(qtest_readq(state.qts, fq_addr + 16), ==, iova); + g_assert_cmphex(qtest_readq(state.qts, fq_addr + 24), ==, 0); + } + +/* Count the original translation, excluding the GPA recovery walk. */ +for (unsigned i =0 ; i <G_N_ELEMENTS (events); i++) { +g_assert_cmpuint (qtest_readq (state.qts, + state.iommu_base + RISCV_IOMMU_REG_IOHPMCTR(i)), ==, expected[i]); + } +riscv_iommu_test_teardown (&state); +} +static void riscv_iommu_dbg_map_leaf (QTestState *qts,uint64_t l1_pa,uint64_t l2_pa,uint64_t va,uint64_t pa,uint64_t page_size)@@ -386,7 +507,7 @@ static void test_riscv_iommu_dbg(gconstpointer data) cfg->g_page_size : cfg->s_page_size;uint64_t response; -if (!riscv_iommu_test_setup (&state)) { +if (!riscv_iommu_test_setup (&state, false)) {return ; }@@ -462,6 +583,12 @@ int main(int argc, char **argv) test_riscv_iommu_g_stage_only);qtest_add_func ("/iommu-testdev/translation/ns-nested" , test_riscv_iommu_nested); +for (unsigned i =0 ; i <G_N_ELEMENTS (msi_tests); i++) { + g_autofree char *path = g_strdup_printf("/riscv-iommu/msi/%s", + msi_tests[i].name); + +qtest_add_data_func (path, &msi_tests[i], test_riscv_iommu_msi); + }for (unsigned i =0 ; i <G_N_ELEMENTS (debug_tests); i++) { g_autofree char *path = g_strdup_printf("/iommu-testdev/debug/%s", debug_tests[i].name);
PATCH 2/3 hw/riscv/riscv-iommu: Fix MSI fault PV reporting for process ID zero
In reply to Zephyr LiMSI fault reporting derives PV from the numeric process ID, so process zero always produces PV=0. Ordinary translation faults and recovery faults instead use the device context's PDTV bit.
Use the same PDTV policy for MSI faults. Add a qtest with a real PD8 process directory and DPE-selected default process zero, reaching an invalid MSI PTE after nonidentity nested translation. Check PV=1, PID=0, and the original IOVA. Also check PV=0 in the existing fault cases without PDTV.
This aligns QEMU's current context-based reporting policy. Preserving the validity of an explicitly supplied request PID separately from its value is outside this change.
--- hw/riscv/riscv-iommu.c | 2 +- tests/qtest/iommu-riscv-test.c | 36 ++++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-)
hw/riscv/riscv-iommu.c +1 -1
@@ -831,7 +831,7 @@ static MemTxResult riscv_iommu_msi_write(RISCVIOMMUState *s, err:riscv_iommu_report_fault (s, ctx, fault_type, cause, - !!ctx->process_id, iova,0 ); + !!(ctx->tc &RISCV_IOMMU_DC_TC_PDTV ), iova,0 );return res; }
tests/qtest/iommu-riscv-test.c +32 -4
@@ -27,6 +27,7 @@#define MSI_TABLE_PA 0x80030000ull#define MSI_TARGET_PA 0x80040000ull +#define MSI_PROCESS_GPA 0x00050000ull#define MSI_DATA 0x37#define EDU_DEVICE_ID 0x11e8#define EDU_IRQ_RAISE 0x60
@@ -62,13 +63,15 @@ typedef struct RiscvIommuMsiTest {bool valid_pte;uint64_t s_walks;uint64_t g_walks; +bool default_process; } RiscvIommuMsiTest;static const RiscvIommuMsiTest msi_tests[] = { - {"first-stage-bare-fault" ,QRIOMMU_TM_G_STAGE_ONLY , false,0 ,0 }, - {"nested-fault" ,QRIOMMU_TM_NESTED , false,3 ,9 }, - {"first-stage-bare-basic" ,QRIOMMU_TM_G_STAGE_ONLY , true,0 ,0 }, - {"nested-basic" ,QRIOMMU_TM_NESTED , true,3 ,9 }, + {"first-stage-bare-fault" ,QRIOMMU_TM_G_STAGE_ONLY , false,0 ,0 , false }, + {"nested-fault" ,QRIOMMU_TM_NESTED , false,3 ,9 , false }, + {"first-stage-bare-basic" ,QRIOMMU_TM_G_STAGE_ONLY , true,0 ,0 , false }, + {"nested-basic" ,QRIOMMU_TM_NESTED , true,3 ,9 , false }, + {"nested-pdtv-pid0-fault" ,QRIOMMU_TM_NESTED , false,3 ,9 , true }, };static const RiscvIommuDbgTest debug_tests[] = {
@@ -389,6 +392,27 @@ static void test_riscv_iommu_msi(gconstpointer data)/* Match one GPA page; interrupt-file number zero selects the first PTE. */ dc_addr =QRIOMMU_SPACE_OFFS +QRIOMMU_DC_BASE + state.testdev->devfn *sizeof (struct riscv_iommu_dc ); +if (cfg->default_process) { +uint64_t process_pa =QRIOMMU_SPACE_OFFS +MSI_PROCESS_GPA ; +uint64_t g_leaf_pa =QRIOMMU_SPACE_OFFS +QRIOMMU_G_L1_PTE_VAL ; +uint64_t fsc =qtest_readq (state.qts, dc_addr +24 ); + + g_assert_cmpuint(cfg->mode, ==, QRIOMMU_TM_NESTED); + g_assert_cmphex(cap & RISCV_IOMMU_CAP_PD8, !=, 0); +/* Map the PD8 table through G-stage; PID zero selects its first PC. */ +qtest_writeq (state.qts, + g_leaf_pa + ((MSI_PROCESS_GPA >>12 ) &0x1ff ) *sizeof (uint64_t ), + ((process_pa >>12 ) <<10 ) |QRIOMMU_LEAF_PTE_RW_MASK ); +qtest_memset (state.qts, process_pa,0 ,0x1000 ); +qtest_writeq (state.qts, process_pa,RISCV_IOMMU_PC_TA_V ); +qtest_writeq (state.qts, process_pa +8 , fsc); +qtest_writeq (state.qts, dc_addr, +RISCV_IOMMU_DC_TC_V |RISCV_IOMMU_DC_TC_PDTV | +RISCV_IOMMU_DC_TC_DPE ); +qtest_writeq (state.qts, dc_addr +24 , + (MSI_PROCESS_GPA >>12 ) | + ((uint64_t )RISCV_IOMMU_DC_FSC_PDTP_MODE_PD8 <<60 )); + }qtest_writeq (state.qts, dc_addr +32 , (MSI_TABLE_PA >>12 ) | (1ull <<60 ));qtest_writeq (state.qts, dc_addr +40 ,0 );
@@ -437,6 +461,10 @@ static void test_riscv_iommu_msi(gconstpointer data) RISCV_IOMMU_FQ_TTYPE_UADDR_WR); g_assert_cmpuint((hdr & RISCV_IOMMU_FQ_HDR_DID) >> 40, ==, state.testdev->devfn); +/* Check QEMU's PDTV policy using DPE, without an EDU PASID. */ + g_assert_cmpuint(!!(hdr & RISCV_IOMMU_FQ_HDR_PV), ==, + cfg->default_process); + g_assert_cmphex(hdr & RISCV_IOMMU_FQ_HDR_PID, ==, 0); g_assert_cmphex(qtest_readq(state.qts, fq_addr + 16), ==, iova); g_assert_cmphex(qtest_readq(state.qts, fq_addr + 24), ==, 0); }
Signature · 1 line
2.43.0
Re: [PATCH 2/3] hw/riscv/riscv-iommu: Fix MSI fault PV reporting for process ID zero
Quoted 10 lines · Zephyr Li
MSI fault reporting derives PV from the numeric process ID, so process zero always produces PV=0. Ordinary translation faults and recovery faults instead use the device context's PDTV bit.
Use the same PDTV policy for MSI faults. Add a qtest with a real PD8 process directory and DPE-selected default process zero, reaching an invalid MSI PTE after nonidentity nested translation. Check PV=1, PID=0, and the original IOVA. Also check PV=0 in the existing fault cases without PDTV.
Quoted 7 lines
This aligns QEMU's current context-based reporting policy. Preserving the validity of an explicitly supplied request PID separately from its value is outside this change.
Fixes 0c54acb8243d ("hw/riscv: add RISC-V IOMMU base emulation")Signed-off-by Zephyr Li <[email protected]>---
Quoted 88 lines
hw/riscv/riscv-iommu.c | 2 +- tests/qtest/iommu-riscv-test.c | 36 ++++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-)hw/riscv/riscv-iommu.c +1
-1@@ -831,7 +831,7 @@ static MemTxResult riscv_iommu_msi_write(RISCVIOMMUState *s, err:riscv_iommu_report_fault (s, ctx, fault_type, cause, - !!ctx->process_id, iova,0 ); + !!(ctx->tc &RISCV_IOMMU_DC_TC_PDTV ), iova,0 );return res; }tests/qtest/iommu-riscv-test.c +32
-4@@ -27,6 +27,7 @@#define MSI_TABLE_PA 0x80030000ull#define MSI_TARGET_PA 0x80040000ull +#define MSI_PROCESS_GPA 0x00050000ull#define MSI_DATA 0x37#define EDU_DEVICE_ID 0x11e8#define EDU_IRQ_RAISE 0x60@@ -62,13 +63,15 @@ typedef struct RiscvIommuMsiTest {bool valid_pte;uint64_t s_walks;uint64_t g_walks; +bool default_process; } RiscvIommuMsiTest;static const RiscvIommuMsiTest msi_tests[] = { - {"first-stage-bare-fault" ,QRIOMMU_TM_G_STAGE_ONLY , false,0 ,0 }, - {"nested-fault" ,QRIOMMU_TM_NESTED , false,3 ,9 }, - {"first-stage-bare-basic" ,QRIOMMU_TM_G_STAGE_ONLY , true,0 ,0 }, - {"nested-basic" ,QRIOMMU_TM_NESTED , true,3 ,9 }, + {"first-stage-bare-fault" ,QRIOMMU_TM_G_STAGE_ONLY , false,0 ,0 , false }, + {"nested-fault" ,QRIOMMU_TM_NESTED , false,3 ,9 , false }, + {"first-stage-bare-basic" ,QRIOMMU_TM_G_STAGE_ONLY , true,0 ,0 , false }, + {"nested-basic" ,QRIOMMU_TM_NESTED , true,3 ,9 , false }, + {"nested-pdtv-pid0-fault" ,QRIOMMU_TM_NESTED , false,3 ,9 , true }, };static const RiscvIommuDbgTest debug_tests[] = {@@ -389,6 +392,27 @@ static void test_riscv_iommu_msi(gconstpointer data)/* Match one GPA page; interrupt-file number zero selects the first PTE. */ dc_addr =QRIOMMU_SPACE_OFFS +QRIOMMU_DC_BASE + state.testdev->devfn *sizeof (struct riscv_iommu_dc ); +if (cfg->default_process) { +uint64_t process_pa =QRIOMMU_SPACE_OFFS +MSI_PROCESS_GPA ; +uint64_t g_leaf_pa =QRIOMMU_SPACE_OFFS +QRIOMMU_G_L1_PTE_VAL ; +uint64_t fsc =qtest_readq (state.qts, dc_addr +24 ); + + g_assert_cmpuint(cfg->mode, ==, QRIOMMU_TM_NESTED); + g_assert_cmphex(cap & RISCV_IOMMU_CAP_PD8, !=, 0); +/* Map the PD8 table through G-stage; PID zero selects its first PC. */ +qtest_writeq (state.qts, + g_leaf_pa + ((MSI_PROCESS_GPA >>12 ) &0x1ff ) *sizeof (uint64_t ), + ((process_pa >>12 ) <<10 ) |QRIOMMU_LEAF_PTE_RW_MASK ); +qtest_memset (state.qts, process_pa,0 ,0x1000 ); +qtest_writeq (state.qts, process_pa,RISCV_IOMMU_PC_TA_V ); +qtest_writeq (state.qts, process_pa +8 , fsc); +qtest_writeq (state.qts, dc_addr, +RISCV_IOMMU_DC_TC_V |RISCV_IOMMU_DC_TC_PDTV | +RISCV_IOMMU_DC_TC_DPE ); +qtest_writeq (state.qts, dc_addr +24 , + (MSI_PROCESS_GPA >>12 ) | + ((uint64_t )RISCV_IOMMU_DC_FSC_PDTP_MODE_PD8 <<60 )); + }qtest_writeq (state.qts, dc_addr +32 , (MSI_TABLE_PA >>12 ) | (1ull <<60 ));qtest_writeq (state.qts, dc_addr +40 ,0 );@@ -437,6 +461,10 @@ static void test_riscv_iommu_msi(gconstpointer data) RISCV_IOMMU_FQ_TTYPE_UADDR_WR); g_assert_cmpuint((hdr & RISCV_IOMMU_FQ_HDR_DID) >> 40, ==, state.testdev->devfn); +/* Check QEMU's PDTV policy using DPE, without an EDU PASID. */ + g_assert_cmpuint(!!(hdr & RISCV_IOMMU_FQ_HDR_PV), ==, + cfg->default_process); + g_assert_cmphex(hdr & RISCV_IOMMU_FQ_HDR_PID, ==, 0); g_assert_cmphex(qtest_readq(state.qts, fq_addr + 16), ==, iova); g_assert_cmphex(qtest_readq(state.qts, fq_addr + 24), ==, 0); }
PATCH 3/3 hw/riscv/riscv-iommu: Add unit tests for MSI trap handling
In reply to Zephyr LiThe recovery-fault and ordinary-DMA fallback paths are difficult to exercise deterministically with a qtest that updates page tables between the initial translation and the synchronous trap callback.
Move the private callback into an implementation fragment shared with a unit test. Use real QEMU types and replace the callback dependencies only in the test translation unit, without exporting a new API or adding runtime test hooks.
Cover rejected attributes, missing contexts, MSI forwarding, recovery faults, and 4/8-byte fallback writes with success and error returns. Check the addresses, little-endian bytes, transaction attributes, context release, single-fault behavior, and disabled replay HPM accounting. The PV assertions describe the existing callback policy, not complete request-PID validity handling.
Register the test for system builds and add it to the RISC-V MAINTAINERS entry. Use the usual pointer declaration in the moved callback. The production behavior is unchanged.
Use a distinct opaque reference in the context stub so the cleanup checks distinguish the returned context from its cache-lifetime reference.
--- MAINTAINERS | 1 + hw/riscv/riscv-iommu-trap.c.inc | 69 ++++++++ hw/riscv/riscv-iommu.c | 61 +------ tests/unit/meson.build | 1 + tests/unit/test-riscv-iommu-trap.c | 266 +++++++++++++++++++++++++++++ 5 files changed, 338 insertions(+), 60 deletions(-) create mode 100644 hw/riscv/riscv-iommu-trap.c.inc create mode 100644 tests/unit/test-riscv-iommu-trap.c
MAINTAINERS +1 -0
@@ -387,6 +387,7 @@ F: tests/functional/riscv64 F: tests/tcg/riscv32/ F: tests/tcg/riscv64/ F: tests/qtest/iommu-riscv-test.c +F: tests/unit/test-riscv-iommu-trap.c RISC-V XThead* extensions M: Christoph Muellner <[email protected]>
hw/riscv/riscv-iommu-trap.c.inc new file +69 -0
69 lines not on this page · raw message
hw/riscv/riscv-iommu.c +1 -60
67 lines not on this page · raw message
tests/unit/meson.build +1 -0
@@ -132,6 +132,7 @@ if have_system 'test-iov': [], 'test-opts-visitor': [testqapi], 'test-xs-node': [qom], + 'test-riscv-iommu-trap': [], 'test-virtio-dmabuf': [meson.project_source_root() / 'hw/display/virtio-dmabuf.c'], 'test-qmp-cmds': [testqapi], 'test-xbzrle': [migration],
tests/unit/test-riscv-iommu-trap.c new file +266 -0
266 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH 3/3] hw/riscv/riscv-iommu: Add unit tests for MSI trap handling
Quoted 20 lines · Zephyr Li
The recovery-fault and ordinary-DMA fallback paths are difficult to exercise deterministically with a qtest that updates page tables between the initial translation and the synchronous trap callback.
Move the private callback into an implementation fragment shared with a unit test. Use real QEMU types and replace the callback dependencies only in the test translation unit, without exporting a new API or adding runtime test hooks.
Cover rejected attributes, missing contexts, MSI forwarding, recovery faults, and 4/8-byte fallback writes with success and error returns. Check the addresses, little-endian bytes, transaction attributes, context release, single-fault behavior, and disabled replay HPM accounting. The PV assertions describe the existing callback policy, not complete request-PID validity handling.
Register the test for system builds and add it to the RISC-V
MAINTAINERSentry. Use the usual pointer declaration in the moved callback. The production behavior is unchanged.
Quoted 6 lines
Use a distinct opaque reference in the context stub so the cleanup checks distinguish the returned context from its cache-lifetime reference.
Signed-off-by Zephyr Li <[email protected]>---
Quoted 452 lines
MAINTAINERS | 1 + hw/riscv/riscv-iommu-trap.c.inc | 69 ++++++++ hw/riscv/riscv-iommu.c | 61 +------ tests/unit/meson.build | 1 + tests/unit/test-riscv-iommu-trap.c | 266 +++++++++++++++++++++++++++++ 5 files changed, 338 insertions(+), 60 deletions(-)create mode 100644 hw/riscv/riscv-iommu-trap.c.inc create mode 100644
tests/unit/test-riscv-iommu-trap.cMAINTAINERS +1
-0@@ -387,6 +387,7 @@ F: tests/functional/riscv64 F: tests/tcg/riscv32/ F: tests/tcg/riscv64/ F: tests/qtest/iommu-riscv-test.c +F: tests/unit/test-riscv-iommu-trap.c RISC-V XThead* extensions M: Christoph Muellner <[email protected]>hw/riscv/riscv-iommu-trap.c.inc new file +69
-0@@ -0,0 +1,69 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Private RISC-V IOMMU MSI trap callback + * + * Copyright (C) 2021-2023, Rivos Inc. + */ + +/* Shared privately with the deterministic trap callback unit tests. */ + +/* + * Translations matching MSI pattern check are redirected to "riscv-iommu-trap" + * memory region as untranslated address, for additional MSI/MRIF interception + * by IOMMU interrupt remapping implementation. + * Note: Device emulation code generating an MSI is expected to provide a valid + * memory transaction attributes with requested_id set. + */ +static MemTxResult riscv_iommu_trap_write(void *opaque, hwaddr addr, + uint64_t data, unsigned size, MemTxAttrs attrs) +{ + RISCVIOMMUState *s = opaque; + RISCVIOMMUContext *ctx; + MemTxResult res; + void *ref; + uint32_t devid = attrs.requester_id; + IOMMUTLBEntry iotlb = { + .iova = addr, + .target_as = s->target_as, + .addr_mask = ~0ULL, + .perm = IOMMU_WO, + }; + int cause; + + if (attrs.unspecified) { + return MEMTX_ACCESS_ERROR; + } + + /* FIXME: PCIe bus remapping for attached endpoints. */ + devid |= s->bus << 8; + + ctx = riscv_iommu_ctx(s, devid, attrs.pid, IOMMU_WO, addr, &ref); + if (ctx == NULL) { + res = MEMTX_ACCESS_ERROR; + } else { + /* + * The trap address is the original IOVA. Resolve the MSI GPA + * without consulting or updating the translation cache. + * Exclude this internal replay from the modeled HPM events. + */ + cause = riscv_iommu_spa_fetch(s, ctx, &iotlb, false); + if (cause) { + riscv_iommu_report_fault(s, ctx, + RISCV_IOMMU_FQ_TTYPE_UADDR_WR, cause, + !!(ctx->tc & RISCV_IOMMU_DC_TC_PDTV), + addr, iotlb.translated_addr); + res = MEMTX_ACCESS_ERROR; + } else if (iotlb.target_as == &s->trap_as) { + res = riscv_iommu_msi_write(s, ctx, addr, + iotlb.translated_addr, data, + size, attrs); + } else { + /* The mapping may have changed since the original walk. */ + data = cpu_to_le64(data); + res = dma_memory_write(iotlb.target_as, iotlb.translated_addr, + &data, size, attrs); + } + } + riscv_iommu_ctx_put(s, ref); + return res; +}hw/riscv/riscv-iommu.c +1
-60@@ -2599,66 +2599,7 @@ static const MemoryRegionOps riscv_iommu_mmio_ops = { } }; -/* -* Translations matching MSI pattern check are redirected to "riscv-iommu-trap" -* memory region as untranslated address, for additional MSI/MRIF interception -* by IOMMU interrupt remapping implementation. -* Note: Device emulation code generating an MSI is expected to provide a valid -* memory transaction attributes with requested_id set. -*/ -static MemTxResult riscv_iommu_trap_write (void *opaque,hwaddr addr, -uint64_t data,unsigned size,MemTxAttrs attrs) -{ -RISCVIOMMUState * s = (RISCVIOMMUState *)opaque; -RISCVIOMMUContext *ctx; -MemTxResult res; -void *ref; -uint32_t devid = attrs.requester_id; -IOMMUTLBEntry iotlb = { - .iova = addr, - .target_as = s->target_as, - .addr_mask = ~0ULL , - .perm =IOMMU_WO , - }; -int cause; - -if (attrs.unspecified) { -return MEMTX_ACCESS_ERROR ; - } - -/* FIXME: PCIe bus remapping for attached endpoints. */ - devid |= s->bus <<8 ; - - ctx =riscv_iommu_ctx (s, devid, attrs.pid,IOMMU_WO , addr, &ref); -if (ctx ==NULL ) { - res =MEMTX_ACCESS_ERROR ; - }else { -/* -* The trap address is the original IOVA. Resolve the MSI GPA -* without consulting or updating the translation cache. -* Exclude this internal replay from the modeled HPM events. -*/ - cause =riscv_iommu_spa_fetch (s, ctx, &iotlb, false); -if (cause) { -riscv_iommu_report_fault (s, ctx, -RISCV_IOMMU_FQ_TTYPE_UADDR_WR , cause, - !!(ctx->tc &RISCV_IOMMU_DC_TC_PDTV ), - addr, iotlb.translated_addr); - res =MEMTX_ACCESS_ERROR ; - }else if (iotlb.target_as == &s->trap_as) { - res =riscv_iommu_msi_write (s, ctx, addr, - iotlb.translated_addr, data, - size, attrs); - }else { -/* The mapping may have changed since the original walk. */ - data =cpu_to_le64 (data); - res =dma_memory_write (iotlb.target_as, iotlb.translated_addr, - &data, size, attrs); - } - } -riscv_iommu_ctx_put (s, ref); -return res; -} +#include "riscv-iommu-trap.c.inc" static MemTxResult riscv_iommu_trap_read (void *opaque,hwaddr addr,uint64_t *data,unsigned size,MemTxAttrs attrs)tests/unit/meson.build +1
-0@@ -132,6 +132,7 @@ if have_system 'test-iov': [], 'test-opts-visitor': [testqapi], 'test-xs-node': [qom], + 'test-riscv-iommu-trap': [], 'test-virtio-dmabuf': [meson.project_source_root() / 'hw/display/virtio-dmabuf.c'], 'test-qmp-cmds': [testqapi], 'test-xbzrle': [migration],tests/unit/test-riscv-iommu-trap.c new file +266
-0@@ -0,0 +1,266 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* Deterministic tests of the private RISC-V IOMMU MSI trap callback. */ +#include "qemu/osdep.h" +#include "qemu/bswap.h" +#include "hw/riscv/riscv-iommu.h" + +#define TEST_IOVA 0x400040ull +#define TEST_GPA 0x28000040ull +#define TEST_DATA 0x1122334455667788ull + +static struct { +RISCVIOMMUState state; +RISCVIOMMUContext ctx; +unsigned ref_cookie; +AddressSpace physical_as; +AddressSpace changed_as; +MemTxAttrs attrs; +bool missing_ctx; +int walk_fault; +AddressSpace *walk_as; +hwaddr walk_addr; +MemTxResult msi_result; +MemTxResult dma_result; +unsigned size; +unsigned ctx_calls; +unsigned put_calls; +unsigned walk_calls; +unsigned msi_calls; +unsigned fault_calls; +unsigned dma_calls; +} test; + +static void assert_attrs (MemTxAttrs attrs) +{ + g_assert_cmpuint(attrs.requester_id, ==, test.attrs.requester_id); + g_assert_cmpuint(attrs.pid, ==, test.attrs.pid); + g_assert_cmpuint(attrs.secure, ==, test.attrs.secure); + g_assert_cmpuint(attrs.space, ==, test.attrs.space); + g_assert_cmpuint(attrs.user, ==, test.attrs.user); + g_assert_cmpuint(attrs.memory, ==, test.attrs.memory); + g_assert_cmpuint(attrs.debug, ==, test.attrs.debug); + g_assert_cmpuint(attrs.address_type, ==, test.attrs.address_type); + g_assert_cmpuint(attrs.unspecified, ==, test.attrs.unspecified); +} + +static RISCVIOMMUContext *test_ctx (RISCVIOMMUState *s,unsigned devid, +unsigned pid,IOMMUAccessFlags perm, +uint64_t iova,void **ref) +{ +g_assert_true (s == &test.state); + g_assert_cmpuint(devid, ==, + test.attrs.requester_id | (test.state.bus <<8 )); + g_assert_cmpuint(pid, ==, test.attrs.pid); + g_assert_cmpuint(perm, ==, IOMMU_WO); + g_assert_cmphex(iova, ==, TEST_IOVA); + test.ctx_calls++; +/* Cached contexts return a separate reference for ctx_put(). */ + *ref = test.missing_ctx ?NULL : &test.ref_cookie; +return test.missing_ctx ?NULL : &test.ctx; +} + +static void test_ctx_put (RISCVIOMMUState *s,void *ref) +{ +g_assert_true (s == &test.state); +g_assert_true (ref == (test.missing_ctx ?NULL : &test.ref_cookie)); + test.put_calls++; +} + +static int test_spa_fetch (RISCVIOMMUState *s,RISCVIOMMUContext *ctx, +IOMMUTLBEntry *iotlb,bool update_hpm) +{ +g_assert_true (s == &test.state); +g_assert_true (ctx == &test.ctx); + g_assert_cmphex(iotlb->iova, ==, TEST_IOVA); +g_assert_true (iotlb->target_as == &test.physical_as); + g_assert_cmphex(iotlb->addr_mask, ==, UINT64_MAX); + g_assert_cmpuint(iotlb->perm, ==, IOMMU_WO); +g_assert_false (update_hpm); + test.walk_calls++; + iotlb->target_as = test.walk_as; + iotlb->translated_addr = test.walk_addr; +return test.walk_fault; +} + +static MemTxResult test_msi_write (RISCVIOMMUState *s, +RISCVIOMMUContext *ctx,uint64_t iova, +uint64_t gpa,uint64_t data,unsigned size, +MemTxAttrs attrs) +{ +g_assert_true (s == &test.state); +g_assert_true (ctx == &test.ctx); + g_assert_cmphex(iova, ==, TEST_IOVA); + g_assert_cmphex(gpa, ==, test.walk_addr); + g_assert_cmphex(iova, !=, gpa); + g_assert_cmphex(data, ==, TEST_DATA); + g_assert_cmpuint(size, ==, test.size); +assert_attrs (attrs); + test.msi_calls++; +return test.msi_result; +} + +static void test_report_fault (RISCVIOMMUState *s,RISCVIOMMUContext *ctx, +uint32_t ttype,uint32_t cause,bool pv, +uint64_t iotval,uint64_t iotval2) +{ +g_assert_true (s == &test.state); +g_assert_true (ctx == &test.ctx); + g_assert_cmpuint(ttype, ==, RISCV_IOMMU_FQ_TTYPE_UADDR_WR); + g_assert_cmpuint(cause, ==, test.walk_fault); +/* Check the existing callback policy, not the complete PID/DPE model. */ + g_assert_cmpuint(pv, ==, !!(test.ctx.tc & RISCV_IOMMU_DC_TC_PDTV)); + g_assert_cmphex(iotval, ==, TEST_IOVA); + g_assert_cmphex(iotval2, ==, test.walk_addr); + test.fault_calls++; +} + +static MemTxResult test_dma_write (AddressSpace *as,dma_addr_t addr, +const void *buf,dma_addr_t len, +MemTxAttrs attrs) +{ +static const uint8_t expected[] = { +0x88 ,0x77 ,0x66 ,0x55 ,0x44 ,0x33 ,0x22 ,0x11 , + }; + +g_assert_true (as == &test.changed_as); + g_assert_cmphex(addr, ==, test.walk_addr); + g_assert_cmpuint(len, ==, test.size); +g_assert_cmpmem (buf, len, expected, len); +assert_attrs (attrs); + test.dma_calls++; +return test.dma_result; +} + +/* Redirect dependencies only while compiling the private implementation. */ +#define riscv_iommu_ctx test_ctx +#define riscv_iommu_ctx_put test_ctx_put +#define riscv_iommu_spa_fetch test_spa_fetch +#define riscv_iommu_msi_write test_msi_write +#define riscv_iommu_report_fault test_report_fault +#define dma_memory_write test_dma_write +#include "hw/riscv/riscv-iommu-trap.c.inc" +#undef riscv_iommu_ctx +#undef riscv_iommu_ctx_put +#undef riscv_iommu_spa_fetch +#undef riscv_iommu_msi_write +#undef riscv_iommu_report_fault +#undef dma_memory_write + +static void reset_test (void ) +{ +memset (&test,0 ,sizeof (test)); + test.state.target_as = &test.physical_as; + test.state.bus =0x80 ; + test.attrs = (MemTxAttrs ) { + .requester_id =0x3456 , + .pid =0x5a , + .secure = true, + .space =1 , + .user = true, + .memory = true, + .debug = true, + .address_type =1 , + }; + test.ctx.process_id = test.attrs.pid; + test.walk_as = &test.state.trap_as; + test.walk_addr =TEST_GPA ; + test.size =4 ; +} + +static MemTxResult trap_write (void ) +{ +return riscv_iommu_trap_write (&test.state,TEST_IOVA ,TEST_DATA , + test.size, test.attrs); +} + +static void assert_calls (unsigned ctx,unsigned walk,unsigned msi, +unsigned fault,unsigned dma) +{ + g_assert_cmpuint(test.ctx_calls, ==, ctx); + g_assert_cmpuint(test.put_calls, ==, ctx); + g_assert_cmpuint(test.walk_calls, ==, walk); + g_assert_cmpuint(test.msi_calls, ==, msi); + g_assert_cmpuint(test.fault_calls, ==, fault); + g_assert_cmpuint(test.dma_calls, ==, dma); +} + +static void test_unspecified (void ) +{ +reset_test (); + test.attrs.unspecified = true; + g_assert_cmpuint(trap_write(), ==, MEMTX_ACCESS_ERROR); +assert_calls (0 ,0 ,0 ,0 ,0 ); +} + +static void test_missing_context (void ) +{ +reset_test (); + test.missing_ctx = true; + g_assert_cmpuint(trap_write(), ==, MEMTX_ACCESS_ERROR); +assert_calls (1 ,0 ,0 ,0 ,0 ); +} + +static void test_msi_forward (void ) +{ +reset_test (); + test.size =8 ; + test.msi_result =MEMTX_ERROR ; + g_assert_cmpuint(trap_write(), ==, MEMTX_ERROR); +assert_calls (1 ,1 ,1 ,0 ,0 ); +} + +static void test_rewalk_fault (gconstpointer data) +{ +reset_test (); + test.attrs.pid =0 ; + test.ctx.process_id =0 ; + test.ctx.tc = data ?RISCV_IOMMU_DC_TC_PDTV :0 ; + test.walk_fault =RISCV_IOMMU_FQ_CAUSE_WR_FAULT_VS ; + g_assert_cmpuint(trap_write(), ==, MEMTX_ACCESS_ERROR); +assert_calls (1 ,1 ,0 ,1 ,0 ); +} + +typedef struct FallbackCase { +const char *name; +unsigned size; +MemTxResult result; +}FallbackCase ; + +static void test_fallback (gconstpointer data) +{ +const FallbackCase *cfg = data; + +reset_test (); + test.size = cfg->size; + test.walk_as = &test.changed_as; + test.walk_addr =0x90000040 ; + test.dma_result = cfg->result; + g_assert_cmpuint(trap_write(), ==, cfg->result); +assert_calls (1 ,1 ,0 ,0 ,1 ); +} + +int main (int argc,char **argv) +{ +static const FallbackCase fallback_cases[] = { + {"fallback-4-success" ,4 ,MEMTX_OK }, + {"fallback-4-error" ,4 ,MEMTX_ERROR }, + {"fallback-8-success" ,8 ,MEMTX_OK }, + {"fallback-8-error" ,8 ,MEMTX_ERROR }, + }; + +g_test_init (&argc, &argv,NULL ); +g_test_add_func ("/riscv-iommu/trap/unspecified" , test_unspecified); +g_test_add_func ("/riscv-iommu/trap/missing-context" , test_missing_context); +g_test_add_func ("/riscv-iommu/trap/msi-forward" , test_msi_forward); +g_test_add_data_func ("/riscv-iommu/trap/rewalk-fault-pdtv-off" ,NULL , + test_rewalk_fault); +g_test_add_data_func ("/riscv-iommu/trap/rewalk-fault-pdtv-on" , +GUINT_TO_POINTER (1 ), test_rewalk_fault); +for (unsigned i =0 ; i <G_N_ELEMENTS (fallback_cases); i++) { + g_autofree char *path = g_strdup_printf("/riscv-iommu/trap/%s", + fallback_cases[i].name); + +g_test_add_data_func (path, &fallback_cases[i], test_fallback); + } +return g_test_run (); +}