mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
kernel: implement thread exit support
Threads now have the ability to exit. Previously this resulted in a kernel panic. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
6b70922381
commit
7c2d3a2247
4 changed files with 88 additions and 5 deletions
|
|
@ -4,7 +4,7 @@
|
|||
use crate::arch::current_pid;
|
||||
use crate::arch::mem::MemoryMapping;
|
||||
use crate::arch::process::Process as ArchProcess;
|
||||
use crate::arch::process::{Thread, RETURN_FROM_ISR};
|
||||
use crate::arch::process::{Thread, EXIT_THREAD, RETURN_FROM_ISR};
|
||||
use crate::services::SystemServices;
|
||||
use riscv::register::{scause, sepc, sstatus, stval, vexriscv::sim, vexriscv::sip};
|
||||
use xous_kernel::{SysCall, PID, TID};
|
||||
|
|
@ -77,7 +77,7 @@ pub extern "C" fn trap_handler(
|
|||
panic!("Ran out of kernel stack");
|
||||
}
|
||||
|
||||
let pid = crate::arch::current_pid();
|
||||
let pid = current_pid();
|
||||
|
||||
if (sc.bits() == 9) || (sc.bits() == 8) {
|
||||
// We got here because of an `ecall` instruction. When we return, skip
|
||||
|
|
@ -149,6 +149,18 @@ pub extern "C" fn trap_handler(
|
|||
);
|
||||
println!("Page was not allocated");
|
||||
}
|
||||
RiscvException::InstructionPageFault(EXIT_THREAD, _offset) => {
|
||||
let tid = ArchProcess::with_current(|process| process.current_tid());
|
||||
|
||||
// This address indicates a thread has exited. Destroy the thread.
|
||||
// This activates another thread within this process.
|
||||
SystemServices::with_mut(|ss| ss.destroy_thread(pid, tid)).unwrap();
|
||||
|
||||
// Resume the new thread within the same process.
|
||||
ArchProcess::with_current_mut(|p| {
|
||||
crate::arch::syscall::resume(current_pid().get() == 1, p.current_thread())
|
||||
});
|
||||
}
|
||||
RiscvException::InstructionPageFault(RETURN_FROM_ISR, _offset) => {
|
||||
// If we hit this address, then an ISR has just returned. Since
|
||||
// we're in an interrupt context, it is safe to access this
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ pub const MAX_PROCESS_COUNT: usize = 64;
|
|||
pub const RETURN_FROM_ISR: usize = 0xff80_2000;
|
||||
|
||||
/// This is the address a thread will return to when it exits.
|
||||
const EXIT_THREAD: usize = 0xff80_3000;
|
||||
pub const EXIT_THREAD: usize = 0xff80_3000;
|
||||
|
||||
// Thread IDs have three possible meaning:
|
||||
// Logical Thread ID: What the user sees
|
||||
|
|
@ -350,6 +350,38 @@ impl Process {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Destroy a given thread and return its return value.
|
||||
///
|
||||
/// # Returns
|
||||
/// The return value of the function
|
||||
///
|
||||
/// # Errors
|
||||
/// xous::ThreadNotAvailable - the thread did not exist
|
||||
pub fn destroy_thread(&mut self, tid: TID) -> Result<usize, xous_kernel::Error> {
|
||||
let thread = self.thread_mut(tid);
|
||||
|
||||
// Ensure this thread is valid
|
||||
if thread.sepc == 0 || tid == IRQ_TID {
|
||||
Err(xous_kernel::Error::ThreadNotAvailable)?;
|
||||
}
|
||||
|
||||
// thread.registers[0] == x1
|
||||
// thread.registers[1] == x2
|
||||
// ...
|
||||
// thread.registers[4] == x5 == t0
|
||||
// ...
|
||||
// thread.registers[9] == x10 == a0
|
||||
// thread.registers[10] == x11 == a1
|
||||
let return_value = thread.registers[9];
|
||||
|
||||
for val in &mut thread.registers {
|
||||
*val = 0;
|
||||
}
|
||||
thread.sepc = 0;
|
||||
|
||||
Ok(return_value)
|
||||
}
|
||||
|
||||
pub fn print_all_threads(&self) {
|
||||
let process = unsafe { &mut *PROCESS };
|
||||
&mut process.threads[process.hardware_thread - 1];
|
||||
|
|
|
|||
|
|
@ -162,8 +162,8 @@ pub fn irq(_irq_number: usize, _arg: *mut usize) {
|
|||
for process in &system_services.processes {
|
||||
if !process.free() {
|
||||
println!(
|
||||
"PID {} {}:",
|
||||
process.pid,
|
||||
"{:?} {}:",
|
||||
process,
|
||||
system_services.process_name(process.pid).unwrap_or("")
|
||||
);
|
||||
process.activate().unwrap();
|
||||
|
|
|
|||
|
|
@ -1290,6 +1290,10 @@ impl SystemServices {
|
|||
Err(xous_kernel::Error::BadAddress)?;
|
||||
}
|
||||
|
||||
// If memory is getting returned to the kernel, then it is memory that was
|
||||
// borrowed but
|
||||
if dest_pid.get() == 1 {}
|
||||
|
||||
// Iterators and `ptr.wrapping_add()` operate on `usize` types,
|
||||
// which effectively lowers the `len`.
|
||||
let usize_len = len / core::mem::size_of::<usize>();
|
||||
|
|
@ -1405,6 +1409,41 @@ impl SystemServices {
|
|||
Ok(new_tid)
|
||||
}
|
||||
|
||||
/// Destroy the given thread.
|
||||
/// NOTE: You MUST immediately switch away from this process.
|
||||
/// # Errors
|
||||
///
|
||||
/// * **ThreadNotAvailable**: The thread does not exist in this process
|
||||
#[cfg(baremetal)]
|
||||
pub fn destroy_thread(&mut self, pid: PID, thread: TID) -> Result<usize, xous_kernel::Error> {
|
||||
let current_pid = self.current_pid();
|
||||
assert_eq!(pid, current_pid);
|
||||
|
||||
let mut process = self.get_process_mut(pid)?;
|
||||
process.activate()?;
|
||||
|
||||
// Destroy the thread at a hardware level
|
||||
let mut arch_process = crate::arch::process::Process::current();
|
||||
let return_value = arch_process.destroy_thread(thread).unwrap();
|
||||
|
||||
// If there's another thread waiting on the return value of this thread,
|
||||
// wake it up and set its return value.
|
||||
|
||||
// Mark this process as `Ready`.
|
||||
// We can do this because the current thread has just exited. Note that if there
|
||||
// are no threads available, this is an error.
|
||||
|
||||
process.state = match process.state {
|
||||
ProcessState::Running(x) if x != 0 => ProcessState::Ready(x),
|
||||
_ => panic!("Process was in an invalid state: {:?}", process.state),
|
||||
};
|
||||
|
||||
// Switch to the next available TID. This moves the process back to a `Running` state.
|
||||
self.switch_to_thread(pid, None)?;
|
||||
|
||||
Ok(return_value)
|
||||
}
|
||||
|
||||
/// Allocate a new server ID for this process and return the address. If the
|
||||
/// server table is full, or if there is not enough memory to map the server queue,
|
||||
/// return an error.
|
||||
|
|
|
|||
Loading…
Reference in a new issue