Commit 5a050f59c1 for qemu.org
commit 5a050f59c1b9e4f97253d5028a72aa3d45ba5e8a
Author: Frank Mehnert <frank.mehnert@gmail.com>
Date: Tue Dec 1 10:27:35 2026 +0000
target/arm: set the event register on a failed store-exclusive
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.
Cc: qemu-stable@nongnu.org
Fixes: 01b223f895a7 ("target/arm: enable WFE sleeping for A-profile")
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4938
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Frank Mehnert <frank.mehnert@kernkonzept.com>
Message-id: 20261126143258.1056688-2-frank.mehnert@kernkonzept.com
[PMM: expand the comments a bit]
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
diff --git a/target/arm/tcg/translate-a64.c b/target/arm/tcg/translate-a64.c
index 14888202ba..d2167d1c28 100644
--- a/target/arm/tcg/translate-a64.c
+++ b/target/arm/tcg/translate-a64.c
@@ -3531,10 +3531,19 @@ 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 for this CPU
+ * was cleared, which is a transition that sets the event register.
+ * Architecturally the transition and the wakeup event should have
+ * happened when the store by the other CPU occurred; because we
+ * don't model the local or global monitors we do it here, which is
+ * late but good enough for the usual "lock loop with a WFE" idiom.
+ */
+ 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 9d80294c9b..62746a6a82 100644
--- a/target/arm/tcg/translate.c
+++ b/target/arm/tcg/translate.c
@@ -2137,10 +2137,19 @@ 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 for this CPU
+ * was cleared, which is a transition that sets the event register.
+ * Architecturally the transition and the wakeup event should have
+ * happened when the store by the other CPU occurred; because we
+ * don't model the local or global monitors we do it here, which is
+ * late but good enough for the usual "lock loop with a WFE" idiom.
+ */
+ gen_event_reg();
gen_set_label(done_label);
tcg_gen_movi_i64(cpu_exclusive_addr, -1);
}