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

Last change on this file since 672 was 670, checked in by alain, 4 years ago

1) Introduce up to 4 command lines arguments in the KSH "load" command.
These arguments are transfered to the user process through the
argc/argv mechanism, using the user space "args" vseg.

2) Introduce the named and anonymous "pipes", for inter-process communication
through the pipe() and mkfifo() syscalls.

3) Introduce the "chat" application to validate the two above mechanisms.

4) Improve printk() and assert() fonctions in printk.c.

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        mapper_xp = XPTR( file_cxy , mapper_ptr );
196        vseg_type = VSEG_TYPE_FILE;
197        vseg_cxy  = file_cxy;
198    }
199    ///////////////////////////////////////////////////////// MAP_ANON
200    else if ( map_anon )                                 
201    {
202        mapper_xp = XPTR_NULL;
203        vseg_type = VSEG_TYPE_ANON;
204        vseg_cxy  = local_cxy;
205
206#if (DEBUG_SYS_MMAP & 1)
207if ( DEBUG_SYS_MMAP < tm_start )
208printk("\n[%s] thread[%x,%x] type anon / %x bytes / cluster %x\n",
209__FUNCTION__, process->pid, this->trdid, length, vseg_cxy );
210#endif
211
212    } 
213    /////////////////////////////////////////////////////// MAP_REMOTE
214    else                                                 
215    {
216        mapper_xp = XPTR_NULL;
217        vseg_type = VSEG_TYPE_REMOTE;
218        vseg_cxy  = k_attr.fdid;
219
220#if (DEBUG_SYS_MMAP & 1)
221if ( DEBUG_SYS_MMAP < tm_start )
222printk("\n[%s] thread[%x,%x] type remote / %x bytes / target cluster %x\n",
223__FUNCTION__, process->pid, this->trdid, length, vseg_cxy );
224#endif
225 
226        if( cluster_is_active( vseg_cxy ) == false )
227        {
228
229#if DEBUG_SYSCALLS_ERROR
230printk("\n[ERROR] in %s : thread[%x,%x] / illegal cxy %x for REMOTE\n",
231__FUNCTION__, this->trdid , process->pid, vseg_cxy );
232#endif
233            this->errno = EINVAL;
234            return -1;
235        }
236    }
237
238    // enable IRQs
239    hal_enable_irq( &save_sr );
240
241    // get reference process cluster and local pointer
242    xptr_t      ref_xp  = process->ref_xp;
243    cxy_t       ref_cxy = GET_CXY( ref_xp );
244    process_t * ref_ptr = GET_PTR( ref_xp );
245
246    // register vseg in reference VSL
247    if( local_cxy == ref_cxy )
248    {
249        vseg = vmm_create_vseg( process,
250                                vseg_type,
251                                0,               // vseg base (unused for mmap)
252                                length,          // vseg size
253                                offset,          // file offset
254                                0,               // file_size (unused for mmap)
255                                mapper_xp,
256                                vseg_cxy );
257    }
258    else
259    {
260        rpc_vmm_create_vseg_client( ref_cxy,
261                                    ref_ptr,
262                                    vseg_type,
263                                    0,            // vseg base (unused for mmap)
264                                    length,       // vseg size
265                                    offset,       // file offset
266                                    0,            // file size (unused for mmap)
267                                    mapper_xp,
268                                    vseg_cxy,
269                                    &vseg ); 
270    }
271   
272    // restore IRQs
273    hal_restore_irq( save_sr );
274
275    if( vseg == NULL )
276    {
277
278#if DEBUG_SYSCALLS_ERROR
279printk("\n[ERROR] in %s : thread[%x,%x] / cannot create vseg\n",
280__FUNCTION__, process->pid, this->trdid );
281#endif
282        this->errno = ENOMEM;
283        return -1;
284    }
285
286    // copy vseg base address to user space mmap_attr_t
287    hal_copy_to_uspace( &attr->addr,
288                        XPTR( ref_cxy , &vseg->min ),
289                        sizeof(intptr_t) );
290    hal_fence();
291
292#if (DEBUG_SYS_MMAP || CONFIG_INSTRUMENTATION_SYSCALLS)
293uint64_t     tm_end = hal_get_cycles();
294#endif
295
296#if CONFIG_INSTRUMENTATION_SYSCALLS
297hal_atomic_add( &syscalls_cumul_cost[SYS_MMAP] , tm_end - tm_start );
298hal_atomic_add( &syscalls_occurences[SYS_MMAP] , 1 );
299#endif
300
301#if DEBUG_SYS_MMAP
302if ( DEBUG_SYS_MMAP < tm_end )
303printk("\n[%s] thread[%x,%x] exit / %s / cxy %x / base %x / size %x / cycle %d\n",
304__FUNCTION__, process->pid, this->trdid,
305vseg_type_str(vseg->type), vseg->cxy, vseg->min, length, (uint32_t)tm_end );
306#endif
307
308        return 0;
309
310}  // end sys_mmap()
311
Note: See TracBrowser for help on using the repository browser.