2023-03-08 21:10:51 +01:00
|
|
|
use super::{process::Process, system::ObjectType};
|
2023-03-08 15:48:33 +01:00
|
|
|
use crate::{simulator::machine::{NUM_INT_REGS, NUM_FP_REGS, STACK_REG}};
|
2023-02-28 14:43:40 +01:00
|
|
|
|
2023-03-01 15:45:49 +01:00
|
|
|
const SIMULATORSTACKSIZE: usize = 32 * 1024;
|
2023-03-06 16:31:35 +01:00
|
|
|
const STACK_FENCEPOST: u32 = 0xdeadbeef;
|
2023-02-28 14:43:40 +01:00
|
|
|
|
2023-03-01 11:10:15 +01:00
|
|
|
#[derive(PartialEq)]
|
2023-02-28 14:43:40 +01:00
|
|
|
struct ThreadContext {
|
|
|
|
pub int_registers: [i64; NUM_INT_REGS],
|
|
|
|
pub float_registers: [i64; NUM_FP_REGS],
|
|
|
|
pc: i64,
|
|
|
|
}
|
|
|
|
|
2023-03-01 11:10:15 +01:00
|
|
|
#[derive(PartialEq)]
|
2023-02-28 14:43:40 +01:00
|
|
|
pub struct Thread {
|
|
|
|
name: String,
|
2023-03-08 21:10:51 +01:00
|
|
|
pub process: Option<Process>,
|
2023-03-06 16:31:35 +01:00
|
|
|
// simulation_context: UContextT,
|
2023-02-28 14:43:40 +01:00
|
|
|
thread_context: ThreadContext,
|
|
|
|
stack_pointer: i32,
|
2023-03-08 21:10:51 +01:00
|
|
|
object_type: ObjectType
|
2023-02-28 14:43:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Thread {
|
|
|
|
|
2023-03-08 15:48:33 +01:00
|
|
|
/// Thread constructor
|
2023-03-08 21:10:51 +01:00
|
|
|
pub fn new(name: String) -> Self {
|
2023-02-28 14:43:40 +01:00
|
|
|
Self {
|
|
|
|
name,
|
|
|
|
process: None,
|
2023-03-06 16:31:35 +01:00
|
|
|
// simulation_context: UContextT::new(),
|
2023-02-28 14:43:40 +01:00
|
|
|
thread_context: ThreadContext {
|
|
|
|
int_registers: [0; NUM_INT_REGS],
|
|
|
|
float_registers: [0; NUM_FP_REGS],
|
|
|
|
pc: 0
|
|
|
|
},
|
|
|
|
stack_pointer: 0,
|
2023-03-08 15:48:33 +01:00
|
|
|
object_type: ObjectType::ThreadType,
|
2023-02-28 14:43:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 21:10:51 +01:00
|
|
|
pub fn init_thread_context(&mut self, initial_pc_reg: i64, initial_sp: i64, arg: i64) {
|
2023-03-01 15:45:49 +01:00
|
|
|
self.thread_context.pc = initial_pc_reg;
|
|
|
|
self.thread_context.int_registers[10] = arg;
|
|
|
|
self.thread_context.int_registers[STACK_REG] = initial_sp;
|
|
|
|
}
|
|
|
|
|
2023-03-08 21:10:51 +01:00
|
|
|
pub fn init_simulator_context(&self, base_stack_addr: [i8; SIMULATORSTACKSIZE]) {
|
2023-03-06 16:31:35 +01:00
|
|
|
// let res = self.simulation_context.get_context();
|
|
|
|
// if res != 0 {
|
|
|
|
// panic!("getcontext returns non-zero value {}", res);
|
|
|
|
// }
|
|
|
|
// self.simulation_context.buf.uc_stack.ss_sp = base_stack_addr;
|
|
|
|
// self.simulation_context.buf.uc_stack.ss_size = base_stack_addr.len();
|
|
|
|
// self.simulation_context.buf.uc_stack.ss_flags = 0;
|
|
|
|
// self.simulation_context.buf.uc_link = UContextT::new().buf;
|
|
|
|
// self.simulation_context.make_context(start_thread_execution, 0);
|
|
|
|
|
|
|
|
// self.simulation_context.stackBottom = base_stack_addr.to_vec();
|
|
|
|
// self.simulation_context.stackBottom[0] = STACK_FENCEPOST;
|
2023-02-28 14:43:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Put the thread to sleep and relinquish the processor
|
|
|
|
pub fn sleep(&self) {
|
|
|
|
todo!();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finish the execution of the thread and prepare its deallocation
|
2023-03-08 15:48:33 +01:00
|
|
|
pub fn finish(&self) {
|
2023-02-28 14:43:40 +01:00
|
|
|
todo!();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if a thread has overflowed its stack
|
|
|
|
pub fn check_overflow(&self) {
|
|
|
|
todo!();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save_processor_state(&self) {
|
|
|
|
todo!();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn restore_processor_state(&self) {
|
|
|
|
todo!();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save_simulator_state(&self) {
|
|
|
|
todo!();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn restore_simulator_state(&self) {
|
|
|
|
todo!();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_name(&self) -> String {
|
|
|
|
self.name.clone()
|
|
|
|
}
|
|
|
|
|
2023-03-01 15:45:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Thread {
|
|
|
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.object_type = ObjectType::InvalidType;
|
|
|
|
todo!();
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:31:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn start_thread_execution() {
|
|
|
|
|
2023-02-28 14:43:40 +01:00
|
|
|
}
|