All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH 0/2] target/riscv: Check store permission before split AMOs with Zicfiss
@ 2026-11-24  7:18 Rishab Pradeep
  2026-11-24  7:18 ` [RFC PATCH 1/2] accel/tcg: Add probe_access_perm() Rishab Pradeep
  2026-11-24  7:18 ` [RFC PATCH 2/2] target/riscv: Check store permission before split AMOs with Zicfiss Rishab Pradeep
  0 siblings, 2 replies; 3+ messages in thread
From: Rishab Pradeep @ 2026-11-24  7:18 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-riscv, richard.henderson, pbonzini, palmer, alistair.francis,
	liwei1518, daniel.barboza, zhiwei_liu, chao.liu, tarun.patanjali,
	christopher.elwood2024, Rishab Pradeep

With Zicfiss, an ordinary AMO to a shadow-stack page must fault with a
store/AMO access fault (cause 7).  When TCG lowers the AMO to a load
followed by a store (!CF_PARALLEL), the load runs first.  That gives
the wrong cause (15) when the PTE's A bit is clear under Svade, and it
lets the load's side effects (e.g. a read watchpoint) happen when an
earlier load has left a read-permitting TLB entry for the page.

Patch 1 adds probe_access_perm(), a permission-only lookup that takes
the real access size and skips watchpoints, dirty marking and the
plugin slow path.  Patch 2 uses it from the RISC-V AMO/AMOCAS
translation, only for TBs with shadow stacks enabled and !CF_PARALLEL
(the parallel helpers already look up the store first).

Tested on RV64 system emulation (debug, ASan, UBSan build):
 - cold-TLB AMO with A=D=0 on a shadow-stack page gives cause 7
   (cause 15 on baseline), single-thread TCG
 - read watchpoint after a warming load does not stop with the
   series, and stops on baseline, single-thread TCG
 - page-crossing Zama16b, page-crossing PMP and PMP-boundary AMOs,
   single-thread TCG and MTTCG
 - unit and qtest pass
 - check-tcg: rv128-sstatus (UBSan, pmp.c) fails and the
   interruptedmemory plugin test times out, both identically on master
 - check-functional (sanitized build): 4 timeouts, and three pass with
   a longer limit.  sifive_u passes in about 5s on baseline and
   patched builds without sanitizers.  I did not run check-functional
   on a sanitized master build.

Not tested: RV32 at runtime (including the amocas.d path in
gen_cmpxchg64), and the cold-TLB A=D=0 case and the read watchpoint
under MTTCG.  riscv32-softmmu and the linux-user targets compile.

Open questions:
 - Is a permission-only helper in accel/tcg acceptable?  The
   alternative is a RISC-V-local helper built on probe_access_flags(),
   at the cost of early dirty marking.
 - The probe is a store lookup, so with hardware A/D updates it may set
   D before the AMO's load phase runs.  If that load then faults, D is
   set on a page the AMO never accessed.  I have not tested this.
 - The second-page probe for page-crossing Zama16b accesses mirrors
   what the real operation does today.  A page-crossing access
   arguably should fault, since a granule cannot cross a page.
   Enforcing that would be a separate change.

Rishab Pradeep (2):
  accel/tcg: Add probe_access_perm()
  target/riscv: Check store permission before split AMOs with Zicfiss

 accel/tcg/cputlb.c                            | 11 ++++++
 include/accel/tcg/probe.h                     | 20 ++++++++++
 target/riscv/helper.h                         |  5 +++
 .../riscv/tcg/insn_trans/trans_rvzacas.c.inc  |  2 +
 target/riscv/tcg/op_helper.c                  | 37 +++++++++++++++++++
 target/riscv/tcg/translate.c                  | 23 ++++++++++++
 6 files changed, 98 insertions(+)

-- 
2.56.0



^ permalink raw reply	[flat|nested] 3+ messages in thread

