Process & Context
Contexts (threads/processes), scheduling state, and per-context resources.
src/context/context.rs
Status enum
enum Status {
Runnable,
Blocked,
HardBlocked,
Dead,
}The status of a context - used for scheduling
HardBlockedReason enum
enum HardBlockedReason {
Stopped,
AwaitingMmap,
NotYetStarted,
PtraceStop,
Emergency,
}Context struct
struct Context { /* … */ }A context, which is typically mapped to a userspace thread
SignalState struct
struct SignalState { /* … */ }Context::block fn
fn block(&mut self, reason : &'static str) -> boolBlock the context, and return true if it was runnable before being blocked
Context::unblock fn
fn unblock(&mut self) -> boolUnblock context, and return true if it was blocked before being marked runnable
Context::unblock_no_ipi fn
fn unblock_no_ipi(&mut self) -> boolUnblock context without IPI, and return true if it was blocked before being marked runnable
Context::add_file fn
fn add_file(&self, file : FileDescriptor) -> Option<FileHandle>Add a file to the lowest available slot. Return the file descriptor number or None if no slot was found
Context::add_file_min fn
fn add_file_min(&self, file : FileDescriptor, min : usize) -> Option<FileHandle>Add a file to the lowest available slot greater than or equal to min. Return the file descriptor number or None if no slot was found
Context::get_file fn
fn get_file(&self, i : FileHandle) -> Option<FileDescriptor>Get a file
Context::insert_file fn
fn insert_file(&self, i : FileHandle, file : FileDescriptor) -> Option<FileHandle>Insert a file with a specific handle number. This is used by dup2 Return the file descriptor number or None if the slot was not empty, or i was invalid
Context::remove_file fn
fn remove_file(&self, i : FileHandle) -> Option<FileDescriptor>Remove a file
BorrowedHtBuf struct
struct BorrowedHtBuf { /* … */ }Wrapper struct for borrowing the syscall head or tail buf.
Kstack struct
struct Kstack { /* … */ }src/context/signal.rs
signal_handler fn
fn signal_handler()excp_handler fn
fn excp_handler(excp : kernel_syscall::Exception)src/context/file.rs
File structs
FileDescription struct
struct FileDescription { /* … */ }A file description
FileDescriptor struct
struct FileDescriptor { /* … */ }A file descriptor
FileDescription::try_close fn
fn try_close(self) -> Result<()>Try closing a file, although at this point the description will be destroyed anyway, if doing so fails.
src/context/switch.rs
tick fn
fn tick()Tick function to update PIT ticks and trigger a context switch if necessary.
Called periodically, this function increments a per-CPU tick counter and performs a context switch if the counter reaches a set threshold (e.g., every 3 ticks).
The function also calls the signal handler after switching contexts.
switch_finish_hook fn
unsafe extern "C" fn switch_finish_hook()Finishes the context switch by clearing any temporary data and resetting the lock.
This function is called after a context switch is completed to perform cleanup, including clearing the switch result data and releasing the context switch lock.
Safety
This function involves unsafe operations such as resetting state and releasing locks.
SwitchResult enum
enum SwitchResult {
Switched,
AllContextsIdle,
}switch fn
fn switch() -> SwitchResultSelects and switches to the next context using a round-robin scheduler.
This function performs the context switch, checking each context in a loop for eligibility until it finds a context ready to run. If no other context is runnable, it returns to the idle context.
Warning
This is not memory-unsafe to call. But do NOT call this while holding locks!
Returns
SwitchResult::Switched: Indicates a successful switch to a new context.SwitchResult::AllContextsIdle: Indicates all contexts are idle, and the CPU will switch to an idle context.
ContextSwitchPercpu struct
struct ContextSwitchPercpu { /* … */ }Holds per-CPU state necessary for context switching.
This struct contains information such as the idle context, current context, and PIT tick counts, as well as fields required for managing ptrace sessions and signals.
ContextSwitchPercpu::with_context fn
fn with_context<T>(&self, f : impl FnOnce(&Arc<RwSpinlock<Context>>) -> T) -> TApplies a function to the current context, allowing controlled access.
Parameters
f: A closure that receives a reference to the current context and returns a value.
Returns
The result of applying f to the current context.
ContextSwitchPercpu::try_with_context fn
fn try_with_context<T>(&self, f : impl FnOnce(Option<&Arc<RwSpinlock<Context>>>) -> T) -> TApplies a function to the current context, allowing controlled access.
Parameters
f: A closure that receives a reference to the current context and returns a value.
Returns
The result of applying f to the current context if any.
ContextSwitchPercpu::set_current_context fn
unsafe fn set_current_context(&self, new : Arc<RwSpinlock<Context>>)Sets the current context to a new value.
Safety
This function is unsafe as it modifies the context state directly.
Parameters
new: The new context to be set as the current context.
ContextSwitchPercpu::set_idle_context fn
unsafe fn set_idle_context(&self, new : Arc<RwSpinlock<Context>>)Sets the idle context to a new value.
Safety
This function is unsafe as it modifies the idle context state directly.
Parameters
new: The new context to be set as the idle context.
ContextSwitchPercpu::idle_context fn
fn idle_context(&self) -> Arc<RwSpinlock<Context>>Retrieves the current idle context.
Returns
A reference to the idle context.
src/context/timeout.rs
register fn
fn register(scheme_id : SchemeId, event_id : usize, clock : usize, time : TimeSpec)trigger fn
fn trigger()src/context/mod.rs
Context management
For resources on contexts, please consult wikipedia and osdev
CONTEXT_MAX_FILES const
const CONTEXT_MAX_FILES : usizeMaximum context files
init fn
fn init()contexts fn
fn contexts() -> RwLockReadGuard<'static, BTreeSet<ContextRef>>Get the global schemes list, const
contexts_mut fn
fn contexts_mut() -> RwLockWriteGuard<'static, BTreeSet<ContextRef>>Get the global schemes list, mutable
current fn
fn current() -> Arc<RwSpinlock<Context>>try_current fn
fn try_current() -> Option<Arc<RwSpinlock<Context>>>is_current fn
fn is_current(context : &Arc<RwSpinlock<Context>>) -> boolContextRef struct
struct ContextRef { /* … */ }spawn fn
fn spawn(userspace_allowed : bool, owner_proc_id : Option<NonZeroUsize>, func : extern "C" fn(),) -> Result<Arc<RwSpinlock<Context>>>Spawn a context from a function.
force_kill_by_pid fn
fn force_kill_by_pid(pid : usize) -> boolForce-kill a context by PID (used by emergency handler and proc scheme).
reap_context fn
fn reap_context(ctx_ref : &Arc<RwSpinlock<Context>>)Tear down a context immediately (emergency path — frees address space and files).