Skip to contentSkip to conversation

target-arm queue

GIT PULL Applied Peter Maydell 2 participants 4 messages Dec 1 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

Hi; here's a target-arm pull for this week's RC. Just one bug fix (plus its test case), which fixes hangs due to WFE not being woken up when used in a common load/store-exclusive lock pattern.

thanks
-- PMM

The following changes since commit fae886567b48c672977acde6b775dd7e2d2121c1:

Merge tag 'hw-misc-20261130' of https://github.com/philmd/qemu into staging (2026-11-30 14:02:58 -0800)

are available in the Git repository at:

https://gitlab.com/pm215/qemu.git tags/pull-target-arm-20261201-1

for you to fetch changes up to 7341f17fe3377b25984cd68909ba5e0a22fa88ec:

tests/tcg/aarch64: test WFE after a failed store-exclusive (2026-12-01 13:38:09 +0000)

----------------------------------------------------------------
target-arm queue:
* target/arm: set the event register on a failed store-exclusive

----------------------------------------------------------------
Frank Mehnert (2):
target/arm: set the event register on a failed store-exclusive tests/tcg/aarch64: test WFE after a failed store-exclusive

 target/arm/tcg/translate-a64.c       |  11 +++-
 target/arm/tcg/translate.c           |  11 +++-
 tests/tcg/aarch64/system/wfxt.c      | 103 ++++++++++++++++++++++++++++++++++-
 tests/tcg/aarch64/system/meson.build |   2 +-
 4 files changed, 123 insertions(+), 4 deletions(-)
#1 lore raw

[PULL 1/2] target/arm: set the event register on a failed store-exclusive

From: Frank Mehnert <[email protected]>

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

Message-id: [email protected]

[PMM: expand the comments a bit]
---
 target/arm/tcg/translate-a64.c | 11 ++++++++++-
 target/arm/tcg/translate.c     | 11 ++++++++++-
 2 files changed, 20 insertions(+), 2 deletions(-)
target/arm/tcg/translate-a64.c +10 -1
index 14888202bad..d2167d1c280 100644
@@ -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);
 }
target/arm/tcg/translate.c +10 -1
index 9d80294c9bb..62746a6a822 100644
@@ -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);
 }
Signature · 1 line
2.43.0
#2 lore raw

[PULL 2/2] tests/tcg/aarch64: test WFE after a failed store-exclusive

In reply to Peter Maydell

From: Frank Mehnert <[email protected]>

Add a test case for the common spin lock pattern "WFE, load-exclusive, store-exclusive, on failure back to WFE", where the lock is released by a plain store and without SEV.

CPU 0 executes LDAXR on the free lock, then CPU 1 takes the lock with a plain store. The following STXR of CPU 0 must fail, and the WFE after it must complete at once because the store of CPU 1 cleared the global monitor of CPU 0, which generates a WFE wakeup event. The virtual timer interrupt, 1s out, ends a WFE that misses the event, so the test fails instead of hanging.

The test starts CPU 1 through PSCI and therefore needs two CPUs.

Message-id: [email protected]

---
 tests/tcg/aarch64/system/wfxt.c      | 103 ++++++++++++++++++++++++++-
 tests/tcg/aarch64/system/meson.build |   2 +-
 2 files changed, 103 insertions(+), 2 deletions(-)
