mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-19 09:00:54 +00:00
kernel: rework server structures
They are now inverted and mvoed into their own file. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
484e21fd30
commit
e298f125c9
5 changed files with 321 additions and 271 deletions
46
kernel/src/macros.rs
Normal file
46
kernel/src/macros.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
// Shamelessly taken from
|
||||||
|
// https://stackoverflow.com/questions/36258417/using-a-macro-to-initialize-a-big-array-of-non-copy-elements
|
||||||
|
// Allows us to fill an array with a predefined value.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! filled_array {
|
||||||
|
(@accum (0, $($_es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@as_expr [$($body)*])};
|
||||||
|
(@accum (1, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (0, $($es),*) -> ($($body)* $($es,)*))};
|
||||||
|
(@accum (2, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (0, $($es),*) -> ($($body)* $($es,)* $($es,)*))};
|
||||||
|
(@accum (3, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (2, $($es),*) -> ($($body)* $($es,)*))};
|
||||||
|
(@accum (4, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (2, $($es,)* $($es),*) -> ($($body)*))};
|
||||||
|
(@accum (5, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (4, $($es),*) -> ($($body)* $($es,)*))};
|
||||||
|
(@accum (6, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (4, $($es),*) -> ($($body)* $($es,)* $($es,)*))};
|
||||||
|
(@accum (7, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (4, $($es),*) -> ($($body)* $($es,)* $($es,)* $($es,)*))};
|
||||||
|
(@accum (8, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (4, $($es,)* $($es),*) -> ($($body)*))};
|
||||||
|
(@accum (16, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (8, $($es,)* $($es),*) -> ($($body)*))};
|
||||||
|
(@accum (32, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (16, $($es,)* $($es),*) -> ($($body)*))};
|
||||||
|
(@accum (64, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (32, $($es,)* $($es),*) -> ($($body)*))};
|
||||||
|
(@accum (128, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (64, $($es,)* $($es),*) -> ($($body)*))};
|
||||||
|
(@accum (256, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (128, $($es,)* $($es),*) -> ($($body)*))};
|
||||||
|
(@accum (512, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (256, $($es,)* $($es),*) -> ($($body)*))};
|
||||||
|
(@accum (1024, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (512, $($es,)* $($es),*) -> ($($body)*))};
|
||||||
|
(@accum (2048, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (1024, $($es,)* $($es),*) -> ($($body)*))};
|
||||||
|
(@accum (4096, $($es:expr),*) -> ($($body:tt)*))
|
||||||
|
=> {filled_array!(@accum (2048, $($es,)* $($es),*) -> ($($body)*))};
|
||||||
|
|
||||||
|
(@as_expr $e:expr) => {$e};
|
||||||
|
|
||||||
|
[$e:expr; $n:tt] => { filled_array!(@accum ($n, $e) -> ()) };
|
||||||
|
}
|
||||||
|
|
@ -15,9 +15,11 @@ mod arch;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod args;
|
mod args;
|
||||||
mod irq;
|
mod irq;
|
||||||
|
mod macros;
|
||||||
mod mem;
|
mod mem;
|
||||||
mod services;
|
mod services;
|
||||||
mod syscall;
|
mod syscall;
|
||||||
|
mod server;
|
||||||
|
|
||||||
use mem::MemoryManagerHandle;
|
use mem::MemoryManagerHandle;
|
||||||
use services::SystemServicesHandle;
|
use services::SystemServicesHandle;
|
||||||
|
|
@ -57,7 +59,7 @@ fn next_pid_to_run(last_pid: Option<PID>) -> Option<PID> {
|
||||||
// PIDs are 1-indexed but arrays are 0-indexed. By not subtracting
|
// PIDs are 1-indexed but arrays are 0-indexed. By not subtracting
|
||||||
// 1 from the PID when we use it as an array index, we automatically
|
// 1 from the PID when we use it as an array index, we automatically
|
||||||
// pick the next process in the list.
|
// pick the next process in the list.
|
||||||
let mut current_pid = last_pid.unwrap_or(0) as usize + 1;
|
let current_pid = last_pid.unwrap_or(0) as usize;
|
||||||
|
|
||||||
let system_services = SystemServicesHandle::get();
|
let system_services = SystemServicesHandle::get();
|
||||||
for test_idx in current_pid..system_services.processes.len() {
|
for test_idx in current_pid..system_services.processes.len() {
|
||||||
|
|
|
||||||
233
kernel/src/server.rs
Normal file
233
kernel/src/server.rs
Normal file
|
|
@ -0,0 +1,233 @@
|
||||||
|
pub use crate::arch::ProcessContext;
|
||||||
|
use core::{mem, slice};
|
||||||
|
use xous::{MemoryAddress, MemorySize, PID, SID};
|
||||||
|
|
||||||
|
/// Internal representation of a queued message for a server.
|
||||||
|
/// This should be exactly 8 words / 32 bytes, yielding 128
|
||||||
|
/// queued messages per server
|
||||||
|
#[repr(usize)]
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
enum QueuedMessage {
|
||||||
|
Empty,
|
||||||
|
ScalarMessage(
|
||||||
|
usize, /* sender */
|
||||||
|
usize, /* context */
|
||||||
|
usize, /* id */
|
||||||
|
usize, /* arg1 */
|
||||||
|
usize, /* arg2 */
|
||||||
|
usize, /* arg3 */
|
||||||
|
usize, /* arg4 */
|
||||||
|
),
|
||||||
|
MemoryMessageSend(
|
||||||
|
usize, /* sender */
|
||||||
|
usize, /* context */
|
||||||
|
usize, /* id */
|
||||||
|
usize, /* buf */
|
||||||
|
usize, /* buf_size */
|
||||||
|
usize, /* offset */
|
||||||
|
usize, /* valid */
|
||||||
|
),
|
||||||
|
MemoryMessageROLend(
|
||||||
|
usize, /* sender */
|
||||||
|
usize, /* context */
|
||||||
|
usize, /* id */
|
||||||
|
usize, /* buf */
|
||||||
|
usize, /* buf_size */
|
||||||
|
usize, /* offset */
|
||||||
|
usize, /* valid */
|
||||||
|
),
|
||||||
|
MemoryMessageRWLend(
|
||||||
|
usize, /* sender */
|
||||||
|
usize, /* context */
|
||||||
|
usize, /* id */
|
||||||
|
usize, /* buf */
|
||||||
|
usize, /* buf_size */
|
||||||
|
usize, /* offset */
|
||||||
|
usize, /* valid */
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A pointer to resolve a server ID to a particular process
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
pub struct Server {
|
||||||
|
/// A randomly-generated ID
|
||||||
|
pub sid: SID,
|
||||||
|
|
||||||
|
/// The process that owns this server
|
||||||
|
pub pid: PID,
|
||||||
|
|
||||||
|
/// An index into the queue
|
||||||
|
queue_head: usize,
|
||||||
|
|
||||||
|
queue_tail: usize,
|
||||||
|
|
||||||
|
/// Where data will appear
|
||||||
|
queue: &'static mut [QueuedMessage],
|
||||||
|
|
||||||
|
/// The `context mask` is a bitfield of contexts that are
|
||||||
|
/// able to handle this message.
|
||||||
|
/// If there are no available contexts, then messages will
|
||||||
|
/// need to be queued.
|
||||||
|
ready_contexts: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Server {
|
||||||
|
pub fn init(
|
||||||
|
new: &mut Option<Server>,
|
||||||
|
pid: PID,
|
||||||
|
sid: SID,
|
||||||
|
queue_addr: *mut usize,
|
||||||
|
queue_size: usize,
|
||||||
|
) -> Result<(), xous::Error> {
|
||||||
|
if new != &None {
|
||||||
|
return Err(xous::Error::MemoryInUse);
|
||||||
|
}
|
||||||
|
|
||||||
|
let queue = unsafe {
|
||||||
|
slice::from_raw_parts_mut(
|
||||||
|
queue_addr as *mut QueuedMessage,
|
||||||
|
queue_size / mem::size_of::<QueuedMessage>(),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
*new = Some(Server {
|
||||||
|
sid,
|
||||||
|
pid,
|
||||||
|
queue_head: 0,
|
||||||
|
queue_tail: 0,
|
||||||
|
queue,
|
||||||
|
ready_contexts: 0,
|
||||||
|
});
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
/// Remove a message from the server's queue and replace it with
|
||||||
|
/// QueuedMessage::Empty. Advance the queue pointer while we're at it.
|
||||||
|
pub fn take_next_message(&mut self) -> Option<(xous::MessageEnvelope, usize)> {
|
||||||
|
let result = match self.queue[self.queue_head] {
|
||||||
|
QueuedMessage::Empty => return None,
|
||||||
|
QueuedMessage::MemoryMessageROLend(
|
||||||
|
sender,
|
||||||
|
context,
|
||||||
|
id,
|
||||||
|
buf,
|
||||||
|
buf_size,
|
||||||
|
offset,
|
||||||
|
valid,
|
||||||
|
) => (
|
||||||
|
xous::MessageEnvelope {
|
||||||
|
sender: sender,
|
||||||
|
message: xous::Message::ImmutableBorrow(xous::MemoryMessage {
|
||||||
|
id,
|
||||||
|
buf: MemoryAddress::new(buf),
|
||||||
|
buf_size: MemorySize::new(buf_size),
|
||||||
|
_offset: MemorySize::new(offset),
|
||||||
|
_valid: MemorySize::new(valid),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
context,
|
||||||
|
),
|
||||||
|
QueuedMessage::MemoryMessageRWLend(
|
||||||
|
sender,
|
||||||
|
context,
|
||||||
|
id,
|
||||||
|
buf,
|
||||||
|
buf_size,
|
||||||
|
offset,
|
||||||
|
valid,
|
||||||
|
) => (
|
||||||
|
xous::MessageEnvelope {
|
||||||
|
sender: sender,
|
||||||
|
message: xous::Message::MutableBorrow(xous::MemoryMessage {
|
||||||
|
id,
|
||||||
|
buf: MemoryAddress::new(buf),
|
||||||
|
buf_size: MemorySize::new(buf_size),
|
||||||
|
_offset: MemorySize::new(offset),
|
||||||
|
_valid: MemorySize::new(valid),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
context,
|
||||||
|
),
|
||||||
|
QueuedMessage::MemoryMessageSend(sender, context, id, buf, buf_size, offset, valid) => {
|
||||||
|
(
|
||||||
|
xous::MessageEnvelope {
|
||||||
|
sender: sender,
|
||||||
|
message: xous::Message::Move(xous::MemoryMessage {
|
||||||
|
id,
|
||||||
|
buf: MemoryAddress::new(buf),
|
||||||
|
buf_size: MemorySize::new(buf_size),
|
||||||
|
_offset: MemorySize::new(offset),
|
||||||
|
_valid: MemorySize::new(valid),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
context,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
QueuedMessage::ScalarMessage(sender, context, id, arg1, arg2, arg3, arg4) => (
|
||||||
|
xous::MessageEnvelope {
|
||||||
|
sender: sender,
|
||||||
|
message: xous::Message::Scalar(xous::ScalarMessage {
|
||||||
|
id,
|
||||||
|
arg1,
|
||||||
|
arg2,
|
||||||
|
arg3,
|
||||||
|
arg4,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
context,
|
||||||
|
),
|
||||||
|
};
|
||||||
|
self.queue[self.queue_head] = QueuedMessage::Empty;
|
||||||
|
self.queue_head += 1;
|
||||||
|
if self.queue_head >= self.queue.len() {
|
||||||
|
self.queue_head = 0;
|
||||||
|
}
|
||||||
|
Some(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Add the given message to this server's queue.
|
||||||
|
pub fn queue_message(
|
||||||
|
&mut self,
|
||||||
|
envelope: xous::MessageEnvelope,
|
||||||
|
context: usize,
|
||||||
|
) -> core::result::Result<(), xous::Error> {
|
||||||
|
if self.queue[self.queue_head] != QueuedMessage::Empty {
|
||||||
|
return Err(xous::Error::ServerQueueFull);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.queue_head += 1;
|
||||||
|
if self.queue_head >= self.queue.len() {
|
||||||
|
self.queue_head = 0;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// assert!(
|
||||||
|
// mem::size_of::<QueuedMessage>() == 32,
|
||||||
|
// "QueuedMessage was supposed to be 32 bytes, but instead was {} bytes",
|
||||||
|
// mem::size_of::<QueuedMessage>()
|
||||||
|
// );
|
||||||
|
|
||||||
|
/// Return a context ID that is available and blocking. If no such context ID exists,
|
||||||
|
/// or if this server isn't actually ready to receive packets, return None.
|
||||||
|
pub fn take_available_context(&mut self) -> Option<usize> {
|
||||||
|
if self.ready_contexts == 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let mut test_ctx_mask = 1;
|
||||||
|
let mut ctx_number = 0;
|
||||||
|
loop {
|
||||||
|
// If the context mask matches this context number, remove it
|
||||||
|
// and return the index.
|
||||||
|
if self.ready_contexts & test_ctx_mask == test_ctx_mask {
|
||||||
|
self.ready_contexts = self.ready_contexts & !test_ctx_mask;
|
||||||
|
return Some(ctx_number);
|
||||||
|
}
|
||||||
|
// Advance to the next slot.
|
||||||
|
test_ctx_mask = test_ctx_mask.rotate_left(1);
|
||||||
|
ctx_number = ctx_number + 1;
|
||||||
|
if test_ctx_mask == 1 {
|
||||||
|
panic!("didn't find a free context, even though there should be one");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,9 +3,11 @@ use crate::arch::mem::MemoryMapping;
|
||||||
use crate::arch::process::ProcessHandle;
|
use crate::arch::process::ProcessHandle;
|
||||||
pub use crate::arch::ProcessContext;
|
pub use crate::arch::ProcessContext;
|
||||||
use crate::args::KernelArguments;
|
use crate::args::KernelArguments;
|
||||||
|
use crate::filled_array;
|
||||||
use crate::mem::{MemoryManagerHandle, PAGE_SIZE};
|
use crate::mem::{MemoryManagerHandle, PAGE_SIZE};
|
||||||
|
use crate::server::Server;
|
||||||
use core::{mem, slice};
|
use core::{mem, slice};
|
||||||
use xous::{MemoryAddress, MemoryFlags, MemorySize, CID, PID, SID};
|
use xous::{ MemoryFlags, CID, PID, SID};
|
||||||
|
|
||||||
const MAX_PROCESS_COUNT: usize = 32;
|
const MAX_PROCESS_COUNT: usize = 32;
|
||||||
const MAX_SERVER_COUNT: usize = 32;
|
const MAX_SERVER_COUNT: usize = 32;
|
||||||
|
|
@ -125,229 +127,6 @@ impl Process {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq)]
|
|
||||||
#[repr(usize)]
|
|
||||||
enum ServerState {
|
|
||||||
/// This server slot is unallocated
|
|
||||||
Free,
|
|
||||||
|
|
||||||
/// This server can receive messages.
|
|
||||||
/// The `context mask` is a bitfield of contexts that are
|
|
||||||
/// able to handle this message.
|
|
||||||
/// If there are no available contexts, then messages will
|
|
||||||
/// need to be queued.
|
|
||||||
Ready(usize /* context mask */),
|
|
||||||
|
|
||||||
/// This server's inbox is full
|
|
||||||
Full,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Internal representation of a queued message for a server.
|
|
||||||
/// This should be exactly 8 words / 32 bytes, yielding 128
|
|
||||||
/// queued messages per server
|
|
||||||
#[repr(usize)]
|
|
||||||
#[derive(PartialEq)]
|
|
||||||
enum QueuedMessage {
|
|
||||||
Empty,
|
|
||||||
ScalarMessage(
|
|
||||||
usize, /* sender */
|
|
||||||
usize, /* context */
|
|
||||||
usize, /* id */
|
|
||||||
usize, /* arg1 */
|
|
||||||
usize, /* arg2 */
|
|
||||||
usize, /* arg3 */
|
|
||||||
usize, /* arg4 */
|
|
||||||
),
|
|
||||||
MemoryMessageSend(
|
|
||||||
usize, /* sender */
|
|
||||||
usize, /* context */
|
|
||||||
usize, /* id */
|
|
||||||
usize, /* buf */
|
|
||||||
usize, /* buf_size */
|
|
||||||
usize, /* offset */
|
|
||||||
usize, /* valid */
|
|
||||||
),
|
|
||||||
MemoryMessageROLend(
|
|
||||||
usize, /* sender */
|
|
||||||
usize, /* context */
|
|
||||||
usize, /* id */
|
|
||||||
usize, /* buf */
|
|
||||||
usize, /* buf_size */
|
|
||||||
usize, /* offset */
|
|
||||||
usize, /* valid */
|
|
||||||
),
|
|
||||||
MemoryMessageRWLend(
|
|
||||||
usize, /* sender */
|
|
||||||
usize, /* context */
|
|
||||||
usize, /* id */
|
|
||||||
usize, /* buf */
|
|
||||||
usize, /* buf_size */
|
|
||||||
usize, /* offset */
|
|
||||||
usize, /* valid */
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A pointer to resolve a server ID to a particular process
|
|
||||||
#[derive(Copy, Clone)]
|
|
||||||
pub struct Server {
|
|
||||||
/// A randomly-generated ID
|
|
||||||
sid: SID,
|
|
||||||
|
|
||||||
/// The process that owns this server
|
|
||||||
pub pid: PID,
|
|
||||||
|
|
||||||
/// The current state of this slot
|
|
||||||
state: ServerState,
|
|
||||||
|
|
||||||
/// An index into the queue
|
|
||||||
queue_index: usize,
|
|
||||||
|
|
||||||
/// Where data will appear
|
|
||||||
queue: &'static [QueuedMessage],
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Server {
|
|
||||||
/// Remove a message from the server's queue and replace it with
|
|
||||||
/// QueuedMessage::Empty. Advance the queue pointer while we're at it.
|
|
||||||
pub fn take_next_message(&mut self) -> Option<(xous::MessageEnvelope, usize)> {
|
|
||||||
let result = match self.queue[self.queue_index] {
|
|
||||||
QueuedMessage::Empty => return None,
|
|
||||||
QueuedMessage::MemoryMessageROLend(
|
|
||||||
sender,
|
|
||||||
context,
|
|
||||||
id,
|
|
||||||
buf,
|
|
||||||
buf_size,
|
|
||||||
offset,
|
|
||||||
valid,
|
|
||||||
) => (
|
|
||||||
xous::MessageEnvelope {
|
|
||||||
sender: sender,
|
|
||||||
message: xous::Message::ImmutableBorrow(xous::MemoryMessage {
|
|
||||||
id,
|
|
||||||
buf: MemoryAddress::new(buf),
|
|
||||||
buf_size: MemorySize::new(buf_size),
|
|
||||||
_offset: MemorySize::new(offset),
|
|
||||||
_valid: MemorySize::new(valid),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
context,
|
|
||||||
),
|
|
||||||
QueuedMessage::MemoryMessageRWLend(
|
|
||||||
sender,
|
|
||||||
context,
|
|
||||||
id,
|
|
||||||
buf,
|
|
||||||
buf_size,
|
|
||||||
offset,
|
|
||||||
valid,
|
|
||||||
) => (
|
|
||||||
xous::MessageEnvelope {
|
|
||||||
sender: sender,
|
|
||||||
message: xous::Message::MutableBorrow(xous::MemoryMessage {
|
|
||||||
id,
|
|
||||||
buf: MemoryAddress::new(buf),
|
|
||||||
buf_size: MemorySize::new(buf_size),
|
|
||||||
_offset: MemorySize::new(offset),
|
|
||||||
_valid: MemorySize::new(valid),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
context,
|
|
||||||
),
|
|
||||||
QueuedMessage::MemoryMessageSend(sender, context, id, buf, buf_size, offset, valid) => {
|
|
||||||
(
|
|
||||||
xous::MessageEnvelope {
|
|
||||||
sender: sender,
|
|
||||||
message: xous::Message::Move(xous::MemoryMessage {
|
|
||||||
id,
|
|
||||||
buf: MemoryAddress::new(buf),
|
|
||||||
buf_size: MemorySize::new(buf_size),
|
|
||||||
_offset: MemorySize::new(offset),
|
|
||||||
_valid: MemorySize::new(valid),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
context,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
QueuedMessage::ScalarMessage(sender, context, id, arg1, arg2, arg3, arg4) => (
|
|
||||||
xous::MessageEnvelope {
|
|
||||||
sender: sender,
|
|
||||||
message: xous::Message::Scalar(xous::ScalarMessage {
|
|
||||||
id,
|
|
||||||
arg1,
|
|
||||||
arg2,
|
|
||||||
arg3,
|
|
||||||
arg4,
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
context,
|
|
||||||
),
|
|
||||||
};
|
|
||||||
self.queue[self.queue_index] = QueuedMessage::Empty;
|
|
||||||
self.queue_index += 1;
|
|
||||||
if self.queue_index >= self.queue.len() {
|
|
||||||
self.queue_index = 0;
|
|
||||||
}
|
|
||||||
Some(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Add the given message to this server's queue.
|
|
||||||
pub fn queue_message(
|
|
||||||
&mut self,
|
|
||||||
envelope: xous::MessageEnvelope,
|
|
||||||
context: usize,
|
|
||||||
) -> core::result::Result<(), xous::Error> {
|
|
||||||
if self.queue[self.queue_index] != QueuedMessage::Empty {
|
|
||||||
return Err(xous::Error::ServerQueueFull);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.queue_index += 1;
|
|
||||||
if self.queue_index >= self.queue.len() {
|
|
||||||
self.queue_index = 0;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return a context ID that is available and blocking. If no such context ID exists,
|
|
||||||
/// or if this server isn't actually ready to receive packets, return None.
|
|
||||||
pub fn take_available_context(&mut self) -> Option<usize> {
|
|
||||||
let context_index = match self.state {
|
|
||||||
ServerState::Ready(0) => return None,
|
|
||||||
ServerState::Ready(x) => x,
|
|
||||||
_ => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut test_ctx_mask = 1;
|
|
||||||
let mut ctx_number = 0;
|
|
||||||
loop {
|
|
||||||
// If the context mask matches this context number, remove it
|
|
||||||
// and return the index.
|
|
||||||
if context_index & test_ctx_mask == test_ctx_mask {
|
|
||||||
self.state = ServerState::Ready(context_index & !test_ctx_mask);
|
|
||||||
return Some(ctx_number);
|
|
||||||
}
|
|
||||||
// Advance to the next slot.
|
|
||||||
test_ctx_mask = test_ctx_mask.rotate_left(1);
|
|
||||||
ctx_number = ctx_number + 1;
|
|
||||||
if test_ctx_mask == 1 {
|
|
||||||
panic!("didn't find a free context, even though there should be one");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Server {
|
|
||||||
fn default() -> Self {
|
|
||||||
Server {
|
|
||||||
sid: (0, 0, 0, 0),
|
|
||||||
pid: 0,
|
|
||||||
state: ServerState::Free,
|
|
||||||
queue_index: 0,
|
|
||||||
queue: &[],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
/// The stage1 bootloader sets up some initial processes. These are reported
|
/// The stage1 bootloader sets up some initial processes. These are reported
|
||||||
/// to us as (satp, entrypoint, sp) tuples, which can be turned into a structure.
|
/// to us as (satp, entrypoint, sp) tuples, which can be turned into a structure.
|
||||||
|
|
@ -374,7 +153,7 @@ pub struct SystemServices {
|
||||||
pub processes: [Process; MAX_PROCESS_COUNT],
|
pub processes: [Process; MAX_PROCESS_COUNT],
|
||||||
|
|
||||||
/// A table of all servers in the system
|
/// A table of all servers in the system
|
||||||
servers: [Server; MAX_SERVER_COUNT],
|
servers: [Option<Server>; MAX_SERVER_COUNT],
|
||||||
|
|
||||||
/// A log of the currently-active syscall depth
|
/// A log of the currently-active syscall depth
|
||||||
syscall_stack: [(usize, usize); 3],
|
syscall_stack: [(usize, usize); 3],
|
||||||
|
|
@ -390,13 +169,7 @@ static mut SYSTEM_SERVICES: SystemServices = SystemServices {
|
||||||
ppid: 0,
|
ppid: 0,
|
||||||
mapping: arch::mem::DEFAULT_MEMORY_MAPPING,
|
mapping: arch::mem::DEFAULT_MEMORY_MAPPING,
|
||||||
}; MAX_PROCESS_COUNT],
|
}; MAX_PROCESS_COUNT],
|
||||||
servers: [Server {
|
servers: filled_array![None; 32], // Note we can't use MAX_SERVER_COUNT here because of how Rust's tokenization works
|
||||||
sid: (0, 0, 0, 0),
|
|
||||||
pid: 0,
|
|
||||||
state: ServerState::Free,
|
|
||||||
queue_index: 0,
|
|
||||||
queue: &[],
|
|
||||||
}; MAX_SERVER_COUNT],
|
|
||||||
syscall_stack: [(0, 0), (0, 0), (0, 0)],
|
syscall_stack: [(0, 0), (0, 0), (0, 0)],
|
||||||
syscall_depth: 0,
|
syscall_depth: 0,
|
||||||
};
|
};
|
||||||
|
|
@ -637,33 +410,22 @@ impl SystemServices {
|
||||||
pub fn create_server(&mut self, name: usize) -> Result<SID, xous::Error> {
|
pub fn create_server(&mut self, name: usize) -> Result<SID, xous::Error> {
|
||||||
println!("Looking through server list for free server");
|
println!("Looking through server list for free server");
|
||||||
println!("Server entries are {} bytes long", mem::size_of::<Server>());
|
println!("Server entries are {} bytes long", mem::size_of::<Server>());
|
||||||
assert!(
|
|
||||||
mem::size_of::<QueuedMessage>() == 32,
|
|
||||||
"QueuedMessage was supposed to be 32 bytes, but instead was {} bytes",
|
|
||||||
mem::size_of::<QueuedMessage>()
|
|
||||||
);
|
|
||||||
|
|
||||||
for entry in self.servers.iter_mut() {
|
for entry in self.servers.iter_mut() {
|
||||||
if entry.state == ServerState::Free {
|
if entry == &None {
|
||||||
let pid = self.pid;
|
|
||||||
println!("Found a free slot. Allocating an entry");
|
println!("Found a free slot. Allocating an entry");
|
||||||
// Allocate memory for the new server.
|
let pid = self.pid;
|
||||||
entry.queue = {
|
let sid = (pid as usize, name as usize, pid as usize, name as usize);
|
||||||
|
let (addr, size) = {
|
||||||
let mut mm = MemoryManagerHandle::get();
|
let mut mm = MemoryManagerHandle::get();
|
||||||
let page = mm.map_zeroed_page(pid, false)?;
|
(mm.map_zeroed_page(pid, false)?, PAGE_SIZE)
|
||||||
unsafe {
|
|
||||||
slice::from_raw_parts_mut(
|
|
||||||
page as *mut QueuedMessage,
|
|
||||||
PAGE_SIZE / mem::size_of::<QueuedMessage>(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
println!("Managed to allocate a handle");
|
Server::init(entry, pid, sid, addr, size).or_else(|x| {
|
||||||
entry.state = ServerState::Ready(0);
|
let mut mm = MemoryManagerHandle::get();
|
||||||
entry.pid = self.pid;
|
mm.unmap_page(addr);
|
||||||
entry.sid = (pid as usize, name as usize, pid as usize, name as usize);
|
Err(x)
|
||||||
println!("Returning SID");
|
})?;
|
||||||
return Ok(entry.sid);
|
return Ok(sid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(xous::Error::OutOfMemory)
|
Err(xous::Error::OutOfMemory)
|
||||||
|
|
@ -685,24 +447,28 @@ impl SystemServices {
|
||||||
slot_idx = Some(idx);
|
slot_idx = Some(idx);
|
||||||
}
|
}
|
||||||
// If a connection to this server ID exists already, return it.
|
// If a connection to this server ID exists already, return it.
|
||||||
if self.servers[*server_idx as usize].sid == sid {
|
if let Some(allocated_server) = &self.servers[*server_idx as usize] {
|
||||||
return Ok(idx as CID + 1);
|
if allocated_server.sid == sid {
|
||||||
|
return Ok(idx as CID + 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let slot_idx = slot_idx.ok_or_else(|| xous::Error::OutOfMemory)?;
|
let slot_idx = slot_idx.ok_or_else(|| xous::Error::OutOfMemory)?;
|
||||||
|
|
||||||
// Look through all servers for one whose SID matches.
|
// Look through all servers for one whose SID matches.
|
||||||
for (idx, server) in self.servers.iter().enumerate() {
|
for (idx, server) in self.servers.iter().enumerate() {
|
||||||
if server.sid == sid {
|
if let Some(allocated_server) = server {
|
||||||
process.inner.connection_map[slot_idx] = idx as u8 + 1;
|
if allocated_server.sid == sid {
|
||||||
return Ok(idx + 1);
|
process.inner.connection_map[slot_idx] = idx as u8 + 1;
|
||||||
|
return Ok(idx + 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(xous::Error::OutOfMemory)
|
Err(xous::Error::OutOfMemory)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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_cid(&self, cid: CID) -> Option<&Server> {
|
pub fn server_from_cid(&mut self, cid: CID) -> Option<&mut Server> {
|
||||||
if cid == 0 {
|
if cid == 0 {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
@ -718,18 +484,17 @@ impl SystemServices {
|
||||||
if server_idx >= self.servers.len() {
|
if server_idx >= self.servers.len() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
if let ServerState::Ready(_) = self.servers[server_idx].state {
|
|
||||||
} else {
|
self.servers[server_idx].as_mut()
|
||||||
return None;
|
|
||||||
}
|
|
||||||
Some(&self.servers[server_idx])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a server based on a SID
|
/// Get a server based on a SID
|
||||||
pub fn server_mut(&mut self, sid: SID) -> Option<&mut Server> {
|
pub fn server_mut(&mut self, sid: SID) -> Option<&mut Server> {
|
||||||
for server in self.servers.iter_mut() {
|
for server in self.servers.iter_mut() {
|
||||||
if server.sid == sid {
|
if let Some(active_server) = server {
|
||||||
return Some(server);
|
if active_server.sid == sid {
|
||||||
|
return server.as_mut();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
|
|
|
||||||
|
|
@ -228,7 +228,7 @@ pub fn handle(call: SysCall) -> core::result::Result<xous::Result, xous::Error>
|
||||||
SysCall::ReceiveMessage(sid) => {
|
SysCall::ReceiveMessage(sid) => {
|
||||||
let mut ss = SystemServicesHandle::get();
|
let mut ss = SystemServicesHandle::get();
|
||||||
// See if there is a pending message. If so, return immediately.
|
// See if there is a pending message. If so, return immediately.
|
||||||
let mut server = ss.server_mut(sid).ok_or(xous::Error::ServerNotFound)?;
|
let server = ss.server_mut(sid).ok_or(xous::Error::ServerNotFound)?;
|
||||||
|
|
||||||
// Ensure the server is for this PID
|
// Ensure the server is for this PID
|
||||||
if server.pid != pid {
|
if server.pid != pid {
|
||||||
|
|
@ -269,10 +269,13 @@ pub fn handle(call: SysCall) -> core::result::Result<xous::Result, xous::Error>
|
||||||
}
|
}
|
||||||
SysCall::SendMessage(cid, message) => {
|
SysCall::SendMessage(cid, message) => {
|
||||||
let mut ss = SystemServicesHandle::get();
|
let mut ss = SystemServicesHandle::get();
|
||||||
let server = ss.server_from_cid(cid).ok_or(xous::Error::ServerNotFound)?;
|
let available_contexts = {
|
||||||
|
let mut server = ss.server_from_cid(cid).ok_or(xous::Error::ServerNotFound)?;
|
||||||
|
server.take_available_context()
|
||||||
|
};
|
||||||
|
|
||||||
// If the server has an available context to receive the message, transfer it right away.
|
// If the server has an available context to receive the message, transfer it right away.
|
||||||
if let Some(ctx_number) = server.take_available_context() {
|
if let Some(ctx_number) = available_contexts {
|
||||||
// We can pass control on to this context, and add the existing context
|
// We can pass control on to this context, and add the existing context
|
||||||
// to the blocking pool.
|
// to the blocking pool.
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -283,6 +286,7 @@ pub fn handle(call: SysCall) -> core::result::Result<xous::Result, xous::Error>
|
||||||
|
|
||||||
// Add this message to the queue. If the queue is full, this
|
// Add this message to the queue. If the queue is full, this
|
||||||
// returns an error.
|
// returns an error.
|
||||||
|
let mut server = ss.server_from_cid(cid).ok_or(xous::Error::ServerNotFound)?;
|
||||||
server.queue_message(MessageEnvelope { sender: 0, message }, context_nr)?;
|
server.queue_message(MessageEnvelope { sender: 0, message }, context_nr)?;
|
||||||
|
|
||||||
// Park this context. This is roughly equivalent to a "Yield".
|
// Park this context. This is roughly equivalent to a "Yield".
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue