Skip to contentSkip to conversation

KVM: arm64: Fix kvm_vcpu_{set,is}_be() to deal with EL2 state

PATCH Applied Marc Zyngier 2 participants 4 messages Sep 16, 2025 – Sep 17, 2025

Subsystems (2)
lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

PATCH 1/1 KVM: arm64: Fix kvm_vcpu_{set,is}_be() to deal with EL2 state

Nobody really cares about BE, but KVM currently only deals with SCTLR_EL1 when evaluating or setting the endianness in PSCI, meaning that we evaluate whatever the L2 state has been at some point.

Teach these primitives about SCTLR_EL2, and forget about BE...

---
 arch/arm64/include/asm/kvm_emulate.h | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +14 -6
index fa8a08a1ccd5c..85f998dffea5d 100644
@@ -511,21 +511,29 @@ static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu)
 	if (vcpu_mode_is_32bit(vcpu)) {
 		*vcpu_cpsr(vcpu) |= PSR_AA32_E_BIT;
 	} else {
-		u64 sctlr = vcpu_read_sys_reg(vcpu, SCTLR_EL1);
+		enum vcpu_sysreg r;
+		u64 sctlr;
+
+		r = vcpu_has_nv(vcpu) ? SCTLR_EL2 : SCTLR_EL1;
+
+		sctlr = vcpu_read_sys_reg(vcpu, r);
 		sctlr |= SCTLR_ELx_EE;
-		vcpu_write_sys_reg(vcpu, sctlr, SCTLR_EL1);
+		vcpu_write_sys_reg(vcpu, sctlr, r);
 	}
 }
 
 static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
 {
+	enum vcpu_sysreg r;
+	u64 bit;
+
 	if (vcpu_mode_is_32bit(vcpu))
 		return !!(*vcpu_cpsr(vcpu) & PSR_AA32_E_BIT);
 
-	if (vcpu_mode_priv(vcpu))
-		return !!(vcpu_read_sys_reg(vcpu, SCTLR_EL1) & SCTLR_ELx_EE);
-	else
-		return !!(vcpu_read_sys_reg(vcpu, SCTLR_EL1) & SCTLR_EL1_E0E);
+	r = is_hyp_ctxt(vcpu) ? SCTLR_EL2 : SCTLR_EL1;
+	bit = vcpu_mode_priv(vcpu) ? SCTLR_ELx_EE : SCTLR_EL1_E0E;
+
+	return vcpu_read_sys_reg(vcpu, r) & bit;
 }
 
 static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu,
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Marc Zyngier

Nobody really cares about BE, but KVM currently only deals with SCTLR_EL1 when evaluating or setting the endianness in PSCI, meaning that we evaluate whatever the L2 state has been at some point.

Teach these primitives about SCTLR_EL2, and forget about BE...

You know, I was about to say "don't we need to sanitise this?" although that seems to already be in place. Nice.

Thanks,
Oliver

author maintainer
#2 lore raw
Quoted 6 lines · Oliver Upton
Quoted 4 lines · Marc Zyngier

Nobody really cares about BE, but KVM currently only deals with SCTLR_EL1 when evaluating or setting the endianness in PSCI, meaning that we evaluate whatever the L2 state has been at some point.

Quoted 6 lines
Quoted 3 lines

Teach these primitives about SCTLR_EL2, and forget about BE...

You know, I was about to say "don't we need to sanitise this?" although that seems to already be in place. Nice.

Only for SCTLR_EL1.

SCTLR_EL2, annoyingly enough, isn't, owing to plenty of bits depending on E2H. It's on my long list of things to address, but I'd like a common mechanism that'd deal with horrors such as CNTHCTL_EL2...

Quoted 1 line

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author maintainer Applied
#3 lore raw
In reply to Marc Zyngier
Quoted 8 lines · Marc Zyngier

Nobody really cares about BE, but KVM currently only deals with SCTLR_EL1 when evaluating or setting the endianness in PSCI, meaning that we evaluate whatever the L2 state has been at some point.

Teach these primitives about SCTLR_EL2, and forget about BE...

[...]

Applied to next, thanks!

[1/1] KVM: arm64: Fix kvm_vcpu_{set,is}_be() to deal with EL2 state

      commit: 6515c612e79949b17ef4b8c4180c07bbeaf01e4d

Cheers,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.