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

Last change on this file since 635 was 635, checked in by alain, 18 months 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
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;
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;
48    reg_t         save_sr;      // required to enable IRQs
49
50        thread_t    * this    = CURRENT_THREAD;
51        process_t   * process = this->process;
52
53#if (DEBUG_SYS_MMAP || CONFIG_INSTRUMENTATION_SYSCALLS)
54uint64_t     tm_start = hal_get_cycles();
55#endif
56
57#if DEBUG_SYS_MMAP
58if( DEBUG_SYS_MMAP < tm_start )
59printk("\n[%s] thread[%x,%x] enter / cycle %d\n",
60__FUNCTION__, process->pid, this->trdid, (uint32_t)tm_start );
61#endif
62
63    // check user buffer (containing attributes) is mapped
64    error = vmm_get_vseg( process , (intptr_t)attr , &vseg );
65
66    if( error )
67    {
68
69#if DEBUG_SYSCALLS_ERROR
70printk("\n[ERROR] in %s : thread[%x,%x] / mmap attributes unmapped %x\n",
71__FUNCTION__ , process->pid, this->trdid, (intptr_t)attr );
72#endif
73                this->errno = EINVAL;
74                return -1;
75    }
76
77    // copy attributes from user space to kernel space
78    hal_copy_from_uspace( local_cxy,
79                          &k_attr,
80                          attr,
81                          sizeof(mmap_attr_t) );
82
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;
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    {
98
99#if DEBUG_SYSCALLS_ERROR
100printk("\n[ERROR] in %s : thread[%x,%x] / MAP_FIXED not supported\n",
101__FUNCTION__ , process->pid, this->trdid );
102#endif
103        this->errno = EINVAL;
104        return -1;
105    }
106
107    if( map_shared == map_private )
108    {
109
110#if DEBUG_SYSCALLS_ERROR
111printk("\n[ERROR] in %s : thread[%x,%x] / MAP_SHARED == MAP_PRIVATE\n",
112__FUNCTION__ , process->pid, this->trdid );
113#endif
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
122    /////////////////////////////////////////////////////////// MAP_FILE
123    if( (map_anon == false) && (map_remote == false) )   
124    {
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
132            // FIXME: handle concurent delete of file by another thread closing it
133
134                if( fdid >= CONFIG_PROCESS_FILE_MAX_NR ) 
135                {
136
137#if DEBUG_SYSCALLS_ERROR
138printk("\n[ERROR] in %s : thread[%x,%x] / bad file descriptor %d\n",
139__FUNCTION__ , process->pid , this->trdid , fdid );
140#endif
141            this->errno = EBADFD;
142            return -1;
143        }
144
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        {
150
151#if DEBUG_SYSCALLS_ERROR
152printk("\n[ERROR] in %s : thread[%x,%x] / file descriptor %d not found\n",
153__FUNCTION__  , this->trdid , process->pid , fdid );
154#endif
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
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
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
174                uint32_t size = hal_remote_l32( XPTR( file_cxy , &inode_ptr->size ) );
175
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
182        // chek offset and length arguments
183                if( (offset + length) > size)
184                {
185
186#if DEBUG_SYSCALLS_ERROR
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 );
189#endif
190            this->errno = ERANGE;
191            return -1;
192                }
193
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
200        // check access rights
201                if( (prot_read  && !(file_attr & FD_ATTR_READ_ENABLE)) ||
202                    (prot_write && !(file_attr & FD_ATTR_WRITE_ENABLE)) )
203                {
204
205#if DEBUG_SYSCALLS_ERROR
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 );
208#endif
209                        this->errno = EACCES;
210                        return -1;
211                }
212*/
213
214                // increment file refcount
215                vfs_file_count_up( file_xp );
216
217        mapper_xp = XPTR( file_cxy , mapper_ptr );
218        vseg_type = VSEG_TYPE_FILE;
219        vseg_cxy  = file_cxy;
220    }
221    ///////////////////////////////////////////////////////// MAP_ANON
222    else if ( map_anon )                                 
223    {
224        mapper_xp = XPTR_NULL;
225        vseg_type = VSEG_TYPE_ANON;
226        vseg_cxy  = local_cxy;
227
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    } 
235    /////////////////////////////////////////////////////// MAP_REMOTE
236    else                                                 
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 ) )
249        {
250
251#if DEBUG_SYSCALLS_ERROR
252printk("\n[ERROR] in %s : thread[%x,%x] / illegal cxy %x for REMOTE\n",
253__FUNCTION__, this->trdid , process->pid, vseg_cxy );
254#endif
255            this->errno = EINVAL;
256            return -1;
257        }
258    }
259
260    // enable IRQs
261    hal_enable_irq( &save_sr );
262
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 );
266    process_t * ref_ptr = GET_PTR( ref_xp );
267
268    // create the vseg in reference cluster
269    if( local_cxy == ref_cxy )
270    {
271        vseg = vmm_create_vseg( process,
272                                vseg_type,
273                                0,               // vseg base (unused for mmap)
274                                length,          // vseg size
275                                offset,          // file offset
276                                0,               // file_size (unused for mmap)
277                                mapper_xp,
278                                vseg_cxy );
279    }
280    else
281    {
282        rpc_vmm_create_vseg_client( ref_cxy,
283                                    ref_ptr,
284                                    vseg_type,
285                                    0,            // vseg base (unused for mmap)
286                                    length,       // vseg size
287                                    offset,       // file offset
288                                    0,            // file size (unused for mmap)
289                                    mapper_xp,
290                                    vseg_cxy,
291                                    &vseg ); 
292    }
293   
294    // restore IRQs
295    hal_restore_irq( save_sr );
296
297    if( vseg == NULL )
298    {
299
300#if DEBUG_SYSCALLS_ERROR
301printk("\n[ERROR] in %s : thread[%x,%x] / cannot create vseg\n",
302__FUNCTION__, process->pid, this->trdid );
303#endif
304        this->errno = ENOMEM;
305        return -1;
306    }
307
308    // copy vseg base address to user space
309    hal_copy_to_uspace( local_cxy,
310                        &vseg->min,
311                        &attr->addr,
312                        sizeof(intptr_t) );
313    hal_fence();
314
315#if (DEBUG_SYS_MMAP || CONFIG_INSTRUMENTATION_SYSCALLS)
316uint64_t     tm_end = hal_get_cycles();
317#endif
318
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
324#if DEBUG_SYS_MMAP
325if ( DEBUG_SYS_MMAP < tm_end )
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 );
329#endif
330
331        return 0;
332
333}  // end sys_mmap()
334
Note: See TracBrowser for help on using the repository browser.