1 | /* |
---|
2 | * sys_sem.c - Acces a POSIX unamed semaphore. |
---|
3 | * |
---|
4 | * Authors 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_uspace.h> |
---|
26 | #include <shared_semaphore.h> |
---|
27 | #include <errno.h> |
---|
28 | #include <thread.h> |
---|
29 | #include <printk.h> |
---|
30 | #include <vmm.h> |
---|
31 | #include <remote_sem.h> |
---|
32 | #include <syscalls.h> |
---|
33 | |
---|
34 | #if DEBUG_SYS_SEM |
---|
35 | ////////////////////////////////////////////////// |
---|
36 | static char * sys_sem_op_str( uint32_t operation ) |
---|
37 | { |
---|
38 | if ( operation == SEM_INIT ) return "INIT"; |
---|
39 | else if( operation == SEM_WAIT ) return "WAIT"; |
---|
40 | else if( operation == SEM_POST ) return "POST"; |
---|
41 | else if( operation == SEM_GETVALUE ) return "GETVALUE"; |
---|
42 | else if( operation == SEM_DESTROY ) return "DESTROY"; |
---|
43 | else return "undefined"; |
---|
44 | } |
---|
45 | #endif |
---|
46 | |
---|
47 | ////////////////////////////////// |
---|
48 | int sys_sem( void * vaddr, // semaphore virtual address |
---|
49 | uint32_t operation, // requested operation type |
---|
50 | uint32_t init_value, // initial value |
---|
51 | uint32_t * current_value ) // pointer on current value buffer |
---|
52 | { |
---|
53 | vseg_t * vseg; |
---|
54 | error_t error; |
---|
55 | uint32_t current; // semaphore current value |
---|
56 | xptr_t sem_xp; // extended pointer on semaphore |
---|
57 | |
---|
58 | thread_t * this = CURRENT_THREAD; |
---|
59 | process_t * process = this->process; |
---|
60 | |
---|
61 | #if DEBUG_SYS_SEM |
---|
62 | uint64_t tm_start; |
---|
63 | uint64_t tm_end; |
---|
64 | tm_start = hal_get_cycles(); |
---|
65 | if( DEBUG_SYS_SEM < tm_start ) |
---|
66 | printk("\n[DBG] %s : thread %x in process %x enter for %s / cycle %d\n", |
---|
67 | __FUNCTION__, this->trdid, process->pid, sys_sem_op_str( operation ), (uint32_t)tm_start ); |
---|
68 | #endif |
---|
69 | |
---|
70 | // check vaddr in user vspace |
---|
71 | error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg ); |
---|
72 | if( error ) |
---|
73 | { |
---|
74 | |
---|
75 | #if DEBUG_SYSCALLS_ERROR |
---|
76 | printk("\n[ERROR] in %s : unmapped semaphore pointer %x / thread %x in process %x / cycle %d\n", |
---|
77 | __FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid, (uint32_t)hal_get_cycles() ); |
---|
78 | vmm_display( process , false ); |
---|
79 | #endif |
---|
80 | this->errno = EINVAL; |
---|
81 | return -1; |
---|
82 | } |
---|
83 | |
---|
84 | // execute requested operation |
---|
85 | switch( operation ) |
---|
86 | { |
---|
87 | ////////////// |
---|
88 | case SEM_INIT: |
---|
89 | { |
---|
90 | // call relevant kernel function to initialize semaphore |
---|
91 | error = remote_sem_create( (intptr_t)vaddr , |
---|
92 | init_value, |
---|
93 | XPTR( local_cxy , &sem_xp ) ); |
---|
94 | |
---|
95 | if ( error ) |
---|
96 | { |
---|
97 | |
---|
98 | #if DEBUG_SYSCALLS_ERROR |
---|
99 | printk("\n[ERROR] in %s INIT: cannot create semaphore / thread %x in process %x / cycle %d\n", |
---|
100 | __FUNCTION__, this->trdid, process->pid, (uint32_t)hal_get_cycles() ); |
---|
101 | #endif |
---|
102 | this->errno = ENOMEM; |
---|
103 | return -1; |
---|
104 | } |
---|
105 | |
---|
106 | break; |
---|
107 | } |
---|
108 | ////////////////// |
---|
109 | case SEM_GETVALUE: |
---|
110 | { |
---|
111 | // check current_value buffer in user vspace |
---|
112 | error = vmm_get_vseg( process , (intptr_t)current_value , &vseg ); |
---|
113 | if( error ) |
---|
114 | { |
---|
115 | |
---|
116 | #if DEBUG_SYSCALLS_ERROR |
---|
117 | printk("\n[ERROR] in %s GETVALUE: unmapped target buffer %x / thread %x in process %x / cycle %d\n", |
---|
118 | __FUNCTION__ , (intptr_t)current_value, this->trdid, process->pid, (uint32_t)hal_get_cycles() ); |
---|
119 | vmm_display( process , false ); |
---|
120 | #endif |
---|
121 | this->errno = EINVAL; |
---|
122 | return -1; |
---|
123 | } |
---|
124 | |
---|
125 | // get extended pointer on remote semaphore |
---|
126 | sem_xp = remote_sem_from_vaddr( (intptr_t)vaddr ); |
---|
127 | |
---|
128 | // check semaphore registered |
---|
129 | if( sem_xp == XPTR_NULL ) |
---|
130 | { |
---|
131 | |
---|
132 | #if DEBUG_SYSCALLS_ERROR |
---|
133 | printk("\n[ERROR] in %s GETVALUE: semaphore %x not found / thread %x in process %x / cycle %d\n", |
---|
134 | __FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid, (uint32_t)hal_get_cycles() ); |
---|
135 | #endif |
---|
136 | this->errno = EINVAL; |
---|
137 | return -1; |
---|
138 | } |
---|
139 | |
---|
140 | // call relevant kernel function to get semaphore current value |
---|
141 | remote_sem_get_value( sem_xp , ¤t ); |
---|
142 | |
---|
143 | // return value to user |
---|
144 | hal_copy_to_uspace( current_value , ¤t , sizeof(uint32_t) ); |
---|
145 | |
---|
146 | break; |
---|
147 | } |
---|
148 | ////////////// |
---|
149 | case SEM_WAIT: |
---|
150 | { |
---|
151 | // get extended pointer on remote semaphore |
---|
152 | sem_xp = remote_sem_from_vaddr( (intptr_t)vaddr ); |
---|
153 | |
---|
154 | // check semaphore registered |
---|
155 | if( sem_xp == XPTR_NULL ) |
---|
156 | { |
---|
157 | |
---|
158 | #if DEBUG_SYSCALLS_ERROR |
---|
159 | printk("\n[ERROR] in %s WAIT: semaphore %x not found / thread %x in process %x / cycle %d\n", |
---|
160 | __FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid, (uint32_t)hal_get_cycles() ); |
---|
161 | vmm_display( process , true ); |
---|
162 | #endif |
---|
163 | this->errno = EINVAL; |
---|
164 | return -1; |
---|
165 | } |
---|
166 | |
---|
167 | // call relevant kernel function to wait semaphore available |
---|
168 | remote_sem_wait( sem_xp ); |
---|
169 | |
---|
170 | break; |
---|
171 | } |
---|
172 | ////////////// |
---|
173 | case SEM_POST: |
---|
174 | { |
---|
175 | // get extended pointer on remote semaphore |
---|
176 | sem_xp = remote_sem_from_vaddr( (intptr_t)vaddr ); |
---|
177 | |
---|
178 | // check semaphore registered |
---|
179 | if( sem_xp == XPTR_NULL ) |
---|
180 | { |
---|
181 | |
---|
182 | #if DEBUG_SYSCALLS_ERROR |
---|
183 | printk("\n[ERROR] in %s POST: semaphore %x not found / thread %x in process %x / cycle %d\n", |
---|
184 | __FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid, (uint32_t)hal_get_cycles() ); |
---|
185 | #endif |
---|
186 | this->errno = EINVAL; |
---|
187 | return -1; |
---|
188 | } |
---|
189 | |
---|
190 | // call relevant kernel function to release semaphore |
---|
191 | remote_sem_post( sem_xp ); |
---|
192 | |
---|
193 | break; |
---|
194 | } |
---|
195 | ///////////////// |
---|
196 | case SEM_DESTROY: |
---|
197 | { |
---|
198 | // get extended pointer on remote semaphore |
---|
199 | sem_xp = remote_sem_from_vaddr( (intptr_t)vaddr ); |
---|
200 | |
---|
201 | // check semaphore registered |
---|
202 | if( sem_xp == XPTR_NULL ) |
---|
203 | { |
---|
204 | |
---|
205 | #if DEBUG_SYSCALLS_ERROR |
---|
206 | printk("\n[ERROR] in %s DESTROY: semaphore %x not found / thread %x in process %x / cycle %d\n", |
---|
207 | __FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid, (uint32_t)hal_get_cycles() ); |
---|
208 | #endif |
---|
209 | this->errno = EINVAL; |
---|
210 | return -1; |
---|
211 | } |
---|
212 | |
---|
213 | // destroy semaphore |
---|
214 | remote_sem_destroy( sem_xp ); |
---|
215 | |
---|
216 | break; |
---|
217 | } |
---|
218 | /////// |
---|
219 | default: // undefined operation |
---|
220 | { |
---|
221 | |
---|
222 | #if DEBUG_SYSCALLS_ERROR |
---|
223 | printk("\n[ERROR] in %s : undefined operation type %d / thread %x in process %x / cycle %d\n", |
---|
224 | __FUNCTION__ , operation, this->trdid, process->pid, (uint32_t)hal_get_cycles() ); |
---|
225 | #endif |
---|
226 | this->errno = EINVAL; |
---|
227 | return -1; |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | hal_fence(); |
---|
232 | |
---|
233 | #if DEBUG_SYS_SEM |
---|
234 | tm_end = hal_get_cycles(); |
---|
235 | if( DEBUG_SYS_SEM < tm_end ) |
---|
236 | { |
---|
237 | cxy_t sem_cxy = GET_CXY( sem_xp ); |
---|
238 | remote_sem_t * sem_ptr = GET_PTR( sem_xp ); |
---|
239 | uint32_t value = hal_remote_lw( XPTR( sem_cxy , &sem_ptr->count ) ); |
---|
240 | printk("\n[DBG] %s : thread %x in process %x exit for %s / value %d / cost = %d / cycle %d\n", |
---|
241 | __FUNCTION__, this->trdid, process->pid, sys_sem_op_str( operation ), value, |
---|
242 | (uint32_t)(tm_end - tm_start), (uint32_t)tm_end ); |
---|
243 | } |
---|
244 | #endif |
---|
245 | |
---|
246 | return 0; |
---|
247 | |
---|
248 | } // end sys_sem() |
---|