From bdd866eafe735f9a55a778445f21d7fd73b17528 Mon Sep 17 00:00:00 2001 From: Erin Date: Fri, 18 Feb 2022 19:12:07 +0100 Subject: [PATCH 1/4] Working Rhai REPL --- ableos/src/arch/x86_64/interrupts.rs | 1 + ableos/src/scratchpad.rs | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/ableos/src/arch/x86_64/interrupts.rs b/ableos/src/arch/x86_64/interrupts.rs index 1429a9b8..1dca10a0 100644 --- a/ableos/src/arch/x86_64/interrupts.rs +++ b/ableos/src/arch/x86_64/interrupts.rs @@ -90,6 +90,7 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac // Backspace 8 => { WRITER.lock().backspace(); + KEYBUFF.lock().push(8_u8 as _); // print!(" "); // WRITER.lock().backspace(); } diff --git a/ableos/src/scratchpad.rs b/ableos/src/scratchpad.rs index c0e7a605..6d7f4b5e 100644 --- a/ableos/src/scratchpad.rs +++ b/ableos/src/scratchpad.rs @@ -36,7 +36,7 @@ pub fn scratchpad() { */ // interp(); - // rhai_shell(); + rhai_shell(); } pub struct PciIO {} @@ -94,16 +94,28 @@ impl ProcessMessage { */ pub fn rhai_shell() { - let mut engine = rhai::Engine::new(); + let engine = rhai::Engine::new(); + let mut buf = String::new(); + print!("> "); - let ret = engine.eval::("1 + 2"); + loop { + match x86_64::instructions::interrupts::without_interrupts(|| KEYBUFF.lock().pop()) { + Some('\n') => { + match engine.eval::(&buf) { + Ok(o) => println!("{o}"), + Err(e) => println!("Eval error: {e}"), + }; - match ret { - Ok(x) => println!("{}", x), - Err(e) => println!("{}", e), + buf.clear(); + print!("> "); + } + Some('\u{0008}') => { + buf.pop(); + } + Some(chr) => buf.push(chr), + None => (), + } } - - loop {} } lazy_static::lazy_static!( pub static ref KEYBUFF: spin::Mutex> = spin::Mutex::new( From 44c252ed84359573af0b60da8ec0b92bfea9157a Mon Sep 17 00:00:00 2001 From: Able Date: Sat, 19 Feb 2022 07:17:44 -0600 Subject: [PATCH 2/4] 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 ecfd00fa..8ed6d3cd 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 52057404..e4a99bf8 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 0fa1e4b3..5d31c241 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 6d7f4b5e..2fd3d807 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; +} From b7431ba47482ecb04fe25e72f6746b57213f505e Mon Sep 17 00:00:00 2001 From: Able Date: Sat, 19 Feb 2022 08:46:11 -0600 Subject: [PATCH 3/4] move rhai shell to its own sub folder --- ableos/src/lib.rs | 1 + ableos/src/print.rs | 6 --- ableos/src/rhai_shell/mod.rs | 76 +++++++++++++++++++++++++++++++++++ ableos/src/scratchpad.rs | 77 +----------------------------------- 4 files changed, 79 insertions(+), 81 deletions(-) create mode 100644 ableos/src/rhai_shell/mod.rs diff --git a/ableos/src/lib.rs b/ableos/src/lib.rs index 76429ea6..c78ef88b 100644 --- a/ableos/src/lib.rs +++ b/ableos/src/lib.rs @@ -33,6 +33,7 @@ pub mod arch; #[macro_use] pub mod print; pub mod devices; +pub mod rhai_shell; pub mod wasm_jumploader; #[macro_use] diff --git a/ableos/src/print.rs b/ableos/src/print.rs index 710f4f77..b1bb0f4c 100644 --- a/ableos/src/print.rs +++ b/ableos/src/print.rs @@ -48,12 +48,6 @@ macro_rules! print { } #[macro_export] macro_rules! println { - // TODO: The panic here should not be here - () =>{ - - // ::core::writeln!($crate::print::Stdout, "\n") - panic![]; - }; ($($tt:tt)*) => { ::core::writeln!($crate::print::Stdout, $($tt)*) // panic![]; diff --git a/ableos/src/rhai_shell/mod.rs b/ableos/src/rhai_shell/mod.rs new file mode 100644 index 00000000..b14bf64d --- /dev/null +++ b/ableos/src/rhai_shell/mod.rs @@ -0,0 +1,76 @@ +use alloc::vec::Vec; + +pub fn rhai_shell() { + 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_with_scope::(&mut scope, &buf) { + Ok(o) => println!("{o}"), + Err(e) => println!("Eval error: {e}"), + }; + + buf.clear(); + print!("> "); + } + Some('\u{0008}') => { + buf.pop(); + } + Some(chr) => buf.push(chr), + None => (), + } + } +} +lazy_static::lazy_static!( + pub static ref KEYBUFF: spin::Mutex> = spin::Mutex::new( + Vec::new()) + ; +); + +use alloc::string::{String, ToString}; +use x86_64::instructions::interrupts::{disable, enable}; + +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; +} diff --git a/ableos/src/scratchpad.rs b/ableos/src/scratchpad.rs index 2fd3d807..5a7bcff6 100644 --- a/ableos/src/scratchpad.rs +++ b/ableos/src/scratchpad.rs @@ -1,3 +1,5 @@ +use crate::rhai_shell::rhai_shell; + use { crate::{ devices::{pci_inner::DeviceClass, Device, DEVICE_TABLE}, @@ -92,78 +94,3 @@ impl ProcessMessage { } } */ - -pub fn rhai_shell() { - 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_with_scope::(&mut scope, &buf) { - Ok(o) => println!("{o}"), - Err(e) => println!("Eval error: {e}"), - }; - - buf.clear(); - print!("> "); - } - Some('\u{0008}') => { - buf.pop(); - } - Some(chr) => buf.push(chr), - None => (), - } - } -} -lazy_static::lazy_static!( - pub static ref KEYBUFF: spin::Mutex> = spin::Mutex::new( - Vec::new()) - ; -); - -use alloc::string::{String, ToString}; -use x86_64::instructions::interrupts::{disable, enable}; - -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; -} From c49eb97397c4d77eae4466378b85af7c721916e4 Mon Sep 17 00:00:00 2001 From: Able Date: Sat, 19 Feb 2022 09:07:33 -0600 Subject: [PATCH 4/4] patchup --- ableos/assets/key_event.txt | 17 ++++++++++++++ ableos/src/arch/x86_64/interrupts.rs | 3 ++- ableos/src/rhai_shell/mod.rs | 33 ++++++++++++++++------------ 3 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 ableos/assets/key_event.txt diff --git a/ableos/assets/key_event.txt b/ableos/assets/key_event.txt new file mode 100644 index 00000000..0617522d --- /dev/null +++ b/ableos/assets/key_event.txt @@ -0,0 +1,17 @@ +pub struct KeyEvent{ + lctrl 1 + rctrl 2 + lalt 3 + ralt 4 + lsup 5 + rsup 6 + lshift 7 + rshift 8 + caps 9 + + down 10 + # Keycodes + key 11-32 +} + + diff --git a/ableos/src/arch/x86_64/interrupts.rs b/ableos/src/arch/x86_64/interrupts.rs index 1dca10a0..1649093b 100644 --- a/ableos/src/arch/x86_64/interrupts.rs +++ b/ableos/src/arch/x86_64/interrupts.rs @@ -1,7 +1,8 @@ use crate::{ arch::{drivers::vga::WRITER, gdt}, kernel_state::KERNEL_STATE, - print, println, KEYBUFF, + print, println, + rhai_shell::KEYBUFF, }; use lazy_static::lazy_static; diff --git a/ableos/src/rhai_shell/mod.rs b/ableos/src/rhai_shell/mod.rs index b14bf64d..1fde8dca 100644 --- a/ableos/src/rhai_shell/mod.rs +++ b/ableos/src/rhai_shell/mod.rs @@ -1,20 +1,7 @@ use alloc::vec::Vec; pub fn rhai_shell() { - 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 engine = engine_construction(); let mut scope = rhai::Scope::new(); let mut buf = String::new(); @@ -46,6 +33,7 @@ lazy_static::lazy_static!( ); use alloc::string::{String, ToString}; +use rhai::Engine; use x86_64::instructions::interrupts::{disable, enable}; use crate::{ @@ -74,3 +62,20 @@ pub fn set_hostname(name: String) { let mut kstate = KERNEL_STATE.lock(); kstate.hostname = name; } + +fn engine_construction() -> Engine { + 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); + engine +}