[16] | 1 | /* |
---|
| 2 | * do_syscall.c - architecture independant entry-point for system calls. |
---|
| 3 | * |
---|
[23] | 4 | * Author Alain Greiner (2016) |
---|
[16] | 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #include <hal_types.h> |
---|
| 25 | #include <hal_irqmask.h> |
---|
| 26 | #include <do_syscall.h> |
---|
| 27 | #include <errno.h> |
---|
| 28 | #include <thread.h> |
---|
| 29 | #include <printk.h> |
---|
| 30 | #include <syscalls.h> |
---|
[408] | 31 | #include <shared_syscalls.h> |
---|
[16] | 32 | |
---|
[408] | 33 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[407] | 34 | // This ƒonction should never be called... |
---|
[408] | 35 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[407] | 36 | static int sys_undefined() |
---|
[23] | 37 | { |
---|
[428] | 38 | assert( false , __FUNCTION__ , "undefined system call" ); |
---|
[23] | 39 | return 0; |
---|
| 40 | } |
---|
| 41 | |
---|
[408] | 42 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[23] | 43 | // This array of pointers define the kernel functions implementing the syscalls. |
---|
[407] | 44 | // It must be kept consistent with the enum in "shared_syscalls.h" file. |
---|
[408] | 45 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[16] | 46 | |
---|
| 47 | typedef int (*sys_func_t) (); |
---|
| 48 | |
---|
| 49 | static const sys_func_t syscall_tbl[SYSCALLS_NR] = |
---|
| 50 | { |
---|
[23] | 51 | sys_thread_exit, // 0 |
---|
[407] | 52 | sys_thread_yield, // 1 |
---|
[23] | 53 | sys_thread_create, // 2 |
---|
| 54 | sys_thread_join, // 3 |
---|
| 55 | sys_thread_detach, // 4 |
---|
[409] | 56 | sys_thread_cancel, // 5 |
---|
[23] | 57 | sys_sem, // 6 |
---|
| 58 | sys_condvar, // 7 |
---|
| 59 | sys_barrier, // 8 |
---|
| 60 | sys_mutex, // 9 |
---|
[407] | 61 | |
---|
[408] | 62 | sys_exit, // 10 |
---|
[407] | 63 | sys_munmap, // 11 |
---|
[23] | 64 | sys_open, // 12 |
---|
[407] | 65 | sys_mmap, // 13 |
---|
[23] | 66 | sys_read, // 14 |
---|
| 67 | sys_write, // 15 |
---|
| 68 | sys_lseek, // 16 |
---|
| 69 | sys_close, // 17 |
---|
| 70 | sys_unlink, // 18 |
---|
| 71 | sys_pipe, // 19 |
---|
[407] | 72 | |
---|
[23] | 73 | sys_chdir, // 20 |
---|
| 74 | sys_mkdir, // 21 |
---|
| 75 | sys_mkfifo, // 22 |
---|
| 76 | sys_opendir, // 23 |
---|
| 77 | sys_readdir, // 24 |
---|
| 78 | sys_closedir, // 25 |
---|
| 79 | sys_getcwd, // 26 |
---|
[407] | 80 | sys_undefined, // 27 |
---|
[23] | 81 | sys_alarm, // 28 |
---|
| 82 | sys_rmdir, // 29 |
---|
[407] | 83 | |
---|
[23] | 84 | sys_utls, // 30 |
---|
| 85 | sys_chmod, // 31 |
---|
| 86 | sys_signal, // 32 |
---|
[50] | 87 | sys_timeofday, // 33 |
---|
[23] | 88 | sys_kill, // 34 |
---|
| 89 | sys_getpid, // 35 |
---|
| 90 | sys_fork, // 36 |
---|
| 91 | sys_exec, // 37 |
---|
| 92 | sys_stat, // 38 |
---|
[428] | 93 | sys_wait, // 39 |
---|
[407] | 94 | |
---|
| 95 | sys_get_config, // 40 |
---|
| 96 | sys_get_core, // 41 |
---|
| 97 | sys_get_cycle, // 42 |
---|
[428] | 98 | sys_display, // 43 |
---|
| 99 | sys_undefined, // 44 |
---|
[407] | 100 | sys_thread_sleep, // 45 |
---|
| 101 | sys_thread_wakeup, // 46 |
---|
[428] | 102 | sys_trace, // 47 |
---|
| 103 | sys_fg, // 48 |
---|
[16] | 104 | }; |
---|
| 105 | |
---|
[408] | 106 | //////////////////////////////////// |
---|
| 107 | char * syscall_str( uint32_t index ) |
---|
| 108 | { |
---|
| 109 | if ( index == SYS_THREAD_EXIT ) return "THREAD_EXIT"; // 0 |
---|
| 110 | else if( index == SYS_THREAD_YIELD ) return "THREAD_YIELD"; // 1 |
---|
| 111 | else if( index == SYS_THREAD_CREATE ) return "THREAD_CREATE"; // 2 |
---|
| 112 | else if( index == SYS_THREAD_JOIN ) return "THREAD_JOIN"; // 3 |
---|
| 113 | else if( index == SYS_THREAD_DETACH ) return "THREAD_DETACH"; // 4 |
---|
[409] | 114 | else if( index == SYS_THREAD_CANCEL ) return "THREAD_CANCEL"; // 5 |
---|
[408] | 115 | else if( index == SYS_SEM ) return "SEM"; // 6 |
---|
| 116 | else if( index == SYS_CONDVAR ) return "CONDVAR"; // 7 |
---|
| 117 | else if( index == SYS_BARRIER ) return "BARRIER"; // 8 |
---|
| 118 | else if( index == SYS_MUTEX ) return "MUTEX"; // 9 |
---|
| 119 | |
---|
| 120 | else if( index == SYS_EXIT ) return "EXIT"; // 10 |
---|
| 121 | else if( index == SYS_MUNMAP ) return "MUNMAP"; // 11 |
---|
| 122 | else if( index == SYS_OPEN ) return "OPEN"; // 12 |
---|
| 123 | else if( index == SYS_MMAP ) return "MMAP"; // 13 |
---|
| 124 | else if( index == SYS_READ ) return "READ"; // 14 |
---|
| 125 | else if( index == SYS_WRITE ) return "WRITE"; // 15 |
---|
| 126 | else if( index == SYS_LSEEK ) return "LSEEK"; // 16 |
---|
| 127 | else if( index == SYS_CLOSE ) return "CLOSE"; // 17 |
---|
| 128 | else if( index == SYS_UNLINK ) return "UNLINK"; // 18 |
---|
| 129 | else if( index == SYS_PIPE ) return "PIPE"; // 19 |
---|
| 130 | |
---|
| 131 | else if( index == SYS_CHDIR ) return "CHDIR"; // 20 |
---|
| 132 | else if( index == SYS_MKDIR ) return "MKDIR"; // 21 |
---|
| 133 | else if( index == SYS_MKFIFO ) return "MKFIFO"; // 22 |
---|
| 134 | else if( index == SYS_OPENDIR ) return "OPENDIR"; // 23 |
---|
| 135 | else if( index == SYS_READDIR ) return "READDIR"; // 24 |
---|
| 136 | else if( index == SYS_CLOSEDIR ) return "CLOSEDIR"; // 25 |
---|
| 137 | else if( index == SYS_GETCWD ) return "GETCWD"; // 26 |
---|
| 138 | else if( index == SYS_ALARM ) return "ALARM"; // 28 |
---|
| 139 | else if( index == SYS_RMDIR ) return "RMDIR"; // 29 |
---|
| 140 | |
---|
| 141 | else if( index == SYS_UTLS ) return "UTLS"; // 30 |
---|
| 142 | else if( index == SYS_CHMOD ) return "CHMOD"; // 31 |
---|
| 143 | else if( index == SYS_SIGNAL ) return "SIGNAL"; // 32 |
---|
| 144 | else if( index == SYS_TIMEOFDAY ) return "TIMEOFDAY"; // 33 |
---|
| 145 | else if( index == SYS_KILL ) return "KILL"; // 34 |
---|
| 146 | else if( index == SYS_GETPID ) return "GETPID"; // 35 |
---|
| 147 | else if( index == SYS_FORK ) return "FORK"; // 36 |
---|
| 148 | else if( index == SYS_EXEC ) return "EXEC"; // 37 |
---|
| 149 | else if( index == SYS_STAT ) return "STAT"; // 38 |
---|
[428] | 150 | else if( index == SYS_WAIT ) return "WAIT"; // 39 |
---|
[408] | 151 | |
---|
| 152 | else if( index == SYS_GET_CONFIG ) return "GET_CONFIG"; // 40 |
---|
| 153 | else if( index == SYS_GET_CORE ) return "GET_CORE"; // 41 |
---|
| 154 | else if( index == SYS_GET_CYCLE ) return "GET_CYCLE"; // 42 |
---|
[428] | 155 | else if( index == SYS_DISPLAY ) return "DISPLAY"; // 43 |
---|
| 156 | else if( index == SYS_THREAD_SLEEP ) return "THREAD_SLEEP"; // 45 |
---|
| 157 | else if( index == SYS_THREAD_WAKEUP ) return "THREAD_WAKEUP"; // 46 |
---|
| 158 | else if( index == SYS_TRACE ) return "TRACE"; // 47 |
---|
| 159 | else if( index == SYS_FG ) return "FG"; // 48 |
---|
[408] | 160 | |
---|
| 161 | else return "undefined"; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | |
---|
[16] | 165 | ////////////////////////////////// |
---|
| 166 | reg_t do_syscall( thread_t * this, |
---|
| 167 | reg_t arg0, |
---|
| 168 | reg_t arg1, |
---|
| 169 | reg_t arg2, |
---|
| 170 | reg_t arg3, |
---|
| 171 | reg_t service_num ) |
---|
| 172 | { |
---|
[408] | 173 | int error = 0; |
---|
[16] | 174 | |
---|
| 175 | // update user time |
---|
| 176 | thread_user_time_update( this ); |
---|
| 177 | |
---|
| 178 | // check syscall index |
---|
| 179 | if( service_num >= SYSCALLS_NR ) |
---|
| 180 | { |
---|
| 181 | printk("\n[ERROR] in %s : Undefined syscall %d, for thread %x\n", |
---|
| 182 | __FUNCTION__ , service_num , this ); |
---|
| 183 | |
---|
| 184 | this->errno = ENOSYS; |
---|
| 185 | hal_disable_irq(NULL); |
---|
| 186 | return ENOSYS;; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | // reset errno |
---|
| 190 | this->errno = 0; |
---|
| 191 | |
---|
| 192 | // call relevant kernel function |
---|
| 193 | error = syscall_tbl[service_num] ( arg0 , arg1 , arg2 , arg3 ); |
---|
| 194 | |
---|
[409] | 195 | // check kernel stack overflow |
---|
| 196 | assert( (this->signature == THREAD_SIGNATURE), __FUNCTION__, "kernel stack overflow\n" ); |
---|
| 197 | |
---|
[16] | 198 | // update kernel time |
---|
| 199 | thread_kernel_time_update( this ); |
---|
| 200 | |
---|
| 201 | return error; |
---|
| 202 | } |
---|