Skip to contentSkip to conversation

RISC-V: enable rust

RFC In review Conor Dooley 4 participants 7 messages Feb 24, 2023

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

cover RISC-V: enable rust

This is a somewhat blind (and maybe foolish) attempt at enabling Rust for RISC-V. I've tested this on Icicle, and the modules seem to work. I'd like to play around with Rust on RISC-V, but I'm not interested in using downstream kernels, so figured I should try and see what's missing...
I've tagged this as RFC in case I've missed some "WAaaaa you can't do this" somewhere :)

Thanks,
Conor.

Changes in RFC-RESEND:
- fixed the asymmetrical additions in the Makefile bits
- added 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]>
CC Tom Rix <[email protected]>

Miguel Ojeda (2):
scripts: generate_rust_target: enable building on RISC-V
RISC-V: enable building the 64-bit kernels with rust support

 Documentation/rust/arch-support.rst |  2 ++
 arch/riscv/Kconfig                  |  1 +
 arch/riscv/Makefile                 |  3 ++-
 scripts/generate_rust_target.rs     | 19 +++++++++++++++++++
 4 files changed, 24 insertions(+), 1 deletion(-)
Signature · 1 line
2.39.2
#1 lore raw

RFC 1/2 scripts: generate_rust_target: enable building on RISC-V

From: Miguel Ojeda <[email protected]>

Add the required bits from rust-for-linux to enable generating a RISC-V target for rust.

---
 scripts/generate_rust_target.rs | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/generate_rust_target.rs +19 -0
index 3c6cbe2b278d..72428fc66502 100644
@@ -161,6 +161,25 @@ fn main() {
         ts.push("features", features);
         ts.push("llvm-target", "x86_64-linux-gnu");
         ts.push("target-pointer-width", "64");
+    } else if cfg.has("RISCV") {
+        if cfg.has("64BIT") {
+            ts.push("arch", "riscv64");
+            ts.push("data-layout", "e-m:e-p:64:64-i64:64-i128:128-n64-S128");
+            ts.push("llvm-target", "riscv64-linux-gnu");
+            ts.push("target-pointer-width", "64");
+        } else {
+            ts.push("arch", "riscv32");
+            ts.push("data-layout", "e-m:e-p:32:32-i64:64-n32-S128");
+            ts.push("llvm-target", "riscv32-linux-gnu");
+            ts.push("target-pointer-width", "32");
+        }
+        ts.push("code-model", "medium");
+        ts.push("disable-redzone", true);
+        let mut features = "+m,+a".to_string();
+        if cfg.has("RISCV_ISA_C") {
+            features += ",+c";
+        }
+        ts.push("features", features);
     } else {
         panic!("Unsupported architecture");
     }
Signature · 1 line
2.39.2
#2 lore raw

RFC 2/2 RISC-V: enable building the 64-bit kernels with rust support

In reply to Conor Dooley

From: Miguel Ojeda <[email protected]>

The rust modules work on 64-bit RISC-V, with no twiddling required. Select HAS_RUST and provide the required flags to kbuild so that the modules can be used.
32-bit is broken in core rust code, so support is limited to 64-bit
only: ld.lld: error: undefined symbol: __udivdi3

---
 Documentation/rust/arch-support.rst | 2 ++
 arch/riscv/Kconfig                  | 1 +
 arch/riscv/Makefile                 | 3 ++-
 3 files changed, 5 insertions(+), 1 deletion(-)
Documentation/rust/arch-support.rst +2 -0
index 6982b63775da..197919158596 100644
@@ -15,5 +15,7 @@ support corresponds to ``S`` values in the ``MAINTAINERS`` file.
 ============  ================  ==============================================
 Architecture  Level of support  Constraints
 ============  ================  ==============================================
+``riscv``     Maintained        ``rv64`` only.
+============  ================  ==============================================
 ``x86``       Maintained        ``x86_64`` only.
 ============  ================  ==============================================
arch/riscv/Kconfig +1 -0
index 81eb031887d2..73174157212d 100644
@@ -115,6 +115,7 @@ config RISCV
 	select HAVE_POSIX_CPU_TIMERS_TASK_WORK
 	select HAVE_REGS_AND_STACK_ACCESS_API
 	select HAVE_RSEQ
+	select HAVE_RUST if 64BIT
 	select HAVE_STACKPROTECTOR
 	select HAVE_SYSCALL_TRACEPOINTS
 	select IRQ_DOMAIN
arch/riscv/Makefile +2 -1
index 76989561566b..0d6fc4e25221 100644
@@ -29,8 +29,8 @@ ifeq ($(CONFIG_ARCH_RV64I),y)
 
 	KBUILD_CFLAGS += -mabi=lp64
 	KBUILD_AFLAGS += -mabi=lp64
