source: trunk/kernel/syscalls/sys_mutex.c @ 638

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

This version is a major evolution: The physical memory allocators,
defined in the kmem.c, ppm.c, and kcm.c files have been modified
to support remote accesses. The RPCs that were previously user
to allocate physical memory in a remote cluster have been removed.
This has been done to cure a dead-lock in case of concurrent page-faults.

This version 2.2 has been tested on a (4 clusters / 2 cores per cluster)
TSAR architecture, for both the "sort" and the "fft" applications.

File size: 6.7 KB
RevLine 
[1]1/*
[23]2 * sys_mutex.c - Access a POSIX mutex.
[1]3 *
[625]4 * Author    Alain Greiner (2016,2017,2018,2019)
[1]5 *
[23]6 * Copyright (c) UPMC Sorbonne Universites
[1]7 *
[23]8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
[1]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 *
[23]14 * ALMOS-MKH is distributed in the hope that it will be useful, but
[1]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
[23]20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
[1]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_special.h>
[625]26#include <hal_vmm.h>
[1]27#include <errno.h>
28#include <thread.h>
[23]29#include <printk.h>
[1]30#include <vmm.h>
[23]31#include <syscalls.h>
32#include <remote_mutex.h>
[1]33
34
[566]35#if DEBUG_SYS_MUTEX
36////////////////////////////////////////////////////
37static char * sys_mutex_op_str( uint32_t operation )
38{
39        if     ( operation == MUTEX_INIT     ) return "INIT";
40        else if( operation == MUTEX_LOCK     ) return "LOCK";
41        else if( operation == MUTEX_UNLOCK   ) return "UNLOCK";
42        else if( operation == MUTEX_TRYLOCK  ) return "TRYLOCK";
43        else if( operation == MUTEX_DESTROY  ) return "DESTROY";
44        else                                   return "undefined";
45}
46#endif
47
[23]48/////////////////////////////////
49int sys_mutex( void     * vaddr,
50               uint32_t   operation,
51               uint32_t   attr )
[1]52{
[440]53        error_t     error;
[566]54    vseg_t    * vseg;      // for vaddr check
[1]55
[440]56    thread_t  * this    = CURRENT_THREAD;
57    process_t * process = this->process;
[1]58
[625]59#if (DEBUG_SYS_MUTEX || CONFIG_INSTRUMENTATION_SYSCALLS)
60uint64_t     tm_start = hal_get_cycles();
61#endif
62
[566]63#if DEBUG_SYS_MUTEX
64if( DEBUG_SYS_MUTEX < tm_start )
[625]65printk("\n[%s] thread[%x,%x] enter for %s / cycle %d\n",
[566]66__FUNCTION__, this->trdid, process->pid, sys_mutex_op_str( operation ), (uint32_t)tm_start );
67#endif
68
[23]69    // check vaddr in user vspace
[440]70        error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg );
71
[23]72        if( error )
73    {
[440]74
75#if DEBUG_SYSCALLS_ERROR
76printk("\n[ERROR] in %s : mutex unmapped %x / thread %x / process %x\n",
77__FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid );
78#endif
[23]79        this->errno = error;
80        return -1;
81    }
[1]82
[23]83    // execute requested operation
84        switch( operation )
85        {   
86        ////////////////
87            case MUTEX_INIT:
88        { 
89            if( attr != 0 )
90            {
[440]91
92#if DEBUG_SYSCALLS_ERROR
93printk("\n[ERROR] in %s : mutex attribute non supported / thread %x / process %x\n",
94__FUNCTION__ , this->trdid , process->pid );
95#endif
[23]96                this->errno = error;
97                return -1;
98            }
99   
100            error = remote_mutex_create( (intptr_t)vaddr );
[1]101
[23]102            if( error )
103            {
[440]104
105#if DEBUG_SYSCALLS_ERROR
106printk("\n[ERROR] in %s : cannot create mutex / thread %x / process %x\n",
107__FUNCTION__ , this->trdid , process->pid );
108#endif
[23]109                this->errno = error;
110                return -1;
111            } 
112                    break;
113                }
114        ///////////////////
115            case MUTEX_DESTROY:
116        {
117            xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr );
[1]118
[23]119            if( mutex_xp == XPTR_NULL )     // user error
120            {
[440]121
122#if DEBUG_SYSCALLS_ERROR
123printk("\n[ERROR] in %s : mutex %x not registered / thread %x / process %x\n",
124__FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid );
125#endif
[23]126                this->errno = EINVAL;
127                return -1;
128            }
129            else                          // success
130            {
131                remote_mutex_destroy( mutex_xp ); 
132            }
133            break;
134        }
135        ////////////////
136            case MUTEX_LOCK:
137        {
138            xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr );
[1]139
[23]140            if( mutex_xp == XPTR_NULL )     // user error
141            {
[440]142
143#if DEBUG_SYSCALLS_ERROR
144printk("\n[ERROR] in %s : mutex %x not registered / thread %x / process %x\n",
145__FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid );
146#endif
[23]147                this->errno = EINVAL;
148                return -1;
149            }
150            else                          // success
151            {
152                remote_mutex_lock( mutex_xp ); 
153            }
154            break;
155        }
156        //////////////////
157            case MUTEX_UNLOCK:
158        {
159            xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr );
[1]160
[23]161            if( mutex_xp == XPTR_NULL )     // user error
162            {
[440]163
164#if DEBUG_SYSCALLS_ERROR
165printk("\n[ERROR] in %s : mutex %x not registered / thread %x / process %x\n",
166__FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid );
167#endif
[23]168                this->errno = EINVAL;
169                return -1;
170            }
171            else                          // success
172            {
[566]173                error = remote_mutex_unlock( mutex_xp ); 
174
175                if( error )
176                {
177
178#if DEBUG_SYSCALLS_ERROR
179printk("\n[ERROR] in %s : mutex %x not owned in UNLOCK / thread %x / process %x\n",
180__FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid );
181#endif
182                    this->errno = EINVAL;
183                    return -1;
184                }
[23]185            }
186            break;
187        }
[566]188        ///////////////////
189            case MUTEX_TRYLOCK:
190        {
191            xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr );
192
193            if( mutex_xp == XPTR_NULL )     // user error
194            {
195
196#if DEBUG_SYSCALLS_ERROR
197printk("\n[ERROR] in %s : mutex %x not registered / thread %x / process %x\n",
198__FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid );
199#endif
200                this->errno = EINVAL;
201                return -1;
202            }
203            else                          // success
204            {
205                error = remote_mutex_trylock( mutex_xp ); 
206
207                if( error ) // non fatal : mutex already taken
208                {
209                    this->errno = EBUSY;
210                    return -1; 
211                }
212            }
213            break;
214        }
[23]215        ////////
[566]216        default: 
217        {
[508]218            assert ( false, "illegal operation type <%x>", operation );
[23]219        }
220        }
[1]221
[566]222    hal_fence();
223
[625]224#if (DEBUG_SYS_MUTEX || CONFIG_INSTRUMENTATION_SYSCALLS)
225uint64_t     tm_end = hal_get_cycles();
226#endif
227
[566]228#if DEBUG_SYS_MUTEX
[625]229if( DEBUG_SYS_MUTEX < tm_end )
230printk("\n[%s] thread[%x,%x] exit for %s / cycle %d\n",
231__FUNCTION__, this->trdid, process->pid, sys_mutex_op_str( operation ), (uint32_t)tm_end );
[566]232#endif
233
[625]234#if CONFIG_INSTRUMENTATION_SYSCALLS
235hal_atomic_add( &syscalls_cumul_cost[SYS_MUTEX] , tm_end - tm_start );
236hal_atomic_add( &syscalls_occurences[SYS_MUTEX] , 1 );
237#endif
238
[23]239        return 0;
[1]240
[23]241}  // end sys_mutex()
[1]242
Note: See TracBrowser for help on using the repository browser.