diff --git a/kernel/src/arch/hosted/mod.rs b/kernel/src/arch/hosted/mod.rs index 0fa432494..ebe55be6e 100644 --- a/kernel/src/arch/hosted/mod.rs +++ b/kernel/src/arch/hosted/mod.rs @@ -7,7 +7,7 @@ use std::env; use std::io::Read; use std::mem::size_of; use std::net::{TcpListener, TcpStream}; -use std::sync::mpsc::{channel, Sender, Receiver}; +use std::sync::mpsc::{channel, Receiver, Sender}; use std::thread::spawn; use std::time::Duration; @@ -16,6 +16,10 @@ use xous::{Result, SysCall, PID}; use crate::arch::process::ProcessHandle; use crate::services::SystemServicesHandle; +pub type KernelArguments = Option; + +const DEFAULT_LISTEN_ADDRESS: &str = "localhost:9687"; + /// 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 { @@ -49,15 +53,19 @@ fn handle_connection(mut conn: TcpStream, pid: PID, 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()); +fn listen_thread(address: Option, chn: Sender<(PID, SysCall)>, quit: Receiver<()>) { + let listen_addr = address.unwrap_or_else(|| { + env::var("XOUS_LISTEN_ADDR").unwrap_or_else(|_| DEFAULT_LISTEN_ADDRESS.to_owned()) + }); println!("Starting Xous server on {}...", listen_addr); let listener = TcpListener::bind(listen_addr).unwrap_or_else(|e| { panic!("Unable to create server: {}", e); }); // 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"); + listener + .set_nonblocking(true) + .expect("couldn't set TcpListener to nonblocking"); loop { match listener.accept() { Ok((conn, addr)) => { @@ -76,12 +84,12 @@ fn listen_thread(chn: Sender<(PID, SysCall)>, quit: Receiver<()>) { match quit.recv_timeout(Duration::from_millis(10)) { Err(std::sync::mpsc::RecvTimeoutError::Timeout) => { continue; - }, + } _ => { return; } } - }, + } Err(e) => { eprintln!("error accepting connections: {}", e); return; @@ -93,19 +101,13 @@ fn listen_thread(chn: Sender<(PID, SysCall)>, quit: Receiver<()>) { /// 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() -> bool { +pub fn idle(args: &KernelArguments) -> bool { // Start listening. let (sender, receiver) = channel(); 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) - // } + let server_addr = args.clone(); + let listen_thread_handle = spawn(move || listen_thread(server_addr, sender, term_receiver)); while let Ok((pid, call)) = receiver.recv() { { @@ -117,7 +119,9 @@ pub fn idle() -> bool { // 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"); + term_sender + .send(()) + .expect("unable to send shutdown message"); } // Handle the syscall within the Xous kernel diff --git a/kernel/src/main.rs b/kernel/src/main.rs index a33271683..767b50347 100644 --- a/kernel/src/main.rs +++ b/kernel/src/main.rs @@ -126,7 +126,7 @@ fn next_pid_to_run(last_pid: Option) -> Option { /// Common main function for baremetal and hosted environments. #[no_mangle] -pub extern "C" fn kmain() { +pub extern "C" fn kmain(args: crate::arch::KernelArguments) { // Start performing round-robin on all child processes. // Note that at this point, no new direct children of INIT may be created. @@ -144,7 +144,7 @@ pub extern "C" fn kmain() { None => { println!("No runnable tasks found. Entering idle state..."); // Special case for testing: idle can return `false` to indicate exit - if ! arch::idle() { + if ! arch::idle(&args) { return; } } @@ -156,5 +156,5 @@ pub extern "C" fn kmain() { /// this function does not exist. #[cfg(not(baremetal))] fn main() { - kmain(); + kmain(None); } diff --git a/xous-rs/src/hosted/mod.rs b/xous-rs/src/hosted/mod.rs index 82d055df3..a99ee40a4 100644 --- a/xous-rs/src/hosted/mod.rs +++ b/xous-rs/src/hosted/mod.rs @@ -30,14 +30,30 @@ pub fn _xous_syscall( let conn = TcpStream::connect("localhost:9687").unwrap(); *xous_server_connection = Some(conn); } + let xsc: &mut TcpStream = (*xous_server_connection).as_mut().unwrap(); + _xous_syscall_to(nr, a1, a2, a3, a4, a5, a6, a7, ret, xsc) +} +#[allow(clippy::too_many_arguments)] +#[no_mangle] +pub fn _xous_syscall_to( + nr: usize, + a1: usize, + a2: usize, + a3: usize, + a4: usize, + a5: usize, + a6: usize, + a7: usize, + ret: &mut Result, + xsc: &mut TcpStream, +) { print!( "Making Syscall: {:?}", SysCall::from_args(nr, a1, a2, a3, a4, a5, a6, a7).unwrap() ); // Send the packet to the server - let mut xsc = xous_server_connection.as_ref().unwrap(); for word in &[nr, a1, a2, a3, a4, a5, a6, a7] { xsc.write_all(&word.to_le_bytes()).expect("Disconnection"); } diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index 294dd1851..275081ea6 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -5,6 +5,9 @@ use crate::{ use num_derive::FromPrimitive; use num_traits::FromPrimitive; +#[cfg(not(baremetal))] +use std::net::TcpStream; + #[derive(Debug, PartialEq)] pub enum SysCall { /// Allocates pages of memory, equal to a total of `size` bytes. A physical @@ -633,6 +636,19 @@ extern "Rust" { a7: usize, ret: &mut Result, ); + #[cfg(not(baremetal))] + pub fn _xous_syscall_to( + nr: usize, + a1: usize, + a2: usize, + a3: usize, + a4: usize, + a5: usize, + a6: usize, + a7: usize, + ret: &mut Result, + xsc: &mut TcpStream, + ); } /// Map the given physical address to the given virtual address. @@ -804,6 +820,21 @@ pub fn rsyscall(call: SysCall) -> SysCallResult { } } +/// Perform a remote syscall to a given address +#[cfg(not(baremetal))] +pub fn rsyscall_to(call: SysCall, stream: &mut TcpStream) -> SysCallResult { + let mut ret = Result::Ok; + let args = call.as_args(); + unsafe { + _xous_syscall_to( + args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], &mut ret, stream, + ) + }; + match ret { + Result::Error(e) => Err(e), + other => Ok(other), + } +} // /// This is dangerous, but fast. // pub unsafe fn dangerous_syscall(call: SysCall) -> SyscallResult { // use core::mem::{transmute, MaybeUninit};