source: trunk/kernel/syscalls/sys_mmap.c @ 461

Last change on this file since 461 was 457, checked in by alain, 6 years ago

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

File size: 8.6 KB
RevLine 
[1]1/*
[23]2 * sys_mmap.c - map files, memory or devices into process virtual address space
[1]3 *
[23]4 * Authors       Ghassan Almaless (2008,2009,2010,2011,2012)
[440]5 *               Alain Greiner (2016,2017,2018)
[1]6 *
[23]7 * Copyright (c) UPMC Sorbonne Universites
[1]8 *
[23]9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
[1]12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
[23]15 * ALMOS-MKH is distributed in the hope that it will be useful, but
[1]16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
[23]21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
[1]22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
[457]25#include <hal_kernel_types.h>
[407]26#include <hal_uspace.h>
[435]27#include <hal_irqmask.h>
[407]28#include <shared_syscalls.h>
[1]29#include <errno.h>
30#include <thread.h>
[23]31#include <printk.h>
[407]32#include <mapper.h>
[1]33#include <vfs.h>
34#include <process.h>
35#include <vmm.h>
36
[407]37//////////////////////////////////
[23]38int sys_mmap( mmap_attr_t * attr )
[1]39{
[407]40    vseg_t      * vseg;
41    cxy_t         vseg_cxy;
42    vseg_type_t   vseg_type;
43    mmap_attr_t   k_attr;       // attributes copy in kernel space
44    xptr_t        mapper_xp;
45    error_t       error;
[435]46    reg_t         save_sr;      // required to enable IRQs
[1]47
[407]48        thread_t    * this    = CURRENT_THREAD;
49        process_t   * process = this->process;
50
[438]51#if DEBUG_SYS_MMAP
[435]52uint64_t      tm_start;
53uint64_t      tm_end;
54tm_start = hal_get_cycles();
[438]55if ( DEBUG_SYS_MMAP < tm_start )
[435]56printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n",
57__FUNCTION__, this, process->pid, (uint32_t)tm_start );
58#endif
59
[407]60    // check arguments in user space
[440]61    error = vmm_get_vseg( process , (intptr_t)attr , &vseg );
[407]62
63    if ( error )
64    {
[435]65
[438]66#if DEBUG_SYSCALLS_ERROR
[440]67printk("\n[ERROR] in %s : user buffer unmapped %x / thread %x / process %x\n",
68__FUNCTION__ , (intptr_t)attr , this->trdid , process->pid );
69vmm_display( process , false );
[435]70#endif
[407]71                this->errno = EINVAL;
72                return -1;
73    }
74
75    // copy arguments from uspace
76    hal_copy_from_uspace( &k_attr , attr , sizeof(mmap_attr_t) );
77
78    // get fdid, offset, and length arguments
79    uint32_t fdid   = k_attr.fdid;
80    uint32_t offset = k_attr.offset;
81    uint32_t length = k_attr.length;
82
83    // get flags
84    bool_t     map_fixed   = ( (k_attr.flags & MAP_FIXED)   != 0 );
85    bool_t     map_anon    = ( (k_attr.flags & MAP_ANON)    != 0 );
86    bool_t     map_remote  = ( (k_attr.flags & MAP_REMOTE)  != 0 );
87    bool_t     map_shared  = ( (k_attr.flags & MAP_SHARED)  != 0 );
88    bool_t     map_private = ( (k_attr.flags & MAP_PRIVATE) != 0 );
89
90    // MAP_FIXED not supported
91    if( map_fixed )
92    {
[435]93
[438]94#if DEBUG_SYSCALLS_ERROR
[440]95printk("\n[ERROR] in %s : MAP_FIXED not supported / thread %x / process %x\n",
96__FUNCTION__ , this->trdid , process->pid );
[435]97#endif
[407]98        this->errno = EINVAL;
99        return -1;
100    }
101
102    if( map_shared == map_private )
103    {
[435]104
[438]105#if DEBUG_SYSCALLS_ERROR
[440]106printk("\n[ERROR] in %s : MAP_SHARED == MAP_PRIVATE / thread %x / process %x\n",
107__FUNCTION__ , this->trdid , process->pid );
[435]108#endif
[407]109        this->errno = EINVAL;
110        return -1;
111    }
112
113    // FIXME handle Copy_On_Write for MAP_PRIVATE...
114
115    // get access rigths
116    bool_t     prot_read   = ( (k_attr.prot & PROT_READ )   != 0 );
117    bool_t     prot_write  = ( (k_attr.prot & PROT_WRITE)   != 0 );
118
119    // test mmap type : can be FILE / ANON / REMOTE
120
121    if( (map_anon == false) && (map_remote == false) )   // FILE
122    {
123            // FIXME: handle concurent delete of file by another thread closing it
124
125                if( fdid >= CONFIG_PROCESS_FILE_MAX_NR ) 
[1]126                {
[435]127
[438]128#if DEBUG_SYSCALLS_ERROR
[440]129printk("\n[ERROR] in %s: bad file descriptor %d / thread %x / process %x\n",
130__FUNCTION__ , fdid , this->trdid , process->pid );
[435]131#endif
[407]132            this->errno = EBADFD;
133            return -1;
134        }
[1]135
[407]136        // get extended pointer on file descriptor
137        xptr_t file_xp = process_fd_get_xptr( process , fdid );
138
139        if( file_xp == XPTR_NULL )
140        {
[435]141
[438]142#if DEBUG_SYSCALLS_ERROR
[440]143printk("\n[ERROR] in %s: file %d not found / thread %x / process %x\n",
144__FUNCTION__ , fdid , this->trdid , process->pid );
[435]145#endif
[407]146            this->errno = EBADFD;
147            return -1;
148        }
149
150        // get file cluster and local pointer
151        cxy_t        file_cxy = GET_CXY( file_xp );
152        vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
153
154        // get inode pointer, mapper pointer and file attributes
155        vfs_inode_t * inode_ptr  = hal_remote_lpt(XPTR(file_cxy , &file_ptr->inode ));
156        uint32_t      file_attr  = hal_remote_lw (XPTR(file_cxy , &file_ptr->attr  ));
157        mapper_t    * mapper_ptr = hal_remote_lpt(XPTR(file_cxy , &file_ptr->mapper));
158
159        // get file size
160                uint32_t size = hal_remote_lw( XPTR( file_cxy , &inode_ptr->size ) );
161
162        // chek offset and length arguments
163                if( (offset + length) > size)
[1]164                {
[435]165
[438]166#if DEBUG_SYSCALLS_ERROR
[440]167printk("\n[ERROR] in %s: offset(%d) + len(%d) >= file's size(%d) / thread %x / process %x\n", 
168__FUNCTION__, k_attr.offset, k_attr.length, size, this->trdid, process->pid );
[435]169#endif
[407]170            this->errno = ERANGE;
171            return -1;
[1]172                }
173
[407]174        // check access rights
175                if( (prot_read  && !(file_attr & FD_ATTR_READ_ENABLE)) ||
176                    (prot_write && !(file_attr & FD_ATTR_WRITE_ENABLE)) )
[1]177                {
[435]178
[438]179#if DEBUG_SYSCALLS_ERROR
[440]180printk("\n[ERROR] in %s: prot = %x / file_attr = %x / thread %x , process %x\n", 
181__FUNCTION__ , k_attr.prot , file_attr , this->trdid , process->pid );
[435]182#endif
[407]183                        this->errno = EACCES;
184                        return -1;
[1]185                }
186
[407]187                // increment file refcount
188                vfs_file_count_up( file_xp );
[1]189
[407]190        mapper_xp = XPTR( file_cxy , mapper_ptr );
191        vseg_type = VSEG_TYPE_FILE;
192        vseg_cxy  = file_cxy;
193    }
194    else                                                // ANON or REMOTE
195    {
196        // no mapper for ANON or REMOTE
197        mapper_xp = XPTR_NULL;
[1]198
[407]199        if( map_anon )
200        {
201            vseg_type = VSEG_TYPE_ANON;
202            vseg_cxy  = local_cxy;
203        }
204        else
205        {
206            vseg_type = VSEG_TYPE_REMOTE;
207            vseg_cxy  = k_attr.fdid;
208 
209            if( cluster_is_undefined( vseg_cxy ) )
210            {
[435]211
[438]212#if DEBUG_SYSCALLS_ERROR
[440]213printk("\n[ERROR] in %s : illegal cxy for MAP_REMOTE / thread %x / process %x\n",
214__FUNCTION__, this->trdid , process->pid );
[435]215#endif
[407]216                this->errno = EINVAL;
217                return -1;
218            }
219        }
220    }
[1]221
[435]222    // enable IRQs
223    hal_enable_irq( &save_sr );
224
[407]225    // get reference process cluster and local pointer
226    xptr_t      ref_xp  = process->ref_xp;
227    cxy_t       ref_cxy = GET_CXY( ref_xp );
228    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
229
230    // create the vseg in reference cluster
231    if( local_cxy == ref_cxy )
232    {
233        vseg = vmm_create_vseg( process,
234                                vseg_type,
235                                0,               // base address unused for mmap()
236                                length,
237                                offset,
238                                0,               // file_size unused for mmap()
239                                mapper_xp,
240                                vseg_cxy );
241    }
242    else
243    {
244        rpc_vmm_create_vseg_client( ref_cxy,
245                                    ref_ptr,
246                                    vseg_type,
247                                    0,            // base address unused for mmap()
248                                    length,
249                                    offset,
250                                    0,            // file size unused for mmap()
251                                    mapper_xp,
252                                    vseg_cxy,
253                                    &vseg ); 
254    }
255   
[435]256    // restore IRQs
257    hal_restore_irq( save_sr );
258
[407]259    if( vseg == NULL )
260    {
[435]261
[438]262#if DEBUG_SYSCALLS_ERROR
[440]263printk("\n[ERROR] in %s : cannot create vseg / thread %x / process %x\n",
264__FUNCTION__, this->trdid , process->pid );
[435]265#endif
[407]266        this->errno = ENOMEM;
267        return -1;
268    }
269
270    // copy vseg base address to user space
271    hal_copy_to_uspace( &attr->addr , &vseg->min , sizeof(intptr_t) );
272
[435]273    hal_fence();
[407]274
[438]275#if DEBUG_SYS_MMAP
[435]276tm_end = hal_get_cycles();
[438]277if ( DEBUG_SYS_MMAP < tm_start )
[435]278printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n"
279"vseg %s / cluster %x / base %x / size %x / cost %d\n", 
280__FUNCTION__, this, process->pid, (uint32_t)tm_end,
281vseg_type_str(vseg->type), vseg->cxy, vseg->min, length, (uint32_t)(tm_end - tm_start) );
282#endif
[407]283
284        return 0;
285
[23]286}  // end sys_mmap()
[407]287
Note: See TracBrowser for help on using the repository browser.