Skip to content

Commit a412275

Browse files
committed
Initial RISC-V64 support
1 parent ff853a7 commit a412275

File tree

34 files changed

+1196
-34
lines changed

34 files changed

+1196
-34
lines changed

.gitmodules

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[submodule "openlibm"]
22
path = openlibm
3-
url = https://gitlab.redox-os.org/redox-os/openlibm.git
3+
url = https://github.com/JuliaMath/openlibm.git
4+
branch = master
45
[submodule "posix-regex"]
56
path = posix-regex
67
url = https://gitlab.redox-os.org/redox-os/posix-regex.git

Cargo.lock

+4-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,6 @@ panic = "abort"
7474

7575
[profile.release]
7676
panic = "abort"
77+
78+
[patch.crates-io]
79+
cc-11 = { git = "https://github.com/tea/cc-rs", branch="riscv-abi-arch-fix", package = "cc" }

Makefile

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
TARGET?=$(shell rustc -Z unstable-options --print target-spec-json | grep llvm-target | cut -d '"' -f4)
1+
export TARGET?=$(shell rustc -Z unstable-options --print target-spec-json | grep llvm-target | cut -d '"' -f4)
22

33
CARGO?=cargo
44
CARGO_TEST?=$(CARGO)
@@ -20,35 +20,54 @@ ifeq ($(TARGET),aarch64-unknown-linux-gnu)
2020
export CC=aarch64-linux-gnu-gcc
2121
export LD=aarch64-linux-gnu-ld
2222
export AR=aarch64-linux-gnu-ar
23+
export NM=aarch64-linux-gnu-nm
2324
export OBJCOPY=aarch64-linux-gnu-objcopy
25+
export CPPFLAGS=
2426
endif
2527

2628
ifeq ($(TARGET),aarch64-unknown-redox)
2729
export CC=aarch64-unknown-redox-gcc
2830
export LD=aarch64-unknown-redox-ld
2931
export AR=aarch64-unknown-redox-ar
32+
export NM=aarch64-unknown-redox-nm
3033
export OBJCOPY=aarch64-unknown-redox-objcopy
34+
export CPPFLAGS=
3135
endif
3236

3337
ifeq ($(TARGET),x86_64-unknown-linux-gnu)
3438
export CC=x86_64-linux-gnu-gcc
3539
export LD=x86_64-linux-gnu-ld
3640
export AR=x86_64-linux-gnu-ar
41+
export NM=x86_64-linux-gnu-nm
3742
export OBJCOPY=x86_64-linux-gnu-objcopy
43+
export CPPFLAGS=
3844
endif
3945

4046
ifeq ($(TARGET),i686-unknown-redox)
4147
export CC=i686-unknown-redox-gcc
4248
export LD=i686-unknown-redox-ld
4349
export AR=i686-unknown-redox-ar
50+
export NM=i686-unknown-redox-nm
4451
export OBJCOPY=i686-unknown-redox-objcopy
52+
export CPPFLAGS=
4553
endif
4654

4755
ifeq ($(TARGET),x86_64-unknown-redox)
4856
export CC=x86_64-unknown-redox-gcc
4957
export LD=x86_64-unknown-redox-ld
5058
export AR=x86_64-unknown-redox-ar
59+
export NM=x86_64-unknown-redox-nm
5160
export OBJCOPY=x86_64-unknown-redox-objcopy
61+
export CPPFLAGS=
62+
endif
63+
64+
ifeq ($(TARGET),riscv64gc-unknown-redox)
65+
export CC=riscv64-unknown-redox-gcc
66+
export LD=riscv64-unknown-redox-ld
67+
export AR=riscv64-unknown-redox-ar
68+
export NM=riscv64-unknown-redox-nm
69+
export OBJCOPY=riscv64-unknown-redox-objcopy
70+
export CPPFLAGS=-march=rv64gc -mabi=lp64d
5271
endif
5372

5473
SRC=\
@@ -158,7 +177,7 @@ $(BUILD)/debug/libc.so: $(BUILD)/debug/librelibc.a $(BUILD)/openlibm/libopenlibm
158177

