Skip to contentSkip to conversation

Rust enablement for AArch64

PATCH Applied Jamie Cunliffe 17 participants 38 messages Oct 20, 2023 – Feb 9, 2024

Subsystems (7)
Documentation x86 Kbuild Common clk +3 more subsystems
lore
Share & tools
lore
#0 lore raw

v4 cover Rust enablement for AArch64

Enable Rust support for the AArch64 architecture.

Since the v3 this has been refactored to split up the x86 Makefile changes. Updated the x86-64 conditionals as suggested by Boqun and addressed the formatting issues Miguel raised.

Jamie Cunliffe (2):
rust: Refactor the build target to allow the use of builtin targets
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                    | 4 +++-
 scripts/generate_rust_target.rs     | 4 +++-
 8 files changed, 18 insertions(+), 4 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v4 1/2 rust: Refactor the build target to allow the use of builtin targets

Eventually we want all architectures to be using the target as defined by rustc. However currently some architectures can't do that and are using the target.json specification. This puts in place the foundation to allow the use of the builtin target definition or a target.json specification.

---
 Makefile          | 1 -
 arch/x86/Makefile | 1 +
 rust/Makefile     | 5 ++++-
 scripts/Makefile  | 4 +++-
 4 files changed, 8 insertions(+), 3 deletions(-)
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/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 +4 -1
index 87958e864be0..8d75a6ffe951 100644
@@ -428,8 +428,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)
+ifdef CONFIG_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 -1
index 576cf64be667..c85d130a4125 100644
@@ -11,12 +11,14 @@ 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
 
+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
 	$(call filechk,rust_target)
+endif
 
 hostprogs += generate_rust_target
 generate_rust_target-rust := y
Signature · 1 line
2.30.2
maintainer
#2 lore raw

Re: [PATCH v4 1/2] rust: Refactor the build target to allow the use of builtin targets

Quoted 6 lines · Jamie Cunliffe

Eventually we want all architectures to be using the target as defined by rustc. However currently some architectures can't do that and are using the target.json specification. This puts in place the foundation to allow the use of the builtin target definition or a target.json specification.

Quoted 7 lines
---
 Makefile          | 1 -
 arch/x86/Makefile | 1 +
 rust/Makefile     | 5 ++++-
 scripts/Makefile  | 4 +++-
 4 files changed, 8 insertions(+), 3 deletions(-)

Adding the x86 maintainers - no functional change for x86, any objection to this patch going in via the arm64 tree (part of the two-patch series enabling Rust for arm64)?

Thanks.

(keeping the rest of the patch below for your reference)

Quoted 63 lines
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/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 +4 -1
index 87958e864be0..8d75a6ffe951 100644
@@ -428,8 +428,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)
+ifdef CONFIG_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 -1
index 576cf64be667..c85d130a4125 100644
@@ -11,12 +11,14 @@ 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
 
+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
 	$(call filechk,rust_target)
+endif
 
 hostprogs += generate_rust_target
 generate_rust_target-rust := y
Signature · 1 line
2.30.2
#3 lore raw

Re: [PATCH v4 1/2] rust: Refactor the build target to allow the use of builtin targets

Quoted 15 lines · Catalin Marinas
Quoted 6 lines · Jamie Cunliffe

Eventually we want all architectures to be using the target as defined by rustc. However currently some architectures can't do that and are using the target.json specification. This puts in place the foundation to allow the use of the builtin target definition or a target.json specification.

Quoted 7 lines
---
 Makefile          | 1 -
 arch/x86/Makefile | 1 +
 rust/Makefile     | 5 ++++-
 scripts/Makefile  | 4 +++-
 4 files changed, 8 insertions(+), 3 deletions(-)
Quoted 7 lines

Adding the x86 maintainers - no functional change for x86, any objection to this patch going in via the arm64 tree (part of the two-patch series enabling Rust for arm64)?

Thanks.

(keeping the rest of the patch below for your reference)

If Masahiro Yamada (kbuild maintainer, Cc:-ed) is fine with it, then it will be OK to me too.

Thanks,

Ingo

reviewer
#4 lore raw

Re: [PATCH v4 1/2] rust: Refactor the build target to allow the use of builtin targets

Quoted 10 lines · Ingo Molnar
Quoted 7 lines · Catalin Marinas

Adding the x86 maintainers - no functional change for x86, any objection to this patch going in via the arm64 tree (part of the two-patch series enabling Rust for arm64)?

Thanks.

Quoted 9 lines
Quoted 1 line

(keeping the rest of the patch below for your reference)

If Masahiro Yamada (kbuild maintainer, Cc:-ed) is fine with it, then it will be OK to me too.

Thanks,

Ingo

I don't think this has been picked up yet. Is it only waiting on Masahiro 's ack or is a new version expected?

- Trevor

maintainer
#5 lore raw

Re: [PATCH v4 1/2] rust: Refactor the build target to allow the use of builtin targets

Quoted 12 lines · Trevor Gross
Quoted 11 lines · Ingo Molnar
Quoted 7 lines · Catalin Marinas

Adding the x86 maintainers - no functional change for x86, any objection to this patch going in via the arm64 tree (part of the two-patch series enabling Rust for arm64)?

Thanks.

(keeping the rest of the patch below for your reference)

If Masahiro Yamada (kbuild maintainer, Cc:-ed) is fine with it, then it will be OK to me too.

[...]

Quoted 2 lines

I don't think this has been picked up yet. Is it only waiting on Masahiro 's ack or is a new version expected?

I'd say Masahiro's ack only. However, it's probably too late for 6.7 now, the merging window is already open and maintainers tend to pick stuff up in the weeks before.

Signature · 1 line
Catalin
reviewer
#6 lore raw

Re: [PATCH v4 1/2] rust: Refactor the build target to allow the use of builtin targets

Quoted 3 lines · Catalin Marinas

I'd say Masahiro's ack only. However, it's probably too late for 6.7 now, the merging window is already open and maintainers tend to pick stuff up in the weeks before.

Right, thanks for confirming. I was unsure whether the BE discussion changed anything, but that could likely come separately.

- Trevor

maintainer
#7 lore raw

Re: [PATCH v4 1/2] rust: Refactor the build target to allow the use of builtin targets

In reply to Catalin Marinas
Quoted 13 lines · Catalin Marinas
Quoted 6 lines · Trevor Gross
Quoted 5 lines · Ingo Molnar
Quoted 4 lines · Catalin Marinas

Adding the x86 maintainers - no functional change for x86, any objection to this patch going in via the arm64 tree (part of the two-patch series enabling Rust for arm64)?

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

Thanks.

(keeping the rest of the patch below for your reference)

If Masahiro Yamada (kbuild maintainer, Cc:-ed) is fine with it, then it will be OK to me too.

Quoted 7 lines

[...]

Quoted 2 lines

