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

Last change on this file since 635 was 635, checked in by alain, 5 years ago

This version is a major evolution: The physical memory allocators,
defined in the kmem.c, ppm.c, and kcm.c files have been modified
to support remote accesses. The RPCs that were previously user
to allocate physical memory in a remote cluster have been removed.
This has been done to cure a dead-lock in case of concurrent page-faults.

This version 2.2 has been tested on a (4 clusters / 2 cores per cluster)
TSAR architecture, for both the "sort" and the "fft" applications.

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