1 | /* |
---|
2 | * sys_read.c - Kernel function implementing the "read" 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 | |
---|
39 | extern uint32_t enter_sys_read; |
---|
40 | extern uint32_t enter_devfs_read; |
---|
41 | extern uint32_t enter_txt_read; |
---|
42 | extern uint32_t enter_chdev_cmd_read; |
---|
43 | extern uint32_t enter_chdev_server_read; |
---|
44 | extern uint32_t enter_tty_cmd_read; |
---|
45 | extern uint32_t enter_tty_isr_read; |
---|
46 | extern uint32_t exit_tty_isr_read; |
---|
47 | extern uint32_t exit_tty_cmd_read; |
---|
48 | extern uint32_t exit_chdev_server_read; |
---|
49 | extern uint32_t exit_chdev_cmd_read; |
---|
50 | extern uint32_t exit_txt_read; |
---|
51 | extern uint32_t exit_devfs_read; |
---|
52 | extern uint32_t exit_sys_read; |
---|
53 | |
---|
54 | |
---|
55 | ///////////////////////////////// |
---|
56 | int sys_read( uint32_t file_id, |
---|
57 | void * vaddr, |
---|
58 | uint32_t count ) |
---|
59 | { |
---|
60 | error_t error; |
---|
61 | vseg_t * vseg; // required for user space checking |
---|
62 | xptr_t file_xp; // remote file extended pointer |
---|
63 | vfs_file_t * file_ptr; // remote file local pointer |
---|
64 | cxy_t file_cxy; // remote file cluster identifier |
---|
65 | uint32_t file_type; // file type |
---|
66 | uint32_t file_offset; // file offset |
---|
67 | uint32_t file_attr; // file attributes |
---|
68 | vfs_inode_t * inode_ptr; // local pointer on file inode |
---|
69 | uint32_t nbytes; // number of bytes actually read |
---|
70 | reg_t save_sr; // required to enable IRQs during syscall |
---|
71 | |
---|
72 | thread_t * this = CURRENT_THREAD; |
---|
73 | process_t * process = this->process; |
---|
74 | xptr_t process_owner_xp = process->owner_xp; |
---|
75 | |
---|
76 | #if DEBUG_SYS_READ || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS |
---|
77 | uint64_t tm_start = hal_get_cycles(); |
---|
78 | #endif |
---|
79 | |
---|
80 | #if DEBUG_SYS_READ |
---|
81 | if( DEBUG_SYS_READ < tm_start ) |
---|
82 | printk("\n[%s] thread[%x,%x] enter / vaddr %x / count %d / cycle %d\n", |
---|
83 | __FUNCTION__, process->pid, this->trdid, vaddr, count, (uint32_t)tm_start ); |
---|
84 | #endif |
---|
85 | |
---|
86 | #if (DEBUG_SYS_READ & 1) |
---|
87 | enter_sys_read = (uint32_t)tm_start; |
---|
88 | #endif |
---|
89 | |
---|
90 | // check file_id argument |
---|
91 | if( file_id >= CONFIG_PROCESS_FILE_MAX_NR ) |
---|
92 | { |
---|
93 | |
---|
94 | #if DEBUG_SYSCALLS_ERROR |
---|
95 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
96 | printk("\n[ERROR] in %s : thread[%x,%x] / illegal file descriptor index %d\n", |
---|
97 | __FUNCTION__ , process->pid, this->trdid, file_id ); |
---|
98 | #endif |
---|
99 | this->errno = EBADFD; |
---|
100 | return -1; |
---|
101 | } |
---|
102 | |
---|
103 | // check user buffer in user space |
---|
104 | error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg ); |
---|
105 | |
---|
106 | if ( error ) |
---|
107 | { |
---|
108 | |
---|
109 | #if DEBUG_SYSCALLS_ERROR |
---|
110 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
111 | printk("\n[ERROR] in %s : thread[%x,%x] / user buffer unmapped %x\n", |
---|
112 | __FUNCTION__ , process->pid, this->trdid, (intptr_t)vaddr ); |
---|
113 | #endif |
---|
114 | this->errno = EINVAL; |
---|
115 | return -1; |
---|
116 | } |
---|
117 | |
---|
118 | // get extended pointer on remote file descriptor |
---|
119 | file_xp = process_fd_get_xptr_from_local( process , file_id ); |
---|
120 | |
---|
121 | if( file_xp == XPTR_NULL ) |
---|
122 | { |
---|
123 | |
---|
124 | #if DEBUG_SYSCALLS_ERROR |
---|
125 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
126 | printk("\n[ERROR] in %s : thread[%x,%x] / undefined fd_id %d\n", |
---|
127 | __FUNCTION__, process->pid, this->trdid, file_id ); |
---|
128 | #endif |
---|
129 | this->errno = EBADFD; |
---|
130 | return -1; |
---|
131 | } |
---|
132 | |
---|
133 | // get file descriptor cluster and local pointer |
---|
134 | file_ptr = GET_PTR( file_xp ); |
---|
135 | file_cxy = GET_CXY( file_xp ); |
---|
136 | |
---|
137 | // get inode, file type, offset and attributes |
---|
138 | inode_ptr = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) ); |
---|
139 | file_type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) ); |
---|
140 | file_offset = hal_remote_l32( XPTR( file_cxy , &file_ptr->offset ) ); |
---|
141 | file_attr = hal_remote_l32( XPTR( file_cxy , &file_ptr->attr ) ); |
---|
142 | |
---|
143 | // enable IRQs |
---|
144 | hal_enable_irq( &save_sr ); |
---|
145 | |
---|
146 | // check file readable |
---|
147 | if( (file_attr & FD_ATTR_READ_ENABLE) == 0 ) |
---|
148 | { |
---|
149 | |
---|
150 | #if DEBUG_SYSCALLS_ERROR |
---|
151 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
152 | printk("\n[ERROR] in %s : thread[%x,%x] / file %d not readable\n", |
---|
153 | __FUNCTION__, process->pid, this->trdid, file_id ); |
---|
154 | #endif |
---|
155 | hal_restore_irq( save_sr ); |
---|
156 | this->errno = EBADFD; |
---|
157 | return -1; |
---|
158 | } |
---|
159 | |
---|
160 | // action depend on file type: |
---|
161 | if( file_type == FILE_TYPE_REG ) // read from mapper |
---|
162 | { |
---|
163 | // try to move count bytes from mapper |
---|
164 | nbytes = vfs_user_move( true, // from mapper |
---|
165 | file_xp, |
---|
166 | vaddr, |
---|
167 | count ); |
---|
168 | } |
---|
169 | else if( file_type == FILE_TYPE_DEV ) // read from TXT device |
---|
170 | { |
---|
171 | // get cluster and pointers on TXT_RX chdev |
---|
172 | xptr_t chdev_xp = chdev_from_file( file_xp ); |
---|
173 | cxy_t chdev_cxy = GET_CXY( chdev_xp ); |
---|
174 | chdev_t * chdev_ptr = GET_PTR( chdev_xp ); |
---|
175 | |
---|
176 | volatile xptr_t txt_owner_xp; |
---|
177 | uint32_t iter = 0; |
---|
178 | |
---|
179 | while( 1 ) |
---|
180 | { |
---|
181 | // extended pointer on TXT owner process |
---|
182 | txt_owner_xp = hal_remote_l64( XPTR( chdev_cxy , |
---|
183 | &chdev_ptr->ext.txt.owner_xp ) ); |
---|
184 | // wait for TXT_RX ownership |
---|
185 | if ( process_owner_xp != txt_owner_xp ) |
---|
186 | { |
---|
187 | if( (iter & 0xFFF) == 0 ) |
---|
188 | printk("\n[WARNING] in %s : thread[%x,%x] wait TXT_RX / cycle %d\n", |
---|
189 | __FUNCTION__, process->pid, this->trdid, (uint32_t)hal_get_cycles() ); |
---|
190 | |
---|
191 | // deschedule without blocking |
---|
192 | sched_yield( "wait TXT_RX ownership" ); |
---|
193 | |
---|
194 | iter++; |
---|
195 | } |
---|
196 | else |
---|
197 | { |
---|
198 | break; |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | // try to move count bytes from TXT device |
---|
203 | nbytes = devfs_user_move( true, // from device |
---|
204 | file_xp, |
---|
205 | vaddr, |
---|
206 | count ); |
---|
207 | } |
---|
208 | else if( (file_type == FILE_TYPE_PIPE) || |
---|
209 | (file_type == FILE_TYPE_FIFO) ) // read from pipe |
---|
210 | { |
---|
211 | // try to move count bytes from pipe |
---|
212 | nbytes = pipe_user_move( true, // from pipe |
---|
213 | file_xp, |
---|
214 | vaddr, |
---|
215 | count ); |
---|
216 | } |
---|
217 | else // unsupported type |
---|
218 | { |
---|
219 | |
---|
220 | #if DEBUG_SYSCALLS_ERROR |
---|
221 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
222 | printk("\n[ERROR] in %s : thread[%x,%x] / unsupported inode type %d\n", |
---|
223 | __FUNCTION__, vfs_inode_type_str( file_type ) ); |
---|
224 | #endif |
---|
225 | this->errno = EBADFD; |
---|
226 | hal_restore_irq( save_sr ); |
---|
227 | return -1; |
---|
228 | } |
---|
229 | |
---|
230 | // check error |
---|
231 | if( nbytes == 0xFFFFFFFF ) |
---|
232 | { |
---|
233 | |
---|
234 | #if DEBUG_SYSCALLS_ERROR |
---|
235 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
236 | printk("\n[ERROR] in %s : thread[%x,‰x] cannot read data from file %d\n", |
---|
237 | __FUNCTION__, process->pid, this->trdid, file_id ); |
---|
238 | #endif |
---|
239 | this->errno = EIO; |
---|
240 | hal_restore_irq( save_sr ); |
---|
241 | return -1; |
---|
242 | } |
---|
243 | |
---|
244 | // restore IRQs |
---|
245 | hal_restore_irq( save_sr ); |
---|
246 | |
---|
247 | hal_fence(); |
---|
248 | |
---|
249 | #if (DEBUG_SYS_READ || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
250 | uint64_t tm_end = hal_get_cycles(); |
---|
251 | #endif |
---|
252 | |
---|
253 | #if DEBUG_SYS_READ |
---|
254 | if( DEBUG_SYS_READ < tm_end ) |
---|
255 | printk("\n[%s] thread[%x,%x] exit / cycle %d\n", |
---|
256 | __FUNCTION__ , process->pid, this->trdid, (uint32_t)tm_end ); |
---|
257 | #endif |
---|
258 | |
---|
259 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
260 | hal_atomic_add( &syscalls_cumul_cost[SYS_READ] , tm_end - tm_start ); |
---|
261 | hal_atomic_add( &syscalls_occurences[SYS_READ] , 1 ); |
---|
262 | #endif |
---|
263 | |
---|
264 | #if (DEBUG_SYS_READ & 1) |
---|
265 | exit_sys_read = (uint32_t)tm_end; |
---|
266 | |
---|
267 | printk("\n***** timing to read one character *****\n" |
---|
268 | " - enter_sys_read = %d / delta %d\n" |
---|
269 | " - enter_devfs_read = %d / delta %d\n" |
---|
270 | " - enter_txt_read = %d / delta %d\n" |
---|
271 | " - enter_chdev_cmd_read = %d / delta %d\n" |
---|
272 | " - enter_chdev_server_read = %d / delta %d\n" |
---|
273 | " - enter_tty_cmd_read = %d / delta %d\n" |
---|
274 | " - enter_tty_isr_read = %d / delta %d\n" |
---|
275 | " - exit_tty_isr_read = %d / delta %d\n" |
---|
276 | " - exit_tty_cmd_read = %d / delta %d\n" |
---|
277 | " - exit_chdev_server_read = %d / delta %d\n" |
---|
278 | " - exit_chdev_cmd_read = %d / delta %d\n" |
---|
279 | " - exit_txt_read = %d / delta %d\n" |
---|
280 | " - exit_devfs_read = %d / delta %d\n" |
---|
281 | " - exit_sys_read = %d / delta %d\n", |
---|
282 | enter_sys_read , 0 , |
---|
283 | enter_devfs_read , enter_devfs_read - enter_sys_read , |
---|
284 | enter_txt_read , enter_txt_read - enter_devfs_read , |
---|
285 | enter_chdev_cmd_read , enter_chdev_cmd_read - enter_txt_read , |
---|
286 | enter_chdev_server_read , enter_chdev_server_read - enter_chdev_cmd_read , |
---|
287 | enter_tty_cmd_read , enter_tty_cmd_read - enter_chdev_server_read , |
---|
288 | enter_tty_isr_read , enter_tty_isr_read - enter_tty_cmd_read , |
---|
289 | exit_tty_isr_read , exit_tty_isr_read - enter_tty_isr_read , |
---|
290 | exit_tty_cmd_read , exit_tty_cmd_read - exit_tty_isr_read , |
---|
291 | exit_chdev_server_read , exit_chdev_server_read - exit_tty_cmd_read , |
---|
292 | exit_chdev_cmd_read , exit_chdev_cmd_read - exit_chdev_server_read , |
---|
293 | exit_txt_read , exit_txt_read - exit_chdev_cmd_read , |
---|
294 | exit_devfs_read , exit_devfs_read - exit_txt_read , |
---|
295 | exit_sys_read , exit_sys_read - exit_devfs_read ); |
---|
296 | #endif |
---|
297 | |
---|
298 | return nbytes; |
---|
299 | |
---|
300 | } // end sys_read() |
---|