Skip to main content

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