]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 A |
2 | * Copyright (c) 2007 Apple Inc. All Rights Reserved. |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
1c79356b | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
8f6c56a5 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1988 University of Utah. | |
30 | * Copyright (c) 1991, 1993 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * | |
33 | * This code is derived from software contributed to Berkeley by | |
34 | * the Systems Programming Group of the University of Utah Computer | |
35 | * Science Department. | |
36 | * | |
37 | * Redistribution and use in source and binary forms, with or without | |
38 | * modification, are permitted provided that the following conditions | |
39 | * are met: | |
40 | * 1. Redistributions of source code must retain the above copyright | |
41 | * notice, this list of conditions and the following disclaimer. | |
42 | * 2. Redistributions in binary form must reproduce the above copyright | |
43 | * notice, this list of conditions and the following disclaimer in the | |
44 | * documentation and/or other materials provided with the distribution. | |
45 | * 3. All advertising materials mentioning features or use of this software | |
46 | * must display the following acknowledgement: | |
47 | * This product includes software developed by the University of | |
48 | * California, Berkeley and its contributors. | |
49 | * 4. Neither the name of the University nor the names of its contributors | |
50 | * may be used to endorse or promote products derived from this software | |
51 | * without specific prior written permission. | |
52 | * | |
53 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
54 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
55 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
56 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
57 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
58 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
59 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
60 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
61 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
62 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
63 | * SUCH DAMAGE. | |
64 | * | |
65 | * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$ | |
66 | * | |
67 | * @(#)vm_mmap.c 8.10 (Berkeley) 2/19/95 | |
68 | */ | |
2d21ac55 A |
69 | /* |
70 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
71 | * support for mandatory and extensible security protections. This notice | |
72 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
73 | * Version 2.0. | |
74 | */ | |
1c79356b A |
75 | |
76 | /* | |
77 | * Mapped file (mmap) interface to VM | |
78 | */ | |
79 | ||
80 | #include <sys/param.h> | |
81 | #include <sys/systm.h> | |
82 | #include <sys/filedesc.h> | |
91447636 A |
83 | #include <sys/proc_internal.h> |
84 | #include <sys/kauth.h> | |
1c79356b | 85 | #include <sys/resourcevar.h> |
91447636 | 86 | #include <sys/vnode_internal.h> |
1c79356b A |
87 | #include <sys/acct.h> |
88 | #include <sys/wait.h> | |
91447636 | 89 | #include <sys/file_internal.h> |
1c79356b A |
90 | #include <sys/vadvise.h> |
91 | #include <sys/trace.h> | |
92 | #include <sys/mman.h> | |
93 | #include <sys/conf.h> | |
94 | #include <sys/stat.h> | |
95 | #include <sys/ubc.h> | |
2d21ac55 | 96 | #include <sys/ubc_internal.h> |
91447636 | 97 | #include <sys/sysproto.h> |
1c79356b | 98 | |
2d21ac55 A |
99 | #include <sys/syscall.h> |
100 | #include <sys/kdebug.h> | |
101 | ||
b0d623f7 | 102 | #include <security/audit/audit.h> |
e5568f75 A |
103 | #include <bsm/audit_kevents.h> |
104 | ||
1c79356b | 105 | #include <mach/mach_types.h> |
91447636 A |
106 | #include <mach/mach_traps.h> |
107 | #include <mach/vm_sync.h> | |
108 | #include <mach/vm_behavior.h> | |
109 | #include <mach/vm_inherit.h> | |
110 | #include <mach/vm_statistics.h> | |
111 | #include <mach/mach_vm.h> | |
112 | #include <mach/vm_map.h> | |
113 | #include <mach/host_priv.h> | |
1c79356b A |
114 | |
115 | #include <kern/cpu_number.h> | |
91447636 | 116 | #include <kern/host.h> |
1c79356b A |
117 | |
118 | #include <vm/vm_map.h> | |
119 | #include <vm/vm_kern.h> | |
120 | #include <vm/vm_pager.h> | |
b0d623f7 | 121 | #include <vm/vm_protos.h> |
2d21ac55 A |
122 | |
123 | /* XXX the following function should probably be static */ | |
124 | kern_return_t map_fd_funneled(int, vm_object_offset_t, vm_offset_t *, | |
125 | boolean_t, vm_size_t); | |
126 | ||
2d21ac55 A |
127 | /* |
128 | * XXX Internally, we use VM_PROT_* somewhat interchangeably, but the correct | |
129 | * XXX usage is PROT_* from an interface perspective. Thus the values of | |
130 | * XXX VM_PROT_* and PROT_* need to correspond. | |
131 | */ | |
1c79356b | 132 | int |
2d21ac55 | 133 | mmap(proc_t p, struct mmap_args *uap, user_addr_t *retval) |
1c79356b A |
134 | { |
135 | /* | |
136 | * Map in special device (must be SHARED) or file | |
137 | */ | |
91447636 | 138 | struct fileproc *fp; |
1c79356b A |
139 | register struct vnode *vp; |
140 | int flags; | |
593a1d5f | 141 | int prot; |
1c79356b A |
142 | int err=0; |
143 | vm_map_t user_map; | |
144 | kern_return_t result; | |
91447636 A |
145 | mach_vm_offset_t user_addr; |
146 | mach_vm_size_t user_size; | |
147 | vm_object_offset_t pageoff; | |
1c79356b | 148 | vm_object_offset_t file_pos; |
2d21ac55 | 149 | int alloc_flags=0; |
91447636 | 150 | boolean_t docow; |
1c79356b A |
151 | vm_prot_t maxprot; |
152 | void *handle; | |
b0d623f7 A |
153 | memory_object_t pager = MEMORY_OBJECT_NULL; |
154 | memory_object_control_t control; | |
1c79356b | 155 | int mapanon=0; |
91447636 A |
156 | int fpref=0; |
157 | int error =0; | |
158 | int fd = uap->fd; | |
1c79356b | 159 | |
91447636 A |
160 | user_addr = (mach_vm_offset_t)uap->addr; |
161 | user_size = (mach_vm_size_t) uap->len; | |
162 | ||
163 | AUDIT_ARG(addr, user_addr); | |
164 | AUDIT_ARG(len, user_size); | |
e5568f75 A |
165 | AUDIT_ARG(fd, uap->fd); |
166 | ||
1c79356b | 167 | prot = (uap->prot & VM_PROT_ALL); |
2d21ac55 A |
168 | #if 3777787 |
169 | /* | |
170 | * Since the hardware currently does not support writing without | |
171 | * read-before-write, or execution-without-read, if the request is | |
172 | * for write or execute access, we must imply read access as well; | |
173 | * otherwise programs expecting this to work will fail to operate. | |
174 | */ | |
175 | if (prot & (VM_PROT_EXECUTE | VM_PROT_WRITE)) | |
176 | prot |= VM_PROT_READ; | |
177 | #endif /* radar 3777787 */ | |
178 | ||
1c79356b | 179 | flags = uap->flags; |
91447636 | 180 | vp = NULLVP; |
1c79356b A |
181 | |
182 | /* | |
183 | * The vm code does not have prototypes & compiler doesn't do the' | |
184 | * the right thing when you cast 64bit value and pass it in function | |
185 | * call. So here it is. | |
186 | */ | |
187 | file_pos = (vm_object_offset_t)uap->pos; | |
188 | ||
189 | ||
190 | /* make sure mapping fits into numeric range etc */ | |
2d21ac55 | 191 | if (file_pos + user_size > (vm_object_offset_t)-PAGE_SIZE_64) |
1c79356b A |
192 | return (EINVAL); |
193 | ||
194 | /* | |
195 | * Align the file position to a page boundary, | |
196 | * and save its page offset component. | |
197 | */ | |
91447636 | 198 | pageoff = (file_pos & PAGE_MASK); |
1c79356b A |
199 | file_pos -= (vm_object_offset_t)pageoff; |
200 | ||
201 | ||
202 | /* Adjust size for rounding (on both ends). */ | |
203 | user_size += pageoff; /* low end... */ | |
91447636 | 204 | user_size = mach_vm_round_page(user_size); /* hi end */ |
1c79356b A |
205 | |
206 | ||
207 | /* | |
208 | * Check for illegal addresses. Watch out for address wrap... Note | |
209 | * that VM_*_ADDRESS are not constants due to casts (argh). | |
210 | */ | |
211 | if (flags & MAP_FIXED) { | |
212 | /* | |
213 | * The specified address must have the same remainder | |
214 | * as the file offset taken modulo PAGE_SIZE, so it | |
215 | * should be aligned after adjustment by pageoff. | |
216 | */ | |
217 | user_addr -= pageoff; | |
218 | if (user_addr & PAGE_MASK) | |
2d21ac55 | 219 | return (EINVAL); |
1c79356b A |
220 | } |
221 | #ifdef notyet | |
222 | /* DO not have apis to get this info, need to wait till then*/ | |
223 | /* | |
224 | * XXX for non-fixed mappings where no hint is provided or | |
225 | * the hint would fall in the potential heap space, | |
226 | * place it after the end of the largest possible heap. | |
227 | * | |
228 | * There should really be a pmap call to determine a reasonable | |
229 | * location. | |
230 | */ | |
91447636 A |
231 | else if (addr < mach_vm_round_page(p->p_vmspace->vm_daddr + MAXDSIZ)) |
232 | addr = mach_vm_round_page(p->p_vmspace->vm_daddr + MAXDSIZ); | |
1c79356b A |
233 | |
234 | #endif | |
235 | ||
2d21ac55 | 236 | alloc_flags = 0; |
1c79356b A |
237 | |
238 | if (flags & MAP_ANON) { | |
239 | /* | |
2d21ac55 A |
240 | * Mapping blank space is trivial. Use positive fds as the alias |
241 | * value for memory tracking. | |
1c79356b | 242 | */ |
2d21ac55 A |
243 | if (fd != -1) { |
244 | /* | |
245 | * Use "fd" to pass (some) Mach VM allocation flags, | |
246 | * (see the VM_FLAGS_* definitions). | |
247 | */ | |
248 | alloc_flags = fd & (VM_FLAGS_ALIAS_MASK | | |
249 | VM_FLAGS_PURGABLE); | |
250 | if (alloc_flags != fd) { | |
251 | /* reject if there are any extra flags */ | |
252 | return EINVAL; | |
253 | } | |
254 | } | |
255 | ||
1c79356b A |
256 | handle = NULL; |
257 | maxprot = VM_PROT_ALL; | |
258 | file_pos = 0; | |
259 | mapanon = 1; | |
260 | } else { | |
91447636 | 261 | struct vnode_attr va; |
2d21ac55 A |
262 | vfs_context_t ctx = vfs_context_current(); |
263 | ||
1c79356b A |
264 | /* |
265 | * Mapping file, get fp for validation. Obtain vnode and make | |
266 | * sure it is of appropriate type. | |
267 | */ | |
91447636 | 268 | err = fp_lookup(p, fd, &fp, 0); |
1c79356b A |
269 | if (err) |
270 | return(err); | |
91447636 A |
271 | fpref = 1; |
272 | if(fp->f_fglob->fg_type == DTYPE_PSXSHM) { | |
273 | uap->addr = (user_addr_t)user_addr; | |
274 | uap->len = (user_size_t)user_size; | |
1c79356b A |
275 | uap->prot = prot; |
276 | uap->flags = flags; | |
277 | uap->pos = file_pos; | |
91447636 A |
278 | error = pshm_mmap(p, uap, retval, fp, (off_t)pageoff); |
279 | goto bad; | |
1c79356b A |
280 | } |
281 | ||
91447636 A |
282 | if (fp->f_fglob->fg_type != DTYPE_VNODE) { |
283 | error = EINVAL; | |
284 | goto bad; | |
285 | } | |
286 | vp = (struct vnode *)fp->f_fglob->fg_data; | |
287 | error = vnode_getwithref(vp); | |
288 | if(error != 0) | |
289 | goto bad; | |
290 | ||
291 | if (vp->v_type != VREG && vp->v_type != VCHR) { | |
292 | (void)vnode_put(vp); | |
293 | error = EINVAL; | |
294 | goto bad; | |
295 | } | |
e5568f75 A |
296 | |
297 | AUDIT_ARG(vnpath, vp, ARG_VNODE1); | |
91447636 | 298 | |
2d21ac55 A |
299 | /* |
300 | * POSIX: mmap needs to update access time for mapped files | |
91447636 | 301 | */ |
2d21ac55 A |
302 | if ((vnode_vfsvisflags(vp) & MNT_NOATIME) == 0) { |
303 | VATTR_INIT(&va); | |
304 | nanotime(&va.va_access_time); | |
305 | VATTR_SET_ACTIVE(&va, va_access_time); | |
306 | vnode_setattr(vp, &va, ctx); | |
307 | } | |
b0d623f7 | 308 | |
1c79356b A |
309 | /* |
310 | * XXX hack to handle use of /dev/zero to map anon memory (ala | |
311 | * SunOS). | |
312 | */ | |
313 | if (vp->v_type == VCHR || vp->v_type == VSTR) { | |
91447636 A |
314 | (void)vnode_put(vp); |
315 | error = ENODEV; | |
316 | goto bad; | |
1c79356b A |
317 | } else { |
318 | /* | |
319 | * Ensure that file and memory protections are | |
320 | * compatible. Note that we only worry about | |
321 | * writability if mapping is shared; in this case, | |
322 | * current and max prot are dictated by the open file. | |
323 | * XXX use the vnode instead? Problem is: what | |
324 | * credentials do we use for determination? What if | |
325 | * proc does a setuid? | |
326 | */ | |
327 | maxprot = VM_PROT_EXECUTE; /* ??? */ | |
91447636 | 328 | if (fp->f_fglob->fg_flag & FREAD) |
1c79356b | 329 | maxprot |= VM_PROT_READ; |
91447636 A |
330 | else if (prot & PROT_READ) { |
331 | (void)vnode_put(vp); | |
332 | error = EACCES; | |
333 | goto bad; | |
334 | } | |
1c79356b A |
335 | /* |
336 | * If we are sharing potential changes (either via | |
337 | * MAP_SHARED or via the implicit sharing of character | |
338 | * device mappings), and we are trying to get write | |
339 | * permission although we opened it without asking | |
340 | * for it, bail out. | |
341 | */ | |
342 | ||
343 | if ((flags & MAP_SHARED) != 0) { | |
b0d623f7 A |
344 | if ((fp->f_fglob->fg_flag & FWRITE) != 0 && |
345 | /* | |
346 | * Do not allow writable mappings of | |
347 | * swap files (see vm_swapfile_pager.c). | |
348 | */ | |
349 | !vnode_isswap(vp)) { | |
91447636 A |
350 | /* |
351 | * check for write access | |
352 | * | |
353 | * Note that we already made this check when granting FWRITE | |
354 | * against the file, so it seems redundant here. | |
355 | */ | |
2d21ac55 | 356 | error = vnode_authorize(vp, NULL, KAUTH_VNODE_CHECKIMMUTABLE, ctx); |
91447636 A |
357 | |
358 | /* if not granted for any reason, but we wanted it, bad */ | |
359 | if ((prot & PROT_WRITE) && (error != 0)) { | |
360 | vnode_put(vp); | |
361 | goto bad; | |
362 | } | |
363 | ||
364 | /* if writable, remember */ | |
365 | if (error == 0) | |
366 | maxprot |= VM_PROT_WRITE; | |
367 | ||
368 | } else if ((prot & PROT_WRITE) != 0) { | |
369 | (void)vnode_put(vp); | |
370 | error = EACCES; | |
371 | goto bad; | |
372 | } | |
1c79356b A |
373 | } else |
374 | maxprot |= VM_PROT_WRITE; | |
375 | ||
376 | handle = (void *)vp; | |
2d21ac55 A |
377 | #if CONFIG_MACF |
378 | error = mac_file_check_mmap(vfs_context_ucred(ctx), | |
379 | fp->f_fglob, prot, flags, &maxprot); | |
380 | if (error) { | |
381 | (void)vnode_put(vp); | |
382 | goto bad; | |
383 | } | |
384 | #endif /* MAC */ | |
1c79356b A |
385 | } |
386 | } | |
387 | ||
91447636 A |
388 | if (user_size == 0) { |
389 | if (!mapanon) | |
390 | (void)vnode_put(vp); | |
391 | error = 0; | |
392 | goto bad; | |
393 | } | |
1c79356b A |
394 | |
395 | /* | |
396 | * We bend a little - round the start and end addresses | |
397 | * to the nearest page boundary. | |
398 | */ | |
91447636 | 399 | user_size = mach_vm_round_page(user_size); |
1c79356b | 400 | |
91447636 A |
401 | if (file_pos & PAGE_MASK_64) { |
402 | if (!mapanon) | |
403 | (void)vnode_put(vp); | |
404 | error = EINVAL; | |
405 | goto bad; | |
406 | } | |
1c79356b A |
407 | |
408 | user_map = current_map(); | |
409 | ||
410 | if ((flags & MAP_FIXED) == 0) { | |
2d21ac55 | 411 | alloc_flags |= VM_FLAGS_ANYWHERE; |
91447636 | 412 | user_addr = mach_vm_round_page(user_addr); |
1c79356b | 413 | } else { |
91447636 A |
414 | if (user_addr != mach_vm_trunc_page(user_addr)) { |
415 | if (!mapanon) | |
416 | (void)vnode_put(vp); | |
417 | error = EINVAL; | |
418 | goto bad; | |
419 | } | |
420 | /* | |
421 | * mmap(MAP_FIXED) will replace any existing mappings in the | |
422 | * specified range, if the new mapping is successful. | |
423 | * If we just deallocate the specified address range here, | |
424 | * another thread might jump in and allocate memory in that | |
425 | * range before we get a chance to establish the new mapping, | |
426 | * and we won't have a chance to restore the old mappings. | |
427 | * So we use VM_FLAGS_OVERWRITE to let Mach VM know that it | |
428 | * has to deallocate the existing mappings and establish the | |
429 | * new ones atomically. | |
430 | */ | |
2d21ac55 | 431 | alloc_flags |= VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE; |
1c79356b A |
432 | } |
433 | ||
2d21ac55 A |
434 | if (flags & MAP_NOCACHE) |
435 | alloc_flags |= VM_FLAGS_NO_CACHE; | |
1c79356b A |
436 | |
437 | /* | |
438 | * Lookup/allocate object. | |
439 | */ | |
1c79356b | 440 | if (handle == NULL) { |
b0d623f7 | 441 | control = NULL; |
1c79356b A |
442 | #ifdef notyet |
443 | /* Hmm .. */ | |
444 | #if defined(VM_PROT_READ_IS_EXEC) | |
445 | if (prot & VM_PROT_READ) | |
446 | prot |= VM_PROT_EXECUTE; | |
1c79356b A |
447 | if (maxprot & VM_PROT_READ) |
448 | maxprot |= VM_PROT_EXECUTE; | |
449 | #endif | |
450 | #endif | |
2d21ac55 A |
451 | |
452 | #if 3777787 | |
453 | if (prot & (VM_PROT_EXECUTE | VM_PROT_WRITE)) | |
454 | prot |= VM_PROT_READ; | |
455 | if (maxprot & (VM_PROT_EXECUTE | VM_PROT_WRITE)) | |
456 | maxprot |= VM_PROT_READ; | |
457 | #endif /* radar 3777787 */ | |
458 | ||
459 | result = vm_map_enter_mem_object(user_map, | |
460 | &user_addr, user_size, | |
461 | 0, alloc_flags, | |
462 | IPC_PORT_NULL, 0, FALSE, | |
463 | prot, maxprot, | |
464 | (flags & MAP_SHARED) ? | |
465 | VM_INHERIT_SHARE : | |
466 | VM_INHERIT_DEFAULT); | |
1c79356b | 467 | } else { |
b0d623f7 A |
468 | if (vnode_isswap(vp)) { |
469 | /* | |
470 | * Map swap files with a special pager | |
471 | * that returns obfuscated contents. | |
472 | */ | |
473 | control = NULL; | |
474 | pager = swapfile_pager_setup(vp); | |
475 | if (pager != MEMORY_OBJECT_NULL) { | |
476 | control = swapfile_pager_control(pager); | |
477 | } | |
478 | } else { | |
479 | control = ubc_getobject(vp, UBC_FLAGS_NONE); | |
480 | } | |
1c79356b | 481 | |
b0d623f7 | 482 | if (control == NULL) { |
91447636 A |
483 | (void)vnode_put(vp); |
484 | error = ENOMEM; | |
485 | goto bad; | |
486 | } | |
1c79356b A |
487 | |
488 | /* | |
489 | * Set credentials: | |
490 | * FIXME: if we're writing the file we need a way to | |
491 | * ensure that someone doesn't replace our R/W creds | |
492 | * with ones that only work for read. | |
493 | */ | |
494 | ||
13fec989 | 495 | ubc_setthreadcred(vp, p, current_thread()); |
1c79356b A |
496 | docow = FALSE; |
497 | if ((flags & (MAP_ANON|MAP_SHARED)) == 0) { | |
498 | docow = TRUE; | |
499 | } | |
500 | ||
501 | #ifdef notyet | |
502 | /* Hmm .. */ | |
503 | #if defined(VM_PROT_READ_IS_EXEC) | |
504 | if (prot & VM_PROT_READ) | |
505 | prot |= VM_PROT_EXECUTE; | |
1c79356b A |
506 | if (maxprot & VM_PROT_READ) |
507 | maxprot |= VM_PROT_EXECUTE; | |
508 | #endif | |
509 | #endif /* notyet */ | |
510 | ||
2d21ac55 A |
511 | #if 3777787 |
512 | if (prot & (VM_PROT_EXECUTE | VM_PROT_WRITE)) | |
513 | prot |= VM_PROT_READ; | |
514 | if (maxprot & (VM_PROT_EXECUTE | VM_PROT_WRITE)) | |
515 | maxprot |= VM_PROT_READ; | |
516 | #endif /* radar 3777787 */ | |
517 | ||
b0d623f7 | 518 | result = vm_map_enter_mem_object_control(user_map, |
2d21ac55 A |
519 | &user_addr, user_size, |
520 | 0, alloc_flags, | |
b0d623f7 | 521 | control, file_pos, |
2d21ac55 A |
522 | docow, prot, maxprot, |
523 | (flags & MAP_SHARED) ? | |
524 | VM_INHERIT_SHARE : | |
525 | VM_INHERIT_DEFAULT); | |
1c79356b A |
526 | } |
527 | ||
b0d623f7 | 528 | if (!mapanon) { |
91447636 | 529 | (void)vnode_put(vp); |
b0d623f7 | 530 | } |
1c79356b | 531 | |
1c79356b A |
532 | switch (result) { |
533 | case KERN_SUCCESS: | |
91447636 A |
534 | *retval = user_addr + pageoff; |
535 | error = 0; | |
536 | break; | |
1c79356b A |
537 | case KERN_INVALID_ADDRESS: |
538 | case KERN_NO_SPACE: | |
91447636 A |
539 | error = ENOMEM; |
540 | break; | |
1c79356b | 541 | case KERN_PROTECTION_FAILURE: |
91447636 A |
542 | error = EACCES; |
543 | break; | |
1c79356b | 544 | default: |
91447636 A |
545 | error = EINVAL; |
546 | break; | |
1c79356b | 547 | } |
91447636 | 548 | bad: |
b0d623f7 A |
549 | if (pager != MEMORY_OBJECT_NULL) { |
550 | /* | |
551 | * Release the reference on the pager. | |
552 | * If the mapping was successful, it now holds | |
553 | * an extra reference. | |
554 | */ | |
555 | memory_object_deallocate(pager); | |
556 | } | |
91447636 A |
557 | if (fpref) |
558 | fp_drop(p, fd, fp, 0); | |
2d21ac55 A |
559 | |
560 | KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO, SYS_mmap) | DBG_FUNC_NONE), fd, (uint32_t)(*retval), (uint32_t)user_size, error, 0); | |
561 | KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO2, SYS_mmap) | DBG_FUNC_NONE), (uint32_t)(*retval >> 32), (uint32_t)(user_size >> 32), | |
562 | (uint32_t)(file_pos >> 32), (uint32_t)file_pos, 0); | |
563 | ||
91447636 | 564 | return(error); |
1c79356b A |
565 | } |
566 | ||
1c79356b | 567 | int |
b0d623f7 | 568 | msync(__unused proc_t p, struct msync_args *uap, int32_t *retval) |
2d21ac55 A |
569 | { |
570 | __pthread_testcancel(1); | |
571 | return(msync_nocancel(p, (struct msync_nocancel_args *)uap, retval)); | |
572 | } | |
573 | ||
574 | int | |
b0d623f7 | 575 | msync_nocancel(__unused proc_t p, struct msync_nocancel_args *uap, __unused int32_t *retval) |
1c79356b | 576 | { |
91447636 A |
577 | mach_vm_offset_t addr; |
578 | mach_vm_size_t size; | |
1c79356b A |
579 | int flags; |
580 | vm_map_t user_map; | |
581 | int rv; | |
582 | vm_sync_t sync_flags=0; | |
583 | ||
91447636 A |
584 | addr = (mach_vm_offset_t) uap->addr; |
585 | size = (mach_vm_size_t)uap->len; | |
1c79356b | 586 | |
2d21ac55 A |
587 | KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO, SYS_msync) | DBG_FUNC_NONE), (uint32_t)(addr >> 32), (uint32_t)(size >> 32), 0, 0, 0); |
588 | ||
91447636 A |
589 | if (addr & PAGE_MASK_64) { |
590 | /* UNIX SPEC: user address is not page-aligned, return EINVAL */ | |
591 | return EINVAL; | |
592 | } | |
1c79356b A |
593 | if (size == 0) { |
594 | /* | |
595 | * We cannot support this properly without maintaining | |
596 | * list all mmaps done. Cannot use vm_map_entry as they could be | |
597 | * split or coalesced by indepenedant actions. So instead of | |
598 | * inaccurate results, lets just return error as invalid size | |
599 | * specified | |
600 | */ | |
55e303ae | 601 | return (EINVAL); /* XXX breaks posix apps */ |
1c79356b A |
602 | } |
603 | ||
91447636 A |
604 | flags = uap->flags; |
605 | /* disallow contradictory flags */ | |
2d21ac55 | 606 | if ((flags & (MS_SYNC|MS_ASYNC)) == (MS_SYNC|MS_ASYNC)) |
91447636 A |
607 | return (EINVAL); |
608 | ||
1c79356b A |
609 | if (flags & MS_KILLPAGES) |
610 | sync_flags |= VM_SYNC_KILLPAGES; | |
611 | if (flags & MS_DEACTIVATE) | |
612 | sync_flags |= VM_SYNC_DEACTIVATE; | |
613 | if (flags & MS_INVALIDATE) | |
614 | sync_flags |= VM_SYNC_INVALIDATE; | |
615 | ||
616 | if ( !(flags & (MS_KILLPAGES | MS_DEACTIVATE))) { | |
617 | if (flags & MS_ASYNC) | |
618 | sync_flags |= VM_SYNC_ASYNCHRONOUS; | |
619 | else | |
620 | sync_flags |= VM_SYNC_SYNCHRONOUS; | |
621 | } | |
91447636 A |
622 | |
623 | sync_flags |= VM_SYNC_CONTIGUOUS; /* complain if holes */ | |
624 | ||
625 | user_map = current_map(); | |
626 | rv = mach_vm_msync(user_map, addr, size, sync_flags); | |
1c79356b A |
627 | |
628 | switch (rv) { | |
629 | case KERN_SUCCESS: | |
630 | break; | |
91447636 A |
631 | case KERN_INVALID_ADDRESS: /* hole in region being sync'ed */ |
632 | return (ENOMEM); | |
1c79356b A |
633 | case KERN_FAILURE: |
634 | return (EIO); | |
635 | default: | |
636 | return (EINVAL); | |
637 | } | |
1c79356b | 638 | return (0); |
1c79356b A |
639 | } |
640 | ||
641 | ||
55e303ae | 642 | int |
b0d623f7 | 643 | munmap(__unused proc_t p, struct munmap_args *uap, __unused int32_t *retval) |
1c79356b | 644 | { |
91447636 A |
645 | mach_vm_offset_t user_addr; |
646 | mach_vm_size_t user_size; | |
1c79356b A |
647 | kern_return_t result; |
648 | ||
91447636 A |
649 | user_addr = (mach_vm_offset_t) uap->addr; |
650 | user_size = (mach_vm_size_t) uap->len; | |
1c79356b | 651 | |
91447636 A |
652 | AUDIT_ARG(addr, user_addr); |
653 | AUDIT_ARG(len, user_size); | |
e5568f75 | 654 | |
91447636 A |
655 | if (user_addr & PAGE_MASK_64) { |
656 | /* UNIX SPEC: user address is not page-aligned, return EINVAL */ | |
657 | return EINVAL; | |
658 | } | |
1c79356b | 659 | |
1c79356b A |
660 | if (user_addr + user_size < user_addr) |
661 | return(EINVAL); | |
662 | ||
91447636 A |
663 | if (user_size == 0) { |
664 | /* UNIX SPEC: size is 0, return EINVAL */ | |
665 | return EINVAL; | |
666 | } | |
1c79356b | 667 | |
91447636 | 668 | result = mach_vm_deallocate(current_map(), user_addr, user_size); |
1c79356b A |
669 | if (result != KERN_SUCCESS) { |
670 | return(EINVAL); | |
671 | } | |
672 | return(0); | |
673 | } | |
674 | ||
1c79356b | 675 | int |
b0d623f7 | 676 | mprotect(__unused proc_t p, struct mprotect_args *uap, __unused int32_t *retval) |
1c79356b A |
677 | { |
678 | register vm_prot_t prot; | |
91447636 A |
679 | mach_vm_offset_t user_addr; |
680 | mach_vm_size_t user_size; | |
1c79356b A |
681 | kern_return_t result; |
682 | vm_map_t user_map; | |
2d21ac55 A |
683 | #if CONFIG_MACF |
684 | int error; | |
685 | #endif | |
1c79356b | 686 | |
e5568f75 A |
687 | AUDIT_ARG(addr, uap->addr); |
688 | AUDIT_ARG(len, uap->len); | |
b0d623f7 | 689 | AUDIT_ARG(value32, uap->prot); |
91447636 A |
690 | |
691 | user_addr = (mach_vm_offset_t) uap->addr; | |
692 | user_size = (mach_vm_size_t) uap->len; | |
1c79356b A |
693 | prot = (vm_prot_t)(uap->prot & VM_PROT_ALL); |
694 | ||
91447636 A |
695 | if (user_addr & PAGE_MASK_64) { |
696 | /* UNIX SPEC: user address is not page-aligned, return EINVAL */ | |
697 | return EINVAL; | |
698 | } | |
699 | ||
1c79356b A |
700 | #ifdef notyet |
701 | /* Hmm .. */ | |
702 | #if defined(VM_PROT_READ_IS_EXEC) | |
703 | if (prot & VM_PROT_READ) | |
704 | prot |= VM_PROT_EXECUTE; | |
705 | #endif | |
706 | #endif /* notyet */ | |
707 | ||
2d21ac55 A |
708 | #if 3936456 |
709 | if (prot & (VM_PROT_EXECUTE | VM_PROT_WRITE)) | |
710 | prot |= VM_PROT_READ; | |
711 | #endif /* 3936456 */ | |
712 | ||
1c79356b A |
713 | user_map = current_map(); |
714 | ||
2d21ac55 A |
715 | #if CONFIG_MACF |
716 | /* | |
717 | * The MAC check for mprotect is of limited use for 2 reasons: | |
718 | * Without mmap revocation, the caller could have asked for the max | |
719 | * protections initially instead of a reduced set, so a mprotect | |
720 | * check would offer no new security. | |
721 | * It is not possible to extract the vnode from the pager object(s) | |
722 | * of the target memory range. | |
723 | * However, the MAC check may be used to prevent a process from, | |
724 | * e.g., making the stack executable. | |
725 | */ | |
726 | error = mac_proc_check_mprotect(p, user_addr, | |
727 | user_size, prot); | |
728 | if (error) | |
729 | return (error); | |
730 | #endif | |
91447636 A |
731 | result = mach_vm_protect(user_map, user_addr, user_size, |
732 | FALSE, prot); | |
1c79356b A |
733 | switch (result) { |
734 | case KERN_SUCCESS: | |
735 | return (0); | |
736 | case KERN_PROTECTION_FAILURE: | |
737 | return (EACCES); | |
91447636 A |
738 | case KERN_INVALID_ADDRESS: |
739 | /* UNIX SPEC: for an invalid address range, return ENOMEM */ | |
740 | return ENOMEM; | |
1c79356b A |
741 | } |
742 | return (EINVAL); | |
743 | } | |
744 | ||
745 | ||
1c79356b | 746 | int |
b0d623f7 | 747 | minherit(__unused proc_t p, struct minherit_args *uap, __unused int32_t *retval) |
1c79356b | 748 | { |
91447636 A |
749 | mach_vm_offset_t addr; |
750 | mach_vm_size_t size; | |
1c79356b A |
751 | register vm_inherit_t inherit; |
752 | vm_map_t user_map; | |
753 | kern_return_t result; | |
754 | ||
e5568f75 A |
755 | AUDIT_ARG(addr, uap->addr); |
756 | AUDIT_ARG(len, uap->len); | |
b0d623f7 | 757 | AUDIT_ARG(value32, uap->inherit); |
1c79356b | 758 | |
91447636 A |
759 | addr = (mach_vm_offset_t)uap->addr; |
760 | size = (mach_vm_size_t)uap->len; | |
761 | inherit = uap->inherit; | |
1c79356b A |
762 | |
763 | user_map = current_map(); | |
91447636 | 764 | result = mach_vm_inherit(user_map, addr, size, |
1c79356b A |
765 | inherit); |
766 | switch (result) { | |
767 | case KERN_SUCCESS: | |
768 | return (0); | |
769 | case KERN_PROTECTION_FAILURE: | |
770 | return (EACCES); | |
771 | } | |
772 | return (EINVAL); | |
773 | } | |
774 | ||
1c79356b | 775 | int |
b0d623f7 | 776 | madvise(__unused proc_t p, struct madvise_args *uap, __unused int32_t *retval) |
1c79356b A |
777 | { |
778 | vm_map_t user_map; | |
91447636 A |
779 | mach_vm_offset_t start; |
780 | mach_vm_size_t size; | |
1c79356b A |
781 | vm_behavior_t new_behavior; |
782 | kern_return_t result; | |
783 | ||
1c79356b A |
784 | /* |
785 | * Since this routine is only advisory, we default to conservative | |
786 | * behavior. | |
787 | */ | |
1c79356b A |
788 | switch (uap->behav) { |
789 | case MADV_RANDOM: | |
790 | new_behavior = VM_BEHAVIOR_RANDOM; | |
9bccf70c | 791 | break; |
1c79356b A |
792 | case MADV_SEQUENTIAL: |
793 | new_behavior = VM_BEHAVIOR_SEQUENTIAL; | |
9bccf70c | 794 | break; |
1c79356b | 795 | case MADV_NORMAL: |
1c79356b | 796 | new_behavior = VM_BEHAVIOR_DEFAULT; |
9bccf70c A |
797 | break; |
798 | case MADV_WILLNEED: | |
799 | new_behavior = VM_BEHAVIOR_WILLNEED; | |
800 | break; | |
801 | case MADV_DONTNEED: | |
802 | new_behavior = VM_BEHAVIOR_DONTNEED; | |
803 | break; | |
b0d623f7 A |
804 | case MADV_FREE: |
805 | new_behavior = VM_BEHAVIOR_FREE; | |
806 | break; | |
807 | case MADV_ZERO_WIRED_PAGES: | |
808 | new_behavior = VM_BEHAVIOR_ZERO_WIRED_PAGES; | |
809 | break; | |
810 | case MADV_FREE_REUSABLE: | |
811 | new_behavior = VM_BEHAVIOR_REUSABLE; | |
812 | break; | |
813 | case MADV_FREE_REUSE: | |
814 | new_behavior = VM_BEHAVIOR_REUSE; | |
815 | break; | |
816 | case MADV_CAN_REUSE: | |
817 | new_behavior = VM_BEHAVIOR_CAN_REUSE; | |
818 | break; | |
9bccf70c A |
819 | default: |
820 | return(EINVAL); | |
1c79356b A |
821 | } |
822 | ||
91447636 A |
823 | start = (mach_vm_offset_t) uap->addr; |
824 | size = (mach_vm_size_t) uap->len; | |
825 | ||
826 | user_map = current_map(); | |
827 | ||
828 | result = mach_vm_behavior_set(user_map, start, size, new_behavior); | |
1c79356b A |
829 | switch (result) { |
830 | case KERN_SUCCESS: | |
831 | return (0); | |
832 | case KERN_INVALID_ADDRESS: | |
91447636 | 833 | return (ENOMEM); |
1c79356b A |
834 | } |
835 | ||
836 | return (EINVAL); | |
837 | } | |
838 | ||
1c79356b | 839 | int |
b0d623f7 | 840 | mincore(__unused proc_t p, struct mincore_args *uap, __unused int32_t *retval) |
1c79356b | 841 | { |
91447636 | 842 | mach_vm_offset_t addr, first_addr, end; |
1c79356b | 843 | vm_map_t map; |
91447636 | 844 | user_addr_t vec; |
1c79356b A |
845 | int error; |
846 | int vecindex, lastvecindex; | |
847 | int mincoreinfo=0; | |
848 | int pqueryinfo; | |
849 | kern_return_t ret; | |
850 | int numref; | |
851 | ||
91447636 A |
852 | char c; |
853 | ||
1c79356b A |
854 | map = current_map(); |
855 | ||
856 | /* | |
857 | * Make sure that the addresses presented are valid for user | |
858 | * mode. | |
859 | */ | |
91447636 A |
860 | first_addr = addr = mach_vm_trunc_page(uap->addr); |
861 | end = addr + mach_vm_round_page(uap->len); | |
1c79356b | 862 | |
1c79356b A |
863 | if (end < addr) |
864 | return (EINVAL); | |
865 | ||
866 | /* | |
867 | * Address of byte vector | |
868 | */ | |
869 | vec = uap->vec; | |
870 | ||
871 | map = current_map(); | |
872 | ||
873 | /* | |
874 | * Do this on a map entry basis so that if the pages are not | |
875 | * in the current processes address space, we can easily look | |
876 | * up the pages elsewhere. | |
877 | */ | |
878 | lastvecindex = -1; | |
91447636 | 879 | for( ; addr < end; addr += PAGE_SIZE ) { |
1c79356b | 880 | pqueryinfo = 0; |
2d21ac55 | 881 | ret = mach_vm_page_query(map, addr, &pqueryinfo, &numref); |
1c79356b A |
882 | if (ret != KERN_SUCCESS) |
883 | pqueryinfo = 0; | |
884 | mincoreinfo = 0; | |
885 | if (pqueryinfo & VM_PAGE_QUERY_PAGE_PRESENT) | |
886 | mincoreinfo |= MINCORE_INCORE; | |
887 | if (pqueryinfo & VM_PAGE_QUERY_PAGE_REF) | |
888 | mincoreinfo |= MINCORE_REFERENCED; | |
889 | if (pqueryinfo & VM_PAGE_QUERY_PAGE_DIRTY) | |
890 | mincoreinfo |= MINCORE_MODIFIED; | |
891 | ||
892 | ||
893 | /* | |
894 | * calculate index into user supplied byte vector | |
895 | */ | |
896 | vecindex = (addr - first_addr)>> PAGE_SHIFT; | |
897 | ||
898 | /* | |
899 | * If we have skipped map entries, we need to make sure that | |
900 | * the byte vector is zeroed for those skipped entries. | |
901 | */ | |
902 | while((lastvecindex + 1) < vecindex) { | |
91447636 A |
903 | c = 0; |
904 | error = copyout(&c, vec + lastvecindex, 1); | |
1c79356b A |
905 | if (error) { |
906 | return (EFAULT); | |
907 | } | |
908 | ++lastvecindex; | |
909 | } | |
910 | ||
911 | /* | |
912 | * Pass the page information to the user | |
913 | */ | |
91447636 A |
914 | c = (char)mincoreinfo; |
915 | error = copyout(&c, vec + vecindex, 1); | |
1c79356b A |
916 | if (error) { |
917 | return (EFAULT); | |
918 | } | |
919 | lastvecindex = vecindex; | |
920 | } | |
921 | ||
922 | ||
923 | /* | |
924 | * Zero the last entries in the byte vector. | |
925 | */ | |
926 | vecindex = (end - first_addr) >> PAGE_SHIFT; | |
927 | while((lastvecindex + 1) < vecindex) { | |
91447636 A |
928 | c = 0; |
929 | error = copyout(&c, vec + lastvecindex, 1); | |
1c79356b A |
930 | if (error) { |
931 | return (EFAULT); | |
932 | } | |
933 | ++lastvecindex; | |
934 | } | |
935 | ||
936 | return (0); | |
937 | } | |
938 | ||
1c79356b | 939 | int |
b0d623f7 | 940 | mlock(__unused proc_t p, struct mlock_args *uap, __unused int32_t *retvalval) |
1c79356b A |
941 | { |
942 | vm_map_t user_map; | |
91447636 A |
943 | vm_map_offset_t addr; |
944 | vm_map_size_t size, pageoff; | |
1c79356b A |
945 | kern_return_t result; |
946 | ||
e5568f75 A |
947 | AUDIT_ARG(addr, uap->addr); |
948 | AUDIT_ARG(len, uap->len); | |
1c79356b | 949 | |
91447636 A |
950 | addr = (vm_map_offset_t) uap->addr; |
951 | size = (vm_map_size_t)uap->len; | |
1c79356b A |
952 | |
953 | /* disable wrap around */ | |
954 | if (addr + size < addr) | |
955 | return (EINVAL); | |
91447636 A |
956 | |
957 | if (size == 0) | |
958 | return (0); | |
959 | ||
960 | pageoff = (addr & PAGE_MASK); | |
961 | addr -= pageoff; | |
962 | size = vm_map_round_page(size+pageoff); | |
1c79356b A |
963 | user_map = current_map(); |
964 | ||
91447636 A |
965 | /* have to call vm_map_wire directly to pass "I don't know" protections */ |
966 | result = vm_map_wire(user_map, addr, addr+size, VM_PROT_NONE, TRUE); | |
2d21ac55 A |
967 | |
968 | if (result == KERN_RESOURCE_SHORTAGE) | |
969 | return EAGAIN; | |
970 | else if (result != KERN_SUCCESS) | |
971 | return ENOMEM; | |
972 | ||
973 | return 0; /* KERN_SUCCESS */ | |
1c79356b A |
974 | } |
975 | ||
1c79356b | 976 | int |
b0d623f7 | 977 | munlock(__unused proc_t p, struct munlock_args *uap, __unused int32_t *retval) |
1c79356b | 978 | { |
91447636 A |
979 | mach_vm_offset_t addr; |
980 | mach_vm_size_t size; | |
1c79356b A |
981 | vm_map_t user_map; |
982 | kern_return_t result; | |
983 | ||
e5568f75 | 984 | AUDIT_ARG(addr, uap->addr); |
91447636 | 985 | AUDIT_ARG(addr, uap->len); |
1c79356b | 986 | |
91447636 A |
987 | addr = (mach_vm_offset_t) uap->addr; |
988 | size = (mach_vm_size_t)uap->len; | |
1c79356b A |
989 | user_map = current_map(); |
990 | ||
91447636 A |
991 | /* JMM - need to remove all wirings by spec - this just removes one */ |
992 | result = mach_vm_wire(host_priv_self(), user_map, addr, size, VM_PROT_NONE); | |
1c79356b A |
993 | return (result == KERN_SUCCESS ? 0 : ENOMEM); |
994 | } | |
995 | ||
996 | ||
1c79356b | 997 | int |
b0d623f7 | 998 | mlockall(__unused proc_t p, __unused struct mlockall_args *uap, __unused int32_t *retval) |
1c79356b A |
999 | { |
1000 | return (ENOSYS); | |
1001 | } | |
1002 | ||
1c79356b | 1003 | int |
b0d623f7 | 1004 | munlockall(__unused proc_t p, __unused struct munlockall_args *uap, __unused int32_t *retval) |
1c79356b A |
1005 | { |
1006 | return(ENOSYS); | |
1007 | } | |
1008 | ||
55e303ae A |
1009 | /* USV: No! need to obsolete map_fd()! mmap() already supports 64 bits */ |
1010 | kern_return_t | |
91447636 | 1011 | map_fd(struct map_fd_args *args) |
1c79356b | 1012 | { |
91447636 A |
1013 | int fd = args->fd; |
1014 | vm_offset_t offset = args->offset; | |
1015 | vm_offset_t *va = args->va; | |
1016 | boolean_t findspace = args->findspace; | |
1017 | vm_size_t size = args->size; | |
1c79356b | 1018 | kern_return_t ret; |
1c79356b | 1019 | |
e5568f75 | 1020 | AUDIT_MACH_SYSCALL_ENTER(AUE_MAPFD); |
b0d623f7 | 1021 | AUDIT_ARG(addr, CAST_DOWN(user_addr_t, args->va)); |
e5568f75 A |
1022 | AUDIT_ARG(fd, fd); |
1023 | ||
91447636 | 1024 | ret = map_fd_funneled( fd, (vm_object_offset_t)offset, va, findspace, size); |
1c79356b | 1025 | |
e5568f75 | 1026 | AUDIT_MACH_SYSCALL_EXIT(ret); |
1c79356b A |
1027 | return ret; |
1028 | } | |
1029 | ||
55e303ae A |
1030 | kern_return_t |
1031 | map_fd_funneled( | |
1c79356b A |
1032 | int fd, |
1033 | vm_object_offset_t offset, | |
1034 | vm_offset_t *va, | |
1035 | boolean_t findspace, | |
1036 | vm_size_t size) | |
1037 | { | |
1038 | kern_return_t result; | |
91447636 | 1039 | struct fileproc *fp; |
1c79356b A |
1040 | struct vnode *vp; |
1041 | void * pager; | |
1042 | vm_offset_t map_addr=0; | |
1043 | vm_size_t map_size; | |
1c79356b A |
1044 | int err=0; |
1045 | vm_map_t my_map; | |
2d21ac55 | 1046 | proc_t p = current_proc(); |
91447636 | 1047 | struct vnode_attr vattr; |
1c79356b A |
1048 | |
1049 | /* | |
1050 | * Find the inode; verify that it's a regular file. | |
1051 | */ | |
1052 | ||
91447636 | 1053 | err = fp_lookup(p, fd, &fp, 0); |
1c79356b A |
1054 | if (err) |
1055 | return(err); | |
1056 | ||
91447636 A |
1057 | if (fp->f_fglob->fg_type != DTYPE_VNODE){ |
1058 | err = KERN_INVALID_ARGUMENT; | |
1059 | goto bad; | |
1060 | } | |
b4c24cb9 | 1061 | |
91447636 A |
1062 | if (!(fp->f_fglob->fg_flag & FREAD)) { |
1063 | err = KERN_PROTECTION_FAILURE; | |
1064 | goto bad; | |
1065 | } | |
b4c24cb9 | 1066 | |
91447636 A |
1067 | vp = (struct vnode *)fp->f_fglob->fg_data; |
1068 | err = vnode_getwithref(vp); | |
1069 | if(err != 0) | |
1070 | goto bad; | |
1c79356b | 1071 | |
91447636 A |
1072 | if (vp->v_type != VREG) { |
1073 | (void)vnode_put(vp); | |
1074 | err = KERN_INVALID_ARGUMENT; | |
1075 | goto bad; | |
1076 | } | |
1c79356b | 1077 | |
e5568f75 A |
1078 | AUDIT_ARG(vnpath, vp, ARG_VNODE1); |
1079 | ||
2d21ac55 A |
1080 | /* |
1081 | * POSIX: mmap needs to update access time for mapped files | |
91447636 | 1082 | */ |
2d21ac55 A |
1083 | if ((vnode_vfsvisflags(vp) & MNT_NOATIME) == 0) { |
1084 | VATTR_INIT(&vattr); | |
1085 | nanotime(&vattr.va_access_time); | |
1086 | VATTR_SET_ACTIVE(&vattr, va_access_time); | |
1087 | vnode_setattr(vp, &vattr, vfs_context_current()); | |
1088 | } | |
1089 | ||
1c79356b | 1090 | if (offset & PAGE_MASK_64) { |
9bccf70c | 1091 | printf("map_fd: file offset not page aligned(%d : %s)\n",p->p_pid, p->p_comm); |
91447636 A |
1092 | (void)vnode_put(vp); |
1093 | err = KERN_INVALID_ARGUMENT; | |
1094 | goto bad; | |
1c79356b | 1095 | } |
91447636 | 1096 | map_size = round_page(size); |
1c79356b A |
1097 | |
1098 | /* | |
1099 | * Allow user to map in a zero length file. | |
1100 | */ | |
91447636 A |
1101 | if (size == 0) { |
1102 | (void)vnode_put(vp); | |
1103 | err = KERN_SUCCESS; | |
1104 | goto bad; | |
1105 | } | |
1c79356b A |
1106 | /* |
1107 | * Map in the file. | |
1108 | */ | |
2d21ac55 | 1109 | pager = (void *)ubc_getpager(vp); |
91447636 A |
1110 | if (pager == NULL) { |
1111 | (void)vnode_put(vp); | |
1112 | err = KERN_FAILURE; | |
1113 | goto bad; | |
1114 | } | |
1c79356b A |
1115 | |
1116 | ||
1117 | my_map = current_map(); | |
1118 | ||
1119 | result = vm_map_64( | |
1120 | my_map, | |
91447636 A |
1121 | &map_addr, map_size, (vm_offset_t)0, |
1122 | VM_FLAGS_ANYWHERE, pager, offset, TRUE, | |
1c79356b A |
1123 | VM_PROT_DEFAULT, VM_PROT_ALL, |
1124 | VM_INHERIT_DEFAULT); | |
91447636 A |
1125 | if (result != KERN_SUCCESS) { |
1126 | (void)vnode_put(vp); | |
1127 | err = result; | |
1128 | goto bad; | |
1129 | } | |
1c79356b A |
1130 | |
1131 | ||
1132 | if (!findspace) { | |
b0d623f7 A |
1133 | //K64todo fix for 64bit user? |
1134 | uint32_t dst_addr; | |
1c79356b A |
1135 | vm_map_copy_t tmp; |
1136 | ||
91447636 | 1137 | if (copyin(CAST_USER_ADDR_T(va), &dst_addr, sizeof (dst_addr)) || |
b0d623f7 | 1138 | trunc_page(dst_addr) != dst_addr) { |
1c79356b A |
1139 | (void) vm_map_remove( |
1140 | my_map, | |
1141 | map_addr, map_addr + map_size, | |
1142 | VM_MAP_NO_FLAGS); | |
91447636 A |
1143 | (void)vnode_put(vp); |
1144 | err = KERN_INVALID_ADDRESS; | |
1145 | goto bad; | |
1c79356b A |
1146 | } |
1147 | ||
91447636 A |
1148 | result = vm_map_copyin(my_map, (vm_map_address_t)map_addr, |
1149 | (vm_map_size_t)map_size, TRUE, &tmp); | |
1c79356b A |
1150 | if (result != KERN_SUCCESS) { |
1151 | ||
91447636 A |
1152 | (void) vm_map_remove(my_map, vm_map_trunc_page(map_addr), |
1153 | vm_map_round_page(map_addr + map_size), | |
1c79356b | 1154 | VM_MAP_NO_FLAGS); |
91447636 A |
1155 | (void)vnode_put(vp); |
1156 | err = result; | |
1157 | goto bad; | |
1c79356b A |
1158 | } |
1159 | ||
91447636 A |
1160 | result = vm_map_copy_overwrite(my_map, |
1161 | (vm_map_address_t)dst_addr, tmp, FALSE); | |
1c79356b A |
1162 | if (result != KERN_SUCCESS) { |
1163 | vm_map_copy_discard(tmp); | |
91447636 A |
1164 | (void)vnode_put(vp); |
1165 | err = result; | |
1166 | goto bad; | |
1c79356b A |
1167 | } |
1168 | } else { | |
b0d623f7 A |
1169 | // K64todo bug compatible now, should fix for 64bit user |
1170 | uint32_t user_map_addr = CAST_DOWN_EXPLICIT(uint32_t, map_addr); | |
1171 | if (copyout(&user_map_addr, CAST_USER_ADDR_T(va), sizeof (user_map_addr))) { | |
91447636 A |
1172 | (void) vm_map_remove(my_map, vm_map_trunc_page(map_addr), |
1173 | vm_map_round_page(map_addr + map_size), | |
1c79356b | 1174 | VM_MAP_NO_FLAGS); |
91447636 A |
1175 | (void)vnode_put(vp); |
1176 | err = KERN_INVALID_ADDRESS; | |
1177 | goto bad; | |
1c79356b A |
1178 | } |
1179 | } | |
1180 | ||
13fec989 | 1181 | ubc_setthreadcred(vp, current_proc(), current_thread()); |
91447636 A |
1182 | (void)vnode_put(vp); |
1183 | err = 0; | |
1184 | bad: | |
1185 | fp_drop(p, fd, fp, 0); | |
1186 | return (err); | |
1c79356b | 1187 | } |
91447636 | 1188 |