mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
kernel: working on getting process termination functioning
Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
9361055aec
commit
14bb1ac1e5
7 changed files with 273 additions and 36 deletions
|
|
@ -22,8 +22,8 @@ fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender<(PID, SysCall)>)
|
|||
for word in pkt.iter_mut() {
|
||||
if let Err(e) = conn.read_exact(&mut incoming_word) {
|
||||
println!(
|
||||
"Client {} disconnected. Shutting down virtual process.",
|
||||
pid
|
||||
"Client {} disconnected: {}. Shutting down virtual process.",
|
||||
pid, e
|
||||
);
|
||||
let call = xous::SysCall::TerminateProcess;
|
||||
chn.send((pid, call)).unwrap();
|
||||
|
|
@ -92,7 +92,11 @@ pub fn idle() {
|
|||
for word in response.to_args().iter_mut() {
|
||||
response_vec.extend_from_slice(&word.to_le_bytes());
|
||||
}
|
||||
processes.send(&response_vec);
|
||||
processes.send(&response_vec).unwrap_or_else(|e| {
|
||||
// If we're unable to send data to the process, assume it's dead and terminate it.
|
||||
println!("Unable to send response to process: {:?} -- terminating", e);
|
||||
crate::syscall::handle(pid, SysCall::TerminateProcess).ok();
|
||||
});
|
||||
}
|
||||
let mut ss = SystemServicesHandle::get();
|
||||
ss.switch_from(pid, 1, true).unwrap();
|
||||
|
|
|
|||
|
|
@ -140,7 +140,6 @@ pub extern "C" fn kmain() {
|
|||
Some(pid) => {
|
||||
// println!("Attempting to switch to PID {}", pid);
|
||||
xous::rsyscall(xous::SysCall::SwitchTo(pid, 0)).expect("couldn't switch to pid");
|
||||
()
|
||||
}
|
||||
None => {
|
||||
println!("No runnable tasks found. Entering idle state...");
|
||||
|
|
|
|||
|
|
@ -17,9 +17,18 @@ impl SenderID {
|
|||
}
|
||||
|
||||
pub enum WaitingMessage {
|
||||
|
||||
/// There is no waiting message.
|
||||
None,
|
||||
|
||||
/// The memory was borrowed and should be returned to the given process.
|
||||
BorrowedMemory(PID, CtxID, MemoryAddress, MemoryAddress, MemorySize),
|
||||
|
||||
/// The memory was moved, and so shouldn't be returned.
|
||||
MovedMemory,
|
||||
|
||||
/// This memory should be returned to the system.
|
||||
ForgetMemory(MemoryRange),
|
||||
}
|
||||
|
||||
/// Internal representation of a queued message for a server. This should be
|
||||
|
|
@ -65,6 +74,30 @@ enum QueuedMessage {
|
|||
usize, /* valid */
|
||||
),
|
||||
|
||||
/// The process lending this memory terminated before
|
||||
/// we could receive the message.
|
||||
MemoryMessageROLendTerminated(
|
||||
usize, /* sender PID/CtxID */
|
||||
usize, /* sender base address */
|
||||
usize, /* id */
|
||||
usize, /* buf */
|
||||
usize, /* buf_size */
|
||||
usize, /* offset */
|
||||
usize, /* valid */
|
||||
),
|
||||
|
||||
/// The process lending this memory terminated before
|
||||
/// we could receive the message.
|
||||
MemoryMessageRWLendTerminated(
|
||||
usize, /* sender PID/CtxID */
|
||||
usize, /* sender base address */
|
||||
usize, /* id */
|
||||
usize, /* buf */
|
||||
usize, /* buf_size */
|
||||
usize, /* offset */
|
||||
usize, /* valid */
|
||||
),
|
||||
|
||||
/// When a message is taken that needs to be returned -- such as an ROLend
|
||||
/// or RWLend -- the slot is replaced with a WaitingResponse token and its
|
||||
/// index is returned as the message sender. This is used to unblock the
|
||||
|
|
@ -75,6 +108,15 @@ enum QueuedMessage {
|
|||
usize, /* Server base address */
|
||||
usize, /* Range size */
|
||||
),
|
||||
|
||||
/// When a server goes away, its memory must be forgotten instead of being returned
|
||||
/// to the previous process.
|
||||
WaitingForget(
|
||||
usize, /* sender PID/CtxID */
|
||||
usize, /* Client base address */
|
||||
usize, /* Server base address */
|
||||
usize, /* Range size */
|
||||
),
|
||||
}
|
||||
|
||||
/// A pointer to resolve a server ID to a particular process
|
||||
|
|
@ -152,6 +194,27 @@ impl Server {
|
|||
// }
|
||||
// }
|
||||
|
||||
/// When a process terminates, there may be memory that is lent to us.
|
||||
/// Mark all of that memory to be discarded when it is returned, rather than
|
||||
/// giving it back to the previous process space.
|
||||
pub fn discard_messages_for_pid(&mut self, pid: PID) {
|
||||
for entry in self.queue.iter_mut() {
|
||||
match entry {
|
||||
&mut QueuedMessage::MemoryMessageROLend(pid_ctx, arg1, arg2, arg3, arg4, arg5, arg6)
|
||||
if (pid_ctx >> 16) as PID == pid =>
|
||||
{
|
||||
*entry = QueuedMessage::MemoryMessageROLendTerminated(pid_ctx, arg1, arg2, arg3, arg4, arg5, arg6);
|
||||
}
|
||||
&mut QueuedMessage::MemoryMessageRWLend(pid_ctx, arg1, arg2, arg3, arg4, arg5, arg6)
|
||||
if (pid_ctx >> 16) as PID == pid =>
|
||||
{
|
||||
*entry = QueuedMessage::MemoryMessageRWLendTerminated(pid_ctx, arg1, arg2, arg3, arg4, arg5, arg6);
|
||||
}
|
||||
x => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a `QueuedMesage::WaitingResponse` into `QueuedMessage::Empty`
|
||||
/// and return the pair. Advance the tail. Note that the `idx` could be
|
||||
/// somewhere other than the tail, but as long as it points to a valid
|
||||
|
|
@ -160,9 +223,12 @@ impl Server {
|
|||
if idx > self.queue.len() {
|
||||
return Err(xous::Error::BadAddress);
|
||||
}
|
||||
let (pid_ctx, server_addr, client_addr, len) = match self.queue[idx] {
|
||||
let (pid_ctx, server_addr, client_addr, len, forget) = match self.queue[idx] {
|
||||
QueuedMessage::WaitingResponse(pid_ctx, server_addr, client_addr, len) => {
|
||||
(pid_ctx, server_addr, client_addr, len)
|
||||
(pid_ctx, server_addr, client_addr, len, false)
|
||||
}
|
||||
QueuedMessage::WaitingForget(pid_ctx, server_addr, client_addr, len) => {
|
||||
(pid_ctx, server_addr, client_addr, len, true)
|
||||
}
|
||||
_ => return Ok(WaitingMessage::None),
|
||||
};
|
||||
|
|
@ -172,12 +238,20 @@ impl Server {
|
|||
self.queue_tail = 0;
|
||||
}
|
||||
|
||||
// Destructure the PID and context ID from the `pid_ctx` field
|
||||
let pid = ((pid_ctx >> 16) & 0xff) as PID;
|
||||
let ctx = (pid_ctx & 0xffff) as CtxID;
|
||||
|
||||
if forget {
|
||||
return Ok(WaitingMessage::ForgetMemory(MemoryRange::new(server_addr, len)));
|
||||
}
|
||||
|
||||
// If a `move` address somehow ends up here, indicate the memory has been moved.
|
||||
let server_addr = match MemoryAddress::new(server_addr) {
|
||||
Some(o) => o,
|
||||
None => return Ok(WaitingMessage::MovedMemory),
|
||||
};
|
||||
|
||||
let client_addr = MemoryAddress::new(client_addr)
|
||||
.expect("client memory address was 0, but server address was not");
|
||||
let len = MemorySize::new(len).expect("memory length was 0, but address was not None");
|
||||
|
|
@ -208,6 +282,7 @@ impl Server {
|
|||
let result = match self.queue[self.queue_tail] {
|
||||
QueuedMessage::Empty => return None,
|
||||
QueuedMessage::WaitingResponse(_, _, _, _) => return None,
|
||||
QueuedMessage::WaitingForget(_, _, _, _) => return None,
|
||||
QueuedMessage::MemoryMessageROLend(
|
||||
pid_ctx,
|
||||
client_addr,
|
||||
|
|
@ -230,6 +305,7 @@ impl Server {
|
|||
buf,
|
||||
client_addr,
|
||||
buf_size,
|
||||
false,
|
||||
),
|
||||
QueuedMessage::MemoryMessageRWLend(
|
||||
pid_ctx,
|
||||
|
|
@ -253,6 +329,55 @@ impl Server {
|
|||
buf,
|
||||
client_addr,
|
||||
buf_size,
|
||||
false,
|
||||
),
|
||||
QueuedMessage::MemoryMessageROLendTerminated(
|
||||
pid_ctx,
|
||||
client_addr,
|
||||
id,
|
||||
buf,
|
||||
buf_size,
|
||||
offset,
|
||||
valid,
|
||||
) => (
|
||||
xous::MessageEnvelope {
|
||||
sender: self.queue_tail | (server_idx << 16),
|
||||
message: xous::Message::ImmutableBorrow(xous::MemoryMessage {
|
||||
id,
|
||||
buf: MemoryRange::new(buf, buf_size),
|
||||
offset: MemorySize::new(offset),
|
||||
valid: MemorySize::new(valid),
|
||||
}),
|
||||
},
|
||||
pid_ctx,
|
||||
buf,
|
||||
client_addr,
|
||||
buf_size,
|
||||
true,
|
||||
),
|
||||
QueuedMessage::MemoryMessageRWLendTerminated(
|
||||
pid_ctx,
|
||||
client_addr,
|
||||
id,
|
||||
buf,
|
||||
buf_size,
|
||||
offset,
|
||||
valid,
|
||||
) => (
|
||||
xous::MessageEnvelope {
|
||||
sender: self.queue_tail | (server_idx << 16),
|
||||
message: xous::Message::MutableBorrow(xous::MemoryMessage {
|
||||
id,
|
||||
buf: MemoryRange::new(buf, buf_size),
|
||||
offset: MemorySize::new(offset),
|
||||
valid: MemorySize::new(valid),
|
||||
}),
|
||||
},
|
||||
pid_ctx,
|
||||
buf,
|
||||
client_addr,
|
||||
buf_size,
|
||||
true,
|
||||
),
|
||||
QueuedMessage::MemoryMessageSend(
|
||||
pid_ctx,
|
||||
|
|
@ -300,8 +425,13 @@ impl Server {
|
|||
return Some(msg);
|
||||
}
|
||||
};
|
||||
self.queue[self.queue_tail] =
|
||||
QueuedMessage::WaitingResponse(result.1, result.2, result.3, result.4);
|
||||
if result.5 {
|
||||
self.queue[self.queue_tail] =
|
||||
QueuedMessage::WaitingForget(result.1, result.2, result.3, result.4);
|
||||
} else {
|
||||
self.queue[self.queue_tail] =
|
||||
QueuedMessage::WaitingResponse(result.1, result.2, result.3, result.4);
|
||||
}
|
||||
Some(result.0)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -119,6 +119,7 @@ impl Default for ProcessInner {
|
|||
}
|
||||
|
||||
impl Process {
|
||||
/// This process has at least one context that may be run
|
||||
pub fn runnable(&self) -> bool {
|
||||
match self.state {
|
||||
ProcessState::Setup(_) | ProcessState::Ready(_) => true,
|
||||
|
|
@ -126,6 +127,14 @@ impl Process {
|
|||
}
|
||||
}
|
||||
|
||||
/// This process slot is unallocated and may be turn into a process
|
||||
pub fn free(&self) -> bool {
|
||||
match self.state {
|
||||
ProcessState::Free => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn activate(&self, pid: PID) -> Result<(), xous::Error> {
|
||||
self.mapping.activate()?;
|
||||
{
|
||||
|
|
@ -135,6 +144,19 @@ impl Process {
|
|||
ProcessHandle::set(pid);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn terminate(&mut self) -> Result<(), xous::Error> {
|
||||
if ! self.free() {
|
||||
return Err(xous::Error::ProcessNotFound);
|
||||
}
|
||||
|
||||
// TODO: Free all pages
|
||||
|
||||
// TODO: Free all IRQs
|
||||
|
||||
self.state = ProcessState::Free;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// A big unifying struct containing all of the system state.
|
||||
|
|
@ -242,6 +264,7 @@ impl SystemServices {
|
|||
}
|
||||
arch::process::Process::create(idx as PID + 1, init_process);
|
||||
// HACK: Mark the first PID as being "Running"
|
||||
#[allow(clippy::unit_arg)]
|
||||
if idx == 0 {
|
||||
entry.state = ProcessState::Running(0);
|
||||
} else {
|
||||
|
|
@ -1137,7 +1160,7 @@ impl SystemServices {
|
|||
// If a connection to this server ID exists already, return it.
|
||||
if let Some(allocated_server) = &self.servers[*server_idx as usize] {
|
||||
if allocated_server.sid == sid {
|
||||
return Ok(idx as CID + 1);
|
||||
return Ok(idx as CID + 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1147,7 +1170,7 @@ impl SystemServices {
|
|||
for (idx, server) in self.servers.iter().enumerate() {
|
||||
if let Some(allocated_server) = server {
|
||||
if allocated_server.sid == sid {
|
||||
process.inner.connection_map[slot_idx] = idx as u8 + 1;
|
||||
process.inner.connection_map[slot_idx] = idx as u8 + 2;
|
||||
return Ok(idx + 1);
|
||||
}
|
||||
}
|
||||
|
|
@ -1166,10 +1189,15 @@ impl SystemServices {
|
|||
|
||||
pub fn sidx_from_cid(&self, cid: CID) -> Option<usize> {
|
||||
if cid == 0 {
|
||||
println!("CID is 0, returning");
|
||||
println!("CID is invalid -- returning");
|
||||
return None;
|
||||
} else if cid == 1 {
|
||||
println!("Server has terminated -- returning");
|
||||
return None;
|
||||
}
|
||||
let cid = cid - 1;
|
||||
|
||||
let cid = cid - 2;
|
||||
|
||||
let process = ProcessHandle::get();
|
||||
if cid >= process.inner.connection_map.len() {
|
||||
println!("CID {} > connection map len", cid);
|
||||
|
|
@ -1202,19 +1230,17 @@ impl SystemServices {
|
|||
.pid;
|
||||
{
|
||||
let server_process = self.get_process(server_pid)?;
|
||||
server_process.mapping.activate();
|
||||
server_process.mapping.activate().unwrap();
|
||||
}
|
||||
let server = self
|
||||
.server_from_sidx(sidx)
|
||||
.expect("couldn't re-discover server index");
|
||||
let result = server.queue_message(pid, context, message, original_address);
|
||||
// server.print_queue();
|
||||
result
|
||||
server.queue_message(pid, context, message, original_address)
|
||||
};
|
||||
let current_process = self
|
||||
.get_process(current_pid)
|
||||
.expect("couldn't restore previous process");
|
||||
current_process.mapping.activate();
|
||||
current_process.mapping.activate()?;
|
||||
result
|
||||
}
|
||||
|
||||
|
|
@ -1229,25 +1255,18 @@ impl SystemServices {
|
|||
client_address: Option<MemoryAddress>,
|
||||
) -> Result<usize, 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_address(pid, context, message, client_address)
|
||||
};
|
||||
// let current_process = self
|
||||
// .get_process(current_pid)
|
||||
// .expect("couldn't restore previous process");
|
||||
// current_process.mapping.activate();
|
||||
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_address(pid, context, message, client_address)
|
||||
}
|
||||
|
||||
/// Get a server index based on a SID
|
||||
|
|
@ -1261,6 +1280,57 @@ impl SystemServices {
|
|||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Terminate the given process. Returns the process' parent PID.
|
||||
pub fn terminate_process(&mut self, target_pid: PID) -> Result<PID, xous::Error> {
|
||||
// To terminate a process, we must perform the following:
|
||||
//
|
||||
// 1. If we have any client connections, remove them.
|
||||
// 2. If there are any clients connected to our server, insert a tombstone so writes fail
|
||||
// 3. If there are any incoming server requests queued, dequeue them and return an error
|
||||
// 4. Mark all "Borrowed" memory as "Free-when-returned". That way, if we've shared
|
||||
// memory to a Server, it will be reclaimed by the system when it comes back
|
||||
|
||||
// 1. Find all servers associated with this PID and remove them.
|
||||
for (idx, server) in self.servers.iter_mut().enumerate() {
|
||||
if let Some(server) = server {
|
||||
if server.pid == target_pid {
|
||||
// This is our server, so look through the connection map of each
|
||||
// process to determine if this connection needs to be replaced
|
||||
// with a tombstone.
|
||||
for process in self.processes.iter() {
|
||||
if process.free() {
|
||||
continue;
|
||||
}
|
||||
process.activate(target_pid);
|
||||
let mut p = ProcessHandle::get();
|
||||
|
||||
// Look through the connection map for a connection
|
||||
// that matches this index. Note that connection map entries
|
||||
// are offset by two, because 0 == free and 1 == "tombstone".
|
||||
for mapping in p.inner.connection_map.iter_mut() {
|
||||
if *mapping == (idx as u8) + 2 {
|
||||
*mapping = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Look through this server's memory space to determine if this process
|
||||
// is mentioned there as having some memory lent out.
|
||||
server.discard_messages_for_pid(target_pid);
|
||||
}
|
||||
}
|
||||
let process = self.get_process_mut(target_pid)?;
|
||||
process.activate(target_pid);
|
||||
let parent_pid = process.ppid;
|
||||
process.terminate();
|
||||
|
||||
let process = self.get_process(parent_pid)?;
|
||||
process.activate(parent_pid).unwrap();
|
||||
|
||||
Ok(parent_pid)
|
||||
}
|
||||
}
|
||||
|
||||
/// How many people have checked out the handle object. This should be replaced
|
||||
|
|
|
|||
|
|
@ -281,6 +281,23 @@ pub fn handle(pid: PID, call: SysCall) -> core::result::Result<xous::Result, xou
|
|||
WaitingMessage::MovedMemory => {
|
||||
return Ok(xous::Result::Ok);
|
||||
}
|
||||
WaitingMessage::ForgetMemory(range) => {
|
||||
let mut mm = MemoryManagerHandle::get();
|
||||
let mut result = Ok(xous::Result::Ok);
|
||||
let virt = range.addr.get();
|
||||
let size = range.size.get();
|
||||
if virt & 0xfff != 0 {
|
||||
return Err(xous::Error::BadAlignment);
|
||||
}
|
||||
for addr in (virt..(virt + size)).step_by(PAGE_SIZE) {
|
||||
if let Err(e) = mm.unmap_page(addr as *mut usize) {
|
||||
if result.is_ok() {
|
||||
result = Err(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
WaitingMessage::None => {
|
||||
println!("WARNING: Tried to wait on a message that didn't exist");
|
||||
return Err(xous::Error::ProcessNotFound);
|
||||
|
|
@ -464,6 +481,17 @@ pub fn handle(pid: PID, call: SysCall) -> core::result::Result<xous::Result, xou
|
|||
}
|
||||
}
|
||||
}
|
||||
SysCall::TerminateProcess => {
|
||||
let mut ss = SystemServicesHandle::get();
|
||||
let context_nr = ss.current_context_nr();
|
||||
ss.switch_from(pid, context_nr, false)?;
|
||||
let ppid = ss.terminate_process(pid)?;
|
||||
if cfg!(baremetal) {
|
||||
ss.switch_to(ppid, None).map(|_| xous::Result::ResumeProcess)
|
||||
} else {
|
||||
Ok(xous::Result::Ok)
|
||||
}
|
||||
}
|
||||
_ => Err(xous::Error::UnhandledSyscall),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -122,6 +122,7 @@ impl fmt::Display for ElfReadError {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::cognitive_complexity)]
|
||||
pub fn read_program<P: AsRef<Path>>(filename: P) -> Result<ProgramDescription, ElfReadError> {
|
||||
let mut b = Vec::new();
|
||||
{
|
||||
|
|
@ -261,6 +262,7 @@ pub fn read_program<P: AsRef<Path>>(filename: P) -> Result<ProgramDescription, E
|
|||
}
|
||||
|
||||
/// Read an ELF file into a mini ELF file.
|
||||
#[allow(clippy::cognitive_complexity)]
|
||||
pub fn read_minielf<P: AsRef<Path>>(filename: P) -> Result<MiniElf, ElfReadError> {
|
||||
let mut b = Vec::new();
|
||||
{
|
||||
|
|
|
|||
|
|
@ -290,6 +290,10 @@ impl MemoryRange {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_parts(addr: MemoryAddress, size: MemorySize) -> MemoryRange {
|
||||
MemoryRange { addr, size }
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.size.get()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue