feat: implement context switching, SavedContext with pc/status fields, wire all syscall outcomes and timer preemption
This commit is contained in:
@@ -29,14 +29,17 @@ pub enum ThreadState {
|
||||
|
||||
pub struct SavedContext {
|
||||
registers: [usize; 32],
|
||||
pc: usize,
|
||||
status: usize,
|
||||
}
|
||||
|
||||
pub const PC_REGISTER: usize = 0;
|
||||
pub const SP_REGISTER: usize = 1;
|
||||
|
||||
impl SavedContext {
|
||||
pub fn empty() -> Self {
|
||||
Self { registers: [0; 32] }
|
||||
Self {
|
||||
registers: [0; 32],
|
||||
pc: 0,
|
||||
status: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_entry_and_stack(
|
||||
@@ -44,17 +47,33 @@ impl SavedContext {
|
||||
stack: types::address::StackPointer,
|
||||
) -> Self {
|
||||
let mut ctx = Self::empty();
|
||||
ctx.registers[PC_REGISTER] = entry.raw();
|
||||
ctx.registers[SP_REGISTER] = stack.raw();
|
||||
ctx.pc = entry.raw();
|
||||
ctx.registers[2] = stack.raw();
|
||||
ctx
|
||||
}
|
||||
|
||||
pub fn program_counter(&self) -> usize {
|
||||
self.registers[PC_REGISTER]
|
||||
self.pc
|
||||
}
|
||||
|
||||
pub fn stack_pointer(&self) -> usize {
|
||||
self.registers[SP_REGISTER]
|
||||
pub fn set_pc(&mut self, pc: usize) {
|
||||
self.pc = pc;
|
||||
}
|
||||
|
||||
pub fn status(&self) -> usize {
|
||||
self.status
|
||||
}
|
||||
|
||||
pub fn set_status(&mut self, status: usize) {
|
||||
self.status = status;
|
||||
}
|
||||
|
||||
pub fn registers(&self) -> &[usize; 32] {
|
||||
&self.registers
|
||||
}
|
||||
|
||||
pub fn registers_mut(&mut self) -> &mut [usize; 32] {
|
||||
&mut self.registers
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,6 +126,10 @@ impl Thread {
|
||||
&self.saved_context
|
||||
}
|
||||
|
||||
pub fn saved_context_mut(&mut self) -> &mut SavedContext {
|
||||
&mut self.saved_context
|
||||
}
|
||||
|
||||
pub fn id(&self) -> ThreadId {
|
||||
self.id
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user