ARM: rust: Enable Rust for ARMv4T/v5TE/v6K and change ARMv7 codegen
Subsystems (3)
v3 cover ARM: rust: Enable Rust for ARMv4T/v5TE/v6K and change ARMv7 codegen
Rust on 32-bit ARM is limited to kernels that contain an ARMv7 CPU. This series extends it to ARMv6K-only, ARMv5TE and ARMv4T kernels, and gives ARMv7 kernels their own Rust target.
Based on v7.3-rc1.
Linus Walleij asked for older ARM cores during review of the ARMv7 support [1]. Christian Schrefl's earlier ARMv5 attempt [2] hit atomic and ABI problems. The kernel crate now uses C atomic helpers, and the *-linux-gnueabi targets used here keep the Linux EABI.
Patch 1 fixes a Rust build failure with gcc 8.1.0 and CC_OPTIMIZE_FOR_SIZE on ARMv7, without the rest of this series. It can be merged separately and is tagged for stable.
Patches 4 and 5 depend on patch 2 for 16-bit xchg() on pre-ARMv6 CPUs. Patch 6 switches ARMv7 kernels to the ARMv7 Rust target and can be left out. Combined ARMv6K and ARMv7 kernels keep the generic ARMv6 target.
Arnd suggested -Ctarget-cpu with arm-unknown-linux-gnueabi. Its explicit +v6 remains in effect with -Ctarget-cpu=arm926ej-s, so libcore contains ARMv6 instructions. Disabling +v6 produces ARMv5TE code but warns about an unstable target feature. The ARMv4T and ARMv5TE targets also declare 32-bit atomics rather than 64-bit. The series selects the target next to -march.
ARMv4 without Thumb, ARMv7-M and plain ARMv6 (ARM1136r0) stay excluded.
The Rust samples, Rust KUnit suites and doctests passed in the following ARM-mode kernels on v7.3-rc1.
QEMU:
- ARMv4T: sx1 (OMAP310, ARM925T), omap1_defconfig, 360 doctests. CPU_DCACHE_WRITETHROUGH was disabled because QEMU does not implement the CP15 register it writes.
- ARMv5TE: versatilepb (ARM926EJ-S), versatile_defconfig, 355 doctests.
- ARMv6K: raspi0 (ARM1176), bcm2835_defconfig without ARCH_MULTI_V7, 368 doctests.
- ARMv7: virt (Cortex-A15), 371 doctests. With clang 22.1.8, Rust modules also loaded, including the PCI and DMA samples on pci-testdev.
The ARMv4T, ARMv5TE and ARMv6K QEMU runs used these toolchains:
- clang 22.1.8, rustc 1.99.0, bindgen 0.73.2
- gcc 15.2.0, rustc 1.98.0, bindgen 0.72.1
- minimum LLVM 17.0.1, rustc 1.85.0, bindgen 0.71.1
- minimum gcc 8.1.0, binutils 2.30, rustc 1.85.0, bindgen 0.71.1
ARMv7 virt also passed with both minimum toolchains. No additional toolchain requirement was found.
Hardware:
- Microchip SAM9X75 Curiosity (ARM926EJ-S), clang 22.1.8, 356 doctests. Tested with an ARMv5-only config and with at91_dt_defconfig, which is built for ARMv4T. Rust sample modules loaded and unloaded.
- Raspberry Pi 1 Model B+ Rev 1.2 (ARM1176JZF-S), bcm2835_defconfig without ARCH_MULTI_V7, clang 22.1.8, 368 doctests. Ten Rust modules loaded and unloaded. Misc ioctls, configfs, scoped debugfs and rnull I/O checks passed. The always-disabled KUnit selftest was skipped. No warnings or oopses in the test kernel.
- Raspberry Pi 400 (Cortex-A72), 32-bit LPAE kernel, 371 doctests. Both ARMv6 and ARMv7 Rust targets were tested with clang 22.1.8 and with gcc 8.1.0 at CC_OPTIMIZE_FOR_SIZE. With the ARMv7 target and both compilers, Rust sample modules loaded and unloaded. rnull read and write tests passed, and Rust cpufreq-dt switched between 600 MHz and 1.8 GHz.
The optional panic QR renderer was disabled for the GCC build with the ARMv7 Rust target. With rustc 1.85.0 and size optimization, its object references __aeabi_uldivmod, which the ARM kernel does not provide. An unpatched v7.3-rc1 QR object has the same reference.
Built-in Rust code ran on Thumb-2 kernels on the Pi 400 and in QEMU, with both ARMv6 and ARMv7 Rust targets. Tested Rust modules failed with "unsupported interworking call (ARM -> Thumb)", also on unpatched v7.3-rc1. One open question is whether HAVE_RUST should exclude THUMB2_KERNEL && MODULES or leave module loading to a follow-up.
I plan to submit the enablement patches through Russell's patch system unless the ARM and Rust maintainers agree on another route.
Changes in v3:
- Shorten patch 1's commit message as requested by Gary and collect Gary's and Bradley's Reviewed-by tags. No code changes.
- Tighten the commit messages for patches 3-6. No code changes.
- Add Raspberry Pi 1 Model B+ hardware results for the full series.
Changes in v2:
- Select the rustc target next to -march and explain why -Ctarget-cpu on the generic target is not enough (Arnd).
- Add ARMv4T and ARMv6K-only support (Arnd).
- Add optional patch 6 for the ARMv7 Rust target (Arnd).
- Keep ARMv7-M and plain ARMv6 excluded (Arnd, Bradley).
- Test Thumb-2 kernels and document the Rust module failure (Arnd).
- Test gcc and the minimum tool versions (Miguel).
- Add patch 1 for the build failure found with gcc 8.1.0.
- Patch 2: unchanged code, collected Arnd's Acked-by and Reviewed-by tags from Linus Walleij and Bradley Morgan, and explained the plain ARMv6 exclusion (Arnd).
- Update the atomic-helper architecture lists and architecture table.
v2: https://lore.kernel.org/rust-for-linux/[email protected]/
v1: https://lore.kernel.org/rust-for-linux/[email protected]/
[1] https://lore.kernel.org/rust-for-linux/CACRpkdYF0sVB2-qgy=GzETSR3+2sagVQPGdunDQDJrn8KqJorA@mail.gmail.com/
[2] https://lore.kernel.org/rust-for-linux/[email protected]/
Karl Mehltretter (6):
ARM: cmpxchg: always inline __arch_xchg() and __cmpxchg()
ARM: cmpxchg: support 2-byte xchg() on pre-ARMv6 CPUs
ARM: rust: Enable Rust support for ARMv6K-only kernels
ARM: rust: Enable Rust support for ARMv5TE
ARM: rust: Enable Rust support for ARMv4T
ARM: rust: Build Rust code for ARMv7 on ARMv7 kernels
Documentation/rust/arch-support.rst | 8 ++++---- arch/arm/Kconfig | 8 +++++++- arch/arm/Makefile | 10 +++++++++- arch/arm/include/asm/cmpxchg.h | 26 +++++++++++++++++--------- rust/helpers/atomic_ext.c | 4 ++-- 5 files changed, 39 insertions(+), 17 deletions(-)
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v3 1/6 ARM: cmpxchg: always inline __arch_xchg() and __cmpxchg()
__arch_xchg(), __cmpxchg() and __cmpxchg_local() rely on inlining to remove their undefined-function calls for unsupported sizes.
With gcc 8.1.0 and CC_OPTIMIZE_FOR_SIZE, bcm2835_defconfig with RUST=y fails to link. The compiler emits out-of-line copies for the many callers in rust/helpers/helpers.c, retaining those calls:
helpers.c:(.text+0x424): undefined reference to __bad_xchg' helpers.c:(.text+0x4ac): undefined reference to __bad_cmpxchg'
Mark the functions __always_inline so the compiler can remove the unsupported-size path for each caller's constant size.
--- arch/arm/include/asm/cmpxchg.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
arch/arm/include/asm/cmpxchg.h +6 -6
@@ -26,7 +26,7 @@#define swp_is_buggy#endif -static inline unsigned long +static __always_inline unsigned long __arch_xchg (unsigned long x,volatile void *ptr,int size) {extern void __bad_xchg (volatile void *,int );
@@ -155,8 +155,8 @@ extern void __bad_cmpxchg(volatile void *ptr, int size);* cmpxchg only support 32-bits operands on ARMv6. */ -static inline unsigned long __cmpxchg (volatile void *ptr,unsigned long old, -unsigned long new,int size) +static __always_inline unsigned long +__cmpxchg (volatile void *ptr,unsigned long old,unsigned long new,int size) {unsigned long oldval, res;
@@ -220,9 +220,9 @@ static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old, sizeof(*(ptr))); \ }) -static inline unsigned long __cmpxchg_local (volatile void *ptr, -unsigned long old, -unsigned long new,int size) +static __always_inline unsigned long +__cmpxchg_local (volatile void *ptr,unsigned long old,unsigned long new, +int size) {unsigned long ret;
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v3 2/6 ARM: cmpxchg: support 2-byte xchg() on pre-ARMv6 CPUs
In reply to Karl MehltretterPre-ARMv6 CPUs have no halfword swp, so __arch_xchg() only handles 1- and 4-byte exchanges there and turns any other size into a link error via __bad_xchg().
No C code needs a 16-bit xchg() on these CPUs, but the Rust atomic helpers in rust/helpers/atomic_ext.c provide xchg() for Atomic<i16> unconditionally, so building with CONFIG_RUST=y for ARMv5 fails:
ld.lld: error: undefined symbol: __bad_xchg
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
referenced by
helpers.c
rust/helpers/helpers.o:(rust_helper_atomic_i16_xchg)
Implement the 2-byte case with interrupts disabled, as the pre-ARMv6 atomic_t operations already do. This is safe because a kernel built for ARMv5 or older is never SMP.
ARMv6 before ARMv6K (ARM1136r0) has no 16-bit exclusive access either. It is not handled here. Rust stays disabled for those kernels, and they cannot be SMP since commit d70242427110 ("ARM: rework ARM11 CPU selection logic").
--- arch/arm/include/asm/cmpxchg.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
arch/arm/include/asm/cmpxchg.h +11 -3
@@ -31,11 +31,10 @@ __arch_xchg(unsigned long x, volatile void *ptr, int size) {extern void __bad_xchg (volatile void *,int );unsigned long ret; -#ifdef swp_is_buggy -unsigned long flags; -#endif #if __LINUX_ARM_ARCH__ >=6 unsigned int tmp; +#else +unsigned long flags;#endif prefetchw ((const void *)ptr);
@@ -106,6 +105,15 @@ __arch_xchg(unsigned long x, volatile void *ptr, int size) : "r" (x), "r" (ptr) : "memory", "cc");break ; +#endif +#if __LINUX_ARM_ARCH__ <6 +case 2 : +/* swp has no halfword form. Pre-ARMv6 is never SMP. */ +raw_local_irq_save (flags); + ret = *(volatile unsigned short *)ptr; + *(volatile unsigned short *)ptr = x; +raw_local_irq_restore (flags); +break ;#endif default :/* Cause a link-time error, the xchg() size is not supported */
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v3 3/6 ARM: rust: Enable Rust support for ARMv6K-only kernels
In reply to Karl MehltretterHAVE_RUST currently requires CPU_32v7, excluding ARMv6K-only kernels even though arm-unknown-linux-gnueabi already builds ARMv6 code.
Select HAVE_RUST for CPU_32v6K. CPU_V7 selects CPU_32v6K, so ARMv7 remains covered.
Keep plain ARMv6 (CPU_V6, ARM1136r0) excluded. It lacks 1- and 2-byte xchg() and 2-byte cmpxchg(), which the Rust atomic helpers need. The old condition exposes Rust on combined CPU_V6 and ARMv7 kernels, which do not link.
Tested the Rust samples, KUnit suites and doctests in QEMU raspi0 (ARM1176) with bcm2835_defconfig without ARCH_MULTI_V7.
---
Tested the full six-patch series on a Raspberry Pi 1 Model B+ Rev 1.2 (ARM1176JZF-S), with bcm2835_defconfig without ARCH_MULTI_V7, clang 22.1.8, rustc 1.99.0 and bindgen 0.73.2.
All 368 doctests and the Rust KUnit suites passed. The intentionally disabled KUnit selftest was skipped. Ten Rust modules loaded and unloaded. Misc ioctls, configfs, scoped debugfs and rnull read/write checks passed. No warnings or oopses in the test kernel.
Documentation/rust/arch-support.rst | 2 +- arch/arm/Kconfig | 2 +- rust/helpers/atomic_ext.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-)
Documentation/rust/arch-support.rst +1 -1
@@ -15,7 +15,7 @@ support corresponds to ``S`` values in the ``MAINTAINERS`` file. ============= ================ ============================================== Architecture Level of support Constraints ============= ================ ============================================== -``arm`` Maintained ARMv7 Little Endian only. +``arm`` Maintained ARMv6K and ARMv7, Little Endian only. ``arm64`` Maintained Little Endian only. ``loongarch`` Maintained \- ``riscv`` Maintained ``riscv64`` and LLVM/Clang only.
arch/arm/Kconfig +1 -1
@@ -138,7 +138,7 @@ config ARM select MMU_GATHER_RCU_TABLE_FREE if SMP && ARM_LPAE select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_RSEQ - select HAVE_RUST if CPU_LITTLE_ENDIAN && CPU_32v7 && !KASAN + select HAVE_RUST if CPU_LITTLE_ENDIAN && CPU_32v6K && !CPU_V6 && !KASAN select HAVE_STACKPROTECTOR select HAVE_SYSCALL_TRACEPOINTS select HAVE_UID16
rust/helpers/atomic_ext.c +2 -2
@@ -42,7 +42,7 @@ GEN_READ_SET_HELPERS(ptr, const void *)* xchg helpers depend on ARCH_SUPPORTS_ATOMIC_RMW and on the * architecture provding xchg() support for i8 and i16. * -* The architectures that currently support Rust (x86_64, armv7, +* The architectures that currently support Rust (x86_64, arm, * arm64, riscv, and loongarch) satisfy these requirements. */ #define GEN_XCHG_HELPER (tname, type, suffix) \
@@ -66,7 +66,7 @@ GEN_XCHG_HELPERS(ptr, const void *)* try_cmpxchg helpers depend on ARCH_SUPPORTS_ATOMIC_RMW and on the * architecture provding try_cmpxchg() support for i8 and i16. * -* The architectures that currently support Rust (x86_64, armv7, +* The architectures that currently support Rust (x86_64, arm, * arm64, riscv, and loongarch) satisfy these requirements. */ #define GEN_TRY_CMPXCHG_HELPER (tname, type, suffix) \
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v3 4/6 ARM: rust: Enable Rust support for ARMv5TE
In reply to Karl MehltretterThe arm-unknown-linux-gnueabi target enables +v6 and emits instructions that ARMv5 does not support. -Ctarget-cpu=arm926ej-s does not remove +v6, while -Ctarget-feature=-v6 produces an unstable-feature warning.
Use armv5te-unknown-linux-gnueabi for CPU_32v5. It uses the Linux soft-float EABI, requires strict alignment and declares 32-bit atomics. Select it alongside -march so Rust and C follow the same CPU selection. Kernels containing ARMv4 or ARMv4T CPUs remain excluded. CPU_32v5 also covers ARM1020, for which C already uses -march=armv5te.
The kernel crate implements atomics through C helpers. The preceding patch supplies the missing 2-byte xchg() on pre-ARMv6 CPUs. core::sync::atomic remains unusable because LLVM lowers its operations to __sync_* libcalls that the kernel does not provide. No in-tree code uses them.
Tested the Rust samples, KUnit suites and doctests in QEMU versatilepb with versatile_defconfig and on a Microchip SAM9X75 Curiosity board.
--- Documentation/rust/arch-support.rst | 2 +- arch/arm/Kconfig | 8 +++++++- arch/arm/Makefile | 7 ++++++- 3 files changed, 14 insertions(+), 3 deletions(-)
Documentation/rust/arch-support.rst +1 -1
@@ -15,7 +15,7 @@ support corresponds to ``S`` values in the ``MAINTAINERS`` file. ============= ================ ============================================== Architecture Level of support Constraints ============= ================ ============================================== -``arm`` Maintained ARMv6K and ARMv7, Little Endian only. +``arm`` Maintained ARMv5TE, ARMv6K and ARMv7, Little Endian only. ``arm64`` Maintained Little Endian only. ``loongarch`` Maintained \- ``riscv`` Maintained ``riscv64`` and LLVM/Clang only.
arch/arm/Kconfig +7 -1
@@ -138,7 +138,7 @@ config ARM select MMU_GATHER_RCU_TABLE_FREE if SMP && ARM_LPAE select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_RSEQ - select HAVE_RUST if CPU_LITTLE_ENDIAN && CPU_32v6K && !CPU_V6 && !KASAN + select HAVE_RUST if CPU_LITTLE_ENDIAN && ARM_RUST_TARGET && !KASAN select HAVE_STACKPROTECTOR select HAVE_SYSCALL_TRACEPOINTS select HAVE_UID16
@@ -172,6 +172,12 @@ config ARM Europe. There is an ARM Linux project with a web page at <http://www.arm.linux.org.uk/>. +# The CPU selections for which rustc has a matching target +config ARM_RUST_TARGET + def_bool y + depends on (CPU_32v6K && !CPU_V6) || \ + (CPU_32v5 && !CPU_32v4T && !CPU_32v4) + config ARM_HAS_GROUP_RELOCS def_bool !COMPILE_TEST help
arch/arm/Makefile +6 -1
@@ -73,6 +73,11 @@ arch-$(CONFIG_CPU_32v4T) :=-march=armv4t arch-$(CONFIG_CPU_32v4) :=-march=armv4 arch-$(CONFIG_CPU_32v3) :=-march=armv3m +# The rustc target fixes the instruction set. -Ctarget-cpu cannot remove +# the +v6 of the generic target, so older CPUs need their own target. +rust-target-y :=arm-unknown-linux-gnueabi +rust-target-$(CONFIG_CPU_32v5) :=armv5te-unknown-linux-gnueabi + # Note that GCC does not numerically define an architecture version # macro, but instead defines a whole series of macros which makes # testing for a specific architecture or later rather impossible.
@@ -150,7 +155,7 @@ endif KBUILD_CPPFLAGS +=$(cpp-y) KBUILD_CFLAGS +=$(CFLAGS_ABI) $(CFLAGS_ISA) $(arch-y) $(tune-y) $(call cc-option,-mshort-load-bytes,$(call cc-option,-malignment-traps,)) -msoft-float -Uarm KBUILD_AFLAGS +=$(CFLAGS_ABI) $(AFLAGS_ISA) -Wa,$(arch-y) $(tune-y) -include $(srctree)/arch/arm/include/asm/unified.h -msoft-float -KBUILD_RUSTFLAGS += --target=arm-unknown-linux-gnueabi +KBUILD_RUSTFLAGS += --target=$(rust-target-y) CHECKFLAGS += -D__arm__
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v3 5/6 ARM: rust: Enable Rust support for ARMv4T
In reply to Karl MehltretterThe armv5te target emits clz and blx, which ARMv4T lacks. Use rustc's armv4t-unknown-linux-gnueabi target for CPU_32v4T. A combined ARMv4T and ARMv5 kernel then uses ARMv4T code for both C and Rust.
Plain ARMv4 remains excluded because rustc has no matching Linux target. This also depends on the preceding 16-bit xchg() support for pre-ARMv6 CPUs.
Tested the Rust samples, KUnit suites and doctests in QEMU sx1 (ARM925T) with omap1_defconfig and on a Microchip SAM9X75 Curiosity board with at91_dt_defconfig.
--- Documentation/rust/arch-support.rst | 8 ++++---- arch/arm/Kconfig | 2 +- arch/arm/Makefile | 1 + 3 files changed, 6 insertions(+), 5 deletions(-)
Documentation/rust/arch-support.rst +4 -4
@@ -12,15 +12,15 @@ which uses ``libclang``. Below is a general summary of architectures that currently work. Level of support corresponds to ``S`` values in the ``MAINTAINERS`` file. -============= ================ ============================================== +============= ================ ====================================================== Architecture Level of support Constraints -============= ================ ============================================== -``arm`` Maintained ARMv5TE, ARMv6K and ARMv7, Little Endian only. +============= ================ ====================================================== +``arm`` Maintained ARMv4T, ARMv5TE, ARMv6K and ARMv7, Little Endian only. ``arm64`` Maintained Little Endian only. ``loongarch`` Maintained \- ``riscv`` Maintained ``riscv64`` and LLVM/Clang only. ``s390`` Maintained ``CONFIG_EXPOLINE`` must be disabled. ``um`` Maintained \- ``x86`` Maintained ``x86_64`` only. -============= ================ ============================================== +============= ================ ======================================================
arch/arm/Kconfig +1 -1
@@ -176,7 +176,7 @@ config ARM config ARM_RUST_TARGET def_bool y depends on (CPU_32v6K && !CPU_V6) || \ - (CPU_32v5 && !CPU_32v4T && !CPU_32v4) + ((CPU_32v5 || CPU_32v4T) && !CPU_32v4) config ARM_HAS_GROUP_RELOCS def_bool !COMPILE_TEST
arch/arm/Makefile +1 -0
@@ -77,6 +77,7 @@ arch-$(CONFIG_CPU_32v3) :=-march=armv3m # the +v6 of the generic target, so older CPUs need their own target. rust-target-y :=arm-unknown-linux-gnueabi rust-target-$(CONFIG_CPU_32v5) :=armv5te-unknown-linux-gnueabi +rust-target-$(CONFIG_CPU_32v4T) :=armv4t-unknown-linux-gnueabi # Note that GCC does not numerically define an architecture version # macro, but instead defines a whole series of macros which makes
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v3 6/6 ARM: rust: Build Rust code for ARMv7 on ARMv7 kernels
In reply to Karl MehltretterThe arm-unknown-linux-gnueabi target builds ARMv6 code, so ARMv7 kernels cannot use ARMv7 instructions in Rust code.
Use armv7-unknown-linux-gnueabi when C is built with -march=armv7-a. The target uses the soft-float EABI with NEON disabled and permits unaligned word and halfword accesses, matching C on MMU-enabled ARMv7 kernels. A kernel that also contains an ARMv6K CPU is built with -march=armv6k and keeps the generic target.
In the tested Raspberry Pi 400 configuration, Rust text grows by 1.9%. Six small benchmarks range from 10% shorter to 5% longer runtimes.
Tested on a Raspberry Pi 400 (Cortex-A72, LPAE) with clang 22.1.8 and in QEMU virt (Cortex-A15) with the minimum tool versions. Rust modules load with the movw/movt relocations.
--- arch/arm/Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
arch/arm/Makefile +4 -2
@@ -73,9 +73,11 @@ arch-$(CONFIG_CPU_32v4T) :=-march=armv4t arch-$(CONFIG_CPU_32v4) :=-march=armv4 arch-$(CONFIG_CPU_32v3) :=-march=armv3m -# The rustc target fixes the instruction set. -Ctarget-cpu cannot remove -# the +v6 of the generic target, so older CPUs need their own target. +# The rustc target fixes the instruction set, so pick it like -march above. +# The generic target builds ARMv6 code. -Ctarget-cpu cannot remove its +v6. rust-target-y :=arm-unknown-linux-gnueabi +rust-target-$(CONFIG_CPU_32v7) :=armv7-unknown-linux-gnueabi +rust-target-$(CONFIG_CPU_32v6) :=arm-unknown-linux-gnueabi rust-target-$(CONFIG_CPU_32v5) :=armv5te-unknown-linux-gnueabi rust-target-$(CONFIG_CPU_32v4T) :=armv4t-unknown-linux-gnueabi
Signature · 1 line
2.39.5 (Apple Git-154)