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
RevLine 
[1]1/*
[625]2 * sys_read.c - Kernel function implementing the "read" system call.
[1]3 *
[670]4 * Author     Alain Greiner (2016,2017,2018,2019,2020)
[1]5 *
[23]6 * Copyright (c) UPMC Sorbonne Universites
[1]7 *
[23]8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
[1]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 *
[23]14 * ALMOS-MKH is distributed in the hope that it will be useful, but
[1]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
[23]20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
[1]21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
[23]24#include <kernel_config.h>
[457]25#include <hal_kernel_types.h>
[625]26#include <hal_vmm.h>
[23]27#include <hal_uspace.h>
[409]28#include <hal_irqmask.h>
[23]29#include <hal_special.h>
[1]30#include <errno.h>
[23]31#include <vfs.h>
32#include <vmm.h>
[1]33#include <thread.h>
[23]34#include <printk.h>
[670]35#include <pipe.h>
[23]36#include <process.h>
[1]37
[506]38
[407]39extern uint32_t enter_sys_read;
[436]40extern uint32_t enter_devfs_read;
[407]41extern uint32_t enter_txt_read;
[436]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;
[407]50extern uint32_t exit_txt_read;
[436]51extern uint32_t exit_devfs_read;
[407]52extern uint32_t exit_sys_read;
53
54
[23]55/////////////////////////////////
56int sys_read( uint32_t   file_id,
[407]57              void     * vaddr,
[23]58              uint32_t   count )
[1]59{
[604]60    error_t       error;
61    vseg_t      * vseg;            // required for user space checking
[633]62    xptr_t        file_xp;         // remote file extended pointer
[604]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
[656]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
[604]69    uint32_t      nbytes;          // number of bytes actually read
70    reg_t         save_sr;         // required to enable IRQs during syscall
[407]71
[604]72        thread_t    * this             = CURRENT_THREAD;
73        process_t   * process          = this->process;
74    xptr_t        process_owner_xp = process->owner_xp;
[23]75 
[670]76#if DEBUG_SYS_READ || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS
[566]77uint64_t     tm_start = hal_get_cycles();
78#endif
79
[438]80#if DEBUG_SYS_READ
81if( DEBUG_SYS_READ < tm_start )
[610]82printk("\n[%s] thread[%x,%x] enter / vaddr %x / count %d / cycle %d\n",
[584]83__FUNCTION__, process->pid, this->trdid, vaddr, count, (uint32_t)tm_start );
[433]84#endif
85
[438]86#if (DEBUG_SYS_READ & 1)
[436]87enter_sys_read = (uint32_t)tm_start;
88#endif
89
[23]90    // check file_id argument
91        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
[1]92        {
[435]93
[438]94#if DEBUG_SYSCALLS_ERROR
[670]95if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
96printk("\n[ERROR] in %s : thread[%x,%x] / illegal file descriptor index %d\n",
[584]97__FUNCTION__ , process->pid, this->trdid, file_id );
[435]98#endif
[23]99                this->errno = EBADFD;
[1]100                return -1;
101        }
102
[23]103    // check user buffer in user space
[440]104    error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg );
[23]105
106    if ( error )
107    {
[435]108
[438]109#if DEBUG_SYSCALLS_ERROR
[670]110if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
111printk("\n[ERROR] in %s : thread[%x,%x] / user buffer unmapped %x\n",
[584]112__FUNCTION__ , process->pid, this->trdid, (intptr_t)vaddr );
[435]113#endif
[23]114                this->errno = EINVAL;
115                return -1;
116    }
117
118    // get extended pointer on remote file descriptor
[664]119    file_xp = process_fd_get_xptr_from_local( process , file_id );
[23]120
121    if( file_xp == XPTR_NULL )
122    {
[435]123
[438]124#if DEBUG_SYSCALLS_ERROR
[670]125if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
126printk("\n[ERROR] in %s : thread[%x,%x] / undefined fd_id %d\n",
[584]127__FUNCTION__, process->pid, this->trdid, file_id );
[435]128#endif
[23]129        this->errno = EBADFD;
130        return -1;
131    }
132
133    // get file descriptor cluster and local pointer
[604]134    file_ptr = GET_PTR( file_xp );
135    file_cxy = GET_CXY( file_xp );
[23]136
[656]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   ) );
[313]142
[604]143    // enable IRQs
144    hal_enable_irq( &save_sr );
145
[670]146    // check file readable
147    if( (file_attr & FD_ATTR_READ_ENABLE) == 0 )
[23]148    {
[435]149
[438]150#if DEBUG_SYSCALLS_ERROR
[670]151if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
152printk("\n[ERROR] in %s : thread[%x,%x] / file %d not readable\n",
[584]153__FUNCTION__, process->pid, this->trdid, file_id );
[435]154#endif
[670]155        hal_restore_irq( save_sr );
156        this->errno = EBADFD;
157        return -1;
158    }
[421]159
[670]160    // action depend on file type:
161    if( file_type == FILE_TYPE_REG )                           // read from mapper
162    {
[656]163        // try to move count bytes from mapper
[670]164        nbytes = vfs_user_move( true,   //  from mapper
[407]165                                file_xp,
166                                vaddr, 
167                                count );
168    }
[670]169    else if( file_type == FILE_TYPE_DEV )                      // read from TXT device
[407]170    {
[436]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
[446]176        volatile xptr_t    txt_owner_xp;   
177        uint32_t           iter = 0;
[436]178
179        while( 1 )
180        {
[446]181            // extended pointer on TXT owner process
[670]182            txt_owner_xp  = hal_remote_l64( XPTR( chdev_cxy , 
183                                                  &chdev_ptr->ext.txt.owner_xp ) );
[656]184            // wait for TXT_RX ownership
[446]185            if ( process_owner_xp != txt_owner_xp )
[436]186            {
[446]187                if( (iter & 0xFFF) == 0 )
[584]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() );
[446]190
[436]191                // deschedule without blocking
[446]192                sched_yield( "wait TXT_RX ownership" );
193
194                iter++;
[436]195            }
196            else
197            {
198                break;
199            }
200        }
201
[656]202        // try to move count bytes from TXT device
[670]203        nbytes = devfs_user_move( true,    // from device
[407]204                                  file_xp,
205                                  vaddr,
206                                  count );
207    }
[670]208    else if( (file_type == FILE_TYPE_PIPE) ||
209             (file_type == FILE_TYPE_FIFO) )                  // read from pipe
[407]210    {
[670]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    {
[594]219
220#if DEBUG_SYSCALLS_ERROR
[670]221if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
222printk("\n[ERROR] in %s : thread[%x,%x] / unsupported inode type %d\n",
[604]223__FUNCTION__, vfs_inode_type_str( file_type ) );
[594]224#endif
225                this->errno = EBADFD;
[604]226        hal_restore_irq( save_sr );
[594]227                return -1;
[407]228    }
229
[656]230    // check error
231    if( nbytes == 0xFFFFFFFF )
232    {
233
234#if DEBUG_SYSCALLS_ERROR
[670]235if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
[656]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
[408]244    // restore IRQs
[421]245    hal_restore_irq( save_sr );
[408]246
[124]247    hal_fence();
[23]248
[566]249#if (DEBUG_SYS_READ || CONFIG_INSTRUMENTATION_SYSCALLS)
250uint64_t     tm_end = hal_get_cycles();
251#endif
252
[438]253#if DEBUG_SYS_READ
254if( DEBUG_SYS_READ < tm_end )
[610]255printk("\n[%s] thread[%x,%x] exit / cycle %d\n",
[584]256__FUNCTION__ , process->pid, this->trdid, (uint32_t)tm_end );
[409]257#endif
[23]258
[566]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
[438]264#if (DEBUG_SYS_READ & 1)
[418]265exit_sys_read = (uint32_t)tm_end;
[407]266
[443]267printk("\n***** timing to read one character *****\n"
[435]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         );
[407]296#endif
297 
298        return nbytes;
299
[23]300}  // end sys_read()
Note: See TracBrowser for help on using the repository browser.