| 1 | /*
|
|---|
| 2 | * sys_sem.c - Acces a POSIX unamed semaphore.
|
|---|
| 3 | *
|
|---|
| 4 | * Authors 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_uspace.h>
|
|---|
| 26 | #include <errno.h>
|
|---|
| 27 | #include <thread.h>
|
|---|
| 28 | #include <printk.h>
|
|---|
| 29 | #include <vmm.h>
|
|---|
| 30 | #include <remote_sem.h>
|
|---|
| 31 | #include <syscalls.h>
|
|---|
| 32 |
|
|---|
| 33 | //////////////////////////////////
|
|---|
| 34 | int sys_sem( void * vaddr, // semaphore virtual address
|
|---|
| 35 | uint32_t operation, // requested operation type
|
|---|
| 36 | uint32_t * value ) // pointer on in/out argument
|
|---|
| 37 | {
|
|---|
| 38 | uint32_t data;
|
|---|
| 39 | paddr_t paddr;
|
|---|
| 40 | error_t error;
|
|---|
| 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 semaphore virtual address = %x\n",
|
|---|
| 49 | __FUNCTION__ , (intptr_t)vaddr );
|
|---|
| 50 | this->errno = error;
|
|---|
| 51 | return -1;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | // check value in user vspace
|
|---|
| 55 | error = vmm_v2p_translate( false , value , &paddr );
|
|---|
| 56 | if( error )
|
|---|
| 57 | {
|
|---|
| 58 | printk("\n[ERROR] in %s : illegal argument virtual address = %x\n",
|
|---|
| 59 | __FUNCTION__ , (intptr_t)value );
|
|---|
| 60 | this->errno = error;
|
|---|
| 61 | return -1;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | // execute requested operation
|
|---|
| 65 | switch( operation )
|
|---|
| 66 | {
|
|---|
| 67 | //////////////
|
|---|
| 68 | case SEM_INIT:
|
|---|
| 69 | {
|
|---|
| 70 | // get argument
|
|---|
| 71 | hal_copy_from_uspace( &data , value , sizeof(uint32_t) );
|
|---|
| 72 |
|
|---|
| 73 | // call init function
|
|---|
| 74 | error = remote_sem_create( (intptr_t)vaddr , data );
|
|---|
| 75 |
|
|---|
| 76 | if ( error )
|
|---|
| 77 | {
|
|---|
| 78 | printk("\n[ERROR] in %s : cannot create semaphore = %x\n",
|
|---|
| 79 | __FUNCTION__ , (intptr_t)value );
|
|---|
| 80 | this->errno = error;
|
|---|
| 81 | return -1;
|
|---|
| 82 | }
|
|---|
| 83 | break;
|
|---|
| 84 | }
|
|---|
| 85 | //////////////////
|
|---|
| 86 | case SEM_GETVALUE:
|
|---|
| 87 | {
|
|---|
| 88 | // get extended pointer on remote semaphore
|
|---|
| 89 | xptr_t sem_xp = remote_sem_from_vaddr( (intptr_t)vaddr );
|
|---|
| 90 |
|
|---|
| 91 | if( sem_xp == XPTR_NULL ) // user error
|
|---|
| 92 | {
|
|---|
| 93 | printk("\n[ERROR] in %s : semaphore %x not registered\n",
|
|---|
| 94 | __FUNCTION__ , (intptr_t)value );
|
|---|
| 95 | this->errno = EINVAL;
|
|---|
| 96 | return -1;
|
|---|
| 97 | }
|
|---|
| 98 | else // success
|
|---|
| 99 | {
|
|---|
| 100 | // get semaphore current value
|
|---|
| 101 | remote_sem_get_value( sem_xp , &data );
|
|---|
| 102 |
|
|---|
| 103 | // return value to user
|
|---|
| 104 | hal_copy_to_uspace( value , &data , sizeof(uint32_t) );
|
|---|
| 105 | }
|
|---|
| 106 | break;
|
|---|
| 107 | }
|
|---|
| 108 | //////////////
|
|---|
| 109 | case SEM_WAIT:
|
|---|
| 110 | {
|
|---|
| 111 | // get extended pointer on remote semaphore
|
|---|
| 112 | xptr_t sem_xp = remote_sem_from_vaddr( (intptr_t)vaddr );
|
|---|
| 113 |
|
|---|
| 114 | if( sem_xp == XPTR_NULL ) // user error
|
|---|
| 115 | {
|
|---|
| 116 | printk("\n[ERROR] in %s : semaphore %x not registered\n",
|
|---|
| 117 | __FUNCTION__ , (intptr_t)value );
|
|---|
| 118 | this->errno = EINVAL;
|
|---|
| 119 | return -1;
|
|---|
| 120 | }
|
|---|
| 121 | else // success
|
|---|
| 122 | {
|
|---|
| 123 | // wait semaphore available
|
|---|
| 124 | remote_sem_wait( sem_xp );
|
|---|
| 125 | }
|
|---|
| 126 | break;
|
|---|
| 127 | }
|
|---|
| 128 | //////////////
|
|---|
| 129 | case SEM_POST:
|
|---|
| 130 | {
|
|---|
| 131 | // get extended pointer on remote semaphore
|
|---|
| 132 | xptr_t sem_xp = remote_sem_from_vaddr( (intptr_t)vaddr );
|
|---|
| 133 |
|
|---|
| 134 | if( sem_xp == XPTR_NULL ) // user error
|
|---|
| 135 | {
|
|---|
| 136 | printk("\n[ERROR] in %s : semaphore %x not registered\n",
|
|---|
| 137 | __FUNCTION__ , (intptr_t)value );
|
|---|
| 138 | this->errno = EINVAL;
|
|---|
| 139 | return -1;
|
|---|
| 140 | }
|
|---|
| 141 | else // success
|
|---|
| 142 | {
|
|---|
| 143 | // release semaphore
|
|---|
| 144 | remote_sem_post( sem_xp );
|
|---|
| 145 | }
|
|---|
| 146 | break;
|
|---|
| 147 | }
|
|---|
| 148 | /////////////////
|
|---|
| 149 | case SEM_DESTROY:
|
|---|
| 150 | {
|
|---|
| 151 | // get extended pointer on remote semaphore
|
|---|
| 152 | xptr_t sem_xp = remote_sem_from_vaddr( (intptr_t)vaddr );
|
|---|
| 153 |
|
|---|
| 154 | if( sem_xp == XPTR_NULL ) // user error
|
|---|
| 155 | {
|
|---|
| 156 | printk("\n[ERROR] in %s : semaphore %x not registered\n",
|
|---|
| 157 | __FUNCTION__ , (intptr_t)value );
|
|---|
| 158 | this->errno = EINVAL;
|
|---|
| 159 | return -1;
|
|---|
| 160 | }
|
|---|
| 161 | else // success
|
|---|
| 162 | {
|
|---|
| 163 | // destroy semaphore
|
|---|
| 164 | remote_sem_destroy( sem_xp );
|
|---|
| 165 | }
|
|---|
| 166 | break;
|
|---|
| 167 | }
|
|---|
| 168 | ///////
|
|---|
| 169 | default: // undefined operation
|
|---|
| 170 | {
|
|---|
| 171 | printk("\n[PANIC] in %s : illegal operation type\n", __FUNCTION__ );
|
|---|
| 172 | hal_core_sleep();
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | return 0;
|
|---|
| 177 |
|
|---|
| 178 | } // end sys_sem()
|
|---|