* [RFC PATCH 1/2] accel/tcg: Add probe_access_perm()
  2026-11-24  7:18 [RFC PATCH 0/2] target/riscv: Check store permission before split AMOs with Zicfiss Rishab Pradeep
@ 2026-11-24  7:18 ` Rishab Pradeep
  2026-11-24  7:18 ` [RFC PATCH 2/2] target/riscv: Check store permission before split AMOs with Zicfiss Rishab Pradeep
  1 sibling, 0 replies; 3+ messages in thread
From: Rishab Pradeep @ 2026-11-24  7:18 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-riscv, richard.henderson, pbonzini, palmer, alistair.francis,
	liwei1518, daniel.barboza, zhiwei_liu, chao.liu, tarun.patanjali,
	christopher.elwood2024, Rishab Pradeep

Add a permission-only variant of probe_access() for callers that need
a faulting translation check without the side effects of an access.
The first user is the RISC-V Zicfiss AMO check in the next patch.

After a successful lookup it does not process watchpoints, mark clean
RAM dirty, or request the plugin slow path.  It also does not assert
that (addr, size) stays within one page: a caller probing the first
page of a page-crossing access should pass the full size, so that
target hooks (e.g. RISC-V PMP) see the same range as the real access,
and then probe the second page itself.

Side effects of the target's tlb_fill hook still apply, for example a
page walk updating guest PTE A/D bits.

Co-developed-by: Tarun Patanjali <tarun.patanjali@utexas.edu>
Signed-off-by: Tarun Patanjali <tarun.patanjali@utexas.edu>
Co-developed-by: Chris Elwood <christopher.elwood2024@gmail.com>
Signed-off-by: Chris Elwood <christopher.elwood2024@gmail.com>
Signed-off-by: Rishab Pradeep <rishab@rispop.dev>
---
 accel/tcg/cputlb.c        | 11 +++++++++++
 include/accel/tcg/probe.h | 20 ++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
index d79eae58df..6144a4efb0 100644
--- a/accel/tcg/cputlb.c
+++ b/accel/tcg/cputlb.c
@@ -1435,6 +1435,17 @@ int probe_access_flags(CPUArchState *env, vaddr addr, int size,
     return flags;
 }
 
