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

Last change on this file since 666 was 664, checked in by alain, 4 years ago
  • Introduce the sys_socket.c file implementing all socket related syscalls.
  • Improve the non-standard sys_get_config() function.
File size: 9.5 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,2020)
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 <errno.h>
29#include <thread.h>
30#include <printk.h>
31#include <mapper.h>
32#include <vfs.h>
33#include <process.h>
34#include <vmm.h>
35
36#include <syscalls.h>
37#include <shared_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_from_local( 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 mapper pointer
167        mapper_t    * mapper_ptr = hal_remote_lpt(XPTR(file_cxy , &file_ptr->mapper));
168
169#if (DEBUG_SYS_MMAP & 1)
170if ( DEBUG_SYS_MMAP < tm_start )
171printk("\n[%s] thread[%x,%x] get file mapper %x\n",
172__FUNCTION__, process->pid, this->trdid, mapper_ptr );
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
181        // check access rights
182                if( (prot_read  && !(file_attr & FD_ATTR_READ_ENABLE)) ||
183                    (prot_write && !(file_attr & FD_ATTR_WRITE_ENABLE)) )
184                {
185
186#if DEBUG_SYSCALLS_ERROR
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 );
189#endif
190                        this->errno = EACCES;
191                        return -1;
192                }
193*/
194
195                // increment file refcount
196                vfs_file_count_up( file_xp );
197
198        mapper_xp = XPTR( file_cxy , mapper_ptr );
199        vseg_type = VSEG_TYPE_FILE;
200        vseg_cxy  = file_cxy;
201    }
202    ///////////////////////////////////////////////////////// MAP_ANON
203    else if ( map_anon )                                 
204    {
205        mapper_xp = XPTR_NULL;
206        vseg_type = VSEG_TYPE_ANON;
207        vseg_cxy  = local_cxy;
208
209#if (DEBUG_SYS_MMAP & 1)
210if ( DEBUG_SYS_MMAP < tm_start )
211printk("\n[%s] thread[%x,%x] type anon / %x bytes / cluster %x\n",
212__FUNCTION__, process->pid, this->trdid, length, vseg_cxy );
213#endif
214
215    } 
216    /////////////////////////////////////////////////////// MAP_REMOTE
217    else                                                 
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 )
225printk("\n[%s] thread[%x,%x] type remote / %x bytes / target cluster %x\n",
226__FUNCTION__, process->pid, this->trdid, length, vseg_cxy );
227#endif
228 
229        if( cluster_is_active( vseg_cxy ) == false )
230        {
231
232#if DEBUG_SYSCALLS_ERROR
233printk("\n[ERROR] in %s : thread[%x,%x] / illegal cxy %x for REMOTE\n",
234__FUNCTION__, this->trdid , process->pid, vseg_cxy );
235#endif
236            this->errno = EINVAL;
237            return -1;
238        }
239    }
240
241    // enable IRQs
242    hal_enable_irq( &save_sr );
243
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 );
247    process_t * ref_ptr = GET_PTR( ref_xp );
248
249    // register vseg in reference VSL
250    if( local_cxy == ref_cxy )
251    {
252        vseg = vmm_create_vseg( process,
253                                vseg_type,
254                                0,               // vseg base (unused for mmap)
255                                length,          // vseg size
256                                offset,          // file offset
257                                0,               // file_size (unused for mmap)
258                                mapper_xp,
259                                vseg_cxy );
260    }
261    else
262    {
263        rpc_vmm_create_vseg_client( ref_cxy,
264                                    ref_ptr,
265                                    vseg_type,
266                                    0,            // vseg base (unused for mmap)
267                                    length,       // vseg size
268                                    offset,       // file offset
269                                    0,            // file size (unused for mmap)
270                                    mapper_xp,
271                                    vseg_cxy,
272                                    &vseg ); 
273    }
274   
275    // restore IRQs
276    hal_restore_irq( save_sr );
277
278    if( vseg == NULL )
279    {
280
281#if DEBUG_SYSCALLS_ERROR
282printk("\n[ERROR] in %s : thread[%x,%x] / cannot create vseg\n",
283__FUNCTION__, process->pid, this->trdid );
284#endif
285        this->errno = ENOMEM;
286        return -1;
287    }
288
289    // copy vseg base address to user space mmap_attr_t
290    hal_copy_to_uspace( &attr->addr,
291                        XPTR( ref_cxy , &vseg->min ),
292                        sizeof(intptr_t) );
293    hal_fence();
294
295#if (DEBUG_SYS_MMAP || CONFIG_INSTRUMENTATION_SYSCALLS)
296uint64_t     tm_end = hal_get_cycles();
297#endif
298
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
304#if DEBUG_SYS_MMAP
305if ( DEBUG_SYS_MMAP < tm_end )
306printk("\n[%s] thread[%x,%x] exit / %s / cxy %x / base %x / size %x / cycle %d\n",
307__FUNCTION__, process->pid, this->trdid,
308vseg_type_str(vseg->type), vseg->cxy, vseg->min, length, (uint32_t)tm_end );
309#endif
310
311        return 0;
312
313}  // end sys_mmap()
314
Note: See TracBrowser for help on using the repository browser.