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

Last change on this file since 603 was 566, checked in by alain, 6 years ago

Complete restructuration of kernel locks.

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