| 1 | /*
|
|---|
| 2 | * sys_mutex.c - Access a POSIX mutex.
|
|---|
| 3 | *
|
|---|
| 4 | * Author 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_mutex.h>
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | /////////////////////////////////
|
|---|
| 35 | int sys_mutex( void * vaddr,
|
|---|
| 36 | uint32_t operation,
|
|---|
| 37 | uint32_t attr )
|
|---|
| 38 | {
|
|---|
| 39 | error_t error;
|
|---|
| 40 | vseg_t * vseg;
|
|---|
| 41 |
|
|---|
| 42 | thread_t * this = CURRENT_THREAD;
|
|---|
| 43 | process_t * process = this->process;
|
|---|
| 44 |
|
|---|
| 45 | // check vaddr in user vspace
|
|---|
| 46 | error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg );
|
|---|
| 47 |
|
|---|
| 48 | if( error )
|
|---|
| 49 | {
|
|---|
| 50 |
|
|---|
| 51 | #if DEBUG_SYSCALLS_ERROR
|
|---|
| 52 | printk("\n[ERROR] in %s : mutex unmapped %x / thread %x / process %x\n",
|
|---|
| 53 | __FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid );
|
|---|
| 54 | vmm_display( process , false );
|
|---|
| 55 | #endif
|
|---|
| 56 | this->errno = error;
|
|---|
| 57 | return -1;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | // execute requested operation
|
|---|
| 61 | switch( operation )
|
|---|
| 62 | {
|
|---|
| 63 | ////////////////
|
|---|
| 64 | case MUTEX_INIT:
|
|---|
| 65 | {
|
|---|
| 66 | if( attr != 0 )
|
|---|
| 67 | {
|
|---|
| 68 |
|
|---|
| 69 | #if DEBUG_SYSCALLS_ERROR
|
|---|
| 70 | printk("\n[ERROR] in %s : mutex attribute non supported / thread %x / process %x\n",
|
|---|
| 71 | __FUNCTION__ , this->trdid , process->pid );
|
|---|
| 72 | #endif
|
|---|
| 73 | this->errno = error;
|
|---|
| 74 | return -1;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | error = remote_mutex_create( (intptr_t)vaddr );
|
|---|
| 78 |
|
|---|
| 79 | if( error )
|
|---|
| 80 | {
|
|---|
| 81 |
|
|---|
| 82 | #if DEBUG_SYSCALLS_ERROR
|
|---|
| 83 | printk("\n[ERROR] in %s : cannot create mutex / thread %x / process %x\n",
|
|---|
| 84 | __FUNCTION__ , this->trdid , process->pid );
|
|---|
| 85 | #endif
|
|---|
| 86 | this->errno = error;
|
|---|
| 87 | return -1;
|
|---|
| 88 | }
|
|---|
| 89 | break;
|
|---|
| 90 | }
|
|---|
| 91 | ///////////////////
|
|---|
| 92 | case MUTEX_DESTROY:
|
|---|
| 93 | {
|
|---|
| 94 | xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr );
|
|---|
| 95 |
|
|---|
| 96 | if( mutex_xp == XPTR_NULL ) // user error
|
|---|
| 97 | {
|
|---|
| 98 |
|
|---|
| 99 | #if DEBUG_SYSCALLS_ERROR
|
|---|
| 100 | printk("\n[ERROR] in %s : mutex %x not registered / thread %x / process %x\n",
|
|---|
| 101 | __FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid );
|
|---|
| 102 | #endif
|
|---|
| 103 | this->errno = EINVAL;
|
|---|
| 104 | return -1;
|
|---|
| 105 | }
|
|---|
| 106 | else // success
|
|---|
| 107 | {
|
|---|
| 108 | remote_mutex_destroy( mutex_xp );
|
|---|
| 109 | }
|
|---|
| 110 | break;
|
|---|
| 111 | }
|
|---|
| 112 | ////////////////
|
|---|
| 113 | case MUTEX_LOCK:
|
|---|
| 114 | {
|
|---|
| 115 | xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr );
|
|---|
| 116 |
|
|---|
| 117 | if( mutex_xp == XPTR_NULL ) // user error
|
|---|
| 118 | {
|
|---|
| 119 |
|
|---|
| 120 | #if DEBUG_SYSCALLS_ERROR
|
|---|
| 121 | printk("\n[ERROR] in %s : mutex %x not registered / thread %x / process %x\n",
|
|---|
| 122 | __FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid );
|
|---|
| 123 | #endif
|
|---|
| 124 | this->errno = EINVAL;
|
|---|
| 125 | return -1;
|
|---|
| 126 | }
|
|---|
| 127 | else // success
|
|---|
| 128 | {
|
|---|
| 129 | remote_mutex_lock( mutex_xp );
|
|---|
| 130 | }
|
|---|
| 131 | break;
|
|---|
| 132 | }
|
|---|
| 133 | //////////////////
|
|---|
| 134 | case MUTEX_UNLOCK:
|
|---|
| 135 | {
|
|---|
| 136 | xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr );
|
|---|
| 137 |
|
|---|
| 138 | if( mutex_xp == XPTR_NULL ) // user error
|
|---|
| 139 | {
|
|---|
| 140 |
|
|---|
| 141 | #if DEBUG_SYSCALLS_ERROR
|
|---|
| 142 | printk("\n[ERROR] in %s : mutex %x not registered / thread %x / process %x\n",
|
|---|
| 143 | __FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid );
|
|---|
| 144 | #endif
|
|---|
| 145 | this->errno = EINVAL;
|
|---|
| 146 | return -1;
|
|---|
| 147 | }
|
|---|
| 148 | else // success
|
|---|
| 149 | {
|
|---|
| 150 | remote_mutex_unlock( mutex_xp );
|
|---|
| 151 | }
|
|---|
| 152 | break;
|
|---|
| 153 | }
|
|---|
| 154 | ////////
|
|---|
| 155 | default: {
|
|---|
| 156 | assert ( false, "illegal operation type <%x>", operation );
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | return 0;
|
|---|
| 161 |
|
|---|
| 162 | } // end sys_mutex()
|
|---|
| 163 |
|
|---|