[PATCH] target/riscv: Check Svnapot PTE before updating A/D bits

dodochicha posted 1 patch 1 month, 2 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261005115916.21196-1-kevincheng60303@gmail.com
Maintainers: Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Chao Liu <chao.liu@processmission.com>
target/riscv/tcg/cpu_helper.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
[PATCH] target/riscv: Check Svnapot PTE before updating A/D bits
Posted by dodochicha 1 month, 2 weeks ago
Reserved Svnapot PTE encodings must raise a page fault without modifying the PTE. Currently the A/D bits may be updated before the Svnapot encoding is validated.

Move the Svnapot validation before the A/D update so reserved PTEs fault without changing the PTE.

Signed-off-by: dodochicha <kevincheng60303@gmail.com>
---
 target/riscv/tcg/cpu_helper.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/target/riscv/tcg/cpu_helper.c b/target/riscv/tcg/cpu_helper.c
index 07d9222652..105f2f1927 100644
--- a/target/riscv/tcg/cpu_helper.c
+++ b/target/riscv/tcg/cpu_helper.c
@@ -1403,6 +1403,13 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
         return sstack_page ? TRANSLATE_PMP_FAIL : TRANSLATE_FAIL;
     }
 
+    if (riscv_cpu_cfg(env)->ext_svnapot && (pte & PTE_N)) {
+        napot_bits = ctzl(ppn) + 1;
+        if ((i != (levels - 1)) || (napot_bits != 4)) {
+            return TRANSLATE_FAIL;
+        }
+    }
+
     target_ulong updated_pte = pte;
 
     /*
@@ -1473,13 +1480,6 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
     /* For superpage mappings, make a fake leaf PTE for the TLB's benefit. */
     target_ulong vpn = addr >> PGSHIFT;
 
-    if (riscv_cpu_cfg(env)->ext_svnapot && (pte & PTE_N)) {
-        napot_bits = ctzl(ppn) + 1;
-        if ((i != (levels - 1)) || (napot_bits != 4)) {
-            return TRANSLATE_FAIL;
-        }
-    }
-
     napot_mask = (1 << napot_bits) - 1;
     *physical = (((ppn & ~napot_mask) | (vpn & napot_mask) |
                   (vpn & (((target_ulong)1 << ptshift) - 1))
-- 
2.50.1 (Apple Git-155)
Re: [PATCH] target/riscv: Check Svnapot PTE before updating A/D bits
Posted by Alistair 1 month, 2 weeks ago
On Mon, 2026-10-05 at 19:59 +0800, dodochicha wrote:
> Reserved Svnapot PTE encodings must raise a page fault without
> modifying the PTE. Currently the A/D bits may be updated before the
> Svnapot encoding is validated.
> 
> Move the Svnapot validation before the A/D update so reserved PTEs
> fault without changing the PTE.
> 

In future please wrap your commit messages at around 70 characters.

Thanks!

Applied to riscv-to-apply.next

Alistair


> Signed-off-by: dodochicha <kevincheng60303@gmail.com>
> ---
>  target/riscv/tcg/cpu_helper.c | 14 +++++++-------
>  1 file changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/target/riscv/tcg/cpu_helper.c
> b/target/riscv/tcg/cpu_helper.c
> index 07d9222652..105f2f1927 100644
> --- a/target/riscv/tcg/cpu_helper.c
> +++ b/target/riscv/tcg/cpu_helper.c
> @@ -1403,6 +1403,13 @@ static int get_physical_address(CPURISCVState
> *env, hwaddr *physical,
>          return sstack_page ? TRANSLATE_PMP_FAIL : TRANSLATE_FAIL;
>      }
>  
> +    if (riscv_cpu_cfg(env)->ext_svnapot && (pte & PTE_N)) {
> +        napot_bits = ctzl(ppn) + 1;
> +        if ((i != (levels - 1)) || (napot_bits != 4)) {
> +            return TRANSLATE_FAIL;
> +        }
> +    }
> +
>      target_ulong updated_pte = pte;
>  
>      /*
> @@ -1473,13 +1480,6 @@ static int get_physical_address(CPURISCVState
> *env, hwaddr *physical,
>      /* For superpage mappings, make a fake leaf PTE for the TLB's
> benefit. */
>      target_ulong vpn = addr >> PGSHIFT;
>  
> -    if (riscv_cpu_cfg(env)->ext_svnapot && (pte & PTE_N)) {
> -        napot_bits = ctzl(ppn) + 1;
> -        if ((i != (levels - 1)) || (napot_bits != 4)) {
> -            return TRANSLATE_FAIL;
> -        }
> -    }
> -
>      napot_mask = (1 << napot_bits) - 1;
>      *physical = (((ppn & ~napot_mask) | (vpn & napot_mask) |
>                    (vpn & (((target_ulong)1 << ptshift) - 1))
Re: [PATCH] target/riscv: Check Svnapot PTE before updating A/D bits
Posted by Alistair 1 month, 2 weeks ago
On Mon, 2026-10-05 at 19:59 +0800, dodochicha wrote:
> Reserved Svnapot PTE encodings must raise a page fault without
> modifying the PTE. Currently the A/D bits may be updated before the
> Svnapot encoding is validated.
> 
> Move the Svnapot validation before the A/D update so reserved PTEs
> fault without changing the PTE.
> 
> Signed-off-by: dodochicha <kevincheng60303@gmail.com>

Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

Alistair

> ---
>  target/riscv/tcg/cpu_helper.c | 14 +++++++-------
>  1 file changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/target/riscv/tcg/cpu_helper.c
> b/target/riscv/tcg/cpu_helper.c
> index 07d9222652..105f2f1927 100644
> --- a/target/riscv/tcg/cpu_helper.c
> +++ b/target/riscv/tcg/cpu_helper.c
> @@ -1403,6 +1403,13 @@ static int get_physical_address(CPURISCVState
> *env, hwaddr *physical,
>          return sstack_page ? TRANSLATE_PMP_FAIL : TRANSLATE_FAIL;
>      }
>  
> +    if (riscv_cpu_cfg(env)->ext_svnapot && (pte & PTE_N)) {
> +        napot_bits = ctzl(ppn) + 1;
> +        if ((i != (levels - 1)) || (napot_bits != 4)) {
> +            return TRANSLATE_FAIL;
> +        }
> +    }
> +
>      target_ulong updated_pte = pte;
>  
>      /*
> @@ -1473,13 +1480,6 @@ static int get_physical_address(CPURISCVState
> *env, hwaddr *physical,
>      /* For superpage mappings, make a fake leaf PTE for the TLB's
> benefit. */
>      target_ulong vpn = addr >> PGSHIFT;
>  
> -    if (riscv_cpu_cfg(env)->ext_svnapot && (pte & PTE_N)) {
> -        napot_bits = ctzl(ppn) + 1;
> -        if ((i != (levels - 1)) || (napot_bits != 4)) {
> -            return TRANSLATE_FAIL;
> -        }
> -    }
> -
>      napot_mask = (1 << napot_bits) - 1;
>      *physical = (((ppn & ~napot_mask) | (vpn & napot_mask) |
>                    (vpn & (((target_ulong)1 << ptshift) - 1))