simplification

This commit is contained in:
Able 2022-04-09 17:26:43 -05:00
parent fe342406b4
commit 72457cc34e
18 changed files with 264 additions and 205 deletions

2
ableos/Cargo.lock generated
View file

@ -335,7 +335,7 @@ checksum = "33a33a362ce288760ec6a508b94caaec573ae7d3bbbd91b87aa0bad4456839db"
[[package]]
name = "libwasm"
version = "0.1.0"
source = "git+https://git.ablecorp.us/able/libwasm.git#dedbb769ba01a4b75992437e52ca9a5c2bb9e0f9"
source = "git+https://git.ablecorp.us/able/libwasm.git#aa1f7d5c0985649b6d73249dcad908272e82d7eb"
[[package]]
name = "linked_list_allocator"

View file

@ -60,6 +60,8 @@ acpi = "4.1.0"
axel = { git = "https://git.ablecorp.us:443/able/axel.git" }
[dependencies.logos]
version = "0.12.0"
default-features = false

View file

@ -16,7 +16,6 @@ impl log::Log for SimpleLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Trace
}
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
let color;
@ -40,6 +39,8 @@ impl log::Log for SimpleLogger {
// kprint!("{}", msg);
// NOTE: This needs to be fixed before merge
if KERNEL_CONF.logging.log_to_serial {
// #[track_caller]
serial_println!(
"[{}{}{}][{}{}{}] {}",
color.0,

View file

@ -3,6 +3,7 @@ pub fn shell() {}
#[cfg(target_arch = "x86_64")]
pub fn shell() {
let mut current_dir = "/".to_string();
let engine = engine_construction();
let mut scope = rhai::Scope::new();
@ -14,6 +15,7 @@ pub fn shell() {
Some('\n') => {
match engine.eval_with_scope::<rhai::Dynamic>(&mut scope, &buf) {
Ok(o) => println!("{o}"),
Err(e) => println!("Eval error: {e}"),
};
@ -37,9 +39,8 @@ pub fn shell() {
}
}
lazy_static::lazy_static!(
pub static ref KEYBUFF: spin::Mutex<Vec<char>> = spin::Mutex::new(
Vec::new())
;
pub static ref KEYBUFF: spin::Mutex<Vec<char>> = spin::Mutex::new(Vec::new());
pub static ref CURRENT_DIR: spin::Mutex<String> = spin::Mutex::new("/".to_string());
);
use rhai::Engine;
@ -96,6 +97,9 @@ fn engine_construction() -> Engine {
debug!("{} at {:?}: {}", src, pos, x);
});
engine.register_fn("ls", ls);
engine.register_fn("cat", echo_file);
engine.register_fn("cd", change_directory);
engine.register_fn("afetch", afetch);
engine.register_fn("set_hostname", set_hostname);
engine.register_fn("shutdown", shutdown);
@ -110,16 +114,34 @@ fn engine_construction() -> Engine {
/// Examine a memory pointer
pub fn peek_memory(ptr: i64) -> u8 {
let ptr: usize = ptr as usize;
let ptr: usize = ptr.abs() as usize;
println!(">:(");
unsafe { *(ptr as *const u8) }
}
pub fn poke_memory(ptr: i64, val: u8) {
let ptr: usize = ptr as usize;
let ptr: usize = ptr.abs() as usize;
unsafe { *(ptr as *mut u8) = val }
}
pub fn ls() {
let mut current_dir = CURRENT_DIR.lock();
let fs = &*FILE_SYSTEM.lock();
let file = fs
.open(current_dir.as_bytes(), OpenOptions::new().read(true))
.unwrap();
let mut files = file.directory().unwrap();
println!("current dir: {}", *current_dir);
while let Some(Ok(entry)) = files.next() {
let inode_name = entry.name;
let s = String::from_utf8_lossy(&inode_name);
println!("{}", s);
}
}
pub fn log_dump() {
use crate::network::socket::SimpleSock;
use crate::relib::network::socket::Socket;
@ -142,3 +164,58 @@ pub fn log_dump() {
None => warn!("No socket found for Logger"),
}
}
use crate::filesystem::FILE_SYSTEM;
use genfs::{DirEntry, Fs, OpenOptions};
pub fn echo_file(path: String) {
let mut current_dir = CURRENT_DIR.lock();
let fs = &*FILE_SYSTEM.lock();
current_dir.push_str(&path);
let file = fs
.open(current_dir.as_bytes(), OpenOptions::new().read(true))
.unwrap();
if file.is_dir() {
println!("{} is a directory", path);
return;
} else {
let mut file_contents = Vec::new();
let ret = file.read_to_end(&mut file_contents).unwrap();
let file_contents_str = String::from_utf8_lossy(&file_contents);
println!("{}", file_contents_str);
}
}
pub fn change_directory(path: String) {
let mut current_dir = CURRENT_DIR.lock();
let fs = &*FILE_SYSTEM.lock();
if path == "." || path == ".." {
let mut split_dir = current_dir.split("/").collect::<Vec<&str>>();
let mut new_dir = String::new();
split_dir.remove(split_dir.len() - 1);
println!("{:?}", split_dir);
if split_dir.len() == 0 {
new_dir = "/".to_string();
} else {
for x in split_dir {
new_dir.push_str(x);
new_dir.push('/');
}
}
*current_dir = new_dir;
} else {
if !current_dir.ends_with('/') {
current_dir.push_str("/");
}
current_dir.push_str(&path);
}
}

View file

@ -10,15 +10,13 @@ use crate::devices::Device::Vterm;
/// Experimental scratchpad for testing.
pub fn scratchpad() {
let axel_raw = "
kernel{
let axel_raw = "kernel{
vals=
time: 123
fn|
print: (None) -> (None);
foo: (None) -> (Num);
}
";
}";
let axel = axel::parse(axel_raw.to_string());
for node in axel {
info!("{:?}", node);

View file

@ -1,3 +1,5 @@
use core::arch;
use wasmi::{
Error, Externals, FuncInstance, FuncRef, ModuleImportResolver, RuntimeArgs, RuntimeValue,
Signature, Trap, ValueType,
@ -8,6 +10,7 @@ pub struct HostExternals {}
const ADD_FUNC_INDEX: usize = 0;
const SEND_SIGNAL_INDEX: usize = 1;
const GET_TIME_INDEX: usize = 2;
const GET_RANDOM_INDEX: usize = 3;
impl Externals for HostExternals {
fn invoke_index(
@ -34,28 +37,98 @@ impl Externals for HostExternals {
GET_TIME_INDEX => {
use core::sync::atomic::Ordering::*;
// x86_64::instructions::interrupts::disable();
println!("SYSCALL: get time");
x86_64::instructions::interrupts::disable();
let tick_time = kernel::TICK.load(Relaxed);
// x86_64::instructions::interrupts::enable();
x86_64::instructions::interrupts::enable();
let ret = RuntimeValue::I64(tick_time.try_into().unwrap());
let ret = RuntimeValue::I32(tick_time.try_into().unwrap());
Ok(Some(ret))
}
GET_RANDOM_INDEX => {
println!("SYSCALL: get random");
let rand = generate_process_pass();
let ret = RuntimeValue::I32(rand as i32);
// let ret = RuntimeValue::I32(rand.try_into().unwrap());
Ok(Some(ret))
}
_ => panic!("Unimplemented function at {}", index),
}
}
}
use crate::arch::generate_process_pass;
impl HostExternals {
fn check_signature(&self, index: usize, signature: &Signature) -> bool {
let (params, ret_ty): (&[ValueType], Option<ValueType>) = match index {
ADD_FUNC_INDEX => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)),
SEND_SIGNAL_INDEX => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)),
GET_TIME_INDEX => (&[], Some(ValueType::I32)),
_ => return false,
};
signature.params() == params && signature.return_type() == ret_ty
match index {
ADD_FUNC_INDEX => {
let (params, ret_ty): (&[ValueType], Option<ValueType>) =
(&[ValueType::I32, ValueType::I32], Some(ValueType::I32));
if params.len() != signature.params().len() {
return false;
}
if ret_ty != signature.return_type() {
return false;
}
for (ty, param) in params.iter().zip(signature.params()) {
if *ty != *param {
return false;
}
}
return true;
}
SEND_SIGNAL_INDEX => {
let (params, ret_ty): (&[ValueType], Option<ValueType>) =
(&[ValueType::I32, ValueType::I32], Some(ValueType::I32));
if params.len() != signature.params().len() {
return false;
}
if ret_ty != signature.return_type() {
return false;
}
for (ty, param) in params.iter().zip(signature.params()) {
if *ty != *param {
return false;
}
}
return true;
}
GET_TIME_INDEX => {
let (params, ret_ty): (&[ValueType], Option<ValueType>) =
(&[], Some(ValueType::I32));
if params.len() != signature.params().len() {
return false;
}
if ret_ty != signature.return_type() {
return false;
}
for (ty, param) in params.iter().zip(signature.params()) {
if *ty != *param {
return false;
}
}
return true;
}
GET_RANDOM_INDEX => {
let (params, ret_ty): (&[ValueType], Option<ValueType>) =
(&[], Some(ValueType::I32));
if params.len() != signature.params().len() {
return false;
}
if ret_ty != signature.return_type() {
return false;
}
for (ty, param) in params.iter().zip(signature.params()) {
if *ty != *param {
return false;
}
}
return true;
}
_ => false,
}
}
}
@ -64,6 +137,8 @@ impl ModuleImportResolver for HostExternals {
let index = match field_name {
"add" => ADD_FUNC_INDEX,
"send_signal" => SEND_SIGNAL_INDEX,
"get_time" => GET_TIME_INDEX,
"get_random" => GET_RANDOM_INDEX,
_ => {
return Err(Error::Instantiation(format!(
"Export {} not found",
@ -74,14 +149,18 @@ impl ModuleImportResolver for HostExternals {
if !self.check_signature(index, signature) {
return Err(Error::Instantiation(format!(
"Export {} has a bad signature",
field_name
"Export {} has a bad signature {:?}",
field_name, signature
)));
}
trace!("Resolved export {} as func {}", field_name, index);
Ok(FuncInstance::alloc_host(
Ok(FuncInstance::alloc_host(signature.clone(), index))
/*Ok(FuncInstance::alloc_host(
Signature::new(&[ValueType::I32, ValueType::I32][..], Some(ValueType::I32)),
index,
))
))*/
}
}
use crate::wasm_jumploader::host_functions::ValueType::I32;

View file

@ -25,18 +25,76 @@ pub fn interp() {
trace!("Binary size {}", ret);
// Load wasm binary and prepare it for instantiation.
let module = wasmi::Module::from_buffer(&wasm_binary).expect("failed to load wasm");
trace!("Loaded wasm binary");
let imports = ImportsBuilder::new().with_resolver("env", &host_functions::HostExternals {});
trace!("Created imports");
// Instantiate a module with empty imports and
// assert that there is no `start` function.
let instance = ModuleInstance::new(&module, &imports)
.expect("failed to instantiate wasm module")
.assert_no_start();
let instance = ModuleInstance::new(&module, &imports); // .expect("failed to instantiate wasm module")
// .assert_no_start();
match instance {
Ok(inst) => {
let mut instance = inst.assert_no_start();
// trace!("Instantiated wasm module");
let mut is_driver = false;
let mut is_program = false;
let mut has_driver_entry = false;
let mut has_driver_exit = false;
let mut has_start = false;
if let Some(val) = instance.export_by_name("driver_entry") {
has_driver_entry = true;
}
if let Some(val) = instance.export_by_name("driver_exit") {
has_driver_exit = true;
}
// trace!("Checking for start function");
match instance.export_by_name("start") {
Some(val) => {
trace!("Program start function found");
has_start = true;
}
None => debug!("No start function found"),
}
match (has_driver_entry, has_driver_exit) {
(true, true) => {
trace!("Valid driver entry and exit functions found");
is_driver = true;
}
(true, false) => error!("Driver entry function found but no driver exit function"),
(false, true) => error!("Driver exit function found but no driver entry function"),
(false, false) => {
trace!("No driver entry or exit functions found");
}
}
if has_start && has_driver_entry {
info!("Dual Program and Driver");
}
if has_start {
let ret = instance
.invoke_export("start", &[], &mut HostExternals {})
.expect("failed to execute export");
println!("collected wasm return value: {:?}", ret);
} else if is_driver {
let ret = instance
.invoke_export("driver_entry", &[], &mut HostExternals {})
.expect("failed to execute export");
println!("collected wasm return value: {:?}", ret);
}
}
Err(err) => error!("{}", err),
}
}
pub fn run_program() {}

17
shadeable/Cargo.lock generated
View file

@ -20,6 +20,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cfg-if"
version = "1.0.0"
@ -158,11 +164,12 @@ dependencies = [
[[package]]
name = "rhai"
version = "1.4.0"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c7433068977c56619bf2b7831da26eb986d0645fe56f2ad9357eda7ae4c435e"
checksum = "c0a10b3f41db25733e2e953811858dd90c1e96cd618606ddf6961d34b3910b18"
dependencies = [
"ahash",
"bitflags",
"core-error",
"instant",
"libm",
@ -201,11 +208,13 @@ checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83"
[[package]]
name = "smartstring"
version = "0.2.9"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31aa6a31c0c2b21327ce875f7e8952322acfcfd0c27569a6e18a647281352c9b"
checksum = "3fb72c633efbaa2dd666986505016c32c3044395ceaf881518399d2f4127ee29"
dependencies = [
"autocfg",
"static_assertions",
"version_check",
]
[[package]]

View file

@ -12,7 +12,7 @@ libm = "*"
log ="*"
[dependencies.rhai]
version = "*"
version = "1.5"
features = [ "no_std", "only_i64"]
default-features = false

View file

@ -1,6 +0,0 @@
[build]
target = "wasm32-unknown-unknown"
# [unstable]
# build-std = ["core", "compiler_builtins", "alloc"]
# build-std-features = ["compiler-builtins-mem"]

View file

@ -1,15 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aos_wasm_stress_test"
version = "0.1.0"
dependencies = [
"libwasm",
]
[[package]]
name = "libwasm"
version = "0.1.0"
source = "git+https://git.ablecorp.us/able/libwasm.git#dedbb769ba01a4b75992437e52ca9a5c2bb9e0f9"

View file

@ -1,9 +0,0 @@
[package]
name = "aos_wasm_stress_test"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
libwasm = {git="https://git.ablecorp.us:443/able/libwasm.git"}

View file

@ -1,4 +0,0 @@
This is a stress test and simple program for ableOS.

View file

@ -1,36 +0,0 @@
#![no_std]
#![no_main]
#[no_mangle]
fn start() -> i32 {
let ret = unsafe { add(1, 2) };
unsafe {
send_signal(PID(1), Signals::Quit);
}
ret as i32
}
extern "C" {
/// Send a signal to a process
///
/// # Arguments
///
/// * `pid` - The PID of the process to send the signal to
/// * `signal` - The signal to send
pub fn send_signal(pid: PID, signal: Signals) -> bool;
}
use {
libwasm::process::{signals::Signals, PID},
libwasm::syscalls::add,
};
use libwasm::*;
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
use core::panic::PanicInfo;

Binary file not shown.

View file

@ -1,75 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "proc-macro2"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145"
dependencies = [
"proc-macro2",
]
[[package]]
name = "serde"
version = "1.0.136"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.136"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "toml"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
dependencies = [
"serde",
]
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "wasm_pk_data"
version = "0.1.0"
dependencies = [
"serde",
"toml",
]

View file

@ -1,17 +0,0 @@
[package]
name = "wasm_pk_data"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
toml = "0.5"
[dependencies.serde]
version = "1.0"
features = ["derive"]

View file

@ -1,3 +0,0 @@
name = "bruh"
version = [1233, 123, 123]
authors = ["John Doe", "Jane Doe"]