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

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

Introduce the non-standard pthread_parallel_create() system call
and re-write the <fft> and <sort> applications to improve the
intrinsic paralelism in applications.

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