Commit f11290e0 authored by Fuad Tabba's avatar Fuad Tabba Committed by Marc Zyngier
Browse files

KVM: arm64: Refactor checks for FP state ownership



To avoid direct comparison against the fp_owner enum, add a new
function that performs the check, host_owns_fp_regs(), to
complement the existing guest_owns_fp_regs().

To check for fpsimd state ownership, use the helpers instead of
directly using the enums.

No functional change intended.

Suggested-by: default avatarMarc Zyngier <maz@kernel.org>
Signed-off-by: default avatarFuad Tabba <tabba@google.com>
Reviewed-by: default avatarMark Brown <broonie@kernel.org>
Acked-by: default avatarOliver Upton <oliver.upton@linux.dev>
Link: https://lore.kernel.org/r/20240423150538.2103045-4-tabba@google.com


Signed-off-by: default avatarMarc Zyngier <maz@kernel.org>
parent b5b85bd7
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -587,16 +587,14 @@ static __always_inline u64 kvm_get_reset_cptr_el2(struct kvm_vcpu *vcpu)
	} else if (has_hvhe()) {
		val = (CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN);

		if (!vcpu_has_sve(vcpu) ||
		    (*host_data_ptr(fp_owner) != FP_STATE_GUEST_OWNED))
		if (!vcpu_has_sve(vcpu) || !guest_owns_fp_regs())
			val |= CPACR_EL1_ZEN_EL1EN | CPACR_EL1_ZEN_EL0EN;
		if (cpus_have_final_cap(ARM64_SME))
			val |= CPACR_EL1_SMEN_EL1EN | CPACR_EL1_SMEN_EL0EN;
	} else {
		val = CPTR_NVHE_EL2_RES1;

		if (vcpu_has_sve(vcpu) &&
		    (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED))
		if (vcpu_has_sve(vcpu) && guest_owns_fp_regs())
			val |= CPTR_EL2_TZ;
		if (cpus_have_final_cap(ARM64_SME))
			val &= ~CPTR_EL2_TSM;
+6 −0
Original line number Diff line number Diff line
@@ -1213,6 +1213,12 @@ static inline bool guest_owns_fp_regs(void)
	return *host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED;
}

/* Check whether the FP regs are owned by the host */
static inline bool host_owns_fp_regs(void)
{
	return *host_data_ptr(fp_owner) == FP_STATE_HOST_OWNED;
}

static inline void kvm_init_host_cpu_context(struct kvm_cpu_context *cpu_ctxt)
{
	/* The host's MPIDR is immutable, so let's set it up at boot time */
+2 −3
Original line number Diff line number Diff line
@@ -141,8 +141,7 @@ void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)

	WARN_ON_ONCE(!irqs_disabled());

	if (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED) {

	if (guest_owns_fp_regs()) {
		/*
		 * Currently we do not support SME guests so SVCR is
		 * always 0 and we just need a variable to point to.
@@ -195,7 +194,7 @@ void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
		isb();
	}

	if (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED) {
	if (guest_owns_fp_regs()) {
		if (vcpu_has_sve(vcpu)) {
			__vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);

+1 −1
Original line number Diff line number Diff line
@@ -370,7 +370,7 @@ static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
	isb();

	/* Write out the host state if it's in the registers */
	if (*host_data_ptr(fp_owner) == FP_STATE_HOST_OWNED)
	if (host_owns_fp_regs())
		__fpsimd_save_state(*host_data_ptr(fpsimd_state));

	/* Restore the guest state */
+1 −1
Original line number Diff line number Diff line
@@ -337,7 +337,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)

	__sysreg_restore_state_nvhe(host_ctxt);

	if (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED)
	if (guest_owns_fp_regs())
		__fpsimd_save_fpexc32(vcpu);

	__debug_switch_to_host(vcpu);
Loading