From nobody Mon Nov 23 04:38:03 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 179458288587789.67524948973767; Fri, 13 Nov 2026 07:14:45 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xRsym-0001dn-PO; Fri, 13 Nov 2026 10:14:18 -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 1xRsxn-00017J-HN; Fri, 13 Nov 2026 10:13:15 -0500 Received: from smtp81.cstnet.cn ([159.226.251.81] helo=cstnet.cn) by eggs.gnu.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1xRsxk-0001rX-80; Fri, 13 Nov 2026 10:13:15 -0500 Received: from k3-03 (unknown [159.226.43.8]) by APP-03 (Coremail) with SMTP id rQCowACneZr1KPdqPKn0AA--.15904S3; Fri, 13 Nov 2026 23:13:01 +0800 (CST) From: Tan Chi To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, daniel.barboza@oss.qualcomm.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, pierrick.bouvier@oss.qualcomm.com, nutty.liu@hotmail.com, Tan Chi Subject: [PATCH v2 1/3] hw/riscv: iommu: Fix implicit G-stage page-table accesses Date: Fri, 13 Nov 2026 23:12:49 +0800 Message-ID: <20261113151251.30618-2-tanchi25@mails.ucas.ac.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261113151251.30618-1-tanchi25@mails.ucas.ac.cn> References: <20261113151251.30618-1-tanchi25@mails.ucas.ac.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: rQCowACneZr1KPdqPKn0AA--.15904S3 X-Coremail-Antispam: 1UD129KBjvJXoW3Wryxtry3Kw17WFWrCr47XFb_yoWxWF15pF Z5WrW29r43Jr13Zanaywn7GF1Fka1kKrW5Ww4ag3sayF4YqrW5Xrs8ta4Yya90gFykZ3W8 Jw45ZrsrZF4Ut3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUQK14x267AKxVW5JVWrJwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr4l82xGYIkIc2 x26xkF7I0E14v26r4j6ryUM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1l84 ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_GcCE3s1lnxkE FVAIw20F6cxK64vIFxWle2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I 8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r4j6F4UMcvjeVCF s4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lFI xGxcIEc7CjxVA2Y2ka0xkIwI1lc7CjxVAaw2AFwI0_Jw0_GFylc2xSY4AK67AK6ryrMxAI w28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr 4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxG rwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJw CI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2 z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7sRinmRPUUUUU== X-Originating-IP: [159.226.43.8] X-CM-SenderInfo: pwdquxrlsvqzpdlo2hpxfd2hldfou0/ 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=159.226.251.81; envelope-from=tanchi25@mails.ucas.ac.cn; helo=cstnet.cn X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-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-ZM-MESSAGEID: 1794582893301158500 Content-Type: text/plain; charset="utf-8" When translating a DMA write, G-stage accesses to first-stage page tables are implicit reads. The current implementation checks permissions using the original DMA access type, which may incorrectly require write permission for these reads. Use read permission for implicit page-table accesses while preserving the original DMA access type for the final translation and fault reporting. Also track the fault address separately from the translation result and set the implicit-access flag in iotval2 for guest-page faults. Route address-range failures through the common page-fault path to report the correct fault address. Signed-off-by: Tan Chi Reviewed-by: Nutty Liu --- hw/riscv/riscv-iommu.c | 58 ++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index 0e6970773a..5b8cfff3bd 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -273,18 +273,21 @@ static hwaddr riscv_iommu_napot_page_mask(hwaddr ppn,= hwaddr addr, hwaddr *out) * Keeping them separate for now, as error reporting and flow specifics are * sufficiently different for separate implementation. * - * @s : IOMMU Device State - * @ctx : Translation context for device id and process address space= id. - * @iotlb : translation data: physical address and access mode. - * @return : success or fault cause code. + * @s : IOMMU Device State + * @ctx : Translation context for device id and process + * address space id. + * @iotlb : translation data: physical address and access mode. + * @fault_iotval2 : Fault address and implicit-access flags for iotval2. + * @return : success or fault cause code. */ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ct= x, - IOMMUTLBEntry *iotlb) + IOMMUTLBEntry *iotlb, uint64_t *fault_iotval2) { IOMMUAccessFlags trans_perm =3D IOMMU_NONE; dma_addr_t addr, base; uint64_t satp, gatp, pte; bool en_s, en_g; + bool implicit_read; struct { unsigned char step; unsigned char levels; @@ -299,10 +302,10 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, = RISCVIOMMUContext *ctx, MemTxResult ret; bool pv =3D !!ctx->process_id; /* - * Keep the request permission separate from iotlb->perm. G-stage + * Keep the request permission separate from iotlb->perm. G-stage * walks translate S-stage PTE addresses before the real leaf is * reached, but permission checks and fault types must still use the - * original request. A successful walk leaves iotlb->perm with the + * original request. A successful walk leaves iotlb->perm with the * effective leaf permission for the translation cache. */ const IOMMUAccessFlags req_perm =3D iotlb->perm; @@ -424,6 +427,14 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, R= ISCVIOMMUContext *ctx, const dma_addr_t pte_addr =3D base + idx * sc[pass].ptesize; const bool ade =3D ctx->tc & (pass ? RISCV_IOMMU_DC_TC_GADE : RISCV_IOMMU_DC_TC_S= ADE); + /* + * G-stage accesses to S-stage page tables are implicit reads. + * The original DMA permission is used for the final data access. + */ + implicit_read =3D + pass =3D=3D G_STAGE && en_s && sc[S_STAGE].step !=3D sc[S_STAG= E].levels; + const IOMMUAccessFlags acc_perm =3D + implicit_read ? IOMMU_RO : req_perm; =20 if (ade && !(s->cap & RISCV_IOMMU_CAP_AMO_HWAD)) { /* GADE/SADE are reserved bits if AMO_HWAD is cleared. */ @@ -442,15 +453,11 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, = RISCVIOMMUContext *ctx, masked_msbs =3D (addr >> (va_len - 1)) & mask; =20 if (masked_msbs !=3D 0 && masked_msbs !=3D mask) { - return (req_perm & IOMMU_WO) ? - RISCV_IOMMU_FQ_CAUSE_WR_FAULT_S : - RISCV_IOMMU_FQ_CAUSE_RD_FAULT_S; + goto page_fault; } } else { if ((addr & va_mask) !=3D addr) { - return (req_perm & IOMMU_WO) ? - RISCV_IOMMU_FQ_CAUSE_WR_FAULT_VS : - RISCV_IOMMU_FQ_CAUSE_RD_FAULT_VS; + goto page_fault; } } } @@ -499,13 +506,13 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, = RISCVIOMMUContext *ctx, * non-user mode leaf PTE and !pv we need to fault. */ break; - } else if ((req_perm & IOMMU_RO) && !(pte & PTE_R)) { + } else if ((acc_perm & IOMMU_RO) && !(pte & PTE_R)) { break; /* Read access check failed */ - } else if ((req_perm & IOMMU_WO) && !(pte & PTE_W)) { + } else if ((acc_perm & IOMMU_WO) && !(pte & PTE_W)) { break; /* Write access check failed */ } else if (!ade && !(pte & PTE_A)) { break; /* Access bit not set */ - } else if ((req_perm & IOMMU_WO) && !ade && !(pte & PTE_D)) { + } else if ((acc_perm & IOMMU_WO) && !ade && !(pte & PTE_D)) { break; /* Dirty bit not set */ } else if (pass =3D=3D G_STAGE && !(pte & PTE_U)) { /* @@ -587,13 +594,19 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, = RISCVIOMMUContext *ctx, } } while (1); =20 +page_fault: /* * riscv_iommu_translate() will receive a fault and then call - * riscv_iommu_report_fault() using iotlb->translated_addr - * as iotval2. Update translated_addr it with the latest - * translated addr we have. + * riscv_iommu_report_fault() using fault_iotval2 as iotval2. + * Set fault_iotval2 to zero for S-stage page faults. + * For guest-page faults, report the faulting GPA and set the implicit + * first-stage page-table access flag in the low bit. */ - iotlb->translated_addr =3D addr; + *fault_iotval2 =3D 0; + + if (pass =3D=3D G_STAGE) { + *fault_iotval2 =3D (addr & ~3ULL) | (implicit_read ? 1 : 0); + } =20 return (req_perm & IOMMU_WO) ? (pass ? RISCV_IOMMU_FQ_CAUSE_WR_FAULT_VS : @@ -1740,6 +1753,7 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, = RISCVIOMMUContext *ctx, bool enable_pri; hwaddr phys =3D 0; int fault; + uint64_t fault_iotval2 =3D 0; =20 riscv_iommu_hpm_incr_ctr(s, ctx, RISCV_IOMMU_HPMEVENT_URQ); =20 @@ -1776,7 +1790,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, &fault_iotval2); =20 if (!fault && iotlb->target_as =3D=3D &s->trap_as) { /* Do not cache trapped MSI translations */ @@ -1827,7 +1841,7 @@ done: } =20 riscv_iommu_report_fault(s, ctx, ttype, fault, enable_pid, - iotlb->iova, iotlb->translated_addr); + iotlb->iova, fault_iotval2); return fault; } =20 --=20 2.53.0 From nobody Mon Nov 23 04:38:03 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 179458287643132.073545765185145; Fri, 13 Nov 2026 07:14:36 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xRsyo-0001ee-KL; Fri, 13 Nov 2026 10:14:20 -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 1xRsxo-0001Cu-Mg; Fri, 13 Nov 2026 10:13:37 -0500 Received: from smtp81.cstnet.cn ([159.226.251.81] helo=cstnet.cn) by eggs.gnu.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1xRsxm-0001sG-Gg; Fri, 13 Nov 2026 10:13:16 -0500 Received: from k3-03 (unknown [159.226.43.8]) by APP-03 (Coremail) with SMTP id rQCowACneZr1KPdqPKn0AA--.15904S4; Fri, 13 Nov 2026 23:13:02 +0800 (CST) From: Tan Chi To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, daniel.barboza@oss.qualcomm.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, pierrick.bouvier@oss.qualcomm.com, nutty.liu@hotmail.com, Tan Chi Subject: [PATCH v2 2/3] hw/riscv: iommu: Check permissions on IOATC hits Date: Fri, 13 Nov 2026 23:12:50 +0800 Message-ID: <20261113151251.30618-3-tanchi25@mails.ucas.ac.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261113151251.30618-1-tanchi25@mails.ucas.ac.cn> References: <20261113151251.30618-1-tanchi25@mails.ucas.ac.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: rQCowACneZr1KPdqPKn0AA--.15904S4 X-Coremail-Antispam: 1UD129KBjvJXoW7Kw17uF45KFWktr1DJrWxtFb_yoW8Gr48pr WDGr4Y9ayFqrn3Za42ya1DAF1Ut3Z3WF1aqrySkF4UtF4Fqr4UAr45tayjgw1jkF4kuFy2 9wsF9rn5Zr1DtaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUQE14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UM2 8EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2vY z4IE04k24VAvwVAKI4IrM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c 02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVW8JVWxJwAm72CE 4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4 IIrI8v6xkF7I0E8cxan2IY04v7MxkF7I0En4kS14v26r1q6r43MxkIecxEwVAFwVW5WwCF 04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r 18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vI r41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr 1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvE x4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x0JUYNtxUUUUU= X-Originating-IP: [159.226.43.8] X-CM-SenderInfo: pwdquxrlsvqzpdlo2hpxfd2hldfou0/ 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=159.226.251.81; envelope-from=tanchi25@mails.ucas.ac.cn; helo=cstnet.cn X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-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-ZM-MESSAGEID: 1794582891773158500 Content-Type: text/plain; charset="utf-8" The current implementation reuses cached translations without checking whether their permissions satisfy the requested access. Only reuse an IOATC entry when its permissions cover the current request. Otherwise, perform a page-table walk to check the requested access. Preserve the existing behavior for requests with IOMMU_NONE. This is also needed for hardware A/D updates, where mappings with a clear D bit must not cache write permission until the bit has been set. Signed-off-by: Tan Chi Reviewed-by: Daniel Henrique Barboza Reviewed-by: Nutty Liu --- hw/riscv/riscv-iommu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index 5b8cfff3bd..8c26b7f2b2 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -1779,7 +1779,9 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, = RISCVIOMMUContext *ctx, phys =3D iot->phys; } qemu_mutex_unlock(&s->iot_lock); - if (perm !=3D IOMMU_NONE) { + if (perm !=3D IOMMU_NONE && + (iotlb->perm =3D=3D IOMMU_NONE || + (perm & iotlb->perm) =3D=3D iotlb->perm)) { iotlb->translated_addr =3D PPN_PHYS(phys); iotlb->addr_mask =3D ~TARGET_PAGE_MASK; iotlb->perm =3D perm; --=20 2.53.0 From nobody Mon Nov 23 04:38:03 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1794582880088856.8693526936677; Fri, 13 Nov 2026 07:14:40 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xRsyO-0001L8-Tq; Fri, 13 Nov 2026 10:13:55 -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 1xRsxn-00017G-8t; Fri, 13 Nov 2026 10:13:15 -0500 Received: from smtp81.cstnet.cn ([159.226.251.81] helo=cstnet.cn) by eggs.gnu.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1xRsxj-0001rb-Ky; Fri, 13 Nov 2026 10:13:15 -0500 Received: from k3-03 (unknown [159.226.43.8]) by APP-03 (Coremail) with SMTP id rQCowACneZr1KPdqPKn0AA--.15904S5; Fri, 13 Nov 2026 23:13:03 +0800 (CST) From: Tan Chi To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, daniel.barboza@oss.qualcomm.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, pierrick.bouvier@oss.qualcomm.com, nutty.liu@hotmail.com, Tan Chi Subject: [PATCH v2 3/3] hw/riscv: iommu: Support automatic PTE A/D updates Date: Fri, 13 Nov 2026 23:12:51 +0800 Message-ID: <20261113151251.30618-4-tanchi25@mails.ucas.ac.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261113151251.30618-1-tanchi25@mails.ucas.ac.cn> References: <20261113151251.30618-1-tanchi25@mails.ucas.ac.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: rQCowACneZr1KPdqPKn0AA--.15904S5 X-Coremail-Antispam: 1UD129KBjvJXoW3ZFyUKFyktry8Gw4rXry8Grg_yoWkCr1UpF 95GrW3Kr43tryS9FsxJw1DGF18W3Z5G343W390gasYkF4UtrW5Xan8ta45AFWDKFyDZ3WF qa1ruFsrZF40yrJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUQE14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JrWl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UM2 8EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2vY z4IE04k24VAvwVAKI4IrM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c 02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVW8JVWxJwAm72CE 4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4 IIrI8v6xkF7I0E8cxan2IY04v7MxkF7I0En4kS14v26r1q6r43MxkIecxEwVAFwVW5WwCF 04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r 18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vI r41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr 1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvE x4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x0JUO_-kUUUUU= X-Originating-IP: [159.226.43.8] X-CM-SenderInfo: pwdquxrlsvqzpdlo2hpxfd2hldfou0/ 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=159.226.251.81; envelope-from=tanchi25@mails.ucas.ac.cn; helo=cstnet.cn X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-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-ZM-MESSAGEID: 1794582891846158500 Content-Type: text/plain; charset="utf-8" Add support for the AMO_HWAD capability to automatically update the accessed and dirty bits of leaf PTEs when SADE or GADE is enabled. Use atomic compare-and-exchange to update PTEs, restarting the page-table walk if a concurrent modification is detected. For nested translation, check G-stage write permissions before updating an S-stage PTE. Restart the walk if the write translation resolves to a different physical address. Avoid caching write permission when the PTE's dirty bit is clear, so subsequent writes can trigger the required update. Expose the capability through the new "amo-hwad" device property, disabled by default. Signed-off-by: Tan Chi Reviewed-by: Daniel Henrique Barboza Reviewed-by: Nutty Liu --- docs/specs/riscv-iommu.rst | 2 + hw/riscv/riscv-iommu.c | 196 ++++++++++++++++++++++++++++++++++++- hw/riscv/riscv-iommu.h | 1 + 3 files changed, 195 insertions(+), 4 deletions(-) diff --git a/docs/specs/riscv-iommu.rst b/docs/specs/riscv-iommu.rst index 571a6a6cc9..7912361196 100644 --- a/docs/specs/riscv-iommu.rst +++ b/docs/specs/riscv-iommu.rst @@ -69,6 +69,7 @@ Several options are available to control the capabilities= of the device, namely: - "ioatc-limit": size of the Address Translation Cache (default to 2Mb) - "intremap": enable/disable MSI support - "ats": enable ATS support +- "amo-hwad": enable automatic PTE A/D bit updates when SADE or GADE is set - "off" (Out-of-reset translation mode: 'on' for DMA disabled, 'off' for '= BARE' (passthrough)) - "s-stage": enable s-stage support - "g-stage": enable g-stage support @@ -96,6 +97,7 @@ riscv-iommu options: - "ioatc-limit": default value (2Mb) - "intremap": enabled - "ats": enabled +- "amo-hwad": disabled - "off": on (DMA disabled) - "s-stage": enabled - "g-stage": enabled diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index 8c26b7f2b2..5b1a388364 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -265,6 +265,128 @@ static hwaddr riscv_iommu_napot_page_mask(hwaddr ppn,= hwaddr addr, hwaddr *out) return page_mask; } =20 +typedef enum RISCVIOMMUADResult { + RISCV_IOMMU_AD_SUCCESS, + RISCV_IOMMU_AD_RETRY, + RISCV_IOMMU_AD_FAULT, +} RISCVIOMMUADResult; + +/* + * Atomically update the A/D bits of a leaf PTE. + * Retry the page-table walk if the PTE has changed. + */ +static RISCVIOMMUADResult +riscv_iommu_ad_update(RISCVIOMMUState *s, hwaddr pte_addr, + unsigned int pte_size, uint64_t pte, uint64_t update= d_pte) +{ + MemoryRegion *mr; + hwaddr offset, len =3D pte_size; + uint64_t old_pte; + void *pte_ptr; + + if (pte_addr & (pte_size - 1)) { + return RISCV_IOMMU_AD_FAULT; + } + + RCU_READ_LOCK_GUARD(); + + mr =3D address_space_translate(s->target_as, pte_addr, &offset, + &len, true, MEMTXATTRS_UNSPECIFIED); + + if (len < pte_size || !memory_region_is_ram(mr)) { + /* + * The entire PTE must be backed by a single RAM region + * for an atomic update. + */ + return RISCV_IOMMU_AD_FAULT; + } + + pte_ptr =3D qemu_map_ram_ptr(mr->ram_block, offset); + + if (pte_size =3D=3D 4) { + uint32_t expected =3D cpu_to_le32((uint32_t)pte); + uint32_t desired =3D cpu_to_le32((uint32_t)updated_pte); + + old_pte =3D qatomic_cmpxchg((uint32_t *)pte_ptr, expected, desired= ); + old_pte =3D le32_to_cpu(old_pte); + } else { + uint64_t expected =3D cpu_to_le64(pte); + uint64_t desired =3D cpu_to_le64(updated_pte); + + old_pte =3D qatomic_cmpxchg((uint64_t *)pte_ptr, expected, desired= ); + old_pte =3D le64_to_cpu(old_pte); + } + + if (old_pte !=3D pte) { + return RISCV_IOMMU_AD_RETRY; + } + + return RISCV_IOMMU_AD_SUCCESS; +} + +static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ct= x, + IOMMUTLBEntry *iotlb, bool implicit_write, uint64_t *fault_iotval2); + +/* + * Updating a first-stage leaf PTE requires an implicit write. + * Translate its GPA through the G-stage, checking write permission + * and updating the G-stage's A/D bits as needed. + */ +static int riscv_iommu_check_pte_write(RISCVIOMMUState *s, + RISCVIOMMUContext *ctx, + dma_addr_t pte_gpa, + IOMMUAccessFlags req_perm, + hwaddr *pte_spa, + uint64_t *fault_iotval2) +{ + RISCVIOMMUContext gctx =3D *ctx; + uint64_t iotval2 =3D 0; + int fault; + + IOMMUTLBEntry iotlb =3D { + .iova =3D pte_gpa, + .target_as =3D s->target_as, + .addr_mask =3D ~0ULL, + .perm =3D IOMMU_WO, + }; + + gctx.satp =3D RISCV_IOMMU_DC_FSC_MODE_BARE; + + fault =3D riscv_iommu_spa_fetch(s, &gctx, &iotlb, true, &iotval2); + + if (fault) { + *fault_iotval2 =3D iotval2; + + if (fault =3D=3D RISCV_IOMMU_FQ_CAUSE_RD_FAULT_VS || + fault =3D=3D RISCV_IOMMU_FQ_CAUSE_WR_FAULT_VS) { + /* + * Per the RISC-V IOMMU Fault/Event Queue format, iotval2[0] + * indicates an implicit first-stage access and iotval2[1] + * indicates an implicit write. Report the fault for the + * original DMA access rather than the implicit write. + */ + *fault_iotval2 =3D (pte_gpa & ~3ULL) | 3; + + fault =3D (req_perm & IOMMU_WO) ? + RISCV_IOMMU_FQ_CAUSE_WR_FAULT_VS : + RISCV_IOMMU_FQ_CAUSE_RD_FAULT_VS; + } else if (fault =3D=3D RISCV_IOMMU_FQ_CAUSE_RD_FAULT || + fault =3D=3D RISCV_IOMMU_FQ_CAUSE_WR_FAULT) { + /* + * A physical access fault also uses the original + * DMA access type. + */ + fault =3D (req_perm & IOMMU_WO) ? + RISCV_IOMMU_FQ_CAUSE_WR_FAULT : + RISCV_IOMMU_FQ_CAUSE_RD_FAULT; + } + return fault; + } + + *pte_spa =3D iotlb.translated_addr; + return 0; +} + /* * RISCV IOMMU Address Translation Lookup - Page Table Walk * @@ -277,14 +399,15 @@ static hwaddr riscv_iommu_napot_page_mask(hwaddr ppn,= hwaddr addr, hwaddr *out) * @ctx : Translation context for device id and process * address space id. * @iotlb : translation data: physical address and access mode. + * @implicit_write : Whether this is an implicit write to a first-stage P= TE. * @fault_iotval2 : Fault address and implicit-access flags for iotval2. * @return : success or fault cause code. */ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ct= x, - IOMMUTLBEntry *iotlb, uint64_t *fault_iotval2) + IOMMUTLBEntry *iotlb, bool implicit_write, uint64_t *fault_iotval2) { IOMMUAccessFlags trans_perm =3D IOMMU_NONE; - dma_addr_t addr, base; + dma_addr_t addr, base, s_base_gpa =3D 0; uint64_t satp, gatp, pte; bool en_s, en_g; bool implicit_read; @@ -309,6 +432,8 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RI= SCVIOMMUContext *ctx, * effective leaf permission for the translation cache. */ const IOMMUAccessFlags req_perm =3D iotlb->perm; + const hwaddr addr_mask =3D iotlb->addr_mask; + const hwaddr translated_addr =3D iotlb->translated_addr; =20 satp =3D get_field(ctx->satp, RISCV_IOMMU_ATP_MODE_FIELD); gatp =3D get_field(ctx->gatp, RISCV_IOMMU_ATP_MODE_FIELD); @@ -327,7 +452,7 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RI= SCVIOMMUContext *ctx, * means we can't do an early MSI check unless we have * strictly !en_s. */ - if (!en_s && (req_perm & IOMMU_WO) && + if (!implicit_write && !en_s && (req_perm & IOMMU_WO) && riscv_iommu_msi_check(s, ctx, iotlb->iova)) { iotlb->target_as =3D &s->trap_as; iotlb->translated_addr =3D iotlb->iova; @@ -414,6 +539,17 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, R= ISCVIOMMUContext *ctx, /* S/G stages translation tables root pointers */ gatp =3D PPN_PHYS(get_field(ctx->gatp, RISCV_IOMMU_ATP_PPN_FIELD)); satp =3D PPN_PHYS(get_field(ctx->satp, RISCV_IOMMU_ATP_PPN_FIELD)); + +restart: + sc[S_STAGE].step =3D 0; + sc[G_STAGE].step =3D 0; + trans_perm =3D IOMMU_NONE; + iotlb->addr_mask =3D addr_mask; + iotlb->translated_addr =3D translated_addr; + iotlb->perm =3D req_perm; + s_base_gpa =3D 0; + *fault_iotval2 =3D 0; + addr =3D (en_s && en_g) ? satp : iotlb->iova; base =3D en_g ? gatp : satp; pass =3D en_g ? G_STAGE : S_STAGE; @@ -541,8 +677,51 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, R= ISCVIOMMUContext *ctx, iotlb->addr_mask &=3D (1ULL << va_skip) - 1; } =20 + /* Automatically update the leaf PTE's A/D bits when enabled. = */ + uint64_t updated_pte =3D pte; + if (ade) { + updated_pte |=3D PTE_A; + + if (acc_perm & IOMMU_WO) { + updated_pte |=3D PTE_D; + } + } + if (updated_pte !=3D pte) { + if (pass =3D=3D S_STAGE && en_g) { + const dma_addr_t s_pte_gpa =3D + s_base_gpa + idx * sc[pass].ptesize; + hwaddr write_spa; + + int fault =3D riscv_iommu_check_pte_write( + s, ctx, s_pte_gpa, req_perm, &write_spa, + fault_iotval2); + if (fault) { + return fault; + } + + if (write_spa !=3D pte_addr) { + goto restart; + } + } + + RISCVIOMMUADResult result =3D riscv_iommu_ad_update( + s, pte_addr, sc[pass].ptesize, pte, updated_pte); + + if (result =3D=3D RISCV_IOMMU_AD_RETRY) { + goto restart; + } + if (result =3D=3D RISCV_IOMMU_AD_FAULT) { + return (req_perm & IOMMU_WO) ? + RISCV_IOMMU_FQ_CAUSE_WR_FAULT : + RISCV_IOMMU_FQ_CAUSE_RD_FAULT; + } + pte =3D updated_pte; + } + /* Continue with S-Stage translation? */ if (pass && sc[0].step !=3D sc[0].levels) { + /* Save the S-stage page-table GPA for A/D updates. */ + s_base_gpa =3D addr; pass =3D S_STAGE; addr =3D iotlb->iova; continue; @@ -553,6 +732,11 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, R= ISCVIOMMUContext *ctx, ((pte & PTE_R) ? IOMMU_RW : IOMMU= _WO) : IOMMU_RO; =20 + /* Do not cache write permission until the D bit is set. */ + if (!(pte & PTE_D)) { + leaf_perm &=3D ~IOMMU_WO; + } + trans_perm =3D trans_perm =3D=3D IOMMU_NONE ? leaf_perm : trans_perm & leaf_perm; =20 @@ -1792,7 +1976,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_iotval2); + fault =3D riscv_iommu_spa_fetch(s, ctx, iotlb, false, &fault_iotval2); =20 if (!fault && iotlb->target_as =3D=3D &s->trap_as) { /* Do not cache trapped MSI translations */ @@ -2730,6 +2914,9 @@ static void riscv_iommu_realize(DeviceState *dev, Err= or **errp) if (s->enable_ats) { s->cap |=3D RISCV_IOMMU_CAP_ATS; } + if (s->enable_amo_hwad) { + s->cap |=3D RISCV_IOMMU_CAP_AMO_HWAD; + } if (s->enable_s_stage) { s->cap |=3D RISCV_IOMMU_CAP_SV32 | RISCV_IOMMU_CAP_SV39 | RISCV_IOMMU_CAP_SV48 | RISCV_IOMMU_CAP_SV57; @@ -2889,6 +3076,7 @@ static const Property riscv_iommu_properties[] =3D { LIMIT_CACHE_IOT), DEFINE_PROP_BOOL("intremap", RISCVIOMMUState, enable_msi, TRUE), DEFINE_PROP_BOOL("ats", RISCVIOMMUState, enable_ats, TRUE), + DEFINE_PROP_BOOL("amo-hwad", RISCVIOMMUState, enable_amo_hwad, false), DEFINE_PROP_BOOL("off", RISCVIOMMUState, enable_off, TRUE), DEFINE_PROP_BOOL("s-stage", RISCVIOMMUState, enable_s_stage, TRUE), DEFINE_PROP_BOOL("g-stage", RISCVIOMMUState, enable_g_stage, TRUE), diff --git a/hw/riscv/riscv-iommu.h b/hw/riscv/riscv-iommu.h index 403d10b321..8cf228f698 100644 --- a/hw/riscv/riscv-iommu.h +++ b/hw/riscv/riscv-iommu.h @@ -44,6 +44,7 @@ struct RISCVIOMMUState { bool enable_off; /* Enable out-of-reset OFF mode (DMA disabled) */ bool enable_msi; /* Enable MSI remapping */ bool enable_ats; /* Enable ATS support */ + bool enable_amo_hwad; /* Enable hardware A/D updates */ bool enable_s_stage; /* Enable S/VS-Stage translation */ bool enable_g_stage; /* Enable G-Stage translation */ =20 --=20 2.53.0