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