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

Last change on this file since 578 was 566, checked in by alain, 6 years ago

Complete restructuration of kernel locks.

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
[506]37#include <syscalls.h>
38
[407]39//////////////////////////////////
[23]40int sys_mmap( mmap_attr_t * attr )
[1]41{
[407]42    vseg_t      * vseg;
43    cxy_t         vseg_cxy;
44    vseg_type_t   vseg_type;
45    mmap_attr_t   k_attr;       // attributes copy in kernel space
46    xptr_t        mapper_xp;
47    error_t       error;
[435]48    reg_t         save_sr;      // required to enable IRQs
[1]49
[407]50        thread_t    * this    = CURRENT_THREAD;
51        process_t   * process = this->process;
52
[438]53#if DEBUG_SYS_MMAP
[435]54uint64_t      tm_start;
55uint64_t      tm_end;
56tm_start = hal_get_cycles();
[438]57if ( DEBUG_SYS_MMAP < tm_start )
[435]58printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n",
59__FUNCTION__, this, process->pid, (uint32_t)tm_start );
60#endif
61
[407]62    // check arguments in user space
[440]63    error = vmm_get_vseg( process , (intptr_t)attr , &vseg );
[407]64
65    if ( error )
66    {
[435]67
[438]68#if DEBUG_SYSCALLS_ERROR
[440]69printk("\n[ERROR] in %s : user buffer unmapped %x / thread %x / process %x\n",
70__FUNCTION__ , (intptr_t)attr , this->trdid , process->pid );
71vmm_display( process , false );
[435]72#endif
[407]73                this->errno = EINVAL;
74                return -1;
75    }
76
77    // copy arguments from uspace
78    hal_copy_from_uspace( &k_attr , attr , sizeof(mmap_attr_t) );
79
80    // get fdid, offset, and length arguments
81    uint32_t fdid   = k_attr.fdid;
82    uint32_t offset = k_attr.offset;
83    uint32_t length = k_attr.length;
84
85    // get flags
86    bool_t     map_fixed   = ( (k_attr.flags & MAP_FIXED)   != 0 );
87    bool_t     map_anon    = ( (k_attr.flags & MAP_ANON)    != 0 );
88    bool_t     map_remote  = ( (k_attr.flags & MAP_REMOTE)  != 0 );
89    bool_t     map_shared  = ( (k_attr.flags & MAP_SHARED)  != 0 );
90    bool_t     map_private = ( (k_attr.flags & MAP_PRIVATE) != 0 );
91
92    // MAP_FIXED not supported
93    if( map_fixed )
94    {
[435]95
[438]96#if DEBUG_SYSCALLS_ERROR
[440]97printk("\n[ERROR] in %s : MAP_FIXED not supported / thread %x / process %x\n",
98__FUNCTION__ , this->trdid , process->pid );
[435]99#endif
[407]100        this->errno = EINVAL;
101        return -1;
102    }
103
104    if( map_shared == map_private )
105    {
[435]106
[438]107#if DEBUG_SYSCALLS_ERROR
[440]108printk("\n[ERROR] in %s : MAP_SHARED == MAP_PRIVATE / thread %x / process %x\n",
109__FUNCTION__ , this->trdid , process->pid );
[435]110#endif
[407]111        this->errno = EINVAL;
112        return -1;
113    }
114
115    // FIXME handle Copy_On_Write for MAP_PRIVATE...
116
117    // get access rigths
118    bool_t     prot_read   = ( (k_attr.prot & PROT_READ )   != 0 );
119    bool_t     prot_write  = ( (k_attr.prot & PROT_WRITE)   != 0 );
120
121    // test mmap type : can be FILE / ANON / REMOTE
122
123    if( (map_anon == false) && (map_remote == false) )   // FILE
124    {
125            // FIXME: handle concurent delete of file by another thread closing it
126
127                if( fdid >= CONFIG_PROCESS_FILE_MAX_NR ) 
[1]128                {
[435]129
[438]130#if DEBUG_SYSCALLS_ERROR
[440]131printk("\n[ERROR] in %s: bad file descriptor %d / thread %x / process %x\n",
132__FUNCTION__ , fdid , this->trdid , process->pid );
[435]133#endif
[407]134            this->errno = EBADFD;
135            return -1;
136        }
[1]137
[407]138        // get extended pointer on file descriptor
139        xptr_t file_xp = process_fd_get_xptr( process , fdid );
140
141        if( file_xp == XPTR_NULL )
142        {
[435]143
[438]144#if DEBUG_SYSCALLS_ERROR
[440]145printk("\n[ERROR] in %s: file %d not found / thread %x / process %x\n",
146__FUNCTION__ , fdid , this->trdid , process->pid );
[435]147#endif
[407]148            this->errno = EBADFD;
149            return -1;
150        }
151
152        // get file cluster and local pointer
153        cxy_t        file_cxy = GET_CXY( file_xp );
154        vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
155
156        // get inode pointer, mapper pointer and file attributes
157        vfs_inode_t * inode_ptr  = hal_remote_lpt(XPTR(file_cxy , &file_ptr->inode ));
[566]158        uint32_t      file_attr  = hal_remote_l32 (XPTR(file_cxy , &file_ptr->attr  ));
[407]159        mapper_t    * mapper_ptr = hal_remote_lpt(XPTR(file_cxy , &file_ptr->mapper));
160
161        // get file size
[566]162                uint32_t size = hal_remote_l32( XPTR( file_cxy , &inode_ptr->size ) );
[407]163
164        // chek offset and length arguments
165                if( (offset + length) > size)
[1]166                {
[435]167
[438]168#if DEBUG_SYSCALLS_ERROR
[440]169printk("\n[ERROR] in %s: offset(%d) + len(%d) >= file's size(%d) / thread %x / process %x\n", 
170__FUNCTION__, k_attr.offset, k_attr.length, size, this->trdid, process->pid );
[435]171#endif
[407]172            this->errno = ERANGE;
173            return -1;
[1]174                }
175
[407]176        // check access rights
177                if( (prot_read  && !(file_attr & FD_ATTR_READ_ENABLE)) ||
178                    (prot_write && !(file_attr & FD_ATTR_WRITE_ENABLE)) )
[1]179                {
[435]180
[438]181#if DEBUG_SYSCALLS_ERROR
[440]182printk("\n[ERROR] in %s: prot = %x / file_attr = %x / thread %x , process %x\n", 
183__FUNCTION__ , k_attr.prot , file_attr , this->trdid , process->pid );
[435]184#endif
[407]185                        this->errno = EACCES;
186                        return -1;
[1]187                }
188
[407]189                // increment file refcount
190                vfs_file_count_up( file_xp );
[1]191
[407]192        mapper_xp = XPTR( file_cxy , mapper_ptr );
193        vseg_type = VSEG_TYPE_FILE;
194        vseg_cxy  = file_cxy;
195    }
196    else                                                // ANON or REMOTE
197    {
198        // no mapper for ANON or REMOTE
199        mapper_xp = XPTR_NULL;
[1]200
[407]201        if( map_anon )
202        {
203            vseg_type = VSEG_TYPE_ANON;
204            vseg_cxy  = local_cxy;
205        }
206        else
207        {
208            vseg_type = VSEG_TYPE_REMOTE;
209            vseg_cxy  = k_attr.fdid;
210 
211            if( cluster_is_undefined( vseg_cxy ) )
212            {
[435]213
[438]214#if DEBUG_SYSCALLS_ERROR
[440]215printk("\n[ERROR] in %s : illegal cxy for MAP_REMOTE / thread %x / process %x\n",
216__FUNCTION__, this->trdid , process->pid );
[435]217#endif
[407]218                this->errno = EINVAL;
219                return -1;
220            }
221        }
222    }
[1]223
[435]224    // enable IRQs
225    hal_enable_irq( &save_sr );
226
[407]227    // get reference process cluster and local pointer
228    xptr_t      ref_xp  = process->ref_xp;
229    cxy_t       ref_cxy = GET_CXY( ref_xp );
230    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
231
232    // create the vseg in reference cluster
233    if( local_cxy == ref_cxy )
234    {
235        vseg = vmm_create_vseg( process,
236                                vseg_type,
237                                0,               // base address unused for mmap()
238                                length,
239                                offset,
240                                0,               // file_size unused for mmap()
241                                mapper_xp,
242                                vseg_cxy );
243    }
244    else
245    {
246        rpc_vmm_create_vseg_client( ref_cxy,
247                                    ref_ptr,
248                                    vseg_type,
249                                    0,            // base address unused for mmap()
250                                    length,
251                                    offset,
252                                    0,            // file size unused for mmap()
253                                    mapper_xp,
254                                    vseg_cxy,
255                                    &vseg ); 
256    }
257   
[435]258    // restore IRQs
259    hal_restore_irq( save_sr );
260
[407]261    if( vseg == NULL )
262    {
[435]263
[438]264#if DEBUG_SYSCALLS_ERROR
[440]265printk("\n[ERROR] in %s : cannot create vseg / thread %x / process %x\n",
266__FUNCTION__, this->trdid , process->pid );
[435]267#endif
[407]268        this->errno = ENOMEM;
269        return -1;
270    }
271
272    // copy vseg base address to user space
273    hal_copy_to_uspace( &attr->addr , &vseg->min , sizeof(intptr_t) );
274
[435]275    hal_fence();
[407]276
[438]277#if DEBUG_SYS_MMAP
[435]278tm_end = hal_get_cycles();
[438]279if ( DEBUG_SYS_MMAP < tm_start )
[435]280printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n"
281"vseg %s / cluster %x / base %x / size %x / cost %d\n", 
282__FUNCTION__, this, process->pid, (uint32_t)tm_end,
283vseg_type_str(vseg->type), vseg->cxy, vseg->min, length, (uint32_t)(tm_end - tm_start) );
284#endif
[407]285
286        return 0;
287
[23]288}  // end sys_mmap()
[407]289
Note: See TracBrowser for help on using the repository browser.