[1] | 1 | /* |
---|
[23] | 2 | * sys_barrier.c - Access a POSIX barrier. |
---|
[1] | 3 | * |
---|
[625] | 4 | * authors Alain Greiner (2016,2017,2018,2019) |
---|
[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 | |
---|
[23] | 35 | ////////////////////////////////// |
---|
[619] | 36 | int sys_barrier( intptr_t vaddr, |
---|
[23] | 37 | uint32_t operation, |
---|
[619] | 38 | uint32_t count, |
---|
| 39 | intptr_t attr ) |
---|
[1] | 40 | { |
---|
[619] | 41 | error_t error; |
---|
| 42 | vseg_t * vseg; |
---|
| 43 | pthread_barrierattr_t k_attr; |
---|
[1] | 44 | |
---|
[619] | 45 | thread_t * this = CURRENT_THREAD; |
---|
| 46 | process_t * process = this->process; |
---|
| 47 | |
---|
[670] | 48 | #if DEBUG_SYS_BARRIER || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS |
---|
[625] | 49 | uint64_t tm_start = hal_get_cycles(); |
---|
| 50 | #endif |
---|
| 51 | |
---|
[581] | 52 | #if DEBUG_SYS_BARRIER |
---|
| 53 | if( DEBUG_SYS_BARRIER < tm_start ) |
---|
[619] | 54 | printk("\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, |
---|
[581] | 56 | (uint32_t)tm_start ); |
---|
| 57 | #endif |
---|
| 58 | |
---|
[23] | 59 | // check vaddr in user vspace |
---|
[619] | 60 | error = vmm_get_vseg( process , vaddr , &vseg ); |
---|
[23] | 61 | if( error ) |
---|
| 62 | { |
---|
[440] | 63 | |
---|
| 64 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 65 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[637] | 66 | printk("\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 ); |
---|
[440] | 68 | #endif |
---|
[23] | 69 | this->errno = error; |
---|
| 70 | return -1; |
---|
| 71 | } |
---|
[1] | 72 | |
---|
[23] | 73 | // execute requested operation |
---|
| 74 | switch( operation ) |
---|
[1] | 75 | { |
---|
[23] | 76 | ////////////////// |
---|
| 77 | case BARRIER_INIT: |
---|
| 78 | { |
---|
[619] | 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 |
---|
[670] | 86 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[637] | 87 | printk("\n[ERROR] in %s for INIT : unmapped barrier attributes %x / thread[%x,%x]\n", |
---|
| 88 | __FUNCTION__ , attr , process->pid , this->trdid ); |
---|
[619] | 89 | #endif |
---|
| 90 | this->errno = EINVAL; |
---|
| 91 | return -1; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | // copy barrier attributes into kernel space |
---|
[637] | 95 | hal_copy_from_uspace( XPTR( local_cxy , &k_attr ), |
---|
| 96 | (void *)attr, |
---|
[626] | 97 | sizeof(pthread_barrierattr_t) ); |
---|
[619] | 98 | |
---|
| 99 | if ( count != k_attr.x_size * k_attr.y_size *k_attr.nthreads ) |
---|
| 100 | { |
---|
| 101 | |
---|
| 102 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 103 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[637] | 104 | printk("\n[ERROR] in %s for INIT : count (%d) != x_size (%d) * y_size (%d) * nthreads (%x)\n", |
---|
[619] | 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 | |
---|
[23] | 120 | if( error ) |
---|
| 121 | { |
---|
[440] | 122 | |
---|
| 123 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 124 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[637] | 125 | printk("\n[ERROR] in %s for INIT : cannot create barrier %x / thread[%x,%x]\n", |
---|
| 126 | __FUNCTION__ , vaddr , process->pid , this->trdid ); |
---|
[440] | 127 | #endif |
---|
[619] | 128 | this->errno = ENOMEM; |
---|
[23] | 129 | return -1; |
---|
| 130 | } |
---|
[1] | 131 | break; |
---|
[23] | 132 | } |
---|
| 133 | ////////////////// |
---|
| 134 | case BARRIER_WAIT: |
---|
| 135 | { |
---|
[619] | 136 | xptr_t barrier_xp = generic_barrier_from_ident( vaddr ); |
---|
[1] | 137 | |
---|
[23] | 138 | if( barrier_xp == XPTR_NULL ) // user error |
---|
| 139 | { |
---|
[440] | 140 | |
---|
| 141 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 142 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[637] | 143 | printk("\n[ERROR] in %s for WAIT : barrier %x not registered / thread[%x,%x]\n", |
---|
| 144 | __FUNCTION__ , (intptr_t)vaddr , process->pid, this->trdid ); |
---|
[440] | 145 | #endif |
---|
[23] | 146 | this->errno = EINVAL; |
---|
| 147 | return -1; |
---|
| 148 | } |
---|
| 149 | else // success |
---|
| 150 | { |
---|
[619] | 151 | generic_barrier_wait( barrier_xp ); |
---|
[23] | 152 | } |
---|
| 153 | break; |
---|
| 154 | } |
---|
| 155 | ///////////////////// |
---|
| 156 | case BARRIER_DESTROY: |
---|
| 157 | { |
---|
[619] | 158 | xptr_t barrier_xp = generic_barrier_from_ident( vaddr ); |
---|
[1] | 159 | |
---|
[23] | 160 | if( barrier_xp == XPTR_NULL ) // user error |
---|
| 161 | { |
---|
[440] | 162 | |
---|
| 163 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 164 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[637] | 165 | printk("\n[ERROR] in %s for DESTROY : barrier %x not registered / thread[%x,%x]\n", |
---|
| 166 | __FUNCTION__ , (intptr_t)vaddr , process->pid, this->trdid ); |
---|
[440] | 167 | #endif |
---|
[23] | 168 | this->errno = EINVAL; |
---|
| 169 | return -1; |
---|
| 170 | } |
---|
| 171 | else // success |
---|
| 172 | { |
---|
[619] | 173 | generic_barrier_destroy( barrier_xp ); |
---|
[23] | 174 | } |
---|
| 175 | break; |
---|
| 176 | } |
---|
| 177 | //////// |
---|
[629] | 178 | default: |
---|
| 179 | { |
---|
[670] | 180 | assert( __FUNCTION__, false, "illegal operation type <%x>", operation ); |
---|
[23] | 181 | } |
---|
| 182 | } // end switch |
---|
[1] | 183 | |
---|
[625] | 184 | hal_fence(); |
---|
| 185 | |
---|
| 186 | #if (DEBUG_SYS_BARRIER || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 187 | uint64_t tm_end = hal_get_cycles(); |
---|
| 188 | #endif |
---|
| 189 | |
---|
[581] | 190 | #if DEBUG_SYS_BARRIER |
---|
| 191 | if( DEBUG_SYS_BARRIER < tm_end ) |
---|
[625] | 192 | printk("\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 ); |
---|
[581] | 194 | #endif |
---|
| 195 | |
---|
[625] | 196 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
| 197 | hal_atomic_add( &syscalls_cumul_cost[SYS_BARRIER] , tm_end - tm_start ); |
---|
| 198 | hal_atomic_add( &syscalls_occurences[SYS_BARRIER] , 1 ); |
---|
| 199 | #endif |
---|
| 200 | |
---|
[23] | 201 | return 0; |
---|
[1] | 202 | |
---|
[23] | 203 | } // end sys_barrier() |
---|
[1] | 204 | |
---|