Skip to contentSkip to conversation

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

PATCH In review Borislav Petkov 2 participants 6 messages Jun 16, 2020 – Jun 17, 2020 x86

lore
Share & tools
lore

Borislav Petkov

author maintainer
#0 lore raw

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

Ok,

here's the fix first so that it goes in. I'll hammer on the test case later.

---
From: Petteri Aimonen <[email protected]>

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. ]

---
 arch/x86/include/asm/fpu/internal.h | 5 +++++
 arch/x86/kernel/fpu/core.c          | 3 +++
 2 files changed, 8 insertions(+)
arch/x86/include/asm/fpu/internal.h +5 -0
index 42159f45bf9c..845e7481ab77 100644
@@ -623,6 +623,11 @@ static inline void switch_fpu_finish(struct fpu *new_fpu)
  * MXCSR and XCR definitions:
  */
 
+static inline void ldmxcsr(u32 mxcsr)
+{
+	asm volatile("ldmxcsr %0" :: "m" (mxcsr));
+}
+
 extern unsigned int mxcsr_feature_mask;
 
 #define XCR_XFEATURE_ENABLED_MASK	0x00000000
arch/x86/kernel/fpu/core.c +3 -0
index 06c818967bb6..f398fedc590a 100644
@@ -101,6 +101,9 @@ void kernel_fpu_begin(void)
 		copy_fpregs_to_fpstate(&current->thread.fpu);
 	}
 	__cpu_invalidate_fpregs_state();
+
+	if (boot_cpu_has(X86_FEATURE_XMM))
+		ldmxcsr(MXCSR_DEFAULT);
 }
 EXPORT_SYMBOL_GPL(kernel_fpu_begin);
 
Signature · 8 lines
2.21.0


-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#1 lore raw
Quoted 4 lines · Borislav Petkov

Ok,

here's the fix first so that it goes in. I'll hammer on the test case later.

Does the 32-bit case need FNINIT?

Quoted 61 lines

---
From: Petteri Aimonen <[email protected]>

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. ]

---
 arch/x86/include/asm/fpu/internal.h | 5 +++++
 arch/x86/kernel/fpu/core.c          | 3 +++
 2 files changed, 8 insertions(+)
arch/x86/include/asm/fpu/internal.h +5 -0
index 42159f45bf9c..845e7481ab77 100644
@@ -623,6 +623,11 @@ static inline void switch_fpu_finish(struct fpu *new_fpu)
  * MXCSR and XCR definitions:
  */
 
+static inline void ldmxcsr(u32 mxcsr)
+{
+       asm volatile("ldmxcsr %0" :: "m" (mxcsr));
+}
+
 extern unsigned int mxcsr_feature_mask;
 
 #define XCR_XFEATURE_ENABLED_MASK      0x00000000
arch/x86/kernel/fpu/core.c +3 -1
index 06c818967bb6..f398fedc590a 100644
@@ -101,6 +101,9 @@ void kernel_fpu_begin(void)
                copy_fpregs_to_fpstate(&current->thread.fpu);
        }
        __cpu_invalidate_fpregs_state();
+
+       if (boot_cpu_has(X86_FEATURE_XMM))
+               ldmxcsr(MXCSR_DEFAULT);
 }
 EXPORT_SYMBOL_GPL(kernel_fpu_begin);
 
--

2.21.0

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
author maintainer
#2 lore raw
Quoted 7 lines · Andy Lutomirski
Quoted 4 lines · Borislav Petkov

Ok,

here's the fix first so that it goes in. I'll hammer on the test case later.

Does the 32-bit case need FNINIT?

Pasting from IRC:

I'm thinking if you'd need to reinit the FPU, then you need to do it for both, not only 32-bit or do you mean something else? Also, if you end up doing FNSAVE (old CPU) that one reinits state.

Whatever we decide doing, this should be a separate patch anyway.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#3 lore raw
Quoted 11 lines · Borislav Petkov
Quoted 6 lines · Andy Lutomirski
Quoted 5 lines · Borislav Petkov

Ok,

here's the fix first so that it goes in. I'll hammer on the test case later.

Quoted 7 lines
Quoted 1 line

Does the 32-bit case need FNINIT?

Pasting from IRC:

I'm thinking if you'd need to reinit the FPU, then you need to do it for both, not only 32-bit or do you mean something else? Also, if you end up doing FNSAVE (old CPU) that one reinits state.

We definitely need to sanitize MXCSR for kernel fpu if kernel fpu means SSE2. If kernel fpu means x87, we need to fix the fpu control word.

On x86_64, I suspect the UEFI ABI technically requires a clean x87 control word too. If we’re willing to declare that the kernel proper won’t use x87, then we could shove that into the UEFI code.

Quoted 10 lines

Whatever we decide doing, this should be a separate patch anyway.

Thx.

Signature · 4 lines
Regards/Gruss,
   Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author maintainer
#4 lore raw
Quoted 3 lines · Andy Lutomirski

We definitely need to sanitize MXCSR for kernel fpu if kernel fpu means SSE2. If kernel fpu means x87, we need to fix the fpu control word.

Bah, there's no need to beat around the bush - let's just do:

        if (boot_cpu_has(X86_FEATURE_XMM))
                ldmxcsr(MXCSR_DEFAULT);

        if (boot_cpu_has(X86_FEATURE_FPU))
                asm volatile ("fninit");

and be sure that kernel users get a squeaky-clean FPU.

Quoted 3 lines

On x86_64, I suspect the UEFI ABI technically requires a clean x87 control word too. If we’re willing to declare that the kernel proper won’t use x87, then we could shove that into the UEFI code.

Nah, we don't trust the firmware.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#5 lore raw
Quoted 14 lines · Borislav Petkov
Quoted 3 lines · Andy Lutomirski

We definitely need to sanitize MXCSR for kernel fpu if kernel fpu means SSE2. If kernel fpu means x87, we need to fix the fpu control word.

Bah, there's no need to beat around the bush - let's just do:

        if (boot_cpu_has(X86_FEATURE_XMM))
                ldmxcsr(MXCSR_DEFAULT);

        if (boot_cpu_has(X86_FEATURE_FPU))
                asm volatile ("fninit");
Quoted 7 lines

and be sure that kernel users get a squeaky-clean FPU.

Quoted 3 lines

On x86_64, I suspect the UEFI ABI technically requires a clean x87 control word too. If we’re willing to declare that the kernel proper won’t use x87, then we could shove that into the UEFI code.

Nah, we don't trust the firmware.

What I mean is: if we trust ourselves to have no x87 instructions in the kernel, we could put the FNINIT in the UEFI stubs to save some cycles. I don't know how slow FNINIT is.

Quoted 6 lines

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC