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 | ////////////////////////////////// |
---|
40 | int 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 |
---|
54 | uint64_t tm_start; |
---|
55 | uint64_t tm_end; |
---|
56 | tm_start = hal_get_cycles(); |
---|
57 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
58 | printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n", |
---|
59 | __FUNCTION__, this, process->pid, (uint32_t)tm_start ); |
---|
60 | #endif |
---|
61 | |
---|
62 | // check arguments in user space |
---|
63 | error = vmm_get_vseg( process , (intptr_t)attr , &vseg ); |
---|
64 | |
---|
65 | if ( error ) |
---|
66 | { |
---|
67 | |
---|
68 | #if DEBUG_SYSCALLS_ERROR |
---|
69 | printk("\n[ERROR] in %s : user buffer unmapped %x / thread %x / process %x\n", |
---|
70 | __FUNCTION__ , (intptr_t)attr , this->trdid , process->pid ); |
---|
71 | vmm_display( process , false ); |
---|
72 | #endif |
---|
73 | this->errno = EINVAL; |
---|
74 | return -1; |
---|
75 | } |
---|
76 | |
---|
77 | // copy arguments from uspace |
---|
78 | hal_copy_from_uspace( &k_attr , attr , sizeof(mmap_attr_t) ); |
---|
79 | |
---|
80 | // get fdid, offset, and length arguments |
---|
81 | uint32_t fdid = k_attr.fdid; |
---|
82 | uint32_t offset = k_attr.offset; |
---|
83 | uint32_t length = k_attr.length; |
---|
84 | |
---|
85 | // get flags |
---|
86 | bool_t map_fixed = ( (k_attr.flags & MAP_FIXED) != 0 ); |
---|
87 | bool_t map_anon = ( (k_attr.flags & MAP_ANON) != 0 ); |
---|
88 | bool_t map_remote = ( (k_attr.flags & MAP_REMOTE) != 0 ); |
---|
89 | bool_t map_shared = ( (k_attr.flags & MAP_SHARED) != 0 ); |
---|
90 | bool_t map_private = ( (k_attr.flags & MAP_PRIVATE) != 0 ); |
---|
91 | |
---|
92 | // MAP_FIXED not supported |
---|
93 | if( map_fixed ) |
---|
94 | { |
---|
95 | |
---|
96 | #if DEBUG_SYSCALLS_ERROR |
---|
97 | printk("\n[ERROR] in %s : MAP_FIXED not supported / thread %x / process %x\n", |
---|
98 | __FUNCTION__ , this->trdid , process->pid ); |
---|
99 | #endif |
---|
100 | this->errno = EINVAL; |
---|
101 | return -1; |
---|
102 | } |
---|
103 | |
---|
104 | if( map_shared == map_private ) |
---|
105 | { |
---|
106 | |
---|
107 | #if DEBUG_SYSCALLS_ERROR |
---|
108 | printk("\n[ERROR] in %s : MAP_SHARED == MAP_PRIVATE / thread %x / process %x\n", |
---|
109 | __FUNCTION__ , this->trdid , process->pid ); |
---|
110 | #endif |
---|
111 | this->errno = EINVAL; |
---|
112 | return -1; |
---|
113 | } |
---|
114 | |
---|
115 | // FIXME handle Copy_On_Write for MAP_PRIVATE... |
---|
116 | |
---|
117 | // get access rigths |
---|
118 | bool_t prot_read = ( (k_attr.prot & PROT_READ ) != 0 ); |
---|
119 | bool_t prot_write = ( (k_attr.prot & PROT_WRITE) != 0 ); |
---|
120 | |
---|
121 | // test mmap type : can be FILE / ANON / REMOTE |
---|
122 | |
---|
123 | if( (map_anon == false) && (map_remote == false) ) // FILE |
---|
124 | { |
---|
125 | // FIXME: handle concurent delete of file by another thread closing it |
---|
126 | |
---|
127 | if( fdid >= CONFIG_PROCESS_FILE_MAX_NR ) |
---|
128 | { |
---|
129 | |
---|
130 | #if DEBUG_SYSCALLS_ERROR |
---|
131 | printk("\n[ERROR] in %s: bad file descriptor %d / thread %x / process %x\n", |
---|
132 | __FUNCTION__ , fdid , this->trdid , process->pid ); |
---|
133 | #endif |
---|
134 | this->errno = EBADFD; |
---|
135 | return -1; |
---|
136 | } |
---|
137 | |
---|
138 | // get extended pointer on file descriptor |
---|
139 | xptr_t file_xp = process_fd_get_xptr( process , fdid ); |
---|
140 | |
---|
141 | if( file_xp == XPTR_NULL ) |
---|
142 | { |
---|
143 | |
---|
144 | #if DEBUG_SYSCALLS_ERROR |
---|
145 | printk("\n[ERROR] in %s: file %d not found / thread %x / process %x\n", |
---|
146 | __FUNCTION__ , fdid , this->trdid , process->pid ); |
---|
147 | #endif |
---|
148 | this->errno = EBADFD; |
---|
149 | return -1; |
---|
150 | } |
---|
151 | |
---|
152 | // get file cluster and local pointer |
---|
153 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
154 | vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); |
---|
155 | |
---|
156 | // get inode pointer, mapper pointer and file attributes |
---|
157 | vfs_inode_t * inode_ptr = hal_remote_lpt(XPTR(file_cxy , &file_ptr->inode )); |
---|
158 | uint32_t file_attr = hal_remote_l32 (XPTR(file_cxy , &file_ptr->attr )); |
---|
159 | mapper_t * mapper_ptr = hal_remote_lpt(XPTR(file_cxy , &file_ptr->mapper)); |
---|
160 | |
---|
161 | // get file size |
---|
162 | uint32_t size = hal_remote_l32( XPTR( file_cxy , &inode_ptr->size ) ); |
---|
163 | |
---|
164 | // chek offset and length arguments |
---|
165 | if( (offset + length) > size) |
---|
166 | { |
---|
167 | |
---|
168 | #if DEBUG_SYSCALLS_ERROR |
---|
169 | printk("\n[ERROR] in %s: offset(%d) + len(%d) >= file's size(%d) / thread %x / process %x\n", |
---|
170 | __FUNCTION__, k_attr.offset, k_attr.length, size, this->trdid, process->pid ); |
---|
171 | #endif |
---|
172 | this->errno = ERANGE; |
---|
173 | return -1; |
---|
174 | } |
---|
175 | |
---|
176 | // check access rights |
---|
177 | if( (prot_read && !(file_attr & FD_ATTR_READ_ENABLE)) || |
---|
178 | (prot_write && !(file_attr & FD_ATTR_WRITE_ENABLE)) ) |
---|
179 | { |
---|
180 | |
---|
181 | #if DEBUG_SYSCALLS_ERROR |
---|
182 | printk("\n[ERROR] in %s: prot = %x / file_attr = %x / thread %x , process %x\n", |
---|
183 | __FUNCTION__ , k_attr.prot , file_attr , this->trdid , process->pid ); |
---|
184 | #endif |
---|
185 | this->errno = EACCES; |
---|
186 | return -1; |
---|
187 | } |
---|
188 | |
---|
189 | // increment file refcount |
---|
190 | vfs_file_count_up( file_xp ); |
---|
191 | |
---|
192 | mapper_xp = XPTR( file_cxy , mapper_ptr ); |
---|
193 | vseg_type = VSEG_TYPE_FILE; |
---|
194 | vseg_cxy = file_cxy; |
---|
195 | } |
---|
196 | else // ANON or REMOTE |
---|
197 | { |
---|
198 | // no mapper for ANON or REMOTE |
---|
199 | mapper_xp = XPTR_NULL; |
---|
200 | |
---|
201 | if( map_anon ) |
---|
202 | { |
---|
203 | vseg_type = VSEG_TYPE_ANON; |
---|
204 | vseg_cxy = local_cxy; |
---|
205 | } |
---|
206 | else |
---|
207 | { |
---|
208 | vseg_type = VSEG_TYPE_REMOTE; |
---|
209 | vseg_cxy = k_attr.fdid; |
---|
210 | |
---|
211 | if( cluster_is_undefined( vseg_cxy ) ) |
---|
212 | { |
---|
213 | |
---|
214 | #if DEBUG_SYSCALLS_ERROR |
---|
215 | printk("\n[ERROR] in %s : illegal cxy for MAP_REMOTE / thread %x / process %x\n", |
---|
216 | __FUNCTION__, this->trdid , process->pid ); |
---|
217 | #endif |
---|
218 | this->errno = EINVAL; |
---|
219 | return -1; |
---|
220 | } |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | // enable IRQs |
---|
225 | hal_enable_irq( &save_sr ); |
---|
226 | |
---|
227 | // get reference process cluster and local pointer |
---|
228 | xptr_t ref_xp = process->ref_xp; |
---|
229 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
230 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
231 | |
---|
232 | // create the vseg in reference cluster |
---|
233 | if( local_cxy == ref_cxy ) |
---|
234 | { |
---|
235 | vseg = vmm_create_vseg( process, |
---|
236 | vseg_type, |
---|
237 | 0, // base address unused for mmap() |
---|
238 | length, |
---|
239 | offset, |
---|
240 | 0, // file_size unused for mmap() |
---|
241 | mapper_xp, |
---|
242 | vseg_cxy ); |
---|
243 | } |
---|
244 | else |
---|
245 | { |
---|
246 | rpc_vmm_create_vseg_client( ref_cxy, |
---|
247 | ref_ptr, |
---|
248 | vseg_type, |
---|
249 | 0, // base address unused for mmap() |
---|
250 | length, |
---|
251 | offset, |
---|
252 | 0, // file size unused for mmap() |
---|
253 | mapper_xp, |
---|
254 | vseg_cxy, |
---|
255 | &vseg ); |
---|
256 | } |
---|
257 | |
---|
258 | // restore IRQs |
---|
259 | hal_restore_irq( save_sr ); |
---|
260 | |
---|
261 | if( vseg == NULL ) |
---|
262 | { |
---|
263 | |
---|
264 | #if DEBUG_SYSCALLS_ERROR |
---|
265 | printk("\n[ERROR] in %s : cannot create vseg / thread %x / process %x\n", |
---|
266 | __FUNCTION__, this->trdid , process->pid ); |
---|
267 | #endif |
---|
268 | this->errno = ENOMEM; |
---|
269 | return -1; |
---|
270 | } |
---|
271 | |
---|
272 | // copy vseg base address to user space |
---|
273 | hal_copy_to_uspace( &attr->addr , &vseg->min , sizeof(intptr_t) ); |
---|
274 | |
---|
275 | hal_fence(); |
---|
276 | |
---|
277 | #if DEBUG_SYS_MMAP |
---|
278 | tm_end = hal_get_cycles(); |
---|
279 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
280 | printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n" |
---|
281 | "vseg %s / cluster %x / base %x / size %x / cost %d\n", |
---|
282 | __FUNCTION__, this, process->pid, (uint32_t)tm_end, |
---|
283 | vseg_type_str(vseg->type), vseg->cxy, vseg->min, length, (uint32_t)(tm_end - tm_start) ); |
---|
284 | #endif |
---|
285 | |
---|
286 | return 0; |
---|
287 | |
---|
288 | } // end sys_mmap() |
---|
289 | |
---|