Skip to content

[WIP] feat: la64 boot #1132

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
58 changes: 26 additions & 32 deletions .github/workflows/makefile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

strategy:
matrix:
arch: [x86_64, riscv64]
arch: [x86_64, riscv64, loongarch64]

steps:
- run: echo "Running in dragonos/dragonos-dev:v1.9"
Expand All @@ -39,7 +39,7 @@ jobs:

strategy:
matrix:
arch: [x86_64, riscv64]
arch: [x86_64, riscv64, loongarch64]

steps:
- run: echo "Running in dragonos/dragonos-dev:v1.9"
Expand All @@ -53,46 +53,40 @@ jobs:
HOME: /root
run: bash -c "source /root/.cargo/env && cd kernel && make test && make test-rbpf"

build-x86_64:
build:
name: Build ${{ matrix.arch }}
runs-on: ubuntu-latest
container: dragonos/dragonos-dev:v1.9
strategy:
matrix:
include:
- arch: x86_64
make_target: all
checkout_params: {}
- arch: riscv64
make_target: all
checkout_params:
submodules: "recursive"
- arch: loongarch64
make_target: all
checkout_params: {}

steps:
- run: echo "Running in dragonos/dragonos-dev:v1.9"

- uses: actions/checkout@v3
- name: build the DragonOS
with: ${{ matrix.checkout_params }}

- name: Build the DragonOS
env:
ARCH: x86_64
ARCH: ${{ matrix.arch }}
HOME: /root
shell: bash -ileo pipefail {0}

run: |
source ~/.bashrc
source ~/.cargo/env
export DragonOS_GCC=$HOME/opt/dragonos-gcc/gcc-x86_64-unknown-none/bin
sed -i 's/arch = ".*"/arch = "${{ env.ARCH }}"/' dadk-manifest.toml

make all -j $(nproc)

build-riscv64:
runs-on: ubuntu-latest
container: dragonos/dragonos-dev:v1.9

steps:
- run: echo "Running in dragonos/dragonos-dev:v1.9"

- uses: actions/checkout@v3
with:
submodules: "recursive"

- name: build the DragonOS
shell: bash -ileo pipefail {0}
env:
ARCH: riscv64
HOME: /root

run: |
source ~/.bashrc && source ~/.cargo/env
if [[ "$ARCH" == "x86_64" ]]; then
export DragonOS_GCC=$HOME/opt/dragonos-gcc/gcc-x86_64-unknown-none/bin
fi
sed -i 's/arch = ".*"/arch = "${{ env.ARCH }}"/' dadk-manifest.toml
make kernel -j $(nproc)
make ${{ matrix.make_target }} -j $(nproc)
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
//"./tools/Cargo.toml",

],
// "rust-analyzer.cargo.target": "loongarch64-unknown-none",
// "rust-analyzer.cargo.target": "riscv64gc-unknown-none-elf",
"rust-analyzer.cargo.target": "x86_64-unknown-none",
"rust-analyzer.check.overrideCommand": [
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ clean-docs:
gdb:
ifeq ($(ARCH), x86_64)
rust-gdb -n -x tools/.gdbinit
else ifeq ($(ARCH), loongarch64)
loongarch64-unknown-linux-gnu-gdb -n -x tools/.gdbinit
else
gdb-multiarch -n -x tools/.gdbinit
endif
Expand Down
10 changes: 10 additions & 0 deletions build-scripts/kernel_build/src/bindgen/arch/loongarch64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use super::BindgenArch;

pub struct LoongArch64BindgenArch;
impl BindgenArch for LoongArch64BindgenArch {
fn generate_bindings(&self, builder: bindgen::Builder) -> bindgen::Builder {
builder
.clang_arg("-I./src/arch/loongarch64/include")
.clang_arg("--target=x86_64-none-none") // 由于clang不支持loongarch64,所以使用x86_64作为目标,按理来说问题不大
}
}
2 changes: 2 additions & 0 deletions build-scripts/kernel_build/src/bindgen/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::utils::cargo_handler::{CargoHandler, TargetArch};

use self::x86_64::X86_64BindgenArch;

pub mod loongarch64;
pub mod riscv64;
pub mod x86_64;

Expand All @@ -15,6 +16,7 @@ pub(super) fn current_bindgenarch() -> &'static dyn BindgenArch {
match arch {
TargetArch::X86_64 => &X86_64BindgenArch,
TargetArch::Riscv64 => &riscv64::RiscV64BindgenArch,
TargetArch::LoongArch64 => &loongarch64::LoongArch64BindgenArch,
_ => panic!("Unsupported arch: {:?}", arch),
}
}
40 changes: 40 additions & 0 deletions build-scripts/kernel_build/src/cfiles/arch/loongarch64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::{collections::HashSet, path::PathBuf};

