diff --git a/kernel/src/arch/riscv/irq.rs b/kernel/src/arch/riscv/irq.rs index c6773533c..4d04ea497 100644 --- a/kernel/src/arch/riscv/irq.rs +++ b/kernel/src/arch/riscv/irq.rs @@ -3,9 +3,9 @@ use crate::arch::mem::MemoryMapping; use crate::arch::process::Process as ArchProcess; use crate::arch::process::{Thread, RETURN_FROM_ISR}; use crate::mem::{MemoryManager, PAGE_SIZE}; +use crate::services::SystemServices; use riscv::register::{scause, sepc, sie, sstatus, stval, vexriscv::sim, vexriscv::sip}; use xous::{SysCall, PID, TID}; -use crate::services::SystemServices; extern "Rust" { fn _xous_syscall_return_result(result: &xous::Result, context: &Thread) -> !; @@ -82,27 +82,35 @@ pub extern "C" fn trap_handler( // will want to adjust the return value of the current process prior to // performing the switch in order to avoid constantly executing the same // instruction. - let (tid, thread) = ArchProcess::with_current(|p| (p.current_tid(), p.current_thread())); - thread.sepc += 4; - let call = SysCall::from_args(a0, a1, a2, a3, a4, a5, a6, a7).unwrap_or_else(|_| unsafe { - _xous_syscall_return_result(&xous::Result::Error(xous::Error::UnhandledSyscall), thread) + let tid = ArchProcess::with_current_mut(|p| { + p.current_thread().sepc += 4; + p.current_tid() + }); + let call = SysCall::from_args(a0, a1, a2, a3, a4, a5, a6, a7).unwrap_or_else(|_| { + ArchProcess::with_current_mut(|p| unsafe { + _xous_syscall_return_result( + &xous::Result::Error(xous::Error::UnhandledSyscall), + p.current_thread(), + ) + }) }); let response = crate::syscall::handle(pid, tid, call).unwrap_or_else(|e| xous::Result::Error(e)); println!("Syscall Result: {:?}", response); - let thread = ArchProcess::with_current(|p| p.current_thread()); - - // If we're resuming a process that was previously sleeping, restore the - // context. Otherwise, keep the context the same but pass the return - // values in 8 return registers. - if response == xous::Result::ResumeProcess { - crate::arch::syscall::resume(current_pid().get() == 1, thread); - } else { - // println!("Returning to address {:08x}", tid.sepc); - unsafe { _xous_syscall_return_result(&response, thread) }; - } + ArchProcess::with_current_mut(|p| { + let thread = p.current_thread(); + // If we're resuming a process that was previously sleeping, restore the + // context. Otherwise, keep the context the same but pass the return + // values in 8 return registers. + if response == xous::Result::ResumeProcess { + crate::arch::syscall::resume(current_pid().get() == 1, thread); + } else { + // println!("Returning to address {:08x}", tid.sepc); + unsafe { _xous_syscall_return_result(&response, thread) }; + } + }); } use crate::arch::exception::RiscvException; @@ -127,7 +135,9 @@ pub extern "C" fn trap_handler( // the page isn't shared, then this is a reserved page. Allocate // a real page to back it and resume execution. if flags & 1 == 0 && flags != 0 && flags & (1 << 8) == 0 { - let new_page = MemoryManager::with_mut(|mm| mm.alloc_page(pid).expect("Couldn't allocate new page")); + let new_page = MemoryManager::with_mut(|mm| { + mm.alloc_page(pid).expect("Couldn't allocate new page") + }); let ppn1 = (new_page >> 22) & ((1 << 12) - 1); let ppn0 = (new_page >> 12) & ((1 << 10) - 1); unsafe { @@ -149,7 +159,7 @@ pub extern "C" fn trap_handler( flush_mmu(); }; - ArchProcess::with_current(|process| { + ArchProcess::with_current_mut(|process| { crate::arch::syscall::resume( current_pid().get() == 1, process.current_thread(), @@ -179,7 +189,7 @@ pub extern "C" fn trap_handler( // Re-enable interrupts now that they're handled enable_all_irqs(); - ArchProcess::with_current(|process| { + ArchProcess::with_current_mut(|process| { crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread()) }); } @@ -202,7 +212,7 @@ pub extern "C" fn trap_handler( } } crate::irq::handle(irqs_pending).expect("Couldn't handle IRQ"); - ArchProcess::with_current(|process| { + ArchProcess::with_current_mut(|process| { crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread()) }) } diff --git a/kernel/src/arch/riscv/process.rs b/kernel/src/arch/riscv/process.rs index d68f1ab06..dd202d922 100644 --- a/kernel/src/arch/riscv/process.rs +++ b/kernel/src/arch/riscv/process.rs @@ -126,6 +126,15 @@ impl Process { f(&process) } + /// Calls the provided function with the current inner process state. + pub fn with_current_mut(f: F) -> R + where + F: FnOnce(&mut Process) -> R, + { + let mut process = Self::current(); + f(&mut process) + } + pub fn with_inner_mut(f: F) -> R where F: FnOnce(&mut ProcessInner) -> R, diff --git a/kernel/src/services.rs b/kernel/src/services.rs index a7503bb0f..d6f78328f 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -43,9 +43,6 @@ pub use crate::arch::process::{INITIAL_TID, MAX_PROCESS_COUNT}; /// A big unifying struct containing all of the system state. /// This is inherited from the stage 1 bootloader. pub struct SystemServices { - /// Current PID. - // pid: PID, - /// A table of all processes in the system pub processes: [Process; MAX_PROCESS_COUNT], @@ -218,6 +215,7 @@ impl Process { } } +#[cfg(not(baremetal))] thread_local!(static SYSTEM_SERVICES: RefCell = RefCell::new(SystemServices { processes: [Process { state: ProcessState::Free, @@ -234,6 +232,23 @@ thread_local!(static SYSTEM_SERVICES: RefCell = RefCell::new(Sys _syscall_depth: 0, })); +#[cfg(baremetal)] +static mut SYSTEM_SERVICES: SystemServices = SystemServices { + processes: [Process { + state: ProcessState::Free, + ppid: unsafe { PID::new_unchecked(1) }, + pid: unsafe { PID::new_unchecked(1) }, + mapping: arch::mem::DEFAULT_MEMORY_MAPPING, + // current_thread: 0, + previous_context: INITIAL_TID as u8, + }; MAX_PROCESS_COUNT], + // Note we can't use MAX_SERVER_COUNT here because of how Rust's + // macro tokenization works + servers: filled_array![None; 32], + _syscall_stack: [(0, 0), (0, 0), (0, 0)], + _syscall_depth: 0, +}; + impl core::fmt::Debug for Process { fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> { write!( @@ -253,9 +268,8 @@ impl SystemServices { F: FnOnce(&SystemServices) -> R, { #[cfg(baremetal)] - { - let ss = unsafe { SYSTEM_SERVICES }.borrow(); - f(&*ss) + unsafe { + f(&SYSTEM_SERVICES) } #[cfg(not(baremetal))] SYSTEM_SERVICES.with(|ss| f(&ss.borrow())) @@ -266,9 +280,8 @@ impl SystemServices { F: FnOnce(&mut SystemServices) -> R, { #[cfg(baremetal)] - { - let mut ss = unsafe { SYSTEM_SERVICES }.borrow_mut(); - f(&mut *ss) + unsafe { + f(&mut SYSTEM_SERVICES) } #[cfg(not(baremetal))] @@ -781,8 +794,7 @@ impl SystemServices { // Ensure the new process can be run. match new.state { ProcessState::Free => return Err(xous::Error::ProcessNotFound), - ProcessState::Setup(_) | - ProcessState::Allocated => new_tid = INITIAL_TID, + ProcessState::Setup(_) | ProcessState::Allocated => new_tid = INITIAL_TID, ProcessState::Running(x) | ProcessState::Ready(x) => { // If no new context is specified, take the previous // context. If that is not runnable, do a round-robin