| 1 | /* |
|---|
| 2 | * sys_barrier.c - Access a POSIX barrier. |
|---|
| 3 | * |
|---|
| 4 | * authors Alain Greiner (2016,2017,2018) |
|---|
| 5 | * |
|---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
|---|
| 7 | * |
|---|
| 8 | * This file is part of ALMOS-MKH. |
|---|
| 9 | * |
|---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
|---|
| 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 | * |
|---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
|---|
| 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 |
|---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
|---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | #include <hal_kernel_types.h> |
|---|
| 25 | #include <hal_special.h> |
|---|
| 26 | #include <errno.h> |
|---|
| 27 | #include <thread.h> |
|---|
| 28 | #include <printk.h> |
|---|
| 29 | #include <vmm.h> |
|---|
| 30 | #include <syscalls.h> |
|---|
| 31 | #include <remote_barrier.h> |
|---|
| 32 | |
|---|
| 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 | |
|---|
| 44 | ////////////////////////////////// |
|---|
| 45 | int sys_barrier( void * vaddr, |
|---|
| 46 | uint32_t operation, |
|---|
| 47 | uint32_t count ) |
|---|
| 48 | { |
|---|
| 49 | error_t error; |
|---|
| 50 | vseg_t * vseg; |
|---|
| 51 | |
|---|
| 52 | thread_t * this = CURRENT_THREAD; |
|---|
| 53 | process_t * process = this->process; |
|---|
| 54 | |
|---|
| 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 | |
|---|
| 65 | // check vaddr in user vspace |
|---|
| 66 | error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg ); |
|---|
| 67 | |
|---|
| 68 | if( error ) |
|---|
| 69 | { |
|---|
| 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 |
|---|
| 76 | this->errno = error; |
|---|
| 77 | return -1; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | // execute requested operation |
|---|
| 81 | switch( operation ) |
|---|
| 82 | { |
|---|
| 83 | ////////////////// |
|---|
| 84 | case BARRIER_INIT: |
|---|
| 85 | { |
|---|
| 86 | error = remote_barrier_create( (intptr_t)vaddr , count ); |
|---|
| 87 | |
|---|
| 88 | if( error ) |
|---|
| 89 | { |
|---|
| 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 |
|---|
| 95 | this->errno = error; |
|---|
| 96 | return -1; |
|---|
| 97 | } |
|---|
| 98 | break; |
|---|
| 99 | } |
|---|
| 100 | ////////////////// |
|---|
| 101 | case BARRIER_WAIT: |
|---|
| 102 | { |
|---|
| 103 | xptr_t barrier_xp = remote_barrier_from_ident( (intptr_t)vaddr ); |
|---|
| 104 | |
|---|
| 105 | if( barrier_xp == XPTR_NULL ) // user error |
|---|
| 106 | { |
|---|
| 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 |
|---|
| 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 ); |
|---|
| 125 | |
|---|
| 126 | if( barrier_xp == XPTR_NULL ) // user error |
|---|
| 127 | { |
|---|
| 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 |
|---|
| 133 | this->errno = EINVAL; |
|---|
| 134 | return -1; |
|---|
| 135 | } |
|---|
| 136 | else // success |
|---|
| 137 | { |
|---|
| 138 | remote_barrier_destroy( barrier_xp ); |
|---|
| 139 | } |
|---|
| 140 | break; |
|---|
| 141 | } |
|---|
| 142 | //////// |
|---|
| 143 | default: { |
|---|
| 144 | assert ( false, "illegal operation type <%x>", operation ); |
|---|
| 145 | } |
|---|
| 146 | } // end switch |
|---|
| 147 | |
|---|
| 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 | |
|---|
| 156 | return 0; |
|---|
| 157 | |
|---|
| 158 | } // end sys_barrier() |
|---|
| 159 | |
|---|