Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9A666C43458 for ; Mon, 6 Jul 2026 10:42:59 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wggif-0007E3-VN; Mon, 06 Jul 2026 06:38:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wggiR-00073P-4N for qemu-devel@nongnu.org; Mon, 06 Jul 2026 06:38:22 -0400 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1wggiO-0003KT-Kl for qemu-devel@nongnu.org; Mon, 06 Jul 2026 06:38:18 -0400 Received: by mail-wm1-x341.google.com with SMTP id 5b1f17b1804b1-493c59f740cso19655155e9.3 for ; Mon, 06 Jul 2026 03:38:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1783334291; x=1783939091; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=+wrMWj5qI1NLseeZ04VrSRvuInkOzHOMvMqTvd4qMTs=; b=JMsIhyY/HrGPEOM6wcu98NjEUZLWOS82b1q8hy4HijeffHxwEBzMl1r63939fE/92a LGH8lclfiW7QvR+bqBqRix6BAlIyKbghsG0XZ2N+YdpHZPKhSGATCkjvwp8xCrlqxQiC LUM3A7TXUso16jd0ngz8yeAfDoTIx9AAi8Ooceraz6+7f6DkcaGvLirKH3Z9Z8RTvA7c NUDE6uw0jyazARz2qCONKzYEtMxR/BJXGsDtNT6soUsmCAvzkzELGWz9Q/Mgny1Fjm0L G5HKzfK+2+fY2qXC5V/XAYsMDd0oXb2RDJzlMId9rIL4bP/JVUZuWH4K26L4SMNk0J7E Iv6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783334291; x=1783939091; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=+wrMWj5qI1NLseeZ04VrSRvuInkOzHOMvMqTvd4qMTs=; b=o7FJIOLbUkeV1UyItYUatRJMpHHSSWOUDk3feu1zd6pSciD9jdtalxf46UJZ2uzH9y yCgIPmZjDn2dFY4Be5zyqYQKnNkj97rX2pSJi/qCcWvUPWWBMFXgcdZlVYxDJCEY8WbC jasMoX6Qt/aBeCfJi09uFU0MrK7Z2kkpUdv6iymG5g5sm5cIBlxJvoEzMbp3dwqX5sXQ LHdf1RiKYe3GTfUG9PVsf/CDPDk/34wPt5Ju7lNCo2zs1Dh7P7lE30tjUsUQraFS7KC/ NtouQK35DtEWQ1C29uX35AlSSgr4M5sqfD2xQ707U7fPZc8Enl+fp3/rMjdvAphaNVBN 6jkg== X-Gm-Message-State: AOJu0Yz7VAO7zJ+tX68ynLpTYavpSMYWwteuLYwwFEvycGXuF6UV3ajH H/Bvilhe1Zoz24N4CDK+7I83p+f6zR+k8LKNWl2mWIvwrv7uk4FElSg8ajMv5Wvc/UWc8du67gK gDsfOLvLpllW+ X-Gm-Gg: AfdE7cmpEU7OENjjzmIP/eT/fEoaa/TZCLKtATu4B/wDt6HuEOzNAw0qpKB6w4cT169 8DQL5jtRqugxamqC5tTd0JRtMQcuD7qOwvs1yD/cVp+T6hH+gw7MnoZ4jXFZOSUs9sMVyMtVMa7 tXNbyF/Z2XCxgFJxmVhso5+2pUjgln4xmIqh43xwuNwnQPqSAS16IpUGv+ld3Nb+mub0YNQWEp8 MAl3AI0m9ngCGwvGiS8vWUDx8jokSIgh9L0TLY98jgqCf7Brz+bCnZP4SobRBrZA0bzroJNos9l h2EddV6r3opvPqJdmN5KzNa3upoq2mFKNdlYrRhVTD3h4DVkmT3lvj5LeiLz49tCA3R2h3W/eNk dt/K6HsuPVTJN8d9/xEKLilDz/8q8kc3jv/YWQZF0GArlUvSTvX/24gEn+ZZYW5BXQNhpXATV7z KzKjCA8c3AZbE7trGMSYmflgDZ0tvWSAEzifyWTJ6ahybrrMh0Um+F8ziCLCTJvpW2aFCLdwysL Jcj67YfF9lNZ34A4jZdAQ== X-Received: by 2002:a05:600c:620e:b0:493:bed7:6d7c with SMTP id 5b1f17b1804b1-493d30d39d8mr88797895e9.25.1783334291413; Mon, 06 Jul 2026 03:38:11 -0700 (PDT) Received: from lanath.. (wildly.archaic.org.uk. [81.2.115.145]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-493c63b6f1bsm329501805e9.10.2026.07.06.03.38.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jul 2026 03:38:10 -0700 (PDT) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PULL 17/49] target/arm: implement WFET Date: Mon, 6 Jul 2026 11:37:20 +0100 Message-ID: <20260706103752.320511-18-peter.maydell@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260706103752.320511-1-peter.maydell@linaro.org> References: <20260706103752.320511-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2a00:1450:4864:20::341; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x341.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: 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. Reviewed-by: Richard Henderson Signed-off-by: Alex Bennée Message-id: 20260624103049.884930-8-alex.bennee@linaro.org Signed-off-by: Peter Maydell --- 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(-) diff --git a/target/arm/tcg/helper-defs.h b/target/arm/tcg/helper-defs.h index 99ebd75494..0077aeb4e2 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 3321e29898..c4433be2ed 100644 --- a/target/arm/tcg/op_helper.c +++ b/target/arm/tcg/op_helper.c @@ -641,6 +641,100 @@ 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 + CPUState *cs = env_cpu(env); + uint32_t excp; + int target_el; + ARMCPU *cpu; + uint64_t cntval, timeout, offset, cntvct, nexttick; + int64_t next_event; + + /* + * As for WFE if the event register is already set we can consume + * the event and return immediately. + */ + if (qatomic_xchg(&env->event_register, false)) { + return; + } + + /* + * Don't bother to go into our "low power state" if + * we would just wake up immediately. + * + * 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(). + */ + cntval = gt_get_countervalue(env); + offset = gt_direct_access_timer_offset(env, GTIMER_VIRT); + cntvct = cntval - offset; + timeout = env->xregs[rd]; + if (cpu_has_work(cs) || cntvct >= timeout) { + 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) { + env->pc -= 4; + raise_exception(env, excp, syn_wfx(1, 0xe, rd, true, WFET, false), target_el); + } + + /* + * If the CPU has entered the exclusive region we could sleep + * until the global monitor moves from Exclusive to Open Access. + * However it would be expensive for QEMU to fully model the + * global monitor and not doing so would potentially trigger + * deadlocks in WFE enabled locking code. However as WFE is a hint + * instruction the architecture allows for the PE to leave + * low-power state for any reason. QEMU chooses to treat being in + * an exclusive region as such and return directly. + */ + if (env->exclusive_addr != -1) { + return; + } + + /* + * Finally work out if the timeout or event stream will kick in + * earlier. + * + * The WFET should time out when CNTVCT_EL0 >= the specified value. + */ + cpu = env_archcpu(env); + 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 cdf843984d..69648ad94b 100644 --- a/target/arm/tcg/translate-a64.c +++ b/target/arm/tcg/translate-a64.c @@ -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; } -- 2.43.0