From 13edd25c8bb6c5093b60dfc1e5cbda36b3462bba Mon Sep 17 00:00:00 2001 From: Able Date: Thu, 27 Jan 2022 01:37:12 -0600 Subject: [PATCH] reduce race conditions --- ableos/src/alias_table/mod.rs | 28 ++++ ableos/src/arch/x86_64/init.rs | 3 +- ableos/src/kernel_state.rs | 7 + ableos/src/kmain.rs | 190 ++------------------------- ableos/src/lib.rs | 6 + ableos/src/scheduler/capabilities.rs | 20 ++- ableos/src/scheduler/mod.rs | 22 +++- ableos/src/tests.rs | 186 ++++++++++++++++++++++++++ 8 files changed, 272 insertions(+), 190 deletions(-) create mode 100644 ableos/src/alias_table/mod.rs create mode 100644 ableos/src/tests.rs diff --git a/ableos/src/alias_table/mod.rs b/ableos/src/alias_table/mod.rs new file mode 100644 index 0000000..36d3d1d --- /dev/null +++ b/ableos/src/alias_table/mod.rs @@ -0,0 +1,28 @@ +use alloc::string::{String, ToString}; +use hashbrown::HashMap; + +lazy_static::lazy_static! { + pub static ref ALIAS_TABLE: spin::Mutex = spin::Mutex::new(AliasTable::new()); +} +/// A table of aliases +/// +/// This is used to allow users to specify aliases for files and commands +pub struct AliasTable { + pub table: HashMap, +} + +impl AliasTable { + pub fn new() -> Self { + Self { + table: HashMap::new(), + } + } + pub fn add_alias(&mut self, alias: String, path: String) { + self.table.insert(alias, path); + } + + pub fn get_alias(&self, alias: String) -> Option { + let alias_table = ALIAS_TABLE.lock(); + alias_table.table.get(&alias).map(|x| x.to_string()) + } +} diff --git a/ableos/src/arch/x86_64/init.rs b/ableos/src/arch/x86_64/init.rs index bf4b461..bc80f9b 100644 --- a/ableos/src/arch/x86_64/init.rs +++ b/ableos/src/arch/x86_64/init.rs @@ -15,7 +15,8 @@ pub fn init() { use crate::scheduler::Priority; let mut scheduler = SCHEDULER.lock(); use Priority::*; - scheduler.new_process(High); + let process_0 = scheduler.new_process(High); + scheduler.add_process(process_0); drop(scheduler); interrupts::init_idt(); diff --git a/ableos/src/kernel_state.rs b/ableos/src/kernel_state.rs index e1f9fd8..5205740 100644 --- a/ableos/src/kernel_state.rs +++ b/ableos/src/kernel_state.rs @@ -1,3 +1,4 @@ +use alloc::string::{String, ToString}; use lazy_static::lazy_static; lazy_static! { @@ -6,6 +7,7 @@ lazy_static! { } pub struct KernelInternalState { + hostname: String, should_shutdown: bool, } @@ -13,8 +15,13 @@ impl KernelInternalState { pub fn new() -> Self { Self { should_shutdown: false, + hostname: "".to_string(), } } + + pub fn set_hostname(&mut self, hostname: String) { + self.hostname = hostname; + } pub fn shutdown(&mut self) { self.should_shutdown = true; } diff --git a/ableos/src/kmain.rs b/ableos/src/kmain.rs index 5f1b931..dc6f351 100644 --- a/ableos/src/kmain.rs +++ b/ableos/src/kmain.rs @@ -7,6 +7,7 @@ use crate::{ capabilities::FileAccess, file::{File, PathRep}, scheduler::SCHEDULER, + ALIAS_TABLE, }; // use crate::scheduler; @@ -56,45 +57,20 @@ lazy_static! { pub fn kernel_main() -> ! { init::init(); log::set_max_level(BOOT_CONF.log_level()); - // info!("Initialized"); - - // facepalm::start_facepalm(); - - graphics_pipe_startup(); - - if false { - let mut proc_1 = SimpleSock::new(); - proc_1.register_protocol("ProcessSpawner".to_string()); - - let version = 0; - use rkyv::ser::Serializer; - let mut serializer = AllocSerializer::<0>::default(); - serializer.serialize_value(&version).unwrap(); - - let bytes = serializer.into_serializer().into_inner(); - proc_1.write(bytes.into_vec()); - unsafe { - let the_bytes = proc_1.read(3).unwrap(); - let archived = rkyv::archived_root::(&the_bytes); - let deserialized: Version = archived.deserialize(&mut rkyv::Infallible).unwrap(); - trace!("{:?}", deserialized); - } - } use crate::scheduler::Priority; let mut scheduler = SCHEDULER.lock(); use Priority::*; - let mut xyz = scheduler.new_process(High); - xyz.capabilities.files = FileAccess::Some(vec![PathRep { + let mut process_1 = scheduler.new_process(High); + process_1.capabilities.files = FileAccess::Some(vec![PathRep { location: FileLocations::Home, file_name: "test".to_string(), }]); - - scheduler.add_process(xyz); - for x in &scheduler.list { - trace!("{:?}", x); + scheduler.add_process(process_1); + for ref_process in &scheduler.list { + trace!("{:?}", ref_process); } drop(scheduler); @@ -103,8 +79,8 @@ pub fn kernel_main() -> ! { let mut file_table = FILE_TABLE.lock(); let mut new_file = File::new(FileLocations::Bin, "test".to_string(), "txt".to_string()); - new_file.write_bytes(b"Hello, world!"); + new_file.write_bytes(b"Hello, world!"); file_table.add_file("test", new_file); let file = file_table.get_file("test"); @@ -120,27 +96,12 @@ pub fn kernel_main() -> ! { } } + crate::tests::screen_writer_test(); + use crate::wasm::WasmProgram; let ret = WasmProgram::new_from_bytes(&[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]); trace!("Binary Valid: {:?}", ret.validate_header()); - if BOOT_CONF.run_tests { - // quick and dirty testing framework - screen_writer_test(); - socket_test(); - graphics_pipe_test(); - socket_startup_rng(); - socket_test_rng(); - } - - if BOOT_CONF.run_demos { - graphics_api_demo(); - } - - if BOOT_CONF.run_shader_tests { - shader_tests(); - } - sloop() } @@ -174,136 +135,3 @@ pub fn log_version_data() { master().unwrap().brand_string().unwrap() ); } - -pub fn socket_test() { - let mut xyz = SimpleSock::new(); - - xyz.peek(); - - xyz.write(vec![0, 1, 2, 3]); - - let x = "simple 🧦".to_string().into_bytes(); - xyz.write(x); - - info!("{:?}", &xyz.read(4).unwrap()); - - match &xyz.peek() { - SocketReturns::ReadOk(strr) => { - let out = String::from_utf8_lossy(strr); - - info!("{}", out); - } - SocketReturns::ReadIndexOutOfBounds => todo!(), - SocketReturns::WriteOk => todo!(), - } -} - -pub fn socket_test_rng() { - let mut test_sock = SimpleSock::grab_socket("RNGProvider".to_string()).unwrap(); - info!( - "Recieving {} from protocol {}", - test_sock.read(1).unwrap()[0], - test_sock.protocol().unwrap() - ); -} - -pub fn socket_startup_rng() { - use picorand::{WyRand, RNG}; - let mut random_socket = SimpleSock::new(); - - random_socket.register_protocol("RNGProvider".to_string()); - let bruh = TICK.load(core::sync::atomic::Ordering::Relaxed); - - let mut rng = RNG::::new(bruh); - - for _ in 0..512 { - let x = rng.generate(); - random_socket.write(vec![x]); - } -} - -pub fn screen_writer_test() { - let mut sock_print_id = SimpleSock::new(); - - sock_print_id.register_protocol("Screen Printer".to_string()); - // sock_print_id.write(format!("a原 b画 cフ dァ eイ fル 集").into()); - // sock_print_id.write(format!("⋮").into()); - - sock_print_id.write(format!("λ³ Half Life 3 booting up ㎣").into()); - // sock_print_id.write(format!("Happy birthday 🎉").into()); - // sock_print_id.write(format!("I look forward to ur ai stuff :^>").into()); - // sock_print_id.write(format!("1....2....3....4....5....6....7....8....9").into()); - - for current in (*String::from_utf8_lossy(&sock_print_id.peek().unwrap())).chars() { - vga_e::draw_char(0, 0, current, 0); - } -} - -pub fn vga_boot_screen() { - let mut abcde = SCREEN_BUFFER.lock(); - - for y in 0..480 { - for x in 0..640 { - let segment_x = x * 4 / 640; - let segment_y = y * 4 / 480; - let segment = segment_x + segment_y * 4; - - abcde.set_pixel(x, y, from_vga_16(num_to_vga16(segment as u8))); - } - } -} - -pub fn graphics_pipe_test() { - let mut graphics_pipe = SimpleSock::grab_socket("YCompositor".to_string()).unwrap(); - - let version = Version { - major: 13, - minor: 123, - patch: 0, - }; - use rkyv::ser::Serializer; - let mut serializer = AllocSerializer::<0>::default(); - serializer.serialize_value(&version).unwrap(); - // let bytes = serializer.into_serializer().into_inner(); - - // info!("{:?}", bytes); - let bytes = serializer.into_serializer().into_inner(); - graphics_pipe.write(bytes.into_vec()); - unsafe { - let the_bytes = graphics_pipe.read(3).unwrap(); - let archived = rkyv::archived_root::(&the_bytes); - let deserialized: Version = archived.deserialize(&mut rkyv::Infallible).unwrap(); - trace!("{:?}", deserialized); - } -} - -pub fn graphics_pipe_startup() { - let mut graphics_pipe = SimpleSock::new(); - - graphics_pipe.register_protocol("YCompositor".to_string()); -} - -pub fn graphics_api_demo() { - let mut abcde = SCREEN_BUFFER.lock(); - abcde.force_redraw(); - - abcde.draw_filled_circle(100, 100, 300, 0x0000ff00); - abcde.draw_unfilled_rect(100, 100, 400, 200, 0xff000000); - abcde.draw_filled_rect(300, 300, 400, 400, 0xff000000); - abcde.draw_line(100, 100, 400, 200, 0xff000000); - abcde.copy_to_buffer(); -} - -pub fn shader_tests() { - let mut graphics = SCREEN_BUFFER.lock(); - - graphics.clear(); - graphics.force_redraw(); - graphics.draw_filled_rect(25, 25, 50, 50, 0xff000000); - graphics.set_pixel(50, 50, 0xffffff00); - graphics.shade(); - - graphics.copy_to_buffer(); - - // drop(graphics) -} diff --git a/ableos/src/lib.rs b/ableos/src/lib.rs index 1805912..c959e21 100644 --- a/ableos/src/lib.rs +++ b/ableos/src/lib.rs @@ -82,3 +82,9 @@ pub use wasm::*; ////////////////// pub mod virtio; pub use virtio::*; + +pub mod alias_table; +pub use alias_table::*; + +pub mod tests; +pub use tests::*; diff --git a/ableos/src/scheduler/capabilities.rs b/ableos/src/scheduler/capabilities.rs index 7e53501..0ba1b87 100644 --- a/ableos/src/scheduler/capabilities.rs +++ b/ableos/src/scheduler/capabilities.rs @@ -48,15 +48,14 @@ pub enum NetworkAccess { /// A set of capabilities that a process has #[derive(Clone, Debug, PartialEq)] pub struct Capabilities { + // TODO: Add more capabilities pub files: FileAccess, pub mouse: MouseAccess, pub keyboard: KeyboardAccess, pub controllers: ControllerAccess, pub sound_cards: SoundCardAccess, - pub network_access: NetworkAccess, } - impl Capabilities { /// Generate a set of empty capabilities pub fn empty() -> Self { @@ -69,4 +68,21 @@ impl Capabilities { network_access: NetworkAccess::No, } } + + /// Generate a set of capabilities that allows all access + /// to all devices + /// + /// # Safety + /// This is a very dangerous function and should not be used + /// unless you know what you are doing + pub unsafe fn all() -> Self { + Self { + files: FileAccess::All, + mouse: MouseAccess::Yes, + keyboard: KeyboardAccess::Yes, + controllers: ControllerAccess::All, + sound_cards: SoundCardAccess::All, + network_access: NetworkAccess::Yes, + } + } } diff --git a/ableos/src/scheduler/mod.rs b/ableos/src/scheduler/mod.rs index 9ad5b45..06f8413 100644 --- a/ableos/src/scheduler/mod.rs +++ b/ableos/src/scheduler/mod.rs @@ -8,6 +8,8 @@ use proc::{Process, PID}; #[cfg(test)] mod tests; +use crate::file::PathRep; + use self::capabilities::Capabilities; lazy_static::lazy_static!( @@ -28,14 +30,10 @@ pub enum Priority { Low, } -/// Placeholder -#[derive(Clone, Debug)] -pub struct FileID(u8); - #[derive(Clone, Debug)] pub enum FileAccessTypes { All, - Some(Vec), + Some(Vec), None, } @@ -118,7 +116,19 @@ impl Scheduler { // self.list.push(process); process } - + /// Add a created process to the scheduler + /// + /// # Arguments + /// + /// * `process` - The process to add + /// + /// # Example + /// + /// ``` + /// let mut scheduler = Scheduler::new(); + /// let mut process = scheduler.new_process(Priority::Medium); + /// scheduler.add_process(process); + /// ``` pub fn add_process(&mut self, process: Process) { self.list.push(process); diff --git a/ableos/src/tests.rs b/ableos/src/tests.rs new file mode 100644 index 0000000..090e39c --- /dev/null +++ b/ableos/src/tests.rs @@ -0,0 +1,186 @@ +use alloc::{ + format, + string::{String, ToString}, + vec, +}; +use picorand::PicoRandGenerate; +use rkyv::{ser::serializers::AllocSerializer, Deserialize}; +use shadeable::pixel_format::from_vga_16; +use y_compositor_protocol::Version; + +use crate::{ + kmain::{BOOT_CONF, TICK}, + network::socket::{SimpleSock, Socket, SocketReturns}, + num_to_vga16, vga_e, VgaBuffer, SCREEN_BUFFER, +}; + +pub fn run_tests() { + if BOOT_CONF.run_tests { + // quick and dirty testing framework + screen_writer_test(); + socket_test(); + graphics_pipe_test(); + socket_startup_rng(); + socket_test_rng(); + } + + if BOOT_CONF.run_demos { + graphics_api_demo(); + } + + if BOOT_CONF.run_shader_tests { + shader_tests(); + } +} + +pub fn socket_test() { + let mut xyz = SimpleSock::new(); + + xyz.peek(); + + xyz.write(vec![0, 1, 2, 3]); + + let x = "simple 🧦".to_string().into_bytes(); + xyz.write(x); + + info!("{:?}", &xyz.read(4).unwrap()); + + match &xyz.peek() { + SocketReturns::ReadOk(strr) => { + let out = String::from_utf8_lossy(strr); + + info!("{}", out); + } + SocketReturns::ReadIndexOutOfBounds => todo!(), + SocketReturns::WriteOk => todo!(), + } +} + +pub fn socket_test_rng() { + let mut test_sock = SimpleSock::grab_socket("RNGProvider".to_string()).unwrap(); + info!( + "Recieving {} from protocol {}", + test_sock.read(1).unwrap()[0], + test_sock.protocol().unwrap() + ); +} + +pub fn socket_startup_rng() { + use picorand::{WyRand, RNG}; + let mut random_socket = SimpleSock::new(); + + random_socket.register_protocol("RNGProvider".to_string()); + let bruh = TICK.load(core::sync::atomic::Ordering::Relaxed); + + let mut rng = RNG::::new(bruh); + + for _ in 0..512 { + let x = rng.generate(); + random_socket.write(vec![x]); + } +} + +pub fn screen_writer_test() { + let mut sock_print_id = SimpleSock::new(); + + sock_print_id.register_protocol("Screen Printer".to_string()); + // sock_print_id.write(format!("a原 b画 cフ dァ eイ fル 集").into()); + // sock_print_id.write(format!("⋮").into()); + + sock_print_id.write(format!("λ³ Half Life 3 booting up ㎣").into()); + // sock_print_id.write(format!("Happy birthday 🎉").into()); + // sock_print_id.write(format!("I look forward to ur ai stuff :^>").into()); + // sock_print_id.write(format!("1....2....3....4....5....6....7....8....9").into()); + + for current in (*String::from_utf8_lossy(&sock_print_id.peek().unwrap())).chars() { + vga_e::draw_char(0, 0, current, 0); + } +} + +pub fn vga_boot_screen() { + let mut abcde = SCREEN_BUFFER.lock(); + + for y in 0..480 { + for x in 0..640 { + let segment_x = x * 4 / 640; + let segment_y = y * 4 / 480; + let segment = segment_x + segment_y * 4; + + abcde.set_pixel(x, y, from_vga_16(num_to_vga16(segment as u8))); + } + } +} + +pub fn graphics_pipe_test() { + let mut graphics_pipe = SimpleSock::grab_socket("YCompositor".to_string()).unwrap(); + + let version = Version { + major: 13, + minor: 123, + patch: 0, + }; + use rkyv::ser::Serializer; + let mut serializer = AllocSerializer::<0>::default(); + serializer.serialize_value(&version).unwrap(); + // let bytes = serializer.into_serializer().into_inner(); + + // info!("{:?}", bytes); + let bytes = serializer.into_serializer().into_inner(); + graphics_pipe.write(bytes.into_vec()); + unsafe { + let the_bytes = graphics_pipe.read(3).unwrap(); + let archived = rkyv::archived_root::(&the_bytes); + let deserialized: Version = archived.deserialize(&mut rkyv::Infallible).unwrap(); + trace!("{:?}", deserialized); + } +} + +pub fn graphics_pipe_startup() { + let mut graphics_pipe = SimpleSock::new(); + + graphics_pipe.register_protocol("YCompositor".to_string()); +} + +pub fn graphics_api_demo() { + let mut abcde = SCREEN_BUFFER.lock(); + abcde.force_redraw(); + + abcde.draw_filled_circle(100, 100, 300, 0x0000ff00); + abcde.draw_unfilled_rect(100, 100, 400, 200, 0xff000000); + abcde.draw_filled_rect(300, 300, 400, 400, 0xff000000); + abcde.draw_line(100, 100, 400, 200, 0xff000000); + abcde.copy_to_buffer(); +} + +pub fn shader_tests() { + let mut graphics = SCREEN_BUFFER.lock(); + + graphics.clear(); + graphics.force_redraw(); + graphics.draw_filled_rect(25, 25, 50, 50, 0xff000000); + graphics.set_pixel(50, 50, 0xffffff00); + graphics.shade(); + + graphics.copy_to_buffer(); + + // drop(graphics) +} + +pub fn ser_de_test() { + let mut proc_1 = SimpleSock::new(); + proc_1.register_protocol("ProcessSpawner".to_string()); + + let version = 0; + use rkyv::ser::Serializer; + let mut serializer = AllocSerializer::<0>::default(); + serializer.serialize_value(&version).unwrap(); + + let bytes = serializer.into_serializer().into_inner(); + proc_1.write(bytes.into_vec()); + unsafe { + let the_bytes = proc_1.read(3).unwrap(); + let archived = rkyv::archived_root::(&the_bytes); + let deserialized: Version = archived.deserialize(&mut rkyv::Infallible).unwrap(); + trace!("{:?}", deserialized); + } +}