diff --git a/Cargo.lock b/Cargo.lock index 63c768a81..7d308dee9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -633,6 +633,18 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "tests" +version = "0.1.0" +dependencies = [ + "bitflags", + "hex", + "lazy_static", + "rand", + "sha3", + "xous", +] + [[package]] name = "textwrap" version = "0.11.0" diff --git a/Cargo.toml b/Cargo.toml index 2b5a7b024..f66280c15 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ "kernel", + "tests", "loader", "tools", "xous-rs", diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 10870ef13..f1d3d63fa 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -10,24 +10,16 @@ bitflags = "1.2.1" xous = { path = "../xous-rs", features = ["forget-memory-messages"] } debug-here = "0.2.2" stats_alloc = { version = "0.1.8", optional = true } +sha3 = { default-features = false, version = "0.8.2" } [target.'cfg(target_arch = "riscv32")'.dependencies] riscv = "0.5.6" -[dependencies.sha3] -default-features = false -version = "0.8.2" - -[target.'cfg(windows)'.dependencies] -lazy_static = "1.4" - -[target.'cfg(unix)'.dependencies] -lazy_static = "1.4" - [features] debug-print = [] report-memory = ["stats_alloc"] [target.'cfg(any(windows, unix))'.dependencies] +lazy_static = "1.4" rand = "0.7" hex = "0.4" diff --git a/kernel/src/arch/hosted/process.rs b/kernel/src/arch/hosted/process.rs index b7661923b..9afb9ae49 100644 --- a/kernel/src/arch/hosted/process.rs +++ b/kernel/src/arch/hosted/process.rs @@ -212,9 +212,6 @@ impl Process { let mut process_table = pt.borrow_mut(); let current_pid_idx = process_table.current.get() as usize - 1; let process = &mut process_table.table[current_pid_idx].as_mut().unwrap(); - if !process.threads[thread - 1].allocated { - ::debug_here::debug_here!(); - } assert!( process.threads[thread - 1].allocated, "tried to switch to thread {} which wasn't allocated", diff --git a/kernel/src/services.rs b/kernel/src/services.rs index eaba7bbee..c0bc9079f 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -1239,7 +1239,6 @@ impl SystemServices { // TODO: Come up with a way to randomize the server ID let ppid = self.get_process(pid)?.ppid.get(); if ppid != 1 { - debug_here::debug_here!(); panic!("Clients cannot start servers yet"); } diff --git a/tests/Cargo.toml b/tests/Cargo.toml new file mode 100644 index 000000000..99db0deb7 --- /dev/null +++ b/tests/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "tests" +version = "0.1.0" +authors = ["Sean Cross "] +edition = "2018" +description = "Core kernel for Xous, including task switching and memory management" + +[lib] +name = "kernel" +path = "../kernel/src/main.rs" +test = true + +[dependencies] +bitflags = "1.2.1" +sha3 = { default-features = false, version = "0.8.2" } +xous = { path = "../xous-rs", features = ["forget-memory-messages", "processes-as-threads"] } + +[target.'cfg(any(windows, unix))'.dependencies] +rand = "0.7" +hex = "0.4" +lazy_static = "1.4" diff --git a/xous-rs/Cargo.toml b/xous-rs/Cargo.toml index c0086533c..21867e64e 100644 --- a/xous-rs/Cargo.toml +++ b/xous-rs/Cargo.toml @@ -18,6 +18,10 @@ default-features = false # will not be implemented. This should only be set by the kernel. forget-memory-messages = [] +# If this is set, create_process() will instead create a thread in +# the current process. This is used by the test suite. +processes-as-threads = [] + [target.'cfg(any(windows,unix))'.dependencies] lazy_static = "1.4" hex = "0.4" diff --git a/xous-rs/src/arch/hosted.rs b/xous-rs/src/arch/hosted.rs index 5fb412ddf..a8de8c99a 100644 --- a/xous-rs/src/arch/hosted.rs +++ b/xous-rs/src/arch/hosted.rs @@ -25,25 +25,180 @@ pub struct ProcessInit { pub key: ProcessKey, } -pub struct ProcessArgs { - main: Option, - name: String, -} +#[cfg(feature = "processes-as-threads")] +mod process { + pub use super::*; + pub struct ProcessArgs { + main: Option, + name: String, + } -impl ProcessArgs -where - F: FnOnce(), -{ - pub fn new(name: &str, main: F) -> ProcessArgs { - ProcessArgs { - main: Some(main), - name: name.to_owned(), + impl ProcessArgs + where + F: FnOnce(), + { + pub fn new(name: &str, main: F) -> ProcessArgs { + ProcessArgs { + main: Some(main), + name: name.to_owned(), + } } } + pub struct ProcessHandle(std::thread::JoinHandle<()>); + + /// If no connection exists, create a new connection to the server. This means + /// our parent PID will be PID1. Otherwise, reuse the same connection. + pub fn create_process_pre( + _args: &ProcessArgs, + ) -> core::result::Result + where + F: FnOnce(), + { + ensure_connection()?; + + // Ensure there is a connection, because after this function returns + // we'll make a syscall with CreateProcess(). This should only need + // to happen for PID1. + Ok(ProcessInit { + key: PROCESS_KEY + .with(|pk| *pk.borrow()) + .unwrap_or_else(default_process_key), + }) + } + + pub fn create_process_post( + mut args: ProcessArgs, + init: ProcessInit, + pid: PID, + ) -> core::result::Result + where + F: FnOnce() + Send + 'static, + { + let server_address = xous_address(); + + let f = args.main.take().unwrap(); + let thread_main = std::thread::Builder::new() + .name(args.name) + .spawn(move || { + set_xous_address(server_address); + THREAD_ID.with(|tid| *tid.borrow_mut() = 1); + PROCESS_ID.with(|p| *p.borrow_mut() = pid); + XOUS_SERVER_CONNECTION.with(|xsc| { + let mut xsc = xsc.borrow_mut(); + match xous_connect_impl(server_address, &init.key) { + Ok(a) => { + *xsc = Some(a); + Ok(()) + } + Err(_) => Err(crate::Error::InternalError), + } + })?; + + crate::create_thread(f) + }) + .map_err(|_| crate::Error::InternalError)? + .join() + .unwrap() + .unwrap(); + + Ok(ProcessHandle(thread_main.0)) + } + + pub fn wait_process(joiner: ProcessHandle) -> crate::SysCallResult { + joiner.0.join().map(|_| Result::Ok).map_err(|_x| { + // panic!("wait error: {:?}", x); + crate::Error::InternalError + }) + } } +#[cfg(not(feature = "processes-as-threads"))] +mod process { + pub use super::*; + pub struct ProcessArgs { + command: String, + name: String, + _ign: std::marker::PhantomData, + } + + impl ProcessArgs { + pub fn new(name: &str, command: String) -> ProcessArgs { + ProcessArgs { + command, + name: name.to_owned(), + _ign: std::marker::PhantomData, + } + } + } + pub struct ProcessHandle(std::process::Child); + + /// If no connection exists, create a new connection to the server. This means + /// our parent PID will be PID1. Otherwise, reuse the same connection. + pub fn create_process_pre( + _args: &ProcessArgs, + ) -> core::result::Result { + ensure_connection()?; + + // Ensure there is a connection, because after this function returns + // we'll make a syscall with CreateProcess(). This should only need + // to happen for PID1. + Ok(ProcessInit { + key: PROCESS_KEY + .with(|pk| *pk.borrow()) + .unwrap_or_else(default_process_key), + }) + } + + pub fn create_process_post( + args: ProcessArgs, + init: ProcessInit, + pid: PID, + ) -> core::result::Result { + use std::process::Command; + let server_env = format!("{}", xous_address()); + let pid_env = format!("{}", pid); + let process_name_env = args.name.to_string(); + let process_key_env = hex::encode(&init.key.0); + let (shell, args) = if cfg!(windows) { + ("cmd", ["/C", &args.command]) + } else if cfg!(unix) { + ("sh", ["-c", &args.command]) + } else { + panic!("unrecognized platform -- don't know how to shell out"); + }; + + Command::new(shell) + .args(&args) + .env("XOUS_SERVER", server_env) + .env("XOUS_PID", pid_env) + .env("XOUS_PROCESS_NAME", process_name_env) + .env("XOUS_PROCESS_KEY", process_key_env) + .spawn() + .map(ProcessHandle) + .map_err(|e| { + eprintln!("couldn't start command: {}", e); + crate::Error::InternalError + }) + } + + pub fn wait_process(mut joiner: ProcessHandle) -> crate::SysCallResult { + joiner + .0 + .wait() + .or(Err(crate::Error::InternalError)) + .and_then(|e| { + if e.success() { + Ok(crate::Result::Ok) + } else { + Err(crate::Error::UnknownError) + } + }) + } +} + +pub use process::*; + pub struct WaitHandle(std::thread::JoinHandle); -pub struct ProcessHandle(std::thread::JoinHandle<()>); #[derive(Clone)] struct ServerConnection { @@ -214,71 +369,6 @@ pub fn ensure_connection() -> core::result::Result<(), crate::Error> { }) } -/// If no connection exists, create a new connection to the server. This means -/// our parent PID will be PID1. Otherwise, reuse the same connection. -pub fn create_process_pre( - _args: &ProcessArgs, -) -> core::result::Result -where - F: FnOnce(), -{ - ensure_connection()?; - - // Ensure there is a connection, because after this function returns - // we'll make a syscall with CreateProcess(). This should only need - // to happen for PID1. - Ok(ProcessInit { - key: PROCESS_KEY - .with(|pk| *pk.borrow()) - .unwrap_or_else(default_process_key), - }) -} - -pub fn create_process_post( - mut args: ProcessArgs, - init: ProcessInit, - pid: PID, -) -> core::result::Result -where - F: FnOnce() + Send + 'static, -{ - let server_address = xous_address(); - - let f = args.main.take().unwrap(); - let thread_main = std::thread::Builder::new() - .name(args.name) - .spawn(move || { - set_xous_address(server_address); - THREAD_ID.with(|tid| *tid.borrow_mut() = 1); - PROCESS_ID.with(|p| *p.borrow_mut() = pid); - XOUS_SERVER_CONNECTION.with(|xsc| { - let mut xsc = xsc.borrow_mut(); - match xous_connect_impl(server_address, &init.key) { - Ok(a) => { - *xsc = Some(a); - Ok(()) - } - Err(_) => Err(crate::Error::InternalError), - } - })?; - - crate::create_thread(f) - }) - .map_err(|_| crate::Error::InternalError)? - .join() - .unwrap() - .unwrap(); - - Ok(ProcessHandle(thread_main.0)) -} - -pub fn wait_process(joiner: ProcessHandle) -> crate::SysCallResult { - joiner.0.join().map(|_| Result::Ok).map_err(|_x| { - // panic!("wait error: {:?}", x); - crate::Error::InternalError - }) -} - fn xous_connect_impl( addr: SocketAddr, key: &ProcessKey,