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

Last change on this file since 525 was 469, checked in by alain, 6 years ago

1) Introduce the libsemaphore library.
2) Introduce a small libmath library, required by the "fft" application..
3) Introduce the multithreaded "fft" application.
4) Fix a bad synchronisation bug in the Copy-On-Write mechanism.

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