2020-06-03 07:34:55 +00:00
|
|
|
pub mod irq;
|
|
|
|
|
pub mod mem;
|
|
|
|
|
pub mod process;
|
|
|
|
|
pub mod syscall;
|
|
|
|
|
|
2020-06-15 10:13:10 +00:00
|
|
|
use std::env;
|
2020-06-18 03:39:46 +00:00
|
|
|
use std::io::Read;
|
2020-06-03 07:34:55 +00:00
|
|
|
use std::mem::size_of;
|
|
|
|
|
use std::net::{TcpListener, TcpStream};
|
2020-06-15 10:13:10 +00:00
|
|
|
use std::process::exit;
|
|
|
|
|
use std::sync::mpsc::{channel, Sender};
|
2020-06-18 03:39:46 +00:00
|
|
|
use std::thread::spawn;
|
2020-06-15 10:13:10 +00:00
|
|
|
use xous::{Result, SysCall, PID};
|
2020-06-03 07:34:55 +00:00
|
|
|
|
2020-06-18 03:39:46 +00:00
|
|
|
use crate::arch::process::ProcessHandle;
|
2020-06-15 10:13:10 +00:00
|
|
|
use crate::services::SystemServicesHandle;
|
2020-06-03 07:34:55 +00:00
|
|
|
|
2020-06-18 03:39:46 +00:00
|
|
|
fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender<(PID, SysCall)>) {
|
2020-06-03 07:34:55 +00:00
|
|
|
loop {
|
|
|
|
|
let mut pkt = [0usize; 8];
|
|
|
|
|
let mut incoming_word = [0u8; size_of::<usize>()];
|
|
|
|
|
for word in pkt.iter_mut() {
|
2020-06-18 03:39:46 +00:00
|
|
|
if let Err(e) = conn.read_exact(&mut incoming_word) {
|
|
|
|
|
println!(
|
2020-06-22 09:42:24 +00:00
|
|
|
"Client {} disconnected: {}. Shutting down virtual process.",
|
|
|
|
|
pid, e
|
2020-06-18 03:39:46 +00:00
|
|
|
);
|
|
|
|
|
let call = xous::SysCall::TerminateProcess;
|
|
|
|
|
chn.send((pid, call)).unwrap();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-06-03 07:34:55 +00:00
|
|
|
*word = usize::from_le_bytes(incoming_word);
|
|
|
|
|
}
|
2020-06-15 10:13:10 +00:00
|
|
|
let call = xous::SysCall::from_args(
|
|
|
|
|
pkt[0], pkt[1], pkt[2], pkt[3], pkt[4], pkt[5], pkt[6], pkt[7],
|
2020-06-03 07:34:55 +00:00
|
|
|
);
|
2020-06-15 10:13:10 +00:00
|
|
|
match call {
|
|
|
|
|
Err(e) => println!("Received invalid syscall: {:?}", e),
|
|
|
|
|
Ok(call) => {
|
|
|
|
|
println!(
|
|
|
|
|
"Received packet: {:08x} {} {} {} {} {} {} {}: {:?}",
|
|
|
|
|
pkt[0], pkt[1], pkt[2], pkt[3], pkt[4], pkt[5], pkt[6], pkt[7], call
|
|
|
|
|
);
|
2020-06-18 03:39:46 +00:00
|
|
|
chn.send((pid, call)).expect("couldn't make syscall");
|
2020-06-15 10:13:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 03:39:46 +00:00
|
|
|
fn listen_thread(chn: Sender<(PID, SysCall)>) {
|
2020-06-15 10:13:10 +00:00
|
|
|
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);
|
|
|
|
|
});
|
2020-06-03 07:34:55 +00:00
|
|
|
loop {
|
|
|
|
|
let (conn, addr) = listener.accept().expect("Unable to accept connection");
|
|
|
|
|
println!("New client connected from {}", addr);
|
|
|
|
|
let thr_chn = chn.clone();
|
2020-06-15 10:13:10 +00:00
|
|
|
|
|
|
|
|
let new_pid = {
|
|
|
|
|
let mut ss = SystemServicesHandle::get();
|
2020-06-18 03:39:46 +00:00
|
|
|
ss.spawn_process(process::ProcessInit::new(conn.try_clone().unwrap()), ())
|
|
|
|
|
.unwrap()
|
2020-06-15 10:13:10 +00:00
|
|
|
};
|
|
|
|
|
println!("Assigned PID {}", new_pid);
|
|
|
|
|
spawn(move || handle_connection(conn, new_pid, thr_chn));
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-15 10:13:10 +00:00
|
|
|
/// 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.
|
2020-06-03 07:34:55 +00:00
|
|
|
pub fn idle() {
|
2020-06-15 10:13:10 +00:00
|
|
|
// Start listening.
|
|
|
|
|
let (sender, receiver) = channel();
|
|
|
|
|
let listen_thread_handle = spawn(move || listen_thread(sender));
|
2020-06-03 07:34:55 +00:00
|
|
|
|
2020-06-18 03:39:46 +00:00
|
|
|
while let Ok((pid, call)) = receiver.recv() {
|
2020-06-16 08:34:49 +00:00
|
|
|
{
|
|
|
|
|
let mut ss = SystemServicesHandle::get();
|
|
|
|
|
ss.switch_to(pid, Some(1)).unwrap();
|
|
|
|
|
}
|
2020-06-18 03:39:46 +00:00
|
|
|
let is_terminate = call == SysCall::TerminateProcess;
|
2020-06-15 10:13:10 +00:00
|
|
|
let response = crate::syscall::handle(pid, call).unwrap_or_else(Result::Error);
|
2020-06-18 03:39:46 +00:00
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
}
|
2020-06-22 09:42:24 +00:00
|
|
|
processes.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);
|
|
|
|
|
crate::syscall::handle(pid, SysCall::TerminateProcess).ok();
|
|
|
|
|
});
|
2020-06-18 03:39:46 +00:00
|
|
|
}
|
2020-06-16 08:34:49 +00:00
|
|
|
let mut ss = SystemServicesHandle::get();
|
|
|
|
|
ss.switch_from(pid, 1, true).unwrap();
|
|
|
|
|
}
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|
2020-06-15 10:13:10 +00:00
|
|
|
|
|
|
|
|
eprintln!("Exiting Xous because the listen thread channel has closed. Waiting for thread to finish...");
|
2020-06-18 03:39:46 +00:00
|
|
|
listen_thread_handle
|
|
|
|
|
.join()
|
|
|
|
|
.expect("error waiting for listen thread to return");
|
2020-06-15 10:13:10 +00:00
|
|
|
|
|
|
|
|
eprintln!("Thank you for using Xous!");
|
|
|
|
|
exit(0);
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|