mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-21 09:14:34 +00:00
kernel: work-in-progress for process termination
Processes can go away. This is initial work to support process termination. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
2786ad697b
commit
9361055aec
4 changed files with 55 additions and 25 deletions
|
|
@ -4,22 +4,31 @@ pub mod process;
|
||||||
pub mod syscall;
|
pub mod syscall;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::io::{Read, Write};
|
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::process::exit;
|
||||||
use std::sync::mpsc::{channel, Sender};
|
use std::sync::mpsc::{channel, Sender};
|
||||||
use std::thread::{spawn};
|
use std::thread::spawn;
|
||||||
use xous::{Result, SysCall, PID};
|
use xous::{Result, SysCall, PID};
|
||||||
|
|
||||||
|
use crate::arch::process::ProcessHandle;
|
||||||
use crate::services::SystemServicesHandle;
|
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 {
|
loop {
|
||||||
let mut pkt = [0usize; 8];
|
let mut pkt = [0usize; 8];
|
||||||
let mut incoming_word = [0u8; size_of::<usize>()];
|
let mut incoming_word = [0u8; size_of::<usize>()];
|
||||||
for word in pkt.iter_mut() {
|
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);
|
*word = usize::from_le_bytes(incoming_word);
|
||||||
}
|
}
|
||||||
let call = xous::SysCall::from_args(
|
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} {} {} {} {} {} {} {}: {:?}",
|
"Received packet: {:08x} {} {} {} {} {} {} {}: {:?}",
|
||||||
pkt[0], pkt[1], pkt[2], pkt[3], pkt[4], pkt[5], pkt[6], pkt[7], call
|
pkt[0], pkt[1], pkt[2], pkt[3], pkt[4], pkt[5], pkt[6], pkt[7], call
|
||||||
);
|
);
|
||||||
chn.send((
|
chn.send((pid, call)).expect("couldn't make syscall");
|
||||||
conn.try_clone().expect("couldn't clone connection"),
|
|
||||||
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());
|
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| {
|
||||||
|
|
@ -56,7 +61,8 @@ fn listen_thread(chn: Sender<(TcpStream, PID, SysCall)>) {
|
||||||
|
|
||||||
let new_pid = {
|
let new_pid = {
|
||||||
let mut ss = SystemServicesHandle::get();
|
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);
|
println!("Assigned PID {}", new_pid);
|
||||||
spawn(move || handle_connection(conn, new_pid, thr_chn));
|
spawn(move || handle_connection(conn, new_pid, thr_chn));
|
||||||
|
|
@ -71,23 +77,32 @@ pub fn idle() {
|
||||||
let (sender, receiver) = channel();
|
let (sender, receiver) = channel();
|
||||||
let listen_thread_handle = spawn(move || listen_thread(sender));
|
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();
|
let mut ss = SystemServicesHandle::get();
|
||||||
ss.switch_to(pid, Some(1)).unwrap();
|
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);
|
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();
|
let mut ss = SystemServicesHandle::get();
|
||||||
ss.switch_from(pid, 1, true).unwrap();
|
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...");
|
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!");
|
eprintln!("Thank you for using Xous!");
|
||||||
exit(0);
|
exit(0);
|
||||||
|
|
|
||||||
|
|
@ -132,6 +132,11 @@ impl Process {
|
||||||
|
|
||||||
pid
|
pid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn send(&mut self, bytes: &[u8]) -> Result<(), xous::Error> {
|
||||||
|
self.conn.write_all(bytes).unwrap();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Context {}
|
impl Context {}
|
||||||
|
|
|
||||||
|
|
@ -308,7 +308,7 @@ impl MemoryRange {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Which memory region the operation should affect.
|
/// Which memory region the operation should affect.
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||||
pub enum MemoryType {
|
pub enum MemoryType {
|
||||||
/// The address where addresses go when no `virt` is specified.
|
/// The address where addresses go when no `virt` is specified.
|
||||||
Default = 1,
|
Default = 1,
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ use crate::{
|
||||||
use num_derive::FromPrimitive;
|
use num_derive::FromPrimitive;
|
||||||
use num_traits::FromPrimitive;
|
use num_traits::FromPrimitive;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum SysCall {
|
pub enum SysCall {
|
||||||
/// Allocates pages of memory, equal to a total of `size` bytes. A physical
|
/// 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
|
/// address may be specified, which can be used to allocate regions such as
|
||||||
|
|
@ -244,6 +244,13 @@ pub enum SysCall {
|
||||||
Option<MemoryAddress>, /* argument */
|
Option<MemoryAddress>, /* 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
|
/// 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),
|
||||||
|
|
@ -270,6 +277,8 @@ enum SysCallNumber {
|
||||||
SpawnThread = 18,
|
SpawnThread = 18,
|
||||||
UnmapMemory = 19,
|
UnmapMemory = 19,
|
||||||
ReturnMemory = 20,
|
ReturnMemory = 20,
|
||||||
|
CreateProcess = 21,
|
||||||
|
TerminateProcess = 22,
|
||||||
Invalid,
|
Invalid,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -474,6 +483,8 @@ impl SysCall {
|
||||||
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::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,
|
||||||
|
|
@ -585,6 +596,8 @@ impl SysCall {
|
||||||
MemoryAddress::new(a2).ok_or(Error::InvalidSyscall)?,
|
MemoryAddress::new(a2).ok_or(Error::InvalidSyscall)?,
|
||||||
MemoryAddress::new(a3),
|
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),
|
Some(SysCallNumber::Invalid) => SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7),
|
||||||
None => return Err(Error::InvalidSyscall),
|
None => return Err(Error::InvalidSyscall),
|
||||||
})
|
})
|
||||||
|
|
@ -748,10 +761,7 @@ pub fn receive_message(server: SID) -> core::result::Result<MessageEnvelope, Err
|
||||||
/// * **ServerNotFound**: The server does not exist so the connection is now invalid
|
/// * **ServerNotFound**: The server does not exist so the connection is now invalid
|
||||||
/// * **BadAddress**: The client tried to pass a Memory message using an address it doesn't own
|
/// * **BadAddress**: The client tried to pass a Memory message using an address it doesn't own
|
||||||
/// * **Timeout**: The timeout limit has been reached
|
/// * **Timeout**: The timeout limit has been reached
|
||||||
pub fn send_message(
|
pub fn send_message(connection: CID, message: Message) -> core::result::Result<(), Error> {
|
||||||
connection: CID,
|
|
||||||
message: Message,
|
|
||||||
) -> core::result::Result<(), Error> {
|
|
||||||
let result =
|
let result =
|
||||||
rsyscall(SysCall::SendMessage(connection, message)).expect("couldn't send message");
|
rsyscall(SysCall::SendMessage(connection, message)).expect("couldn't send message");
|
||||||
if let Result::Ok = result {
|
if let Result::Ok = result {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue