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