kernel: get shutdown and multi-clients working

THis now has proper authentication for tests.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-07-28 13:28:14 +08:00
parent 7258fc83b7
commit 19222c34a9
8 changed files with 138 additions and 137 deletions

1
Cargo.lock generated
View file

@ -301,6 +301,7 @@ dependencies = [
"bitflags",
"debug-here",
"lazy_static",
"rand",
"riscv",
"sha3",
"stats_alloc",

View file

@ -27,3 +27,6 @@ lazy_static = "1.4"
[features]
debug-print = []
report-memory = ["stats_alloc"]
[target.'cfg(any(windows, unix))'.dependencies]
rand = "0.7"

View file

@ -5,7 +5,7 @@ pub mod syscall;
use std::cell::RefCell;
use std::env;
use std::io::{Read, Write};
use std::io::Read;
use std::mem::size_of;
use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener, TcpStream, ToSocketAddrs};
use std::sync::mpsc::{channel, Receiver, Sender};
@ -24,11 +24,15 @@ enum ThreadMessage {
}
#[derive(Debug)]
enum BackchannelMessage {
Exit,
enum NewPidMessage {
NewPid(PID),
}
#[derive(Debug)]
enum ExitMessage {
Exit,
}
thread_local!(static NETWORK_LISTEN_ADDRESS: RefCell<SocketAddr> = RefCell::new(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080)));
thread_local!(static SEND_ADDR: RefCell<Option<Sender<SocketAddr>>> = RefCell::new(None));
thread_local!(static PID1_KEY: RefCell<[u8; 16]> = RefCell::new([0u8; 16]));
@ -65,8 +69,6 @@ fn handle_connection(
loop {
let mut pkt = [0usize; 9];
let mut incoming_word = [0u8; size_of::<usize>()];
conn.set_nonblocking(false)
.expect("couldn't enable nonblocking mode");
conn.set_read_timeout(Some(Duration::from_millis(1000)))
.unwrap();
for word in pkt.iter_mut() {
@ -171,7 +173,8 @@ fn listen_thread(
listen_addr: SocketAddr,
chn: Sender<ThreadMessage>,
mut local_addr_sender: Option<Sender<SocketAddr>>,
backchannel: Receiver<BackchannelMessage>,
new_pid_channel: Receiver<NewPidMessage>,
exit_channel: Receiver<ExitMessage>
) {
let should_exit = std::sync::Arc::new(core::sync::atomic::AtomicBool::new(false));
@ -186,36 +189,16 @@ fn listen_thread(
let mut clients = vec![];
// let pid1_should_exit = should_exit.clone();
// let pid1_thread = spawn(move || {
// let mut client = TcpStream::connect(client_addr).expect("couldn't connect to xous server");
// client.set_read_timeout(Some(Duration::from_millis(100))).expect("couldn't set read timeout duration");
// let mut buffer = [0; size_of::<usize>() * 9];
// // println!("KERNEL(1): Started PID1 idle thread");
// loop {
// if pid1_should_exit.load(core::sync::atomic::Ordering::Relaxed) {
// return;
// }
// match client.read_exact(&mut buffer) {
// Err(e) if e.kind() == std::io::ErrorKind::WouldBlock || e.kind() == std::io::ErrorKind::TimedOut => {
// continue;
// }
// Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => return,
// Err(e) => panic!("KERNEL(1): Unable to read buffer: {}", e),
// Ok(()) => (), //println!("KERNEL(1): Read {} bytes", x),
// }
// }
// });
fn accept_new_connection(
mut conn: TcpStream,
chn: &Sender<ThreadMessage>,
backchannel: &Receiver<BackchannelMessage>,
new_pid_channel: &Receiver<NewPidMessage>,
clients: &mut Vec<(std::thread::JoinHandle<()>, TcpStream)>,
should_exit: &std::sync::Arc<core::sync::atomic::AtomicBool>,
) -> bool {
let thr_chn = chn.clone();
// Read the challenge access key from the client
let mut access_key = [0u8; 16];
conn.read_exact(&mut access_key).unwrap();
@ -226,13 +209,8 @@ fn listen_thread(
access_key,
))
.expect("couldn't request a new PID");
let new_pid = match backchannel
.recv()
.expect("couldn't receive message from main thread")
{
BackchannelMessage::NewPid(p) => p,
BackchannelMessage::Exit => return true,
};
let NewPidMessage::NewPid(new_pid) = new_pid_channel.recv()
.expect("couldn't receive message from main thread");
// println!("KERNEL({}): New client connected from {}", new_pid, _addr);
let conn_copy = conn.try_clone().expect("couldn't duplicate connection");
let should_exit = should_exit.clone();
@ -263,10 +241,11 @@ fn listen_thread(
// Use `listener` in a nonblocking setup so that we can exit when doing tests
enum ClientMessage {
NewConnection(TcpStream),
// BackChannel(BackchannelMessage),
Exit,
};
let (sender, receiver) = channel();
let tcp_sender = sender.clone();
let exit_sender = sender;
spawn(move || loop {
match listener.accept() {
Ok((conn, _addr)) => {
@ -278,31 +257,30 @@ fn listen_thread(
}
}
});
// spawn(move || loop {
// match backchannel.recv() {
// Err(e) => panic!(
// "KERNEL: got error when trying to receive quit timeout: {:?} ({})",
// e, e
// ),
// Ok(msg) => sender.send(ClientMessage::BackChannel(msg)).unwrap(),
// }
// });
spawn(move || loop {
match exit_channel.recv() {
Ok(ExitMessage::Exit) => {
exit_sender.send(ClientMessage::Exit).unwrap();
}
Err(e) => {
println!("error receiving exit command: {}", e);
return;
}
}
});
for msg in receiver {
match msg {
ClientMessage::NewConnection(conn) => {
if accept_new_connection(conn, &chn, &backchannel, &mut clients, &should_exit) {
if accept_new_connection(conn, &chn, &new_pid_channel, &mut clients, &should_exit) {
exit_server(should_exit, clients);
return;
}
},
ClientMessage::Exit => {
exit_server(should_exit, clients);
return;
}
// ClientMessage::BackChannel(BackchannelMessage::NewPid(x)) => {
// panic!("got unexpected message from main thread: new pid {}", x)
// }
// ClientMessage::BackChannel(BackchannelMessage::Exit) => {
// exit_server(should_exit, clients);
// return;
// }
}
}
}
@ -313,10 +291,11 @@ fn listen_thread(
pub fn idle() -> bool {
// Start listening.
let (sender, receiver) = channel();
let (backchannel_sender, backchannel_receiver) = channel();
let (new_pid_sender, new_pid_receiver) = channel();
let (exit_sender, exit_receiver) = channel();
// Allocate PID1 with the key we were passed.
let pid1_key = PID1_KEY.with(|p1k| p1k.borrow().clone());
let pid1_key = PID1_KEY.with(|p1k| *p1k.borrow());
let pid1_init = ProcessInit { key: pid1_key };
let new_pid = SystemServices::with_mut(|ss| ss.create_process(pid1_init)).unwrap();
assert_eq!(new_pid.get(), 1);
@ -332,7 +311,7 @@ pub fn idle() -> bool {
let listen_thread_handle = SEND_ADDR.with(|sa| {
let sa = sa.borrow_mut().take();
spawn(move || listen_thread(listen_addr, sender, sa, backchannel_receiver))
spawn(move || listen_thread(listen_addr, sender, sa, new_pid_receiver, exit_receiver))
});
while let Ok(msg) = receiver.recv() {
@ -340,27 +319,16 @@ pub fn idle() -> bool {
ThreadMessage::NewConnection(conn, access_key) => {
// The new process should already have a PID registered. Convert its access key
// into a PID, and register the connection with the server.
println!("KERNEL: Attempting to look up access key {:?}", access_key);
let new_pid =
crate::arch::process::register_connection_for_key(conn, access_key).unwrap();
println!(
"KERNEL: Access key {:?} mapped to PID {}",
access_key, new_pid
);
// // On the initial connection, set the parent PID to 1. In the future
// // there may be some sort of security token check here.
// crate::arch::process::set_current_pid(PID::new(1).unwrap());
// // Spawn a new process inside the kernel. This will assign us a PID.
// let new_pid = SystemServices::with_mut(|ss| {
// ss.create_process(process::ProcessInit::new(conn.try_clone().unwrap()), ())
// })
// .unwrap();
// println!(
// "KERNEL: Access key {:?} mapped to PID {}",
// access_key, new_pid
// );
// Inform the backchannel of the new process ID.
backchannel_sender
.send(BackchannelMessage::NewPid(new_pid))
new_pid_sender
.send(NewPidMessage::NewPid(new_pid))
.expect("couldn't send new pid to new connection");
// conn.write_all(&new_pid.get().to_le_bytes())
@ -380,13 +348,12 @@ pub fn idle() -> bool {
// because we won't be able to send a response.
let is_terminate = call == SysCall::TerminateProcess;
let is_shutdown = call == SysCall::Shutdown;
let is_shutdown = call == SysCall::Shutdown;
// For a "Shutdown" command, send the response before we issue the shutdown.
// This is because the "process" will be "terminated" (the network socket will be closed),
// and we won't be able to send the response after we're done.
if is_shutdown {
// println!("KERNEL: Detected shutdown -- sending final \"Ok\" to the client");
println!("KERNEL: Detected shutdown -- sending final \"Ok\" to the client");
let mut process = Process::current();
let mut response_vec = Vec::new();
response_vec.extend_from_slice(&thread_id.to_le_bytes());
@ -395,10 +362,10 @@ pub fn idle() -> bool {
}
process.send(&response_vec).unwrap_or_else(|_e| {
// If we're unable to send data to the process, assume it's dead and terminate it.
// println!("Unable to send response to process: {:?} -- terminating", _e);
println!("Unable to send response to process: {:?} -- terminating", _e);
crate::syscall::handle(pid, thread_id, SysCall::TerminateProcess).ok();
});
// println!("KERNEL: Done sending");
println!("KERNEL: Done sending");
}
// Handle the syscall within the Xous kernel
@ -436,8 +403,8 @@ pub fn idle() -> bool {
}
if is_shutdown {
backchannel_sender
.send(BackchannelMessage::Exit)
exit_sender
.send(ExitMessage::Exit)
.expect("couldn't send shutdown signal");
break;
}
@ -445,11 +412,11 @@ pub fn idle() -> bool {
}
}
// println!("Exiting Xous because the listen thread channel has closed. Waiting for thread to finish...");
println!("Exiting Xous because the listen thread channel has closed. Waiting for thread to finish...");
listen_thread_handle
.join()
.expect("error waiting for listen thread to return");
// println!("Thank you for using Xous!");
println!("Thank you for using Xous!");
false
}

View file

@ -657,10 +657,13 @@ impl SystemServices {
ProcessState::Running(x)
}
}
other => panic!(
"PID {} TID {} was not in a state to be switched from: {:?}",
pid, tid, other
),
other => {
// ::debug_here::debug_here!();
panic!(
"PID {} TID {} was not in a state to be switched from: {:?}",
pid, tid, other
);
},
};
// println!(
// "switch_from_thread({}:{}): New state is {:?}",
@ -1207,6 +1210,10 @@ impl SystemServices {
// Queue the thread to run
process.state = match process.state {
ProcessState::Running(x) => ProcessState::Running(x | (1 << new_tid)),
// This is the initial thread in this process -- schedule it to be run.
ProcessState::Allocated => ProcessState::Ready(1 << new_tid),
other => panic!(
"error spawning thread: process was in an invalid state {:?}",
other

View file

@ -509,7 +509,7 @@ pub fn handle_inner(pid: PID, tid: TID, call: SysCall) -> SysCallResult {
}),
SysCall::CreateProcess(process_init) => SystemServices::with_mut(|ss| {
ss.create_process(process_init)
.map(|new_pid| xous::Result::ProcessID(new_pid))
.map(xous::Result::ProcessID)
}),
SysCall::CreateServer(name) => {
SystemServices::with_mut(|ss| ss.create_server(pid, name).map(xous::Result::ServerID))

View file

@ -25,6 +25,14 @@ fn start_kernel(server_spec: &str) -> JoinHandle<()> {
"XOUS_SERVER environment variable must be unset to run tests"
);
use rand::{Rng, thread_rng};
let mut pid1_key = [0u8; 16];
let mut rng = thread_rng();
for b in pid1_key.iter_mut() {
*b = rng.gen();
}
xous::arch::set_process_key(&pid1_key);
let server_addr = server_spec
.to_socket_addrs()
.expect("invalid server address")
@ -37,9 +45,11 @@ fn start_kernel(server_spec: &str) -> JoinHandle<()> {
let (send_addr, recv_addr) = channel();
// Launch the main thread
// Launch the main thread. We pass a `send_addr` channel so that the
// server can notify us when it's ready to listen.
let main_thread = std::thread::spawn(move || {
let server_spec_server = server_addr;
crate::arch::set_pid1_key(pid1_key);
crate::arch::set_send_addr(send_addr);
crate::arch::set_listen_address(&server_spec_server);
kmain()
@ -80,18 +90,21 @@ fn start_kernel(server_spec: &str) -> JoinHandle<()> {
// })
// }
// #[test]
// fn shutdown() {
// // Start the server in another thread.
// let main_thread = start_kernel(SERVER_SPEC);
#[test]
fn shutdown() {
// Start the server in another thread.
let main_thread = start_kernel(SERVER_SPEC);
// // Send a raw `Shutdown` message to terminate the kernel.
// let call_result = rsyscall(SysCall::Shutdown);
// println!("Call result: {:?}", call_result);
// Send a raw `Shutdown` message to terminate the kernel.
xous::create_process(xous::ProcessArgs::new(|| {
println!("Running in new process...");
let call_result = rsyscall(SysCall::Shutdown);
println!("Call result: {:?}", call_result);
})).unwrap();
// // Wait for the kernel to exit.
// main_thread.join().expect("couldn't join main thread");
// }
// Wait for the kernel to exit.
main_thread.join().expect("couldn't join main thread");
}
// #[test]
// fn send_scalar_message() {
@ -432,11 +445,10 @@ fn server_client_same_process() {
let msg = xous::receive_message(server).expect("couldn't receive message");
assert_eq!(msg.message, xous::Message::Scalar(msg_contents));
})).expect("couldn't start server");
}))
.expect("couldn't start server");
xous::wait_process(internal_server)
.expect("couldn't join internal_server process");
xous::wait_process(internal_server).expect("couldn't join internal_server process");
// Any process ought to be able to shut down the system currently.
rsyscall(SysCall::Shutdown).expect("unable to shutdown server");

View file

@ -9,7 +9,8 @@ use std::thread_local;
use crate::{Result, PID, TID};
pub type ThreadInit = ();
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ThreadInit {}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct ProcessInit {
@ -65,7 +66,7 @@ pub fn args_to_thread(
_a6: usize,
_a7: usize,
) -> core::result::Result<ThreadInit, crate::Error> {
Ok(())
Ok(ThreadInit {})
}
pub fn args_to_process(
@ -84,15 +85,14 @@ pub fn args_to_process(
v.extend_from_slice(&(a4 as u32).to_le_bytes());
let mut key = [0u8; 16];
key.copy_from_slice(&v);
Ok(ProcessInit {
key
})
Ok(ProcessInit { key })
}
thread_local!(static NETWORK_CONNECT_ADDRESS: RefCell<Option<SocketAddr>> = RefCell::new(None));
thread_local!(static XOUS_SERVER_CONNECTION: RefCell<Option<ServerConnection>> = RefCell::new(None));
thread_local!(static THREAD_ID: RefCell<TID> = RefCell::new(1));
thread_local!(static PROCESS_ID: RefCell<PID> = RefCell::new(PID::new(1).unwrap()));
thread_local!(static PROCESS_KEY: RefCell<[u8; 16]> = RefCell::new([0u8; 16]));
fn default_xous_address() -> SocketAddr {
std::env::var("XOUS_SERVER")
@ -105,6 +105,10 @@ fn default_xous_address() -> SocketAddr {
.unwrap_or_else(|_| SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0))
}
pub fn set_process_key(new_key: &[u8; 16]) {
PROCESS_KEY.with(|pk| *pk.borrow_mut() = *new_key);
}
/// Set the network address for this particular thread.
pub fn set_xous_address(new_address: SocketAddr) {
NETWORK_CONNECT_ADDRESS.with(|nca| {
@ -141,7 +145,7 @@ where
F: Send + 'static,
T: Send + 'static,
{
Ok(())
Ok(ThreadInit {})
}
pub fn create_thread_post<F, T>(
@ -186,14 +190,14 @@ where
F: FnOnce(),
{
// Ensure there is a connection, because after this function returns
// we'll make a syscall with CreateProcess(). This should only happen
// for PID1.
// we'll make a syscall with CreateProcess(). This should only need
// to happen for PID1.
XOUS_SERVER_CONNECTION.with(|xsc| {
let mut xsc = xsc.borrow_mut();
if xsc.is_none() {
NETWORK_CONNECT_ADDRESS.with(|nca| {
let addr = nca.borrow().unwrap_or_else(default_xous_address);
let pid1_key = [0u8; 16];
let pid1_key = PROCESS_KEY.with(|pk| *pk.borrow());
match xous_connect_impl(addr, &pid1_key) {
Ok(a) => {
*xsc = Some(a);
@ -206,9 +210,7 @@ where
Ok(())
}
})?;
Ok(ProcessInit {
key: [1; 16],
})
Ok(ProcessInit { key: PROCESS_KEY.with(|pk| *pk.borrow()) })
}
pub fn create_process_post<F>(
@ -219,17 +221,29 @@ pub fn create_process_post<F>(
where
F: FnOnce() + Send + 'static,
{
let server_spec = xous_address();
let server_address = xous_address();
Ok(std::thread::Builder::new()
let thread_main = std::thread::Builder::new()
.spawn(move || {
set_xous_address(server_spec);
xous_connect_impl(server_spec, &init.key).unwrap();
set_xous_address(server_address);
THREAD_ID.with(|tid| *tid.borrow_mut() = 1);
PROCESS_ID.with(|p| *p.borrow_mut() = pid);
(args.main.take().unwrap())();
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(args.main.take().unwrap())
})
.map(ProcessHandle)
.map_err(|_| crate::Error::InternalError)?)
.map_err(|_| crate::Error::InternalError)?.join().unwrap().unwrap();
Ok(ProcessHandle(thread_main.0))
}
pub fn wait_process(joiner: ProcessHandle) -> crate::SysCallResult {
@ -240,12 +254,14 @@ pub fn wait_process(joiner: ProcessHandle) -> crate::SysCallResult {
.map_err(|_| crate::Error::InternalError)
}
fn xous_connect_impl(addr: SocketAddr, key: &[u8; 16]) -> core::result::Result<ServerConnection, ()> {
// println!("Opening connection to Xous server @ {}...", addr);
fn xous_connect_impl(
addr: SocketAddr,
key: &[u8; 16],
) -> core::result::Result<ServerConnection, ()> {
eprintln!("Opening connection to Xous server @ {} with key {:?}...", addr, key);
assert_ne!(key, &[0u8; 16]);
match TcpStream::connect(addr) {
Ok(mut conn) => {
// let mut pid = [0u8; 1];
// conn.read_exact(&mut pid).unwrap();
conn.write_all(key).unwrap(); // Send key to authenticate us as PID 1
Ok(ServerConnection {
send: Arc::new(Mutex::new(conn.try_clone().unwrap())),
@ -294,12 +310,7 @@ pub fn _xous_syscall(
&call,
&mut xsc_asmut.send.lock().unwrap(),
);
_xous_syscall_result(
&call,
ret,
*tid.borrow(),
xsc_asmut,
);
_xous_syscall_result(&call, ret, *tid.borrow(), xsc_asmut);
})
});
}
@ -363,7 +374,7 @@ fn _xous_syscall_result(
let response = Result::from_args(pkt);
// println!(" Response: {:?}", response);
println!(" Response: {:?}", response);
if Result::BlockedProcess == response {
// println!(" Waiting again");
continue;
@ -444,10 +455,10 @@ fn _xous_syscall_to(
call: &crate::SysCall,
xsc: &mut TcpStream,
) {
// println!(
// "Making Syscall: {:?}",
// crate::SysCall::from_args(nr, a1, a2, a3, a4, a5, a6, a7).unwrap()
// );
println!(
"Making Syscall: {:?}",
crate::SysCall::from_args(nr, a1, a2, a3, a4, a5, a6, a7).unwrap()
);
// Send the packet to the server
let mut pkt = vec![];

View file

@ -819,8 +819,8 @@ where
F: Send + 'static,
T: Send + 'static,
{
let ctx_info = crate::arch::create_thread_pre(&f)?;
rsyscall(SysCall::CreateThread(ctx_info)).and_then(|result| {
let thread_info = crate::arch::create_thread_pre(&f)?;
rsyscall(SysCall::CreateThread(thread_info)).and_then(|result| {
if let Result::ThreadID(thread_id) = result {
crate::arch::create_thread_post(f, thread_id)
} else {