use crate::{constant::ARCH_DIR_LOONGARCH64, utils::FileUtils};

use super::CFilesArch;

pub(super) struct LoongArch64CFilesArch;

impl CFilesArch for LoongArch64CFilesArch {
fn setup_defines(&self, c: &mut cc::Build) {
c.define("__loongarch64__", None);
c.define("__loongarch", None);
}

fn setup_global_include_dir(&self, include_dirs: &mut HashSet<PathBuf>) {
include_dirs.insert("src/arch/loongarch64/include".into());
}

fn setup_files(&self, _c: &mut cc::Build, files: &mut HashSet<PathBuf>) {
// files.insert(PathBuf::from("src/arch/loongarch64/asm/head.S"));

FileUtils::list_all_files(&arch_path("asm"), Some("c"), true)
.into_iter()
.for_each(|f| {
files.insert(f);
});
}

fn setup_global_flags(&self, c: &mut cc::Build) {
// 在这里设置编译器,不然的话vscode的rust-analyzer会报错
c.compiler("loongarch64-unknown-linux-gnu-gcc");
c.flag("-mcmodel=normal");

c.flag("-march=loongarch64");
}
}

fn arch_path(relative_path: &str) -> PathBuf {
PathBuf::from(format!("{}/{}", ARCH_DIR_LOONGARCH64, relative_path))
}
3 changes: 3 additions & 0 deletions build-scripts/kernel_build/src/cfiles/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::utils::cargo_handler::{CargoHandler, TargetArch};

use self::x86_64::X86_64CFilesArch;

pub mod loongarch64;
pub mod riscv64;
pub mod x86_64;

