From nobody Mon Nov 23 04:34:28 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1794815601; cv=none; d=zohomail.com; s=zohoarc; b=SULIKdznGUqqcNdJ8SfoljBTDBnGOTgcM1mxUqmv4ZPFnl2j6BZ5jSM9TKwh8ADKOxzA5+CCbEsE0Vym/niWRVC/upuEC5yWiUWGtT+k2y36k66H/VaT6ij5UAINgbEJjwB+5TD2zkyUoeVLFXfZQ+LGM+nuX5gl7nTPvoK2pbo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794815601; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=4yaVM/4Ndz0g7rHFZsC1PhMy0pfcgoOnC4kFkKQPDt4=; b=F4w22WTkwFLqbt0/In8E1FIhbYRHn9sGloshQG/KjCNkFdLQvzwWv0vQlz6mwFeeMJkrCcpanafc7codYKjmOi9gQL9VeZa26vCCtfR7Jr2XBk/R7JM01PHf7d7TxdA864j5rz9vj2+UXU/KtntyLyMz+cZK1JNgqjaO90fNYbc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1794815601826799.0857387369927; Sun, 15 Nov 2026 23:53:21 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xSrWA-0002XT-ET; Mon, 16 Nov 2026 02:52:46 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xSrW8-0002Wi-VU for qemu-devel@nongnu.org; Mon, 16 Nov 2026 02:52:45 -0500 Received: from mail-pj2-x05.google.com ([2607:f8b0:4864:39::5]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xSrW6-0006PT-1M for qemu-devel@nongnu.org; Mon, 16 Nov 2026 02:52:44 -0500 Received: by mail-pj2-x05.google.com with SMTP id 98e67ed59e1d1-3aec0b1d816so1689970a91.0 for ; Sun, 15 Nov 2026 23:52:41 -0800 (PST) Received: from Dell-WorkStation.localdomain ([152.175.67.81]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b197d0f6e0sm20745622a91.9.2026.11.15.23.52.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Nov 2026 23:52:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794815561; x=1795420361; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=4yaVM/4Ndz0g7rHFZsC1PhMy0pfcgoOnC4kFkKQPDt4=; b=yZE2twhQrQtKdGXZ7PqONdZwxivxy037SRBGMm0OOb/FXB8AXluoym3ywuBDUoX1JQ x+UFv9UuFNfEhJsO645aBEnCUQap6oWq6wsF9pq4M+sZghpLZzGD/LzbgpFKoONbOeoW VMc2ZsSI67myVL+ySB+Woj53CHiYOY/5M9Lgo/VZzP05syrBg9CU8H41oUUC1cOmz5F9 feKGEO0mieehs6KLrTVHMWKOsRcCs1JMqM0Jqo/nwt3ubojeGE/R0Y473JXadT6fUqXn 0Q2cxZq41VCELB+/zhQR/LbOU6VaVc8DhGJ4RMTiIa4iA4lUa1CJ5+UIRXBw3vDsyUY9 j42g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794815561; x=1795420361; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=4yaVM/4Ndz0g7rHFZsC1PhMy0pfcgoOnC4kFkKQPDt4=; b=sBTN5NDwBAsxMoNw0vL+XWlLh+0JabzO4Nyy8Z7FyLx6TEqxpNqeeQgTMXAm635yE1 RLRnRtci/dUMVX+6ChHkBPPjDaJ51xEZLWENPf9km9ViFew39OjRG/CRAxXPeLWIS8qV 5HQMN6EOdCwY73fyZBNBG40F0H1xsIwXwTLNt4iyckG3tZj5uz+5xjLTm5rwZT+Z1n/n IEqiFSHMXrKIA+LTtYWmDLKE9bZcHPGvVQ6MAsTdLOEkXC/zB+I6GGJEYPiC/5g8V4Fd 4yVblzDrE6dcR0VM6q89Q5RW493EsB9+bUE/tnvWOTWHNhBDe9r2VfDahpbQm/53Z50B 7o6Q== X-Gm-Message-State: APuU4Z8f/fixH9ko49j54D7w8/zpX9tm7jfgXbp7uCsTdtOfsVJ1wbAW vTNJaMBE4yByBqf8wF5V2GrvU1nMhP0nOGS1Anzr0cg4l38XRwogvwuyaXQrqRGzUiILN/XN63s = X-Gm-Gg: AZFdTZfJ2IHmJsBXUyISboJugjKve+hAm0WJHHsHkij4HFvsfjzThS4Gwjad8R+RVW0 IQJHzf+QM1dPxc6CC+qRh1HAjLuVTjo/VvVPmqVgbLiAwKZlps4u/HULUUD+iYGvkKCQSKjLzt3 aLn6G2vJteTefO7n+zMLKXfGBSTS6cMd95ObF3h0e+EAqyPcSwmZqSpBdcC+LNF8hIUKLYp/4kD cSEQ6hCXP5X1Dj1ik4f4afLKfgQ8ItACpvQPe86wBTzsyFcgD4Nv4DIgg/BH/6+lRX7r5Apz0YC t0Lq0vS4jBh0LjdUFnXc9CMfhajhODo1QruubNEDTnqHBZW/+3NGzEIkSmS6rcdW15bv1Ybi3AD 7OfuyDpOGWAN/JJUvlLSGhmVdXae9ONr1OPvCSpUmM23i4KQimJglJxIuBBPMecA4gTy5wSRQpB P97APWx7PehWwivZsIqnpgND68mDX9yahICy8WqPs9uoK7dopSytryDBJOlcbOAOJfca/8rknvy qsJM4OCF0D6rjTclT7HQl0G5Gfv9ubz2gPK0T1/J+1pVrEARE4sjZ5+T4ExgT6uQKsdnpVZih/a 8eInQZNKEVDPSrIJli2JH1ZweUJ7sTQrdWRMsdncvN6RC3St06OLbOqc X-Received: by 2002:a17:90b:2250:b0:3ac:7556:8ee6 with SMTP id 98e67ed59e1d1-3b1b0b668c7mr9870592a91.16.1794815560139; Sun, 15 Nov 2026 23:52:40 -0800 (PST) From: Zephyr Li To: qemu-devel@nongnu.org Cc: palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, daniel.barboza@oss.qualcomm.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, qemu-riscv@nongnu.org, farosas@suse.de, lvivier@redhat.com, pbonzini@redhat.com Subject: [PATCH 1/3] hw/riscv/riscv-iommu: Preserve IOVA in MSI fault records Date: Mon, 16 Nov 2026 15:51:39 +0800 Message-ID: <20261116075141.167317-2-fritchleybohrer@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261116075141.167317-1-fritchleybohrer@gmail.com> References: <20261116075141.167317-1-fritchleybohrer@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:39::5; envelope-from=fritchleybohrer@gmail.com; helo=mail-pj2-x05.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_FILL_THIS_FORM_SHORT=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1794815602748158500 Content-Type: text/plain; charset="utf-8" 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. 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 --- hw/riscv/riscv-iommu.c | 60 ++++++++++--- tests/qtest/iommu-riscv-test.c | 153 ++++++++++++++++++++++++++++++--- 2 files changed, 189 insertions(+), 24 deletions(-) diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index 0e6970773a..016e95fce7 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -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 *ct= x, - IOMMUTLBEntry *iotlb) + IOMMUTLBEntry *iotlb, bool update_hpm) { IOMMUAccessFlags trans_perm =3D IOMMU_NONE; dma_addr_t addr, base; @@ -456,10 +457,14 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, = RISCVIOMMUContext *ctx, } =20 =20 - if (pass =3D=3D S_STAGE) { - riscv_iommu_hpm_incr_ctr(s, ctx, RISCV_IOMMU_HPMEVENT_S_VS_WAL= KS); - } else { - riscv_iommu_hpm_incr_ctr(s, ctx, RISCV_IOMMU_HPMEVENT_G_WALKS); + if (update_hpm) { + if (pass =3D=3D 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); + } } =20 /* Read page table entry */ @@ -641,9 +646,9 @@ static void riscv_iommu_report_fault(RISCVIOMMUState *s, riscv_iommu_fault(s, &ev); } =20 -/* 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(RISCVIOMMUStat= e *s, =20 err: riscv_iommu_report_fault(s, ctx, fault_type, cause, - !!ctx->process_id, 0, 0); + !!ctx->process_id, iova, 0); return res; } =20 @@ -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); =20 /* Translate using device directory / page table information. */ - fault =3D riscv_iommu_spa_fetch(s, ctx, iotlb); + fault =3D riscv_iommu_spa_fetch(s, ctx, iotlb, true); =20 if (!fault && iotlb->target_as =3D=3D &s->trap_as) { /* Do not cache trapped MSI translations */ @@ -2609,6 +2614,13 @@ static MemTxResult riscv_iommu_trap_write(void *opaq= ue, hwaddr addr, MemTxResult res; void *ref; uint32_t devid =3D attrs.requester_id; + IOMMUTLBEntry iotlb =3D { + .iova =3D addr, + .target_as =3D s->target_as, + .addr_mask =3D ~0ULL, + .perm =3D IOMMU_WO, + }; + int cause; =20 if (attrs.unspecified) { return MEMTX_ACCESS_ERROR; @@ -2617,11 +2629,32 @@ static MemTxResult riscv_iommu_trap_write(void *opa= que, hwaddr addr, /* FIXME: PCIe bus remapping for attached endpoints. */ devid |=3D s->bus << 8; =20 - ctx =3D riscv_iommu_ctx(s, devid, 0, IOMMU_RW, addr, &ref); + ctx =3D riscv_iommu_ctx(s, devid, attrs.pid, IOMMU_WO, addr, &ref); if (ctx =3D=3D NULL) { res =3D MEMTX_ACCESS_ERROR; } else { - res =3D 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 =3D 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 =3D MEMTX_ACCESS_ERROR; + } else if (iotlb.target_as =3D=3D &s->trap_as) { + res =3D riscv_iommu_msi_write(s, ctx, addr, + iotlb.translated_addr, data, + size, attrs); + } else { + /* The mapping may have changed since the original walk. */ + data =3D cpu_to_le64(data); + res =3D dma_memory_write(iotlb.target_as, iotlb.translated_add= r, + &data, size, attrs); + } } riscv_iommu_ctx_put(s, ref); return res; @@ -2943,6 +2976,11 @@ static IOMMUTLBEntry riscv_iommu_memory_region_trans= late( IOMMU_FLAG_STR[flag & IOMMU_RW], iotlb.iova, iotlb.translated_addr); =20 + if (iotlb.target_as =3D=3D &as->iommu->trap_as) { + /* Preserve the transaction IOVA through the MSI write callback. */ + iotlb.translated_addr =3D iotlb.iova; + } + riscv_iommu_ctx_put(as->iommu, ref); =20 return iotlb; diff --git a/tests/qtest/iommu-riscv-test.c b/tests/qtest/iommu-riscv-test.c index 1cd753c4f2..3678d8a0d4 100644 --- a/tests/qtest/iommu-riscv-test.c +++ b/tests/qtest/iommu-riscv-test.c @@ -25,6 +25,12 @@ #define DBG_TRANSLATED_PA 0x90000000ull #define DBG_NESTED_GPA 0x200000ull =20 +#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; =20 +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[] =3D { + { "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[] =3D { { .name =3D "page-4k", @@ -157,15 +178,16 @@ static QPCIDevice *find_riscv_iommu_pci(QGenericPCIBu= s *gbus, return iommu_dev; } =20 -static QPCIDevice *find_iommu_testdev(QGenericPCIBus *gbus, QPCIBar *bar) +static QPCIDevice *find_testdev(QGenericPCIBus *gbus, QPCIBar *bar, bool m= si) { QPCIDevice *dev =3D NULL; + uint16_t vendor =3D msi ? PCI_VENDOR_ID_QEMU : IOMMU_TESTDEV_VENDOR_ID; + uint16_t device =3D msi ? EDU_DEVICE_ID : IOMMU_TESTDEV_DEVICE_ID; =20 - g_test_message("Searching for iommu-testdev (vendor=3D0x%04x, device= =3D0x%04x)", - IOMMU_TESTDEV_VENDOR_ID, IOMMU_TESTDEV_DEVICE_ID); + g_test_message("Searching for %s (vendor=3D0x%04x, device=3D0x%04x)", + msi ? "edu" : "iommu-testdev", vendor, device); =20 - 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); =20 qpci_device_enable(dev); @@ -175,17 +197,21 @@ static QPCIDevice *find_iommu_testdev(QGenericPCIBus = *gbus, QPCIBar *bar) return dev; } =20 -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; + } =20 - state->qts =3D qtest_init("-machine virt,acpi=3Doff " - "-cpu max -smp 1 -m 512 -net none " - "-device riscv-iommu-pci " - "-device iommu-testdev"); + state->qts =3D qtest_initf("-machine virt,acpi=3Doff " + "-cpu max -smp 1 -m 512 -bios none -net none " + "-device riscv-iommu-pci " + "-device %s", msi ? "edu" : "iommu-testdev"); =20 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 =3D find_riscv_iommu_pci(&state->gbus, &state->iommu_= base); g_assert(state->iommu_dev); =20 - state->testdev =3D find_iommu_testdev(&state->gbus, &state->testdev_ba= r); + state->testdev =3D find_testdev(&state->gbus, &state->testdev_bar, msi= ); g_assert(state->testdev); =20 return true; @@ -267,7 +293,7 @@ static void run_riscv_iommu_translation(const QRIOMMUTe= stConfig *cfg) { RiscvIommuTestState state =3D { 0 }; =20 - if (!riscv_iommu_test_setup(&state)) { + if (!riscv_iommu_test_setup(&state, false)) { return; } =20 @@ -328,6 +354,101 @@ static void test_riscv_iommu_nested(void) run_riscv_iommu_translation(&cfg); } =20 +static void test_riscv_iommu_msi(gconstpointer data) +{ + static const unsigned events[] =3D { + RISCV_IOMMU_HPMEVENT_URQ, + RISCV_IOMMU_HPMEVENT_TLB_MISS, + RISCV_IOMMU_HPMEVENT_S_VS_WALKS, + RISCV_IOMMU_HPMEVENT_G_WALKS, + }; + const RiscvIommuMsiTest *cfg =3D data; + const uint64_t expected[] =3D { 1, 1, cfg->s_walks, cfg->g_walks }; + RiscvIommuTestState state =3D { 0 }; + uint64_t iova =3D (QRIOMMU_IOVA & ~0xfffull) + + (cfg->valid_pte ? 0 : 0x40); + uint64_t gpa =3D iova; + uint64_t dc_addr, pte =3D 0, hdr, cap; + uint64_t fq_addr =3D QRIOMMU_SPACE_OFFS + QRIOMMU_FQ_BASE_ADDR; + uint8_t msi_cap; + uint16_t flags; + + if (!riscv_iommu_test_setup(&state, true)) { + return; + } + + cap =3D riscv_iommu_check(state.qts, state.iommu_base, cfg->mode); + g_assert_cmphex(cap & RISCV_IOMMU_CAP_MSI_FLAT, !=3D, 0); + g_assert_cmphex(cap & RISCV_IOMMU_CAP_HPM, !=3D, 0); + qriommu_build_translation(state.qts, cfg->mode, state.testdev->devfn); + if (cfg->mode =3D=3D QRIOMMU_TM_NESTED) { + gpa =3D QRIOMMU_L2_PTE_VAL + (iova & 0xfff); + g_assert_cmphex(iova, !=3D, gpa); + } + + /* Match one GPA page; interrupt-file number zero selects the first PT= E. */ + dc_addr =3D 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 =3D ((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 =3D 0; i < G_N_ELEMENTS(events); i++) { + qtest_writeq(state.qts, state.iommu_base + RISCV_IOMMU_REG_IOHPMEV= T(i), + events[i]); + qtest_writeq(state.qts, state.iommu_base + RISCV_IOMMU_REG_IOHPMCT= R(i), + 0); + g_assert_cmpuint(qtest_readq(state.qts, + state.iommu_base + RISCV_IOMMU_REG_IOHPMCTR(i)), =3D=3D, 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 =3D qpci_find_capability(state.testdev, PCI_CAP_ID_MSI, 0); + g_assert_cmpuint(msi_cap, !=3D, 0); + flags =3D qpci_config_readw(state.testdev, msi_cap + PCI_MSI_FLAGS); + g_assert_cmpuint(flags & PCI_MSI_FLAGS_64BIT, !=3D, 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), =3D=3D, !cfg->valid_pte); + if (cfg->valid_pte) { + g_assert_cmphex(qtest_readl(state.qts, MSI_TARGET_PA), =3D=3D, MSI= _DATA); + } else { + hdr =3D qtest_readq(state.qts, fq_addr); + g_assert_cmpuint(hdr & RISCV_IOMMU_FQ_HDR_CAUSE, =3D=3D, + RISCV_IOMMU_FQ_CAUSE_MSI_INVALID); + g_assert_cmpuint((hdr & RISCV_IOMMU_FQ_HDR_TTYPE) >> 34, =3D=3D, + RISCV_IOMMU_FQ_TTYPE_UADDR_WR); + g_assert_cmpuint((hdr & RISCV_IOMMU_FQ_HDR_DID) >> 40, =3D=3D, + state.testdev->devfn); + g_assert_cmphex(qtest_readq(state.qts, fq_addr + 16), =3D=3D, iova= ); + g_assert_cmphex(qtest_readq(state.qts, fq_addr + 24), =3D=3D, 0); + } + + /* Count the original translation, excluding the GPA recovery walk. */ + for (unsigned i =3D 0; i < G_N_ELEMENTS(events); i++) { + g_assert_cmpuint(qtest_readq(state.qts, + state.iommu_base + RISCV_IOMMU_REG_IOHPMCTR(i)), =3D=3D, expec= ted[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; =20 - if (!riscv_iommu_test_setup(&state)) { + if (!riscv_iommu_test_setup(&state, false)) { return; } =20 @@ -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 =3D 0; i < G_N_ELEMENTS(msi_tests); i++) { + g_autofree char *path =3D 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 =3D 0; i < G_N_ELEMENTS(debug_tests); i++) { g_autofree char *path =3D g_strdup_printf("/iommu-testdev/debug/%s= ", debug_tests[i].name); --=20 2.43.0 From nobody Mon Nov 23 04:34:28 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1794815621; cv=none; d=zohomail.com; s=zohoarc; b=Ek1Ma1V8DsCgu/AvvwCX42Hlb7MX7iCjoGGz1vj4UHE2ntb6tWZDNJ44x6KFDUQHYzR/xz1sewnEBG42Tt2IMAZ7pyxsxYIN27CgAU68ntRzS62mi5hFFjxDWWcL9B396xrYx2MvLk3VU5dvhqle/HjCZUl1/TXMXV568119mQY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794815621; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=hIjlzjraPZGou6DwZuYcV0PsGSSXUwI1uycVRUHglCk=; b=CCqOmw7ofX/Ugd0N5U43lnO/29Tcs6aqnWlsTqGB0N7RroRfH4l7CBe9oCCJlw6wWdfCtFrW7tz4uJ6uT1wmPqMd8+DXyHOSHp8DUOVBEUYvjQkqd9QAzEh7uJROb1ckpNNUM36PZcKtAEHZ5PLhu/osCeHXXOVM1LM8X07Jr6Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1794815621635670.0763203846246; Sun, 15 Nov 2026 23:53:41 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xSrWU-0002dV-Qx; Mon, 16 Nov 2026 02:53:06 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xSrWR-0002cJ-7D for qemu-devel@nongnu.org; Mon, 16 Nov 2026 02:53:04 -0500 Received: from mail-pj2-x09.google.com ([2607:f8b0:4864:39::9]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xSrWP-0006Re-D9 for qemu-devel@nongnu.org; Mon, 16 Nov 2026 02:53:02 -0500 Received: by mail-pj2-x09.google.com with SMTP id 98e67ed59e1d1-3b1478f06e3so864247a91.0 for ; Sun, 15 Nov 2026 23:53:01 -0800 (PST) Received: from Dell-WorkStation.localdomain ([152.175.67.81]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b197d0f6e0sm20745622a91.9.2026.11.15.23.52.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Nov 2026 23:52:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794815580; x=1795420380; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=hIjlzjraPZGou6DwZuYcV0PsGSSXUwI1uycVRUHglCk=; b=SWmApJc8Z715GGNIvuonZNt8OP1n6SnDOOIAol/xbDF95nrQAD7zNoSApYEJbBZ8x2 01u+8XqNBSnnXBXndTJ+BwG10gLL61PWsxYHit5UD100VfFOKcrMBFnfF4d096lhDavz gQVRrpE5TumwDMPMDkL8twhtWcDKQL9XDtDJ3Bw8uNmLg7lqJFwRkeofsWoAcJnY+Ppj vpSK73Wes59q4khHzfp3294NRlyMKZECS8E7FiiH7xtWHD6Z2zS2dYPel0S+U7xvTIr9 oCPNCKEB6gPhFZODz+Ydcrl1b6C/l5pFX5kJ98frN1sp2Bai6cpj3xprJUvP8ujLRng6 yhAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794815580; x=1795420380; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=hIjlzjraPZGou6DwZuYcV0PsGSSXUwI1uycVRUHglCk=; b=fK4xW7n0w2sXUeI9Y0bHT4oWvXM7GRHwIS/14vdArEUx7JzWMNJX5bE913jZ+VKc9c zRBkdtqbWl3mpYkM85eq0WzbJx8sss9US0VY+KKzTImEmrKRLgcZ9x9NbGwNve1h0i2M 3hzDljaH/ToLAkgudQAagWzQ34a9M9BYJ0bcrkCdHQgfVTq6jhyBrC5bF2ukY/elvbKx wvdQWbjYlbvaGBrHriJbY7r3KKxGQQftLa6uRc+kg4reJ+j/AxNZggHXnKgvgrwKy9a7 is0/qCNE/PGWhz95gTtGquoEP4aRmX6oH0DQO355/pIDDLDptmN+wwNRBZh2SjL9cu54 ehlQ== X-Gm-Message-State: APuU4Z8dv6flt5EtbfMAe3qPTjP+MDxnV4FvHkPy9NnaCC7LvtSdPGFC V4Rn6pH1hdMor5rcCkkVadx04dTWuiDRN8ZFFdJP+2QJQLfcwD3uHu5v++mlgQavCOScGIIXoBw = X-Gm-Gg: AZFdTZezzewwqxeUujk4rkMLSgaMQorH5pDbdV8cQVLkI0XacNmgJX6olKmEg8eryDp pX41i+zMv8HV02AKWeW4Y5Oe7LcUPnheJfJPjjBsWcAYVce+Ch7Tia0BcEJEw+JpLK1qPBbQiLE 5BhroygJMeqzeZO68AskL7a7Kk5HiErrE7u7URg/8ofhgWiknaU3UvJs9wOW7A49uw1ksUNbqjJ /x9S2pHXFRkb7V5ARuNIpZgb0h/65UqXmeCgsc+5jAElqxqvXQQZLoJBjoYfyZ1KJERwiu7E/JF 3nb7f2UB7egykgn+0mPTCmYEyJXWIEpy6bmCap0eL4Tp6ixIDrBhFqWvL0I8DZ1E+Tdr4pBhcL1 skboAeWLyBxtkcpGvO/5vBD/fltLl5rivyJhP0Qw18TUVz+7ym2wCbEDj5oHaEEri/LMCk3ZrxU y7V+/orBVgdhCrNUyd6/eyQLsYRLdztwB13Bc5leDE0mdADrf6O7fHluWtiYrTU/oi2kdAqcZMI lhxrvP8NCGQiFgXzFBr53UIZBjQuQ7jDZwAPlLbu8cPsH6yhIu/kQX2QHhWplvmAiaUBHeAvWhM 4SBFqO6Xg6HhzmJRjhSk0ZMh9tXKKwW8bjac75Ju/SXDFaoPCg84XujY X-Received: by 2002:a17:90b:54cb:b0:3b0:ce1c:e74f with SMTP id 98e67ed59e1d1-3b1b0a3a251mr9045461a91.27.1794815579609; Sun, 15 Nov 2026 23:52:59 -0800 (PST) From: Zephyr Li To: qemu-devel@nongnu.org Cc: palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, daniel.barboza@oss.qualcomm.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, qemu-riscv@nongnu.org, farosas@suse.de, lvivier@redhat.com, pbonzini@redhat.com Subject: [PATCH 2/3] hw/riscv/riscv-iommu: Fix MSI fault PV reporting for process ID zero Date: Mon, 16 Nov 2026 15:51:40 +0800 Message-ID: <20261116075141.167317-3-fritchleybohrer@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261116075141.167317-1-fritchleybohrer@gmail.com> References: <20261116075141.167317-1-fritchleybohrer@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:39::9; envelope-from=fritchleybohrer@gmail.com; helo=mail-pj2-x09.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1794815622571158500 Content-Type: text/plain; charset="utf-8" MSI fault reporting derives PV from the numeric process ID, so process zero always produces PV=3D0. 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=3D1, PID=3D0, and the original IOVA. Also check PV=3D0 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. Fixes: 0c54acb8243d ("hw/riscv: add RISC-V IOMMU base emulation") Signed-off-by: Zephyr Li --- hw/riscv/riscv-iommu.c | 2 +- tests/qtest/iommu-riscv-test.c | 36 ++++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index 016e95fce7..5752a4776b 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -831,7 +831,7 @@ static MemTxResult riscv_iommu_msi_write(RISCVIOMMUStat= e *s, =20 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; } =20 diff --git a/tests/qtest/iommu-riscv-test.c b/tests/qtest/iommu-riscv-test.c index 3678d8a0d4..28af06d770 100644 --- a/tests/qtest/iommu-riscv-test.c +++ b/tests/qtest/iommu-riscv-test.c @@ -27,6 +27,7 @@ =20 #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; =20 static const RiscvIommuMsiTest msi_tests[] =3D { - { "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, fals= e }, + { "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 }, }; =20 static const RiscvIommuDbgTest debug_tests[] =3D { @@ -389,6 +392,27 @@ static void test_riscv_iommu_msi(gconstpointer data) /* Match one GPA page; interrupt-file number zero selects the first PT= E. */ dc_addr =3D QRIOMMU_SPACE_OFFS + QRIOMMU_DC_BASE + state.testdev->devfn * sizeof(struct riscv_iommu_dc); + if (cfg->default_process) { + uint64_t process_pa =3D QRIOMMU_SPACE_OFFS + MSI_PROCESS_GPA; + uint64_t g_leaf_pa =3D QRIOMMU_SPACE_OFFS + QRIOMMU_G_L1_PTE_VAL; + uint64_t fsc =3D qtest_readq(state.qts, dc_addr + 24); + + g_assert_cmpuint(cfg->mode, =3D=3D, QRIOMMU_TM_NESTED); + g_assert_cmphex(cap & RISCV_IOMMU_CAP_PD8, !=3D, 0); + /* Map the PD8 table through G-stage; PID zero selects its first P= C. */ + 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, =3D=3D, state.testdev->devfn); + /* Check QEMU's PDTV policy using DPE, without an EDU PASID. */ + g_assert_cmpuint(!!(hdr & RISCV_IOMMU_FQ_HDR_PV), =3D=3D, + cfg->default_process); + g_assert_cmphex(hdr & RISCV_IOMMU_FQ_HDR_PID, =3D=3D, 0); g_assert_cmphex(qtest_readq(state.qts, fq_addr + 16), =3D=3D, iova= ); g_assert_cmphex(qtest_readq(state.qts, fq_addr + 24), =3D=3D, 0); } --=20 2.43.0 From nobody Mon Nov 23 04:34:28 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1794815615; cv=none; d=zohomail.com; s=zohoarc; b=ngpsjJv3CxmDDCmpolyYAykyiXGjILetnK/yhhenp6ihM1jPhxv0WNICbwILiw6NLZbDd6mCSJMbeXYJhjdzvnN/pY9QZ6Qqn7Xkynst9+Q1G7iw5E8QaHCBVBeJsrmaYYJ3fJyDpRPym4SnoyUzEUfPl3hHZUSaOugwrgIbiO0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794815615; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=t8MMnDqot1SowKtbk3GWRs6afUCmGfGIuFgIQIPOXCQ=; b=NFHEM+pufiUFtUaaX/igiaS3ZMRLQv2HWes2KnRpPTuC+9EcIAfdCofD6T68XFBNKp9lcydpSIMy2S03aIzSA9wvII+sjMhavEzfUmLp2g3InPWRSH00lSsM/cZJyj2Mh77nfD0mn37n3ZnYKmhLbTA31Myu+C9oNX+zhbNGJXM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1794815615729666.2057845280318; Sun, 15 Nov 2026 23:53:35 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xSrWr-00036N-HS; Mon, 16 Nov 2026 02:53:29 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xSrWo-0002so-DA for qemu-devel@nongnu.org; Mon, 16 Nov 2026 02:53:26 -0500 Received: from mail-pj2-x02.google.com ([2607:f8b0:4864:39::2]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xSrWl-0006Td-Mp for qemu-devel@nongnu.org; Mon, 16 Nov 2026 02:53:26 -0500 Received: by mail-pj2-x02.google.com with SMTP id 98e67ed59e1d1-3b196fefa1fso1512007a91.1 for ; Sun, 15 Nov 2026 23:53:23 -0800 (PST) Received: from Dell-WorkStation.localdomain ([152.175.67.81]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b197d0f6e0sm20745622a91.9.2026.11.15.23.53.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Nov 2026 23:53:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794815602; x=1795420402; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=t8MMnDqot1SowKtbk3GWRs6afUCmGfGIuFgIQIPOXCQ=; b=BT/YhZjpvoKjzvAJtVIdVVhEvAJcU3Y69jYLK/s2tozgOvspB69G60dOtHIZi9H/Il MQELQlFIDghNgvJLtadt+qH85LdpwvFrr0AbThWbMQDUK/UPr0yUsvBMTyitrWtOjGDd 1OmoGFOGsATJkRDTLZA93xkMdZmzhcaNznS5+u6F6g4XcpIDRMHf7QzFMC5KdHWN8KSE LWb4rEq+hM76+soJD2V+YGSTWfnJwIiRzGZ8ubGLbNt4Z7AfOZEL9W9PJwTv2mYvQHS8 rl3ATgdBtzaMqrNuLAzeaj7nmPD5XGwtTWWASsacNPVR85THjX1XXZ4K+ufHZcFg2ZY2 294A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794815602; x=1795420402; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=t8MMnDqot1SowKtbk3GWRs6afUCmGfGIuFgIQIPOXCQ=; b=H3nzxlnXgbgCf7gcsPIdYiic513UIEJnpbPP/ZhEcASQKq3iFtQX7fhq4v3Yz2y7Jn AuUv/FeeuOsDZqnjy4cAxU8/v10DBvfXJr0A+1MOLU97m1uDehmw1XI/5dnwTMN1kBwF 0NPiEs3zEi5Vc21jYwfIYl5eWBxdOCHqPbDMVTT2lWau9p4T1hkZbJGcAn6f8EQOiOVO cNYiiaAa4Z/K2Q1XVje9/CclO+JF/VC+s+bGSbh69UxVnkBADDFDL5G5RNGQ8ZYCFupU uey7rFobtbI0AT5dzRS/EEgedjM67ntHXMBBVjmZ/X1HRldG3y/ZrQU4ABxtH7ChyWHR fktw== X-Gm-Message-State: APuU4Z+taeRH316bi0TsiMVGKdZSMXViSZZ3Vfx66Zy9iyAeOtb+h/19 y5XHZpQfQ47N9JGhEvdQBmq8CzFhx/Af8BpBNeVhE3u2auT2omnEKLWporP9/nqdlzeCV4wIEW8 = X-Gm-Gg: AZFdTZd+mm1mjHCoflrO2y1mYPe4TX404Nl8V9WUpHHTQ5iYdkSwDyzkh9IrJmQ2saE FVGZvB+7BBnmvDH6ZaUDu4qBn0ppPTeVnEjwKQ8Ud4YJZ9fKt4kmdcFaLOyThlVC7qNYZmdLFy7 bF/pE4mxr8xKWqL+Et224A34LOhviZm6PVzFQiVfpZLNr5hKUg279OUQIVuQb6mK9BJUsg5NUZq PoCvAh7clTReSxkmfEnfj9Ra4qmUtGaiKStAY3SYQ7VmTL7hpzCnUVvxT2ZS5Lho/E1/aIyXwFV wqV3/GPU1eJ2NBmMGEsEm/s+4aUj10x53QsUn3PQuhzqG6ltMSFH+u9AmsYoh+5TkEMYHPG0748 zijKEftlu5uSHE2ros8+dfEi37QwaTP8f6JHFT1zEv2dOauA+DqlshS3n/9pSIAan1hwRisvs3B Lwom6QstjzpXkA4/pveooH0Bb7VssQdrdIMW8xsMR1vgR4nQjU90jzwQciZRMIBOHtcMaWt7Anb kuJYb9H322PNeESqMdHUiyc369qktcpbokWBkOyHBjyrwDDGsyCnsuVwqgRnThT+REgdpF8O48V bUKO7I4+0tXokGM4GceocJjzdZwDuMLksO7U/cnWneDhqSOWTe6oSU7Q X-Received: by 2002:a17:90a:da86:b0:3a4:b0f3:6597 with SMTP id 98e67ed59e1d1-3b1b05b9444mr9861461a91.0.1794815601658; Sun, 15 Nov 2026 23:53:21 -0800 (PST) From: Zephyr Li To: qemu-devel@nongnu.org Cc: palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, daniel.barboza@oss.qualcomm.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, qemu-riscv@nongnu.org, farosas@suse.de, lvivier@redhat.com, pbonzini@redhat.com Subject: [PATCH 3/3] hw/riscv/riscv-iommu: Add unit tests for MSI trap handling Date: Mon, 16 Nov 2026 15:51:41 +0800 Message-ID: <20261116075141.167317-4-fritchleybohrer@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261116075141.167317-1-fritchleybohrer@gmail.com> References: <20261116075141.167317-1-fritchleybohrer@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:39::2; envelope-from=fritchleybohrer@gmail.com; helo=mail-pj2-x02.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1794815616752158500 Content-Type: text/plain; charset="utf-8" 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 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. Signed-off-by: Zephyr Li --- 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 diff --git a/MAINTAINERS b/MAINTAINERS index 3d53e104c5..75de9034b8 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -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 =20 RISC-V XThead* extensions M: Christoph Muellner diff --git a/hw/riscv/riscv-iommu-trap.c.inc b/hw/riscv/riscv-iommu-trap.c.= inc new file mode 100644 index 0000000000..1e3eaee930 --- /dev/null +++ b/hw/riscv/riscv-iommu-trap.c.inc @@ -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 intercep= tion + * 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 =3D opaque; + RISCVIOMMUContext *ctx; + MemTxResult res; + void *ref; + uint32_t devid =3D attrs.requester_id; + IOMMUTLBEntry iotlb =3D { + .iova =3D addr, + .target_as =3D s->target_as, + .addr_mask =3D ~0ULL, + .perm =3D IOMMU_WO, + }; + int cause; + + if (attrs.unspecified) { + return MEMTX_ACCESS_ERROR; + } + + /* FIXME: PCIe bus remapping for attached endpoints. */ + devid |=3D s->bus << 8; + + ctx =3D riscv_iommu_ctx(s, devid, attrs.pid, IOMMU_WO, addr, &ref); + if (ctx =3D=3D NULL) { + res =3D 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 =3D 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 =3D MEMTX_ACCESS_ERROR; + } else if (iotlb.target_as =3D=3D &s->trap_as) { + res =3D riscv_iommu_msi_write(s, ctx, addr, + iotlb.translated_addr, data, + size, attrs); + } else { + /* The mapping may have changed since the original walk. */ + data =3D cpu_to_le64(data); + res =3D dma_memory_write(iotlb.target_as, iotlb.translated_add= r, + &data, size, attrs); + } + } + riscv_iommu_ctx_put(s, ref); + return res; +} diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index 5752a4776b..cb98840121 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -2599,66 +2599,7 @@ static const MemoryRegionOps riscv_iommu_mmio_ops = =3D { } }; =20 -/* - * Translations matching MSI pattern check are redirected to "riscv-iommu-= trap" - * memory region as untranslated address, for additional MSI/MRIF intercep= tion - * 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 =3D (RISCVIOMMUState *)opaque; - RISCVIOMMUContext *ctx; - MemTxResult res; - void *ref; - uint32_t devid =3D attrs.requester_id; - IOMMUTLBEntry iotlb =3D { - .iova =3D addr, - .target_as =3D s->target_as, - .addr_mask =3D ~0ULL, - .perm =3D IOMMU_WO, - }; - int cause; - - if (attrs.unspecified) { - return MEMTX_ACCESS_ERROR; - } - - /* FIXME: PCIe bus remapping for attached endpoints. */ - devid |=3D s->bus << 8; - - ctx =3D riscv_iommu_ctx(s, devid, attrs.pid, IOMMU_WO, addr, &ref); - if (ctx =3D=3D NULL) { - res =3D 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 =3D 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 =3D MEMTX_ACCESS_ERROR; - } else if (iotlb.target_as =3D=3D &s->trap_as) { - res =3D riscv_iommu_msi_write(s, ctx, addr, - iotlb.translated_addr, data, - size, attrs); - } else { - /* The mapping may have changed since the original walk. */ - data =3D cpu_to_le64(data); - res =3D dma_memory_write(iotlb.target_as, iotlb.translated_add= r, - &data, size, attrs); - } - } - riscv_iommu_ctx_put(s, ref); - return res; -} +#include "riscv-iommu-trap.c.inc" =20 static MemTxResult riscv_iommu_trap_read(void *opaque, hwaddr addr, uint64_t *data, unsigned size, MemTxAttrs attrs) diff --git a/tests/unit/meson.build b/tests/unit/meson.build index 37b561d4a7..e4c425fbad 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -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/virti= o-dmabuf.c'], 'test-qmp-cmds': [testqapi], 'test-xbzrle': [migration], diff --git a/tests/unit/test-riscv-iommu-trap.c b/tests/unit/test-riscv-iom= mu-trap.c new file mode 100644 index 0000000000..d4753c7f31 --- /dev/null +++ b/tests/unit/test-riscv-iommu-trap.c @@ -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, =3D=3D, test.attrs.requester_id); + g_assert_cmpuint(attrs.pid, =3D=3D, test.attrs.pid); + g_assert_cmpuint(attrs.secure, =3D=3D, test.attrs.secure); + g_assert_cmpuint(attrs.space, =3D=3D, test.attrs.space); + g_assert_cmpuint(attrs.user, =3D=3D, test.attrs.user); + g_assert_cmpuint(attrs.memory, =3D=3D, test.attrs.memory); + g_assert_cmpuint(attrs.debug, =3D=3D, test.attrs.debug); + g_assert_cmpuint(attrs.address_type, =3D=3D, test.attrs.address_type); + g_assert_cmpuint(attrs.unspecified, =3D=3D, test.attrs.unspecified); +} + +static RISCVIOMMUContext *test_ctx(RISCVIOMMUState *s, unsigned devid, + unsigned pid, IOMMUAccessFlags perm, + uint64_t iova, void **ref) +{ + g_assert_true(s =3D=3D &test.state); + g_assert_cmpuint(devid, =3D=3D, + test.attrs.requester_id | (test.state.bus << 8)); + g_assert_cmpuint(pid, =3D=3D, test.attrs.pid); + g_assert_cmpuint(perm, =3D=3D, IOMMU_WO); + g_assert_cmphex(iova, =3D=3D, TEST_IOVA); + test.ctx_calls++; + /* Cached contexts return a separate reference for ctx_put(). */ + *ref =3D 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 =3D=3D &test.state); + g_assert_true(ref =3D=3D (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 =3D=3D &test.state); + g_assert_true(ctx =3D=3D &test.ctx); + g_assert_cmphex(iotlb->iova, =3D=3D, TEST_IOVA); + g_assert_true(iotlb->target_as =3D=3D &test.physical_as); + g_assert_cmphex(iotlb->addr_mask, =3D=3D, UINT64_MAX); + g_assert_cmpuint(iotlb->perm, =3D=3D, IOMMU_WO); + g_assert_false(update_hpm); + test.walk_calls++; + iotlb->target_as =3D test.walk_as; + iotlb->translated_addr =3D 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 si= ze, + MemTxAttrs attrs) +{ + g_assert_true(s =3D=3D &test.state); + g_assert_true(ctx =3D=3D &test.ctx); + g_assert_cmphex(iova, =3D=3D, TEST_IOVA); + g_assert_cmphex(gpa, =3D=3D, test.walk_addr); + g_assert_cmphex(iova, !=3D, gpa); + g_assert_cmphex(data, =3D=3D, TEST_DATA); + g_assert_cmpuint(size, =3D=3D, 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 =3D=3D &test.state); + g_assert_true(ctx =3D=3D &test.ctx); + g_assert_cmpuint(ttype, =3D=3D, RISCV_IOMMU_FQ_TTYPE_UADDR_WR); + g_assert_cmpuint(cause, =3D=3D, test.walk_fault); + /* Check the existing callback policy, not the complete PID/DPE model.= */ + g_assert_cmpuint(pv, =3D=3D, !!(test.ctx.tc & RISCV_IOMMU_DC_TC_PDTV)); + g_assert_cmphex(iotval, =3D=3D, TEST_IOVA); + g_assert_cmphex(iotval2, =3D=3D, 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[] =3D { + 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, + }; + + g_assert_true(as =3D=3D &test.changed_as); + g_assert_cmphex(addr, =3D=3D, test.walk_addr); + g_assert_cmpuint(len, =3D=3D, 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 =3D &test.physical_as; + test.state.bus =3D 0x80; + test.attrs =3D (MemTxAttrs) { + .requester_id =3D 0x3456, + .pid =3D 0x5a, + .secure =3D true, + .space =3D 1, + .user =3D true, + .memory =3D true, + .debug =3D true, + .address_type =3D 1, + }; + test.ctx.process_id =3D test.attrs.pid; + test.walk_as =3D &test.state.trap_as; + test.walk_addr =3D TEST_GPA; + test.size =3D 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, =3D=3D, ctx); + g_assert_cmpuint(test.put_calls, =3D=3D, ctx); + g_assert_cmpuint(test.walk_calls, =3D=3D, walk); + g_assert_cmpuint(test.msi_calls, =3D=3D, msi); + g_assert_cmpuint(test.fault_calls, =3D=3D, fault); + g_assert_cmpuint(test.dma_calls, =3D=3D, dma); +} + +static void test_unspecified(void) +{ + reset_test(); + test.attrs.unspecified =3D true; + g_assert_cmpuint(trap_write(), =3D=3D, MEMTX_ACCESS_ERROR); + assert_calls(0, 0, 0, 0, 0); +} + +static void test_missing_context(void) +{ + reset_test(); + test.missing_ctx =3D true; + g_assert_cmpuint(trap_write(), =3D=3D, MEMTX_ACCESS_ERROR); + assert_calls(1, 0, 0, 0, 0); +} + +static void test_msi_forward(void) +{ + reset_test(); + test.size =3D 8; + test.msi_result =3D MEMTX_ERROR; + g_assert_cmpuint(trap_write(), =3D=3D, MEMTX_ERROR); + assert_calls(1, 1, 1, 0, 0); +} + +static void test_rewalk_fault(gconstpointer data) +{ + reset_test(); + test.attrs.pid =3D 0; + test.ctx.process_id =3D 0; + test.ctx.tc =3D data ? RISCV_IOMMU_DC_TC_PDTV : 0; + test.walk_fault =3D RISCV_IOMMU_FQ_CAUSE_WR_FAULT_VS; + g_assert_cmpuint(trap_write(), =3D=3D, 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 =3D data; + + reset_test(); + test.size =3D cfg->size; + test.walk_as =3D &test.changed_as; + test.walk_addr =3D 0x90000040; + test.dma_result =3D cfg->result; + g_assert_cmpuint(trap_write(), =3D=3D, cfg->result); + assert_calls(1, 1, 0, 0, 1); +} + +int main(int argc, char **argv) +{ + static const FallbackCase fallback_cases[] =3D { + { "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_cont= ext); + 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 =3D 0; i < G_N_ELEMENTS(fallback_cases); i++) { + g_autofree char *path =3D 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(); +} --=20 2.43.0