1
0
Fork 0
forked from AbleOS/ableos

speed up cpu + ipc

This commit is contained in:
able 2022-07-29 06:13:26 -05:00
parent 3f4c9fdb5a
commit dc83d8e175
8 changed files with 228 additions and 53 deletions

View file

@ -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 => (),
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);
}

View file

@ -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 {

View file

@ -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<ChannelMessage>,
}
impl Channel {
pub fn new() -> Self {
pub fn new<S: Into<String>>(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<ChannelMessage, ChannelError> {
pub fn read(&mut self) -> Result<ChannelMessage, IPCError> {
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<S: Into<String>>(data: S) -> Result<Self, IPCError> {
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() {}
}

View file

@ -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<IPCService> = spin::Mutex::new(IPCService {
sockets: HashMap::new(),
channels: HashMap::new(),
public_board: vec![],
});
}
pub struct IPCService {
pub sockets: HashMap<Handle, Socket>,
pub channels: HashMap<Handle, Socket>,
pub channels: HashMap<Handle, Channel>,
// TODO: Add a public board of each down below which use some method of pointing to the above
pub public_board: Vec<Handle>,
}
impl IPCService {
pub fn examine_board(&self) -> Vec<Handle> {
self.public_board.clone()
}
}
// Socket Related Impl
impl IPCService {
pub fn create_socket(&mut self) -> Handle {
pub fn create_socket<S: Into<String>>(&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: String) -> Result<(), SocketError> {
pub fn send_socket<S: Into<Vec<u8>>>(
&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);
match sock {
Some(socket) => {
return socket.write(data.into());
}
None => return Err(IPCError::NonexistantSocket),
}
}
_ => return Err(IPCError::InvalidHandle),
}
}
pub fn read_socket(&mut self, handle: Handle) -> Result<Vec<u8>, 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<S: Into<String>>(&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<ChannelMessage, IPCError> {
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(SocketError::NonexistantSocket),
None => return Err(IPCError::NonexistantSocket),
}
}
_ => return Err(IPCError::InvalidHandle),
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum IPCError {
InvalidHandle,
NonexistantSocket,
EmptySocket,
EmptyChannel,
InvalidMessage,
}

View file

@ -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<String>,
pub public: bool,
name: String,
stream: Vec<u8>,
}
impl Socket {
pub fn new() -> Self {
pub fn new<S: Into<String>>(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() {
// <S: Into<String>>(name: S)
pub fn write<S: Into<Vec<u8>>>(&mut self, data: S) -> Result<(), IPCError> {
for c in data.into() {
self.stream.push(c as u8);
}
Ok(())
}
pub fn read(&mut self) -> Result<Vec<u8>, SocketError> {
pub fn read(&mut self) -> Result<Vec<u8>, 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;
}
}

View file

@ -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<KernelConfig> = 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());

View file

@ -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

View file

@ -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
}