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