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

Last change on this file since 657 was 657, checked in by alain, 4 years ago

Introduce remote_buf.c/.h & socket.c/.h files.
Update dev_nic.c/.h files.

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