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

Last change on this file since 670 was 670, checked in by alain, 18 months ago

1) Introduce up to 4 command lines arguments in the KSH "load" command.
These arguments are transfered to the user process through the
argc/argv mechanism, using the user space "args" vseg.

2) Introduce the named and anonymous "pipes", for inter-process communication
through the pipe() and mkfifo() syscalls.

3) Introduce the "chat" application to validate the two above mechanisms.

4) Improve printk() and assert() fonctions in printk.c.

File size: 9.1 KB
Line 
1/*
2 * sys_write.c - Kernel function implementing the "write" system call.
3 *
4 * Author        Alain Greiner (2016,2017,2018,2019,2020)
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 <pipe.h>
36#include <process.h>
37
38
39extern uint32_t enter_sys_write;
40extern uint32_t enter_devfs_write;
41extern uint32_t enter_txt_write;
42extern uint32_t enter_chdev_cmd_write;
43extern uint32_t enter_chdev_server_write;
44extern uint32_t enter_tty_cmd_write;
45extern uint32_t enter_tty_isr_write;
46extern uint32_t exit_tty_isr_write;
47extern uint32_t exit_tty_cmd_write;
48extern uint32_t exit_chdev_server_write;
49extern uint32_t exit_chdev_cmd_write;
50extern uint32_t exit_txt_write;
51extern uint32_t exit_devfs_write;
52extern uint32_t exit_sys_write;
53
54//////////////////////////////////
55int sys_write( uint32_t   file_id,
56               void     * vaddr,
57               uint32_t   count )
58{
59    error_t       error;
60    vseg_t      * vseg;            // required for user space checking
61        xptr_t        file_xp;         // remote file extended pointer
62    vfs_file_t  * file_ptr;        // remote file local pointer
63    cxy_t         file_cxy;        // remote file cluster identifier
64    uint32_t      file_type;       // file type
65    uint32_t      file_offset;     // file offset
66    uint32_t      file_attr;       // file attributes
67    vfs_inode_t * inode_ptr;       // local pointer on associated inode
68    uint32_t      nbytes;          // number of bytes actually written
69    reg_t         save_sr;         // required to enable IRQs during syscall
70
71        thread_t    * this = CURRENT_THREAD;
72        process_t   * process = this->process;
73
74#if DEBUG_SYS_WRITE || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS
75uint64_t     tm_start = hal_get_cycles();
76#endif
77
78#if DEBUG_SYS_WRITE
79if( DEBUG_SYS_WRITE < tm_start )
80printk("\n[%s] thread[%x,%x] enter / vaddr %x / %d bytes / cycle %d\n",
81__FUNCTION__, process->pid, this->trdid, vaddr, count, (uint32_t)tm_start );
82#endif
83 
84#if (DEBUG_SYS_WRITE & 1)
85if( DEBUG_SYS_WRITE < tm_start )
86enter_sys_write = (uint32_t)tm_start;
87#endif
88
89    // check file_id argument
90        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
91        {
92
93#if DEBUG_SYSCALLS_ERROR
94if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
95printk("\n[ERROR] in %s : thread[%x,%x] / illegal file descriptor index %d\n",
96__FUNCTION__, process->pid, this->trdid, file_id );
97#endif
98        this->errno = EBADFD;
99                return -1;
100        }
101
102    // check user buffer in user space
103    error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg );
104
105    if ( error )
106    {
107
108#if DEBUG_SYSCALLS_ERROR
109if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
110printk("\n[ERROR] in %s : thread[%x,%x] / user buffer unmapped %x\n",
111__FUNCTION__ , process->pid, this->trdid, (intptr_t)vaddr );
112#endif
113                this->errno = EINVAL;
114                return -1;
115    }
116
117    // get extended pointer on remote file descriptor
118    file_xp = process_fd_get_xptr_from_local( process , file_id );
119
120    if( file_xp == XPTR_NULL )
121    {
122
123#if DEBUG_SYSCALLS_ERROR
124if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
125printk("\n[ERROR] in %s : thread[%x,%x] / undefined file descriptor = %d\n",
126__FUNCTION__, process->pid, this->trdid, file_id );
127#endif
128                this->errno = EBADFD;
129                return -1;
130    }
131
132    // get file descriptor cluster and local pointer
133    file_ptr = GET_PTR( file_xp );
134    file_cxy = GET_CXY( file_xp );
135
136    // get file type, offset, aatributes, and associated inode
137    file_type   = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) );
138    file_offset = hal_remote_l32( XPTR( file_cxy , &file_ptr->offset ) );
139    inode_ptr   = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) );
140    file_attr   = hal_remote_l32( XPTR( file_cxy , &file_ptr->attr ) );
141
142    // enable IRQs
143    hal_enable_irq( &save_sr );
144
145    // check file writable
146    if( (file_attr & FD_ATTR_WRITE_ENABLE) == 0 )
147    {
148
149#if DEBUG_SYSCALLS_ERROR
150if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
151printk("\n[ERROR] in %s : thread[%x,%x] / file %d not writable\n",
152__FUNCTION__ , process->pid, this->trdid, file_id );
153#endif
154            hal_restore_irq( save_sr );
155                    this->errno = EBADFD;
156                    return -1;
157    }
158
159    // action depend on file type:
160    if( file_type == FILE_TYPE_REG )                         // write to mapper
161    {
162        // move count bytes to mapper
163        nbytes = vfs_user_move( false,    // to mapper
164                                file_xp,
165                                vaddr, 
166                                count );
167    }
168    else if( file_type == FILE_TYPE_DEV )                     // write to TXT device
169    {
170        // move count bytes to device
171        nbytes = devfs_user_move( false,  // to device
172                                  file_xp,
173                                  vaddr,
174                                  count );
175    }
176    else if( (file_type == FILE_TYPE_PIPE) ||
177             (file_type == FILE_TYPE_FIFO) )                  // write to pipe
178    {
179        // move count bytes to pipe
180        nbytes = pipe_user_move( false,   // to pipe
181                                 file_xp,
182                                 vaddr,
183                                 count );
184    }
185    else                                                       // unsupported type
186    {
187
188#if DEBUG_SYSCALLS_ERROR
189if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
190printk("\n[ERROR] in %s : thread[%x,%x] / illegal inode type %\n",
191__FUNCTION__, vfs_inode_type_str( file_type ) );
192#endif
193        hal_restore_irq( save_sr );
194                this->errno = EBADFD;
195                return -1;
196    }
197
198    // chek error
199    if( nbytes == 0xFFFFFFFF )
200    {
201
202#if DEBUG_SYSCALLS_ERROR
203if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
204printk("\n[ERROR] in %s : thread[%x,‰x] cannot write data to file %d\n",
205__FUNCTION__ , process->pid, this->trdid, file_id );
206#endif
207        hal_restore_irq( save_sr );
208        this->errno = EIO;
209        return -1;
210    }
211
212    // restore IRQs
213    hal_restore_irq( save_sr );
214
215    hal_fence();
216
217#if (DEBUG_SYS_WRITE || CONFIG_INSTRUMENTATION_SYSCALLS)
218uint64_t     tm_end = hal_get_cycles();
219#endif
220
221#if DEBUG_SYS_WRITE
222if( DEBUG_SYS_WRITE < tm_end )
223printk("\n[%s] thread[%x,%x] exit / cycle %d\n",
224__FUNCTION__, process->pid, this->trdid, (uint32_t)tm_end );
225#endif
226 
227#if CONFIG_INSTRUMENTATION_SYSCALLS
228hal_atomic_add( &syscalls_cumul_cost[SYS_WRITE] , tm_end - tm_start );
229hal_atomic_add( &syscalls_occurences[SYS_WRITE] , 1 );
230#endif
231
232#if (DEBUG_SYS_WRITE & 1)
233if( DEBUG_SYS_WRITE < tm_start )
234exit_sys_write = (uint32_t)tm_end;
235
236if( DEBUG_SYS_WRITE < tm_start )
237printk("\n***** timing to write a string *****\n"
238" - enter_sys_write          = %d / delta %d\n"
239" - enter_devfs_write        = %d / delta %d\n"
240" - enter_txt_write          = %d / delta %d\n"
241" - enter_chdev_cmd_write    = %d / delta %d\n"
242" - enter_chdev_server_write = %d / delta %d\n"
243" - enter_tty_cmd_write      = %d / delta %d\n"
244" - enter_tty_isr_write      = %d / delta %d\n"
245" - exit_tty_isr_write       = %d / delta %d\n"
246" - exit_tty_cmd_write       = %d / delta %d\n"
247" - exit_chdev_server_write  = %d / delta %d\n"
248" - exit_chdev_cmd_write     = %d / delta %d\n"
249" - exit_txt_write           = %d / delta %d\n"
250" - exit_devfs_write         = %d / delta %d\n"
251" - exit_sys_write           = %d / delta %d\n",
252enter_sys_write          , 0 ,
253enter_devfs_write        , enter_devfs_write        - enter_sys_write          ,
254enter_txt_write          , enter_txt_write          - enter_devfs_write        ,
255enter_chdev_cmd_write    , enter_chdev_cmd_write    - enter_txt_write          ,
256enter_chdev_server_write , enter_chdev_server_write - enter_chdev_cmd_write    ,
257enter_tty_cmd_write      , enter_tty_cmd_write      - enter_chdev_server_write ,
258enter_tty_isr_write      , enter_tty_isr_write      - enter_tty_cmd_write      ,
259exit_tty_isr_write       , exit_tty_isr_write       - enter_tty_isr_write      ,
260exit_tty_cmd_write       , exit_tty_cmd_write       - exit_tty_isr_write       ,
261exit_chdev_server_write  , exit_chdev_server_write  - exit_tty_cmd_write       ,
262exit_chdev_cmd_write     , exit_chdev_cmd_write     - exit_chdev_server_write  ,
263exit_txt_write           , exit_txt_write           - exit_chdev_cmd_write     ,
264exit_devfs_write         , exit_devfs_write         - exit_txt_write           ,
265exit_sys_write           , exit_sys_write           - exit_devfs_write         );
266#endif
267 
268        return nbytes;
269
270}  // end sys_write()
Note: See TracBrowser for help on using the repository browser.