From dc83d8e175d4198d4dccf99bb30b6dd03aeac203 Mon Sep 17 00:00:00 2001 From: able Date: Fri, 29 Jul 2022 06:13:26 -0500 Subject: [PATCH] speed up cpu + ipc --- ableos/src/arch/x86_64/interrupts.rs | 31 ++++-- ableos/src/handle.rs | 4 +- ableos/src/ipc/channel.rs | 39 ++++++-- ableos/src/ipc/mod.rs | 141 ++++++++++++++++++++++++--- ableos/src/ipc/socket.rs | 29 +++--- ableos/src/kmain.rs | 26 ++++- ableos/src/logger.rs | 3 +- ableos/src/time.rs | 8 +- 8 files changed, 228 insertions(+), 53 deletions(-) diff --git a/ableos/src/arch/x86_64/interrupts.rs b/ableos/src/arch/x86_64/interrupts.rs index d66ea7d14..831f55ace 100644 --- a/ableos/src/arch/x86_64/interrupts.rs +++ b/ableos/src/arch/x86_64/interrupts.rs @@ -70,8 +70,8 @@ extern "x86-interrupt" fn double_fault_handler( #[naked] extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFrame) { use super::task_switcher; - unsafe { + // print!("."); asm!( // Kernel tick "call {tick}", @@ -121,10 +121,17 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac print!("{chr}"); } }, - DecodedKey::RawKey(key) => match KeyCode::from(key) { - KeyCode::AltLeft | KeyCode::AltRight => (), - kc => print!("{kc:?}"), - }, + DecodedKey::RawKey(key) => { + use KeyCode::*; + match KeyCode::from(key) { + AltLeft | AltRight => (), + ArrowDown | ArrowRight | ArrowLeft | ArrowUp => { + warn!("ArrowKeys are unsupported currently"); + } + + kc => print!("{kc:?}"), + }; + } } } @@ -143,7 +150,17 @@ pub fn init_idt() { } pub fn set_pit_frequency(pit: u16, freq: u32) { - let divisor: u16 = (1193180 / freq).try_into().unwrap(); + let ret = (1193180 / freq).try_into(); + + let divisor: u16 = match ret { + Ok(div) => div, + Err(err) => { + error!("{}", err); + + warn!("Defaulting to 1000 on PIT{}", pit); + 1000 + } + }; unsafe { outb(0x36, 0x43); @@ -164,7 +181,7 @@ pub fn set_pit_3(freq: u32) { } pub fn reset_pit_for_cpu() { - set_pit_1(1000); + set_pit_1(50); set_pit_2(1000); set_pit_3(1000); } diff --git a/ableos/src/handle.rs b/ableos/src/handle.rs index b6fd420e1..8f6f162e0 100644 --- a/ableos/src/handle.rs +++ b/ableos/src/handle.rs @@ -20,8 +20,8 @@ pub enum HandleResource { #[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)] pub struct Handle { - inner: u128, - res: HandleResource, + pub inner: u128, + pub res: HandleResource, } impl Display for Handle { diff --git a/ableos/src/ipc/channel.rs b/ableos/src/ipc/channel.rs index 33daa452d..852ed2aed 100644 --- a/ableos/src/ipc/channel.rs +++ b/ableos/src/ipc/channel.rs @@ -1,5 +1,7 @@ use alloc::collections::VecDeque; +use super::IPCError; + #[derive(Debug)] pub struct ChannelPermission { @@ -13,26 +15,33 @@ pub struct ChannelPermission { #[derive(Debug)] pub struct Channel { - // public: bool, + pub public: bool, + name: String, inner: VecDeque, } impl Channel { - pub fn new() -> Self { + pub fn new>(name: S, public: bool) -> Self { let deq = VecDeque::from([]); - Self { inner: deq } + Self { + public, + name: name.into(), + inner: deq, + } } - pub fn read(&mut self) -> Result { + pub fn read(&mut self) -> Result { if let Some(msg) = self.inner.pop_front() { return Ok(msg); } - return Err(ChannelError::EmptyBuffer); + return Err(IPCError::EmptyChannel); } - pub fn send(&mut self, data: ChannelMessage) { + pub fn write(&mut self, data: ChannelMessage) -> Result<(), IPCError> { self.inner.push_back(data); + + Ok(()) } } @@ -47,8 +56,20 @@ pub struct ChannelMessage { } impl ChannelMessage { - pub fn new(data: [u8; 4096]) -> Self { - Self { inner: data } + pub fn new>(data: S) -> Result { + let data = data.into(); + let mut message = Self { inner: [0; 4096] }; + + if data.len() > 4096 { + return Err(IPCError::InvalidMessage); + } else { + let mut idx = 0; + for b in data.bytes() { + message.inner[idx] = b; + idx += 1; + } + } + + Ok(message) } - pub fn from_string() {} } diff --git a/ableos/src/ipc/mod.rs b/ableos/src/ipc/mod.rs index d648cc3e9..997321725 100644 --- a/ableos/src/ipc/mod.rs +++ b/ableos/src/ipc/mod.rs @@ -1,10 +1,10 @@ use hashbrown::HashMap; -use crate::handle::Handle; +use crate::handle::{Handle, HandleResource}; use self::{ - channel::Channel, - socket::{Socket, SocketError}, + channel::{Channel, ChannelMessage}, + socket::Socket, }; pub mod channel; @@ -14,33 +14,148 @@ lazy_static::lazy_static! { pub static ref IPC: spin::Mutex = spin::Mutex::new(IPCService { sockets: HashMap::new(), channels: HashMap::new(), + public_board: vec![], }); } pub struct IPCService { pub sockets: HashMap, - pub channels: HashMap, + pub channels: HashMap, // TODO: Add a public board of each down below which use some method of pointing to the above + pub public_board: Vec, +} + +impl IPCService { + pub fn examine_board(&self) -> Vec { + self.public_board.clone() + } } // Socket Related Impl impl IPCService { - pub fn create_socket(&mut self) -> Handle { + pub fn create_socket>(&mut self, name: S, public: bool) -> Handle { let handle = Handle::new(crate::handle::HandleResource::Socket); - let sock = Socket::new(); + let mut sock = Socket::new(name.into()); + if public { + sock.public = true; + + self.public_board.push(handle); + } + self.sockets.insert(handle.clone(), sock); handle } + pub fn send_socket>>( + &mut self, + handle: Handle, + data: S, + ) -> Result<(), IPCError> { + trace!("Sending data to {}", handle); + match handle { + Handle { + inner: _inner, + res: HandleResource::Socket, + } => { + let sock = self.sockets.get_mut(&handle); - pub fn send_socket(&mut self, handle: Handle, data: String) -> Result<(), SocketError> { - let sock = self.sockets.get_mut(&handle); - - match sock { - Some(socket) => { - return socket.write(data); + match sock { + Some(socket) => { + return socket.write(data.into()); + } + None => return Err(IPCError::NonexistantSocket), + } } - None => return Err(SocketError::NonexistantSocket), + _ => return Err(IPCError::InvalidHandle), + } + } + + pub fn read_socket(&mut self, handle: Handle) -> Result, IPCError> { + trace!("Reading data from {}", handle); + + match handle { + Handle { + inner: _inner, + res: HandleResource::Socket, + } => { + let sock = self.sockets.get_mut(&handle); + + match sock { + Some(socket) => { + return socket.read(); + } + None => return Err(IPCError::NonexistantSocket), + } + } + _ => return Err(IPCError::InvalidHandle), } } } + +impl IPCService { + pub fn create_channel>(&mut self, name: S, public: bool) -> Handle { + let handle = Handle::new(crate::handle::HandleResource::Channel); + + let mut chan = Channel::new(name.into(), public); + if public { + chan.public = true; + + self.public_board.push(handle); + } + + self.channels.insert(handle.clone(), chan); + + handle + } + + pub fn read_channel(&mut self, handle: Handle) -> Result { + trace!("Reading data from {}", handle); + + match handle { + Handle { + inner: _inner, + res: HandleResource::Channel, + } => { + let sock = self.channels.get_mut(&handle); + + match sock { + Some(socket) => { + return socket.read(); + } + None => return Err(IPCError::NonexistantSocket), + } + } + _ => return Err(IPCError::InvalidHandle), + } + } + + pub fn send_channel(&mut self, handle: Handle, data: ChannelMessage) -> Result<(), IPCError> { + trace!("Sending data to {}", handle); + match handle { + Handle { + inner: _inner, + res: HandleResource::Channel, + } => { + let sock = self.channels.get_mut(&handle); + + match sock { + Some(socket) => { + return socket.write(data); + } + None => return Err(IPCError::NonexistantSocket), + } + } + _ => return Err(IPCError::InvalidHandle), + } + } +} + +#[derive(Debug)] +#[non_exhaustive] +pub enum IPCError { + InvalidHandle, + NonexistantSocket, + EmptySocket, + EmptyChannel, + InvalidMessage, +} diff --git a/ableos/src/ipc/socket.rs b/ableos/src/ipc/socket.rs index 1546c3971..5956d6781 100644 --- a/ableos/src/ipc/socket.rs +++ b/ableos/src/ipc/socket.rs @@ -1,40 +1,43 @@ // SEEALSO: https://git.ablecorp.us/able/ableos/src/branch/master/ableos/src/relib/network/socket.rs -pub enum SocketError { - NonexistantSocket, - EmptySocket, -} +use super::IPCError; + #[derive(Debug)] pub struct Socket { - name: Option, + pub public: bool, + name: String, stream: Vec, } impl Socket { - pub fn new() -> Self { + pub fn new>(name: S) -> Self { Self { - name: None, + public: false, + name: name.into(), stream: vec![], } } - pub fn write(&mut self, data: String) -> Result<(), SocketError> { - for c in data.chars() { + + // >(name: S) + + pub fn write>>(&mut self, data: S) -> Result<(), IPCError> { + for c in data.into() { self.stream.push(c as u8); } Ok(()) } - pub fn read(&mut self) -> Result, SocketError> { + pub fn read(&mut self) -> Result, IPCError> { if self.stream.len() != 0 { let skt = self.stream.clone(); self.stream = vec![]; return Ok(skt); } - return Err(SocketError::EmptySocket); + return Err(IPCError::EmptySocket); } - pub fn name(&mut self, name: String) { - self.name = Some(name); + pub fn rename(&mut self, name: String) { + self.name = name; } } diff --git a/ableos/src/kmain.rs b/ableos/src/kmain.rs index 986a54fcc..fa052bee9 100644 --- a/ableos/src/kmain.rs +++ b/ableos/src/kmain.rs @@ -3,9 +3,11 @@ use core::sync::atomic::Ordering; use crate::arch::drivers::sysinfo::master; +use crate::ipc::channel::ChannelMessage; use crate::ipc::{self, IPC}; use crate::scheduler::SCHEDULER; use crate::time::fetch_time; +use crate::SectionType; use crate::{ arch::{init, sloop}, relib::network::socket::{SimpleSock, Socket}, @@ -13,7 +15,9 @@ use crate::{ }; use crate::{boot_conf::KernelConfig, systeminfo::RELEASE_TYPE}; use kernel::{KERNEL_VERSION, TICK}; +use libwasm::syscalls::time_calls::get_time; use spin::Lazy; +use x86_64::instructions::interrupts::{disable, enable}; // TODO: Change this structure to allow for multiple cores loaded pub static KERNEL_CONF: Lazy = Lazy::new(KernelConfig::new); @@ -31,18 +35,34 @@ pub fn kernel_main() -> ! { let mut ipc_service = IPC.lock(); // create some channels or whatever here then drop it - let handle = ipc_service.create_socket(); - ipc_service.send_socket(handle, "nerd".to_string()); + let log_handle = ipc_service.create_channel("LOG", true); drop(ipc_service); x86_64::instructions::interrupts::without_interrupts(|| { - SCHEDULER.lock().enqueue_spawn(scratchpad); + let mut scheduler = SCHEDULER.lock(); + + // TODO: This task never gets run + scheduler.enqueue_spawn(traceloop); + + // + scheduler.enqueue_spawn(scratchpad); }); sloop() } +pub fn traceloop() { + let mut last_time = 0.0; + loop { + // let time = fetch_time(); + // if time > last_time { + // last_time = time; + // trace!("Timer"); + // } + } +} + pub fn cpu_socket_startup() { let mut cpu_info_socket = SimpleSock::new(); cpu_info_socket.register_protocol("CPU_INFO".to_string()); diff --git a/ableos/src/logger.rs b/ableos/src/logger.rs index 8b7af29d4..2482dd892 100644 --- a/ableos/src/logger.rs +++ b/ableos/src/logger.rs @@ -46,7 +46,7 @@ impl log::Log for SimpleLogger { record.args() ); } - + /* let log_socket_id = SimpleSock::grab_socket("Logger".to_string()); match log_socket_id { Some(mut log_socket_id) => { @@ -54,6 +54,7 @@ impl log::Log for SimpleLogger { } None => warn!("No socket found for Logger"), } + */ } } /// Clear the log buffer diff --git a/ableos/src/time.rs b/ableos/src/time.rs index d0f232f82..7184a6417 100644 --- a/ableos/src/time.rs +++ b/ableos/src/time.rs @@ -3,11 +3,9 @@ use kernel::TICK; #[cfg(target_arch = "x86_64")] pub fn fetch_time() -> f64 { - use x86_64::instructions::interrupts::{disable, enable}; - - disable(); - let time = TICK.load(Ordering::Relaxed) as f64; - enable(); + let time = x86_64::instructions::interrupts::without_interrupts(|| { + TICK.load(Ordering::Relaxed) as f64 + }); time }