| 1 | /*
|
|---|
| 2 | * sys_pipe.c - open a pipe communication channel
|
|---|
| 3 | *
|
|---|
| 4 | * Author Alain Greiner (2016,1017,2018,2019,2020)
|
|---|
| 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 <vfs.h>
|
|---|
| 26 | #include <hal_uspace.h>
|
|---|
| 27 | #include <process.h>
|
|---|
| 28 | #include <thread.h>
|
|---|
| 29 | #include <printk.h>
|
|---|
| 30 | #include <pipe.h>
|
|---|
| 31 |
|
|---|
| 32 | #include <syscalls.h>
|
|---|
| 33 |
|
|---|
| 34 | ////////////////////////////
|
|---|
| 35 | int sys_pipe ( fdid_t * fd )
|
|---|
| 36 | {
|
|---|
| 37 | vseg_t * vseg;
|
|---|
| 38 | pipe_t * pipe;
|
|---|
| 39 | vfs_file_t * file_0;
|
|---|
| 40 | vfs_file_t * file_1;
|
|---|
| 41 | fdid_t fdid_0;
|
|---|
| 42 | fdid_t fdid_1;
|
|---|
| 43 | error_t error;
|
|---|
| 44 |
|
|---|
| 45 | thread_t * this = CURRENT_THREAD;
|
|---|
| 46 | process_t * process = this->process;
|
|---|
| 47 |
|
|---|
| 48 | #if DEBUG_SYS_PIPE || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS
|
|---|
| 49 | uint64_t tm_start = hal_get_cycles();
|
|---|
| 50 | #endif
|
|---|
| 51 |
|
|---|
| 52 | #if DEBUG_SYS_PIPE
|
|---|
| 53 | if( DEBUG_SYS_PIPE < tm_end )
|
|---|
| 54 | printk("\n[%s] thread[%x,%x] enter for <%s> / cycle %d\n",
|
|---|
| 55 | __FUNCTION__, process->pid, this->trdid, pathname, (uint32_t)tm_end );
|
|---|
| 56 | #endif
|
|---|
| 57 |
|
|---|
| 58 | // check user buffer is mapped
|
|---|
| 59 | if( vmm_get_vseg( process , (intptr_t)fd , &vseg ) )
|
|---|
| 60 | {
|
|---|
| 61 |
|
|---|
| 62 | #if DEBUG_SYSCALLS_ERROR
|
|---|
| 63 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
|
|---|
| 64 | printk("\n[ERROR] in %s : thread[%x,%x] / output user buffer unmapped %x\n",
|
|---|
| 65 | __FUNCTION__ , process->pid, this->trdid, (intptr_t)fd );
|
|---|
| 66 | #endif
|
|---|
| 67 | this->errno = EINVAL;
|
|---|
| 68 | return -1;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | // 1. allocate memory in local cluster for pipe descriptor,
|
|---|
| 72 | // remote buf_descriptor, and associated data buffer
|
|---|
| 73 | pipe = pipe_create( local_cxy,
|
|---|
| 74 | CONFIG_PIPE_BUF_SIZE );
|
|---|
| 75 |
|
|---|
| 76 | if( pipe == NULL )
|
|---|
| 77 | {
|
|---|
| 78 |
|
|---|
| 79 | #if DEBUG_SYSCALLS_ERROR
|
|---|
| 80 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
|
|---|
| 81 | printk("\n[ERROR] in %s : thread[%x,%x] / no memory for pipe\n",
|
|---|
| 82 | __FUNCTION__ , process->pid, this->trdid );
|
|---|
| 83 | #endif
|
|---|
| 84 | goto error_1;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | // 2. allocate memory for fd[0] file descriptor in local cluster
|
|---|
| 88 | // we don't use the vfs_file_create function because there is no inode.
|
|---|
| 89 | file_0 = kmem_alloc( bits_log2(sizeof(vfs_file_t)) , AF_ZERO );
|
|---|
| 90 |
|
|---|
| 91 | if( file_0 == NULL )
|
|---|
| 92 | {
|
|---|
| 93 |
|
|---|
| 94 | #if DEBUG_SYSCALLS_ERROR
|
|---|
| 95 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
|
|---|
| 96 | printk("\n[ERROR] in %s : thread[%x,%x] / no memory for file descriptor\n",
|
|---|
| 97 | __FUNCTION__, process->pid, this->trdid );
|
|---|
| 98 | #endif
|
|---|
| 99 | goto error_2;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | // 3. get fd[0] fdid value and register it in owner process fd_array[]
|
|---|
| 103 | error = process_fd_register( process->owner_xp,
|
|---|
| 104 | XPTR( local_cxy , file_0 ),
|
|---|
| 105 | &fdid_0 );
|
|---|
| 106 | if ( error )
|
|---|
| 107 | {
|
|---|
| 108 |
|
|---|
| 109 | #if DEBUG_SYSCALLS_ERROR
|
|---|
| 110 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
|
|---|
| 111 | printk("\n[ERROR] in %s : thread[%x,%x] / cannot register file descriptor \n",
|
|---|
| 112 | __FUNCTION__, process->pid, this->trdid );
|
|---|
| 113 | #endif
|
|---|
| 114 | goto error_3;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | // 4. allocate memory for fd[1] file descriptor in local cluster
|
|---|
| 118 | // we don't use the vfs_file_create function because there is no inode.
|
|---|
| 119 | file_1 = kmem_alloc( bits_log2(sizeof(vfs_file_t)) , AF_ZERO );
|
|---|
| 120 |
|
|---|
| 121 | if( file_1 == NULL )
|
|---|
| 122 | {
|
|---|
| 123 |
|
|---|
| 124 | #if DEBUG_SYSCALLS_ERROR
|
|---|
| 125 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
|
|---|
| 126 | printk("\n[ERROR] in %s : thread[%x,%x] / no memory for file descriptor\n",
|
|---|
| 127 | __FUNCTION__, process->pid, this->trdid );
|
|---|
| 128 | #endif
|
|---|
| 129 | goto error_4;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | // 5. get fd[1] fdid value and register it in owner process fd_array[]
|
|---|
| 133 | error = process_fd_register( process->owner_xp,
|
|---|
| 134 | XPTR( local_cxy , file_1 ),
|
|---|
| 135 | &fdid_1 );
|
|---|
| 136 | if ( error )
|
|---|
| 137 | {
|
|---|
| 138 |
|
|---|
| 139 | #if DEBUG_SYSCALLS_ERROR
|
|---|
| 140 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
|
|---|
| 141 | printk("\n[ERROR] in %s : thread[%x,%x] / cannot register file descriptor \n",
|
|---|
| 142 | __FUNCTION__, process->pid, this->trdid );
|
|---|
| 143 | #endif
|
|---|
| 144 | goto error_5;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | // link the two file descriptors to the pipe
|
|---|
| 148 | file_0->pipe = pipe;
|
|---|
| 149 | file_1->pipe = pipe;
|
|---|
| 150 |
|
|---|
| 151 | // copy fdid_0 & fdid_1 values to user buffer
|
|---|
| 152 | hal_copy_to_uspace( &fd[0] , XPTR( local_cxy , &fdid_0 ) , sizeof(fdid_t) );
|
|---|
| 153 | hal_copy_to_uspace( &fd[1] , XPTR( local_cxy , &fdid_1 ) , sizeof(fdid_t) );
|
|---|
| 154 |
|
|---|
| 155 | #if (DEBUG_SYS_PIPE || CONFIG_INSTRUMENTATION_SYSCALLS)
|
|---|
| 156 | uint64_t tm_end = hal_get_cycles();
|
|---|
| 157 | #endif
|
|---|
| 158 |
|
|---|
| 159 | #if DEBUG_SYS_PIPE
|
|---|
| 160 | if( DEBUG_SYS_PIPE < tm_end )
|
|---|
| 161 | printk("\n[%s] thread[%x,%x] exit for <%s> / cycle %d\n",
|
|---|
| 162 | __FUNCTION__, this->process->pid, this->trdid, pathname, (uint32_t)tm_end );
|
|---|
| 163 | #endif
|
|---|
| 164 |
|
|---|
| 165 | #if CONFIG_INSTRUMENTATION_SYSCALLS
|
|---|
| 166 | hal_atomic_add( &syscalls_cumul_cost[SYS_PIPE] , tm_end - tm_start );
|
|---|
| 167 | hal_atomic_add( &syscalls_occurences[SYS_PIPE] , 1 );
|
|---|
| 168 | #endif
|
|---|
| 169 |
|
|---|
| 170 | return 0;
|
|---|
| 171 |
|
|---|
| 172 | error_5: // release memory allocated for fd[1] file descriptor
|
|---|
| 173 |
|
|---|
| 174 | kmem_free( file_1 , bits_log2(sizeof(vfs_file_t)) );
|
|---|
| 175 |
|
|---|
| 176 | error_4: // release fdid_0 from fd_array[]
|
|---|
| 177 |
|
|---|
| 178 | process_fd_remove( process->ref_xp , fdid_0 );
|
|---|
| 179 |
|
|---|
| 180 | error_3: // release memory allocated for fd[0] file descriptor
|
|---|
| 181 |
|
|---|
| 182 | kmem_free( file_0 , bits_log2(sizeof(vfs_file_t)) );
|
|---|
| 183 |
|
|---|
| 184 | error_2: // release memory allocated for the pipe
|
|---|
| 185 |
|
|---|
| 186 | pipe_destroy( XPTR( local_cxy , pipe ) );
|
|---|
| 187 |
|
|---|
| 188 | error_1: // set errno and return error
|
|---|
| 189 |
|
|---|
| 190 | this->errno = ENOMEM;
|
|---|
| 191 | return -1;
|
|---|
| 192 |
|
|---|
| 193 | } // end sys_pipe()
|
|---|