From: "Alex Bennée" <alex.bennee@linaro.org>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: Frank Mehnert <frank.mehnert@gmail.com>,
qemu-devel@nongnu.org, qemu-arm@nongnu.org,
Frank Mehnert <frank.mehnert@kernkonzept.com>,
Richard Henderson <richard.henderson@linaro.org>
Subject: Re: [PATCH for-11.2 v2 1/2] target/arm: set the event register on a failed store-exclusive
Date: Mon, 30 Nov 2026 13:56:17 +0000 [thread overview]
Message-ID: <875wxe5sou.fsf@draig.linaro.org> (raw)
In-Reply-To: <CAFEAcA9hm7zA3cL-VBDevT5B6bhwOK18RgjDCnWfE3047mwzrQ@mail.gmail.com> (Peter Maydell's message of "Mon, 30 Nov 2026 10:10:06 +0000")
Peter Maydell <peter.maydell@linaro.org> writes:
> On Thu, 26 Nov 2026 at 14:33, Frank Mehnert <frank.mehnert@gmail.com> wrote:
>>
>> 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
>> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
>> 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.
>> + */
>
> We might add "Architecturally the wakeup event should have happened when
> the store by the other CPU happened, but we do it here."
>
>> + gen_event_reg();
>> gen_set_label(done_label);
>> tcg_gen_movi_i64(cpu_exclusive_addr, -1);
>
> The architecture permits the global monitor to be cleared when a
> StoreExcl() succeeds as well as when it fails. Given that gen_event_reg()
> is just a byte-store, we could also unconditionally do it after the
> done_label, to avoid the branching around. Not sure which is
> preferable.
Isn't the global monitor subtly different from the per-vcpu event
register in this case as we are modelling the effect of it being cleared
without properly modelling the actual global monitor itself? Or does our
messing with cpu_exclusive_addr count?
However making the store occur always still passes the test case so I
don't have any objection if this is easier.
>
>> }
>> 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
>
> -- PMM
--
Alex Bennée
Virtualisation Tech Lead @ Linaro
next prev parent reply other threads:[~2026-11-30 13:56 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 14:32 [PATCH for-11.2 v2 0/2] target/arm: WFE wakeup after a failed store-exclusive Frank Mehnert
2026-11-26 14:32 ` [PATCH for-11.2 v2 1/2] target/arm: set the event register on " Frank Mehnert
2026-11-30 10:10 ` Peter Maydell
2026-11-30 13:56 ` Alex Bennée [this message]
2026-11-30 15:00 ` Peter Maydell
2026-11-30 15:53 ` Alex Bennée
2026-11-30 16:23 ` Peter Maydell
2026-11-30 16:35 ` Frank Mehnert
2026-11-30 16:53 ` Peter Maydell
2026-11-26 14:32 ` [PATCH for-11.2 v2 2/2] tests/tcg/aarch64: test WFE after " Frank Mehnert
2026-11-26 15:09 ` Alex Bennée
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=875wxe5sou.fsf@draig.linaro.org \
--to=alex.bennee@linaro.org \
--cc=frank.mehnert@gmail.com \
--cc=frank.mehnert@kernkonzept.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
/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.