mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Steffen Eiden <seiden@linux.ibm.com>
To: Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>,
	Fuad Tabba <fuad.tabba@linux.dev>,
	Joey Gouly <joey.gouly@arm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Zenghui Yu <yuzenghui@huawei.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Andreas Grapentin <gra@linux.ibm.com>,
	Christian Borntraeger <borntraeger@linux.ibm.com>,
	Janosch Frank <frankja@linux.ibm.com>,
	Claudio Imbrenda <imbrenda@linux.ibm.com>,
	David Hildenbrand <david@kernel.org>,
	Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Alexander Gordeev <agordeev@linux.ibm.com>,
	Sven Schnelle <svens@linux.ibm.com>,
	Friedrich Welter <fritz@linux.ibm.com>,
	Gautam Gala <ggala@linux.ibm.com>,
	Hariharan Mari <hari55@linux.ibm.com>,
	Hendrik Brueckner <brueckner@linux.ibm.com>,
	Nico Boehr <nrb@linux.ibm.com>,
	Ulrich Weigand <Ulrich.Weigand@de.ibm.com>,
	Arnd Bergmann <arnd@arndb.de>,
	Paolo Bonzini <pbonzini@redhat.com>
Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
	linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
	linux-s390@vger.kernel.org, Steffen Eiden <seiden@linux.ibm.com>
Subject: [PATCH 11/12] KVM: s390: arm64: Trap WFx
Date: Wed, 02 Dec 2026 15:11:49 +0100	[thread overview]
Message-ID: <20261202-smccc-v1-11-48340e2737b6@linux.ibm.com> (raw)
In-Reply-To: <20261202-smccc-v1-0-48340e2737b6@linux.ibm.com>

From: Gautam Gala <ggala@linux.ibm.com>

Enable the TWI and TWE trap bits in HCR_EL2 and handle the resulting
WFx exceptions in kvm_handle_wfx().

WFI halts the vCPU until it becomes runnable again, i.e. until userspace
asserts an IRQ/FIQ line (KVM_IRQ_LINE) or a guest timer output level
changes. WFE is treated as a directed yield to another vCPU of the same
VM. WFxT carries a deadline: compare it against the guest counter
obtained with PTFF QAGPT from the guest physical time offset (GPTO) in
the SAE block, and complete the instruction immediately if it has
already passed. Otherwise, halt as for WFI with IN_WFIT set so the timer
code takes the deadline into account. WFxT with an invalid RN is
executed as plain WFI/WFE.

Co-developed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Gautam Gala <ggala@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
---
 arch/s390/kvm/arm64/handle_exit.c | 54 +++++++++++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/reset.c       |  3 ++-
 2 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/arch/s390/kvm/arm64/handle_exit.c b/arch/s390/kvm/arm64/handle_exit.c
index 551cf6fc3afe..06f73be83f46 100644
--- a/arch/s390/kvm/arm64/handle_exit.c
+++ b/arch/s390/kvm/arm64/handle_exit.c
@@ -6,6 +6,8 @@
 #include <arm64/kvm_emulate.h>
 #include <kvm/arm_hypercalls.h>
 
+#include <asm/timex.h>
+
 #include "handle_exit.h"
 
 typedef int (*exit_handle_fn)(struct kvm_vcpu *);
@@ -43,8 +45,60 @@ static int handle_smc(struct kvm_vcpu *vcpu)
 	return kvm_smccc_call_handler(vcpu);
 }
 
