fix up wasm-sys and implement experimental futex

This commit is contained in:
Able 2022-01-05 07:10:52 -06:00
parent e43891ce9f
commit 6d31f4db29
7 changed files with 173 additions and 95 deletions

2
ableos/Cargo.lock generated
View file

@ -59,7 +59,7 @@ checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650"
[[package]] [[package]]
name = "externc-libm" name = "externc-libm"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/HaruxOS/externc-libm#ad2865a706fd46d829550ed4cdfa4126f2c81e19" source = "git+ssh://root@git.ablecorp.us:20/able/externc-libm.git#0781df85c094bcd7e5ef7505e9c5cc6317eeda75"
dependencies = [ dependencies = [
"libm", "libm",
] ]

View file

@ -41,7 +41,7 @@ features = ["spin_no_std"]
version = "1.0" version = "1.0"
[dependencies.externc-libm] [dependencies.externc-libm]
git = "https://github.com/HaruxOS/externc-libm" git = "ssh://root@git.ablecorp.us:20/able/externc-libm.git"
[target.'cfg(target_arch = "x86_64")'.dependencies] [target.'cfg(target_arch = "x86_64")'.dependencies]
bootloader = { version = "0.9.8", features = ["map_physical_memory"] } bootloader = { version = "0.9.8", features = ["map_physical_memory"] }

0
ableos/notes/mouse.md Normal file
View file

View file

