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