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]]
name = "tests"
version = "0.1.0"
version = "0.1.1"
dependencies = [
"bitflags",
"hex",

View file

@ -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<SocketAddr>) {
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");
}
}

View file

@ -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();
}

View file

@ -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) {

View file

@ -1,6 +1,6 @@
[package]
name = "tests"
version = "0.1.0"
version = "0.1.1"
authors = ["Sean Cross <sean@xobs.io>"]
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" }

View file

@ -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"

View file

@ -115,27 +115,27 @@ mod process {
#[cfg(not(feature = "processes-as-threads"))]
mod process {
pub use super::*;
pub struct ProcessArgs<F> {
pub struct ProcessArgs {
command: String,
name: String,
_ign: std::marker::PhantomData<F>,
}
impl<F> ProcessArgs<F> {
pub fn new(name: &str, command: String) -> ProcessArgs<F> {
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<F>(
_args: &ProcessArgs<F>,
pub fn create_process_pre(
_args: &ProcessArgs,
) -> core::result::Result<ProcessInit, crate::Error> {
ensure_connection()?;
@ -149,8 +149,8 @@ mod process {
})
}
pub fn create_process_post<F>(
args: ProcessArgs<F>,
pub fn create_process_post(
args: ProcessArgs,
init: ProcessInit,
pid: PID,
) -> core::result::Result<ProcessHandle, crate::Error> {
@ -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 {

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
pub fn create_process<F>(
args: ProcessArgs<F>,
) -> core::result::Result<crate::arch::ProcessHandle, Error>
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<F>(
args: ProcessArgs<F>,
) -> core::result::Result<crate::arch::ProcessHandle, Error>
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<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 {
let mut ret = Result::Ok;
let args = call.as_args();