All of lore.kernel.org
 help / color / mirror / Atom feed
From: Rishab Pradeep <rishab@rispop.dev>
To: qemu-devel@nongnu.org
Cc: qemu-riscv@nongnu.org, richard.henderson@linaro.org,
	pbonzini@redhat.com, palmer@dabbelt.com,
	alistair.francis@wdc.com, liwei1518@gmail.com,
	daniel.barboza@oss.qualcomm.com, zhiwei_liu@linux.alibaba.com,
	chao.liu@processmission.com, tarun.patanjali@utexas.edu,
	christopher.elwood2024@gmail.com,
	Rishab Pradeep <rishab@rispop.dev>
Subject: [RFC PATCH 2/2] target/riscv: Check store permission before split AMOs with Zicfiss
Date: Tue, 24 Nov 2026 01:18:36 -0600	[thread overview]
Message-ID: <20261124071836.208740-3-rishab@rispop.dev> (raw)
In-Reply-To: <20261124071836.208740-1-rishab@rispop.dev>

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



      parent reply	other threads:[~2026-11-24  7:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124071836.208740-3-rishab@rispop.dev \
    --to=rishab@rispop.dev \
    --cc=alistair.francis@wdc.com \
    --cc=chao.liu@processmission.com \
    --cc=christopher.elwood2024@gmail.com \
    --cc=daniel.barboza@oss.qualcomm.com \
    --cc=liwei1518@gmail.com \
    --cc=palmer@dabbelt.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=tarun.patanjali@utexas.edu \
    --cc=zhiwei_liu@linux.alibaba.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.