mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
messaging: first message passing example
THis commit allows us to pass Scalar messages between processes. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
f56b5e969d
commit
8c3f931445
11 changed files with 224 additions and 37 deletions
|
|
@ -22,6 +22,8 @@ showAnalyzer uart
|
|||
showAnalyzer uartlog
|
||||
#showAnalyzer memlcd
|
||||
|
||||
logLevel -1 kbd
|
||||
|
||||
# The macro `reset` gets called implicitly when running `machine Reset`
|
||||
macro reset
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -25,6 +25,14 @@ fn handle_irq(irq_no: usize, arg: *mut usize) {
|
|||
println!("");
|
||||
}
|
||||
|
||||
fn print_and_yield(index: *mut usize) -> ! {
|
||||
let num = index as usize;
|
||||
loop {
|
||||
println!("THREAD {}", num);
|
||||
xous::syscall::yield_slice();
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
fn main() {
|
||||
let uart = xous::syscall::map_memory(
|
||||
|
|
@ -86,6 +94,19 @@ fn main() {
|
|||
|
||||
timer::init();
|
||||
|
||||
// for i in 1usize..5 {
|
||||
// xous::rsyscall(xous::SysCall::SpawnThread(
|
||||
// print_and_yield as *mut usize,
|
||||
// (0x8000_0000 - 32768 - i * 4096) as *mut usize,
|
||||
// i as *mut usize,
|
||||
// )).expect("couldn't spawn thread");
|
||||
// }
|
||||
// loop {
|
||||
// println!("main thread waiting...");
|
||||
// xous::syscall::wait_event();
|
||||
// println!("MAIN THREAD WOKE UP");
|
||||
// }
|
||||
|
||||
let mut connection = None;
|
||||
println!("Attempting to connect to server...");
|
||||
while connection.is_none() {
|
||||
|
|
@ -100,7 +121,7 @@ fn main() {
|
|||
|
||||
let mut counter = 0;
|
||||
loop {
|
||||
println!("Sending a message...");
|
||||
println!("Sending a scalar message with id {}...", counter + 4096);
|
||||
let result = xous::syscall::send_message(
|
||||
connection,
|
||||
xous::Message::Scalar(xous::ScalarMessage {
|
||||
|
|
@ -113,5 +134,8 @@ fn main() {
|
|||
)
|
||||
.expect("couldn't send message");
|
||||
counter += 1;
|
||||
if counter & 2 == 0 {
|
||||
xous::syscall::yield_slice();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,12 @@ const SYSTEM_CLOCK_FREQUENCY: u32 = 12_000_000;
|
|||
const TIMER_BASE: usize = 0xF000_3000;
|
||||
|
||||
fn timer_tick(_irq_no: usize, _arg: *mut usize) {
|
||||
println!("Timer tick");
|
||||
println!(">>> Timer tick");
|
||||
let ptr = TIMER_BASE as *mut usize;
|
||||
|
||||
// acknowledge the timer
|
||||
unsafe { ptr.add(6).write_volatile(1) };
|
||||
println!("<<< Returning from timer_tick()");
|
||||
}
|
||||
|
||||
pub fn init() {
|
||||
|
|
|
|||
|
|
@ -106,7 +106,7 @@ pub extern "C" fn trap_handler(
|
|||
|
||||
let response = crate::syscall::handle(call).unwrap_or_else(|e| xous::Result::Error(e));
|
||||
|
||||
// println!("Result: {:?}", response);
|
||||
println!("Syscall Result: {:?}", response);
|
||||
let mut process = ProcessHandle::get();
|
||||
let ctx = process.current_context();
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ pub const MAX_CONTEXT: CtxID = 31;
|
|||
use crate::arch::mem::PAGE_SIZE;
|
||||
use crate::services::ProcessInner;
|
||||
use xous::CtxID;
|
||||
use xous;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
|
|
@ -79,6 +80,14 @@ impl Process {
|
|||
None
|
||||
}
|
||||
|
||||
pub fn set_context_result(&mut self, context_nr: CtxID, result: xous::Result) {
|
||||
let vals = unsafe { mem::transmute::<_, [usize; 8]>(result) };
|
||||
let context = self.context(context_nr);
|
||||
for (idx, reg) in vals.iter().enumerate() {
|
||||
context.registers[9 + idx] = *reg;
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize this process context with the given entrypoint and stack
|
||||
/// addresses.
|
||||
pub fn init(&mut self, entrypoint: usize, stack: usize, context: usize) {
|
||||
|
|
|
|||
|
|
@ -375,6 +375,7 @@ impl MemoryManager {
|
|||
if is_user {
|
||||
crate::arch::mem::hand_page_to_user(virt)?;
|
||||
}
|
||||
println!("Mapped {:08x} -> {:08x} (user? {})", phys as usize, virt as usize, is_user);
|
||||
Ok(virt)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -185,13 +185,22 @@ impl Server {
|
|||
/// Add the given message to this server's queue.
|
||||
pub fn queue_message(
|
||||
&mut self,
|
||||
envelope: xous::MessageEnvelope,
|
||||
context: usize,
|
||||
envelope: xous::MessageEnvelope,
|
||||
) -> core::result::Result<(), xous::Error> {
|
||||
if self.queue[self.queue_head] != QueuedMessage::Empty {
|
||||
return Err(xous::Error::ServerQueueFull);
|
||||
}
|
||||
|
||||
self.queue[self.queue_head] = match envelope.message {
|
||||
xous::Message::Scalar(msg) => QueuedMessage::ScalarMessage(
|
||||
envelope.sender, context, msg.id, msg.arg1, msg.arg2, msg.arg3, msg.arg4,
|
||||
),
|
||||
xous::Message::Move(msg) => unimplemented!(),
|
||||
xous::Message::MutableBorrow(msg) => unimplemented!(),
|
||||
xous::Message::ImmutableBorrow(msg) => unimplemented!(),
|
||||
};
|
||||
|
||||
self.queue_head += 1;
|
||||
if self.queue_head >= self.queue.len() {
|
||||
self.queue_head = 0;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use crate::filled_array;
|
|||
use crate::mem::{MemoryManagerHandle, PAGE_SIZE};
|
||||
use crate::server::Server;
|
||||
use core::{mem, slice};
|
||||
use xous::{CtxID, MemoryFlags, CID, PID, SID};
|
||||
use xous::{CtxID, MemoryFlags, MessageEnvelope, CID, PID, SID};
|
||||
|
||||
const MAX_PROCESS_COUNT: usize = 32;
|
||||
const MAX_SERVER_COUNT: usize = 32;
|
||||
|
|
@ -381,6 +381,7 @@ impl SystemServices {
|
|||
ProcessState::Running(x) => ProcessState::Ready(x | (1 << current.current_context)),
|
||||
y => panic!("current process was {:?}, not 'Running(_)'", y),
|
||||
};
|
||||
println!("Making PID {} state {:?}", current_pid, current.state);
|
||||
}
|
||||
|
||||
// Get the new process, and ensure that it is in a state where it's fit
|
||||
|
|
@ -425,12 +426,51 @@ impl SystemServices {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Mark the specified context as ready to run
|
||||
pub fn ready_context(&mut self, pid: PID, context: CtxID) -> Result<(), xous::Error> {
|
||||
let process = self.get_process_mut(pid)?;
|
||||
process.state = match process.state {
|
||||
ProcessState::Running(x) if x & (1 << context) == 0 => {
|
||||
ProcessState::Running(x | (1 << context))
|
||||
}
|
||||
ProcessState::Ready(x) if x & (1 << context) == 0 => {
|
||||
ProcessState::Ready(x | (1 << context))
|
||||
}
|
||||
ProcessState::Sleeping => ProcessState::Ready(1 << context),
|
||||
other => panic!(
|
||||
"PID {} was not in a state to wake a context: {:?}",
|
||||
pid, other
|
||||
),
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_context_result(
|
||||
&mut self,
|
||||
pid: PID,
|
||||
context: CtxID,
|
||||
result: xous::Result,
|
||||
) -> Result<(), xous::Error> {
|
||||
let current_pid = self.current_pid();
|
||||
{
|
||||
let target_process = self.get_process(pid)?;
|
||||
target_process.mapping.activate();
|
||||
let mut arch_process = ProcessHandle::get();
|
||||
arch_process.set_context_result(context, result);
|
||||
}
|
||||
let current_process = self
|
||||
.get_process(current_pid)
|
||||
.expect("couldn't switch back after setting context result");
|
||||
current_process.mapping.activate();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Resume the given process, picking up exactly where it left off. If the
|
||||
/// process is in the Setup state, set it up and then resume.
|
||||
pub fn activate_process_context(
|
||||
&mut self,
|
||||
new_pid: PID,
|
||||
mut new_context: usize,
|
||||
mut new_context: CtxID,
|
||||
can_resume: bool,
|
||||
advance_context: bool,
|
||||
) -> Result<CtxID, xous::Error> {
|
||||
|
|
@ -472,7 +512,7 @@ impl SystemServices {
|
|||
return Err(xous::Error::ProcessNotFound);
|
||||
}
|
||||
}
|
||||
// println!(" -- picked context {}", new_context);
|
||||
// println!(" -- picked context {}", new_context);
|
||||
} else if x & (1 << new_context) == 0 {
|
||||
println!(
|
||||
"context is {:?}, which is not valid for new context {}",
|
||||
|
|
@ -709,6 +749,19 @@ impl SystemServices {
|
|||
|
||||
/// Return a server based on the connection id and the current process
|
||||
pub fn server_from_cid(&mut self, cid: CID) -> Option<&mut Server> {
|
||||
self.servers[self.sidx_from_cid(cid)?].as_mut()
|
||||
}
|
||||
|
||||
/// Return a server based on the connection id and the current process
|
||||
pub fn server_from_sidx(&mut self, sidx: usize) -> Option<&mut Server> {
|
||||
if sidx > self.servers.len() {
|
||||
None
|
||||
} else {
|
||||
self.servers[sidx].as_mut()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sidx_from_cid(&self, cid: CID) -> Option<usize> {
|
||||
if cid == 0 {
|
||||
println!("CID is 0, returning");
|
||||
return None;
|
||||
|
|
@ -722,11 +775,38 @@ impl SystemServices {
|
|||
let server_idx = process.inner.connection_map[cid] as usize;
|
||||
if server_idx >= self.servers.len() {
|
||||
println!("CID {} and server_idx >= {}", cid, server_idx);
|
||||
return None;
|
||||
None
|
||||
} else {
|
||||
Some(server_idx)
|
||||
}
|
||||
}
|
||||
|
||||
println!("Returning self.servers[{}]", server_idx);
|
||||
self.servers[server_idx].as_mut()
|
||||
pub fn queue_server_message(
|
||||
&mut self,
|
||||
sidx: usize,
|
||||
context: usize,
|
||||
envelope: MessageEnvelope,
|
||||
) -> Result<(), xous::Error> {
|
||||
let current_pid = self.current_pid();
|
||||
let result = {
|
||||
let server_pid = self
|
||||
.server_from_sidx(sidx)
|
||||
.ok_or(xous::Error::ServerNotFound)?
|
||||
.pid;
|
||||
{
|
||||
let server_process = self.get_process(server_pid)?;
|
||||
server_process.mapping.activate();
|
||||
}
|
||||
let server = self
|
||||
.server_from_sidx(sidx)
|
||||
.expect("couldn't re-discover server index");
|
||||
server.queue_message(context, envelope)
|
||||
};
|
||||
let current_process = self
|
||||
.get_process(current_pid)
|
||||
.expect("couldn't restore previous process");
|
||||
current_process.mapping.activate();
|
||||
result
|
||||
}
|
||||
|
||||
/// Get a server based on a SID
|
||||
|
|
|
|||
|
|
@ -136,7 +136,7 @@ use xous::*;
|
|||
pub fn handle(call: SysCall) -> core::result::Result<xous::Result, xous::Error> {
|
||||
let pid = arch::current_pid();
|
||||
|
||||
// println!("PID{} Syscall: {:?}", pid, call);
|
||||
println!("PID{} Syscall: {:?}", pid, call);
|
||||
match call {
|
||||
SysCall::MapMemory(phys, virt, size, req_flags) => {
|
||||
let mut mm = MemoryManagerHandle::get();
|
||||
|
|
@ -276,35 +276,73 @@ pub fn handle(call: SysCall) -> core::result::Result<xous::Result, xous::Error>
|
|||
}
|
||||
SysCall::SendMessage(cid, message) => {
|
||||
let mut ss = SystemServicesHandle::get();
|
||||
let available_contexts = {
|
||||
let server = ss.server_from_cid(cid).ok_or(xous::Error::ServerNotFound)?;
|
||||
server.take_available_context()
|
||||
let sidx = ss.sidx_from_cid(cid).ok_or(xous::Error::ServerNotFound)?;
|
||||
let (server_pid, available_context) = {
|
||||
let server = ss
|
||||
.server_from_sidx(sidx)
|
||||
.ok_or(xous::Error::ServerNotFound)?;
|
||||
(server.pid, server.take_available_context())
|
||||
};
|
||||
|
||||
// If the server has an available context to receive the message, transfer it right away.
|
||||
if let Some(ctx_number) = available_contexts {
|
||||
println!("There are contexts available to handle this message");
|
||||
// Determine whether the call is blocking. If so, switch to the
|
||||
// server context right away.
|
||||
let blocking = match message {
|
||||
Message::MutableBorrow(_) | Message::ImmutableBorrow(_) => true,
|
||||
Message::Scalar(_) | Message::Move(_) => false,
|
||||
};
|
||||
|
||||
// Determine whether the call is blocking. If so, switch to the
|
||||
// server context right away.
|
||||
let blocking = match message {
|
||||
Message::MutableBorrow(_) | Message::ImmutableBorrow(_) => true,
|
||||
Message::Scalar(_) | Message::Move(_) => false,
|
||||
};
|
||||
let envelope = MessageEnvelope {
|
||||
sender: pid as usize,
|
||||
message,
|
||||
};
|
||||
|
||||
// If the server has an available context to receive the message,
|
||||
// transfer it right away.
|
||||
if let Some(ctx_number) = available_context {
|
||||
println!(
|
||||
"There are contexts available to handle this message. Marking PID {} as Ready",
|
||||
server_pid
|
||||
);
|
||||
ss.ready_context(server_pid, ctx_number)?;
|
||||
if blocking {
|
||||
println!("Activating Server context and switching away from Client");
|
||||
ss.activate_process_context(server_pid, ctx_number, !blocking, blocking)
|
||||
.map(|_| Ok(xous::Result::Message(envelope)))
|
||||
.unwrap_or(Err(xous::Error::ProcessNotFound))
|
||||
} else {
|
||||
println!("Setting the return value of the Server and returning to Client");
|
||||
ss.set_context_result(server_pid, ctx_number, xous::Result::Message(envelope))
|
||||
.map(|_| xous::Result::Ok)
|
||||
}
|
||||
} else {
|
||||
println!("No contexts available to handle this. Queueing message and parking this context.");
|
||||
println!("No contexts available to handle this. Queueing message.");
|
||||
// There is no server context we can use, so add the message to
|
||||
// the queue.
|
||||
let context_nr = ss.current_context_nr();
|
||||
|
||||
// Add this message to the queue. If the queue is full, this
|
||||
// returns an error.
|
||||
let server = ss.server_from_cid(cid).ok_or(xous::Error::ServerNotFound)?;
|
||||
server.queue_message(MessageEnvelope { sender: 0, message }, context_nr)?;
|
||||
let server = ss
|
||||
.server_from_sidx(sidx)
|
||||
.ok_or(xous::Error::ServerNotFound)?;
|
||||
println!("Adding to queue");
|
||||
ss.queue_server_message(sidx, context_nr, envelope)?;
|
||||
println!("Done adding to queue");
|
||||
|
||||
// Park this context. This is roughly equivalent to a "Yield".
|
||||
// Park this context if it's blocking. This is roughly
|
||||
// equivalent to a "Yield".
|
||||
if blocking {
|
||||
println!("Returning to parent");
|
||||
let process = ss.get_process(pid).expect("Can't get current process");
|
||||
let ppid = process.ppid;
|
||||
ss.activate_process_context(ppid, 0, !blocking, blocking)
|
||||
.map(|_| Ok(xous::Result::ResumeProcess))
|
||||
.unwrap_or(Err(xous::Error::ProcessNotFound))
|
||||
} else {
|
||||
println!("Returning to Client with Ok result");
|
||||
Ok(xous::Result::Ok)
|
||||
}
|
||||
}
|
||||
Err(xous::Error::UnhandledSyscall)
|
||||
}
|
||||
_ => Err(xous::Error::UnhandledSyscall),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ const FLG_R: usize = 0x2;
|
|||
const FLG_U: usize = 0x10;
|
||||
const FLG_A: usize = 0x40;
|
||||
const FLG_D: usize = 0x80;
|
||||
const STACK_PAGE_COUNT: usize = 1;
|
||||
const STACK_PAGE_COUNT: usize = 5;
|
||||
|
||||
mod debug;
|
||||
|
||||
|
|
|
|||
|
|
@ -668,9 +668,12 @@ pub fn map_memory(
|
|||
flags,
|
||||
))?;
|
||||
if let Result::MemoryRange(range) = result {
|
||||
return Ok(range);
|
||||
Ok(range)
|
||||
} else if let Result::Error(e) = result {
|
||||
Err(e)
|
||||
} else {
|
||||
Err(Error::InternalError)
|
||||
}
|
||||
Err(Error::InternalError)
|
||||
}
|
||||
|
||||
/// Claim a hardware interrupt for this process.
|
||||
|
|
@ -685,9 +688,12 @@ pub fn claim_interrupt(
|
|||
arg as *mut usize,
|
||||
))?;
|
||||
if let Result::Ok = result {
|
||||
return Ok(());
|
||||
Ok(())
|
||||
} else if let Result::Error(e) = result {
|
||||
Err(e)
|
||||
} else {
|
||||
Err(Error::InternalError)
|
||||
}
|
||||
Err(Error::InternalError)
|
||||
}
|
||||
|
||||
/// Create a new server with the given name. This enables other processes to
|
||||
|
|
@ -703,9 +709,12 @@ pub fn claim_interrupt(
|
|||
pub fn create_server(name: usize) -> core::result::Result<SID, Error> {
|
||||
let result = rsyscall(SysCall::CreateServer(name))?;
|
||||
if let Result::ServerID(sid) = result {
|
||||
return Ok(sid);
|
||||
Ok(sid)
|
||||
} else if let Result::Error(e) = result {
|
||||
Err(e)
|
||||
} else {
|
||||
Err(Error::InternalError)
|
||||
}
|
||||
Err(Error::InternalError)
|
||||
}
|
||||
|
||||
/// Connect to a server with the given SID
|
||||
|
|
@ -713,6 +722,8 @@ pub fn connect(server: SID) -> core::result::Result<CID, Error> {
|
|||
let result = rsyscall(SysCall::Connect(server))?;
|
||||
if let Result::ConnectionID(cid) = result {
|
||||
Ok(cid)
|
||||
} else if let Result::Error(e) = result {
|
||||
Err(e)
|
||||
} else {
|
||||
Err(Error::InternalError)
|
||||
}
|
||||
|
|
@ -725,7 +736,13 @@ pub fn connect(server: SID) -> core::result::Result<CID, Error> {
|
|||
///
|
||||
pub fn receive_message(server: SID) -> core::result::Result<MessageEnvelope, Error> {
|
||||
let result = rsyscall(SysCall::ReceiveMessage(server)).expect("Couldn't call watimessage");
|
||||
Err(Error::UnhandledSyscall)
|
||||
if let Result::Message(envelope) = result {
|
||||
Ok(envelope)
|
||||
} else if let Result::Error(e) = result {
|
||||
Err(e)
|
||||
} else {
|
||||
Err(Error::InternalError)
|
||||
}
|
||||
}
|
||||
|
||||
/// Send a message to a server. Depending on the mesage type (move or borrow), it
|
||||
|
|
@ -741,7 +758,13 @@ pub fn receive_message(server: SID) -> core::result::Result<MessageEnvelope, Err
|
|||
pub fn send_message(connection: CID, message: Message) -> core::result::Result<(), Error> {
|
||||
let result =
|
||||
rsyscall(SysCall::SendMessage(connection, message)).expect("couldn't send message");
|
||||
Err(Error::UnhandledSyscall)
|
||||
if result == Result::Ok {
|
||||
Ok(())
|
||||
} else if let Result::Error(e) = result {
|
||||
Err(e)
|
||||
} else {
|
||||
panic!("Unexpected return value: {:?}", result);
|
||||
}
|
||||
}
|
||||
|
||||
/// Return execution to the kernel. This function may return at any time,
|
||||
|
|
|
|||
Loading…
Reference in a new issue