Skip to contentSkip to conversation

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

PATCH Applied Alex Bennée 2 participants 20 messages May 29 – Jun 15 KVM

v7 of 9 Applied Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v7 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]>

All patches are now reviewed.

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 (8):
target/arm: fix WFET typo in syndrome target/arm: teach arm_cpu_has_work about halting reasons target/arm: redefine event stream fields target/arm: ensure aarch64 DISAS_WFE will exit target/arm: implements SEV/SEVL for all modes target/arm: enable event stream on WFE instructions target/arm: handle the WFE trap case target/arm: implement WFET

 target/arm/cpu.h               |  16 +++
 target/arm/internals.h         |  22 ++-
 target/arm/syndrome.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 +-
 target/arm/arm-powerctl.c      |   6 +-
 target/arm/cpu.c               |  53 +++++--
 target/arm/helper.c            |   8 +-
 target/arm/kvm.c               |   5 +-
 target/arm/machine.c           |   2 +-
 target/arm/tcg/op_helper.c     | 253 ++++++++++++++++++++++++++++++---
 target/arm/tcg/translate-a64.c |  64 +++++----
 target/arm/tcg/translate.c     |  31 ++--
 17 files changed, 395 insertions(+), 106 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v7 1/8 target/arm: fix WFET typo in syndrome

A stray x slipped in and we didn't notice! Fortunately we haven't implemented WFET yet so nothing is affected. But we are about to so lets fix it.

Fixes 4575da5ecb7 (target/arm: report register in WFIT syndromes)

---
v7
- mention how we got away with it

---
 target/arm/syndrome.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/syndrome.h +1 -1
index 4d1f1c529e2..0eb54c15ce7 100644
@@ -674,7 +674,7 @@ typedef enum {
     WFI = 0b00,
     WFE = 0b01,
     WFIT = 0b10,
-    WFET = 0xb11
+    WFET = 0b11
 } wfx_ti;
 
 static inline uint32_t syn_wfx(int cv, int cond, int rn, bool rv, wfx_ti ti, bool is_16bit)
Signature · 1 line
2.47.3
#2 lore raw

PATCH v7 2/8 target/arm: teach arm_cpu_has_work about halting reasons

In reply to Alex Bennée

With the advent of WFE and WFI we need to pay closer attention to the reason why the vCPU may be sleeping to figure out if we should wake it up.

Create env->halt_reason to track this and then re-order the tests so we:

- ignore everything is the vCPU is powered off
- wake up if the event_register is set and we were in a WFE
- otherwise any IRQ event does wake the vCPU up.

---
v3
- move arm_set_cpu_power_state to internals.h
- drop excess brackets
v5
- more arm_set_cpu_power_state cases

---
 target/arm/cpu.h           | 16 +++++++++++++++
 target/arm/internals.h     | 11 +++++++++++
 target/arm/arm-powerctl.c  |  6 +++---
 target/arm/cpu.c           | 40 +++++++++++++++++++++++++++-----------
 target/arm/kvm.c           |  5 +++--
 target/arm/machine.c       |  2 +-
 target/arm/tcg/op_helper.c |  3 +++
 7 files changed, 66 insertions(+), 17 deletions(-)
