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

x86: enable initial Rust support

Note that only x86_64 is covered and not all features nor mitigations
are handled, but it is enough as a starting point and showcases
the basics needed to add Rust support for a new architecture.

Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Co-developed-by: Alex Gaynor <alex.gaynor@gmail.com>
Signed-off-by: Alex Gaynor <alex.gaynor@gmail.com>
Co-developed-by: Wedson Almeida Filho <wedsonaf@google.com>
Signed-off-by: Wedson Almeida Filho <wedsonaf@google.com>
Co-developed-by: David Gow <davidgow@google.com>
Signed-off-by: David Gow <davidgow@google.com>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

+25 -2
+1
Documentation/rust/arch-support.rst
··· 15 15 ============ ================ ============================================== 16 16 Architecture Level of support Constraints 17 17 ============ ================ ============================================== 18 + ``x86`` Maintained ``x86_64`` only. 18 19 ============ ================ ==============================================
+1
arch/x86/Kconfig
··· 257 257 select HAVE_STATIC_CALL_INLINE if HAVE_OBJTOOL 258 258 select HAVE_PREEMPT_DYNAMIC_CALL 259 259 select HAVE_RSEQ 260 + select HAVE_RUST if X86_64 260 261 select HAVE_SYSCALL_TRACEPOINTS 261 262 select HAVE_UACCESS_VALIDATION if HAVE_OBJTOOL 262 263 select HAVE_UNSTABLE_SCHED_CLOCK
+10
arch/x86/Makefile
··· 68 68 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53383 69 69 # 70 70 KBUILD_CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx 71 + KBUILD_RUSTFLAGS += -Ctarget-feature=-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 71 72 72 73 ifeq ($(CONFIG_X86_KERNEL_IBT),y) 73 74 # ··· 156 155 cflags-$(CONFIG_GENERIC_CPU) += -mtune=generic 157 156 KBUILD_CFLAGS += $(cflags-y) 158 157 158 + rustflags-$(CONFIG_MK8) += -Ctarget-cpu=k8 159 + rustflags-$(CONFIG_MPSC) += -Ctarget-cpu=nocona 160 + rustflags-$(CONFIG_MCORE2) += -Ctarget-cpu=core2 161 + rustflags-$(CONFIG_MATOM) += -Ctarget-cpu=atom 162 + rustflags-$(CONFIG_GENERIC_CPU) += -Ztune-cpu=generic 163 + KBUILD_RUSTFLAGS += $(rustflags-y) 164 + 159 165 KBUILD_CFLAGS += -mno-red-zone 160 166 KBUILD_CFLAGS += -mcmodel=kernel 167 + KBUILD_RUSTFLAGS += -Cno-redzone=y 168 + KBUILD_RUSTFLAGS += -Ccode-model=kernel 161 169 endif 162 170 163 171 #
+13 -2
scripts/generate_rust_target.rs
··· 148 148 let mut ts = TargetSpec::new(); 149 149 150 150 // `llvm-target`s are taken from `scripts/Makefile.clang`. 151 - if cfg.has("DUMMY_ARCH") { 152 - ts.push("arch", "dummy_arch"); 151 + if cfg.has("X86_64") { 152 + ts.push("arch", "x86_64"); 153 + ts.push( 154 + "data-layout", 155 + "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", 156 + ); 157 + let mut features = "-3dnow,-3dnowa,-mmx,+soft-float".to_string(); 158 + if cfg.has("RETPOLINE") { 159 + features += ",+retpoline-external-thunk"; 160 + } 161 + ts.push("features", features); 162 + ts.push("llvm-target", "x86_64-linux-gnu"); 163 + ts.push("target-pointer-width", "64"); 153 164 } else { 154 165 panic!("Unsupported architecture"); 155 166 }