Skip to content

Commit

Permalink
patch: patch for loongarch64 temporarily
Browse files Browse the repository at this point in the history
  • Loading branch information
yfblock authored and Azure-stars committed Feb 11, 2025
1 parent 0775001 commit fc898df
Show file tree
Hide file tree
Showing 16 changed files with 1,914 additions and 30 deletions.
1,743 changes: 1,743 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,25 @@ num_enum = { version = "0.7", default-features = false }
syscalls = { version = "0.6", default-features = false }

axconfig = { git = "https://github.com/arceos-org/arceos.git" }
axfs = { git = "https://github.com/arceos-org/arceos.git", features = ["thread-local"] }
axfs = { git = "https://github.com/arceos-org/arceos.git" }
axstd = { git = "https://github.com/arceos-org/arceos.git", features = ["paging"] }
axhal = { git = "https://github.com/arceos-org/arceos.git", features = ["uspace"] }
axmm = { git = "https://github.com/arceos-org/arceos.git" }
axtask = { git = "https://github.com/arceos-org/arceos.git" }
axsync = { git = "https://github.com/arceos-org/arceos.git" }
axruntime = { git = "https://github.com/arceos-org/arceos.git", features = ["multitask"] }
arceos_posix_api = { git = "https://github.com/arceos-org/arceos.git", features = ["uspace"] }
arceos_posix_api = { git = "https://github.com/arceos-org/arceos.git", features = ["uspace", "smp", "irq", "fs", "multitask", "net", "pipe", "select", "epoll"] }
axns = { git = "https://github.com/arceos-org/arceos.git", features = ["thread-local"] }

[patch.crates-io]
syscalls = { git = "https://github.com/jasonwhite/syscalls.git", rev = "92624de"}
# kernel-elf-parser = { path = "../kernel-elf-parser" }
kernel-elf-parser = { git = "https://github.com/yfblock/kernel-elf-parser", rev = "d00e14e"}
page_table_multiarch = { git = "https://github.com/yfblock/page_table_multiarch.git", rev = "c8a13b0"}
page_table_entry = { git = "https://github.com/yfblock/page_table_multiarch.git", rev = "c8a13b0" }
# page_table_multiarch = { path = "../page_table_multiarch/page_table_multiarch" }
# page_table_entry = { path = "../page_table_multiarch/page_table_entry" }

[target.'cfg(target_arch = "x86_64")'.dependencies]
x86 = "0.52"

