1e6b673b7SDave Martin // SPDX-License-Identifier: GPL-2.0 2e6b673b7SDave Martin /* 3e6b673b7SDave Martin * arch/arm64/kvm/fpsimd.c: Guest/host FPSIMD context coordination helpers 4e6b673b7SDave Martin * 5e6b673b7SDave Martin * Copyright 2018 Arm Limited 6e6b673b7SDave Martin * Author: Dave Martin <Dave.Martin@arm.com> 7e6b673b7SDave Martin */ 8*b045e4d0SDave Martin #include <linux/irqflags.h> 9e6b673b7SDave Martin #include <linux/sched.h> 10e6b673b7SDave Martin #include <linux/thread_info.h> 11e6b673b7SDave Martin #include <linux/kvm_host.h> 12e6b673b7SDave Martin #include <asm/kvm_asm.h> 13e6b673b7SDave Martin #include <asm/kvm_host.h> 14e6b673b7SDave Martin #include <asm/kvm_mmu.h> 15e6b673b7SDave Martin 16e6b673b7SDave Martin /* 17e6b673b7SDave Martin * Called on entry to KVM_RUN unless this vcpu previously ran at least 18e6b673b7SDave Martin * once and the most recent prior KVM_RUN for this vcpu was called from 19e6b673b7SDave Martin * the same task as current (highly likely). 20e6b673b7SDave Martin * 21e6b673b7SDave Martin * This is guaranteed to execute before kvm_arch_vcpu_load_fp(vcpu), 22e6b673b7SDave Martin * such that on entering hyp the relevant parts of current are already 23e6b673b7SDave Martin * mapped. 24e6b673b7SDave Martin */ 25e6b673b7SDave Martin int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu) 26e6b673b7SDave Martin { 27e6b673b7SDave Martin int ret; 28e6b673b7SDave Martin 29e6b673b7SDave Martin struct thread_info *ti = ¤t->thread_info; 30e6b673b7SDave Martin struct user_fpsimd_state *fpsimd = ¤t->thread.uw.fpsimd_state; 31e6b673b7SDave Martin 32e6b673b7SDave Martin /* 33e6b673b7SDave Martin * Make sure the host task thread flags and fpsimd state are 34e6b673b7SDave Martin * visible to hyp: 35e6b673b7SDave Martin */ 36e6b673b7SDave Martin ret = create_hyp_mappings(ti, ti + 1, PAGE_HYP); 37e6b673b7SDave Martin if (ret) 38e6b673b7SDave Martin goto error; 39e6b673b7SDave Martin 40e6b673b7SDave Martin ret = create_hyp_mappings(fpsimd, fpsimd + 1, PAGE_HYP); 41e6b673b7SDave Martin if (ret) 42e6b673b7SDave Martin goto error; 43e6b673b7SDave Martin 44e6b673b7SDave Martin vcpu->arch.host_thread_info = kern_hyp_va(ti); 45e6b673b7SDave Martin vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd); 46e6b673b7SDave Martin error: 47e6b673b7SDave Martin return ret; 48e6b673b7SDave Martin } 49e6b673b7SDave Martin 50e6b673b7SDave Martin /* 51e6b673b7SDave Martin * Prepare vcpu for saving the host's FPSIMD state and loading the guest's. 52e6b673b7SDave Martin * The actual loading is done by the FPSIMD access trap taken to hyp. 53e6b673b7SDave Martin * 54e6b673b7SDave Martin * Here, we just set the correct metadata to indicate that the FPSIMD 55e6b673b7SDave Martin * state in the cpu regs (if any) belongs to current on the host. 56e6b673b7SDave Martin * 57e6b673b7SDave Martin * TIF_SVE is backed up here, since it may get clobbered with guest state. 58e6b673b7SDave Martin * This flag is restored by kvm_arch_vcpu_put_fp(vcpu). 59e6b673b7SDave Martin */ 60e6b673b7SDave Martin void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu) 61e6b673b7SDave Martin { 62e6b673b7SDave Martin BUG_ON(!current->mm); 63e6b673b7SDave Martin 64e6b673b7SDave Martin vcpu->arch.flags &= ~(KVM_ARM64_FP_ENABLED | KVM_ARM64_HOST_SVE_IN_USE); 65e6b673b7SDave Martin vcpu->arch.flags |= KVM_ARM64_FP_HOST; 66e6b673b7SDave Martin if (test_thread_flag(TIF_SVE)) 67e6b673b7SDave Martin vcpu->arch.flags |= KVM_ARM64_HOST_SVE_IN_USE; 68e6b673b7SDave Martin } 69e6b673b7SDave Martin 70e6b673b7SDave Martin /* 71e6b673b7SDave Martin * If the guest FPSIMD state was loaded, update the host's context 72e6b673b7SDave Martin * tracking data mark the CPU FPSIMD regs as dirty and belonging to vcpu 73e6b673b7SDave Martin * so that they will be written back if the kernel clobbers them due to 74e6b673b7SDave Martin * kernel-mode NEON before re-entry into the guest. 75e6b673b7SDave Martin */ 76e6b673b7SDave Martin void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu) 77e6b673b7SDave Martin { 78e6b673b7SDave Martin WARN_ON_ONCE(!irqs_disabled()); 79e6b673b7SDave Martin 80e6b673b7SDave Martin if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) { 81e6b673b7SDave Martin fpsimd_bind_state_to_cpu(&vcpu->arch.ctxt.gp_regs.fp_regs); 82e6b673b7SDave Martin clear_thread_flag(TIF_FOREIGN_FPSTATE); 83e6b673b7SDave Martin clear_thread_flag(TIF_SVE); 84e6b673b7SDave Martin } 85e6b673b7SDave Martin } 86e6b673b7SDave Martin 87e6b673b7SDave Martin /* 88e6b673b7SDave Martin * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the 89e6b673b7SDave Martin * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu 90e6b673b7SDave Martin * disappears and another task or vcpu appears that recycles the same 91e6b673b7SDave Martin * struct fpsimd_state. 92e6b673b7SDave Martin */ 93e6b673b7SDave Martin void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu) 94e6b673b7SDave Martin { 95*b045e4d0SDave Martin unsigned long flags; 96*b045e4d0SDave Martin 97*b045e4d0SDave Martin local_irq_save(flags); 98e6b673b7SDave Martin 99e6b673b7SDave Martin update_thread_flag(TIF_SVE, 100e6b673b7SDave Martin vcpu->arch.flags & KVM_ARM64_HOST_SVE_IN_USE); 101e6b673b7SDave Martin 102e6b673b7SDave Martin if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) { 103e6b673b7SDave Martin /* Clean guest FP state to memory and invalidate cpu view */ 104e6b673b7SDave Martin fpsimd_save(); 105e6b673b7SDave Martin fpsimd_flush_cpu_state(); 106e6b673b7SDave Martin } else if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) { 107e6b673b7SDave Martin /* Ensure user trap controls are correctly restored */ 108e6b673b7SDave Martin fpsimd_bind_task_to_cpu(); 109e6b673b7SDave Martin } 110e6b673b7SDave Martin 111*b045e4d0SDave Martin local_irq_restore(flags); 112e6b673b7SDave Martin } 113