source: trunk/kernel/syscalls/sys_read.c @ 674

Last change on this file since 674 was 670, checked in by alain, 4 years ago

1) Introduce up to 4 command lines arguments in the KSH "load" command.
These arguments are transfered to the user process through the
argc/argv mechanism, using the user space "args" vseg.

2) Introduce the named and anonymous "pipes", for inter-process communication
through the pipe() and mkfifo() syscalls.

3) Introduce the "chat" application to validate the two above mechanisms.

4) Improve printk() and assert() fonctions in printk.c.

File size: 10.0 KB
Line 
1/*
2 * sys_read.c - Kernel function implementing the "read" system call.
3 *
4 * Author     Alain Greiner (2016,2017,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 <kernel_config.h>
25#include <hal_kernel_types.h>
26#include <hal_vmm.h>
27#include <hal_uspace.h>
28#include <hal_irqmask.h>
29#include <hal_special.h>
30#include <errno.h>
31#include <vfs.h>
32#include <vmm.h>
33#include <thread.h>
34#include <printk.h>
35#include <pipe.h>
36#include <process.h>
37
38
39extern uint32_t enter_sys_read;
40extern uint32_t enter_devfs_read;
41extern uint32_t enter_txt_read;
42extern uint32_t enter_chdev_cmd_read;
43extern uint32_t enter_chdev_server_read;
44extern uint32_t enter_tty_cmd_read;
45extern uint32_t enter_tty_isr_read;
46extern uint32_t exit_tty_isr_read;
47extern uint32_t exit_tty_cmd_read;
48extern uint32_t exit_chdev_server_read;
49extern uint32_t exit_chdev_cmd_read;
50extern uint32_t exit_txt_read;
51extern uint32_t exit_devfs_read;
52extern uint32_t exit_sys_read;
53
54
55/////////////////////////////////
56int sys_read( uint32_t   file_id,
57              void     * vaddr,
58              uint32_t   count )
59{
60    error_t       error;
61    vseg_t      * vseg;            // required for user space checking
62    xptr_t        file_xp;         // remote file extended pointer
63    vfs_file_t  * file_ptr;        // remote file local pointer
64    cxy_t         file_cxy;        // remote file cluster identifier
65    uint32_t      file_type;       // file type
66    uint32_t      file_offset;     // file offset
67    uint32_t      file_attr;       // file attributes
68    vfs_inode_t * inode_ptr;       // local pointer on file inode
69    uint32_t      nbytes;          // number of bytes actually read
70    reg_t         save_sr;         // required to enable IRQs during syscall
71
72        thread_t    * this             = CURRENT_THREAD;
73        process_t   * process          = this->process;
74    xptr_t        process_owner_xp = process->owner_xp;
75 
76#if DEBUG_SYS_READ || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS
77uint64_t     tm_start = hal_get_cycles();
78#endif
79
80#if DEBUG_SYS_READ
81if( DEBUG_SYS_READ < tm_start )
82printk("\n[%s] thread[%x,%x] enter / vaddr %x / count %d / cycle %d\n",
83__FUNCTION__, process->pid, this->trdid, vaddr, count, (uint32_t)tm_start );
84#endif
85
86#if (DEBUG_SYS_READ & 1)
87enter_sys_read = (uint32_t)tm_start;
88#endif
89
90    // check file_id argument
91        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
92        {
93
94#if DEBUG_SYSCALLS_ERROR
95if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
96printk("\n[ERROR] in %s : thread[%x,%x] / illegal file descriptor index %d\n",
97__FUNCTION__ , process->pid, this->trdid, file_id );
98#endif
99                this->errno = EBADFD;
100                return -1;
101        }
102
103    // check user buffer in user space
104    error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg );
105
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] / user buffer unmapped %x\n",
112__FUNCTION__ , process->pid, this->trdid, (intptr_t)vaddr );
113#endif
114                this->errno = EINVAL;
115                return -1;
116    }
117
118    // get extended pointer on remote file descriptor
119    file_xp = process_fd_get_xptr_from_local( process , file_id );
120
121    if( file_xp == XPTR_NULL )
122    {
123
124#if DEBUG_SYSCALLS_ERROR
125if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
126printk("\n[ERROR] in %s : thread[%x,%x] / undefined fd_id %d\n",
127__FUNCTION__, process->pid, this->trdid, file_id );
128#endif
129        this->errno = EBADFD;
130        return -1;
131    }
132
133    // get file descriptor cluster and local pointer
134    file_ptr = GET_PTR( file_xp );
135    file_cxy = GET_CXY( file_xp );
136
137    // get inode, file type, offset and attributes
138    inode_ptr   = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode  ) );
139    file_type   = hal_remote_l32( XPTR( file_cxy , &file_ptr->type   ) );
140    file_offset = hal_remote_l32( XPTR( file_cxy , &file_ptr->offset ) );
141    file_attr   = hal_remote_l32( XPTR( file_cxy , &file_ptr->attr   ) );
142
143    // enable IRQs
144    hal_enable_irq( &save_sr );
145
146    // check file readable
147    if( (file_attr & FD_ATTR_READ_ENABLE) == 0 )
148    {
149
150#if DEBUG_SYSCALLS_ERROR
151if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
152printk("\n[ERROR] in %s : thread[%x,%x] / file %d not readable\n",
153__FUNCTION__, process->pid, this->trdid, file_id );
154#endif
155        hal_restore_irq( save_sr );
156        this->errno = EBADFD;
157        return -1;
158    }
159
160    // action depend on file type:
161    if( file_type == FILE_TYPE_REG )                           // read from mapper
162    {
163        // try to move count bytes from mapper
164        nbytes = vfs_user_move( true,   //  from mapper
165                                file_xp,
166                                vaddr, 
167                                count );
168    }
169    else if( file_type == FILE_TYPE_DEV )                      // read from TXT device
170    {
171        // get cluster and pointers on TXT_RX chdev
172        xptr_t    chdev_xp  = chdev_from_file( file_xp );
173        cxy_t     chdev_cxy = GET_CXY( chdev_xp );
174        chdev_t * chdev_ptr = GET_PTR( chdev_xp );
175
176        volatile xptr_t    txt_owner_xp;   
177        uint32_t           iter = 0;
178
179        while( 1 )
180        {
181            // extended pointer on TXT owner process
182            txt_owner_xp  = hal_remote_l64( XPTR( chdev_cxy , 
183                                                  &chdev_ptr->ext.txt.owner_xp ) );
184            // wait for TXT_RX ownership
185            if ( process_owner_xp != txt_owner_xp )
186            {
187                if( (iter & 0xFFF) == 0 )
188                printk("\n[WARNING] in %s : thread[%x,%x] wait TXT_RX / cycle %d\n",
189                __FUNCTION__, process->pid, this->trdid, (uint32_t)hal_get_cycles() );
190
191                // deschedule without blocking
192                sched_yield( "wait TXT_RX ownership" );
193
194                iter++;
195            }
196            else
197            {
198                break;
199            }
200        }
201
202        // try to move count bytes from TXT device
203        nbytes = devfs_user_move( true,    // from device
204                                  file_xp,
205                                  vaddr,
206                                  count );
207    }
208    else if( (file_type == FILE_TYPE_PIPE) ||
209             (file_type == FILE_TYPE_FIFO) )                  // read from pipe
210    {
211        // try to move count bytes from pipe
212        nbytes = pipe_user_move( true,    // from pipe
213                                 file_xp,
214                                 vaddr,
215                                 count );
216    }   
217    else                                                      // unsupported type
218    {
219
220#if DEBUG_SYSCALLS_ERROR
221if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
222printk("\n[ERROR] in %s : thread[%x,%x] / unsupported inode type %d\n",
223__FUNCTION__, vfs_inode_type_str( file_type ) );
224#endif
225                this->errno = EBADFD;
226        hal_restore_irq( save_sr );
227                return -1;
228    }
229
230    // check error
231    if( nbytes == 0xFFFFFFFF )
232    {
233
234#if DEBUG_SYSCALLS_ERROR
235if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
236printk("\n[ERROR] in %s : thread[%x,‰x] cannot read data from file %d\n",
237__FUNCTION__, process->pid, this->trdid, file_id );
238#endif
239        this->errno = EIO;
240        hal_restore_irq( save_sr );
241        return -1;
242    }
243
244    // restore IRQs
245    hal_restore_irq( save_sr );
246
247    hal_fence();
248
249#if (DEBUG_SYS_READ || CONFIG_INSTRUMENTATION_SYSCALLS)
250uint64_t     tm_end = hal_get_cycles();
251#endif
252
253#if DEBUG_SYS_READ
254if( DEBUG_SYS_READ < tm_end )
255printk("\n[%s] thread[%x,%x] exit / cycle %d\n",
256__FUNCTION__ , process->pid, this->trdid, (uint32_t)tm_end );
257#endif
258
259#if CONFIG_INSTRUMENTATION_SYSCALLS
260hal_atomic_add( &syscalls_cumul_cost[SYS_READ] , tm_end - tm_start );
261hal_atomic_add( &syscalls_occurences[SYS_READ] , 1 );
262#endif
263
264#if (DEBUG_SYS_READ & 1)
265exit_sys_read = (uint32_t)tm_end;
266
267printk("\n***** timing to read one character *****\n"
268" - enter_sys_read          = %d / delta %d\n"
269" - enter_devfs_read        = %d / delta %d\n"
270" - enter_txt_read          = %d / delta %d\n"
271" - enter_chdev_cmd_read    = %d / delta %d\n"
272" - enter_chdev_server_read = %d / delta %d\n"
273" - enter_tty_cmd_read      = %d / delta %d\n"
274" - enter_tty_isr_read      = %d / delta %d\n"
275" - exit_tty_isr_read       = %d / delta %d\n"
276" - exit_tty_cmd_read       = %d / delta %d\n"
277" - exit_chdev_server_read  = %d / delta %d\n"
278" - exit_chdev_cmd_read     = %d / delta %d\n"
279" - exit_txt_read           = %d / delta %d\n"
280" - exit_devfs_read         = %d / delta %d\n"
281" - exit_sys_read           = %d / delta %d\n",
282enter_sys_read          , 0 ,
283enter_devfs_read        , enter_devfs_read        - enter_sys_read          ,
284enter_txt_read          , enter_txt_read          - enter_devfs_read        ,
285enter_chdev_cmd_read    , enter_chdev_cmd_read    - enter_txt_read          ,
286enter_chdev_server_read , enter_chdev_server_read - enter_chdev_cmd_read    ,
287enter_tty_cmd_read      , enter_tty_cmd_read      - enter_chdev_server_read ,
288enter_tty_isr_read      , enter_tty_isr_read      - enter_tty_cmd_read      ,
289exit_tty_isr_read       , exit_tty_isr_read       - enter_tty_isr_read      ,
290exit_tty_cmd_read       , exit_tty_cmd_read       - exit_tty_isr_read       ,
291exit_chdev_server_read  , exit_chdev_server_read  - exit_tty_cmd_read       ,
292exit_chdev_cmd_read     , exit_chdev_cmd_read     - exit_chdev_server_read  ,
293exit_txt_read           , exit_txt_read           - exit_chdev_cmd_read     ,
294exit_devfs_read         , exit_devfs_read         - exit_txt_read           ,
295exit_sys_read           , exit_sys_read           - exit_devfs_read         );
296#endif
297 
298        return nbytes;
299
300}  // end sys_read()
Note: See TracBrowser for help on using the repository browser.