Skip to content

Commit f11290e

Browse files
Fuad TabbaMarc Zyngier
authored andcommitted
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: Marc Zyngier <maz@kernel.org> Signed-off-by: Fuad Tabba <tabba@google.com> Reviewed-by: Mark Brown <broonie@kernel.org> Acked-by: Oliver Upton <oliver.upton@linux.dev> Link: https://lore.kernel.org/r/20240423150538.2103045-4-tabba@google.com Signed-off-by: Marc Zyngier <maz@kernel.org>
1 parent b5b85bd commit f11290e

6 files changed

Lines changed: 13 additions & 10 deletions

File tree

arch/arm64/include/asm/kvm_emulate.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,16 +587,14 @@ static __always_inline u64 kvm_get_reset_cptr_el2(struct kvm_vcpu *vcpu)
587587
} else if (has_hvhe()) {
588588
val = (CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN);
589589

590-
if (!vcpu_has_sve(vcpu) ||
591-
(*host_data_ptr(fp_owner) != FP_STATE_GUEST_OWNED))
590+
if (!vcpu_has_sve(vcpu) || !guest_owns_fp_regs())
592591
val |= CPACR_EL1_ZEN_EL1EN | CPACR_EL1_ZEN_EL0EN;
593592
if (cpus_have_final_cap(ARM64_SME))
594593
val |= CPACR_EL1_SMEN_EL1EN | CPACR_EL1_SMEN_EL0EN;
595594
} else {
596595
val = CPTR_NVHE_EL2_RES1;
597596

598-
if (vcpu_has_sve(vcpu) &&
599-
(*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED))
597+
if (vcpu_has_sve(vcpu) && guest_owns_fp_regs())
600598
val |= CPTR_EL2_TZ;
601599
if (cpus_have_final_cap(ARM64_SME))
602600
val &= ~CPTR_EL2_TSM;

arch/arm64/include/asm/kvm_host.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,12 @@ static inline bool guest_owns_fp_regs(void)
12131213
return *host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED;
12141214
}
12151215

1216+
/* Check whether the FP regs are owned by the host */
1217+
static inline bool host_owns_fp_regs(void)
1218+
{
1219+
return *host_data_ptr(fp_owner) == FP_STATE_HOST_OWNED;
1220+
}
1221+
12161222
static inline void kvm_init_host_cpu_context(struct kvm_cpu_context *cpu_ctxt)
12171223
{
12181224
/* The host's MPIDR is immutable, so let's set it up at boot time */

arch/arm64/kvm/fpsimd.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
141141

142142
WARN_ON_ONCE(!irqs_disabled());
143143

144-
if (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED) {
145-
144+
if (guest_owns_fp_regs()) {
146145
/*
147146
* Currently we do not support SME guests so SVCR is
148147
* 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)
195194
isb();
196195
}
197196

198-
if (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED) {
197+
if (guest_owns_fp_regs()) {
199198
if (vcpu_has_sve(vcpu)) {
200199
__vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
201200

arch/arm64/kvm/hyp/include/hyp/switch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
370370
isb();
371371

372372
/* Write out the host state if it's in the registers */
373-
if (*host_data_ptr(fp_owner) == FP_STATE_HOST_OWNED)
373+
if (host_owns_fp_regs())
374374
__fpsimd_save_state(*host_data_ptr(fpsimd_state));
375375

376376
/* Restore the guest state */

arch/arm64/kvm/hyp/nvhe/switch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
337337

338338
__sysreg_restore_state_nvhe(host_ctxt);
339339

340-
if (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED)
340+
if (guest_owns_fp_regs())
341341
__fpsimd_save_fpexc32(vcpu);
342342

343343
__debug_switch_to_host(vcpu);

arch/arm64/kvm/hyp/vhe/switch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ static int __kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu)
258258

259259
sysreg_restore_host_state_vhe(host_ctxt);
260260

261-
if (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED)
261+
if (guest_owns_fp_regs())
262262
__fpsimd_save_fpexc32(vcpu);
263263

264264
__debug_switch_to_host(vcpu);

0 commit comments

Comments
 (0)