I don't think this has been picked up yet. Is it only waiting on Masahiro 's ack or is a new version expected?

I'd say Masahiro's ack only. However, it's probably too late for 6.7 now, the merging window is already open and maintainers tend to pick stuff up in the weeks before.

Masahiro -- please can we have your Ack on this so that we can take it via the arm64 tree?

Thanks,

Will

reviewer
#8 lore raw

Re: [PATCH v4 1/2] rust: Refactor the build target to allow the use of builtin targets

In reply to Jamie Cunliffe
Quoted 7 lines · Jamie Cunliffe

Eventually we want all architectures to be using the target as defined by rustc. However currently some architectures can't do that and are using the target.json specification. This puts in place the foundation to allow the use of the builtin target definition or a target.json specification.

Quoted 7 lines
---
 Makefile          | 1 -
 arch/x86/Makefile | 1 +
 rust/Makefile     | 5 ++++-
 scripts/Makefile  | 4 +++-
 4 files changed, 8 insertions(+), 3 deletions(-)

Adding LoongArch maintainers - this patch will need a small update to arch/loongarch/Makefile similar to the change in arch/x86/Makefile (included below), since that support is now in-tree and uses generate_rust_target [1]. Also a very minor conflict in docs at patch 2/2.

I believe this patchset is still waiting on an ack from the kbuild team. Series at [2] for reference.

Thanks,
Trevor

[1]: https://lore.kernel.org/rust-for-linux/[email protected]/
[2]: https://lore.kernel.org/rust-for-linux/[email protected]/

Quoted 65 lines
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/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 +4 -1
index 87958e864be0..8d75a6ffe951 100644
@@ -428,8 +428,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)
+ifdef CONFIG_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..c85d130a4125 100644
@@ -11,12 +11,14 @@ 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
 
+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
        $(call filechk,rust_target)
+endif
 
 hostprogs += generate_rust_target
 generate_rust_target-rust := y
--

2.30.2

#9 lore raw

Re: [PATCH v4 1/2] rust: Refactor the build target to allow the use of builtin targets

Hello,

Quoted 23 lines · Trevor Gross
Quoted 7 lines · Jamie Cunliffe

Eventually we want all architectures to be using the target as defined by rustc. However currently some architectures can't do that and are using the target.json specification. This puts in place the foundation to allow the use of the builtin target definition or a target.json specification.

Quoted 7 lines
---
 Makefile          | 1 -
 arch/x86/Makefile | 1 +
 rust/Makefile     | 5 ++++-
 scripts/Makefile  | 4 +++-
 4 files changed, 8 insertions(+), 3 deletions(-)

Adding LoongArch maintainers - this patch will need a small update to arch/loongarch/Makefile similar to the change in arch/x86/Makefile (included below), since that support is now in-tree and uses generate_rust_target [1]. Also a very minor conflict in docs at patch 2/2.

Quoted 8 lines

I believe this patchset is still waiting on an ack from the kbuild team. Series at [2] for reference.

Thanks,
Trevor

[1]: https://lore.kernel.org/rust-for-linux/[email protected]/
[2]: https://lore.kernel.org/rust-for-linux/[email protected]/

I can confirm that the fix patch [2], based on patch [1], is suitable for LoongArch. LoongArch encounters no issues when using the built-in loongarch64-unknown-none-softfloat target. Once patch [1] is merged into the mainline, I'll send a patch to switch to using the built-in target.

Thanks,
Rui

[1]: https://lore.kernel.org/rust-for-linux/[email protected]/
[2]: https://github.com/heiher/linux/commit/c504d83a6edd963e2ba58ec538e3f06da78b187e.patch

Quoted 67 lines
Quoted 66 lines
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/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 +4 -1
index 87958e864be0..8d75a6ffe951 100644
@@ -428,8 +428,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)
+ifdef CONFIG_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..c85d130a4125 100644
@@ -11,12 +11,14 @@ 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
 
+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
        $(call filechk,rust_target)
+endif
 
 hostprogs += generate_rust_target
 generate_rust_target-rust := y
--

2.30.2

#10 lore raw

Re: [PATCH v4 1/2] rust: Refactor the build target to allow the use of builtin targets

Quoted 33 lines · WANG Rui

Hello,

Quoted 29 lines · Trevor Gross
Quoted 7 lines · Jamie Cunliffe

Eventually we want all architectures to be using the target as defined by rustc. However currently some architectures can't do that and are using the target.json specification. This puts in place the foundation to allow the use of the builtin target definition or a target.json specification.

Quoted 7 lines
---
 Makefile          | 1 -
 arch/x86/Makefile | 1 +
 rust/Makefile     | 5 ++++-
 scripts/Makefile  | 4 +++-
 4 files changed, 8 insertions(+), 3 deletions(-)

Adding LoongArch maintainers - this patch will need a small update to arch/loongarch/Makefile similar to the change in arch/x86/Makefile (included below), since that support is now in-tree and uses generate_rust_target [1]. Also a very minor conflict in docs at patch 2/2.

I believe this patchset is still waiting on an ack from the kbuild team. Series at [2] for reference.

Thanks,
Trevor

Quoted 8 lines
Quoted 2 lines

[1]: https://lore.kernel.org/rust-for-linux/[email protected]/
[2]: https://lore.kernel.org/rust-for-linux/[email protected]/

I can confirm that the fix patch [2], based on patch [1], is suitable for LoongArch. LoongArch encounters no issues when using the built-in loongarch64-unknown-none-softfloat target. Once patch [1] is merged into the mainline, I'll send a patch to switch to using the built-in target.

But, [1] and [2] must be squashed together. Otherwise, [1] is changing the behavior of loongach, while the commit description does not mention it.

Quoted 75 lines

Thanks,
Rui

[1]: https://lore.kernel.org/rust-for-linux/[email protected]/
[2]: https://github.com/heiher/linux/commit/c504d83a6edd963e2ba58ec538e3f06da78b187e.patch

Quoted 67 lines
Quoted 66 lines
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/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 +4 -1
index 87958e864be0..8d75a6ffe951 100644
@@ -428,8 +428,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)
+ifdef CONFIG_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..c85d130a4125 100644
@@ -11,12 +11,14 @@ 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
 
+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
        $(call filechk,rust_target)
+endif
 
 hostprogs += generate_rust_target
 generate_rust_target-rust := y
--

2.30.2

Signature · 2 lines
Best Regards
Masahiro Yamada
#11 lore raw

Re: [PATCH v4 1/2] rust: Refactor the build target to allow the use of builtin targets

Quoted 44 lines · Masahiro Yamada
Quoted 33 lines · WANG Rui

Hello,

Quoted 29 lines · Trevor Gross
Quoted 7 lines · Jamie Cunliffe

