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