2020-03-10 04:04:17 +00:00
|
|
|
use crate::arch;
|
2020-03-16 11:03:10 +00:00
|
|
|
use crate::arch::process::ProcessHandle;
|
2020-03-10 04:04:17 +00:00
|
|
|
use crate::irq::interrupt_claim;
|
2020-03-13 04:44:23 +00:00
|
|
|
use crate::mem::{MemoryManagerHandle, PAGE_SIZE};
|
2020-04-03 04:28:44 +00:00
|
|
|
use crate::server::{SenderID, WaitingMessage};
|
2020-03-27 04:30:29 +00:00
|
|
|
use crate::services::SystemServicesHandle;
|
2020-03-17 03:35:38 +00:00
|
|
|
use core::mem;
|
2020-03-10 04:04:17 +00:00
|
|
|
use xous::*;
|
|
|
|
|
|
|
|
|
|
// extern "Rust" {
|
|
|
|
|
// /// Allocates kernel structures for a new process, and returns the new PID.
|
|
|
|
|
// /// This removes `page_count` page tables from the calling process at `origin_address`
|
|
|
|
|
// /// and places them at `target_address`.
|
|
|
|
|
// ///
|
|
|
|
|
// /// If the process was created successfully, then the new PID is returned to
|
|
|
|
|
// /// the calling process. The child is not automatically scheduled for running.
|
|
|
|
|
// ///
|
|
|
|
|
// /// # Errors
|
|
|
|
|
// ///
|
|
|
|
|
// /// * **BadAlignment**: `origin_address` or `target_address` were not page-aligned,
|
|
|
|
|
// /// or `address_size` was not a multiple of the page address size.
|
|
|
|
|
// /// * **OutOfMemory**: The kernel couldn't allocate memory for the new process.
|
|
|
|
|
// #[allow(dead_code)]
|
|
|
|
|
// pub fn sys_process_spawn(
|
|
|
|
|
// origin_address: MemoryAddress,
|
|
|
|
|
// target_address: MemoryAddress,
|
|
|
|
|
// address_size: MemorySize,
|
|
|
|
|
// ) -> Result<PID, xous::Error>;
|
|
|
|
|
|
|
|
|
|
// /// Interrupts the current process and returns control to the parent process.
|
|
|
|
|
// ///
|
|
|
|
|
// /// # Errors
|
|
|
|
|
// ///
|
|
|
|
|
// /// * **ProcessNotFound**: The provided PID doesn't exist, or is not running on the given CPU.
|
|
|
|
|
// #[allow(dead_code)]
|
|
|
|
|
// pub fn sysi_process_suspend(pid: PID, cpu_id: XousCpuId) -> Result<(), xous::Error>;
|
|
|
|
|
|
|
|
|
|
// #[allow(dead_code)]
|
|
|
|
|
// pub fn sys_process_resume(
|
|
|
|
|
// process_id: PID,
|
|
|
|
|
// stack_pointer: Option<usize>,
|
|
|
|
|
// additional_contexts: &Option<&[XousContext]>,
|
|
|
|
|
// ) -> Result<
|
|
|
|
|
// (
|
|
|
|
|
// Option<XousContext>,
|
|
|
|
|
// Option<XousContext>,
|
|
|
|
|
// Option<XousContext>,
|
|
|
|
|
// ),
|
|
|
|
|
// xous::Error,
|
|
|
|
|
// >;
|
|
|
|
|
|
|
|
|
|
// /// Causes a process to terminate immediately.
|
|
|
|
|
// ///
|
|
|
|
|
// /// It is recommended that this function only be called on processes that
|
|
|
|
|
// /// have cleaned up after themselves, e.g. shut down any servers and
|
|
|
|
|
// /// flushed any file descriptors.
|
|
|
|
|
// ///
|
|
|
|
|
// /// # Errors
|
|
|
|
|
// ///
|
|
|
|
|
// /// * **ProcessNotFound**: The requested process does not exist
|
|
|
|
|
// /// * **ProcessNotChild**: The requested process is not our child process
|
|
|
|
|
// #[allow(dead_code)]
|
|
|
|
|
// pub fn sys_process_terminate(process_id: PID) -> Result<(), xous::Error>;
|
|
|
|
|
|
|
|
|
|
// /// Equivalent to the Unix `sbrk` call. Adjusts the
|
|
|
|
|
// /// heap size to be equal to the specified value. Heap
|
|
|
|
|
// /// sizes start out at 0 bytes in new processes.
|
|
|
|
|
// ///
|
|
|
|
|
// /// # Errors
|
|
|
|
|
// ///
|
|
|
|
|
// /// * **OutOfMemory**: The region couldn't be extended.
|
|
|
|
|
// #[allow(dead_code)]
|
|
|
|
|
// pub fn sys_heap_resize(size: MemorySize) -> Result<(), xous::Error>;
|
|
|
|
|
|
|
|
|
|
// ///! Message Passing Functions
|
|
|
|
|
|
|
|
|
|
// /// Create a new server with the given name. This enables other processes to
|
|
|
|
|
// /// connect to this server to send messages. Only one server name may exist
|
|
|
|
|
// /// on a system at a time.
|
|
|
|
|
// ///
|
|
|
|
|
// /// # Errors
|
|
|
|
|
// ///
|
|
|
|
|
// /// * **ServerExists**: A server has already registered with that name
|
|
|
|
|
// /// * **InvalidString**: The name was not a valid UTF-8 string
|
|
|
|
|
// #[allow(dead_code)]
|
|
|
|
|
// pub fn sys_server_create(server_name: usize) -> Result<XousSid, xous::Error>;
|
|
|
|
|
|
|
|
|
|
// /// Suspend the current process until a message is received. This thread will
|
|
|
|
|
// /// block until a message is received.
|
|
|
|
|
// ///
|
|
|
|
|
// /// # Errors
|
|
|
|
|
// ///
|
|
|
|
|
// #[allow(dead_code)]
|
|
|
|
|
// pub fn sys_server_receive(server_id: XousSid) -> Result<XousMessageReceived, xous::Error>;
|
|
|
|
|
|
|
|
|
|
// /// Reply to a message received. The thread will be unblocked, and will be
|
|
|
|
|
// /// scheduled to run sometime in the future.
|
|
|
|
|
// ///
|
|
|
|
|
// /// If the message that we're responding to is a Memory message, then it should be
|
|
|
|
|
// /// passed back directly to the destination without modification -- the actual contents
|
|
|
|
|
// /// will be passed in the `out` address pointed to by the structure.
|
|
|
|
|
// ///
|
|
|
|
|
// /// # Errors
|
|
|
|
|
// ///
|
|
|
|
|
// /// * **ProcessTerminated**: The process we're replying to doesn't exist any more.
|
|
|
|
|
// /// * **BadAddress**: The message didn't pass back all the memory it should have.
|
|
|
|
|
// #[allow(dead_code)]
|
|
|
|
|
// pub fn sys_server_reply(
|
|
|
|
|
// destination: XousMessageSender,
|
|
|
|
|
// message: XousMessage,
|
|
|
|
|
// ) -> Result<(), xous::Error>;
|
|
|
|
|
|
|
|
|
|
// /// Look up a server name and connect to it.
|
|
|
|
|
// ///
|
|
|
|
|
// /// # Errors
|
|
|
|
|
// ///
|
|
|
|
|
// /// * **ServerNotFound**: No server is registered with that name.
|
|
|
|
|
// #[allow(dead_code)]
|
|
|
|
|
// pub fn sys_client_connect(server_name: usize) -> Result<XousConnection, xous::Error>;
|
|
|
|
|
|
|
|
|
|
// /// Send a message to a server. This thread will block until the message is responded to.
|
|
|
|
|
// /// If the message type is `Memory`, then the memory addresses pointed to will be
|
|
|
|
|
// /// unavailable to this process until this function returns.
|
|
|
|
|
// ///
|
|
|
|
|
// /// # Errors
|
|
|
|
|
// ///
|
|
|
|
|
// /// * **ServerNotFound**: The server does not exist so the connection is now invalid
|
|
|
|
|
// /// * **BadAddress**: The client tried to pass a Memory message using an address it doesn't own
|
|
|
|
|
// /// * **Timeout**: The timeout limit has been reached
|
|
|
|
|
// #[allow(dead_code)]
|
|
|
|
|
// pub fn sys_client_send(
|
|
|
|
|
// server: XousConnection,
|
|
|
|
|
// message: XousMessage,
|
|
|
|
|
// ) -> Result<XousMessage, xous::Error>;
|
|
|
|
|
// }
|
|
|
|
|
|
2020-03-23 06:07:54 +00:00
|
|
|
pub fn handle(call: SysCall) -> core::result::Result<xous::Result, xous::Error> {
|
2020-03-10 04:04:17 +00:00
|
|
|
let pid = arch::current_pid();
|
|
|
|
|
|
2020-03-30 08:07:58 +00:00
|
|
|
println!("PID{} Syscall: {:?}", pid, call);
|
2020-03-10 04:04:17 +00:00
|
|
|
match call {
|
2020-03-13 12:33:08 +00:00
|
|
|
SysCall::MapMemory(phys, virt, size, req_flags) => {
|
2020-03-10 04:04:17 +00:00
|
|
|
let mut mm = MemoryManagerHandle::get();
|
2020-03-13 04:44:23 +00:00
|
|
|
// Don't let the address exceed the user area (unless it's PID 1)
|
2020-03-10 04:04:17 +00:00
|
|
|
if pid != 1 && (virt as usize) != 0 && (virt as usize) >= arch::mem::USER_AREA_END {
|
2020-03-23 06:07:54 +00:00
|
|
|
return Err(xous::Error::BadAddress);
|
2020-03-13 04:44:23 +00:00
|
|
|
|
|
|
|
|
// Don't allow mapping non-page values
|
|
|
|
|
} else if size & (PAGE_SIZE - 1) != 0 {
|
2020-03-10 04:04:17 +00:00
|
|
|
// println!("map: bad alignment of size {:08x}", size);
|
2020-03-23 06:07:54 +00:00
|
|
|
return Err(xous::Error::BadAlignment);
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
|
|
|
|
// println!(
|
|
|
|
|
// "Mapping {:08x} -> {:08x} ({} bytes, flags: {:?})",
|
|
|
|
|
// phys as u32, virt as u32, size, req_flags
|
|
|
|
|
// );
|
2020-03-31 10:03:19 +00:00
|
|
|
let range = mm.map_range(phys, virt, size, pid, req_flags, MemoryType::Default)?;
|
|
|
|
|
|
|
|
|
|
// If we're handing back an address in main RAM, zero it out. If
|
|
|
|
|
// phys is 0, then the page will be lazily allocated, so we
|
|
|
|
|
// don't need to do this.
|
|
|
|
|
if phys as usize != 0 {
|
|
|
|
|
if mm.is_main_memory(phys) {
|
|
|
|
|
println!(
|
|
|
|
|
"Going to zero out {} bytes @ {:08x}",
|
2020-04-01 02:28:36 +00:00
|
|
|
range.size.get(),
|
|
|
|
|
range.addr.get()
|
2020-03-31 10:03:19 +00:00
|
|
|
);
|
|
|
|
|
unsafe {
|
|
|
|
|
range
|
2020-04-01 02:28:36 +00:00
|
|
|
.as_mut_ptr()
|
|
|
|
|
.write_bytes(0, range.size.get() / mem::size_of::<usize>())
|
2020-03-31 10:03:19 +00:00
|
|
|
};
|
|
|
|
|
println!("Done zeroing out");
|
2020-03-17 03:35:38 +00:00
|
|
|
}
|
2020-03-31 10:03:19 +00:00
|
|
|
for offset in
|
2020-04-01 02:28:36 +00:00
|
|
|
(range.addr.get()..(range.addr.get() + range.size.get())).step_by(PAGE_SIZE)
|
2020-03-31 10:03:19 +00:00
|
|
|
{
|
|
|
|
|
println!("Handing page to user");
|
2020-03-24 06:52:25 +00:00
|
|
|
crate::arch::mem::hand_page_to_user(offset as *mut usize)
|
|
|
|
|
.expect("couldn't hand page to user");
|
2020-03-17 03:35:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-03-31 10:03:19 +00:00
|
|
|
|
|
|
|
|
Ok(xous::Result::MemoryRange(range))
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
2020-03-31 10:37:08 +00:00
|
|
|
SysCall::UnmapMemory(virt, size) => {
|
|
|
|
|
let mut mm = MemoryManagerHandle::get();
|
|
|
|
|
let mut result = Ok(xous::Result::Ok);
|
|
|
|
|
let virt = virt as usize;
|
|
|
|
|
if virt & 0xfff != 0 {
|
|
|
|
|
return Err(xous::Error::BadAlignment);
|
|
|
|
|
}
|
2020-04-01 02:28:36 +00:00
|
|
|
for addr in (virt..(virt + size)).step_by(PAGE_SIZE) {
|
2020-03-31 10:37:08 +00:00
|
|
|
if let Err(e) = mm.unmap_page(addr as *mut usize) {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
result = Err(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
|
|
|
|
}
|
2020-03-10 04:04:17 +00:00
|
|
|
SysCall::IncreaseHeap(delta, flags) => {
|
|
|
|
|
if delta & 0xfff != 0 {
|
2020-03-23 06:07:54 +00:00
|
|
|
return Err(xous::Error::BadAlignment);
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
|
|
|
|
let start = {
|
2020-03-16 10:22:59 +00:00
|
|
|
let mut process = ProcessHandle::get();
|
2020-03-10 04:04:17 +00:00
|
|
|
|
2020-03-16 10:22:59 +00:00
|
|
|
if process.inner.mem_heap_size + delta > process.inner.mem_heap_max {
|
2020-03-23 06:07:54 +00:00
|
|
|
return Err(xous::Error::OutOfMemory);
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-16 10:22:59 +00:00
|
|
|
let start = process.inner.mem_heap_base + process.inner.mem_heap_size;
|
|
|
|
|
process.inner.mem_heap_size += delta;
|
2020-03-10 04:04:17 +00:00
|
|
|
start as *mut usize
|
|
|
|
|
};
|
|
|
|
|
let mut mm = MemoryManagerHandle::get();
|
2020-03-31 10:03:19 +00:00
|
|
|
Ok(xous::Result::MemoryRange(
|
|
|
|
|
mm.reserve_range(start, delta, flags)?,
|
|
|
|
|
))
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
|
|
|
|
SysCall::DecreaseHeap(delta) => {
|
|
|
|
|
if delta & 0xfff != 0 {
|
2020-03-23 06:07:54 +00:00
|
|
|
return Err(xous::Error::BadAlignment);
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
|
|
|
|
let start = {
|
2020-03-16 10:22:59 +00:00
|
|
|
let mut process = ProcessHandle::get();
|
2020-03-10 04:04:17 +00:00
|
|
|
|
2020-03-16 10:22:59 +00:00
|
|
|
if process.inner.mem_heap_size + delta > process.inner.mem_heap_max {
|
2020-03-23 06:07:54 +00:00
|
|
|
return Err(xous::Error::OutOfMemory);
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-16 10:22:59 +00:00
|
|
|
let start = process.inner.mem_heap_base + process.inner.mem_heap_size;
|
|
|
|
|
process.inner.mem_heap_size -= delta;
|
2020-03-10 04:04:17 +00:00
|
|
|
start
|
|
|
|
|
};
|
|
|
|
|
let mut mm = MemoryManagerHandle::get();
|
|
|
|
|
for page in ((start - delta)..start).step_by(crate::arch::mem::PAGE_SIZE) {
|
|
|
|
|
mm.unmap_page(page as *mut usize)
|
|
|
|
|
.expect("unable to unmap page");
|
|
|
|
|
}
|
2020-03-23 06:07:54 +00:00
|
|
|
Ok(xous::Result::Ok)
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
|
|
|
|
SysCall::SwitchTo(pid, context) => {
|
|
|
|
|
let mut ss = SystemServicesHandle::get();
|
2020-03-27 07:58:59 +00:00
|
|
|
ss.activate_process_context(pid, context, true, false)
|
2020-03-31 10:37:08 +00:00
|
|
|
.map(|_ctx| {
|
|
|
|
|
// println!("switchto ({}, {})", pid, _ctx);
|
2020-03-27 08:59:19 +00:00
|
|
|
xous::Result::ResumeProcess
|
|
|
|
|
})
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
|
|
|
|
SysCall::ClaimInterrupt(no, callback, arg) => {
|
2020-03-24 06:52:25 +00:00
|
|
|
interrupt_claim(no, pid as definitions::PID, callback, arg).map(|_| xous::Result::Ok)
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
|
|
|
|
SysCall::Yield => {
|
|
|
|
|
let mut ss = SystemServicesHandle::get();
|
2020-03-27 04:30:29 +00:00
|
|
|
let ppid = ss.get_process(pid).expect("can't get current process").ppid;
|
2020-03-10 04:04:17 +00:00
|
|
|
assert_ne!(ppid, 0, "no parent process id");
|
2020-03-27 07:58:59 +00:00
|
|
|
ss.activate_process_context(ppid, 0, true, true)
|
2020-03-23 06:07:54 +00:00
|
|
|
.map(|_| Ok(xous::Result::ResumeProcess))
|
|
|
|
|
.unwrap_or(Err(xous::Error::ProcessNotFound))
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
2020-03-23 08:56:56 +00:00
|
|
|
SysCall::ReceiveMessage(sid) => {
|
2020-03-19 09:25:12 +00:00
|
|
|
let mut ss = SystemServicesHandle::get();
|
2020-03-27 04:30:29 +00:00
|
|
|
let context_nr = ss.current_context_nr();
|
2020-03-19 09:25:12 +00:00
|
|
|
// See if there is a pending message. If so, return immediately.
|
2020-04-01 09:27:59 +00:00
|
|
|
let sidx = ss.server_sidx(sid).ok_or(xous::Error::ServerNotFound)?;
|
|
|
|
|
let server = ss
|
|
|
|
|
.server_from_sidx(sidx)
|
|
|
|
|
.ok_or(xous::Error::ServerNotFound)?;
|
|
|
|
|
server.print_queue();
|
2020-03-19 09:25:12 +00:00
|
|
|
|
|
|
|
|
// Ensure the server is for this PID
|
|
|
|
|
if server.pid != pid {
|
2020-03-23 06:07:54 +00:00
|
|
|
return Err(xous::Error::ServerNotFound);
|
2020-03-19 09:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If there is a pending message, return it immediately.
|
2020-04-01 09:27:59 +00:00
|
|
|
if let Some(msg) = server.take_next_message(sidx) {
|
|
|
|
|
println!("PID {} had a message ready -- returning it", pid);
|
|
|
|
|
return Ok(xous::Result::Message(msg));
|
2020-03-19 09:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
2020-04-01 09:27:59 +00:00
|
|
|
// There is no pending message, so return control to the parent
|
|
|
|
|
// process and mark ourselves as awaiting an event. When a message
|
|
|
|
|
// arrives, our return value will already be set to the
|
|
|
|
|
// MessageEnvelope of the incoming message.
|
2020-04-03 04:28:44 +00:00
|
|
|
println!(
|
|
|
|
|
"PID {} did not have any waiting messages -- parking context",
|
|
|
|
|
pid
|
|
|
|
|
);
|
2020-03-24 06:52:25 +00:00
|
|
|
server.park_context(context_nr);
|
|
|
|
|
|
2020-03-19 09:25:12 +00:00
|
|
|
let ppid = ss.get_process(pid).expect("Can't get current process").ppid;
|
|
|
|
|
assert_ne!(ppid, 0, "no parent process id");
|
2020-03-27 07:58:59 +00:00
|
|
|
ss.activate_process_context(ppid, 0, false, true)
|
2020-03-23 06:07:54 +00:00
|
|
|
.map(|_| Ok(xous::Result::ResumeProcess))
|
|
|
|
|
.unwrap_or(Err(xous::Error::ProcessNotFound))
|
2020-03-19 09:25:12 +00:00
|
|
|
}
|
2020-03-10 04:04:17 +00:00
|
|
|
SysCall::WaitEvent => {
|
|
|
|
|
let mut ss = SystemServicesHandle::get();
|
|
|
|
|
let process = ss.get_process(pid).expect("Can't get current process");
|
|
|
|
|
let ppid = process.ppid;
|
|
|
|
|
assert_ne!(ppid, 0, "no parent process id");
|
2020-03-27 07:58:59 +00:00
|
|
|
ss.activate_process_context(ppid, 0, false, true)
|
2020-03-23 06:07:54 +00:00
|
|
|
.map(|_| Ok(xous::Result::ResumeProcess))
|
|
|
|
|
.unwrap_or(Err(xous::Error::ProcessNotFound))
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
2020-03-27 08:59:19 +00:00
|
|
|
SysCall::SpawnThread(entrypoint, stack_pointer, argument) => {
|
2020-03-27 07:58:59 +00:00
|
|
|
let mut ss = SystemServicesHandle::get();
|
2020-03-27 08:59:19 +00:00
|
|
|
ss.spawn_thread(entrypoint, stack_pointer, argument)
|
|
|
|
|
.map(|ctx| xous::Result::ThreadID(ctx))
|
|
|
|
|
}
|
2020-03-16 11:03:10 +00:00
|
|
|
SysCall::CreateServer(name) => {
|
|
|
|
|
let mut ss = SystemServicesHandle::get();
|
2020-03-24 06:52:25 +00:00
|
|
|
ss.create_server(name).map(|x| xous::Result::ServerID(x))
|
2020-03-16 11:03:10 +00:00
|
|
|
}
|
2020-03-19 00:54:52 +00:00
|
|
|
SysCall::Connect(sid) => {
|
|
|
|
|
let mut ss = SystemServicesHandle::get();
|
|
|
|
|
ss.connect_to_server(sid)
|
|
|
|
|
.map(|x| xous::Result::ConnectionID(x))
|
|
|
|
|
}
|
2020-04-03 06:23:15 +00:00
|
|
|
SysCall::ReturnMemory(sender, arg1, arg2) => {
|
2020-04-01 09:27:59 +00:00
|
|
|
let mut ss = SystemServicesHandle::get();
|
|
|
|
|
let sender = SenderID::from_usize(sender)?;
|
|
|
|
|
|
|
|
|
|
let server = ss
|
|
|
|
|
.server_from_sidx(sender.sidx)
|
|
|
|
|
.ok_or(xous::Error::ServerNotFound)?;
|
|
|
|
|
if server.pid != pid {
|
|
|
|
|
return Err(xous::Error::ServerNotFound);
|
|
|
|
|
}
|
|
|
|
|
let result = server.take_waiting_message(sender.tidx)?;
|
|
|
|
|
let (client_pid, client_ctx, server_addr, client_addr, len) = match result {
|
2020-04-03 04:28:44 +00:00
|
|
|
WaitingMessage::BorrowedMemory(
|
|
|
|
|
client_pid,
|
|
|
|
|
client_ctx,
|
|
|
|
|
server_addr,
|
|
|
|
|
client_addr,
|
|
|
|
|
len,
|
|
|
|
|
) => (client_pid, client_ctx, server_addr, client_addr, len),
|
|
|
|
|
WaitingMessage::MovedMemory => {
|
2020-04-01 09:27:59 +00:00
|
|
|
return Ok(xous::Result::Ok);
|
|
|
|
|
}
|
2020-04-03 04:28:44 +00:00
|
|
|
WaitingMessage::None => {
|
|
|
|
|
println!("WARNING: Tried to wait on a message that didn't exist");
|
|
|
|
|
return Err(xous::Error::ProcessNotFound);
|
|
|
|
|
}
|
2020-04-01 09:27:59 +00:00
|
|
|
};
|
|
|
|
|
println!(
|
|
|
|
|
"Returning {} bytes from {:08x} in PID {} to {:08x} in PID {} in context {}",
|
|
|
|
|
len,
|
|
|
|
|
server_addr.get(),
|
|
|
|
|
pid,
|
|
|
|
|
client_addr.get(),
|
|
|
|
|
client_pid,
|
|
|
|
|
client_ctx
|
|
|
|
|
);
|
|
|
|
|
|
2020-04-03 04:28:44 +00:00
|
|
|
// Return the memory to the calling process
|
|
|
|
|
ss.return_memory(
|
2020-04-01 09:27:59 +00:00
|
|
|
server_addr.get() as *mut usize,
|
|
|
|
|
client_pid,
|
|
|
|
|
client_addr.get() as *mut usize,
|
|
|
|
|
len.get(),
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
// Unblock the client context to allow it to continue.
|
|
|
|
|
println!("Unblocking PID {} CTX {}", client_pid, client_ctx);
|
|
|
|
|
ss.ready_context(client_pid, client_ctx)?;
|
2020-04-03 06:23:15 +00:00
|
|
|
ss.set_context_result(client_pid, client_ctx, xous::Result::MessageResult(arg1, arg2))?;
|
2020-04-01 09:27:59 +00:00
|
|
|
Ok(xous::Result::Ok)
|
|
|
|
|
}
|
2020-03-23 06:07:54 +00:00
|
|
|
SysCall::SendMessage(cid, message) => {
|
|
|
|
|
let mut ss = SystemServicesHandle::get();
|
2020-04-01 05:21:23 +00:00
|
|
|
let context_nr = ss.current_context_nr();
|
2020-03-30 08:07:58 +00:00
|
|
|
let sidx = ss.sidx_from_cid(cid).ok_or(xous::Error::ServerNotFound)?;
|
2020-03-23 08:56:56 +00:00
|
|
|
|
2020-03-31 10:03:19 +00:00
|
|
|
let server_pid = ss
|
|
|
|
|
.server_from_sidx(sidx)
|
|
|
|
|
.expect("server couldn't be located")
|
|
|
|
|
.pid;
|
|
|
|
|
|
2020-04-01 09:27:59 +00:00
|
|
|
// Remember the address the message came from, in case we need to
|
|
|
|
|
// return it after the borrow is through.
|
|
|
|
|
let client_address = match &message {
|
|
|
|
|
Message::Scalar(_) => None,
|
|
|
|
|
Message::Move(msg)
|
|
|
|
|
| Message::MutableBorrow(msg)
|
|
|
|
|
| Message::ImmutableBorrow(msg) => Some(msg.buf.addr),
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-31 10:03:19 +00:00
|
|
|
// Translate memory messages from the client process to the server
|
2020-04-01 09:27:59 +00:00
|
|
|
// process. Additionally, determine whether the call is blocking. If
|
|
|
|
|
// so, switch to the server context right away.
|
2020-03-31 10:03:19 +00:00
|
|
|
let (message, blocking) = match message {
|
|
|
|
|
Message::Scalar(_) => (message, false),
|
|
|
|
|
Message::Move(msg) => {
|
2020-04-01 02:28:36 +00:00
|
|
|
let new_virt = ss.send_memory(
|
2020-04-01 09:27:59 +00:00
|
|
|
msg.buf.as_mut_ptr(),
|
|
|
|
|
server_pid,
|
|
|
|
|
0 as *mut usize,
|
|
|
|
|
msg.buf.len(),
|
|
|
|
|
)?;
|
2020-03-31 10:03:19 +00:00
|
|
|
(
|
|
|
|
|
Message::Move(MemoryMessage {
|
|
|
|
|
id: msg.id,
|
2020-04-03 04:28:44 +00:00
|
|
|
buf: MemoryRange::new(new_virt as usize, msg.buf.len()),
|
2020-03-31 10:03:19 +00:00
|
|
|
offset: msg.offset,
|
|
|
|
|
valid: msg.valid,
|
|
|
|
|
}),
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
Message::MutableBorrow(_) => unimplemented!(),
|
2020-04-01 05:21:23 +00:00
|
|
|
Message::ImmutableBorrow(msg) => {
|
2020-04-03 04:28:44 +00:00
|
|
|
let new_virt = ss.lend_memory(
|
2020-04-01 09:27:59 +00:00
|
|
|
msg.buf.as_mut_ptr(),
|
|
|
|
|
server_pid,
|
|
|
|
|
0 as *mut usize,
|
|
|
|
|
msg.buf.len(),
|
|
|
|
|
false,
|
|
|
|
|
)?;
|
2020-04-01 05:21:23 +00:00
|
|
|
(
|
|
|
|
|
Message::ImmutableBorrow(MemoryMessage {
|
|
|
|
|
id: msg.id,
|
2020-04-03 04:28:44 +00:00
|
|
|
buf: MemoryRange::new(new_virt as usize, msg.buf.len()),
|
2020-04-01 05:21:23 +00:00
|
|
|
offset: msg.offset,
|
|
|
|
|
valid: msg.valid,
|
|
|
|
|
}),
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
}
|
2020-03-30 08:07:58 +00:00
|
|
|
};
|
2020-03-24 06:52:25 +00:00
|
|
|
|
2020-03-30 08:07:58 +00:00
|
|
|
// If the server has an available context to receive the message,
|
|
|
|
|
// transfer it right away.
|
2020-03-31 10:03:19 +00:00
|
|
|
if let Some(ctx_number) = ss
|
|
|
|
|
.server_from_sidx(sidx)
|
|
|
|
|
.expect("server couldn't be located")
|
|
|
|
|
.take_available_context()
|
|
|
|
|
{
|
2020-03-30 08:07:58 +00:00
|
|
|
println!(
|
|
|
|
|
"There are contexts available to handle this message. Marking PID {} as Ready",
|
|
|
|
|
server_pid
|
|
|
|
|
);
|
2020-04-01 09:27:59 +00:00
|
|
|
let sender =
|
|
|
|
|
ss.remember_server_message(sidx, pid, context_nr, &message, client_address)?;
|
|
|
|
|
let envelope = MessageEnvelope { sender, message };
|
|
|
|
|
|
2020-03-30 08:07:58 +00:00
|
|
|
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))
|
2020-04-03 06:23:15 +00:00
|
|
|
.map(|_| xous::Result::MessageResult(0, 0))
|
2020-03-30 08:07:58 +00:00
|
|
|
}
|
2020-03-23 08:56:56 +00:00
|
|
|
} else {
|
2020-03-30 08:07:58 +00:00
|
|
|
println!("No contexts available to handle this. Queueing message.");
|
2020-03-23 08:56:56 +00:00
|
|
|
// There is no server context we can use, so add the message to
|
|
|
|
|
// the queue.
|
2020-03-27 04:30:29 +00:00
|
|
|
let context_nr = ss.current_context_nr();
|
2020-03-23 08:56:56 +00:00
|
|
|
|
|
|
|
|
// Add this message to the queue. If the queue is full, this
|
|
|
|
|
// returns an error.
|
2020-04-01 09:27:59 +00:00
|
|
|
ss.queue_server_message(sidx, pid, context_nr, message, client_address)?;
|
2020-03-23 08:56:56 +00:00
|
|
|
|
2020-03-30 08:07:58 +00:00
|
|
|
// 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)
|
|
|
|
|
}
|
2020-03-23 08:56:56 +00:00
|
|
|
}
|
2020-03-23 06:07:54 +00:00
|
|
|
}
|
|
|
|
|
_ => Err(xous::Error::UnhandledSyscall),
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
|
|
|
|
}
|