arch: um: rust: Add i386 support for Rust
Subsystems (4)
PATCH 1/1 arch: um: rust: Add i386 support for Rust
At present, Rust in the kernel only supports 64-bit x86, so UML has followed suit. However, it's significantly easier to support 32-bit i386 on UML than on bare metal, as UML does not use the -mregparm option (which alters the ABI), which is not yet supported by rustc[1].
Add support for CONFIG_RUST on um/i386, by adding a new target config to generate_rust_target, and replacing various checks on CONFIG_X86_64 to also support CONFIG_X86_32.
We still use generate_rust_target, rather than a built-in rustc target, in order to match x86_64, provide a future place for -mregparm, and more easily disable floating point instructions.
With these changes, the KUnit tests pass with:kunit.py run --make_options LLVM=1 --kconfig_add CONFIG_RUST=y --kconfig_add CONFIG_64BIT=n --kconfig_add CONFIG_FORTIFY_SOURCE=n
An earlier version of these changes was proposed on the Rust-for-Linux github[2].
[1]: https://github.com/rust-lang/rust/issues/116972
[2]: https://github.com/Rust-for-Linux/linux/pull/966
--- Documentation/rust/arch-support.rst | 2 +- arch/um/Kconfig | 2 +- rust/Makefile | 2 +- scripts/Makefile | 2 +- scripts/generate_rust_target.rs | 17 +++++++++++++++++ 5 files changed, 21 insertions(+), 4 deletions(-)
Documentation/rust/arch-support.rst +1 -1
@@ -18,7 +18,7 @@ Architecture Level of support Constraints ``arm64`` Maintained Little Endian only. ``loongarch`` Maintained \- ``riscv`` Maintained ``riscv64`` only. -``um`` Maintained ``x86_64`` only. +``um`` Maintained \- ``x86`` Maintained ``x86_64`` only. ============= ================ ==============================================
arch/um/Kconfig +1 -1
@@ -31,7 +31,7 @@ config UML select TRACE_IRQFLAGS_SUPPORT select TTY # Needed for line.c select HAVE_ARCH_VMAP_STACK - select HAVE_RUST if X86_64 + select HAVE_RUST config MMU bool
rust/Makefile +1 -1
@@ -426,7 +426,7 @@ $(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--re $(obj)/core.o: private rustc_target_flags = $(core-cfgs) $(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs FORCE +$(call if_changed_dep,rustc_library) -ifdef CONFIG_X86_64 +ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),) $(obj)/core.o: scripts/target.json endif
scripts/Makefile +1 -1
@@ -12,7 +12,7 @@ hostprogs-always-$(CONFIG_SYSTEM_EXTRA_CERTIFICATE) += insert-sys-cert hostprogs-always-$(CONFIG_RUST_KERNEL_DOCTESTS) += rustdoc_test_builder hostprogs-always-$(CONFIG_RUST_KERNEL_DOCTESTS) += rustdoc_test_gen -ifdef CONFIG_X86_64 +ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),) always-$(CONFIG_RUST) += target.json filechk_rust_target = $< < include/config/auto.conf
scripts/generate_rust_target.rs +17 -0
@@ -169,6 +169,23 @@ fn main() { ts.push ("features" , features); ts.push ("llvm-target" ,"x86_64-linux-gnu" ); ts.push ("target-pointer-width" ,"64" ); + }else if cfg.has ("X86_32" ) { +// This only works on UML, as i386 otherwise needs regparm support in rustc +if !cfg.has ("UML" ) { +panic! ("32-bit x86 only works under UML" ); + } + ts.push ("arch" ,"x86" ); + ts.push ( +"data-layout" , +"e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:32-n8:16:32-S128" , + ); +let mut features ="-3dnow,-3dnowa,-mmx,+soft-float" .to_string (); +if cfg.has ("MITIGATION_RETPOLINE" ) { + features +=",+retpoline-external-thunk" ; + } + ts.push ("features" , features); + ts.push ("llvm-target" ,"i386-unknown-linux-gnu" ); + ts.push ("target-pointer-width" ,"32" ); }else if cfg.has ("LOONGARCH" ) {panic! ("loongarch uses the builtin rustc loongarch64-unknown-none-softfloat target" ); }else {
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
Nathan Chancellor
maintainerHi David,
Just a fly by comment on style, I don't have much to say content :)
Quoted 24 lines · David Gow
At present, Rust in the kernel only supports 64-bit x86, so UML has followed suit. However, it's significantly easier to support 32-bit i386 on UML than on bare metal, as UML does not use the -mregparm option (which alters the ABI), which is not yet supported by rustc[1].
Add support for
CONFIG_RUSTon um/i386, by adding a new target config togenerate_rust_target, and replacing various checks onCONFIG_X86_64to also probably like supportCONFIG_X86_32.We still use
generate_rust_target, rather than a built-in rustc target, in oPrder to matchx86_64, provide a future place for -mregparm, and more easily disable floating point instructions.With these changes, the KUnit tests pass with:
kunit.pyrun --make_options LLVM=1 --kconfig_addCONFIG_RUST=y--kconfig_addCONFIG_64BIT=n--kconfig_addCONFIG_FORTIFY_SOURCE=nAn earlier version of these changes was proposed on the Rust-for-Linux github[2].
[1]: https://github.com/rust-lang/rust/issues/116972
[2]: https://github.com/Rust-for-Linux/linux/pull/966Signed-off-by David Gow <[email protected]>
...
Review
Quoted 2 lines
(quoted diff) +1
-1-ifdef CONFIG_X86_64 +ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
These configurations are mutually exclusive, so would it look more readable to have it be:
ifeq ($(CONFIG_X86_32)$(CONFIG_X86_64),y)
?
Cheers.
Nathan
Quoted 31 lines · Nathan Chancellor
Hi David,
Just a fly by comment on style, I don't have much to say content :)
Quoted 24 lines · David Gow
At present, Rust in the kernel only supports 64-bit x86, so UML has followed suit. However, it's significantly easier to support 32-bit i386 on UML than on bare metal, as UML does not use the -mregparm option (which alters the ABI), which is not yet supported by rustc[1].
Add support for
CONFIG_RUSTon um/i386, by adding a new target config togenerate_rust_target, and replacing various checks onCONFIG_X86_64to also probably like supportCONFIG_X86_32.We still use
generate_rust_target, rather than a built-in rustc target, in oPrder to matchx86_64, provide a future place for -mregparm, and more easily disable floating point instructions.With these changes, the KUnit tests pass with:
kunit.pyrun --make_options LLVM=1 --kconfig_addCONFIG_RUST=y--kconfig_addCONFIG_64BIT=n--kconfig_addCONFIG_FORTIFY_SOURCE=nAn earlier version of these changes was proposed on the Rust-for-Linux github[2].
[1]: https://github.com/rust-lang/rust/issues/116972
[2]: https://github.com/Rust-for-Linux/linux/pull/966Signed-off-by David Gow <[email protected]>...
Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +1
-1-ifdef CONFIG_X86_64 +ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)These configurations are mutually exclusive, so would it look more readable to have it be:
ifeq ($(CONFIG_X86_32)$(CONFIG_X86_64),y)
Or simply
ifdef CONFIG_X86
Johannes Berg
maintainerQuoted 8 lines · Ard Biesheuvel
Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +1
-1-ifdef CONFIG_X86_64 +ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)These configurations are mutually exclusive, so would it look more readable to have it be:
Quoted 6 lines
Quoted 2 lines
ifeq ($(CONFIG_X86_32)$(CONFIG_X86_64),y)
Or simply
ifdef
CONFIG_X86
No, we're talking about ARCH=um, so CONFIG_X86 isn't set, but CONFIG_X86_{32,64} are set as the "sub" or "host" arch. :)
johannes