Skip to content

Commit ab0f4ce

Browse files
sulixjmberg-intel
authored andcommitted
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]: rust-lang/rust#116972 [2]: Rust-for-Linux#966 Signed-off-by: David Gow <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Johannes Berg <[email protected]>
1 parent 9a2123b commit ab0f4ce

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

Documentation/rust/arch-support.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Architecture Level of support Constraints
1818
``arm64`` Maintained Little Endian only.
1919
``loongarch`` Maintained \-
2020
``riscv`` Maintained ``riscv64`` only.
21-
``um`` Maintained ``x86_64`` only.
21+
``um`` Maintained \-
2222
``x86`` Maintained ``x86_64`` only.
2323
============= ================ ==============================================
2424

arch/um/Kconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ config UML
3131
select TRACE_IRQFLAGS_SUPPORT
3232
select TTY # Needed for line.c
3333
select HAVE_ARCH_VMAP_STACK
34-
select HAVE_RUST if X86_64
34+
select HAVE_RUST
3535

3636
config MMU
3737
bool

rust/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ $(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--re
426426
$(obj)/core.o: private rustc_target_flags = $(core-cfgs)
427427
$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
428428
+$(call if_changed_dep,rustc_library)
429-
ifdef CONFIG_X86_64
429+
ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
430430
$(obj)/core.o: scripts/target.json
431431
endif
432432

scripts/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ hostprogs-always-$(CONFIG_SYSTEM_EXTRA_CERTIFICATE) += insert-sys-cert
1212
hostprogs-always-$(CONFIG_RUST_KERNEL_DOCTESTS) += rustdoc_test_builder
1313
hostprogs-always-$(CONFIG_RUST_KERNEL_DOCTESTS) += rustdoc_test_gen
1414

15-
ifdef CONFIG_X86_64
15+
ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
1616
always-$(CONFIG_RUST) += target.json
1717
filechk_rust_target = $< < include/config/auto.conf
1818

scripts/generate_rust_target.rs

+17
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,23 @@ fn main() {
169169
ts.push("features", features);
170170
ts.push("llvm-target", "x86_64-linux-gnu");
171171
ts.push("target-pointer-width", "64");
172+
} else if cfg.has("X86_32") {
173+
// This only works on UML, as i386 otherwise needs regparm support in rustc
174+
if !cfg.has("UML") {
175+
panic!("32-bit x86 only works under UML");
176+
}
177+
ts.push("arch", "x86");
178+
ts.push(
179+
"data-layout",
180+
"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",
181+
);
182+
let mut features = "-3dnow,-3dnowa,-mmx,+soft-float".to_string();
183+
if cfg.has("MITIGATION_RETPOLINE") {
184+
features += ",+retpoline-external-thunk";
185+
}
186+
ts.push("features", features);
187+
ts.push("llvm-target", "i386-unknown-linux-gnu");
188+
ts.push("target-pointer-width", "32");
172189
} else if cfg.has("LOONGARCH") {
173190
panic!("loongarch uses the builtin rustc loongarch64-unknown-none-softfloat target");
174191
} else {

0 commit comments

Comments
 (0)