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

Last change on this file since 635 was 635, checked in by alain, 15 months 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: 7.4 KB
Line 
1/*
2 * sys_sem.c - Acces a POSIX unamed semaphore.
3 *
4 * Authors     Alain Greiner (2016,2017,2018,2019)
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 <hal_vmm.h>
27#include <shared_semaphore.h>
28#include <errno.h>
29#include <thread.h>
30#include <printk.h>
31#include <vmm.h>
32#include <remote_sem.h>
33#include <syscalls.h>
34
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
48//////////////////////////////////
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
53{
54        vseg_t         * vseg;        // for vaddr check
55    error_t          error;
56
57    thread_t       * this    = CURRENT_THREAD;
58    process_t      * process = this->process;
59
60#if DEBUG_SYS_SEM
61uint64_t    tm_start;
62uint64_t    tm_end;
63tm_start = hal_get_cycles();
64if( DEBUG_SYS_SEM < tm_start )
65printk("\n[DBG] %s : thread %x in process %x enter for %s / cycle %d\n",
66__FUNCTION__, this->trdid, process->pid, sys_sem_op_str( operation ), (uint32_t)tm_start );
67#endif
68
69    // check vaddr in user vspace
70        error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg );
71        if( error )
72    {
73
74#if DEBUG_SYSCALLS_ERROR
75printk("\n[ERROR] in %s : unmapped semaphore pointer %x / thread %x in process %x / cycle %d\n",
76__FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid, (uint32_t)hal_get_cycles() );
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() );
114#endif
115                this->errno = EINVAL;
116                return -1;
117            }
118
119            // get extended pointer on remote semaphore
120            xptr_t sem_xp = remote_sem_from_ident( (intptr_t)vaddr );
121
122            // check semaphore registered
123            if( sem_xp == XPTR_NULL )
124            {
125
126#if DEBUG_SYSCALLS_ERROR
127printk("\n[ERROR] in %s GETVALUE: semaphore %x not found / thread %x in process %x / cycle %d\n",
128__FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid, (uint32_t)hal_get_cycles() );
129#endif
130                this->errno = EINVAL;
131                return -1;
132            }
133
134            // call relevant kernel function to get semaphore current value
135            uint32_t current;
136                    remote_sem_get_value( sem_xp , &current );
137 
138            // return value to user
139            hal_copy_to_uspace( local_cxy,
140                                &current,
141                                current_value,
142                                sizeof(uint32_t) );
143        }
144        break;
145        //////////////
146            case SEM_WAIT:
147        { 
148            // get extended pointer on remote semaphore
149            xptr_t sem_xp = remote_sem_from_ident( (intptr_t)vaddr );
150
151            // check semaphore registered
152            if( sem_xp == XPTR_NULL )
153            {
154
155#if DEBUG_SYSCALLS_ERROR
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() );
158#endif
159                this->errno = EINVAL;
160                return -1;
161            }
162
163            // call relevant kernel function to wait semaphore available
164            remote_sem_wait( sem_xp );
165        }
166        break;
167        //////////////
168            case SEM_POST:
169        {
170            // get extended pointer on remote semaphore
171            xptr_t sem_xp = remote_sem_from_ident( (intptr_t)vaddr );
172
173            // check semaphore registered
174            if( sem_xp == XPTR_NULL )
175            {
176
177#if DEBUG_SYSCALLS_ERROR
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() );
180#endif
181                this->errno = EINVAL;
182                return -1;
183            }
184
185            // call relevant kernel function to release semaphore
186            remote_sem_post( sem_xp );
187        }
188                break;
189        /////////////////
190            case SEM_DESTROY:
191        {
192            // get extended pointer on remote semaphore
193            xptr_t sem_xp = remote_sem_from_ident( (intptr_t)vaddr );
194
195            // check semaphore registered
196            if( sem_xp == XPTR_NULL )
197            {
198
199#if DEBUG_SYSCALLS_ERROR
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() );
202#endif
203                this->errno = EINVAL;
204                return -1;
205            }
206
207            // destroy semaphore
208            remote_sem_destroy( sem_xp );
209            }   
210        break;
211        ///////
212            default:  // undefined operation                       
213        {
214
215#if DEBUG_SYSCALLS_ERROR
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() );
218#endif
219            this->errno = EINVAL;
220            return -1;
221        }
222        }
223
224    hal_fence();
225
226#if DEBUG_SYS_SEM
227tm_end = hal_get_cycles();
228if( DEBUG_SYS_SEM < tm_end )
229printk("\n[DBG] %s : thread %x in process %x exit for %s / cost = %d / cycle %d\n",
230__FUNCTION__, this->trdid, process->pid, sys_sem_op_str( operation ),
231(uint32_t)(tm_end - tm_start), (uint32_t)tm_end );
232#endif
233
234    return 0;
235
236}  // end sys_sem()
Note: See TracBrowser for help on using the repository browser.