From c8455f9352a0282b85db34d4c520920e2416c77e Mon Sep 17 00:00:00 2001 From: Able Date: Fri, 24 Dec 2021 03:30:27 -0600 Subject: [PATCH] work done --- ableos/.cargo/config.toml | 3 +- ableos/Cargo.lock | 122 +++------------------- ableos/Cargo.toml | 26 ++--- ableos/src/arch/riscv/drivers/serial.rs | 20 ++-- ableos/src/arch/x86_64/init.rs | 1 + ableos/src/kmain.rs | 102 +++++++++--------- ableos/src/lib.rs | 1 + ableos/src/log.rs | 14 ++- ableos/src/relib/math/rand/linearshift.rs | 9 +- ableos/src/scheduler.rs | 79 ++++++++++++++ ableos/src/wasm/mod.rs | 15 +-- 11 files changed, 188 insertions(+), 204 deletions(-) create mode 100644 ableos/src/scheduler.rs diff --git a/ableos/.cargo/config.toml b/ableos/.cargo/config.toml index 7c70298..ef48109 100644 --- a/ableos/.cargo/config.toml +++ b/ableos/.cargo/config.toml @@ -2,9 +2,8 @@ target = "./json_targets/x86_64-ableos.json" [unstable] -build-std-features = ["compiler-builtins-mem"] build-std = ["core", "compiler_builtins", "alloc"] - +build-std-features = ["compiler-builtins-mem"] [target.'cfg(target_arch = "x86_64")'] diff --git a/ableos/Cargo.lock b/ableos/Cargo.lock index d3d38a5..c312307 100644 --- a/ableos/Cargo.lock +++ b/ableos/Cargo.lock @@ -8,11 +8,11 @@ version = "0.1.0" dependencies = [ "bootloader", "cpuio", + "externc-libm", "lazy_static", "linked_list_allocator", "lliw", "pic8259", - "psp", "qoi_rs", "spin", "uart_16550", @@ -41,32 +41,29 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bootloader" -version = "0.9.19" +version = "0.9.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7c452074efc3c0bfb241fb7bc87df04741c7c85e926f6a07c05f8fbd6008240" +checksum = "6b0718f186cd449b21f044683933284ed90fb83f3e13949ff0e03b0b6f02e38e" [[package]] name = "cpuio" version = "0.3.0" source = "git+https://github.com/anyusernameworks/cpuio.git#3908ecab79df80670ee1c5121870fc131f07627b" -[[package]] -name = "derivative" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "downcast-rs" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" +[[package]] +name = "externc-libm" +version = "0.1.0" +source = "git+https://github.com/HaruxOS/externc-libm#597110801cbcbbfb5d4731746d13014494deb39f" +dependencies = [ + "libm", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -142,52 +139,12 @@ dependencies = [ "autocfg", ] -[[package]] -name = "num_enum" -version = "0.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9bd055fb730c4f8f4f57d45d35cd6b3f0980535b056dc7ff119cee6a66ed6f" -dependencies = [ - "derivative", - "num_enum_derive", -] - -[[package]] -name = "num_enum_derive" -version = "0.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "486ea01961c4a818096de679a8b740b26d9033146ac5291b1c98557658f8cdd9" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "parity-wasm" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be5e13c266502aadf83426d87d81a0f5d1ef45b8027f5a471c360abfe4bfae92" -[[package]] -name = "paste" -version = "0.1.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45ca20c77d80be666aef2b45486da86238fabe33e38306bd3118fe4af33fa880" -dependencies = [ - "paste-impl", - "proc-macro-hack", -] - -[[package]] -name = "paste-impl" -version = "0.1.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d95a7db200b97ef370c8e6de0088252f7e0dfff7d047a28528e47456c0fc98b6" -dependencies = [ - "proc-macro-hack", -] - [[package]] name = "pic8259" version = "0.10.2" @@ -197,48 +154,12 @@ dependencies = [ "x86_64", ] -[[package]] -name = "proc-macro-hack" -version = "0.5.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" - -[[package]] -name = "proc-macro2" -version = "1.0.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43" -dependencies = [ - "unicode-xid", -] - -[[package]] -name = "psp" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b3021953a10d1bc6735a606ab9d5a1510282df7a55a160a270e9ea3cf479669" -dependencies = [ - "bitflags", - "num_enum", - "num_enum_derive", - "paste", -] - [[package]] name = "qoi_rs" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7ac44f12a8cec33865a699b2257e8454499fb4c3b13835710ff35c66bb65669" -[[package]] -name = "quote" -version = "1.0.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05" -dependencies = [ - "proc-macro2", -] - [[package]] name = "scopeguard" version = "1.1.0" @@ -260,17 +181,6 @@ dependencies = [ "lock_api", ] -[[package]] -name = "syn" -version = "1.0.81" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2afee18b8beb5a596ecb4a2dce128c719b4ba399d34126b9e4396e3f9860966" -dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", -] - [[package]] name = "uart_16550" version = "0.2.15" @@ -281,12 +191,6 @@ dependencies = [ "x86_64", ] -[[package]] -name = "unicode-xid" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" - [[package]] name = "volatile" version = "0.2.7" @@ -325,9 +229,9 @@ dependencies = [ [[package]] name = "x86_64" -version = "0.14.6" +version = "0.14.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbc6ed1ed2cd4536b083c34041aff7b84448ee25ac4aa5e9d54802ce226f9815" +checksum = "fb611915c917c6296d11e23f71ff1ecfe49c5766daba92cd3df52df6b58285b6" dependencies = [ "bit_field", "bitflags", diff --git a/ableos/Cargo.toml b/ableos/Cargo.toml index 30d92b7..9c6bda8 100644 --- a/ableos/Cargo.toml +++ b/ableos/Cargo.toml @@ -1,41 +1,43 @@ [package] +edition = "2021" name = "ableos" version = "0.1.0" -edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [profile.release] panic = "abort" [package.metadata.bootimage] +run-args = ["-serial", "stdio"] test-args = [ - "-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio" + "-device", + "isa-debug-exit,iobase=0xf4,iosize=0x04", + "-serial", + "stdio", ] -run-args=["-serial", "stdio"] [dependencies] -spin = "0.5.2" linked_list_allocator = "0.9.0" lliw = "0.2.0" qoi_rs = "*" - +spin = "0.5.2" [dependencies.wasmi] -version = "*" default-features = false features = ["core"] +version = "*" [dependencies.lazy_static] features = ["spin_no_std"] version = "1.0" -[target.'cfg(target_arch = "mips")'.dependencies] -psp = "0.1.5" +[dependencies.externc-libm] +git = "https://github.com/HaruxOS/externc-libm" [target.'cfg(target_arch = "x86_64")'.dependencies] -volatile = "0.2.6" -bootloader = {version = "0.9.8", features = ["map_physical_memory"]} +bootloader = { version = "0.9.8", features = ["map_physical_memory"] } cpuio = { git = "https://github.com/anyusernameworks/cpuio.git" } -x86_64 = "*" -uart_16550 = "0.2.0" pic8259 = "0.10.1" +uart_16550 = "0.2.0" +volatile = "0.2.6" +x86_64 = "*" diff --git a/ableos/src/arch/riscv/drivers/serial.rs b/ableos/src/arch/riscv/drivers/serial.rs index 6a58d9e..590d8d0 100644 --- a/ableos/src/arch/riscv/drivers/serial.rs +++ b/ableos/src/arch/riscv/drivers/serial.rs @@ -1,13 +1,8 @@ -use core::fmt::Arguments; -use core::fmt::Error; - /// Prints to the host through the serial interface. #[macro_export] macro_rules! serial_print { ($($arg:tt)*) => { - -crate::arch::drivers::serial::SERIAL.lock().out(format_args!($($arg)*)) - + crate::arch::drivers::serial::SERIAL.lock().out(format_args!($($arg)*)) }; } /// Prints to the host through the serial interface, appending a newline. @@ -20,9 +15,6 @@ macro_rules! serial_println { }; ($($arg:tt)*) => ($crate::print!("{}\r\n", format_args!($($arg)*))); - - - } pub struct Serial123 { @@ -38,10 +30,6 @@ impl Serial123 { } } -use spin::Mutex; - -use lazy_static::lazy_static; - lazy_static! { pub static ref SERIAL: Mutex = { let serial_port = Serial123 { @@ -51,3 +39,9 @@ lazy_static! { Mutex::new(serial_port) }; } + +use { + core::fmt::{Arguments, Error}, + lazy_static::lazy_static, + spin::Mutex, +}; diff --git a/ableos/src/arch/x86_64/init.rs b/ableos/src/arch/x86_64/init.rs index 62f4e66..98b8371 100644 --- a/ableos/src/arch/x86_64/init.rs +++ b/ableos/src/arch/x86_64/init.rs @@ -1,6 +1,7 @@ #![allow(clippy::print_literal)] use super::{gdt, interrupts}; use crate::{println, serial_println}; + pub fn init() { gdt::init(); interrupts::init_idt(); diff --git a/ableos/src/kmain.rs b/ableos/src/kmain.rs index d6ff17a..e893723 100644 --- a/ableos/src/kmain.rs +++ b/ableos/src/kmain.rs @@ -1,15 +1,19 @@ #![allow(clippy::empty_loop)] -use crate::{ - arch::{drivers::graphics::GraphicsBuffer, init, sloop}, - driver_traits::graphics::Graphics, - experiments::systeminfo::{KERNEL_VERSION, RELEASE_TYPE}, - keyboard::DecodedKey, - relib::math::rand::RAND_HANDLE, -}; +pub extern crate externc_libm as libm; -use alloc::{boxed::Box, rc::Rc}; -use lazy_static::lazy_static; +use { + crate::{ + arch::{drivers::graphics::GraphicsBuffer, init, sloop}, + driver_traits::graphics::Graphics, + experiments::systeminfo::{KERNEL_VERSION, RELEASE_TYPE}, + keyboard::DecodedKey, + relib::math::rand::RAND_HANDLE, + scheduler::{test_fn, Thread, ThreadList}, + }, + alloc::{boxed::Box, rc::Rc, vec, vec::Vec}, + lazy_static::lazy_static, +}; #[no_mangle] #[allow(unconditional_recursion)] @@ -22,55 +26,32 @@ pub extern "C" fn stack_overflow() -> u8 { lazy_static! { pub static ref KEY_BUFFER: [DecodedKey; 256] = [DecodedKey::RawKey(123); 256]; pub static ref KEY_BUFFER_POINTER: u8 = 0; + pub static ref THREAD_LIST: spin::Mutex = spin::Mutex::new(vec![]); + pub static ref TICK: spin::Mutex = spin::Mutex::new(0); } -// Defines the entry point +use alloc::format; +use crate::log::{self, Log}; #[no_mangle] pub fn kernel_main() -> ! { init::init(); + let mut a_thread = Thread::new(); + + a_thread.new_task(test_fn); + a_thread.new_task(test_fn); + + THREAD_LIST.lock().push(a_thread); GraphicsBuffer::draw(); GraphicsBuffer::hide_cursor(); GraphicsBuffer::show_cursor(); - - { - use alloc::{vec, vec::Vec}; - let x: Vec = vec![1]; - println!("{:?}", x); - - let heap_value = Box::new(41); - println!("heap_value at {:p}", heap_value); - - // create a dynamically sized vector - let mut vec = Vec::new(); - for i in 0..500 { - vec.push(i); - } - println!("vec at {:p}", vec.as_slice()); - - // create a reference counted vector -> will be freed when count reaches 0 - let reference_counted = Rc::new(vec![1, 2, 3]); - let cloned_reference = reference_counted.clone(); - println!( - "current reference count is {}", - Rc::strong_count(&cloned_reference) - ); - core::mem::drop(reference_counted); - println!( - "reference count is {} now", - Rc::strong_count(&cloned_reference) - ); + if false { + test_alloc(); } - // crate::wasm::evaluate(); - /* If AES is present then AES init rng as well - // Maybe via a cfg - AES::init_rng(); - - */ - println!("{} v{}", RELEASE_TYPE, KERNEL_VERSION); + log::ANSISerialLogger::debug(&format!("{} v{}", RELEASE_TYPE, KERNEL_VERSION)); { use crate::experiments::mail::MailBoxes; @@ -85,9 +66,6 @@ pub fn kernel_main() -> ! { sloop() } -lazy_static! { - pub static ref TICK: spin::Mutex = spin::Mutex::new(0); -} /// called by arch specific timers to tick up all kernel related functions pub fn tick() { let mut data = TICK.lock(); @@ -98,3 +76,31 @@ pub fn tick() { pub fn key_entropy(key: u8) { RAND_HANDLE.lock().seed_entropy_keyboard(key); } + +fn test_alloc() { + let x: Vec = vec![1]; + println!("{:?}", x); + + let heap_value = Box::new(41); + println!("heap_value at {:p}", heap_value); + + // create a dynamically sized vector + let mut vec = Vec::new(); + for i in 0..500 { + vec.push(i); + } + println!("vec at {:p}", vec.as_slice()); + + // create a reference counted vector -> will be freed when count reaches 0 + let reference_counted = Rc::new(vec![1, 2, 3]); + let cloned_reference = reference_counted.clone(); + println!( + "current reference count is {}", + Rc::strong_count(&cloned_reference) + ); + core::mem::drop(reference_counted); + println!( + "reference count is {} now", + Rc::strong_count(&cloned_reference) + ); +} diff --git a/ableos/src/lib.rs b/ableos/src/lib.rs index 75d4a04..efa40d5 100644 --- a/ableos/src/lib.rs +++ b/ableos/src/lib.rs @@ -36,6 +36,7 @@ pub mod kmain; pub mod log; pub mod panic; pub mod relib; +pub mod scheduler; pub mod wasm; extern crate alloc; diff --git a/ableos/src/log.rs b/ableos/src/log.rs index 452df75..3ef3863 100644 --- a/ableos/src/log.rs +++ b/ableos/src/log.rs @@ -1,7 +1,7 @@ pub trait Log { - fn debug(); + fn debug(val: &str); fn error(); - fn log(); + fn info(val: &str); fn trace(); } @@ -9,16 +9,14 @@ use crate::serial_print; use lliw::{Fg, Reset}; pub struct ANSISerialLogger; impl Log for ANSISerialLogger { - fn debug() { - serial_print!("[{}Debug{}]", Fg::Blue, Reset); - - todo!(); + fn debug(val: &str) { + serial_print!("[{}Debug{}] {}\n", Fg::Blue, Reset, val); } fn error() { todo!(); } - fn log() { - todo!(); + fn info(val: &str) { + serial_print!("[{}Info{}] {}\n", Fg::Blue, Reset, val); } fn trace() { todo!(); diff --git a/ableos/src/relib/math/rand/linearshift.rs b/ableos/src/relib/math/rand/linearshift.rs index 8e0c715..0bdd475 100644 --- a/ableos/src/relib/math/rand/linearshift.rs +++ b/ableos/src/relib/math/rand/linearshift.rs @@ -1,6 +1,7 @@ use crate::relib::math::rand::RNG; + pub struct LinearShiftRegister { - reg: u64, + reg: u64, } // 64 bit // non-cryptographically secure @@ -10,6 +11,7 @@ impl RNG for LinearShiftRegister { } fn rand(&mut self) -> u64 { let newbit = (self.reg >> 1) ^ (self.reg >> 2) ^ (self.reg >> 7); + self.reg = (self.reg >> 1) | (newbit << 3); newbit } @@ -23,11 +25,6 @@ impl RNG for LinearShiftRegister { let mult = shifted.wrapping_mul(multitude); let seeded_bit = seed / mult; - if false { - // crate::serial_println!("Entropy {}", entropy); - // crate::serial_println!("Multitude {}", multitude); - // crate::serial_println!("Seeded Bit {}", seeded_bit); - } for _ in 0..seeded_bit { self.rand(); } diff --git a/ableos/src/scheduler.rs b/ableos/src/scheduler.rs new file mode 100644 index 0000000..207968d --- /dev/null +++ b/ableos/src/scheduler.rs @@ -0,0 +1,79 @@ +use core::cmp::Ordering; + +use alloc::vec; +use alloc::vec::Vec; + +use crate::kmain::THREAD_LIST; + +pub type Pointer = fn(); +pub type ThreadID = u64; +pub type TaskID = u64; +pub type ThreadList = Vec; + +#[derive(Eq, Debug)] +pub struct Task { + id: TaskID, + parent_thread: ThreadID, + fn_pointer: Pointer, +} +impl Task { + fn new(parent_thread: ThreadID, task_id: u64, function_pointer: fn()) -> Self { + Self { + id: task_id, + parent_thread, + fn_pointer: function_pointer, + } + } +} + +impl Ord for Task { + fn cmp(&self, other: &Self) -> Ordering { + self.id.cmp(&other.id) + } +} + +impl PartialOrd for Task { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl PartialEq for Task { + fn eq(&self, other: &Self) -> bool { + self.id == other.id + } +} +#[derive(Debug)] +pub struct Thread { + pub id: ThreadID, + pub tasks: Vec, +} +impl Thread { + pub fn new() -> Self { + let threads = &*THREAD_LIST.lock(); + let mut final_threadid = 0; + match threads.last() { + Some(last_thread) => final_threadid = last_thread.id + 1, + None => {} + } + + Self { + id: final_threadid, + tasks: vec![], + } + } + pub fn sort_tasks(&mut self) { + self.tasks.sort(); + } + pub fn new_task_id(&self) -> TaskID { + self.tasks.len().try_into().unwrap() + } + pub fn new_task(&mut self, function_pointer: fn()) { + let x = Task::new(self.id, self.new_task_id(), function_pointer); + self.tasks.push(x); + } +} + +pub fn test_fn() { + println!("Hello"); +} diff --git a/ableos/src/wasm/mod.rs b/ableos/src/wasm/mod.rs index 01fd336..54a16b5 100644 --- a/ableos/src/wasm/mod.rs +++ b/ableos/src/wasm/mod.rs @@ -1,12 +1,15 @@ -// use wabt; -use wasmi::{ - Error, Externals, FuncInstance, FuncRef, ImportsBuilder, ModuleImportResolver, ModuleInstance, - RuntimeArgs, RuntimeValue, Signature, Trap, ValueType, +use { + alloc::format, + wasm_sys::SysCall, + wasmi::{ + Error, Externals, FuncInstance, FuncRef, ImportsBuilder, ModuleImportResolver, + ModuleInstance, RuntimeArgs, RuntimeValue, Signature, Trap, ValueType, + }, }; mod wasm_sys; -use alloc::format; -use wasm_sys::SysCall; +pub extern crate externc_libm as libm; + struct HostFunctions; impl HostFunctions { fn check_signature(&self, index: usize, signature: &Signature) -> bool {