loader now return better understanable errors, fix when compiler when to use bss section, add join exception, fix get_string_param, add support for thread in obbAddr, add a test

This commit is contained in:
Quentin Legot
2023-04-13 00:18:35 +02:00
parent 05f72af035
commit f144438490
6 changed files with 130 additions and 34 deletions

View File

@ -3,7 +3,7 @@ use std::{cell::RefCell, rc::Rc};
use crate::{simulator::{machine::{ExceptionType, Machine}, error::{MachineOk, MachineError}}};
use crate::kernel::synch::{Lock, Semaphore};
use super::{system::System, thread::Thread};
use super::{system::{System, self}, thread::Thread};
/// The halt system call. Stops Burritos.
pub const SC_SHUTDOWN: u8 = 0;
@ -142,7 +142,7 @@ fn syscall(machine: &mut Machine, system: &mut System) -> Result<MachineOk, Mach
Ok(MachineOk::Ok)
},
SC_EXEC => todo!(),
SC_JOIN => todo!(),
SC_JOIN => sc_join(machine, system),
SC_CREATE => todo!(),
SC_OPEN => todo!(),
SC_READ => todo!(),
@ -231,7 +231,7 @@ fn sc_v(machine: &mut Machine, system: &mut System) -> Result<MachineOk, Machine
fn sc_sem_create(machine: &mut Machine, system: &mut System) -> Result<MachineOk, MachineError> {
let addr_name = machine.read_int_register(10) as usize;
let initial_count = machine.read_int_register(11) as i32;
let size = get_length_param(addr_name as usize, machine);
let size = get_length_param(addr_name, machine);
let _name = get_string_param(addr_name, size, machine);
match initial_count < 0 {
true => Err(format!("Initial_count < 0"))?,
@ -262,6 +262,7 @@ fn sc_new_thread(machine: &mut Machine, system: &mut System) -> Result<MachineOk
let n_thread = Thread::new(thread_name.as_str());
let n_thread = Rc::new(RefCell::new(n_thread));
let tid = system.get_thread_manager().get_obj_addrs().add_thread(Rc::clone(&n_thread));
let current_thread = match system.get_thread_manager().get_g_current_thread() {
Some(th) => {
Rc::clone(th)
@ -274,25 +275,49 @@ fn sc_new_thread(machine: &mut Machine, system: &mut System) -> Result<MachineOk
if let Some(process) = current_thread.get_process_owner() {
system.get_thread_manager().start_thread(n_thread, Rc::clone(&process), func as u64, current_thread.thread_context.int_registers[2] as u64, args);
// TODO changé la valeur de sp quand on supportera les addresses virtuels
// machine.write_fp_register(10, tid); // tid obtenu en faisant int32_t tid = g_object_addrs->AddObject(ptThread);
machine.write_int_register(10, tid as i64);
Ok(MachineOk::Ok)
} else {
return Err("Process owner of current thread is none")?;
}
}
fn get_length_param(addr: usize, machine: & Machine) -> usize{
fn sc_join(machine: &mut Machine, system: &mut System) -> Result<MachineOk, MachineError> {
let tid = machine.read_int_register(10);
let p_thread = system.get_thread_manager().get_obj_addrs().search_thread(tid as i32);
match p_thread {
Some(_) => {
if let Some(current_thread) = system.get_thread_manager().get_g_current_thread() {
let rc = Rc::clone(current_thread);
system.get_thread_manager().thread_join(machine, rc);
Ok(MachineOk::Ok)
} else {
Ok(MachineOk::Ok)
}
},
None => {
// Thread already terminated (type set to INVALID_TYPE) or call on an object
// that is not a thread
// Exit with no error code since we cannot separate the two cases
Ok(MachineOk::Ok)
}
}
}
fn get_length_param(addr: usize, machine: & Machine) -> usize {
let mut i = 0;
let mut c = 1;
while c!= 0 {
while c != 0 {
c = machine.read_memory(1, addr + i);
i+=1;
i +=1;
}
println!("addr: {:x}, i: {}", addr, i + 1);
i + 1
}
fn get_string_param(addr: usize, maxlen: usize, machine: &Machine) -> Vec<char>{
fn get_string_param(addr: usize, maxlen: usize, machine: &Machine) -> Vec<char> {
let mut dest = Vec::with_capacity(maxlen);
let mut i: usize = 0;
@ -300,12 +325,10 @@ fn get_string_param(addr: usize, maxlen: usize, machine: &Machine) -> Vec<char>{
while c != 0 && i < maxlen {
c = machine.read_memory(1, addr + i) as u8;
//dest.push(c as char);
dest[i] = c as char;
dest.push(c as char);
i += 1;
}
dest[maxlen - 1] = '\0';
dest.push('\0');
dest
}