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;
15const SYSCALL_YIELD: usize = 124;
16const SYSCALL_GET_TIME: usize = 169;
17
18mod fs;
19mod process;
20
21use fs::*;
22use process::*;
23
24/// handle syscall exception with `syscall_id` and other arguments
25pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
26 match syscall_id {
27 SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]),
28 SYSCALL_EXIT => sys_exit(args[0] as i32),
29 SYSCALL_YIELD => sys_yield(),
30 SYSCALL_GET_TIME => sys_get_time(),
31 _ => panic!("Unsupported syscall_id: {}", syscall_id),
32 }
33}