Skip to contentSkip to conversation

Rust enablement for AArch64

PATCH Superseded Jamie Cunliffe 5 participants 9 messages Oct 16, 2023 – Oct 19, 2023

Subsystems (5)
Documentation x86 Kbuild arm64 +1 more subsystems

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Rust enablement for AArch64

Enable Rust support for the AArch64 architecture.

The only change here since the v2 patch is the removal of the fp-armv8 feature. rustc ties fp and Neon together. However due to a bug in versions before 1.71 fp wasn't correctly being disabled. Now the compiler version used is 1.71 we can drop this option which now allows this to build without warnings.

Jamie Cunliffe (1):
arm64: rust: Enable Rust support for AArch64

 Documentation/rust/arch-support.rst | 1 +
 Makefile                            | 1 -
 arch/arm64/Kconfig                  | 1 +
 arch/arm64/Makefile                 | 4 ++++
 arch/x86/Makefile                   | 1 +
 rust/Makefile                       | 6 +++++-
 scripts/Makefile                    | 5 +++--
 scripts/generate_rust_target.rs     | 4 +++-
 8 files changed, 18 insertions(+), 5 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v3 1/1 arm64: rust: Enable Rust support for AArch64

This commit provides the build flags for Rust for AArch64. The core Rust support already in the kernel does the rest. This enables the PAC ret and BTI options in the Rust build flags to match the options that are used when building C.

The Rust samples have been tested with this commit.

---
 Documentation/rust/arch-support.rst | 1 +
 Makefile                            | 1 -
 arch/arm64/Kconfig                  | 1 +
 arch/arm64/Makefile                 | 4 ++++
 arch/x86/Makefile                   | 1 +
 rust/Makefile                       | 6 +++++-
 scripts/Makefile                    | 5 +++--
 scripts/generate_rust_target.rs     | 4 +++-
 8 files changed, 18 insertions(+), 5 deletions(-)
Documentation/rust/arch-support.rst +1 -0
index b91e9ef4d0c2..6bcb3b97c5b6 100644
@@ -15,6 +15,7 @@ support corresponds to ``S`` values in the ``MAINTAINERS`` file.
 ============  ================  ==============================================
 Architecture  Level of support  Constraints
 ============  ================  ==============================================
+``arm64``     Maintained        Little Endian only.
 ``um``        Maintained        ``x86_64`` only.
 ``x86``       Maintained        ``x86_64`` only.
 ============  ================  ==============================================
Makefile +0 -1
index 88ebf6547964..23100f193da3 100644
@@ -566,7 +566,6 @@ KBUILD_CFLAGS += -fno-strict-aliasing
 
 KBUILD_CPPFLAGS := -D__KERNEL__
 KBUILD_RUSTFLAGS := $(rust_common_flags) \
-		    --target=$(objtree)/scripts/target.json \
 		    -Cpanic=abort -Cembed-bitcode=n -Clto=n \
 		    -Cforce-unwind-tables=n -Ccodegen-units=1 \
 		    -Csymbol-mangling-version=v0 \
arch/arm64/Kconfig +1 -0
index 78f20e632712..d72618433521 100644
@@ -227,6 +227,7 @@ config ARM64
 	select HAVE_FUNCTION_ARG_ACCESS_API
 	select MMU_GATHER_RCU_TABLE_FREE
 	select HAVE_RSEQ
+	select HAVE_RUST if CPU_LITTLE_ENDIAN
 	select HAVE_STACKPROTECTOR
 	select HAVE_SYSCALL_TRACEPOINTS
 	select HAVE_KPROBES
arch/arm64/Makefile +4 -0
index 2d49aea0ff67..4562a8173e90 100644
@@ -41,6 +41,8 @@ KBUILD_CFLAGS	+= -mgeneral-regs-only	\
 KBUILD_CFLAGS	+= $(call cc-disable-warning, psabi)
 KBUILD_AFLAGS	+= $(compat_vdso)
 
+KBUILD_RUSTFLAGS += --target aarch64-unknown-none -C target-feature="-neon"
+
 KBUILD_CFLAGS	+= $(call cc-option,-mabi=lp64)
 KBUILD_AFLAGS	+= $(call cc-option,-mabi=lp64)
 
@@ -65,7 +67,9 @@ endif
 
 ifeq ($(CONFIG_ARM64_BTI_KERNEL),y)
   KBUILD_CFLAGS += -mbranch-protection=pac-ret+bti
+  KBUILD_RUSTFLAGS += -Z branch-protection=bti,pac-ret
 else ifeq ($(CONFIG_ARM64_PTR_AUTH_KERNEL),y)
+  KBUILD_RUSTFLAGS += -Z branch-protection=pac-ret
   ifeq ($(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET),y)
     KBUILD_CFLAGS += -mbranch-protection=pac-ret
   else
arch/x86/Makefile +1 -0
index 5bfe5caaa444..0f339d4abd40 100644
@@ -68,6 +68,7 @@ export BITS
 #    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53383
 #
 KBUILD_CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx
+KBUILD_RUSTFLAGS += --target=$(objtree)/scripts/target.json
 KBUILD_RUSTFLAGS += -Ctarget-feature=-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-avx,-avx2
 
 ifeq ($(CONFIG_X86_KERNEL_IBT),y)
rust/Makefile +5 -1
index 87958e864be0..b3127501e0e1 100644
@@ -294,6 +294,7 @@ bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
 
 # Derived from `scripts/Makefile.clang`.
 BINDGEN_TARGET_x86	:= x86_64-linux-gnu
+BINDGEN_TARGET_arm64    := aarch64-linux-gnu
 BINDGEN_TARGET		:= $(BINDGEN_TARGET_$(SRCARCH))
 
 # All warnings are inhibited since GCC builds are very experimental,
