Skip to content

Commit ef3be86

Browse files
author
Marc Zyngier
committed
KVM: arm64: Add save/restore support for FPMR
Just like the rest of the FP/SIMD state, FPMR needs to be context switched. The only interesting thing here is that we need to treat the pKVM part a bit differently, as the host FP state is never written back to the vcpu thread, but instead stored locally and eagerly restored. Reviewed-by: Mark Brown <[email protected]> Tested-by: Mark Brown <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Marc Zyngier <[email protected]>
1 parent 7d9c1ed commit ef3be86

File tree

6 files changed

+35
-0
lines changed

6 files changed

+35
-0
lines changed

arch/arm64/include/asm/kvm_host.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,16 @@ struct kvm_host_data {
599599
struct cpu_sve_state *sve_state;
600600
};
601601

602+
union {
603+
/* HYP VA pointer to the host storage for FPMR */
604+
u64 *fpmr_ptr;
605+
/*
606+
* Used by pKVM only, as it needs to provide storage
607+
* for the host
608+
*/
609+
u64 fpmr;
610+
};
611+
602612
/* Ownership of the FP regs */
603613
enum {
604614
FP_STATE_FREE,

arch/arm64/kvm/fpsimd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
6363
*/
6464
*host_data_ptr(fp_owner) = FP_STATE_HOST_OWNED;
6565
*host_data_ptr(fpsimd_state) = kern_hyp_va(&current->thread.uw.fpsimd_state);
66+
*host_data_ptr(fpmr_ptr) = kern_hyp_va(&current->thread.uw.fpmr);
6667

6768
vcpu_clear_flag(vcpu, HOST_SVE_ENABLED);
6869
if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
404404
else
405405
__fpsimd_restore_state(&vcpu->arch.ctxt.fp_regs);
406406

407+
if (kvm_has_fpmr(kern_hyp_va(vcpu->kvm)))
408+
write_sysreg_s(__vcpu_sys_reg(vcpu, FPMR), SYS_FPMR);
409+
407410
/* Skip restoring fpexc32 for AArch64 guests */
408411
if (!(read_sysreg(hcr_el2) & HCR_RW))
409412
write_sysreg(__vcpu_sys_reg(vcpu, FPEXC32_EL2), fpexc32_el2);

arch/arm64/kvm/hyp/nvhe/hyp-main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ static void fpsimd_sve_flush(void)
6262

6363
static void fpsimd_sve_sync(struct kvm_vcpu *vcpu)
6464
{
65+
bool has_fpmr;
66+
6567
if (!guest_owns_fp_regs())
6668
return;
6769

@@ -73,11 +75,18 @@ static void fpsimd_sve_sync(struct kvm_vcpu *vcpu)
7375
else
7476
__fpsimd_save_state(&vcpu->arch.ctxt.fp_regs);
7577

78+
has_fpmr = kvm_has_fpmr(kern_hyp_va(vcpu->kvm));
79+
if (has_fpmr)
80+
__vcpu_sys_reg(vcpu, FPMR) = read_sysreg_s(SYS_FPMR);
81+
7682
if (system_supports_sve())
7783
__hyp_sve_restore_host();
7884
else
7985
__fpsimd_restore_state(*host_data_ptr(fpsimd_state));
8086

87+
if (has_fpmr)
88+
write_sysreg_s(*host_data_ptr(fpmr), SYS_FPMR);
89+
8190
*host_data_ptr(fp_owner) = FP_STATE_HOST_OWNED;
8291
}
8392

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu)
198198
} else {
199199
__fpsimd_save_state(*host_data_ptr(fpsimd_state));
200200
}
201+
202+
if (kvm_has_fpmr(kern_hyp_va(vcpu->kvm))) {
203+
u64 val = read_sysreg_s(SYS_FPMR);
204+
205+
if (unlikely(is_protected_kvm_enabled()))
206+
*host_data_ptr(fpmr) = val;
207+
else
208+
**host_data_ptr(fpmr_ptr) = val;
209+
}
201210
}
202211

203212
static const exit_handler_fn hyp_exit_handlers[] = {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ static bool kvm_hyp_handle_eret(struct kvm_vcpu *vcpu, u64 *exit_code)
312312
static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu)
313313
{
314314
__fpsimd_save_state(*host_data_ptr(fpsimd_state));
315+
316+
if (kvm_has_fpmr(vcpu->kvm))
317+
**host_data_ptr(fpmr_ptr) = read_sysreg_s(SYS_FPMR);
315318
}
316319

317320
static bool kvm_hyp_handle_tlbi_el2(struct kvm_vcpu *vcpu, u64 *exit_code)

0 commit comments

Comments
 (0)