diff --git a/cykusz-rs/src/arch/x86_64/syscall.rs b/cykusz-rs/src/arch/x86_64/syscall.rs index f321362d..f38b2ff9 100644 --- a/cykusz-rs/src/arch/x86_64/syscall.rs +++ b/cykusz-rs/src/arch/x86_64/syscall.rs @@ -50,13 +50,13 @@ pub extern "C" fn fast_syscall_handler(sys_frame: &mut SyscallFrame, regs: &mut } } else { //logln!("syscall {:?} {:?}", regs, sys_frame); - logln_disabled!("syscall {}, ret: 0x{:x}", regs.rax, sys_frame.rip); + logln!("syscall {}, ret: 0x{:x}", regs.rax, sys_frame.rip); let res = crate::kernel::syscall::syscall_handler( regs.rax, regs.rdi, regs.rsi, regs.rdx, regs.r10, regs.r8, regs.r9, ); - logln_disabled!("done syscall {} = {:?}", regs.rax, res); + logln!("done syscall {} = {:?}", regs.rax, res); crate::arch::signal::arch_sys_check_signals(res, sys_frame, regs); } diff --git a/cykusz-rs/src/drivers/block/ahci/device.rs b/cykusz-rs/src/drivers/block/ahci/device.rs index e1887e38..d4bd13ae 100644 --- a/cykusz-rs/src/drivers/block/ahci/device.rs +++ b/cykusz-rs/src/drivers/block/ahci/device.rs @@ -7,7 +7,6 @@ use crate::drivers::block::ahci::port::Port; use crate::drivers::block::ahci::reg::*; use crate::drivers::pci::PciHeader; use crate::kernel::block::{register_blkdev, BlockDevice}; - use crate::kernel::mm::VirtAddr; pub struct AhciDevice { diff --git a/cykusz-rs/src/drivers/block/ahci/port/hba.rs b/cykusz-rs/src/drivers/block/ahci/port/hba.rs index c0214867..78e19fa7 100644 --- a/cykusz-rs/src/drivers/block/ahci/port/hba.rs +++ b/cykusz-rs/src/drivers/block/ahci/port/hba.rs @@ -1,9 +1,7 @@ use crate::arch::mm::virt::map_to_flags; - use crate::drivers::block::ahci::reg::*; use crate::drivers::block::ata::request::DmaBuf; use crate::drivers::block::ata::AtaCommand; - use crate::kernel::mm::allocate_order; use crate::kernel::mm::virt::PageFlags; diff --git a/cykusz-rs/src/drivers/block/ahci/reg/cmd.rs b/cykusz-rs/src/drivers/block/ahci/reg/cmd.rs index df406fac..de758b02 100644 --- a/cykusz-rs/src/drivers/block/ahci/reg/cmd.rs +++ b/cykusz-rs/src/drivers/block/ahci/reg/cmd.rs @@ -3,7 +3,6 @@ use mmio::VCell; use crate::drivers::block::ahci::reg::FisRegH2D; use crate::kernel::mm::PhysAddr; -use crate::kernel::utils::slice::ToBytesMut; bitflags! { pub struct HbaCmdHeaderFlags: u16 { diff --git a/cykusz-rs/src/drivers/block/ahci/reg/fis.rs b/cykusz-rs/src/drivers/block/ahci/reg/fis.rs index 810344e8..6b9d4b37 100644 --- a/cykusz-rs/src/drivers/block/ahci/reg/fis.rs +++ b/cykusz-rs/src/drivers/block/ahci/reg/fis.rs @@ -2,8 +2,6 @@ use bit_field::BitField; use mmio::VCell; use crate::drivers::block::ata::AtaCommand; -use crate::kernel::mm::PhysAddr; -use crate::kernel::utils::slice::ToBytesMut; #[repr(u8)] #[derive(Copy, Clone)] diff --git a/cykusz-rs/src/drivers/block/ahci/reg/mod.rs b/cykusz-rs/src/drivers/block/ahci/reg/mod.rs index ca96e16b..eafabf1a 100644 --- a/cykusz-rs/src/drivers/block/ahci/reg/mod.rs +++ b/cykusz-rs/src/drivers/block/ahci/reg/mod.rs @@ -1,7 +1,6 @@ #![allow(unused)] use bit_field::BitField; -use mmio::VCell; pub use self::cmd::*; pub use self::fis::*; diff --git a/cykusz-rs/src/drivers/block/ahci/reg/port.rs b/cykusz-rs/src/drivers/block/ahci/reg/port.rs index db9469ea..0c95b836 100644 --- a/cykusz-rs/src/drivers/block/ahci/reg/port.rs +++ b/cykusz-rs/src/drivers/block/ahci/reg/port.rs @@ -3,7 +3,6 @@ use mmio::VCell; use crate::drivers::block::ahci::reg::HbaCmdHeader; use crate::kernel::mm::PhysAddr; -use crate::kernel::mm::VirtAddr; bitflags! { pub struct HbaPortISReg: u32 { diff --git a/cykusz-rs/src/drivers/pci/mod.rs b/cykusz-rs/src/drivers/pci/mod.rs index 3b7d27aa..8047f048 100644 --- a/cykusz-rs/src/drivers/pci/mod.rs +++ b/cykusz-rs/src/drivers/pci/mod.rs @@ -1,12 +1,13 @@ +use alloc::sync::Arc; +use alloc::vec::Vec; + +use bit_field::BitField; + use crate::arch::idt::{add_shared_irq_handler, InterruptFn, SharedInterruptFn}; use crate::arch::int::{set_active_high, set_irq_dest, set_level_triggered}; use crate::arch::mm::{PhysAddr, VirtAddr}; use crate::kernel::mm::map_to_flags; use crate::kernel::mm::virt::PageFlags; -use alloc::sync::Arc; -use alloc::vec::Vec; -use bit_field::BitField; - use crate::kernel::sync::Spin; mod epci; diff --git a/cykusz-rs/src/drivers/ps2/kbd/handler.rs b/cykusz-rs/src/drivers/ps2/kbd/handler.rs index 400d7169..a95cde0c 100644 --- a/cykusz-rs/src/drivers/ps2/kbd/handler.rs +++ b/cykusz-rs/src/drivers/ps2/kbd/handler.rs @@ -1,21 +1,23 @@ -use crate::drivers::ps2::{controller, Error}; -use crate::kernel::sync::Spin; -use crate::kernel::utils::buffer::BufferQueue; use alloc::string::String; use alloc::sync::{Arc, Weak}; + use spin::Once; +use syscall_defs::events::{Event, EventType}; +use syscall_defs::poll::PollEventFlags; +use syscall_defs::time::Timeval; +use syscall_defs::OpenFlags; + +use crate::drivers::ps2::{controller, Error}; use crate::kernel::device::dev_t::DevId; use crate::kernel::device::Device; use crate::kernel::fs::inode::INode; use crate::kernel::fs::poll::PollTable; use crate::kernel::fs::vfs::FsError; +use crate::kernel::sync::Spin; use crate::kernel::timer::current_ns; +use crate::kernel::utils::buffer::BufferQueue; use crate::kernel::utils::wait_queue::WaitQueueFlags; -use syscall_defs::events::{Event, EventType}; -use syscall_defs::poll::PollEventFlags; -use syscall_defs::time::Timeval; -use syscall_defs::OpenFlags; use super::scancode; diff --git a/cykusz-rs/src/drivers/ps2/kbd/scancode.rs b/cykusz-rs/src/drivers/ps2/kbd/scancode.rs index 2bec30cb..91d0baa4 100644 --- a/cykusz-rs/src/drivers/ps2/kbd/scancode.rs +++ b/cykusz-rs/src/drivers/ps2/kbd/scancode.rs @@ -1,4 +1,5 @@ use syscall_defs::events::keys::KeyCode; + macro_rules! init_arr ( ($a: ident, $([$k: expr, $v: expr]),+) => { $($a[$k] = $v;)* diff --git a/cykusz-rs/src/drivers/ps2/mod.rs b/cykusz-rs/src/drivers/ps2/mod.rs index 29d91d86..b2c9e698 100644 --- a/cykusz-rs/src/drivers/ps2/mod.rs +++ b/cykusz-rs/src/drivers/ps2/mod.rs @@ -1,4 +1,5 @@ use core::hint::spin_loop; + use spin::Once; use crate::kernel::sync::IrqGuard; diff --git a/cykusz-rs/src/drivers/ps2/mouse/handler.rs b/cykusz-rs/src/drivers/ps2/mouse/handler.rs index 41b5f632..39e93b4c 100644 --- a/cykusz-rs/src/drivers/ps2/mouse/handler.rs +++ b/cykusz-rs/src/drivers/ps2/mouse/handler.rs @@ -1,3 +1,15 @@ +use alloc::string::String; +use alloc::sync::{Arc, Weak}; + +use bit_field::BitField; +use spin::Once; + +use syscall_defs::events::buttons::{ButtonCode, RelCode}; +use syscall_defs::events::{Event, EventType}; +use syscall_defs::poll::PollEventFlags; +use syscall_defs::time::Timeval; +use syscall_defs::OpenFlags; + use crate::drivers::ps2::{controller, Error}; use crate::kernel::device::dev_t::DevId; use crate::kernel::device::Device; @@ -8,15 +20,6 @@ use crate::kernel::sync::Spin; use crate::kernel::timer::current_ns; use crate::kernel::utils::buffer::BufferQueue; use crate::kernel::utils::wait_queue::WaitQueueFlags; -use alloc::string::String; -use alloc::sync::{Arc, Weak}; -use bit_field::BitField; -use spin::Once; -use syscall_defs::events::buttons::{ButtonCode, RelCode}; -use syscall_defs::events::{Event, EventType}; -use syscall_defs::poll::PollEventFlags; -use syscall_defs::time::Timeval; -use syscall_defs::OpenFlags; struct MouseState { state: Spin, diff --git a/cykusz-rs/src/drivers/ps2/mouse/mod.rs b/cykusz-rs/src/drivers/ps2/mouse/mod.rs index 4f0cc6d9..8e0b9a89 100644 --- a/cykusz-rs/src/drivers/ps2/mouse/mod.rs +++ b/cykusz-rs/src/drivers/ps2/mouse/mod.rs @@ -1,8 +1,8 @@ -mod handler; - use crate::arch::int; use crate::drivers::ps2::{controller, Command, Error, PS}; +mod handler; + #[repr(u8)] #[allow(dead_code)] #[derive(Debug, Copy, Clone)] diff --git a/cykusz-rs/src/drivers/random.rs b/cykusz-rs/src/drivers/random.rs index d7960fee..6e755189 100644 --- a/cykusz-rs/src/drivers/random.rs +++ b/cykusz-rs/src/drivers/random.rs @@ -1,12 +1,13 @@ -use crate::kernel::device::Device; -use crate::kernel::fs::inode::INode; use alloc::string::String; use alloc::sync::{Arc, Weak}; +use rand::{RngCore, SeedableRng}; + use crate::kernel::device::dev_t::DevId; +use crate::kernel::device::Device; +use crate::kernel::fs::inode::INode; use crate::kernel::sync::Spin; use crate::kernel::timer::current_ns; -use rand::{RngCore, SeedableRng}; struct Random { id: DevId, diff --git a/cykusz-rs/src/drivers/tty/mod.rs b/cykusz-rs/src/drivers/tty/mod.rs index d8a41b33..aa1bf89b 100644 --- a/cykusz-rs/src/drivers/tty/mod.rs +++ b/cykusz-rs/src/drivers/tty/mod.rs @@ -23,7 +23,6 @@ use crate::kernel::session::{sessions, Group}; use crate::kernel::signal::{SignalError, SignalResult}; use crate::kernel::sync::{Spin, SpinGuard}; use crate::kernel::task::Task; - use crate::kernel::tty::TerminalDevice; use crate::kernel::utils::types::Prefault; use crate::kernel::utils::wait_queue::{WaitQueue, WaitQueueFlags}; diff --git a/cykusz-rs/src/drivers/video/fb.rs b/cykusz-rs/src/drivers/video/fb.rs index a9db7e62..4ac58c3a 100644 --- a/cykusz-rs/src/drivers/video/fb.rs +++ b/cykusz-rs/src/drivers/video/fb.rs @@ -1,3 +1,10 @@ +use alloc::string::String; +use alloc::sync::{Arc, Weak}; +use alloc::vec::Vec; + +use bit_field::BitField; +use spin::Once; + use crate::arch::mm::VirtAddr; use crate::arch::output::{register_video_driver, Color, ColorCode, ScreenChar, VideoDriver}; use crate::drivers::multiboot2::framebuffer_info::{FramebufferInfo, FramebufferType}; @@ -11,11 +18,6 @@ use crate::kernel::mm::{map_to_flags, virt, MappedAddr, PhysAddr, PAGE_SIZE}; use crate::kernel::sync::Spin; use crate::kernel::timer::TimerObject; use crate::kernel::utils::types::Align; -use alloc::string::String; -use alloc::sync::{Arc, Weak}; -use alloc::vec::Vec; -use bit_field::BitField; -use spin::Once; static FONT: &'static [u8] = &[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, diff --git a/cykusz-rs/src/kernel/device/mod.rs b/cykusz-rs/src/kernel/device/mod.rs index 7d7143d7..e7a50aa0 100644 --- a/cykusz-rs/src/kernel/device/mod.rs +++ b/cykusz-rs/src/kernel/device/mod.rs @@ -1,6 +1,3 @@ -pub mod dev_t; - -use crate::kernel::device::dev_t::DevId; use alloc::collections::BTreeMap; use alloc::string::String; use alloc::sync::Arc; @@ -8,9 +5,12 @@ use alloc::vec::Vec; use core::sync::atomic::AtomicUsize; use core::sync::atomic::Ordering; +use crate::kernel::device::dev_t::DevId; use crate::kernel::fs::inode::INode; use crate::kernel::sync::RwSpin; +pub mod dev_t; + pub trait Device: Send + Sync { fn id(&self) -> dev_t::DevId; fn name(&self) -> String; @@ -67,3 +67,7 @@ pub fn register_device_listener(listener: Arc) { pub fn devices() -> &'static RwSpin>> { &DEVICES } + +pub fn find_device(id: DevId) -> Option> { + devices().read().get(&id).cloned() +} diff --git a/cykusz-rs/src/kernel/fs/devnode.rs b/cykusz-rs/src/kernel/fs/devnode.rs deleted file mode 100644 index 6653951a..00000000 --- a/cykusz-rs/src/kernel/fs/devnode.rs +++ /dev/null @@ -1,62 +0,0 @@ -use alloc::sync::Arc; - -use syscall_defs::OpenFlags; -use syscall_defs::poll::PollEventFlags; - -use crate::kernel::device::{DevError, Device}; -use crate::kernel::device::dev_t::DevId; -use crate::kernel::device::Result as DevResult; -use crate::kernel::fs::inode::INode; -use crate::kernel::fs::pcache::MappedAccess; -use crate::kernel::fs::poll::PollTable; -use crate::kernel::fs::vfs::Result; - -pub struct DevNode { - dev: Arc, -} - -impl DevNode { - pub fn new(devid: DevId) -> DevResult> { - let dev = crate::kernel::device::devices().read(); - - if let Some(d) = dev.get(&devid) { - Ok(Arc::new(DevNode { dev: d.clone() })) - } else { - Err(DevError::DeviceNotFound) - } - } - - pub fn device(&self) -> Arc { - self.dev.clone() - } -} - -impl INode for DevNode { - fn read_at(&self, offset: usize, buf: &mut [u8]) -> Result { - self.dev.inode().read_at(offset, buf) - } - - fn write_at(&self, offset: usize, buf: &[u8]) -> Result { - self.dev.inode().write_at(offset, buf) - } - - fn poll( - &self, - ptable: Option<&mut PollTable>, - flags: PollEventFlags, - ) -> Result { - self.dev.inode().poll(ptable, flags) - } - - fn open(&self, flags: OpenFlags) -> Result<()> { - self.dev.inode().open(flags) - } - - fn ioctl(&self, cmd: usize, arg: usize) -> Result { - self.dev.inode().ioctl(cmd, arg) - } - - fn as_mappable(&self) -> Option> { - self.dev.inode().as_mappable() - } -} diff --git a/cykusz-rs/src/kernel/fs/ext2/disk/inode.rs b/cykusz-rs/src/kernel/fs/ext2/disk/inode.rs index 4671a320..94778828 100644 --- a/cykusz-rs/src/kernel/fs/ext2/disk/inode.rs +++ b/cykusz-rs/src/kernel/fs/ext2/disk/inode.rs @@ -1,6 +1,7 @@ #![allow(dead_code)] use bit_field::BitField; + use crate::kernel::device::dev_t::DevId; #[derive(Copy, Clone, PartialEq)] diff --git a/cykusz-rs/src/kernel/fs/ext2/inode.rs b/cykusz-rs/src/kernel/fs/ext2/inode.rs index c945cbab..07abc0da 100644 --- a/cykusz-rs/src/kernel/fs/ext2/inode.rs +++ b/cykusz-rs/src/kernel/fs/ext2/inode.rs @@ -815,7 +815,7 @@ impl INode for LockedExt2INode { parent: DirEntryItem, name: &str, mode: Mode, - devid: DevId + devid: DevId, ) -> Result { let inode = self.create(parent, name, mode.into())?.inode(); @@ -1044,6 +1044,16 @@ impl INode for LockedExt2INode { Some(Arc::new(SysDirEntIter::new(parent, self.self_ref()))) } + fn device_id(&self) -> Option { + if ![FileType::Block, FileType::Char].contains(&self.ftype().ok()?) { + return None; + } + + let id = self.node.read().d_inode().get_rdevid(); + + return if id != 0 { Some(id) } else { None }; + } + fn sync(&self) -> Result<()> { let mut pages = self.dirty_list.lock(); @@ -1060,10 +1070,6 @@ impl INode for LockedExt2INode { return Ok(()); } - fn as_inode(&self) -> Option> { - Some(self.self_ref()) - } - fn as_cacheable(&self) -> Option> { if self.ftype().unwrap() == FileType::File { Some(self.self_ref()) diff --git a/cykusz-rs/src/kernel/fs/inode.rs b/cykusz-rs/src/kernel/fs/inode.rs index 5e951b73..dfa927ff 100644 --- a/cykusz-rs/src/kernel/fs/inode.rs +++ b/cykusz-rs/src/kernel/fs/inode.rs @@ -8,7 +8,6 @@ use syscall_defs::{FileType, OpenFlags}; use syscall_defs::poll::PollEventFlags; use crate::kernel::device::dev_t::DevId; -use crate::kernel::device::Device; use crate::kernel::fs::dirent::DirEntryItem; use crate::kernel::fs::filesystem::Filesystem; use crate::kernel::fs::icache::{INodeItem, INodeItemInt}; @@ -135,8 +134,8 @@ pub trait INode: Send + Sync + DowncastSync { None } - fn device(&self) -> Result> { - return Err(FsError::EntryNotFound); + fn device_id(&self) -> Option { + None } fn ioctl(&self, _cmd: usize, _arg: usize) -> Result { @@ -149,10 +148,6 @@ pub trait INode: Send + Sync + DowncastSync { fn ref_update(&self, _new_ref: Weak) {} - fn as_inode(&self) -> Option> { - None - } - fn as_cacheable(&self) -> Option> { None } diff --git a/cykusz-rs/src/kernel/fs/mod.rs b/cykusz-rs/src/kernel/fs/mod.rs index d40db0b2..80f4693c 100644 --- a/cykusz-rs/src/kernel/fs/mod.rs +++ b/cykusz-rs/src/kernel/fs/mod.rs @@ -21,7 +21,6 @@ use crate::kernel::fs::vfs::{FsError, Result}; use crate::kernel::sched::current_task; pub mod cache; -pub mod devnode; pub mod dirent; pub mod ext2; pub mod filesystem; diff --git a/cykusz-rs/src/kernel/fs/ramfs.rs b/cykusz-rs/src/kernel/fs/ramfs.rs index ffebcaae..1fddd0c6 100644 --- a/cykusz-rs/src/kernel/fs/ramfs.rs +++ b/cykusz-rs/src/kernel/fs/ramfs.rs @@ -12,7 +12,6 @@ use syscall_defs::poll::PollEventFlags; use crate::kernel::device::{alloc_id, Device}; use crate::kernel::device::dev_t::DevId; -use crate::kernel::fs::devnode::DevNode; use crate::kernel::fs::dirent::DirEntryItem; use crate::kernel::fs::ext2::FsDevice; use crate::kernel::fs::filesystem::Filesystem; @@ -30,7 +29,7 @@ struct LockedRamINode(RwSpin); enum Content { Bytes(Spin>), - DevNode(Option>), + DevNode(DevId), None, } @@ -77,8 +76,8 @@ impl INode for LockedRamINode { stat.st_ino = self.id()? as u64; let content = self.0.read(); - stat.st_rdev = if let Content::DevNode(Some(d)) = &content.content { - d.device().id() + stat.st_rdev = if let Content::DevNode(id) = &content.content { + *id } else { 0 }; @@ -144,13 +143,6 @@ impl INode for LockedRamINode { Ok(to_copy) } - Content::DevNode(Some(node)) => { - let n = node.clone(); - drop(i); - - // read_at may sleep, so drop the lock - n.read_at(offset, buf) - } _ => Err(FsError::NotSupported), } } @@ -170,13 +162,6 @@ impl INode for LockedRamINode { Ok(buf.len()) } - Content::DevNode(Some(node)) => { - let n = node.clone(); - drop(i); - - // write_at may sleep, so drop the lock - n.write_at(offset, buf) - } _ => Err(FsError::NotSupported), } } @@ -186,17 +171,7 @@ impl INode for LockedRamINode { ptable: Option<&mut PollTable>, flags: PollEventFlags, ) -> Result { - let i = self.0.read(); - - match &i.content { - Content::DevNode(Some(n)) => { - let n = n.clone(); - drop(i); - - n.poll(ptable, flags) - } - _ => Err(FsError::NotSupported), - } + Err(FsError::NotSupported) } fn fs(&self) -> Option> { @@ -212,11 +187,7 @@ impl INode for LockedRamINode { } fn open(&self, flags: OpenFlags) -> Result<()> { - if let Content::DevNode(Some(d)) = &self.0.read().content { - d.open(flags) - } else { - Ok(()) - } + Ok(()) } fn mknode( @@ -227,9 +198,7 @@ impl INode for LockedRamINode { devid: DevId, ) -> Result { self.make_inode(name, mode.into(), |inode| { - inode.0.write().content = Content::DevNode(Some( - DevNode::new(devid).map_err(|e| FsError::EntryNotFound)?, - )); + inode.0.write().content = Content::DevNode(devid); Ok(()) }) } @@ -237,16 +206,16 @@ impl INode for LockedRamINode { fn truncate(&self, size: usize) -> Result<()> { let node = self.0.write(); - match &node.content { + return match &node.content { Content::Bytes(vec) => { let mut v = vec.lock(); v.resize(size, 0); - return Ok(()); + Ok(()) } - _ => return Ok(()), - } + _ => Ok(()), + }; } fn dir_ent(&self, parent: DirEntryItem, idx: usize) -> Result> { @@ -285,31 +254,20 @@ impl INode for LockedRamINode { Ok(dir) } - fn device(&self) -> Result> { - if let Content::DevNode(Some(d)) = &self.0.read().content { - Ok(d.device()) + fn device_id(&self) -> Option { + if let Content::DevNode(d) = &self.0.read().content { + Some(*d) } else { - Err(FsError::EntryNotFound) + None } } fn ioctl(&self, cmd: usize, arg: usize) -> Result { - let read = self.0.read(); - if let Content::DevNode(Some(d)) = &read.content { - let dev = d.clone(); - drop(read); - dev.ioctl(cmd, arg) - } else { - Err(FsError::NotSupported) - } + Err(FsError::NotSupported) } fn as_mappable(&self) -> Option> { - if let Content::DevNode(Some(d)) = &self.0.read().content { - d.device().inode().as_mappable() - } else { - None - } + None } } @@ -461,8 +419,8 @@ impl RamFS { children: BTreeMap::new(), fs: Weak::default(), content: match typ { - FileType::Char => Content::DevNode(None), - FileType::Block => Content::DevNode(None), + FileType::Char => Content::DevNode(0), + FileType::Block => Content::DevNode(0), FileType::File => Content::Bytes(Spin::new(Vec::new())), FileType::Dir => Content::None, FileType::Symlink => Content::None, diff --git a/cykusz-rs/src/kernel/kbd.rs b/cykusz-rs/src/kernel/kbd.rs index fea74cb0..c64281d0 100644 --- a/cykusz-rs/src/kernel/kbd.rs +++ b/cykusz-rs/src/kernel/kbd.rs @@ -1,9 +1,9 @@ use alloc::vec::Vec; -use crate::kernel::sync::RwSpin; - use syscall_defs::events::keys::KeyCode; +use crate::kernel::sync::RwSpin; + pub trait KeyListener: Sync { fn on_new_key(&self, key: KeyCode, released: bool); } diff --git a/cykusz-rs/src/kernel/params.rs b/cykusz-rs/src/kernel/params.rs index 317bb0a0..ddb2a00d 100644 --- a/cykusz-rs/src/kernel/params.rs +++ b/cykusz-rs/src/kernel/params.rs @@ -1,4 +1,5 @@ use alloc::string::String; + use spin::Once; pub struct Params { diff --git a/cykusz-rs/src/kernel/sched/mod.rs b/cykusz-rs/src/kernel/sched/mod.rs index 89cc2546..3281e574 100644 --- a/cykusz-rs/src/kernel/sched/mod.rs +++ b/cykusz-rs/src/kernel/sched/mod.rs @@ -14,7 +14,6 @@ use crate::kernel::sched::round_robin::RRScheduler; use crate::kernel::sched::task_container::TaskContainer; use crate::kernel::session::sessions; use crate::kernel::signal::SignalResult; - use crate::kernel::task::Task; #[macro_export] diff --git a/cykusz-rs/src/kernel/sched/round_robin.rs b/cykusz-rs/src/kernel/sched/round_robin.rs index 9557334d..19d8dfde 100644 --- a/cykusz-rs/src/kernel/sched/round_robin.rs +++ b/cykusz-rs/src/kernel/sched/round_robin.rs @@ -1,13 +1,12 @@ use alloc::sync::Arc; -use crate::arch::int; use intrusive_collections::LinkedList; +use crate::arch::int; use crate::kernel::sched::{SchedulerInterface, SleepFlags}; use crate::kernel::signal::{SignalError, SignalResult}; use crate::kernel::sync::{IrqGuard, Spin, SpinGuard}; use crate::kernel::task::{SchedTaskAdapter, Task, TaskState}; - use crate::kernel::utils::PerCpu; #[thread_local] diff --git a/cykusz-rs/src/kernel/session.rs b/cykusz-rs/src/kernel/session.rs index db4bce05..43e41912 100644 --- a/cykusz-rs/src/kernel/session.rs +++ b/cykusz-rs/src/kernel/session.rs @@ -1,6 +1,6 @@ use alloc::sync::{Arc, Weak}; -use hashbrown::HashMap; +use hashbrown::HashMap; use spin::Once; use syscall_defs::{SyscallError, SyscallResult}; diff --git a/cykusz-rs/src/kernel/signal/mod.rs b/cykusz-rs/src/kernel/signal/mod.rs index 5c1bc2d1..862e36a5 100644 --- a/cykusz-rs/src/kernel/signal/mod.rs +++ b/cykusz-rs/src/kernel/signal/mod.rs @@ -13,7 +13,6 @@ use syscall_defs::{SyscallError, SyscallResult}; use crate::kernel::fs::vfs::FsError; use crate::kernel::sched::current_task_ref; - use crate::kernel::sync::{IrqGuard, Spin, SpinGuard}; use crate::kernel::task::Task; diff --git a/cykusz-rs/src/kernel/syscall/mod.rs b/cykusz-rs/src/kernel/syscall/mod.rs index 1f451456..7eded46d 100644 --- a/cykusz-rs/src/kernel/syscall/mod.rs +++ b/cykusz-rs/src/kernel/syscall/mod.rs @@ -108,7 +108,7 @@ pub fn syscall_handler(num: u64, a: u64, b: u64, c: u64, d: u64, e: u64, f: u64) }; if res.is_err() { - logln4!("syscall {} result = {:?}", num, res); + logln!("syscall {} result = {:?}", num, res); } res diff --git a/cykusz-rs/src/kernel/syscall/sys.rs b/cykusz-rs/src/kernel/syscall/sys.rs index 70ae390a..4677efdb 100644 --- a/cykusz-rs/src/kernel/syscall/sys.rs +++ b/cykusz-rs/src/kernel/syscall/sys.rs @@ -3,7 +3,6 @@ use alloc::sync::Arc; use alloc::sync::Weak; use alloc::vec::Vec; -use crate::kernel::device::dev_t::DevId; use syscall_defs::net::{MsgFlags, MsgHdr, SockAddr, SockDomain, SockOption, SockTypeFlags}; use syscall_defs::poll::{FdSet, PollEventFlags}; use syscall_defs::signal::SigAction; @@ -14,8 +13,10 @@ use syscall_defs::{ }; use syscall_defs::{OpenFlags, SyscallError}; +use crate::kernel::device::dev_t::DevId; use crate::kernel::fs::dirent::{DirEntry, DirEntryItem}; use crate::kernel::fs::filesystem::FilesystemKind; +use crate::kernel::fs::inode::INode; use crate::kernel::fs::path::Path; use crate::kernel::fs::poll::PollTable; use crate::kernel::fs::{lookup_by_path, lookup_by_path_at, lookup_by_real_path, LookupMode}; @@ -64,7 +65,7 @@ fn get_dir_entry( lookup_mode: LookupMode, get_symlink_entry: bool, ) -> Result { - logln4!( + logln!( "get dir entry: {:?} {:?} {:?} get_symlink_entry: {}", fd, path, @@ -75,13 +76,19 @@ fn get_dir_entry( let task = current_task_ref(); let file_dir = match fd { - OpenFD::Fd(fd) => task + OpenFD::Fd(fd) if path.is_some() => task .get_handle(fd) .ok_or(SyscallError::EBADFD)? - .inode - .clone(), + .get_inode() + .get_fs_dir_item(), + OpenFD::Fd(fd) if !path.is_some() => { + return Ok(task + .get_handle(fd) + .ok_or(SyscallError::EBADFD)? + .get_dir_item()); + } OpenFD::Cwd => task.get_dent().ok_or(SyscallError::EBADFD)?.clone(), - OpenFD::None => { + _ => { return Err(SyscallError::EINVAL); } }; @@ -99,7 +106,7 @@ fn get_dir_entry( } pub fn sys_open(at: u64, path: u64, len: u64, mode: u64) -> SyscallResult { - logln5!("sys_open {} {} {:x}", at, make_str(path, len), mode); + logln!("sys_open {} {} {:x}", at, make_str(path, len), mode); let mut flags = OpenFlags::from_bits(mode as usize).ok_or(SyscallError::EINVAL)?; if !flags.intersects(OpenFlags::RDONLY | OpenFlags::RDWR | OpenFlags::WRONLY) { flags.insert(OpenFlags::RDONLY); @@ -363,8 +370,7 @@ pub fn sys_mmap(addr: u64, len: u64, prot: u64, flags: u64, fd: u64, offset: u64 Some( task.get_handle(fd as usize) .ok_or(SyscallError::EBADFD)? - .inode - .clone(), + .get_dir_item(), ) } else { None @@ -783,11 +789,7 @@ fn get_socket(fd: usize) -> Result, SyscallError> { let sock = task.get_handle(fd).ok_or(SyscallError::EBADFD)?; - Ok(sock - .inode - .inode() - .as_socket() - .ok_or(SyscallError::ENOTSOCK)?) + Ok(sock.get_inode().as_socket().ok_or(SyscallError::ENOTSOCK)?) } pub fn sys_bind(sockfd: u64, addr_ptr: u64, addrlen: u64) -> SyscallResult { @@ -1124,8 +1126,8 @@ pub fn sys_mount( let dev = lookup_by_path(&Path::new(dev_path), LookupMode::None)?.inode(); let dest = lookup_by_path(&Path::new(dest_path), LookupMode::None)?; - let dev = - crate::kernel::block::get_blkdev_by_id(dev.device()?.id()).ok_or(SyscallError::ENODEV)?; + let dev = crate::kernel::block::get_blkdev_by_id(dev.device_id().ok_or(SyscallError::ENODEV)?) + .ok_or(SyscallError::ENODEV)?; crate::kernel::fs::mount::mount(dest, Some(dev), FilesystemKind::Ext2FS) .and(Ok(0)) @@ -1458,8 +1460,7 @@ pub fn sys_truncate(fd: u64, size: u64) -> SyscallResult { task.filetable() .get_handle(fd as usize) .ok_or(SyscallError::EBADFD)? - .inode - .inode() + .get_inode() .truncate(size as usize) .map(|_r| Ok(0))? } @@ -1532,7 +1533,7 @@ pub fn sys_fsync(fd: u64) -> SyscallResult { .get_handle(fd as usize) .ok_or(SyscallError::EBADFD)?; - file.inode.inode().sync()?; + file.get_inode().sync()?; Ok(0) } diff --git a/cykusz-rs/src/kernel/task/children_events.rs b/cykusz-rs/src/kernel/task/children_events.rs index 48078bb7..d6649746 100644 --- a/cykusz-rs/src/kernel/task/children_events.rs +++ b/cykusz-rs/src/kernel/task/children_events.rs @@ -1,11 +1,12 @@ use alloc::sync::Arc; -use crate::kernel::sched::current_task_ref; -use crate::kernel::session::sessions; use intrusive_collections::LinkedList; + use syscall_defs::waitpid::WaitPidFlags; use syscall_defs::{SyscallError, SyscallResult}; +use crate::kernel::sched::current_task_ref; +use crate::kernel::session::sessions; use crate::kernel::signal::SignalResult; use crate::kernel::sync::Spin; use crate::kernel::task::{Task, WaitPidTaskAdapter}; diff --git a/cykusz-rs/src/kernel/task/filetable/inode_wrap.rs b/cykusz-rs/src/kernel/task/filetable/inode_wrap.rs new file mode 100644 index 00000000..f5fcb55c --- /dev/null +++ b/cykusz-rs/src/kernel/task/filetable/inode_wrap.rs @@ -0,0 +1,130 @@ +use alloc::sync::{Arc, Weak}; +use alloc::vec::Vec; + +use syscall_defs::poll::PollEventFlags; +use syscall_defs::stat::Stat; +use syscall_defs::{FileType, OpenFlags}; + +use crate::kernel::device::dev_t::DevId; +use crate::kernel::fs::dirent::{DirEntry, DirEntryItem}; +use crate::kernel::fs::filesystem::Filesystem; +use crate::kernel::fs::icache::{INodeItem, INodeItemInt}; +use crate::kernel::fs::inode::INode; +use crate::kernel::fs::pcache::{CachedAccess, MappedAccess}; +use crate::kernel::fs::poll::PollTable; +use crate::kernel::fs::vfs; +use crate::kernel::fs::vfs::{DirEntIter, Metadata}; +use crate::kernel::net::socket::SocketService; + +pub struct INodeOpsWrap { + dev_inode: Option>, + fs_inode: DirEntryItem, + self_ref: Weak, +} + +fn get_dev_inode(inode: &DirEntryItem) -> Option> { + let fs_inode = inode.inode(); + + return match fs_inode.ftype() { + Ok(FileType::Fifo) => { + if let Some(ino) = crate::kernel::fs::pipe::pipes().get_or_insert(&fs_inode) { + Some(ino) + } else { + None + } + } + Ok(FileType::Char) | Ok(FileType::Block) => { + crate::kernel::device::find_device(fs_inode.device_id()?).and_then(|d| Some(d.inode())) + } + _ => None, + }; +} + +impl INodeOpsWrap { + pub fn new(inode: DirEntryItem) -> Arc { + Arc::new_cyclic(|me| INodeOpsWrap { + dev_inode: get_dev_inode(&inode), + fs_inode: inode, + self_ref: me.clone(), + }) + } + + fn get_inode(&self) -> Arc { + if let Some(i) = &self.dev_inode { + i.clone() + } else { + self.fs_inode.inode().inode_arc() + } + } + + pub fn get_fs_dir_item(&self) -> DirEntryItem { + return self.fs_inode.clone(); + } + + pub fn get_dir_item(&self) -> DirEntryItem { + return DirEntry::inode_wrap(self.self_ref.upgrade().unwrap().clone()); + } +} + +macro_rules! impl_delegate { + ($name:tt, $res: ty) => { + fn $name(&self) -> $res { + self.get_inode().$name() + } + }; + ($name:tt, $res: ty, $($v:tt: $t:ty),*) => { + fn $name(&self, $($v: $t),+) -> $res { + self.get_inode().$name($($v,)*) + } + } +} + +#[allow(unused)] +macro_rules! impl_delegate_fs { + ($name:tt, $res: ty) => { + fn $name(&self) -> $res { + self.fs_inode.inode().$name() + } + }; + ($name:tt, $res: ty, $($v:tt: $t:ty),*) => { + fn $name(&self, $($v: $t),+) -> $res { + self.fs_inode.inode().$name($($v,)*) + } + } +} + +impl INode for INodeOpsWrap { + impl_delegate!(id, vfs::Result); + impl_delegate!(ftype, vfs::Result); + impl_delegate!(metadata, vfs::Result); + impl_delegate!(stat, vfs::Result); + impl_delegate!(lookup, vfs::Result, parent: DirEntryItem, name: &str); + impl_delegate!(mkdir, vfs::Result, name: &str); + impl_delegate!(rmdir, vfs::Result<()>, name: &str); + impl_delegate!(unlink, vfs::Result<()>, name: &str); + impl_delegate!(read_at, vfs::Result, offset: usize, buf: &mut [u8]); + impl_delegate!(read_all, Vec); + impl_delegate!(write_at, vfs::Result, offset: usize, buf: &[u8]); + impl_delegate!(poll, vfs::Result, poll_table: Option<&mut PollTable>, flags: PollEventFlags); + impl_delegate!(fs, Option>); + impl_delegate!(create, vfs::Result, parent: DirEntryItem,name: &str, ftype: FileType); + impl_delegate!(open, vfs::Result<()>, flags: OpenFlags); + impl_delegate!(close, (), flags: OpenFlags); + impl_delegate!(mknode, vfs::Result, parent: DirEntryItem, name: &str, mode: syscall_defs::stat::Mode, devid: DevId); + impl_delegate!(symlink, vfs::Result<()>, name: &str, target: &str); + impl_delegate!(link, vfs::Result<()>, name: &str, target: INodeItem); + impl_delegate!(rename, vfs::Result<()>, old: DirEntryItem, new_name: &str); + impl_delegate!(chmod, vfs::Result<()>, mode: syscall_defs::stat::Mode); + impl_delegate!(utime, vfs::Result<()>, times: &[syscall_defs::time::Timespec; 2]); + impl_delegate!(truncate, vfs::Result<()>, size: usize); + impl_delegate!(dir_ent, vfs::Result>, parent: DirEntryItem, idx: usize); + impl_delegate!(dir_iter, Option>, parent: DirEntryItem); + impl_delegate!(device_id, Option); + impl_delegate!(ioctl, vfs::Result, cmd: usize, arg: usize); + impl_delegate!(sync, vfs::Result<()>); + impl_delegate!(ref_update, (), new_ref: Weak); + impl_delegate!(as_cacheable, Option>); + impl_delegate!(as_mappable, Option>); + impl_delegate!(as_socket, Option>); + impl_delegate!(debug, ()); +} diff --git a/cykusz-rs/src/kernel/task/filetable.rs b/cykusz-rs/src/kernel/task/filetable/mod.rs similarity index 88% rename from cykusz-rs/src/kernel/task/filetable.rs rename to cykusz-rs/src/kernel/task/filetable/mod.rs index 5764e06b..8a42dae4 100644 --- a/cykusz-rs/src/kernel/task/filetable.rs +++ b/cykusz-rs/src/kernel/task/filetable/mod.rs @@ -12,49 +12,45 @@ use crate::kernel::fs::inode::INode; use crate::kernel::fs::poll::PollTable; use crate::kernel::fs::vfs::{DirEntIter, FsError, Result}; use crate::kernel::sync::{Mutex, RwMutex}; +use crate::kernel::task::filetable::inode_wrap::INodeOpsWrap; + +mod inode_wrap; const FILE_NUM: usize = 256; pub struct FileHandle { - pub fd: usize, - pub inode: DirEntryItem, - pub offset: AtomicUsize, - pub flags: AtomicUsize, - pub dir_iter: Mutex<(Option>, Option)>, + fd: usize, + inode: Arc, + offset: AtomicUsize, + flags: AtomicUsize, + dir_iter: Mutex<(Option>, Option)>, } impl FileHandle { pub fn new(fd: usize, inode: DirEntryItem, flags: OpenFlags) -> FileHandle { FileHandle { fd, - inode: inode.clone(), + inode: INodeOpsWrap::new(inode), offset: AtomicUsize::new(0), flags: AtomicUsize::from(flags.bits()), dir_iter: Mutex::new((None, None)), } } - fn get_inode(&self) -> Arc { - let fs_inode = self.inode.inode(); + pub fn get_dir_item(&self) -> DirEntryItem { + self.inode.get_dir_item() + } - return match fs_inode.ftype() { - Ok(FileType::Fifo) => { - if let Some(ino) = crate::kernel::fs::pipe::pipes().get_or_insert(&fs_inode) { - ino - } else { - fs_inode.inode_arc() - } - } - _ => fs_inode.inode_arc(), - }; + pub fn get_inode(&self) -> &Arc { + &self.inode } pub fn open(&self, flags: OpenFlags) -> Result<()> { - self.get_inode().open(flags) + self.inode.open(flags) } pub fn close(&self, flags: OpenFlags) { - self.get_inode().close(flags) + self.inode.close(flags) } pub fn read(&self, buf: &mut [u8]) -> Result { @@ -81,11 +77,11 @@ impl FileHandle { } pub fn read_at(&self, buf: &mut [u8], offset: usize) -> Result { - Ok(self.get_inode().read_at(offset, buf)?) + Ok(self.inode.read_at(offset, buf)?) } pub fn write_at(&self, buf: &[u8], offset: usize) -> Result { - let inode = self.get_inode(); + let inode = &self.inode; Ok(match inode.as_cacheable() { Some(cacheable) => { if let Some(w) = cacheable.write_cached(offset, buf) { @@ -99,7 +95,7 @@ impl FileHandle { } pub fn seek(&self, off: isize, whence: syscall_defs::SeekWhence) -> Result { - let meta = self.get_inode().metadata().ok().ok_or(FsError::IsPipe)?; + let meta = self.inode.metadata().ok().ok_or(FsError::IsPipe)?; if meta.typ == FileType::File { match whence { @@ -129,7 +125,7 @@ impl FileHandle { } pub fn ioctl(&self, cmd: usize, arg: usize) -> Result { - self.get_inode().ioctl(cmd, arg) + self.inode.ioctl(cmd, arg) } pub fn poll( @@ -137,14 +133,18 @@ impl FileHandle { poll_table: Option<&mut PollTable>, flags: PollEventFlags, ) -> Result { - self.get_inode().poll(poll_table, flags) + self.inode.poll(poll_table, flags) } fn get_dir_iter(&self) -> (Option>, Option) { let mut lock = self.dir_iter.lock(); if self.offset.load(Ordering::SeqCst) == 0 && lock.0.is_none() { - let i = self.inode.inode().dir_iter(self.inode.clone()); + let i = self + .inode + .get_fs_dir_item() + .inode() + .dir_iter(self.inode.get_fs_dir_item()); lock.0 = i; }; @@ -192,7 +192,11 @@ impl FileHandle { let o = self.offset.load(Ordering::SeqCst); match &iter { Some(i) => i.next(), - None => self.inode.inode().dir_ent(self.inode.clone(), o)?, + None => self + .inode + .get_fs_dir_item() + .inode() + .dir_ent(self.inode.get_fs_dir_item(), o)?, } } }; @@ -244,7 +248,7 @@ impl FileHandle { impl Drop for FileHandle { fn drop(&mut self) { - self.get_inode().close(self.flags()); + self.inode.close(self.flags()); } } @@ -320,7 +324,7 @@ impl FileTable { "[{}] fd: {} {} {:?}", i, f.handle.fd, - f.handle.inode.full_path(), + f.handle.inode.get_fs_dir_item().full_path(), f.handle.flags() ); } @@ -340,15 +344,10 @@ impl FileTable { Some(FileDescriptor::new( Arc::new(FileHandle { fd, - inode: inode.clone(), + inode: INodeOpsWrap::new(inode.clone()), offset: AtomicUsize::new(if append { size as usize } else { 0 }), flags: AtomicUsize::new(flags.bits()), dir_iter: Mutex::new((None, None)), - //fs: if let Some(fs) = inode.inode().fs() { - // fs.upgrade() - //} else { - // None - //}, }), FDFlags::from(flags), )) diff --git a/cykusz-rs/src/kernel/task/mod.rs b/cykusz-rs/src/kernel/task/mod.rs index 9e56d90b..11d2c386 100644 --- a/cykusz-rs/src/kernel/task/mod.rs +++ b/cykusz-rs/src/kernel/task/mod.rs @@ -15,7 +15,6 @@ use crate::arch::task::Task as ArchTask; use crate::kernel::fs::dirent::DirEntryItem; use crate::kernel::fs::root_dentry; use crate::kernel::sched::{new_task_tid, SleepFlags}; - use crate::kernel::signal::{SignalResult, Signals, KSIGSTOPTHR}; use crate::kernel::sync::{RwSpin, Spin, SpinGuard}; use crate::kernel::task::children_events::WaitPidEvents; diff --git a/cykusz-rs/src/kernel/tty.rs b/cykusz-rs/src/kernel/tty.rs index 1b647eee..203e45ce 100644 --- a/cykusz-rs/src/kernel/tty.rs +++ b/cykusz-rs/src/kernel/tty.rs @@ -1,9 +1,9 @@ use alloc::collections::BTreeMap; use alloc::sync::{Arc, Weak}; -use crate::kernel::device::dev_t::DevId; use spin::Once; +use crate::kernel::device::dev_t::DevId; use crate::kernel::fs::path::Path; use crate::kernel::fs::vfs::FsError; use crate::kernel::fs::{lookup_by_real_path, LookupMode}; @@ -58,9 +58,9 @@ impl Default for Terminal { pub fn get_tty_by_path(path: &str) -> Result, FsError> { let entry = lookup_by_real_path(&Path::new(path), LookupMode::None)?; - let device = entry.inode().device()?; + let device = entry.inode().device_id().ok_or(FsError::EntryNotFound)?; - if let Some(tty) = get_tty_by_id(device.id()) { + if let Some(tty) = get_tty_by_id(device) { Ok(tty) } else { Err(FsError::EntryNotFound)