target/arm/cpu.h +16 -0
index 85552b573c4..dfc369b5844 100644
@@ -224,6 +224,19 @@ typedef enum ARMFPStatusFlavour {
 /* Architecturally there are 128 PPIs in a GICv5 */
 #define GICV5_NUM_PPIS 128
 
+/**
+ * ARMHaltReason - the reason we have entered halt state
+ *
+ * To be able to correctly wake up via arm_cpu_has_work() we need to
+ * track the reason we went to sleep.
+ */
+typedef enum {
+    NOT_HALTED = 0,
+    HALT_PSCI,
+    HALT_WFI,
+    HALT_WFE
+} ARMHaltReason;
+
 typedef struct CPUArchState {
     /* Regs for current mode.  */
     uint32_t regs[16];
@@ -746,6 +759,9 @@ typedef struct CPUArchState {
     /* Optional fault info across tlb lookup. */
     ARMMMUFaultInfo *tlb_fi;
 
+    /* Reason the CPU is halted */
+    ARMHaltReason halt_reason;
+
     /*
      * The event register is shared by all ARM profiles (A/R/M),
      * so it is stored in the top-level CPU state.
target/arm/internals.h +11 -0
index ae5afc5362e..fb9df50da29 100644
@@ -2027,4 +2027,15 @@ bool arm_cpu_match_cpreg_mig_tolerance(ARMCPU *cpu, uint64_t kvmidx,
                                        ARMCPRegMigToleranceType type);
 
 
+/**
+ * arm_set_cpu_power_state() - set power state synced with halt_reason
+ */
+static inline void arm_set_cpu_power_state(ARMCPU *cpu, ARMPSCIState state)
+{
+    CPUARMState *env = &cpu->env;
+
+    cpu->power_state = state;
+    env->halt_reason = state == PSCI_OFF ? HALT_PSCI : NOT_HALTED;
+}
+
 #endif
target/arm/arm-powerctl.c +3 -3
index a788376d1d3..a06be5cc997 100644
@@ -78,7 +78,7 @@ static void arm_set_cpu_on_async_work(CPUState *target_cpu_state,
 
     /* Finally set the power status */
     assert(bql_locked());
-    target_cpu->power_state = PSCI_ON;
+    arm_set_cpu_power_state(target_cpu, PSCI_ON);
 }
 
 int arm_set_cpu_on(uint64_t cpuid, uint64_t entry, uint64_t context_id,
@@ -186,7 +186,7 @@ static void arm_set_cpu_on_and_reset_async_work(CPUState *target_cpu_state,
 
     /* Finally set the power status */
     assert(bql_locked());
-    target_cpu->power_state = PSCI_ON;
+    arm_set_cpu_power_state(target_cpu, PSCI_ON);
 }
 
 int arm_set_cpu_on_and_reset(uint64_t cpuid)
@@ -239,7 +239,7 @@ static void arm_set_cpu_off_async_work(CPUState *target_cpu_state,
     ARMCPU *target_cpu = ARM_CPU(target_cpu_state);
 
     assert(bql_locked());
-    target_cpu->power_state = PSCI_OFF;
+    arm_set_cpu_power_state(target_cpu, PSCI_OFF);
     target_cpu_state->halted = 1;
     target_cpu_state->exception_index = EXCP_HLT;
 }
target/arm/cpu.c +29 -11
index a13e6dae2a2..8771b695d9d 100644
@@ -145,18 +145,36 @@ static bool arm_cpu_has_work(CPUState *cs)
 {
     ARMCPU *cpu = ARM_CPU(cs);
 
-    if (arm_feature(&cpu->env, ARM_FEATURE_M)) {
-        if (cpu->env.event_register) {
-            return true;
-        }
+    /*
+     * Only another PSCI call can wake the CPU up in which case the
+     * power_state would be set by arm_set_cpu_on_and_reset_async_work()
+     */
+    if (cpu->power_state == PSCI_OFF) {
+        g_assert(cpu->env.halt_reason == HALT_PSCI);
+        return false;
+    }
+
+    /*
+     * 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;
+    }
+
+    /*
+     * Otherwise pretty much any IRQ would wake us up
+     */
+    if (cpu_test_interrupt(cs,
+                           CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD
+                           | 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;
     }
 
-    return (cpu->power_state != PSCI_OFF)
-        && cpu_test_interrupt(cs,
-               CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD
-               | CPU_INTERRUPT_NMI | CPU_INTERRUPT_VINMI | CPU_INTERRUPT_VFNMI
-               | CPU_INTERRUPT_VFIQ | CPU_INTERRUPT_VIRQ | CPU_INTERRUPT_VSERR
-               | CPU_INTERRUPT_EXITTB);
+    return false;
 }
 #endif /* !CONFIG_USER_ONLY */
 
@@ -327,7 +345,7 @@ static void arm_cpu_reset_hold(Object *obj, ResetType type)
     env->vfp.xregs[ARM_VFP_MVFR1] = cpu->isar.mvfr1;
     env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2;
 
-    cpu->power_state = cs->start_powered_off ? PSCI_OFF : PSCI_ON;
+    arm_set_cpu_power_state(cpu, cs->start_powered_off ? PSCI_OFF : PSCI_ON);
 
     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
         /* 64 bit CPUs always start in 64 bit mode */
target/arm/kvm.c +3 -2
index 7d194ea112b..a54ef51ec2a 100644
@@ -1149,11 +1149,12 @@ static int kvm_arm_sync_mpstate_to_qemu(ARMCPU *cpu)
     if (cap_has_mp_state) {
         struct kvm_mp_state mp_state;
         int ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MP_STATE, &mp_state);
+        ARMPSCIState state;
         if (ret) {
             return ret;
         }
-        cpu->power_state = (mp_state.mp_state == KVM_MP_STATE_STOPPED) ?
-            PSCI_OFF : PSCI_ON;
+        state = (mp_state.mp_state == KVM_MP_STATE_STOPPED) ? PSCI_OFF : PSCI_ON;
+        arm_set_cpu_power_state(cpu, state);
     }
     return 0;
 }
target/arm/machine.c +1 -1
index 58f8dfd53c8..fde3b3e8d75 100644
@@ -916,7 +916,7 @@ static int get_power(QEMUFile *f, void *opaque, size_t size,
 {
     ARMCPU *cpu = opaque;
     bool powered_off = qemu_get_byte(f);
-    cpu->power_state = powered_off ? PSCI_OFF : PSCI_ON;
+    arm_set_cpu_power_state(cpu, powered_off ? PSCI_OFF : PSCI_ON);
     return 0;
 }
 
target/arm/tcg/op_helper.c +3 -0
index e8f0996ed39..504526153a6 100644
@@ -402,6 +402,7 @@ void HELPER(wfi)(CPUARMState *env, uint32_t insn_len)
                         target_el);
     }
 
+    env->halt_reason = HALT_WFI;
     cs->exception_index = EXCP_HLT;
     cs->halted = 1;
     cpu_loop_exit(cs);
@@ -463,6 +464,7 @@ void HELPER(wfit)(CPUARMState *env, uint32_t rd)
     } else {
         timer_mod(cpu->wfxt_timer, nexttick);
     }
+    env->halt_reason = HALT_WFI;
     cs->exception_index = EXCP_HLT;
     cs->halted = 1;
     cpu_loop_exit(cs);
@@ -507,6 +509,7 @@ void HELPER(wfe)(CPUARMState *env)
             return;
         }
 
+        env->halt_reason = HALT_WFE;
         cs->exception_index = EXCP_HLT;
         cs->halted = 1;
         cpu_loop_exit(cs);
Signature · 1 line
2.47.3
#3 lore raw

PATCH v7 3/8 target/arm: redefine event stream fields

In reply to Alex Bennée

The event stream control bits are the same for both CNTHCTL and CNTKCTL so rather than duplicating the definitions rename them to be useful in both cases.

We will need these in a later commit when we start implementing event streams.

---
 target/arm/internals.h | 11 +++++++----
 target/arm/helper.c    |  8 ++++----
 2 files changed, 11 insertions(+), 8 deletions(-)
target/arm/internals.h +7 -4
index fb9df50da29..93f5bd0e830 100644
@@ -272,14 +272,17 @@ FIELD(VSTCR, SA, 30, 1)
  * have different bit definitions, and EL1PCTEN might be
  * bit 0 or bit 10. We use _E2H1 and _E2H0 suffixes to
  * disambiguate if necessary.
+ *
+ * The event stream bits (EVN*) are in the same position for
+ * CNTKCTL_EL1/CTNKCTL.
  */
 FIELD(CNTHCTL, EL0PCTEN_E2H1, 0, 1)
 FIELD(CNTHCTL, EL0VCTEN_E2H1, 1, 1)
 FIELD(CNTHCTL, EL1PCTEN_E2H0, 0, 1)
 FIELD(CNTHCTL, EL1PCEN_E2H0, 1, 1)
-FIELD(CNTHCTL, EVNTEN, 2, 1)
-FIELD(CNTHCTL, EVNTDIR, 3, 1)
-FIELD(CNTHCTL, EVNTI, 4, 4)
+FIELD(CNTxCTL, EVNTEN, 2, 1)
+FIELD(CNTxCTL, EVNTDIR, 3, 1)
+FIELD(CNTxCTL, EVNTI, 4, 4)
 FIELD(CNTHCTL, EL0VTEN, 8, 1)
 FIELD(CNTHCTL, EL0PTEN, 9, 1)
 FIELD(CNTHCTL, EL1PCTEN_E2H1, 10, 1)
@@ -289,7 +292,7 @@ FIELD(CNTHCTL, EL1TVT, 13, 1)
 FIELD(CNTHCTL, EL1TVCT, 14, 1)
 FIELD(CNTHCTL, EL1NVPCT, 15, 1)
 FIELD(CNTHCTL, EL1NVVCT, 16, 1)
-FIELD(CNTHCTL, EVNTIS, 17, 1)
+FIELD(CNTxCTL, EVNTIS, 17, 1)
 FIELD(CNTHCTL, CNTVMASK, 18, 1)
 FIELD(CNTHCTL, CNTPMASK, 19, 1)
 
target/arm/helper.c +4 -4
index 34487eeaa36..bed58e65b4d 100644
@@ -1769,9 +1769,9 @@ static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
     uint32_t valid_mask =
         R_CNTHCTL_EL0PCTEN_E2H1_MASK |
         R_CNTHCTL_EL0VCTEN_E2H1_MASK |
-        R_CNTHCTL_EVNTEN_MASK |
-        R_CNTHCTL_EVNTDIR_MASK |
-        R_CNTHCTL_EVNTI_MASK |
+        R_CNTxCTL_EVNTEN_MASK |
+        R_CNTxCTL_EVNTDIR_MASK |
+        R_CNTxCTL_EVNTI_MASK |
         R_CNTHCTL_EL0VTEN_MASK |
         R_CNTHCTL_EL0PTEN_MASK |
         R_CNTHCTL_EL1PCTEN_E2H1_MASK |
@@ -1786,7 +1786,7 @@ static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
             R_CNTHCTL_EL1TVCT_MASK |
             R_CNTHCTL_EL1NVPCT_MASK |
             R_CNTHCTL_EL1NVVCT_MASK |
-            R_CNTHCTL_EVNTIS_MASK;
+            R_CNTxCTL_EVNTIS_MASK;
     }
     if (cpu_isar_feature(aa64_ecv, cpu)) {
         valid_mask |= R_CNTHCTL_ECV_MASK;
Signature · 1 line
2.47.3
#4 lore raw

PATCH v7 4/8 target/arm: ensure aarch64 DISAS_WFE will exit

In reply to Alex Bennée

This mirrors the logic for DISAS_WFE in 32 bit world. As the WFE/WFI have similar behaviours shuffle the case statements around a little and update the commentary to cover both.

Fixes 252ec405768 (target-arm: implement WFE/YIELD as a yield for AArch64)

---
v4
- shuffle case statements, unify the comments.

---
 target/arm/tcg/translate-a64.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
target/arm/tcg/translate-a64.c +12 -12
index 15b40090c0f..c20269b1501 100644
@@ -10939,25 +10939,25 @@ static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
         case DISAS_NORETURN:
         case DISAS_SWI:
             break;
-        case DISAS_WFE:
-            gen_a64_update_pc(dc, 4);
-            gen_helper_wfe(tcg_env);
-            break;
         case DISAS_YIELD:
             gen_a64_update_pc(dc, 4);
             gen_helper_yield(tcg_env);
             break;
+        /*
+         * Both WFE/WFI can cause exceptions or exit the loop to
+         * halt so we have to make sure we have rectified the PC.
+         * However they can also return directly if they don't
+         * enter a wait state so we must add an exit block so we exit
+         * the loop and check for interrupts.
+         */
+        case DISAS_WFE:
+            gen_a64_update_pc(dc, 4);
+            gen_helper_wfe(tcg_env);
+            tcg_gen_exit_tb(NULL, 0);
+            break;
         case DISAS_WFI:
-            /*
-             * This is a special case because we don't want to just halt
-             * the CPU if trying to debug across a WFI.
-             */
             gen_a64_update_pc(dc, 4);
             gen_helper_wfi(tcg_env, tcg_constant_i32(4));
-            /*
-             * The helper doesn't necessarily throw an exception, but we
-             * must go back to the main loop to check for interrupts anyway.
-             */
             tcg_gen_exit_tb(NULL, 0);
             break;
         }
