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

Last change on this file since 664 was 664, checked in by alain, 3 years ago
  • Introduce the sys_socket.c file implementing all socket related syscalls.
  • Improve the non-standard sys_get_config() function.
File size: 8.4 KB
Line 
1/*
2 * sys_write.c - Kernel function implementing the "write" 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_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
53//////////////////////////////////
54int sys_write( uint32_t   file_id,
55               void     * vaddr,
56               uint32_t   count )
57{
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
64    uint32_t      file_offset;     // file offset
65    uint32_t      file_attr;       // file attributes
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
69
70        thread_t    * this = CURRENT_THREAD;
71        process_t   * process = this->process;
72
73#if (DEBUG_SYS_WRITE || CONFIG_INSTRUMENTATION_SYSCALLS)
74uint64_t     tm_start = hal_get_cycles();
75#endif
76
77#if DEBUG_SYS_WRITE
78if( DEBUG_SYS_WRITE < tm_start )
79printk("\n[%s] thread[%x,%x] enter / vaddr %x / %d bytes / cycle %d\n",
80__FUNCTION__, process->pid, this->trdid, vaddr, count, (uint32_t)tm_start );
81#endif
82 
83#if (DEBUG_SYS_WRITE & 1)
84if( DEBUG_SYS_WRITE < tm_start )
85enter_sys_write = (uint32_t)tm_start;
86#endif
87
88    // check file_id argument
89        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
90        {
91
92#if DEBUG_SYSCALLS_ERROR
93printk("\n[ERROR] in %s : thread[%x,%x] illegal file descriptor index %d\n",
94__FUNCTION__, process->pid, this->trdid, file_id );
95#endif
96        this->errno = EBADFD;
97                return -1;
98        }
99
100    // check user buffer in user space
101    error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg );
102
103    if ( error )
104    {
105
106#if DEBUG_SYSCALLS_ERROR
107printk("\n[ERROR] in %s : thread[%x,%x] user buffer unmapped %x\n",
108__FUNCTION__ , process->pid, this->trdid, (intptr_t)vaddr );
109#endif
110                this->errno = EINVAL;
111                return -1;
112    }
113
114    // get extended pointer on remote file descriptor
115    file_xp = process_fd_get_xptr_from_local( process , file_id );
116
117    if( file_xp == XPTR_NULL )
118    {
119
120#if DEBUG_SYSCALLS_ERROR
121printk("\n[ERROR] in %s : thread[%x,%x] undefined file descriptor = %d\n",
122__FUNCTION__, process->pid, this->trdid, file_id );
123#endif
124                this->errno = EBADFD;
125                return -1;
126    }
127
128    // get file descriptor cluster and local pointer
129    file_ptr = GET_PTR( file_xp );
130    file_cxy = GET_CXY( file_xp );
131
132    // get file type, offset, aatributes, and associated inode
133    file_type   = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) );
134    file_offset = hal_remote_l32( XPTR( file_cxy , &file_ptr->offset ) );
135    inode_ptr   = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) );
136    file_attr   = hal_remote_l32( XPTR( file_cxy , &file_ptr->attr ) );
137
138    // enable IRQs
139    hal_enable_irq( &save_sr );
140
141    // action depend on file type:
142
143    if( file_type == INODE_TYPE_FILE )  // write to a file mapper
144    {
145        // check file writable
146        if( (file_attr & FD_ATTR_WRITE_ENABLE) == 0 )
147            {
148
149#if DEBUG_SYSCALLS_ERROR
150printk("\n[ERROR] in %s : thread[%x,%x] file %d not writable\n",
151__FUNCTION__ , process->pid, this->trdid, file_id );
152#endif
153            hal_restore_irq( save_sr );
154                    this->errno = EBADFD;
155                    return -1;
156            }
157
158        // move count bytes to mapper
159        nbytes = vfs_user_move( false,               // from buffer to mapper
160                                file_xp,
161                                vaddr, 
162                                count );
163    }
164    else if( file_type == INODE_TYPE_DEV )  // write to TXT device
165    {
166        // move count bytes to device
167        nbytes = devfs_user_move( false,             // from buffer to device
168                                  file_xp,
169                                  vaddr,
170                                  count );
171    }
172    else  // not FILE and not DEV
173    {
174
175#if DEBUG_SYSCALLS_ERROR
176printk("\n[ERROR] in %s : thread[%x,%x] / illegal inode type %\n",
177__FUNCTION__, vfs_inode_type_str( file_type ) );
178#endif
179        hal_restore_irq( save_sr );
180                this->errno = EBADFD;
181                return -1;
182    }
183
184    // chek error
185    if( nbytes == 0xFFFFFFFF )
186    {
187
188#if DEBUG_SYSCALLS_ERROR
189printk("\n[ERROR] in %s : thread[%x,‰x] cannot write data to file %d\n",
190__FUNCTION__ , process->pid, this->trdid, file_id );
191#endif
192        hal_restore_irq( save_sr );
193        this->errno = EIO;
194        return -1;
195    }
196
197    // restore IRQs
198    hal_restore_irq( save_sr );
199
200    hal_fence();
201
202#if (DEBUG_SYS_WRITE || CONFIG_INSTRUMENTATION_SYSCALLS)
203uint64_t     tm_end = hal_get_cycles();
204#endif
205
206#if DEBUG_SYS_WRITE
207if( DEBUG_SYS_WRITE < tm_end )
208printk("\n[%s] thread[%x,%x] exit / cycle %d\n",
209__FUNCTION__, process->pid, this->trdid, (uint32_t)tm_end );
210#endif
211 
212#if CONFIG_INSTRUMENTATION_SYSCALLS
213hal_atomic_add( &syscalls_cumul_cost[SYS_WRITE] , tm_end - tm_start );
214hal_atomic_add( &syscalls_occurences[SYS_WRITE] , 1 );
215#endif
216
217#if (DEBUG_SYS_WRITE & 1)
218if( DEBUG_SYS_WRITE < tm_start )
219exit_sys_write = (uint32_t)tm_end;
220
221if( DEBUG_SYS_WRITE < tm_start )
222printk("\n***** timing to write a string *****\n"
223" - enter_sys_write          = %d / delta %d\n"
224" - enter_devfs_write        = %d / delta %d\n"
225" - enter_txt_write          = %d / delta %d\n"
226" - enter_chdev_cmd_write    = %d / delta %d\n"
227" - enter_chdev_server_write = %d / delta %d\n"
228" - enter_tty_cmd_write      = %d / delta %d\n"
229" - enter_tty_isr_write      = %d / delta %d\n"
230" - exit_tty_isr_write       = %d / delta %d\n"
231" - exit_tty_cmd_write       = %d / delta %d\n"
232" - exit_chdev_server_write  = %d / delta %d\n"
233" - exit_chdev_cmd_write     = %d / delta %d\n"
234" - exit_txt_write           = %d / delta %d\n"
235" - exit_devfs_write         = %d / delta %d\n"
236" - exit_sys_write           = %d / delta %d\n",
237enter_sys_write          , 0 ,
238enter_devfs_write        , enter_devfs_write        - enter_sys_write          ,
239enter_txt_write          , enter_txt_write          - enter_devfs_write        ,
240enter_chdev_cmd_write    , enter_chdev_cmd_write    - enter_txt_write          ,
241enter_chdev_server_write , enter_chdev_server_write - enter_chdev_cmd_write    ,
242enter_tty_cmd_write      , enter_tty_cmd_write      - enter_chdev_server_write ,
243enter_tty_isr_write      , enter_tty_isr_write      - enter_tty_cmd_write      ,
244exit_tty_isr_write       , exit_tty_isr_write       - enter_tty_isr_write      ,
245exit_tty_cmd_write       , exit_tty_cmd_write       - exit_tty_isr_write       ,
246exit_chdev_server_write  , exit_chdev_server_write  - exit_tty_cmd_write       ,
247exit_chdev_cmd_write     , exit_chdev_cmd_write     - exit_chdev_server_write  ,
248exit_txt_write           , exit_txt_write           - exit_chdev_cmd_write     ,
249exit_devfs_write         , exit_devfs_write         - exit_txt_write           ,
250exit_sys_write           , exit_sys_write           - exit_devfs_write         );
251#endif
252 
253        return nbytes;
254
255}  // end sys_write()
Note: See TracBrowser for help on using the repository browser.