| 1 | /* |
|---|
| 2 | * sys_getcwd.c - kernel function implementing the "getcwd" 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 <kernel_config.h> |
|---|
| 25 | #include <hal_kernel_types.h> |
|---|
| 26 | #include <hal_uspace.h> |
|---|
| 27 | #include <hal_special.h> |
|---|
| 28 | #include <errno.h> |
|---|
| 29 | #include <vfs.h> |
|---|
| 30 | #include <vmm.h> |
|---|
| 31 | #include <process.h> |
|---|
| 32 | #include <thread.h> |
|---|
| 33 | #include <printk.h> |
|---|
| 34 | |
|---|
| 35 | #include <syscalls.h> |
|---|
| 36 | |
|---|
| 37 | /////////////////////////////////// |
|---|
| 38 | int sys_getcwd ( char * buffer, |
|---|
| 39 | uint32_t nbytes ) |
|---|
| 40 | { |
|---|
| 41 | error_t error; |
|---|
| 42 | vseg_t * vseg; |
|---|
| 43 | char * first; // first character valid in buffer |
|---|
| 44 | |
|---|
| 45 | char kbuf[CONFIG_VFS_MAX_PATH_LENGTH]; |
|---|
| 46 | |
|---|
| 47 | thread_t * this = CURRENT_THREAD; |
|---|
| 48 | process_t * process = this->process; |
|---|
| 49 | |
|---|
| 50 | #if (DEBUG_SYS_GETCWD || CONFIG_INSTRUMENTATION_SYSCALLS) |
|---|
| 51 | uint64_t tm_start = hal_get_cycles(); |
|---|
| 52 | #endif |
|---|
| 53 | |
|---|
| 54 | // check buffer size |
|---|
| 55 | if( nbytes < CONFIG_VFS_MAX_PATH_LENGTH ) |
|---|
| 56 | { |
|---|
| 57 | |
|---|
| 58 | #if DEBUG_SYSCALLS_ERROR |
|---|
| 59 | printk("\n[ERROR] in %s : buffer too small for thread %x,%x]\n", |
|---|
| 60 | __FUNCTION__ , process->pid, this->trdid ); |
|---|
| 61 | #endif |
|---|
| 62 | this->errno = EINVAL; |
|---|
| 63 | return -1; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | // check buffer in user space |
|---|
| 67 | error = vmm_get_vseg( process, (intptr_t)buffer , &vseg ); |
|---|
| 68 | |
|---|
| 69 | if( error ) |
|---|
| 70 | { |
|---|
| 71 | |
|---|
| 72 | #if DEBUG_SYSCALLS_ERROR |
|---|
| 73 | printk("\n[ERROR] in %s : user buffer unmapped %x for thread[%x,%x]\n", |
|---|
| 74 | __FUNCTION__ , (intptr_t)buffer , process->pid, this->trdid ); |
|---|
| 75 | #endif |
|---|
| 76 | this->errno = EINVAL; |
|---|
| 77 | return -1; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | #if DEBUG_SYS_GETCWD |
|---|
| 81 | if( DEBUG_SYS_GETCWD < tm_start ) |
|---|
| 82 | printk("\n[%s] thread[%x,%x] enter / cycle %d\n", |
|---|
| 83 | __FUNCTION__, process->pid, this->trdid, (uint32_t)tm_start ); |
|---|
| 84 | #endif |
|---|
| 85 | |
|---|
| 86 | // get extended pointer on CWD inode from the reference process |
|---|
| 87 | xptr_t ref_xp = process->ref_xp; |
|---|
| 88 | process_t * ref_ptr = GET_PTR( ref_xp ); |
|---|
| 89 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
|---|
| 90 | xptr_t cwd_xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->cwd_xp ) ); |
|---|
| 91 | |
|---|
| 92 | // call relevant VFS function |
|---|
| 93 | error = vfs_get_path( cwd_xp, |
|---|
| 94 | kbuf, |
|---|
| 95 | &first, |
|---|
| 96 | CONFIG_VFS_MAX_PATH_LENGTH ); |
|---|
| 97 | |
|---|
| 98 | // copy kernel buffer to user space |
|---|
| 99 | hal_strcpy_to_uspace( buffer, |
|---|
| 100 | XPTR( local_cxy , first ), |
|---|
| 101 | CONFIG_VFS_MAX_PATH_LENGTH ); |
|---|
| 102 | |
|---|
| 103 | hal_fence(); |
|---|
| 104 | |
|---|
| 105 | #if (DEBUG_SYS_GETCWD || CONFIG_INSTRUMENTATION_SYSCALLS) |
|---|
| 106 | uint64_t tm_end = hal_get_cycles(); |
|---|
| 107 | #endif |
|---|
| 108 | |
|---|
| 109 | #if DEBUG_SYS_GETCWD |
|---|
| 110 | if( DEBUG_SYS_GETCWD < tm_end ) |
|---|
| 111 | printk("\n[%s] thread[%x,%x] exit / cycle %d\n", |
|---|
| 112 | __FUNCTION__, process->pid, this->trdid, (uint32_t)tm_end ); |
|---|
| 113 | #endif |
|---|
| 114 | |
|---|
| 115 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
|---|
| 116 | hal_atomic_add( &syscalls_cumul_cost[SYS_GETCWD] , tm_end - tm_start ); |
|---|
| 117 | hal_atomic_add( &syscalls_occurences[SYS_GETCWD] , 1 ); |
|---|
| 118 | #endif |
|---|
| 119 | |
|---|
| 120 | return 0; |
|---|
| 121 | |
|---|
| 122 | } // end sys_getcwd() |
|---|