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

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