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

Last change on this file since 440 was 440, checked in by alain, 6 years ago

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

File size: 7.4 KB
RevLine 
[1]1/*
[23]2 * sys_write.c - write bytes to a file
[1]3 *
[440]4 * Author        Alain Greiner (2016,2017,2018)
[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>
25#include <hal_types.h>
26#include <hal_uspace.h>
[409]27#include <hal_irqmask.h>
[23]28#include <hal_special.h>
[1]29#include <errno.h>
30#include <vfs.h>
31#include <thread.h>
[23]32#include <printk.h>
33#include <process.h>
[1]34
[407]35/* TODO: concurrent user page(s) unmap need to be handled [AG] */
[23]36
[440]37extern uint32_t enter_sys_write;
38extern uint32_t enter_devfs_write;
39extern uint32_t enter_txt_write;
40extern uint32_t enter_chdev_cmd_write;
41extern uint32_t enter_chdev_server_write;
42extern uint32_t enter_tty_cmd_write;
43extern uint32_t enter_tty_isr_write;
44extern uint32_t exit_tty_isr_write;
45extern uint32_t exit_tty_cmd_write;
46extern uint32_t exit_chdev_server_write;
47extern uint32_t exit_chdev_cmd_write;
48extern uint32_t exit_txt_write;
49extern uint32_t exit_devfs_write;
50extern uint32_t exit_sys_write;
51
[23]52//////////////////////////////////
53int sys_write( uint32_t   file_id,
[407]54               void     * vaddr,
[23]55               uint32_t   count )
[1]56{
[23]57    error_t      error;
[440]58    vseg_t     * vseg;            // required for user space checking
[408]59        xptr_t       file_xp;         // remote file extended pointer
60    uint32_t     nbytes;          // number of bytes actually written
61    reg_t        save_sr;         // required to enable IRQs during syscall
[407]62
[440]63#if (DEBUG_SYS_WRITE & 1)
64enter_sys_write = (uint32_t)tm_start;
[435]65#endif
66
[433]67        thread_t   * this = CURRENT_THREAD;
68        process_t  * process = this->process;
69
[438]70#if DEBUG_SYS_WRITE
[409]71uint32_t     tm_start;
72uint32_t     tm_end;
73tm_start = hal_get_cycles();
[438]74if( DEBUG_SYS_WRITE < tm_start )
[435]75printk("\n[DBG] %s : thread %x enter / process %x / vaddr %x / count %d / cycle %d\n",
[433]76__FUNCTION__, this, process->pid, vaddr, count, (uint32_t)tm_start );
[409]77#endif
[1]78 
[23]79    // check file_id argument
80        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
[1]81        {
[433]82
[438]83#if DEBUG_SYSCALLS_ERROR
[433]84printk("\n[ERROR] in %s : illegal file descriptor index\n", __FUNCTION__ );
85#endif
[23]86        this->errno = EBADFD;
[1]87                return -1;
88        }
89
[23]90    // check user buffer in user space
[440]91    error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg );
[23]92
93    if ( error )
94    {
[433]95
[438]96#if DEBUG_SYSCALLS_ERROR
[440]97printk("\n[ERROR] in %s : user buffer unmapped %x / thread %x / process %x\n",
98__FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid );
99vmm_display( process , false );
[433]100#endif
[23]101                this->errno = EINVAL;
102                return -1;
103    }
104
[408]105    // enable IRQs
106    hal_enable_irq( &save_sr );
107
[23]108    // get extended pointer on remote file descriptor
109    file_xp = process_fd_get_xptr( process , file_id );
110
111    if( file_xp == XPTR_NULL )
112    {
[433]113
[438]114#if DEBUG_SYSCALLS_ERROR
[433]115printk("\n[ERROR] in %s : undefined file descriptor index = %d in process %x\n",
116__FUNCTION__ , file_id , process->pid );
117#endif
[23]118                this->errno = EBADFD;
119                return -1;
120    }
121
122    // get file descriptor cluster and local pointer
123    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
124    cxy_t        file_cxy = GET_CXY( file_xp );
125
[421]126 
[407]127    // get file type
128    vfs_inode_type_t type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) );
[313]129
[407]130    // action depend on file type
[421]131    if( type == INODE_TYPE_FILE )  // check file writable & write to mapper
[23]132    {
[421]133        // check file writable
134        uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) );
135        if( (attr & FD_ATTR_WRITE_ENABLE) == 0 )
136            {
[433]137
[438]138#if DEBUG_SYSCALLS_ERROR
[433]139printk("\n[ERROR] in %s : file %d not writable in process %x\n",
140__FUNCTION__ , file_id , process->pid );
141#endif
[421]142                    this->errno = EBADFD;
143                    return -1;
144            }
145
146        // move count bytes to mapper
[407]147        nbytes = vfs_user_move( false,               // from buffer to mapper
148                                file_xp,
149                                vaddr, 
150                                count );
151    }
[421]152    else if( type == INODE_TYPE_DEV )  // check ownership & write to device
[407]153    {
[421]154        // move count bytes to device
[407]155        nbytes = devfs_user_move( false,             // from buffer to device
156                                 file_xp,
157                                 vaddr,
158                                 count );
159    }
160    else
161    {
162        nbytes = 0;
[421]163        assert( false , __FUNCTION__ , "file type %d non supported\n", type );
[407]164    }
165
166    if( nbytes != count )
167    {
[433]168
[438]169#if DEBUG_SYSCALLS_ERROR
[433]170printk("\n[ERROR] in %s cannot write data to file %d in process %x\n",
171__FUNCTION__ , file_id , process->pid );
172#endif
[313]173        this->errno = error;
174        return -1;
[23]175    }
176
[408]177    // restore IRQs
178    hal_restore_irq( save_sr );
179
[124]180    hal_fence();
[23]181
[438]182#if DEBUG_SYS_WRITE
[409]183tm_end = hal_get_cycles();
[438]184if( DEBUG_SYS_WRITE < tm_end )
[435]185printk("\n[DBG] %s : thread %x exit / process %x / cycle %d\n"
[408]186"nbytes = %d / first byte = %c / file_id = %d / cost = %d\n",
[433]187__FUNCTION__, this, process->pid, (uint32_t)tm_start,
188nbytes, *((char *)(intptr_t)paddr) , file_id , (uint32_t)(tm_end - tm_start) );
[409]189#endif
[407]190 
[438]191#if (DEBUG_SYS_WRITE & 1)
[435]192exit_sys_write = (uint32_t)tm_end;
193
194printk("\n@@@@@@@@@@@@ timing to write string %c\n"
195" - enter_sys_write          = %d / delta %d\n"
196" - enter_devfs_write        = %d / delta %d\n"
197" - enter_txt_write          = %d / delta %d\n"
198" - enter_chdev_cmd_write    = %d / delta %d\n"
199" - enter_chdev_server_write = %d / delta %d\n"
200" - enter_tty_cmd_write      = %d / delta %d\n"
201" - enter_tty_isr_write      = %d / delta %d\n"
202" - exit_tty_isr_write       = %d / delta %d\n"
203" - exit_tty_cmd_write       = %d / delta %d\n"
204" - exit_chdev_server_write  = %d / delta %d\n"
205" - exit_chdev_cmd_write     = %d / delta %d\n"
206" - exit_txt_write           = %d / delta %d\n"
207" - exit_devfs_write         = %d / delta %d\n"
208" - exit_sys_write           = %d / delta %d\n",
209*((char *)(intptr_t)paddr) ,
210enter_sys_write          , 0 ,
211enter_devfs_write        , enter_devfs_write        - enter_sys_write          ,
212enter_txt_write          , enter_txt_write          - enter_devfs_write        ,
213enter_chdev_cmd_write    , enter_chdev_cmd_write    - enter_txt_write          ,
214enter_chdev_server_write , enter_chdev_server_write - enter_chdev_cmd_write    ,
215enter_tty_cmd_write      , enter_tty_cmd_write      - enter_chdev_server_write ,
216enter_tty_isr_write      , enter_tty_isr_write      - enter_tty_cmd_write      ,
217exit_tty_isr_write       , exit_tty_isr_write       - enter_tty_isr_write      ,
218exit_tty_cmd_write       , exit_tty_cmd_write       - exit_tty_isr_write       ,
219exit_chdev_server_write  , exit_chdev_server_write  - exit_tty_cmd_write       ,
220exit_chdev_cmd_write     , exit_chdev_cmd_write     - exit_chdev_server_write  ,
221exit_txt_write           , exit_txt_write           - exit_chdev_cmd_write     ,
222exit_devfs_write         , exit_devfs_write         - exit_txt_write           ,
223exit_sys_write           , exit_sys_write           - exit_devfs_write         );
224#endif
225 
[407]226        return nbytes;
227
[23]228}  // end sys_write()
Note: See TracBrowser for help on using the repository browser.