-
 	KBUILD_LDFLAGS += -melf64lriscv
+	KBUILD_RUSTFLAGS += -Ctarget-cpu=generic-rv64
 else
 	BITS := 32
 	UTS_MACHINE := riscv32
@@ -38,6 +38,7 @@ else
 	KBUILD_CFLAGS += -mabi=ilp32
 	KBUILD_AFLAGS += -mabi=ilp32
 	KBUILD_LDFLAGS += -melf32lriscv
+	KBUILD_RUSTFLAGS += -Ctarget-cpu=generic-rv32
 endif
 
 ifeq ($(CONFIG_LD_IS_LLD),y)
Signature · 1 line
2.39.2
#3 lore raw

Re: [RFC RESEND 2/2] RISC-V: enable building the 64-bit kernels with rust support

Hi Conor,

Quoted 33 lines · Conor Dooley

From: Miguel Ojeda <[email protected]>

The rust modules work on 64-bit RISC-V, with no twiddling required. Select HAS_RUST and provide the required flags to kbuild so that the modules can be used.
32-bit is broken in core rust code, so support is limited to 64-bit
only: ld.lld: error: undefined symbol: __udivdi3

---
 Documentation/rust/arch-support.rst | 2 ++
 arch/riscv/Kconfig                  | 1 +
 arch/riscv/Makefile                 | 3 ++-
 3 files changed, 5 insertions(+), 1 deletion(-)
Documentation/rust/arch-support.rst +2 -0
index 6982b63775da..197919158596 100644
@@ -15,5 +15,7 @@ support corresponds to ``S`` values in the ``MAINTAINERS`` file.
 ============  ================  ==============================================
 Architecture  Level of support  Constraints
 ============  ================  ==============================================
+``riscv``     Maintained        ``rv64`` only.
+============  ================  ==============================================
 ``x86``       Maintained        ``x86_64`` only.
 ============  ================  ==============================================
arch/riscv/Kconfig +0 -0
index 81eb031887d2..73174157212d 100644

arch/riscv/Kconfig config RISCV · line 118

Quoted 4 lines
arch/riscv/Kconfig +1 -0
@@ -115,6 +115,7 @@ config RISCV
 	select HAVE_POSIX_CPU_TIMERS_TASK_WORK
 	select HAVE_REGS_AND_STACK_ACCESS_API
 	select HAVE_RSEQ
+	select HAVE_RUST if 64BIT

Just a small drive by comment, you have 'if 64BIT' here...

Quoted 18 lines
arch/riscv/Kconfig +0 -0
 	select HAVE_STACKPROTECTOR
 	select HAVE_SYSCALL_TRACEPOINTS
 	select IRQ_DOMAIN
arch/riscv/Makefile +1 -1
index 76989561566b..0d6fc4e25221 100644
@@ -29,8 +29,8 @@ ifeq ($(CONFIG_ARCH_RV64I),y)
 
 	KBUILD_CFLAGS += -mabi=lp64
 	KBUILD_AFLAGS += -mabi=lp64
-
 	KBUILD_LDFLAGS += -melf64lriscv
+	KBUILD_RUSTFLAGS += -Ctarget-cpu=generic-rv64
 else
 	BITS := 32
 	UTS_MACHINE := riscv32

arch/riscv/Makefile else · line 41

Quoted 4 lines
arch/riscv/Makefile +1 -0
@@ -38,6 +38,7 @@ else
 	KBUILD_CFLAGS += -mabi=ilp32
 	KBUILD_AFLAGS += -mabi=ilp32
 	KBUILD_LDFLAGS += -melf32lriscv
+	KBUILD_RUSTFLAGS += -Ctarget-cpu=generic-rv32

but also add KBUILD_RUSTFLAGS for the !64BIT case. Seems like one of those can be removed.

Quoted 6 lines
arch/riscv/Makefile +0 -0
 endif
 
 ifeq ($(CONFIG_LD_IS_LLD),y)
Signature · 1 line
2.39.2

Cheers,
Nathan

#4 lore raw

Re: [RFC RESEND 2/2] RISC-V: enable building the 64-bit kernels with rust support

Quoted 52 lines · Nathan Chancellor

Hi Conor,

Quoted 33 lines · Conor Dooley

From: Miguel Ojeda <[email protected]>

The rust modules work on 64-bit RISC-V, with no twiddling required. Select HAS_RUST and provide the required flags to kbuild so that the modules can be used.
32-bit is broken in core rust code, so support is limited to 64-bit
only: ld.lld: error: undefined symbol: __udivdi3

---
 Documentation/rust/arch-support.rst | 2 ++
 arch/riscv/Kconfig                  | 1 +
 arch/riscv/Makefile                 | 3 ++-
 3 files changed, 5 insertions(+), 1 deletion(-)
