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

Last change on this file since 634 was 633, checked in by alain, 5 years ago

cosmetic

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