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