From 80a003c83979a67146d16f2461e7936fb94313bd Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 14 Oct 2020 19:18:16 +0800 Subject: [PATCH 1/4] kernel: syscall: implement `connect()` This implements the `connect()` syscall as a blocking version of `try_connect()`. Signed-off-by: Sean Cross --- kernel/src/syscall.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index eb0a47eca..72e9125ee 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -10,6 +10,15 @@ use xous_kernel::*; /// This is the context that called SwitchTo static mut SWITCHTO_CALLER: Option<(PID, TID)> = None; +fn retry_syscall(pid: PID, tid: TID) -> SysCallResult { + if cfg!(baremetal) { + arch::process::Process::with_current_mut(|p| p.retry_instruction(tid))?; + do_yield(pid, tid) + } else { + Ok(xous_kernel::Result::WouldBlock) + } +} + fn do_yield(_pid: PID, tid: TID) -> SysCallResult { // If we're not running on bare metal, treat this as a no-op. if !cfg!(baremetal) { @@ -698,7 +707,6 @@ pub fn handle_inner(pid: PID, tid: TID, call: SysCall) -> SysCallResult { SysCall::ReturnMemory(sender, buf) => return_memory(pid, tid, sender, buf), SysCall::ReturnScalar1(sender, arg) => return_scalar(pid, tid, sender, arg), SysCall::ReturnScalar2(sender, arg1, arg2) => return_scalar2(pid, tid, sender, arg1, arg2), - // SysCall::ReturnScalar2(sender, arg, arg2) => return_memory(pid, tid, sender, arg, arg2), SysCall::TrySendMessage(cid, message) => send_message(pid, tid, cid, message), SysCall::TerminateProcess => SystemServices::with_mut(|ss| { ss.switch_from_thread(pid, tid)?; @@ -714,17 +722,22 @@ pub fn handle_inner(pid: PID, tid: TID, call: SysCall) -> SysCallResult { SystemServices::with_mut(|ss| ss.shutdown().map(|_| xous_kernel::Result::Ok)) } - // SysCall::Connect(sid) => { - // SystemServices::with_mut(|ss| ss.connect_to_server(sid).map(xous_kernel::Result::ConnectionID)) - // } + SysCall::Connect(sid) => { + let result = SystemServices::with_mut(|ss| { + ss.connect_to_server(sid) + .map(xous_kernel::Result::ConnectionID) + }); + match result { + Ok(o) => Ok(o), + Err(xous_kernel::Error::ServerNotFound) => retry_syscall(pid, tid), + Err(e) => Err(e), + } + } SysCall::SendMessage(cid, message) => { let result = send_message(pid, tid, cid, message); match result { Ok(o) => Ok(o), - Err(xous_kernel::Error::ServerQueueFull) => { - arch::process::Process::with_current_mut(|p| p.retry_instruction(tid))?; - do_yield(pid, tid) - } + Err(xous_kernel::Error::ServerQueueFull) => retry_syscall(pid, tid), Err(e) => Err(e), } } From 797d622cc7c51785ce3fd105b02146605aae6cfb Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 14 Oct 2020 19:19:01 +0800 Subject: [PATCH 2/4] kernel: use `connect()` for some kernel tests Now that `connect()` works, use that for some tests rather than exclusively using `try_connect()`. Signed-off-by: Sean Cross --- kernel/src/test.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/src/test.rs b/kernel/src/test.rs index c999f5ddf..099035e94 100644 --- a/kernel/src/test.rs +++ b/kernel/src/test.rs @@ -377,7 +377,7 @@ fn send_borrow_message() { // Perform a connection to the server // println!("CLIENT: Connecting to server..."); - let conn = xous_kernel::try_connect(sid).expect("couldn't connect to server"); + let conn = xous_kernel::connect(sid).expect("couldn't connect to server"); // Convert the message into a "Carton" that can be shipped as a message // println!("CLIENT: Creating carton..."); @@ -443,7 +443,7 @@ fn send_mutableborrow_message() { let sid = server_addr_recv.recv().unwrap(); // Perform a connection to the server - let conn = xous_kernel::try_connect(sid).expect("couldn't connect to server"); + let conn = xous_kernel::connect(sid).expect("couldn't connect to server"); // Convert the message into a "Carton" that can be shipped as a message let mut carton = xous_kernel::carton::Carton::from_bytes(&test_bytes); @@ -516,7 +516,7 @@ fn send_mutableborrow_message_repeat() { let sid = server_addr_recv.recv().unwrap(); // Perform a connection to the server - let conn = xous_kernel::try_connect(sid).expect("couldn't connect to server"); + let conn = xous_kernel::connect(sid).expect("couldn't connect to server"); // Convert the message into a "Carton" that can be shipped as a message for iteration in 0..loops { From 2b66378556e34c53d0974b74d4452fc86f0b9cc7 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 14 Oct 2020 19:26:13 +0800 Subject: [PATCH 3/4] xous: definitions: add WouldBlock This opcode indicates a command would block and should be retried. Signed-off-by: Sean Cross --- xous-rs/src/definitions.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/xous-rs/src/definitions.rs b/xous-rs/src/definitions.rs index 2667f615a..1812d6fa9 100644 --- a/xous-rs/src/definitions.rs +++ b/xous-rs/src/definitions.rs @@ -459,6 +459,9 @@ pub enum Result { /// A scalar with two values Scalar2(usize, usize), + /// The syscall should be attempted again + WouldBlock, + UnknownResult(usize, usize, usize, usize, usize, usize, usize), } @@ -494,6 +497,7 @@ impl Result { let s = sid.to_u32(); [15, s.0 as _, s.1 as _, s.2 as _, s.3 as _, *cid, 0, 0] } + Result::WouldBlock => [16, 0, 0, 0, 0, 0, 0, 0], Result::UnknownResult(arg1, arg2, arg3, arg4, arg5, arg6, arg7) => { [usize::MAX, *arg1, *arg2, *arg3, *arg4, *arg5, *arg6, *arg7] } @@ -567,6 +571,7 @@ impl Result { SID::from_u32(src[1] as _, src[2] as _, src[3] as _, src[4] as _), src[5] as _, ), + 16 => Result::WouldBlock, _ => Result::UnknownResult(src[0], src[1], src[2], src[3], src[4], src[5], src[6]), } } From 4ca97125ad7cdf4156c071ccbe88c1f819c97dca Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 14 Oct 2020 19:26:33 +0800 Subject: [PATCH 4/4] xous: hosted: retry when WouldBlock is received Retry a message when we receive a WouldBlock response. Signed-off-by: Sean Cross --- xous-rs/src/arch/hosted.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/xous-rs/src/arch/hosted.rs b/xous-rs/src/arch/hosted.rs index 2788045d1..64bbab36b 100644 --- a/xous-rs/src/arch/hosted.rs +++ b/xous-rs/src/arch/hosted.rs @@ -428,19 +428,24 @@ pub fn _xous_syscall( let mut xsc_borrowed = xsc.borrow_mut(); let xsc_asmut = xsc_borrowed.as_mut().expect("not connected to server!"); - _xous_syscall_to( - nr, - a1, - a2, - a3, - a4, - a5, - a6, - a7, - &call, - &mut xsc_asmut.send.lock().unwrap(), - ); - _xous_syscall_result(&call, ret, *tid.borrow(), xsc_asmut); + loop { + _xous_syscall_to( + nr, + a1, + a2, + a3, + a4, + a5, + a6, + a7, + &call, + &mut xsc_asmut.send.lock().unwrap(), + ); + _xous_syscall_result(&call, ret, *tid.borrow(), xsc_asmut); + if *ret != Result::WouldBlock { + return; + } + } }) }); }