From 97250e98deb62a471a05653da3e96d9f6cd1ba88 Mon Sep 17 00:00:00 2001 From: able Date: Sun, 29 Oct 2023 07:27:10 -0500 Subject: [PATCH] More work on the psuedo STD lib and IPC --- kernel/src/holeybytes/ecah.rs | 25 +++++++++++++++++++++--- kernel/src/ipc/buffer.rs | 22 +++++++++++++++++++-- kernel/src/kmain.rs | 14 ++++++++++++-- repbuild/hblib/std.rhai | 32 +++++++++++++++++++++++++++++-- repbuild/holeybytes/ecall.rhai | 4 ++-- repbuild/holeybytes/main.rhai | 9 ++++++--- repbuild/holeybytes/vfs.rhai | 13 +++++++++++++ repbuild/holeybytes/vfs_test.rhai | 19 ++++++++++++++++++ repbuild/limine.cfg | 3 +++ repbuild/src/main.rs | 5 +++++ 10 files changed, 132 insertions(+), 14 deletions(-) create mode 100644 repbuild/holeybytes/vfs.rhai create mode 100644 repbuild/holeybytes/vfs_test.rhai diff --git a/kernel/src/holeybytes/ecah.rs b/kernel/src/holeybytes/ecah.rs index 45683ea..932e087 100644 --- a/kernel/src/holeybytes/ecah.rs +++ b/kernel/src/holeybytes/ecah.rs @@ -47,6 +47,7 @@ pub fn handler(vm: &mut Vm) { let buff_id = arch::hardware_random_u64(); buffs.insert(buff_id, abc); debug!("Buffer ID: {}", buff_id); + vm.registers[1] = hbvm::value::Value(buff_id); } 2 => { // Delete buffer @@ -66,14 +67,31 @@ pub fn handler(vm: &mut Vm) { match buffer_id { 1 => { log_msg_handler(vm, mem_addr, length); - // error!("Logging via IPC isn't quite ready") } buffer_id => { - info!("Message has been sent to {}", buffer_id) + 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); } } } - // 4 + 4 => { + let r2 = vm.registers[2].cast::(); + + let mut buffs = IPC_BUFFERS.lock(); + let mut buff = buffs.get_mut(&r2).unwrap(); + let msg = buff.pop(); + info!("Recieve {:?} from Buffer({})", msg, r2); + } // 5 _ => { log::error!("Syscall unknown {:?}", r1); @@ -108,6 +126,7 @@ fn log_msg_handler(vm: &mut Vm, mem_addr: u64, length: usize) -> Result<(), LogE Ok(()) } +#[derive(Debug)] pub enum LogError { InvalidLogFormat, } diff --git a/kernel/src/ipc/buffer.rs b/kernel/src/ipc/buffer.rs index 8d17e73..1823413 100644 --- a/kernel/src/ipc/buffer.rs +++ b/kernel/src/ipc/buffer.rs @@ -11,8 +11,8 @@ pub enum BufferTypes { } /// Interproccess buffer pub struct IpcBuffer { - protocol: Protocol, - buffer: BufferTypes, + pub protocol: Protocol, + pub buffer: BufferTypes, } impl IpcBuffer { @@ -48,6 +48,24 @@ impl IpcBuffer { pub fn validate_messages(&mut self) -> Result<(), IpcError> { Ok(()) } + pub fn push(&mut self, msg: Message) { + match &self.buffer { + BufferTypes::Unbound(buff) => buff.push(msg.clone()), + BufferTypes::Bound(buff) => { + let _ = buff.push(msg.clone()); + } + }; + } + pub fn pop(&mut self) -> Message { + let msg = match &self.buffer { + BufferTypes::Unbound(buff) => buff.pop(), + BufferTypes::Bound(buff) => buff.pop(), + }; + match msg { + Some(msg) => return msg, + None => panic!("Recieving msg error. No messages!"), + } + } } /// Interprocess Communication Errors pub enum IpcError { diff --git a/kernel/src/kmain.rs b/kernel/src/kmain.rs index 36041dd..21de41f 100644 --- a/kernel/src/kmain.rs +++ b/kernel/src/kmain.rs @@ -2,7 +2,11 @@ use {hashbrown::HashMap, hbvm::mem::Address}; -use crate::{capabilities, holeybytes::ExecThread, ipc::buffer::IpcBuffer}; +use crate::{ + capabilities, + holeybytes::ExecThread, + ipc::buffer::{self, IpcBuffer}, +}; // use crate::arch::sloop; use { crate::{ @@ -68,7 +72,13 @@ pub static DEVICE_TREE: Lazy> = Lazy::new(|| { use alloc::vec::Vec; pub type IpcBuffers = HashMap; pub static IPC_BUFFERS: Lazy> = Lazy::new(|| { - let bufs = HashMap::new(); + let mut bufs = HashMap::new(); + let log_buffer = IpcBuffer::new(false, 0); + let file_buffer = IpcBuffer::new(false, 0); + + bufs.insert(1, log_buffer); + bufs.insert(2, file_buffer); + Mutex::new(bufs) }); diff --git a/repbuild/hblib/std.rhai b/repbuild/hblib/std.rhai index 1d64a83..a1f286c 100644 --- a/repbuild/hblib/std.rhai +++ b/repbuild/hblib/std.rhai @@ -1,4 +1,3 @@ - fn ipc_send(buffer_id, mem_addr, length){ // set the ecall li8(r1, 3); @@ -8,11 +7,33 @@ fn ipc_send(buffer_id, mem_addr, length){ // set the length li64(r4, length); // ecall - eca(); + eca(); +} + + +fn ipc_recv(buffer_id){ + li8(r1, 4); + eca(); +} + + +fn ipc_make_bound_buffer(length) { + li8(r1, 1); + li8(r2, 1); + li64(r3, length); + eca(); + + // The ipc buffer id is in r1 } private fn log(log_level, string){ + // This is NOT the final format let str = data::str(string + log_level); + + // 1 byte for the log level + // 8 bytes for the length to the message + // 8 bytes for the pointer to the string + ipc_send(1, str, str.len); } @@ -23,3 +44,10 @@ fn Info(string) {log(2, string);} // because of this all of the log levels are upper case fn Debug(string) {log(3, string);} fn Trace(string) {log(4, string);} + + +fn open(string_path){ + let file_path = data::str("Meow meow meow bark bark bark"); + ipc_send(2, file_path, file_path.len); +} +fn write(){} \ No newline at end of file diff --git a/repbuild/holeybytes/ecall.rhai b/repbuild/holeybytes/ecall.rhai index 996e706..a6e287e 100644 --- a/repbuild/holeybytes/ecall.rhai +++ b/repbuild/holeybytes/ecall.rhai @@ -1,3 +1,3 @@ -li8 (r1, 0x69); -// eca (); +li8 (r1, 2); +eca (); tx (); diff --git a/repbuild/holeybytes/main.rhai b/repbuild/holeybytes/main.rhai index 1d66659..c832874 100644 --- a/repbuild/holeybytes/main.rhai +++ b/repbuild/holeybytes/main.rhai @@ -2,10 +2,13 @@ import "repbuild/hblib/std" as std; fn main(){ std::Error(":+)"); - std::Debug("ABC"); - std::Info("Hello, world!"); - std::Trace("Trace Deez"); std::Warn("Your mom fell in a well!"); + std::Info("Hello, world!"); + std::Debug("XYZ"); + std::Trace("Trace Deez"); + + // let ret = std::open("/file.hbf"); + // std::Info("" + ret); tx(); } diff --git a/repbuild/holeybytes/vfs.rhai b/repbuild/holeybytes/vfs.rhai new file mode 100644 index 0000000..71a5d21 --- /dev/null +++ b/repbuild/holeybytes/vfs.rhai @@ -0,0 +1,13 @@ +/// Act as a shim of a virtual file system that recieves one message from buffer 2 +import "repbuild/hblib/std" as std; + + +fn main() { + + // jmp here + +} + + +main(); +tx(); \ No newline at end of file diff --git a/repbuild/holeybytes/vfs_test.rhai b/repbuild/holeybytes/vfs_test.rhai new file mode 100644 index 0000000..3a37b07 --- /dev/null +++ b/repbuild/holeybytes/vfs_test.rhai @@ -0,0 +1,19 @@ +import "repbuild/hblib/std" as std; + +fn main(){ + std::Info("Trying to open a file."); + // let ret = std::open("/file.hbf"); + // std::Info("File return " + ret); + + std::ipc_make_bound_buffer(1000); + + let str = data::str("ABC XYZ"); + std::ipc_send(2, str, str.len); + + std::ipc_recv(2); + + + tx(); +} + +main(); \ No newline at end of file diff --git a/repbuild/limine.cfg b/repbuild/limine.cfg index 840548e..a8d8cb4 100644 --- a/repbuild/limine.cfg +++ b/repbuild/limine.cfg @@ -26,3 +26,6 @@ TERM_BACKDROP=008080 MODULE_PATH=boot:///main.hbf MODULE_CMDLINE="" + + MODULE_PATH=boot:///vfs_test.hbf + MODULE_CMDLINE="" \ No newline at end of file diff --git a/repbuild/src/main.rs b/repbuild/src/main.rs index 41a010c..2390e5a 100644 --- a/repbuild/src/main.rs +++ b/repbuild/src/main.rs @@ -162,6 +162,11 @@ fn get_fs() -> Result, io::Error> { &mut fs.root_dir().create_file("main.hbf")?, )?; + io::copy( + &mut File::open("target/holeybytes/vfs_test.hbf")?, + &mut fs.root_dir().create_file("vfs_test.hbf")?, + )?; + drop(bootdir); Ok(fs) }