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

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

Introduce sigificant modifs in VFS to support the <ls> command,
and the . and .. directories entries.

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