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

Last change on this file since 517 was 506, checked in by viala@…, 6 years ago

[syscalls] add interface in implementation.

Add const where possible, fix protoypes to follow interface.

File size: 8.6 KB
RevLine 
[1]1/*
[23]2 * sys_read.c - read bytes from a file
[1]3 *
[440]4 * Author     Alain Greiner (2016,2017,2018)
[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>
[23]26#include <hal_uspace.h>
[409]27#include <hal_irqmask.h>
[23]28#include <hal_special.h>
[1]29#include <errno.h>
[23]30#include <vfs.h>
31#include <vmm.h>
[1]32#include <thread.h>
[23]33#include <printk.h>
34#include <process.h>
[1]35
[506]36#include <syscalls.h>
37
[407]38// TODO: concurrent user page(s) munmap need to be handled [AG]
[23]39
[407]40extern uint32_t enter_sys_read;
[436]41extern uint32_t enter_devfs_read;
[407]42extern uint32_t enter_txt_read;
[436]43extern uint32_t enter_chdev_cmd_read;
44extern uint32_t enter_chdev_server_read;
45extern uint32_t enter_tty_cmd_read;
46extern uint32_t enter_tty_isr_read;
47extern uint32_t exit_tty_isr_read;
48extern uint32_t exit_tty_cmd_read;
49extern uint32_t exit_chdev_server_read;
50extern uint32_t exit_chdev_cmd_read;
[407]51extern uint32_t exit_txt_read;
[436]52extern uint32_t exit_devfs_read;
[407]53extern uint32_t exit_sys_read;
54
55
[23]56/////////////////////////////////
57int sys_read( uint32_t   file_id,
[407]58              void     * vaddr,
[23]59              uint32_t   count )
[1]60{
[23]61    error_t      error;
[440]62    vseg_t     * vseg;        // required for user space checking
[23]63        xptr_t       file_xp;     // remote file extended pointer
[407]64    uint32_t     nbytes;      // number of bytes actually read
[408]65    reg_t        save_sr;     // required to enable IRQs during syscall
[407]66
[446]67        thread_t  *  this             = CURRENT_THREAD;
68        process_t *  process          = this->process;
69    xptr_t       process_owner_xp = process->owner_xp;
[23]70 
[438]71#if DEBUG_SYS_READ
[433]72uint64_t     tm_start;
73uint64_t     tm_end;
74tm_start = hal_get_cycles();
[438]75if( DEBUG_SYS_READ < tm_start )
[459]76printk("\n[DBG] %s : thread %x in process %x enter / vaddr %x / count %d / cycle %d\n",
77__FUNCTION__, this->trdid, process->pid, vaddr, count, (uint32_t)tm_start );
[433]78#endif
79
[438]80#if (DEBUG_SYS_READ & 1)
[436]81enter_sys_read = (uint32_t)tm_start;
82#endif
83
[23]84    // check file_id argument
85        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
[1]86        {
[435]87
[438]88#if DEBUG_SYSCALLS_ERROR
[435]89printk("\n[ERROR] in %s : illegal file descriptor index = %d\n", __FUNCTION__ , file_id );
90#endif
[23]91                this->errno = EBADFD;
[1]92                return -1;
93        }
94
[23]95    // check user buffer in user space
[440]96    error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg );
[23]97
98    if ( error )
99    {
[435]100
[438]101#if DEBUG_SYSCALLS_ERROR
[440]102printk("\n[ERROR] in %s : user buffer unmapped %x / thread %x / process %x\n",
103__FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid );
104vmm_display( process , false );
[435]105#endif
[23]106                this->errno = EINVAL;
107                return -1;
108    }
109
[408]110    // enable IRQs
[421]111    hal_enable_irq( &save_sr );
[408]112
[23]113    // get extended pointer on remote file descriptor
114    file_xp = process_fd_get_xptr( process , file_id );
115
116    if( file_xp == XPTR_NULL )
117    {
[435]118
[438]119#if DEBUG_SYSCALLS_ERROR
[435]120printk("\n[ERROR] in %s : undefined fd_id %d in process %x\n",
121__FUNCTION__ , file_id , process->pid );
122#endif
[23]123        this->errno = EBADFD;
124        return -1;
125    }
126
127    // get file descriptor cluster and local pointer
[436]128    vfs_file_t * file_ptr = GET_PTR( file_xp );
[23]129    cxy_t        file_cxy = GET_CXY( file_xp );
130
131    // check file readable
132    uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) );
133    if( (attr & FD_ATTR_READ_ENABLE) == 0 )
[1]134        {
[435]135
[438]136#if DEBUG_SYSCALLS_ERROR
[435]137printk("\n[ERROR] in %s : file %d not readable in process %x\n",
138__FUNCTION__ , file_id , process->pid );
139#endif
[23]140                this->errno = EBADFD;
[1]141                return -1;
142        }
[313]143   
[407]144    // get file type
145    vfs_inode_type_t type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) );
[313]146
[407]147    // action depend on file type
[421]148    if( type == INODE_TYPE_FILE )      // check file readable & read from mapper
[23]149    {
[421]150        // check file readable
151        uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) );
[443]152
[421]153        if( (attr & FD_ATTR_READ_ENABLE) == 0 )
154            {
[435]155
[438]156#if DEBUG_SYSCALLS_ERROR
[435]157printk("\n[ERROR] in %s : file %d not readable in process %x\n",
158__FUNCTION__ , file_id , process->pid );
159#endif
[421]160                    this->errno = EBADFD;
161                    return -1;
162            }
163
164        // move count bytes from mapper
[407]165        nbytes = vfs_user_move( true,               // from mapper to buffer
166                                file_xp,
167                                vaddr, 
168                                count );
169    }
[446]170    else if( type == INODE_TYPE_DEV )  // check ownership & read from device
[407]171    {
[436]172        // get cluster and pointers on TXT_RX chdev
173        xptr_t    chdev_xp  = chdev_from_file( file_xp );
174        cxy_t     chdev_cxy = GET_CXY( chdev_xp );
175        chdev_t * chdev_ptr = GET_PTR( chdev_xp );
176
[446]177        volatile xptr_t    txt_owner_xp;   
178        uint32_t           iter = 0;
[436]179
180        while( 1 )
181        {
[446]182            // extended pointer on TXT owner process
183            txt_owner_xp  = hal_remote_lwd( XPTR( chdev_cxy , &chdev_ptr->ext.txt.owner_xp ) );
[436]184
185            // check TXT_RX ownership
[446]186            if ( process_owner_xp != txt_owner_xp )
[436]187            {
[446]188                if( (iter & 0xFFF) == 0 )
189                printk("\n[WARNING] in %s : thread %x in process %x wait TXT_RX / cycle %d\n",
190                __FUNCTION__, this->trdid, process->pid, (uint32_t)hal_get_cycles() );
191
[436]192                // deschedule without blocking
[446]193                sched_yield( "wait TXT_RX ownership" );
194
195                iter++;
[436]196            }
197            else
198            {
199                break;
200            }
201        }
202
[421]203        // move count bytes from device
[407]204        nbytes = devfs_user_move( true,             // from device to buffer
205                                  file_xp,
206                                  vaddr,
207                                  count );
[421]208
[407]209    }
210    else
211    {
212        nbytes = 0;
[492]213        assert( false , "file type %d non supported yet\n", type );
[407]214    }
215
216    if( nbytes != count )
217    {
[435]218
[438]219#if DEBUG_SYSCALLS_ERROR
[435]220printk("\n[ERROR] in %s cannot read data from file %d in process %x\n",
221__FUNCTION__ , file_id , process->pid );
222#endif
[313]223        this->errno = error;
224        return -1;
[23]225    }
226
[408]227    // restore IRQs
[421]228    hal_restore_irq( save_sr );
[408]229
[124]230    hal_fence();
[23]231
[438]232#if DEBUG_SYS_READ
[409]233tm_end = hal_get_cycles();
[438]234if( DEBUG_SYS_READ < tm_end )
[469]235printk("\n[DBG] %s : thread %x in process %x exit / cycle %d / cost %d\n",
236__FUNCTION__ , this->trdid, process->pid, (uint32_t)tm_start, (uint32_t)(tm_end - tm_start) );
[409]237#endif
[23]238
[438]239#if (DEBUG_SYS_READ & 1)
[418]240exit_sys_read = (uint32_t)tm_end;
[407]241
[443]242printk("\n***** timing to read one character *****\n"
[435]243" - enter_sys_read          = %d / delta %d\n"
244" - enter_devfs_read        = %d / delta %d\n"
245" - enter_txt_read          = %d / delta %d\n"
246" - enter_chdev_cmd_read    = %d / delta %d\n"
247" - enter_chdev_server_read = %d / delta %d\n"
248" - enter_tty_cmd_read      = %d / delta %d\n"
249" - enter_tty_isr_read      = %d / delta %d\n"
250" - exit_tty_isr_read       = %d / delta %d\n"
251" - exit_tty_cmd_read       = %d / delta %d\n"
252" - exit_chdev_server_read  = %d / delta %d\n"
253" - exit_chdev_cmd_read     = %d / delta %d\n"
254" - exit_txt_read           = %d / delta %d\n"
255" - exit_devfs_read         = %d / delta %d\n"
256" - exit_sys_read           = %d / delta %d\n",
257enter_sys_read          , 0 ,
258enter_devfs_read        , enter_devfs_read        - enter_sys_read          ,
259enter_txt_read          , enter_txt_read          - enter_devfs_read        ,
260enter_chdev_cmd_read    , enter_chdev_cmd_read    - enter_txt_read          ,
261enter_chdev_server_read , enter_chdev_server_read - enter_chdev_cmd_read    ,
262enter_tty_cmd_read      , enter_tty_cmd_read      - enter_chdev_server_read ,
263enter_tty_isr_read      , enter_tty_isr_read      - enter_tty_cmd_read      ,
264exit_tty_isr_read       , exit_tty_isr_read       - enter_tty_isr_read      ,
265exit_tty_cmd_read       , exit_tty_cmd_read       - exit_tty_isr_read       ,
266exit_chdev_server_read  , exit_chdev_server_read  - exit_tty_cmd_read       ,
267exit_chdev_cmd_read     , exit_chdev_cmd_read     - exit_chdev_server_read  ,
268exit_txt_read           , exit_txt_read           - exit_chdev_cmd_read     ,
269exit_devfs_read         , exit_devfs_read         - exit_txt_read           ,
270exit_sys_read           , exit_sys_read           - exit_devfs_read         );
[407]271#endif
272 
273        return nbytes;
274
[23]275}  // end sys_read()
Note: See TracBrowser for help on using the repository browser.