Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

x86/fpu: Reset MXCSR to default in kernel_fpu_begin()

Previously, kernel floating point code would run with the MXCSR control
register value last set by userland code by the thread that was active
on the CPU core just before kernel call. This could affect calculation
results if rounding mode was changed, or a crash if a FPU/SIMD exception
was unmasked.

Restore MXCSR to the kernel's default value.

[ bp: Carve out from a bigger patch by Petteri, add feature check, add
FNINIT call too (amluto). ]

Signed-off-by: Petteri Aimonen <jpa@git.mail.kapsi.fi>
Signed-off-by: Borislav Petkov <bp@suse.de>
Link: https://bugzilla.kernel.org/show_bug.cgi?id=207979
Link: https://lkml.kernel.org/r/20200624114646.28953-2-bp@alien8.de

authored by

Petteri Aimonen and committed by
Borislav Petkov
7ad81676 48778464

+11
+5
arch/x86/include/asm/fpu/internal.h
··· 623 623 * MXCSR and XCR definitions: 624 624 */ 625 625 626 + static inline void ldmxcsr(u32 mxcsr) 627 + { 628 + asm volatile("ldmxcsr %0" :: "m" (mxcsr)); 629 + } 630 + 626 631 extern unsigned int mxcsr_feature_mask; 627 632 628 633 #define XCR_XFEATURE_ENABLED_MASK 0x00000000
+6
arch/x86/kernel/fpu/core.c
··· 101 101 copy_fpregs_to_fpstate(&current->thread.fpu); 102 102 } 103 103 __cpu_invalidate_fpregs_state(); 104 + 105 + if (boot_cpu_has(X86_FEATURE_XMM)) 106 + ldmxcsr(MXCSR_DEFAULT); 107 + 108 + if (boot_cpu_has(X86_FEATURE_FPU)) 109 + asm volatile ("fninit"); 104 110 } 105 111 EXPORT_SYMBOL_GPL(kernel_fpu_begin); 106 112