From b1a41978baccf8ba00ae466aeba46c17842f0aa9 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Tue, 23 Jun 2020 13:58:15 +0800 Subject: [PATCH] shutdown: add shutdown syscall This will be used during testing to shutdown the kernel once it's finished. Signed-off-by: Sean Cross --- kernel/src/arch/hosted/mod.rs | 78 ++++++++++++++++++++++++++--------- kernel/src/main.rs | 5 ++- kernel/src/test.rs | 3 -- xous-rs/src/syscall.rs | 6 +++ 4 files changed, 69 insertions(+), 23 deletions(-) delete mode 100644 kernel/src/test.rs diff --git a/kernel/src/arch/hosted/mod.rs b/kernel/src/arch/hosted/mod.rs index d4a34199d..0fa432494 100644 --- a/kernel/src/arch/hosted/mod.rs +++ b/kernel/src/arch/hosted/mod.rs @@ -7,14 +7,16 @@ use std::env; use std::io::Read; use std::mem::size_of; use std::net::{TcpListener, TcpStream}; -use std::process::exit; -use std::sync::mpsc::{channel, Sender}; +use std::sync::mpsc::{channel, Sender, Receiver}; use std::thread::spawn; +use std::time::Duration; + use xous::{Result, SysCall, PID}; use crate::arch::process::ProcessHandle; 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)>) { loop { 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()); println!("Starting Xous server on {}...", listen_addr); let listener = TcpListener::bind(listen_addr).unwrap_or_else(|e| { - eprintln!("Unable to create server: {}", e); - exit(1); + panic!("Unable to create server: {}", e); }); - 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 = { - 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)); + // Use `listener` in a nonblocking setup so that we can exit when doing tests + listener.set_nonblocking(true).expect("couldn't set TcpListener to nonblocking"); + loop { + match listener.accept() { + Ok((conn, addr)) => { + println!("New client connected from {}", addr); + 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 /// that kmain can activate. In a hosted environment,this is the primary /// thread that handles network communications, and this function never returns. -pub fn idle() { +pub fn idle() -> bool { // Start listening. 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() { { let mut ss = SystemServicesHandle::get(); 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; + 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); + // 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 { { let mut processes = ProcessHandle::get(); @@ -109,5 +149,5 @@ pub fn idle() { .expect("error waiting for listen thread to return"); eprintln!("Thank you for using Xous!"); - exit(0); + false } diff --git a/kernel/src/main.rs b/kernel/src/main.rs index d8fbaca47..a33271683 100644 --- a/kernel/src/main.rs +++ b/kernel/src/main.rs @@ -143,7 +143,10 @@ pub extern "C" fn kmain() { } None => { 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; + } } } } diff --git a/kernel/src/test.rs b/kernel/src/test.rs deleted file mode 100644 index 37536464b..000000000 --- a/kernel/src/test.rs +++ /dev/null @@ -1,3 +0,0 @@ -#[test] -fn sanity_check() { -} diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index 621cdf33d..294dd1851 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -251,6 +251,9 @@ pub enum SysCall { /// Terminate the current process, closing all server connections. TerminateProcess, + /// Shut down the entire system + Shutdown, + /// This syscall does not exist. It captures all possible /// arguments so detailed analysis can be performed. Invalid(usize, usize, usize, usize, usize, usize, usize), @@ -279,6 +282,7 @@ enum SysCallNumber { ReturnMemory = 20, CreateProcess = 21, TerminateProcess = 22, + Shutdown = 23, Invalid, } @@ -485,6 +489,7 @@ impl SysCall { ], 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::Shutdown => [SysCallNumber::Shutdown as usize, 0, 0, 0, 0, 0, 0, 0], SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7) => [ SysCallNumber::Invalid as usize, *a1, @@ -598,6 +603,7 @@ impl SysCall { ), Some(SysCallNumber::CreateProcess) => SysCall::CreateProcess, Some(SysCallNumber::TerminateProcess) => SysCall::TerminateProcess, + Some(SysCallNumber::Shutdown) => SysCall::Shutdown, Some(SysCallNumber::Invalid) => SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7), None => return Err(Error::InvalidSyscall), })