[1] | 1 | /* |
---|
[23] | 2 | * sys_barrier.c - Access a POSIX barrier. |
---|
[1] | 3 | * |
---|
[683] | 4 | * authors Alain Greiner (2016,2017,2018,2019,2020) |
---|
[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 | |
---|
[457] | 24 | #include <hal_kernel_types.h> |
---|
[23] | 25 | #include <hal_special.h> |
---|
[619] | 26 | #include <hal_uspace.h> |
---|
[625] | 27 | #include <hal_vmm.h> |
---|
[1] | 28 | #include <errno.h> |
---|
| 29 | #include <thread.h> |
---|
[23] | 30 | #include <printk.h> |
---|
[1] | 31 | #include <vmm.h> |
---|
[23] | 32 | #include <syscalls.h> |
---|
| 33 | #include <remote_barrier.h> |
---|
[1] | 34 | |
---|
[683] | 35 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 36 | // This function returns a printable string for the barrier related command type. |
---|
| 37 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 38 | |
---|
| 39 | #if DEBUG_SYS_SOCKET || DEBUG_SYSCALLS_ERROR |
---|
| 40 | static 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 | |
---|
[23] | 49 | ////////////////////////////////// |
---|
[619] | 50 | int sys_barrier( intptr_t vaddr, |
---|
[23] | 51 | uint32_t operation, |
---|
[619] | 52 | uint32_t count, |
---|
| 53 | intptr_t attr ) |
---|
[1] | 54 | { |
---|
[619] | 55 | error_t error; |
---|
| 56 | vseg_t * vseg; |
---|
| 57 | pthread_barrierattr_t k_attr; |
---|
[1] | 58 | |
---|
[619] | 59 | thread_t * this = CURRENT_THREAD; |
---|
| 60 | process_t * process = this->process; |
---|
| 61 | |
---|
[670] | 62 | #if DEBUG_SYS_BARRIER || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS |
---|
[625] | 63 | uint64_t tm_start = hal_get_cycles(); |
---|
| 64 | #endif |
---|
| 65 | |
---|
[581] | 66 | #if DEBUG_SYS_BARRIER |
---|
| 67 | if( DEBUG_SYS_BARRIER < tm_start ) |
---|
[619] | 68 | printk("\n[%s] thread[%x,%x] enters for %s / count %d / cycle %d\n", |
---|
[683] | 69 | __FUNCTION__, process->pid, this->trdid, barrier_cmd_str(operation), count, |
---|
[581] | 70 | (uint32_t)tm_start ); |
---|
| 71 | #endif |
---|
| 72 | |
---|
[23] | 73 | // check vaddr in user vspace |
---|
[683] | 74 | if( vmm_get_vseg( process , vaddr , &vseg ) ) |
---|
[23] | 75 | { |
---|
[440] | 76 | |
---|
| 77 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 78 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[637] | 79 | printk("\n[ERROR] in %s for %s : unmapped barrier %x / thread[%x,%x]\n", |
---|
[683] | 80 | __FUNCTION__, barrier_cmd_str(operation), vaddr, process->pid, this->trdid ); |
---|
[440] | 81 | #endif |
---|
[683] | 82 | this->errno = EINVAL; |
---|
[23] | 83 | return -1; |
---|
| 84 | } |
---|
[1] | 85 | |
---|
[23] | 86 | // execute requested operation |
---|
| 87 | switch( operation ) |
---|
[1] | 88 | { |
---|
[23] | 89 | ////////////////// |
---|
| 90 | case BARRIER_INIT: |
---|
| 91 | { |
---|
[619] | 92 | if( attr != 0 ) // QDT barrier required |
---|
| 93 | { |
---|
[683] | 94 | if( vmm_get_vseg( process , attr , &vseg ) ) |
---|
[619] | 95 | { |
---|
| 96 | |
---|
| 97 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 98 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[637] | 99 | printk("\n[ERROR] in %s for INIT : unmapped barrier attributes %x / thread[%x,%x]\n", |
---|
| 100 | __FUNCTION__ , attr , process->pid , this->trdid ); |
---|
[619] | 101 | #endif |
---|
| 102 | this->errno = EINVAL; |
---|
| 103 | return -1; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | // copy barrier attributes into kernel space |
---|
[637] | 107 | hal_copy_from_uspace( XPTR( local_cxy , &k_attr ), |
---|
| 108 | (void *)attr, |
---|
[626] | 109 | sizeof(pthread_barrierattr_t) ); |
---|
[619] | 110 | |
---|
| 111 | if ( count != k_attr.x_size * k_attr.y_size *k_attr.nthreads ) |
---|
| 112 | { |
---|
| 113 | |
---|
| 114 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 115 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[637] | 116 | printk("\n[ERROR] in %s for INIT : count (%d) != x_size (%d) * y_size (%d) * nthreads (%x)\n", |
---|
[619] | 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 | |
---|
[23] | 132 | if( error ) |
---|
| 133 | { |
---|
[440] | 134 | |
---|
| 135 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 136 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[637] | 137 | printk("\n[ERROR] in %s for INIT : cannot create barrier %x / thread[%x,%x]\n", |
---|
| 138 | __FUNCTION__ , vaddr , process->pid , this->trdid ); |
---|
[440] | 139 | #endif |
---|
[619] | 140 | this->errno = ENOMEM; |
---|
[23] | 141 | return -1; |
---|
| 142 | } |
---|
[1] | 143 | break; |
---|
[23] | 144 | } |
---|
| 145 | ////////////////// |
---|
| 146 | case BARRIER_WAIT: |
---|
| 147 | { |
---|
[619] | 148 | xptr_t barrier_xp = generic_barrier_from_ident( vaddr ); |
---|
[1] | 149 | |
---|
[23] | 150 | if( barrier_xp == XPTR_NULL ) // user error |
---|
| 151 | { |
---|
[440] | 152 | |
---|
| 153 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 154 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[637] | 155 | printk("\n[ERROR] in %s for WAIT : barrier %x not registered / thread[%x,%x]\n", |
---|
| 156 | __FUNCTION__ , (intptr_t)vaddr , process->pid, this->trdid ); |
---|
[440] | 157 | #endif |
---|
[23] | 158 | this->errno = EINVAL; |
---|
| 159 | return -1; |
---|
| 160 | } |
---|
| 161 | else // success |
---|
| 162 | { |
---|
[619] | 163 | generic_barrier_wait( barrier_xp ); |
---|
[23] | 164 | } |
---|
| 165 | break; |
---|
| 166 | } |
---|
| 167 | ///////////////////// |
---|
| 168 | case BARRIER_DESTROY: |
---|
| 169 | { |
---|
[619] | 170 | xptr_t barrier_xp = generic_barrier_from_ident( vaddr ); |
---|
[1] | 171 | |
---|
[23] | 172 | if( barrier_xp == XPTR_NULL ) // user error |
---|
| 173 | { |
---|
[440] | 174 | |
---|
| 175 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 176 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[637] | 177 | printk("\n[ERROR] in %s for DESTROY : barrier %x not registered / thread[%x,%x]\n", |
---|
| 178 | __FUNCTION__ , (intptr_t)vaddr , process->pid, this->trdid ); |
---|
[440] | 179 | #endif |
---|
[23] | 180 | this->errno = EINVAL; |
---|
| 181 | return -1; |
---|
| 182 | } |
---|
| 183 | else // success |
---|
| 184 | { |
---|
[619] | 185 | generic_barrier_destroy( barrier_xp ); |
---|
[23] | 186 | } |
---|
| 187 | break; |
---|
| 188 | } |
---|
| 189 | //////// |
---|
[629] | 190 | default: |
---|
| 191 | { |
---|
[670] | 192 | assert( __FUNCTION__, false, "illegal operation type <%x>", operation ); |
---|
[23] | 193 | } |
---|
| 194 | } // end switch |
---|
[1] | 195 | |
---|
[625] | 196 | hal_fence(); |
---|
| 197 | |
---|
| 198 | #if (DEBUG_SYS_BARRIER || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 199 | uint64_t tm_end = hal_get_cycles(); |
---|
| 200 | #endif |
---|
| 201 | |
---|
[581] | 202 | #if DEBUG_SYS_BARRIER |
---|
| 203 | if( DEBUG_SYS_BARRIER < tm_end ) |
---|
[625] | 204 | printk("\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 ); |
---|
[581] | 206 | #endif |
---|
| 207 | |
---|
[625] | 208 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
| 209 | hal_atomic_add( &syscalls_cumul_cost[SYS_BARRIER] , tm_end - tm_start ); |
---|
| 210 | hal_atomic_add( &syscalls_occurences[SYS_BARRIER] , 1 ); |
---|
| 211 | #endif |
---|
| 212 | |
---|
[23] | 213 | return 0; |
---|
[1] | 214 | |
---|
[23] | 215 | } // end sys_barrier() |
---|
[1] | 216 | |
---|