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

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

Fix several bugs in the fork() syscall.

File size: 6.5 KB
Line 
1/*
2 * sys_read.c - read bytes from a file
3 *
4 * Author     Alain Greiner (2016,2017)
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_types.h>
26#include <hal_uspace.h>
27#include <hal_special.h>
28#include <errno.h>
29#include <vfs.h>
30#include <vmm.h>
31#include <thread.h>
32#include <printk.h>
33#include <process.h>
34
35// TODO: concurrent user page(s) munmap need to be handled [AG]
36
37// instrumentation
38extern uint32_t enter_sys_read;
39extern uint32_t enter_devfs_move;
40extern uint32_t enter_txt_read;
41extern uint32_t enter_chdev_cmd;
42extern uint32_t enter_chdev_server;
43extern uint32_t enter_tty_cmd;
44extern uint32_t enter_tty_isr;
45extern uint32_t exit_tty_isr;
46extern uint32_t exit_tty_cmd;
47extern uint32_t exit_chdev_server;
48extern uint32_t exit_chdev_cmd;
49extern uint32_t exit_txt_read;
50extern uint32_t exit_devfs_move;
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    paddr_t      paddr;       // required for user space checking
61        xptr_t       file_xp;     // remote file extended pointer
62    uint32_t     nbytes;      // number of bytes actually read
63    reg_t        save_sr;     // required to enable IRQs during syscall
64        uint32_t     tm_start;
65        uint32_t     tm_end;
66
67        tm_start = hal_get_cycles();
68
69#if CONFIG_READ_START
70enter_sys_read = tm_start;
71#endif
72
73        thread_t  *  this    = CURRENT_THREAD;
74        process_t *  process = this->process;
75 
76    // check file_id argument
77        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
78        {
79        printk("\n[ERROR] in %s : illegal file descriptor index = %d\n",
80        __FUNCTION__ , file_id );
81                this->errno = EBADFD;
82                return -1;
83        }
84
85    // check user buffer in user space
86    error = vmm_v2p_translate( false , vaddr , &paddr );
87
88    if ( error )
89    {
90        printk("\n[ERROR] in %s : user buffer unmapped = %x\n",
91        __FUNCTION__ , (intptr_t)vaddr );
92                this->errno = EINVAL;
93                return -1;
94    }
95
96    // enable IRQs
97    hal_enable_irq( &save_sr );
98
99    // get extended pointer on remote file descriptor
100    file_xp = process_fd_get_xptr( process , file_id );
101
102    if( file_xp == XPTR_NULL )
103    {
104        printk("\n[ERROR] in %s : undefined file descriptor index = %d in process %x\n",
105        __FUNCTION__ , file_id , process->pid );
106        this->errno = EBADFD;
107        return -1;
108    }
109
110    // get file descriptor cluster and local pointer
111    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
112    cxy_t        file_cxy = GET_CXY( file_xp );
113
114    // check file readable
115    uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) );
116    if( (attr & FD_ATTR_READ_ENABLE) == 0 )
117        {
118        printk("\n[ERROR] in %s : file %d not readable in process %x\n",
119        __FUNCTION__ , file_id , process->pid );
120                this->errno = EBADFD;
121                return -1;
122        }
123   
124    // get file type
125    vfs_inode_type_t type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) );
126
127    // action depend on file type
128    if( type == INODE_TYPE_FILE )      // transfer count bytes from file mapper
129    {
130        nbytes = vfs_user_move( true,               // from mapper to buffer
131                                file_xp,
132                                vaddr, 
133                                count );
134    }
135    else if( type == INODE_TYPE_DEV )  // transfer count bytes from device
136    {
137        nbytes = devfs_user_move( true,             // from device to buffer
138                                  file_xp,
139                                  vaddr,
140                                  count );
141    }
142    else
143    {
144        nbytes = 0;
145        panic("file type %d non supported yet", type );
146    }
147
148    if( nbytes != count )
149    {
150        printk("\n[ERROR] in %s cannot read data from file %d in process %x\n",
151        __FUNCTION__ , file_id , process->pid );
152        this->errno = error;
153        return -1;
154    }
155
156    // restore IRQs
157    hal_restore_irq( save_sr );
158
159    hal_fence();
160
161    tm_end = hal_get_cycles();
162
163#if CONFIG_READ_DEBUG
164exit_sys_read = tm_end;
165
166printk("\n@@@@@@@@@@@@ timing ro read character %c\n"
167" - enter_sys_read     = %d / delta %d\n"
168" - enter_devfs_move   = %d / delta %d\n"
169" - enter_txt_read     = %d / delta %d\n"
170" - enter_chdev_cmd    = %d / delta %d\n"
171" - enter_chdev_server = %d / delta %d\n"
172" - enter_tty_cmd      = %d / delta %d\n"
173" - enter_tty_isr      = %d / delta %d\n"
174" - exit_tty_isr       = %d / delta %d\n"
175" - exit_tty_cmd       = %d / delta %d\n"
176" - exit_chdev_server  = %d / delta %d\n"
177" - exit_chdev_cmd     = %d / delta %d\n"
178" - exit_txt_read      = %d / delta %d\n"
179" - exit_devfs_move    = %d / delta %d\n"
180" - exit_sys_read      = %d / delta %d\n",
181*((char *)(intptr_t)paddr) ,
182enter_sys_read     , 0 ,
183enter_devfs_move   , enter_devfs_move   - enter_sys_read     ,
184enter_txt_read     , enter_txt_read     - enter_devfs_move   ,
185enter_chdev_cmd    , enter_chdev_cmd    - enter_txt_read     ,
186enter_chdev_server , enter_chdev_server - enter_chdev_cmd    ,
187enter_tty_cmd      , enter_tty_cmd      - enter_chdev_server ,
188enter_tty_isr      , enter_tty_isr      - enter_tty_cmd      ,
189exit_tty_isr       , exit_tty_isr       - enter_tty_isr      ,
190exit_tty_cmd       , exit_tty_cmd       - exit_tty_isr       ,
191exit_chdev_server  , exit_chdev_server  - exit_tty_cmd       ,
192exit_chdev_cmd     , exit_chdev_cmd     - exit_chdev_server  ,
193exit_txt_read      , exit_txt_read      - exit_chdev_cmd     ,
194exit_devfs_move    , exit_devfs_move    - exit_txt_read      ,
195exit_sys_read      , exit_sys_read      - exit_devfs_move    );
196#endif
197
198syscall_dmsg("\n[DBG] %s : core[%x,%d] / thread %x in process %x / cycle %d\n"
199"nbytes = %d / first byte = %c / file_id = %d / cost = %d\n",
200__FUNCTION__ , local_cxy , this->core->lid , this->trdid , this->process->pid ,
201tm_start , nbytes , *((char *)(intptr_t)paddr) , file_id , tm_end - tm_start );
202 
203        return nbytes;
204
205}  // end sys_read()
Note: See TracBrowser for help on using the repository browser.