From 7ab2338ff3a1a77396f9298771e7e07f13825bb2 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 1 Nov 2020 16:27:39 +0800 Subject: [PATCH] kernel: add `in_irq` parameter to syscall::handle() This parameter will determine whether a syscall is being handled from within an interrupt context. This is because certain calls cannot be called from such a context. Signed-off-by: Sean Cross --- kernel/src/arch/hosted.rs | 6 +++--- kernel/src/arch/riscv/irq.rs | 12 ++++++------ kernel/src/syscall.rs | 10 ++++++++-- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/kernel/src/arch/hosted.rs b/kernel/src/arch/hosted.rs index 1b4f1c36f..e49831d73 100644 --- a/kernel/src/arch/hosted.rs +++ b/kernel/src/arch/hosted.rs @@ -544,14 +544,14 @@ pub fn idle() -> bool { "Unable to send response to process: {:?} -- terminating", _e ); - crate::syscall::handle(pid, thread_id, SysCall::TerminateProcess).ok(); + crate::syscall::handle(pid, thread_id, false, SysCall::TerminateProcess).ok(); }); // println!("KERNEL: Done sending"); } // Handle the syscall within the Xous kernel let response = - crate::syscall::handle(pid, thread_id, call).unwrap_or_else(Result::Error); + crate::syscall::handle(pid, thread_id, false, call).unwrap_or_else(Result::Error); // println!("KERNEL({}): Syscall response {:?}", pid, response); // There's a response if it wasn't a blocked process and we're not terminating. @@ -580,7 +580,7 @@ pub fn idle() -> bool { "KERNEL({}): Unable to send response to process: {:?} -- terminating", pid, _e ); - crate::syscall::handle(pid, thread_id, SysCall::TerminateProcess).ok(); + crate::syscall::handle(pid, thread_id, false, SysCall::TerminateProcess).ok(); }); crate::arch::process::set_current_pid(existing_pid); // SystemServices::with_mut(|ss| { diff --git a/kernel/src/arch/riscv/irq.rs b/kernel/src/arch/riscv/irq.rs index 13b895736..2e0ba52e5 100644 --- a/kernel/src/arch/riscv/irq.rs +++ b/kernel/src/arch/riscv/irq.rs @@ -96,8 +96,8 @@ pub extern "C" fn trap_handler( }) }); - let response = - crate::syscall::handle(pid, tid, call).unwrap_or_else(|e| xous_kernel::Result::Error(e)); + let response = crate::syscall::handle(pid, tid, unsafe { PREVIOUS_PAIR.is_some() }, call) + .unwrap_or_else(|e| xous_kernel::Result::Error(e)); // println!("Syscall Result: {:?}", response); ArchProcess::with_current_mut(|p| { @@ -122,7 +122,10 @@ pub extern "C" fn trap_handler( match ex { RiscvException::StorePageFault(pc, addr) | RiscvException::LoadPageFault(pc, addr) => { #[cfg(any(feature = "debug-print", feature = "print-panics"))] - println!("KERNEL({}): RISC-V fault: {} @ {:08x}, addr {:08x}", pid, ex, pc, addr); + println!( + "KERNEL({}): RISC-V fault: {} @ {:08x}, addr {:08x}", + pid, ex, pc, addr + ); let entry = crate::arch::mem::pagetable_entry(addr).unwrap_or_else(|x| { // MemoryManagerHandle::get().print_ownership(); MemoryMapping::current().print_map(); @@ -212,9 +215,6 @@ pub extern "C" fn trap_handler( if PREVIOUS_PAIR.is_none() { let tid = crate::arch::process::current_tid(); PREVIOUS_PAIR = Some((pid, tid)); - // println!("ISR: Setting previous pair to ({}, {})", pid, tid); - // } else { - // println!("ISR: Previous pair is not None"); } } crate::irq::handle(irqs_pending).expect("Couldn't handle IRQ"); diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 656a37a63..f9de4f223 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -514,10 +514,16 @@ fn receive_message(pid: PID, tid: TID, sid: SID) -> SysCallResult { }) } -pub fn handle(pid: PID, tid: TID, call: SysCall) -> SysCallResult { +pub fn handle(pid: PID, tid: TID, irq: bool, call: SysCall) -> SysCallResult { #[cfg(feature = "debug-print")] print!("KERNEL({}:{}): Syscall {:x?}", pid, tid, call); - let result = handle_inner(pid, tid, call); + + let result = if irq && !call.can_call_from_interrupt() { + Err(xous_kernel::Error::InvalidSyscall) + } else { + handle_inner(pid, tid, call) + }; + #[cfg(feature = "debug-print")] println!( " -> ({}:{}) {:?}",