Eventually we want all architectures to be using the target as defined by rustc. However currently some architectures can't do that and are using the target.json specification. This puts in place the foundation to allow the use of the builtin target definition or a target.json specification.

Quoted 7 lines
---
 Makefile          | 1 -
 arch/x86/Makefile | 1 +
 rust/Makefile     | 5 ++++-
 scripts/Makefile  | 4 +++-
 4 files changed, 8 insertions(+), 3 deletions(-)

Adding LoongArch maintainers - this patch will need a small update to arch/loongarch/Makefile similar to the change in arch/x86/Makefile (included below), since that support is now in-tree and uses generate_rust_target [1]. Also a very minor conflict in docs at patch 2/2.

I believe this patchset is still waiting on an ack from the kbuild team. Series at [2] for reference.

Thanks,
Trevor

Quoted 8 lines
Quoted 2 lines

[1]: https://lore.kernel.org/rust-for-linux/[email protected]/
[2]: https://lore.kernel.org/rust-for-linux/[email protected]/

I can confirm that the fix patch [2], based on patch [1], is suitable for LoongArch. LoongArch encounters no issues when using the built-in loongarch64-unknown-none-softfloat target. Once patch [1] is merged into the mainline, I'll send a patch to switch to using the built-in target.

Quoted 3 lines

But, [1] and [2] must be squashed together. Otherwise, [1] is changing the behavior of loongach, while the commit description does not mention it.

I agree. It would be great if Jamie could squash them together.

Thanks,
Rui

Quoted 85 lines
Quoted 4 lines
Quoted 75 lines

Thanks,
Rui

[1]: https://lore.kernel.org/rust-for-linux/[email protected]/
[2]: https://github.com/heiher/linux/commit/c504d83a6edd963e2ba58ec538e3f06da78b187e.patch

Quoted 67 lines
Quoted 66 lines
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/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 +4 -1
index 87958e864be0..8d75a6ffe951 100644
@@ -428,8 +428,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)
+ifdef CONFIG_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..c85d130a4125 100644
@@ -11,12 +11,14 @@ 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
 
+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
        $(call filechk,rust_target)
+endif
 
 hostprogs += generate_rust_target
 generate_rust_target-rust := y
--

2.30.2

--
Best Regards
Masahiro Yamada

#12 lore raw

Re: [PATCH v4 1/2] rust: Refactor the build target to allow the use of builtin targets

Quoted 2 lines · WANG Rui

I agree. It would be great if Jamie could squash them together.

Either that or perhaps Catalin could do it at apply time with your Co-developed-by and/or Huacai's Acked-by? I could also take if needed.

Thanks Masahiro & Rui!

Cheers,
Miguel

reviewer Tested-by on v4 1/2
#13 lore raw

Re: [PATCH v4 1/2] rust: Refactor the build target to allow the use of builtin targets

In reply to Jamie Cunliffe
Quoted 7 lines · Jamie Cunliffe

Eventually we want all architectures to be using the target as defined by rustc. However currently some architectures can't do that and are using the target.json specification. This puts in place the foundation to allow the use of the builtin target definition or a target.json specification.

Tested with Rust Binder.

Acked-by on v4 1/2
#14 lore raw

Re: [PATCH v4 1/2] rust: Refactor the build target to allow the use of builtin targets

In reply to Jamie Cunliffe
Quoted 61 lines · Jamie Cunliffe

Eventually we want all architectures to be using the target as defined by rustc. However currently some architectures can't do that and are using the target.json specification. This puts in place the foundation to allow the use of the builtin target definition or a target.json specification.

---
 Makefile          | 1 -
 arch/x86/Makefile | 1 +
 rust/Makefile     | 5 ++++-
 scripts/Makefile  | 4 +++-
 4 files changed, 8 insertions(+), 3 deletions(-)
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/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 +4 -1
index 87958e864be0..8d75a6ffe951 100644
@@ -428,8 +428,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)
+ifdef CONFIG_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 +0 -0
index 576cf64be667..c85d130a4125 100644

scripts/Makefile hostprogs-always-$(CONFIG_MODULE_SIG_FORMAT) += sign-file · line 15

Quoted 6 lines
scripts/Makefile +1 -1
@@ -11,12 +11,14 @@ 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
 
+ifdef CONFIG_X86_64

Sorry for the delay.

I did not notice that this was waiting for my Ack.

Now, Rust for loongarch is upstreamed, and it also uses the custom target.

This block should be changed to:

ifneq ($(CONFIG_X86_64)$(CONFIG_LOONGARCH),)
...
endif

With that changed,

Or, if you want to make it a little bit cleaner,
perhaps you can introduce a new CONFIG option.

Let's say CONFIG_RUST_USE_CUSTOM_TARGET
(please pick up any option name you like).

In scripts/Makefile,

-always-$(CONFIG_RUST) += target.json
+always-$(CONFIG_RUST_USE_CUSTOM_TARGET) += target.json

In the top Makefile,

ifdef CONFIG_RUST_USE_CUSTOM_TARGET KBUILD_RUSTFLAGS += --target=$(objtree)/scripts/target.json endif

Quoted 13 lines
scripts/Makefile +2 -1
+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
--

2.30.2

Signature · 2 lines
Best Regards
Masahiro Yamada
#15 lore raw

PATCH v4 2/2 arm64: rust: Enable Rust support for AArch64

In reply to 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.

---
 Documentation/rust/arch-support.rst | 1 +
 arch/arm64/Kconfig                  | 1 +
 arch/arm64/Makefile                 | 4 ++++
 rust/Makefile                       | 1 +
 scripts/generate_rust_target.rs     | 4 +++-
 5 files changed, 10 insertions(+), 1 deletion(-)
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.
 ============  ================  ==============================================
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..52d75c9483a9 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 -Ctarget-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 += -Zbranch-protection=bti,pac-ret
 else ifeq ($(CONFIG_ARM64_PTR_AUTH_KERNEL),y)
+  KBUILD_RUSTFLAGS += -Zbranch-protection=pac-ret
   ifeq ($(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET),y)
     KBUILD_CFLAGS += -mbranch-protection=pac-ret
   else
rust/Makefile +1 -0
index 8d75a6ffe951..8ced65c9b905 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,
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
maintainer Acked-by on v4 2/2
#16 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

Quoted 9 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.

Miguel, Will acked this already so I think it's good to go. Do you plan to pick both patches via the Rust tree?

Thanks.

Acked-by
#17 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

Quoted 3 lines · Catalin Marinas

Miguel, Will acked this already so I think it's good to go. Do you plan to pick both patches via the Rust tree?

Thanks for taking a look!

As you prefer -- ideally the changes for each architecture would go through their own trees (we are trying to get maintainers of the different parts of the kernel involved with Rust for their own bits and pieces), but if you think there is a reason to avoid that, I can also take them as fallback for the time being.

Quoted 1 line

If you decide to take them:

