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

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

Fix a bug in scheduler related to RPC blocking.

File size: 8.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_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_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    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        thread_t  *  this    = CURRENT_THREAD;
66        process_t *  process = this->process;
67 
68#if DEBUG_SYS_READ
69uint64_t     tm_start;
70uint64_t     tm_end;
71tm_start = hal_get_cycles();
72if( DEBUG_SYS_READ < tm_start )
73printk("\n[DBG] %s : thread %x enter / process %x / vaddr = %x / count %d / cycle %d\n",
74__FUNCTION__, this, process->pid, vaddr, count, (uint32_t)tm_start );
75#endif
76
77#if (DEBUG_SYS_READ & 1)
78enter_sys_read = (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 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 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 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 = 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 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 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        // get cluster and pointers on TXT_RX chdev
168        xptr_t    chdev_xp  = chdev_from_file( file_xp );
169        cxy_t     chdev_cxy = GET_CXY( chdev_xp );
170        chdev_t * chdev_ptr = GET_PTR( chdev_xp );
171
172        volatile xptr_t    owner_xp;   
173
174        while( 1 )
175        {
176            // extended pointer on owner process
177            owner_xp  = hal_remote_lwd( XPTR( chdev_cxy , &chdev_ptr->ext.txt.owner_xp ) );
178
179            // check TXT_RX ownership
180            if ( XPTR( local_cxy , process ) != owner_xp )
181            {
182                // deschedule without blocking
183                sched_yield( "wait TXT ownership" );
184            }
185            else
186            {
187                break;
188            }
189        }
190
191        // move count bytes from device
192        nbytes = devfs_user_move( true,             // from device to buffer
193                                  file_xp,
194                                  vaddr,
195                                  count );
196
197    }
198    else
199    {
200        nbytes = 0;
201        assert( false , __FUNCTION__ , "file type %d non supported yet\n", type );
202    }
203
204    if( nbytes != count )
205    {
206
207#if DEBUG_SYSCALLS_ERROR
208printk("\n[ERROR] in %s cannot read data from file %d in process %x\n",
209__FUNCTION__ , file_id , process->pid );
210#endif
211        this->errno = error;
212        return -1;
213    }
214
215    // restore IRQs
216    hal_restore_irq( save_sr );
217
218    hal_fence();
219
220#if DEBUG_SYS_READ
221tm_end = hal_get_cycles();
222if( DEBUG_SYS_READ < tm_end )
223printk("\n[DBG] %s : thread %x exit / process %x / cycle %d\n"
224"nbytes = %d / first byte = %c / file_id = %d / cost = %d\n",
225__FUNCTION__ , this, process->pid,
226(uint32_t)tm_start , nbytes , *((char *)(intptr_t)paddr) , file_id ,
227(uint32_t)(tm_end - tm_start) );
228#endif
229
230#if (DEBUG_SYS_READ & 1)
231exit_sys_read = (uint32_t)tm_end;
232
233printk("\n@@@@@@@@@@@@ timing to read character %c\n"
234" - enter_sys_read          = %d / delta %d\n"
235" - enter_devfs_read        = %d / delta %d\n"
236" - enter_txt_read          = %d / delta %d\n"
237" - enter_chdev_cmd_read    = %d / delta %d\n"
238" - enter_chdev_server_read = %d / delta %d\n"
239" - enter_tty_cmd_read      = %d / delta %d\n"
240" - enter_tty_isr_read      = %d / delta %d\n"
241" - exit_tty_isr_read       = %d / delta %d\n"
242" - exit_tty_cmd_read       = %d / delta %d\n"
243" - exit_chdev_server_read  = %d / delta %d\n"
244" - exit_chdev_cmd_read     = %d / delta %d\n"
245" - exit_txt_read           = %d / delta %d\n"
246" - exit_devfs_read         = %d / delta %d\n"
247" - exit_sys_read           = %d / delta %d\n",
248*((char *)(intptr_t)paddr) ,
249enter_sys_read          , 0 ,
250enter_devfs_read        , enter_devfs_read        - enter_sys_read          ,
251enter_txt_read          , enter_txt_read          - enter_devfs_read        ,
252enter_chdev_cmd_read    , enter_chdev_cmd_read    - enter_txt_read          ,
253enter_chdev_server_read , enter_chdev_server_read - enter_chdev_cmd_read    ,
254enter_tty_cmd_read      , enter_tty_cmd_read      - enter_chdev_server_read ,
255enter_tty_isr_read      , enter_tty_isr_read      - enter_tty_cmd_read      ,
256exit_tty_isr_read       , exit_tty_isr_read       - enter_tty_isr_read      ,
257exit_tty_cmd_read       , exit_tty_cmd_read       - exit_tty_isr_read       ,
258exit_chdev_server_read  , exit_chdev_server_read  - exit_tty_cmd_read       ,
259exit_chdev_cmd_read     , exit_chdev_cmd_read     - exit_chdev_server_read  ,
260exit_txt_read           , exit_txt_read           - exit_chdev_cmd_read     ,
261exit_devfs_read         , exit_devfs_read         - exit_txt_read           ,
262exit_sys_read           , exit_sys_read           - exit_devfs_read         );
263#endif
264 
265        return nbytes;
266
267}  // end sys_read()
Note: See TracBrowser for help on using the repository browser.