source: trunk/kernel/syscalls/sys_open.c @ 484

Last change on this file since 484 was 459, checked in by alain, 6 years ago

Introduce the math library, to support the floating point
data used by the multi-thread fft application.
Fix several bugs regarding the FPU context save/restore.
Introduce support for the %f format in printf.

File size: 3.7 KB
Line 
1/*
2 * sys_open.c - open a file.
3 *
4 * Author        Alain Greiner (2016,2017)
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 <errno.h>
28#include <thread.h>
29#include <printk.h>
30#include <vfs.h>
31#include <process.h>
32#include <remote_spinlock.h>
33#include <remote_rwlock.h>
34
35///////////////////////////////////
36int sys_open ( char     * pathname,
37               uint32_t   flags,
38               uint32_t   mode )
39{
40    error_t        error;
41    xptr_t         file_xp;                          // extended pointer on vfs_file_t
42    uint32_t       file_id;                          // file descriptor index
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_OPEN
49uint32_t     tm_start;
50uint32_t     tm_end;
51tm_start = hal_get_cycles();
52#endif
53 
54    // check fd_array not full
55    if( process_fd_array_full() )
56    {
57        printk("\n[ERROR] in %s : file descriptor array full for process %x\n",
58               __FUNCTION__ , process->pid );
59        this->errno = ENFILE;
60        return -1;
61    }
62
63    // check pathname length
64    if( hal_strlen_from_uspace( pathname ) >= CONFIG_VFS_MAX_PATH_LENGTH )
65    {
66        printk("\n[ERROR] in %s : pathname too long\n", __FUNCTION__ );
67        this->errno = ENFILE;
68        return -1;
69    }
70
71    // copy pathname in kernel space
72    hal_strcpy_from_uspace( kbuf , pathname , CONFIG_VFS_MAX_PATH_LENGTH );
73
74#if DEBUG_SYS_OPEN
75if( DEBUG_SYS_OPEN < tm_start )
76printk("\n[DBG] %s : thread %x in process %x enter / path %s / flags %x / cycle %d\n",
77__FUNCTION__, this->trdid, process->pid, kbuf, flags, (uint32_t)tm_start );
78#endif
79 
80    // get cluster and local pointer on reference process
81    xptr_t      ref_xp  = process->ref_xp;
82    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
83    cxy_t       ref_cxy = GET_CXY( ref_xp );
84
85    // get the cwd lock in read mode from reference process
86    remote_rwlock_rd_lock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) );
87
88    // call the relevant VFS function
89    error = vfs_open( process,
90                      kbuf,
91                      flags,
92                      mode,
93                      &file_xp,
94                      &file_id );
95
96    // release the cwd lock
97    remote_rwlock_rd_unlock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) );
98
99    if( error )
100    {
101        printk("\n[ERROR] in %s : cannot create file descriptor for %s\n",
102        __FUNCTION__ , kbuf );
103        this->errno = ENFILE;
104        return -1;
105    }
106
107    // update local fd_array
108    remote_spinlock_lock( XPTR( local_cxy , &process->fd_array.lock ) );
109    process->fd_array.array[file_id] = file_xp;
110    remote_spinlock_unlock( XPTR( local_cxy , &process->fd_array.lock ) );
111
112    hal_fence();
113
114#if DEBUG_SYS_OPEN
115tm_end = hal_get_cycles();
116if( DEBUG_SYS_OPEN < tm_start )
117printk("\n[DBG] %s : thread %x in process %x exit / cost %d / cycle %d\n",
118__FUNCTION__, this->trdid, process->pid, (uint32_t)(tm_end - tm_start), (uint32_t)tm_start );
119#endif
120 
121    return file_id;
122}
Note: See TracBrowser for help on using the repository browser.