[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> |
---|
| 31 | |
---|
| 32 | ///////////////////////////////////////////////////////////////////////////////////////////// |
---|
[23] | 33 | ///////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 34 | static inline int sys_undefined() |
---|
| 35 | { |
---|
[375] | 36 | panic("undefined system call"); |
---|
[23] | 37 | return 0; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | ///////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 41 | // This array of pointers define the kernel functions implementing the syscalls. |
---|
[16] | 42 | // It must be kept consistent with the enum in syscalls.h |
---|
| 43 | ///////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 44 | |
---|
| 45 | typedef int (*sys_func_t) (); |
---|
| 46 | |
---|
| 47 | static const sys_func_t syscall_tbl[SYSCALLS_NR] = |
---|
| 48 | { |
---|
[23] | 49 | sys_thread_exit, // 0 |
---|
| 50 | sys_mmap, // 1 |
---|
| 51 | sys_thread_create, // 2 |
---|
| 52 | sys_thread_join, // 3 |
---|
| 53 | sys_thread_detach, // 4 |
---|
| 54 | sys_thread_yield, // 5 |
---|
| 55 | sys_sem, // 6 |
---|
| 56 | sys_condvar, // 7 |
---|
| 57 | sys_barrier, // 8 |
---|
| 58 | sys_mutex, // 9 |
---|
| 59 | sys_thread_sleep, // 10 |
---|
| 60 | sys_thread_wakeup, // 11 |
---|
| 61 | sys_open, // 12 |
---|
| 62 | sys_creat, // 13 |
---|
| 63 | sys_read, // 14 |
---|
| 64 | sys_write, // 15 |
---|
| 65 | sys_lseek, // 16 |
---|
| 66 | sys_close, // 17 |
---|
| 67 | sys_unlink, // 18 |
---|
| 68 | sys_pipe, // 19 |
---|
| 69 | sys_chdir, // 20 |
---|
| 70 | sys_mkdir, // 21 |
---|
| 71 | sys_mkfifo, // 22 |
---|
| 72 | sys_opendir, // 23 |
---|
| 73 | sys_readdir, // 24 |
---|
| 74 | sys_closedir, // 25 |
---|
| 75 | sys_getcwd, // 26 |
---|
| 76 | sys_clock, // 27 |
---|
| 77 | sys_alarm, // 28 |
---|
| 78 | sys_rmdir, // 29 |
---|
| 79 | sys_utls, // 30 |
---|
| 80 | sys_chmod, // 31 |
---|
| 81 | sys_signal, // 32 |
---|
[50] | 82 | sys_timeofday, // 33 |
---|
[23] | 83 | sys_kill, // 34 |
---|
| 84 | sys_getpid, // 35 |
---|
| 85 | sys_fork, // 36 |
---|
| 86 | sys_exec, // 37 |
---|
| 87 | sys_stat, // 38 |
---|
| 88 | sys_trace, // 39 |
---|
[16] | 89 | }; |
---|
| 90 | |
---|
| 91 | ////////////////////////////////// |
---|
| 92 | reg_t do_syscall( thread_t * this, |
---|
| 93 | reg_t arg0, |
---|
| 94 | reg_t arg1, |
---|
| 95 | reg_t arg2, |
---|
| 96 | reg_t arg3, |
---|
| 97 | reg_t service_num ) |
---|
| 98 | { |
---|
| 99 | int error = 0; |
---|
| 100 | |
---|
| 101 | // update user time |
---|
| 102 | thread_user_time_update( this ); |
---|
| 103 | |
---|
| 104 | // enable IRQs |
---|
| 105 | hal_enable_irq( NULL ); |
---|
| 106 | |
---|
| 107 | // check syscall index |
---|
| 108 | if( service_num >= SYSCALLS_NR ) |
---|
| 109 | { |
---|
| 110 | printk("\n[ERROR] in %s : Undefined syscall %d, for thread %x\n", |
---|
| 111 | __FUNCTION__ , service_num , this ); |
---|
| 112 | |
---|
| 113 | this->errno = ENOSYS; |
---|
| 114 | hal_disable_irq(NULL); |
---|
| 115 | return ENOSYS;; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | syscall_dmsg("\n[INFO] %s : pid = %x / trdid = %x / service #%d\n" |
---|
| 119 | " arg0 = %x / arg1 = %x / arg2 = %x / arg3 = %x\n", |
---|
| 120 | __FUNCTION__ , this->process->pid , this->trdid , service_num , |
---|
| 121 | arg0 , arg1 , arg2 , arg3 ); |
---|
| 122 | |
---|
| 123 | // reset errno |
---|
| 124 | this->errno = 0; |
---|
| 125 | |
---|
| 126 | // call relevant kernel function |
---|
| 127 | error = syscall_tbl[service_num] ( arg0 , arg1 , arg2 , arg3 ); |
---|
| 128 | |
---|
| 129 | // handle pending signals for the calling thread |
---|
| 130 | thread_signals_handle( this ); |
---|
| 131 | |
---|
| 132 | // disable IRQs |
---|
| 133 | hal_disable_irq( NULL ); |
---|
| 134 | |
---|
| 135 | // update kernel time |
---|
| 136 | thread_kernel_time_update( this ); |
---|
| 137 | |
---|
| 138 | return error; |
---|
| 139 | } |
---|