Skip to content

Commit eef4ce6

Browse files
Fuad TabbaMarc Zyngier
authored andcommitted
KVM: arm64: Clarify rationale for ZCR_EL1 value restored on guest exit
Expand comment clarifying why the host value representing SVE vector length being restored for ZCR_EL1 on guest exit isn't the same as it was on guest entry. 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-21-tabba@google.com Signed-off-by: Marc Zyngier <maz@kernel.org>
1 parent b6ed4fa commit eef4ce6

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

arch/arm64/kvm/fpsimd.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,34 @@ void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
175175
if (vcpu_has_sve(vcpu)) {
176176
__vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
177177

178-
/* Restore the VL that was saved when bound to the CPU */
178+
/*
179+
* Restore the VL that was saved when bound to the CPU,
180+
* which is the maximum VL for the guest. Because the
181+
* layout of the data when saving the sve state depends
182+
* on the VL, we need to use a consistent (i.e., the
183+
* maximum) VL.
184+
* Note that this means that at guest exit ZCR_EL1 is
185+
* not necessarily the same as on guest entry.
186+
*
187+
* Restoring the VL isn't needed in VHE mode since
188+
* ZCR_EL2 (accessed via ZCR_EL1) would fulfill the same
189+
* role when doing the save from EL2.
190+
*/
179191
if (!has_vhe())
180192
sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1,
181193
SYS_ZCR_EL1);
182194
}
183195

196+
/*
197+
* Flush (save and invalidate) the fpsimd/sve state so that if
198+
* the host tries to use fpsimd/sve, it's not using stale data
199+
* from the guest.
200+
*
201+
* Flushing the state sets the TIF_FOREIGN_FPSTATE bit for the
202+
* context unconditionally, in both nVHE and VHE. This allows
203+
* the kernel to restore the fpsimd/sve state, including ZCR_EL1
204+
* when needed.
205+
*/
184206
fpsimd_save_and_flush_cpu_state();
185207
} else if (has_vhe() && system_supports_sve()) {
186208
/*

0 commit comments

Comments
 (0)