From 64d6e1e1668b5d6f2e3264600d9206d47e954896 Mon Sep 17 00:00:00 2001 From: Able Date: Tue, 22 Feb 2022 18:15:16 -0600 Subject: [PATCH] PIT Timer settable --- ableos/Cargo.lock | 2 +- ableos/Cargo.toml | 2 +- ableos/src/arch/x86_64/interrupts.rs | 38 ++++++---- ableos/src/kmain.rs | 2 +- ableos/src/lib.rs | 2 + ableos/src/messaging/NOTES.md | 0 ableos/src/messaging/mod.rs | 100 +++++++++++++++++++++++++++ ableos/src/rhai_shell/mod.rs | 2 +- ableos/src/scratchpad.rs | 39 ++++------- ableos/src/utils.rs | 2 +- 10 files changed, 144 insertions(+), 45 deletions(-) create mode 100644 ableos/src/messaging/NOTES.md create mode 100644 ableos/src/messaging/mod.rs diff --git a/ableos/Cargo.lock b/ableos/Cargo.lock index 35cf6a0..9938dab 100644 --- a/ableos/Cargo.lock +++ b/ableos/Cargo.lock @@ -24,7 +24,7 @@ dependencies = [ [[package]] name = "ableos" -version = "0.1.0" +version = "0.1.1" dependencies = [ "ab_glyph", "acpi", diff --git a/ableos/Cargo.toml b/ableos/Cargo.toml index 9802670..5f2383f 100644 --- a/ableos/Cargo.toml +++ b/ableos/Cargo.toml @@ -1,7 +1,7 @@ [package] edition = "2021" name = "ableos" -version = "0.1.0" +version = "0.1.1" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [profile.release] diff --git a/ableos/src/arch/x86_64/interrupts.rs b/ableos/src/arch/x86_64/interrupts.rs index 1649093..2f6fd55 100644 --- a/ableos/src/arch/x86_64/interrupts.rs +++ b/ableos/src/arch/x86_64/interrupts.rs @@ -5,6 +5,7 @@ use crate::{ rhai_shell::KEYBUFF, }; +use cpuio::outb; use lazy_static::lazy_static; use pic8259::ChainedPics; use spin; @@ -20,6 +21,8 @@ pub static PICS: spin::Mutex = pub enum InterruptIndex { Timer = PIC_1_OFFSET, Keyboard, + // SecondInterrupt = PIC_2_OFFSET, + Cmos = 0x70, } impl InterruptIndex { fn as_u8(self) -> u8 { @@ -38,10 +41,18 @@ lazy_static! { idt.breakpoint.set_handler_fn(breakpoint_handler); unsafe { idt.double_fault.set_handler_fn(double_fault_handler) - .set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX); // new + .set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX); } + + // This gives fast interrupts + set_pit_frequency(1000); + idt[InterruptIndex::Timer.as_usize()].set_handler_fn(timer_interrupt_handler); idt[InterruptIndex::Keyboard.as_usize()] .set_handler_fn(keyboard_interrupt_handler); + + idt[6].set_handler_fn(floppy_disk_interrupt_handler); + + idt }; } @@ -128,16 +139,17 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac } } -/* -extern "x86-interrupt" fn page_fault_handler( - stack_frame: InterruptStackFrame, - error_code: PageFaultErrorCode, -) { - use x86_64::registers::control::Cr2; - println!["Exception: Page Fault"]; - println!["Address: {:?}", Cr2::read()]; - println!["Error Code: {:?}", error_code]; - println!["{:#?}", stack_frame]; - sloop(); +extern "x86-interrupt" fn floppy_disk_interrupt_handler(_stack_frame: InterruptStackFrame) { + println!("EXCEPTION: FLOPPY DISK"); +} + +fn set_pit_frequency(freq: u32) { + let divisor: u16 = (1193180 / freq).try_into().unwrap(); + + unsafe { + outb(0x36, 0x43); + + outb((divisor & 0xFF) as u8, 0x40); + outb((divisor >> 8) as u8, 0x40); + } } -*/ diff --git a/ableos/src/kmain.rs b/ableos/src/kmain.rs index 64caa08..5e6ab8c 100644 --- a/ableos/src/kmain.rs +++ b/ableos/src/kmain.rs @@ -110,7 +110,7 @@ pub fn kernel_main() -> ! { /// called by arch specific timers to tick up all kernel related functions pub fn tick() { let mut data = TICK.load(Relaxed); - data += 1; + data = data.wrapping_add(1); crate::kernel_state::KERNEL_STATE.lock().update_state(); // let mut scheduler = SCHEDULER.lock(); diff --git a/ableos/src/lib.rs b/ableos/src/lib.rs index c78ef88..c54c348 100644 --- a/ableos/src/lib.rs +++ b/ableos/src/lib.rs @@ -97,3 +97,5 @@ pub use syscalls::*; pub mod scratchpad; pub use scratchpad::*; pub mod filesystem; + +pub mod messaging; diff --git a/ableos/src/messaging/NOTES.md b/ableos/src/messaging/NOTES.md new file mode 100644 index 0000000..e69de29 diff --git a/ableos/src/messaging/mod.rs b/ableos/src/messaging/mod.rs new file mode 100644 index 0000000..ac47f22 --- /dev/null +++ b/ableos/src/messaging/mod.rs @@ -0,0 +1,100 @@ +use { + crate::{kmain::TICK, proc::PID}, + alloc::string::String, + core::{ + fmt::{Display, Formatter, Result as FmtResult}, + result::Result, + sync::atomic::Ordering::Relaxed, + }, + libwasm::syscalls::time_calls::SecondsTime, + x86_64::instructions::interrupts::{disable, enable}, +}; +#[derive(Debug)] +pub enum MessagingError { + MessageTooLarge, +} + +impl Display for MessagingError { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + match self { + MessagingError::MessageTooLarge => write!(f, "Message too large"), + } + } +} + +/// Not properly implemented +pub fn fetch_time() -> SecondsTime { + disable(); + let time = TICK.load(Relaxed); + enable(); + + SecondsTime { + seconds: time, + milliseconds: 0, + } +} + +pub type MessageSmall = [u8; 2048]; +pub type MessageLarge = [u8; 16384]; + +pub enum Message { + Small(MessageSmall), + Large(MessageLarge), +} + +pub struct ProcessMessage { + pub to_pid: PID, + pub from_pid: PID, + pub message: Message, + pub sender_time: SecondsTime, +} + +impl ProcessMessage { + pub fn new(to_pid: PID, from_pid: PID, message: Message) -> Self { + Self { + to_pid, + from_pid, + message, + sender_time: fetch_time(), + } + } + + pub fn new_from_string( + to_pid: PID, + from_pid: PID, + string: String, + ) -> Result { + let mut mssg = Message::Small([0u8; 2048]); + match string.len() { + 0..=2047 => { + let mut message = [0u8; 2048]; + for (i, c) in string.chars().enumerate() { + message[i] = c as u8; + } + mssg = Message::Small(message); + } + + 2048..=16383 => { + let mut message = [0u8; 16384]; + for (i, c) in string.chars().enumerate() { + message[i] = c as u8; + } + mssg = Message::Large(message); + } + _ => return Err(MessagingError::MessageTooLarge), + } + + Ok(Self { + to_pid, + from_pid, + message: mssg, + sender_time: fetch_time(), + }) + } +} + +pub fn send_sync_message(message: ProcessMessage) {} +pub fn send_async_message(message: ProcessMessage) {} + +pub fn recv_sync_message() {} +pub fn recv_async_message() {} diff --git a/ableos/src/rhai_shell/mod.rs b/ableos/src/rhai_shell/mod.rs index 1fde8dc..b20fb02 100644 --- a/ableos/src/rhai_shell/mod.rs +++ b/ableos/src/rhai_shell/mod.rs @@ -49,13 +49,13 @@ pub fn afetch() { disable(); let tick_time = TICK.load(Relaxed); - enable(); println!("OS: AbleOS"); println!("Host: {}", kstate.hostname); println!("Kernel: AKern-{}-v{}", RELEASE_TYPE, KERNEL_VERSION); println!("Uptime: {}", tick_time); + enable(); drop(kstate); } pub fn set_hostname(name: String) { diff --git a/ableos/src/scratchpad.rs b/ableos/src/scratchpad.rs index e4645a9..85939ac 100644 --- a/ableos/src/scratchpad.rs +++ b/ableos/src/scratchpad.rs @@ -1,4 +1,9 @@ -use crate::{absi::colorify, rhai_shell::rhai_shell}; +use core::arch::asm; + +use alloc::string::{String, ToString}; +use cpuio::outb; + +use crate::{absi::colorify, messaging::ProcessMessage, rhai_shell::rhai_shell}; use { crate::{ @@ -38,6 +43,12 @@ pub fn scratchpad() { */ // interp(); // crate::experiments::absi::colorify(); + + let message = "Hello, world!"; + + let xyz = ProcessMessage::new_from_string(PID(123), PID(0), message.to_string()); + + // print!("{:?}", xyz); rhai_shell(); } @@ -68,29 +79,3 @@ impl PortOps for PciIO { cpuio::outl(val, port as u16); } } -/* -/// An experimental process message format -pub struct ProcessMessage { - pub to_pid: PID, - pub from_pid: PID, - pub message: [u8; 2048], - - pub sender_time: SecondsTime, -} -// -// use libwasm::syscalls::time_calls::SecondsTime; - -impl ProcessMessage { - pub fn new(to_pid: PID, from_pid: PID, message: [u8; 2048]) -> Self { - ProcessMessage { - to_pid, - from_pid, - message, - sender_time: SecondsTime { - seconds: 0, - milliseconds: 0, - }, - } - } -} -*/ diff --git a/ableos/src/utils.rs b/ableos/src/utils.rs index 5ec7612..d8de1fa 100644 --- a/ableos/src/utils.rs +++ b/ableos/src/utils.rs @@ -2,7 +2,7 @@ //! //! //! - +#[inline] pub fn type_of(_: &T) -> &str { core::any::type_name::() }