159178
$(BUILD)/debug/librelibc.a: $(SRC)
160179
$(CARGO) rustc $(CARGOFLAGS) -- --emit link=$@ $(RUSTCFLAGS)
161-
./renamesyms.sh $@ $(BUILD)/debug/deps/
180+
./renamesyms.sh "$@" "$(BUILD)/debug/deps/"
162181
touch $@
163182

164183
$(BUILD)/debug/crt0.o: $(SRC)
@@ -198,7 +217,7 @@ $(BUILD)/release/librelibc.a: $(SRC)
198217
$(CARGO) rustc --release $(CARGOFLAGS) -- --emit link=$@ $(RUSTCFLAGS)
199218
# TODO: Better to only allow a certain whitelisted set of symbols? Perhaps
200219
# use some cbindgen hook, specify them manually, or grep for #[no_mangle].
201-
./renamesyms.sh $@ $(BUILD)/release/deps/
220+
./renamesyms.sh "$@" "$(BUILD)/release/deps/"
202221
touch $@
203222

204223
$(BUILD)/release/crt0.o: $(SRC)
@@ -230,4 +249,4 @@ $(BUILD)/openlibm: openlibm
230249
touch $@
231250

232251
$(BUILD)/openlibm/libopenlibm.a: $(BUILD)/openlibm $(BUILD)/release/librelibc.a
233-
$(MAKE) AR=$(AR) CC=$(CC) LD=$(LD) CPPFLAGS="-fno-stack-protector -I$(shell pwd)/include -I$(TARGET_HEADERS)" -C $< libopenlibm.a
252+
$(MAKE) AR=$(AR) CC=$(CC) LD=$(LD) CPPFLAGS="$(CPPFLAGS) -fno-stack-protector -I$(shell pwd)/include -I$(TARGET_HEADERS)" -C $< libopenlibm.a

cbindgen.globdefs.toml

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"target_arch=x86" = "__i386__"
99
"target_arch=x86_64" = "__x86_64__"
1010
"target_arch=aarch64" = "__aarch64__"
11+
# This is not exact. It should be `defined(__riscv) && defined(__LP64__)`, or `defined(__riscv) && __riscv_xlen==64`
12+
# This will do however, as long as we only support riscv64 and not riscv32
13+
"target_arch=riscv64" = "__riscv"
1114

1215
# XXX: silences a warning
1316
"feature = no_std" = "__relibc__"

generic-rt/src/lib.rs

+15
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ impl<Os> GenericTcb<Os> {
6464
value
6565
}
6666

67+
/// Architecture specific code to read a usize from the TCB - riscv64
68+
#[inline(always)]
69+
#[cfg(target_arch = "riscv64")]
70+
unsafe fn arch_read(offset: usize) -> usize {
71+
let value;
72+
asm!(
73+
"ld {value}, -8(tp)", // TCB
74+
"add {value}, {value}, {offset}",
75+
"ld {value}, 0({value})",
76+
value = out(reg) value,
77+
offset = in(reg) offset,
78+
);
79+
value
80+
}
81+
6782
pub unsafe fn current_ptr() -> Option<*mut Self> {
6883
let tcb_ptr = Self::arch_read(offset_of!(Self, tcb_ptr)) as *mut Self;
6984
let tcb_len = Self::arch_read(offset_of!(Self, tcb_len));

include/setjmp.h

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ typedef unsigned long long jmp_buf[8];
6161
typedef unsigned long jmp_buf[8];
6262
#endif
6363

64+
#ifdef __riscv
65+
typedef unsigned long jmp_buf[26];
66+
#endif
67+
6468
#ifdef __cplusplus
6569
extern "C" {
6670
#endif

include/sys/user.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
#include <arch/x64/user.h>
55
#elif defined(__aarch64__)
66
#include <arch/aarch64/user.h>
7+
#elif defined(__riscv) && __riscv_xlen==64
8+
#include <arch/riscv64/user.h>
79
#else
810
#error "Unknown architecture"
911
#endif
1012

11-
#endif
13+
#endif

0 commit comments

Comments
 (0)