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

Last change on this file since 423 was 421, checked in by alain, 7 years ago

Introduce sys_fg() , sys_display() , sys_wait() syscalls.

File size: 4.9 KB
RevLine 
[1]1/*
[23]2 * sys_write.c - write bytes to a file
[1]3 *
[23]4 * Author        Alain Greiner (2016,2017)
[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
37//////////////////////////////////
38int sys_write( uint32_t   file_id,
[407]39               void     * vaddr,
[23]40               uint32_t   count )
[1]41{
[23]42    error_t      error;
[408]43    paddr_t      paddr;           // unused, but required for user space checking
44        xptr_t       file_xp;         // remote file extended pointer
45    uint32_t     nbytes;          // number of bytes actually written
46    reg_t        save_sr;         // required to enable IRQs during syscall
[407]47
[418]48#if CONFIG_WRITE_DEBUG
[409]49uint32_t     tm_start;
50uint32_t     tm_end;
51tm_start = hal_get_cycles();
52#endif
[407]53
[23]54        thread_t   * this = CURRENT_THREAD;
55        process_t  * process = this->process;
[1]56 
[23]57    // check file_id argument
58        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
[1]59        {
[23]60        printk("\n[ERROR] in %s : illegal file descriptor index\n", __FUNCTION__ );
61        this->errno = EBADFD;
[1]62                return -1;
63        }
64
[23]65    // check user buffer in user space
[407]66    error = vmm_v2p_translate( false , vaddr , &paddr );
[23]67
68    if ( error )
69    {
70        printk("\n[ERROR] in %s : user buffer unmapped = %x\n",
[407]71        __FUNCTION__ , (intptr_t)vaddr );
[23]72                this->errno = EINVAL;
73                return -1;
74    }
75
[408]76    // enable IRQs
77    hal_enable_irq( &save_sr );
78
[23]79    // get extended pointer on remote file descriptor
80    file_xp = process_fd_get_xptr( process , file_id );
81
82    if( file_xp == XPTR_NULL )
83    {
[407]84        printk("\n[ERROR] in %s : undefined file descriptor index = %d in process %x\n",
85        __FUNCTION__ , file_id , process->pid );
[23]86                this->errno = EBADFD;
87                return -1;
88    }
89
90    // get file descriptor cluster and local pointer
91    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
92    cxy_t        file_cxy = GET_CXY( file_xp );
93
[421]94 
[407]95    // get file type
96    vfs_inode_type_t type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) );
[313]97
[407]98    // action depend on file type
[421]99    if( type == INODE_TYPE_FILE )  // check file writable & write to mapper
[23]100    {
[421]101        // check file writable
102        uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) );
103        if( (attr & FD_ATTR_WRITE_ENABLE) == 0 )
104            {
105            printk("\n[ERROR] in %s : file %d not writable in process %x\n",
106            __FUNCTION__ , file_id , process->pid );
107                    this->errno = EBADFD;
108                    return -1;
109            }
110
111        // move count bytes to mapper
[407]112        nbytes = vfs_user_move( false,               // from buffer to mapper
113                                file_xp,
114                                vaddr, 
115                                count );
116    }
[421]117    else if( type == INODE_TYPE_DEV )  // check ownership & write to device
[407]118    {
[421]119        // move count bytes to device
[407]120        nbytes = devfs_user_move( false,             // from buffer to device
121                                 file_xp,
122                                 vaddr,
123                                 count );
124    }
125    else
126    {
127        nbytes = 0;
[421]128        assert( false , __FUNCTION__ , "file type %d non supported\n", type );
[407]129    }
130
131    if( nbytes != count )
132    {
133        printk("\n[ERROR] in %s cannot write data to file %d in process %x\n",
134        __FUNCTION__ , file_id , process->pid );
[313]135        this->errno = error;
136        return -1;
[23]137    }
138
[408]139    // restore IRQs
140    hal_restore_irq( save_sr );
141
[124]142    hal_fence();
[23]143
[418]144#if CONFIG_WRITE_DEBUG
[409]145tm_end = hal_get_cycles();
[416]146printk("\n[DBG] %s : core[%x,%d] / thread %x in process %x / cycle %d\n"
[408]147"nbytes = %d / first byte = %c / file_id = %d / cost = %d\n",
148__FUNCTION__ , local_cxy , this->core->lid , this->trdid , this->process->pid ,
[416]149(uint32_t)tm_start , nbytes , *((char *)(intptr_t)paddr) , file_id , 
150(uint32_t)(tm_end - tm_start) );
[409]151#endif
[407]152 
[421]153#if (CONFIG_WRITE_DEBUG & 0x1)
[418]154printk("\n@@@@@@@@@@@@ timing to write character %c\n"
155" - enter_sys_write    = %d\n"
156" - exit_sys_write     = %d\n",
157*((char *)(intptr_t)paddr) , (uint32_t)tm_start , (uint32_t)tm_end );
158#endif
159
[407]160        return nbytes;
161
[23]162}  // end sys_write()
Note: See TracBrowser for help on using the repository browser.