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