Skip to content

Commit

Permalink
Add Rust support for RISC-V arch
Browse files Browse the repository at this point in the history
Signed-off-by: Gary Guo <gary@garyguo.net>
  • Loading branch information
nbdd0121 committed Apr 25, 2021
1 parent 3e1c596 commit 87447f4
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ KBUILD_CFLAGS := -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs \
-Werror=return-type -Wno-format-security \
-std=gnu89
KBUILD_CPPFLAGS := -D__KERNEL__
KBUILD_RUSTC_TARGET := $(srctree)/arch/$(SRCARCH)/rust/target.json
KBUILD_RUSTCFLAGS := --emit=dep-info,obj,metadata --edition=2018 \
-Cpanic=abort -Cembed-bitcode=n -Clto=n -Crpath=n \
-Cforce-unwind-tables=n -Ccodegen-units=1 \
Expand Down Expand Up @@ -542,7 +543,7 @@ export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE

export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS
export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
export KBUILD_RUSTCFLAGS RUSTCFLAGS_KERNEL RUSTCFLAGS_MODULE
export KBUILD_RUSTC_TARGET KBUILD_RUSTCFLAGS RUSTCFLAGS_KERNEL RUSTCFLAGS_MODULE
export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_RUSTCFLAGS_MODULE KBUILD_LDFLAGS_MODULE
export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL KBUILD_RUSTCFLAGS_KERNEL
Expand Down
1 change: 1 addition & 0 deletions arch/riscv/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ riscv-march-$(CONFIG_FPU) := $(riscv-march-y)fd
riscv-march-$(CONFIG_RISCV_ISA_C) := $(riscv-march-y)c
KBUILD_CFLAGS += -march=$(subst fd,,$(riscv-march-y))
KBUILD_AFLAGS += -march=$(riscv-march-y)
KBUILD_RUSTC_TARGET := $(srctree)/arch/riscv/rust/$(subst fd,,$(riscv-march-y)).json

