mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
kernel: fix message sender parameter
The `sender` was, previously, somewhat mislabeled. It was a combination of PID and TID, except when it wasn't. Instead, make this value a combination of ConnectionID and queue index. This makes it easier to reason about which connection we're responding to. Additionally, don't expose teh global Server Index Table to userspace programs. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
ad154e998e
commit
e5583a7646
4 changed files with 378 additions and 113 deletions
|
|
@ -3,16 +3,24 @@ use core::mem;
|
||||||
use xous_kernel::{MemoryAddress, MemoryRange, MemorySize, Message, PID, SID, TID};
|
use xous_kernel::{MemoryAddress, MemoryRange, MemorySize, Message, PID, SID, TID};
|
||||||
|
|
||||||
pub struct SenderID {
|
pub struct SenderID {
|
||||||
pub sidx: usize,
|
/// The connection ID inside the server
|
||||||
pub tidx: usize,
|
pub cid: usize,
|
||||||
|
/// The index into the queue array
|
||||||
|
pub idx: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SenderID {
|
impl From<usize> for SenderID {
|
||||||
pub fn from_usize(src: usize) -> Result<SenderID, xous_kernel::Error> {
|
fn from(item: usize) -> SenderID {
|
||||||
Ok(SenderID {
|
SenderID {
|
||||||
sidx: src >> 16,
|
cid: item >> 16,
|
||||||
tidx: src & 0xffff,
|
idx: item & 0xffff,
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Into<usize> for SenderID {
|
||||||
|
fn into(self) -> usize {
|
||||||
|
(self.cid << 16) | (self.idx & 0xffff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -26,6 +34,9 @@ pub enum WaitingMessage {
|
||||||
/// The memory was moved, and so shouldn't be returned.
|
/// The memory was moved, and so shouldn't be returned.
|
||||||
MovedMemory,
|
MovedMemory,
|
||||||
|
|
||||||
|
/// The message was a scalar message, so you should return the result to the process
|
||||||
|
ScalarMessage(PID, TID),
|
||||||
|
|
||||||
/// This memory should be returned to the system.
|
/// This memory should be returned to the system.
|
||||||
ForgetMemory(MemoryRange),
|
ForgetMemory(MemoryRange),
|
||||||
}
|
}
|
||||||
|
|
@ -37,9 +48,9 @@ pub enum WaitingMessage {
|
||||||
enum QueuedMessage {
|
enum QueuedMessage {
|
||||||
Empty,
|
Empty,
|
||||||
BlockingScalarMessage(
|
BlockingScalarMessage(
|
||||||
u16, /* sender PID */
|
u16, /* client PID */
|
||||||
u16, /* Sender CTX */
|
u16, /* client CTX */
|
||||||
usize, /* sender base address */
|
usize, /* server return address */
|
||||||
usize, /* id */
|
usize, /* id */
|
||||||
usize, /* arg1 */
|
usize, /* arg1 */
|
||||||
usize, /* arg2 */
|
usize, /* arg2 */
|
||||||
|
|
@ -47,9 +58,9 @@ enum QueuedMessage {
|
||||||
usize, /* arg4 */
|
usize, /* arg4 */
|
||||||
),
|
),
|
||||||
ScalarMessage(
|
ScalarMessage(
|
||||||
u16, /* sender PID */
|
u16, /* client PID */
|
||||||
u16, /* Sender CTX */
|
u16, /* client CTX */
|
||||||
usize, /* sender base address */
|
usize, /* server return address */
|
||||||
usize, /* id */
|
usize, /* id */
|
||||||
usize, /* arg1 */
|
usize, /* arg1 */
|
||||||
usize, /* arg2 */
|
usize, /* arg2 */
|
||||||
|
|
@ -57,8 +68,8 @@ enum QueuedMessage {
|
||||||
usize, /* arg4 */
|
usize, /* arg4 */
|
||||||
),
|
),
|
||||||
MemoryMessageSend(
|
MemoryMessageSend(
|
||||||
u16, /* sender PID */
|
u16, /* client PID */
|
||||||
u16, /* Sender CTX */
|
u16, /* client CTX */
|
||||||
usize, /* reserved */
|
usize, /* reserved */
|
||||||
usize, /* id */
|
usize, /* id */
|
||||||
usize, /* buf */
|
usize, /* buf */
|
||||||
|
|
@ -67,9 +78,9 @@ enum QueuedMessage {
|
||||||
usize, /* valid */
|
usize, /* valid */
|
||||||
),
|
),
|
||||||
MemoryMessageROLend(
|
MemoryMessageROLend(
|
||||||
u16, /* sender PID */
|
u16, /* client PID */
|
||||||
u16, /* Sender CTX */
|
u16, /* client CTX */
|
||||||
usize, /* sender base address */
|
usize, /* address of memory base in server */
|
||||||
usize, /* id */
|
usize, /* id */
|
||||||
usize, /* buf */
|
usize, /* buf */
|
||||||
usize, /* buf_size */
|
usize, /* buf_size */
|
||||||
|
|
@ -77,9 +88,9 @@ enum QueuedMessage {
|
||||||
usize, /* valid */
|
usize, /* valid */
|
||||||
),
|
),
|
||||||
MemoryMessageRWLend(
|
MemoryMessageRWLend(
|
||||||
u16, /* sender PID */
|
u16, /* client PID */
|
||||||
u16, /* Sender CTX */
|
u16, /* client CTX */
|
||||||
usize, /* sender base address */
|
usize, /* address of memory base in server */
|
||||||
usize, /* id */
|
usize, /* id */
|
||||||
usize, /* buf */
|
usize, /* buf */
|
||||||
usize, /* buf_size */
|
usize, /* buf_size */
|
||||||
|
|
@ -90,9 +101,9 @@ enum QueuedMessage {
|
||||||
/// The process lending this memory terminated before
|
/// The process lending this memory terminated before
|
||||||
/// we could receive the message.
|
/// we could receive the message.
|
||||||
MemoryMessageROLendTerminated(
|
MemoryMessageROLendTerminated(
|
||||||
u16, /* sender PID */
|
u16, /* client PID */
|
||||||
u16, /* Sender CTX */
|
u16, /* client CTX */
|
||||||
usize, /* sender base address */
|
usize, /* address of memory base in server */
|
||||||
usize, /* id */
|
usize, /* id */
|
||||||
usize, /* buf */
|
usize, /* buf */
|
||||||
usize, /* buf_size */
|
usize, /* buf_size */
|
||||||
|
|
@ -103,9 +114,9 @@ enum QueuedMessage {
|
||||||
/// The process lending this memory terminated before
|
/// The process lending this memory terminated before
|
||||||
/// we could receive the message.
|
/// we could receive the message.
|
||||||
MemoryMessageRWLendTerminated(
|
MemoryMessageRWLendTerminated(
|
||||||
u16, /* sender PID */
|
u16, /* client PID */
|
||||||
u16, /* Sender CTX */
|
u16, /* client CTX */
|
||||||
usize, /* sender base address */
|
usize, /* address of memory base in server */
|
||||||
usize, /* id */
|
usize, /* id */
|
||||||
usize, /* buf */
|
usize, /* buf */
|
||||||
usize, /* buf_size */
|
usize, /* buf_size */
|
||||||
|
|
@ -116,9 +127,9 @@ enum QueuedMessage {
|
||||||
/// The process waiting for the response terminated before
|
/// The process waiting for the response terminated before
|
||||||
/// we could receive the message.
|
/// we could receive the message.
|
||||||
BlockingScalarTerminated(
|
BlockingScalarTerminated(
|
||||||
u16, /* sender PID */
|
u16, /* client PID */
|
||||||
u16, /* Sender CTX */
|
u16, /* client CTX */
|
||||||
usize, /* sender base address */
|
usize, /* server return address */
|
||||||
usize, /* id */
|
usize, /* id */
|
||||||
usize, /* arg1 */
|
usize, /* arg1 */
|
||||||
usize, /* arg2 */
|
usize, /* arg2 */
|
||||||
|
|
@ -127,30 +138,34 @@ enum QueuedMessage {
|
||||||
),
|
),
|
||||||
|
|
||||||
/// When a message is taken that needs to be returned -- such as an ROLend
|
/// When a message is taken that needs to be returned -- such as an ROLend
|
||||||
/// or RWLend -- the slot is replaced with a WaitingResponse token and its
|
/// or RWLend -- the slot is replaced with a WaitingReturnMemory token and its
|
||||||
/// index is returned as the message sender. This is used to unblock the
|
/// index is returned as the message sender. This is used to unblock the
|
||||||
/// sending process.
|
/// sending process.
|
||||||
WaitingResponse(
|
WaitingReturnMemory(
|
||||||
u16, /* sender PID */
|
u16, /* client PID */
|
||||||
u16, /* Sender CTX */
|
u16, /* client CTX */
|
||||||
usize, /* Client base address */
|
usize, /* address of memory base in server */
|
||||||
usize, /* Server base address */
|
usize, /* client base address */
|
||||||
usize, /* Range size */
|
usize, /* Range size */
|
||||||
),
|
),
|
||||||
|
|
||||||
/// When a server goes away, its memory must be forgotten instead of being returned
|
/// When a server goes away, its memory must be forgotten instead of being returned
|
||||||
/// to the previous process.
|
/// to the previous process.
|
||||||
WaitingForget(
|
WaitingForget(
|
||||||
u16, /* sender PID */
|
u16, /* client PID */
|
||||||
u16, /* Sender CTX */
|
u16, /* client CTX */
|
||||||
usize, /* Client base address */
|
usize, /* address of memory base in server */
|
||||||
usize, /* Server base address */
|
usize, /* client base address */
|
||||||
usize, /* Range size */
|
usize, /* Range size */
|
||||||
),
|
),
|
||||||
|
|
||||||
/// This is the state when a message is blocking, but has no associated memory
|
/// This is the state when a message is blocking, but has no associated memory
|
||||||
/// page.
|
/// page.
|
||||||
WaitingScalar(u16 /* sender PID */, u16 /* sender CTX */),
|
WaitingReturnScalar(
|
||||||
|
u16, /* client PID */
|
||||||
|
u16, /* client CTX */
|
||||||
|
usize, /* server return address */
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A pointer to resolve a server ID to a particular process
|
/// A pointer to resolve a server ID to a particular process
|
||||||
|
|
@ -179,11 +194,6 @@ pub struct Server {
|
||||||
ready_threads: usize,
|
ready_threads: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert a PID and CTX pair into a `usize` sender
|
|
||||||
fn make_sender(pid: u16, ctx: u16) -> usize {
|
|
||||||
(((pid as usize) << 16) & 0xffff_0000) | (ctx as usize & 0xffff)
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Server {
|
impl Server {
|
||||||
/// Initialize a server in the given option array. This function is
|
/// Initialize a server in the given option array. This function is
|
||||||
/// designed to be called with `new` pointing to an entry in a vec.
|
/// designed to be called with `new` pointing to an entry in a vec.
|
||||||
|
|
@ -313,29 +323,38 @@ impl Server {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert a `QueuedMesage::WaitingResponse` into `QueuedMessage::Empty`
|
/// Convert a `QueuedMesage::WaitingReturnMemory` into `QueuedMessage::Empty`
|
||||||
/// and return the pair. Advance the tail. Note that the `idx` could be
|
/// and return the pair. Advance the tail. Note that the `idx` could be
|
||||||
/// somewhere other than the tail, but as long as it points to a valid
|
/// somewhere other than the tail, but as long as it points to a valid
|
||||||
/// message that's waiting a response, that's acceptable.
|
/// message that's waiting a response, that's acceptable.
|
||||||
pub fn take_waiting_message(
|
pub fn take_waiting_message(
|
||||||
&mut self,
|
&mut self,
|
||||||
idx: usize,
|
idx: usize,
|
||||||
buf: MemoryRange,
|
buf: Option<&MemoryRange>,
|
||||||
) -> Result<WaitingMessage, xous_kernel::Error> {
|
) -> Result<WaitingMessage, xous_kernel::Error> {
|
||||||
if idx > self.queue.len() {
|
if idx > self.queue.len() {
|
||||||
return Err(xous_kernel::Error::BadAddress);
|
return Err(xous_kernel::Error::BadAddress);
|
||||||
}
|
}
|
||||||
let (pid, ctx, server_addr, client_addr, len, forget) = match self.queue[idx] {
|
let (pid, ctx, server_addr, client_addr, len, forget, is_memory) = match self.queue[idx] {
|
||||||
QueuedMessage::WaitingResponse(pid, ctx, server_addr, client_addr, len) => {
|
QueuedMessage::WaitingReturnMemory(pid, ctx, server_addr, client_addr, len) => {
|
||||||
(pid, ctx, server_addr, client_addr, len, false)
|
(pid, ctx, server_addr, client_addr, len, false, true)
|
||||||
}
|
}
|
||||||
QueuedMessage::WaitingForget(pid, ctx, server_addr, client_addr, len) => {
|
QueuedMessage::WaitingForget(pid, ctx, server_addr, client_addr, len) => {
|
||||||
(pid, ctx, server_addr, client_addr, len, true)
|
(pid, ctx, server_addr, client_addr, len, true, true)
|
||||||
|
}
|
||||||
|
QueuedMessage::WaitingReturnScalar(pid, ctx, return_address) => {
|
||||||
|
(pid, ctx, return_address, 0, 0, true, false)
|
||||||
}
|
}
|
||||||
_ => return Ok(WaitingMessage::None),
|
_ => return Ok(WaitingMessage::None),
|
||||||
};
|
};
|
||||||
if server_addr != buf.as_ptr() as usize || len != buf.len() {
|
|
||||||
return Err(xous_kernel::Error::BadAddress);
|
// Sanity check the specified address was correct, and matches what we
|
||||||
|
// had cached.
|
||||||
|
if is_memory {
|
||||||
|
let buf = buf.expect("memory message expected but no buffer passed!");
|
||||||
|
if server_addr != buf.as_ptr() as usize || len != buf.len() {
|
||||||
|
return Err(xous_kernel::Error::BadAddress);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self.queue[idx] = QueuedMessage::Empty;
|
self.queue[idx] = QueuedMessage::Empty;
|
||||||
self.queue_tail += 1;
|
self.queue_tail += 1;
|
||||||
|
|
@ -346,6 +365,13 @@ impl Server {
|
||||||
// Destructure the PID and context ID from the `pid_ctx` field
|
// Destructure the PID and context ID from the `pid_ctx` field
|
||||||
// println!("Taking waiting message -- pid: {} ctx: {}", pid, ctx);
|
// println!("Taking waiting message -- pid: {} ctx: {}", pid, ctx);
|
||||||
|
|
||||||
|
if !is_memory {
|
||||||
|
return Ok(WaitingMessage::ScalarMessage(
|
||||||
|
PID::new(pid as _).unwrap(),
|
||||||
|
ctx as _,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
if forget {
|
if forget {
|
||||||
return Ok(WaitingMessage::ForgetMemory(MemoryRange::new(
|
return Ok(WaitingMessage::ForgetMemory(MemoryRange::new(
|
||||||
server_addr,
|
server_addr,
|
||||||
|
|
@ -371,7 +397,7 @@ impl Server {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove a message from the server's queue and replace it with either a QueuedMessage::WaitingResponse
|
/// Remove a message from the server's queue and replace it with either a QueuedMessage::WaitingReturnMemory
|
||||||
/// or, for Scalar messages, QueuedMessage::Empty.
|
/// or, for Scalar messages, QueuedMessage::Empty.
|
||||||
///
|
///
|
||||||
/// For non-Scalar messages, you must call `take_waiting_message()` in order to return
|
/// For non-Scalar messages, you must call `take_waiting_message()` in order to return
|
||||||
|
|
@ -381,16 +407,20 @@ impl Server {
|
||||||
///
|
///
|
||||||
/// * **None**: There are no waiting messages
|
/// * **None**: There are no waiting messages
|
||||||
/// ***Some(MessageEnvelope): This message is queued.
|
/// ***Some(MessageEnvelope): This message is queued.
|
||||||
pub fn take_next_message(&mut self, server_idx: usize) -> Option<xous_kernel::MessageEnvelope> {
|
pub fn take_next_message(&mut self, cid: xous_kernel::CID) -> Option<xous_kernel::MessageEnvelope> {
|
||||||
// println!(
|
// println!(
|
||||||
// "queue_head: ((({}))) queue_tail: ((({}))): {:?}",
|
// "queue_head: ((({}))) queue_tail: ((({}))): {:?} CID: ((({})))",
|
||||||
// self.queue_head, self.queue_tail, self.queue[self.queue_tail]
|
// self.queue_head, self.queue_tail, self.queue[self.queue_tail], cid
|
||||||
// );
|
// );
|
||||||
|
let sender = SenderID {
|
||||||
|
idx: self.queue_tail,
|
||||||
|
cid,
|
||||||
|
}.into();
|
||||||
let (result, response) = match self.queue[self.queue_tail] {
|
let (result, response) = match self.queue[self.queue_tail] {
|
||||||
QueuedMessage::Empty => return None,
|
QueuedMessage::Empty => return None,
|
||||||
QueuedMessage::WaitingResponse(_, _, _, _, _) => return None,
|
QueuedMessage::WaitingReturnMemory(_, _, _, _, _) => return None,
|
||||||
QueuedMessage::WaitingForget(_, _, _, _, _) => return None,
|
QueuedMessage::WaitingForget(_, _, _, _, _) => return None,
|
||||||
QueuedMessage::WaitingScalar(_, _) => return None,
|
QueuedMessage::WaitingReturnScalar(_, _, _) => return None,
|
||||||
QueuedMessage::MemoryMessageROLend(
|
QueuedMessage::MemoryMessageROLend(
|
||||||
pid,
|
pid,
|
||||||
ctx,
|
ctx,
|
||||||
|
|
@ -402,7 +432,7 @@ impl Server {
|
||||||
valid,
|
valid,
|
||||||
) => (
|
) => (
|
||||||
xous_kernel::MessageEnvelope {
|
xous_kernel::MessageEnvelope {
|
||||||
sender: self.queue_tail | (server_idx << 16),
|
sender,
|
||||||
body: xous_kernel::Message::Borrow(xous_kernel::MemoryMessage {
|
body: xous_kernel::Message::Borrow(xous_kernel::MemoryMessage {
|
||||||
id,
|
id,
|
||||||
buf: MemoryRange::new(buf, buf_size).ok()?,
|
buf: MemoryRange::new(buf, buf_size).ok()?,
|
||||||
|
|
@ -423,7 +453,7 @@ impl Server {
|
||||||
valid,
|
valid,
|
||||||
) => (
|
) => (
|
||||||
xous_kernel::MessageEnvelope {
|
xous_kernel::MessageEnvelope {
|
||||||
sender: self.queue_tail | (server_idx << 16),
|
sender,
|
||||||
body: xous_kernel::Message::MutableBorrow(xous_kernel::MemoryMessage {
|
body: xous_kernel::Message::MutableBorrow(xous_kernel::MemoryMessage {
|
||||||
id,
|
id,
|
||||||
buf: MemoryRange::new(buf, buf_size).ok()?,
|
buf: MemoryRange::new(buf, buf_size).ok()?,
|
||||||
|
|
@ -444,7 +474,7 @@ impl Server {
|
||||||
valid,
|
valid,
|
||||||
) => (
|
) => (
|
||||||
xous_kernel::MessageEnvelope {
|
xous_kernel::MessageEnvelope {
|
||||||
sender: self.queue_tail | (server_idx << 16),
|
sender,
|
||||||
body: xous_kernel::Message::Borrow(xous_kernel::MemoryMessage {
|
body: xous_kernel::Message::Borrow(xous_kernel::MemoryMessage {
|
||||||
id,
|
id,
|
||||||
buf: MemoryRange::new(buf, buf_size).ok()?,
|
buf: MemoryRange::new(buf, buf_size).ok()?,
|
||||||
|
|
@ -452,7 +482,7 @@ impl Server {
|
||||||
valid: MemorySize::new(valid),
|
valid: MemorySize::new(valid),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
QueuedMessage::WaitingResponse(pid, ctx, buf, client_addr, buf_size),
|
QueuedMessage::WaitingReturnMemory(pid, ctx, buf, client_addr, buf_size),
|
||||||
),
|
),
|
||||||
QueuedMessage::MemoryMessageRWLendTerminated(
|
QueuedMessage::MemoryMessageRWLendTerminated(
|
||||||
pid,
|
pid,
|
||||||
|
|
@ -465,7 +495,7 @@ impl Server {
|
||||||
valid,
|
valid,
|
||||||
) => (
|
) => (
|
||||||
xous_kernel::MessageEnvelope {
|
xous_kernel::MessageEnvelope {
|
||||||
sender: self.queue_tail | (server_idx << 16),
|
sender,
|
||||||
body: xous_kernel::Message::MutableBorrow(xous_kernel::MemoryMessage {
|
body: xous_kernel::Message::MutableBorrow(xous_kernel::MemoryMessage {
|
||||||
id,
|
id,
|
||||||
buf: MemoryRange::new(buf, buf_size).ok()?,
|
buf: MemoryRange::new(buf, buf_size).ok()?,
|
||||||
|
|
@ -473,13 +503,13 @@ impl Server {
|
||||||
valid: MemorySize::new(valid),
|
valid: MemorySize::new(valid),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
QueuedMessage::WaitingResponse(pid, ctx, buf, client_addr, buf_size),
|
QueuedMessage::WaitingReturnMemory(pid, ctx, buf, client_addr, buf_size),
|
||||||
),
|
),
|
||||||
|
|
||||||
QueuedMessage::BlockingScalarMessage(
|
QueuedMessage::BlockingScalarMessage(
|
||||||
pid,
|
pid,
|
||||||
ctx,
|
ctx,
|
||||||
_reserved,
|
client_addr,
|
||||||
id,
|
id,
|
||||||
arg1,
|
arg1,
|
||||||
arg2,
|
arg2,
|
||||||
|
|
@ -487,8 +517,8 @@ impl Server {
|
||||||
arg4,
|
arg4,
|
||||||
) => (
|
) => (
|
||||||
xous_kernel::MessageEnvelope {
|
xous_kernel::MessageEnvelope {
|
||||||
sender: make_sender(pid, ctx),
|
sender,
|
||||||
body: xous_kernel::Message::Scalar(xous_kernel::ScalarMessage {
|
body: xous_kernel::Message::BlockingScalar(xous_kernel::ScalarMessage {
|
||||||
id,
|
id,
|
||||||
arg1,
|
arg1,
|
||||||
arg2,
|
arg2,
|
||||||
|
|
@ -496,11 +526,11 @@ impl Server {
|
||||||
arg4,
|
arg4,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
QueuedMessage::WaitingScalar(pid, ctx),
|
QueuedMessage::WaitingReturnScalar(pid, ctx, client_addr),
|
||||||
),
|
),
|
||||||
QueuedMessage::MemoryMessageSend(
|
QueuedMessage::MemoryMessageSend(
|
||||||
pid,
|
_pid,
|
||||||
ctx,
|
_ctx,
|
||||||
_reserved,
|
_reserved,
|
||||||
id,
|
id,
|
||||||
buf,
|
buf,
|
||||||
|
|
@ -509,7 +539,7 @@ impl Server {
|
||||||
valid,
|
valid,
|
||||||
) => {
|
) => {
|
||||||
let msg = xous_kernel::MessageEnvelope {
|
let msg = xous_kernel::MessageEnvelope {
|
||||||
sender: make_sender(pid, ctx),
|
sender,
|
||||||
body: xous_kernel::Message::Move(xous_kernel::MemoryMessage {
|
body: xous_kernel::Message::Move(xous_kernel::MemoryMessage {
|
||||||
id,
|
id,
|
||||||
buf: MemoryRange::new(buf, buf_size).ok()?,
|
buf: MemoryRange::new(buf, buf_size).ok()?,
|
||||||
|
|
@ -526,9 +556,9 @@ impl Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scalar messages have nothing to return, so they can go straight to the `Free` state
|
// Scalar messages have nothing to return, so they can go straight to the `Free` state
|
||||||
QueuedMessage::ScalarMessage(pid, ctx, _reserved, id, arg1, arg2, arg3, arg4) => {
|
QueuedMessage::ScalarMessage(_pid, _ctx, _reserved, id, arg1, arg2, arg3, arg4) => {
|
||||||
let msg = xous_kernel::MessageEnvelope {
|
let msg = xous_kernel::MessageEnvelope {
|
||||||
sender: make_sender(pid, ctx),
|
sender,
|
||||||
body: xous_kernel::Message::Scalar(xous_kernel::ScalarMessage {
|
body: xous_kernel::Message::Scalar(xous_kernel::ScalarMessage {
|
||||||
id,
|
id,
|
||||||
arg1,
|
arg1,
|
||||||
|
|
@ -544,9 +574,18 @@ impl Server {
|
||||||
}
|
}
|
||||||
return Some(msg);
|
return Some(msg);
|
||||||
}
|
}
|
||||||
QueuedMessage::BlockingScalarTerminated(pid, ctx, _reserved, id, arg1, arg2, arg3, arg4) => {
|
QueuedMessage::BlockingScalarTerminated(
|
||||||
|
_pid,
|
||||||
|
_ctx,
|
||||||
|
_reserved,
|
||||||
|
id,
|
||||||
|
arg1,
|
||||||
|
arg2,
|
||||||
|
arg3,
|
||||||
|
arg4,
|
||||||
|
) => {
|
||||||
let msg = xous_kernel::MessageEnvelope {
|
let msg = xous_kernel::MessageEnvelope {
|
||||||
sender: make_sender(pid, ctx),
|
sender,
|
||||||
body: xous_kernel::Message::Scalar(xous_kernel::ScalarMessage {
|
body: xous_kernel::Message::Scalar(xous_kernel::ScalarMessage {
|
||||||
id,
|
id,
|
||||||
arg1,
|
arg1,
|
||||||
|
|
@ -646,7 +685,7 @@ impl Server {
|
||||||
Ok(idx)
|
Ok(idx)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn queue_address(
|
pub fn queue_response(
|
||||||
&mut self,
|
&mut self,
|
||||||
pid: PID,
|
pid: PID,
|
||||||
context: TID,
|
context: TID,
|
||||||
|
|
@ -657,22 +696,37 @@ impl Server {
|
||||||
if self.queue[self.queue_head] != QueuedMessage::Empty {
|
if self.queue[self.queue_head] != QueuedMessage::Empty {
|
||||||
return Err(xous_kernel::Error::ServerQueueFull);
|
return Err(xous_kernel::Error::ServerQueueFull);
|
||||||
}
|
}
|
||||||
let (server_address, len) = match message {
|
self.queue[self.queue_head] = match message {
|
||||||
xous_kernel::Message::Scalar(_)
|
xous_kernel::Message::Scalar(_) | xous_kernel::Message::BlockingScalar(_) => {
|
||||||
| xous_kernel::Message::BlockingScalar(_)
|
QueuedMessage::WaitingReturnScalar(
|
||||||
| xous_kernel::Message::Move(_) => (0, 0),
|
pid.get() as _,
|
||||||
|
context as _,
|
||||||
|
client_address.map(|x| x.get()).unwrap_or(0),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
xous_kernel::Message::Move(msg) => {
|
||||||
|
let server_address = msg.buf.addr.get();
|
||||||
|
let len = msg.buf.size.get();
|
||||||
|
QueuedMessage::WaitingForget(
|
||||||
|
pid.get() as _,
|
||||||
|
context as _,
|
||||||
|
server_address,
|
||||||
|
client_address.map(|x| x.get()).unwrap_or(0),
|
||||||
|
len,
|
||||||
|
)
|
||||||
|
}
|
||||||
xous_kernel::Message::MutableBorrow(msg) | xous_kernel::Message::Borrow(msg) => {
|
xous_kernel::Message::MutableBorrow(msg) | xous_kernel::Message::Borrow(msg) => {
|
||||||
(msg.buf.addr.get(), msg.buf.size.get())
|
let server_address = msg.buf.addr.get();
|
||||||
|
let len = msg.buf.size.get();
|
||||||
|
QueuedMessage::WaitingReturnMemory(
|
||||||
|
pid.get() as _,
|
||||||
|
context as _,
|
||||||
|
server_address,
|
||||||
|
client_address.map(|x| x.get()).unwrap_or(0),
|
||||||
|
len,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
self.queue[self.queue_head] = QueuedMessage::WaitingResponse(
|
|
||||||
pid.get() as _,
|
|
||||||
context as _,
|
|
||||||
server_address,
|
|
||||||
client_address.map(|x| x.get()).unwrap_or(0),
|
|
||||||
len,
|
|
||||||
);
|
|
||||||
let idx = self.queue_head;
|
let idx = self.queue_head;
|
||||||
self.queue_head += 1;
|
self.queue_head += 1;
|
||||||
if self.queue_head >= self.queue.len() {
|
if self.queue_head >= self.queue.len() {
|
||||||
|
|
|
||||||
|
|
@ -1288,7 +1288,7 @@ impl SystemServices {
|
||||||
/// queue.
|
/// queue.
|
||||||
/// * **ServerNotFound**: The server queue was full and a free slot could not
|
/// * **ServerNotFound**: The server queue was full and a free slot could not
|
||||||
/// be found.
|
/// be found.
|
||||||
pub fn create_server(&mut self, pid: PID, sid: SID) -> Result<SID, xous_kernel::Error> {
|
pub fn create_server(&mut self, pid: PID, sid: SID) -> Result<(SID, CID), xous_kernel::Error> {
|
||||||
// println!(
|
// println!(
|
||||||
// "KERNEL({}): Looking through server list for free server",
|
// "KERNEL({}): Looking through server list for free server",
|
||||||
// self.pid.get()
|
// self.pid.get()
|
||||||
|
|
@ -1303,7 +1303,7 @@ impl SystemServices {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (_idx, entry) in self.servers.iter_mut().enumerate() {
|
for entry in self.servers.iter_mut() {
|
||||||
if entry == &None {
|
if entry == &None {
|
||||||
#[cfg(baremetal)]
|
#[cfg(baremetal)]
|
||||||
// Allocate a single page for the server queue
|
// Allocate a single page for the server queue
|
||||||
|
|
@ -1325,7 +1325,9 @@ impl SystemServices {
|
||||||
|
|
||||||
// Initialize the server with the given memory page.
|
// Initialize the server with the given memory page.
|
||||||
Server::init(entry, pid, sid, backing).map_err(|x| x)?;
|
Server::init(entry, pid, sid, backing).map_err(|x| x)?;
|
||||||
return Ok(sid);
|
|
||||||
|
let cid = self.connect_to_server(sid)?;
|
||||||
|
return Ok((sid, cid));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(xous_kernel::Error::ServerNotFound)
|
Err(xous_kernel::Error::ServerNotFound)
|
||||||
|
|
@ -1338,7 +1340,7 @@ impl SystemServices {
|
||||||
// While doing this, find a free slot in case we haven't
|
// While doing this, find a free slot in case we haven't
|
||||||
// yet connected.
|
// yet connected.
|
||||||
|
|
||||||
let _pid = crate::arch::process::current_pid();
|
// let _pid = crate::arch::process::current_pid();
|
||||||
// println!("KERNEL({}): Server table: {:?}", _pid.get(), self.servers);
|
// println!("KERNEL({}): Server table: {:?}", _pid.get(), self.servers);
|
||||||
ArchProcess::with_inner_mut(|process_inner| {
|
ArchProcess::with_inner_mut(|process_inner| {
|
||||||
let mut slot_idx = None;
|
let mut slot_idx = None;
|
||||||
|
|
@ -1390,7 +1392,16 @@ impl SystemServices {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return a server based on the connection id and the current process
|
/// Return a server based on the connection id and the current process
|
||||||
pub fn server_from_sidx(&mut self, sidx: usize) -> Option<&mut Server> {
|
pub fn server_from_sidx(&self, sidx: usize) -> Option<&Server> {
|
||||||
|
if sidx > self.servers.len() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
self.servers[sidx].as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return a server based on the connection id and the current process
|
||||||
|
pub fn server_from_sidx_mut(&mut self, sidx: usize) -> Option<&mut Server> {
|
||||||
if sidx > self.servers.len() {
|
if sidx > self.servers.len() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -1450,7 +1461,7 @@ impl SystemServices {
|
||||||
server_process.mapping.activate().unwrap();
|
server_process.mapping.activate().unwrap();
|
||||||
}
|
}
|
||||||
let server = self
|
let server = self
|
||||||
.server_from_sidx(sidx)
|
.server_from_sidx_mut(sidx)
|
||||||
.expect("couldn't re-discover server index");
|
.expect("couldn't re-discover server index");
|
||||||
server.queue_message(pid, context, message, original_address)
|
server.queue_message(pid, context, message, original_address)
|
||||||
};
|
};
|
||||||
|
|
@ -1461,6 +1472,28 @@ impl SystemServices {
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Obtain the connection ID of the server from within the server process.
|
||||||
|
pub fn server_cid(&mut self, sidx: usize) -> Result<CID, xous_kernel::Error> {
|
||||||
|
let current_pid = self.current_pid();
|
||||||
|
let result = {
|
||||||
|
let server = self
|
||||||
|
.server_from_sidx(sidx)
|
||||||
|
.ok_or(xous_kernel::Error::ServerNotFound)?;
|
||||||
|
let server_pid = server.pid;
|
||||||
|
let sid = server.sid;
|
||||||
|
{
|
||||||
|
let server_process = self.get_process(server_pid)?;
|
||||||
|
server_process.mapping.activate().unwrap();
|
||||||
|
}
|
||||||
|
self.connect_to_server(sid)
|
||||||
|
};
|
||||||
|
let current_process = self
|
||||||
|
.get_process(current_pid)
|
||||||
|
.expect("couldn't restore previous process");
|
||||||
|
current_process.mapping.activate()?;
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
/// Switch to the server's address space and add a "remember this address"
|
/// Switch to the server's address space and add a "remember this address"
|
||||||
/// entry to its server queue, then switch back to the original address space.
|
/// entry to its server queue, then switch back to the original address space.
|
||||||
pub fn remember_server_message(
|
pub fn remember_server_message(
|
||||||
|
|
@ -1481,9 +1514,9 @@ impl SystemServices {
|
||||||
// server_process.mapping.activate();
|
// server_process.mapping.activate();
|
||||||
// }
|
// }
|
||||||
let server = self
|
let server = self
|
||||||
.server_from_sidx(sidx)
|
.server_from_sidx_mut(sidx)
|
||||||
.expect("couldn't re-discover server index");
|
.expect("couldn't re-discover server index");
|
||||||
server.queue_address(pid, context, message, client_address)
|
server.queue_response(pid, context, message, client_address)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a server index based on a SID
|
/// Get a server index based on a SID
|
||||||
|
|
|
||||||
|
|
@ -94,7 +94,7 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes
|
||||||
// If the server has an available context to receive the message,
|
// If the server has an available context to receive the message,
|
||||||
// transfer it right away.
|
// transfer it right away.
|
||||||
if let Some(server_tid) = ss
|
if let Some(server_tid) = ss
|
||||||
.server_from_sidx(sidx)
|
.server_from_sidx_mut(sidx)
|
||||||
.expect("server couldn't be located")
|
.expect("server couldn't be located")
|
||||||
.take_available_thread()
|
.take_available_thread()
|
||||||
{
|
{
|
||||||
|
|
@ -102,10 +102,11 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes
|
||||||
// "There are contexts available to handle this message. Marking PID {} as Ready",
|
// "There are contexts available to handle this message. Marking PID {} as Ready",
|
||||||
// server_pid
|
// server_pid
|
||||||
// );
|
// );
|
||||||
let sender = if message.is_blocking() {
|
let server_cid = ss.server_cid(sidx)?;
|
||||||
|
let sender_idx = if message.is_blocking() {
|
||||||
ss.remember_server_message(sidx, pid, thread, &message, client_address)
|
ss.remember_server_message(sidx, pid, thread, &message, client_address)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
ss.server_from_sidx(sidx)
|
ss.server_from_sidx_mut(sidx)
|
||||||
.expect("server couldn't be located")
|
.expect("server couldn't be located")
|
||||||
.return_available_thread(thread);
|
.return_available_thread(thread);
|
||||||
e
|
e
|
||||||
|
|
@ -113,15 +114,19 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes
|
||||||
} else {
|
} else {
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
|
let sender = SenderID {
|
||||||
|
cid: server_cid,
|
||||||
|
idx: sender_idx,
|
||||||
|
};
|
||||||
let envelope = MessageEnvelope {
|
let envelope = MessageEnvelope {
|
||||||
sender,
|
sender: sender.into(),
|
||||||
body: message,
|
body: message,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Mark the server's context as "Ready". If this fails, return the context
|
// Mark the server's context as "Ready". If this fails, return the context
|
||||||
// to the blocking list.
|
// to the blocking list.
|
||||||
ss.ready_thread(server_pid, server_tid).map_err(|e| {
|
ss.ready_thread(server_pid, server_tid).map_err(|e| {
|
||||||
ss.server_from_sidx(sidx)
|
ss.server_from_sidx_mut(sidx)
|
||||||
.expect("server couldn't be located")
|
.expect("server couldn't be located")
|
||||||
.return_available_thread(thread);
|
.return_available_thread(thread);
|
||||||
e
|
e
|
||||||
|
|
@ -192,15 +197,16 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes
|
||||||
|
|
||||||
fn return_memory(pid: PID, tid: TID, sender: MessageSender, buf: MemoryRange) -> SysCallResult {
|
fn return_memory(pid: PID, tid: TID, sender: MessageSender, buf: MemoryRange) -> SysCallResult {
|
||||||
SystemServices::with_mut(|ss| {
|
SystemServices::with_mut(|ss| {
|
||||||
let sender = SenderID::from_usize(sender)?;
|
let sender = SenderID::from(sender);
|
||||||
|
|
||||||
|
let sidx = ss.sidx_from_cid(sender.cid).ok_or(xous_kernel::Error::ServerNotFound)?;
|
||||||
let server = ss
|
let server = ss
|
||||||
.server_from_sidx(sender.sidx)
|
.server_from_sidx_mut(sidx)
|
||||||
.ok_or(xous_kernel::Error::ServerNotFound)?;
|
.ok_or(xous_kernel::Error::ServerNotFound)?;
|
||||||
if server.pid != pid {
|
if server.pid != pid {
|
||||||
return Err(xous_kernel::Error::ServerNotFound);
|
return Err(xous_kernel::Error::ServerNotFound);
|
||||||
}
|
}
|
||||||
let result = server.take_waiting_message(sender.tidx, buf)?;
|
let result = server.take_waiting_message(sender.idx, Some(&buf))?;
|
||||||
let (client_pid, client_tid, server_addr, client_addr, len) = match result {
|
let (client_pid, client_tid, server_addr, client_addr, len) = match result {
|
||||||
WaitingMessage::BorrowedMemory(
|
WaitingMessage::BorrowedMemory(
|
||||||
client_pid,
|
client_pid,
|
||||||
|
|
@ -230,6 +236,10 @@ fn return_memory(pid: PID, tid: TID, sender: MessageSender, buf: MemoryRange) ->
|
||||||
result
|
result
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
WaitingMessage::ScalarMessage(_pid, _tid) => {
|
||||||
|
println!("WARNING: Tried to wait on a message that was a scalar");
|
||||||
|
return Err(xous_kernel::Error::InternalError);
|
||||||
|
}
|
||||||
WaitingMessage::None => {
|
WaitingMessage::None => {
|
||||||
println!("WARNING: Tried to wait on a message that didn't exist");
|
println!("WARNING: Tried to wait on a message that didn't exist");
|
||||||
return Err(xous_kernel::Error::ProcessNotFound);
|
return Err(xous_kernel::Error::ProcessNotFound);
|
||||||
|
|
@ -267,6 +277,82 @@ fn return_memory(pid: PID, tid: TID, sender: MessageSender, buf: MemoryRange) ->
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn return_scalar(pid: PID, _tid: TID, sender: MessageSender, arg: usize) -> SysCallResult {
|
||||||
|
SystemServices::with_mut(|ss| {
|
||||||
|
let sender = SenderID::from(sender);
|
||||||
|
|
||||||
|
let sidx = ss.sidx_from_cid(sender.cid).ok_or(xous_kernel::Error::ServerNotFound)?;
|
||||||
|
let server = ss
|
||||||
|
.server_from_sidx_mut(sidx)
|
||||||
|
.ok_or(xous_kernel::Error::ServerNotFound)?;
|
||||||
|
if server.pid != pid {
|
||||||
|
return Err(xous_kernel::Error::ServerNotFound);
|
||||||
|
}
|
||||||
|
let result = server.take_waiting_message(sender.idx, None)?;
|
||||||
|
let (client_pid, client_tid) = match result {
|
||||||
|
WaitingMessage::ScalarMessage(pid, tid) => (pid, tid),
|
||||||
|
WaitingMessage::ForgetMemory(_) => {
|
||||||
|
println!("WARNING: Tried to wait on a scalar message that was actually forgettingmemory");
|
||||||
|
return Err(xous_kernel::Error::ProcessNotFound);
|
||||||
|
}
|
||||||
|
WaitingMessage::BorrowedMemory(_, _, _, _, _) => {
|
||||||
|
println!("WARNING: Tried to wait on a scalar message that was actually borrowed memory");
|
||||||
|
return Err(xous_kernel::Error::ProcessNotFound);
|
||||||
|
}
|
||||||
|
WaitingMessage::MovedMemory => {
|
||||||
|
println!("WARNING: Tried to wait on a scalar message that was actually moved memory");
|
||||||
|
return Err(xous_kernel::Error::ProcessNotFound);
|
||||||
|
}
|
||||||
|
WaitingMessage::None => {
|
||||||
|
println!("WARNING: Tried to wait on a message that didn't exist");
|
||||||
|
return Err(xous_kernel::Error::ProcessNotFound);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
ss.ready_thread(client_pid, client_tid)?;
|
||||||
|
ss.switch_to_thread(client_pid, Some(client_tid))?;
|
||||||
|
ss.set_thread_result(client_pid, client_tid, xous_kernel::Result::Scalar1(arg))?;
|
||||||
|
Ok(xous_kernel::Result::Ok)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn return_scalar2(pid: PID, _tid: TID, sender: MessageSender, arg1: usize, arg2: usize) -> SysCallResult {
|
||||||
|
SystemServices::with_mut(|ss| {
|
||||||
|
let sender = SenderID::from(sender);
|
||||||
|
|
||||||
|
let sidx = ss.sidx_from_cid(sender.cid).ok_or(xous_kernel::Error::ServerNotFound)?;
|
||||||
|
let server = ss
|
||||||
|
.server_from_sidx_mut(sidx)
|
||||||
|
.ok_or(xous_kernel::Error::ServerNotFound)?;
|
||||||
|
if server.pid != pid {
|
||||||
|
return Err(xous_kernel::Error::ServerNotFound);
|
||||||
|
}
|
||||||
|
let result = server.take_waiting_message(sender.idx, None)?;
|
||||||
|
let (client_pid, client_tid) = match result {
|
||||||
|
WaitingMessage::ScalarMessage(pid, tid) => (pid, tid),
|
||||||
|
WaitingMessage::ForgetMemory(_) => {
|
||||||
|
println!("WARNING: Tried to wait on a scalar message that was actually forgetting memory");
|
||||||
|
return Err(xous_kernel::Error::ProcessNotFound);
|
||||||
|
}
|
||||||
|
WaitingMessage::BorrowedMemory(_, _, _, _, _) => {
|
||||||
|
println!("WARNING: Tried to wait on a scalar message that was actually borrowed memory");
|
||||||
|
return Err(xous_kernel::Error::ProcessNotFound);
|
||||||
|
}
|
||||||
|
WaitingMessage::MovedMemory => {
|
||||||
|
println!("WARNING: Tried to wait on a scalar message that was actually moved memory");
|
||||||
|
return Err(xous_kernel::Error::ProcessNotFound);
|
||||||
|
}
|
||||||
|
WaitingMessage::None => {
|
||||||
|
println!("WARNING: Tried to wait on a message that didn't exist");
|
||||||
|
return Err(xous_kernel::Error::ProcessNotFound);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
ss.ready_thread(client_pid, client_tid)?;
|
||||||
|
ss.switch_to_thread(client_pid, Some(client_tid))?;
|
||||||
|
ss.set_thread_result(client_pid, client_tid, xous_kernel::Result::Scalar2(arg1, arg2))?;
|
||||||
|
Ok(xous_kernel::Result::Ok)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn receive_message(pid: PID, tid: TID, sid: SID) -> SysCallResult {
|
fn receive_message(pid: PID, tid: TID, sid: SID) -> SysCallResult {
|
||||||
SystemServices::with_mut(|ss| {
|
SystemServices::with_mut(|ss| {
|
||||||
assert!(
|
assert!(
|
||||||
|
|
@ -274,11 +360,12 @@ fn receive_message(pid: PID, tid: TID, sid: SID) -> SysCallResult {
|
||||||
"current thread is not running"
|
"current thread is not running"
|
||||||
);
|
);
|
||||||
// See if there is a pending message. If so, return immediately.
|
// See if there is a pending message. If so, return immediately.
|
||||||
|
let cid = ss.connect_to_server(sid)?;
|
||||||
let sidx = ss
|
let sidx = ss
|
||||||
.server_sidx(sid)
|
.server_sidx(sid)
|
||||||
.ok_or(xous_kernel::Error::ServerNotFound)?;
|
.ok_or(xous_kernel::Error::ServerNotFound)?;
|
||||||
let server = ss
|
let server = ss
|
||||||
.server_from_sidx(sidx)
|
.server_from_sidx_mut(sidx)
|
||||||
.ok_or(xous_kernel::Error::ServerNotFound)?;
|
.ok_or(xous_kernel::Error::ServerNotFound)?;
|
||||||
// server.print_queue();
|
// server.print_queue();
|
||||||
|
|
||||||
|
|
@ -288,8 +375,7 @@ fn receive_message(pid: PID, tid: TID, sid: SID) -> SysCallResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there is a pending message, return it immediately.
|
// If there is a pending message, return it immediately.
|
||||||
if let Some(msg) = server.take_next_message(sidx) {
|
if let Some(msg) = server.take_next_message(cid) {
|
||||||
// println!("PID {} had a message ready -- returning it", pid);
|
|
||||||
return Ok(xous_kernel::Result::Message(msg));
|
return Ok(xous_kernel::Result::Message(msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -531,13 +617,16 @@ pub fn handle_inner(pid: PID, tid: TID, call: SysCall) -> SysCallResult {
|
||||||
}),
|
}),
|
||||||
SysCall::CreateServer(name) => SystemServices::with_mut(|ss| {
|
SysCall::CreateServer(name) => SystemServices::with_mut(|ss| {
|
||||||
ss.create_server(pid, name)
|
ss.create_server(pid, name)
|
||||||
.map(xous_kernel::Result::ServerID)
|
.map(|(sid, cid)| xous_kernel::Result::NewServerID(sid, cid))
|
||||||
}),
|
}),
|
||||||
SysCall::TryConnect(sid) => SystemServices::with_mut(|ss| {
|
SysCall::TryConnect(sid) => SystemServices::with_mut(|ss| {
|
||||||
ss.connect_to_server(sid)
|
ss.connect_to_server(sid)
|
||||||
.map(xous_kernel::Result::ConnectionID)
|
.map(xous_kernel::Result::ConnectionID)
|
||||||
}),
|
}),
|
||||||
SysCall::ReturnMemory(sender, buf) => return_memory(pid, tid, sender, buf),
|
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::TrySendMessage(cid, message) => send_message(pid, tid, cid, message),
|
||||||
SysCall::TerminateProcess => SystemServices::with_mut(|ss| {
|
SysCall::TerminateProcess => SystemServices::with_mut(|ss| {
|
||||||
ss.switch_from_thread(pid, tid)?;
|
ss.switch_from_thread(pid, tid)?;
|
||||||
|
|
@ -557,6 +646,6 @@ pub fn handle_inner(pid: PID, tid: TID, call: SysCall) -> SysCallResult {
|
||||||
// SystemServices::with_mut(|ss| ss.connect_to_server(sid).map(xous_kernel::Result::ConnectionID))
|
// SystemServices::with_mut(|ss| ss.connect_to_server(sid).map(xous_kernel::Result::ConnectionID))
|
||||||
// }
|
// }
|
||||||
// SysCall::SendMessage(cid, message) => send_message(pid, tid, cid, message),
|
// SysCall::SendMessage(cid, message) => send_message(pid, tid, cid, message),
|
||||||
_ => Err(xous_kernel::Error::UnhandledSyscall),
|
_ => panic!("Unhandled Syscall: {:?}", call), //Err(xous_kernel::Error::UnhandledSyscall),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -179,6 +179,95 @@ fn send_scalar_message() {
|
||||||
main_thread.join().expect("couldn't join kernel process");
|
main_thread.join().expect("couldn't join kernel process");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn send_blocking_scalar_message() {
|
||||||
|
// Start the server in another thread
|
||||||
|
let main_thread = start_kernel(SERVER_SPEC);
|
||||||
|
|
||||||
|
let (server_addr_send, server_addr_recv) = channel();
|
||||||
|
|
||||||
|
// Spawn the server "process" (which just lives in a separate thread)
|
||||||
|
// and receive the message. Note that we need to communicate to the
|
||||||
|
// "Client" what our server ID is. Normally this would be done via
|
||||||
|
// an external nameserver.
|
||||||
|
let xous_server = xous_kernel::create_process_as_thread(xous_kernel::ProcessArgsAsThread::new(
|
||||||
|
"send_scalar_message server",
|
||||||
|
move || {
|
||||||
|
let sid = xous_kernel::create_server(b"send_scalar_mesg")
|
||||||
|
.expect("couldn't create test server");
|
||||||
|
server_addr_send.send(sid).unwrap();
|
||||||
|
let envelope = xous_kernel::receive_message(sid).expect("couldn't receive messages");
|
||||||
|
assert_eq!(
|
||||||
|
envelope.body,
|
||||||
|
xous_kernel::Message::BlockingScalar(xous_kernel::ScalarMessage {
|
||||||
|
id: 1,
|
||||||
|
arg1: 2,
|
||||||
|
arg2: 3,
|
||||||
|
arg3: 4,
|
||||||
|
arg4: 5
|
||||||
|
})
|
||||||
|
);
|
||||||
|
xous_kernel::return_scalar(envelope.sender, 42).expect("couldn't return scalar");
|
||||||
|
|
||||||
|
let envelope = xous_kernel::receive_message(sid).expect("couldn't receive messages");
|
||||||
|
assert_eq!(
|
||||||
|
envelope.body,
|
||||||
|
xous_kernel::Message::BlockingScalar(xous_kernel::ScalarMessage {
|
||||||
|
id: 6,
|
||||||
|
arg1: 7,
|
||||||
|
arg2: 8,
|
||||||
|
arg3: 9,
|
||||||
|
arg4: 10
|
||||||
|
})
|
||||||
|
);
|
||||||
|
xous_kernel::return_scalar2(envelope.sender, 56, 78).expect("couldn't return scalar");
|
||||||
|
},
|
||||||
|
))
|
||||||
|
.expect("couldn't spawn server process");
|
||||||
|
|
||||||
|
// Spawn the client "process" and wait for the server address.
|
||||||
|
let xous_client = xous_kernel::create_process_as_thread(xous_kernel::ProcessArgsAsThread::new(
|
||||||
|
"send_scalar_message client",
|
||||||
|
move || {
|
||||||
|
let sid = server_addr_recv.recv().unwrap();
|
||||||
|
let conn = xous_kernel::try_connect(sid).expect("couldn't connect to server");
|
||||||
|
let result = xous_kernel::try_send_message(
|
||||||
|
conn,
|
||||||
|
xous_kernel::Message::BlockingScalar(xous_kernel::ScalarMessage {
|
||||||
|
id: 1,
|
||||||
|
arg1: 2,
|
||||||
|
arg2: 3,
|
||||||
|
arg3: 4,
|
||||||
|
arg4: 5,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.expect("couldn't send message");
|
||||||
|
assert_eq!(result, xous_kernel::Result::Scalar1(42));
|
||||||
|
|
||||||
|
let result = xous_kernel::try_send_message(
|
||||||
|
conn,
|
||||||
|
xous_kernel::Message::BlockingScalar(xous_kernel::ScalarMessage {
|
||||||
|
id: 6,
|
||||||
|
arg1: 7,
|
||||||
|
arg2: 8,
|
||||||
|
arg3: 9,
|
||||||
|
arg4: 10,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.expect("couldn't send message");
|
||||||
|
assert_eq!(result, xous_kernel::Result::Scalar2(56, 78));
|
||||||
|
},
|
||||||
|
))
|
||||||
|
.expect("couldn't spawn client process");
|
||||||
|
|
||||||
|
// Wait for both processes to finish
|
||||||
|
crate::wait_process_as_thread(xous_server).expect("couldn't join server process");
|
||||||
|
crate::wait_process_as_thread(xous_client).expect("couldn't join client process");
|
||||||
|
shutdown_kernel();
|
||||||
|
|
||||||
|
main_thread.join().expect("couldn't join kernel process");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn send_move_message() {
|
fn send_move_message() {
|
||||||
let test_str = "Hello, world!";
|
let test_str = "Hello, world!";
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue