From 01cdd68b284fc14353a8a4003188eb08ea054813 Mon Sep 17 00:00:00 2001 From: Able Date: Wed, 12 Apr 2023 13:08:07 -0500 Subject: [PATCH] cleanup + rustfmt config --- kernel/data/test.wat | 8 +- kernel/src/handle.rs | 39 ++++++---- kernel/src/interp/host_functions.rs | 76 +++++++++++++++++++ kernel/src/{interp.rs => interp/mod.rs} | 96 ++++++++++++------------ kernel/src/interp/objects.rs | 11 +++ kernel/src/kmain.rs | 22 +++--- rustfmt.toml | 2 + test.wasm | Bin 205 -> 198 bytes 8 files changed, 175 insertions(+), 79 deletions(-) create mode 100644 kernel/src/interp/host_functions.rs rename kernel/src/{interp.rs => interp/mod.rs} (73%) create mode 100644 kernel/src/interp/objects.rs create mode 100644 rustfmt.toml diff --git a/kernel/data/test.wat b/kernel/data/test.wat index c8ff930..8473c2c 100644 --- a/kernel/data/test.wat +++ b/kernel/data/test.wat @@ -8,7 +8,7 @@ (memory (export "memory") 1) (func - (export "start")(result i32) + (export "start")(result i64) ;; Copy into memory the object name (memory.init 0 (i32.const 0) ;; target offset @@ -27,10 +27,10 @@ i32.const 5 call $co - i32.const 6 - i32.const 1 + ;; i32.const 6 + ;; i32.const 1 - call $roa + ;; call $roa ) ) diff --git a/kernel/src/handle.rs b/kernel/src/handle.rs index c1c82f9..6d1b748 100644 --- a/kernel/src/handle.rs +++ b/kernel/src/handle.rs @@ -1,37 +1,50 @@ -use core::fmt::{self, Formatter}; - use crate::arch::hardware_random_u64; +use alloc::vec::Vec; +use core::fmt::{self, Formatter}; +#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)] +pub struct OSHandle { + pub id: u64, +} -#[derive(Debug, Eq, Hash, PartialEq)] +impl OSHandle { + pub fn new_from_u64(id: u64) -> Self { + Self { id } + } + pub fn random_new() -> Self { + Self { + id: hardware_random_u64(), + } + } +} + +#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)] pub struct Handle { - pub handle_data: i64, + id: OSHandle, perms: Permissions, } impl Handle { pub fn new() -> Handle { Handle { - handle_data: hardware_random_u64() as i64, + id: OSHandle::random_new(), perms: Permissions::new(), } } + + pub fn as_u64(&self) -> u64 { + self.id.id + } } impl fmt::Display for Handle { fn fmt(&self, w: &mut Formatter<'_>) -> Result<(), core::fmt::Error> { - write!(w, "{}", self.handle_data); + write!(w, "{:?}", self.id); Ok(()) } } -impl Into for Handle { - fn into(self) -> i64 { - self.handle_data - // (abc[0..3] as i64, abc[4..8] as i64) - } -} +#[derive(PartialEq, Hash, Eq, Debug, Clone, Copy)] -#[derive(PartialEq, Hash, Eq, Debug)] pub struct Permissions { edit_children: bool, edit_attributes: bool, diff --git a/kernel/src/interp/host_functions.rs b/kernel/src/interp/host_functions.rs new file mode 100644 index 0000000..401e7b4 --- /dev/null +++ b/kernel/src/interp/host_functions.rs @@ -0,0 +1,76 @@ +use { + crate::interp::{HFIDT, OBJECTS}, + alloc::string::String, + log::trace, + wasmi::{Caller, TypedFunc}, +}; + +use super::{HostState, WasmContext}; + +pub fn host_register_idt_handler( + caller: Caller<'_, HostState>, + interupt_number: i32, + address_start: i32, + length_of_string: i32, +) -> i32 { + // TODO: get the proc_id to address which function it is + // TODO: Register the function name and proc_id into the idt handler + + let mem = caller.get_export("memory").unwrap().into_memory().unwrap(); + let mem_array = mem.data(&caller); + let mut name = String::new(); + for i in address_start..(address_start + length_of_string) { + let ch = mem_array[i as usize] as char; + name.push(ch); + } + + let index = interupt_number as usize; + + let hf = HFIDT.lock(); + + // hf.insert(index); + + trace!("{}", name); + 0 +} + +use crate::interp::Handle; + +pub fn host_make_object( + mut caller: Caller<'_, HostState>, + address_start: i32, + length_of_string: i32, +) -> i64 { + trace!( + "Called with addr {{ start {} length {} }}", + address_start, + length_of_string + ); + let mem = caller.get_export("memory").unwrap().into_memory().unwrap(); + let mem_array = mem.data(&caller); + let mut name = String::new(); + for i in address_start..(address_start + length_of_string) { + let ch = mem_array[i as usize] as char; + name.push(ch); + } + trace!("Object Name {}", name); + let hand = Handle::new(); + { + let binding = OBJECTS; + let mut olock = binding.lock(); + let obj = xml::XMLElement::new(name); + + olock.push(Some(obj)) + } + caller.data_mut().handles.push(hand); + // hand.into() + hand.as_u64().try_into().unwrap() +} + +pub type WFIDT = TypedFunc<(), ()>; + +fn get_fn_from_wc(wc: WasmContext, function_name: String) -> WFIDT { + wc.instance + .get_typed_func(wc.store, &function_name) + .unwrap() +} diff --git a/kernel/src/interp.rs b/kernel/src/interp/mod.rs similarity index 73% rename from kernel/src/interp.rs rename to kernel/src/interp/mod.rs index 4d5b1b6..550cd0b 100644 --- a/kernel/src/interp.rs +++ b/kernel/src/interp/mod.rs @@ -1,11 +1,19 @@ -use alloc::string::String; -use alloc::vec::Vec; -use log::trace; -use wasmi::Instance; -use wasmi::{Caller, Func, Linker, Module, Store}; -use xml::XMLElement; - -use crate::handle; +mod host_functions; +mod objects; +use { + crate::{ + handle::{self, Handle}, + interp::{host_functions::host_make_object, objects::OBJECTS}, + }, + alloc::{string::String, vec::Vec}, + hashbrown::HashMap, + log::trace, + spin::{Lazy, Mutex}, + wasmi::{Caller, Error, Func, Instance, Linker, Module, Store, TypedFunc}, + xml::XMLElement, +}; +// Seperate use statement +use alloc::vec; #[derive(Debug)] @@ -16,8 +24,7 @@ pub struct WasmContext { } pub fn wasm() -> Result<(), wasmi::Error> { - use wasmi::Config; - use wasmi::Engine; + use wasmi::{Config, Engine}; let mut conf = Config::default(); conf.wasm_bulk_memory(true); // conf., @@ -25,12 +32,12 @@ pub fn wasm() -> Result<(), wasmi::Error> { // trace!("Engine constructed"); // let wasm = include_bytes!("../../wasm_syscall_test.wasm"); - let wasm = include_bytes!("../../test.wasm"); + let wasm = include_bytes!("../../../test.wasm"); // trace!("Loading WASM binary"); let module = Module::new(&engine, &wasm[..]).unwrap(); // trace!("Constructing wasm module"); - let hs = HostState {}; + let hs = HostState { handles: vec![] }; let mut store = Store::new(&engine, hs); // trace!("constructing host store"); @@ -40,6 +47,7 @@ pub fn wasm() -> Result<(), wasmi::Error> { ); let mut linker = >::new(&engine); + linker.define( "host", "read_mem_addr", @@ -51,6 +59,12 @@ pub fn wasm() -> Result<(), wasmi::Error> { ), )?; + linker.define( + "host", + "register_idt_handler", + Func::wrap(&mut store, host_functions::host_register_idt_handler), + )?; + linker.define( "host", "read_object_attribute", @@ -70,51 +84,24 @@ pub fn wasm() -> Result<(), wasmi::Error> { let version = instance.get_global(&store, "VERSION"); // trace!("Version: {:?}", version); - let hello = instance.get_typed_func::<(), (i32, i32)>(&store, "start")?; + let hello = instance.get_typed_func::<(), i64>(&store, "start")?; let ret = hello.call(&mut store, ())?; - trace!("Called _start got return of {:?}", ret); + trace!("Called start got return of {:?}", ret); Ok(()) } + #[derive(Clone, Debug)] -pub struct HostState {} +pub struct HostState { + handles: Vec, +} pub fn read_memory_address(caller: Caller<'_, HostState>, address: i32) -> i32 { trace!("Address: {}", address); // let obj = host_make_object(caller, 16, 23); 0 } -use crate::kmain::OBJECTS; -pub fn host_make_object( - caller: Caller<'_, HostState>, - address_start: i32, - length_of_string: i32, -) -> i64 { - trace!( - "Called with addr {{ start {} length {} }}", - address_start, - length_of_string - ); - let mem = caller.get_export("memory").unwrap().into_memory().unwrap(); - let mem_array = mem.data(&caller); - let mut name = String::new(); - for i in address_start..(address_start + length_of_string) { - let ch = mem_array[i as usize] as char; - name.push(ch); - } - trace!("Object Name {}", name); - let hand = handle::Handle::new(); - { - let binding = OBJECTS; - let mut olock = binding.lock(); - let obj = XMLElement::new(name); - - olock.insert(handle::Handle::new(), obj); - } - - hand.into() -} pub fn host_read_object_attribute( caller: Caller<'_, HostState>, @@ -141,8 +128,7 @@ pub fn host_read_object_attribute( } pub fn build_wasm_context(bytes: Vec) -> Result { - use wasmi::Config; - use wasmi::Engine; + use wasmi::{Config, Engine}; let mut conf = Config::default(); conf.wasm_bulk_memory(true); // conf., @@ -150,12 +136,12 @@ pub fn build_wasm_context(bytes: Vec) -> Result { // trace!("Engine constructed"); // let wasm = include_bytes!("../../wasm_syscall_test.wasm"); - let wasm = include_bytes!("../../test.wasm"); + let wasm = include_bytes!("../../../test.wasm"); // trace!("Loading WASM binary"); let module = Module::new(&engine, &wasm[..]).unwrap(); // trace!("Constructing wasm module"); - let hs = HostState {}; + let hs = HostState { handles: vec![] }; let mut store = Store::new(&engine, hs); // trace!("constructing host store"); @@ -200,3 +186,15 @@ pub fn build_wasm_context(bytes: Vec) -> Result { Ok(wc) } + +pub type HostFunctionIDT = HashMap; +pub struct WCFunction { + wc: WasmContext, + function: TypedFunc<(), ()>, +} + +pub static HFIDT: Lazy> = Lazy::new(|| { + let mut hfidt = HashMap::new(); + + Mutex::new(hfidt) +}); diff --git a/kernel/src/interp/objects.rs b/kernel/src/interp/objects.rs new file mode 100644 index 0000000..acaf3a2 --- /dev/null +++ b/kernel/src/interp/objects.rs @@ -0,0 +1,11 @@ +use alloc::vec; +use alloc::vec::Vec; + +use spin::{Lazy, Mutex}; + +pub type HostObjects = Vec>; + +pub const OBJECTS: Lazy> = Lazy::new(|| { + let mut obj = vec![]; + Mutex::new(obj) +}); diff --git a/kernel/src/kmain.rs b/kernel/src/kmain.rs index 01f4a9f..19388f4 100644 --- a/kernel/src/kmain.rs +++ b/kernel/src/kmain.rs @@ -2,6 +2,7 @@ // use std::collections::HashMap; +use alloc::vec::Vec; use log::{info, trace}; use spin::{Lazy, Mutex}; @@ -44,24 +45,19 @@ pub fn kmain(cmdline: &str, bootstrap: Option<&'static [u8]>) -> ! { // kcmd.set_attribute("") // OBJECTS.lock().insert(hnd, kcmd); - // let abc = interp::wasm(); + let abc = interp::wasm(); - // trace!("{:?}", abc); + trace!("{:?}", abc); - let sch = SCHEDULER; - let mut sch = sch.lock(); - let wc = interp::build_wasm_context(alloc::vec::Vec::new()).unwrap(); - sch.schedule(wc, crate::schedule::ContextWake::None); + // let sch = SCHEDULER; + // let mut sch = sch.lock(); + // let wc = interp::build_wasm_context(alloc::vec::Vec::new()).unwrap(); + // sch.schedule(wc, crate::schedule::ContextWake::None); - sch.run(); + // sch.run(); - // crate::arch::sloop() + crate::arch::sloop() } -pub const OBJECTS: Lazy>> = Lazy::new(|| { - let mut obj: HashMap = HashMap::new(); - Mutex::new(obj) -}); -use hashbrown::HashMap; pub const SCHEDULER: Lazy> = Lazy::new(|| { let mut sch = Scheduler::new(); diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..547b7dc --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,2 @@ +hex_literal_case = "Upper" +imports_granularity = "One" diff --git a/test.wasm b/test.wasm index 17f3b1507a41e4d73012531cba64014b6cd2a0dd..da05794df633a154cf69362ca3273f286f0b4344 100644 GIT binary patch delta 67 zcmX@hc#KhrA+b1@k%57MQGz9bv7WI$fvLWpu`YqRuD%|~N?>5Do2Z=4tih-@aiXdq VH#Z+MBWrGcX>lqeV+A8)B>*z35GViu delta 74 zcmX@cc$QI_A+b1@k%57MQIaKrv7WI$fvLWpu`YqRuD%|~N?-sI6P2==^%!*~PEZwh bWOHN`VB+THV`gN{%`YuZWn`>iWUK@Ls?ZTu