      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109

    const SYSCALL_DUP: usize = 24;
    const SYSCALL_OPEN: usize = 56;
    const SYSCALL_CLOSE: usize = 57;
    const SYSCALL_PIPE: usize = 59;
    const SYSCALL_READ: usize = 63;
    const SYSCALL_WRITE: usize = 64;
    const SYSCALL_EXIT: usize = 93;
    const SYSCALL_SLEEP: usize = 101;
    const SYSCALL_YIELD: usize = 124;
    const SYSCALL_KILL: usize = 129;
    const SYSCALL_GET_TIME: usize = 169;
    const SYSCALL_GETPID: usize = 172;
    const SYSCALL_FORK: usize = 220;
    const SYSCALL_EXEC: usize = 221;
    const SYSCALL_WAITPID: usize = 260;
    const SYSCALL_THREAD_CREATE: usize = 1000;
    const SYSCALL_GETTID: usize = 1001;
    const SYSCALL_WAITTID: usize = 1002;
    const SYSCALL_MUTEX_CREATE: usize = 1010;
    const SYSCALL_MUTEX_LOCK: usize = 1011;
    const SYSCALL_MUTEX_UNLOCK: usize = 1012;
    const SYSCALL_SEMAPHORE_CREATE: usize = 1020;
    const SYSCALL_SEMAPHORE_UP: usize = 1021;
    const SYSCALL_SEMAPHORE_DOWN: usize = 1022;
    const SYSCALL_CONDVAR_CREATE: usize = 1030;
    const SYSCALL_CONDVAR_SIGNAL: usize = 1031;
    const SYSCALL_CONDVAR_WAIT: usize = 1032;

    mod fs;
    mod process;
    mod sync;
    mod thread;

    use fs::*;
    use process::*;
    use sync::*;
    use thread::*;

    pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
        if syscall_id !=SYSCALL_YIELD &&
           syscall_id !=SYSCALL_WAITPID &&
           !(syscall_id ==SYSCALL_READ && args[0] == 0) &&
           !(syscall_id ==SYSCALL_WRITE && (args[0] == 1|| args[0] == 2)) {
            kprintln!("[KERN] syscall::syscall(id: {}) begin", sys_id_str(syscall_id));
        }
        match syscall_id {
            SYSCALL_DUP => sys_dup(args[0]),
            SYSCALL_OPEN => sys_open(args[0] as *const u8, args[1] as u32),
            SYSCALL_CLOSE => sys_close(args[0]),
            SYSCALL_PIPE => sys_pipe(args[0] as *mut usize),
            SYSCALL_READ => sys_read(args[0], args[1] as *const u8, args[2]),
            SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]),
            SYSCALL_EXIT => sys_exit(args[0] as i32),
            SYSCALL_SLEEP => sys_sleep(args[0]),
            SYSCALL_YIELD => sys_yield(),
            SYSCALL_KILL => sys_kill(args[0], args[1] as u32),
            SYSCALL_GET_TIME => sys_get_time(),
            SYSCALL_GETPID => sys_getpid(),
            SYSCALL_FORK => sys_fork(),
            SYSCALL_EXEC => sys_exec(args[0] as *const u8, args[1] as *const usize),
            SYSCALL_WAITPID => sys_waitpid(args[0] as isize, args[1] as *mut i32),
            SYSCALL_THREAD_CREATE => sys_thread_create(args[0], args[1]),
            SYSCALL_GETTID => sys_gettid(),
            SYSCALL_WAITTID => sys_waittid(args[0]) as isize,
            SYSCALL_MUTEX_CREATE => sys_mutex_create(args[0] == 1),
            SYSCALL_MUTEX_LOCK => sys_mutex_lock(args[0]),
            SYSCALL_MUTEX_UNLOCK => sys_mutex_unlock(args[0]),
            SYSCALL_SEMAPHORE_CREATE => sys_semaphore_create(args[0]),
            SYSCALL_SEMAPHORE_UP => sys_semaphore_up(args[0]),
            SYSCALL_SEMAPHORE_DOWN => sys_semaphore_down(args[0]),
            SYSCALL_CONDVAR_CREATE => sys_condvar_create(args[0]),
            SYSCALL_CONDVAR_SIGNAL => sys_condvar_signal(args[0]),
            SYSCALL_CONDVAR_WAIT => sys_condvar_wait(args[0], args[1]),
            _ => panic!("Unsupported syscall_id: {}", syscall_id),
        }
    }

    pub fn sys_id_str(syscall_id: usize) -> &'static str {
        match syscall_id {
            SYSCALL_DUP => "sys_dup",
            SYSCALL_OPEN => "sys_open",
            SYSCALL_CLOSE => "sys_close",
            SYSCALL_PIPE => "sys_pipe",
            SYSCALL_READ => "sys_read",
            SYSCALL_WRITE => "sys_write",
            SYSCALL_EXIT => "sys_exit",
            SYSCALL_SLEEP => "sys_sleep",
            SYSCALL_YIELD => "sys_yield",
            SYSCALL_KILL => "sys_kill",
            SYSCALL_GET_TIME => "sys_get_time",
            SYSCALL_GETPID => "sys_getpid",
            SYSCALL_FORK => "sys_fork",
            SYSCALL_EXEC => "sys_exec",
            SYSCALL_WAITPID => "sys_waitpid",
            SYSCALL_THREAD_CREATE => "sys_thread_create",
            SYSCALL_GETTID => "sys_gettid",
            SYSCALL_WAITTID => "sys_waittid",
            SYSCALL_MUTEX_CREATE => "sys_mutex_create",
            SYSCALL_MUTEX_LOCK => "sys_mutex_lock",
            SYSCALL_MUTEX_UNLOCK => "sys_mutex_unlock",
            SYSCALL_SEMAPHORE_CREATE => "sys_semaphore_create",
            SYSCALL_SEMAPHORE_UP => "sys_semaphore_up",
            SYSCALL_SEMAPHORE_DOWN => "sys_semaphore_down",
            SYSCALL_CONDVAR_CREATE => "sys_condvar_create",
            SYSCALL_CONDVAR_SIGNAL => "sys_condvar_signal",
            SYSCALL_CONDVAR_WAIT => "sys_condvar_wait",
            _ => "Unsupported syscall_id",
        }
    }