From 8c844c3e5cbea14c0e320ac21457df602d10b678 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Sun, 2 Apr 2023 19:55:06 +0200 Subject: [PATCH] Initialize sp value for each threads (temporary workaround) --- src/kernel/thread.rs | 4 ++-- src/kernel/thread_manager.rs | 8 ++++---- src/simulator/loader.rs | 13 +++++++------ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/kernel/thread.rs b/src/kernel/thread.rs index 05d94ad..4b856c2 100644 --- a/src/kernel/thread.rs +++ b/src/kernel/thread.rs @@ -47,10 +47,10 @@ impl Thread { } } - pub fn init_thread_context(&mut self, initial_pc_reg: u64, initial_sp: i64, arg: i64) { + pub fn init_thread_context(&mut self, initial_pc_reg: u64, initial_sp: u64, arg: i64) { self.thread_context.pc = initial_pc_reg; self.thread_context.int_registers[10] = arg; - self.thread_context.int_registers[STACK_REG] = initial_sp; + self.thread_context.int_registers[STACK_REG] = initial_sp as i64; } pub fn init_simulator_context(&self, base_stack_addr: [i8; SIMULATORSTACKSIZE]) { diff --git a/src/kernel/thread_manager.rs b/src/kernel/thread_manager.rs index acecc5b..e5b0e14 100644 --- a/src/kernel/thread_manager.rs +++ b/src/kernel/thread_manager.rs @@ -86,11 +86,11 @@ impl ThreadManager { } /// Start a thread, attaching it to a process - pub fn start_thread(&mut self, thread: Rc>, owner: Process, func_pc: u64, argument: i64) { + pub fn start_thread(&mut self, thread: Rc>, owner: Process, func_pc: u64, sp_loc: u64, argument: i64) { let mut thread_m = thread.borrow_mut(); assert_eq!(thread_m.process, Option::None); thread_m.process = Option::Some(owner); - let ptr = 0; // todo addrspace + let ptr = sp_loc; // todo addrspace thread_m.init_thread_context(func_pc, ptr, argument); let base_stack_addr: [i8; SIMULATORSTACKSIZE] = [0; SIMULATORSTACKSIZE]; // todo AllocBoundedArray thread_m.init_simulator_context(base_stack_addr); @@ -206,7 +206,7 @@ mod test { #[ignore = "Pas encore terminé, contient des bugs"] fn test_thread_context() { let mut machine = Machine::init_machine(); - let loader = loader::Loader::new("./test/riscv_instructions/simple_arithmetics/unsigned_addition", &mut machine, 0).expect("IO Error"); + let (loader, ptr) = loader::Loader::new("./test/riscv_instructions/simple_arithmetics/unsigned_addition", &mut machine, 0).expect("IO Error"); let start_pc = loader.elf_header.entrypoint; let system = &mut System::default(); @@ -215,7 +215,7 @@ mod test { system.get_thread_manager().get_g_alive().push(Rc::clone(&thread1)); let owner = Process { num_thread: 0 }; - system.get_thread_manager().start_thread(Rc::clone(&thread1), owner, start_pc, -1); + system.get_thread_manager().start_thread(Rc::clone(&thread1), owner, start_pc, ptr, -1); debug_assert_eq!(thread1.borrow_mut().thread_context.pc, start_pc); let to_run = system.get_thread_manager().find_next_to_run().unwrap(); diff --git a/src/simulator/loader.rs b/src/simulator/loader.rs index 9bbe132..a3350e9 100644 --- a/src/simulator/loader.rs +++ b/src/simulator/loader.rs @@ -308,16 +308,18 @@ pub enum LoaderError { impl Loader { - pub fn new(path: &str, machine: &mut Machine, start_index: usize) -> Result { + pub fn new(path: &str, machine: &mut Machine, start_index: usize) -> Result<(Self, u64), LoaderError> { let loader = Self::load_and_parse(path)?; - loader.load_into_machine(machine, start_index)?; - Ok(loader) + let end_alloc = loader.load_into_machine(machine, start_index)?; + Ok((loader, end_alloc)) } - fn load_into_machine(&self, machine: &mut Machine, start_index: usize) -> Result{ + fn load_into_machine(&self, machine: &mut Machine, start_index: usize) -> Result { + let mut end_index = 0; for i in 0..self.sections.len() { let section = &self.sections[i]; if section.does_flag_contains_key(FlagValue::ShfAlloc) { + end_index = section.virt_addr + section.section_size; // Can allocate to machine memory for j in (0..section.section_size as usize).step_by(4) { let mut buf: [u8; 4] = [0; 4]; @@ -328,8 +330,7 @@ impl Loader { } } } - let last = self.sections.last().ok_or(LoaderError::ParsingError)?; - Ok(start_index as u64 + last.virt_addr + last.section_size) + Ok(start_index as u64 + end_index) } fn load_and_parse(path: &str) -> Result {