1 | /* |
---|
2 | * sys_mutex.c - Access a POSIX mutex. |
---|
3 | * |
---|
4 | * Author Alain Greiner (2016,2017,2018,2019) |
---|
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_special.h> |
---|
26 | #include <hal_vmm.h> |
---|
27 | #include <errno.h> |
---|
28 | #include <thread.h> |
---|
29 | #include <printk.h> |
---|
30 | #include <vmm.h> |
---|
31 | #include <syscalls.h> |
---|
32 | #include <remote_mutex.h> |
---|
33 | |
---|
34 | |
---|
35 | #if DEBUG_SYS_MUTEX |
---|
36 | //////////////////////////////////////////////////// |
---|
37 | static char * sys_mutex_op_str( uint32_t operation ) |
---|
38 | { |
---|
39 | if ( operation == MUTEX_INIT ) return "INIT"; |
---|
40 | else if( operation == MUTEX_LOCK ) return "LOCK"; |
---|
41 | else if( operation == MUTEX_UNLOCK ) return "UNLOCK"; |
---|
42 | else if( operation == MUTEX_TRYLOCK ) return "TRYLOCK"; |
---|
43 | else if( operation == MUTEX_DESTROY ) return "DESTROY"; |
---|
44 | else return "undefined"; |
---|
45 | } |
---|
46 | #endif |
---|
47 | |
---|
48 | ///////////////////////////////// |
---|
49 | int sys_mutex( void * vaddr, |
---|
50 | uint32_t operation, |
---|
51 | uint32_t attr ) |
---|
52 | { |
---|
53 | error_t error; |
---|
54 | vseg_t * vseg; // for vaddr check |
---|
55 | |
---|
56 | thread_t * this = CURRENT_THREAD; |
---|
57 | process_t * process = this->process; |
---|
58 | |
---|
59 | #if (DEBUG_SYS_MUTEX || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
60 | uint64_t tm_start = hal_get_cycles(); |
---|
61 | #endif |
---|
62 | |
---|
63 | #if DEBUG_SYS_MUTEX |
---|
64 | if( DEBUG_SYS_MUTEX < tm_start ) |
---|
65 | printk("\n[%s] thread[%x,%x] enter for %s / cycle %d\n", |
---|
66 | __FUNCTION__, this->trdid, process->pid, sys_mutex_op_str( operation ), (uint32_t)tm_start ); |
---|
67 | #endif |
---|
68 | |
---|
69 | // check vaddr in user vspace |
---|
70 | error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg ); |
---|
71 | |
---|
72 | if( error ) |
---|
73 | { |
---|
74 | |
---|
75 | #if DEBUG_SYSCALLS_ERROR |
---|
76 | printk("\n[ERROR] in %s : mutex unmapped %x / thread %x / process %x\n", |
---|
77 | __FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid ); |
---|
78 | #endif |
---|
79 | this->errno = error; |
---|
80 | return -1; |
---|
81 | } |
---|
82 | |
---|
83 | // execute requested operation |
---|
84 | switch( operation ) |
---|
85 | { |
---|
86 | //////////////// |
---|
87 | case MUTEX_INIT: |
---|
88 | { |
---|
89 | if( attr != 0 ) |
---|
90 | { |
---|
91 | |
---|
92 | #if DEBUG_SYSCALLS_ERROR |
---|
93 | printk("\n[ERROR] in %s : mutex attribute non supported / thread %x / process %x\n", |
---|
94 | __FUNCTION__ , this->trdid , process->pid ); |
---|
95 | #endif |
---|
96 | this->errno = error; |
---|
97 | return -1; |
---|
98 | } |
---|
99 | |
---|
100 | error = remote_mutex_create( (intptr_t)vaddr ); |
---|
101 | |
---|
102 | if( error ) |
---|
103 | { |
---|
104 | |
---|
105 | #if DEBUG_SYSCALLS_ERROR |
---|
106 | printk("\n[ERROR] in %s : cannot create mutex / thread %x / process %x\n", |
---|
107 | __FUNCTION__ , this->trdid , process->pid ); |
---|
108 | #endif |
---|
109 | this->errno = error; |
---|
110 | return -1; |
---|
111 | } |
---|
112 | break; |
---|
113 | } |
---|
114 | /////////////////// |
---|
115 | case MUTEX_DESTROY: |
---|
116 | { |
---|
117 | xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr ); |
---|
118 | |
---|
119 | if( mutex_xp == XPTR_NULL ) // user error |
---|
120 | { |
---|
121 | |
---|
122 | #if DEBUG_SYSCALLS_ERROR |
---|
123 | printk("\n[ERROR] in %s : mutex %x not registered / thread %x / process %x\n", |
---|
124 | __FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid ); |
---|
125 | #endif |
---|
126 | this->errno = EINVAL; |
---|
127 | return -1; |
---|
128 | } |
---|
129 | else // success |
---|
130 | { |
---|
131 | remote_mutex_destroy( mutex_xp ); |
---|
132 | } |
---|
133 | break; |
---|
134 | } |
---|
135 | //////////////// |
---|
136 | case MUTEX_LOCK: |
---|
137 | { |
---|
138 | xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr ); |
---|
139 | |
---|
140 | if( mutex_xp == XPTR_NULL ) // user error |
---|
141 | { |
---|
142 | |
---|
143 | #if DEBUG_SYSCALLS_ERROR |
---|
144 | printk("\n[ERROR] in %s : mutex %x not registered / thread %x / process %x\n", |
---|
145 | __FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid ); |
---|
146 | #endif |
---|
147 | this->errno = EINVAL; |
---|
148 | return -1; |
---|
149 | } |
---|
150 | else // success |
---|
151 | { |
---|
152 | remote_mutex_lock( mutex_xp ); |
---|
153 | } |
---|
154 | break; |
---|
155 | } |
---|
156 | ////////////////// |
---|
157 | case MUTEX_UNLOCK: |
---|
158 | { |
---|
159 | xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr ); |
---|
160 | |
---|
161 | if( mutex_xp == XPTR_NULL ) // user error |
---|
162 | { |
---|
163 | |
---|
164 | #if DEBUG_SYSCALLS_ERROR |
---|
165 | printk("\n[ERROR] in %s : mutex %x not registered / thread %x / process %x\n", |
---|
166 | __FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid ); |
---|
167 | #endif |
---|
168 | this->errno = EINVAL; |
---|
169 | return -1; |
---|
170 | } |
---|
171 | else // success |
---|
172 | { |
---|
173 | error = remote_mutex_unlock( mutex_xp ); |
---|
174 | |
---|
175 | if( error ) |
---|
176 | { |
---|
177 | |
---|
178 | #if DEBUG_SYSCALLS_ERROR |
---|
179 | printk("\n[ERROR] in %s : mutex %x not owned in UNLOCK / thread %x / process %x\n", |
---|
180 | __FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid ); |
---|
181 | #endif |
---|
182 | this->errno = EINVAL; |
---|
183 | return -1; |
---|
184 | } |
---|
185 | } |
---|
186 | break; |
---|
187 | } |
---|
188 | /////////////////// |
---|
189 | case MUTEX_TRYLOCK: |
---|
190 | { |
---|
191 | xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr ); |
---|
192 | |
---|
193 | if( mutex_xp == XPTR_NULL ) // user error |
---|
194 | { |
---|
195 | |
---|
196 | #if DEBUG_SYSCALLS_ERROR |
---|
197 | printk("\n[ERROR] in %s : mutex %x not registered / thread %x / process %x\n", |
---|
198 | __FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid ); |
---|
199 | #endif |
---|
200 | this->errno = EINVAL; |
---|
201 | return -1; |
---|
202 | } |
---|
203 | else // success |
---|
204 | { |
---|
205 | error = remote_mutex_trylock( mutex_xp ); |
---|
206 | |
---|
207 | if( error ) // non fatal : mutex already taken |
---|
208 | { |
---|
209 | this->errno = EBUSY; |
---|
210 | return -1; |
---|
211 | } |
---|
212 | } |
---|
213 | break; |
---|
214 | } |
---|
215 | //////// |
---|
216 | default: |
---|
217 | { |
---|
218 | assert ( false, "illegal operation type <%x>", operation ); |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | hal_fence(); |
---|
223 | |
---|
224 | #if (DEBUG_SYS_MUTEX || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
225 | uint64_t tm_end = hal_get_cycles(); |
---|
226 | #endif |
---|
227 | |
---|
228 | #if DEBUG_SYS_MUTEX |
---|
229 | if( DEBUG_SYS_MUTEX < tm_end ) |
---|
230 | printk("\n[%s] thread[%x,%x] exit for %s / cycle %d\n", |
---|
231 | __FUNCTION__, this->trdid, process->pid, sys_mutex_op_str( operation ), (uint32_t)tm_end ); |
---|
232 | #endif |
---|
233 | |
---|
234 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
235 | hal_atomic_add( &syscalls_cumul_cost[SYS_MUTEX] , tm_end - tm_start ); |
---|
236 | hal_atomic_add( &syscalls_occurences[SYS_MUTEX] , 1 ); |
---|
237 | #endif |
---|
238 | |
---|
239 | return 0; |
---|
240 | |
---|
241 | } // end sys_mutex() |
---|
242 | |
---|