Skip to content

Commit 87447f4

Browse files
committed
Add Rust support for RISC-V arch
Signed-off-by: Gary Guo <[email protected]>
1 parent 3e1c596 commit 87447f4

File tree

9 files changed

+176
-5
lines changed

9 files changed

+176
-5
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ KBUILD_CFLAGS := -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs \
511511
-Werror=return-type -Wno-format-security \
512512
-std=gnu89
513513
KBUILD_CPPFLAGS := -D__KERNEL__
514+
KBUILD_RUSTC_TARGET := $(srctree)/arch/$(SRCARCH)/rust/target.json
514515
KBUILD_RUSTCFLAGS := --emit=dep-info,obj,metadata --edition=2018 \
515516
-Cpanic=abort -Cembed-bitcode=n -Clto=n -Crpath=n \
516517
-Cforce-unwind-tables=n -Ccodegen-units=1 \
@@ -542,7 +543,7 @@ export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE
542543

543544
export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS
544545
export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
545-
export KBUILD_RUSTCFLAGS RUSTCFLAGS_KERNEL RUSTCFLAGS_MODULE
546+
export KBUILD_RUSTC_TARGET KBUILD_RUSTCFLAGS RUSTCFLAGS_KERNEL RUSTCFLAGS_MODULE
546547
export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
547548
export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_RUSTCFLAGS_MODULE KBUILD_LDFLAGS_MODULE
548549
export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL KBUILD_RUSTCFLAGS_KERNEL

arch/riscv/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ riscv-march-$(CONFIG_FPU) := $(riscv-march-y)fd
4545
riscv-march-$(CONFIG_RISCV_ISA_C) := $(riscv-march-y)c
4646
KBUILD_CFLAGS += -march=$(subst fd,,$(riscv-march-y))
4747
KBUILD_AFLAGS += -march=$(riscv-march-y)
48+
KBUILD_RUSTC_TARGET := $(srctree)/arch/riscv/rust/$(subst fd,,$(riscv-march-y)).json
4849

4950
KBUILD_CFLAGS += -mno-save-restore
5051
KBUILD_CFLAGS += -DCONFIG_PAGE_OFFSET=$(CONFIG_PAGE_OFFSET)

arch/riscv/rust/rv32ima.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"arch": "riscv32",
3+
"code-model": "medium",
4+
"cpu": "generic-rv32",
5+
"data-layout": "e-m:e-p:32:32-i64:64-n32-S128",
6+
"disable-redzone": true,
7+
"eliminate-frame-pointer": false,
8+
"emit-debug-gdb-scripts": false,
9+
"env": "gnu",
10+
"features": "+m,+a",
11+
"function-sections": false,
12+
"is-builtin": true,
13+
"linker-flavor": "gcc",
14+
"linker-is-gnu": true,
15+
"llvm-target": "riscv32",
16+
"max-atomic-width": 32,
17+
"needs-plt": true,
18+
"os": "none",
19+
"panic-strategy": "abort",
20+
"position-independent-executables": true,
21+
"pre-link-args": {
22+
"gcc": [
23+
"-Wl,--as-needed",
24+
"-Wl,-z,noexecstack",
25+
"-m32"
26+
]
27+
},
28+
"relocation-model": "static",
29+
"relro-level": "full",
30+
"stack-probes": {
31+
"kind": "inline-or-call",
32+
"min-llvm-version-for-inline": [
33+
11,
34+
0,
35+
1
36+
]
37+
},
38+
"target-c-int-width": "32",
39+
"target-endian": "little",
40+
"target-pointer-width": "32",
41+
"vendor": ""
42+
}

arch/riscv/rust/rv32imac.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"arch": "riscv32",
3+
"code-model": "medium",
4+
"cpu": "generic-rv32",
5+
"data-layout": "e-m:e-p:32:32-i64:64-n32-S128",
6+
"disable-redzone": true,
7+
"eliminate-frame-pointer": false,
8+
"emit-debug-gdb-scripts": false,
9+
"env": "gnu",
10+
"features": "+m,+a,+c",
11+
"function-sections": false,
12+
"is-builtin": true,
13+
"linker-flavor": "gcc",
14+
"linker-is-gnu": true,
15+
"llvm-target": "riscv32",
16+
"max-atomic-width": 32,
17+
"needs-plt": true,
18+
"os": "none",
19+
"panic-strategy": "abort",
20+
"position-independent-executables": true,
21+
"pre-link-args": {
22+
"gcc": [
23+
"-Wl,--as-needed",
24+
"-Wl,-z,noexecstack",
25+
"-m32"
26+
]
27+
},
28+
"relocation-model": "static",
29+
"relro-level": "full",
30+
"stack-probes": {
31+
"kind": "inline-or-call",
32+
"min-llvm-version-for-inline": [
33+
11,
34+
0,
35+
1
36+
]
37+
},
38+
"target-c-int-width": "32",
39+
"target-endian": "little",
40+
"target-pointer-width": "32",
41+
"vendor": ""
42+
}

