2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1988 University of Utah.
25 * Copyright (c) 1991, 1993
26 * The Regents of the University of California. All rights reserved.
28 * This code is derived from software contributed to Berkeley by
29 * the Systems Programming Group of the University of Utah Computer
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
62 * @(#)vm_mmap.c 8.10 (Berkeley) 2/19/95
66 * Mapped file (mmap) interface to VM
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/filedesc.h>
72 #include <sys/proc_internal.h>
73 #include <sys/kauth.h>
74 #include <sys/resourcevar.h>
75 #include <sys/vnode_internal.h>
78 #include <sys/file_internal.h>
79 #include <sys/vadvise.h>
80 #include <sys/trace.h>
85 #include <sys/sysproto.h>
87 #include <bsm/audit_kernel.h>
88 #include <bsm/audit_kevents.h>
90 #include <mach/mach_types.h>
91 #include <mach/mach_traps.h>
92 #include <mach/vm_sync.h>
93 #include <mach/vm_behavior.h>
94 #include <mach/vm_inherit.h>
95 #include <mach/vm_statistics.h>
96 #include <mach/mach_vm.h>
97 #include <mach/vm_map.h>
98 #include <mach/host_priv.h>
100 #include <kern/cpu_number.h>
101 #include <kern/host.h>
103 #include <vm/vm_map.h>
104 #include <vm/vm_kern.h>
105 #include <vm/vm_pager.h>
108 sbrk(__unused
struct proc
*p
, __unused
struct sbrk_args
*uap
, __unused register_t
*retval
)
110 /* Not yet implemented */
115 sstk(__unused
struct proc
*p
, __unused
struct sstk_args
*uap
, __unused register_t
*retval
)
117 /* Not yet implemented */
134 register struct osmmap_args
*uap
,
137 struct mmap_args newargs
;
141 if ((uap
->share
== MAP_SHARED
)|| (uap
->share
== MAP_PRIVATE
)) {
142 newargs
.addr
= CAST_USER_ADDR_T(uap
->addr
);
143 newargs
.len
= CAST_USER_ADDR_T(uap
->len
);
144 newargs
.prot
= uap
->prot
;
145 newargs
.flags
= uap
->share
;
146 newargs
.fd
= uap
->fd
;
147 newargs
.pos
= (off_t
)uap
->pos
;
148 ret
= mmap(curp
, &newargs
, &addr
);
150 *retval
= CAST_DOWN(register_t
, addr
);
158 mmap(struct proc
*p
, struct mmap_args
*uap
, user_addr_t
*retval
)
161 * Map in special device (must be SHARED) or file
164 register struct vnode
*vp
;
169 kern_return_t result
;
170 mach_vm_offset_t user_addr
;
171 mach_vm_size_t user_size
;
172 vm_object_offset_t pageoff
;
173 vm_object_offset_t file_pos
;
184 user_addr
= (mach_vm_offset_t
)uap
->addr
;
185 user_size
= (mach_vm_size_t
) uap
->len
;
187 AUDIT_ARG(addr
, user_addr
);
188 AUDIT_ARG(len
, user_size
);
189 AUDIT_ARG(fd
, uap
->fd
);
191 prot
= (uap
->prot
& VM_PROT_ALL
);
196 * The vm code does not have prototypes & compiler doesn't do the'
197 * the right thing when you cast 64bit value and pass it in function
198 * call. So here it is.
200 file_pos
= (vm_object_offset_t
)uap
->pos
;
203 /* make sure mapping fits into numeric range etc */
204 if ((file_pos
+ user_size
> (vm_object_offset_t
)-PAGE_SIZE_64
) ||
205 ((flags
& MAP_ANON
) && fd
!= -1))
209 * Align the file position to a page boundary,
210 * and save its page offset component.
212 pageoff
= (file_pos
& PAGE_MASK
);
213 file_pos
-= (vm_object_offset_t
)pageoff
;
216 /* Adjust size for rounding (on both ends). */
217 user_size
+= pageoff
; /* low end... */
218 user_size
= mach_vm_round_page(user_size
); /* hi end */
222 * Check for illegal addresses. Watch out for address wrap... Note
223 * that VM_*_ADDRESS are not constants due to casts (argh).
225 if (flags
& MAP_FIXED
) {
227 * The specified address must have the same remainder
228 * as the file offset taken modulo PAGE_SIZE, so it
229 * should be aligned after adjustment by pageoff.
231 user_addr
-= pageoff
;
232 if (user_addr
& PAGE_MASK
)
236 /* DO not have apis to get this info, need to wait till then*/
238 * XXX for non-fixed mappings where no hint is provided or
239 * the hint would fall in the potential heap space,
240 * place it after the end of the largest possible heap.
242 * There should really be a pmap call to determine a reasonable
245 else if (addr
< mach_vm_round_page(p
->p_vmspace
->vm_daddr
+ MAXDSIZ
))
246 addr
= mach_vm_round_page(p
->p_vmspace
->vm_daddr
+ MAXDSIZ
);
251 if (flags
& MAP_ANON
) {
253 * Mapping blank space is trivial.
256 maxprot
= VM_PROT_ALL
;
260 struct vnode_attr va
;
261 struct vfs_context context
;
263 * Mapping file, get fp for validation. Obtain vnode and make
264 * sure it is of appropriate type.
266 err
= fp_lookup(p
, fd
, &fp
, 0);
270 if(fp
->f_fglob
->fg_type
== DTYPE_PSXSHM
) {
271 uap
->addr
= (user_addr_t
)user_addr
;
272 uap
->len
= (user_size_t
)user_size
;
276 error
= pshm_mmap(p
, uap
, retval
, fp
, (off_t
)pageoff
);
280 if (fp
->f_fglob
->fg_type
!= DTYPE_VNODE
) {
284 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
285 error
= vnode_getwithref(vp
);
289 if (vp
->v_type
!= VREG
&& vp
->v_type
!= VCHR
) {
295 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
297 /* conformance change - mmap needs to update access time for mapped
301 nanotime(&va
.va_access_time
);
302 VATTR_SET_ACTIVE(&va
, va_access_time
);
304 context
.vc_ucred
= kauth_cred_get();
305 vnode_setattr(vp
, &va
, &context
);
308 * XXX hack to handle use of /dev/zero to map anon memory (ala
311 if (vp
->v_type
== VCHR
|| vp
->v_type
== VSTR
) {
317 * Ensure that file and memory protections are
318 * compatible. Note that we only worry about
319 * writability if mapping is shared; in this case,
320 * current and max prot are dictated by the open file.
321 * XXX use the vnode instead? Problem is: what
322 * credentials do we use for determination? What if
323 * proc does a setuid?
325 maxprot
= VM_PROT_EXECUTE
; /* ??? */
326 if (fp
->f_fglob
->fg_flag
& FREAD
)
327 maxprot
|= VM_PROT_READ
;
328 else if (prot
& PROT_READ
) {
334 * If we are sharing potential changes (either via
335 * MAP_SHARED or via the implicit sharing of character
336 * device mappings), and we are trying to get write
337 * permission although we opened it without asking
341 if ((flags
& MAP_SHARED
) != 0) {
342 if ((fp
->f_fglob
->fg_flag
& FWRITE
) != 0) {
344 * check for write access
346 * Note that we already made this check when granting FWRITE
347 * against the file, so it seems redundant here.
349 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_CHECKIMMUTABLE
, &context
);
351 /* if not granted for any reason, but we wanted it, bad */
352 if ((prot
& PROT_WRITE
) && (error
!= 0)) {
357 /* if writable, remember */
359 maxprot
|= VM_PROT_WRITE
;
361 } else if ((prot
& PROT_WRITE
) != 0) {
367 maxprot
|= VM_PROT_WRITE
;
373 if (user_size
== 0) {
381 * We bend a little - round the start and end addresses
382 * to the nearest page boundary.
384 user_size
= mach_vm_round_page(user_size
);
386 if (file_pos
& PAGE_MASK_64
) {
393 user_map
= current_map();
395 if ((flags
& MAP_FIXED
) == 0) {
396 alloc_flags
= VM_FLAGS_ANYWHERE
;
397 user_addr
= mach_vm_round_page(user_addr
);
399 if (user_addr
!= mach_vm_trunc_page(user_addr
)) {
406 * mmap(MAP_FIXED) will replace any existing mappings in the
407 * specified range, if the new mapping is successful.
408 * If we just deallocate the specified address range here,
409 * another thread might jump in and allocate memory in that
410 * range before we get a chance to establish the new mapping,
411 * and we won't have a chance to restore the old mappings.
412 * So we use VM_FLAGS_OVERWRITE to let Mach VM know that it
413 * has to deallocate the existing mappings and establish the
414 * new ones atomically.
416 alloc_flags
= VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
;
421 * Lookup/allocate object.
423 if (handle
== NULL
) {
427 #if defined(VM_PROT_READ_IS_EXEC)
428 if (prot
& VM_PROT_READ
)
429 prot
|= VM_PROT_EXECUTE
;
431 if (maxprot
& VM_PROT_READ
)
432 maxprot
|= VM_PROT_EXECUTE
;
435 result
= mach_vm_map(user_map
, &user_addr
, user_size
, 0,
436 alloc_flags
, IPC_PORT_NULL
, 0,
437 FALSE
, prot
, maxprot
,
438 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
:
440 if (result
!= KERN_SUCCESS
)
443 UBCINFOCHECK("mmap", vp
);
444 pager
= (vm_pager_t
)ubc_getpager(vp
);
454 * FIXME: if we're writing the file we need a way to
455 * ensure that someone doesn't replace our R/W creds
456 * with ones that only work for read.
461 if ((flags
& (MAP_ANON
|MAP_SHARED
)) == 0) {
467 #if defined(VM_PROT_READ_IS_EXEC)
468 if (prot
& VM_PROT_READ
)
469 prot
|= VM_PROT_EXECUTE
;
471 if (maxprot
& VM_PROT_READ
)
472 maxprot
|= VM_PROT_EXECUTE
;
476 result
= mach_vm_map(user_map
, &user_addr
, user_size
,
477 0, alloc_flags
, (ipc_port_t
)pager
, file_pos
,
478 docow
, prot
, maxprot
,
479 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
:
482 if (result
!= KERN_SUCCESS
) {
487 (void)ubc_map(vp
,(prot
& ( PROT_EXEC
| PROT_READ
| PROT_WRITE
| PROT_EXEC
)));
496 *retval
= user_addr
+ pageoff
;
499 case KERN_INVALID_ADDRESS
:
503 case KERN_PROTECTION_FAILURE
:
512 fp_drop(p
, fd
, fp
, 0);
517 msync(__unused
struct proc
*p
, struct msync_args
*uap
, __unused register_t
*retval
)
519 mach_vm_offset_t addr
;
524 vm_sync_t sync_flags
=0;
526 addr
= (mach_vm_offset_t
) uap
->addr
;
527 size
= (mach_vm_size_t
)uap
->len
;
529 if (addr
& PAGE_MASK_64
) {
530 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
535 * We cannot support this properly without maintaining
536 * list all mmaps done. Cannot use vm_map_entry as they could be
537 * split or coalesced by indepenedant actions. So instead of
538 * inaccurate results, lets just return error as invalid size
541 return (EINVAL
); /* XXX breaks posix apps */
545 /* disallow contradictory flags */
546 if ((flags
& (MS_SYNC
|MS_ASYNC
)) == (MS_SYNC
|MS_ASYNC
) ||
547 (flags
& (MS_ASYNC
|MS_INVALIDATE
)) == (MS_ASYNC
|MS_INVALIDATE
))
550 if (flags
& MS_KILLPAGES
)
551 sync_flags
|= VM_SYNC_KILLPAGES
;
552 if (flags
& MS_DEACTIVATE
)
553 sync_flags
|= VM_SYNC_DEACTIVATE
;
554 if (flags
& MS_INVALIDATE
)
555 sync_flags
|= VM_SYNC_INVALIDATE
;
557 if ( !(flags
& (MS_KILLPAGES
| MS_DEACTIVATE
))) {
558 if (flags
& MS_ASYNC
)
559 sync_flags
|= VM_SYNC_ASYNCHRONOUS
;
561 sync_flags
|= VM_SYNC_SYNCHRONOUS
;
564 sync_flags
|= VM_SYNC_CONTIGUOUS
; /* complain if holes */
566 user_map
= current_map();
567 rv
= mach_vm_msync(user_map
, addr
, size
, sync_flags
);
572 case KERN_INVALID_ADDRESS
: /* hole in region being sync'ed */
586 /* Not yet implemented */
591 munmap(__unused
struct proc
*p
, struct munmap_args
*uap
, __unused register_t
*retval
)
593 mach_vm_offset_t user_addr
;
594 mach_vm_size_t user_size
;
595 kern_return_t result
;
597 user_addr
= (mach_vm_offset_t
) uap
->addr
;
598 user_size
= (mach_vm_size_t
) uap
->len
;
600 AUDIT_ARG(addr
, user_addr
);
601 AUDIT_ARG(len
, user_size
);
603 if (user_addr
& PAGE_MASK_64
) {
604 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
608 if (user_addr
+ user_size
< user_addr
)
611 if (user_size
== 0) {
612 /* UNIX SPEC: size is 0, return EINVAL */
616 result
= mach_vm_deallocate(current_map(), user_addr
, user_size
);
617 if (result
!= KERN_SUCCESS
) {
624 mprotect(__unused
struct proc
*p
, struct mprotect_args
*uap
, __unused register_t
*retval
)
626 register vm_prot_t prot
;
627 mach_vm_offset_t user_addr
;
628 mach_vm_size_t user_size
;
629 kern_return_t result
;
632 AUDIT_ARG(addr
, uap
->addr
);
633 AUDIT_ARG(len
, uap
->len
);
634 AUDIT_ARG(value
, uap
->prot
);
636 user_addr
= (mach_vm_offset_t
) uap
->addr
;
637 user_size
= (mach_vm_size_t
) uap
->len
;
638 prot
= (vm_prot_t
)(uap
->prot
& VM_PROT_ALL
);
640 if (user_addr
& PAGE_MASK_64
) {
641 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
647 #if defined(VM_PROT_READ_IS_EXEC)
648 if (prot
& VM_PROT_READ
)
649 prot
|= VM_PROT_EXECUTE
;
653 user_map
= current_map();
655 result
= mach_vm_protect(user_map
, user_addr
, user_size
,
660 case KERN_PROTECTION_FAILURE
:
662 case KERN_INVALID_ADDRESS
:
663 /* UNIX SPEC: for an invalid address range, return ENOMEM */
671 minherit(__unused
struct proc
*p
, struct minherit_args
*uap
, __unused register_t
*retval
)
673 mach_vm_offset_t addr
;
675 register vm_inherit_t inherit
;
677 kern_return_t result
;
679 AUDIT_ARG(addr
, uap
->addr
);
680 AUDIT_ARG(len
, uap
->len
);
681 AUDIT_ARG(value
, uap
->inherit
);
683 addr
= (mach_vm_offset_t
)uap
->addr
;
684 size
= (mach_vm_size_t
)uap
->len
;
685 inherit
= uap
->inherit
;
687 user_map
= current_map();
688 result
= mach_vm_inherit(user_map
, addr
, size
,
693 case KERN_PROTECTION_FAILURE
:
700 madvise(__unused
struct proc
*p
, struct madvise_args
*uap
, __unused register_t
*retval
)
703 mach_vm_offset_t start
;
705 vm_behavior_t new_behavior
;
706 kern_return_t result
;
709 * Since this routine is only advisory, we default to conservative
712 switch (uap
->behav
) {
714 new_behavior
= VM_BEHAVIOR_RANDOM
;
716 case MADV_SEQUENTIAL
:
717 new_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
720 new_behavior
= VM_BEHAVIOR_DEFAULT
;
723 new_behavior
= VM_BEHAVIOR_WILLNEED
;
726 new_behavior
= VM_BEHAVIOR_DONTNEED
;
732 start
= (mach_vm_offset_t
) uap
->addr
;
733 size
= (mach_vm_size_t
) uap
->len
;
735 user_map
= current_map();
737 result
= mach_vm_behavior_set(user_map
, start
, size
, new_behavior
);
741 case KERN_INVALID_ADDRESS
:
749 mincore(__unused
struct proc
*p
, struct mincore_args
*uap
, __unused register_t
*retval
)
751 mach_vm_offset_t addr
, first_addr
, end
;
755 int vecindex
, lastvecindex
;
766 * Make sure that the addresses presented are valid for user
769 first_addr
= addr
= mach_vm_trunc_page(uap
->addr
);
770 end
= addr
+ mach_vm_round_page(uap
->len
);
776 * Address of byte vector
783 * Do this on a map entry basis so that if the pages are not
784 * in the current processes address space, we can easily look
785 * up the pages elsewhere.
788 for( ; addr
< end
; addr
+= PAGE_SIZE
) {
790 ret
= vm_map_page_query(map
, addr
, &pqueryinfo
, &numref
);
791 if (ret
!= KERN_SUCCESS
)
794 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_PRESENT
)
795 mincoreinfo
|= MINCORE_INCORE
;
796 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_REF
)
797 mincoreinfo
|= MINCORE_REFERENCED
;
798 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_DIRTY
)
799 mincoreinfo
|= MINCORE_MODIFIED
;
803 * calculate index into user supplied byte vector
805 vecindex
= (addr
- first_addr
)>> PAGE_SHIFT
;
808 * If we have skipped map entries, we need to make sure that
809 * the byte vector is zeroed for those skipped entries.
811 while((lastvecindex
+ 1) < vecindex
) {
813 error
= copyout(&c
, vec
+ lastvecindex
, 1);
821 * Pass the page information to the user
823 c
= (char)mincoreinfo
;
824 error
= copyout(&c
, vec
+ vecindex
, 1);
828 lastvecindex
= vecindex
;
833 * Zero the last entries in the byte vector.
835 vecindex
= (end
- first_addr
) >> PAGE_SHIFT
;
836 while((lastvecindex
+ 1) < vecindex
) {
838 error
= copyout(&c
, vec
+ lastvecindex
, 1);
849 mlock(__unused
struct proc
*p
, struct mlock_args
*uap
, __unused register_t
*retvalval
)
852 vm_map_offset_t addr
;
853 vm_map_size_t size
, pageoff
;
854 kern_return_t result
;
856 AUDIT_ARG(addr
, uap
->addr
);
857 AUDIT_ARG(len
, uap
->len
);
859 addr
= (vm_map_offset_t
) uap
->addr
;
860 size
= (vm_map_size_t
)uap
->len
;
862 /* disable wrap around */
863 if (addr
+ size
< addr
)
869 pageoff
= (addr
& PAGE_MASK
);
871 size
= vm_map_round_page(size
+pageoff
);
874 /* Hmm.. What am I going to do with this? */
875 if (atop(size
) + cnt
.v_wire_count
> vm_page_max_wired
)
877 #ifdef pmap_wired_count
878 if (size
+ ptoa(pmap_wired_count(vm_map_pmap(&p
->p_vmspace
->vm_map
))) >
879 p
->p_rlimit
[RLIMIT_MEMLOCK
].rlim_cur
)
882 error
= suser(kauth_cred_get(), &p
->p_acflag
);
888 user_map
= current_map();
890 /* have to call vm_map_wire directly to pass "I don't know" protections */
891 result
= vm_map_wire(user_map
, addr
, addr
+size
, VM_PROT_NONE
, TRUE
);
892 return (result
== KERN_SUCCESS
? 0 : ENOMEM
);
896 munlock(__unused
struct proc
*p
, struct munlock_args
*uap
, __unused register_t
*retval
)
898 mach_vm_offset_t addr
;
901 kern_return_t result
;
903 AUDIT_ARG(addr
, uap
->addr
);
904 AUDIT_ARG(addr
, uap
->len
);
906 addr
= (mach_vm_offset_t
) uap
->addr
;
907 size
= (mach_vm_size_t
)uap
->len
;
911 /* Hmm.. What am I going to do with this? */
912 #ifndef pmap_wired_count
913 error
= suser(kauth_cred_get(), &p
->p_acflag
);
919 user_map
= current_map();
921 /* JMM - need to remove all wirings by spec - this just removes one */
922 result
= mach_vm_wire(host_priv_self(), user_map
, addr
, size
, VM_PROT_NONE
);
923 return (result
== KERN_SUCCESS
? 0 : ENOMEM
);
928 mlockall(__unused
struct proc
*p
, __unused
struct mlockall_args
*uap
, __unused register_t
*retval
)
934 munlockall(__unused
struct proc
*p
, __unused
struct munlockall_args
*uap
, __unused register_t
*retval
)
942 obreak(__unused
struct proc
*p
, __unused
struct obreak_args
*uap
, __unused register_t
*retval
)
944 /* Not implemented, obsolete */
951 ovadvise(__unused
struct proc
*p
, __unused
struct ovadvise_args
*uap
, __unused register_t
*retval
)
961 /* USV: No! need to obsolete map_fd()! mmap() already supports 64 bits */
963 map_fd(struct map_fd_args
*args
)
966 vm_offset_t offset
= args
->offset
;
967 vm_offset_t
*va
= args
->va
;
968 boolean_t findspace
= args
->findspace
;
969 vm_size_t size
= args
->size
;
972 AUDIT_MACH_SYSCALL_ENTER(AUE_MAPFD
);
973 AUDIT_ARG(addr
, CAST_DOWN(user_addr_t
, va
));
976 ret
= map_fd_funneled( fd
, (vm_object_offset_t
)offset
, va
, findspace
, size
);
978 AUDIT_MACH_SYSCALL_EXIT(ret
);
985 vm_object_offset_t offset
,
990 kern_return_t result
;
994 vm_offset_t map_addr
=0;
998 struct proc
*p
=(struct proc
*)current_proc();
999 struct vnode_attr vattr
;
1000 struct vfs_context context
;
1003 * Find the inode; verify that it's a regular file.
1006 err
= fp_lookup(p
, fd
, &fp
, 0);
1010 if (fp
->f_fglob
->fg_type
!= DTYPE_VNODE
){
1011 err
= KERN_INVALID_ARGUMENT
;
1015 if (!(fp
->f_fglob
->fg_flag
& FREAD
)) {
1016 err
= KERN_PROTECTION_FAILURE
;
1020 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1021 err
= vnode_getwithref(vp
);
1025 if (vp
->v_type
!= VREG
) {
1026 (void)vnode_put(vp
);
1027 err
= KERN_INVALID_ARGUMENT
;
1031 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
1033 /* conformance change - mmap needs to update access time for mapped
1037 nanotime(&vattr
.va_access_time
);
1038 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
1039 context
.vc_proc
= p
;
1040 context
.vc_ucred
= kauth_cred_get();
1041 vnode_setattr(vp
, &vattr
, &context
);
1043 if (offset
& PAGE_MASK_64
) {
1044 printf("map_fd: file offset not page aligned(%d : %s)\n",p
->p_pid
, p
->p_comm
);
1045 (void)vnode_put(vp
);
1046 err
= KERN_INVALID_ARGUMENT
;
1049 map_size
= round_page(size
);
1052 * Allow user to map in a zero length file.
1055 (void)vnode_put(vp
);
1062 UBCINFOCHECK("map_fd_funneled", vp
);
1063 pager
= (void *) ubc_getpager(vp
);
1064 if (pager
== NULL
) {
1065 (void)vnode_put(vp
);
1071 my_map
= current_map();
1075 &map_addr
, map_size
, (vm_offset_t
)0,
1076 VM_FLAGS_ANYWHERE
, pager
, offset
, TRUE
,
1077 VM_PROT_DEFAULT
, VM_PROT_ALL
,
1078 VM_INHERIT_DEFAULT
);
1079 if (result
!= KERN_SUCCESS
) {
1080 (void)vnode_put(vp
);
1087 vm_offset_t dst_addr
;
1090 if (copyin(CAST_USER_ADDR_T(va
), &dst_addr
, sizeof (dst_addr
)) ||
1091 trunc_page_32(dst_addr
) != dst_addr
) {
1092 (void) vm_map_remove(
1094 map_addr
, map_addr
+ map_size
,
1096 (void)vnode_put(vp
);
1097 err
= KERN_INVALID_ADDRESS
;
1101 result
= vm_map_copyin(my_map
, (vm_map_address_t
)map_addr
,
1102 (vm_map_size_t
)map_size
, TRUE
, &tmp
);
1103 if (result
!= KERN_SUCCESS
) {
1105 (void) vm_map_remove(my_map
, vm_map_trunc_page(map_addr
),
1106 vm_map_round_page(map_addr
+ map_size
),
1108 (void)vnode_put(vp
);
1113 result
= vm_map_copy_overwrite(my_map
,
1114 (vm_map_address_t
)dst_addr
, tmp
, FALSE
);
1115 if (result
!= KERN_SUCCESS
) {
1116 vm_map_copy_discard(tmp
);
1117 (void)vnode_put(vp
);
1122 if (copyout(&map_addr
, CAST_USER_ADDR_T(va
), sizeof (map_addr
))) {
1123 (void) vm_map_remove(my_map
, vm_map_trunc_page(map_addr
),
1124 vm_map_round_page(map_addr
+ map_size
),
1126 (void)vnode_put(vp
);
1127 err
= KERN_INVALID_ADDRESS
;
1132 ubc_setcred(vp
, current_proc());
1133 (void)ubc_map(vp
, (PROT_READ
| PROT_WRITE
| PROT_EXEC
));
1134 (void)vnode_put(vp
);
1137 fp_drop(p
, fd
, fp
, 0);