From c18035feb4813d3193cced780ae1b9f9449d4da8 Mon Sep 17 00:00:00 2001 From: Able Date: Fri, 11 Mar 2022 17:13:41 -0600 Subject: [PATCH] Patch| Serial out agnostic --- ableos/Cargo.toml | 23 ------------ ableos/src/arch/riscv/drivers/uart.rs | 24 ------------ ableos/src/arch/riscv/mod.rs | 6 +-- ableos/src/arch/x86_64/drivers/serial.rs | 10 ++--- ableos/src/kmain.rs | 26 ------------- ableos/src/lib.rs | 4 ++ ableos/src/logger.rs | 9 ++--- ableos/src/prelude/rust_2021.rs | 2 + ableos/src/rhai_shell/mod.rs | 15 ++++---- ableos/src/scratchpad.rs | 4 +- ableos/src/serial_print.rs | 48 ++++++++++++++++++++++++ ableos/src/time.rs | 17 +++++++++ 12 files changed, 92 insertions(+), 96 deletions(-) create mode 100644 ableos/src/serial_print.rs create mode 100644 ableos/src/time.rs diff --git a/ableos/Cargo.toml b/ableos/Cargo.toml index 4fe49e5..0dabb52 100644 --- a/ableos/Cargo.toml +++ b/ableos/Cargo.toml @@ -10,8 +10,6 @@ panic = "abort" [package.metadata.bootimage] run-args = [ "--nodefaults", - - "-cpu", "Broadwell-v3", @@ -20,13 +18,9 @@ run-args = [ "-smp", "cores=2", - "-device", #"ati-vga", "cirrus-vga", - - - # "-device", # "virtio-blk-pci,drive=drive0,id=virtblk0,num-queues=4", # A simple example of a boot image @@ -48,27 +42,16 @@ test-args = [ ] [dependencies] - - - linked_list_allocator = "0.9.0" lliw = "0.2.0" -# qoi_rs = "*" spin = "0.5.2" -# vga = "*" log = "*" - - pretty-hex = "0.2.1" unicode-width = "0.1.7" picorand = "*" watson = "0.4" - genfs = "0.1.0" -# pc-beeper = "0.1.0" rhai = "1.5" - -# pci = {git="https://gitlab.com/robigalia/pci"} libwasm = {git="https://git.ablecorp.us:443/able/libwasm.git"} acpi = "4.1.0" @@ -77,12 +60,10 @@ version = "0.12.0" default-features = false features = ["export_derive"] - [dependencies.rdrand] version = "0.8.1" default-features = false - [dependencies.kernel] path = "../kernel" @@ -91,10 +72,6 @@ version = "*" default-features = false features = ["derive", "alloc"] - - - - [dependencies.hashbrown] version = "0.7.2" default-features = false diff --git a/ableos/src/arch/riscv/drivers/uart.rs b/ableos/src/arch/riscv/drivers/uart.rs index 33fb0ca..2ca6598 100644 --- a/ableos/src/arch/riscv/drivers/uart.rs +++ b/ableos/src/arch/riscv/drivers/uart.rs @@ -128,27 +128,3 @@ impl Write for Uart { Ok(()) } } - -// /////////////////////////////////// -// / RUST MACROS -// /////////////////////////////////// -#[macro_export] -macro_rules! sprint -{ - ($($args:tt)+) => ({ - use core::fmt::Write; - let _ = write!(crate::arch::drivers::uart::Uart::new(0x1000_0000), $($args)+); - }); -} -#[macro_export] -macro_rules! sprintln -{ - () => ({ - sprint!("\r\n") - }); - - ($fmt:expr) => (sprint!(concat!($fmt, "\r\n"))); - ($fmt:expr, $($args:tt)+) => ({ - sprint!(concat!($fmt, "\r\n"), $($args)+) - }); -} diff --git a/ableos/src/arch/riscv/mod.rs b/ableos/src/arch/riscv/mod.rs index 9ab9800..643563e 100644 --- a/ableos/src/arch/riscv/mod.rs +++ b/ableos/src/arch/riscv/mod.rs @@ -33,8 +33,8 @@ unsafe extern "C" fn _boot() -> ! { extern "C" fn _start() -> ! { let uart = crate::arch::drivers::uart::Uart::new(0x1000_0000); uart.init(); - - println!("Hello, world!"); + use crate::serial_println; + serial_println!("Hello, world!\r"); loop { if let Some(c) = uart.get() { @@ -52,7 +52,7 @@ extern "C" fn _start() -> ! { } } - println!("Serial connection closed."); + serial_println!("Serial connection closed.\r"); sloop() } diff --git a/ableos/src/arch/x86_64/drivers/serial.rs b/ableos/src/arch/x86_64/drivers/serial.rs index 85b29cc..e34f4f4 100644 --- a/ableos/src/arch/x86_64/drivers/serial.rs +++ b/ableos/src/arch/x86_64/drivers/serial.rs @@ -18,16 +18,16 @@ pub fn _print(args: ::core::fmt::Arguments) { } /// Prints to the host through the serial interface. #[macro_export] -macro_rules! serial_print { +macro_rules! sprint { ($($arg:tt)*) => { $crate::arch::drivers::serial::_print(format_args!($($arg)*)); }; } /// Prints to the host through the serial interface, appending a newline. #[macro_export] -macro_rules! serial_println { - () => ($crate::serial_print!("\n")); - ($fmt:expr) => ($crate::serial_print!(concat!($fmt, "\n"))); - ($fmt:expr, $($arg:tt)*) => ($crate::serial_print!( +macro_rules! sprintln { + () => ($crate::sprint!("\n")); + ($fmt:expr) => ($crate::sprint!(concat!($fmt, "\n"))); + ($fmt:expr, $($arg:tt)*) => ($crate::sprint!( concat!($fmt, "\n"), $($arg)*)); } diff --git a/ableos/src/kmain.rs b/ableos/src/kmain.rs index c88d733..7260574 100644 --- a/ableos/src/kmain.rs +++ b/ableos/src/kmain.rs @@ -11,7 +11,6 @@ use { relib::network::socket::{SimpleSock, Socket}, scratchpad, }, - facepalm::start_facepalm, lazy_static::lazy_static, }; @@ -73,36 +72,11 @@ pub fn generate_process_pass() -> u128 { // ret 123 } -/* - -// TODO: move to a better place -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct AcpiStruct {} - -impl acpi::AcpiHandler for AcpiStruct { - unsafe fn map_physical_region( - &self, - physical_address: usize, - size: usize, - ) -> acpi::PhysicalMapping { - info!("PHYS ADDR: {:?}", physical_address); - info!("Size: {:?}", size); - - todo!("map_physical_region"); - } - - fn unmap_physical_region(_region: &acpi::PhysicalMapping) { - todo!("unmap_physical_region"); - } -} -*/ use kernel::KERNEL_VERSION; use crate::{ boot_conf::KernelConfig, - kprint, scheduler::{capabilities::FileAccess, SCHEDULER}, - serial_print, serial_println, systeminfo::RELEASE_TYPE, }; diff --git a/ableos/src/lib.rs b/ableos/src/lib.rs index a7914d9..4a30abe 100644 --- a/ableos/src/lib.rs +++ b/ableos/src/lib.rs @@ -40,6 +40,10 @@ pub mod wasm_jumploader; #[cfg(target_arch = "x86_64")] pub mod port_io; +#[macro_use] +pub mod serial_print; +pub mod time; + #[macro_use] pub extern crate log; diff --git a/ableos/src/logger.rs b/ableos/src/logger.rs index 0341ebd..dfe539e 100644 --- a/ableos/src/logger.rs +++ b/ableos/src/logger.rs @@ -1,7 +1,7 @@ use core::sync::atomic::Ordering; use crate::network::socket::{SimpleSock, Socket}; -use crate::{kprint, serial_println}; +use crate::time::fetch_time; use kernel::TICK; use lliw::{Fg, Reset}; @@ -19,10 +19,8 @@ impl log::Log for SimpleLogger { fn log(&self, record: &Record) { if self.enabled(record.metadata()) { let color; - disable(); - let time = TICK.load(Ordering::Relaxed) as f64; - enable(); - let time_float = time; + + let time_float = fetch_time(); match record.level() { log::Level::Error => color = (Fg::Red, "$RED$"), @@ -66,7 +64,6 @@ impl log::Log for SimpleLogger { } use log::{LevelFilter, SetLoggerError}; -use x86_64::instructions::interrupts::{disable, enable}; static LOGGER: SimpleLogger = SimpleLogger; diff --git a/ableos/src/prelude/rust_2021.rs b/ableos/src/prelude/rust_2021.rs index dc001f5..502037f 100644 --- a/ableos/src/prelude/rust_2021.rs +++ b/ableos/src/prelude/rust_2021.rs @@ -3,6 +3,8 @@ pub use core::prelude::rust_2021::*; pub use core::prelude::v1::*; pub use crate::print::*; +pub use crate::serial_print::*; + pub use log::{debug, info, trace, warn}; pub use alloc::{boxed::Box, format, string::*, vec, vec::*}; diff --git a/ableos/src/rhai_shell/mod.rs b/ableos/src/rhai_shell/mod.rs index cf7773f..8c26922 100644 --- a/ableos/src/rhai_shell/mod.rs +++ b/ableos/src/rhai_shell/mod.rs @@ -1,4 +1,8 @@ -pub fn rhai_shell() { +#[cfg(target_arch = "riscv64")] +pub fn shell() {} + +#[cfg(target_arch = "x86_64")] +pub fn shell() { let engine = engine_construction(); let mut scope = rhai::Scope::new(); @@ -31,22 +35,19 @@ lazy_static::lazy_static!( ); use rhai::Engine; -use x86_64::instructions::interrupts::{disable, enable}; +use crate::time::fetch_time; use crate::wasm_jumploader::interp; use crate::{ arch::{shutdown, sloop}, systeminfo::{KERNEL_VERSION, RELEASE_TYPE}, KERNEL_STATE, }; -use kernel::TICK; pub fn afetch() { let kstate = KERNEL_STATE.lock(); - use core::sync::atomic::Ordering::*; - disable(); - let tick_time = TICK.load(Relaxed); + let tick_time = fetch_time(); println!( "OS: AbleOS @@ -55,7 +56,7 @@ Kernel: AKern-{}-v{} Uptime: {}", kstate.hostname, RELEASE_TYPE, KERNEL_VERSION, tick_time ); - enable(); + drop(kstate); } pub fn set_hostname(name: String) { diff --git a/ableos/src/scratchpad.rs b/ableos/src/scratchpad.rs index 2782206..1f4e96e 100644 --- a/ableos/src/scratchpad.rs +++ b/ableos/src/scratchpad.rs @@ -1,12 +1,12 @@ use acpi::AcpiTables; -use crate::{kprintln, rhai_shell::rhai_shell, serial_println}; +use crate::rhai_shell::shell; /// Experimental scratchpad for testing. pub fn scratchpad() { // pci_fun(); - rhai_shell(); + shell(); } pub fn pci_fun() {} diff --git a/ableos/src/serial_print.rs b/ableos/src/serial_print.rs new file mode 100644 index 0000000..84d880d --- /dev/null +++ b/ableos/src/serial_print.rs @@ -0,0 +1,48 @@ +pub struct Serialout; +use core::fmt::{Arguments, Error}; +impl Serialout { + pub fn write_fmt(&mut self, arg: Arguments<'_>) /*-> Result<(), Error> */ + { + let _ = core::fmt::Write::write_fmt(self, arg); + // Ok(()) + } +} +impl core::fmt::Write for Serialout { + #[cfg(target_arch = "aarch64")] + fn write_str(&mut self, s: &str) -> Result<(), Error> { + // Don't actually print anything yet lmao + Ok(()) + } + #[cfg(target_arch = "x86_64")] + fn write_str(&mut self, s: &str) -> Result<(), Error> { + crate::arch::drivers::serial::_print(format_args!("{}", s)); + + Ok(()) + } + #[cfg(target_arch = "riscv64")] + fn write_str(&mut self, s: &str) -> Result<(), Error> { + write!(crate::arch::drivers::uart::Uart::new(0x1000_0000), "{}", s) + } + fn write_char(&mut self, c: char) -> core::fmt::Result { + self.write_str(c.encode_utf8(&mut [0; 4])) + } + fn write_fmt(mut self: &mut Self, args: Arguments<'_>) -> core::fmt::Result { + core::fmt::write(&mut self, args) + } +} +#[macro_export] +macro_rules! serial_print { + () => { + ::core::write!($crate::serial_print::Serialout, "") + }; + ($($tt:tt)*) => { + ::core::write!($crate::serial_print::Serialout, $($tt)*) + }; +} +#[macro_export] +macro_rules! serial_println { + ($($tt:tt)*) => { + ::core::writeln!($crate::serial_print::Serialout, $($tt)*) + // panic![]; + }; +} diff --git a/ableos/src/time.rs b/ableos/src/time.rs new file mode 100644 index 0000000..e0cf60f --- /dev/null +++ b/ableos/src/time.rs @@ -0,0 +1,17 @@ +#[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(); + time +} + +use core::sync::atomic::Ordering; +use kernel::TICK; +#[cfg(target_arch = "riscv64")] +pub fn fetch_time() -> f64 { + let time = TICK.load(Ordering::Relaxed) as f64; + time +}