[1] | 1 | /* |
---|
[23] | 2 | * sys_read.c - read bytes from a file |
---|
[1] | 3 | * |
---|
[23] | 4 | * Author Alain Greiner (2016,2017) |
---|
[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> |
---|
| 25 | #include <hal_types.h> |
---|
| 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 | |
---|
[407] | 36 | // TODO: concurrent user page(s) munmap need to be handled [AG] |
---|
[23] | 37 | |
---|
[409] | 38 | // TODO : remove these debug variables |
---|
[407] | 39 | extern uint32_t enter_sys_read; |
---|
| 40 | extern uint32_t enter_devfs_move; |
---|
| 41 | extern uint32_t enter_txt_read; |
---|
| 42 | extern uint32_t enter_chdev_cmd; |
---|
| 43 | extern uint32_t enter_chdev_server; |
---|
| 44 | extern uint32_t enter_tty_cmd; |
---|
| 45 | extern uint32_t enter_tty_isr; |
---|
| 46 | extern uint32_t exit_tty_isr; |
---|
| 47 | extern uint32_t exit_tty_cmd; |
---|
| 48 | extern uint32_t exit_chdev_server; |
---|
| 49 | extern uint32_t exit_chdev_cmd; |
---|
| 50 | extern uint32_t exit_txt_read; |
---|
| 51 | extern uint32_t exit_devfs_move; |
---|
| 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 | { |
---|
[23] | 60 | error_t error; |
---|
[313] | 61 | paddr_t paddr; // required for user space checking |
---|
[23] | 62 | xptr_t file_xp; // remote file extended pointer |
---|
[407] | 63 | uint32_t nbytes; // number of bytes actually read |
---|
[408] | 64 | reg_t save_sr; // required to enable IRQs during syscall |
---|
[407] | 65 | |
---|
[418] | 66 | #if CONFIG_READ_DEBUG |
---|
| 67 | uint64_t tm_start; |
---|
| 68 | uint64_t tm_end; |
---|
[409] | 69 | tm_start = hal_get_cycles(); |
---|
| 70 | #endif |
---|
[407] | 71 | |
---|
[409] | 72 | #if CONFIG_READ_DEBUG |
---|
[418] | 73 | enter_sys_read = (uint32_t)tm_start; |
---|
[407] | 74 | #endif |
---|
| 75 | |
---|
[23] | 76 | thread_t * this = CURRENT_THREAD; |
---|
| 77 | process_t * process = this->process; |
---|
| 78 | |
---|
| 79 | // check file_id argument |
---|
| 80 | if( file_id >= CONFIG_PROCESS_FILE_MAX_NR ) |
---|
[1] | 81 | { |
---|
[23] | 82 | printk("\n[ERROR] in %s : illegal file descriptor index = %d\n", |
---|
[407] | 83 | __FUNCTION__ , file_id ); |
---|
[23] | 84 | this->errno = EBADFD; |
---|
[1] | 85 | return -1; |
---|
| 86 | } |
---|
| 87 | |
---|
[23] | 88 | // check user buffer in user space |
---|
[407] | 89 | error = vmm_v2p_translate( false , vaddr , &paddr ); |
---|
[23] | 90 | |
---|
| 91 | if ( error ) |
---|
| 92 | { |
---|
| 93 | printk("\n[ERROR] in %s : user buffer unmapped = %x\n", |
---|
[407] | 94 | __FUNCTION__ , (intptr_t)vaddr ); |
---|
[23] | 95 | this->errno = EINVAL; |
---|
| 96 | return -1; |
---|
| 97 | } |
---|
| 98 | |
---|
[408] | 99 | // enable IRQs |
---|
[421] | 100 | hal_enable_irq( &save_sr ); |
---|
[408] | 101 | |
---|
[23] | 102 | // get extended pointer on remote file descriptor |
---|
| 103 | file_xp = process_fd_get_xptr( process , file_id ); |
---|
| 104 | |
---|
| 105 | if( file_xp == XPTR_NULL ) |
---|
| 106 | { |
---|
[407] | 107 | printk("\n[ERROR] in %s : undefined file descriptor index = %d in process %x\n", |
---|
| 108 | __FUNCTION__ , file_id , process->pid ); |
---|
[23] | 109 | this->errno = EBADFD; |
---|
| 110 | return -1; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | // get file descriptor cluster and local pointer |
---|
| 114 | vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); |
---|
| 115 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
| 116 | |
---|
| 117 | // check file readable |
---|
| 118 | uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) ); |
---|
| 119 | if( (attr & FD_ATTR_READ_ENABLE) == 0 ) |
---|
[1] | 120 | { |
---|
[407] | 121 | printk("\n[ERROR] in %s : file %d not readable in process %x\n", |
---|
| 122 | __FUNCTION__ , file_id , process->pid ); |
---|
[23] | 123 | this->errno = EBADFD; |
---|
[1] | 124 | return -1; |
---|
| 125 | } |
---|
[313] | 126 | |
---|
[407] | 127 | // get file type |
---|
| 128 | vfs_inode_type_t type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) ); |
---|
[313] | 129 | |
---|
[407] | 130 | // action depend on file type |
---|
[421] | 131 | if( type == INODE_TYPE_FILE ) // check file readable & read from mapper |
---|
[23] | 132 | { |
---|
[421] | 133 | // check file readable |
---|
| 134 | uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) ); |
---|
| 135 | if( (attr & FD_ATTR_READ_ENABLE) == 0 ) |
---|
| 136 | { |
---|
| 137 | printk("\n[ERROR] in %s : file %d not readable in process %x\n", |
---|
| 138 | __FUNCTION__ , file_id , process->pid ); |
---|
| 139 | this->errno = EBADFD; |
---|
| 140 | return -1; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | // move count bytes from mapper |
---|
[407] | 144 | nbytes = vfs_user_move( true, // from mapper to buffer |
---|
| 145 | file_xp, |
---|
| 146 | vaddr, |
---|
| 147 | count ); |
---|
| 148 | } |
---|
[421] | 149 | else if( type == INODE_TYPE_DEV ) // check ownership & read from from device |
---|
[407] | 150 | { |
---|
[421] | 151 | // move count bytes from device |
---|
[407] | 152 | nbytes = devfs_user_move( true, // from device to buffer |
---|
| 153 | file_xp, |
---|
| 154 | vaddr, |
---|
| 155 | count ); |
---|
[421] | 156 | |
---|
| 157 | // check ownership |
---|
| 158 | xptr_t chdev_xp = chdev_from_file( file_xp ); |
---|
| 159 | cxy_t chdev_cxy = GET_CXY( chdev_xp ); |
---|
| 160 | chdev_t * chdev_ptr = (chdev_t *)GET_PTR( chdev_xp ); |
---|
| 161 | xptr_t owner_xp = hal_remote_lwd( XPTR( chdev_cxy , &chdev_ptr->ext.txt.owner_xp ) ); |
---|
| 162 | |
---|
| 163 | if( XPTR( local_cxy , process ) != owner_xp ) |
---|
| 164 | { |
---|
| 165 | printk("\n[ERROR] in %s : process %x not in foreground for TXT%d\n", |
---|
| 166 | __FUNCTION__, process->pid, hal_remote_lw( XPTR(chdev_cxy,&chdev_ptr->channel) ) ); |
---|
| 167 | this->errno = EBADFD; |
---|
| 168 | return -1; |
---|
| 169 | } |
---|
[407] | 170 | } |
---|
| 171 | else |
---|
| 172 | { |
---|
| 173 | nbytes = 0; |
---|
[421] | 174 | assert( false , __FUNCTION__ , "file type %d non supported yet\n", type ); |
---|
[407] | 175 | } |
---|
| 176 | |
---|
| 177 | if( nbytes != count ) |
---|
| 178 | { |
---|
| 179 | printk("\n[ERROR] in %s cannot read data from file %d in process %x\n", |
---|
| 180 | __FUNCTION__ , file_id , process->pid ); |
---|
[313] | 181 | this->errno = error; |
---|
| 182 | return -1; |
---|
[23] | 183 | } |
---|
| 184 | |
---|
[408] | 185 | // restore IRQs |
---|
[421] | 186 | hal_restore_irq( save_sr ); |
---|
[408] | 187 | |
---|
[124] | 188 | hal_fence(); |
---|
[23] | 189 | |
---|
[418] | 190 | #if CONFIG_READ_DEBUG |
---|
[409] | 191 | tm_end = hal_get_cycles(); |
---|
[416] | 192 | printk("\n[DBG] %s : core[%x,%d] / thread %x in process %x / cycle %d\n" |
---|
[409] | 193 | "nbytes = %d / first byte = %c / file_id = %d / cost = %d\n", |
---|
| 194 | __FUNCTION__ , local_cxy , this->core->lid , this->trdid , this->process->pid , |
---|
[416] | 195 | (uint32_t)tm_start , nbytes , *((char *)(intptr_t)paddr) , file_id , |
---|
| 196 | (uint32_t)(tm_end - tm_start) ); |
---|
[409] | 197 | #endif |
---|
[23] | 198 | |
---|
[421] | 199 | #if (CONFIG_READ_DEBUG & 0x1) |
---|
[418] | 200 | exit_sys_read = (uint32_t)tm_end; |
---|
[407] | 201 | |
---|
[409] | 202 | printk("\n@@@@@@@@@@@@ timing to read character %c\n" |
---|
[407] | 203 | " - enter_sys_read = %d / delta %d\n" |
---|
| 204 | " - enter_devfs_move = %d / delta %d\n" |
---|
| 205 | " - enter_txt_read = %d / delta %d\n" |
---|
| 206 | " - enter_chdev_cmd = %d / delta %d\n" |
---|
| 207 | " - enter_chdev_server = %d / delta %d\n" |
---|
| 208 | " - enter_tty_cmd = %d / delta %d\n" |
---|
| 209 | " - enter_tty_isr = %d / delta %d\n" |
---|
| 210 | " - exit_tty_isr = %d / delta %d\n" |
---|
| 211 | " - exit_tty_cmd = %d / delta %d\n" |
---|
| 212 | " - exit_chdev_server = %d / delta %d\n" |
---|
| 213 | " - exit_chdev_cmd = %d / delta %d\n" |
---|
| 214 | " - exit_txt_read = %d / delta %d\n" |
---|
| 215 | " - exit_devfs_move = %d / delta %d\n" |
---|
| 216 | " - exit_sys_read = %d / delta %d\n", |
---|
| 217 | *((char *)(intptr_t)paddr) , |
---|
| 218 | enter_sys_read , 0 , |
---|
| 219 | enter_devfs_move , enter_devfs_move - enter_sys_read , |
---|
| 220 | enter_txt_read , enter_txt_read - enter_devfs_move , |
---|
| 221 | enter_chdev_cmd , enter_chdev_cmd - enter_txt_read , |
---|
| 222 | enter_chdev_server , enter_chdev_server - enter_chdev_cmd , |
---|
| 223 | enter_tty_cmd , enter_tty_cmd - enter_chdev_server , |
---|
| 224 | enter_tty_isr , enter_tty_isr - enter_tty_cmd , |
---|
| 225 | exit_tty_isr , exit_tty_isr - enter_tty_isr , |
---|
| 226 | exit_tty_cmd , exit_tty_cmd - exit_tty_isr , |
---|
| 227 | exit_chdev_server , exit_chdev_server - exit_tty_cmd , |
---|
| 228 | exit_chdev_cmd , exit_chdev_cmd - exit_chdev_server , |
---|
| 229 | exit_txt_read , exit_txt_read - exit_chdev_cmd , |
---|
| 230 | exit_devfs_move , exit_devfs_move - exit_txt_read , |
---|
| 231 | exit_sys_read , exit_sys_read - exit_devfs_move ); |
---|
| 232 | #endif |
---|
| 233 | |
---|
| 234 | return nbytes; |
---|
| 235 | |
---|
[23] | 236 | } // end sys_read() |
---|