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