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>
72 #include <sys/resourcevar.h>
74 #include <sys/vnode.h>
78 #include <sys/vadvise.h>
79 #include <sys/trace.h>
85 #include <bsm/audit_kernel.h>
86 #include <bsm/audit_kevents.h>
88 #include <mach/mach_types.h>
90 #include <kern/cpu_number.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_kern.h>
94 #include <vm/vm_pager.h>
96 #include <mach/vm_sync.h>
97 #include <mach/vm_behavior.h>
98 #include <mach/vm_inherit.h>
99 #include <mach/vm_statistics.h>
109 struct sbrk_args
*uap
;
112 /* Not yet implemented */
124 struct sstk_args
*uap
;
127 /* Not yet implemented */
134 ogetpagesize(p
, uap
, retval
)
143 #endif /* COMPAT_43 */
155 osmmap(curp
, uap
, retval
)
157 register struct osmmap_args
*uap
;
166 #ifdef DOUBLE_ALIGN_PARAMS
172 if ((uap
->share
== MAP_SHARED
)|| (uap
->share
== MAP_PRIVATE
)) {
173 newargs
.addr
= uap
->addr
;
174 newargs
.len
= (size_t)uap
->len
;
175 newargs
.prot
= uap
->prot
;
176 newargs
.flags
= uap
->share
;
177 newargs
.fd
= uap
->fd
;
178 newargs
.pos
= (off_t
)uap
->pos
;
179 return(mmap(curp
,&newargs
, retval
));
190 #ifdef DOUBLE_ALIGN_PARAMS
198 struct mmap_args
*uap
;
202 * Map in special device (must be SHARED) or file
205 register struct vnode
*vp
;
210 kern_return_t result
;
211 vm_offset_t user_addr
;
214 vm_object_offset_t file_pos
;
215 boolean_t find_space
, docow
;
221 user_addr
= (vm_offset_t
)uap
->addr
;
222 user_size
= (vm_size_t
) uap
->len
;
223 AUDIT_ARG(addr
, (void *)user_addr
);
224 AUDIT_ARG(len
, (int) user_size
);
225 AUDIT_ARG(fd
, uap
->fd
);
227 prot
= (uap
->prot
& VM_PROT_ALL
);
231 * The vm code does not have prototypes & compiler doesn't do the'
232 * the right thing when you cast 64bit value and pass it in function
233 * call. So here it is.
235 file_pos
= (vm_object_offset_t
)uap
->pos
;
238 /* make sure mapping fits into numeric range etc */
239 if ((file_pos
+ user_size
> (vm_object_offset_t
)-PAGE_SIZE_64
) ||
240 ((ssize_t
) uap
->len
< 0 )||
241 ((flags
& MAP_ANON
) && uap
->fd
!= -1))
245 * Align the file position to a page boundary,
246 * and save its page offset component.
248 pageoff
= ((vm_offset_t
)file_pos
& PAGE_MASK
);
249 file_pos
-= (vm_object_offset_t
)pageoff
;
252 /* Adjust size for rounding (on both ends). */
253 user_size
+= pageoff
; /* low end... */
254 user_size
= (vm_size_t
) round_page_32(user_size
); /* hi end */
258 * Check for illegal addresses. Watch out for address wrap... Note
259 * that VM_*_ADDRESS are not constants due to casts (argh).
261 if (flags
& MAP_FIXED
) {
263 * The specified address must have the same remainder
264 * as the file offset taken modulo PAGE_SIZE, so it
265 * should be aligned after adjustment by pageoff.
267 user_addr
-= pageoff
;
268 if (user_addr
& PAGE_MASK
)
270 /* Address range must be all in user VM space. */
271 if (VM_MAX_ADDRESS
> 0 && (user_addr
+ user_size
> VM_MAX_ADDRESS
))
273 if (VM_MIN_ADDRESS
> 0 && user_addr
< VM_MIN_ADDRESS
)
275 if (user_addr
+ user_size
< user_addr
)
279 /* DO not have apis to get this info, need to wait till then*/
281 * XXX for non-fixed mappings where no hint is provided or
282 * the hint would fall in the potential heap space,
283 * place it after the end of the largest possible heap.
285 * There should really be a pmap call to determine a reasonable
288 else if (addr
< round_page_32(p
->p_vmspace
->vm_daddr
+ MAXDSIZ
))
289 addr
= round_page_32(p
->p_vmspace
->vm_daddr
+ MAXDSIZ
);
294 if (flags
& MAP_ANON
) {
296 * Mapping blank space is trivial.
299 maxprot
= VM_PROT_ALL
;
304 * Mapping file, get fp for validation. Obtain vnode and make
305 * sure it is of appropriate type.
307 err
= fdgetf(p
, uap
->fd
, &fp
);
310 if(fp
->f_type
== DTYPE_PSXSHM
) {
311 uap
->addr
= (caddr_t
)user_addr
;
312 uap
->len
= user_size
;
316 return(pshm_mmap(p
, uap
, retval
, fp
, pageoff
));
319 if (fp
->f_type
!= DTYPE_VNODE
)
321 vp
= (struct vnode
*)fp
->f_data
;
323 if (vp
->v_type
!= VREG
&& vp
->v_type
!= VCHR
)
326 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
329 * XXX hack to handle use of /dev/zero to map anon memory (ala
332 if (vp
->v_type
== VCHR
|| vp
->v_type
== VSTR
) {
336 * Ensure that file and memory protections are
337 * compatible. Note that we only worry about
338 * writability if mapping is shared; in this case,
339 * current and max prot are dictated by the open file.
340 * XXX use the vnode instead? Problem is: what
341 * credentials do we use for determination? What if
342 * proc does a setuid?
344 maxprot
= VM_PROT_EXECUTE
; /* ??? */
345 if (fp
->f_flag
& FREAD
)
346 maxprot
|= VM_PROT_READ
;
347 else if (prot
& PROT_READ
)
350 * If we are sharing potential changes (either via
351 * MAP_SHARED or via the implicit sharing of character
352 * device mappings), and we are trying to get write
353 * permission although we opened it without asking
357 if ((flags
& MAP_SHARED
) != 0) {
358 if ((fp
->f_flag
& FWRITE
) != 0) {
365 (IMMUTABLE
|APPEND
)) == 0)
366 maxprot
|= VM_PROT_WRITE
;
367 else if (prot
& PROT_WRITE
)
369 } else if ((prot
& PROT_WRITE
) != 0)
372 maxprot
|= VM_PROT_WRITE
;
382 * We bend a little - round the start and end addresses
383 * to the nearest page boundary.
385 user_size
= round_page_32(user_size
);
387 if (file_pos
& PAGE_MASK_64
)
390 user_map
= current_map();
392 if ((flags
& MAP_FIXED
) == 0) {
394 user_addr
= round_page_32(user_addr
);
396 if (user_addr
!= trunc_page_32(user_addr
))
399 (void) vm_deallocate(user_map
, user_addr
, user_size
);
404 * Lookup/allocate object.
406 if (flags
& MAP_ANON
) {
408 * Unnamed anonymous regions always start at 0.
414 if (handle
== NULL
) {
418 #if defined(VM_PROT_READ_IS_EXEC)
419 if (prot
& VM_PROT_READ
)
420 prot
|= VM_PROT_EXECUTE
;
422 if (maxprot
& VM_PROT_READ
)
423 maxprot
|= VM_PROT_EXECUTE
;
426 result
= vm_allocate(user_map
, &user_addr
, user_size
, find_space
);
427 if (result
!= KERN_SUCCESS
)
430 result
= vm_protect(user_map
, user_addr
, user_size
, TRUE
, maxprot
);
431 if (result
!= KERN_SUCCESS
)
433 result
= vm_protect(user_map
, user_addr
, user_size
, FALSE
, prot
);
434 if (result
!= KERN_SUCCESS
)
438 UBCINFOCHECK("mmap", vp
);
439 pager
= (vm_pager_t
)ubc_getpager(vp
);
446 * FIXME: if we're writing the file we need a way to
447 * ensure that someone doesn't replace our R/W creds
448 * with ones that only work for read.
453 if ((flags
& (MAP_ANON
|MAP_SHARED
)) == 0) {
459 #if defined(VM_PROT_READ_IS_EXEC)
460 if (prot
& VM_PROT_READ
)
461 prot
|= VM_PROT_EXECUTE
;
463 if (maxprot
& VM_PROT_READ
)
464 maxprot
|= VM_PROT_EXECUTE
;
468 result
= vm_map_64(user_map
, &user_addr
, user_size
,
469 0, find_space
, pager
, file_pos
, docow
,
473 if (result
!= KERN_SUCCESS
)
479 if (flags
& MAP_SHARED
) {
480 result
= vm_inherit(user_map
, user_addr
, user_size
,
482 if (result
!= KERN_SUCCESS
) {
483 (void) vm_deallocate(user_map
, user_addr
, user_size
);
492 *fdflags(p
, uap
->fd
) |= UF_MAPPED
;
493 *retval
= (register_t
)(user_addr
+ pageoff
);
495 case KERN_INVALID_ADDRESS
:
498 case KERN_PROTECTION_FAILURE
:
512 msync(p
, uap
, retval
)
514 struct msync_args
*uap
;
518 vm_size_t size
, pageoff
;
522 vm_sync_t sync_flags
=0;
524 addr
= (vm_offset_t
) uap
->addr
;
525 pageoff
= (addr
& PAGE_MASK
);
528 size
= (vm_size_t
) round_page_32(size
);
531 if (addr
+ size
< addr
)
534 user_map
= current_map();
536 if ((flags
& (MS_ASYNC
|MS_SYNC
)) == (MS_ASYNC
|MS_SYNC
))
539 if ((flags
& (MS_ASYNC
|MS_INVALIDATE
)) == (MS_ASYNC
|MS_INVALIDATE
))
544 * We cannot support this properly without maintaining
545 * list all mmaps done. Cannot use vm_map_entry as they could be
546 * split or coalesced by indepenedant actions. So instead of
547 * inaccurate results, lets just return error as invalid size
550 return (EINVAL
); /* XXX breaks posix apps */
553 if (flags
& MS_KILLPAGES
)
554 sync_flags
|= VM_SYNC_KILLPAGES
;
555 if (flags
& MS_DEACTIVATE
)
556 sync_flags
|= VM_SYNC_DEACTIVATE
;
557 if (flags
& MS_INVALIDATE
)
558 sync_flags
|= VM_SYNC_INVALIDATE
;
560 if ( !(flags
& (MS_KILLPAGES
| MS_DEACTIVATE
))) {
561 if (flags
& MS_ASYNC
)
562 sync_flags
|= VM_SYNC_ASYNCHRONOUS
;
564 sync_flags
|= VM_SYNC_SYNCHRONOUS
;
566 rv
= vm_msync(user_map
, addr
, size
, sync_flags
);
571 case KERN_INVALID_ADDRESS
:
572 return (EINVAL
); /* Sun returns ENOMEM? */
586 /* Not yet implemented */
595 munmap(p
, uap
, retval
)
597 struct munmap_args
*uap
;
601 vm_offset_t user_addr
;
602 vm_size_t user_size
, pageoff
;
603 kern_return_t result
;
605 user_addr
= (vm_offset_t
) uap
->addr
;
606 user_size
= (vm_size_t
) uap
->len
;
608 AUDIT_ARG(addr
, (void *)user_addr
);
609 AUDIT_ARG(len
, (int) user_size
);
611 pageoff
= (user_addr
& PAGE_MASK
);
613 user_addr
-= pageoff
;
614 user_size
+= pageoff
;
615 user_size
= round_page_32(user_size
);
616 if (user_addr
+ user_size
< user_addr
)
622 /* Address range must be all in user VM space. */
623 if (VM_MAX_ADDRESS
> 0 && (user_addr
+ user_size
> VM_MAX_ADDRESS
))
625 if (VM_MIN_ADDRESS
> 0 && user_addr
< VM_MIN_ADDRESS
)
629 result
= vm_deallocate(current_map(), user_addr
, user_size
);
630 if (result
!= KERN_SUCCESS
) {
642 * XXX should vm_deallocate any regions mapped to this file
644 *fdflags(p
, fd
) &= ~UF_MAPPED
;
647 struct mprotect_args
{
653 mprotect(p
, uap
, retval
)
655 struct mprotect_args
*uap
;
658 register vm_prot_t prot
;
659 vm_offset_t user_addr
;
660 vm_size_t user_size
, pageoff
;
661 kern_return_t result
;
664 AUDIT_ARG(addr
, uap
->addr
);
665 AUDIT_ARG(len
, uap
->len
);
666 AUDIT_ARG(value
, uap
->prot
);
667 user_addr
= (vm_offset_t
) uap
->addr
;
668 user_size
= (vm_size_t
) uap
->len
;
669 prot
= (vm_prot_t
)(uap
->prot
& VM_PROT_ALL
);
673 #if defined(VM_PROT_READ_IS_EXEC)
674 if (prot
& VM_PROT_READ
)
675 prot
|= VM_PROT_EXECUTE
;
679 pageoff
= (user_addr
& PAGE_MASK
);
680 user_addr
-= pageoff
;
681 user_size
+= pageoff
;
682 user_size
= round_page_32(user_size
);
683 if (user_addr
+ user_size
< user_addr
)
686 user_map
= current_map();
688 result
= vm_map_protect(user_map
, user_addr
, user_addr
+user_size
, prot
,
693 case KERN_PROTECTION_FAILURE
:
700 struct minherit_args
{
707 minherit(p
, uap
, retval
)
709 struct minherit_args
*uap
;
713 vm_size_t size
, pageoff
;
714 register vm_inherit_t inherit
;
716 kern_return_t result
;
718 AUDIT_ARG(addr
, uap
->addr
);
719 AUDIT_ARG(len
, uap
->len
);
720 AUDIT_ARG(value
, uap
->inherit
);
721 addr
= (vm_offset_t
)uap
->addr
;
723 inherit
= uap
->inherit
;
725 pageoff
= (addr
& PAGE_MASK
);
728 size
= (vm_size_t
) round_page_32(size
);
729 if (addr
+ size
< addr
)
732 user_map
= current_map();
733 result
= vm_inherit(user_map
, addr
, size
,
738 case KERN_PROTECTION_FAILURE
:
744 struct madvise_args
{
751 madvise(p
, uap
, retval
)
753 struct madvise_args
*uap
;
757 vm_offset_t start
, end
;
758 vm_behavior_t new_behavior
;
759 kern_return_t result
;
762 * Check for illegal addresses. Watch out for address wrap... Note
763 * that VM_*_ADDRESS are not constants due to casts (argh).
765 if (VM_MAX_ADDRESS
> 0 &&
766 ((vm_offset_t
) uap
->addr
+ uap
->len
) > VM_MAX_ADDRESS
)
768 if (VM_MIN_ADDRESS
> 0 && uap
->addr
< VM_MIN_ADDRESS
)
771 if (((vm_offset_t
) uap
->addr
+ uap
->len
) < (vm_offset_t
) uap
->addr
)
775 * Since this routine is only advisory, we default to conservative
778 start
= trunc_page_32((vm_offset_t
) uap
->addr
);
779 end
= round_page_32((vm_offset_t
) uap
->addr
+ uap
->len
);
781 user_map
= current_map();
783 switch (uap
->behav
) {
785 new_behavior
= VM_BEHAVIOR_RANDOM
;
787 case MADV_SEQUENTIAL
:
788 new_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
791 new_behavior
= VM_BEHAVIOR_DEFAULT
;
794 new_behavior
= VM_BEHAVIOR_WILLNEED
;
797 new_behavior
= VM_BEHAVIOR_DONTNEED
;
803 result
= vm_behavior_set(user_map
, start
, end
, new_behavior
);
807 case KERN_INVALID_ADDRESS
:
814 struct mincore_args
{
821 mincore(p
, uap
, retval
)
823 struct mincore_args
*uap
;
826 vm_offset_t addr
, first_addr
;
831 int vecindex
, lastvecindex
;
840 * Make sure that the addresses presented are valid for user
843 first_addr
= addr
= trunc_page_32((vm_offset_t
) uap
->addr
);
844 end
= addr
+ (vm_size_t
)round_page_32(uap
->len
);
846 if (VM_MAX_ADDRESS
> 0 && end
> VM_MAX_ADDRESS
)
852 * Address of byte vector
859 * Do this on a map entry basis so that if the pages are not
860 * in the current processes address space, we can easily look
861 * up the pages elsewhere.
864 for(addr
; addr
< end
; addr
+= PAGE_SIZE
) {
866 ret
= vm_map_page_query(map
, addr
, &pqueryinfo
, &numref
);
867 if (ret
!= KERN_SUCCESS
)
870 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_PRESENT
)
871 mincoreinfo
|= MINCORE_INCORE
;
872 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_REF
)
873 mincoreinfo
|= MINCORE_REFERENCED
;
874 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_DIRTY
)
875 mincoreinfo
|= MINCORE_MODIFIED
;
879 * calculate index into user supplied byte vector
881 vecindex
= (addr
- first_addr
)>> PAGE_SHIFT
;
884 * If we have skipped map entries, we need to make sure that
885 * the byte vector is zeroed for those skipped entries.
887 while((lastvecindex
+ 1) < vecindex
) {
888 error
= subyte( vec
+ lastvecindex
, 0);
896 * Pass the page information to the user
898 error
= subyte( vec
+ vecindex
, mincoreinfo
);
902 lastvecindex
= vecindex
;
907 * Zero the last entries in the byte vector.
909 vecindex
= (end
- first_addr
) >> PAGE_SHIFT
;
910 while((lastvecindex
+ 1) < vecindex
) {
911 error
= subyte( vec
+ lastvecindex
, 0);
927 mlock(p
, uap
, retval
)
929 struct mlock_args
*uap
;
934 vm_size_t size
, pageoff
;
936 kern_return_t result
;
938 AUDIT_ARG(addr
, uap
->addr
);
939 AUDIT_ARG(len
, uap
->len
);
940 addr
= (vm_offset_t
) uap
->addr
;
943 pageoff
= (addr
& PAGE_MASK
);
946 size
= (vm_size_t
) round_page_32(size
);
948 /* disable wrap around */
949 if (addr
+ size
< addr
)
952 /* Hmm.. What am I going to do with this? */
953 if (atop(size
) + cnt
.v_wire_count
> vm_page_max_wired
)
955 #ifdef pmap_wired_count
956 if (size
+ ptoa(pmap_wired_count(vm_map_pmap(&p
->p_vmspace
->vm_map
))) >
957 p
->p_rlimit
[RLIMIT_MEMLOCK
].rlim_cur
)
960 error
= suser(p
->p_ucred
, &p
->p_acflag
);
966 user_map
= current_map();
969 result
= vm_map_wire(user_map
, addr
, (vm_offset_t
)(addr
+size
), VM_PROT_NONE
, TRUE
);
970 return (result
== KERN_SUCCESS
? 0 : ENOMEM
);
973 struct munlock_args
{
978 munlock(p
, uap
, retval
)
980 struct munlock_args
*uap
;
984 vm_size_t size
, pageoff
;
987 kern_return_t result
;
989 AUDIT_ARG(addr
, uap
->addr
);
990 AUDIT_ARG(len
, uap
->len
);
991 addr
= (vm_offset_t
) uap
->addr
;
994 pageoff
= (addr
& PAGE_MASK
);
997 size
= (vm_size_t
) round_page_32(size
);
999 /* disable wrap around */
1000 if (addr
+ size
< addr
)
1004 /* Hmm.. What am I going to do with this? */
1005 #ifndef pmap_wired_count
1006 error
= suser(p
->p_ucred
, &p
->p_acflag
);
1012 user_map
= current_map();
1015 result
= vm_wire(host_priv_self(), user_map
, addr
, size
, VM_PROT_NONE
);
1016 return (result
== KERN_SUCCESS
? 0 : ENOMEM
);
1020 struct mlockall_args
{
1027 struct mlockall_args
*uap
;
1032 struct munlockall_args
{
1039 struct munlockall_args
*uap
;
1046 struct obreak_args
{
1050 obreak(p
, uap
, retval
)
1052 struct obreak_args
*uap
;
1055 /* Not implemented, obsolete */
1071 /* CDY need to fix interface to allow user to map above 32 bits */
1072 /* USV: No! need to obsolete map_fd()! mmap() already supports 64 bits */
1078 boolean_t findspace
,
1082 boolean_t funnel_state
;
1084 AUDIT_MACH_SYSCALL_ENTER(AUE_MAPFD
);
1085 AUDIT_ARG(addr
, va
);
1088 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1090 ret
= map_fd_funneled( fd
, (vm_object_offset_t
)offset
,
1091 va
, findspace
, size
);
1093 (void) thread_funnel_set(kernel_flock
, FALSE
);
1095 AUDIT_MACH_SYSCALL_EXIT(ret
);
1102 vm_object_offset_t offset
,
1104 boolean_t findspace
,
1107 kern_return_t result
;
1111 vm_offset_t map_addr
=0;
1116 struct proc
*p
=(struct proc
*)current_proc();
1119 * Find the inode; verify that it's a regular file.
1122 err
= fdgetf(p
, fd
, &fp
);
1126 if (fp
->f_type
!= DTYPE_VNODE
)
1127 return(KERN_INVALID_ARGUMENT
);
1129 if (!(fp
->f_flag
& FREAD
))
1130 return (KERN_PROTECTION_FAILURE
);
1132 vp
= (struct vnode
*)fp
->f_data
;
1134 if (vp
->v_type
!= VREG
)
1135 return (KERN_INVALID_ARGUMENT
);
1137 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
1139 if (offset
& PAGE_MASK_64
) {
1140 printf("map_fd: file offset not page aligned(%d : %s)\n",p
->p_pid
, p
->p_comm
);
1141 return (KERN_INVALID_ARGUMENT
);
1143 map_size
= round_page_32(size
);
1146 * Allow user to map in a zero length file.
1149 return (KERN_SUCCESS
);
1153 UBCINFOCHECK("map_fd_funneled", vp
);
1154 pager
= (void *) ubc_getpager(vp
);
1156 return (KERN_FAILURE
);
1159 my_map
= current_map();
1163 &map_addr
, map_size
, (vm_offset_t
)0, TRUE
,
1164 pager
, offset
, TRUE
,
1165 VM_PROT_DEFAULT
, VM_PROT_ALL
,
1166 VM_INHERIT_DEFAULT
);
1167 if (result
!= KERN_SUCCESS
)
1172 vm_offset_t dst_addr
;
1175 if (copyin(va
, &dst_addr
, sizeof (dst_addr
)) ||
1176 trunc_page_32(dst_addr
) != dst_addr
) {
1177 (void) vm_map_remove(
1179 map_addr
, map_addr
+ map_size
,
1181 return (KERN_INVALID_ADDRESS
);
1184 result
= vm_map_copyin(
1186 map_addr
, map_size
, TRUE
,
1188 if (result
!= KERN_SUCCESS
) {
1190 (void) vm_map_remove(
1192 map_addr
, map_addr
+ map_size
,
1197 result
= vm_map_copy_overwrite(
1199 dst_addr
, tmp
, FALSE
);
1200 if (result
!= KERN_SUCCESS
) {
1201 vm_map_copy_discard(tmp
);
1205 if (copyout(&map_addr
, va
, sizeof (map_addr
))) {
1206 (void) vm_map_remove(
1208 map_addr
, map_addr
+ map_size
,
1210 return (KERN_INVALID_ADDRESS
);
1214 ubc_setcred(vp
, current_proc());
1217 return (KERN_SUCCESS
);