1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_FPU_SCHED_H
3 #define _ASM_X86_FPU_SCHED_H
4
5 #include <linux/sched.h>
6
7 #include <asm/cpufeature.h>
8 #include <asm/fpu/types.h>
9
10 #include <asm/trace/fpu.h>
11
12 extern void save_fpregs_to_fpstate(struct fpu *fpu);
13 extern void fpu__drop(struct task_struct *tsk);
14 extern int fpu_clone(struct task_struct *dst, unsigned long clone_flags, bool minimal,
15 unsigned long shstk_addr);
16 extern void fpu_flush_thread(void);
17
18 /*
19 * FPU state switching for scheduling.
20 *
21 * switch_fpu() saves the old state and sets TIF_NEED_FPU_LOAD if
22 * TIF_NEED_FPU_LOAD is not set. This is done within the context
23 * of the old process.
24 *
25 * Once TIF_NEED_FPU_LOAD is set, it is required to load the
26 * registers before returning to userland or using the content
27 * otherwise.
28 *
29 * The FPU context is only stored/restored for a user task and
30 * PF_KTHREAD is used to distinguish between kernel and user threads.
31 */
switch_fpu(struct task_struct * old,int cpu)32 static inline void switch_fpu(struct task_struct *old, int cpu)
33 {
34 if (!test_tsk_thread_flag(old, TIF_NEED_FPU_LOAD) &&
35 cpu_feature_enabled(X86_FEATURE_FPU) &&
36 !(old->flags & (PF_KTHREAD | PF_USER_WORKER))) {
37 struct fpu *old_fpu = x86_task_fpu(old);
38
39 set_tsk_thread_flag(old, TIF_NEED_FPU_LOAD);
40 save_fpregs_to_fpstate(old_fpu);
41 /*
42 * The save operation preserved register state, so the
43 * fpu_fpregs_owner_ctx is still @old_fpu. Store the
44 * current CPU number in @old_fpu, so the next return
45 * to user space can avoid the FPU register restore
46 * when is returns on the same CPU and still owns the
47 * context. See fpregs_restore_userregs().
48 */
49 old_fpu->last_cpu = cpu;
50
51 trace_x86_fpu_regs_deactivated(old_fpu);
52 }
53 }
54
55 #endif /* _ASM_X86_FPU_SCHED_H */
56