Skip to content

Commit 70a57b2

Browse files
ojedapalmer-dabbelt
authored andcommitted
RISC-V: enable building 64-bit kernels with rust support
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 <[email protected]> Signed-off-by: Gary Guo <[email protected]> Signed-off-by: Miguel Ojeda <[email protected]> Co-developed-by: Conor Dooley <[email protected]> Signed-off-by: Conor Dooley <[email protected]> Link: https://lore.kernel.org/r/20240409-silencer-book-ce1320f06aab@spud Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 300ce44 commit 70a57b2

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

Documentation/rust/arch-support.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Architecture Level of support Constraints
1717
============= ================ ==============================================
1818
``arm64`` Maintained Little Endian only.
1919
``loongarch`` Maintained -
20+
``riscv`` Maintained ``riscv64`` only.
2021
``um`` Maintained ``x86_64`` only.
2122
``x86`` Maintained ``x86_64`` only.
2223
============= ================ ==============================================

arch/riscv/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ config RISCV
155155
select HAVE_REGS_AND_STACK_ACCESS_API
156156
select HAVE_RETHOOK if !XIP_KERNEL
157157
select HAVE_RSEQ
158+
select HAVE_RUST if 64BIT
158159
select HAVE_SAMPLE_FTRACE_DIRECT
159160
select HAVE_SAMPLE_FTRACE_DIRECT_MULTI
160161
select HAVE_STACKPROTECTOR

arch/riscv/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ ifeq ($(CONFIG_ARCH_RV64I),y)
3434
KBUILD_AFLAGS += -mabi=lp64
3535

3636
KBUILD_LDFLAGS += -melf64lriscv
37+
38+
KBUILD_RUSTFLAGS += -Ctarget-cpu=generic-rv64 --target=riscv64imac-unknown-none-elf \
39+
-Cno-redzone
3740
else
3841
BITS := 32
3942
UTS_MACHINE := riscv32
@@ -68,6 +71,10 @@ riscv-march-$(CONFIG_FPU) := $(riscv-march-y)fd
6871
riscv-march-$(CONFIG_RISCV_ISA_C) := $(riscv-march-y)c
6972
riscv-march-$(CONFIG_RISCV_ISA_V) := $(riscv-march-y)v
7073

74+
ifneq ($(CONFIG_RISCV_ISA_C),y)
75+
KBUILD_RUSTFLAGS += -Ctarget-feature=-c
76+
endif
77+
7178
ifdef CONFIG_TOOLCHAIN_NEEDS_OLD_ISA_SPEC
7279
KBUILD_CFLAGS += -Wa,-misa-spec=2.2
7380
KBUILD_AFLAGS += -Wa,-misa-spec=2.2

scripts/generate_rust_target.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ fn main() {
150150
// `llvm-target`s are taken from `scripts/Makefile.clang`.
151151
if cfg.has("ARM64") {
152152
panic!("arm64 uses the builtin rustc aarch64-unknown-none target");
153+
} else if cfg.has("RISCV") {
154+
if cfg.has("64BIT") {
155+
panic!("64-bit RISC-V uses the builtin rustc riscv64-unknown-none-elf target");
156+
} else {
157+
panic!("32-bit RISC-V is an unsupported architecture");
158+
}
153159
} else if cfg.has("X86_64") {
154160
ts.push("arch", "x86_64");
155161
ts.push(

0 commit comments

Comments
 (0)