source: trunk/kernel/libk/remote_sem.c @ 457

Last change on this file since 457 was 457, checked in by alain, 2 years ago

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

File size: 9.0 KB
Line 
1/*
2 * remote_sem.c - Kernel function implementing the semaphore related syscalls.
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_kernel_types.h>
25#include <hal_remote.h>
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///////////////////////////////////////////////
35xptr_t remote_sem_from_vaddr( intptr_t  vaddr )
36{
37    // get pointer on local process_descriptor
38    process_t * process = CURRENT_THREAD->process;
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 );
45    process_t    * ref_ptr = (process_t *)GET_PTR( ref_xp );
46
47    // get extended pointer on root of semaphores list
48    xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->sem_root );
49   
50    // scan reference process semaphores list
51    xptr_t         iter_xp;
52    xptr_t         sem_xp;
53    cxy_t          sem_cxy;
54    remote_sem_t * sem_ptr;
55    intptr_t       ident;
56    bool_t         found = false;
57           
58    XLIST_FOREACH( root_xp , iter_xp )
59    {
60        sem_xp  = XLIST_ELEMENT( iter_xp , remote_sem_t , list );
61        sem_cxy = GET_CXY( sem_xp );
62        sem_ptr = (remote_sem_t *)GET_PTR( sem_xp );
63        ident   = (intptr_t)hal_remote_lpt( XPTR( sem_cxy , &sem_ptr->ident ) );   
64        if( ident == vaddr )
65        {
66            found = true;
67            break;
68        }
69    }
70
71    if( found == false )  return XPTR_NULL;
72    else                  return sem_xp;
73
74}  // end remote_sem_from_vaddr()
75
76///////////////////////////////////////////
77error_t remote_sem_create( intptr_t   vaddr,
78                           uint32_t   value,
79                           xptr_t     sem_xp_xp )
80{
81    remote_sem_t * sem_ptr;
82    xptr_t         sem_xp;
83
84    // get pointer on local process descriptor
85    process_t * process = CURRENT_THREAD->process;
86
87    // get extended pointer on reference process
88    xptr_t      ref_xp = process->ref_xp;
89
90    // get reference process cluster and local pointer
91    cxy_t       ref_cxy = GET_CXY( ref_xp );
92    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
93
94    // allocate memory for new semaphore in reference cluster
95    if( ref_cxy == local_cxy )  // local cluster is the reference
96    {
97        kmem_req_t req;   
98        req.type  = KMEM_SEM;
99        req.flags = AF_ZERO;
100        sem_ptr   = kmem_alloc( &req );
101        sem_xp    = XPTR( local_cxy , sem_ptr );
102    }
103    else                         // reference is remote
104    {
105        rpc_kcm_alloc_client( ref_cxy , KMEM_SEM , &sem_xp );
106        sem_ptr = GET_PTR( sem_xp );
107    }
108
109    if( sem_xp == XPTR_NULL ) return -1;
110
111    // initialise semaphore
112    hal_remote_sw ( XPTR( ref_cxy , &sem_ptr->count ) , value );
113        hal_remote_spt( XPTR( ref_cxy , &sem_ptr->ident ) , (void *)vaddr );
114    remote_spinlock_init( XPTR( ref_cxy , &sem_ptr->lock ) );
115        xlist_root_init( XPTR( ref_cxy , &sem_ptr->root ) );
116        xlist_entry_init( XPTR( ref_cxy , &sem_ptr->list ) );
117
118    // register semaphore in reference process xlist
119    xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->sem_root );
120    xptr_t xp_list = XPTR( ref_cxy , &sem_ptr->list );
121    remote_spinlock_lock( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
122    xlist_add_first( root_xp , xp_list );
123    remote_spinlock_unlock( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
124
125    // write extended pointer on semaphore in calling thread buffer
126    hal_remote_swd( sem_xp_xp , sem_xp );
127
128    return 0;
129
130}  // en remote_sem_create()
131 
132////////////////////////////////////////
133void remote_sem_destroy( xptr_t sem_xp )
134{
135    // get pointer on local process descriptor
136    process_t * process = CURRENT_THREAD->process;
137
138    // get extended pointer on reference process
139    xptr_t      ref_xp = process->ref_xp;
140
141    // get reference process cluster and local pointer
142    cxy_t       ref_cxy = GET_CXY( ref_xp );
143    process_t * ref_ptr = GET_PTR( ref_xp );
144
145    // get semaphore cluster and local pointer
146    cxy_t          sem_cxy = GET_CXY( sem_xp );
147    remote_sem_t * sem_ptr = (remote_sem_t *)GET_PTR( sem_xp );
148
149    // get lock protecting semaphore
150    remote_spinlock_lock( XPTR( sem_cxy , &sem_ptr->lock ) );
151 
152    // get remote pointer on waiting queue root
153    xptr_t root_xp = XPTR( sem_cxy , &sem_ptr->root );
154 
155    if( !xlist_is_empty( root_xp ) )   // user error
156    {
157        printk("WARNING in %s for thread %x in process %x : "
158               "destroy semaphore, but  waiting threads queue not empty\n", 
159               __FUNCTION__ , CURRENT_THREAD->trdid , CURRENT_THREAD->process->pid );
160    }
161
162    // reset semaphore count
163    hal_remote_sw( XPTR( sem_cxy , &sem_ptr->count ) , 0 );
164
165    // remove semaphore from reference process xlist
166    remote_spinlock_lock( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
167    xlist_unlink( XPTR( sem_cxy , &sem_ptr->list ) );
168    remote_spinlock_unlock( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
169
170    // release lock
171    remote_spinlock_unlock( XPTR( sem_cxy , &sem_ptr->lock ) );
172
173    // release memory allocated for semaphore descriptor
174    if( sem_cxy == local_cxy )                            // reference is local
175    {
176        kmem_req_t  req;
177        req.type = KMEM_SEM;
178        req.ptr  = sem_ptr;
179        kmem_free( &req );
180    }
181    else                                                  // reference is remote
182    {
183        rpc_kcm_free_client( sem_cxy , sem_ptr , KMEM_SEM );
184    }
185
186}  // end remote_sem_destroy()
187
188/////////////////////////////////////
189void remote_sem_wait( xptr_t sem_xp )
190{ 
191    // get semaphore cluster and local pointer
192    cxy_t          sem_cxy = GET_CXY( sem_xp );
193    remote_sem_t * sem_ptr = GET_PTR( sem_xp );
194
195    // get lock protecting semaphore     
196        remote_spinlock_lock( XPTR( sem_cxy , &sem_ptr->lock ) );
197 
198    // get semaphore current value
199    uint32_t count = hal_remote_lw( XPTR( sem_cxy , &sem_ptr->count ) );
200
201        if( count > 0 )       // success
202        {
203        // decrement semaphore value
204        hal_remote_sw( XPTR( sem_cxy , &sem_ptr->count ) , count - 1 );
205
206        // release lock
207            remote_spinlock_unlock( XPTR( sem_cxy , &sem_ptr->lock ) );
208        }
209        else                 // failure
210        {
211        thread_t * this = CURRENT_THREAD;
212
213        // register thread in waiting queue
214        xptr_t root_xp = XPTR( sem_cxy   , &sem_ptr->root );
215        xptr_t list_xp = XPTR( local_cxy , &this->wait_list );
216                xlist_add_last( root_xp , list_xp );
217
218        // release lock
219            remote_spinlock_unlock( XPTR( sem_cxy , &sem_ptr->lock ) );
220
221        // block and deschedule
222        thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_SEM ); 
223        sched_yield("blocked on semaphore");
224        }
225}  // end remote_sem_wait()
226
227/////////////////////////////////////
228void remote_sem_post( xptr_t sem_xp )
229{
230    // get semaphore cluster and local pointer
231    cxy_t          sem_cxy = GET_CXY( sem_xp );
232    remote_sem_t * sem_ptr = GET_PTR( sem_xp );
233
234    // get lock protecting semaphore
235        remote_spinlock_lock( XPTR( sem_cxy , &sem_ptr->lock ) );
236 
237    // get semaphore current value
238    uint32_t count = hal_remote_lw( XPTR( sem_cxy , &sem_ptr->count ) );
239
240    // get remote pointer on waiting queue root
241    xptr_t root_xp = XPTR( sem_cxy , &sem_ptr->root );
242 
243        if( xlist_is_empty( root_xp ) )   // no waiting thread
244    {
245        // increment semaphore value
246        hal_remote_sw( XPTR( sem_cxy , &sem_ptr->count ) , count + 1 );
247    }
248    else
249    {
250        // get first waiting thread from queue
251        xptr_t thread_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list );
252
253        // get thread cluster and local poiner
254        cxy_t      thread_cxy = GET_CXY( thread_xp );
255        thread_t * thread_ptr = GET_PTR( thread_xp );
256
257        // remove this thread from the waiting queue, and unblock it
258        xlist_unlink( XPTR( thread_cxy , &thread_ptr->wait_list ) );
259                thread_unblock( thread_xp , THREAD_BLOCKED_SEM );
260    }
261
262    // release lock
263        remote_spinlock_unlock( XPTR( sem_cxy , &sem_ptr->lock ) );
264
265}  // end remote_sem_post()
266
267
268//////////////////////////////////////////////
269void remote_sem_get_value( xptr_t      sem_xp,
270                           uint32_t  * data )
271{
272    // get semaphore cluster and local pointer
273    cxy_t          sem_cxy = GET_CXY( sem_xp );
274    remote_sem_t * sem_ptr = GET_PTR( sem_xp );
275
276    *data = hal_remote_lw( XPTR( sem_cxy , &sem_ptr->count ) );
277
278}  // end remote_sem_get_value()
279
280
Note: See TracBrowser for help on using the repository browser.