[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 | |
---|
[581] | 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 | |
---|
[23] | 44 | ////////////////////////////////// |
---|
[619] | 45 | int sys_barrier( intptr_t vaddr, |
---|
[23] | 46 | uint32_t operation, |
---|
[619] | 47 | uint32_t count, |
---|
| 48 | intptr_t attr ) |
---|
[1] | 49 | { |
---|
[619] | 50 | error_t error; |
---|
| 51 | vseg_t * vseg; |
---|
| 52 | pthread_barrierattr_t k_attr; |
---|
[1] | 53 | |
---|
[619] | 54 | thread_t * this = CURRENT_THREAD; |
---|
| 55 | process_t * process = this->process; |
---|
| 56 | |
---|
[625] | 57 | #if (DEBUG_SYS_BARRIER || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 58 | uint64_t tm_start = hal_get_cycles(); |
---|
| 59 | #endif |
---|
| 60 | |
---|
[581] | 61 | #if DEBUG_SYS_BARRIER |
---|
| 62 | if( DEBUG_SYS_BARRIER < tm_start ) |
---|
[619] | 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, |
---|
[581] | 65 | (uint32_t)tm_start ); |
---|
| 66 | #endif |
---|
| 67 | |
---|
[23] | 68 | // check vaddr in user vspace |
---|
[619] | 69 | error = vmm_get_vseg( process , vaddr , &vseg ); |
---|
[23] | 70 | if( error ) |
---|
| 71 | { |
---|
[440] | 72 | |
---|
| 73 | #if DEBUG_SYSCALLS_ERROR |
---|
[637] | 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 ); |
---|
[440] | 76 | #endif |
---|
[23] | 77 | this->errno = error; |
---|
| 78 | return -1; |
---|
| 79 | } |
---|
[1] | 80 | |
---|
[23] | 81 | // execute requested operation |
---|
| 82 | switch( operation ) |
---|
[1] | 83 | { |
---|
[23] | 84 | ////////////////// |
---|
| 85 | case BARRIER_INIT: |
---|
| 86 | { |
---|
[619] | 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 |
---|
[637] | 94 | printk("\n[ERROR] in %s for INIT : unmapped barrier attributes %x / thread[%x,%x]\n", |
---|
| 95 | __FUNCTION__ , attr , process->pid , this->trdid ); |
---|
[619] | 96 | #endif |
---|
| 97 | this->errno = EINVAL; |
---|
| 98 | return -1; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | // copy barrier attributes into kernel space |
---|
[637] | 102 | hal_copy_from_uspace( XPTR( local_cxy , &k_attr ), |
---|
| 103 | (void *)attr, |
---|
[626] | 104 | sizeof(pthread_barrierattr_t) ); |
---|
[619] | 105 | |
---|
| 106 | if ( count != k_attr.x_size * k_attr.y_size *k_attr.nthreads ) |
---|
| 107 | { |
---|
| 108 | |
---|
| 109 | #if DEBUG_SYSCALLS_ERROR |
---|
[637] | 110 | printk("\n[ERROR] in %s for INIT : count (%d) != x_size (%d) * y_size (%d) * nthreads (%x)\n", |
---|
[619] | 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 | |
---|
[23] | 126 | if( error ) |
---|
| 127 | { |
---|
[440] | 128 | |
---|
| 129 | #if DEBUG_SYSCALLS_ERROR |
---|
[637] | 130 | printk("\n[ERROR] in %s for INIT : cannot create barrier %x / thread[%x,%x]\n", |
---|
| 131 | __FUNCTION__ , vaddr , process->pid , this->trdid ); |
---|
[440] | 132 | #endif |
---|
[619] | 133 | this->errno = ENOMEM; |
---|
[23] | 134 | return -1; |
---|
| 135 | } |
---|
[1] | 136 | break; |
---|
[23] | 137 | } |
---|
| 138 | ////////////////// |
---|
| 139 | case BARRIER_WAIT: |
---|
| 140 | { |
---|
[619] | 141 | xptr_t barrier_xp = generic_barrier_from_ident( vaddr ); |
---|
[1] | 142 | |
---|
[23] | 143 | if( barrier_xp == XPTR_NULL ) // user error |
---|
| 144 | { |
---|
[440] | 145 | |
---|
| 146 | #if DEBUG_SYSCALLS_ERROR |
---|
[637] | 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 ); |
---|
[440] | 149 | #endif |
---|
[23] | 150 | this->errno = EINVAL; |
---|
| 151 | return -1; |
---|
| 152 | } |
---|
| 153 | else // success |
---|
| 154 | { |
---|
[619] | 155 | generic_barrier_wait( barrier_xp ); |
---|
[23] | 156 | } |
---|
| 157 | break; |
---|
| 158 | } |
---|
| 159 | ///////////////////// |
---|
| 160 | case BARRIER_DESTROY: |
---|
| 161 | { |
---|
[619] | 162 | xptr_t barrier_xp = generic_barrier_from_ident( vaddr ); |
---|
[1] | 163 | |
---|
[23] | 164 | if( barrier_xp == XPTR_NULL ) // user error |
---|
| 165 | { |
---|
[440] | 166 | |
---|
| 167 | #if DEBUG_SYSCALLS_ERROR |
---|
[637] | 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 ); |
---|
[440] | 170 | #endif |
---|
[23] | 171 | this->errno = EINVAL; |
---|
| 172 | return -1; |
---|
| 173 | } |
---|
| 174 | else // success |
---|
| 175 | { |
---|
[619] | 176 | generic_barrier_destroy( barrier_xp ); |
---|
[23] | 177 | } |
---|
| 178 | break; |
---|
| 179 | } |
---|
| 180 | //////// |
---|
[629] | 181 | default: |
---|
| 182 | { |
---|
[508] | 183 | assert ( false, "illegal operation type <%x>", operation ); |
---|
[23] | 184 | } |
---|
| 185 | } // end switch |
---|
[1] | 186 | |
---|
[625] | 187 | hal_fence(); |
---|
| 188 | |
---|
| 189 | #if (DEBUG_SYS_BARRIER || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 190 | uint64_t tm_end = hal_get_cycles(); |
---|
| 191 | #endif |
---|
| 192 | |
---|
[581] | 193 | #if DEBUG_SYS_BARRIER |
---|
| 194 | if( DEBUG_SYS_BARRIER < tm_end ) |
---|
[625] | 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 ); |
---|
[581] | 197 | #endif |
---|
| 198 | |
---|
[625] | 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 | |
---|
[23] | 204 | return 0; |
---|
[1] | 205 | |
---|
[23] | 206 | } // end sys_barrier() |
---|
[1] | 207 | |
---|