Signature · 1 line
2.47.3
#5 lore raw

PATCH v7 5/8 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.

---
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     | 13 ++++++++-----
 8 files changed, 50 insertions(+), 20 deletions(-)
target/arm/tcg/translate.h +18 -0
index d4bcc5bad4c..b71b1d5be66 100644
@@ -856,6 +856,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 02c7264cb99..7ea52ac23b9 100644
@@ -237,9 +237,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 504526153a6..2b1fb1e059d 100644
@@ -476,9 +476,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 c20269b1501..4b978c8b958 100644
@@ -2033,6 +2033,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 +8 -5
index c744b163453..e1e5539f571 100644
@@ -3246,17 +3246,20 @@ 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.
      */
 #ifndef CONFIG_USER_ONLY
-    if (arm_dc_feature(s, ARM_FEATURE_M)) {
-        gen_helper_sev(tcg_env);
-    }
+    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_WFE *a)
 {
     /*
Signature · 1 line
2.47.3
#6 lore raw

Re: [PATCH v7 5/8] target/arm: implements SEV/SEVL for all modes

Quoted 70 lines · Alex Bennée

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

---
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/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/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/translate.c +0 -0
index c744b163453..e1e5539f571 100644
@@ -3246,17 +3246,20 @@ static bool trans_YIELD(DisasContext *s, arg_YIELD *a)
 static bool trans_SEV(DisasContext *s, arg_SEV *a)
 {
     /*

target/arm/tcg/translate.c static bool trans_YIELD(DisasContext *s, arg_YIELD *a) · line 3253

Quoted 10 lines
target/arm/tcg/translate.c +2 -5
-     * 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.
      */
 #ifndef CONFIG_USER_ONLY
-    if (arm_dc_feature(s, ARM_FEATURE_M)) {
-        gen_helper_sev(tcg_env);
-    }
+    gen_helper_sev(tcg_env);
 #endif

The AArch32 SEV encodings are only SEV for M-profile and for Armv7A and above -- for v6T2 and earlier they are NOP hints. We need a feature-check here.

target/arm/tcg/translate.c static bool trans_YIELD(DisasContext *s, arg_YIELD *a) · line 3260

Quoted 7 lines
target/arm/tcg/translate.c +4 -0
     return true;
 }
 
