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