Expand Down
76 changes: 71 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,82 @@
A monolithic kernel based on [ArceOS](https://github.com/arceos-org/arceos).

## Quick Start
```sh

### 1. Install Build Dependencies

Install [cargo-binutils](https://github.com/rust-embedded/cargo-binutils) to use `rust-objcopy` and `rust-objdump` tools:

```bash
cargo install cargo-binutils
```

#### Dependencies for C apps

Install `libclang-dev`:

```bash
sudo apt install libclang-dev
```

Download & install [musl](https://musl.cc) toolchains:

```bash
# download
wget https://musl.cc/aarch64-linux-musl-cross.tgz
wget https://musl.cc/riscv64-linux-musl-cross.tgz
wget https://musl.cc/x86_64-linux-musl-cross.tgz
# install
tar zxf aarch64-linux-musl-cross.tgz
tar zxf riscv64-linux-musl-cross.tgz
tar zxf x86_64-linux-musl-cross.tgz
# exec below command in bash OR add below info in ~/.bashrc
export PATH=`pwd`/x86_64-linux-musl-cross/bin:`pwd`/aarch64-linux-musl-cross/bin:`pwd`/riscv64-linux-musl-cross/bin:$PATH
```

#### Dependencies for running apps

```bash
# for Debian/Ubuntu
sudo apt-get install qemu-system
```

```bash
# for macos
brew install qemu
```

Notice: The version of `qemu` should **be no less than 8.2.0**.

Other systems, arch and version please refer to [Qemu Download](https://www.qemu.org/download/#linux)

### 2. Build & Run

```bash
# Clone the base repository
./scripts/get_deps.sh

# Build user applications
make user_apps
make user_apps ARCH=<arch> AX_TESTCASE=<testcases>

# Build kernel
make ARCH=x86_64 build
make ARCH=<arch> LOG=<log> AX_TESTCASE=<testcases> build

# Run kernel
make ARCH=x86_64 run
```
make ARCH=<arch> LOG=<log> AX_TESTCASE=<testcases> run
```

Where `testcases` are shown under the `apps/` folder.

`<arch>` should be one of `riscv64`, `aarch64`, `x86_64`.

`<log>` should be one of `off`, `error`, `warn`, `info`, `debug`, `trace`.

More arguments and targets can be found in [Makefile](./Makefile).

For example, to run the [nimbos testcases](apps/nimbos/) on `qemu-system-x86_64` with log level `info`:

```bash
make ARCH=x86_64 LOG=info AX_TESTCASE=nimbos run
```

Note: Arguments like `NET`, `BLK`, and `GRAPHIC` enable devices in QEMU, which take effect only at runtime, not at build time.
18 changes: 13 additions & 5 deletions apps/libc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ ifeq ($(TARGET),musl)
$(warning "Warn: Rust musl target not supported for riscv64")
RUST_TARGET := ""
RUSTFLAGS := ""
else ifeq ($(ARCH),loongarch64)
RUST_TARGET := loongarch64-unknown-linux-musl
RUSTFLAGS := "-C linker=loongarch-linux-musl-ld"
else
$(error "Unknown ARCH")
endif
Expand All @@ -31,6 +34,8 @@ else ifeq ($(TARGET),gnu)
RUST_TARGET := aarch64-unknown-linux-gnu
else ifeq ($(ARCH),riscv64)
RUST_TARGET := riscv64gc-unknown-linux-gnu
else ifeq ($(ARCH),loongarch64)
RUST_TARGET := loongarch64-unknown-linux-gnu
else
$(error "Unknown ARCH")
endif
Expand All @@ -47,11 +52,14 @@ build_dir:
@mkdir -p build/$(ARCH)

build_c:
@for app in $(wildcard c/*/*.c); do \
echo "Building $${app%.c}"; \
app_name=$$(basename $$(dirname $${app})); \
$(CC) -o build/$(ARCH)/$${app_name}_c $${app} $(CFLAGS); \
done
# No build for loongarch64
if [ "$(ARCH)" != "loongarch64" ]; then \
for app in $(wildcard c/*/*.c); do \
echo "Building $${app%.c}"; \
app_name=$$(basename $$(dirname $${app})); \
$(CC) -o build/$(ARCH)/$${app_name}_c $${app} $(CFLAGS); \
done \
fi

build_rust:
if [ -n $(RUST_TARGET) ]; then \
Expand Down
8 changes: 7 additions & 1 deletion apps/nimbos/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ ARCH ?= x86_64
MODE ?= release

BUILD_RUST ?= y
BUILD_C ?= y

ifeq ($(ARCH),loongarch64)
# No build C for loongarch64
BUILD_C := n
else
BUILD_C := y
endif

build_dir := build/$(ARCH)

Expand Down
2 changes: 2 additions & 0 deletions apps/nimbos/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ elseif (${ARCH} STREQUAL riscv64)
set(CMAKE_C_FLAGS "-march=rv64imac -mabi=lp64 -mcmodel=medany")
elseif (${ARCH} STREQUAL aarch64)
set(CMAKE_C_FLAGS "-mgeneral-regs-only")
elseif (${ARCH} STREQUAL loongarch64)
set(CMAKE_C_FLAGS "-march=loongarch64")
else()
message("Unsupported arch: ${ARCH}")
endif ()
Expand Down
2 changes: 2 additions & 0 deletions apps/nimbos/rust/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ else ifeq ($(ARCH), aarch64)
target := aarch64-unknown-none-softfloat
else ifeq ($(ARCH), riscv64)
target := riscv64imac-unknown-none-elf
else ifeq ($(ARCH), loongarch64)
target := loongarch64-unknown-none
else
$(error Unsupported architecture "$(ARCH)")
endif
Expand Down
24 changes: 24 additions & 0 deletions apps/nimbos/rust/src/arch/loongarch64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use core::arch::asm;

pub fn syscall(id: usize, args: [usize; 3]) -> isize {
let ret;
unsafe {
asm!(
"syscall 0",
inlateout("$r4") args[0] => ret,
in("$r5") args[1],
in("$r6") args[2],
in("$r11") id,
);
}
ret
}

#[allow(improper_ctypes_definitions)]
pub extern "C" fn sys_clone(_entry: fn(usize) -> i32, _arg: usize, _newsp: usize) -> isize {
// sys_clone(entry, arg, newsp)
// a0, a1, a2
// syscall(SYSCALL_CLONE, newsp)
// a7, x0
unimplemented!()
}
4 changes: 4 additions & 0 deletions apps/nimbos/rust/src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ mod aarch64;
mod x86_64;
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
mod riscv;
#[cfg(target_arch = "loongarch64")]
mod loongarch64;

#[cfg(target_arch = "aarch64")]
pub use aarch64::*;
#[cfg(target_arch = "x86_64")]
pub use x86_64::*;
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
pub use riscv::*;
#[cfg(target_arch = "loongarch64")]
pub use loongarch64::*;
2 changes: 2 additions & 0 deletions apps/nimbos/rust/src/bin/thread_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ fn get_sp() -> usize {
core::arch::asm!("mov {}, sp", out(reg) sp, options(pure, readonly));
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
core::arch::asm!("mv {}, sp", out(reg) sp, options(pure, readonly));
#[cfg(target_arch = "loongarch64")]
core::arch::asm!("move {}, $r3", out(reg) sp, options(pure, readonly));
}
sp
}
Expand Down
28 changes: 16 additions & 12 deletions apps/nimbos/rust/src/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cfg_if::cfg_if! {
pub const SYSCALL_WRITE: usize = 64;
pub const SYSCALL_YIELD: usize = 124;
pub const SYSCALL_GETPID: usize = 172;
#[allow(dead_code)]
pub const SYSCALL_CLONE: usize = 220;
pub const SYSCALL_FORK: usize = 220;
pub const SYSCALL_EXEC: usize = 221;
Expand All @@ -33,10 +34,11 @@ cfg_if::cfg_if! {
}

pub fn sys_read(fd: usize, buffer: &mut [u8]) -> isize {
syscall(
SYSCALL_READ,
[fd, buffer.as_mut_ptr() as usize, buffer.len()],
)
syscall(SYSCALL_READ, [
fd,
buffer.as_mut_ptr() as usize,
buffer.len(),
])
}

pub fn sys_write(fd: usize, buffer: &[u8]) -> isize {
Expand Down Expand Up @@ -65,19 +67,21 @@ pub fn sys_exec(path: &str) -> isize {
}

pub fn sys_waitpid(pid: isize, exit_code: *mut i32, options: u32) -> isize {
syscall(
SYSCALL_WAITPID,
[pid as usize, exit_code as _, options as _],
)
syscall(SYSCALL_WAITPID, [
pid as usize,
exit_code as _,
options as _,
])
}

pub fn sys_clock_gettime(clk: ClockId, req: &mut TimeSpec) -> isize {
syscall(SYSCALL_CLOCK_GETTIME, [clk as _, req as *mut _ as usize, 0])
}

pub fn sys_clock_nanosleep(clk: ClockId, flags: u32, req: &TimeSpec) -> isize {
syscall(
SYSCALL_CLOCK_NANOSLEEP,
[clk as _, flags as _, req as *const _ as usize],
)
syscall(SYSCALL_CLOCK_NANOSLEEP, [
clk as _,
flags as _,
req as *const _ as usize,
])
}
4 changes: 1 addition & 3 deletions apps/nimbos/testcase_list
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
hello_world
hello_c
yield
sleep_simple
yield
13 changes: 13 additions & 0 deletions configs/loongarch64.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[plat]
# The base address of the user space.
user-space-base = 0x1000
# The size of the user space.
user-space-size = 0x3f_ffff_f000

# The highest address of the user stack.
user-stack-top = 0x4_0000_0000
# The size of the user stack.
user-stack-size = 0x1_0000

# The size of the kernel stack.
kernel-stack-size = 0x40000
3 changes: 2 additions & 1 deletion scripts/get_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
AX_ROOT=.arceos

test ! -d "$AX_ROOT" && echo "Cloning repositories ..." || true
test ! -d "$AX_ROOT" && git clone https://github.com/arceos-org/arceos -b monolithic "$AX_ROOT" --depth=1 || true
# test ! -d "$AX_ROOT" && git clone https://github.com/arceos-org/arceos -b monolithic "$AX_ROOT" --depth=1 || true
test ! -d "$AX_ROOT" && git clone https://github.com/Azure-stars/arceos -b feat_loongarch64 "$AX_ROOT" --depth=1 || true

$(dirname $0)/set_ax_root.sh $AX_ROOT
2 changes: 2 additions & 0 deletions src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ pub(crate) fn load_elf(name: &str, base_addr: VirtAddr) -> ELFInfo {
header::Machine::AArch64
} else if cfg!(target_arch = "riscv64") {
header::Machine::RISC_V
} else if cfg!(target_arch = "loongarch64") {
header::Machine::Other(0x102)
} else {
panic!("Unsupported architecture!");
};
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn main() {
let user_task = task::spawn_user_task(
Arc::new(Mutex::new(uspace)),
UspaceContext::new(entry_vaddr.into(), ustack_top, 2333),
);
);
let exit_code = user_task.join();
info!("User task {} exited with code: {:?}", testcase, exit_code);
}
Expand Down

0 comments on commit fc898df

Please sign in to comment.