kernel: fix interrupts

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-08-10 22:43:38 +08:00
parent 31f63c3f5f
commit cb959eb1cf
4 changed files with 30 additions and 23 deletions

View file

@ -119,8 +119,11 @@ pub struct Thread {
impl Process { impl Process {
pub fn current() -> Process { pub fn current() -> Process {
let pid = unsafe { PROCESS_TABLE.current };
let hardware_pid = (riscv::register::satp::read().bits() >> 22) & ((1 << 9) - 1);
assert!((pid.get() as usize) == hardware_pid);
Process { Process {
pid: unsafe { PROCESS_TABLE.current }, pid,
} }
} }
@ -184,8 +187,9 @@ impl Process {
/// Set the current thread number. /// Set the current thread number.
pub fn set_thread(&mut self, thread: TID) -> Result<(), xous::Error> { pub fn set_thread(&mut self, thread: TID) -> Result<(), xous::Error> {
let mut process = unsafe { &mut *PROCESS }; let mut process = unsafe { &mut *PROCESS };
// println!("KERNEL({}:{}): Switching to thread {}", self.pid, process.hardware_thread - 1, thread);
assert!( assert!(
thread > 0 && thread <= process.threads.len(), thread <= process.threads.len(),
"attempt to switch to an invalid thread {}", "attempt to switch to an invalid thread {}",
thread thread
); );
@ -196,7 +200,7 @@ impl Process {
pub fn thread_mut(&mut self, thread: TID) -> &mut Thread { pub fn thread_mut(&mut self, thread: TID) -> &mut Thread {
let process = unsafe { &mut *PROCESS }; let process = unsafe { &mut *PROCESS };
assert!( assert!(
thread > 0 && thread <= process.threads.len(), thread <= process.threads.len(),
"attempt to retrieve an invalid thread {}", "attempt to retrieve an invalid thread {}",
thread thread
); );
@ -206,7 +210,7 @@ impl Process {
// pub fn thread(&self, thread: TID) -> &Thread { // pub fn thread(&self, thread: TID) -> &Thread {
// let process = unsafe { &mut *PROCESS }; // let process = unsafe { &mut *PROCESS };
// assert!( // assert!(
// thread > 0 && thread <= process.threads.len(), // thread <= process.threads.len(),
// "attempt to retrieve an invalid thread {}", // "attempt to retrieve an invalid thread {}",
// thread // thread
// ); // );
@ -316,7 +320,7 @@ impl Process {
// Create the new context and set it to run in the new address space. // Create the new context and set it to run in the new address space.
let pid = self.pid.get(); let pid = self.pid.get();
let thread = self.thread_mut(new_tid); let thread = self.thread_mut(new_tid);
println!("Setting up thread {}, pid {}", new_tid, pid); // println!("Setting up thread {}, pid {}", new_tid, pid);
crate::arch::syscall::invoke( crate::arch::syscall::invoke(
thread, thread,
pid == 1, pid == 1,
@ -411,7 +415,7 @@ pub fn current_pid() -> PID {
} }
pub fn current_tid() -> TID { pub fn current_tid() -> TID {
unsafe { ((*PROCESS).hardware_thread) + 1 } unsafe { ((*PROCESS).hardware_thread) - 1 }
} }
// pub struct ProcessHandle<'a> { // pub struct ProcessHandle<'a> {

View file

@ -4,7 +4,7 @@ use xous::{MemoryAddress, PID};
static mut IRQ_HANDLERS: [Option<(PID, MemoryAddress, Option<MemoryAddress>)>; 32] = [None; 32]; static mut IRQ_HANDLERS: [Option<(PID, MemoryAddress, Option<MemoryAddress>)>; 32] = [None; 32];
#[allow(dead_code)] #[cfg(baremetal)]
pub fn handle(irqs_pending: usize) -> Result<xous::Result, xous::Error> { pub fn handle(irqs_pending: usize) -> Result<xous::Result, xous::Error> {
// Unsafe is required here because we're accessing a static // Unsafe is required here because we're accessing a static
// mutable value, and it could be modified from various threads. // mutable value, and it could be modified from various threads.

View file

@ -81,19 +81,19 @@ impl MemoryManager {
} }
} }
/// Calls the provided function with the current inner process state. // /// Calls the provided function with the current inner process state.
pub fn with<F, R>(f: F) -> R // pub fn with<F, R>(f: F) -> R
where // where
F: FnOnce(&MemoryManager) -> R, // F: FnOnce(&MemoryManager) -> R,
{ // {
#[cfg(baremetal)] // #[cfg(baremetal)]
unsafe { // unsafe {
f(&MEMORY_MANAGER) // f(&MEMORY_MANAGER)
} // }
#[cfg(not(baremetal))] // #[cfg(not(baremetal))]
MEMORY_MANAGER.with(|ss| f(&ss.borrow())) // MEMORY_MANAGER.with(|ss| f(&ss.borrow()))
} // }
pub fn with_mut<F, R>(f: F) -> R pub fn with_mut<F, R>(f: F) -> R
where where

View file

@ -397,7 +397,7 @@ impl SystemServices {
/// 1. Pause the current process and switch to the new one /// 1. Pause the current process and switch to the new one
/// 2. Save the process state, if it hasn't already been saved /// 2. Save the process state, if it hasn't already been saved
/// 3. Run the new process, returning to an illegal instruction /// 3. Run the new process, returning to an illegal instruction
#[allow(dead_code)] #[cfg(baremetal)]
pub fn finish_callback_and_resume(&mut self, pid: PID, tid: TID) -> Result<(), xous::Error> { pub fn finish_callback_and_resume(&mut self, pid: PID, tid: TID) -> Result<(), xous::Error> {
// Get the current process (which was the interrupt handler) and mark it // Get the current process (which was the interrupt handler) and mark it
// as Ready. Note that the new PID may very well be the same PID. // as Ready. Note that the new PID may very well be the same PID.
@ -406,6 +406,7 @@ impl SystemServices {
let mut current = self let mut current = self
.get_process_mut(current_pid) .get_process_mut(current_pid)
.expect("couldn't get current PID"); .expect("couldn't get current PID");
println!("Finishing callback in PID {}", current_pid);
current.state = match current.state { current.state = match current.state {
ProcessState::Running(0) => ProcessState::Sleeping, ProcessState::Running(0) => ProcessState::Sleeping,
ProcessState::Running(x) => ProcessState::Ready(x), ProcessState::Running(x) => ProcessState::Ready(x),
@ -423,13 +424,14 @@ impl SystemServices {
let available_contexts = match process.state { let available_contexts = match process.state {
ProcessState::Ready(x) if x & 1 << tid != 0 => x & !(1 << tid), ProcessState::Ready(x) if x & 1 << tid != 0 => x & !(1 << tid),
other => panic!( other => panic!(
"process was in an invalid state {:?} -- thread {} not available to run", "process {} was in an invalid state {:?} -- thread {} not available to run",
other, tid pid, other, tid
), ),
}; };
process.state = ProcessState::Running(available_contexts); process.state = ProcessState::Running(available_contexts);
// process.current_thread = tid as u8; // process.current_thread = tid as u8;
process.mapping.activate()?; process.mapping.activate()?;
process.activate()?;
// Activate the current context // Activate the current context
let mut arch_process = crate::arch::process::Process::current(); let mut arch_process = crate::arch::process::Process::current();
@ -472,7 +474,7 @@ impl SystemServices {
.get_process_mut(current_pid) .get_process_mut(current_pid)
.expect("couldn't get current PID"); .expect("couldn't get current PID");
current.state = match current.state { current.state = match current.state {
ProcessState::Running(x) => ProcessState::Ready(x | (1 << current.current_thread)), ProcessState::Running(x) => ProcessState::Ready(x | (1 << arch::process::current_tid())),
y => panic!("current process was {:?}, not 'Running(_)'", y), y => panic!("current process was {:?}, not 'Running(_)'", y),
}; };
println!("Making PID {} state {:?}", current_pid, current.state); println!("Making PID {} state {:?}", current_pid, current.state);
@ -495,6 +497,7 @@ impl SystemServices {
process.previous_thread = process.current_thread; process.previous_thread = process.current_thread;
process.current_thread = arch::process::IRQ_TID; process.current_thread = arch::process::IRQ_TID;
process.mapping.activate()?; process.mapping.activate()?;
process.activate()?;
} }
// Switch to new process memory space, allowing us to save the context // Switch to new process memory space, allowing us to save the context