diff --git a/.vscode/settings.json b/.vscode/settings.json index 4e57780..465a071 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "rust-analyzer.checkOnSave.allTargets": false + "rust-analyzer.checkOnSave.allTargets": false, + "rust-analyzer.showUnlinkedFileNotification": false } \ No newline at end of file diff --git a/kernel/src/arch/x86_64/interrupts.rs b/kernel/src/arch/x86_64/interrupts.rs index 1d58f73..090f031 100644 --- a/kernel/src/arch/x86_64/interrupts.rs +++ b/kernel/src/arch/x86_64/interrupts.rs @@ -41,6 +41,10 @@ static IDT: Lazy = Lazy::new(|| { .set_stack_index(super::gdt::DOUBLE_FAULT_IX); } idt.page_fault.set_handler_fn(page_fault); + + idt[Interrupt::ApicErr as usize].set_handler_fn(apic_err); + idt[Interrupt::Spurious as usize].set_handler_fn(spurious); + idt[Interrupt::Timer as usize].set_handler_fn(timer); idt }); diff --git a/kernel/src/interp.rs b/kernel/src/interp.rs index ef8d268..4d5b1b6 100644 --- a/kernel/src/interp.rs +++ b/kernel/src/interp.rs @@ -1,11 +1,19 @@ -use alloc::{string::String, vec::Vec}; -use core::fmt::Debug; -use core::fmt::Display; +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::{self, Handle}; +use crate::handle; + +#[derive(Debug)] + +pub struct WasmContext { + pub proc_id: Option, + pub instance: Instance, + pub store: Store, +} pub fn wasm() -> Result<(), wasmi::Error> { use wasmi::Config; @@ -22,9 +30,7 @@ pub fn wasm() -> Result<(), wasmi::Error> { // trace!("Loading WASM binary"); let module = Module::new(&engine, &wasm[..]).unwrap(); // trace!("Constructing wasm module"); - let hs = HostState { - proc_handle: crate::handle::Handle::new(), - }; + let hs = HostState {}; let mut store = Store::new(&engine, hs); // trace!("constructing host store"); @@ -71,14 +77,10 @@ pub fn wasm() -> Result<(), wasmi::Error> { Ok(()) } - -pub struct HostState { - /// In ableOS a handle is an unsigned 128 bit number - proc_handle: Handle, -} +#[derive(Clone, Debug)] +pub struct HostState {} pub fn read_memory_address(caller: Caller<'_, HostState>, address: i32) -> i32 { - trace!("Proccess Handle: {:?}", caller.data().proc_handle); trace!("Address: {}", address); // let obj = host_make_object(caller, 16, 23); 0 @@ -137,3 +139,64 @@ pub fn host_read_object_attribute( (0, 0) } + +pub fn build_wasm_context(bytes: Vec) -> Result { + use wasmi::Config; + use wasmi::Engine; + let mut conf = Config::default(); + conf.wasm_bulk_memory(true); + // conf., + let engine = Engine::new(&conf); + // trace!("Engine constructed"); + + // let wasm = include_bytes!("../../wasm_syscall_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 mut store = Store::new(&engine, hs); + // trace!("constructing host store"); + + let read_mem_addr = Func::wrap( + &mut store, + |caller: Caller<'_, HostState>, param: i32| -> i32 { read_memory_address(caller, param) }, + ); + + let mut linker = >::new(&engine); + linker.define( + "host", + "read_mem_addr", + Func::wrap( + &mut store, + |caller: Caller<'_, HostState>, param: i32| -> i32 { + read_memory_address(caller, param) + }, + ), + )?; + + linker.define( + "host", + "read_object_attribute", + Func::wrap(&mut store, host_read_object_attribute), + )?; + + linker.define( + "host", + "create_object", + Func::wrap(&mut store, host_make_object), + )?; + + let instance = linker + .instantiate(&mut store, &module)? + .ensure_no_start(&mut store)?; + + let wc = WasmContext { + instance, + store, + proc_id: None, + }; + + Ok(wc) +} diff --git a/kernel/src/kmain.rs b/kernel/src/kmain.rs index d1a5fd7..01f4a9f 100644 --- a/kernel/src/kmain.rs +++ b/kernel/src/kmain.rs @@ -7,6 +7,7 @@ use spin::{Lazy, Mutex}; use crate::arch::{hardware_random_u64, sloop}; use crate::handle::Handle; +use crate::schedule::Scheduler; use crate::{interp, task}; use crate::alloc::string::ToString; @@ -21,13 +22,13 @@ pub fn kmain(cmdline: &str, bootstrap: Option<&'static [u8]>) -> ! { let kcmd = clparse::Arguments::parse(cmdline.to_string()).unwrap(); log::info!("Cmdline: {kcmd:?}"); - if kcmd.arguments.get("baka") == Some(&"true".to_string()) { - let _ = crate::arch::log(format_args!(include_str!("../data/⑨. バカ"))); - } + // if kcmd.arguments.get("baka") == Some(&"true".to_string()) { + // let _ = crate::arch::log(format_args!(include_str!("../data/⑨. バカ"))); + // } - if kcmd.arguments.get("foobles") == Some(&"true".to_string()) { - let _ = crate::arch::log(format_args!("foobles\n")); - } + // if kcmd.arguments.get("foobles") == Some(&"true".to_string()) { + // let _ = crate::arch::log(format_args!("foobles\n")); + // } let bootstrap = bootstrap/*.expect("no bootstrap found")*/; match bootstrap { @@ -37,18 +38,32 @@ pub fn kmain(cmdline: &str, bootstrap: Option<&'static [u8]>) -> ! { } } - use xml::XMLElement; - let kcmd = XMLElement::new("cmdline"); - let hnd = Handle::new(); - OBJECTS.lock().insert(hnd, kcmd); + // use xml::XMLElement; + // let kcmd = XMLElement::new("cmdline"); + // let hnd = Handle::new(); + // kcmd.set_attribute("") + // OBJECTS.lock().insert(hnd, kcmd); - let abc = interp::wasm(); + // let abc = interp::wasm(); - trace!("{:?}", abc); - crate::arch::sloop() + // 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); + + sch.run(); + + // 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(); + Mutex::new(sch) +}); diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index ce014a7..c2e1808 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -10,6 +10,7 @@ ptr_sub_ptr )] #![no_std] +// #![deny(missing_docs)] extern crate alloc; @@ -20,6 +21,7 @@ pub mod interp; mod kmain; mod logger; mod memory; +mod schedule; mod task; use versioning::Version; diff --git a/kernel/src/schedule.rs b/kernel/src/schedule.rs new file mode 100644 index 0000000..05f1b29 --- /dev/null +++ b/kernel/src/schedule.rs @@ -0,0 +1,104 @@ +use core::arch; + +// WasmContext +use crate::{arch::sloop, interp::WasmContext}; +use alloc::vec::Vec; +use log::trace; + +pub type ProcId = u64; + +pub struct Scheduler { + running: Vec, + halted: Vec<(ContextWake, WasmContext)>, + // time_halt: Vec<(ContextWake, WasmContext)>, + next_proc_id: ProcId, +} + +impl Scheduler { + pub fn new() -> Scheduler { + Self { + running: Vec::new(), + halted: Vec::new(), + // time_halt: Vec::new(), + next_proc_id: 0, + } + } + pub fn run(&mut self) -> ! { + loop { + let proc = self.running.pop(); + + // self.time_halt.sort(); + + match proc { + Some(proc) => { + // trace!("SWAP WasmContext"); + self.running.push(proc); + } + + None => { + panic!("nothing scheduled."); + sloop(); + } + } + } + } + fn sleep_inner(&self, id: ProcId) -> Result { + let proc_len = self.running.len(); + let mut proc_found = true; + let mut sleep_index = 0; + let mut i = 0; + for wc in &self.running { + if wc.proc_id == Some(id) { + sleep_index = i; + proc_found = true; + break; + } + if i == proc_len { + proc_found = false; + trace!("no process with ID {} found", id); + return Err(SchedulerError::ProcessIDNotFound(id)); + } + i += 1; + } + + Ok(sleep_index) + } + pub fn sleep(&mut self, id: ProcId, time: u64) -> Result<(), SchedulerError> { + match self.sleep_inner(id) { + Ok(sid) => self + .halted + .push((ContextWake::Time(time), self.running.remove(sid))), + Err(error) => return Err(error), + } + + Ok(()) + } + + pub fn schedule(&mut self, wc: WasmContext, cw: ContextWake) -> Result<(), SchedulerError> { + if wc.proc_id != None { + panic!("Already Scheduled with PROC_ID {}", wc.proc_id.unwrap()); + } + trace!("Scheduling WC with ProcID {}", self.next_proc_id); + + if cw == ContextWake::None { + self.running.push(wc) + } else { + self.halted.push((cw, wc)); + } + + self.next_proc_id += 1; + Ok(()) + } +} +#[derive(PartialEq)] +pub enum ContextWake { + /// Used when spawning a new process to have it instantly start + None, + Time(u64), + ObjectEvent, +} + +pub enum SchedulerError { + ProcessIDNotFound(ProcId), + AlreadyScheduled(), +} diff --git a/meta.md b/meta.md new file mode 100644 index 0000000..c9e2b7d --- /dev/null +++ b/meta.md @@ -0,0 +1,51 @@ + + + + + + + +ARI AbleOS Remote Install + +Server +/boot/server_kernel_x86_64.bin +/boot/kernel_x86_64.bin +/boot/kernel_aarch64.bin + +/home/projects/askl.askl - aksldfhlkasjdhflkajshdflkj + + +ARI_SERVER.wasm +NAS 10.1.10.10 + +ARI 10.1.10.10 +/boot/limine.cfg +/boot/kernel_x86_64.bin +/boot/kernel.toml +/home/projects/askl.askl +aksldfhlkasjdhflkajshdflkj + + +ARI 10.1.10.10 +/boot/limine.cfg +/boot/kernel_aarch64.bin +/boot/kernel.toml + + + +/system/ +/shared/ +/home/programs/ + project_name/ + project_name.wasm + project_name.toml + + + + +/system/pkgman.toml + +////// +[repositories] +PUR = "https://git.ablecorp.us/ableos/pur" +