[1] | 1 | /* |
---|
[566] | 2 | * sys_write.c - Kernel function implementing the "write" system call. |
---|
[1] | 3 | * |
---|
[670] | 4 | * Author Alain Greiner (2016,2017,2018,2019,2020) |
---|
[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> |
---|
[624] | 26 | #include <hal_vmm.h> |
---|
[23] | 27 | #include <hal_uspace.h> |
---|
[409] | 28 | #include <hal_irqmask.h> |
---|
[23] | 29 | #include <hal_special.h> |
---|
[1] | 30 | #include <errno.h> |
---|
| 31 | #include <vfs.h> |
---|
[566] | 32 | #include <vmm.h> |
---|
[1] | 33 | #include <thread.h> |
---|
[23] | 34 | #include <printk.h> |
---|
[670] | 35 | #include <pipe.h> |
---|
[23] | 36 | #include <process.h> |
---|
[1] | 37 | |
---|
[23] | 38 | |
---|
[440] | 39 | extern uint32_t enter_sys_write; |
---|
| 40 | extern uint32_t enter_devfs_write; |
---|
| 41 | extern uint32_t enter_txt_write; |
---|
| 42 | extern uint32_t enter_chdev_cmd_write; |
---|
| 43 | extern uint32_t enter_chdev_server_write; |
---|
| 44 | extern uint32_t enter_tty_cmd_write; |
---|
| 45 | extern uint32_t enter_tty_isr_write; |
---|
| 46 | extern uint32_t exit_tty_isr_write; |
---|
| 47 | extern uint32_t exit_tty_cmd_write; |
---|
| 48 | extern uint32_t exit_chdev_server_write; |
---|
| 49 | extern uint32_t exit_chdev_cmd_write; |
---|
| 50 | extern uint32_t exit_txt_write; |
---|
| 51 | extern uint32_t exit_devfs_write; |
---|
| 52 | extern uint32_t exit_sys_write; |
---|
| 53 | |
---|
[23] | 54 | ////////////////////////////////// |
---|
| 55 | int sys_write( uint32_t file_id, |
---|
[407] | 56 | void * vaddr, |
---|
[23] | 57 | uint32_t count ) |
---|
[1] | 58 | { |
---|
[604] | 59 | error_t error; |
---|
| 60 | vseg_t * vseg; // required for user space checking |
---|
| 61 | xptr_t file_xp; // remote file extended pointer |
---|
| 62 | vfs_file_t * file_ptr; // remote file local pointer |
---|
| 63 | cxy_t file_cxy; // remote file cluster identifier |
---|
| 64 | uint32_t file_type; // file type |
---|
[656] | 65 | uint32_t file_offset; // file offset |
---|
| 66 | uint32_t file_attr; // file attributes |
---|
[604] | 67 | vfs_inode_t * inode_ptr; // local pointer on associated inode |
---|
| 68 | uint32_t nbytes; // number of bytes actually written |
---|
| 69 | reg_t save_sr; // required to enable IRQs during syscall |
---|
[407] | 70 | |
---|
[604] | 71 | thread_t * this = CURRENT_THREAD; |
---|
| 72 | process_t * process = this->process; |
---|
[433] | 73 | |
---|
[670] | 74 | #if DEBUG_SYS_WRITE || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS |
---|
[566] | 75 | uint64_t tm_start = hal_get_cycles(); |
---|
| 76 | #endif |
---|
| 77 | |
---|
[438] | 78 | #if DEBUG_SYS_WRITE |
---|
| 79 | if( DEBUG_SYS_WRITE < tm_start ) |
---|
[625] | 80 | printk("\n[%s] thread[%x,%x] enter / vaddr %x / %d bytes / cycle %d\n", |
---|
[584] | 81 | __FUNCTION__, process->pid, this->trdid, vaddr, count, (uint32_t)tm_start ); |
---|
[409] | 82 | #endif |
---|
[1] | 83 | |
---|
[469] | 84 | #if (DEBUG_SYS_WRITE & 1) |
---|
[664] | 85 | if( DEBUG_SYS_WRITE < tm_start ) |
---|
[469] | 86 | enter_sys_write = (uint32_t)tm_start; |
---|
| 87 | #endif |
---|
| 88 | |
---|
[23] | 89 | // check file_id argument |
---|
| 90 | if( file_id >= CONFIG_PROCESS_FILE_MAX_NR ) |
---|
[1] | 91 | { |
---|
[433] | 92 | |
---|
[438] | 93 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 94 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
| 95 | printk("\n[ERROR] in %s : thread[%x,%x] / illegal file descriptor index %d\n", |
---|
[584] | 96 | __FUNCTION__, process->pid, this->trdid, file_id ); |
---|
[433] | 97 | #endif |
---|
[23] | 98 | this->errno = EBADFD; |
---|
[1] | 99 | return -1; |
---|
| 100 | } |
---|
| 101 | |
---|
[23] | 102 | // check user buffer in user space |
---|
[440] | 103 | error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg ); |
---|
[23] | 104 | |
---|
| 105 | if ( error ) |
---|
| 106 | { |
---|
[433] | 107 | |
---|
[438] | 108 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 109 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
| 110 | printk("\n[ERROR] in %s : thread[%x,%x] / user buffer unmapped %x\n", |
---|
[584] | 111 | __FUNCTION__ , process->pid, this->trdid, (intptr_t)vaddr ); |
---|
[433] | 112 | #endif |
---|
[23] | 113 | this->errno = EINVAL; |
---|
| 114 | return -1; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | // get extended pointer on remote file descriptor |
---|
[664] | 118 | file_xp = process_fd_get_xptr_from_local( process , file_id ); |
---|
[23] | 119 | |
---|
| 120 | if( file_xp == XPTR_NULL ) |
---|
| 121 | { |
---|
[433] | 122 | |
---|
[438] | 123 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 124 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
| 125 | printk("\n[ERROR] in %s : thread[%x,%x] / undefined file descriptor = %d\n", |
---|
[584] | 126 | __FUNCTION__, process->pid, this->trdid, file_id ); |
---|
[433] | 127 | #endif |
---|
[23] | 128 | this->errno = EBADFD; |
---|
| 129 | return -1; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | // get file descriptor cluster and local pointer |
---|
[604] | 133 | file_ptr = GET_PTR( file_xp ); |
---|
| 134 | file_cxy = GET_CXY( file_xp ); |
---|
[23] | 135 | |
---|
[604] | 136 | // get file type, offset, aatributes, and associated inode |
---|
| 137 | file_type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) ); |
---|
| 138 | file_offset = hal_remote_l32( XPTR( file_cxy , &file_ptr->offset ) ); |
---|
| 139 | inode_ptr = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) ); |
---|
| 140 | file_attr = hal_remote_l32( XPTR( file_cxy , &file_ptr->attr ) ); |
---|
[313] | 141 | |
---|
[443] | 142 | // enable IRQs |
---|
| 143 | hal_enable_irq( &save_sr ); |
---|
| 144 | |
---|
[670] | 145 | // check file writable |
---|
| 146 | if( (file_attr & FD_ATTR_WRITE_ENABLE) == 0 ) |
---|
[23] | 147 | { |
---|
[433] | 148 | |
---|
[438] | 149 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 150 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
| 151 | printk("\n[ERROR] in %s : thread[%x,%x] / file %d not writable\n", |
---|
[584] | 152 | __FUNCTION__ , process->pid, this->trdid, file_id ); |
---|
[433] | 153 | #endif |
---|
[604] | 154 | hal_restore_irq( save_sr ); |
---|
[421] | 155 | this->errno = EBADFD; |
---|
| 156 | return -1; |
---|
[670] | 157 | } |
---|
[421] | 158 | |
---|
[670] | 159 | // action depend on file type: |
---|
| 160 | if( file_type == FILE_TYPE_REG ) // write to mapper |
---|
| 161 | { |
---|
[421] | 162 | // move count bytes to mapper |
---|
[670] | 163 | nbytes = vfs_user_move( false, // to mapper |
---|
[407] | 164 | file_xp, |
---|
| 165 | vaddr, |
---|
| 166 | count ); |
---|
| 167 | } |
---|
[670] | 168 | else if( file_type == FILE_TYPE_DEV ) // write to TXT device |
---|
[407] | 169 | { |
---|
[421] | 170 | // move count bytes to device |
---|
[670] | 171 | nbytes = devfs_user_move( false, // to device |
---|
[604] | 172 | file_xp, |
---|
| 173 | vaddr, |
---|
| 174 | count ); |
---|
[407] | 175 | } |
---|
[670] | 176 | else if( (file_type == FILE_TYPE_PIPE) || |
---|
| 177 | (file_type == FILE_TYPE_FIFO) ) // write to pipe |
---|
[407] | 178 | { |
---|
[670] | 179 | // move count bytes to pipe |
---|
| 180 | nbytes = pipe_user_move( false, // to pipe |
---|
| 181 | file_xp, |
---|
| 182 | vaddr, |
---|
| 183 | count ); |
---|
| 184 | } |
---|
| 185 | else // unsupported type |
---|
| 186 | { |
---|
[594] | 187 | |
---|
| 188 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 189 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[594] | 190 | printk("\n[ERROR] in %s : thread[%x,%x] / illegal inode type %\n", |
---|
[604] | 191 | __FUNCTION__, vfs_inode_type_str( file_type ) ); |
---|
[594] | 192 | #endif |
---|
[604] | 193 | hal_restore_irq( save_sr ); |
---|
[594] | 194 | this->errno = EBADFD; |
---|
| 195 | return -1; |
---|
[407] | 196 | } |
---|
| 197 | |
---|
[656] | 198 | // chek error |
---|
| 199 | if( nbytes == 0xFFFFFFFF ) |
---|
| 200 | { |
---|
| 201 | |
---|
| 202 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 203 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[656] | 204 | printk("\n[ERROR] in %s : thread[%x,‰x] cannot write data to file %d\n", |
---|
| 205 | __FUNCTION__ , process->pid, this->trdid, file_id ); |
---|
| 206 | #endif |
---|
| 207 | hal_restore_irq( save_sr ); |
---|
| 208 | this->errno = EIO; |
---|
| 209 | return -1; |
---|
| 210 | } |
---|
| 211 | |
---|
[408] | 212 | // restore IRQs |
---|
| 213 | hal_restore_irq( save_sr ); |
---|
| 214 | |
---|
[124] | 215 | hal_fence(); |
---|
[23] | 216 | |
---|
[566] | 217 | #if (DEBUG_SYS_WRITE || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 218 | uint64_t tm_end = hal_get_cycles(); |
---|
| 219 | #endif |
---|
| 220 | |
---|
[438] | 221 | #if DEBUG_SYS_WRITE |
---|
| 222 | if( DEBUG_SYS_WRITE < tm_end ) |
---|
[610] | 223 | printk("\n[%s] thread[%x,%x] exit / cycle %d\n", |
---|
[584] | 224 | __FUNCTION__, process->pid, this->trdid, (uint32_t)tm_end ); |
---|
[409] | 225 | #endif |
---|
[407] | 226 | |
---|
[566] | 227 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
| 228 | hal_atomic_add( &syscalls_cumul_cost[SYS_WRITE] , tm_end - tm_start ); |
---|
| 229 | hal_atomic_add( &syscalls_occurences[SYS_WRITE] , 1 ); |
---|
| 230 | #endif |
---|
| 231 | |
---|
[438] | 232 | #if (DEBUG_SYS_WRITE & 1) |
---|
[664] | 233 | if( DEBUG_SYS_WRITE < tm_start ) |
---|
[435] | 234 | exit_sys_write = (uint32_t)tm_end; |
---|
| 235 | |
---|
[664] | 236 | if( DEBUG_SYS_WRITE < tm_start ) |
---|
[443] | 237 | printk("\n***** timing to write a string *****\n" |
---|
[435] | 238 | " - enter_sys_write = %d / delta %d\n" |
---|
| 239 | " - enter_devfs_write = %d / delta %d\n" |
---|
| 240 | " - enter_txt_write = %d / delta %d\n" |
---|
| 241 | " - enter_chdev_cmd_write = %d / delta %d\n" |
---|
| 242 | " - enter_chdev_server_write = %d / delta %d\n" |
---|
| 243 | " - enter_tty_cmd_write = %d / delta %d\n" |
---|
| 244 | " - enter_tty_isr_write = %d / delta %d\n" |
---|
| 245 | " - exit_tty_isr_write = %d / delta %d\n" |
---|
| 246 | " - exit_tty_cmd_write = %d / delta %d\n" |
---|
| 247 | " - exit_chdev_server_write = %d / delta %d\n" |
---|
| 248 | " - exit_chdev_cmd_write = %d / delta %d\n" |
---|
| 249 | " - exit_txt_write = %d / delta %d\n" |
---|
| 250 | " - exit_devfs_write = %d / delta %d\n" |
---|
| 251 | " - exit_sys_write = %d / delta %d\n", |
---|
| 252 | enter_sys_write , 0 , |
---|
| 253 | enter_devfs_write , enter_devfs_write - enter_sys_write , |
---|
| 254 | enter_txt_write , enter_txt_write - enter_devfs_write , |
---|
| 255 | enter_chdev_cmd_write , enter_chdev_cmd_write - enter_txt_write , |
---|
| 256 | enter_chdev_server_write , enter_chdev_server_write - enter_chdev_cmd_write , |
---|
| 257 | enter_tty_cmd_write , enter_tty_cmd_write - enter_chdev_server_write , |
---|
| 258 | enter_tty_isr_write , enter_tty_isr_write - enter_tty_cmd_write , |
---|
| 259 | exit_tty_isr_write , exit_tty_isr_write - enter_tty_isr_write , |
---|
| 260 | exit_tty_cmd_write , exit_tty_cmd_write - exit_tty_isr_write , |
---|
| 261 | exit_chdev_server_write , exit_chdev_server_write - exit_tty_cmd_write , |
---|
| 262 | exit_chdev_cmd_write , exit_chdev_cmd_write - exit_chdev_server_write , |
---|
| 263 | exit_txt_write , exit_txt_write - exit_chdev_cmd_write , |
---|
| 264 | exit_devfs_write , exit_devfs_write - exit_txt_write , |
---|
| 265 | exit_sys_write , exit_sys_write - exit_devfs_write ); |
---|
| 266 | #endif |
---|
| 267 | |
---|
[407] | 268 | return nbytes; |
---|
| 269 | |
---|
[23] | 270 | } // end sys_write() |
---|