Expand All @@ -27,6 +28,8 @@ pub(super) fn current_cfiles_arch() -> &'static dyn CFilesArch {
match arch {
TargetArch::X86_64 => &X86_64CFilesArch,
TargetArch::Riscv64 => &riscv64::RiscV64CFilesArch,
TargetArch::LoongArch64 => &loongarch64::LoongArch64CFilesArch,

_ => panic!("Unsupported arch: {:?}", arch),
}
}
1 change: 1 addition & 0 deletions build-scripts/kernel_build/src/constant/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub const ARCH_DIR_X86_64: &str = "src/arch/x86_64";
pub const ARCH_DIR_RISCV64: &str = "src/arch/riscv64";
pub const ARCH_DIR_LOONGARCH64: &str = "src/arch/loongarch64";
2 changes: 2 additions & 0 deletions build-scripts/kernel_build/src/utils/cargo_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub enum TargetArch {
Riscv64,
Mips64,
Powerpc64,
LoongArch64,
S390x,
Sparc64,
Unknown,
Expand All @@ -85,6 +86,7 @@ impl TargetArch {
"riscv64" => TargetArch::Riscv64,
"mips64" => TargetArch::Mips64,
"powerpc64" => TargetArch::Powerpc64,
"loongarch64" => TargetArch::LoongArch64,
"s390x" => TargetArch::S390x,
"sparc64" => TargetArch::Sparc64,
_ => TargetArch::Unknown,
Expand Down
2 changes: 1 addition & 1 deletion dadk-manifest.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# DADK 总控文件

[metadata]
# Target architecture. Options: x86_64, riscv64
# Target architecture. Options: x86_64, riscv64, loongarch64
arch = "x86_64"

# Hypervisor config path
Expand Down
3 changes: 2 additions & 1 deletion env.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

ifeq ($(ARCH), )
# !!!!在这里设置ARCH,可选 x86_64 和 riscv64
# !!!!在这里设置ARCH,可选:
# x86_64, riscv64, loongarch64
# !!!!!!!如果不同时调整这里以及vscode的settings.json,那么自动补全和检查将会失效
export ARCH?=x86_64
endif
Expand Down
31 changes: 11 additions & 20 deletions kernel/Cargo.lock

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

22 changes: 15 additions & 7 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,14 @@ kprobe = { path = "crates/kprobe" }
lru = "0.12.3"

rbpf = { path = "crates/rbpf" }
printf-compat = { version = "0.1.1", default-features = false }
printf-compat = { git = "https://git.mirrors.dragonos.org.cn/DragonOS-Community/printf-compat", rev = "5f5c9cc363", default-features = false }

static-keys = "=0.6.1"
unwinding = { version = "=0.2.3", default-features = false, features = [
"unwinder",
"fde-gnu-eh-frame-hdr",
"panic",
"personality",
] }

defer = "0.2.1"
cfg-if = { version = "1.0.0" }


# target为x86_64时,使用下面的依赖
[target.'cfg(target_arch = "x86_64")'.dependencies]
multiboot2 = { git = "https://git.mirrors.dragonos.org.cn/DragonOS-Community/multiboot2", rev = "05739aab40" }
Expand All @@ -100,6 +96,18 @@ riscv = { git = "https://git.mirrors.dragonos.org.cn/DragonOS-Community/riscv.gi
] }
sbi-rt = { version = "=0.0.3", features = ["legacy"] }

# target为loongarch64时,使用下面的依赖
[target.'cfg(target_arch = "loongarch64")'.dependencies]
loongArch64 = { git = "https://github.com/fslongjin/loongArch64", rev = "7af150e" }

# 由于unwinding库不支持loongarch64架构,因此需要排除该依赖项
[target.'cfg(not(target_arch = "loongarch64"))'.dependencies]
unwinding = { version = "=0.2.3", default-features = false, features = [
"unwinder",
"fde-gnu-eh-frame-hdr",
"panic",
"personality",
] }

# 构建时依赖项
[build-dependencies]
Expand Down
4 changes: 4 additions & 0 deletions kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ ifeq ($(ARCH), x86_64)
export TARGET_JSON=arch/x86_64/x86_64-unknown-none.json
else ifeq ($(ARCH), riscv64)
export TARGET_JSON=arch/riscv64/riscv64gc-unknown-none-elf.json
else ifeq ($(ARCH), loongarch64)
export TARGET_JSON=arch/loongarch64/loongarch64-unknown-none.json
else
$(error "Unsupported ARCH: $(ARCH)")
endif

export CARGO_ZBUILD=-Z build-std=core,alloc,compiler_builtins -Z build-std-features=compiler-builtins-mem
Expand Down
2 changes: 2 additions & 0 deletions kernel/crates/kprobe/src/arch/loongarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl KprobeBuilder {
};
let inst_tmp_ptr = point.inst_tmp.as_ptr() as usize;
let inst_32 = unsafe { core::ptr::read(address as *const u32) };

unsafe {
core::ptr::write(address as *mut u32, EBREAK_INST);
// inst_32 :0-32
Expand All @@ -90,6 +91,7 @@ impl KprobeBuilder {
self.symbol,
inst_32
);
Arc::new(point)
}
}

Expand Down
6 changes: 6 additions & 0 deletions kernel/env.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ ifeq ($(ARCH), x86_64)
CCPREFIX=x86_64-linux-gnu-
else ifeq ($(ARCH), riscv64)
CCPREFIX=riscv64-linux-gnu-
else ifeq ($(ARCH), loongarch64)
CCPREFIX=loongarch64-unknown-linux-gnu-
else
$(error "Unsupported ARCH: $(ARCH)")
endif

export CC=$(CCPREFIX)gcc
Expand All @@ -24,6 +28,8 @@ ifeq ($(ARCH), x86_64)
GLOBAL_CFLAGS += -mcmodel=large -m64
else ifeq ($(ARCH), riscv64)
GLOBAL_CFLAGS += -mcmodel=medany -march=rv64gc -mabi=lp64d
else ifeq ($(ARCH), loongarch64)
GLOBAL_CFLAGS += -mcmodel=large -march=loongarch64
endif

ifeq ($(DEBUG), DEBUG)
Expand Down
Loading