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.
12const SYSCALL_READ: usize = 63;
13const SYSCALL_WRITE: usize = 64;
14const SYSCALL_EXIT: usize = 93;
15const SYSCALL_YIELD: usize = 124;
16const SYSCALL_GET_TIME: usize = 169;
17const SYSCALL_GETPID: usize = 172;
18const SYSCALL_FORK: usize = 220;
19const SYSCALL_EXEC: usize = 221;
20const SYSCALL_WAITPID: usize = 260;
21
22mod fs;
23mod process;
24
25use fs::*;
26use process::*;
27/// handle syscall exception with `syscall_id` and other arguments
28pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
29    match syscall_id {
30        SYSCALL_READ => sys_read(args[0], args[1] as *const u8, args[2]),
31        SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]),
32        SYSCALL_EXIT => sys_exit(args[0] as i32),
33        SYSCALL_YIELD => sys_yield(),
34        SYSCALL_GET_TIME => sys_get_time(),
35        SYSCALL_GETPID => sys_getpid(),
36        SYSCALL_FORK => sys_fork(),
37        SYSCALL_EXEC => sys_exec(args[0] as *const u8),
38        SYSCALL_WAITPID => sys_waitpid(args[0] as isize, args[1] as *mut i32),
39        _ => panic!("Unsupported syscall_id: {}", syscall_id),
40    }
41}