blob: 845e7481ab776e91c8d2a6afe8d942638ef7ddea [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>
Borislav Petkovcd4d09e2016-01-26 22:12:04 +010022#include <asm/cpufeature.h>
Dave Hansend1898b72016-06-01 10:42:20 -070023#include <asm/trace/fpu.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080024
Ingo Molnar6ffc1522015-04-29 20:24:14 +020025/*
26 * High level FPU state handling functions:
27 */
Ingo Molnar369a0362017-09-23 13:37:45 +020028extern void fpu__prepare_read(struct fpu *fpu);
29extern void fpu__prepare_write(struct fpu *fpu);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020030extern void fpu__save(struct fpu *fpu);
Ingo Molnar82c0e452015-04-29 21:09:18 +020031extern int fpu__restore_sig(void __user *buf, int ia32_frame);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020032extern void fpu__drop(struct fpu *fpu);
Rik van Riel5f409e22019-04-03 18:41:52 +020033extern int fpu__copy(struct task_struct *dst, struct task_struct *src);
Fenghua Yub860eb82020-05-12 07:54:39 -070034extern void fpu__clear_user_states(struct fpu *fpu);
35extern void fpu__clear_all(struct fpu *fpu);
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020036extern int fpu__exception_code(struct fpu *fpu, int trap_nr);
37extern int dump_fpu(struct pt_regs *ptregs, struct user_i387_struct *fpstate);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020038
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020039/*
40 * Boot time FPU initialization functions:
41 */
42extern void fpu__init_cpu(void);
43extern void fpu__init_system_xstate(void);
44extern void fpu__init_cpu_xstate(void);
45extern void fpu__init_system(struct cpuinfo_x86 *c);
Ingo Molnar952f07e2015-04-26 16:56:05 +020046extern void fpu__init_check_bugs(void);
47extern void fpu__resume_cpu(void);
yu-cheng yua5fe93a2016-01-06 14:24:53 -080048extern u64 fpu__get_supported_xfeatures_mask(void);
Ingo Molnar952f07e2015-04-26 16:56:05 +020049
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
Yu-cheng Yua5828ed2017-01-24 10:25:46 -080089
90static inline void fpstate_init_xstate(struct xregs_state *xsave)
91{
92 /*
93 * XRSTORS requires these bits set in xcomp_bv, or it will
94 * trigger #GP:
95 */
Yu-cheng Yu524bb732020-05-12 07:54:37 -070096 xsave->header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT | xfeatures_mask_all;
Yu-cheng Yua5828ed2017-01-24 10:25:46 -080097}
98
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020099static inline void fpstate_init_fxstate(struct fxregs_state *fx)
100{
101 fx->cwd = 0x37f;
102 fx->mxcsr = MXCSR_DEFAULT;
103}
Ingo Molnar36e49e7f2015-04-28 11:25:02 +0200104extern void fpstate_sanitize_xstate(struct fpu *fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800105
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700106#define user_insn(insn, output, input...) \
107({ \
108 int err; \
Sebastian Andrzej Siewior66374012018-11-28 23:20:11 +0100109 \
110 might_fault(); \
111 \
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700112 asm volatile(ASM_STAC "\n" \
113 "1:" #insn "\n\t" \
114 "2: " ASM_CLAC "\n" \
115 ".section .fixup,\"ax\"\n" \
116 "3: movl $-1,%[err]\n" \
117 " jmp 2b\n" \
118 ".previous\n" \
119 _ASM_EXTABLE(1b, 3b) \
120 : [err] "=r" (err), output \
121 : "0"(0), input); \
122 err; \
123})
Linus Torvalds1361b832012-02-21 13:19:22 -0800124
Sebastian Andrzej Siewior926b21f2019-04-03 18:41:50 +0200125#define kernel_insn_err(insn, output, input...) \
126({ \
127 int err; \
128 asm volatile("1:" #insn "\n\t" \
129 "2:\n" \
130 ".section .fixup,\"ax\"\n" \
131 "3: movl $-1,%[err]\n" \
132 " jmp 2b\n" \
133 ".previous\n" \
134 _ASM_EXTABLE(1b, 3b) \
135 : [err] "=r" (err), output \
136 : "0"(0), input); \
137 err; \
138})
139
Eric Biggersd5c80282017-09-23 15:00:09 +0200140#define kernel_insn(insn, output, input...) \
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700141 asm volatile("1:" #insn "\n\t" \
142 "2:\n" \
Eric Biggersd5c80282017-09-23 15:00:09 +0200143 _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_fprestore) \
144 : output : input)
Linus Torvalds1361b832012-02-21 13:19:22 -0800145
Ingo Molnarc47ada32015-04-30 17:15:32 +0200146static inline int copy_fregs_to_user(struct fregs_state __user *fx)
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700147{
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700148 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800149}
150
Ingo Molnarc47ada32015-04-30 17:15:32 +0200151static inline int copy_fxregs_to_user(struct fxregs_state __user *fx)
Linus Torvalds1361b832012-02-21 13:19:22 -0800152{
Masahiro Yamada97f26452016-08-03 13:45:50 -0700153 if (IS_ENABLED(CONFIG_X86_32))
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700154 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Borislav Petkovbae54dc2019-01-18 00:05:40 +0100155 else
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700156 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800157
Linus Torvalds1361b832012-02-21 13:19:22 -0800158}
159
Ingo Molnar9ccc27a2015-05-25 11:27:46 +0200160static inline void copy_kernel_to_fxregs(struct fxregs_state *fx)
Linus Torvalds1361b832012-02-21 13:19:22 -0800161{
Borislav Petkovbae54dc2019-01-18 00:05:40 +0100162 if (IS_ENABLED(CONFIG_X86_32))
Eric Biggersd5c80282017-09-23 15:00:09 +0200163 kernel_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Borislav Petkovbae54dc2019-01-18 00:05:40 +0100164 else
165 kernel_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700166}
167
Sebastian Andrzej Siewior926b21f2019-04-03 18:41:50 +0200168static inline int copy_kernel_to_fxregs_err(struct fxregs_state *fx)
169{
170 if (IS_ENABLED(CONFIG_X86_32))
171 return kernel_insn_err(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
172 else
173 return kernel_insn_err(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
174}
175
Ingo Molnarc47ada32015-04-30 17:15:32 +0200176static inline int copy_user_to_fxregs(struct fxregs_state __user *fx)
H. Peter Anvine139e952012-09-25 15:42:18 -0700177{
Masahiro Yamada97f26452016-08-03 13:45:50 -0700178 if (IS_ENABLED(CONFIG_X86_32))
H. Peter Anvine139e952012-09-25 15:42:18 -0700179 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Borislav Petkovbae54dc2019-01-18 00:05:40 +0100180 else
H. Peter Anvine139e952012-09-25 15:42:18 -0700181 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
H. Peter Anvine139e952012-09-25 15:42:18 -0700182}
183
Ingo Molnar9ccc27a2015-05-25 11:27:46 +0200184static inline void copy_kernel_to_fregs(struct fregs_state *fx)
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700185{
Eric Biggersd5c80282017-09-23 15:00:09 +0200186 kernel_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800187}
188
Sebastian Andrzej Siewior926b21f2019-04-03 18:41:50 +0200189static inline int copy_kernel_to_fregs_err(struct fregs_state *fx)
190{
191 return kernel_insn_err(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
192}
193
Ingo Molnarc47ada32015-04-30 17:15:32 +0200194static inline int copy_user_to_fregs(struct fregs_state __user *fx)
H. Peter Anvine139e952012-09-25 15:42:18 -0700195{
196 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
197}
198
Ingo Molnarc6813142015-04-30 11:34:09 +0200199static inline void copy_fxregs_to_kernel(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800200{
Masahiro Yamada97f26452016-08-03 13:45:50 -0700201 if (IS_ENABLED(CONFIG_X86_32))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200202 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave));
Borislav Petkovbae54dc2019-01-18 00:05:40 +0100203 else
Ingo Molnar7366ed72015-04-27 04:19:39 +0200204 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave));
Linus Torvalds1361b832012-02-21 13:19:22 -0800205}
206
Ingo Molnarfd169b02015-05-25 09:55:39 +0200207/* These macros all use (%edi)/(%rdi) as the single memory argument. */
208#define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
209#define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
210#define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
211#define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
212#define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
213
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100214#define XSTATE_OP(op, st, lmask, hmask, err) \
215 asm volatile("1:" op "\n\t" \
216 "xor %[err], %[err]\n" \
217 "2:\n\t" \
218 ".pushsection .fixup,\"ax\"\n\t" \
219 "3: movl $-2,%[err]\n\t" \
220 "jmp 2b\n\t" \
221 ".popsection\n\t" \
Jann Hornac26d1f2018-11-27 14:32:00 +0100222 _ASM_EXTABLE(1b, 3b) \
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100223 : [err] "=r" (err) \
224 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
225 : "memory")
226
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100227/*
228 * If XSAVES is enabled, it replaces XSAVEOPT because it supports a compact
229 * format and supervisor states in addition to modified optimization in
230 * XSAVEOPT.
231 *
232 * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
233 * supports modified optimization which is not supported by XSAVE.
234 *
235 * We use XSAVE as a fallback.
236 *
237 * The 661 label is defined in the ALTERNATIVE* macros as the address of the
238 * original instruction which gets replaced. We need to use it here as the
239 * address of the instruction where we might get an exception at.
240 */
241#define XSTATE_XSAVE(st, lmask, hmask, err) \
242 asm volatile(ALTERNATIVE_2(XSAVE, \
243 XSAVEOPT, X86_FEATURE_XSAVEOPT, \
244 XSAVES, X86_FEATURE_XSAVES) \
245 "\n" \
246 "xor %[err], %[err]\n" \
247 "3:\n" \
248 ".pushsection .fixup,\"ax\"\n" \
249 "4: movl $-2, %[err]\n" \
250 "jmp 3b\n" \
251 ".popsection\n" \
252 _ASM_EXTABLE(661b, 4b) \
253 : [err] "=r" (err) \
254 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
255 : "memory")
256
257/*
258 * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
259 * XSAVE area format.
260 */
Eric Biggersd5c80282017-09-23 15:00:09 +0200261#define XSTATE_XRESTORE(st, lmask, hmask) \
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100262 asm volatile(ALTERNATIVE(XRSTOR, \
263 XRSTORS, X86_FEATURE_XSAVES) \
264 "\n" \
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100265 "3:\n" \
Eric Biggersd5c80282017-09-23 15:00:09 +0200266 _ASM_EXTABLE_HANDLE(661b, 3b, ex_handler_fprestore)\
267 : \
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100268 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
269 : "memory")
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100270
Ingo Molnarfd169b02015-05-25 09:55:39 +0200271/*
272 * This function is called only during boot time when x86 caps are not set
273 * up and alternative can not be used yet.
274 */
Ingo Molnar8c05f052015-05-24 09:23:25 +0200275static inline void copy_xregs_to_kernel_booting(struct xregs_state *xstate)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200276{
277 u64 mask = -1;
278 u32 lmask = mask;
279 u32 hmask = mask >> 32;
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100280 int err;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200281
282 WARN_ON(system_state != SYSTEM_BOOTING);
283
Borislav Petkov67e87d42019-03-29 19:52:59 +0100284 if (boot_cpu_has(X86_FEATURE_XSAVES))
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100285 XSTATE_OP(XSAVES, xstate, lmask, hmask, err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200286 else
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100287 XSTATE_OP(XSAVE, xstate, lmask, hmask, err);
Ingo Molnar8c05f052015-05-24 09:23:25 +0200288
289 /* We should never fault when copying to a kernel buffer: */
290 WARN_ON_FPU(err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200291}
292
293/*
294 * This function is called only during boot time when x86 caps are not set
295 * up and alternative can not be used yet.
296 */
Ingo Molnard65fcd62015-05-27 14:04:44 +0200297static inline void copy_kernel_to_xregs_booting(struct xregs_state *xstate)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200298{
Ingo Molnard65fcd62015-05-27 14:04:44 +0200299 u64 mask = -1;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200300 u32 lmask = mask;
301 u32 hmask = mask >> 32;
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100302 int err;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200303
304 WARN_ON(system_state != SYSTEM_BOOTING);
305
Borislav Petkov67e87d42019-03-29 19:52:59 +0100306 if (boot_cpu_has(X86_FEATURE_XSAVES))
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100307 XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200308 else
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100309 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
Ingo Molnar8c05f052015-05-24 09:23:25 +0200310
Eric Biggersd5c80282017-09-23 15:00:09 +0200311 /*
312 * We should never fault when copying from a kernel buffer, and the FPU
313 * state we set at boot time should be valid.
314 */
Ingo Molnar8c05f052015-05-24 09:23:25 +0200315 WARN_ON_FPU(err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200316}
317
318/*
319 * Save processor xstate to xsave area.
320 */
Ingo Molnar8c05f052015-05-24 09:23:25 +0200321static inline void copy_xregs_to_kernel(struct xregs_state *xstate)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200322{
323 u64 mask = -1;
324 u32 lmask = mask;
325 u32 hmask = mask >> 32;
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100326 int err;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200327
Andi Kleen03eaec82017-09-23 15:00:06 +0200328 WARN_ON_FPU(!alternatives_patched);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200329
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100330 XSTATE_XSAVE(xstate, lmask, hmask, err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200331
Ingo Molnar8c05f052015-05-24 09:23:25 +0200332 /* We should never fault when copying to a kernel buffer: */
333 WARN_ON_FPU(err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200334}
335
336/*
337 * Restore processor xstate from xsave area.
338 */
Ingo Molnar8c05f052015-05-24 09:23:25 +0200339static inline void copy_kernel_to_xregs(struct xregs_state *xstate, u64 mask)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200340{
Ingo Molnarfd169b02015-05-25 09:55:39 +0200341 u32 lmask = mask;
342 u32 hmask = mask >> 32;
343
Eric Biggersd5c80282017-09-23 15:00:09 +0200344 XSTATE_XRESTORE(xstate, lmask, hmask);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200345}
346
347/*
348 * Save xstate to user space xsave area.
349 *
350 * We don't use modified optimization because xrstor/xrstors might track
351 * a different application.
352 *
353 * We don't use compacted format xsave area for
354 * backward compatibility for old applications which don't understand
355 * compacted format of xsave area.
356 */
357static inline int copy_xregs_to_user(struct xregs_state __user *buf)
358{
359 int err;
360
361 /*
362 * Clear the xsave header first, so that reserved fields are
363 * initialized to zero.
364 */
365 err = __clear_user(&buf->header, sizeof(buf->header));
366 if (unlikely(err))
367 return -EFAULT;
368
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100369 stac();
370 XSTATE_OP(XSAVE, buf, -1, -1, err);
371 clac();
372
Ingo Molnarfd169b02015-05-25 09:55:39 +0200373 return err;
374}
375
376/*
377 * Restore xstate from user space xsave area.
378 */
379static inline int copy_user_to_xregs(struct xregs_state __user *buf, u64 mask)
380{
Ingo Molnarfd169b02015-05-25 09:55:39 +0200381 struct xregs_state *xstate = ((__force struct xregs_state *)buf);
382 u32 lmask = mask;
383 u32 hmask = mask >> 32;
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100384 int err;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200385
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100386 stac();
387 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
388 clac();
389
Ingo Molnarfd169b02015-05-25 09:55:39 +0200390 return err;
391}
392
Linus Torvalds1361b832012-02-21 13:19:22 -0800393/*
Sebastian Andrzej Siewior926b21f2019-04-03 18:41:50 +0200394 * Restore xstate from kernel space xsave area, return an error code instead of
395 * an exception.
396 */
397static inline int copy_kernel_to_xregs_err(struct xregs_state *xstate, u64 mask)
398{
399 u32 lmask = mask;
400 u32 hmask = mask >> 32;
401 int err;
402
Yu-cheng Yuc95473e2020-05-12 07:54:41 -0700403 if (static_cpu_has(X86_FEATURE_XSAVES))
404 XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
405 else
406 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
Sebastian Andrzej Siewior926b21f2019-04-03 18:41:50 +0200407
408 return err;
409}
410
411/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800412 * These must be called with preempt disabled. Returns
Ingo Molnar4f836342015-04-27 02:53:16 +0200413 * 'true' if the FPU state is still intact and we can
414 * keep registers active.
415 *
416 * The legacy FNSAVE instruction cleared all FPU state
417 * unconditionally, so registers are essentially destroyed.
418 * Modern FPU state can be kept in registers, if there are
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200419 * no pending FP exceptions.
Linus Torvalds1361b832012-02-21 13:19:22 -0800420 */
Ingo Molnar4f836342015-04-27 02:53:16 +0200421static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800422{
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200423 if (likely(use_xsave())) {
Ingo Molnarc6813142015-04-30 11:34:09 +0200424 copy_xregs_to_kernel(&fpu->state.xsave);
Aubrey Li2f7726f2019-01-18 02:38:20 +0800425
426 /*
427 * AVX512 state is tracked here because its use is
428 * known to slow the max clock speed of the core.
429 */
430 if (fpu->state.xsave.header.xfeatures & XFEATURE_MASK_AVX512)
431 fpu->avx512_timestamp = jiffies;
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200432 return 1;
433 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800434
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200435 if (likely(use_fxsr())) {
Ingo Molnarc6813142015-04-30 11:34:09 +0200436 copy_fxregs_to_kernel(fpu);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200437 return 1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800438 }
439
440 /*
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200441 * Legacy FPU register saving, FNSAVE always clears FPU registers,
442 * so we have to mark them inactive:
Linus Torvalds1361b832012-02-21 13:19:22 -0800443 */
Ingo Molnar87dafd42015-05-25 10:57:06 +0200444 asm volatile("fnsave %[fp]; fwait" : [fp] "=m" (fpu->state.fsave));
Ingo Molnar4f836342015-04-27 02:53:16 +0200445
Ingo Molnar4f836342015-04-27 02:53:16 +0200446 return 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800447}
448
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +0200449static inline void __copy_kernel_to_fpregs(union fpregs_state *fpstate, u64 mask)
Linus Torvalds1361b832012-02-21 13:19:22 -0800450{
Ingo Molnar8c05f052015-05-24 09:23:25 +0200451 if (use_xsave()) {
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +0200452 copy_kernel_to_xregs(&fpstate->xsave, mask);
Ingo Molnar8c05f052015-05-24 09:23:25 +0200453 } else {
454 if (use_fxsr())
Ingo Molnar003e2e82015-05-25 11:59:35 +0200455 copy_kernel_to_fxregs(&fpstate->fxsave);
Ingo Molnar8c05f052015-05-24 09:23:25 +0200456 else
Ingo Molnar003e2e82015-05-25 11:59:35 +0200457 copy_kernel_to_fregs(&fpstate->fsave);
Ingo Molnar8c05f052015-05-24 09:23:25 +0200458 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800459}
460
Ingo Molnar003e2e82015-05-25 11:59:35 +0200461static inline void copy_kernel_to_fpregs(union fpregs_state *fpstate)
Linus Torvalds1361b832012-02-21 13:19:22 -0800462{
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100463 /*
464 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
465 * pending. Clear the x87 state here by setting it to fixed values.
466 * "m" is a random variable that should be in L1.
467 */
Borislav Petkovbc696ca2016-01-26 22:12:05 +0100468 if (unlikely(static_cpu_has_bug(X86_BUG_FXSAVE_LEAK))) {
Linus Torvalds26bef132014-01-11 19:15:52 -0800469 asm volatile(
470 "fnclex\n\t"
471 "emms\n\t"
472 "fildl %P[addr]" /* set F?P to defined value */
Ingo Molnar003e2e82015-05-25 11:59:35 +0200473 : : [addr] "m" (fpstate));
Linus Torvalds26bef132014-01-11 19:15:52 -0800474 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800475
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +0200476 __copy_kernel_to_fpregs(fpstate, -1);
Linus Torvalds1361b832012-02-21 13:19:22 -0800477}
478
Ingo Molnar87dafd42015-05-25 10:57:06 +0200479extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fp, int size);
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200480
481/*
482 * FPU context switch related helper methods:
483 */
484
485DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
486
487/*
Rik van Riel25d83b52016-10-04 20:34:36 -0400488 * The in-register FPU state for an FPU context on a CPU is assumed to be
489 * valid if the fpu->last_cpu matches the CPU, and the fpu_fpregs_owner_ctx
490 * matches the FPU.
491 *
492 * If the FPU register state is valid, the kernel can skip restoring the
493 * FPU state from memory.
494 *
495 * Any code that clobbers the FPU registers or updates the in-memory
496 * FPU state for a task MUST let the rest of the kernel know that the
Rik van Riel317b6222016-10-14 08:15:30 -0400497 * FPU registers are no longer valid for this task.
Rik van Riel25d83b52016-10-04 20:34:36 -0400498 *
Rik van Riel317b6222016-10-14 08:15:30 -0400499 * Either one of these invalidation functions is enough. Invalidate
500 * a resource you control: CPU if using the CPU for something else
501 * (with preemption disabled), FPU for the current task, or a task that
502 * is prevented from running by the current task.
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200503 */
Rik van Riel317b6222016-10-14 08:15:30 -0400504static inline void __cpu_invalidate_fpregs_state(void)
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200505{
Rik van Riel317b6222016-10-14 08:15:30 -0400506 __this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200507}
508
Rik van Riel25d83b52016-10-04 20:34:36 -0400509static inline void __fpu_invalidate_fpregs_state(struct fpu *fpu)
510{
511 fpu->last_cpu = -1;
512}
513
514static inline int fpregs_state_valid(struct fpu *fpu, unsigned int cpu)
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200515{
Sebastian Andrzej Siewior59c4bd82019-11-28 09:53:06 +0100516 return fpu == this_cpu_read(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200517}
518
Rik van Riel66f314e2016-10-04 20:34:37 -0400519/*
520 * These generally need preemption protection to work,
521 * do try to avoid using these on their own:
522 */
523static inline void fpregs_deactivate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800524{
Ingo Molnar36b544d2015-04-23 12:18:28 +0200525 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Dave Hansend1898b72016-06-01 10:42:20 -0700526 trace_x86_fpu_regs_deactivated(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800527}
528
Rik van Riel66f314e2016-10-04 20:34:37 -0400529static inline void fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800530{
Ingo Molnarc0311f62015-04-23 12:24:59 +0200531 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
Dave Hansend1898b72016-06-01 10:42:20 -0700532 trace_x86_fpu_regs_activated(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800533}
534
535/*
Rik van Riel4ee91512019-04-03 18:41:38 +0200536 * Internal helper, do not use directly. Use switch_fpu_return() instead.
537 */
Rik van Riel5f409e22019-04-03 18:41:52 +0200538static inline void __fpregs_load_activate(void)
Rik van Riel4ee91512019-04-03 18:41:38 +0200539{
Rik van Riel5f409e22019-04-03 18:41:52 +0200540 struct fpu *fpu = &current->thread.fpu;
541 int cpu = smp_processor_id();
542
Christoph Hellwig8d3289f2019-06-04 19:54:12 +0200543 if (WARN_ON_ONCE(current->flags & PF_KTHREAD))
Rik van Riel5f409e22019-04-03 18:41:52 +0200544 return;
545
Rik van Riel4ee91512019-04-03 18:41:38 +0200546 if (!fpregs_state_valid(fpu, cpu)) {
Rik van Riel5f409e22019-04-03 18:41:52 +0200547 copy_kernel_to_fpregs(&fpu->state);
Rik van Riel4ee91512019-04-03 18:41:38 +0200548 fpregs_activate(fpu);
Rik van Riel5f409e22019-04-03 18:41:52 +0200549 fpu->last_cpu = cpu;
Rik van Riel4ee91512019-04-03 18:41:38 +0200550 }
Rik van Riel5f409e22019-04-03 18:41:52 +0200551 clear_thread_flag(TIF_NEED_FPU_LOAD);
Rik van Riel4ee91512019-04-03 18:41:38 +0200552}
553
554/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800555 * FPU state switching for scheduling.
556 *
557 * This is a two-stage process:
558 *
Rik van Rielc474e502016-10-14 08:15:31 -0400559 * - switch_fpu_prepare() saves the old state.
560 * This is done within the context of the old process.
Linus Torvalds1361b832012-02-21 13:19:22 -0800561 *
Rik van Riel5f409e22019-04-03 18:41:52 +0200562 * - switch_fpu_finish() sets TIF_NEED_FPU_LOAD; the floating point state
563 * will get loaded on return to userspace, or when the kernel needs it.
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200564 *
Sebastian Andrzej Siewior383c2522019-04-03 18:41:45 +0200565 * If TIF_NEED_FPU_LOAD is cleared then the CPU's FPU registers
566 * are saved in the current thread's FPU register state.
567 *
568 * If TIF_NEED_FPU_LOAD is set then CPU's FPU registers may not
569 * hold current()'s FPU registers. It is required to load the
570 * registers before returning to userland or using the content
571 * otherwise.
572 *
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200573 * The FPU context is only stored/restored for a user task and
Christoph Hellwig8d3289f2019-06-04 19:54:12 +0200574 * PF_KTHREAD is used to distinguish between kernel and user threads.
Linus Torvalds1361b832012-02-21 13:19:22 -0800575 */
Borislav Petkov67e87d42019-03-29 19:52:59 +0100576static inline void switch_fpu_prepare(struct fpu *old_fpu, int cpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800577{
Christoph Hellwig8d3289f2019-06-04 19:54:12 +0200578 if (static_cpu_has(X86_FEATURE_FPU) && !(current->flags & PF_KTHREAD)) {
Ingo Molnar4f836342015-04-27 02:53:16 +0200579 if (!copy_fpregs_to_fpstate(old_fpu))
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200580 old_fpu->last_cpu = -1;
Rik van Riel1361ef22015-02-06 15:02:03 -0500581 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200582 old_fpu->last_cpu = cpu;
Rik van Riel1361ef22015-02-06 15:02:03 -0500583
Ingo Molnar36b544d2015-04-23 12:18:28 +0200584 /* But leave fpu_fpregs_owner_ctx! */
Dave Hansend1898b72016-06-01 10:42:20 -0700585 trace_x86_fpu_regs_deactivated(old_fpu);
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200586 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800587}
588
589/*
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200590 * Misc helper functions:
591 */
592
593/*
Rik van Riel5f409e22019-04-03 18:41:52 +0200594 * Load PKRU from the FPU context if available. Delay loading of the
595 * complete FPU state until the return to userland.
Linus Torvalds1361b832012-02-21 13:19:22 -0800596 */
Rik van Riel5f409e22019-04-03 18:41:52 +0200597static inline void switch_fpu_finish(struct fpu *new_fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800598{
Rik van Riel0cecca92019-04-03 18:41:44 +0200599 u32 pkru_val = init_pkru_value;
600 struct pkru_state *pk;
Rik van Rielc474e502016-10-14 08:15:31 -0400601
Rik van Riel0cecca92019-04-03 18:41:44 +0200602 if (!static_cpu_has(X86_FEATURE_FPU))
603 return;
604
Rik van Riel5f409e22019-04-03 18:41:52 +0200605 set_thread_flag(TIF_NEED_FPU_LOAD);
Rik van Riel0cecca92019-04-03 18:41:44 +0200606
607 if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
608 return;
609
610 /*
611 * PKRU state is switched eagerly because it needs to be valid before we
612 * return to userland e.g. for a copy_to_user() operation.
613 */
614 if (current->mm) {
615 pk = get_xsave_addr(&new_fpu->state.xsave, XFEATURE_PKRU);
616 if (pk)
617 pkru_val = pk->pkru;
Rik van Rielc474e502016-10-14 08:15:31 -0400618 }
Rik van Riel0cecca92019-04-03 18:41:44 +0200619 __write_pkru(pkru_val);
Linus Torvalds1361b832012-02-21 13:19:22 -0800620}
621
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200622/*
623 * MXCSR and XCR definitions:
624 */
625
Petteri Aimonen7ad81672020-06-16 11:12:57 +0200626static inline void ldmxcsr(u32 mxcsr)
627{
628 asm volatile("ldmxcsr %0" :: "m" (mxcsr));
629}
630
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200631extern unsigned int mxcsr_feature_mask;
632
633#define XCR_XFEATURE_ENABLED_MASK 0x00000000
634
635static inline u64 xgetbv(u32 index)
636{
637 u32 eax, edx;
638
639 asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
640 : "=a" (eax), "=d" (edx)
641 : "c" (index));
642 return eax + ((u64)edx << 32);
643}
644
645static inline void xsetbv(u32 index, u64 value)
646{
647 u32 eax = value;
648 u32 edx = value >> 32;
649
650 asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
651 : : "a" (eax), "d" (edx), "c" (index));
652}
653
Ingo Molnar78f7f1e2015-04-24 02:54:44 +0200654#endif /* _ASM_X86_FPU_INTERNAL_H */