diff --git a/awkernel_lib/Cargo.toml b/awkernel_lib/Cargo.toml index 83a350322..c746e744a 100644 --- a/awkernel_lib/Cargo.toml +++ b/awkernel_lib/Cargo.toml @@ -31,7 +31,7 @@ version = "0.2" optional = true [dependencies.riscv] -version = "0.11.1" +version = "0.16.1" optional = true [dependencies.x86_64] @@ -95,7 +95,7 @@ x86 = [ ] aarch64 = ["dep:awkernel_aarch64", "awkernel_sync/aarch64"] rv32 = ["dep:riscv", "awkernel_sync/rv32"] -rv64 = ["awkernel_sync/rv64"] +rv64 = ["dep:riscv", "awkernel_sync/rv64"] std = ["dep:libc", "dep:socket2", "awkernel_sync/std"] spinlock = ["awkernel_sync/spinlock"] # LFN (Long File Name) support diff --git a/awkernel_lib/src/arch.rs b/awkernel_lib/src/arch.rs index c0d5d56bc..e979f1bdd 100644 --- a/awkernel_lib/src/arch.rs +++ b/awkernel_lib/src/arch.rs @@ -4,6 +4,9 @@ pub mod aarch64; #[cfg(all(feature = "x86", not(feature = "std")))] pub mod x86_64; +#[cfg(all(any(feature = "rv32", feature = "rv64"), not(feature = "std")))] +pub mod rv_common; + #[cfg(all(feature = "rv32", not(feature = "std")))] pub mod rv32; diff --git a/awkernel_lib/src/arch/rv32.rs b/awkernel_lib/src/arch/rv32.rs index 88537139f..8de6bf621 100644 --- a/awkernel_lib/src/arch/rv32.rs +++ b/awkernel_lib/src/arch/rv32.rs @@ -4,7 +4,6 @@ pub(super) mod cpu; pub(super) mod delay; pub(super) mod dvfs; pub(super) mod frame_allocator; -pub(super) mod interrupt; pub(super) mod memory; pub(super) mod page_table; pub(super) mod paging; diff --git a/awkernel_lib/src/arch/rv32/interrupt.rs b/awkernel_lib/src/arch/rv32/interrupt.rs deleted file mode 100644 index a01f0e5ed..000000000 --- a/awkernel_lib/src/arch/rv32/interrupt.rs +++ /dev/null @@ -1,33 +0,0 @@ -use crate::interrupt::Interrupt; - -impl Interrupt for super::RV32 { - fn get_flag() -> usize { - let x: usize; - unsafe { core::arch::asm!("csrr {}, mstatus", out(reg) x) }; - x & 0x08 - } - - fn disable() { - let _x: usize; - unsafe { core::arch::asm!("csrrc {}, mstatus, 0x08", out(reg) _x) }; - } - - fn enable() { - let _x: usize; - unsafe { core::arch::asm!("csrrs {}, mstatus, 0x08", out(reg) _x) }; - } - - fn are_enabled() -> bool { - let x: usize; - unsafe { core::arch::asm!("csrr {}, mstatus", out(reg) x) }; - (x & 0x08) > 0 - } - - fn set_flag(flag: usize) { - if flag & 0x08 > 0 { - Self::enable(); - } else { - Self::disable(); - } - } -} diff --git a/awkernel_lib/src/arch/rv64.rs b/awkernel_lib/src/arch/rv64.rs index e23965343..c121a3a09 100644 --- a/awkernel_lib/src/arch/rv64.rs +++ b/awkernel_lib/src/arch/rv64.rs @@ -4,7 +4,6 @@ pub(super) mod cpu; pub(super) mod delay; pub(super) mod dvfs; pub(super) mod frame_allocator; -pub(super) mod interrupt; pub(super) mod page_table; pub(super) mod paging; pub(super) mod vm; diff --git a/awkernel_lib/src/arch/rv64/interrupt.rs b/awkernel_lib/src/arch/rv64/interrupt.rs deleted file mode 100644 index 72ade36d8..000000000 --- a/awkernel_lib/src/arch/rv64/interrupt.rs +++ /dev/null @@ -1,33 +0,0 @@ -use crate::interrupt::Interrupt; - -impl Interrupt for super::RV64 { - fn get_flag() -> usize { - let x: usize; - unsafe { core::arch::asm!("csrr {}, mstatus", out(reg) x) }; - x & 0x08 - } - - fn disable() { - let _x: usize; - unsafe { core::arch::asm!("csrrc {}, mstatus, 0x08", out(reg) _x) }; - } - - fn enable() { - let _x: usize; - unsafe { core::arch::asm!("csrrs {}, mstatus, 0x08", out(reg) _x) }; - } - - fn are_enabled() -> bool { - let x: usize; - unsafe { core::arch::asm!("csrr {}, mstatus", out(reg) x) }; - (x & 0x08) > 0 - } - - fn set_flag(flag: usize) { - if flag & 0x08 > 0 { - Self::enable(); - } else { - Self::disable(); - } - } -} diff --git a/awkernel_lib/src/arch/rv_common.rs b/awkernel_lib/src/arch/rv_common.rs new file mode 100644 index 000000000..40fb603e2 --- /dev/null +++ b/awkernel_lib/src/arch/rv_common.rs @@ -0,0 +1,6 @@ +#[cfg(feature = "rv32")] +use super::rv32::RV32 as ArchImpl; +#[cfg(feature = "rv64")] +use super::rv64::RV64 as ArchImpl; + +pub(super) mod interrupt; diff --git a/awkernel_lib/src/arch/rv_common/interrupt.rs b/awkernel_lib/src/arch/rv_common/interrupt.rs new file mode 100644 index 000000000..74211b8b2 --- /dev/null +++ b/awkernel_lib/src/arch/rv_common/interrupt.rs @@ -0,0 +1,31 @@ +use super::ArchImpl; +use crate::interrupt::Interrupt; +use riscv::register::mstatus; + +impl Interrupt for ArchImpl { + #[inline] + fn get_flag() -> usize { + let mie_mask = 0x08; + mstatus::read().bits() & mie_mask + } + #[inline] + fn disable() { + unsafe { mstatus::clear_mie() }; + } + #[inline] + fn enable() { + unsafe { mstatus::set_mie() }; + } + #[inline] + fn are_enabled() -> bool { + mstatus::read().mie() + } + #[inline] + fn set_flag(flag: usize) { + if flag & 0x08 > 0 { + Self::enable(); + } else { + Self::disable(); + } + } +}