diff --git a/kernel/src/arch/riscv/process.rs b/kernel/src/arch/riscv/process.rs index 4507499a4..ecfeadbb1 100644 --- a/kernel/src/arch/riscv/process.rs +++ b/kernel/src/arch/riscv/process.rs @@ -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> { diff --git a/kernel/src/irq.rs b/kernel/src/irq.rs index 5825fd2f6..12100ffc8 100644 --- a/kernel/src/irq.rs +++ b/kernel/src/irq.rs @@ -4,7 +4,7 @@ use xous::{MemoryAddress, PID}; static mut IRQ_HANDLERS: [Option<(PID, MemoryAddress, Option)>; 32] = [None; 32]; -#[allow(dead_code)] +#[cfg(baremetal)] pub fn handle(irqs_pending: usize) -> Result { // Unsafe is required here because we're accessing a static // mutable value, and it could be modified from various threads. diff --git a/kernel/src/mem.rs b/kernel/src/mem.rs index 4311b993d..b6a473c43 100644 --- a/kernel/src/mem.rs +++ b/kernel/src/mem.rs @@ -81,19 +81,19 @@ impl MemoryManager { } } - /// Calls the provided function with the current inner process state. - pub fn with(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: 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: F) -> R where diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 494f3387f..195c12076 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -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