Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __X86_KERNEL_FPU_CONTEXT_H
0003 #define __X86_KERNEL_FPU_CONTEXT_H
0004 
0005 #include <asm/fpu/xstate.h>
0006 #include <asm/trace/fpu.h>
0007 
0008 /* Functions related to FPU context tracking */
0009 
0010 /*
0011  * The in-register FPU state for an FPU context on a CPU is assumed to be
0012  * valid if the fpu->last_cpu matches the CPU, and the fpu_fpregs_owner_ctx
0013  * matches the FPU.
0014  *
0015  * If the FPU register state is valid, the kernel can skip restoring the
0016  * FPU state from memory.
0017  *
0018  * Any code that clobbers the FPU registers or updates the in-memory
0019  * FPU state for a task MUST let the rest of the kernel know that the
0020  * FPU registers are no longer valid for this task.
0021  *
0022  * Either one of these invalidation functions is enough. Invalidate
0023  * a resource you control: CPU if using the CPU for something else
0024  * (with preemption disabled), FPU for the current task, or a task that
0025  * is prevented from running by the current task.
0026  */
0027 static inline void __cpu_invalidate_fpregs_state(void)
0028 {
0029     __this_cpu_write(fpu_fpregs_owner_ctx, NULL);
0030 }
0031 
0032 static inline void __fpu_invalidate_fpregs_state(struct fpu *fpu)
0033 {
0034     fpu->last_cpu = -1;
0035 }
0036 
0037 static inline int fpregs_state_valid(struct fpu *fpu, unsigned int cpu)
0038 {
0039     return fpu == this_cpu_read(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
0040 }
0041 
0042 static inline void fpregs_deactivate(struct fpu *fpu)
0043 {
0044     __this_cpu_write(fpu_fpregs_owner_ctx, NULL);
0045     trace_x86_fpu_regs_deactivated(fpu);
0046 }
0047 
0048 static inline void fpregs_activate(struct fpu *fpu)
0049 {
0050     __this_cpu_write(fpu_fpregs_owner_ctx, fpu);
0051     trace_x86_fpu_regs_activated(fpu);
0052 }
0053 
0054 /* Internal helper for switch_fpu_return() and signal frame setup */
0055 static inline void fpregs_restore_userregs(void)
0056 {
0057     struct fpu *fpu = &current->thread.fpu;
0058     int cpu = smp_processor_id();
0059 
0060     if (WARN_ON_ONCE(current->flags & PF_KTHREAD))
0061         return;
0062 
0063     if (!fpregs_state_valid(fpu, cpu)) {
0064         /*
0065          * This restores _all_ xstate which has not been
0066          * established yet.
0067          *
0068          * If PKRU is enabled, then the PKRU value is already
0069          * correct because it was either set in switch_to() or in
0070          * flush_thread(). So it is excluded because it might be
0071          * not up to date in current->thread.fpu.xsave state.
0072          *
0073          * XFD state is handled in restore_fpregs_from_fpstate().
0074          */
0075         restore_fpregs_from_fpstate(fpu->fpstate, XFEATURE_MASK_FPSTATE);
0076 
0077         fpregs_activate(fpu);
0078         fpu->last_cpu = cpu;
0079     }
0080     clear_thread_flag(TIF_NEED_FPU_LOAD);
0081 }
0082 
0083 #endif