+static bool trans_SEVL(DisasContext *s, arg_SEV *a)
+{
+    gen_event_reg();
+    return true;

The SEVL encodings only exist for v8A -- they are unallocated must-NOP hints for M-profile and v7A and earlier. We need a feature-check here.

target/arm/tcg/translate.c static bool trans_YIELD(DisasContext *s, arg_YIELD *a) · line 3263

Quoted 3 lines
target/arm/tcg/translate.c +2 -0
+}
+
 static bool trans_WFE(DisasContext *s, arg_WFE *a)

thanks
-- PMM

#7 lore raw

PATCH v7 6/8 target/arm: enable event stream on WFE instructions

In reply to Alex Bennée

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.

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

---
 target/arm/cpu.c               |  13 +++
 target/arm/tcg/op_helper.c     | 142 ++++++++++++++++++++++++++++-----
 target/arm/tcg/translate-a64.c |  10 +--
 target/arm/tcg/translate.c     |  16 +---
 4 files changed, 139 insertions(+), 42 deletions(-)
target/arm/cpu.c +13 -0
index 8771b695d9d..d01b4cee5f0 100644
@@ -879,10 +879,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 +122 -20

160 lines not on this page · raw message

target/arm/tcg/translate-a64.c +1 -9
index 4b978c8b958..ec03eaa3bbd 100644
@@ -2052,15 +2052,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;
 }
 
