[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> |
---|
| 27 | #include <hal_special.h> |
---|
[1] | 28 | #include <errno.h> |
---|
[23] | 29 | #include <vfs.h> |
---|
| 30 | #include <vmm.h> |
---|
[1] | 31 | #include <thread.h> |
---|
[23] | 32 | #include <printk.h> |
---|
| 33 | #include <process.h> |
---|
[1] | 34 | |
---|
[407] | 35 | // TODO: concurrent user page(s) munmap need to be handled [AG] |
---|
[23] | 36 | |
---|
[407] | 37 | // instrumentation |
---|
| 38 | extern uint32_t enter_sys_read; |
---|
| 39 | extern uint32_t enter_devfs_move; |
---|
| 40 | extern uint32_t enter_txt_read; |
---|
| 41 | extern uint32_t enter_chdev_cmd; |
---|
| 42 | extern uint32_t enter_chdev_server; |
---|
| 43 | extern uint32_t enter_tty_cmd; |
---|
| 44 | extern uint32_t enter_tty_isr; |
---|
| 45 | extern uint32_t exit_tty_isr; |
---|
| 46 | extern uint32_t exit_tty_cmd; |
---|
| 47 | extern uint32_t exit_chdev_server; |
---|
| 48 | extern uint32_t exit_chdev_cmd; |
---|
| 49 | extern uint32_t exit_txt_read; |
---|
| 50 | extern uint32_t exit_devfs_move; |
---|
| 51 | extern uint32_t exit_sys_read; |
---|
| 52 | |
---|
| 53 | |
---|
[23] | 54 | ///////////////////////////////// |
---|
| 55 | int sys_read( uint32_t file_id, |
---|
[407] | 56 | void * vaddr, |
---|
[23] | 57 | uint32_t count ) |
---|
[1] | 58 | { |
---|
[23] | 59 | error_t error; |
---|
[313] | 60 | paddr_t paddr; // required for user space checking |
---|
[23] | 61 | xptr_t file_xp; // remote file extended pointer |
---|
[407] | 62 | uint32_t nbytes; // number of bytes actually read |
---|
[1] | 63 | |
---|
[407] | 64 | uint32_t tm_start; |
---|
| 65 | uint32_t tm_end; |
---|
| 66 | |
---|
| 67 | tm_start = hal_get_cycles(); |
---|
| 68 | |
---|
| 69 | #if CONFIG_READ_START |
---|
| 70 | enter_sys_read = tm_start; |
---|
| 71 | #endif |
---|
| 72 | |
---|
[23] | 73 | thread_t * this = CURRENT_THREAD; |
---|
| 74 | process_t * process = this->process; |
---|
| 75 | |
---|
| 76 | // check file_id argument |
---|
| 77 | if( file_id >= CONFIG_PROCESS_FILE_MAX_NR ) |
---|
[1] | 78 | { |
---|
[23] | 79 | printk("\n[ERROR] in %s : illegal file descriptor index = %d\n", |
---|
[407] | 80 | __FUNCTION__ , file_id ); |
---|
[23] | 81 | this->errno = EBADFD; |
---|
[1] | 82 | return -1; |
---|
| 83 | } |
---|
| 84 | |
---|
[23] | 85 | // check user buffer in user space |
---|
[407] | 86 | error = vmm_v2p_translate( false , vaddr , &paddr ); |
---|
[23] | 87 | |
---|
| 88 | if ( error ) |
---|
| 89 | { |
---|
| 90 | printk("\n[ERROR] in %s : user buffer unmapped = %x\n", |
---|
[407] | 91 | __FUNCTION__ , (intptr_t)vaddr ); |
---|
[23] | 92 | this->errno = EINVAL; |
---|
| 93 | return -1; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | // get extended pointer on remote file descriptor |
---|
| 97 | file_xp = process_fd_get_xptr( process , file_id ); |
---|
| 98 | |
---|
| 99 | if( file_xp == XPTR_NULL ) |
---|
| 100 | { |
---|
[407] | 101 | printk("\n[ERROR] in %s : undefined file descriptor index = %d in process %x\n", |
---|
| 102 | __FUNCTION__ , file_id , process->pid ); |
---|
[23] | 103 | this->errno = EBADFD; |
---|
| 104 | return -1; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // get file descriptor cluster and local pointer |
---|
| 108 | vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); |
---|
| 109 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
| 110 | |
---|
| 111 | // check file readable |
---|
| 112 | uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) ); |
---|
| 113 | if( (attr & FD_ATTR_READ_ENABLE) == 0 ) |
---|
[1] | 114 | { |
---|
[407] | 115 | printk("\n[ERROR] in %s : file %d not readable in process %x\n", |
---|
| 116 | __FUNCTION__ , file_id , process->pid ); |
---|
[23] | 117 | this->errno = EBADFD; |
---|
[1] | 118 | return -1; |
---|
| 119 | } |
---|
[313] | 120 | |
---|
[407] | 121 | // get file type |
---|
| 122 | vfs_inode_type_t type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) ); |
---|
[313] | 123 | |
---|
[407] | 124 | // action depend on file type |
---|
| 125 | if( type == INODE_TYPE_FILE ) // transfer count bytes from file mapper |
---|
[23] | 126 | { |
---|
[407] | 127 | nbytes = vfs_user_move( true, // from mapper to buffer |
---|
| 128 | file_xp, |
---|
| 129 | vaddr, |
---|
| 130 | count ); |
---|
| 131 | } |
---|
| 132 | else if( type == INODE_TYPE_DEV ) // transfer count bytes from device |
---|
| 133 | { |
---|
| 134 | nbytes = devfs_user_move( true, // from device to buffer |
---|
| 135 | file_xp, |
---|
| 136 | vaddr, |
---|
| 137 | count ); |
---|
| 138 | } |
---|
| 139 | else |
---|
| 140 | { |
---|
| 141 | nbytes = 0; |
---|
| 142 | panic("file type %d non supported yet", type ); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | if( nbytes != count ) |
---|
| 146 | { |
---|
| 147 | printk("\n[ERROR] in %s cannot read data from file %d in process %x\n", |
---|
| 148 | __FUNCTION__ , file_id , process->pid ); |
---|
[313] | 149 | this->errno = error; |
---|
| 150 | return -1; |
---|
[23] | 151 | } |
---|
| 152 | |
---|
[124] | 153 | hal_fence(); |
---|
[23] | 154 | |
---|
[407] | 155 | tm_end = hal_get_cycles(); |
---|
[23] | 156 | |
---|
[407] | 157 | #if CONFIG_READ_DEBUG |
---|
| 158 | exit_sys_read = tm_end; |
---|
| 159 | |
---|
| 160 | printk("\n@@@@@@@@@@@@ timing ro read character %c\n" |
---|
| 161 | " - enter_sys_read = %d / delta %d\n" |
---|
| 162 | " - enter_devfs_move = %d / delta %d\n" |
---|
| 163 | " - enter_txt_read = %d / delta %d\n" |
---|
| 164 | " - enter_chdev_cmd = %d / delta %d\n" |
---|
| 165 | " - enter_chdev_server = %d / delta %d\n" |
---|
| 166 | " - enter_tty_cmd = %d / delta %d\n" |
---|
| 167 | " - enter_tty_isr = %d / delta %d\n" |
---|
| 168 | " - exit_tty_isr = %d / delta %d\n" |
---|
| 169 | " - exit_tty_cmd = %d / delta %d\n" |
---|
| 170 | " - exit_chdev_server = %d / delta %d\n" |
---|
| 171 | " - exit_chdev_cmd = %d / delta %d\n" |
---|
| 172 | " - exit_txt_read = %d / delta %d\n" |
---|
| 173 | " - exit_devfs_move = %d / delta %d\n" |
---|
| 174 | " - exit_sys_read = %d / delta %d\n", |
---|
| 175 | *((char *)(intptr_t)paddr) , |
---|
| 176 | enter_sys_read , 0 , |
---|
| 177 | enter_devfs_move , enter_devfs_move - enter_sys_read , |
---|
| 178 | enter_txt_read , enter_txt_read - enter_devfs_move , |
---|
| 179 | enter_chdev_cmd , enter_chdev_cmd - enter_txt_read , |
---|
| 180 | enter_chdev_server , enter_chdev_server - enter_chdev_cmd , |
---|
| 181 | enter_tty_cmd , enter_tty_cmd - enter_chdev_server , |
---|
| 182 | enter_tty_isr , enter_tty_isr - enter_tty_cmd , |
---|
| 183 | exit_tty_isr , exit_tty_isr - enter_tty_isr , |
---|
| 184 | exit_tty_cmd , exit_tty_cmd - exit_tty_isr , |
---|
| 185 | exit_chdev_server , exit_chdev_server - exit_tty_cmd , |
---|
| 186 | exit_chdev_cmd , exit_chdev_cmd - exit_chdev_server , |
---|
| 187 | exit_txt_read , exit_txt_read - exit_chdev_cmd , |
---|
| 188 | exit_devfs_move , exit_devfs_move - exit_txt_read , |
---|
| 189 | exit_sys_read , exit_sys_read - exit_devfs_move ); |
---|
| 190 | #endif |
---|
| 191 | |
---|
| 192 | syscall_dmsg("\n[DBG] %s : core[%x,%d] / thread %x / nbytes = %d / cycle %d\n" |
---|
| 193 | " first byte = %c / file_id = %d / cost = %d\n", |
---|
| 194 | __FUNCTION__ , local_cxy , this->core->lid , this->trdid , nbytes , tm_start , |
---|
| 195 | *((char *)(intptr_t)paddr) , file_id , tm_end - tm_start ); |
---|
| 196 | |
---|
| 197 | return nbytes; |
---|
| 198 | |
---|
[23] | 199 | } // end sys_read() |
---|