[PATCH 11/12] KVM: s390: arm64: Trap WFx

Steffen Eiden seiden at linux.ibm.com
Wed Dec 2 06:11:49 PST 2026


From: Gautam Gala <ggala at 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 at linux.ibm.com>
Signed-off-by: Gautam Gala <ggala at linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden at 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




More information about the linux-arm-kernel mailing list