1
0
forked from Rativel/BurritOS

Merge remote-tracking branch 'origin/thread_scheduler' into thread_scheduler

# Conflicts:
#	src/kernel/exception.rs
This commit is contained in:
Samy Solhi
2023-04-05 16:00:06 +02:00
34 changed files with 1100 additions and 148 deletions

View File

@@ -1,7 +1,9 @@
use libc::printf;
use std::rc::Rc;
use crate::simulator::{machine::{ExceptionType, Machine}, error::{MachineOk, MachineError}};
use super::system::System;
pub const SC_SHUTDOWN: u8 = 0;
pub const SC_EXIT: u8 = 1;
@@ -42,11 +44,11 @@ pub const SC_DEBUG: u8 = 34;
pub const CONSOLE_OUTPUT: u8 = 1;
// todo : returns new types, not just machine errors and machine ok
pub fn call(exception: ExceptionType, machine: &Machine) -> Result<MachineOk, MachineError> {
pub fn call(exception: &ExceptionType, machine: &mut Machine, system: &mut System) -> Result<MachineOk, MachineError> {
match exception {
ExceptionType::NoException => todo!(),
ExceptionType::SyscallException => syscall(machine),
ExceptionType::SyscallException => syscall(machine, system),
ExceptionType::PagefaultException => todo!(),
ExceptionType::ReadOnlyException => todo!(),
ExceptionType::BusErrorException => todo!(),
@@ -57,12 +59,19 @@ pub fn call(exception: ExceptionType, machine: &Machine) -> Result<MachineOk, Ma
}
}
fn syscall(machine: &Machine) -> Result<MachineOk, MachineError> {
fn syscall(machine: &mut Machine, system: &mut System) -> Result<MachineOk, MachineError> {
let call_type = machine.read_int_register(17) as u8;
match call_type {
SC_SHUTDOWN => Ok(MachineOk::Shutdown),
SC_EXIT => todo!(),
SC_EXIT => {
let th = match &system.get_thread_manager().g_current_thread {
Some(th) => th.clone(),
None => Err("Current thread is None")?
};
system.get_thread_manager().thread_finish(machine, th);
Ok(MachineOk::Ok)
},
SC_EXEC => todo!(),
SC_JOIN => todo!(),
SC_CREATE => todo!(),
@@ -96,7 +105,13 @@ fn syscall(machine: &Machine) -> Result<MachineOk, MachineError> {
SC_PERROR => todo!(),
SC_P => todo!(),
SC_V => todo!(),
SC_SEM_CREATE => todo!(),
SC_SEM_CREATE => {
let addr_name = machine.read_int_register(10);
let initial_count = machine.read_int_register((11));
let size = get_length_param(addr_name as usize, machine);
Ok(MachineOk::Ok)
},
SC_SEM_DESTROY => todo!(),
SC_LOCK_CREATE => todo!(),
SC_LOCK_DESTROY => todo!(),
@@ -120,6 +135,17 @@ fn syscall(machine: &Machine) -> Result<MachineOk, MachineError> {
}
}
fn get_length_param(addr: usize, machine: & Machine) -> usize{
let mut i = 0;
let mut c = 1;
while c!= 0 {
c = machine.read_memory(1, addr + i);
i+=1;
}
i + 1
}
fn get_string_param(addr: i64, maxlen: i64, machine: &Machine) -> Vec<char>{
let mut dest = Vec::with_capacity(maxlen as usize);
@@ -141,6 +167,7 @@ fn get_string_param(addr: i64, maxlen: i64, machine: &Machine) -> Vec<char>{
#[cfg(test)]
mod test {
use crate::kernel::exception::{SC_SHUTDOWN, SC_WRITE};
use crate::kernel::system::System;
use crate::simulator::instruction::Instruction;
use crate::simulator::machine::Machine;
@@ -152,8 +179,8 @@ mod test {
machine.write_memory(4, 0, 0b000000000000_00000_000_00000_1110011); // ecall
machine.write_memory(4, 4, 0b000000001010_00000_000_00001_0010011); // r1 <- 10
machine.run();
let mut system = System::default();
machine.run(&mut system);
// If the machine was stopped with no error, the shutdown worked
assert_ne!(machine.read_int_register(1), 10); // Check if the next instruction was executed
}
@@ -183,8 +210,8 @@ mod test {
machine.write_memory(4, 4, 0b000000000000_00000_000_10001_0010011); // r17 <- SC_SHUTDOWN
machine.write_memory(4, 8, 0b000000000000_00000_000_00000_1110011); // ecall
machine.run();
let mut system = System::default();
machine.run(&mut system);
}
}