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

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

blip

File size: 7.9 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_irqmask.h>
28#include <hal_special.h>
29#include <errno.h>
30#include <vfs.h>
31#include <vmm.h>
32#include <thread.h>
33#include <printk.h>
34#include <process.h>
35
36// TODO: concurrent user page(s) munmap need to be handled [AG]
37
38// TODO : remove these debug variables
39extern uint32_t enter_sys_read;
40extern uint32_t enter_devfs_move;
41extern uint32_t enter_txt_read;
42extern uint32_t enter_chdev_cmd;
43extern uint32_t enter_chdev_server;
44extern uint32_t enter_tty_cmd;
45extern uint32_t enter_tty_isr;
46extern uint32_t exit_tty_isr;
47extern uint32_t exit_tty_cmd;
48extern uint32_t exit_chdev_server;
49extern uint32_t exit_chdev_cmd;
50extern uint32_t exit_txt_read;
51extern uint32_t exit_devfs_move;
52extern uint32_t exit_sys_read;
53
54
55/////////////////////////////////
56int sys_read( uint32_t   file_id,
57              void     * vaddr,
58              uint32_t   count )
59{
60    error_t      error;
61    paddr_t      paddr;       // required for user space checking
62        xptr_t       file_xp;     // remote file extended pointer
63    uint32_t     nbytes;      // number of bytes actually read
64    reg_t        save_sr;     // required to enable IRQs during syscall
65
66#if CONFIG_READ_DEBUG
67enter_sys_read = (uint32_t)tm_start;
68#endif
69
70        thread_t  *  this    = CURRENT_THREAD;
71        process_t *  process = this->process;
72 
73#if CONFIG_DEBUG_SYS_READ
74uint64_t     tm_start;
75uint64_t     tm_end;
76tm_start = hal_get_cycles();
77if( CONFIG_DEBUG_SYS_READ < tm_start )
78printk("\n[DBG] %s : thread %d enter / process %x / vaddr = %x / count %d / cycle %d\n",
79__FUNCTION__, this, process->pid, vaddr, count, (uint32_t)tm_start );
80#endif
81
82    // check file_id argument
83        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
84        {
85        printk("\n[ERROR] in %s : illegal file descriptor index = %d\n",
86        __FUNCTION__ , file_id );
87                this->errno = EBADFD;
88                return -1;
89        }
90
91    // check user buffer in user space
92    error = vmm_v2p_translate( false , vaddr , &paddr );
93
94    if ( error )
95    {
96        printk("\n[ERROR] in %s : user buffer unmapped = %x\n",
97        __FUNCTION__ , (intptr_t)vaddr );
98                this->errno = EINVAL;
99                return -1;
100    }
101
102    // enable IRQs
103    hal_enable_irq( &save_sr );
104
105    // get extended pointer on remote file descriptor
106    file_xp = process_fd_get_xptr( process , file_id );
107
108    if( file_xp == XPTR_NULL )
109    {
110        printk("\n[ERROR] in %s : undefined file descriptor index = %d in process %x\n",
111        __FUNCTION__ , file_id , process->pid );
112        this->errno = EBADFD;
113        return -1;
114    }
115
116    // get file descriptor cluster and local pointer
117    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
118    cxy_t        file_cxy = GET_CXY( file_xp );
119
120    // check file readable
121    uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) );
122    if( (attr & FD_ATTR_READ_ENABLE) == 0 )
123        {
124        printk("\n[ERROR] in %s : file %d not readable in process %x\n",
125        __FUNCTION__ , file_id , process->pid );
126                this->errno = EBADFD;
127                return -1;
128        }
129   
130    // get file type
131    vfs_inode_type_t type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) );
132
133    // action depend on file type
134    if( type == INODE_TYPE_FILE )      // check file readable & read from mapper
135    {
136        // check file readable
137        uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) );
138        if( (attr & FD_ATTR_READ_ENABLE) == 0 )
139            {
140            printk("\n[ERROR] in %s : file %d not readable in process %x\n",
141            __FUNCTION__ , file_id , process->pid );
142                    this->errno = EBADFD;
143                    return -1;
144            }
145
146        // move count bytes from mapper
147        nbytes = vfs_user_move( true,               // from mapper to buffer
148                                file_xp,
149                                vaddr, 
150                                count );
151    }
152    else if( type == INODE_TYPE_DEV )  // check ownership & read from from device
153    {
154        // move count bytes from device
155        nbytes = devfs_user_move( true,             // from device to buffer
156                                  file_xp,
157                                  vaddr,
158                                  count );
159
160        // check ownership
161        xptr_t    chdev_xp  = chdev_from_file( file_xp );
162        cxy_t     chdev_cxy = GET_CXY( chdev_xp );
163        chdev_t * chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
164        xptr_t    owner_xp  = hal_remote_lwd( XPTR( chdev_cxy , &chdev_ptr->ext.txt.owner_xp ) );
165
166        if( XPTR( local_cxy , process ) != owner_xp )
167        {
168            printk("\n[ERROR] in %s : process %x not in foreground for TXT%d\n",
169            __FUNCTION__, process->pid, hal_remote_lw( XPTR(chdev_cxy,&chdev_ptr->channel) ) );
170                    this->errno = EBADFD;
171                    return -1;
172        }
173    }
174    else
175    {
176        nbytes = 0;
177        assert( false , __FUNCTION__ , "file type %d non supported yet\n", type );
178    }
179
180    if( nbytes != count )
181    {
182        printk("\n[ERROR] in %s cannot read data from file %d in process %x\n",
183        __FUNCTION__ , file_id , process->pid );
184        this->errno = error;
185        return -1;
186    }
187
188    // restore IRQs
189    hal_restore_irq( save_sr );
190
191    hal_fence();
192
193#if CONFIG_DEBUG_SYS_READ
194tm_end = hal_get_cycles();
195if( CONFIG_DEBUG_SYS_READ < tm_end )
196printk("\n[DBG] %s : thread %x / process %x / cycle %d\n"
197"nbytes = %d / first byte = %c / file_id = %d / cost = %d\n",
198__FUNCTION__ , local_cxy , this->core->lid , this->trdid , this->process->pid ,
199(uint32_t)tm_start , nbytes , *((char *)(intptr_t)paddr) , file_id ,
200(uint32_t)(tm_end - tm_start) );
201#endif
202
203#if (CONFIG_READ_DEBUG & 0x1)
204exit_sys_read = (uint32_t)tm_end;
205
206printk("\n@@@@@@@@@@@@ timing to read character %c\n"
207" - enter_sys_read     = %d / delta %d\n"
208" - enter_devfs_move   = %d / delta %d\n"
209" - enter_txt_read     = %d / delta %d\n"
210" - enter_chdev_cmd    = %d / delta %d\n"
211" - enter_chdev_server = %d / delta %d\n"
212" - enter_tty_cmd      = %d / delta %d\n"
213" - enter_tty_isr      = %d / delta %d\n"
214" - exit_tty_isr       = %d / delta %d\n"
215" - exit_tty_cmd       = %d / delta %d\n"
216" - exit_chdev_server  = %d / delta %d\n"
217" - exit_chdev_cmd     = %d / delta %d\n"
218" - exit_txt_read      = %d / delta %d\n"
219" - exit_devfs_move    = %d / delta %d\n"
220" - exit_sys_read      = %d / delta %d\n",
221*((char *)(intptr_t)paddr) ,
222enter_sys_read     , 0 ,
223enter_devfs_move   , enter_devfs_move   - enter_sys_read     ,
224enter_txt_read     , enter_txt_read     - enter_devfs_move   ,
225enter_chdev_cmd    , enter_chdev_cmd    - enter_txt_read     ,
226enter_chdev_server , enter_chdev_server - enter_chdev_cmd    ,
227enter_tty_cmd      , enter_tty_cmd      - enter_chdev_server ,
228enter_tty_isr      , enter_tty_isr      - enter_tty_cmd      ,
229exit_tty_isr       , exit_tty_isr       - enter_tty_isr      ,
230exit_tty_cmd       , exit_tty_cmd       - exit_tty_isr       ,
231exit_chdev_server  , exit_chdev_server  - exit_tty_cmd       ,
232exit_chdev_cmd     , exit_chdev_cmd     - exit_chdev_server  ,
233exit_txt_read      , exit_txt_read      - exit_chdev_cmd     ,
234exit_devfs_move    , exit_devfs_move    - exit_txt_read      ,
235exit_sys_read      , exit_sys_read      - exit_devfs_move    );
236#endif
237 
238        return nbytes;
239
240}  // end sys_read()
Note: See TracBrowser for help on using the repository browser.