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

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

First implementation of fork/exec.

File size: 6.3 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
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    // get extended pointer on remote file descriptor
97    file_xp = process_fd_get_xptr( process , file_id );
98
99    if( file_xp == XPTR_NULL )
100    {
101        printk("\n[ERROR] in %s : undefined file descriptor index = %d in process %x\n",
102        __FUNCTION__ , file_id , process->pid );
103        this->errno = EBADFD;
104        return -1;
105    }
106
107    // get file descriptor cluster and local pointer
108    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
109    cxy_t        file_cxy = GET_CXY( file_xp );
110
111    // check file readable
112    uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) );
113    if( (attr & FD_ATTR_READ_ENABLE) == 0 )
114        {
115        printk("\n[ERROR] in %s : file %d not readable in process %x\n",
116        __FUNCTION__ , file_id , process->pid );
117                this->errno = EBADFD;
118                return -1;
119        }
120   
121    // get file type
122    vfs_inode_type_t type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) );
123
124    // action depend on file type
125    if( type == INODE_TYPE_FILE )      // transfer count bytes from file mapper
126    {
127        nbytes = vfs_user_move( true,               // from mapper to buffer
128                                file_xp,
129                                vaddr, 
130                                count );
131    }
132    else if( type == INODE_TYPE_DEV )  // transfer count bytes from device
133    {
134        nbytes = devfs_user_move( true,             // from device to buffer
135                                  file_xp,
136                                  vaddr,
137                                  count );
138    }
139    else
140    {
141        nbytes = 0;
142        panic("file type %d non supported yet", type );
143    }
144
145    if( nbytes != count )
146    {
147        printk("\n[ERROR] in %s cannot read data from file %d in process %x\n",
148        __FUNCTION__ , file_id , process->pid );
149        this->errno = error;
150        return -1;
151    }
152
153    hal_fence();
154
155    tm_end = hal_get_cycles();
156
157#if CONFIG_READ_DEBUG
158exit_sys_read = tm_end;
159
160printk("\n@@@@@@@@@@@@ timing ro read character %c\n"
161" - enter_sys_read     = %d / delta %d\n"
162" - enter_devfs_move   = %d / delta %d\n"
163" - enter_txt_read     = %d / delta %d\n"
164" - enter_chdev_cmd    = %d / delta %d\n"
165" - enter_chdev_server = %d / delta %d\n"
166" - enter_tty_cmd      = %d / delta %d\n"
167" - enter_tty_isr      = %d / delta %d\n"
168" - exit_tty_isr       = %d / delta %d\n"
169" - exit_tty_cmd       = %d / delta %d\n"
170" - exit_chdev_server  = %d / delta %d\n"
171" - exit_chdev_cmd     = %d / delta %d\n"
172" - exit_txt_read      = %d / delta %d\n"
173" - exit_devfs_move    = %d / delta %d\n"
174" - exit_sys_read      = %d / delta %d\n",
175*((char *)(intptr_t)paddr) ,
176enter_sys_read     , 0 ,
177enter_devfs_move   , enter_devfs_move   - enter_sys_read     ,
178enter_txt_read     , enter_txt_read     - enter_devfs_move   ,
179enter_chdev_cmd    , enter_chdev_cmd    - enter_txt_read     ,
180enter_chdev_server , enter_chdev_server - enter_chdev_cmd    ,
181enter_tty_cmd      , enter_tty_cmd      - enter_chdev_server ,
182enter_tty_isr      , enter_tty_isr      - enter_tty_cmd      ,
183exit_tty_isr       , exit_tty_isr       - enter_tty_isr      ,
184exit_tty_cmd       , exit_tty_cmd       - exit_tty_isr       ,
185exit_chdev_server  , exit_chdev_server  - exit_tty_cmd       ,
186exit_chdev_cmd     , exit_chdev_cmd     - exit_chdev_server  ,
187exit_txt_read      , exit_txt_read      - exit_chdev_cmd     ,
188exit_devfs_move    , exit_devfs_move    - exit_txt_read      ,
189exit_sys_read      , exit_sys_read      - exit_devfs_move    );
190#endif
191
192syscall_dmsg("\n[DBG] %s : core[%x,%d] / thread %x / nbytes = %d / cycle %d\n"
193" first byte = %c / file_id = %d / cost = %d\n",
194__FUNCTION__ , local_cxy , this->core->lid , this->trdid , nbytes , tm_start ,
195*((char *)(intptr_t)paddr) , file_id , tm_end - tm_start );
196 
197        return nbytes;
198
199}  // end sys_read()
Note: See TracBrowser for help on using the repository browser.