Skip to content

Process & Context

Contexts (threads/processes), scheduling state, and per-context resources.

src/context/context.rs

Status enum

rust
enum Status {
    Runnable,
    Blocked,
    HardBlocked,
    Dead,
}

The status of a context - used for scheduling

HardBlockedReason enum

rust
enum HardBlockedReason {
    Stopped,
    AwaitingMmap,
    NotYetStarted,
    PtraceStop,
    Emergency,
}

Context struct

rust
struct Context { /* … */ }

A context, which is typically mapped to a userspace thread

SignalState struct

rust
struct SignalState { /* … */ }

Context::block fn

rust
fn block(&mut self, reason : &'static str) -> bool

Block the context, and return true if it was runnable before being blocked

Context::unblock fn

rust
fn unblock(&mut self) -> bool

Unblock context, and return true if it was blocked before being marked runnable

Context::unblock_no_ipi fn

rust
fn unblock_no_ipi(&mut self) -> bool

Unblock context without IPI, and return true if it was blocked before being marked runnable

Context::add_file fn

rust
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

rust
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

rust
fn get_file(&self, i : FileHandle) -> Option<FileDescriptor>

Get a file

Context::insert_file fn

rust
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

rust
fn remove_file(&self, i : FileHandle) -> Option<FileDescriptor>

Remove a file

BorrowedHtBuf struct

rust
struct BorrowedHtBuf { /* … */ }

Wrapper struct for borrowing the syscall head or tail buf.

Kstack struct

rust
struct Kstack { /* … */ }

src/context/signal.rs

signal_handler fn

rust
fn signal_handler()

excp_handler fn

rust
fn excp_handler(excp : kernel_syscall::Exception)

src/context/file.rs

File structs

FileDescription struct

rust
struct FileDescription { /* … */ }

A file description

FileDescriptor struct

rust
struct FileDescriptor { /* … */ }

A file descriptor

FileDescription::try_close fn

rust
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

rust
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

rust
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

rust
enum SwitchResult {
    Switched,
    AllContextsIdle,
}

switch fn

rust
fn switch() -> SwitchResult

Selects 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

rust
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

rust
fn with_context<T>(&self, f : impl FnOnce(&Arc<RwSpinlock<Context>>) -> T) -> T

Applies 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

rust
fn try_with_context<T>(&self, f : impl FnOnce(Option<&Arc<RwSpinlock<Context>>>) -> T) -> T

Applies 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

rust
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

rust
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

rust
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

rust
fn register(scheme_id : SchemeId, event_id : usize, clock : usize, time : TimeSpec)

trigger fn

rust
fn trigger()

src/context/mod.rs

Context management

For resources on contexts, please consult wikipedia and osdev

CONTEXT_MAX_FILES const

rust
const CONTEXT_MAX_FILES : usize

Maximum context files

init fn

rust
fn init()

contexts fn

rust
fn contexts() -> RwLockReadGuard<'static, BTreeSet<ContextRef>>

Get the global schemes list, const

contexts_mut fn

rust
fn contexts_mut() -> RwLockWriteGuard<'static, BTreeSet<ContextRef>>

Get the global schemes list, mutable

current fn

rust
fn current() -> Arc<RwSpinlock<Context>>

try_current fn

rust
fn try_current() -> Option<Arc<RwSpinlock<Context>>>

is_current fn

rust
fn is_current(context : &Arc<RwSpinlock<Context>>) -> bool

ContextRef struct

rust
struct ContextRef { /* … */ }

spawn fn

rust
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

rust
fn force_kill_by_pid(pid : usize) -> bool

Force-kill a context by PID (used by emergency handler and proc scheme).

reap_context fn

rust
fn reap_context(ctx_ref : &Arc<RwSpinlock<Context>>)

Tear down a context immediately (emergency path — frees address space and files).

Released under the AWFixer Source Available License v0.4. #linuswasright