@ -1,6 +1,15 @@
pub enum PS2MouseButton {
LeftMB,
RightMB, // TODO: Bitmasking
pub enum Mouse {
Button1,
Button2,
Button3,
Button4,
Button5,
X(i8),
Y(i8),
Wheel(i8),
} }
pub trait PS2Mouse { pub trait PS2Mouse {

View file

@ -0,0 +1,66 @@
use core::time::Duration;
use alloc::vec::Vec;
use crate::scheduler::ThreadID;
// pub struct Duration {}
pub struct AtomicU32(u32);
impl AtomicU32 {
//if v != current value
pub fn wait(&self, _v: u32) {
todo!();
}
pub fn wait_timeout(&self, _v: u32, _timeout: Duration) -> bool {
todo!();
}
pub fn wake_single(&self) {
todo!();
}
pub fn wake_all(&self) {
todo!();
}
}
/*
SUPER HANDWAVEY
YOU WILL NEED LOCKING THAT I DIDNT WRITE OUT (you == zuurr#9735)
// all the red is by design
pub fn futex_wait(atom: &AtomicU32, value: usize, current_thread: ThreadID) {
let address = atomic as *const _ as usize;
let waiters = waiters_for(address); // Hold lock
waiters.add(current_thread);
if self.load() == value {
current_thread.sleep();
} else {
waiters.remove(current_thread);
}
}
pub fn futex_wake(atom: &AtomicU32, threads_to_wake: usize) {
let address = atomic as *const _ as usize;
let waiters = waiters_for(address);
for waiting_thread in waiters.into_iter().take(threads_to_wake) {
waiting_thread.wake()
}
}
*/
struct FutexWaitlist {
address: u8,
data: Vec<ThreadID>,
}
impl FutexWaitlist {
pub fn remove(&mut self) {
todo!();
}
pub fn add(&mut self) {
todo!();
}
}

View file

@ -10,5 +10,6 @@ pub mod virtual_memory;
pub mod kinfo; pub mod kinfo;
pub mod mail; pub mod mail;
pub mod futex;
pub mod info; pub mod info;
pub const BANNER: &str = include_str!("banner.txt"); pub const BANNER: &str = include_str!("banner.txt");

View file

@ -2,114 +2,116 @@ use alloc::string::String;
use wasmi::TrapKind; use wasmi::TrapKind;
use { use {
alloc::format, alloc::format,
wasm_sys::SysCall, wasm_sys::SysCall,
wasmi::{ wasmi::{
Error, Externals, FuncInstance, FuncRef, ImportsBuilder, ModuleImportResolver, Error, Externals, FuncInstance, FuncRef, ImportsBuilder, ModuleImportResolver,
ModuleInstance, RuntimeArgs, RuntimeValue, Signature, Trap, ValueType, ModuleInstance, RuntimeArgs, RuntimeValue, Signature, Trap, ValueType,
}, },
}; };
mod wasm_sys; mod wasm_sys;
struct HostFunctions; struct HostFunctions;
impl HostFunctions { impl HostFunctions {
fn check_signature(&self, index: usize, signature: &Signature) -> bool { fn check_signature(&self, index: usize, signature: &Signature) -> bool {
let (params, ret_ty): (&[ValueType], Option<ValueType>) = match index.into() { let (params, ret_ty): (&[ValueType], Option<ValueType>) = match index.into() {
SysCall::KILL => (&[], None), SysCall::KILL => (&[], None),
SysCall::EMPTY => (&[], None), SysCall::EMPTY => (&[], None),
_ => return false, _ => return false,
}; };
signature.params() == params && signature.return_type() == ret_ty signature.params() == params && signature.return_type() == ret_ty
} }
} }
impl Externals for HostFunctions { impl Externals for HostFunctions {
fn invoke_index( fn invoke_index(
&mut self, &mut self,
index: usize, index: usize,
args: RuntimeArgs, args: RuntimeArgs,
) -> Result<Option<RuntimeValue>, Trap> { ) -> Result<Option<RuntimeValue>, Trap> {
match index.into() { match index.into() {
// Take in one arg discard the rest // Take in one arg discard the rest
SysCall::KILL => { SysCall::KILL => {
info!("Program run at runtime called a system call"); info!("Program run at runtime called a system call");
debug!("Runtime arguments: {:?}", args); debug!("Runtime arguments: {:?}", args);
Ok(None) Ok(None)
} }
// Do nothing // Do nothing
SysCall::EMPTY => Ok(None), SysCall::EMPTY => Ok(None),
SysCall::EXIT => Ok(None), SysCall::EXIT => Ok(None),
SysCall::CONSOLE_RESET => Ok(None), SysCall::CONSOLE_RESET => Ok(None),
SysCall::CONSOLE_IN => { SysCall::CONSOLE_IN => {
info!("In"); info!("In");
Ok(None) Ok(None)
} }
SysCall::CONSOLE_OUT => { SysCall::CONSOLE_OUT => {
// Eventually change this to 2- ptr and len // Eventually change this to 2- ptr and len
if args.len() != 1 { if args.len() != 1 {
return Err(Trap::new(TrapKind::UnexpectedSignature)); return Err(Trap::new(TrapKind::UnexpectedSignature));
} }
let arg: u64 = args.nth(0); let arg: u64 = args.nth(0);
let buf = unsafe { String::from_utf8_unchecked(arg.to_le_bytes().to_vec()) }; let buf = unsafe { String::from_utf8_unchecked(arg.to_le_bytes().to_vec()) };
println!["{}", buf]; println!["helllooooooo{}", buf];
Ok(None) Ok(None)
} }
SysCall::CONSOLE_GET_TITLE => Ok(None), SysCall::CONSOLE_GET_TITLE => Ok(None),
SysCall::CONSOLE_SET_TITLE => Ok(None), SysCall::CONSOLE_SET_TITLE => Ok(None),
_ => panic!("Unimplemented function at {}", index), _ => panic!("Unimplemented function at {}", index),
} }
} }
} }
impl ModuleImportResolver for HostFunctions { impl ModuleImportResolver for HostFunctions {
fn resolve_func(&self, field_name: &str, signature: &Signature) -> Result<FuncRef, Error> { fn resolve_func(&self, field_name: &str, signature: &Signature) -> Result<FuncRef, Error> {
let index = match field_name { let index = match field_name {
"kill" => SysCall::KILL as usize, "kill" => SysCall::KILL as usize,
"empty" => SysCall::EMPTY as usize, "empty" => SysCall::EMPTY as usize,
"exit" => SysCall::EXIT as usize, "exit" => SysCall::EXIT as usize,
_ => { "console_out" => SysCall::CONSOLE_OUT as usize,
return Err(Error::Instantiation(format!( _ => {
"Export {} not found", return Err(Error::Instantiation(format!(
field_name "Export {} not found",
))) field_name
} )))
}; }
};
if !self.check_signature(index, signature) { if !self.check_signature(index, signature) {
return Err(Error::Instantiation(format!( return Err(Error::Instantiation(format!(
"Export {} has a bad signature", "Export {} has a bad signature",
field_name field_name
))); )));
} }
Ok(FuncInstance::alloc_host( Ok(FuncInstance::alloc_host(
Signature::new(&[][..], None), Signature::new(&[][..], None),
index, index,
)) ))
} }
} }
pub fn evaluate() { pub fn evaluate() {
let wasm_binary = include_bytes!("bin/rust.wasm"); let wasm_binary = include_bytes!("bin/console_out_test.wasm");
// Load wasm binary and prepare it for instantiation. // Load wasm binary and prepare it for instantiation.
let module = wasmi::Module::from_buffer(&wasm_binary).expect("failed to load wasm"); let module = wasmi::Module::from_buffer(&wasm_binary).expect("failed to load wasm");
let imports = ImportsBuilder::new().with_resolver("env", &HostFunctions); let imports = ImportsBuilder::new().with_resolver("env", &HostFunctions);
// Instantiate a module with empty imports and // Instantiate a module with empty imports and
// assert that there is no `start` function. // assert that there is no `start` function.
let instance = ModuleInstance::new(&module, &imports) let instance = ModuleInstance::new(&module, &imports)
.expect("failed to instantiate wasm module") // .expect("failed to instantiate wasm module")
.assert_no_start(); .unwrap()
.assert_no_start();
// Finally, invoke the exported function "test" with no parameters // Finally, invoke the exported function "test" with no parameters
// and empty external function executor. // and empty external function executor.
let result: i32 = instance let result: i32 = instance
.invoke_export("_start", &[], &mut HostFunctions) .invoke_export("_start", &[], &mut HostFunctions)
.expect("failed to execute export") .expect("failed to execute export")
.unwrap() .unwrap()
.try_into() .try_into()
.unwrap(); .unwrap();
println!("{:?}", result); println!("{:?}", result);
} }