mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
kernel: rework hosted network protocol
This fixes issues regarding passing memory between processes. We now properly catch `ReturnMemory` and give the memory back. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
7aab01c95d
commit
fa9a3e0954
10 changed files with 194 additions and 109 deletions
1
kernel/Cargo.lock
generated
1
kernel/Cargo.lock
generated
|
|
@ -513,6 +513,7 @@ name = "xous"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
|
"debug-here",
|
||||||
"hex",
|
"hex",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"xous-macros",
|
"xous-macros",
|
||||||
|
|
|
||||||
|
|
@ -89,10 +89,10 @@ fn handle_connection(
|
||||||
loop {
|
loop {
|
||||||
let mut raw_data = [0u8; 9 * std::mem::size_of::<usize>()];
|
let mut raw_data = [0u8; 9 * std::mem::size_of::<usize>()];
|
||||||
if let Err(_e) = conn.read_exact(&mut raw_data) {
|
if let Err(_e) = conn.read_exact(&mut raw_data) {
|
||||||
// println!(
|
println!(
|
||||||
// "KERNEL(?): Client disconnected: {} ({:?}). Shutting down virtual process.",
|
"KERNEL(?): Client disconnected: {} ({:?}). Shutting down virtual process.",
|
||||||
// _e, _e
|
_e, _e
|
||||||
// );
|
);
|
||||||
sender.send(ServerMessage::Exit).ok();
|
sender.send(ServerMessage::Exit).ok();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -105,23 +105,33 @@ fn handle_connection(
|
||||||
*word = usize::from_le_bytes(bytes.try_into().unwrap());
|
*word = usize::from_le_bytes(bytes.try_into().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (packet_data[1] == xous_kernel::syscall::SysCallNumber::SendMessage as _
|
sender
|
||||||
|| packet_data[1] == xous_kernel::syscall::SysCallNumber::TrySendMessage as _)
|
.send(
|
||||||
&& (packet_data[3] == 1 || packet_data[3] == 2 || packet_data[3] == 3)
|
if (packet_data[1] == xous_kernel::syscall::SysCallNumber::SendMessage as _
|
||||||
{
|
|| packet_data[1]
|
||||||
let mut v = vec![0; packet_data[6]];
|
== xous_kernel::syscall::SysCallNumber::TrySendMessage as _)
|
||||||
if conn.read_exact(&mut v).is_err() {
|
&& (packet_data[3] == 1 || packet_data[3] == 2 || packet_data[3] == 3)
|
||||||
sender.send(ServerMessage::Exit).ok();
|
{
|
||||||
return;
|
let mut v = vec![0; packet_data[6]];
|
||||||
}
|
if conn.read_exact(&mut v).is_err() {
|
||||||
sender
|
sender.send(ServerMessage::Exit).ok();
|
||||||
.send(ServerMessage::ServerPacketWithData(packet_data, v))
|
return;
|
||||||
.unwrap();
|
}
|
||||||
} else {
|
ServerMessage::ServerPacketWithData(packet_data, v)
|
||||||
sender
|
} else if packet_data[1]
|
||||||
.send(ServerMessage::ServerPacket(packet_data))
|
== xous_kernel::syscall::SysCallNumber::ReturnMemory as _
|
||||||
.unwrap();
|
{
|
||||||
}
|
let mut v = vec![0; packet_data[4]];
|
||||||
|
if conn.read_exact(&mut v).is_err() {
|
||||||
|
sender.send(ServerMessage::Exit).ok();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ServerMessage::ServerPacketWithData(packet_data, v)
|
||||||
|
} else {
|
||||||
|
ServerMessage::ServerPacket(packet_data)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -148,7 +158,10 @@ fn handle_connection(
|
||||||
|
|
||||||
for msg in receiver {
|
for msg in receiver {
|
||||||
match msg {
|
match msg {
|
||||||
ServerMessage::Exit => break,
|
ServerMessage::Exit => {
|
||||||
|
println!("KERNEL({}): Received ServerMessage::Exit", pid);
|
||||||
|
break;
|
||||||
|
}
|
||||||
ServerMessage::ServerPacket(pkt) => {
|
ServerMessage::ServerPacket(pkt) => {
|
||||||
let thread_id = pkt[0];
|
let thread_id = pkt[0];
|
||||||
let call = xous_kernel::SysCall::from_args(
|
let call = xous_kernel::SysCall::from_args(
|
||||||
|
|
@ -216,8 +229,28 @@ fn handle_connection(
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
xous_kernel::Message::Scalar(_) | xous_kernel::Message::BlockingScalar(_) => (),
|
xous_kernel::Message::Scalar(_)
|
||||||
|
| xous_kernel::Message::BlockingScalar(_) => (),
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
SysCall::ReturnMemory(_sender, ref mut buf) => {
|
||||||
|
let sliced_data = data.into_boxed_slice();
|
||||||
|
assert_eq!(
|
||||||
|
sliced_data.len(),
|
||||||
|
buf.len(),
|
||||||
|
"deconstructed data {} != message buf length {}",
|
||||||
|
sliced_data.len(),
|
||||||
|
buf.len()
|
||||||
|
);
|
||||||
|
buf.addr =
|
||||||
|
match MemoryAddress::new(Box::into_raw(sliced_data)
|
||||||
|
as *mut u8
|
||||||
|
as usize)
|
||||||
|
{
|
||||||
|
Some(a) => a,
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
_ => panic!("unsupported message type"),
|
_ => panic!("unsupported message type"),
|
||||||
}
|
}
|
||||||
|
|
@ -228,7 +261,10 @@ fn handle_connection(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// eprintln!("KERNEL({}): Finished the thread so sending TerminateProcess", pid);
|
eprintln!(
|
||||||
|
"KERNEL({}): Finished the thread so sending TerminateProcess",
|
||||||
|
pid
|
||||||
|
);
|
||||||
chn.send(ThreadMessage::SysCall(
|
chn.send(ThreadMessage::SysCall(
|
||||||
pid,
|
pid,
|
||||||
1,
|
1,
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,9 @@ pub fn enable_irq(_irq_no: usize) {
|
||||||
unimplemented!();
|
unimplemented!();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn take_isr_return_pair() -> Option<(PID, TID)> {
|
// pub unsafe fn take_isr_return_pair() -> Option<(PID, TID)> {
|
||||||
unimplemented!()
|
// unimplemented!()
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub unsafe fn set_isr_return_pair(_pid: PID, _ctx: TID) {
|
pub unsafe fn set_isr_return_pair(_pid: PID, _ctx: TID) {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
|
|
|
||||||
|
|
@ -288,8 +288,16 @@ impl Process {
|
||||||
response.extend_from_slice(&word.to_le_bytes());
|
response.extend_from_slice(&word.to_le_bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(mem) = result.memory() {
|
||||||
|
let s = unsafe { core::slice::from_raw_parts(mem.as_ptr(), mem.len()) };
|
||||||
|
response.extend_from_slice(&s);
|
||||||
|
}
|
||||||
|
|
||||||
// If there is memory to return for this thread, also return that.
|
// If there is memory to return for this thread, also return that.
|
||||||
if let Some(buf) = process.memory_to_return[tid - 1].take() {
|
if let Some(buf) = process.memory_to_return[tid - 1].take() {
|
||||||
|
if result.memory().is_some() {
|
||||||
|
panic!("Result has memory and we're also returning memory!");
|
||||||
|
}
|
||||||
// if let xous_kernel::Result::Message(_) = result {
|
// if let xous_kernel::Result::Message(_) = result {
|
||||||
// } else {
|
// } else {
|
||||||
// panic!(
|
// panic!(
|
||||||
|
|
|
||||||
|
|
@ -42,10 +42,10 @@ pub unsafe fn set_isr_return_pair(pid: PID, tid: TID) {
|
||||||
PREVIOUS_PAIR = Some((pid, tid));
|
PREVIOUS_PAIR = Some((pid, tid));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
// #[allow(dead_code)]
|
||||||
pub unsafe fn take_isr_return_pair() -> Option<(PID, TID)> {
|
// pub unsafe fn take_isr_return_pair() -> Option<(PID, TID)> {
|
||||||
PREVIOUS_PAIR.take()
|
// PREVIOUS_PAIR.take()
|
||||||
}
|
// }
|
||||||
|
|
||||||
/// Trap entry point rust (_start_trap_rust)
|
/// Trap entry point rust (_start_trap_rust)
|
||||||
///
|
///
|
||||||
|
|
|
||||||
|
|
@ -136,6 +136,16 @@ pub extern "C" fn kmain() {
|
||||||
// Start performing round-robin on all child processes.
|
// Start performing round-robin on all child processes.
|
||||||
// Note that at this point, no new direct children of INIT may be created.
|
// Note that at this point, no new direct children of INIT may be created.
|
||||||
let mut pid = None;
|
let mut pid = None;
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "none"))]
|
||||||
|
{
|
||||||
|
use std::panic;
|
||||||
|
panic::set_hook(Box::new(|arg| {
|
||||||
|
println!("Panic Details: {:?}", arg);
|
||||||
|
debug_here::debug_here!();
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
arch::irq::disable_all_irqs();
|
arch::irq::disable_all_irqs();
|
||||||
pid = next_pid_to_run(pid);
|
pid = next_pid_to_run(pid);
|
||||||
|
|
|
||||||
|
|
@ -331,6 +331,7 @@ impl MemoryManager {
|
||||||
return Ok(potential_start as *mut u8);
|
return Ok(potential_start as *mut u8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// println!("Couldn't find virtual address");
|
||||||
Err(xous_kernel::Error::BadAddress)
|
Err(xous_kernel::Error::BadAddress)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -622,10 +623,10 @@ impl MemoryManager {
|
||||||
offset += region.mem_size as usize / PAGE_SIZE;
|
offset += region.mem_size as usize / PAGE_SIZE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!(
|
// println!(
|
||||||
"mem: unable to claim or release physical address {:08x}",
|
// "mem: unable to claim or release physical address {:08x}",
|
||||||
addr
|
// addr
|
||||||
);
|
// );
|
||||||
Err(xous_kernel::Error::BadAddress)
|
Err(xous_kernel::Error::BadAddress)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,8 @@ 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 {
|
||||||
/// The connection ID inside the server
|
/// The index of the server within the SystemServices table
|
||||||
pub cid: usize,
|
pub sidx: usize,
|
||||||
/// The index into the queue array
|
/// The index into the queue array
|
||||||
pub idx: usize,
|
pub idx: usize,
|
||||||
}
|
}
|
||||||
|
|
@ -12,7 +12,7 @@ pub struct SenderID {
|
||||||
impl From<usize> for SenderID {
|
impl From<usize> for SenderID {
|
||||||
fn from(item: usize) -> SenderID {
|
fn from(item: usize) -> SenderID {
|
||||||
SenderID {
|
SenderID {
|
||||||
cid: item >> 16,
|
sidx: item >> 16,
|
||||||
idx: item & 0xffff,
|
idx: item & 0xffff,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -20,10 +20,11 @@ impl From<usize> for SenderID {
|
||||||
|
|
||||||
impl Into<usize> for SenderID {
|
impl Into<usize> for SenderID {
|
||||||
fn into(self) -> usize {
|
fn into(self) -> usize {
|
||||||
(self.cid << 16) | (self.idx & 0xffff)
|
(self.sidx << 16) | (self.idx & 0xffff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum WaitingMessage {
|
pub enum WaitingMessage {
|
||||||
/// There is no waiting message.
|
/// There is no waiting message.
|
||||||
None,
|
None,
|
||||||
|
|
@ -333,6 +334,7 @@ impl Server {
|
||||||
buf: Option<&MemoryRange>,
|
buf: Option<&MemoryRange>,
|
||||||
) -> Result<WaitingMessage, xous_kernel::Error> {
|
) -> Result<WaitingMessage, xous_kernel::Error> {
|
||||||
if idx > self.queue.len() {
|
if idx > self.queue.len() {
|
||||||
|
// println!("KERNEL: index exceeds queue length");
|
||||||
return Err(xous_kernel::Error::BadAddress);
|
return Err(xous_kernel::Error::BadAddress);
|
||||||
}
|
}
|
||||||
let (pid, ctx, server_addr, client_addr, len, forget, is_memory) = match self.queue[idx] {
|
let (pid, ctx, server_addr, client_addr, len, forget, is_memory) = match self.queue[idx] {
|
||||||
|
|
@ -350,9 +352,10 @@ impl Server {
|
||||||
|
|
||||||
// Sanity check the specified address was correct, and matches what we
|
// Sanity check the specified address was correct, and matches what we
|
||||||
// had cached.
|
// had cached.
|
||||||
if is_memory {
|
if is_memory && cfg!(baremetal) {
|
||||||
let buf = buf.expect("memory message expected but no buffer passed!");
|
let buf = buf.expect("memory message expected but no buffer passed!");
|
||||||
if server_addr != buf.as_ptr() as usize || len != buf.len() {
|
if server_addr != buf.as_ptr() as usize || len != buf.len() {
|
||||||
|
// println!("KERNEL: Memory is attached but the returned buffer doesn't match (len: {} vs {}), buf addr: {:08x} vs {:08x}", len, buf.len(), server_addr, buf.as_ptr() as usize);
|
||||||
return Err(xous_kernel::Error::BadAddress);
|
return Err(xous_kernel::Error::BadAddress);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -407,14 +410,14 @@ 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, cid: xous_kernel::CID) -> Option<xous_kernel::MessageEnvelope> {
|
pub fn take_next_message(&mut self, sidx: usize) -> Option<xous_kernel::MessageEnvelope> {
|
||||||
// println!(
|
// println!(
|
||||||
// "queue_head: ((({}))) queue_tail: ((({}))): {:?} CID: ((({})))",
|
// "queue_head: ((({}))) queue_tail: ((({}))): {:?} CID: ((({})))",
|
||||||
// self.queue_head, self.queue_tail, self.queue[self.queue_tail], cid
|
// self.queue_head, self.queue_tail, self.queue[self.queue_tail], cid
|
||||||
// );
|
// );
|
||||||
let sender = SenderID {
|
let sender = SenderID {
|
||||||
idx: self.queue_tail,
|
idx: self.queue_tail,
|
||||||
cid,
|
sidx,
|
||||||
}.into();
|
}.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,
|
||||||
|
|
|
||||||
|
|
@ -596,6 +596,7 @@ impl SystemServices {
|
||||||
let mut p = crate::arch::process::Process::current();
|
let mut p = crate::arch::process::Process::current();
|
||||||
p.setup_thread(INITIAL_TID, setup)?;
|
p.setup_thread(INITIAL_TID, setup)?;
|
||||||
p.set_thread(INITIAL_TID)?;
|
p.set_thread(INITIAL_TID)?;
|
||||||
|
ArchProcess::with_inner_mut(|process_inner| process_inner.pid = pid);
|
||||||
// process.current_thread = INITIAL_TID as u8;
|
// process.current_thread = INITIAL_TID as u8;
|
||||||
|
|
||||||
// Mark the current proces state as "running, and no waiting contexts"
|
// Mark the current proces state as "running, and no waiting contexts"
|
||||||
|
|
@ -857,10 +858,14 @@ impl SystemServices {
|
||||||
// println!("Setting up new process...");
|
// println!("Setting up new process...");
|
||||||
ArchProcess::setup_process(new_pid, thread_init)
|
ArchProcess::setup_process(new_pid, thread_init)
|
||||||
.expect("couldn't set up new process");
|
.expect("couldn't set up new process");
|
||||||
|
ArchProcess::with_inner_mut(|process_inner| process_inner.pid = new_pid);
|
||||||
|
|
||||||
ProcessState::Running(0)
|
ProcessState::Running(0)
|
||||||
}
|
}
|
||||||
ProcessState::Allocated => ProcessState::Running(0),
|
ProcessState::Allocated => {
|
||||||
|
ArchProcess::with_inner_mut(|process_inner| process_inner.pid = new_pid);
|
||||||
|
ProcessState::Running(0)
|
||||||
|
},
|
||||||
ProcessState::Free => panic!("process was suddenly Free"),
|
ProcessState::Free => panic!("process was suddenly Free"),
|
||||||
ProcessState::Ready(x) | ProcessState::Running(x) => {
|
ProcessState::Ready(x) | ProcessState::Running(x) => {
|
||||||
ProcessState::Running(x & !(1 << new_tid))
|
ProcessState::Running(x & !(1 << new_tid))
|
||||||
|
|
@ -1267,7 +1272,10 @@ impl SystemServices {
|
||||||
ProcessState::Running(x) => ProcessState::Running(x | (1 << new_tid)),
|
ProcessState::Running(x) => ProcessState::Running(x | (1 << new_tid)),
|
||||||
|
|
||||||
// This is the initial thread in this process -- schedule it to be run.
|
// This is the initial thread in this process -- schedule it to be run.
|
||||||
ProcessState::Allocated => ProcessState::Ready(1 << new_tid),
|
ProcessState::Allocated => {
|
||||||
|
ArchProcess::with_inner_mut(|process_inner| process_inner.pid = pid);
|
||||||
|
ProcessState::Ready(1 << new_tid)
|
||||||
|
},
|
||||||
|
|
||||||
other => panic!(
|
other => panic!(
|
||||||
"error spawning thread: process was in an invalid state {:?}",
|
"error spawning thread: process was in an invalid state {:?}",
|
||||||
|
|
@ -1340,9 +1348,10 @@ 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| {
|
||||||
|
assert_eq!(pid, process_inner.pid);
|
||||||
let mut slot_idx = None;
|
let mut slot_idx = None;
|
||||||
// Look through the connection map for (1) a free slot, and (2) an
|
// Look through the connection map for (1) a free slot, and (2) an
|
||||||
// existing connection
|
// existing connection
|
||||||
|
|
@ -1378,9 +1387,10 @@ impl SystemServices {
|
||||||
process_inner.connection_map[slot_idx] =
|
process_inner.connection_map[slot_idx] =
|
||||||
Some(NonZeroU8::new((server_idx as u8) + 2).unwrap());
|
Some(NonZeroU8::new((server_idx as u8) + 2).unwrap());
|
||||||
// println!(
|
// println!(
|
||||||
// "KERNEL({}): New connection to {:?}. After connection, process connection map is: {:?}",
|
// "KERNEL({}): New connection to {:?}. After connection, cid is {} and process connection map is: {:?}",
|
||||||
// _pid.get(),
|
// pid.get(),
|
||||||
// sid,
|
// sid,
|
||||||
|
// slot_idx + 2,
|
||||||
// process_inner.connection_map
|
// process_inner.connection_map
|
||||||
// );
|
// );
|
||||||
return Ok((slot_idx as CID) + 2);
|
return Ok((slot_idx as CID) + 2);
|
||||||
|
|
@ -1391,6 +1401,21 @@ impl SystemServices {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Retrieve the server ID index from the specified SID.
|
||||||
|
/// This may only be called if the SID is a server owned by
|
||||||
|
/// the current process.
|
||||||
|
pub fn sidx_from_sid(&mut self, sid: SID, pid: PID) -> Option<usize> {
|
||||||
|
// println!("KERNEL({}): Server table: {:?}", pid.get(), self.servers);
|
||||||
|
for (idx, slot) in self.servers.iter().enumerate() {
|
||||||
|
if let Some(server) = slot {
|
||||||
|
if server.pid == pid && server.sid == sid {
|
||||||
|
return Some(idx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
/// 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(&self, sidx: usize) -> Option<&Server> {
|
pub fn server_from_sidx(&self, sidx: usize) -> Option<&Server> {
|
||||||
if sidx > self.servers.len() {
|
if sidx > self.servers.len() {
|
||||||
|
|
@ -1409,32 +1434,41 @@ impl SystemServices {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Retrieve a Server ID (Extended) value from the given Connection ID
|
||||||
|
/// within the current process.
|
||||||
pub fn sidx_from_cid(&self, cid: CID) -> Option<usize> {
|
pub fn sidx_from_cid(&self, cid: CID) -> Option<usize> {
|
||||||
|
// println!("KERNEL({}): Attempting to get SIDX from CID {}", crate::arch::process::current_pid(), cid);
|
||||||
if cid == 0 {
|
if cid == 0 {
|
||||||
println!("CID is invalid -- returning");
|
// println!("KERNEL({}): CID is invalid -- returning", crate::arch::process::current_pid());
|
||||||
return None;
|
return None;
|
||||||
} else if cid == 1 {
|
} else if cid == 1 {
|
||||||
println!("Server has terminated -- returning");
|
// println!("KERNEL({}): Server has terminated -- returning", crate::arch::process::current_pid());
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let cid = cid - 2;
|
let cid = cid - 2;
|
||||||
|
|
||||||
ArchProcess::with_inner(|process_inner| {
|
ArchProcess::with_inner(|process_inner| {
|
||||||
|
assert_eq!(crate::arch::process::current_pid(), process_inner.pid);
|
||||||
if cid >= process_inner.connection_map.len() {
|
if cid >= process_inner.connection_map.len() {
|
||||||
println!("KERNEL: CID {} > connection map len", cid);
|
// println!("KERNEL({}): CID {} > connection map len", crate::arch::process::current_pid(), cid);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
// if process_inner.connection_map[cid].is_none() {
|
||||||
|
// println!("KERNEL({}): CID {} doesn't exist in the connection map", crate::arch::process::current_pid(), cid + 2);
|
||||||
|
// println!("KERNEL({}): Process inner is: {:?}", crate::arch::process::current_pid(), process_inner);
|
||||||
|
// }
|
||||||
let mut server_idx = process_inner.connection_map[cid]?.get() as usize;
|
let mut server_idx = process_inner.connection_map[cid]?.get() as usize;
|
||||||
if server_idx < 2 {
|
if server_idx == 1 {
|
||||||
panic!("KERNEL: CID {} is invalid", cid + 2);
|
// println!("KERNEL({}): CID {} is no longer valid", crate::arch::process::current_pid(), cid + 2);
|
||||||
|
return None;
|
||||||
}
|
}
|
||||||
server_idx -= 2;
|
server_idx -= 2;
|
||||||
if server_idx >= self.servers.len() {
|
if server_idx >= self.servers.len() {
|
||||||
println!("KERNEL: CID {} and server_idx >= {}", cid + 2, server_idx);
|
// println!("KERNEL({}): CID {} and server_idx >= {}", crate::arch::process::current_pid(), cid + 2, server_idx);
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
// println!("KERNEL: SIDX for CID {} found at index {}", cid + 2, server_idx);
|
// println!("KERNEL({}): SIDX for CID {} found at index {}", crate::arch::process::current_pid(), cid + 2, server_idx);
|
||||||
Some(server_idx)
|
Some(server_idx)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
@ -1472,27 +1506,27 @@ impl SystemServices {
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Obtain the connection ID of the server from within the server process.
|
// /// 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> {
|
// pub fn server_cid(&mut self, sidx: usize) -> Result<CID, xous_kernel::Error> {
|
||||||
let current_pid = self.current_pid();
|
// let current_pid = self.current_pid();
|
||||||
let result = {
|
// let result = {
|
||||||
let server = self
|
// let server = self
|
||||||
.server_from_sidx(sidx)
|
// .server_from_sidx(sidx)
|
||||||
.ok_or(xous_kernel::Error::ServerNotFound)?;
|
// .ok_or(xous_kernel::Error::ServerNotFound)?;
|
||||||
let server_pid = server.pid;
|
// let server_pid = server.pid;
|
||||||
let sid = server.sid;
|
// let sid = server.sid;
|
||||||
{
|
// {
|
||||||
let server_process = self.get_process(server_pid)?;
|
// let server_process = self.get_process(server_pid)?;
|
||||||
server_process.mapping.activate().unwrap();
|
// server_process.mapping.activate().unwrap();
|
||||||
}
|
// }
|
||||||
self.connect_to_server(sid)
|
// self.connect_to_server(sid)
|
||||||
};
|
// };
|
||||||
let current_process = self
|
// let current_process = self
|
||||||
.get_process(current_pid)
|
// .get_process(current_pid)
|
||||||
.expect("couldn't restore previous process");
|
// .expect("couldn't restore previous process");
|
||||||
current_process.mapping.activate()?;
|
// current_process.mapping.activate()?;
|
||||||
result
|
// 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.
|
||||||
|
|
@ -1521,17 +1555,17 @@ impl SystemServices {
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a server index based on a SID
|
// /// Get a server index based on a SID
|
||||||
pub fn server_sidx(&mut self, sid: SID) -> Option<usize> {
|
// pub fn server_sidx(&mut self, sid: SID) -> Option<usize> {
|
||||||
for (idx, server) in self.servers.iter_mut().enumerate() {
|
// for (idx, server) in self.servers.iter_mut().enumerate() {
|
||||||
if let Some(active_server) = server {
|
// if let Some(active_server) = server {
|
||||||
if active_server.sid == sid {
|
// if active_server.sid == sid {
|
||||||
return Some(idx);
|
// return Some(idx);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
None
|
// None
|
||||||
}
|
// }
|
||||||
|
|
||||||
/// Terminate the given process. Returns the process' parent PID.
|
/// Terminate the given process. Returns the process' parent PID.
|
||||||
pub fn terminate_process(&mut self, target_pid: PID) -> Result<PID, xous_kernel::Error> {
|
pub fn terminate_process(&mut self, target_pid: PID) -> Result<PID, xous_kernel::Error> {
|
||||||
|
|
|
||||||
|
|
@ -130,7 +130,6 @@ 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 server_cid = ss.server_cid(sidx)?;
|
|
||||||
let sender_idx = if message.is_blocking() {
|
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| {
|
||||||
|
|
@ -143,9 +142,13 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
let sender = SenderID {
|
let sender = SenderID {
|
||||||
cid: server_cid,
|
sidx,
|
||||||
idx: sender_idx,
|
idx: sender_idx,
|
||||||
};
|
};
|
||||||
|
// println!(
|
||||||
|
// "KERNEL({}): server connection data: sidx: {}, idx: {}, server pid: {}",
|
||||||
|
// pid, sidx, sender_idx, server_pid
|
||||||
|
// );
|
||||||
let envelope = MessageEnvelope {
|
let envelope = MessageEnvelope {
|
||||||
sender: sender.into(),
|
sender: sender.into(),
|
||||||
body: message,
|
body: message,
|
||||||
|
|
@ -227,11 +230,8 @@ fn return_memory(pid: PID, tid: TID, sender: MessageSender, buf: MemoryRange) ->
|
||||||
SystemServices::with_mut(|ss| {
|
SystemServices::with_mut(|ss| {
|
||||||
let sender = SenderID::from(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_mut(sidx)
|
.server_from_sidx_mut(sender.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);
|
||||||
|
|
@ -276,13 +276,14 @@ fn return_memory(pid: PID, tid: TID, sender: MessageSender, buf: MemoryRange) ->
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// println!(
|
// println!(
|
||||||
// "Returning {} bytes from {:08x} in PID {} to {:08x} in PID {} in context {}",
|
// "KERNEL({}): Returning {} bytes from {:08x} in PID {} to {:08x} in PID {} in context {}",
|
||||||
|
// pid,
|
||||||
// len,
|
// len,
|
||||||
// server_addr.get(),
|
// server_addr.get(),
|
||||||
// pid,
|
// pid,
|
||||||
// client_addr.get(),
|
// client_addr.get(),
|
||||||
// client_pid,
|
// client_pid,
|
||||||
// client_ctx
|
// client_tid
|
||||||
// );
|
// );
|
||||||
|
|
||||||
// Return the memory to the calling process
|
// Return the memory to the calling process
|
||||||
|
|
@ -296,10 +297,6 @@ fn return_memory(pid: PID, tid: TID, sender: MessageSender, buf: MemoryRange) ->
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// Unblock the client context to allow it to continue.
|
// Unblock the client context to allow it to continue.
|
||||||
// println!(
|
|
||||||
// "KERNEL({}): Unblocking PID {} CTX {}",
|
|
||||||
// pid, client_pid, client_ctx
|
|
||||||
// );
|
|
||||||
if !cfg!(baremetal) {
|
if !cfg!(baremetal) {
|
||||||
ss.ready_thread(client_pid, client_tid)?;
|
ss.ready_thread(client_pid, client_tid)?;
|
||||||
ss.switch_to_thread(client_pid, Some(client_tid))?;
|
ss.switch_to_thread(client_pid, Some(client_tid))?;
|
||||||
|
|
@ -328,11 +325,8 @@ fn return_scalar(
|
||||||
SystemServices::with_mut(|ss| {
|
SystemServices::with_mut(|ss| {
|
||||||
let sender = SenderID::from(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_mut(sidx)
|
.server_from_sidx_mut(sender.sidx)
|
||||||
.ok_or(xous_kernel::Error::ServerNotFound)?;
|
.ok_or(xous_kernel::Error::ServerNotFound)?;
|
||||||
if server.pid != server_pid {
|
if server.pid != server_pid {
|
||||||
return Err(xous_kernel::Error::ServerNotFound);
|
return Err(xous_kernel::Error::ServerNotFound);
|
||||||
|
|
@ -397,12 +391,10 @@ fn return_scalar2(
|
||||||
SystemServices::with_mut(|ss| {
|
SystemServices::with_mut(|ss| {
|
||||||
let sender = SenderID::from(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_mut(sidx)
|
.server_from_sidx_mut(sender.sidx)
|
||||||
.ok_or(xous_kernel::Error::ServerNotFound)?;
|
.ok_or(xous_kernel::Error::ServerNotFound)?;
|
||||||
|
// .expect("Couldn't get server from SIDX");
|
||||||
if server.pid != server_pid {
|
if server.pid != server_pid {
|
||||||
return Err(xous_kernel::Error::ServerNotFound);
|
return Err(xous_kernel::Error::ServerNotFound);
|
||||||
}
|
}
|
||||||
|
|
@ -465,9 +457,8 @@ 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)
|
.sidx_from_sid(sid, pid)
|
||||||
.ok_or(xous_kernel::Error::ServerNotFound)?;
|
.ok_or(xous_kernel::Error::ServerNotFound)?;
|
||||||
let server = ss
|
let server = ss
|
||||||
.server_from_sidx_mut(sidx)
|
.server_from_sidx_mut(sidx)
|
||||||
|
|
@ -480,7 +471,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(cid) {
|
if let Some(msg) = server.take_next_message(sidx) {
|
||||||
return Ok(xous_kernel::Result::Message(msg));
|
return Ok(xous_kernel::Result::Message(msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -545,6 +536,7 @@ pub fn handle_inner(pid: PID, tid: TID, call: SysCall) -> SysCallResult {
|
||||||
.map(|x| x.get() >= arch::mem::USER_AREA_END)
|
.map(|x| x.get() >= arch::mem::USER_AREA_END)
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
{
|
{
|
||||||
|
println!("Exceeded user area");
|
||||||
return Err(xous_kernel::Error::BadAddress);
|
return Err(xous_kernel::Error::BadAddress);
|
||||||
|
|
||||||
// Don't allow mapping non-page values
|
// Don't allow mapping non-page values
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue