diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..da15068 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,5 @@ +[workspace] + +members = [ + "libwasm", +] \ No newline at end of file diff --git a/libwasm/Cargo.toml b/libwasm/Cargo.toml new file mode 100644 index 0000000..8cab3f3 --- /dev/null +++ b/libwasm/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "libwasm" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/libwasm/src/driver.rs b/libwasm/src/driver.rs new file mode 100644 index 0000000..82dde53 --- /dev/null +++ b/libwasm/src/driver.rs @@ -0,0 +1,4 @@ +pub enum DriverExitCode { + Success = 0, + Failure = 1, +} diff --git a/libwasm/src/lib.rs b/libwasm/src/lib.rs new file mode 100644 index 0000000..46b24bf --- /dev/null +++ b/libwasm/src/lib.rs @@ -0,0 +1,13 @@ +#![no_std] + +#[macro_use] +pub mod logger; +pub mod driver; +pub mod process; +pub mod syscalls; + +pub use core::*; + +extern "C" { + pub fn get_random() -> u32; +} diff --git a/libwasm/src/logger/mod.rs b/libwasm/src/logger/mod.rs new file mode 100644 index 0000000..8f0d077 --- /dev/null +++ b/libwasm/src/logger/mod.rs @@ -0,0 +1,68 @@ +#![allow(unused_macros)] + +#[repr(C)] +pub enum LogLevel { + Error, + Warn, + Info, + Debug, + Trace, +} + +extern "C" { + pub fn host_log(log_level: LogLevel, log_message: *const u8, log_message_len: u32); +} +#[macro_export] +macro_rules! log { + ($a:expr, $b:expr) => {{ + let log_level: LogLevel = $a; + let log_message: &[u8] = $b; + + let pointer = log_message.as_ptr(); + + unsafe { + $crate::logger::host_log(log_level, pointer, log_message.len() as u32); + } + }}; +} +#[macro_export] + +macro_rules! debug { + ($a:expr) => { + use crate::logger::{LogLevel::*, *}; + + log!(Debug, $a); + }; +} +#[macro_export] +macro_rules! error { + ($a:expr) => { + use crate::logger::{LogLevel::*, *}; + + log!(Error, $a); + }; +} +#[macro_export] +macro_rules! trace { + ($a:expr) => { + use crate::logger::*; + + log!(Trace, $a); + }; +} +#[macro_export] +macro_rules! warn { + ($a:expr) => { + use crate::logger::{LogLevel::*, *}; + + log!(Warn, $a); + }; +} +#[macro_export] +macro_rules! info { + ($a:expr) => { + use crate::logger::{LogLevel::*, *}; + + log!(Info, $a); + }; +} diff --git a/libwasm/src/process/mod.rs b/libwasm/src/process/mod.rs new file mode 100644 index 0000000..cab46e3 --- /dev/null +++ b/libwasm/src/process/mod.rs @@ -0,0 +1,6 @@ +pub mod signals; + +/// Process Identification +#[derive(Clone, Copy, PartialEq, Debug)] +#[repr(C)] +pub struct PID(pub usize); diff --git a/libwasm/src/process/signals.rs b/libwasm/src/process/signals.rs new file mode 100644 index 0000000..8df469d --- /dev/null +++ b/libwasm/src/process/signals.rs @@ -0,0 +1,8 @@ +#[repr(C)] +/// Signals that can be sent to a process +pub enum Signals { + /// Terminate the process + Terminate, + /// Shutdown the process and allow it to shutdown cleanly + Quit, +} diff --git a/libwasm/src/syscalls/file_calls.rs b/libwasm/src/syscalls/file_calls.rs new file mode 100644 index 0000000..653eced --- /dev/null +++ b/libwasm/src/syscalls/file_calls.rs @@ -0,0 +1,43 @@ +//! File system related system calls. + +/// Temporary representation of a file path +pub type Path = *const u8; + +/// Remove a Directory from the filesystem +/// +/// # Arguments +/// * `full_path` - The full path of the directory to remove +/// * `force` - Whether to remove the directory even if it is not empty +#[no_mangle] +pub extern "C" fn remove_directory(path: Path, force_delete: bool) { + unimplemented!(); +} + +/// Create a new directory at the given path +/// +/// # Arguments +/// * `full_path` - The full path of the directory to create +#[no_mangle] +pub extern "C" fn create_directory(path: Path) -> FSReturns { + unimplemented!(); +} + +#[repr(C)] +/// A return used by the file related system calls +pub enum FSReturns { + /// The system call was successful + Ok, + + /// The directory can not be created + DirectoryCouldNotBeCreated, + /// The directory could not be removed + DirectoryCouldNotBeRemoved, + /// + FileCouldNotBeCreated, + /// + FileCouldNotBeRemoved, + /// The file could not be opened + FileCouldNotBeOpened, + /// + FileCouldNotBeClosed, +} diff --git a/libwasm/src/syscalls/mod.rs b/libwasm/src/syscalls/mod.rs new file mode 100644 index 0000000..e618ae8 --- /dev/null +++ b/libwasm/src/syscalls/mod.rs @@ -0,0 +1,24 @@ +#![deny(missing_docs)] +//! The module of syscalls. + +use crate::process::{signals::Signals, PID}; + +pub mod file_calls; +pub mod time_calls; + +#[no_mangle] +/// All system calls are defined here. +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; + + /// A temporary function to test the syscall interface + pub fn add(a: u32, b: u32) -> u32; + +} diff --git a/libwasm/src/syscalls/time_calls.rs b/libwasm/src/syscalls/time_calls.rs new file mode 100644 index 0000000..d22adc1 --- /dev/null +++ b/libwasm/src/syscalls/time_calls.rs @@ -0,0 +1,24 @@ +//! Time related system calls. + +/// Seconds and milliseconds since the Unix epoch. +#[repr(C)] +#[derive(Debug)] +pub struct SecondsTime { + /// Seconds + pub seconds: u64, + /// Milliseconds + pub milliseconds: u64, +} + +extern "C" { + /// Sleep the calling process for the given number of milliseconds + pub fn sleep(time: SecondsTime); + + /// Get the current time in seconds, milliseconds + /// + /// Temporarily returns an i32 instead of a SecondsTime + pub fn get_time() -> i32; + + /// Set the current time in seconds, milliseconds + pub fn set_time(time: SecondsTime); +}