2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1988 University of Utah.
24 * Copyright (c) 1991, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * the Systems Programming Group of the University of Utah Computer
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
61 * @(#)vm_mmap.c 8.10 (Berkeley) 2/19/95
65 * Mapped file (mmap) interface to VM
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/filedesc.h>
71 #include <sys/proc_internal.h>
72 #include <sys/kauth.h>
73 #include <sys/resourcevar.h>
74 #include <sys/vnode_internal.h>
77 #include <sys/file_internal.h>
78 #include <sys/vadvise.h>
79 #include <sys/trace.h>
84 #include <sys/sysproto.h>
86 #include <bsm/audit_kernel.h>
87 #include <bsm/audit_kevents.h>
89 #include <mach/mach_types.h>
90 #include <mach/mach_traps.h>
91 #include <mach/vm_sync.h>
92 #include <mach/vm_behavior.h>
93 #include <mach/vm_inherit.h>
94 #include <mach/vm_statistics.h>
95 #include <mach/mach_vm.h>
96 #include <mach/vm_map.h>
97 #include <mach/host_priv.h>
99 #include <kern/cpu_number.h>
100 #include <kern/host.h>
102 #include <vm/vm_map.h>
103 #include <vm/vm_kern.h>
104 #include <vm/vm_pager.h>
107 sbrk(__unused
struct proc
*p
, __unused
struct sbrk_args
*uap
, __unused register_t
*retval
)
109 /* Not yet implemented */
114 sstk(__unused
struct proc
*p
, __unused
struct sstk_args
*uap
, __unused register_t
*retval
)
116 /* Not yet implemented */
133 register struct osmmap_args
*uap
,
136 struct mmap_args newargs
;
140 if ((uap
->share
== MAP_SHARED
)|| (uap
->share
== MAP_PRIVATE
)) {
141 newargs
.addr
= CAST_USER_ADDR_T(uap
->addr
);
142 newargs
.len
= CAST_USER_ADDR_T(uap
->len
);
143 newargs
.prot
= uap
->prot
;
144 newargs
.flags
= uap
->share
;
145 newargs
.fd
= uap
->fd
;
146 newargs
.pos
= (off_t
)uap
->pos
;
147 ret
= mmap(curp
, &newargs
, &addr
);
149 *retval
= CAST_DOWN(register_t
, addr
);
157 mmap(struct proc
*p
, struct mmap_args
*uap
, user_addr_t
*retval
)
160 * Map in special device (must be SHARED) or file
163 register struct vnode
*vp
;
168 kern_return_t result
;
169 mach_vm_offset_t user_addr
;
170 mach_vm_size_t user_size
;
171 vm_object_offset_t pageoff
;
172 vm_object_offset_t file_pos
;
183 user_addr
= (mach_vm_offset_t
)uap
->addr
;
184 user_size
= (mach_vm_size_t
) uap
->len
;
186 AUDIT_ARG(addr
, user_addr
);
187 AUDIT_ARG(len
, user_size
);
188 AUDIT_ARG(fd
, uap
->fd
);
190 prot
= (uap
->prot
& VM_PROT_ALL
);
195 * The vm code does not have prototypes & compiler doesn't do the'
196 * the right thing when you cast 64bit value and pass it in function
197 * call. So here it is.
199 file_pos
= (vm_object_offset_t
)uap
->pos
;
202 /* make sure mapping fits into numeric range etc */
203 if ((file_pos
+ user_size
> (vm_object_offset_t
)-PAGE_SIZE_64
) ||
204 ((flags
& MAP_ANON
) && fd
!= -1))
208 * Align the file position to a page boundary,
209 * and save its page offset component.
211 pageoff
= (file_pos
& PAGE_MASK
);
212 file_pos
-= (vm_object_offset_t
)pageoff
;
215 /* Adjust size for rounding (on both ends). */
216 user_size
+= pageoff
; /* low end... */
217 user_size
= mach_vm_round_page(user_size
); /* hi end */
221 * Check for illegal addresses. Watch out for address wrap... Note
222 * that VM_*_ADDRESS are not constants due to casts (argh).
224 if (flags
& MAP_FIXED
) {
226 * The specified address must have the same remainder
227 * as the file offset taken modulo PAGE_SIZE, so it
228 * should be aligned after adjustment by pageoff.
230 user_addr
-= pageoff
;
231 if (user_addr
& PAGE_MASK
)
235 /* DO not have apis to get this info, need to wait till then*/
237 * XXX for non-fixed mappings where no hint is provided or
238 * the hint would fall in the potential heap space,
239 * place it after the end of the largest possible heap.
241 * There should really be a pmap call to determine a reasonable
244 else if (addr
< mach_vm_round_page(p
->p_vmspace
->vm_daddr
+ MAXDSIZ
))
245 addr
= mach_vm_round_page(p
->p_vmspace
->vm_daddr
+ MAXDSIZ
);
250 if (flags
& MAP_ANON
) {
252 * Mapping blank space is trivial.
255 maxprot
= VM_PROT_ALL
;
259 struct vnode_attr va
;
260 struct vfs_context context
;
262 * Mapping file, get fp for validation. Obtain vnode and make
263 * sure it is of appropriate type.
265 err
= fp_lookup(p
, fd
, &fp
, 0);
269 if(fp
->f_fglob
->fg_type
== DTYPE_PSXSHM
) {
270 uap
->addr
= (user_addr_t
)user_addr
;
271 uap
->len
= (user_size_t
)user_size
;
275 error
= pshm_mmap(p
, uap
, retval
, fp
, (off_t
)pageoff
);
279 if (fp
->f_fglob
->fg_type
!= DTYPE_VNODE
) {
283 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
284 error
= vnode_getwithref(vp
);
288 if (vp
->v_type
!= VREG
&& vp
->v_type
!= VCHR
) {
294 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
296 /* conformance change - mmap needs to update access time for mapped
300 nanotime(&va
.va_access_time
);
301 VATTR_SET_ACTIVE(&va
, va_access_time
);
303 context
.vc_ucred
= kauth_cred_get();
304 vnode_setattr(vp
, &va
, &context
);
307 * XXX hack to handle use of /dev/zero to map anon memory (ala
310 if (vp
->v_type
== VCHR
|| vp
->v_type
== VSTR
) {
316 * Ensure that file and memory protections are
317 * compatible. Note that we only worry about
318 * writability if mapping is shared; in this case,
319 * current and max prot are dictated by the open file.
320 * XXX use the vnode instead? Problem is: what
321 * credentials do we use for determination? What if
322 * proc does a setuid?
324 maxprot
= VM_PROT_EXECUTE
; /* ??? */
325 if (fp
->f_fglob
->fg_flag
& FREAD
)
326 maxprot
|= VM_PROT_READ
;
327 else if (prot
& PROT_READ
) {
333 * If we are sharing potential changes (either via
334 * MAP_SHARED or via the implicit sharing of character
335 * device mappings), and we are trying to get write
336 * permission although we opened it without asking
340 if ((flags
& MAP_SHARED
) != 0) {
341 if ((fp
->f_fglob
->fg_flag
& FWRITE
) != 0) {
343 * check for write access
345 * Note that we already made this check when granting FWRITE
346 * against the file, so it seems redundant here.
348 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_CHECKIMMUTABLE
, &context
);
350 /* if not granted for any reason, but we wanted it, bad */
351 if ((prot
& PROT_WRITE
) && (error
!= 0)) {
356 /* if writable, remember */
358 maxprot
|= VM_PROT_WRITE
;
360 } else if ((prot
& PROT_WRITE
) != 0) {
366 maxprot
|= VM_PROT_WRITE
;
372 if (user_size
== 0) {
380 * We bend a little - round the start and end addresses
381 * to the nearest page boundary.
383 user_size
= mach_vm_round_page(user_size
);
385 if (file_pos
& PAGE_MASK_64
) {
392 user_map
= current_map();
394 if ((flags
& MAP_FIXED
) == 0) {
395 alloc_flags
= VM_FLAGS_ANYWHERE
;
396 user_addr
= mach_vm_round_page(user_addr
);
398 if (user_addr
!= mach_vm_trunc_page(user_addr
)) {
405 * mmap(MAP_FIXED) will replace any existing mappings in the
406 * specified range, if the new mapping is successful.
407 * If we just deallocate the specified address range here,
408 * another thread might jump in and allocate memory in that
409 * range before we get a chance to establish the new mapping,
410 * and we won't have a chance to restore the old mappings.
411 * So we use VM_FLAGS_OVERWRITE to let Mach VM know that it
412 * has to deallocate the existing mappings and establish the
413 * new ones atomically.
415 alloc_flags
= VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
;
420 * Lookup/allocate object.
422 if (handle
== NULL
) {
426 #if defined(VM_PROT_READ_IS_EXEC)
427 if (prot
& VM_PROT_READ
)
428 prot
|= VM_PROT_EXECUTE
;
430 if (maxprot
& VM_PROT_READ
)
431 maxprot
|= VM_PROT_EXECUTE
;
434 result
= mach_vm_map(user_map
, &user_addr
, user_size
, 0,
435 alloc_flags
, IPC_PORT_NULL
, 0,
436 FALSE
, prot
, maxprot
,
437 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
:
439 if (result
!= KERN_SUCCESS
)
442 UBCINFOCHECK("mmap", vp
);
443 pager
= (vm_pager_t
)ubc_getpager(vp
);
453 * FIXME: if we're writing the file we need a way to
454 * ensure that someone doesn't replace our R/W creds
455 * with ones that only work for read.
460 if ((flags
& (MAP_ANON
|MAP_SHARED
)) == 0) {
466 #if defined(VM_PROT_READ_IS_EXEC)
467 if (prot
& VM_PROT_READ
)
468 prot
|= VM_PROT_EXECUTE
;
470 if (maxprot
& VM_PROT_READ
)
471 maxprot
|= VM_PROT_EXECUTE
;
475 result
= mach_vm_map(user_map
, &user_addr
, user_size
,
476 0, alloc_flags
, (ipc_port_t
)pager
, file_pos
,
477 docow
, prot
, maxprot
,
478 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
:
481 if (result
!= KERN_SUCCESS
) {
486 (void)ubc_map(vp
,(prot
& ( PROT_EXEC
| PROT_READ
| PROT_WRITE
| PROT_EXEC
)));
495 *retval
= user_addr
+ pageoff
;
498 case KERN_INVALID_ADDRESS
:
502 case KERN_PROTECTION_FAILURE
:
511 fp_drop(p
, fd
, fp
, 0);
516 msync(__unused
struct proc
*p
, struct msync_args
*uap
, __unused register_t
*retval
)
518 mach_vm_offset_t addr
;
523 vm_sync_t sync_flags
=0;
525 addr
= (mach_vm_offset_t
) uap
->addr
;
526 size
= (mach_vm_size_t
)uap
->len
;
528 if (addr
& PAGE_MASK_64
) {
529 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
534 * We cannot support this properly without maintaining
535 * list all mmaps done. Cannot use vm_map_entry as they could be
536 * split or coalesced by indepenedant actions. So instead of
537 * inaccurate results, lets just return error as invalid size
540 return (EINVAL
); /* XXX breaks posix apps */
544 /* disallow contradictory flags */
545 if ((flags
& (MS_SYNC
|MS_ASYNC
)) == (MS_SYNC
|MS_ASYNC
) ||
546 (flags
& (MS_ASYNC
|MS_INVALIDATE
)) == (MS_ASYNC
|MS_INVALIDATE
))
549 if (flags
& MS_KILLPAGES
)
550 sync_flags
|= VM_SYNC_KILLPAGES
;
551 if (flags
& MS_DEACTIVATE
)
552 sync_flags
|= VM_SYNC_DEACTIVATE
;
553 if (flags
& MS_INVALIDATE
)
554 sync_flags
|= VM_SYNC_INVALIDATE
;
556 if ( !(flags
& (MS_KILLPAGES
| MS_DEACTIVATE
))) {
557 if (flags
& MS_ASYNC
)
558 sync_flags
|= VM_SYNC_ASYNCHRONOUS
;
560 sync_flags
|= VM_SYNC_SYNCHRONOUS
;
563 sync_flags
|= VM_SYNC_CONTIGUOUS
; /* complain if holes */
565 user_map
= current_map();
566 rv
= mach_vm_msync(user_map
, addr
, size
, sync_flags
);
571 case KERN_INVALID_ADDRESS
: /* hole in region being sync'ed */
585 /* Not yet implemented */
590 munmap(__unused
struct proc
*p
, struct munmap_args
*uap
, __unused register_t
*retval
)
592 mach_vm_offset_t user_addr
;
593 mach_vm_size_t user_size
;
594 kern_return_t result
;
596 user_addr
= (mach_vm_offset_t
) uap
->addr
;
597 user_size
= (mach_vm_size_t
) uap
->len
;
599 AUDIT_ARG(addr
, user_addr
);
600 AUDIT_ARG(len
, user_size
);
602 if (user_addr
& PAGE_MASK_64
) {
603 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
607 if (user_addr
+ user_size
< user_addr
)
610 if (user_size
== 0) {
611 /* UNIX SPEC: size is 0, return EINVAL */
615 result
= mach_vm_deallocate(current_map(), user_addr
, user_size
);
616 if (result
!= KERN_SUCCESS
) {
623 mprotect(__unused
struct proc
*p
, struct mprotect_args
*uap
, __unused register_t
*retval
)
625 register vm_prot_t prot
;
626 mach_vm_offset_t user_addr
;
627 mach_vm_size_t user_size
;
628 kern_return_t result
;
631 AUDIT_ARG(addr
, uap
->addr
);
632 AUDIT_ARG(len
, uap
->len
);
633 AUDIT_ARG(value
, uap
->prot
);
635 user_addr
= (mach_vm_offset_t
) uap
->addr
;
636 user_size
= (mach_vm_size_t
) uap
->len
;
637 prot
= (vm_prot_t
)(uap
->prot
& VM_PROT_ALL
);
639 if (user_addr
& PAGE_MASK_64
) {
640 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
646 #if defined(VM_PROT_READ_IS_EXEC)
647 if (prot
& VM_PROT_READ
)
648 prot
|= VM_PROT_EXECUTE
;
652 user_map
= current_map();
654 result
= mach_vm_protect(user_map
, user_addr
, user_size
,
659 case KERN_PROTECTION_FAILURE
:
661 case KERN_INVALID_ADDRESS
:
662 /* UNIX SPEC: for an invalid address range, return ENOMEM */
670 minherit(__unused
struct proc
*p
, struct minherit_args
*uap
, __unused register_t
*retval
)
672 mach_vm_offset_t addr
;
674 register vm_inherit_t inherit
;
676 kern_return_t result
;
678 AUDIT_ARG(addr
, uap
->addr
);
679 AUDIT_ARG(len
, uap
->len
);
680 AUDIT_ARG(value
, uap
->inherit
);
682 addr
= (mach_vm_offset_t
)uap
->addr
;
683 size
= (mach_vm_size_t
)uap
->len
;
684 inherit
= uap
->inherit
;
686 user_map
= current_map();
687 result
= mach_vm_inherit(user_map
, addr
, size
,
692 case KERN_PROTECTION_FAILURE
:
699 madvise(__unused
struct proc
*p
, struct madvise_args
*uap
, __unused register_t
*retval
)
702 mach_vm_offset_t start
;
704 vm_behavior_t new_behavior
;
705 kern_return_t result
;
708 * Since this routine is only advisory, we default to conservative
711 switch (uap
->behav
) {
713 new_behavior
= VM_BEHAVIOR_RANDOM
;
715 case MADV_SEQUENTIAL
:
716 new_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
719 new_behavior
= VM_BEHAVIOR_DEFAULT
;
722 new_behavior
= VM_BEHAVIOR_WILLNEED
;
725 new_behavior
= VM_BEHAVIOR_DONTNEED
;
731 start
= (mach_vm_offset_t
) uap
->addr
;
732 size
= (mach_vm_size_t
) uap
->len
;
734 user_map
= current_map();
736 result
= mach_vm_behavior_set(user_map
, start
, size
, new_behavior
);
740 case KERN_INVALID_ADDRESS
:
748 mincore(__unused
struct proc
*p
, struct mincore_args
*uap
, __unused register_t
*retval
)
750 mach_vm_offset_t addr
, first_addr
, end
;
754 int vecindex
, lastvecindex
;
765 * Make sure that the addresses presented are valid for user
768 first_addr
= addr
= mach_vm_trunc_page(uap
->addr
);
769 end
= addr
+ mach_vm_round_page(uap
->len
);
775 * Address of byte vector
782 * Do this on a map entry basis so that if the pages are not
783 * in the current processes address space, we can easily look
784 * up the pages elsewhere.
787 for( ; addr
< end
; addr
+= PAGE_SIZE
) {
789 ret
= vm_map_page_query(map
, addr
, &pqueryinfo
, &numref
);
790 if (ret
!= KERN_SUCCESS
)
793 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_PRESENT
)
794 mincoreinfo
|= MINCORE_INCORE
;
795 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_REF
)
796 mincoreinfo
|= MINCORE_REFERENCED
;
797 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_DIRTY
)
798 mincoreinfo
|= MINCORE_MODIFIED
;
802 * calculate index into user supplied byte vector
804 vecindex
= (addr
- first_addr
)>> PAGE_SHIFT
;
807 * If we have skipped map entries, we need to make sure that
808 * the byte vector is zeroed for those skipped entries.
810 while((lastvecindex
+ 1) < vecindex
) {
812 error
= copyout(&c
, vec
+ lastvecindex
, 1);
820 * Pass the page information to the user
822 c
= (char)mincoreinfo
;
823 error
= copyout(&c
, vec
+ vecindex
, 1);
827 lastvecindex
= vecindex
;
832 * Zero the last entries in the byte vector.
834 vecindex
= (end
- first_addr
) >> PAGE_SHIFT
;
835 while((lastvecindex
+ 1) < vecindex
) {
837 error
= copyout(&c
, vec
+ lastvecindex
, 1);
848 mlock(__unused
struct proc
*p
, struct mlock_args
*uap
, __unused register_t
*retvalval
)
851 vm_map_offset_t addr
;
852 vm_map_size_t size
, pageoff
;
853 kern_return_t result
;
855 AUDIT_ARG(addr
, uap
->addr
);
856 AUDIT_ARG(len
, uap
->len
);
858 addr
= (vm_map_offset_t
) uap
->addr
;
859 size
= (vm_map_size_t
)uap
->len
;
861 /* disable wrap around */
862 if (addr
+ size
< addr
)
868 pageoff
= (addr
& PAGE_MASK
);
870 size
= vm_map_round_page(size
+pageoff
);
873 /* Hmm.. What am I going to do with this? */
874 if (atop(size
) + cnt
.v_wire_count
> vm_page_max_wired
)
876 #ifdef pmap_wired_count
877 if (size
+ ptoa(pmap_wired_count(vm_map_pmap(&p
->p_vmspace
->vm_map
))) >
878 p
->p_rlimit
[RLIMIT_MEMLOCK
].rlim_cur
)
881 error
= suser(kauth_cred_get(), &p
->p_acflag
);
887 user_map
= current_map();
889 /* have to call vm_map_wire directly to pass "I don't know" protections */
890 result
= vm_map_wire(user_map
, addr
, addr
+size
, VM_PROT_NONE
, TRUE
);
891 return (result
== KERN_SUCCESS
? 0 : ENOMEM
);
895 munlock(__unused
struct proc
*p
, struct munlock_args
*uap
, __unused register_t
*retval
)
897 mach_vm_offset_t addr
;
900 kern_return_t result
;
902 AUDIT_ARG(addr
, uap
->addr
);
903 AUDIT_ARG(addr
, uap
->len
);
905 addr
= (mach_vm_offset_t
) uap
->addr
;
906 size
= (mach_vm_size_t
)uap
->len
;
910 /* Hmm.. What am I going to do with this? */
911 #ifndef pmap_wired_count
912 error
= suser(kauth_cred_get(), &p
->p_acflag
);
918 user_map
= current_map();
920 /* JMM - need to remove all wirings by spec - this just removes one */
921 result
= mach_vm_wire(host_priv_self(), user_map
, addr
, size
, VM_PROT_NONE
);
922 return (result
== KERN_SUCCESS
? 0 : ENOMEM
);
927 mlockall(__unused
struct proc
*p
, __unused
struct mlockall_args
*uap
, __unused register_t
*retval
)
933 munlockall(__unused
struct proc
*p
, __unused
struct munlockall_args
*uap
, __unused register_t
*retval
)
941 obreak(__unused
struct proc
*p
, __unused
struct obreak_args
*uap
, __unused register_t
*retval
)
943 /* Not implemented, obsolete */
950 ovadvise(__unused
struct proc
*p
, __unused
struct ovadvise_args
*uap
, __unused register_t
*retval
)
960 /* USV: No! need to obsolete map_fd()! mmap() already supports 64 bits */
962 map_fd(struct map_fd_args
*args
)
965 vm_offset_t offset
= args
->offset
;
966 vm_offset_t
*va
= args
->va
;
967 boolean_t findspace
= args
->findspace
;
968 vm_size_t size
= args
->size
;
971 AUDIT_MACH_SYSCALL_ENTER(AUE_MAPFD
);
972 AUDIT_ARG(addr
, CAST_DOWN(user_addr_t
, va
));
975 ret
= map_fd_funneled( fd
, (vm_object_offset_t
)offset
, va
, findspace
, size
);
977 AUDIT_MACH_SYSCALL_EXIT(ret
);
984 vm_object_offset_t offset
,
989 kern_return_t result
;
993 vm_offset_t map_addr
=0;
997 struct proc
*p
=(struct proc
*)current_proc();
998 struct vnode_attr vattr
;
999 struct vfs_context context
;
1002 * Find the inode; verify that it's a regular file.
1005 err
= fp_lookup(p
, fd
, &fp
, 0);
1009 if (fp
->f_fglob
->fg_type
!= DTYPE_VNODE
){
1010 err
= KERN_INVALID_ARGUMENT
;
1014 if (!(fp
->f_fglob
->fg_flag
& FREAD
)) {
1015 err
= KERN_PROTECTION_FAILURE
;
1019 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1020 err
= vnode_getwithref(vp
);
1024 if (vp
->v_type
!= VREG
) {
1025 (void)vnode_put(vp
);
1026 err
= KERN_INVALID_ARGUMENT
;
1030 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
1032 /* conformance change - mmap needs to update access time for mapped
1036 nanotime(&vattr
.va_access_time
);
1037 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
1038 context
.vc_proc
= p
;
1039 context
.vc_ucred
= kauth_cred_get();
1040 vnode_setattr(vp
, &vattr
, &context
);
1042 if (offset
& PAGE_MASK_64
) {
1043 printf("map_fd: file offset not page aligned(%d : %s)\n",p
->p_pid
, p
->p_comm
);
1044 (void)vnode_put(vp
);
1045 err
= KERN_INVALID_ARGUMENT
;
1048 map_size
= round_page(size
);
1051 * Allow user to map in a zero length file.
1054 (void)vnode_put(vp
);
1061 UBCINFOCHECK("map_fd_funneled", vp
);
1062 pager
= (void *) ubc_getpager(vp
);
1063 if (pager
== NULL
) {
1064 (void)vnode_put(vp
);
1070 my_map
= current_map();
1074 &map_addr
, map_size
, (vm_offset_t
)0,
1075 VM_FLAGS_ANYWHERE
, pager
, offset
, TRUE
,
1076 VM_PROT_DEFAULT
, VM_PROT_ALL
,
1077 VM_INHERIT_DEFAULT
);
1078 if (result
!= KERN_SUCCESS
) {
1079 (void)vnode_put(vp
);
1086 vm_offset_t dst_addr
;
1089 if (copyin(CAST_USER_ADDR_T(va
), &dst_addr
, sizeof (dst_addr
)) ||
1090 trunc_page_32(dst_addr
) != dst_addr
) {
1091 (void) vm_map_remove(
1093 map_addr
, map_addr
+ map_size
,
1095 (void)vnode_put(vp
);
1096 err
= KERN_INVALID_ADDRESS
;
1100 result
= vm_map_copyin(my_map
, (vm_map_address_t
)map_addr
,
1101 (vm_map_size_t
)map_size
, TRUE
, &tmp
);
1102 if (result
!= KERN_SUCCESS
) {
1104 (void) vm_map_remove(my_map
, vm_map_trunc_page(map_addr
),
1105 vm_map_round_page(map_addr
+ map_size
),
1107 (void)vnode_put(vp
);
1112 result
= vm_map_copy_overwrite(my_map
,
1113 (vm_map_address_t
)dst_addr
, tmp
, FALSE
);
1114 if (result
!= KERN_SUCCESS
) {
1115 vm_map_copy_discard(tmp
);
1116 (void)vnode_put(vp
);
1121 if (copyout(&map_addr
, CAST_USER_ADDR_T(va
), sizeof (map_addr
))) {
1122 (void) vm_map_remove(my_map
, vm_map_trunc_page(map_addr
),
1123 vm_map_round_page(map_addr
+ map_size
),
1125 (void)vnode_put(vp
);
1126 err
= KERN_INVALID_ADDRESS
;
1131 ubc_setcred(vp
, current_proc());
1132 (void)ubc_map(vp
, (PROT_READ
| PROT_WRITE
| PROT_EXEC
));
1133 (void)vnode_put(vp
);
1136 fp_drop(p
, fd
, fp
, 0);