source: trunk/kernel/syscalls/sys_write.c @ 656

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

Fix several bugs in the FATFS and in the VFS,
related to the creation of big files requiring
more than 4 Kbytes (one cluster) on device.

File size: 8.3 KB
RevLine 
[1]1/*
[566]2 * sys_write.c - Kernel function implementing the "write" system call.
[1]3 *
[625]4 * Author        Alain Greiner (2016,2017,2018,2019)
[1]5 *
[23]6 * Copyright (c) UPMC Sorbonne Universites
[1]7 *
[23]8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
[1]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 *
[23]14 * ALMOS-MKH is distributed in the hope that it will be useful, but
[1]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
[23]20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
[1]21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
[23]24#include <kernel_config.h>
[457]25#include <hal_kernel_types.h>
[624]26#include <hal_vmm.h>
[23]27#include <hal_uspace.h>
[409]28#include <hal_irqmask.h>
[23]29#include <hal_special.h>
[1]30#include <errno.h>
31#include <vfs.h>
[566]32#include <vmm.h>
[1]33#include <thread.h>
[23]34#include <printk.h>
35#include <process.h>
[1]36
[23]37
[440]38extern uint32_t enter_sys_write;
39extern uint32_t enter_devfs_write;
40extern uint32_t enter_txt_write;
41extern uint32_t enter_chdev_cmd_write;
42extern uint32_t enter_chdev_server_write;
43extern uint32_t enter_tty_cmd_write;
44extern uint32_t enter_tty_isr_write;
45extern uint32_t exit_tty_isr_write;
46extern uint32_t exit_tty_cmd_write;
47extern uint32_t exit_chdev_server_write;
48extern uint32_t exit_chdev_cmd_write;
49extern uint32_t exit_txt_write;
50extern uint32_t exit_devfs_write;
51extern uint32_t exit_sys_write;
52
[23]53//////////////////////////////////
54int sys_write( uint32_t   file_id,
[407]55               void     * vaddr,
[23]56               uint32_t   count )
[1]57{
[604]58    error_t       error;
59    vseg_t      * vseg;            // required for user space checking
60        xptr_t        file_xp;         // remote file extended pointer
61    vfs_file_t  * file_ptr;        // remote file local pointer
62    cxy_t         file_cxy;        // remote file cluster identifier
63    uint32_t      file_type;       // file type
[656]64    uint32_t      file_offset;     // file offset
65    uint32_t      file_attr;       // file attributes
[604]66    vfs_inode_t * inode_ptr;       // local pointer on associated inode
67    uint32_t      nbytes;          // number of bytes actually written
68    reg_t         save_sr;         // required to enable IRQs during syscall
[407]69
[604]70        thread_t    * this = CURRENT_THREAD;
71        process_t   * process = this->process;
[433]72
[566]73#if (DEBUG_SYS_WRITE || CONFIG_INSTRUMENTATION_SYSCALLS)
74uint64_t     tm_start = hal_get_cycles();
75#endif
76
[438]77#if DEBUG_SYS_WRITE
78if( DEBUG_SYS_WRITE < tm_start )
[625]79printk("\n[%s] thread[%x,%x] enter / vaddr %x / %d bytes / cycle %d\n",
[584]80__FUNCTION__, process->pid, this->trdid, vaddr, count, (uint32_t)tm_start );
[409]81#endif
[1]82 
[469]83#if (DEBUG_SYS_WRITE & 1)
84enter_sys_write = (uint32_t)tm_start;
85#endif
86
[23]87    // check file_id argument
88        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
[1]89        {
[433]90
[438]91#if DEBUG_SYSCALLS_ERROR
[584]92printk("\n[ERROR] in %s : thread[%x,%x] illegal file descriptor index %d\n",
93__FUNCTION__, process->pid, this->trdid, file_id );
[433]94#endif
[23]95        this->errno = EBADFD;
[1]96                return -1;
97        }
98
[23]99    // check user buffer in user space
[440]100    error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg );
[23]101
102    if ( error )
103    {
[433]104
[438]105#if DEBUG_SYSCALLS_ERROR
[584]106printk("\n[ERROR] in %s : thread[%x,%x] user buffer unmapped %x\n",
107__FUNCTION__ , process->pid, this->trdid, (intptr_t)vaddr );
[433]108#endif
[23]109                this->errno = EINVAL;
110                return -1;
111    }
112
113    // get extended pointer on remote file descriptor
114    file_xp = process_fd_get_xptr( process , file_id );
115
116    if( file_xp == XPTR_NULL )
117    {
[433]118
[438]119#if DEBUG_SYSCALLS_ERROR
[604]120printk("\n[ERROR] in %s : thread[%x,%x] undefined file descriptor = %d\n",
[584]121__FUNCTION__, process->pid, this->trdid, file_id );
[433]122#endif
[23]123                this->errno = EBADFD;
124                return -1;
125    }
126
127    // get file descriptor cluster and local pointer
[604]128    file_ptr = GET_PTR( file_xp );
129    file_cxy = GET_CXY( file_xp );
[23]130
[604]131    // get file type, offset, aatributes, and associated inode
132    file_type   = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) );
133    file_offset = hal_remote_l32( XPTR( file_cxy , &file_ptr->offset ) );
134    inode_ptr   = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) );
135    file_attr   = hal_remote_l32( XPTR( file_cxy , &file_ptr->attr ) );
[313]136
[443]137    // enable IRQs
138    hal_enable_irq( &save_sr );
139
[656]140    // action depend on file type:
141
[625]142    if( file_type == INODE_TYPE_FILE )  // write to a file mapper
[23]143    {
[421]144        // check file writable
[604]145        if( (file_attr & FD_ATTR_WRITE_ENABLE) == 0 )
[421]146            {
[433]147
[438]148#if DEBUG_SYSCALLS_ERROR
[584]149printk("\n[ERROR] in %s : thread[%x,%x] file %d not writable\n",
150__FUNCTION__ , process->pid, this->trdid, file_id );
[433]151#endif
[604]152            hal_restore_irq( save_sr );
[421]153                    this->errno = EBADFD;
154                    return -1;
155            }
156
157        // move count bytes to mapper
[407]158        nbytes = vfs_user_move( false,               // from buffer to mapper
159                                file_xp,
160                                vaddr, 
161                                count );
162    }
[604]163    else if( file_type == INODE_TYPE_DEV )  // write to TXT device
[407]164    {
[421]165        // move count bytes to device
[407]166        nbytes = devfs_user_move( false,             // from buffer to device
[604]167                                  file_xp,
168                                  vaddr,
169                                  count );
[407]170    }
[443]171    else  // not FILE and not DEV
[407]172    {
[594]173
174#if DEBUG_SYSCALLS_ERROR
175printk("\n[ERROR] in %s : thread[%x,%x] / illegal inode type %\n",
[604]176__FUNCTION__, vfs_inode_type_str( file_type ) );
[594]177#endif
[604]178        hal_restore_irq( save_sr );
[594]179                this->errno = EBADFD;
180                return -1;
[407]181    }
182
[656]183    // chek error
184    if( nbytes == 0xFFFFFFFF )
185    {
186
187#if DEBUG_SYSCALLS_ERROR
188printk("\n[ERROR] in %s : thread[%x,‰x] cannot write data to file %d\n",
189__FUNCTION__ , process->pid, this->trdid, file_id );
190#endif
191        hal_restore_irq( save_sr );
192        this->errno = EIO;
193        return -1;
194    }
195
[408]196    // restore IRQs
197    hal_restore_irq( save_sr );
198
[124]199    hal_fence();
[23]200
[566]201#if (DEBUG_SYS_WRITE || CONFIG_INSTRUMENTATION_SYSCALLS)
202uint64_t     tm_end = hal_get_cycles();
203#endif
204
[438]205#if DEBUG_SYS_WRITE
206if( DEBUG_SYS_WRITE < tm_end )
[610]207printk("\n[%s] thread[%x,%x] exit / cycle %d\n",
[584]208__FUNCTION__, process->pid, this->trdid, (uint32_t)tm_end );
[409]209#endif
[407]210 
[566]211#if CONFIG_INSTRUMENTATION_SYSCALLS
212hal_atomic_add( &syscalls_cumul_cost[SYS_WRITE] , tm_end - tm_start );
213hal_atomic_add( &syscalls_occurences[SYS_WRITE] , 1 );
214#endif
215
[438]216#if (DEBUG_SYS_WRITE & 1)
[435]217exit_sys_write = (uint32_t)tm_end;
218
[443]219printk("\n***** timing to write a string *****\n"
[435]220" - enter_sys_write          = %d / delta %d\n"
221" - enter_devfs_write        = %d / delta %d\n"
222" - enter_txt_write          = %d / delta %d\n"
223" - enter_chdev_cmd_write    = %d / delta %d\n"
224" - enter_chdev_server_write = %d / delta %d\n"
225" - enter_tty_cmd_write      = %d / delta %d\n"
226" - enter_tty_isr_write      = %d / delta %d\n"
227" - exit_tty_isr_write       = %d / delta %d\n"
228" - exit_tty_cmd_write       = %d / delta %d\n"
229" - exit_chdev_server_write  = %d / delta %d\n"
230" - exit_chdev_cmd_write     = %d / delta %d\n"
231" - exit_txt_write           = %d / delta %d\n"
232" - exit_devfs_write         = %d / delta %d\n"
233" - exit_sys_write           = %d / delta %d\n",
234enter_sys_write          , 0 ,
235enter_devfs_write        , enter_devfs_write        - enter_sys_write          ,
236enter_txt_write          , enter_txt_write          - enter_devfs_write        ,
237enter_chdev_cmd_write    , enter_chdev_cmd_write    - enter_txt_write          ,
238enter_chdev_server_write , enter_chdev_server_write - enter_chdev_cmd_write    ,
239enter_tty_cmd_write      , enter_tty_cmd_write      - enter_chdev_server_write ,
240enter_tty_isr_write      , enter_tty_isr_write      - enter_tty_cmd_write      ,
241exit_tty_isr_write       , exit_tty_isr_write       - enter_tty_isr_write      ,
242exit_tty_cmd_write       , exit_tty_cmd_write       - exit_tty_isr_write       ,
243exit_chdev_server_write  , exit_chdev_server_write  - exit_tty_cmd_write       ,
244exit_chdev_cmd_write     , exit_chdev_cmd_write     - exit_chdev_server_write  ,
245exit_txt_write           , exit_txt_write           - exit_chdev_cmd_write     ,
246exit_devfs_write         , exit_devfs_write         - exit_txt_write           ,
247exit_sys_write           , exit_sys_write           - exit_devfs_write         );
248#endif
249 
[407]250        return nbytes;
251
[23]252}  // end sys_write()
Note: See TracBrowser for help on using the repository browser.