[1] | 1 | /* |
---|
[23] | 2 | * sys_write.c - write bytes to 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> |
---|
[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 | |
---|
| 37 | ////////////////////////////////// |
---|
| 38 | int sys_write( uint32_t file_id, |
---|
[407] | 39 | void * vaddr, |
---|
[23] | 40 | uint32_t count ) |
---|
[1] | 41 | { |
---|
[23] | 42 | error_t error; |
---|
[435] | 43 | paddr_t paddr; // required for user space checking |
---|
[408] | 44 | xptr_t file_xp; // remote file extended pointer |
---|
| 45 | uint32_t nbytes; // number of bytes actually written |
---|
| 46 | reg_t save_sr; // required to enable IRQs during syscall |
---|
[407] | 47 | |
---|
[435] | 48 | #if (CONFIG_DEBUG_SYS_WRITE_DEBUG & 1) |
---|
| 49 | enter_sys_read = (uint32_t)tm_start; |
---|
| 50 | #endif |
---|
| 51 | |
---|
[433] | 52 | thread_t * this = CURRENT_THREAD; |
---|
| 53 | process_t * process = this->process; |
---|
| 54 | |
---|
| 55 | #if CONFIG_DEBUG_SYS_WRITE |
---|
[409] | 56 | uint32_t tm_start; |
---|
| 57 | uint32_t tm_end; |
---|
| 58 | tm_start = hal_get_cycles(); |
---|
[433] | 59 | if( CONFIG_DEBUG_SYS_WRITE < tm_start ) |
---|
[435] | 60 | printk("\n[DBG] %s : thread %x enter / process %x / vaddr %x / count %d / cycle %d\n", |
---|
[433] | 61 | __FUNCTION__, this, process->pid, vaddr, count, (uint32_t)tm_start ); |
---|
[409] | 62 | #endif |
---|
[1] | 63 | |
---|
[23] | 64 | // check file_id argument |
---|
| 65 | if( file_id >= CONFIG_PROCESS_FILE_MAX_NR ) |
---|
[1] | 66 | { |
---|
[433] | 67 | |
---|
| 68 | #if CONFIG_DEBUG_SYSCALLS_ERROR |
---|
| 69 | printk("\n[ERROR] in %s : illegal file descriptor index\n", __FUNCTION__ ); |
---|
| 70 | #endif |
---|
[23] | 71 | this->errno = EBADFD; |
---|
[1] | 72 | return -1; |
---|
| 73 | } |
---|
| 74 | |
---|
[23] | 75 | // check user buffer in user space |
---|
[407] | 76 | error = vmm_v2p_translate( false , vaddr , &paddr ); |
---|
[23] | 77 | |
---|
| 78 | if ( error ) |
---|
| 79 | { |
---|
[433] | 80 | |
---|
| 81 | #if CONFIG_DEBUG_SYSCALLS_ERROR |
---|
| 82 | printk("\n[ERROR] in %s : user buffer unmapped = %x\n", __FUNCTION__ , (intptr_t)vaddr ); |
---|
| 83 | #endif |
---|
[23] | 84 | this->errno = EINVAL; |
---|
| 85 | return -1; |
---|
| 86 | } |
---|
| 87 | |
---|
[408] | 88 | // enable IRQs |
---|
| 89 | hal_enable_irq( &save_sr ); |
---|
| 90 | |
---|
[23] | 91 | // get extended pointer on remote file descriptor |
---|
| 92 | file_xp = process_fd_get_xptr( process , file_id ); |
---|
| 93 | |
---|
| 94 | if( file_xp == XPTR_NULL ) |
---|
| 95 | { |
---|
[433] | 96 | |
---|
| 97 | #if CONFIG_DEBUG_SYSCALLS_ERROR |
---|
| 98 | printk("\n[ERROR] in %s : undefined file descriptor index = %d in process %x\n", |
---|
| 99 | __FUNCTION__ , file_id , process->pid ); |
---|
| 100 | #endif |
---|
[23] | 101 | this->errno = EBADFD; |
---|
| 102 | return -1; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | // get file descriptor cluster and local pointer |
---|
| 106 | vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); |
---|
| 107 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
| 108 | |
---|
[421] | 109 | |
---|
[407] | 110 | // get file type |
---|
| 111 | vfs_inode_type_t type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) ); |
---|
[313] | 112 | |
---|
[407] | 113 | // action depend on file type |
---|
[421] | 114 | if( type == INODE_TYPE_FILE ) // check file writable & write to mapper |
---|
[23] | 115 | { |
---|
[421] | 116 | // check file writable |
---|
| 117 | uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) ); |
---|
| 118 | if( (attr & FD_ATTR_WRITE_ENABLE) == 0 ) |
---|
| 119 | { |
---|
[433] | 120 | |
---|
| 121 | #if CONFIG_DEBUG_SYSCALLS_ERROR |
---|
| 122 | printk("\n[ERROR] in %s : file %d not writable in process %x\n", |
---|
| 123 | __FUNCTION__ , file_id , process->pid ); |
---|
| 124 | #endif |
---|
[421] | 125 | this->errno = EBADFD; |
---|
| 126 | return -1; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | // move count bytes to mapper |
---|
[407] | 130 | nbytes = vfs_user_move( false, // from buffer to mapper |
---|
| 131 | file_xp, |
---|
| 132 | vaddr, |
---|
| 133 | count ); |
---|
| 134 | } |
---|
[421] | 135 | else if( type == INODE_TYPE_DEV ) // check ownership & write to device |
---|
[407] | 136 | { |
---|
[421] | 137 | // move count bytes to device |
---|
[407] | 138 | nbytes = devfs_user_move( false, // from buffer to device |
---|
| 139 | file_xp, |
---|
| 140 | vaddr, |
---|
| 141 | count ); |
---|
| 142 | } |
---|
| 143 | else |
---|
| 144 | { |
---|
| 145 | nbytes = 0; |
---|
[421] | 146 | assert( false , __FUNCTION__ , "file type %d non supported\n", type ); |
---|
[407] | 147 | } |
---|
| 148 | |
---|
| 149 | if( nbytes != count ) |
---|
| 150 | { |
---|
[433] | 151 | |
---|
| 152 | #if CONFIG_DEBUG_SYSCALLS_ERROR |
---|
| 153 | printk("\n[ERROR] in %s cannot write data to file %d in process %x\n", |
---|
| 154 | __FUNCTION__ , file_id , process->pid ); |
---|
| 155 | #endif |
---|
[313] | 156 | this->errno = error; |
---|
| 157 | return -1; |
---|
[23] | 158 | } |
---|
| 159 | |
---|
[408] | 160 | // restore IRQs |
---|
| 161 | hal_restore_irq( save_sr ); |
---|
| 162 | |
---|
[124] | 163 | hal_fence(); |
---|
[23] | 164 | |
---|
[433] | 165 | #if CONFIG_DEBUG_SYS_WRITE |
---|
[409] | 166 | tm_end = hal_get_cycles(); |
---|
[433] | 167 | if( CONFIG_DEBUG_SYS_WRITE < tm_end ) |
---|
[435] | 168 | printk("\n[DBG] %s : thread %x exit / process %x / cycle %d\n" |
---|
[408] | 169 | "nbytes = %d / first byte = %c / file_id = %d / cost = %d\n", |
---|
[433] | 170 | __FUNCTION__, this, process->pid, (uint32_t)tm_start, |
---|
| 171 | nbytes, *((char *)(intptr_t)paddr) , file_id , (uint32_t)(tm_end - tm_start) ); |
---|
[409] | 172 | #endif |
---|
[407] | 173 | |
---|
[435] | 174 | #if (CONFIG_DEBUG_SYS_WRITE & 1) |
---|
| 175 | exit_sys_write = (uint32_t)tm_end; |
---|
| 176 | |
---|
| 177 | printk("\n@@@@@@@@@@@@ timing to write string %c\n" |
---|
| 178 | " - enter_sys_write = %d / delta %d\n" |
---|
| 179 | " - enter_devfs_write = %d / delta %d\n" |
---|
| 180 | " - enter_txt_write = %d / delta %d\n" |
---|
| 181 | " - enter_chdev_cmd_write = %d / delta %d\n" |
---|
| 182 | " - enter_chdev_server_write = %d / delta %d\n" |
---|
| 183 | " - enter_tty_cmd_write = %d / delta %d\n" |
---|
| 184 | " - enter_tty_isr_write = %d / delta %d\n" |
---|
| 185 | " - exit_tty_isr_write = %d / delta %d\n" |
---|
| 186 | " - exit_tty_cmd_write = %d / delta %d\n" |
---|
| 187 | " - exit_chdev_server_write = %d / delta %d\n" |
---|
| 188 | " - exit_chdev_cmd_write = %d / delta %d\n" |
---|
| 189 | " - exit_txt_write = %d / delta %d\n" |
---|
| 190 | " - exit_devfs_write = %d / delta %d\n" |
---|
| 191 | " - exit_sys_write = %d / delta %d\n", |
---|
| 192 | *((char *)(intptr_t)paddr) , |
---|
| 193 | enter_sys_write , 0 , |
---|
| 194 | enter_devfs_write , enter_devfs_write - enter_sys_write , |
---|
| 195 | enter_txt_write , enter_txt_write - enter_devfs_write , |
---|
| 196 | enter_chdev_cmd_write , enter_chdev_cmd_write - enter_txt_write , |
---|
| 197 | enter_chdev_server_write , enter_chdev_server_write - enter_chdev_cmd_write , |
---|
| 198 | enter_tty_cmd_write , enter_tty_cmd_write - enter_chdev_server_write , |
---|
| 199 | enter_tty_isr_write , enter_tty_isr_write - enter_tty_cmd_write , |
---|
| 200 | exit_tty_isr_write , exit_tty_isr_write - enter_tty_isr_write , |
---|
| 201 | exit_tty_cmd_write , exit_tty_cmd_write - exit_tty_isr_write , |
---|
| 202 | exit_chdev_server_write , exit_chdev_server_write - exit_tty_cmd_write , |
---|
| 203 | exit_chdev_cmd_write , exit_chdev_cmd_write - exit_chdev_server_write , |
---|
| 204 | exit_txt_write , exit_txt_write - exit_chdev_cmd_write , |
---|
| 205 | exit_devfs_write , exit_devfs_write - exit_txt_write , |
---|
| 206 | exit_sys_write , exit_sys_write - exit_devfs_write ); |
---|
| 207 | #endif |
---|
| 208 | |
---|
[407] | 209 | return nbytes; |
---|
| 210 | |
---|
[23] | 211 | } // end sys_write() |
---|