Skip to contentSkip to conversation

RISC-V: enable rust

PATCH Applied Conor Dooley 4 participants 5 messages Apr 9, 2024 – Apr 17, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover RISC-V: enable rust

From: Conor Dooley <[email protected]>

I've intentionally not turned on the gcc support, as discussed on v1. I've also switched over to using the target, but it is a bit heavier than the one arm64 seems to be using. RISC-V has fewer targets available and this was the closest. I preserved the redzone disabling, just moved into the Makefile. Any comment from Gary or the LLVM lads on the target would be great I think:
https://github.com/rust-lang/rust/blob/master/compiler/rustc_target/src/spec/targets/riscv64imac_unknown_none_elf.rs
arm64 is using:
https://github.com/rust-lang/rust/blob/master/compiler/rustc_target/src/spec/targets/aarch64_unknown_none.rs

I was gonna send this yesterday, but found out last minute I had invalid code in the target generation script. The kernel test robot had given my branch the global all-clear - the rust coverage with all the "depends on !FOO" must really limit the build coverage. I built for x86 with rust enabled locally this time to make sure..

As this as lifted from the state of the Rust-for-Linux tree, the commit messages from there cannot be preserved, so these patches have commit messages that I wrote.

I've tested this on Icicle, and the modules seem to work as expected. Unfortunately there appear to be implicit 32-bit divisions (or similar) in core Rust code, so, as in the downstream Rust-for-Linux tree, Rust is only enabled for 64-bit.

Thanks,
Conor.

Changes in v3:
- Use a builtin target and modify it in kbuild
- Drop the kCFI patch, that's been merged independently

Changes in v2:
- Rebase, since a good bit of time has passed!
- Add the extra patch, disabling when CFI_CLANG is enabled.

Changes in v1:
- rebase on v6.3-rc1
- resort the um entry in the arch-support table while adding RISC-V to it
- drop 32-bit bits
- have another crack at assigning authorship

Changes in RFC-RESEND:
- fix the asymmetrical additions in the Makefile bits
- add cc-cover to my git send-email command...

CC Miguel Ojeda <[email protected]>
CC Alex Gaynor <[email protected]>
CC Wedson Almeida Filho <[email protected]>
CC Boqun Feng <[email protected]>
CC Gary Guo <[email protected]>
CC Björn Roy Baron <[email protected]>
CC Jonathan Corbet <[email protected]>
CC Paul Walmsley <[email protected]>
CC Palmer Dabbelt <[email protected]>
CC Nathan Chancellor <[email protected]>
CC Nick Desaulniers <[email protected]>

Miguel Ojeda (1):
RISC-V: enable building 64-bit kernels with rust support

 Documentation/rust/arch-support.rst | 1 +
 arch/riscv/Kconfig                  | 1 +
 arch/riscv/Makefile                 | 7 +++++++
 scripts/generate_rust_target.rs     | 6 ++++++
 4 files changed, 15 insertions(+)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/1 RISC-V: enable building 64-bit kernels with rust support

From: Miguel Ojeda <[email protected]>

The rust modules work on 64-bit RISC-V, with no twiddling required. Select HAVE_RUST and provide the required flags to kbuild so that the modules can be used. The Makefile and Kconfig changes are lifted from work done by Miguel in the Rust-for-Linux tree, hence his authorship. Following the rabbit hole, the Makefile changes originated in a script, created based on config files originally added by Gary, hence his co-authorship.

32-bit is broken in core rust code, so support is limited to 64-bit:
ld.lld: error: undefined symbol: __udivdi3

As 64-bit RISC-V is now supported, add it to the arch support table.

---
 Documentation/rust/arch-support.rst | 1 +
 arch/riscv/Kconfig                  | 1 +
 arch/riscv/Makefile                 | 7 +++++++
 scripts/generate_rust_target.rs     | 6 ++++++
 4 files changed, 15 insertions(+)
Documentation/rust/arch-support.rst +1 -0
index 5c4fa9f5d1cd..4d1495ded2aa 100644
@@ -17,6 +17,7 @@ Architecture   Level of support  Constraints
 =============  ================  ==============================================
 ``arm64``      Maintained        Little Endian only.
 ``loongarch``  Maintained        -
+``riscv``      Maintained        ``riscv64`` only.
 ``um``         Maintained        ``x86_64`` only.
 ``x86``        Maintained        ``x86_64`` only.
 =============  ================  ==============================================
arch/riscv/Kconfig +1 -0
index be09c8836d56..cad31864fd0f 100644
@@ -155,6 +155,7 @@ config RISCV
 	select HAVE_REGS_AND_STACK_ACCESS_API
 	select HAVE_RETHOOK if !XIP_KERNEL
 	select HAVE_RSEQ
+	select HAVE_RUST if 64BIT
 	select HAVE_SAMPLE_FTRACE_DIRECT
 	select HAVE_SAMPLE_FTRACE_DIRECT_MULTI
 	select HAVE_STACKPROTECTOR
arch/riscv/Makefile +7 -0
index 252d63942f34..adbc9023d7f0 100644
@@ -34,6 +34,9 @@ ifeq ($(CONFIG_ARCH_RV64I),y)
 	KBUILD_AFLAGS += -mabi=lp64
 
 	KBUILD_LDFLAGS += -melf64lriscv
+
+	KBUILD_RUSTFLAGS += -Ctarget-cpu=generic-rv64 --target=riscv64imac-unknown-none-elf \
+			    -Cno-redzone
 else
 	BITS := 32
 	UTS_MACHINE := riscv32
