2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
40 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
41 * $FreeBSD: src/sys/vm/vm_mmap.c,v 1.108.2.6 2002/07/02 20:06:19 dillon Exp $
42 * $DragonFly: src/sys/vm/vm_mmap.c,v 1.30 2006/06/05 07:26:11 dillon Exp $
46 * Mapped file (mmap) interface to VM
49 #include <sys/param.h>
50 #include <sys/kernel.h>
51 #include <sys/systm.h>
52 #include <sys/sysproto.h>
53 #include <sys/filedesc.h>
54 #include <sys/kern_syscall.h>
56 #include <sys/resource.h>
57 #include <sys/resourcevar.h>
58 #include <sys/vnode.h>
59 #include <sys/fcntl.h>
64 #include <sys/vmmeter.h>
65 #include <sys/sysctl.h>
68 #include <vm/vm_param.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_pager.h>
75 #include <vm/vm_pageout.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_page.h>
78 #include <vm/vm_kern.h>
80 #include <sys/file2.h>
81 #include <sys/thread2.h>
83 static int max_proc_mmap
;
84 SYSCTL_INT(_vm
, OID_AUTO
, max_proc_mmap
, CTLFLAG_RW
, &max_proc_mmap
, 0, "");
87 * Set the maximum number of vm_map_entry structures per process. Roughly
88 * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
89 * of our KVM malloc space still results in generous limits. We want a
90 * default that is good enough to prevent the kernel running out of resources
91 * if attacked from compromised user account but generous enough such that
92 * multi-threaded processes are not unduly inconvenienced.
95 static void vmmapentry_rsrc_init (void *);
96 SYSINIT(vmmersrc
, SI_SUB_KVM_RSRC
, SI_ORDER_FIRST
, vmmapentry_rsrc_init
, NULL
)
99 vmmapentry_rsrc_init(void *dummy
)
101 max_proc_mmap
= (VM_MAX_KERNEL_ADDRESS
- VM_MIN_KERNEL_ADDRESS
) /
102 sizeof(struct vm_map_entry
);
103 max_proc_mmap
/= 100;
108 sys_sbrk(struct sbrk_args
*uap
)
110 /* Not yet implemented */
115 * sstk_args(int incr)
119 sys_sstk(struct sstk_args
*uap
)
121 /* Not yet implemented */
126 * mmap_args(void *addr, size_t len, int prot, int flags, int fd,
127 * long pad, off_t pos)
129 * Memory Map (mmap) system call. Note that the file offset
130 * and address are allowed to be NOT page aligned, though if
131 * the MAP_FIXED flag it set, both must have the same remainder
132 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
133 * page-aligned, the actual mapping starts at trunc_page(addr)
134 * and the return value is adjusted up by the page offset.
136 * Generally speaking, only character devices which are themselves
137 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
138 * there would be no cache coherency between a descriptor and a VM mapping
139 * both to the same character device.
141 * Block devices can be mmap'd no matter what they represent. Cache coherency
142 * is maintained as long as you do not write directly to the underlying
147 kern_mmap(caddr_t uaddr
, size_t ulen
, int uprot
, int uflags
, int fd
,
148 off_t upos
, void **res
)
150 struct thread
*td
= curthread
;
151 struct proc
*p
= td
->td_proc
;
152 struct file
*fp
= NULL
;
155 vm_size_t size
, pageoff
;
156 vm_prot_t prot
, maxprot
;
159 int disablexworkaround
;
161 struct vmspace
*vms
= p
->p_vmspace
;
166 addr
= (vm_offset_t
) uaddr
;
168 prot
= uprot
& VM_PROT_ALL
;
172 /* make sure mapping fits into numeric range etc */
173 if ((ssize_t
) ulen
< 0 ||
174 ((flags
& MAP_ANON
) && fd
!= -1))
177 if (flags
& MAP_STACK
) {
179 ((prot
& (PROT_READ
| PROT_WRITE
)) != (PROT_READ
| PROT_WRITE
)))
186 * Align the file position to a page boundary,
187 * and save its page offset component.
189 pageoff
= (pos
& PAGE_MASK
);
192 /* Adjust size for rounding (on both ends). */
193 size
+= pageoff
; /* low end... */
194 size
= (vm_size_t
) round_page(size
); /* hi end */
197 * Check for illegal addresses. Watch out for address wrap... Note
198 * that VM_*_ADDRESS are not constants due to casts (argh).
200 if (flags
& MAP_FIXED
) {
202 * The specified address must have the same remainder
203 * as the file offset taken modulo PAGE_SIZE, so it
204 * should be aligned after adjustment by pageoff.
207 if (addr
& PAGE_MASK
)
209 /* Address range must be all in user VM space. */
210 if (VM_MAXUSER_ADDRESS
> 0 && addr
+ size
> VM_MAXUSER_ADDRESS
)
213 if (VM_MIN_ADDRESS
> 0 && addr
< VM_MIN_ADDRESS
)
216 if (addr
+ size
< addr
)
220 * XXX for non-fixed mappings where no hint is provided or
221 * the hint would fall in the potential heap space,
222 * place it after the end of the largest possible heap.
224 * There should really be a pmap call to determine a reasonable
227 else if (addr
== 0 ||
228 (addr
>= round_page((vm_offset_t
)vms
->vm_taddr
) &&
229 addr
< round_page((vm_offset_t
)vms
->vm_daddr
+ maxdsiz
)))
230 addr
= round_page((vm_offset_t
)vms
->vm_daddr
+ maxdsiz
);
232 if (flags
& MAP_ANON
) {
234 * Mapping blank space is trivial.
237 maxprot
= VM_PROT_ALL
;
241 * Mapping file, get fp for validation. Obtain vnode and make
242 * sure it is of appropriate type.
244 fp
= holdfp(p
->p_fd
, fd
, -1);
247 if (fp
->f_type
!= DTYPE_VNODE
) {
252 * POSIX shared-memory objects are defined to have
253 * kernel persistence, and are not defined to support
254 * read(2)/write(2) -- or even open(2). Thus, we can
255 * use MAP_ASYNC to trade on-disk coherence for speed.
256 * The shm_open(3) library routine turns on the FPOSIXSHM
257 * flag to request this behavior.
259 if (fp
->f_flag
& FPOSIXSHM
)
261 vp
= (struct vnode
*) fp
->f_data
;
262 if (vp
->v_type
!= VREG
&& vp
->v_type
!= VCHR
) {
266 if (vp
->v_type
== VREG
) {
268 * Get the proper underlying object
270 if ((obj
= vp
->v_object
) == NULL
) {
274 KKASSERT(vp
== (struct vnode
*)obj
->handle
);
278 * XXX hack to handle use of /dev/zero to map anon memory (ala
281 if (vp
->v_type
== VCHR
&& iszerodev(vp
->v_rdev
)) {
283 maxprot
= VM_PROT_ALL
;
288 * cdevs does not provide private mappings of any kind.
291 * However, for XIG X server to continue to work,
292 * we should allow the superuser to do it anyway.
293 * We only allow it at securelevel < 1.
294 * (Because the XIG X server writes directly to video
295 * memory via /dev/mem, it should never work at any
297 * XXX this will have to go
299 if (securelevel
>= 1)
300 disablexworkaround
= 1;
302 disablexworkaround
= suser(td
);
303 if (vp
->v_type
== VCHR
&& disablexworkaround
&&
304 (flags
& (MAP_PRIVATE
|MAP_COPY
))) {
309 * Ensure that file and memory protections are
310 * compatible. Note that we only worry about
311 * writability if mapping is shared; in this case,
312 * current and max prot are dictated by the open file.
313 * XXX use the vnode instead? Problem is: what
314 * credentials do we use for determination? What if
315 * proc does a setuid?
317 maxprot
= VM_PROT_EXECUTE
; /* ??? */
318 if (fp
->f_flag
& FREAD
) {
319 maxprot
|= VM_PROT_READ
;
320 } else if (prot
& PROT_READ
) {
325 * If we are sharing potential changes (either via
326 * MAP_SHARED or via the implicit sharing of character
327 * device mappings), and we are trying to get write
328 * permission although we opened it without asking
329 * for it, bail out. Check for superuser, only if
330 * we're at securelevel < 1, to allow the XIG X server
331 * to continue to work.
334 if ((flags
& MAP_SHARED
) != 0 ||
335 (vp
->v_type
== VCHR
&& disablexworkaround
)) {
336 if ((fp
->f_flag
& FWRITE
) != 0) {
338 if ((error
= VOP_GETATTR(vp
, &va
))) {
342 (IMMUTABLE
|APPEND
)) == 0) {
343 maxprot
|= VM_PROT_WRITE
;
344 } else if (prot
& PROT_WRITE
) {
348 } else if ((prot
& PROT_WRITE
) != 0) {
353 maxprot
|= VM_PROT_WRITE
;
360 * Do not allow more then a certain number of vm_map_entry structures
361 * per process. Scale with the number of rforks sharing the map
362 * to make the limit reasonable for threads.
365 vms
->vm_map
.nentries
>= max_proc_mmap
* vms
->vm_refcnt
) {
370 error
= vm_mmap(&vms
->vm_map
, &addr
, size
, prot
, maxprot
,
373 *res
= (void *)(addr
+ pageoff
);
381 sys_mmap(struct mmap_args
*uap
)
385 error
= kern_mmap(uap
->addr
, uap
->len
, uap
->prot
, uap
->flags
,
386 uap
->fd
, uap
->pos
, &uap
->sysmsg_resultp
);
392 * msync_args(void *addr, int len, int flags)
395 sys_msync(struct msync_args
*uap
)
397 struct proc
*p
= curproc
;
399 vm_size_t size
, pageoff
;
404 addr
= (vm_offset_t
) uap
->addr
;
408 pageoff
= (addr
& PAGE_MASK
);
411 size
= (vm_size_t
) round_page(size
);
412 if (addr
+ size
< addr
)
415 if ((flags
& (MS_ASYNC
|MS_INVALIDATE
)) == (MS_ASYNC
|MS_INVALIDATE
))
418 map
= &p
->p_vmspace
->vm_map
;
421 * XXX Gak! If size is zero we are supposed to sync "all modified
422 * pages with the region containing addr". Unfortunately, we don't
423 * really keep track of individual mmaps so we approximate by flushing
424 * the range of the map entry containing addr. This can be incorrect
425 * if the region splits or is coalesced with a neighbor.
428 vm_map_entry_t entry
;
430 vm_map_lock_read(map
);
431 rv
= vm_map_lookup_entry(map
, addr
, &entry
);
432 vm_map_unlock_read(map
);
436 size
= entry
->end
- entry
->start
;
440 * Clean the pages and interpret the return value.
442 rv
= vm_map_clean(map
, addr
, addr
+ size
, (flags
& MS_ASYNC
) == 0,
443 (flags
& MS_INVALIDATE
) != 0);
448 case KERN_INVALID_ADDRESS
:
449 return (EINVAL
); /* Sun returns ENOMEM? */
460 * munmap_args(void *addr, size_t len)
463 sys_munmap(struct munmap_args
*uap
)
465 struct proc
*p
= curproc
;
467 vm_size_t size
, pageoff
;
470 addr
= (vm_offset_t
) uap
->addr
;
473 pageoff
= (addr
& PAGE_MASK
);
476 size
= (vm_size_t
) round_page(size
);
477 if (addr
+ size
< addr
)
484 * Check for illegal addresses. Watch out for address wrap... Note
485 * that VM_*_ADDRESS are not constants due to casts (argh).
487 if (VM_MAXUSER_ADDRESS
> 0 && addr
+ size
> VM_MAXUSER_ADDRESS
)
490 if (VM_MIN_ADDRESS
> 0 && addr
< VM_MIN_ADDRESS
)
493 map
= &p
->p_vmspace
->vm_map
;
495 * Make sure entire range is allocated.
497 if (!vm_map_check_protection(map
, addr
, addr
+ size
, VM_PROT_NONE
))
499 /* returns nothing but KERN_SUCCESS anyway */
500 vm_map_remove(map
, addr
, addr
+ size
);
505 * mprotect_args(const void *addr, size_t len, int prot)
508 sys_mprotect(struct mprotect_args
*uap
)
510 struct proc
*p
= curproc
;
512 vm_size_t size
, pageoff
;
515 addr
= (vm_offset_t
) uap
->addr
;
517 prot
= uap
->prot
& VM_PROT_ALL
;
518 #if defined(VM_PROT_READ_IS_EXEC)
519 if (prot
& VM_PROT_READ
)
520 prot
|= VM_PROT_EXECUTE
;
523 pageoff
= (addr
& PAGE_MASK
);
526 size
= (vm_size_t
) round_page(size
);
527 if (addr
+ size
< addr
)
530 switch (vm_map_protect(&p
->p_vmspace
->vm_map
, addr
, addr
+ size
, prot
,
534 case KERN_PROTECTION_FAILURE
:
541 * minherit_args(void *addr, size_t len, int inherit)
544 sys_minherit(struct minherit_args
*uap
)
546 struct proc
*p
= curproc
;
548 vm_size_t size
, pageoff
;
549 vm_inherit_t inherit
;
551 addr
= (vm_offset_t
)uap
->addr
;
553 inherit
= uap
->inherit
;
555 pageoff
= (addr
& PAGE_MASK
);
558 size
= (vm_size_t
) round_page(size
);
559 if (addr
+ size
< addr
)
562 switch (vm_map_inherit(&p
->p_vmspace
->vm_map
, addr
, addr
+size
,
566 case KERN_PROTECTION_FAILURE
:
573 * madvise_args(void *addr, size_t len, int behav)
577 sys_madvise(struct madvise_args
*uap
)
579 struct proc
*p
= curproc
;
580 vm_offset_t start
, end
;
583 * Check for illegal behavior
585 if (uap
->behav
< 0 || uap
->behav
> MADV_CORE
)
588 * Check for illegal addresses. Watch out for address wrap... Note
589 * that VM_*_ADDRESS are not constants due to casts (argh).
591 if (VM_MAXUSER_ADDRESS
> 0 &&
592 ((vm_offset_t
) uap
->addr
+ uap
->len
) > VM_MAXUSER_ADDRESS
)
595 if (VM_MIN_ADDRESS
> 0 && uap
->addr
< VM_MIN_ADDRESS
)
598 if (((vm_offset_t
) uap
->addr
+ uap
->len
) < (vm_offset_t
) uap
->addr
)
602 * Since this routine is only advisory, we default to conservative
605 start
= trunc_page((vm_offset_t
) uap
->addr
);
606 end
= round_page((vm_offset_t
) uap
->addr
+ uap
->len
);
608 if (vm_map_madvise(&p
->p_vmspace
->vm_map
, start
, end
, uap
->behav
))
614 * mincore_args(const void *addr, size_t len, char *vec)
618 sys_mincore(struct mincore_args
*uap
)
620 struct proc
*p
= curproc
;
621 vm_offset_t addr
, first_addr
;
622 vm_offset_t end
, cend
;
627 int vecindex
, lastvecindex
;
628 vm_map_entry_t current
;
629 vm_map_entry_t entry
;
631 unsigned int timestamp
;
634 * Make sure that the addresses presented are valid for user
637 first_addr
= addr
= trunc_page((vm_offset_t
) uap
->addr
);
638 end
= addr
+ (vm_size_t
)round_page(uap
->len
);
639 if (VM_MAXUSER_ADDRESS
> 0 && end
> VM_MAXUSER_ADDRESS
)
645 * Address of byte vector
649 map
= &p
->p_vmspace
->vm_map
;
650 pmap
= vmspace_pmap(p
->p_vmspace
);
652 vm_map_lock_read(map
);
654 timestamp
= map
->timestamp
;
656 if (!vm_map_lookup_entry(map
, addr
, &entry
))
660 * Do this on a map entry basis so that if the pages are not
661 * in the current processes address space, we can easily look
662 * up the pages elsewhere.
666 (current
!= &map
->header
) && (current
->start
< end
);
667 current
= current
->next
) {
670 * ignore submaps (for now) or null objects
672 if ((current
->eflags
& MAP_ENTRY_IS_SUB_MAP
) ||
673 current
->object
.vm_object
== NULL
)
677 * limit this scan to the current map entry and the
678 * limits for the mincore call
680 if (addr
< current
->start
)
681 addr
= current
->start
;
687 * scan this entry one page at a time
689 while (addr
< cend
) {
691 * Check pmap first, it is likely faster, also
692 * it can provide info as to whether we are the
693 * one referencing or modifying the page.
695 mincoreinfo
= pmap_mincore(pmap
, addr
);
702 * calculate the page index into the object
704 offset
= current
->offset
+ (addr
- current
->start
);
705 pindex
= OFF_TO_IDX(offset
);
708 * if the page is resident, then gather
709 * information about it. spl protection is
710 * required to maintain the object
711 * association. And XXX what if the page is
712 * busy? What's the deal with that?
715 m
= vm_page_lookup(current
->object
.vm_object
,
718 mincoreinfo
= MINCORE_INCORE
;
721 mincoreinfo
|= MINCORE_MODIFIED_OTHER
;
722 if ((m
->flags
& PG_REFERENCED
) ||
723 pmap_ts_referenced(m
)) {
724 vm_page_flag_set(m
, PG_REFERENCED
);
725 mincoreinfo
|= MINCORE_REFERENCED_OTHER
;
732 * subyte may page fault. In case it needs to modify
733 * the map, we release the lock.
735 vm_map_unlock_read(map
);
738 * calculate index into user supplied byte vector
740 vecindex
= OFF_TO_IDX(addr
- first_addr
);
743 * If we have skipped map entries, we need to make sure that
744 * the byte vector is zeroed for those skipped entries.
746 while((lastvecindex
+ 1) < vecindex
) {
747 error
= subyte( vec
+ lastvecindex
, 0);
755 * Pass the page information to the user
757 error
= subyte( vec
+ vecindex
, mincoreinfo
);
763 * If the map has changed, due to the subyte, the previous
764 * output may be invalid.
766 vm_map_lock_read(map
);
767 if (timestamp
!= map
->timestamp
)
770 lastvecindex
= vecindex
;
776 * subyte may page fault. In case it needs to modify
777 * the map, we release the lock.
779 vm_map_unlock_read(map
);
782 * Zero the last entries in the byte vector.
784 vecindex
= OFF_TO_IDX(end
- first_addr
);
785 while((lastvecindex
+ 1) < vecindex
) {
786 error
= subyte( vec
+ lastvecindex
, 0);
794 * If the map has changed, due to the subyte, the previous
795 * output may be invalid.
797 vm_map_lock_read(map
);
798 if (timestamp
!= map
->timestamp
)
800 vm_map_unlock_read(map
);
806 * mlock_args(const void *addr, size_t len)
809 sys_mlock(struct mlock_args
*uap
)
812 vm_size_t size
, pageoff
;
814 struct proc
*p
= curproc
;
816 addr
= (vm_offset_t
) uap
->addr
;
819 pageoff
= (addr
& PAGE_MASK
);
822 size
= (vm_size_t
) round_page(size
);
824 /* disable wrap around */
825 if (addr
+ size
< addr
)
828 if (atop(size
) + vmstats
.v_wire_count
> vm_page_max_wired
)
831 #ifdef pmap_wired_count
832 if (size
+ ptoa(pmap_wired_count(vm_map_pmap(&p
->p_vmspace
->vm_map
))) >
833 p
->p_rlimit
[RLIMIT_MEMLOCK
].rlim_cur
)
836 error
= suser_cred(p
->p_ucred
, 0);
841 error
= vm_map_unwire(&p
->p_vmspace
->vm_map
, addr
, addr
+ size
, FALSE
);
842 return (error
== KERN_SUCCESS
? 0 : ENOMEM
);
846 * mlockall_args(int how)
849 sys_mlockall(struct mlockall_args
*uap
)
855 * munlockall_args(void)
858 sys_munlockall(struct munlockall_args
*uap
)
864 * munlock_args(const void *addr, size_t len)
867 sys_munlock(struct munlock_args
*uap
)
869 struct thread
*td
= curthread
;
870 struct proc
*p
= td
->td_proc
;
872 vm_size_t size
, pageoff
;
875 addr
= (vm_offset_t
) uap
->addr
;
878 pageoff
= (addr
& PAGE_MASK
);
881 size
= (vm_size_t
) round_page(size
);
883 /* disable wrap around */
884 if (addr
+ size
< addr
)
887 #ifndef pmap_wired_count
893 error
= vm_map_unwire(&p
->p_vmspace
->vm_map
, addr
, addr
+ size
, TRUE
);
894 return (error
== KERN_SUCCESS
? 0 : ENOMEM
);
898 * Internal version of mmap.
899 * Currently used by mmap, exec, and sys5 shared memory.
900 * Handle is either a vnode pointer or NULL for MAP_ANON.
903 vm_mmap(vm_map_t map
, vm_offset_t
*addr
, vm_size_t size
, vm_prot_t prot
,
904 vm_prot_t maxprot
, int flags
,
910 struct vnode
*vp
= NULL
;
912 int rv
= KERN_SUCCESS
;
915 struct thread
*td
= curthread
; /* XXX */
916 struct proc
*p
= td
->td_proc
;
923 objsize
= size
= round_page(size
);
925 if (p
->p_vmspace
->vm_map
.size
+ size
>
926 p
->p_rlimit
[RLIMIT_VMEM
].rlim_cur
) {
931 * We currently can only deal with page aligned file offsets.
932 * The check is here rather than in the syscall because the
933 * kernel calls this function internally for other mmaping
934 * operations (such as in exec) and non-aligned offsets will
935 * cause pmap inconsistencies...so we want to be sure to
936 * disallow this in all cases.
938 if (foff
& PAGE_MASK
)
941 if ((flags
& MAP_FIXED
) == 0) {
943 *addr
= round_page(*addr
);
945 if (*addr
!= trunc_page(*addr
))
948 vm_map_remove(map
, *addr
, *addr
+ size
);
952 * Lookup/allocate object.
954 if (flags
& MAP_ANON
) {
957 * Unnamed anonymous regions always start at 0.
962 vp
= (struct vnode
*) handle
;
963 if (vp
->v_type
== VCHR
) {
965 handle
= (void *)(intptr_t)vp
->v_rdev
;
970 error
= VOP_GETATTR(vp
, &vat
);
973 objsize
= vat
.va_size
;
976 * if it is a regular file without any references
977 * we do not need to sync it.
979 if (vp
->v_type
== VREG
&& vat
.va_nlink
== 0) {
985 if (handle
== NULL
) {
989 object
= vm_pager_allocate(type
, handle
, objsize
, prot
, foff
);
991 return (type
== OBJT_DEVICE
? EINVAL
: ENOMEM
);
992 docow
= MAP_PREFAULT_PARTIAL
;
996 * Force device mappings to be shared.
998 if (type
== OBJT_DEVICE
|| type
== OBJT_PHYS
) {
999 flags
&= ~(MAP_PRIVATE
|MAP_COPY
);
1000 flags
|= MAP_SHARED
;
1003 if ((flags
& (MAP_ANON
|MAP_SHARED
)) == 0)
1004 docow
|= MAP_COPY_ON_WRITE
;
1005 if (flags
& MAP_NOSYNC
)
1006 docow
|= MAP_DISABLE_SYNCER
;
1007 if (flags
& MAP_NOCORE
)
1008 docow
|= MAP_DISABLE_COREDUMP
;
1010 #if defined(VM_PROT_READ_IS_EXEC)
1011 if (prot
& VM_PROT_READ
)
1012 prot
|= VM_PROT_EXECUTE
;
1014 if (maxprot
& VM_PROT_READ
)
1015 maxprot
|= VM_PROT_EXECUTE
;
1019 *addr
= pmap_addr_hint(object
, *addr
, size
);
1022 if (flags
& MAP_STACK
)
1023 rv
= vm_map_stack (map
, *addr
, size
, prot
,
1026 rv
= vm_map_find(map
, object
, foff
, addr
, size
, fitit
,
1027 prot
, maxprot
, docow
);
1029 if (rv
!= KERN_SUCCESS
) {
1031 * Lose the object reference. Will destroy the
1032 * object if it's an unnamed anonymous mapping
1033 * or named anonymous without other references.
1035 vm_object_deallocate(object
);
1040 * Shared memory is also shared with children.
1042 if (flags
& (MAP_SHARED
|MAP_INHERIT
)) {
1043 rv
= vm_map_inherit(map
, *addr
, *addr
+ size
, VM_INHERIT_SHARE
);
1044 if (rv
!= KERN_SUCCESS
) {
1045 vm_map_remove(map
, *addr
, *addr
+ size
);
1053 case KERN_INVALID_ADDRESS
:
1056 case KERN_PROTECTION_FAILURE
: