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

Last change on this file since 458 was 457, checked in by alain, 6 years ago

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

File size: 7.4 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
[457]76printk("\n[ERROR] in %s : unmapped semaphore pointer %x / thread %x in process %x\n",
[440]77__FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid );
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
99printk("\n[ERROR] in %s : cannot create semaphore / thread %x in process %x\n",
100__FUNCTION__, this->trdid, process->pid );
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
117printk("\n[ERROR] in %s : unmapped buffer for current value %x / thread %x in process %x\n",
118__FUNCTION__ , (intptr_t)current_value, this->trdid, process->pid );
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
[457]133printk("\n[ERROR] in %s : semaphore %x not registered / thread %x in process %x\n",
134__FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid );
[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
[457]159printk("\n[ERROR] in %s : semaphore %x not registered / thread %x in process %x\n",
160__FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid );
[440]161#endif
[1]162                this->errno = EINVAL;
163                return -1;
164            }
[457]165
166            // call relevant kernel function to wait semaphore available
167            remote_sem_wait( sem_xp );
168           
[1]169            break;
170        }
171        //////////////
172            case SEM_POST:
173        {
174            // get extended pointer on remote semaphore
[457]175            sem_xp = remote_sem_from_vaddr( (intptr_t)vaddr );
[1]176
[457]177            // check semaphore registered
178            if( sem_xp == XPTR_NULL )
[1]179            {
[440]180
181#if DEBUG_SYSCALLS_ERROR
[457]182printk("\n[ERROR] in %s : semaphore %x not registered / thread %x in process %x\n",
183__FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid );
[440]184#endif
[1]185                this->errno = EINVAL;
186                return -1;
187            }
[457]188
189            // call relevant kernel function to release semaphore
190            remote_sem_post( sem_xp );
191
[1]192                        break;
193        }
194        /////////////////
195            case SEM_DESTROY:
196        {
197            // get extended pointer on remote semaphore
[457]198            sem_xp = remote_sem_from_vaddr( (intptr_t)vaddr );
[1]199
[457]200            // check semaphore registered
201            if( sem_xp == XPTR_NULL )
[1]202            {
[440]203
204#if DEBUG_SYSCALLS_ERROR
[457]205printk("\n[ERROR] in %s : semaphore %x not registered / thread %x in process %x\n",
206__FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid );
[440]207#endif
[1]208                this->errno = EINVAL;
209                return -1;
210            }
[457]211
212            // destroy semaphore
213            remote_sem_destroy( sem_xp );
214
[1]215            break;
216            }   
217        ///////
218            default:  // undefined operation                       
219        {
[457]220
221#if DEBUG_SYSCALLS_ERROR
222printk("\n[ERROR] in %s : undefined operation type %d / thread %x in process %x\n",
223__FUNCTION__ , operation, this->trdid, process->pid );
224#endif
225            this->errno = EINVAL;
226            return -1;
[1]227        }
228        }
229
[457]230    hal_fence();
231
232#if DEBUG_SYS_SEM
233tm_end = hal_get_cycles();
234if( DEBUG_SYS_SEM < tm_end )
235{
236    cxy_t          sem_cxy = GET_CXY( sem_xp );
237    remote_sem_t * sem_ptr = GET_PTR( sem_xp );
238    uint32_t       value   = hal_remote_lw( XPTR( sem_cxy , &sem_ptr->count ) ); 
239    printk("\n[DBG] %s : thread %x in process %x exit for %s / value %d / cost = %d / cycle %d\n",
240    __FUNCTION__, this->trdid, process->pid, sys_sem_op_str( operation ), value, 
241    (uint32_t)(tm_end - tm_start), (uint32_t)tm_end );
242}
243#endif
244
[1]245    return 0;
246
247}  // end sys_sem()
Note: See TracBrowser for help on using the repository browser.