KBUILD_CFLAGS += -mno-save-restore
KBUILD_CFLAGS += -DCONFIG_PAGE_OFFSET=$(CONFIG_PAGE_OFFSET)
Expand Down
42 changes: 42 additions & 0 deletions arch/riscv/rust/rv32ima.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"arch": "riscv32",
"code-model": "medium",
"cpu": "generic-rv32",
"data-layout": "e-m:e-p:32:32-i64:64-n32-S128",
"disable-redzone": true,
"eliminate-frame-pointer": false,
"emit-debug-gdb-scripts": false,
"env": "gnu",
"features": "+m,+a",
"function-sections": false,
"is-builtin": true,
"linker-flavor": "gcc",
"linker-is-gnu": true,
"llvm-target": "riscv32",
"max-atomic-width": 32,
"needs-plt": true,
"os": "none",
"panic-strategy": "abort",
"position-independent-executables": true,
"pre-link-args": {
"gcc": [
"-Wl,--as-needed",
"-Wl,-z,noexecstack",
"-m32"
]
},
"relocation-model": "static",
"relro-level": "full",
"stack-probes": {
"kind": "inline-or-call",
"min-llvm-version-for-inline": [
11,
0,
1
]
},
"target-c-int-width": "32",
"target-endian": "little",
"target-pointer-width": "32",
"vendor": ""
}
42 changes: 42 additions & 0 deletions arch/riscv/rust/rv32imac.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"arch": "riscv32",
"code-model": "medium",
"cpu": "generic-rv32",
"data-layout": "e-m:e-p:32:32-i64:64-n32-S128",
"disable-redzone": true,
"eliminate-frame-pointer": false,
"emit-debug-gdb-scripts": false,
"env": "gnu",
"features": "+m,+a,+c",
"function-sections": false,
"is-builtin": true,
"linker-flavor": "gcc",
"linker-is-gnu": true,
"llvm-target": "riscv32",
"max-atomic-width": 32,
"needs-plt": true,
"os": "none",
"panic-strategy": "abort",
"position-independent-executables": true,
"pre-link-args": {
"gcc": [
"-Wl,--as-needed",
"-Wl,-z,noexecstack",
"-m32"
]
},
"relocation-model": "static",
"relro-level": "full",
"stack-probes": {
"kind": "inline-or-call",
"min-llvm-version-for-inline": [
11,
0,
1
]
},
"target-c-int-width": "32",
"target-endian": "little",
"target-pointer-width": "32",
"vendor": ""
}
42 changes: 42 additions & 0 deletions arch/riscv/rust/rv64ima.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"arch": "riscv64",
"code-model": "medium",
"cpu": "generic-rv64",
"data-layout": "e-m:e-p:64:64-i64:64-i128:128-n64-S128",
"disable-redzone": true,
"eliminate-frame-pointer": false,
"emit-debug-gdb-scripts": false,
"env": "gnu",
"features": "+m,+a",
"function-sections": false,
"is-builtin": true,
"linker-flavor": "gcc",
"linker-is-gnu": true,
"llvm-target": "riscv64",
"max-atomic-width": 64,
"needs-plt": true,
"os": "none",
"panic-strategy": "abort",
"position-independent-executables": true,
"pre-link-args": {
"gcc": [
"-Wl,--as-needed",
"-Wl,-z,noexecstack",
"-m64"
]
},
"relocation-model": "static",
"relro-level": "full",
"stack-probes": {
"kind": "inline-or-call",
"min-llvm-version-for-inline": [
11,
0,
1
]
},
"target-c-int-width": "32",
"target-endian": "little",
"target-pointer-width": "64",
"vendor": ""
}
42 changes: 42 additions & 0 deletions arch/riscv/rust/rv64imac.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"arch": "riscv64",
"code-model": "medium",
"cpu": "generic-rv64",
"data-layout": "e-m:e-p:64:64-i64:64-i128:128-n64-S128",
"disable-redzone": true,
"eliminate-frame-pointer": false,
"emit-debug-gdb-scripts": false,
"env": "gnu",
"features": "+m,+a,+c",
"function-sections": false,
"is-builtin": true,
"linker-flavor": "gcc",
"linker-is-gnu": true,
"llvm-target": "riscv64",
"max-atomic-width": 64,
"needs-plt": true,
"os": "none",
"panic-strategy": "abort",
"position-independent-executables": true,
"pre-link-args": {
"gcc": [
"-Wl,--as-needed",
"-Wl,-z,noexecstack",
"-m64"
]
},
"relocation-model": "static",
"relro-level": "full",
"stack-probes": {
"kind": "inline-or-call",
"min-llvm-version-for-inline": [
11,
0,
1
]
},
"target-c-int-width": "32",
"target-endian": "little",
"target-pointer-width": "64",
"vendor": ""
}
2 changes: 1 addition & 1 deletion init/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ config LLD_VERSION
default 0

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

config RUSTC_VERSION
Expand Down
5 changes: 3 additions & 2 deletions rust/kernel/c_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#![allow(non_camel_case_types)]

#[cfg(any(target_arch = "arm", target_arch = "x86"))]
#[cfg(any(target_arch = "arm", target_arch = "x86", target_arch = "riscv32",))]
mod c {
/// C `void` type.
pub type c_void = core::ffi::c_void;
Expand Down Expand Up @@ -63,7 +63,8 @@ mod c {
#[cfg(any(
target_arch = "aarch64",
target_arch = "x86_64",
target_arch = "powerpc64"
target_arch = "powerpc64",
target_arch = "riscv64",
))]
mod c {
/// C `void` type.
Expand Down
2 changes: 1 addition & 1 deletion scripts/Makefile.build
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ $(obj)/%.lst: $(src)/%.c FORCE
# Compile Rust sources (.rs)
# ---------------------------------------------------------------------------

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

quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
cmd_rustc_o_rs = \
Expand Down

0 comments on commit 87447f4

Please sign in to comment.