os/syscall/mod.rs
1//! Implementation of syscalls
2//!
3//! The single entry point to all system calls, [`syscall()`], is called
4//! whenever userspace wishes to perform a system call using the `ecall`
5//! instruction. In this case, the processor raises an 'Environment call from
6//! U-mode' exception, which is handled as one of the cases in
7//! [`crate::trap::trap_handler`].
8//!
9//! For clarity, each single syscall is implemented as its own function, named
10//! `sys_` then the name of the syscall. You can find functions like this in
11//! submodules, and you should also implement syscalls this way.
12
13const SYSCALL_WRITE: usize = 64;
14const SYSCALL_EXIT: usize = 93;
15
16mod fs;
17mod process;
18
19use fs::*;
20use process::*;
21
22/// handle syscall exception with `syscall_id` and other arguments
23pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
24 match syscall_id {
25 SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]),
26 SYSCALL_EXIT => sys_exit(args[0] as i32),
27 _ => panic!("Unsupported syscall_id: {}", syscall_id),
28 }
29}