2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1988 University of Utah.
32 * Copyright (c) 1991, 1993
33 * The Regents of the University of California. All rights reserved.
35 * This code is derived from software contributed to Berkeley by
36 * the Systems Programming Group of the University of Utah Computer
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
69 * @(#)vm_mmap.c 8.10 (Berkeley) 2/19/95
73 * Mapped file (mmap) interface to VM
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/filedesc.h>
79 #include <sys/proc_internal.h>
80 #include <sys/kauth.h>
81 #include <sys/resourcevar.h>
82 #include <sys/vnode_internal.h>
85 #include <sys/file_internal.h>
86 #include <sys/vadvise.h>
87 #include <sys/trace.h>
92 #include <sys/sysproto.h>
94 #include <bsm/audit_kernel.h>
95 #include <bsm/audit_kevents.h>
97 #include <mach/mach_types.h>
98 #include <mach/mach_traps.h>
99 #include <mach/vm_sync.h>
100 #include <mach/vm_behavior.h>
101 #include <mach/vm_inherit.h>
102 #include <mach/vm_statistics.h>
103 #include <mach/mach_vm.h>
104 #include <mach/vm_map.h>
105 #include <mach/host_priv.h>
107 #include <kern/cpu_number.h>
108 #include <kern/host.h>
110 #include <vm/vm_map.h>
111 #include <vm/vm_kern.h>
112 #include <vm/vm_pager.h>
115 sbrk(__unused
struct proc
*p
, __unused
struct sbrk_args
*uap
, __unused register_t
*retval
)
117 /* Not yet implemented */
122 sstk(__unused
struct proc
*p
, __unused
struct sstk_args
*uap
, __unused register_t
*retval
)
124 /* Not yet implemented */
141 register struct osmmap_args
*uap
,
144 struct mmap_args newargs
;
148 if ((uap
->share
== MAP_SHARED
)|| (uap
->share
== MAP_PRIVATE
)) {
149 newargs
.addr
= CAST_USER_ADDR_T(uap
->addr
);
150 newargs
.len
= CAST_USER_ADDR_T(uap
->len
);
151 newargs
.prot
= uap
->prot
;
152 newargs
.flags
= uap
->share
;
153 newargs
.fd
= uap
->fd
;
154 newargs
.pos
= (off_t
)uap
->pos
;
155 ret
= mmap(curp
, &newargs
, &addr
);
157 *retval
= CAST_DOWN(register_t
, addr
);
165 mmap(struct proc
*p
, struct mmap_args
*uap
, user_addr_t
*retval
)
168 * Map in special device (must be SHARED) or file
171 register struct vnode
*vp
;
176 kern_return_t result
;
177 mach_vm_offset_t user_addr
;
178 mach_vm_size_t user_size
;
179 vm_object_offset_t pageoff
;
180 vm_object_offset_t file_pos
;
191 user_addr
= (mach_vm_offset_t
)uap
->addr
;
192 user_size
= (mach_vm_size_t
) uap
->len
;
194 AUDIT_ARG(addr
, user_addr
);
195 AUDIT_ARG(len
, user_size
);
196 AUDIT_ARG(fd
, uap
->fd
);
198 prot
= (uap
->prot
& VM_PROT_ALL
);
203 * The vm code does not have prototypes & compiler doesn't do the'
204 * the right thing when you cast 64bit value and pass it in function
205 * call. So here it is.
207 file_pos
= (vm_object_offset_t
)uap
->pos
;
210 /* make sure mapping fits into numeric range etc */
211 if ((file_pos
+ user_size
> (vm_object_offset_t
)-PAGE_SIZE_64
) ||
212 ((flags
& MAP_ANON
) && fd
!= -1))
216 * Align the file position to a page boundary,
217 * and save its page offset component.
219 pageoff
= (file_pos
& PAGE_MASK
);
220 file_pos
-= (vm_object_offset_t
)pageoff
;
223 /* Adjust size for rounding (on both ends). */
224 user_size
+= pageoff
; /* low end... */
225 user_size
= mach_vm_round_page(user_size
); /* hi end */
229 * Check for illegal addresses. Watch out for address wrap... Note
230 * that VM_*_ADDRESS are not constants due to casts (argh).
232 if (flags
& MAP_FIXED
) {
234 * The specified address must have the same remainder
235 * as the file offset taken modulo PAGE_SIZE, so it
236 * should be aligned after adjustment by pageoff.
238 user_addr
-= pageoff
;
239 if (user_addr
& PAGE_MASK
)
243 /* DO not have apis to get this info, need to wait till then*/
245 * XXX for non-fixed mappings where no hint is provided or
246 * the hint would fall in the potential heap space,
247 * place it after the end of the largest possible heap.
249 * There should really be a pmap call to determine a reasonable
252 else if (addr
< mach_vm_round_page(p
->p_vmspace
->vm_daddr
+ MAXDSIZ
))
253 addr
= mach_vm_round_page(p
->p_vmspace
->vm_daddr
+ MAXDSIZ
);
258 if (flags
& MAP_ANON
) {
260 * Mapping blank space is trivial.
263 maxprot
= VM_PROT_ALL
;
267 struct vnode_attr va
;
268 struct vfs_context context
;
270 * Mapping file, get fp for validation. Obtain vnode and make
271 * sure it is of appropriate type.
273 err
= fp_lookup(p
, fd
, &fp
, 0);
277 if(fp
->f_fglob
->fg_type
== DTYPE_PSXSHM
) {
278 uap
->addr
= (user_addr_t
)user_addr
;
279 uap
->len
= (user_size_t
)user_size
;
283 error
= pshm_mmap(p
, uap
, retval
, fp
, (off_t
)pageoff
);
287 if (fp
->f_fglob
->fg_type
!= DTYPE_VNODE
) {
291 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
292 error
= vnode_getwithref(vp
);
296 if (vp
->v_type
!= VREG
&& vp
->v_type
!= VCHR
) {
302 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
304 /* conformance change - mmap needs to update access time for mapped
308 nanotime(&va
.va_access_time
);
309 VATTR_SET_ACTIVE(&va
, va_access_time
);
311 context
.vc_ucred
= kauth_cred_get();
312 vnode_setattr(vp
, &va
, &context
);
315 * XXX hack to handle use of /dev/zero to map anon memory (ala
318 if (vp
->v_type
== VCHR
|| vp
->v_type
== VSTR
) {
324 * Ensure that file and memory protections are
325 * compatible. Note that we only worry about
326 * writability if mapping is shared; in this case,
327 * current and max prot are dictated by the open file.
328 * XXX use the vnode instead? Problem is: what
329 * credentials do we use for determination? What if
330 * proc does a setuid?
332 maxprot
= VM_PROT_EXECUTE
; /* ??? */
333 if (fp
->f_fglob
->fg_flag
& FREAD
)
334 maxprot
|= VM_PROT_READ
;
335 else if (prot
& PROT_READ
) {
341 * If we are sharing potential changes (either via
342 * MAP_SHARED or via the implicit sharing of character
343 * device mappings), and we are trying to get write
344 * permission although we opened it without asking
348 if ((flags
& MAP_SHARED
) != 0) {
349 if ((fp
->f_fglob
->fg_flag
& FWRITE
) != 0) {
351 * check for write access
353 * Note that we already made this check when granting FWRITE
354 * against the file, so it seems redundant here.
356 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_CHECKIMMUTABLE
, &context
);
358 /* if not granted for any reason, but we wanted it, bad */
359 if ((prot
& PROT_WRITE
) && (error
!= 0)) {
364 /* if writable, remember */
366 maxprot
|= VM_PROT_WRITE
;
368 } else if ((prot
& PROT_WRITE
) != 0) {
374 maxprot
|= VM_PROT_WRITE
;
380 if (user_size
== 0) {
388 * We bend a little - round the start and end addresses
389 * to the nearest page boundary.
391 user_size
= mach_vm_round_page(user_size
);
393 if (file_pos
& PAGE_MASK_64
) {
400 user_map
= current_map();
402 if ((flags
& MAP_FIXED
) == 0) {
403 alloc_flags
= VM_FLAGS_ANYWHERE
;
404 user_addr
= mach_vm_round_page(user_addr
);
406 if (user_addr
!= mach_vm_trunc_page(user_addr
)) {
413 * mmap(MAP_FIXED) will replace any existing mappings in the
414 * specified range, if the new mapping is successful.
415 * If we just deallocate the specified address range here,
416 * another thread might jump in and allocate memory in that
417 * range before we get a chance to establish the new mapping,
418 * and we won't have a chance to restore the old mappings.
419 * So we use VM_FLAGS_OVERWRITE to let Mach VM know that it
420 * has to deallocate the existing mappings and establish the
421 * new ones atomically.
423 alloc_flags
= VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
;
428 * Lookup/allocate object.
430 if (handle
== NULL
) {
434 #if defined(VM_PROT_READ_IS_EXEC)
435 if (prot
& VM_PROT_READ
)
436 prot
|= VM_PROT_EXECUTE
;
438 if (maxprot
& VM_PROT_READ
)
439 maxprot
|= VM_PROT_EXECUTE
;
442 result
= mach_vm_map(user_map
, &user_addr
, user_size
, 0,
443 alloc_flags
, IPC_PORT_NULL
, 0,
444 FALSE
, prot
, maxprot
,
445 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
:
447 if (result
!= KERN_SUCCESS
)
450 UBCINFOCHECK("mmap", vp
);
451 pager
= (vm_pager_t
)ubc_getpager(vp
);
461 * FIXME: if we're writing the file we need a way to
462 * ensure that someone doesn't replace our R/W creds
463 * with ones that only work for read.
466 ubc_setthreadcred(vp
, p
, current_thread());
468 if ((flags
& (MAP_ANON
|MAP_SHARED
)) == 0) {
474 #if defined(VM_PROT_READ_IS_EXEC)
475 if (prot
& VM_PROT_READ
)
476 prot
|= VM_PROT_EXECUTE
;
478 if (maxprot
& VM_PROT_READ
)
479 maxprot
|= VM_PROT_EXECUTE
;
483 result
= mach_vm_map(user_map
, &user_addr
, user_size
,
484 0, alloc_flags
, (ipc_port_t
)pager
, file_pos
,
485 docow
, prot
, maxprot
,
486 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
:
489 if (result
!= KERN_SUCCESS
) {
494 (void)ubc_map(vp
,(prot
& ( PROT_EXEC
| PROT_READ
| PROT_WRITE
| PROT_EXEC
)));
503 *retval
= user_addr
+ pageoff
;
506 case KERN_INVALID_ADDRESS
:
510 case KERN_PROTECTION_FAILURE
:
519 fp_drop(p
, fd
, fp
, 0);
524 msync(__unused
struct proc
*p
, struct msync_args
*uap
, __unused register_t
*retval
)
526 mach_vm_offset_t addr
;
531 vm_sync_t sync_flags
=0;
533 addr
= (mach_vm_offset_t
) uap
->addr
;
534 size
= (mach_vm_size_t
)uap
->len
;
536 if (addr
& PAGE_MASK_64
) {
537 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
542 * We cannot support this properly without maintaining
543 * list all mmaps done. Cannot use vm_map_entry as they could be
544 * split or coalesced by indepenedant actions. So instead of
545 * inaccurate results, lets just return error as invalid size
548 return (EINVAL
); /* XXX breaks posix apps */
552 /* disallow contradictory flags */
553 if ((flags
& (MS_SYNC
|MS_ASYNC
)) == (MS_SYNC
|MS_ASYNC
) ||
554 (flags
& (MS_ASYNC
|MS_INVALIDATE
)) == (MS_ASYNC
|MS_INVALIDATE
))
557 if (flags
& MS_KILLPAGES
)
558 sync_flags
|= VM_SYNC_KILLPAGES
;
559 if (flags
& MS_DEACTIVATE
)
560 sync_flags
|= VM_SYNC_DEACTIVATE
;
561 if (flags
& MS_INVALIDATE
)
562 sync_flags
|= VM_SYNC_INVALIDATE
;
564 if ( !(flags
& (MS_KILLPAGES
| MS_DEACTIVATE
))) {
565 if (flags
& MS_ASYNC
)
566 sync_flags
|= VM_SYNC_ASYNCHRONOUS
;
568 sync_flags
|= VM_SYNC_SYNCHRONOUS
;
571 sync_flags
|= VM_SYNC_CONTIGUOUS
; /* complain if holes */
573 user_map
= current_map();
574 rv
= mach_vm_msync(user_map
, addr
, size
, sync_flags
);
579 case KERN_INVALID_ADDRESS
: /* hole in region being sync'ed */
593 /* Not yet implemented */
598 munmap(__unused
struct proc
*p
, struct munmap_args
*uap
, __unused register_t
*retval
)
600 mach_vm_offset_t user_addr
;
601 mach_vm_size_t user_size
;
602 kern_return_t result
;
604 user_addr
= (mach_vm_offset_t
) uap
->addr
;
605 user_size
= (mach_vm_size_t
) uap
->len
;
607 AUDIT_ARG(addr
, user_addr
);
608 AUDIT_ARG(len
, user_size
);
610 if (user_addr
& PAGE_MASK_64
) {
611 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
615 if (user_addr
+ user_size
< user_addr
)
618 if (user_size
== 0) {
619 /* UNIX SPEC: size is 0, return EINVAL */
623 result
= mach_vm_deallocate(current_map(), user_addr
, user_size
);
624 if (result
!= KERN_SUCCESS
) {
631 mprotect(__unused
struct proc
*p
, struct mprotect_args
*uap
, __unused register_t
*retval
)
633 register vm_prot_t prot
;
634 mach_vm_offset_t user_addr
;
635 mach_vm_size_t user_size
;
636 kern_return_t result
;
639 AUDIT_ARG(addr
, uap
->addr
);
640 AUDIT_ARG(len
, uap
->len
);
641 AUDIT_ARG(value
, uap
->prot
);
643 user_addr
= (mach_vm_offset_t
) uap
->addr
;
644 user_size
= (mach_vm_size_t
) uap
->len
;
645 prot
= (vm_prot_t
)(uap
->prot
& VM_PROT_ALL
);
647 if (user_addr
& PAGE_MASK_64
) {
648 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
654 #if defined(VM_PROT_READ_IS_EXEC)
655 if (prot
& VM_PROT_READ
)
656 prot
|= VM_PROT_EXECUTE
;
660 user_map
= current_map();
662 result
= mach_vm_protect(user_map
, user_addr
, user_size
,
667 case KERN_PROTECTION_FAILURE
:
669 case KERN_INVALID_ADDRESS
:
670 /* UNIX SPEC: for an invalid address range, return ENOMEM */
678 minherit(__unused
struct proc
*p
, struct minherit_args
*uap
, __unused register_t
*retval
)
680 mach_vm_offset_t addr
;
682 register vm_inherit_t inherit
;
684 kern_return_t result
;
686 AUDIT_ARG(addr
, uap
->addr
);
687 AUDIT_ARG(len
, uap
->len
);
688 AUDIT_ARG(value
, uap
->inherit
);
690 addr
= (mach_vm_offset_t
)uap
->addr
;
691 size
= (mach_vm_size_t
)uap
->len
;
692 inherit
= uap
->inherit
;
694 user_map
= current_map();
695 result
= mach_vm_inherit(user_map
, addr
, size
,
700 case KERN_PROTECTION_FAILURE
:
707 madvise(__unused
struct proc
*p
, struct madvise_args
*uap
, __unused register_t
*retval
)
710 mach_vm_offset_t start
;
712 vm_behavior_t new_behavior
;
713 kern_return_t result
;
716 * Since this routine is only advisory, we default to conservative
719 switch (uap
->behav
) {
721 new_behavior
= VM_BEHAVIOR_RANDOM
;
723 case MADV_SEQUENTIAL
:
724 new_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
727 new_behavior
= VM_BEHAVIOR_DEFAULT
;
730 new_behavior
= VM_BEHAVIOR_WILLNEED
;
733 new_behavior
= VM_BEHAVIOR_DONTNEED
;
739 start
= (mach_vm_offset_t
) uap
->addr
;
740 size
= (mach_vm_size_t
) uap
->len
;
742 user_map
= current_map();
744 result
= mach_vm_behavior_set(user_map
, start
, size
, new_behavior
);
748 case KERN_INVALID_ADDRESS
:
756 mincore(__unused
struct proc
*p
, struct mincore_args
*uap
, __unused register_t
*retval
)
758 mach_vm_offset_t addr
, first_addr
, end
;
762 int vecindex
, lastvecindex
;
773 * Make sure that the addresses presented are valid for user
776 first_addr
= addr
= mach_vm_trunc_page(uap
->addr
);
777 end
= addr
+ mach_vm_round_page(uap
->len
);
783 * Address of byte vector
790 * Do this on a map entry basis so that if the pages are not
791 * in the current processes address space, we can easily look
792 * up the pages elsewhere.
795 for( ; addr
< end
; addr
+= PAGE_SIZE
) {
797 ret
= vm_map_page_query(map
, addr
, &pqueryinfo
, &numref
);
798 if (ret
!= KERN_SUCCESS
)
801 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_PRESENT
)
802 mincoreinfo
|= MINCORE_INCORE
;
803 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_REF
)
804 mincoreinfo
|= MINCORE_REFERENCED
;
805 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_DIRTY
)
806 mincoreinfo
|= MINCORE_MODIFIED
;
810 * calculate index into user supplied byte vector
812 vecindex
= (addr
- first_addr
)>> PAGE_SHIFT
;
815 * If we have skipped map entries, we need to make sure that
816 * the byte vector is zeroed for those skipped entries.
818 while((lastvecindex
+ 1) < vecindex
) {
820 error
= copyout(&c
, vec
+ lastvecindex
, 1);
828 * Pass the page information to the user
830 c
= (char)mincoreinfo
;
831 error
= copyout(&c
, vec
+ vecindex
, 1);
835 lastvecindex
= vecindex
;
840 * Zero the last entries in the byte vector.
842 vecindex
= (end
- first_addr
) >> PAGE_SHIFT
;
843 while((lastvecindex
+ 1) < vecindex
) {
845 error
= copyout(&c
, vec
+ lastvecindex
, 1);
856 mlock(__unused
struct proc
*p
, struct mlock_args
*uap
, __unused register_t
*retvalval
)
859 vm_map_offset_t addr
;
860 vm_map_size_t size
, pageoff
;
861 kern_return_t result
;
863 AUDIT_ARG(addr
, uap
->addr
);
864 AUDIT_ARG(len
, uap
->len
);
866 addr
= (vm_map_offset_t
) uap
->addr
;
867 size
= (vm_map_size_t
)uap
->len
;
869 /* disable wrap around */
870 if (addr
+ size
< addr
)
876 pageoff
= (addr
& PAGE_MASK
);
878 size
= vm_map_round_page(size
+pageoff
);
881 /* Hmm.. What am I going to do with this? */
882 if (atop(size
) + cnt
.v_wire_count
> vm_page_max_wired
)
884 #ifdef pmap_wired_count
885 if (size
+ ptoa(pmap_wired_count(vm_map_pmap(&p
->p_vmspace
->vm_map
))) >
886 p
->p_rlimit
[RLIMIT_MEMLOCK
].rlim_cur
)
889 error
= suser(kauth_cred_get(), &p
->p_acflag
);
895 user_map
= current_map();
897 /* have to call vm_map_wire directly to pass "I don't know" protections */
898 result
= vm_map_wire(user_map
, addr
, addr
+size
, VM_PROT_NONE
, TRUE
);
899 return (result
== KERN_SUCCESS
? 0 : ENOMEM
);
903 munlock(__unused
struct proc
*p
, struct munlock_args
*uap
, __unused register_t
*retval
)
905 mach_vm_offset_t addr
;
908 kern_return_t result
;
910 AUDIT_ARG(addr
, uap
->addr
);
911 AUDIT_ARG(addr
, uap
->len
);
913 addr
= (mach_vm_offset_t
) uap
->addr
;
914 size
= (mach_vm_size_t
)uap
->len
;
918 /* Hmm.. What am I going to do with this? */
919 #ifndef pmap_wired_count
920 error
= suser(kauth_cred_get(), &p
->p_acflag
);
926 user_map
= current_map();
928 /* JMM - need to remove all wirings by spec - this just removes one */
929 result
= mach_vm_wire(host_priv_self(), user_map
, addr
, size
, VM_PROT_NONE
);
930 return (result
== KERN_SUCCESS
? 0 : ENOMEM
);
935 mlockall(__unused
struct proc
*p
, __unused
struct mlockall_args
*uap
, __unused register_t
*retval
)
941 munlockall(__unused
struct proc
*p
, __unused
struct munlockall_args
*uap
, __unused register_t
*retval
)
949 obreak(__unused
struct proc
*p
, __unused
struct obreak_args
*uap
, __unused register_t
*retval
)
951 /* Not implemented, obsolete */
958 ovadvise(__unused
struct proc
*p
, __unused
struct ovadvise_args
*uap
, __unused register_t
*retval
)
968 /* USV: No! need to obsolete map_fd()! mmap() already supports 64 bits */
970 map_fd(struct map_fd_args
*args
)
973 vm_offset_t offset
= args
->offset
;
974 vm_offset_t
*va
= args
->va
;
975 boolean_t findspace
= args
->findspace
;
976 vm_size_t size
= args
->size
;
979 AUDIT_MACH_SYSCALL_ENTER(AUE_MAPFD
);
980 AUDIT_ARG(addr
, CAST_DOWN(user_addr_t
, va
));
983 ret
= map_fd_funneled( fd
, (vm_object_offset_t
)offset
, va
, findspace
, size
);
985 AUDIT_MACH_SYSCALL_EXIT(ret
);
992 vm_object_offset_t offset
,
997 kern_return_t result
;
1001 vm_offset_t map_addr
=0;
1005 struct proc
*p
=(struct proc
*)current_proc();
1006 struct vnode_attr vattr
;
1007 struct vfs_context context
;
1010 * Find the inode; verify that it's a regular file.
1013 err
= fp_lookup(p
, fd
, &fp
, 0);
1017 if (fp
->f_fglob
->fg_type
!= DTYPE_VNODE
){
1018 err
= KERN_INVALID_ARGUMENT
;
1022 if (!(fp
->f_fglob
->fg_flag
& FREAD
)) {
1023 err
= KERN_PROTECTION_FAILURE
;
1027 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1028 err
= vnode_getwithref(vp
);
1032 if (vp
->v_type
!= VREG
) {
1033 (void)vnode_put(vp
);
1034 err
= KERN_INVALID_ARGUMENT
;
1038 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
1040 /* conformance change - mmap needs to update access time for mapped
1044 nanotime(&vattr
.va_access_time
);
1045 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
1046 context
.vc_proc
= p
;
1047 context
.vc_ucred
= kauth_cred_get();
1048 vnode_setattr(vp
, &vattr
, &context
);
1050 if (offset
& PAGE_MASK_64
) {
1051 printf("map_fd: file offset not page aligned(%d : %s)\n",p
->p_pid
, p
->p_comm
);
1052 (void)vnode_put(vp
);
1053 err
= KERN_INVALID_ARGUMENT
;
1056 map_size
= round_page(size
);
1059 * Allow user to map in a zero length file.
1062 (void)vnode_put(vp
);
1069 UBCINFOCHECK("map_fd_funneled", vp
);
1070 pager
= (void *) ubc_getpager(vp
);
1071 if (pager
== NULL
) {
1072 (void)vnode_put(vp
);
1078 my_map
= current_map();
1082 &map_addr
, map_size
, (vm_offset_t
)0,
1083 VM_FLAGS_ANYWHERE
, pager
, offset
, TRUE
,
1084 VM_PROT_DEFAULT
, VM_PROT_ALL
,
1085 VM_INHERIT_DEFAULT
);
1086 if (result
!= KERN_SUCCESS
) {
1087 (void)vnode_put(vp
);
1094 vm_offset_t dst_addr
;
1097 if (copyin(CAST_USER_ADDR_T(va
), &dst_addr
, sizeof (dst_addr
)) ||
1098 trunc_page_32(dst_addr
) != dst_addr
) {
1099 (void) vm_map_remove(
1101 map_addr
, map_addr
+ map_size
,
1103 (void)vnode_put(vp
);
1104 err
= KERN_INVALID_ADDRESS
;
1108 result
= vm_map_copyin(my_map
, (vm_map_address_t
)map_addr
,
1109 (vm_map_size_t
)map_size
, TRUE
, &tmp
);
1110 if (result
!= KERN_SUCCESS
) {
1112 (void) vm_map_remove(my_map
, vm_map_trunc_page(map_addr
),
1113 vm_map_round_page(map_addr
+ map_size
),
1115 (void)vnode_put(vp
);
1120 result
= vm_map_copy_overwrite(my_map
,
1121 (vm_map_address_t
)dst_addr
, tmp
, FALSE
);
1122 if (result
!= KERN_SUCCESS
) {
1123 vm_map_copy_discard(tmp
);
1124 (void)vnode_put(vp
);
1129 if (copyout(&map_addr
, CAST_USER_ADDR_T(va
), sizeof (map_addr
))) {
1130 (void) vm_map_remove(my_map
, vm_map_trunc_page(map_addr
),
1131 vm_map_round_page(map_addr
+ map_size
),
1133 (void)vnode_put(vp
);
1134 err
= KERN_INVALID_ADDRESS
;
1139 ubc_setthreadcred(vp
, current_proc(), current_thread());
1140 (void)ubc_map(vp
, (PROT_READ
| PROT_WRITE
| PROT_EXEC
));
1141 (void)vnode_put(vp
);
1144 fp_drop(p
, fd
, fp
, 0);