test: get initial test passing

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-06-23 14:42:01 +08:00
parent b1a41978ba
commit e9469991be
4 changed files with 71 additions and 20 deletions

View file

@ -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<String>;
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<String>, 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

View file

@ -126,7 +126,7 @@ fn next_pid_to_run(last_pid: Option<PID>) -> Option<PID> {
/// 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);
}

View file

@ -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");
}

View file

@ -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};