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

Last change on this file since 21 was 16, checked in by alain, 7 years ago

mprove the HAL for interrupt, exception, syscall handling.

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