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