[1] | 1 | /* |
---|
[610] | 2 | * sys_chdir.c - kernel function implementing the "chdir" syscall. |
---|
[301] | 3 | * |
---|
[637] | 4 | * Author Alain Greiner (2016,2017,2018, 2019) |
---|
[23] | 5 | * |
---|
[610] | 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
[1] | 7 | * |
---|
[23] | 8 | * This file is part of ALMOS-MKH. |
---|
[1] | 9 | * |
---|
[23] | 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 | |
---|
[457] | 24 | #include <hal_kernel_types.h> |
---|
[23] | 25 | #include <hal_uspace.h> |
---|
[11] | 26 | #include <chdev.h> |
---|
[1] | 27 | #include <rwlock.h> |
---|
| 28 | #include <vfs.h> |
---|
[23] | 29 | #include <errno.h> |
---|
[1] | 30 | #include <thread.h> |
---|
[23] | 31 | #include <printk.h> |
---|
| 32 | #include <process.h> |
---|
[1] | 33 | |
---|
[506] | 34 | #include <syscalls.h> |
---|
| 35 | |
---|
[23] | 36 | ///////////////////////////////// |
---|
[566] | 37 | int sys_chdir ( char * pathname ) |
---|
[1] | 38 | { |
---|
[301] | 39 | error_t error; |
---|
[610] | 40 | vseg_t * vseg; |
---|
| 41 | xptr_t root_inode_xp; |
---|
| 42 | |
---|
[23] | 43 | char kbuf[CONFIG_VFS_MAX_PATH_LENGTH]; |
---|
[1] | 44 | |
---|
[301] | 45 | thread_t * this = CURRENT_THREAD; |
---|
[23] | 46 | process_t * process = this->process; |
---|
[1] | 47 | |
---|
[670] | 48 | #if DEBUG_SYS_CHDIR || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS |
---|
[610] | 49 | uint64_t tm_start = hal_get_cycles(); |
---|
| 50 | #endif |
---|
| 51 | |
---|
[407] | 52 | // check pathname length |
---|
| 53 | if( hal_strlen_from_uspace( pathname ) >= CONFIG_VFS_MAX_PATH_LENGTH ) |
---|
[23] | 54 | { |
---|
[566] | 55 | |
---|
| 56 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 57 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[610] | 58 | printk("\n[ERROR] in %s : pathname too long / thread[%x,%x]\n", |
---|
| 59 | __FUNCTION__, process->pid, this->trdid ); |
---|
[566] | 60 | #endif |
---|
[610] | 61 | this->errno = EINVAL; |
---|
[23] | 62 | return -1; |
---|
| 63 | } |
---|
| 64 | |
---|
[610] | 65 | // check pathname in user space |
---|
| 66 | if( vmm_get_vseg( process, (intptr_t)pathname , &vseg ) ) |
---|
| 67 | { |
---|
| 68 | |
---|
| 69 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 70 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[610] | 71 | printk("\n[ERROR] in %s : user buffer unmapped %x for thread[%x,%x]\n", |
---|
| 72 | __FUNCTION__ , (intptr_t)pathname , process->pid, this->trdid ); |
---|
| 73 | #endif |
---|
| 74 | this->errno = EINVAL; |
---|
| 75 | return -1; |
---|
| 76 | } |
---|
| 77 | |
---|
[407] | 78 | // copy pathname in kernel space |
---|
[637] | 79 | hal_strcpy_from_uspace( XPTR( local_cxy , kbuf ), |
---|
| 80 | pathname, |
---|
| 81 | CONFIG_VFS_MAX_PATH_LENGTH ); |
---|
[407] | 82 | |
---|
[610] | 83 | #if DEBUG_SYS_CHDIR |
---|
[670] | 84 | if( DEBUG_SYS_CHDIR < (uint32_t)tm_start ) |
---|
[610] | 85 | printk("\n[%s] thread[%x,%x] enter for <%s> / cycle %d\n", |
---|
| 86 | __FUNCTION__, process->pid, this->trdid, kbuf, (uint32_t)tm_start ); |
---|
| 87 | #endif |
---|
[566] | 88 | |
---|
[610] | 89 | // compute root inode for path |
---|
| 90 | if( kbuf[0] == '/' ) // absolute path |
---|
| 91 | { |
---|
| 92 | // use extended pointer on VFS root inode |
---|
| 93 | root_inode_xp = process->vfs_root_xp; |
---|
| 94 | } |
---|
| 95 | else // relative path |
---|
| 96 | { |
---|
| 97 | // get cluster and local pointer on reference process |
---|
| 98 | xptr_t ref_xp = process->ref_xp; |
---|
| 99 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 100 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
[23] | 101 | |
---|
[610] | 102 | // use extended pointer on CWD inode |
---|
| 103 | root_inode_xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->cwd_xp ) ); |
---|
| 104 | } |
---|
[23] | 105 | |
---|
[610] | 106 | // call the relevant VFS function |
---|
| 107 | error = vfs_chdir( root_inode_xp , kbuf ); |
---|
[301] | 108 | |
---|
| 109 | if( error ) |
---|
| 110 | { |
---|
[610] | 111 | |
---|
| 112 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 113 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[610] | 114 | printk("\n[ERROR] in %s / thread[%x,%x] : cannot change CWD\n", |
---|
| 115 | __FUNCTION__ , process->pid , this->trdid ); |
---|
| 116 | #endif |
---|
[301] | 117 | this->errno = error; |
---|
| 118 | return -1; |
---|
| 119 | } |
---|
| 120 | |
---|
[610] | 121 | hal_fence(); |
---|
| 122 | |
---|
| 123 | #if (DEBUG_SYS_CHDIR || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 124 | uint64_t tm_end = hal_get_cycles(); |
---|
| 125 | #endif |
---|
| 126 | |
---|
| 127 | #if DEBUG_SYS_CHDIR |
---|
| 128 | if( DEBUG_SYS_CHDIR < tm_end ) |
---|
| 129 | printk("\n[%s] thread[%x,%x] exit / cycle %d\n", |
---|
| 130 | __FUNCTION__, process->pid, this->trdid, (uint32_t)tm_end ); |
---|
| 131 | #endif |
---|
| 132 | |
---|
| 133 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
| 134 | hal_atomic_add( &syscalls_cumul_cost[SYS_CHDIR] , tm_end - tm_start ); |
---|
| 135 | hal_atomic_add( &syscalls_occurences[SYS_CHDIR] , 1 ); |
---|
| 136 | #endif |
---|
| 137 | |
---|
[301] | 138 | return 0; |
---|
[1] | 139 | } |
---|