From 9361055aecb9405e34946288abaa6bda650004a9 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 18 Jun 2020 11:39:46 +0800 Subject: [PATCH] kernel: work-in-progress for process termination Processes can go away. This is initial work to support process termination. Signed-off-by: Sean Cross --- kernel/src/arch/hosted/mod.rs | 49 ++++++++++++++++++++----------- kernel/src/arch/hosted/process.rs | 5 ++++ xous-rs/src/definitions.rs | 2 +- xous-rs/src/syscall.rs | 24 ++++++++++----- 4 files changed, 55 insertions(+), 25 deletions(-) diff --git a/kernel/src/arch/hosted/mod.rs b/kernel/src/arch/hosted/mod.rs index 1d838cef7..b54d1756c 100644 --- a/kernel/src/arch/hosted/mod.rs +++ b/kernel/src/arch/hosted/mod.rs @@ -4,22 +4,31 @@ pub mod process; pub mod syscall; use std::env; -use std::io::{Read, Write}; +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::thread::{spawn}; +use std::thread::spawn; use xous::{Result, SysCall, PID}; +use crate::arch::process::ProcessHandle; use crate::services::SystemServicesHandle; -fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender<(TcpStream, PID, SysCall)>) { +fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender<(PID, SysCall)>) { loop { let mut pkt = [0usize; 8]; let mut incoming_word = [0u8; size_of::()]; for word in pkt.iter_mut() { - conn.read_exact(&mut incoming_word).expect("Disconnection"); + if let Err(e) = conn.read_exact(&mut incoming_word) { + println!( + "Client {} disconnected. Shutting down virtual process.", + pid + ); + let call = xous::SysCall::TerminateProcess; + chn.send((pid, call)).unwrap(); + return; + } *word = usize::from_le_bytes(incoming_word); } let call = xous::SysCall::from_args( @@ -32,17 +41,13 @@ fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender<(TcpStream, PID, "Received packet: {:08x} {} {} {} {} {} {} {}: {:?}", pkt[0], pkt[1], pkt[2], pkt[3], pkt[4], pkt[5], pkt[6], pkt[7], call ); - chn.send(( - conn.try_clone().expect("couldn't clone connection"), - pid, - call, - )).expect("couldn't make syscall"); + chn.send((pid, call)).expect("couldn't make syscall"); } } } } -fn listen_thread(chn: Sender<(TcpStream, PID, SysCall)>) { +fn listen_thread(chn: Sender<(PID, SysCall)>) { 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| { @@ -56,7 +61,8 @@ fn listen_thread(chn: Sender<(TcpStream, PID, SysCall)>) { let new_pid = { let mut ss = SystemServicesHandle::get(); - ss.spawn_process(process::ProcessInit::new(conn.try_clone().unwrap()), ()).unwrap() + 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)); @@ -71,23 +77,32 @@ pub fn idle() { let (sender, receiver) = channel(); let listen_thread_handle = spawn(move || listen_thread(sender)); - while let Ok((mut conn, pid, call)) = receiver.recv() { + while let Ok((pid, call)) = receiver.recv() { { let mut ss = SystemServicesHandle::get(); ss.switch_to(pid, Some(1)).unwrap(); } + let is_terminate = call == SysCall::TerminateProcess; let response = crate::syscall::handle(pid, call).unwrap_or_else(Result::Error); - if response != Result::BlockedProcess { + + if response != Result::BlockedProcess && !is_terminate { + { + let mut processes = ProcessHandle::get(); + let mut response_vec = Vec::new(); + for word in response.to_args().iter_mut() { + response_vec.extend_from_slice(&word.to_le_bytes()); + } + processes.send(&response_vec); + } let mut ss = SystemServicesHandle::get(); ss.switch_from(pid, 1, true).unwrap(); } - for word in response.to_args().iter_mut() { - conn.write_all(&word.to_le_bytes()).expect("Disconnection"); - } } eprintln!("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"); + listen_thread_handle + .join() + .expect("error waiting for listen thread to return"); eprintln!("Thank you for using Xous!"); exit(0); diff --git a/kernel/src/arch/hosted/process.rs b/kernel/src/arch/hosted/process.rs index f09112403..a16e01495 100644 --- a/kernel/src/arch/hosted/process.rs +++ b/kernel/src/arch/hosted/process.rs @@ -132,6 +132,11 @@ impl Process { pid } + + pub fn send(&mut self, bytes: &[u8]) -> Result<(), xous::Error> { + self.conn.write_all(bytes).unwrap(); + Ok(()) + } } impl Context {} diff --git a/xous-rs/src/definitions.rs b/xous-rs/src/definitions.rs index e3e3fe095..48d02bde8 100644 --- a/xous-rs/src/definitions.rs +++ b/xous-rs/src/definitions.rs @@ -308,7 +308,7 @@ impl MemoryRange { } /// Which memory region the operation should affect. -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, PartialEq)] pub enum MemoryType { /// The address where addresses go when no `virt` is specified. Default = 1, diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index a4a02fff9..621cdf33d 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -5,7 +5,7 @@ use crate::{ use num_derive::FromPrimitive; use num_traits::FromPrimitive; -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum SysCall { /// Allocates pages of memory, equal to a total of `size` bytes. A physical /// address may be specified, which can be used to allocate regions such as @@ -139,8 +139,8 @@ pub enum SysCall { /// system /// * **InterruptInUse**: The specified interrupt has already been claimed ClaimInterrupt( - usize, /* IRQ number */ - MemoryAddress, /* function pointer */ + usize, /* IRQ number */ + MemoryAddress, /* function pointer */ Option, /* argument */ ), @@ -244,6 +244,13 @@ pub enum SysCall { Option, /* argument */ ), + /// Create a new process, setting the current process as the parent ID. + /// Does not start the process immediately. + CreateProcess, + + /// Terminate the current process, closing all server connections. + TerminateProcess, + /// This syscall does not exist. It captures all possible /// arguments so detailed analysis can be performed. Invalid(usize, usize, usize, usize, usize, usize, usize), @@ -270,6 +277,8 @@ enum SysCallNumber { SpawnThread = 18, UnmapMemory = 19, ReturnMemory = 20, + CreateProcess = 21, + TerminateProcess = 22, Invalid, } @@ -474,6 +483,8 @@ impl SysCall { 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::Invalid(a1, a2, a3, a4, a5, a6, a7) => [ SysCallNumber::Invalid as usize, *a1, @@ -585,6 +596,8 @@ impl SysCall { MemoryAddress::new(a2).ok_or(Error::InvalidSyscall)?, MemoryAddress::new(a3), ), + Some(SysCallNumber::CreateProcess) => SysCall::CreateProcess, + Some(SysCallNumber::TerminateProcess) => SysCall::TerminateProcess, Some(SysCallNumber::Invalid) => SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7), None => return Err(Error::InvalidSyscall), }) @@ -748,10 +761,7 @@ pub fn receive_message(server: SID) -> core::result::Result core::result::Result<(), Error> { +pub fn send_message(connection: CID, message: Message) -> core::result::Result<(), Error> { let result = rsyscall(SysCall::SendMessage(connection, message)).expect("couldn't send message"); if let Result::Ok = result {