1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
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
11 #ifndef _ASM_X86_FPU_INTERNAL_H
12 #define _ASM_X86_FPU_INTERNAL_H
13
14 #include <linux/compat.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17
18 #include <asm/user.h>
19 #include <asm/fpu/api.h>
20 #include <asm/fpu/xstate.h>
21 #include <asm/cpufeature.h>
22 #include <asm/trace/fpu.h>
23
24 /*
25 * High level FPU state handling functions:
26 */
27 extern void fpu__initialize(struct fpu *fpu);
28 extern void fpu__prepare_read(struct fpu *fpu);
29 extern void fpu__prepare_write(struct fpu *fpu);
30 extern void fpu__save(struct fpu *fpu);
31 extern void fpu__restore(struct fpu *fpu);
32 extern int fpu__restore_sig(void __user *buf, int ia32_frame);
33 extern void fpu__drop(struct fpu *fpu);
34 extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
35 extern void fpu__clear(struct fpu *fpu);
36 extern int fpu__exception_code(struct fpu *fpu, int trap_nr);
37 extern int dump_fpu(struct pt_regs *ptregs, struct user_i387_struct *fpstate);
38
39 /*
40 * Boot time FPU initialization functions:
41 */
42 extern void fpu__init_cpu(void);
43 extern void fpu__init_system_xstate(void);
44 extern void fpu__init_cpu_xstate(void);
45 extern void fpu__init_system(struct cpuinfo_x86 *c);
46 extern void fpu__init_check_bugs(void);
47 extern void fpu__resume_cpu(void);
48 extern u64 fpu__get_supported_xfeatures_mask(void);
49
50 /*
51 * Debugging facility:
52 */
53 #ifdef CONFIG_X86_DEBUG_FPU
54 # define WARN_ON_FPU(x) WARN_ON_ONCE(x)
55 #else
56 # define WARN_ON_FPU(x) ({ (void)(x); 0; })
57 #endif
58
59 /*
60 * FPU related CPU feature flag helper routines:
61 */
use_xsaveopt(void)62 static __always_inline __pure bool use_xsaveopt(void)
63 {
64 return static_cpu_has(X86_FEATURE_XSAVEOPT);
65 }
66
use_xsave(void)67 static __always_inline __pure bool use_xsave(void)
68 {
69 return static_cpu_has(X86_FEATURE_XSAVE);
70 }
71
use_fxsr(void)72 static __always_inline __pure bool use_fxsr(void)
73 {
74 return static_cpu_has(X86_FEATURE_FXSR);
75 }
76
77 /*
78 * fpstate handling functions:
79 */
80
81 extern union fpregs_state init_fpstate;
82
83 extern void fpstate_init(union fpregs_state *state);
84 #ifdef CONFIG_MATH_EMULATION
85 extern void fpstate_init_soft(struct swregs_state *soft);
86 #else
fpstate_init_soft(struct swregs_state * soft)87 static inline void fpstate_init_soft(struct swregs_state *soft) {}
88 #endif
89
fpstate_init_xstate(struct xregs_state * xsave)90 static 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 */
96 xsave->header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT | xfeatures_mask;
97 }
98
fpstate_init_fxstate(struct fxregs_state * fx)99 static inline void fpstate_init_fxstate(struct fxregs_state *fx)
100 {
101 fx->cwd = 0x37f;
102 fx->mxcsr = MXCSR_DEFAULT;
103 }
104 extern void fpstate_sanitize_xstate(struct fpu *fpu);
105
106 #define user_insn(insn, output, input...) \
107 ({ \
108 int err; \
109 \
110 might_fault(); \
111 \
112 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 })
124
125 #define kernel_insn(insn, output, input...) \
126 asm volatile("1:" #insn "\n\t" \
127 "2:\n" \
128 _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_fprestore) \
129 : output : input)
130
copy_fregs_to_user(struct fregs_state __user * fx)131 static inline int copy_fregs_to_user(struct fregs_state __user *fx)
132 {
133 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
134 }
135
copy_fxregs_to_user(struct fxregs_state __user * fx)136 static inline int copy_fxregs_to_user(struct fxregs_state __user *fx)
137 {
138 if (IS_ENABLED(CONFIG_X86_32))
139 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
140 else if (IS_ENABLED(CONFIG_AS_FXSAVEQ))
141 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
142
143 /* See comment in copy_fxregs_to_kernel() below. */
144 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
145 }
146
copy_kernel_to_fxregs(struct fxregs_state * fx)147 static inline void copy_kernel_to_fxregs(struct fxregs_state *fx)
148 {
149 if (IS_ENABLED(CONFIG_X86_32)) {
150 kernel_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
151 } else {
152 if (IS_ENABLED(CONFIG_AS_FXSAVEQ)) {
153 kernel_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
154 } else {
155 /* See comment in copy_fxregs_to_kernel() below. */
156 kernel_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx), "m" (*fx));
157 }
158 }
159 }
160
copy_user_to_fxregs(struct fxregs_state __user * fx)161 static inline int copy_user_to_fxregs(struct fxregs_state __user *fx)
162 {
163 if (IS_ENABLED(CONFIG_X86_32))
164 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
165 else if (IS_ENABLED(CONFIG_AS_FXSAVEQ))
166 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
167
168 /* See comment in copy_fxregs_to_kernel() below. */
169 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
170 "m" (*fx));
171 }
172
copy_kernel_to_fregs(struct fregs_state * fx)173 static inline void copy_kernel_to_fregs(struct fregs_state *fx)
174 {
175 kernel_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
176 }
177
copy_user_to_fregs(struct fregs_state __user * fx)178 static inline int copy_user_to_fregs(struct fregs_state __user *fx)
179 {
180 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
181 }
182
copy_fxregs_to_kernel(struct fpu * fpu)183 static inline void copy_fxregs_to_kernel(struct fpu *fpu)
184 {
185 if (IS_ENABLED(CONFIG_X86_32))
186 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave));
187 else if (IS_ENABLED(CONFIG_AS_FXSAVEQ))
188 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave));
189 else {
190 /* Using "rex64; fxsave %0" is broken because, if the memory
191 * operand uses any extended registers for addressing, a second
192 * REX prefix will be generated (to the assembler, rex64
193 * followed by semicolon is a separate instruction), and hence
194 * the 64-bitness is lost.
195 *
196 * Using "fxsaveq %0" would be the ideal choice, but is only
197 * supported starting with gas 2.16.
198 *
199 * Using, as a workaround, the properly prefixed form below
200 * isn't accepted by any binutils version so far released,
201 * complaining that the same type of prefix is used twice if
202 * an extended register is needed for addressing (fix submitted
203 * to mainline 2005-11-21).
204 *
205 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave));
206 *
207 * This, however, we can work around by forcing the compiler to
208 * select an addressing mode that doesn't require extended
209 * registers.
210 */
211 asm volatile( "rex64/fxsave (%[fx])"
212 : "=m" (fpu->state.fxsave)
213 : [fx] "R" (&fpu->state.fxsave));
214 }
215 }
216
217 /* These macros all use (%edi)/(%rdi) as the single memory argument. */
218 #define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
219 #define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
220 #define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
221 #define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
222 #define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
223
224 #define XSTATE_OP(op, st, lmask, hmask, err) \
225 asm volatile("1:" op "\n\t" \
226 "xor %[err], %[err]\n" \
227 "2:\n\t" \
228 ".pushsection .fixup,\"ax\"\n\t" \
229 "3: movl $-2,%[err]\n\t" \
230 "jmp 2b\n\t" \
231 ".popsection\n\t" \
232 _ASM_EXTABLE(1b, 3b) \
233 : [err] "=r" (err) \
234 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
235 : "memory")
236
237 /*
238 * If XSAVES is enabled, it replaces XSAVEOPT because it supports a compact
239 * format and supervisor states in addition to modified optimization in
240 * XSAVEOPT.
241 *
242 * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
243 * supports modified optimization which is not supported by XSAVE.
244 *
245 * We use XSAVE as a fallback.
246 *
247 * The 661 label is defined in the ALTERNATIVE* macros as the address of the
248 * original instruction which gets replaced. We need to use it here as the
249 * address of the instruction where we might get an exception at.
250 */
251 #define XSTATE_XSAVE(st, lmask, hmask, err) \
252 asm volatile(ALTERNATIVE_2(XSAVE, \
253 XSAVEOPT, X86_FEATURE_XSAVEOPT, \
254 XSAVES, X86_FEATURE_XSAVES) \
255 "\n" \
256 "xor %[err], %[err]\n" \
257 "3:\n" \
258 ".pushsection .fixup,\"ax\"\n" \
259 "4: movl $-2, %[err]\n" \
260 "jmp 3b\n" \
261 ".popsection\n" \
262 _ASM_EXTABLE(661b, 4b) \
263 : [err] "=r" (err) \
264 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
265 : "memory")
266
267 /*
268 * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
269 * XSAVE area format.
270 */
271 #define XSTATE_XRESTORE(st, lmask, hmask) \
272 asm volatile(ALTERNATIVE(XRSTOR, \
273 XRSTORS, X86_FEATURE_XSAVES) \
274 "\n" \
275 "3:\n" \
276 _ASM_EXTABLE_HANDLE(661b, 3b, ex_handler_fprestore)\
277 : \
278 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
279 : "memory")
280
281 /*
282 * This function is called only during boot time when x86 caps are not set
283 * up and alternative can not be used yet.
284 */
copy_xregs_to_kernel_booting(struct xregs_state * xstate)285 static inline void copy_xregs_to_kernel_booting(struct xregs_state *xstate)
286 {
287 u64 mask = -1;
288 u32 lmask = mask;
289 u32 hmask = mask >> 32;
290 int err;
291
292 WARN_ON(system_state != SYSTEM_BOOTING);
293
294 if (static_cpu_has(X86_FEATURE_XSAVES))
295 XSTATE_OP(XSAVES, xstate, lmask, hmask, err);
296 else
297 XSTATE_OP(XSAVE, xstate, lmask, hmask, err);
298
299 /* We should never fault when copying to a kernel buffer: */
300 WARN_ON_FPU(err);
301 }
302
303 /*
304 * This function is called only during boot time when x86 caps are not set
305 * up and alternative can not be used yet.
306 */
copy_kernel_to_xregs_booting(struct xregs_state * xstate)307 static inline void copy_kernel_to_xregs_booting(struct xregs_state *xstate)
308 {
309 u64 mask = -1;
310 u32 lmask = mask;
311 u32 hmask = mask >> 32;
312 int err;
313
314 WARN_ON(system_state != SYSTEM_BOOTING);
315
316 if (static_cpu_has(X86_FEATURE_XSAVES))
317 XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
318 else
319 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
320
321 /*
322 * We should never fault when copying from a kernel buffer, and the FPU
323 * state we set at boot time should be valid.
324 */
325 WARN_ON_FPU(err);
326 }
327
328 /*
329 * Save processor xstate to xsave area.
330 */
copy_xregs_to_kernel(struct xregs_state * xstate)331 static inline void copy_xregs_to_kernel(struct xregs_state *xstate)
332 {
333 u64 mask = -1;
334 u32 lmask = mask;
335 u32 hmask = mask >> 32;
336 int err;
337
338 WARN_ON_FPU(!alternatives_patched);
339
340 XSTATE_XSAVE(xstate, lmask, hmask, err);
341
342 /* We should never fault when copying to a kernel buffer: */
343 WARN_ON_FPU(err);
344 }
345
346 /*
347 * Restore processor xstate from xsave area.
348 */
copy_kernel_to_xregs(struct xregs_state * xstate,u64 mask)349 static inline void copy_kernel_to_xregs(struct xregs_state *xstate, u64 mask)
350 {
351 u32 lmask = mask;
352 u32 hmask = mask >> 32;
353
354 XSTATE_XRESTORE(xstate, lmask, hmask);
355 }
356
357 /*
358 * Save xstate to user space xsave area.
359 *
360 * We don't use modified optimization because xrstor/xrstors might track
361 * a different application.
362 *
363 * We don't use compacted format xsave area for
364 * backward compatibility for old applications which don't understand
365 * compacted format of xsave area.
366 */
copy_xregs_to_user(struct xregs_state __user * buf)367 static inline int copy_xregs_to_user(struct xregs_state __user *buf)
368 {
369 int err;
370
371 /*
372 * Clear the xsave header first, so that reserved fields are
373 * initialized to zero.
374 */
375 err = __clear_user(&buf->header, sizeof(buf->header));
376 if (unlikely(err))
377 return -EFAULT;
378
379 stac();
380 XSTATE_OP(XSAVE, buf, -1, -1, err);
381 clac();
382
383 return err;
384 }
385
386 /*
387 * Restore xstate from user space xsave area.
388 */
copy_user_to_xregs(struct xregs_state __user * buf,u64 mask)389 static inline int copy_user_to_xregs(struct xregs_state __user *buf, u64 mask)
390 {
391 struct xregs_state *xstate = ((__force struct xregs_state *)buf);
392 u32 lmask = mask;
393 u32 hmask = mask >> 32;
394 int err;
395
396 stac();
397 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
398 clac();
399
400 return err;
401 }
402
403 /*
404 * These must be called with preempt disabled. Returns
405 * 'true' if the FPU state is still intact and we can
406 * keep registers active.
407 *
408 * The legacy FNSAVE instruction cleared all FPU state
409 * unconditionally, so registers are essentially destroyed.
410 * Modern FPU state can be kept in registers, if there are
411 * no pending FP exceptions.
412 */
copy_fpregs_to_fpstate(struct fpu * fpu)413 static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
414 {
415 if (likely(use_xsave())) {
416 copy_xregs_to_kernel(&fpu->state.xsave);
417 return 1;
418 }
419
420 if (likely(use_fxsr())) {
421 copy_fxregs_to_kernel(fpu);
422 return 1;
423 }
424
425 /*
426 * Legacy FPU register saving, FNSAVE always clears FPU registers,
427 * so we have to mark them inactive:
428 */
429 asm volatile("fnsave %[fp]; fwait" : [fp] "=m" (fpu->state.fsave));
430
431 return 0;
432 }
433
__copy_kernel_to_fpregs(union fpregs_state * fpstate,u64 mask)434 static inline void __copy_kernel_to_fpregs(union fpregs_state *fpstate, u64 mask)
435 {
436 if (use_xsave()) {
437 copy_kernel_to_xregs(&fpstate->xsave, mask);
438 } else {
439 if (use_fxsr())
440 copy_kernel_to_fxregs(&fpstate->fxsave);
441 else
442 copy_kernel_to_fregs(&fpstate->fsave);
443 }
444 }
445
copy_kernel_to_fpregs(union fpregs_state * fpstate)446 static inline void copy_kernel_to_fpregs(union fpregs_state *fpstate)
447 {
448 /*
449 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
450 * pending. Clear the x87 state here by setting it to fixed values.
451 * "m" is a random variable that should be in L1.
452 */
453 if (unlikely(static_cpu_has_bug(X86_BUG_FXSAVE_LEAK))) {
454 asm volatile(
455 "fnclex\n\t"
456 "emms\n\t"
457 "fildl %P[addr]" /* set F?P to defined value */
458 : : [addr] "m" (fpstate));
459 }
460
461 __copy_kernel_to_fpregs(fpstate, -1);
462 }
463
464 extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fp, int size);
465
466 /*
467 * FPU context switch related helper methods:
468 */
469
470 DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
471
472 /*
473 * The in-register FPU state for an FPU context on a CPU is assumed to be
474 * valid if the fpu->last_cpu matches the CPU, and the fpu_fpregs_owner_ctx
475 * matches the FPU.
476 *
477 * If the FPU register state is valid, the kernel can skip restoring the
478 * FPU state from memory.
479 *
480 * Any code that clobbers the FPU registers or updates the in-memory
481 * FPU state for a task MUST let the rest of the kernel know that the
482 * FPU registers are no longer valid for this task.
483 *
484 * Either one of these invalidation functions is enough. Invalidate
485 * a resource you control: CPU if using the CPU for something else
486 * (with preemption disabled), FPU for the current task, or a task that
487 * is prevented from running by the current task.
488 */
__cpu_invalidate_fpregs_state(void)489 static inline void __cpu_invalidate_fpregs_state(void)
490 {
491 __this_cpu_write(fpu_fpregs_owner_ctx, NULL);
492 }
493
__fpu_invalidate_fpregs_state(struct fpu * fpu)494 static inline void __fpu_invalidate_fpregs_state(struct fpu *fpu)
495 {
496 fpu->last_cpu = -1;
497 }
498
fpregs_state_valid(struct fpu * fpu,unsigned int cpu)499 static inline int fpregs_state_valid(struct fpu *fpu, unsigned int cpu)
500 {
501 return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
502 }
503
504 /*
505 * These generally need preemption protection to work,
506 * do try to avoid using these on their own:
507 */
fpregs_deactivate(struct fpu * fpu)508 static inline void fpregs_deactivate(struct fpu *fpu)
509 {
510 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
511 trace_x86_fpu_regs_deactivated(fpu);
512 }
513
fpregs_activate(struct fpu * fpu)514 static inline void fpregs_activate(struct fpu *fpu)
515 {
516 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
517 trace_x86_fpu_regs_activated(fpu);
518 }
519
520 /*
521 * FPU state switching for scheduling.
522 *
523 * This is a two-stage process:
524 *
525 * - switch_fpu_prepare() saves the old state.
526 * This is done within the context of the old process.
527 *
528 * - switch_fpu_finish() restores the new state as
529 * necessary.
530 */
531 static inline void
switch_fpu_prepare(struct fpu * old_fpu,int cpu)532 switch_fpu_prepare(struct fpu *old_fpu, int cpu)
533 {
534 if (static_cpu_has(X86_FEATURE_FPU) && old_fpu->initialized) {
535 if (!copy_fpregs_to_fpstate(old_fpu))
536 old_fpu->last_cpu = -1;
537 else
538 old_fpu->last_cpu = cpu;
539
540 /* But leave fpu_fpregs_owner_ctx! */
541 trace_x86_fpu_regs_deactivated(old_fpu);
542 } else
543 old_fpu->last_cpu = -1;
544 }
545
546 /*
547 * Misc helper functions:
548 */
549
550 /*
551 * Set up the userspace FPU context for the new task, if the task
552 * has used the FPU.
553 */
switch_fpu_finish(struct fpu * new_fpu,int cpu)554 static inline void switch_fpu_finish(struct fpu *new_fpu, int cpu)
555 {
556 bool preload = static_cpu_has(X86_FEATURE_FPU) &&
557 new_fpu->initialized;
558
559 if (preload) {
560 if (!fpregs_state_valid(new_fpu, cpu))
561 copy_kernel_to_fpregs(&new_fpu->state);
562 fpregs_activate(new_fpu);
563 }
564 }
565
566 /*
567 * Needs to be preemption-safe.
568 *
569 * NOTE! user_fpu_begin() must be used only immediately before restoring
570 * the save state. It does not do any saving/restoring on its own. In
571 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
572 * the task can lose the FPU right after preempt_enable().
573 */
user_fpu_begin(void)574 static inline void user_fpu_begin(void)
575 {
576 struct fpu *fpu = ¤t->thread.fpu;
577
578 preempt_disable();
579 fpregs_activate(fpu);
580 preempt_enable();
581 }
582
583 /*
584 * MXCSR and XCR definitions:
585 */
586
587 extern unsigned int mxcsr_feature_mask;
588
589 #define XCR_XFEATURE_ENABLED_MASK 0x00000000
590
xgetbv(u32 index)591 static inline u64 xgetbv(u32 index)
592 {
593 u32 eax, edx;
594
595 asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
596 : "=a" (eax), "=d" (edx)
597 : "c" (index));
598 return eax + ((u64)edx << 32);
599 }
600
xsetbv(u32 index,u64 value)601 static inline void xsetbv(u32 index, u64 value)
602 {
603 u32 eax = value;
604 u32 edx = value >> 32;
605
606 asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
607 : : "a" (eax), "d" (edx), "c" (index));
608 }
609
610 #endif /* _ASM_X86_FPU_INTERNAL_H */
611