| 1 | /* |
|---|
| 2 | * sys_mutex.c - Access a POSIX mutex. |
|---|
| 3 | * |
|---|
| 4 | * Author Alain Greiner (2016,2017) |
|---|
| 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_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 | paddr_t paddr; |
|---|
| 41 | |
|---|
| 42 | thread_t * this = CURRENT_THREAD; |
|---|
| 43 | |
|---|
| 44 | // check vaddr in user vspace |
|---|
| 45 | error = vmm_v2p_translate( false , vaddr , &paddr ); |
|---|
| 46 | if( error ) |
|---|
| 47 | { |
|---|
| 48 | printk("\n[ERROR] in %s : illegal virtual address = %x\n", |
|---|
| 49 | __FUNCTION__ , (intptr_t)vaddr ); |
|---|
| 50 | this->errno = error; |
|---|
| 51 | return -1; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | // execute requested operation |
|---|
| 55 | switch( operation ) |
|---|
| 56 | { |
|---|
| 57 | //////////////// |
|---|
| 58 | case MUTEX_INIT: |
|---|
| 59 | { |
|---|
| 60 | if( attr != 0 ) |
|---|
| 61 | { |
|---|
| 62 | printk("\n[ERROR] in %s : mutex attributes non supported yet\n", |
|---|
| 63 | __FUNCTION__ ); |
|---|
| 64 | this->errno = error; |
|---|
| 65 | return -1; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | error = remote_mutex_create( (intptr_t)vaddr ); |
|---|
| 69 | |
|---|
| 70 | if( error ) |
|---|
| 71 | { |
|---|
| 72 | printk("\n[ERROR] in %s : cannot create mutex\n", |
|---|
| 73 | __FUNCTION__ ); |
|---|
| 74 | this->errno = error; |
|---|
| 75 | return -1; |
|---|
| 76 | } |
|---|
| 77 | break; |
|---|
| 78 | } |
|---|
| 79 | /////////////////// |
|---|
| 80 | case MUTEX_DESTROY: |
|---|
| 81 | { |
|---|
| 82 | xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr ); |
|---|
| 83 | |
|---|
| 84 | if( mutex_xp == XPTR_NULL ) // user error |
|---|
| 85 | { |
|---|
| 86 | printk("\n[ERROR] in %s : mutex %x not registered\n", |
|---|
| 87 | __FUNCTION__ , (intptr_t)vaddr ); |
|---|
| 88 | this->errno = EINVAL; |
|---|
| 89 | return -1; |
|---|
| 90 | } |
|---|
| 91 | else // success |
|---|
| 92 | { |
|---|
| 93 | remote_mutex_destroy( mutex_xp ); |
|---|
| 94 | } |
|---|
| 95 | break; |
|---|
| 96 | } |
|---|
| 97 | //////////////// |
|---|
| 98 | case MUTEX_LOCK: |
|---|
| 99 | { |
|---|
| 100 | xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr ); |
|---|
| 101 | |
|---|
| 102 | if( mutex_xp == XPTR_NULL ) // user error |
|---|
| 103 | { |
|---|
| 104 | printk("\n[ERROR] in %s : mutex %x not registered\n", |
|---|
| 105 | __FUNCTION__ , (intptr_t)vaddr ); |
|---|
| 106 | this->errno = EINVAL; |
|---|
| 107 | return -1; |
|---|
| 108 | } |
|---|
| 109 | else // success |
|---|
| 110 | { |
|---|
| 111 | remote_mutex_lock( mutex_xp ); |
|---|
| 112 | } |
|---|
| 113 | break; |
|---|
| 114 | } |
|---|
| 115 | ////////////////// |
|---|
| 116 | case MUTEX_UNLOCK: |
|---|
| 117 | { |
|---|
| 118 | xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr ); |
|---|
| 119 | |
|---|
| 120 | if( mutex_xp == XPTR_NULL ) // user error |
|---|
| 121 | { |
|---|
| 122 | printk("\n[ERROR] in %s : mutex %x not registered\n", |
|---|
| 123 | __FUNCTION__ , (intptr_t)vaddr ); |
|---|
| 124 | this->errno = EINVAL; |
|---|
| 125 | return -1; |
|---|
| 126 | } |
|---|
| 127 | else // success |
|---|
| 128 | { |
|---|
| 129 | remote_mutex_unlock( mutex_xp ); |
|---|
| 130 | } |
|---|
| 131 | break; |
|---|
| 132 | } |
|---|
| 133 | //////// |
|---|
| 134 | default: |
|---|
| 135 | { |
|---|
| 136 | printk("\n[PANIC] in %s : illegal operation type\n", __FUNCTION__ ); |
|---|
| 137 | hal_core_sleep(); |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | return 0; |
|---|
| 142 | |
|---|
| 143 | } // end sys_mutex() |
|---|
| 144 | |
|---|