[1] | 1 | /* |
---|
[625] | 2 | * sys_read.c - Kernel function implementing the "read" system call. |
---|
[1] | 3 | * |
---|
[670] | 4 | * Author Alain Greiner (2016,2017,2018,2019,2020) |
---|
[1] | 5 | * |
---|
[23] | 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
[1] | 7 | * |
---|
[23] | 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
[1] | 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 | * |
---|
[23] | 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
[1] | 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 |
---|
[23] | 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
[1] | 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
[23] | 24 | #include <kernel_config.h> |
---|
[457] | 25 | #include <hal_kernel_types.h> |
---|
[625] | 26 | #include <hal_vmm.h> |
---|
[23] | 27 | #include <hal_uspace.h> |
---|
[409] | 28 | #include <hal_irqmask.h> |
---|
[23] | 29 | #include <hal_special.h> |
---|
[1] | 30 | #include <errno.h> |
---|
[23] | 31 | #include <vfs.h> |
---|
| 32 | #include <vmm.h> |
---|
[1] | 33 | #include <thread.h> |
---|
[23] | 34 | #include <printk.h> |
---|
[670] | 35 | #include <pipe.h> |
---|
[23] | 36 | #include <process.h> |
---|
[1] | 37 | |
---|
[506] | 38 | |
---|
[407] | 39 | extern uint32_t enter_sys_read; |
---|
[436] | 40 | extern uint32_t enter_devfs_read; |
---|
[407] | 41 | extern uint32_t enter_txt_read; |
---|
[436] | 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; |
---|
[407] | 50 | extern uint32_t exit_txt_read; |
---|
[436] | 51 | extern uint32_t exit_devfs_read; |
---|
[407] | 52 | extern uint32_t exit_sys_read; |
---|
| 53 | |
---|
| 54 | |
---|
[23] | 55 | ///////////////////////////////// |
---|
| 56 | int sys_read( uint32_t file_id, |
---|
[407] | 57 | void * vaddr, |
---|
[23] | 58 | uint32_t count ) |
---|
[1] | 59 | { |
---|
[604] | 60 | error_t error; |
---|
| 61 | vseg_t * vseg; // required for user space checking |
---|
[633] | 62 | xptr_t file_xp; // remote file extended pointer |
---|
[604] | 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 |
---|
[656] | 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 |
---|
[604] | 69 | uint32_t nbytes; // number of bytes actually read |
---|
| 70 | reg_t save_sr; // required to enable IRQs during syscall |
---|
[407] | 71 | |
---|
[604] | 72 | thread_t * this = CURRENT_THREAD; |
---|
| 73 | process_t * process = this->process; |
---|
| 74 | xptr_t process_owner_xp = process->owner_xp; |
---|
[23] | 75 | |
---|
[670] | 76 | #if DEBUG_SYS_READ || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS |
---|
[566] | 77 | uint64_t tm_start = hal_get_cycles(); |
---|
| 78 | #endif |
---|
| 79 | |
---|
[438] | 80 | #if DEBUG_SYS_READ |
---|
| 81 | if( DEBUG_SYS_READ < tm_start ) |
---|
[610] | 82 | printk("\n[%s] thread[%x,%x] enter / vaddr %x / count %d / cycle %d\n", |
---|
[584] | 83 | __FUNCTION__, process->pid, this->trdid, vaddr, count, (uint32_t)tm_start ); |
---|
[433] | 84 | #endif |
---|
| 85 | |
---|
[438] | 86 | #if (DEBUG_SYS_READ & 1) |
---|
[436] | 87 | enter_sys_read = (uint32_t)tm_start; |
---|
| 88 | #endif |
---|
| 89 | |
---|
[23] | 90 | // check file_id argument |
---|
| 91 | if( file_id >= CONFIG_PROCESS_FILE_MAX_NR ) |
---|
[1] | 92 | { |
---|
[435] | 93 | |
---|
[438] | 94 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 95 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
| 96 | printk("\n[ERROR] in %s : thread[%x,%x] / illegal file descriptor index %d\n", |
---|
[584] | 97 | __FUNCTION__ , process->pid, this->trdid, file_id ); |
---|
[435] | 98 | #endif |
---|
[23] | 99 | this->errno = EBADFD; |
---|
[1] | 100 | return -1; |
---|
| 101 | } |
---|
| 102 | |
---|
[23] | 103 | // check user buffer in user space |
---|
[440] | 104 | error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg ); |
---|
[23] | 105 | |
---|
| 106 | if ( error ) |
---|
| 107 | { |
---|
[435] | 108 | |
---|
[438] | 109 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 110 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
| 111 | printk("\n[ERROR] in %s : thread[%x,%x] / user buffer unmapped %x\n", |
---|
[584] | 112 | __FUNCTION__ , process->pid, this->trdid, (intptr_t)vaddr ); |
---|
[435] | 113 | #endif |
---|
[23] | 114 | this->errno = EINVAL; |
---|
| 115 | return -1; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | // get extended pointer on remote file descriptor |
---|
[664] | 119 | file_xp = process_fd_get_xptr_from_local( process , file_id ); |
---|
[23] | 120 | |
---|
| 121 | if( file_xp == XPTR_NULL ) |
---|
| 122 | { |
---|
[435] | 123 | |
---|
[438] | 124 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 125 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
| 126 | printk("\n[ERROR] in %s : thread[%x,%x] / undefined fd_id %d\n", |
---|
[584] | 127 | __FUNCTION__, process->pid, this->trdid, file_id ); |
---|
[435] | 128 | #endif |
---|
[23] | 129 | this->errno = EBADFD; |
---|
| 130 | return -1; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | // get file descriptor cluster and local pointer |
---|
[604] | 134 | file_ptr = GET_PTR( file_xp ); |
---|
| 135 | file_cxy = GET_CXY( file_xp ); |
---|
[23] | 136 | |
---|
[656] | 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 ) ); |
---|
[313] | 142 | |
---|
[604] | 143 | // enable IRQs |
---|
| 144 | hal_enable_irq( &save_sr ); |
---|
| 145 | |
---|
[670] | 146 | // check file readable |
---|
| 147 | if( (file_attr & FD_ATTR_READ_ENABLE) == 0 ) |
---|
[23] | 148 | { |
---|
[435] | 149 | |
---|
[438] | 150 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 151 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
| 152 | printk("\n[ERROR] in %s : thread[%x,%x] / file %d not readable\n", |
---|
[584] | 153 | __FUNCTION__, process->pid, this->trdid, file_id ); |
---|
[435] | 154 | #endif |
---|
[670] | 155 | hal_restore_irq( save_sr ); |
---|
| 156 | this->errno = EBADFD; |
---|
| 157 | return -1; |
---|
| 158 | } |
---|
[421] | 159 | |
---|
[670] | 160 | // action depend on file type: |
---|
| 161 | if( file_type == FILE_TYPE_REG ) // read from mapper |
---|
| 162 | { |
---|
[656] | 163 | // try to move count bytes from mapper |
---|
[670] | 164 | nbytes = vfs_user_move( true, // from mapper |
---|
[407] | 165 | file_xp, |
---|
| 166 | vaddr, |
---|
| 167 | count ); |
---|
| 168 | } |
---|
[670] | 169 | else if( file_type == FILE_TYPE_DEV ) // read from TXT device |
---|
[407] | 170 | { |
---|
[436] | 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 | |
---|
[446] | 176 | volatile xptr_t txt_owner_xp; |
---|
| 177 | uint32_t iter = 0; |
---|
[436] | 178 | |
---|
| 179 | while( 1 ) |
---|
| 180 | { |
---|
[446] | 181 | // extended pointer on TXT owner process |
---|
[670] | 182 | txt_owner_xp = hal_remote_l64( XPTR( chdev_cxy , |
---|
| 183 | &chdev_ptr->ext.txt.owner_xp ) ); |
---|
[656] | 184 | // wait for TXT_RX ownership |
---|
[446] | 185 | if ( process_owner_xp != txt_owner_xp ) |
---|
[436] | 186 | { |
---|
[446] | 187 | if( (iter & 0xFFF) == 0 ) |
---|
[584] | 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() ); |
---|
[446] | 190 | |
---|
[436] | 191 | // deschedule without blocking |
---|
[446] | 192 | sched_yield( "wait TXT_RX ownership" ); |
---|
| 193 | |
---|
| 194 | iter++; |
---|
[436] | 195 | } |
---|
| 196 | else |
---|
| 197 | { |
---|
| 198 | break; |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
[656] | 202 | // try to move count bytes from TXT device |
---|
[670] | 203 | nbytes = devfs_user_move( true, // from device |
---|
[407] | 204 | file_xp, |
---|
| 205 | vaddr, |
---|
| 206 | count ); |
---|
| 207 | } |
---|
[670] | 208 | else if( (file_type == FILE_TYPE_PIPE) || |
---|
| 209 | (file_type == FILE_TYPE_FIFO) ) // read from pipe |
---|
[407] | 210 | { |
---|
[670] | 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 | { |
---|
[594] | 219 | |
---|
| 220 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 221 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
| 222 | printk("\n[ERROR] in %s : thread[%x,%x] / unsupported inode type %d\n", |
---|
[604] | 223 | __FUNCTION__, vfs_inode_type_str( file_type ) ); |
---|
[594] | 224 | #endif |
---|
| 225 | this->errno = EBADFD; |
---|
[604] | 226 | hal_restore_irq( save_sr ); |
---|
[594] | 227 | return -1; |
---|
[407] | 228 | } |
---|
| 229 | |
---|
[656] | 230 | // check error |
---|
| 231 | if( nbytes == 0xFFFFFFFF ) |
---|
| 232 | { |
---|
| 233 | |
---|
| 234 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 235 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[656] | 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 | |
---|
[408] | 244 | // restore IRQs |
---|
[421] | 245 | hal_restore_irq( save_sr ); |
---|
[408] | 246 | |
---|
[124] | 247 | hal_fence(); |
---|
[23] | 248 | |
---|
[566] | 249 | #if (DEBUG_SYS_READ || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 250 | uint64_t tm_end = hal_get_cycles(); |
---|
| 251 | #endif |
---|
| 252 | |
---|
[438] | 253 | #if DEBUG_SYS_READ |
---|
| 254 | if( DEBUG_SYS_READ < tm_end ) |
---|
[610] | 255 | printk("\n[%s] thread[%x,%x] exit / cycle %d\n", |
---|
[584] | 256 | __FUNCTION__ , process->pid, this->trdid, (uint32_t)tm_end ); |
---|
[409] | 257 | #endif |
---|
[23] | 258 | |
---|
[566] | 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 | |
---|
[438] | 264 | #if (DEBUG_SYS_READ & 1) |
---|
[418] | 265 | exit_sys_read = (uint32_t)tm_end; |
---|
[407] | 266 | |
---|
[443] | 267 | printk("\n***** timing to read one character *****\n" |
---|
[435] | 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 ); |
---|
[407] | 296 | #endif |
---|
| 297 | |
---|
| 298 | return nbytes; |
---|
| 299 | |
---|
[23] | 300 | } // end sys_read() |
---|