* [PATCH v3 1/1] RISC-V: enable building 64-bit kernels with rust support
2024-04-09 17:25 [PATCH v3 0/1] RISC-V: enable rust Conor Dooley
@ 2024-04-09 17:25 ` Conor Dooley
2024-04-17 14:00 ` Gary Guo
2024-04-10 13:22 ` [PATCH v3 0/1] RISC-V: enable rust Björn Töpel
2024-04-10 14:20 ` patchwork-bot+linux-riscv
2 siblings, 1 reply; 5+ messages in thread
From: Conor Dooley @ 2024-04-09 17:25 UTC (permalink / raw)
To: linux-riscv
Cc: conor, Conor Dooley, Miguel Ojeda, Alex Gaynor,
Wedson Almeida Filho, Boqun Feng, Gary Guo, Björn Roy Baron,
Jonathan Corbet, Paul Walmsley, Palmer Dabbelt, Nathan Chancellor,
Nick Desaulniers, rust-for-linux, linux-doc, linux-kernel, llvm
From: Miguel Ojeda <ojeda@kernel.org>
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.
Co-developed-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Co-developed-by: Conor Dooley <conor.dooley@microchip.com>
Signed-off-by: Conor Dooley <conor.dooley@microchip.com>
---
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(+)
diff --git a/Documentation/rust/arch-support.rst b/Documentation/rust/arch-support.rst
index 5c4fa9f5d1cd..4d1495ded2aa 100644
--- a/Documentation/rust/arch-support.rst
+++ b/Documentation/rust/arch-support.rst
@@ -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.
============= ================ ==============================================
diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index be09c8836d56..cad31864fd0f 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -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
diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile
index 252d63942f34..adbc9023d7f0 100644
--- a/arch/riscv/Makefile
+++ b/arch/riscv/Makefile
@@ -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
diff --git a/scripts/generate_rust_target.rs b/scripts/generate_rust_target.rs
index 54919cf48621..8f7846b9029a 100644
--- a/scripts/generate_rust_target.rs
+++ b/scripts/generate_rust_target.rs
@@ -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(
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v3 1/1] RISC-V: enable building 64-bit kernels with rust support
2024-04-09 17:25 ` [PATCH v3 1/1] RISC-V: enable building 64-bit kernels with rust support Conor Dooley
@ 2024-04-17 14:00 ` Gary Guo
0 siblings, 0 replies; 5+ messages in thread
From: Gary Guo @ 2024-04-17 14:00 UTC (permalink / raw)
To: Conor Dooley
Cc: linux-riscv, Conor Dooley, Miguel Ojeda, Alex Gaynor,
Wedson Almeida Filho, Boqun Feng, Björn Roy Baron,
Jonathan Corbet, Paul Walmsley, Palmer Dabbelt, Nathan Chancellor,
Nick Desaulniers, rust-for-linux, linux-doc, linux-kernel, llvm
On Tue, 9 Apr 2024 18:25:16 +0100
Conor Dooley <conor@kernel.org> wrote:
> From: Miguel Ojeda <ojeda@kernel.org>
>
> 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!
> 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).
>
> As 64-bit RISC-V is now supported, add it to the arch support table.
>
> Co-developed-by: Gary Guo <gary@garyguo.net>
> Signed-off-by: Gary Guo <gary@garyguo.net>
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
> Co-developed-by: Conor Dooley <conor.dooley@microchip.com>
> Signed-off-by: Conor Dooley <conor.dooley@microchip.com>
> ---
> 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(+)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v3 0/1] RISC-V: enable rust
2024-04-09 17:25 [PATCH v3 0/1] RISC-V: enable rust Conor Dooley
2024-04-09 17:25 ` [PATCH v3 1/1] RISC-V: enable building 64-bit kernels with rust support Conor Dooley
@ 2024-04-10 13:22 ` Björn Töpel
2024-04-10 14:20 ` patchwork-bot+linux-riscv
2 siblings, 0 replies; 5+ messages in thread
From: Björn Töpel @ 2024-04-10 13:22 UTC (permalink / raw)
To: Conor Dooley, linux-riscv
Cc: conor, Conor Dooley, Miguel Ojeda, Alex Gaynor,
Wedson Almeida Filho, Boqun Feng, Gary Guo, Björn Roy Baron,
Jonathan Corbet, Paul Walmsley, Palmer Dabbelt, Nathan Chancellor,
Nick Desaulniers, rust-for-linux, linux-doc, linux-kernel, llvm
Conor Dooley <conor@kernel.org> writes:
> From: Conor Dooley <conor.dooley@microchip.com>
>
> 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.
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/
Tested-by: Björn Töpel <bjorn@rivosinc.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v3 0/1] RISC-V: enable rust
2024-04-09 17:25 [PATCH v3 0/1] RISC-V: enable rust Conor Dooley
2024-04-09 17:25 ` [PATCH v3 1/1] RISC-V: enable building 64-bit kernels with rust support Conor Dooley
2024-04-10 13:22 ` [PATCH v3 0/1] RISC-V: enable rust Björn Töpel
@ 2024-04-10 14:20 ` patchwork-bot+linux-riscv
2 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+linux-riscv @ 2024-04-10 14:20 UTC (permalink / raw)
To: Conor Dooley
Cc: linux-riscv, conor.dooley, ojeda, alex.gaynor, wedsonaf,
boqun.feng, gary, bjorn3_gh, corbet, paul.walmsley, palmer,
nathan, ndesaulniers, rust-for-linux, linux-doc, linux-kernel,
llvm
Hello:
This patch was applied to riscv/linux.git (for-next)
by Palmer Dabbelt <palmer@rivosinc.com>:
On Tue, 9 Apr 2024 18:25:15 +0100 you wrote:
> From: Conor Dooley <conor.dooley@microchip.com>
>
> 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
>
> [...]
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!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 5+ messages in thread