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