Skip to contentSkip to conversation

target/arm: fully model WFxT instructions for A-profile

PATCH Applied Alex Bennée 4 participants 14 messages Jun 24 – Oct 26

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v9 cover target/arm: fully model WFxT instructions for A-profile

This series fully models the behaviour of WFxT instructions. We already had support for WFE for M-profile but we left off A-profile as it has more potential sources of wake-ups. The main one is the event stream which includes events from significant bits of the timer ticking over.

The refactoring from the previous iterations of the patch have now been merged.

I've dropped trying to fully model the global monitor in favour of taking advantage of the architectural flexibility to have an IMPDEF event wake up for any reason. We treat the setting of exclusive_addr as such a reason because it indicates the current vCPU is in a ldstx exclusive region and we don't want the guest to deadlock. This does mean the system won't sleep on WFE enabled locks but people shouldn't be relying on QEMU to model real world sleep patterns anyway given the efficiency of emulation compared to real HW.

I've written a test case using kvm-unit-tests:

Message-ID: <[email protected]>
Date: Wed, 27 May 2026 12:18:21 +0100
Subject: [kvm-unit-tests PATCH v2] arm: add wfx test case
From: =?UTF-8?q?Alex=20Benn=C3=A9e?= <[email protected]>

The only remaining unreviewed patch is:

target/arm: ensure we create the wxft_timer for all modes

v9
- add more r-b's
- only test arm_feature(env, ARM_FEATURE_V6K)

v8
- Fixed quanta-gsj model SEV errata failure
- Update the aspeed_anacapa test case
- Ensure wake up events for V6K/V7 and plain V8
- merged trap with implementation

v7
- more r-b's
- extended comment for typo fix
v6
- add typo fix
- addressed rth's comments

v5
- addressed rth's comments
- all but one patch now reviewed

v4
- precursor refactor patches now merged
- dropped global monitor emulation
- addressed other comments
- tested with check-tcg and kvm-unit-test wfx tests

v3
- fixed the WFE exception handling

v2
- I've kept the tests separate, see:

    Message-ID: <[email protected]>
    Date: Fri, 17 Apr 2026 17:43:20 +0100
    Subject: [PATCH 0/7] tests/tcg: more capabilities for aarch64-softmmu tests
    From: =?UTF-8?q?Alex=20Benn=C3=A9e?= <[email protected]>

- tweaks to global monitor behaviour
- STLR/STL will also trigger events to avoid deadlocks
- see under the --- in the patches for details

Alex.

Alex Bennée (7):
hw/arm: use cortex-a9 mpcore base for CBAR on npcm7xx machines tests/functional: update anacapa-bmc image target/arm: do not clear halting reason in has_work helper target/arm: ensure we create the wxft_timer for all modes target/arm: implements SEV/SEVL for all modes target/arm: enable WFE sleeping for A-profile target/arm: implement WFET

 include/hw/core/sysemu-cpu-ops.h            |   3 +
 target/arm/cpu.h                            |   2 +-
 target/arm/tcg/helper-defs.h                |   3 +-
 target/arm/tcg/translate.h                  |  18 ++
 target/arm/tcg/a32.decode                   |   5 +-
 target/arm/tcg/a64.decode                   |   5 +-
 target/arm/tcg/t16.decode                   |   4 +-
 target/arm/tcg/t32.decode                   |   4 +-
 hw/arm/npcm7xx.c                            |   2 +-
 target/arm/cpu.c                            |  23 +-
 target/arm/tcg/op_helper.c                  | 252 ++++++++++++++++++--
 target/arm/tcg/translate-a64.c              |  42 ++--
 target/arm/tcg/translate.c                  |  34 +--
 tests/functional/arm/test_aspeed_anacapa.py |   4 +-
 14 files changed, 328 insertions(+), 73 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v9 1/7 hw/arm: use cortex-a9 mpcore base for CBAR on npcm7xx machines

CBAR is an IMPDEF register and according to the A9 TRM [1]:

In Cortex-A9 MPCore implementations, the base address is reset to PERIPHBASE[31:13] so that software can determine the location of the private memory region [2].

If it doesn't we will confuse the Linux kernel as it probes the system SCU registers [3] and erroneously assumes the system is a buggy Aegis SOC and nerf the emission of SEV instructions, deadlocking any WFE's in the kernel (or QEMU smpboot code).

