xref: /DragonOS/kernel/src/arch/riscv64/syscall/mod.rs (revision 539ee3eaeb83834e020efbb7582ed5ce2e9646ce)
1 /// 系统调用号
2 pub mod nr;
3 use system_error::SystemError;
4 
5 use crate::{exception::InterruptArch, process::ProcessManager, syscall::Syscall};
6 
7 use super::{interrupt::TrapFrame, CurrentIrqArch};
8 
9 /// 系统调用初始化
arch_syscall_init() -> Result<(), SystemError>10 pub fn arch_syscall_init() -> Result<(), SystemError> {
11     return Ok(());
12 }
13 
14 macro_rules! syscall_return {
15     ($val:expr, $regs:expr, $show:expr) => {{
16         let ret = $val;
17         $regs.a0 = ret;
18 
19         if $show {
20             let pid = ProcessManager::current_pcb().pid();
21             log::debug!("syscall return:pid={:?},ret= {:?}\n", pid, ret as isize);
22         }
23 
24         unsafe {
25             CurrentIrqArch::interrupt_disable();
26         }
27         return;
28     }};
29 }
30 
syscall_handler(syscall_num: usize, frame: &mut TrapFrame) -> ()31 pub(super) fn syscall_handler(syscall_num: usize, frame: &mut TrapFrame) -> () {
32     // debug!("syscall_handler: syscall_num: {}", syscall_num);
33     unsafe {
34         CurrentIrqArch::interrupt_enable();
35     }
36 
37     let args = [frame.a0, frame.a1, frame.a2, frame.a3, frame.a4, frame.a5];
38     let mut syscall_handle = || -> usize {
39         #[cfg(feature = "backtrace")]
40         {
41             Syscall::catch_handle(syscall_num, &args, frame)
42                 .unwrap_or_else(|e| e.to_posix_errno() as usize)
43         }
44         #[cfg(not(feature = "backtrace"))]
45         {
46             Syscall::handle(syscall_num, &args, frame)
47                 .unwrap_or_else(|e| e.to_posix_errno() as usize)
48         }
49     };
50     syscall_return!(syscall_handle(), frame, false);
51 }
52