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 1794148843045231.6447550071067; Sun, 8 Nov 2026 06:40:43 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xQ43v-0003jt-Uu; Sun, 08 Nov 2026 09:40:03 -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 1xQ43s-0003im-NO; Sun, 08 Nov 2026 09:40:00 -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 1xQ43p-00038l-9N; Sun, 08 Nov 2026 09:40:00 -0500 Received: from k3-03 (unknown [159.226.43.8]) by APP-03 (Coremail) with SMTP id rQCowAAXJpetifBqBgKWAA--.15550S3; Sun, 08 Nov 2026 22:39:48 +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, Tan Chi Subject: [PATCH 1/3] hw/riscv: iommu: Fix implicit G-stage page-table accesses Date: Sun, 8 Nov 2026 22:39:31 +0800 Message-ID: <20261108143934.129923-2-tanchi25@mails.ucas.ac.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261108143934.129923-1-tanchi25@mails.ucas.ac.cn> References: <20261108143934.129923-1-tanchi25@mails.ucas.ac.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: rQCowAAXJpetifBqBgKWAA--.15550S3 X-Coremail-Antispam: 1UD129KBjvJXoW3WryxCrWDZr4UAF15Jr4ktFb_yoWxXFy8pF Z5WrWa9r43Jr1fZan3twn7GF1Fk3WkKrW5Ww1agasayF4YqrW5Xrs0ya4Yy3yqgFykZ3W8 Jw45ZrsrZF4Yy3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPC14x267AKxVW5JVWrJwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr4l82xGYIkIc2 x26xkF7I0E14v26r1I6r4UM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UM2 8EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2vY z4IE04k24VAvwVAKI4IrM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c 02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE 4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7Mx kF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4U MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67 AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0 cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z2 80aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI 43ZEXa7VUjiiSJUUUUU== 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: 1794148852670158500 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 | 60 ++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index 0e6970773a..2dcc528599 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -273,13 +273,15 @@ 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; @@ -299,10 +301,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 +426,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. + */ + const bool 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 +452,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 +505,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 +593,22 @@ 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. + * Update fault_iotval2 with the latest translated address. + * For guest-page faults, set the implicit first-stage + * page-table access flag in the low bit. */ - iotlb->translated_addr =3D addr; + *fault_iotval2 =3D addr; + + if (pass =3D=3D G_STAGE) { + bool implicit_read =3D + en_s && sc[S_STAGE].step !=3D sc[S_STAGE].levels; + *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 +1755,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 +1792,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 +1843,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 1794148843760904.3542377254545; Sun, 8 Nov 2026 06:40:43 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xQ43y-0003kp-0k; Sun, 08 Nov 2026 09:40: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 1xQ43t-0003iz-AU; Sun, 08 Nov 2026 09:40:01 -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 1xQ43q-00038p-VQ; Sun, 08 Nov 2026 09:40:01 -0500 Received: from k3-03 (unknown [159.226.43.8]) by APP-03 (Coremail) with SMTP id rQCowAAXJpetifBqBgKWAA--.15550S4; Sun, 08 Nov 2026 22:39:52 +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, Tan Chi Subject: [PATCH 2/3] hw/riscv: iommu: Check permissions on IOATC hits Date: Sun, 8 Nov 2026 22:39:32 +0800 Message-ID: <20261108143934.129923-3-tanchi25@mails.ucas.ac.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261108143934.129923-1-tanchi25@mails.ucas.ac.cn> References: <20261108143934.129923-1-tanchi25@mails.ucas.ac.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: rQCowAAXJpetifBqBgKWAA--.15550S4 X-Coremail-Antispam: 1UD129KBjvJXoW7Xr15JFW5ZFWUXw48ZF15twb_yoW8Jr15pr WDKr4Yvw4Fqrn3Z3W3ta1DAF1Uta4fWF43Wr1SyFs5JayFqrW5ZF45KayUZw1jkF1kuFy2 9rsI9rn3Zr1Dt3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPC14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26r4j6ryUM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UM2 8EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2vY z4IE04k24VAvwVAKI4IrM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c 02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE 4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7Mx kF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4U MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67 AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0 cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z2 80aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI 43ZEXa7VUjjQ6JUUUUU== 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: 1794148852604158500 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 2dcc528599..6760699eb8 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -1781,7 +1781,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 1794148866212470.6251708888045; Sun, 8 Nov 2026 06:41:06 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xQ446-0003mm-Ru; Sun, 08 Nov 2026 09:40:14 -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 1xQ43u-0003jw-G9; Sun, 08 Nov 2026 09:40:03 -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 1xQ43q-00038r-WC; Sun, 08 Nov 2026 09:40:02 -0500 Received: from k3-03 (unknown [159.226.43.8]) by APP-03 (Coremail) with SMTP id rQCowAAXJpetifBqBgKWAA--.15550S5; Sun, 08 Nov 2026 22:39:54 +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, Tan Chi Subject: [PATCH 3/3] hw/riscv: iommu: Support automatic PTE A/D updates Date: Sun, 8 Nov 2026 22:39:33 +0800 Message-ID: <20261108143934.129923-4-tanchi25@mails.ucas.ac.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261108143934.129923-1-tanchi25@mails.ucas.ac.cn> References: <20261108143934.129923-1-tanchi25@mails.ucas.ac.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: rQCowAAXJpetifBqBgKWAA--.15550S5 X-Coremail-Antispam: 1UD129KBjvJXoW3ZFyUKFyktr4kXryktr4ruFg_yoWDZr1DpF 95GrWagr45tr1fuFsxJw1DGF1ru3Z5G343W390gasYkF4jqrW5Xan0ya45AF4DKryUZ3WF qa1rZFsxZr4IvrJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPC14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JrWl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UM2 8EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2vY z4IE04k24VAvwVAKI4IrM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c 02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE 4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7Mx kF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4U MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67 AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0 cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z2 80aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI 43ZEXa7VUbBMKJUUUUU== 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: 1794148868368158500 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 --- hw/riscv/riscv-iommu.c | 196 ++++++++++++++++++++++++++++++++++++++++- hw/riscv/riscv-iommu.h | 1 + 2 files changed, 193 insertions(+), 4 deletions(-) diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index 6760699eb8..04d50cd620 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; struct { @@ -308,6 +431,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); @@ -326,7 +451,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; @@ -413,6 +538,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; @@ -540,8 +676,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; @@ -552,6 +731,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 @@ -1794,7 +1978,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 */ @@ -2732,6 +2916,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; @@ -2891,6 +3078,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