Skip to content

Commit

Permalink
fix: use new kernel-sync crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Godones committed Sep 30, 2023
1 parent 2c916e8 commit 64177d6
Show file tree
Hide file tree
Showing 29 changed files with 48 additions and 47 deletions.
2 changes: 0 additions & 2 deletions boot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ use kernel::interrupt::init_plic;
use kernel::memory::{init_memory_system, kernel_info};
use kernel::print::init_print;
use kernel::sbi::hart_start;
use kernel::task::init_per_cpu;
use kernel::{config, init_machine_info, println, task, thread_local_init, timer, trap};

mod entry;
Expand Down Expand Up @@ -102,7 +101,6 @@ pub fn main(_: usize, _: usize) -> ! {
// init all device
init_device();
trap::init_trap_subsystem();
init_per_cpu();
init_vfs();
task::init_process();
CPUS.fetch_add(1, Ordering::Release);
Expand Down
3 changes: 1 addition & 2 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ 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 }
gmanager = { path = "../modules/gmanager" }
kernel-sync = { path = "../../os-modules/kernel-sync" }

kernel-sync = { git = "https://github.com/os-module/kernel-sync.git" }
syscall_define = { git = "https://github.com/os-module/pconst.git", package = "pconst" }
basemachine = { path = "../modules/basemachine" }
uart16550 = { version = "0.0.1", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/board/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloc::collections::BTreeMap;

use cfg_if::cfg_if;

use kernel_sync::Mutex;
use crate::ksync::Mutex;

use crate::device::{DeviceInfo, DeviceType};

Expand Down
2 changes: 1 addition & 1 deletion kernel/src/driver/block_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rvfs::superblock::Device;
use virtio_drivers::device::blk::VirtIOBlk;
use virtio_drivers::transport::mmio::{MmioTransport, VirtIOHeader};

use kernel_sync::Mutex;
use crate::ksync::Mutex;

use crate::config::BLOCK_CACHE_FRAMES;
use crate::config::FRAME_SIZE;
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/driver/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tinybmp::Bmp;
use virtio_drivers::device::gpu::VirtIOGpu;
use virtio_drivers::transport::mmio::{MmioTransport, VirtIOHeader};

use kernel_sync::Mutex;
use crate::ksync::Mutex;

use crate::device::GpuDevice;
use crate::driver::hal::HalImpl;
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/driver/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::ptr::NonNull;
use virtio_drivers::device::input::VirtIOInput;
use virtio_drivers::transport::mmio::{MmioTransport, VirtIOHeader};

use kernel_sync::Mutex;
use crate::ksync::Mutex;
use smpscheduler::FifoTask;

use crate::device::InputDevice;
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/driver/uart.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ksync::Mutex;
use alloc::boxed::Box;
use alloc::collections::VecDeque;
use alloc::sync::Arc;
use kernel_sync::Mutex;
use smpscheduler::FifoTask;

#[cfg(not(feature = "vf2"))]
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::ops::{Deref, DerefMut};

use rvfs::file::{File, OpenFlags};

use kernel_sync::{Mutex, MutexGuard};
use crate::ksync::{Mutex, MutexGuard};

use crate::net::socket::SocketData;
use crate::timer::TimeSpec;
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/fs/vfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rvfs::mount_rootfs;
use rvfs::ramfs::tmpfs::TMP_FS_TYPE;
use rvfs::superblock::{register_filesystem, DataOps, Device};

use kernel_sync::Mutex;
use crate::ksync::Mutex;

use crate::config::{INTERRUPT_RECORD, MEMINFO, PASSWORD, RTC_TIME, UTC};
use crate::device::{get_rtc_time, BLOCK_DEVICE};
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/interrupt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use cfg_if::cfg_if;
use lazy_static::lazy_static;
use spin::Once;

use crate::ksync::Mutex;
pub use ext_interrupt::external_interrupt_handler;
use kernel_sync::Mutex;
use plic::{Mode, PLIC};

use crate::arch::hart_id;
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/ipc/futex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use alloc::vec;
use alloc::vec::Vec;
use core::cmp::min;

use kernel_sync::Mutex;
use crate::ksync::Mutex;
use smpscheduler::FifoTask;

use crate::error::{AlienError, AlienResult};
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/ipc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::sync::atomic::{AtomicI32, Ordering};

use lazy_static::lazy_static;

use kernel_sync::Mutex;
use crate::ksync::Mutex;
pub use pipe::*;
pub use shm::*;
pub use signal::*;
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/ipc/shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use alloc::vec::Vec;
use page_table::addr::{align_down_4k, PhysAddr, VirtAddr};
use page_table::table::PageSize;

use kernel_sync::{Mutex, MutexGuard};
use crate::ksync::{Mutex, MutexGuard};
use syscall_define::ipc::{ShmAtFlags, ShmCtlCmd, ShmGetFlags, IPC_PRIVATE};
use syscall_define::LinuxErrno;
use syscall_table::syscall_func;
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/ipc/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use alloc::sync::Arc;
use alloc::vec::Vec;
use core::mem::size_of;

use kernel_sync::Mutex;
use crate::ksync::Mutex;
use syscall_define::signal::{
SigAction, SigActionDefault, SigActionFlags, SigInfo, SigProcMaskHow, SignalNumber,
SignalReceivers, SignalUserContext, SimpleBitSet,
Expand Down
7 changes: 7 additions & 0 deletions kernel/src/ksync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use kernel_sync::TicketMutexGuard;

pub type SpinMutex<T> = kernel_sync::SpinMutex<T>;
pub type TicketMutex<T> = kernel_sync::TicketMutex<T>;
pub type RwLock<T> = kernel_sync::RwLock<T>;
pub type Mutex<T> = TicketMutex<T>;
pub type MutexGuard<'a, T> = TicketMutexGuard<'a, T>;
1 change: 1 addition & 0 deletions kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub mod fs;
pub mod gui;
pub mod interrupt;
pub mod ipc;
pub mod ksync;
pub mod memory;
pub mod net;
pub mod panic;
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/memory/frame.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::manager::FrameRefManager;
use crate::config::{FRAME_BITS, FRAME_SIZE};
use crate::ksync::Mutex;
use alloc::format;
use alloc::vec::Vec;
use core::ops::{Deref, DerefMut};
use kernel_sync::Mutex;
use pager::{PageAllocator, PageAllocatorExt};
use spin::Lazy;

Expand Down
2 changes: 1 addition & 1 deletion kernel/src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub use rslab::*;
#[cfg(feature = "talloc")]
use talc::{Talc, Talck};

use crate::ksync::Mutex;
pub use frame::*;
use kernel_sync::Mutex;
pub use map::*;
use syscall_table::syscall_func;
pub use vmm::*;
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/memory/vmm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use page_table::pte::MappingFlags;
use page_table::table::{PagingIf, Sv39PageTable};
use xmas_elf::program::{SegmentData, Type};

use kernel_sync::RwLock;
use crate::ksync::RwLock;

use crate::config::{
ELF_BASE_RELOCATE, FRAME_BITS, FRAME_SIZE, MMIO, TRAMPOLINE, TRAP_CONTEXT_BASE, USER_STACK_SIZE,
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/print/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::sync::atomic::{AtomicBool, Ordering};

use preprint::Print;

use kernel_sync::Mutex;
use crate::ksync::Mutex;

use crate::device::UART_DEVICE;
use crate::sbi::console_putchar;
Expand Down
30 changes: 14 additions & 16 deletions kernel/src/task/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use alloc::string::{String, ToString};
use alloc::sync::Arc;
use alloc::vec;
use alloc::vec::Vec;
use core::cell::UnsafeCell;
use core::ops::{Index, IndexMut};
use smpscheduler::{FifoSmpScheduler, FifoTask, ScheduleHart};
use spin::{Lazy, Once};
use spin::Lazy;

use kernel_sync::Mutex;
use crate::ksync::Mutex;
use syscall_define::ipc::FutexOp;
use syscall_define::signal::SignalNumber;
use syscall_define::task::{CloneFlags, WaitOptions};
Expand Down Expand Up @@ -89,10 +90,18 @@ impl CPU {
&mut self.context as *mut Context
}
}
pub struct SafeRefCell<T>(UnsafeCell<T>);
impl<T> SafeRefCell<T> {
const fn new(t: T) -> Self {
Self(UnsafeCell::new(t))
}
}
/// #Safety: Only the corresponding cpu will access it.
unsafe impl<CPU> Sync for SafeRefCell<CPU> {}

const DEFAULT_CPU: SafeRefCell<CPU> = SafeRefCell::new(CPU::empty());
/// 保存每个核的信息
static mut CPU_MANAGER: Once<CpuManager<CPU_NUM>> = Once::new();

static CPU_MANAGER: [SafeRefCell<CPU>; CPU_NUM] = [DEFAULT_CPU; CPU_NUM];
#[derive(Debug)]
pub struct ScheduleHartImpl;

Expand All @@ -106,21 +115,10 @@ pub static GLOBAL_TASK_MANAGER: Lazy<
FifoSmpScheduler<CPU_NUM, Arc<Task>, Mutex<()>, ScheduleHartImpl>,
> = Lazy::new(|| FifoSmpScheduler::new());

/// 初始化 CPU 的相关信息
pub fn init_per_cpu() {
unsafe {
CPU_MANAGER.call_once(|| CpuManager::new());
}
println!("{} cpus in total", CPU_NUM);
}

/// 获取当前 cpu 的信息
pub fn current_cpu() -> &'static mut CPU {
let hart_id = arch::hart_id();
unsafe {
let cpu_manager = CPU_MANAGER.get_mut().unwrap();
cpu_manager.index_mut(hart_id)
}
unsafe { &mut (*(CPU_MANAGER[hart_id].0.get())) }
}

/// 获取当前 CPU 上的线程
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/task/stack.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! 进程内核栈空间
use alloc::vec::Vec;

use kernel_sync::Mutex;
use crate::ksync::Mutex;

use crate::config::FRAME_BITS;
use crate::memory::{frame_alloc_contiguous, FrameTracker};
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/task/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use rvfs::info::ProcessFsInfo;
use rvfs::link::vfs_unlink;
use rvfs::mount::VfsMount;

use crate::ksync::{Mutex, MutexGuard};
use gmanager::MinimalManager;
use kernel_sync::{Mutex, MutexGuard};
use syscall_define::aux::{
AT_BASE, AT_EGID, AT_ENTRY, AT_EUID, AT_EXECFN, AT_GID, AT_PAGESZ, AT_PHDR, AT_PHENT, AT_PHNUM,
AT_PLATFORM, AT_RANDOM, AT_SECURE, AT_UID,
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/timer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use core::cmp::Ordering;

use lazy_static::lazy_static;

use kernel_sync::Mutex;
use crate::ksync::Mutex;
use smpscheduler::FifoTask;
use syscall_define::sys::TimeVal;
use syscall_define::time::{ClockId, TimerType};
Expand Down
3 changes: 2 additions & 1 deletion modules/simple-net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ edition = "2021"

[dependencies]
virtio-drivers = { git = "https://github.com/rcore-os/virtio-drivers", rev = "de1c3b130e507702f13d142b9bee55670a4a2858" }
kernel-sync = { path = "../../../os-modules/kernel-sync" }
kernel-sync = { git = "https://github.com/os-module/kernel-sync.git" }

spin = "0.9.8"
log = "0.4.17"
preprint = "0.1.0"
Expand Down
2 changes: 1 addition & 1 deletion modules/simple-net/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use smoltcp::wire::{EthernetAddress, HardwareAddress, IpAddress, IpCidr};
use virtio_drivers::transport::Transport;
use virtio_drivers::Hal;

use kernel_sync::Mutex;
use kernel_sync::TicketMutex as Mutex;

use crate::common::{TCP_RX_BUF_LEN, TCP_TX_BUF_LEN, UDP_RX_BUF_LEN, UDP_TX_BUF_LEN};
use crate::device::VirtIONetDeviceWrapper;
Expand Down
3 changes: 1 addition & 2 deletions modules/simple-net/src/listen_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ use smoltcp::iface::{SocketHandle, SocketSet};
use smoltcp::socket::tcp::{self, State};
use smoltcp::wire::{IpAddress, IpEndpoint, IpListenEndpoint};

use kernel_sync::Mutex;

use crate::common::{NetError, NetResult, LISTEN_QUEUE_SIZE};
use kernel_sync::TicketMutex as Mutex;

use super::{SocketSetWrapper, SOCKET_SET};

Expand Down
3 changes: 1 addition & 2 deletions modules/simple-net/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use smoltcp::iface::SocketHandle;
use smoltcp::socket::tcp::{self, ConnectError, State};
use smoltcp::wire::{IpEndpoint, IpListenEndpoint};

use kernel_sync::Mutex;

use crate::common::{NetError, NetPollState, NetResult};
use crate::{KERNEL_NET_FUNC, LISTENING_TABLE, NET_INTERFACE};
use kernel_sync::TicketMutex as Mutex;

use super::addr::{from_core_sockaddr, into_core_sockaddr, is_unspecified, UNSPECIFIED_ENDPOINT};
use super::{SocketSetWrapper, SOCKET_SET};
Expand Down
3 changes: 1 addition & 2 deletions modules/simple-net/src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use smoltcp::socket::udp::{self, BindError, SendError};
use smoltcp::wire::{IpEndpoint, IpListenEndpoint};
use spin::RwLock;

use kernel_sync::Mutex;

use crate::common::{NetError, NetPollState, NetResult};
use crate::KERNEL_NET_FUNC;
use kernel_sync::TicketMutex as Mutex;

use super::addr::{from_core_sockaddr, into_core_sockaddr, is_unspecified, UNSPECIFIED_ENDPOINT};
use super::{SocketSetWrapper, SOCKET_SET};
Expand Down

0 comments on commit 64177d6

Please sign in to comment.