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