From nobody Mon Nov 23 04:52:50 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 1792001813637323.7008403397541; Wed, 14 Oct 2026 11:16:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xH3WY-0002T5-5i; Wed, 14 Oct 2026 14:16:22 -0400 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 1xH3WR-0002Sq-8u; Wed, 14 Oct 2026 14:16:15 -0400 Received: from smtp21.cstnet.cn ([159.226.251.21] 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 1xH3WN-00010h-Uv; Wed, 14 Oct 2026 14:16:15 -0400 Received: from DESKTOP-7FLBREN (unknown [124.16.137.194]) by APP-01 (Coremail) with SMTP id qwCowADnuOzgxs9qifjeCg--.49849S4; Thu, 15 Oct 2026 02:16:01 +0800 (CST) From: wangyang To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, palmer@dabbelt.com, alistair.francis@wdc.com, daniel.barboza@oss.qualcomm.com Subject: [PATCH] linux-user/riscv: preserve rs1 address for cbo.zero fault reporting Date: Thu, 15 Oct 2026 02:15:59 +0800 Message-ID: <20261014181559.1567-1-wangyang25@otcaix.iscas.ac.cn> X-Mailer: git-send-email 2.55.0.windows.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qwCowADnuOzgxs9qifjeCg--.49849S4 X-Coremail-Antispam: 1UD129KBjvJXoW3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: [124.16.137.194] X-CM-SenderInfo: 5zdqw5xdqjjk46rwut1l0ox2xfdvhtffof0/ 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.21; envelope-from=wangyang25@otcaix.iscas.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: 1792001818744158500 Content-Type: text/plain; charset="utf-8" helper_cbo_zero() aligns the address down to the cache-block size before probe_write(), and accel/tcg/user-exec.c:probe_access_internal() reports that aligned address to cpu_loop_exit_sigsegv(), so a store fault from a legal cbo.zero (e.g. on a read-only page) reports the aligned block base as si_addr instead of the rs1 value supplied by the program. Native RISC-V Linux reports the exact rs1 address. Keep the original address in CPUState and use it when the fault is reported. The saved address must not outlive the instruction that produced it. The helper alone cannot guarantee that, because a store trigger or breakpoint on the cbo.zero access leaves through the debug path instead of one of the four fault consumers, and a later unrelated fault would then report the stale cbo.zero address. Also drop the pending override on every trap delivery: at the top of riscv_cpu_do_interrupt() and in riscv_cpu_debug_excp_handler() for system mode, and right after cpu_exec() returns in the linux-user cpu loop. Tested: the read-only-page reproducer reports rs1 for offsets 1, 65 and 129; an mcontrol store-trigger reproducer reports the follow-up load's own access-fault address instead of the earlier cbo.zero rs1. Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4151 Suggested-by: David Harris Signed-off-by: wangyang --- accel/tcg/user-exec.c | 7 ++++++- include/hw/core/cpu.h | 2 ++ linux-user/riscv/cpu_loop.c | 3 +++ target/riscv/tcg/cpu_helper.c | 27 ++++++++++++++++++++++++--- target/riscv/tcg/debug.c | 3 +++ target/riscv/tcg/op_helper.c | 5 +++++ 6 files changed, 43 insertions(+), 4 deletions(-) diff --git a/accel/tcg/user-exec.c b/accel/tcg/user-exec.c index a35aca7889..13c3845811 100644 --- a/accel/tcg/user-exec.c +++ b/accel/tcg/user-exec.c @@ -787,7 +787,12 @@ static int probe_access_internal(CPUArchState *env, va= ddr addr, return TLB_INVALID_MASK; } =20 - cpu_loop_exit_sigsegv(env_cpu(env), addr, access_type, maperr, ra); + CPUState *cpu =3D env_cpu(env); + if (cpu->exception_addr_valid) { + addr =3D cpu->exception_addr; + cpu->exception_addr_valid =3D false; + } + cpu_loop_exit_sigsegv(cpu, addr, access_type, maperr, ra); } =20 int probe_access_flags(CPUArchState *env, vaddr addr, int size, diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index ae66f5e33e..1fd68367f6 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -560,6 +560,8 @@ struct CPUState { uint32_t tcg_cflags; uint32_t halted; int32_t exception_index; + vaddr exception_addr; + bool exception_addr_valid; =20 bool vcpu_dirty; AccelCPUState *accel; diff --git a/linux-user/riscv/cpu_loop.c b/linux-user/riscv/cpu_loop.c index 6f27a06da4..dbf1d42d33 100644 --- a/linux-user/riscv/cpu_loop.c +++ b/linux-user/riscv/cpu_loop.c @@ -38,6 +38,9 @@ void cpu_loop(CPURISCVState *env) cpu_exec_end(cs); qemu_process_cpu_events(cs); =20 + /* A trap ends the pending cbo.zero address override window. */ + cs->exception_addr_valid =3D false; + switch (trapnr) { case EXCP_INTERRUPT: /* just indicate that signals should be handled asap */ diff --git a/target/riscv/tcg/cpu_helper.c b/target/riscv/tcg/cpu_helper.c index 07d9222652..4513c9bdbe 100644 --- a/target/riscv/tcg/cpu_helper.c +++ b/target/riscv/tcg/cpu_helper.c @@ -1537,7 +1537,12 @@ static void raise_mmu_exception(CPURISCVState *env, = target_ulong address, default: g_assert_not_reached(); } - env->badaddr =3D address; + if (cs->exception_addr_valid) { + env->badaddr =3D cs->exception_addr; + cs->exception_addr_valid =3D false; + } else { + env->badaddr =3D address; + } env->two_stage_lookup =3D two_stage; env->two_stage_indirect_lookup =3D two_stage_indirect; } @@ -1588,7 +1593,12 @@ void riscv_cpu_do_transaction_failed(CPUState *cs, h= waddr physaddr, cs->exception_index =3D RISCV_EXCP_INST_ACCESS_FAULT; } =20 - env->badaddr =3D addr; + if (cs->exception_addr_valid) { + env->badaddr =3D cs->exception_addr; + cs->exception_addr_valid =3D false; + } else { + env->badaddr =3D addr; + } env->two_stage_lookup =3D mmuidx_2stage(mmu_idx); env->two_stage_indirect_lookup =3D false; cpu_loop_exit_restore(cs, retaddr); @@ -1621,7 +1631,12 @@ void riscv_cpu_do_unaligned_access(CPUState *cs, vad= dr addr, default: g_assert_not_reached(); } - env->badaddr =3D addr; + if (cs->exception_addr_valid) { + env->badaddr =3D cs->exception_addr; + cs->exception_addr_valid =3D false; + } else { + env->badaddr =3D addr; + } env->two_stage_lookup =3D mmuidx_2stage(mmu_idx); env->two_stage_indirect_lookup =3D false; cpu_loop_exit_restore(cs, retaddr); @@ -2047,6 +2062,12 @@ void riscv_cpu_do_interrupt(CPUState *cs) { RISCVCPU *cpu =3D RISCV_CPU(cs); CPURISCVState *env =3D &cpu->env; + + /* + * A trap ends the pending cbo.zero address override window, so drop + * any saved rs1 that a store trigger or breakpoint left behind. + */ + cs->exception_addr_valid =3D false; bool virt =3D env->virt_enabled; bool write_gva =3D false; bool always_storeamo =3D (env->excp_uw2 & RISCV_UW2_ALWAYS_STORE_AMO); diff --git a/target/riscv/tcg/debug.c b/target/riscv/tcg/debug.c index e4d719a1da..6962557251 100644 --- a/target/riscv/tcg/debug.c +++ b/target/riscv/tcg/debug.c @@ -973,6 +973,9 @@ void riscv_cpu_debug_excp_handler(CPUState *cs) RISCVCPU *cpu =3D RISCV_CPU(cs); CPURISCVState *env =3D &cpu->env; =20 + /* A debug exit ends the cbo.zero address override window too. */ + cs->exception_addr_valid =3D false; + if (cs->watchpoint_hit) { if (cs->watchpoint_hit->flags & BP_CPU) { do_trigger_action(env, DBG_ACTION_BP); diff --git a/target/riscv/tcg/op_helper.c b/target/riscv/tcg/op_helper.c index b67c7718db..3fc993a207 100644 --- a/target/riscv/tcg/op_helper.c +++ b/target/riscv/tcg/op_helper.c @@ -188,9 +188,13 @@ void helper_cbo_zero(CPURISCVState *env, target_ulong = address) int mmu_idx =3D riscv_env_mmu_index(env, false); uintptr_t ra =3D GETPC(); void *mem; + CPUState *cs =3D env_cpu(env); =20 check_zicbo_envcfg(env, MENVCFG_CBZE, ra); =20 + cs->exception_addr =3D address; + cs->exception_addr_valid =3D true; + /* Mask off low-bits to align-down to the cache-block. */ address &=3D ~(cbozlen - 1); =20 @@ -218,6 +222,7 @@ void helper_cbo_zero(CPURISCVState *env, target_ulong a= ddress) cpu_stb_mmuidx_ra(env, address + i, 0, mmu_idx, ra); } } + cs->exception_addr_valid =3D false; } =20 /* --=20 2.55.0.windows.2