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