From 36f2a8336f794604add6fe6cddcc64e975045d2b Mon Sep 17 00:00:00 2001 From: able Date: Mon, 26 Jun 2023 06:36:30 -0500 Subject: [PATCH] most recent HBVM works --- Cargo.lock | 120 ++++++++++++++++++++++++++++++++++- kernel/Cargo.toml | 2 + kernel/src/host.rs | 101 +++++++++++++++++++++++++++++ kernel/src/host_functions.rs | 64 ------------------- kernel/src/kmain.rs | 39 ++++++++++-- kernel/src/lib.rs | 2 +- 6 files changed, 256 insertions(+), 72 deletions(-) create mode 100644 kernel/src/host.rs delete mode 100644 kernel/src/host_functions.rs diff --git a/Cargo.lock b/Cargo.lock index f5158d04..96497fee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -43,6 +43,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "allocator-api2" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56fc6cf8dc8c4158eed8649f9b8b0ea1518eb62b544fe9490d66fa0b349eafe9" + [[package]] name = "android_system_properties" version = "0.1.5" @@ -81,6 +87,12 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b7e4c2464d97fe331d41de9d5db0def0a96f4d823b8b32a2efd503578988973" +[[package]] +name = "beef" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a8241f3ebb85c056b509d4327ad0358fbbba6ffb340bf388f26350aeda225b1" + [[package]] name = "bit" version = "0.1.1" @@ -158,6 +170,12 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + [[package]] name = "core-foundation-sys" version = "0.8.4" @@ -227,14 +245,27 @@ dependencies = [ "syn 2.0.13", ] +[[package]] +name = "delegate" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d358e0ec5c59a5e1603b933def447096886121660fc680dc1e64a0753981fe3c" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "derive_more" version = "0.99.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" dependencies = [ + "convert_case", "proc-macro2", "quote", + "rustc_version", "syn 1.0.109", ] @@ -336,6 +367,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "getrandom" version = "0.2.8" @@ -365,13 +402,45 @@ dependencies = [ "ahash 0.8.3", ] +[[package]] +name = "hashbrown" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" +dependencies = [ + "ahash 0.8.3", + "allocator-api2", +] + +[[package]] +name = "hbasm" +version = "0.1.0" +source = "git+https://git.ablecorp.us/ableos/holey-bytes#b7c34775e4514c05bf0927bd2bd2f6de90715f0d" +dependencies = [ + "hashbrown 0.14.0", + "hbbytecode", + "lasso", + "logos", + "paste", +] + +[[package]] +name = "hbbytecode" +version = "0.1.0" +source = "git+https://git.ablecorp.us/ableos/holey-bytes#b7c34775e4514c05bf0927bd2bd2f6de90715f0d" + [[package]] name = "hbvm" version = "0.1.0" -source = "git+https://git.ablecorp.us/ableos/holey-bytes#76302b4ca8aeda6597ee51cb1cf5f59a5d385c4f" +source = "git+https://git.ablecorp.us/ableos/holey-bytes#b7c34775e4514c05bf0927bd2bd2f6de90715f0d" dependencies = [ + "delegate", + "derive_more", "hashbrown 0.13.2", + "hbbytecode", "log", + "paste", + "static_assertions", ] [[package]] @@ -468,6 +537,7 @@ dependencies = [ "embedded-graphics", "error-stack 0.3.1", "hashbrown 0.12.3", + "hbasm", "hbvm", "limine", "log", @@ -483,6 +553,16 @@ dependencies = [ "xml", ] +[[package]] +name = "lasso" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4644821e1c3d7a560fe13d842d13f587c07348a1a05d3a797152d41c90c56df2" +dependencies = [ + "ahash 0.8.3", + "hashbrown 0.13.2", +] + [[package]] name = "libc" version = "0.2.141" @@ -528,6 +608,38 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "logos" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c000ca4d908ff18ac99b93a062cb8958d331c3220719c52e77cb19cc6ac5d2c1" +dependencies = [ + "logos-derive", +] + +[[package]] +name = "logos-codegen" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc487311295e0002e452025d6b580b77bb17286de87b57138f3b5db711cded68" +dependencies = [ + "beef", + "fnv", + "proc-macro2", + "quote", + "regex-syntax", + "syn 2.0.13", +] + +[[package]] +name = "logos-derive" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbfc0d229f1f42d790440136d941afd806bc9e949e2bcb8faa813b0f00d1267e" +dependencies = [ + "logos-codegen", +] + [[package]] name = "memchr" version = "2.5.0" @@ -740,6 +852,12 @@ dependencies = [ "lock_api", ] +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "supports-color" version = "1.3.1" diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 0e831a95..2f9df3b6 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -7,6 +7,8 @@ version = "0.2.0" [dependencies] hbvm = { git = "https://git.ablecorp.us/ableos/holey-bytes" } +hbasm = { git = "https://git.ablecorp.us/ableos/holey-bytes" } + embedded-graphics = "0.7.1" diff --git a/kernel/src/host.rs b/kernel/src/host.rs new file mode 100644 index 00000000..965116f5 --- /dev/null +++ b/kernel/src/host.rs @@ -0,0 +1,101 @@ +//! the system interface + +// use { +// crate::ipc::message::Message, +// alloc::vec::Vec, +// crossbeam_queue::{ArrayQueue, SegQueue}, +// // hbvm::engine::Engine, +// log::trace, +// HostError::MemoryError, +// }; +/// Host errors +pub enum HostError { + /// A host memory error + MemoryError, +} + +// / Check f0 register for the handle +// / check f1 for the message ptr +// / check f2 for the message length +// pub fn ipc_send(engine: &mut Engine) -> Result<(), HostError> { +// let _handle = engine.registers.f0; +// let message_start = engine.registers.f1; +// let message_length = engine.registers.f2; + +// let mut ipc_msg: Vec = alloc::vec![]; + +// for x in message_start..message_start + message_length { +// let byte = engine.read_mem_addr_8(x); +// match byte { +// Ok(byte) => ipc_msg.push(byte), +// Err(_) => return Err(MemoryError), +// } +// } +// log::trace!("Message bytes {:?}", ipc_msg); +// Ok(()) +// } + +// // pub fn ipc_recv(_engine: &mut Engine) {} + +// /// Check f0 for the buffer type +// /// 0 means an unbound buffer +// /// 1 means a bound buffer +// /// Check f1 if the buffer is bound +// /// +// /// f2 Return a handle to the sender +// /// f3 returns a handle the the reciever +// pub fn ipc_mkbuf(engine: &mut Engine) { +// match engine.registers.f0 as usize { +// 0 => { +// trace!("Making a new ipc unbound buffer"); +// let _buf: SegQueue = SegQueue::new(); +// } +// 1 => { +// let buf_len = engine.registers.f1 as usize; +// trace!("Making a new ipc buffer with capacity {}", buf_len); +// let _buf: ArrayQueue = ArrayQueue::new(buf_len); +// } +// _ => {} +// } +// } + +// // pub fn rpc_discover(_engine: &mut Engine) {} +// // pub fn rpc_register(_engine: &mut Engine) {} +// // pub fn rpc_call(_engine: &mut Engine) {} + +use hbvm::vm::{ + mem::{Memory, MemoryAccessReason, PageSize}, + trap::HandleTrap, + value::Value, +}; + +/// AbleOS HBVM traphandler +pub struct TrapHandler; +impl HandleTrap for TrapHandler { + fn page_fault( + &mut self, + _: MemoryAccessReason, + _: &mut Memory, + _: u64, + _: PageSize, + _: *mut u8, + ) -> bool { + false + } + + fn invalid_op(&mut self, _: &mut [Value; 256], _: &mut usize, _: &mut Memory, _: u8) -> bool + where + Self: Sized, + { + log::trace!("Invalid opcode"); + + false + } + + fn ecall(&mut self, _: &mut [Value; 256], _: &mut usize, _: &mut Memory) + where + Self: Sized, + { + log::trace!("ableOS system call made"); + } +} diff --git a/kernel/src/host_functions.rs b/kernel/src/host_functions.rs deleted file mode 100644 index 1f39fef7..00000000 --- a/kernel/src/host_functions.rs +++ /dev/null @@ -1,64 +0,0 @@ -//! the system interface - -use { - crate::ipc::message::Message, - alloc::vec::Vec, - crossbeam_queue::{ArrayQueue, SegQueue}, - hbvm::engine::Engine, - log::trace, - HostError::MemoryError, -}; -/// Host errors -pub enum HostError { - /// A host memory error - MemoryError, -} - -/// Check f0 register for the handle -/// check f1 for the message ptr -/// check f2 for the message length -pub fn ipc_send(engine: &mut Engine) -> Result<(), HostError> { - let _handle = engine.registers.f0; - let message_start = engine.registers.f1; - let message_length = engine.registers.f2; - - let mut ipc_msg: Vec = alloc::vec![]; - - for x in message_start..message_start + message_length { - let byte = engine.read_mem_addr_8(x); - match byte { - Ok(byte) => ipc_msg.push(byte), - Err(_) => return Err(MemoryError), - } - } - log::trace!("Message bytes {:?}", ipc_msg); - Ok(()) -} - -// pub fn ipc_recv(_engine: &mut Engine) {} - -/// Check f0 for the buffer type -/// 0 means an unbound buffer -/// 1 means a bound buffer -/// Check f1 if the buffer is bound -/// -/// f2 Return a handle to the sender -/// f3 returns a handle the the reciever -pub fn ipc_mkbuf(engine: &mut Engine) { - match engine.registers.f0 as usize { - 0 => { - trace!("Making a new ipc unbound buffer"); - let _buf: SegQueue = SegQueue::new(); - } - 1 => { - let buf_len = engine.registers.f1 as usize; - trace!("Making a new ipc buffer with capacity {}", buf_len); - let _buf: ArrayQueue = ArrayQueue::new(buf_len); - } - _ => {} - } -} - -// pub fn rpc_discover(_engine: &mut Engine) {} -// pub fn rpc_register(_engine: &mut Engine) {} -// pub fn rpc_call(_engine: &mut Engine) {} diff --git a/kernel/src/kmain.rs b/kernel/src/kmain.rs index 8bd3972b..98cd84f5 100644 --- a/kernel/src/kmain.rs +++ b/kernel/src/kmain.rs @@ -3,7 +3,6 @@ use { crate::{alloc::string::ToString, arch::logging::SERIAL_CONSOLE, device_tree::DeviceTree}, clparse::Arguments, - hbvm::engine::Engine, log::{debug, trace}, spin::{Lazy, Mutex}, xml::XMLElement, @@ -42,12 +41,40 @@ pub fn kmain(cmdline: &str, bootstrap: Option<&'static [u8]>) -> ! { // TODO: schedule the disk driver from the initramfs // TODO: schedule the filesystem driver from the initramfs // TODO: schedule the init system from the initramfs + let code = "li r1, 8 +li r2, 10 +add r1, r1, r2 - let bytes = alloc::vec![0]; - let mut prog = Engine::new(bytes); - // prog.enviroment_call_table[0] = read_device_tree; - let _ = prog.run(); - prog.dump(); + +ecall" + .to_string(); + + let mut prog = alloc::vec![]; + // TODO: Port hbasm to be fully nostd + if let Err(e) = hbasm::assembly(&code, &mut prog) { + log::error!( + "Error {:?} at {:?} (`{}`)", + e.kind, + e.span.clone(), + &code[e.span], + ); + } + + use hbvm::validate::validate; + + #[allow(clippy::redundant_else)] + if let Err(e) = validate(&prog) { + log::error!("Program validation error: {e:?}"); + } else { + log::info!("valid program"); + unsafe { + use {crate::host::TrapHandler, hbvm::vm::Vm}; + let mut vm = Vm::new_unchecked(&prog, TrapHandler); + vm.memory.insert_test_page(); + log::info!("Program interrupt: {:?}", vm.run()); + log::info!("{:?}", vm.registers); + } + } // TODO: change this to a driver { diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index ada0dd76..1c712d22 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -23,7 +23,7 @@ mod allocator; mod arch; pub mod device_tree; pub mod handle; -pub mod host_functions; +pub mod host; pub mod ipc; mod kmain; mod logger;