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

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

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

File size: 5.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
24#include <hal_types.h>
[23]25#include <hal_uspace.h>
26#include <errno.h>
27#include <thread.h>
28#include <printk.h>
29#include <vmm.h>
[1]30#include <remote_sem.h>
[23]31#include <syscalls.h>
[1]32
33//////////////////////////////////
34int sys_sem( void         * vaddr,        // semaphore virtual  address
35             uint32_t       operation,    // requested operation type
36             uint32_t     * value )       // pointer on in/out argument
37{
[440]38        uint32_t         data;   
39        vseg_t         * vseg;
40    error_t          error;
[1]41
[440]42    thread_t       * this    = CURRENT_THREAD;
43    process_t      * process = this->process;
[1]44
45    // check vaddr in user vspace
[440]46        error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg );
[23]47        if( error )
[1]48    {
[440]49
50#if DEBUG_SYSCALLS_ERROR
51printk("\n[ERROR] in %s : unmapped semaphore %x / thread %x / process %x\n",
52__FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid );
53vmm_display( process , false );
54#endif
55        this->errno = EINVAL;
[1]56        return -1;
57    }
58
59    // check value in user vspace
[440]60        error = vmm_get_vseg( process , (intptr_t)value , &vseg );
[23]61        if( error )
[1]62    {
[440]63
64#if DEBUG_SYSCALLS_ERROR
65printk("\n[ERROR] in %s : unmapped value %x / thread %x / process %x\n",
66__FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid );
67vmm_display( process , false );
68#endif
69        this->errno = EINVAL;
70        return -1;
[1]71    }
[440]72
[1]73    // execute requested operation
74        switch( operation )
75        {
76        //////////////
77            case SEM_INIT:
78        {
79            // get argument
80                    hal_copy_from_uspace( &data , value , sizeof(uint32_t) );
81
82            // call init function
[23]83            error = remote_sem_create( (intptr_t)vaddr , data );
[1]84
85            if ( error )
86            {
[23]87                printk("\n[ERROR] in %s : cannot create semaphore = %x\n",
88                       __FUNCTION__ , (intptr_t)value );
[1]89                this->errno = error;
90                return -1;
91            }
92            break;
93        }
94        //////////////////
95        case SEM_GETVALUE:
96        {
97            // get extended pointer on remote semaphore
98            xptr_t sem_xp = remote_sem_from_vaddr( (intptr_t)vaddr );
99
100            if( sem_xp == XPTR_NULL )     // user error
101            {
[440]102
103#if DEBUG_SYSCALLS_ERROR
104printk("\n[ERROR] in %s : semaphore %x not registered / thread %x / process %x\n",
105__FUNCTION__ , (intptr_t)value, this->trdid, process->pid );
106#endif
[1]107                this->errno = EINVAL;
108                return -1;
109            }
110            else                          // success
111            {
112                // get semaphore current value
113                        remote_sem_get_value( sem_xp , &data );
114 
115                // return value to user
116                hal_copy_to_uspace( value , &data , sizeof(uint32_t) );
117            }
118            break;
119        }
120        //////////////
121            case SEM_WAIT:
122        { 
123            // get extended pointer on remote semaphore
124            xptr_t sem_xp = remote_sem_from_vaddr( (intptr_t)vaddr );
125
126            if( sem_xp == XPTR_NULL )     // user error
127            {
[440]128
129#if DEBUG_SYSCALLS_ERROR
130printk("\n[ERROR] in %s : semaphore %x not registered / thread %x / process %x\n",
131__FUNCTION__ , (intptr_t)value, this->trdid, process->pid );
132#endif
[1]133                this->errno = EINVAL;
134                return -1;
135            }
136            else                          // success
137            {
138                // wait semaphore available
139                remote_sem_wait( sem_xp );
140            }
141            break;
142        }
143        //////////////
144            case SEM_POST:
145        {
146            // get extended pointer on remote semaphore
147            xptr_t sem_xp = remote_sem_from_vaddr( (intptr_t)vaddr );
148
149            if( sem_xp == XPTR_NULL )     // user error
150            {
[440]151
152#if DEBUG_SYSCALLS_ERROR
153printk("\n[ERROR] in %s : semaphore %x not registered / thread %x / process %x\n",
154__FUNCTION__ , (intptr_t)value, this->trdid, process->pid );
155#endif
[1]156                this->errno = EINVAL;
157                return -1;
158            }
159            else                          // success
160            {
161                // release semaphore
162                remote_sem_post( sem_xp );
163            }
164                        break;
165        }
166        /////////////////
167            case SEM_DESTROY:
168        {
169            // get extended pointer on remote semaphore
170            xptr_t sem_xp = remote_sem_from_vaddr( (intptr_t)vaddr );
171
172            if( sem_xp == XPTR_NULL )     // user error
173            {
[440]174
175#if DEBUG_SYSCALLS_ERROR
176printk("\n[ERROR] in %s : semaphore %x not registered / thread %x / process %x\n",
177__FUNCTION__ , (intptr_t)value, this->trdid, process->pid );
178#endif
[1]179                this->errno = EINVAL;
180                return -1;
181            }
182            else                          // success
183            {
184                // destroy semaphore
185                remote_sem_destroy( sem_xp );
186            }
187            break;
188            }   
189        ///////
190            default:  // undefined operation                       
191        {
[23]192            printk("\n[PANIC] in %s : illegal operation type\n", __FUNCTION__ );
193            hal_core_sleep();
[1]194        }
195        }
196
197    return 0;
198
199}  // end sys_sem()
Note: See TracBrowser for help on using the repository browser.