Skip to content

Commit 7ad8167

Browse files
PetteriAimonensuryasaimadhu
authored andcommitted
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 <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Link: https://bugzilla.kernel.org/show_bug.cgi?id=207979 Link: https://lkml.kernel.org/r/[email protected]
1 parent 4877846 commit 7ad8167

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

arch/x86/include/asm/fpu/internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,11 @@ static inline void switch_fpu_finish(struct fpu *new_fpu)
623623
* MXCSR and XCR definitions:
624624
*/
625625

626+
static inline void ldmxcsr(u32 mxcsr)
627+
{
628+
asm volatile("ldmxcsr %0" :: "m" (mxcsr));
629+
}
630+
626631
extern unsigned int mxcsr_feature_mask;
627632

628633
#define XCR_XFEATURE_ENABLED_MASK 0x00000000

arch/x86/kernel/fpu/core.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ void kernel_fpu_begin(void)
101101
copy_fpregs_to_fpstate(&current->thread.fpu);
102102
}
103103
__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");
104110
}
105111
EXPORT_SYMBOL_GPL(kernel_fpu_begin);
106112

0 commit comments

Comments
 (0)