From 44c252ed84359573af0b60da8ec0b92bfea9157a Mon Sep 17 00:00:00 2001 From: Able Date: Sat, 19 Feb 2022 07:17:44 -0600 Subject: [PATCH] persistant scope --- ableos/Cargo.toml | 6 ----- ableos/src/kernel_state.rs | 2 +- ableos/src/logger.rs | 2 +- ableos/src/scratchpad.rs | 49 ++++++++++++++++++++++++++++++++++---- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/ableos/Cargo.toml b/ableos/Cargo.toml index ecfd00f..8ed6d3c 100644 --- a/ableos/Cargo.toml +++ b/ableos/Cargo.toml @@ -12,12 +12,6 @@ run-args = [ "-cpu", "Broadwell-v3", - - - - - - "-serial", "stdio", "-smp", diff --git a/ableos/src/kernel_state.rs b/ableos/src/kernel_state.rs index 5205740..e4a99bf 100644 --- a/ableos/src/kernel_state.rs +++ b/ableos/src/kernel_state.rs @@ -7,7 +7,7 @@ lazy_static! { } pub struct KernelInternalState { - hostname: String, + pub hostname: String, should_shutdown: bool, } diff --git a/ableos/src/logger.rs b/ableos/src/logger.rs index 0fa1e4b..5d31c24 100644 --- a/ableos/src/logger.rs +++ b/ableos/src/logger.rs @@ -22,7 +22,7 @@ impl log::Log for SimpleLogger { let color; let time = TICK.load(Ordering::Relaxed) as f64; - let time_float = time / TIMER_INTERRUPT_HERTZ; + let time_float = time; match record.level() { log::Level::Error => color = (Fg::Red, "$RED$"), diff --git a/ableos/src/scratchpad.rs b/ableos/src/scratchpad.rs index 6d7f4b5..2fd3d80 100644 --- a/ableos/src/scratchpad.rs +++ b/ableos/src/scratchpad.rs @@ -94,14 +94,29 @@ impl ProcessMessage { */ pub fn rhai_shell() { - let engine = rhai::Engine::new(); + let mut engine = rhai::Engine::new(); + + engine.on_print(|x| println!("{}", x)); + + engine.on_debug(|x, src, pos| { + let src = src.unwrap_or("unknown"); + println!("DEBUG: {} at {:?}: {}", src, pos, x); + debug!("{} at {:?}: {}", src, pos, x); + }); + + engine.register_fn("afetch", afetch); + engine.register_fn("set_hostname", set_hostname); + engine.register_fn("shutdown", shutdown); + + let mut scope = rhai::Scope::new(); + let mut buf = String::new(); print!("> "); loop { match x86_64::instructions::interrupts::without_interrupts(|| KEYBUFF.lock().pop()) { Some('\n') => { - match engine.eval::(&buf) { + match engine.eval_with_scope::(&mut scope, &buf) { Ok(o) => println!("{o}"), Err(e) => println!("Eval error: {e}"), }; @@ -123,6 +138,32 @@ lazy_static::lazy_static!( ; ); -use alloc::string::String; +use alloc::string::{String, ToString}; +use x86_64::instructions::interrupts::{disable, enable}; -use crate::arch::sloop; +use crate::{ + arch::{shutdown, sloop}, + kmain::{tick, TICK}, + systeminfo::{KERNEL_VERSION, RELEASE_TYPE}, + KERNEL_STATE, +}; + +pub fn afetch() { + let kstate = KERNEL_STATE.lock(); + use core::sync::atomic::Ordering::*; + + 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); + + drop(kstate); +} +pub fn set_hostname(name: String) { + let mut kstate = KERNEL_STATE.lock(); + kstate.hostname = name; +}