From 6c3a67e6b5d6b709e8c1c413ffd7eecea6124ecc Mon Sep 17 00:00:00 2001 From: Able Date: Mon, 28 Feb 2022 08:54:41 -0600 Subject: [PATCH] seperate the core kernel into its own directory --- .gitignore | 1 + ableos/Cargo.lock | 8 ++ ableos/Cargo.toml | 6 ++ ableos/src/arch/x86_64/interrupts.rs | 2 +- ableos/src/kmain.rs | 14 --- ableos/src/lib.rs | 14 +-- ableos/src/logger.rs | 2 +- ableos/src/messaging/NOTES.md | 0 ableos/src/messaging/mod.rs | 100 ------------------- ableos/src/panic.rs | 22 ---- ableos/src/rhai_shell/mod.rs | 2 +- ableos/src/tests.rs | 3 +- ableos/src/wasm_jumploader/host_functions.rs | 2 +- kernel/Cargo.lock | 16 +++ kernel/Cargo.toml | 8 ++ kernel/src/kernel_time.rs | 10 ++ kernel/src/lib.rs | 25 +++++ kernel/src/messaging.rs | 65 ++++++++++++ kernel/src/panic.rs | 8 ++ kernel/src/proccess.rs | 4 + 20 files changed, 164 insertions(+), 148 deletions(-) delete mode 100644 ableos/src/messaging/NOTES.md delete mode 100644 ableos/src/messaging/mod.rs delete mode 100644 ableos/src/panic.rs create mode 100644 kernel/Cargo.lock create mode 100644 kernel/Cargo.toml create mode 100644 kernel/src/kernel_time.rs create mode 100644 kernel/src/lib.rs create mode 100644 kernel/src/messaging.rs create mode 100644 kernel/src/panic.rs create mode 100644 kernel/src/proccess.rs diff --git a/.gitignore b/.gitignore index 7cc83998..06901ea0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ facepalm/target shadeable/target qprofiler userland/*/target +kernel/target diff --git a/ableos/Cargo.lock b/ableos/Cargo.lock index 9938dabe..5f0e17a8 100644 --- a/ableos/Cargo.lock +++ b/ableos/Cargo.lock @@ -35,6 +35,7 @@ dependencies = [ "facepalm", "genfs", "hashbrown", + "kernel", "lazy_static", "libwasm", "linked_list_allocator", @@ -270,6 +271,13 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" +[[package]] +name = "kernel" +version = "0.1.1" +dependencies = [ + "lazy_static", +] + [[package]] name = "lazy_static" version = "1.4.0" diff --git a/ableos/Cargo.toml b/ableos/Cargo.toml index 961bb183..283ff547 100644 --- a/ableos/Cargo.toml +++ b/ableos/Cargo.toml @@ -49,6 +49,10 @@ test-args = [ ] [dependencies] + + + + linked_list_allocator = "0.9.0" lliw = "0.2.0" # qoi_rs = "*" @@ -81,6 +85,8 @@ version = "0.8.1" default-features = false +[dependencies.kernel] +path = "../kernel" [dependencies.serde] version = "*" diff --git a/ableos/src/arch/x86_64/interrupts.rs b/ableos/src/arch/x86_64/interrupts.rs index 2f6fd551..f16208ae 100644 --- a/ableos/src/arch/x86_64/interrupts.rs +++ b/ableos/src/arch/x86_64/interrupts.rs @@ -67,7 +67,7 @@ extern "x86-interrupt" fn double_fault_handler( panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame); } extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFrame) { - crate::kmain::tick(); + kernel::tick(); unsafe { PICS.lock() .notify_end_of_interrupt(InterruptIndex::Timer.as_u8()); diff --git a/ableos/src/kmain.rs b/ableos/src/kmain.rs index 5e6ab8c2..4ce7390f 100644 --- a/ableos/src/kmain.rs +++ b/ableos/src/kmain.rs @@ -35,7 +35,6 @@ use { lazy_static! { // TODO: Change this structure to allow for multiple cores loaded - pub static ref TICK: AtomicU64 = AtomicU64::new(0); pub static ref BOOT_CONF: BootConfig = boot_conf::BootConfig::new(); } @@ -107,19 +106,6 @@ pub fn kernel_main() -> ! { sloop() } -/// called by arch specific timers to tick up all kernel related functions -pub fn tick() { - let mut data = TICK.load(Relaxed); - data = data.wrapping_add(1); - - crate::kernel_state::KERNEL_STATE.lock().update_state(); - // let mut scheduler = SCHEDULER.lock(); - // scheduler.bump_exec(); - // drop(scheduler); - - TICK.store(data, Relaxed) -} - 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/lib.rs b/ableos/src/lib.rs index c54c3486..283e4220 100644 --- a/ableos/src/lib.rs +++ b/ableos/src/lib.rs @@ -51,7 +51,6 @@ pub mod kernel_state; pub mod keyboard; pub mod kmain; pub mod logger; -pub mod panic; pub mod proto_filetable; pub mod relib; pub mod scheduler; @@ -74,7 +73,6 @@ pub use graphics::*; pub use kernel_state::*; pub use keyboard::*; pub use logger::*; -pub use panic::*; pub use proto_filetable::*; pub use relib::*; pub use scheduler::*; @@ -82,7 +80,6 @@ pub use utils::*; pub use vga_e::*; pub use wasm::*; -////////////////// pub mod virtio; pub use virtio::*; @@ -91,11 +88,14 @@ pub use alias_table::*; pub mod tests; pub use tests::*; -/*pub mod syscalls; -pub use syscalls::*; -*/ + pub mod scratchpad; pub use scratchpad::*; pub mod filesystem; -pub mod messaging; +/////////////// +/// Kernel /// +///////////// +pub use kernel; +pub use kernel::messaging; +pub use kernel::panic; diff --git a/ableos/src/logger.rs b/ableos/src/logger.rs index 5d31c241..49962ea0 100644 --- a/ableos/src/logger.rs +++ b/ableos/src/logger.rs @@ -1,7 +1,7 @@ use core::sync::atomic::Ordering; -use crate::kmain::TICK; use crate::serial_println; +use kernel::TICK; use lliw::{Fg, Reset}; pub use log::{debug, info, trace, warn}; use log::{Level, Metadata, Record}; diff --git a/ableos/src/messaging/NOTES.md b/ableos/src/messaging/NOTES.md deleted file mode 100644 index e69de29b..00000000 diff --git a/ableos/src/messaging/mod.rs b/ableos/src/messaging/mod.rs deleted file mode 100644 index ac47f229..00000000 --- a/ableos/src/messaging/mod.rs +++ /dev/null @@ -1,100 +0,0 @@ -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/panic.rs b/ableos/src/panic.rs deleted file mode 100644 index 9ae4d7c1..00000000 --- a/ableos/src/panic.rs +++ /dev/null @@ -1,22 +0,0 @@ -use {crate::arch::sloop, core::panic::PanicInfo}; - -/// A function to handle a panic in the kernel. -/// # Example -/// ``` -/// use ableos::panic::panic; -/// panic!("This is a panic!"); -/// ``` -/// -/// # Safety -/// This function is unsafe because it does not guarantee that the panic is handled. -#[panic_handler] -fn panic(info: &PanicInfo) -> ! { - error!("{}", info); - // help me use facepalm::start_facepalm; - - sloop() -} - -pub fn test_panic() { - panic!("test panic!"); -} diff --git a/ableos/src/rhai_shell/mod.rs b/ableos/src/rhai_shell/mod.rs index cb9fa0f6..8bdfc355 100644 --- a/ableos/src/rhai_shell/mod.rs +++ b/ableos/src/rhai_shell/mod.rs @@ -39,10 +39,10 @@ use x86_64::instructions::interrupts::{disable, enable}; use crate::wasm_jumploader::interp; use crate::{ arch::{shutdown, sloop}, - kmain::{tick, TICK}, systeminfo::{KERNEL_VERSION, RELEASE_TYPE}, KERNEL_STATE, }; +use kernel::TICK; pub fn afetch() { let kstate = KERNEL_STATE.lock(); diff --git a/ableos/src/tests.rs b/ableos/src/tests.rs index bbfca8da..043e7bad 100644 --- a/ableos/src/tests.rs +++ b/ableos/src/tests.rs @@ -10,10 +10,11 @@ use vga::colors::Color16; use y_compositor_protocol::Version; use crate::{ - kmain::{BOOT_CONF, TICK}, + kmain::BOOT_CONF, network::socket::{SimpleSock, Socket, SocketReturns}, num_to_vga16, vga_e, VgaBuffer, SCREEN_BUFFER, }; +use kernel::TICK; pub fn run_tests() { if BOOT_CONF.run_tests { diff --git a/ableos/src/wasm_jumploader/host_functions.rs b/ableos/src/wasm_jumploader/host_functions.rs index 83072b59..13a7d088 100644 --- a/ableos/src/wasm_jumploader/host_functions.rs +++ b/ableos/src/wasm_jumploader/host_functions.rs @@ -37,7 +37,7 @@ impl Externals for HostExternals { use core::sync::atomic::Ordering::*; x86_64::instructions::interrupts::disable(); - let tick_time = crate::kmain::TICK.load(Relaxed); + let tick_time = kernel::TICK.load(Relaxed); x86_64::instructions::interrupts::enable(); let ret = RuntimeValue::I64(tick_time.try_into().unwrap()); diff --git a/kernel/Cargo.lock b/kernel/Cargo.lock new file mode 100644 index 00000000..852bf6ce --- /dev/null +++ b/kernel/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "kernel" +version = "0.1.1" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml new file mode 100644 index 00000000..8f8ecbd7 --- /dev/null +++ b/kernel/Cargo.toml @@ -0,0 +1,8 @@ +[package] +edition = "2021" +name = "kernel" +version = "0.1.1" + + +[dependencies] +lazy_static = "*" \ No newline at end of file diff --git a/kernel/src/kernel_time.rs b/kernel/src/kernel_time.rs new file mode 100644 index 00000000..f4f1ed93 --- /dev/null +++ b/kernel/src/kernel_time.rs @@ -0,0 +1,10 @@ +//! Time + +/// An internal structure that is used to keep track of the time +pub struct Time { + /// The number of seconds since the kernel was started + pub seconds: u64, + + /// The number of nanoseconds since the kernel was started + pub nanoseconds: u32, +} diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs new file mode 100644 index 00000000..2a5d6934 --- /dev/null +++ b/kernel/src/lib.rs @@ -0,0 +1,25 @@ +#![deny(missing_docs)] +#![no_std] +//! The ableOS kernel. + +pub mod kernel_time; +pub mod messaging; +pub mod proccess; + +// #[cfg(no_std)] +pub mod panic; + +use core::sync::atomic::{AtomicU64, Ordering::Relaxed}; + +/// called by arch specific timers to tick up all kernel related functions +pub fn tick() { + let mut data = TICK.load(Relaxed); + data = data.wrapping_add(1); + + TICK.store(data, Relaxed) +} + +lazy_static::lazy_static! { + /// The number of ticks since the first CPU was started + pub static ref TICK: AtomicU64 = AtomicU64::new(0); +} diff --git a/kernel/src/messaging.rs b/kernel/src/messaging.rs new file mode 100644 index 00000000..4d3535b4 --- /dev/null +++ b/kernel/src/messaging.rs @@ -0,0 +1,65 @@ +//! Interprocess communication. + +use crate::{kernel_time::Time, proccess::PID}; + +/// 128 Bytes +pub type Tiny = [u8; 128]; + +/// 1 KibiByte +pub type Small = [u8; 1024]; + +/// 65.536 KibiBytes +pub type Medium = [u8; 65536]; + +/// 1MiB +pub type Large = [u8; 1048576]; + +/// 16MiB +pub type Huge = [u8; 16777216]; + +/// An internal message to be held in a process message +pub enum Message { + /// A Tiny message + /// + /// The message is 128 bytes long + Tiny(Tiny), + /// A Small message + /// + /// The message is 1 KiB long + Small(Small), + /// A Medium message + /// + /// The message is 65.536 KiB long + Medium(Medium), + /// A Large message + /// + /// The message is 1 MiB long + Large(Large), + /// A Huge message + /// + /// The message is 16 MiB long + Huge(Huge), +} + +/// A message that can be sent between processes +pub struct ProcessMessage { + /// The sender of the message + pub to_pid: PID, + /// The receiver of the message + pub from_pid: PID, + /// The message + pub message: Message, + /// The time the message was sent + pub sender_time: Time, + /// The time the message was received + pub receiver_time: Time, +} + +#[derive(Debug)] +/// An enum of all possible errors that can occur when sending a message +pub enum MessagingError { + /// The message is too large to be sent + MessageTooLarge, + /// The reciever of the message is not valid + ProcessNonExistant, +} diff --git a/kernel/src/panic.rs b/kernel/src/panic.rs new file mode 100644 index 00000000..e3761be6 --- /dev/null +++ b/kernel/src/panic.rs @@ -0,0 +1,8 @@ +//! + +use core::panic::PanicInfo; + +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + loop {} +} diff --git a/kernel/src/proccess.rs b/kernel/src/proccess.rs new file mode 100644 index 00000000..7077cbd3 --- /dev/null +++ b/kernel/src/proccess.rs @@ -0,0 +1,4 @@ +//! Platform agnostic process + +/// A process ID +pub type PID = u64;