Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Pedro Barbuda" <pbarbuda@microsoft.com>,
	"Alexander Graf" <agraf@csgraf.de>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Mohamed Mediouni" <mohamed@unpredictable.fr>,
	kvm@vger.kernel.org, qemu-arm@nongnu.org,
	"Alex Bennée" <alex.bennee@linaro.org>
Subject: [PATCH v3 32/32] target/arm: implement WFET
Date: Wed, 22 Apr 2026 13:52:49 +0100	[thread overview]
Message-ID: <20260422125250.1303100-33-alex.bennee@linaro.org> (raw)
In-Reply-To: <20260422125250.1303100-1-alex.bennee@linaro.org>

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

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

---
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
---
 target/arm/tcg/helper-defs.h   |  1 +
 target/arm/tcg/op_helper.c     | 80 ++++++++++++++++++++++++++++++++++
 target/arm/tcg/translate-a64.c | 15 ++++---
 3 files changed, 89 insertions(+), 7 deletions(-)

diff --git a/target/arm/tcg/helper-defs.h b/target/arm/tcg/helper-defs.h
index ebdf09be38a..5e4d828dd55 100644
--- a/target/arm/tcg/helper-defs.h
+++ b/target/arm/tcg/helper-defs.h
@@ -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)
diff --git a/target/arm/tcg/op_helper.c b/target/arm/tcg/op_helper.c
index fe34c94e890..112e0c2eb1a 100644
--- a/target/arm/tcg/op_helper.c
+++ b/target/arm/tcg/op_helper.c
@@ -620,6 +620,86 @@ void HELPER(wfe)(CPUARMState *env, uint32_t insn_len)
 #endif
 }
 
+void HELPER(wfet)(CPUARMState *env, uint32_t rd)
+{
+#ifdef CONFIG_USER_ONLY
+    /*
+     * As for WFIT make it NOP here, because trying to raise EXCP_HLT
+     * would trigger an abort.
+     */
+    return;
+#else
+    ARMCPU *cpu = env_archcpu(env);
+    CPUState *cs = env_cpu(env);
+    uint32_t excp;
+    int target_el = check_wfx_trap(env, true, &excp);
+    /* The WFET should time out when CNTVCT_EL0 >= the specified value. */
+    uint64_t cntval = gt_get_countervalue(env);
+    uint64_t timeout = env->xregs[rd];
+    /*
+     * We want the value that we would get if we read CNTVCT_EL0 from
+     * the current exception level, so the direct_access offset, not
+     * the indirect_access one. Compare the pseudocode LocalTimeoutEvent(),
+     * which calls VirtualCounterTimer().
+     */
+    uint64_t offset = gt_direct_access_timer_offset(env, GTIMER_VIRT);
+    uint64_t cntvct = cntval - offset;
+    uint64_t nexttick;
+    int64_t next_event;
+
+    /*
+     * As for WFE if the event register is already set we can consume
+     * the event and return immediately.
+     */
+    if (env->event_register) {
+        env->event_register = false;
+        return;
+    }
+
+
+    if (cpu_has_work(cs) || cntvct >= timeout) {
+        /*
+         * Don't bother to go into our "low power state" if
+         * we would just wake up immediately.
+         */
+        return;
+    }
+
+    /* We might sleep, so now we check to see if we should trap */
+    if (target_el) {
+        env->pc -= 4;
+        raise_exception(env, excp, syn_wfx(1, 0xe, rd, true, WFET, false), target_el);
+    }
+
+    /*
+     * Finally work out if the timeout or event stream will kick in
+     * earlier.
+     */
+    if (uadd64_overflow(timeout, offset, &nexttick)) {
+        nexttick = UINT64_MAX;
+    }
+    if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
+        nexttick = INT64_MAX;
+    }
+
+    next_event = gt_calc_next_event_stream(env);
+    if (next_event > 0 && next_event < nexttick) {
+        timer_mod(cpu->wfxt_timer, next_event);
+    } else {
+        if (nexttick == INT64_MAX) {
+            timer_mod_ns(cpu->wfxt_timer, INT64_MAX);
+        } else {
+            timer_mod(cpu->wfxt_timer, nexttick);
+        }
+    }
+
+    env->halt_reason = HALT_WFE;
+    cs->exception_index = EXCP_HLT;
+    cs->halted = 1;
+    cpu_loop_exit(cs);
+#endif
+}
+
 void HELPER(yield)(CPUARMState *env)
 {
     CPUState *cs = env_cpu(env);
diff --git a/target/arm/tcg/translate-a64.c b/target/arm/tcg/translate-a64.c
index 83b2a6e848f..c8c8a27956b 100644
--- a/target/arm/tcg/translate-a64.c
+++ b/target/arm/tcg/translate-a64.c
@@ -2093,14 +2093,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;
 }
 
