feat: add SavedContext::with_entry_and_stack, wire SpawnThread to preserve entry point and stack

This commit is contained in:
2026-10-18 01:24:56 +02:00
parent 4833f2e2bb
commit 142505d8a4
4 changed files with 88 additions and 3 deletions

View File

@@ -31,10 +31,31 @@ pub struct SavedContext {
registers: [usize; 32],
}
pub const PC_REGISTER: usize = 0;
pub const SP_REGISTER: usize = 1;
impl SavedContext {
pub fn empty() -> Self {
Self { registers: [0; 32] }
}
pub fn with_entry_and_stack(
entry: types::address::EntryPoint,
stack: types::address::StackPointer,
) -> Self {
let mut ctx = Self::empty();
ctx.registers[PC_REGISTER] = entry.raw();
ctx.registers[SP_REGISTER] = stack.raw();
ctx
}
pub fn program_counter(&self) -> usize {
self.registers[PC_REGISTER]
}
pub fn stack_pointer(&self) -> usize {
self.registers[SP_REGISTER]
}
}
pub enum BlockReason {
@@ -63,6 +84,29 @@ impl Thread {
}
}
pub fn with_context(
id: ThreadId,
process: ProcessId,
priority: Priority,
time_slice: TimeSlice,
core_affinity: Option<CoreId>,
context: SavedContext,
) -> Self {
Self {
id,
process,
state: ThreadState::Ready,
saved_context: context,
priority,
time_slice,
core_affinity,
}
}
pub fn saved_context(&self) -> &SavedContext {
&self.saved_context
}
pub fn id(&self) -> ThreadId {
self.id
}
@@ -173,6 +217,25 @@ impl ThreadTable {
Ok(id)
}
pub fn spawn_with_context(
&mut self,
process: ProcessId,
priority: Priority,
time_slice: TimeSlice,
core_affinity: Option<CoreId>,
context: SavedContext,
) -> Result<ThreadId, KernelError> {
let idx = self
.threads
.iter()
.position(|s| s.is_none())
.ok_or(KernelError::TooManyThreads)?;
let id = ThreadId::from_raw(idx as u16);
self.threads[idx] = Some(Thread::with_context(id, process, priority, time_slice, core_affinity, context));
Ok(id)
}
pub fn get(&self, id: ThreadId) -> Result<&Thread, KernelError> {
let idx = id.raw() as usize;
self.threads