[16] | 1 | /* |
---|
| 2 | * do_syscall.c - architecture independant entry-point for system calls. |
---|
| 3 | * |
---|
[626] | 4 | * Author Alain Greiner (2016,2017,2018, 2019) |
---|
[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 | |
---|
[457] | 24 | #include <hal_kernel_types.h> |
---|
[16] | 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> |
---|
[527] | 32 | #include <syscalls_numbers.h> |
---|
[16] | 33 | |
---|
[408] | 34 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[407] | 35 | // This ƒonction should never be called... |
---|
[408] | 36 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[583] | 37 | int sys_undefined( void ) |
---|
[23] | 38 | { |
---|
[669] | 39 | assert( __FUNCTION__, false , "undefined system call" ); |
---|
[23] | 40 | return 0; |
---|
| 41 | } |
---|
| 42 | |
---|
[408] | 43 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[23] | 44 | // This array of pointers define the kernel functions implementing the syscalls. |
---|
[583] | 45 | // It must be kept consistent with the enum in "syscalls_numbers.h" file. |
---|
[408] | 46 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[16] | 47 | |
---|
| 48 | typedef int (*sys_func_t) (); |
---|
| 49 | |
---|
| 50 | static const sys_func_t syscall_tbl[SYSCALLS_NR] = |
---|
| 51 | { |
---|
[23] | 52 | sys_thread_exit, // 0 |
---|
[407] | 53 | sys_thread_yield, // 1 |
---|
[23] | 54 | sys_thread_create, // 2 |
---|
| 55 | sys_thread_join, // 3 |
---|
| 56 | sys_thread_detach, // 4 |
---|
[409] | 57 | sys_thread_cancel, // 5 |
---|
[23] | 58 | sys_sem, // 6 |
---|
| 59 | sys_condvar, // 7 |
---|
| 60 | sys_barrier, // 8 |
---|
| 61 | sys_mutex, // 9 |
---|
[407] | 62 | |
---|
[610] | 63 | sys_rename, // 10 |
---|
[407] | 64 | sys_munmap, // 11 |
---|
[23] | 65 | sys_open, // 12 |
---|
[407] | 66 | sys_mmap, // 13 |
---|
[23] | 67 | sys_read, // 14 |
---|
| 68 | sys_write, // 15 |
---|
| 69 | sys_lseek, // 16 |
---|
| 70 | sys_close, // 17 |
---|
| 71 | sys_unlink, // 18 |
---|
| 72 | sys_pipe, // 19 |
---|
[407] | 73 | |
---|
[23] | 74 | sys_chdir, // 20 |
---|
| 75 | sys_mkdir, // 21 |
---|
| 76 | sys_mkfifo, // 22 |
---|
| 77 | sys_opendir, // 23 |
---|
| 78 | sys_readdir, // 24 |
---|
| 79 | sys_closedir, // 25 |
---|
| 80 | sys_getcwd, // 26 |
---|
[437] | 81 | sys_isatty, // 27 |
---|
[23] | 82 | sys_alarm, // 28 |
---|
| 83 | sys_rmdir, // 29 |
---|
[407] | 84 | |
---|
[23] | 85 | sys_utls, // 30 |
---|
| 86 | sys_chmod, // 31 |
---|
| 87 | sys_signal, // 32 |
---|
[50] | 88 | sys_timeofday, // 33 |
---|
[23] | 89 | sys_kill, // 34 |
---|
| 90 | sys_getpid, // 35 |
---|
| 91 | sys_fork, // 36 |
---|
| 92 | sys_exec, // 37 |
---|
| 93 | sys_stat, // 38 |
---|
[428] | 94 | sys_wait, // 39 |
---|
[407] | 95 | |
---|
| 96 | sys_get_config, // 40 |
---|
[637] | 97 | sys_get_core_id, // 41 |
---|
[407] | 98 | sys_get_cycle, // 42 |
---|
[428] | 99 | sys_display, // 43 |
---|
[583] | 100 | sys_place_fork, // 44 |
---|
[407] | 101 | sys_thread_sleep, // 45 |
---|
| 102 | sys_thread_wakeup, // 46 |
---|
[428] | 103 | sys_trace, // 47 |
---|
| 104 | sys_fg, // 48 |
---|
[457] | 105 | sys_is_fg, // 49 |
---|
[610] | 106 | |
---|
| 107 | sys_exit, // 50 |
---|
[626] | 108 | sys_sync, // 51 |
---|
| 109 | sys_fsync, // 52 |
---|
[637] | 110 | sys_get_best_core, // 53 |
---|
| 111 | sys_get_nb_cores, // 54 |
---|
[641] | 112 | sys_get_thread_info, // 55 |
---|
[647] | 113 | sys_fbf, // 56 |
---|
[657] | 114 | sys_socket, // 57 |
---|
[16] | 115 | }; |
---|
| 116 | |
---|
[583] | 117 | //////////////////////////////////////////// |
---|
[527] | 118 | const char * syscall_str( syscalls_t index ) |
---|
[408] | 119 | { |
---|
[583] | 120 | switch (index) |
---|
| 121 | { |
---|
[527] | 122 | case SYS_THREAD_EXIT: return "THREAD_EXIT"; // 0 |
---|
| 123 | case SYS_THREAD_YIELD: return "THREAD_YIELD"; // 1 |
---|
| 124 | case SYS_THREAD_CREATE: return "THREAD_CREATE"; // 2 |
---|
| 125 | case SYS_THREAD_JOIN: return "THREAD_JOIN"; // 3 |
---|
| 126 | case SYS_THREAD_DETACH: return "THREAD_DETACH"; // 4 |
---|
| 127 | case SYS_THREAD_CANCEL: return "THREAD_CANCEL"; // 5 |
---|
| 128 | case SYS_SEM : return "SEM"; // 6 |
---|
| 129 | case SYS_CONDVAR: return "CONDVAR"; // 7 |
---|
| 130 | case SYS_BARRIER: return "BARRIER"; // 8 |
---|
| 131 | case SYS_MUTEX : return "MUTEX"; // 9 |
---|
[408] | 132 | |
---|
[610] | 133 | case SYS_RENAME: return "RENAME"; // 10 |
---|
[527] | 134 | case SYS_MUNMAP: return "MUNMAP"; // 11 |
---|
| 135 | case SYS_OPEN: return "OPEN"; // 12 |
---|
| 136 | case SYS_MMAP: return "MMAP"; // 13 |
---|
| 137 | case SYS_READ: return "READ"; // 14 |
---|
| 138 | case SYS_WRITE: return "WRITE"; // 15 |
---|
| 139 | case SYS_LSEEK: return "LSEEK"; // 16 |
---|
| 140 | case SYS_CLOSE: return "CLOSE"; // 17 |
---|
| 141 | case SYS_UNLINK: return "UNLINK"; // 18 |
---|
| 142 | case SYS_PIPE: return "PIPE"; // 19 |
---|
[408] | 143 | |
---|
[527] | 144 | case SYS_CHDIR: return "CHDIR"; // 20 |
---|
| 145 | case SYS_MKDIR: return "MKDIR"; // 21 |
---|
| 146 | case SYS_MKFIFO: return "MKFIFO"; // 22 |
---|
| 147 | case SYS_OPENDIR: return "OPENDIR"; // 23 |
---|
| 148 | case SYS_READDIR: return "READDIR"; // 24 |
---|
| 149 | case SYS_CLOSEDIR: return "CLOSEDIR"; // 25 |
---|
| 150 | case SYS_GETCWD: return "GETCWD"; // 26 |
---|
| 151 | case SYS_ISATTY: return "ISATTY"; // 27 |
---|
| 152 | case SYS_ALARM : return "ALARM"; // 28 |
---|
| 153 | case SYS_RMDIR : return "RMDIR"; // 29 |
---|
[408] | 154 | |
---|
[527] | 155 | case SYS_UTLS: return "UTLS"; // 30 |
---|
| 156 | case SYS_CHMOD : return "CHMOD"; // 31 |
---|
| 157 | case SYS_SIGNAL: return "SIGNAL"; // 32 |
---|
| 158 | case SYS_TIMEOFDAY: return "TIMEOFDAY"; // 33 |
---|
| 159 | case SYS_KILL: return "KILL"; // 34 |
---|
| 160 | case SYS_GETPID: return "GETPID"; // 35 |
---|
| 161 | case SYS_FORK: return "FORK"; // 36 |
---|
| 162 | case SYS_EXEC: return "EXEC"; // 37 |
---|
| 163 | case SYS_STAT: return "STAT"; // 38 |
---|
| 164 | case SYS_WAIT: return "WAIT"; // 39 |
---|
[408] | 165 | |
---|
[527] | 166 | case SYS_GET_CONFIG: return "GET_CONFIG"; // 40 |
---|
[637] | 167 | case SYS_GET_CORE_ID: return "GET_CORE_ID"; // 41 |
---|
[527] | 168 | case SYS_GET_CYCLE: return "GET_CYCLE"; // 42 |
---|
| 169 | case SYS_DISPLAY: return "DISPLAY"; // 43 |
---|
[583] | 170 | case SYS_PLACE_FORK: return "PLACE_FORK"; // 44 |
---|
[527] | 171 | case SYS_THREAD_SLEEP: return "THREAD_SLEEP"; // 45 |
---|
| 172 | case SYS_THREAD_WAKEUP: return "THREAD_WAKEUP"; // 46 |
---|
| 173 | case SYS_TRACE: return "TRACE"; // 47 |
---|
| 174 | case SYS_FG: return "FG"; // 48 |
---|
| 175 | case SYS_IS_FG: return "IS_FG"; // 49 |
---|
[610] | 176 | |
---|
| 177 | case SYS_EXIT: return "EXIT"; // 50 |
---|
[626] | 178 | case SYS_SYNC: return "SYNC"; // 51 |
---|
[637] | 179 | case SYS_FSYNC: return "FSYNC"; // 52 |
---|
| 180 | case SYS_GET_BEST_CORE: return "GET_BEST_CORE"; // 53 |
---|
| 181 | case SYS_GET_NB_CORES: return "GET_NB_CORES"; // 54 |
---|
[641] | 182 | case SYS_GET_THREAD_INFO: return "GET_THREAD_INFO"; // 55 |
---|
[647] | 183 | case SYS_FBF: return "FBF"; // 56 |
---|
[657] | 184 | case SYS_SOCKET: return "SOCKET"; // 57 |
---|
[610] | 185 | |
---|
[527] | 186 | default: return "undefined"; |
---|
[583] | 187 | } |
---|
[408] | 188 | } |
---|
| 189 | |
---|
| 190 | |
---|
[16] | 191 | ////////////////////////////////// |
---|
| 192 | reg_t do_syscall( thread_t * this, |
---|
| 193 | reg_t arg0, |
---|
| 194 | reg_t arg1, |
---|
| 195 | reg_t arg2, |
---|
| 196 | reg_t arg3, |
---|
| 197 | reg_t service_num ) |
---|
| 198 | { |
---|
[408] | 199 | int error = 0; |
---|
[16] | 200 | |
---|
[669] | 201 | assert( __FUNCTION__, (this == CURRENT_THREAD), |
---|
[438] | 202 | "wrong <this> argument\n" ); |
---|
| 203 | |
---|
[16] | 204 | // update user time |
---|
[473] | 205 | thread_time_update( this , 1 ); |
---|
[16] | 206 | |
---|
| 207 | // check syscall index |
---|
| 208 | if( service_num >= SYSCALLS_NR ) |
---|
| 209 | { |
---|
| 210 | printk("\n[ERROR] in %s : Undefined syscall %d, for thread %x\n", |
---|
| 211 | __FUNCTION__ , service_num , this ); |
---|
| 212 | |
---|
| 213 | this->errno = ENOSYS; |
---|
| 214 | hal_disable_irq(NULL); |
---|
| 215 | return ENOSYS;; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | // reset errno |
---|
| 219 | this->errno = 0; |
---|
| 220 | |
---|
| 221 | // call relevant kernel function |
---|
| 222 | error = syscall_tbl[service_num] ( arg0 , arg1 , arg2 , arg3 ); |
---|
| 223 | |
---|
[409] | 224 | // check kernel stack overflow |
---|
[669] | 225 | assert( __FUNCTION__, (CURRENT_THREAD->signature == THREAD_SIGNATURE), |
---|
[438] | 226 | "kernel stack overflow after for thread %x in cluster %x\n", CURRENT_THREAD, local_cxy ); |
---|
[409] | 227 | |
---|
[16] | 228 | // update kernel time |
---|
[473] | 229 | thread_time_update( this , 0 ); |
---|
[16] | 230 | |
---|
| 231 | return error; |
---|
| 232 | } |
---|