x86/pkeys: Default to a restrictive init PKRU
[cascardo/linux.git] / arch / x86 / kernel / fpu / core.c
1 /*
2  *  Copyright (C) 1994 Linus Torvalds
3  *
4  *  Pentium III FXSR, SSE support
5  *  General FPU state handling cleanups
6  *      Gareth Hughes <gareth@valinux.com>, May 2000
7  */
8 #include <asm/fpu/internal.h>
9 #include <asm/fpu/regset.h>
10 #include <asm/fpu/signal.h>
11 #include <asm/fpu/types.h>
12 #include <asm/traps.h>
13
14 #include <linux/hardirq.h>
15 #include <linux/pkeys.h>
16
17 #define CREATE_TRACE_POINTS
18 #include <asm/trace/fpu.h>
19
20 /*
21  * Represents the initial FPU state. It's mostly (but not completely) zeroes,
22  * depending on the FPU hardware format:
23  */
24 union fpregs_state init_fpstate __read_mostly;
25
26 /*
27  * Track whether the kernel is using the FPU state
28  * currently.
29  *
30  * This flag is used:
31  *
32  *   - by IRQ context code to potentially use the FPU
33  *     if it's unused.
34  *
35  *   - to debug kernel_fpu_begin()/end() correctness
36  */
37 static DEFINE_PER_CPU(bool, in_kernel_fpu);
38
39 /*
40  * Track which context is using the FPU on the CPU:
41  */
42 DEFINE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
43
44 static void kernel_fpu_disable(void)
45 {
46         WARN_ON_FPU(this_cpu_read(in_kernel_fpu));
47         this_cpu_write(in_kernel_fpu, true);
48 }
49
50 static void kernel_fpu_enable(void)
51 {
52         WARN_ON_FPU(!this_cpu_read(in_kernel_fpu));
53         this_cpu_write(in_kernel_fpu, false);
54 }
55
56 static bool kernel_fpu_disabled(void)
57 {
58         return this_cpu_read(in_kernel_fpu);
59 }
60
61 /*
62  * Were we in an interrupt that interrupted kernel mode?
63  *
64  * On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that
65  * pair does nothing at all: the thread must not have fpu (so
66  * that we don't try to save the FPU state), and TS must
67  * be set (so that the clts/stts pair does nothing that is
68  * visible in the interrupted kernel thread).
69  *
70  * Except for the eagerfpu case when we return true; in the likely case
71  * the thread has FPU but we are not going to set/clear TS.
72  */
73 static bool interrupted_kernel_fpu_idle(void)
74 {
75         if (kernel_fpu_disabled())
76                 return false;
77
78         if (use_eager_fpu())
79                 return true;
80
81         return !current->thread.fpu.fpregs_active && (read_cr0() & X86_CR0_TS);
82 }
83
84 /*
85  * Were we in user mode (or vm86 mode) when we were
86  * interrupted?
87  *
88  * Doing kernel_fpu_begin/end() is ok if we are running
89  * in an interrupt context from user mode - we'll just
90  * save the FPU state as required.
91  */
92 static bool interrupted_user_mode(void)
93 {
94         struct pt_regs *regs = get_irq_regs();
95         return regs && user_mode(regs);
96 }
97
98 /*
99  * Can we use the FPU in kernel mode with the
100  * whole "kernel_fpu_begin/end()" sequence?
101  *
102  * It's always ok in process context (ie "not interrupt")
103  * but it is sometimes ok even from an irq.
104  */
105 bool irq_fpu_usable(void)
106 {
107         return !in_interrupt() ||
108                 interrupted_user_mode() ||
109                 interrupted_kernel_fpu_idle();
110 }
111 EXPORT_SYMBOL(irq_fpu_usable);
112
113 void __kernel_fpu_begin(void)
114 {
115         struct fpu *fpu = &current->thread.fpu;
116
117         WARN_ON_FPU(!irq_fpu_usable());
118
119         kernel_fpu_disable();
120
121         if (fpu->fpregs_active) {
122                 /*
123                  * Ignore return value -- we don't care if reg state
124                  * is clobbered.
125                  */
126                 copy_fpregs_to_fpstate(fpu);
127         } else {
128                 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
129                 __fpregs_activate_hw();
130         }
131 }
132 EXPORT_SYMBOL(__kernel_fpu_begin);
133
134 void __kernel_fpu_end(void)
135 {
136         struct fpu *fpu = &current->thread.fpu;
137
138         if (fpu->fpregs_active)
139                 copy_kernel_to_fpregs(&fpu->state);
140         else
141                 __fpregs_deactivate_hw();
142
143         kernel_fpu_enable();
144 }
145 EXPORT_SYMBOL(__kernel_fpu_end);
146
147 void kernel_fpu_begin(void)
148 {
149         preempt_disable();
150         __kernel_fpu_begin();
151 }
152 EXPORT_SYMBOL_GPL(kernel_fpu_begin);
153
154 void kernel_fpu_end(void)
155 {
156         __kernel_fpu_end();
157         preempt_enable();
158 }
159 EXPORT_SYMBOL_GPL(kernel_fpu_end);
160
161 /*
162  * CR0::TS save/restore functions:
163  */
164 int irq_ts_save(void)
165 {
166         /*
167          * If in process context and not atomic, we can take a spurious DNA fault.
168          * Otherwise, doing clts() in process context requires disabling preemption
169          * or some heavy lifting like kernel_fpu_begin()
170          */
171         if (!in_atomic())
172                 return 0;
173
174         if (read_cr0() & X86_CR0_TS) {
175                 clts();
176                 return 1;
177         }
178
179         return 0;
180 }
181 EXPORT_SYMBOL_GPL(irq_ts_save);
182
183 void irq_ts_restore(int TS_state)
184 {
185         if (TS_state)
186                 stts();
187 }
188 EXPORT_SYMBOL_GPL(irq_ts_restore);
189
190 /*
191  * Save the FPU state (mark it for reload if necessary):
192  *
193  * This only ever gets called for the current task.
194  */
195 void fpu__save(struct fpu *fpu)
196 {
197         WARN_ON_FPU(fpu != &current->thread.fpu);
198
199         preempt_disable();
200         trace_x86_fpu_before_save(fpu);
201         if (fpu->fpregs_active) {
202                 if (!copy_fpregs_to_fpstate(fpu)) {
203                         if (use_eager_fpu())
204                                 copy_kernel_to_fpregs(&fpu->state);
205                         else
206                                 fpregs_deactivate(fpu);
207                 }
208         }
209         trace_x86_fpu_after_save(fpu);
210         preempt_enable();
211 }
212 EXPORT_SYMBOL_GPL(fpu__save);
213
214 /*
215  * Legacy x87 fpstate state init:
216  */
217 static inline void fpstate_init_fstate(struct fregs_state *fp)
218 {
219         fp->cwd = 0xffff037fu;
220         fp->swd = 0xffff0000u;
221         fp->twd = 0xffffffffu;
222         fp->fos = 0xffff0000u;
223 }
224
225 void fpstate_init(union fpregs_state *state)
226 {
227         if (!static_cpu_has(X86_FEATURE_FPU)) {
228                 fpstate_init_soft(&state->soft);
229                 return;
230         }
231
232         memset(state, 0, fpu_kernel_xstate_size);
233
234         /*
235          * XRSTORS requires that this bit is set in xcomp_bv, or
236          * it will #GP. Make sure it is replaced after the memset().
237          */
238         if (static_cpu_has(X86_FEATURE_XSAVES))
239                 state->xsave.header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT;
240
241         if (static_cpu_has(X86_FEATURE_FXSR))
242                 fpstate_init_fxstate(&state->fxsave);
243         else
244                 fpstate_init_fstate(&state->fsave);
245 }
246 EXPORT_SYMBOL_GPL(fpstate_init);
247
248 int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu)
249 {
250         dst_fpu->counter = 0;
251         dst_fpu->fpregs_active = 0;
252         dst_fpu->last_cpu = -1;
253
254         if (!src_fpu->fpstate_active || !static_cpu_has(X86_FEATURE_FPU))
255                 return 0;
256
257         WARN_ON_FPU(src_fpu != &current->thread.fpu);
258
259         /*
260          * Don't let 'init optimized' areas of the XSAVE area
261          * leak into the child task:
262          */
263         if (use_eager_fpu())
264                 memset(&dst_fpu->state.xsave, 0, fpu_kernel_xstate_size);
265
266         /*
267          * Save current FPU registers directly into the child
268          * FPU context, without any memory-to-memory copying.
269          * In lazy mode, if the FPU context isn't loaded into
270          * fpregs, CR0.TS will be set and do_device_not_available
271          * will load the FPU context.
272          *
273          * We have to do all this with preemption disabled,
274          * mostly because of the FNSAVE case, because in that
275          * case we must not allow preemption in the window
276          * between the FNSAVE and us marking the context lazy.
277          *
278          * It shouldn't be an issue as even FNSAVE is plenty
279          * fast in terms of critical section length.
280          */
281         preempt_disable();
282         if (!copy_fpregs_to_fpstate(dst_fpu)) {
283                 memcpy(&src_fpu->state, &dst_fpu->state,
284                        fpu_kernel_xstate_size);
285
286                 if (use_eager_fpu())
287                         copy_kernel_to_fpregs(&src_fpu->state);
288                 else
289                         fpregs_deactivate(src_fpu);
290         }
291         preempt_enable();
292
293         trace_x86_fpu_copy_src(src_fpu);
294         trace_x86_fpu_copy_dst(dst_fpu);
295
296         return 0;
297 }
298
299 /*
300  * Activate the current task's in-memory FPU context,
301  * if it has not been used before:
302  */
303 void fpu__activate_curr(struct fpu *fpu)
304 {
305         WARN_ON_FPU(fpu != &current->thread.fpu);
306
307         if (!fpu->fpstate_active) {
308                 fpstate_init(&fpu->state);
309                 trace_x86_fpu_init_state(fpu);
310
311                 trace_x86_fpu_activate_state(fpu);
312                 /* Safe to do for the current task: */
313                 fpu->fpstate_active = 1;
314         }
315 }
316 EXPORT_SYMBOL_GPL(fpu__activate_curr);
317
318 /*
319  * This function must be called before we read a task's fpstate.
320  *
321  * If the task has not used the FPU before then initialize its
322  * fpstate.
323  *
324  * If the task has used the FPU before then save it.
325  */
326 void fpu__activate_fpstate_read(struct fpu *fpu)
327 {
328         /*
329          * If fpregs are active (in the current CPU), then
330          * copy them to the fpstate:
331          */
332         if (fpu->fpregs_active) {
333                 fpu__save(fpu);
334         } else {
335                 if (!fpu->fpstate_active) {
336                         fpstate_init(&fpu->state);
337                         trace_x86_fpu_init_state(fpu);
338
339                         trace_x86_fpu_activate_state(fpu);
340                         /* Safe to do for current and for stopped child tasks: */
341                         fpu->fpstate_active = 1;
342                 }
343         }
344 }
345
346 /*
347  * This function must be called before we write a task's fpstate.
348  *
349  * If the task has used the FPU before then unlazy it.
350  * If the task has not used the FPU before then initialize its fpstate.
351  *
352  * After this function call, after registers in the fpstate are
353  * modified and the child task has woken up, the child task will
354  * restore the modified FPU state from the modified context. If we
355  * didn't clear its lazy status here then the lazy in-registers
356  * state pending on its former CPU could be restored, corrupting
357  * the modifications.
358  */
359 void fpu__activate_fpstate_write(struct fpu *fpu)
360 {
361         /*
362          * Only stopped child tasks can be used to modify the FPU
363          * state in the fpstate buffer:
364          */
365         WARN_ON_FPU(fpu == &current->thread.fpu);
366
367         if (fpu->fpstate_active) {
368                 /* Invalidate any lazy state: */
369                 fpu->last_cpu = -1;
370         } else {
371                 fpstate_init(&fpu->state);
372                 trace_x86_fpu_init_state(fpu);
373
374                 trace_x86_fpu_activate_state(fpu);
375                 /* Safe to do for stopped child tasks: */
376                 fpu->fpstate_active = 1;
377         }
378 }
379
380 /*
381  * This function must be called before we write the current
382  * task's fpstate.
383  *
384  * This call gets the current FPU register state and moves
385  * it in to the 'fpstate'.  Preemption is disabled so that
386  * no writes to the 'fpstate' can occur from context
387  * swiches.
388  *
389  * Must be followed by a fpu__current_fpstate_write_end().
390  */
391 void fpu__current_fpstate_write_begin(void)
392 {
393         struct fpu *fpu = &current->thread.fpu;
394
395         /*
396          * Ensure that the context-switching code does not write
397          * over the fpstate while we are doing our update.
398          */
399         preempt_disable();
400
401         /*
402          * Move the fpregs in to the fpu's 'fpstate'.
403          */
404         fpu__activate_fpstate_read(fpu);
405
406         /*
407          * The caller is about to write to 'fpu'.  Ensure that no
408          * CPU thinks that its fpregs match the fpstate.  This
409          * ensures we will not be lazy and skip a XRSTOR in the
410          * future.
411          */
412         fpu->last_cpu = -1;
413 }
414
415 /*
416  * This function must be paired with fpu__current_fpstate_write_begin()
417  *
418  * This will ensure that the modified fpstate gets placed back in
419  * the fpregs if necessary.
420  *
421  * Note: This function may be called whether or not an _actual_
422  * write to the fpstate occurred.
423  */
424 void fpu__current_fpstate_write_end(void)
425 {
426         struct fpu *fpu = &current->thread.fpu;
427
428         /*
429          * 'fpu' now has an updated copy of the state, but the
430          * registers may still be out of date.  Update them with
431          * an XRSTOR if they are active.
432          */
433         if (fpregs_active())
434                 copy_kernel_to_fpregs(&fpu->state);
435
436         /*
437          * Our update is done and the fpregs/fpstate are in sync
438          * if necessary.  Context switches can happen again.
439          */
440         preempt_enable();
441 }
442
443 /*
444  * 'fpu__restore()' is called to copy FPU registers from
445  * the FPU fpstate to the live hw registers and to activate
446  * access to the hardware registers, so that FPU instructions
447  * can be used afterwards.
448  *
449  * Must be called with kernel preemption disabled (for example
450  * with local interrupts disabled, as it is in the case of
451  * do_device_not_available()).
452  */
453 void fpu__restore(struct fpu *fpu)
454 {
455         fpu__activate_curr(fpu);
456
457         /* Avoid __kernel_fpu_begin() right after fpregs_activate() */
458         kernel_fpu_disable();
459         trace_x86_fpu_before_restore(fpu);
460         fpregs_activate(fpu);
461         copy_kernel_to_fpregs(&fpu->state);
462         fpu->counter++;
463         trace_x86_fpu_after_restore(fpu);
464         kernel_fpu_enable();
465 }
466 EXPORT_SYMBOL_GPL(fpu__restore);
467
468 /*
469  * Drops current FPU state: deactivates the fpregs and
470  * the fpstate. NOTE: it still leaves previous contents
471  * in the fpregs in the eager-FPU case.
472  *
473  * This function can be used in cases where we know that
474  * a state-restore is coming: either an explicit one,
475  * or a reschedule.
476  */
477 void fpu__drop(struct fpu *fpu)
478 {
479         preempt_disable();
480         fpu->counter = 0;
481
482         if (fpu->fpregs_active) {
483                 /* Ignore delayed exceptions from user space */
484                 asm volatile("1: fwait\n"
485                              "2:\n"
486                              _ASM_EXTABLE(1b, 2b));
487                 fpregs_deactivate(fpu);
488         }
489
490         fpu->fpstate_active = 0;
491
492         trace_x86_fpu_dropped(fpu);
493
494         preempt_enable();
495 }
496
497 /*
498  * Clear FPU registers by setting them up from
499  * the init fpstate:
500  */
501 static inline void copy_init_fpstate_to_fpregs(void)
502 {
503         if (use_xsave())
504                 copy_kernel_to_xregs(&init_fpstate.xsave, -1);
505         else if (static_cpu_has(X86_FEATURE_FXSR))
506                 copy_kernel_to_fxregs(&init_fpstate.fxsave);
507         else
508                 copy_kernel_to_fregs(&init_fpstate.fsave);
509
510         if (boot_cpu_has(X86_FEATURE_OSPKE))
511                 copy_init_pkru_to_fpregs();
512 }
513
514 /*
515  * Clear the FPU state back to init state.
516  *
517  * Called by sys_execve(), by the signal handler code and by various
518  * error paths.
519  */
520 void fpu__clear(struct fpu *fpu)
521 {
522         WARN_ON_FPU(fpu != &current->thread.fpu); /* Almost certainly an anomaly */
523
524         if (!use_eager_fpu() || !static_cpu_has(X86_FEATURE_FPU)) {
525                 /* FPU state will be reallocated lazily at the first use. */
526                 fpu__drop(fpu);
527         } else {
528                 if (!fpu->fpstate_active) {
529                         fpu__activate_curr(fpu);
530                         user_fpu_begin();
531                 }
532                 copy_init_fpstate_to_fpregs();
533         }
534 }
535
536 /*
537  * x87 math exception handling:
538  */
539
540 int fpu__exception_code(struct fpu *fpu, int trap_nr)
541 {
542         int err;
543
544         if (trap_nr == X86_TRAP_MF) {
545                 unsigned short cwd, swd;
546                 /*
547                  * (~cwd & swd) will mask out exceptions that are not set to unmasked
548                  * status.  0x3f is the exception bits in these regs, 0x200 is the
549                  * C1 reg you need in case of a stack fault, 0x040 is the stack
550                  * fault bit.  We should only be taking one exception at a time,
551                  * so if this combination doesn't produce any single exception,
552                  * then we have a bad program that isn't synchronizing its FPU usage
553                  * and it will suffer the consequences since we won't be able to
554                  * fully reproduce the context of the exception.
555                  */
556                 if (boot_cpu_has(X86_FEATURE_FXSR)) {
557                         cwd = fpu->state.fxsave.cwd;
558                         swd = fpu->state.fxsave.swd;
559                 } else {
560                         cwd = (unsigned short)fpu->state.fsave.cwd;
561                         swd = (unsigned short)fpu->state.fsave.swd;
562                 }
563
564                 err = swd & ~cwd;
565         } else {
566                 /*
567                  * The SIMD FPU exceptions are handled a little differently, as there
568                  * is only a single status/control register.  Thus, to determine which
569                  * unmasked exception was caught we must mask the exception mask bits
570                  * at 0x1f80, and then use these to mask the exception bits at 0x3f.
571                  */
572                 unsigned short mxcsr = MXCSR_DEFAULT;
573
574                 if (boot_cpu_has(X86_FEATURE_XMM))
575                         mxcsr = fpu->state.fxsave.mxcsr;
576
577                 err = ~(mxcsr >> 7) & mxcsr;
578         }
579
580         if (err & 0x001) {      /* Invalid op */
581                 /*
582                  * swd & 0x240 == 0x040: Stack Underflow
583                  * swd & 0x240 == 0x240: Stack Overflow
584                  * User must clear the SF bit (0x40) if set
585                  */
586                 return FPE_FLTINV;
587         } else if (err & 0x004) { /* Divide by Zero */
588                 return FPE_FLTDIV;
589         } else if (err & 0x008) { /* Overflow */
590                 return FPE_FLTOVF;
591         } else if (err & 0x012) { /* Denormal, Underflow */
592                 return FPE_FLTUND;
593         } else if (err & 0x020) { /* Precision */
594                 return FPE_FLTRES;
595         }
596
597         /*
598          * If we're using IRQ 13, or supposedly even some trap
599          * X86_TRAP_MF implementations, it's possible
600          * we get a spurious trap, which is not an error.
601          */
602         return 0;
603 }