source: trunk/kernel/kern/do_syscall.c

Last change on this file was 683, checked in by alain, 3 years ago

All modifications required to support the <tcp_chat> application
including error recovery in case of packet loss.A

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