[1] https://developer.arm.com/documentation/ddi0388/i/system-control/register-descriptions/configuration-base-address-register
[2] https://developer.arm.com/documentation/ddi0407/g/Introduction/Private-Memory-Region
[3] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/arm/kernel/head.S?h=v7.1#n550

Fixes 2d8f048c25ab ("hw/arm: Add NPCM730 and NPCM750 SoC models")

---
v2
- rewrote commit message for clarity, added links
- used arnd's arndb.de address

---
 hw/arm/npcm7xx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/arm/npcm7xx.c +1 -1
index c2bbcd89dbc..c27f149c04a 100644
@@ -492,7 +492,7 @@ static void npcm7xx_realize(DeviceState *dev, Error **errp)
     /* CPUs */
     for (i = 0; i < nc->num_cpus; i++) {
         object_property_set_int(OBJECT(&s->cpu[i]), "reset-cbar",
-                                NPCM7XX_GIC_CPU_IF_ADDR, &error_abort);
+                                NPCM7XX_CPUP_BA, &error_abort);
         object_property_set_bool(OBJECT(&s->cpu[i]), "reset-hivecs", true,
                                  &error_abort);
 
Signature · 1 line
2.47.3
#2 lore raw

PATCH v9 2/7 tests/functional: update anacapa-bmc image

In reply to Alex Bennée

The initial version had the wrong DTB which caused issues with image corruption [1]. Update to the latest version.

[1] https://github.com/legoater/qemu-aspeed-boot/pull/7

---
 tests/functional/arm/test_aspeed_anacapa.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/functional/arm/test_aspeed_anacapa.py +2 -2
index 27f8bd8b56a..b16c6035c95 100644
@@ -11,8 +11,8 @@
 class AnacapaMachine(FacebookAspeedTest):
 
     ASSET_ANACAPA_FLASH = Asset(
-        'https://github.com/legoater/qemu-aspeed-boot/raw/3fa3212827b04be4034d43b5adeef57c27d6ab18/images/anacapa-bmc/openbmc-20260512025228/obmc-phosphor-image-anacapa-20260512025228.static.mtd.xz',
-        '2232e241abcfb6d4f6b82cb6c378ce5ce05e364aac6d118785c2b6cc33fe43f3')
+        'https://github.com/legoater/qemu-aspeed-boot/raw/refs/heads/master/images/anacapa-bmc/openbmc-20260616025349/obmc-phosphor-image-anacapa-20260616025349.static.mtd.xz',
+        'de3841fb6ed3085aec6424358ee6efc4b8ee85688361e5aa1987fd1acb7d3fb4')
 
     def test_arm_ast2600_anacapa_openbmc(self):
         image_path = self.uncompress(self.ASSET_ANACAPA_FLASH)
Signature · 1 line
2.47.3
#3 lore raw

PATCH v9 3/7 target/arm: do not clear halting reason in has_work helper

In reply to Alex Bennée

The helper will be called multiple times as we exit a loop and until we actually restart (via arm_cpu_exec_halt) we should leave the condition the same.

Fixes 6fd2fcdc61b (target/arm: teach arm_cpu_has_work about halting reasons)
---
 include/hw/core/sysemu-cpu-ops.h | 3 +++
 target/arm/cpu.c                 | 4 ++--
 2 files changed, 5 insertions(+), 2 deletions(-)
include/hw/core/sysemu-cpu-ops.h +3 -0
index 8be6a84bd54..9a45596169a 100644
@@ -18,6 +18,9 @@
 typedef struct SysemuCPUOps {
     /**
      * @has_work: Callback for checking if there is work to do.
+     *
+     * This function should be idempotent (i.e. not change state) as
+     * it will likely be queried multiple times before a CPU resumes.
      */
     bool (*has_work)(CPUState *cpu); /* MANDATORY NON-NULL */
     /**
target/arm/cpu.c +2 -2
index 86aae36ae55..597e0626e7f 100644
@@ -158,7 +158,6 @@ static bool arm_cpu_has_work(CPUState *cs)
      * A wake-up event should only wake us if we are halted on a WFE
      */
     if (cpu->env.halt_reason == HALT_WFE && cpu->env.event_register) {
-        cpu->env.halt_reason = NOT_HALTED;
         return true;
     }
 
@@ -170,7 +169,6 @@ static bool arm_cpu_has_work(CPUState *cs)
                            | CPU_INTERRUPT_NMI | CPU_INTERRUPT_VINMI | CPU_INTERRUPT_VFNMI
                            | CPU_INTERRUPT_VFIQ | CPU_INTERRUPT_VIRQ | CPU_INTERRUPT_VSERR
                            | CPU_INTERRUPT_EXITTB)) {
-        cpu->env.halt_reason = NOT_HALTED;
         return true;
     }
 
