shutdown: add shutdown syscall

This will be used during testing to shutdown the kernel once it's
finished.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-06-23 13:58:15 +08:00
parent 14bb1ac1e5
commit b1a41978ba
4 changed files with 69 additions and 23 deletions

View file

@ -7,14 +7,16 @@ use std::env;
use std::io::Read; use std::io::Read;
use std::mem::size_of; use std::mem::size_of;
use std::net::{TcpListener, TcpStream}; use std::net::{TcpListener, TcpStream};
use std::process::exit; use std::sync::mpsc::{channel, Sender, Receiver};
use std::sync::mpsc::{channel, Sender};
use std::thread::spawn; use std::thread::spawn;
use std::time::Duration;
use xous::{Result, SysCall, PID}; use xous::{Result, SysCall, PID};
use crate::arch::process::ProcessHandle; use crate::arch::process::ProcessHandle;
use crate::services::SystemServicesHandle; use crate::services::SystemServicesHandle;
/// Each client gets its own connection and its own thread, which is handled here.
fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender<(PID, SysCall)>) { fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender<(PID, SysCall)>) {
loop { loop {
let mut pkt = [0usize; 8]; let mut pkt = [0usize; 8];
@ -47,44 +49,82 @@ fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender<(PID, SysCall)>)
} }
} }
fn listen_thread(chn: Sender<(PID, SysCall)>) { fn listen_thread(chn: Sender<(PID, SysCall)>, quit: Receiver<()>) {
let listen_addr = env::var("XOUS_LISTEN_ADDR").unwrap_or_else(|_| "localhost:9687".to_owned()); let listen_addr = env::var("XOUS_LISTEN_ADDR").unwrap_or_else(|_| "localhost:9687".to_owned());
println!("Starting Xous server on {}...", listen_addr); println!("Starting Xous server on {}...", listen_addr);
let listener = TcpListener::bind(listen_addr).unwrap_or_else(|e| { let listener = TcpListener::bind(listen_addr).unwrap_or_else(|e| {
eprintln!("Unable to create server: {}", e); panic!("Unable to create server: {}", e);
exit(1);
}); });
loop {
let (conn, addr) = listener.accept().expect("Unable to accept connection");
println!("New client connected from {}", addr);
let thr_chn = chn.clone();
let new_pid = { // Use `listener` in a nonblocking setup so that we can exit when doing tests
let mut ss = SystemServicesHandle::get(); listener.set_nonblocking(true).expect("couldn't set TcpListener to nonblocking");
ss.spawn_process(process::ProcessInit::new(conn.try_clone().unwrap()), ()) loop {
.unwrap() match listener.accept() {
}; Ok((conn, addr)) => {
println!("Assigned PID {}", new_pid); println!("New client connected from {}", addr);
spawn(move || handle_connection(conn, new_pid, thr_chn)); let thr_chn = chn.clone();
let new_pid = {
let mut ss = SystemServicesHandle::get();
ss.spawn_process(process::ProcessInit::new(conn.try_clone().unwrap()), ())
.unwrap()
};
println!("Assigned PID {}", new_pid);
spawn(move || handle_connection(conn, new_pid, thr_chn));
}
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
match quit.recv_timeout(Duration::from_millis(10)) {
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
continue;
},
_ => {
return;
}
}
},
Err(e) => {
eprintln!("error accepting connections: {}", e);
return;
}
}
} }
} }
/// The idle function is run when there are no directly-runnable processes /// The idle function is run when there are no directly-runnable processes
/// that kmain can activate. In a hosted environment,this is the primary /// that kmain can activate. In a hosted environment,this is the primary
/// thread that handles network communications, and this function never returns. /// thread that handles network communications, and this function never returns.
pub fn idle() { pub fn idle() -> bool {
// Start listening. // Start listening.
let (sender, receiver) = channel(); let (sender, receiver) = channel();
let listen_thread_handle = spawn(move || listen_thread(sender)); let (term_sender, term_receiver) = channel();
let listen_thread_handle = spawn(move || listen_thread(sender, term_receiver));
// // Spawn initial programs
// let all_args = env::args();
// all_args.next();
// for i in all_args {
// let listen_addr = env::var("XOUS_LISTEN_ADDR").unwrap_or_else(|_| "localhost:9687".to_owned();
// let cmd = Command::new(i).env("XOUS_LISTEN_ADDR", listen_addr)
// }
while let Ok((pid, call)) = receiver.recv() { while let Ok((pid, call)) = receiver.recv() {
{ {
let mut ss = SystemServicesHandle::get(); let mut ss = SystemServicesHandle::get();
ss.switch_to(pid, Some(1)).unwrap(); ss.switch_to(pid, Some(1)).unwrap();
} }
// If the call being made is to terminate the current process, we need to know
// because we won't be able to send a response.
let is_terminate = call == SysCall::TerminateProcess; let is_terminate = call == SysCall::TerminateProcess;
if call == SysCall::Shutdown {
term_sender.send(()).expect("unable to send shutdown message");
}
// Handle the syscall within the Xous kernel
let response = crate::syscall::handle(pid, call).unwrap_or_else(Result::Error); let response = crate::syscall::handle(pid, call).unwrap_or_else(Result::Error);
// There's a response if it wasn't a blocked process and we're not terminating.
// Send the response back to the target.
if response != Result::BlockedProcess && !is_terminate { if response != Result::BlockedProcess && !is_terminate {
{ {
let mut processes = ProcessHandle::get(); let mut processes = ProcessHandle::get();
@ -109,5 +149,5 @@ pub fn idle() {
.expect("error waiting for listen thread to return"); .expect("error waiting for listen thread to return");
eprintln!("Thank you for using Xous!"); eprintln!("Thank you for using Xous!");
exit(0); false
} }

View file

@ -143,7 +143,10 @@ pub extern "C" fn kmain() {
} }
None => { None => {
println!("No runnable tasks found. Entering idle state..."); println!("No runnable tasks found. Entering idle state...");
arch::idle(); // Special case for testing: idle can return `false` to indicate exit
if ! arch::idle() {
return;
}
} }
} }
} }

View file

@ -1,3 +0,0 @@
#[test]
fn sanity_check() {
}

View file

@ -251,6 +251,9 @@ pub enum SysCall {
/// Terminate the current process, closing all server connections. /// Terminate the current process, closing all server connections.
TerminateProcess, TerminateProcess,
/// Shut down the entire system
Shutdown,
/// This syscall does not exist. It captures all possible /// This syscall does not exist. It captures all possible
/// arguments so detailed analysis can be performed. /// arguments so detailed analysis can be performed.
Invalid(usize, usize, usize, usize, usize, usize, usize), Invalid(usize, usize, usize, usize, usize, usize, usize),
@ -279,6 +282,7 @@ enum SysCallNumber {
ReturnMemory = 20, ReturnMemory = 20,
CreateProcess = 21, CreateProcess = 21,
TerminateProcess = 22, TerminateProcess = 22,
Shutdown = 23,
Invalid, Invalid,
} }
@ -485,6 +489,7 @@ impl SysCall {
], ],
SysCall::CreateProcess => [SysCallNumber::CreateProcess as usize, 0, 0, 0, 0, 0, 0, 0], SysCall::CreateProcess => [SysCallNumber::CreateProcess as usize, 0, 0, 0, 0, 0, 0, 0],
SysCall::TerminateProcess => [SysCallNumber::TerminateProcess as usize, 0, 0, 0, 0, 0, 0, 0], SysCall::TerminateProcess => [SysCallNumber::TerminateProcess as usize, 0, 0, 0, 0, 0, 0, 0],
SysCall::Shutdown => [SysCallNumber::Shutdown as usize, 0, 0, 0, 0, 0, 0, 0],
SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7) => [ SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7) => [
SysCallNumber::Invalid as usize, SysCallNumber::Invalid as usize,
*a1, *a1,
@ -598,6 +603,7 @@ impl SysCall {
), ),
Some(SysCallNumber::CreateProcess) => SysCall::CreateProcess, Some(SysCallNumber::CreateProcess) => SysCall::CreateProcess,
Some(SysCallNumber::TerminateProcess) => SysCall::TerminateProcess, Some(SysCallNumber::TerminateProcess) => SysCall::TerminateProcess,
Some(SysCallNumber::Shutdown) => SysCall::Shutdown,
Some(SysCallNumber::Invalid) => SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7), Some(SysCallNumber::Invalid) => SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7),
None => return Err(Error::InvalidSyscall), None => return Err(Error::InvalidSyscall),
}) })