I have boot tested it in aarch64 QEMU and saw the KUnit Rust tests passing and checked that the x86 side still works fine.

Cheers,
Miguel

maintainer
#18 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

Quoted 11 lines · Miguel Ojeda
Quoted 2 lines · Catalin Marinas

Miguel, Will acked this already so I think it's good to go. Do you plan to pick both patches via the Rust tree?

Thanks for taking a look!

As you prefer -- ideally the changes for each architecture would go through their own trees (we are trying to get maintainers of the different parts of the kernel involved with Rust for their own bits and pieces), but if you think there is a reason to avoid that, I can also take them as fallback for the time being.

The main reason I asked was that the first patch touches x86 and none of the x86 maintainers have been cc'ed or acked the patch. I thought there's some deal that Rust-related changes go via you tree.

Quoted 8 lines
Quoted 1 line

If you decide to take them:

I have boot tested it in aarch64 QEMU and saw the KUnit Rust tests passing and checked that the x86 side still works fine.

Thanks. I'll reply to the first patch and cc some x86 maintainers. I assume your ack applies to the first patch as well.

Signature · 1 line
Catalin
#19 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

Quoted 3 lines · Catalin Marinas

The main reason I asked was that the first patch touches x86 and none of the x86 maintainers have been cc'ed or acked the patch.

Ah, yeah, they should have been Cc'd.

Quoted 2 lines

I thought
there's some deal that Rust-related changes go via you tree.

I see. The initial pieces went through our tree, yeah, and we are happy to take things if needed (without the intention to bypass maintainers), but if maintainers can take things already, that is way better; e.g. some subsystems have already started to do that (Kbuild, KUnit, wq...). So thanks a lot!

The only thing we ask for, for the time being, is that patches relevant to Rust or that contain Rust code are Cc'd into the rust-for-linux ML if possible (i.e. in addition to their usual lists), so that anybody that wants to follow things in the initial setup period can do so in a single place (e.g. maintainers that want to see the sort of thing to look out for in reviews).

Quoted 2 lines

Thanks. I'll reply to the first patch and cc some x86 maintainers. I assume your ack applies to the first patch as well.

Yeah, it does.

Thanks again Catalin & Will!

Cheers,
Miguel

#20 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

In reply to Jamie Cunliffe
Quoted 1 line

+``arm64`` Maintained Little Endian only.

This question is just out of curiosity, not the patchset itself.

What is missing to make big endian work?

Network developers have expressed an interesting in testing Rust code on big endian systems, since the code should work on both endians. It might be easier to get an ARM board running big endian than get access to an S390x machine.

Andrew

#21 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

Quoted 6 lines · Andrew Lunn
Quoted 1 line

+``arm64`` Maintained Little Endian only.

This question is just out of curiosity, not the patchset itself.

What is missing to make big endian work?

FWIW, I tried the following:

arch/arm64/Kconfig +1 -1
index 8784284988e5..b697c2d7da68 100644
@@ -227,7 +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_RUST
        select HAVE_STACKPROTECTOR
        select HAVE_SYSCALL_TRACEPOINTS
        select HAVE_KPROBES
arch/arm64/Makefile +4 -0
index 4562a8173e90..4621f1e00e06 100644
@@ -41,7 +41,11 @@ KBUILD_CFLAGS        += -mgeneral-regs-only  \
 KBUILD_CFLAGS  += $(call cc-disable-warning, psabi)
 KBUILD_AFLAGS  += $(compat_vdso)
 
+ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
+KBUILD_RUSTFLAGS += --target aarch64_be-unknown-linux-gnu -C target-feature="-neon"
+else
 KBUILD_RUSTFLAGS += --target aarch64-unknown-none -C target-feature="-neon"
+endif
 
 KBUILD_CFLAGS  += $(call cc-option,-mabi=lp64)
 KBUILD_AFLAGS  += $(call cc-option,-mabi=lp64)

and ran the following kunit command (it will run a few tests in a qemu emulated VM):

	./tools/testing/kunit/kunit.py run --make_options LLVM=1 --arch arm64 --kconfig_add CONFIG_RUST=y --kconfig_add CONFIG_CPU_BIG_ENDIAN=y

The kernel was built successfully, and all Rust related tests passed.

Of course this doesn't mean a lot, we still need people with deep Rust compiler knowledge to confirm whether the support is completed or not. But I think if people want to do experiments, the tool is there.

P.S. An unrelated topic: I found a few clk related tests (in drivers/clk/clk-gate_test.c IIUC) don't pass (mostly due to that the expected values don't handle big endian), a sample failure output:

[11:13:26] # clk_gate_test_enable: EXPECTATION FAILED at drivers/clk/clk-gate_test.c:169
[11:13:26] Expected enable_val == ctx->fake_reg, but
[11:13:26] enable_val == 32 (0x20)
[11:13:26] ctx->fake_reg == 536870912 (0x20000000)
[11:13:26] clk_unregister: unregistering prepared clock: test_gate [11:13:26] clk_unregister: unregistering prepared clock: test_parent [11:13:26] [FAILED] clk_gate_test_enable

(Cc clk folks)

Regards,
Boqun

Quoted 6 lines

Network developers have expressed an interesting in testing Rust code on big endian systems, since the code should work on both endians. It might be easier to get an ARM board running big endian than get access to an S390x machine.

Andrew

#22 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

Quoted 3 lines

Of course this doesn't mean a lot, we still need people with deep Rust compiler knowledge to confirm whether the support is completed or not. But I think if people want to do experiments, the tool is there.

Thanks for testing this, its good to know.

Andrew

#23 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

In reply to Boqun Feng
Quoted 43 lines · Boqun Feng
Quoted 6 lines · Andrew Lunn
Quoted 1 line

+``arm64`` Maintained Little Endian only.

This question is just out of curiosity, not the patchset itself.

What is missing to make big endian work?

FWIW, I tried the following:

arch/arm64/Kconfig +1 -1
index 8784284988e5..b697c2d7da68 100644
@@ -227,7 +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_RUST
         select HAVE_STACKPROTECTOR
         select HAVE_SYSCALL_TRACEPOINTS
         select HAVE_KPROBES
arch/arm64/Makefile +4 -0
index 4562a8173e90..4621f1e00e06 100644
@@ -41,7 +41,11 @@ KBUILD_CFLAGS        += -mgeneral-regs-only  \
  KBUILD_CFLAGS  += $(call cc-disable-warning, psabi)
  KBUILD_AFLAGS  += $(compat_vdso)
 
+ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
+KBUILD_RUSTFLAGS += --target aarch64_be-unknown-linux-gnu -C target-feature="-neon"
+else
  KBUILD_RUSTFLAGS += --target aarch64-unknown-none -C target-feature="-neon"