@@ -878,6 +876,8 @@ bool arm_cpu_exec_halt(CPUState *cs)
         if (cpu->wfxt_timer) {
             timer_del(cpu->wfxt_timer);
         }
+        /* clear the halt reason */
+        cpu->env.halt_reason = NOT_HALTED;
     }
     return leave_halt;
 }
Signature · 1 line
2.47.3
Reviewed-by on v9 3/7
#4 lore raw

Re: [PATCH v9 3/7] target/arm: do not clear halting reason in has_work helper

Quoted 11 lines · Alex Bennée

The helper will be called multiple times as we exit a loop and until we actually restart (via arm_cpu_exec_halt) we should leave the condition the same.

Fixes 6fd2fcdc61b (target/arm: teach arm_cpu_has_work about halting reasons)
---
  include/hw/core/sysemu-cpu-ops.h | 3 +++
  target/arm/cpu.c                 | 4 ++--
  2 files changed, 5 insertions(+), 2 deletions(-)

r~

#5 lore raw

PATCH v9 4/7 target/arm: ensure we create the wxft_timer for all modes

In reply to Alex Bennée

We don't want to just use it for timeouts as we will calculate which will comes first. Remove the wxft feature test in favour of the broader architecture checks.

---
v9
- we only need to check ARM_FEATURE_V6K as V7 and V8 imply it thanks to arm_cpu_propagate_feature_implications
v8
- new commit

---
 target/arm/cpu.h | 2 +-
 target/arm/cpu.c | 6 +++++-
 2 files changed, 6 insertions(+), 2 deletions(-)
target/arm/cpu.h +1 -1
index 31a5567c951..8aa7437a397 100644
@@ -966,7 +966,7 @@ struct ArchCPU {
      * pmu_op_finish() - it does not need other handling during migration
      */
     QEMUTimer *pmu_timer;
-    /* Timer used for WFxT timeouts */
+    /* Timer used for WFxT timeouts OR event stream events */
     QEMUTimer *wfxt_timer;
 
     /* GPIO outputs for generic timer */
target/arm/cpu.c +5 -1
index 597e0626e7f..4b49f8c43ec 100644
@@ -2269,7 +2269,11 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
     }
 
 #ifndef CONFIG_USER_ONLY
-    if (tcg_enabled() && cpu_isar_feature(aa64_wfxt, cpu)) {
+    /*
+     * We use the wfxt_timer for timeouts and event stream so we
+     * enable from V6K up. There is no event stream on M-profile.
+     */
+    if (tcg_enabled() && arm_feature(env, ARM_FEATURE_V6K)) {
         cpu->wfxt_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
                                        arm_wfxt_timer_cb, cpu);
     }
Signature · 1 line
2.47.3
#7 lore raw

PATCH v9 5/7 target/arm: implements SEV/SEVL for all modes

In reply to Alex Bennée

Remove the restrictions that make this a M-profile only operation and enable the instructions for all Arm profiles.

---
v3
- restrict SEV to M profile plus
- restrict SEVL to V8
v2
- fix alignment in a32.decode
- set bool directly, defend with QEMU_BUILD_BUG_ON
- s/instructions/profiles/
- share get_event_reg between translate/translate-a64

---
 target/arm/tcg/translate.h     | 18 ++++++++++++++++++
 target/arm/tcg/a32.decode      |  5 ++---
 target/arm/tcg/a64.decode      |  5 ++---
 target/arm/tcg/t16.decode      |  4 +---
 target/arm/tcg/t32.decode      |  4 +---
 target/arm/tcg/op_helper.c     |  4 +---
 target/arm/tcg/translate-a64.c | 17 +++++++++++++++++
 target/arm/tcg/translate.c     | 20 +++++++++++++++++---
 8 files changed, 59 insertions(+), 18 deletions(-)
