1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_FPU_SCHED_H
3 #define _ASM_X86_FPU_SCHED_H
5 #include <linux/sched.h>
7 #include <asm/cpufeature.h>
8 #include <asm/fpu/types.h>
10 #include <asm/trace/fpu.h>
12 extern void save_fpregs_to_fpstate(struct fpu *fpu);
13 extern void fpu__drop(struct fpu *fpu);
14 extern int fpu_clone(struct task_struct *dst, unsigned long clone_flags, bool minimal);
15 extern void fpu_flush_thread(void);
18 * FPU state switching for scheduling.
20 * This is a two-stage process:
22 * - switch_fpu_prepare() saves the old state.
23 * This is done within the context of the old process.
25 * - switch_fpu_finish() sets TIF_NEED_FPU_LOAD; the floating point state
26 * will get loaded on return to userspace, or when the kernel needs it.
28 * If TIF_NEED_FPU_LOAD is cleared then the CPU's FPU registers
29 * are saved in the current thread's FPU register state.
31 * If TIF_NEED_FPU_LOAD is set then CPU's FPU registers may not
32 * hold current()'s FPU registers. It is required to load the
33 * registers before returning to userland or using the content
36 * The FPU context is only stored/restored for a user task and
37 * PF_KTHREAD is used to distinguish between kernel and user threads.
39 static inline void switch_fpu_prepare(struct fpu *old_fpu, int cpu)
41 if (cpu_feature_enabled(X86_FEATURE_FPU) &&
42 !(current->flags & PF_KTHREAD)) {
43 save_fpregs_to_fpstate(old_fpu);
45 * The save operation preserved register state, so the
46 * fpu_fpregs_owner_ctx is still @old_fpu. Store the
47 * current CPU number in @old_fpu, so the next return
48 * to user space can avoid the FPU register restore
49 * when is returns on the same CPU and still owns the
52 old_fpu->last_cpu = cpu;
54 trace_x86_fpu_regs_deactivated(old_fpu);
59 * Delay loading of the complete FPU state until the return to userland.
60 * PKRU is handled separately.
62 static inline void switch_fpu_finish(void)
64 if (cpu_feature_enabled(X86_FEATURE_FPU))
65 set_thread_flag(TIF_NEED_FPU_LOAD);
68 #endif /* _ASM_X86_FPU_SCHED_H */