source: trunk/kernel/syscalls/sys_barrier.c

Last change on this file was 683, checked in by alain, 11 months ago

All modifications required to support the <tcp_chat> application
including error recovery in case of packet loss.A

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