forked from Rativel/BurritOS
Add Exit Exception
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
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;
|
||||
@@ -40,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!(),
|
||||
@@ -55,12 +59,23 @@ 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 => {
|
||||
match &system.get_thread_manager().g_current_thread {
|
||||
Some(th) => {
|
||||
let th = Rc::clone(th);
|
||||
system.get_thread_manager().thread_finish(machine, th);
|
||||
Ok(MachineOk::Ok)
|
||||
},
|
||||
None => {
|
||||
Err("Current thread is None".into())
|
||||
}
|
||||
}
|
||||
},
|
||||
SC_EXEC => todo!(),
|
||||
SC_JOIN => todo!(),
|
||||
SC_CREATE => todo!(),
|
||||
@@ -121,6 +136,7 @@ fn syscall(machine: &Machine) -> Result<MachineOk, MachineError> {
|
||||
#[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;
|
||||
|
||||
@@ -132,8 +148,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
|
||||
}
|
||||
@@ -162,9 +178,9 @@ 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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user