The command line option `no387' is designed to disable the FPU entirely.
The documentation says to disable the coprocessor and the Kconfig entry
for MATH_EMULATION says to set it in order to use emulation. It should
be restricted to 32bit only because 64bit expect SSE (which includes
basic FPU and there is not emulation for SSE).

The command line option `nofxsr' should also be limited to 32bit because
FXSR is part of the required flags on 64bit so turning it off is not
possible.

Clearing X86_FEATURE_FPU without emulation enabled will not work anyway
and hang in fpu__init_system_early_generic() before console is enabled.

Setting additioal dependencies, ensures that the CPU still boots on a
modern CPU. Otherwise, dropping FPU will leave FXSR enabled causing the
kernel to crash early in fpu__init_system_mxcsr().
With XSAVE support it will crash in fpu__init_cpu_xstate(). The problem
is that xsetbv() with YMM set and SSE cleared is not allowed.  That
means XSAVE has to be disabled. The XSAVE support is disabled in
fpu__init_system_xstate_size_legacy() but it is too late. It can be
removed, it has been added in commit

  1f999ab5a1360 ("x86, xsave: Disable xsave in i387 emulation mode")

to use `no387' on a CPU with XSAVE support.

All this happens before console output.
After hat, the next possible crash is in RAID6 detect code because MMX
remained enabled. With a 3DNOW enabled config it will explode in
memcpy() for instance due to kernel_fpu_begin() but this is
unconditionally enabled.

This is enough to boot a Debian Wheezy on a 32bit qemu "host" CPU which
supports everything up to XSAVES, AVX2 without 3DNOW. Later, Debian
increased the minimum requirements to i686 which means it does not boot
userland atleast due to CMOV.

After masking the additional features it still keeps SSE4A and 3DNOW*
enabled (if present on the host) but those are unused in the kernel.

Restrict `no387' and `nofxsr' otions to 32bit only. Add dependencies for
FPU, FXSR to additionaly mask CMOV, MMX, XSAVE if FXSR or FPU is cleared.

Link: https://lkml.kernel.org/r/20190701121710.vardxktdc63gt...@linutronix.de>
Reported-by: Vegard Nossum <vegard.nos...@oracle.com>
Signed-off-by: Sebastian Andrzej Siewior <bige...@linutronix.de>
---
 arch/x86/kernel/cpu/cpuid-deps.c |  5 +++++
 arch/x86/kernel/fpu/init.c       | 17 +++++++----------
 2 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/arch/x86/kernel/cpu/cpuid-deps.c b/arch/x86/kernel/cpu/cpuid-deps.c
index 2c0bd38a44ab1..e794e3860fc83 100644
--- a/arch/x86/kernel/cpu/cpuid-deps.c
+++ b/arch/x86/kernel/cpu/cpuid-deps.c
@@ -20,6 +20,7 @@ struct cpuid_dep {
  * but it's difficult to tell that to the init reference checker.
  */
 static const struct cpuid_dep cpuid_deps[] = {
+       { X86_FEATURE_FXSR,             X86_FEATURE_FPU       },
        { X86_FEATURE_XSAVEOPT,         X86_FEATURE_XSAVE     },
        { X86_FEATURE_XSAVEC,           X86_FEATURE_XSAVE     },
        { X86_FEATURE_XSAVES,           X86_FEATURE_XSAVE     },
@@ -27,7 +28,11 @@ static const struct cpuid_dep cpuid_deps[] = {
        { X86_FEATURE_PKU,              X86_FEATURE_XSAVE     },
        { X86_FEATURE_MPX,              X86_FEATURE_XSAVE     },
        { X86_FEATURE_XGETBV1,          X86_FEATURE_XSAVE     },
+       { X86_FEATURE_CMOV,             X86_FEATURE_FXSR      },
+       { X86_FEATURE_MMX,              X86_FEATURE_FXSR      },
+       { X86_FEATURE_MMXEXT,           X86_FEATURE_MMX       },
        { X86_FEATURE_FXSR_OPT,         X86_FEATURE_FXSR      },
+       { X86_FEATURE_XSAVE,            X86_FEATURE_FXSR      },
        { X86_FEATURE_XMM,              X86_FEATURE_FXSR      },
        { X86_FEATURE_XMM2,             X86_FEATURE_XMM       },
        { X86_FEATURE_XMM3,             X86_FEATURE_XMM2      },
diff --git a/arch/x86/kernel/fpu/init.c b/arch/x86/kernel/fpu/init.c
index ef0030e3fe6b9..5baae74af4f91 100644
--- a/arch/x86/kernel/fpu/init.c
+++ b/arch/x86/kernel/fpu/init.c
@@ -204,12 +204,6 @@ static void __init 
fpu__init_system_xstate_size_legacy(void)
         */
 
        if (!boot_cpu_has(X86_FEATURE_FPU)) {
-               /*
-                * Disable xsave as we do not support it if i387
-                * emulation is enabled.
-                */
-               setup_clear_cpu_cap(X86_FEATURE_XSAVE);
-               setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
                fpu_kernel_xstate_size = sizeof(struct swregs_state);
        } else {
                if (boot_cpu_has(X86_FEATURE_FXSR))
@@ -252,14 +246,17 @@ static void __init fpu__init_parse_early_param(void)
        char *argptr = arg;
        int bit;
 
+#ifdef CONFIG_X86_32
        if (cmdline_find_option_bool(boot_command_line, "no387"))
+#ifdef CONFIG_MATH_EMULATION
                setup_clear_cpu_cap(X86_FEATURE_FPU);
+#else
+               pr_err("Option 'no387' required CONFIG_MATH_EMULATION 
enabled.\n");
+#endif
 
-       if (cmdline_find_option_bool(boot_command_line, "nofxsr")) {
+       if (cmdline_find_option_bool(boot_command_line, "nofxsr"))
                setup_clear_cpu_cap(X86_FEATURE_FXSR);
-               setup_clear_cpu_cap(X86_FEATURE_FXSR_OPT);
-               setup_clear_cpu_cap(X86_FEATURE_XMM);
-       }
+#endif
 
        if (cmdline_find_option_bool(boot_command_line, "noxsave"))
                fpu__xstate_clear_all_cpu_caps();
-- 
2.20.1

Reply via email to