1 | /* |
---|
2 | * sys_write.c - Kernel function implementing the "write" system call. |
---|
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_kernel_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 <vmm.h> |
---|
32 | #include <thread.h> |
---|
33 | #include <printk.h> |
---|
34 | #include <process.h> |
---|
35 | |
---|
36 | |
---|
37 | extern uint32_t enter_sys_write; |
---|
38 | extern uint32_t enter_devfs_write; |
---|
39 | extern uint32_t enter_txt_write; |
---|
40 | extern uint32_t enter_chdev_cmd_write; |
---|
41 | extern uint32_t enter_chdev_server_write; |
---|
42 | extern uint32_t enter_tty_cmd_write; |
---|
43 | extern uint32_t enter_tty_isr_write; |
---|
44 | extern uint32_t exit_tty_isr_write; |
---|
45 | extern uint32_t exit_tty_cmd_write; |
---|
46 | extern uint32_t exit_chdev_server_write; |
---|
47 | extern uint32_t exit_chdev_cmd_write; |
---|
48 | extern uint32_t exit_txt_write; |
---|
49 | extern uint32_t exit_devfs_write; |
---|
50 | extern uint32_t exit_sys_write; |
---|
51 | |
---|
52 | ////////////////////////////////// |
---|
53 | int 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 | thread_t * this = CURRENT_THREAD; |
---|
64 | process_t * process = this->process; |
---|
65 | |
---|
66 | #if (DEBUG_SYS_WRITE || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
67 | uint64_t tm_start = hal_get_cycles(); |
---|
68 | #endif |
---|
69 | |
---|
70 | #if DEBUG_SYS_WRITE |
---|
71 | tm_start = hal_get_cycles(); |
---|
72 | if( DEBUG_SYS_WRITE < tm_start ) |
---|
73 | printk("\n[DBG] %s : thread %x in process %x enter / vaddr %x / count %d / cycle %d\n", |
---|
74 | __FUNCTION__, this->trdid, process->pid, vaddr, count, (uint32_t)tm_start ); |
---|
75 | #endif |
---|
76 | |
---|
77 | #if (DEBUG_SYS_WRITE & 1) |
---|
78 | enter_sys_write = (uint32_t)tm_start; |
---|
79 | #endif |
---|
80 | |
---|
81 | // check file_id argument |
---|
82 | if( file_id >= CONFIG_PROCESS_FILE_MAX_NR ) |
---|
83 | { |
---|
84 | |
---|
85 | #if DEBUG_SYSCALLS_ERROR |
---|
86 | printk("\n[ERROR] in %s : illegal file descriptor index\n", __FUNCTION__ ); |
---|
87 | #endif |
---|
88 | this->errno = EBADFD; |
---|
89 | return -1; |
---|
90 | } |
---|
91 | |
---|
92 | // check user buffer in user space |
---|
93 | error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg ); |
---|
94 | |
---|
95 | if ( error ) |
---|
96 | { |
---|
97 | |
---|
98 | #if DEBUG_SYSCALLS_ERROR |
---|
99 | printk("\n[ERROR] in %s : user buffer unmapped %x / thread %x / process %x\n", |
---|
100 | __FUNCTION__ , (intptr_t)vaddr, this->trdid, process->pid ); |
---|
101 | vmm_display( process , false ); |
---|
102 | #endif |
---|
103 | this->errno = EINVAL; |
---|
104 | return -1; |
---|
105 | } |
---|
106 | |
---|
107 | // get extended pointer on remote file descriptor |
---|
108 | file_xp = process_fd_get_xptr( process , file_id ); |
---|
109 | |
---|
110 | if( file_xp == XPTR_NULL ) |
---|
111 | { |
---|
112 | |
---|
113 | #if DEBUG_SYSCALLS_ERROR |
---|
114 | printk("\n[ERROR] in %s : undefined file descriptor index = %d in process %x\n", |
---|
115 | __FUNCTION__ , file_id , process->pid ); |
---|
116 | #endif |
---|
117 | this->errno = EBADFD; |
---|
118 | return -1; |
---|
119 | } |
---|
120 | |
---|
121 | // get file descriptor cluster and local pointer |
---|
122 | vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); |
---|
123 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
124 | |
---|
125 | |
---|
126 | // get file type |
---|
127 | vfs_inode_type_t type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) ); |
---|
128 | |
---|
129 | // enable IRQs |
---|
130 | hal_enable_irq( &save_sr ); |
---|
131 | |
---|
132 | // action depend on file type |
---|
133 | if( type == INODE_TYPE_FILE ) // check file writable & write to mapper |
---|
134 | { |
---|
135 | // check file writable |
---|
136 | uint32_t attr = hal_remote_l32( XPTR( file_cxy , &file_ptr->attr ) ); |
---|
137 | if( (attr & FD_ATTR_WRITE_ENABLE) == 0 ) |
---|
138 | { |
---|
139 | |
---|
140 | #if DEBUG_SYSCALLS_ERROR |
---|
141 | printk("\n[ERROR] in %s : file %d not writable in process %x\n", |
---|
142 | __FUNCTION__ , file_id , process->pid ); |
---|
143 | #endif |
---|
144 | this->errno = EBADFD; |
---|
145 | return -1; |
---|
146 | } |
---|
147 | |
---|
148 | // move count bytes to mapper |
---|
149 | nbytes = vfs_user_move( false, // from buffer to mapper |
---|
150 | file_xp, |
---|
151 | vaddr, |
---|
152 | count ); |
---|
153 | } |
---|
154 | else if( type == INODE_TYPE_DEV ) // check ownership & write to device |
---|
155 | { |
---|
156 | // check user buffer size for TXT_TX |
---|
157 | if( (type == INODE_TYPE_DEV) && (count >= CONFIG_TXT_KBUF_SIZE) ) |
---|
158 | { |
---|
159 | |
---|
160 | #if DEBUG_SYSCALLS_ERROR |
---|
161 | printk("\n[ERROR] in %s : user buffer size %x too large / thread %x / process %x\n", |
---|
162 | __FUNCTION__ , count, this->trdid, process->pid ); |
---|
163 | #endif |
---|
164 | this->errno = EINVAL; |
---|
165 | return -1; |
---|
166 | } |
---|
167 | |
---|
168 | // move count bytes to device |
---|
169 | nbytes = devfs_user_move( false, // from buffer to device |
---|
170 | file_xp, |
---|
171 | vaddr, |
---|
172 | count ); |
---|
173 | } |
---|
174 | else // not FILE and not DEV |
---|
175 | { |
---|
176 | nbytes = 0; |
---|
177 | assert( false , "file type %d non supported\n", type ); |
---|
178 | } |
---|
179 | |
---|
180 | if( nbytes != count ) |
---|
181 | { |
---|
182 | |
---|
183 | #if DEBUG_SYSCALLS_ERROR |
---|
184 | printk("\n[ERROR] in %s cannot write data to file %d in process %x\n", |
---|
185 | __FUNCTION__ , file_id , process->pid ); |
---|
186 | #endif |
---|
187 | this->errno = error; |
---|
188 | return -1; |
---|
189 | } |
---|
190 | |
---|
191 | // restore IRQs |
---|
192 | hal_restore_irq( save_sr ); |
---|
193 | |
---|
194 | hal_fence(); |
---|
195 | |
---|
196 | #if (DEBUG_SYS_WRITE || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
197 | uint64_t tm_end = hal_get_cycles(); |
---|
198 | #endif |
---|
199 | |
---|
200 | #if DEBUG_SYS_WRITE |
---|
201 | if( DEBUG_SYS_WRITE < tm_end ) |
---|
202 | printk("\n[DBG] %s : thread %x in process %x exit / cycle %d\n", |
---|
203 | __FUNCTION__, this->trdid, process->pid, (uint32_t)tm_end ); |
---|
204 | #endif |
---|
205 | |
---|
206 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
207 | hal_atomic_add( &syscalls_cumul_cost[SYS_WRITE] , tm_end - tm_start ); |
---|
208 | hal_atomic_add( &syscalls_occurences[SYS_WRITE] , 1 ); |
---|
209 | #endif |
---|
210 | |
---|
211 | #if (DEBUG_SYS_WRITE & 1) |
---|
212 | exit_sys_write = (uint32_t)tm_end; |
---|
213 | |
---|
214 | printk("\n***** timing to write a string *****\n" |
---|
215 | " - enter_sys_write = %d / delta %d\n" |
---|
216 | " - enter_devfs_write = %d / delta %d\n" |
---|
217 | " - enter_txt_write = %d / delta %d\n" |
---|
218 | " - enter_chdev_cmd_write = %d / delta %d\n" |
---|
219 | " - enter_chdev_server_write = %d / delta %d\n" |
---|
220 | " - enter_tty_cmd_write = %d / delta %d\n" |
---|
221 | " - enter_tty_isr_write = %d / delta %d\n" |
---|
222 | " - exit_tty_isr_write = %d / delta %d\n" |
---|
223 | " - exit_tty_cmd_write = %d / delta %d\n" |
---|
224 | " - exit_chdev_server_write = %d / delta %d\n" |
---|
225 | " - exit_chdev_cmd_write = %d / delta %d\n" |
---|
226 | " - exit_txt_write = %d / delta %d\n" |
---|
227 | " - exit_devfs_write = %d / delta %d\n" |
---|
228 | " - exit_sys_write = %d / delta %d\n", |
---|
229 | enter_sys_write , 0 , |
---|
230 | enter_devfs_write , enter_devfs_write - enter_sys_write , |
---|
231 | enter_txt_write , enter_txt_write - enter_devfs_write , |
---|
232 | enter_chdev_cmd_write , enter_chdev_cmd_write - enter_txt_write , |
---|
233 | enter_chdev_server_write , enter_chdev_server_write - enter_chdev_cmd_write , |
---|
234 | enter_tty_cmd_write , enter_tty_cmd_write - enter_chdev_server_write , |
---|
235 | enter_tty_isr_write , enter_tty_isr_write - enter_tty_cmd_write , |
---|
236 | exit_tty_isr_write , exit_tty_isr_write - enter_tty_isr_write , |
---|
237 | exit_tty_cmd_write , exit_tty_cmd_write - exit_tty_isr_write , |
---|
238 | exit_chdev_server_write , exit_chdev_server_write - exit_tty_cmd_write , |
---|
239 | exit_chdev_cmd_write , exit_chdev_cmd_write - exit_chdev_server_write , |
---|
240 | exit_txt_write , exit_txt_write - exit_chdev_cmd_write , |
---|
241 | exit_devfs_write , exit_devfs_write - exit_txt_write , |
---|
242 | exit_sys_write , exit_sys_write - exit_devfs_write ); |
---|
243 | #endif |
---|
244 | |
---|
245 | return nbytes; |
---|
246 | |
---|
247 | } // end sys_write() |
---|