1
0
Fork 0
forked from koniifer/ableos
ableos-framebuffer/ableos/src/scratchpad.rs

170 lines
3.9 KiB
Rust
Raw Normal View History

2022-02-12 03:25:02 -06:00
use {
crate::{
devices::{pci_inner::DeviceClass, Device, DEVICE_TABLE},
proc::PID,
wasm_jumploader::interp,
},
alloc::{format, vec::Vec},
pci::PortOps,
};
2022-02-08 03:01:29 -06:00
/// Experimental scratchpad for testing.
pub fn scratchpad() {
2022-02-12 03:25:02 -06:00
let mut dev_list = Vec::new();
let bus_scan;
unsafe {
bus_scan = pci::scan_bus(&PciIO {}, pci::CSpaceAccessMethod::IO);
}
for dev in bus_scan {
dev_list.push(dev);
}
2022-02-08 03:01:29 -06:00
2022-02-12 03:25:02 -06:00
let device_table = &mut *DEVICE_TABLE.lock();
2022-02-08 03:01:29 -06:00
2022-02-12 03:25:02 -06:00
for x in dev_list {
let device_name = format!("{:?}-{}", DeviceClass::from_u8(x.id.class), x.id.device_id);
2022-02-08 04:13:53 -06:00
2022-02-18 02:24:10 -06:00
device_table
.devices
.insert(device_name.clone(), Device::Pci(x));
}
2022-02-08 04:13:53 -06:00
2022-02-18 02:24:10 -06:00
/*
for (key, _value) in device_table.devices.iter() {
debug!("{}", key);
}
*/
2022-02-17 04:05:56 -06:00
// interp();
2022-02-18 12:12:07 -06:00
rhai_shell();
2022-02-08 04:13:53 -06:00
}
2022-02-12 03:25:02 -06:00
pub struct PciIO {}
impl PortOps for PciIO {
unsafe fn read8(&self, port: u16) -> u8 {
cpuio::inb(port as u16)
}
unsafe fn read16(&self, port: u16) -> u16 {
cpuio::inw(port as u16)
}
unsafe fn read32(&self, port: u16) -> u32 {
cpuio::inl(port as u16)
}
unsafe fn write8(&self, port: u16, val: u8) {
cpuio::outb(val, port as u16);
}
unsafe fn write16(&self, port: u16, val: u16) {
cpuio::outw(val, port as u16);
}
unsafe fn write32(&self, port: u16, val: u32) {
cpuio::outl(val, port as u16);
}
}
2022-02-17 04:05:56 -06:00
/*
2022-02-12 03:25:02 -06:00
/// An experimental process message format
pub struct ProcessMessage {
pub to_pid: PID,
pub from_pid: PID,
pub message: [u8; 2048],
pub sender_time: SecondsTime,
}
//
2022-02-17 04:05:56 -06:00
// use libwasm::syscalls::time_calls::SecondsTime;
2022-02-12 03:25:02 -06:00
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,
},
}
}
}
2022-02-17 04:05:56 -06:00
*/
pub fn rhai_shell() {
2022-02-19 07:17:44 -06:00
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();
2022-02-18 12:12:07 -06:00
let mut buf = String::new();
print!("> ");
loop {
match x86_64::instructions::interrupts::without_interrupts(|| KEYBUFF.lock().pop()) {
Some('\n') => {
2022-02-19 07:17:44 -06:00
match engine.eval_with_scope::<rhai::Dynamic>(&mut scope, &buf) {
2022-02-18 12:12:07 -06:00
Ok(o) => println!("{o}"),
Err(e) => println!("Eval error: {e}"),
};
buf.clear();
print!("> ");
}
Some('\u{0008}') => {
buf.pop();
}
Some(chr) => buf.push(chr),
None => (),
}
2022-02-17 04:05:56 -06:00
}
}
lazy_static::lazy_static!(
pub static ref KEYBUFF: spin::Mutex<Vec<char>> = spin::Mutex::new(
Vec::new())
;
);
2022-02-19 07:17:44 -06:00
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();
2022-02-17 04:05:56 -06:00
2022-02-19 07:17:44 -06:00
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;
}