[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 | { |
---|
[375] | 38 | panic("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 |
---|
| 93 | sys_trace, // 39 |
---|
[407] | 94 | |
---|
| 95 | sys_get_config, // 40 |
---|
| 96 | sys_get_core, // 41 |
---|
| 97 | sys_get_cycle, // 42 |
---|
| 98 | sys_get_sched, // 43 |
---|
| 99 | sys_panic, // 44 |
---|
| 100 | sys_thread_sleep, // 45 |
---|
| 101 | sys_thread_wakeup, // 46 |
---|
[16] | 102 | }; |
---|
| 103 | |
---|
[408] | 104 | //////////////////////////////////// |
---|
| 105 | char * syscall_str( uint32_t index ) |
---|
| 106 | { |
---|
| 107 | if ( index == SYS_THREAD_EXIT ) return "THREAD_EXIT"; // 0 |
---|
| 108 | else if( index == SYS_THREAD_YIELD ) return "THREAD_YIELD"; // 1 |
---|
| 109 | else if( index == SYS_THREAD_CREATE ) return "THREAD_CREATE"; // 2 |
---|
| 110 | else if( index == SYS_THREAD_JOIN ) return "THREAD_JOIN"; // 3 |
---|
| 111 | else if( index == SYS_THREAD_DETACH ) return "THREAD_DETACH"; // 4 |
---|
[409] | 112 | else if( index == SYS_THREAD_CANCEL ) return "THREAD_CANCEL"; // 5 |
---|
[408] | 113 | else if( index == SYS_SEM ) return "SEM"; // 6 |
---|
| 114 | else if( index == SYS_CONDVAR ) return "CONDVAR"; // 7 |
---|
| 115 | else if( index == SYS_BARRIER ) return "BARRIER"; // 8 |
---|
| 116 | else if( index == SYS_MUTEX ) return "MUTEX"; // 9 |
---|
| 117 | |
---|
| 118 | else if( index == SYS_EXIT ) return "EXIT"; // 10 |
---|
| 119 | else if( index == SYS_MUNMAP ) return "MUNMAP"; // 11 |
---|
| 120 | else if( index == SYS_OPEN ) return "OPEN"; // 12 |
---|
| 121 | else if( index == SYS_MMAP ) return "MMAP"; // 13 |
---|
| 122 | else if( index == SYS_READ ) return "READ"; // 14 |
---|
| 123 | else if( index == SYS_WRITE ) return "WRITE"; // 15 |
---|
| 124 | else if( index == SYS_LSEEK ) return "LSEEK"; // 16 |
---|
| 125 | else if( index == SYS_CLOSE ) return "CLOSE"; // 17 |
---|
| 126 | else if( index == SYS_UNLINK ) return "UNLINK"; // 18 |
---|
| 127 | else if( index == SYS_PIPE ) return "PIPE"; // 19 |
---|
| 128 | |
---|
| 129 | else if( index == SYS_CHDIR ) return "CHDIR"; // 20 |
---|
| 130 | else if( index == SYS_MKDIR ) return "MKDIR"; // 21 |
---|
| 131 | else if( index == SYS_MKFIFO ) return "MKFIFO"; // 22 |
---|
| 132 | else if( index == SYS_OPENDIR ) return "OPENDIR"; // 23 |
---|
| 133 | else if( index == SYS_READDIR ) return "READDIR"; // 24 |
---|
| 134 | else if( index == SYS_CLOSEDIR ) return "CLOSEDIR"; // 25 |
---|
| 135 | else if( index == SYS_GETCWD ) return "GETCWD"; // 26 |
---|
| 136 | else if( index == SYS_ALARM ) return "ALARM"; // 28 |
---|
| 137 | else if( index == SYS_RMDIR ) return "RMDIR"; // 29 |
---|
| 138 | |
---|
| 139 | else if( index == SYS_UTLS ) return "UTLS"; // 30 |
---|
| 140 | else if( index == SYS_CHMOD ) return "CHMOD"; // 31 |
---|
| 141 | else if( index == SYS_SIGNAL ) return "SIGNAL"; // 32 |
---|
| 142 | else if( index == SYS_TIMEOFDAY ) return "TIMEOFDAY"; // 33 |
---|
| 143 | else if( index == SYS_KILL ) return "KILL"; // 34 |
---|
| 144 | else if( index == SYS_GETPID ) return "GETPID"; // 35 |
---|
| 145 | else if( index == SYS_FORK ) return "FORK"; // 36 |
---|
| 146 | else if( index == SYS_EXEC ) return "EXEC"; // 37 |
---|
| 147 | else if( index == SYS_STAT ) return "STAT"; // 38 |
---|
| 148 | else if( index == SYS_TRACE ) return "TRACE"; // 39 |
---|
| 149 | |
---|
| 150 | else if( index == SYS_GET_CONFIG ) return "GET_CONFIG"; // 40 |
---|
| 151 | else if( index == SYS_GET_CORE ) return "GET_CORE"; // 41 |
---|
| 152 | else if( index == SYS_GET_CYCLE ) return "GET_CYCLE"; // 42 |
---|
| 153 | else if( index == SYS_GET_SCHED ) return "GET_SCHED"; // 43 |
---|
| 154 | else if( index == SYS_PANIC ) return "PANIC"; // 44 |
---|
| 155 | else if( index == SYS_SLEEP ) return "SLEEP"; // 45 |
---|
| 156 | else if( index == SYS_WAKEUP ) return "WAKEUP"; // 46 |
---|
| 157 | |
---|
| 158 | else return "undefined"; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | |
---|
[16] | 162 | ////////////////////////////////// |
---|
| 163 | reg_t do_syscall( thread_t * this, |
---|
| 164 | reg_t arg0, |
---|
| 165 | reg_t arg1, |
---|
| 166 | reg_t arg2, |
---|
| 167 | reg_t arg3, |
---|
| 168 | reg_t service_num ) |
---|
| 169 | { |
---|
[408] | 170 | int error = 0; |
---|
[16] | 171 | |
---|
| 172 | // update user time |
---|
| 173 | thread_user_time_update( this ); |
---|
| 174 | |
---|
| 175 | // check syscall index |
---|
| 176 | if( service_num >= SYSCALLS_NR ) |
---|
| 177 | { |
---|
| 178 | printk("\n[ERROR] in %s : Undefined syscall %d, for thread %x\n", |
---|
| 179 | __FUNCTION__ , service_num , this ); |
---|
| 180 | |
---|
| 181 | this->errno = ENOSYS; |
---|
| 182 | hal_disable_irq(NULL); |
---|
| 183 | return ENOSYS;; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | // reset errno |
---|
| 187 | this->errno = 0; |
---|
| 188 | |
---|
| 189 | // call relevant kernel function |
---|
| 190 | error = syscall_tbl[service_num] ( arg0 , arg1 , arg2 , arg3 ); |
---|
| 191 | |
---|
[409] | 192 | // check kernel stack overflow |
---|
| 193 | assert( (this->signature == THREAD_SIGNATURE), __FUNCTION__, "kernel stack overflow\n" ); |
---|
| 194 | |
---|
[16] | 195 | // update kernel time |
---|
| 196 | thread_kernel_time_update( this ); |
---|
| 197 | |
---|
| 198 | return error; |
---|
| 199 | } |
---|