2 * Copyright (c) 2007 Apple Inc. All Rights Reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1988 University of Utah.
30 * Copyright (c) 1991, 1993
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * the Systems Programming Group of the University of Utah Computer
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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.
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
65 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
67 * @(#)vm_mmap.c 8.10 (Berkeley) 2/19/95
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,
77 * Mapped file (mmap) interface to VM
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/filedesc.h>
83 #include <sys/proc_internal.h>
84 #include <sys/kauth.h>
85 #include <sys/resourcevar.h>
86 #include <sys/vnode_internal.h>
89 #include <sys/file_internal.h>
90 #include <sys/vadvise.h>
91 #include <sys/trace.h>
96 #include <sys/ubc_internal.h>
97 #include <sys/sysproto.h>
99 #include <sys/cprotect.h>
102 #include <sys/syscall.h>
103 #include <sys/kdebug.h>
105 #include <security/audit/audit.h>
106 #include <bsm/audit_kevents.h>
108 #include <mach/mach_types.h>
109 #include <mach/mach_traps.h>
110 #include <mach/vm_sync.h>
111 #include <mach/vm_behavior.h>
112 #include <mach/vm_inherit.h>
113 #include <mach/vm_statistics.h>
114 #include <mach/mach_vm.h>
115 #include <mach/vm_map.h>
116 #include <mach/host_priv.h>
118 #include <machine/machine_routines.h>
120 #include <kern/cpu_number.h>
121 #include <kern/host.h>
122 #include <kern/task.h>
124 #include <vm/vm_map.h>
125 #include <vm/vm_kern.h>
126 #include <vm/vm_pager.h>
127 #include <vm/vm_protos.h>
129 /* XXX the following function should probably be static */
130 kern_return_t
map_fd_funneled(int, vm_object_offset_t
, vm_offset_t
*,
131 boolean_t
, vm_size_t
);
134 * XXX Internally, we use VM_PROT_* somewhat interchangeably, but the correct
135 * XXX usage is PROT_* from an interface perspective. Thus the values of
136 * XXX VM_PROT_* and PROT_* need to correspond.
139 mmap(proc_t p
, struct mmap_args
*uap
, user_addr_t
*retval
)
142 * Map in special device (must be SHARED) or file
145 register struct vnode
*vp
;
150 kern_return_t result
;
151 vm_map_offset_t user_addr
;
152 vm_map_size_t user_size
;
153 vm_object_offset_t pageoff
;
154 vm_object_offset_t file_pos
;
159 memory_object_t pager
= MEMORY_OBJECT_NULL
;
160 memory_object_control_t control
;
167 user_addr
= (vm_map_offset_t
)uap
->addr
;
168 user_size
= (vm_map_size_t
) uap
->len
;
170 AUDIT_ARG(addr
, user_addr
);
171 AUDIT_ARG(len
, user_size
);
172 AUDIT_ARG(fd
, uap
->fd
);
174 prot
= (uap
->prot
& VM_PROT_ALL
);
177 * Since the hardware currently does not support writing without
178 * read-before-write, or execution-without-read, if the request is
179 * for write or execute access, we must imply read access as well;
180 * otherwise programs expecting this to work will fail to operate.
182 if (prot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
183 prot
|= VM_PROT_READ
;
184 #endif /* radar 3777787 */
190 * The vm code does not have prototypes & compiler doesn't do the'
191 * the right thing when you cast 64bit value and pass it in function
192 * call. So here it is.
194 file_pos
= (vm_object_offset_t
)uap
->pos
;
197 /* make sure mapping fits into numeric range etc */
198 if (file_pos
+ user_size
> (vm_object_offset_t
)-PAGE_SIZE_64
)
202 * Align the file position to a page boundary,
203 * and save its page offset component.
205 pageoff
= (file_pos
& PAGE_MASK
);
206 file_pos
-= (vm_object_offset_t
)pageoff
;
209 /* Adjust size for rounding (on both ends). */
210 user_size
+= pageoff
; /* low end... */
211 user_size
= mach_vm_round_page(user_size
); /* hi end */
213 if ((flags
& MAP_JIT
) && ((flags
& MAP_FIXED
) || (flags
& MAP_SHARED
) || !(flags
& MAP_ANON
))){
217 * Check for illegal addresses. Watch out for address wrap... Note
218 * that VM_*_ADDRESS are not constants due to casts (argh).
220 if (flags
& MAP_FIXED
) {
222 * The specified address must have the same remainder
223 * as the file offset taken modulo PAGE_SIZE, so it
224 * should be aligned after adjustment by pageoff.
226 user_addr
-= pageoff
;
227 if (user_addr
& PAGE_MASK
)
231 /* DO not have apis to get this info, need to wait till then*/
233 * XXX for non-fixed mappings where no hint is provided or
234 * the hint would fall in the potential heap space,
235 * place it after the end of the largest possible heap.
237 * There should really be a pmap call to determine a reasonable
240 else if (addr
< mach_vm_round_page(p
->p_vmspace
->vm_daddr
+ MAXDSIZ
))
241 addr
= mach_vm_round_page(p
->p_vmspace
->vm_daddr
+ MAXDSIZ
);
247 if (flags
& MAP_ANON
) {
249 maxprot
= VM_PROT_ALL
;
254 error
= mac_proc_check_map_anon(p
, user_addr
, user_size
, prot
, flags
, &maxprot
);
261 * Mapping blank space is trivial. Use positive fds as the alias
262 * value for memory tracking.
266 * Use "fd" to pass (some) Mach VM allocation flags,
267 * (see the VM_FLAGS_* definitions).
269 alloc_flags
= fd
& (VM_FLAGS_ALIAS_MASK
| VM_FLAGS_SUPERPAGE_MASK
|
271 if (alloc_flags
!= fd
) {
272 /* reject if there are any extra flags */
281 struct vnode_attr va
;
282 vfs_context_t ctx
= vfs_context_current();
288 * Mapping file, get fp for validation. Obtain vnode and make
289 * sure it is of appropriate type.
291 err
= fp_lookup(p
, fd
, &fp
, 0);
295 if(fp
->f_fglob
->fg_type
== DTYPE_PSXSHM
) {
296 uap
->addr
= (user_addr_t
)user_addr
;
297 uap
->len
= (user_size_t
)user_size
;
301 error
= pshm_mmap(p
, uap
, retval
, fp
, (off_t
)pageoff
);
305 if (fp
->f_fglob
->fg_type
!= DTYPE_VNODE
) {
309 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
310 error
= vnode_getwithref(vp
);
314 if (vp
->v_type
!= VREG
&& vp
->v_type
!= VCHR
) {
320 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
323 * POSIX: mmap needs to update access time for mapped files
325 if ((vnode_vfsvisflags(vp
) & MNT_NOATIME
) == 0) {
327 nanotime(&va
.va_access_time
);
328 VATTR_SET_ACTIVE(&va
, va_access_time
);
329 vnode_setattr(vp
, &va
, ctx
);
333 * XXX hack to handle use of /dev/zero to map anon memory (ala
336 if (vp
->v_type
== VCHR
|| vp
->v_type
== VSTR
) {
342 * Ensure that file and memory protections are
343 * compatible. Note that we only worry about
344 * writability if mapping is shared; in this case,
345 * current and max prot are dictated by the open file.
346 * XXX use the vnode instead? Problem is: what
347 * credentials do we use for determination? What if
348 * proc does a setuid?
350 maxprot
= VM_PROT_EXECUTE
; /* ??? */
351 if (fp
->f_fglob
->fg_flag
& FREAD
)
352 maxprot
|= VM_PROT_READ
;
353 else if (prot
& PROT_READ
) {
359 * If we are sharing potential changes (either via
360 * MAP_SHARED or via the implicit sharing of character
361 * device mappings), and we are trying to get write
362 * permission although we opened it without asking
366 if ((flags
& MAP_SHARED
) != 0) {
367 if ((fp
->f_fglob
->fg_flag
& FWRITE
) != 0 &&
369 * Do not allow writable mappings of
370 * swap files (see vm_swapfile_pager.c).
374 * check for write access
376 * Note that we already made this check when granting FWRITE
377 * against the file, so it seems redundant here.
379 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_CHECKIMMUTABLE
, ctx
);
381 /* if not granted for any reason, but we wanted it, bad */
382 if ((prot
& PROT_WRITE
) && (error
!= 0)) {
387 /* if writable, remember */
389 maxprot
|= VM_PROT_WRITE
;
391 } else if ((prot
& PROT_WRITE
) != 0) {
397 maxprot
|= VM_PROT_WRITE
;
401 error
= mac_file_check_mmap(vfs_context_ucred(ctx
),
402 fp
->f_fglob
, prot
, flags
, &maxprot
);
411 error
= cp_handle_vnop(vp
, CP_READ_ACCESS
| CP_WRITE_ACCESS
, 0);
413 (void) vnode_put(vp
);
417 #endif /* CONFIG_PROTECT */
423 if (user_size
== 0) {
431 * We bend a little - round the start and end addresses
432 * to the nearest page boundary.
434 user_size
= mach_vm_round_page(user_size
);
436 if (file_pos
& PAGE_MASK_64
) {
443 user_map
= current_map();
445 if ((flags
& MAP_FIXED
) == 0) {
446 alloc_flags
|= VM_FLAGS_ANYWHERE
;
447 user_addr
= mach_vm_round_page(user_addr
);
449 if (user_addr
!= mach_vm_trunc_page(user_addr
)) {
456 * mmap(MAP_FIXED) will replace any existing mappings in the
457 * specified range, if the new mapping is successful.
458 * If we just deallocate the specified address range here,
459 * another thread might jump in and allocate memory in that
460 * range before we get a chance to establish the new mapping,
461 * and we won't have a chance to restore the old mappings.
462 * So we use VM_FLAGS_OVERWRITE to let Mach VM know that it
463 * has to deallocate the existing mappings and establish the
464 * new ones atomically.
466 alloc_flags
|= VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
;
469 if (flags
& MAP_NOCACHE
)
470 alloc_flags
|= VM_FLAGS_NO_CACHE
;
472 if (flags
& MAP_JIT
){
473 alloc_flags
|= VM_FLAGS_MAP_JIT
;
476 * Lookup/allocate object.
478 if (handle
== NULL
) {
482 #if defined(VM_PROT_READ_IS_EXEC)
483 if (prot
& VM_PROT_READ
)
484 prot
|= VM_PROT_EXECUTE
;
485 if (maxprot
& VM_PROT_READ
)
486 maxprot
|= VM_PROT_EXECUTE
;
491 if (prot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
492 prot
|= VM_PROT_READ
;
493 if (maxprot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
494 maxprot
|= VM_PROT_READ
;
495 #endif /* radar 3777787 */
497 result
= vm_map_enter_mem_object(user_map
,
498 &user_addr
, user_size
,
500 IPC_PORT_NULL
, 0, FALSE
,
502 (flags
& MAP_SHARED
) ?
506 /* If a non-binding address was specified for this anonymous
507 * mapping, retry the mapping with a zero base
508 * in the event the mapping operation failed due to
509 * lack of space between the address and the map's maximum.
511 if ((result
== KERN_NO_SPACE
) && ((flags
& MAP_FIXED
) == 0) && user_addr
&& (num_retries
++ == 0)) {
512 user_addr
= PAGE_SIZE
;
516 if (vnode_isswap(vp
)) {
518 * Map swap files with a special pager
519 * that returns obfuscated contents.
522 pager
= swapfile_pager_setup(vp
);
523 if (pager
!= MEMORY_OBJECT_NULL
) {
524 control
= swapfile_pager_control(pager
);
527 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
530 if (control
== NULL
) {
538 * FIXME: if we're writing the file we need a way to
539 * ensure that someone doesn't replace our R/W creds
540 * with ones that only work for read.
543 ubc_setthreadcred(vp
, p
, current_thread());
545 if ((flags
& (MAP_ANON
|MAP_SHARED
)) == 0) {
551 #if defined(VM_PROT_READ_IS_EXEC)
552 if (prot
& VM_PROT_READ
)
553 prot
|= VM_PROT_EXECUTE
;
554 if (maxprot
& VM_PROT_READ
)
555 maxprot
|= VM_PROT_EXECUTE
;
560 if (prot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
561 prot
|= VM_PROT_READ
;
562 if (maxprot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
563 maxprot
|= VM_PROT_READ
;
564 #endif /* radar 3777787 */
566 result
= vm_map_enter_mem_object_control(user_map
,
567 &user_addr
, user_size
,
570 docow
, prot
, maxprot
,
571 (flags
& MAP_SHARED
) ?
575 /* If a non-binding address was specified for this file backed
576 * mapping, retry the mapping with a zero base
577 * in the event the mapping operation failed due to
578 * lack of space between the address and the map's maximum.
580 if ((result
== KERN_NO_SPACE
) && ((flags
& MAP_FIXED
) == 0) && user_addr
&& (num_retries
++ == 0)) {
581 user_addr
= PAGE_SIZE
;
592 *retval
= user_addr
+ pageoff
;
595 case KERN_INVALID_ADDRESS
:
599 case KERN_PROTECTION_FAILURE
:
607 if (pager
!= MEMORY_OBJECT_NULL
) {
609 * Release the reference on the pager.
610 * If the mapping was successful, it now holds
611 * an extra reference.
613 memory_object_deallocate(pager
);
616 fp_drop(p
, fd
, fp
, 0);
618 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);
619 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO2
, SYS_mmap
) | DBG_FUNC_NONE
), (uint32_t)(*retval
>> 32), (uint32_t)(user_size
>> 32),
620 (uint32_t)(file_pos
>> 32), (uint32_t)file_pos
, 0);
625 msync(__unused proc_t p
, struct msync_args
*uap
, int32_t *retval
)
627 __pthread_testcancel(1);
628 return(msync_nocancel(p
, (struct msync_nocancel_args
*)uap
, retval
));
632 msync_nocancel(__unused proc_t p
, struct msync_nocancel_args
*uap
, __unused
int32_t *retval
)
634 mach_vm_offset_t addr
;
639 vm_sync_t sync_flags
=0;
641 addr
= (mach_vm_offset_t
) uap
->addr
;
642 size
= (mach_vm_size_t
)uap
->len
;
643 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);
644 if (addr
& PAGE_MASK_64
) {
645 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
650 * We cannot support this properly without maintaining
651 * list all mmaps done. Cannot use vm_map_entry as they could be
652 * split or coalesced by indepenedant actions. So instead of
653 * inaccurate results, lets just return error as invalid size
656 return (EINVAL
); /* XXX breaks posix apps */
660 /* disallow contradictory flags */
661 if ((flags
& (MS_SYNC
|MS_ASYNC
)) == (MS_SYNC
|MS_ASYNC
))
664 if (flags
& MS_KILLPAGES
)
665 sync_flags
|= VM_SYNC_KILLPAGES
;
666 if (flags
& MS_DEACTIVATE
)
667 sync_flags
|= VM_SYNC_DEACTIVATE
;
668 if (flags
& MS_INVALIDATE
)
669 sync_flags
|= VM_SYNC_INVALIDATE
;
671 if ( !(flags
& (MS_KILLPAGES
| MS_DEACTIVATE
))) {
672 if (flags
& MS_ASYNC
)
673 sync_flags
|= VM_SYNC_ASYNCHRONOUS
;
675 sync_flags
|= VM_SYNC_SYNCHRONOUS
;
678 sync_flags
|= VM_SYNC_CONTIGUOUS
; /* complain if holes */
680 user_map
= current_map();
681 rv
= mach_vm_msync(user_map
, addr
, size
, sync_flags
);
686 case KERN_INVALID_ADDRESS
: /* hole in region being sync'ed */
698 munmap(__unused proc_t p
, struct munmap_args
*uap
, __unused
int32_t *retval
)
700 mach_vm_offset_t user_addr
;
701 mach_vm_size_t user_size
;
702 kern_return_t result
;
704 user_addr
= (mach_vm_offset_t
) uap
->addr
;
705 user_size
= (mach_vm_size_t
) uap
->len
;
707 AUDIT_ARG(addr
, user_addr
);
708 AUDIT_ARG(len
, user_size
);
710 if (user_addr
& PAGE_MASK_64
) {
711 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
715 if (user_addr
+ user_size
< user_addr
)
718 if (user_size
== 0) {
719 /* UNIX SPEC: size is 0, return EINVAL */
723 result
= mach_vm_deallocate(current_map(), user_addr
, user_size
);
724 if (result
!= KERN_SUCCESS
) {
731 mprotect(__unused proc_t p
, struct mprotect_args
*uap
, __unused
int32_t *retval
)
733 register vm_prot_t prot
;
734 mach_vm_offset_t user_addr
;
735 mach_vm_size_t user_size
;
736 kern_return_t result
;
742 AUDIT_ARG(addr
, uap
->addr
);
743 AUDIT_ARG(len
, uap
->len
);
744 AUDIT_ARG(value32
, uap
->prot
);
746 user_addr
= (mach_vm_offset_t
) uap
->addr
;
747 user_size
= (mach_vm_size_t
) uap
->len
;
748 prot
= (vm_prot_t
)(uap
->prot
& (VM_PROT_ALL
| VM_PROT_TRUSTED
));
750 if (user_addr
& PAGE_MASK_64
) {
751 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
757 #if defined(VM_PROT_READ_IS_EXEC)
758 if (prot
& VM_PROT_READ
)
759 prot
|= VM_PROT_EXECUTE
;
764 if (prot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
765 prot
|= VM_PROT_READ
;
768 user_map
= current_map();
772 * The MAC check for mprotect is of limited use for 2 reasons:
773 * Without mmap revocation, the caller could have asked for the max
774 * protections initially instead of a reduced set, so a mprotect
775 * check would offer no new security.
776 * It is not possible to extract the vnode from the pager object(s)
777 * of the target memory range.
778 * However, the MAC check may be used to prevent a process from,
779 * e.g., making the stack executable.
781 error
= mac_proc_check_mprotect(p
, user_addr
,
787 if(prot
& VM_PROT_TRUSTED
) {
788 #if CONFIG_DYNAMIC_CODE_SIGNING
789 /* CODE SIGNING ENFORCEMENT - JIT support */
790 /* The special protection value VM_PROT_TRUSTED requests that we treat
791 * this page as if it had a valid code signature.
792 * If this is enabled, there MUST be a MAC policy implementing the
793 * mac_proc_check_mprotect() hook above. Otherwise, Codesigning will be
794 * compromised because the check would always succeed and thusly any
795 * process could sign dynamically. */
796 result
= vm_map_sign(user_map
,
797 vm_map_trunc_page(user_addr
),
798 vm_map_round_page(user_addr
+user_size
));
802 case KERN_INVALID_ADDRESS
:
803 /* UNIX SPEC: for an invalid address range, return ENOMEM */
812 prot
&= ~VM_PROT_TRUSTED
;
814 result
= mach_vm_protect(user_map
, user_addr
, user_size
,
819 case KERN_PROTECTION_FAILURE
:
821 case KERN_INVALID_ADDRESS
:
822 /* UNIX SPEC: for an invalid address range, return ENOMEM */
830 minherit(__unused proc_t p
, struct minherit_args
*uap
, __unused
int32_t *retval
)
832 mach_vm_offset_t addr
;
834 register vm_inherit_t inherit
;
836 kern_return_t result
;
838 AUDIT_ARG(addr
, uap
->addr
);
839 AUDIT_ARG(len
, uap
->len
);
840 AUDIT_ARG(value32
, uap
->inherit
);
842 addr
= (mach_vm_offset_t
)uap
->addr
;
843 size
= (mach_vm_size_t
)uap
->len
;
844 inherit
= uap
->inherit
;
846 user_map
= current_map();
847 result
= mach_vm_inherit(user_map
, addr
, size
,
852 case KERN_PROTECTION_FAILURE
:
859 madvise(__unused proc_t p
, struct madvise_args
*uap
, __unused
int32_t *retval
)
862 mach_vm_offset_t start
;
864 vm_behavior_t new_behavior
;
865 kern_return_t result
;
868 * Since this routine is only advisory, we default to conservative
871 switch (uap
->behav
) {
873 new_behavior
= VM_BEHAVIOR_RANDOM
;
875 case MADV_SEQUENTIAL
:
876 new_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
879 new_behavior
= VM_BEHAVIOR_DEFAULT
;
882 new_behavior
= VM_BEHAVIOR_WILLNEED
;
885 new_behavior
= VM_BEHAVIOR_DONTNEED
;
888 new_behavior
= VM_BEHAVIOR_FREE
;
890 case MADV_ZERO_WIRED_PAGES
:
891 new_behavior
= VM_BEHAVIOR_ZERO_WIRED_PAGES
;
893 case MADV_FREE_REUSABLE
:
894 new_behavior
= VM_BEHAVIOR_REUSABLE
;
896 case MADV_FREE_REUSE
:
897 new_behavior
= VM_BEHAVIOR_REUSE
;
900 new_behavior
= VM_BEHAVIOR_CAN_REUSE
;
906 start
= (mach_vm_offset_t
) uap
->addr
;
907 size
= (mach_vm_size_t
) uap
->len
;
909 user_map
= current_map();
911 result
= mach_vm_behavior_set(user_map
, start
, size
, new_behavior
);
915 case KERN_INVALID_ADDRESS
:
925 mincore(__unused proc_t p
, struct mincore_args
*uap
, __unused
int32_t *retval
)
927 mach_vm_offset_t addr
, first_addr
, end
;
931 int vecindex
, lastvecindex
;
942 * Make sure that the addresses presented are valid for user
945 first_addr
= addr
= mach_vm_trunc_page(uap
->addr
);
946 end
= addr
+ mach_vm_round_page(uap
->len
);
952 * Address of byte vector
959 * Do this on a map entry basis so that if the pages are not
960 * in the current processes address space, we can easily look
961 * up the pages elsewhere.
964 for( ; addr
< end
; addr
+= PAGE_SIZE
) {
966 ret
= mach_vm_page_query(map
, addr
, &pqueryinfo
, &numref
);
967 if (ret
!= KERN_SUCCESS
)
970 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_PRESENT
)
971 mincoreinfo
|= MINCORE_INCORE
;
972 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_REF
)
973 mincoreinfo
|= MINCORE_REFERENCED
;
974 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_DIRTY
)
975 mincoreinfo
|= MINCORE_MODIFIED
;
979 * calculate index into user supplied byte vector
981 vecindex
= (addr
- first_addr
)>> PAGE_SHIFT
;
984 * If we have skipped map entries, we need to make sure that
985 * the byte vector is zeroed for those skipped entries.
987 while((lastvecindex
+ 1) < vecindex
) {
989 error
= copyout(&c
, vec
+ lastvecindex
, 1);
997 * Pass the page information to the user
999 c
= (char)mincoreinfo
;
1000 error
= copyout(&c
, vec
+ vecindex
, 1);
1004 lastvecindex
= vecindex
;
1009 * Zero the last entries in the byte vector.
1011 vecindex
= (end
- first_addr
) >> PAGE_SHIFT
;
1012 while((lastvecindex
+ 1) < vecindex
) {
1014 error
= copyout(&c
, vec
+ lastvecindex
, 1);
1025 mlock(__unused proc_t p
, struct mlock_args
*uap
, __unused
int32_t *retvalval
)
1028 vm_map_offset_t addr
;
1029 vm_map_size_t size
, pageoff
;
1030 kern_return_t result
;
1032 AUDIT_ARG(addr
, uap
->addr
);
1033 AUDIT_ARG(len
, uap
->len
);
1035 addr
= (vm_map_offset_t
) uap
->addr
;
1036 size
= (vm_map_size_t
)uap
->len
;
1038 /* disable wrap around */
1039 if (addr
+ size
< addr
)
1045 pageoff
= (addr
& PAGE_MASK
);
1047 size
= vm_map_round_page(size
+pageoff
);
1048 user_map
= current_map();
1050 /* have to call vm_map_wire directly to pass "I don't know" protections */
1051 result
= vm_map_wire(user_map
, addr
, addr
+size
, VM_PROT_NONE
, TRUE
);
1053 if (result
== KERN_RESOURCE_SHORTAGE
)
1055 else if (result
!= KERN_SUCCESS
)
1058 return 0; /* KERN_SUCCESS */
1062 munlock(__unused proc_t p
, struct munlock_args
*uap
, __unused
int32_t *retval
)
1064 mach_vm_offset_t addr
;
1065 mach_vm_size_t size
;
1067 kern_return_t result
;
1069 AUDIT_ARG(addr
, uap
->addr
);
1070 AUDIT_ARG(addr
, uap
->len
);
1072 addr
= (mach_vm_offset_t
) uap
->addr
;
1073 size
= (mach_vm_size_t
)uap
->len
;
1074 user_map
= current_map();
1076 /* JMM - need to remove all wirings by spec - this just removes one */
1077 result
= mach_vm_wire(host_priv_self(), user_map
, addr
, size
, VM_PROT_NONE
);
1078 return (result
== KERN_SUCCESS
? 0 : ENOMEM
);
1083 mlockall(__unused proc_t p
, __unused
struct mlockall_args
*uap
, __unused
int32_t *retval
)
1089 munlockall(__unused proc_t p
, __unused
struct munlockall_args
*uap
, __unused
int32_t *retval
)
1094 #if !defined(CONFIG_EMBEDDED)
1095 /* USV: No! need to obsolete map_fd()! mmap() already supports 64 bits */
1097 map_fd(struct map_fd_args
*args
)
1100 vm_offset_t offset
= args
->offset
;
1101 vm_offset_t
*va
= args
->va
;
1102 boolean_t findspace
= args
->findspace
;
1103 vm_size_t size
= args
->size
;
1106 AUDIT_MACH_SYSCALL_ENTER(AUE_MAPFD
);
1107 AUDIT_ARG(addr
, CAST_DOWN(user_addr_t
, args
->va
));
1110 ret
= map_fd_funneled( fd
, (vm_object_offset_t
)offset
, va
, findspace
, size
);
1112 AUDIT_MACH_SYSCALL_EXIT(ret
);
1119 vm_object_offset_t offset
,
1121 boolean_t findspace
,
1124 kern_return_t result
;
1125 struct fileproc
*fp
;
1128 vm_offset_t map_addr
=0;
1131 vm_prot_t maxprot
= VM_PROT_ALL
;
1133 proc_t p
= current_proc();
1134 struct vnode_attr vattr
;
1137 * Find the inode; verify that it's a regular file.
1140 err
= fp_lookup(p
, fd
, &fp
, 0);
1144 if (fp
->f_fglob
->fg_type
!= DTYPE_VNODE
){
1145 err
= KERN_INVALID_ARGUMENT
;
1149 if (!(fp
->f_fglob
->fg_flag
& FREAD
)) {
1150 err
= KERN_PROTECTION_FAILURE
;
1154 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1155 err
= vnode_getwithref(vp
);
1159 if (vp
->v_type
!= VREG
) {
1160 (void)vnode_put(vp
);
1161 err
= KERN_INVALID_ARGUMENT
;
1166 err
= mac_file_check_mmap(vfs_context_ucred(vfs_context_current()),
1167 fp
->f_fglob
, VM_PROT_DEFAULT
, MAP_FILE
, &maxprot
);
1169 (void)vnode_put(vp
);
1175 /* check for content protection access */
1177 err
= cp_handle_vnop(vp
, CP_READ_ACCESS
| CP_WRITE_ACCESS
, 0);
1179 (void) vnode_put(vp
);
1183 #endif /* CONFIG_PROTECT */
1185 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
1188 * POSIX: mmap needs to update access time for mapped files
1190 if ((vnode_vfsvisflags(vp
) & MNT_NOATIME
) == 0) {
1192 nanotime(&vattr
.va_access_time
);
1193 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
1194 vnode_setattr(vp
, &vattr
, vfs_context_current());
1197 if (offset
& PAGE_MASK_64
) {
1198 printf("map_fd: file offset not page aligned(%d : %s)\n",p
->p_pid
, p
->p_comm
);
1199 (void)vnode_put(vp
);
1200 err
= KERN_INVALID_ARGUMENT
;
1203 map_size
= round_page(size
);
1206 * Allow user to map in a zero length file.
1209 (void)vnode_put(vp
);
1216 pager
= (void *)ubc_getpager(vp
);
1217 if (pager
== NULL
) {
1218 (void)vnode_put(vp
);
1224 my_map
= current_map();
1228 &map_addr
, map_size
, (vm_offset_t
)0,
1229 VM_FLAGS_ANYWHERE
, pager
, offset
, TRUE
,
1230 VM_PROT_DEFAULT
, maxprot
,
1231 VM_INHERIT_DEFAULT
);
1232 if (result
!= KERN_SUCCESS
) {
1233 (void)vnode_put(vp
);
1240 //K64todo fix for 64bit user?
1244 if (copyin(CAST_USER_ADDR_T(va
), &dst_addr
, sizeof (dst_addr
)) ||
1245 trunc_page(dst_addr
) != dst_addr
) {
1246 (void) vm_map_remove(
1248 map_addr
, map_addr
+ map_size
,
1250 (void)vnode_put(vp
);
1251 err
= KERN_INVALID_ADDRESS
;
1255 result
= vm_map_copyin(my_map
, (vm_map_address_t
)map_addr
,
1256 (vm_map_size_t
)map_size
, TRUE
, &tmp
);
1257 if (result
!= KERN_SUCCESS
) {
1259 (void) vm_map_remove(my_map
, vm_map_trunc_page(map_addr
),
1260 vm_map_round_page(map_addr
+ map_size
),
1262 (void)vnode_put(vp
);
1267 result
= vm_map_copy_overwrite(my_map
,
1268 (vm_map_address_t
)dst_addr
, tmp
, FALSE
);
1269 if (result
!= KERN_SUCCESS
) {
1270 vm_map_copy_discard(tmp
);
1271 (void)vnode_put(vp
);
1276 // K64todo bug compatible now, should fix for 64bit user
1277 uint32_t user_map_addr
= CAST_DOWN_EXPLICIT(uint32_t, map_addr
);
1278 if (copyout(&user_map_addr
, CAST_USER_ADDR_T(va
), sizeof (user_map_addr
))) {
1279 (void) vm_map_remove(my_map
, vm_map_trunc_page(map_addr
),
1280 vm_map_round_page(map_addr
+ map_size
),
1282 (void)vnode_put(vp
);
1283 err
= KERN_INVALID_ADDRESS
;
1288 ubc_setthreadcred(vp
, current_proc(), current_thread());
1289 (void)vnode_put(vp
);
1292 fp_drop(p
, fd
, fp
, 0);
1295 #endif /* !defined(CONFIG_EMBEDDED) */