2023-10-27 20:26:04 -05:00
|
|
|
//! Environment call handling routines
|
|
|
|
use {
|
|
|
|
super::{mem::Memory, Vm},
|
|
|
|
crate::{arch, ipc::buffer::IpcBuffer, kmain::IPC_BUFFERS},
|
2023-10-28 08:43:32 -05:00
|
|
|
alloc::string::String,
|
2023-10-27 20:26:04 -05:00
|
|
|
log::{debug, error, info, trace, warn},
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn handler(vm: &mut Vm) {
|
2023-10-28 08:28:07 -05:00
|
|
|
let r1 = vm.registers[1].cast::<u64>();
|
2023-10-27 20:26:04 -05:00
|
|
|
|
2023-10-28 23:14:36 -05:00
|
|
|
// debug!("Ecall number {:?}", r1);
|
|
|
|
// trace!("Register dump: {:?}", vm.registers);
|
2023-10-27 20:26:04 -05:00
|
|
|
|
2023-10-28 08:28:07 -05:00
|
|
|
match r1 {
|
2023-10-27 20:26:04 -05:00
|
|
|
0 => {
|
|
|
|
// TODO: explode computer
|
|
|
|
// hello world ecall
|
|
|
|
for x in 0u64..=255 {
|
|
|
|
vm.registers[x as usize] = x.into();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
1 => {
|
|
|
|
// Make buffer
|
|
|
|
|
2023-10-28 08:28:07 -05:00
|
|
|
let bounded = match vm.registers[2].cast::<u64>() {
|
2023-10-27 20:26:04 -05:00
|
|
|
0 => false,
|
|
|
|
1 => true,
|
|
|
|
_ => {
|
|
|
|
panic!("Bad");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-28 08:28:07 -05:00
|
|
|
let length = vm.registers[3].cast::<u64>();
|
2023-10-27 20:26:04 -05:00
|
|
|
|
|
|
|
let mut buffs = IPC_BUFFERS.lock();
|
|
|
|
let abc;
|
|
|
|
|
|
|
|
match bounded {
|
|
|
|
false => {
|
|
|
|
abc = IpcBuffer::new(false, 0);
|
|
|
|
}
|
|
|
|
true => {
|
|
|
|
abc = IpcBuffer::new(true, length);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let buff_id = arch::hardware_random_u64();
|
|
|
|
buffs.insert(buff_id, abc);
|
|
|
|
debug!("Buffer ID: {}", buff_id);
|
2023-10-29 07:27:10 -05:00
|
|
|
vm.registers[1] = hbvm::value::Value(buff_id);
|
2023-10-27 20:26:04 -05:00
|
|
|
}
|
|
|
|
2 => {
|
|
|
|
// Delete buffer
|
|
|
|
}
|
|
|
|
3 => {
|
|
|
|
// Send a message to a buffer
|
2023-10-28 08:43:32 -05:00
|
|
|
let r2 = vm.registers[2].cast::<u64>();
|
|
|
|
let r3 = vm.registers[3].cast::<u64>();
|
|
|
|
let r4 = vm.registers[4].cast::<u64>();
|
2023-10-27 20:26:04 -05:00
|
|
|
|
2023-10-28 08:43:32 -05:00
|
|
|
let buffer_id = r2;
|
|
|
|
let mem_addr = r3;
|
|
|
|
let length = r4 as usize;
|
2023-10-27 20:26:04 -05:00
|
|
|
trace!("IPC address: {:?}", mem_addr);
|
|
|
|
use alloc::vec::Vec;
|
|
|
|
|
|
|
|
match buffer_id {
|
|
|
|
1 => {
|
2023-10-28 08:43:32 -05:00
|
|
|
log_msg_handler(vm, mem_addr, length);
|
2023-10-27 20:26:04 -05:00
|
|
|
}
|
|
|
|
buffer_id => {
|
2023-10-29 07:27:10 -05:00
|
|
|
let mut buffs = IPC_BUFFERS.lock();
|
|
|
|
let mut buff = buffs.get_mut(&buffer_id).unwrap();
|
|
|
|
let mut msg_vec = vec![];
|
|
|
|
|
|
|
|
for x in 0..(length as isize) {
|
|
|
|
let xyz = mem_addr as *const u8;
|
|
|
|
let value = unsafe { xyz.offset(x).read() };
|
|
|
|
msg_vec.push(value);
|
|
|
|
}
|
|
|
|
buff.push(msg_vec.clone());
|
|
|
|
info!("Message {:?} has been sent to {}", msg_vec, buffer_id);
|
|
|
|
drop(buffs);
|
2023-10-27 20:26:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-29 07:27:10 -05:00
|
|
|
4 => {
|
|
|
|
let r2 = vm.registers[2].cast::<u64>();
|
|
|
|
|
|
|
|
let mut buffs = IPC_BUFFERS.lock();
|
|
|
|
let mut buff = buffs.get_mut(&r2).unwrap();
|
|
|
|
let msg = buff.pop();
|
|
|
|
info!("Recieve {:?} from Buffer({})", msg, r2);
|
|
|
|
}
|
2023-10-27 20:26:04 -05:00
|
|
|
// 5
|
|
|
|
_ => {
|
2023-10-28 08:28:07 -05:00
|
|
|
log::error!("Syscall unknown {:?}", r1);
|
2023-10-27 20:26:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-28 08:43:32 -05:00
|
|
|
|
|
|
|
fn log_msg_handler(vm: &mut Vm, mem_addr: u64, length: usize) -> Result<(), LogError> {
|
2023-10-28 23:14:36 -05:00
|
|
|
// let message_length = 8 + 8 + 8;
|
2023-10-28 08:43:32 -05:00
|
|
|
let mut msg_vec = vec![];
|
2023-10-28 23:14:36 -05:00
|
|
|
|
|
|
|
for x in 0..(length as isize) {
|
2023-10-28 08:43:32 -05:00
|
|
|
let xyz = mem_addr as *const u8;
|
|
|
|
let value = unsafe { xyz.offset(x).read() };
|
2023-10-28 23:14:36 -05:00
|
|
|
// info!("{} - {:x?} - {}", value, value, value as char);
|
2023-10-28 08:43:32 -05:00
|
|
|
msg_vec.push(value);
|
|
|
|
}
|
2023-10-28 23:14:36 -05:00
|
|
|
let log_level = msg_vec.pop().unwrap();
|
|
|
|
let strr = String::from_utf8(msg_vec).unwrap();
|
|
|
|
// info!("{}", strr);
|
2023-10-28 08:43:32 -05:00
|
|
|
|
|
|
|
// use LogLevel::*;
|
2023-10-28 23:14:36 -05:00
|
|
|
let ll = match log_level {
|
|
|
|
48 => error!("{}", strr),
|
|
|
|
49 => warn!("{}", strr),
|
|
|
|
50 => info!("{}", strr),
|
|
|
|
51 => debug!("{}", strr),
|
|
|
|
52 => trace!("{}", strr),
|
|
|
|
_ => return Err(LogError::InvalidLogFormat),
|
|
|
|
};
|
2023-10-28 08:43:32 -05:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-29 07:27:10 -05:00
|
|
|
#[derive(Debug)]
|
2023-10-28 08:43:32 -05:00
|
|
|
pub enum LogError {
|
|
|
|
InvalidLogFormat,
|
|
|
|
}
|
2023-10-28 23:14:36 -05:00
|
|
|
use {alloc::vec, log::Record};
|