target/arm/tcg/translate.h +18 -0
index 462d4c1c74f..83b413ee368 100644
@@ -860,6 +860,24 @@ static inline void gen_restore_rmode(TCGv_i32 old, TCGv_ptr fpst)
     gen_helper_set_rmode(old, old, fpst);
 }
 
+/*
+ * Event Register signalling.
+ *
+ * A bunch of activities trigger events, we just need to latch on to
+ * true. The event eventually gets consumed by WFE/WFET.
+ *
+ * user-mode treats these as NOPs.
+ */
+
+static inline void gen_event_reg(void)
+{
+#ifndef CONFIG_USER_ONLY
+    TCGv_i32 set_event = tcg_constant_i32(1);
+    QEMU_BUILD_BUG_ON(sizeof_field(CPUARMState, event_register) != 1);
+    tcg_gen_st8_i32(set_event, tcg_env, offsetof(CPUARMState, event_register));
+#endif
+}
+
 /*
  * Helpers for implementing sets of trans_* functions.
  * Defer the implementation of NAME to FUNC, with optional extra arguments.
target/arm/tcg/a32.decode +2 -3
index f2ca4809495..547aa2b1490 100644
@@ -192,9 +192,8 @@ SMULTT           .... 0001 0110 .... 0000 .... 1110 ....      @rd0mn
       WFE        ---- 0011 0010 0000 1111 ---- 0000 0010
       WFI        ---- 0011 0010 0000 1111 ---- 0000 0011
 
-      # TODO: Implement SEV, SEVL; may help SMP performance.
-      # SEV      ---- 0011 0010 0000 1111 ---- 0000 0100
-      # SEVL     ---- 0011 0010 0000 1111 ---- 0000 0101
+      SEV        ---- 0011 0010 0000 1111 ---- 0000 0100
+      SEVL       ---- 0011 0010 0000 1111 ---- 0000 0101
 
       ESB        ---- 0011 0010 0000 1111 ---- 0001 0000
     ]
target/arm/tcg/a64.decode +2 -3
index 28cd1faf61a..81070dac0a6 100644
@@ -250,9 +250,8 @@ ERETA           1101011 0100 11111 00001 m:1 11111 11111 &reta  # ERETAA, ERETAB
     YIELD       1101 0101 0000 0011 0010 0000 001 11111
     WFE         1101 0101 0000 0011 0010 0000 010 11111
     WFI         1101 0101 0000 0011 0010 0000 011 11111
-    # We implement WFE to never block, so our SEV/SEVL are NOPs
-    # SEV       1101 0101 0000 0011 0010 0000 100 11111
-    # SEVL      1101 0101 0000 0011 0010 0000 101 11111
+    SEV         1101 0101 0000 0011 0010 0000 100 11111
+    SEVL        1101 0101 0000 0011 0010 0000 101 11111
     # Our DGL is a NOP because we don't merge memory accesses anyway.
     # DGL       1101 0101 0000 0011 0010 0000 110 11111
     XPACLRI     1101 0101 0000 0011 0010 0000 111 11111
target/arm/tcg/t16.decode +1 -3
index 778fbf16275..9a8f89538ac 100644
@@ -228,10 +228,8 @@ REVSH           1011 1010 11 ... ...            @rdm
     WFE         1011 1111 0010 0000
     WFI         1011 1111 0011 0000
 
-    # M-profile SEV is implemented.
-    # TODO: Implement SEV for other profiles, and SEVL for all profiles; may help SMP performance.
     SEV         1011 1111 0100 0000
-    # SEVL      1011 1111 0101 0000
+    SEVL        1011 1111 0101 0000
 
     # The canonical nop has the second nibble as 0000, but the whole of the
     # rest of the space is a reserved hint, behaves as nop.
target/arm/tcg/t32.decode +1 -3
index 49b8d0037ec..8ae277fe112 100644
@@ -369,10 +369,8 @@ CLZ              1111 1010 1011 ---- 1111 .... 1000 ....      @rdm
         WFE      1111 0011 1010 1111 1000 0000 0000 0010
         WFI      1111 0011 1010 1111 1000 0000 0000 0011
 
-        # M-profile SEV is implemented.
-        # TODO: Implement SEV for other profiles, and SEVL for all profiles; may help SMP performance.
         SEV      1111 0011 1010 1111 1000 0000 0000 0100
-        # SEVL   1111 0011 1010 1111 1000 0000 0000 0101
+        SEVL     1111 0011 1010 1111 1000 0000 0000 0101
 
         ESB      1111 0011 1010 1111 1000 0000 0001 0000
       ]
target/arm/tcg/op_helper.c +1 -3
index 9f9ea39be5a..d15062e155f 100644
@@ -477,9 +477,7 @@ void HELPER(sev)(CPUARMState *env)
     CPUState *cs = env_cpu(env);
     CPU_FOREACH(cs) {
         ARMCPU *target_cpu = ARM_CPU(cs);
-        if (arm_feature(&target_cpu->env, ARM_FEATURE_M)) {
-            target_cpu->env.event_register = true;
-        }
+        target_cpu->env.event_register = true;
         if (!qemu_cpu_is_self(cs)) {
             qemu_cpu_kick(cs);
         }
target/arm/tcg/translate-a64.c +17 -0
index 227719ef25b..df5bac22acd 100644
@@ -2142,6 +2142,23 @@ static bool trans_WFI(DisasContext *s, arg_WFI *a)
     return true;
 }
 
+static bool trans_SEV(DisasContext *s, arg_SEV *a)
+{
+    /*
+     * SEV is a NOP for user-mode emulation.
+     */
+#ifndef CONFIG_USER_ONLY
+    gen_helper_sev(tcg_env);
+#endif
+    return true;
+}
+
+static bool trans_SEVL(DisasContext *s, arg_SEV *a)
+{
+    gen_event_reg();
+    return true;
+}
+
 static bool trans_WFE(DisasContext *s, arg_WFI *a)
 {
     /*
target/arm/tcg/translate.c +17 -3
index c744b163453..9079458a297 100644
@@ -3246,17 +3246,31 @@ static bool trans_YIELD(DisasContext *s, arg_YIELD *a)
 static bool trans_SEV(DisasContext *s, arg_SEV *a)
 {
     /*
-     * Currently SEV is a NOP for non-M-profile and in user-mode emulation.
-     * For system-mode M-profile, it sets the event register.
+     * SEV is a NOP for user-mode emulation. For v6T2 and earlier
+     * non-M-profile cores this encoding is a NOP hint.
      */
 #ifndef CONFIG_USER_ONLY
-    if (arm_dc_feature(s, ARM_FEATURE_M)) {
+    if (arm_dc_feature(s, ARM_FEATURE_M) ||
+        arm_dc_feature(s, ARM_FEATURE_V7)) {
         gen_helper_sev(tcg_env);
     }
 #endif
     return true;
 }
 
+static bool trans_SEVL(DisasContext *s, arg_SEV *a)
+{
+    /*
+     * SEVL only exists for v8A; for M-profile and v7A and earlier
+     * this encoding is an unallocated must-NOP hint.
+     */
+    if (!arm_dc_feature(s, ARM_FEATURE_M) &&
+        arm_dc_feature(s, ARM_FEATURE_V8)) {
+        gen_event_reg();
+    }
+    return true;
+}
+
 static bool trans_WFE(DisasContext *s, arg_WFE *a)
 {
     /*
Signature · 1 line
2.47.3
#8 lore raw

PATCH v9 6/7 target/arm: enable WFE sleeping for A-profile

In reply to Alex Bennée

To enable full architectural behaviour for A-profile we need to do a number of things:

- add support for the event stream to wake things up
- add support for potential trap on sleep
- handle the global monitor's interactions with WFE
- remove the M-profile specific gates

Event stream
------------

Two generic timers (K and H) are capable of generating timer event stream events. Provide a helper to calculate when the nearest one will happen.

Now we can calculate when the next event stream event is we can re-use the wfxt_timer and configure it to fire as we enter a WFE that is going to sleep. Reverse the M-profile logic so we can enter a sleep state in both profiles.

We also take care to use atomics for accessing env->event_register as we now have potential access outside the vCPU context.

Traps
-----

A-profile can trap WFE's *if* the instruction would otherwise sleep. To do this we need to pass the instruction size so we can deal with the is_16bit syndrome encoding.

Global Monitor
--------------

To avoid issues with QEMU's incomplete ldst exclusive handling causing potential deadlocks in common WFE enabled locking patterns we take advantage of the architectures flexibility and treat being in the exclusive region as a reason to exit.

---
v2
- merged target/arm: add gt_calc_next_event_stream
- update to use halt_reason
- made arm_wfxt_timer_cb atomically consume halt_reason
v4
- skip sleep if in the exclusive region
- update commit message
- remove the CF_PARALLEL guards so we work in smp
v5
- use env_archcpu for ARMCPU rather then expensive QOM cast
- rely on cpu->wfxt_timer to guard event stream leg
v6
- use atomic_xchg to consume event_register
- remove extraneous target_el calculation
- defer calculating target_el until after the early return
v7
- merged with trap patch
- rewrite commit message

---
 target/arm/tcg/helper-defs.h   |   2 +-
 target/arm/cpu.c               |  13 +++
 target/arm/tcg/op_helper.c     | 156 ++++++++++++++++++++++++++++-----
 target/arm/tcg/translate-a64.c |  12 +--
 target/arm/tcg/translate.c     |  18 +---
 5 files changed, 156 insertions(+), 45 deletions(-)
target/arm/tcg/helper-defs.h +1 -1
index 8ec6c163195..99ebd754942 100644
@@ -54,7 +54,7 @@ DEF_HELPER_2(exception_swstep, noreturn, env, i32)
 DEF_HELPER_2(exception_pc_alignment, noreturn, env, vaddr)
 DEF_HELPER_1(setend, void, env)
 DEF_HELPER_2(wfi, void, env, i32)
-DEF_HELPER_1(wfe, void, env)
+DEF_HELPER_2(wfe, void, env, i32)
 DEF_HELPER_2(wfit, void, env, i32)
 DEF_HELPER_1(yield, void, env)
 DEF_HELPER_1(pre_hvc, void, env)
target/arm/cpu.c +13 -0
index 4b49f8c43ec..b3de399f40c 100644
@@ -883,10 +883,23 @@ bool arm_cpu_exec_halt(CPUState *cs)
 }
 #endif
 
+/*
+ * Unlike almost everything else that messes with the halt_reason and
+ * event_register details the timer callbacks are not in the vCPU
+ * context.
+ *
+ * To prevent races we atomically consume a HALT_WFE and set the event
+ * register. Either way we trigger the an exit event.
+ */
 static void arm_wfxt_timer_cb(void *opaque)
 {
     ARMCPU *cpu = opaque;
     CPUState *cs = CPU(cpu);
+    CPUARMState *env = &cpu->env;
+
+    if (qatomic_cmpxchg(&env->halt_reason, HALT_WFE, NOT_HALTED)) {
+        qatomic_set(&env->event_register, true);
+    }
 
     /*
      * We expect the CPU to be halted; this will cause arm_cpu_is_work()
target/arm/tcg/op_helper.c +136 -20

175 lines not on this page · raw message

target/arm/tcg/translate-a64.c +2 -10
index df5bac22acd..b45aac6d269 100644
@@ -2161,15 +2161,7 @@ static bool trans_SEVL(DisasContext *s, arg_SEV *a)
 
 static bool trans_WFE(DisasContext *s, arg_WFI *a)
 {
-    /*
-     * When running in MTTCG we don't generate jumps to the yield and
-     * WFE helpers as it won't affect the scheduling of other vCPUs.
-     * If we wanted to more completely model WFE/SEV so we don't busy
-     * spin unnecessarily we would need to do something more involved.
-     */
-    if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
-        s->base.is_jmp = DISAS_WFE;
-    }
+    s->base.is_jmp = DISAS_WFE;
     return true;
 }
 
@@ -11232,7 +11224,7 @@ static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
          */
         case DISAS_WFE:
             gen_a64_update_pc(dc, 4);
-            gen_helper_wfe(tcg_env);
+            gen_helper_wfe(tcg_env, tcg_constant_i32(4));
             tcg_gen_exit_tb(NULL, 0);
             break;
         case DISAS_WFI:
target/arm/tcg/translate.c +4 -14
index 9079458a297..a1fc0506188 100644
@@ -3273,19 +3273,9 @@ static bool trans_SEVL(DisasContext *s, arg_SEV *a)
 
 static bool trans_WFE(DisasContext *s, arg_WFE *a)
 {
-    /*
-     * When running single-threaded TCG code, use the helper to ensure that
-     * the next round-robin scheduled vCPU gets a crack.
-     *
-     * For Cortex-M, we implement the architectural WFE behavior (sleeping
-     * until an event occurs or the Event Register is set).
-     * For other profiles, we currently treat this as a NOP or yield,
-     * to preserve existing performance characteristics.
-     */
-    if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
-        gen_update_pc(s, curr_insn_len(s));
-        s->base.is_jmp = DISAS_WFE;
-    }
+    /* For WFE, halt the vCPU until an event. */
+    gen_update_pc(s, curr_insn_len(s));
+    s->base.is_jmp = DISAS_WFE;
     return true;
 }
 
