1 // SPDX-License-Identifier: GPL-2.0-only
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  */
9 #include <asm/fpu/api.h>
10 #include <asm/fpu/regset.h>
11 #include <asm/fpu/sched.h>
12 #include <asm/fpu/signal.h>
13 #include <asm/fpu/types.h>
14 #include <asm/traps.h>
15 #include <asm/irq_regs.h>
16 
17 #include <linux/hardirq.h>
18 #include <linux/pkeys.h>
19 #include <linux/vmalloc.h>
20 
21 #include "context.h"
22 #include "internal.h"
23 #include "legacy.h"
24 #include "xstate.h"
25 
26 #define CREATE_TRACE_POINTS
27 #include <asm/trace/fpu.h>
28 
29 #ifdef CONFIG_X86_64
30 DEFINE_STATIC_KEY_FALSE(__fpu_state_size_dynamic);
31 DEFINE_PER_CPU(u64, xfd_state);
32 #endif
33 
34 /* The FPU state configuration data for kernel and user space */
35 struct fpu_state_config	fpu_kernel_cfg __ro_after_init;
36 struct fpu_state_config fpu_user_cfg __ro_after_init;
37 
38 /*
39  * Represents the initial FPU state. It's mostly (but not completely) zeroes,
40  * depending on the FPU hardware format:
41  */
42 struct fpstate init_fpstate __ro_after_init;
43 
44 /*
45  * Track whether the kernel is using the FPU state
46  * currently.
47  *
48  * This flag is used:
49  *
50  *   - by IRQ context code to potentially use the FPU
51  *     if it's unused.
52  *
53  *   - to debug kernel_fpu_begin()/end() correctness
54  */
55 static DEFINE_PER_CPU(bool, in_kernel_fpu);
56 
57 /*
58  * Track which context is using the FPU on the CPU:
59  */
60 DEFINE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
61 
kernel_fpu_disabled(void)62 static bool kernel_fpu_disabled(void)
63 {
64 	return this_cpu_read(in_kernel_fpu);
65 }
66 
interrupted_kernel_fpu_idle(void)67 static bool interrupted_kernel_fpu_idle(void)
68 {
69 	return !kernel_fpu_disabled();
70 }
71 
72 /*
73  * Were we in user mode (or vm86 mode) when we were
74  * interrupted?
75  *
76  * Doing kernel_fpu_begin/end() is ok if we are running
77  * in an interrupt context from user mode - we'll just
78  * save the FPU state as required.
79  */
interrupted_user_mode(void)80 static bool interrupted_user_mode(void)
81 {
82 	struct pt_regs *regs = get_irq_regs();
83 	return regs && user_mode(regs);
84 }
85 
86 /*
87  * Can we use the FPU in kernel mode with the
88  * whole "kernel_fpu_begin/end()" sequence?
89  *
90  * It's always ok in process context (ie "not interrupt")
91  * but it is sometimes ok even from an irq.
92  */
irq_fpu_usable(void)93 bool irq_fpu_usable(void)
94 {
95 	return !in_interrupt() ||
96 		interrupted_user_mode() ||
97 		interrupted_kernel_fpu_idle();
98 }
99 EXPORT_SYMBOL(irq_fpu_usable);
100 
101 /*
102  * Save the FPU register state in fpu->fpstate->regs. The register state is
103  * preserved.
104  *
105  * Must be called with fpregs_lock() held.
106  *
107  * The legacy FNSAVE instruction clears all FPU state unconditionally, so
108  * register state has to be reloaded. That might be a pointless exercise
109  * when the FPU is going to be used by another task right after that. But
110  * this only affects 20+ years old 32bit systems and avoids conditionals all
111  * over the place.
112  *
113  * FXSAVE and all XSAVE variants preserve the FPU register state.
114  */
save_fpregs_to_fpstate(struct fpu * fpu)115 void save_fpregs_to_fpstate(struct fpu *fpu)
116 {
117 	if (likely(use_xsave())) {
118 		os_xsave(fpu->fpstate);
119 
120 		/*
121 		 * AVX512 state is tracked here because its use is
122 		 * known to slow the max clock speed of the core.
123 		 */
124 		if (fpu->fpstate->regs.xsave.header.xfeatures & XFEATURE_MASK_AVX512)
125 			fpu->avx512_timestamp = jiffies;
126 		return;
127 	}
128 
129 	if (likely(use_fxsr())) {
130 		fxsave(&fpu->fpstate->regs.fxsave);
131 		return;
132 	}
133 
134 	/*
135 	 * Legacy FPU register saving, FNSAVE always clears FPU registers,
136 	 * so we have to reload them from the memory state.
137 	 */
138 	asm volatile("fnsave %[fp]; fwait" : [fp] "=m" (fpu->fpstate->regs.fsave));
139 	frstor(&fpu->fpstate->regs.fsave);
140 }
141 
restore_fpregs_from_fpstate(struct fpstate * fpstate,u64 mask)142 void restore_fpregs_from_fpstate(struct fpstate *fpstate, u64 mask)
143 {
144 	/*
145 	 * AMD K7/K8 and later CPUs up to Zen don't save/restore
146 	 * FDP/FIP/FOP unless an exception is pending. Clear the x87 state
147 	 * here by setting it to fixed values.  "m" is a random variable
148 	 * that should be in L1.
149 	 */
150 	if (unlikely(static_cpu_has_bug(X86_BUG_FXSAVE_LEAK))) {
151 		asm volatile(
152 			"fnclex\n\t"
153 			"emms\n\t"
154 			"fildl %P[addr]"	/* set F?P to defined value */
155 			: : [addr] "m" (fpstate));
156 	}
157 
158 	if (use_xsave()) {
159 		/*
160 		 * Dynamically enabled features are enabled in XCR0, but
161 		 * usage requires also that the corresponding bits in XFD
162 		 * are cleared.  If the bits are set then using a related
163 		 * instruction will raise #NM. This allows to do the
164 		 * allocation of the larger FPU buffer lazy from #NM or if
165 		 * the task has no permission to kill it which would happen
166 		 * via #UD if the feature is disabled in XCR0.
167 		 *
168 		 * XFD state is following the same life time rules as
169 		 * XSTATE and to restore state correctly XFD has to be
170 		 * updated before XRSTORS otherwise the component would
171 		 * stay in or go into init state even if the bits are set
172 		 * in fpstate::regs::xsave::xfeatures.
173 		 */
174 		xfd_update_state(fpstate);
175 
176 		/*
177 		 * Restoring state always needs to modify all features
178 		 * which are in @mask even if the current task cannot use
179 		 * extended features.
180 		 *
181 		 * So fpstate->xfeatures cannot be used here, because then
182 		 * a feature for which the task has no permission but was
183 		 * used by the previous task would not go into init state.
184 		 */
185 		mask = fpu_kernel_cfg.max_features & mask;
186 
187 		os_xrstor(fpstate, mask);
188 	} else {
189 		if (use_fxsr())
190 			fxrstor(&fpstate->regs.fxsave);
191 		else
192 			frstor(&fpstate->regs.fsave);
193 	}
194 }
195 
fpu_reset_from_exception_fixup(void)196 void fpu_reset_from_exception_fixup(void)
197 {
198 	restore_fpregs_from_fpstate(&init_fpstate, XFEATURE_MASK_FPSTATE);
199 }
200 
201 #if IS_ENABLED(CONFIG_KVM)
202 static void __fpstate_reset(struct fpstate *fpstate);
203 
fpu_alloc_guest_fpstate(struct fpu_guest * gfpu)204 bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu)
205 {
206 	struct fpstate *fpstate;
207 	unsigned int size;
208 
209 	size = fpu_user_cfg.default_size + ALIGN(offsetof(struct fpstate, regs), 64);
210 	fpstate = vzalloc(size);
211 	if (!fpstate)
212 		return false;
213 
214 	__fpstate_reset(fpstate);
215 	fpstate_init_user(fpstate);
216 	fpstate->is_valloc	= true;
217 	fpstate->is_guest	= true;
218 
219 	gfpu->fpstate = fpstate;
220 	return true;
221 }
222 EXPORT_SYMBOL_GPL(fpu_alloc_guest_fpstate);
223 
fpu_free_guest_fpstate(struct fpu_guest * gfpu)224 void fpu_free_guest_fpstate(struct fpu_guest *gfpu)
225 {
226 	struct fpstate *fps = gfpu->fpstate;
227 
228 	if (!fps)
229 		return;
230 
231 	if (WARN_ON_ONCE(!fps->is_valloc || !fps->is_guest || fps->in_use))
232 		return;
233 
234 	gfpu->fpstate = NULL;
235 	vfree(fps);
236 }
237 EXPORT_SYMBOL_GPL(fpu_free_guest_fpstate);
238 
fpu_swap_kvm_fpstate(struct fpu_guest * guest_fpu,bool enter_guest)239 int fpu_swap_kvm_fpstate(struct fpu_guest *guest_fpu, bool enter_guest)
240 {
241 	struct fpstate *guest_fps = guest_fpu->fpstate;
242 	struct fpu *fpu = &current->thread.fpu;
243 	struct fpstate *cur_fps = fpu->fpstate;
244 
245 	fpregs_lock();
246 	if (!cur_fps->is_confidential && !test_thread_flag(TIF_NEED_FPU_LOAD))
247 		save_fpregs_to_fpstate(fpu);
248 
249 	/* Swap fpstate */
250 	if (enter_guest) {
251 		fpu->__task_fpstate = cur_fps;
252 		fpu->fpstate = guest_fps;
253 		guest_fps->in_use = true;
254 	} else {
255 		guest_fps->in_use = false;
256 		fpu->fpstate = fpu->__task_fpstate;
257 		fpu->__task_fpstate = NULL;
258 	}
259 
260 	cur_fps = fpu->fpstate;
261 
262 	if (!cur_fps->is_confidential) {
263 		/* Includes XFD update */
264 		restore_fpregs_from_fpstate(cur_fps, XFEATURE_MASK_FPSTATE);
265 	} else {
266 		/*
267 		 * XSTATE is restored by firmware from encrypted
268 		 * memory. Make sure XFD state is correct while
269 		 * running with guest fpstate
270 		 */
271 		xfd_update_state(cur_fps);
272 	}
273 
274 	fpregs_mark_activate();
275 	fpregs_unlock();
276 	return 0;
277 }
278 EXPORT_SYMBOL_GPL(fpu_swap_kvm_fpstate);
279 
fpu_copy_guest_fpstate_to_uabi(struct fpu_guest * gfpu,void * buf,unsigned int size,u32 pkru)280 void fpu_copy_guest_fpstate_to_uabi(struct fpu_guest *gfpu, void *buf,
281 				    unsigned int size, u32 pkru)
282 {
283 	struct fpstate *kstate = gfpu->fpstate;
284 	union fpregs_state *ustate = buf;
285 	struct membuf mb = { .p = buf, .left = size };
286 
287 	if (cpu_feature_enabled(X86_FEATURE_XSAVE)) {
288 		__copy_xstate_to_uabi_buf(mb, kstate, pkru, XSTATE_COPY_XSAVE);
289 	} else {
290 		memcpy(&ustate->fxsave, &kstate->regs.fxsave,
291 		       sizeof(ustate->fxsave));
292 		/* Make it restorable on a XSAVE enabled host */
293 		ustate->xsave.header.xfeatures = XFEATURE_MASK_FPSSE;
294 	}
295 }
296 EXPORT_SYMBOL_GPL(fpu_copy_guest_fpstate_to_uabi);
297 
fpu_copy_uabi_to_guest_fpstate(struct fpu_guest * gfpu,const void * buf,u64 xcr0,u32 * vpkru)298 int fpu_copy_uabi_to_guest_fpstate(struct fpu_guest *gfpu, const void *buf,
299 				   u64 xcr0, u32 *vpkru)
300 {
301 	struct fpstate *kstate = gfpu->fpstate;
302 	const union fpregs_state *ustate = buf;
303 	struct pkru_state *xpkru;
304 	int ret;
305 
306 	if (!cpu_feature_enabled(X86_FEATURE_XSAVE)) {
307 		if (ustate->xsave.header.xfeatures & ~XFEATURE_MASK_FPSSE)
308 			return -EINVAL;
309 		if (ustate->fxsave.mxcsr & ~mxcsr_feature_mask)
310 			return -EINVAL;
311 		memcpy(&kstate->regs.fxsave, &ustate->fxsave, sizeof(ustate->fxsave));
312 		return 0;
313 	}
314 
315 	if (ustate->xsave.header.xfeatures & ~xcr0)
316 		return -EINVAL;
317 
318 	ret = copy_uabi_from_kernel_to_xstate(kstate, ustate);
319 	if (ret)
320 		return ret;
321 
322 	/* Retrieve PKRU if not in init state */
323 	if (kstate->regs.xsave.header.xfeatures & XFEATURE_MASK_PKRU) {
324 		xpkru = get_xsave_addr(&kstate->regs.xsave, XFEATURE_PKRU);
325 		*vpkru = xpkru->pkru;
326 	}
327 
328 	/* Ensure that XCOMP_BV is set up for XSAVES */
329 	xstate_init_xcomp_bv(&kstate->regs.xsave, kstate->xfeatures);
330 	return 0;
331 }
332 EXPORT_SYMBOL_GPL(fpu_copy_uabi_to_guest_fpstate);
333 #endif /* CONFIG_KVM */
334 
kernel_fpu_begin_mask(unsigned int kfpu_mask)335 void kernel_fpu_begin_mask(unsigned int kfpu_mask)
336 {
337 	preempt_disable();
338 
339 	WARN_ON_FPU(!irq_fpu_usable());
340 	WARN_ON_FPU(this_cpu_read(in_kernel_fpu));
341 
342 	this_cpu_write(in_kernel_fpu, true);
343 
344 	if (!(current->flags & PF_KTHREAD) &&
345 	    !test_thread_flag(TIF_NEED_FPU_LOAD)) {
346 		set_thread_flag(TIF_NEED_FPU_LOAD);
347 		save_fpregs_to_fpstate(&current->thread.fpu);
348 	}
349 	__cpu_invalidate_fpregs_state();
350 
351 	/* Put sane initial values into the control registers. */
352 	if (likely(kfpu_mask & KFPU_MXCSR) && boot_cpu_has(X86_FEATURE_XMM))
353 		ldmxcsr(MXCSR_DEFAULT);
354 
355 	if (unlikely(kfpu_mask & KFPU_387) && boot_cpu_has(X86_FEATURE_FPU))
356 		asm volatile ("fninit");
357 }
358 EXPORT_SYMBOL_GPL(kernel_fpu_begin_mask);
359 
kernel_fpu_end(void)360 void kernel_fpu_end(void)
361 {
362 	WARN_ON_FPU(!this_cpu_read(in_kernel_fpu));
363 
364 	this_cpu_write(in_kernel_fpu, false);
365 	preempt_enable();
366 }
367 EXPORT_SYMBOL_GPL(kernel_fpu_end);
368 
369 /*
370  * Sync the FPU register state to current's memory register state when the
371  * current task owns the FPU. The hardware register state is preserved.
372  */
fpu_sync_fpstate(struct fpu * fpu)373 void fpu_sync_fpstate(struct fpu *fpu)
374 {
375 	WARN_ON_FPU(fpu != &current->thread.fpu);
376 
377 	fpregs_lock();
378 	trace_x86_fpu_before_save(fpu);
379 
380 	if (!test_thread_flag(TIF_NEED_FPU_LOAD))
381 		save_fpregs_to_fpstate(fpu);
382 
383 	trace_x86_fpu_after_save(fpu);
384 	fpregs_unlock();
385 }
386 
init_fpstate_copy_size(void)387 static inline unsigned int init_fpstate_copy_size(void)
388 {
389 	if (!use_xsave())
390 		return fpu_kernel_cfg.default_size;
391 
392 	/* XSAVE(S) just needs the legacy and the xstate header part */
393 	return sizeof(init_fpstate.regs.xsave);
394 }
395 
fpstate_init_fxstate(struct fpstate * fpstate)396 static inline void fpstate_init_fxstate(struct fpstate *fpstate)
397 {
398 	fpstate->regs.fxsave.cwd = 0x37f;
399 	fpstate->regs.fxsave.mxcsr = MXCSR_DEFAULT;
400 }
401 
402 /*
403  * Legacy x87 fpstate state init:
404  */
fpstate_init_fstate(struct fpstate * fpstate)405 static inline void fpstate_init_fstate(struct fpstate *fpstate)
406 {
407 	fpstate->regs.fsave.cwd = 0xffff037fu;
408 	fpstate->regs.fsave.swd = 0xffff0000u;
409 	fpstate->regs.fsave.twd = 0xffffffffu;
410 	fpstate->regs.fsave.fos = 0xffff0000u;
411 }
412 
413 /*
414  * Used in two places:
415  * 1) Early boot to setup init_fpstate for non XSAVE systems
416  * 2) fpu_init_fpstate_user() which is invoked from KVM
417  */
fpstate_init_user(struct fpstate * fpstate)418 void fpstate_init_user(struct fpstate *fpstate)
419 {
420 	if (!cpu_feature_enabled(X86_FEATURE_FPU)) {
421 		fpstate_init_soft(&fpstate->regs.soft);
422 		return;
423 	}
424 
425 	xstate_init_xcomp_bv(&fpstate->regs.xsave, fpstate->xfeatures);
426 
427 	if (cpu_feature_enabled(X86_FEATURE_FXSR))
428 		fpstate_init_fxstate(fpstate);
429 	else
430 		fpstate_init_fstate(fpstate);
431 }
432 
__fpstate_reset(struct fpstate * fpstate)433 static void __fpstate_reset(struct fpstate *fpstate)
434 {
435 	/* Initialize sizes and feature masks */
436 	fpstate->size		= fpu_kernel_cfg.default_size;
437 	fpstate->user_size	= fpu_user_cfg.default_size;
438 	fpstate->xfeatures	= fpu_kernel_cfg.default_features;
439 	fpstate->user_xfeatures	= fpu_user_cfg.default_features;
440 	fpstate->xfd		= init_fpstate.xfd;
441 }
442 
fpstate_reset(struct fpu * fpu)443 void fpstate_reset(struct fpu *fpu)
444 {
445 	/* Set the fpstate pointer to the default fpstate */
446 	fpu->fpstate = &fpu->__fpstate;
447 	__fpstate_reset(fpu->fpstate);
448 
449 	/* Initialize the permission related info in fpu */
450 	fpu->perm.__state_perm		= fpu_kernel_cfg.default_features;
451 	fpu->perm.__state_size		= fpu_kernel_cfg.default_size;
452 	fpu->perm.__user_state_size	= fpu_user_cfg.default_size;
453 }
454 
fpu_inherit_perms(struct fpu * dst_fpu)455 static inline void fpu_inherit_perms(struct fpu *dst_fpu)
456 {
457 	if (fpu_state_size_dynamic()) {
458 		struct fpu *src_fpu = &current->group_leader->thread.fpu;
459 
460 		spin_lock_irq(&current->sighand->siglock);
461 		/* Fork also inherits the permissions of the parent */
462 		dst_fpu->perm = src_fpu->perm;
463 		spin_unlock_irq(&current->sighand->siglock);
464 	}
465 }
466 
467 /* Clone current's FPU state on fork */
fpu_clone(struct task_struct * dst,unsigned long clone_flags)468 int fpu_clone(struct task_struct *dst, unsigned long clone_flags)
469 {
470 	struct fpu *src_fpu = &current->thread.fpu;
471 	struct fpu *dst_fpu = &dst->thread.fpu;
472 
473 	/* The new task's FPU state cannot be valid in the hardware. */
474 	dst_fpu->last_cpu = -1;
475 
476 	fpstate_reset(dst_fpu);
477 
478 	if (!cpu_feature_enabled(X86_FEATURE_FPU))
479 		return 0;
480 
481 	/*
482 	 * Enforce reload for user space tasks and prevent kernel threads
483 	 * from trying to save the FPU registers on context switch.
484 	 */
485 	set_tsk_thread_flag(dst, TIF_NEED_FPU_LOAD);
486 
487 	/*
488 	 * No FPU state inheritance for kernel threads and IO
489 	 * worker threads.
490 	 */
491 	if (dst->flags & (PF_KTHREAD | PF_IO_WORKER)) {
492 		/* Clear out the minimal state */
493 		memcpy(&dst_fpu->fpstate->regs, &init_fpstate.regs,
494 		       init_fpstate_copy_size());
495 		return 0;
496 	}
497 
498 	/*
499 	 * If a new feature is added, ensure all dynamic features are
500 	 * caller-saved from here!
501 	 */
502 	BUILD_BUG_ON(XFEATURE_MASK_USER_DYNAMIC != XFEATURE_MASK_XTILE_DATA);
503 
504 	/*
505 	 * Save the default portion of the current FPU state into the
506 	 * clone. Assume all dynamic features to be defined as caller-
507 	 * saved, which enables skipping both the expansion of fpstate
508 	 * and the copying of any dynamic state.
509 	 *
510 	 * Do not use memcpy() when TIF_NEED_FPU_LOAD is set because
511 	 * copying is not valid when current uses non-default states.
512 	 */
513 	fpregs_lock();
514 	if (test_thread_flag(TIF_NEED_FPU_LOAD))
515 		fpregs_restore_userregs();
516 	save_fpregs_to_fpstate(dst_fpu);
517 	if (!(clone_flags & CLONE_THREAD))
518 		fpu_inherit_perms(dst_fpu);
519 	fpregs_unlock();
520 
521 	trace_x86_fpu_copy_src(src_fpu);
522 	trace_x86_fpu_copy_dst(dst_fpu);
523 
524 	return 0;
525 }
526 
527 /*
528  * Whitelist the FPU register state embedded into task_struct for hardened
529  * usercopy.
530  */
fpu_thread_struct_whitelist(unsigned long * offset,unsigned long * size)531 void fpu_thread_struct_whitelist(unsigned long *offset, unsigned long *size)
532 {
533 	*offset = offsetof(struct thread_struct, fpu.__fpstate.regs);
534 	*size = fpu_kernel_cfg.default_size;
535 }
536 
537 /*
538  * Drops current FPU state: deactivates the fpregs and
539  * the fpstate. NOTE: it still leaves previous contents
540  * in the fpregs in the eager-FPU case.
541  *
542  * This function can be used in cases where we know that
543  * a state-restore is coming: either an explicit one,
544  * or a reschedule.
545  */
fpu__drop(struct fpu * fpu)546 void fpu__drop(struct fpu *fpu)
547 {
548 	preempt_disable();
549 
550 	if (fpu == &current->thread.fpu) {
551 		/* Ignore delayed exceptions from user space */
552 		asm volatile("1: fwait\n"
553 			     "2:\n"
554 			     _ASM_EXTABLE(1b, 2b));
555 		fpregs_deactivate(fpu);
556 	}
557 
558 	trace_x86_fpu_dropped(fpu);
559 
560 	preempt_enable();
561 }
562 
563 /*
564  * Clear FPU registers by setting them up from the init fpstate.
565  * Caller must do fpregs_[un]lock() around it.
566  */
restore_fpregs_from_init_fpstate(u64 features_mask)567 static inline void restore_fpregs_from_init_fpstate(u64 features_mask)
568 {
569 	if (use_xsave())
570 		os_xrstor(&init_fpstate, features_mask);
571 	else if (use_fxsr())
572 		fxrstor(&init_fpstate.regs.fxsave);
573 	else
574 		frstor(&init_fpstate.regs.fsave);
575 
576 	pkru_write_default();
577 }
578 
579 /*
580  * Reset current->fpu memory state to the init values.
581  */
fpu_reset_fpregs(void)582 static void fpu_reset_fpregs(void)
583 {
584 	struct fpu *fpu = &current->thread.fpu;
585 
586 	fpregs_lock();
587 	fpu__drop(fpu);
588 	/*
589 	 * This does not change the actual hardware registers. It just
590 	 * resets the memory image and sets TIF_NEED_FPU_LOAD so a
591 	 * subsequent return to usermode will reload the registers from the
592 	 * task's memory image.
593 	 *
594 	 * Do not use fpstate_init() here. Just copy init_fpstate which has
595 	 * the correct content already except for PKRU.
596 	 *
597 	 * PKRU handling does not rely on the xstate when restoring for
598 	 * user space as PKRU is eagerly written in switch_to() and
599 	 * flush_thread().
600 	 */
601 	memcpy(&fpu->fpstate->regs, &init_fpstate.regs, init_fpstate_copy_size());
602 	set_thread_flag(TIF_NEED_FPU_LOAD);
603 	fpregs_unlock();
604 }
605 
606 /*
607  * Reset current's user FPU states to the init states.  current's
608  * supervisor states, if any, are not modified by this function.  The
609  * caller guarantees that the XSTATE header in memory is intact.
610  */
fpu__clear_user_states(struct fpu * fpu)611 void fpu__clear_user_states(struct fpu *fpu)
612 {
613 	WARN_ON_FPU(fpu != &current->thread.fpu);
614 
615 	fpregs_lock();
616 	if (!cpu_feature_enabled(X86_FEATURE_FPU)) {
617 		fpu_reset_fpregs();
618 		fpregs_unlock();
619 		return;
620 	}
621 
622 	/*
623 	 * Ensure that current's supervisor states are loaded into their
624 	 * corresponding registers.
625 	 */
626 	if (xfeatures_mask_supervisor() &&
627 	    !fpregs_state_valid(fpu, smp_processor_id()))
628 		os_xrstor_supervisor(fpu->fpstate);
629 
630 	/* Reset user states in registers. */
631 	restore_fpregs_from_init_fpstate(XFEATURE_MASK_USER_RESTORE);
632 
633 	/*
634 	 * Now all FPU registers have their desired values.  Inform the FPU
635 	 * state machine that current's FPU registers are in the hardware
636 	 * registers. The memory image does not need to be updated because
637 	 * any operation relying on it has to save the registers first when
638 	 * current's FPU is marked active.
639 	 */
640 	fpregs_mark_activate();
641 	fpregs_unlock();
642 }
643 
fpu_flush_thread(void)644 void fpu_flush_thread(void)
645 {
646 	fpstate_reset(&current->thread.fpu);
647 	fpu_reset_fpregs();
648 }
649 /*
650  * Load FPU context before returning to userspace.
651  */
switch_fpu_return(void)652 void switch_fpu_return(void)
653 {
654 	if (!static_cpu_has(X86_FEATURE_FPU))
655 		return;
656 
657 	fpregs_restore_userregs();
658 }
659 EXPORT_SYMBOL_GPL(switch_fpu_return);
660 
661 #ifdef CONFIG_X86_DEBUG_FPU
662 /*
663  * If current FPU state according to its tracking (loaded FPU context on this
664  * CPU) is not valid then we must have TIF_NEED_FPU_LOAD set so the context is
665  * loaded on return to userland.
666  */
fpregs_assert_state_consistent(void)667 void fpregs_assert_state_consistent(void)
668 {
669 	struct fpu *fpu = &current->thread.fpu;
670 
671 	if (test_thread_flag(TIF_NEED_FPU_LOAD))
672 		return;
673 
674 	WARN_ON_FPU(!fpregs_state_valid(fpu, smp_processor_id()));
675 }
676 EXPORT_SYMBOL_GPL(fpregs_assert_state_consistent);
677 #endif
678 
fpregs_mark_activate(void)679 void fpregs_mark_activate(void)
680 {
681 	struct fpu *fpu = &current->thread.fpu;
682 
683 	fpregs_activate(fpu);
684 	fpu->last_cpu = smp_processor_id();
685 	clear_thread_flag(TIF_NEED_FPU_LOAD);
686 }
687 
688 /*
689  * x87 math exception handling:
690  */
691 
fpu__exception_code(struct fpu * fpu,int trap_nr)692 int fpu__exception_code(struct fpu *fpu, int trap_nr)
693 {
694 	int err;
695 
696 	if (trap_nr == X86_TRAP_MF) {
697 		unsigned short cwd, swd;
698 		/*
699 		 * (~cwd & swd) will mask out exceptions that are not set to unmasked
700 		 * status.  0x3f is the exception bits in these regs, 0x200 is the
701 		 * C1 reg you need in case of a stack fault, 0x040 is the stack
702 		 * fault bit.  We should only be taking one exception at a time,
703 		 * so if this combination doesn't produce any single exception,
704 		 * then we have a bad program that isn't synchronizing its FPU usage
705 		 * and it will suffer the consequences since we won't be able to
706 		 * fully reproduce the context of the exception.
707 		 */
708 		if (boot_cpu_has(X86_FEATURE_FXSR)) {
709 			cwd = fpu->fpstate->regs.fxsave.cwd;
710 			swd = fpu->fpstate->regs.fxsave.swd;
711 		} else {
712 			cwd = (unsigned short)fpu->fpstate->regs.fsave.cwd;
713 			swd = (unsigned short)fpu->fpstate->regs.fsave.swd;
714 		}
715 
716 		err = swd & ~cwd;
717 	} else {
718 		/*
719 		 * The SIMD FPU exceptions are handled a little differently, as there
720 		 * is only a single status/control register.  Thus, to determine which
721 		 * unmasked exception was caught we must mask the exception mask bits
722 		 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
723 		 */
724 		unsigned short mxcsr = MXCSR_DEFAULT;
725 
726 		if (boot_cpu_has(X86_FEATURE_XMM))
727 			mxcsr = fpu->fpstate->regs.fxsave.mxcsr;
728 
729 		err = ~(mxcsr >> 7) & mxcsr;
730 	}
731 
732 	if (err & 0x001) {	/* Invalid op */
733 		/*
734 		 * swd & 0x240 == 0x040: Stack Underflow
735 		 * swd & 0x240 == 0x240: Stack Overflow
736 		 * User must clear the SF bit (0x40) if set
737 		 */
738 		return FPE_FLTINV;
739 	} else if (err & 0x004) { /* Divide by Zero */
740 		return FPE_FLTDIV;
741 	} else if (err & 0x008) { /* Overflow */
742 		return FPE_FLTOVF;
743 	} else if (err & 0x012) { /* Denormal, Underflow */
744 		return FPE_FLTUND;
745 	} else if (err & 0x020) { /* Precision */
746 		return FPE_FLTRES;
747 	}
748 
749 	/*
750 	 * If we're using IRQ 13, or supposedly even some trap
751 	 * X86_TRAP_MF implementations, it's possible
752 	 * we get a spurious trap, which is not an error.
753 	 */
754 	return 0;
755 }
756