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