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

Last change on this file since 407 was 407, checked in by alain, 5 years ago

First implementation of fork/exec.

File size: 4.2 KB
Line 
1/*
2 * sys_write.c - write bytes to 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_special.h>
28#include <errno.h>
29#include <vfs.h>
30#include <thread.h>
31#include <printk.h>
32#include <process.h>
33
34/* TODO: concurrent user page(s) unmap need to be handled [AG] */
35
36//////////////////////////////////
37int sys_write( uint32_t   file_id,
38               void     * vaddr,
39               uint32_t   count )
40{
41    error_t      error;
42    paddr_t      paddr;                // unused, but required for user space checking
43        xptr_t       file_xp;              // remote file extended pointer
44    uint32_t     nbytes;               // number of bytes actually written
45
46        uint32_t     tm_start;
47        uint32_t     tm_end;
48
49        tm_start = hal_get_cycles();
50
51        thread_t   * this = CURRENT_THREAD;
52        process_t  * process = this->process;
53 
54    // check file_id argument
55        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
56        {
57        printk("\n[ERROR] in %s : illegal file descriptor index\n", __FUNCTION__ );
58        this->errno = EBADFD;
59                return -1;
60        }
61
62    // check user buffer in user space
63    error = vmm_v2p_translate( false , vaddr , &paddr );
64
65    if ( error )
66    {
67        printk("\n[ERROR] in %s : user buffer unmapped = %x\n",
68        __FUNCTION__ , (intptr_t)vaddr );
69                this->errno = EINVAL;
70                return -1;
71    }
72
73    // get extended pointer on remote file descriptor
74    file_xp = process_fd_get_xptr( process , file_id );
75
76    if( file_xp == XPTR_NULL )
77    {
78        printk("\n[ERROR] in %s : undefined file descriptor index = %d in process %x\n",
79        __FUNCTION__ , file_id , process->pid );
80                this->errno = EBADFD;
81                return -1;
82    }
83
84    // get file descriptor cluster and local pointer
85    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
86    cxy_t        file_cxy = GET_CXY( file_xp );
87
88    // check file writable
89    uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) );
90    if( (attr & FD_ATTR_WRITE_ENABLE) == 0 )
91        {
92        printk("\n[ERROR] in %s : file %d not writable in process %x\n",
93        __FUNCTION__ , file_id , process->pid );
94                this->errno = EBADFD;
95                return -1;
96        }
97   
98    // get file type
99    vfs_inode_type_t type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) );
100
101    // action depend on file type
102    if( type == INODE_TYPE_FILE )      // transfer count bytes to file mapper
103    {
104        nbytes = vfs_user_move( false,               // from buffer to mapper
105                                file_xp,
106                                vaddr, 
107                                count );
108    }
109    else if( type == INODE_TYPE_DEV )  // transfer count bytes to device
110    {
111        nbytes = devfs_user_move( false,             // from buffer to device
112                                 file_xp,
113                                 vaddr,
114                                 count );
115    }
116    else
117    {
118        nbytes = 0;
119        panic("file type %d non supported", type );
120    }
121
122    if( nbytes != count )
123    {
124        printk("\n[ERROR] in %s cannot write data to file %d in process %x\n",
125        __FUNCTION__ , file_id , process->pid );
126        this->errno = error;
127        return -1;
128    }
129
130    hal_fence();
131
132    tm_end = hal_get_cycles();
133
134syscall_dmsg("\n[DBG] %s : core[%x,%d] / thread %x / nbytes = %d / cycle %d\n"
135" first byte = %c / file_id = %d / cost = %d\n",
136__FUNCTION__ , local_cxy , this->core->lid , this->trdid , nbytes , tm_start ,
137*((char *)(intptr_t)paddr) , file_id , tm_end - tm_start );
138 
139        return nbytes;
140
141}  // end sys_write()
Note: See TracBrowser for help on using the repository browser.