1
0
Fork 0
forked from AbleOS/ableos

reduce race conditions

This commit is contained in:
Able 2022-01-27 01:37:12 -06:00
parent 03a30e5109
commit 13edd25c8b
8 changed files with 272 additions and 190 deletions

View file

@ -0,0 +1,28 @@
use alloc::string::{String, ToString};
use hashbrown::HashMap;
lazy_static::lazy_static! {
pub static ref ALIAS_TABLE: spin::Mutex<AliasTable> = 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<String, String>,
}
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<String> {
let alias_table = ALIAS_TABLE.lock();
alias_table.table.get(&alias).map(|x| x.to_string())
}
}

View file

@ -15,7 +15,8 @@ pub fn init() {
use crate::scheduler::Priority; use crate::scheduler::Priority;
let mut scheduler = SCHEDULER.lock(); let mut scheduler = SCHEDULER.lock();
use Priority::*; use Priority::*;
scheduler.new_process(High); let process_0 = scheduler.new_process(High);
scheduler.add_process(process_0);
drop(scheduler); drop(scheduler);
interrupts::init_idt(); interrupts::init_idt();

View file

@ -1,3 +1,4 @@
use alloc::string::{String, ToString};
use lazy_static::lazy_static; use lazy_static::lazy_static;
lazy_static! { lazy_static! {
@ -6,6 +7,7 @@ lazy_static! {
} }
pub struct KernelInternalState { pub struct KernelInternalState {
hostname: String,
should_shutdown: bool, should_shutdown: bool,
} }
@ -13,8 +15,13 @@ impl KernelInternalState {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
should_shutdown: false, should_shutdown: false,
hostname: "".to_string(),
} }
} }
pub fn set_hostname(&mut self, hostname: String) {
self.hostname = hostname;
}
pub fn shutdown(&mut self) { pub fn shutdown(&mut self) {
self.should_shutdown = true; self.should_shutdown = true;
} }

View file

@ -7,6 +7,7 @@ use crate::{
capabilities::FileAccess, capabilities::FileAccess,
file::{File, PathRep}, file::{File, PathRep},
scheduler::SCHEDULER, scheduler::SCHEDULER,
ALIAS_TABLE,
}; };
// use crate::scheduler; // use crate::scheduler;
@ -56,45 +57,20 @@ lazy_static! {
pub fn kernel_main() -> ! { pub fn kernel_main() -> ! {
init::init(); init::init();
log::set_max_level(BOOT_CONF.log_level()); 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::<Version>(&the_bytes);
let deserialized: Version = archived.deserialize(&mut rkyv::Infallible).unwrap();
trace!("{:?}", deserialized);
}
}
use crate::scheduler::Priority; use crate::scheduler::Priority;
let mut scheduler = SCHEDULER.lock(); let mut scheduler = SCHEDULER.lock();
use Priority::*; use Priority::*;
let mut xyz = scheduler.new_process(High); let mut process_1 = scheduler.new_process(High);
xyz.capabilities.files = FileAccess::Some(vec![PathRep { process_1.capabilities.files = FileAccess::Some(vec![PathRep {
location: FileLocations::Home, location: FileLocations::Home,
file_name: "test".to_string(), file_name: "test".to_string(),
}]); }]);
scheduler.add_process(process_1);
scheduler.add_process(xyz); for ref_process in &scheduler.list {
for x in &scheduler.list { trace!("{:?}", ref_process);
trace!("{:?}", x);
} }
drop(scheduler); drop(scheduler);
@ -103,8 +79,8 @@ pub fn kernel_main() -> ! {
let mut file_table = FILE_TABLE.lock(); let mut file_table = FILE_TABLE.lock();
let mut new_file = File::new(FileLocations::Bin, "test".to_string(), "txt".to_string()); 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); file_table.add_file("test", new_file);
let file = file_table.get_file("test"); let file = file_table.get_file("test");
@ -120,27 +96,12 @@ pub fn kernel_main() -> ! {
} }
} }
crate::tests::screen_writer_test();
use crate::wasm::WasmProgram; use crate::wasm::WasmProgram;
let ret = WasmProgram::new_from_bytes(&[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]); let ret = WasmProgram::new_from_bytes(&[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]);
trace!("Binary Valid: {:?}", ret.validate_header()); 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() sloop()
} }
@ -174,136 +135,3 @@ pub fn log_version_data() {
master().unwrap().brand_string().unwrap() 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::<WyRand, u8>::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::<Version>(&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)
}

View file

@ -82,3 +82,9 @@ pub use wasm::*;
////////////////// //////////////////
pub mod virtio; pub mod virtio;
pub use virtio::*; pub use virtio::*;
pub mod alias_table;
pub use alias_table::*;
pub mod tests;
pub use tests::*;

View file

@ -48,15 +48,14 @@ pub enum NetworkAccess {
/// A set of capabilities that a process has /// A set of capabilities that a process has
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct Capabilities { pub struct Capabilities {
// TODO: Add more capabilities
pub files: FileAccess, pub files: FileAccess,
pub mouse: MouseAccess, pub mouse: MouseAccess,
pub keyboard: KeyboardAccess, pub keyboard: KeyboardAccess,
pub controllers: ControllerAccess, pub controllers: ControllerAccess,
pub sound_cards: SoundCardAccess, pub sound_cards: SoundCardAccess,
pub network_access: NetworkAccess, pub network_access: NetworkAccess,
} }
impl Capabilities { impl Capabilities {
/// Generate a set of empty capabilities /// Generate a set of empty capabilities
pub fn empty() -> Self { pub fn empty() -> Self {
@ -69,4 +68,21 @@ impl Capabilities {
network_access: NetworkAccess::No, 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,
}
}
} }

View file

@ -8,6 +8,8 @@ use proc::{Process, PID};
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
use crate::file::PathRep;
use self::capabilities::Capabilities; use self::capabilities::Capabilities;
lazy_static::lazy_static!( lazy_static::lazy_static!(
@ -28,14 +30,10 @@ pub enum Priority {
Low, Low,
} }
/// Placeholder
#[derive(Clone, Debug)]
pub struct FileID(u8);
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum FileAccessTypes { pub enum FileAccessTypes {
All, All,
Some(Vec<FileID>), Some(Vec<PathRep>),
None, None,
} }
@ -118,7 +116,19 @@ impl Scheduler {
// self.list.push(process); // self.list.push(process);
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) { pub fn add_process(&mut self, process: Process) {
self.list.push(process); self.list.push(process);

186
ableos/src/tests.rs Normal file
View file

@ -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::<WyRand, u8>::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::<Version>(&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::<Version>(&the_bytes);
let deserialized: Version = archived.deserialize(&mut rkyv::Infallible).unwrap();
trace!("{:?}", deserialized);
}
}