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

Last change on this file since 665 was 664, checked in by alain, 4 years ago
  • Introduce the sys_socket.c file implementing all socket related syscalls.
  • Improve the non-standard sys_get_config() function.
File size: 9.3 KB
Line 
1/*
2 * sys_read.c - Kernel function implementing the "read" system call.
3 *
4 * Author     Alain Greiner (2016,2017,2018,2019)
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_kernel_types.h>
26#include <hal_vmm.h>
27#include <hal_uspace.h>
28#include <hal_irqmask.h>
29#include <hal_special.h>
30#include <errno.h>
31#include <vfs.h>
32#include <vmm.h>
33#include <thread.h>
34#include <printk.h>
35#include <process.h>
36
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    vseg_t      * vseg;            // required for user space checking
61    xptr_t        file_xp;         // remote file extended pointer
62    vfs_file_t  * file_ptr;        // remote file local pointer
63    cxy_t         file_cxy;        // remote file cluster identifier
64    uint32_t      file_type;       // file type
65    uint32_t      file_offset;     // file offset
66    uint32_t      file_attr;       // file attributes
67    vfs_inode_t * inode_ptr;       // local pointer on file inode
68    uint32_t      nbytes;          // number of bytes actually read
69    reg_t         save_sr;         // required to enable IRQs during syscall
70
71        thread_t    * this             = CURRENT_THREAD;
72        process_t   * process          = this->process;
73    xptr_t        process_owner_xp = process->owner_xp;
74 
75#if (DEBUG_SYS_READ || CONFIG_INSTRUMENTATION_SYSCALLS)
76uint64_t     tm_start = hal_get_cycles();
77#endif
78
79#if DEBUG_SYS_READ
80if( DEBUG_SYS_READ < tm_start )
81printk("\n[%s] thread[%x,%x] enter / vaddr %x / count %d / cycle %d\n",
82__FUNCTION__, process->pid, this->trdid, vaddr, count, (uint32_t)tm_start );
83#endif
84
85#if (DEBUG_SYS_READ & 1)
86enter_sys_read = (uint32_t)tm_start;
87#endif
88
89    // check file_id argument
90        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
91        {
92
93#if DEBUG_SYSCALLS_ERROR
94printk("\n[ERROR] in %s : thread[%x,%x] illegal file descriptor index %d\n",
95__FUNCTION__ , process->pid, this->trdid, file_id );
96#endif
97                this->errno = EBADFD;
98                return -1;
99        }
100
101    // check user buffer in user space
102    error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg );
103
104    if ( error )
105    {
106
107#if DEBUG_SYSCALLS_ERROR
108printk("\n[ERROR] in %s : thread[%x,%x] user buffer unmapped %x\n",
109__FUNCTION__ , process->pid, this->trdid, (intptr_t)vaddr );
110#endif
111                this->errno = EINVAL;
112                return -1;
113    }
114
115    // get extended pointer on remote file descriptor
116    file_xp = process_fd_get_xptr_from_local( process , file_id );
117
118    if( file_xp == XPTR_NULL )
119    {
120
121#if DEBUG_SYSCALLS_ERROR
122printk("\n[ERROR] in %s : thread[%x,%x] undefined fd_id %d\n",
123__FUNCTION__, process->pid, this->trdid, file_id );
124#endif
125        this->errno = EBADFD;
126        return -1;
127    }
128
129    // get file descriptor cluster and local pointer
130    file_ptr = GET_PTR( file_xp );
131    file_cxy = GET_CXY( file_xp );
132
133    // get inode, file type, offset and attributes
134    inode_ptr   = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode  ) );
135    file_type   = hal_remote_l32( XPTR( file_cxy , &file_ptr->type   ) );
136    file_offset = hal_remote_l32( XPTR( file_cxy , &file_ptr->offset ) );
137    file_attr   = hal_remote_l32( XPTR( file_cxy , &file_ptr->attr   ) );
138
139    // enable IRQs
140    hal_enable_irq( &save_sr );
141
142    // action depend on file type:
143
144    if( file_type == INODE_TYPE_FILE )      // read from file mapper
145    {
146        // check file readable
147        if( (file_attr & FD_ATTR_READ_ENABLE) == 0 )
148            {
149
150#if DEBUG_SYSCALLS_ERROR
151printk("\n[ERROR] in %s : thread[%x,%x] file %d not readable\n",
152__FUNCTION__, process->pid, this->trdid, file_id );
153#endif
154            hal_restore_irq( save_sr );
155                    this->errno = EBADFD;
156                    return -1;
157            }
158
159        // try to 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( file_type == INODE_TYPE_DEV )  // read from TXT 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    txt_owner_xp;   
173        uint32_t           iter = 0;
174
175        while( 1 )
176        {
177            // extended pointer on TXT owner process
178            txt_owner_xp  = hal_remote_l64( XPTR( chdev_cxy , &chdev_ptr->ext.txt.owner_xp ) );
179
180            // wait for TXT_RX ownership
181            if ( process_owner_xp != txt_owner_xp )
182            {
183                if( (iter & 0xFFF) == 0 )
184                printk("\n[WARNING] in %s : thread[%x,%x] wait TXT_RX / cycle %d\n",
185                __FUNCTION__, process->pid, this->trdid, (uint32_t)hal_get_cycles() );
186
187                // deschedule without blocking
188                sched_yield( "wait TXT_RX ownership" );
189
190                iter++;
191            }
192            else
193            {
194                break;
195            }
196        }
197
198        // try to move count bytes from TXT device
199        nbytes = devfs_user_move( true,             // from device to buffer
200                                  file_xp,
201                                  vaddr,
202                                  count );
203    }
204    else    // not FILE and not DEV
205    {
206
207#if DEBUG_SYSCALLS_ERROR
208printk("\n[ERROR] in %s : thread[%x,%x] / illegal inode type %\n",
209__FUNCTION__, vfs_inode_type_str( file_type ) );
210#endif
211                this->errno = EBADFD;
212        hal_restore_irq( save_sr );
213                return -1;
214    }
215
216    // check error
217    if( nbytes == 0xFFFFFFFF )
218    {
219
220#if DEBUG_SYSCALLS_ERROR
221printk("\n[ERROR] in %s : thread[%x,‰x] cannot read data from file %d\n",
222__FUNCTION__, process->pid, this->trdid, file_id );
223#endif
224        this->errno = EIO;
225        hal_restore_irq( save_sr );
226        return -1;
227    }
228
229    // restore IRQs
230    hal_restore_irq( save_sr );
231
232    hal_fence();
233
234#if (DEBUG_SYS_READ || CONFIG_INSTRUMENTATION_SYSCALLS)
235uint64_t     tm_end = hal_get_cycles();
236#endif
237
238#if DEBUG_SYS_READ
239if( DEBUG_SYS_READ < tm_end )
240printk("\n[%s] thread[%x,%x] exit / cycle %d\n",
241__FUNCTION__ , process->pid, this->trdid, (uint32_t)tm_end );
242#endif
243
244#if CONFIG_INSTRUMENTATION_SYSCALLS
245hal_atomic_add( &syscalls_cumul_cost[SYS_READ] , tm_end - tm_start );
246hal_atomic_add( &syscalls_occurences[SYS_READ] , 1 );
247#endif
248
249#if (DEBUG_SYS_READ & 1)
250exit_sys_read = (uint32_t)tm_end;
251
252printk("\n***** timing to read one character *****\n"
253" - enter_sys_read          = %d / delta %d\n"
254" - enter_devfs_read        = %d / delta %d\n"
255" - enter_txt_read          = %d / delta %d\n"
256" - enter_chdev_cmd_read    = %d / delta %d\n"
257" - enter_chdev_server_read = %d / delta %d\n"
258" - enter_tty_cmd_read      = %d / delta %d\n"
259" - enter_tty_isr_read      = %d / delta %d\n"
260" - exit_tty_isr_read       = %d / delta %d\n"
261" - exit_tty_cmd_read       = %d / delta %d\n"
262" - exit_chdev_server_read  = %d / delta %d\n"
263" - exit_chdev_cmd_read     = %d / delta %d\n"
264" - exit_txt_read           = %d / delta %d\n"
265" - exit_devfs_read         = %d / delta %d\n"
266" - exit_sys_read           = %d / delta %d\n",
267enter_sys_read          , 0 ,
268enter_devfs_read        , enter_devfs_read        - enter_sys_read          ,
269enter_txt_read          , enter_txt_read          - enter_devfs_read        ,
270enter_chdev_cmd_read    , enter_chdev_cmd_read    - enter_txt_read          ,
271enter_chdev_server_read , enter_chdev_server_read - enter_chdev_cmd_read    ,
272enter_tty_cmd_read      , enter_tty_cmd_read      - enter_chdev_server_read ,
273enter_tty_isr_read      , enter_tty_isr_read      - enter_tty_cmd_read      ,
274exit_tty_isr_read       , exit_tty_isr_read       - enter_tty_isr_read      ,
275exit_tty_cmd_read       , exit_tty_cmd_read       - exit_tty_isr_read       ,
276exit_chdev_server_read  , exit_chdev_server_read  - exit_tty_cmd_read       ,
277exit_chdev_cmd_read     , exit_chdev_cmd_read     - exit_chdev_server_read  ,
278exit_txt_read           , exit_txt_read           - exit_chdev_cmd_read     ,
279exit_devfs_read         , exit_devfs_read         - exit_txt_read           ,
280exit_sys_read           , exit_sys_read           - exit_devfs_read         );
281#endif
282 
283        return nbytes;
284
285}  // end sys_read()
Note: See TracBrowser for help on using the repository browser.