Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented riscv64 syscall support by modifying source code of opensbi and sbi-spec #69

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,19 @@ jobs:
crate: cargo-binutils
version: latest
use-tool-cache: true
# musl toolchain is also needed in non-musl apps
# because we need to build opensbi under riscv64
- uses: ./.github/workflows/actions/setup-musl
with:
arch: ${{ matrix.arch }}

- name: Build display/basic_painting
run: make ARCH=${{ matrix.arch }} A=apps/display/basic_painting
- name: Build display/draw_map
run: make ARCH=${{ matrix.arch }} A=apps/display/draw_map
- name: Build fs/shell
run: make ARCH=${{ matrix.arch }} A=apps/fs/shell

- uses: ./.github/workflows/actions/setup-musl
with:
arch: ${{ matrix.arch }}
- name: Build c/helloworld
run: make ARCH=${{ matrix.arch }} A=apps/c/helloworld
- name: Build c/memtest
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "patches/opensbi"]
path = patches/opensbi
url = https://github.com/Sssssaltyfish/opensbi.git
12 changes: 4 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ members = [
"crates/driver_net",
"crates/driver_pci",
"crates/driver_virtio",
"crates/dtb",
"crates/dtb",
"crates/flatten_objects",
"crates/lazy_init",
"crates/linked_list",
Expand Down Expand Up @@ -67,3 +67,6 @@ lto = true
[profile.reldebug]
inherits = "release"
debug = true

[patch.crates-io]
sbi-spec = { git = "https://github.com/Sssssaltyfish/rustsbi.git", rev = "a25d448d3d770c6db493a472e3e78d949b63f523", optional = true }
24 changes: 23 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,19 @@ LD_SCRIPT := $(CURDIR)/modules/ruxhal/linker_$(PLATFORM_NAME).lds
OUT_ELF := $(OUT_DIR)/$(APP_NAME)_$(PLATFORM_NAME).elf
OUT_BIN := $(OUT_DIR)/$(APP_NAME)_$(PLATFORM_NAME).bin

PREBUILD := $(CURDIR)/scripts/prebuild/$(ARCH).mk

all: build

# prebuild option to be overriden in `$(PREBUILD)`
define run_prebuild
endef

# prebuild makefile might override some variables by their own
# so it must be placed before the rest of the makefiles
ifneq ($(wildcard $(PREBUILD)),)
include $(PREBUILD)
endif
include scripts/make/utils.mk
include scripts/make/build.mk
include scripts/make/qemu.mk
Expand All @@ -178,6 +189,16 @@ else ifeq ($(PLATFORM_NAME), aarch64-bsta1000b)
include scripts/make/bsta1000b-fada.mk
endif

_force: ;

# prebuild scripts must track their dependencies by themselves
prebuild: _force
$(call run_prebuild)

$(OUT_DIR): prebuild

$(OUT_BIN): prebuild

build: $(OUT_DIR) $(OUT_BIN)

disasm:
Expand Down Expand Up @@ -248,4 +269,5 @@ clean_musl:
rm -rf ulib/ruxmusl/build_*
rm -rf ulib/ruxmusl/install

.PHONY: all build disasm run justrun debug clippy fmt fmt_c test test_no_fail_fast clean clean_c clean_musl doc disk_image debug_no_attach
.PHONY: all build disasm run justrun debug clippy fmt fmt_c test test_no_fail_fast clean clean_c\
clean_musl doc disk_image debug_no_attach prebuild _force
7 changes: 5 additions & 2 deletions api/ruxos_posix_api/src/imp/pthread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Pthread {
}

/// Posix create, used by musl libc
#[cfg(all(feature = "musl", any(target_arch = "x86_64", target_arch = "aarch64")))]
#[cfg(feature = "musl")]
fn pcreate(
_attr: *const ctypes::pthread_attr_t,
start_routine: extern "C" fn(arg: *mut c_void) -> *mut c_void,
Expand Down Expand Up @@ -246,7 +246,10 @@ unsafe impl<T> Send for ForceSendSync<T> {}
unsafe impl<T> Sync for ForceSendSync<T> {}

/// Create new thread by `sys_clone`, return new thread ID
#[cfg(all(feature = "musl", target_arch = "aarch64"))]
#[cfg(all(
feature = "musl",
any(target_arch = "aarch64", target_arch = "riscv64")
))]
pub unsafe fn sys_clone(
flags: c_int,
stack: *mut c_void,
Expand Down
6 changes: 1 addition & 5 deletions api/ruxos_posix_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,7 @@ pub use imp::signal::{sys_getitimer, sys_kill, sys_setitimer, sys_sigaction, sys

#[cfg(feature = "multitask")]
pub use imp::pthread::futex::sys_futex;
#[cfg(all(
feature = "multitask",
feature = "musl",
any(target_arch = "aarch64", target_arch = "x86_64")
))]
#[cfg(all(feature = "multitask", feature = "musl"))]
pub use imp::pthread::sys_clone;
#[cfg(all(feature = "multitask", feature = "musl"))]
pub use imp::pthread::sys_set_tid_address;
Expand Down
2 changes: 1 addition & 1 deletion modules/ruxhal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ raw-cpuid = "11.0"