@@ -6857,7 +6847,7 @@ static void arm_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
             tcg_gen_exit_tb(NULL, 0);
             break;
         case DISAS_WFE:
-            gen_helper_wfe(tcg_env);
+            gen_helper_wfe(tcg_env, tcg_constant_i32(curr_insn_len(dc)));
             /*
              * The helper can return if the event register is set, so we
              * must go back to the main loop to check for events.
Signature · 1 line
2.47.3
#9 lore raw

Re: [PATCH v9 6/7] target/arm: enable WFE sleeping for A-profile

Quoted 7 lines · Alex Bennée

To enable full architectural behaviour for A-profile we need to do a number of things:

- add support for the event stream to wake things up
- add support for potential trap on sleep
- handle the global monitor's interactions with WFE
- remove the M-profile specific gates

Hi Alex,

this patch broke the test_arm_quanta_gsj test in tests/functional/arm/test_quanta_gsj.py. The test is not run by default, so nobody noticed. But if you run it like this:

QEMU_TEST_ALLOW_SLOW=1 QEMU_TEST_QEMU_BINARY=./qemu-system-arm ./run \
~/devel/qemu/tests/functional/arm/test_quanta_gsj.py

you'll notice that the test never finishes on the current master branch. When switching back to the commit before the "enable WFE" patch, the test works fine again and finishes within a minute or two here.

Could you please have a look when you've got some spare time?

Thanks,
Thomas

#10 lore raw

Re: [PATCH v9 6/7] target/arm: enable WFE sleeping for A-profile

Quoted 16 lines · Thomas Huth
Quoted 6 lines · Alex Bennée

To enable full architectural behaviour for A-profile we need to do a number of things:
- add support for the event stream to wake things up
- add support for potential trap on sleep
- handle the global monitor's interactions with WFE
- remove the M-profile specific gates

Hi Alex,

this patch broke the test_arm_quanta_gsj test in tests/functional/arm/test_quanta_gsj.py. The test is not run by default, so nobody noticed. But if you run it like this:

QEMU_TEST_ALLOW_SLOW=1 QEMU_TEST_QEMU_BINARY=./qemu-system-arm ./run \
~/devel/qemu/tests/functional/arm/test_quanta_gsj.py

Quoted 6 lines

you'll notice that the test never finishes on the current master branch. When switching back to the commit before the "enable WFE" patch, the test works fine again and finishes within a minute or two here.

Could you please have a look when you've got some spare time?

It works on my current dev tree, probably because I have:

Message-ID: <[email protected]>
Date: Mon, 12 Oct 2026 14:48:40 +0100
Subject: [RFC PATCH] target/arm: hold bql when SEV sets the event_register
From: =?UTF-8?q?Alex=20Benn=C3=A9e?= <[email protected]>

which needs a light re-spin when I have some spare time (sorry it's review season so I'm trying to get them all done by EoTM).

Quoted 3 lines

Thanks,
Thomas

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#11 lore raw

Re: [PATCH v9 6/7] target/arm: enable WFE sleeping for A-profile

Quoted 23 lines · Alex Bennée
Quoted 21 lines · Thomas Huth
Quoted 6 lines · Alex Bennée

To enable full architectural behaviour for A-profile we need to do a number of things:
- add support for the event stream to wake things up
- add support for potential trap on sleep
- handle the global monitor's interactions with WFE
- remove the M-profile specific gates

Hi Alex,

this patch broke the test_arm_quanta_gsj test in tests/functional/arm/test_quanta_gsj.py. The test is not run by default, so nobody noticed. But if you run it like this:

  QEMU_TEST_ALLOW_SLOW=1 QEMU_TEST_QEMU_BINARY=./qemu-system-arm ./run \
   ~/devel/qemu/tests/functional/arm/test_quanta_gsj.py

you'll notice that the test never finishes on the current master branch. When switching back to the commit before the "enable WFE" patch, the test works fine again and finishes within a minute or two here.

Quoted 8 lines
Quoted 1 line

Could you please have a look when you've got some spare time?

It works on my current dev tree, probably because I have:

   Message-ID: <[email protected]>
   Date: Mon, 12 Oct 2026 14:48:40 +0100
   Subject: [RFC PATCH] target/arm: hold bql when SEV sets the event_register
   From: =?UTF-8?q?Alex=20Benn=C3=A9e?= <[email protected]>

I gave it a quick try, and this patch seems to fix the problem, indeed.

Thanks,
Thomas

PS: I think we should remove @@skipSlowTest decorator from the quanta test once your patch has been merged - the test does not seem to run that slow that the decorator is really justified here.

#12 lore raw

PATCH v9 7/7 target/arm: implement WFET

In reply to Alex Bennée

Now we have the event stream and SEV/SEVL implemented we can finally enable WFET for Aarch64.

To avoid issues with QEMU's incomplete ldst exclusive handling causing potential deadlocks in common WFE enabled locking patterns we take advantage of the architectures flexibility and treat being in the exclusive region as a reason to exit.

---
v2
- fix exception syndrome by using enum value
- use env->halt_reason
v3
- fix check_wfx_trap(s/false/true/) as it is a WFE
v4
- defer expensive calculations until needed
- treat cs->exclusive_addr as a IMPDEF WFE exit
- update commit message
v5
- use atomic_xchg to consume event_register

---
 target/arm/tcg/helper-defs.h   |  1 +
 target/arm/tcg/op_helper.c     | 94 ++++++++++++++++++++++++++++++++++
 target/arm/tcg/translate-a64.c | 15 +++---
 3 files changed, 103 insertions(+), 7 deletions(-)
target/arm/tcg/helper-defs.h +1 -0
index 99ebd754942..0077aeb4e22 100644
@@ -56,6 +56,7 @@ DEF_HELPER_1(setend, void, env)
 DEF_HELPER_2(wfi, void, env, i32)
 DEF_HELPER_2(wfe, void, env, i32)
 DEF_HELPER_2(wfit, void, env, i32)
+DEF_HELPER_2(wfet, void, env, i32)
 DEF_HELPER_1(yield, void, env)
 DEF_HELPER_1(pre_hvc, void, env)
 DEF_HELPER_2(pre_smc, void, env, i32)
target/arm/tcg/op_helper.c +94 -0

100 lines not on this page · raw message

target/arm/tcg/translate-a64.c +8 -7
index b45aac6d269..4a24e0a7fa0 100644
@@ -2195,14 +2195,15 @@ static bool trans_WFET(DisasContext *s, arg_WFET *a)
         return false;
     }
 
-    /*
-     * We rely here on our WFE implementation being a NOP, so we
-     * don't need to do anything different to handle the WFET timeout
-     * from what trans_WFE does.
-     */
-    if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
-        s->base.is_jmp = DISAS_WFE;
+    if (s->ss_active) {
+        /* Act like a NOP under architectural singlestep */
+        return true;
     }
+
+    gen_a64_update_pc(s, 4);
+    gen_helper_wfet(tcg_env, tcg_constant_i32(a->rd));
+    /* Go back to the main loop to check for interrupts */
+    s->base.is_jmp = DISAS_EXIT;
     return true;
 }
 
Signature · 1 line
2.47.3
#13 lore raw
In reply to Alex Bennée
Quoted 10 lines · Alex Bennée

This series fully models the behaviour of WFxT instructions. We already had support for WFE for M-profile but we left off A-profile as it has more potential sources of wake-ups. The main one is the event stream which includes events from significant bits of the timer ticking over.

The refactoring from the previous iterations of the patch have now been merged.

Quoted 9 lines

I've dropped trying to fully model the global monitor in favour of taking advantage of the architectural flexibility to have an IMPDEF event wake up for any reason. We treat the setting of exclusive_addr as such a reason because it indicates the current vCPU is in a ldstx exclusive region and we don't want the guest to deadlock. This does mean the system won't sleep on WFE enabled locks but people shouldn't be relying on QEMU to model real world sleep patterns anyway given the efficiency of emulation compared to real HW.

Applied to target-arm.next, thanks.

-- PMM