xref: /openbmc/linux/arch/x86/kernel/fpu/core.c (revision dea54fba)
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 static bool interrupted_kernel_fpu_idle(void)
62 {
63 	return !kernel_fpu_disabled();
64 }
65 
66 /*
67  * Were we in user mode (or vm86 mode) when we were
68  * interrupted?
69  *
70  * Doing kernel_fpu_begin/end() is ok if we are running
71  * in an interrupt context from user mode - we'll just
72  * save the FPU state as required.
73  */
74 static bool interrupted_user_mode(void)
75 {
76 	struct pt_regs *regs = get_irq_regs();
77 	return regs && user_mode(regs);
78 }
79 
80 /*
81  * Can we use the FPU in kernel mode with the
82  * whole "kernel_fpu_begin/end()" sequence?
83  *
84  * It's always ok in process context (ie "not interrupt")
85  * but it is sometimes ok even from an irq.
86  */
87 bool irq_fpu_usable(void)
88 {
89 	return !in_interrupt() ||
90 		interrupted_user_mode() ||
91 		interrupted_kernel_fpu_idle();
92 }
93 EXPORT_SYMBOL(irq_fpu_usable);
94 
95 void __kernel_fpu_begin(void)
96 {
97 	struct fpu *fpu = &current->thread.fpu;
98 
99 	WARN_ON_FPU(!irq_fpu_usable());
100 
101 	kernel_fpu_disable();
102 
103 	if (fpu->fpregs_active) {
104 		/*
105 		 * Ignore return value -- we don't care if reg state
106 		 * is clobbered.
107 		 */
108 		copy_fpregs_to_fpstate(fpu);
109 	} else {
110 		__cpu_invalidate_fpregs_state();
111 	}
112 }
113 EXPORT_SYMBOL(__kernel_fpu_begin);
114 
115 void __kernel_fpu_end(void)
116 {
117 	struct fpu *fpu = &current->thread.fpu;
118 
119 	if (fpu->fpregs_active)
120 		copy_kernel_to_fpregs(&fpu->state);
121 
122 	kernel_fpu_enable();
123 }
124 EXPORT_SYMBOL(__kernel_fpu_end);
125 
126 void kernel_fpu_begin(void)
127 {
128 	preempt_disable();
129 	__kernel_fpu_begin();
130 }
131 EXPORT_SYMBOL_GPL(kernel_fpu_begin);
132 
133 void kernel_fpu_end(void)
134 {
135 	__kernel_fpu_end();
136 	preempt_enable();
137 }
138 EXPORT_SYMBOL_GPL(kernel_fpu_end);
139 
140 /*
141  * Save the FPU state (mark it for reload if necessary):
142  *
143  * This only ever gets called for the current task.
144  */
145 void fpu__save(struct fpu *fpu)
146 {
147 	WARN_ON_FPU(fpu != &current->thread.fpu);
148 
149 	preempt_disable();
150 	trace_x86_fpu_before_save(fpu);
151 	if (fpu->fpregs_active) {
152 		if (!copy_fpregs_to_fpstate(fpu)) {
153 			copy_kernel_to_fpregs(&fpu->state);
154 		}
155 	}
156 	trace_x86_fpu_after_save(fpu);
157 	preempt_enable();
158 }
159 EXPORT_SYMBOL_GPL(fpu__save);
160 
161 /*
162  * Legacy x87 fpstate state init:
163  */
164 static inline void fpstate_init_fstate(struct fregs_state *fp)
165 {
166 	fp->cwd = 0xffff037fu;
167 	fp->swd = 0xffff0000u;
168 	fp->twd = 0xffffffffu;
169 	fp->fos = 0xffff0000u;
170 }
171 
172 void fpstate_init(union fpregs_state *state)
173 {
174 	if (!static_cpu_has(X86_FEATURE_FPU)) {
175 		fpstate_init_soft(&state->soft);
176 		return;
177 	}
178 
179 	memset(state, 0, fpu_kernel_xstate_size);
180 
181 	if (static_cpu_has(X86_FEATURE_XSAVES))
182 		fpstate_init_xstate(&state->xsave);
183 	if (static_cpu_has(X86_FEATURE_FXSR))
184 		fpstate_init_fxstate(&state->fxsave);
185 	else
186 		fpstate_init_fstate(&state->fsave);
187 }
188 EXPORT_SYMBOL_GPL(fpstate_init);
189 
190 int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu)
191 {
192 	dst_fpu->fpregs_active = 0;
193 	dst_fpu->last_cpu = -1;
194 
195 	if (!src_fpu->fpstate_active || !static_cpu_has(X86_FEATURE_FPU))
196 		return 0;
197 
198 	WARN_ON_FPU(src_fpu != &current->thread.fpu);
199 
200 	/*
201 	 * Don't let 'init optimized' areas of the XSAVE area
202 	 * leak into the child task:
203 	 */
204 	memset(&dst_fpu->state.xsave, 0, fpu_kernel_xstate_size);
205 
206 	/*
207 	 * Save current FPU registers directly into the child
208 	 * FPU context, without any memory-to-memory copying.
209 	 * In lazy mode, if the FPU context isn't loaded into
210 	 * fpregs, CR0.TS will be set and do_device_not_available
211 	 * will load the FPU context.
212 	 *
213 	 * We have to do all this with preemption disabled,
214 	 * mostly because of the FNSAVE case, because in that
215 	 * case we must not allow preemption in the window
216 	 * between the FNSAVE and us marking the context lazy.
217 	 *
218 	 * It shouldn't be an issue as even FNSAVE is plenty
219 	 * fast in terms of critical section length.
220 	 */
221 	preempt_disable();
222 	if (!copy_fpregs_to_fpstate(dst_fpu)) {
223 		memcpy(&src_fpu->state, &dst_fpu->state,
224 		       fpu_kernel_xstate_size);
225 
226 		copy_kernel_to_fpregs(&src_fpu->state);
227 	}
228 	preempt_enable();
229 
230 	trace_x86_fpu_copy_src(src_fpu);
231 	trace_x86_fpu_copy_dst(dst_fpu);
232 
233 	return 0;
234 }
235 
236 /*
237  * Activate the current task's in-memory FPU context,
238  * if it has not been used before:
239  */
240 void fpu__activate_curr(struct fpu *fpu)
241 {
242 	WARN_ON_FPU(fpu != &current->thread.fpu);
243 
244 	if (!fpu->fpstate_active) {
245 		fpstate_init(&fpu->state);
246 		trace_x86_fpu_init_state(fpu);
247 
248 		trace_x86_fpu_activate_state(fpu);
249 		/* Safe to do for the current task: */
250 		fpu->fpstate_active = 1;
251 	}
252 }
253 EXPORT_SYMBOL_GPL(fpu__activate_curr);
254 
255 /*
256  * This function must be called before we read a task's fpstate.
257  *
258  * If the task has not used the FPU before then initialize its
259  * fpstate.
260  *
261  * If the task has used the FPU before then save it.
262  */
263 void fpu__activate_fpstate_read(struct fpu *fpu)
264 {
265 	/*
266 	 * If fpregs are active (in the current CPU), then
267 	 * copy them to the fpstate:
268 	 */
269 	if (fpu->fpregs_active) {
270 		fpu__save(fpu);
271 	} else {
272 		if (!fpu->fpstate_active) {
273 			fpstate_init(&fpu->state);
274 			trace_x86_fpu_init_state(fpu);
275 
276 			trace_x86_fpu_activate_state(fpu);
277 			/* Safe to do for current and for stopped child tasks: */
278 			fpu->fpstate_active = 1;
279 		}
280 	}
281 }
282 
283 /*
284  * This function must be called before we write a task's fpstate.
285  *
286  * If the task has used the FPU before then unlazy it.
287  * If the task has not used the FPU before then initialize its fpstate.
288  *
289  * After this function call, after registers in the fpstate are
290  * modified and the child task has woken up, the child task will
291  * restore the modified FPU state from the modified context. If we
292  * didn't clear its lazy status here then the lazy in-registers
293  * state pending on its former CPU could be restored, corrupting
294  * the modifications.
295  */
296 void fpu__activate_fpstate_write(struct fpu *fpu)
297 {
298 	/*
299 	 * Only stopped child tasks can be used to modify the FPU
300 	 * state in the fpstate buffer:
301 	 */
302 	WARN_ON_FPU(fpu == &current->thread.fpu);
303 
304 	if (fpu->fpstate_active) {
305 		/* Invalidate any lazy state: */
306 		__fpu_invalidate_fpregs_state(fpu);
307 	} else {
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 stopped child tasks: */
313 		fpu->fpstate_active = 1;
314 	}
315 }
316 
317 /*
318  * This function must be called before we write the current
319  * task's fpstate.
320  *
321  * This call gets the current FPU register state and moves
322  * it in to the 'fpstate'.  Preemption is disabled so that
323  * no writes to the 'fpstate' can occur from context
324  * swiches.
325  *
326  * Must be followed by a fpu__current_fpstate_write_end().
327  */
328 void fpu__current_fpstate_write_begin(void)
329 {
330 	struct fpu *fpu = &current->thread.fpu;
331 
332 	/*
333 	 * Ensure that the context-switching code does not write
334 	 * over the fpstate while we are doing our update.
335 	 */
336 	preempt_disable();
337 
338 	/*
339 	 * Move the fpregs in to the fpu's 'fpstate'.
340 	 */
341 	fpu__activate_fpstate_read(fpu);
342 
343 	/*
344 	 * The caller is about to write to 'fpu'.  Ensure that no
345 	 * CPU thinks that its fpregs match the fpstate.  This
346 	 * ensures we will not be lazy and skip a XRSTOR in the
347 	 * future.
348 	 */
349 	__fpu_invalidate_fpregs_state(fpu);
350 }
351 
352 /*
353  * This function must be paired with fpu__current_fpstate_write_begin()
354  *
355  * This will ensure that the modified fpstate gets placed back in
356  * the fpregs if necessary.
357  *
358  * Note: This function may be called whether or not an _actual_
359  * write to the fpstate occurred.
360  */
361 void fpu__current_fpstate_write_end(void)
362 {
363 	struct fpu *fpu = &current->thread.fpu;
364 
365 	/*
366 	 * 'fpu' now has an updated copy of the state, but the
367 	 * registers may still be out of date.  Update them with
368 	 * an XRSTOR if they are active.
369 	 */
370 	if (fpregs_active())
371 		copy_kernel_to_fpregs(&fpu->state);
372 
373 	/*
374 	 * Our update is done and the fpregs/fpstate are in sync
375 	 * if necessary.  Context switches can happen again.
376 	 */
377 	preempt_enable();
378 }
379 
380 /*
381  * 'fpu__restore()' is called to copy FPU registers from
382  * the FPU fpstate to the live hw registers and to activate
383  * access to the hardware registers, so that FPU instructions
384  * can be used afterwards.
385  *
386  * Must be called with kernel preemption disabled (for example
387  * with local interrupts disabled, as it is in the case of
388  * do_device_not_available()).
389  */
390 void fpu__restore(struct fpu *fpu)
391 {
392 	fpu__activate_curr(fpu);
393 
394 	/* Avoid __kernel_fpu_begin() right after fpregs_activate() */
395 	kernel_fpu_disable();
396 	trace_x86_fpu_before_restore(fpu);
397 	fpregs_activate(fpu);
398 	copy_kernel_to_fpregs(&fpu->state);
399 	trace_x86_fpu_after_restore(fpu);
400 	kernel_fpu_enable();
401 }
402 EXPORT_SYMBOL_GPL(fpu__restore);
403 
404 /*
405  * Drops current FPU state: deactivates the fpregs and
406  * the fpstate. NOTE: it still leaves previous contents
407  * in the fpregs in the eager-FPU case.
408  *
409  * This function can be used in cases where we know that
410  * a state-restore is coming: either an explicit one,
411  * or a reschedule.
412  */
413 void fpu__drop(struct fpu *fpu)
414 {
415 	preempt_disable();
416 
417 	if (fpu->fpregs_active) {
418 		/* Ignore delayed exceptions from user space */
419 		asm volatile("1: fwait\n"
420 			     "2:\n"
421 			     _ASM_EXTABLE(1b, 2b));
422 		fpregs_deactivate(fpu);
423 	}
424 
425 	fpu->fpstate_active = 0;
426 
427 	trace_x86_fpu_dropped(fpu);
428 
429 	preempt_enable();
430 }
431 
432 /*
433  * Clear FPU registers by setting them up from
434  * the init fpstate:
435  */
436 static inline void copy_init_fpstate_to_fpregs(void)
437 {
438 	if (use_xsave())
439 		copy_kernel_to_xregs(&init_fpstate.xsave, -1);
440 	else if (static_cpu_has(X86_FEATURE_FXSR))
441 		copy_kernel_to_fxregs(&init_fpstate.fxsave);
442 	else
443 		copy_kernel_to_fregs(&init_fpstate.fsave);
444 
445 	if (boot_cpu_has(X86_FEATURE_OSPKE))
446 		copy_init_pkru_to_fpregs();
447 }
448 
449 /*
450  * Clear the FPU state back to init state.
451  *
452  * Called by sys_execve(), by the signal handler code and by various
453  * error paths.
454  */
455 void fpu__clear(struct fpu *fpu)
456 {
457 	WARN_ON_FPU(fpu != &current->thread.fpu); /* Almost certainly an anomaly */
458 
459 	fpu__drop(fpu);
460 
461 	/*
462 	 * Make sure fpstate is cleared and initialized.
463 	 */
464 	if (static_cpu_has(X86_FEATURE_FPU)) {
465 		fpu__activate_curr(fpu);
466 		user_fpu_begin();
467 		copy_init_fpstate_to_fpregs();
468 	}
469 }
470 
471 /*
472  * x87 math exception handling:
473  */
474 
475 int fpu__exception_code(struct fpu *fpu, int trap_nr)
476 {
477 	int err;
478 
479 	if (trap_nr == X86_TRAP_MF) {
480 		unsigned short cwd, swd;
481 		/*
482 		 * (~cwd & swd) will mask out exceptions that are not set to unmasked
483 		 * status.  0x3f is the exception bits in these regs, 0x200 is the
484 		 * C1 reg you need in case of a stack fault, 0x040 is the stack
485 		 * fault bit.  We should only be taking one exception at a time,
486 		 * so if this combination doesn't produce any single exception,
487 		 * then we have a bad program that isn't synchronizing its FPU usage
488 		 * and it will suffer the consequences since we won't be able to
489 		 * fully reproduce the context of the exception.
490 		 */
491 		if (boot_cpu_has(X86_FEATURE_FXSR)) {
492 			cwd = fpu->state.fxsave.cwd;
493 			swd = fpu->state.fxsave.swd;
494 		} else {
495 			cwd = (unsigned short)fpu->state.fsave.cwd;
496 			swd = (unsigned short)fpu->state.fsave.swd;
497 		}
498 
499 		err = swd & ~cwd;
500 	} else {
501 		/*
502 		 * The SIMD FPU exceptions are handled a little differently, as there
503 		 * is only a single status/control register.  Thus, to determine which
504 		 * unmasked exception was caught we must mask the exception mask bits
505 		 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
506 		 */
507 		unsigned short mxcsr = MXCSR_DEFAULT;
508 
509 		if (boot_cpu_has(X86_FEATURE_XMM))
510 			mxcsr = fpu->state.fxsave.mxcsr;
511 
512 		err = ~(mxcsr >> 7) & mxcsr;
513 	}
514 
515 	if (err & 0x001) {	/* Invalid op */
516 		/*
517 		 * swd & 0x240 == 0x040: Stack Underflow
518 		 * swd & 0x240 == 0x240: Stack Overflow
519 		 * User must clear the SF bit (0x40) if set
520 		 */
521 		return FPE_FLTINV;
522 	} else if (err & 0x004) { /* Divide by Zero */
523 		return FPE_FLTDIV;
524 	} else if (err & 0x008) { /* Overflow */
525 		return FPE_FLTOVF;
526 	} else if (err & 0x012) { /* Denormal, Underflow */
527 		return FPE_FLTUND;
528 	} else if (err & 0x020) { /* Precision */
529 		return FPE_FLTRES;
530 	}
531 
532 	/*
533 	 * If we're using IRQ 13, or supposedly even some trap
534 	 * X86_TRAP_MF implementations, it's possible
535 	 * we get a spurious trap, which is not an error.
536 	 */
537 	return 0;
538 }
539