+/**
+ * kvm_handle_wfx - handle a wait-for-interrupts or wait-for-event
+ *		    instruction executed by a guest
+ *
+ * @vcpu:	the vcpu pointer
+ *
+ * WFI: Virtualize the instruction by halting the vCPU until it becomes
+ * runnable, i.e. until userspace asserts an IRQ/FIQ line (KVM_IRQ_LINE) or
+ * a guest timer output level changes.
+ * WFE: Handled as a directed yield to another vCPU of the same VM.
+ * WFxT: WFI/WFE with a deadline; if the deadline has already passed, the
+ * instruction completes immediately.
+ */
+static int kvm_handle_wfx(struct kvm_vcpu *vcpu)
+{
+	u64 esr = kvm_vcpu_get_esr(vcpu);
+	bool is_wfe = !!(esr & ESR_ELx_WFx_ISS_WFE);
+
+	if (is_wfe)
+		vcpu->stat.wfe_exit_stat++;
+	else
+		vcpu->stat.wfi_exit_stat++;
+
+	if (esr & ESR_ELx_WFx_ISS_WFxT) {
+		if (esr & ESR_ELx_WFx_ISS_RV) {
+			u64 val, now;
+
+			now = ptff_qagpt(vcpu->arch.sae_block.gpto);
+			val = vcpu_get_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu));
+			if (now >= val)
+				goto out;
+		} else {
+			/* Treat WFxT as WFx if RN is invalid */
+			esr &= ~ESR_ELx_WFx_ISS_WFxT;
+		}
+	}
+
+	if (esr & ESR_ELx_WFx_ISS_WFE) {
+		kvm_vcpu_on_spin(vcpu, vcpu_mode_priv(vcpu));
+	} else {
+		if (esr & ESR_ELx_WFx_ISS_WFxT)
+			vcpu_set_flag(vcpu, IN_WFIT);
+
+		kvm_vcpu_wfi(vcpu);
+	}
+out:
+	kvm_incr_pc(vcpu);
+
+	return 1;
+}
+
 static exit_handle_fn arm_exit_handlers[] = {
 	[0 ... ESR_ELx_EC_MAX]	= kvm_handle_unknown_ec,
+	[ESR_ELx_EC_WFx]	= kvm_handle_wfx,
 	[ESR_ELx_EC_SMC64]	= handle_smc,
 	[ESR_ELx_EC_HVC64]	= handle_hvc,
 	[ESR_ELx_EC_SYS64]	= kvm_handle_sys_reg,
diff --git a/arch/s390/kvm/arm64/reset.c b/arch/s390/kvm/arm64/reset.c
index 391f6e5e49b7..feec5009d2cb 100644
--- a/arch/s390/kvm/arm64/reset.c
+++ b/arch/s390/kvm/arm64/reset.c
@@ -30,7 +30,8 @@ static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
 	/* traps */
 	vcpu->arch.hcr_el2 |= HCR_EL2_TSC | HCR_EL2_TID1 | HCR_EL2_TID2 |
 			      HCR_EL2_TID3 | HCR_EL2_TID4 | HCR_EL2_TID5 |
-			      HCR_EL2_TIDCP | HCR_EL2_TLOR;
+			      HCR_EL2_TIDCP | HCR_EL2_TLOR |
+			      HCR_EL2_TWI | HCR_EL2_TWE;
 }
 
 void vcpu_reset_hcrx(struct kvm_vcpu *vcpu)

-- 
2.56.1


  parent reply	other threads:[~2026-12-02 14:12 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 14:11 [PATCH 00/12] KVM: s390: arm64: SMCCC, timers and IRQ line Steffen Eiden
2026-12-02 14:11 ` [PATCH 01/12] arm64: Prepare for sharing with s390 Steffen Eiden
2026-12-02 14:11 ` [PATCH 02/12] arm64: Share timer and smccc functionality " Steffen Eiden
2026-12-02 14:11 ` [PATCH 03/12] KVM: arm64: Share timer and smccc code " Steffen Eiden
2026-12-02 14:11 ` [PATCH 04/12] KVM: s390: arm64: Use register shadow helper Steffen Eiden
2026-12-02 14:11 ` [PATCH 05/12] KVM: s390: arm64: Shadow the guest timer CVAL registers Steffen Eiden
2026-12-02 14:11 ` [PATCH 06/12] KVM: s390: arm64: Use mpstate for guests Steffen Eiden
2026-12-02 14:11 ` [PATCH 07/12] KVM: s390: arm64: Enable full smccc support Steffen Eiden
2026-12-02 14:11 ` [PATCH 08/12] KVM: s390: arm64: Report fwregs to userspace Steffen Eiden
2026-12-02 14:11 ` [PATCH 09/12] KVM: s390: arm64: Provide timer infrastructure Steffen Eiden
2026-12-02 14:11 ` [PATCH 10/12] KVM: s390: arm64: Integrate guest timer Steffen Eiden
2026-12-02 14:11 ` Steffen Eiden [this message]
2026-12-02 14:11 ` [PATCH 12/12] KVM: s390: arm64: Add IRQ line Steffen Eiden

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=20261202-smccc-v1-11-48340e2737b6@linux.ibm.com \
    --to=seiden@linux.ibm.com \
    --cc=Ulrich.Weigand@de.ibm.com \
    --cc=agordeev@linux.ibm.com \
    --cc=arnd@arndb.de \
    --cc=borntraeger@linux.ibm.com \
    --cc=brueckner@linux.ibm.com \
    --cc=catalin.marinas@arm.com \
    --cc=david@kernel.org \
    --cc=frankja@linux.ibm.com \
    --cc=fritz@linux.ibm.com \
    --cc=fuad.tabba@linux.dev \
    --cc=ggala@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=gra@linux.ibm.com \
    --cc=hari55@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=imbrenda@linux.ibm.com \
    --cc=joey.gouly@arm.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=nrb@linux.ibm.com \
    --cc=oupton@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=suzuki.poulose@arm.com \
    --cc=svens@linux.ibm.com \
    --cc=will@kernel.org \
    --cc=yuzenghui@huawei.com \
    /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

all inboxes | Powered by JetHome®