diff --git a/Cargo.lock b/Cargo.lock index 264cd4e7..7f256a51 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1463,7 +1463,6 @@ source = "git+https://github.com/os-module/kernel-sync.git#e7e366d8ae91ba6c00870 dependencies = [ "cfg-if", "lock_api", - "riscv", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index e34ea202..46501212 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,34 +1,34 @@ -[workspace] -resolver = "2" -members = [ - "kernel", - "userlib", - "boot", - "dep/gmanager", - "dep/basemachine", - "apps/slint-helper", - "apps/cat", - "apps/sleep", - "apps/todo", - "apps/printdemo", - "apps/egui", - "apps/sysinfo", - "apps/init", - "apps/ls", - "apps/mkdir", - "apps/pwd", - "apps/shell", - "apps/touch", - "apps/run_test", - "apps/slint", - "apps/socket_test", - "apps/tests", - "apps/memory-game", - "apps/print", - "apps/final_test", -] - - -[profile.release] -#lto = true -#codegen-units = 1 +[workspace] +resolver = "2" +members = [ + "kernel", + "userlib", + "boot", + "dep/gmanager", + "dep/basemachine", + "apps/slint-helper", + "apps/cat", + "apps/sleep", + "apps/todo", + "apps/printdemo", + "apps/egui", + "apps/sysinfo", + "apps/init", + "apps/ls", + "apps/mkdir", + "apps/pwd", + "apps/shell", + "apps/touch", + "apps/run_test", + "apps/slint", + "apps/socket_test", + "apps/tests", + "apps/memory-game", + "apps/print", + "apps/final_test", +] + + +[profile.release] +#lto = true +#codegen-units = 1 diff --git a/assert/desktop.bmp b/assert/desktop.bmp deleted file mode 100644 index a754f7a7..00000000 Binary files a/assert/desktop.bmp and /dev/null differ diff --git a/assert/file.bmp b/assert/file.bmp deleted file mode 100644 index 4a4e83bb..00000000 Binary files a/assert/file.bmp and /dev/null differ diff --git a/assert/folder.bmp b/assert/folder.bmp deleted file mode 100644 index c8384cb7..00000000 Binary files a/assert/folder.bmp and /dev/null differ diff --git a/assert/mouse.bmp b/assert/mouse.bmp deleted file mode 100644 index 22a4613c..00000000 Binary files a/assert/mouse.bmp and /dev/null differ diff --git a/docs/doc/musl.md b/docs/doc/musl.md new file mode 100644 index 00000000..47722767 --- /dev/null +++ b/docs/doc/musl.md @@ -0,0 +1,4 @@ +# 编译 riscv64gc-unknown-linux-musl rust程序 + +## 提示找不到运行时库的问题 +https://stackoverflow.com/questions/74424444/how-to-build-a-rust-program-for-riscv64gc-unknown-linux-musl \ No newline at end of file diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 99f2dc88..a4e8a836 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -27,7 +27,7 @@ rslab = { version = "0.2.1", optional = true } syscall-table = { git = "https://github.com/os-module/syscall-table.git" } plic = { git = "https://github.com/os-module/plic" } pager = { git = "https://github.com/os-module/pager", default-features = false, optional = true } -kernel-sync = { git = "https://github.com/os-module/kernel-sync.git",features = ["lock_api","riscv"] } +kernel-sync = { git = "https://github.com/os-module/kernel-sync.git"} pconst = { git = "https://github.com/os-module/pconst.git", features = [ "trick", ] } diff --git a/kernel/src/ksync.rs b/kernel/src/ksync.rs index f912945b..8583dd43 100644 --- a/kernel/src/ksync.rs +++ b/kernel/src/ksync.rs @@ -1,7 +1,84 @@ -use kernel_sync::TicketMutexGuard; +use kernel_sync::{LockAction, ticket::TicketMutexGuard}; +use core::cell::{RefCell, RefMut}; +use crate::arch::{hart_id, interrupt_disable, interrupt_enable, is_interrupt_enable}; +use crate::config::CPU_NUM; -pub type SpinMutex = kernel_sync::SpinMutex; -pub type TicketMutex = kernel_sync::TicketMutex; + +pub type SpinMutex = kernel_sync::spin::SpinMutex; +pub type TicketMutex = kernel_sync::ticket::TicketMutex; pub type RwLock = kernel_sync::RwLock; pub type Mutex = TicketMutex; -pub type MutexGuard<'a, T> = TicketMutexGuard<'a, T>; +pub type MutexGuard<'a, T> = TicketMutexGuard<'a, T, KernelLockAction>; + +#[derive(Debug, Default, Clone, Copy)] +#[repr(align(64))] +pub struct Cpu { + pub noff: i32, // Depth of push_off() nesting. + pub interrupt_enable: bool, // Were interrupts enabled before push_off()? +} + +impl Cpu { + const fn new() -> Self { + Self { + noff: 0, + interrupt_enable: false, + } + } +} + +pub struct SafeRefCell(RefCell); + +/// # Safety: Only the corresponding cpu will access it. +unsafe impl Sync for SafeRefCell {} + +impl SafeRefCell { + const fn new(t: T) -> Self { + Self(RefCell::new(t)) + } +} + +#[allow(clippy::declare_interior_mutable_const)] +const DEFAULT_CPU: SafeRefCell = SafeRefCell::new(Cpu::new()); + +static CPUS: [SafeRefCell; CPU_NUM] = [DEFAULT_CPU; CPU_NUM]; + +pub fn mycpu() -> RefMut<'static, Cpu> { + CPUS[hart_id()].0.borrow_mut() +} + +// push_off/pop_off are like intr_off()/intr_on() except that they are matched: +// it takes two pop_off()s to undo two push_off()s. Also, if interrupts +// are initially off, then push_off, pop_off leaves them off. +pub(crate) fn push_off() { + let old = is_interrupt_enable(); + interrupt_disable(); + let mut cpu = mycpu(); + if cpu.noff == 0 { + cpu.interrupt_enable = old; + } + cpu.noff += 1; +} + +pub(crate) fn pop_off() { + let mut cpu = mycpu(); + if is_interrupt_enable() || cpu.noff < 1 { + panic!("pop_off"); + } + cpu.noff -= 1; + let should_enable = cpu.noff == 0 && cpu.interrupt_enable; + drop(cpu); + // NOTICE: intr_on() may lead to an immediate inerrupt, so we *MUST* drop(cpu) in advance. + if should_enable { + interrupt_enable(); + } +} + +pub struct KernelLockAction; +impl LockAction for KernelLockAction { + fn before_lock() { + push_off(); + } + fn after_lock() { + pop_off(); + } +}