arch/riscv/rust/rv64ima.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"arch": "riscv64",
3+
"code-model": "medium",
4+
"cpu": "generic-rv64",
5+
"data-layout": "e-m:e-p:64:64-i64:64-i128:128-n64-S128",
6+
"disable-redzone": true,
7+
"eliminate-frame-pointer": false,
8+
"emit-debug-gdb-scripts": false,
9+
"env": "gnu",
10+
"features": "+m,+a",
11+
"function-sections": false,
12+
"is-builtin": true,
13+
"linker-flavor": "gcc",
14+
"linker-is-gnu": true,
15+
"llvm-target": "riscv64",
16+
"max-atomic-width": 64,
17+
"needs-plt": true,
18+
"os": "none",
19+
"panic-strategy": "abort",
20+
"position-independent-executables": true,
21+
"pre-link-args": {
22+
"gcc": [
23+
"-Wl,--as-needed",
24+
"-Wl,-z,noexecstack",
25+
"-m64"
26+
]
27+
},
28+
"relocation-model": "static",
29+
"relro-level": "full",
30+
"stack-probes": {
31+
"kind": "inline-or-call",
32+
"min-llvm-version-for-inline": [
33+
11,
34+
0,
35+
1
36+
]
37+
},
38+
"target-c-int-width": "32",
39+
"target-endian": "little",
40+
"target-pointer-width": "64",
41+
"vendor": ""
42+
}

arch/riscv/rust/rv64imac.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"arch": "riscv64",
3+
"code-model": "medium",
4+
"cpu": "generic-rv64",
5+
"data-layout": "e-m:e-p:64:64-i64:64-i128:128-n64-S128",
6+
"disable-redzone": true,
7+
"eliminate-frame-pointer": false,
8+
"emit-debug-gdb-scripts": false,
9+
"env": "gnu",
10+
"features": "+m,+a,+c",
11+
"function-sections": false,
12+
"is-builtin": true,
13+
"linker-flavor": "gcc",
14+
"linker-is-gnu": true,
15+
"llvm-target": "riscv64",
16+
"max-atomic-width": 64,
17+
"needs-plt": true,
18+
"os": "none",
19+
"panic-strategy": "abort",
20+
"position-independent-executables": true,
21+
"pre-link-args": {
22+
"gcc": [
23+
"-Wl,--as-needed",
24+
"-Wl,-z,noexecstack",
25+
"-m64"
26+
]
27+
},
28+
"relocation-model": "static",
29+
"relro-level": "full",
30+
"stack-probes": {
31+
"kind": "inline-or-call",
32+
"min-llvm-version-for-inline": [
33+
11,
34+
0,
35+
1
36+
]
37+
},
38+
"target-c-int-width": "32",
39+
"target-endian": "little",
40+
"target-pointer-width": "64",
41+
"vendor": ""
42+
}

init/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ config LLD_VERSION
5858
default 0
5959

6060
config HAS_RUST
61-
depends on ARM64 || CPU_32v6 || CPU_32v6K || (PPC64 && CPU_LITTLE_ENDIAN) || X86_64
61+
depends on ARM64 || CPU_32v6 || CPU_32v6K || (PPC64 && CPU_LITTLE_ENDIAN) || X86_64 || RISCV
6262
def_bool $(success,$(RUSTC) --version)
6363

6464
config RUSTC_VERSION

rust/kernel/c_types.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
#![allow(non_camel_case_types)]
1111

12-
#[cfg(any(target_arch = "arm", target_arch = "x86"))]
12+
#[cfg(any(target_arch = "arm", target_arch = "x86", target_arch = "riscv32",))]
1313
mod c {
1414
/// C `void` type.
1515
pub type c_void = core::ffi::c_void;
@@ -63,7 +63,8 @@ mod c {
6363
#[cfg(any(
6464
target_arch = "aarch64",
6565
target_arch = "x86_64",
66-
target_arch = "powerpc64"
66+
target_arch = "powerpc64",
67+
target_arch = "riscv64",
6768
))]
6869
mod c {
6970
/// C `void` type.

scripts/Makefile.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ $(obj)/%.lst: $(src)/%.c FORCE
291291
# Compile Rust sources (.rs)
292292
# ---------------------------------------------------------------------------
293293

294-
rustc_cross_flags := --target=$(srctree)/arch/$(SRCARCH)/rust/target.json
294+
rustc_cross_flags := --target=$(KBUILD_RUSTC_TARGET)
295295

296296
quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
297297
cmd_rustc_o_rs = \

0 commit comments

Comments
 (0)