[1] | 1 | /* |
---|
[23] | 2 | * sys_barrier.c - Access a POSIX barrier. |
---|
[1] | 3 | * |
---|
[440] | 4 | * authors Alain Greiner (2016,2017,2018) |
---|
[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> |
---|
[1] | 26 | #include <errno.h> |
---|
| 27 | #include <thread.h> |
---|
[23] | 28 | #include <printk.h> |
---|
[1] | 29 | #include <vmm.h> |
---|
[23] | 30 | #include <syscalls.h> |
---|
| 31 | #include <remote_barrier.h> |
---|
[1] | 32 | |
---|
[581] | 33 | #if DEBUG_SYS_BARRIER |
---|
| 34 | ////////////////////////////////////////////////////// |
---|
| 35 | static char * sys_barrier_op_str( uint32_t operation ) |
---|
| 36 | { |
---|
| 37 | if ( operation == BARRIER_INIT ) return "INIT"; |
---|
| 38 | else if( operation == BARRIER_DESTROY ) return "DESTROY"; |
---|
| 39 | else if( operation == BARRIER_WAIT ) return "WAIT"; |
---|
| 40 | else return "undefined"; |
---|
| 41 | } |
---|
| 42 | #endif |
---|
| 43 | |
---|
[23] | 44 | ////////////////////////////////// |
---|
| 45 | int sys_barrier( void * vaddr, |
---|
| 46 | uint32_t operation, |
---|
| 47 | uint32_t count ) |
---|
[1] | 48 | { |
---|
[23] | 49 | error_t error; |
---|
[440] | 50 | vseg_t * vseg; |
---|
[1] | 51 | |
---|
[440] | 52 | thread_t * this = CURRENT_THREAD; |
---|
| 53 | process_t * process = this->process; |
---|
[1] | 54 | |
---|
[581] | 55 | #if DEBUG_SYS_BARRIER |
---|
| 56 | uint64_t tm_start; |
---|
| 57 | uint64_t tm_end; |
---|
| 58 | tm_start = hal_get_cycles(); |
---|
| 59 | if( DEBUG_SYS_BARRIER < tm_start ) |
---|
| 60 | printk("\n[DBG] %s : thread %x in process %x enter for %s / count %d / cycle %d\n", |
---|
| 61 | __FUNCTION__, this->trdid, process->pid, sys_barrier_op_str(operation), count, |
---|
| 62 | (uint32_t)tm_start ); |
---|
| 63 | #endif |
---|
| 64 | |
---|
[23] | 65 | // check vaddr in user vspace |
---|
[440] | 66 | error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg ); |
---|
| 67 | |
---|
[23] | 68 | if( error ) |
---|
| 69 | { |
---|
[440] | 70 | |
---|
| 71 | #if DEBUG_SYSCALLS_ERROR |
---|
| 72 | printk("\n[ERROR] in %s : unmapped barrier %x / thread %x / process %x\n", |
---|
| 73 | __FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid ); |
---|
| 74 | vmm_display( process , false ); |
---|
| 75 | #endif |
---|
[23] | 76 | this->errno = error; |
---|
| 77 | return -1; |
---|
| 78 | } |
---|
[1] | 79 | |
---|
[23] | 80 | // execute requested operation |
---|
| 81 | switch( operation ) |
---|
[1] | 82 | { |
---|
[23] | 83 | ////////////////// |
---|
| 84 | case BARRIER_INIT: |
---|
| 85 | { |
---|
| 86 | error = remote_barrier_create( (intptr_t)vaddr , count ); |
---|
[1] | 87 | |
---|
[23] | 88 | if( error ) |
---|
| 89 | { |
---|
[440] | 90 | |
---|
| 91 | #if DEBUG_SYSCALLS_ERROR |
---|
| 92 | printk("\n[ERROR] in %s : cannot create barrier %x / thread %x / process %x\n", |
---|
| 93 | __FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid ); |
---|
| 94 | #endif |
---|
[23] | 95 | this->errno = error; |
---|
| 96 | return -1; |
---|
| 97 | } |
---|
[1] | 98 | break; |
---|
[23] | 99 | } |
---|
| 100 | ////////////////// |
---|
| 101 | case BARRIER_WAIT: |
---|
| 102 | { |
---|
| 103 | xptr_t barrier_xp = remote_barrier_from_ident( (intptr_t)vaddr ); |
---|
[1] | 104 | |
---|
[23] | 105 | if( barrier_xp == XPTR_NULL ) // user error |
---|
| 106 | { |
---|
[440] | 107 | |
---|
| 108 | #if DEBUG_SYSCALLS_ERROR |
---|
| 109 | printk("\n[ERROR] in %s : barrier %x not registered / thread %x / process %x\n", |
---|
| 110 | __FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid ); |
---|
| 111 | #endif |
---|
[23] | 112 | this->errno = EINVAL; |
---|
| 113 | return -1; |
---|
| 114 | } |
---|
| 115 | else // success |
---|
| 116 | { |
---|
| 117 | remote_barrier_wait( barrier_xp ); |
---|
| 118 | } |
---|
| 119 | break; |
---|
| 120 | } |
---|
| 121 | ///////////////////// |
---|
| 122 | case BARRIER_DESTROY: |
---|
| 123 | { |
---|
| 124 | xptr_t barrier_xp = remote_barrier_from_ident( (intptr_t)vaddr ); |
---|
[1] | 125 | |
---|
[23] | 126 | if( barrier_xp == XPTR_NULL ) // user error |
---|
| 127 | { |
---|
[440] | 128 | |
---|
| 129 | #if DEBUG_SYSCALLS_ERROR |
---|
| 130 | printk("\n[ERROR] in %s : barrier %x not registered / thread %x / process %x\n", |
---|
| 131 | __FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid ); |
---|
| 132 | #endif |
---|
[23] | 133 | this->errno = EINVAL; |
---|
| 134 | return -1; |
---|
| 135 | } |
---|
| 136 | else // success |
---|
| 137 | { |
---|
| 138 | remote_barrier_destroy( barrier_xp ); |
---|
| 139 | } |
---|
| 140 | break; |
---|
| 141 | } |
---|
| 142 | //////// |
---|
[508] | 143 | default: { |
---|
| 144 | assert ( false, "illegal operation type <%x>", operation ); |
---|
[23] | 145 | } |
---|
| 146 | } // end switch |
---|
[1] | 147 | |
---|
[581] | 148 | #if DEBUG_SYS_BARRIER |
---|
| 149 | tm_end = hal_get_cycles(); |
---|
| 150 | if( DEBUG_SYS_BARRIER < tm_end ) |
---|
| 151 | printk("\n[DBG] %s : thread %x in process %x exit for %s / cost %d / cycle %d\n", |
---|
| 152 | __FUNCTION__, this->trdid, process->pid, sys_barrier_op_str(operation), |
---|
| 153 | (uint32_t)(tm_end - tm_start), (uint32_t)tm_end ); |
---|
| 154 | #endif |
---|
| 155 | |
---|
[23] | 156 | return 0; |
---|
[1] | 157 | |
---|
[23] | 158 | } // end sys_barrier() |
---|
[1] | 159 | |
---|