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 {
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 {
pid: unsafe { PROCESS_TABLE.current },
pid,
}
}
@ -184,8 +187,9 @@ impl Process {
/// Set the current thread number.
pub fn set_thread(&mut self, thread: TID) -> Result<(), xous::Error> {
let mut process = unsafe { &mut *PROCESS };
// println!("KERNEL({}:{}): Switching to thread {}", self.pid, process.hardware_thread - 1, thread);
assert!(
thread > 0 && thread <= process.threads.len(),
thread <= process.threads.len(),
"attempt to switch to an invalid thread {}",
thread
);
@ -196,7 +200,7 @@ impl Process {
pub fn thread_mut(&mut self, thread: TID) -> &mut Thread {
let process = unsafe { &mut *PROCESS };
assert!(
thread > 0 && thread <= process.threads.len(),
thread <= process.threads.len(),
"attempt to retrieve an invalid thread {}",
thread
);
@ -206,7 +210,7 @@ impl Process {
// pub fn thread(&self, thread: TID) -> &Thread {
// let process = unsafe { &mut *PROCESS };
// assert!(
// thread > 0 && thread <= process.threads.len(),
// thread <= process.threads.len(),
// "attempt to retrieve an invalid thread {}",
// thread
// );
@ -316,7 +320,7 @@ impl Process {
// Create the new context and set it to run in the new address space.
let pid = self.pid.get();
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(
thread,
pid == 1,
@ -411,7 +415,7 @@ pub fn current_pid() -> PID {
}
pub fn current_tid() -> TID {
unsafe { ((*PROCESS).hardware_thread) + 1 }
unsafe { ((*PROCESS).hardware_thread) - 1 }
}
// 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];
#[allow(dead_code)]
#[cfg(baremetal)]
pub fn handle(irqs_pending: usize) -> Result<xous::Result, xous::Error> {
// Unsafe is required here because we're accessing a static
// 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.
pub fn with<F, R>(f: F) -> R
where
F: FnOnce(&MemoryManager) -> R,
{
#[cfg(baremetal)]
unsafe {
f(&MEMORY_MANAGER)
}
// /// Calls the provided function with the current inner process state.
// pub fn with<F, R>(f: F) -> R
// where
// F: FnOnce(&MemoryManager) -> R,
// {
// #[cfg(baremetal)]
// unsafe {
// f(&MEMORY_MANAGER)
// }
#[cfg(not(baremetal))]
MEMORY_MANAGER.with(|ss| f(&ss.borrow()))
}
// #[cfg(not(baremetal))]
// MEMORY_MANAGER.with(|ss| f(&ss.borrow()))
// }
pub fn with_mut<F, R>(f: F) -> R
where

View file

@ -397,7 +397,7 @@ impl SystemServices {
/// 1. Pause the current process and switch to the new one
/// 2. Save the process state, if it hasn't already been saved
/// 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> {
// 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.
@ -406,6 +406,7 @@ impl SystemServices {
let mut current = self
.get_process_mut(current_pid)
.expect("couldn't get current PID");
println!("Finishing callback in PID {}", current_pid);
current.state = match current.state {
ProcessState::Running(0) => ProcessState::Sleeping,
ProcessState::Running(x) => ProcessState::Ready(x),
@ -423,13 +424,14 @@ impl SystemServices {
let available_contexts = match process.state {
ProcessState::Ready(x) if x & 1 << tid != 0 => x & !(1 << tid),
other => panic!(
"process was in an invalid state {:?} -- thread {} not available to run",
other, tid
"process {} was in an invalid state {:?} -- thread {} not available to run",
pid, other, tid
),
};
process.state = ProcessState::Running(available_contexts);
// process.current_thread = tid as u8;
process.mapping.activate()?;
process.activate()?;
// Activate the current context
let mut arch_process = crate::arch::process::Process::current();
@ -472,7 +474,7 @@ impl SystemServices {
.get_process_mut(current_pid)
.expect("couldn't get current PID");
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),
};
println!("Making PID {} state {:?}", current_pid, current.state);
@ -495,6 +497,7 @@ impl SystemServices {
process.previous_thread = process.current_thread;
process.current_thread = arch::process::IRQ_TID;
process.mapping.activate()?;
process.activate()?;
}
// Switch to new process memory space, allowing us to save the context