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

Last change on this file since 440 was 440, checked in by alain, 8 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
Line 
1/*
2 * sys_write.c - write bytes to a file
3 *
4 * Author Alain Greiner (2016,2017,2018)
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_irqmask.h>
28#include <hal_special.h>
29#include <errno.h>
30#include <vfs.h>
31#include <thread.h>
32#include <printk.h>
33#include <process.h>
34
35/* TODO: concurrent user page(s) unmap need to be handled [AG] */
36
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
52//////////////////////////////////
53int sys_write( uint32_t file_id,
54 void * vaddr,
55 uint32_t count )
56{
57 error_t error;
58 vseg_t * vseg; // required for user space checking
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
62
63#if (DEBUG_SYS_WRITE & 1)
64enter_sys_write = (uint32_t)tm_start;
65#endif
66
67 thread_t * this = CURRENT_THREAD;
68 process_t * process = this->process;
69
70#if DEBUG_SYS_WRITE
71uint32_t tm_start;
72uint32_t tm_end;
73tm_start = hal_get_cycles();
74if( DEBUG_SYS_WRITE < tm_start )
75printk("\n[DBG] %s : thread %x enter / process %x / vaddr %x / count %d / cycle %d\n",
76__FUNCTION__, this, process->pid, vaddr, count, (uint32_t)tm_start );
77#endif
78
79 // check file_id argument
80 if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
81 {
82
83#if DEBUG_SYSCALLS_ERROR
84printk("\n[ERROR] in %s : illegal file descriptor index\n", __FUNCTION__ );
85#endif
86 this->errno = EBADFD;
87 return -1;
88 }
89
90 // check user buffer in user space
91 error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg );
92
93 if ( error )
94 {
95
96#if DEBUG_SYSCALLS_ERROR
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 );
100#endif
101 this->errno = EINVAL;
102 return -1;
103 }
104
105 // enable IRQs
106 hal_enable_irq( &save_sr );
107
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 {
113
114#if DEBUG_SYSCALLS_ERROR
115printk("\n[ERROR] in %s : undefined file descriptor index = %d in process %x\n",
116__FUNCTION__ , file_id , process->pid );
117#endif
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
126
127 // get file type
128 vfs_inode_type_t type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) );
129
130 // action depend on file type
131 if( type == INODE_TYPE_FILE ) // check file writable & write to mapper
132 {
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 {
137
138#if DEBUG_SYSCALLS_ERROR
139printk("\n[ERROR] in %s : file %d not writable in process %x\n",
140__FUNCTION__ , file_id , process->pid );
141#endif
142 this->errno = EBADFD;
143 return -1;
144 }
145
146 // move count bytes to mapper
147 nbytes = vfs_user_move( false, // from buffer to mapper
148 file_xp,
149 vaddr,
150 count );
151 }
152 else if( type == INODE_TYPE_DEV ) // check ownership & write to device
153 {
154 // move count bytes to device
155 nbytes = devfs_user_move( false, // from buffer to device
156 file_xp,
157 vaddr,
158 count );
159 }
160 else
161 {
162 nbytes = 0;
163 assert( false , __FUNCTION__ , "file type %d non supported\n", type );
164 }
165
166 if( nbytes != count )
167 {
168
169#if DEBUG_SYSCALLS_ERROR
170printk("\n[ERROR] in %s cannot write data to file %d in process %x\n",
171__FUNCTION__ , file_id , process->pid );
172#endif
173 this->errno = error;
174 return -1;
175 }
176
177 // restore IRQs
178 hal_restore_irq( save_sr );
179
180 hal_fence();
181
182#if DEBUG_SYS_WRITE
183tm_end = hal_get_cycles();
184if( DEBUG_SYS_WRITE < tm_end )
185printk("\n[DBG] %s : thread %x exit / process %x / cycle %d\n"
186"nbytes = %d / first byte = %c / file_id = %d / cost = %d\n",
187__FUNCTION__, this, process->pid, (uint32_t)tm_start,
188nbytes, *((char *)(intptr_t)paddr) , file_id , (uint32_t)(tm_end - tm_start) );
189#endif
190
191#if (DEBUG_SYS_WRITE & 1)
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
226 return nbytes;
227
228} // end sys_write()
Note: See TracBrowser for help on using the repository browser.