[PATCH 09/11] x86: rework CONFIG_GENERIC_CPU compiler flags
From: Arnd Bergmann
Date: Wed Dec 04 2024 - 05:36:00 EST
From: Arnd Bergmann <arnd@xxxxxxxx>
Building an x86-64 kernel with CONFIG_GENERIC_CPU is documented to
run on all CPUs, but the Makefile does not actually pass an -march=
argument, instead relying on the default that was used to configure
the toolchain.
In many cases, gcc will be configured to -march=x86-64 or -march=k8
for maximum compatibility, but in other cases a distribution default
may be either raised to a more recent ISA, or set to -march=native
to build for the CPU used for compilation. This still works in the
case of building a custom kernel for the local machine.
The point where it breaks down is building a kernel for another
machine that is older the the default target. Changing the default
to -march=x86-64 would make it work reliable, but possibly produce
worse code on distros that intentionally default to a newer ISA.
To allow reliably building a kernel for either the oldest x86-64
CPUs or a more recent level, add three separate options for
v1, v2 and v3 of the architecture as defined by gcc and clang
and make them all turn on CONFIG_GENERIC_CPU. Based on this it
should be possible to change runtime feature detection into
build-time detection for things like cmpxchg16b, or possibly
gate features that are only available on older architectures.
Link: https://lists.llvm.org/pipermail/llvm-dev/2020-July/143289.html
Signed-off-by: Arnd Bergmann <arnd@xxxxxxxx>
---
arch/x86/Kconfig.cpu | 39 ++++++++++++++++++++++++++++++++++-----
arch/x86/Makefile | 6 ++++++
2 files changed, 40 insertions(+), 5 deletions(-)
diff --git a/arch/x86/Kconfig.cpu b/arch/x86/Kconfig.cpu
index 139db904e564..1461a739237b 100644
--- a/arch/x86/Kconfig.cpu
+++ b/arch/x86/Kconfig.cpu
@@ -260,7 +260,7 @@ endchoice
choice
prompt "x86-64 Processor family"
depends on X86_64
- default GENERIC_CPU
+ default X86_64_V2
help
This is the processor type of your CPU. This information is
used for optimizing purposes. In order to compile a kernel
@@ -314,15 +314,44 @@ config MSILVERMONT
early Atom CPUs based on the Bonnell microarchitecture,
such as Atom 230/330, D4xx/D5xx, D2xxx, N2xxx or Z2xxx.
-config GENERIC_CPU
- bool "Generic-x86-64"
+config X86_64_V1
+ bool "Generic x86-64"
depends on X86_64
help
- Generic x86-64 CPU.
- Run equally well on all x86-64 CPUs.
+ Generic x86-64-v1 CPU.
+ Run equally well on all x86-64 CPUs, including early Pentium-4
+ variants lacking the sahf and cmpxchg16b instructions as well
+ as the AMD K8 and Intel Core 2 lacking popcnt.
+
+config X86_64_V2
+ bool "Generic x86-64 v2"
+ depends on X86_64
+ help
+ Generic x86-64-v2 CPU.
+ Run equally well on all x86-64 CPUs that meet the x86-64-v2
+ definition as well as those that only miss the optional
+ SSE3/SSSE3/SSE4.1 portions.
+ Examples of this include Intel Nehalem and Silvermont,
+ AMD Bulldozer (K10) and Jaguar as well as VIA Nano that
+ include popcnt, cmpxchg16b and sahf.
+
+config X86_64_V3
+ bool "Generic x86-64 v3"
+ depends on X86_64
+ help
+ Generic x86-64-v3 CPU.
+ Run equally well on all x86-64 CPUs that meet the x86-64-v3
+ definition as well as those that only miss the optional
+ AVX/AVX2 portions.
+ Examples of this include the Intel Haswell and AMD Excavator
+ microarchitectures that include the bmi1/bmi2, lzncnt, movbe
+ and xsave instruction set extensions.
endchoice
+config GENERIC_CPU
+ def_bool X86_64_V1 || X86_64_V2 || X86_64_V3
+
config X86_GENERIC
bool "Generic x86 support"
depends on X86_32
diff --git a/arch/x86/Makefile b/arch/x86/Makefile
index 05887ae282f5..1fdc3fc6a54e 100644
--- a/arch/x86/Makefile
+++ b/arch/x86/Makefile
@@ -183,6 +183,9 @@ else
cflags-$(CONFIG_MPSC) += -march=nocona
cflags-$(CONFIG_MCORE2) += -march=core2
cflags-$(CONFIG_MSILVERMONT) += -march=silvermont
+ cflags-$(CONFIG_MX86_64_V1) += -march=x86-64
+ cflags-$(CONFIG_MX86_64_V2) += $(call cc-option,-march=x86-64-v2,-march=x86-64)
+ cflags-$(CONFIG_MX86_64_V3) += $(call cc-option,-march=x86-64-v3,-march=x86-64)
cflags-$(CONFIG_GENERIC_CPU) += -mtune=generic
KBUILD_CFLAGS += $(cflags-y)
@@ -190,6 +193,9 @@ else
rustflags-$(CONFIG_MPSC) += -Ctarget-cpu=nocona
rustflags-$(CONFIG_MCORE2) += -Ctarget-cpu=core2
rustflags-$(CONFIG_MSILVERMONT) += -Ctarget-cpu=silvermont
+ rustflags-$(CONFIG_MX86_64_V1) += -Ctarget-cpu=x86-64
+ rustflags-$(CONFIG_MX86_64_V2) += -Ctarget-cpu=x86-64-v2
+ rustflags-$(CONFIG_MX86_64_V3) += -Ctarget-cpu=x86-64-v3
rustflags-$(CONFIG_GENERIC_CPU) += -Ztune-cpu=generic
KBUILD_RUSTFLAGS += $(rustflags-y)
--
2.39.5