diff --git a/fs/vfs/src/node.rs b/fs/vfs/src/node.rs index 8ff2185..1e066b9 100644 --- a/fs/vfs/src/node.rs +++ b/fs/vfs/src/node.rs @@ -414,7 +414,7 @@ impl Vnode { } } - pub fn check_access(&self, ioctx: &Ioctx, access: AccessMode) -> Result<(), Errno> { + pub fn check_access(&self, _ioctx: &Ioctx, access: AccessMode) -> Result<(), Errno> { let props = self.props.borrow(); let mode = props.mode; diff --git a/kernel/src/arch/aarch64/exception.rs b/kernel/src/arch/aarch64/exception.rs index 5be036d..ff9a74e 100644 --- a/kernel/src/arch/aarch64/exception.rs +++ b/kernel/src/arch/aarch64/exception.rs @@ -4,7 +4,7 @@ use crate::arch::machine; use crate::debug::Level; use crate::dev::irq::{IntController, IrqContext}; use crate::mem; -use crate::proc::{sched, Thread, Process}; +use crate::proc::{sched, Thread}; use crate::syscall; use cortex_a::registers::{ESR_EL1, FAR_EL1}; use libsys::{abi::SystemCall, signal::Signal}; @@ -120,7 +120,6 @@ extern "C" fn __aa64_exc_sync_handler(exc: &mut ExceptionFrame) { let num = SystemCall::from_repr(exc.x[8]); if num.is_none() { todo!(); - return; } let num = num.unwrap(); diff --git a/kernel/src/init.rs b/kernel/src/init.rs index 53aea9f..9744253 100644 --- a/kernel/src/init.rs +++ b/kernel/src/init.rs @@ -3,7 +3,7 @@ use crate::config::{ConfigKey, CONFIG}; use crate::fs::{devfs, MemfsBlockAlloc}; use crate::mem; -use crate::proc::{wait, elf, Process}; +use crate::proc::{elf, Process}; use libsys::stat::{FileDescriptor, OpenFlags}; use memfs::Ramfs; use vfs::{Filesystem, Ioctx}; diff --git a/kernel/src/mem/phys/manager.rs b/kernel/src/mem/phys/manager.rs index 74ce890..de4ba15 100644 --- a/kernel/src/mem/phys/manager.rs +++ b/kernel/src/mem/phys/manager.rs @@ -93,18 +93,18 @@ impl SimpleManager { self.stats.available -= count; } - fn update_stats_free(&mut self, pu: PageUsage, count: usize) { - let field = match pu { - PageUsage::Kernel => &mut self.stats.kernel, - PageUsage::KernelHeap => &mut self.stats.kernel_heap, - PageUsage::Paging => &mut self.stats.paging, - PageUsage::UserPrivate => &mut self.stats.user_private, - PageUsage::Filesystem => &mut self.stats.filesystem, - _ => panic!("TODO {:?}", pu), - }; - *field -= count; - self.stats.available += count; - } + // fn update_stats_free(&mut self, pu: PageUsage, count: usize) { + // let field = match pu { + // PageUsage::Kernel => &mut self.stats.kernel, + // PageUsage::KernelHeap => &mut self.stats.kernel_heap, + // PageUsage::Paging => &mut self.stats.paging, + // PageUsage::UserPrivate => &mut self.stats.user_private, + // PageUsage::Filesystem => &mut self.stats.filesystem, + // _ => panic!("TODO {:?}", pu), + // }; + // *field -= count; + // self.stats.available += count; + // } } unsafe impl Manager for SimpleManager { fn alloc_page(&mut self, pu: PageUsage) -> Result { @@ -134,25 +134,21 @@ unsafe impl Manager for SimpleManager { Err(Errno::OutOfMemory) } fn free_page(&mut self, addr: usize) -> Result<(), Errno> { - let usage = { - let index = self.page_index(addr); - let page = &mut self.pages[index]; + let index = self.page_index(addr); + let page = &mut self.pages[index]; - let usage = page.usage; - assert!(page.usage != PageUsage::Reserved && page.usage != PageUsage::Available); + assert!(page.usage != PageUsage::Reserved && page.usage != PageUsage::Available); - if page.refcount > 1 { - page.refcount -= 1; - } else { - assert_eq!(page.refcount, 1); - page.usage = PageUsage::Available; - page.refcount = 0; + if page.refcount > 1 { + page.refcount -= 1; + } else { + assert_eq!(page.refcount, 1); + page.usage = PageUsage::Available; + page.refcount = 0; - self.last_index = index; - } + self.last_index = index; + } - usage - }; // FIXME // self.update_stats_free(usage, 1); diff --git a/kernel/src/proc/process.rs b/kernel/src/proc/process.rs index 06bbed6..e11f518 100644 --- a/kernel/src/proc/process.rs +++ b/kernel/src/proc/process.rs @@ -6,11 +6,10 @@ use crate::mem::{ virt::{MapAttributes, Space}, }; use crate::proc::{ - wait::Wait, Context, ProcessIo, Thread, ThreadRef, ThreadState, PROCESSES, SCHED, THREADS, + wait::Wait, Context, ProcessIo, Thread, ThreadRef, ThreadState, PROCESSES, SCHED, }; use crate::sync::{IrqSafeSpinLock, IrqSafeSpinLockGuard}; use alloc::{rc::Rc, vec::Vec}; -use core::cell::UnsafeCell; use core::sync::atomic::{AtomicU32, Ordering}; use libsys::{ error::Errno, @@ -108,7 +107,7 @@ impl Process { /// Sets a pending signal for a process pub fn set_signal(&self, signal: Signal) { - let mut lock = self.inner.lock(); + let lock = self.inner.lock(); let main_thread = Thread::get(lock.threads[0]).unwrap(); // TODO check that `signal` is not a fault signal @@ -133,7 +132,11 @@ impl Process { } } - fn enter_signal_on(mut inner: IrqSafeSpinLockGuard, thread: ThreadRef, signal: Signal) { + fn enter_signal_on( + mut inner: IrqSafeSpinLockGuard, + thread: ThreadRef, + signal: Signal, + ) { let ttbr0 = inner.space.as_mut().unwrap().address_phys() | ((inner.id.asid() as usize) << 48); drop(inner); diff --git a/kernel/src/proc/sched.rs b/kernel/src/proc/sched.rs index 28f002e..ccdc751 100644 --- a/kernel/src/proc/sched.rs +++ b/kernel/src/proc/sched.rs @@ -1,5 +1,5 @@ //! -use crate::proc::{Pid, Process, ProcessRef, Thread, ThreadRef, PROCESSES, THREADS}; +use crate::proc::{Thread, ThreadRef, THREADS}; use crate::sync::IrqSafeSpinLock; use crate::util::InitOnce; use alloc::{collections::VecDeque, rc::Rc}; diff --git a/kernel/src/proc/thread.rs b/kernel/src/proc/thread.rs index 3e4ed85..7c23763 100644 --- a/kernel/src/proc/thread.rs +++ b/kernel/src/proc/thread.rs @@ -2,10 +2,14 @@ use crate::arch::aarch64::exception::ExceptionFrame; use crate::proc::{wait::Wait, Process, ProcessRef, SCHED, THREADS}; use crate::sync::IrqSafeSpinLock; use crate::util::InitOnce; -use alloc::{rc::Rc, vec::Vec}; +use alloc::rc::Rc; use core::cell::UnsafeCell; use core::sync::atomic::{AtomicU32, Ordering}; -use libsys::{error::Errno, proc::{Pid, ExitCode}, signal::Signal}; +use libsys::{ + error::Errno, + proc::{ExitCode, Pid}, + signal::Signal, +}; pub use crate::arch::platform::context::{self, Context}; @@ -297,7 +301,7 @@ impl Thread { panic!("Already handling a signal (maybe handle this case)"); } - let mut lock = self.inner.lock(); + let lock = self.inner.lock(); if lock.signal_entry == 0 || lock.signal_stack == 0 { drop(lock); Process::exit_thread(self, ExitCode::from(-1)); @@ -317,7 +321,12 @@ impl Thread { assert_eq!(lock.state, State::Running); unsafe { - signal_ctx.setup_signal_entry(lock.signal_entry, signal as usize, ttbr0, lock.signal_stack); + signal_ctx.setup_signal_entry( + lock.signal_entry, + signal as usize, + ttbr0, + lock.signal_stack, + ); } drop(lock); diff --git a/kernel/src/proc/wait.rs b/kernel/src/proc/wait.rs index 2d21c8e..5885275 100644 --- a/kernel/src/proc/wait.rs +++ b/kernel/src/proc/wait.rs @@ -2,11 +2,11 @@ use crate::arch::machine; use crate::dev::timer::TimestampSource; -use crate::proc::{self, sched::SCHED, Process, Thread, ThreadRef}; +use crate::proc::{sched::SCHED, Thread, ThreadRef}; use crate::sync::IrqSafeSpinLock; use alloc::collections::LinkedList; use core::time::Duration; -use libsys::{error::Errno, stat::FdSet, proc::Pid}; +use libsys::{error::Errno, stat::FdSet}; /// Wait channel structure. Contains a queue of processes /// waiting for some event to happen. @@ -61,46 +61,45 @@ pub fn select( mut wfds: Option<&mut FdSet>, timeout: Option, ) -> Result { - todo!(); - // // TODO support wfds - // if wfds.is_some() || rfds.is_none() { - // todo!(); - // } - // let read = rfds.as_deref().map(FdSet::clone); - // let write = wfds.as_deref().map(FdSet::clone); - // rfds.as_deref_mut().map(FdSet::reset); - // wfds.as_deref_mut().map(FdSet::reset); + if wfds.is_none() && rfds.is_none() { + todo!(); + } + let read = rfds.as_deref().map(FdSet::clone); + let write = wfds.as_deref().map(FdSet::clone); + rfds.as_deref_mut().map(FdSet::reset); + wfds.as_deref_mut().map(FdSet::reset); - // let deadline = timeout.map(|v| v + machine::local_timer().timestamp().unwrap()); - // let mut io = proc.io.lock(); + let deadline = timeout.map(|v| v + machine::local_timer().timestamp().unwrap()); + let proc = thread.owner().unwrap(); + let mut io = proc.io.lock(); - // loop { - // if let Some(read) = &read { - // for fd in read.iter() { - // let file = io.file(fd)?; - // if file.borrow().is_ready(false)? { - // rfds.as_mut().unwrap().set(fd); - // return Ok(1); - // } - // } - // } - // if let Some(write) = &write { - // for fd in write.iter() { - // let file = io.file(fd)?; - // if file.borrow().is_ready(true)? { - // wfds.as_mut().unwrap().set(fd); - // return Ok(1); - // } - // } - // } + loop { + if let Some(read) = &read { + for fd in read.iter() { + let file = io.file(fd)?; + if file.borrow().is_ready(false)? { + rfds.as_mut().unwrap().set(fd); + return Ok(1); + } + } + } + if let Some(write) = &write { + for fd in write.iter() { + let file = io.file(fd)?; + if file.borrow().is_ready(true)? { + wfds.as_mut().unwrap().set(fd); + return Ok(1); + } + } + } - // // Suspend - // match WAIT_SELECT.wait(deadline) { - // Err(Errno::TimedOut) => return Ok(0), - // Err(e) => return Err(e), - // Ok(_) => {} - // } - // } + // Suspend + match WAIT_SELECT.wait(deadline) { + Err(Errno::TimedOut) => return Ok(0), + Err(e) => return Err(e), + Ok(_) => {} + } + } } impl Wait { diff --git a/kernel/src/syscall/arg.rs b/kernel/src/syscall/arg.rs index 58d3d1f..937ec98 100644 --- a/kernel/src/syscall/arg.rs +++ b/kernel/src/syscall/arg.rs @@ -2,7 +2,6 @@ use crate::mem; use core::alloc::Layout; -use core::mem::size_of; use libsys::error::Errno; use crate::proc::Process; @@ -100,7 +99,7 @@ fn validate_ptr(base: usize, len: usize, write: bool) -> Result<(), Errno> { }) } else { todo!(); - Err(Errno::DoesNotExist) + // Err(Errno::DoesNotExist) }; if res.is_ok() { diff --git a/libsys/src/calls.rs b/libsys/src/calls.rs index 20acb26..b5e13e7 100644 --- a/libsys/src/calls.rs +++ b/libsys/src/calls.rs @@ -191,7 +191,7 @@ pub fn sys_fstatat( /// System call #[inline(always)] pub unsafe fn sys_fork() -> Result, Errno> { - Errno::from_syscall(unsafe { syscall!(SystemCall::Fork) }).map(|res| { + Errno::from_syscall(syscall!(SystemCall::Fork)).map(|res| { if res != 0 { Some(unsafe { Pid::from_raw(res as u32) }) } else { diff --git a/libusr/src/file.rs b/libusr/src/file.rs index 80e7727..7603252 100644 --- a/libusr/src/file.rs +++ b/libusr/src/file.rs @@ -1,6 +1,4 @@ use crate::io::{AsRawFd, Error}; -use crate::os; -use crate::trace; use libsys::stat::FileDescriptor; pub struct File { @@ -8,7 +6,7 @@ pub struct File { } impl File { - pub fn open(path: &str) -> Result { + pub fn open(_path: &str) -> Result { todo!() } } diff --git a/libusr/src/io/error.rs b/libusr/src/io/error.rs index 9818834..64b570a 100644 --- a/libusr/src/io/error.rs +++ b/libusr/src/io/error.rs @@ -2,6 +2,7 @@ use libsys::error::Errno; #[derive(Debug)] pub struct Error { + #[allow(dead_code)] repr: Repr, } diff --git a/libusr/src/io/mod.rs b/libusr/src/io/mod.rs index ac29239..f4c9ee3 100644 --- a/libusr/src/io/mod.rs +++ b/libusr/src/io/mod.rs @@ -27,6 +27,6 @@ pub trait AsRawFd { pub fn stat(pathname: &str) -> Result { let mut buf = Stat::default(); // TODO error handling - let res = sys_fstatat(None, pathname, &mut buf, 0).unwrap(); + sys_fstatat(None, pathname, &mut buf, 0).unwrap(); Ok(buf) } diff --git a/libusr/src/io/stdio.rs b/libusr/src/io/stdio.rs index de7fc99..17f8afe 100644 --- a/libusr/src/io/stdio.rs +++ b/libusr/src/io/stdio.rs @@ -1,40 +1,40 @@ -use libsys::{ - stat::FileDescriptor, - calls::{sys_read, sys_write} -}; -use crate::io::{Read, Write, Error}; -use crate::sync::{Mutex, MutexGuard}; +use crate::io::{Error, Read, Write}; +use crate::sync::Mutex; use core::fmt; +use libsys::{ + calls::{sys_read, sys_write}, + stat::FileDescriptor, +}; struct InputInner { - fd: FileDescriptor + fd: FileDescriptor, } struct OutputInner { - fd: FileDescriptor + fd: FileDescriptor, } -pub struct StdinLock<'a> { - lock: MutexGuard<'a, InputInner> -} - -pub struct StdoutLock<'a> { - lock: MutexGuard<'a, OutputInner> -} - -pub struct StderrLock<'a> { - lock: MutexGuard<'a, OutputInner> -} +//pub struct StdinLock<'a> { +// lock: MutexGuard<'a, InputInner> +//} +// +//pub struct StdoutLock<'a> { +// lock: MutexGuard<'a, OutputInner> +//} +// +//pub struct StderrLock<'a> { +// lock: MutexGuard<'a, OutputInner> +//} pub struct Stdin { inner: &'static Mutex, } pub struct Stdout { - inner: &'static Mutex + inner: &'static Mutex, } pub struct Stderr { - inner: &'static Mutex + inner: &'static Mutex, } // STDIN @@ -51,6 +51,14 @@ impl Read for Stdin { } } +// impl Stdin { +// pub fn lock(&self) -> StdinLock { +// StdinLock { +// lock: self.inner.lock() +// } +// } +// } + // STDOUT/STDERR impl fmt::Write for OutputInner { @@ -89,21 +97,21 @@ impl Write for Stderr { } } -impl Stdout { - pub fn lock(&self) -> StdoutLock { - StdoutLock { - lock: self.inner.lock() - } - } -} - -impl Stderr { - pub fn lock(&self) -> StderrLock { - StderrLock { - lock: self.inner.lock() - } - } -} +// impl Stdout { +// pub fn lock(&self) -> StdoutLock { +// StdoutLock { +// lock: self.inner.lock() +// } +// } +// } +// +// impl Stderr { +// pub fn lock(&self) -> StderrLock { +// StderrLock { +// lock: self.inner.lock() +// } +// } +// } lazy_static! { static ref STDIN: Mutex = Mutex::new(InputInner { diff --git a/libusr/src/io/writer.rs b/libusr/src/io/writer.rs index 30ed6d8..9ddcc08 100644 --- a/libusr/src/io/writer.rs +++ b/libusr/src/io/writer.rs @@ -1,5 +1,5 @@ use core::fmt; -use crate::io::{self, Write}; +use crate::io::Write; #[macro_export] macro_rules! print { diff --git a/libusr/src/os.rs b/libusr/src/os.rs index 85a385a..f7d0f10 100644 --- a/libusr/src/os.rs +++ b/libusr/src/os.rs @@ -1,7 +1,5 @@ use crate::sys; use core::fmt; -use core::mem::{size_of, MaybeUninit}; -use libsys::{ioctl::IoctlCmd, stat::FileDescriptor, termios::Termios}; #[macro_export] macro_rules! trace { diff --git a/libusr/src/signal.rs b/libusr/src/signal.rs index e6fd5a0..f34793c 100644 --- a/libusr/src/signal.rs +++ b/libusr/src/signal.rs @@ -1,11 +1,15 @@ -use libsys::{calls::{sys_exit, sys_ex_sigreturn}, signal::Signal, proc::ExitCode}; -use crate::{trace, thread}; +use crate::trace; +use libsys::{ + calls::{sys_ex_sigreturn, sys_exit}, + proc::ExitCode, + signal::Signal, +}; #[derive(Clone, Copy)] pub enum SignalHandler { Func(fn(Signal) -> ()), Ignore, - Terminate + Terminate, } // TODO per-thread signal handler table diff --git a/libusr/src/sys/mod.rs b/libusr/src/sys/mod.rs index 8a6ab86..9b43f6b 100644 --- a/libusr/src/sys/mod.rs +++ b/libusr/src/sys/mod.rs @@ -23,11 +23,6 @@ impl RawMutex { self.inner.compare_exchange_weak(false, true, Ordering::Acquire, Ordering::Relaxed).is_ok() } - #[inline] - unsafe fn is_locked(&self) -> bool { - self.inner.load(Ordering::Acquire) - } - #[inline] pub unsafe fn lock(&self) { while !self.try_lock() { diff --git a/libusr/src/thread.rs b/libusr/src/thread.rs index 71df64e..266c11a 100644 --- a/libusr/src/thread.rs +++ b/libusr/src/thread.rs @@ -1,15 +1,13 @@ +use crate::signal; use alloc::{boxed::Box, sync::Arc, vec}; +use core::any::Any; use core::cell::UnsafeCell; +use core::fmt; use core::mem::MaybeUninit; use libsys::{ - calls::{sys_ex_clone, sys_ex_signal, sys_ex_thread_exit, sys_ex_thread_wait, sys_ex_gettid}, - error::Errno, + calls::{sys_ex_clone, sys_ex_gettid, sys_ex_signal, sys_ex_thread_exit, sys_ex_thread_wait}, proc::ExitCode, }; -use core::sync::atomic::{AtomicU32, Ordering}; -use core::any::Any; -use crate::{trace, signal}; -use core::fmt; struct NativeData where @@ -24,7 +22,7 @@ where #[derive(Clone)] pub struct Thread { - id: u32 + id: u32, } pub type ThreadResult = Result>; @@ -32,7 +30,7 @@ pub type ThreadPacket = Arc>>>; pub struct JoinHandle { native: u32, - result: ThreadPacket + result: ThreadPacket, } impl Thread { @@ -43,26 +41,33 @@ impl Thread { impl fmt::Debug for Thread { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("Thread").field("id", &self.id).finish_non_exhaustive() + f.debug_struct("Thread") + .field("id", &self.id) + .finish_non_exhaustive() } } impl JoinHandle { pub fn join(self) -> ThreadResult { sys_ex_thread_wait(self.native).unwrap(); - unsafe { Arc::try_unwrap(self.result).unwrap().into_inner().assume_init() } + unsafe { + Arc::try_unwrap(self.result) + .unwrap() + .into_inner() + .assume_init() + } } } unsafe fn init_common(signal_stack_pointer: *mut u8) { - let tid = sys_ex_gettid(); - asm!("msr tpidr_el0, {}", in(reg) tid); + let tid = sys_ex_gettid() as u64; + asm!("msr tpidr_el0, {:x}", in(reg) tid); // thread::current() should be valid at this point sys_ex_signal( signal::signal_handler as usize, - signal_stack_pointer as usize + signal_stack_pointer as usize, ) .unwrap(); } @@ -70,19 +75,19 @@ unsafe fn init_common(signal_stack_pointer: *mut u8) { pub(crate) unsafe fn init_main() { #[repr(align(16))] struct StackWrapper { - data: [u8; 8192] + data: [u8; 8192], } static mut STACK: StackWrapper = StackWrapper { data: [0; 8192] }; init_common(STACK.data.as_mut_ptr().add(8192)) } pub fn current() -> Thread { - let mut id: u32; + let mut id: u64; unsafe { - asm!("mrs {}, tpidr_el0", out(reg) id); + asm!("mrs {:x}, tpidr_el0", out(reg) id); } - Thread { id } + Thread { id: id as u32 } } pub fn spawn(f: F) -> JoinHandle @@ -101,7 +106,7 @@ where F: Send + 'static, T: Send + 'static, { - let (stack, len) = { + let (_stack, _len) = { // Setup signal handling let mut signal_stack = vec![0u8; 8192]; @@ -123,7 +128,7 @@ where sys_ex_thread_exit(ExitCode::from(0)); } - let native = unsafe { + let native = { let stack = stack.as_mut_ptr() as usize + stack.len(); let data: *mut NativeData = Box::into_raw(Box::new(NativeData { closure: f, diff --git a/user/src/fuzzy/main.rs b/user/src/fuzzy/main.rs index 8cde8fa..cfc7d4e 100644 --- a/user/src/fuzzy/main.rs +++ b/user/src/fuzzy/main.rs @@ -2,11 +2,13 @@ #![no_std] #![no_main] +#![allow(unused_macros)] +#![allow(dead_code)] + #[macro_use] extern crate libusr; -use libusr::sys::{abi::SystemCall, stat::Stat, Signal}; -use libusr::signal::{self, SignalHandler}; +use libusr::sys::{abi::SystemCall, stat::Stat}; static mut STATE: u64 = 0; @@ -115,9 +117,10 @@ fn main() -> i32 { // Test sys_ex_getcputime() let mut prev_time = libusr::sys::sys_ex_getcputime().unwrap().as_nanos(); - for _ in 0..100000 { + for _ in 0..1000 { let t = libusr::sys::sys_ex_getcputime().unwrap().as_nanos(); assert!(t >= prev_time); + prev_time = t; } // Test non-utf8 input fed into syscalls expecting strings diff --git a/user/src/shell/main.rs b/user/src/shell/main.rs index 64ea822..b2a835e 100644 --- a/user/src/shell/main.rs +++ b/user/src/shell/main.rs @@ -6,7 +6,7 @@ extern crate libusr; extern crate alloc; use alloc::borrow::ToOwned; -use libusr::sys::{sys_faccessat, sys_exit, sys_execve, sys_waitpid, sys_fork, ExitCode, Errno, AccessMode}; +use libusr::sys::{sys_exit, sys_execve, sys_waitpid, sys_fork, ExitCode, Errno}; use libusr::io::{self, Read}; fn readline<'a, F: Read>(f: &mut F, bytes: &'a mut [u8]) -> Result, io::Error> { @@ -19,7 +19,7 @@ fn readline<'a, F: Read>(f: &mut F, bytes: &'a mut [u8]) -> Result ! { - sys_execve(&("/bin/".to_owned() + cmd)); + sys_execve(&("/bin/".to_owned() + cmd)).unwrap(); sys_exit(ExitCode::from(-1)); } @@ -52,7 +52,7 @@ fn main() -> i32 { continue; } - execute(line); + execute(line).ok(); } 0 }