[1] | 1 | /* |
---|
[563] | 2 | * remote_sem.c - POSIX unnamed semaphore implementation. |
---|
[1] | 3 | * |
---|
[683] | 4 | * Author Alain Greiner (2016,2017,2018,2019,2020) |
---|
[1] | 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 | |
---|
[457] | 24 | #include <hal_kernel_types.h> |
---|
[23] | 25 | #include <hal_remote.h> |
---|
[1] | 26 | #include <thread.h> |
---|
| 27 | #include <kmem.h> |
---|
| 28 | #include <printk.h> |
---|
| 29 | #include <process.h> |
---|
| 30 | #include <vmm.h> |
---|
| 31 | #include <remote_sem.h> |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | /////////////////////////////////////////////// |
---|
[563] | 35 | xptr_t remote_sem_from_ident( intptr_t ident ) |
---|
[1] | 36 | { |
---|
| 37 | // get pointer on local process_descriptor |
---|
[23] | 38 | process_t * process = CURRENT_THREAD->process; |
---|
[1] | 39 | |
---|
| 40 | // get extended pointer on reference process |
---|
| 41 | xptr_t ref_xp = process->ref_xp; |
---|
| 42 | |
---|
| 43 | // get cluster and local pointer on reference process |
---|
| 44 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
[469] | 45 | process_t * ref_ptr = GET_PTR( ref_xp ); |
---|
[1] | 46 | |
---|
[563] | 47 | // get extended pointer on semaphores list |
---|
[1] | 48 | xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->sem_root ); |
---|
[563] | 49 | xptr_t lock_xp = XPTR( ref_cxy , &ref_ptr->sync_lock ); |
---|
[1] | 50 | |
---|
[563] | 51 | // get lock protecting synchro lists |
---|
| 52 | remote_queuelock_acquire( lock_xp ); |
---|
| 53 | |
---|
[1] | 54 | // scan reference process semaphores list |
---|
| 55 | xptr_t iter_xp; |
---|
| 56 | xptr_t sem_xp; |
---|
| 57 | cxy_t sem_cxy; |
---|
| 58 | remote_sem_t * sem_ptr; |
---|
[563] | 59 | intptr_t current; |
---|
[1] | 60 | bool_t found = false; |
---|
| 61 | |
---|
| 62 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
| 63 | { |
---|
[23] | 64 | sem_xp = XLIST_ELEMENT( iter_xp , remote_sem_t , list ); |
---|
[1] | 65 | sem_cxy = GET_CXY( sem_xp ); |
---|
[469] | 66 | sem_ptr = GET_PTR( sem_xp ); |
---|
[563] | 67 | current = (intptr_t)hal_remote_lpt( XPTR( sem_cxy , &sem_ptr->ident ) ); |
---|
[469] | 68 | |
---|
[563] | 69 | if( current == ident ) |
---|
[1] | 70 | { |
---|
| 71 | found = true; |
---|
| 72 | break; |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
[563] | 76 | // relese lock protecting synchros lists |
---|
| 77 | remote_queuelock_release( lock_xp ); |
---|
| 78 | |
---|
[1] | 79 | if( found == false ) return XPTR_NULL; |
---|
| 80 | else return sem_xp; |
---|
| 81 | |
---|
[563] | 82 | } // end remote_sem_from_ident() |
---|
[1] | 83 | |
---|
[23] | 84 | /////////////////////////////////////////// |
---|
[457] | 85 | error_t remote_sem_create( intptr_t vaddr, |
---|
[563] | 86 | uint32_t value ) |
---|
[1] | 87 | { |
---|
[457] | 88 | remote_sem_t * sem_ptr; |
---|
[1] | 89 | |
---|
| 90 | // get pointer on local process descriptor |
---|
[23] | 91 | process_t * process = CURRENT_THREAD->process; |
---|
[1] | 92 | |
---|
| 93 | // get extended pointer on reference process |
---|
| 94 | xptr_t ref_xp = process->ref_xp; |
---|
| 95 | |
---|
[23] | 96 | // get reference process cluster and local pointer |
---|
[1] | 97 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
| 98 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 99 | |
---|
| 100 | // allocate memory for new semaphore in reference cluster |
---|
[683] | 101 | sem_ptr = kmem_remote_alloc( ref_cxy , bits_log2(sizeof(remote_sem_t)) , AF_ZERO ); |
---|
[635] | 102 | |
---|
| 103 | if( sem_ptr == NULL ) |
---|
[1] | 104 | { |
---|
[635] | 105 | printk("\n[ERROR] in %s : cannot create semaphore\n", __FUNCTION__ ); |
---|
| 106 | return -1; |
---|
[1] | 107 | } |
---|
| 108 | |
---|
[23] | 109 | // initialise semaphore |
---|
[563] | 110 | hal_remote_s32 ( XPTR( ref_cxy , &sem_ptr->count ) , value ); |
---|
[1] | 111 | hal_remote_spt( XPTR( ref_cxy , &sem_ptr->ident ) , (void *)vaddr ); |
---|
[23] | 112 | xlist_root_init( XPTR( ref_cxy , &sem_ptr->root ) ); |
---|
| 113 | xlist_entry_init( XPTR( ref_cxy , &sem_ptr->list ) ); |
---|
[563] | 114 | remote_busylock_init( XPTR( ref_cxy , &sem_ptr->lock ), LOCK_SEM_STATE ); |
---|
[1] | 115 | |
---|
| 116 | xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->sem_root ); |
---|
[563] | 117 | xptr_t list_xp = XPTR( ref_cxy , &sem_ptr->list ); |
---|
[1] | 118 | |
---|
[563] | 119 | // get lock protecting user synchro lists |
---|
| 120 | remote_queuelock_acquire( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
[457] | 121 | |
---|
[563] | 122 | // register semaphore in reference process list of semaphores |
---|
| 123 | xlist_add_first( root_xp , list_xp ); |
---|
| 124 | |
---|
| 125 | // release lock protecting user synchro lists |
---|
| 126 | remote_queuelock_release( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
| 127 | |
---|
| 128 | #if DEBUG_SEM |
---|
| 129 | thread_t * this = CURRENT_THREAD; |
---|
| 130 | if( (uint32_t)hal_get_cycles() > DEBUG_SEM ) |
---|
| 131 | printk("\n[DBG] %s : thread %x in process %x INITIALIZE sem(%x,%x) / value %d\n", |
---|
| 132 | __FUNCTION__, this->trdid, this->process->pid, local_cxy, sem_ptr, value ); |
---|
| 133 | #endif |
---|
| 134 | |
---|
[1] | 135 | return 0; |
---|
| 136 | |
---|
[563] | 137 | } // end remote_sem_create() |
---|
[1] | 138 | |
---|
[23] | 139 | //////////////////////////////////////// |
---|
| 140 | void remote_sem_destroy( xptr_t sem_xp ) |
---|
| 141 | { |
---|
| 142 | // get pointer on local process descriptor |
---|
| 143 | process_t * process = CURRENT_THREAD->process; |
---|
| 144 | |
---|
| 145 | // get extended pointer on reference process |
---|
| 146 | xptr_t ref_xp = process->ref_xp; |
---|
| 147 | |
---|
| 148 | // get reference process cluster and local pointer |
---|
| 149 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
[457] | 150 | process_t * ref_ptr = GET_PTR( ref_xp ); |
---|
[23] | 151 | |
---|
| 152 | // get semaphore cluster and local pointer |
---|
| 153 | cxy_t sem_cxy = GET_CXY( sem_xp ); |
---|
[563] | 154 | remote_sem_t * sem_ptr = GET_PTR( sem_xp ); |
---|
[23] | 155 | |
---|
[457] | 156 | // get remote pointer on waiting queue root |
---|
| 157 | xptr_t root_xp = XPTR( sem_cxy , &sem_ptr->root ); |
---|
[23] | 158 | |
---|
| 159 | if( !xlist_is_empty( root_xp ) ) // user error |
---|
| 160 | { |
---|
| 161 | printk("WARNING in %s for thread %x in process %x : " |
---|
| 162 | "destroy semaphore, but waiting threads queue not empty\n", |
---|
| 163 | __FUNCTION__ , CURRENT_THREAD->trdid , CURRENT_THREAD->process->pid ); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | // remove semaphore from reference process xlist |
---|
[563] | 167 | remote_queuelock_acquire( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
[23] | 168 | xlist_unlink( XPTR( sem_cxy , &sem_ptr->list ) ); |
---|
[563] | 169 | remote_queuelock_release( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
[23] | 170 | |
---|
| 171 | // release memory allocated for semaphore descriptor |
---|
[683] | 172 | kmem_remote_free( sem_cxy , sem_ptr , bits_log2(sizeof(remote_sem_t)) ); |
---|
[23] | 173 | |
---|
| 174 | } // end remote_sem_destroy() |
---|
| 175 | |
---|
[457] | 176 | ///////////////////////////////////// |
---|
[1] | 177 | void remote_sem_wait( xptr_t sem_xp ) |
---|
| 178 | { |
---|
[563] | 179 | thread_t * this = CURRENT_THREAD; |
---|
| 180 | |
---|
| 181 | // check calling thread can yield |
---|
[671] | 182 | assert( __FUNCTION__, (this->busylocks == 0), |
---|
[563] | 183 | "cannot yield : busylocks = %d\n", this->busylocks ); |
---|
| 184 | |
---|
| 185 | |
---|
[1] | 186 | // get semaphore cluster and local pointer |
---|
| 187 | cxy_t sem_cxy = GET_CXY( sem_xp ); |
---|
[457] | 188 | remote_sem_t * sem_ptr = GET_PTR( sem_xp ); |
---|
[1] | 189 | |
---|
[563] | 190 | // get extended pointers on sem fields |
---|
| 191 | xptr_t count_xp = XPTR( sem_cxy , &sem_ptr->count ); |
---|
| 192 | xptr_t root_xp = XPTR( sem_cxy , &sem_ptr->root ); |
---|
| 193 | xptr_t lock_xp = XPTR( sem_cxy , &sem_ptr->lock ); |
---|
| 194 | |
---|
| 195 | while( 1 ) |
---|
| 196 | { |
---|
| 197 | // get busylock protecting semaphore |
---|
| 198 | remote_busylock_acquire( lock_xp ); |
---|
[1] | 199 | |
---|
[563] | 200 | // get semaphore current value |
---|
| 201 | uint32_t count = hal_remote_l32( count_xp ); |
---|
[1] | 202 | |
---|
[563] | 203 | if( count > 0 ) // success |
---|
| 204 | { |
---|
| 205 | // decrement semaphore value |
---|
| 206 | hal_remote_s32( count_xp , count - 1 ); |
---|
[1] | 207 | |
---|
[563] | 208 | #if DEBUG_SEM |
---|
| 209 | if( (uint32_t)hal_get_cycles() > DEBUG_SEM ) |
---|
| 210 | printk("\n[DBG] %s : thread %x in process %x DECREMENT sem(%x,%x) / value %d\n", |
---|
| 211 | __FUNCTION__, this->trdid, this->process->pid, sem_cxy, sem_ptr, count-1 ); |
---|
| 212 | #endif |
---|
| 213 | // release busylock protecting semaphore |
---|
| 214 | remote_busylock_release( XPTR( sem_cxy , &sem_ptr->lock ) ); |
---|
[1] | 215 | |
---|
[563] | 216 | return; |
---|
| 217 | } |
---|
| 218 | else // failure |
---|
| 219 | { |
---|
| 220 | // get cluster and pointers on calling thread |
---|
| 221 | cxy_t caller_cxy = local_cxy; |
---|
| 222 | thread_t * caller_ptr = CURRENT_THREAD; |
---|
| 223 | xptr_t caller_xp = XPTR( caller_cxy , caller_ptr ); |
---|
[1] | 224 | |
---|
[563] | 225 | // block the calling thread |
---|
| 226 | thread_block( caller_xp , THREAD_BLOCKED_SEM ); |
---|
[1] | 227 | |
---|
[563] | 228 | // register calling thread in waiting queue |
---|
| 229 | xptr_t entry_xp = XPTR( caller_cxy , &caller_ptr->wait_xlist ); |
---|
| 230 | xlist_add_last( root_xp , entry_xp ); |
---|
| 231 | |
---|
| 232 | #if DEBUG_SEM |
---|
| 233 | if( (uint32_t)hal_get_cycles() > DEBUG_SEM ) |
---|
| 234 | printk("\n[DBG] %s : thread %x in process %x BLOCK on sem(%x,%x) / value %d\n", |
---|
| 235 | __FUNCTION__, this->trdid, this->process->pid, sem_cxy, sem_ptr, count ); |
---|
| 236 | #endif |
---|
| 237 | // release busylock protecting semaphore |
---|
| 238 | remote_busylock_release( XPTR( sem_cxy , &sem_ptr->lock ) ); |
---|
| 239 | |
---|
| 240 | // deschedule calling thread |
---|
| 241 | sched_yield("blocked on semaphore"); |
---|
| 242 | } |
---|
[1] | 243 | } |
---|
| 244 | } // end remote_sem_wait() |
---|
| 245 | |
---|
| 246 | ///////////////////////////////////// |
---|
| 247 | void remote_sem_post( xptr_t sem_xp ) |
---|
| 248 | { |
---|
[563] | 249 | // memory barrier before sem release |
---|
| 250 | hal_fence(); |
---|
| 251 | |
---|
[1] | 252 | // get semaphore cluster and local pointer |
---|
| 253 | cxy_t sem_cxy = GET_CXY( sem_xp ); |
---|
[457] | 254 | remote_sem_t * sem_ptr = GET_PTR( sem_xp ); |
---|
[1] | 255 | |
---|
[563] | 256 | // get extended pointers on sem fields |
---|
| 257 | xptr_t count_xp = XPTR( sem_cxy , &sem_ptr->count ); |
---|
| 258 | xptr_t root_xp = XPTR( sem_cxy , &sem_ptr->root ); |
---|
| 259 | xptr_t lock_xp = XPTR( sem_cxy , &sem_ptr->lock ); |
---|
| 260 | |
---|
| 261 | // get busylock protecting semaphore |
---|
| 262 | remote_busylock_acquire( lock_xp ); |
---|
[1] | 263 | |
---|
[563] | 264 | // increment semaphore value |
---|
| 265 | hal_remote_atomic_add( count_xp , 1 ); |
---|
[457] | 266 | |
---|
[563] | 267 | #if DEBUG_SEM |
---|
| 268 | uint32_t count = hal_remote_l32( count_xp ); |
---|
| 269 | thread_t * this = CURRENT_THREAD; |
---|
| 270 | if( (uint32_t)hal_get_cycles() > DEBUG_SEM ) |
---|
| 271 | printk("\n[DBG] %s : thread %x in process %x INCREMENT sem(%x,%x) / value %d\n", |
---|
| 272 | __FUNCTION__, this->trdid, this->process->pid, sem_cxy, sem_ptr, count ); |
---|
| 273 | #endif |
---|
| 274 | |
---|
| 275 | // scan waiting queue to unblock all waiting threads |
---|
| 276 | while( xlist_is_empty( root_xp ) == false ) // waiting queue non empty |
---|
[1] | 277 | { |
---|
| 278 | // get first waiting thread from queue |
---|
[563] | 279 | xptr_t thread_xp = XLIST_FIRST( root_xp , thread_t , wait_xlist ); |
---|
[1] | 280 | cxy_t thread_cxy = GET_CXY( thread_xp ); |
---|
[457] | 281 | thread_t * thread_ptr = GET_PTR( thread_xp ); |
---|
[1] | 282 | |
---|
[563] | 283 | // remove this thread from the waiting queue |
---|
| 284 | xlist_unlink( XPTR( thread_cxy , &thread_ptr->wait_xlist ) ); |
---|
| 285 | |
---|
| 286 | // unblock this waiting thread |
---|
[1] | 287 | thread_unblock( thread_xp , THREAD_BLOCKED_SEM ); |
---|
[563] | 288 | |
---|
| 289 | #if DEBUG_SEM |
---|
| 290 | if( (uint32_t)hal_get_cycles() > DEBUG_SEM ) |
---|
| 291 | { |
---|
| 292 | trdid_t trdid = hal_remote_l32( XPTR( thread_cxy , &thread_ptr->trdid ) ); |
---|
| 293 | process_t * process = hal_remote_lpt( XPTR( thread_cxy , &thread_ptr->process ) ); |
---|
| 294 | pid_t pid = hal_remote_l32( XPTR( thread_cxy , &process->pid ) ); |
---|
| 295 | printk("\n[DBG] %s : thread %x in process %x UNBLOCK thread %x in process %x / sem(%x,%x)\n", |
---|
| 296 | __FUNCTION__, this->trdid, this->process->pid, trdid, pid, sem_cxy, sem_ptr ); |
---|
| 297 | } |
---|
| 298 | #endif |
---|
| 299 | |
---|
[1] | 300 | } |
---|
| 301 | |
---|
[563] | 302 | // release busylock protecting the semaphore |
---|
| 303 | remote_busylock_release( XPTR( sem_cxy , &sem_ptr->lock ) ); |
---|
[1] | 304 | |
---|
| 305 | } // end remote_sem_post() |
---|
| 306 | |
---|
| 307 | |
---|
| 308 | ////////////////////////////////////////////// |
---|
| 309 | void remote_sem_get_value( xptr_t sem_xp, |
---|
| 310 | uint32_t * data ) |
---|
| 311 | { |
---|
| 312 | // get semaphore cluster and local pointer |
---|
| 313 | cxy_t sem_cxy = GET_CXY( sem_xp ); |
---|
[457] | 314 | remote_sem_t * sem_ptr = GET_PTR( sem_xp ); |
---|
[1] | 315 | |
---|
[563] | 316 | *data = hal_remote_l32( XPTR( sem_cxy , &sem_ptr->count ) ); |
---|
[1] | 317 | |
---|
| 318 | } // end remote_sem_get_value() |
---|
| 319 | |
---|
| 320 | |
---|