+endif
 
  KBUILD_CFLAGS  += $(call cc-option,-mabi=lp64)
  KBUILD_AFLAGS  += $(call cc-option,-mabi=lp64)

and ran the following kunit command (it will run a few tests in a qemu emulated VM):

Quoted 7 lines
	./tools/testing/kunit/kunit.py run --make_options LLVM=1 --arch arm64 --kconfig_add CONFIG_RUST=y --kconfig_add CONFIG_CPU_BIG_ENDIAN=y

The kernel was built successfully, and all Rust related tests passed.

Of course this doesn't mean a lot, we still need people with deep Rust compiler knowledge to confirm whether the support is completed or not. But I think if people want to do experiments, the tool is there.

For what it's worth, I have some experience with big endian Rust in userspace, and it generally works without issues. I would expect that it doesn't need any special support beyond setting the target architecture properly.

Alice

#24 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

In reply to Boqun Feng
Quoted 2 lines · Boqun Feng

But I think if people want to do experiments, the tool is there.

Yeah. If the Arm maintainers are OK adding it, I think we should also put it in (next cycle perhaps) -- the sooner we have it in-tree, the more testing it will hopefully get over time, and we can eventually ask the CIs to add a run with it if they have the resources.

Cheers,
Miguel

#25 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

Quoted 8 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng

But I think if people want to do experiments, the tool is there.

Yeah. If the Arm maintainers are OK adding it, I think we should also put it in (next cycle perhaps) -- the sooner we have it in-tree, the more testing it will hopefully get over time, and we can eventually ask the CIs to add a run with it if they have the resources.

As i said elsewhere, endiannes is interesting for networking.

Maybe at the netdev conference next month, how to handle endiannes can be part of the presentation? I'm guessing, but is it part of the type system? So long as a type is marked as 'cpu endian', little endian, or big endian, the compiler will take care of adding the needed swaps?

Andrew

#26 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

Quoted 5 lines · Andrew Lunn

Maybe at the netdev conference next month, how to handle endiannes can be part of the presentation? I'm guessing, but is it part of the type system? So long as a type is marked as 'cpu endian', little endian, or big endian, the compiler will take care of adding the needed swaps?

Yeah, the type system can take care of ensuring that one does not forget which type of quantity one is handling.

For instance, in this out-of-tree commit [1] from Wedson a LE generic is added, which allows you to use e.g. LE<u32> in your structs:

    struct Example {
        a: LE<u32>,
        b: LE<u32>,
    }

    fn sum(e: &Example) -> u32 {
        // `value` extracts the value in cpu representation.
        e.a.value() + e.b.value()
    }

Relatedly, the primitive integer types also provide "raw" functionality for endianness, e.g. the to_{le,be} and to_{le,be,ne}_bytes methods [2][3], and there are third-party crates on this, e.g. [4].

[1] https://github.com/wedsonaf/linux/commit/ca4a93caff8b96a54a68fb052959801468bce01a
[2] https://doc.rust-lang.org/std/primitive.u32.html#method.to_le
[3] https://doc.rust-lang.org/std/primitive.u32.html#method.to_be_bytes
[4] https://docs.rs/byteorder/latest/byteorder/

Cheers,
Miguel

maintainer
#27 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

In reply to Boqun Feng

Quoting Boqun Feng (2023-10-20 11:33:10)

Quoted 63 lines · Andrew Lunn
Quoted 6 lines
Quoted 1 line

+``arm64`` Maintained Little Endian only.

This question is just out of curiosity, not the patchset itself.

What is missing to make big endian work?

FWIW, I tried the following:

arch/arm64/Kconfig +1 -1
index 8784284988e5..b697c2d7da68 100644
@@ -227,7 +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_RUST
        select HAVE_STACKPROTECTOR
        select HAVE_SYSCALL_TRACEPOINTS
        select HAVE_KPROBES
arch/arm64/Makefile +4 -0
index 4562a8173e90..4621f1e00e06 100644
@@ -41,7 +41,11 @@ KBUILD_CFLAGS        += -mgeneral-regs-only  \
 KBUILD_CFLAGS  += $(call cc-disable-warning, psabi)
 KBUILD_AFLAGS  += $(compat_vdso)
 
+ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
+KBUILD_RUSTFLAGS += --target aarch64_be-unknown-linux-gnu -C target-feature="-neon"
+else
 KBUILD_RUSTFLAGS += --target aarch64-unknown-none -C target-feature="-neon"
+endif
 
 KBUILD_CFLAGS  += $(call cc-option,-mabi=lp64)
 KBUILD_AFLAGS  += $(call cc-option,-mabi=lp64)

and ran the following kunit command (it will run a few tests in a qemu emulated VM):

        ./tools/testing/kunit/kunit.py run --make_options LLVM=1 --arch arm64 --kconfig_add CONFIG_RUST=y --kconfig_add CONFIG_CPU_BIG_ENDIAN=y

The kernel was built successfully, and all Rust related tests passed.

Of course this doesn't mean a lot, we still need people with deep Rust compiler knowledge to confirm whether the support is completed or not. But I think if people want to do experiments, the tool is there.

P.S. An unrelated topic: I found a few clk related tests (in drivers/clk/clk-gate_test.c IIUC) don't pass (mostly due to that the expected values don't handle big endian), a sample failure output:

[11:13:26] # clk_gate_test_enable: EXPECTATION FAILED at drivers/clk/clk-gate_test.c:169
[11:13:26] Expected enable_val == ctx->fake_reg, but
[11:13:26] enable_val == 32 (0x20)
[11:13:26] ctx->fake_reg == 536870912 (0x20000000)
[11:13:26] clk_unregister: unregistering prepared clock: test_gate [11:13:26] clk_unregister: unregistering prepared clock: test_parent [11:13:26] [FAILED] clk_gate_test_enable

Quoted 2 lines

(Cc clk folks)

Thanks for the report! We should treat it as an __le32 for now. I'll have to add tests for the big endian flag as well. Does this fix it?

---8<----

drivers/clk/clk-gate_test.c +15 -15
index e136aaad48bf..c96d93b19ddf 100644
@@ -131,7 +131,7 @@ struct clk_gate_test_context {
 	void __iomem *fake_mem;
 	struct clk_hw *hw;
 	struct clk_hw *parent;
-	u32 fake_reg; /* Keep at end, KASAN can detect out of bounds */
+	__le32 fake_reg; /* Keep at end, KASAN can detect out of bounds */
 };
 
 static struct clk_gate_test_context *clk_gate_test_alloc_ctx(struct kunit *test)
@@ -166,7 +166,7 @@ static void clk_gate_test_enable(struct kunit *test)
 
 	KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
 
-	KUNIT_EXPECT_EQ(test, enable_val, ctx->fake_reg);
+	KUNIT_EXPECT_EQ(test, enable_val, le32_to_cpu(ctx->fake_reg));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(hw));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_prepared(hw));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(parent));
@@ -183,10 +183,10 @@ static void clk_gate_test_disable(struct kunit *test)
 	u32 disable_val = 0;
 
 	KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
-	KUNIT_ASSERT_EQ(test, enable_val, ctx->fake_reg);
+	KUNIT_ASSERT_EQ(test, enable_val, le32_to_cpu(ctx->fake_reg));
 
 	clk_disable_unprepare(clk);
-	KUNIT_EXPECT_EQ(test, disable_val, ctx->fake_reg);
+	KUNIT_EXPECT_EQ(test, disable_val, le32_to_cpu(ctx->fake_reg));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(hw));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_prepared(hw));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(parent));
@@ -246,7 +246,7 @@ static void clk_gate_test_invert_enable(struct kunit *test)
 
 	KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
 
-	KUNIT_EXPECT_EQ(test, enable_val, ctx->fake_reg);
+	KUNIT_EXPECT_EQ(test, enable_val, le32_to_cpu(ctx->fake_reg));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(hw));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_prepared(hw));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(parent));
@@ -263,10 +263,10 @@ static void clk_gate_test_invert_disable(struct kunit *test)
 	u32 disable_val = BIT(15);
 
 	KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
-	KUNIT_ASSERT_EQ(test, enable_val, ctx->fake_reg);
+	KUNIT_ASSERT_EQ(test, enable_val, le32_to_cpu(ctx->fake_reg));
 
 	clk_disable_unprepare(clk);
-	KUNIT_EXPECT_EQ(test, disable_val, ctx->fake_reg);
+	KUNIT_EXPECT_EQ(test, disable_val, le32_to_cpu(ctx->fake_reg));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(hw));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_prepared(hw));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(parent));
@@ -290,7 +290,7 @@ static int clk_gate_test_invert_init(struct kunit *test)
 					    2000000);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
 
-	ctx->fake_reg = BIT(15); /* Default to off */
+	ctx->fake_reg = cpu_to_le32(BIT(15)); /* Default to off */
 	hw = clk_hw_register_gate_parent_hw(NULL, "test_gate", parent, 0,
 					    ctx->fake_mem, 15,
 					    CLK_GATE_SET_TO_DISABLE, NULL);
@@ -319,7 +319,7 @@ static void clk_gate_test_hiword_enable(struct kunit *test)
 
 	KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
 
-	KUNIT_EXPECT_EQ(test, enable_val, ctx->fake_reg);
+	KUNIT_EXPECT_EQ(test, enable_val, le32_to_cpu(ctx->fake_reg));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(hw));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_prepared(hw));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(parent));
@@ -336,10 +336,10 @@ static void clk_gate_test_hiword_disable(struct kunit *test)
 	u32 disable_val = BIT(9 + 16);
 
 	KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
-	KUNIT_ASSERT_EQ(test, enable_val, ctx->fake_reg);
+	KUNIT_ASSERT_EQ(test, enable_val, le32_to_cpu(ctx->fake_reg));
 
 	clk_disable_unprepare(clk);
-	KUNIT_EXPECT_EQ(test, disable_val, ctx->fake_reg);
+	KUNIT_EXPECT_EQ(test, disable_val, le32_to_cpu(ctx->fake_reg));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(hw));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_prepared(hw));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(parent));
@@ -387,7 +387,7 @@ static void clk_gate_test_is_enabled(struct kunit *test)
 	struct clk_gate_test_context *ctx;
 
 	ctx = clk_gate_test_alloc_ctx(test);
-	ctx->fake_reg = BIT(7);
+	ctx->fake_reg = cpu_to_le32(BIT(7));
 	hw = clk_hw_register_gate(NULL, "test_gate", NULL, 0, ctx->fake_mem, 7,
 				  0, NULL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
@@ -402,7 +402,7 @@ static void clk_gate_test_is_disabled(struct kunit *test)
 	struct clk_gate_test_context *ctx;
 
 	ctx = clk_gate_test_alloc_ctx(test);
-	ctx->fake_reg = BIT(4);
+	ctx->fake_reg = cpu_to_le32(BIT(4));
 	hw = clk_hw_register_gate(NULL, "test_gate", NULL, 0, ctx->fake_mem, 7,
 				  0, NULL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
@@ -417,7 +417,7 @@ static void clk_gate_test_is_enabled_inverted(struct kunit *test)
 	struct clk_gate_test_context *ctx;
 
 	ctx = clk_gate_test_alloc_ctx(test);
-	ctx->fake_reg = BIT(31);
+	ctx->fake_reg = cpu_to_le32(BIT(31));
 	hw = clk_hw_register_gate(NULL, "test_gate", NULL, 0, ctx->fake_mem, 2,
 				  CLK_GATE_SET_TO_DISABLE, NULL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
@@ -432,7 +432,7 @@ static void clk_gate_test_is_disabled_inverted(struct kunit *test)
 	struct clk_gate_test_context *ctx;
 
 	ctx = clk_gate_test_alloc_ctx(test);
-	ctx->fake_reg = BIT(29);
+	ctx->fake_reg = cpu_to_le32(BIT(29));
 	hw = clk_hw_register_gate(NULL, "test_gate", NULL, 0, ctx->fake_mem, 29,
 				  CLK_GATE_SET_TO_DISABLE, NULL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
Tested-by
#28 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

Quoted 64 lines · Stephen Boyd

Quoting Boqun Feng (2023-10-20 11:33:10)

Quoted 63 lines · Andrew Lunn
Quoted 6 lines
Quoted 1 line

+``arm64`` Maintained Little Endian only.

This question is just out of curiosity, not the patchset itself.

What is missing to make big endian work?

FWIW, I tried the following:

arch/arm64/Kconfig +1 -1
index 8784284988e5..b697c2d7da68 100644
@@ -227,7 +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_RUST
        select HAVE_STACKPROTECTOR
        select HAVE_SYSCALL_TRACEPOINTS
        select HAVE_KPROBES
arch/arm64/Makefile +4 -0
index 4562a8173e90..4621f1e00e06 100644
@@ -41,7 +41,11 @@ KBUILD_CFLAGS        += -mgeneral-regs-only  \
 KBUILD_CFLAGS  += $(call cc-disable-warning, psabi)
 KBUILD_AFLAGS  += $(compat_vdso)
 
+ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
+KBUILD_RUSTFLAGS += --target aarch64_be-unknown-linux-gnu -C target-feature="-neon"
+else
 KBUILD_RUSTFLAGS += --target aarch64-unknown-none -C target-feature="-neon"
+endif
 
 KBUILD_CFLAGS  += $(call cc-option,-mabi=lp64)
 KBUILD_AFLAGS  += $(call cc-option,-mabi=lp64)

and ran the following kunit command (it will run a few tests in a qemu emulated VM):

        ./tools/testing/kunit/kunit.py run --make_options LLVM=1 --arch arm64 --kconfig_add CONFIG_RUST=y --kconfig_add CONFIG_CPU_BIG_ENDIAN=y

The kernel was built successfully, and all Rust related tests passed.

Of course this doesn't mean a lot, we still need people with deep Rust compiler knowledge to confirm whether the support is completed or not. But I think if people want to do experiments, the tool is there.

P.S. An unrelated topic: I found a few clk related tests (in drivers/clk/clk-gate_test.c IIUC) don't pass (mostly due to that the expected values don't handle big endian), a sample failure output:

[11:13:26] # clk_gate_test_enable: EXPECTATION FAILED at drivers/clk/clk-gate_test.c:169
[11:13:26] Expected enable_val == ctx->fake_reg, but
[11:13:26] enable_val == 32 (0x20)
[11:13:26] ctx->fake_reg == 536870912 (0x20000000)
[11:13:26] clk_unregister: unregistering prepared clock: test_gate [11:13:26] clk_unregister: unregistering prepared clock: test_parent [11:13:26] [FAILED] clk_gate_test_enable

Quoted 5 lines
Quoted 2 lines

(Cc clk folks)

Thanks for the report! We should treat it as an __le32 for now. I'll have to add tests for the big endian flag as well. Does this fix it?

Yep! I just tested with kunit, and all the tests pass.

Feel free to add:

Regards,
Boqun

Quoted 127 lines

---8<----

drivers/clk/clk-gate_test.c +15 -15
index e136aaad48bf..c96d93b19ddf 100644
@@ -131,7 +131,7 @@ struct clk_gate_test_context {
 	void __iomem *fake_mem;
 	struct clk_hw *hw;
 	struct clk_hw *parent;
-	u32 fake_reg; /* Keep at end, KASAN can detect out of bounds */
+	__le32 fake_reg; /* Keep at end, KASAN can detect out of bounds */
 };
 
 static struct clk_gate_test_context *clk_gate_test_alloc_ctx(struct kunit *test)
@@ -166,7 +166,7 @@ static void clk_gate_test_enable(struct kunit *test)
 
 	KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
 
-	KUNIT_EXPECT_EQ(test, enable_val, ctx->fake_reg);
+	KUNIT_EXPECT_EQ(test, enable_val, le32_to_cpu(ctx->fake_reg));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(hw));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_prepared(hw));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(parent));
@@ -183,10 +183,10 @@ static void clk_gate_test_disable(struct kunit *test)
 	u32 disable_val = 0;
 
 	KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