[target.'cfg(any(target_arch = "riscv32", target_arch = "riscv64"))'.dependencies]
riscv = "0.10"
sbi-rt = { version = "0.0.2", features = ["legacy"] }
sbi-rt = { version = "0.0.3", features = ["legacy"] }
dtb = {path = "../../crates/dtb" }

[target.'cfg(target_arch = "aarch64")'.dependencies]
Expand Down
15 changes: 15 additions & 0 deletions modules/ruxhal/src/arch/riscv/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ fn riscv_trap_handler(tf: &mut TrapFrame, _from_user: bool) {
match scause.cause() {
Trap::Exception(E::Breakpoint) => handle_breakpoint(&mut tf.sepc),
Trap::Interrupt(_) => crate::trap::handle_irq_extern(scause.bits()),
#[cfg(feature = "musl")]
Trap::Exception(E::UserEnvCall) => {
let ret = crate::trap::handle_syscall(
tf.regs.a7,
[
tf.regs.a0 as _,
tf.regs.a1 as _,
tf.regs.a2 as _,
tf.regs.a3 as _,
tf.regs.a4 as _,
tf.regs.a5 as _,
],
);
tf.regs.a0 = ret as _;
}
_ => {
panic!(
"Unhandled trap {:?} @ {:#x}:\n{:#x?}",
Expand Down
18 changes: 13 additions & 5 deletions modules/ruxhal/src/platform/riscv64_qemu_virt/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use riscv::register::satp;

use ruxconfig::{PHYS_VIRT_OFFSET, TASK_STACK_SIZE};
use ruxconfig::{PHYS_MEMORY_SIZE, PHYS_VIRT_OFFSET, TASK_STACK_SIZE};

#[link_section = ".bss.stack"]
static mut BOOT_STACK: [u8; TASK_STACK_SIZE] = [0; TASK_STACK_SIZE];
Expand All @@ -18,10 +18,18 @@ static mut BOOT_STACK: [u8; TASK_STACK_SIZE] = [0; TASK_STACK_SIZE];
static mut BOOT_PT_SV39: [u64; 512] = [0; 512];

unsafe fn init_boot_page_table() {
// 0x8000_0000..0xc000_0000, VRWX_GAD, 1G block
BOOT_PT_SV39[2] = (0x80000 << 10) | 0xef;
// 0xffff_ffc0_8000_0000..0xffff_ffc0_c000_0000, VRWX_GAD, 1G block
BOOT_PT_SV39[0x102] = (0x80000 << 10) | 0xef;
const MEMORY_GBS: usize = PHYS_MEMORY_SIZE.div_ceil(1024 * 1024 * 1024);
const PPN_2_OFFSET: u64 = 10 + 9 + 9;
// start from 0x8000_0000, first block 0x8000_0000..0xc000_0000
// VRWX_GAD, 1G Gigapages
for i in 0..MEMORY_GBS {
BOOT_PT_SV39[2 + i] = ((2 + i as u64) << PPN_2_OFFSET) | 0xef;
}
// start from 0xffff_ffc0_8000_0000, first block 0xffff_ffc0_8000_0000..0xffff_ffc0_c000_0000
// VRWX_GAD, 1G Gigapages
for i in 0..MEMORY_GBS {
BOOT_PT_SV39[0x102 + i] = ((2 + i as u64) << PPN_2_OFFSET) | 0xef;
}
}

unsafe fn init_mmu() {
Expand Down
1 change: 1 addition & 0 deletions patches/opensbi
Submodule opensbi added at 76345f
2 changes: 1 addition & 1 deletion platforms/aarch64-qemu-virt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ family = "aarch64-qemu-virt"
phys-memory-base = "0x4000_0000"
# Size of the whole physical memory.
# phys-memory-size = "0x800_0000" # 128M
# phys-memory-size = "0x1000_0000" # 1G
# phys-memory-size = "0x4000_0000" # 1G
phys-memory-size = "0x8000_0000" # 2G
# phys-memory-size = "0xc000_0000" # 3G
# phys-memory-size = "0x1_0000_0000" # 4G
Expand Down
6 changes: 6 additions & 0 deletions platforms/riscv64-qemu-virt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ family = "riscv64-qemu-virt"
# Base address of the whole physical memory.
phys-memory-base = "0x8000_0000"
# Size of the whole physical memory.
# Size of the whole physical memory.
# phys-memory-size = "0x800_0000" # 128M
# phys-memory-size = "0x4000_0000" # 1G
phys-memory-size = "0x8000_0000" # 2G
# phys-memory-size = "0xc000_0000" # 3G
# phys-memory-size = "0x1_0000_0000" # 4G
# phys-memory-size = "0x1_8000_0000" # 6G
# phys-memory-size = "0x2_0000_0000" # 8G
# Base physical address of the kernel image.
kernel-base-paddr = "0x8020_0000"
# Base virtual address of the kernel image.
Expand Down
2 changes: 2 additions & 0 deletions scripts/make/build_c.mk
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ app-objs := $(addprefix $(APP)/,$(app-objs))

$(ulib_hdr): _cargo_build

$(app-objs): $(ulib_hdr) prebuild

$(APP)/%.o: $(APP)/%.c $(ulib_hdr)
$(call run_cmd,$(CC),$(CFLAGS) $(APP_CFLAGS) -c -o $@ $<)

Expand Down
17 changes: 12 additions & 5 deletions scripts/make/build_musl.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ rust_lib := target/$(TARGET)/$(MODE)/lib$(rust_lib_name).a

musl_version := 1.2.3

build_suffix := _$(ARCH)_$(MODE)
muslibc_dir := ulib/ruxmusl
build_dir := $(muslibc_dir)/build_musl_$(ARCH)
build_dir := $(muslibc_dir)/build_musl$(build_suffix)
install_dir_name := install$(build_suffix)
install_dir := $(muslibc_dir)/$(install_dir_name)

musl_dir := $(muslibc_dir)/musl-$(musl_version)
inc_dir := $(muslibc_dir)/install/include
c_lib := $(muslibc_dir)/install/lib/libc.a
inc_dir := $(install_dir)/include
c_lib := $(install_dir)/lib/libc.a

libgcc :=

CFLAGS += -nostdinc -fno-builtin -ffreestanding -Wall
Expand Down Expand Up @@ -42,14 +47,14 @@ else
endif

build_musl:
ifeq ($(wildcard $(build_dir)),)
ifeq ($(wildcard $(install_dir)),)
ifeq ($(wildcard $(musl_dir)),)
@echo "Download musl-1.2.3 source code"
wget https://musl.libc.org/releases/musl-1.2.3.tar.gz -P $(muslibc_dir)
tar -zxvf $(muslibc_dir)/musl-1.2.3.tar.gz -C $(muslibc_dir) && rm -f $(muslibc_dir)/musl-1.2.3.tar.gz
endif
mkdir -p $(build_dir)
cd $(build_dir) && ../musl-1.2.3/configure --prefix=../install --exec-prefix=../ --syslibdir=../install/lib --disable-shared ARCH=$(RUX_ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) CFLAGS='$(CFLAGS)'
cd $(build_dir) && ../musl-1.2.3/configure --prefix=../$(install_dir_name) --exec-prefix=../ --syslibdir=../$(install_dir_name)/lib --disable-shared ARCH=$(RUX_ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) CFLAGS='$(CFLAGS)'
cd $(build_dir) && $(MAKE) -j && $(MAKE) install
endif

Expand All @@ -61,6 +66,8 @@ app-objs := main.o

app-objs := $(addprefix $(APP)/,$(app-objs))

$(app-objs): build_musl prebuild

$(APP)/%.o: $(APP)/%.c build_musl
$(call run_cmd,$(CC),$(CFLAGS) $(APP_CFLAGS) -c -o $@ $<)

Expand Down
12 changes: 12 additions & 0 deletions scripts/prebuild/riscv64.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# non-musl riscv64 still needs a usable bios
# instead of the non-funtioning default one
define run_prebuild
git submodule update --init --recursive --remote patches/opensbi
endef

RISCV_BIOS := $(CURDIR)/patches/opensbi/build/platform/generic/firmware/fw_dynamic.bin

$(RISCV_BIOS): prebuild
CROSS_COMPILE=riscv64-linux-musl- $(MAKE) -C patches/opensbi PLATFORM=generic

build: $(RISCV_BIOS)
1 change: 1 addition & 0 deletions ulib/ruxmusl/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build_*
install
install_*
musl-*
3 changes: 3 additions & 0 deletions ulib/ruxmusl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ cfg_if::cfg_if! {
mod x86_64;
#[cfg(feature = "musl")]
use x86_64::{syscall, syscall_id};
} else if #[cfg(target_arch = "riscv64")]{
mod riscv64;
use riscv64::{syscall, syscall_id};
} else {
mod dummy;
use dummy::{syscall, syscall_id};
Expand Down
Loading
Loading