source: trunk/kernel/syscalls/sys_barrier.c @ 670

Last change on this file since 670 was 670, checked in by alain, 3 years ago

1) Introduce up to 4 command lines arguments in the KSH "load" command.
These arguments are transfered to the user process through the
argc/argv mechanism, using the user space "args" vseg.

2) Introduce the named and anonymous "pipes", for inter-process communication
through the pipe() and mkfifo() syscalls.

3) Introduce the "chat" application to validate the two above mechanisms.

4) Improve printk() and assert() fonctions in printk.c.

File size: 6.1 KB
Line 
1/*
2 * sys_barrier.c - Access a POSIX barrier.
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_special.h>
26#include <hal_uspace.h>
27#include <hal_vmm.h>
28#include <errno.h>
29#include <thread.h>
30#include <printk.h>
31#include <vmm.h>
32#include <syscalls.h>
33#include <remote_barrier.h>
34
35//////////////////////////////////
36int sys_barrier( intptr_t   vaddr,
37                 uint32_t   operation,
38                 uint32_t   count,
39                 intptr_t   attr )   
40{
41        error_t                 error;
42    vseg_t                * vseg;
43    pthread_barrierattr_t   k_attr;
44
45    thread_t  * this    = CURRENT_THREAD;
46    process_t * process = this->process;
47
48#if DEBUG_SYS_BARRIER || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS
49uint64_t     tm_start = hal_get_cycles();
50#endif
51
52#if DEBUG_SYS_BARRIER
53if( DEBUG_SYS_BARRIER < tm_start )
54printk("\n[%s] thread[%x,%x] enters for %s / count %d / cycle %d\n",
55__FUNCTION__, process->pid, this->trdid, sys_barrier_op_str(operation), count,
56(uint32_t)tm_start );
57#endif
58
59    // check vaddr in user vspace
60        error = vmm_get_vseg( process , vaddr , &vseg );
61        if( error )
62    {
63
64#if DEBUG_SYSCALLS_ERROR
65if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 
66printk("\n[ERROR] in %s for %s : unmapped barrier %x / thread[%x,%x]\n",
67__FUNCTION__, sys_barrier_op_str(operation), vaddr, process->pid, this->trdid );
68#endif
69        this->errno = error;
70        return -1;
71    }
72
73    // execute requested operation
74        switch( operation )
75        {
76        //////////////////
77            case BARRIER_INIT:
78        {
79            if( attr != 0 )   // QDT barrier required
80            {
81                error = vmm_get_vseg( process , attr , &vseg );
82                if( error )
83                {
84
85#if DEBUG_SYSCALLS_ERROR
86if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 
87printk("\n[ERROR] in %s for INIT : unmapped barrier attributes %x / thread[%x,%x]\n",
88__FUNCTION__ , attr , process->pid , this->trdid );
89#endif
90                    this->errno = EINVAL;
91                    return -1;
92                }
93 
94                // copy barrier attributes into kernel space
95                hal_copy_from_uspace( XPTR( local_cxy , &k_attr ),
96                                      (void *)attr,
97                                      sizeof(pthread_barrierattr_t) );
98
99                if ( count != k_attr.x_size * k_attr.y_size *k_attr.nthreads ) 
100                {
101
102#if DEBUG_SYSCALLS_ERROR
103if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 
104printk("\n[ERROR] in %s for INIT : count (%d) != x_size (%d) * y_size (%d) * nthreads (%x)\n",
105__FUNCTION__, count, k_attr.x_size, k_attr.y_size, k_attr.nthreads );
106#endif
107                    this->errno = EINVAL;
108                    return -1;
109                }
110 
111
112                // call relevant system function
113                error = generic_barrier_create( vaddr , count , &k_attr );
114            }
115            else               // simple barrier required
116            {
117                error = generic_barrier_create( vaddr , count , NULL );
118            }
119
120                    if( error )
121            {
122
123#if DEBUG_SYSCALLS_ERROR
124if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 
125printk("\n[ERROR] in %s for INIT : cannot create barrier %x / thread[%x,%x]\n",
126__FUNCTION__ , vaddr , process->pid , this->trdid );
127#endif
128                this->errno = ENOMEM;
129                return -1;
130            }
131                        break;
132        }
133        //////////////////
134            case BARRIER_WAIT:
135        {
136            xptr_t barrier_xp = generic_barrier_from_ident( vaddr );
137
138            if( barrier_xp == XPTR_NULL )     // user error
139            {
140
141#if DEBUG_SYSCALLS_ERROR
142if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 
143printk("\n[ERROR] in %s for WAIT : barrier %x not registered / thread[%x,%x]\n",
144__FUNCTION__ , (intptr_t)vaddr , process->pid, this->trdid );
145#endif
146                this->errno = EINVAL;
147                return -1;
148            }
149            else                          // success
150            {
151                generic_barrier_wait( barrier_xp ); 
152            }
153            break;
154        }
155        /////////////////////
156            case BARRIER_DESTROY:
157        {
158            xptr_t barrier_xp = generic_barrier_from_ident( vaddr );
159
160            if( barrier_xp == XPTR_NULL )     // user error
161            {
162
163#if DEBUG_SYSCALLS_ERROR
164if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 
165printk("\n[ERROR] in %s for DESTROY : barrier %x not registered / thread[%x,%x]\n",
166__FUNCTION__ , (intptr_t)vaddr , process->pid, this->trdid );
167#endif
168                this->errno = EINVAL;
169                return -1;
170            }
171            else                          // success
172            {
173                generic_barrier_destroy( barrier_xp ); 
174            }
175            break;
176        }
177        ////////
178        default:
179        {
180            assert( __FUNCTION__, false, "illegal operation type <%x>", operation );
181        }
182        }  // end switch
183
184    hal_fence();
185
186#if (DEBUG_SYS_BARRIER || CONFIG_INSTRUMENTATION_SYSCALLS)
187uint64_t     tm_end = hal_get_cycles();
188#endif
189
190#if DEBUG_SYS_BARRIER
191if( DEBUG_SYS_BARRIER < tm_end )
192printk("\n[%s] thread[%x,%x] exit for %s / cycle %d\n",
193__FUNCTION__, process->pid, this->trdid, sys_barrier_op_str(operation), (uint32_t)tm_end );
194#endif
195
196#if CONFIG_INSTRUMENTATION_SYSCALLS
197hal_atomic_add( &syscalls_cumul_cost[SYS_BARRIER] , tm_end - tm_start );
198hal_atomic_add( &syscalls_occurences[SYS_BARRIER] , 1 );
199#endif
200
201        return 0;
202
203}  // end sys_barrier()
204
Note: See TracBrowser for help on using the repository browser.