tests/tcg/aarch64/system/wfxt.c +102 -1
index 9d50590a684..2cec6cfbbec 100644
@@ -1,5 +1,5 @@
 /*
- * WFIT/WFET timeout test
+ * WFIT/WFET timeout test, WFE wakeup after a failed store-exclusive
  *
  * Copyright (c) 2026 Google LLC
  * Author: Fuad Tabba <[email protected]>
@@ -91,6 +91,106 @@ static int test_one(const char *name, void (*wait)(uint64_t), uint64_t freq)
     return 0;
 }
 
+/*
+ * At EL2 with virtualization enabled, QEMU's PSCI implementation uses the
+ * SMC conduit.
+ */
+#define PSCI_CPU_ON 0xc4000003UL
+
+static uint64_t psci_cpu_on(uint64_t mpidr, uint64_t entry)
+{
+    register uint64_t x0 asm("x0") = PSCI_CPU_ON;
+    register uint64_t x1 asm("x1") = mpidr;
+    register uint64_t x2 asm("x2") = entry;
+    register uint64_t x3 asm("x3") = 0;
+
+    asm volatile("smc #0" : "+r" (x0) : "r" (x1), "r" (x2), "r" (x3)
+                 : "memory");
+    return x0;
+}
+
+static uint32_t lock_word;
+static uint32_t lock_step;
+
+asm(".pushsection .bss\n"
+    ".balign 16\n"
+    "secondary_stack:\n"
+    ".space 4096\n"
+    "secondary_stack_end:\n"
+    ".popsection\n"
+    ".global secondary_entry\n"
+    "secondary_entry:\n"
+    "adrp x0, secondary_stack_end\n"
+    "add x0, x0, :lo12:secondary_stack_end\n"
+    "mov sp, x0\n"
+    "b secondary_main\n");
+
+/* CPU 1 takes the lock while CPU 0 is between LDAXR and STXR. */
+static void __attribute__((used)) secondary_main(void)
+{
+    while (__atomic_load_n(&lock_step, __ATOMIC_ACQUIRE) != 1) {
+    }
+    __atomic_store_n(&lock_word, 1, __ATOMIC_RELAXED);
+    __atomic_store_n(&lock_step, 2, __ATOMIC_RELEASE);
+
+    for (;;) {
+        asm volatile("wfi");
+    }
+}
+
+/*
+ * Spin lock pattern: "LDAXR, STXR, on failure WFE" with a lock released
+ * by a plain store and without SEV. The store-exclusive fails because
+ * another CPU took the lock. That store cleared the global monitor of
+ * this CPU, which generates a WFE wakeup event, so the following WFE
+ * must complete at once.
+ */
+static int test_wfe_lock(uint64_t freq)
+{
+    uint64_t entry, now, elapsed;
+    uint32_t val, status;
+
+    asm("adrp %0, secondary_entry\n"
+        "add %0, %0, :lo12:secondary_entry" : "=r" (entry));
+    if (psci_cpu_on(1, entry) != 0) {
+        ml_printf("FAIL: cannot start CPU 1\n");
+        return 1;
+    }
+
+    /* Clear the event register */
+    asm volatile("sevl; wfe");
+
+    asm volatile("ldaxr %w0, [%1]" : "=r" (val) : "r" (&lock_word)
+                 : "memory");
+    __atomic_store_n(&lock_step, 1, __ATOMIC_RELEASE);
+    while (__atomic_load_n(&lock_step, __ATOMIC_ACQUIRE) != 2) {
+    }
+    asm volatile("stxr %w0, %w2, [%1]" : "=&r" (status)
+                 : "r" (&lock_word), "r" (1) : "memory");
+
+    if (val != 0 || status == 0) {
+        ml_printf("FAIL: store-exclusive succeeded on a taken lock\n");
+        return 1;
+    }
+
+    /* The virtual timer interrupt, 1s out, wakes the CPU if WFE sleeps */
+    now = read_sysreg(cntvct_el0);
+    write_sysreg(cntv_cval_el0, now + freq);
+    write_sysreg(cntv_ctl_el0, 1);
+
+    asm volatile("wfe");
+
+    elapsed = read_sysreg(cntvct_el0) - now;
+    write_sysreg(cntv_ctl_el0, 0);
+
+    ml_printf("wfe after failed stxr: woke after %ld ticks\n", elapsed);
+    if (elapsed > freq / 2) {
+        ml_printf("FAIL: wfe missed the event of the failed stxr\n");
+        return 1;
+    }
+    return 0;
+}
+
 int main(void)
 {
     uint64_t freq;
@@ -113,6 +213,7 @@ int main(void)
 
     ret = test_one("wfit", wfit, freq);
     ret |= test_one("wfet", wfet, freq);
+    ret |= test_wfe_lock(freq);
 
     return ret;
 }
tests/tcg/aarch64/system/meson.build +1 -1
index 8710ebfdad3..a3cc77c1a0d 100644
@@ -75,7 +75,7 @@ tests += {
   'wfxt.c': {
     'cflags': cflags,
     'qemu_args': ['-M', 'virt,virtualization=on,gic-version=2',
-                  '-cpu', 'max', '-smp', '1',
+                  '-cpu', 'max', '-smp', '2',
                   '-semihosting-config', 'enable=on,arg=2',
                   qemu_base_args]
   },
Signature · 1 line
2.43.0
#3 lore raw
In reply to Peter Maydell
Quoted 15 lines · Peter Maydell

Hi; here's a target-arm pull for this week's RC. Just one bug fix (plus its test case), which fixes hangs due to WFE not being woken up when used in a common load/store-exclusive lock pattern.

thanks
-- PMM

The following changes since commit fae886567b48c672977acde6b775dd7e2d2121c1:

   Merge tag 'hw-misc-20261130' ofhttps://github.com/philmd/qemu into staging (2026-11-30 14:02:58 -0800)

are available in the Git repository at:

   https://gitlab.com/pm215/qemu.git tags/pull-target-arm-20261201-1
Quoted 7 lines

for you to fetch changes up to 7341f17fe3377b25984cd68909ba5e0a22fa88ec:

   tests/tcg/aarch64: test WFE after a failed store-exclusive (2026-12-01 13:38:09 +0000)

----------------------------------------------------------------
target-arm queue:
* target/arm: set the event register on a failed store-exclusive

Applied, thanks.

r~