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

Last change on this file since 251 was 23, checked in by alain, 7 years ago

Introduce syscalls.

File size: 8.9 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_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{
80    xptr_t         sem_xp;
81    remote_sem_t * sem_ptr;
82
83    // get pointer on local process descriptor
84    process_t * process = CURRENT_THREAD->process;
85
86    // get extended pointer on reference process
87    xptr_t      ref_xp = process->ref_xp;
88
89    // get reference process cluster and local pointer
90    cxy_t       ref_cxy = GET_CXY( ref_xp );
91    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
92
93    // allocate memory for new semaphore in reference cluster
94    if( ref_cxy == local_cxy )  // local cluster is the reference
95    {
96        kmem_req_t req;   
97        req.type  = KMEM_SEM;
98        req.flags = AF_ZERO;
99        sem_ptr   = kmem_alloc( &req );
100        sem_xp    = XPTR( local_cxy , sem_ptr );
101    }
102    else                         // reference is remote
103    {
104        rpc_kcm_alloc_client( ref_cxy , KMEM_SEM , &sem_xp );
105        sem_ptr = (remote_sem_t *)GET_PTR( sem_xp );
106    }
107
108    if( sem_xp == XPTR_NULL ) return ENOMEM;
109
110    // initialise semaphore
111    hal_remote_sw ( XPTR( ref_cxy , &sem_ptr->count ) , value );
112        hal_remote_spt( XPTR( ref_cxy , &sem_ptr->ident ) , (void *)vaddr );
113
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
122    remote_spinlock_lock( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
123    xlist_add_first( root_xp , xp_list );
124    remote_spinlock_unlock( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
125
126    return 0;
127
128}  // en remote_sem_init()
129 
130////////////////////////////////////////
131void remote_sem_destroy( xptr_t sem_xp )
132{
133    // get pointer on local process descriptor
134    process_t * process = CURRENT_THREAD->process;
135
136    // get extended pointer on reference process
137    xptr_t      ref_xp = process->ref_xp;
138
139    // get reference process cluster and local pointer
140    cxy_t       ref_cxy = GET_CXY( ref_xp );
141    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
142
143    // get semaphore cluster and local pointer
144    cxy_t          sem_cxy = GET_CXY( sem_xp );
145    remote_sem_t * sem_ptr = (remote_sem_t *)GET_PTR( sem_xp );
146
147    // get lock protecting semaphore
148    remote_spinlock_lock( XPTR( sem_cxy , &sem_ptr->lock ) );
149 
150    // get remote pointer on waiting queue
151    xptr_t root_xp = (xptr_t)hal_remote_lwd( XPTR( sem_cxy , &sem_ptr->root ) );
152 
153    if( !xlist_is_empty( root_xp ) )   // user error
154    {
155        printk("WARNING in %s for thread %x in process %x : "
156               "destroy semaphore, but  waiting threads queue not empty\n", 
157               __FUNCTION__ , CURRENT_THREAD->trdid , CURRENT_THREAD->process->pid );
158    }
159
160    // reset semaphore count
161    hal_remote_sw( XPTR( sem_cxy , &sem_ptr->count ) , 0 );
162
163    // remove semaphore from reference process xlist
164    remote_spinlock_lock( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
165    xlist_unlink( XPTR( sem_cxy , &sem_ptr->list ) );
166    remote_spinlock_unlock( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
167
168    // release lock
169    remote_spinlock_unlock( XPTR( sem_cxy , &sem_ptr->lock ) );
170
171    // release memory allocated for semaphore descriptor
172    if( sem_cxy == local_cxy )                            // reference is local
173    {
174        kmem_req_t  req;
175        req.type = KMEM_SEM;
176        req.ptr  = sem_ptr;
177        kmem_free( &req );
178    }
179    else                                                  // reference is remote
180    {
181        rpc_kcm_free_client( sem_cxy , sem_ptr , KMEM_SEM );
182    }
183
184}  // end remote_sem_destroy()
185
186//////////////////////////////////
187void remote_sem_wait( xptr_t sem_xp )
188{ 
189    // get semaphore cluster and local pointer
190    cxy_t          sem_cxy = GET_CXY( sem_xp );
191    remote_sem_t * sem_ptr = (remote_sem_t *)GET_PTR( sem_xp );
192
193    // get lock protecting semaphore     
194        remote_spinlock_lock( XPTR( sem_cxy , &sem_ptr->lock ) );
195 
196    // get semaphore current value
197    uint32_t count = hal_remote_lw( XPTR( sem_cxy , &sem_ptr->count ) );
198
199        if( count > 0 )       // success
200        {
201        // decrement semaphore value
202        hal_remote_sw( XPTR( sem_cxy , &sem_ptr->count ) , count - 1 );
203
204        // release lock
205            remote_spinlock_unlock( XPTR( sem_cxy , &sem_ptr->lock ) );
206        }
207        else                 // failure
208        {
209        thread_t * this = CURRENT_THREAD;
210
211        // register thread in waiting queue
212        xptr_t root_xp   = (xptr_t)hal_remote_lwd( XPTR( sem_cxy , &sem_ptr->root ) );
213        xptr_t thread_xp = XPTR( local_cxy , this );
214                xlist_add_last( root_xp , thread_xp );
215
216        // release lock
217            remote_spinlock_unlock( XPTR( sem_cxy , &sem_ptr->lock ) );
218
219        // block and deschedule
220        thread_block( this , THREAD_BLOCKED_SEM ); 
221        sched_yield();
222        }
223}  // end remote_sem_wait()
224
225/////////////////////////////////////
226void remote_sem_post( xptr_t sem_xp )
227{
228    // get semaphore cluster and local pointer
229    cxy_t          sem_cxy = GET_CXY( sem_xp );
230    remote_sem_t * sem_ptr = (remote_sem_t *)GET_PTR( sem_xp );
231
232    // get lock protecting semaphore
233        remote_spinlock_lock( XPTR( sem_cxy , &sem_ptr->lock ) );
234 
235    // get remote pointer on waiting queue root
236    xptr_t root_xp = (xptr_t)hal_remote_lwd( XPTR( sem_cxy , &sem_ptr->root ) );
237 
238        if( xlist_is_empty( root_xp ) )   // no waiting thread
239    {
240        // get semaphore current value
241        uint32_t count = hal_remote_lw( XPTR( sem_cxy , &sem_ptr->count ) );
242
243        // increment semaphore value
244        hal_remote_sw( XPTR( sem_cxy , &sem_ptr->count ) , count + 1 );
245    }
246    else
247    {
248        // get first waiting thread from queue
249        xptr_t thread_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list );
250
251        // get thread cluster and local poiner
252        cxy_t      thread_cxy = GET_CXY( thread_xp );
253        thread_t * thread_ptr = (thread_t *)GET_PTR( thread_xp );
254
255        // remove the thread from the waiting queue, and unblock
256        xlist_unlink( XPTR( thread_cxy , &thread_ptr->wait_list ) );
257                thread_unblock( thread_xp , THREAD_BLOCKED_SEM );
258    }
259
260    // release lock
261        remote_spinlock_unlock( XPTR( sem_cxy , &sem_ptr->lock ) );
262
263}  // end remote_sem_post()
264
265
266//////////////////////////////////////////////
267void remote_sem_get_value( xptr_t      sem_xp,
268                           uint32_t  * data )
269{
270    // get semaphore cluster and local pointer
271    cxy_t          sem_cxy = GET_CXY( sem_xp );
272    remote_sem_t * sem_ptr = (remote_sem_t *)GET_PTR( sem_xp );
273
274    *data = hal_remote_lw( XPTR( sem_cxy , &sem_ptr->count ) );
275
276}  // end remote_sem_get_value()
277
278
Note: See TracBrowser for help on using the repository browser.