From b5635bbf5912545582e2c18973d1d8a57e16dab9 Mon Sep 17 00:00:00 2001 From: Able Date: Tue, 25 Jan 2022 19:40:37 -0600 Subject: [PATCH] scheduler work and commit --- ableos/Cargo.toml | 10 +-- ableos/assets/kernel_config.json | 3 +- ableos/src/arch/x86_64/drivers/mod.rs | 3 + ableos/src/arch/x86_64/drivers/timer.rs | 2 + ableos/src/arch/x86_64/interrupts.rs | 15 +--- ableos/src/boot_conf.rs | 4 +- ableos/src/experiments/futex.rs | 4 +- ableos/src/experiments/schedule.rs | 26 ------ ableos/src/kmain.rs | 2 - ableos/src/lib.rs | 6 +- ableos/src/logger.rs | 24 +++--- ableos/src/proc.rs | 5 -- ableos/src/relib/clparse/mod.rs | 4 +- ableos/src/scheduler.rs | 85 ------------------ ableos/src/scheduler/capabilities.rs | 64 ++++++++++++++ ableos/src/scheduler/mod.rs | 110 ++++++++++++++++++++++++ ableos/src/scheduler/proc.rs | 27 ++++++ ableos/src/vga_e.rs | 2 +- ableos/src/wasm/mod.rs | 50 +++++++++++ 19 files changed, 285 insertions(+), 161 deletions(-) create mode 100644 ableos/src/arch/x86_64/drivers/timer.rs delete mode 100644 ableos/src/experiments/schedule.rs delete mode 100644 ableos/src/proc.rs delete mode 100644 ableos/src/scheduler.rs create mode 100644 ableos/src/scheduler/capabilities.rs create mode 100644 ableos/src/scheduler/mod.rs create mode 100644 ableos/src/scheduler/proc.rs create mode 100644 ableos/src/wasm/mod.rs diff --git a/ableos/Cargo.toml b/ableos/Cargo.toml index ba895b1f..ee3afede 100644 --- a/ableos/Cargo.toml +++ b/ableos/Cargo.toml @@ -52,32 +52,24 @@ version = "*" default-features = false features = ["alloc"] - - [dependencies.rkyv] version = "0.7.29" default-features = false features = ["size_64", "alloc"] - [dependencies.smoltcp] version = "0.8.0" default-features = false features = ["log", "proto-ipv4"] - - [dependencies.y-compositor-protocol] git = "https://git.ablecorp.us:443/able/y-compositor-protocol.git" - - - - [dependencies.shadeable] path = "../shadeable" [dependencies.ab_glyph] +version = "*" default-features = false features = ["libm"] diff --git a/ableos/assets/kernel_config.json b/ableos/assets/kernel_config.json index 97b07a5c..88b0866c 100644 --- a/ableos/assets/kernel_config.json +++ b/ableos/assets/kernel_config.json @@ -1,6 +1,7 @@ { "logging_level": "Trace", + "logger_padding": 10, "run_tests": false, "run_demos": false, - "run_shader_tests": true + "run_shader_tests": false } \ No newline at end of file diff --git a/ableos/src/arch/x86_64/drivers/mod.rs b/ableos/src/arch/x86_64/drivers/mod.rs index 8ac1cbc8..3872f699 100644 --- a/ableos/src/arch/x86_64/drivers/mod.rs +++ b/ableos/src/arch/x86_64/drivers/mod.rs @@ -1,5 +1,8 @@ pub mod allocator; pub mod graphics; pub mod serial; + +pub mod timer; + // #[deprecated(note = "The use of hardware specific drivers for VGA is discouraged")] pub mod vga; diff --git a/ableos/src/arch/x86_64/drivers/timer.rs b/ableos/src/arch/x86_64/drivers/timer.rs new file mode 100644 index 00000000..83d32a78 --- /dev/null +++ b/ableos/src/arch/x86_64/drivers/timer.rs @@ -0,0 +1,2 @@ +/// This magic value is the herts of the timer interupts normalized +pub const TIMER_INTERRUPT_HERTZ: f64 = 1193182.0; diff --git a/ableos/src/arch/x86_64/interrupts.rs b/ableos/src/arch/x86_64/interrupts.rs index 1ebfd15b..962e3189 100644 --- a/ableos/src/arch/x86_64/interrupts.rs +++ b/ableos/src/arch/x86_64/interrupts.rs @@ -93,16 +93,12 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac // print!(" "); // WRITER.lock().backspace(); } - 0x12 => { - KERNEL_STATE.lock().shutdown(); - } // Enter 0x0A => { - let _xyz = crate::kmain::KEY_BUFFER.lock(); + // let _xyz = crate::kmain::KEY_BUFFER.lock(); - // println!("{:?}", clparse::Command::parse(xyz.to_string())); - print!("{}", char::try_from(character).unwrap()); + // print!("{}", char::try_from(character).unwrap()); } _ => { print!("{}", char::try_from(character).unwrap()); @@ -123,11 +119,8 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac value: key, } => match KeyCode::from(key) { KeyCode::AltLeft => {} - - KeyCode::AltRight => { - let xyz = crate::kmain::KEY_BUFFER.lock(); - println!("{:?}", &xyz); - } + // KeyCode::Escape => {} + KeyCode::AltRight => {} _ => { print!("{:?}", KeyCode::from(key)) } diff --git a/ableos/src/boot_conf.rs b/ableos/src/boot_conf.rs index 4762e467..fd08d2a4 100644 --- a/ableos/src/boot_conf.rs +++ b/ableos/src/boot_conf.rs @@ -20,7 +20,7 @@ pub enum LogLevel { #[derive(Serialize, Debug, Deserialize)] pub struct BootConfig { pub logging_level: LogLevel, - + pub logger_padding: usize, pub run_tests: bool, pub run_demos: bool, pub run_shader_tests: bool, @@ -34,7 +34,7 @@ impl BootConfig { // same function as the one that produced serde_json::Value above, but // now we are asking it for a Person as output. let p: BootConfig = serde_json::from_str(data).unwrap(); - info!("{:?}", p); + // info!("{:?}", p); p } diff --git a/ableos/src/experiments/futex.rs b/ableos/src/experiments/futex.rs index 70412a39..13b1ac89 100644 --- a/ableos/src/experiments/futex.rs +++ b/ableos/src/experiments/futex.rs @@ -2,8 +2,6 @@ use core::time::Duration; use alloc::vec::Vec; -use crate::scheduler::ThreadID; - // pub struct Duration {} pub struct AtomicU32(u32); @@ -53,7 +51,7 @@ pub fn futex_wake(atom: &AtomicU32, threads_to_wake: usize) { struct FutexWaitlist { address: u8, - data: Vec, + // data: Vec, } impl FutexWaitlist { diff --git a/ableos/src/experiments/schedule.rs b/ableos/src/experiments/schedule.rs deleted file mode 100644 index e168f4d4..00000000 --- a/ableos/src/experiments/schedule.rs +++ /dev/null @@ -1,26 +0,0 @@ -// Deprecated - -pub type Priority = [Process; 512]; - -pub struct VirtualMemoryTable {} - -struct Process { - id: u64, - mem_table: *mut VirtualMemoryTable, // Pointer to a memory table -} - -pub struct Scheduler { - pub high_priority: Priority, //150 - pub medium_priority: Priority, //100 - pub low_priority: Priority, // 50 - pub next_pid: u64, -} - -impl Scheduler { - pub fn bump_up() {} - pub fn bump_down() {} - pub fn schedule(&mut self) { - let current_pid = self.next_pid; - self.next_pid += 1; - } -} diff --git a/ableos/src/kmain.rs b/ableos/src/kmain.rs index e3163555..ae6c9863 100644 --- a/ableos/src/kmain.rs +++ b/ableos/src/kmain.rs @@ -100,8 +100,6 @@ pub fn kernel_main() -> ! { let ret = WasmProgram::new_from_bytes(&[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]); trace!("Binary Valid: {:?}", ret.validate_header()); - // add a plus b - if BOOT_CONF.run_tests { // quick and dirty testing framework screen_writer_test(); diff --git a/ableos/src/lib.rs b/ableos/src/lib.rs index eeb29698..47e4c6fb 100644 --- a/ableos/src/lib.rs +++ b/ableos/src/lib.rs @@ -3,11 +3,11 @@ #![no_std] #![feature( abi_x86_interrupt, - asm, + // asm, asm_sym, alloc_error_handler, core_intrinsics, - global_asm, + // global_asm, lang_items, llvm_asm, naked_functions @@ -41,11 +41,11 @@ pub mod keyboard; pub mod kmain; pub mod logger; pub mod panic; -pub mod proc; pub mod relib; pub mod scheduler; mod unicode_utils; pub mod utils; +pub mod wasm; pub extern crate alloc; pub extern crate externc_libm as libm; diff --git a/ableos/src/logger.rs b/ableos/src/logger.rs index 5b193162..374d5453 100644 --- a/ableos/src/logger.rs +++ b/ableos/src/logger.rs @@ -1,8 +1,13 @@ +use core::sync::atomic::Ordering; + +use crate::kmain::{BOOT_CONF, TICK}; use crate::serial_println; use lliw::{Fg, Reset}; pub use log::{debug, info, trace, warn}; use log::{Level, Metadata, Record}; +use crate::arch::drivers::timer::TIMER_INTERRUPT_HERTZ; + struct SimpleLogger; impl log::Log for SimpleLogger { @@ -12,7 +17,10 @@ impl log::Log for SimpleLogger { fn log(&self, record: &Record) { if self.enabled(record.metadata()) { - let color; // = (Fg::Reset, "$GREEN$"); + let color; + let time = TICK.load(Ordering::Relaxed) as f64; + + let time_float = time / TIMER_INTERRUPT_HERTZ; match record.level() { log::Level::Error => color = (Fg::Red, "$RED$"), @@ -21,22 +29,16 @@ impl log::Log for SimpleLogger { log::Level::Debug => color = (Fg::Blue, "$BLUE$"), log::Level::Trace => color = (Fg::Yellow, "$YELLOW$"), } - /* - println!( - "[{}{}$RESET$][$GREEN$FakeTempTime$RESET$] {}", - color.1, - record.level(), - record.args() - ); - */ + serial_println!( - "[{}{}{}][{}FakeTempTime{}] {}", + "[{}{}{}][{}{}{}] {}", color.0, record.level(), Fg::Reset, Fg::Green, + time_float, Reset, - record.args() + record.args(), ); } } diff --git a/ableos/src/proc.rs b/ableos/src/proc.rs deleted file mode 100644 index 38a9da74..00000000 --- a/ableos/src/proc.rs +++ /dev/null @@ -1,5 +0,0 @@ -use crate::relib::clparse::Command; - -pub struct ProccessStart { - command: Command, -} diff --git a/ableos/src/relib/clparse/mod.rs b/ableos/src/relib/clparse/mod.rs index 319633d0..c71a1886 100644 --- a/ableos/src/relib/clparse/mod.rs +++ b/ableos/src/relib/clparse/mod.rs @@ -10,13 +10,13 @@ clparse */ // use std::collections::HashMap; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Argument { key: String, value: String, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Command { pub root_command: String, // arguments: HashMap, diff --git a/ableos/src/scheduler.rs b/ableos/src/scheduler.rs deleted file mode 100644 index a395ac38..00000000 --- a/ableos/src/scheduler.rs +++ /dev/null @@ -1,85 +0,0 @@ -use { - crate::kmain::THREAD_LIST, - alloc::{vec, vec::Vec}, - core::cmp::Ordering, -}; - -pub type Pointer = fn(); -pub type ThreadID = u64; -pub type TaskID = u64; -pub type ThreadList = Vec; - -#[allow(dead_code)] -#[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 { - debug!( - "New task with TaskID: {} | Parent ThreadID: {}", - task_id, parent_thread - ); - - 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 => {} - } - debug!("New thread with ThreadID: {}", final_threadid); - Self { - id: final_threadid, - tasks: vec![], - } - } - pub fn sort_tasks(&mut self) { - self.tasks.sort(); - } - pub fn new_task_id(&self) -> TaskID { - let task_id = self.tasks.len().try_into().unwrap(); - trace!("New TaskID: {}", task_id); - task_id - } - 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/scheduler/capabilities.rs b/ableos/src/scheduler/capabilities.rs new file mode 100644 index 00000000..5baf6a00 --- /dev/null +++ b/ableos/src/scheduler/capabilities.rs @@ -0,0 +1,64 @@ +use alloc::vec::Vec; + +/// Placeholder to be replaced with a proper FileReference when such a thing exists +pub type FileID = u8; +pub type SoundCardID = u8; +pub type DeviceID = u8; +pub type ControllerID = u8; + +#[derive(Clone, Debug)] +pub enum FileAccess { + All, + Some(Vec), + None, +} + +#[derive(Clone, Debug)] +pub enum ControllerAccess { + All, + Some(Vec), + None, +} + +#[derive(Clone, Debug)] +pub enum SoundCardAccess { + All, + Some(Vec), + None, +} + +#[derive(Clone, Debug)] +pub enum MouseAccess { + Yes, + No, +} + +#[derive(Clone, Debug)] +pub enum KeyboardAccess { + Yes, + No, +} + +#[derive(Clone, Debug)] +pub struct Capabilities { + files: FileAccess, + mouse: MouseAccess, + keyboard: KeyboardAccess, + controllers: ControllerAccess, + sound_cards: SoundCardAccess, +} + +impl Capabilities { + /// Generate a set of empty capabilities + pub fn empty() -> Self { + Self { + files: FileAccess::None, + mouse: MouseAccess::No, + keyboard: KeyboardAccess::No, + controllers: ControllerAccess::None, + sound_cards: SoundCardAccess::None, + } + } +} + + diff --git a/ableos/src/scheduler/mod.rs b/ableos/src/scheduler/mod.rs new file mode 100644 index 00000000..125aab47 --- /dev/null +++ b/ableos/src/scheduler/mod.rs @@ -0,0 +1,110 @@ +#![warn(missing_docs)] +use alloc::{vec, vec::Vec}; + +pub mod capabilities; +pub mod proc; +use proc::{Process, ProcessPermissions, PID}; + +use self::capabilities::Capabilities; + +lazy_static::lazy_static!( + + /// The standard implementation for ableOS + pub static ref SCHEDULER: spin::Mutex = spin::Mutex::new(Scheduler::new()); + +); + +// pub struct VirtualMemoryTable {} + +/// Scheduler priority model +#[derive(Clone, Copy, Debug)] +pub enum Priority { + /// Exclusively Kernel space | 20 Timer Tick execution time + High, + /// Kernel / User space | 15 Timer Tick execution time + Medium, + /// low priority userspace code | 10 Timer Tick execution time + Low, +} + +/// Placeholder +#[derive(Clone, Debug)] +pub struct FileID(u8); + +#[derive(Clone, Debug)] +pub enum FileAccessTypes { + All, + Some(Vec), + None, +} + +/// +#[derive(Clone, Debug)] +pub struct Scheduler { + pub free_pid: PID, + + pub process_exec_time: u64, + pub list: Vec, +} + +impl Scheduler { + pub fn new() -> Self { + Self { + list: vec![], + process_exec_time: 0, + free_pid: PID(0), + } + } + + pub fn next_process(&mut self) { + self.process_exec_time = 0; + let previous_task = self.list[0].clone(); + self.list.remove(0); + self.list.push(previous_task); + } + + /// Create a + pub fn new_process(&mut self, priority: Priority) { + // let new_pid = self.free_pid; + + let process = Process { + id: self.free_pid.clone(), + capabilities: Capabilities::empty(), + priority, + }; + self.free_pid.0 += 1; + self.list.push(process); + } + /// Terminate the process with the matching PID + pub fn term_process(&mut self, pid: PID) { + let mut process_index = 0; + for x in &self.list { + if x.id == pid { + self.list.remove(process_index); + break; + } + process_index += 1 + } + } + + pub fn bump_exec(&mut self) { + self.process_exec_time += 1; + use Priority::*; + + if self.list.len() > 0 { + match (self.process_exec_time, self.list[0].priority) { + (20, High) => { + self.next_process(); + } + (15, Medium) => { + self.next_process(); + } + (10, Low) => { + self.next_process(); + } + + (_, _) => {} + } + } + } +} diff --git a/ableos/src/scheduler/proc.rs b/ableos/src/scheduler/proc.rs new file mode 100644 index 00000000..23139c52 --- /dev/null +++ b/ableos/src/scheduler/proc.rs @@ -0,0 +1,27 @@ +//! Process definition and general utilities surrounding them + +use super::{capabilities::Capabilities, FileAccessTypes, Priority}; + +/// Process Identification +#[derive(Clone, PartialEq, Debug)] + +pub struct PID(pub usize); + +/// Temporary till integration of capabilities +#[derive(Clone, Debug)] +pub struct ProcessPermissions { + pub file_access: FileAccessTypes, +} + +/// A process +#[derive(Clone, Debug)] +pub struct Process { + /// Internal PID + pub id: PID, + /// + pub capabilities: Capabilities, + /// A process's priority + pub priority: Priority, +} + +impl Process {} diff --git a/ableos/src/vga_e.rs b/ableos/src/vga_e.rs index f32736cd..773193f7 100644 --- a/ableos/src/vga_e.rs +++ b/ableos/src/vga_e.rs @@ -32,7 +32,7 @@ pub fn draw_char(previous_character: Option, character: char, _offset: usi .unwrap(); let font2 = FontRef::try_from_slice(include_bytes!( - "../../ableos/assets/fonts/unifont_upper-14.0.01.ttf" // "../../ableos/assets/fonts/OpenSansEmoji.ttf" + "../../ableos/assets/fonts/unifont_upper-14.0.01.ttf" )) .unwrap(); diff --git a/ableos/src/wasm/mod.rs b/ableos/src/wasm/mod.rs new file mode 100644 index 00000000..152ee5ee --- /dev/null +++ b/ableos/src/wasm/mod.rs @@ -0,0 +1,50 @@ +use alloc::vec::Vec; + +/// `NULL a s m` as an array of 4 bytes +pub const WASM_BINARY_MAGIC: [u8; 4] = [0x00, 0x61, 0x73, 0x6d]; +/// `1 0 0 0` as an array of 4 bytes +pub const WASM_VERSION: [u8; 4] = [0x01, 0x00, 0x00, 0x00]; + +/// Validate a wasm header (8 bytes) + +pub enum SectionType { + None = 0, + Type = 1, +} + +pub struct Section { + stype: SectionType, + section_size: u8, +} + +pub struct WasmProgram { + raw_bytes: Vec, + // version: +} + +impl WasmProgram { + pub fn new_from_bytes(bytes: &[u8]) -> Self { + Self { + raw_bytes: bytes.to_vec(), + } + } + + pub fn validate_header(self) -> (bool, bool) { + let mut byte_magic_valid = false; + let mut byte_version_valid = false; + + if self.raw_bytes[0..4] == WASM_BINARY_MAGIC { + byte_magic_valid = true; + } + + if self.raw_bytes[4..8] == WASM_VERSION { + byte_version_valid = true; + } + return (byte_magic_valid, byte_version_valid); + } + + + + + +}