target/arm/tcg/translate.c +3 -13
index e1e5539f571..c10cc382d15 100644
@@ -3262,19 +3262,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;
 }
 
Signature · 1 line
2.47.3
#8 lore raw

PATCH v7 7/8 target/arm: handle the WFE trap case

In reply to Alex Bennée

Now WFE can actually suspend on A-profile we also need to handle when its trapped. To do this we need to pass the instruction size so we can deal with the is_16bit syndrome encoding.

---
v6
- defer calculating target_el until after the early return

---
 target/arm/tcg/helper-defs.h   |  2 +-
 target/arm/tcg/op_helper.c     | 16 +++++++++++++++-
 target/arm/tcg/translate-a64.c |  2 +-
 target/arm/tcg/translate.c     |  2 +-
 4 files changed, 18 insertions(+), 4 deletions(-)
target/arm/tcg/helper-defs.h +1 -1
index a05f2258f29..ebdf09be38a 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/tcg/op_helper.c +15 -1
index b803c9bf162..060b155d559 100644
@@ -574,7 +574,7 @@ static int64_t gt_calc_next_event_stream(CPUARMState *env)
 }
 #endif
 
-void HELPER(wfe)(CPUARMState *env)
+void HELPER(wfe)(CPUARMState *env, uint32_t insn_len)
 {
 #ifdef CONFIG_USER_ONLY
     /*
@@ -592,11 +592,25 @@ void HELPER(wfe)(CPUARMState *env)
      */
     CPUState *cs = env_cpu(env);
     ARMCPU *cpu = env_archcpu(env);
+    uint32_t excp;
+    int target_el;
 
     if (qatomic_xchg(&env->event_register, false)) {
         return;
     }
 
+    /* We might sleep, so now we check to see if we should trap */
+    target_el = check_wfx_trap(env, true, &excp);
+    if (target_el) {
+        if (env->aarch64) {
+            env->pc -= insn_len;
+        } else {
+            env->regs[15] -= insn_len;
+        }
+        raise_exception(env, excp, syn_wfx(1, 0xe, 0, false, WFE, insn_len == 2),
+                        target_el);
+    }
+
     /*
      * If the CPU has entered the exclusive region we could sleep
      * until the global monitor moves from Exclusive to Open Access.
target/arm/tcg/translate-a64.c +1 -1
index ec03eaa3bbd..a4603e1a5c7 100644
@@ -10961,7 +10961,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 +1 -1
index c10cc382d15..1f554f6c271 100644
@@ -6836,7 +6836,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 v7 7/8] target/arm: handle the WFE trap case

Quoted 7 lines · Alex Bennée

Now WFE can actually suspend on A-profile we also need to handle when its trapped. To do this we need to pass the instruction size so we can deal with the is_16bit syndrome encoding.

Doesn't this need to go earlier in the patchset? Otherwise there's a point where WFE can go to sleep but doesn't honour the trap bit.

thanks
-- PMM

#10 lore raw

Re: [PATCH v7 7/8] target/arm: handle the WFE trap case

Quoted 11 lines · Peter Maydell
Quoted 7 lines · Alex Bennée

Now WFE can actually suspend on A-profile we also need to handle when its trapped. To do this we need to pass the instruction size so we can deal with the is_16bit syndrome encoding.

Doesn't this need to go earlier in the patchset? Otherwise there's a point where WFE can go to sleep but doesn't honour the trap bit.

Hmm - the way I read the description was we would only trap if we were going to sleep. Pre 6/8 we never sleep so we can't trap. I could merge it with 6/8 but then that makes the patch even bigger.

Quoted 3 lines

thanks
-- PMM

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

Re: [PATCH v7 7/8] target/arm: handle the WFE trap case

Quoted 9 lines · Alex Bennée
Quoted 6 lines · Peter Maydell
Quoted 5 lines · Alex Bennée

Now WFE can actually suspend on A-profile we also need to handle when its trapped. To do this we need to pass the instruction size so we can deal with the is_16bit syndrome encoding.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Doesn't this need to go earlier in the patchset? Otherwise there's a point where WFE can go to sleep but doesn't honour the trap bit.

Hmm - the way I read the description was we would only trap if we were going to sleep. Pre 6/8 we never sleep so we can't trap.

Yes, that's the point. The guarantee we have to uphold with the trap bits is "if the WFE would actually go to sleep, then we need to take the trap".

- before this patchset: we never sleep, so never need to trap
- after patch 6: we can sleep, but we incorrectly won't trap
- after this patch: we can sleep and will trap

So there's a window in this patchseries (after 6 but before 7) where we don't do the right thing.

-- PMM

#12 lore raw

Re: [PATCH v7 7/8] target/arm: handle the WFE trap case

Quoted 23 lines · Peter Maydell
Quoted 9 lines · Alex Bennée
Quoted 6 lines · Peter Maydell
Quoted 5 lines · Alex Bennée

Now WFE can actually suspend on A-profile we also need to handle when its trapped. To do this we need to pass the instruction size so we can deal with the is_16bit syndrome encoding.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Doesn't this need to go earlier in the patchset? Otherwise there's a point where WFE can go to sleep but doesn't honour the trap bit.

Hmm - the way I read the description was we would only trap if we were going to sleep. Pre 6/8 we never sleep so we can't trap.

Yes, that's the point. The guarantee we have to uphold with the trap bits is "if the WFE would actually go to sleep, then we need to take the trap".

Quoted 6 lines

- before this patchset: we never sleep, so never need to trap
- after patch 6: we can sleep, but we incorrectly won't trap
- after this patch: we can sleep and will trap

So there's a window in this patchseries (after 6 but before 7) where we don't do the right thing.

OK I'll squash the two patches together.

Quoted 2 lines

-- PMM

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

PATCH v7 8/8 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 ebdf09be38a..5e4d828dd55 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 a4603e1a5c7..fb9a212df4b 100644
@@ -2086,14 +2086,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
#14 lore raw
In reply to Alex Bennée
Quoted 19 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.

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.

Quoted 8 lines

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]>

All patches are now reviewed.

Applied to target-arm.next, thanks.

-- PMM

#15 lore raw
Quoted 28 lines · Peter Maydell
Quoted 26 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.

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]>

Quoted 5 lines
Quoted 1 line

All patches are now reviewed.

Applied to target-arm.next, thanks.

This turns out to cause failures in "make check-functional": some tests now time out. It looks like the patch that triggers this is the "enable event stream on WFE instructions".

13/79 func-thorough+func-arm-thorough+thorough -
qemu:func-arm-aspeed_rainier ERROR 36.17s
exit status 1
74/79 func-thorough+func-arm-thorough+thorough -
qemu:func-arm-smdkc210 TIMEOUT 90.01s
killed by signal 15 SIGTERM
75/79 func-thorough+func-arm-thorough+thorough - qemu:func-arm-raspi2
TIMEOUT 120.01s killed by signal 15
SIGTERM
76/79 func-thorough+func-arm-thorough+thorough -
qemu:func-arm-quanta_gsj TIMEOUT 240.01s
killed by signal 15 SIGTERM
77/79 func-thorough+func-arm-thorough+thorough - qemu:func-arm-bpim2u
TIMEOUT 500.01s killed by signal 15
SIGTERM
78/79 func-thorough+func-arm-thorough+thorough -
qemu:func-arm-orangepi TIMEOUT 540.01s
killed by signal 15 SIGTERM
79/79 func-thorough+func-aarch64-thorough+thorough -
qemu:func-aarch64-sbsaref_freebsd TIMEOUT 720.02s
killed by signal 15 SIGTERM

I've kept the first 4 patches, but dropped patches 5-8. I also noticed a problem with patch 5 which I've commented on.

thanks
-- PMM

#16 lore raw
Quoted 62 lines · Peter Maydell
Quoted 28 lines · Peter Maydell
Quoted 26 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.

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]>

Quoted 5 lines
Quoted 1 line

All patches are now reviewed.

Applied to target-arm.next, thanks.

This turns out to cause failures in "make check-functional": some tests now time out. It looks like the patch that triggers this is the "enable event stream on WFE instructions".

13/79 func-thorough+func-arm-thorough+thorough -
qemu:func-arm-aspeed_rainier ERROR 36.17s
exit status 1
74/79 func-thorough+func-arm-thorough+thorough -
qemu:func-arm-smdkc210 TIMEOUT 90.01s
killed by signal 15 SIGTERM
75/79 func-thorough+func-arm-thorough+thorough - qemu:func-arm-raspi2
TIMEOUT 120.01s killed by signal 15
SIGTERM
76/79 func-thorough+func-arm-thorough+thorough -
qemu:func-arm-quanta_gsj TIMEOUT 240.01s
killed by signal 15 SIGTERM
77/79 func-thorough+func-arm-thorough+thorough - qemu:func-arm-bpim2u
TIMEOUT 500.01s killed by signal 15
SIGTERM
78/79 func-thorough+func-arm-thorough+thorough -
qemu:func-arm-orangepi TIMEOUT 540.01s
killed by signal 15 SIGTERM
79/79 func-thorough+func-aarch64-thorough+thorough -
qemu:func-aarch64-sbsaref_freebsd TIMEOUT 720.02s
killed by signal 15 SIGTERM

Quoted 2 lines

I've kept the first 4 patches, but dropped patches 5-8. I also noticed a problem with patch 5 which I've commented on.

OK I'll have a look.

Quoted 3 lines

thanks
-- PMM

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#17 lore raw
In reply to Peter Maydell
Quoted 27 lines · Peter Maydell
Quoted 26 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.

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]>

Quoted 5 lines
Quoted 1 line

All patches are now reviewed.

Applied to target-arm.next, thanks.

Is it too late to drop them? I've a fix for 2/8:

--8<---------------cut here---------------start------------->8---
modified  include/hw/core/sysemu-cpu-ops.h
@@ -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 */
     /**
modified    target/arm/cpu.c
@@ -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;
     }

@@ -874,6 +872,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;

}
--8<---------------cut here---------------end--------------->8---

Quoted 2 lines

-- PMM

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#18 lore raw
Quoted 30 lines · Alex Bennée
Quoted 27 lines · Peter Maydell
Quoted 26 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.

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]>

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

All patches are now reviewed.

Applied to target-arm.next, thanks.

Is it too late to drop them? I've a fix for 2/8:

That change is already in upstream git, so you'll need to send the fix as a standalone patch, I'm afraid.

-- PMM

#19 lore raw
Quoted 35 lines · Peter Maydell
Quoted 34 lines · Alex Bennée
Quoted 31 lines · Peter Maydell
Quoted 27 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.

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]>

All patches are now reviewed.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Applied to target-arm.next, thanks.

Is it too late to drop them? I've a fix for 2/8:

That change is already in upstream git, so you'll need to send the fix as a standalone patch, I'm afraid.

No worries, I'll fix it up in the re-base.

Quoted 2 lines

-- PMM

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro