Commit 7341f17fe3 for qemu.org

commit 7341f17fe3377b25984cd68909ba5e0a22fa88ec
Author: Frank Mehnert <frank.mehnert@gmail.com>
Date:   Tue Dec 1 10:27:35 2026 +0000

    tests/tcg/aarch64: test WFE after a failed store-exclusive

    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.

    Signed-off-by: Frank Mehnert <frank.mehnert@kernkonzept.com>
    Tested-by: Alex Bennée <alex.bennee@linaro.org>
    Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
    Message-id: 20261126143258.1056688-3-frank.mehnert@kernkonzept.com
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

diff --git a/tests/tcg/aarch64/system/meson.build b/tests/tcg/aarch64/system/meson.build
index 8710ebfdad..a3cc77c1a0 100644
--- a/tests/tcg/aarch64/system/meson.build
+++ b/tests/tcg/aarch64/system/meson.build
@@ -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]
   },
diff --git a/tests/tcg/aarch64/system/wfxt.c b/tests/tcg/aarch64/system/wfxt.c
index 9d50590a68..2cec6cfbbe 100644
--- a/tests/tcg/aarch64/system/wfxt.c
+++ b/tests/tcg/aarch64/system/wfxt.c
@@ -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 <fuad.tabba@linux.dev>
@@ -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;
 }