2023-07-10 13:13:42 -05:00
|
|
|
use {
|
|
|
|
alloc::{collections::VecDeque, rc::Rc, slice, vec::Vec},
|
|
|
|
hbvm::validate::validate,
|
|
|
|
};
|
2023-07-10 06:44:11 -05:00
|
|
|
|
|
|
|
use {crate::host::TrapHandler, hbvm::vm::Vm};
|
2023-07-13 03:41:15 -05:00
|
|
|
const TIMER_QUOTIENT: usize = 100;
|
2023-06-26 07:55:37 -05:00
|
|
|
|
|
|
|
pub struct Scheduler<'a> {
|
2023-07-13 03:27:47 -05:00
|
|
|
data: VecDeque<Vm<'a, TrapHandler, TIMER_QUOTIENT>>,
|
2023-06-26 07:55:37 -05:00
|
|
|
}
|
|
|
|
|
2023-07-10 13:13:42 -05:00
|
|
|
// NOTE: This is a very simple schduler and it sucks and should be replaced with a better one
|
|
|
|
// Written By Yours Truly: Munir
|
|
|
|
|
2023-06-26 07:55:37 -05:00
|
|
|
impl Scheduler<'_> {
|
2023-07-10 06:44:11 -05:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
data: VecDeque::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn new_process(&mut self, program: Vec<u8>) {
|
|
|
|
let prog = program.clone();
|
|
|
|
let prog_arc = Rc::new(prog);
|
2023-07-10 13:13:42 -05:00
|
|
|
|
2023-07-10 06:44:11 -05:00
|
|
|
let binding = Rc::try_unwrap(prog_arc).ok().unwrap();
|
2023-07-10 13:13:42 -05:00
|
|
|
|
2023-07-10 06:44:11 -05:00
|
|
|
#[allow(clippy::redundant_else)]
|
|
|
|
if let Err(e) = validate(&program.as_slice()) {
|
|
|
|
log::error!("Program validation error: {e:?}");
|
|
|
|
} else {
|
|
|
|
log::info!("valid program");
|
|
|
|
unsafe {
|
|
|
|
let slice = slice::from_raw_parts(binding.as_ptr(), binding.len());
|
|
|
|
let mut vm = Vm::new_unchecked(&*slice, TrapHandler);
|
|
|
|
vm.memory.insert_test_page();
|
|
|
|
self.data.push_front(vm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-12 12:22:13 -05:00
|
|
|
pub fn run(&mut self) -> ! {
|
2023-07-10 06:44:11 -05:00
|
|
|
loop {
|
2023-07-13 03:27:47 -05:00
|
|
|
// If there are no programs to run then sleep.
|
2023-07-12 12:22:13 -05:00
|
|
|
if self.data.is_empty() {
|
2023-07-15 07:47:46 -05:00
|
|
|
use crate::arch::spin_loop;
|
|
|
|
spin_loop();
|
2023-07-12 12:22:13 -05:00
|
|
|
}
|
|
|
|
|
2023-07-10 06:44:11 -05:00
|
|
|
let mut prog = self.data.pop_front().unwrap();
|
2023-07-15 05:51:19 -05:00
|
|
|
let ret = prog.run();
|
|
|
|
match ret {
|
|
|
|
Ok(oki) => match oki {
|
|
|
|
hbvm::vm::VmRunOk::End => {
|
|
|
|
log::info!(
|
|
|
|
"Program ended. {} programs remaining.",
|
|
|
|
// Add one here because we pop a program
|
|
|
|
self.data.len() + 1
|
|
|
|
)
|
|
|
|
}
|
|
|
|
hbvm::vm::VmRunOk::Timer => {
|
|
|
|
log::info!("Timer exhausted. Scheduled program");
|
|
|
|
self.data.push_back(prog);
|
|
|
|
}
|
|
|
|
hbvm::vm::VmRunOk::Ecall => {
|
|
|
|
// panic!();
|
|
|
|
log::info!("{:?}", prog.registers);
|
|
|
|
self.data.push_back(prog);
|
|
|
|
}
|
|
|
|
},
|
2023-07-13 03:27:47 -05:00
|
|
|
|
2023-07-15 05:51:19 -05:00
|
|
|
Err(_) => {}
|
|
|
|
}
|
2023-07-11 03:02:40 -05:00
|
|
|
}
|
2023-07-10 06:44:11 -05:00
|
|
|
}
|
2023-06-26 07:55:37 -05:00
|
|
|
}
|