1 // SPDX-License-Identifier: GPL-2.0
3 * arch/arm64/kvm/fpsimd.c: Guest/host FPSIMD context coordination helpers
5 * Copyright 2018 Arm Limited
6 * Author: Dave Martin <Dave.Martin@arm.com>
8 #include <linux/irqflags.h>
9 #include <linux/sched.h>
10 #include <linux/thread_info.h>
11 #include <linux/kvm_host.h>
12 #include <asm/fpsimd.h>
13 #include <asm/kvm_asm.h>
14 #include <asm/kvm_hyp.h>
15 #include <asm/kvm_mmu.h>
16 #include <asm/sysreg.h>
19 * Called on entry to KVM_RUN unless this vcpu previously ran at least
20 * once and the most recent prior KVM_RUN for this vcpu was called from
21 * the same task as current (highly likely).
23 * This is guaranteed to execute before kvm_arch_vcpu_load_fp(vcpu),
24 * such that on entering hyp the relevant parts of current are already
27 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
31 struct thread_info *ti = ¤t->thread_info;
32 struct user_fpsimd_state *fpsimd = ¤t->thread.uw.fpsimd_state;
35 * Make sure the host task thread flags and fpsimd state are
38 ret = create_hyp_mappings(ti, ti + 1, PAGE_HYP);
42 ret = create_hyp_mappings(fpsimd, fpsimd + 1, PAGE_HYP);
46 if (vcpu->arch.sve_state) {
49 sve_end = vcpu->arch.sve_state + vcpu_sve_state_size(vcpu);
51 ret = create_hyp_mappings(vcpu->arch.sve_state, sve_end,
57 vcpu->arch.host_thread_info = kern_hyp_va(ti);
58 vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd);
64 * Prepare vcpu for saving the host's FPSIMD state and loading the guest's.
65 * The actual loading is done by the FPSIMD access trap taken to hyp.
67 * Here, we just set the correct metadata to indicate that the FPSIMD
68 * state in the cpu regs (if any) belongs to current on the host.
70 * TIF_SVE is backed up here, since it may get clobbered with guest state.
71 * This flag is restored by kvm_arch_vcpu_put_fp(vcpu).
73 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
77 vcpu->arch.flags &= ~(KVM_ARM64_FP_ENABLED |
78 KVM_ARM64_HOST_SVE_IN_USE |
79 KVM_ARM64_HOST_SVE_ENABLED);
80 vcpu->arch.flags |= KVM_ARM64_FP_HOST;
82 if (test_thread_flag(TIF_SVE))
83 vcpu->arch.flags |= KVM_ARM64_HOST_SVE_IN_USE;
85 if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
86 vcpu->arch.flags |= KVM_ARM64_HOST_SVE_ENABLED;
90 * If the guest FPSIMD state was loaded, update the host's context
91 * tracking data mark the CPU FPSIMD regs as dirty and belonging to vcpu
92 * so that they will be written back if the kernel clobbers them due to
93 * kernel-mode NEON before re-entry into the guest.
95 void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
97 WARN_ON_ONCE(!irqs_disabled());
99 if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
100 fpsimd_bind_state_to_cpu(&vcpu->arch.ctxt.fp_regs,
101 vcpu->arch.sve_state,
102 vcpu->arch.sve_max_vl);
104 clear_thread_flag(TIF_FOREIGN_FPSTATE);
105 update_thread_flag(TIF_SVE, vcpu_has_sve(vcpu));
110 * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the
111 * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu
112 * disappears and another task or vcpu appears that recycles the same
113 * struct fpsimd_state.
115 void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
118 bool host_has_sve = system_supports_sve();
119 bool guest_has_sve = vcpu_has_sve(vcpu);
121 local_irq_save(flags);
123 if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
125 __vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
127 /* Restore the VL that was saved when bound to the CPU */
129 sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1,
133 fpsimd_save_and_flush_cpu_state();
134 } else if (has_vhe() && host_has_sve) {
136 * The FPSIMD/SVE state in the CPU has not been touched, and we
137 * have SVE (and VHE): CPACR_EL1 (alias CPTR_EL2) has been
138 * reset to CPACR_EL1_DEFAULT by the Hyp code, disabling SVE
139 * for EL0. To avoid spurious traps, restore the trap state
140 * seen by kvm_arch_vcpu_load_fp():
142 if (vcpu->arch.flags & KVM_ARM64_HOST_SVE_ENABLED)
143 sysreg_clear_set(CPACR_EL1, 0, CPACR_EL1_ZEN_EL0EN);
145 sysreg_clear_set(CPACR_EL1, CPACR_EL1_ZEN_EL0EN, 0);
148 update_thread_flag(TIF_SVE,
149 vcpu->arch.flags & KVM_ARM64_HOST_SVE_IN_USE);
151 local_irq_restore(flags);