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

feat: add basic loongarch64 support #126

Open
wants to merge 19 commits into
base: main
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
actual.out
qemu.log
rusty-tags.vi
.idea
44 changes: 22 additions & 22 deletions Cargo.lock

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

10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,13 @@ else ifeq ($(ARCH), aarch64)
ACCEL ?= n
PLATFORM_NAME ?= aarch64-qemu-virt
TARGET := aarch64-unknown-none-softfloat
else ifeq ($(ARCH), loongarch64)
ACCEL ?= n
PLATFORM_NAME ?= loongarch64-qemu-virt
TARGET := loongarch64-unknown-none
BUS := pci
else
$(error "ARCH" must be one of "x86_64", "riscv64", or "aarch64")
$(error "ARCH" must be one of "x86_64", "riscv64", "aarch64" or "loongarch64")
endif

export AX_ARCH=$(ARCH)
Expand All @@ -118,6 +123,9 @@ export AX_GW=$(GW)

# Binutils
CROSS_COMPILE ?= $(ARCH)-linux-musl-
ifeq ($(ARCH), loongarch64)
CROSS_COMPILE := $(ARCH)-unknown-linux-gnu-
endif
CC := $(CROSS_COMPILE)gcc
AR := $(CROSS_COMPILE)ar
RANLIB := $(CROSS_COMPILE)ranlib
Expand Down
19 changes: 19 additions & 0 deletions apps/exception/expect_debug_loongarch64.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
smp = 1
build_mode = release
log_level = debug

Primary CPU 0 started,
Found physcial memory regions:
.text (READ | EXECUTE | RESERVED)
.rodata (READ | RESERVED)
.data .tdata .tbss .percpu (READ | WRITE | RESERVED)
.percpu (READ | WRITE | RESERVED)
boot stack (READ | WRITE | RESERVED)
.bss (READ | WRITE | RESERVED)
free memory (READ | WRITE | FREE)
Initialize platform devices...
Primary CPU 0 init OK.
Running exception tests...
Exception(Breakpoint) @ 0x[0-9a-f]\{16\}
Exception tests run OK!
Shutting down...
2 changes: 2 additions & 0 deletions apps/exception/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ fn raise_break_exception() {
asm!("brk #0");
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
asm!("ebreak");
#[cfg(any(target_arch = "loongarch64"))]
asm!("break 0");
}
}

Expand Down
18 changes: 18 additions & 0 deletions crates/kernel_guard/src/arch/loongarch64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use core::arch::asm;

/// Bit 2: Supervisor Interrupt Enable
const IE_BIT: usize = 1 << 2;

#[inline]
pub fn local_irq_save_and_disable() -> usize {
let mut flags: usize = 0;
// clear the `IE` bit, and return the old CSR
unsafe { asm!("csrxchg {}, {}, 0x0", inout(reg) flags, in(reg) IE_BIT) };
flags & IE_BIT
}

#[inline]
pub fn local_irq_restore(mut flags: usize) {
// restore the `IE` bit
unsafe { asm!("csrxchg {}, {}, 0x0", inout(reg) flags, in(reg) IE_BIT) };
}
3 changes: 3 additions & 0 deletions crates/kernel_guard/src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ cfg_if::cfg_if! {
} else if #[cfg(target_arch = "aarch64")] {
mod aarch64;
pub use self::aarch64::*;
} else if #[cfg(target_arch = "loongarch64")] {
mod loongarch64;
pub use self::loongarch64::*;
}
}
17 changes: 17 additions & 0 deletions crates/page_table/src/arch/loongarch64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! LoongArch64 specific page table structures.

use crate::{PageTable64, PagingMetaData};

use page_table_entry::loongarch64::LA64PTE;
/// Metadata of LoongArch64 page tables.
#[derive(Copy, Clone, Debug)]
pub struct LA64MetaData;

impl const PagingMetaData for LA64MetaData {
const LEVELS: usize = 4;
const PA_MAX_BITS: usize = 48;
const VA_MAX_BITS: usize = 48;
}

/// Page table for LoongArch64 systems.
pub type LA64PageTable<I> = PageTable64<LA64MetaData, LA64PTE, I>;
3 changes: 3 additions & 0 deletions crates/page_table/src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ pub mod riscv;

#[cfg(any(target_arch = "aarch64", doc))]
pub mod aarch64;

#[cfg(any(target_arch = "loongarch64", doc))]
pub mod loongarch64;
Loading
Loading