kernel: broken commit: add server message polling

This [broken] commit starts to add message polling to the kernel, so a
server can use the MessageReceive call.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-03-19 17:25:12 +08:00
parent 26cccb6374
commit ae473b5aa7
3 changed files with 102 additions and 21 deletions

View file

@ -53,6 +53,36 @@ pub extern "C" fn init(arg_offset: *const u32, init_offset: *const u32, rpt_offs
arch::init(); arch::init();
} }
fn next_pid_to_run(last_pid: Option<PID>) -> Option<PID> {
// 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
// pick the next process in the list.
let mut current_pid = last_pid.unwrap_or(0) as usize + 2;
let system_services = SystemServicesHandle::get();
for test_idx in current_pid..system_services.processes.len() {
if system_services.processes[test_idx].ppid == 1 {
print!("PID {} is owned by PID1... ", test_idx + 1);
if system_services.processes[test_idx].runnable() {
println!(" and is runnable");
return Some((test_idx + 1) as PID);
}
println!(" and is NOT RUNNABLE");
}
}
for test_idx in 0..current_pid {
if system_services.processes[test_idx].ppid == 1 {
print!("PID {} is owned by PID1... ", test_idx + 1);
if system_services.processes[test_idx].runnable() {
println!(" and is runnable");
return Some((test_idx + 1) as PID);
}
println!(" and is NOT RUNNABLE");
}
}
None
}
#[no_mangle] #[no_mangle]
pub extern "C" fn main() { pub extern "C" fn main() {
// Either map memory using a syscall, or if we're debugging the syscall // Either map memory using a syscall, or if we're debugging the syscall
@ -96,25 +126,13 @@ pub extern "C" fn main() {
} }
} }
let mut pid = None;
loop { loop {
let mut next_pid = None; arch::irq::disable_all_irqs();
{ pid = next_pid_to_run(pid);
arch::irq::disable_all_irqs(); arch::irq::enable_all_irqs();
{
let system_services = SystemServicesHandle::get();
for (pid_idx, process) in system_services.processes.iter().enumerate() {
// If this process is owned by the kernel, and if it can be run, run it.
if process.ppid == 1 && process.runnable() {
println!("PID {} is owned by PID 1, and is runnable", pid_idx + 1);
next_pid = Some(pid_idx as PID + 1);
break;
}
}
}
arch::irq::enable_all_irqs();
}
match next_pid { match pid {
Some(pid) => { Some(pid) => {
println!("Attempting to switch to PID {}", pid); println!("Attempting to switch to PID {}", pid);
xous::rsyscall(xous::SysCall::SwitchTo(pid, 0)) xous::rsyscall(xous::SysCall::SwitchTo(pid, 0))

View file

@ -35,6 +35,8 @@ pub enum ProcessState {
/// This is the current active process /// This is the current active process
Running, Running,
/// This process is waiting for a
/// This process is waiting for an event, such as /// This process is waiting for an event, such as
/// as message or an interrupt /// as message or an interrupt
Sleeping, Sleeping,
@ -119,8 +121,12 @@ enum ServerState {
/// This server slot is unallocated /// This server slot is unallocated
Free, Free,
/// This server can receive messages /// This server can receive messages.
Ready, /// 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 /// This server's inbox is full
Full, Full,
@ -130,6 +136,7 @@ enum ServerState {
/// This should be exactly 8 words / 32 bytes, yielding 128 /// This should be exactly 8 words / 32 bytes, yielding 128
/// queued messages per server /// queued messages per server
#[repr(usize)] #[repr(usize)]
#[derive(PartialEq)]
enum QueuedMessage { enum QueuedMessage {
Empty, Empty,
ScalarMessage( ScalarMessage(
@ -159,21 +166,39 @@ pub struct Server {
sid: SID, sid: SID,
/// The process that owns this server /// The process that owns this server
pid: PID, pub pid: PID,
/// The current state of this slot /// The current state of this slot
state: ServerState, state: ServerState,
/// An index into the queue
queue_index: usize,
/// Where data will appear /// Where data will appear
queue: &'static [QueuedMessage], queue: &'static [QueuedMessage],
} }
impl Server {
pub fn take_next_message(&mut self) -> Option<&'static QueuedMessage> {
if self.queue[self.queue_index] == QueuedMessage::Empty {
return None;
}
let result = &self.queue[self.queue_index];
self.queue_index += 1;
if self.queue_index >= self.queue.len() {
self.queue_index = 0;
}
Some(result)
}
}
impl Default for Server { impl Default for Server {
fn default() -> Self { fn default() -> Self {
Server { Server {
sid: (0, 0, 0, 0), sid: (0, 0, 0, 0),
pid: 0, pid: 0,
state: ServerState::Free, state: ServerState::Free,
queue_index: 0,
queue: &[], queue: &[],
} }
} }
@ -219,6 +244,7 @@ static mut SYSTEM_SERVICES: SystemServices = SystemServices {
sid: (0, 0, 0, 0), sid: (0, 0, 0, 0),
pid: 0, pid: 0,
state: ServerState::Free, state: ServerState::Free,
queue_index: 0,
queue: &[], queue: &[],
}; MAX_SERVER_COUNT], }; MAX_SERVER_COUNT],
}; };
@ -481,7 +507,7 @@ impl SystemServices {
} }
}; };
println!("Managed to allocate a handle"); println!("Managed to allocate a handle");
entry.state = ServerState::Ready; entry.state = ServerState::Ready(0);
entry.pid = self.pid; entry.pid = self.pid;
entry.sid = (pid as usize, name as usize, pid as usize, name as usize); entry.sid = (pid as usize, name as usize, pid as usize, name as usize);
println!("Returning SID"); println!("Returning SID");
@ -496,6 +522,16 @@ impl SystemServices {
pub fn connect_to_server(&mut self, sid: SID) -> Result<CID, xous::Error> { pub fn connect_to_server(&mut self, sid: SID) -> Result<CID, xous::Error> {
Err(xous::Error::OutOfMemory) Err(xous::Error::OutOfMemory)
} }
/// Get a server based on a SID
pub fn get_server(&mut self, sid: SID) -> Option<&Server> {
for server in self.servers.iter_mut() {
if server.sid == sid {
return Some(server);
}
}
None
}
} }
/// How many people have checked out the handle object. /// How many people have checked out the handle object.

View file

@ -229,6 +229,33 @@ pub fn handle(call: SysCall) -> xous::Result {
.map(|_| xous::Result::ResumeProcess) .map(|_| xous::Result::ResumeProcess)
.unwrap_or(xous::Result::Error(xous::Error::ProcessNotFound)) .unwrap_or(xous::Result::Error(xous::Error::ProcessNotFound))
} }
SysCall::WaitMessage(sid) => {
let mut ss = SystemServicesHandle::get();
// See if there is a pending message. If so, return immediately.
let server = ss.get_server(sid);
if server.is_none() {
return xous::Result::Error(xous::Error::ServerNotFound);
}
let server = server.unwrap();
// Ensure the server is for this PID
if server.pid != pid {
return xous::Result::Error(xous::Error::ServerNotFound);
}
// If there is a pending message, return it immediately.
if let Some(msg) = server.take_next_message() {
return xous::Result::Message(msg);
}
// There is no pending message, so return control to the parent process
// and mark ourselves as awaiting an event.
let ppid = ss.get_process(pid).expect("Can't get current process").ppid;
assert_ne!(ppid, 0, "no parent process id");
ss.resume_pid(ppid, ProcessState::WaitEvent)
.map(|_| xous::Result::ResumeProcess)
.unwrap_or(xous::Result::Error(xous::Error::ProcessNotFound))
}
SysCall::WaitEvent => { SysCall::WaitEvent => {
let mut ss = SystemServicesHandle::get(); let mut ss = SystemServicesHandle::get();
let process = ss.get_process(pid).expect("Can't get current process"); let process = ss.get_process(pid).expect("Can't get current process");