-	KUNIT_ASSERT_EQ(test, enable_val, ctx->fake_reg);
+	KUNIT_ASSERT_EQ(test, enable_val, le32_to_cpu(ctx->fake_reg));
 
 	clk_disable_unprepare(clk);
-	KUNIT_EXPECT_EQ(test, disable_val, ctx->fake_reg);
+	KUNIT_EXPECT_EQ(test, disable_val, le32_to_cpu(ctx->fake_reg));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(hw));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_prepared(hw));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(parent));
@@ -246,7 +246,7 @@ static void clk_gate_test_invert_enable(struct kunit *test)
 
 	KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
 
-	KUNIT_EXPECT_EQ(test, enable_val, ctx->fake_reg);
+	KUNIT_EXPECT_EQ(test, enable_val, le32_to_cpu(ctx->fake_reg));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(hw));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_prepared(hw));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(parent));
@@ -263,10 +263,10 @@ static void clk_gate_test_invert_disable(struct kunit *test)
 	u32 disable_val = BIT(15);
 
 	KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
-	KUNIT_ASSERT_EQ(test, enable_val, ctx->fake_reg);
+	KUNIT_ASSERT_EQ(test, enable_val, le32_to_cpu(ctx->fake_reg));
 
 	clk_disable_unprepare(clk);
-	KUNIT_EXPECT_EQ(test, disable_val, ctx->fake_reg);
+	KUNIT_EXPECT_EQ(test, disable_val, le32_to_cpu(ctx->fake_reg));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(hw));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_prepared(hw));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(parent));
@@ -290,7 +290,7 @@ static int clk_gate_test_invert_init(struct kunit *test)
 					    2000000);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
 
-	ctx->fake_reg = BIT(15); /* Default to off */
+	ctx->fake_reg = cpu_to_le32(BIT(15)); /* Default to off */
 	hw = clk_hw_register_gate_parent_hw(NULL, "test_gate", parent, 0,
 					    ctx->fake_mem, 15,
 					    CLK_GATE_SET_TO_DISABLE, NULL);
@@ -319,7 +319,7 @@ static void clk_gate_test_hiword_enable(struct kunit *test)
 
 	KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
 
-	KUNIT_EXPECT_EQ(test, enable_val, ctx->fake_reg);
+	KUNIT_EXPECT_EQ(test, enable_val, le32_to_cpu(ctx->fake_reg));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(hw));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_prepared(hw));
 	KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(parent));
@@ -336,10 +336,10 @@ static void clk_gate_test_hiword_disable(struct kunit *test)
 	u32 disable_val = BIT(9 + 16);
 
 	KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
-	KUNIT_ASSERT_EQ(test, enable_val, ctx->fake_reg);
+	KUNIT_ASSERT_EQ(test, enable_val, le32_to_cpu(ctx->fake_reg));
 
 	clk_disable_unprepare(clk);
-	KUNIT_EXPECT_EQ(test, disable_val, ctx->fake_reg);
+	KUNIT_EXPECT_EQ(test, disable_val, le32_to_cpu(ctx->fake_reg));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(hw));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_prepared(hw));
 	KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(parent));
@@ -387,7 +387,7 @@ static void clk_gate_test_is_enabled(struct kunit *test)
 	struct clk_gate_test_context *ctx;
 
 	ctx = clk_gate_test_alloc_ctx(test);