@@ -428,8 +429,11 @@ $(obj)/core.o: private skip_clippy = 1
 $(obj)/core.o: private skip_flags = -Dunreachable_pub
 $(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
 $(obj)/core.o: private rustc_target_flags = $(core-cfgs)
-$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs scripts/target.json FORCE
+$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
 	$(call if_changed_dep,rustc_library)
+ifeq ($(ARCH),x86_64)
+$(obj)/core.o: scripts/target.json
+endif
 
 $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
 $(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
scripts/Makefile +3 -2
index 576cf64be667..0aa78339128c 100644
@@ -11,12 +11,13 @@ hostprogs-always-$(CONFIG_MODULE_SIG_FORMAT)		+= sign-file
 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
-always-$(CONFIG_RUST)					+= target.json
 
+ifeq ($(ARCH),x86_64)
+always-$(CONFIG_RUST)					+= target.json
 filechk_rust_target = $< < include/config/auto.conf
-
 $(obj)/target.json: scripts/generate_rust_target include/config/auto.conf FORCE
 	$(call filechk,rust_target)
+endif
 
 hostprogs += generate_rust_target
 generate_rust_target-rust := y
scripts/generate_rust_target.rs +3 -1
index 3c6cbe2b278d..ec5ef35dbe52 100644
@@ -148,7 +148,9 @@ fn main() {
     let mut ts = TargetSpec::new();
 
     // `llvm-target`s are taken from `scripts/Makefile.clang`.
-    if cfg.has("X86_64") {
+    if cfg.has("ARM64") {
+        panic!("arm64 uses the builtin rustc aarch64-unknown-none target");
+    } else if cfg.has("X86_64") {
         ts.push("arch", "x86_64");
         ts.push(
             "data-layout",
Signature · 1 line
2.30.2
Tested-by on v3 1/1
#2 lore raw

Re: [PATCH v3 1/1] arm64: rust: Enable Rust support for AArch64

Quoted 8 lines · Jamie Cunliffe

This commit provides the build flags for Rust for AArch64. The core Rust support already in the kernel does the rest. This enables the PAC ret and BTI options in the Rust build flags to match the options that are used when building C.

The Rust samples have been tested with this commit.

Like I post previouly, the following is needed upon this to make the command "make LLVM=1" still work on x86:

(the reason is https://lore.kernel.org/rust-for-linux/ZNu9JhYlsFWmliQE@boqun-archlinux/)

rust/Makefile +1 -1
index 3a784aa3a8a9..ac2ae21b830f 100644
@@ -429,7 +429,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)
-ifeq ($(ARCH),x86_64)
+ifdef CONFIG_X86_64
 $(obj)/core.o: scripts/target.json
 endif
 
scripts/Makefile +1 -1
index 0aa78339128c..a1b01c16688d 100644
@@ -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
 
-ifeq ($(ARCH),x86_64)
+ifdef CONFIG_X86_64
 always-$(CONFIG_RUST)                                  += target.json
 filechk_rust_target = $< < include/config/auto.conf
 $(obj)/target.json: scripts/generate_rust_target include/config/auto.conf FORCE

I put an updated version in the rust-dev branch:

https://github.com/Rust-for-Linux/linux/commits/rust-dev

and have tested it (with kunit), so feel free to add:

More information for ARM64 maintainers, I have queued the previous version for a while and tested with it whenever I updated the rust-dev, so far no problem. I also have accesses to some ARM64 VMs (on Microsoft Azure), if you want to see more testing, feel free to let me know.

Regards,
Boqun

Quoted 145 lines
---
 Documentation/rust/arch-support.rst | 1 +
 Makefile                            | 1 -
 arch/arm64/Kconfig                  | 1 +
 arch/arm64/Makefile                 | 4 ++++
 arch/x86/Makefile                   | 1 +
 rust/Makefile                       | 6 +++++-
 scripts/Makefile                    | 5 +++--
 scripts/generate_rust_target.rs     | 4 +++-
 8 files changed, 18 insertions(+), 5 deletions(-)
Documentation/rust/arch-support.rst +1 -0
index b91e9ef4d0c2..6bcb3b97c5b6 100644
@@ -15,6 +15,7 @@ support corresponds to ``S`` values in the ``MAINTAINERS`` file.
 ============  ================  ==============================================
 Architecture  Level of support  Constraints
 ============  ================  ==============================================
+``arm64``     Maintained        Little Endian only.
 ``um``        Maintained        ``x86_64`` only.
 ``x86``       Maintained        ``x86_64`` only.
 ============  ================  ==============================================
Makefile +0 -1
index 88ebf6547964..23100f193da3 100644
@@ -566,7 +566,6 @@ KBUILD_CFLAGS += -fno-strict-aliasing
 
 KBUILD_CPPFLAGS := -D__KERNEL__
 KBUILD_RUSTFLAGS := $(rust_common_flags) \
-		    --target=$(objtree)/scripts/target.json \
 		    -Cpanic=abort -Cembed-bitcode=n -Clto=n \
 		    -Cforce-unwind-tables=n -Ccodegen-units=1 \
 		    -Csymbol-mangling-version=v0 \
arch/arm64/Kconfig +1 -0
index 78f20e632712..d72618433521 100644
@@ -227,6 +227,7 @@ config ARM64
 	select HAVE_FUNCTION_ARG_ACCESS_API
 	select MMU_GATHER_RCU_TABLE_FREE
 	select HAVE_RSEQ
+	select HAVE_RUST if CPU_LITTLE_ENDIAN
 	select HAVE_STACKPROTECTOR
 	select HAVE_SYSCALL_TRACEPOINTS
 	select HAVE_KPROBES
arch/arm64/Makefile +4 -0
index 2d49aea0ff67..4562a8173e90 100644
@@ -41,6 +41,8 @@ KBUILD_CFLAGS	+= -mgeneral-regs-only	\
 KBUILD_CFLAGS	+= $(call cc-disable-warning, psabi)
 KBUILD_AFLAGS	+= $(compat_vdso)
 
+KBUILD_RUSTFLAGS += --target aarch64-unknown-none -C target-feature="-neon"
+
 KBUILD_CFLAGS	+= $(call cc-option,-mabi=lp64)
 KBUILD_AFLAGS	+= $(call cc-option,-mabi=lp64)
 
@@ -65,7 +67,9 @@ endif
 
 ifeq ($(CONFIG_ARM64_BTI_KERNEL),y)
   KBUILD_CFLAGS += -mbranch-protection=pac-ret+bti
+  KBUILD_RUSTFLAGS += -Z branch-protection=bti,pac-ret
 else ifeq ($(CONFIG_ARM64_PTR_AUTH_KERNEL),y)
+  KBUILD_RUSTFLAGS += -Z branch-protection=pac-ret
   ifeq ($(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET),y)
     KBUILD_CFLAGS += -mbranch-protection=pac-ret
   else
arch/x86/Makefile +1 -0
index 5bfe5caaa444..0f339d4abd40 100644
@@ -68,6 +68,7 @@ export BITS
 #    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53383
 #
 KBUILD_CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx
+KBUILD_RUSTFLAGS += --target=$(objtree)/scripts/target.json
 KBUILD_RUSTFLAGS += -Ctarget-feature=-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-avx,-avx2
 
 ifeq ($(CONFIG_X86_KERNEL_IBT),y)
rust/Makefile +5 -1
index 87958e864be0..b3127501e0e1 100644
@@ -294,6 +294,7 @@ bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
 
 # Derived from `scripts/Makefile.clang`.
 BINDGEN_TARGET_x86	:= x86_64-linux-gnu
+BINDGEN_TARGET_arm64    := aarch64-linux-gnu
 BINDGEN_TARGET		:= $(BINDGEN_TARGET_$(SRCARCH))
 
 # All warnings are inhibited since GCC builds are very experimental,
@@ -428,8 +429,11 @@ $(obj)/core.o: private skip_clippy = 1
 $(obj)/core.o: private skip_flags = -Dunreachable_pub
 $(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
 $(obj)/core.o: private rustc_target_flags = $(core-cfgs)
-$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs scripts/target.json FORCE
+$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
 	$(call if_changed_dep,rustc_library)
+ifeq ($(ARCH),x86_64)
+$(obj)/core.o: scripts/target.json
+endif
 
 $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
 $(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
scripts/Makefile +3 -2
index 576cf64be667..0aa78339128c 100644
@@ -11,12 +11,13 @@ hostprogs-always-$(CONFIG_MODULE_SIG_FORMAT)		+= sign-file
 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
-always-$(CONFIG_RUST)					+= target.json
 
+ifeq ($(ARCH),x86_64)
+always-$(CONFIG_RUST)					+= target.json
 filechk_rust_target = $< < include/config/auto.conf
-
 $(obj)/target.json: scripts/generate_rust_target include/config/auto.conf FORCE
 	$(call filechk,rust_target)
+endif
 
 hostprogs += generate_rust_target
 generate_rust_target-rust := y
scripts/generate_rust_target.rs +3 -1
index 3c6cbe2b278d..ec5ef35dbe52 100644
@@ -148,7 +148,9 @@ fn main() {
     let mut ts = TargetSpec::new();
 
     // `llvm-target`s are taken from `scripts/Makefile.clang`.
-    if cfg.has("X86_64") {
+    if cfg.has("ARM64") {
+        panic!("arm64 uses the builtin rustc aarch64-unknown-none target");
+    } else if cfg.has("X86_64") {
         ts.push("arch", "x86_64");
         ts.push(
             "data-layout",
Signature · 1 line
2.30.2
#3 lore raw

Re: [PATCH v3 1/1] arm64: rust: Enable Rust support for AArch64

In reply to Jamie Cunliffe

arch/x86/Makefile export BITS · line 71

Quoted 7 lines
arch/x86/Makefile +1 -0
@@ -68,6 +68,7 @@ export BITS
 #    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53383
 #
 KBUILD_CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx
+KBUILD_RUSTFLAGS += --target=$(objtree)/scripts/target.json

It seems odd that adding arm64 support requires adding to the x86 Makefile.

Should this be moved into a patch of its own explaining why it is needed? Do the refactoring first, then add support for a new architecture?

Andrew

#4 lore raw

Re: [PATCH v3 1/1] arm64: rust: Enable Rust support for AArch64

Quoted 7 lines · Andrew Lunn

It seems odd that adding arm64 support requires adding to the x86 Makefile.

Should this be moved into a patch of its own explaining why it is needed? Do the refactoring first, then add support for a new architecture?

That would be nice, yeah.

(It is needed because they are not using the target specification file for arm64, which is something that an architecture should use only if required. Eventually the goal is to not use it for any architecture).

Cheers,
Miguel

#5 lore raw

Re: [PATCH v3 1/1] arm64: rust: Enable Rust support for AArch64

In reply to Jamie Cunliffe
Quoted 3 lines · Jamie Cunliffe

BINDGEN_TARGET_x86 := x86_64-linux-gnu
+BINDGEN_TARGET_arm64 := aarch64-linux-gnu

Tabs vs. spaces.

Quoted 2 lines

filechk_rust_target = $< < include/config/auto.conf
-

Spurious removal?

Cheers,
Miguel

#6 lore raw

Re: [PATCH v3 1/1] arm64: rust: Enable Rust support for AArch64

In reply to Jamie Cunliffe
Quoted 2 lines · Jamie Cunliffe

+KBUILD_RUSTFLAGS += --target aarch64-unknown-none -C target-feature="-neon"

For consistency with other flags we pass in Rust, please avoid the space after -C.

By the way, perhaps we could already add others like (untested):

ifeq ($(CONFIG_ARM64_USE_LSE_ATOMICS),y)
+KBUILD_RUSTFLAGS += -Ctarget-feature="+lse"
ifneq ($(CONFIG_ARM64_LSE_ATOMICS),y)
@echo "warning: LSE atomics not supported by binutils" >&2
endif

This one in particular (LSE) came up in a Binder discussion yesterday.

Cheers,
Miguel

#7 lore raw

Re: [PATCH v3 1/1] arm64: rust: Enable Rust support for AArch64

Quoted 8 lines · Miguel Ojeda

By the way, perhaps we could already add others like (untested):

ifeq ($(CONFIG_ARM64_USE_LSE_ATOMICS),y)
+KBUILD_RUSTFLAGS += -Ctarget-feature="+lse"
ifneq ($(CONFIG_ARM64_LSE_ATOMICS),y)
@echo "warning: LSE atomics not supported by binutils" >&2
endif

Ignore this, sorry -- as Boqun reminded me, this uses an ALTERNATIVE, rather than enable it unconditionally. See
https://github.com/Rust-for-Linux/linux/issues/970.

Cheers,
Miguel

maintainer Acked-by on v3 1/1
#8 lore raw

Re: [PATCH v3 1/1] arm64: rust: Enable Rust support for AArch64

In reply to Jamie Cunliffe
Quoted 8 lines · Jamie Cunliffe

This commit provides the build flags for Rust for AArch64. The core Rust support already in the kernel does the rest. This enables the PAC ret and BTI options in the Rust build flags to match the options that are used when building C.

The Rust samples have been tested with this commit.

Quoted 10 lines
---
 Documentation/rust/arch-support.rst | 1 +
 Makefile                            | 1 -
 arch/arm64/Kconfig                  | 1 +
 arch/arm64/Makefile                 | 4 ++++
 arch/x86/Makefile                   | 1 +
 rust/Makefile                       | 6 +++++-
 scripts/Makefile                    | 5 +++--
 scripts/generate_rust_target.rs     | 4 +++-
 8 files changed, 18 insertions(+), 5 deletions(-)

Will