source: trunk/kernel/syscalls/sys_sem.c @ 653

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

Introduce the non-standard pthread_parallel_create() system call
and re-write the <fft> and <sort> applications to improve the
intrinsic paralelism in applications.

File size: 7.7 KB
RevLine 
[1]1/*
2 * sys_sem.c - Acces a POSIX unamed semaphore.
3 *
[624]4 * Authors     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_uspace.h>
[624]26#include <hal_vmm.h>
[457]27#include <shared_semaphore.h>
[23]28#include <errno.h>
29#include <thread.h>
30#include <printk.h>
31#include <vmm.h>
[1]32#include <remote_sem.h>
[23]33#include <syscalls.h>
[1]34
[457]35#if DEBUG_SYS_SEM
36//////////////////////////////////////////////////
37static char * sys_sem_op_str( uint32_t operation )
38{
39        if     ( operation == SEM_INIT     ) return "INIT";
40        else if( operation == SEM_WAIT     ) return "WAIT";
41        else if( operation == SEM_POST     ) return "POST";
42        else if( operation == SEM_GETVALUE ) return "GETVALUE";
43        else if( operation == SEM_DESTROY  ) return "DESTROY";
44        else                                 return "undefined";
45}
46#endif
47
[1]48//////////////////////////////////
[457]49int sys_sem( void         * vaddr,            // semaphore virtual  address
50             uint32_t       operation,        // requested operation type
51             uint32_t       init_value,       // initial value
52             uint32_t     * current_value )   // pointer on current value buffer
[1]53{
[566]54        vseg_t         * vseg;        // for vaddr check
[440]55    error_t          error;
[1]56
[440]57    thread_t       * this    = CURRENT_THREAD;
58    process_t      * process = this->process;
[1]59
[637]60#if (DEBUG_SYS_SEM || CONFIG_INSTRUMENTATION_SYSCALLS)
61uint64_t     tm_start = hal_get_cycles();
62#endif
63
[457]64#if DEBUG_SYS_SEM
65if( DEBUG_SYS_SEM < tm_start )
66printk("\n[DBG] %s : thread %x in process %x enter for %s / cycle %d\n",
67__FUNCTION__, this->trdid, process->pid, sys_sem_op_str( operation ), (uint32_t)tm_start );
68#endif
69
[1]70    // check vaddr in user vspace
[440]71        error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg );
[23]72        if( error )
[1]73    {
[440]74
75#if DEBUG_SYSCALLS_ERROR
[469]76printk("\n[ERROR] in %s : unmapped semaphore pointer %x / thread %x in process %x / cycle %d\n",
77__FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid, (uint32_t)hal_get_cycles() );
[440]78#endif
79        this->errno = EINVAL;
[1]80        return -1;
81    }
82
83    // execute requested operation
84        switch( operation )
85        {
86        //////////////
87            case SEM_INIT:
88        {
[457]89            // call relevant kernel function to initialize semaphore
90            error = remote_sem_create( (intptr_t)vaddr , 
[566]91                                       init_value );
[1]92            if ( error )
93            {
[457]94
95#if DEBUG_SYSCALLS_ERROR
[469]96printk("\n[ERROR] in %s INIT: cannot create semaphore / thread %x in process %x / cycle %d\n",
97__FUNCTION__, this->trdid, process->pid, (uint32_t)hal_get_cycles() );
[457]98#endif
99                this->errno = ENOMEM;
[1]100                return -1;
101            }
102        }
[566]103        break;
[1]104        //////////////////
105        case SEM_GETVALUE:
106        {
[457]107            // check current_value buffer in user vspace
108                error = vmm_get_vseg( process , (intptr_t)current_value , &vseg );
109            if( error )
110            {
111
112#if DEBUG_SYSCALLS_ERROR
[566]113printk("\n[ERROR] in %s GETVALUE: unmapped buffer %x / thread %x in process %x / cycle %d\n",
[469]114__FUNCTION__ , (intptr_t)current_value, this->trdid, process->pid, (uint32_t)hal_get_cycles() );
[457]115#endif
116                this->errno = EINVAL;
117                return -1;
118            }
119
[1]120            // get extended pointer on remote semaphore
[566]121            xptr_t sem_xp = remote_sem_from_ident( (intptr_t)vaddr );
[1]122
[457]123            // check semaphore registered
124            if( sem_xp == XPTR_NULL )
[1]125            {
[440]126
127#if DEBUG_SYSCALLS_ERROR
[469]128printk("\n[ERROR] in %s GETVALUE: semaphore %x not found / thread %x in process %x / cycle %d\n",
129__FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid, (uint32_t)hal_get_cycles() );
[440]130#endif
[1]131                this->errno = EINVAL;
132                return -1;
133            }
[457]134
135            // call relevant kernel function to get semaphore current value
[566]136            uint32_t current;
[457]137                    remote_sem_get_value( sem_xp , &current );
[1]138 
[457]139            // return value to user
[637]140            hal_copy_to_uspace( current_value,
141                                XPTR( local_cxy , &current ),
[626]142                                sizeof(uint32_t) );
[1]143        }
[566]144        break;
[1]145        //////////////
146            case SEM_WAIT:
147        { 
148            // get extended pointer on remote semaphore
[566]149            xptr_t sem_xp = remote_sem_from_ident( (intptr_t)vaddr );
[1]150
[457]151            // check semaphore registered
152            if( sem_xp == XPTR_NULL )
[1]153            {
[440]154
155#if DEBUG_SYSCALLS_ERROR
[469]156printk("\n[ERROR] in %s WAIT: semaphore %x not found / thread %x in process %x / cycle %d\n",
157__FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid, (uint32_t)hal_get_cycles() );
[440]158#endif
[1]159                this->errno = EINVAL;
160                return -1;
161            }
[457]162
163            // call relevant kernel function to wait semaphore available
164            remote_sem_wait( sem_xp );
[1]165        }
[566]166        break;
[1]167        //////////////
168            case SEM_POST:
169        {
170            // get extended pointer on remote semaphore
[566]171            xptr_t sem_xp = remote_sem_from_ident( (intptr_t)vaddr );
[1]172
[457]173            // check semaphore registered
174            if( sem_xp == XPTR_NULL )
[1]175            {
[440]176
177#if DEBUG_SYSCALLS_ERROR
[469]178printk("\n[ERROR] in %s POST: semaphore %x not found / thread %x in process %x / cycle %d\n",
179__FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid, (uint32_t)hal_get_cycles() );
[440]180#endif
[1]181                this->errno = EINVAL;
182                return -1;
183            }
[457]184
185            // call relevant kernel function to release semaphore
186            remote_sem_post( sem_xp );
[1]187        }
[566]188                break;
[1]189        /////////////////
190            case SEM_DESTROY:
191        {
192            // get extended pointer on remote semaphore
[566]193            xptr_t sem_xp = remote_sem_from_ident( (intptr_t)vaddr );
[1]194
[457]195            // check semaphore registered
196            if( sem_xp == XPTR_NULL )
[1]197            {
[440]198
199#if DEBUG_SYSCALLS_ERROR
[469]200printk("\n[ERROR] in %s DESTROY: semaphore %x not found / thread %x in process %x / cycle %d\n",
201__FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid, (uint32_t)hal_get_cycles() );
[440]202#endif
[1]203                this->errno = EINVAL;
204                return -1;
205            }
[457]206
207            // destroy semaphore
208            remote_sem_destroy( sem_xp );
[1]209            }   
[566]210        break;
[1]211        ///////
212            default:  // undefined operation                       
213        {
[457]214
215#if DEBUG_SYSCALLS_ERROR
[469]216printk("\n[ERROR] in %s : undefined operation type %d / thread %x in process %x / cycle %d\n",
217__FUNCTION__ , operation, this->trdid, process->pid, (uint32_t)hal_get_cycles() );
[457]218#endif
219            this->errno = EINVAL;
220            return -1;
[1]221        }
222        }
223
[457]224    hal_fence();
225
[637]226#if (DEBUG_SYS_SEM || CONFIG_INSTRUMENTATION_SYSCALLS)
227uint64_t     tm_end = hal_get_cycles();
228#endif
229
[457]230#if DEBUG_SYS_SEM
231if( DEBUG_SYS_SEM < tm_end )
[566]232printk("\n[DBG] %s : thread %x in process %x exit for %s / cost = %d / cycle %d\n",
233__FUNCTION__, this->trdid, process->pid, sys_sem_op_str( operation ),
234(uint32_t)(tm_end - tm_start), (uint32_t)tm_end );
[457]235#endif
236
[637]237#if CONFIG_INSTRUMENTATION_SYSCALLS
238hal_atomic_add( &syscalls_cumul_cost[SYS_SEM] , tm_end - tm_start );
239hal_atomic_add( &syscalls_occurences[SYS_SEM] , 1 );
240#endif
241
[1]242    return 0;
243
244}  // end sys_sem()
Note: See TracBrowser for help on using the repository browser.