[1] | 1 | /* |
---|
[651] | 2 | * kern/sys_lseek.c - Kernel function implementing the lseek system call. |
---|
[1] | 3 | * |
---|
[651] | 4 | * AUthor Alain Greiner (2016,2017,2018,2019) |
---|
| 5 | s |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
[1] | 7 | * |
---|
[651] | 8 | * This file is part of ALMOS-MKH. |
---|
[1] | 9 | * |
---|
[651] | 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 | * |
---|
[651] | 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 |
---|
[651] | 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 <errno.h> |
---|
[1] | 28 | #include <vfs.h> |
---|
[23] | 29 | #include <vmm.h> |
---|
[1] | 30 | #include <thread.h> |
---|
[23] | 31 | #include <printk.h> |
---|
| 32 | #include <process.h> |
---|
[1] | 33 | |
---|
[651] | 34 | ///////////////////////////////// |
---|
| 35 | int sys_lseek ( uint32_t file_id, |
---|
| 36 | uint32_t offset, |
---|
| 37 | uint32_t whence ) |
---|
[1] | 38 | { |
---|
[23] | 39 | error_t error; |
---|
| 40 | xptr_t file_xp; |
---|
| 41 | uint32_t new_offset; |
---|
[1] | 42 | |
---|
[23] | 43 | thread_t * this = CURRENT_THREAD; |
---|
| 44 | process_t * process = this->process; |
---|
[1] | 45 | |
---|
[651] | 46 | #if (DEBUG_SYS_LSEEK || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 47 | uint64_t tm_start = hal_get_cycles(); |
---|
| 48 | #endif |
---|
| 49 | |
---|
| 50 | #if DEBUG_SYS_LSEEK |
---|
| 51 | if( DEBUG_SYS_LSEEK < tm_start ) |
---|
| 52 | printk("\n[%s] thread[%x,%x] enter / file_id %d / offset %d / cycle %d\n", |
---|
| 53 | __FUNCTION__, process->pid, this->trdid, file_id, offset, (uint32_t)tm_start ); |
---|
| 54 | #endif |
---|
| 55 | |
---|
[23] | 56 | // check file_id argument |
---|
| 57 | if( file_id >= CONFIG_PROCESS_FILE_MAX_NR ) |
---|
[1] | 58 | { |
---|
[651] | 59 | |
---|
| 60 | #if DEBUG_SYSCALLS_ERROR |
---|
| 61 | printk("\n[ERROR] in %s : thread[%x,%x] illegal file descriptor index %d\n", |
---|
| 62 | __FUNCTION__ , process->pid, this->trdid, file_id ); |
---|
| 63 | #endif |
---|
[23] | 64 | this->errno = EBADFD; |
---|
[1] | 65 | return -1; |
---|
| 66 | } |
---|
| 67 | |
---|
[23] | 68 | // get extended pointer on remote file descriptor |
---|
[664] | 69 | file_xp = process_fd_get_xptr_from_local( process , file_id ); |
---|
[23] | 70 | |
---|
| 71 | if( file_xp == XPTR_NULL ) |
---|
| 72 | { |
---|
[651] | 73 | |
---|
| 74 | #if DEBUG_SYSCALLS_ERROR |
---|
| 75 | printk("\n[ERROR] in %s : thread[%x,%x] undefined fd_id %d\n", |
---|
| 76 | __FUNCTION__, process->pid, this->trdid, file_id ); |
---|
| 77 | #endif |
---|
[23] | 78 | this->errno = EBADFD; |
---|
| 79 | return -1; |
---|
| 80 | } |
---|
| 81 | |
---|
[651] | 82 | // FIXME: file may be closed in parallel of seek/read/write/mmap ..etc |
---|
[23] | 83 | |
---|
| 84 | // call relevant VFS function |
---|
| 85 | error = vfs_lseek( file_xp , offset , whence , &new_offset ); |
---|
| 86 | |
---|
| 87 | if( error ) |
---|
[1] | 88 | { |
---|
[651] | 89 | |
---|
| 90 | #if DEBUG_SYSCALLS_ERROR |
---|
| 91 | printk("\n[ERROR] in %s : thread[%x,%x] cannot seek file_id %d\n", |
---|
| 92 | __FUNCTION__, process->pid, this->trdid, file_id ); |
---|
| 93 | #endif |
---|
[23] | 94 | this->errno = error; |
---|
[1] | 95 | return -1; |
---|
| 96 | } |
---|
[651] | 97 | |
---|
| 98 | #if (DEBUG_SYS_LSEEK || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 99 | uint64_t tm_end = hal_get_cycles(); |
---|
| 100 | #endif |
---|
| 101 | |
---|
| 102 | #if DEBUG_SYS_LSEEK |
---|
| 103 | if( DEBUG_SYS_LSEEK < tm_end ) |
---|
| 104 | printk("\n[%s] thread[%x,%x] exit / cycle %d\n", |
---|
| 105 | __FUNCTION__ , process->pid, this->trdid, (uint32_t)tm_end ); |
---|
| 106 | #endif |
---|
| 107 | |
---|
| 108 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
| 109 | hal_atomic_add( &syscalls_cumul_cost[SYS_LSEEK] , tm_end - tm_start ); |
---|
| 110 | hal_atomic_add( &syscalls_occurences[SYS_LSEEK] , 1 ); |
---|
| 111 | #endif |
---|
[1] | 112 | |
---|
| 113 | return new_offset; |
---|
| 114 | } |
---|