-- 
2.47.3


  parent reply	other threads:[~2026-04-22 13:02 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-22 12:52 [PATCH v3 00/32] target/arm: fully model WFxT instructions for A-profile Alex Bennée
2026-04-22 12:52 ` [PATCH v3 01/32] target/arm: migrate basic syndrome helpers to registerfields Alex Bennée
2026-04-22 12:52 ` [PATCH v3 02/32] target/arm: migrate system/cp trap syndromes " Alex Bennée
2026-04-22 12:52 ` [PATCH v3 03/32] target/arm: migrate FP/SIMD " Alex Bennée
2026-04-22 12:52 ` [PATCH v3 04/32] target/arm: migrate eret " Alex Bennée
2026-04-22 12:52 ` [PATCH v3 05/32] target/arm: migrate SME " Alex Bennée
2026-04-22 12:52 ` [PATCH v3 06/32] target/arm: migrate PAC " Alex Bennée
2026-04-22 12:52 ` [PATCH v3 07/32] target/arm: migrate BTI " Alex Bennée
2026-04-22 12:52 ` [PATCH v3 08/32] target/arm: migrate BXJ " Alex Bennée
2026-04-22 12:52 ` [PATCH v3 09/32] target/arm: migrate Granule Protection traps " Alex Bennée
2026-04-22 12:52 ` [PATCH v3 10/32] target/arm: migrate fault syndromes " Alex Bennée
2026-04-22 16:54   ` Philippe Mathieu-Daudé
2026-04-22 12:52 ` [PATCH v3 11/32] target/arm: migrate debug " Alex Bennée
2026-04-22 12:52 ` [PATCH v3 12/32] target/arm: migrate wfx " Alex Bennée
2026-04-22 12:52 ` [PATCH v3 13/32] target/arm: migrate gcs " Alex Bennée
2026-04-22 12:52 ` [PATCH v3 14/32] target/arm: migrate memory op " Alex Bennée
2026-04-22 12:52 ` [PATCH v3 15/32] target/arm: migrate check_hcr_el2_trap to use syndrome helper Alex Bennée
2026-04-22 12:52 ` [PATCH v3 16/32] target/arm: use syndrome helpers in arm_cpu_do_interrupt_aarch32_hyp Alex Bennée
2026-04-22 12:52 ` [PATCH v3 17/32] target/arm: use syndrome helpers to set SAME_EL EC bit Alex Bennée
2026-04-22 12:52 ` [PATCH v3 18/32] target/arm: make whpx use syndrome helpers for decode Alex Bennée
2026-04-22 12:52 ` [PATCH v3 19/32] target/arm: make hvf " Alex Bennée
2026-04-22 16:54   ` Philippe Mathieu-Daudé
2026-04-22 12:52 ` [PATCH v3 20/32] target/arm: use syndrome helpers in merge_syn_data_abort Alex Bennée
2026-04-22 12:52 ` [PATCH v3 21/32] target/arm: use syndrome helpers to query VNCR bit Alex Bennée
2026-04-22 12:52 ` [PATCH v3 22/32] target/arm: remove old syndrome defines Alex Bennée
2026-04-22 12:52 ` [PATCH v3 23/32] target/arm: report register in WFIT syndromes Alex Bennée
2026-04-22 12:52 ` [PATCH v3 24/32] target/arm: teach arm_cpu_has_work about halting reasons Alex Bennée
2026-04-22 12:52 ` [PATCH v3 25/32] target/arm: redefine event stream fields Alex Bennée
2026-04-22 12:52 ` [PATCH v3 26/32] target/arm: ensure aarch64 DISAS_WFE will exit Alex Bennée
2026-04-22 16:57   ` Philippe Mathieu-Daudé
2026-04-22 12:52 ` [PATCH v3 27/32] target/arm: implements SEV/SEVL for all modes Alex Bennée
2026-04-22 12:52 ` [PATCH v3 28/32] target/arm: hoist event broadcast code into a helper Alex Bennée
2026-04-22 12:52 ` [PATCH v3 29/32] target/arm: implement global monitor events Alex Bennée
2026-04-22 12:52 ` [PATCH v3 30/32] target/arm: enable event stream on WFE instructions Alex Bennée
2026-04-22 12:52 ` [PATCH v3 31/32] target/arm: handle the WFE trap case Alex Bennée
2026-04-22 12:52 ` Alex Bennée [this message]
2026-04-24 18:20 ` [PATCH v3 00/32] target/arm: fully model WFxT instructions for A-profile Alex Bennée
2026-04-27 10:47   ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260422125250.1303100-33-alex.bennee@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=agraf@csgraf.de \
    --cc=kvm@vger.kernel.org \
    --cc=mohamed@unpredictable.fr \
    --cc=pbarbuda@microsoft.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox