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 <kern/cpu_number.h>
119 #include <kern/host.h>
121 #include <vm/vm_map.h>
122 #include <vm/vm_kern.h>
123 #include <vm/vm_pager.h>
124 #include <vm/vm_protos.h>
126 /* XXX the following function should probably be static */
127 kern_return_t
map_fd_funneled(int, vm_object_offset_t
, vm_offset_t
*,
128 boolean_t
, vm_size_t
);
131 * XXX Internally, we use VM_PROT_* somewhat interchangeably, but the correct
132 * XXX usage is PROT_* from an interface perspective. Thus the values of
133 * XXX VM_PROT_* and PROT_* need to correspond.
136 mmap(proc_t p
, struct mmap_args
*uap
, user_addr_t
*retval
)
139 * Map in special device (must be SHARED) or file
142 register struct vnode
*vp
;
147 kern_return_t result
;
148 mach_vm_offset_t user_addr
;
149 mach_vm_size_t user_size
;
150 vm_object_offset_t pageoff
;
151 vm_object_offset_t file_pos
;
156 memory_object_t pager
= MEMORY_OBJECT_NULL
;
157 memory_object_control_t control
;
164 user_addr
= (mach_vm_offset_t
)uap
->addr
;
165 user_size
= (mach_vm_size_t
) uap
->len
;
167 AUDIT_ARG(addr
, user_addr
);
168 AUDIT_ARG(len
, user_size
);
169 AUDIT_ARG(fd
, uap
->fd
);
171 prot
= (uap
->prot
& VM_PROT_ALL
);
174 * Since the hardware currently does not support writing without
175 * read-before-write, or execution-without-read, if the request is
176 * for write or execute access, we must imply read access as well;
177 * otherwise programs expecting this to work will fail to operate.
179 if (prot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
180 prot
|= VM_PROT_READ
;
181 #endif /* radar 3777787 */
187 * The vm code does not have prototypes & compiler doesn't do the'
188 * the right thing when you cast 64bit value and pass it in function
189 * call. So here it is.
191 file_pos
= (vm_object_offset_t
)uap
->pos
;
194 /* make sure mapping fits into numeric range etc */
195 if (file_pos
+ user_size
> (vm_object_offset_t
)-PAGE_SIZE_64
)
199 * Align the file position to a page boundary,
200 * and save its page offset component.
202 pageoff
= (file_pos
& PAGE_MASK
);
203 file_pos
-= (vm_object_offset_t
)pageoff
;
206 /* Adjust size for rounding (on both ends). */
207 user_size
+= pageoff
; /* low end... */
208 user_size
= mach_vm_round_page(user_size
); /* hi end */
210 if ((flags
& MAP_JIT
) && ((flags
& MAP_FIXED
) || (flags
& MAP_SHARED
) || (flags
& MAP_FILE
))){
214 * Check for illegal addresses. Watch out for address wrap... Note
215 * that VM_*_ADDRESS are not constants due to casts (argh).
217 if (flags
& MAP_FIXED
) {
219 * The specified address must have the same remainder
220 * as the file offset taken modulo PAGE_SIZE, so it
221 * should be aligned after adjustment by pageoff.
223 user_addr
-= pageoff
;
224 if (user_addr
& PAGE_MASK
)
228 /* DO not have apis to get this info, need to wait till then*/
230 * XXX for non-fixed mappings where no hint is provided or
231 * the hint would fall in the potential heap space,
232 * place it after the end of the largest possible heap.
234 * There should really be a pmap call to determine a reasonable
237 else if (addr
< mach_vm_round_page(p
->p_vmspace
->vm_daddr
+ MAXDSIZ
))
238 addr
= mach_vm_round_page(p
->p_vmspace
->vm_daddr
+ MAXDSIZ
);
244 if (flags
& MAP_ANON
) {
246 maxprot
= VM_PROT_ALL
;
250 * Re-enable once mac* is implemented.
252 /*error = mac_proc_check_map_anon(p, user_addr, user_size, prot, flags, &maxprot);
259 * Mapping blank space is trivial. Use positive fds as the alias
260 * value for memory tracking.
264 * Use "fd" to pass (some) Mach VM allocation flags,
265 * (see the VM_FLAGS_* definitions).
267 alloc_flags
= fd
& (VM_FLAGS_ALIAS_MASK
| VM_FLAGS_SUPERPAGE_MASK
|
269 if (alloc_flags
!= fd
) {
270 /* reject if there are any extra flags */
279 struct vnode_attr va
;
280 vfs_context_t ctx
= vfs_context_current();
283 * Mapping file, get fp for validation. Obtain vnode and make
284 * sure it is of appropriate type.
286 err
= fp_lookup(p
, fd
, &fp
, 0);
290 if(fp
->f_fglob
->fg_type
== DTYPE_PSXSHM
) {
291 uap
->addr
= (user_addr_t
)user_addr
;
292 uap
->len
= (user_size_t
)user_size
;
296 error
= pshm_mmap(p
, uap
, retval
, fp
, (off_t
)pageoff
);
300 if (fp
->f_fglob
->fg_type
!= DTYPE_VNODE
) {
304 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
305 error
= vnode_getwithref(vp
);
309 if (vp
->v_type
!= VREG
&& vp
->v_type
!= VCHR
) {
315 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
318 * POSIX: mmap needs to update access time for mapped files
320 if ((vnode_vfsvisflags(vp
) & MNT_NOATIME
) == 0) {
322 nanotime(&va
.va_access_time
);
323 VATTR_SET_ACTIVE(&va
, va_access_time
);
324 vnode_setattr(vp
, &va
, ctx
);
328 * XXX hack to handle use of /dev/zero to map anon memory (ala
331 if (vp
->v_type
== VCHR
|| vp
->v_type
== VSTR
) {
337 * Ensure that file and memory protections are
338 * compatible. Note that we only worry about
339 * writability if mapping is shared; in this case,
340 * current and max prot are dictated by the open file.
341 * XXX use the vnode instead? Problem is: what
342 * credentials do we use for determination? What if
343 * proc does a setuid?
345 maxprot
= VM_PROT_EXECUTE
; /* ??? */
346 if (fp
->f_fglob
->fg_flag
& FREAD
)
347 maxprot
|= VM_PROT_READ
;
348 else if (prot
& PROT_READ
) {
354 * If we are sharing potential changes (either via
355 * MAP_SHARED or via the implicit sharing of character
356 * device mappings), and we are trying to get write
357 * permission although we opened it without asking
361 if ((flags
& MAP_SHARED
) != 0) {
362 if ((fp
->f_fglob
->fg_flag
& FWRITE
) != 0 &&
364 * Do not allow writable mappings of
365 * swap files (see vm_swapfile_pager.c).
369 * check for write access
371 * Note that we already made this check when granting FWRITE
372 * against the file, so it seems redundant here.
374 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_CHECKIMMUTABLE
, ctx
);
376 /* if not granted for any reason, but we wanted it, bad */
377 if ((prot
& PROT_WRITE
) && (error
!= 0)) {
382 /* if writable, remember */
384 maxprot
|= VM_PROT_WRITE
;
386 } else if ((prot
& PROT_WRITE
) != 0) {
392 maxprot
|= VM_PROT_WRITE
;
396 error
= mac_file_check_mmap(vfs_context_ucred(ctx
),
397 fp
->f_fglob
, prot
, flags
, &maxprot
);
407 if ((cnode
= cp_get_protected_cnode(vp
)) != NULL
) {
408 error
= cp_handle_vnop(cnode
, CP_READ_ACCESS
| CP_WRITE_ACCESS
);
410 (void) vnode_put(vp
);
415 #endif /* CONFIG_PROTECT */
421 if (user_size
== 0) {
429 * We bend a little - round the start and end addresses
430 * to the nearest page boundary.
432 user_size
= mach_vm_round_page(user_size
);
434 if (file_pos
& PAGE_MASK_64
) {
441 user_map
= current_map();
443 if ((flags
& MAP_FIXED
) == 0) {
444 alloc_flags
|= VM_FLAGS_ANYWHERE
;
445 user_addr
= mach_vm_round_page(user_addr
);
447 if (user_addr
!= mach_vm_trunc_page(user_addr
)) {
454 * mmap(MAP_FIXED) will replace any existing mappings in the
455 * specified range, if the new mapping is successful.
456 * If we just deallocate the specified address range here,
457 * another thread might jump in and allocate memory in that
458 * range before we get a chance to establish the new mapping,
459 * and we won't have a chance to restore the old mappings.
460 * So we use VM_FLAGS_OVERWRITE to let Mach VM know that it
461 * has to deallocate the existing mappings and establish the
462 * new ones atomically.
464 alloc_flags
|= VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
;
467 if (flags
& MAP_NOCACHE
)
468 alloc_flags
|= VM_FLAGS_NO_CACHE
;
470 if (flags
& MAP_JIT
){
471 alloc_flags
|= VM_FLAGS_MAP_JIT
;
474 * Lookup/allocate object.
476 if (handle
== NULL
) {
480 #if defined(VM_PROT_READ_IS_EXEC)
481 if (prot
& VM_PROT_READ
)
482 prot
|= VM_PROT_EXECUTE
;
483 if (maxprot
& VM_PROT_READ
)
484 maxprot
|= VM_PROT_EXECUTE
;
489 if (prot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
490 prot
|= VM_PROT_READ
;
491 if (maxprot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
492 maxprot
|= VM_PROT_READ
;
493 #endif /* radar 3777787 */
495 result
= vm_map_enter_mem_object(user_map
,
496 &user_addr
, user_size
,
498 IPC_PORT_NULL
, 0, FALSE
,
500 (flags
& MAP_SHARED
) ?
504 /* If a non-binding address was specified for this anonymous
505 * mapping, retry the mapping with a zero base
506 * in the event the mapping operation failed due to
507 * lack of space between the address and the map's maximum.
509 if ((result
== KERN_NO_SPACE
) && ((flags
& MAP_FIXED
) == 0) && user_addr
&& (num_retries
++ == 0)) {
510 user_addr
= PAGE_SIZE
;
514 if (vnode_isswap(vp
)) {
516 * Map swap files with a special pager
517 * that returns obfuscated contents.
520 pager
= swapfile_pager_setup(vp
);
521 if (pager
!= MEMORY_OBJECT_NULL
) {
522 control
= swapfile_pager_control(pager
);
525 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
528 if (control
== NULL
) {
536 * FIXME: if we're writing the file we need a way to
537 * ensure that someone doesn't replace our R/W creds
538 * with ones that only work for read.
541 ubc_setthreadcred(vp
, p
, current_thread());
543 if ((flags
& (MAP_ANON
|MAP_SHARED
)) == 0) {
549 #if defined(VM_PROT_READ_IS_EXEC)
550 if (prot
& VM_PROT_READ
)
551 prot
|= VM_PROT_EXECUTE
;
552 if (maxprot
& VM_PROT_READ
)
553 maxprot
|= VM_PROT_EXECUTE
;
558 if (prot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
559 prot
|= VM_PROT_READ
;
560 if (maxprot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
561 maxprot
|= VM_PROT_READ
;
562 #endif /* radar 3777787 */
564 result
= vm_map_enter_mem_object_control(user_map
,
565 &user_addr
, user_size
,
568 docow
, prot
, maxprot
,
569 (flags
& MAP_SHARED
) ?
573 /* If a non-binding address was specified for this file backed
574 * mapping, retry the mapping with a zero base
575 * in the event the mapping operation failed due to
576 * lack of space between the address and the map's maximum.
578 if ((result
== KERN_NO_SPACE
) && ((flags
& MAP_FIXED
) == 0) && user_addr
&& (num_retries
++ == 0)) {
579 user_addr
= PAGE_SIZE
;
590 *retval
= user_addr
+ pageoff
;
593 case KERN_INVALID_ADDRESS
:
597 case KERN_PROTECTION_FAILURE
:
605 if (pager
!= MEMORY_OBJECT_NULL
) {
607 * Release the reference on the pager.
608 * If the mapping was successful, it now holds
609 * an extra reference.
611 memory_object_deallocate(pager
);
614 fp_drop(p
, fd
, fp
, 0);
616 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);
617 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO2
, SYS_mmap
) | DBG_FUNC_NONE
), (uint32_t)(*retval
>> 32), (uint32_t)(user_size
>> 32),
618 (uint32_t)(file_pos
>> 32), (uint32_t)file_pos
, 0);
624 msync(__unused proc_t p
, struct msync_args
*uap
, int32_t *retval
)
626 __pthread_testcancel(1);
627 return(msync_nocancel(p
, (struct msync_nocancel_args
*)uap
, retval
));
631 msync_nocancel(__unused proc_t p
, struct msync_nocancel_args
*uap
, __unused
int32_t *retval
)
633 mach_vm_offset_t addr
;
638 vm_sync_t sync_flags
=0;
640 addr
= (mach_vm_offset_t
) uap
->addr
;
641 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);
645 if (addr
& PAGE_MASK_64
) {
646 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
651 * We cannot support this properly without maintaining
652 * list all mmaps done. Cannot use vm_map_entry as they could be
653 * split or coalesced by indepenedant actions. So instead of
654 * inaccurate results, lets just return error as invalid size
657 return (EINVAL
); /* XXX breaks posix apps */
661 /* disallow contradictory flags */
662 if ((flags
& (MS_SYNC
|MS_ASYNC
)) == (MS_SYNC
|MS_ASYNC
))
665 if (flags
& MS_KILLPAGES
)
666 sync_flags
|= VM_SYNC_KILLPAGES
;
667 if (flags
& MS_DEACTIVATE
)
668 sync_flags
|= VM_SYNC_DEACTIVATE
;
669 if (flags
& MS_INVALIDATE
)
670 sync_flags
|= VM_SYNC_INVALIDATE
;
672 if ( !(flags
& (MS_KILLPAGES
| MS_DEACTIVATE
))) {
673 if (flags
& MS_ASYNC
)
674 sync_flags
|= VM_SYNC_ASYNCHRONOUS
;
676 sync_flags
|= VM_SYNC_SYNCHRONOUS
;
679 sync_flags
|= VM_SYNC_CONTIGUOUS
; /* complain if holes */
681 user_map
= current_map();
682 rv
= mach_vm_msync(user_map
, addr
, size
, sync_flags
);
687 case KERN_INVALID_ADDRESS
: /* hole in region being sync'ed */
699 munmap(__unused proc_t p
, struct munmap_args
*uap
, __unused
int32_t *retval
)
701 mach_vm_offset_t user_addr
;
702 mach_vm_size_t user_size
;
703 kern_return_t result
;
705 user_addr
= (mach_vm_offset_t
) uap
->addr
;
706 user_size
= (mach_vm_size_t
) uap
->len
;
708 AUDIT_ARG(addr
, user_addr
);
709 AUDIT_ARG(len
, user_size
);
711 if (user_addr
& PAGE_MASK_64
) {
712 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
716 if (user_addr
+ user_size
< user_addr
)
719 if (user_size
== 0) {
720 /* UNIX SPEC: size is 0, return EINVAL */
724 result
= mach_vm_deallocate(current_map(), user_addr
, user_size
);
725 if (result
!= KERN_SUCCESS
) {
732 mprotect(__unused proc_t p
, struct mprotect_args
*uap
, __unused
int32_t *retval
)
734 register vm_prot_t prot
;
735 mach_vm_offset_t user_addr
;
736 mach_vm_size_t user_size
;
737 kern_return_t result
;
743 AUDIT_ARG(addr
, uap
->addr
);
744 AUDIT_ARG(len
, uap
->len
);
745 AUDIT_ARG(value32
, uap
->prot
);
747 user_addr
= (mach_vm_offset_t
) uap
->addr
;
748 user_size
= (mach_vm_size_t
) uap
->len
;
749 prot
= (vm_prot_t
)(uap
->prot
& (VM_PROT_ALL
| VM_PROT_TRUSTED
));
751 if (user_addr
& PAGE_MASK_64
) {
752 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
758 #if defined(VM_PROT_READ_IS_EXEC)
759 if (prot
& VM_PROT_READ
)
760 prot
|= VM_PROT_EXECUTE
;
765 if (prot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
766 prot
|= VM_PROT_READ
;
769 user_map
= current_map();
773 * The MAC check for mprotect is of limited use for 2 reasons:
774 * Without mmap revocation, the caller could have asked for the max
775 * protections initially instead of a reduced set, so a mprotect
776 * check would offer no new security.
777 * It is not possible to extract the vnode from the pager object(s)
778 * of the target memory range.
779 * However, the MAC check may be used to prevent a process from,
780 * e.g., making the stack executable.
782 error
= mac_proc_check_mprotect(p
, user_addr
,
788 if(prot
& VM_PROT_TRUSTED
) {
789 #if CONFIG_DYNAMIC_CODE_SIGNING
790 /* CODE SIGNING ENFORCEMENT - JIT support */
791 /* The special protection value VM_PROT_TRUSTED requests that we treat
792 * this page as if it had a valid code signature.
793 * If this is enabled, there MUST be a MAC policy implementing the
794 * mac_proc_check_mprotect() hook above. Otherwise, Codesigning will be
795 * compromised because the check would always succeed and thusly any
796 * process could sign dynamically. */
797 result
= vm_map_sign(user_map
,
798 vm_map_trunc_page(user_addr
),
799 vm_map_round_page(user_addr
+user_size
));
803 case KERN_INVALID_ADDRESS
:
804 /* UNIX SPEC: for an invalid address range, return ENOMEM */
813 prot
&= ~VM_PROT_TRUSTED
;
815 result
= mach_vm_protect(user_map
, user_addr
, user_size
,
820 case KERN_PROTECTION_FAILURE
:
822 case KERN_INVALID_ADDRESS
:
823 /* UNIX SPEC: for an invalid address range, return ENOMEM */
831 minherit(__unused proc_t p
, struct minherit_args
*uap
, __unused
int32_t *retval
)
833 mach_vm_offset_t addr
;
835 register vm_inherit_t inherit
;
837 kern_return_t result
;
839 AUDIT_ARG(addr
, uap
->addr
);
840 AUDIT_ARG(len
, uap
->len
);
841 AUDIT_ARG(value32
, uap
->inherit
);
843 addr
= (mach_vm_offset_t
)uap
->addr
;
844 size
= (mach_vm_size_t
)uap
->len
;
845 inherit
= uap
->inherit
;
847 user_map
= current_map();
848 result
= mach_vm_inherit(user_map
, addr
, size
,
853 case KERN_PROTECTION_FAILURE
:
860 madvise(__unused proc_t p
, struct madvise_args
*uap
, __unused
int32_t *retval
)
863 mach_vm_offset_t start
;
865 vm_behavior_t new_behavior
;
866 kern_return_t result
;
869 * Since this routine is only advisory, we default to conservative
872 switch (uap
->behav
) {
874 new_behavior
= VM_BEHAVIOR_RANDOM
;
876 case MADV_SEQUENTIAL
:
877 new_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
880 new_behavior
= VM_BEHAVIOR_DEFAULT
;
883 new_behavior
= VM_BEHAVIOR_WILLNEED
;
886 new_behavior
= VM_BEHAVIOR_DONTNEED
;
889 new_behavior
= VM_BEHAVIOR_FREE
;
891 case MADV_ZERO_WIRED_PAGES
:
892 new_behavior
= VM_BEHAVIOR_ZERO_WIRED_PAGES
;
894 case MADV_FREE_REUSABLE
:
895 new_behavior
= VM_BEHAVIOR_REUSABLE
;
897 case MADV_FREE_REUSE
:
898 new_behavior
= VM_BEHAVIOR_REUSE
;
901 new_behavior
= VM_BEHAVIOR_CAN_REUSE
;
907 start
= (mach_vm_offset_t
) uap
->addr
;
908 size
= (mach_vm_size_t
) uap
->len
;
910 user_map
= current_map();
912 result
= mach_vm_behavior_set(user_map
, start
, size
, new_behavior
);
916 case KERN_INVALID_ADDRESS
:
926 mincore(__unused proc_t p
, struct mincore_args
*uap
, __unused
int32_t *retval
)
928 mach_vm_offset_t addr
, first_addr
, end
;
932 int vecindex
, lastvecindex
;
943 * Make sure that the addresses presented are valid for user
946 first_addr
= addr
= mach_vm_trunc_page(uap
->addr
);
947 end
= addr
+ mach_vm_round_page(uap
->len
);
953 * Address of byte vector
960 * Do this on a map entry basis so that if the pages are not
961 * in the current processes address space, we can easily look
962 * up the pages elsewhere.
965 for( ; addr
< end
; addr
+= PAGE_SIZE
) {
967 ret
= mach_vm_page_query(map
, addr
, &pqueryinfo
, &numref
);
968 if (ret
!= KERN_SUCCESS
)
971 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_PRESENT
)
972 mincoreinfo
|= MINCORE_INCORE
;
973 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_REF
)
974 mincoreinfo
|= MINCORE_REFERENCED
;
975 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_DIRTY
)
976 mincoreinfo
|= MINCORE_MODIFIED
;
980 * calculate index into user supplied byte vector
982 vecindex
= (addr
- first_addr
)>> PAGE_SHIFT
;
985 * If we have skipped map entries, we need to make sure that
986 * the byte vector is zeroed for those skipped entries.
988 while((lastvecindex
+ 1) < vecindex
) {
990 error
= copyout(&c
, vec
+ lastvecindex
, 1);
998 * Pass the page information to the user
1000 c
= (char)mincoreinfo
;
1001 error
= copyout(&c
, vec
+ vecindex
, 1);
1005 lastvecindex
= vecindex
;
1010 * Zero the last entries in the byte vector.
1012 vecindex
= (end
- first_addr
) >> PAGE_SHIFT
;
1013 while((lastvecindex
+ 1) < vecindex
) {
1015 error
= copyout(&c
, vec
+ lastvecindex
, 1);
1026 mlock(__unused proc_t p
, struct mlock_args
*uap
, __unused
int32_t *retvalval
)
1029 vm_map_offset_t addr
;
1030 vm_map_size_t size
, pageoff
;
1031 kern_return_t result
;
1033 AUDIT_ARG(addr
, uap
->addr
);
1034 AUDIT_ARG(len
, uap
->len
);
1036 addr
= (vm_map_offset_t
) uap
->addr
;
1037 size
= (vm_map_size_t
)uap
->len
;
1039 /* disable wrap around */
1040 if (addr
+ size
< addr
)
1046 pageoff
= (addr
& PAGE_MASK
);
1048 size
= vm_map_round_page(size
+pageoff
);
1049 user_map
= current_map();
1051 /* have to call vm_map_wire directly to pass "I don't know" protections */
1052 result
= vm_map_wire(user_map
, addr
, addr
+size
, VM_PROT_NONE
, TRUE
);
1054 if (result
== KERN_RESOURCE_SHORTAGE
)
1056 else if (result
!= KERN_SUCCESS
)
1059 return 0; /* KERN_SUCCESS */
1063 munlock(__unused proc_t p
, struct munlock_args
*uap
, __unused
int32_t *retval
)
1065 mach_vm_offset_t addr
;
1066 mach_vm_size_t size
;
1068 kern_return_t result
;
1070 AUDIT_ARG(addr
, uap
->addr
);
1071 AUDIT_ARG(addr
, uap
->len
);
1073 addr
= (mach_vm_offset_t
) uap
->addr
;
1074 size
= (mach_vm_size_t
)uap
->len
;
1075 user_map
= current_map();
1077 /* JMM - need to remove all wirings by spec - this just removes one */
1078 result
= mach_vm_wire(host_priv_self(), user_map
, addr
, size
, VM_PROT_NONE
);
1079 return (result
== KERN_SUCCESS
? 0 : ENOMEM
);
1084 mlockall(__unused proc_t p
, __unused
struct mlockall_args
*uap
, __unused
int32_t *retval
)
1090 munlockall(__unused proc_t p
, __unused
struct munlockall_args
*uap
, __unused
int32_t *retval
)
1095 #if !defined(CONFIG_EMBEDDED)
1096 /* USV: No! need to obsolete map_fd()! mmap() already supports 64 bits */
1098 map_fd(struct map_fd_args
*args
)
1101 vm_offset_t offset
= args
->offset
;
1102 vm_offset_t
*va
= args
->va
;
1103 boolean_t findspace
= args
->findspace
;
1104 vm_size_t size
= args
->size
;
1107 AUDIT_MACH_SYSCALL_ENTER(AUE_MAPFD
);
1108 AUDIT_ARG(addr
, CAST_DOWN(user_addr_t
, args
->va
));
1111 ret
= map_fd_funneled( fd
, (vm_object_offset_t
)offset
, va
, findspace
, size
);
1113 AUDIT_MACH_SYSCALL_EXIT(ret
);
1120 vm_object_offset_t offset
,
1122 boolean_t findspace
,
1125 kern_return_t result
;
1126 struct fileproc
*fp
;
1129 vm_offset_t map_addr
=0;
1132 vm_prot_t maxprot
= VM_PROT_ALL
;
1134 proc_t p
= current_proc();
1135 struct vnode_attr vattr
;
1138 * Find the inode; verify that it's a regular file.
1141 err
= fp_lookup(p
, fd
, &fp
, 0);
1145 if (fp
->f_fglob
->fg_type
!= DTYPE_VNODE
){
1146 err
= KERN_INVALID_ARGUMENT
;
1150 if (!(fp
->f_fglob
->fg_flag
& FREAD
)) {
1151 err
= KERN_PROTECTION_FAILURE
;
1155 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1156 err
= vnode_getwithref(vp
);
1160 if (vp
->v_type
!= VREG
) {
1161 (void)vnode_put(vp
);
1162 err
= KERN_INVALID_ARGUMENT
;
1167 err
= mac_file_check_mmap(vfs_context_ucred(vfs_context_current()),
1168 fp
->f_fglob
, VM_PROT_DEFAULT
, MAP_FILE
, &maxprot
);
1170 (void)vnode_put(vp
);
1176 /* check for content protection access */
1179 if ((cnode
= cp_get_protected_cnode(vp
)) != NULL
) {
1180 err
= cp_handle_vnop(cnode
, CP_READ_ACCESS
| CP_WRITE_ACCESS
);
1182 (void)vnode_put(vp
);
1187 #endif /* CONFIG_PROTECT */
1189 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
1192 * POSIX: mmap needs to update access time for mapped files
1194 if ((vnode_vfsvisflags(vp
) & MNT_NOATIME
) == 0) {
1196 nanotime(&vattr
.va_access_time
);
1197 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
1198 vnode_setattr(vp
, &vattr
, vfs_context_current());
1201 if (offset
& PAGE_MASK_64
) {
1202 printf("map_fd: file offset not page aligned(%d : %s)\n",p
->p_pid
, p
->p_comm
);
1203 (void)vnode_put(vp
);
1204 err
= KERN_INVALID_ARGUMENT
;
1207 map_size
= round_page(size
);
1210 * Allow user to map in a zero length file.
1213 (void)vnode_put(vp
);
1220 pager
= (void *)ubc_getpager(vp
);
1221 if (pager
== NULL
) {
1222 (void)vnode_put(vp
);
1228 my_map
= current_map();
1232 &map_addr
, map_size
, (vm_offset_t
)0,
1233 VM_FLAGS_ANYWHERE
, pager
, offset
, TRUE
,
1234 VM_PROT_DEFAULT
, maxprot
,
1235 VM_INHERIT_DEFAULT
);
1236 if (result
!= KERN_SUCCESS
) {
1237 (void)vnode_put(vp
);
1244 //K64todo fix for 64bit user?
1248 if (copyin(CAST_USER_ADDR_T(va
), &dst_addr
, sizeof (dst_addr
)) ||
1249 trunc_page(dst_addr
) != dst_addr
) {
1250 (void) vm_map_remove(
1252 map_addr
, map_addr
+ map_size
,
1254 (void)vnode_put(vp
);
1255 err
= KERN_INVALID_ADDRESS
;
1259 result
= vm_map_copyin(my_map
, (vm_map_address_t
)map_addr
,
1260 (vm_map_size_t
)map_size
, TRUE
, &tmp
);
1261 if (result
!= KERN_SUCCESS
) {
1263 (void) vm_map_remove(my_map
, vm_map_trunc_page(map_addr
),
1264 vm_map_round_page(map_addr
+ map_size
),
1266 (void)vnode_put(vp
);
1271 result
= vm_map_copy_overwrite(my_map
,
1272 (vm_map_address_t
)dst_addr
, tmp
, FALSE
);
1273 if (result
!= KERN_SUCCESS
) {
1274 vm_map_copy_discard(tmp
);
1275 (void)vnode_put(vp
);
1280 // K64todo bug compatible now, should fix for 64bit user
1281 uint32_t user_map_addr
= CAST_DOWN_EXPLICIT(uint32_t, map_addr
);
1282 if (copyout(&user_map_addr
, CAST_USER_ADDR_T(va
), sizeof (user_map_addr
))) {
1283 (void) vm_map_remove(my_map
, vm_map_trunc_page(map_addr
),
1284 vm_map_round_page(map_addr
+ map_size
),
1286 (void)vnode_put(vp
);
1287 err
= KERN_INVALID_ADDRESS
;
1292 ubc_setthreadcred(vp
, current_proc(), current_thread());
1293 (void)vnode_put(vp
);
1296 fp_drop(p
, fd
, fp
, 0);
1299 #endif /* !defined(CONFIG_EMBEDDED) */