blob: 5a18694a89b24d64701766f0f0447c89c93cf6dd [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1361b832012-02-21 13:19:22 -08002/*
3 * Copyright (C) 1994 Linus Torvalds
4 *
5 * Pentium III FXSR, SSE support
6 * General FPU state handling cleanups
7 * Gareth Hughes <gareth@valinux.com>, May 2000
8 * x86-64 work by Andi Kleen 2002
9 */
10
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020011#ifndef _ASM_X86_FPU_INTERNAL_H
12#define _ASM_X86_FPU_INTERNAL_H
Linus Torvalds1361b832012-02-21 13:19:22 -080013
Suresh Siddha050902c2012-07-24 16:05:27 -070014#include <linux/compat.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020015#include <linux/sched.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080016#include <linux/slab.h>
Rik van Riel0cecca92019-04-03 18:41:44 +020017#include <linux/mm.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020018
Linus Torvalds1361b832012-02-21 13:19:22 -080019#include <asm/user.h>
Ingo Molnardf6b35f2015-04-24 02:46:00 +020020#include <asm/fpu/api.h>
Ingo Molnar669ebab2015-04-28 08:41:33 +020021#include <asm/fpu/xstate.h>
Joerg Roedel1b4fb852020-09-07 15:15:27 +020022#include <asm/fpu/xcr.h>
Borislav Petkovcd4d09e2016-01-26 22:12:04 +010023#include <asm/cpufeature.h>
Dave Hansend1898b72016-06-01 10:42:20 -070024#include <asm/trace/fpu.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080025
Ingo Molnar6ffc1522015-04-29 20:24:14 +020026/*
27 * High level FPU state handling functions:
28 */
Ingo Molnar82c0e452015-04-29 21:09:18 +020029extern int fpu__restore_sig(void __user *buf, int ia32_frame);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020030extern void fpu__drop(struct fpu *fpu);
Fenghua Yub860eb82020-05-12 07:54:39 -070031extern void fpu__clear_user_states(struct fpu *fpu);
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020032extern int fpu__exception_code(struct fpu *fpu, int trap_nr);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020033
Thomas Gleixnerb2681e72021-06-23 14:02:06 +020034extern void fpu_sync_fpstate(struct fpu *fpu);
35
Thomas Gleixnere7ecad12021-06-23 14:02:12 +020036/* Clone and exit operations */
Thomas Gleixnerb2681e72021-06-23 14:02:06 +020037extern int fpu_clone(struct task_struct *dst);
Thomas Gleixnere7ecad12021-06-23 14:02:12 +020038extern void fpu_flush_thread(void);
Thomas Gleixnerb2681e72021-06-23 14:02:06 +020039
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020040/*
41 * Boot time FPU initialization functions:
42 */
43extern void fpu__init_cpu(void);
44extern void fpu__init_system_xstate(void);
45extern void fpu__init_cpu_xstate(void);
46extern void fpu__init_system(struct cpuinfo_x86 *c);
Ingo Molnar952f07e2015-04-26 16:56:05 +020047extern void fpu__init_check_bugs(void);
48extern void fpu__resume_cpu(void);
49
Ingo Molnare97131a2015-05-05 11:34:49 +020050/*
51 * Debugging facility:
52 */
53#ifdef CONFIG_X86_DEBUG_FPU
54# define WARN_ON_FPU(x) WARN_ON_ONCE(x)
55#else
Ingo Molnar83242c52015-05-27 12:22:29 +020056# define WARN_ON_FPU(x) ({ (void)(x); 0; })
Ingo Molnare97131a2015-05-05 11:34:49 +020057#endif
58
Rik van Riel1c927ee2015-02-06 15:02:01 -050059/*
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020060 * FPU related CPU feature flag helper routines:
Rik van Riel1c927ee2015-02-06 15:02:01 -050061 */
Linus Torvalds1361b832012-02-21 13:19:22 -080062static __always_inline __pure bool use_xsaveopt(void)
63{
Borislav Petkovbc696ca2016-01-26 22:12:05 +010064 return static_cpu_has(X86_FEATURE_XSAVEOPT);
Linus Torvalds1361b832012-02-21 13:19:22 -080065}
66
67static __always_inline __pure bool use_xsave(void)
68{
Borislav Petkovbc696ca2016-01-26 22:12:05 +010069 return static_cpu_has(X86_FEATURE_XSAVE);
Linus Torvalds1361b832012-02-21 13:19:22 -080070}
71
72static __always_inline __pure bool use_fxsr(void)
73{
Borislav Petkovbc696ca2016-01-26 22:12:05 +010074 return static_cpu_has(X86_FEATURE_FXSR);
Linus Torvalds1361b832012-02-21 13:19:22 -080075}
76
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020077/*
78 * fpstate handling functions:
79 */
80
81extern union fpregs_state init_fpstate;
82
83extern void fpstate_init(union fpregs_state *state);
84#ifdef CONFIG_MATH_EMULATION
85extern void fpstate_init_soft(struct swregs_state *soft);
86#else
87static inline void fpstate_init_soft(struct swregs_state *soft) {}
88#endif
Thomas Gleixner08ded2c2021-06-23 14:02:00 +020089extern void save_fpregs_to_fpstate(struct fpu *fpu);
Yu-cheng Yua5828ed2017-01-24 10:25:46 -080090
Thomas Gleixneraee8c672021-06-23 14:02:30 +020091/* Returns 0 or the negated trap number, which results in -EFAULT for #PF */
H. Peter Anvin49b8c6952012-09-21 17:18:44 -070092#define user_insn(insn, output, input...) \
93({ \
94 int err; \
Sebastian Andrzej Siewior66374012018-11-28 23:20:11 +010095 \
96 might_fault(); \
97 \
H. Peter Anvin49b8c6952012-09-21 17:18:44 -070098 asm volatile(ASM_STAC "\n" \
Thomas Gleixneraee8c672021-06-23 14:02:30 +020099 "1: " #insn "\n" \
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700100 "2: " ASM_CLAC "\n" \
101 ".section .fixup,\"ax\"\n" \
Thomas Gleixneraee8c672021-06-23 14:02:30 +0200102 "3: negl %%eax\n" \
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700103 " jmp 2b\n" \
104 ".previous\n" \
Thomas Gleixneraee8c672021-06-23 14:02:30 +0200105 _ASM_EXTABLE_FAULT(1b, 3b) \
106 : [err] "=a" (err), output \
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700107 : "0"(0), input); \
108 err; \
109})
Linus Torvalds1361b832012-02-21 13:19:22 -0800110
Sebastian Andrzej Siewior926b21f2019-04-03 18:41:50 +0200111#define kernel_insn_err(insn, output, input...) \
112({ \
113 int err; \
114 asm volatile("1:" #insn "\n\t" \
115 "2:\n" \
116 ".section .fixup,\"ax\"\n" \
117 "3: movl $-1,%[err]\n" \
118 " jmp 2b\n" \
119 ".previous\n" \
120 _ASM_EXTABLE(1b, 3b) \
121 : [err] "=r" (err), output \
122 : "0"(0), input); \
123 err; \
124})
125
Eric Biggersd5c80282017-09-23 15:00:09 +0200126#define kernel_insn(insn, output, input...) \
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700127 asm volatile("1:" #insn "\n\t" \
128 "2:\n" \
Eric Biggersd5c80282017-09-23 15:00:09 +0200129 _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_fprestore) \
130 : output : input)
Linus Torvalds1361b832012-02-21 13:19:22 -0800131
Thomas Gleixner6fdc908c2021-06-23 14:01:56 +0200132static inline int fnsave_to_user_sigframe(struct fregs_state __user *fx)
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700133{
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700134 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800135}
136
Thomas Gleixner16dcf432021-06-23 14:01:54 +0200137static inline int fxsave_to_user_sigframe(struct fxregs_state __user *fx)
Linus Torvalds1361b832012-02-21 13:19:22 -0800138{
Masahiro Yamada97f26452016-08-03 13:45:50 -0700139 if (IS_ENABLED(CONFIG_X86_32))
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700140 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Borislav Petkovbae54dc2019-01-18 00:05:40 +0100141 else
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700142 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800143
Linus Torvalds1361b832012-02-21 13:19:22 -0800144}
145
Thomas Gleixner16dcf432021-06-23 14:01:54 +0200146static inline void fxrstor(struct fxregs_state *fx)
Linus Torvalds1361b832012-02-21 13:19:22 -0800147{
Borislav Petkovbae54dc2019-01-18 00:05:40 +0100148 if (IS_ENABLED(CONFIG_X86_32))
Eric Biggersd5c80282017-09-23 15:00:09 +0200149 kernel_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Borislav Petkovbae54dc2019-01-18 00:05:40 +0100150 else
151 kernel_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700152}
153
Thomas Gleixner16dcf432021-06-23 14:01:54 +0200154static inline int fxrstor_safe(struct fxregs_state *fx)
Sebastian Andrzej Siewior926b21f2019-04-03 18:41:50 +0200155{
156 if (IS_ENABLED(CONFIG_X86_32))
157 return kernel_insn_err(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
158 else
159 return kernel_insn_err(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
160}
161
Thomas Gleixner16dcf432021-06-23 14:01:54 +0200162static inline int fxrstor_from_user_sigframe(struct fxregs_state __user *fx)
H. Peter Anvine139e952012-09-25 15:42:18 -0700163{
Masahiro Yamada97f26452016-08-03 13:45:50 -0700164 if (IS_ENABLED(CONFIG_X86_32))
H. Peter Anvine139e952012-09-25 15:42:18 -0700165 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Borislav Petkovbae54dc2019-01-18 00:05:40 +0100166 else
H. Peter Anvine139e952012-09-25 15:42:18 -0700167 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
H. Peter Anvine139e952012-09-25 15:42:18 -0700168}
169
Thomas Gleixner6fdc908c2021-06-23 14:01:56 +0200170static inline void frstor(struct fregs_state *fx)
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700171{
Eric Biggersd5c80282017-09-23 15:00:09 +0200172 kernel_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800173}
174
Thomas Gleixner6fdc908c2021-06-23 14:01:56 +0200175static inline int frstor_safe(struct fregs_state *fx)
Sebastian Andrzej Siewior926b21f2019-04-03 18:41:50 +0200176{
177 return kernel_insn_err(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
178}
179
Thomas Gleixner6fdc908c2021-06-23 14:01:56 +0200180static inline int frstor_from_user_sigframe(struct fregs_state __user *fx)
H. Peter Anvine139e952012-09-25 15:42:18 -0700181{
182 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
183}
184
Thomas Gleixnerf9dfb5e2021-06-18 16:18:25 +0200185static inline void fxsave(struct fxregs_state *fx)
186{
187 if (IS_ENABLED(CONFIG_X86_32))
188 asm volatile( "fxsave %[fx]" : [fx] "=m" (*fx));
189 else
190 asm volatile("fxsaveq %[fx]" : [fx] "=m" (*fx));
191}
192
Ingo Molnarfd169b02015-05-25 09:55:39 +0200193/* These macros all use (%edi)/(%rdi) as the single memory argument. */
194#define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
195#define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
196#define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
197#define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
198#define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
199
Thomas Gleixneraee8c672021-06-23 14:02:30 +0200200/*
201 * After this @err contains 0 on success or the negated trap number when
202 * the operation raises an exception. For faults this results in -EFAULT.
203 */
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100204#define XSTATE_OP(op, st, lmask, hmask, err) \
205 asm volatile("1:" op "\n\t" \
206 "xor %[err], %[err]\n" \
207 "2:\n\t" \
208 ".pushsection .fixup,\"ax\"\n\t" \
Thomas Gleixneraee8c672021-06-23 14:02:30 +0200209 "3: negl %%eax\n\t" \
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100210 "jmp 2b\n\t" \
211 ".popsection\n\t" \
Thomas Gleixneraee8c672021-06-23 14:02:30 +0200212 _ASM_EXTABLE_FAULT(1b, 3b) \
213 : [err] "=a" (err) \
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100214 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
215 : "memory")
216
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100217/*
218 * If XSAVES is enabled, it replaces XSAVEOPT because it supports a compact
219 * format and supervisor states in addition to modified optimization in
220 * XSAVEOPT.
221 *
222 * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
223 * supports modified optimization which is not supported by XSAVE.
224 *
225 * We use XSAVE as a fallback.
226 *
227 * The 661 label is defined in the ALTERNATIVE* macros as the address of the
228 * original instruction which gets replaced. We need to use it here as the
229 * address of the instruction where we might get an exception at.
230 */
231#define XSTATE_XSAVE(st, lmask, hmask, err) \
232 asm volatile(ALTERNATIVE_2(XSAVE, \
233 XSAVEOPT, X86_FEATURE_XSAVEOPT, \
234 XSAVES, X86_FEATURE_XSAVES) \
235 "\n" \
236 "xor %[err], %[err]\n" \
237 "3:\n" \
238 ".pushsection .fixup,\"ax\"\n" \
239 "4: movl $-2, %[err]\n" \
240 "jmp 3b\n" \
241 ".popsection\n" \
242 _ASM_EXTABLE(661b, 4b) \
243 : [err] "=r" (err) \
244 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
245 : "memory")
246
247/*
248 * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
249 * XSAVE area format.
250 */
Eric Biggersd5c80282017-09-23 15:00:09 +0200251#define XSTATE_XRESTORE(st, lmask, hmask) \
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100252 asm volatile(ALTERNATIVE(XRSTOR, \
253 XRSTORS, X86_FEATURE_XSAVES) \
254 "\n" \
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100255 "3:\n" \
Eric Biggersd5c80282017-09-23 15:00:09 +0200256 _ASM_EXTABLE_HANDLE(661b, 3b, ex_handler_fprestore)\
257 : \
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100258 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
259 : "memory")
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100260
Ingo Molnarfd169b02015-05-25 09:55:39 +0200261/*
262 * This function is called only during boot time when x86 caps are not set
263 * up and alternative can not be used yet.
264 */
Thomas Gleixnerb16313f2021-06-23 14:01:52 +0200265static inline void os_xrstor_booting(struct xregs_state *xstate)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200266{
Thomas Gleixner30a304a2021-06-23 14:02:20 +0200267 u64 mask = xfeatures_mask_fpstate();
Ingo Molnarfd169b02015-05-25 09:55:39 +0200268 u32 lmask = mask;
269 u32 hmask = mask >> 32;
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100270 int err;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200271
272 WARN_ON(system_state != SYSTEM_BOOTING);
273
Borislav Petkov67e87d42019-03-29 19:52:59 +0100274 if (boot_cpu_has(X86_FEATURE_XSAVES))
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100275 XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200276 else
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100277 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
Ingo Molnar8c05f052015-05-24 09:23:25 +0200278
Eric Biggersd5c80282017-09-23 15:00:09 +0200279 /*
280 * We should never fault when copying from a kernel buffer, and the FPU
281 * state we set at boot time should be valid.
282 */
Ingo Molnar8c05f052015-05-24 09:23:25 +0200283 WARN_ON_FPU(err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200284}
285
286/*
287 * Save processor xstate to xsave area.
Thomas Gleixnerb16313f2021-06-23 14:01:52 +0200288 *
289 * Uses either XSAVE or XSAVEOPT or XSAVES depending on the CPU features
290 * and command line options. The choice is permanent until the next reboot.
Ingo Molnarfd169b02015-05-25 09:55:39 +0200291 */
Thomas Gleixnerb16313f2021-06-23 14:01:52 +0200292static inline void os_xsave(struct xregs_state *xstate)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200293{
Kan Lianga063bf22020-07-03 05:49:25 -0700294 u64 mask = xfeatures_mask_all;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200295 u32 lmask = mask;
296 u32 hmask = mask >> 32;
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100297 int err;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200298
Andi Kleen03eaec82017-09-23 15:00:06 +0200299 WARN_ON_FPU(!alternatives_patched);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200300
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100301 XSTATE_XSAVE(xstate, lmask, hmask, err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200302
Ingo Molnar8c05f052015-05-24 09:23:25 +0200303 /* We should never fault when copying to a kernel buffer: */
304 WARN_ON_FPU(err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200305}
306
307/*
308 * Restore processor xstate from xsave area.
Thomas Gleixnerb16313f2021-06-23 14:01:52 +0200309 *
310 * Uses XRSTORS when XSAVES is used, XRSTOR otherwise.
Ingo Molnarfd169b02015-05-25 09:55:39 +0200311 */
Thomas Gleixnerb16313f2021-06-23 14:01:52 +0200312static inline void os_xrstor(struct xregs_state *xstate, u64 mask)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200313{
Ingo Molnarfd169b02015-05-25 09:55:39 +0200314 u32 lmask = mask;
315 u32 hmask = mask >> 32;
316
Eric Biggersd5c80282017-09-23 15:00:09 +0200317 XSTATE_XRESTORE(xstate, lmask, hmask);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200318}
319
320/*
321 * Save xstate to user space xsave area.
322 *
323 * We don't use modified optimization because xrstor/xrstors might track
324 * a different application.
325 *
326 * We don't use compacted format xsave area for
327 * backward compatibility for old applications which don't understand
328 * compacted format of xsave area.
329 */
Thomas Gleixner6b862ba2021-06-23 14:01:53 +0200330static inline int xsave_to_user_sigframe(struct xregs_state __user *buf)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200331{
Thomas Gleixner65e95212021-06-23 14:02:16 +0200332 /*
333 * Include the features which are not xsaved/rstored by the kernel
334 * internally, e.g. PKRU. That's user space ABI and also required
335 * to allow the signal handler to modify PKRU.
336 */
337 u64 mask = xfeatures_mask_uabi();
Kan Lianga063bf22020-07-03 05:49:25 -0700338 u32 lmask = mask;
339 u32 hmask = mask >> 32;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200340 int err;
341
342 /*
343 * Clear the xsave header first, so that reserved fields are
344 * initialized to zero.
345 */
346 err = __clear_user(&buf->header, sizeof(buf->header));
347 if (unlikely(err))
348 return -EFAULT;
349
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100350 stac();
Kan Lianga063bf22020-07-03 05:49:25 -0700351 XSTATE_OP(XSAVE, buf, lmask, hmask, err);
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100352 clac();
353
Ingo Molnarfd169b02015-05-25 09:55:39 +0200354 return err;
355}
356
357/*
358 * Restore xstate from user space xsave area.
359 */
Thomas Gleixner6b862ba2021-06-23 14:01:53 +0200360static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64 mask)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200361{
Ingo Molnarfd169b02015-05-25 09:55:39 +0200362 struct xregs_state *xstate = ((__force struct xregs_state *)buf);
363 u32 lmask = mask;
364 u32 hmask = mask >> 32;
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100365 int err;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200366
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100367 stac();
368 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
369 clac();
370
Ingo Molnarfd169b02015-05-25 09:55:39 +0200371 return err;
372}
373
Linus Torvalds1361b832012-02-21 13:19:22 -0800374/*
Sebastian Andrzej Siewior926b21f2019-04-03 18:41:50 +0200375 * Restore xstate from kernel space xsave area, return an error code instead of
376 * an exception.
377 */
Thomas Gleixnerb16313f2021-06-23 14:01:52 +0200378static inline int os_xrstor_safe(struct xregs_state *xstate, u64 mask)
Sebastian Andrzej Siewior926b21f2019-04-03 18:41:50 +0200379{
380 u32 lmask = mask;
381 u32 hmask = mask >> 32;
382 int err;
383
Thomas Gleixnerb16313f2021-06-23 14:01:52 +0200384 if (cpu_feature_enabled(X86_FEATURE_XSAVES))
Yu-cheng Yuc95473e2020-05-12 07:54:41 -0700385 XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
386 else
387 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
Sebastian Andrzej Siewior926b21f2019-04-03 18:41:50 +0200388
389 return err;
390}
391
Thomas Gleixner1d9bffa2021-06-23 14:02:15 +0200392extern void __restore_fpregs_from_fpstate(union fpregs_state *fpstate, u64 mask);
Linus Torvalds1361b832012-02-21 13:19:22 -0800393
Thomas Gleixner1c61fad2021-06-23 14:02:01 +0200394static inline void restore_fpregs_from_fpstate(union fpregs_state *fpstate)
Linus Torvalds1361b832012-02-21 13:19:22 -0800395{
Thomas Gleixner30a304a2021-06-23 14:02:20 +0200396 __restore_fpregs_from_fpstate(fpstate, xfeatures_mask_fpstate());
Linus Torvalds1361b832012-02-21 13:19:22 -0800397}
398
Ingo Molnar87dafd42015-05-25 10:57:06 +0200399extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fp, int size);
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200400
401/*
402 * FPU context switch related helper methods:
403 */
404
405DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
406
407/*
Rik van Riel25d83b52016-10-04 20:34:36 -0400408 * The in-register FPU state for an FPU context on a CPU is assumed to be
409 * valid if the fpu->last_cpu matches the CPU, and the fpu_fpregs_owner_ctx
410 * matches the FPU.
411 *
412 * If the FPU register state is valid, the kernel can skip restoring the
413 * FPU state from memory.
414 *
415 * Any code that clobbers the FPU registers or updates the in-memory
416 * FPU state for a task MUST let the rest of the kernel know that the
Rik van Riel317b6222016-10-14 08:15:30 -0400417 * FPU registers are no longer valid for this task.
Rik van Riel25d83b52016-10-04 20:34:36 -0400418 *
Rik van Riel317b6222016-10-14 08:15:30 -0400419 * Either one of these invalidation functions is enough. Invalidate
420 * a resource you control: CPU if using the CPU for something else
421 * (with preemption disabled), FPU for the current task, or a task that
422 * is prevented from running by the current task.
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200423 */
Rik van Riel317b6222016-10-14 08:15:30 -0400424static inline void __cpu_invalidate_fpregs_state(void)
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200425{
Rik van Riel317b6222016-10-14 08:15:30 -0400426 __this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200427}
428
Rik van Riel25d83b52016-10-04 20:34:36 -0400429static inline void __fpu_invalidate_fpregs_state(struct fpu *fpu)
430{
431 fpu->last_cpu = -1;
432}
433
434static inline int fpregs_state_valid(struct fpu *fpu, unsigned int cpu)
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200435{
Sebastian Andrzej Siewior59c4bd82019-11-28 09:53:06 +0100436 return fpu == this_cpu_read(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200437}
438
Rik van Riel66f314e2016-10-04 20:34:37 -0400439/*
440 * These generally need preemption protection to work,
441 * do try to avoid using these on their own:
442 */
443static inline void fpregs_deactivate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800444{
Ingo Molnar36b544d2015-04-23 12:18:28 +0200445 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Dave Hansend1898b72016-06-01 10:42:20 -0700446 trace_x86_fpu_regs_deactivated(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800447}
448
Rik van Riel66f314e2016-10-04 20:34:37 -0400449static inline void fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800450{
Ingo Molnarc0311f62015-04-23 12:24:59 +0200451 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
Dave Hansend1898b72016-06-01 10:42:20 -0700452 trace_x86_fpu_regs_activated(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800453}
454
Thomas Gleixner727d0112021-06-23 14:02:14 +0200455/* Internal helper for switch_fpu_return() and signal frame setup */
456static inline void fpregs_restore_userregs(void)
Rik van Riel4ee91512019-04-03 18:41:38 +0200457{
Rik van Riel5f409e22019-04-03 18:41:52 +0200458 struct fpu *fpu = &current->thread.fpu;
459 int cpu = smp_processor_id();
460
Christoph Hellwig8d3289f2019-06-04 19:54:12 +0200461 if (WARN_ON_ONCE(current->flags & PF_KTHREAD))
Rik van Riel5f409e22019-04-03 18:41:52 +0200462 return;
463
Rik van Riel4ee91512019-04-03 18:41:38 +0200464 if (!fpregs_state_valid(fpu, cpu)) {
Thomas Gleixner2ebe81c2021-06-23 14:02:17 +0200465 u64 mask;
466
467 /*
468 * This restores _all_ xstate which has not been
469 * established yet.
470 *
471 * If PKRU is enabled, then the PKRU value is already
472 * correct because it was either set in switch_to() or in
473 * flush_thread(). So it is excluded because it might be
474 * not up to date in current->thread.fpu.xsave state.
475 */
476 mask = xfeatures_mask_restore_user() |
477 xfeatures_mask_supervisor();
478 __restore_fpregs_from_fpstate(&fpu->state, mask);
479
Rik van Riel4ee91512019-04-03 18:41:38 +0200480 fpregs_activate(fpu);
Rik van Riel5f409e22019-04-03 18:41:52 +0200481 fpu->last_cpu = cpu;
Rik van Riel4ee91512019-04-03 18:41:38 +0200482 }
Rik van Riel5f409e22019-04-03 18:41:52 +0200483 clear_thread_flag(TIF_NEED_FPU_LOAD);
Rik van Riel4ee91512019-04-03 18:41:38 +0200484}
485
486/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800487 * FPU state switching for scheduling.
488 *
489 * This is a two-stage process:
490 *
Rik van Rielc474e502016-10-14 08:15:31 -0400491 * - switch_fpu_prepare() saves the old state.
492 * This is done within the context of the old process.
Linus Torvalds1361b832012-02-21 13:19:22 -0800493 *
Rik van Riel5f409e22019-04-03 18:41:52 +0200494 * - switch_fpu_finish() sets TIF_NEED_FPU_LOAD; the floating point state
495 * will get loaded on return to userspace, or when the kernel needs it.
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200496 *
Sebastian Andrzej Siewior383c2522019-04-03 18:41:45 +0200497 * If TIF_NEED_FPU_LOAD is cleared then the CPU's FPU registers
498 * are saved in the current thread's FPU register state.
499 *
500 * If TIF_NEED_FPU_LOAD is set then CPU's FPU registers may not
501 * hold current()'s FPU registers. It is required to load the
502 * registers before returning to userland or using the content
503 * otherwise.
504 *
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200505 * The FPU context is only stored/restored for a user task and
Christoph Hellwig8d3289f2019-06-04 19:54:12 +0200506 * PF_KTHREAD is used to distinguish between kernel and user threads.
Linus Torvalds1361b832012-02-21 13:19:22 -0800507 */
Borislav Petkov67e87d42019-03-29 19:52:59 +0100508static inline void switch_fpu_prepare(struct fpu *old_fpu, int cpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800509{
Christoph Hellwig8d3289f2019-06-04 19:54:12 +0200510 if (static_cpu_has(X86_FEATURE_FPU) && !(current->flags & PF_KTHREAD)) {
Thomas Gleixner08ded2c2021-06-23 14:02:00 +0200511 save_fpregs_to_fpstate(old_fpu);
512 /*
513 * The save operation preserved register state, so the
514 * fpu_fpregs_owner_ctx is still @old_fpu. Store the
515 * current CPU number in @old_fpu, so the next return
516 * to user space can avoid the FPU register restore
517 * when is returns on the same CPU and still owns the
518 * context.
519 */
520 old_fpu->last_cpu = cpu;
Rik van Riel1361ef22015-02-06 15:02:03 -0500521
Dave Hansend1898b72016-06-01 10:42:20 -0700522 trace_x86_fpu_regs_deactivated(old_fpu);
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200523 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800524}
525
526/*
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200527 * Misc helper functions:
528 */
529
530/*
Thomas Gleixner95443692021-06-23 14:02:21 +0200531 * Delay loading of the complete FPU state until the return to userland.
532 * PKRU is handled separately.
Linus Torvalds1361b832012-02-21 13:19:22 -0800533 */
Rik van Riel5f409e22019-04-03 18:41:52 +0200534static inline void switch_fpu_finish(struct fpu *new_fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800535{
Thomas Gleixner95443692021-06-23 14:02:21 +0200536 if (cpu_feature_enabled(X86_FEATURE_FPU))
537 set_thread_flag(TIF_NEED_FPU_LOAD);
Linus Torvalds1361b832012-02-21 13:19:22 -0800538}
539
Ingo Molnar78f7f1e2015-04-24 02:54:44 +0200540#endif /* _ASM_X86_FPU_INTERNAL_H */