kernel: fix pid termination

Previously, we were recycling PIDs when devices disconnected, however we
weren't cleaning up PIDs within the system-level table.

This now cleans up these entries.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-06-25 18:15:36 +08:00
parent 9f1b1e7ec8
commit acfe8e5076
3 changed files with 29 additions and 20 deletions

View file

@ -184,10 +184,12 @@ pub fn idle() -> bool {
while let Ok(msg) = receiver.recv() {
match msg {
ThreadMessage::NewConnection(conn) => {
println!("KERNEL(?): Going to call ss.spawn_process()");
let new_pid = SystemServices::with_mut(|ss| {
ss.spawn_process(process::ProcessInit::new(conn.try_clone().unwrap()), ())
})
.unwrap();
println!("KERNEL({}): SystemServices assigned new PID of {}", new_pid, new_pid);
backchannel_sender
.send(BackchannelMessage::NewPid(new_pid))
.expect("couldn't send new pid to new connection");
@ -217,6 +219,7 @@ pub fn idle() -> bool {
println!("Unable to send response to process: {:?} -- terminating", e);
crate::syscall::handle(pid, SysCall::TerminateProcess).ok();
});
println!("Done sending");
}
// Handle the syscall within the Xous kernel

View file

@ -19,9 +19,6 @@ struct ProcessImpl {
/// The network connection to the client process.
conn: TcpStream,
/// Current process ID
pid: PID,
}
impl PartialEq for Process {
@ -32,7 +29,7 @@ impl PartialEq for Process {
struct ProcessTable {
current: PID,
table: Vec<ProcessImpl>,
table: Vec<Option<ProcessImpl>>,
}
thread_local!(
@ -89,7 +86,7 @@ impl Process {
{
PROCESS_TABLE.with(|pt| {
let process_table = pt.borrow();
let current = &process_table.table[process_table.current.get() as usize - 1];
let current = &process_table.table[process_table.current.get() as usize - 1].as_ref().unwrap();
f(&current.inner)
})
}
@ -101,7 +98,7 @@ impl Process {
PROCESS_TABLE.with(|pt| {
let mut process_table = pt.borrow_mut();
let current_pid_idx = process_table.current.get() as usize - 1;
let current = &mut process_table.table[current_pid_idx];
let current = &mut process_table.table[current_pid_idx].as_mut().unwrap();
f(&mut current.inner)
})
}
@ -138,7 +135,7 @@ impl Process {
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];
let process = &mut process_table.table[current_pid_idx].as_mut().unwrap();
for word in result.to_args().iter_mut() {
process.conn
.write_all(&word.to_le_bytes())
@ -154,18 +151,14 @@ impl Process {
let mut process_table = process_table.borrow_mut();
let pid_idx = (pid.get() - 1) as usize;
assert!(
pid_idx >= process_table.table.len(),
"PID {} already allocated",
pid
);
let process = ProcessImpl {
inner: Default::default(),
conn: init_data.conn,
pid,
};
if pid_idx >= process_table.table.len() {
process_table.table.push(process);
process_table.table.push(Some(process));
} else if process_table.table[pid_idx].is_none() {
process_table.table[pid_idx] = Some(process);
} else {
panic!("pid already allocated!");
}
@ -173,11 +166,24 @@ impl Process {
})
}
pub fn destroy(pid: PID) -> Result<(), xous::Error> {
println!("KERNEL: Destroying PID {}", pid);
PROCESS_TABLE.with(|pt| {
let mut process_table = pt.borrow_mut();
let pid_idx = pid.get() as usize - 1;
if pid_idx >= process_table.table.len() {
panic!("attempted to destroy PID that exceeds table index: {}", pid);
}
process_table.table[pid_idx] = None;
Ok(())
})
}
pub fn send(&mut self, bytes: &[u8]) -> Result<(), xous::Error> {
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];
let process = &mut process_table.table[current_pid_idx].as_mut().unwrap();
process.conn.write_all(bytes).unwrap();
});
Ok(())

View file

@ -177,7 +177,7 @@ impl Process {
}
pub fn terminate(&mut self) -> Result<(), xous::Error> {
if !self.free() {
if self.free() {
return Err(xous::Error::ProcessNotFound);
}
@ -186,8 +186,8 @@ impl Process {
// TODO: Free all IRQs
// TODO: Free memory mapping
self.state = ProcessState::Free;
crate::arch::process::Process::destroy(self.pid)?;
Ok(())
}
}
@ -1378,15 +1378,15 @@ impl SystemServices {
// Destroy all servers. This will cause all queued messages to be lost.
for server in &mut self.servers {
if server.is_some() {
Server::destroy(server)?;
Server::destroy(server).unwrap();
}
}
// Destroy all processes. This will cause them to immediately terminate.
for process in &mut self.processes {
if !process.free() {
process.activate()?;
process.terminate()?;
process.activate().unwrap();
process.terminate().unwrap();
}
}
Ok(())