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/syscall.h>
100 #include <sys/kdebug.h>
102 #include <bsm/audit_kernel.h>
103 #include <bsm/audit_kevents.h>
105 #include <mach/mach_types.h>
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>
115 #include <kern/cpu_number.h>
116 #include <kern/host.h>
118 #include <vm/vm_map.h>
119 #include <vm/vm_kern.h>
120 #include <vm/vm_pager.h>
131 /* XXX the following function should probably be static */
132 kern_return_t
map_fd_funneled(int, vm_object_offset_t
, vm_offset_t
*,
133 boolean_t
, vm_size_t
);
135 /* XXX the following two functions aren't used anywhere */
136 int osmmap(proc_t
, struct osmmap_args
*, register_t
*);
140 sbrk(__unused proc_t p
, __unused
struct sbrk_args
*uap
, __unused register_t
*retval
)
142 /* Not yet implemented */
147 sstk(__unused proc_t p
, __unused
struct sstk_args
*uap
, __unused register_t
*retval
)
149 /* Not yet implemented */
157 struct osmmap_args
*uap
,
160 struct mmap_args newargs
;
164 if ((uap
->share
== MAP_SHARED
)|| (uap
->share
== MAP_PRIVATE
)) {
165 newargs
.addr
= CAST_USER_ADDR_T(uap
->addr
);
166 newargs
.len
= CAST_USER_ADDR_T(uap
->len
);
167 newargs
.prot
= uap
->prot
;
168 newargs
.flags
= uap
->share
;
169 newargs
.fd
= uap
->fd
;
170 newargs
.pos
= (off_t
)uap
->pos
;
171 ret
= mmap(curp
, &newargs
, &addr
);
173 *retval
= CAST_DOWN(register_t
, addr
);
181 * XXX Internally, we use VM_PROT_* somewhat interchangeably, but the correct
182 * XXX usage is PROT_* from an interface perspective. Thus the values of
183 * XXX VM_PROT_* and PROT_* need to correspond.
186 mmap(proc_t p
, struct mmap_args
*uap
, user_addr_t
*retval
)
189 * Map in special device (must be SHARED) or file
192 register struct vnode
*vp
;
197 kern_return_t result
;
198 mach_vm_offset_t user_addr
;
199 mach_vm_size_t user_size
;
200 vm_object_offset_t pageoff
;
201 vm_object_offset_t file_pos
;
212 user_addr
= (mach_vm_offset_t
)uap
->addr
;
213 user_size
= (mach_vm_size_t
) uap
->len
;
215 AUDIT_ARG(addr
, user_addr
);
216 AUDIT_ARG(len
, user_size
);
217 AUDIT_ARG(fd
, uap
->fd
);
219 prot
= (uap
->prot
& VM_PROT_ALL
);
222 * Since the hardware currently does not support writing without
223 * read-before-write, or execution-without-read, if the request is
224 * for write or execute access, we must imply read access as well;
225 * otherwise programs expecting this to work will fail to operate.
227 if (prot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
228 prot
|= VM_PROT_READ
;
229 #endif /* radar 3777787 */
235 * The vm code does not have prototypes & compiler doesn't do the'
236 * the right thing when you cast 64bit value and pass it in function
237 * call. So here it is.
239 file_pos
= (vm_object_offset_t
)uap
->pos
;
242 /* make sure mapping fits into numeric range etc */
243 if (file_pos
+ user_size
> (vm_object_offset_t
)-PAGE_SIZE_64
)
247 * Align the file position to a page boundary,
248 * and save its page offset component.
250 pageoff
= (file_pos
& PAGE_MASK
);
251 file_pos
-= (vm_object_offset_t
)pageoff
;
254 /* Adjust size for rounding (on both ends). */
255 user_size
+= pageoff
; /* low end... */
256 user_size
= mach_vm_round_page(user_size
); /* hi end */
260 * Check for illegal addresses. Watch out for address wrap... Note
261 * that VM_*_ADDRESS are not constants due to casts (argh).
263 if (flags
& MAP_FIXED
) {
265 * The specified address must have the same remainder
266 * as the file offset taken modulo PAGE_SIZE, so it
267 * should be aligned after adjustment by pageoff.
269 user_addr
-= pageoff
;
270 if (user_addr
& PAGE_MASK
)
274 /* DO not have apis to get this info, need to wait till then*/
276 * XXX for non-fixed mappings where no hint is provided or
277 * the hint would fall in the potential heap space,
278 * place it after the end of the largest possible heap.
280 * There should really be a pmap call to determine a reasonable
283 else if (addr
< mach_vm_round_page(p
->p_vmspace
->vm_daddr
+ MAXDSIZ
))
284 addr
= mach_vm_round_page(p
->p_vmspace
->vm_daddr
+ MAXDSIZ
);
290 if (flags
& MAP_ANON
) {
292 * Mapping blank space is trivial. Use positive fds as the alias
293 * value for memory tracking.
297 * Use "fd" to pass (some) Mach VM allocation flags,
298 * (see the VM_FLAGS_* definitions).
300 alloc_flags
= fd
& (VM_FLAGS_ALIAS_MASK
|
302 if (alloc_flags
!= fd
) {
303 /* reject if there are any extra flags */
309 maxprot
= VM_PROT_ALL
;
313 struct vnode_attr va
;
314 vfs_context_t ctx
= vfs_context_current();
317 * Mapping file, get fp for validation. Obtain vnode and make
318 * sure it is of appropriate type.
320 err
= fp_lookup(p
, fd
, &fp
, 0);
324 if(fp
->f_fglob
->fg_type
== DTYPE_PSXSHM
) {
325 uap
->addr
= (user_addr_t
)user_addr
;
326 uap
->len
= (user_size_t
)user_size
;
330 error
= pshm_mmap(p
, uap
, retval
, fp
, (off_t
)pageoff
);
334 if (fp
->f_fglob
->fg_type
!= DTYPE_VNODE
) {
338 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
339 error
= vnode_getwithref(vp
);
343 if (vp
->v_type
!= VREG
&& vp
->v_type
!= VCHR
) {
349 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
352 * POSIX: mmap needs to update access time for mapped files
354 if ((vnode_vfsvisflags(vp
) & MNT_NOATIME
) == 0) {
356 nanotime(&va
.va_access_time
);
357 VATTR_SET_ACTIVE(&va
, va_access_time
);
358 vnode_setattr(vp
, &va
, ctx
);
362 * XXX hack to handle use of /dev/zero to map anon memory (ala
365 if (vp
->v_type
== VCHR
|| vp
->v_type
== VSTR
) {
371 * Ensure that file and memory protections are
372 * compatible. Note that we only worry about
373 * writability if mapping is shared; in this case,
374 * current and max prot are dictated by the open file.
375 * XXX use the vnode instead? Problem is: what
376 * credentials do we use for determination? What if
377 * proc does a setuid?
379 maxprot
= VM_PROT_EXECUTE
; /* ??? */
380 if (fp
->f_fglob
->fg_flag
& FREAD
)
381 maxprot
|= VM_PROT_READ
;
382 else if (prot
& PROT_READ
) {
388 * If we are sharing potential changes (either via
389 * MAP_SHARED or via the implicit sharing of character
390 * device mappings), and we are trying to get write
391 * permission although we opened it without asking
395 if ((flags
& MAP_SHARED
) != 0) {
396 if ((fp
->f_fglob
->fg_flag
& FWRITE
) != 0) {
398 * check for write access
400 * Note that we already made this check when granting FWRITE
401 * against the file, so it seems redundant here.
403 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_CHECKIMMUTABLE
, ctx
);
405 /* if not granted for any reason, but we wanted it, bad */
406 if ((prot
& PROT_WRITE
) && (error
!= 0)) {
411 /* if writable, remember */
413 maxprot
|= VM_PROT_WRITE
;
415 } else if ((prot
& PROT_WRITE
) != 0) {
421 maxprot
|= VM_PROT_WRITE
;
425 error
= mac_file_check_mmap(vfs_context_ucred(ctx
),
426 fp
->f_fglob
, prot
, flags
, &maxprot
);
435 if (user_size
== 0) {
443 * We bend a little - round the start and end addresses
444 * to the nearest page boundary.
446 user_size
= mach_vm_round_page(user_size
);
448 if (file_pos
& PAGE_MASK_64
) {
455 user_map
= current_map();
457 if ((flags
& MAP_FIXED
) == 0) {
458 alloc_flags
|= VM_FLAGS_ANYWHERE
;
459 user_addr
= mach_vm_round_page(user_addr
);
461 if (user_addr
!= mach_vm_trunc_page(user_addr
)) {
468 * mmap(MAP_FIXED) will replace any existing mappings in the
469 * specified range, if the new mapping is successful.
470 * If we just deallocate the specified address range here,
471 * another thread might jump in and allocate memory in that
472 * range before we get a chance to establish the new mapping,
473 * and we won't have a chance to restore the old mappings.
474 * So we use VM_FLAGS_OVERWRITE to let Mach VM know that it
475 * has to deallocate the existing mappings and establish the
476 * new ones atomically.
478 alloc_flags
|= VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
;
481 if (flags
& MAP_NOCACHE
)
482 alloc_flags
|= VM_FLAGS_NO_CACHE
;
485 * Lookup/allocate object.
487 if (handle
== NULL
) {
491 #if defined(VM_PROT_READ_IS_EXEC)
492 if (prot
& VM_PROT_READ
)
493 prot
|= VM_PROT_EXECUTE
;
494 if (maxprot
& VM_PROT_READ
)
495 maxprot
|= VM_PROT_EXECUTE
;
500 if (prot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
501 prot
|= VM_PROT_READ
;
502 if (maxprot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
503 maxprot
|= VM_PROT_READ
;
504 #endif /* radar 3777787 */
506 result
= vm_map_enter_mem_object(user_map
,
507 &user_addr
, user_size
,
509 IPC_PORT_NULL
, 0, FALSE
,
511 (flags
& MAP_SHARED
) ?
514 if (result
!= KERN_SUCCESS
)
517 pager
= (vm_pager_t
)ubc_getpager(vp
);
527 * FIXME: if we're writing the file we need a way to
528 * ensure that someone doesn't replace our R/W creds
529 * with ones that only work for read.
532 ubc_setthreadcred(vp
, p
, current_thread());
534 if ((flags
& (MAP_ANON
|MAP_SHARED
)) == 0) {
540 #if defined(VM_PROT_READ_IS_EXEC)
541 if (prot
& VM_PROT_READ
)
542 prot
|= VM_PROT_EXECUTE
;
543 if (maxprot
& VM_PROT_READ
)
544 maxprot
|= VM_PROT_EXECUTE
;
549 if (prot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
550 prot
|= VM_PROT_READ
;
551 if (maxprot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
552 maxprot
|= VM_PROT_READ
;
553 #endif /* radar 3777787 */
555 result
= vm_map_enter_mem_object(user_map
,
556 &user_addr
, user_size
,
558 (ipc_port_t
)pager
, file_pos
,
559 docow
, prot
, maxprot
,
560 (flags
& MAP_SHARED
) ?
564 if (result
!= KERN_SUCCESS
) {
576 *retval
= user_addr
+ pageoff
;
579 case KERN_INVALID_ADDRESS
:
583 case KERN_PROTECTION_FAILURE
:
592 fp_drop(p
, fd
, fp
, 0);
594 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);
595 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO2
, SYS_mmap
) | DBG_FUNC_NONE
), (uint32_t)(*retval
>> 32), (uint32_t)(user_size
>> 32),
596 (uint32_t)(file_pos
>> 32), (uint32_t)file_pos
, 0);
602 msync(__unused proc_t p
, struct msync_args
*uap
, register_t
*retval
)
604 __pthread_testcancel(1);
605 return(msync_nocancel(p
, (struct msync_nocancel_args
*)uap
, retval
));
609 msync_nocancel(__unused proc_t p
, struct msync_nocancel_args
*uap
, __unused register_t
*retval
)
611 mach_vm_offset_t addr
;
616 vm_sync_t sync_flags
=0;
618 addr
= (mach_vm_offset_t
) uap
->addr
;
619 size
= (mach_vm_size_t
)uap
->len
;
621 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);
623 if (addr
& PAGE_MASK_64
) {
624 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
629 * We cannot support this properly without maintaining
630 * list all mmaps done. Cannot use vm_map_entry as they could be
631 * split or coalesced by indepenedant actions. So instead of
632 * inaccurate results, lets just return error as invalid size
635 return (EINVAL
); /* XXX breaks posix apps */
639 /* disallow contradictory flags */
640 if ((flags
& (MS_SYNC
|MS_ASYNC
)) == (MS_SYNC
|MS_ASYNC
))
643 if (flags
& MS_KILLPAGES
)
644 sync_flags
|= VM_SYNC_KILLPAGES
;
645 if (flags
& MS_DEACTIVATE
)
646 sync_flags
|= VM_SYNC_DEACTIVATE
;
647 if (flags
& MS_INVALIDATE
)
648 sync_flags
|= VM_SYNC_INVALIDATE
;
650 if ( !(flags
& (MS_KILLPAGES
| MS_DEACTIVATE
))) {
651 if (flags
& MS_ASYNC
)
652 sync_flags
|= VM_SYNC_ASYNCHRONOUS
;
654 sync_flags
|= VM_SYNC_SYNCHRONOUS
;
657 sync_flags
|= VM_SYNC_CONTIGUOUS
; /* complain if holes */
659 user_map
= current_map();
660 rv
= mach_vm_msync(user_map
, addr
, size
, sync_flags
);
665 case KERN_INVALID_ADDRESS
: /* hole in region being sync'ed */
679 /* Not yet implemented */
684 munmap(__unused proc_t p
, struct munmap_args
*uap
, __unused register_t
*retval
)
686 mach_vm_offset_t user_addr
;
687 mach_vm_size_t user_size
;
688 kern_return_t result
;
690 user_addr
= (mach_vm_offset_t
) uap
->addr
;
691 user_size
= (mach_vm_size_t
) uap
->len
;
693 AUDIT_ARG(addr
, user_addr
);
694 AUDIT_ARG(len
, user_size
);
696 if (user_addr
& PAGE_MASK_64
) {
697 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
701 if (user_addr
+ user_size
< user_addr
)
704 if (user_size
== 0) {
705 /* UNIX SPEC: size is 0, return EINVAL */
709 result
= mach_vm_deallocate(current_map(), user_addr
, user_size
);
710 if (result
!= KERN_SUCCESS
) {
717 mprotect(__unused proc_t p
, struct mprotect_args
*uap
, __unused register_t
*retval
)
719 register vm_prot_t prot
;
720 mach_vm_offset_t user_addr
;
721 mach_vm_size_t user_size
;
722 kern_return_t result
;
728 AUDIT_ARG(addr
, uap
->addr
);
729 AUDIT_ARG(len
, uap
->len
);
730 AUDIT_ARG(value
, uap
->prot
);
732 user_addr
= (mach_vm_offset_t
) uap
->addr
;
733 user_size
= (mach_vm_size_t
) uap
->len
;
734 prot
= (vm_prot_t
)(uap
->prot
& VM_PROT_ALL
);
736 if (user_addr
& PAGE_MASK_64
) {
737 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
743 #if defined(VM_PROT_READ_IS_EXEC)
744 if (prot
& VM_PROT_READ
)
745 prot
|= VM_PROT_EXECUTE
;
750 if (prot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
751 prot
|= VM_PROT_READ
;
754 user_map
= current_map();
758 * The MAC check for mprotect is of limited use for 2 reasons:
759 * Without mmap revocation, the caller could have asked for the max
760 * protections initially instead of a reduced set, so a mprotect
761 * check would offer no new security.
762 * It is not possible to extract the vnode from the pager object(s)
763 * of the target memory range.
764 * However, the MAC check may be used to prevent a process from,
765 * e.g., making the stack executable.
767 error
= mac_proc_check_mprotect(p
, user_addr
,
772 result
= mach_vm_protect(user_map
, user_addr
, user_size
,
777 case KERN_PROTECTION_FAILURE
:
779 case KERN_INVALID_ADDRESS
:
780 /* UNIX SPEC: for an invalid address range, return ENOMEM */
788 minherit(__unused proc_t p
, struct minherit_args
*uap
, __unused register_t
*retval
)
790 mach_vm_offset_t addr
;
792 register vm_inherit_t inherit
;
794 kern_return_t result
;
796 AUDIT_ARG(addr
, uap
->addr
);
797 AUDIT_ARG(len
, uap
->len
);
798 AUDIT_ARG(value
, uap
->inherit
);
800 addr
= (mach_vm_offset_t
)uap
->addr
;
801 size
= (mach_vm_size_t
)uap
->len
;
802 inherit
= uap
->inherit
;
804 user_map
= current_map();
805 result
= mach_vm_inherit(user_map
, addr
, size
,
810 case KERN_PROTECTION_FAILURE
:
817 madvise(__unused proc_t p
, struct madvise_args
*uap
, __unused register_t
*retval
)
820 mach_vm_offset_t start
;
822 vm_behavior_t new_behavior
;
823 kern_return_t result
;
826 * Since this routine is only advisory, we default to conservative
829 switch (uap
->behav
) {
831 new_behavior
= VM_BEHAVIOR_RANDOM
;
833 case MADV_SEQUENTIAL
:
834 new_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
837 new_behavior
= VM_BEHAVIOR_DEFAULT
;
840 new_behavior
= VM_BEHAVIOR_WILLNEED
;
843 new_behavior
= VM_BEHAVIOR_DONTNEED
;
849 start
= (mach_vm_offset_t
) uap
->addr
;
850 size
= (mach_vm_size_t
) uap
->len
;
852 user_map
= current_map();
854 result
= mach_vm_behavior_set(user_map
, start
, size
, new_behavior
);
858 case KERN_INVALID_ADDRESS
:
866 mincore(__unused proc_t p
, struct mincore_args
*uap
, __unused register_t
*retval
)
868 mach_vm_offset_t addr
, first_addr
, end
;
872 int vecindex
, lastvecindex
;
883 * Make sure that the addresses presented are valid for user
886 first_addr
= addr
= mach_vm_trunc_page(uap
->addr
);
887 end
= addr
+ mach_vm_round_page(uap
->len
);
893 * Address of byte vector
900 * Do this on a map entry basis so that if the pages are not
901 * in the current processes address space, we can easily look
902 * up the pages elsewhere.
905 for( ; addr
< end
; addr
+= PAGE_SIZE
) {
907 ret
= mach_vm_page_query(map
, addr
, &pqueryinfo
, &numref
);
908 if (ret
!= KERN_SUCCESS
)
911 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_PRESENT
)
912 mincoreinfo
|= MINCORE_INCORE
;
913 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_REF
)
914 mincoreinfo
|= MINCORE_REFERENCED
;
915 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_DIRTY
)
916 mincoreinfo
|= MINCORE_MODIFIED
;
920 * calculate index into user supplied byte vector
922 vecindex
= (addr
- first_addr
)>> PAGE_SHIFT
;
925 * If we have skipped map entries, we need to make sure that
926 * the byte vector is zeroed for those skipped entries.
928 while((lastvecindex
+ 1) < vecindex
) {
930 error
= copyout(&c
, vec
+ lastvecindex
, 1);
938 * Pass the page information to the user
940 c
= (char)mincoreinfo
;
941 error
= copyout(&c
, vec
+ vecindex
, 1);
945 lastvecindex
= vecindex
;
950 * Zero the last entries in the byte vector.
952 vecindex
= (end
- first_addr
) >> PAGE_SHIFT
;
953 while((lastvecindex
+ 1) < vecindex
) {
955 error
= copyout(&c
, vec
+ lastvecindex
, 1);
966 mlock(__unused proc_t p
, struct mlock_args
*uap
, __unused register_t
*retvalval
)
969 vm_map_offset_t addr
;
970 vm_map_size_t size
, pageoff
;
971 kern_return_t result
;
973 AUDIT_ARG(addr
, uap
->addr
);
974 AUDIT_ARG(len
, uap
->len
);
976 addr
= (vm_map_offset_t
) uap
->addr
;
977 size
= (vm_map_size_t
)uap
->len
;
979 /* disable wrap around */
980 if (addr
+ size
< addr
)
986 pageoff
= (addr
& PAGE_MASK
);
988 size
= vm_map_round_page(size
+pageoff
);
989 user_map
= current_map();
991 /* have to call vm_map_wire directly to pass "I don't know" protections */
992 result
= vm_map_wire(user_map
, addr
, addr
+size
, VM_PROT_NONE
, TRUE
);
994 if (result
== KERN_RESOURCE_SHORTAGE
)
996 else if (result
!= KERN_SUCCESS
)
999 return 0; /* KERN_SUCCESS */
1003 munlock(__unused proc_t p
, struct munlock_args
*uap
, __unused register_t
*retval
)
1005 mach_vm_offset_t addr
;
1006 mach_vm_size_t size
;
1008 kern_return_t result
;
1010 AUDIT_ARG(addr
, uap
->addr
);
1011 AUDIT_ARG(addr
, uap
->len
);
1013 addr
= (mach_vm_offset_t
) uap
->addr
;
1014 size
= (mach_vm_size_t
)uap
->len
;
1015 user_map
= current_map();
1017 /* JMM - need to remove all wirings by spec - this just removes one */
1018 result
= mach_vm_wire(host_priv_self(), user_map
, addr
, size
, VM_PROT_NONE
);
1019 return (result
== KERN_SUCCESS
? 0 : ENOMEM
);
1024 mlockall(__unused proc_t p
, __unused
struct mlockall_args
*uap
, __unused register_t
*retval
)
1030 munlockall(__unused proc_t p
, __unused
struct munlockall_args
*uap
, __unused register_t
*retval
)
1038 obreak(__unused proc_t p
, __unused
struct obreak_args
*uap
, __unused register_t
*retval
)
1040 /* Not implemented, obsolete */
1047 ovadvise(__unused proc_t p
, __unused
struct ovadvise_args
*uap
, __unused register_t
*retval
)
1057 /* USV: No! need to obsolete map_fd()! mmap() already supports 64 bits */
1059 map_fd(struct map_fd_args
*args
)
1062 vm_offset_t offset
= args
->offset
;
1063 vm_offset_t
*va
= args
->va
;
1064 boolean_t findspace
= args
->findspace
;
1065 vm_size_t size
= args
->size
;
1068 AUDIT_MACH_SYSCALL_ENTER(AUE_MAPFD
);
1069 AUDIT_ARG(addr
, CAST_DOWN(user_addr_t
, va
));
1072 ret
= map_fd_funneled( fd
, (vm_object_offset_t
)offset
, va
, findspace
, size
);
1074 AUDIT_MACH_SYSCALL_EXIT(ret
);
1081 vm_object_offset_t offset
,
1083 boolean_t findspace
,
1086 kern_return_t result
;
1087 struct fileproc
*fp
;
1090 vm_offset_t map_addr
=0;
1094 proc_t p
= current_proc();
1095 struct vnode_attr vattr
;
1098 * Find the inode; verify that it's a regular file.
1101 err
= fp_lookup(p
, fd
, &fp
, 0);
1105 if (fp
->f_fglob
->fg_type
!= DTYPE_VNODE
){
1106 err
= KERN_INVALID_ARGUMENT
;
1110 if (!(fp
->f_fglob
->fg_flag
& FREAD
)) {
1111 err
= KERN_PROTECTION_FAILURE
;
1115 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1116 err
= vnode_getwithref(vp
);
1120 if (vp
->v_type
!= VREG
) {
1121 (void)vnode_put(vp
);
1122 err
= KERN_INVALID_ARGUMENT
;
1126 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
1129 * POSIX: mmap needs to update access time for mapped files
1131 if ((vnode_vfsvisflags(vp
) & MNT_NOATIME
) == 0) {
1133 nanotime(&vattr
.va_access_time
);
1134 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
1135 vnode_setattr(vp
, &vattr
, vfs_context_current());
1138 if (offset
& PAGE_MASK_64
) {
1139 printf("map_fd: file offset not page aligned(%d : %s)\n",p
->p_pid
, p
->p_comm
);
1140 (void)vnode_put(vp
);
1141 err
= KERN_INVALID_ARGUMENT
;
1144 map_size
= round_page(size
);
1147 * Allow user to map in a zero length file.
1150 (void)vnode_put(vp
);
1157 pager
= (void *)ubc_getpager(vp
);
1158 if (pager
== NULL
) {
1159 (void)vnode_put(vp
);
1165 my_map
= current_map();
1169 &map_addr
, map_size
, (vm_offset_t
)0,
1170 VM_FLAGS_ANYWHERE
, pager
, offset
, TRUE
,
1171 VM_PROT_DEFAULT
, VM_PROT_ALL
,
1172 VM_INHERIT_DEFAULT
);
1173 if (result
!= KERN_SUCCESS
) {
1174 (void)vnode_put(vp
);
1181 vm_offset_t dst_addr
;
1184 if (copyin(CAST_USER_ADDR_T(va
), &dst_addr
, sizeof (dst_addr
)) ||
1185 trunc_page_32(dst_addr
) != dst_addr
) {
1186 (void) vm_map_remove(
1188 map_addr
, map_addr
+ map_size
,
1190 (void)vnode_put(vp
);
1191 err
= KERN_INVALID_ADDRESS
;
1195 result
= vm_map_copyin(my_map
, (vm_map_address_t
)map_addr
,
1196 (vm_map_size_t
)map_size
, TRUE
, &tmp
);
1197 if (result
!= KERN_SUCCESS
) {
1199 (void) vm_map_remove(my_map
, vm_map_trunc_page(map_addr
),
1200 vm_map_round_page(map_addr
+ map_size
),
1202 (void)vnode_put(vp
);
1207 result
= vm_map_copy_overwrite(my_map
,
1208 (vm_map_address_t
)dst_addr
, tmp
, FALSE
);
1209 if (result
!= KERN_SUCCESS
) {
1210 vm_map_copy_discard(tmp
);
1211 (void)vnode_put(vp
);
1216 if (copyout(&map_addr
, CAST_USER_ADDR_T(va
), sizeof (map_addr
))) {
1217 (void) vm_map_remove(my_map
, vm_map_trunc_page(map_addr
),
1218 vm_map_round_page(map_addr
+ map_size
),
1220 (void)vnode_put(vp
);
1221 err
= KERN_INVALID_ADDRESS
;
1226 ubc_setthreadcred(vp
, current_proc(), current_thread());
1227 (void)vnode_put(vp
);
1230 fp_drop(p
, fd
, fp
, 0);