kernel: hosted: don't send responses twice

If we've already sent a response, don't send a second response.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-11-11 12:55:58 +08:00
parent b497ad113e
commit fc9f597092
2 changed files with 36 additions and 24 deletions

View file

@ -550,6 +550,7 @@ pub fn idle() -> bool {
}
// Handle the syscall within the Xous kernel
crate::arch::process::clear_response_sent_already();
let response =
crate::syscall::handle(pid, thread_id, false, call).unwrap_or_else(Result::Error);
@ -574,17 +575,18 @@ pub fn idle() -> bool {
let s = unsafe { core::slice::from_raw_parts(mem.as_ptr(), mem.len()) };
response_vec.extend_from_slice(s);
}
process.send(&response_vec).unwrap_or_else(|_e| {
// If we're unable to send data to the process, assume it's dead and terminate it.
eprintln!(
"KERNEL({}): Unable to send response to process: {:?} -- terminating",
pid, _e
);
crate::syscall::handle(pid, thread_id, false, SysCall::TerminateProcess).ok();
});
// Send a response only if we haven't already sent one
if ! crate::arch::process::response_sent_already() {
process.send(&response_vec).unwrap_or_else(|_e| {
// If we're unable to send data to the process, assume it's dead and terminate it.
eprintln!(
"KERNEL({}): Unable to send response to process: {:?} -- terminating",
pid, _e
);
crate::syscall::handle(pid, thread_id, false, SysCall::TerminateProcess).ok();
});
}
crate::arch::process::set_current_pid(existing_pid);
// SystemServices::with_mut(|ss| {
// ss.switch_from(pid, 1, true)}).unwrap();
}
if is_shutdown {

View file

@ -27,6 +27,9 @@ struct ProcessImpl {
/// Memory that may need to be returned to the caller for each thread
memory_to_return: [Option<Vec<u8>>; MAX_THREAD + 1],
/// We've already sent a response, so don't send another one
response_sent: bool,
/// This enables the kernel to keep track of threads in the
/// target process, and know which threads are ready to
/// receive messages.
@ -58,9 +61,27 @@ thread_local!(
current: unsafe { PID::new_unchecked(1) },
total: 0,
table: Vec::new(),
})
});
);
pub fn response_sent_already() -> bool {
PROCESS_TABLE.with(|pt| {
let mut process_table = pt.borrow_mut();
let current_pid_idx = process_table.current.get() as usize - 1;
let process = &mut process_table.table[current_pid_idx].as_mut().unwrap();
process.response_sent
})
}
pub fn clear_response_sent_already() {
PROCESS_TABLE.with(|pt| {
let mut process_table = pt.borrow_mut();
let current_pid_idx = process_table.current.get() as usize - 1;
let process = &mut process_table.table[current_pid_idx].as_mut().unwrap();
process.response_sent = false;
})
}
pub fn current_pid() -> PID {
PROCESS_TABLE.with(|pt| pt.borrow().current)
}
@ -299,22 +320,10 @@ impl Process {
if result.memory().is_some() {
panic!("Result has memory and we're also returning memory!");
}
// if let xous_kernel::Result::Message(_) = result {
// } else {
// panic!(
// "memory was waiting to be returned, but message was not a result message"
// );
// }
print!(" [adding {} additional bytes from memory being returned]", buf.len());
response.extend_from_slice(&buf);
}
// eprintln!(
// "KERNEL({}): Setting thread return value to {:?}",
// current_pid_idx + 1,
// response
// );
print!(" [setting thread return value to {} bytes]", response.len());
process.response_sent = true;
process
.conn
.as_mut()
@ -347,6 +356,7 @@ impl Process {
conn: None,
key: init_data.key,
memory_to_return: filled_array![None; 32 /* MAX_THREAD */],
response_sent: false,
current_thread: INITIAL_TID,
threads: [Thread { allocated: false }; MAX_THREAD + 1],
};