+void probe_access_perm(CPUArchState *env, vaddr addr, int fault_size,
+                       MMUAccessType access_type, int mmu_idx,
+                       uintptr_t retaddr)
+{
+    CPUTLBEntryFull *full;
+    void *host;
+
+    probe_access_internal(env_cpu(env), addr, fault_size, access_type,
+                          mmu_idx, false, &host, &full, retaddr, false);
+}
+
 void *probe_access(CPUArchState *env, vaddr addr, int size,
                    MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
 {
diff --git a/include/accel/tcg/probe.h b/include/accel/tcg/probe.h
index 0b788901ba..4233aa9489 100644
--- a/include/accel/tcg/probe.h
+++ b/include/accel/tcg/probe.h
@@ -71,6 +71,26 @@ int probe_access_flags(CPUArchState *env, vaddr addr, int size,
 
 #ifndef CONFIG_USER_ONLY
 
+/**
+ * probe_access_perm:
+ * @env: CPUArchState
+ * @addr: guest virtual address to look up
+ * @fault_size: size passed to the target's tlb_fill (not asserted to
+ *              stay within one page; the caller splits page crossings)
+ * @access_type: read, write or execute permission
+ * @mmu_idx: MMU index to use for lookup
+ * @retaddr: return address for unwinding
+ *
+ * Permission-only lookup.  Raise an exception if the page does not
+ * satisfy @access_type.  Unlike probe_access, after a successful lookup
+ * do not process watchpoints or mark clean RAM dirty, and do not request
+ * plugin slow-path.  Side effects of the target's tlb_fill hook still
+ * apply, for example a page walk updating guest PTE A/D bits.
+ */
+void probe_access_perm(CPUArchState *env, vaddr addr, int fault_size,
+                       MMUAccessType access_type, int mmu_idx,
+                       uintptr_t retaddr);
+
 /**
  * probe_access_full:
  * Like probe_access_flags, except also return into @pfull.
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [RFC PATCH 2/2] target/riscv: Check store permission before split AMOs with Zicfiss
  2026-11-24  7:18 [RFC PATCH 0/2] target/riscv: Check store permission before split AMOs with Zicfiss Rishab Pradeep
  2026-11-24  7:18 ` [RFC PATCH 1/2] accel/tcg: Add probe_access_perm() Rishab Pradeep
@ 2026-11-24  7:18 ` Rishab Pradeep
  1 sibling, 0 replies; 3+ messages in thread
From: Rishab Pradeep @ 2026-11-24  7:18 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-riscv, richard.henderson, pbonzini, palmer, alistair.francis,
	liwei1518, daniel.barboza, zhiwei_liu, chao.liu, tarun.patanjali,
	christopher.elwood2024, Rishab Pradeep

With Zicfiss enabled, an ordinary AMO or AMOCAS to a shadow-stack page
must raise a store/AMO access fault (cause 7).

When TCG lowers an AMO to a load followed by a store (!CF_PARALLEL),
the load runs first, which goes wrong in two ways:

 - If the PTE's A bit is clear and hardware A/D updates are disabled
   (Svade), the load's page walk fails and raises a load page fault,
   which is promoted to a store page fault (cause 15) instead of
   cause 7.

 - If an earlier ordinary load has installed a TLB entry that permits
   reads for the page, the AMO's load hits it and executes, and only
   the store phase faults.  The cause is then correct, but the load's
   side effects (e.g. a read watchpoint) have already happened.

Add an amo_probe_write helper that does a store-permission lookup at
the normal MMU index before the load, using probe_access_perm(), and
call it from gen_amo, gen_cmpxchg, gen_cmpxchg64 and the amocas.q
translation.  It is emitted only when shadow stacks are enabled and
the TB is not CF_PARALLEL, since the parallel atomic helpers already
look up the store first.

The first lookup uses the full access size, as mmu_lookup() does, so
PMP sees the same range as the real access.  A page-crossing access
adds a second lookup at the pointer_wrap()'d page base for the
remaining bytes.  If the operation requires natural alignment and the
address is misaligned, the probe is skipped so that the real access
raises the address-misaligned exception first.

RISCV_UW2_ALWAYS_STORE_AMO and promote_load_fault() are unchanged;
they still cover load-phase faults that the probe cannot prevent.

Page-crossing Zama16b accesses are probed to match the current
behaviour of the real operation; enforcing the 16-byte granule would
be a separate change.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4840
Co-developed-by: Tarun Patanjali <tarun.patanjali@utexas.edu>
Signed-off-by: Tarun Patanjali <tarun.patanjali@utexas.edu>
Co-developed-by: Chris Elwood <christopher.elwood2024@gmail.com>
Signed-off-by: Chris Elwood <christopher.elwood2024@gmail.com>
Signed-off-by: Rishab Pradeep <rishab@rispop.dev>
---
 target/riscv/helper.h                         |  5 +++
 .../riscv/tcg/insn_trans/trans_rvzacas.c.inc  |  2 +
 target/riscv/tcg/op_helper.c                  | 37 +++++++++++++++++++
 target/riscv/tcg/translate.c                  | 23 ++++++++++++
 4 files changed, 67 insertions(+)

diff --git a/target/riscv/helper.h b/target/riscv/helper.h
index 1cffcad580..f30fc3ad4f 100644
--- a/target/riscv/helper.h
+++ b/target/riscv/helper.h
@@ -1387,6 +1387,11 @@ DEF_HELPER_1(ssamoswap_disabled, void, env)
 /* Zalrsc SC write probe */
 DEF_HELPER_FLAGS_3(sc_probe_write, TCG_CALL_NO_WG, void, env, tl, tl)
 
+#ifndef CONFIG_USER_ONLY
+/* Zicfiss: store-permission check before the load of a split AMO */
+DEF_HELPER_FLAGS_4(amo_probe_write, TCG_CALL_NO_WG, void, env, tl, tl, tl)
+#endif
+
 /* SiFive Custom int8 Matrix-Multiply */
 DEF_HELPER_5(sf_vqmaccu_4x8x4, void, ptr, ptr, ptr, env, i32)
 DEF_HELPER_5(sf_vqmacc_4x8x4, void, ptr, ptr, ptr, env, i32)
diff --git a/target/riscv/tcg/insn_trans/trans_rvzacas.c.inc b/target/riscv/tcg/insn_trans/trans_rvzacas.c.inc
index 79bca1e957..e7eb4aae33 100644
--- a/target/riscv/tcg/insn_trans/trans_rvzacas.c.inc
+++ b/target/riscv/tcg/insn_trans/trans_rvzacas.c.inc
@@ -78,6 +78,7 @@ static bool gen_cmpxchg64(DisasContext *ctx, arg_atomic *a, MemOp mop)
 
     mop |= ctx->mo_endianness;
     decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO);
+    gen_amo_probe(ctx, src1, mop, true);
     tcg_gen_atomic_cmpxchg_i64(dest, src1, dest, src2, ctx->mem_idx, mop);
 
     gen_set_gpr_pair(ctx, a->rd, dest);
@@ -125,6 +126,7 @@ static bool trans_amocas_q(DisasContext *ctx, arg_amocas_q *a)
     tcg_gen_concat_i64_i128(src2, src2l, src2h);
     tcg_gen_concat_i64_i128(dest, destl, desth);
     decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO);
+    gen_amo_probe(ctx, src1, memop, true);
     tcg_gen_atomic_cmpxchg_i128(dest, src1, dest, src2, ctx->mem_idx, memop);
 
     tcg_gen_extr_i128_i64(destl, desth, dest);
diff --git a/target/riscv/tcg/op_helper.c b/target/riscv/tcg/op_helper.c
index f9c79fe6b9..49696114ed 100644
--- a/target/riscv/tcg/op_helper.c
+++ b/target/riscv/tcg/op_helper.c
@@ -29,6 +29,10 @@
 #include "accel/tcg/cpu-ldst.h"
 #include "accel/tcg/cpu-loop.h"
 #include "accel/tcg/probe.h"
+#ifndef CONFIG_USER_ONLY
+#include "accel/tcg/cpu-ops.h"
+#include "exec/target_page.h"
+#endif
 #include "exec/helper-proto.h"
 #include "exec/tlb-flags.h"
 #include "trace.h"
@@ -300,6 +304,39 @@ void HELPER(sc_probe_write)(CPURISCVState *env, target_ulong addr,
 
 #ifndef CONFIG_USER_ONLY
 
+/*
+ * Check store permission before the load of an AMO that TCG lowers to
+ * load+store.  Needed when the load itself would be allowed (e.g. a
+ * cached read translation on a shadow-stack page) so that the AMO
+ * faults with the store cause before any load side effect.
+ *
+ * The first lookup carries the full size (matching mmu_lookup, so PMP
+ * sees the same range); a page crossing adds a second lookup for the
+ * remaining bytes at the wrapped page base.
+ */
+void HELPER(amo_probe_write)(CPURISCVState *env, target_ulong addr,
+                             target_ulong size, target_ulong strict)
+{
+    uintptr_t ra = GETPC();
+    int mmu_idx = riscv_env_mmu_index(env, false);
+    CPUState *cs = env_cpu(env);
+    vaddr last = addr + size - 1;
+
+    if (strict && (addr & (size - 1))) {
+        return;                 /* the real op raises misaligned first */
+    }
+
+    probe_access_perm(env, addr, size, MMU_DATA_STORE, mmu_idx, ra);
+
+    if ((addr ^ last) & TARGET_PAGE_MASK) {
+        vaddr addr1 = last & TARGET_PAGE_MASK;
+        int size1 = size - (addr1 - addr);  /* before wrapping */
+
+        addr1 = cs->cc->tcg_ops->pointer_wrap(cs, mmu_idx, addr1, addr);
+        probe_access_perm(env, addr1, size1, MMU_DATA_STORE, mmu_idx, ra);
+    }
+}
+
 target_ulong HELPER(sret)(CPURISCVState *env)
 {
     uint64_t mstatus;
diff --git a/target/riscv/tcg/translate.c b/target/riscv/tcg/translate.c
index 79b0f444ef..5dffb61d51 100644
--- a/target/riscv/tcg/translate.c
+++ b/target/riscv/tcg/translate.c
@@ -1150,6 +1150,25 @@ static bool gen_unary_per_ol(DisasContext *ctx, arg_r2 *a, DisasExtend ext,
     return gen_unary(ctx, a, ext, f_tl);
 }
 
+/*
+ * Zicfiss: without CF_PARALLEL, TCG lowers AMOs to load+store, so check
+ * store permission up front.  The parallel helpers (atomic_mmu_lookup)
+ * already do the store lookup first.  @strict: the real op enforces
+ * natural alignment.
+ */
+static void gen_amo_probe(DisasContext *ctx, TCGv addr, MemOp mop,
+                          bool strict)
+{
+#ifndef CONFIG_USER_ONLY
+    if (ctx->bcfi_enabled &&
+        !(tb_cflags(ctx->base.tb) & CF_PARALLEL)) {
+        gen_helper_amo_probe_write(tcg_env, addr,
+                                   tcg_constant_tl(memop_size(mop)),
+                                   tcg_constant_tl(strict));
+    }
+#endif
+}
+
 static bool gen_amo(DisasContext *ctx, arg_atomic *a,
                     void(*func)(TCGv, TCGv, TCGv, TCGArg, MemOp),
                     MemOp mop)
@@ -1157,16 +1176,19 @@ static bool gen_amo(DisasContext *ctx, arg_atomic *a,
     TCGv dest = dest_gpr(ctx, a->rd);
     TCGv src1, src2 = get_gpr(ctx, a->rs2, EXT_NONE);
     MemOp size = mop & MO_SIZE;
+    bool strict = true;
 
     mop |= ctx->mo_endianness;
     if (ctx->cfg_ptr->ext_zama16b && size >= MO_32) {
         mop |= MO_ATOM_WITHIN16;
+        strict = false;
     } else {
         mop |= MO_ALIGN;
     }
 
     decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO);
     src1 = get_address(ctx, a->rs1, 0);
+    gen_amo_probe(ctx, src1, mop, strict);
     func(dest, src1, src2, ctx->mem_idx, mop);
 
     gen_set_gpr(ctx, a->rd, dest);
@@ -1181,6 +1203,7 @@ static bool gen_cmpxchg(DisasContext *ctx, arg_atomic *a, MemOp mop)
 
     mop |= ctx->mo_endianness;
     decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO);
+    gen_amo_probe(ctx, src1, mop, true);
     tcg_gen_atomic_cmpxchg_tl(dest, src1, dest, src2, ctx->mem_idx, mop);
 
     gen_set_gpr(ctx, a->rd, dest);
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-24  7:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24  7:18 [RFC PATCH 0/2] target/riscv: Check store permission before split AMOs with Zicfiss Rishab Pradeep
2026-11-24  7:18 ` [RFC PATCH 1/2] accel/tcg: Add probe_access_perm() Rishab Pradeep
2026-11-24  7:18 ` [RFC PATCH 2/2] target/riscv: Check store permission before split AMOs with Zicfiss Rishab Pradeep

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.