kernel: get tests and running working

This fixes process creation so that it works for both tests and running
it as a program.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-07-29 22:37:03 +08:00
parent 039975323e
commit 869ebcff8b
8 changed files with 88 additions and 50 deletions

2
Cargo.lock generated
View file

@ -635,7 +635,7 @@ dependencies = [
[[package]] [[package]]
name = "tests" name = "tests"
version = "0.1.0" version = "0.1.1"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"hex", "hex",

View file

@ -50,12 +50,14 @@ pub fn set_listen_address(new_address: &SocketAddr) {
} }
/// Set the network address for this particular thread. /// Set the network address for this particular thread.
#[allow(dead_code)]
pub fn set_send_addr(send_addr: Sender<SocketAddr>) { pub fn set_send_addr(send_addr: Sender<SocketAddr>) {
SEND_ADDR.with(|sa| { SEND_ADDR.with(|sa| {
*sa.borrow_mut() = Some(send_addr); *sa.borrow_mut() = Some(send_addr);
}); });
} }
#[cfg(not(feature = "testing"))]
fn generate_pid_key() -> [u8; 16] { fn generate_pid_key() -> [u8; 16] {
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
let mut process_key = [0u8; 16]; let mut process_key = [0u8; 16];
@ -384,7 +386,9 @@ pub fn idle() -> bool {
// Allocate PID1 with the key we were passed. // Allocate PID1 with the key we were passed.
let pid1_key = PID1_KEY.with(|p1k| *p1k.borrow()); 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(); let pid1 = SystemServices::with_mut(|ss| ss.create_process(pid1_init)).unwrap();
assert_eq!(pid1.get(), 1); assert_eq!(pid1.get(), 1);
@ -397,7 +401,7 @@ pub fn idle() -> bool {
}) })
.unwrap_or_else(|_| NETWORK_LISTEN_ADDRESS.with(|nla| *nla.borrow())); .unwrap_or_else(|_| NETWORK_LISTEN_ADDRESS.with(|nla| *nla.borrow()));
#[cfg(not(test))] #[cfg(not(feature = "testing"))]
let address_receiver = { let address_receiver = {
let (sender, receiver) = channel(); let (sender, receiver) = channel();
set_send_addr(sender); set_send_addr(sender);
@ -412,9 +416,10 @@ pub fn idle() -> bool {
.expect("couldn't spawn listen thread") .expect("couldn't spawn listen thread")
}); });
#[cfg(not(test))] #[cfg(not(feature = "testing"))]
{ {
let address = address_receiver.recv().unwrap(); let address = address_receiver.recv().unwrap();
xous::arch::set_xous_address(address);
println!("KERNEL: Xous server listening on {}", address); println!("KERNEL: Xous server listening on {}", address);
println!("KERNEL: Starting initial processes:"); println!("KERNEL: Starting initial processes:");
let mut args = std::env::args(); let mut args = std::env::args();
@ -424,24 +429,19 @@ pub fn idle() -> bool {
// are owned by PID1. // are owned by PID1.
crate::arch::process::set_current_pid(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!(" PID | Command");
println!("-------+------------------"); println!("-------+------------------");
for arg in args { for arg in args {
let process_key = generate_pid_key(); 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(); let new_pid = SystemServices::with_mut(|ss| ss.create_process(init)).unwrap();
println!(" {:^5} | {}", new_pid, arg); println!(" {:^5} | {}", new_pid, arg);
let process_args = xous::ProcessArgs::new("program", arg);
use std::process::Command; xous::arch::create_process_post(process_args, init, new_pid).expect("couldn't spawn");
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");
} }
} }

View file

@ -158,7 +158,7 @@ pub extern "C" fn kmain() {
/// The main entrypoint when run in hosted mode. When running in embedded mode, /// The main entrypoint when run in hosted mode. When running in embedded mode,
/// this function does not exist. /// this function does not exist.
#[cfg(not(baremetal))] #[cfg(all(not(baremetal),not(feature = "testing")))]
fn main() { fn main() {
kmain(); kmain();
} }

View file

@ -112,22 +112,22 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes
Message::Scalar(_) | Message::Move(_) => 0, Message::Scalar(_) | Message::Move(_) => 0,
Message::Borrow(_) | Message::MutableBorrow(_) => ss Message::Borrow(_) | Message::MutableBorrow(_) => ss
.remember_server_message(sidx, pid, thread, &message, client_address) .remember_server_message(sidx, pid, thread, &message, client_address)
.or_else(|e| { .map_err(|e| {
ss.server_from_sidx(sidx) ss.server_from_sidx(sidx)
.expect("server couldn't be located") .expect("server couldn't be located")
.return_available_thread(thread); .return_available_thread(thread);
Err(e) e
})?, })?,
}; };
let envelope = MessageEnvelope { sender, message }; let envelope = MessageEnvelope { sender, message };
// Mark the server's context as "Ready". If this fails, return the context // Mark the server's context as "Ready". If this fails, return the context
// to the blocking list. // 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) ss.server_from_sidx(sidx)
.expect("server couldn't be located") .expect("server couldn't be located")
.return_available_thread(thread); .return_available_thread(thread);
Err(e) e
})?; })?;
if blocking && cfg!(baremetal) { if blocking && cfg!(baremetal) {

View file

@ -1,6 +1,6 @@
[package] [package]
name = "tests" name = "tests"
version = "0.1.0" version = "0.1.1"
authors = ["Sean Cross <sean@xobs.io>"] authors = ["Sean Cross <sean@xobs.io>"]
edition = "2018" edition = "2018"
description = "Core kernel for Xous, including task switching and memory management" description = "Core kernel for Xous, including task switching and memory management"
@ -10,6 +10,10 @@ name = "kernel"
path = "../kernel/src/main.rs" path = "../kernel/src/main.rs"
test = true test = true
[features]
testing = []
default = ["testing"]
[dependencies] [dependencies]
bitflags = "1.2.1" bitflags = "1.2.1"
sha3 = { default-features = false, version = "0.8.2" } sha3 = { default-features = false, version = "0.8.2" }

View file

@ -22,6 +22,8 @@ forget-memory-messages = []
# the current process. This is used by the test suite. # the current process. This is used by the test suite.
processes-as-threads = [] processes-as-threads = []
default = []
[target.'cfg(any(windows,unix))'.dependencies] [target.'cfg(any(windows,unix))'.dependencies]
lazy_static = "1.4" lazy_static = "1.4"
hex = "0.4" hex = "0.4"

View file

@ -115,27 +115,27 @@ mod process {
#[cfg(not(feature = "processes-as-threads"))] #[cfg(not(feature = "processes-as-threads"))]
mod process { mod process {
pub use super::*; pub use super::*;
pub struct ProcessArgs<F> { pub struct ProcessArgs {
command: String, command: String,
name: String, name: String,
_ign: std::marker::PhantomData<F>,
} }
impl<F> ProcessArgs<F> { impl ProcessArgs {
pub fn new(name: &str, command: String) -> ProcessArgs<F> { pub fn new(name: &str, command: String) -> ProcessArgs {
ProcessArgs { ProcessArgs {
command, command,
name: name.to_owned(), name: name.to_owned(),
_ign: std::marker::PhantomData,
} }
} }
} }
#[derive(Debug)]
pub struct ProcessHandle(std::process::Child); pub struct ProcessHandle(std::process::Child);
/// If no connection exists, create a new connection to the server. This means /// If no connection exists, create a new connection to the server. This means
/// our parent PID will be PID1. Otherwise, reuse the same connection. /// our parent PID will be PID1. Otherwise, reuse the same connection.
pub fn create_process_pre<F>( pub fn create_process_pre(
_args: &ProcessArgs<F>, _args: &ProcessArgs,
) -> core::result::Result<ProcessInit, crate::Error> { ) -> core::result::Result<ProcessInit, crate::Error> {
ensure_connection()?; ensure_connection()?;
@ -149,8 +149,8 @@ mod process {
}) })
} }
pub fn create_process_post<F>( pub fn create_process_post(
args: ProcessArgs<F>, args: ProcessArgs,
init: ProcessInit, init: ProcessInit,
pid: PID, pid: PID,
) -> core::result::Result<ProcessHandle, crate::Error> { ) -> core::result::Result<ProcessHandle, crate::Error> {
@ -167,7 +167,8 @@ mod process {
panic!("unrecognized platform -- don't know how to shell out"); panic!("unrecognized platform -- don't know how to shell out");
}; };
Command::new(shell) println!("Launching process...");
let result = Command::new(shell)
.args(&args) .args(&args)
.env("XOUS_SERVER", server_env) .env("XOUS_SERVER", server_env)
.env("XOUS_PID", pid_env) .env("XOUS_PID", pid_env)
@ -178,7 +179,9 @@ mod process {
.map_err(|e| { .map_err(|e| {
eprintln!("couldn't start command: {}", e); eprintln!("couldn't start command: {}", e);
crate::Error::InternalError crate::Error::InternalError
}) });
println!("Process result: {:?}", result);
result
} }
pub fn wait_process(mut joiner: ProcessHandle) -> crate::SysCallResult { pub fn wait_process(mut joiner: ProcessHandle) -> crate::SysCallResult {

View file

@ -837,27 +837,56 @@ pub fn wait_thread<T>(joiner: crate::arch::WaitHandle<T>) -> SysCallResult {
} }
/// Create a new process by running it in its own thread /// Create a new process by running it in its own thread
pub fn create_process<F>( #[cfg(feature = "processes-as-threads")]
args: ProcessArgs<F>, mod process {
) -> core::result::Result<crate::arch::ProcessHandle, Error> use super::*;
where pub fn create_process<F>(
F: FnOnce() + Send + 'static, args: ProcessArgs<F>,
{ ) -> core::result::Result<crate::arch::ProcessHandle, Error>
let process_init = crate::arch::create_process_pre(&args)?; where
rsyscall(SysCall::CreateProcess(process_init)).and_then(|result| { F: FnOnce() + Send + 'static,
if let Result::ProcessID(pid) = result { {
crate::arch::create_process_post(args, process_init, pid) let process_init = crate::arch::create_process_pre(&args)?;
} else { rsyscall(SysCall::CreateProcess(process_init)).and_then(|result| {
Err(Error::InternalError) 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 #[cfg(not(feature = "processes-as-threads"))]
pub fn wait_process(joiner: crate::arch::ProcessHandle) -> SysCallResult { mod process {
crate::arch::wait_process(joiner) use super::*;
pub fn create_process(
args: ProcessArgs,
) -> core::result::Result<crate::arch::ProcessHandle, Error>
{
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 { pub fn rsyscall(call: SysCall) -> SysCallResult {
let mut ret = Result::Ok; let mut ret = Result::Ok;
let args = call.as_args(); let args = call.as_args();