source: trunk/kernel/syscalls/sys_pipe.c @ 687

Last change on this file since 687 was 683, checked in by alain, 4 years ago

All modifications required to support the <tcp_chat> application
including error recovery in case of packet loss.A

File size: 5.6 KB
Line 
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////////////////////////////
35int 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
49uint64_t   tm_start = hal_get_cycles();
50#endif
51
52#if DEBUG_SYS_PIPE
53if( DEBUG_SYS_PIPE < tm_end )
54printk("\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
63if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
64printk("\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
80if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
81printk("\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
95if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
96printk("\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
110if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
111printk("\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
125if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
126printk("\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
140if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
141printk("\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)
156uint64_t     tm_end = hal_get_cycles();
157#endif
158
159#if DEBUG_SYS_PIPE
160if( DEBUG_SYS_PIPE < tm_end )
161printk("\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
166hal_atomic_add( &syscalls_cumul_cost[SYS_PIPE] , tm_end - tm_start );
167hal_atomic_add( &syscalls_occurences[SYS_PIPE] , 1 );
168#endif
169
170    return 0;
171
172error_5:    // release memory allocated for fd[1] file descriptor
173
174    kmem_free( file_1 , bits_log2(sizeof(vfs_file_t)) );
175
176error_4:    // release fdid_0 from fd_array[]
177
178    process_fd_remove( process->ref_xp , fdid_0 );
179
180error_3:    // release memory allocated for fd[0] file descriptor
181
182    kmem_free( file_0 , bits_log2(sizeof(vfs_file_t)) );
183
184error_2:    // release memory allocated for the pipe
185
186    pipe_destroy( XPTR( local_cxy , pipe ) );
187
188error_1:   // set errno and return error
189
190    this->errno = ENOMEM;
191    return -1;
192
193}  // end sys_pipe()
Note: See TracBrowser for help on using the repository browser.