target/arm: fully model WFxT instructions for A-profile
v6 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]>
Only the following patches still need review:
target/arm: enable event stream on WFE instructions target/arm: fix WFET typo in syndrome
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
PATCH v6 1/8 target/arm: fix WFET typo in syndrome
A stray x slipped in and we didn't notice!
--- target/arm/syndrome.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/syndrome.h +1 -1
@@ -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
Re: [PATCH v6 1/8] target/arm: fix WFET typo in syndrome
Quoted 13 lines · Alex Bennée
A stray x slipped in and we didn't notice!
Fixes 4575da5ecb7 (target/arm: report register in WFIT syndromes)Signed-off-by Alex Bennée <[email protected]>--- target/arm/syndrome.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/arm/syndrome.h +0
-0
target/arm/syndrome.h typedef enum { · line 680
Quoted 8 lines
target/arm/syndrome.h +1
-1@@ -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)
r~
Re: [PATCH v6 1/8] target/arm: fix WFET typo in syndrome
In reply to Alex BennéeQuoted 2 lines · Alex Bennée
A stray x slipped in and we didn't notice!
You might mention that this didn't cause any actual bugs because at the moment we don't use the "WFET" enum value anywhere, only "WFI" and "WFIT".
thanks
-- PMM
PATCH v6 2/8 target/arm: teach arm_cpu_has_work about halting reasons
In reply to Alex BennéeWith 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
@@ -260,6 +260,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];
@@ -781,6 +794,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
@@ -2010,4 +2010,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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH v6 3/8 target/arm: redefine event stream fields
In reply to Alex BennéeThe 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
@@ -269,14 +269,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 )
@@ -286,7 +289,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
@@ -1766,9 +1766,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 |
@@ -1783,7 +1783,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
PATCH v6 4/8 target/arm: ensure aarch64 DISAS_WFE will exit
In reply to Alex BennéeThis 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.
---
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
@@ -10913,25 +10913,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
PATCH v6 5/8 target/arm: implements SEV/SEVL for all modes
In reply to Alex BennéeRemove 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
@@ -854,6 +854,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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH v6 6/8 target/arm: enable event stream on WFE instructions
In reply to Alex BennéeTwo 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
@@ -876,10 +876,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
@@ -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
@@ -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
Re: [PATCH v6 6/8] target/arm: enable event stream on WFE instructions
Quoted 31 lines · 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_timerand 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.
Signed-off-by Alex Bennée<[email protected]>---
v2
- merged target/arm: addgt_calc_next_event_stream
- update to usehalt_reason
- madearm_wfxt_timer_cbatomically consumehalt_reason
v4
- skip sleep if in the exclusive region
- update commit message
- remove theCF_PARALLELguards so we work in smp
v5
- useenv_archcpufor ARMCPU rather then expensive QOM cast
- rely oncpu->wfxt_timerto guard event stream leg
v6
- useatomic_xchgto consumeevent_register
- remove extraneoustarget_elcalculation
Quoted 6 lines
--- 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(-)
r~
PATCH v6 7/8 target/arm: handle the WFE trap case
In reply to Alex BennéeNow 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
@@ -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
@@ -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
@@ -10935,7 +10935,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
@@ -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
PATCH v6 8/8 target/arm: implement WFET
In reply to Alex BennéeNow 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
@@ -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
@@ -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