[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 | |
---|
[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> |
---|
[16] | 32 | |
---|
[408] | 33 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[407] | 34 | // This ƒonction should never be called... |
---|
[408] | 35 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[407] | 36 | static int sys_undefined() |
---|
[23] | 37 | { |
---|
[492] | 38 | assert( false , "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 |
---|
[437] | 80 | sys_isatty, // 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 |
---|
[457] | 104 | sys_is_fg, // 49 |
---|
[16] | 105 | }; |
---|
| 106 | |
---|
[408] | 107 | //////////////////////////////////// |
---|
[488] | 108 | const char * syscall_str( uint32_t index ) |
---|
[408] | 109 | { |
---|
| 110 | if ( index == SYS_THREAD_EXIT ) return "THREAD_EXIT"; // 0 |
---|
| 111 | else if( index == SYS_THREAD_YIELD ) return "THREAD_YIELD"; // 1 |
---|
| 112 | else if( index == SYS_THREAD_CREATE ) return "THREAD_CREATE"; // 2 |
---|
| 113 | else if( index == SYS_THREAD_JOIN ) return "THREAD_JOIN"; // 3 |
---|
| 114 | else if( index == SYS_THREAD_DETACH ) return "THREAD_DETACH"; // 4 |
---|
[409] | 115 | else if( index == SYS_THREAD_CANCEL ) return "THREAD_CANCEL"; // 5 |
---|
[408] | 116 | else if( index == SYS_SEM ) return "SEM"; // 6 |
---|
| 117 | else if( index == SYS_CONDVAR ) return "CONDVAR"; // 7 |
---|
| 118 | else if( index == SYS_BARRIER ) return "BARRIER"; // 8 |
---|
| 119 | else if( index == SYS_MUTEX ) return "MUTEX"; // 9 |
---|
| 120 | |
---|
| 121 | else if( index == SYS_EXIT ) return "EXIT"; // 10 |
---|
| 122 | else if( index == SYS_MUNMAP ) return "MUNMAP"; // 11 |
---|
| 123 | else if( index == SYS_OPEN ) return "OPEN"; // 12 |
---|
| 124 | else if( index == SYS_MMAP ) return "MMAP"; // 13 |
---|
| 125 | else if( index == SYS_READ ) return "READ"; // 14 |
---|
| 126 | else if( index == SYS_WRITE ) return "WRITE"; // 15 |
---|
| 127 | else if( index == SYS_LSEEK ) return "LSEEK"; // 16 |
---|
| 128 | else if( index == SYS_CLOSE ) return "CLOSE"; // 17 |
---|
| 129 | else if( index == SYS_UNLINK ) return "UNLINK"; // 18 |
---|
| 130 | else if( index == SYS_PIPE ) return "PIPE"; // 19 |
---|
| 131 | |
---|
| 132 | else if( index == SYS_CHDIR ) return "CHDIR"; // 20 |
---|
| 133 | else if( index == SYS_MKDIR ) return "MKDIR"; // 21 |
---|
| 134 | else if( index == SYS_MKFIFO ) return "MKFIFO"; // 22 |
---|
| 135 | else if( index == SYS_OPENDIR ) return "OPENDIR"; // 23 |
---|
| 136 | else if( index == SYS_READDIR ) return "READDIR"; // 24 |
---|
| 137 | else if( index == SYS_CLOSEDIR ) return "CLOSEDIR"; // 25 |
---|
| 138 | else if( index == SYS_GETCWD ) return "GETCWD"; // 26 |
---|
[443] | 139 | else if( index == SYS_ISATTY ) return "ISATTY"; // 27 |
---|
[408] | 140 | else if( index == SYS_ALARM ) return "ALARM"; // 28 |
---|
| 141 | else if( index == SYS_RMDIR ) return "RMDIR"; // 29 |
---|
| 142 | |
---|
| 143 | else if( index == SYS_UTLS ) return "UTLS"; // 30 |
---|
| 144 | else if( index == SYS_CHMOD ) return "CHMOD"; // 31 |
---|
| 145 | else if( index == SYS_SIGNAL ) return "SIGNAL"; // 32 |
---|
| 146 | else if( index == SYS_TIMEOFDAY ) return "TIMEOFDAY"; // 33 |
---|
| 147 | else if( index == SYS_KILL ) return "KILL"; // 34 |
---|
| 148 | else if( index == SYS_GETPID ) return "GETPID"; // 35 |
---|
| 149 | else if( index == SYS_FORK ) return "FORK"; // 36 |
---|
| 150 | else if( index == SYS_EXEC ) return "EXEC"; // 37 |
---|
| 151 | else if( index == SYS_STAT ) return "STAT"; // 38 |
---|
[428] | 152 | else if( index == SYS_WAIT ) return "WAIT"; // 39 |
---|
[408] | 153 | |
---|
| 154 | else if( index == SYS_GET_CONFIG ) return "GET_CONFIG"; // 40 |
---|
| 155 | else if( index == SYS_GET_CORE ) return "GET_CORE"; // 41 |
---|
| 156 | else if( index == SYS_GET_CYCLE ) return "GET_CYCLE"; // 42 |
---|
[428] | 157 | else if( index == SYS_DISPLAY ) return "DISPLAY"; // 43 |
---|
| 158 | else if( index == SYS_THREAD_SLEEP ) return "THREAD_SLEEP"; // 45 |
---|
| 159 | else if( index == SYS_THREAD_WAKEUP ) return "THREAD_WAKEUP"; // 46 |
---|
| 160 | else if( index == SYS_TRACE ) return "TRACE"; // 47 |
---|
| 161 | else if( index == SYS_FG ) return "FG"; // 48 |
---|
[457] | 162 | else if( index == SYS_IS_FG ) return "IS_FG"; // 49 |
---|
[408] | 163 | |
---|
| 164 | else return "undefined"; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | |
---|
[16] | 168 | ////////////////////////////////// |
---|
| 169 | reg_t do_syscall( thread_t * this, |
---|
| 170 | reg_t arg0, |
---|
| 171 | reg_t arg1, |
---|
| 172 | reg_t arg2, |
---|
| 173 | reg_t arg3, |
---|
| 174 | reg_t service_num ) |
---|
| 175 | { |
---|
[408] | 176 | int error = 0; |
---|
[16] | 177 | |
---|
[492] | 178 | assert( (this == CURRENT_THREAD), |
---|
[438] | 179 | "wrong <this> argument\n" ); |
---|
| 180 | |
---|
[16] | 181 | // update user time |
---|
[473] | 182 | thread_time_update( this , 1 ); |
---|
[16] | 183 | |
---|
| 184 | // check syscall index |
---|
| 185 | if( service_num >= SYSCALLS_NR ) |
---|
| 186 | { |
---|
| 187 | printk("\n[ERROR] in %s : Undefined syscall %d, for thread %x\n", |
---|
| 188 | __FUNCTION__ , service_num , this ); |
---|
| 189 | |
---|
| 190 | this->errno = ENOSYS; |
---|
| 191 | hal_disable_irq(NULL); |
---|
| 192 | return ENOSYS;; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | // reset errno |
---|
| 196 | this->errno = 0; |
---|
| 197 | |
---|
| 198 | // call relevant kernel function |
---|
| 199 | error = syscall_tbl[service_num] ( arg0 , arg1 , arg2 , arg3 ); |
---|
| 200 | |
---|
[409] | 201 | // check kernel stack overflow |
---|
[492] | 202 | assert( (CURRENT_THREAD->signature == THREAD_SIGNATURE), |
---|
[438] | 203 | "kernel stack overflow after for thread %x in cluster %x\n", CURRENT_THREAD, local_cxy ); |
---|
[409] | 204 | |
---|
[16] | 205 | // update kernel time |
---|
[473] | 206 | thread_time_update( this , 0 ); |
---|
[16] | 207 | |
---|
| 208 | return error; |
---|
| 209 | } |
---|