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

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

Fix a bad bug in scheduler...

File size: 8.2 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
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
65#if (CONFIG_DEBUG_SYS_READ & 1)
66enter_sys_read = (uint32_t)tm_start;
67#endif
68
69        thread_t  *  this    = CURRENT_THREAD;
70        process_t *  process = this->process;
71 
72#if CONFIG_DEBUG_SYS_READ
73uint64_t     tm_start;
74uint64_t     tm_end;
75tm_start = hal_get_cycles();
76if( CONFIG_DEBUG_SYS_READ < tm_start )
77printk("\n[DBG] %s : thread %x enter / process %x / vaddr = %x / count %d / cycle %d\n",
78__FUNCTION__, this, process->pid, vaddr, count, (uint32_t)tm_start );
79#endif
80
81    // check file_id argument
82        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
83        {
84
85#if CONFIG_DEBUG_SYSCALLS_ERROR
86printk("\n[ERROR] in %s : illegal file descriptor index = %d\n", __FUNCTION__ , file_id );
87#endif
88                this->errno = EBADFD;
89                return -1;
90        }
91
92    // check user buffer in user space
93    error = vmm_v2p_translate( false , vaddr , &paddr );
94
95    if ( error )
96    {
97
98#if CONFIG_DEBUG_SYSCALLS_ERROR
99printk("\n[ERROR] in %s : user buffer unmapped = %x\n", 
100__FUNCTION__ , (intptr_t)vaddr );
101#endif
102                this->errno = EINVAL;
103                return -1;
104    }
105
106    // enable IRQs
107    hal_enable_irq( &save_sr );
108
109    // get extended pointer on remote file descriptor
110    file_xp = process_fd_get_xptr( process , file_id );
111
112    if( file_xp == XPTR_NULL )
113    {
114
115#if CONFIG_DEBUG_SYSCALLS_ERROR
116printk("\n[ERROR] in %s : undefined fd_id %d in process %x\n",
117__FUNCTION__ , file_id , process->pid );
118#endif
119        this->errno = EBADFD;
120        return -1;
121    }
122
123    // get file descriptor cluster and local pointer
124    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
125    cxy_t        file_cxy = GET_CXY( file_xp );
126
127    // check file readable
128    uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) );
129    if( (attr & FD_ATTR_READ_ENABLE) == 0 )
130        {
131
132#if CONFIG_DEBUG_SYSCALLS_ERROR
133printk("\n[ERROR] in %s : file %d not readable in process %x\n",
134__FUNCTION__ , file_id , process->pid );
135#endif
136                this->errno = EBADFD;
137                return -1;
138        }
139   
140    // get file type
141    vfs_inode_type_t type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) );
142
143    // action depend on file type
144    if( type == INODE_TYPE_FILE )      // check file readable & read from mapper
145    {
146        // check file readable
147        uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) );
148        if( (attr & FD_ATTR_READ_ENABLE) == 0 )
149            {
150
151#if CONFIG_DEBUG_SYSCALLS_ERROR
152printk("\n[ERROR] in %s : file %d not readable in process %x\n",
153__FUNCTION__ , file_id , process->pid );
154#endif
155                    this->errno = EBADFD;
156                    return -1;
157            }
158
159        // move count bytes from mapper
160        nbytes = vfs_user_move( true,               // from mapper to buffer
161                                file_xp,
162                                vaddr, 
163                                count );
164    }
165    else if( type == INODE_TYPE_DEV )  // check ownership & read from from device
166    {
167        // move count bytes from device
168        nbytes = devfs_user_move( true,             // from device to buffer
169                                  file_xp,
170                                  vaddr,
171                                  count );
172
173        // check ownership
174        xptr_t    chdev_xp  = chdev_from_file( file_xp );
175        cxy_t     chdev_cxy = GET_CXY( chdev_xp );
176        chdev_t * chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
177        xptr_t    owner_xp  = hal_remote_lwd( XPTR( chdev_cxy , &chdev_ptr->ext.txt.owner_xp ) );
178
179        if( XPTR( local_cxy , process ) != owner_xp )
180        {
181
182#if CONFIG_DEBUG_SYSCALLS_ERROR
183printk("\n[ERROR] in %s : process %x not in foreground for TXT%d\n",
184__FUNCTION__, process->pid, hal_remote_lw( XPTR(chdev_cxy,&chdev_ptr->channel) ) );
185#endif
186                    this->errno = EBADFD;
187                    return -1;
188        }
189    }
190    else
191    {
192        nbytes = 0;
193        assert( false , __FUNCTION__ , "file type %d non supported yet\n", type );
194    }
195
196    if( nbytes != count )
197    {
198
199#if CONFIG_DEBUG_SYSCALLS_ERROR
200printk("\n[ERROR] in %s cannot read data from file %d in process %x\n",
201__FUNCTION__ , file_id , process->pid );
202#endif
203        this->errno = error;
204        return -1;
205    }
206
207    // restore IRQs
208    hal_restore_irq( save_sr );
209
210    hal_fence();
211
212#if CONFIG_DEBUG_SYS_READ
213tm_end = hal_get_cycles();
214if( CONFIG_DEBUG_SYS_READ < tm_end )
215printk("\n[DBG] %s : thread %x exit / process %x / cycle %d\n"
216"nbytes = %d / first byte = %c / file_id = %d / cost = %d\n",
217__FUNCTION__ , local_cxy , this->core->lid , this->trdid , this->process->pid ,
218(uint32_t)tm_start , nbytes , *((char *)(intptr_t)paddr) , file_id ,
219(uint32_t)(tm_end - tm_start) );
220#endif
221
222#if (CONFIG_DEBUG_SYS_READ & 1)
223exit_sys_read = (uint32_t)tm_end;
224
225printk("\n@@@@@@@@@@@@ timing to read character %c\n"
226" - enter_sys_read          = %d / delta %d\n"
227" - enter_devfs_read        = %d / delta %d\n"
228" - enter_txt_read          = %d / delta %d\n"
229" - enter_chdev_cmd_read    = %d / delta %d\n"
230" - enter_chdev_server_read = %d / delta %d\n"
231" - enter_tty_cmd_read      = %d / delta %d\n"
232" - enter_tty_isr_read      = %d / delta %d\n"
233" - exit_tty_isr_read       = %d / delta %d\n"
234" - exit_tty_cmd_read       = %d / delta %d\n"
235" - exit_chdev_server_read  = %d / delta %d\n"
236" - exit_chdev_cmd_read     = %d / delta %d\n"
237" - exit_txt_read           = %d / delta %d\n"
238" - exit_devfs_read         = %d / delta %d\n"
239" - exit_sys_read           = %d / delta %d\n",
240*((char *)(intptr_t)paddr) ,
241enter_sys_read          , 0 ,
242enter_devfs_read        , enter_devfs_read        - enter_sys_read          ,
243enter_txt_read          , enter_txt_read          - enter_devfs_read        ,
244enter_chdev_cmd_read    , enter_chdev_cmd_read    - enter_txt_read          ,
245enter_chdev_server_read , enter_chdev_server_read - enter_chdev_cmd_read    ,
246enter_tty_cmd_read      , enter_tty_cmd_read      - enter_chdev_server_read ,
247enter_tty_isr_read      , enter_tty_isr_read      - enter_tty_cmd_read      ,
248exit_tty_isr_read       , exit_tty_isr_read       - enter_tty_isr_read      ,
249exit_tty_cmd_read       , exit_tty_cmd_read       - exit_tty_isr_read       ,
250exit_chdev_server_read  , exit_chdev_server_read  - exit_tty_cmd_read       ,
251exit_chdev_cmd_read     , exit_chdev_cmd_read     - exit_chdev_server_read  ,
252exit_txt_read           , exit_txt_read           - exit_chdev_cmd_read     ,
253exit_devfs_read         , exit_devfs_read         - exit_txt_read           ,
254exit_sys_read           , exit_sys_read           - exit_devfs_read         );
255#endif
256 
257        return nbytes;
258
259}  // end sys_read()
Note: See TracBrowser for help on using the repository browser.