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

Last change on this file since 446 was 446, checked in by alain, 6 years ago

miscelaneous...

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