-	ctx->fake_reg = BIT(7);
+	ctx->fake_reg = cpu_to_le32(BIT(7));
 	hw = clk_hw_register_gate(NULL, "test_gate", NULL, 0, ctx->fake_mem, 7,
 				  0, NULL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
@@ -402,7 +402,7 @@ static void clk_gate_test_is_disabled(struct kunit *test)
 	struct clk_gate_test_context *ctx;
 
 	ctx = clk_gate_test_alloc_ctx(test);
-	ctx->fake_reg = BIT(4);
+	ctx->fake_reg = cpu_to_le32(BIT(4));
 	hw = clk_hw_register_gate(NULL, "test_gate", NULL, 0, ctx->fake_mem, 7,
 				  0, NULL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
@@ -417,7 +417,7 @@ static void clk_gate_test_is_enabled_inverted(struct kunit *test)
 	struct clk_gate_test_context *ctx;
 
 	ctx = clk_gate_test_alloc_ctx(test);
-	ctx->fake_reg = BIT(31);
+	ctx->fake_reg = cpu_to_le32(BIT(31));
 	hw = clk_hw_register_gate(NULL, "test_gate", NULL, 0, ctx->fake_mem, 2,
 				  CLK_GATE_SET_TO_DISABLE, NULL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
@@ -432,7 +432,7 @@ static void clk_gate_test_is_disabled_inverted(struct kunit *test)
 	struct clk_gate_test_context *ctx;
 
 	ctx = clk_gate_test_alloc_ctx(test);
-	ctx->fake_reg = BIT(29);
+	ctx->fake_reg = cpu_to_le32(BIT(29));
 	hw = clk_hw_register_gate(NULL, "test_gate", NULL, 0, ctx->fake_mem, 29,
 				  CLK_GATE_SET_TO_DISABLE, NULL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
#29 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

In reply to Boqun Feng

[Andrew wrote]

Quoted 4 lines
Quoted 4 lines

Network developers have expressed an interesting in testing Rust code on big endian systems, since the code should work on both endians. It might be easier to get an ARM board running big endian than get access to an S390x machine.

If you mean ARM32 BE I have that on the IXP4xx systems up and running mainline kernels and mainline OpenWrt now, but I haven't seen rust enablement for ARM32 yet (but it can't be that hard).

Yours,
Linus Walleij

#30 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

In reply to Jamie Cunliffe
Quoted 1 line · Jamie Cunliffe

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

If I have aarch64-unknown-none target support already installed in my local Rust compiler (either build a custom compiler and enable the target, as is my real case, or as the common case, run rustup target add aarch64-unknown-none), this produces a build error I didn't get from the previous target.json approach:

error[E0464]: multiple candidates for rmeta dependency core found
|
= note: candidate #1:
/usr/local/google/home/mmaurer/android/linux/out/cache/65c6e8ea/common/rust/libcore.rmeta
= note: candidate #2:
/usr/local/google/home/mmaurer/android/linux/prebuilts/rust/linux-x86/1.72.1/lib/rustlib/aarch64-unknown-none/lib/libcore-690113bb4de6f7fb.rlib

Are we sure that we want to try to use --target=aarch64-unknown-none rather than a custom target.json? If our libcore and their libcore may be compiled differently (and they can be, because we are controlling additional compilation options), it seems to me that we ought to be defining our own target.

If we really do want to go ahead with using targets directly form rustc, this patch should add --sysroot=/dev/null or a similar override to rustc_library_cmd and rust_common_cmd to prevent rustc from picking up any already installed sysroot libraries.

#31 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

Quoted 15 lines · Matthew Maurer
Quoted 1 line · Jamie Cunliffe

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

If I have aarch64-unknown-none target support already installed in my local Rust compiler (either build a custom compiler and enable the target, as is my real case, or as the common case, run rustup target add aarch64-unknown-none), this produces a build error I didn't get from the previous target.json approach:

error[E0464]: multiple candidates for rmeta dependency core found
|
= note: candidate #1:
/usr/local/google/home/mmaurer/android/linux/out/cache/65c6e8ea/common/rust/libcore.rmeta
= note: candidate #2:
/usr/local/google/home/mmaurer/android/linux/prebuilts/rust/linux-x86/1.72.1/lib/rustlib/aarch64-unknown-none/lib/libcore-690113bb4de6f7fb.rlib

Quoted 6 lines

Are we sure that we want to try to use --target=aarch64-unknown-none rather than a custom target.json? If our libcore and their libcore may be compiled differently (and they can be, because we are controlling additional compilation options), it seems to me that we ought to be defining our own target.

FWIW, the discussion around this:

	https://lore.kernel.org/rust-for-linux/20230126163552.GD29438@willie-the-truck/	
Quoted 5 lines

If we really do want to go ahead with using targets directly form rustc, this patch should add --sysroot=/dev/null or a similar override to rustc_library_cmd and rust_common_cmd to prevent rustc from picking up any already installed sysroot libraries.

Agreed.

Regards,
Boqun

#32 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

In reply to Matthew Maurer
Quoted 5 lines · Matthew Maurer

If we really do want to go ahead with using targets directly form rustc, this patch should add --sysroot=/dev/null or a similar override to rustc_library_cmd and rust_common_cmd to prevent rustc from picking up any already installed sysroot libraries.

For reference, Matthew sent the patch to do this at
https://lore.kernel.org/rust-for-linux/[email protected]/
and I have picked it up in rust-next (with a couple extra changes noted in that thread), so this should be OK.

When this series also arrives in -next I will be testing the combination further just in case.

Cheers,
Miguel

Tested-by on v4 2/2
#34 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

In reply to Jamie Cunliffe
Quoted 9 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.

I'm using it since some weeks cross compiling from x86. Boots executing the sample code and the KUnit doctests. So:

Would be nice if we could get this merged :)

Dirk

Quoted 82 lines
---
  Documentation/rust/arch-support.rst | 1 +
  arch/arm64/Kconfig                  | 1 +
  arch/arm64/Makefile                 | 4 ++++
  rust/Makefile                       | 1 +
  scripts/generate_rust_target.rs     | 4 +++-
  5 files changed, 10 insertions(+), 1 deletion(-)
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.
  ============  ================  ==============================================
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..52d75c9483a9 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 -Ctarget-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 += -Zbranch-protection=bti,pac-ret
  else ifeq ($(CONFIG_ARM64_PTR_AUTH_KERNEL),y)
+  KBUILD_RUSTFLAGS += -Zbranch-protection=pac-ret
    ifeq ($(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET),y)
      KBUILD_CFLAGS += -mbranch-protection=pac-ret
    else
rust/Makefile +1 -0
index 8d75a6ffe951..8ced65c9b905 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,
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",
reviewer Tested-by on v4 2/2
#35 lore raw

Re: [PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

In reply to Jamie Cunliffe
Quoted 9 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.

Tested with Rust Binder.

maintainer Applied
#36 lore raw
In reply to Jamie Cunliffe
Quoted 8 lines · Jamie Cunliffe

Enable Rust support for the AArch64 architecture.

Since the v3 this has been refactored to split up the x86 Makefile changes. Updated the x86-64 conditionals as suggested by Boqun and addressed the formatting issues Miguel raised.

[...]

Applied to arm64 (for-next/rust-for-arm64), thanks!

I also folded Rui's loongarch fix into the first patch to avoid breaking this arch:

https://github.com/heiher/linux/commit/c504d83a6edd963e2ba58ec538e3f06da78b187e.patch

[1/2] rust: Refactor the build target to allow the use of builtin targets

      https://git.kernel.org/arm64/c/f82811e22b48
[2/2] arm64: rust: Enable Rust support for AArch64
      https://git.kernel.org/arm64/c/724a75ac9542
Signature · 1 line
Catalin