source: trunk/kernel/kern/do_syscall.c @ 407

Last change on this file since 407 was 407, checked in by alain, 6 years ago

First implementation of fork/exec.

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