Documentation/rust/arch-support.rst +2 -0
index 6982b63775da..197919158596 100644
@@ -15,5 +15,7 @@ support corresponds to ``S`` values in the ``MAINTAINERS`` file.
 ============  ================  ==============================================
 Architecture  Level of support  Constraints
 ============  ================  ==============================================
+``riscv``     Maintained        ``rv64`` only.
+============  ================  ==============================================
 ``x86``       Maintained        ``x86_64`` only.
 ============  ================  ==============================================
arch/riscv/Kconfig +0 -0
index 81eb031887d2..73174157212d 100644

arch/riscv/Kconfig config RISCV · line 118

Quoted 4 lines
arch/riscv/Kconfig +1 -0
@@ -115,6 +115,7 @@ config RISCV
 	select HAVE_POSIX_CPU_TIMERS_TASK_WORK
 	select HAVE_REGS_AND_STACK_ACCESS_API
 	select HAVE_RSEQ
+	select HAVE_RUST if 64BIT

Just a small drive by comment, you have 'if 64BIT' here...

Quoted 9 lines
arch/riscv/Kconfig +0 -0
 	select HAVE_STACKPROTECTOR
 	select HAVE_SYSCALL_TRACEPOINTS
 	select IRQ_DOMAIN
arch/riscv/Makefile +0 -0
index 76989561566b..0d6fc4e25221 100644
@@ -29,8 +29,8 @@ ifeq ($(CONFIG_ARCH_RV64I),y)
 
Quoted 16 lines

arch/riscv/Makefile else · line 41

Quoted 13 lines
arch/riscv/Makefile +2 -1
 	KBUILD_CFLAGS += -mabi=lp64
 	KBUILD_AFLAGS += -mabi=lp64
-
 	KBUILD_LDFLAGS += -melf64lriscv
+	KBUILD_RUSTFLAGS += -Ctarget-cpu=generic-rv64
 else
 	BITS := 32
 	UTS_MACHINE := riscv32
@@ -38,6 +38,7 @@ else
 	KBUILD_CFLAGS += -mabi=ilp32
 	KBUILD_AFLAGS += -mabi=ilp32
 	KBUILD_LDFLAGS += -melf32lriscv
+	KBUILD_RUSTFLAGS += -Ctarget-cpu=generic-rv32

but also add KBUILD_RUSTFLAGS for the !64BIT case. Seems like one of those can be removed.

Yeah & it's ditto for the rv32 handling in 1/2 as well. Ideally there wouldn't be implicit 64-bit division and the "if 64BIT" could go away. I just left things as-lifted, but I'll go drop anything 32-bit related if this series looses the RFC prefix ;)

#5 lore raw

Re: [RFC RESEND 2/2] RISC-V: enable building the 64-bit kernels with rust support

In reply to Conor Dooley
Quoted 3 lines · Conor Dooley

The rust modules work on 64-bit RISC-V, with no twiddling required. Select HAS_RUST and provide the required flags to kbuild so that the

HAS -> HAVE

Documentation/rust/arch-support.rst support corresponds to ``S`` values in the ``MAINTAINERS`` file. · line 20

Quoted 11 lines
Documentation/rust/arch-support.rst +2 -0
index 6982b63775da..197919158596 100644
@@ -15,5 +15,7 @@ support corresponds to ``S`` values in the ``MAINTAINERS`` file.
 ============  ================  ==============================================
 Architecture  Level of support  Constraints
 ============  ================  ==============================================
+``riscv``     Maintained        ``rv64`` only.
+============  ================  ==============================================
 ``x86``       Maintained        ``x86_64`` only.

I think this separator between rows should not be here (it is not in rust-for-linux/rust). Please see
https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#simple-tables.

Cheers,
Miguel

#6 lore raw

Re: [RFC RESEND 2/2] RISC-V: enable building the 64-bit kernels with rust support

Quoted 17 lines · Miguel Ojeda

Documentation/rust/arch-support.rst support corresponds to ``S`` values in the ``MAINTAINERS`` file. · line 20

Quoted 11 lines · Conor Dooley
Documentation/rust/arch-support.rst +2 -0
index 6982b63775da..197919158596 100644
@@ -15,5 +15,7 @@ support corresponds to ``S`` values in the ``MAINTAINERS`` file.
 ============  ================  ==============================================
 Architecture  Level of support  Constraints
 ============  ================  ==============================================
+``riscv``     Maintained        ``rv64`` only.
+============  ================  ==============================================
 ``x86``       Maintained        ``x86_64`` only.

I think this separator between rows should not be here (it is not in rust-for-linux/rust). Please see
https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#simple-tables.

That's what the "let the automation tell me if I messed up rst" approach gets you ;)