diff --git a/src/context/mod.rs b/src/context/mod.rs index b815690f92548caa559ede678e6c17cc00155cc0..c1e0d19d6e44c02b38d613d998bc7b6d3b853b48 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -1,4 +1,6 @@ -//! Context management +//! # Context management +//! +//! For resources on contexts, please consult [wikipedia](https://en.wikipedia.org/wiki/Context_switch) and [osdev](https://wiki.osdev.org/Context_Switching) use alloc::boxed::Box; use core::alloc::{Alloc, GlobalAlloc, Layout}; use core::sync::atomic::Ordering; @@ -68,6 +70,8 @@ fn init_contexts() -> RwLock<ContextList> { /// Get the global schemes list, const pub fn contexts() -> RwLockReadGuard<'static, ContextList> { + //call once will init_contexts only once during the kernel's exececution, otherwise it will return the current context via a + //cache. CONTEXTS.call_once(init_contexts).read() } diff --git a/src/lib.rs b/src/lib.rs index a8e6c59c850c3e17eb45741894c658365744552c..c3caf85a44885f9d42a600da32cbc08ec37ee409 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -153,6 +153,7 @@ pub fn kmain(cpus: usize, env: &[u8]) -> ! { CPU_ID.store(0, Ordering::SeqCst); CPU_COUNT.store(cpus, Ordering::SeqCst); + //Initialize the first context, stored in kernel/src/context/mod.rs context::init(); let pid = syscall::getpid(); diff --git a/src/syscall/futex.rs b/src/syscall/futex.rs index d6f5696a7c7e77b36b7919370a5d58d3bacc1d4d..a05e8a7a331e01edc47646c6d639cc6d6b0fa868 100644 --- a/src/syscall/futex.rs +++ b/src/syscall/futex.rs @@ -1,3 +1,7 @@ +//! # Futex +//! Futex or Fast Userspace Mutex is "a method for waiting until a certain condition becomes true." +//! +//! For more information about futexes, please read [this](https://eli.thegreenplace.net/2018/basics-of-futexes/) blog post, and the [futex(2)](http://man7.org/linux/man-pages/man2/futex.2.html) man page use alloc::arc::Arc; use alloc::VecDeque; use core::intrinsics; diff --git a/src/syscall/mod.rs b/src/syscall/mod.rs index e65d2a2216741150c2dac38f489d0702f2e5a9e7..f06f1de64c44380d99048b48ccd6335839ef8de1 100644 --- a/src/syscall/mod.rs +++ b/src/syscall/mod.rs @@ -1,4 +1,6 @@ -///! Syscall handlers +//! +//! This module provides syscall definitions and the necessary resources to parse incoming +//! syscalls extern crate syscall; @@ -44,9 +46,12 @@ pub mod time; /// Validate input pub mod validate; +/// This function is the syscall handler of the kernel, it is composed of an inner function that returns a `Result<usize>`. After the inner function runs, the syscall +/// function calls [`Error::mux`] on it. pub fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, bp: usize, stack: &mut SyscallStack) -> usize { #[inline(always)] fn inner(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, bp: usize, stack: &mut SyscallStack) -> Result<usize> { + //SYS_* is declared in kernel/syscall/src/number.rs match a & SYS_CLASS { SYS_CLASS_FILE => { let fd = FileHandle::from(b); @@ -165,6 +170,11 @@ pub fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, bp: u } */ + // The next lines set the current syscall in the context struct, then once the inner() function + // completes, we set the current syscall to none. + // + // When the code below falls out of scope it will release the lock + // see the spin crate for details { let contexts = ::context::contexts(); if let Some(context_lock) = contexts.current() { @@ -204,5 +214,6 @@ pub fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, bp: u } */ + // errormux turns Result<usize> into -errno Error::mux(result) } diff --git a/src/syscall/time.rs b/src/syscall/time.rs index cef0bba93d870c679b283e62fe12c24410d1ff01..e40c27111f8cbb960bb25750f7123d4693663fa6 100644 --- a/src/syscall/time.rs +++ b/src/syscall/time.rs @@ -16,7 +16,9 @@ pub fn clock_gettime(clock: usize, time: &mut TimeSpec) -> Result<usize> { Ok(0) } +/// Nanosleep will sleep by switching the current context pub fn nanosleep(req: &TimeSpec, rem_opt: Option<&mut TimeSpec>) -> Result<usize> { + //start is a tuple of (seconds, nanoseconds) let start = time::monotonic(); let sum = start.1 + req.tv_nsec as u64; let end = (start.0 + req.tv_sec as u64 + sum / 1_000_000_000, sum % 1_000_000_000);