@@ -68,6 +71,10 @@ riscv-march-$(CONFIG_FPU)		:= $(riscv-march-y)fd
 riscv-march-$(CONFIG_RISCV_ISA_C)	:= $(riscv-march-y)c
 riscv-march-$(CONFIG_RISCV_ISA_V)	:= $(riscv-march-y)v
 
+ifneq ($(CONFIG_RISCV_ISA_C),y)
+	KBUILD_RUSTFLAGS += -Ctarget-feature=-c
+endif
+
 ifdef CONFIG_TOOLCHAIN_NEEDS_OLD_ISA_SPEC
 KBUILD_CFLAGS += -Wa,-misa-spec=2.2
 KBUILD_AFLAGS += -Wa,-misa-spec=2.2
scripts/generate_rust_target.rs +6 -0
index 54919cf48621..8f7846b9029a 100644
@@ -150,6 +150,12 @@ fn main() {
     // `llvm-target`s are taken from `scripts/Makefile.clang`.
     if cfg.has("ARM64") {
         panic!("arm64 uses the builtin rustc aarch64-unknown-none target");
+    } else if cfg.has("RISCV") {
+        if cfg.has("64BIT") {
+            panic!("64-bit RISC-V uses the builtin rustc riscv64-unknown-none-elf target");
+        } else {
+            panic!("32-bit RISC-V is an unsupported architecture");
+        }
     } else if cfg.has("X86_64") {
         ts.push("arch", "x86_64");
         ts.push(
Signature · 1 line
2.43.0
reviewer
#2 lore raw

Re: [PATCH v3 1/1] RISC-V: enable building 64-bit kernels with rust support

Quoted 9 lines · On Tue, 9 Apr 2024 18:25:16 +0100 Conor Dooley <[email protected]> wrote:

From: Miguel Ojeda <[email protected]>

The rust modules work on 64-bit RISC-V, with no twiddling required. Select HAVE_RUST and provide the required flags to kbuild so that the modules can be used. The Makefile and Kconfig changes are lifted from work done by Miguel in the Rust-for-Linux tree, hence his authorship. Following the rabbit hole, the Makefile changes originated in a script, created based on config files originally added by Gary, hence his co-authorship.

Thanks for sending the patch!

Quoted 2 lines

32-bit is broken in core rust code, so support is limited to 64-bit:
ld.lld: error: undefined symbol: __udivdi3

I wouldn't call the core rust code broken :)

The missing symbol here should be easily stubbed by changing the redirect-intrinsics in rust/Makefile and adding a stub in compiler_builtins.rs. Although we probably instead of panicking, want to redirect the division to kernel division routine (IIRC the division is used for formatting integers, which is a legit use of division).

Quoted 14 lines

As 64-bit RISC-V is now supported, add it to the arch support table.

---
 Documentation/rust/arch-support.rst | 1 +
 arch/riscv/Kconfig                  | 1 +
 arch/riscv/Makefile                 | 7 +++++++
 scripts/generate_rust_target.rs     | 6 ++++++
 4 files changed, 15 insertions(+)
Tested-by on the v3 series
#3 lore raw
In reply to Conor Dooley
Quoted 18 lines · Conor Dooley

From: Conor Dooley <[email protected]>

I've intentionally not turned on the gcc support, as discussed on v1. I've also switched over to using the target, but it is a bit heavier than the one arm64 seems to be using. RISC-V has fewer targets available and this was the closest. I preserved the redzone disabling, just moved into the Makefile. Any comment from Gary or the LLVM lads on the target would be great I think:
https://github.com/rust-lang/rust/blob/master/compiler/rustc_target/src/spec/targets/riscv64imac_unknown_none_elf.rs
arm64 is using:
https://github.com/rust-lang/rust/blob/master/compiler/rustc_target/src/spec/targets/aarch64_unknown_none.rs

I was gonna send this yesterday, but found out last minute I had invalid code in the target generation script. The kernel test robot had given my branch the global all-clear - the rust coverage with all the "depends on !FOO" must really limit the build coverage. I built for x86 with rust enabled locally this time to make sure..

Quoted 8 lines

As this as lifted from the state of the Rust-for-Linux tree, the commit messages from there cannot be preserved, so these patches have commit messages that I wrote.

I've tested this on Icicle, and the modules seem to work as expected. Unfortunately there appear to be implicit 32-bit divisions (or similar) in core Rust code, so, as in the downstream Rust-for-Linux tree, Rust is only enabled for 64-bit.

Nice, works with my simple test on VisionFive 2 as well! Cool to have Rust support in! Now, BTF just needs to be supported, and I can have Rust *and* BPF in my kernels! :-P \o/

#4 lore raw
In reply to Conor Dooley
This patch was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Quoted 12 lines · On Tue, 9 Apr 2024 18:25:15 +0100 you wrote:

From: Conor Dooley <[email protected]>

I've intentionally not turned on the gcc support, as discussed on v1. I've also switched over to using the target, but it is a bit heavier than the one arm64 seems to be using. RISC-V has fewer targets available and this was the closest. I preserved the redzone disabling, just moved into the Makefile. Any comment from Gary or the LLVM lads on the target would be great I think:
https://github.com/rust-lang/rust/blob/master/compiler/rustc_target/src/spec/targets/riscv64imac_unknown_none_elf.rs
arm64 is using:
https://github.com/rust-lang/rust/blob/master/compiler/rustc_target/src/spec/targets/aarch64_unknown_none.rs

Quoted 1 line

[...]

Here is the summary with links:
- [v3,1/1] RISC-V: enable building 64-bit kernels with rust support

    https://git.kernel.org/riscv/c/81889e8523e6

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html