source: trunk/kernel/kern/remote_sem.c @ 1

Last change on this file since 1 was 1, checked in by alain, 5 years ago

First import

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