diff --git a/Cargo.lock b/Cargo.lock index 7d308dee9..65f4f488d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -635,7 +635,7 @@ dependencies = [ [[package]] name = "tests" -version = "0.1.0" +version = "0.1.1" dependencies = [ "bitflags", "hex", diff --git a/kernel/src/arch/hosted.rs b/kernel/src/arch/hosted.rs index 46d8bbd69..444802757 100644 --- a/kernel/src/arch/hosted.rs +++ b/kernel/src/arch/hosted.rs @@ -50,12 +50,14 @@ pub fn set_listen_address(new_address: &SocketAddr) { } /// Set the network address for this particular thread. +#[allow(dead_code)] pub fn set_send_addr(send_addr: Sender) { SEND_ADDR.with(|sa| { *sa.borrow_mut() = Some(send_addr); }); } +#[cfg(not(feature = "testing"))] fn generate_pid_key() -> [u8; 16] { use rand::{thread_rng, Rng}; let mut process_key = [0u8; 16]; @@ -384,7 +386,9 @@ pub fn idle() -> bool { // Allocate PID1 with the key we were passed. let pid1_key = PID1_KEY.with(|p1k| *p1k.borrow()); - let pid1_init = ProcessInit { key: ProcessKey::new(pid1_key) }; + let pid1_init = ProcessInit { + key: ProcessKey::new(pid1_key), + }; let pid1 = SystemServices::with_mut(|ss| ss.create_process(pid1_init)).unwrap(); assert_eq!(pid1.get(), 1); @@ -397,7 +401,7 @@ pub fn idle() -> bool { }) .unwrap_or_else(|_| NETWORK_LISTEN_ADDRESS.with(|nla| *nla.borrow())); - #[cfg(not(test))] + #[cfg(not(feature = "testing"))] let address_receiver = { let (sender, receiver) = channel(); set_send_addr(sender); @@ -412,9 +416,10 @@ pub fn idle() -> bool { .expect("couldn't spawn listen thread") }); - #[cfg(not(test))] + #[cfg(not(feature = "testing"))] { let address = address_receiver.recv().unwrap(); + xous::arch::set_xous_address(address); println!("KERNEL: Xous server listening on {}", address); println!("KERNEL: Starting initial processes:"); let mut args = std::env::args(); @@ -424,24 +429,19 @@ pub fn idle() -> bool { // are owned by PID1. crate::arch::process::set_current_pid(pid1); + // Go through each arg and spawn it as a new process. Failures here will + // halt the entire system. println!(" PID | Command"); println!("-------+------------------"); for arg in args { - let process_key = generate_pid_key(); - let init = xous::ProcessInit { key: ProcessKey::new(process_key) }; + let init = xous::ProcessInit { + key: ProcessKey::new(process_key), + }; let new_pid = SystemServices::with_mut(|ss| ss.create_process(init)).unwrap(); println!(" {:^5} | {}", new_pid, arg); - - use std::process::Command; - let server_env = format!("{}", address); - let process_key_env = hex::encode(process_key); - let cmd = Command::new("cmd") - .args(&["/C", &arg]) - .env("XOUS_SERVER", server_env) - .env("XOUS_PROCESS_KEY", process_key_env) - .spawn() - .expect("couldn't start command"); + let process_args = xous::ProcessArgs::new("program", arg); + xous::arch::create_process_post(process_args, init, new_pid).expect("couldn't spawn"); } } diff --git a/kernel/src/main.rs b/kernel/src/main.rs index cfa9c737c..048af7cd8 100644 --- a/kernel/src/main.rs +++ b/kernel/src/main.rs @@ -158,7 +158,7 @@ pub extern "C" fn kmain() { /// The main entrypoint when run in hosted mode. When running in embedded mode, /// this function does not exist. -#[cfg(not(baremetal))] +#[cfg(all(not(baremetal),not(feature = "testing")))] fn main() { kmain(); } diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 6df593cc3..46a297553 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -112,22 +112,22 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes Message::Scalar(_) | Message::Move(_) => 0, Message::Borrow(_) | Message::MutableBorrow(_) => ss .remember_server_message(sidx, pid, thread, &message, client_address) - .or_else(|e| { + .map_err(|e| { ss.server_from_sidx(sidx) .expect("server couldn't be located") .return_available_thread(thread); - Err(e) + e })?, }; let envelope = MessageEnvelope { sender, message }; // Mark the server's context as "Ready". If this fails, return the context // to the blocking list. - ss.ready_thread(server_pid, server_tid).or_else(|e| { + ss.ready_thread(server_pid, server_tid).map_err(|e| { ss.server_from_sidx(sidx) .expect("server couldn't be located") .return_available_thread(thread); - Err(e) + e })?; if blocking && cfg!(baremetal) { diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 99db0deb7..a20306f79 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tests" -version = "0.1.0" +version = "0.1.1" authors = ["Sean Cross "] edition = "2018" description = "Core kernel for Xous, including task switching and memory management" @@ -10,6 +10,10 @@ name = "kernel" path = "../kernel/src/main.rs" test = true +[features] +testing = [] +default = ["testing"] + [dependencies] bitflags = "1.2.1" sha3 = { default-features = false, version = "0.8.2" } diff --git a/xous-rs/Cargo.toml b/xous-rs/Cargo.toml index 21867e64e..0cecb5757 100644 --- a/xous-rs/Cargo.toml +++ b/xous-rs/Cargo.toml @@ -22,6 +22,8 @@ forget-memory-messages = [] # the current process. This is used by the test suite. processes-as-threads = [] +default = [] + [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 a8de8c99a..0a2cba4f0 100644 --- a/xous-rs/src/arch/hosted.rs +++ b/xous-rs/src/arch/hosted.rs @@ -115,27 +115,27 @@ mod process { #[cfg(not(feature = "processes-as-threads"))] mod process { pub use super::*; - pub struct ProcessArgs { + pub struct ProcessArgs { command: String, name: String, - _ign: std::marker::PhantomData, } - impl ProcessArgs { - pub fn new(name: &str, command: String) -> ProcessArgs { + impl ProcessArgs { + pub fn new(name: &str, command: String) -> ProcessArgs { ProcessArgs { command, name: name.to_owned(), - _ign: std::marker::PhantomData, } } } + + #[derive(Debug)] 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, + pub fn create_process_pre( + _args: &ProcessArgs, ) -> core::result::Result { ensure_connection()?; @@ -149,8 +149,8 @@ mod process { }) } - pub fn create_process_post( - args: ProcessArgs, + pub fn create_process_post( + args: ProcessArgs, init: ProcessInit, pid: PID, ) -> core::result::Result { @@ -167,7 +167,8 @@ mod process { panic!("unrecognized platform -- don't know how to shell out"); }; - Command::new(shell) + println!("Launching process..."); + let result = Command::new(shell) .args(&args) .env("XOUS_SERVER", server_env) .env("XOUS_PID", pid_env) @@ -178,7 +179,9 @@ mod process { .map_err(|e| { eprintln!("couldn't start command: {}", e); crate::Error::InternalError - }) + }); + println!("Process result: {:?}", result); + result } pub fn wait_process(mut joiner: ProcessHandle) -> crate::SysCallResult { diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index 03359ee97..17b9e4853 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -837,27 +837,56 @@ pub fn wait_thread(joiner: crate::arch::WaitHandle) -> SysCallResult { } /// Create a new process by running it in its own thread -pub fn create_process( - args: ProcessArgs, -) -> core::result::Result -where - F: FnOnce() + Send + 'static, -{ - let process_init = crate::arch::create_process_pre(&args)?; - rsyscall(SysCall::CreateProcess(process_init)).and_then(|result| { - if let Result::ProcessID(pid) = result { - crate::arch::create_process_post(args, process_init, pid) - } else { - Err(Error::InternalError) - } - }) +#[cfg(feature = "processes-as-threads")] +mod process { + use super::*; + pub fn create_process( + args: ProcessArgs, + ) -> core::result::Result + where + F: FnOnce() + Send + 'static, + { + let process_init = crate::arch::create_process_pre(&args)?; + rsyscall(SysCall::CreateProcess(process_init)).and_then(|result| { + if let Result::ProcessID(pid) = result { + crate::arch::create_process_post(args, process_init, pid) + } else { + Err(Error::InternalError) + } + }) + } + + /// Wait for a thread to finish + pub fn wait_process(joiner: crate::arch::ProcessHandle) -> SysCallResult { + crate::arch::wait_process(joiner) + } } -/// Wait for a thread to finish -pub fn wait_process(joiner: crate::arch::ProcessHandle) -> SysCallResult { - crate::arch::wait_process(joiner) +#[cfg(not(feature = "processes-as-threads"))] +mod process { + use super::*; + pub fn create_process( + args: ProcessArgs, + ) -> core::result::Result + { + let process_init = crate::arch::create_process_pre(&args)?; + rsyscall(SysCall::CreateProcess(process_init)).and_then(|result| { + if let Result::ProcessID(pid) = result { + crate::arch::create_process_post(args, process_init, pid) + } else { + Err(Error::InternalError) + } + }) + } + + /// Wait for a thread to finish + pub fn wait_process(joiner: crate::arch::ProcessHandle) -> SysCallResult { + crate::arch::wait_process(joiner) + } } +pub use process::*; + pub fn rsyscall(call: SysCall) -> SysCallResult { let mut ret = Result::Ok; let args = call.as_args();