* [PATCH for-11.2] target/arm: set the event register on a failed store-exclusive
@ 2026-11-25 8:21 Frank Mehnert
2026-11-26 11:42 ` Alex Bennée
0 siblings, 1 reply; 3+ messages in thread
From: Frank Mehnert @ 2026-11-25 8:21 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-arm, peter.maydell, alex.bennee, Frank Mehnert
Since WFE may sleep on A-profile, a WFE executed outside an exclusive
region halts the vCPU until an event arrives. Lock code commonly
branches back to WFE after a failed store-exclusive, for example:
1: wfe
ldaxr w1, [x0]
cbnz w1, 1b
stxr w2, w3, [x0]
cbnz w2, 1b
The store-exclusive clears exclusive_addr, so the following WFE sleeps.
On hardware, the store by another PE that made the store-exclusive fail
cleared the global monitor of this PE, and clearing the global monitor
sets the Event Register (Arm ARM D1.6.1, R_XRZRK). The WFE then
completes immediately. QEMU does not model this, so with interrupts
masked and no event stream the vCPU sleeps until another PE executes
SEV, which such lock code never does.
Set the Event Register when a store-exclusive fails, in both the
AArch64 and the AArch32 translator. A failure for another reason causes
a spurious wakeup, which the architecture permits.
Fixes: 01b223f895a7 ("target/arm: enable WFE sleeping for A-profile")
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4938
Signed-off-by: Frank Mehnert <frank.mehnert@kernkonzept.com>
---
target/arm/tcg/translate-a64.c | 7 ++++++-
target/arm/tcg/translate.c | 7 ++++++-
2 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/target/arm/tcg/translate-a64.c b/target/arm/tcg/translate-a64.c
index 49472f7d97..45f3b3df03 100644
--- a/target/arm/tcg/translate-a64.c
+++ b/target/arm/tcg/translate-a64.c
@@ -3531,10 +3531,15 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
}
tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
- tcg_gen_br(done_label);
+ tcg_gen_brcondi_i64(TCG_COND_EQ, tmp, 0, done_label);
gen_set_label(fail_label);
tcg_gen_movi_i64(cpu_reg(s, rd), 1);
+ /*
+ * A failing store-exclusive means the global monitor was cleared, which
+ * generates a WFE wakeup event.
+ */
+ gen_event_reg();
gen_set_label(done_label);
tcg_gen_movi_i64(cpu_exclusive_addr, -1);
}
diff --git a/target/arm/tcg/translate.c b/target/arm/tcg/translate.c
index 442c34ce84..2eef2ceef2 100644
--- a/target/arm/tcg/translate.c
+++ b/target/arm/tcg/translate.c
@@ -2137,10 +2137,15 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t2);
}
tcg_gen_mov_i32(cpu_R[rd], t0);
- tcg_gen_br(done_label);
+ tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, done_label);
gen_set_label(fail_label);
tcg_gen_movi_i32(cpu_R[rd], 1);
+ /*
+ * A failing store-exclusive means the global monitor was cleared, which
+ * generates a WFE wakeup event.
+ */
+ gen_event_reg();
gen_set_label(done_label);
tcg_gen_movi_i64(cpu_exclusive_addr, -1);
}
--
2.55.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH for-11.2] target/arm: set the event register on a failed store-exclusive
2026-11-25 8:21 [PATCH for-11.2] target/arm: set the event register on a failed store-exclusive Frank Mehnert
@ 2026-11-26 11:42 ` Alex Bennée
2026-11-26 14:31 ` Frank Mehnert
0 siblings, 1 reply; 3+ messages in thread
From: Alex Bennée @ 2026-11-26 11:42 UTC (permalink / raw)
To: Frank Mehnert; +Cc: qemu-devel, qemu-arm, peter.maydell, Frank Mehnert
Frank Mehnert <frank.mehnert@gmail.com> writes:
> Since WFE may sleep on A-profile, a WFE executed outside an exclusive
> region halts the vCPU until an event arrives. Lock code commonly
> branches back to WFE after a failed store-exclusive, for example:
>
> 1: wfe
> ldaxr w1, [x0]
> cbnz w1, 1b
> stxr w2, w3, [x0]
> cbnz w2, 1b
>
> The store-exclusive clears exclusive_addr, so the following WFE sleeps.
> On hardware, the store by another PE that made the store-exclusive fail
> cleared the global monitor of this PE, and clearing the global monitor
> sets the Event Register (Arm ARM D1.6.1, R_XRZRK). The WFE then
> completes immediately. QEMU does not model this, so with interrupts
> masked and no event stream the vCPU sleeps until another PE executes
> SEV, which such lock code never does.
>
> Set the Event Register when a store-exclusive fails, in both the
> AArch64 and the AArch32 translator. A failure for another reason causes
> a spurious wakeup, which the architecture permits.
>
> Fixes: 01b223f895a7 ("target/arm: enable WFE sleeping for A-profile")
> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4938
> Signed-off-by: Frank Mehnert <frank.mehnert@kernkonzept.com>
Looks good to me - is it possible to extend the wxft test case with a
test case to exercise this common lock pattern?
For the fix though:
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
> ---
> target/arm/tcg/translate-a64.c | 7 ++++++-
> target/arm/tcg/translate.c | 7 ++++++-
> 2 files changed, 12 insertions(+), 2 deletions(-)
>
> diff --git a/target/arm/tcg/translate-a64.c b/target/arm/tcg/translate-a64.c
> index 49472f7d97..45f3b3df03 100644
> --- a/target/arm/tcg/translate-a64.c
> +++ b/target/arm/tcg/translate-a64.c
> @@ -3531,10 +3531,15 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
> tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
> }
> tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
> - tcg_gen_br(done_label);
> + tcg_gen_brcondi_i64(TCG_COND_EQ, tmp, 0, done_label);
>
> gen_set_label(fail_label);
> tcg_gen_movi_i64(cpu_reg(s, rd), 1);
> + /*
> + * A failing store-exclusive means the global monitor was cleared, which
> + * generates a WFE wakeup event.
> + */
> + gen_event_reg();
> gen_set_label(done_label);
> tcg_gen_movi_i64(cpu_exclusive_addr, -1);
> }
> diff --git a/target/arm/tcg/translate.c b/target/arm/tcg/translate.c
> index 442c34ce84..2eef2ceef2 100644
> --- a/target/arm/tcg/translate.c
> +++ b/target/arm/tcg/translate.c
> @@ -2137,10 +2137,15 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
> tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t2);
> }
> tcg_gen_mov_i32(cpu_R[rd], t0);
> - tcg_gen_br(done_label);
> + tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, done_label);
>
> gen_set_label(fail_label);
> tcg_gen_movi_i32(cpu_R[rd], 1);
> + /*
> + * A failing store-exclusive means the global monitor was cleared, which
> + * generates a WFE wakeup event.
> + */
> + gen_event_reg();
> gen_set_label(done_label);
> tcg_gen_movi_i64(cpu_exclusive_addr, -1);
> }
--
Alex Bennée
Virtualisation Tech Lead @ Linaro
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH for-11.2] target/arm: set the event register on a failed store-exclusive
2026-11-26 11:42 ` Alex Bennée
@ 2026-11-26 14:31 ` Frank Mehnert
0 siblings, 0 replies; 3+ messages in thread
From: Frank Mehnert @ 2026-11-26 14:31 UTC (permalink / raw)
To: Alex Bennée; +Cc: qemu-devel, qemu-arm, peter.maydell
Hi Alex,
On Donnerstag, 26. November 2026 12:42 Alex Bennée wrote:
> Frank Mehnert <frank.mehnert@gmail.com> writes:
> > [...]
> >
> > Fixes: 01b223f895a7 ("target/arm: enable WFE sleeping for A-profile")
> > Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4938
> > Signed-off-by: Frank Mehnert <frank.mehnert@kernkonzept.com>
>
> Looks good to me - is it possible to extend the wxft test case with a
> test case to exercise this common lock pattern?
Yes. I added a test case to tests/tcg/aarch64/system/wfxt.c and will
send it together with the fix as v2.
The test does not depend on a race between the CPUs. CPU 0 executes
LDAXR on the free lock, then CPU 1, started through PSCI CPU_ON, takes
the lock with a plain store. The following STXR of CPU 0 must fail,
and the WFE after it must complete at once, with interrupts masked and
without any SEV. Like the WFIT/WFET cases, the test uses the virtual
timer interrupt, 1s out, as a watchdog, so a WFE that misses the event
makes the test fail instead of hanging.
The test needs a second CPU, so wfxt.c now runs with -smp 2.
With the fix, the test passed in 80 of 80 runs, with both
multi-threaded and single-threaded TCG, built with -O0 and -O2. With
the fix reverted, it failed in 20 of 20 runs: WFE woke only on the
timer interrupt after 1s.
> For the fix though:
>
> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
>
> > [...]
I've added your Reviewed-by to the fix.
Thanks!
Frank
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-26 14:32 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 8:21 [PATCH for-11.2] target/arm: set the event register on a failed store-exclusive Frank Mehnert
2026-11-26 11:42 ` Alex Bennée
2026-11-26 14:31 ` Frank Mehnert
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.